1 /********************* */
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
12 ** \brief Definitions of SMT2 constants.
14 ** Definitions of SMT2 constants.
16 #include "parser/smt2/smt2.h"
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"
31 // ANTLR defines these, which is really bad!
38 Smt2::Smt2(api::Solver
* solver
, Input
* input
, bool strictMode
, bool parseOnly
)
39 : Parser(solver
, input
, strictMode
, parseOnly
), d_logicSet(false)
41 if (!strictModeEnabled())
43 addTheory(Smt2::THEORY_CORE
);
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
);
57 // NOTE: this operator is non-standard
58 addOperator(kind::POW
, "^");
61 void Smt2::addTranscendentalOperators()
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");
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");
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
);
120 Parser::addOperator(kind::INT_TO_BITVECTOR
);
121 Parser::addOperator(kind::BITVECTOR_TO_NAT
);
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
)
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");
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");
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.<=");
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");
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
);
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
);
213 void Smt2::addTheory(Theory theory
) {
216 addOperator(kind::SELECT
, "select");
217 addOperator(kind::STORE
, "store");
220 case THEORY_BITVECTORS
:
221 addBitvectorOperators();
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
);
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
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
);
255 defineType("Real", getExprManager()->realType());
256 addArithmeticOperators();
257 Parser::addOperator(kind::DIVISION
);
260 case THEORY_TRANSCENDENTALS
: addTranscendentalOperators(); break;
262 case THEORY_QUANTIFIERS
:
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");
281 case THEORY_DATATYPES
:
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
);
293 defineType("String", getExprManager()->stringType());
294 defineType("RegLan", getExprManager()->regExpType());
295 defineType("Int", getExprManager()->integerType());
296 addStringOperators();
300 Parser::addOperator(kind::APPLY_UF
);
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();
317 std::stringstream ss
;
318 ss
<< "internal error: unsupported theory " << theory
;
319 throw ParserException(ss
.str());
323 void Smt2::addOperator(Kind kind
, const std::string
& name
) {
324 Debug("parser") << "Smt2::addOperator( " << kind
<< ", " << name
<< " )"
326 Parser::addOperator(kind
);
327 operatorKindMap
[name
] = kind
;
330 Kind
Smt2::getOperatorKind(const std::string
& name
) const {
331 // precondition: isOperatorEnabled(name)
332 return operatorKindMap
.find(name
)->second
;
335 bool Smt2::isOperatorEnabled(const std::string
& name
) const {
336 return operatorKindMap
.find(name
) != operatorKindMap
.end();
339 bool Smt2::isTheoryEnabled(Theory theory
) const {
342 return d_logic
.isTheoryEnabled(theory::THEORY_ARRAYS
);
343 case THEORY_BITVECTORS
:
344 return d_logic
.isTheoryEnabled(theory::THEORY_BV
);
347 case THEORY_DATATYPES
:
348 return d_logic
.isTheoryEnabled(theory::THEORY_DATATYPES
);
350 return d_logic
.isTheoryEnabled(theory::THEORY_ARITH
) &&
351 d_logic
.areIntegersUsed() && ( !d_logic
.areRealsUsed() );
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();
361 return d_logic
.isTheoryEnabled(theory::THEORY_SETS
);
363 return d_logic
.isTheoryEnabled(theory::THEORY_STRINGS
);
365 return d_logic
.isTheoryEnabled(theory::THEORY_UF
);
367 return d_logic
.isTheoryEnabled(theory::THEORY_FP
);
369 return d_logic
.isTheoryEnabled(theory::THEORY_SEP
);
371 std::stringstream ss
;
372 ss
<< "internal error: unsupported theory " << theory
;
373 throw ParserException(ss
.str());
377 bool Smt2::logicIsSet() {
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
);
389 return Parser::getExpressionForNameAndType(name
, t
);
393 Expr
Smt2::mkDefineFunRec(
394 const std::string
& fname
,
395 const std::vector
<std::pair
<std::string
, Type
> >& sortedVarNames
,
397 std::vector
<Expr
>& flattenVars
)
399 std::vector
<Type
> sorts
;
400 for (const std::pair
<std::string
, CVC4::Type
>& svn
: sortedVarNames
)
402 sorts
.push_back(svn
.second
);
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
);
410 return mkVar(fname
, ft
, ExprManager::VAR_FLAG_NONE
, true);
413 void Smt2::pushDefineFunRecScope(
414 const std::vector
<std::pair
<std::string
, Type
> >& sortedVarNames
,
416 const std::vector
<Expr
>& flattenVars
,
417 std::vector
<Expr
>& bvs
,
420 pushScope(bindingLevel
);
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
)
426 Expr v
= mkBoundVar(svn
.first
, svn
.second
);
430 bvs
.insert(bvs
.end(), flattenVars
.begin(), flattenVars
.end());
435 d_logic
= LogicInfo();
436 operatorKindMap
.clear();
437 d_lastNamedTerm
= std::pair
<Expr
, std::string
>();
438 this->Parser::reset();
440 if( !strictModeEnabled() ) {
441 addTheory(Smt2::THEORY_CORE
);
445 void Smt2::resetAssertions() {
446 // Remove all declarations except the ones at level 0.
447 while (this->scopeLevel() > 0) {
452 void Smt2::setLogic(std::string name
) {
455 // non-smt2-standard sygus logic names go here (http://sygus.seas.upenn.edu/files/sygus.pdf Section 3.2)
456 if(name
== "Arrays") {
458 }else if(name
== "Reals") {
464 if(logicIsForced()) {
465 d_logic
= getForcedLogic();
470 // if sygus is enabled, we must enable UF, datatypes, integer arithmetic and
473 if (!d_logic
.isQuantified())
475 warning("Logics in sygus are assumed to contain quantifiers.");
476 warning("Omit QF_ from the logic to avoid this warning.");
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();
489 // Core theory belongs to every logic
490 addTheory(THEORY_CORE
);
492 if(d_logic
.isTheoryEnabled(theory::THEORY_UF
)) {
493 addTheory(THEORY_UF
);
496 if(d_logic
.isTheoryEnabled(theory::THEORY_ARITH
)) {
497 if(d_logic
.areIntegersUsed()) {
498 if(d_logic
.areRealsUsed()) {
499 addTheory(THEORY_REALS_INTS
);
501 addTheory(THEORY_INTS
);
503 } else if(d_logic
.areRealsUsed()) {
504 addTheory(THEORY_REALS
);
507 if (d_logic
.areTranscendentalsUsed())
509 addTheory(THEORY_TRANSCENDENTALS
);
513 if(d_logic
.isTheoryEnabled(theory::THEORY_ARRAYS
)) {
514 addTheory(THEORY_ARRAYS
);
517 if(d_logic
.isTheoryEnabled(theory::THEORY_BV
)) {
518 addTheory(THEORY_BITVECTORS
);
521 if(d_logic
.isTheoryEnabled(theory::THEORY_DATATYPES
)) {
522 addTheory(THEORY_DATATYPES
);
525 if(d_logic
.isTheoryEnabled(theory::THEORY_SETS
)) {
526 addTheory(THEORY_SETS
);
529 if(d_logic
.isTheoryEnabled(theory::THEORY_STRINGS
)) {
530 addTheory(THEORY_STRINGS
);
533 if(d_logic
.isQuantified()) {
534 addTheory(THEORY_QUANTIFIERS
);
537 if (d_logic
.isTheoryEnabled(theory::THEORY_FP
)) {
538 addTheory(THEORY_FP
);
541 if (d_logic
.isTheoryEnabled(theory::THEORY_SEP
)) {
542 addTheory(THEORY_SEP
);
545 }/* Smt2::setLogic() */
547 void Smt2::setInfo(const std::string
& flag
, const SExpr
& sexpr
) {
551 void Smt2::setOption(const std::string
& flag
, const SExpr
& sexpr
) {
555 void Smt2::checkThatLogicIsSet() {
556 if( ! logicIsSet() ) {
557 if(strictModeEnabled()) {
558 parseError("set-logic must appear before this point.");
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).");
567 Command
* c
= new SetBenchmarkLogicCommand("ALL");
574 /* The include are managed in the lexer but called in the parser */
575 // Inspired by http://www.antlr3.org/api/C/interop.html
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.
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 */
589 Debug("parser") << "Can't open " << filename
<< std::endl
;
592 // Same thing as the predefined PUSHSTREAM(in);
593 lexer
->pushCharStream(lexer
, in
);
595 //lexer->rec->state->tokenStartCharIndex = -10;
596 //lexer->emit(lexer);
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.
606 //TODO what said before
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.");
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();
622 // Find the directory of the current input file
624 size_t pos
= inputName
.rfind('/');
625 if(pos
!= std::string::npos
) {
626 path
= std::string(inputName
, 0, pos
+ 1);
628 path
.append(filename
);
629 if(!newInputStream(path
, lexer
)) {
630 parseError("Couldn't open include file `" + path
+ "'");
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));
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
667 << (sgt
.d_gterm_type
== SygusGTerm::gterm_let
)
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
;
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
);
683 if((pos
= sgt
.d_name
.find(oldName
, pos
)) != std::string::npos
){
684 sgt
.d_name
.replace(pos
, oldName
.length(), newName
);
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
696 pushSygusDatatypeDef( null_type
, sub_dname
, datatypes
, sorts
, ops
, cnames
, cargs
, allow_const
, unresolved_gterm_sym
);
697 int sub_dt_index
= datatypes
.size()-1;
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
);
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
);
712 }else if( sgt
.d_gterm_type
==SygusGTerm::gterm_constant
){
713 if( sgt
.getNumChildren()!=0 ){
714 parseError("Bad syntax for Sygus Constant.");
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
;
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
>() );
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.");
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
;
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
>() );
743 }else if( sgt
.d_gterm_type
==SygusGTerm::gterm_nested_sort
){
745 }else if( sgt
.d_gterm_type
==SygusGTerm::gterm_unresolved
){
747 if( isUnresolvedType(sgt
.d_name
) ){
748 ret
= getSort(sgt
.d_name
);
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());
756 //will resolve when adding constructors
757 unresolved_gterm_sym
[index
].push_back(sgt
.d_name
);
759 }else if( sgt
.d_gterm_type
==SygusGTerm::gterm_ignore
){
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
){
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
>());
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
){
790 datatypes
.pop_back();
794 allow_const
.pop_back();
795 unresolved_gterm_sym
.pop_back();
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
) {
809 Debug("parser-sygus") << "Argument is ";
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."));
818 Expr sop
= ops
[sub_dt_index
][0];
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)
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())
830 d_sygus_bound_var_type
[sop
] = t
;
834 std::vector
< Expr
> children
;
835 if( sop
.getKind() != kind::BUILTIN
){
836 children
.push_back( sop
);
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
;
848 parseError(ss
.str());
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
];
858 Debug("parser-sygus") << ": child " << i
<< " existing sygus to builtin expr : " << it
->second
<< std::endl
;
859 children
.push_back( it
->second
);
862 Kind sk
= sop
.getKind() != kind::BUILTIN
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
;
871 sorts
[sub_dt_index
] = curr_t
;
872 sygus_to_builtin
[t
] = curr_t
;
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
);
883 void Smt2::processSygusLetConstructor( std::vector
< CVC4::Expr
>& let_vars
,
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
;
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
;
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
];
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
] );
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;
932 //last argument is the return, pop
933 cargs
[index
][dindex
].pop_back();
934 collectSygusLetArgs( let_body
, cargs
[index
][dindex
], let_define_args
);
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())
942 std::stringstream ss
;
943 ss
<< "Wrong number of let body terms." << std::endl
;
944 parseError(ss
.str());
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());
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
) );
959 ops
[index
].pop_back();
960 ops
[index
].push_back( let_func
);
961 cnames
[index
].pop_back();
962 cnames
[index
].push_back(ss
.str());
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();
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());
983 for( unsigned i
=0; i
<e
.getNumChildren(); i
++ ){
984 collectSygusLetArgs( e
[i
], sygusArgs
, builtinArgs
);
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
) {
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
];
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
;
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
;
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
++)
1025 bool is_dup
= false;
1026 bool is_dup_op
= false;
1027 for (unsigned j
= 0; j
< i
; j
++)
1029 if( ops
[i
]==ops
[j
] ){
1031 if( cargs
[i
].size()==cargs
[j
].size() ){
1033 for( unsigned k
=0; k
<cargs
[i
].size(); k
++ ){
1034 if( cargs
[i
][k
]!=cargs
[j
][k
] ){
1045 Debug("parser-sygus") << "SYGUS CONS " << i
<< " : ";
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 );
1055 std::shared_ptr
<SygusPrintCallback
> spc
;
1058 Debug("parser-sygus") << "--> Duplicate gterm operator : " << ops
[i
]
1060 // make into define-fun
1061 std::vector
<Type
> ltypes
;
1062 for (unsigned j
= 0, size
= cargs
[i
].size(); j
< size
; j
++)
1064 ltypes
.push_back(sygus_to_builtin
[cargs
[i
][j
]]);
1066 std::vector
<Expr
> largs
;
1067 Expr lbvl
= makeSygusBoundVarList(dt
, i
, ltypes
, largs
);
1069 // make the let_body
1070 std::vector
<Expr
> children
;
1071 if (ops
[i
].getKind() != kind::BUILTIN
)
1073 children
.push_back(ops
[i
]);
1075 children
.insert(children
.end(), largs
.begin(), largs
.end());
1076 Kind sk
= ops
[i
].getKind() != kind::BUILTIN
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
);
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())
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
++)
1101 ltypes
.push_back(let_args
[j
].getType());
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
);
1112 else if (ops
[i
].getType().isBitVector() && ops
[i
].isConst())
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
1120 spc
= std::make_shared
<printer::SygusNamedPrintCallback
>(cnames
[i
]);
1122 else if (isDefinedFunction(ops
[i
]))
1124 Debug("parser-sygus") << "--> Defined function " << ops
[i
]
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())
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
]
1143 Debug("parser-sygus") << " ...replace op : " << ops
[i
]
1146 // keep a callback to say it should be printed with the defined name
1147 spc
= std::make_shared
<printer::SygusNamedPrintCallback
>(cnames
[i
]);
1151 Debug("parser-sygus") << "--> Default case " << ops
[i
] << std::endl
;
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
] << "..."
1160 // add the sygus constructor
1161 dt
.addSygusConstructor(ops
[i
], cnames
[i
], cargs
[i
], spc
);
1162 Debug("parser-sygus") << " finished constructing the datatype"
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
);
1179 Type bt
= dt
.getSygusType();
1180 Debug("parser-sygus") << ": make identity function for " << bt
<< ", argument type " << t
<< std::endl
;
1182 std::stringstream ss
;
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
);
1191 // empty sygus callback (should not be printed)
1192 std::shared_ptr
<SygusPrintCallback
> sepc
=
1193 std::make_shared
<printer::SygusEmptyPrintCallback
>();
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
);
1201 ops
.push_back( id_op
);
1204 Debug("parser-sygus") << " ignore. (likely a free let variable)" << std::endl
;
1210 Expr
Smt2::makeSygusBoundVarList(Datatype
& dt
,
1212 const std::vector
<Type
>& ltypes
,
1213 std::vector
<Expr
>& lvars
)
1215 for (unsigned j
= 0, size
= ltypes
.size(); j
< size
; j
++)
1217 std::stringstream ss
;
1218 ss
<< dt
.getName() << "_x_" << i
<< "_" << j
;
1219 Expr v
= mkBoundVar(ss
.str(), ltypes
[j
]);
1222 return getExprManager()->mkExpr(kind::BOUND_VAR_LIST
, lvars
);
1225 InputLanguage
Smt2::getLanguage() const
1227 ExprManager
* em
= getExprManager();
1228 return em
->getOptions().getInputLanguage();
1231 }/* CVC4::parser namespace */
1232 }/* CVC4 namespace */