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-2016 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/sep/theory_sep.h"
23 #include "theory/quantifiers/alpha_equivalence.h"
24 #include "theory/quantifiers/ambqi_builder.h"
25 #include "theory/quantifiers/bounded_integers.h"
26 #include "theory/quantifiers/ce_guided_instantiation.h"
27 #include "theory/quantifiers/conjecture_generator.h"
28 #include "theory/quantifiers/first_order_model.h"
29 #include "theory/quantifiers/full_model_check.h"
30 #include "theory/quantifiers/fun_def_engine.h"
31 #include "theory/quantifiers/inst_strategy_cbqi.h"
32 #include "theory/quantifiers/inst_strategy_e_matching.h"
33 #include "theory/quantifiers/instantiation_engine.h"
34 #include "theory/quantifiers/local_theory_ext.h"
35 #include "theory/quantifiers/model_engine.h"
36 #include "theory/quantifiers/quant_conflict_find.h"
37 #include "theory/quantifiers/quant_equality_engine.h"
38 #include "theory/quantifiers/quantifiers_rewriter.h"
39 #include "theory/quantifiers/relevant_domain.h"
40 #include "theory/quantifiers/rewrite_engine.h"
41 #include "theory/quantifiers/term_database.h"
42 #include "theory/quantifiers/trigger.h"
43 #include "theory/quantifiers/quant_split.h"
44 #include "theory/quantifiers/anti_skolem.h"
45 #include "theory/quantifiers/equality_infer.h"
46 #include "theory/quantifiers/inst_propagator.h"
47 #include "theory/theory_engine.h"
48 #include "theory/uf/equality_engine.h"
49 #include "theory/uf/theory_uf.h"
50 #include "theory/uf/theory_uf_strong_solver.h"
54 using namespace CVC4::kind
;
55 using namespace CVC4::context
;
56 using namespace CVC4::theory
;
57 using namespace CVC4::theory::inst
;
59 QuantifiersEngine::QuantifiersEngine(context::Context
* c
, context::UserContext
* u
, TheoryEngine
* te
):
61 d_conflict_c(c
, false),
64 d_lemmas_produced_c(u
),
69 //d_ierCounterLastLc(c),
73 d_presolve_cache_wq(u
),
74 d_presolve_cache_wic(u
){
76 d_eq_query
= new EqualityQueryQuantifiersEngine( c
, this );
77 d_util
.push_back( d_eq_query
);
79 d_term_db
= new quantifiers::TermDb( c
, u
, this );
80 d_util
.push_back( d_term_db
);
82 if( options::instPropagate() ){
83 d_inst_prop
= new quantifiers::InstPropagator( this );
84 d_util
.push_back( d_inst_prop
);
85 d_inst_notify
.push_back( d_inst_prop
->getInstantiationNotify() );
90 d_tr_trie
= new inst::TriggerTrie
;
91 d_curr_effort_level
= QEFFORT_NONE
;
93 d_hasAddedLemma
= false;
94 //don't add true lemma
95 d_lemmas_produced_c
[d_term_db
->d_true
] = true;
97 Trace("quant-engine-debug") << "Initialize quantifiers engine." << std::endl
;
98 Trace("quant-engine-debug") << "Initialize model, mbqi : " << options::mbqiMode() << std::endl
;
101 if( options::mbqiMode()==quantifiers::MBQI_FMC
||
102 options::mbqiMode()==quantifiers::MBQI_FMC_INTERVAL
|| options::fmfBound() ||
103 options::mbqiMode()==quantifiers::MBQI_TRUST
){
104 d_model
= new quantifiers::fmcheck::FirstOrderModelFmc( this, c
, "FirstOrderModelFmc" );
105 }else if( options::mbqiMode()==quantifiers::MBQI_ABS
){
106 d_model
= new quantifiers::FirstOrderModelAbs( this, c
, "FirstOrderModelAbs" );
108 d_model
= new quantifiers::FirstOrderModelIG( this, c
, "FirstOrderModelIG" );
110 if( options::relevantTriggers() ){
111 d_quant_rel
= new QuantRelevance( false );
116 if( options::quantEpr() ){
117 Assert( !options::incrementalSolving() );
118 d_qepr
= new QuantEPR
;
126 d_inst_engine
= NULL
;
129 d_anti_skolem
= NULL
;
130 d_model_engine
= NULL
;
134 d_lte_part_inst
= NULL
;
135 d_alpha_equiv
= NULL
;
136 d_fun_def_engine
= NULL
;
142 d_total_inst_count_debug
= 0;
143 //allow theory combination to go first, once initially
144 d_ierCounter
= options::instWhenTcFirst() ? 0 : 1;
145 d_ierCounter_c
= d_ierCounter
;
147 d_ierCounterLastLc
= 0;
148 d_inst_when_phase
= 1 + ( options::instWhenPhase()<1 ? 1 : options::instWhenPhase() );
151 QuantifiersEngine::~QuantifiersEngine(){
152 for(std::map
< Node
, inst::CDInstMatchTrie
* >::iterator
153 i
= d_c_inst_match_trie
.begin(), iend
= d_c_inst_match_trie
.end();
158 d_c_inst_match_trie
.clear();
160 delete d_alpha_equiv
;
165 delete d_model_engine
;
166 delete d_inst_engine
;
176 delete d_lte_part_inst
;
177 delete d_fun_def_engine
;
182 delete d_anti_skolem
;
186 EqualityQueryQuantifiersEngine
* QuantifiersEngine::getEqualityQuery() {
190 context::Context
* QuantifiersEngine::getSatContext(){
191 return d_te
->theoryOf( THEORY_QUANTIFIERS
)->getSatContext();
194 context::UserContext
* QuantifiersEngine::getUserContext(){
195 return d_te
->theoryOf( THEORY_QUANTIFIERS
)->getUserContext();
198 OutputChannel
& QuantifiersEngine::getOutputChannel(){
199 return d_te
->theoryOf( THEORY_QUANTIFIERS
)->getOutputChannel();
201 /** get default valuation for the quantifiers engine */
202 Valuation
& QuantifiersEngine::getValuation(){
203 return d_te
->theoryOf( THEORY_QUANTIFIERS
)->getValuation();
206 void QuantifiersEngine::finishInit(){
207 context::Context
* c
= getSatContext();
208 Trace("quant-engine-debug") << "QuantifiersEngine : finishInit " << std::endl
;
209 bool needsBuilder
= false;
210 bool needsRelDom
= false;
211 //add quantifiers modules
212 if( options::quantConflictFind() || options::quantRewriteRules() ){
213 d_qcf
= new quantifiers::QuantConflictFind( this, c
);
214 d_modules
.push_back( d_qcf
);
216 if( options::conjectureGen() ){
217 d_sg_gen
= new quantifiers::ConjectureGenerator( this, c
);
218 d_modules
.push_back( d_sg_gen
);
220 if( !options::finiteModelFind() || options::fmfInstEngine() ){
221 d_inst_engine
= new quantifiers::InstantiationEngine( this );
222 d_modules
.push_back( d_inst_engine
);
224 if( options::cbqi() ){
225 d_i_cbqi
= new quantifiers::InstStrategyCegqi( this );
226 d_modules
.push_back( d_i_cbqi
);
227 if( options::cbqiModel() ){
231 if( options::ceGuidedInst() ){
232 d_ceg_inst
= new quantifiers::CegInstantiation( this, c
);
233 d_modules
.push_back( d_ceg_inst
);
236 //finite model finding
237 if( options::finiteModelFind() ){
238 if( options::fmfBound() ){
239 d_bint
= new quantifiers::BoundedIntegers( c
, this );
240 d_modules
.push_back( d_bint
);
242 d_model_engine
= new quantifiers::ModelEngine( c
, this );
243 d_modules
.push_back( d_model_engine
);
246 if( options::quantRewriteRules() ){
247 d_rr_engine
= new quantifiers::RewriteEngine( c
, this );
248 d_modules
.push_back(d_rr_engine
);
250 if( options::ltePartialInst() ){
251 d_lte_part_inst
= new quantifiers::LtePartialInst( this, c
);
252 d_modules
.push_back( d_lte_part_inst
);
254 if( ( options::finiteModelFind() && options::quantDynamicSplit()!=quantifiers::QUANT_DSPLIT_MODE_NONE
) ||
255 options::quantDynamicSplit()==quantifiers::QUANT_DSPLIT_MODE_AGG
){
256 d_qsplit
= new quantifiers::QuantDSplit( this, c
);
257 d_modules
.push_back( d_qsplit
);
259 if( options::quantAntiSkolem() ){
260 d_anti_skolem
= new quantifiers::QuantAntiSkolem( this );
261 d_modules
.push_back( d_anti_skolem
);
263 if( options::quantAlphaEquiv() ){
264 d_alpha_equiv
= new quantifiers::AlphaEquivalence( this );
266 //if( options::funDefs() ){
267 // d_fun_def_engine = new quantifiers::FunDefEngine( this, c );
268 // d_modules.push_back( d_fun_def_engine );
270 if( options::quantEqualityEngine() ){
271 d_uee
= new quantifiers::QuantEqualityEngine( this, c
);
272 d_modules
.push_back( d_uee
);
274 //full saturation : instantiate from relevant domain, then arbitrary terms
275 if( options::fullSaturateQuant() || options::fullSaturateInst() ){
276 d_fs
= new quantifiers::FullSaturation( this );
277 d_modules
.push_back( d_fs
);
282 d_rel_dom
= new quantifiers::RelevantDomain( this, d_model
);
283 d_util
.push_back( d_rel_dom
);
287 Trace("quant-engine-debug") << "Initialize model engine, mbqi : " << options::mbqiMode() << " " << options::fmfBound() << std::endl
;
288 if( options::mbqiMode()==quantifiers::MBQI_FMC
|| options::mbqiMode()==quantifiers::MBQI_FMC_INTERVAL
||
289 options::mbqiMode()==quantifiers::MBQI_TRUST
|| options::fmfBound() ){
290 Trace("quant-engine-debug") << "...make fmc builder." << std::endl
;
291 d_builder
= new quantifiers::fmcheck::FullModelChecker( c
, this );
292 }else if( options::mbqiMode()==quantifiers::MBQI_ABS
){
293 Trace("quant-engine-debug") << "...make abs mbqi builder." << std::endl
;
294 d_builder
= new quantifiers::AbsMbqiBuilder( c
, this );
296 Trace("quant-engine-debug") << "...make default model builder." << std::endl
;
297 d_builder
= new quantifiers::QModelBuilderDefault( c
, this );
303 QuantifiersModule
* QuantifiersEngine::getOwner( Node q
) {
304 std::map
< Node
, QuantifiersModule
* >::iterator it
= d_owner
.find( q
);
305 if( it
==d_owner
.end() ){
312 void QuantifiersEngine::setOwner( Node q
, QuantifiersModule
* m
, int priority
) {
313 QuantifiersModule
* mo
= getOwner( q
);
316 if( priority
<=d_owner_priority
[q
] ){
317 Trace("quant-warn") << "WARNING: setting owner of " << q
<< " to " << ( m
? m
->identify() : "null" ) << ", but already has owner " << mo
->identify() << " with higher priority!" << std::endl
;
322 d_owner_priority
[q
] = priority
;
326 bool QuantifiersEngine::hasOwnership( Node q
, QuantifiersModule
* m
) {
327 QuantifiersModule
* mo
= getOwner( q
);
328 return mo
==m
|| mo
==NULL
;
331 void QuantifiersEngine::presolve() {
332 Trace("quant-engine-proc") << "QuantifiersEngine : presolve " << std::endl
;
333 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
334 d_modules
[i
]->presolve();
336 d_term_db
->presolve();
338 //add all terms to database
339 if( options::incrementalSolving() ){
340 Trace("quant-engine-proc") << "Add presolve cache " << d_presolve_cache
.size() << std::endl
;
341 for( unsigned i
=0; i
<d_presolve_cache
.size(); i
++ ){
342 addTermToDatabase( d_presolve_cache
[i
], d_presolve_cache_wq
[i
], d_presolve_cache_wic
[i
] );
344 Trace("quant-engine-proc") << "Done add presolve cache " << std::endl
;
348 void QuantifiersEngine::ppNotifyAssertions( std::vector
< Node
>& assertions
) {
349 Trace("quant-engine-proc") << "ppNotifyAssertions in QE, #assertions = " << assertions
.size() << " check epr = " << (d_qepr
!=NULL
) << std::endl
;
350 if( ( options::instLevelInputOnly() && options::instMaxLevel()!=-1 ) || d_qepr
!=NULL
){
351 for( unsigned i
=0; i
<assertions
.size(); i
++ ) {
352 if( options::instLevelInputOnly() && options::instMaxLevel()!=-1 ){
353 setInstantiationLevelAttr( assertions
[i
], 0 );
356 d_qepr
->registerAssertion( assertions
[i
] );
360 //must handle sources of other new constants e.g. separation logic
362 ((sep::TheorySep
*)getTheoryEngine()->theoryOf( THEORY_SEP
))->initializeBounds();
363 d_qepr
->finishInit();
368 void QuantifiersEngine::check( Theory::Effort e
){
369 CodeTimer
codeTimer(d_statistics
.d_time
);
370 if( !getMasterEqualityEngine()->consistent() ){
371 Trace("quant-engine-debug") << "Master equality engine not consistent, return." << std::endl
;
374 bool needsCheck
= !d_lemmas_waiting
.empty();
375 unsigned needsModelE
= QEFFORT_NONE
;
376 std::vector
< QuantifiersModule
* > qm
;
377 if( d_model
->checkNeeded() ){
378 needsCheck
= needsCheck
|| e
>=Theory::EFFORT_LAST_CALL
; //always need to check at or above last call
379 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
380 if( d_modules
[i
]->needsCheck( e
) ){
381 qm
.push_back( d_modules
[i
] );
383 //can only request model at last call since theory combination can find inconsistencies
384 if( e
>=Theory::EFFORT_LAST_CALL
){
385 unsigned me
= d_modules
[i
]->needsModel( e
);
386 needsModelE
= me
<needsModelE
? me
: needsModelE
;
393 d_hasAddedLemma
= false;
394 bool setIncomplete
= false;
395 bool usedModelBuilder
= false;
397 Trace("quant-engine-debug2") << "Quantifiers Engine call to check, level = " << e
<< ", needsCheck=" << needsCheck
<< std::endl
;
399 //this will fail if a set of instances is marked as a conflict, but is not
400 Assert( !d_conflict_c
.get() );
401 //flush previous lemmas (for instance, if was interupted), or other lemmas to process
403 if( d_hasAddedLemma
){
406 if( !d_recorded_inst
.empty() ){
407 Trace("quant-engine-debug") << "Removing " << d_recorded_inst
.size() << " instantiations..." << std::endl
;
408 //remove explicitly recorded instantiations
409 for( unsigned i
=0; i
<d_recorded_inst
.size(); i
++ ){
410 removeInstantiationInternal( d_recorded_inst
[i
].first
, d_recorded_inst
[i
].second
);
412 d_recorded_inst
.clear();
416 if( Trace
.isOn("quant-engine") ){
417 clSet
= double(clock())/double(CLOCKS_PER_SEC
);
418 Trace("quant-engine") << ">>>>> Quantifiers Engine Round, effort = " << e
<< " <<<<<" << std::endl
;
421 if( Trace
.isOn("quant-engine-debug") ){
422 Trace("quant-engine-debug") << "Quantifiers Engine check, level = " << e
<< std::endl
;
423 Trace("quant-engine-debug") << " depth : " << d_ierCounter_c
<< std::endl
;
424 Trace("quant-engine-debug") << " modules to check : ";
425 for( unsigned i
=0; i
<qm
.size(); i
++ ){
426 Trace("quant-engine-debug") << qm
[i
]->identify() << " ";
428 Trace("quant-engine-debug") << std::endl
;
429 Trace("quant-engine-debug") << " # quantified formulas = " << d_model
->getNumAssertedQuantifiers() << std::endl
;
430 if( !d_lemmas_waiting
.empty() ){
431 Trace("quant-engine-debug") << " lemmas waiting = " << d_lemmas_waiting
.size() << std::endl
;
433 Trace("quant-engine-debug") << " Theory engine finished : " << !d_te
->needCheck() << std::endl
;
434 Trace("quant-engine-debug") << " Needs model effort : " << needsModelE
<< std::endl
;
436 if( Trace
.isOn("quant-engine-ee-pre") ){
437 Trace("quant-engine-ee-pre") << "Equality engine (pre-inference): " << std::endl
;
438 debugPrintEqualityEngine( "quant-engine-ee-pre" );
440 if( Trace
.isOn("quant-engine-assert") ){
441 Trace("quant-engine-assert") << "Assertions : " << std::endl
;
442 getTheoryEngine()->printAssertions("quant-engine-assert");
446 Trace("quant-engine-debug") << "Resetting all utilities..." << std::endl
;
447 for( unsigned i
=0; i
<d_util
.size(); i
++ ){
448 Trace("quant-engine-debug2") << "Reset " << d_util
[i
]->identify().c_str() << "..." << std::endl
;
449 if( !d_util
[i
]->reset( e
) ){
451 if( d_hasAddedLemma
){
454 //should only fail reset if added a lemma
460 if( Trace
.isOn("quant-engine-ee") ){
461 Trace("quant-engine-ee") << "Equality engine : " << std::endl
;
462 debugPrintEqualityEngine( "quant-engine-ee" );
466 Trace("quant-engine-debug") << "Reset model..." << std::endl
;
467 d_model
->reset_round();
470 Trace("quant-engine-debug") << "Resetting all modules..." << std::endl
;
471 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
472 Trace("quant-engine-debug2") << "Reset " << d_modules
[i
]->identify().c_str() << std::endl
;
473 d_modules
[i
]->reset_round( e
);
475 Trace("quant-engine-debug") << "Done resetting all modules." << std::endl
;
476 //reset may have added lemmas
478 if( d_hasAddedLemma
){
482 if( e
==Theory::EFFORT_LAST_CALL
){
483 //if effort is last call, try to minimize model first
484 //uf::StrongSolverTheoryUF * ufss = ((uf::TheoryUF*)getTheoryEngine()->theoryOf( THEORY_UF ))->getStrongSolver();
485 //if( ufss && !ufss->minimize() ){ return; }
486 ++(d_statistics
.d_instantiation_rounds_lc
);
487 }else if( e
==Theory::EFFORT_FULL
){
488 ++(d_statistics
.d_instantiation_rounds
);
490 Trace("quant-engine-debug") << "Check modules that needed check..." << std::endl
;
491 for( unsigned quant_e
= QEFFORT_CONFLICT
; quant_e
<=QEFFORT_LAST_CALL
; quant_e
++ ){
492 d_curr_effort_level
= quant_e
;
494 //build the model if any module requested it
495 if( needsModelE
==quant_e
){
496 Assert( d_builder
!=NULL
);
497 Trace("quant-engine-debug") << "Build model..." << std::endl
;
498 usedModelBuilder
= true;
499 d_builder
->d_addedLemmas
= 0;
500 d_builder
->buildModel( d_model
, false );
501 //we are done if model building was unsuccessful
502 if( d_builder
->d_addedLemmas
>0 ){
508 for( unsigned i
=0; i
<qm
.size(); i
++ ){
509 Trace("quant-engine-debug") << "Check " << qm
[i
]->identify().c_str() << " at effort " << quant_e
<< "..." << std::endl
;
510 qm
[i
]->check( e
, quant_e
);
512 Trace("quant-engine-debug") << "...conflict!" << std::endl
;
517 //flush all current lemmas
519 //if we have added one, stop
520 if( d_hasAddedLemma
){
523 Assert( !d_conflict
);
524 if( quant_e
==QEFFORT_CONFLICT
){
525 if( e
==Theory::EFFORT_FULL
){
526 //increment if a last call happened, we are not strictly enforcing interleaving, or already were in phase
527 if( d_ierCounterLastLc
!=d_ierCounter_lc
|| !options::instWhenStrictInterleave() || d_ierCounter
%d_inst_when_phase
!=0 ){
528 d_ierCounter
= d_ierCounter
+ 1;
529 d_ierCounterLastLc
= d_ierCounter_lc
;
530 d_ierCounter_c
= d_ierCounter_c
.get() + 1;
532 }else if( e
==Theory::EFFORT_LAST_CALL
){
533 d_ierCounter_lc
= d_ierCounter_lc
+ 1;
535 }else if( quant_e
==QEFFORT_MODEL
){
536 if( e
==Theory::EFFORT_LAST_CALL
){
537 //sources of incompleteness
538 if( !d_recorded_inst
.empty() ){
539 Trace("quant-engine-debug") << "Set incomplete due to recorded instantiations." << std::endl
;
540 setIncomplete
= true;
542 //if we have a chance not to set incomplete
543 if( !setIncomplete
){
544 setIncomplete
= false;
545 //check if we should set the incomplete flag
546 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
547 if( !d_modules
[i
]->checkComplete() ){
548 Trace("quant-engine-debug") << "Set incomplete because " << d_modules
[i
]->identify().c_str() << " was incomplete." << std::endl
;
549 setIncomplete
= true;
553 if( !setIncomplete
){
554 //look at individual quantified formulas, one module must claim completeness for each one
555 for( unsigned i
=0; i
<d_model
->getNumAssertedQuantifiers(); i
++ ){
556 bool hasCompleteM
= false;
557 Node q
= d_model
->getAssertedQuantifier( i
);
558 QuantifiersModule
* qmd
= getOwner( q
);
560 hasCompleteM
= qmd
->checkCompleteFor( q
);
562 for( unsigned j
=0; j
<d_modules
.size(); j
++ ){
563 if( d_modules
[j
]->checkCompleteFor( q
) ){
571 Trace("quant-engine-debug") << "Set incomplete because " << q
<< " was not fully processed." << std::endl
;
572 setIncomplete
= true;
576 Trace("quant-engine-debug2") << "Complete for " << q
<< " due to " << qmd
->identify().c_str() << std::endl
;
581 //if setIncomplete = false, we will answer SAT, otherwise we will run at quant_e QEFFORT_LAST_CALL
582 if( !setIncomplete
){
589 d_curr_effort_level
= QEFFORT_NONE
;
590 Trace("quant-engine-debug") << "Done check modules that needed check." << std::endl
;
591 if( d_hasAddedLemma
){
593 if( Trace
.isOn("inst-per-quant-round") ){
594 for( std::map
< Node
, int >::iterator it
= d_temp_inst_debug
.begin(); it
!= d_temp_inst_debug
.end(); ++it
){
595 Trace("inst-per-quant-round") << " * " << it
->second
<< " for " << it
->first
<< std::endl
;
596 d_temp_inst_debug
[it
->first
] = 0;
600 if( Trace
.isOn("quant-engine") ){
601 double clSet2
= double(clock())/double(CLOCKS_PER_SEC
);
602 Trace("quant-engine") << "Finished quantifiers engine, total time = " << (clSet2
-clSet
);
603 Trace("quant-engine") << ", added lemma = " << d_hasAddedLemma
;
604 Trace("quant-engine") << std::endl
;
607 Trace("quant-engine-debug2") << "Finished quantifiers engine check." << std::endl
;
609 Trace("quant-engine-debug2") << "Quantifiers Engine does not need check." << std::endl
;
613 if( e
==Theory::EFFORT_LAST_CALL
&& !d_hasAddedLemma
){
614 if( options::produceModels() ){
615 if( usedModelBuilder
){
616 Trace("quant-engine-debug") << "Build completed model..." << std::endl
;
617 d_builder
->buildModel( d_model
, true );
618 }else if( !d_model
->isModelSet() ){
619 //use default model builder when no module built the model
620 Trace("quant-engine-debug") << "Build the model..." << std::endl
;
621 d_te
->getModelBuilder()->buildModel( d_model
, true );
622 Trace("quant-engine-debug") << "Done building the model." << std::endl
;
626 Trace("quant-engine") << "Set incomplete flag." << std::endl
;
627 getOutputChannel().setIncomplete();
630 if( Trace
.isOn("inst-per-quant") ){
631 for( std::map
< Node
, int >::iterator it
= d_total_inst_debug
.begin(); it
!= d_total_inst_debug
.end(); ++it
){
632 Trace("inst-per-quant") << " * " << it
->second
<< " for " << it
->first
<< std::endl
;
638 void QuantifiersEngine::notifyCombineTheories() {
639 //if allowing theory combination to happen at most once between instantiation rounds
641 //d_ierCounterLastLc = -1;
644 bool QuantifiersEngine::reduceQuantifier( Node q
) {
645 //TODO: this can be unified with preregistration: AlphaEquivalence takes ownership of reducable quants
646 BoolMap::const_iterator it
= d_quants_red
.find( q
);
647 if( it
==d_quants_red
.end() ){
649 std::map
< Node
, Node
>::iterator itr
= d_quants_red_lem
.find( q
);
650 if( itr
==d_quants_red_lem
.end() ){
652 Trace("quant-engine-red") << "Alpha equivalence " << q
<< "?" << std::endl
;
653 //add equivalence with another quantified formula
654 lem
= d_alpha_equiv
->reduceQuantifier( q
);
656 Trace("quant-engine-red") << "...alpha equivalence success." << std::endl
;
657 ++(d_statistics
.d_red_alpha_equiv
);
660 d_quants_red_lem
[q
] = lem
;
665 getOutputChannel().lemma( lem
);
667 d_quants_red
[q
] = !lem
.isNull();
668 return !lem
.isNull();
674 bool QuantifiersEngine::registerQuantifier( Node f
){
675 std::map
< Node
, bool >::iterator it
= d_quants
.find( f
);
676 if( it
==d_quants
.end() ){
677 Trace("quant") << "QuantifiersEngine : Register quantifier ";
678 Trace("quant") << " : " << f
<< std::endl
;
679 ++(d_statistics
.d_num_quant
);
680 Assert( f
.getKind()==FORALL
);
682 //check whether we should apply a reduction
683 if( reduceQuantifier( f
) ){
684 Trace("quant") << "...reduced." << std::endl
;
688 //make instantiation constants for f
689 d_term_db
->makeInstantiationConstantsFor( f
);
690 d_term_db
->computeAttributes( f
);
691 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
692 Trace("quant-debug") << "pre-register with " << d_modules
[i
]->identify() << "..." << std::endl
;
693 d_modules
[i
]->preRegisterQuantifier( f
);
695 QuantifiersModule
* qm
= getOwner( f
);
697 Trace("quant") << " Owner : " << qm
->identify() << std::endl
;
699 //register with quantifier relevance
701 d_quant_rel
->registerQuantifier( f
);
703 //register with each module
704 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
705 Trace("quant-debug") << "register with " << d_modules
[i
]->identify() << "..." << std::endl
;
706 d_modules
[i
]->registerQuantifier( f
);
709 Node ceBody
= d_term_db
->getInstConstantBody( f
);
710 //also register it with the strong solver
711 //if( options::finiteModelFind() ){
712 // ((uf::TheoryUF*)d_te->theoryOf( THEORY_UF ))->getStrongSolver()->registerQuantifier( f );
722 void QuantifiersEngine::registerPattern( std::vector
<Node
> & pattern
) {
723 for(std::vector
<Node
>::iterator p
= pattern
.begin(); p
!= pattern
.end(); ++p
){
724 std::set
< Node
> added
;
725 getTermDatabase()->addTerm( *p
, added
);
729 void QuantifiersEngine::assertQuantifier( Node f
, bool pol
){
732 if( !reduceQuantifier( f
) ){
734 if( d_skolemized
.find( f
)==d_skolemized
.end() ){
735 Node body
= d_term_db
->getSkolemizedBody( f
);
736 NodeBuilder
<> nb(kind::OR
);
737 nb
<< f
<< body
.notNode();
739 if( Trace
.isOn("quantifiers-sk-debug") ){
740 Node slem
= Rewriter::rewrite( lem
);
741 Trace("quantifiers-sk-debug") << "Skolemize lemma : " << slem
<< std::endl
;
743 getOutputChannel().lemma( lem
, false, true );
744 d_skolemized
[f
] = true;
748 //assert to modules TODO : also for !pol?
749 //register the quantifier, assert it to each module
750 if( registerQuantifier( f
) ){
751 d_model
->assertQuantifier( f
);
752 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
753 d_modules
[i
]->assertNode( f
);
755 addTermToDatabase( d_term_db
->getInstConstantBody( f
), true );
760 void QuantifiersEngine::propagate( Theory::Effort level
){
761 CodeTimer
codeTimer(d_statistics
.d_time
);
763 for( int i
=0; i
<(int)d_modules
.size(); i
++ ){
764 d_modules
[i
]->propagate( level
);
768 Node
QuantifiersEngine::getNextDecisionRequest( unsigned& priority
){
769 unsigned min_priority
= 0;
771 for( unsigned i
=0; i
<d_modules
.size(); i
++ ){
772 Node n
= d_modules
[i
]->getNextDecisionRequest( priority
);
773 if( !n
.isNull() && ( dec
.isNull() || priority
<min_priority
) ){
775 min_priority
= priority
;
781 quantifiers::TermDbSygus
* QuantifiersEngine::getTermDatabaseSygus() {
782 return getTermDatabase()->getTermDatabaseSygus();
785 void QuantifiersEngine::addTermToDatabase( Node n
, bool withinQuant
, bool withinInstClosure
){
786 if( options::incrementalSolving() ){
787 if( d_presolve_in
.find( n
)==d_presolve_in
.end() ){
788 d_presolve_in
.insert( n
);
789 d_presolve_cache
.push_back( n
);
790 d_presolve_cache_wq
.push_back( withinQuant
);
791 d_presolve_cache_wic
.push_back( withinInstClosure
);
794 //only wait if we are doing incremental solving
795 if( !d_presolve
|| !options::incrementalSolving() ){
796 std::set
< Node
> added
;
797 getTermDatabase()->addTerm( n
, added
, withinQuant
, withinInstClosure
);
799 //added contains also the Node that just have been asserted in this branch
801 for( std::set
< Node
>::iterator i
=added
.begin(), end
=added
.end(); i
!=end
; i
++ ){
803 d_quant_rel
->setRelevance( i
->getOperator(), 0 );
810 void QuantifiersEngine::eqNotifyNewClass(TNode t
) {
811 addTermToDatabase( t
);
812 if( d_eq_query
->getEqualityInference() ){
813 d_eq_query
->getEqualityInference()->eqNotifyNewClass( t
);
817 void QuantifiersEngine::eqNotifyPreMerge(TNode t1
, TNode t2
) {
818 if( d_eq_query
->getEqualityInference() ){
819 d_eq_query
->getEqualityInference()->eqNotifyMerge( t1
, t2
);
823 void QuantifiersEngine::eqNotifyPostMerge(TNode t1
, TNode t2
) {
827 void QuantifiersEngine::eqNotifyDisequal(TNode t1
, TNode t2
, TNode reason
) {
829 // d_qcf->assertDisequal( t1, t2 );
833 void QuantifiersEngine::computeTermVector( Node f
, InstMatch
& m
, std::vector
< Node
>& vars
, std::vector
< Node
>& terms
){
834 for( size_t i
=0; i
<f
[0].getNumChildren(); i
++ ){
837 vars
.push_back( f
[0][i
] );
838 terms
.push_back( n
);
844 bool QuantifiersEngine::recordInstantiationInternal( Node q
, std::vector
< Node
>& terms
, bool modEq
, bool addedLem
) {
846 //record the instantiation for deletion later
847 d_recorded_inst
.push_back( std::pair
< Node
, std::vector
< Node
> >( q
, terms
) );
849 if( options::incrementalSolving() ){
850 Trace("inst-add-debug") << "Adding into context-dependent inst trie, modEq = " << modEq
<< std::endl
;
851 inst::CDInstMatchTrie
* imt
;
852 std::map
< Node
, inst::CDInstMatchTrie
* >::iterator it
= d_c_inst_match_trie
.find( q
);
853 if( it
!=d_c_inst_match_trie
.end() ){
856 imt
= new CDInstMatchTrie( getUserContext() );
857 d_c_inst_match_trie
[q
] = imt
;
859 return imt
->addInstMatch( this, q
, terms
, getUserContext(), modEq
);
861 Trace("inst-add-debug") << "Adding into inst trie" << std::endl
;
862 return d_inst_match_trie
[q
].addInstMatch( this, q
, terms
, modEq
);
866 bool QuantifiersEngine::removeInstantiationInternal( Node q
, std::vector
< Node
>& terms
) {
867 if( options::incrementalSolving() ){
868 std::map
< Node
, inst::CDInstMatchTrie
* >::iterator it
= d_c_inst_match_trie
.find( q
);
869 if( it
!=d_c_inst_match_trie
.end() ){
870 return it
->second
->removeInstMatch( this, q
, terms
);
875 return d_inst_match_trie
[q
].removeInstMatch( this, q
, terms
);
879 void QuantifiersEngine::setInstantiationLevelAttr( Node n
, Node qn
, uint64_t level
){
880 Trace("inst-level-debug2") << "IL : " << n
<< " " << qn
<< " " << level
<< std::endl
;
881 //if not from the vector of terms we instantiatied
882 if( qn
.getKind()!=BOUND_VARIABLE
&& n
!=qn
){
883 //if this is a new term, without an instantiation level
884 if( !n
.hasAttribute(InstLevelAttribute()) ){
885 InstLevelAttribute ila
;
886 n
.setAttribute(ila
,level
);
887 Trace("inst-level-debug") << "Set instantiation level " << n
<< " to " << level
<< std::endl
;
888 Assert( n
.getNumChildren()==qn
.getNumChildren() );
889 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
890 setInstantiationLevelAttr( n
[i
], qn
[i
], level
);
896 void QuantifiersEngine::setInstantiationLevelAttr( Node n
, uint64_t level
){
897 if( !n
.hasAttribute(InstLevelAttribute()) ){
898 InstLevelAttribute ila
;
899 n
.setAttribute(ila
,level
);
900 Trace("inst-level-debug") << "Set instantiation level " << n
<< " to " << level
<< std::endl
;
901 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
902 setInstantiationLevelAttr( n
[i
], level
);
907 Node
QuantifiersEngine::getSubstitute( Node n
, std::vector
< Node
>& terms
){
908 if( n
.getKind()==INST_CONSTANT
){
909 Debug("check-inst") << "Substitute inst constant : " << n
<< std::endl
;
910 return terms
[n
.getAttribute(InstVarNumAttribute())];
912 //if( !quantifiers::TermDb::hasInstConstAttr( n ) ){
913 //Debug("check-inst") << "No inst const attr : " << n << std::endl;
916 //Debug("check-inst") << "Recurse on : " << n << std::endl;
917 std::vector
< Node
> cc
;
918 if( n
.getMetaKind() == kind::metakind::PARAMETERIZED
){
919 cc
.push_back( n
.getOperator() );
921 bool changed
= false;
922 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
923 Node c
= getSubstitute( n
[i
], terms
);
925 changed
= changed
|| c
!=n
[i
];
928 Node ret
= NodeManager::currentNM()->mkNode( n
.getKind(), cc
);
937 Node
QuantifiersEngine::getInstantiation( Node q
, std::vector
< Node
>& vars
, std::vector
< Node
>& terms
, bool doVts
){
939 Assert( vars
.size()==terms
.size() );
940 //process partial instantiation if necessary
941 if( q
[0].getNumChildren()!=vars
.size() ){
942 body
= q
[ 1 ].substitute( vars
.begin(), vars
.end(), terms
.begin(), terms
.end() );
943 std::vector
< Node
> uninst_vars
;
944 //doing a partial instantiation, must add quantifier for all uninstantiated variables
945 for( unsigned i
=0; i
<q
[0].getNumChildren(); i
++ ){
946 if( std::find( vars
.begin(), vars
.end(), q
[0][i
] )==vars
.end() ){
947 uninst_vars
.push_back( q
[0][i
] );
950 Trace("partial-inst") << "Partially instantiating with " << vars
.size() << " / " << q
[0].getNumChildren() << " for " << q
<< std::endl
;
951 Assert( !uninst_vars
.empty() );
952 Node bvl
= NodeManager::currentNM()->mkNode( BOUND_VAR_LIST
, uninst_vars
);
953 body
= NodeManager::currentNM()->mkNode( FORALL
, bvl
, body
);
954 Trace("partial-inst") << "Partial instantiation : " << q
<< std::endl
;
955 Trace("partial-inst") << " : " << body
<< std::endl
;
957 if( options::cbqi() ){
958 body
= q
[ 1 ].substitute( vars
.begin(), vars
.end(), terms
.begin(), terms
.end() );
960 //do optimized version
961 Node icb
= d_term_db
->getInstConstantBody( q
);
962 body
= getSubstitute( icb
, terms
);
963 if( Debug
.isOn("check-inst") ){
964 Node body2
= q
[ 1 ].substitute( vars
.begin(), vars
.end(), terms
.begin(), terms
.end() );
966 Debug("check-inst") << "Substitution is wrong : " << body
<< " " << body2
<< std::endl
;
972 //do virtual term substitution
973 body
= Rewriter::rewrite( body
);
974 Trace("quant-vts-debug") << "Rewrite vts symbols in " << body
<< std::endl
;
975 Node body_r
= d_term_db
->rewriteVtsSymbols( body
);
976 Trace("quant-vts-debug") << " ...result: " << body_r
<< std::endl
;
982 Node
QuantifiersEngine::getInstantiation( Node q
, InstMatch
& m
, bool doVts
){
983 std::vector
< Node
> vars
;
984 std::vector
< Node
> terms
;
985 computeTermVector( q
, m
, vars
, terms
);
986 return getInstantiation( q
, vars
, terms
, doVts
);
989 Node
QuantifiersEngine::getInstantiation( Node q
, std::vector
< Node
>& terms
, bool doVts
) {
990 Assert( d_term_db
->d_vars
.find( q
)!=d_term_db
->d_vars
.end() );
991 return getInstantiation( q
, d_term_db
->d_vars
[q
], terms
, doVts
);
995 bool QuantifiersEngine::existsInstantiation( Node f, InstMatch& m, bool modEq ){
996 if( options::incrementalSolving() ){
997 if( d_c_inst_match_trie.find( f )!=d_c_inst_match_trie.end() ){
998 if( d_c_inst_match_trie[f]->existsInstMatch( this, f, m, getUserContext(), modEq ) ){
1003 if( d_inst_match_trie.find( f )!=d_inst_match_trie.end() ){
1004 if( d_inst_match_trie[f].existsInstMatch( this, f, m, modEq ) ){
1013 bool QuantifiersEngine::addLemma( Node lem
, bool doCache
, bool doRewrite
){
1016 lem
= Rewriter::rewrite(lem
);
1018 Trace("inst-add-debug") << "Adding lemma : " << lem
<< std::endl
;
1019 BoolMap::const_iterator itp
= d_lemmas_produced_c
.find( lem
);
1020 if( itp
==d_lemmas_produced_c
.end() || !(*itp
).second
){
1021 //d_curr_out->lemma( lem, false, true );
1022 d_lemmas_produced_c
[ lem
] = true;
1023 d_lemmas_waiting
.push_back( lem
);
1024 Trace("inst-add-debug") << "Added lemma" << std::endl
;
1027 Trace("inst-add-debug") << "Duplicate." << std::endl
;
1031 //do not need to rewrite, will be rewritten after sending
1032 d_lemmas_waiting
.push_back( lem
);
1037 bool QuantifiersEngine::removeLemma( Node lem
) {
1038 std::vector
< Node
>::iterator it
= std::find( d_lemmas_waiting
.begin(), d_lemmas_waiting
.end(), lem
);
1039 if( it
!=d_lemmas_waiting
.end() ){
1040 d_lemmas_waiting
.erase( it
, it
+ 1 );
1041 d_lemmas_produced_c
[ lem
] = false;
1048 void QuantifiersEngine::addRequirePhase( Node lit
, bool req
){
1049 d_phase_req_waiting
[lit
] = req
;
1052 bool QuantifiersEngine::addInstantiation( Node q
, InstMatch
& m
, bool mkRep
, bool modEq
, bool doVts
){
1053 std::vector
< Node
> terms
;
1054 m
.getTerms( q
, terms
);
1055 return addInstantiation( q
, terms
, mkRep
, modEq
, doVts
);
1058 bool QuantifiersEngine::addInstantiation( Node q
, std::vector
< Node
>& terms
, bool mkRep
, bool modEq
, bool doVts
) {
1059 // For resource-limiting (also does a time check).
1060 getOutputChannel().safePoint(options::quantifierStep());
1061 Assert( !d_conflict
);
1062 Assert( terms
.size()==q
[0].getNumChildren() );
1063 Trace("inst-add-debug") << "For quantified formula " << q
<< ", add instantiation: " << std::endl
;
1064 std::vector
< Node
> rlv_cond
;
1065 for( unsigned i
=0; i
<terms
.size(); i
++ ){
1066 Trace("inst-add-debug") << " " << q
[0][i
];
1067 Trace("inst-add-debug2") << " -> " << terms
[i
];
1068 if( terms
[i
].isNull() ){
1069 terms
[i
] = d_term_db
->getModelBasisTerm( q
[0][i
].getType() );
1072 //pick the best possible representative for instantiation, based on past use and simplicity of term
1073 terms
[i
] = d_eq_query
->getInternalRepresentative( terms
[i
], q
, i
);
1075 //ensure the type is correct
1076 terms
[i
] = quantifiers::TermDb::ensureType( terms
[i
], q
[0][i
].getType() );
1078 Trace("inst-add-debug") << " -> " << terms
[i
] << std::endl
;
1079 if( terms
[i
].isNull() ){
1080 Trace("inst-add-debug") << " --> Failed to make term vector, due to term/type restrictions." << std::endl
;
1083 //get relevancy conditions
1084 if( options::instRelevantCond() ){
1085 quantifiers::TermDb::getRelevancyCondition( terms
[i
], rlv_cond
);
1088 #ifdef CVC4_ASSERTIONS
1089 bool bad_inst
= false;
1090 if( quantifiers::TermDb::containsUninterpretedConstant( terms
[i
] ) ){
1091 Trace("inst") << "***& inst contains uninterpreted constant : " << terms
[i
] << std::endl
;
1093 }else if( !terms
[i
].getType().isSubtypeOf( q
[0][i
].getType() ) ){
1094 Trace("inst") << "***& inst bad type : " << terms
[i
] << " " << terms
[i
].getType() << "/" << q
[0][i
].getType() << std::endl
;
1096 }else if( options::cbqi() ){
1097 Node icf
= quantifiers::TermDb::getInstConstAttr(terms
[i
]);
1098 if( !icf
.isNull() ){
1100 Trace("inst") << "***& inst contains inst constant attr : " << terms
[i
] << std::endl
;
1102 }else if( quantifiers::TermDb::containsTerms( terms
[i
], d_term_db
->d_inst_constants
[q
] ) ){
1103 Trace("inst") << "***& inst contains inst constants : " << terms
[i
] << std::endl
;
1108 //this assertion is critical to soundness
1110 Trace("inst")<< "***& Bad Instantiate " << q
<< " with " << std::endl
;
1111 for( unsigned j
=0; j
<terms
.size(); j
++ ){
1112 Trace("inst") << " " << terms
[j
] << std::endl
;
1119 //check based on instantiation level
1120 if( options::instMaxLevel()!=-1 || options::lteRestrictInstClosure() ){
1121 for( unsigned i
=0; i
<terms
.size(); i
++ ){
1122 if( !d_term_db
->isTermEligibleForInstantiation( terms
[i
], q
, true ) ){
1128 //check for positive entailment
1129 if( options::instNoEntail() ){
1130 //TODO: check consistency of equality engine (if not aborting on utility's reset)
1131 std::map
< TNode
, TNode
> subs
;
1132 for( unsigned i
=0; i
<terms
.size(); i
++ ){
1133 subs
[q
[0][i
]] = terms
[i
];
1135 if( d_term_db
->isEntailed( q
[1], subs
, false, true ) ){
1136 Trace("inst-add-debug") << " --> Currently entailed." << std::endl
;
1137 ++(d_statistics
.d_inst_duplicate_ent
);
1140 //Node eval = d_term_db->evaluateTerm( q[1], subs, false, true );
1141 //Trace("inst-add-debug2") << "Instantiation evaluates to : " << std::endl;
1142 //Trace("inst-add-debug2") << " " << eval << std::endl;
1145 //check for term vector duplication
1146 bool alreadyExists
= !recordInstantiationInternal( q
, terms
, modEq
);
1147 if( alreadyExists
){
1148 Trace("inst-add-debug") << " --> Already exists." << std::endl
;
1149 ++(d_statistics
.d_inst_duplicate_eq
);
1153 //construct the instantiation
1154 Trace("inst-add-debug") << "Constructing instantiation..." << std::endl
;
1155 Assert( d_term_db
->d_vars
[q
].size()==terms
.size() );
1156 Node body
= getInstantiation( q
, d_term_db
->d_vars
[q
], terms
, doVts
); //do virtual term substitution
1157 Node orig_body
= body
;
1158 if( options::cbqiNestedQE() && d_i_cbqi
){
1159 body
= d_i_cbqi
->doNestedQE( q
, terms
, body
, doVts
);
1161 body
= quantifiers::QuantifiersRewriter::preprocess( body
, true );
1162 Trace("inst-debug") << "...preprocess to " << body
<< std::endl
;
1164 //construct the lemma
1165 Trace("inst-assert") << "(assert " << body
<< ")" << std::endl
;
1166 body
= Rewriter::rewrite(body
);
1168 if( rlv_cond
.empty() ){
1169 lem
= NodeManager::currentNM()->mkNode( kind::OR
, q
.negate(), body
);
1171 rlv_cond
.push_back( q
.negate() );
1172 rlv_cond
.push_back( body
);
1173 lem
= NodeManager::currentNM()->mkNode( kind::OR
, rlv_cond
);
1175 lem
= Rewriter::rewrite(lem
);
1177 //check for lemma duplication
1178 if( addLemma( lem
, true, false ) ){
1179 d_total_inst_debug
[q
]++;
1180 d_temp_inst_debug
[q
]++;
1181 d_total_inst_count_debug
++;
1182 if( Trace
.isOn("inst") ){
1183 Trace("inst") << "*** Instantiate " << q
<< " with " << std::endl
;
1184 for( unsigned i
=0; i
<terms
.size(); i
++ ){
1185 if( Trace
.isOn("inst") ){
1186 Trace("inst") << " " << terms
[i
];
1187 if( Trace
.isOn("inst-debug") ){
1188 Trace("inst-debug") << ", type=" << terms
[i
].getType() << ", var_type=" << q
[0][i
].getType();
1190 Trace("inst") << std::endl
;
1194 if( options::instMaxLevel()!=-1 ){
1196 //virtual term substitution/instantiation level features are incompatible
1199 uint64_t maxInstLevel
= 0;
1200 for( unsigned i
=0; i
<terms
.size(); i
++ ){
1201 if( terms
[i
].hasAttribute(InstLevelAttribute()) ){
1202 if( terms
[i
].getAttribute(InstLevelAttribute())>maxInstLevel
){
1203 maxInstLevel
= terms
[i
].getAttribute(InstLevelAttribute());
1207 setInstantiationLevelAttr( orig_body
, q
[1], maxInstLevel
+1 );
1210 if( d_curr_effort_level
>QEFFORT_CONFLICT
&& d_curr_effort_level
<QEFFORT_NONE
){
1212 for( unsigned j
=0; j
<d_inst_notify
.size(); j
++ ){
1213 if( !d_inst_notify
[j
]->notifyInstantiation( d_curr_effort_level
, q
, lem
, terms
, body
) ){
1214 Trace("inst-add-debug") << "...we are in conflict." << std::endl
;
1216 d_conflict_c
= true;
1217 Assert( !d_lemmas_waiting
.empty() );
1222 if( options::trackInstLemmas() ){
1224 if( options::incrementalSolving() ){
1225 recorded
= d_c_inst_match_trie
[q
]->recordInstLemma( q
, terms
, lem
);
1227 recorded
= d_inst_match_trie
[q
].recordInstLemma( q
, terms
, lem
);
1229 Trace("inst-add-debug") << "...was recorded : " << recorded
<< std::endl
;
1232 Trace("inst-add-debug") << " --> Success." << std::endl
;
1233 ++(d_statistics
.d_instantiations
);
1236 Trace("inst-add-debug") << " --> Lemma already exists." << std::endl
;
1237 ++(d_statistics
.d_inst_duplicate
);
1242 bool QuantifiersEngine::removeInstantiation( Node q
, Node lem
, std::vector
< Node
>& terms
) {
1243 //lem must occur in d_waiting_lemmas
1244 if( removeLemma( lem
) ){
1245 return removeInstantiationInternal( q
, terms
);
1251 bool QuantifiersEngine::addSplit( Node n
, bool reqPhase
, bool reqPhasePol
){
1252 n
= Rewriter::rewrite( n
);
1253 Node lem
= NodeManager::currentNM()->mkNode( OR
, n
, n
.notNode() );
1254 if( addLemma( lem
) ){
1255 Debug("inst") << "*** Add split " << n
<< std::endl
;
1261 bool QuantifiersEngine::addSplitEquality( Node n1
, Node n2
, bool reqPhase
, bool reqPhasePol
){
1262 //Assert( !areEqual( n1, n2 ) );
1263 //Assert( !areDisequal( n1, n2 ) );
1264 Kind knd
= n1
.getType()==NodeManager::currentNM()->booleanType() ? IFF
: EQUAL
;
1265 Node fm
= NodeManager::currentNM()->mkNode( knd
, n1
, n2
);
1266 return addSplit( fm
);
1269 bool QuantifiersEngine::addEPRAxiom( TypeNode tn
) {
1271 Assert( !options::incrementalSolving() );
1272 if( d_qepr
->isEPR( tn
) && !d_qepr
->hasEPRAxiom( tn
) ){
1273 Node lem
= d_qepr
->mkEPRAxiom( tn
);
1274 Trace("quant-epr") << "EPR lemma for " << tn
<< " : " << lem
<< std::endl
;
1275 getOutputChannel().lemma( lem
);
1281 void QuantifiersEngine::markRelevant( Node q
) {
1282 d_model
->markRelevant( q
);
1285 bool QuantifiersEngine::getInstWhenNeedsCheck( Theory::Effort e
) {
1286 Trace("quant-engine-debug2") << "Get inst when needs check, counts=" << d_ierCounter
<< ", " << d_ierCounter_lc
<< std::endl
;
1287 //determine if we should perform check, based on instWhenMode
1288 bool performCheck
= false;
1289 if( options::instWhenMode()==quantifiers::INST_WHEN_FULL
){
1290 performCheck
= ( e
>= Theory::EFFORT_FULL
);
1291 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_DELAY
){
1292 performCheck
= ( e
>= Theory::EFFORT_FULL
) && !getTheoryEngine()->needCheck();
1293 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_LAST_CALL
){
1294 performCheck
= ( ( e
==Theory::EFFORT_FULL
&& d_ierCounter
%d_inst_when_phase
!=0 ) || e
==Theory::EFFORT_LAST_CALL
);
1295 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_DELAY_LAST_CALL
){
1296 performCheck
= ( ( e
==Theory::EFFORT_FULL
&& !getTheoryEngine()->needCheck() && d_ierCounter
%d_inst_when_phase
!=0 ) || e
==Theory::EFFORT_LAST_CALL
);
1297 }else if( options::instWhenMode()==quantifiers::INST_WHEN_LAST_CALL
){
1298 performCheck
= ( e
>= Theory::EFFORT_LAST_CALL
);
1300 performCheck
= true;
1302 if( e
==Theory::EFFORT_LAST_CALL
){
1303 //with bounded integers, skip every other last call,
1304 // since matching loops may occur with infinite quantification
1305 if( d_ierCounter_lc
%2==0 && options::fmfBound() ){
1306 performCheck
= false;
1309 return performCheck
;
1312 quantifiers::UserPatMode
QuantifiersEngine::getInstUserPatMode() {
1313 if( options::userPatternsQuant()==quantifiers::USER_PAT_MODE_INTERLEAVE
){
1314 return d_ierCounter
%2==0 ? quantifiers::USER_PAT_MODE_USE
: quantifiers::USER_PAT_MODE_RESORT
;
1316 return options::userPatternsQuant();
1320 void QuantifiersEngine::flushLemmas(){
1321 if( !d_lemmas_waiting
.empty() ){
1322 //filter based on notify classes
1323 if( !d_inst_notify
.empty() ){
1324 unsigned prev_lem_sz
= d_lemmas_waiting
.size();
1325 for( unsigned j
=0; j
<d_inst_notify
.size(); j
++ ){
1326 d_inst_notify
[j
]->filterInstantiations();
1328 if( prev_lem_sz
!=d_lemmas_waiting
.size() ){
1329 Trace("quant-engine") << "...filtered instances : " << d_lemmas_waiting
.size() << " / " << prev_lem_sz
<< std::endl
;
1332 //take default output channel if none is provided
1333 d_hasAddedLemma
= true;
1334 for( unsigned i
=0; i
<d_lemmas_waiting
.size(); i
++ ){
1335 Trace("qe-lemma") << "Lemma : " << d_lemmas_waiting
[i
] << std::endl
;
1336 getOutputChannel().lemma( d_lemmas_waiting
[i
], false, true );
1338 d_lemmas_waiting
.clear();
1340 if( !d_phase_req_waiting
.empty() ){
1341 for( std::map
< Node
, bool >::iterator it
= d_phase_req_waiting
.begin(); it
!= d_phase_req_waiting
.end(); ++it
){
1342 Trace("qe-lemma") << "Require phase : " << it
->first
<< " -> " << it
->second
<< std::endl
;
1343 getOutputChannel().requirePhase( it
->first
, it
->second
);
1345 d_phase_req_waiting
.clear();
1349 bool QuantifiersEngine::getUnsatCoreLemmas( std::vector
< Node
>& active_lemmas
) {
1350 //only if unsat core available
1351 bool isUnsatCoreAvailable
= false;
1352 if( options::proof() ){
1353 isUnsatCoreAvailable
= ProofManager::currentPM()->unsatCoreAvailable();
1355 if( isUnsatCoreAvailable
){
1356 Trace("inst-unsat-core") << "Get instantiations in unsat core..." << std::endl
;
1357 ProofManager::currentPM()->getLemmasInUnsatCore(theory::THEORY_QUANTIFIERS
, active_lemmas
);
1358 if( Trace
.isOn("inst-unsat-core") ){
1359 Trace("inst-unsat-core") << "Quantifiers lemmas in unsat core: " << std::endl
;
1360 for (unsigned i
= 0; i
< active_lemmas
.size(); ++i
) {
1361 Trace("inst-unsat-core") << " " << active_lemmas
[i
] << std::endl
;
1363 Trace("inst-unsat-core") << std::endl
;
1371 bool QuantifiersEngine::getUnsatCoreLemmas( std::vector
< Node
>& active_lemmas
, std::map
< Node
, Node
>& weak_imp
) {
1372 if( getUnsatCoreLemmas( active_lemmas
) ){
1373 for (unsigned i
= 0; i
< active_lemmas
.size(); ++i
) {
1374 Node n
= ProofManager::currentPM()->getWeakestImplicantInUnsatCore(active_lemmas
[i
]);
1375 if( n
!=active_lemmas
[i
] ){
1376 Trace("inst-unsat-core") << " weaken : " << active_lemmas
[i
] << " -> " << n
<< std::endl
;
1378 weak_imp
[active_lemmas
[i
]] = n
;
1386 void QuantifiersEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
1387 std::vector
< Node
> lemmas
;
1388 getInstantiations( q
, lemmas
);
1389 std::map
< Node
, Node
> quant
;
1390 std::map
< Node
, std::vector
< Node
> > tvec
;
1391 getExplanationForInstLemmas( lemmas
, quant
, tvec
);
1392 for( std::map
< Node
, std::vector
< Node
> >::iterator it
= tvec
.begin(); it
!= tvec
.end(); ++it
){
1393 tvecs
.push_back( it
->second
);
1397 void QuantifiersEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
1398 if( options::incrementalSolving() ){
1399 for( std::map
< Node
, inst::CDInstMatchTrie
* >::iterator it
= d_c_inst_match_trie
.begin(); it
!= d_c_inst_match_trie
.end(); ++it
){
1400 getInstantiationTermVectors( it
->first
, insts
[it
->first
] );
1403 for( std::map
< Node
, inst::InstMatchTrie
>::iterator it
= d_inst_match_trie
.begin(); it
!= d_inst_match_trie
.end(); ++it
){
1404 getInstantiationTermVectors( it
->first
, insts
[it
->first
] );
1409 void QuantifiersEngine::getExplanationForInstLemmas( std::vector
< Node
>& lems
, std::map
< Node
, Node
>& quant
, std::map
< Node
, std::vector
< Node
> >& tvec
) {
1410 if( options::trackInstLemmas() ){
1411 if( options::incrementalSolving() ){
1412 for( std::map
< Node
, inst::CDInstMatchTrie
* >::iterator it
= d_c_inst_match_trie
.begin(); it
!= d_c_inst_match_trie
.end(); ++it
){
1413 it
->second
->getExplanationForInstLemmas( it
->first
, lems
, quant
, tvec
);
1416 for( std::map
< Node
, inst::InstMatchTrie
>::iterator it
= d_inst_match_trie
.begin(); it
!= d_inst_match_trie
.end(); ++it
){
1417 it
->second
.getExplanationForInstLemmas( it
->first
, lems
, quant
, tvec
);
1420 #ifdef CVC4_ASSERTIONS
1421 for( unsigned j
=0; j
<lems
.size(); j
++ ){
1422 Assert( quant
.find( lems
[j
] )!=quant
.end() );
1423 Assert( tvec
.find( lems
[j
] )!=tvec
.end() );
1431 void QuantifiersEngine::printInstantiations( std::ostream
& out
) {
1432 bool useUnsatCore
= false;
1433 std::vector
< Node
> active_lemmas
;
1434 if( options::trackInstLemmas() && getUnsatCoreLemmas( active_lemmas
) ){
1435 useUnsatCore
= true;
1438 bool printed
= false;
1439 for( BoolMap::iterator it
= d_skolemized
.begin(); it
!= d_skolemized
.end(); ++it
){
1440 Node q
= (*it
).first
;
1442 out
<< "(skolem " << q
<< std::endl
;
1444 for( unsigned i
=0; i
<d_term_db
->d_skolem_constants
[q
].size(); i
++ ){
1445 if( i
>0 ){ out
<< " "; }
1446 out
<< d_term_db
->d_skolem_constants
[q
][i
];
1448 out
<< " )" << std::endl
;
1449 out
<< ")" << std::endl
;
1451 if( options::incrementalSolving() ){
1452 for( std::map
< Node
, inst::CDInstMatchTrie
* >::iterator it
= d_c_inst_match_trie
.begin(); it
!= d_c_inst_match_trie
.end(); ++it
){
1453 bool firstTime
= true;
1454 it
->second
->print( out
, it
->first
, firstTime
, useUnsatCore
, active_lemmas
);
1456 out
<< ")" << std::endl
;
1458 printed
= printed
|| !firstTime
;
1461 for( std::map
< Node
, inst::InstMatchTrie
>::iterator it
= d_inst_match_trie
.begin(); it
!= d_inst_match_trie
.end(); ++it
){
1462 bool firstTime
= true;
1463 it
->second
.print( out
, it
->first
, firstTime
, useUnsatCore
, active_lemmas
);
1465 out
<< ")" << std::endl
;
1467 printed
= printed
|| !firstTime
;
1471 out
<< "No instantiations" << std::endl
;
1475 void QuantifiersEngine::printSynthSolution( std::ostream
& out
) {
1477 d_ceg_inst
->printSynthSolution( out
);
1479 out
<< "Internal error : module for synth solution not found." << std::endl
;
1483 void QuantifiersEngine::getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
) {
1484 if( options::incrementalSolving() ){
1485 for( std::map
< Node
, inst::CDInstMatchTrie
* >::iterator it
= d_c_inst_match_trie
.begin(); it
!= d_c_inst_match_trie
.end(); ++it
){
1486 qs
.push_back( it
->first
);
1489 for( std::map
< Node
, inst::InstMatchTrie
>::iterator it
= d_inst_match_trie
.begin(); it
!= d_inst_match_trie
.end(); ++it
){
1490 qs
.push_back( it
->first
);
1495 void QuantifiersEngine::getInstantiations( std::map
< Node
, std::vector
< Node
> >& insts
) {
1496 bool useUnsatCore
= false;
1497 std::vector
< Node
> active_lemmas
;
1498 if( options::trackInstLemmas() && getUnsatCoreLemmas( active_lemmas
) ){
1499 useUnsatCore
= true;
1502 if( options::incrementalSolving() ){
1503 for( std::map
< Node
, inst::CDInstMatchTrie
* >::iterator it
= d_c_inst_match_trie
.begin(); it
!= d_c_inst_match_trie
.end(); ++it
){
1504 it
->second
->getInstantiations( insts
[it
->first
], it
->first
, this, useUnsatCore
, active_lemmas
);
1507 for( std::map
< Node
, inst::InstMatchTrie
>::iterator it
= d_inst_match_trie
.begin(); it
!= d_inst_match_trie
.end(); ++it
){
1508 it
->second
.getInstantiations( insts
[it
->first
], it
->first
, this, useUnsatCore
, active_lemmas
);
1513 void QuantifiersEngine::getInstantiations( Node q
, std::vector
< Node
>& insts
) {
1514 if( options::incrementalSolving() ){
1515 std::map
< Node
, inst::CDInstMatchTrie
* >::iterator it
= d_c_inst_match_trie
.find( q
);
1516 if( it
!=d_c_inst_match_trie
.end() ){
1517 std::vector
< Node
> active_lemmas
;
1518 it
->second
->getInstantiations( insts
, it
->first
, this, false, active_lemmas
);
1521 std::map
< Node
, inst::InstMatchTrie
>::iterator it
= d_inst_match_trie
.find( q
);
1522 if( it
!=d_inst_match_trie
.end() ){
1523 std::vector
< Node
> active_lemmas
;
1524 it
->second
.getInstantiations( insts
, it
->first
, this, false, active_lemmas
);
1529 Node
QuantifiersEngine::getInstantiatedConjunction( Node q
) {
1530 Assert( q
.getKind()==FORALL
);
1531 std::vector
< Node
> insts
;
1532 getInstantiations( q
, insts
);
1533 if( insts
.empty() ){
1534 return NodeManager::currentNM()->mkConst(true);
1537 if( insts
.size()==1 ){
1540 ret
= NodeManager::currentNM()->mkNode( kind::AND
, insts
);
1542 //have to remove q, TODO: avoid this in a better way
1544 TNode tt
= d_term_db
->d_true
;
1545 ret
= ret
.substitute( tq
, tt
);
1550 QuantifiersEngine::Statistics::Statistics()
1551 : d_time("theory::QuantifiersEngine::time"),
1552 d_qcf_time("theory::QuantifiersEngine::time_qcf"),
1553 d_ematching_time("theory::QuantifiersEngine::time_ematching"),
1554 d_num_quant("QuantifiersEngine::Num_Quantifiers", 0),
1555 d_instantiation_rounds("QuantifiersEngine::Rounds_Instantiation_Full", 0),
1556 d_instantiation_rounds_lc("QuantifiersEngine::Rounds_Instantiation_Last_Call", 0),
1557 d_instantiations("QuantifiersEngine::Instantiations_Total", 0),
1558 d_inst_duplicate("QuantifiersEngine::Duplicate_Inst", 0),
1559 d_inst_duplicate_eq("QuantifiersEngine::Duplicate_Inst_Eq", 0),
1560 d_inst_duplicate_ent("QuantifiersEngine::Duplicate_Inst_Entailed", 0),
1561 d_triggers("QuantifiersEngine::Triggers", 0),
1562 d_simple_triggers("QuantifiersEngine::Triggers_Simple", 0),
1563 d_multi_triggers("QuantifiersEngine::Triggers_Multi", 0),
1564 d_multi_trigger_instantiations("QuantifiersEngine::Multi_Trigger_Instantiations", 0),
1565 d_red_alpha_equiv("QuantifiersEngine::Reductions_Alpha_Equivalence", 0),
1566 d_instantiations_user_patterns("QuantifiersEngine::Instantiations_User_Patterns", 0),
1567 d_instantiations_auto_gen("QuantifiersEngine::Instantiations_Auto_Gen", 0),
1568 d_instantiations_guess("QuantifiersEngine::Instantiations_Guess", 0),
1569 d_instantiations_qcf("QuantifiersEngine::Instantiations_Qcf_Conflict", 0),
1570 d_instantiations_qcf_prop("QuantifiersEngine::Instantiations_Qcf_Prop", 0),
1571 d_instantiations_fmf_exh("QuantifiersEngine::Instantiations_Fmf_Exh", 0),
1572 d_instantiations_fmf_mbqi("QuantifiersEngine::Instantiations_Fmf_Mbqi", 0),
1573 d_instantiations_cbqi("QuantifiersEngine::Instantiations_Cbqi", 0),
1574 d_instantiations_rr("QuantifiersEngine::Instantiations_Rewrite_Rules", 0)
1576 smtStatisticsRegistry()->registerStat(&d_time
);
1577 smtStatisticsRegistry()->registerStat(&d_qcf_time
);
1578 smtStatisticsRegistry()->registerStat(&d_ematching_time
);
1579 smtStatisticsRegistry()->registerStat(&d_num_quant
);
1580 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds
);
1581 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds_lc
);
1582 smtStatisticsRegistry()->registerStat(&d_instantiations
);
1583 smtStatisticsRegistry()->registerStat(&d_inst_duplicate
);
1584 smtStatisticsRegistry()->registerStat(&d_inst_duplicate_eq
);
1585 smtStatisticsRegistry()->registerStat(&d_inst_duplicate_ent
);
1586 smtStatisticsRegistry()->registerStat(&d_triggers
);
1587 smtStatisticsRegistry()->registerStat(&d_simple_triggers
);
1588 smtStatisticsRegistry()->registerStat(&d_multi_triggers
);
1589 smtStatisticsRegistry()->registerStat(&d_multi_trigger_instantiations
);
1590 smtStatisticsRegistry()->registerStat(&d_red_alpha_equiv
);
1591 smtStatisticsRegistry()->registerStat(&d_instantiations_user_patterns
);
1592 smtStatisticsRegistry()->registerStat(&d_instantiations_auto_gen
);
1593 smtStatisticsRegistry()->registerStat(&d_instantiations_guess
);
1594 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf
);
1595 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf_prop
);
1596 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_exh
);
1597 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_mbqi
);
1598 smtStatisticsRegistry()->registerStat(&d_instantiations_cbqi
);
1599 smtStatisticsRegistry()->registerStat(&d_instantiations_rr
);
1602 QuantifiersEngine::Statistics::~Statistics(){
1603 smtStatisticsRegistry()->unregisterStat(&d_time
);
1604 smtStatisticsRegistry()->unregisterStat(&d_qcf_time
);
1605 smtStatisticsRegistry()->unregisterStat(&d_ematching_time
);
1606 smtStatisticsRegistry()->unregisterStat(&d_num_quant
);
1607 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds
);
1608 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds_lc
);
1609 smtStatisticsRegistry()->unregisterStat(&d_instantiations
);
1610 smtStatisticsRegistry()->unregisterStat(&d_inst_duplicate
);
1611 smtStatisticsRegistry()->unregisterStat(&d_inst_duplicate_eq
);
1612 smtStatisticsRegistry()->unregisterStat(&d_inst_duplicate_ent
);
1613 smtStatisticsRegistry()->unregisterStat(&d_triggers
);
1614 smtStatisticsRegistry()->unregisterStat(&d_simple_triggers
);
1615 smtStatisticsRegistry()->unregisterStat(&d_multi_triggers
);
1616 smtStatisticsRegistry()->unregisterStat(&d_multi_trigger_instantiations
);
1617 smtStatisticsRegistry()->unregisterStat(&d_red_alpha_equiv
);
1618 smtStatisticsRegistry()->unregisterStat(&d_instantiations_user_patterns
);
1619 smtStatisticsRegistry()->unregisterStat(&d_instantiations_auto_gen
);
1620 smtStatisticsRegistry()->unregisterStat(&d_instantiations_guess
);
1621 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf
);
1622 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf_prop
);
1623 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_exh
);
1624 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_mbqi
);
1625 smtStatisticsRegistry()->unregisterStat(&d_instantiations_cbqi
);
1626 smtStatisticsRegistry()->unregisterStat(&d_instantiations_rr
);
1629 eq::EqualityEngine
* QuantifiersEngine::getMasterEqualityEngine(){
1630 return getTheoryEngine()->getMasterEqualityEngine();
1633 void QuantifiersEngine::debugPrintEqualityEngine( const char * c
) {
1634 eq::EqualityEngine
* ee
= getMasterEqualityEngine();
1635 eq::EqClassesIterator eqcs_i
= eq::EqClassesIterator( ee
);
1636 std::map
< TypeNode
, int > typ_num
;
1637 while( !eqcs_i
.isFinished() ){
1638 TNode r
= (*eqcs_i
);
1639 TypeNode tr
= r
.getType();
1640 if( typ_num
.find( tr
)==typ_num
.end() ){
1644 bool firstTime
= true;
1645 Trace(c
) << " " << r
;
1646 Trace(c
) << " : { ";
1647 eq::EqClassIterator eqc_i
= eq::EqClassIterator( r
, ee
);
1648 while( !eqc_i
.isFinished() ){
1652 Trace(c
) << std::endl
;
1655 Trace(c
) << " " << n
<< std::endl
;
1659 if( !firstTime
){ Trace(c
) << " "; }
1660 Trace(c
) << "}" << std::endl
;
1663 Trace(c
) << std::endl
;
1664 for( std::map
< TypeNode
, int >::iterator it
= typ_num
.begin(); it
!= typ_num
.end(); ++it
){
1665 Trace(c
) << "# eqc for " << it
->first
<< " : " << it
->second
<< std::endl
;
1670 EqualityQueryQuantifiersEngine::EqualityQueryQuantifiersEngine( context::Context
* c
, QuantifiersEngine
* qe
) : d_qe( qe
), d_eqi_counter( c
), d_reset_count( 0 ){
1671 if( options::inferArithTriggerEq() ){
1672 d_eq_inference
= new quantifiers::EqualityInference( c
, options::inferArithTriggerEqExp() );
1674 d_eq_inference
= NULL
;
1678 EqualityQueryQuantifiersEngine::~EqualityQueryQuantifiersEngine(){
1679 delete d_eq_inference
;
1682 bool EqualityQueryQuantifiersEngine::reset( Theory::Effort e
){
1685 return processInferences( e
);
1688 bool EqualityQueryQuantifiersEngine::processInferences( Theory::Effort e
) {
1689 if( options::inferArithTriggerEq() ){
1690 eq::EqualityEngine
* ee
= getEngine();
1691 //updated implementation
1692 while( d_eqi_counter
.get()<d_eq_inference
->getNumPendingMerges() ){
1693 Node eq
= d_eq_inference
->getPendingMerge( d_eqi_counter
.get() );
1694 Node eq_exp
= d_eq_inference
->getPendingMergeExplanation( d_eqi_counter
.get() );
1695 Trace("quant-engine-ee-proc") << "processInferences : Infer : " << eq
<< std::endl
;
1696 Trace("quant-engine-ee-proc") << " explanation : " << eq_exp
<< std::endl
;
1697 Assert( ee
->hasTerm( eq
[0] ) );
1698 Assert( ee
->hasTerm( eq
[1] ) );
1699 if( areDisequal( eq
[0], eq
[1] ) ){
1700 Trace("quant-engine-ee-proc") << "processInferences : Conflict : " << eq
<< std::endl
;
1701 if( Trace
.isOn("term-db-lemma") ){
1702 Trace("term-db-lemma") << "Disequal terms, equal by normalization : " << eq
[0] << " " << eq
[1] << "!!!!" << std::endl
;
1703 if( !d_qe
->getTheoryEngine()->needCheck() ){
1704 Trace("term-db-lemma") << " all theories passed with no lemmas." << std::endl
;
1705 //this should really never happen (implies arithmetic is incomplete when sharing is enabled)
1708 Trace("term-db-lemma") << " add split on : " << eq
<< std::endl
;
1710 d_qe
->addSplit( eq
);
1713 ee
->assertEquality( eq
, true, eq_exp
);
1714 d_eqi_counter
= d_eqi_counter
.get() + 1;
1717 Assert( ee
->consistent() );
1722 bool EqualityQueryQuantifiersEngine::hasTerm( Node a
){
1723 return getEngine()->hasTerm( a
);
1726 Node
EqualityQueryQuantifiersEngine::getRepresentative( Node a
){
1727 eq::EqualityEngine
* ee
= getEngine();
1728 if( ee
->hasTerm( a
) ){
1729 return ee
->getRepresentative( a
);
1735 bool EqualityQueryQuantifiersEngine::areEqual( Node a
, Node b
){
1739 eq::EqualityEngine
* ee
= getEngine();
1740 if( ee
->hasTerm( a
) && ee
->hasTerm( b
) ){
1741 return ee
->areEqual( a
, b
);
1748 bool EqualityQueryQuantifiersEngine::areDisequal( Node a
, Node b
){
1752 eq::EqualityEngine
* ee
= getEngine();
1753 if( ee
->hasTerm( a
) && ee
->hasTerm( b
) ){
1754 return ee
->areDisequal( a
, b
, false );
1756 return a
.isConst() && b
.isConst();
1761 Node
EqualityQueryQuantifiersEngine::getInternalRepresentative( Node a
, Node f
, int index
){
1762 Assert( f
.isNull() || f
.getKind()==FORALL
);
1763 Node r
= getRepresentative( a
);
1764 if( options::finiteModelFind() ){
1765 if( r
.isConst() && quantifiers::TermDb::containsUninterpretedConstant( r
) ){
1766 //map back from values assigned by model, if any
1767 if( d_qe
->getModel() ){
1768 std::map
< Node
, Node
>::iterator it
= d_qe
->getModel()->d_rep_set
.d_values_to_terms
.find( r
);
1769 if( it
!=d_qe
->getModel()->d_rep_set
.d_values_to_terms
.end() ){
1771 r
= getRepresentative( r
);
1773 if( r
.getType().isSort() ){
1774 Trace("internal-rep-warn") << "No representative for UF constant." << std::endl
;
1775 //should never happen : UF constants should never escape model
1782 if( options::quantRepMode()==quantifiers::QUANT_REP_MODE_EE
){
1785 TypeNode v_tn
= f
.isNull() ? a
.getType() : f
[0][index
].getType();
1786 std::map
< Node
, Node
>::iterator itir
= d_int_rep
[v_tn
].find( r
);
1787 if( itir
==d_int_rep
[v_tn
].end() ){
1788 //find best selection for representative
1790 //if( options::fmfRelevantDomain() && !f.isNull() ){
1791 // Trace("internal-rep-debug") << "Consult relevant domain to mkRep " << r << std::endl;
1792 // r_best = d_qe->getRelevantDomain()->getRelevantTerm( f, index, r );
1793 // Trace("internal-rep-debug") << "Returned " << r_best << " " << r << std::endl;
1795 std::vector
< Node
> eqc
;
1796 getEquivalenceClass( r
, eqc
);
1797 Trace("internal-rep-select") << "Choose representative for equivalence class : { ";
1798 for( unsigned i
=0; i
<eqc
.size(); i
++ ){
1799 if( i
>0 ) Trace("internal-rep-select") << ", ";
1800 Trace("internal-rep-select") << eqc
[i
];
1802 Trace("internal-rep-select") << " }, type = " << v_tn
<< std::endl
;
1803 int r_best_score
= -1;
1804 for( size_t i
=0; i
<eqc
.size(); i
++ ){
1805 int score
= getRepScore( eqc
[i
], f
, index
, v_tn
);
1807 if( r_best
.isNull() || ( score
>=0 && ( r_best_score
<0 || score
<r_best_score
) ) ){
1809 r_best_score
= score
;
1813 if( r_best
.isNull() ){
1814 Trace("internal-rep-warn") << "No valid choice for representative in eqc class." << std::endl
;
1817 //now, make sure that no other member of the class is an instance
1818 std::hash_map
<TNode
, Node
, TNodeHashFunction
> cache
;
1819 r_best
= getInstance( r_best
, eqc
, cache
);
1820 //store that this representative was chosen at this point
1821 if( d_rep_score
.find( r_best
)==d_rep_score
.end() ){
1822 d_rep_score
[ r_best
] = d_reset_count
;
1824 Trace("internal-rep-select") << "...Choose " << r_best
<< " with score " << r_best_score
<< std::endl
;
1825 Assert( r_best
.getType().isSubtypeOf( v_tn
) );
1826 d_int_rep
[v_tn
][r
] = r_best
;
1828 Trace("internal-rep-debug") << "rep( " << a
<< " ) = " << r
<< ", " << std::endl
;
1829 Trace("internal-rep-debug") << "int_rep( " << a
<< " ) = " << r_best
<< ", " << std::endl
;
1833 return itir
->second
;
1838 void EqualityQueryQuantifiersEngine::flattenRepresentatives( std::map
< TypeNode
, std::vector
< Node
> >& reps
) {
1839 //make sure internal representatives currently exist
1840 for( std::map
< TypeNode
, std::vector
< Node
> >::iterator it
= reps
.begin(); it
!= reps
.end(); ++it
){
1841 if( it
->first
.isSort() ){
1842 for( unsigned i
=0; i
<it
->second
.size(); i
++ ){
1843 Node r
= getInternalRepresentative( it
->second
[i
], Node::null(), 0 );
1847 Trace("internal-rep-flatten") << "---Flattening representatives : " << std::endl
;
1848 for( std::map
< TypeNode
, std::map
< Node
, Node
> >::iterator itt
= d_int_rep
.begin(); itt
!= d_int_rep
.end(); ++itt
){
1849 for( std::map
< Node
, Node
>::iterator it
= itt
->second
.begin(); it
!= itt
->second
.end(); ++it
){
1850 Trace("internal-rep-flatten") << itt
->first
<< " : irep( " << it
->first
<< " ) = " << it
->second
<< std::endl
;
1853 //store representatives for newly created terms
1854 std::map
< Node
, Node
> temp_rep_map
;
1859 for( std::map
< TypeNode
, std::map
< Node
, Node
> >::iterator itt
= d_int_rep
.begin(); itt
!= d_int_rep
.end(); ++itt
){
1860 for( std::map
< Node
, Node
>::iterator it
= itt
->second
.begin(); it
!= itt
->second
.end(); ++it
){
1861 if( it
->second
.getKind()==APPLY_UF
&& it
->second
.getType().isSort() ){
1862 Node ni
= it
->second
;
1863 std::vector
< Node
> cc
;
1864 cc
.push_back( it
->second
.getOperator() );
1865 bool changed
= false;
1866 for( unsigned j
=0; j
<ni
.getNumChildren(); j
++ ){
1867 if( ni
[j
].getType().isSort() ){
1868 Node r
= getRepresentative( ni
[j
] );
1869 if( itt
->second
.find( r
)==itt
->second
.end() ){
1870 Assert( temp_rep_map
.find( r
)!=temp_rep_map
.end() );
1871 r
= temp_rep_map
[r
];
1874 //found sub-term as instance
1875 Trace("internal-rep-flatten-debug") << "...Changed " << it
->second
<< " to subterm " << ni
[j
] << std::endl
;
1876 itt
->second
[it
->first
] = ni
[j
];
1881 Node ir
= itt
->second
[r
];
1893 Node n
= NodeManager::currentNM()->mkNode( APPLY_UF
, cc
);
1894 Trace("internal-rep-flatten-debug") << "...Changed " << it
->second
<< " to " << n
<< std::endl
;
1896 itt
->second
[it
->first
] = n
;
1897 temp_rep_map
[n
] = it
->first
;
1903 Trace("internal-rep-flatten") << "---After flattening : " << std::endl
;
1904 for( std::map
< TypeNode
, std::map
< Node
, Node
> >::iterator itt
= d_int_rep
.begin(); itt
!= d_int_rep
.end(); ++itt
){
1905 for( std::map
< Node
, Node
>::iterator it
= itt
->second
.begin(); it
!= itt
->second
.end(); ++it
){
1906 Trace("internal-rep-flatten") << itt
->first
<< " : irep( " << it
->first
<< " ) = " << it
->second
<< std::endl
;
1911 eq::EqualityEngine
* EqualityQueryQuantifiersEngine::getEngine(){
1912 return d_qe
->getMasterEqualityEngine();
1915 void EqualityQueryQuantifiersEngine::getEquivalenceClass( Node a
, std::vector
< Node
>& eqc
){
1916 eq::EqualityEngine
* ee
= getEngine();
1917 if( ee
->hasTerm( a
) ){
1918 Node rep
= ee
->getRepresentative( a
);
1919 eq::EqClassIterator
eqc_iter( rep
, ee
);
1920 while( !eqc_iter
.isFinished() ){
1921 eqc
.push_back( *eqc_iter
);
1927 //a should be in its equivalence class
1928 Assert( std::find( eqc
.begin(), eqc
.end(), a
)!=eqc
.end() );
1931 TNode
EqualityQueryQuantifiersEngine::getCongruentTerm( Node f
, std::vector
< TNode
>& args
) {
1932 return d_qe
->getTermDatabase()->getCongruentTerm( f
, args
);
1937 Node
EqualityQueryQuantifiersEngine::getInstance( Node n
, const std::vector
< Node
>& eqc
, std::hash_map
<TNode
, Node
, TNodeHashFunction
>& cache
){
1938 if(cache
.find(n
) != cache
.end()) {
1941 for( size_t i
=0; i
<n
.getNumChildren(); i
++ ){
1942 Node nn
= getInstance( n
[i
], eqc
, cache
);
1944 return cache
[n
] = nn
;
1947 if( std::find( eqc
.begin(), eqc
.end(), n
)!=eqc
.end() ){
1948 return cache
[n
] = n
;
1950 return cache
[n
] = Node::null();
1954 //-2 : invalid, -1 : undesired, otherwise : smaller the score, the better
1955 int EqualityQueryQuantifiersEngine::getRepScore( Node n
, Node f
, int index
, TypeNode v_tn
){
1956 if( options::cbqi() && quantifiers::TermDb::hasInstConstAttr(n
) ){ //reject
1958 }else if( !n
.getType().isSubtypeOf( v_tn
) ){ //reject if incorrect type
1960 }else if( options::lteRestrictInstClosure() && ( !d_qe
->getTermDatabase()->isInstClosure( n
) || !d_qe
->getTermDatabase()->hasTermCurrent( n
, false ) ) ){
1962 }else if( options::instMaxLevel()!=-1 ){
1963 //score prefer lowest instantiation level
1964 if( n
.hasAttribute(InstLevelAttribute()) ){
1965 return n
.getAttribute(InstLevelAttribute());
1967 return options::instLevelInputOnly() ? -1 : 0;
1970 if( options::quantRepMode()==quantifiers::QUANT_REP_MODE_FIRST
){
1971 //score prefers earliest use of this term as a representative
1972 return d_rep_score
.find( n
)==d_rep_score
.end() ? -1 : d_rep_score
[n
];
1974 Assert( options::quantRepMode()==quantifiers::QUANT_REP_MODE_DEPTH
);
1975 return quantifiers::TermDb::getTermDepth( n
);