GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/parser/smt2/smt2.cpp Lines: 591 676 87.4 %
Date: 2021-09-29 Branches: 1068 2236 47.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Andres Noetzli, Morgan Deters
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 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.
11
 * ****************************************************************************
12
 *
13
 * Definitions of SMT2 constants.
14
 */
15
#include "parser/smt2/smt2.h"
16
17
#include <algorithm>
18
19
#include "base/check.h"
20
#include "parser/antlr_input.h"
21
#include "parser/parser.h"
22
#include "parser/smt2/smt2_input.h"
23
24
// ANTLR defines these, which is really bad!
25
#undef true
26
#undef false
27
28
namespace cvc5 {
29
namespace parser {
30
31
4013
Smt2::Smt2(api::Solver* solver,
32
           SymbolManager* sm,
33
           bool strictMode,
34
4013
           bool parseOnly)
35
    : Parser(solver, sm, strictMode, parseOnly),
36
      d_logicSet(false),
37
4013
      d_seenSetLogic(false)
38
{
39
4013
}
40
41
8026
Smt2::~Smt2() {}
42
43
4490
void Smt2::addArithmeticOperators() {
44
4490
  addOperator(api::PLUS, "+");
45
4490
  addOperator(api::MINUS, "-");
46
  // api::MINUS is converted to api::UMINUS if there is only a single operand
47
4490
  Parser::addOperator(api::UMINUS);
48
4490
  addOperator(api::MULT, "*");
49
4490
  addOperator(api::LT, "<");
50
4490
  addOperator(api::LEQ, "<=");
51
4490
  addOperator(api::GT, ">");
52
4490
  addOperator(api::GEQ, ">=");
53
54
4490
  if (!strictModeEnabled())
55
  {
56
    // NOTE: this operator is non-standard
57
4486
    addOperator(api::POW, "^");
58
  }
59
4490
}
60
61
1516
void Smt2::addTranscendentalOperators()
62
{
63
1516
  addOperator(api::EXPONENTIAL, "exp");
64
1516
  addOperator(api::SINE, "sin");
65
1516
  addOperator(api::COSINE, "cos");
66
1516
  addOperator(api::TANGENT, "tan");
67
1516
  addOperator(api::COSECANT, "csc");
68
1516
  addOperator(api::SECANT, "sec");
69
1516
  addOperator(api::COTANGENT, "cot");
70
1516
  addOperator(api::ARCSINE, "arcsin");
71
1516
  addOperator(api::ARCCOSINE, "arccos");
72
1516
  addOperator(api::ARCTANGENT, "arctan");
73
1516
  addOperator(api::ARCCOSECANT, "arccsc");
74
1516
  addOperator(api::ARCSECANT, "arcsec");
75
1516
  addOperator(api::ARCCOTANGENT, "arccot");
76
1516
  addOperator(api::SQRT, "sqrt");
77
1516
}
78
79
2013
void Smt2::addQuantifiersOperators()
80
{
81
2013
}
82
83
2264
void Smt2::addBitvectorOperators() {
84
2264
  addOperator(api::BITVECTOR_CONCAT, "concat");
85
2264
  addOperator(api::BITVECTOR_NOT, "bvnot");
86
2264
  addOperator(api::BITVECTOR_AND, "bvand");
87
2264
  addOperator(api::BITVECTOR_OR, "bvor");
88
2264
  addOperator(api::BITVECTOR_NEG, "bvneg");
89
2264
  addOperator(api::BITVECTOR_ADD, "bvadd");
90
2264
  addOperator(api::BITVECTOR_MULT, "bvmul");
91
2264
  addOperator(api::BITVECTOR_UDIV, "bvudiv");
92
2264
  addOperator(api::BITVECTOR_UREM, "bvurem");
93
2264
  addOperator(api::BITVECTOR_SHL, "bvshl");
94
2264
  addOperator(api::BITVECTOR_LSHR, "bvlshr");
95
2264
  addOperator(api::BITVECTOR_ULT, "bvult");
96
2264
  addOperator(api::BITVECTOR_NAND, "bvnand");
97
2264
  addOperator(api::BITVECTOR_NOR, "bvnor");
98
2264
  addOperator(api::BITVECTOR_XOR, "bvxor");
99
2264
  addOperator(api::BITVECTOR_XNOR, "bvxnor");
100
2264
  addOperator(api::BITVECTOR_COMP, "bvcomp");
101
2264
  addOperator(api::BITVECTOR_SUB, "bvsub");
102
2264
  addOperator(api::BITVECTOR_SDIV, "bvsdiv");
103
2264
  addOperator(api::BITVECTOR_SREM, "bvsrem");
104
2264
  addOperator(api::BITVECTOR_SMOD, "bvsmod");
105
2264
  addOperator(api::BITVECTOR_ASHR, "bvashr");
106
2264
  addOperator(api::BITVECTOR_ULE, "bvule");
107
2264
  addOperator(api::BITVECTOR_UGT, "bvugt");
108
2264
  addOperator(api::BITVECTOR_UGE, "bvuge");
109
2264
  addOperator(api::BITVECTOR_SLT, "bvslt");
110
2264
  addOperator(api::BITVECTOR_SLE, "bvsle");
111
2264
  addOperator(api::BITVECTOR_SGT, "bvsgt");
112
2264
  addOperator(api::BITVECTOR_SGE, "bvsge");
113
2264
  addOperator(api::BITVECTOR_REDOR, "bvredor");
114
2264
  addOperator(api::BITVECTOR_REDAND, "bvredand");
115
116
2264
  addIndexedOperator(api::BITVECTOR_EXTRACT, api::BITVECTOR_EXTRACT, "extract");
117
2264
  addIndexedOperator(api::BITVECTOR_REPEAT, api::BITVECTOR_REPEAT, "repeat");
118
2264
  addIndexedOperator(
119
      api::BITVECTOR_ZERO_EXTEND, api::BITVECTOR_ZERO_EXTEND, "zero_extend");
120
2264
  addIndexedOperator(
121
      api::BITVECTOR_SIGN_EXTEND, api::BITVECTOR_SIGN_EXTEND, "sign_extend");
122
2264
  addIndexedOperator(
123
      api::BITVECTOR_ROTATE_LEFT, api::BITVECTOR_ROTATE_LEFT, "rotate_left");
124
2264
  addIndexedOperator(
125
      api::BITVECTOR_ROTATE_RIGHT, api::BITVECTOR_ROTATE_RIGHT, "rotate_right");
126
2264
}
127
128
1514
void Smt2::addDatatypesOperators()
129
{
130
1514
  Parser::addOperator(api::APPLY_CONSTRUCTOR);
131
1514
  Parser::addOperator(api::APPLY_TESTER);
132
1514
  Parser::addOperator(api::APPLY_SELECTOR);
133
134
1514
  if (!strictModeEnabled())
135
  {
136
1514
    Parser::addOperator(api::APPLY_UPDATER);
137
1514
    addOperator(api::DT_SIZE, "dt.size");
138
1514
    addIndexedOperator(api::APPLY_UPDATER, api::APPLY_UPDATER, "tuple_update");
139
  }
140
1514
}
141
142
1798
void Smt2::addStringOperators() {
143
3596
  defineVar(
144
      "re.all",
145
3596
      getSolver()->mkTerm(api::REGEXP_STAR, getSolver()->mkRegexpSigma()));
146
1798
  addOperator(api::STRING_CONCAT, "str.++");
147
1798
  addOperator(api::STRING_LENGTH, "str.len");
148
1798
  addOperator(api::STRING_SUBSTR, "str.substr");
149
1798
  addOperator(api::STRING_CONTAINS, "str.contains");
150
1798
  addOperator(api::STRING_CHARAT, "str.at");
151
1798
  addOperator(api::STRING_INDEXOF, "str.indexof");
152
1798
  addOperator(api::STRING_REPLACE, "str.replace");
153
1798
  addOperator(api::STRING_PREFIX, "str.prefixof");
154
1798
  addOperator(api::STRING_SUFFIX, "str.suffixof");
155
1798
  addOperator(api::STRING_FROM_CODE, "str.from_code");
156
1798
  addOperator(api::STRING_IS_DIGIT, "str.is_digit");
157
1798
  addOperator(api::STRING_REPLACE_RE, "str.replace_re");
158
1798
  addOperator(api::STRING_REPLACE_RE_ALL, "str.replace_re_all");
159
1798
  if (!strictModeEnabled())
160
  {
161
1798
    addOperator(api::STRING_INDEXOF_RE, "str.indexof_re");
162
1798
    addOperator(api::STRING_UPDATE, "str.update");
163
1798
    addOperator(api::STRING_TOLOWER, "str.tolower");
164
1798
    addOperator(api::STRING_TOUPPER, "str.toupper");
165
1798
    addOperator(api::STRING_REV, "str.rev");
166
    // sequence versions
167
1798
    addOperator(api::SEQ_CONCAT, "seq.++");
168
1798
    addOperator(api::SEQ_LENGTH, "seq.len");
169
1798
    addOperator(api::SEQ_EXTRACT, "seq.extract");
170
1798
    addOperator(api::SEQ_UPDATE, "seq.update");
171
1798
    addOperator(api::SEQ_AT, "seq.at");
172
1798
    addOperator(api::SEQ_CONTAINS, "seq.contains");
173
1798
    addOperator(api::SEQ_INDEXOF, "seq.indexof");
174
1798
    addOperator(api::SEQ_REPLACE, "seq.replace");
175
1798
    addOperator(api::SEQ_PREFIX, "seq.prefixof");
176
1798
    addOperator(api::SEQ_SUFFIX, "seq.suffixof");
177
1798
    addOperator(api::SEQ_REV, "seq.rev");
178
1798
    addOperator(api::SEQ_REPLACE_ALL, "seq.replace_all");
179
1798
    addOperator(api::SEQ_UNIT, "seq.unit");
180
1798
    addOperator(api::SEQ_NTH, "seq.nth");
181
  }
182
1798
  addOperator(api::STRING_FROM_INT, "str.from_int");
183
1798
  addOperator(api::STRING_TO_INT, "str.to_int");
184
1798
  addOperator(api::STRING_IN_REGEXP, "str.in_re");
185
1798
  addOperator(api::STRING_TO_REGEXP, "str.to_re");
186
1798
  addOperator(api::STRING_TO_CODE, "str.to_code");
187
1798
  addOperator(api::STRING_REPLACE_ALL, "str.replace_all");
188
189
1798
  addOperator(api::REGEXP_CONCAT, "re.++");
190
1798
  addOperator(api::REGEXP_UNION, "re.union");
191
1798
  addOperator(api::REGEXP_INTER, "re.inter");
192
1798
  addOperator(api::REGEXP_STAR, "re.*");
193
1798
  addOperator(api::REGEXP_PLUS, "re.+");
194
1798
  addOperator(api::REGEXP_OPT, "re.opt");
195
1798
  addIndexedOperator(api::REGEXP_REPEAT, api::REGEXP_REPEAT, "re.^");
196
1798
  addIndexedOperator(api::REGEXP_LOOP, api::REGEXP_LOOP, "re.loop");
197
1798
  addOperator(api::REGEXP_RANGE, "re.range");
198
1798
  addOperator(api::REGEXP_COMPLEMENT, "re.comp");
199
1798
  addOperator(api::REGEXP_DIFF, "re.diff");
200
1798
  addOperator(api::STRING_LT, "str.<");
201
1798
  addOperator(api::STRING_LEQ, "str.<=");
202
1798
}
203
204
1509
void Smt2::addFloatingPointOperators() {
205
1509
  addOperator(api::FLOATINGPOINT_FP, "fp");
206
1509
  addOperator(api::FLOATINGPOINT_EQ, "fp.eq");
207
1509
  addOperator(api::FLOATINGPOINT_ABS, "fp.abs");
208
1509
  addOperator(api::FLOATINGPOINT_NEG, "fp.neg");
209
1509
  addOperator(api::FLOATINGPOINT_ADD, "fp.add");
210
1509
  addOperator(api::FLOATINGPOINT_SUB, "fp.sub");
211
1509
  addOperator(api::FLOATINGPOINT_MULT, "fp.mul");
212
1509
  addOperator(api::FLOATINGPOINT_DIV, "fp.div");
213
1509
  addOperator(api::FLOATINGPOINT_FMA, "fp.fma");
214
1509
  addOperator(api::FLOATINGPOINT_SQRT, "fp.sqrt");
215
1509
  addOperator(api::FLOATINGPOINT_REM, "fp.rem");
216
1509
  addOperator(api::FLOATINGPOINT_RTI, "fp.roundToIntegral");
217
1509
  addOperator(api::FLOATINGPOINT_MIN, "fp.min");
218
1509
  addOperator(api::FLOATINGPOINT_MAX, "fp.max");
219
1509
  addOperator(api::FLOATINGPOINT_LEQ, "fp.leq");
220
1509
  addOperator(api::FLOATINGPOINT_LT, "fp.lt");
221
1509
  addOperator(api::FLOATINGPOINT_GEQ, "fp.geq");
222
1509
  addOperator(api::FLOATINGPOINT_GT, "fp.gt");
223
1509
  addOperator(api::FLOATINGPOINT_ISN, "fp.isNormal");
224
1509
  addOperator(api::FLOATINGPOINT_ISSN, "fp.isSubnormal");
225
1509
  addOperator(api::FLOATINGPOINT_ISZ, "fp.isZero");
226
1509
  addOperator(api::FLOATINGPOINT_ISINF, "fp.isInfinite");
227
1509
  addOperator(api::FLOATINGPOINT_ISNAN, "fp.isNaN");
228
1509
  addOperator(api::FLOATINGPOINT_ISNEG, "fp.isNegative");
229
1509
  addOperator(api::FLOATINGPOINT_ISPOS, "fp.isPositive");
230
1509
  addOperator(api::FLOATINGPOINT_TO_REAL, "fp.to_real");
231
232
1509
  addIndexedOperator(api::FLOATINGPOINT_TO_FP_GENERIC,
233
                     api::FLOATINGPOINT_TO_FP_GENERIC,
234
                     "to_fp");
235
1509
  addIndexedOperator(api::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR,
236
                     api::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR,
237
                     "to_fp_unsigned");
238
1509
  addIndexedOperator(
239
      api::FLOATINGPOINT_TO_UBV, api::FLOATINGPOINT_TO_UBV, "fp.to_ubv");
240
1509
  addIndexedOperator(
241
      api::FLOATINGPOINT_TO_SBV, api::FLOATINGPOINT_TO_SBV, "fp.to_sbv");
242
243
1509
  if (!strictModeEnabled())
244
  {
245
1507
    addIndexedOperator(api::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR,
246
                       api::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR,
247
                       "to_fp_bv");
248
1507
    addIndexedOperator(api::FLOATINGPOINT_TO_FP_FLOATINGPOINT,
249
                       api::FLOATINGPOINT_TO_FP_FLOATINGPOINT,
250
                       "to_fp_fp");
251
1507
    addIndexedOperator(api::FLOATINGPOINT_TO_FP_REAL,
252
                       api::FLOATINGPOINT_TO_FP_REAL,
253
                       "to_fp_real");
254
1507
    addIndexedOperator(api::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR,
255
                       api::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR,
256
                       "to_fp_signed");
257
  }
258
1509
}
259
260
1476
void Smt2::addSepOperators() {
261
1476
  addOperator(api::SEP_STAR, "sep");
262
1476
  addOperator(api::SEP_PTO, "pto");
263
1476
  addOperator(api::SEP_WAND, "wand");
264
1476
  addOperator(api::SEP_EMP, "emp");
265
1476
  Parser::addOperator(api::SEP_STAR);
266
1476
  Parser::addOperator(api::SEP_PTO);
267
1476
  Parser::addOperator(api::SEP_WAND);
268
1476
  Parser::addOperator(api::SEP_EMP);
269
1476
}
270
271
3949
void Smt2::addCoreSymbols()
272
{
273
3949
  defineType("Bool", d_solver->getBooleanSort(), true, true);
274
3949
  defineVar("true", d_solver->mkTrue(), true, true);
275
3949
  defineVar("false", d_solver->mkFalse(), true, true);
276
3949
  addOperator(api::AND, "and");
277
3949
  addOperator(api::DISTINCT, "distinct");
278
3949
  addOperator(api::EQUAL, "=");
279
3949
  addOperator(api::IMPLIES, "=>");
280
3949
  addOperator(api::ITE, "ite");
281
3949
  addOperator(api::NOT, "not");
282
3949
  addOperator(api::OR, "or");
283
3949
  addOperator(api::XOR, "xor");
284
3949
}
285
286
368235
void Smt2::addOperator(api::Kind kind, const std::string& name)
287
{
288
736470
  Debug("parser") << "Smt2::addOperator( " << kind << ", " << name << " )"
289
368235
                  << std::endl;
290
368235
  Parser::addOperator(kind);
291
368235
  operatorKindMap[name] = kind;
292
368235
}
293
294
37852
void Smt2::addIndexedOperator(api::Kind tKind,
295
                              api::Kind opKind,
296
                              const std::string& name)
297
{
298
37852
  Parser::addOperator(tKind);
299
37852
  d_indexedOpKindMap[name] = opKind;
300
37852
}
301
302
1997275
api::Kind Smt2::getOperatorKind(const std::string& name) const
303
{
304
  // precondition: isOperatorEnabled(name)
305
1997275
  return operatorKindMap.find(name)->second;
306
}
307
308
2267115
bool Smt2::isOperatorEnabled(const std::string& name) const {
309
2267115
  return operatorKindMap.find(name) != operatorKindMap.end();
310
}
311
312
19380
bool Smt2::isTheoryEnabled(theory::TheoryId theory) const
313
{
314
19380
  return d_logic.isTheoryEnabled(theory);
315
}
316
317
1998233
bool Smt2::isHoEnabled() const { return d_logic.isHigherOrder(); }
318
319
197775
bool Smt2::logicIsSet() {
320
197775
  return d_logicSet;
321
}
322
323
2606994
api::Term Smt2::getExpressionForNameAndType(const std::string& name,
324
                                            api::Sort t)
325
{
326
2606994
  if (isAbstractValue(name))
327
  {
328
    return mkAbstractValue(name);
329
  }
330
2606994
  return Parser::getExpressionForNameAndType(name, t);
331
}
332
333
1761
bool Smt2::getTesterName(api::Term cons, std::string& name)
334
{
335
1761
  if ((v2_6() || sygus()) && strictModeEnabled())
336
  {
337
    // 2.6 or above uses indexed tester symbols, if we are in strict mode,
338
    // we do not automatically define is-cons for constructor cons.
339
    return false;
340
  }
341
3522
  std::stringstream ss;
342
1761
  ss << "is-" << cons;
343
1761
  name = ss.str();
344
1761
  return true;
345
}
346
347
47855
api::Term Smt2::mkIndexedConstant(const std::string& name,
348
                                  const std::vector<uint64_t>& numerals)
349
{
350
47855
  if (d_logic.isTheoryEnabled(theory::THEORY_FP))
351
  {
352
907
    if (name == "+oo")
353
    {
354
4
      return d_solver->mkPosInf(numerals[0], numerals[1]);
355
    }
356
903
    else if (name == "-oo")
357
    {
358
2
      return d_solver->mkNegInf(numerals[0], numerals[1]);
359
    }
360
901
    else if (name == "NaN")
361
    {
362
4
      return d_solver->mkNaN(numerals[0], numerals[1]);
363
    }
364
897
    else if (name == "+zero")
365
    {
366
3
      return d_solver->mkPosZero(numerals[0], numerals[1]);
367
    }
368
894
    else if (name == "-zero")
369
    {
370
2
      return d_solver->mkNegZero(numerals[0], numerals[1]);
371
    }
372
  }
373
374
47840
  if (d_logic.isTheoryEnabled(theory::THEORY_BV) && name.find("bv") == 0)
375
  {
376
95680
    std::string bvStr = name.substr(2);
377
47840
    return d_solver->mkBitVector(numerals[0], bvStr, 10);
378
  }
379
380
  // NOTE: Theory parametric constants go here
381
382
  parseError(std::string("Unknown indexed literal `") + name + "'");
383
  return api::Term();
384
}
385
386
41580
api::Kind Smt2::getIndexedOpKind(const std::string& name)
387
{
388
41580
  const auto& kIt = d_indexedOpKindMap.find(name);
389
41580
  if (kIt != d_indexedOpKindMap.end())
390
  {
391
41580
    return (*kIt).second;
392
  }
393
  parseError(std::string("Unknown indexed function `") + name + "'");
394
  return api::UNDEFINED_KIND;
395
}
396
397
115
api::Term Smt2::bindDefineFunRec(
398
    const std::string& fname,
399
    const std::vector<std::pair<std::string, api::Sort>>& sortedVarNames,
400
    api::Sort t,
401
    std::vector<api::Term>& flattenVars)
402
{
403
230
  std::vector<api::Sort> sorts;
404
383
  for (const std::pair<std::string, api::Sort>& svn : sortedVarNames)
405
  {
406
268
    sorts.push_back(svn.second);
407
  }
408
409
  // make the flattened function type, add bound variables
410
  // to flattenVars if the defined function was given a function return type.
411
230
  api::Sort ft = mkFlatFunctionType(sorts, t, flattenVars);
412
413
  // allow overloading
414
230
  return bindVar(fname, ft, false, true);
415
}
416
417
115
void Smt2::pushDefineFunRecScope(
418
    const std::vector<std::pair<std::string, api::Sort>>& sortedVarNames,
419
    api::Term func,
420
    const std::vector<api::Term>& flattenVars,
421
    std::vector<api::Term>& bvs)
422
{
423
115
  pushScope();
424
425
  // bound variables are those that are explicitly named in the preamble
426
  // of the define-fun(s)-rec command, we define them here
427
383
  for (const std::pair<std::string, api::Sort>& svn : sortedVarNames)
428
  {
429
536
    api::Term v = bindBoundVar(svn.first, svn.second);
430
268
    bvs.push_back(v);
431
  }
432
433
115
  bvs.insert(bvs.end(), flattenVars.begin(), flattenVars.end());
434
115
}
435
436
25
void Smt2::reset() {
437
25
  d_logicSet = false;
438
25
  d_seenSetLogic = false;
439
25
  d_logic = LogicInfo();
440
25
  operatorKindMap.clear();
441
25
  d_lastNamedTerm = std::pair<api::Term, std::string>();
442
25
}
443
444
14
std::unique_ptr<Command> Smt2::invConstraint(
445
    const std::vector<std::string>& names)
446
{
447
14
  checkThatLogicIsSet();
448
14
  Debug("parser-sygus") << "Sygus : define sygus funs..." << std::endl;
449
14
  Debug("parser-sygus") << "Sygus : read inv-constraint..." << std::endl;
450
451
14
  if (names.size() != 4)
452
  {
453
    parseError(
454
        "Bad syntax for inv-constraint: expected 4 "
455
        "arguments.");
456
  }
457
458
28
  std::vector<api::Term> terms;
459
70
  for (const std::string& name : names)
460
  {
461
56
    if (!isDeclared(name))
462
    {
463
      std::stringstream ss;
464
      ss << "Function " << name << " in inv-constraint is not defined.";
465
      parseError(ss.str());
466
    }
467
468
56
    terms.push_back(getVariable(name));
469
  }
470
471
28
  return std::unique_ptr<Command>(new SygusInvConstraintCommand(terms));
472
}
473
474
3956
Command* Smt2::setLogic(std::string name, bool fromCommand)
475
{
476
3956
  if (fromCommand)
477
  {
478
3890
    if (d_seenSetLogic)
479
    {
480
2
      parseError("Only one set-logic is allowed.");
481
    }
482
3889
    d_seenSetLogic = true;
483
484
3889
    if (logicIsForced())
485
    {
486
      // If the logic is forced, we ignore all set-logic requests from commands.
487
6
      return new EmptyCommand();
488
    }
489
  }
490
491
3949
  d_logicSet = true;
492
3949
  d_logic = name;
493
494
  // if sygus is enabled, we must enable UF, datatypes, and integer arithmetic
495
3949
  if(sygus()) {
496
198
    if (!d_logic.isQuantified())
497
    {
498
      warning("Logics in sygus are assumed to contain quantifiers.");
499
      warning("Omit QF_ from the logic to avoid this warning.");
500
    }
501
  }
502
503
  // Core theory belongs to every logic
504
3949
  addCoreSymbols();
505
506
3949
  if(d_logic.isTheoryEnabled(theory::THEORY_UF)) {
507
2367
    Parser::addOperator(api::APPLY_UF);
508
509
2367
    if (!strictModeEnabled() && d_logic.hasCardinalityConstraints())
510
    {
511
14
      addOperator(api::CARDINALITY_CONSTRAINT, "fmf.card");
512
14
      addOperator(api::CARDINALITY_VALUE, "fmf.card.val");
513
    }
514
  }
515
516
3949
  if(d_logic.isTheoryEnabled(theory::THEORY_ARITH)) {
517
2922
    if(d_logic.areIntegersUsed()) {
518
2603
      defineType("Int", d_solver->getIntegerSort(), true, true);
519
2603
      addArithmeticOperators();
520
2603
      if (!strictModeEnabled() || !d_logic.isLinear())
521
      {
522
2599
        addOperator(api::INTS_DIVISION, "div");
523
2599
        addOperator(api::INTS_MODULUS, "mod");
524
2599
        addOperator(api::ABS, "abs");
525
      }
526
2603
      addIndexedOperator(api::DIVISIBLE, api::DIVISIBLE, "divisible");
527
    }
528
529
2922
    if (d_logic.areRealsUsed())
530
    {
531
1887
      defineType("Real", d_solver->getRealSort(), true, true);
532
1887
      addArithmeticOperators();
533
1887
      addOperator(api::DIVISION, "/");
534
1887
      if (!strictModeEnabled())
535
      {
536
1887
        addOperator(api::ABS, "abs");
537
      }
538
    }
539
540
2922
    if (d_logic.areIntegersUsed() && d_logic.areRealsUsed())
541
    {
542
1568
      addOperator(api::TO_INTEGER, "to_int");
543
1568
      addOperator(api::IS_INTEGER, "is_int");
544
1568
      addOperator(api::TO_REAL, "to_real");
545
    }
546
547
2922
    if (d_logic.areTranscendentalsUsed())
548
    {
549
1516
      defineVar("real.pi", d_solver->mkTerm(api::PI));
550
1516
      addTranscendentalOperators();
551
    }
552
2922
    if (!strictModeEnabled())
553
    {
554
      // integer version of AND
555
2918
      addIndexedOperator(api::IAND, api::IAND, "iand");
556
      // pow2
557
2918
      addOperator(api::POW2, "int.pow2");
558
    }
559
  }
560
561
3949
  if(d_logic.isTheoryEnabled(theory::THEORY_ARRAYS)) {
562
1878
    addOperator(api::SELECT, "select");
563
1878
    addOperator(api::STORE, "store");
564
1878
    addOperator(api::EQ_RANGE, "eqrange");
565
  }
566
567
3949
  if(d_logic.isTheoryEnabled(theory::THEORY_BV)) {
568
2264
    addBitvectorOperators();
569
570
6789
    if (!strictModeEnabled() && d_logic.isTheoryEnabled(theory::THEORY_ARITH)
571
3845
        && d_logic.areIntegersUsed())
572
    {
573
      // Conversions between bit-vectors and integers
574
1573
      addOperator(api::BITVECTOR_TO_NAT, "bv2nat");
575
1573
      addIndexedOperator(
576
          api::INT_TO_BITVECTOR, api::INT_TO_BITVECTOR, "int2bv");
577
    }
578
  }
579
580
3949
  if(d_logic.isTheoryEnabled(theory::THEORY_DATATYPES)) {
581
3028
    const std::vector<api::Sort> types;
582
1514
    defineType("Tuple", d_solver->mkTupleSort(types), true, true);
583
1514
    addDatatypesOperators();
584
  }
585
586
3949
  if(d_logic.isTheoryEnabled(theory::THEORY_SETS)) {
587
1531
    defineVar("emptyset", d_solver->mkEmptySet(d_solver->getNullSort()));
588
    // the Boolean sort is a placeholder here since we don't have type info
589
    // without type annotation
590
1531
    defineVar("univset", d_solver->mkUniverseSet(d_solver->getBooleanSort()));
591
592
1531
    addOperator(api::UNION, "union");
593
1531
    addOperator(api::INTERSECTION, "intersection");
594
1531
    addOperator(api::SETMINUS, "setminus");
595
1531
    addOperator(api::SUBSET, "subset");
596
1531
    addOperator(api::MEMBER, "member");
597
1531
    addOperator(api::SINGLETON, "singleton");
598
1531
    addOperator(api::INSERT, "insert");
599
1531
    addOperator(api::CARD, "card");
600
1531
    addOperator(api::COMPLEMENT, "complement");
601
1531
    addOperator(api::CHOOSE, "choose");
602
1531
    addOperator(api::IS_SINGLETON, "is_singleton");
603
1531
    addOperator(api::JOIN, "join");
604
1531
    addOperator(api::PRODUCT, "product");
605
1531
    addOperator(api::TRANSPOSE, "transpose");
606
1531
    addOperator(api::TCLOSURE, "tclosure");
607
1531
    addOperator(api::JOIN_IMAGE, "join_image");
608
1531
    addOperator(api::IDEN, "iden");
609
  }
610
611
3949
  if (d_logic.isTheoryEnabled(theory::THEORY_BAGS))
612
  {
613
1474
    defineVar("emptybag", d_solver->mkEmptyBag(d_solver->getNullSort()));
614
1474
    addOperator(api::UNION_MAX, "union_max");
615
1474
    addOperator(api::UNION_DISJOINT, "union_disjoint");
616
1474
    addOperator(api::INTERSECTION_MIN, "intersection_min");
617
1474
    addOperator(api::DIFFERENCE_SUBTRACT, "difference_subtract");
618
1474
    addOperator(api::DIFFERENCE_REMOVE, "difference_remove");
619
1474
    addOperator(api::SUBBAG, "subbag");
620
1474
    addOperator(api::BAG_COUNT, "bag.count");
621
1474
    addOperator(api::DUPLICATE_REMOVAL, "duplicate_removal");
622
1474
    addOperator(api::MK_BAG, "bag");
623
1474
    addOperator(api::BAG_CARD, "bag.card");
624
1474
    addOperator(api::BAG_CHOOSE, "bag.choose");
625
1474
    addOperator(api::BAG_IS_SINGLETON, "bag.is_singleton");
626
1474
    addOperator(api::BAG_FROM_SET, "bag.from_set");
627
1474
    addOperator(api::BAG_TO_SET, "bag.to_set");
628
1474
    addOperator(api::BAG_MAP, "bag.map");
629
  }
630
3949
  if(d_logic.isTheoryEnabled(theory::THEORY_STRINGS)) {
631
1798
    defineType("String", d_solver->getStringSort(), true, true);
632
1798
    defineType("RegLan", d_solver->getRegExpSort(), true, true);
633
1798
    defineType("Int", d_solver->getIntegerSort(), true, true);
634
635
1798
    defineVar("re.none", d_solver->mkRegexpEmpty());
636
1798
    defineVar("re.allchar", d_solver->mkRegexpSigma());
637
638
    // Boolean is a placeholder
639
3596
    defineVar("seq.empty",
640
3596
              d_solver->mkEmptySequence(d_solver->getBooleanSort()));
641
642
1798
    addStringOperators();
643
  }
644
645
3949
  if(d_logic.isQuantified()) {
646
2013
    addQuantifiersOperators();
647
  }
648
649
3949
  if (d_logic.isTheoryEnabled(theory::THEORY_FP)) {
650
1509
    defineType("RoundingMode", d_solver->getRoundingModeSort(), true, true);
651
1509
    defineType("Float16", d_solver->mkFloatingPointSort(5, 11), true, true);
652
1509
    defineType("Float32", d_solver->mkFloatingPointSort(8, 24), true, true);
653
1509
    defineType("Float64", d_solver->mkFloatingPointSort(11, 53), true, true);
654
1509
    defineType("Float128", d_solver->mkFloatingPointSort(15, 113), true, true);
655
656
1509
    defineVar("RNE", d_solver->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_EVEN));
657
3018
    defineVar("roundNearestTiesToEven",
658
3018
              d_solver->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_EVEN));
659
1509
    defineVar("RNA", d_solver->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_AWAY));
660
3018
    defineVar("roundNearestTiesToAway",
661
3018
              d_solver->mkRoundingMode(api::ROUND_NEAREST_TIES_TO_AWAY));
662
1509
    defineVar("RTP", d_solver->mkRoundingMode(api::ROUND_TOWARD_POSITIVE));
663
3018
    defineVar("roundTowardPositive",
664
3018
              d_solver->mkRoundingMode(api::ROUND_TOWARD_POSITIVE));
665
1509
    defineVar("RTN", d_solver->mkRoundingMode(api::ROUND_TOWARD_NEGATIVE));
666
3018
    defineVar("roundTowardNegative",
667
3018
              d_solver->mkRoundingMode(api::ROUND_TOWARD_NEGATIVE));
668
1509
    defineVar("RTZ", d_solver->mkRoundingMode(api::ROUND_TOWARD_ZERO));
669
3018
    defineVar("roundTowardZero",
670
3018
              d_solver->mkRoundingMode(api::ROUND_TOWARD_ZERO));
671
672
1509
    addFloatingPointOperators();
673
  }
