1 /********************* */
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Andres Noetzli, Morgan Deters
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 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"
20 #include "base/check.h"
21 #include "expr/type.h"
22 #include "options/options.h"
23 #include "parser/antlr_input.h"
24 #include "parser/parser.h"
25 #include "parser/smt2/smt2_input.h"
26 #include "util/bitvector.h"
28 // ANTLR defines these, which is really bad!
35 Smt2::Smt2(api::Solver
* solver
,
40 : Parser(solver
, sm
, input
, strictMode
, parseOnly
),
47 Smt2::~Smt2() { popScope(); }
49 void Smt2::addArithmeticOperators() {
50 addOperator(api::PLUS
, "+");
51 addOperator(api::MINUS
, "-");
52 // api::MINUS is converted to api::UMINUS if there is only a single operand
53 Parser::addOperator(api::UMINUS
);
54 addOperator(api::MULT
, "*");
55 addOperator(api::LT
, "<");
56 addOperator(api::LEQ
, "<=");
57 addOperator(api::GT
, ">");
58 addOperator(api::GEQ
, ">=");
60 if (!strictModeEnabled())
62 // NOTE: this operator is non-standard
63 addOperator(api::POW
, "^");
67 void Smt2::addTranscendentalOperators()
69 addOperator(api::EXPONENTIAL
, "exp");
70 addOperator(api::SINE
, "sin");
71 addOperator(api::COSINE
, "cos");
72 addOperator(api::TANGENT
, "tan");
73 addOperator(api::COSECANT
, "csc");
74 addOperator(api::SECANT
, "sec");
75 addOperator(api::COTANGENT
, "cot");
76 addOperator(api::ARCSINE
, "arcsin");
77 addOperator(api::ARCCOSINE
, "arccos");
78 addOperator(api::ARCTANGENT
, "arctan");
79 addOperator(api::ARCCOSECANT
, "arccsc");
80 addOperator(api::ARCSECANT
, "arcsec");
81 addOperator(api::ARCCOTANGENT
, "arccot");
82 addOperator(api::SQRT
, "sqrt");
85 void Smt2::addQuantifiersOperators()
87 if (!strictModeEnabled())
89 addOperator(api::INST_CLOSURE
, "inst-closure");
93 void Smt2::addBitvectorOperators() {
94 addOperator(api::BITVECTOR_CONCAT
, "concat");
95 addOperator(api::BITVECTOR_NOT
, "bvnot");
96 addOperator(api::BITVECTOR_AND
, "bvand");
97 addOperator(api::BITVECTOR_OR
, "bvor");
98 addOperator(api::BITVECTOR_NEG
, "bvneg");
99 addOperator(api::BITVECTOR_PLUS
, "bvadd");
100 addOperator(api::BITVECTOR_MULT
, "bvmul");
101 addOperator(api::BITVECTOR_UDIV
, "bvudiv");
102 addOperator(api::BITVECTOR_UREM
, "bvurem");
103 addOperator(api::BITVECTOR_SHL
, "bvshl");
104 addOperator(api::BITVECTOR_LSHR
, "bvlshr");
105 addOperator(api::BITVECTOR_ULT
, "bvult");
106 addOperator(api::BITVECTOR_NAND
, "bvnand");
107 addOperator(api::BITVECTOR_NOR
, "bvnor");
108 addOperator(api::BITVECTOR_XOR
, "bvxor");
109 addOperator(api::BITVECTOR_XNOR
, "bvxnor");
110 addOperator(api::BITVECTOR_COMP
, "bvcomp");
111 addOperator(api::BITVECTOR_SUB
, "bvsub");
112 addOperator(api::BITVECTOR_SDIV
, "bvsdiv");
113 addOperator(api::BITVECTOR_SREM
, "bvsrem");
114 addOperator(api::BITVECTOR_SMOD
, "bvsmod");
115 addOperator(api::BITVECTOR_ASHR
, "bvashr");
116 addOperator(api::BITVECTOR_ULE
, "bvule");
117 addOperator(api::BITVECTOR_UGT
, "bvugt");
118 addOperator(api::BITVECTOR_UGE
, "bvuge");
119 addOperator(api::BITVECTOR_SLT
, "bvslt");
120 addOperator(api::BITVECTOR_SLE
, "bvsle");
121 addOperator(api::BITVECTOR_SGT
, "bvsgt");
122 addOperator(api::BITVECTOR_SGE
, "bvsge");
123 addOperator(api::BITVECTOR_REDOR
, "bvredor");
124 addOperator(api::BITVECTOR_REDAND
, "bvredand");
126 addIndexedOperator(api::BITVECTOR_EXTRACT
, api::BITVECTOR_EXTRACT
, "extract");
127 addIndexedOperator(api::BITVECTOR_REPEAT
, api::BITVECTOR_REPEAT
, "repeat");
129 api::BITVECTOR_ZERO_EXTEND
, api::BITVECTOR_ZERO_EXTEND
, "zero_extend");
131 api::BITVECTOR_SIGN_EXTEND
, api::BITVECTOR_SIGN_EXTEND
, "sign_extend");
133 api::BITVECTOR_ROTATE_LEFT
, api::BITVECTOR_ROTATE_LEFT
, "rotate_left");
135 api::BITVECTOR_ROTATE_RIGHT
, api::BITVECTOR_ROTATE_RIGHT
, "rotate_right");
138 void Smt2::addDatatypesOperators()
140 Parser::addOperator(api::APPLY_CONSTRUCTOR
);
141 Parser::addOperator(api::APPLY_TESTER
);
142 Parser::addOperator(api::APPLY_SELECTOR
);
144 if (!strictModeEnabled())
146 addOperator(api::DT_SIZE
, "dt.size");
150 void Smt2::addStringOperators() {
153 getSolver()->mkTerm(api::REGEXP_STAR
, getSolver()->mkRegexpSigma()));
154 addOperator(api::STRING_CONCAT
, "str.++");
155 addOperator(api::STRING_LENGTH
, "str.len");
156 addOperator(api::STRING_SUBSTR
, "str.substr");
157 addOperator(api::STRING_CONTAINS
, "str.contains");
158 addOperator(api::STRING_CHARAT
, "str.at");
159 addOperator(api::STRING_INDEXOF
, "str.indexof");
160 addOperator(api::STRING_REPLACE
, "str.replace");
161 addOperator(api::STRING_PREFIX
, "str.prefixof");
162 addOperator(api::STRING_SUFFIX
, "str.suffixof");
163 addOperator(api::STRING_FROM_CODE
, "str.from_code");
164 addOperator(api::STRING_IS_DIGIT
, "str.is_digit");
165 addOperator(api::STRING_REPLACE_RE
, "str.replace_re");
166 addOperator(api::STRING_REPLACE_RE_ALL
, "str.replace_re_all");
167 if (!strictModeEnabled())
169 addOperator(api::STRING_UPDATE
, "str.update");
170 addOperator(api::STRING_TOLOWER
, "str.tolower");
171 addOperator(api::STRING_TOUPPER
, "str.toupper");
172 addOperator(api::STRING_REV
, "str.rev");
174 addOperator(api::SEQ_CONCAT
, "seq.++");
175 addOperator(api::SEQ_LENGTH
, "seq.len");
176 addOperator(api::SEQ_EXTRACT
, "seq.extract");
177 addOperator(api::SEQ_UPDATE
, "seq.update");
178 addOperator(api::SEQ_AT
, "seq.at");
179 addOperator(api::SEQ_CONTAINS
, "seq.contains");
180 addOperator(api::SEQ_INDEXOF
, "seq.indexof");
181 addOperator(api::SEQ_REPLACE
, "seq.replace");
182 addOperator(api::SEQ_PREFIX
, "seq.prefixof");
183 addOperator(api::SEQ_SUFFIX
, "seq.suffixof");
184 addOperator(api::SEQ_REV
, "seq.rev");
185 addOperator(api::SEQ_REPLACE_ALL
, "seq.replace_all");
186 addOperator(api::SEQ_UNIT
, "seq.unit");
187 addOperator(api::SEQ_NTH
, "seq.nth");
189 // at the moment, we only use this syntax for smt2.6
190 if (getLanguage() == language::input::LANG_SMTLIB_V2_6
191 || getLanguage() == language::input::LANG_SYGUS_V2
)
193 addOperator(api::STRING_FROM_INT
, "str.from_int");
194 addOperator(api::STRING_TO_INT
, "str.to_int");
195 addOperator(api::STRING_IN_REGEXP
, "str.in_re");
196 addOperator(api::STRING_TO_REGEXP
, "str.to_re");
197 addOperator(api::STRING_TO_CODE
, "str.to_code");
198 addOperator(api::STRING_REPLACE_ALL
, "str.replace_all");
202 addOperator(api::STRING_FROM_INT
, "int.to.str");
203 addOperator(api::STRING_TO_INT
, "str.to.int");
204 addOperator(api::STRING_IN_REGEXP
, "str.in.re");
205 addOperator(api::STRING_TO_REGEXP
, "str.to.re");
206 addOperator(api::STRING_TO_CODE
, "str.code");
207 addOperator(api::STRING_REPLACE_ALL
, "str.replaceall");
210 addOperator(api::REGEXP_CONCAT
, "re.++");
211 addOperator(api::REGEXP_UNION
, "re.union");
212 addOperator(api::REGEXP_INTER
, "re.inter");
213 addOperator(api::REGEXP_STAR
, "re.*");
214 addOperator(api::REGEXP_PLUS
, "re.+");
215 addOperator(api::REGEXP_OPT
, "re.opt");
216 addIndexedOperator(api::REGEXP_REPEAT
, api::REGEXP_REPEAT
, "re.^");
217 addIndexedOperator(api::REGEXP_LOOP
, api::REGEXP_LOOP
, "re.loop");
218 addOperator(api::REGEXP_RANGE
, "re.range");
219 addOperator(api::REGEXP_COMPLEMENT
, "re.comp");
220 addOperator(api::REGEXP_DIFF
, "re.diff");
221 addOperator(api::STRING_LT
, "str.<");
222 addOperator(api::STRING_LEQ
, "str.<=");
225 void Smt2::addFloatingPointOperators() {
226 addOperator(api::FLOATINGPOINT_FP
, "fp");
227 addOperator(api::FLOATINGPOINT_EQ
, "fp.eq");
228 addOperator(api::FLOATINGPOINT_ABS
, "fp.abs");
229 addOperator(api::FLOATINGPOINT_NEG
, "fp.neg");
230 addOperator(api::FLOATINGPOINT_PLUS
, "fp.add");
231 addOperator(api::FLOATINGPOINT_SUB
, "fp.sub");
232 addOperator(api::FLOATINGPOINT_MULT
, "fp.mul");
233 addOperator(api::FLOATINGPOINT_DIV
, "fp.div");
234 addOperator(api::FLOATINGPOINT_FMA
, "fp.fma");
235 addOperator(api::FLOATINGPOINT_SQRT
, "fp.sqrt");
236 addOperator(api::FLOATINGPOINT_REM
, "fp.rem");
237 addOperator(api::FLOATINGPOINT_RTI
, "fp.roundToIntegral");
238 addOperator(api::FLOATINGPOINT_MIN
, "fp.min");
239 addOperator(api::FLOATINGPOINT_MAX
, "fp.max");
240 addOperator(api::FLOATINGPOINT_LEQ
, "fp.leq");
241 addOperator(api::FLOATINGPOINT_LT
, "fp.lt");
242 addOperator(api::FLOATINGPOINT_GEQ
, "fp.geq");
243 addOperator(api::FLOATINGPOINT_GT
, "fp.gt");
244 addOperator(api::FLOATINGPOINT_ISN
, "fp.isNormal");
245 addOperator(api::FLOATINGPOINT_ISSN
, "fp.isSubnormal");
246 addOperator(api::FLOATINGPOINT_ISZ
, "fp.isZero");
247 addOperator(api::FLOATINGPOINT_ISINF
, "fp.isInfinite");
248 addOperator(api::FLOATINGPOINT_ISNAN
, "fp.isNaN");
249 addOperator(api::FLOATINGPOINT_ISNEG
, "fp.isNegative");
250 addOperator(api::FLOATINGPOINT_ISPOS
, "fp.isPositive");
251 addOperator(api::FLOATINGPOINT_TO_REAL
, "fp.to_real");
253 addIndexedOperator(api::FLOATINGPOINT_TO_FP_GENERIC
,
254 api::FLOATINGPOINT_TO_FP_GENERIC
,
256 addIndexedOperator(api::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR
,
257 api::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR
,
260 api::FLOATINGPOINT_TO_UBV
, api::FLOATINGPOINT_TO_UBV
, "fp.to_ubv");
262 api::FLOATINGPOINT_TO_SBV
, api::FLOATINGPOINT_TO_SBV
, "fp.to_sbv");
264 if (!strictModeEnabled())
266 addIndexedOperator(api::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR
,
267 api::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR
,
269 addIndexedOperator(api::FLOATINGPOINT_TO_FP_FLOATINGPOINT
,
270 api::FLOATINGPOINT_TO_FP_FLOATINGPOINT
,
272 addIndexedOperator(api::FLOATINGPOINT_TO_FP_REAL
,
273 api::FLOATINGPOINT_TO_FP_REAL
,
275 addIndexedOperator(api::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR
,
276 api::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR
,
281 void Smt2::addSepOperators() {
282 addOperator(api::SEP_STAR
, "sep");
283 addOperator(api::SEP_PTO
, "pto");
284 addOperator(api::SEP_WAND
, "wand");
285 addOperator(api::SEP_EMP
, "emp");
286 Parser::addOperator(api::SEP_STAR
);
287 Parser::addOperator(api::SEP_PTO
);
288 Parser::addOperator(api::SEP_WAND
);
289 Parser::addOperator(api::SEP_EMP
);
292 void Smt2::addCoreSymbols()
294 defineType("Bool", d_solver
->getBooleanSort(), true, true);
295 defineVar("true", d_solver
->mkTrue(), true, true);
296 defineVar("false", d_solver
->mkFalse(), true, true);
297 addOperator(api::AND
, "and");
298 addOperator(api::DISTINCT
, "distinct");
299 addOperator(api::EQUAL
, "=");
300 addOperator(api::IMPLIES
, "=>");
301 addOperator(api::ITE
, "ite");
302 addOperator(api::NOT
, "not");
303 addOperator(api::OR
, "or");
304 addOperator(api::XOR
, "xor");
307 void Smt2::addOperator(api::Kind kind
, const std::string
& name
)
309 Debug("parser") << "Smt2::addOperator( " << kind
<< ", " << name
<< " )"
311 Parser::addOperator(kind
);
312 operatorKindMap
[name
] = kind
;
315 void Smt2::addIndexedOperator(api::Kind tKind
,
317 const std::string
& name
)
319 Parser::addOperator(tKind
);
320 d_indexedOpKindMap
[name
] = opKind
;
323 api::Kind
Smt2::getOperatorKind(const std::string
& name
) const
325 // precondition: isOperatorEnabled(name)
326 return operatorKindMap
.find(name
)->second
;
329 bool Smt2::isOperatorEnabled(const std::string
& name
) const {
330 return operatorKindMap
.find(name
) != operatorKindMap
.end();
333 bool Smt2::isTheoryEnabled(theory::TheoryId theory
) const
335 return d_logic
.isTheoryEnabled(theory
);
338 bool Smt2::isHoEnabled() const
340 return getLogic().isHigherOrder() && d_solver
->getOptions().getUfHo();
343 bool Smt2::logicIsSet() {
347 api::Term
Smt2::getExpressionForNameAndType(const std::string
& name
,
350 if (isAbstractValue(name
))
352 return mkAbstractValue(name
);
354 return Parser::getExpressionForNameAndType(name
, t
);
357 bool Smt2::getTesterName(api::Term cons
, std::string
& name
)
359 if ((v2_6() || sygus_v2()) && strictModeEnabled())
361 // 2.6 or above uses indexed tester symbols, if we are in strict mode,
362 // we do not automatically define is-cons for constructor cons.
365 std::stringstream ss
;
371 api::Term
Smt2::mkIndexedConstant(const std::string
& name
,
372 const std::vector
<uint64_t>& numerals
)
374 if (d_logic
.isTheoryEnabled(theory::THEORY_FP
))
378 return d_solver
->mkPosInf(numerals
[0], numerals
[1]);
380 else if (name
== "-oo")
382 return d_solver
->mkNegInf(numerals
[0], numerals
[1]);
384 else if (name
== "NaN")
386 return d_solver
->mkNaN(numerals
[0], numerals
[1]);
388 else if (name
== "+zero")
390 return d_solver
->mkPosZero(numerals
[0], numerals
[1]);
392 else if (name
== "-zero")
394 return d_solver
->mkNegZero(numerals
[0], numerals
[1]);
398 if (d_logic
.isTheoryEnabled(theory::THEORY_BV
) && name
.find("bv") == 0)
400 std::string bvStr
= name
.substr(2);
401 return d_solver
->mkBitVector(numerals
[0], bvStr
, 10);
404 // NOTE: Theory parametric constants go here
406 parseError(std::string("Unknown indexed literal `") + name
+ "'");
410 api::Op
Smt2::mkIndexedOp(const std::string
& name
,
411 const std::vector
<uint64_t>& numerals
)
413 const auto& kIt
= d_indexedOpKindMap
.find(name
);
414 if (kIt
!= d_indexedOpKindMap
.end())
416 api::Kind k
= (*kIt
).second
;
417 if (numerals
.size() == 1)
419 return d_solver
->mkOp(k
, numerals
[0]);
421 else if (numerals
.size() == 2)
423 return d_solver
->mkOp(k
, numerals
[0], numerals
[1]);
427 parseError(std::string("Unknown indexed function `") + name
+ "'");
431 api::Term
Smt2::bindDefineFunRec(
432 const std::string
& fname
,
433 const std::vector
<std::pair
<std::string
, api::Sort
>>& sortedVarNames
,
435 std::vector
<api::Term
>& flattenVars
)
437 std::vector
<api::Sort
> sorts
;
438 for (const std::pair
<std::string
, api::Sort
>& svn
: sortedVarNames
)
440 sorts
.push_back(svn
.second
);
443 // make the flattened function type, add bound variables
444 // to flattenVars if the defined function was given a function return type.
445 api::Sort ft
= mkFlatFunctionType(sorts
, t
, flattenVars
);
448 return bindVar(fname
, ft
, ExprManager::VAR_FLAG_NONE
, true);
451 void Smt2::pushDefineFunRecScope(
452 const std::vector
<std::pair
<std::string
, api::Sort
>>& sortedVarNames
,
454 const std::vector
<api::Term
>& flattenVars
,
455 std::vector
<api::Term
>& bvs
)
459 // bound variables are those that are explicitly named in the preamble
460 // of the define-fun(s)-rec command, we define them here
461 for (const std::pair
<std::string
, api::Sort
>& svn
: sortedVarNames
)
463 api::Term v
= bindBoundVar(svn
.first
, svn
.second
);
467 bvs
.insert(bvs
.end(), flattenVars
.begin(), flattenVars
.end());
472 d_seenSetLogic
= false;
473 d_logic
= LogicInfo();
474 operatorKindMap
.clear();
475 d_lastNamedTerm
= std::pair
<api::Term
, std::string
>();
476 this->Parser::reset();
480 void Smt2::resetAssertions() {
481 // Remove all declarations except the ones at level 0.
482 while (this->scopeLevel() > 0) {
488 std::unique_ptr
<Command
> Smt2::invConstraint(
489 const std::vector
<std::string
>& names
)
491 checkThatLogicIsSet();
492 Debug("parser-sygus") << "Sygus : define sygus funs..." << std::endl
;
493 Debug("parser-sygus") << "Sygus : read inv-constraint..." << std::endl
;
495 if (names
.size() != 4)
498 "Bad syntax for inv-constraint: expected 4 "
502 std::vector
<api::Term
> terms
;
503 for (const std::string
& name
: names
)
505 if (!isDeclared(name
))
507 std::stringstream ss
;
508 ss
<< "Function " << name
<< " in inv-constraint is not defined.";
509 parseError(ss
.str());
512 terms
.push_back(getVariable(name
));
515 return std::unique_ptr
<Command
>(new SygusInvConstraintCommand(terms
));
518 Command
* Smt2::setLogic(std::string name
, bool fromCommand
)
524 parseError("Only one set-logic is allowed.");
526 d_seenSetLogic
= true;
530 // If the logic is forced, we ignore all set-logic requests from commands.
531 return new EmptyCommand();
538 // if sygus is enabled, we must enable UF, datatypes, integer arithmetic and
541 if (!d_logic
.isQuantified())
543 warning("Logics in sygus are assumed to contain quantifiers.");
544 warning("Omit QF_ from the logic to avoid this warning.");
548 // Core theory belongs to every logic
551 if(d_logic
.isTheoryEnabled(theory::THEORY_UF
)) {
552 Parser::addOperator(api::APPLY_UF
);
554 if (!strictModeEnabled() && d_logic
.hasCardinalityConstraints())
556 addOperator(api::CARDINALITY_CONSTRAINT
, "fmf.card");
557 addOperator(api::CARDINALITY_VALUE
, "fmf.card.val");
561 if(d_logic
.isTheoryEnabled(theory::THEORY_ARITH
)) {
562 if(d_logic
.areIntegersUsed()) {
563 defineType("Int", d_solver
->getIntegerSort(), true, true);
564 addArithmeticOperators();
565 addOperator(api::INTS_DIVISION
, "div");
566 addOperator(api::INTS_MODULUS
, "mod");
567 addOperator(api::ABS
, "abs");
568 addIndexedOperator(api::DIVISIBLE
, api::DIVISIBLE
, "divisible");
571 if (d_logic
.areRealsUsed())
573 defineType("Real", d_solver
->getRealSort(), true, true);
574 addArithmeticOperators();
575 addOperator(api::DIVISION
, "/");
576 if (!strictModeEnabled())
578 addOperator(api::ABS
, "abs");
582 if (d_logic
.areIntegersUsed() && d_logic
.areRealsUsed())
584 addOperator(api::TO_INTEGER
, "to_int");
585 addOperator(api::IS_INTEGER
, "is_int");
586 addOperator(api::TO_REAL
, "to_real");
589 if (d_logic
.areTranscendentalsUsed())
591 defineVar("real.pi", d_solver
->mkTerm(api::PI
));
592 addTranscendentalOperators();
594 if (!strictModeEnabled())
596 // integer version of AND
597 addIndexedOperator(api::IAND
, api::IAND
, "iand");
601 if(d_logic
.isTheoryEnabled(theory::THEORY_ARRAYS
)) {
602 addOperator(api::SELECT
, "select");
603 addOperator(api::STORE
, "store");
604 addOperator(api::EQ_RANGE
, "eqrange");
607 if(d_logic
.isTheoryEnabled(theory::THEORY_BV
)) {
608 addBitvectorOperators();
610 if (!strictModeEnabled() && d_logic
.isTheoryEnabled(theory::THEORY_ARITH
)
611 && d_logic
.areIntegersUsed())
613 // Conversions between bit-vectors and integers
614 addOperator(api::BITVECTOR_TO_NAT
, "bv2nat");
616 api::INT_TO_BITVECTOR
, api::INT_TO_BITVECTOR
, "int2bv");
620 if(d_logic
.isTheoryEnabled(theory::THEORY_DATATYPES
)) {
621 const std::vector
<api::Sort
> types
;
622 defineType("Tuple", d_solver
->mkTupleSort(types
), true, true);
623 addDatatypesOperators();
626 if(d_logic
.isTheoryEnabled(theory::THEORY_SETS
)) {
627 defineVar("emptyset", d_solver
->mkEmptySet(d_solver
->getNullSort()));
628 // the Boolean sort is a placeholder here since we don't have type info
629 // without type annotation
630 defineVar("univset", d_solver
->mkUniverseSet(d_solver
->getBooleanSort()));
632 addOperator(api::UNION
, "union");
633 addOperator(api::INTERSECTION
, "intersection");
634 addOperator(api::SETMINUS
, "setminus");
635 addOperator(api::SUBSET
, "subset");
636 addOperator(api::MEMBER
, "member");
637 addOperator(api::SINGLETON
, "singleton");
638 addOperator(api::INSERT
, "insert");
639 addOperator(api::CARD
, "card");
640 addOperator(api::COMPLEMENT
, "complement");
641 addOperator(api::CHOOSE
, "choose");
642 addOperator(api::IS_SINGLETON
, "is_singleton");
643 addOperator(api::JOIN
, "join");
644 addOperator(api::PRODUCT
, "product");
645 addOperator(api::TRANSPOSE
, "transpose");
646 addOperator(api::TCLOSURE
, "tclosure");
649 if (d_logic
.isTheoryEnabled(theory::THEORY_BAGS
))
651 defineVar("emptybag", d_solver
->mkEmptyBag(d_solver
->getNullSort()));
652 addOperator(api::UNION_MAX
, "union_max");
653 addOperator(api::UNION_DISJOINT
, "union_disjoint");
654 addOperator(api::INTERSECTION_MIN
, "intersection_min");
655 addOperator(api::DIFFERENCE_SUBTRACT
, "difference_subtract");
656 addOperator(api::DIFFERENCE_REMOVE
, "difference_remove");
657 addOperator(api::SUBBAG
, "bag.is_included");
658 addOperator(api::BAG_COUNT
, "bag.count");
659 addOperator(api::DUPLICATE_REMOVAL
, "duplicate_removal");
660 addOperator(api::MK_BAG
, "bag");
661 addOperator(api::BAG_CARD
, "bag.card");
662 addOperator(api::BAG_CHOOSE
, "bag.choose");
663 addOperator(api::BAG_IS_SINGLETON
, "bag.is_singleton");
664 addOperator(api::BAG_FROM_SET
, "bag.from_set");
665 addOperator(api::BAG_TO_SET
, "bag.to_set");
667 if(d_logic
.isTheoryEnabled(theory::THEORY_STRINGS
)) {
668 defineType("String", d_solver
->getStringSort(), true, true);
669 defineType("RegLan", d_solver
->getRegExpSort(), true, true);
670 defineType("Int", d_solver
->getIntegerSort(), true, true);
672 if (getLanguage() == language::input::LANG_SMTLIB_V2_6
673 || getLanguage() == language::input::LANG_SYGUS_V2
)
675 defineVar("re.none", d_solver
->mkRegexpEmpty());
679 defineVar("re.nostr", d_solver
->mkRegexpEmpty());
681 defineVar("re.allchar", d_solver
->mkRegexpSigma());
683 // Boolean is a placeholder
684 defineVar("seq.empty",
685 d_solver
->mkEmptySequence(d_solver
->getBooleanSort()));
687 addStringOperators();
690 if(d_logic
.isQuantified()) {
691 addQuantifiersOperators();
694 if (d_logic
.isTheoryEnabled(theory::THEORY_FP
)) {
695 defineType("RoundingMode", d_solver
->getRoundingModeSort(), true, true);
696 defineType("Float16", d_solver
->mkFloatingPointSort(5, 11), true, true);
697 defineType("Float32", d_solver
->mkFloatingPointSort(8, 24), true, true);
698 defineType("Float64", d_solver
->mkFloatingPointSort(11, 53), true, true);
699 defineType("Float128", d_solver
->mkFloatingPointSort(15, 113), true, true);
701 defineVar("RNE", d_solver
->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_EVEN
));
702 defineVar("roundNearestTiesToEven",
703 d_solver
->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_EVEN
));
704 defineVar("RNA", d_solver
->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_AWAY
));
705 defineVar("roundNearestTiesToAway",
706 d_solver
->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_AWAY
));
707 defineVar("RTP", d_solver
->mkRoundingMode(api::ROUND_TOWARD_POSITIVE
));
708 defineVar("roundTowardPositive",
709 d_solver
->mkRoundingMode(api::ROUND_TOWARD_POSITIVE
));
710 defineVar("RTN", d_solver
->mkRoundingMode(api::ROUND_TOWARD_NEGATIVE
));
711 defineVar("roundTowardNegative",
712 d_solver
->mkRoundingMode(api::ROUND_TOWARD_NEGATIVE
));
713 defineVar("RTZ", d_solver
->mkRoundingMode(api::ROUND_TOWARD_ZERO
));
714 defineVar("roundTowardZero",
715 d_solver
->mkRoundingMode(api::ROUND_TOWARD_ZERO
));
717 addFloatingPointOperators();
720 if (d_logic
.isTheoryEnabled(theory::THEORY_SEP
)) {
721 // the Boolean sort is a placeholder here since we don't have type info
722 // without type annotation
723 defineVar("sep.nil", d_solver
->mkSepNil(d_solver
->getBooleanSort()));
729 new SetBenchmarkLogicCommand(sygus() ? d_logic
.getLogicString() : name
);
730 cmd
->setMuted(!fromCommand
);
732 } /* Smt2::setLogic() */
734 api::Grammar
* Smt2::mkGrammar(const std::vector
<api::Term
>& boundVars
,
735 const std::vector
<api::Term
>& ntSymbols
)
737 d_allocGrammars
.emplace_back(
738 new api::Grammar(d_solver
->mkSygusGrammar(boundVars
, ntSymbols
)));
739 return d_allocGrammars
.back().get();
742 bool Smt2::sygus() const
744 InputLanguage ilang
= getLanguage();
745 return ilang
== language::input::LANG_SYGUS_V2
;
748 bool Smt2::sygus_v2() const
750 return getLanguage() == language::input::LANG_SYGUS_V2
;
753 void Smt2::setInfo(const std::string
& flag
, const SExpr
& sexpr
) {
757 void Smt2::setOption(const std::string
& flag
, const SExpr
& sexpr
) {
761 void Smt2::checkThatLogicIsSet()
765 if (strictModeEnabled())
767 parseError("set-logic must appear before this point.");
771 Command
* cmd
= nullptr;
774 cmd
= setLogic(getForcedLogic(), false);
778 warning("No set-logic command was given before this point.");
779 warning("CVC4 will make all theories available.");
781 "Consider setting a stricter logic for (likely) better "
783 warning("To suppress this warning in the future use (set-logic ALL).");
785 cmd
= setLogic("ALL", false);
792 void Smt2::checkLogicAllowsFreeSorts()
794 if (!d_logic
.isTheoryEnabled(theory::THEORY_UF
)
795 && !d_logic
.isTheoryEnabled(theory::THEORY_ARRAYS
)
796 && !d_logic
.isTheoryEnabled(theory::THEORY_DATATYPES
)
797 && !d_logic
.isTheoryEnabled(theory::THEORY_SETS
)
798 && !d_logic
.isTheoryEnabled(theory::THEORY_BAGS
))
800 parseErrorLogic("Free sort symbols not allowed in ");
804 void Smt2::checkLogicAllowsFunctions()
806 if (!d_logic
.isTheoryEnabled(theory::THEORY_UF
))
809 "Functions (of non-zero arity) cannot "
810 "be declared in logic "
811 + d_logic
.getLogicString() + " unless option --uf-ho is used");
815 /* The include are managed in the lexer but called in the parser */
816 // Inspired by http://www.antlr3.org/api/C/interop.html
818 static bool newInputStream(const std::string
& filename
, pANTLR3_LEXER lexer
) {
819 Debug("parser") << "Including " << filename
<< std::endl
;
820 // Create a new input stream and take advantage of built in stream stacking
821 // in C target runtime.
823 pANTLR3_INPUT_STREAM in
;
824 #ifdef CVC4_ANTLR3_OLD_INPUT_STREAM
825 in
= antlr3AsciiFileStreamNew((pANTLR3_UINT8
) filename
.c_str());
826 #else /* CVC4_ANTLR3_OLD_INPUT_STREAM */
827 in
= antlr3FileStreamNew((pANTLR3_UINT8
) filename
.c_str(), ANTLR3_ENC_8BIT
);
828 #endif /* CVC4_ANTLR3_OLD_INPUT_STREAM */
830 Debug("parser") << "Can't open " << filename
<< std::endl
;
833 // Same thing as the predefined PUSHSTREAM(in);
834 lexer
->pushCharStream(lexer
, in
);
836 //lexer->rec->state->tokenStartCharIndex = -10;
837 //lexer->emit(lexer);
839 // Note that the input stream is not closed when it EOFs, I don't bother
840 // to do it here, but it is up to you to track streams created like this
841 // and destroy them when the whole parse session is complete. Remember that you
842 // don't want to do this until all tokens have been manipulated all the way through
843 // your tree parsers etc as the token does not store the text it just refers
844 // back to the input stream and trying to get the text for it will abort if you
845 // close the input stream too early.
847 //TODO what said before
851 void Smt2::includeFile(const std::string
& filename
) {
852 // security for online version
853 if(!canIncludeFile()) {
854 parseError("include-file feature was disabled for this run.");
858 AntlrInput
* ai
= static_cast<AntlrInput
*>(getInput());
859 pANTLR3_LEXER lexer
= ai
->getAntlr3Lexer();
860 // get the name of the current stream "Does it work inside an include?"
861 const std::string inputName
= ai
->getInputStreamName();
863 // Find the directory of the current input file
865 size_t pos
= inputName
.rfind('/');
866 if(pos
!= std::string::npos
) {
867 path
= std::string(inputName
, 0, pos
+ 1);
869 path
.append(filename
);
870 if(!newInputStream(path
, lexer
)) {
871 parseError("Couldn't open include file `" + path
+ "'");
874 bool Smt2::isAbstractValue(const std::string
& name
)
876 return name
.length() >= 2 && name
[0] == '@' && name
[1] != '0'
877 && name
.find_first_not_of("0123456789", 1) == std::string::npos
;
880 api::Term
Smt2::mkAbstractValue(const std::string
& name
)
882 assert(isAbstractValue(name
));
884 return d_solver
->mkAbstractValue(name
.substr(1));
887 InputLanguage
Smt2::getLanguage() const
889 return d_solver
->getOptions().getInputLanguage();
892 void Smt2::parseOpApplyTypeAscription(ParseOp
& p
, api::Sort type
)
894 Debug("parser") << "parseOpApplyTypeAscription : " << p
<< " " << type
896 // (as const (Array T1 T2))
897 if (p
.d_kind
== api::CONST_ARRAY
)
901 std::stringstream ss
;
902 ss
<< "expected array constant term, but cast is not of array type"
904 << "cast type: " << type
;
905 parseError(ss
.str());
910 if (p
.d_expr
.isNull())
912 Trace("parser-overloading")
913 << "Getting variable expression with name " << p
.d_name
<< " and type "
914 << type
<< std::endl
;
915 // get the variable expression for the type
916 if (isDeclared(p
.d_name
, SYM_VARIABLE
))
918 p
.d_expr
= getExpressionForNameAndType(p
.d_name
, type
);
919 p
.d_name
= std::string("");
921 if (p
.d_expr
.isNull())
923 std::stringstream ss
;
924 ss
<< "Could not resolve expression with name " << p
.d_name
925 << " and type " << type
<< std::endl
;
926 parseError(ss
.str());
929 Trace("parser-qid") << "Resolve ascription " << type
<< " on " << p
.d_expr
;
930 Trace("parser-qid") << " " << p
.d_expr
.getKind() << " " << p
.d_expr
.getSort();
931 Trace("parser-qid") << std::endl
;
932 // otherwise, we process the type ascription
933 p
.d_expr
= applyTypeAscription(p
.d_expr
, type
);
936 api::Term
Smt2::parseOpToExpr(ParseOp
& p
)
938 Debug("parser") << "parseOpToExpr: " << p
<< std::endl
;
940 if (p
.d_kind
!= api::NULL_EXPR
|| !p
.d_type
.isNull())
943 "Bad syntax for qualified identifier operator in term position.");
945 else if (!p
.d_expr
.isNull())
949 else if (!isDeclared(p
.d_name
, SYM_VARIABLE
))
951 std::stringstream ss
;
952 ss
<< "Symbol " << p
.d_name
<< " is not declared.";
953 parseError(ss
.str());
957 expr
= getExpressionForName(p
.d_name
);
959 assert(!expr
.isNull());
963 api::Term
Smt2::applyParseOp(ParseOp
& p
, std::vector
<api::Term
>& args
)
965 bool isBuiltinOperator
= false;
966 // the builtin kind of the overall return expression
967 api::Kind kind
= api::NULL_EXPR
;
968 // First phase: process the operator
969 if (Debug
.isOn("parser"))
971 Debug("parser") << "applyParseOp: " << p
<< " to:" << std::endl
;
972 for (std::vector
<api::Term
>::iterator i
= args
.begin(); i
!= args
.end();
975 Debug("parser") << "++ " << *i
<< std::endl
;
979 if (p
.d_kind
!= api::NULL_EXPR
)
981 // It is a special case, e.g. tupSel or array constant specification.
982 // We have to wait until the arguments are parsed to resolve it.
984 else if (!p
.d_expr
.isNull())
986 // An explicit operator, e.g. an apply function
987 api::Kind fkind
= getKindForFunction(p
.d_expr
);
988 if (fkind
!= api::UNDEFINED_KIND
)
990 // Some operators may require a specific kind.
991 // Testers are handled differently than other indexed operators,
992 // since they require a kind.
994 Debug("parser") << "Got function kind " << kind
<< " for expression "
997 args
.insert(args
.begin(), p
.d_expr
);
999 else if (!p
.d_op
.isNull())
1001 // it was given an operator
1006 isBuiltinOperator
= isOperatorEnabled(p
.d_name
);
1007 if (isBuiltinOperator
)
1009 // a builtin operator, convert to kind
1010 kind
= getOperatorKind(p
.d_name
);
1014 // A non-built-in function application, get the expression
1015 checkDeclaration(p
.d_name
, CHECK_DECLARED
, SYM_VARIABLE
);
1016 api::Term v
= getVariable(p
.d_name
);
1019 checkFunctionLike(v
);
1020 kind
= getKindForFunction(v
);
1021 args
.insert(args
.begin(), v
);
1025 // Overloaded symbol?
1026 // Could not find the expression. It may be an overloaded symbol,
1027 // in which case we may find it after knowing the types of its
1029 std::vector
<api::Sort
> argTypes
;
1030 for (std::vector
<api::Term
>::iterator i
= args
.begin(); i
!= args
.end();
1033 argTypes
.push_back((*i
).getSort());
1035 api::Term fop
= getOverloadedFunctionForTypes(p
.d_name
, argTypes
);
1038 checkFunctionLike(fop
);
1039 kind
= getKindForFunction(fop
);
1040 args
.insert(args
.begin(), fop
);
1045 "Cannot find unambiguous overloaded function for argument "
1051 // Second phase: apply the arguments to the parse op
1052 const Options
& opts
= d_solver
->getOptions();
1053 // handle special cases
1054 if (p
.d_kind
== api::CONST_ARRAY
&& !p
.d_type
.isNull())
1056 if (args
.size() != 1)
1058 parseError("Too many arguments to array constant.");
1060 api::Term constVal
= args
[0];
1062 // To parse array constants taking reals whose values are specified by
1063 // rationals, e.g. ((as const (Array Int Real)) (/ 1 3)), we must handle
1064 // the fact that (/ 1 3) is the division of constants 1 and 3, and not
1065 // the resulting constant rational value. Thus, we must construct the
1066 // resulting rational here. This also is applied for integral real values
1067 // like 5.0 which are converted to (/ 5 1) to distinguish them from
1068 // integer constants. We must ensure numerator and denominator are
1069 // constant and the denominator is non-zero.
1070 if (constVal
.getKind() == api::DIVISION
)
1072 std::stringstream sdiv
;
1073 sdiv
<< constVal
[0] << "/" << constVal
[1];
1074 constVal
= d_solver
->mkReal(sdiv
.str());
1077 if (!p
.d_type
.getArrayElementSort().isComparableTo(constVal
.getSort()))
1079 std::stringstream ss
;
1080 ss
<< "type mismatch inside array constant term:" << std::endl
1081 << "array type: " << p
.d_type
<< std::endl
1082 << "expected const type: " << p
.d_type
.getArrayElementSort()
1084 << "computed const type: " << constVal
.getSort();
1085 parseError(ss
.str());
1087 api::Term ret
= d_solver
->mkConstArray(p
.d_type
, constVal
);
1088 Debug("parser") << "applyParseOp: return store all " << ret
<< std::endl
;
1091 else if (p
.d_kind
== api::APPLY_SELECTOR
&& !p
.d_expr
.isNull())
1093 // tuple selector case
1094 Integer x
= p
.d_expr
.getExpr().getConst
<Rational
>().getNumerator();
1095 if (!x
.fitsUnsignedInt())
1097 parseError("index of tupSel is larger than size of unsigned int");
1099 unsigned int n
= x
.toUnsignedInt();
1100 if (args
.size() != 1)
1102 parseError("tupSel should only be applied to one tuple argument");
1104 api::Sort t
= args
[0].getSort();
1107 parseError("tupSel applied to non-tuple");
1109 size_t length
= t
.getTupleLength();
1112 std::stringstream ss
;
1113 ss
<< "tuple is of length " << length
<< "; cannot access index " << n
;
1114 parseError(ss
.str());
1116 const api::Datatype
& dt
= t
.getDatatype();
1117 api::Term ret
= d_solver
->mkTerm(
1118 api::APPLY_SELECTOR
, dt
[0][n
].getSelectorTerm(), args
[0]);
1119 Debug("parser") << "applyParseOp: return selector " << ret
<< std::endl
;
1122 else if (p
.d_kind
!= api::NULL_EXPR
)
1124 // it should not have an expression or type specified at this point
1125 if (!p
.d_expr
.isNull() || !p
.d_type
.isNull())
1127 std::stringstream ss
;
1128 ss
<< "Could not process parsed qualified identifier kind " << p
.d_kind
;
1129 parseError(ss
.str());
1131 // otherwise it is a simple application
1134 else if (isBuiltinOperator
)
1136 if (!opts
.getUfHo() && (kind
== api::EQUAL
|| kind
== api::DISTINCT
))
1138 // need --uf-ho if these operators are applied over function args
1139 for (std::vector
<api::Term
>::iterator i
= args
.begin(); i
!= args
.end();
1142 if ((*i
).getSort().isFunction())
1145 "Cannot apply equalty to functions unless --uf-ho is set.");
1149 if (!strictModeEnabled() && (kind
== api::AND
|| kind
== api::OR
)
1150 && args
.size() == 1)
1152 // Unary AND/OR can be replaced with the argument.
1153 Debug("parser") << "applyParseOp: return unary " << args
[0] << std::endl
;
1156 else if (kind
== api::MINUS
&& args
.size() == 1)
1158 api::Term ret
= d_solver
->mkTerm(api::UMINUS
, args
[0]);
1159 Debug("parser") << "applyParseOp: return uminus " << ret
<< std::endl
;
1162 if (kind
== api::EQ_RANGE
&& d_solver
->getOption("arrays-exp") != "true")
1165 "eqrange predicate requires option --arrays-exp to be enabled.");
1167 if (kind
== api::SINGLETON
&& args
.size() == 1)
1169 api::Term ret
= d_solver
->mkTerm(api::SINGLETON
, args
[0]);
1170 Debug("parser") << "applyParseOp: return singleton " << ret
<< std::endl
;
1173 api::Term ret
= d_solver
->mkTerm(kind
, args
);
1174 Debug("parser") << "applyParseOp: return default builtin " << ret
1179 if (args
.size() >= 2)
1181 // may be partially applied function, in this case we use HO_APPLY
1182 api::Sort argt
= args
[0].getSort();
1183 if (argt
.isFunction())
1185 unsigned arity
= argt
.getFunctionArity();
1186 if (args
.size() - 1 < arity
)
1188 if (!opts
.getUfHo())
1190 parseError("Cannot partially apply functions unless --uf-ho is set.");
1192 Debug("parser") << "Partial application of " << args
[0];
1193 Debug("parser") << " : #argTypes = " << arity
;
1194 Debug("parser") << ", #args = " << args
.size() - 1 << std::endl
;
1195 api::Term ret
= d_solver
->mkTerm(api::HO_APPLY
, args
);
1196 Debug("parser") << "applyParseOp: return curry higher order " << ret
1198 // must curry the partial application
1205 api::Term ret
= d_solver
->mkTerm(op
, args
);
1206 Debug("parser") << "applyParseOp: return op : " << ret
<< std::endl
;
1209 if (kind
== api::NULL_EXPR
)
1211 // should never happen in the new API
1212 parseError("do not know how to process parse op");
1214 Debug("parser") << "Try default term construction for kind " << kind
1215 << " #args = " << args
.size() << "..." << std::endl
;
1216 api::Term ret
= d_solver
->mkTerm(kind
, args
);
1217 Debug("parser") << "applyParseOp: return : " << ret
<< std::endl
;
1221 void Smt2::notifyNamedExpression(api::Term
& expr
, std::string name
)
1223 checkUserSymbol(name
);
1224 // remember the expression name in the symbol manager
1225 getSymbolManager()->setExpressionName(expr
, name
, false);
1226 // define the variable
1227 defineVar(name
, expr
);
1228 // set the last named term, which ensures that we catch when assertions are
1230 setLastNamedTerm(expr
, name
);
1233 api::Term
Smt2::mkAnd(const std::vector
<api::Term
>& es
)
1237 return d_solver
->mkTrue();
1239 else if (es
.size() == 1)
1245 return d_solver
->mkTerm(api::AND
, es
);
1249 } // namespace parser
1250 }/* CVC4 namespace */