Add support for re.all (#2980)
[cvc5.git] / src / parser / smt2 / smt2.cpp
1 /********************* */
2 /*! \file smt2.cpp
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Kshitij Bansal, Morgan Deters
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
11 **
12 ** \brief Definitions of SMT2 constants.
13 **
14 ** Definitions of SMT2 constants.
15 **/
16 #include "parser/smt2/smt2.h"
17
18 #include "api/cvc4cpp.h"
19 #include "expr/type.h"
20 #include "options/options.h"
21 #include "parser/antlr_input.h"
22 #include "parser/parser.h"
23 #include "parser/smt1/smt1.h"
24 #include "parser/smt2/smt2_input.h"
25 #include "printer/sygus_print_callback.h"
26 #include "smt/command.h"
27 #include "util/bitvector.h"
28
29 #include <algorithm>
30
31 // ANTLR defines these, which is really bad!
32 #undef true
33 #undef false
34
35 namespace CVC4 {
36 namespace parser {
37
38 Smt2::Smt2(api::Solver* solver, Input* input, bool strictMode, bool parseOnly)
39 : Parser(solver, input, strictMode, parseOnly), d_logicSet(false)
40 {
41 if (!strictModeEnabled())
42 {
43 addTheory(Smt2::THEORY_CORE);
44 }
45 }
46
47 void Smt2::addArithmeticOperators() {
48 Parser::addOperator(kind::PLUS);
49 Parser::addOperator(kind::MINUS);
50 Parser::addOperator(kind::UMINUS);
51 Parser::addOperator(kind::MULT);
52 Parser::addOperator(kind::LT);
53 Parser::addOperator(kind::LEQ);
54 Parser::addOperator(kind::GT);
55 Parser::addOperator(kind::GEQ);
56
57 // NOTE: this operator is non-standard
58 addOperator(kind::POW, "^");
59 }
60
61 void Smt2::addTranscendentalOperators()
62 {
63 addOperator(kind::EXPONENTIAL, "exp");
64 addOperator(kind::SINE, "sin");
65 addOperator(kind::COSINE, "cos");
66 addOperator(kind::TANGENT, "tan");
67 addOperator(kind::COSECANT, "csc");
68 addOperator(kind::SECANT, "sec");
69 addOperator(kind::COTANGENT, "cot");
70 addOperator(kind::ARCSINE, "arcsin");
71 addOperator(kind::ARCCOSINE, "arccos");
72 addOperator(kind::ARCTANGENT, "arctan");
73 addOperator(kind::ARCCOSECANT, "arccsc");
74 addOperator(kind::ARCSECANT, "arcsec");
75 addOperator(kind::ARCCOTANGENT, "arccot");
76 addOperator(kind::SQRT, "sqrt");
77 }
78
79 void Smt2::addBitvectorOperators() {
80 addOperator(kind::BITVECTOR_CONCAT, "concat");
81 addOperator(kind::BITVECTOR_NOT, "bvnot");
82 addOperator(kind::BITVECTOR_AND, "bvand");
83 addOperator(kind::BITVECTOR_OR, "bvor");
84 addOperator(kind::BITVECTOR_NEG, "bvneg");
85 addOperator(kind::BITVECTOR_PLUS, "bvadd");
86 addOperator(kind::BITVECTOR_MULT, "bvmul");
87 addOperator(kind::BITVECTOR_UDIV, "bvudiv");
88 addOperator(kind::BITVECTOR_UREM, "bvurem");
89 addOperator(kind::BITVECTOR_SHL, "bvshl");
90 addOperator(kind::BITVECTOR_LSHR, "bvlshr");
91 addOperator(kind::BITVECTOR_ULT, "bvult");
92 addOperator(kind::BITVECTOR_NAND, "bvnand");
93 addOperator(kind::BITVECTOR_NOR, "bvnor");
94 addOperator(kind::BITVECTOR_XOR, "bvxor");
95 addOperator(kind::BITVECTOR_XNOR, "bvxnor");
96 addOperator(kind::BITVECTOR_COMP, "bvcomp");
97 addOperator(kind::BITVECTOR_SUB, "bvsub");
98 addOperator(kind::BITVECTOR_SDIV, "bvsdiv");
99 addOperator(kind::BITVECTOR_SREM, "bvsrem");
100 addOperator(kind::BITVECTOR_SMOD, "bvsmod");
101 addOperator(kind::BITVECTOR_ASHR, "bvashr");
102 addOperator(kind::BITVECTOR_ULE, "bvule");
103 addOperator(kind::BITVECTOR_UGT, "bvugt");
104 addOperator(kind::BITVECTOR_UGE, "bvuge");
105 addOperator(kind::BITVECTOR_SLT, "bvslt");
106 addOperator(kind::BITVECTOR_SLE, "bvsle");
107 addOperator(kind::BITVECTOR_SGT, "bvsgt");
108 addOperator(kind::BITVECTOR_SGE, "bvsge");
109 addOperator(kind::BITVECTOR_REDOR, "bvredor");
110 addOperator(kind::BITVECTOR_REDAND, "bvredand");
111
112 Parser::addOperator(kind::BITVECTOR_BITOF);
113 Parser::addOperator(kind::BITVECTOR_EXTRACT);
114 Parser::addOperator(kind::BITVECTOR_REPEAT);
115 Parser::addOperator(kind::BITVECTOR_ZERO_EXTEND);
116 Parser::addOperator(kind::BITVECTOR_SIGN_EXTEND);
117 Parser::addOperator(kind::BITVECTOR_ROTATE_LEFT);
118 Parser::addOperator(kind::BITVECTOR_ROTATE_RIGHT);
119
120 Parser::addOperator(kind::INT_TO_BITVECTOR);
121 Parser::addOperator(kind::BITVECTOR_TO_NAT);
122 }
123
124 void Smt2::addStringOperators() {
125 defineVar("re.all",
126 getSolver()
127 ->mkTerm(api::REGEXP_STAR, getSolver()->mkRegexpSigma())
128 .getExpr());
129
130 addOperator(kind::STRING_CONCAT, "str.++");
131 addOperator(kind::STRING_LENGTH, "str.len");
132 addOperator(kind::STRING_SUBSTR, "str.substr" );
133 addOperator(kind::STRING_STRCTN, "str.contains" );
134 addOperator(kind::STRING_CHARAT, "str.at" );
135 addOperator(kind::STRING_STRIDOF, "str.indexof" );
136 addOperator(kind::STRING_STRREPL, "str.replace" );
137 addOperator(kind::STRING_STRREPLALL, "str.replaceall");
138 addOperator(kind::STRING_PREFIX, "str.prefixof" );
139 addOperator(kind::STRING_SUFFIX, "str.suffixof" );
140 // at the moment, we only use this syntax for smt2.6.1
141 if (getLanguage() == language::input::LANG_SMTLIB_V2_6_1)
142 {
143 addOperator(kind::STRING_ITOS, "str.from-int");
144 addOperator(kind::STRING_STOI, "str.to-int");
145 addOperator(kind::STRING_IN_REGEXP, "str.in-re");
146 addOperator(kind::STRING_TO_REGEXP, "str.to-re");
147 }
148 else
149 {
150 addOperator(kind::STRING_ITOS, "int.to.str");
151 addOperator(kind::STRING_STOI, "str.to.int");
152 addOperator(kind::STRING_IN_REGEXP, "str.in.re");
153 addOperator(kind::STRING_TO_REGEXP, "str.to.re");
154 }
155
156 addOperator(kind::REGEXP_CONCAT, "re.++");
157 addOperator(kind::REGEXP_UNION, "re.union");
158 addOperator(kind::REGEXP_INTER, "re.inter");
159 addOperator(kind::REGEXP_STAR, "re.*");
160 addOperator(kind::REGEXP_PLUS, "re.+");
161 addOperator(kind::REGEXP_OPT, "re.opt");
162 addOperator(kind::REGEXP_RANGE, "re.range");
163 addOperator(kind::REGEXP_LOOP, "re.loop");
164 addOperator(kind::STRING_CODE, "str.code");
165 addOperator(kind::STRING_LT, "str.<");
166 addOperator(kind::STRING_LEQ, "str.<=");
167 }
168
169 void Smt2::addFloatingPointOperators() {
170 addOperator(kind::FLOATINGPOINT_FP, "fp");
171 addOperator(kind::FLOATINGPOINT_EQ, "fp.eq");
172 addOperator(kind::FLOATINGPOINT_ABS, "fp.abs");
173 addOperator(kind::FLOATINGPOINT_NEG, "fp.neg");
174 addOperator(kind::FLOATINGPOINT_PLUS, "fp.add");
175 addOperator(kind::FLOATINGPOINT_SUB, "fp.sub");
176 addOperator(kind::FLOATINGPOINT_MULT, "fp.mul");
177 addOperator(kind::FLOATINGPOINT_DIV, "fp.div");
178 addOperator(kind::FLOATINGPOINT_FMA, "fp.fma");
179 addOperator(kind::FLOATINGPOINT_SQRT, "fp.sqrt");
180 addOperator(kind::FLOATINGPOINT_REM, "fp.rem");
181 addOperator(kind::FLOATINGPOINT_RTI, "fp.roundToIntegral");
182 addOperator(kind::FLOATINGPOINT_MIN, "fp.min");
183 addOperator(kind::FLOATINGPOINT_MAX, "fp.max");
184 addOperator(kind::FLOATINGPOINT_LEQ, "fp.leq");
185 addOperator(kind::FLOATINGPOINT_LT, "fp.lt");
186 addOperator(kind::FLOATINGPOINT_GEQ, "fp.geq");
187 addOperator(kind::FLOATINGPOINT_GT, "fp.gt");
188 addOperator(kind::FLOATINGPOINT_ISN, "fp.isNormal");
189 addOperator(kind::FLOATINGPOINT_ISSN, "fp.isSubnormal");
190 addOperator(kind::FLOATINGPOINT_ISZ, "fp.isZero");
191 addOperator(kind::FLOATINGPOINT_ISINF, "fp.isInfinite");
192 addOperator(kind::FLOATINGPOINT_ISNAN, "fp.isNaN");
193 addOperator(kind::FLOATINGPOINT_ISNEG, "fp.isNegative");
194 addOperator(kind::FLOATINGPOINT_ISPOS, "fp.isPositive");
195 addOperator(kind::FLOATINGPOINT_TO_REAL, "fp.to_real");
196
197 Parser::addOperator(kind::FLOATINGPOINT_TO_FP_GENERIC);
198 Parser::addOperator(kind::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR);
199 Parser::addOperator(kind::FLOATINGPOINT_TO_FP_FLOATINGPOINT);
200 Parser::addOperator(kind::FLOATINGPOINT_TO_FP_REAL);
201 Parser::addOperator(kind::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR);
202 Parser::addOperator(kind::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR);
203 Parser::addOperator(kind::FLOATINGPOINT_TO_UBV);
204 Parser::addOperator(kind::FLOATINGPOINT_TO_SBV);
205 }
206
207 void Smt2::addSepOperators() {
208 addOperator(kind::SEP_STAR, "sep");
209 addOperator(kind::SEP_PTO, "pto");
210 addOperator(kind::SEP_WAND, "wand");
211 addOperator(kind::SEP_EMP, "emp");
212 Parser::addOperator(kind::SEP_STAR);
213 Parser::addOperator(kind::SEP_PTO);
214 Parser::addOperator(kind::SEP_WAND);
215 Parser::addOperator(kind::SEP_EMP);
216 }
217
218 void Smt2::addTheory(Theory theory) {
219 switch(theory) {
220 case THEORY_ARRAYS:
221 addOperator(kind::SELECT, "select");
222 addOperator(kind::STORE, "store");
223 break;
224
225 case THEORY_BITVECTORS:
226 addBitvectorOperators();
227 break;
228
229 case THEORY_CORE:
230 defineType("Bool", getExprManager()->booleanType());
231 defineVar("true", getExprManager()->mkConst(true));
232 defineVar("false", getExprManager()->mkConst(false));
233 Parser::addOperator(kind::AND);
234 Parser::addOperator(kind::DISTINCT);
235 Parser::addOperator(kind::EQUAL);
236 Parser::addOperator(kind::IMPLIES);
237 Parser::addOperator(kind::ITE);
238 Parser::addOperator(kind::NOT);
239 Parser::addOperator(kind::OR);
240 Parser::addOperator(kind::XOR);
241 break;
242
243 case THEORY_REALS_INTS:
244 defineType("Real", getExprManager()->realType());
245 Parser::addOperator(kind::DIVISION);
246 addOperator(kind::TO_INTEGER, "to_int");
247 addOperator(kind::IS_INTEGER, "is_int");
248 addOperator(kind::TO_REAL, "to_real");
249 // falling through on purpose, to add Ints part of Reals_Ints
250 case THEORY_INTS:
251 defineType("Int", getExprManager()->integerType());
252 addArithmeticOperators();
253 addOperator(kind::INTS_DIVISION, "div");
254 addOperator(kind::INTS_MODULUS, "mod");
255 addOperator(kind::ABS, "abs");
256 Parser::addOperator(kind::DIVISIBLE);
257 break;
258
259 case THEORY_REALS:
260 defineType("Real", getExprManager()->realType());
261 addArithmeticOperators();
262 Parser::addOperator(kind::DIVISION);
263 break;
264
265 case THEORY_TRANSCENDENTALS: addTranscendentalOperators(); break;
266
267 case THEORY_QUANTIFIERS:
268 break;
269
270 case THEORY_SETS:
271 addOperator(kind::UNION, "union");
272 addOperator(kind::INTERSECTION, "intersection");
273 addOperator(kind::SETMINUS, "setminus");
274 addOperator(kind::SUBSET, "subset");
275 addOperator(kind::MEMBER, "member");
276 addOperator(kind::SINGLETON, "singleton");
277 addOperator(kind::INSERT, "insert");
278 addOperator(kind::CARD, "card");
279 addOperator(kind::COMPLEMENT, "complement");
280 addOperator(kind::JOIN, "join");
281 addOperator(kind::PRODUCT, "product");
282 addOperator(kind::TRANSPOSE, "transpose");
283 addOperator(kind::TCLOSURE, "tclosure");
284 break;
285
286 case THEORY_DATATYPES:
287 {
288 const std::vector<Type> types;
289 defineType("Tuple", getExprManager()->mkTupleType(types));
290 Parser::addOperator(kind::APPLY_CONSTRUCTOR);
291 Parser::addOperator(kind::APPLY_TESTER);
292 Parser::addOperator(kind::APPLY_SELECTOR);
293 Parser::addOperator(kind::APPLY_SELECTOR_TOTAL);
294 break;
295 }
296
297 case THEORY_STRINGS:
298 defineType("String", getExprManager()->stringType());
299 defineType("RegLan", getExprManager()->regExpType());
300 defineType("Int", getExprManager()->integerType());
301 addStringOperators();
302 break;
303
304 case THEORY_UF:
305 Parser::addOperator(kind::APPLY_UF);
306 break;
307
308 case THEORY_FP:
309 defineType("RoundingMode", getExprManager()->roundingModeType());
310 defineType("Float16", getExprManager()->mkFloatingPointType(5, 11));
311 defineType("Float32", getExprManager()->mkFloatingPointType(8, 24));
312 defineType("Float64", getExprManager()->mkFloatingPointType(11, 53));
313 defineType("Float128", getExprManager()->mkFloatingPointType(15, 113));
314 addFloatingPointOperators();
315 break;
316
317 case THEORY_SEP:
318 addSepOperators();
319 break;
320
321 default:
322 std::stringstream ss;
323 ss << "internal error: unsupported theory " << theory;
324 throw ParserException(ss.str());
325 }
326 }
327
328 void Smt2::addOperator(Kind kind, const std::string& name) {
329 Debug("parser") << "Smt2::addOperator( " << kind << ", " << name << " )"
330 << std::endl;
331 Parser::addOperator(kind);
332 operatorKindMap[name] = kind;
333 }
334
335 Kind Smt2::getOperatorKind(const std::string& name) const {
336 // precondition: isOperatorEnabled(name)
337 return operatorKindMap.find(name)->second;
338 }
339
340 bool Smt2::isOperatorEnabled(const std::string& name) const {
341 return operatorKindMap.find(name) != operatorKindMap.end();
342 }
343
344 bool Smt2::isTheoryEnabled(Theory theory) const {
345 switch(theory) {
346 case THEORY_ARRAYS:
347 return d_logic.isTheoryEnabled(theory::THEORY_ARRAYS);
348 case THEORY_BITVECTORS:
349 return d_logic.isTheoryEnabled(theory::THEORY_BV);
350 case THEORY_CORE:
351 return true;
352 case THEORY_DATATYPES:
353 return d_logic.isTheoryEnabled(theory::THEORY_DATATYPES);
354 case THEORY_INTS:
355 return d_logic.isTheoryEnabled(theory::THEORY_ARITH) &&
356 d_logic.areIntegersUsed() && ( !d_logic.areRealsUsed() );
357 case THEORY_REALS:
358 return d_logic.isTheoryEnabled(theory::THEORY_ARITH) &&
359 ( !d_logic.areIntegersUsed() ) && d_logic.areRealsUsed();
360 case THEORY_REALS_INTS:
361 return d_logic.isTheoryEnabled(theory::THEORY_ARITH) &&
362 d_logic.areIntegersUsed() && d_logic.areRealsUsed();
363 case THEORY_QUANTIFIERS:
364 return d_logic.isQuantified();
365 case THEORY_SETS:
366 return d_logic.isTheoryEnabled(theory::THEORY_SETS);
367 case THEORY_STRINGS:
368 return d_logic.isTheoryEnabled(theory::THEORY_STRINGS);
369 case THEORY_UF:
370 return d_logic.isTheoryEnabled(theory::THEORY_UF);
371 case THEORY_FP:
372 return d_logic.isTheoryEnabled(theory::THEORY_FP);
373 case THEORY_SEP:
374 return d_logic.isTheoryEnabled(theory::THEORY_SEP);
375 default:
376 std::stringstream ss;
377 ss << "internal error: unsupported theory " << theory;
378 throw ParserException(ss.str());
379 }
380 }
381
382 bool Smt2::logicIsSet() {
383 return d_logicSet;
384 }
385
386 Expr Smt2::getExpressionForNameAndType(const std::string& name, Type t) {
387 if(sygus() && name[0]=='-' &&
388 name.find_first_not_of("0123456789", 1) == std::string::npos) {
389 //allow unary minus in sygus
390 return getExprManager()->mkConst(Rational(name));
391 }else if(isAbstractValue(name)) {
392 return mkAbstractValue(name);
393 }else{
394 return Parser::getExpressionForNameAndType(name, t);
395 }
396 }
397
398 Expr Smt2::mkDefineFunRec(
399 const std::string& fname,
400 const std::vector<std::pair<std::string, Type> >& sortedVarNames,
401 Type t,
402 std::vector<Expr>& flattenVars)
403 {
404 std::vector<Type> sorts;
405 for (const std::pair<std::string, CVC4::Type>& svn : sortedVarNames)
406 {
407 sorts.push_back(svn.second);
408 }
409
410 // make the flattened function type, add bound variables
411 // to flattenVars if the defined function was given a function return type.
412 Type ft = mkFlatFunctionType(sorts, t, flattenVars);
413
414 // allow overloading
415 return mkVar(fname, ft, ExprManager::VAR_FLAG_NONE, true);
416 }
417
418 void Smt2::pushDefineFunRecScope(
419 const std::vector<std::pair<std::string, Type> >& sortedVarNames,
420 Expr func,
421 const std::vector<Expr>& flattenVars,
422 std::vector<Expr>& bvs,
423 bool bindingLevel)
424 {
425 pushScope(bindingLevel);
426
427 // bound variables are those that are explicitly named in the preamble
428 // of the define-fun(s)-rec command, we define them here
429 for (const std::pair<std::string, CVC4::Type>& svn : sortedVarNames)
430 {
431 Expr v = mkBoundVar(svn.first, svn.second);
432 bvs.push_back(v);
433 }
434
435 bvs.insert(bvs.end(), flattenVars.begin(), flattenVars.end());
436 }
437
438 void Smt2::reset() {
439 d_logicSet = false;
440 d_logic = LogicInfo();
441 operatorKindMap.clear();
442 d_lastNamedTerm = std::pair<Expr, std::string>();
443 this->Parser::reset();
444
445 if( !strictModeEnabled() ) {
446 addTheory(Smt2::THEORY_CORE);
447 }
448 }
449
450 void Smt2::resetAssertions() {
451 // Remove all declarations except the ones at level 0.
452 while (this->scopeLevel() > 0) {
453 this->popScope();
454 }
455 }
456
457 void Smt2::setLogic(std::string name) {
458
459 if(sygus()) {
460 // non-smt2-standard sygus logic names go here (http://sygus.seas.upenn.edu/files/sygus.pdf Section 3.2)
461 if(name == "Arrays") {
462 name = "A";
463 }else if(name == "Reals") {
464 name = "LRA";
465 }
466 }
467
468 d_logicSet = true;
469 if(logicIsForced()) {
470 d_logic = getForcedLogic();
471 } else {
472 d_logic = name;
473 }
474
475 // if sygus is enabled, we must enable UF, datatypes, integer arithmetic and
476 // higher-order
477 if(sygus()) {
478 if (!d_logic.isQuantified())
479 {
480 warning("Logics in sygus are assumed to contain quantifiers.");
481 warning("Omit QF_ from the logic to avoid this warning.");
482 }
483 // get unlocked copy, modify, copy and relock
484 LogicInfo log(d_logic.getUnlockedCopy());
485 log.enableQuantifiers();
486 log.enableTheory(theory::THEORY_UF);
487 log.enableTheory(theory::THEORY_DATATYPES);
488 log.enableIntegers();
489 log.enableHigherOrder();
490 d_logic = log;
491 d_logic.lock();
492 }
493
494 // Core theory belongs to every logic
495 addTheory(THEORY_CORE);
496
497 if(d_logic.isTheoryEnabled(theory::THEORY_UF)) {
498 addTheory(THEORY_UF);
499 }
500
501 if(d_logic.isTheoryEnabled(theory::THEORY_ARITH)) {
502 if(d_logic.areIntegersUsed()) {
503 if(d_logic.areRealsUsed()) {
504 addTheory(THEORY_REALS_INTS);
505 } else {
506 addTheory(THEORY_INTS);
507 }
508 } else if(d_logic.areRealsUsed()) {
509 addTheory(THEORY_REALS);
510 }
511
512 if (d_logic.areTranscendentalsUsed())
513 {
514 addTheory(THEORY_TRANSCENDENTALS);
515 }
516 }
517
518 if(d_logic.isTheoryEnabled(theory::THEORY_ARRAYS)) {
519 addTheory(THEORY_ARRAYS);
520 }
521
522 if(d_logic.isTheoryEnabled(theory::THEORY_BV)) {
523 addTheory(THEORY_BITVECTORS);
524 }
525
526 if(d_logic.isTheoryEnabled(theory::THEORY_DATATYPES)) {
527 addTheory(THEORY_DATATYPES);
528 }
529
530 if(d_logic.isTheoryEnabled(theory::THEORY_SETS)) {
531 addTheory(THEORY_SETS);
532 }
533
534 if(d_logic.isTheoryEnabled(theory::THEORY_STRINGS)) {
535 addTheory(THEORY_STRINGS);
536 }
537
538 if(d_logic.isQuantified()) {
539 addTheory(THEORY_QUANTIFIERS);
540 }
541
542 if (d_logic.isTheoryEnabled(theory::THEORY_FP)) {
543 addTheory(THEORY_FP);
544 }
545
546 if (d_logic.isTheoryEnabled(theory::THEORY_SEP)) {
547 addTheory(THEORY_SEP);
548 }
549
550 }/* Smt2::setLogic() */
551
552 void Smt2::setInfo(const std::string& flag, const SExpr& sexpr) {
553 // TODO: ???
554 }
555
556 void Smt2::setOption(const std::string& flag, const SExpr& sexpr) {
557 // TODO: ???
558 }
559
560 void Smt2::checkThatLogicIsSet() {
561 if( ! logicIsSet() ) {
562 if(strictModeEnabled()) {
563 parseError("set-logic must appear before this point.");
564 } else {
565 warning("No set-logic command was given before this point.");
566 warning("CVC4 will make all theories available.");
567 warning("Consider setting a stricter logic for (likely) better performance.");
568 warning("To suppress this warning in the future use (set-logic ALL).");
569
570 setLogic("ALL");
571
572 Command* c = new SetBenchmarkLogicCommand("ALL");
573 c->setMuted(true);
574 preemptCommand(c);
575 }
576 }
577 }
578
579 /* The include are managed in the lexer but called in the parser */
580 // Inspired by http://www.antlr3.org/api/C/interop.html
581
582 static bool newInputStream(const std::string& filename, pANTLR3_LEXER lexer) {
583 Debug("parser") << "Including " << filename << std::endl;
584 // Create a new input stream and take advantage of built in stream stacking
585 // in C target runtime.
586 //
587 pANTLR3_INPUT_STREAM in;
588 #ifdef CVC4_ANTLR3_OLD_INPUT_STREAM
589 in = antlr3AsciiFileStreamNew((pANTLR3_UINT8) filename.c_str());
590 #else /* CVC4_ANTLR3_OLD_INPUT_STREAM */
591 in = antlr3FileStreamNew((pANTLR3_UINT8) filename.c_str(), ANTLR3_ENC_8BIT);
592 #endif /* CVC4_ANTLR3_OLD_INPUT_STREAM */
593 if( in == NULL ) {
594 Debug("parser") << "Can't open " << filename << std::endl;
595 return false;
596 }
597 // Same thing as the predefined PUSHSTREAM(in);
598 lexer->pushCharStream(lexer, in);
599 // restart it
600 //lexer->rec->state->tokenStartCharIndex = -10;
601 //lexer->emit(lexer);
602
603 // Note that the input stream is not closed when it EOFs, I don't bother
604 // to do it here, but it is up to you to track streams created like this
605 // and destroy them when the whole parse session is complete. Remember that you
606 // don't want to do this until all tokens have been manipulated all the way through
607 // your tree parsers etc as the token does not store the text it just refers
608 // back to the input stream and trying to get the text for it will abort if you
609 // close the input stream too early.
610
611 //TODO what said before
612 return true;
613 }
614
615 void Smt2::includeFile(const std::string& filename) {
616 // security for online version
617 if(!canIncludeFile()) {
618 parseError("include-file feature was disabled for this run.");
619 }
620
621 // Get the lexer
622 AntlrInput* ai = static_cast<AntlrInput*>(getInput());
623 pANTLR3_LEXER lexer = ai->getAntlr3Lexer();
624 // get the name of the current stream "Does it work inside an include?"
625 const std::string inputName = ai->getInputStreamName();
626
627 // Find the directory of the current input file
628 std::string path;
629 size_t pos = inputName.rfind('/');
630 if(pos != std::string::npos) {
631 path = std::string(inputName, 0, pos + 1);
632 }
633 path.append(filename);
634 if(!newInputStream(path, lexer)) {
635 parseError("Couldn't open include file `" + path + "'");
636 }
637 }
638
639 void Smt2::mkSygusConstantsForType( const Type& type, std::vector<CVC4::Expr>& ops ) {
640 if( type.isInteger() ){
641 ops.push_back(getExprManager()->mkConst(Rational(0)));
642 ops.push_back(getExprManager()->mkConst(Rational(1)));
643 }else if( type.isBitVector() ){
644 unsigned sz = ((BitVectorType)type).getSize();
645 BitVector bval0(sz, (unsigned int)0);
646 ops.push_back( getExprManager()->mkConst(bval0) );
647 BitVector bval1(sz, (unsigned int)1);
648 ops.push_back( getExprManager()->mkConst(bval1) );
649 }else if( type.isBoolean() ){
650 ops.push_back(getExprManager()->mkConst(true));
651 ops.push_back(getExprManager()->mkConst(false));
652 }
653 //TODO : others?
654 }
655
656 // This method adds N operators to ops[index], N names to cnames[index] and N type argument vectors to cargs[index] (where typically N=1)
657 // This method may also add new elements pairwise into datatypes/sorts/ops/cnames/cargs in the case of non-flat gterms.
658 void Smt2::processSygusGTerm( CVC4::SygusGTerm& sgt, int index,
659 std::vector< CVC4::Datatype >& datatypes,
660 std::vector< CVC4::Type>& sorts,
661 std::vector< std::vector<CVC4::Expr> >& ops,
662 std::vector< std::vector<std::string> >& cnames,
663 std::vector< std::vector< std::vector< CVC4::Type > > >& cargs,
664 std::vector< bool >& allow_const,
665 std::vector< std::vector< std::string > >& unresolved_gterm_sym,
666 std::vector<CVC4::Expr>& sygus_vars,
667 std::map< CVC4::Type, CVC4::Type >& sygus_to_builtin, std::map< CVC4::Type, CVC4::Expr >& sygus_to_builtin_expr,
668 CVC4::Type& ret, bool isNested ){
669 if( sgt.d_gterm_type==SygusGTerm::gterm_op || sgt.d_gterm_type==SygusGTerm::gterm_let ){
670 Debug("parser-sygus") << "Add " << sgt.d_expr << " to datatype " << index
671 << ", isLet = "
672 << (sgt.d_gterm_type == SygusGTerm::gterm_let)
673 << std::endl;
674 Kind oldKind;
675 Kind newKind = kind::UNDEFINED_KIND;
676 //convert to UMINUS if one child of MINUS
677 if( sgt.d_children.size()==1 && sgt.d_expr==getExprManager()->operatorOf(kind::MINUS) ){
678 oldKind = kind::MINUS;
679 newKind = kind::UMINUS;
680 }
681 if( newKind!=kind::UNDEFINED_KIND ){
682 Expr newExpr = getExprManager()->operatorOf(newKind);
683 Debug("parser-sygus") << "Replace " << sgt.d_expr << " with " << newExpr << std::endl;
684 sgt.d_expr = newExpr;
685 std::string oldName = kind::kindToString(oldKind);
686 std::string newName = kind::kindToString(newKind);
687 size_t pos = 0;
688 if((pos = sgt.d_name.find(oldName, pos)) != std::string::npos){
689 sgt.d_name.replace(pos, oldName.length(), newName);
690 }
691 }
692 ops[index].push_back( sgt.d_expr );
693 cnames[index].push_back( sgt.d_name );
694 cargs[index].push_back( std::vector< CVC4::Type >() );
695 for( unsigned i=0; i<sgt.d_children.size(); i++ ){
696 std::stringstream ss;
697 ss << datatypes[index].getName() << "_" << ops[index].size() << "_arg_" << i;
698 std::string sub_dname = ss.str();
699 //add datatype for child
700 Type null_type;
701 pushSygusDatatypeDef( null_type, sub_dname, datatypes, sorts, ops, cnames, cargs, allow_const, unresolved_gterm_sym );
702 int sub_dt_index = datatypes.size()-1;
703 //process child
704 Type sub_ret;
705 processSygusGTerm( sgt.d_children[i], sub_dt_index, datatypes, sorts, ops, cnames, cargs, allow_const, unresolved_gterm_sym,
706 sygus_vars, sygus_to_builtin, sygus_to_builtin_expr, sub_ret, true );
707 //process the nested gterm (either pop the last datatype, or flatten the argument)
708 Type tt = processSygusNestedGTerm( sub_dt_index, sub_dname, datatypes, sorts, ops, cnames, cargs, allow_const, unresolved_gterm_sym,
709 sygus_to_builtin, sygus_to_builtin_expr, sub_ret );
710 cargs[index].back().push_back(tt);
711 }
712 //if let, must create operator
713 if( sgt.d_gterm_type==SygusGTerm::gterm_let ){
714 processSygusLetConstructor( sgt.d_let_vars, index, datatypes, sorts, ops, cnames, cargs,
715 sygus_vars, sygus_to_builtin, sygus_to_builtin_expr );
716 }
717 }else if( sgt.d_gterm_type==SygusGTerm::gterm_constant ){
718 if( sgt.getNumChildren()!=0 ){
719 parseError("Bad syntax for Sygus Constant.");
720 }
721 std::vector< Expr > consts;
722 mkSygusConstantsForType( sgt.d_type, consts );
723 Debug("parser-sygus") << "...made " << consts.size() << " constants." << std::endl;
724 for( unsigned i=0; i<consts.size(); i++ ){
725 std::stringstream ss;
726 ss << consts[i];
727 Debug("parser-sygus") << "...add for constant " << ss.str() << std::endl;
728 ops[index].push_back( consts[i] );
729 cnames[index].push_back( ss.str() );
730 cargs[index].push_back( std::vector< CVC4::Type >() );
731 }
732 allow_const[index] = true;
733 }else if( sgt.d_gterm_type==SygusGTerm::gterm_variable || sgt.d_gterm_type==SygusGTerm::gterm_input_variable ){
734 if( sgt.getNumChildren()!=0 ){
735 parseError("Bad syntax for Sygus Variable.");
736 }
737 Debug("parser-sygus") << "...process " << sygus_vars.size() << " variables." << std::endl;
738 for( unsigned i=0; i<sygus_vars.size(); i++ ){
739 if( sygus_vars[i].getType()==sgt.d_type ){
740 std::stringstream ss;
741 ss << sygus_vars[i];
742 Debug("parser-sygus") << "...add for variable " << ss.str() << std::endl;
743 ops[index].push_back( sygus_vars[i] );
744 cnames[index].push_back( ss.str() );
745 cargs[index].push_back( std::vector< CVC4::Type >() );
746 }
747 }
748 }else if( sgt.d_gterm_type==SygusGTerm::gterm_nested_sort ){
749 ret = sgt.d_type;
750 }else if( sgt.d_gterm_type==SygusGTerm::gterm_unresolved ){
751 if( isNested ){
752 if( isUnresolvedType(sgt.d_name) ){
753 ret = getSort(sgt.d_name);
754 }else{
755 //nested, unresolved symbol...fail
756 std::stringstream ss;
757 ss << "Cannot handle nested unresolved symbol " << sgt.d_name << std::endl;
758 parseError(ss.str());
759 }
760 }else{
761 //will resolve when adding constructors
762 unresolved_gterm_sym[index].push_back(sgt.d_name);
763 }
764 }else if( sgt.d_gterm_type==SygusGTerm::gterm_ignore ){
765
766 }
767 }
768
769 bool Smt2::pushSygusDatatypeDef( Type t, std::string& dname,
770 std::vector< CVC4::Datatype >& datatypes,
771 std::vector< CVC4::Type>& sorts,
772 std::vector< std::vector<CVC4::Expr> >& ops,
773 std::vector< std::vector<std::string> >& cnames,
774 std::vector< std::vector< std::vector< CVC4::Type > > >& cargs,
775 std::vector< bool >& allow_const,
776 std::vector< std::vector< std::string > >& unresolved_gterm_sym ){
777 sorts.push_back(t);
778 datatypes.push_back(Datatype(dname));
779 ops.push_back(std::vector<Expr>());
780 cnames.push_back(std::vector<std::string>());
781 cargs.push_back(std::vector<std::vector<CVC4::Type> >());
782 allow_const.push_back(false);
783 unresolved_gterm_sym.push_back(std::vector< std::string >());
784 return true;
785 }
786
787 bool Smt2::popSygusDatatypeDef( std::vector< CVC4::Datatype >& datatypes,
788 std::vector< CVC4::Type>& sorts,
789 std::vector< std::vector<CVC4::Expr> >& ops,
790 std::vector< std::vector<std::string> >& cnames,
791 std::vector< std::vector< std::vector< CVC4::Type > > >& cargs,
792 std::vector< bool >& allow_const,
793 std::vector< std::vector< std::string > >& unresolved_gterm_sym ){
794 sorts.pop_back();
795 datatypes.pop_back();
796 ops.pop_back();
797 cnames.pop_back();
798 cargs.pop_back();
799 allow_const.pop_back();
800 unresolved_gterm_sym.pop_back();
801 return true;
802 }
803
804 Type Smt2::processSygusNestedGTerm( int sub_dt_index, std::string& sub_dname, std::vector< CVC4::Datatype >& datatypes,
805 std::vector< CVC4::Type>& sorts,
806 std::vector< std::vector<CVC4::Expr> >& ops,
807 std::vector< std::vector<std::string> >& cnames,
808 std::vector< std::vector< std::vector< CVC4::Type > > >& cargs,
809 std::vector< bool >& allow_const,
810 std::vector< std::vector< std::string > >& unresolved_gterm_sym,
811 std::map< CVC4::Type, CVC4::Type >& sygus_to_builtin,
812 std::map< CVC4::Type, CVC4::Expr >& sygus_to_builtin_expr, Type sub_ret ) {
813 Type t = sub_ret;
814 Debug("parser-sygus") << "Argument is ";
815 if( t.isNull() ){
816 //then, it is the datatype we constructed, which should have a single constructor
817 t = mkUnresolvedType(sub_dname);
818 Debug("parser-sygus") << "inline flattening of (auxiliary, local) datatype " << t << std::endl;
819 Debug("parser-sygus") << ": to compute type, construct ground term witnessing the grammar, #cons=" << cargs[sub_dt_index].size() << std::endl;
820 if( cargs[sub_dt_index].empty() ){
821 parseError(std::string("Internal error : datatype for nested gterm does not have a constructor."));
822 }
823 Expr sop = ops[sub_dt_index][0];
824 Type curr_t;
825 if( sop.getKind() != kind::BUILTIN && ( sop.isConst() || cargs[sub_dt_index][0].empty() ) ){
826 curr_t = sop.getType();
827 Debug("parser-sygus") << ": it is constant/0-arg cons " << sop << " with type " << sop.getType() << ", debug=" << sop.isConst() << " " << cargs[sub_dt_index][0].size() << std::endl;
828 // only cache if it is a singleton datatype (has unique expr)
829 if (ops[sub_dt_index].size() == 1)
830 {
831 sygus_to_builtin_expr[t] = sop;
832 // store that term sop has dedicated sygus type t
833 if (d_sygus_bound_var_type.find(sop) == d_sygus_bound_var_type.end())
834 {
835 d_sygus_bound_var_type[sop] = t;
836 }
837 }
838 }else{
839 std::vector< Expr > children;
840 if( sop.getKind() != kind::BUILTIN ){
841 children.push_back( sop );
842 }
843 for( unsigned i=0; i<cargs[sub_dt_index][0].size(); i++ ){
844 std::map< CVC4::Type, CVC4::Expr >::iterator it = sygus_to_builtin_expr.find( cargs[sub_dt_index][0][i] );
845 if( it==sygus_to_builtin_expr.end() ){
846 if( sygus_to_builtin.find( cargs[sub_dt_index][0][i] )==sygus_to_builtin.end() ){
847 std::stringstream ss;
848 ss << "Missing builtin type for type " << cargs[sub_dt_index][0][i] << "!" << std::endl;
849 ss << "Builtin types are currently : " << std::endl;
850 for( std::map< CVC4::Type, CVC4::Type >::iterator itb = sygus_to_builtin.begin(); itb != sygus_to_builtin.end(); ++itb ){
851 ss << " " << itb->first << " -> " << itb->second << std::endl;
852 }
853 parseError(ss.str());
854 }
855 Type bt = sygus_to_builtin[cargs[sub_dt_index][0][i]];
856 Debug("parser-sygus") << ": child " << i << " introduce type elem for " << cargs[sub_dt_index][0][i] << " " << bt << std::endl;
857 std::stringstream ss;
858 ss << t << "_x_" << i;
859 Expr bv = mkBoundVar(ss.str(), bt);
860 children.push_back( bv );
861 d_sygus_bound_var_type[bv] = cargs[sub_dt_index][0][i];
862 }else{
863 Debug("parser-sygus") << ": child " << i << " existing sygus to builtin expr : " << it->second << std::endl;
864 children.push_back( it->second );
865 }
866 }
867 Kind sk = sop.getKind() != kind::BUILTIN
868 ? kind::APPLY_UF
869 : getExprManager()->operatorToKind(sop);
870 Debug("parser-sygus") << ": operator " << sop << " with " << sop.getKind() << " " << sk << std::endl;
871 Expr e = getExprManager()->mkExpr( sk, children );
872 Debug("parser-sygus") << ": constructed " << e << ", which has type " << e.getType() << std::endl;
873 curr_t = e.getType();
874 sygus_to_builtin_expr[t] = e;
875 }
876 sorts[sub_dt_index] = curr_t;
877 sygus_to_builtin[t] = curr_t;
878 }else{
879 Debug("parser-sygus") << "simple argument " << t << std::endl;
880 Debug("parser-sygus") << "...removing " << datatypes.back().getName() << std::endl;
881 //otherwise, datatype was unecessary
882 //pop argument datatype definition
883 popSygusDatatypeDef( datatypes, sorts, ops, cnames, cargs, allow_const, unresolved_gterm_sym );
884 }
885 return t;
886 }
887
888 void Smt2::processSygusLetConstructor( std::vector< CVC4::Expr >& let_vars,
889 int index,
890 std::vector< CVC4::Datatype >& datatypes,
891 std::vector< CVC4::Type>& sorts,
892 std::vector< std::vector<CVC4::Expr> >& ops,
893 std::vector< std::vector<std::string> >& cnames,
894 std::vector< std::vector< std::vector< CVC4::Type > > >& cargs,
895 std::vector<CVC4::Expr>& sygus_vars,
896 std::map< CVC4::Type, CVC4::Type >& sygus_to_builtin,
897 std::map< CVC4::Type, CVC4::Expr >& sygus_to_builtin_expr ) {
898 std::vector< CVC4::Expr > let_define_args;
899 Expr let_body;
900 int dindex = cargs[index].size()-1;
901 Debug("parser-sygus") << "Process let constructor for datatype " << datatypes[index].getName() << ", #subtypes = " << cargs[index][dindex].size() << std::endl;
902 for( unsigned i=0; i<cargs[index][dindex].size(); i++ ){
903 Debug("parser-sygus") << " " << i << " : " << cargs[index][dindex][i] << std::endl;
904 if( i+1==cargs[index][dindex].size() ){
905 std::map< CVC4::Type, CVC4::Expr >::iterator it = sygus_to_builtin_expr.find( cargs[index][dindex][i] );
906 if( it!=sygus_to_builtin_expr.end() ){
907 let_body = it->second;
908 }else{
909 std::stringstream ss;
910 ss << datatypes[index].getName() << "_body";
911 let_body = mkBoundVar(ss.str(), sygus_to_builtin[cargs[index][dindex][i]]);
912 d_sygus_bound_var_type[let_body] = cargs[index][dindex][i];
913 }
914 }
915 }
916 Debug("parser-sygus") << std::endl;
917 Debug("parser-sygus") << "Body is " << let_body << std::endl;
918 Debug("parser-sygus") << "# let vars = " << let_vars.size() << std::endl;
919 for( unsigned i=0; i<let_vars.size(); i++ ){
920 Debug("parser-sygus") << " let var " << i << " : " << let_vars[i] << " " << let_vars[i].getType() << std::endl;
921 let_define_args.push_back( let_vars[i] );
922 }
923 /*
924 Debug("parser-sygus") << "index = " << index << ", start index = " << start_index << ", #Current datatypes = " << datatypes.size() << std::endl;
925 for( unsigned i=start_index; i<datatypes.size(); i++ ){
926 Debug("parser-sygus") << " datatype " << i << " : " << datatypes[i].getName() << ", #cons = " << cargs[i].size() << std::endl;
927 if( !cargs[i].empty() ){
928 Debug("parser-sygus") << " operator 0 is " << ops[i][0] << std::endl;
929 Debug("parser-sygus") << " cons 0 has " << cargs[i][0].size() << " sub fields." << std::endl;
930 for( unsigned j=0; j<cargs[i][0].size(); j++ ){
931 Type bt = sygus_to_builtin[cargs[i][0][j]];
932 Debug("parser-sygus") << " cons 0, selector " << j << " : " << cargs[i][0][j] << " " << bt << std::endl;
933 }
934 }
935 }
936 */
937 //last argument is the return, pop
938 cargs[index][dindex].pop_back();
939 collectSygusLetArgs( let_body, cargs[index][dindex], let_define_args );
940
941 Debug("parser-sygus") << "Make define-fun with "
942 << cargs[index][dindex].size()
943 << " operator arguments and " << let_define_args.size()
944 << " provided arguments..." << std::endl;
945 if (cargs[index][dindex].size() != let_define_args.size())
946 {
947 std::stringstream ss;
948 ss << "Wrong number of let body terms." << std::endl;
949 parseError(ss.str());
950 }
951 std::vector<CVC4::Type> fsorts;
952 for( unsigned i=0; i<cargs[index][dindex].size(); i++ ){
953 Debug("parser-sygus") << " " << i << " : " << let_define_args[i] << " " << let_define_args[i].getType() << " " << cargs[index][dindex][i] << std::endl;
954 fsorts.push_back(let_define_args[i].getType());
955 }
956
957 Type ft = getExprManager()->mkFunctionType(fsorts, let_body.getType());
958 std::stringstream ss;
959 ss << datatypes[index].getName() << "_let";
960 Expr let_func = mkFunction(ss.str(), ft, ExprManager::VAR_FLAG_DEFINED);
961 d_sygus_defined_funs.push_back( let_func );
962 preemptCommand( new DefineFunctionCommand(ss.str(), let_func, let_define_args, let_body) );
963
964 ops[index].pop_back();
965 ops[index].push_back( let_func );
966 cnames[index].pop_back();
967 cnames[index].push_back(ss.str());
968
969 //mark function as let constructor
970 d_sygus_let_func_to_vars[let_func].insert( d_sygus_let_func_to_vars[let_func].end(), let_define_args.begin(), let_define_args.end() );
971 d_sygus_let_func_to_body[let_func] = let_body;
972 d_sygus_let_func_to_num_input_vars[let_func] = let_vars.size();
973 }
974
975
976 void Smt2::collectSygusLetArgs( CVC4::Expr e, std::vector< CVC4::Type >& sygusArgs, std::vector< CVC4::Expr >& builtinArgs ) {
977 if( e.getKind()==kind::BOUND_VARIABLE ){
978 if( std::find( builtinArgs.begin(), builtinArgs.end(), e )==builtinArgs.end() ){
979 builtinArgs.push_back( e );
980 sygusArgs.push_back( d_sygus_bound_var_type[e] );
981 if( d_sygus_bound_var_type[e].isNull() ){
982 std::stringstream ss;
983 ss << "While constructing body of let gterm, can't map " << e << " to sygus type." << std::endl;
984 parseError(ss.str());
985 }
986 }
987 }else{
988 for( unsigned i=0; i<e.getNumChildren(); i++ ){
989 collectSygusLetArgs( e[i], sygusArgs, builtinArgs );
990 }
991 }
992 }
993
994 void Smt2::setSygusStartIndex( std::string& fun, int startIndex,
995 std::vector< CVC4::Datatype >& datatypes,
996 std::vector< CVC4::Type>& sorts,
997 std::vector< std::vector<CVC4::Expr> >& ops ) {
998 if( startIndex>0 ){
999 CVC4::Datatype tmp_dt = datatypes[0];
1000 Type tmp_sort = sorts[0];
1001 std::vector< Expr > tmp_ops;
1002 tmp_ops.insert( tmp_ops.end(), ops[0].begin(), ops[0].end() );
1003 datatypes[0] = datatypes[startIndex];
1004 sorts[0] = sorts[startIndex];
1005 ops[0].clear();
1006 ops[0].insert( ops[0].end(), ops[startIndex].begin(), ops[startIndex].end() );
1007 datatypes[startIndex] = tmp_dt;
1008 sorts[startIndex] = tmp_sort;
1009 ops[startIndex].clear();
1010 ops[startIndex].insert( ops[startIndex].begin(), tmp_ops.begin(), tmp_ops.end() );
1011 }else if( startIndex<0 ){
1012 std::stringstream ss;
1013 ss << "warning: no symbol named Start for synth-fun " << fun << std::endl;
1014 warning(ss.str());
1015 }
1016 }
1017
1018 void Smt2::mkSygusDatatype( CVC4::Datatype& dt, std::vector<CVC4::Expr>& ops,
1019 std::vector<std::string>& cnames, std::vector< std::vector< CVC4::Type > >& cargs,
1020 std::vector<std::string>& unresolved_gterm_sym,
1021 std::map< CVC4::Type, CVC4::Type >& sygus_to_builtin ) {
1022 Debug("parser-sygus") << "Making sygus datatype " << dt.getName() << std::endl;
1023 Debug("parser-sygus") << " add constructors..." << std::endl;
1024
1025 Debug("parser-sygus") << "SMT2 sygus parser : Making constructors for sygus datatype " << dt.getName() << std::endl;
1026 Debug("parser-sygus") << " add constructors..." << std::endl;
1027 // size of cnames changes, this loop must check size
1028 for (unsigned i = 0; i < cnames.size(); i++)
1029 {
1030 bool is_dup = false;
1031 bool is_dup_op = false;
1032 for (unsigned j = 0; j < i; j++)
1033 {
1034 if( ops[i]==ops[j] ){
1035 is_dup_op = true;
1036 if( cargs[i].size()==cargs[j].size() ){
1037 is_dup = true;
1038 for( unsigned k=0; k<cargs[i].size(); k++ ){
1039 if( cargs[i][k]!=cargs[j][k] ){
1040 is_dup = false;
1041 break;
1042 }
1043 }
1044 }
1045 if( is_dup ){
1046 break;
1047 }
1048 }
1049 }
1050 Debug("parser-sygus") << "SYGUS CONS " << i << " : ";
1051 if( is_dup ){
1052 Debug("parser-sygus") << "--> Duplicate gterm : " << ops[i] << std::endl;
1053 ops.erase( ops.begin() + i, ops.begin() + i + 1 );
1054 cnames.erase( cnames.begin() + i, cnames.begin() + i + 1 );
1055 cargs.erase( cargs.begin() + i, cargs.begin() + i + 1 );
1056 i--;
1057 }
1058 else
1059 {
1060 std::shared_ptr<SygusPrintCallback> spc;
1061 if (is_dup_op)
1062 {
1063 Debug("parser-sygus") << "--> Duplicate gterm operator : " << ops[i]
1064 << std::endl;
1065 // make into define-fun
1066 std::vector<Type> ltypes;
1067 for (unsigned j = 0, size = cargs[i].size(); j < size; j++)
1068 {
1069 ltypes.push_back(sygus_to_builtin[cargs[i][j]]);
1070 }
1071 std::vector<Expr> largs;
1072 Expr lbvl = makeSygusBoundVarList(dt, i, ltypes, largs);
1073
1074 // make the let_body
1075 std::vector<Expr> children;
1076 if (ops[i].getKind() != kind::BUILTIN)
1077 {
1078 children.push_back(ops[i]);
1079 }
1080 children.insert(children.end(), largs.begin(), largs.end());
1081 Kind sk = ops[i].getKind() != kind::BUILTIN
1082 ? kind::APPLY_UF
1083 : getExprManager()->operatorToKind(ops[i]);
1084 Expr body = getExprManager()->mkExpr(sk, children);
1085 // replace by lambda
1086 ops[i] = getExprManager()->mkExpr(kind::LAMBDA, lbvl, body);
1087 Debug("parser-sygus") << " ...replace op : " << ops[i] << std::endl;
1088 // callback prints as the expression
1089 spc = std::make_shared<printer::SygusExprPrintCallback>(body, largs);
1090 }
1091 else
1092 {
1093 std::map<Expr, Expr>::iterator it =
1094 d_sygus_let_func_to_body.find(ops[i]);
1095 if (it != d_sygus_let_func_to_body.end())
1096 {
1097 Debug("parser-sygus") << "--> Let expression " << ops[i] << std::endl;
1098 Expr let_body = it->second;
1099 std::vector<Expr> let_args = d_sygus_let_func_to_vars[ops[i]];
1100 unsigned let_num_input_args =
1101 d_sygus_let_func_to_num_input_vars[ops[i]];
1102 // the operator is just the body for the arguments
1103 std::vector<Type> ltypes;
1104 for (unsigned j = 0, size = let_args.size(); j < size; j++)
1105 {
1106 ltypes.push_back(let_args[j].getType());
1107 }
1108 std::vector<Expr> largs;
1109 Expr lbvl = makeSygusBoundVarList(dt, i, ltypes, largs);
1110 Expr sbody = let_body.substitute(let_args, largs);
1111 ops[i] = getExprManager()->mkExpr(kind::LAMBDA, lbvl, sbody);
1112 Debug("parser-sygus") << " ...replace op : " << ops[i] << std::endl;
1113 // callback prints as a let expression
1114 spc = std::make_shared<printer::SygusLetExprPrintCallback>(
1115 let_body, let_args, let_num_input_args);
1116 }
1117 else if (ops[i].getType().isBitVector() && ops[i].isConst())
1118 {
1119 Debug("parser-sygus") << "--> Bit-vector constant " << ops[i] << " ("
1120 << cnames[i] << ")" << std::endl;
1121 // Since there are multiple output formats for bit-vectors and
1122 // we are required by sygus standards to print in the exact input
1123 // format given by the user, we use a print callback to custom print
1124 // the given name.
1125 spc = std::make_shared<printer::SygusNamedPrintCallback>(cnames[i]);
1126 }
1127 else if (isDefinedFunction(ops[i]))
1128 {
1129 Debug("parser-sygus") << "--> Defined function " << ops[i]
1130 << std::endl;
1131 // turn f into (lammbda (x) (f x))
1132 // in a degenerate case, ops[i] may be a defined constant,
1133 // in which case we do not replace by a lambda.
1134 if (ops[i].getType().isFunction())
1135 {
1136 std::vector<Type> ftypes =
1137 static_cast<FunctionType>(ops[i].getType()).getArgTypes();
1138 std::vector<Expr> largs;
1139 Expr lbvl = makeSygusBoundVarList(dt, i, ftypes, largs);
1140 largs.insert(largs.begin(), ops[i]);
1141 Expr body = getExprManager()->mkExpr(kind::APPLY_UF, largs);
1142 ops[i] = getExprManager()->mkExpr(kind::LAMBDA, lbvl, body);
1143 Debug("parser-sygus") << " ...replace op : " << ops[i]
1144 << std::endl;
1145 }
1146 else
1147 {
1148 Debug("parser-sygus") << " ...replace op : " << ops[i]
1149 << std::endl;
1150 }
1151 // keep a callback to say it should be printed with the defined name
1152 spc = std::make_shared<printer::SygusNamedPrintCallback>(cnames[i]);
1153 }
1154 else
1155 {
1156 Debug("parser-sygus") << "--> Default case " << ops[i] << std::endl;
1157 }
1158 }
1159 // must rename to avoid duplication
1160 std::stringstream ss;
1161 ss << dt.getName() << "_" << i << "_" << cnames[i];
1162 cnames[i] = ss.str();
1163 Debug("parser-sygus") << " construct the datatype " << cnames[i] << "..."
1164 << std::endl;
1165 // add the sygus constructor
1166 dt.addSygusConstructor(ops[i], cnames[i], cargs[i], spc);
1167 Debug("parser-sygus") << " finished constructing the datatype"
1168 << std::endl;
1169 }
1170 }
1171
1172 Debug("parser-sygus") << " add constructors for unresolved symbols..." << std::endl;
1173 if( !unresolved_gterm_sym.empty() ){
1174 std::vector< Type > types;
1175 Debug("parser-sygus") << "...resolve " << unresolved_gterm_sym.size() << " symbols..." << std::endl;
1176 for( unsigned i=0; i<unresolved_gterm_sym.size(); i++ ){
1177 Debug("parser-sygus") << " resolve : " << unresolved_gterm_sym[i] << std::endl;
1178 if( isUnresolvedType(unresolved_gterm_sym[i]) ){
1179 Debug("parser-sygus") << " it is an unresolved type." << std::endl;
1180 Type t = getSort(unresolved_gterm_sym[i]);
1181 if( std::find( types.begin(), types.end(), t )==types.end() ){
1182 types.push_back( t );
1183 //identity element
1184 Type bt = dt.getSygusType();
1185 Debug("parser-sygus") << ": make identity function for " << bt << ", argument type " << t << std::endl;
1186
1187 std::stringstream ss;
1188 ss << t << "_x";
1189 Expr var = mkBoundVar(ss.str(), bt);
1190 std::vector<Expr> lchildren;
1191 lchildren.push_back(
1192 getExprManager()->mkExpr(kind::BOUND_VAR_LIST, var));
1193 lchildren.push_back(var);
1194 Expr id_op = getExprManager()->mkExpr(kind::LAMBDA, lchildren);
1195
1196 // empty sygus callback (should not be printed)
1197 std::shared_ptr<SygusPrintCallback> sepc =
1198 std::make_shared<printer::SygusEmptyPrintCallback>();
1199
1200 //make the sygus argument list
1201 std::vector< Type > id_carg;
1202 id_carg.push_back( t );
1203 dt.addSygusConstructor(id_op, unresolved_gterm_sym[i], id_carg, sepc);
1204
1205 //add to operators
1206 ops.push_back( id_op );
1207 }
1208 }else{
1209 Debug("parser-sygus") << " ignore. (likely a free let variable)" << std::endl;
1210 }
1211 }
1212 }
1213 }
1214
1215 Expr Smt2::makeSygusBoundVarList(Datatype& dt,
1216 unsigned i,
1217 const std::vector<Type>& ltypes,
1218 std::vector<Expr>& lvars)
1219 {
1220 for (unsigned j = 0, size = ltypes.size(); j < size; j++)
1221 {
1222 std::stringstream ss;
1223 ss << dt.getName() << "_x_" << i << "_" << j;
1224 Expr v = mkBoundVar(ss.str(), ltypes[j]);
1225 lvars.push_back(v);
1226 }
1227 return getExprManager()->mkExpr(kind::BOUND_VAR_LIST, lvars);
1228 }
1229
1230 InputLanguage Smt2::getLanguage() const
1231 {
1232 ExprManager* em = getExprManager();
1233 return em->getOptions().getInputLanguage();
1234 }
1235
1236 }/* CVC4::parser namespace */
1237 }/* CVC4 namespace */