1 /********************* */
2 /*! \file quantifiers_engine.cpp
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Morgan Deters, Mathias Preiner
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** \brief Implementation of quantifiers engine class
15 #include "theory/quantifiers_engine.h"
17 #include "options/quantifiers_options.h"
18 #include "options/uf_options.h"
19 #include "smt/smt_engine_scope.h"
20 #include "smt/smt_statistics_registry.h"
21 #include "theory/quantifiers/fmf/full_model_check.h"
22 #include "theory/quantifiers/quantifiers_modules.h"
23 #include "theory/quantifiers/quantifiers_rewriter.h"
24 #include "theory/theory_engine.h"
25 #include "theory/uf/equality_engine.h"
28 using namespace CVC4::kind
;
33 QuantifiersEngine::QuantifiersEngine(TheoryEngine
* te
, DecisionManager
& dm
,
34 ProofNodeManager
* pnm
)
36 d_context(te
->getSatContext()),
37 d_userContext(te
->getUserContext()),
39 d_masterEqualityEngine(nullptr),
41 new quantifiers::EqualityQueryQuantifiersEngine(d_context
, this)),
42 d_tr_trie(new inst::TriggerTrie
),
46 d_term_util(new quantifiers::TermUtil(this)),
47 d_term_canon(new expr::TermCanonize
),
48 d_term_db(new quantifiers::TermDb(d_context
, d_userContext
, this)),
50 d_quant_attr(new quantifiers::QuantAttributes(this)),
51 d_instantiate(new quantifiers::Instantiate(this, d_userContext
, pnm
)),
52 d_skolemize(new quantifiers::Skolemize(this, d_userContext
)),
53 d_term_enum(new quantifiers::TermEnumeration
),
54 d_conflict_c(d_context
, false),
55 d_quants_prereg(d_userContext
),
56 d_quants_red(d_userContext
),
57 d_lemmas_produced_c(d_userContext
),
58 d_ierCounter_c(d_context
),
59 d_presolve(d_userContext
, true),
60 d_presolve_in(d_userContext
),
61 d_presolve_cache(d_userContext
),
62 d_presolve_cache_wq(d_userContext
),
63 d_presolve_cache_wic(d_userContext
)
66 d_util
.push_back(d_eq_query
.get());
67 // term util must come before the other utilities
68 d_util
.push_back(d_term_util
.get());
69 d_util
.push_back(d_term_db
.get());
71 if (options::sygus() || options::sygusInst())
73 // must be constructed here since it is required for datatypes finistInit
74 d_sygus_tdb
.reset(new quantifiers::TermDbSygus(d_context
, this));
77 d_util
.push_back(d_instantiate
.get());
79 d_curr_effort_level
= QuantifiersModule::QEFFORT_NONE
;
81 d_hasAddedLemma
= false;
82 //don't add true lemma
83 d_lemmas_produced_c
[d_term_util
->d_true
] = true;
85 Trace("quant-engine-debug") << "Initialize quantifiers engine." << std::endl
;
86 Trace("quant-engine-debug") << "Initialize model, mbqi : " << options::mbqiMode() << std::endl
;
88 if( options::quantEpr() ){
89 Assert(!options::incrementalSolving());
90 d_qepr
.reset(new quantifiers::QuantEPR
);
94 //allow theory combination to go first, once initially
95 d_ierCounter
= options::instWhenTcFirst() ? 0 : 1;
96 d_ierCounter_c
= d_ierCounter
;
98 d_ierCounterLastLc
= 0;
99 d_inst_when_phase
= 1 + ( options::instWhenPhase()<1 ? 1 : options::instWhenPhase() );
101 // Finite model finding requires specialized ways of building the model.
102 // We require constructing the model and model builder here, since it is
103 // required for initializing the CombinationEngine.
104 if (options::finiteModelFind() || options::fmfBound())
106 Trace("quant-engine-debug") << "Initialize model engine, mbqi : " << options::mbqiMode() << " " << options::fmfBound() << std::endl
;
107 if (options::mbqiMode() == options::MbqiMode::FMC
108 || options::mbqiMode() == options::MbqiMode::TRUST
109 || options::fmfBound())
111 Trace("quant-engine-debug") << "...make fmc builder." << std::endl
;
112 d_model
.reset(new quantifiers::fmcheck::FirstOrderModelFmc(
113 this, d_context
, "FirstOrderModelFmc"));
115 new quantifiers::fmcheck::FullModelChecker(d_context
, this));
117 Trace("quant-engine-debug") << "...make default model builder." << std::endl
;
119 new quantifiers::FirstOrderModel(this, d_context
, "FirstOrderModel"));
120 d_builder
.reset(new quantifiers::QModelBuilder(d_context
, this));
124 new quantifiers::FirstOrderModel(this, d_context
, "FirstOrderModel"));
128 QuantifiersEngine::~QuantifiersEngine() {}
130 void QuantifiersEngine::finishInit()
132 // Initialize the modules and the utilities here. We delay their
133 // initialization to here, since this is after TheoryQuantifiers finishInit,
134 // which has initialized the state and inference manager of this engine.
135 d_qmodules
.reset(new quantifiers::QuantifiersModules
);
136 d_qmodules
->initialize(this, d_context
, d_modules
);
137 if (d_qmodules
->d_rel_dom
.get())
139 d_util
.push_back(d_qmodules
->d_rel_dom
.get());
143 void QuantifiersEngine::setMasterEqualityEngine(eq::EqualityEngine
* mee
)
145 d_masterEqualityEngine
= mee
;
148 TheoryEngine
* QuantifiersEngine::getTheoryEngine() const { return d_te
; }
150 DecisionManager
* QuantifiersEngine::getDecisionManager()
152 return &d_decManager
;
155 context::Context
* QuantifiersEngine::getSatContext() { return d_context
; }
157 context::UserContext
* QuantifiersEngine::getUserContext()
159 return d_userContext
;
162 OutputChannel
& QuantifiersEngine::getOutputChannel()
164 return d_te
->theoryOf(THEORY_QUANTIFIERS
)->getOutputChannel();
166 /** get default valuation for the quantifiers engine */
167 Valuation
& QuantifiersEngine::getValuation()
169 return d_te
->theoryOf(THEORY_QUANTIFIERS
)->getValuation();
172 const LogicInfo
& QuantifiersEngine::getLogicInfo() const
174 return d_te
->getLogicInfo();
177 EqualityQuery
* QuantifiersEngine::getEqualityQuery() const
179 return d_eq_query
.get();
181 quantifiers::QModelBuilder
* QuantifiersEngine::getModelBuilder() const
183 return d_builder
.get();
185 quantifiers::QuantEPR
* QuantifiersEngine::getQuantEPR() const
189 quantifiers::FirstOrderModel
* QuantifiersEngine::getModel() const
191 return d_model
.get();
193 quantifiers::TermDb
* QuantifiersEngine::getTermDatabase() const
195 return d_term_db
.get();
197 quantifiers::TermDbSygus
* QuantifiersEngine::getTermDatabaseSygus() const
199 return d_sygus_tdb
.get();
201 quantifiers::TermUtil
* QuantifiersEngine::getTermUtil() const
203 return d_term_util
.get();
205 expr::TermCanonize
* QuantifiersEngine::getTermCanonize() const
207 return d_term_canon
.get();
209 quantifiers::QuantAttributes
* QuantifiersEngine::getQuantAttributes() const
211 return d_quant_attr
.get();
213 quantifiers::Instantiate
* QuantifiersEngine::getInstantiate() const
215 return d_instantiate
.get();
217 quantifiers::Skolemize
* QuantifiersEngine::getSkolemize() const
219 return d_skolemize
.get();
221 quantifiers::TermEnumeration
* QuantifiersEngine::getTermEnumeration() const
223 return d_term_enum
.get();
225 inst::TriggerTrie
* QuantifiersEngine::getTriggerDatabase() const
227 return d_tr_trie
.get();
230 QuantifiersModule
* QuantifiersEngine::getOwner( Node q
) {
231 std::map
< Node
, QuantifiersModule
* >::iterator it
= d_owner
.find( q
);
232 if( it
==d_owner
.end() ){
239 void QuantifiersEngine::setOwner( Node q
, QuantifiersModule
* m
, int priority
) {
240 QuantifiersModule
* mo
= getOwner( q
);
243 if( priority
<=d_owner_priority
[q
] ){
244 Trace("quant-warn") << "WARNING: setting owner of " << q
<< " to " << ( m
? m
->identify() : "null" ) << ", but already has owner " << mo
->identify() << " with higher priority!" << std::endl
;
249 d_owner_priority
[q
] = priority
;
253 void QuantifiersEngine::setOwner(Node q
, quantifiers::QAttributes
& qa
)
255 if (qa
.d_sygus
|| (options::sygusRecFun() && !qa
.d_fundef_f
.isNull()))
257 if (d_qmodules
->d_synth_e
.get() == nullptr)
259 Trace("quant-warn") << "WARNING : synth engine is null, and we have : "
262 // set synth engine as owner since this is either a conjecture or a function
263 // definition to be used by sygus
264 setOwner(q
, d_qmodules
->d_synth_e
.get(), 2);
268 bool QuantifiersEngine::hasOwnership( Node q
, QuantifiersModule
* m
) {
269 QuantifiersModule
* mo
= getOwner( q
);
270 return mo
==m
|| mo
==NULL
;
273 bool QuantifiersEngine::isFiniteBound(Node q
, Node v
) const
275 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
276 if (bi
&& bi
->isBound(q
, v
))
280 TypeNode tn
= v
.getType();
281 if (tn
.isSort() && options::finiteModelFind())
285 else if (d_term_enum
->mayComplete(tn
))
292 BoundVarType
QuantifiersEngine::getBoundVarType(Node q
, Node v
) const
294 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
297 return bi
->getBoundVarType(q
, v
);
299 return isFiniteBound(q
, v
) ? BOUND_FINITE
: BOUND_NONE
;
302 void QuantifiersEngine::getBoundVarIndices(Node q
,
303 std::vector
<unsigned>& indices
) const
305 Assert(indices
.empty());
306 // we take the bounded variables first
307 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
310 bi
->getBoundVarIndices(q
, indices
);
312 // then get the remaining ones
313 for (unsigned i
= 0, nvars
= q
[0].getNumChildren(); i
< nvars
; i
++)
315 if (std::find(indices
.begin(), indices
.end(), i
) == indices
.end())
317 indices
.push_back(i
);
322 bool QuantifiersEngine::getBoundElements(RepSetIterator
* rsi
,
326 std::vector
<Node
>& elements
) const
328 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
331 return bi
->getBoundElements(rsi
, initial
, q
, v
, elements
);
336 void QuantifiersEngine::presolve() {
337 Trace("quant-engine-proc") << "QuantifiersEngine : presolve " << std::endl
;
338 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
339 d_modules
[i
]->presolve();
341 d_term_db
->presolve();
343 //add all terms to database
344 if( options::incrementalSolving() ){
345 Trace("quant-engine-proc") << "Add presolve cache " << d_presolve_cache
.size() << std::endl
;
346 for( unsigned i
=0; i
<d_presolve_cache
.size(); i
++ ){
347 addTermToDatabase( d_presolve_cache
[i
], d_presolve_cache_wq
[i
], d_presolve_cache_wic
[i
] );
349 Trace("quant-engine-proc") << "Done add presolve cache " << std::endl
;
353 void QuantifiersEngine::ppNotifyAssertions(
354 const std::vector
<Node
>& assertions
) {
355 Trace("quant-engine-proc")
356 << "ppNotifyAssertions in QE, #assertions = " << assertions
.size()
357 << " check epr = " << (d_qepr
!= NULL
) << std::endl
;
358 if (options::instLevelInputOnly() && options::instMaxLevel() != -1)
360 for (const Node
& a
: assertions
)
362 quantifiers::QuantAttributes::setInstantiationLevelAttr(a
, 0);
365 if (options::sygus())
367 quantifiers::SynthEngine
* sye
= d_qmodules
->d_synth_e
.get();
368 for (const Node
& a
: assertions
)
370 sye
->preregisterAssertion(a
);
375 void QuantifiersEngine::check( Theory::Effort e
){
376 CodeTimer
codeTimer(d_statistics
.d_time
);
378 if (!getMasterEqualityEngine()->consistent())
380 Trace("quant-engine-debug") << "Master equality engine not consistent, return." << std::endl
;
383 if (d_conflict_c
.get())
385 if (e
< Theory::EFFORT_LAST_CALL
)
387 // this can happen in rare cases when quantifiers is the first to realize
388 // there is a quantifier-free conflict, for example, when it discovers
389 // disequal and congruent terms in the master equality engine during
390 // term indexing. In such cases, quantifiers reports a "conflicting lemma"
391 // that is, one that is entailed to be false by the current assignment.
392 // If this lemma is not a SAT conflict, we may get another call to full
393 // effort check and the quantifier-free solvers still haven't realized
394 // there is a conflict. In this case, we return, trusting that theory
395 // combination will do the right thing (split on equalities until there is
396 // a conflict at the quantifier-free level).
397 Trace("quant-engine-debug")
398 << "Conflicting lemma already reported by quantifiers, return."
402 // we reported what we thought was a conflicting lemma, but now we have
403 // gotten a check at LAST_CALL effort, indicating that the lemma we reported
404 // was not conflicting. This should never happen, but in production mode, we
405 // proceed with the check.
408 bool needsCheck
= !d_lemmas_waiting
.empty();
409 QuantifiersModule::QEffort needsModelE
= QuantifiersModule::QEFFORT_NONE
;
410 std::vector
< QuantifiersModule
* > qm
;
411 if( d_model
->checkNeeded() ){
412 needsCheck
= needsCheck
|| e
>=Theory::EFFORT_LAST_CALL
; //always need to check at or above last call
413 for (QuantifiersModule
*& mdl
: d_modules
)
415 if (mdl
->needsCheck(e
))
419 //can only request model at last call since theory combination can find inconsistencies
420 if( e
>=Theory::EFFORT_LAST_CALL
){
421 QuantifiersModule::QEffort me
= mdl
->needsModel(e
);
422 needsModelE
= me
<needsModelE
? me
: needsModelE
;
429 d_hasAddedLemma
= false;
430 bool setIncomplete
= false;
432 Trace("quant-engine-debug2") << "Quantifiers Engine call to check, level = " << e
<< ", needsCheck=" << needsCheck
<< std::endl
;
434 //flush previous lemmas (for instance, if was interrupted), or other lemmas to process
436 if( d_hasAddedLemma
){
441 if( Trace
.isOn("quant-engine") ){
442 clSet
= double(clock())/double(CLOCKS_PER_SEC
);
443 Trace("quant-engine") << ">>>>> Quantifiers Engine Round, effort = " << e
<< " <<<<<" << std::endl
;
446 if( Trace
.isOn("quant-engine-debug") ){
447 Trace("quant-engine-debug") << "Quantifiers Engine check, level = " << e
<< std::endl
;
448 Trace("quant-engine-debug") << " depth : " << d_ierCounter_c
<< std::endl
;
449 Trace("quant-engine-debug") << " modules to check : ";
450 for( unsigned i
=0; i
<qm
.size(); i
++ ){
451 Trace("quant-engine-debug") << qm
[i
]->identify() << " ";
453 Trace("quant-engine-debug") << std::endl
;
454 Trace("quant-engine-debug") << " # quantified formulas = " << d_model
->getNumAssertedQuantifiers() << std::endl
;
455 if( !d_lemmas_waiting
.empty() ){
456 Trace("quant-engine-debug") << " lemmas waiting = " << d_lemmas_waiting
.size() << std::endl
;
458 Trace("quant-engine-debug")
459 << " Theory engine finished : " << !theoryEngineNeedsCheck()
461 Trace("quant-engine-debug") << " Needs model effort : " << needsModelE
<< std::endl
;
462 Trace("quant-engine-debug")
463 << " In conflict : " << d_conflict
<< std::endl
;
465 if( Trace
.isOn("quant-engine-ee-pre") ){
466 Trace("quant-engine-ee-pre") << "Equality engine (pre-inference): " << std::endl
;
467 debugPrintEqualityEngine( "quant-engine-ee-pre" );
469 if( Trace
.isOn("quant-engine-assert") ){
470 Trace("quant-engine-assert") << "Assertions : " << std::endl
;
471 getTheoryEngine()->printAssertions("quant-engine-assert");
475 Trace("quant-engine-debug") << "Resetting all utilities..." << std::endl
;
476 for (QuantifiersUtil
*& util
: d_util
)
478 Trace("quant-engine-debug2") << "Reset " << util
->identify().c_str()
479 << "..." << std::endl
;
483 if( d_hasAddedLemma
){
486 //should only fail reset if added a lemma
492 if( Trace
.isOn("quant-engine-ee") ){
493 Trace("quant-engine-ee") << "Equality engine : " << std::endl
;
494 debugPrintEqualityEngine( "quant-engine-ee" );
498 Trace("quant-engine-debug") << "Reset model..." << std::endl
;
499 d_model
->reset_round();
502 Trace("quant-engine-debug") << "Resetting all modules..." << std::endl
;
503 for (QuantifiersModule
*& mdl
: d_modules
)
505 Trace("quant-engine-debug2") << "Reset " << mdl
->identify().c_str()
509 Trace("quant-engine-debug") << "Done resetting all modules." << std::endl
;
510 //reset may have added lemmas
512 if( d_hasAddedLemma
){
516 if( e
==Theory::EFFORT_LAST_CALL
){
517 ++(d_statistics
.d_instantiation_rounds_lc
);
518 }else if( e
==Theory::EFFORT_FULL
){
519 ++(d_statistics
.d_instantiation_rounds
);
521 Trace("quant-engine-debug") << "Check modules that needed check..." << std::endl
;
522 for (unsigned qef
= QuantifiersModule::QEFFORT_CONFLICT
;
523 qef
<= QuantifiersModule::QEFFORT_LAST_CALL
;
526 QuantifiersModule::QEffort quant_e
=
527 static_cast<QuantifiersModule::QEffort
>(qef
);
528 d_curr_effort_level
= quant_e
;
529 // Force the theory engine to build the model if any module requested it.
530 if (needsModelE
== quant_e
)
532 Trace("quant-engine-debug") << "Build model..." << std::endl
;
533 if (!d_te
->buildModel())
535 // If we failed to build the model, flush all pending lemmas and
541 if( !d_hasAddedLemma
){
543 for (QuantifiersModule
*& mdl
: qm
)
545 Trace("quant-engine-debug") << "Check " << mdl
->identify().c_str()
546 << " at effort " << quant_e
<< "..."
548 mdl
->check(e
, quant_e
);
550 Trace("quant-engine-debug") << "...conflict!" << std::endl
;
554 //flush all current lemmas
557 //if we have added one, stop
558 if( d_hasAddedLemma
){
562 if (quant_e
== QuantifiersModule::QEFFORT_CONFLICT
)
564 if( e
==Theory::EFFORT_FULL
){
565 //increment if a last call happened, we are not strictly enforcing interleaving, or already were in phase
566 if( d_ierCounterLastLc
!=d_ierCounter_lc
|| !options::instWhenStrictInterleave() || d_ierCounter
%d_inst_when_phase
!=0 ){
567 d_ierCounter
= d_ierCounter
+ 1;
568 d_ierCounterLastLc
= d_ierCounter_lc
;
569 d_ierCounter_c
= d_ierCounter_c
.get() + 1;
571 }else if( e
==Theory::EFFORT_LAST_CALL
){
572 d_ierCounter_lc
= d_ierCounter_lc
+ 1;
575 else if (quant_e
== QuantifiersModule::QEFFORT_MODEL
)
577 if( e
==Theory::EFFORT_LAST_CALL
){
578 //sources of incompleteness
579 for (QuantifiersUtil
*& util
: d_util
)
581 if (!util
->checkComplete())
583 Trace("quant-engine-debug") << "Set incomplete because utility "
584 << util
->identify().c_str()
585 << " was incomplete." << std::endl
;
586 setIncomplete
= true;
589 if (d_conflict_c
.get())
591 // we reported a conflicting lemma, should return
592 setIncomplete
= true;
594 //if we have a chance not to set incomplete
595 if( !setIncomplete
){
596 //check if we should set the incomplete flag
597 for (QuantifiersModule
*& mdl
: d_modules
)
599 if (!mdl
->checkComplete())
601 Trace("quant-engine-debug")
602 << "Set incomplete because module "
603 << mdl
->identify().c_str() << " was incomplete."
605 setIncomplete
= true;
609 if( !setIncomplete
){
610 //look at individual quantified formulas, one module must claim completeness for each one
611 for( unsigned i
=0; i
<d_model
->getNumAssertedQuantifiers(); i
++ ){
612 bool hasCompleteM
= false;
613 Node q
= d_model
->getAssertedQuantifier( i
);
614 QuantifiersModule
* qmd
= getOwner( q
);
616 hasCompleteM
= qmd
->checkCompleteFor( q
);
618 for( unsigned j
=0; j
<d_modules
.size(); j
++ ){
619 if( d_modules
[j
]->checkCompleteFor( q
) ){
627 Trace("quant-engine-debug") << "Set incomplete because " << q
<< " was not fully processed." << std::endl
;
628 setIncomplete
= true;
632 Trace("quant-engine-debug2") << "Complete for " << q
<< " due to " << qmd
->identify().c_str() << std::endl
;
637 //if setIncomplete = false, we will answer SAT, otherwise we will run at quant_e QEFFORT_LAST_CALL
638 if( !setIncomplete
){
645 d_curr_effort_level
= QuantifiersModule::QEFFORT_NONE
;
646 Trace("quant-engine-debug") << "Done check modules that needed check." << std::endl
;
650 bool debugInstTrace
= Trace
.isOn("inst-per-quant-round");
651 if (options::debugInst() || debugInstTrace
)
653 Options
& sopts
= smt::currentSmtEngine()->getOptions();
654 std::ostream
& out
= *sopts
.getOut();
655 d_instantiate
->debugPrint(out
);
658 if( Trace
.isOn("quant-engine") ){
659 double clSet2
= double(clock())/double(CLOCKS_PER_SEC
);
660 Trace("quant-engine") << "Finished quantifiers engine, total time = " << (clSet2
-clSet
);
661 Trace("quant-engine") << ", added lemma = " << d_hasAddedLemma
;
662 Trace("quant-engine") << std::endl
;
665 Trace("quant-engine-debug2") << "Finished quantifiers engine check." << std::endl
;
667 Trace("quant-engine-debug2") << "Quantifiers Engine does not need check." << std::endl
;
671 if( e
==Theory::EFFORT_LAST_CALL
&& !d_hasAddedLemma
){
673 Trace("quant-engine") << "Set incomplete flag." << std::endl
;
674 getOutputChannel().setIncomplete();
677 d_instantiate
->debugPrintModel();
681 void QuantifiersEngine::notifyCombineTheories() {
682 //if allowing theory combination to happen at most once between instantiation rounds
684 //d_ierCounterLastLc = -1;
687 bool QuantifiersEngine::reduceQuantifier( Node q
) {
688 //TODO: this can be unified with preregistration: AlphaEquivalence takes ownership of reducable quants
689 BoolMap::const_iterator it
= d_quants_red
.find( q
);
690 if( it
==d_quants_red
.end() ){
692 std::map
< Node
, Node
>::iterator itr
= d_quants_red_lem
.find( q
);
693 if( itr
==d_quants_red_lem
.end() ){
694 if (d_qmodules
->d_alpha_equiv
)
696 Trace("quant-engine-red") << "Alpha equivalence " << q
<< "?" << std::endl
;
697 //add equivalence with another quantified formula
698 lem
= d_qmodules
->d_alpha_equiv
->reduceQuantifier(q
);
700 Trace("quant-engine-red") << "...alpha equivalence success." << std::endl
;
701 ++(d_statistics
.d_red_alpha_equiv
);
704 d_quants_red_lem
[q
] = lem
;
709 getOutputChannel().lemma( lem
);
711 d_quants_red
[q
] = !lem
.isNull();
712 return !lem
.isNull();
718 void QuantifiersEngine::registerQuantifierInternal(Node f
)
720 std::map
< Node
, bool >::iterator it
= d_quants
.find( f
);
721 if( it
==d_quants
.end() ){
722 Trace("quant") << "QuantifiersEngine : Register quantifier ";
723 Trace("quant") << " : " << f
<< std::endl
;
724 unsigned prev_lemma_waiting
= d_lemmas_waiting
.size();
725 ++(d_statistics
.d_num_quant
);
726 Assert(f
.getKind() == FORALL
);
727 // register with utilities
728 for (unsigned i
= 0; i
< d_util
.size(); i
++)
730 d_util
[i
]->registerQuantifier(f
);
732 // compute attributes
733 d_quant_attr
->computeAttributes(f
);
735 for (QuantifiersModule
*& mdl
: d_modules
)
737 Trace("quant-debug") << "check ownership with " << mdl
->identify()
738 << "..." << std::endl
;
739 mdl
->checkOwnership(f
);
741 QuantifiersModule
* qm
= getOwner(f
);
742 Trace("quant") << " Owner : " << (qm
== nullptr ? "[none]" : qm
->identify())
744 // register with each module
745 for (QuantifiersModule
*& mdl
: d_modules
)
747 Trace("quant-debug") << "register with " << mdl
->identify() << "..."
749 mdl
->registerQuantifier(f
);
750 // since this is context-independent, we should not add any lemmas during
752 Assert(d_lemmas_waiting
.size() == prev_lemma_waiting
);
754 Trace("quant-debug") << "...finish." << std::endl
;
756 AlwaysAssert(d_lemmas_waiting
.size() == prev_lemma_waiting
);
760 void QuantifiersEngine::preRegisterQuantifier(Node q
)
762 NodeSet::const_iterator it
= d_quants_prereg
.find(q
);
763 if (it
!= d_quants_prereg
.end())
767 Trace("quant-debug") << "QuantifiersEngine : Pre-register " << q
<< std::endl
;
768 d_quants_prereg
.insert(q
);
770 if (reduceQuantifier(q
))
772 // if we can reduce it, nothing left to do
775 // ensure that it is registered
776 registerQuantifierInternal(q
);
777 // register with each module
778 for (QuantifiersModule
*& mdl
: d_modules
)
780 Trace("quant-debug") << "pre-register with " << mdl
->identify() << "..."
782 mdl
->preRegisterQuantifier(q
);
786 Trace("quant-debug") << "...finish pre-register " << q
<< "..." << std::endl
;
789 void QuantifiersEngine::registerPattern( std::vector
<Node
> & pattern
) {
790 for(std::vector
<Node
>::iterator p
= pattern
.begin(); p
!= pattern
.end(); ++p
){
791 std::set
< Node
> added
;
792 getTermDatabase()->addTerm( *p
, added
);
796 void QuantifiersEngine::assertQuantifier( Node f
, bool pol
){
797 if (reduceQuantifier(f
))
799 // if we can reduce it, nothing left to do
804 Node lem
= d_skolemize
->process(f
);
807 if (Trace
.isOn("quantifiers-sk-debug"))
809 Node slem
= Rewriter::rewrite(lem
);
810 Trace("quantifiers-sk-debug")
811 << "Skolemize lemma : " << slem
<< std::endl
;
813 getOutputChannel().lemma(
814 lem
, LemmaProperty::PREPROCESS
| LemmaProperty::NEEDS_JUSTIFY
);
818 // ensure the quantified formula is registered
819 registerQuantifierInternal(f
);
820 // assert it to each module
821 d_model
->assertQuantifier(f
);
822 for (QuantifiersModule
*& mdl
: d_modules
)
826 addTermToDatabase(d_term_util
->getInstConstantBody(f
), true);
829 void QuantifiersEngine::addTermToDatabase( Node n
, bool withinQuant
, bool withinInstClosure
){
830 if( options::incrementalSolving() ){
831 if( d_presolve_in
.find( n
)==d_presolve_in
.end() ){
832 d_presolve_in
.insert( n
);
833 d_presolve_cache
.push_back( n
);
834 d_presolve_cache_wq
.push_back( withinQuant
);
835 d_presolve_cache_wic
.push_back( withinInstClosure
);
838 //only wait if we are doing incremental solving
839 if( !d_presolve
|| !options::incrementalSolving() ){
840 std::set
< Node
> added
;
841 d_term_db
->addTerm(n
, added
, withinQuant
, withinInstClosure
);
845 if (d_sygus_tdb
&& options::sygusEvalUnfold())
847 d_sygus_tdb
->getEvalUnfold()->registerEvalTerm(n
);
853 void QuantifiersEngine::eqNotifyNewClass(TNode t
) {
854 addTermToDatabase( t
);
857 bool QuantifiersEngine::addLemma( Node lem
, bool doCache
, bool doRewrite
){
860 lem
= Rewriter::rewrite(lem
);
862 Trace("inst-add-debug") << "Adding lemma : " << lem
<< std::endl
;
863 BoolMap::const_iterator itp
= d_lemmas_produced_c
.find( lem
);
864 if( itp
==d_lemmas_produced_c
.end() || !(*itp
).second
){
865 d_lemmas_produced_c
[ lem
] = true;
866 d_lemmas_waiting
.push_back( lem
);
867 Trace("inst-add-debug") << "Added lemma" << std::endl
;
870 Trace("inst-add-debug") << "Duplicate." << std::endl
;
874 //do not need to rewrite, will be rewritten after sending
875 d_lemmas_waiting
.push_back( lem
);
880 bool QuantifiersEngine::addTrustedLemma(TrustNode tlem
,
884 Node lem
= tlem
.getProven();
885 if (!addLemma(lem
, doCache
, doRewrite
))
889 d_lemmasWaitingPg
[lem
] = tlem
.getGenerator();
893 bool QuantifiersEngine::removeLemma( Node lem
) {
894 std::vector
< Node
>::iterator it
= std::find( d_lemmas_waiting
.begin(), d_lemmas_waiting
.end(), lem
);
895 if( it
!=d_lemmas_waiting
.end() ){
896 d_lemmas_waiting
.erase( it
, it
+ 1 );
897 d_lemmas_produced_c
[ lem
] = false;
904 void QuantifiersEngine::addRequirePhase( Node lit
, bool req
){
905 d_phase_req_waiting
[lit
] = req
;
908 void QuantifiersEngine::markRelevant( Node q
) {
909 d_model
->markRelevant( q
);
911 bool QuantifiersEngine::hasAddedLemma() const
913 return !d_lemmas_waiting
.empty() || d_hasAddedLemma
;
915 bool QuantifiersEngine::theoryEngineNeedsCheck() const
917 return d_te
->needCheck();
920 void QuantifiersEngine::setConflict()
926 bool QuantifiersEngine::getInstWhenNeedsCheck( Theory::Effort e
) {
927 Trace("quant-engine-debug2") << "Get inst when needs check, counts=" << d_ierCounter
<< ", " << d_ierCounter_lc
<< std::endl
;
928 //determine if we should perform check, based on instWhenMode
929 bool performCheck
= false;
930 if (options::instWhenMode() == options::InstWhenMode::FULL
)
932 performCheck
= ( e
>= Theory::EFFORT_FULL
);
934 else if (options::instWhenMode() == options::InstWhenMode::FULL_DELAY
)
936 performCheck
= (e
>= Theory::EFFORT_FULL
) && !theoryEngineNeedsCheck();
938 else if (options::instWhenMode() == options::InstWhenMode::FULL_LAST_CALL
)
940 performCheck
= ( ( e
==Theory::EFFORT_FULL
&& d_ierCounter
%d_inst_when_phase
!=0 ) || e
==Theory::EFFORT_LAST_CALL
);
942 else if (options::instWhenMode()
943 == options::InstWhenMode::FULL_DELAY_LAST_CALL
)
945 performCheck
= ((e
== Theory::EFFORT_FULL
&& !theoryEngineNeedsCheck()
946 && d_ierCounter
% d_inst_when_phase
!= 0)
947 || e
== Theory::EFFORT_LAST_CALL
);
949 else if (options::instWhenMode() == options::InstWhenMode::LAST_CALL
)
951 performCheck
= ( e
>= Theory::EFFORT_LAST_CALL
);
960 options::UserPatMode
QuantifiersEngine::getInstUserPatMode()
962 if (options::userPatternsQuant() == options::UserPatMode::INTERLEAVE
)
964 return d_ierCounter
% 2 == 0 ? options::UserPatMode::USE
965 : options::UserPatMode::RESORT
;
969 return options::userPatternsQuant();
973 void QuantifiersEngine::flushLemmas(){
974 OutputChannel
& out
= getOutputChannel();
975 if( !d_lemmas_waiting
.empty() ){
976 //take default output channel if none is provided
977 d_hasAddedLemma
= true;
978 std::map
<Node
, ProofGenerator
*>::iterator itp
;
979 for (const Node
& lemw
: d_lemmas_waiting
)
981 Trace("qe-lemma") << "Lemma : " << lemw
<< std::endl
;
982 itp
= d_lemmasWaitingPg
.find(lemw
);
983 if (itp
!= d_lemmasWaitingPg
.end())
985 TrustNode tlemw
= TrustNode::mkTrustLemma(lemw
, itp
->second
);
986 out
.trustedLemma(tlemw
, LemmaProperty::PREPROCESS
);
990 out
.lemma(lemw
, LemmaProperty::PREPROCESS
);
993 d_lemmas_waiting
.clear();
995 if( !d_phase_req_waiting
.empty() ){
996 for( std::map
< Node
, bool >::iterator it
= d_phase_req_waiting
.begin(); it
!= d_phase_req_waiting
.end(); ++it
){
997 Trace("qe-lemma") << "Require phase : " << it
->first
<< " -> " << it
->second
<< std::endl
;
998 out
.requirePhase(it
->first
, it
->second
);
1000 d_phase_req_waiting
.clear();
1004 bool QuantifiersEngine::getUnsatCoreLemmas( std::vector
< Node
>& active_lemmas
) {
1005 return d_instantiate
->getUnsatCoreLemmas(active_lemmas
);
1008 void QuantifiersEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
1009 d_instantiate
->getInstantiationTermVectors(q
, tvecs
);
1012 void QuantifiersEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
1013 d_instantiate
->getInstantiationTermVectors(insts
);
1016 void QuantifiersEngine::getExplanationForInstLemmas(
1017 const std::vector
<Node
>& lems
,
1018 std::map
<Node
, Node
>& quant
,
1019 std::map
<Node
, std::vector
<Node
> >& tvec
)
1021 d_instantiate
->getExplanationForInstLemmas(lems
, quant
, tvec
);
1024 void QuantifiersEngine::printInstantiations( std::ostream
& out
) {
1025 bool printed
= false;
1026 // print the skolemizations
1027 if (options::printInstMode() == options::PrintInstMode::LIST
)
1029 if (d_skolemize
->printSkolemization(out
))
1034 // print the instantiations
1035 if (d_instantiate
->printInstantiations(out
))
1040 out
<< "No instantiations" << std::endl
;
1044 void QuantifiersEngine::printSynthSolution( std::ostream
& out
) {
1045 if (d_qmodules
->d_synth_e
)
1047 d_qmodules
->d_synth_e
->printSynthSolution(out
);
1049 out
<< "Internal error : module for synth solution not found." << std::endl
;
1053 void QuantifiersEngine::getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
) {
1054 d_instantiate
->getInstantiatedQuantifiedFormulas(qs
);
1057 void QuantifiersEngine::getInstantiations( std::map
< Node
, std::vector
< Node
> >& insts
) {
1058 d_instantiate
->getInstantiations(insts
);
1061 void QuantifiersEngine::getInstantiations( Node q
, std::vector
< Node
>& insts
) {
1062 d_instantiate
->getInstantiations(q
, insts
);
1065 Node
QuantifiersEngine::getInstantiatedConjunction( Node q
) {
1066 return d_instantiate
->getInstantiatedConjunction(q
);
1069 QuantifiersEngine::Statistics::Statistics()
1070 : d_time("theory::QuantifiersEngine::time"),
1071 d_qcf_time("theory::QuantifiersEngine::time_qcf"),
1072 d_ematching_time("theory::QuantifiersEngine::time_ematching"),
1073 d_num_quant("QuantifiersEngine::Num_Quantifiers", 0),
1074 d_instantiation_rounds("QuantifiersEngine::Rounds_Instantiation_Full", 0),
1075 d_instantiation_rounds_lc("QuantifiersEngine::Rounds_Instantiation_Last_Call", 0),
1076 d_triggers("QuantifiersEngine::Triggers", 0),
1077 d_simple_triggers("QuantifiersEngine::Triggers_Simple", 0),
1078 d_multi_triggers("QuantifiersEngine::Triggers_Multi", 0),
1079 d_multi_trigger_instantiations("QuantifiersEngine::Multi_Trigger_Instantiations", 0),
1080 d_red_alpha_equiv("QuantifiersEngine::Reductions_Alpha_Equivalence", 0),
1081 d_instantiations_user_patterns("QuantifiersEngine::Instantiations_User_Patterns", 0),
1082 d_instantiations_auto_gen("QuantifiersEngine::Instantiations_Auto_Gen", 0),
1083 d_instantiations_guess("QuantifiersEngine::Instantiations_Guess", 0),
1084 d_instantiations_qcf("QuantifiersEngine::Instantiations_Qcf_Conflict", 0),
1085 d_instantiations_qcf_prop("QuantifiersEngine::Instantiations_Qcf_Prop", 0),
1086 d_instantiations_fmf_exh("QuantifiersEngine::Instantiations_Fmf_Exh", 0),
1087 d_instantiations_fmf_mbqi("QuantifiersEngine::Instantiations_Fmf_Mbqi", 0),
1088 d_instantiations_cbqi("QuantifiersEngine::Instantiations_Cbqi", 0),
1089 d_instantiations_rr("QuantifiersEngine::Instantiations_Rewrite_Rules", 0)
1091 smtStatisticsRegistry()->registerStat(&d_time
);
1092 smtStatisticsRegistry()->registerStat(&d_qcf_time
);
1093 smtStatisticsRegistry()->registerStat(&d_ematching_time
);
1094 smtStatisticsRegistry()->registerStat(&d_num_quant
);
1095 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds
);
1096 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds_lc
);
1097 smtStatisticsRegistry()->registerStat(&d_triggers
);
1098 smtStatisticsRegistry()->registerStat(&d_simple_triggers
);
1099 smtStatisticsRegistry()->registerStat(&d_multi_triggers
);
1100 smtStatisticsRegistry()->registerStat(&d_multi_trigger_instantiations
);
1101 smtStatisticsRegistry()->registerStat(&d_red_alpha_equiv
);
1102 smtStatisticsRegistry()->registerStat(&d_instantiations_user_patterns
);
1103 smtStatisticsRegistry()->registerStat(&d_instantiations_auto_gen
);
1104 smtStatisticsRegistry()->registerStat(&d_instantiations_guess
);
1105 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf
);
1106 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf_prop
);
1107 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_exh
);
1108 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_mbqi
);
1109 smtStatisticsRegistry()->registerStat(&d_instantiations_cbqi
);
1110 smtStatisticsRegistry()->registerStat(&d_instantiations_rr
);
1113 QuantifiersEngine::Statistics::~Statistics(){
1114 smtStatisticsRegistry()->unregisterStat(&d_time
);
1115 smtStatisticsRegistry()->unregisterStat(&d_qcf_time
);
1116 smtStatisticsRegistry()->unregisterStat(&d_ematching_time
);
1117 smtStatisticsRegistry()->unregisterStat(&d_num_quant
);
1118 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds
);
1119 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds_lc
);
1120 smtStatisticsRegistry()->unregisterStat(&d_triggers
);
1121 smtStatisticsRegistry()->unregisterStat(&d_simple_triggers
);
1122 smtStatisticsRegistry()->unregisterStat(&d_multi_triggers
);
1123 smtStatisticsRegistry()->unregisterStat(&d_multi_trigger_instantiations
);
1124 smtStatisticsRegistry()->unregisterStat(&d_red_alpha_equiv
);
1125 smtStatisticsRegistry()->unregisterStat(&d_instantiations_user_patterns
);
1126 smtStatisticsRegistry()->unregisterStat(&d_instantiations_auto_gen
);
1127 smtStatisticsRegistry()->unregisterStat(&d_instantiations_guess
);
1128 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf
);
1129 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf_prop
);
1130 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_exh
);
1131 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_mbqi
);
1132 smtStatisticsRegistry()->unregisterStat(&d_instantiations_cbqi
);
1133 smtStatisticsRegistry()->unregisterStat(&d_instantiations_rr
);
1136 eq::EqualityEngine
* QuantifiersEngine::getMasterEqualityEngine() const
1138 return d_masterEqualityEngine
;
1141 Node
QuantifiersEngine::getInternalRepresentative( Node a
, Node q
, int index
){
1142 return d_eq_query
->getInternalRepresentative(a
, q
, index
);
1145 bool QuantifiersEngine::getSynthSolutions(
1146 std::map
<Node
, std::map
<Node
, Node
> >& sol_map
)
1148 return d_qmodules
->d_synth_e
->getSynthSolutions(sol_map
);
1151 void QuantifiersEngine::debugPrintEqualityEngine( const char * c
) {
1152 eq::EqualityEngine
* ee
= getMasterEqualityEngine();
1153 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( ee
);
1154 std::map
< TypeNode
, int > typ_num
;
1155 while( !eqcs_i
.isFinished() ){
1156 TNode r
= (*eqcs_i
);
1157 TypeNode tr
= r
.getType();
1158 if( typ_num
.find( tr
)==typ_num
.end() ){
1162 bool firstTime
= true;
1163 Trace(c
) << " " << r
;
1164 Trace(c
) << " : { ";
1165 eq::EqClassIterator eqc_i
= eq::EqClassIterator( r
, ee
);
1166 while( !eqc_i
.isFinished() ){
1170 Trace(c
) << std::endl
;
1173 Trace(c
) << " " << n
<< std::endl
;
1177 if( !firstTime
){ Trace(c
) << " "; }
1178 Trace(c
) << "}" << std::endl
;
1181 Trace(c
) << std::endl
;
1182 for( std::map
< TypeNode
, int >::iterator it
= typ_num
.begin(); it
!= typ_num
.end(); ++it
){
1183 Trace(c
) << "# eqc for " << it
->first
<< " : " << it
->second
<< std::endl
;
1187 } // namespace theory