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
),
47 d_term_util(new quantifiers::TermUtil(this)),
48 d_term_canon(new expr::TermCanonize
),
49 d_term_db(new quantifiers::TermDb(d_context
, d_userContext
, this)),
51 d_quant_attr(new quantifiers::QuantAttributes(this)),
52 d_instantiate(new quantifiers::Instantiate(this, d_userContext
, pnm
)),
53 d_skolemize(new quantifiers::Skolemize(this, d_userContext
, pnm
)),
54 d_term_enum(new quantifiers::TermEnumeration
),
55 d_conflict_c(d_context
, false),
56 d_quants_prereg(d_userContext
),
57 d_quants_red(d_userContext
),
58 d_lemmas_produced_c(d_userContext
),
59 d_ierCounter_c(d_context
),
60 d_presolve(d_userContext
, true),
61 d_presolve_in(d_userContext
),
62 d_presolve_cache(d_userContext
),
63 d_presolve_cache_wq(d_userContext
),
64 d_presolve_cache_wic(d_userContext
)
67 d_util
.push_back(d_eq_query
.get());
68 // term util must come before the other utilities
69 d_util
.push_back(d_term_util
.get());
70 d_util
.push_back(d_term_db
.get());
72 if (options::sygus() || options::sygusInst())
74 // must be constructed here since it is required for datatypes finistInit
75 d_sygus_tdb
.reset(new quantifiers::TermDbSygus(d_context
, this));
78 d_util
.push_back(d_instantiate
.get());
80 d_curr_effort_level
= QuantifiersModule::QEFFORT_NONE
;
82 d_hasAddedLemma
= false;
83 //don't add true lemma
84 d_lemmas_produced_c
[d_term_util
->d_true
] = true;
86 Trace("quant-engine-debug") << "Initialize quantifiers engine." << std::endl
;
87 Trace("quant-engine-debug") << "Initialize model, mbqi : " << options::mbqiMode() << std::endl
;
89 if( options::quantEpr() ){
90 Assert(!options::incrementalSolving());
91 d_qepr
.reset(new quantifiers::QuantEPR
);
95 //allow theory combination to go first, once initially
96 d_ierCounter
= options::instWhenTcFirst() ? 0 : 1;
97 d_ierCounter_c
= d_ierCounter
;
99 d_ierCounterLastLc
= 0;
100 d_inst_when_phase
= 1 + ( options::instWhenPhase()<1 ? 1 : options::instWhenPhase() );
102 // Finite model finding requires specialized ways of building the model.
103 // We require constructing the model and model builder here, since it is
104 // required for initializing the CombinationEngine.
105 if (options::finiteModelFind() || options::fmfBound())
107 Trace("quant-engine-debug") << "Initialize model engine, mbqi : " << options::mbqiMode() << " " << options::fmfBound() << std::endl
;
108 if (options::mbqiMode() == options::MbqiMode::FMC
109 || options::mbqiMode() == options::MbqiMode::TRUST
110 || options::fmfBound())
112 Trace("quant-engine-debug") << "...make fmc builder." << std::endl
;
113 d_model
.reset(new quantifiers::fmcheck::FirstOrderModelFmc(
114 this, d_context
, "FirstOrderModelFmc"));
116 new quantifiers::fmcheck::FullModelChecker(d_context
, this));
118 Trace("quant-engine-debug") << "...make default model builder." << std::endl
;
120 new quantifiers::FirstOrderModel(this, d_context
, "FirstOrderModel"));
121 d_builder
.reset(new quantifiers::QModelBuilder(d_context
, this));
125 new quantifiers::FirstOrderModel(this, d_context
, "FirstOrderModel"));
129 QuantifiersEngine::~QuantifiersEngine() {}
131 void QuantifiersEngine::finishInit()
133 // Initialize the modules and the utilities here. We delay their
134 // initialization to here, since this is after TheoryQuantifiers finishInit,
135 // which has initialized the state and inference manager of this engine.
136 d_qmodules
.reset(new quantifiers::QuantifiersModules
);
137 d_qmodules
->initialize(this, d_context
, d_modules
);
138 if (d_qmodules
->d_rel_dom
.get())
140 d_util
.push_back(d_qmodules
->d_rel_dom
.get());
144 void QuantifiersEngine::setMasterEqualityEngine(eq::EqualityEngine
* mee
)
146 d_masterEqualityEngine
= mee
;
149 TheoryEngine
* QuantifiersEngine::getTheoryEngine() const { return d_te
; }
151 DecisionManager
* QuantifiersEngine::getDecisionManager()
153 return &d_decManager
;
156 context::Context
* QuantifiersEngine::getSatContext() { return d_context
; }
158 context::UserContext
* QuantifiersEngine::getUserContext()
160 return d_userContext
;
163 OutputChannel
& QuantifiersEngine::getOutputChannel()
165 return d_te
->theoryOf(THEORY_QUANTIFIERS
)->getOutputChannel();
167 /** get default valuation for the quantifiers engine */
168 Valuation
& QuantifiersEngine::getValuation()
170 return d_te
->theoryOf(THEORY_QUANTIFIERS
)->getValuation();
173 const LogicInfo
& QuantifiersEngine::getLogicInfo() const
175 return d_te
->getLogicInfo();
178 EqualityQuery
* QuantifiersEngine::getEqualityQuery() const
180 return d_eq_query
.get();
182 quantifiers::QModelBuilder
* QuantifiersEngine::getModelBuilder() const
184 return d_builder
.get();
186 quantifiers::QuantEPR
* QuantifiersEngine::getQuantEPR() const
190 quantifiers::FirstOrderModel
* QuantifiersEngine::getModel() const
192 return d_model
.get();
194 quantifiers::TermDb
* QuantifiersEngine::getTermDatabase() const
196 return d_term_db
.get();
198 quantifiers::TermDbSygus
* QuantifiersEngine::getTermDatabaseSygus() const
200 return d_sygus_tdb
.get();
202 quantifiers::TermUtil
* QuantifiersEngine::getTermUtil() const
204 return d_term_util
.get();
206 expr::TermCanonize
* QuantifiersEngine::getTermCanonize() const
208 return d_term_canon
.get();
210 quantifiers::QuantAttributes
* QuantifiersEngine::getQuantAttributes() const
212 return d_quant_attr
.get();
214 quantifiers::Instantiate
* QuantifiersEngine::getInstantiate() const
216 return d_instantiate
.get();
218 quantifiers::Skolemize
* QuantifiersEngine::getSkolemize() const
220 return d_skolemize
.get();
222 quantifiers::TermEnumeration
* QuantifiersEngine::getTermEnumeration() const
224 return d_term_enum
.get();
226 inst::TriggerTrie
* QuantifiersEngine::getTriggerDatabase() const
228 return d_tr_trie
.get();
231 QuantifiersModule
* QuantifiersEngine::getOwner( Node q
) {
232 std::map
< Node
, QuantifiersModule
* >::iterator it
= d_owner
.find( q
);
233 if( it
==d_owner
.end() ){
240 void QuantifiersEngine::setOwner( Node q
, QuantifiersModule
* m
, int priority
) {
241 QuantifiersModule
* mo
= getOwner( q
);
244 if( priority
<=d_owner_priority
[q
] ){
245 Trace("quant-warn") << "WARNING: setting owner of " << q
<< " to " << ( m
? m
->identify() : "null" ) << ", but already has owner " << mo
->identify() << " with higher priority!" << std::endl
;
250 d_owner_priority
[q
] = priority
;
254 void QuantifiersEngine::setOwner(Node q
, quantifiers::QAttributes
& qa
)
256 if (qa
.d_sygus
|| (options::sygusRecFun() && !qa
.d_fundef_f
.isNull()))
258 if (d_qmodules
->d_synth_e
.get() == nullptr)
260 Trace("quant-warn") << "WARNING : synth engine is null, and we have : "
263 // set synth engine as owner since this is either a conjecture or a function
264 // definition to be used by sygus
265 setOwner(q
, d_qmodules
->d_synth_e
.get(), 2);
269 bool QuantifiersEngine::hasOwnership( Node q
, QuantifiersModule
* m
) {
270 QuantifiersModule
* mo
= getOwner( q
);
271 return mo
==m
|| mo
==NULL
;
274 bool QuantifiersEngine::isFiniteBound(Node q
, Node v
) const
276 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
277 if (bi
&& bi
->isBound(q
, v
))
281 TypeNode tn
= v
.getType();
282 if (tn
.isSort() && options::finiteModelFind())
286 else if (d_term_enum
->mayComplete(tn
))
293 BoundVarType
QuantifiersEngine::getBoundVarType(Node q
, Node v
) const
295 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
298 return bi
->getBoundVarType(q
, v
);
300 return isFiniteBound(q
, v
) ? BOUND_FINITE
: BOUND_NONE
;
303 void QuantifiersEngine::getBoundVarIndices(Node q
,
304 std::vector
<unsigned>& indices
) const
306 Assert(indices
.empty());
307 // we take the bounded variables first
308 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
311 bi
->getBoundVarIndices(q
, indices
);
313 // then get the remaining ones
314 for (unsigned i
= 0, nvars
= q
[0].getNumChildren(); i
< nvars
; i
++)
316 if (std::find(indices
.begin(), indices
.end(), i
) == indices
.end())
318 indices
.push_back(i
);
323 bool QuantifiersEngine::getBoundElements(RepSetIterator
* rsi
,
327 std::vector
<Node
>& elements
) const
329 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
332 return bi
->getBoundElements(rsi
, initial
, q
, v
, elements
);
337 void QuantifiersEngine::presolve() {
338 Trace("quant-engine-proc") << "QuantifiersEngine : presolve " << std::endl
;
339 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
340 d_modules
[i
]->presolve();
342 d_term_db
->presolve();
344 //add all terms to database
345 if( options::incrementalSolving() ){
346 Trace("quant-engine-proc") << "Add presolve cache " << d_presolve_cache
.size() << std::endl
;
347 for( unsigned i
=0; i
<d_presolve_cache
.size(); i
++ ){
348 addTermToDatabase( d_presolve_cache
[i
], d_presolve_cache_wq
[i
], d_presolve_cache_wic
[i
] );
350 Trace("quant-engine-proc") << "Done add presolve cache " << std::endl
;
354 void QuantifiersEngine::ppNotifyAssertions(
355 const std::vector
<Node
>& assertions
) {
356 Trace("quant-engine-proc")
357 << "ppNotifyAssertions in QE, #assertions = " << assertions
.size()
358 << " check epr = " << (d_qepr
!= NULL
) << std::endl
;
359 if (options::instLevelInputOnly() && options::instMaxLevel() != -1)
361 for (const Node
& a
: assertions
)
363 quantifiers::QuantAttributes::setInstantiationLevelAttr(a
, 0);
366 if (options::sygus())
368 quantifiers::SynthEngine
* sye
= d_qmodules
->d_synth_e
.get();
369 for (const Node
& a
: assertions
)
371 sye
->preregisterAssertion(a
);
374 /* The SyGuS instantiation module needs a global view of all available
375 * assertions to collect global terms that get added to each grammar.
377 if (options::sygusInst())
379 quantifiers::SygusInst
* si
= d_qmodules
->d_sygus_inst
.get();
380 si
->ppNotifyAssertions(assertions
);
384 void QuantifiersEngine::check( Theory::Effort e
){
385 CodeTimer
codeTimer(d_statistics
.d_time
);
387 if (!getMasterEqualityEngine()->consistent())
389 Trace("quant-engine-debug") << "Master equality engine not consistent, return." << std::endl
;
392 if (d_conflict_c
.get())
394 if (e
< Theory::EFFORT_LAST_CALL
)
396 // this can happen in rare cases when quantifiers is the first to realize
397 // there is a quantifier-free conflict, for example, when it discovers
398 // disequal and congruent terms in the master equality engine during
399 // term indexing. In such cases, quantifiers reports a "conflicting lemma"
400 // that is, one that is entailed to be false by the current assignment.
401 // If this lemma is not a SAT conflict, we may get another call to full
402 // effort check and the quantifier-free solvers still haven't realized
403 // there is a conflict. In this case, we return, trusting that theory
404 // combination will do the right thing (split on equalities until there is
405 // a conflict at the quantifier-free level).
406 Trace("quant-engine-debug")
407 << "Conflicting lemma already reported by quantifiers, return."
411 // we reported what we thought was a conflicting lemma, but now we have
412 // gotten a check at LAST_CALL effort, indicating that the lemma we reported
413 // was not conflicting. This should never happen, but in production mode, we
414 // proceed with the check.
417 bool needsCheck
= !d_lemmas_waiting
.empty();
418 QuantifiersModule::QEffort needsModelE
= QuantifiersModule::QEFFORT_NONE
;
419 std::vector
< QuantifiersModule
* > qm
;
420 if( d_model
->checkNeeded() ){
421 needsCheck
= needsCheck
|| e
>=Theory::EFFORT_LAST_CALL
; //always need to check at or above last call
422 for (QuantifiersModule
*& mdl
: d_modules
)
424 if (mdl
->needsCheck(e
))
428 //can only request model at last call since theory combination can find inconsistencies
429 if( e
>=Theory::EFFORT_LAST_CALL
){
430 QuantifiersModule::QEffort me
= mdl
->needsModel(e
);
431 needsModelE
= me
<needsModelE
? me
: needsModelE
;
438 d_hasAddedLemma
= false;
439 bool setIncomplete
= false;
441 Trace("quant-engine-debug2") << "Quantifiers Engine call to check, level = " << e
<< ", needsCheck=" << needsCheck
<< std::endl
;
443 //flush previous lemmas (for instance, if was interrupted), or other lemmas to process
445 if( d_hasAddedLemma
){
450 if( Trace
.isOn("quant-engine") ){
451 clSet
= double(clock())/double(CLOCKS_PER_SEC
);
452 Trace("quant-engine") << ">>>>> Quantifiers Engine Round, effort = " << e
<< " <<<<<" << std::endl
;
455 if( Trace
.isOn("quant-engine-debug") ){
456 Trace("quant-engine-debug") << "Quantifiers Engine check, level = " << e
<< std::endl
;
457 Trace("quant-engine-debug") << " depth : " << d_ierCounter_c
<< std::endl
;
458 Trace("quant-engine-debug") << " modules to check : ";
459 for( unsigned i
=0; i
<qm
.size(); i
++ ){
460 Trace("quant-engine-debug") << qm
[i
]->identify() << " ";
462 Trace("quant-engine-debug") << std::endl
;
463 Trace("quant-engine-debug") << " # quantified formulas = " << d_model
->getNumAssertedQuantifiers() << std::endl
;
464 if( !d_lemmas_waiting
.empty() ){
465 Trace("quant-engine-debug") << " lemmas waiting = " << d_lemmas_waiting
.size() << std::endl
;
467 Trace("quant-engine-debug")
468 << " Theory engine finished : " << !theoryEngineNeedsCheck()
470 Trace("quant-engine-debug") << " Needs model effort : " << needsModelE
<< std::endl
;
471 Trace("quant-engine-debug")
472 << " In conflict : " << d_conflict
<< std::endl
;
474 if( Trace
.isOn("quant-engine-ee-pre") ){
475 Trace("quant-engine-ee-pre") << "Equality engine (pre-inference): " << std::endl
;
476 debugPrintEqualityEngine( "quant-engine-ee-pre" );
478 if( Trace
.isOn("quant-engine-assert") ){
479 Trace("quant-engine-assert") << "Assertions : " << std::endl
;
480 getTheoryEngine()->printAssertions("quant-engine-assert");
484 Trace("quant-engine-debug") << "Resetting all utilities..." << std::endl
;
485 for (QuantifiersUtil
*& util
: d_util
)
487 Trace("quant-engine-debug2") << "Reset " << util
->identify().c_str()
488 << "..." << std::endl
;
492 if( d_hasAddedLemma
){
495 //should only fail reset if added a lemma
501 if( Trace
.isOn("quant-engine-ee") ){
502 Trace("quant-engine-ee") << "Equality engine : " << std::endl
;
503 debugPrintEqualityEngine( "quant-engine-ee" );
507 Trace("quant-engine-debug") << "Reset model..." << std::endl
;
508 d_model
->reset_round();
511 Trace("quant-engine-debug") << "Resetting all modules..." << std::endl
;
512 for (QuantifiersModule
*& mdl
: d_modules
)
514 Trace("quant-engine-debug2") << "Reset " << mdl
->identify().c_str()
518 Trace("quant-engine-debug") << "Done resetting all modules." << std::endl
;
519 //reset may have added lemmas
521 if( d_hasAddedLemma
){
525 if( e
==Theory::EFFORT_LAST_CALL
){
526 ++(d_statistics
.d_instantiation_rounds_lc
);
527 }else if( e
==Theory::EFFORT_FULL
){
528 ++(d_statistics
.d_instantiation_rounds
);
530 Trace("quant-engine-debug") << "Check modules that needed check..." << std::endl
;
531 for (unsigned qef
= QuantifiersModule::QEFFORT_CONFLICT
;
532 qef
<= QuantifiersModule::QEFFORT_LAST_CALL
;
535 QuantifiersModule::QEffort quant_e
=
536 static_cast<QuantifiersModule::QEffort
>(qef
);
537 d_curr_effort_level
= quant_e
;
538 // Force the theory engine to build the model if any module requested it.
539 if (needsModelE
== quant_e
)
541 Trace("quant-engine-debug") << "Build model..." << std::endl
;
542 if (!d_te
->buildModel())
544 // If we failed to build the model, flush all pending lemmas and
550 if( !d_hasAddedLemma
){
552 for (QuantifiersModule
*& mdl
: qm
)
554 Trace("quant-engine-debug") << "Check " << mdl
->identify().c_str()
555 << " at effort " << quant_e
<< "..."
557 mdl
->check(e
, quant_e
);
559 Trace("quant-engine-debug") << "...conflict!" << std::endl
;
563 //flush all current lemmas
566 //if we have added one, stop
567 if( d_hasAddedLemma
){
571 if (quant_e
== QuantifiersModule::QEFFORT_CONFLICT
)
573 if( e
==Theory::EFFORT_FULL
){
574 //increment if a last call happened, we are not strictly enforcing interleaving, or already were in phase
575 if( d_ierCounterLastLc
!=d_ierCounter_lc
|| !options::instWhenStrictInterleave() || d_ierCounter
%d_inst_when_phase
!=0 ){
576 d_ierCounter
= d_ierCounter
+ 1;
577 d_ierCounterLastLc
= d_ierCounter_lc
;
578 d_ierCounter_c
= d_ierCounter_c
.get() + 1;
580 }else if( e
==Theory::EFFORT_LAST_CALL
){
581 d_ierCounter_lc
= d_ierCounter_lc
+ 1;
584 else if (quant_e
== QuantifiersModule::QEFFORT_MODEL
)
586 if( e
==Theory::EFFORT_LAST_CALL
){
587 //sources of incompleteness
588 for (QuantifiersUtil
*& util
: d_util
)
590 if (!util
->checkComplete())
592 Trace("quant-engine-debug") << "Set incomplete because utility "
593 << util
->identify().c_str()
594 << " was incomplete." << std::endl
;
595 setIncomplete
= true;
598 if (d_conflict_c
.get())
600 // we reported a conflicting lemma, should return
601 setIncomplete
= true;
603 //if we have a chance not to set incomplete
604 if( !setIncomplete
){
605 //check if we should set the incomplete flag
606 for (QuantifiersModule
*& mdl
: d_modules
)
608 if (!mdl
->checkComplete())
610 Trace("quant-engine-debug")
611 << "Set incomplete because module "
612 << mdl
->identify().c_str() << " was incomplete."
614 setIncomplete
= true;
618 if( !setIncomplete
){
619 //look at individual quantified formulas, one module must claim completeness for each one
620 for( unsigned i
=0; i
<d_model
->getNumAssertedQuantifiers(); i
++ ){
621 bool hasCompleteM
= false;
622 Node q
= d_model
->getAssertedQuantifier( i
);
623 QuantifiersModule
* qmd
= getOwner( q
);
625 hasCompleteM
= qmd
->checkCompleteFor( q
);
627 for( unsigned j
=0; j
<d_modules
.size(); j
++ ){
628 if( d_modules
[j
]->checkCompleteFor( q
) ){
636 Trace("quant-engine-debug") << "Set incomplete because " << q
<< " was not fully processed." << std::endl
;
637 setIncomplete
= true;
641 Trace("quant-engine-debug2") << "Complete for " << q
<< " due to " << qmd
->identify().c_str() << std::endl
;
646 //if setIncomplete = false, we will answer SAT, otherwise we will run at quant_e QEFFORT_LAST_CALL
647 if( !setIncomplete
){
654 d_curr_effort_level
= QuantifiersModule::QEFFORT_NONE
;
655 Trace("quant-engine-debug") << "Done check modules that needed check." << std::endl
;
659 bool debugInstTrace
= Trace
.isOn("inst-per-quant-round");
660 if (options::debugInst() || debugInstTrace
)
662 Options
& sopts
= smt::currentSmtEngine()->getOptions();
663 std::ostream
& out
= *sopts
.getOut();
664 d_instantiate
->debugPrint(out
);
667 if( Trace
.isOn("quant-engine") ){
668 double clSet2
= double(clock())/double(CLOCKS_PER_SEC
);
669 Trace("quant-engine") << "Finished quantifiers engine, total time = " << (clSet2
-clSet
);
670 Trace("quant-engine") << ", added lemma = " << d_hasAddedLemma
;
671 Trace("quant-engine") << std::endl
;
674 Trace("quant-engine-debug2") << "Finished quantifiers engine check." << std::endl
;
676 Trace("quant-engine-debug2") << "Quantifiers Engine does not need check." << std::endl
;
680 if( e
==Theory::EFFORT_LAST_CALL
&& !d_hasAddedLemma
){
682 Trace("quant-engine") << "Set incomplete flag." << std::endl
;
683 getOutputChannel().setIncomplete();
686 d_instantiate
->debugPrintModel();
690 void QuantifiersEngine::notifyCombineTheories() {
691 //if allowing theory combination to happen at most once between instantiation rounds
693 //d_ierCounterLastLc = -1;
696 bool QuantifiersEngine::reduceQuantifier( Node q
) {
697 //TODO: this can be unified with preregistration: AlphaEquivalence takes ownership of reducable quants
698 BoolMap::const_iterator it
= d_quants_red
.find( q
);
699 if( it
==d_quants_red
.end() ){
701 std::map
< Node
, Node
>::iterator itr
= d_quants_red_lem
.find( q
);
702 if( itr
==d_quants_red_lem
.end() ){
703 if (d_qmodules
->d_alpha_equiv
)
705 Trace("quant-engine-red") << "Alpha equivalence " << q
<< "?" << std::endl
;
706 //add equivalence with another quantified formula
707 lem
= d_qmodules
->d_alpha_equiv
->reduceQuantifier(q
);
709 Trace("quant-engine-red") << "...alpha equivalence success." << std::endl
;
710 ++(d_statistics
.d_red_alpha_equiv
);
713 d_quants_red_lem
[q
] = lem
;
718 getOutputChannel().lemma( lem
);
720 d_quants_red
[q
] = !lem
.isNull();
721 return !lem
.isNull();
727 void QuantifiersEngine::registerQuantifierInternal(Node f
)
729 std::map
< Node
, bool >::iterator it
= d_quants
.find( f
);
730 if( it
==d_quants
.end() ){
731 Trace("quant") << "QuantifiersEngine : Register quantifier ";
732 Trace("quant") << " : " << f
<< std::endl
;
733 unsigned prev_lemma_waiting
= d_lemmas_waiting
.size();
734 ++(d_statistics
.d_num_quant
);
735 Assert(f
.getKind() == FORALL
);
736 // register with utilities
737 for (unsigned i
= 0; i
< d_util
.size(); i
++)
739 d_util
[i
]->registerQuantifier(f
);
741 // compute attributes
742 d_quant_attr
->computeAttributes(f
);
744 for (QuantifiersModule
*& mdl
: d_modules
)
746 Trace("quant-debug") << "check ownership with " << mdl
->identify()
747 << "..." << std::endl
;
748 mdl
->checkOwnership(f
);
750 QuantifiersModule
* qm
= getOwner(f
);
751 Trace("quant") << " Owner : " << (qm
== nullptr ? "[none]" : qm
->identify())
753 // register with each module
754 for (QuantifiersModule
*& mdl
: d_modules
)
756 Trace("quant-debug") << "register with " << mdl
->identify() << "..."
758 mdl
->registerQuantifier(f
);
759 // since this is context-independent, we should not add any lemmas during
761 Assert(d_lemmas_waiting
.size() == prev_lemma_waiting
);
763 Trace("quant-debug") << "...finish." << std::endl
;
765 AlwaysAssert(d_lemmas_waiting
.size() == prev_lemma_waiting
);
769 void QuantifiersEngine::preRegisterQuantifier(Node q
)
771 NodeSet::const_iterator it
= d_quants_prereg
.find(q
);
772 if (it
!= d_quants_prereg
.end())
776 Trace("quant-debug") << "QuantifiersEngine : Pre-register " << q
<< std::endl
;
777 d_quants_prereg
.insert(q
);
779 if (reduceQuantifier(q
))
781 // if we can reduce it, nothing left to do
784 // ensure that it is registered
785 registerQuantifierInternal(q
);
786 // register with each module
787 for (QuantifiersModule
*& mdl
: d_modules
)
789 Trace("quant-debug") << "pre-register with " << mdl
->identify() << "..."
791 mdl
->preRegisterQuantifier(q
);
795 Trace("quant-debug") << "...finish pre-register " << q
<< "..." << std::endl
;
798 void QuantifiersEngine::registerPattern( std::vector
<Node
> & pattern
) {
799 for(std::vector
<Node
>::iterator p
= pattern
.begin(); p
!= pattern
.end(); ++p
){
800 std::set
< Node
> added
;
801 getTermDatabase()->addTerm( *p
, added
);
805 void QuantifiersEngine::assertQuantifier( Node f
, bool pol
){
806 if (reduceQuantifier(f
))
808 // if we can reduce it, nothing left to do
813 TrustNode lem
= d_skolemize
->process(f
);
816 if (Trace
.isOn("quantifiers-sk-debug"))
818 Node slem
= Rewriter::rewrite(lem
.getNode());
819 Trace("quantifiers-sk-debug")
820 << "Skolemize lemma : " << slem
<< std::endl
;
822 getOutputChannel().trustedLemma(
823 lem
, LemmaProperty::PREPROCESS
| LemmaProperty::NEEDS_JUSTIFY
);
827 // ensure the quantified formula is registered
828 registerQuantifierInternal(f
);
829 // assert it to each module
830 d_model
->assertQuantifier(f
);
831 for (QuantifiersModule
*& mdl
: d_modules
)
835 addTermToDatabase(d_term_util
->getInstConstantBody(f
), true);
838 void QuantifiersEngine::addTermToDatabase( Node n
, bool withinQuant
, bool withinInstClosure
){
839 if( options::incrementalSolving() ){
840 if( d_presolve_in
.find( n
)==d_presolve_in
.end() ){
841 d_presolve_in
.insert( n
);
842 d_presolve_cache
.push_back( n
);
843 d_presolve_cache_wq
.push_back( withinQuant
);
844 d_presolve_cache_wic
.push_back( withinInstClosure
);
847 //only wait if we are doing incremental solving
848 if( !d_presolve
|| !options::incrementalSolving() ){
849 std::set
< Node
> added
;
850 d_term_db
->addTerm(n
, added
, withinQuant
, withinInstClosure
);
854 if (d_sygus_tdb
&& options::sygusEvalUnfold())
856 d_sygus_tdb
->getEvalUnfold()->registerEvalTerm(n
);
862 void QuantifiersEngine::eqNotifyNewClass(TNode t
) {
863 addTermToDatabase( t
);
866 bool QuantifiersEngine::addLemma( Node lem
, bool doCache
, bool doRewrite
){
869 lem
= Rewriter::rewrite(lem
);
871 Trace("inst-add-debug") << "Adding lemma : " << lem
<< std::endl
;
872 BoolMap::const_iterator itp
= d_lemmas_produced_c
.find( lem
);
873 if( itp
==d_lemmas_produced_c
.end() || !(*itp
).second
){
874 d_lemmas_produced_c
[ lem
] = true;
875 d_lemmas_waiting
.push_back( lem
);
876 Trace("inst-add-debug") << "Added lemma" << std::endl
;
879 Trace("inst-add-debug") << "Duplicate." << std::endl
;
883 //do not need to rewrite, will be rewritten after sending
884 d_lemmas_waiting
.push_back( lem
);
889 bool QuantifiersEngine::addTrustedLemma(TrustNode tlem
,
893 Node lem
= tlem
.getProven();
894 if (!addLemma(lem
, doCache
, doRewrite
))
898 d_lemmasWaitingPg
[lem
] = tlem
.getGenerator();
902 bool QuantifiersEngine::removeLemma( Node lem
) {
903 std::vector
< Node
>::iterator it
= std::find( d_lemmas_waiting
.begin(), d_lemmas_waiting
.end(), lem
);
904 if( it
!=d_lemmas_waiting
.end() ){
905 d_lemmas_waiting
.erase( it
, it
+ 1 );
906 d_lemmas_produced_c
[ lem
] = false;
913 void QuantifiersEngine::addRequirePhase( Node lit
, bool req
){
914 d_phase_req_waiting
[lit
] = req
;
917 void QuantifiersEngine::markRelevant( Node q
) {
918 d_model
->markRelevant( q
);
920 bool QuantifiersEngine::hasAddedLemma() const
922 return !d_lemmas_waiting
.empty() || d_hasAddedLemma
;
924 bool QuantifiersEngine::theoryEngineNeedsCheck() const
926 return d_te
->needCheck();
929 void QuantifiersEngine::setConflict()
935 bool QuantifiersEngine::getInstWhenNeedsCheck( Theory::Effort e
) {
936 Trace("quant-engine-debug2") << "Get inst when needs check, counts=" << d_ierCounter
<< ", " << d_ierCounter_lc
<< std::endl
;
937 //determine if we should perform check, based on instWhenMode
938 bool performCheck
= false;
939 if (options::instWhenMode() == options::InstWhenMode::FULL
)
941 performCheck
= ( e
>= Theory::EFFORT_FULL
);
943 else if (options::instWhenMode() == options::InstWhenMode::FULL_DELAY
)
945 performCheck
= (e
>= Theory::EFFORT_FULL
) && !theoryEngineNeedsCheck();
947 else if (options::instWhenMode() == options::InstWhenMode::FULL_LAST_CALL
)
949 performCheck
= ( ( e
==Theory::EFFORT_FULL
&& d_ierCounter
%d_inst_when_phase
!=0 ) || e
==Theory::EFFORT_LAST_CALL
);
951 else if (options::instWhenMode()
952 == options::InstWhenMode::FULL_DELAY_LAST_CALL
)
954 performCheck
= ((e
== Theory::EFFORT_FULL
&& !theoryEngineNeedsCheck()
955 && d_ierCounter
% d_inst_when_phase
!= 0)
956 || e
== Theory::EFFORT_LAST_CALL
);
958 else if (options::instWhenMode() == options::InstWhenMode::LAST_CALL
)
960 performCheck
= ( e
>= Theory::EFFORT_LAST_CALL
);
969 options::UserPatMode
QuantifiersEngine::getInstUserPatMode()
971 if (options::userPatternsQuant() == options::UserPatMode::INTERLEAVE
)
973 return d_ierCounter
% 2 == 0 ? options::UserPatMode::USE
974 : options::UserPatMode::RESORT
;
978 return options::userPatternsQuant();
982 void QuantifiersEngine::flushLemmas(){
983 OutputChannel
& out
= getOutputChannel();
984 if( !d_lemmas_waiting
.empty() ){
985 //take default output channel if none is provided
986 d_hasAddedLemma
= true;
987 std::map
<Node
, ProofGenerator
*>::iterator itp
;
988 // Note: Do not use foreach loop here and do not cache size() call.
989 // New lemmas can be added while iterating over d_lemmas_waiting.
990 for (size_t i
= 0; i
< d_lemmas_waiting
.size(); ++i
)
992 const Node
& lemw
= d_lemmas_waiting
[i
];
993 Trace("qe-lemma") << "Lemma : " << lemw
<< std::endl
;
994 itp
= d_lemmasWaitingPg
.find(lemw
);
995 if (itp
!= d_lemmasWaitingPg
.end())
997 TrustNode tlemw
= TrustNode::mkTrustLemma(lemw
, itp
->second
);
998 out
.trustedLemma(tlemw
, LemmaProperty::PREPROCESS
);
1002 out
.lemma(lemw
, LemmaProperty::PREPROCESS
);
1005 d_lemmas_waiting
.clear();
1007 if( !d_phase_req_waiting
.empty() ){
1008 for( std::map
< Node
, bool >::iterator it
= d_phase_req_waiting
.begin(); it
!= d_phase_req_waiting
.end(); ++it
){
1009 Trace("qe-lemma") << "Require phase : " << it
->first
<< " -> " << it
->second
<< std::endl
;
1010 out
.requirePhase(it
->first
, it
->second
);
1012 d_phase_req_waiting
.clear();
1016 bool QuantifiersEngine::getUnsatCoreLemmas( std::vector
< Node
>& active_lemmas
) {
1017 return d_instantiate
->getUnsatCoreLemmas(active_lemmas
);
1020 void QuantifiersEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
1021 d_instantiate
->getInstantiationTermVectors(q
, tvecs
);
1024 void QuantifiersEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
1025 d_instantiate
->getInstantiationTermVectors(insts
);
1028 void QuantifiersEngine::getExplanationForInstLemmas(
1029 const std::vector
<Node
>& lems
,
1030 std::map
<Node
, Node
>& quant
,
1031 std::map
<Node
, std::vector
<Node
> >& tvec
)
1033 d_instantiate
->getExplanationForInstLemmas(lems
, quant
, tvec
);
1036 void QuantifiersEngine::printInstantiations( std::ostream
& out
) {
1037 bool printed
= false;
1038 // print the skolemizations
1039 if (options::printInstMode() == options::PrintInstMode::LIST
)
1041 if (d_skolemize
->printSkolemization(out
))
1046 // print the instantiations
1047 if (d_instantiate
->printInstantiations(out
))
1052 out
<< "No instantiations" << std::endl
;
1056 void QuantifiersEngine::printSynthSolution( std::ostream
& out
) {
1057 if (d_qmodules
->d_synth_e
)
1059 d_qmodules
->d_synth_e
->printSynthSolution(out
);
1061 out
<< "Internal error : module for synth solution not found." << std::endl
;
1065 void QuantifiersEngine::getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
) {
1066 d_instantiate
->getInstantiatedQuantifiedFormulas(qs
);
1069 void QuantifiersEngine::getInstantiations( std::map
< Node
, std::vector
< Node
> >& insts
) {
1070 d_instantiate
->getInstantiations(insts
);
1073 void QuantifiersEngine::getInstantiations( Node q
, std::vector
< Node
>& insts
) {
1074 d_instantiate
->getInstantiations(q
, insts
);
1077 Node
QuantifiersEngine::getInstantiatedConjunction( Node q
) {
1078 return d_instantiate
->getInstantiatedConjunction(q
);
1081 QuantifiersEngine::Statistics::Statistics()
1082 : d_time("theory::QuantifiersEngine::time"),
1083 d_qcf_time("theory::QuantifiersEngine::time_qcf"),
1084 d_ematching_time("theory::QuantifiersEngine::time_ematching"),
1085 d_num_quant("QuantifiersEngine::Num_Quantifiers", 0),
1086 d_instantiation_rounds("QuantifiersEngine::Rounds_Instantiation_Full", 0),
1087 d_instantiation_rounds_lc("QuantifiersEngine::Rounds_Instantiation_Last_Call", 0),
1088 d_triggers("QuantifiersEngine::Triggers", 0),
1089 d_simple_triggers("QuantifiersEngine::Triggers_Simple", 0),
1090 d_multi_triggers("QuantifiersEngine::Triggers_Multi", 0),
1091 d_multi_trigger_instantiations("QuantifiersEngine::Multi_Trigger_Instantiations", 0),
1092 d_red_alpha_equiv("QuantifiersEngine::Reductions_Alpha_Equivalence", 0),
1093 d_instantiations_user_patterns("QuantifiersEngine::Instantiations_User_Patterns", 0),
1094 d_instantiations_auto_gen("QuantifiersEngine::Instantiations_Auto_Gen", 0),
1095 d_instantiations_guess("QuantifiersEngine::Instantiations_Guess", 0),
1096 d_instantiations_qcf("QuantifiersEngine::Instantiations_Qcf_Conflict", 0),
1097 d_instantiations_qcf_prop("QuantifiersEngine::Instantiations_Qcf_Prop", 0),
1098 d_instantiations_fmf_exh("QuantifiersEngine::Instantiations_Fmf_Exh", 0),
1099 d_instantiations_fmf_mbqi("QuantifiersEngine::Instantiations_Fmf_Mbqi", 0),
1100 d_instantiations_cbqi("QuantifiersEngine::Instantiations_Cbqi", 0),
1101 d_instantiations_rr("QuantifiersEngine::Instantiations_Rewrite_Rules", 0)
1103 smtStatisticsRegistry()->registerStat(&d_time
);
1104 smtStatisticsRegistry()->registerStat(&d_qcf_time
);
1105 smtStatisticsRegistry()->registerStat(&d_ematching_time
);
1106 smtStatisticsRegistry()->registerStat(&d_num_quant
);
1107 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds
);
1108 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds_lc
);
1109 smtStatisticsRegistry()->registerStat(&d_triggers
);
1110 smtStatisticsRegistry()->registerStat(&d_simple_triggers
);
1111 smtStatisticsRegistry()->registerStat(&d_multi_triggers
);
1112 smtStatisticsRegistry()->registerStat(&d_multi_trigger_instantiations
);
1113 smtStatisticsRegistry()->registerStat(&d_red_alpha_equiv
);
1114 smtStatisticsRegistry()->registerStat(&d_instantiations_user_patterns
);
1115 smtStatisticsRegistry()->registerStat(&d_instantiations_auto_gen
);
1116 smtStatisticsRegistry()->registerStat(&d_instantiations_guess
);
1117 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf
);
1118 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf_prop
);
1119 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_exh
);
1120 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_mbqi
);
1121 smtStatisticsRegistry()->registerStat(&d_instantiations_cbqi
);
1122 smtStatisticsRegistry()->registerStat(&d_instantiations_rr
);
1125 QuantifiersEngine::Statistics::~Statistics(){
1126 smtStatisticsRegistry()->unregisterStat(&d_time
);
1127 smtStatisticsRegistry()->unregisterStat(&d_qcf_time
);
1128 smtStatisticsRegistry()->unregisterStat(&d_ematching_time
);
1129 smtStatisticsRegistry()->unregisterStat(&d_num_quant
);
1130 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds
);
1131 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds_lc
);
1132 smtStatisticsRegistry()->unregisterStat(&d_triggers
);
1133 smtStatisticsRegistry()->unregisterStat(&d_simple_triggers
);
1134 smtStatisticsRegistry()->unregisterStat(&d_multi_triggers
);
1135 smtStatisticsRegistry()->unregisterStat(&d_multi_trigger_instantiations
);
1136 smtStatisticsRegistry()->unregisterStat(&d_red_alpha_equiv
);
1137 smtStatisticsRegistry()->unregisterStat(&d_instantiations_user_patterns
);
1138 smtStatisticsRegistry()->unregisterStat(&d_instantiations_auto_gen
);
1139 smtStatisticsRegistry()->unregisterStat(&d_instantiations_guess
);
1140 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf
);
1141 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf_prop
);
1142 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_exh
);
1143 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_mbqi
);
1144 smtStatisticsRegistry()->unregisterStat(&d_instantiations_cbqi
);
1145 smtStatisticsRegistry()->unregisterStat(&d_instantiations_rr
);
1148 eq::EqualityEngine
* QuantifiersEngine::getMasterEqualityEngine() const
1150 return d_masterEqualityEngine
;
1153 Node
QuantifiersEngine::getInternalRepresentative( Node a
, Node q
, int index
){
1154 return d_eq_query
->getInternalRepresentative(a
, q
, index
);
1157 bool QuantifiersEngine::getSynthSolutions(
1158 std::map
<Node
, std::map
<Node
, Node
> >& sol_map
)
1160 return d_qmodules
->d_synth_e
->getSynthSolutions(sol_map
);
1163 void QuantifiersEngine::debugPrintEqualityEngine( const char * c
) {
1164 eq::EqualityEngine
* ee
= getMasterEqualityEngine();
1165 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( ee
);
1166 std::map
< TypeNode
, int > typ_num
;
1167 while( !eqcs_i
.isFinished() ){
1168 TNode r
= (*eqcs_i
);
1169 TypeNode tr
= r
.getType();
1170 if( typ_num
.find( tr
)==typ_num
.end() ){
1174 bool firstTime
= true;
1175 Trace(c
) << " " << r
;
1176 Trace(c
) << " : { ";
1177 eq::EqClassIterator eqc_i
= eq::EqClassIterator( r
, ee
);
1178 while( !eqc_i
.isFinished() ){
1182 Trace(c
) << std::endl
;
1185 Trace(c
) << " " << n
<< std::endl
;
1189 if( !firstTime
){ Trace(c
) << " "; }
1190 Trace(c
) << "}" << std::endl
;
1193 Trace(c
) << std::endl
;
1194 for( std::map
< TypeNode
, int >::iterator it
= typ_num
.begin(); it
!= typ_num
.end(); ++it
){
1195 Trace(c
) << "# eqc for " << it
->first
<< " : " << it
->second
<< std::endl
;
1199 } // namespace theory