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(
34 quantifiers::QuantifiersState
& qstate
,
35 quantifiers::QuantifiersInferenceManager
& qim
,
36 ProofNodeManager
* pnm
)
40 d_decManager(nullptr),
41 d_masterEqualityEngine(nullptr),
42 d_eq_query(new quantifiers::EqualityQueryQuantifiersEngine(qstate
, this)),
43 d_tr_trie(new inst::TriggerTrie
),
46 d_term_util(new quantifiers::TermUtil
),
47 d_term_canon(new expr::TermCanonize
),
48 d_term_db(new quantifiers::TermDb(qstate
, qim
, this)),
50 d_quant_attr(new quantifiers::QuantAttributes(this)),
51 d_instantiate(new quantifiers::Instantiate(this, qstate
, pnm
)),
52 d_skolemize(new quantifiers::Skolemize(this, qstate
, pnm
)),
53 d_term_enum(new quantifiers::TermEnumeration
),
54 d_conflict_c(qstate
.getSatContext(), false),
55 d_quants_prereg(qstate
.getUserContext()),
56 d_quants_red(qstate
.getUserContext()),
57 d_lemmas_produced_c(qstate
.getUserContext()),
58 d_ierCounter_c(qstate
.getSatContext()),
59 d_presolve(qstate
.getUserContext(), true),
60 d_presolve_in(qstate
.getUserContext()),
61 d_presolve_cache(qstate
.getUserContext()),
62 d_presolve_cache_wq(qstate
.getUserContext()),
63 d_presolve_cache_wic(qstate
.getUserContext())
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(this, qstate
, qim
));
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, qstate
, "FirstOrderModelFmc"));
110 d_builder
.reset(new quantifiers::fmcheck::FullModelChecker(this));
112 Trace("quant-engine-debug") << "...make default model builder." << std::endl
;
114 new quantifiers::FirstOrderModel(this, qstate
, "FirstOrderModel"));
115 d_builder
.reset(new quantifiers::QModelBuilder(this));
119 new quantifiers::FirstOrderModel(this, qstate
, "FirstOrderModel"));
123 QuantifiersEngine::~QuantifiersEngine() {}
125 void QuantifiersEngine::finishInit(TheoryEngine
* te
,
127 eq::EqualityEngine
* mee
)
131 d_masterEqualityEngine
= mee
;
132 // Initialize the modules and the utilities here.
133 d_qmodules
.reset(new quantifiers::QuantifiersModules
);
134 d_qmodules
->initialize(this, d_qstate
, d_qim
, d_modules
);
135 if (d_qmodules
->d_rel_dom
.get())
137 d_util
.push_back(d_qmodules
->d_rel_dom
.get());
141 TheoryEngine
* QuantifiersEngine::getTheoryEngine() const { return d_te
; }
143 DecisionManager
* QuantifiersEngine::getDecisionManager()
148 OutputChannel
& QuantifiersEngine::getOutputChannel()
150 return d_te
->theoryOf(THEORY_QUANTIFIERS
)->getOutputChannel();
152 /** get default valuation for the quantifiers engine */
153 Valuation
& QuantifiersEngine::getValuation()
155 return d_te
->theoryOf(THEORY_QUANTIFIERS
)->getValuation();
158 const LogicInfo
& QuantifiersEngine::getLogicInfo() const
160 return d_te
->getLogicInfo();
163 EqualityQuery
* QuantifiersEngine::getEqualityQuery() const
165 return d_eq_query
.get();
167 quantifiers::QModelBuilder
* QuantifiersEngine::getModelBuilder() const
169 return d_builder
.get();
171 quantifiers::FirstOrderModel
* QuantifiersEngine::getModel() const
173 return d_model
.get();
175 quantifiers::TermDb
* QuantifiersEngine::getTermDatabase() const
177 return d_term_db
.get();
179 quantifiers::TermDbSygus
* QuantifiersEngine::getTermDatabaseSygus() const
181 return d_sygus_tdb
.get();
183 quantifiers::TermUtil
* QuantifiersEngine::getTermUtil() const
185 return d_term_util
.get();
187 expr::TermCanonize
* QuantifiersEngine::getTermCanonize() const
189 return d_term_canon
.get();
191 quantifiers::QuantAttributes
* QuantifiersEngine::getQuantAttributes() const
193 return d_quant_attr
.get();
195 quantifiers::Instantiate
* QuantifiersEngine::getInstantiate() const
197 return d_instantiate
.get();
199 quantifiers::Skolemize
* QuantifiersEngine::getSkolemize() const
201 return d_skolemize
.get();
203 quantifiers::TermEnumeration
* QuantifiersEngine::getTermEnumeration() const
205 return d_term_enum
.get();
207 inst::TriggerTrie
* QuantifiersEngine::getTriggerDatabase() const
209 return d_tr_trie
.get();
212 QuantifiersModule
* QuantifiersEngine::getOwner( Node q
) {
213 std::map
< Node
, QuantifiersModule
* >::iterator it
= d_owner
.find( q
);
214 if( it
==d_owner
.end() ){
221 void QuantifiersEngine::setOwner( Node q
, QuantifiersModule
* m
, int priority
) {
222 QuantifiersModule
* mo
= getOwner( q
);
225 if( priority
<=d_owner_priority
[q
] ){
226 Trace("quant-warn") << "WARNING: setting owner of " << q
<< " to " << ( m
? m
->identify() : "null" ) << ", but already has owner " << mo
->identify() << " with higher priority!" << std::endl
;
231 d_owner_priority
[q
] = priority
;
235 void QuantifiersEngine::setOwner(Node q
, quantifiers::QAttributes
& qa
)
237 if (qa
.d_sygus
|| (options::sygusRecFun() && !qa
.d_fundef_f
.isNull()))
239 if (d_qmodules
->d_synth_e
.get() == nullptr)
241 Trace("quant-warn") << "WARNING : synth engine is null, and we have : "
244 // set synth engine as owner since this is either a conjecture or a function
245 // definition to be used by sygus
246 setOwner(q
, d_qmodules
->d_synth_e
.get(), 2);
250 bool QuantifiersEngine::hasOwnership( Node q
, QuantifiersModule
* m
) {
251 QuantifiersModule
* mo
= getOwner( q
);
252 return mo
==m
|| mo
==NULL
;
255 bool QuantifiersEngine::isFiniteBound(Node q
, Node v
) const
257 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
258 if (bi
&& bi
->isBound(q
, v
))
262 TypeNode tn
= v
.getType();
263 if (tn
.isSort() && options::finiteModelFind())
267 else if (d_term_enum
->mayComplete(tn
))
274 BoundVarType
QuantifiersEngine::getBoundVarType(Node q
, Node v
) const
276 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
279 return bi
->getBoundVarType(q
, v
);
281 return isFiniteBound(q
, v
) ? BOUND_FINITE
: BOUND_NONE
;
284 void QuantifiersEngine::getBoundVarIndices(Node q
,
285 std::vector
<unsigned>& indices
) const
287 Assert(indices
.empty());
288 // we take the bounded variables first
289 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
292 bi
->getBoundVarIndices(q
, indices
);
294 // then get the remaining ones
295 for (unsigned i
= 0, nvars
= q
[0].getNumChildren(); i
< nvars
; i
++)
297 if (std::find(indices
.begin(), indices
.end(), i
) == indices
.end())
299 indices
.push_back(i
);
304 bool QuantifiersEngine::getBoundElements(RepSetIterator
* rsi
,
308 std::vector
<Node
>& elements
) const
310 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
313 return bi
->getBoundElements(rsi
, initial
, q
, v
, elements
);
318 void QuantifiersEngine::presolve() {
319 Trace("quant-engine-proc") << "QuantifiersEngine : presolve " << std::endl
;
320 d_lemmas_waiting
.clear();
321 d_phase_req_waiting
.clear();
322 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
323 d_modules
[i
]->presolve();
325 d_term_db
->presolve();
327 //add all terms to database
328 if( options::incrementalSolving() ){
329 Trace("quant-engine-proc") << "Add presolve cache " << d_presolve_cache
.size() << std::endl
;
330 for( unsigned i
=0; i
<d_presolve_cache
.size(); i
++ ){
331 addTermToDatabase( d_presolve_cache
[i
], d_presolve_cache_wq
[i
], d_presolve_cache_wic
[i
] );
333 Trace("quant-engine-proc") << "Done add presolve cache " << std::endl
;
337 void QuantifiersEngine::ppNotifyAssertions(
338 const std::vector
<Node
>& assertions
) {
339 Trace("quant-engine-proc")
340 << "ppNotifyAssertions in QE, #assertions = " << assertions
.size()
342 if (options::instLevelInputOnly() && options::instMaxLevel() != -1)
344 for (const Node
& a
: assertions
)
346 quantifiers::QuantAttributes::setInstantiationLevelAttr(a
, 0);
349 if (options::sygus())
351 quantifiers::SynthEngine
* sye
= d_qmodules
->d_synth_e
.get();
352 for (const Node
& a
: assertions
)
354 sye
->preregisterAssertion(a
);
357 /* The SyGuS instantiation module needs a global view of all available
358 * assertions to collect global terms that get added to each grammar.
360 if (options::sygusInst())
362 quantifiers::SygusInst
* si
= d_qmodules
->d_sygus_inst
.get();
363 si
->ppNotifyAssertions(assertions
);
367 void QuantifiersEngine::check( Theory::Effort e
){
368 CodeTimer
codeTimer(d_statistics
.d_time
);
370 if (!getMasterEqualityEngine()->consistent())
372 Trace("quant-engine-debug") << "Master equality engine not consistent, return." << std::endl
;
375 if (d_conflict_c
.get())
377 if (e
< Theory::EFFORT_LAST_CALL
)
379 // this can happen in rare cases when quantifiers is the first to realize
380 // there is a quantifier-free conflict, for example, when it discovers
381 // disequal and congruent terms in the master equality engine during
382 // term indexing. In such cases, quantifiers reports a "conflicting lemma"
383 // that is, one that is entailed to be false by the current assignment.
384 // If this lemma is not a SAT conflict, we may get another call to full
385 // effort check and the quantifier-free solvers still haven't realized
386 // there is a conflict. In this case, we return, trusting that theory
387 // combination will do the right thing (split on equalities until there is
388 // a conflict at the quantifier-free level).
389 Trace("quant-engine-debug")
390 << "Conflicting lemma already reported by quantifiers, return."
394 // we reported what we thought was a conflicting lemma, but now we have
395 // gotten a check at LAST_CALL effort, indicating that the lemma we reported
396 // was not conflicting. This should never happen, but in production mode, we
397 // proceed with the check.
400 bool needsCheck
= !d_lemmas_waiting
.empty();
401 QuantifiersModule::QEffort needsModelE
= QuantifiersModule::QEFFORT_NONE
;
402 std::vector
< QuantifiersModule
* > qm
;
403 if( d_model
->checkNeeded() ){
404 needsCheck
= needsCheck
|| e
>=Theory::EFFORT_LAST_CALL
; //always need to check at or above last call
405 for (QuantifiersModule
*& mdl
: d_modules
)
407 if (mdl
->needsCheck(e
))
411 //can only request model at last call since theory combination can find inconsistencies
412 if( e
>=Theory::EFFORT_LAST_CALL
){
413 QuantifiersModule::QEffort me
= mdl
->needsModel(e
);
414 needsModelE
= me
<needsModelE
? me
: needsModelE
;
421 d_hasAddedLemma
= false;
422 bool setIncomplete
= false;
424 Trace("quant-engine-debug2") << "Quantifiers Engine call to check, level = " << e
<< ", needsCheck=" << needsCheck
<< std::endl
;
426 //flush previous lemmas (for instance, if was interrupted), or other lemmas to process
428 if( d_hasAddedLemma
){
433 if( Trace
.isOn("quant-engine") ){
434 clSet
= double(clock())/double(CLOCKS_PER_SEC
);
435 Trace("quant-engine") << ">>>>> Quantifiers Engine Round, effort = " << e
<< " <<<<<" << std::endl
;
438 if( Trace
.isOn("quant-engine-debug") ){
439 Trace("quant-engine-debug") << "Quantifiers Engine check, level = " << e
<< std::endl
;
440 Trace("quant-engine-debug") << " depth : " << d_ierCounter_c
<< std::endl
;
441 Trace("quant-engine-debug") << " modules to check : ";
442 for( unsigned i
=0; i
<qm
.size(); i
++ ){
443 Trace("quant-engine-debug") << qm
[i
]->identify() << " ";
445 Trace("quant-engine-debug") << std::endl
;
446 Trace("quant-engine-debug") << " # quantified formulas = " << d_model
->getNumAssertedQuantifiers() << std::endl
;
447 if( !d_lemmas_waiting
.empty() ){
448 Trace("quant-engine-debug") << " lemmas waiting = " << d_lemmas_waiting
.size() << std::endl
;
450 Trace("quant-engine-debug")
451 << " Theory engine finished : " << !theoryEngineNeedsCheck()
453 Trace("quant-engine-debug") << " Needs model effort : " << needsModelE
<< std::endl
;
454 Trace("quant-engine-debug")
455 << " In conflict : " << d_conflict
<< std::endl
;
457 if( Trace
.isOn("quant-engine-ee-pre") ){
458 Trace("quant-engine-ee-pre") << "Equality engine (pre-inference): " << std::endl
;
459 debugPrintEqualityEngine( "quant-engine-ee-pre" );
461 if( Trace
.isOn("quant-engine-assert") ){
462 Trace("quant-engine-assert") << "Assertions : " << std::endl
;
463 getTheoryEngine()->printAssertions("quant-engine-assert");
467 Trace("quant-engine-debug") << "Resetting all utilities..." << std::endl
;
468 for (QuantifiersUtil
*& util
: d_util
)
470 Trace("quant-engine-debug2") << "Reset " << util
->identify().c_str()
471 << "..." << std::endl
;
475 if( d_hasAddedLemma
){
478 //should only fail reset if added a lemma
484 if( Trace
.isOn("quant-engine-ee") ){
485 Trace("quant-engine-ee") << "Equality engine : " << std::endl
;
486 debugPrintEqualityEngine( "quant-engine-ee" );
490 Trace("quant-engine-debug") << "Reset model..." << std::endl
;
491 d_model
->reset_round();
494 Trace("quant-engine-debug") << "Resetting all modules..." << std::endl
;
495 for (QuantifiersModule
*& mdl
: d_modules
)
497 Trace("quant-engine-debug2") << "Reset " << mdl
->identify().c_str()
501 Trace("quant-engine-debug") << "Done resetting all modules." << std::endl
;
502 //reset may have added lemmas
504 if( d_hasAddedLemma
){
508 if( e
==Theory::EFFORT_LAST_CALL
){
509 ++(d_statistics
.d_instantiation_rounds_lc
);
510 }else if( e
==Theory::EFFORT_FULL
){
511 ++(d_statistics
.d_instantiation_rounds
);
513 Trace("quant-engine-debug") << "Check modules that needed check..." << std::endl
;
514 for (unsigned qef
= QuantifiersModule::QEFFORT_CONFLICT
;
515 qef
<= QuantifiersModule::QEFFORT_LAST_CALL
;
518 QuantifiersModule::QEffort quant_e
=
519 static_cast<QuantifiersModule::QEffort
>(qef
);
520 d_curr_effort_level
= quant_e
;
521 // Force the theory engine to build the model if any module requested it.
522 if (needsModelE
== quant_e
)
524 Trace("quant-engine-debug") << "Build model..." << std::endl
;
525 if (!d_te
->buildModel())
527 // If we failed to build the model, flush all pending lemmas and
533 if( !d_hasAddedLemma
){
535 for (QuantifiersModule
*& mdl
: qm
)
537 Trace("quant-engine-debug") << "Check " << mdl
->identify().c_str()
538 << " at effort " << quant_e
<< "..."
540 mdl
->check(e
, quant_e
);
542 Trace("quant-engine-debug") << "...conflict!" << std::endl
;
546 //flush all current lemmas
549 //if we have added one, stop
550 if( d_hasAddedLemma
){
554 if (quant_e
== QuantifiersModule::QEFFORT_CONFLICT
)
556 if( e
==Theory::EFFORT_FULL
){
557 //increment if a last call happened, we are not strictly enforcing interleaving, or already were in phase
558 if( d_ierCounterLastLc
!=d_ierCounter_lc
|| !options::instWhenStrictInterleave() || d_ierCounter
%d_inst_when_phase
!=0 ){
559 d_ierCounter
= d_ierCounter
+ 1;
560 d_ierCounterLastLc
= d_ierCounter_lc
;
561 d_ierCounter_c
= d_ierCounter_c
.get() + 1;
563 }else if( e
==Theory::EFFORT_LAST_CALL
){
564 d_ierCounter_lc
= d_ierCounter_lc
+ 1;
567 else if (quant_e
== QuantifiersModule::QEFFORT_MODEL
)
569 if( e
==Theory::EFFORT_LAST_CALL
){
570 //sources of incompleteness
571 for (QuantifiersUtil
*& util
: d_util
)
573 if (!util
->checkComplete())
575 Trace("quant-engine-debug") << "Set incomplete because utility "
576 << util
->identify().c_str()
577 << " was incomplete." << std::endl
;
578 setIncomplete
= true;
581 if (d_conflict_c
.get())
583 // we reported a conflicting lemma, should return
584 setIncomplete
= true;
586 //if we have a chance not to set incomplete
587 if( !setIncomplete
){
588 //check if we should set the incomplete flag
589 for (QuantifiersModule
*& mdl
: d_modules
)
591 if (!mdl
->checkComplete())
593 Trace("quant-engine-debug")
594 << "Set incomplete because module "
595 << mdl
->identify().c_str() << " was incomplete."
597 setIncomplete
= true;
601 if( !setIncomplete
){
602 //look at individual quantified formulas, one module must claim completeness for each one
603 for( unsigned i
=0; i
<d_model
->getNumAssertedQuantifiers(); i
++ ){
604 bool hasCompleteM
= false;
605 Node q
= d_model
->getAssertedQuantifier( i
);
606 QuantifiersModule
* qmd
= getOwner( q
);
608 hasCompleteM
= qmd
->checkCompleteFor( q
);
610 for( unsigned j
=0; j
<d_modules
.size(); j
++ ){
611 if( d_modules
[j
]->checkCompleteFor( q
) ){
619 Trace("quant-engine-debug") << "Set incomplete because " << q
<< " was not fully processed." << std::endl
;
620 setIncomplete
= true;
624 Trace("quant-engine-debug2") << "Complete for " << q
<< " due to " << qmd
->identify().c_str() << std::endl
;
629 //if setIncomplete = false, we will answer SAT, otherwise we will run at quant_e QEFFORT_LAST_CALL
630 if( !setIncomplete
){
637 d_curr_effort_level
= QuantifiersModule::QEFFORT_NONE
;
638 Trace("quant-engine-debug") << "Done check modules that needed check." << std::endl
;
642 bool debugInstTrace
= Trace
.isOn("inst-per-quant-round");
643 if (options::debugInst() || debugInstTrace
)
645 Options
& sopts
= smt::currentSmtEngine()->getOptions();
646 std::ostream
& out
= *sopts
.getOut();
647 d_instantiate
->debugPrint(out
);
650 if( Trace
.isOn("quant-engine") ){
651 double clSet2
= double(clock())/double(CLOCKS_PER_SEC
);
652 Trace("quant-engine") << "Finished quantifiers engine, total time = " << (clSet2
-clSet
);
653 Trace("quant-engine") << ", added lemma = " << d_hasAddedLemma
;
654 Trace("quant-engine") << std::endl
;
657 Trace("quant-engine-debug2") << "Finished quantifiers engine check." << std::endl
;
659 Trace("quant-engine-debug2") << "Quantifiers Engine does not need check." << std::endl
;
663 if( e
==Theory::EFFORT_LAST_CALL
&& !d_hasAddedLemma
){
665 Trace("quant-engine") << "Set incomplete flag." << std::endl
;
666 getOutputChannel().setIncomplete();
669 d_instantiate
->debugPrintModel();
673 void QuantifiersEngine::notifyCombineTheories() {
674 //if allowing theory combination to happen at most once between instantiation rounds
676 //d_ierCounterLastLc = -1;
679 bool QuantifiersEngine::reduceQuantifier( Node q
) {
680 //TODO: this can be unified with preregistration: AlphaEquivalence takes ownership of reducable quants
681 BoolMap::const_iterator it
= d_quants_red
.find( q
);
682 if( it
==d_quants_red
.end() ){
684 std::map
< Node
, Node
>::iterator itr
= d_quants_red_lem
.find( q
);
685 if( itr
==d_quants_red_lem
.end() ){
686 if (d_qmodules
->d_alpha_equiv
)
688 Trace("quant-engine-red") << "Alpha equivalence " << q
<< "?" << std::endl
;
689 //add equivalence with another quantified formula
690 lem
= d_qmodules
->d_alpha_equiv
->reduceQuantifier(q
);
692 Trace("quant-engine-red") << "...alpha equivalence success." << std::endl
;
693 ++(d_statistics
.d_red_alpha_equiv
);
696 d_quants_red_lem
[q
] = lem
;
701 getOutputChannel().lemma( lem
);
703 d_quants_red
[q
] = !lem
.isNull();
704 return !lem
.isNull();
710 void QuantifiersEngine::registerQuantifierInternal(Node f
)
712 std::map
< Node
, bool >::iterator it
= d_quants
.find( f
);
713 if( it
==d_quants
.end() ){
714 Trace("quant") << "QuantifiersEngine : Register quantifier ";
715 Trace("quant") << " : " << f
<< std::endl
;
716 unsigned prev_lemma_waiting
= d_lemmas_waiting
.size();
717 ++(d_statistics
.d_num_quant
);
718 Assert(f
.getKind() == FORALL
);
719 // register with utilities
720 for (unsigned i
= 0; i
< d_util
.size(); i
++)
722 d_util
[i
]->registerQuantifier(f
);
724 // compute attributes
725 d_quant_attr
->computeAttributes(f
);
727 for (QuantifiersModule
*& mdl
: d_modules
)
729 Trace("quant-debug") << "check ownership with " << mdl
->identify()
730 << "..." << std::endl
;
731 mdl
->checkOwnership(f
);
733 QuantifiersModule
* qm
= getOwner(f
);
734 Trace("quant") << " Owner : " << (qm
== nullptr ? "[none]" : qm
->identify())
736 // register with each module
737 for (QuantifiersModule
*& mdl
: d_modules
)
739 Trace("quant-debug") << "register with " << mdl
->identify() << "..."
741 mdl
->registerQuantifier(f
);
742 // since this is context-independent, we should not add any lemmas during
744 Assert(d_lemmas_waiting
.size() == prev_lemma_waiting
);
746 Trace("quant-debug") << "...finish." << std::endl
;
748 AlwaysAssert(d_lemmas_waiting
.size() == prev_lemma_waiting
);
752 void QuantifiersEngine::preRegisterQuantifier(Node q
)
754 NodeSet::const_iterator it
= d_quants_prereg
.find(q
);
755 if (it
!= d_quants_prereg
.end())
759 Trace("quant-debug") << "QuantifiersEngine : Pre-register " << q
<< std::endl
;
760 d_quants_prereg
.insert(q
);
762 if (reduceQuantifier(q
))
764 // if we can reduce it, nothing left to do
767 // ensure that it is registered
768 registerQuantifierInternal(q
);
769 // register with each module
770 for (QuantifiersModule
*& mdl
: d_modules
)
772 Trace("quant-debug") << "pre-register with " << mdl
->identify() << "..."
774 mdl
->preRegisterQuantifier(q
);
778 Trace("quant-debug") << "...finish pre-register " << q
<< "..." << std::endl
;
781 void QuantifiersEngine::registerPattern( std::vector
<Node
> & pattern
) {
782 for(std::vector
<Node
>::iterator p
= pattern
.begin(); p
!= pattern
.end(); ++p
){
783 std::set
< Node
> added
;
784 getTermDatabase()->addTerm( *p
, added
);
788 void QuantifiersEngine::assertQuantifier( Node f
, bool pol
){
789 if (reduceQuantifier(f
))
791 // if we can reduce it, nothing left to do
796 TrustNode lem
= d_skolemize
->process(f
);
799 if (Trace
.isOn("quantifiers-sk-debug"))
801 Node slem
= Rewriter::rewrite(lem
.getNode());
802 Trace("quantifiers-sk-debug")
803 << "Skolemize lemma : " << slem
<< std::endl
;
805 getOutputChannel().trustedLemma(
806 lem
, LemmaProperty::PREPROCESS
| LemmaProperty::NEEDS_JUSTIFY
);
810 // ensure the quantified formula is registered
811 registerQuantifierInternal(f
);
812 // assert it to each module
813 d_model
->assertQuantifier(f
);
814 for (QuantifiersModule
*& mdl
: d_modules
)
818 addTermToDatabase(d_term_util
->getInstConstantBody(f
), true);
821 void QuantifiersEngine::addTermToDatabase( Node n
, bool withinQuant
, bool withinInstClosure
){
822 if( options::incrementalSolving() ){
823 if( d_presolve_in
.find( n
)==d_presolve_in
.end() ){
824 d_presolve_in
.insert( n
);
825 d_presolve_cache
.push_back( n
);
826 d_presolve_cache_wq
.push_back( withinQuant
);
827 d_presolve_cache_wic
.push_back( withinInstClosure
);
830 //only wait if we are doing incremental solving
831 if( !d_presolve
|| !options::incrementalSolving() ){
832 std::set
< Node
> added
;
833 d_term_db
->addTerm(n
, added
, withinQuant
, withinInstClosure
);
837 if (d_sygus_tdb
&& options::sygusEvalUnfold())
839 d_sygus_tdb
->getEvalUnfold()->registerEvalTerm(n
);
845 void QuantifiersEngine::eqNotifyNewClass(TNode t
) {
846 addTermToDatabase( t
);
849 bool QuantifiersEngine::addLemma( Node lem
, bool doCache
, bool doRewrite
){
852 lem
= Rewriter::rewrite(lem
);
854 Trace("inst-add-debug") << "Adding lemma : " << lem
<< std::endl
;
855 BoolMap::const_iterator itp
= d_lemmas_produced_c
.find( lem
);
856 if( itp
==d_lemmas_produced_c
.end() || !(*itp
).second
){
857 d_lemmas_produced_c
[ lem
] = true;
858 d_lemmas_waiting
.push_back( lem
);
859 Trace("inst-add-debug") << "Added lemma" << std::endl
;
862 Trace("inst-add-debug") << "Duplicate." << std::endl
;
866 //do not need to rewrite, will be rewritten after sending
867 d_lemmas_waiting
.push_back( lem
);
872 bool QuantifiersEngine::addTrustedLemma(TrustNode tlem
,
876 Node lem
= tlem
.getProven();
877 if (!addLemma(lem
, doCache
, doRewrite
))
881 d_lemmasWaitingPg
[lem
] = tlem
.getGenerator();
885 bool QuantifiersEngine::removeLemma( Node lem
) {
886 std::vector
< Node
>::iterator it
= std::find( d_lemmas_waiting
.begin(), d_lemmas_waiting
.end(), lem
);
887 if( it
!=d_lemmas_waiting
.end() ){
888 d_lemmas_waiting
.erase( it
, it
+ 1 );
889 d_lemmas_produced_c
[ lem
] = false;
896 void QuantifiersEngine::addRequirePhase( Node lit
, bool req
){
897 d_phase_req_waiting
[lit
] = req
;
900 void QuantifiersEngine::markRelevant( Node q
) {
901 d_model
->markRelevant( q
);
903 bool QuantifiersEngine::hasAddedLemma() const
905 return !d_lemmas_waiting
.empty() || d_hasAddedLemma
;
907 bool QuantifiersEngine::theoryEngineNeedsCheck() const
909 return d_te
->needCheck();
912 void QuantifiersEngine::setConflict()
918 bool QuantifiersEngine::getInstWhenNeedsCheck( Theory::Effort e
) {
919 Trace("quant-engine-debug2") << "Get inst when needs check, counts=" << d_ierCounter
<< ", " << d_ierCounter_lc
<< std::endl
;
920 //determine if we should perform check, based on instWhenMode
921 bool performCheck
= false;
922 if (options::instWhenMode() == options::InstWhenMode::FULL
)
924 performCheck
= ( e
>= Theory::EFFORT_FULL
);
926 else if (options::instWhenMode() == options::InstWhenMode::FULL_DELAY
)
928 performCheck
= (e
>= Theory::EFFORT_FULL
) && !theoryEngineNeedsCheck();
930 else if (options::instWhenMode() == options::InstWhenMode::FULL_LAST_CALL
)
932 performCheck
= ( ( e
==Theory::EFFORT_FULL
&& d_ierCounter
%d_inst_when_phase
!=0 ) || e
==Theory::EFFORT_LAST_CALL
);
934 else if (options::instWhenMode()
935 == options::InstWhenMode::FULL_DELAY_LAST_CALL
)
937 performCheck
= ((e
== Theory::EFFORT_FULL
&& !theoryEngineNeedsCheck()
938 && d_ierCounter
% d_inst_when_phase
!= 0)
939 || e
== Theory::EFFORT_LAST_CALL
);
941 else if (options::instWhenMode() == options::InstWhenMode::LAST_CALL
)
943 performCheck
= ( e
>= Theory::EFFORT_LAST_CALL
);
952 options::UserPatMode
QuantifiersEngine::getInstUserPatMode()
954 if (options::userPatternsQuant() == options::UserPatMode::INTERLEAVE
)
956 return d_ierCounter
% 2 == 0 ? options::UserPatMode::USE
957 : options::UserPatMode::RESORT
;
961 return options::userPatternsQuant();
965 void QuantifiersEngine::flushLemmas(){
966 OutputChannel
& out
= getOutputChannel();
967 if( !d_lemmas_waiting
.empty() ){
968 //take default output channel if none is provided
969 d_hasAddedLemma
= true;
970 std::map
<Node
, ProofGenerator
*>::iterator itp
;
971 // Note: Do not use foreach loop here and do not cache size() call.
972 // New lemmas can be added while iterating over d_lemmas_waiting.
973 for (size_t i
= 0; i
< d_lemmas_waiting
.size(); ++i
)
975 const Node
& lemw
= d_lemmas_waiting
[i
];
976 Trace("qe-lemma") << "Lemma : " << lemw
<< std::endl
;
977 itp
= d_lemmasWaitingPg
.find(lemw
);
979 LemmaProperty::PREPROCESS
| LemmaProperty::NEEDS_JUSTIFY
;
980 if (itp
!= d_lemmasWaitingPg
.end())
982 TrustNode tlemw
= TrustNode::mkTrustLemma(lemw
, itp
->second
);
983 out
.trustedLemma(tlemw
, p
);
990 d_lemmas_waiting
.clear();
992 if( !d_phase_req_waiting
.empty() ){
993 for( std::map
< Node
, bool >::iterator it
= d_phase_req_waiting
.begin(); it
!= d_phase_req_waiting
.end(); ++it
){
994 Trace("qe-lemma") << "Require phase : " << it
->first
<< " -> " << it
->second
<< std::endl
;
995 out
.requirePhase(it
->first
, it
->second
);
997 d_phase_req_waiting
.clear();
1001 void QuantifiersEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
1002 d_instantiate
->getInstantiationTermVectors(q
, tvecs
);
1005 void QuantifiersEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
1006 d_instantiate
->getInstantiationTermVectors(insts
);
1009 void QuantifiersEngine::printInstantiations( std::ostream
& out
) {
1010 bool printed
= false;
1011 // print the skolemizations
1012 if (options::printInstMode() == options::PrintInstMode::LIST
)
1014 if (d_skolemize
->printSkolemization(out
))
1019 // print the instantiations
1020 if (d_instantiate
->printInstantiations(out
))
1025 out
<< "No instantiations" << std::endl
;
1029 void QuantifiersEngine::printSynthSolution( std::ostream
& out
) {
1030 if (d_qmodules
->d_synth_e
)
1032 d_qmodules
->d_synth_e
->printSynthSolution(out
);
1034 out
<< "Internal error : module for synth solution not found." << std::endl
;
1038 void QuantifiersEngine::getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
) {
1039 d_instantiate
->getInstantiatedQuantifiedFormulas(qs
);
1042 QuantifiersEngine::Statistics::Statistics()
1043 : d_time("theory::QuantifiersEngine::time"),
1044 d_qcf_time("theory::QuantifiersEngine::time_qcf"),
1045 d_ematching_time("theory::QuantifiersEngine::time_ematching"),
1046 d_num_quant("QuantifiersEngine::Num_Quantifiers", 0),
1047 d_instantiation_rounds("QuantifiersEngine::Rounds_Instantiation_Full", 0),
1048 d_instantiation_rounds_lc("QuantifiersEngine::Rounds_Instantiation_Last_Call", 0),
1049 d_triggers("QuantifiersEngine::Triggers", 0),
1050 d_simple_triggers("QuantifiersEngine::Triggers_Simple", 0),
1051 d_multi_triggers("QuantifiersEngine::Triggers_Multi", 0),
1052 d_multi_trigger_instantiations("QuantifiersEngine::Multi_Trigger_Instantiations", 0),
1053 d_red_alpha_equiv("QuantifiersEngine::Reductions_Alpha_Equivalence", 0),
1054 d_instantiations_user_patterns("QuantifiersEngine::Instantiations_User_Patterns", 0),
1055 d_instantiations_auto_gen("QuantifiersEngine::Instantiations_Auto_Gen", 0),
1056 d_instantiations_guess("QuantifiersEngine::Instantiations_Guess", 0),
1057 d_instantiations_qcf("QuantifiersEngine::Instantiations_Qcf_Conflict", 0),
1058 d_instantiations_qcf_prop("QuantifiersEngine::Instantiations_Qcf_Prop", 0),
1059 d_instantiations_fmf_exh("QuantifiersEngine::Instantiations_Fmf_Exh", 0),
1060 d_instantiations_fmf_mbqi("QuantifiersEngine::Instantiations_Fmf_Mbqi", 0),
1061 d_instantiations_cbqi("QuantifiersEngine::Instantiations_Cbqi", 0),
1062 d_instantiations_rr("QuantifiersEngine::Instantiations_Rewrite_Rules", 0)
1064 smtStatisticsRegistry()->registerStat(&d_time
);
1065 smtStatisticsRegistry()->registerStat(&d_qcf_time
);
1066 smtStatisticsRegistry()->registerStat(&d_ematching_time
);
1067 smtStatisticsRegistry()->registerStat(&d_num_quant
);
1068 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds
);
1069 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds_lc
);
1070 smtStatisticsRegistry()->registerStat(&d_triggers
);
1071 smtStatisticsRegistry()->registerStat(&d_simple_triggers
);
1072 smtStatisticsRegistry()->registerStat(&d_multi_triggers
);
1073 smtStatisticsRegistry()->registerStat(&d_multi_trigger_instantiations
);
1074 smtStatisticsRegistry()->registerStat(&d_red_alpha_equiv
);
1075 smtStatisticsRegistry()->registerStat(&d_instantiations_user_patterns
);
1076 smtStatisticsRegistry()->registerStat(&d_instantiations_auto_gen
);
1077 smtStatisticsRegistry()->registerStat(&d_instantiations_guess
);
1078 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf
);
1079 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf_prop
);
1080 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_exh
);
1081 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_mbqi
);
1082 smtStatisticsRegistry()->registerStat(&d_instantiations_cbqi
);
1083 smtStatisticsRegistry()->registerStat(&d_instantiations_rr
);
1086 QuantifiersEngine::Statistics::~Statistics(){
1087 smtStatisticsRegistry()->unregisterStat(&d_time
);
1088 smtStatisticsRegistry()->unregisterStat(&d_qcf_time
);
1089 smtStatisticsRegistry()->unregisterStat(&d_ematching_time
);
1090 smtStatisticsRegistry()->unregisterStat(&d_num_quant
);
1091 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds
);
1092 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds_lc
);
1093 smtStatisticsRegistry()->unregisterStat(&d_triggers
);
1094 smtStatisticsRegistry()->unregisterStat(&d_simple_triggers
);
1095 smtStatisticsRegistry()->unregisterStat(&d_multi_triggers
);
1096 smtStatisticsRegistry()->unregisterStat(&d_multi_trigger_instantiations
);
1097 smtStatisticsRegistry()->unregisterStat(&d_red_alpha_equiv
);
1098 smtStatisticsRegistry()->unregisterStat(&d_instantiations_user_patterns
);
1099 smtStatisticsRegistry()->unregisterStat(&d_instantiations_auto_gen
);
1100 smtStatisticsRegistry()->unregisterStat(&d_instantiations_guess
);
1101 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf
);
1102 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf_prop
);
1103 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_exh
);
1104 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_mbqi
);
1105 smtStatisticsRegistry()->unregisterStat(&d_instantiations_cbqi
);
1106 smtStatisticsRegistry()->unregisterStat(&d_instantiations_rr
);
1109 eq::EqualityEngine
* QuantifiersEngine::getMasterEqualityEngine() const
1111 return d_masterEqualityEngine
;
1114 Node
QuantifiersEngine::getInternalRepresentative( Node a
, Node q
, int index
){
1115 return d_eq_query
->getInternalRepresentative(a
, q
, index
);
1118 bool QuantifiersEngine::getSynthSolutions(
1119 std::map
<Node
, std::map
<Node
, Node
> >& sol_map
)
1121 return d_qmodules
->d_synth_e
->getSynthSolutions(sol_map
);
1124 void QuantifiersEngine::debugPrintEqualityEngine( const char * c
) {
1125 eq::EqualityEngine
* ee
= getMasterEqualityEngine();
1126 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( ee
);
1127 std::map
< TypeNode
, int > typ_num
;
1128 while( !eqcs_i
.isFinished() ){
1129 TNode r
= (*eqcs_i
);
1130 TypeNode tr
= r
.getType();
1131 if( typ_num
.find( tr
)==typ_num
.end() ){
1135 bool firstTime
= true;
1136 Trace(c
) << " " << r
;
1137 Trace(c
) << " : { ";
1138 eq::EqClassIterator eqc_i
= eq::EqClassIterator( r
, ee
);
1139 while( !eqc_i
.isFinished() ){
1143 Trace(c
) << std::endl
;
1146 Trace(c
) << " " << n
<< std::endl
;
1150 if( !firstTime
){ Trace(c
) << " "; }
1151 Trace(c
) << "}" << std::endl
;
1154 Trace(c
) << std::endl
;
1155 for( std::map
< TypeNode
, int >::iterator it
= typ_num
.begin(); it
!= typ_num
.end(); ++it
){
1156 Trace(c
) << "# eqc for " << it
->first
<< " : " << it
->second
<< std::endl
;
1160 } // namespace theory