1008d7d493c58b0b3407670de66f8b2e365718dd
[cvc5.git] / src / theory / quantifiers_engine.cpp
1 /********************* */
2 /*! \file quantifiers_engine.cpp
3 ** \verbatim
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
11 **
12 ** \brief Implementation of quantifiers engine class
13 **/
14
15 #include "theory/quantifiers_engine.h"
16
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/ambqi_builder.h"
24 #include "theory/quantifiers/bounded_integers.h"
25 #include "theory/quantifiers/ce_guided_instantiation.h"
26 #include "theory/quantifiers/conjecture_generator.h"
27 #include "theory/quantifiers/first_order_model.h"
28 #include "theory/quantifiers/full_model_check.h"
29 #include "theory/quantifiers/fun_def_engine.h"
30 #include "theory/quantifiers/inst_strategy_cbqi.h"
31 #include "theory/quantifiers/inst_strategy_e_matching.h"
32 #include "theory/quantifiers/instantiation_engine.h"
33 #include "theory/quantifiers/local_theory_ext.h"
34 #include "theory/quantifiers/model_engine.h"
35 #include "theory/quantifiers/quant_conflict_find.h"
36 #include "theory/quantifiers/quant_equality_engine.h"
37 #include "theory/quantifiers/quantifiers_rewriter.h"
38 #include "theory/quantifiers/relevant_domain.h"
39 #include "theory/quantifiers/rewrite_engine.h"
40 #include "theory/quantifiers/term_database.h"
41 #include "theory/quantifiers/trigger.h"
42 #include "theory/quantifiers/quant_split.h"
43 #include "theory/quantifiers/anti_skolem.h"
44 #include "theory/quantifiers/equality_infer.h"
45 #include "theory/quantifiers/inst_propagator.h"
46 #include "theory/theory_engine.h"
47 #include "theory/uf/equality_engine.h"
48 #include "theory/uf/theory_uf.h"
49 #include "theory/uf/theory_uf_strong_solver.h"
50
51 using namespace std;
52 using namespace CVC4;
53 using namespace CVC4::kind;
54 using namespace CVC4::context;
55 using namespace CVC4::theory;
56 using namespace CVC4::theory::inst;
57
58 QuantifiersEngine::QuantifiersEngine(context::Context* c, context::UserContext* u, TheoryEngine* te):
59 d_te( te ),
60 //d_quants(u),
61 //d_quants_red(u),
62 d_lemmas_produced_c(u),
63 d_skolemized(u),
64 d_ierCounter_c(c),
65 //d_ierCounter(c),
66 //d_ierCounter_lc(c),
67 //d_ierCounterLastLc(c),
68 d_presolve(u, true),
69 d_presolve_in(u),
70 d_presolve_cache(u),
71 d_presolve_cache_wq(u),
72 d_presolve_cache_wic(u){
73 //utilities
74 d_eq_query = new EqualityQueryQuantifiersEngine( c, this );
75 d_util.push_back( d_eq_query );
76
77 d_term_db = new quantifiers::TermDb( c, u, this );
78 d_util.push_back( d_term_db );
79
80 if( options::instPropagate() ){
81 d_inst_prop = new quantifiers::InstPropagator( this );
82 d_util.push_back( d_inst_prop );
83 d_inst_notify.push_back( d_inst_prop->getInstantiationNotify() );
84 }else{
85 d_inst_prop = NULL;
86 }
87
88 d_tr_trie = new inst::TriggerTrie;
89 d_curr_effort_level = QEFFORT_NONE;
90 d_conflict = false;
91 d_num_added_lemmas_round = 0;
92 d_hasAddedLemma = false;
93 //don't add true lemma
94 d_lemmas_produced_c[d_term_db->d_true] = true;
95
96 Trace("quant-engine-debug") << "Initialize quantifiers engine." << std::endl;
97 Trace("quant-engine-debug") << "Initialize model, mbqi : " << options::mbqiMode() << std::endl;
98
99 //the model object
100 if( options::mbqiMode()==quantifiers::MBQI_FMC ||
101 options::mbqiMode()==quantifiers::MBQI_FMC_INTERVAL || options::fmfBoundInt() ||
102 options::mbqiMode()==quantifiers::MBQI_TRUST ){
103 d_model = new quantifiers::fmcheck::FirstOrderModelFmc( this, c, "FirstOrderModelFmc" );
104 }else if( options::mbqiMode()==quantifiers::MBQI_ABS ){
105 d_model = new quantifiers::FirstOrderModelAbs( this, c, "FirstOrderModelAbs" );
106 }else{
107 d_model = new quantifiers::FirstOrderModelIG( this, c, "FirstOrderModelIG" );
108 }
109 if( options::relevantTriggers() ){
110 d_quant_rel = new QuantRelevance( false );
111 }else{
112 d_quant_rel = NULL;
113 }
114
115 d_qcf = NULL;
116 d_sg_gen = NULL;
117 d_inst_engine = NULL;
118 d_i_cbqi = NULL;
119 d_qsplit = NULL;
120 d_anti_skolem = NULL;
121 d_model_engine = NULL;
122 d_bint = NULL;
123 d_rr_engine = NULL;
124 d_ceg_inst = NULL;
125 d_lte_part_inst = NULL;
126 d_alpha_equiv = NULL;
127 d_fun_def_engine = NULL;
128 d_uee = NULL;
129 d_fs = NULL;
130 d_rel_dom = NULL;
131 d_builder = NULL;
132
133 d_total_inst_count_debug = 0;
134 //allow theory combination to go first, once initially
135 d_ierCounter = options::instWhenTcFirst() ? 0 : 1;
136 d_ierCounter_c = d_ierCounter;
137 d_ierCounter_lc = 0;
138 d_ierCounterLastLc = 0;
139 d_inst_when_phase = 1 + ( options::instWhenPhase()<1 ? 1 : options::instWhenPhase() );
140 }
141
142 QuantifiersEngine::~QuantifiersEngine(){
143 for(std::map< Node, inst::CDInstMatchTrie* >::iterator
144 i = d_c_inst_match_trie.begin(), iend = d_c_inst_match_trie.end();
145 i != iend; ++i)
146 {
147 delete (*i).second;
148 }
149 d_c_inst_match_trie.clear();
150
151 delete d_alpha_equiv;
152 delete d_builder;
153 delete d_rr_engine;
154 delete d_bint;
155 delete d_model_engine;
156 delete d_inst_engine;
157 delete d_qcf;
158 delete d_quant_rel;
159 delete d_rel_dom;
160 delete d_model;
161 delete d_tr_trie;
162 delete d_term_db;
163 delete d_eq_query;
164 delete d_sg_gen;
165 delete d_ceg_inst;
166 delete d_lte_part_inst;
167 delete d_fun_def_engine;
168 delete d_uee;
169 delete d_fs;
170 delete d_i_cbqi;
171 delete d_qsplit;
172 delete d_anti_skolem;
173 delete d_inst_prop;
174 }
175
176 EqualityQueryQuantifiersEngine* QuantifiersEngine::getEqualityQuery() {
177 return d_eq_query;
178 }
179
180 context::Context* QuantifiersEngine::getSatContext(){
181 return d_te->theoryOf( THEORY_QUANTIFIERS )->getSatContext();
182 }
183
184 context::UserContext* QuantifiersEngine::getUserContext(){
185 return d_te->theoryOf( THEORY_QUANTIFIERS )->getUserContext();
186 }
187
188 OutputChannel& QuantifiersEngine::getOutputChannel(){
189 return d_te->theoryOf( THEORY_QUANTIFIERS )->getOutputChannel();
190 }
191 /** get default valuation for the quantifiers engine */
192 Valuation& QuantifiersEngine::getValuation(){
193 return d_te->theoryOf( THEORY_QUANTIFIERS )->getValuation();
194 }
195
196 void QuantifiersEngine::finishInit(){
197 context::Context * c = getSatContext();
198 Trace("quant-engine-debug") << "QuantifiersEngine : finishInit " << std::endl;
199 bool needsBuilder = false;
200 bool needsRelDom = false;
201 //add quantifiers modules
202 if( options::quantConflictFind() || options::quantRewriteRules() ){
203 d_qcf = new quantifiers::QuantConflictFind( this, c);
204 d_modules.push_back( d_qcf );
205 }
206 if( options::conjectureGen() ){
207 d_sg_gen = new quantifiers::ConjectureGenerator( this, c );
208 d_modules.push_back( d_sg_gen );
209 }
210 //maintain invariant : either InstantiationEngine or ModelEngine must be in d_modules
211 if( !options::finiteModelFind() || options::fmfInstEngine() ){
212 d_inst_engine = new quantifiers::InstantiationEngine( this );
213 d_modules.push_back( d_inst_engine );
214 }
215 if( options::cbqi() ){
216 if( options::cbqiSplx() ){
217 d_i_cbqi = new quantifiers::InstStrategySimplex( (arith::TheoryArith*)getTheoryEngine()->theoryOf( THEORY_ARITH ), this );
218 d_modules.push_back( d_i_cbqi );
219 }else{
220 d_i_cbqi = new quantifiers::InstStrategyCegqi( this );
221 d_modules.push_back( d_i_cbqi );
222 if( options::cbqiModel() ){
223 needsBuilder = true;
224 }
225 }
226 }
227 //finite model finding
228 if( options::finiteModelFind() ){
229 if( options::fmfBoundInt() ){
230 d_bint = new quantifiers::BoundedIntegers( c, this );
231 d_modules.push_back( d_bint );
232 }
233 d_model_engine = new quantifiers::ModelEngine( c, this );
234 d_modules.push_back( d_model_engine );
235 needsBuilder = true;
236 }
237 if( options::quantRewriteRules() ){
238 d_rr_engine = new quantifiers::RewriteEngine( c, this );
239 d_modules.push_back(d_rr_engine);
240 }
241 if( options::ceGuidedInst() ){
242 d_ceg_inst = new quantifiers::CegInstantiation( this, c );
243 d_modules.push_back( d_ceg_inst );
244 needsBuilder = true;
245 }
246 if( options::ltePartialInst() ){
247 d_lte_part_inst = new quantifiers::LtePartialInst( this, c );
248 d_modules.push_back( d_lte_part_inst );
249 }
250 if( ( options::finiteModelFind() && options::quantDynamicSplit()!=quantifiers::QUANT_DSPLIT_MODE_NONE ) ||
251 options::quantDynamicSplit()==quantifiers::QUANT_DSPLIT_MODE_AGG ){
252 d_qsplit = new quantifiers::QuantDSplit( this, c );
253 d_modules.push_back( d_qsplit );
254 }
255 if( options::quantAntiSkolem() ){
256 d_anti_skolem = new quantifiers::QuantAntiSkolem( this );
257 d_modules.push_back( d_anti_skolem );
258 }
259 if( options::quantAlphaEquiv() ){
260 d_alpha_equiv = new quantifiers::AlphaEquivalence( this );
261 }
262 //if( options::funDefs() ){
263 // d_fun_def_engine = new quantifiers::FunDefEngine( this, c );
264 // d_modules.push_back( d_fun_def_engine );
265 //}
266 if( options::quantEqualityEngine() ){
267 d_uee = new quantifiers::QuantEqualityEngine( this, c );
268 d_modules.push_back( d_uee );
269 }
270 //full saturation : instantiate from relevant domain, then arbitrary terms
271 if( options::fullSaturateQuant() || options::fullSaturateInst() ){
272 d_fs = new quantifiers::FullSaturation( this );
273 d_modules.push_back( d_fs );
274 needsRelDom = true;
275 }
276
277 if( needsRelDom ){
278 d_rel_dom = new quantifiers::RelevantDomain( this, d_model );
279 d_util.push_back( d_rel_dom );
280 }
281
282 if( needsBuilder ){
283 Trace("quant-engine-debug") << "Initialize model engine, mbqi : " << options::mbqiMode() << " " << options::fmfBoundInt() << std::endl;
284 if( options::mbqiMode()==quantifiers::MBQI_FMC || options::mbqiMode()==quantifiers::MBQI_FMC_INTERVAL ||
285 options::mbqiMode()==quantifiers::MBQI_TRUST || options::fmfBoundInt() ){
286 Trace("quant-engine-debug") << "...make fmc builder." << std::endl;
287 d_builder = new quantifiers::fmcheck::FullModelChecker( c, this );
288 }else if( options::mbqiMode()==quantifiers::MBQI_ABS ){
289 Trace("quant-engine-debug") << "...make abs mbqi builder." << std::endl;
290 d_builder = new quantifiers::AbsMbqiBuilder( c, this );
291 }else{
292 Trace("quant-engine-debug") << "...make default model builder." << std::endl;
293 d_builder = new quantifiers::QModelBuilderDefault( c, this );
294 }
295 }
296
297 }
298
299 QuantifiersModule * QuantifiersEngine::getOwner( Node q ) {
300 std::map< Node, QuantifiersModule * >::iterator it = d_owner.find( q );
301 if( it==d_owner.end() ){
302 return NULL;
303 }else{
304 return it->second;
305 }
306 }
307
308 void QuantifiersEngine::setOwner( Node q, QuantifiersModule * m, int priority ) {
309 QuantifiersModule * mo = getOwner( q );
310 if( mo!=m ){
311 if( mo!=NULL ){
312 if( priority<=d_owner_priority[q] ){
313 Trace("quant-warn") << "WARNING: setting owner of " << q << " to " << ( m ? m->identify() : "null" ) << ", but already has owner " << mo->identify() << " with higher priority!" << std::endl;
314 return;
315 }
316 }
317 d_owner[q] = m;
318 d_owner_priority[q] = priority;
319 }
320 }
321
322 bool QuantifiersEngine::hasOwnership( Node q, QuantifiersModule * m ) {
323 QuantifiersModule * mo = getOwner( q );
324 return mo==m || mo==NULL;
325 }
326
327 void QuantifiersEngine::presolve() {
328 Trace("quant-engine-proc") << "QuantifiersEngine : presolve " << std::endl;
329 for( unsigned i=0; i<d_modules.size(); i++ ){
330 d_modules[i]->presolve();
331 }
332 d_term_db->presolve();
333 d_presolve = false;
334 //add all terms to database
335 if( options::incrementalSolving() ){
336 Trace("quant-engine-proc") << "Add presolve cache " << d_presolve_cache.size() << std::endl;
337 for( unsigned i=0; i<d_presolve_cache.size(); i++ ){
338 addTermToDatabase( d_presolve_cache[i], d_presolve_cache_wq[i], d_presolve_cache_wic[i] );
339 }
340 Trace("quant-engine-proc") << "Done add presolve cache " << std::endl;
341 }
342 }
343
344 void QuantifiersEngine::check( Theory::Effort e ){
345 CodeTimer codeTimer(d_statistics.d_time);
346 if( !getMasterEqualityEngine()->consistent() ){
347 Trace("quant-engine-debug") << "Master equality engine not consistent, return." << std::endl;
348 return;
349 }
350 bool needsCheck = !d_lemmas_waiting.empty();
351 unsigned needsModelE = QEFFORT_NONE;
352 std::vector< QuantifiersModule* > qm;
353 if( d_model->checkNeeded() ){
354 needsCheck = needsCheck || e>=Theory::EFFORT_LAST_CALL; //always need to check at or above last call
355 for( int i=0; i<(int)d_modules.size(); i++ ){
356 if( d_modules[i]->needsCheck( e ) ){
357 qm.push_back( d_modules[i] );
358 needsCheck = true;
359 //can only request model at last call since theory combination can find inconsistencies
360 if( e>=Theory::EFFORT_LAST_CALL ){
361 unsigned me = d_modules[i]->needsModel( e );
362 needsModelE = me<needsModelE ? me : needsModelE;
363 }
364 }
365 }
366 }
367
368 d_conflict = false;
369 d_num_added_lemmas_round = 0;
370 d_hasAddedLemma = false;
371 bool setIncomplete = false;
372 if( e==Theory::EFFORT_LAST_CALL ){
373 //sources of incompleteness
374 if( d_lte_part_inst && d_lte_part_inst->wasInvoked() ){
375 Trace("quant-engine-debug") << "Set incomplete due to LTE partial instantiation." << std::endl;
376 setIncomplete = true;
377 }
378 }
379 bool usedModelBuilder = false;
380
381 Trace("quant-engine-debug2") << "Quantifiers Engine call to check, level = " << e << ", needsCheck=" << needsCheck << std::endl;
382 if( needsCheck ){
383 //flush previous lemmas (for instance, if was interupted), or other lemmas to process
384 flushLemmas();
385 if( d_hasAddedLemma ){
386 return;
387 }
388
389 if( Trace.isOn("quant-engine-debug") ){
390 Trace("quant-engine-debug") << "Quantifiers Engine check, level = " << e << std::endl;
391 Trace("quant-engine-debug") << " depth : " << d_ierCounter_c << std::endl;
392 Trace("quant-engine-debug") << " modules to check : ";
393 for( unsigned i=0; i<qm.size(); i++ ){
394 Trace("quant-engine-debug") << qm[i]->identify() << " ";
395 }
396 Trace("quant-engine-debug") << std::endl;
397 Trace("quant-engine-debug") << " # quantified formulas = " << d_model->getNumAssertedQuantifiers() << std::endl;
398 if( !d_lemmas_waiting.empty() ){
399 Trace("quant-engine-debug") << " lemmas waiting = " << d_lemmas_waiting.size() << std::endl;
400 }
401 Trace("quant-engine-debug") << " Theory engine finished : " << !d_te->needCheck() << std::endl;
402 Trace("quant-engine-debug") << " Needs model effort : " << needsModelE << std::endl;
403 }
404 if( Trace.isOn("quant-engine-ee-pre") ){
405 Trace("quant-engine-ee-pre") << "Equality engine (pre-inference): " << std::endl;
406 debugPrintEqualityEngine( "quant-engine-ee-pre" );
407 }
408 if( Trace.isOn("quant-engine-assert") ){
409 Trace("quant-engine-assert") << "Assertions : " << std::endl;
410 getTheoryEngine()->printAssertions("quant-engine-assert");
411 }
412
413 //reset utilities
414 Trace("quant-engine-debug") << "Resetting all utilities..." << std::endl;
415 for( unsigned i=0; i<d_util.size(); i++ ){
416 Trace("quant-engine-debug2") << "Reset " << d_util[i]->identify().c_str() << "..." << std::endl;
417 if( !d_util[i]->reset( e ) ){
418 flushLemmas();
419 if( d_hasAddedLemma ){
420 return;
421 }else{
422 //should only fail reset if added a lemma
423 Assert( false );
424 }
425 }
426 }
427
428 if( Trace.isOn("quant-engine-ee") ){
429 Trace("quant-engine-ee") << "Equality engine : " << std::endl;
430 debugPrintEqualityEngine( "quant-engine-ee" );
431 }
432
433 //reset the model
434 Trace("quant-engine-debug") << "Reset model..." << std::endl;
435 d_model->reset_round();
436
437 //reset the modules
438 Trace("quant-engine-debug") << "Resetting all modules..." << std::endl;
439 for( unsigned i=0; i<d_modules.size(); i++ ){
440 Trace("quant-engine-debug2") << "Reset " << d_modules[i]->identify().c_str() << std::endl;
441 d_modules[i]->reset_round( e );
442 }
443 Trace("quant-engine-debug") << "Done resetting all modules." << std::endl;
444 //reset may have added lemmas
445 flushLemmas();
446 if( d_hasAddedLemma ){
447 return;
448
449 }
450
451 if( e==Theory::EFFORT_LAST_CALL ){
452 //if effort is last call, try to minimize model first
453 //uf::StrongSolverTheoryUF * ufss = ((uf::TheoryUF*)getTheoryEngine()->theoryOf( THEORY_UF ))->getStrongSolver();
454 //if( ufss && !ufss->minimize() ){ return; }
455 ++(d_statistics.d_instantiation_rounds_lc);
456 }else if( e==Theory::EFFORT_FULL ){
457 ++(d_statistics.d_instantiation_rounds);
458 }
459 Trace("quant-engine-debug") << "Check modules that needed check..." << std::endl;
460 for( unsigned quant_e = QEFFORT_CONFLICT; quant_e<=QEFFORT_LAST_CALL; quant_e++ ){
461 d_curr_effort_level = quant_e;
462 bool success = true;
463 //build the model if any module requested it
464 if( needsModelE==quant_e ){
465 Assert( d_builder!=NULL );
466 Trace("quant-engine-debug") << "Build model..." << std::endl;
467 usedModelBuilder = true;
468 d_builder->d_addedLemmas = 0;
469 d_builder->buildModel( d_model, false );
470 //we are done if model building was unsuccessful
471 if( d_builder->d_addedLemmas>0 ){
472 success = false;
473 }
474 }
475 if( success ){
476 //check each module
477 for( unsigned i=0; i<qm.size(); i++ ){
478 Trace("quant-engine-debug") << "Check " << qm[i]->identify().c_str() << " at effort " << quant_e << "..." << std::endl;
479 qm[i]->check( e, quant_e );
480 if( d_conflict ){
481 Trace("quant-engine-debug") << "...conflict!" << std::endl;
482 break;
483 }
484 }
485 }
486 //flush all current lemmas
487 flushLemmas();
488 //if we have added one, stop
489 if( d_hasAddedLemma ){
490 break;
491 }else{
492 Assert( !d_conflict );
493 if( quant_e==QEFFORT_CONFLICT ){
494 if( e==Theory::EFFORT_FULL ){
495 //increment if a last call happened, we are not strictly enforcing interleaving, or already were in phase
496 if( d_ierCounterLastLc!=d_ierCounter_lc || !options::instWhenStrictInterleave() || d_ierCounter%d_inst_when_phase!=0 ){
497 d_ierCounter = d_ierCounter + 1;
498 d_ierCounterLastLc = d_ierCounter_lc;
499 d_ierCounter_c = d_ierCounter_c.get() + 1;
500 }
501 }else if( e==Theory::EFFORT_LAST_CALL ){
502 d_ierCounter_lc = d_ierCounter_lc + 1;
503 }
504 }else if( quant_e==QEFFORT_MODEL ){
505 if( e==Theory::EFFORT_LAST_CALL ){
506 //if we have a chance not to set incomplete
507 if( !setIncomplete ){
508 setIncomplete = false;
509 //check if we should set the incomplete flag
510 for( unsigned i=0; i<qm.size(); i++ ){
511 if( !qm[i]->checkComplete() ){
512 Trace("quant-engine-debug") << "Set incomplete because " << qm[i]->identify().c_str() << " was incomplete." << std::endl;
513 setIncomplete = true;
514 break;
515 }
516 }
517 }
518 //if setIncomplete = false, we will answer SAT, otherwise we will run at quant_e QEFFORT_LAST_CALL
519 if( !setIncomplete ){
520 break;
521 }
522 }
523 }
524 }
525 }
526 d_curr_effort_level = QEFFORT_NONE;
527 Trace("quant-engine-debug") << "Done check modules that needed check." << std::endl;
528 if( d_hasAddedLemma ){
529 //debug information
530 if( Trace.isOn("inst-per-quant-round") ){
531 for( std::map< Node, int >::iterator it = d_temp_inst_debug.begin(); it != d_temp_inst_debug.end(); ++it ){
532 Trace("inst-per-quant-round") << " * " << it->second << " for " << it->first << std::endl;
533 d_temp_inst_debug[it->first] = 0;
534 }
535 }
536 }
537 Trace("quant-engine-debug2") << "Finished quantifiers engine check." << std::endl;
538 }else{
539 Trace("quant-engine-debug2") << "Quantifiers Engine does not need check." << std::endl;
540 }
541
542 //SAT case
543 if( e==Theory::EFFORT_LAST_CALL && !d_hasAddedLemma ){
544 if( options::produceModels() ){
545 if( usedModelBuilder ){
546 Trace("quant-engine-debug") << "Build completed model..." << std::endl;
547 d_builder->buildModel( d_model, true );
548 }else if( !d_model->isModelSet() ){
549 //use default model builder when no module built the model
550 Trace("quant-engine-debug") << "Build the model..." << std::endl;
551 d_te->getModelBuilder()->buildModel( d_model, true );
552 Trace("quant-engine-debug") << "Done building the model." << std::endl;
553 }
554 }
555 if( setIncomplete ){
556 Trace("quant-engine-debug") << "Set incomplete flag." << std::endl;
557 getOutputChannel().setIncomplete();
558 }
559 //output debug stats
560 if( Trace.isOn("inst-per-quant") ){
561 for( std::map< Node, int >::iterator it = d_total_inst_debug.begin(); it != d_total_inst_debug.end(); ++it ){
562 Trace("inst-per-quant") << " * " << it->second << " for " << it->first << std::endl;
563 }
564 }
565 }
566 }
567
568 void QuantifiersEngine::notifyCombineTheories() {
569 //if allowing theory combination to happen at most once between instantiation rounds
570 //d_ierCounter = 1;
571 //d_ierCounterLastLc = -1;
572 }
573
574 bool QuantifiersEngine::reduceQuantifier( Node q ) {
575 std::map< Node, bool >::iterator it = d_quants_red.find( q );
576 if( it==d_quants_red.end() ){
577 if( d_alpha_equiv ){
578 Trace("quant-engine-red") << "Alpha equivalence " << q << "?" << std::endl;
579 //add equivalence with another quantified formula
580 if( d_alpha_equiv->reduceQuantifier( q ) ){
581 Trace("quant-engine-red") << "...alpha equivalence success." << std::endl;
582 ++(d_statistics.d_red_alpha_equiv);
583 d_quants_red[q] = true;
584 return true;
585 }
586 }
587 d_quants_red[q] = false;
588 return false;
589 }else{
590 return (*it).second;
591 }
592 }
593
594 bool QuantifiersEngine::registerQuantifier( Node f ){
595 std::map< Node, bool >::iterator it = d_quants.find( f );
596 if( it==d_quants.end() ){
597 Trace("quant") << "QuantifiersEngine : Register quantifier ";
598 Trace("quant") << " : " << f << std::endl;
599 ++(d_statistics.d_num_quant);
600 Assert( f.getKind()==FORALL );
601
602 //check whether we should apply a reduction
603 if( reduceQuantifier( f ) ){
604 d_quants[f] = false;
605 return false;
606 }else{
607 //make instantiation constants for f
608 d_term_db->makeInstantiationConstantsFor( f );
609 d_term_db->computeAttributes( f );
610 for( unsigned i=0; i<d_modules.size(); i++ ){
611 d_modules[i]->preRegisterQuantifier( f );
612 }
613 QuantifiersModule * qm = getOwner( f );
614 if( qm!=NULL ){
615 Trace("quant") << " Owner : " << qm->identify() << std::endl;
616 }
617 //register with quantifier relevance
618 if( d_quant_rel ){
619 d_quant_rel->registerQuantifier( f );
620 }
621 //register with each module
622 for( unsigned i=0; i<d_modules.size(); i++ ){
623 d_modules[i]->registerQuantifier( f );
624 }
625 Node ceBody = d_term_db->getInstConstantBody( f );
626 //generate the phase requirements
627 //d_phase_reqs[f] = new QuantPhaseReq( ceBody, true );
628 //also register it with the strong solver
629 //if( options::finiteModelFind() ){
630 // ((uf::TheoryUF*)d_te->theoryOf( THEORY_UF ))->getStrongSolver()->registerQuantifier( f );
631 //}
632 d_quants[f] = true;
633 return true;
634 }
635 }else{
636 return (*it).second;
637 }
638 }
639
640 void QuantifiersEngine::registerPattern( std::vector<Node> & pattern) {
641 for(std::vector<Node>::iterator p = pattern.begin(); p != pattern.end(); ++p){
642 std::set< Node > added;
643 getTermDatabase()->addTerm( *p, added );
644 }
645 }
646
647 void QuantifiersEngine::assertQuantifier( Node f, bool pol ){
648 if( !pol ){
649 //if not reduced
650 if( !reduceQuantifier( f ) ){
651 //do skolemization
652 if( d_skolemized.find( f )==d_skolemized.end() ){
653 Node body = d_term_db->getSkolemizedBody( f );
654 NodeBuilder<> nb(kind::OR);
655 nb << f << body.notNode();
656 Node lem = nb;
657 if( Trace.isOn("quantifiers-sk-debug") ){
658 Node slem = Rewriter::rewrite( lem );
659 Trace("quantifiers-sk-debug") << "Skolemize lemma : " << slem << std::endl;
660 }
661 getOutputChannel().lemma( lem, false, true );
662 d_skolemized[f] = true;
663 }
664 }
665 }else{
666 //assert to modules TODO : also for !pol?
667 //register the quantifier, assert it to each module
668 if( registerQuantifier( f ) ){
669 d_model->assertQuantifier( f );
670 for( int i=0; i<(int)d_modules.size(); i++ ){
671 d_modules[i]->assertNode( f );
672 }
673 addTermToDatabase( d_term_db->getInstConstantBody( f ), true );
674 }
675 }
676 }
677
678 void QuantifiersEngine::propagate( Theory::Effort level ){
679 CodeTimer codeTimer(d_statistics.d_time);
680
681 for( int i=0; i<(int)d_modules.size(); i++ ){
682 d_modules[i]->propagate( level );
683 }
684 }
685
686 Node QuantifiersEngine::getNextDecisionRequest(){
687 for( int i=0; i<(int)d_modules.size(); i++ ){
688 Node n = d_modules[i]->getNextDecisionRequest();
689 if( !n.isNull() ){
690 return n;
691 }
692 }
693 return Node::null();
694 }
695
696 quantifiers::TermDbSygus* QuantifiersEngine::getTermDatabaseSygus() {
697 return getTermDatabase()->getTermDatabaseSygus();
698 }
699
700 void QuantifiersEngine::addTermToDatabase( Node n, bool withinQuant, bool withinInstClosure ){
701 if( options::incrementalSolving() ){
702 if( d_presolve_in.find( n )==d_presolve_in.end() ){
703 d_presolve_in.insert( n );
704 d_presolve_cache.push_back( n );
705 d_presolve_cache_wq.push_back( withinQuant );
706 d_presolve_cache_wic.push_back( withinInstClosure );
707 }
708 }
709 //only wait if we are doing incremental solving
710 if( !d_presolve || !options::incrementalSolving() ){
711 std::set< Node > added;
712 getTermDatabase()->addTerm( n, added, withinQuant, withinInstClosure );
713 //maybe have triggered instantiations if we are doing eager instantiation
714 if( options::eagerInstQuant() ){
715 flushLemmas();
716 }
717 //added contains also the Node that just have been asserted in this branch
718 if( d_quant_rel ){
719 for( std::set< Node >::iterator i=added.begin(), end=added.end(); i!=end; i++ ){
720 if( !withinQuant ){
721 d_quant_rel->setRelevance( i->getOperator(), 0 );
722 }
723 }
724 }
725 }
726 }
727
728 void QuantifiersEngine::eqNotifyNewClass(TNode t) {
729 addTermToDatabase( t );
730 if( d_eq_query->getEqualityInference() ){
731 d_eq_query->getEqualityInference()->eqNotifyNewClass( t );
732 }
733 }
734
735 void QuantifiersEngine::eqNotifyPreMerge(TNode t1, TNode t2) {
736 if( d_eq_query->getEqualityInference() ){
737 d_eq_query->getEqualityInference()->eqNotifyMerge( t1, t2 );
738 }
739 }
740
741 void QuantifiersEngine::eqNotifyPostMerge(TNode t1, TNode t2) {
742
743 }
744
745 void QuantifiersEngine::eqNotifyDisequal(TNode t1, TNode t2, TNode reason) {
746 //if( d_qcf ){
747 // d_qcf->assertDisequal( t1, t2 );
748 //}
749 }
750
751 void QuantifiersEngine::computeTermVector( Node f, InstMatch& m, std::vector< Node >& vars, std::vector< Node >& terms ){
752 for( size_t i=0; i<f[0].getNumChildren(); i++ ){
753 Node n = m.get( i );
754 if( !n.isNull() ){
755 vars.push_back( f[0][i] );
756 terms.push_back( n );
757 }
758 }
759 }
760
761
762 bool QuantifiersEngine::recordInstantiationInternal( Node q, std::vector< Node >& terms, bool modEq, bool modInst, bool addedLem ) {
763 if( !addedLem ){
764 //record the instantiation for deletion later
765 //TODO
766 }
767 if( options::incrementalSolving() ){
768 Trace("inst-add-debug") << "Adding into context-dependent inst trie, modEq = " << modEq << ", modInst = " << modInst << std::endl;
769 inst::CDInstMatchTrie* imt;
770 std::map< Node, inst::CDInstMatchTrie* >::iterator it = d_c_inst_match_trie.find( q );
771 if( it!=d_c_inst_match_trie.end() ){
772 imt = it->second;
773 }else{
774 imt = new CDInstMatchTrie( getUserContext() );
775 d_c_inst_match_trie[q] = imt;
776 }
777 return imt->addInstMatch( this, q, terms, getUserContext(), modEq, modInst );
778 }else{
779 Trace("inst-add-debug") << "Adding into inst trie" << std::endl;
780 return d_inst_match_trie[q].addInstMatch( this, q, terms, modEq, modInst );
781 }
782 }
783
784 bool QuantifiersEngine::removeInstantiationInternal( Node q, std::vector< Node >& terms ) {
785 if( options::incrementalSolving() ){
786 std::map< Node, inst::CDInstMatchTrie* >::iterator it = d_c_inst_match_trie.find( q );
787 if( it!=d_c_inst_match_trie.end() ){
788 return it->second->removeInstMatch( this, q, terms );
789 }else{
790 return false;
791 }
792 }else{
793 return d_inst_match_trie[q].removeInstMatch( this, q, terms );
794 }
795 }
796
797 void QuantifiersEngine::setInstantiationLevelAttr( Node n, Node qn, uint64_t level ){
798 Trace("inst-level-debug2") << "IL : " << n << " " << qn << " " << level << std::endl;
799 //if not from the vector of terms we instantiatied
800 if( qn.getKind()!=BOUND_VARIABLE && n!=qn ){
801 //if this is a new term, without an instantiation level
802 if( !n.hasAttribute(InstLevelAttribute()) ){
803 InstLevelAttribute ila;
804 n.setAttribute(ila,level);
805 Trace("inst-level-debug") << "Set instantiation level " << n << " to " << level << std::endl;
806 }
807 Assert( n.getNumChildren()==qn.getNumChildren() );
808 for( unsigned i=0; i<n.getNumChildren(); i++ ){
809 setInstantiationLevelAttr( n[i], qn[i], level );
810 }
811 }
812 }
813
814 void QuantifiersEngine::setInstantiationLevelAttr( Node n, uint64_t level ){
815 if( !n.hasAttribute(InstLevelAttribute()) ){
816 InstLevelAttribute ila;
817 n.setAttribute(ila,level);
818 Trace("inst-level-debug") << "Set instantiation level " << n << " to " << level << std::endl;
819 }
820 for( unsigned i=0; i<n.getNumChildren(); i++ ){
821 setInstantiationLevelAttr( n[i], level );
822 }
823 }
824
825 Node QuantifiersEngine::getSubstitute( Node n, std::vector< Node >& terms ){
826 if( n.getKind()==INST_CONSTANT ){
827 Debug("check-inst") << "Substitute inst constant : " << n << std::endl;
828 return terms[n.getAttribute(InstVarNumAttribute())];
829 }else{
830 //if( !quantifiers::TermDb::hasInstConstAttr( n ) ){
831 //Debug("check-inst") << "No inst const attr : " << n << std::endl;
832 //return n;
833 //}else{
834 //Debug("check-inst") << "Recurse on : " << n << std::endl;
835 std::vector< Node > cc;
836 if( n.getMetaKind() == kind::metakind::PARAMETERIZED ){
837 cc.push_back( n.getOperator() );
838 }
839 bool changed = false;
840 for( unsigned i=0; i<n.getNumChildren(); i++ ){
841 Node c = getSubstitute( n[i], terms );
842 cc.push_back( c );
843 changed = changed || c!=n[i];
844 }
845 if( changed ){
846 Node ret = NodeManager::currentNM()->mkNode( n.getKind(), cc );
847 return ret;
848 }else{
849 return n;
850 }
851 }
852 }
853
854
855 Node QuantifiersEngine::getInstantiation( Node q, std::vector< Node >& vars, std::vector< Node >& terms, bool doVts ){
856 Node body;
857 //process partial instantiation if necessary
858 if( d_term_db->d_vars[q].size()!=vars.size() ){
859 body = q[ 1 ].substitute( vars.begin(), vars.end(), terms.begin(), terms.end() );
860 std::vector< Node > uninst_vars;
861 //doing a partial instantiation, must add quantifier for all uninstantiated variables
862 for( unsigned i=0; i<q[0].getNumChildren(); i++ ){
863 if( std::find( vars.begin(), vars.end(), q[0][i] )==vars.end() ){
864 uninst_vars.push_back( q[0][i] );
865 }
866 }
867 Node bvl = NodeManager::currentNM()->mkNode( BOUND_VAR_LIST, uninst_vars );
868 body = NodeManager::currentNM()->mkNode( FORALL, bvl, body );
869 Trace("partial-inst") << "Partial instantiation : " << q << std::endl;
870 Trace("partial-inst") << " : " << body << std::endl;
871 }else{
872 if( options::cbqi() ){
873 body = q[ 1 ].substitute( vars.begin(), vars.end(), terms.begin(), terms.end() );
874 }else{
875 //do optimized version
876 Node icb = d_term_db->getInstConstantBody( q );
877 body = getSubstitute( icb, terms );
878 if( Debug.isOn("check-inst") ){
879 Node body2 = q[ 1 ].substitute( vars.begin(), vars.end(), terms.begin(), terms.end() );
880 if( body!=body2 ){
881 Debug("check-inst") << "Substitution is wrong : " << body << " " << body2 << std::endl;
882 }
883 }
884 }
885 }
886 if( doVts ){
887 //do virtual term substitution
888 body = Rewriter::rewrite( body );
889 Trace("quant-vts-debug") << "Rewrite vts symbols in " << body << std::endl;
890 Node body_r = d_term_db->rewriteVtsSymbols( body );
891 Trace("quant-vts-debug") << " ...result: " << body_r << std::endl;
892 body = body_r;
893 }
894 return body;
895 }
896
897 Node QuantifiersEngine::getInstantiation( Node q, InstMatch& m, bool doVts ){
898 std::vector< Node > vars;
899 std::vector< Node > terms;
900 computeTermVector( q, m, vars, terms );
901 return getInstantiation( q, vars, terms, doVts );
902 }
903
904 Node QuantifiersEngine::getInstantiation( Node q, std::vector< Node >& terms, bool doVts ) {
905 return getInstantiation( q, d_term_db->d_vars[q], terms, doVts );
906 }
907
908 /*
909 bool QuantifiersEngine::existsInstantiation( Node f, InstMatch& m, bool modEq, bool modInst ){
910 if( options::incrementalSolving() ){
911 if( d_c_inst_match_trie.find( f )!=d_c_inst_match_trie.end() ){
912 if( d_c_inst_match_trie[f]->existsInstMatch( this, f, m, getUserContext(), modEq, modInst ) ){
913 return true;
914 }
915 }
916 }else{
917 if( d_inst_match_trie.find( f )!=d_inst_match_trie.end() ){
918 if( d_inst_match_trie[f].existsInstMatch( this, f, m, modEq, modInst ) ){
919 return true;
920 }
921 }
922 }
923 //also check model builder (it may contain instantiations internally)
924 if( d_builder && d_builder->existsInstantiation( f, m, modEq, modInst ) ){
925 return true;
926 }
927 return false;
928 }
929 */
930
931 bool QuantifiersEngine::addLemma( Node lem, bool doCache, bool doRewrite ){
932 if( doCache ){
933 if( doRewrite ){
934 lem = Rewriter::rewrite(lem);
935 }
936 Trace("inst-add-debug") << "Adding lemma : " << lem << std::endl;
937 BoolMap::const_iterator itp = d_lemmas_produced_c.find( lem );
938 if( itp==d_lemmas_produced_c.end() || !(*itp).second ){
939 //d_curr_out->lemma( lem, false, true );
940 d_lemmas_produced_c[ lem ] = true;
941 d_lemmas_waiting.push_back( lem );
942 Trace("inst-add-debug") << "Added lemma" << std::endl;
943 d_num_added_lemmas_round++;
944 return true;
945 }else{
946 Trace("inst-add-debug") << "Duplicate." << std::endl;
947 return false;
948 }
949 }else{
950 //do not need to rewrite, will be rewritten after sending
951 d_lemmas_waiting.push_back( lem );
952 d_num_added_lemmas_round++;
953 return true;
954 }
955 }
956
957 bool QuantifiersEngine::removeLemma( Node lem ) {
958 std::vector< Node >::iterator it = std::find( d_lemmas_waiting.begin(), d_lemmas_waiting.end(), lem );
959 if( it!=d_lemmas_waiting.end() ){
960 d_lemmas_waiting.erase( it, it + 1 );
961 d_lemmas_produced_c[ lem ] = false;
962 return true;
963 }else{
964 return false;
965 }
966 }
967
968 void QuantifiersEngine::addRequirePhase( Node lit, bool req ){
969 d_phase_req_waiting[lit] = req;
970 }
971
972 bool QuantifiersEngine::addInstantiation( Node q, InstMatch& m, bool mkRep, bool modEq, bool modInst, bool doVts ){
973 std::vector< Node > terms;
974 m.getTerms( q, terms );
975 return addInstantiation( q, terms, mkRep, modEq, modInst, doVts );
976 }
977
978 bool QuantifiersEngine::addInstantiation( Node q, std::vector< Node >& terms, bool mkRep, bool modEq, bool modInst, bool doVts ) {
979 // For resource-limiting (also does a time check).
980 getOutputChannel().safePoint(options::quantifierStep());
981 Assert( !d_conflict );
982 Assert( terms.size()==q[0].getNumChildren() );
983 Trace("inst-add-debug") << "For quantified formula " << q << ", add instantiation: " << std::endl;
984 for( unsigned i=0; i<terms.size(); i++ ){
985 Trace("inst-add-debug") << " " << q[0][i];
986 Trace("inst-add-debug2") << " -> " << terms[i];
987 if( terms[i].isNull() ){
988 terms[i] = d_term_db->getModelBasisTerm( q[0][i].getType() );
989 }
990 if( mkRep ){
991 //pick the best possible representative for instantiation, based on past use and simplicity of term
992 terms[i] = d_eq_query->getInternalRepresentative( terms[i], q, i );
993 }else{
994 //ensure the type is correct
995 terms[i] = quantifiers::TermDb::ensureType( terms[i], q[0][i].getType() );
996 }
997 Trace("inst-add-debug") << " -> " << terms[i] << std::endl;
998 if( terms[i].isNull() ){
999 Trace("inst-add-debug") << " --> Failed to make term vector, due to term/type restrictions." << std::endl;
1000 return false;
1001 }
1002 #ifdef CVC4_ASSERTIONS
1003 bool bad_inst = false;
1004 if( quantifiers::TermDb::containsUninterpretedConstant( terms[i] ) ){
1005 bad_inst = true;
1006 }else if( !terms[i].getType().isSubtypeOf( q[0][i].getType() ) ){
1007 bad_inst = true;
1008 }else if( options::cbqi() ){
1009 Node icf = quantifiers::TermDb::getInstConstAttr(terms[i]);
1010 if( !icf.isNull() ){
1011 if( icf==q ){
1012 bad_inst = true;
1013 }else{
1014 bad_inst = quantifiers::TermDb::containsTerms( terms[i], d_term_db->d_inst_constants[q] );
1015 }
1016 }
1017 }
1018 //this assertion is critical to soundness
1019 if( bad_inst ){
1020 Trace("inst")<< "***& Bad Instantiate " << q << " with " << std::endl;
1021 for( unsigned j=0; j<terms.size(); j++ ){
1022 Trace("inst") << " " << terms[j] << std::endl;
1023 }
1024 Assert( false );
1025 }
1026 #endif
1027 }
1028
1029 //check based on instantiation level
1030 if( options::instMaxLevel()!=-1 || options::lteRestrictInstClosure() ){
1031 for( unsigned i=0; i<terms.size(); i++ ){
1032 if( !d_term_db->isTermEligibleForInstantiation( terms[i], q, true ) ){
1033 return false;
1034 }
1035 }
1036 }
1037
1038 //check for positive entailment
1039 if( options::instNoEntail() ){
1040 //TODO: check consistency of equality engine (if not aborting on utility's reset)
1041 std::map< TNode, TNode > subs;
1042 for( unsigned i=0; i<terms.size(); i++ ){
1043 subs[q[0][i]] = terms[i];
1044 }
1045 if( d_term_db->isEntailed( q[1], subs, false, true ) ){
1046 Trace("inst-add-debug") << " --> Currently entailed." << std::endl;
1047 return false;
1048 }
1049 //Node eval = d_term_db->evaluateTerm( q[1], subs, false, true );
1050 //Trace("ajr-temp") << "Instantiation evaluates to : " << std::endl;
1051 //Trace("ajr-temp") << " " << eval << std::endl;
1052 }
1053
1054 //check for term vector duplication
1055 bool alreadyExists = !recordInstantiationInternal( q, terms, modEq, modInst );
1056 if( alreadyExists ){
1057 Trace("inst-add-debug") << " --> Already exists." << std::endl;
1058 ++(d_statistics.d_inst_duplicate_eq);
1059 return false;
1060 }
1061
1062 //construct the instantiation
1063 Trace("inst-add-debug") << "Constructing instantiation..." << std::endl;
1064 Assert( d_term_db->d_vars[q].size()==terms.size() );
1065 Node body = getInstantiation( q, d_term_db->d_vars[q], terms, doVts ); //do virtual term substitution
1066 body = quantifiers::QuantifiersRewriter::preprocess( body, true );
1067 Trace("inst-debug") << "...preprocess to " << body << std::endl;
1068
1069 //construct the lemma
1070 Trace("inst-assert") << "(assert " << body << ")" << std::endl;
1071 body = Rewriter::rewrite(body);
1072 Node lem = NodeManager::currentNM()->mkNode( kind::OR, q.negate(), body );
1073 lem = Rewriter::rewrite(lem);
1074
1075 //check for lemma duplication
1076 if( addLemma( lem, true, false ) ){
1077 d_total_inst_debug[q]++;
1078 d_temp_inst_debug[q]++;
1079 d_total_inst_count_debug++;
1080 if( Trace.isOn("inst") ){
1081 Trace("inst") << "*** Instantiate " << q << " with " << std::endl;
1082 for( unsigned i=0; i<terms.size(); i++ ){
1083 if( Trace.isOn("inst") ){
1084 Trace("inst") << " " << terms[i];
1085 if( Trace.isOn("inst-debug") ){
1086 Trace("inst-debug") << ", type=" << terms[i].getType() << ", var_type=" << q[0][i].getType();
1087 }
1088 Trace("inst") << std::endl;
1089 }
1090 }
1091 }
1092 if( options::instMaxLevel()!=-1 ){
1093 uint64_t maxInstLevel = 0;
1094 for( unsigned i=0; i<terms.size(); i++ ){
1095 if( terms[i].hasAttribute(InstLevelAttribute()) ){
1096 if( terms[i].getAttribute(InstLevelAttribute())>maxInstLevel ){
1097 maxInstLevel = terms[i].getAttribute(InstLevelAttribute());
1098 }
1099 }
1100 }
1101 setInstantiationLevelAttr( body, q[1], maxInstLevel+1 );
1102 }
1103 if( d_curr_effort_level>QEFFORT_CONFLICT && d_curr_effort_level<QEFFORT_NONE ){
1104 //notify listeners
1105 for( unsigned j=0; j<d_inst_notify.size(); j++ ){
1106 if( !d_inst_notify[j]->notifyInstantiation( d_curr_effort_level, q, lem, terms, body ) ){
1107 Trace("inst-add-debug") << "...we are in conflict." << std::endl;
1108 d_conflict = true;
1109 Assert( !d_lemmas_waiting.empty() );
1110 break;
1111 }
1112 }
1113 }
1114 Trace("inst-add-debug") << " --> Success." << std::endl;
1115 ++(d_statistics.d_instantiations);
1116 return true;
1117 }else{
1118 Trace("inst-add-debug") << " --> Lemma already exists." << std::endl;
1119 ++(d_statistics.d_inst_duplicate);
1120 return false;
1121 }
1122 }
1123
1124 bool QuantifiersEngine::removeInstantiation( Node q, Node lem, std::vector< Node >& terms ) {
1125 //lem must occur in d_waiting_lemmas
1126 if( removeLemma( lem ) ){
1127 return removeInstantiationInternal( q, terms );
1128 }else{
1129 return false;
1130 }
1131 }
1132
1133 bool QuantifiersEngine::addSplit( Node n, bool reqPhase, bool reqPhasePol ){
1134 n = Rewriter::rewrite( n );
1135 Node lem = NodeManager::currentNM()->mkNode( OR, n, n.notNode() );
1136 if( addLemma( lem ) ){
1137 Debug("inst") << "*** Add split " << n<< std::endl;
1138 return true;
1139 }
1140 return false;
1141 }
1142
1143 bool QuantifiersEngine::addSplitEquality( Node n1, Node n2, bool reqPhase, bool reqPhasePol ){
1144 //Assert( !areEqual( n1, n2 ) );
1145 //Assert( !areDisequal( n1, n2 ) );
1146 Kind knd = n1.getType()==NodeManager::currentNM()->booleanType() ? IFF : EQUAL;
1147 Node fm = NodeManager::currentNM()->mkNode( knd, n1, n2 );
1148 return addSplit( fm );
1149 }
1150
1151 void QuantifiersEngine::markRelevant( Node q ) {
1152 d_model->markRelevant( q );
1153 }
1154
1155 bool QuantifiersEngine::getInstWhenNeedsCheck( Theory::Effort e ) {
1156 Trace("quant-engine-debug2") << "Get inst when needs check, counts=" << d_ierCounter << ", " << d_ierCounter_lc << std::endl;
1157 //determine if we should perform check, based on instWhenMode
1158 bool performCheck = false;
1159 if( options::instWhenMode()==quantifiers::INST_WHEN_FULL ){
1160 performCheck = ( e >= Theory::EFFORT_FULL );
1161 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_DELAY ){
1162 performCheck = ( e >= Theory::EFFORT_FULL ) && !getTheoryEngine()->needCheck();
1163 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_LAST_CALL ){
1164 performCheck = ( ( e==Theory::EFFORT_FULL && d_ierCounter%d_inst_when_phase!=0 ) || e==Theory::EFFORT_LAST_CALL );
1165 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_DELAY_LAST_CALL ){
1166 performCheck = ( ( e==Theory::EFFORT_FULL && !getTheoryEngine()->needCheck() && d_ierCounter%d_inst_when_phase!=0 ) || e==Theory::EFFORT_LAST_CALL );
1167 }else if( options::instWhenMode()==quantifiers::INST_WHEN_LAST_CALL ){
1168 performCheck = ( e >= Theory::EFFORT_LAST_CALL );
1169 }else{
1170 performCheck = true;
1171 }
1172 if( e==Theory::EFFORT_LAST_CALL ){
1173 //with bounded integers, skip every other last call,
1174 // since matching loops may occur with infinite quantification
1175 if( d_ierCounter_lc%2==0 && options::fmfBoundInt() ){
1176 performCheck = false;
1177 }
1178 }
1179 return performCheck;
1180 }
1181
1182 quantifiers::UserPatMode QuantifiersEngine::getInstUserPatMode() {
1183 if( options::userPatternsQuant()==quantifiers::USER_PAT_MODE_INTERLEAVE ){
1184 return d_ierCounter%2==0 ? quantifiers::USER_PAT_MODE_USE : quantifiers::USER_PAT_MODE_RESORT;
1185 }else{
1186 return options::userPatternsQuant();
1187 }
1188 }
1189
1190 void QuantifiersEngine::flushLemmas(){
1191 if( !d_lemmas_waiting.empty() ){
1192 //take default output channel if none is provided
1193 d_hasAddedLemma = true;
1194 for( int i=0; i<(int)d_lemmas_waiting.size(); i++ ){
1195 Trace("qe-lemma") << "Lemma : " << d_lemmas_waiting[i] << std::endl;
1196 getOutputChannel().lemma( d_lemmas_waiting[i], false, true );
1197 }
1198 d_lemmas_waiting.clear();
1199 }
1200 if( !d_phase_req_waiting.empty() ){
1201 for( std::map< Node, bool >::iterator it = d_phase_req_waiting.begin(); it != d_phase_req_waiting.end(); ++it ){
1202 Trace("qe-lemma") << "Require phase : " << it->first << " -> " << it->second << std::endl;
1203 getOutputChannel().requirePhase( it->first, it->second );
1204 }
1205 d_phase_req_waiting.clear();
1206 }
1207 }
1208
1209 void QuantifiersEngine::printInstantiations( std::ostream& out ) {
1210 bool printed = false;
1211 for( BoolMap::iterator it = d_skolemized.begin(); it != d_skolemized.end(); ++it ){
1212 Node q = (*it).first;
1213 printed = true;
1214 out << "Skolem constants of " << q << " : " << std::endl;
1215 out << " ( ";
1216 for( unsigned i=0; i<d_term_db->d_skolem_constants[q].size(); i++ ){
1217 if( i>0 ){ out << ", "; }
1218 out << d_term_db->d_skolem_constants[q][i];
1219 }
1220 out << " )" << std::endl;
1221 out << std::endl;
1222 }
1223 if( options::incrementalSolving() ){
1224 for( std::map< Node, inst::CDInstMatchTrie* >::iterator it = d_c_inst_match_trie.begin(); it != d_c_inst_match_trie.end(); ++it ){
1225 printed = true;
1226 out << "Instantiations of " << it->first << " : " << std::endl;
1227 it->second->print( out, it->first );
1228 }
1229 }else{
1230 for( std::map< Node, inst::InstMatchTrie >::iterator it = d_inst_match_trie.begin(); it != d_inst_match_trie.end(); ++it ){
1231 printed = true;
1232 out << "Instantiations of " << it->first << " : " << std::endl;
1233 it->second.print( out, it->first );
1234 out << std::endl;
1235 }
1236 }
1237 if( !printed ){
1238 out << "No instantiations." << std::endl;
1239 }
1240 }
1241
1242 void QuantifiersEngine::printSynthSolution( std::ostream& out ) {
1243 if( d_ceg_inst ){
1244 d_ceg_inst->printSynthSolution( out );
1245 }else{
1246 out << "Internal error : module for synth solution not found." << std::endl;
1247 }
1248 }
1249
1250 void QuantifiersEngine::getInstantiations( std::map< Node, std::vector< Node > >& insts ) {
1251 if( options::incrementalSolving() ){
1252 for( std::map< Node, inst::CDInstMatchTrie* >::iterator it = d_c_inst_match_trie.begin(); it != d_c_inst_match_trie.end(); ++it ){
1253 it->second->getInstantiations( insts[it->first], it->first, this );
1254 }
1255 }else{
1256 for( std::map< Node, inst::InstMatchTrie >::iterator it = d_inst_match_trie.begin(); it != d_inst_match_trie.end(); ++it ){
1257 it->second.getInstantiations( insts[it->first], it->first, this );
1258 }
1259 }
1260 }
1261
1262 QuantifiersEngine::Statistics::Statistics()
1263 : d_time("theory::QuantifiersEngine::time"),
1264 d_num_quant("QuantifiersEngine::Num_Quantifiers", 0),
1265 d_instantiation_rounds("QuantifiersEngine::Rounds_Instantiation_Full", 0),
1266 d_instantiation_rounds_lc("QuantifiersEngine::Rounds_Instantiation_Last_Call", 0),
1267 d_instantiations("QuantifiersEngine::Instantiations_Total", 0),
1268 d_inst_duplicate("QuantifiersEngine::Duplicate_Inst", 0),
1269 d_inst_duplicate_eq("QuantifiersEngine::Duplicate_Inst_Eq", 0),
1270 d_triggers("QuantifiersEngine::Triggers", 0),
1271 d_simple_triggers("QuantifiersEngine::Triggers_Simple", 0),
1272 d_multi_triggers("QuantifiersEngine::Triggers_Multi", 0),
1273 d_multi_trigger_instantiations("QuantifiersEngine::Multi_Trigger_Instantiations", 0),
1274 d_red_alpha_equiv("QuantifiersEngine::Reductions_Alpha_Equivalence", 0),
1275 d_red_lte_partial_inst("QuantifiersEngine::Reductions_Lte_Partial_Inst", 0),
1276 d_instantiations_user_patterns("QuantifiersEngine::Instantiations_User_Patterns", 0),
1277 d_instantiations_auto_gen("QuantifiersEngine::Instantiations_Auto_Gen", 0),
1278 d_instantiations_guess("QuantifiersEngine::Instantiations_Guess", 0),
1279 d_instantiations_cbqi_arith("QuantifiersEngine::Instantiations_Cbqi_Arith", 0)
1280 {
1281 smtStatisticsRegistry()->registerStat(&d_time);
1282 smtStatisticsRegistry()->registerStat(&d_num_quant);
1283 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds);
1284 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds_lc);
1285 smtStatisticsRegistry()->registerStat(&d_instantiations);
1286 smtStatisticsRegistry()->registerStat(&d_inst_duplicate);
1287 smtStatisticsRegistry()->registerStat(&d_inst_duplicate_eq);
1288 smtStatisticsRegistry()->registerStat(&d_triggers);
1289 smtStatisticsRegistry()->registerStat(&d_simple_triggers);
1290 smtStatisticsRegistry()->registerStat(&d_multi_triggers);
1291 smtStatisticsRegistry()->registerStat(&d_multi_trigger_instantiations);
1292 smtStatisticsRegistry()->registerStat(&d_red_alpha_equiv);
1293 smtStatisticsRegistry()->registerStat(&d_red_lte_partial_inst);
1294 smtStatisticsRegistry()->registerStat(&d_instantiations_user_patterns);
1295 smtStatisticsRegistry()->registerStat(&d_instantiations_auto_gen);
1296 smtStatisticsRegistry()->registerStat(&d_instantiations_guess);
1297 smtStatisticsRegistry()->registerStat(&d_instantiations_cbqi_arith);
1298 }
1299
1300 QuantifiersEngine::Statistics::~Statistics(){
1301 smtStatisticsRegistry()->unregisterStat(&d_time);
1302 smtStatisticsRegistry()->unregisterStat(&d_num_quant);
1303 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds);
1304 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds_lc);
1305 smtStatisticsRegistry()->unregisterStat(&d_instantiations);
1306 smtStatisticsRegistry()->unregisterStat(&d_inst_duplicate);
1307 smtStatisticsRegistry()->unregisterStat(&d_inst_duplicate_eq);
1308 smtStatisticsRegistry()->unregisterStat(&d_triggers);
1309 smtStatisticsRegistry()->unregisterStat(&d_simple_triggers);
1310 smtStatisticsRegistry()->unregisterStat(&d_multi_triggers);
1311 smtStatisticsRegistry()->unregisterStat(&d_multi_trigger_instantiations);
1312 smtStatisticsRegistry()->unregisterStat(&d_red_alpha_equiv);
1313 smtStatisticsRegistry()->unregisterStat(&d_red_lte_partial_inst);
1314 smtStatisticsRegistry()->unregisterStat(&d_instantiations_user_patterns);
1315 smtStatisticsRegistry()->unregisterStat(&d_instantiations_auto_gen);
1316 smtStatisticsRegistry()->unregisterStat(&d_instantiations_guess);
1317 smtStatisticsRegistry()->unregisterStat(&d_instantiations_cbqi_arith);
1318 }
1319
1320 eq::EqualityEngine* QuantifiersEngine::getMasterEqualityEngine(){
1321 return getTheoryEngine()->getMasterEqualityEngine();
1322 }
1323
1324 void QuantifiersEngine::debugPrintEqualityEngine( const char * c ) {
1325 eq::EqualityEngine* ee = getMasterEqualityEngine();
1326 eq::EqClassesIterator eqcs_i = eq::EqClassesIterator( ee );
1327 std::map< TypeNode, int > typ_num;
1328 while( !eqcs_i.isFinished() ){
1329 TNode r = (*eqcs_i);
1330 TypeNode tr = r.getType();
1331 if( typ_num.find( tr )==typ_num.end() ){
1332 typ_num[tr] = 0;
1333 }
1334 typ_num[tr]++;
1335 bool firstTime = true;
1336 Trace(c) << " " << r;
1337 Trace(c) << " : { ";
1338 eq::EqClassIterator eqc_i = eq::EqClassIterator( r, ee );
1339 while( !eqc_i.isFinished() ){
1340 TNode n = (*eqc_i);
1341 if( r!=n ){
1342 if( firstTime ){
1343 Trace(c) << std::endl;
1344 firstTime = false;
1345 }
1346 Trace(c) << " " << n << std::endl;
1347 }
1348 ++eqc_i;
1349 }
1350 if( !firstTime ){ Trace(c) << " "; }
1351 Trace(c) << "}" << std::endl;
1352 ++eqcs_i;
1353 }
1354 Trace(c) << std::endl;
1355 for( std::map< TypeNode, int >::iterator it = typ_num.begin(); it != typ_num.end(); ++it ){
1356 Trace(c) << "# eqc for " << it->first << " : " << it->second << std::endl;
1357 }
1358 }
1359
1360
1361 EqualityQueryQuantifiersEngine::EqualityQueryQuantifiersEngine( context::Context* c, QuantifiersEngine* qe ) : d_qe( qe ), d_eqi_counter( c ), d_reset_count( 0 ){
1362 if( options::inferArithTriggerEq() ){
1363 d_eq_inference = new quantifiers::EqualityInference( c, options::inferArithTriggerEqExp() );
1364 }else{
1365 d_eq_inference = NULL;
1366 }
1367 }
1368
1369 EqualityQueryQuantifiersEngine::~EqualityQueryQuantifiersEngine(){
1370 delete d_eq_inference;
1371 }
1372
1373 bool EqualityQueryQuantifiersEngine::reset( Theory::Effort e ){
1374 d_int_rep.clear();
1375 d_reset_count++;
1376 return processInferences( e );
1377 }
1378
1379 bool EqualityQueryQuantifiersEngine::processInferences( Theory::Effort e ) {
1380 if( options::inferArithTriggerEq() ){
1381 eq::EqualityEngine* ee = getEngine();
1382 //updated implementation
1383 while( d_eqi_counter.get()<d_eq_inference->getNumPendingMerges() ){
1384 Node eq = d_eq_inference->getPendingMerge( d_eqi_counter.get() );
1385 Node eq_exp = d_eq_inference->getPendingMergeExplanation( d_eqi_counter.get() );
1386 Trace("quant-engine-ee-proc") << "processInferences : Infer : " << eq << std::endl;
1387 Trace("quant-engine-ee-proc") << " explanation : " << eq_exp << std::endl;
1388 Assert( ee->hasTerm( eq[0] ) );
1389 Assert( ee->hasTerm( eq[1] ) );
1390 if( ee->areDisequal( eq[0], eq[1], false ) ){
1391 Trace("quant-engine-ee-proc") << "processInferences : Conflict : " << eq << std::endl;
1392 if( Trace.isOn("term-db-lemma") ){
1393 Trace("term-db-lemma") << "Disequal terms, equal by normalization : " << eq[0] << " " << eq[1] << "!!!!" << std::endl;
1394 if( !d_qe->getTheoryEngine()->needCheck() ){
1395 Trace("term-db-lemma") << " all theories passed with no lemmas." << std::endl;
1396 //this should really never happen (implies arithmetic is incomplete when sharing is enabled)
1397 Assert( false );
1398 }
1399 Trace("term-db-lemma") << " add split on : " << eq << std::endl;
1400 }
1401 d_qe->addSplit( eq );
1402 return false;
1403 }else{
1404 ee->assertEquality( eq, true, eq_exp );
1405 d_eqi_counter = d_eqi_counter.get() + 1;
1406 }
1407 }
1408 Assert( ee->consistent() );
1409 }
1410 return true;
1411 }
1412
1413 bool EqualityQueryQuantifiersEngine::hasTerm( Node a ){
1414 return getEngine()->hasTerm( a );
1415 }
1416
1417 Node EqualityQueryQuantifiersEngine::getRepresentative( Node a ){
1418 eq::EqualityEngine* ee = getEngine();
1419 if( ee->hasTerm( a ) ){
1420 return ee->getRepresentative( a );
1421 }else{
1422 return a;
1423 }
1424 }
1425
1426 bool EqualityQueryQuantifiersEngine::areEqual( Node a, Node b ){
1427 if( a==b ){
1428 return true;
1429 }else{
1430 eq::EqualityEngine* ee = getEngine();
1431 if( ee->hasTerm( a ) && ee->hasTerm( b ) ){
1432 if( ee->areEqual( a, b ) ){
1433 return true;
1434 }
1435 }
1436 return false;
1437 }
1438 }
1439
1440 bool EqualityQueryQuantifiersEngine::areDisequal( Node a, Node b ){
1441 if( a==b ){
1442 return false;
1443 }else{
1444 eq::EqualityEngine* ee = getEngine();
1445 if( ee->hasTerm( a ) && ee->hasTerm( b ) ){
1446 if( ee->areDisequal( a, b, false ) ){
1447 return true;
1448 }
1449 }
1450 return false;
1451 }
1452 }
1453
1454 Node EqualityQueryQuantifiersEngine::getInternalRepresentative( Node a, Node f, int index ){
1455 Assert( f.isNull() || f.getKind()==FORALL );
1456 Node r = getRepresentative( a );
1457 if( options::finiteModelFind() ){
1458 if( r.isConst() && quantifiers::TermDb::containsUninterpretedConstant( r ) ){
1459 //map back from values assigned by model, if any
1460 if( d_qe->getModel() ){
1461 std::map< Node, Node >::iterator it = d_qe->getModel()->d_rep_set.d_values_to_terms.find( r );
1462 if( it!=d_qe->getModel()->d_rep_set.d_values_to_terms.end() ){
1463 r = it->second;
1464 r = getRepresentative( r );
1465 }else{
1466 if( r.getType().isSort() ){
1467 Trace("internal-rep-warn") << "No representative for UF constant." << std::endl;
1468 //should never happen : UF constants should never escape model
1469 Assert( false );
1470 }
1471 }
1472 }
1473 }
1474 }
1475 if( options::quantRepMode()==quantifiers::QUANT_REP_MODE_EE ){
1476 return r;
1477 }else{
1478 TypeNode v_tn = f.isNull() ? a.getType() : f[0][index].getType();
1479 std::map< Node, Node >::iterator itir = d_int_rep[v_tn].find( r );
1480 if( itir==d_int_rep[v_tn].end() ){
1481 //find best selection for representative
1482 Node r_best;
1483 //if( options::fmfRelevantDomain() && !f.isNull() ){
1484 // Trace("internal-rep-debug") << "Consult relevant domain to mkRep " << r << std::endl;
1485 // r_best = d_qe->getRelevantDomain()->getRelevantTerm( f, index, r );
1486 // Trace("internal-rep-debug") << "Returned " << r_best << " " << r << std::endl;
1487 //}
1488 std::vector< Node > eqc;
1489 getEquivalenceClass( r, eqc );
1490 Trace("internal-rep-select") << "Choose representative for equivalence class : { ";
1491 for( unsigned i=0; i<eqc.size(); i++ ){
1492 if( i>0 ) Trace("internal-rep-select") << ", ";
1493 Trace("internal-rep-select") << eqc[i];
1494 }
1495 Trace("internal-rep-select") << " }, type = " << v_tn << std::endl;
1496 int r_best_score = -1;
1497 for( size_t i=0; i<eqc.size(); i++ ){
1498 int score = getRepScore( eqc[i], f, index, v_tn );
1499 if( score!=-2 ){
1500 if( r_best.isNull() || ( score>=0 && ( r_best_score<0 || score<r_best_score ) ) ){
1501 r_best = eqc[i];
1502 r_best_score = score;
1503 }
1504 }
1505 }
1506 if( r_best.isNull() ){
1507 Trace("internal-rep-warn") << "No valid choice for representative in eqc class." << std::endl;
1508 r_best = r;
1509 }
1510 //now, make sure that no other member of the class is an instance
1511 std::hash_map<TNode, Node, TNodeHashFunction> cache;
1512 r_best = getInstance( r_best, eqc, cache );
1513 //store that this representative was chosen at this point
1514 if( d_rep_score.find( r_best )==d_rep_score.end() ){
1515 d_rep_score[ r_best ] = d_reset_count;
1516 }
1517 Trace("internal-rep-select") << "...Choose " << r_best << " with score " << r_best_score << std::endl;
1518 Assert( r_best.getType().isSubtypeOf( v_tn ) );
1519 d_int_rep[v_tn][r] = r_best;
1520 if( r_best!=a ){
1521 Trace("internal-rep-debug") << "rep( " << a << " ) = " << r << ", " << std::endl;
1522 Trace("internal-rep-debug") << "int_rep( " << a << " ) = " << r_best << ", " << std::endl;
1523 }
1524 return r_best;
1525 }else{
1526 return itir->second;
1527 }
1528 }
1529 }
1530
1531 void EqualityQueryQuantifiersEngine::flattenRepresentatives( std::map< TypeNode, std::vector< Node > >& reps ) {
1532 //make sure internal representatives currently exist
1533 for( std::map< TypeNode, std::vector< Node > >::iterator it = reps.begin(); it != reps.end(); ++it ){
1534 if( it->first.isSort() ){
1535 for( unsigned i=0; i<it->second.size(); i++ ){
1536 Node r = getInternalRepresentative( it->second[i], Node::null(), 0 );
1537 }
1538 }
1539 }
1540 Trace("internal-rep-flatten") << "---Flattening representatives : " << std::endl;
1541 for( std::map< TypeNode, std::map< Node, Node > >::iterator itt = d_int_rep.begin(); itt != d_int_rep.end(); ++itt ){
1542 for( std::map< Node, Node >::iterator it = itt->second.begin(); it != itt->second.end(); ++it ){
1543 Trace("internal-rep-flatten") << itt->first << " : irep( " << it->first << " ) = " << it->second << std::endl;
1544 }
1545 }
1546 //store representatives for newly created terms
1547 std::map< Node, Node > temp_rep_map;
1548
1549 bool success;
1550 do {
1551 success = false;
1552 for( std::map< TypeNode, std::map< Node, Node > >::iterator itt = d_int_rep.begin(); itt != d_int_rep.end(); ++itt ){
1553 for( std::map< Node, Node >::iterator it = itt->second.begin(); it != itt->second.end(); ++it ){
1554 if( it->second.getKind()==APPLY_UF && it->second.getType().isSort() ){
1555 Node ni = it->second;
1556 std::vector< Node > cc;
1557 cc.push_back( it->second.getOperator() );
1558 bool changed = false;
1559 for( unsigned j=0; j<ni.getNumChildren(); j++ ){
1560 if( ni[j].getType().isSort() ){
1561 Node r = getRepresentative( ni[j] );
1562 if( itt->second.find( r )==itt->second.end() ){
1563 Assert( temp_rep_map.find( r )!=temp_rep_map.end() );
1564 r = temp_rep_map[r];
1565 }
1566 if( r==ni ){
1567 //found sub-term as instance
1568 Trace("internal-rep-flatten-debug") << "...Changed " << it->second << " to subterm " << ni[j] << std::endl;
1569 itt->second[it->first] = ni[j];
1570 changed = false;
1571 success = true;
1572 break;
1573 }else{
1574 Node ir = itt->second[r];
1575 cc.push_back( ir );
1576 if( ni[j]!=ir ){
1577 changed = true;
1578 }
1579 }
1580 }else{
1581 changed = false;
1582 break;
1583 }
1584 }
1585 if( changed ){
1586 Node n = NodeManager::currentNM()->mkNode( APPLY_UF, cc );
1587 Trace("internal-rep-flatten-debug") << "...Changed " << it->second << " to " << n << std::endl;
1588 success = true;
1589 itt->second[it->first] = n;
1590 temp_rep_map[n] = it->first;
1591 }
1592 }
1593 }
1594 }
1595 }while( success );
1596 Trace("internal-rep-flatten") << "---After flattening : " << std::endl;
1597 for( std::map< TypeNode, std::map< Node, Node > >::iterator itt = d_int_rep.begin(); itt != d_int_rep.end(); ++itt ){
1598 for( std::map< Node, Node >::iterator it = itt->second.begin(); it != itt->second.end(); ++it ){
1599 Trace("internal-rep-flatten") << itt->first << " : irep( " << it->first << " ) = " << it->second << std::endl;
1600 }
1601 }
1602 }
1603
1604 eq::EqualityEngine* EqualityQueryQuantifiersEngine::getEngine(){
1605 return d_qe->getMasterEqualityEngine();
1606 }
1607
1608 void EqualityQueryQuantifiersEngine::getEquivalenceClass( Node a, std::vector< Node >& eqc ){
1609 eq::EqualityEngine* ee = getEngine();
1610 if( ee->hasTerm( a ) ){
1611 Node rep = ee->getRepresentative( a );
1612 eq::EqClassIterator eqc_iter( rep, ee );
1613 while( !eqc_iter.isFinished() ){
1614 eqc.push_back( *eqc_iter );
1615 eqc_iter++;
1616 }
1617 }else{
1618 eqc.push_back( a );
1619 }
1620 //a should be in its equivalence class
1621 Assert( std::find( eqc.begin(), eqc.end(), a )!=eqc.end() );
1622 }
1623
1624 TNode EqualityQueryQuantifiersEngine::getCongruentTerm( Node f, std::vector< TNode >& args ) {
1625 return d_qe->getTermDatabase()->getCongruentTerm( f, args );
1626 }
1627
1628 //helper functions
1629
1630 Node EqualityQueryQuantifiersEngine::getInstance( Node n, const std::vector< Node >& eqc, std::hash_map<TNode, Node, TNodeHashFunction>& cache ){
1631 if(cache.find(n) != cache.end()) {
1632 return cache[n];
1633 }
1634 for( size_t i=0; i<n.getNumChildren(); i++ ){
1635 Node nn = getInstance( n[i], eqc, cache );
1636 if( !nn.isNull() ){
1637 return cache[n] = nn;
1638 }
1639 }
1640 if( std::find( eqc.begin(), eqc.end(), n )!=eqc.end() ){
1641 return cache[n] = n;
1642 }else{
1643 return cache[n] = Node::null();
1644 }
1645 }
1646
1647 //-2 : invalid, -1 : undesired, otherwise : smaller the score, the better
1648 int EqualityQueryQuantifiersEngine::getRepScore( Node n, Node f, int index, TypeNode v_tn ){
1649 if( options::cbqi() && quantifiers::TermDb::hasInstConstAttr(n) ){ //reject
1650 return -2;
1651 }else if( !n.getType().isSubtypeOf( v_tn ) ){ //reject if incorrect type
1652 return -2;
1653 }else if( options::lteRestrictInstClosure() && ( !d_qe->getTermDatabase()->isInstClosure( n ) || !d_qe->getTermDatabase()->hasTermCurrent( n, false ) ) ){
1654 return -1;
1655 }else if( options::instMaxLevel()!=-1 ){
1656 //score prefer lowest instantiation level
1657 if( n.hasAttribute(InstLevelAttribute()) ){
1658 return n.getAttribute(InstLevelAttribute());
1659 }else{
1660 return options::instLevelInputOnly() ? -1 : 0;
1661 }
1662 }else{
1663 if( options::quantRepMode()==quantifiers::QUANT_REP_MODE_FIRST ){
1664 //score prefers earliest use of this term as a representative
1665 return d_rep_score.find( n )==d_rep_score.end() ? -1 : d_rep_score[n];
1666 }else{
1667 Assert( options::quantRepMode()==quantifiers::QUANT_REP_MODE_DEPTH );
1668 return quantifiers::TermDb::getTermDepth( n );
1669 }
1670 }
1671 }