Modify the smt2 parser to use the Sygus grammar. (#4829)
[cvc5.git] / src / parser / smt2 / smt2.cpp
1 /********************* */
2 /*! \file smt2.cpp
3 ** \verbatim
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
11 **
12 ** \brief Definitions of SMT2 constants.
13 **
14 ** Definitions of SMT2 constants.
15 **/
16 #include "parser/smt2/smt2.h"
17
18 #include <algorithm>
19
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"
27
28 // ANTLR defines these, which is really bad!
29 #undef true
30 #undef false
31
32 namespace CVC4 {
33 namespace parser {
34
35 Smt2::Smt2(api::Solver* solver, Input* input, bool strictMode, bool parseOnly)
36 : Parser(solver, input, strictMode, parseOnly),
37 d_logicSet(false),
38 d_seenSetLogic(false)
39 {
40 if (!strictModeEnabled())
41 {
42 addCoreSymbols();
43 }
44 }
45
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, ">=");
56
57 if (!strictModeEnabled())
58 {
59 // NOTE: this operator is non-standard
60 addOperator(api::POW, "^");
61 }
62 }
63
64 void Smt2::addTranscendentalOperators()
65 {
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");
80 }
81
82 void Smt2::addQuantifiersOperators()
83 {
84 if (!strictModeEnabled())
85 {
86 addOperator(api::INST_CLOSURE, "inst-closure");
87 }
88 }
89
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");
122
123 addIndexedOperator(api::BITVECTOR_EXTRACT, api::BITVECTOR_EXTRACT, "extract");
124 addIndexedOperator(api::BITVECTOR_REPEAT, api::BITVECTOR_REPEAT, "repeat");
125 addIndexedOperator(
126 api::BITVECTOR_ZERO_EXTEND, api::BITVECTOR_ZERO_EXTEND, "zero_extend");
127 addIndexedOperator(
128 api::BITVECTOR_SIGN_EXTEND, api::BITVECTOR_SIGN_EXTEND, "sign_extend");
129 addIndexedOperator(
130 api::BITVECTOR_ROTATE_LEFT, api::BITVECTOR_ROTATE_LEFT, "rotate_left");
131 addIndexedOperator(
132 api::BITVECTOR_ROTATE_RIGHT, api::BITVECTOR_ROTATE_RIGHT, "rotate_right");
133 }
134
135 void Smt2::addDatatypesOperators()
136 {
137 Parser::addOperator(api::APPLY_CONSTRUCTOR);
138 Parser::addOperator(api::APPLY_TESTER);
139 Parser::addOperator(api::APPLY_SELECTOR);
140
141 if (!strictModeEnabled())
142 {
143 addOperator(api::DT_SIZE, "dt.size");
144 }
145 }
146
147 void Smt2::addStringOperators() {
148 defineVar(
149 "re.all",
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())
165 {
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");
170 // sequence versions
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");
185 }
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)
189 {
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");
196 }
197 else
198 {
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");
205 }
206
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.<=");
220 }
221
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");
249
250 addIndexedOperator(api::FLOATINGPOINT_TO_FP_GENERIC,
251 api::FLOATINGPOINT_TO_FP_GENERIC,
252 "to_fp");
253 addIndexedOperator(api::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR,
254 api::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR,
255 "to_fp_unsigned");
256 addIndexedOperator(
257 api::FLOATINGPOINT_TO_UBV, api::FLOATINGPOINT_TO_UBV, "fp.to_ubv");
258 addIndexedOperator(
259 api::FLOATINGPOINT_TO_SBV, api::FLOATINGPOINT_TO_SBV, "fp.to_sbv");
260
261 if (!strictModeEnabled())
262 {
263 addIndexedOperator(api::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR,
264 api::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR,
265 "to_fp_bv");
266 addIndexedOperator(api::FLOATINGPOINT_TO_FP_FLOATINGPOINT,
267 api::FLOATINGPOINT_TO_FP_FLOATINGPOINT,
268 "to_fp_fp");
269 addIndexedOperator(api::FLOATINGPOINT_TO_FP_REAL,
270 api::FLOATINGPOINT_TO_FP_REAL,
271 "to_fp_real");
272 addIndexedOperator(api::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR,
273 api::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR,
274 "to_fp_signed");
275 }
276 }
277
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);
287 }
288
289 void Smt2::addCoreSymbols()
290 {
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");
302 }
303
304 void Smt2::addOperator(api::Kind kind, const std::string& name)
305 {
306 Debug("parser") << "Smt2::addOperator( " << kind << ", " << name << " )"
307 << std::endl;
308 Parser::addOperator(kind);
309 operatorKindMap[name] = kind;
310 }
311
312 void Smt2::addIndexedOperator(api::Kind tKind,
313 api::Kind opKind,
314 const std::string& name)
315 {
316 Parser::addOperator(tKind);
317 d_indexedOpKindMap[name] = opKind;
318 }
319
320 api::Kind Smt2::getOperatorKind(const std::string& name) const
321 {
322 // precondition: isOperatorEnabled(name)
323 return operatorKindMap.find(name)->second;
324 }
325
326 bool Smt2::isOperatorEnabled(const std::string& name) const {
327 return operatorKindMap.find(name) != operatorKindMap.end();
328 }
329
330 bool Smt2::isTheoryEnabled(theory::TheoryId theory) const
331 {
332 return d_logic.isTheoryEnabled(theory);
333 }
334
335 bool Smt2::isHoEnabled() const
336 {
337 return getLogic().isHigherOrder() && d_solver->getOptions().getUfHo();
338 }
339
340 bool Smt2::logicIsSet() {
341 return d_logicSet;
342 }
343
344 api::Term Smt2::getExpressionForNameAndType(const std::string& name,
345 api::Sort t)
346 {
347 if (isAbstractValue(name))
348 {
349 return mkAbstractValue(name);
350 }
351 return Parser::getExpressionForNameAndType(name, t);
352 }
353
354 bool Smt2::getTesterName(api::Term cons, std::string& name)
355 {
356 if ((v2_6() || sygus_v2()) && strictModeEnabled())
357 {
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.
360 return false;
361 }
362 std::stringstream ss;
363 ss << "is-" << cons;
364 name = ss.str();
365 return true;
366 }
367
368 api::Term Smt2::mkIndexedConstant(const std::string& name,
369 const std::vector<uint64_t>& numerals)
370 {
371 if (d_logic.isTheoryEnabled(theory::THEORY_FP))
372 {
373 if (name == "+oo")
374 {
375 return d_solver->mkPosInf(numerals[0], numerals[1]);
376 }
377 else if (name == "-oo")
378 {
379 return d_solver->mkNegInf(numerals[0], numerals[1]);
380 }
381 else if (name == "NaN")
382 {
383 return d_solver->mkNaN(numerals[0], numerals[1]);
384 }
385 else if (name == "+zero")
386 {
387 return d_solver->mkPosZero(numerals[0], numerals[1]);
388 }
389 else if (name == "-zero")
390 {
391 return d_solver->mkNegZero(numerals[0], numerals[1]);
392 }
393 }
394
395 if (d_logic.isTheoryEnabled(theory::THEORY_BV) && name.find("bv") == 0)
396 {
397 std::string bvStr = name.substr(2);
398 return d_solver->mkBitVector(numerals[0], bvStr, 10);
399 }
400
401 // NOTE: Theory parametric constants go here
402
403 parseError(std::string("Unknown indexed literal `") + name + "'");
404 return api::Term();
405 }
406
407 api::Op Smt2::mkIndexedOp(const std::string& name,
408 const std::vector<uint64_t>& numerals)
409 {
410 const auto& kIt = d_indexedOpKindMap.find(name);
411 if (kIt != d_indexedOpKindMap.end())
412 {
413 api::Kind k = (*kIt).second;
414 if (numerals.size() == 1)
415 {
416 return d_solver->mkOp(k, numerals[0]);
417 }
418 else if (numerals.size() == 2)
419 {
420 return d_solver->mkOp(k, numerals[0], numerals[1]);
421 }
422 }
423
424 parseError(std::string("Unknown indexed function `") + name + "'");
425 return api::Op();
426 }
427
428 api::Term Smt2::bindDefineFunRec(
429 const std::string& fname,
430 const std::vector<std::pair<std::string, api::Sort>>& sortedVarNames,
431 api::Sort t,
432 std::vector<api::Term>& flattenVars)
433 {
434 std::vector<api::Sort> sorts;
435 for (const std::pair<std::string, api::Sort>& svn : sortedVarNames)
436 {
437 sorts.push_back(svn.second);
438 }
439
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);
443
444 // allow overloading
445 return bindVar(fname, ft, ExprManager::VAR_FLAG_NONE, true);
446 }
447
448 void Smt2::pushDefineFunRecScope(
449 const std::vector<std::pair<std::string, api::Sort>>& sortedVarNames,
450 api::Term func,
451 const std::vector<api::Term>& flattenVars,
452 std::vector<api::Term>& bvs,
453 bool bindingLevel)
454 {
455 pushScope(bindingLevel);
456
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)
460 {
461 api::Term v = bindBoundVar(svn.first, svn.second);
462 bvs.push_back(v);
463 }
464
465 bvs.insert(bvs.end(), flattenVars.begin(), flattenVars.end());
466 }
467
468 void Smt2::reset() {
469 d_logicSet = false;
470 d_seenSetLogic = false;
471 d_logic = LogicInfo();
472 operatorKindMap.clear();
473 d_lastNamedTerm = std::pair<api::Term, std::string>();
474 this->Parser::reset();
475
476 if( !strictModeEnabled() ) {
477 addCoreSymbols();
478 }
479 }
480
481 void Smt2::resetAssertions() {
482 // Remove all declarations except the ones at level 0.
483 while (this->scopeLevel() > 0) {
484 this->popScope();
485 }
486 }
487
488 Smt2::SynthFunFactory::SynthFunFactory(
489 Smt2* smt2,
490 const std::string& id,
491 bool isInv,
492 api::Sort range,
493 std::vector<std::pair<std::string, api::Sort>>& sortedVarNames)
494 : d_smt2(smt2), d_id(id), d_sort(range), d_isInv(isInv)
495 {
496 if (range.isNull())
497 {
498 smt2->parseError("Must supply return type for synth-fun.");
499 }
500 if (range.isFunction())
501 {
502 smt2->parseError("Cannot use synth-fun with function return type.");
503 }
504
505 std::vector<api::Sort> varSorts;
506 for (const std::pair<std::string, api::Sort>& p : sortedVarNames)
507 {
508 varSorts.push_back(p.second);
509 }
510
511 api::Sort funSort = varSorts.empty()
512 ? range
513 : d_smt2->d_solver->mkFunctionSort(varSorts, range);
514
515 // we do not allow overloading for synth fun
516 d_fun = d_smt2->bindBoundVar(id, funSort);
517
518 Debug("parser-sygus") << "Define synth fun : " << id << std::endl;
519
520 d_smt2->pushScope(true);
521 d_sygusVars = d_smt2->bindBoundVars(sortedVarNames);
522 }
523
524 std::unique_ptr<Command> Smt2::SynthFunFactory::mkCommand(api::Grammar* grammar)
525 {
526 Debug("parser-sygus") << "...read synth fun " << d_id << std::endl;
527 d_smt2->popScope();
528 return std::unique_ptr<Command>(new SynthFunCommand(
529 d_smt2->d_solver, d_id, d_fun, d_sygusVars, d_sort, d_isInv, grammar));
530 }
531
532 std::unique_ptr<Command> Smt2::invConstraint(
533 const std::vector<std::string>& names)
534 {
535 checkThatLogicIsSet();
536 Debug("parser-sygus") << "Sygus : define sygus funs..." << std::endl;
537 Debug("parser-sygus") << "Sygus : read inv-constraint..." << std::endl;
538
539 if (names.size() != 4)
540 {
541 parseError(
542 "Bad syntax for inv-constraint: expected 4 "
543 "arguments.");
544 }
545
546 std::vector<api::Term> terms;
547 for (const std::string& name : names)
548 {
549 if (!isDeclared(name))
550 {
551 std::stringstream ss;
552 ss << "Function " << name << " in inv-constraint is not defined.";
553 parseError(ss.str());
554 }
555
556 terms.push_back(getVariable(name));
557 }
558
559 return std::unique_ptr<Command>(
560 new SygusInvConstraintCommand(api::termVectorToExprs(terms)));
561 }
562
563 Command* Smt2::setLogic(std::string name, bool fromCommand)
564 {
565 if (fromCommand)
566 {
567 if (d_seenSetLogic)
568 {
569 parseError("Only one set-logic is allowed.");
570 }
571 d_seenSetLogic = true;
572
573 if (logicIsForced())
574 {
575 // If the logic is forced, we ignore all set-logic requests from commands.
576 return new EmptyCommand();
577 }
578 }
579
580 d_logicSet = true;
581 d_logic = name;
582
583 // if sygus is enabled, we must enable UF, datatypes, integer arithmetic and
584 // higher-order
585 if(sygus()) {
586 if (!d_logic.isQuantified())
587 {
588 warning("Logics in sygus are assumed to contain quantifiers.");
589 warning("Omit QF_ from the logic to avoid this warning.");
590 }
591 }
592
593 // Core theory belongs to every logic
594 addCoreSymbols();
595
596 if(d_logic.isTheoryEnabled(theory::THEORY_UF)) {
597 Parser::addOperator(api::APPLY_UF);
598
599 if (!strictModeEnabled() && d_logic.hasCardinalityConstraints())
600 {
601 addOperator(api::CARDINALITY_CONSTRAINT, "fmf.card");
602 addOperator(api::CARDINALITY_VALUE, "fmf.card.val");
603 }
604 }
605
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");
614 }
615
616 if (d_logic.areRealsUsed())
617 {
618 defineType("Real", d_solver->getRealSort());
619 addArithmeticOperators();
620 addOperator(api::DIVISION, "/");
621 if (!strictModeEnabled())
622 {
623 addOperator(api::ABS, "abs");
624 }
625 }
626
627 if (d_logic.areIntegersUsed() && d_logic.areRealsUsed())
628 {
629 addOperator(api::TO_INTEGER, "to_int");
630 addOperator(api::IS_INTEGER, "is_int");
631 addOperator(api::TO_REAL, "to_real");
632 }
633
634 if (d_logic.areTranscendentalsUsed())
635 {
636 defineVar("real.pi", d_solver->mkTerm(api::PI));
637 addTranscendentalOperators();
638 }
639 if (!strictModeEnabled())
640 {
641 // integer version of AND
642 addIndexedOperator(api::IAND, api::IAND, "iand");
643 }
644 }
645
646 if(d_logic.isTheoryEnabled(theory::THEORY_ARRAYS)) {
647 addOperator(api::SELECT, "select");
648 addOperator(api::STORE, "store");
649 addOperator(api::EQ_RANGE, "eqrange");
650 }
651
652 if(d_logic.isTheoryEnabled(theory::THEORY_BV)) {
653 addBitvectorOperators();
654
655 if (!strictModeEnabled() && d_logic.isTheoryEnabled(theory::THEORY_ARITH)
656 && d_logic.areIntegersUsed())
657 {
658 // Conversions between bit-vectors and integers
659 addOperator(api::BITVECTOR_TO_NAT, "bv2nat");
660 addIndexedOperator(
661 api::INT_TO_BITVECTOR, api::INT_TO_BITVECTOR, "int2bv");
662 }
663 }
664
665 if(d_logic.isTheoryEnabled(theory::THEORY_DATATYPES)) {
666 const std::vector<api::Sort> types;
667 defineType("Tuple", d_solver->mkTupleSort(types));
668 addDatatypesOperators();
669 }
670
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()));
676
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");
691 }
692
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());
697
698 if (getLanguage() == language::input::LANG_SMTLIB_V2_6
699 || getLanguage() == language::input::LANG_SYGUS_V2)
700 {
701 defineVar("re.none", d_solver->mkRegexpEmpty());
702 }
703 else
704 {
705 defineVar("re.nostr", d_solver->mkRegexpEmpty());
706 }
707 defineVar("re.allchar", d_solver->mkRegexpSigma());
708
709 // Boolean is a placeholder
710 defineVar("seq.empty",
711 d_solver->mkEmptySequence(d_solver->getBooleanSort()));
712
713 addStringOperators();
714 }
715
716 if(d_logic.isQuantified()) {
717 addQuantifiersOperators();
718 }
719
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));
726
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));
742
743 addFloatingPointOperators();
744 }
745
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()));
750
751 addSepOperators();
752 }
753
754 Command* cmd =
755 new SetBenchmarkLogicCommand(sygus() ? d_logic.getLogicString() : name);
756 cmd->setMuted(!fromCommand);
757 return cmd;
758 } /* Smt2::setLogic() */
759
760 api::Grammar* Smt2::mkGrammar(const std::vector<api::Term>& boundVars,
761 const std::vector<api::Term>& ntSymbols)
762 {
763 d_allocGrammars.emplace_back(new api::Grammar(
764 std::move(d_solver->mkSygusGrammar(boundVars, ntSymbols))));
765 return d_allocGrammars.back().get();
766 }
767
768 bool Smt2::sygus() const
769 {
770 InputLanguage ilang = getLanguage();
771 return ilang == language::input::LANG_SYGUS_V2;
772 }
773
774 bool Smt2::sygus_v2() const
775 {
776 return getLanguage() == language::input::LANG_SYGUS_V2;
777 }
778
779 void Smt2::setInfo(const std::string& flag, const SExpr& sexpr) {
780 // TODO: ???
781 }
782
783 void Smt2::setOption(const std::string& flag, const SExpr& sexpr) {
784 // TODO: ???
785 }
786
787 void Smt2::checkThatLogicIsSet()
788 {
789 if (!logicIsSet())
790 {
791 if (strictModeEnabled())
792 {
793 parseError("set-logic must appear before this point.");
794 }
795 else
796 {
797 Command* cmd = nullptr;
798 if (logicIsForced())
799 {
800 cmd = setLogic(getForcedLogic(), false);
801 }
802 else
803 {
804 warning("No set-logic command was given before this point.");
805 warning("CVC4 will make all theories available.");
806 warning(
807 "Consider setting a stricter logic for (likely) better "
808 "performance.");
809 warning("To suppress this warning in the future use (set-logic ALL).");
810
811 cmd = setLogic("ALL", false);
812 }
813 preemptCommand(cmd);
814 }
815 }
816 }
817
818 void Smt2::checkLogicAllowsFreeSorts()
819 {
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))
824 {
825 parseErrorLogic("Free sort symbols not allowed in ");
826 }
827 }
828
829 void Smt2::checkLogicAllowsFunctions()
830 {
831 if (!d_logic.isTheoryEnabled(theory::THEORY_UF))
832 {
833 parseError(
834 "Functions (of non-zero arity) cannot "
835 "be declared in logic "
836 + d_logic.getLogicString() + " unless option --uf-ho is used");
837 }
838 }
839
840 /* The include are managed in the lexer but called in the parser */
841 // Inspired by http://www.antlr3.org/api/C/interop.html
842
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.
847 //
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 */
854 if( in == NULL ) {
855 Debug("parser") << "Can't open " << filename << std::endl;
856 return false;
857 }
858 // Same thing as the predefined PUSHSTREAM(in);
859 lexer->pushCharStream(lexer, in);
860 // restart it
861 //lexer->rec->state->tokenStartCharIndex = -10;
862 //lexer->emit(lexer);
863
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.
871
872 //TODO what said before
873 return true;
874 }
875
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.");
880 }
881
882 // Get the lexer
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();
887
888 // Find the directory of the current input file
889 std::string path;
890 size_t pos = inputName.rfind('/');
891 if(pos != std::string::npos) {
892 path = std::string(inputName, 0, pos + 1);
893 }
894 path.append(filename);
895 if(!newInputStream(path, lexer)) {
896 parseError("Couldn't open include file `" + path + "'");
897 }
898 }
899 bool Smt2::isAbstractValue(const std::string& name)
900 {
901 return name.length() >= 2 && name[0] == '@' && name[1] != '0'
902 && name.find_first_not_of("0123456789", 1) == std::string::npos;
903 }
904
905 api::Term Smt2::mkAbstractValue(const std::string& name)
906 {
907 assert(isAbstractValue(name));
908 // remove the '@'
909 return d_solver->mkAbstractValue(name.substr(1));
910 }
911
912 InputLanguage Smt2::getLanguage() const
913 {
914 return d_solver->getOptions().getInputLanguage();
915 }
916
917 void Smt2::parseOpApplyTypeAscription(ParseOp& p, api::Sort type)
918 {
919 Debug("parser") << "parseOpApplyTypeAscription : " << p << " " << type
920 << std::endl;
921 // (as const (Array T1 T2))
922 if (p.d_kind == api::CONST_ARRAY)
923 {
924 if (!type.isArray())
925 {
926 std::stringstream ss;
927 ss << "expected array constant term, but cast is not of array type"
928 << std::endl
929 << "cast type: " << type;
930 parseError(ss.str());
931 }
932 p.d_type = type;
933 return;
934 }
935 if (p.d_expr.isNull())
936 {
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))
942 {
943 p.d_expr = getExpressionForNameAndType(p.d_name, type);
944 p.d_name = std::string("");
945 }
946 if (p.d_expr.isNull())
947 {
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());
952 }
953 }
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);
959 }
960
961 api::Term Smt2::parseOpToExpr(ParseOp& p)
962 {
963 Debug("parser") << "parseOpToExpr: " << p << std::endl;
964 api::Term expr;
965 if (p.d_kind != api::NULL_EXPR || !p.d_type.isNull())
966 {
967 parseError(
968 "Bad syntax for qualified identifier operator in term position.");
969 }
970 else if (!p.d_expr.isNull())
971 {
972 expr = p.d_expr;
973 }
974 else if (!isDeclared(p.d_name, SYM_VARIABLE))
975 {
976 std::stringstream ss;
977 ss << "Symbol " << p.d_name << " is not declared.";
978 parseError(ss.str());
979 }
980 else
981 {
982 expr = getExpressionForName(p.d_name);
983 }
984 assert(!expr.isNull());
985 return expr;
986 }
987
988 api::Term Smt2::applyParseOp(ParseOp& p, std::vector<api::Term>& args)
989 {
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"))
995 {
996 Debug("parser") << "applyParseOp: " << p << " to:" << std::endl;
997 for (std::vector<api::Term>::iterator i = args.begin(); i != args.end();
998 ++i)
999 {
1000 Debug("parser") << "++ " << *i << std::endl;
1001 }
1002 }
1003 api::Op op;
1004 if (p.d_kind != api::NULL_EXPR)
1005 {
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.
1008 }
1009 else if (!p.d_expr.isNull())
1010 {
1011 // An explicit operator, e.g. an apply function
1012 api::Kind fkind = getKindForFunction(p.d_expr);
1013 if (fkind != api::UNDEFINED_KIND)
1014 {
1015 // Some operators may require a specific kind.
1016 // Testers are handled differently than other indexed operators,
1017 // since they require a kind.
1018 kind = fkind;
1019 Debug("parser") << "Got function kind " << kind << " for expression "
1020 << std::endl;
1021 }
1022 args.insert(args.begin(), p.d_expr);
1023 }
1024 else if (!p.d_op.isNull())
1025 {
1026 // it was given an operator
1027 op = p.d_op;
1028 }
1029 else
1030 {
1031 isBuiltinOperator = isOperatorEnabled(p.d_name);
1032 if (isBuiltinOperator)
1033 {
1034 // a builtin operator, convert to kind
1035 kind = getOperatorKind(p.d_name);
1036 }
1037 else
1038 {
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);
1042 if (!v.isNull())
1043 {
1044 checkFunctionLike(v);
1045 kind = getKindForFunction(v);
1046 args.insert(args.begin(), v);
1047 }
1048 else
1049 {
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
1053 // arguments.
1054 std::vector<api::Sort> argTypes;
1055 for (std::vector<api::Term>::iterator i = args.begin(); i != args.end();
1056 ++i)
1057 {
1058 argTypes.push_back((*i).getSort());
1059 }
1060 api::Term fop = getOverloadedFunctionForTypes(p.d_name, argTypes);
1061 if (!fop.isNull())
1062 {
1063 checkFunctionLike(fop);
1064 kind = getKindForFunction(fop);
1065 args.insert(args.begin(), fop);
1066 }
1067 else
1068 {
1069 parseError(
1070 "Cannot find unambiguous overloaded function for argument "
1071 "types.");
1072 }
1073 }
1074 }
1075 }
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())
1080 {
1081 if (args.size() != 1)
1082 {
1083 parseError("Too many arguments to array constant.");
1084 }
1085 api::Term constVal = args[0];
1086 if (!constVal.isConst())
1087 {
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())
1099 {
1100 std::stringstream sdiv;
1101 sdiv << constVal[0] << "/" << constVal[1];
1102 constVal = d_solver->mkReal(sdiv.str());
1103 }
1104 if (!constVal.isConst())
1105 {
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());
1111 }
1112 }
1113 if (!p.d_type.getArrayElementSort().isComparableTo(constVal.getSort()))
1114 {
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()
1119 << std::endl
1120 << "computed const type: " << constVal.getSort();
1121 parseError(ss.str());
1122 }
1123 api::Term ret = d_solver->mkConstArray(p.d_type, constVal);
1124 Debug("parser") << "applyParseOp: return store all " << ret << std::endl;
1125 return ret;
1126 }
1127 else if (p.d_kind == api::APPLY_SELECTOR && !p.d_expr.isNull())
1128 {
1129 // tuple selector case
1130 Integer x = p.d_expr.getExpr().getConst<Rational>().getNumerator();
1131 if (!x.fitsUnsignedInt())
1132 {
1133 parseError("index of tupSel is larger than size of unsigned int");
1134 }
1135 unsigned int n = x.toUnsignedInt();
1136 if (args.size() != 1)
1137 {
1138 parseError("tupSel should only be applied to one tuple argument");
1139 }
1140 api::Sort t = args[0].getSort();
1141 if (!t.isTuple())
1142 {
1143 parseError("tupSel applied to non-tuple");
1144 }
1145 size_t length = t.getTupleLength();
1146 if (n >= length)
1147 {
1148 std::stringstream ss;
1149 ss << "tuple is of length " << length << "; cannot access index " << n;
1150 parseError(ss.str());
1151 }
1152 const Datatype& dt = ((DatatypeType)t.getType()).getDatatype();
1153 api::Term ret =
1154 d_solver->mkTerm(api::APPLY_SELECTOR,
1155 api::Term(d_solver, dt[0][n].getSelector()),
1156 args[0]);
1157 Debug("parser") << "applyParseOp: return selector " << ret << std::endl;
1158 return ret;
1159 }
1160 else if (p.d_kind != api::NULL_EXPR)
1161 {
1162 // it should not have an expression or type specified at this point
1163 if (!p.d_expr.isNull() || !p.d_type.isNull())
1164 {
1165 std::stringstream ss;
1166 ss << "Could not process parsed qualified identifier kind " << p.d_kind;
1167 parseError(ss.str());
1168 }
1169 // otherwise it is a simple application
1170 kind = p.d_kind;
1171 }
1172 else if (isBuiltinOperator)
1173 {
1174 if (!opts.getUfHo() && (kind == api::EQUAL || kind == api::DISTINCT))
1175 {
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();
1178 ++i)
1179 {
1180 if ((*i).getSort().isFunction())
1181 {
1182 parseError(
1183 "Cannot apply equalty to functions unless --uf-ho is set.");
1184 }
1185 }
1186 }
1187 if (!strictModeEnabled() && (kind == api::AND || kind == api::OR)
1188 && args.size() == 1)
1189 {
1190 // Unary AND/OR can be replaced with the argument.
1191 Debug("parser") << "applyParseOp: return unary " << args[0] << std::endl;
1192 return args[0];
1193 }
1194 else if (kind == api::MINUS && args.size() == 1)
1195 {
1196 api::Term ret = d_solver->mkTerm(api::UMINUS, args[0]);
1197 Debug("parser") << "applyParseOp: return uminus " << ret << std::endl;
1198 return ret;
1199 }
1200 if (kind == api::EQ_RANGE && d_solver->getOption("arrays-exp") != "true")
1201 {
1202 parseError(
1203 "eqrange predicate requires option --arrays-exp to be enabled.");
1204 }
1205 api::Term ret = d_solver->mkTerm(kind, args);
1206 Debug("parser") << "applyParseOp: return default builtin " << ret
1207 << std::endl;
1208 return ret;
1209 }
1210
1211 if (args.size() >= 2)
1212 {
1213 // may be partially applied function, in this case we use HO_APPLY
1214 api::Sort argt = args[0].getSort();
1215 if (argt.isFunction())
1216 {
1217 unsigned arity = argt.getFunctionArity();
1218 if (args.size() - 1 < arity)
1219 {
1220 if (!opts.getUfHo())
1221 {
1222 parseError("Cannot partially apply functions unless --uf-ho is set.");
1223 }
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
1229 << std::endl;
1230 // must curry the partial application
1231 return ret;
1232 }
1233 }
1234 }
1235 if (!op.isNull())
1236 {
1237 api::Term ret = d_solver->mkTerm(op, args);
1238 Debug("parser") << "applyParseOp: return op : " << ret << std::endl;
1239 return ret;
1240 }
1241 if (kind == api::NULL_EXPR)
1242 {
1243 // should never happen in the new API
1244 parseError("do not know how to process parse op");
1245 }
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;
1250 return ret;
1251 }
1252
1253 api::Term Smt2::setNamedAttribute(api::Term& expr, const SExpr& sexpr)
1254 {
1255 if (!sexpr.isKeyword())
1256 {
1257 parseError("improperly formed :named annotation");
1258 }
1259 std::string name = sexpr.getValue();
1260 checkUserSymbol(name);
1261 // ensure expr is a closed subterm
1262 if (expr.getExpr().hasFreeVariable())
1263 {
1264 std::stringstream ss;
1265 ss << ":named annotations can only name terms that are closed";
1266 parseError(ss.str());
1267 }
1268 // check that sexpr is a fresh function symbol, and reserve it
1269 reserveSymbolAtAssertionLevel(name);
1270 // define it
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);
1274 return func;
1275 }
1276
1277 api::Term Smt2::mkAnd(const std::vector<api::Term>& es)
1278 {
1279 if (es.size() == 0)
1280 {
1281 return d_solver->mkTrue();
1282 }
1283 else if (es.size() == 1)
1284 {
1285 return es[0];
1286 }
1287 else
1288 {
1289 return d_solver->mkTerm(api::AND, es);
1290 }
1291 }
1292
1293 } // namespace parser
1294 }/* CVC4 namespace */