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
, Input
* input
, bool strictMode
, bool parseOnly
)
36 : Parser(solver
, input
, strictMode
, parseOnly
),
40 if (!strictModeEnabled())
46 void Smt2::addArithmeticOperators() {
47 addOperator(api::PLUS
, "+");
48 addOperator(api::MINUS
, "-");
49 // api::MINUS is converted to api::UMINUS if there is only a single operand
50 Parser::addOperator(api::UMINUS
);
51 addOperator(api::MULT
, "*");
52 addOperator(api::LT
, "<");
53 addOperator(api::LEQ
, "<=");
54 addOperator(api::GT
, ">");
55 addOperator(api::GEQ
, ">=");
57 if (!strictModeEnabled())
59 // NOTE: this operator is non-standard
60 addOperator(api::POW
, "^");
64 void Smt2::addTranscendentalOperators()
66 addOperator(api::EXPONENTIAL
, "exp");
67 addOperator(api::SINE
, "sin");
68 addOperator(api::COSINE
, "cos");
69 addOperator(api::TANGENT
, "tan");
70 addOperator(api::COSECANT
, "csc");
71 addOperator(api::SECANT
, "sec");
72 addOperator(api::COTANGENT
, "cot");
73 addOperator(api::ARCSINE
, "arcsin");
74 addOperator(api::ARCCOSINE
, "arccos");
75 addOperator(api::ARCTANGENT
, "arctan");
76 addOperator(api::ARCCOSECANT
, "arccsc");
77 addOperator(api::ARCSECANT
, "arcsec");
78 addOperator(api::ARCCOTANGENT
, "arccot");
79 addOperator(api::SQRT
, "sqrt");
82 void Smt2::addQuantifiersOperators()
84 if (!strictModeEnabled())
86 addOperator(api::INST_CLOSURE
, "inst-closure");
90 void Smt2::addBitvectorOperators() {
91 addOperator(api::BITVECTOR_CONCAT
, "concat");
92 addOperator(api::BITVECTOR_NOT
, "bvnot");
93 addOperator(api::BITVECTOR_AND
, "bvand");
94 addOperator(api::BITVECTOR_OR
, "bvor");
95 addOperator(api::BITVECTOR_NEG
, "bvneg");
96 addOperator(api::BITVECTOR_PLUS
, "bvadd");
97 addOperator(api::BITVECTOR_MULT
, "bvmul");
98 addOperator(api::BITVECTOR_UDIV
, "bvudiv");
99 addOperator(api::BITVECTOR_UREM
, "bvurem");
100 addOperator(api::BITVECTOR_SHL
, "bvshl");
101 addOperator(api::BITVECTOR_LSHR
, "bvlshr");
102 addOperator(api::BITVECTOR_ULT
, "bvult");
103 addOperator(api::BITVECTOR_NAND
, "bvnand");
104 addOperator(api::BITVECTOR_NOR
, "bvnor");
105 addOperator(api::BITVECTOR_XOR
, "bvxor");
106 addOperator(api::BITVECTOR_XNOR
, "bvxnor");
107 addOperator(api::BITVECTOR_COMP
, "bvcomp");
108 addOperator(api::BITVECTOR_SUB
, "bvsub");
109 addOperator(api::BITVECTOR_SDIV
, "bvsdiv");
110 addOperator(api::BITVECTOR_SREM
, "bvsrem");
111 addOperator(api::BITVECTOR_SMOD
, "bvsmod");
112 addOperator(api::BITVECTOR_ASHR
, "bvashr");
113 addOperator(api::BITVECTOR_ULE
, "bvule");
114 addOperator(api::BITVECTOR_UGT
, "bvugt");
115 addOperator(api::BITVECTOR_UGE
, "bvuge");
116 addOperator(api::BITVECTOR_SLT
, "bvslt");
117 addOperator(api::BITVECTOR_SLE
, "bvsle");
118 addOperator(api::BITVECTOR_SGT
, "bvsgt");
119 addOperator(api::BITVECTOR_SGE
, "bvsge");
120 addOperator(api::BITVECTOR_REDOR
, "bvredor");
121 addOperator(api::BITVECTOR_REDAND
, "bvredand");
123 addIndexedOperator(api::BITVECTOR_EXTRACT
, api::BITVECTOR_EXTRACT
, "extract");
124 addIndexedOperator(api::BITVECTOR_REPEAT
, api::BITVECTOR_REPEAT
, "repeat");
126 api::BITVECTOR_ZERO_EXTEND
, api::BITVECTOR_ZERO_EXTEND
, "zero_extend");
128 api::BITVECTOR_SIGN_EXTEND
, api::BITVECTOR_SIGN_EXTEND
, "sign_extend");
130 api::BITVECTOR_ROTATE_LEFT
, api::BITVECTOR_ROTATE_LEFT
, "rotate_left");
132 api::BITVECTOR_ROTATE_RIGHT
, api::BITVECTOR_ROTATE_RIGHT
, "rotate_right");
135 void Smt2::addDatatypesOperators()
137 Parser::addOperator(api::APPLY_CONSTRUCTOR
);
138 Parser::addOperator(api::APPLY_TESTER
);
139 Parser::addOperator(api::APPLY_SELECTOR
);
141 if (!strictModeEnabled())
143 addOperator(api::DT_SIZE
, "dt.size");
147 void Smt2::addStringOperators() {
150 getSolver()->mkTerm(api::REGEXP_STAR
, getSolver()->mkRegexpSigma()));
151 addOperator(api::STRING_CONCAT
, "str.++");
152 addOperator(api::STRING_LENGTH
, "str.len");
153 addOperator(api::STRING_SUBSTR
, "str.substr");
154 addOperator(api::STRING_CONTAINS
, "str.contains");
155 addOperator(api::STRING_CHARAT
, "str.at");
156 addOperator(api::STRING_INDEXOF
, "str.indexof");
157 addOperator(api::STRING_REPLACE
, "str.replace");
158 addOperator(api::STRING_PREFIX
, "str.prefixof");
159 addOperator(api::STRING_SUFFIX
, "str.suffixof");
160 addOperator(api::STRING_FROM_CODE
, "str.from_code");
161 addOperator(api::STRING_IS_DIGIT
, "str.is_digit");
162 addOperator(api::STRING_REPLACE_RE
, "str.replace_re");
163 addOperator(api::STRING_REPLACE_RE_ALL
, "str.replace_re_all");
164 if (!strictModeEnabled())
166 addOperator(api::STRING_UPDATE
, "str.update");
167 addOperator(api::STRING_TOLOWER
, "str.tolower");
168 addOperator(api::STRING_TOUPPER
, "str.toupper");
169 addOperator(api::STRING_REV
, "str.rev");
171 addOperator(api::SEQ_CONCAT
, "seq.++");
172 addOperator(api::SEQ_LENGTH
, "seq.len");
173 addOperator(api::SEQ_EXTRACT
, "seq.extract");
174 addOperator(api::SEQ_UPDATE
, "seq.update");
175 addOperator(api::SEQ_AT
, "seq.at");
176 addOperator(api::SEQ_CONTAINS
, "seq.contains");
177 addOperator(api::SEQ_INDEXOF
, "seq.indexof");
178 addOperator(api::SEQ_REPLACE
, "seq.replace");
179 addOperator(api::SEQ_PREFIX
, "seq.prefixof");
180 addOperator(api::SEQ_SUFFIX
, "seq.suffixof");
181 addOperator(api::SEQ_REV
, "seq.rev");
182 addOperator(api::SEQ_REPLACE_ALL
, "seq.replace_all");
183 addOperator(api::SEQ_UNIT
, "seq.unit");
184 addOperator(api::SEQ_NTH
, "seq.nth");
186 // at the moment, we only use this syntax for smt2.6
187 if (getLanguage() == language::input::LANG_SMTLIB_V2_6
188 || getLanguage() == language::input::LANG_SYGUS_V2
)
190 addOperator(api::STRING_FROM_INT
, "str.from_int");
191 addOperator(api::STRING_TO_INT
, "str.to_int");
192 addOperator(api::STRING_IN_REGEXP
, "str.in_re");
193 addOperator(api::STRING_TO_REGEXP
, "str.to_re");
194 addOperator(api::STRING_TO_CODE
, "str.to_code");
195 addOperator(api::STRING_REPLACE_ALL
, "str.replace_all");
199 addOperator(api::STRING_FROM_INT
, "int.to.str");
200 addOperator(api::STRING_TO_INT
, "str.to.int");
201 addOperator(api::STRING_IN_REGEXP
, "str.in.re");
202 addOperator(api::STRING_TO_REGEXP
, "str.to.re");
203 addOperator(api::STRING_TO_CODE
, "str.code");
204 addOperator(api::STRING_REPLACE_ALL
, "str.replaceall");
207 addOperator(api::REGEXP_CONCAT
, "re.++");
208 addOperator(api::REGEXP_UNION
, "re.union");
209 addOperator(api::REGEXP_INTER
, "re.inter");
210 addOperator(api::REGEXP_STAR
, "re.*");
211 addOperator(api::REGEXP_PLUS
, "re.+");
212 addOperator(api::REGEXP_OPT
, "re.opt");
213 addIndexedOperator(api::REGEXP_REPEAT
, api::REGEXP_REPEAT
, "re.^");
214 addIndexedOperator(api::REGEXP_LOOP
, api::REGEXP_LOOP
, "re.loop");
215 addOperator(api::REGEXP_RANGE
, "re.range");
216 addOperator(api::REGEXP_COMPLEMENT
, "re.comp");
217 addOperator(api::REGEXP_DIFF
, "re.diff");
218 addOperator(api::STRING_LT
, "str.<");
219 addOperator(api::STRING_LEQ
, "str.<=");
222 void Smt2::addFloatingPointOperators() {
223 addOperator(api::FLOATINGPOINT_FP
, "fp");
224 addOperator(api::FLOATINGPOINT_EQ
, "fp.eq");
225 addOperator(api::FLOATINGPOINT_ABS
, "fp.abs");
226 addOperator(api::FLOATINGPOINT_NEG
, "fp.neg");
227 addOperator(api::FLOATINGPOINT_PLUS
, "fp.add");
228 addOperator(api::FLOATINGPOINT_SUB
, "fp.sub");
229 addOperator(api::FLOATINGPOINT_MULT
, "fp.mul");
230 addOperator(api::FLOATINGPOINT_DIV
, "fp.div");
231 addOperator(api::FLOATINGPOINT_FMA
, "fp.fma");
232 addOperator(api::FLOATINGPOINT_SQRT
, "fp.sqrt");
233 addOperator(api::FLOATINGPOINT_REM
, "fp.rem");
234 addOperator(api::FLOATINGPOINT_RTI
, "fp.roundToIntegral");
235 addOperator(api::FLOATINGPOINT_MIN
, "fp.min");
236 addOperator(api::FLOATINGPOINT_MAX
, "fp.max");
237 addOperator(api::FLOATINGPOINT_LEQ
, "fp.leq");
238 addOperator(api::FLOATINGPOINT_LT
, "fp.lt");
239 addOperator(api::FLOATINGPOINT_GEQ
, "fp.geq");
240 addOperator(api::FLOATINGPOINT_GT
, "fp.gt");
241 addOperator(api::FLOATINGPOINT_ISN
, "fp.isNormal");
242 addOperator(api::FLOATINGPOINT_ISSN
, "fp.isSubnormal");
243 addOperator(api::FLOATINGPOINT_ISZ
, "fp.isZero");
244 addOperator(api::FLOATINGPOINT_ISINF
, "fp.isInfinite");
245 addOperator(api::FLOATINGPOINT_ISNAN
, "fp.isNaN");
246 addOperator(api::FLOATINGPOINT_ISNEG
, "fp.isNegative");
247 addOperator(api::FLOATINGPOINT_ISPOS
, "fp.isPositive");
248 addOperator(api::FLOATINGPOINT_TO_REAL
, "fp.to_real");
250 addIndexedOperator(api::FLOATINGPOINT_TO_FP_GENERIC
,
251 api::FLOATINGPOINT_TO_FP_GENERIC
,
253 addIndexedOperator(api::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR
,
254 api::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR
,
257 api::FLOATINGPOINT_TO_UBV
, api::FLOATINGPOINT_TO_UBV
, "fp.to_ubv");
259 api::FLOATINGPOINT_TO_SBV
, api::FLOATINGPOINT_TO_SBV
, "fp.to_sbv");
261 if (!strictModeEnabled())
263 addIndexedOperator(api::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR
,
264 api::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR
,
266 addIndexedOperator(api::FLOATINGPOINT_TO_FP_FLOATINGPOINT
,
267 api::FLOATINGPOINT_TO_FP_FLOATINGPOINT
,
269 addIndexedOperator(api::FLOATINGPOINT_TO_FP_REAL
,
270 api::FLOATINGPOINT_TO_FP_REAL
,
272 addIndexedOperator(api::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR
,
273 api::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR
,
278 void Smt2::addSepOperators() {
279 addOperator(api::SEP_STAR
, "sep");
280 addOperator(api::SEP_PTO
, "pto");
281 addOperator(api::SEP_WAND
, "wand");
282 addOperator(api::SEP_EMP
, "emp");
283 Parser::addOperator(api::SEP_STAR
);
284 Parser::addOperator(api::SEP_PTO
);
285 Parser::addOperator(api::SEP_WAND
);
286 Parser::addOperator(api::SEP_EMP
);
289 void Smt2::addCoreSymbols()
291 defineType("Bool", d_solver
->getBooleanSort());
292 defineVar("true", d_solver
->mkTrue());
293 defineVar("false", d_solver
->mkFalse());
294 addOperator(api::AND
, "and");
295 addOperator(api::DISTINCT
, "distinct");
296 addOperator(api::EQUAL
, "=");
297 addOperator(api::IMPLIES
, "=>");
298 addOperator(api::ITE
, "ite");
299 addOperator(api::NOT
, "not");
300 addOperator(api::OR
, "or");
301 addOperator(api::XOR
, "xor");
304 void Smt2::addOperator(api::Kind kind
, const std::string
& name
)
306 Debug("parser") << "Smt2::addOperator( " << kind
<< ", " << name
<< " )"
308 Parser::addOperator(kind
);
309 operatorKindMap
[name
] = kind
;
312 void Smt2::addIndexedOperator(api::Kind tKind
,
314 const std::string
& name
)
316 Parser::addOperator(tKind
);
317 d_indexedOpKindMap
[name
] = opKind
;
320 api::Kind
Smt2::getOperatorKind(const std::string
& name
) const
322 // precondition: isOperatorEnabled(name)
323 return operatorKindMap
.find(name
)->second
;
326 bool Smt2::isOperatorEnabled(const std::string
& name
) const {
327 return operatorKindMap
.find(name
) != operatorKindMap
.end();
330 bool Smt2::isTheoryEnabled(theory::TheoryId theory
) const
332 return d_logic
.isTheoryEnabled(theory
);
335 bool Smt2::isHoEnabled() const
337 return getLogic().isHigherOrder() && d_solver
->getOptions().getUfHo();
340 bool Smt2::logicIsSet() {
344 api::Term
Smt2::getExpressionForNameAndType(const std::string
& name
,
347 if (isAbstractValue(name
))
349 return mkAbstractValue(name
);
351 return Parser::getExpressionForNameAndType(name
, t
);
354 bool Smt2::getTesterName(api::Term cons
, std::string
& name
)
356 if ((v2_6() || sygus_v2()) && strictModeEnabled())
358 // 2.6 or above uses indexed tester symbols, if we are in strict mode,
359 // we do not automatically define is-cons for constructor cons.
362 std::stringstream ss
;
368 api::Term
Smt2::mkIndexedConstant(const std::string
& name
,
369 const std::vector
<uint64_t>& numerals
)
371 if (d_logic
.isTheoryEnabled(theory::THEORY_FP
))
375 return d_solver
->mkPosInf(numerals
[0], numerals
[1]);
377 else if (name
== "-oo")
379 return d_solver
->mkNegInf(numerals
[0], numerals
[1]);
381 else if (name
== "NaN")
383 return d_solver
->mkNaN(numerals
[0], numerals
[1]);
385 else if (name
== "+zero")
387 return d_solver
->mkPosZero(numerals
[0], numerals
[1]);
389 else if (name
== "-zero")
391 return d_solver
->mkNegZero(numerals
[0], numerals
[1]);
395 if (d_logic
.isTheoryEnabled(theory::THEORY_BV
) && name
.find("bv") == 0)
397 std::string bvStr
= name
.substr(2);
398 return d_solver
->mkBitVector(numerals
[0], bvStr
, 10);
401 // NOTE: Theory parametric constants go here
403 parseError(std::string("Unknown indexed literal `") + name
+ "'");
407 api::Op
Smt2::mkIndexedOp(const std::string
& name
,
408 const std::vector
<uint64_t>& numerals
)
410 const auto& kIt
= d_indexedOpKindMap
.find(name
);
411 if (kIt
!= d_indexedOpKindMap
.end())
413 api::Kind k
= (*kIt
).second
;
414 if (numerals
.size() == 1)
416 return d_solver
->mkOp(k
, numerals
[0]);
418 else if (numerals
.size() == 2)
420 return d_solver
->mkOp(k
, numerals
[0], numerals
[1]);
424 parseError(std::string("Unknown indexed function `") + name
+ "'");
428 api::Term
Smt2::bindDefineFunRec(
429 const std::string
& fname
,
430 const std::vector
<std::pair
<std::string
, api::Sort
>>& sortedVarNames
,
432 std::vector
<api::Term
>& flattenVars
)
434 std::vector
<api::Sort
> sorts
;
435 for (const std::pair
<std::string
, api::Sort
>& svn
: sortedVarNames
)
437 sorts
.push_back(svn
.second
);
440 // make the flattened function type, add bound variables
441 // to flattenVars if the defined function was given a function return type.
442 api::Sort ft
= mkFlatFunctionType(sorts
, t
, flattenVars
);
445 return bindVar(fname
, ft
, ExprManager::VAR_FLAG_NONE
, true);
448 void Smt2::pushDefineFunRecScope(
449 const std::vector
<std::pair
<std::string
, api::Sort
>>& sortedVarNames
,
451 const std::vector
<api::Term
>& flattenVars
,
452 std::vector
<api::Term
>& bvs
,
455 pushScope(bindingLevel
);
457 // bound variables are those that are explicitly named in the preamble
458 // of the define-fun(s)-rec command, we define them here
459 for (const std::pair
<std::string
, api::Sort
>& svn
: sortedVarNames
)
461 api::Term v
= bindBoundVar(svn
.first
, svn
.second
);
465 bvs
.insert(bvs
.end(), flattenVars
.begin(), flattenVars
.end());
470 d_seenSetLogic
= false;
471 d_logic
= LogicInfo();
472 operatorKindMap
.clear();
473 d_lastNamedTerm
= std::pair
<api::Term
, std::string
>();
474 this->Parser::reset();
476 if( !strictModeEnabled() ) {
481 void Smt2::resetAssertions() {
482 // Remove all declarations except the ones at level 0.
483 while (this->scopeLevel() > 0) {
488 Smt2::SynthFunFactory::SynthFunFactory(
490 const std::string
& id
,
493 std::vector
<std::pair
<std::string
, api::Sort
>>& sortedVarNames
)
494 : d_smt2(smt2
), d_id(id
), d_sort(range
), d_isInv(isInv
)
498 smt2
->parseError("Must supply return type for synth-fun.");
500 if (range
.isFunction())
502 smt2
->parseError("Cannot use synth-fun with function return type.");
505 std::vector
<api::Sort
> varSorts
;
506 for (const std::pair
<std::string
, api::Sort
>& p
: sortedVarNames
)
508 varSorts
.push_back(p
.second
);
511 api::Sort funSort
= varSorts
.empty()
513 : d_smt2
->d_solver
->mkFunctionSort(varSorts
, range
);
515 // we do not allow overloading for synth fun
516 d_fun
= d_smt2
->bindBoundVar(id
, funSort
);
518 Debug("parser-sygus") << "Define synth fun : " << id
<< std::endl
;
520 d_smt2
->pushScope(true);
521 d_sygusVars
= d_smt2
->bindBoundVars(sortedVarNames
);
524 std::unique_ptr
<Command
> Smt2::SynthFunFactory::mkCommand(api::Grammar
* grammar
)
526 Debug("parser-sygus") << "...read synth fun " << d_id
<< std::endl
;
528 return std::unique_ptr
<Command
>(new SynthFunCommand(
529 d_smt2
->d_solver
, d_id
, d_fun
, d_sygusVars
, d_sort
, d_isInv
, grammar
));
532 std::unique_ptr
<Command
> Smt2::invConstraint(
533 const std::vector
<std::string
>& names
)
535 checkThatLogicIsSet();
536 Debug("parser-sygus") << "Sygus : define sygus funs..." << std::endl
;
537 Debug("parser-sygus") << "Sygus : read inv-constraint..." << std::endl
;
539 if (names
.size() != 4)
542 "Bad syntax for inv-constraint: expected 4 "
546 std::vector
<api::Term
> terms
;
547 for (const std::string
& name
: names
)
549 if (!isDeclared(name
))
551 std::stringstream ss
;
552 ss
<< "Function " << name
<< " in inv-constraint is not defined.";
553 parseError(ss
.str());
556 terms
.push_back(getVariable(name
));
559 return std::unique_ptr
<Command
>(
560 new SygusInvConstraintCommand(api::termVectorToExprs(terms
)));
563 Command
* Smt2::setLogic(std::string name
, bool fromCommand
)
569 parseError("Only one set-logic is allowed.");
571 d_seenSetLogic
= true;
575 // If the logic is forced, we ignore all set-logic requests from commands.
576 return new EmptyCommand();
583 // if sygus is enabled, we must enable UF, datatypes, integer arithmetic and
586 if (!d_logic
.isQuantified())
588 warning("Logics in sygus are assumed to contain quantifiers.");
589 warning("Omit QF_ from the logic to avoid this warning.");
593 // Core theory belongs to every logic
596 if(d_logic
.isTheoryEnabled(theory::THEORY_UF
)) {
597 Parser::addOperator(api::APPLY_UF
);
599 if (!strictModeEnabled() && d_logic
.hasCardinalityConstraints())
601 addOperator(api::CARDINALITY_CONSTRAINT
, "fmf.card");
602 addOperator(api::CARDINALITY_VALUE
, "fmf.card.val");
606 if(d_logic
.isTheoryEnabled(theory::THEORY_ARITH
)) {
607 if(d_logic
.areIntegersUsed()) {
608 defineType("Int", d_solver
->getIntegerSort());
609 addArithmeticOperators();
610 addOperator(api::INTS_DIVISION
, "div");
611 addOperator(api::INTS_MODULUS
, "mod");
612 addOperator(api::ABS
, "abs");
613 addIndexedOperator(api::DIVISIBLE
, api::DIVISIBLE
, "divisible");
616 if (d_logic
.areRealsUsed())
618 defineType("Real", d_solver
->getRealSort());
619 addArithmeticOperators();
620 addOperator(api::DIVISION
, "/");
621 if (!strictModeEnabled())
623 addOperator(api::ABS
, "abs");
627 if (d_logic
.areIntegersUsed() && d_logic
.areRealsUsed())
629 addOperator(api::TO_INTEGER
, "to_int");
630 addOperator(api::IS_INTEGER
, "is_int");
631 addOperator(api::TO_REAL
, "to_real");
634 if (d_logic
.areTranscendentalsUsed())
636 defineVar("real.pi", d_solver
->mkTerm(api::PI
));
637 addTranscendentalOperators();
639 if (!strictModeEnabled())
641 // integer version of AND
642 addIndexedOperator(api::IAND
, api::IAND
, "iand");
646 if(d_logic
.isTheoryEnabled(theory::THEORY_ARRAYS
)) {
647 addOperator(api::SELECT
, "select");
648 addOperator(api::STORE
, "store");
649 addOperator(api::EQ_RANGE
, "eqrange");
652 if(d_logic
.isTheoryEnabled(theory::THEORY_BV
)) {
653 addBitvectorOperators();
655 if (!strictModeEnabled() && d_logic
.isTheoryEnabled(theory::THEORY_ARITH
)
656 && d_logic
.areIntegersUsed())
658 // Conversions between bit-vectors and integers
659 addOperator(api::BITVECTOR_TO_NAT
, "bv2nat");
661 api::INT_TO_BITVECTOR
, api::INT_TO_BITVECTOR
, "int2bv");
665 if(d_logic
.isTheoryEnabled(theory::THEORY_DATATYPES
)) {
666 const std::vector
<api::Sort
> types
;
667 defineType("Tuple", d_solver
->mkTupleSort(types
));
668 addDatatypesOperators();
671 if(d_logic
.isTheoryEnabled(theory::THEORY_SETS
)) {
672 defineVar("emptyset", d_solver
->mkEmptySet(d_solver
->getNullSort()));
673 // the Boolean sort is a placeholder here since we don't have type info
674 // without type annotation
675 defineVar("univset", d_solver
->mkUniverseSet(d_solver
->getBooleanSort()));
677 addOperator(api::UNION
, "union");
678 addOperator(api::INTERSECTION
, "intersection");
679 addOperator(api::SETMINUS
, "setminus");
680 addOperator(api::SUBSET
, "subset");
681 addOperator(api::MEMBER
, "member");
682 addOperator(api::SINGLETON
, "singleton");
683 addOperator(api::INSERT
, "insert");
684 addOperator(api::CARD
, "card");
685 addOperator(api::COMPLEMENT
, "complement");
686 addOperator(api::CHOOSE
, "choose");
687 addOperator(api::JOIN
, "join");
688 addOperator(api::PRODUCT
, "product");
689 addOperator(api::TRANSPOSE
, "transpose");
690 addOperator(api::TCLOSURE
, "tclosure");
693 if(d_logic
.isTheoryEnabled(theory::THEORY_STRINGS
)) {
694 defineType("String", d_solver
->getStringSort());
695 defineType("RegLan", d_solver
->getRegExpSort());
696 defineType("Int", d_solver
->getIntegerSort());
698 if (getLanguage() == language::input::LANG_SMTLIB_V2_6
699 || getLanguage() == language::input::LANG_SYGUS_V2
)
701 defineVar("re.none", d_solver
->mkRegexpEmpty());
705 defineVar("re.nostr", d_solver
->mkRegexpEmpty());
707 defineVar("re.allchar", d_solver
->mkRegexpSigma());
709 // Boolean is a placeholder
710 defineVar("seq.empty",
711 d_solver
->mkEmptySequence(d_solver
->getBooleanSort()));
713 addStringOperators();
716 if(d_logic
.isQuantified()) {
717 addQuantifiersOperators();
720 if (d_logic
.isTheoryEnabled(theory::THEORY_FP
)) {
721 defineType("RoundingMode", d_solver
->getRoundingmodeSort());
722 defineType("Float16", d_solver
->mkFloatingPointSort(5, 11));
723 defineType("Float32", d_solver
->mkFloatingPointSort(8, 24));
724 defineType("Float64", d_solver
->mkFloatingPointSort(11, 53));
725 defineType("Float128", d_solver
->mkFloatingPointSort(15, 113));
727 defineVar("RNE", d_solver
->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_EVEN
));
728 defineVar("roundNearestTiesToEven",
729 d_solver
->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_EVEN
));
730 defineVar("RNA", d_solver
->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_AWAY
));
731 defineVar("roundNearestTiesToAway",
732 d_solver
->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_AWAY
));
733 defineVar("RTP", d_solver
->mkRoundingMode(api::ROUND_TOWARD_POSITIVE
));
734 defineVar("roundTowardPositive",
735 d_solver
->mkRoundingMode(api::ROUND_TOWARD_POSITIVE
));
736 defineVar("RTN", d_solver
->mkRoundingMode(api::ROUND_TOWARD_NEGATIVE
));
737 defineVar("roundTowardNegative",
738 d_solver
->mkRoundingMode(api::ROUND_TOWARD_NEGATIVE
));
739 defineVar("RTZ", d_solver
->mkRoundingMode(api::ROUND_TOWARD_ZERO
));
740 defineVar("roundTowardZero",
741 d_solver
->mkRoundingMode(api::ROUND_TOWARD_ZERO
));
743 addFloatingPointOperators();
746 if (d_logic
.isTheoryEnabled(theory::THEORY_SEP
)) {
747 // the Boolean sort is a placeholder here since we don't have type info
748 // without type annotation
749 defineVar("sep.nil", d_solver
->mkSepNil(d_solver
->getBooleanSort()));
755 new SetBenchmarkLogicCommand(sygus() ? d_logic
.getLogicString() : name
);
756 cmd
->setMuted(!fromCommand
);
758 } /* Smt2::setLogic() */
760 api::Grammar
* Smt2::mkGrammar(const std::vector
<api::Term
>& boundVars
,
761 const std::vector
<api::Term
>& ntSymbols
)
763 d_allocGrammars
.emplace_back(new api::Grammar(
764 std::move(d_solver
->mkSygusGrammar(boundVars
, ntSymbols
))));
765 return d_allocGrammars
.back().get();
768 bool Smt2::sygus() const
770 InputLanguage ilang
= getLanguage();
771 return ilang
== language::input::LANG_SYGUS_V2
;
774 bool Smt2::sygus_v2() const
776 return getLanguage() == language::input::LANG_SYGUS_V2
;
779 void Smt2::setInfo(const std::string
& flag
, const SExpr
& sexpr
) {
783 void Smt2::setOption(const std::string
& flag
, const SExpr
& sexpr
) {
787 void Smt2::checkThatLogicIsSet()
791 if (strictModeEnabled())
793 parseError("set-logic must appear before this point.");
797 Command
* cmd
= nullptr;
800 cmd
= setLogic(getForcedLogic(), false);
804 warning("No set-logic command was given before this point.");
805 warning("CVC4 will make all theories available.");
807 "Consider setting a stricter logic for (likely) better "
809 warning("To suppress this warning in the future use (set-logic ALL).");
811 cmd
= setLogic("ALL", false);
818 void Smt2::checkLogicAllowsFreeSorts()
820 if (!d_logic
.isTheoryEnabled(theory::THEORY_UF
)
821 && !d_logic
.isTheoryEnabled(theory::THEORY_ARRAYS
)
822 && !d_logic
.isTheoryEnabled(theory::THEORY_DATATYPES
)
823 && !d_logic
.isTheoryEnabled(theory::THEORY_SETS
))
825 parseErrorLogic("Free sort symbols not allowed in ");
829 void Smt2::checkLogicAllowsFunctions()
831 if (!d_logic
.isTheoryEnabled(theory::THEORY_UF
))
834 "Functions (of non-zero arity) cannot "
835 "be declared in logic "
836 + d_logic
.getLogicString() + " unless option --uf-ho is used");
840 /* The include are managed in the lexer but called in the parser */
841 // Inspired by http://www.antlr3.org/api/C/interop.html
843 static bool newInputStream(const std::string
& filename
, pANTLR3_LEXER lexer
) {
844 Debug("parser") << "Including " << filename
<< std::endl
;
845 // Create a new input stream and take advantage of built in stream stacking
846 // in C target runtime.
848 pANTLR3_INPUT_STREAM in
;
849 #ifdef CVC4_ANTLR3_OLD_INPUT_STREAM
850 in
= antlr3AsciiFileStreamNew((pANTLR3_UINT8
) filename
.c_str());
851 #else /* CVC4_ANTLR3_OLD_INPUT_STREAM */
852 in
= antlr3FileStreamNew((pANTLR3_UINT8
) filename
.c_str(), ANTLR3_ENC_8BIT
);
853 #endif /* CVC4_ANTLR3_OLD_INPUT_STREAM */
855 Debug("parser") << "Can't open " << filename
<< std::endl
;
858 // Same thing as the predefined PUSHSTREAM(in);
859 lexer
->pushCharStream(lexer
, in
);
861 //lexer->rec->state->tokenStartCharIndex = -10;
862 //lexer->emit(lexer);
864 // Note that the input stream is not closed when it EOFs, I don't bother
865 // to do it here, but it is up to you to track streams created like this
866 // and destroy them when the whole parse session is complete. Remember that you
867 // don't want to do this until all tokens have been manipulated all the way through
868 // your tree parsers etc as the token does not store the text it just refers
869 // back to the input stream and trying to get the text for it will abort if you
870 // close the input stream too early.
872 //TODO what said before
876 void Smt2::includeFile(const std::string
& filename
) {
877 // security for online version
878 if(!canIncludeFile()) {
879 parseError("include-file feature was disabled for this run.");
883 AntlrInput
* ai
= static_cast<AntlrInput
*>(getInput());
884 pANTLR3_LEXER lexer
= ai
->getAntlr3Lexer();
885 // get the name of the current stream "Does it work inside an include?"
886 const std::string inputName
= ai
->getInputStreamName();
888 // Find the directory of the current input file
890 size_t pos
= inputName
.rfind('/');
891 if(pos
!= std::string::npos
) {
892 path
= std::string(inputName
, 0, pos
+ 1);
894 path
.append(filename
);
895 if(!newInputStream(path
, lexer
)) {
896 parseError("Couldn't open include file `" + path
+ "'");
899 bool Smt2::isAbstractValue(const std::string
& name
)
901 return name
.length() >= 2 && name
[0] == '@' && name
[1] != '0'
902 && name
.find_first_not_of("0123456789", 1) == std::string::npos
;
905 api::Term
Smt2::mkAbstractValue(const std::string
& name
)
907 assert(isAbstractValue(name
));
909 return d_solver
->mkAbstractValue(name
.substr(1));
912 InputLanguage
Smt2::getLanguage() const
914 return d_solver
->getOptions().getInputLanguage();
917 void Smt2::parseOpApplyTypeAscription(ParseOp
& p
, api::Sort type
)
919 Debug("parser") << "parseOpApplyTypeAscription : " << p
<< " " << type
921 // (as const (Array T1 T2))
922 if (p
.d_kind
== api::CONST_ARRAY
)
926 std::stringstream ss
;
927 ss
<< "expected array constant term, but cast is not of array type"
929 << "cast type: " << type
;
930 parseError(ss
.str());
935 if (p
.d_expr
.isNull())
937 Trace("parser-overloading")
938 << "Getting variable expression with name " << p
.d_name
<< " and type "
939 << type
<< std::endl
;
940 // get the variable expression for the type
941 if (isDeclared(p
.d_name
, SYM_VARIABLE
))
943 p
.d_expr
= getExpressionForNameAndType(p
.d_name
, type
);
944 p
.d_name
= std::string("");
946 if (p
.d_expr
.isNull())
948 std::stringstream ss
;
949 ss
<< "Could not resolve expression with name " << p
.d_name
950 << " and type " << type
<< std::endl
;
951 parseError(ss
.str());
954 Trace("parser-qid") << "Resolve ascription " << type
<< " on " << p
.d_expr
;
955 Trace("parser-qid") << " " << p
.d_expr
.getKind() << " " << p
.d_expr
.getSort();
956 Trace("parser-qid") << std::endl
;
957 // otherwise, we process the type ascription
958 p
.d_expr
= applyTypeAscription(p
.d_expr
, type
);
961 api::Term
Smt2::parseOpToExpr(ParseOp
& p
)
963 Debug("parser") << "parseOpToExpr: " << p
<< std::endl
;
965 if (p
.d_kind
!= api::NULL_EXPR
|| !p
.d_type
.isNull())
968 "Bad syntax for qualified identifier operator in term position.");
970 else if (!p
.d_expr
.isNull())
974 else if (!isDeclared(p
.d_name
, SYM_VARIABLE
))
976 std::stringstream ss
;
977 ss
<< "Symbol " << p
.d_name
<< " is not declared.";
978 parseError(ss
.str());
982 expr
= getExpressionForName(p
.d_name
);
984 assert(!expr
.isNull());
988 api::Term
Smt2::applyParseOp(ParseOp
& p
, std::vector
<api::Term
>& args
)
990 bool isBuiltinOperator
= false;
991 // the builtin kind of the overall return expression
992 api::Kind kind
= api::NULL_EXPR
;
993 // First phase: process the operator
994 if (Debug
.isOn("parser"))
996 Debug("parser") << "applyParseOp: " << p
<< " to:" << std::endl
;
997 for (std::vector
<api::Term
>::iterator i
= args
.begin(); i
!= args
.end();
1000 Debug("parser") << "++ " << *i
<< std::endl
;
1004 if (p
.d_kind
!= api::NULL_EXPR
)
1006 // It is a special case, e.g. tupSel or array constant specification.
1007 // We have to wait until the arguments are parsed to resolve it.
1009 else if (!p
.d_expr
.isNull())
1011 // An explicit operator, e.g. an apply function
1012 api::Kind fkind
= getKindForFunction(p
.d_expr
);
1013 if (fkind
!= api::UNDEFINED_KIND
)
1015 // Some operators may require a specific kind.
1016 // Testers are handled differently than other indexed operators,
1017 // since they require a kind.
1019 Debug("parser") << "Got function kind " << kind
<< " for expression "
1022 args
.insert(args
.begin(), p
.d_expr
);
1024 else if (!p
.d_op
.isNull())
1026 // it was given an operator
1031 isBuiltinOperator
= isOperatorEnabled(p
.d_name
);
1032 if (isBuiltinOperator
)
1034 // a builtin operator, convert to kind
1035 kind
= getOperatorKind(p
.d_name
);
1039 // A non-built-in function application, get the expression
1040 checkDeclaration(p
.d_name
, CHECK_DECLARED
, SYM_VARIABLE
);
1041 api::Term v
= getVariable(p
.d_name
);
1044 checkFunctionLike(v
);
1045 kind
= getKindForFunction(v
);
1046 args
.insert(args
.begin(), v
);
1050 // Overloaded symbol?
1051 // Could not find the expression. It may be an overloaded symbol,
1052 // in which case we may find it after knowing the types of its
1054 std::vector
<api::Sort
> argTypes
;
1055 for (std::vector
<api::Term
>::iterator i
= args
.begin(); i
!= args
.end();
1058 argTypes
.push_back((*i
).getSort());
1060 api::Term fop
= getOverloadedFunctionForTypes(p
.d_name
, argTypes
);
1063 checkFunctionLike(fop
);
1064 kind
= getKindForFunction(fop
);
1065 args
.insert(args
.begin(), fop
);
1070 "Cannot find unambiguous overloaded function for argument "
1076 // Second phase: apply the arguments to the parse op
1077 const Options
& opts
= d_solver
->getOptions();
1078 // handle special cases
1079 if (p
.d_kind
== api::CONST_ARRAY
&& !p
.d_type
.isNull())
1081 if (args
.size() != 1)
1083 parseError("Too many arguments to array constant.");
1085 api::Term constVal
= args
[0];
1086 if (!constVal
.isConst())
1088 // To parse array constants taking reals whose values are specified by
1089 // rationals, e.g. ((as const (Array Int Real)) (/ 1 3)), we must handle
1090 // the fact that (/ 1 3) is the division of constants 1 and 3, and not
1091 // the resulting constant rational value. Thus, we must construct the
1092 // resulting rational here. This also is applied for integral real values
1093 // like 5.0 which are converted to (/ 5 1) to distinguish them from
1094 // integer constants. We must ensure numerator and denominator are
1095 // constant and the denominator is non-zero.
1096 if (constVal
.getKind() == api::DIVISION
&& constVal
[0].isConst()
1097 && constVal
[1].isConst()
1098 && !constVal
[1].getExpr().getConst
<Rational
>().isZero())
1100 std::stringstream sdiv
;
1101 sdiv
<< constVal
[0] << "/" << constVal
[1];
1102 constVal
= d_solver
->mkReal(sdiv
.str());
1104 if (!constVal
.isConst())
1106 std::stringstream ss
;
1107 ss
<< "expected constant term inside array constant, but found "
1108 << "nonconstant term:" << std::endl
1109 << "the term: " << constVal
;
1110 parseError(ss
.str());
1113 if (!p
.d_type
.getArrayElementSort().isComparableTo(constVal
.getSort()))
1115 std::stringstream ss
;
1116 ss
<< "type mismatch inside array constant term:" << std::endl
1117 << "array type: " << p
.d_type
<< std::endl
1118 << "expected const type: " << p
.d_type
.getArrayElementSort()
1120 << "computed const type: " << constVal
.getSort();
1121 parseError(ss
.str());
1123 api::Term ret
= d_solver
->mkConstArray(p
.d_type
, constVal
);
1124 Debug("parser") << "applyParseOp: return store all " << ret
<< std::endl
;
1127 else if (p
.d_kind
== api::APPLY_SELECTOR
&& !p
.d_expr
.isNull())
1129 // tuple selector case
1130 Integer x
= p
.d_expr
.getExpr().getConst
<Rational
>().getNumerator();
1131 if (!x
.fitsUnsignedInt())
1133 parseError("index of tupSel is larger than size of unsigned int");
1135 unsigned int n
= x
.toUnsignedInt();
1136 if (args
.size() != 1)
1138 parseError("tupSel should only be applied to one tuple argument");
1140 api::Sort t
= args
[0].getSort();
1143 parseError("tupSel applied to non-tuple");
1145 size_t length
= t
.getTupleLength();
1148 std::stringstream ss
;
1149 ss
<< "tuple is of length " << length
<< "; cannot access index " << n
;
1150 parseError(ss
.str());
1152 const Datatype
& dt
= ((DatatypeType
)t
.getType()).getDatatype();
1154 d_solver
->mkTerm(api::APPLY_SELECTOR
,
1155 api::Term(d_solver
, dt
[0][n
].getSelector()),
1157 Debug("parser") << "applyParseOp: return selector " << ret
<< std::endl
;
1160 else if (p
.d_kind
!= api::NULL_EXPR
)
1162 // it should not have an expression or type specified at this point
1163 if (!p
.d_expr
.isNull() || !p
.d_type
.isNull())
1165 std::stringstream ss
;
1166 ss
<< "Could not process parsed qualified identifier kind " << p
.d_kind
;
1167 parseError(ss
.str());
1169 // otherwise it is a simple application
1172 else if (isBuiltinOperator
)
1174 if (!opts
.getUfHo() && (kind
== api::EQUAL
|| kind
== api::DISTINCT
))
1176 // need --uf-ho if these operators are applied over function args
1177 for (std::vector
<api::Term
>::iterator i
= args
.begin(); i
!= args
.end();
1180 if ((*i
).getSort().isFunction())
1183 "Cannot apply equalty to functions unless --uf-ho is set.");
1187 if (!strictModeEnabled() && (kind
== api::AND
|| kind
== api::OR
)
1188 && args
.size() == 1)
1190 // Unary AND/OR can be replaced with the argument.
1191 Debug("parser") << "applyParseOp: return unary " << args
[0] << std::endl
;
1194 else if (kind
== api::MINUS
&& args
.size() == 1)
1196 api::Term ret
= d_solver
->mkTerm(api::UMINUS
, args
[0]);
1197 Debug("parser") << "applyParseOp: return uminus " << ret
<< std::endl
;
1200 if (kind
== api::EQ_RANGE
&& d_solver
->getOption("arrays-exp") != "true")
1203 "eqrange predicate requires option --arrays-exp to be enabled.");
1205 api::Term ret
= d_solver
->mkTerm(kind
, args
);
1206 Debug("parser") << "applyParseOp: return default builtin " << ret
1211 if (args
.size() >= 2)
1213 // may be partially applied function, in this case we use HO_APPLY
1214 api::Sort argt
= args
[0].getSort();
1215 if (argt
.isFunction())
1217 unsigned arity
= argt
.getFunctionArity();
1218 if (args
.size() - 1 < arity
)
1220 if (!opts
.getUfHo())
1222 parseError("Cannot partially apply functions unless --uf-ho is set.");
1224 Debug("parser") << "Partial application of " << args
[0];
1225 Debug("parser") << " : #argTypes = " << arity
;
1226 Debug("parser") << ", #args = " << args
.size() - 1 << std::endl
;
1227 api::Term ret
= d_solver
->mkTerm(api::HO_APPLY
, args
);
1228 Debug("parser") << "applyParseOp: return curry higher order " << ret
1230 // must curry the partial application
1237 api::Term ret
= d_solver
->mkTerm(op
, args
);
1238 Debug("parser") << "applyParseOp: return op : " << ret
<< std::endl
;
1241 if (kind
== api::NULL_EXPR
)
1243 // should never happen in the new API
1244 parseError("do not know how to process parse op");
1246 Debug("parser") << "Try default term construction for kind " << kind
1247 << " #args = " << args
.size() << "..." << std::endl
;
1248 api::Term ret
= d_solver
->mkTerm(kind
, args
);
1249 Debug("parser") << "applyParseOp: return : " << ret
<< std::endl
;
1253 api::Term
Smt2::setNamedAttribute(api::Term
& expr
, const SExpr
& sexpr
)
1255 if (!sexpr
.isKeyword())
1257 parseError("improperly formed :named annotation");
1259 std::string name
= sexpr
.getValue();
1260 checkUserSymbol(name
);
1261 // ensure expr is a closed subterm
1262 if (expr
.getExpr().hasFreeVariable())
1264 std::stringstream ss
;
1265 ss
<< ":named annotations can only name terms that are closed";
1266 parseError(ss
.str());
1268 // check that sexpr is a fresh function symbol, and reserve it
1269 reserveSymbolAtAssertionLevel(name
);
1271 api::Term func
= bindVar(name
, expr
.getSort(), ExprManager::VAR_FLAG_DEFINED
);
1272 // remember the last term to have been given a :named attribute
1273 setLastNamedTerm(expr
, name
);
1277 api::Term
Smt2::mkAnd(const std::vector
<api::Term
>& es
)
1281 return d_solver
->mkTrue();
1283 else if (es
.size() == 1)
1289 return d_solver
->mkTerm(api::AND
, es
);
1293 } // namespace parser
1294 }/* CVC4 namespace */