diff options
author | ajreynol <andrew.j.reynolds@gmail.com> | 2016-02-16 14:55:28 -0600 |
---|---|---|
committer | ajreynol <andrew.j.reynolds@gmail.com> | 2016-02-16 14:55:28 -0600 |
commit | c603a047ac534ed4caafb128b5d333e05e1fd191 (patch) | |
tree | b24999a8050d82df3cbcb0a8ade4ba7990af56c8 /src/smt | |
parent | 80daa7fd5917526513a510261fd3901f03949dfa (diff) |
Public interface for quantifier elimination. Minor changes to datatypes rewriter.
Diffstat (limited to 'src/smt')
-rw-r--r-- | src/smt/command.cpp | 54 | ||||
-rw-r--r-- | src/smt/command.h | 19 | ||||
-rw-r--r-- | src/smt/smt_engine.cpp | 186 | ||||
-rw-r--r-- | src/smt/smt_engine.h | 5 |
4 files changed, 206 insertions, 58 deletions
diff --git a/src/smt/command.cpp b/src/smt/command.cpp index d6ec0769a..89d7b5ca2 100644 --- a/src/smt/command.cpp +++ b/src/smt/command.cpp @@ -1233,6 +1233,60 @@ std::string GetSynthSolutionCommand::getCommandName() const throw() { return "get-instantiations"; } +/* class GetQuantifierEliminationCommand */ + +GetQuantifierEliminationCommand::GetQuantifierEliminationCommand() throw() : + d_expr() { +} + +GetQuantifierEliminationCommand::GetQuantifierEliminationCommand(const Expr& expr, bool doFull) throw() : + d_expr(expr), d_doFull(doFull) { +} + +Expr GetQuantifierEliminationCommand::getExpr() const throw() { + return d_expr; +} +bool GetQuantifierEliminationCommand::getDoFull() const throw() { + return d_doFull; +} + +void GetQuantifierEliminationCommand::invoke(SmtEngine* smtEngine) throw() { + try { + d_result = smtEngine->doQuantifierElimination(d_expr, d_doFull); + d_commandStatus = CommandSuccess::instance(); + } catch(exception& e) { + d_commandStatus = new CommandFailure(e.what()); + } +} + +Expr GetQuantifierEliminationCommand::getResult() const throw() { + return d_result; +} + +void GetQuantifierEliminationCommand::printResult(std::ostream& out, uint32_t verbosity) const throw() { + if(! ok()) { + this->Command::printResult(out, verbosity); + } else { + out << d_result << endl; + } +} + +Command* GetQuantifierEliminationCommand::exportTo(ExprManager* exprManager, ExprManagerMapCollection& variableMap) { + GetQuantifierEliminationCommand* c = new GetQuantifierEliminationCommand(d_expr.exportTo(exprManager, variableMap), d_doFull); + c->d_result = d_result; + return c; +} + +Command* GetQuantifierEliminationCommand::clone() const { + GetQuantifierEliminationCommand* c = new GetQuantifierEliminationCommand(d_expr, d_doFull); + c->d_result = d_result; + return c; +} + +std::string GetQuantifierEliminationCommand::getCommandName() const throw() { + return d_doFull ? "get-qe" : "get-qe-disjunct"; +} + /* class GetUnsatCoreCommand */ GetUnsatCoreCommand::GetUnsatCoreCommand() throw() { diff --git a/src/smt/command.h b/src/smt/command.h index 248e69b0e..512f147da 100644 --- a/src/smt/command.h +++ b/src/smt/command.h @@ -624,6 +624,25 @@ public: std::string getCommandName() const throw(); };/* class GetSynthSolutionCommand */ +class CVC4_PUBLIC GetQuantifierEliminationCommand : public Command { +protected: + Expr d_expr; + bool d_doFull; + Expr d_result; +public: + GetQuantifierEliminationCommand() throw(); + GetQuantifierEliminationCommand(const Expr& expr, bool doFull) throw(); + ~GetQuantifierEliminationCommand() throw() {} + Expr getExpr() const throw(); + bool getDoFull() const throw(); + void invoke(SmtEngine* smtEngine) throw(); + Expr getResult() const throw(); + void printResult(std::ostream& out, uint32_t verbosity = 2) const throw(); + Command* exportTo(ExprManager* exprManager, ExprManagerMapCollection& variableMap); + Command* clone() const; + std::string getCommandName() const throw(); +};/* class GetQuantifierEliminationCommand */ + class CVC4_PUBLIC GetUnsatCoreCommand : public Command { protected: UnsatCore d_result; diff --git a/src/smt/smt_engine.cpp b/src/smt/smt_engine.cpp index 007c5e049..3dc64d61a 100644 --- a/src/smt/smt_engine.cpp +++ b/src/smt/smt_engine.cpp @@ -955,6 +955,45 @@ public: std::ostream* getReplayLog() const { return d_managedReplayLog.getReplayLog(); } + + Node replaceQuantifiersWithInstantiations( Node n, std::map< Node, std::vector< Node > >& insts, std::map< Node, Node >& visited ){ + std::map< Node, Node >::iterator itv = visited.find( n ); + if( itv!=visited.end() ){ + return itv->second; + }else{ + Node ret = n; + if( n.getKind()==kind::FORALL ){ + std::map< Node, std::vector< Node > >::iterator it = insts.find( n ); + if( it==insts.end() ){ + Trace("smt-qe-debug") << "* " << n << " has no instances" << std::endl; + ret = NodeManager::currentNM()->mkConst(true); + }else{ + Trace("smt-qe-debug") << "* " << n << " has " << it->second.size() << " instances" << std::endl; + Node reti = it->second.empty() ? NodeManager::currentNM()->mkConst(true) : ( it->second.size()==1 ? it->second[0] : NodeManager::currentNM()->mkNode( kind::AND, it->second ) ); + Trace("smt-qe-debug") << " return : " << ret << std::endl; + //recursive (for nested quantification) + ret = replaceQuantifiersWithInstantiations( reti, insts, visited ); + } + }else if( n.getNumChildren()>0 ){ + bool childChanged = false; + std::vector< Node > children; + if( n.getMetaKind() == kind::metakind::PARAMETERIZED ){ + children.push_back( n.getOperator() ); + } + for( unsigned i=0; i<n.getNumChildren(); i++ ){ + Node r = replaceQuantifiersWithInstantiations( n[i], insts, visited ); + children.push_back( r ); + childChanged = childChanged || r!=n[i]; + } + if( childChanged ){ + ret = NodeManager::currentNM()->mkNode( n.getKind(), children ); + } + } + visited[n] = ret; + return ret; + } + } + };/* class SmtEnginePrivate */ }/* namespace CVC4::smt */ @@ -4318,80 +4357,80 @@ Result SmtEngine::query(const Expr& ex, bool inUnsatCore) throw(TypeCheckingExce Trace("smt") << "SMT query(" << ex << ")" << endl; try { - if(d_queryMade && !options::incrementalSolving()) { - throw ModalException("Cannot make multiple queries unless " - "incremental solving is enabled " - "(try --incremental)"); - } + if(d_queryMade && !options::incrementalSolving()) { + throw ModalException("Cannot make multiple queries unless " + "incremental solving is enabled " + "(try --incremental)"); + } - // Substitute out any abstract values in ex - Expr e = d_private->substituteAbstractValues(Node::fromExpr(ex)).toExpr(); - // Ensure that the expression is type-checked at this point, and Boolean - ensureBoolean(e); + // Substitute out any abstract values in ex + Expr e = d_private->substituteAbstractValues(Node::fromExpr(ex)).toExpr(); + // Ensure that the expression is type-checked at this point, and Boolean + ensureBoolean(e); - // check to see if a postsolve() is pending - if(d_needPostsolve) { - d_theoryEngine->postsolve(); - d_needPostsolve = false; - } + // check to see if a postsolve() is pending + if(d_needPostsolve) { + d_theoryEngine->postsolve(); + d_needPostsolve = false; + } - // Push the context - internalPush(); + // Push the context + internalPush(); - // Note that a query has been made - d_queryMade = true; + // Note that a query has been made + d_queryMade = true; - // Add the formula - d_problemExtended = true; - if(d_assertionList != NULL) { - d_assertionList->push_back(e.notExpr()); - } - d_private->addFormula(e.getNode().notNode(), inUnsatCore); + // Add the formula + d_problemExtended = true; + if(d_assertionList != NULL) { + d_assertionList->push_back(e.notExpr()); + } + d_private->addFormula(e.getNode().notNode(), inUnsatCore); - // Run the check - Result r(Result::SAT_UNKNOWN, Result::UNKNOWN_REASON); - r = check().asValidityResult(); - d_needPostsolve = true; + // Run the check + Result r(Result::SAT_UNKNOWN, Result::UNKNOWN_REASON); + r = check().asValidityResult(); + d_needPostsolve = true; - // Dump the query if requested - if(Dump.isOn("benchmark")) { - // the expr already got dumped out if assertion-dumping is on - Dump("benchmark") << QueryCommand(ex); - } + // Dump the query if requested + if(Dump.isOn("benchmark")) { + // the expr already got dumped out if assertion-dumping is on + Dump("benchmark") << QueryCommand(ex); + } - // Pop the context - internalPop(); + // Pop the context + internalPop(); - // Remember the status - d_status = r; + // Remember the status + d_status = r; - d_problemExtended = false; + d_problemExtended = false; - Trace("smt") << "SMT query(" << e << ") ==> " << r << endl; + Trace("smt") << "SMT query(" << e << ") ==> " << r << endl; - // Check that SAT results generate a model correctly. - if(options::checkModels()) { - if(r.asSatisfiabilityResult().isSat() == Result::SAT || - (r.isUnknown() && r.whyUnknown() == Result::INCOMPLETE) ){ - checkModel(/* hard failure iff */ ! r.isUnknown()); + // Check that SAT results generate a model correctly. + if(options::checkModels()) { + if(r.asSatisfiabilityResult().isSat() == Result::SAT || + (r.isUnknown() && r.whyUnknown() == Result::INCOMPLETE) ){ + checkModel(/* hard failure iff */ ! r.isUnknown()); + } } - } - // Check that UNSAT results generate a proof correctly. - if(options::checkProofs()) { - if(r.asSatisfiabilityResult().isSat() == Result::UNSAT) { - TimerStat::CodeTimer checkProofTimer(d_stats->d_checkProofTime); - checkProof(); + // Check that UNSAT results generate a proof correctly. + if(options::checkProofs()) { + if(r.asSatisfiabilityResult().isSat() == Result::UNSAT) { + TimerStat::CodeTimer checkProofTimer(d_stats->d_checkProofTime); + checkProof(); + } } - } - // Check that UNSAT results generate an unsat core correctly. - if(options::checkUnsatCores()) { - if(r.asSatisfiabilityResult().isSat() == Result::UNSAT) { - TimerStat::CodeTimer checkUnsatCoreTimer(d_stats->d_checkUnsatCoreTime); - checkUnsatCore(); + // Check that UNSAT results generate an unsat core correctly. + if(options::checkUnsatCores()) { + if(r.asSatisfiabilityResult().isSat() == Result::UNSAT) { + TimerStat::CodeTimer checkUnsatCoreTimer(d_stats->d_checkUnsatCoreTime); + checkUnsatCore(); + } } - } - return r; + return r; } catch (UnsafeInterruptException& e) { AlwaysAssert(d_private->getResourceManager()->out()); Result::UnknownExplanation why = d_private->getResourceManager()->outOfResources() ? @@ -5017,6 +5056,37 @@ void SmtEngine::printSynthSolution( std::ostream& out ) { } } +Expr SmtEngine::doQuantifierElimination(const Expr& e, bool doFull) { + SmtScope smts(this); + Trace("smt-qe") << "Do quantifier elimination " << e << std::endl; + Result r = query(e); + Trace("smt-qe") << "Query returned " << r << std::endl; + if(r.asSatisfiabilityResult().isSat() == Result::SAT) { + Node input = Node::fromExpr( e ); + input = Rewriter::rewrite( input ); + Trace("smt-qe") << "Replace instances in rewritten input: " << input << std::endl; + std::map< Node, std::vector< Node > > insts; + d_theoryEngine->getInstantiations( insts ); + std::map< Node, Node > visited; + Node en = d_private->replaceQuantifiersWithInstantiations( input, insts, visited ); + + //ensure all instantiations were accounted for + for( std::map< Node, std::vector< Node > >::iterator it = insts.begin(); it != insts.end(); ++it ){ + if( visited.find( it->first )==visited.end() ){ + stringstream ss; + ss << "While performing quantifier elimination, processed a quantified formula : " << it->first; + ss << " that was not related to the query. Try option --simplification=none." << std::endl; + InternalError(ss.str().c_str()); + } + } + Trace("smt-qe") << "Returned : " << en << std::endl; + en = Rewriter::rewrite( en ); + return en.toExpr(); + }else{ + return NodeManager::currentNM()->mkConst(false).toExpr(); + } +} + vector<Expr> SmtEngine::getAssertions() throw(ModalException) { SmtScope smts(this); finalOptionsAreSet(); diff --git a/src/smt/smt_engine.h b/src/smt/smt_engine.h index 3616762bc..5aa33731e 100644 --- a/src/smt/smt_engine.h +++ b/src/smt/smt_engine.h @@ -553,6 +553,11 @@ public: void printSynthSolution( std::ostream& out ); /** + * Do quantifier elimination, doFull false means just output one disjunct + */ + Expr doQuantifierElimination(const Expr& e, bool doFull); + + /** * Get an unsatisfiable core (only if immediately preceded by an * UNSAT or VALID query). Only permitted if CVC4 was built with * unsat-core support and produce-unsat-cores is on. |