1 /********************* */
2 /*! \file quantifiers_engine.cpp
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Tim King, Morgan Deters
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2017 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_statistics_registry.h"
20 #include "theory/arrays/theory_arrays.h"
21 #include "theory/datatypes/theory_datatypes.h"
22 #include "theory/quantifiers/alpha_equivalence.h"
23 #include "theory/quantifiers/fmf/ambqi_builder.h"
24 #include "theory/quantifiers/anti_skolem.h"
25 #include "theory/quantifiers/fmf/bounded_integers.h"
26 #include "theory/quantifiers/sygus/ce_guided_instantiation.h"
27 #include "theory/quantifiers/cegqi/ceg_t_instantiator.h"
28 #include "theory/quantifiers/conjecture_generator.h"
29 #include "theory/quantifiers/equality_infer.h"
30 #include "theory/quantifiers/equality_query.h"
31 #include "theory/quantifiers/first_order_model.h"
32 #include "theory/quantifiers/fmf/full_model_check.h"
33 #include "theory/quantifiers/fun_def_engine.h"
34 #include "theory/quantifiers/inst_propagator.h"
35 #include "theory/quantifiers/cegqi/inst_strategy_cbqi.h"
36 #include "theory/quantifiers/ematching/inst_strategy_e_matching.h"
37 #include "theory/quantifiers/inst_strategy_enumerative.h"
38 #include "theory/quantifiers/instantiate.h"
39 #include "theory/quantifiers/ematching/instantiation_engine.h"
40 #include "theory/quantifiers/local_theory_ext.h"
41 #include "theory/quantifiers/fmf/model_engine.h"
42 #include "theory/quantifiers/quant_conflict_find.h"
43 #include "theory/quantifiers/quant_epr.h"
44 #include "theory/quantifiers/quant_equality_engine.h"
45 #include "theory/quantifiers/quant_relevance.h"
46 #include "theory/quantifiers/quant_split.h"
47 #include "theory/quantifiers/quantifiers_attributes.h"
48 #include "theory/quantifiers/quantifiers_rewriter.h"
49 #include "theory/quantifiers/relevant_domain.h"
50 #include "theory/quantifiers/rewrite_engine.h"
51 #include "theory/quantifiers/skolemize.h"
52 #include "theory/quantifiers/term_database.h"
53 #include "theory/quantifiers/sygus/term_database_sygus.h"
54 #include "theory/quantifiers/term_enumeration.h"
55 #include "theory/quantifiers/term_util.h"
56 #include "theory/quantifiers/ematching/trigger.h"
57 #include "theory/sep/theory_sep.h"
58 #include "theory/theory_engine.h"
59 #include "theory/uf/equality_engine.h"
60 #include "theory/uf/theory_uf.h"
61 #include "theory/uf/theory_uf_strong_solver.h"
65 using namespace CVC4::kind
;
66 using namespace CVC4::context
;
67 using namespace CVC4::theory
;
68 using namespace CVC4::theory::inst
;
70 QuantifiersEngine::QuantifiersEngine(context::Context
* c
,
71 context::UserContext
* u
,
74 d_quant_attr(new quantifiers::QuantAttributes(this)),
75 d_instantiate(new quantifiers::Instantiate(this, u
)),
76 d_skolemize(new quantifiers::Skolemize(this, u
)),
77 d_term_enum(new quantifiers::TermEnumeration
),
78 d_conflict_c(c
, false),
81 d_lemmas_produced_c(u
),
84 // d_ierCounter_lc(c),
85 // d_ierCounterLastLc(c),
89 d_presolve_cache_wq(u
),
90 d_presolve_cache_wic(u
)
93 d_eq_query
= new quantifiers::EqualityQueryQuantifiersEngine( c
, this );
94 d_util
.push_back( d_eq_query
);
96 // term util must come first
97 d_term_util
= new quantifiers::TermUtil(this);
98 d_util
.push_back(d_term_util
);
100 d_term_db
= new quantifiers::TermDb( c
, u
, this );
101 d_util
.push_back( d_term_db
);
103 if (options::ceGuidedInst()) {
104 d_sygus_tdb
= new quantifiers::TermDbSygus(c
, this);
109 if( options::instPropagate() ){
110 // notice that this option is incompatible with options::qcfAllConflict()
111 d_inst_prop
= new quantifiers::InstPropagator( this );
112 d_util
.push_back( d_inst_prop
);
113 d_instantiate
->addNotify(d_inst_prop
->getInstantiationNotify());
118 if( options::inferArithTriggerEq() ){
119 d_eq_inference
= new quantifiers::EqualityInference( c
, options::inferArithTriggerEqExp() );
121 d_eq_inference
= NULL
;
124 d_util
.push_back(d_instantiate
.get());
126 d_tr_trie
= new inst::TriggerTrie
;
127 d_curr_effort_level
= QuantifiersModule::QEFFORT_NONE
;
129 d_hasAddedLemma
= false;
130 d_useModelEe
= false;
131 //don't add true lemma
132 d_lemmas_produced_c
[d_term_util
->d_true
] = true;
134 Trace("quant-engine-debug") << "Initialize quantifiers engine." << std::endl
;
135 Trace("quant-engine-debug") << "Initialize model, mbqi : " << options::mbqiMode() << std::endl
;
137 if( options::relevantTriggers() ){
138 d_quant_rel
= new quantifiers::QuantRelevance(false);
139 d_util
.push_back(d_quant_rel
);
144 if( options::quantEpr() ){
145 Assert( !options::incrementalSolving() );
146 d_qepr
= new quantifiers::QuantEPR
;
154 d_inst_engine
= NULL
;
157 d_anti_skolem
= NULL
;
159 d_model_engine
= NULL
;
163 d_lte_part_inst
= NULL
;
164 d_alpha_equiv
= NULL
;
165 d_fun_def_engine
= NULL
;
172 //allow theory combination to go first, once initially
173 d_ierCounter
= options::instWhenTcFirst() ? 0 : 1;
174 d_ierCounter_c
= d_ierCounter
;
176 d_ierCounterLastLc
= 0;
177 d_inst_when_phase
= 1 + ( options::instWhenPhase()<1 ? 1 : options::instWhenPhase() );
180 QuantifiersEngine::~QuantifiersEngine(){
181 delete d_alpha_equiv
;
186 delete d_model_engine
;
187 delete d_inst_engine
;
197 delete d_eq_inference
;
201 delete d_lte_part_inst
;
202 delete d_fun_def_engine
;
207 delete d_anti_skolem
;
211 EqualityQuery
* QuantifiersEngine::getEqualityQuery() {
215 context::Context
* QuantifiersEngine::getSatContext(){
216 return d_te
->theoryOf( THEORY_QUANTIFIERS
)->getSatContext();
219 context::UserContext
* QuantifiersEngine::getUserContext(){
220 return d_te
->theoryOf( THEORY_QUANTIFIERS
)->getUserContext();
223 OutputChannel
& QuantifiersEngine::getOutputChannel(){
224 return d_te
->theoryOf( THEORY_QUANTIFIERS
)->getOutputChannel();
226 /** get default valuation for the quantifiers engine */
227 Valuation
& QuantifiersEngine::getValuation(){
228 return d_te
->theoryOf( THEORY_QUANTIFIERS
)->getValuation();
231 void QuantifiersEngine::finishInit(){
232 context::Context
* c
= getSatContext();
233 Trace("quant-engine-debug") << "QuantifiersEngine : finishInit " << std::endl
;
234 bool needsBuilder
= false;
235 bool needsRelDom
= false;
236 //add quantifiers modules
237 if( options::quantConflictFind() || options::quantRewriteRules() ){
238 d_qcf
= new quantifiers::QuantConflictFind( this, c
);
239 d_modules
.push_back( d_qcf
);
241 if( options::conjectureGen() ){
242 d_sg_gen
= new quantifiers::ConjectureGenerator( this, c
);
243 d_modules
.push_back( d_sg_gen
);
245 if( !options::finiteModelFind() || options::fmfInstEngine() ){
246 d_inst_engine
= new quantifiers::InstantiationEngine( this );
247 d_modules
.push_back( d_inst_engine
);
249 if( options::cbqi() ){
250 d_i_cbqi
= new quantifiers::InstStrategyCegqi( this );
251 d_modules
.push_back( d_i_cbqi
);
252 if( options::cbqiBv() ){
253 // if doing instantiation for BV, need the inverter class
254 d_bv_invert
= new quantifiers::BvInverter
;
257 if( options::ceGuidedInst() ){
258 d_ceg_inst
= new quantifiers::CegInstantiation( this, c
);
259 d_modules
.push_back( d_ceg_inst
);
260 //needsBuilder = true;
262 //finite model finding
263 if( options::finiteModelFind() ){
264 if( options::fmfBound() ){
265 d_bint
= new quantifiers::BoundedIntegers( c
, this );
266 d_modules
.push_back( d_bint
);
268 d_model_engine
= new quantifiers::ModelEngine( c
, this );
269 d_modules
.push_back( d_model_engine
);
270 //finite model finder has special ways of building the model
273 if( options::quantRewriteRules() ){
274 d_rr_engine
= new quantifiers::RewriteEngine( c
, this );
275 d_modules
.push_back(d_rr_engine
);
277 if( options::ltePartialInst() ){
278 d_lte_part_inst
= new quantifiers::LtePartialInst( this, c
);
279 d_modules
.push_back( d_lte_part_inst
);
281 if( options::quantDynamicSplit()!=quantifiers::QUANT_DSPLIT_MODE_NONE
){
282 d_qsplit
= new quantifiers::QuantDSplit( this, c
);
283 d_modules
.push_back( d_qsplit
);
285 if( options::quantAntiSkolem() ){
286 d_anti_skolem
= new quantifiers::QuantAntiSkolem( this );
287 d_modules
.push_back( d_anti_skolem
);
289 if( options::quantAlphaEquiv() ){
290 d_alpha_equiv
= new quantifiers::AlphaEquivalence( this );
292 //if( options::funDefs() ){
293 // d_fun_def_engine = new quantifiers::FunDefEngine( this, c );
294 // d_modules.push_back( d_fun_def_engine );
296 if( options::quantEqualityEngine() ){
297 d_uee
= new quantifiers::QuantEqualityEngine( this, c
);
298 d_modules
.push_back( d_uee
);
300 //full saturation : instantiate from relevant domain, then arbitrary terms
301 if( options::fullSaturateQuant() || options::fullSaturateInterleave() ){
302 d_fs
= new quantifiers::InstStrategyEnum(this);
303 d_modules
.push_back( d_fs
);
308 d_rel_dom
= new quantifiers::RelevantDomain( this );
309 d_util
.push_back( d_rel_dom
);
312 // if we require specialized ways of building the model
314 Trace("quant-engine-debug") << "Initialize model engine, mbqi : " << options::mbqiMode() << " " << options::fmfBound() << std::endl
;
315 if( options::mbqiMode()==quantifiers::MBQI_FMC
|| options::mbqiMode()==quantifiers::MBQI_FMC_INTERVAL
||
316 options::mbqiMode()==quantifiers::MBQI_TRUST
|| options::fmfBound() ){
317 Trace("quant-engine-debug") << "...make fmc builder." << std::endl
;
318 d_model
= new quantifiers::fmcheck::FirstOrderModelFmc( this, c
, "FirstOrderModelFmc" );
319 d_builder
= new quantifiers::fmcheck::FullModelChecker( c
, this );
320 }else if( options::mbqiMode()==quantifiers::MBQI_ABS
){
321 Trace("quant-engine-debug") << "...make abs mbqi builder." << std::endl
;
322 d_model
= new quantifiers::FirstOrderModelAbs( this, c
, "FirstOrderModelAbs" );
323 d_builder
= new quantifiers::AbsMbqiBuilder( c
, this );
325 Trace("quant-engine-debug") << "...make default model builder." << std::endl
;
326 d_model
= new quantifiers::FirstOrderModelIG( this, c
, "FirstOrderModelIG" );
327 d_builder
= new quantifiers::QModelBuilderDefault( c
, this );
330 d_model
= new quantifiers::FirstOrderModelIG( this, c
, "FirstOrderModelIG" );
334 QuantifiersModule
* QuantifiersEngine::getOwner( Node q
) {
335 std::map
< Node
, QuantifiersModule
* >::iterator it
= d_owner
.find( q
);
336 if( it
==d_owner
.end() ){
343 void QuantifiersEngine::setOwner( Node q
, QuantifiersModule
* m
, int priority
) {
344 QuantifiersModule
* mo
= getOwner( q
);
347 if( priority
<=d_owner_priority
[q
] ){
348 Trace("quant-warn") << "WARNING: setting owner of " << q
<< " to " << ( m
? m
->identify() : "null" ) << ", but already has owner " << mo
->identify() << " with higher priority!" << std::endl
;
353 d_owner_priority
[q
] = priority
;
357 bool QuantifiersEngine::hasOwnership( Node q
, QuantifiersModule
* m
) {
358 QuantifiersModule
* mo
= getOwner( q
);
359 return mo
==m
|| mo
==NULL
;
362 bool QuantifiersEngine::isFiniteBound( Node q
, Node v
) {
363 if( getBoundedIntegers() && getBoundedIntegers()->isBoundVar( q
, v
) ){
366 TypeNode tn
= v
.getType();
367 if( tn
.isSort() && options::finiteModelFind() ){
370 else if (d_term_enum
->mayComplete(tn
))
378 void QuantifiersEngine::presolve() {
379 Trace("quant-engine-proc") << "QuantifiersEngine : presolve " << std::endl
;
380 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
381 d_modules
[i
]->presolve();
383 d_term_db
->presolve();
385 //add all terms to database
386 if( options::incrementalSolving() ){
387 Trace("quant-engine-proc") << "Add presolve cache " << d_presolve_cache
.size() << std::endl
;
388 for( unsigned i
=0; i
<d_presolve_cache
.size(); i
++ ){
389 addTermToDatabase( d_presolve_cache
[i
], d_presolve_cache_wq
[i
], d_presolve_cache_wic
[i
] );
391 Trace("quant-engine-proc") << "Done add presolve cache " << std::endl
;
395 void QuantifiersEngine::ppNotifyAssertions(
396 const std::vector
<Node
>& assertions
) {
397 Trace("quant-engine-proc")
398 << "ppNotifyAssertions in QE, #assertions = " << assertions
.size()
399 << " check epr = " << (d_qepr
!= NULL
) << std::endl
;
400 if ((options::instLevelInputOnly() && options::instMaxLevel() != -1) ||
402 for (unsigned i
= 0; i
< assertions
.size(); i
++) {
403 if (options::instLevelInputOnly() && options::instMaxLevel() != -1) {
404 quantifiers::QuantAttributes::setInstantiationLevelAttr(assertions
[i
],
407 if (d_qepr
!= NULL
) {
408 d_qepr
->registerAssertion(assertions
[i
]);
411 if (d_qepr
!= NULL
) {
412 // must handle sources of other new constants e.g. separation logic
414 sep::TheorySep
* theory_sep
=
415 static_cast<sep::TheorySep
*>(getTheoryEngine()->theoryOf(THEORY_SEP
));
416 theory_sep
->initializeBounds();
417 d_qepr
->finishInit();
422 void QuantifiersEngine::check( Theory::Effort e
){
423 CodeTimer
codeTimer(d_statistics
.d_time
);
424 d_useModelEe
= options::quantModelEe() && ( e
>=Theory::EFFORT_LAST_CALL
);
425 // if we want to use the model's equality engine, build the model now
426 if( d_useModelEe
&& !d_model
->isBuilt() ){
427 Trace("quant-engine-debug") << "Build the model." << std::endl
;
428 if( !d_te
->getModelBuilder()->buildModel( d_model
) ){
429 //we are done if model building was unsuccessful
431 if( d_hasAddedLemma
){
432 Trace("quant-engine-debug") << "...failed." << std::endl
;
438 if( !getActiveEqualityEngine()->consistent() ){
439 Trace("quant-engine-debug") << "Master equality engine not consistent, return." << std::endl
;
442 bool needsCheck
= !d_lemmas_waiting
.empty();
443 QuantifiersModule::QEffort needsModelE
= QuantifiersModule::QEFFORT_NONE
;
444 std::vector
< QuantifiersModule
* > qm
;
445 if( d_model
->checkNeeded() ){
446 needsCheck
= needsCheck
|| e
>=Theory::EFFORT_LAST_CALL
; //always need to check at or above last call
447 for (QuantifiersModule
*& mdl
: d_modules
)
449 if (mdl
->needsCheck(e
))
453 //can only request model at last call since theory combination can find inconsistencies
454 if( e
>=Theory::EFFORT_LAST_CALL
){
455 QuantifiersModule::QEffort me
= mdl
->needsModel(e
);
456 needsModelE
= me
<needsModelE
? me
: needsModelE
;
463 d_hasAddedLemma
= false;
464 bool setIncomplete
= false;
466 Trace("quant-engine-debug2") << "Quantifiers Engine call to check, level = " << e
<< ", needsCheck=" << needsCheck
<< std::endl
;
468 //this will fail if a set of instances is marked as a conflict, but is not
469 Assert( !d_conflict_c
.get() );
470 //flush previous lemmas (for instance, if was interupted), or other lemmas to process
472 if( d_hasAddedLemma
){
477 if( Trace
.isOn("quant-engine") ){
478 clSet
= double(clock())/double(CLOCKS_PER_SEC
);
479 Trace("quant-engine") << ">>>>> Quantifiers Engine Round, effort = " << e
<< " <<<<<" << std::endl
;
482 if( Trace
.isOn("quant-engine-debug") ){
483 Trace("quant-engine-debug") << "Quantifiers Engine check, level = " << e
<< std::endl
;
484 Trace("quant-engine-debug") << " depth : " << d_ierCounter_c
<< std::endl
;
485 Trace("quant-engine-debug") << " modules to check : ";
486 for( unsigned i
=0; i
<qm
.size(); i
++ ){
487 Trace("quant-engine-debug") << qm
[i
]->identify() << " ";
489 Trace("quant-engine-debug") << std::endl
;
490 Trace("quant-engine-debug") << " # quantified formulas = " << d_model
->getNumAssertedQuantifiers() << std::endl
;
491 if( !d_lemmas_waiting
.empty() ){
492 Trace("quant-engine-debug") << " lemmas waiting = " << d_lemmas_waiting
.size() << std::endl
;
494 Trace("quant-engine-debug") << " Theory engine finished : " << !d_te
->needCheck() << std::endl
;
495 Trace("quant-engine-debug") << " Needs model effort : " << needsModelE
<< std::endl
;
497 if( Trace
.isOn("quant-engine-ee-pre") ){
498 Trace("quant-engine-ee-pre") << "Equality engine (pre-inference): " << std::endl
;
499 debugPrintEqualityEngine( "quant-engine-ee-pre" );
501 if( Trace
.isOn("quant-engine-assert") ){
502 Trace("quant-engine-assert") << "Assertions : " << std::endl
;
503 getTheoryEngine()->printAssertions("quant-engine-assert");
507 Trace("quant-engine-debug") << "Resetting all utilities..." << std::endl
;
508 for (QuantifiersUtil
*& util
: d_util
)
510 Trace("quant-engine-debug2") << "Reset " << util
->identify().c_str()
511 << "..." << std::endl
;
515 if( d_hasAddedLemma
){
518 //should only fail reset if added a lemma
524 if( Trace
.isOn("quant-engine-ee") ){
525 Trace("quant-engine-ee") << "Equality engine : " << std::endl
;
526 debugPrintEqualityEngine( "quant-engine-ee" );
530 Trace("quant-engine-debug") << "Reset model..." << std::endl
;
531 d_model
->reset_round();
534 Trace("quant-engine-debug") << "Resetting all modules..." << std::endl
;
535 for (QuantifiersModule
*& mdl
: d_modules
)
537 Trace("quant-engine-debug2") << "Reset " << mdl
->identify().c_str()
541 Trace("quant-engine-debug") << "Done resetting all modules." << std::endl
;
542 //reset may have added lemmas
544 if( d_hasAddedLemma
){
548 if( e
==Theory::EFFORT_LAST_CALL
){
549 //if effort is last call, try to minimize model first
550 //uf::StrongSolverTheoryUF * ufss = ((uf::TheoryUF*)getTheoryEngine()->theoryOf( THEORY_UF ))->getStrongSolver();
551 //if( ufss && !ufss->minimize() ){ return; }
552 ++(d_statistics
.d_instantiation_rounds_lc
);
553 }else if( e
==Theory::EFFORT_FULL
){
554 ++(d_statistics
.d_instantiation_rounds
);
556 Trace("quant-engine-debug") << "Check modules that needed check..." << std::endl
;
557 for (unsigned qef
= QuantifiersModule::QEFFORT_CONFLICT
;
558 qef
<= QuantifiersModule::QEFFORT_LAST_CALL
;
561 QuantifiersModule::QEffort quant_e
=
562 static_cast<QuantifiersModule::QEffort
>(qef
);
563 d_curr_effort_level
= quant_e
;
564 //build the model if any module requested it
565 if (needsModelE
== quant_e
)
567 if (!d_model
->isBuilt())
569 // theory engine's model builder is quantifier engine's builder if it
571 Assert(!d_builder
|| d_builder
== d_te
->getModelBuilder());
572 Trace("quant-engine-debug") << "Build model..." << std::endl
;
573 if (!d_te
->getModelBuilder()->buildModel(d_model
))
578 if (!d_model
->isBuiltSuccess())
583 if( !d_hasAddedLemma
){
585 for (QuantifiersModule
*& mdl
: qm
)
587 Trace("quant-engine-debug") << "Check " << mdl
->identify().c_str()
588 << " at effort " << quant_e
<< "..."
590 mdl
->check(e
, quant_e
);
592 Trace("quant-engine-debug") << "...conflict!" << std::endl
;
596 //flush all current lemmas
599 //if we have added one, stop
600 if( d_hasAddedLemma
){
603 Assert( !d_conflict
);
604 if (quant_e
== QuantifiersModule::QEFFORT_CONFLICT
)
606 if( e
==Theory::EFFORT_FULL
){
607 //increment if a last call happened, we are not strictly enforcing interleaving, or already were in phase
608 if( d_ierCounterLastLc
!=d_ierCounter_lc
|| !options::instWhenStrictInterleave() || d_ierCounter
%d_inst_when_phase
!=0 ){
609 d_ierCounter
= d_ierCounter
+ 1;
610 d_ierCounterLastLc
= d_ierCounter_lc
;
611 d_ierCounter_c
= d_ierCounter_c
.get() + 1;
613 }else if( e
==Theory::EFFORT_LAST_CALL
){
614 d_ierCounter_lc
= d_ierCounter_lc
+ 1;
617 else if (quant_e
== QuantifiersModule::QEFFORT_MODEL
)
619 if( e
==Theory::EFFORT_LAST_CALL
){
620 //sources of incompleteness
621 for (QuantifiersUtil
*& util
: d_util
)
623 if (!util
->checkComplete())
625 Trace("quant-engine-debug") << "Set incomplete because utility "
626 << util
->identify().c_str()
627 << " was incomplete." << std::endl
;
628 setIncomplete
= true;
631 //if we have a chance not to set incomplete
632 if( !setIncomplete
){
633 //check if we should set the incomplete flag
634 for (QuantifiersModule
*& mdl
: d_modules
)
636 if (!mdl
->checkComplete())
638 Trace("quant-engine-debug")
639 << "Set incomplete because module "
640 << mdl
->identify().c_str() << " was incomplete."
642 setIncomplete
= true;
646 if( !setIncomplete
){
647 //look at individual quantified formulas, one module must claim completeness for each one
648 for( unsigned i
=0; i
<d_model
->getNumAssertedQuantifiers(); i
++ ){
649 bool hasCompleteM
= false;
650 Node q
= d_model
->getAssertedQuantifier( i
);
651 QuantifiersModule
* qmd
= getOwner( q
);
653 hasCompleteM
= qmd
->checkCompleteFor( q
);
655 for( unsigned j
=0; j
<d_modules
.size(); j
++ ){
656 if( d_modules
[j
]->checkCompleteFor( q
) ){
664 Trace("quant-engine-debug") << "Set incomplete because " << q
<< " was not fully processed." << std::endl
;
665 setIncomplete
= true;
669 Trace("quant-engine-debug2") << "Complete for " << q
<< " due to " << qmd
->identify().c_str() << std::endl
;
674 //if setIncomplete = false, we will answer SAT, otherwise we will run at quant_e QEFFORT_LAST_CALL
675 if( !setIncomplete
){
682 d_curr_effort_level
= QuantifiersModule::QEFFORT_NONE
;
683 Trace("quant-engine-debug") << "Done check modules that needed check." << std::endl
;
684 if( d_hasAddedLemma
){
685 d_instantiate
->debugPrint();
687 if( Trace
.isOn("quant-engine") ){
688 double clSet2
= double(clock())/double(CLOCKS_PER_SEC
);
689 Trace("quant-engine") << "Finished quantifiers engine, total time = " << (clSet2
-clSet
);
690 Trace("quant-engine") << ", added lemma = " << d_hasAddedLemma
;
691 Trace("quant-engine") << std::endl
;
694 Trace("quant-engine-debug2") << "Finished quantifiers engine check." << std::endl
;
696 Trace("quant-engine-debug2") << "Quantifiers Engine does not need check." << std::endl
;
700 if( e
==Theory::EFFORT_LAST_CALL
&& !d_hasAddedLemma
){
702 Trace("quant-engine") << "Set incomplete flag." << std::endl
;
703 getOutputChannel().setIncomplete();
706 d_instantiate
->debugPrintModel();
710 void QuantifiersEngine::notifyCombineTheories() {
711 //if allowing theory combination to happen at most once between instantiation rounds
713 //d_ierCounterLastLc = -1;
716 bool QuantifiersEngine::reduceQuantifier( Node q
) {
717 //TODO: this can be unified with preregistration: AlphaEquivalence takes ownership of reducable quants
718 BoolMap::const_iterator it
= d_quants_red
.find( q
);
719 if( it
==d_quants_red
.end() ){
721 std::map
< Node
, Node
>::iterator itr
= d_quants_red_lem
.find( q
);
722 if( itr
==d_quants_red_lem
.end() ){
724 Trace("quant-engine-red") << "Alpha equivalence " << q
<< "?" << std::endl
;
725 //add equivalence with another quantified formula
726 lem
= d_alpha_equiv
->reduceQuantifier( q
);
728 Trace("quant-engine-red") << "...alpha equivalence success." << std::endl
;
729 ++(d_statistics
.d_red_alpha_equiv
);
732 d_quants_red_lem
[q
] = lem
;
737 getOutputChannel().lemma( lem
);
739 d_quants_red
[q
] = !lem
.isNull();
740 return !lem
.isNull();
746 bool QuantifiersEngine::registerQuantifier( Node f
){
747 std::map
< Node
, bool >::iterator it
= d_quants
.find( f
);
748 if( it
==d_quants
.end() ){
749 Trace("quant") << "QuantifiersEngine : Register quantifier ";
750 Trace("quant") << " : " << f
<< std::endl
;
751 ++(d_statistics
.d_num_quant
);
752 Assert( f
.getKind()==FORALL
);
754 //check whether we should apply a reduction
755 if( reduceQuantifier( f
) ){
756 Trace("quant") << "...reduced." << std::endl
;
760 // register with utilities
761 for (unsigned i
= 0; i
< d_util
.size(); i
++)
763 d_util
[i
]->registerQuantifier(f
);
765 // compute attributes
766 d_quant_attr
->computeAttributes(f
);
768 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
769 Trace("quant-debug") << "pre-register with " << d_modules
[i
]->identify() << "..." << std::endl
;
770 d_modules
[i
]->preRegisterQuantifier( f
);
772 QuantifiersModule
* qm
= getOwner( f
);
774 Trace("quant") << " Owner : " << qm
->identify() << std::endl
;
776 //register with each module
777 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
778 Trace("quant-debug") << "register with " << d_modules
[i
]->identify() << "..." << std::endl
;
779 d_modules
[i
]->registerQuantifier( f
);
782 Node ceBody
= d_term_util
->getInstConstantBody( f
);
783 Trace("quant-debug") << "...finish." << std::endl
;
794 void QuantifiersEngine::registerPattern( std::vector
<Node
> & pattern
) {
795 for(std::vector
<Node
>::iterator p
= pattern
.begin(); p
!= pattern
.end(); ++p
){
796 std::set
< Node
> added
;
797 getTermDatabase()->addTerm( *p
, added
);
801 void QuantifiersEngine::assertQuantifier( Node f
, bool pol
){
804 if( !reduceQuantifier( f
) ){
806 Node lem
= d_skolemize
->process(f
);
809 if( Trace
.isOn("quantifiers-sk-debug") ){
810 Node slem
= Rewriter::rewrite( lem
);
811 Trace("quantifiers-sk-debug") << "Skolemize lemma : " << slem
<< std::endl
;
813 getOutputChannel().lemma( lem
, false, true );
817 //assert to modules TODO : also for !pol?
818 //register the quantifier, assert it to each module
819 if( registerQuantifier( f
) ){
820 d_model
->assertQuantifier( f
);
821 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
822 d_modules
[i
]->assertNode( f
);
824 addTermToDatabase( d_term_util
->getInstConstantBody( f
), true );
829 void QuantifiersEngine::propagate( Theory::Effort level
){
830 CodeTimer
codeTimer(d_statistics
.d_time
);
834 Node
QuantifiersEngine::getNextDecisionRequest( unsigned& priority
){
835 unsigned min_priority
= 0;
837 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
838 Node n
= d_modules
[i
]->getNextDecisionRequest( priority
);
839 if( !n
.isNull() && ( dec
.isNull() || priority
<min_priority
) ){
841 min_priority
= priority
;
847 void QuantifiersEngine::addTermToDatabase( Node n
, bool withinQuant
, bool withinInstClosure
){
848 if( options::incrementalSolving() ){
849 if( d_presolve_in
.find( n
)==d_presolve_in
.end() ){
850 d_presolve_in
.insert( n
);
851 d_presolve_cache
.push_back( n
);
852 d_presolve_cache_wq
.push_back( withinQuant
);
853 d_presolve_cache_wic
.push_back( withinInstClosure
);
856 //only wait if we are doing incremental solving
857 if( !d_presolve
|| !options::incrementalSolving() ){
858 std::set
< Node
> added
;
859 d_term_db
->addTerm(n
, added
, withinQuant
, withinInstClosure
);
865 d_sygus_tdb
->registerEvalTerm(n
);
868 // added contains also the Node that just have been asserted in this
872 for (std::set
<Node
>::iterator i
= added
.begin(), end
= added
.end();
876 d_quant_rel
->setRelevance( i
->getOperator(), 0 );
883 void QuantifiersEngine::eqNotifyNewClass(TNode t
) {
884 addTermToDatabase( t
);
885 if( d_eq_inference
){
886 d_eq_inference
->eqNotifyNewClass( t
);
890 void QuantifiersEngine::eqNotifyPreMerge(TNode t1
, TNode t2
) {
891 if( d_eq_inference
){
892 d_eq_inference
->eqNotifyMerge( t1
, t2
);
896 void QuantifiersEngine::eqNotifyPostMerge(TNode t1
, TNode t2
) {
900 void QuantifiersEngine::eqNotifyDisequal(TNode t1
, TNode t2
, TNode reason
) {
902 // d_qcf->assertDisequal( t1, t2 );
906 bool QuantifiersEngine::addLemma( Node lem
, bool doCache
, bool doRewrite
){
909 lem
= Rewriter::rewrite(lem
);
911 Trace("inst-add-debug") << "Adding lemma : " << lem
<< std::endl
;
912 BoolMap::const_iterator itp
= d_lemmas_produced_c
.find( lem
);
913 if( itp
==d_lemmas_produced_c
.end() || !(*itp
).second
){
914 //d_curr_out->lemma( lem, false, true );
915 d_lemmas_produced_c
[ lem
] = true;
916 d_lemmas_waiting
.push_back( lem
);
917 Trace("inst-add-debug") << "Added lemma" << std::endl
;
920 Trace("inst-add-debug") << "Duplicate." << std::endl
;
924 //do not need to rewrite, will be rewritten after sending
925 d_lemmas_waiting
.push_back( lem
);
930 bool QuantifiersEngine::removeLemma( Node lem
) {
931 std::vector
< Node
>::iterator it
= std::find( d_lemmas_waiting
.begin(), d_lemmas_waiting
.end(), lem
);
932 if( it
!=d_lemmas_waiting
.end() ){
933 d_lemmas_waiting
.erase( it
, it
+ 1 );
934 d_lemmas_produced_c
[ lem
] = false;
941 void QuantifiersEngine::addRequirePhase( Node lit
, bool req
){
942 d_phase_req_waiting
[lit
] = req
;
945 bool QuantifiersEngine::addSplit( Node n
, bool reqPhase
, bool reqPhasePol
){
946 n
= Rewriter::rewrite( n
);
947 Node lem
= NodeManager::currentNM()->mkNode( OR
, n
, n
.notNode() );
948 if( addLemma( lem
) ){
949 Debug("inst") << "*** Add split " << n
<< std::endl
;
955 bool QuantifiersEngine::addSplitEquality( Node n1
, Node n2
, bool reqPhase
, bool reqPhasePol
){
956 //Assert( !areEqual( n1, n2 ) );
957 //Assert( !areDisequal( n1, n2 ) );
958 Node fm
= NodeManager::currentNM()->mkNode( EQUAL
, n1
, n2
);
959 return addSplit( fm
);
962 bool QuantifiersEngine::addEPRAxiom( TypeNode tn
) {
964 Assert( !options::incrementalSolving() );
965 if( d_qepr
->isEPR( tn
) && !d_qepr
->hasEPRAxiom( tn
) ){
966 Node lem
= d_qepr
->mkEPRAxiom( tn
);
967 Trace("quant-epr") << "EPR lemma for " << tn
<< " : " << lem
<< std::endl
;
968 getOutputChannel().lemma( lem
);
974 void QuantifiersEngine::markRelevant( Node q
) {
975 d_model
->markRelevant( q
);
978 void QuantifiersEngine::setConflict() {
983 bool QuantifiersEngine::getInstWhenNeedsCheck( Theory::Effort e
) {
984 Trace("quant-engine-debug2") << "Get inst when needs check, counts=" << d_ierCounter
<< ", " << d_ierCounter_lc
<< std::endl
;
985 //determine if we should perform check, based on instWhenMode
986 bool performCheck
= false;
987 if( options::instWhenMode()==quantifiers::INST_WHEN_FULL
){
988 performCheck
= ( e
>= Theory::EFFORT_FULL
);
989 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_DELAY
){
990 performCheck
= ( e
>= Theory::EFFORT_FULL
) && !getTheoryEngine()->needCheck();
991 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_LAST_CALL
){
992 performCheck
= ( ( e
==Theory::EFFORT_FULL
&& d_ierCounter
%d_inst_when_phase
!=0 ) || e
==Theory::EFFORT_LAST_CALL
);
993 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_DELAY_LAST_CALL
){
994 performCheck
= ( ( e
==Theory::EFFORT_FULL
&& !getTheoryEngine()->needCheck() && d_ierCounter
%d_inst_when_phase
!=0 ) || e
==Theory::EFFORT_LAST_CALL
);
995 }else if( options::instWhenMode()==quantifiers::INST_WHEN_LAST_CALL
){
996 performCheck
= ( e
>= Theory::EFFORT_LAST_CALL
);
1000 if( e
==Theory::EFFORT_LAST_CALL
){
1001 //with bounded integers, skip every other last call,
1002 // since matching loops may occur with infinite quantification
1003 if( d_ierCounter_lc
%2==0 && options::fmfBound() ){
1004 performCheck
= false;
1007 return performCheck
;
1010 quantifiers::UserPatMode
QuantifiersEngine::getInstUserPatMode() {
1011 if( options::userPatternsQuant()==quantifiers::USER_PAT_MODE_INTERLEAVE
){
1012 return d_ierCounter
%2==0 ? quantifiers::USER_PAT_MODE_USE
: quantifiers::USER_PAT_MODE_RESORT
;
1014 return options::userPatternsQuant();
1018 void QuantifiersEngine::flushLemmas(){
1019 if( !d_lemmas_waiting
.empty() ){
1020 //filter based on notify classes
1021 if (d_instantiate
->hasNotify())
1023 unsigned prev_lem_sz
= d_lemmas_waiting
.size();
1024 d_instantiate
->notifyFlushLemmas();
1025 if( prev_lem_sz
!=d_lemmas_waiting
.size() ){
1026 Trace("quant-engine") << "...filtered instances : " << d_lemmas_waiting
.size() << " / " << prev_lem_sz
<< std::endl
;
1029 //take default output channel if none is provided
1030 d_hasAddedLemma
= true;
1031 for( unsigned i
=0; i
<d_lemmas_waiting
.size(); i
++ ){
1032 Trace("qe-lemma") << "Lemma : " << d_lemmas_waiting
[i
] << std::endl
;
1033 getOutputChannel().lemma( d_lemmas_waiting
[i
], false, true );
1035 d_lemmas_waiting
.clear();
1037 if( !d_phase_req_waiting
.empty() ){
1038 for( std::map
< Node
, bool >::iterator it
= d_phase_req_waiting
.begin(); it
!= d_phase_req_waiting
.end(); ++it
){
1039 Trace("qe-lemma") << "Require phase : " << it
->first
<< " -> " << it
->second
<< std::endl
;
1040 getOutputChannel().requirePhase( it
->first
, it
->second
);
1042 d_phase_req_waiting
.clear();
1046 bool QuantifiersEngine::getUnsatCoreLemmas( std::vector
< Node
>& active_lemmas
) {
1047 return d_instantiate
->getUnsatCoreLemmas(active_lemmas
);
1050 bool QuantifiersEngine::getUnsatCoreLemmas( std::vector
< Node
>& active_lemmas
, std::map
< Node
, Node
>& weak_imp
) {
1051 return d_instantiate
->getUnsatCoreLemmas(active_lemmas
, weak_imp
);
1054 void QuantifiersEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
1055 d_instantiate
->getInstantiationTermVectors(q
, tvecs
);
1058 void QuantifiersEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
1059 d_instantiate
->getInstantiationTermVectors(insts
);
1062 void QuantifiersEngine::getExplanationForInstLemmas(
1063 const std::vector
<Node
>& lems
,
1064 std::map
<Node
, Node
>& quant
,
1065 std::map
<Node
, std::vector
<Node
> >& tvec
)
1067 d_instantiate
->getExplanationForInstLemmas(lems
, quant
, tvec
);
1070 void QuantifiersEngine::printInstantiations( std::ostream
& out
) {
1071 bool printed
= false;
1072 // print the skolemizations
1073 if (d_skolemize
->printSkolemization(out
))
1077 // print the instantiations
1078 if (d_instantiate
->printInstantiations(out
))
1083 out
<< "No instantiations" << std::endl
;
1087 void QuantifiersEngine::printSynthSolution( std::ostream
& out
) {
1089 d_ceg_inst
->printSynthSolution( out
);
1091 out
<< "Internal error : module for synth solution not found." << std::endl
;
1095 void QuantifiersEngine::getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
) {
1096 d_instantiate
->getInstantiatedQuantifiedFormulas(qs
);
1099 void QuantifiersEngine::getInstantiations( std::map
< Node
, std::vector
< Node
> >& insts
) {
1100 d_instantiate
->getInstantiations(insts
);
1103 void QuantifiersEngine::getInstantiations( Node q
, std::vector
< Node
>& insts
) {
1104 d_instantiate
->getInstantiations(q
, insts
);
1107 Node
QuantifiersEngine::getInstantiatedConjunction( Node q
) {
1108 return d_instantiate
->getInstantiatedConjunction(q
);
1111 QuantifiersEngine::Statistics::Statistics()
1112 : d_time("theory::QuantifiersEngine::time"),
1113 d_qcf_time("theory::QuantifiersEngine::time_qcf"),
1114 d_ematching_time("theory::QuantifiersEngine::time_ematching"),
1115 d_num_quant("QuantifiersEngine::Num_Quantifiers", 0),
1116 d_instantiation_rounds("QuantifiersEngine::Rounds_Instantiation_Full", 0),
1117 d_instantiation_rounds_lc("QuantifiersEngine::Rounds_Instantiation_Last_Call", 0),
1118 d_triggers("QuantifiersEngine::Triggers", 0),
1119 d_simple_triggers("QuantifiersEngine::Triggers_Simple", 0),
1120 d_multi_triggers("QuantifiersEngine::Triggers_Multi", 0),
1121 d_multi_trigger_instantiations("QuantifiersEngine::Multi_Trigger_Instantiations", 0),
1122 d_red_alpha_equiv("QuantifiersEngine::Reductions_Alpha_Equivalence", 0),
1123 d_instantiations_user_patterns("QuantifiersEngine::Instantiations_User_Patterns", 0),
1124 d_instantiations_auto_gen("QuantifiersEngine::Instantiations_Auto_Gen", 0),
1125 d_instantiations_guess("QuantifiersEngine::Instantiations_Guess", 0),
1126 d_instantiations_qcf("QuantifiersEngine::Instantiations_Qcf_Conflict", 0),
1127 d_instantiations_qcf_prop("QuantifiersEngine::Instantiations_Qcf_Prop", 0),
1128 d_instantiations_fmf_exh("QuantifiersEngine::Instantiations_Fmf_Exh", 0),
1129 d_instantiations_fmf_mbqi("QuantifiersEngine::Instantiations_Fmf_Mbqi", 0),
1130 d_instantiations_cbqi("QuantifiersEngine::Instantiations_Cbqi", 0),
1131 d_instantiations_rr("QuantifiersEngine::Instantiations_Rewrite_Rules", 0)
1133 smtStatisticsRegistry()->registerStat(&d_time
);
1134 smtStatisticsRegistry()->registerStat(&d_qcf_time
);
1135 smtStatisticsRegistry()->registerStat(&d_ematching_time
);
1136 smtStatisticsRegistry()->registerStat(&d_num_quant
);
1137 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds
);
1138 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds_lc
);
1139 smtStatisticsRegistry()->registerStat(&d_triggers
);
1140 smtStatisticsRegistry()->registerStat(&d_simple_triggers
);
1141 smtStatisticsRegistry()->registerStat(&d_multi_triggers
);
1142 smtStatisticsRegistry()->registerStat(&d_multi_trigger_instantiations
);
1143 smtStatisticsRegistry()->registerStat(&d_red_alpha_equiv
);
1144 smtStatisticsRegistry()->registerStat(&d_instantiations_user_patterns
);
1145 smtStatisticsRegistry()->registerStat(&d_instantiations_auto_gen
);
1146 smtStatisticsRegistry()->registerStat(&d_instantiations_guess
);
1147 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf
);
1148 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf_prop
);
1149 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_exh
);
1150 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_mbqi
);
1151 smtStatisticsRegistry()->registerStat(&d_instantiations_cbqi
);
1152 smtStatisticsRegistry()->registerStat(&d_instantiations_rr
);
1155 QuantifiersEngine::Statistics::~Statistics(){
1156 smtStatisticsRegistry()->unregisterStat(&d_time
);
1157 smtStatisticsRegistry()->unregisterStat(&d_qcf_time
);
1158 smtStatisticsRegistry()->unregisterStat(&d_ematching_time
);
1159 smtStatisticsRegistry()->unregisterStat(&d_num_quant
);
1160 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds
);
1161 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds_lc
);
1162 smtStatisticsRegistry()->unregisterStat(&d_triggers
);
1163 smtStatisticsRegistry()->unregisterStat(&d_simple_triggers
);
1164 smtStatisticsRegistry()->unregisterStat(&d_multi_triggers
);
1165 smtStatisticsRegistry()->unregisterStat(&d_multi_trigger_instantiations
);
1166 smtStatisticsRegistry()->unregisterStat(&d_red_alpha_equiv
);
1167 smtStatisticsRegistry()->unregisterStat(&d_instantiations_user_patterns
);
1168 smtStatisticsRegistry()->unregisterStat(&d_instantiations_auto_gen
);
1169 smtStatisticsRegistry()->unregisterStat(&d_instantiations_guess
);
1170 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf
);
1171 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf_prop
);
1172 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_exh
);
1173 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_mbqi
);
1174 smtStatisticsRegistry()->unregisterStat(&d_instantiations_cbqi
);
1175 smtStatisticsRegistry()->unregisterStat(&d_instantiations_rr
);
1178 eq::EqualityEngine
* QuantifiersEngine::getMasterEqualityEngine(){
1179 return d_te
->getMasterEqualityEngine();
1182 eq::EqualityEngine
* QuantifiersEngine::getActiveEqualityEngine() {
1184 return d_model
->getEqualityEngine();
1186 return d_te
->getMasterEqualityEngine();
1190 Node
QuantifiersEngine::getInternalRepresentative( Node a
, Node q
, int index
){
1191 bool prevModelEe
= d_useModelEe
;
1192 d_useModelEe
= false;
1193 Node ret
= d_eq_query
->getInternalRepresentative( a
, q
, index
);
1194 d_useModelEe
= prevModelEe
;
1198 void QuantifiersEngine::getSynthSolutions(std::map
<Node
, Node
>& sol_map
)
1200 d_ceg_inst
->getSynthSolutions(sol_map
);
1203 void QuantifiersEngine::debugPrintEqualityEngine( const char * c
) {
1204 eq::EqualityEngine
* ee
= getActiveEqualityEngine();
1205 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( ee
);
1206 std::map
< TypeNode
, int > typ_num
;
1207 while( !eqcs_i
.isFinished() ){
1208 TNode r
= (*eqcs_i
);
1209 TypeNode tr
= r
.getType();
1210 if( typ_num
.find( tr
)==typ_num
.end() ){
1214 bool firstTime
= true;
1215 Trace(c
) << " " << r
;
1216 Trace(c
) << " : { ";
1217 eq::EqClassIterator eqc_i
= eq::EqClassIterator( r
, ee
);
1218 while( !eqc_i
.isFinished() ){
1222 Trace(c
) << std::endl
;
1225 Trace(c
) << " " << n
<< std::endl
;
1229 if( !firstTime
){ Trace(c
) << " "; }
1230 Trace(c
) << "}" << std::endl
;
1233 Trace(c
) << std::endl
;
1234 for( std::map
< TypeNode
, int >::iterator it
= typ_num
.begin(); it
!= typ_num
.end(); ++it
){
1235 Trace(c
) << "# eqc for " << it
->first
<< " : " << it
->second
<< std::endl
;