/********************* */ /*! \file smt2.cpp ** \verbatim ** Top contributors (to current version): ** Andrew Reynolds, Kshitij Bansal, Morgan Deters ** This file is part of the CVC4 project. ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS ** in the top-level source directory) and their institutional affiliations. ** All rights reserved. See the file COPYING in the top-level source ** directory for licensing information.\endverbatim ** ** \brief Definitions of SMT2 constants. ** ** Definitions of SMT2 constants. **/ #include "parser/smt2/smt2.h" #include "api/cvc4cpp.h" #include "expr/type.h" #include "options/options.h" #include "parser/antlr_input.h" #include "parser/parser.h" #include "parser/smt1/smt1.h" #include "parser/smt2/smt2_input.h" #include "printer/sygus_print_callback.h" #include "smt/command.h" #include "util/bitvector.h" #include // ANTLR defines these, which is really bad! #undef true #undef false namespace CVC4 { namespace parser { Smt2::Smt2(api::Solver* solver, Input* input, bool strictMode, bool parseOnly) : Parser(solver, input, strictMode, parseOnly), d_logicSet(false) { if (!strictModeEnabled()) { addTheory(Smt2::THEORY_CORE); } } void Smt2::addArithmeticOperators() { Parser::addOperator(kind::PLUS); Parser::addOperator(kind::MINUS); Parser::addOperator(kind::UMINUS); Parser::addOperator(kind::MULT); Parser::addOperator(kind::LT); Parser::addOperator(kind::LEQ); Parser::addOperator(kind::GT); Parser::addOperator(kind::GEQ); // NOTE: this operator is non-standard addOperator(kind::POW, "^"); } void Smt2::addTranscendentalOperators() { addOperator(kind::EXPONENTIAL, "exp"); addOperator(kind::SINE, "sin"); addOperator(kind::COSINE, "cos"); addOperator(kind::TANGENT, "tan"); addOperator(kind::COSECANT, "csc"); addOperator(kind::SECANT, "sec"); addOperator(kind::COTANGENT, "cot"); addOperator(kind::ARCSINE, "arcsin"); addOperator(kind::ARCCOSINE, "arccos"); addOperator(kind::ARCTANGENT, "arctan"); addOperator(kind::ARCCOSECANT, "arccsc"); addOperator(kind::ARCSECANT, "arcsec"); addOperator(kind::ARCCOTANGENT, "arccot"); addOperator(kind::SQRT, "sqrt"); } void Smt2::addBitvectorOperators() { addOperator(kind::BITVECTOR_CONCAT, "concat"); addOperator(kind::BITVECTOR_NOT, "bvnot"); addOperator(kind::BITVECTOR_AND, "bvand"); addOperator(kind::BITVECTOR_OR, "bvor"); addOperator(kind::BITVECTOR_NEG, "bvneg"); addOperator(kind::BITVECTOR_PLUS, "bvadd"); addOperator(kind::BITVECTOR_MULT, "bvmul"); addOperator(kind::BITVECTOR_UDIV, "bvudiv"); addOperator(kind::BITVECTOR_UREM, "bvurem"); addOperator(kind::BITVECTOR_SHL, "bvshl"); addOperator(kind::BITVECTOR_LSHR, "bvlshr"); addOperator(kind::BITVECTOR_ULT, "bvult"); addOperator(kind::BITVECTOR_NAND, "bvnand"); addOperator(kind::BITVECTOR_NOR, "bvnor"); addOperator(kind::BITVECTOR_XOR, "bvxor"); addOperator(kind::BITVECTOR_XNOR, "bvxnor"); addOperator(kind::BITVECTOR_COMP, "bvcomp"); addOperator(kind::BITVECTOR_SUB, "bvsub"); addOperator(kind::BITVECTOR_SDIV, "bvsdiv"); addOperator(kind::BITVECTOR_SREM, "bvsrem"); addOperator(kind::BITVECTOR_SMOD, "bvsmod"); addOperator(kind::BITVECTOR_ASHR, "bvashr"); addOperator(kind::BITVECTOR_ULE, "bvule"); addOperator(kind::BITVECTOR_UGT, "bvugt"); addOperator(kind::BITVECTOR_UGE, "bvuge"); addOperator(kind::BITVECTOR_SLT, "bvslt"); addOperator(kind::BITVECTOR_SLE, "bvsle"); addOperator(kind::BITVECTOR_SGT, "bvsgt"); addOperator(kind::BITVECTOR_SGE, "bvsge"); addOperator(kind::BITVECTOR_REDOR, "bvredor"); addOperator(kind::BITVECTOR_REDAND, "bvredand"); Parser::addOperator(kind::BITVECTOR_BITOF); Parser::addOperator(kind::BITVECTOR_EXTRACT); Parser::addOperator(kind::BITVECTOR_REPEAT); Parser::addOperator(kind::BITVECTOR_ZERO_EXTEND); Parser::addOperator(kind::BITVECTOR_SIGN_EXTEND); Parser::addOperator(kind::BITVECTOR_ROTATE_LEFT); Parser::addOperator(kind::BITVECTOR_ROTATE_RIGHT); Parser::addOperator(kind::INT_TO_BITVECTOR); Parser::addOperator(kind::BITVECTOR_TO_NAT); } void Smt2::addStringOperators() { addOperator(kind::STRING_CONCAT, "str.++"); addOperator(kind::STRING_LENGTH, "str.len"); addOperator(kind::STRING_SUBSTR, "str.substr" ); addOperator(kind::STRING_STRCTN, "str.contains" ); addOperator(kind::STRING_CHARAT, "str.at" ); addOperator(kind::STRING_STRIDOF, "str.indexof" ); addOperator(kind::STRING_STRREPL, "str.replace" ); addOperator(kind::STRING_STRREPLALL, "str.replaceall"); addOperator(kind::STRING_PREFIX, "str.prefixof" ); addOperator(kind::STRING_SUFFIX, "str.suffixof" ); // at the moment, we only use this syntax for smt2.6.1 if (getLanguage() == language::input::LANG_SMTLIB_V2_6_1) { addOperator(kind::STRING_ITOS, "str.from-int"); addOperator(kind::STRING_STOI, "str.to-int"); addOperator(kind::STRING_IN_REGEXP, "str.in-re"); addOperator(kind::STRING_TO_REGEXP, "str.to-re"); } else { addOperator(kind::STRING_ITOS, "int.to.str"); addOperator(kind::STRING_STOI, "str.to.int"); addOperator(kind::STRING_IN_REGEXP, "str.in.re"); addOperator(kind::STRING_TO_REGEXP, "str.to.re"); } addOperator(kind::REGEXP_CONCAT, "re.++"); addOperator(kind::REGEXP_UNION, "re.union"); addOperator(kind::REGEXP_INTER, "re.inter"); addOperator(kind::REGEXP_STAR, "re.*"); addOperator(kind::REGEXP_PLUS, "re.+"); addOperator(kind::REGEXP_OPT, "re.opt"); addOperator(kind::REGEXP_RANGE, "re.range"); addOperator(kind::REGEXP_LOOP, "re.loop"); addOperator(kind::STRING_CODE, "str.code"); addOperator(kind::STRING_LT, "str.<"); addOperator(kind::STRING_LEQ, "str.<="); } void Smt2::addFloatingPointOperators() { addOperator(kind::FLOATINGPOINT_FP, "fp"); addOperator(kind::FLOATINGPOINT_EQ, "fp.eq"); addOperator(kind::FLOATINGPOINT_ABS, "fp.abs"); addOperator(kind::FLOATINGPOINT_NEG, "fp.neg"); addOperator(kind::FLOATINGPOINT_PLUS, "fp.add"); addOperator(kind::FLOATINGPOINT_SUB, "fp.sub"); addOperator(kind::FLOATINGPOINT_MULT, "fp.mul"); addOperator(kind::FLOATINGPOINT_DIV, "fp.div"); addOperator(kind::FLOATINGPOINT_FMA, "fp.fma"); addOperator(kind::FLOATINGPOINT_SQRT, "fp.sqrt"); addOperator(kind::FLOATINGPOINT_REM, "fp.rem"); addOperator(kind::FLOATINGPOINT_RTI, "fp.roundToIntegral"); addOperator(kind::FLOATINGPOINT_MIN, "fp.min"); addOperator(kind::FLOATINGPOINT_MAX, "fp.max"); addOperator(kind::FLOATINGPOINT_LEQ, "fp.leq"); addOperator(kind::FLOATINGPOINT_LT, "fp.lt"); addOperator(kind::FLOATINGPOINT_GEQ, "fp.geq"); addOperator(kind::FLOATINGPOINT_GT, "fp.gt"); addOperator(kind::FLOATINGPOINT_ISN, "fp.isNormal"); addOperator(kind::FLOATINGPOINT_ISSN, "fp.isSubnormal"); addOperator(kind::FLOATINGPOINT_ISZ, "fp.isZero"); addOperator(kind::FLOATINGPOINT_ISINF, "fp.isInfinite"); addOperator(kind::FLOATINGPOINT_ISNAN, "fp.isNaN"); addOperator(kind::FLOATINGPOINT_ISNEG, "fp.isNegative"); addOperator(kind::FLOATINGPOINT_ISPOS, "fp.isPositive"); addOperator(kind::FLOATINGPOINT_TO_REAL, "fp.to_real"); Parser::addOperator(kind::FLOATINGPOINT_TO_FP_GENERIC); Parser::addOperator(kind::FLOATINGPOINT_TO_FP_IEEE_BITVECTOR); Parser::addOperator(kind::FLOATINGPOINT_TO_FP_FLOATINGPOINT); Parser::addOperator(kind::FLOATINGPOINT_TO_FP_REAL); Parser::addOperator(kind::FLOATINGPOINT_TO_FP_SIGNED_BITVECTOR); Parser::addOperator(kind::FLOATINGPOINT_TO_FP_UNSIGNED_BITVECTOR); Parser::addOperator(kind::FLOATINGPOINT_TO_UBV); Parser::addOperator(kind::FLOATINGPOINT_TO_SBV); } void Smt2::addSepOperators() { addOperator(kind::SEP_STAR, "sep"); addOperator(kind::SEP_PTO, "pto"); addOperator(kind::SEP_WAND, "wand"); addOperator(kind::SEP_EMP, "emp"); Parser::addOperator(kind::SEP_STAR); Parser::addOperator(kind::SEP_PTO); Parser::addOperator(kind::SEP_WAND); Parser::addOperator(kind::SEP_EMP); } void Smt2::addTheory(Theory theory) { switch(theory) { case THEORY_ARRAYS: addOperator(kind::SELECT, "select"); addOperator(kind::STORE, "store"); break; case THEORY_BITVECTORS: addBitvectorOperators(); break; case THEORY_CORE: defineType("Bool", getExprManager()->booleanType()); defineVar("true", getExprManager()->mkConst(true)); defineVar("false", getExprManager()->mkConst(false)); Parser::addOperator(kind::AND); Parser::addOperator(kind::DISTINCT); Parser::addOperator(kind::EQUAL); Parser::addOperator(kind::IMPLIES); Parser::addOperator(kind::ITE); Parser::addOperator(kind::NOT); Parser::addOperator(kind::OR); Parser::addOperator(kind::XOR); break; case THEORY_REALS_INTS: defineType("Real", getExprManager()->realType()); Parser::addOperator(kind::DIVISION); addOperator(kind::TO_INTEGER, "to_int"); addOperator(kind::IS_INTEGER, "is_int"); addOperator(kind::TO_REAL, "to_real"); // falling through on purpose, to add Ints part of Reals_Ints case THEORY_INTS: defineType("Int", getExprManager()->integerType()); addArithmeticOperators(); addOperator(kind::INTS_DIVISION, "div"); addOperator(kind::INTS_MODULUS, "mod"); addOperator(kind::ABS, "abs"); Parser::addOperator(kind::DIVISIBLE); break; case THEORY_REALS: defineType("Real", getExprManager()->realType()); addArithmeticOperators(); Parser::addOperator(kind::DIVISION); break; case THEORY_TRANSCENDENTALS: addTranscendentalOperators(); break; case THEORY_QUANTIFIERS: break; case THEORY_SETS: addOperator(kind::UNION, "union"); addOperator(kind::INTERSECTION, "intersection"); addOperator(kind::SETMINUS, "setminus"); addOperator(kind::SUBSET, "subset"); addOperator(kind::MEMBER, "member"); addOperator(kind::SINGLETON, "singleton"); addOperator(kind::INSERT, "insert"); addOperator(kind::CARD, "card"); addOperator(kind::COMPLEMENT, "complement"); addOperator(kind::JOIN, "join"); addOperator(kind::PRODUCT, "product"); addOperator(kind::TRANSPOSE, "transpose"); addOperator(kind::TCLOSURE, "tclosure"); break; case THEORY_DATATYPES: { const std::vector types; defineType("Tuple", getExprManager()->mkTupleType(types)); Parser::addOperator(kind::APPLY_CONSTRUCTOR); Parser::addOperator(kind::APPLY_TESTER); Parser::addOperator(kind::APPLY_SELECTOR); Parser::addOperator(kind::APPLY_SELECTOR_TOTAL); break; } case THEORY_STRINGS: defineType("String", getExprManager()->stringType()); defineType("RegLan", getExprManager()->regExpType()); defineType("Int", getExprManager()->integerType()); addStringOperators(); break; case THEORY_UF: Parser::addOperator(kind::APPLY_UF); break; case THEORY_FP: defineType("RoundingMode", getExprManager()->roundingModeType()); defineType("Float16", getExprManager()->mkFloatingPointType(5, 11)); defineType("Float32", getExprManager()->mkFloatingPointType(8, 24)); defineType("Float64", getExprManager()->mkFloatingPointType(11, 53)); defineType("Float128", getExprManager()->mkFloatingPointType(15, 113)); addFloatingPointOperators(); break; case THEORY_SEP: addSepOperators(); break; default: std::stringstream ss; ss << "internal error: unsupported theory " << theory; throw ParserException(ss.str()); } } void Smt2::addOperator(Kind kind, const std::string& name) { Debug("parser") << "Smt2::addOperator( " << kind << ", " << name << " )" << std::endl; Parser::addOperator(kind); operatorKindMap[name] = kind; } Kind Smt2::getOperatorKind(const std::string& name) const { // precondition: isOperatorEnabled(name) return operatorKindMap.find(name)->second; } bool Smt2::isOperatorEnabled(const std::string& name) const { return operatorKindMap.find(name) != operatorKindMap.end(); } bool Smt2::isTheoryEnabled(Theory theory) const { switch(theory) { case THEORY_ARRAYS: return d_logic.isTheoryEnabled(theory::THEORY_ARRAYS); case THEORY_BITVECTORS: return d_logic.isTheoryEnabled(theory::THEORY_BV); case THEORY_CORE: return true; case THEORY_DATATYPES: return d_logic.isTheoryEnabled(theory::THEORY_DATATYPES); case THEORY_INTS: return d_logic.isTheoryEnabled(theory::THEORY_ARITH) && d_logic.areIntegersUsed() && ( !d_logic.areRealsUsed() ); case THEORY_REALS: return d_logic.isTheoryEnabled(theory::THEORY_ARITH) && ( !d_logic.areIntegersUsed() ) && d_logic.areRealsUsed(); case THEORY_REALS_INTS: return d_logic.isTheoryEnabled(theory::THEORY_ARITH) && d_logic.areIntegersUsed() && d_logic.areRealsUsed(); case THEORY_QUANTIFIERS: return d_logic.isQuantified(); case THEORY_SETS: return d_logic.isTheoryEnabled(theory::THEORY_SETS); case THEORY_STRINGS: return d_logic.isTheoryEnabled(theory::THEORY_STRINGS); case THEORY_UF: return d_logic.isTheoryEnabled(theory::THEORY_UF); case THEORY_FP: return d_logic.isTheoryEnabled(theory::THEORY_FP); case THEORY_SEP: return d_logic.isTheoryEnabled(theory::THEORY_SEP); default: std::stringstream ss; ss << "internal error: unsupported theory " << theory; throw ParserException(ss.str()); } } bool Smt2::logicIsSet() { return d_logicSet; } Expr Smt2::getExpressionForNameAndType(const std::string& name, Type t) { if(sygus() && name[0]=='-' && name.find_first_not_of("0123456789", 1) == std::string::npos) { //allow unary minus in sygus return getExprManager()->mkConst(Rational(name)); }else if(isAbstractValue(name)) { return mkAbstractValue(name); }else{ return Parser::getExpressionForNameAndType(name, t); } } Expr Smt2::mkDefineFunRec( const std::string& fname, const std::vector >& sortedVarNames, Type t, std::vector& flattenVars) { std::vector sorts; for (const std::pair& svn : sortedVarNames) { sorts.push_back(svn.second); } // make the flattened function type, add bound variables // to flattenVars if the defined function was given a function return type. Type ft = mkFlatFunctionType(sorts, t, flattenVars); // allow overloading return mkVar(fname, ft, ExprManager::VAR_FLAG_NONE, true); } void Smt2::pushDefineFunRecScope( const std::vector >& sortedVarNames, Expr func, const std::vector& flattenVars, std::vector& bvs, bool bindingLevel) { pushScope(bindingLevel); // bound variables are those that are explicitly named in the preamble // of the define-fun(s)-rec command, we define them here for (const std::pair& svn : sortedVarNames) { Expr v = mkBoundVar(svn.first, svn.second); bvs.push_back(v); } bvs.insert(bvs.end(), flattenVars.begin(), flattenVars.end()); } void Smt2::reset() { d_logicSet = false; d_logic = LogicInfo(); operatorKindMap.clear(); d_lastNamedTerm = std::pair(); this->Parser::reset(); if( !strictModeEnabled() ) { addTheory(Smt2::THEORY_CORE); } } void Smt2::resetAssertions() { // Remove all declarations except the ones at level 0. while (this->scopeLevel() > 0) { this->popScope(); } } void Smt2::setLogic(std::string name) { if(sygus()) { // non-smt2-standard sygus logic names go here (http://sygus.seas.upenn.edu/files/sygus.pdf Section 3.2) if(name == "Arrays") { name = "A"; }else if(name == "Reals") { name = "LRA"; } } d_logicSet = true; if(logicIsForced()) { d_logic = getForcedLogic(); } else { d_logic = name; } // if sygus is enabled, we must enable UF, datatypes, integer arithmetic and // higher-order if(sygus()) { if (!d_logic.isQuantified()) { warning("Logics in sygus are assumed to contain quantifiers."); warning("Omit QF_ from the logic to avoid this warning."); } // get unlocked copy, modify, copy and relock LogicInfo log(d_logic.getUnlockedCopy()); log.enableQuantifiers(); log.enableTheory(theory::THEORY_UF); log.enableTheory(theory::THEORY_DATATYPES); log.enableIntegers(); log.enableHigherOrder(); d_logic = log; d_logic.lock(); } // Core theory belongs to every logic addTheory(THEORY_CORE); if(d_logic.isTheoryEnabled(theory::THEORY_UF)) { addTheory(THEORY_UF); } if(d_logic.isTheoryEnabled(theory::THEORY_ARITH)) { if(d_logic.areIntegersUsed()) { if(d_logic.areRealsUsed()) { addTheory(THEORY_REALS_INTS); } else { addTheory(THEORY_INTS); } } else if(d_logic.areRealsUsed()) { addTheory(THEORY_REALS); } if (d_logic.areTranscendentalsUsed()) { addTheory(THEORY_TRANSCENDENTALS); } } if(d_logic.isTheoryEnabled(theory::THEORY_ARRAYS)) { addTheory(THEORY_ARRAYS); } if(d_logic.isTheoryEnabled(theory::THEORY_BV)) { addTheory(THEORY_BITVECTORS); } if(d_logic.isTheoryEnabled(theory::THEORY_DATATYPES)) { addTheory(THEORY_DATATYPES); } if(d_logic.isTheoryEnabled(theory::THEORY_SETS)) { addTheory(THEORY_SETS); } if(d_logic.isTheoryEnabled(theory::THEORY_STRINGS)) { addTheory(THEORY_STRINGS); } if(d_logic.isQuantified()) { addTheory(THEORY_QUANTIFIERS); } if (d_logic.isTheoryEnabled(theory::THEORY_FP)) { addTheory(THEORY_FP); } if (d_logic.isTheoryEnabled(theory::THEORY_SEP)) { addTheory(THEORY_SEP); } }/* Smt2::setLogic() */ void Smt2::setInfo(const std::string& flag, const SExpr& sexpr) { // TODO: ??? } void Smt2::setOption(const std::string& flag, const SExpr& sexpr) { // TODO: ??? } void Smt2::checkThatLogicIsSet() { if( ! logicIsSet() ) { if(strictModeEnabled()) { parseError("set-logic must appear before this point."); } else { warning("No set-logic command was given before this point."); warning("CVC4 will make all theories available."); warning("Consider setting a stricter logic for (likely) better performance."); warning("To suppress this warning in the future use (set-logic ALL)."); setLogic("ALL"); Command* c = new SetBenchmarkLogicCommand("ALL"); c->setMuted(true); preemptCommand(c); } } } /* The include are managed in the lexer but called in the parser */ // Inspired by http://www.antlr3.org/api/C/interop.html static bool newInputStream(const std::string& filename, pANTLR3_LEXER lexer) { Debug("parser") << "Including " << filename << std::endl; // Create a new input stream and take advantage of built in stream stacking // in C target runtime. // pANTLR3_INPUT_STREAM in; #ifdef CVC4_ANTLR3_OLD_INPUT_STREAM in = antlr3AsciiFileStreamNew((pANTLR3_UINT8) filename.c_str()); #else /* CVC4_ANTLR3_OLD_INPUT_STREAM */ in = antlr3FileStreamNew((pANTLR3_UINT8) filename.c_str(), ANTLR3_ENC_8BIT); #endif /* CVC4_ANTLR3_OLD_INPUT_STREAM */ if( in == NULL ) { Debug("parser") << "Can't open " << filename << std::endl; return false; } // Same thing as the predefined PUSHSTREAM(in); lexer->pushCharStream(lexer, in); // restart it //lexer->rec->state->tokenStartCharIndex = -10; //lexer->emit(lexer); // Note that the input stream is not closed when it EOFs, I don't bother // to do it here, but it is up to you to track streams created like this // and destroy them when the whole parse session is complete. Remember that you // don't want to do this until all tokens have been manipulated all the way through // your tree parsers etc as the token does not store the text it just refers // back to the input stream and trying to get the text for it will abort if you // close the input stream too early. //TODO what said before return true; } void Smt2::includeFile(const std::string& filename) { // security for online version if(!canIncludeFile()) { parseError("include-file feature was disabled for this run."); } // Get the lexer AntlrInput* ai = static_cast(getInput()); pANTLR3_LEXER lexer = ai->getAntlr3Lexer(); // get the name of the current stream "Does it work inside an include?" const std::string inputName = ai->getInputStreamName(); // Find the directory of the current input file std::string path; size_t pos = inputName.rfind('/'); if(pos != std::string::npos) { path = std::string(inputName, 0, pos + 1); } path.append(filename); if(!newInputStream(path, lexer)) { parseError("Couldn't open include file `" + path + "'"); } } void Smt2::mkSygusConstantsForType( const Type& type, std::vector& ops ) { if( type.isInteger() ){ ops.push_back(getExprManager()->mkConst(Rational(0))); ops.push_back(getExprManager()->mkConst(Rational(1))); }else if( type.isBitVector() ){ unsigned sz = ((BitVectorType)type).getSize(); BitVector bval0(sz, (unsigned int)0); ops.push_back( getExprManager()->mkConst(bval0) ); BitVector bval1(sz, (unsigned int)1); ops.push_back( getExprManager()->mkConst(bval1) ); }else if( type.isBoolean() ){ ops.push_back(getExprManager()->mkConst(true)); ops.push_back(getExprManager()->mkConst(false)); } //TODO : others? } // This method adds N operators to ops[index], N names to cnames[index] and N type argument vectors to cargs[index] (where typically N=1) // This method may also add new elements pairwise into datatypes/sorts/ops/cnames/cargs in the case of non-flat gterms. void Smt2::processSygusGTerm( CVC4::SygusGTerm& sgt, int index, std::vector< CVC4::Datatype >& datatypes, std::vector< CVC4::Type>& sorts, std::vector< std::vector >& ops, std::vector< std::vector >& cnames, std::vector< std::vector< std::vector< CVC4::Type > > >& cargs, std::vector< bool >& allow_const, std::vector< std::vector< std::string > >& unresolved_gterm_sym, std::vector& sygus_vars, std::map< CVC4::Type, CVC4::Type >& sygus_to_builtin, std::map< CVC4::Type, CVC4::Expr >& sygus_to_builtin_expr, CVC4::Type& ret, bool isNested ){ if( sgt.d_gterm_type==SygusGTerm::gterm_op || sgt.d_gterm_type==SygusGTerm::gterm_let ){ Debug("parser-sygus") << "Add " << sgt.d_expr << " to datatype " << index << ", isLet = " << (sgt.d_gterm_type == SygusGTerm::gterm_let) << std::endl; Kind oldKind; Kind newKind = kind::UNDEFINED_KIND; //convert to UMINUS if one child of MINUS if( sgt.d_children.size()==1 && sgt.d_expr==getExprManager()->operatorOf(kind::MINUS) ){ oldKind = kind::MINUS; newKind = kind::UMINUS; } if( newKind!=kind::UNDEFINED_KIND ){ Expr newExpr = getExprManager()->operatorOf(newKind); Debug("parser-sygus") << "Replace " << sgt.d_expr << " with " << newExpr << std::endl; sgt.d_expr = newExpr; std::string oldName = kind::kindToString(oldKind); std::string newName = kind::kindToString(newKind); size_t pos = 0; if((pos = sgt.d_name.find(oldName, pos)) != std::string::npos){ sgt.d_name.replace(pos, oldName.length(), newName); } } ops[index].push_back( sgt.d_expr ); cnames[index].push_back( sgt.d_name ); cargs[index].push_back( std::vector< CVC4::Type >() ); for( unsigned i=0; i consts; mkSygusConstantsForType( sgt.d_type, consts ); Debug("parser-sygus") << "...made " << consts.size() << " constants." << std::endl; for( unsigned i=0; i() ); } allow_const[index] = true; }else if( sgt.d_gterm_type==SygusGTerm::gterm_variable || sgt.d_gterm_type==SygusGTerm::gterm_input_variable ){ if( sgt.getNumChildren()!=0 ){ parseError("Bad syntax for Sygus Variable."); } Debug("parser-sygus") << "...process " << sygus_vars.size() << " variables." << std::endl; for( unsigned i=0; i() ); } } }else if( sgt.d_gterm_type==SygusGTerm::gterm_nested_sort ){ ret = sgt.d_type; }else if( sgt.d_gterm_type==SygusGTerm::gterm_unresolved ){ if( isNested ){ if( isUnresolvedType(sgt.d_name) ){ ret = getSort(sgt.d_name); }else{ //nested, unresolved symbol...fail std::stringstream ss; ss << "Cannot handle nested unresolved symbol " << sgt.d_name << std::endl; parseError(ss.str()); } }else{ //will resolve when adding constructors unresolved_gterm_sym[index].push_back(sgt.d_name); } }else if( sgt.d_gterm_type==SygusGTerm::gterm_ignore ){ } } bool Smt2::pushSygusDatatypeDef( Type t, std::string& dname, std::vector< CVC4::Datatype >& datatypes, std::vector< CVC4::Type>& sorts, std::vector< std::vector >& ops, std::vector< std::vector >& cnames, std::vector< std::vector< std::vector< CVC4::Type > > >& cargs, std::vector< bool >& allow_const, std::vector< std::vector< std::string > >& unresolved_gterm_sym ){ sorts.push_back(t); datatypes.push_back(Datatype(dname)); ops.push_back(std::vector()); cnames.push_back(std::vector()); cargs.push_back(std::vector >()); allow_const.push_back(false); unresolved_gterm_sym.push_back(std::vector< std::string >()); return true; } bool Smt2::popSygusDatatypeDef( std::vector< CVC4::Datatype >& datatypes, std::vector< CVC4::Type>& sorts, std::vector< std::vector >& ops, std::vector< std::vector >& cnames, std::vector< std::vector< std::vector< CVC4::Type > > >& cargs, std::vector< bool >& allow_const, std::vector< std::vector< std::string > >& unresolved_gterm_sym ){ sorts.pop_back(); datatypes.pop_back(); ops.pop_back(); cnames.pop_back(); cargs.pop_back(); allow_const.pop_back(); unresolved_gterm_sym.pop_back(); return true; } Type Smt2::processSygusNestedGTerm( int sub_dt_index, std::string& sub_dname, std::vector< CVC4::Datatype >& datatypes, std::vector< CVC4::Type>& sorts, std::vector< std::vector >& ops, std::vector< std::vector >& cnames, std::vector< std::vector< std::vector< CVC4::Type > > >& cargs, std::vector< bool >& allow_const, std::vector< std::vector< std::string > >& unresolved_gterm_sym, std::map< CVC4::Type, CVC4::Type >& sygus_to_builtin, std::map< CVC4::Type, CVC4::Expr >& sygus_to_builtin_expr, Type sub_ret ) { Type t = sub_ret; Debug("parser-sygus") << "Argument is "; if( t.isNull() ){ //then, it is the datatype we constructed, which should have a single constructor t = mkUnresolvedType(sub_dname); Debug("parser-sygus") << "inline flattening of (auxiliary, local) datatype " << t << std::endl; Debug("parser-sygus") << ": to compute type, construct ground term witnessing the grammar, #cons=" << cargs[sub_dt_index].size() << std::endl; if( cargs[sub_dt_index].empty() ){ parseError(std::string("Internal error : datatype for nested gterm does not have a constructor.")); } Expr sop = ops[sub_dt_index][0]; Type curr_t; if( sop.getKind() != kind::BUILTIN && ( sop.isConst() || cargs[sub_dt_index][0].empty() ) ){ curr_t = sop.getType(); Debug("parser-sygus") << ": it is constant/0-arg cons " << sop << " with type " << sop.getType() << ", debug=" << sop.isConst() << " " << cargs[sub_dt_index][0].size() << std::endl; // only cache if it is a singleton datatype (has unique expr) if (ops[sub_dt_index].size() == 1) { sygus_to_builtin_expr[t] = sop; // store that term sop has dedicated sygus type t if (d_sygus_bound_var_type.find(sop) == d_sygus_bound_var_type.end()) { d_sygus_bound_var_type[sop] = t; } } }else{ std::vector< Expr > children; if( sop.getKind() != kind::BUILTIN ){ children.push_back( sop ); } for( unsigned i=0; i::iterator it = sygus_to_builtin_expr.find( cargs[sub_dt_index][0][i] ); if( it==sygus_to_builtin_expr.end() ){ if( sygus_to_builtin.find( cargs[sub_dt_index][0][i] )==sygus_to_builtin.end() ){ std::stringstream ss; ss << "Missing builtin type for type " << cargs[sub_dt_index][0][i] << "!" << std::endl; ss << "Builtin types are currently : " << std::endl; for( std::map< CVC4::Type, CVC4::Type >::iterator itb = sygus_to_builtin.begin(); itb != sygus_to_builtin.end(); ++itb ){ ss << " " << itb->first << " -> " << itb->second << std::endl; } parseError(ss.str()); } Type bt = sygus_to_builtin[cargs[sub_dt_index][0][i]]; Debug("parser-sygus") << ": child " << i << " introduce type elem for " << cargs[sub_dt_index][0][i] << " " << bt << std::endl; std::stringstream ss; ss << t << "_x_" << i; Expr bv = mkBoundVar(ss.str(), bt); children.push_back( bv ); d_sygus_bound_var_type[bv] = cargs[sub_dt_index][0][i]; }else{ Debug("parser-sygus") << ": child " << i << " existing sygus to builtin expr : " << it->second << std::endl; children.push_back( it->second ); } } Kind sk = sop.getKind() != kind::BUILTIN ? kind::APPLY : getExprManager()->operatorToKind(sop); Debug("parser-sygus") << ": operator " << sop << " with " << sop.getKind() << " " << sk << std::endl; Expr e = getExprManager()->mkExpr( sk, children ); Debug("parser-sygus") << ": constructed " << e << ", which has type " << e.getType() << std::endl; curr_t = e.getType(); sygus_to_builtin_expr[t] = e; } sorts[sub_dt_index] = curr_t; sygus_to_builtin[t] = curr_t; }else{ Debug("parser-sygus") << "simple argument " << t << std::endl; Debug("parser-sygus") << "...removing " << datatypes.back().getName() << std::endl; //otherwise, datatype was unecessary //pop argument datatype definition popSygusDatatypeDef( datatypes, sorts, ops, cnames, cargs, allow_const, unresolved_gterm_sym ); } return t; } void Smt2::processSygusLetConstructor( std::vector< CVC4::Expr >& let_vars, int index, std::vector< CVC4::Datatype >& datatypes, std::vector< CVC4::Type>& sorts, std::vector< std::vector >& ops, std::vector< std::vector >& cnames, std::vector< std::vector< std::vector< CVC4::Type > > >& cargs, std::vector& sygus_vars, std::map< CVC4::Type, CVC4::Type >& sygus_to_builtin, std::map< CVC4::Type, CVC4::Expr >& sygus_to_builtin_expr ) { std::vector< CVC4::Expr > let_define_args; Expr let_body; int dindex = cargs[index].size()-1; Debug("parser-sygus") << "Process let constructor for datatype " << datatypes[index].getName() << ", #subtypes = " << cargs[index][dindex].size() << std::endl; for( unsigned i=0; i::iterator it = sygus_to_builtin_expr.find( cargs[index][dindex][i] ); if( it!=sygus_to_builtin_expr.end() ){ let_body = it->second; }else{ std::stringstream ss; ss << datatypes[index].getName() << "_body"; let_body = mkBoundVar(ss.str(), sygus_to_builtin[cargs[index][dindex][i]]); d_sygus_bound_var_type[let_body] = cargs[index][dindex][i]; } } } Debug("parser-sygus") << std::endl; Debug("parser-sygus") << "Body is " << let_body << std::endl; Debug("parser-sygus") << "# let vars = " << let_vars.size() << std::endl; for( unsigned i=0; i fsorts; for( unsigned i=0; imkFunctionType(fsorts, let_body.getType()); std::stringstream ss; ss << datatypes[index].getName() << "_let"; Expr let_func = mkFunction(ss.str(), ft, ExprManager::VAR_FLAG_DEFINED); d_sygus_defined_funs.push_back( let_func ); preemptCommand( new DefineFunctionCommand(ss.str(), let_func, let_define_args, let_body) ); ops[index].pop_back(); ops[index].push_back( let_func ); cnames[index].pop_back(); cnames[index].push_back(ss.str()); //mark function as let constructor d_sygus_let_func_to_vars[let_func].insert( d_sygus_let_func_to_vars[let_func].end(), let_define_args.begin(), let_define_args.end() ); d_sygus_let_func_to_body[let_func] = let_body; d_sygus_let_func_to_num_input_vars[let_func] = let_vars.size(); } void Smt2::collectSygusLetArgs( CVC4::Expr e, std::vector< CVC4::Type >& sygusArgs, std::vector< CVC4::Expr >& builtinArgs ) { if( e.getKind()==kind::BOUND_VARIABLE ){ if( std::find( builtinArgs.begin(), builtinArgs.end(), e )==builtinArgs.end() ){ builtinArgs.push_back( e ); sygusArgs.push_back( d_sygus_bound_var_type[e] ); if( d_sygus_bound_var_type[e].isNull() ){ std::stringstream ss; ss << "While constructing body of let gterm, can't map " << e << " to sygus type." << std::endl; parseError(ss.str()); } } }else{ for( unsigned i=0; i& datatypes, std::vector< CVC4::Type>& sorts, std::vector< std::vector >& ops ) { if( startIndex>0 ){ CVC4::Datatype tmp_dt = datatypes[0]; Type tmp_sort = sorts[0]; std::vector< Expr > tmp_ops; tmp_ops.insert( tmp_ops.end(), ops[0].begin(), ops[0].end() ); datatypes[0] = datatypes[startIndex]; sorts[0] = sorts[startIndex]; ops[0].clear(); ops[0].insert( ops[0].end(), ops[startIndex].begin(), ops[startIndex].end() ); datatypes[startIndex] = tmp_dt; sorts[startIndex] = tmp_sort; ops[startIndex].clear(); ops[startIndex].insert( ops[startIndex].begin(), tmp_ops.begin(), tmp_ops.end() ); }else if( startIndex<0 ){ std::stringstream ss; ss << "warning: no symbol named Start for synth-fun " << fun << std::endl; warning(ss.str()); } } void Smt2::mkSygusDatatype( CVC4::Datatype& dt, std::vector& ops, std::vector& cnames, std::vector< std::vector< CVC4::Type > >& cargs, std::vector& unresolved_gterm_sym, std::map< CVC4::Type, CVC4::Type >& sygus_to_builtin ) { Debug("parser-sygus") << "Making sygus datatype " << dt.getName() << std::endl; Debug("parser-sygus") << " add constructors..." << std::endl; Debug("parser-sygus") << "SMT2 sygus parser : Making constructors for sygus datatype " << dt.getName() << std::endl; Debug("parser-sygus") << " add constructors..." << std::endl; // size of cnames changes, this loop must check size for (unsigned i = 0; i < cnames.size(); i++) { bool is_dup = false; bool is_dup_op = false; for (unsigned j = 0; j < i; j++) { if( ops[i]==ops[j] ){ is_dup_op = true; if( cargs[i].size()==cargs[j].size() ){ is_dup = true; for( unsigned k=0; k Duplicate gterm : " << ops[i] << std::endl; ops.erase( ops.begin() + i, ops.begin() + i + 1 ); cnames.erase( cnames.begin() + i, cnames.begin() + i + 1 ); cargs.erase( cargs.begin() + i, cargs.begin() + i + 1 ); i--; } else { std::shared_ptr spc; if (is_dup_op) { Debug("parser-sygus") << "--> Duplicate gterm operator : " << ops[i] << std::endl; // make into define-fun std::vector ltypes; for (unsigned j = 0, size = cargs[i].size(); j < size; j++) { ltypes.push_back(sygus_to_builtin[cargs[i][j]]); } std::vector largs; Expr lbvl = makeSygusBoundVarList(dt, i, ltypes, largs); // make the let_body std::vector children; if (ops[i].getKind() != kind::BUILTIN) { children.push_back(ops[i]); } children.insert(children.end(), largs.begin(), largs.end()); Kind sk = ops[i].getKind() != kind::BUILTIN ? kind::APPLY : getExprManager()->operatorToKind(ops[i]); Expr body = getExprManager()->mkExpr(sk, children); // replace by lambda ops[i] = getExprManager()->mkExpr(kind::LAMBDA, lbvl, body); Debug("parser-sygus") << " ...replace op : " << ops[i] << std::endl; // callback prints as the expression spc = std::make_shared(body, largs); } else { std::map::iterator it = d_sygus_let_func_to_body.find(ops[i]); if (it != d_sygus_let_func_to_body.end()) { Debug("parser-sygus") << "--> Let expression " << ops[i] << std::endl; Expr let_body = it->second; std::vector let_args = d_sygus_let_func_to_vars[ops[i]]; unsigned let_num_input_args = d_sygus_let_func_to_num_input_vars[ops[i]]; // the operator is just the body for the arguments std::vector ltypes; for (unsigned j = 0, size = let_args.size(); j < size; j++) { ltypes.push_back(let_args[j].getType()); } std::vector largs; Expr lbvl = makeSygusBoundVarList(dt, i, ltypes, largs); Expr sbody = let_body.substitute(let_args, largs); ops[i] = getExprManager()->mkExpr(kind::LAMBDA, lbvl, sbody); Debug("parser-sygus") << " ...replace op : " << ops[i] << std::endl; // callback prints as a let expression spc = std::make_shared( let_body, let_args, let_num_input_args); } else if (ops[i].getType().isBitVector() && ops[i].isConst()) { Debug("parser-sygus") << "--> Bit-vector constant " << ops[i] << " (" << cnames[i] << ")" << std::endl; // Since there are multiple output formats for bit-vectors and // we are required by sygus standards to print in the exact input // format given by the user, we use a print callback to custom print // the given name. spc = std::make_shared(cnames[i]); } else if (isDefinedFunction(ops[i])) { Debug("parser-sygus") << "--> Defined function " << ops[i] << std::endl; // turn f into (lammbda (x) (f x)) // in a degenerate case, ops[i] may be a defined constant, // in which case we do not replace by a lambda. if (ops[i].getType().isFunction()) { std::vector ftypes = static_cast(ops[i].getType()).getArgTypes(); std::vector largs; Expr lbvl = makeSygusBoundVarList(dt, i, ftypes, largs); largs.insert(largs.begin(), ops[i]); Expr body = getExprManager()->mkExpr(kind::APPLY, largs); ops[i] = getExprManager()->mkExpr(kind::LAMBDA, lbvl, body); Debug("parser-sygus") << " ...replace op : " << ops[i] << std::endl; } else { ops[i] = getExprManager()->mkExpr(kind::APPLY, ops[i]); Debug("parser-sygus") << " ...replace op : " << ops[i] << std::endl; } // keep a callback to say it should be printed with the defined name spc = std::make_shared(cnames[i]); } else { Debug("parser-sygus") << "--> Default case " << ops[i] << std::endl; } } // must rename to avoid duplication std::stringstream ss; ss << dt.getName() << "_" << i << "_" << cnames[i]; cnames[i] = ss.str(); Debug("parser-sygus") << " construct the datatype " << cnames[i] << "..." << std::endl; // add the sygus constructor dt.addSygusConstructor(ops[i], cnames[i], cargs[i], spc); Debug("parser-sygus") << " finished constructing the datatype" << std::endl; } } Debug("parser-sygus") << " add constructors for unresolved symbols..." << std::endl; if( !unresolved_gterm_sym.empty() ){ std::vector< Type > types; Debug("parser-sygus") << "...resolve " << unresolved_gterm_sym.size() << " symbols..." << std::endl; for( unsigned i=0; i lchildren; lchildren.push_back( getExprManager()->mkExpr(kind::BOUND_VAR_LIST, var)); lchildren.push_back(var); Expr id_op = getExprManager()->mkExpr(kind::LAMBDA, lchildren); // empty sygus callback (should not be printed) std::shared_ptr sepc = std::make_shared(); //make the sygus argument list std::vector< Type > id_carg; id_carg.push_back( t ); dt.addSygusConstructor(id_op, unresolved_gterm_sym[i], id_carg, sepc); //add to operators ops.push_back( id_op ); } }else{ Debug("parser-sygus") << " ignore. (likely a free let variable)" << std::endl; } } } } Expr Smt2::makeSygusBoundVarList(Datatype& dt, unsigned i, const std::vector& ltypes, std::vector& lvars) { for (unsigned j = 0, size = ltypes.size(); j < size; j++) { std::stringstream ss; ss << dt.getName() << "_x_" << i << "_" << j; Expr v = mkBoundVar(ss.str(), ltypes[j]); lvars.push_back(v); } return getExprManager()->mkExpr(kind::BOUND_VAR_LIST, lvars); } InputLanguage Smt2::getLanguage() const { ExprManager* em = getExprManager(); return em->getOptions().getInputLanguage(); } }/* CVC4::parser namespace */ }/* CVC4 namespace */