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
,
35 ProofNodeManager
* pnm
)
37 d_context(te
->getSatContext()),
38 d_userContext(te
->getUserContext()),
40 d_masterEqualityEngine(nullptr),
42 new quantifiers::EqualityQueryQuantifiersEngine(d_context
, this)),
43 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
, pnm
)),
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
;
90 //allow theory combination to go first, once initially
91 d_ierCounter
= options::instWhenTcFirst() ? 0 : 1;
92 d_ierCounter_c
= d_ierCounter
;
94 d_ierCounterLastLc
= 0;
95 d_inst_when_phase
= 1 + ( options::instWhenPhase()<1 ? 1 : options::instWhenPhase() );
97 // Finite model finding requires specialized ways of building the model.
98 // We require constructing the model and model builder here, since it is
99 // required for initializing the CombinationEngine.
100 if (options::finiteModelFind() || options::fmfBound())
102 Trace("quant-engine-debug") << "Initialize model engine, mbqi : " << options::mbqiMode() << " " << options::fmfBound() << std::endl
;
103 if (options::mbqiMode() == options::MbqiMode::FMC
104 || options::mbqiMode() == options::MbqiMode::TRUST
105 || options::fmfBound())
107 Trace("quant-engine-debug") << "...make fmc builder." << std::endl
;
108 d_model
.reset(new quantifiers::fmcheck::FirstOrderModelFmc(
109 this, d_context
, "FirstOrderModelFmc"));
111 new quantifiers::fmcheck::FullModelChecker(d_context
, this));
113 Trace("quant-engine-debug") << "...make default model builder." << std::endl
;
115 new quantifiers::FirstOrderModel(this, d_context
, "FirstOrderModel"));
116 d_builder
.reset(new quantifiers::QModelBuilder(d_context
, this));
120 new quantifiers::FirstOrderModel(this, d_context
, "FirstOrderModel"));
124 QuantifiersEngine::~QuantifiersEngine() {}
126 void QuantifiersEngine::finishInit()
128 // Initialize the modules and the utilities here. We delay their
129 // initialization to here, since this is after TheoryQuantifiers finishInit,
130 // which has initialized the state and inference manager of this engine.
131 d_qmodules
.reset(new quantifiers::QuantifiersModules
);
132 d_qmodules
->initialize(this, d_context
, d_modules
);
133 if (d_qmodules
->d_rel_dom
.get())
135 d_util
.push_back(d_qmodules
->d_rel_dom
.get());
139 void QuantifiersEngine::setMasterEqualityEngine(eq::EqualityEngine
* mee
)
141 d_masterEqualityEngine
= mee
;
144 TheoryEngine
* QuantifiersEngine::getTheoryEngine() const { return d_te
; }
146 DecisionManager
* QuantifiersEngine::getDecisionManager()
148 return &d_decManager
;
151 context::Context
* QuantifiersEngine::getSatContext() { return d_context
; }
153 context::UserContext
* QuantifiersEngine::getUserContext()
155 return d_userContext
;
158 OutputChannel
& QuantifiersEngine::getOutputChannel()
160 return d_te
->theoryOf(THEORY_QUANTIFIERS
)->getOutputChannel();
162 /** get default valuation for the quantifiers engine */
163 Valuation
& QuantifiersEngine::getValuation()
165 return d_te
->theoryOf(THEORY_QUANTIFIERS
)->getValuation();
168 const LogicInfo
& QuantifiersEngine::getLogicInfo() const
170 return d_te
->getLogicInfo();
173 EqualityQuery
* QuantifiersEngine::getEqualityQuery() const
175 return d_eq_query
.get();
177 quantifiers::QModelBuilder
* QuantifiersEngine::getModelBuilder() const
179 return d_builder
.get();
181 quantifiers::FirstOrderModel
* QuantifiersEngine::getModel() const
183 return d_model
.get();
185 quantifiers::TermDb
* QuantifiersEngine::getTermDatabase() const
187 return d_term_db
.get();
189 quantifiers::TermDbSygus
* QuantifiersEngine::getTermDatabaseSygus() const
191 return d_sygus_tdb
.get();
193 quantifiers::TermUtil
* QuantifiersEngine::getTermUtil() const
195 return d_term_util
.get();
197 expr::TermCanonize
* QuantifiersEngine::getTermCanonize() const
199 return d_term_canon
.get();
201 quantifiers::QuantAttributes
* QuantifiersEngine::getQuantAttributes() const
203 return d_quant_attr
.get();
205 quantifiers::Instantiate
* QuantifiersEngine::getInstantiate() const
207 return d_instantiate
.get();
209 quantifiers::Skolemize
* QuantifiersEngine::getSkolemize() const
211 return d_skolemize
.get();
213 quantifiers::TermEnumeration
* QuantifiersEngine::getTermEnumeration() const
215 return d_term_enum
.get();
217 inst::TriggerTrie
* QuantifiersEngine::getTriggerDatabase() const
219 return d_tr_trie
.get();
222 QuantifiersModule
* QuantifiersEngine::getOwner( Node q
) {
223 std::map
< Node
, QuantifiersModule
* >::iterator it
= d_owner
.find( q
);
224 if( it
==d_owner
.end() ){
231 void QuantifiersEngine::setOwner( Node q
, QuantifiersModule
* m
, int priority
) {
232 QuantifiersModule
* mo
= getOwner( q
);
235 if( priority
<=d_owner_priority
[q
] ){
236 Trace("quant-warn") << "WARNING: setting owner of " << q
<< " to " << ( m
? m
->identify() : "null" ) << ", but already has owner " << mo
->identify() << " with higher priority!" << std::endl
;
241 d_owner_priority
[q
] = priority
;
245 void QuantifiersEngine::setOwner(Node q
, quantifiers::QAttributes
& qa
)
247 if (qa
.d_sygus
|| (options::sygusRecFun() && !qa
.d_fundef_f
.isNull()))
249 if (d_qmodules
->d_synth_e
.get() == nullptr)
251 Trace("quant-warn") << "WARNING : synth engine is null, and we have : "
254 // set synth engine as owner since this is either a conjecture or a function
255 // definition to be used by sygus
256 setOwner(q
, d_qmodules
->d_synth_e
.get(), 2);
260 bool QuantifiersEngine::hasOwnership( Node q
, QuantifiersModule
* m
) {
261 QuantifiersModule
* mo
= getOwner( q
);
262 return mo
==m
|| mo
==NULL
;
265 bool QuantifiersEngine::isFiniteBound(Node q
, Node v
) const
267 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
268 if (bi
&& bi
->isBound(q
, v
))
272 TypeNode tn
= v
.getType();
273 if (tn
.isSort() && options::finiteModelFind())
277 else if (d_term_enum
->mayComplete(tn
))
284 BoundVarType
QuantifiersEngine::getBoundVarType(Node q
, Node v
) const
286 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
289 return bi
->getBoundVarType(q
, v
);
291 return isFiniteBound(q
, v
) ? BOUND_FINITE
: BOUND_NONE
;
294 void QuantifiersEngine::getBoundVarIndices(Node q
,
295 std::vector
<unsigned>& indices
) const
297 Assert(indices
.empty());
298 // we take the bounded variables first
299 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
302 bi
->getBoundVarIndices(q
, indices
);
304 // then get the remaining ones
305 for (unsigned i
= 0, nvars
= q
[0].getNumChildren(); i
< nvars
; i
++)
307 if (std::find(indices
.begin(), indices
.end(), i
) == indices
.end())
309 indices
.push_back(i
);
314 bool QuantifiersEngine::getBoundElements(RepSetIterator
* rsi
,
318 std::vector
<Node
>& elements
) const
320 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
323 return bi
->getBoundElements(rsi
, initial
, q
, v
, elements
);
328 void QuantifiersEngine::presolve() {
329 Trace("quant-engine-proc") << "QuantifiersEngine : presolve " << std::endl
;
330 d_lemmas_waiting
.clear();
331 d_phase_req_waiting
.clear();
332 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
333 d_modules
[i
]->presolve();
335 d_term_db
->presolve();
337 //add all terms to database
338 if( options::incrementalSolving() ){
339 Trace("quant-engine-proc") << "Add presolve cache " << d_presolve_cache
.size() << std::endl
;
340 for( unsigned i
=0; i
<d_presolve_cache
.size(); i
++ ){
341 addTermToDatabase( d_presolve_cache
[i
], d_presolve_cache_wq
[i
], d_presolve_cache_wic
[i
] );
343 Trace("quant-engine-proc") << "Done add presolve cache " << std::endl
;
347 void QuantifiersEngine::ppNotifyAssertions(
348 const std::vector
<Node
>& assertions
) {
349 Trace("quant-engine-proc")
350 << "ppNotifyAssertions in QE, #assertions = " << assertions
.size()
352 if (options::instLevelInputOnly() && options::instMaxLevel() != -1)
354 for (const Node
& a
: assertions
)
356 quantifiers::QuantAttributes::setInstantiationLevelAttr(a
, 0);
359 if (options::sygus())
361 quantifiers::SynthEngine
* sye
= d_qmodules
->d_synth_e
.get();
362 for (const Node
& a
: assertions
)
364 sye
->preregisterAssertion(a
);
367 /* The SyGuS instantiation module needs a global view of all available
368 * assertions to collect global terms that get added to each grammar.
370 if (options::sygusInst())
372 quantifiers::SygusInst
* si
= d_qmodules
->d_sygus_inst
.get();
373 si
->ppNotifyAssertions(assertions
);
377 void QuantifiersEngine::check( Theory::Effort e
){
378 CodeTimer
codeTimer(d_statistics
.d_time
);
380 if (!getMasterEqualityEngine()->consistent())
382 Trace("quant-engine-debug") << "Master equality engine not consistent, return." << std::endl
;
385 if (d_conflict_c
.get())
387 if (e
< Theory::EFFORT_LAST_CALL
)
389 // this can happen in rare cases when quantifiers is the first to realize
390 // there is a quantifier-free conflict, for example, when it discovers
391 // disequal and congruent terms in the master equality engine during
392 // term indexing. In such cases, quantifiers reports a "conflicting lemma"
393 // that is, one that is entailed to be false by the current assignment.
394 // If this lemma is not a SAT conflict, we may get another call to full
395 // effort check and the quantifier-free solvers still haven't realized
396 // there is a conflict. In this case, we return, trusting that theory
397 // combination will do the right thing (split on equalities until there is
398 // a conflict at the quantifier-free level).
399 Trace("quant-engine-debug")
400 << "Conflicting lemma already reported by quantifiers, return."
404 // we reported what we thought was a conflicting lemma, but now we have
405 // gotten a check at LAST_CALL effort, indicating that the lemma we reported
406 // was not conflicting. This should never happen, but in production mode, we
407 // proceed with the check.
410 bool needsCheck
= !d_lemmas_waiting
.empty();
411 QuantifiersModule::QEffort needsModelE
= QuantifiersModule::QEFFORT_NONE
;
412 std::vector
< QuantifiersModule
* > qm
;
413 if( d_model
->checkNeeded() ){
414 needsCheck
= needsCheck
|| e
>=Theory::EFFORT_LAST_CALL
; //always need to check at or above last call
415 for (QuantifiersModule
*& mdl
: d_modules
)
417 if (mdl
->needsCheck(e
))
421 //can only request model at last call since theory combination can find inconsistencies
422 if( e
>=Theory::EFFORT_LAST_CALL
){
423 QuantifiersModule::QEffort me
= mdl
->needsModel(e
);
424 needsModelE
= me
<needsModelE
? me
: needsModelE
;
431 d_hasAddedLemma
= false;
432 bool setIncomplete
= false;
434 Trace("quant-engine-debug2") << "Quantifiers Engine call to check, level = " << e
<< ", needsCheck=" << needsCheck
<< std::endl
;
436 //flush previous lemmas (for instance, if was interrupted), or other lemmas to process
438 if( d_hasAddedLemma
){
443 if( Trace
.isOn("quant-engine") ){
444 clSet
= double(clock())/double(CLOCKS_PER_SEC
);
445 Trace("quant-engine") << ">>>>> Quantifiers Engine Round, effort = " << e
<< " <<<<<" << std::endl
;
448 if( Trace
.isOn("quant-engine-debug") ){
449 Trace("quant-engine-debug") << "Quantifiers Engine check, level = " << e
<< std::endl
;
450 Trace("quant-engine-debug") << " depth : " << d_ierCounter_c
<< std::endl
;
451 Trace("quant-engine-debug") << " modules to check : ";
452 for( unsigned i
=0; i
<qm
.size(); i
++ ){
453 Trace("quant-engine-debug") << qm
[i
]->identify() << " ";
455 Trace("quant-engine-debug") << std::endl
;
456 Trace("quant-engine-debug") << " # quantified formulas = " << d_model
->getNumAssertedQuantifiers() << std::endl
;
457 if( !d_lemmas_waiting
.empty() ){
458 Trace("quant-engine-debug") << " lemmas waiting = " << d_lemmas_waiting
.size() << std::endl
;
460 Trace("quant-engine-debug")
461 << " Theory engine finished : " << !theoryEngineNeedsCheck()
463 Trace("quant-engine-debug") << " Needs model effort : " << needsModelE
<< std::endl
;
464 Trace("quant-engine-debug")
465 << " In conflict : " << d_conflict
<< std::endl
;
467 if( Trace
.isOn("quant-engine-ee-pre") ){
468 Trace("quant-engine-ee-pre") << "Equality engine (pre-inference): " << std::endl
;
469 debugPrintEqualityEngine( "quant-engine-ee-pre" );
471 if( Trace
.isOn("quant-engine-assert") ){
472 Trace("quant-engine-assert") << "Assertions : " << std::endl
;
473 getTheoryEngine()->printAssertions("quant-engine-assert");
477 Trace("quant-engine-debug") << "Resetting all utilities..." << std::endl
;
478 for (QuantifiersUtil
*& util
: d_util
)
480 Trace("quant-engine-debug2") << "Reset " << util
->identify().c_str()
481 << "..." << std::endl
;
485 if( d_hasAddedLemma
){
488 //should only fail reset if added a lemma
494 if( Trace
.isOn("quant-engine-ee") ){
495 Trace("quant-engine-ee") << "Equality engine : " << std::endl
;
496 debugPrintEqualityEngine( "quant-engine-ee" );
500 Trace("quant-engine-debug") << "Reset model..." << std::endl
;
501 d_model
->reset_round();
504 Trace("quant-engine-debug") << "Resetting all modules..." << std::endl
;
505 for (QuantifiersModule
*& mdl
: d_modules
)
507 Trace("quant-engine-debug2") << "Reset " << mdl
->identify().c_str()
511 Trace("quant-engine-debug") << "Done resetting all modules." << std::endl
;
512 //reset may have added lemmas
514 if( d_hasAddedLemma
){
518 if( e
==Theory::EFFORT_LAST_CALL
){
519 ++(d_statistics
.d_instantiation_rounds_lc
);
520 }else if( e
==Theory::EFFORT_FULL
){
521 ++(d_statistics
.d_instantiation_rounds
);
523 Trace("quant-engine-debug") << "Check modules that needed check..." << std::endl
;
524 for (unsigned qef
= QuantifiersModule::QEFFORT_CONFLICT
;
525 qef
<= QuantifiersModule::QEFFORT_LAST_CALL
;
528 QuantifiersModule::QEffort quant_e
=
529 static_cast<QuantifiersModule::QEffort
>(qef
);
530 d_curr_effort_level
= quant_e
;
531 // Force the theory engine to build the model if any module requested it.
532 if (needsModelE
== quant_e
)
534 Trace("quant-engine-debug") << "Build model..." << std::endl
;
535 if (!d_te
->buildModel())
537 // If we failed to build the model, flush all pending lemmas and
543 if( !d_hasAddedLemma
){
545 for (QuantifiersModule
*& mdl
: qm
)
547 Trace("quant-engine-debug") << "Check " << mdl
->identify().c_str()
548 << " at effort " << quant_e
<< "..."
550 mdl
->check(e
, quant_e
);
552 Trace("quant-engine-debug") << "...conflict!" << std::endl
;
556 //flush all current lemmas
559 //if we have added one, stop
560 if( d_hasAddedLemma
){
564 if (quant_e
== QuantifiersModule::QEFFORT_CONFLICT
)
566 if( e
==Theory::EFFORT_FULL
){
567 //increment if a last call happened, we are not strictly enforcing interleaving, or already were in phase
568 if( d_ierCounterLastLc
!=d_ierCounter_lc
|| !options::instWhenStrictInterleave() || d_ierCounter
%d_inst_when_phase
!=0 ){
569 d_ierCounter
= d_ierCounter
+ 1;
570 d_ierCounterLastLc
= d_ierCounter_lc
;
571 d_ierCounter_c
= d_ierCounter_c
.get() + 1;
573 }else if( e
==Theory::EFFORT_LAST_CALL
){
574 d_ierCounter_lc
= d_ierCounter_lc
+ 1;
577 else if (quant_e
== QuantifiersModule::QEFFORT_MODEL
)
579 if( e
==Theory::EFFORT_LAST_CALL
){
580 //sources of incompleteness
581 for (QuantifiersUtil
*& util
: d_util
)
583 if (!util
->checkComplete())
585 Trace("quant-engine-debug") << "Set incomplete because utility "
586 << util
->identify().c_str()
587 << " was incomplete." << std::endl
;
588 setIncomplete
= true;
591 if (d_conflict_c
.get())
593 // we reported a conflicting lemma, should return
594 setIncomplete
= true;
596 //if we have a chance not to set incomplete
597 if( !setIncomplete
){
598 //check if we should set the incomplete flag
599 for (QuantifiersModule
*& mdl
: d_modules
)
601 if (!mdl
->checkComplete())
603 Trace("quant-engine-debug")
604 << "Set incomplete because module "
605 << mdl
->identify().c_str() << " was incomplete."
607 setIncomplete
= true;
611 if( !setIncomplete
){
612 //look at individual quantified formulas, one module must claim completeness for each one
613 for( unsigned i
=0; i
<d_model
->getNumAssertedQuantifiers(); i
++ ){
614 bool hasCompleteM
= false;
615 Node q
= d_model
->getAssertedQuantifier( i
);
616 QuantifiersModule
* qmd
= getOwner( q
);
618 hasCompleteM
= qmd
->checkCompleteFor( q
);
620 for( unsigned j
=0; j
<d_modules
.size(); j
++ ){
621 if( d_modules
[j
]->checkCompleteFor( q
) ){
629 Trace("quant-engine-debug") << "Set incomplete because " << q
<< " was not fully processed." << std::endl
;
630 setIncomplete
= true;
634 Trace("quant-engine-debug2") << "Complete for " << q
<< " due to " << qmd
->identify().c_str() << std::endl
;
639 //if setIncomplete = false, we will answer SAT, otherwise we will run at quant_e QEFFORT_LAST_CALL
640 if( !setIncomplete
){
647 d_curr_effort_level
= QuantifiersModule::QEFFORT_NONE
;
648 Trace("quant-engine-debug") << "Done check modules that needed check." << std::endl
;
652 bool debugInstTrace
= Trace
.isOn("inst-per-quant-round");
653 if (options::debugInst() || debugInstTrace
)
655 Options
& sopts
= smt::currentSmtEngine()->getOptions();
656 std::ostream
& out
= *sopts
.getOut();
657 d_instantiate
->debugPrint(out
);
660 if( Trace
.isOn("quant-engine") ){
661 double clSet2
= double(clock())/double(CLOCKS_PER_SEC
);
662 Trace("quant-engine") << "Finished quantifiers engine, total time = " << (clSet2
-clSet
);
663 Trace("quant-engine") << ", added lemma = " << d_hasAddedLemma
;
664 Trace("quant-engine") << std::endl
;
667 Trace("quant-engine-debug2") << "Finished quantifiers engine check." << std::endl
;
669 Trace("quant-engine-debug2") << "Quantifiers Engine does not need check." << std::endl
;
673 if( e
==Theory::EFFORT_LAST_CALL
&& !d_hasAddedLemma
){
675 Trace("quant-engine") << "Set incomplete flag." << std::endl
;
676 getOutputChannel().setIncomplete();
679 d_instantiate
->debugPrintModel();
683 void QuantifiersEngine::notifyCombineTheories() {
684 //if allowing theory combination to happen at most once between instantiation rounds
686 //d_ierCounterLastLc = -1;
689 bool QuantifiersEngine::reduceQuantifier( Node q
) {
690 //TODO: this can be unified with preregistration: AlphaEquivalence takes ownership of reducable quants
691 BoolMap::const_iterator it
= d_quants_red
.find( q
);
692 if( it
==d_quants_red
.end() ){
694 std::map
< Node
, Node
>::iterator itr
= d_quants_red_lem
.find( q
);
695 if( itr
==d_quants_red_lem
.end() ){
696 if (d_qmodules
->d_alpha_equiv
)
698 Trace("quant-engine-red") << "Alpha equivalence " << q
<< "?" << std::endl
;
699 //add equivalence with another quantified formula
700 lem
= d_qmodules
->d_alpha_equiv
->reduceQuantifier(q
);
702 Trace("quant-engine-red") << "...alpha equivalence success." << std::endl
;
703 ++(d_statistics
.d_red_alpha_equiv
);
706 d_quants_red_lem
[q
] = lem
;
711 getOutputChannel().lemma( lem
);
713 d_quants_red
[q
] = !lem
.isNull();
714 return !lem
.isNull();
720 void QuantifiersEngine::registerQuantifierInternal(Node f
)
722 std::map
< Node
, bool >::iterator it
= d_quants
.find( f
);
723 if( it
==d_quants
.end() ){
724 Trace("quant") << "QuantifiersEngine : Register quantifier ";
725 Trace("quant") << " : " << f
<< std::endl
;
726 unsigned prev_lemma_waiting
= d_lemmas_waiting
.size();
727 ++(d_statistics
.d_num_quant
);
728 Assert(f
.getKind() == FORALL
);
729 // register with utilities
730 for (unsigned i
= 0; i
< d_util
.size(); i
++)
732 d_util
[i
]->registerQuantifier(f
);
734 // compute attributes
735 d_quant_attr
->computeAttributes(f
);
737 for (QuantifiersModule
*& mdl
: d_modules
)
739 Trace("quant-debug") << "check ownership with " << mdl
->identify()
740 << "..." << std::endl
;
741 mdl
->checkOwnership(f
);
743 QuantifiersModule
* qm
= getOwner(f
);
744 Trace("quant") << " Owner : " << (qm
== nullptr ? "[none]" : qm
->identify())
746 // register with each module
747 for (QuantifiersModule
*& mdl
: d_modules
)
749 Trace("quant-debug") << "register with " << mdl
->identify() << "..."
751 mdl
->registerQuantifier(f
);
752 // since this is context-independent, we should not add any lemmas during
754 Assert(d_lemmas_waiting
.size() == prev_lemma_waiting
);
756 Trace("quant-debug") << "...finish." << std::endl
;
758 AlwaysAssert(d_lemmas_waiting
.size() == prev_lemma_waiting
);
762 void QuantifiersEngine::preRegisterQuantifier(Node q
)
764 NodeSet::const_iterator it
= d_quants_prereg
.find(q
);
765 if (it
!= d_quants_prereg
.end())
769 Trace("quant-debug") << "QuantifiersEngine : Pre-register " << q
<< std::endl
;
770 d_quants_prereg
.insert(q
);
772 if (reduceQuantifier(q
))
774 // if we can reduce it, nothing left to do
777 // ensure that it is registered
778 registerQuantifierInternal(q
);
779 // register with each module
780 for (QuantifiersModule
*& mdl
: d_modules
)
782 Trace("quant-debug") << "pre-register with " << mdl
->identify() << "..."
784 mdl
->preRegisterQuantifier(q
);
788 Trace("quant-debug") << "...finish pre-register " << q
<< "..." << std::endl
;
791 void QuantifiersEngine::registerPattern( std::vector
<Node
> & pattern
) {
792 for(std::vector
<Node
>::iterator p
= pattern
.begin(); p
!= pattern
.end(); ++p
){
793 std::set
< Node
> added
;
794 getTermDatabase()->addTerm( *p
, added
);
798 void QuantifiersEngine::assertQuantifier( Node f
, bool pol
){
799 if (reduceQuantifier(f
))
801 // if we can reduce it, nothing left to do
806 TrustNode lem
= d_skolemize
->process(f
);
809 if (Trace
.isOn("quantifiers-sk-debug"))
811 Node slem
= Rewriter::rewrite(lem
.getNode());
812 Trace("quantifiers-sk-debug")
813 << "Skolemize lemma : " << slem
<< std::endl
;
815 getOutputChannel().trustedLemma(
816 lem
, LemmaProperty::PREPROCESS
| LemmaProperty::NEEDS_JUSTIFY
);
820 // ensure the quantified formula is registered
821 registerQuantifierInternal(f
);
822 // assert it to each module
823 d_model
->assertQuantifier(f
);
824 for (QuantifiersModule
*& mdl
: d_modules
)
828 addTermToDatabase(d_term_util
->getInstConstantBody(f
), true);
831 void QuantifiersEngine::addTermToDatabase( Node n
, bool withinQuant
, bool withinInstClosure
){
832 if( options::incrementalSolving() ){
833 if( d_presolve_in
.find( n
)==d_presolve_in
.end() ){
834 d_presolve_in
.insert( n
);
835 d_presolve_cache
.push_back( n
);
836 d_presolve_cache_wq
.push_back( withinQuant
);
837 d_presolve_cache_wic
.push_back( withinInstClosure
);
840 //only wait if we are doing incremental solving
841 if( !d_presolve
|| !options::incrementalSolving() ){
842 std::set
< Node
> added
;
843 d_term_db
->addTerm(n
, added
, withinQuant
, withinInstClosure
);
847 if (d_sygus_tdb
&& options::sygusEvalUnfold())
849 d_sygus_tdb
->getEvalUnfold()->registerEvalTerm(n
);
855 void QuantifiersEngine::eqNotifyNewClass(TNode t
) {
856 addTermToDatabase( t
);
859 bool QuantifiersEngine::addLemma( Node lem
, bool doCache
, bool doRewrite
){
862 lem
= Rewriter::rewrite(lem
);
864 Trace("inst-add-debug") << "Adding lemma : " << lem
<< std::endl
;
865 BoolMap::const_iterator itp
= d_lemmas_produced_c
.find( lem
);
866 if( itp
==d_lemmas_produced_c
.end() || !(*itp
).second
){
867 d_lemmas_produced_c
[ lem
] = true;
868 d_lemmas_waiting
.push_back( lem
);
869 Trace("inst-add-debug") << "Added lemma" << std::endl
;
872 Trace("inst-add-debug") << "Duplicate." << std::endl
;
876 //do not need to rewrite, will be rewritten after sending
877 d_lemmas_waiting
.push_back( lem
);
882 bool QuantifiersEngine::addTrustedLemma(TrustNode tlem
,
886 Node lem
= tlem
.getProven();
887 if (!addLemma(lem
, doCache
, doRewrite
))
891 d_lemmasWaitingPg
[lem
] = tlem
.getGenerator();
895 bool QuantifiersEngine::removeLemma( Node lem
) {
896 std::vector
< Node
>::iterator it
= std::find( d_lemmas_waiting
.begin(), d_lemmas_waiting
.end(), lem
);
897 if( it
!=d_lemmas_waiting
.end() ){
898 d_lemmas_waiting
.erase( it
, it
+ 1 );
899 d_lemmas_produced_c
[ lem
] = false;
906 void QuantifiersEngine::addRequirePhase( Node lit
, bool req
){
907 d_phase_req_waiting
[lit
] = req
;
910 void QuantifiersEngine::markRelevant( Node q
) {
911 d_model
->markRelevant( q
);
913 bool QuantifiersEngine::hasAddedLemma() const
915 return !d_lemmas_waiting
.empty() || d_hasAddedLemma
;
917 bool QuantifiersEngine::theoryEngineNeedsCheck() const
919 return d_te
->needCheck();
922 void QuantifiersEngine::setConflict()
928 bool QuantifiersEngine::getInstWhenNeedsCheck( Theory::Effort e
) {
929 Trace("quant-engine-debug2") << "Get inst when needs check, counts=" << d_ierCounter
<< ", " << d_ierCounter_lc
<< std::endl
;
930 //determine if we should perform check, based on instWhenMode
931 bool performCheck
= false;
932 if (options::instWhenMode() == options::InstWhenMode::FULL
)
934 performCheck
= ( e
>= Theory::EFFORT_FULL
);
936 else if (options::instWhenMode() == options::InstWhenMode::FULL_DELAY
)
938 performCheck
= (e
>= Theory::EFFORT_FULL
) && !theoryEngineNeedsCheck();
940 else if (options::instWhenMode() == options::InstWhenMode::FULL_LAST_CALL
)
942 performCheck
= ( ( e
==Theory::EFFORT_FULL
&& d_ierCounter
%d_inst_when_phase
!=0 ) || e
==Theory::EFFORT_LAST_CALL
);
944 else if (options::instWhenMode()
945 == options::InstWhenMode::FULL_DELAY_LAST_CALL
)
947 performCheck
= ((e
== Theory::EFFORT_FULL
&& !theoryEngineNeedsCheck()
948 && d_ierCounter
% d_inst_when_phase
!= 0)
949 || e
== Theory::EFFORT_LAST_CALL
);
951 else if (options::instWhenMode() == options::InstWhenMode::LAST_CALL
)
953 performCheck
= ( e
>= Theory::EFFORT_LAST_CALL
);
962 options::UserPatMode
QuantifiersEngine::getInstUserPatMode()
964 if (options::userPatternsQuant() == options::UserPatMode::INTERLEAVE
)
966 return d_ierCounter
% 2 == 0 ? options::UserPatMode::USE
967 : options::UserPatMode::RESORT
;
971 return options::userPatternsQuant();
975 void QuantifiersEngine::flushLemmas(){
976 OutputChannel
& out
= getOutputChannel();
977 if( !d_lemmas_waiting
.empty() ){
978 //take default output channel if none is provided
979 d_hasAddedLemma
= true;
980 std::map
<Node
, ProofGenerator
*>::iterator itp
;
981 // Note: Do not use foreach loop here and do not cache size() call.
982 // New lemmas can be added while iterating over d_lemmas_waiting.
983 for (size_t i
= 0; i
< d_lemmas_waiting
.size(); ++i
)
985 const Node
& lemw
= d_lemmas_waiting
[i
];
986 Trace("qe-lemma") << "Lemma : " << lemw
<< std::endl
;
987 itp
= d_lemmasWaitingPg
.find(lemw
);
989 LemmaProperty::PREPROCESS
| LemmaProperty::NEEDS_JUSTIFY
;
990 if (itp
!= d_lemmasWaitingPg
.end())
992 TrustNode tlemw
= TrustNode::mkTrustLemma(lemw
, itp
->second
);
993 out
.trustedLemma(tlemw
, p
);
1000 d_lemmas_waiting
.clear();
1002 if( !d_phase_req_waiting
.empty() ){
1003 for( std::map
< Node
, bool >::iterator it
= d_phase_req_waiting
.begin(); it
!= d_phase_req_waiting
.end(); ++it
){
1004 Trace("qe-lemma") << "Require phase : " << it
->first
<< " -> " << it
->second
<< std::endl
;
1005 out
.requirePhase(it
->first
, it
->second
);
1007 d_phase_req_waiting
.clear();
1011 void QuantifiersEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
1012 d_instantiate
->getInstantiationTermVectors(q
, tvecs
);
1015 void QuantifiersEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
1016 d_instantiate
->getInstantiationTermVectors(insts
);
1019 void QuantifiersEngine::printInstantiations( std::ostream
& out
) {
1020 bool printed
= false;
1021 // print the skolemizations
1022 if (options::printInstMode() == options::PrintInstMode::LIST
)
1024 if (d_skolemize
->printSkolemization(out
))
1029 // print the instantiations
1030 if (d_instantiate
->printInstantiations(out
))
1035 out
<< "No instantiations" << std::endl
;
1039 void QuantifiersEngine::printSynthSolution( std::ostream
& out
) {
1040 if (d_qmodules
->d_synth_e
)
1042 d_qmodules
->d_synth_e
->printSynthSolution(out
);
1044 out
<< "Internal error : module for synth solution not found." << std::endl
;
1048 void QuantifiersEngine::getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
) {
1049 d_instantiate
->getInstantiatedQuantifiedFormulas(qs
);
1052 QuantifiersEngine::Statistics::Statistics()
1053 : d_time("theory::QuantifiersEngine::time"),
1054 d_qcf_time("theory::QuantifiersEngine::time_qcf"),
1055 d_ematching_time("theory::QuantifiersEngine::time_ematching"),
1056 d_num_quant("QuantifiersEngine::Num_Quantifiers", 0),
1057 d_instantiation_rounds("QuantifiersEngine::Rounds_Instantiation_Full", 0),
1058 d_instantiation_rounds_lc("QuantifiersEngine::Rounds_Instantiation_Last_Call", 0),
1059 d_triggers("QuantifiersEngine::Triggers", 0),
1060 d_simple_triggers("QuantifiersEngine::Triggers_Simple", 0),
1061 d_multi_triggers("QuantifiersEngine::Triggers_Multi", 0),
1062 d_multi_trigger_instantiations("QuantifiersEngine::Multi_Trigger_Instantiations", 0),
1063 d_red_alpha_equiv("QuantifiersEngine::Reductions_Alpha_Equivalence", 0),
1064 d_instantiations_user_patterns("QuantifiersEngine::Instantiations_User_Patterns", 0),
1065 d_instantiations_auto_gen("QuantifiersEngine::Instantiations_Auto_Gen", 0),
1066 d_instantiations_guess("QuantifiersEngine::Instantiations_Guess", 0),
1067 d_instantiations_qcf("QuantifiersEngine::Instantiations_Qcf_Conflict", 0),
1068 d_instantiations_qcf_prop("QuantifiersEngine::Instantiations_Qcf_Prop", 0),
1069 d_instantiations_fmf_exh("QuantifiersEngine::Instantiations_Fmf_Exh", 0),
1070 d_instantiations_fmf_mbqi("QuantifiersEngine::Instantiations_Fmf_Mbqi", 0),
1071 d_instantiations_cbqi("QuantifiersEngine::Instantiations_Cbqi", 0),
1072 d_instantiations_rr("QuantifiersEngine::Instantiations_Rewrite_Rules", 0)
1074 smtStatisticsRegistry()->registerStat(&d_time
);
1075 smtStatisticsRegistry()->registerStat(&d_qcf_time
);
1076 smtStatisticsRegistry()->registerStat(&d_ematching_time
);
1077 smtStatisticsRegistry()->registerStat(&d_num_quant
);
1078 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds
);
1079 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds_lc
);
1080 smtStatisticsRegistry()->registerStat(&d_triggers
);
1081 smtStatisticsRegistry()->registerStat(&d_simple_triggers
);
1082 smtStatisticsRegistry()->registerStat(&d_multi_triggers
);
1083 smtStatisticsRegistry()->registerStat(&d_multi_trigger_instantiations
);
1084 smtStatisticsRegistry()->registerStat(&d_red_alpha_equiv
);
1085 smtStatisticsRegistry()->registerStat(&d_instantiations_user_patterns
);
1086 smtStatisticsRegistry()->registerStat(&d_instantiations_auto_gen
);
1087 smtStatisticsRegistry()->registerStat(&d_instantiations_guess
);
1088 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf
);
1089 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf_prop
);
1090 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_exh
);
1091 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_mbqi
);
1092 smtStatisticsRegistry()->registerStat(&d_instantiations_cbqi
);
1093 smtStatisticsRegistry()->registerStat(&d_instantiations_rr
);
1096 QuantifiersEngine::Statistics::~Statistics(){
1097 smtStatisticsRegistry()->unregisterStat(&d_time
);
1098 smtStatisticsRegistry()->unregisterStat(&d_qcf_time
);
1099 smtStatisticsRegistry()->unregisterStat(&d_ematching_time
);
1100 smtStatisticsRegistry()->unregisterStat(&d_num_quant
);
1101 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds
);
1102 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds_lc
);
1103 smtStatisticsRegistry()->unregisterStat(&d_triggers
);
1104 smtStatisticsRegistry()->unregisterStat(&d_simple_triggers
);
1105 smtStatisticsRegistry()->unregisterStat(&d_multi_triggers
);
1106 smtStatisticsRegistry()->unregisterStat(&d_multi_trigger_instantiations
);
1107 smtStatisticsRegistry()->unregisterStat(&d_red_alpha_equiv
);
1108 smtStatisticsRegistry()->unregisterStat(&d_instantiations_user_patterns
);
1109 smtStatisticsRegistry()->unregisterStat(&d_instantiations_auto_gen
);
1110 smtStatisticsRegistry()->unregisterStat(&d_instantiations_guess
);
1111 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf
);
1112 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf_prop
);
1113 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_exh
);
1114 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_mbqi
);
1115 smtStatisticsRegistry()->unregisterStat(&d_instantiations_cbqi
);
1116 smtStatisticsRegistry()->unregisterStat(&d_instantiations_rr
);
1119 eq::EqualityEngine
* QuantifiersEngine::getMasterEqualityEngine() const
1121 return d_masterEqualityEngine
;
1124 Node
QuantifiersEngine::getInternalRepresentative( Node a
, Node q
, int index
){
1125 return d_eq_query
->getInternalRepresentative(a
, q
, index
);
1128 bool QuantifiersEngine::getSynthSolutions(
1129 std::map
<Node
, std::map
<Node
, Node
> >& sol_map
)
1131 return d_qmodules
->d_synth_e
->getSynthSolutions(sol_map
);
1134 void QuantifiersEngine::debugPrintEqualityEngine( const char * c
) {
1135 eq::EqualityEngine
* ee
= getMasterEqualityEngine();
1136 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( ee
);
1137 std::map
< TypeNode
, int > typ_num
;
1138 while( !eqcs_i
.isFinished() ){
1139 TNode r
= (*eqcs_i
);
1140 TypeNode tr
= r
.getType();
1141 if( typ_num
.find( tr
)==typ_num
.end() ){
1145 bool firstTime
= true;
1146 Trace(c
) << " " << r
;
1147 Trace(c
) << " : { ";
1148 eq::EqClassIterator eqc_i
= eq::EqClassIterator( r
, ee
);
1149 while( !eqc_i
.isFinished() ){
1153 Trace(c
) << std::endl
;
1156 Trace(c
) << " " << n
<< std::endl
;
1160 if( !firstTime
){ Trace(c
) << " "; }
1161 Trace(c
) << "}" << std::endl
;
1164 Trace(c
) << std::endl
;
1165 for( std::map
< TypeNode
, int >::iterator it
= typ_num
.begin(); it
!= typ_num
.end(); ++it
){
1166 Trace(c
) << "# eqc for " << it
->first
<< " : " << it
->second
<< std::endl
;
1170 } // namespace theory