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