1 /********************* */
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Morgan Deters, Christopher L. Conway
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** \brief Definitions of SMT2 constants.
14 ** Definitions of SMT2 constants.
17 #include "cvc4parser_private.h"
19 #ifndef CVC4__PARSER__SMT2_H
20 #define CVC4__PARSER__SMT2_H
25 #include <unordered_map>
28 #include "api/cvc4cpp.h"
29 #include "parser/parse_op.h"
30 #include "parser/parser.h"
31 #include "smt/command.h"
32 #include "theory/logic_info.h"
33 #include "util/abstract_value.h"
45 class Smt2
: public Parser
47 friend class ParserBuilder
;
58 THEORY_TRANSCENDENTALS
,
69 /** Has the logic been set (either by forcing it or a set-logic command)? */
71 /** Have we seen a set-logic command yet? */
75 std::unordered_map
<std::string
, Kind
> operatorKindMap
;
77 * Maps indexed symbols to the kind of the operator (e.g. "extract" to
80 std::unordered_map
<std::string
, api::Kind
> d_indexedOpKindMap
;
81 std::pair
<Expr
, std::string
> d_lastNamedTerm
;
83 std::vector
<Expr
> d_sygusVars
, d_sygusVarPrimed
, d_sygusConstraints
,
87 Smt2(api::Solver
* solver
,
89 bool strictMode
= false,
90 bool parseOnly
= false);
94 * Add theory symbols to the parser state.
96 * @param theory the theory to open (e.g., Core, Ints)
98 void addTheory(Theory theory
);
100 void addOperator(Kind k
, const std::string
& name
);
103 * Registers an indexed function symbol.
105 * @param tKind The kind of the term that uses the operator kind (e.g.
106 * BITVECTOR_EXTRACT). NOTE: this is an internal kind for now
107 * because that is what we use to create expressions. Eventually
108 * it will be an api::Kind.
109 * @param opKind The kind of the operator term (e.g. BITVECTOR_EXTRACT)
110 * @param name The name of the symbol (e.g. "extract")
112 void addIndexedOperator(Kind tKind
,
114 const std::string
& name
);
116 Kind
getOperatorKind(const std::string
& name
) const;
118 bool isOperatorEnabled(const std::string
& name
) const;
120 bool isTheoryEnabled(Theory theory
) const;
122 bool logicIsSet() override
;
125 * Creates an indexed constant, e.g. (_ +oo 8 24) (positive infinity
126 * as a 32-bit floating-point constant).
128 * @param name The name of the constant (e.g. "+oo")
129 * @param numerals The parameters for the constant (e.g. [8, 24])
130 * @return The term corresponding to the constant or a parse error if name is
133 api::Term
mkIndexedConstant(const std::string
& name
,
134 const std::vector
<uint64_t>& numerals
);
137 * Creates an indexed operator term, e.g. (_ extract 5 0).
139 * @param name The name of the operator (e.g. "extract")
140 * @param numerals The parameters for the operator (e.g. [5, 0])
141 * @return The operator term corresponding to the indexed operator or a parse
142 * error if the name is not valid.
144 api::Op
mkIndexedOp(const std::string
& name
,
145 const std::vector
<uint64_t>& numerals
);
148 * Returns the expression that name should be interpreted as.
150 Expr
getExpressionForNameAndType(const std::string
& name
, Type t
) override
;
152 /** Make function defined by a define-fun(s)-rec command.
154 * fname : the name of the function.
155 * sortedVarNames : the list of variable arguments for the function.
156 * t : the range type of the function we are defining.
158 * This function will create a bind a new function term to name fname.
159 * The type of this function is
160 * Parser::mkFlatFunctionType(sorts,t,flattenVars),
161 * where sorts are the types in the second components of sortedVarNames.
162 * As descibed in Parser::mkFlatFunctionType, new bound variables may be
163 * added to flattenVars in this function if the function is given a function
167 const std::string
& fname
,
168 const std::vector
<std::pair
<std::string
, Type
> >& sortedVarNames
,
170 std::vector
<Expr
>& flattenVars
);
172 /** Push scope for define-fun-rec
174 * This calls Parser::pushScope(bindingLevel) and sets up
175 * initial information for reading a body of a function definition
176 * in the define-fun-rec and define-funs-rec command.
177 * The input parameters func/flattenVars are the result
178 * of a call to mkDefineRec above.
180 * func : the function whose body we are defining.
181 * sortedVarNames : the list of variable arguments for the function.
182 * flattenVars : the implicit variables introduced when defining func.
185 * (1) Calls Parser::pushScope(bindingLevel).
186 * (2) Computes the bound variable list for the quantified formula
187 * that defined this definition and stores it in bvs.
189 void pushDefineFunRecScope(
190 const std::vector
<std::pair
<std::string
, Type
> >& sortedVarNames
,
192 const std::vector
<Expr
>& flattenVars
,
193 std::vector
<Expr
>& bvs
,
194 bool bindingLevel
= false);
196 void reset() override
;
198 void resetAssertions();
201 * Class for creating instances of `SynthFunCommand`s. Creating an instance
202 * of this class pushes the scope, destroying it pops the scope.
204 class SynthFunFactory
208 * Creates an instance of `SynthFunFactory`.
210 * @param smt2 Pointer to the parser state
211 * @param fun Name of the function to synthesize
212 * @param isInv True if the goal is to synthesize an invariant, false
214 * @param range The return type of the function-to-synthesize
215 * @param sortedVarNames The parameters of the function-to-synthesize
219 const std::string
& fun
,
222 std::vector
<std::pair
<std::string
, CVC4::Type
>>& sortedVarNames
);
225 const std::vector
<Expr
>& getSygusVars() const { return d_sygusVars
; }
228 * Create an instance of `SynthFunCommand`.
230 * @param grammar Optional grammar associated with the synth-fun command
231 * @return The instance of `SynthFunCommand`
233 std::unique_ptr
<Command
> mkCommand(Type grammar
);
241 std::vector
<Expr
> d_sygusVars
;
245 * Creates a command that adds an invariant constraint.
247 * @param names Name of four symbols corresponding to the
248 * function-to-synthesize, precondition, postcondition,
249 * transition relation.
250 * @return The command that adds an invariant constraint
252 std::unique_ptr
<Command
> invConstraint(const std::vector
<std::string
>& names
);
255 * Sets the logic for the current benchmark. Declares any logic and
258 * @param name the name of the logic (e.g., QF_UF, AUFLIA)
259 * @param fromCommand should be set to true if the request originates from a
260 * set-logic command and false otherwise
261 * @return the command corresponding to setting the logic
263 Command
* setLogic(std::string name
, bool fromCommand
= true);
268 const LogicInfo
& getLogic() const { return d_logic
; }
272 return getLanguage() == language::input::LANG_SMTLIB_V2_0
;
275 * Are we using smtlib 2.5 or above? If exact=true, then this method returns
276 * false if the input language is not exactly SMT-LIB 2.5.
278 bool v2_5(bool exact
= false) const
280 return language::isInputLang_smt2_5(getLanguage(), exact
);
283 * Are we using smtlib 2.6 or above? If exact=true, then this method returns
284 * false if the input language is not exactly SMT-LIB 2.6.
286 bool v2_6(bool exact
= false) const
288 return language::isInputLang_smt2_6(getLanguage(), exact
);
290 /** Are we using a sygus language? */
292 /** Are we using the sygus version 1.0 format? */
293 bool sygus_v1() const;
296 * Returns true if the language that we are parsing (SMT-LIB version >=2.5
297 * and SyGuS) treats duplicate double quotes ("") as an escape sequence
298 * denoting a single double quote (").
300 bool escapeDupDblQuote() const { return v2_5() || sygus(); }
302 void setInfo(const std::string
& flag
, const SExpr
& sexpr
);
304 void setOption(const std::string
& flag
, const SExpr
& sexpr
);
306 void checkThatLogicIsSet();
308 void checkUserSymbol(const std::string
& name
) {
309 if(name
.length() > 0 && (name
[0] == '.' || name
[0] == '@')) {
310 std::stringstream ss
;
311 ss
<< "cannot declare or define symbol `" << name
<< "'; symbols starting with . and @ are reserved in SMT-LIB";
312 parseError(ss
.str());
314 else if (isOperatorEnabled(name
))
316 std::stringstream ss
;
317 ss
<< "Symbol `" << name
<< "' is shadowing a theory function symbol";
318 parseError(ss
.str());
322 void includeFile(const std::string
& filename
);
324 void setLastNamedTerm(Expr e
, std::string name
) {
325 d_lastNamedTerm
= std::make_pair(e
, name
);
328 void clearLastNamedTerm() {
329 d_lastNamedTerm
= std::make_pair(Expr(), "");
332 std::pair
<Expr
, std::string
> lastNamedTerm() {
333 return d_lastNamedTerm
;
336 /** Does name denote an abstract value? (of the form '@n' for numeral n). */
337 bool isAbstractValue(const std::string
& name
);
339 /** Make abstract value
341 * Abstract values are used for processing get-value calls. The argument
342 * name should be such that isAbstractValue(name) is true.
344 Expr
mkAbstractValue(const std::string
& name
);
346 void mkSygusConstantsForType( const Type
& type
, std::vector
<CVC4::Expr
>& ops
);
348 void processSygusGTerm(
349 CVC4::SygusGTerm
& sgt
,
351 std::vector
<CVC4::Datatype
>& datatypes
,
352 std::vector
<CVC4::Type
>& sorts
,
353 std::vector
<std::vector
<ParseOp
>>& ops
,
354 std::vector
<std::vector
<std::string
>>& cnames
,
355 std::vector
<std::vector
<std::vector
<CVC4::Type
>>>& cargs
,
356 std::vector
<bool>& allow_const
,
357 std::vector
<std::vector
<std::string
>>& unresolved_gterm_sym
,
358 const std::vector
<CVC4::Expr
>& sygus_vars
,
359 std::map
<CVC4::Type
, CVC4::Type
>& sygus_to_builtin
,
360 std::map
<CVC4::Type
, CVC4::Expr
>& sygus_to_builtin_expr
,
362 bool isNested
= false);
364 bool pushSygusDatatypeDef(
367 std::vector
<CVC4::Datatype
>& datatypes
,
368 std::vector
<CVC4::Type
>& sorts
,
369 std::vector
<std::vector
<ParseOp
>>& ops
,
370 std::vector
<std::vector
<std::string
>>& cnames
,
371 std::vector
<std::vector
<std::vector
<CVC4::Type
>>>& cargs
,
372 std::vector
<bool>& allow_const
,
373 std::vector
<std::vector
<std::string
>>& unresolved_gterm_sym
);
375 bool popSygusDatatypeDef(
376 std::vector
<CVC4::Datatype
>& datatypes
,
377 std::vector
<CVC4::Type
>& sorts
,
378 std::vector
<std::vector
<ParseOp
>>& ops
,
379 std::vector
<std::vector
<std::string
>>& cnames
,
380 std::vector
<std::vector
<std::vector
<CVC4::Type
>>>& cargs
,
381 std::vector
<bool>& allow_const
,
382 std::vector
<std::vector
<std::string
>>& unresolved_gterm_sym
);
384 void setSygusStartIndex(const std::string
& fun
,
386 std::vector
<CVC4::Datatype
>& datatypes
,
387 std::vector
<CVC4::Type
>& sorts
,
388 std::vector
<std::vector
<ParseOp
>>& ops
);
390 void mkSygusDatatype(CVC4::Datatype
& dt
,
391 std::vector
<ParseOp
>& ops
,
392 std::vector
<std::string
>& cnames
,
393 std::vector
<std::vector
<CVC4::Type
>>& cargs
,
394 std::vector
<std::string
>& unresolved_gterm_sym
,
395 std::map
<CVC4::Type
, CVC4::Type
>& sygus_to_builtin
);
398 * Adds a constructor to sygus datatype dt whose sygus operator is term.
400 * ntsToUnres contains a mapping from non-terminal symbols to the unresolved
401 * types they correspond to. This map indicates how the argument term should
402 * be interpreted (instances of symbols from the domain of ntsToUnres
403 * correspond to constructor arguments).
405 * The sygus operator that is actually added to dt corresponds to replacing
406 * each occurrence of non-terminal symbols from the domain of ntsToUnres
407 * with bound variables via purifySygusGTerm, and binding these variables
410 void addSygusConstructorTerm(Datatype
& dt
,
412 std::map
<Expr
, Type
>& ntsToUnres
) const;
414 * This adds constructors to dt for sygus variables in sygusVars whose
415 * type is argument type. This method should be called when the sygus grammar
416 * term (Variable type) is encountered.
418 void addSygusConstructorVariables(Datatype
& dt
,
419 const std::vector
<Expr
>& sygusVars
,
423 * Smt2 parser provides its own checkDeclaration, which does the
424 * same as the base, but with some more helpful errors.
426 void checkDeclaration(const std::string
& name
,
427 DeclarationCheck check
,
428 SymbolType type
= SYM_VARIABLE
,
429 std::string notes
= "")
431 // if the symbol is something like "-1", we'll give the user a helpful
432 // syntax hint. (-1 is a valid identifier in SMT-LIB, NOT unary minus.)
433 if (name
.length() > 1 && name
[0] == '-'
434 && name
.find_first_not_of("0123456789", 1) == std::string::npos
)
438 // "-1" is allowed in SyGuS version 1.0
441 std::stringstream ss
;
442 ss
<< notes
<< "You may have intended to apply unary minus: `(- "
443 << name
.substr(1) << ")'\n";
444 this->Parser::checkDeclaration(name
, check
, type
, ss
.str());
447 this->Parser::checkDeclaration(name
, check
, type
, notes
);
450 void checkOperator(Kind kind
, unsigned numArgs
)
452 Parser::checkOperator(kind
, numArgs
);
453 // strict SMT-LIB mode enables extra checks for some bitvector operators
454 // that CVC4 permits as N-ary but the standard requires is binary
455 if(strictModeEnabled()) {
457 case kind::BITVECTOR_AND
:
458 case kind::BITVECTOR_MULT
:
459 case kind::BITVECTOR_OR
:
460 case kind::BITVECTOR_PLUS
:
461 case kind::BITVECTOR_XOR
:
462 if (numArgs
!= 2 && !v2_6())
465 "Operator requires exactly 2 arguments in strict SMT-LIB "
466 "compliance mode (for versions <2.6): "
467 + kindToString(kind
));
470 case kind::BITVECTOR_CONCAT
:
473 "Operator requires exactly 2 arguments in strict SMT-LIB "
475 + kindToString(kind
));
479 break; /* no problem */
483 /** Set named attribute
485 * This is called when expression expr is annotated with a name, i.e.
486 * (! expr :named sexpr). It sets up the necessary information to process
487 * this naming, including marking that expr is the last named term.
489 * We construct an expression symbol whose name is the name of s-expression
490 * which is used later for tracking assertions in unsat cores. This
491 * symbol is returned by this method.
493 Expr
setNamedAttribute(Expr
& expr
, const SExpr
& sexpr
);
495 // Throw a ParserException with msg appended with the current logic.
496 inline void parseErrorLogic(const std::string
& msg
)
498 const std::string withLogic
= msg
+ getLogic().getLogicString();
499 parseError(withLogic
);
502 //------------------------- processing parse operators
504 * Given a parse operator p, apply a type ascription to it. This method is run
505 * when we encounter "(as t type)" and information regarding t has been stored
508 * This updates p to take into account the ascription. This may include:
509 * - Converting an (pre-ascribed) array constant specification "const" to
510 * an ascribed array constant specification (as const type) where type is
511 * (Array T1 T2) for some T1, T2.
512 * - Converting a (nullary or non-nullary) parametric datatype constructor to
513 * the specialized constructor for the given type.
514 * - Converting an empty set, universe set, or separation nil reference to
515 * the respective term of the given type.
516 * - If p's expression field is set, then we leave p unchanged, check if
517 * that expression has the given type and throw a parse error otherwise.
519 void applyTypeAscription(ParseOp
& p
, Type type
);
521 * This converts a ParseOp to expression, assuming it is a standalone term.
524 * - If p's expression field is set, then that expression is returned.
525 * - If p's name field is set, then we look up that name in the symbol table
527 * In other cases, a parse error is thrown.
529 Expr
parseOpToExpr(ParseOp
& p
);
531 * Apply parse operator to list of arguments, and return the resulting
534 * This method involves two phases.
535 * (1) Processing the operator represented by p,
536 * (2) Applying that operator to the set of arguments.
538 * For (1), this involves determining the kind of the overall expression. We
539 * may be in one the following cases:
540 * - If p's expression field is set, we may choose to prepend it to args, or
541 * otherwise determine the appropriate kind of the overall expression based on
543 * - If p's name field is set, then we get the appropriate symbol for that
544 * name, which may involve disambiguating that name if it is overloaded based
545 * on the types of args. We then determine the overall kind of the return
546 * expression based on that symbol.
547 * - p's kind field may be already set.
549 * For (2), we construct the overall expression, which may involve the
551 * - If p is an array constant specification (as const (Array T1 T2)), then
552 * we return the appropriate array constant based on args[0].
553 * - If p represents a tuple selector, then we infer the appropriate tuple
554 * selector expression based on the type of args[0].
555 * - If the overall kind of the expression is chainable, we may convert it
556 * to a left- or right-associative chain.
557 * - If the overall kind is MINUS and args has size 1, then we return an
558 * application of UMINUS.
559 * - If the overall expression is a partial application, then we process this
560 * as a chain of HO_APPLY terms.
562 Expr
applyParseOp(ParseOp
& p
, std::vector
<Expr
>& args
);
563 //------------------------- end processing parse operators
565 std::map
< CVC4::Expr
, CVC4::Type
> d_sygus_bound_var_type
;
567 Type
processSygusNestedGTerm(
569 std::string
& sub_dname
,
570 std::vector
<CVC4::Datatype
>& datatypes
,
571 std::vector
<CVC4::Type
>& sorts
,
572 std::vector
<std::vector
<ParseOp
>>& ops
,
573 std::vector
<std::vector
<std::string
>>& cnames
,
574 std::vector
<std::vector
<std::vector
<CVC4::Type
>>>& cargs
,
575 std::vector
<bool>& allow_const
,
576 std::vector
<std::vector
<std::string
>>& unresolved_gterm_sym
,
577 std::map
<CVC4::Type
, CVC4::Type
>& sygus_to_builtin
,
578 std::map
<CVC4::Type
, CVC4::Expr
>& sygus_to_builtin_expr
,
581 /** make sygus bound var list
583 * This is used for converting non-builtin sygus operators to lambda
584 * expressions. It takes as input a datatype and constructor index (for
585 * naming) and a vector of type ltypes.
586 * It appends a bound variable to lvars for each type in ltypes, and returns
587 * a bound variable list whose children are lvars.
589 Expr
makeSygusBoundVarList(Datatype
& dt
,
591 const std::vector
<Type
>& ltypes
,
592 std::vector
<Expr
>& lvars
);
594 /** Purify sygus grammar term
596 * This returns a term where all occurrences of non-terminal symbols (those
597 * in the domain of ntsToUnres) are replaced by fresh variables. For each
598 * variable replaced in this way, we add the fresh variable it is replaced
599 * with to args, and the unresolved type corresponding to the non-terminal
600 * symbol to cargs (constructor args). In other words, args contains the
601 * free variables in the term returned by this method (which should be bound
602 * by a lambda), and cargs contains the types of the arguments of the
605 Expr
purifySygusGTerm(Expr term
,
606 std::map
<Expr
, Type
>& ntsToUnres
,
607 std::vector
<Expr
>& args
,
608 std::vector
<Type
>& cargs
) const;
610 void addArithmeticOperators();
612 void addTranscendentalOperators();
614 void addQuantifiersOperators();
616 void addBitvectorOperators();
618 void addDatatypesOperators();
620 void addStringOperators();
622 void addFloatingPointOperators();
624 void addSepOperators();
626 InputLanguage
getLanguage() const;
629 * Utility function to create a conjunction of expressions.
631 * @param es Expressions in the conjunction
632 * @return True if `es` is empty, `e` if `es` consists of a single element
633 * `e`, the conjunction of expressions otherwise.
635 Expr
mkAnd(const std::vector
<Expr
>& es
);
638 }/* CVC4::parser namespace */
639 }/* CVC4 namespace */
641 #endif /* CVC4__PARSER__SMT2_H */