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/printer_options.h"
18 #include "options/quantifiers_options.h"
19 #include "options/uf_options.h"
20 #include "smt/smt_engine_scope.h"
21 #include "smt/smt_statistics_registry.h"
22 #include "theory/quantifiers/fmf/full_model_check.h"
23 #include "theory/quantifiers/quantifiers_modules.h"
24 #include "theory/quantifiers/quantifiers_rewriter.h"
25 #include "theory/theory_engine.h"
26 #include "theory/uf/equality_engine.h"
29 using namespace CVC4::kind
;
34 QuantifiersEngine::QuantifiersEngine(
35 quantifiers::QuantifiersState
& qstate
,
36 quantifiers::QuantifiersInferenceManager
& qim
,
37 ProofNodeManager
* pnm
)
41 d_decManager(nullptr),
42 d_masterEqualityEngine(nullptr),
43 d_eq_query(new quantifiers::EqualityQueryQuantifiersEngine(qstate
, this)),
44 d_tr_trie(new inst::TriggerTrie
),
47 d_term_util(new quantifiers::TermUtil
),
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_quants_prereg(qstate
.getUserContext()),
55 d_quants_red(qstate
.getUserContext()),
56 d_lemmas_produced_c(qstate
.getUserContext()),
57 d_ierCounter_c(qstate
.getSatContext()),
58 d_presolve(qstate
.getUserContext(), true),
59 d_presolve_in(qstate
.getUserContext()),
60 d_presolve_cache(qstate
.getUserContext()),
61 d_presolve_cache_wq(qstate
.getUserContext()),
62 d_presolve_cache_wic(qstate
.getUserContext())
65 d_util
.push_back(d_eq_query
.get());
66 // term util must come before the other utilities
67 d_util
.push_back(d_term_util
.get());
68 d_util
.push_back(d_term_db
.get());
70 if (options::sygus() || options::sygusInst())
72 // must be constructed here since it is required for datatypes finistInit
73 d_sygus_tdb
.reset(new quantifiers::TermDbSygus(this, qstate
, qim
));
76 d_util
.push_back(d_instantiate
.get());
78 d_curr_effort_level
= QuantifiersModule::QEFFORT_NONE
;
79 d_hasAddedLemma
= false;
80 //don't add true lemma
81 d_lemmas_produced_c
[d_term_util
->d_true
] = true;
83 Trace("quant-engine-debug") << "Initialize quantifiers engine." << std::endl
;
84 Trace("quant-engine-debug") << "Initialize model, mbqi : " << options::mbqiMode() << std::endl
;
88 //allow theory combination to go first, once initially
89 d_ierCounter
= options::instWhenTcFirst() ? 0 : 1;
90 d_ierCounter_c
= d_ierCounter
;
92 d_ierCounterLastLc
= 0;
93 d_inst_when_phase
= 1 + ( options::instWhenPhase()<1 ? 1 : options::instWhenPhase() );
95 // Finite model finding requires specialized ways of building the model.
96 // We require constructing the model and model builder here, since it is
97 // required for initializing the CombinationEngine.
98 if (options::finiteModelFind() || options::fmfBound())
100 Trace("quant-engine-debug") << "Initialize model engine, mbqi : " << options::mbqiMode() << " " << options::fmfBound() << std::endl
;
101 if (options::mbqiMode() == options::MbqiMode::FMC
102 || options::mbqiMode() == options::MbqiMode::TRUST
103 || options::fmfBound())
105 Trace("quant-engine-debug") << "...make fmc builder." << std::endl
;
106 d_model
.reset(new quantifiers::fmcheck::FirstOrderModelFmc(
107 this, qstate
, "FirstOrderModelFmc"));
108 d_builder
.reset(new quantifiers::fmcheck::FullModelChecker(this, qstate
));
110 Trace("quant-engine-debug") << "...make default model builder." << std::endl
;
112 new quantifiers::FirstOrderModel(this, qstate
, "FirstOrderModel"));
113 d_builder
.reset(new quantifiers::QModelBuilder(this, qstate
));
117 new quantifiers::FirstOrderModel(this, qstate
, "FirstOrderModel"));
121 QuantifiersEngine::~QuantifiersEngine() {}
123 void QuantifiersEngine::finishInit(TheoryEngine
* te
,
125 eq::EqualityEngine
* mee
)
129 d_masterEqualityEngine
= mee
;
130 // Initialize the modules and the utilities here.
131 d_qmodules
.reset(new quantifiers::QuantifiersModules
);
132 d_qmodules
->initialize(this, d_qstate
, d_qim
, d_modules
);
133 if (d_qmodules
->d_rel_dom
.get())
135 d_util
.push_back(d_qmodules
->d_rel_dom
.get());
139 TheoryEngine
* QuantifiersEngine::getTheoryEngine() const { return d_te
; }
141 DecisionManager
* QuantifiersEngine::getDecisionManager()
146 OutputChannel
& QuantifiersEngine::getOutputChannel()
148 return d_te
->theoryOf(THEORY_QUANTIFIERS
)->getOutputChannel();
150 /** get default valuation for the quantifiers engine */
151 Valuation
& QuantifiersEngine::getValuation() { return d_qstate
.getValuation(); }
153 EqualityQuery
* QuantifiersEngine::getEqualityQuery() const
155 return d_eq_query
.get();
157 quantifiers::QModelBuilder
* QuantifiersEngine::getModelBuilder() const
159 return d_builder
.get();
161 quantifiers::FirstOrderModel
* QuantifiersEngine::getModel() const
163 return d_model
.get();
165 quantifiers::TermDb
* QuantifiersEngine::getTermDatabase() const
167 return d_term_db
.get();
169 quantifiers::TermDbSygus
* QuantifiersEngine::getTermDatabaseSygus() const
171 return d_sygus_tdb
.get();
173 quantifiers::TermUtil
* QuantifiersEngine::getTermUtil() const
175 return d_term_util
.get();
177 quantifiers::QuantAttributes
* QuantifiersEngine::getQuantAttributes() const
179 return d_quant_attr
.get();
181 quantifiers::Instantiate
* QuantifiersEngine::getInstantiate() const
183 return d_instantiate
.get();
185 quantifiers::Skolemize
* QuantifiersEngine::getSkolemize() const
187 return d_skolemize
.get();
189 quantifiers::TermEnumeration
* QuantifiersEngine::getTermEnumeration() const
191 return d_term_enum
.get();
193 inst::TriggerTrie
* QuantifiersEngine::getTriggerDatabase() const
195 return d_tr_trie
.get();
198 QuantifiersModule
* QuantifiersEngine::getOwner( Node q
) {
199 std::map
< Node
, QuantifiersModule
* >::iterator it
= d_owner
.find( q
);
200 if( it
==d_owner
.end() ){
207 void QuantifiersEngine::setOwner( Node q
, QuantifiersModule
* m
, int priority
) {
208 QuantifiersModule
* mo
= getOwner( q
);
211 if( priority
<=d_owner_priority
[q
] ){
212 Trace("quant-warn") << "WARNING: setting owner of " << q
<< " to " << ( m
? m
->identify() : "null" ) << ", but already has owner " << mo
->identify() << " with higher priority!" << std::endl
;
217 d_owner_priority
[q
] = priority
;
221 void QuantifiersEngine::setOwner(Node q
, quantifiers::QAttributes
& qa
)
223 if (qa
.d_sygus
|| (options::sygusRecFun() && !qa
.d_fundef_f
.isNull()))
225 if (d_qmodules
->d_synth_e
.get() == nullptr)
227 Trace("quant-warn") << "WARNING : synth engine is null, and we have : "
230 // set synth engine as owner since this is either a conjecture or a function
231 // definition to be used by sygus
232 setOwner(q
, d_qmodules
->d_synth_e
.get(), 2);
236 bool QuantifiersEngine::hasOwnership( Node q
, QuantifiersModule
* m
) {
237 QuantifiersModule
* mo
= getOwner( q
);
238 return mo
==m
|| mo
==NULL
;
241 bool QuantifiersEngine::isFiniteBound(Node q
, Node v
) const
243 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
244 if (bi
&& bi
->isBound(q
, v
))
248 TypeNode tn
= v
.getType();
249 if (tn
.isSort() && options::finiteModelFind())
253 else if (d_term_enum
->mayComplete(tn
))
260 BoundVarType
QuantifiersEngine::getBoundVarType(Node q
, Node v
) const
262 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
265 return bi
->getBoundVarType(q
, v
);
267 return isFiniteBound(q
, v
) ? BOUND_FINITE
: BOUND_NONE
;
270 void QuantifiersEngine::getBoundVarIndices(Node q
,
271 std::vector
<unsigned>& indices
) const
273 Assert(indices
.empty());
274 // we take the bounded variables first
275 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
278 bi
->getBoundVarIndices(q
, indices
);
280 // then get the remaining ones
281 for (unsigned i
= 0, nvars
= q
[0].getNumChildren(); i
< nvars
; i
++)
283 if (std::find(indices
.begin(), indices
.end(), i
) == indices
.end())
285 indices
.push_back(i
);
290 bool QuantifiersEngine::getBoundElements(RepSetIterator
* rsi
,
294 std::vector
<Node
>& elements
) const
296 quantifiers::BoundedIntegers
* bi
= d_qmodules
->d_bint
.get();
299 return bi
->getBoundElements(rsi
, initial
, q
, v
, elements
);
304 void QuantifiersEngine::presolve() {
305 Trace("quant-engine-proc") << "QuantifiersEngine : presolve " << std::endl
;
306 d_lemmas_waiting
.clear();
307 d_phase_req_waiting
.clear();
308 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
309 d_modules
[i
]->presolve();
311 d_term_db
->presolve();
313 //add all terms to database
314 if( options::incrementalSolving() ){
315 Trace("quant-engine-proc") << "Add presolve cache " << d_presolve_cache
.size() << std::endl
;
316 for( unsigned i
=0; i
<d_presolve_cache
.size(); i
++ ){
317 addTermToDatabase( d_presolve_cache
[i
], d_presolve_cache_wq
[i
], d_presolve_cache_wic
[i
] );
319 Trace("quant-engine-proc") << "Done add presolve cache " << std::endl
;
323 void QuantifiersEngine::ppNotifyAssertions(
324 const std::vector
<Node
>& assertions
) {
325 Trace("quant-engine-proc")
326 << "ppNotifyAssertions in QE, #assertions = " << assertions
.size()
328 if (options::instLevelInputOnly() && options::instMaxLevel() != -1)
330 for (const Node
& a
: assertions
)
332 quantifiers::QuantAttributes::setInstantiationLevelAttr(a
, 0);
335 if (options::sygus())
337 quantifiers::SynthEngine
* sye
= d_qmodules
->d_synth_e
.get();
338 for (const Node
& a
: assertions
)
340 sye
->preregisterAssertion(a
);
343 /* The SyGuS instantiation module needs a global view of all available
344 * assertions to collect global terms that get added to each grammar.
346 if (options::sygusInst())
348 quantifiers::SygusInst
* si
= d_qmodules
->d_sygus_inst
.get();
349 si
->ppNotifyAssertions(assertions
);
353 void QuantifiersEngine::check( Theory::Effort e
){
354 CodeTimer
codeTimer(d_statistics
.d_time
);
356 if (!getMasterEqualityEngine()->consistent())
358 Trace("quant-engine-debug") << "Master equality engine not consistent, return." << std::endl
;
361 if (d_qstate
.isInConflict())
363 if (e
< Theory::EFFORT_LAST_CALL
)
365 // this can happen in rare cases when quantifiers is the first to realize
366 // there is a quantifier-free conflict, for example, when it discovers
367 // disequal and congruent terms in the master equality engine during
368 // term indexing. In such cases, quantifiers reports a "conflicting lemma"
369 // that is, one that is entailed to be false by the current assignment.
370 // If this lemma is not a SAT conflict, we may get another call to full
371 // effort check and the quantifier-free solvers still haven't realized
372 // there is a conflict. In this case, we return, trusting that theory
373 // combination will do the right thing (split on equalities until there is
374 // a conflict at the quantifier-free level).
375 Trace("quant-engine-debug")
376 << "Conflicting lemma already reported by quantifiers, return."
380 // we reported what we thought was a conflicting lemma, but now we have
381 // gotten a check at LAST_CALL effort, indicating that the lemma we reported
382 // was not conflicting. This should never happen, but in production mode, we
383 // proceed with the check.
386 bool needsCheck
= !d_lemmas_waiting
.empty();
387 QuantifiersModule::QEffort needsModelE
= QuantifiersModule::QEFFORT_NONE
;
388 std::vector
< QuantifiersModule
* > qm
;
389 if( d_model
->checkNeeded() ){
390 needsCheck
= needsCheck
|| e
>=Theory::EFFORT_LAST_CALL
; //always need to check at or above last call
391 for (QuantifiersModule
*& mdl
: d_modules
)
393 if (mdl
->needsCheck(e
))
397 //can only request model at last call since theory combination can find inconsistencies
398 if( e
>=Theory::EFFORT_LAST_CALL
){
399 QuantifiersModule::QEffort me
= mdl
->needsModel(e
);
400 needsModelE
= me
<needsModelE
? me
: needsModelE
;
406 d_hasAddedLemma
= false;
407 bool setIncomplete
= false;
409 Trace("quant-engine-debug2") << "Quantifiers Engine call to check, level = " << e
<< ", needsCheck=" << needsCheck
<< std::endl
;
411 //flush previous lemmas (for instance, if was interrupted), or other lemmas to process
413 if( d_hasAddedLemma
){
418 if( Trace
.isOn("quant-engine") ){
419 clSet
= double(clock())/double(CLOCKS_PER_SEC
);
420 Trace("quant-engine") << ">>>>> Quantifiers Engine Round, effort = " << e
<< " <<<<<" << std::endl
;
423 if( Trace
.isOn("quant-engine-debug") ){
424 Trace("quant-engine-debug") << "Quantifiers Engine check, level = " << e
<< std::endl
;
425 Trace("quant-engine-debug") << " depth : " << d_ierCounter_c
<< std::endl
;
426 Trace("quant-engine-debug") << " modules to check : ";
427 for( unsigned i
=0; i
<qm
.size(); i
++ ){
428 Trace("quant-engine-debug") << qm
[i
]->identify() << " ";
430 Trace("quant-engine-debug") << std::endl
;
431 Trace("quant-engine-debug") << " # quantified formulas = " << d_model
->getNumAssertedQuantifiers() << std::endl
;
432 if( !d_lemmas_waiting
.empty() ){
433 Trace("quant-engine-debug") << " lemmas waiting = " << d_lemmas_waiting
.size() << std::endl
;
435 Trace("quant-engine-debug")
436 << " Theory engine finished : "
437 << !d_qstate
.getValuation().needCheck() << std::endl
;
438 Trace("quant-engine-debug") << " Needs model effort : " << needsModelE
<< std::endl
;
439 Trace("quant-engine-debug")
440 << " In conflict : " << d_qstate
.isInConflict() << std::endl
;
442 if( Trace
.isOn("quant-engine-ee-pre") ){
443 Trace("quant-engine-ee-pre") << "Equality engine (pre-inference): " << std::endl
;
444 debugPrintEqualityEngine( "quant-engine-ee-pre" );
446 if( Trace
.isOn("quant-engine-assert") ){
447 Trace("quant-engine-assert") << "Assertions : " << std::endl
;
448 getTheoryEngine()->printAssertions("quant-engine-assert");
452 Trace("quant-engine-debug") << "Resetting all utilities..." << std::endl
;
453 for (QuantifiersUtil
*& util
: d_util
)
455 Trace("quant-engine-debug2") << "Reset " << util
->identify().c_str()
456 << "..." << std::endl
;
460 if( d_hasAddedLemma
){
463 //should only fail reset if added a lemma
469 if( Trace
.isOn("quant-engine-ee") ){
470 Trace("quant-engine-ee") << "Equality engine : " << std::endl
;
471 debugPrintEqualityEngine( "quant-engine-ee" );
475 Trace("quant-engine-debug") << "Reset model..." << std::endl
;
476 d_model
->reset_round();
479 Trace("quant-engine-debug") << "Resetting all modules..." << std::endl
;
480 for (QuantifiersModule
*& mdl
: d_modules
)
482 Trace("quant-engine-debug2") << "Reset " << mdl
->identify().c_str()
486 Trace("quant-engine-debug") << "Done resetting all modules." << std::endl
;
487 //reset may have added lemmas
489 if( d_hasAddedLemma
){
493 if( e
==Theory::EFFORT_LAST_CALL
){
494 ++(d_statistics
.d_instantiation_rounds_lc
);
495 }else if( e
==Theory::EFFORT_FULL
){
496 ++(d_statistics
.d_instantiation_rounds
);
498 Trace("quant-engine-debug") << "Check modules that needed check..." << std::endl
;
499 for (unsigned qef
= QuantifiersModule::QEFFORT_CONFLICT
;
500 qef
<= QuantifiersModule::QEFFORT_LAST_CALL
;
503 QuantifiersModule::QEffort quant_e
=
504 static_cast<QuantifiersModule::QEffort
>(qef
);
505 d_curr_effort_level
= quant_e
;
506 // Force the theory engine to build the model if any module requested it.
507 if (needsModelE
== quant_e
)
509 Trace("quant-engine-debug") << "Build model..." << std::endl
;
510 if (!d_te
->buildModel())
512 // If we failed to build the model, flush all pending lemmas and
518 if( !d_hasAddedLemma
){
520 for (QuantifiersModule
*& mdl
: qm
)
522 Trace("quant-engine-debug") << "Check " << mdl
->identify().c_str()
523 << " at effort " << quant_e
<< "..."
525 mdl
->check(e
, quant_e
);
526 if (d_qstate
.isInConflict())
528 Trace("quant-engine-debug") << "...conflict!" << std::endl
;
532 //flush all current lemmas
535 //if we have added one, stop
536 if( d_hasAddedLemma
){
539 Assert(!d_qstate
.isInConflict());
540 if (quant_e
== QuantifiersModule::QEFFORT_CONFLICT
)
542 if( e
==Theory::EFFORT_FULL
){
543 //increment if a last call happened, we are not strictly enforcing interleaving, or already were in phase
544 if( d_ierCounterLastLc
!=d_ierCounter_lc
|| !options::instWhenStrictInterleave() || d_ierCounter
%d_inst_when_phase
!=0 ){
545 d_ierCounter
= d_ierCounter
+ 1;
546 d_ierCounterLastLc
= d_ierCounter_lc
;
547 d_ierCounter_c
= d_ierCounter_c
.get() + 1;
549 }else if( e
==Theory::EFFORT_LAST_CALL
){
550 d_ierCounter_lc
= d_ierCounter_lc
+ 1;
553 else if (quant_e
== QuantifiersModule::QEFFORT_MODEL
)
555 if( e
==Theory::EFFORT_LAST_CALL
){
556 //sources of incompleteness
557 for (QuantifiersUtil
*& util
: d_util
)
559 if (!util
->checkComplete())
561 Trace("quant-engine-debug") << "Set incomplete because utility "
562 << util
->identify().c_str()
563 << " was incomplete." << std::endl
;
564 setIncomplete
= true;
567 if (d_qstate
.isInConflict())
569 // we reported a conflicting lemma, should return
570 setIncomplete
= true;
572 //if we have a chance not to set incomplete
573 if( !setIncomplete
){
574 //check if we should set the incomplete flag
575 for (QuantifiersModule
*& mdl
: d_modules
)
577 if (!mdl
->checkComplete())
579 Trace("quant-engine-debug")
580 << "Set incomplete because module "
581 << mdl
->identify().c_str() << " was incomplete."
583 setIncomplete
= true;
587 if( !setIncomplete
){
588 //look at individual quantified formulas, one module must claim completeness for each one
589 for( unsigned i
=0; i
<d_model
->getNumAssertedQuantifiers(); i
++ ){
590 bool hasCompleteM
= false;
591 Node q
= d_model
->getAssertedQuantifier( i
);
592 QuantifiersModule
* qmd
= getOwner( q
);
594 hasCompleteM
= qmd
->checkCompleteFor( q
);
596 for( unsigned j
=0; j
<d_modules
.size(); j
++ ){
597 if( d_modules
[j
]->checkCompleteFor( q
) ){
605 Trace("quant-engine-debug") << "Set incomplete because " << q
<< " was not fully processed." << std::endl
;
606 setIncomplete
= true;
610 Trace("quant-engine-debug2") << "Complete for " << q
<< " due to " << qmd
->identify().c_str() << std::endl
;
615 //if setIncomplete = false, we will answer SAT, otherwise we will run at quant_e QEFFORT_LAST_CALL
616 if( !setIncomplete
){
623 d_curr_effort_level
= QuantifiersModule::QEFFORT_NONE
;
624 Trace("quant-engine-debug") << "Done check modules that needed check." << std::endl
;
628 bool debugInstTrace
= Trace
.isOn("inst-per-quant-round");
629 if (options::debugInst() || debugInstTrace
)
631 Options
& sopts
= smt::currentSmtEngine()->getOptions();
632 std::ostream
& out
= *sopts
.getOut();
633 d_instantiate
->debugPrint(out
);
636 if( Trace
.isOn("quant-engine") ){
637 double clSet2
= double(clock())/double(CLOCKS_PER_SEC
);
638 Trace("quant-engine") << "Finished quantifiers engine, total time = " << (clSet2
-clSet
);
639 Trace("quant-engine") << ", added lemma = " << d_hasAddedLemma
;
640 Trace("quant-engine") << std::endl
;
643 Trace("quant-engine-debug2") << "Finished quantifiers engine check." << std::endl
;
645 Trace("quant-engine-debug2") << "Quantifiers Engine does not need check." << std::endl
;
649 if( e
==Theory::EFFORT_LAST_CALL
&& !d_hasAddedLemma
){
651 Trace("quant-engine") << "Set incomplete flag." << std::endl
;
652 getOutputChannel().setIncomplete();
655 d_instantiate
->debugPrintModel();
659 void QuantifiersEngine::notifyCombineTheories() {
660 //if allowing theory combination to happen at most once between instantiation rounds
662 //d_ierCounterLastLc = -1;
665 bool QuantifiersEngine::reduceQuantifier( Node q
) {
666 //TODO: this can be unified with preregistration: AlphaEquivalence takes ownership of reducable quants
667 BoolMap::const_iterator it
= d_quants_red
.find( q
);
668 if( it
==d_quants_red
.end() ){
670 std::map
< Node
, Node
>::iterator itr
= d_quants_red_lem
.find( q
);
671 if( itr
==d_quants_red_lem
.end() ){
672 if (d_qmodules
->d_alpha_equiv
)
674 Trace("quant-engine-red") << "Alpha equivalence " << q
<< "?" << std::endl
;
675 //add equivalence with another quantified formula
676 lem
= d_qmodules
->d_alpha_equiv
->reduceQuantifier(q
);
678 Trace("quant-engine-red") << "...alpha equivalence success." << std::endl
;
679 ++(d_statistics
.d_red_alpha_equiv
);
682 d_quants_red_lem
[q
] = lem
;
687 getOutputChannel().lemma( lem
);
689 d_quants_red
[q
] = !lem
.isNull();
690 return !lem
.isNull();
696 void QuantifiersEngine::registerQuantifierInternal(Node f
)
698 std::map
< Node
, bool >::iterator it
= d_quants
.find( f
);
699 if( it
==d_quants
.end() ){
700 Trace("quant") << "QuantifiersEngine : Register quantifier ";
701 Trace("quant") << " : " << f
<< std::endl
;
702 unsigned prev_lemma_waiting
= d_lemmas_waiting
.size();
703 ++(d_statistics
.d_num_quant
);
704 Assert(f
.getKind() == FORALL
);
705 // register with utilities
706 for (unsigned i
= 0; i
< d_util
.size(); i
++)
708 d_util
[i
]->registerQuantifier(f
);
710 // compute attributes
711 d_quant_attr
->computeAttributes(f
);
713 for (QuantifiersModule
*& mdl
: d_modules
)
715 Trace("quant-debug") << "check ownership with " << mdl
->identify()
716 << "..." << std::endl
;
717 mdl
->checkOwnership(f
);
719 QuantifiersModule
* qm
= getOwner(f
);
720 Trace("quant") << " Owner : " << (qm
== nullptr ? "[none]" : qm
->identify())
722 // register with each module
723 for (QuantifiersModule
*& mdl
: d_modules
)
725 Trace("quant-debug") << "register with " << mdl
->identify() << "..."
727 mdl
->registerQuantifier(f
);
728 // since this is context-independent, we should not add any lemmas during
730 Assert(d_lemmas_waiting
.size() == prev_lemma_waiting
);
732 Trace("quant-debug") << "...finish." << std::endl
;
734 AlwaysAssert(d_lemmas_waiting
.size() == prev_lemma_waiting
);
738 void QuantifiersEngine::preRegisterQuantifier(Node q
)
740 NodeSet::const_iterator it
= d_quants_prereg
.find(q
);
741 if (it
!= d_quants_prereg
.end())
745 Trace("quant-debug") << "QuantifiersEngine : Pre-register " << q
<< std::endl
;
746 d_quants_prereg
.insert(q
);
748 if (reduceQuantifier(q
))
750 // if we can reduce it, nothing left to do
753 // ensure that it is registered
754 registerQuantifierInternal(q
);
755 // register with each module
756 for (QuantifiersModule
*& mdl
: d_modules
)
758 Trace("quant-debug") << "pre-register with " << mdl
->identify() << "..."
760 mdl
->preRegisterQuantifier(q
);
764 Trace("quant-debug") << "...finish pre-register " << q
<< "..." << std::endl
;
767 void QuantifiersEngine::assertQuantifier( Node f
, bool pol
){
768 if (reduceQuantifier(f
))
770 // if we can reduce it, nothing left to do
775 TrustNode lem
= d_skolemize
->process(f
);
778 if (Trace
.isOn("quantifiers-sk-debug"))
780 Node slem
= Rewriter::rewrite(lem
.getNode());
781 Trace("quantifiers-sk-debug")
782 << "Skolemize lemma : " << slem
<< std::endl
;
784 getOutputChannel().trustedLemma(
785 lem
, LemmaProperty::PREPROCESS
| LemmaProperty::NEEDS_JUSTIFY
);
789 // ensure the quantified formula is registered
790 registerQuantifierInternal(f
);
791 // assert it to each module
792 d_model
->assertQuantifier(f
);
793 for (QuantifiersModule
*& mdl
: d_modules
)
797 addTermToDatabase(d_term_util
->getInstConstantBody(f
), true);
800 void QuantifiersEngine::addTermToDatabase( Node n
, bool withinQuant
, bool withinInstClosure
){
801 if( options::incrementalSolving() ){
802 if( d_presolve_in
.find( n
)==d_presolve_in
.end() ){
803 d_presolve_in
.insert( n
);
804 d_presolve_cache
.push_back( n
);
805 d_presolve_cache_wq
.push_back( withinQuant
);
806 d_presolve_cache_wic
.push_back( withinInstClosure
);
809 //only wait if we are doing incremental solving
810 if( !d_presolve
|| !options::incrementalSolving() ){
811 std::set
< Node
> added
;
812 d_term_db
->addTerm(n
, added
, withinQuant
, withinInstClosure
);
816 if (d_sygus_tdb
&& options::sygusEvalUnfold())
818 d_sygus_tdb
->getEvalUnfold()->registerEvalTerm(n
);
824 void QuantifiersEngine::eqNotifyNewClass(TNode t
) {
825 addTermToDatabase( t
);
828 bool QuantifiersEngine::addLemma( Node lem
, bool doCache
, bool doRewrite
){
831 lem
= Rewriter::rewrite(lem
);
833 Trace("inst-add-debug") << "Adding lemma : " << lem
<< std::endl
;
834 BoolMap::const_iterator itp
= d_lemmas_produced_c
.find( lem
);
835 if( itp
==d_lemmas_produced_c
.end() || !(*itp
).second
){
836 d_lemmas_produced_c
[ lem
] = true;
837 d_lemmas_waiting
.push_back( lem
);
838 Trace("inst-add-debug") << "Added lemma" << std::endl
;
841 Trace("inst-add-debug") << "Duplicate." << std::endl
;
845 //do not need to rewrite, will be rewritten after sending
846 d_lemmas_waiting
.push_back( lem
);
851 bool QuantifiersEngine::addTrustedLemma(TrustNode tlem
,
855 Node lem
= tlem
.getProven();
856 if (!addLemma(lem
, doCache
, doRewrite
))
860 d_lemmasWaitingPg
[lem
] = tlem
.getGenerator();
864 bool QuantifiersEngine::removeLemma( Node lem
) {
865 std::vector
< Node
>::iterator it
= std::find( d_lemmas_waiting
.begin(), d_lemmas_waiting
.end(), lem
);
866 if( it
!=d_lemmas_waiting
.end() ){
867 d_lemmas_waiting
.erase( it
, it
+ 1 );
868 d_lemmas_produced_c
[ lem
] = false;
875 void QuantifiersEngine::addRequirePhase( Node lit
, bool req
){
876 d_phase_req_waiting
[lit
] = req
;
879 void QuantifiersEngine::markRelevant( Node q
) {
880 d_model
->markRelevant( q
);
883 bool QuantifiersEngine::hasAddedLemma() const
885 return !d_lemmas_waiting
.empty() || d_hasAddedLemma
;
888 bool QuantifiersEngine::inConflict() const { return d_qstate
.isInConflict(); }
890 bool QuantifiersEngine::getInstWhenNeedsCheck( Theory::Effort e
) {
891 Trace("quant-engine-debug2") << "Get inst when needs check, counts=" << d_ierCounter
<< ", " << d_ierCounter_lc
<< std::endl
;
892 //determine if we should perform check, based on instWhenMode
893 bool performCheck
= false;
894 if (options::instWhenMode() == options::InstWhenMode::FULL
)
896 performCheck
= ( e
>= Theory::EFFORT_FULL
);
898 else if (options::instWhenMode() == options::InstWhenMode::FULL_DELAY
)
901 (e
>= Theory::EFFORT_FULL
) && !d_qstate
.getValuation().needCheck();
903 else if (options::instWhenMode() == options::InstWhenMode::FULL_LAST_CALL
)
905 performCheck
= ( ( e
==Theory::EFFORT_FULL
&& d_ierCounter
%d_inst_when_phase
!=0 ) || e
==Theory::EFFORT_LAST_CALL
);
907 else if (options::instWhenMode()
908 == options::InstWhenMode::FULL_DELAY_LAST_CALL
)
911 ((e
== Theory::EFFORT_FULL
&& !d_qstate
.getValuation().needCheck()
912 && d_ierCounter
% d_inst_when_phase
!= 0)
913 || e
== Theory::EFFORT_LAST_CALL
);
915 else if (options::instWhenMode() == options::InstWhenMode::LAST_CALL
)
917 performCheck
= ( e
>= Theory::EFFORT_LAST_CALL
);
926 options::UserPatMode
QuantifiersEngine::getInstUserPatMode()
928 if (options::userPatternsQuant() == options::UserPatMode::INTERLEAVE
)
930 return d_ierCounter
% 2 == 0 ? options::UserPatMode::USE
931 : options::UserPatMode::RESORT
;
935 return options::userPatternsQuant();
939 void QuantifiersEngine::flushLemmas(){
940 OutputChannel
& out
= getOutputChannel();
941 if( !d_lemmas_waiting
.empty() ){
942 //take default output channel if none is provided
943 d_hasAddedLemma
= true;
944 std::map
<Node
, ProofGenerator
*>::iterator itp
;
945 // Note: Do not use foreach loop here and do not cache size() call.
946 // New lemmas can be added while iterating over d_lemmas_waiting.
947 for (size_t i
= 0; i
< d_lemmas_waiting
.size(); ++i
)
949 const Node
& lemw
= d_lemmas_waiting
[i
];
950 Trace("qe-lemma") << "Lemma : " << lemw
<< std::endl
;
951 itp
= d_lemmasWaitingPg
.find(lemw
);
953 LemmaProperty::PREPROCESS
| LemmaProperty::NEEDS_JUSTIFY
;
954 if (itp
!= d_lemmasWaitingPg
.end())
956 TrustNode tlemw
= TrustNode::mkTrustLemma(lemw
, itp
->second
);
957 out
.trustedLemma(tlemw
, p
);
964 d_lemmas_waiting
.clear();
966 if( !d_phase_req_waiting
.empty() ){
967 for( std::map
< Node
, bool >::iterator it
= d_phase_req_waiting
.begin(); it
!= d_phase_req_waiting
.end(); ++it
){
968 Trace("qe-lemma") << "Require phase : " << it
->first
<< " -> " << it
->second
<< std::endl
;
969 out
.requirePhase(it
->first
, it
->second
);
971 d_phase_req_waiting
.clear();
975 void QuantifiersEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
976 d_instantiate
->getInstantiationTermVectors(q
, tvecs
);
979 void QuantifiersEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
980 d_instantiate
->getInstantiationTermVectors(insts
);
983 void QuantifiersEngine::printSynthSolution( std::ostream
& out
) {
984 if (d_qmodules
->d_synth_e
)
986 d_qmodules
->d_synth_e
->printSynthSolution(out
);
988 out
<< "Internal error : module for synth solution not found." << std::endl
;
992 void QuantifiersEngine::getInstantiatedQuantifiedFormulas(std::vector
<Node
>& qs
)
994 d_instantiate
->getInstantiatedQuantifiedFormulas(qs
);
997 void QuantifiersEngine::getSkolemTermVectors(
998 std::map
<Node
, std::vector
<Node
> >& sks
) const
1000 d_skolemize
->getSkolemTermVectors(sks
);
1003 QuantifiersEngine::Statistics::Statistics()
1004 : d_time("theory::QuantifiersEngine::time"),
1005 d_qcf_time("theory::QuantifiersEngine::time_qcf"),
1006 d_ematching_time("theory::QuantifiersEngine::time_ematching"),
1007 d_num_quant("QuantifiersEngine::Num_Quantifiers", 0),
1008 d_instantiation_rounds("QuantifiersEngine::Rounds_Instantiation_Full", 0),
1009 d_instantiation_rounds_lc("QuantifiersEngine::Rounds_Instantiation_Last_Call", 0),
1010 d_triggers("QuantifiersEngine::Triggers", 0),
1011 d_simple_triggers("QuantifiersEngine::Triggers_Simple", 0),
1012 d_multi_triggers("QuantifiersEngine::Triggers_Multi", 0),
1013 d_multi_trigger_instantiations("QuantifiersEngine::Multi_Trigger_Instantiations", 0),
1014 d_red_alpha_equiv("QuantifiersEngine::Reductions_Alpha_Equivalence", 0),
1015 d_instantiations_user_patterns("QuantifiersEngine::Instantiations_User_Patterns", 0),
1016 d_instantiations_auto_gen("QuantifiersEngine::Instantiations_Auto_Gen", 0),
1017 d_instantiations_guess("QuantifiersEngine::Instantiations_Guess", 0),
1018 d_instantiations_qcf("QuantifiersEngine::Instantiations_Qcf_Conflict", 0),
1019 d_instantiations_qcf_prop("QuantifiersEngine::Instantiations_Qcf_Prop", 0),
1020 d_instantiations_fmf_exh("QuantifiersEngine::Instantiations_Fmf_Exh", 0),
1021 d_instantiations_fmf_mbqi("QuantifiersEngine::Instantiations_Fmf_Mbqi", 0),
1022 d_instantiations_cbqi("QuantifiersEngine::Instantiations_Cbqi", 0),
1023 d_instantiations_rr("QuantifiersEngine::Instantiations_Rewrite_Rules", 0)
1025 smtStatisticsRegistry()->registerStat(&d_time
);
1026 smtStatisticsRegistry()->registerStat(&d_qcf_time
);
1027 smtStatisticsRegistry()->registerStat(&d_ematching_time
);
1028 smtStatisticsRegistry()->registerStat(&d_num_quant
);
1029 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds
);
1030 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds_lc
);
1031 smtStatisticsRegistry()->registerStat(&d_triggers
);
1032 smtStatisticsRegistry()->registerStat(&d_simple_triggers
);
1033 smtStatisticsRegistry()->registerStat(&d_multi_triggers
);
1034 smtStatisticsRegistry()->registerStat(&d_multi_trigger_instantiations
);
1035 smtStatisticsRegistry()->registerStat(&d_red_alpha_equiv
);
1036 smtStatisticsRegistry()->registerStat(&d_instantiations_user_patterns
);
1037 smtStatisticsRegistry()->registerStat(&d_instantiations_auto_gen
);
1038 smtStatisticsRegistry()->registerStat(&d_instantiations_guess
);
1039 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf
);
1040 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf_prop
);
1041 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_exh
);
1042 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_mbqi
);
1043 smtStatisticsRegistry()->registerStat(&d_instantiations_cbqi
);
1044 smtStatisticsRegistry()->registerStat(&d_instantiations_rr
);
1047 QuantifiersEngine::Statistics::~Statistics(){
1048 smtStatisticsRegistry()->unregisterStat(&d_time
);
1049 smtStatisticsRegistry()->unregisterStat(&d_qcf_time
);
1050 smtStatisticsRegistry()->unregisterStat(&d_ematching_time
);
1051 smtStatisticsRegistry()->unregisterStat(&d_num_quant
);
1052 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds
);
1053 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds_lc
);
1054 smtStatisticsRegistry()->unregisterStat(&d_triggers
);
1055 smtStatisticsRegistry()->unregisterStat(&d_simple_triggers
);
1056 smtStatisticsRegistry()->unregisterStat(&d_multi_triggers
);
1057 smtStatisticsRegistry()->unregisterStat(&d_multi_trigger_instantiations
);
1058 smtStatisticsRegistry()->unregisterStat(&d_red_alpha_equiv
);
1059 smtStatisticsRegistry()->unregisterStat(&d_instantiations_user_patterns
);
1060 smtStatisticsRegistry()->unregisterStat(&d_instantiations_auto_gen
);
1061 smtStatisticsRegistry()->unregisterStat(&d_instantiations_guess
);
1062 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf
);
1063 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf_prop
);
1064 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_exh
);
1065 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_mbqi
);
1066 smtStatisticsRegistry()->unregisterStat(&d_instantiations_cbqi
);
1067 smtStatisticsRegistry()->unregisterStat(&d_instantiations_rr
);
1070 eq::EqualityEngine
* QuantifiersEngine::getMasterEqualityEngine() const
1072 return d_masterEqualityEngine
;
1075 Node
QuantifiersEngine::getInternalRepresentative( Node a
, Node q
, int index
){
1076 return d_eq_query
->getInternalRepresentative(a
, q
, index
);
1079 Node
QuantifiersEngine::getNameForQuant(Node q
) const
1081 Node name
= d_quant_attr
->getQuantName(q
);
1089 bool QuantifiersEngine::getNameForQuant(Node q
, Node
& name
, bool req
) const
1091 name
= getNameForQuant(q
);
1092 // if we have a name, or we did not require one
1093 return name
!= q
|| !req
;
1096 bool QuantifiersEngine::getSynthSolutions(
1097 std::map
<Node
, std::map
<Node
, Node
> >& sol_map
)
1099 return d_qmodules
->d_synth_e
->getSynthSolutions(sol_map
);
1102 void QuantifiersEngine::debugPrintEqualityEngine( const char * c
) {
1103 eq::EqualityEngine
* ee
= getMasterEqualityEngine();
1104 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( ee
);
1105 std::map
< TypeNode
, int > typ_num
;
1106 while( !eqcs_i
.isFinished() ){
1107 TNode r
= (*eqcs_i
);
1108 TypeNode tr
= r
.getType();
1109 if( typ_num
.find( tr
)==typ_num
.end() ){
1113 bool firstTime
= true;
1114 Trace(c
) << " " << r
;
1115 Trace(c
) << " : { ";
1116 eq::EqClassIterator eqc_i
= eq::EqClassIterator( r
, ee
);
1117 while( !eqc_i
.isFinished() ){
1121 Trace(c
) << std::endl
;
1124 Trace(c
) << " " << n
<< std::endl
;
1128 if( !firstTime
){ Trace(c
) << " "; }
1129 Trace(c
) << "}" << std::endl
;
1132 Trace(c
) << std::endl
;
1133 for( std::map
< TypeNode
, int >::iterator it
= typ_num
.begin(); it
!= typ_num
.end(); ++it
){
1134 Trace(c
) << "# eqc for " << it
->first
<< " : " << it
->second
<< std::endl
;
1138 } // namespace theory