674
675
3949
  if (d_logic.isTheoryEnabled(theory::THEORY_SEP)) {
676
    // the Boolean sort is a placeholder here since we don't have type info
677
    // without type annotation
678
1476
    defineVar("sep.nil", d_solver->mkSepNil(d_solver->getBooleanSort()));
679
680
1476
    addSepOperators();
681
  }
682
683
7898
  std::string logic = sygus() ? d_logic.getLogicString() : name;
684
3949
  if (!fromCommand)
685
  {
686
    // If not from a command, just set the logic directly. Notice this is
687
    // important since we do not want to enqueue a set-logic command and
688
    // fully initialize the underlying SmtEngine in the meantime before the
689
    // command has a chance to execute, which would lead to an error.
690
66
    d_solver->setLogic(logic);
691
66
    return nullptr;
692
  }
693
3883
  Command* cmd = new SetBenchmarkLogicCommand(logic);
694
3883
  return cmd;
695
} /* Smt2::setLogic() */
696
697
152
api::Grammar* Smt2::mkGrammar(const std::vector<api::Term>& boundVars,
698
                              const std::vector<api::Term>& ntSymbols)
699
{
700
152
  d_allocGrammars.emplace_back(
701
304
      new api::Grammar(d_solver->mkSygusGrammar(boundVars, ntSymbols)));
702
152
  return d_allocGrammars.back().get();
703
}
704
705
196996
bool Smt2::sygus() const
706
{
707
196996
  return d_solver->getOption("input-language") == "LANG_SYGUS_V2";
708
}
709
710
197775
void Smt2::checkThatLogicIsSet()
711
{
712
197775
  if (!logicIsSet())
713
  {
714
70
    if (strictModeEnabled())
715
    {
716
8
      parseError("set-logic must appear before this point.");
717
    }
718
    else
719
    {
720
      // the calls to setLogic below set the logic on the solver directly
721
66
      if (logicIsForced())
722
      {
723
9
        setLogic(getForcedLogic(), false);
724
      }
725
      else
726
      {
727
57
        warning("No set-logic command was given before this point.");
728
57
        warning("cvc5 will make all theories available.");
729
57
        warning(
730
            "Consider setting a stricter logic for (likely) better "
731
            "performance.");
732
57
        warning("To suppress this warning in the future use (set-logic ALL).");
733
734
57
        setLogic("ALL", false);
735
      }
736
    }
737
  }
738
197771
}
739
740
1596
void Smt2::checkLogicAllowsFreeSorts()
741
{
742
3192
  if (!d_logic.isTheoryEnabled(theory::THEORY_UF)
743
18
      && !d_logic.isTheoryEnabled(theory::THEORY_ARRAYS)
744
      && !d_logic.isTheoryEnabled(theory::THEORY_DATATYPES)
745
      && !d_logic.isTheoryEnabled(theory::THEORY_SETS)
746
1596
      && !d_logic.isTheoryEnabled(theory::THEORY_BAGS))
747
  {
748
    parseErrorLogic("Free sort symbols not allowed in ");
749
  }
750
1596
}
751
752
6354
void Smt2::checkLogicAllowsFunctions()
753
{
754
6354
  if (!d_logic.isTheoryEnabled(theory::THEORY_UF) && !isHoEnabled())
755
  {
756
    parseError(
757
        "Functions (of non-zero arity) cannot "
758
        "be declared in logic "
759
        + d_logic.getLogicString()
760
        + ". Try including UF or adding the prefix HO_.");
761
  }
762
6354
}
763
764
/* The include are managed in the lexer but called in the parser */
765
// Inspired by http://www.antlr3.org/api/C/interop.html
766
767
static bool newInputStream(const std::string& filename, pANTLR3_LEXER lexer) {
768
  Debug("parser") << "Including " << filename << std::endl;
769
  // Create a new input stream and take advantage of built in stream stacking
770
  // in C target runtime.
771
  //
772
  pANTLR3_INPUT_STREAM    in;
773
#ifdef CVC5_ANTLR3_OLD_INPUT_STREAM
774
  in = antlr3AsciiFileStreamNew((pANTLR3_UINT8) filename.c_str());
775
#else  /* CVC5_ANTLR3_OLD_INPUT_STREAM */
776
  in = antlr3FileStreamNew((pANTLR3_UINT8) filename.c_str(), ANTLR3_ENC_8BIT);
777
#endif /* CVC5_ANTLR3_OLD_INPUT_STREAM */
778
  if( in == NULL ) {
779
    Debug("parser") << "Can't open " << filename << std::endl;
780
    return false;
781
  }
782
  // Same thing as the predefined PUSHSTREAM(in);
783
  lexer->pushCharStream(lexer, in);
784
  // restart it
785
  //lexer->rec->state->tokenStartCharIndex      = -10;
786
  //lexer->emit(lexer);
787
788
  // Note that the input stream is not closed when it EOFs, I don't bother
789
  // to do it here, but it is up to you to track streams created like this
790
  // and destroy them when the whole parse session is complete. Remember that you
791
  // don't want to do this until all tokens have been manipulated all the way through
792
  // your tree parsers etc as the token does not store the text it just refers
793
  // back to the input stream and trying to get the text for it will abort if you
794
  // close the input stream too early.
795
796
  //TODO what said before
797
  return true;
798
}
799
800
void Smt2::includeFile(const std::string& filename) {
801
  // security for online version
802
  if(!canIncludeFile()) {
803
    parseError("include-file feature was disabled for this run.");
804
  }
805
806
  // Get the lexer
807
  AntlrInput* ai = static_cast<AntlrInput*>(getInput());
808
  pANTLR3_LEXER lexer = ai->getAntlr3Lexer();
809
  // get the name of the current stream "Does it work inside an include?"
810
  const std::string inputName = ai->getInputStreamName();
811
812
  // Find the directory of the current input file
813
  std::string path;
814
  size_t pos = inputName.rfind('/');
815
  if(pos != std::string::npos) {
816
    path = std::string(inputName, 0, pos + 1);
817
  }
818
  path.append(filename);
819
  if(!newInputStream(path, lexer)) {
820
    parseError("Couldn't open include file `" + path + "'");
821
  }
822
}
823
7775455
bool Smt2::isAbstractValue(const std::string& name)
824
{
825
15169927
  return name.length() >= 2 && name[0] == '@' && name[1] != '0'
826
7775461
         && name.find_first_not_of("0123456789", 1) == std::string::npos;
827
}
828
829
api::Term Smt2::mkAbstractValue(const std::string& name)
830
{
831
  Assert(isAbstractValue(name));
832
  // remove the '@'
833
  return d_solver->mkAbstractValue(name.substr(1));
834
}
835
836
247
void Smt2::parseOpApplyTypeAscription(ParseOp& p, api::Sort type)
837
{
838
494
  Debug("parser") << "parseOpApplyTypeAscription : " << p << " " << type
839
247
                  << std::endl;
840
  // (as const (Array T1 T2))
841
247
  if (p.d_kind == api::CONST_ARRAY)
842
  {
843
40
    if (!type.isArray())
844
    {
845
      std::stringstream ss;
846
      ss << "expected array constant term, but cast is not of array type"
847
         << std::endl
848
         << "cast type: " << type;
849
      parseError(ss.str());
850
    }
851
40
    p.d_type = type;
852
40
    return;
853
  }
854
207
  if (p.d_expr.isNull())
855
  {
856
414
    Trace("parser-overloading")
857
207
        << "Getting variable expression with name " << p.d_name << " and type "
858
207
        << type << std::endl;
859
    // get the variable expression for the type
860
207
    if (isDeclared(p.d_name, SYM_VARIABLE))
861
    {
862
207
      p.d_expr = getExpressionForNameAndType(p.d_name, type);
863
207
      p.d_name = std::string("");
864
    }
865
207
    if (p.d_expr.isNull())
866
    {
867
      std::stringstream ss;
868
      ss << "Could not resolve expression with name " << p.d_name
869
         << " and type " << type << std::endl;
870
      parseError(ss.str());
871
    }
872
  }
873
207
  Trace("parser-qid") << "Resolve ascription " << type << " on " << p.d_expr;
874
207
  Trace("parser-qid") << " " << p.d_expr.getKind() << " " << p.d_expr.getSort();
875
207
  Trace("parser-qid") << std::endl;
876
  // otherwise, we process the type ascription
877
207
  p.d_expr = applyTypeAscription(p.d_expr, type);
878
}
879
880
2606995
api::Term Smt2::parseOpToExpr(ParseOp& p)
881
{
882
2606995
  Debug("parser") << "parseOpToExpr: " << p << std::endl;
883
2606995
  api::Term expr;
884
2606995
  if (p.d_kind != api::NULL_EXPR || !p.d_type.isNull())
885
  {
886
    parseError(
887
        "Bad syntax for qualified identifier operator in term position.");
888
  }
889
2606995
  else if (!p.d_expr.isNull())
890
  {
891
199
    expr = p.d_expr;
892
  }
893
2606796
  else if (!isDeclared(p.d_name, SYM_VARIABLE))
894
  {
895
18
    std::stringstream ss;
896
9
    ss << "Symbol " << p.d_name << " is not declared.";
897
18
    parseError(ss.str());
898
  }
899
  else
900
  {
901
2606787
    expr = getExpressionForName(p.d_name);
902
  }
903
2606986
  Assert(!expr.isNull());
904
2606986
  return expr;
905
}
906
907
2170767
api::Term Smt2::applyParseOp(ParseOp& p, std::vector<api::Term>& args)
908
{
909
2170767
  bool isBuiltinOperator = false;
910
  // the builtin kind of the overall return expression
911
2170767
  api::Kind kind = api::NULL_EXPR;
912
  // First phase: process the operator
913
2170767
  if (Debug.isOn("parser"))
914
  {
915
    Debug("parser") << "applyParseOp: " << p << " to:" << std::endl;
916
    for (std::vector<api::Term>::iterator i = args.begin(); i != args.end();
917
         ++i)
918
    {
919
      Debug("parser") << "++ " << *i << std::endl;
920
    }
921
  }
922
4341534
  api::Op op;
923
2170767
  if (p.d_kind != api::NULL_EXPR)
924
  {
925
    // It is a special case, e.g. tupSel or array constant specification.
926
    // We have to wait until the arguments are parsed to resolve it.
927
  }
928
2170688
  else if (!p.d_expr.isNull())
929
  {
930
    // An explicit operator, e.g. an apply function
931
866
    api::Kind fkind = getKindForFunction(p.d_expr);
932
866
    if (fkind != api::UNDEFINED_KIND)
933
    {
934
      // Some operators may require a specific kind.
935
      // Testers are handled differently than other indexed operators,
936
      // since they require a kind.
937
866
      kind = fkind;
938
1732
      Debug("parser") << "Got function kind " << kind << " for expression "
939
866
                      << std::endl;
940
    }
941
866
    args.insert(args.begin(), p.d_expr);
942
  }
943
2169822
  else if (!p.d_op.isNull())
944
  {
945
    // it was given an operator
946
41573
    op = p.d_op;
947
  }
948
  else
949
  {
950
2128249
    isBuiltinOperator = isOperatorEnabled(p.d_name);
951
2128249
    if (isBuiltinOperator)
952
    {
953
      // a builtin operator, convert to kind
954
1997275
      kind = getOperatorKind(p.d_name);
955
    }
956
    else
957
    {
958
      // A non-built-in function application, get the expression
959
130996
      checkDeclaration(p.d_name, CHECK_DECLARED, SYM_VARIABLE);
960
261904
      api::Term v = getVariable(p.d_name);
961
130952
      if (!v.isNull())
962
      {
963
130884
        checkFunctionLike(v);
964
130880
        kind = getKindForFunction(v);
965
130880
        args.insert(args.begin(), v);
966
      }
967
      else
968
      {
969
        // Overloaded symbol?
970
        // Could not find the expression. It may be an overloaded symbol,
971
        // in which case we may find it after knowing the types of its
972
        // arguments.
973
140
        std::vector<api::Sort> argTypes;
974
160
        for (std::vector<api::Term>::iterator i = args.begin(); i != args.end();
975
             ++i)
976
        {
977
90
          argTypes.push_back((*i).getSort());
978
        }
979
140
        api::Term fop = getOverloadedFunctionForTypes(p.d_name, argTypes);
980
70
        if (!fop.isNull())
981
        {
982
70
          checkFunctionLike(fop);
983
70
          kind = getKindForFunction(fop);
984
70
          args.insert(args.begin(), fop);
985
        }
986
        else
987
        {
988
          parseError(
989
              "Cannot find unambiguous overloaded function for argument "
990
              "types.");
991
        }
992
      }
993
    }
994
  }
995
  // handle special cases
996
2170743
  if (p.d_kind == api::CONST_ARRAY && !p.d_type.isNull())
997
  {
998
40
    if (args.size() != 1)
999
    {
1000
      parseError("Too many arguments to array constant.");
1001
    }
1002
80
    api::Term constVal = args[0];
1003
1004
    // To parse array constants taking reals whose values are specified by
1005
    // rationals, e.g. ((as const (Array Int Real)) (/ 1 3)), we must handle
1006
    // the fact that (/ 1 3) is the division of constants 1 and 3, and not
1007
    // the resulting constant rational value. Thus, we must construct the
1008
    // resulting rational here. This also is applied for integral real values
1009
    // like 5.0 which are converted to (/ 5 1) to distinguish them from
1010
    // integer constants. We must ensure numerator and denominator are
1011
    // constant and the denominator is non-zero.
1012
40
    if (constVal.getKind() == api::DIVISION)
1013
    {
1014
4
      std::stringstream sdiv;
1015
2
      sdiv << constVal[0] << "/" << constVal[1];
1016
2
      constVal = d_solver->mkReal(sdiv.str());
1017
    }
1018
1019
40
    if (!p.d_type.getArrayElementSort().isComparableTo(constVal.getSort()))
1020
    {
1021
      std::stringstream ss;
1022
      ss << "type mismatch inside array constant term:" << std::endl
1023
         << "array type:          " << p.d_type << std::endl
1024
         << "expected const type: " << p.d_type.getArrayElementSort()
1025
         << std::endl
1026
         << "computed const type: " << constVal.getSort();
1027
      parseError(ss.str());
1028
    }
1029
80
    api::Term ret = d_solver->mkConstArray(p.d_type, constVal);
1030
40
    Debug("parser") << "applyParseOp: return store all " << ret << std::endl;
1031
40
    return ret;
1032
  }
1033
6512078
  else if ((p.d_kind == api::APPLY_SELECTOR || p.d_kind == api::APPLY_UPDATER)
1034
2170740
           && !p.d_expr.isNull())
1035
  {
1036
    // tuple selector case
1037
37
    if (!p.d_expr.isUInt64Value())
1038
    {
1039
      parseError(
1040
          "index of tuple select or update is larger than size of uint64_t");
1041
    }
1042
37
    uint64_t n = p.d_expr.getUInt64Value();
1043
37
    if (args.size() != (p.d_kind == api::APPLY_SELECTOR ? 1 : 2))
1044
    {
1045
      parseError("wrong number of arguments for tuple select or update");
1046
    }
1047
74
    api::Sort t = args[0].getSort();
1048
37
    if (!t.isTuple())
1049
    {
1050
      parseError("tuple select or update applied to non-tuple");
1051
    }
1052
37
    size_t length = t.getTupleLength();
1053
37
    if (n >= length)
1054
    {
1055
      std::stringstream ss;
1056
      ss << "tuple is of length " << length << "; cannot access index " << n;
1057
      parseError(ss.str());
1058
    }
1059
74
    const api::Datatype& dt = t.getDatatype();
1060
74
    api::Term ret;
1061
37
    if (p.d_kind == api::APPLY_SELECTOR)
1062
    {
1063
62
      ret = d_solver->mkTerm(
1064
62
          api::APPLY_SELECTOR, dt[0][n].getSelectorTerm(), args[0]);
1065
    }
1066
    else
1067
    {
1068
12
      ret = d_solver->mkTerm(
1069
12
          api::APPLY_UPDATER, dt[0][n].getUpdaterTerm(), args[0], args[1]);
1070
    }
1071
37
    Debug("parser") << "applyParseOp: return selector " << ret << std::endl;
1072
37
    return ret;
1073
  }
1074
2170666
  else if (p.d_kind == api::TUPLE_PROJECT)
1075
  {
1076
4
    api::Term ret = d_solver->mkTerm(p.d_op, args[0]);
1077
2
    Debug("parser") << "applyParseOp: return projection " << ret << std::endl;
1078
2
    return ret;
1079
  }
1080
2170664
  else if (p.d_kind != api::NULL_EXPR)
1081
  {
1082
    // it should not have an expression or type specified at this point
1083
    if (!p.d_expr.isNull() || !p.d_type.isNull())
1084
    {
1085
      std::stringstream ss;
1086
      ss << "Could not process parsed qualified identifier kind " << p.d_kind;
1087
      parseError(ss.str());
1088
    }
1089
    // otherwise it is a simple application
1090
    kind = p.d_kind;
1091
  }
1092
2170664
  else if (isBuiltinOperator)
1093
  {
1094
1997275
    if (!isHoEnabled() && (kind == api::EQUAL || kind == api::DISTINCT))
1095
    {
1096
      // need hol if these operators are applied over function args
1097
386136
      for (std::vector<api::Term>::iterator i = args.begin(); i != args.end();
1098
           ++i)
1099
      {
1100
258711
        if ((*i).getSort().isFunction())
1101
        {
1102
          parseError(
1103
              "Cannot apply equality to functions unless logic is prefixed by "
1104
              "HO_.");
1105
        }
1106
      }
1107
    }
1108
5991790
    if (!strictModeEnabled() && (kind == api::AND || kind == api::OR)
1109
2652370
        && args.size() == 1)
1110
    {
1111
      // Unary AND/OR can be replaced with the argument.
1112
54
      Debug("parser") << "applyParseOp: return unary " << args[0] << std::endl;
1113
54
      return args[0];
1114
    }
1115
1997221
    else if (kind == api::MINUS && args.size() == 1)
1116
    {
1117
118180
      api::Term ret = d_solver->mkTerm(api::UMINUS, args[0]);
1118
59090
      Debug("parser") << "applyParseOp: return uminus " << ret << std::endl;
1119
59090
      return ret;
1120
    }
1121
1938131
    if (kind == api::EQ_RANGE && d_solver->getOption("arrays-exp") != "true")
1122
    {
1123
      parseError(
1124
          "eqrange predicate requires option --arrays-exp to be enabled.");
1125
    }
1126
1938131
    if (kind == api::SINGLETON && args.size() == 1)
1127
    {
1128
580
      api::Term ret = d_solver->mkTerm(api::SINGLETON, args[0]);
1129
290
      Debug("parser") << "applyParseOp: return singleton " << ret << std::endl;
1130
290
      return ret;
1131
    }
1132
3875682
    api::Term ret = d_solver->mkTerm(kind, args);
1133
3875682
    Debug("parser") << "applyParseOp: return default builtin " << ret
1134
1937841
                    << std::endl;
1135
1937841
    return ret;
1136
  }
1137
1138
173389
  if (args.size() >= 2)
1139
  {
1140
    // may be partially applied function, in this case we use HO_APPLY
1141
263611
    api::Sort argt = args[0].getSort();
1142
131884
    if (argt.isFunction())
1143
    {
1144
122764
      unsigned arity = argt.getFunctionArity();
1145
122764
      if (args.size() - 1 < arity)
1146
      {
1147
157
        if (!isHoEnabled())
1148
        {
1149
          parseError(
1150
              "Cannot partially apply functions unless logic is prefixed by "
1151
              "HO_.");
1152
        }
1153
157
        Debug("parser") << "Partial application of " << args[0];
1154
157
        Debug("parser") << " : #argTypes = " << arity;
1155
157
        Debug("parser") << ", #args = " << args.size() - 1 << std::endl;
1156
314
        api::Term ret = d_solver->mkTerm(api::HO_APPLY, args);
1157
314
        Debug("parser") << "applyParseOp: return curry higher order " << ret
1158
157
                        << std::endl;
1159
        // must curry the partial application
1160
157
        return ret;
1161
      }
1162
    }
1163
  }
1164
173232
  if (!op.isNull())
1165
  {
1166
83145
    api::Term ret = d_solver->mkTerm(op, args);
1167
41572
    Debug("parser") << "applyParseOp: return op : " << ret << std::endl;
1168
41572
    return ret;
1169
  }
1170
131659
  if (kind == api::NULL_EXPR)
1171
  {
1172
    // should never happen in the new API
1173
    parseError("do not know how to process parse op");
1174
  }
1175
263318
  Debug("parser") << "Try default term construction for kind " << kind
1176
131659
                  << " #args = " << args.size() << "..." << std::endl;
1177
263317
  api::Term ret = d_solver->mkTerm(kind, args);
1178
131658
  Debug("parser") << "applyParseOp: return : " << ret << std::endl;
1179
131658
  return ret;
1180
}
1181
1182
1601
void Smt2::notifyNamedExpression(api::Term& expr, std::string name)
1183
{
1184
1601
  checkUserSymbol(name);
1185
  // remember the expression name in the symbol manager
1186
3202
  if (getSymbolManager()->setExpressionName(expr, name, false)
1187
1601
      == NamingResult::ERROR_IN_BINDER)
1188
  {
1189
2
    parseError(
1190
        "Cannot name a term in a binder (e.g., quantifiers, definitions)");
1191
  }
1192
  // define the variable
1193
1600
  defineVar(name, expr);
1194
  // set the last named term, which ensures that we catch when assertions are
1195
  // named
1196
1600
  setLastNamedTerm(expr, name);
1197
1600
}
1198
1199
api::Term Smt2::mkAnd(const std::vector<api::Term>& es)
1200
{
1201
  if (es.size() == 0)
1202
  {
1203
    return d_solver->mkTrue();
1204
  }
1205
  else if (es.size() == 1)
1206
  {
1207
    return es[0];
1208
  }
1209
  else
1210
  {
1211
    return d_solver->mkTerm(api::AND, es);
1212
  }
1213
}
1214
1215
}  // namespace parser
1216
22731
}  // namespace cvc5