Improvements to symmetry breaking in sygus search. Minor fix for getting instantiatio...
[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_conflict_c(c, false),
61 //d_quants(u),
62 d_quants_red(u),
63 d_lemmas_produced_c(u),
64 d_skolemized(u),
65 d_ierCounter_c(c),
66 //d_ierCounter(c),
67 //d_ierCounter_lc(c),
68 //d_ierCounterLastLc(c),
69 d_presolve(u, true),
70 d_presolve_in(u),
71 d_presolve_cache(u),
72 d_presolve_cache_wq(u),
73 d_presolve_cache_wic(u){
74 //utilities
75 d_eq_query = new EqualityQueryQuantifiersEngine( c, this );
76 d_util.push_back( d_eq_query );
77
78 d_term_db = new quantifiers::TermDb( c, u, this );
79 d_util.push_back( d_term_db );
80
81 if( options::instPropagate() ){
82 d_inst_prop = new quantifiers::InstPropagator( this );
83 d_util.push_back( d_inst_prop );
84 d_inst_notify.push_back( d_inst_prop->getInstantiationNotify() );
85 }else{
86 d_inst_prop = NULL;
87 }
88
89 d_tr_trie = new inst::TriggerTrie;
90 d_curr_effort_level = QEFFORT_NONE;
91 d_conflict = false;
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( unsigned i=0; i<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_hasAddedLemma = false;
370 bool setIncomplete = false;
371 if( e==Theory::EFFORT_LAST_CALL ){
372 //sources of incompleteness
373 if( d_lte_part_inst && d_lte_part_inst->wasInvoked() ){
374 Trace("quant-engine-debug") << "Set incomplete due to LTE partial instantiation." << std::endl;
375 setIncomplete = true;
376 }
377 }
378 bool usedModelBuilder = false;
379
380 Trace("quant-engine-debug2") << "Quantifiers Engine call to check, level = " << e << ", needsCheck=" << needsCheck << std::endl;
381 if( needsCheck ){
382 //this will fail if a set of instances is marked as a conflict, but is not
383 Assert( !d_conflict_c.get() );
384 //flush previous lemmas (for instance, if was interupted), or other lemmas to process
385 flushLemmas();
386 if( d_hasAddedLemma ){
387 return;
388 }
389 if( !d_recorded_inst.empty() ){
390 Trace("quant-engine-debug") << "Removing " << d_recorded_inst.size() << " instantiations..." << std::endl;
391 //remove explicitly recorded instantiations
392 for( unsigned i=0; i<d_recorded_inst.size(); i++ ){
393 removeInstantiationInternal( d_recorded_inst[i].first, d_recorded_inst[i].second );
394 }
395 d_recorded_inst.clear();
396 }
397
398 double clSet = 0;
399 if( Trace.isOn("quant-engine") ){
400 clSet = double(clock())/double(CLOCKS_PER_SEC);
401 Trace("quant-engine") << ">>>>> Quantifiers Engine Round, effort = " << e << " <<<<<" << std::endl;
402 }
403
404 if( Trace.isOn("quant-engine-debug") ){
405 Trace("quant-engine-debug") << "Quantifiers Engine check, level = " << e << std::endl;
406 Trace("quant-engine-debug") << " depth : " << d_ierCounter_c << std::endl;
407 Trace("quant-engine-debug") << " modules to check : ";
408 for( unsigned i=0; i<qm.size(); i++ ){
409 Trace("quant-engine-debug") << qm[i]->identify() << " ";
410 }
411 Trace("quant-engine-debug") << std::endl;
412 Trace("quant-engine-debug") << " # quantified formulas = " << d_model->getNumAssertedQuantifiers() << std::endl;
413 if( !d_lemmas_waiting.empty() ){
414 Trace("quant-engine-debug") << " lemmas waiting = " << d_lemmas_waiting.size() << std::endl;
415 }
416 Trace("quant-engine-debug") << " Theory engine finished : " << !d_te->needCheck() << std::endl;
417 Trace("quant-engine-debug") << " Needs model effort : " << needsModelE << std::endl;
418 }
419 if( Trace.isOn("quant-engine-ee-pre") ){
420 Trace("quant-engine-ee-pre") << "Equality engine (pre-inference): " << std::endl;
421 debugPrintEqualityEngine( "quant-engine-ee-pre" );
422 }
423 if( Trace.isOn("quant-engine-assert") ){
424 Trace("quant-engine-assert") << "Assertions : " << std::endl;
425 getTheoryEngine()->printAssertions("quant-engine-assert");
426 }
427
428 //reset utilities
429 Trace("quant-engine-debug") << "Resetting all utilities..." << std::endl;
430 for( unsigned i=0; i<d_util.size(); i++ ){
431 Trace("quant-engine-debug2") << "Reset " << d_util[i]->identify().c_str() << "..." << std::endl;
432 if( !d_util[i]->reset( e ) ){
433 flushLemmas();
434 if( d_hasAddedLemma ){
435 return;
436 }else{
437 //should only fail reset if added a lemma
438 Assert( false );
439 }
440 }
441 }
442
443 if( Trace.isOn("quant-engine-ee") ){
444 Trace("quant-engine-ee") << "Equality engine : " << std::endl;
445 debugPrintEqualityEngine( "quant-engine-ee" );
446 }
447
448 //reset the model
449 Trace("quant-engine-debug") << "Reset model..." << std::endl;
450 d_model->reset_round();
451
452 //reset the modules
453 Trace("quant-engine-debug") << "Resetting all modules..." << std::endl;
454 for( unsigned i=0; i<d_modules.size(); i++ ){
455 Trace("quant-engine-debug2") << "Reset " << d_modules[i]->identify().c_str() << std::endl;
456 d_modules[i]->reset_round( e );
457 }
458 Trace("quant-engine-debug") << "Done resetting all modules." << std::endl;
459 //reset may have added lemmas
460 flushLemmas();
461 if( d_hasAddedLemma ){
462 return;
463 }
464
465 if( e==Theory::EFFORT_LAST_CALL ){
466 //if effort is last call, try to minimize model first
467 //uf::StrongSolverTheoryUF * ufss = ((uf::TheoryUF*)getTheoryEngine()->theoryOf( THEORY_UF ))->getStrongSolver();
468 //if( ufss && !ufss->minimize() ){ return; }
469 ++(d_statistics.d_instantiation_rounds_lc);
470 }else if( e==Theory::EFFORT_FULL ){
471 ++(d_statistics.d_instantiation_rounds);
472 }
473 Trace("quant-engine-debug") << "Check modules that needed check..." << std::endl;
474 for( unsigned quant_e = QEFFORT_CONFLICT; quant_e<=QEFFORT_LAST_CALL; quant_e++ ){
475 d_curr_effort_level = quant_e;
476 bool success = true;
477 //build the model if any module requested it
478 if( needsModelE==quant_e ){
479 Assert( d_builder!=NULL );
480 Trace("quant-engine-debug") << "Build model..." << std::endl;
481 usedModelBuilder = true;
482 d_builder->d_addedLemmas = 0;
483 d_builder->buildModel( d_model, false );
484 //we are done if model building was unsuccessful
485 if( d_builder->d_addedLemmas>0 ){
486 success = false;
487 }
488 }
489 if( success ){
490 //check each module
491 for( unsigned i=0; i<qm.size(); i++ ){
492 Trace("quant-engine-debug") << "Check " << qm[i]->identify().c_str() << " at effort " << quant_e << "..." << std::endl;
493 qm[i]->check( e, quant_e );
494 if( d_conflict ){
495 Trace("quant-engine-debug") << "...conflict!" << std::endl;
496 break;
497 }
498 }
499 }
500 //flush all current lemmas
501 flushLemmas();
502 //if we have added one, stop
503 if( d_hasAddedLemma ){
504 break;
505 }else{
506 Assert( !d_conflict );
507 if( quant_e==QEFFORT_CONFLICT ){
508 if( e==Theory::EFFORT_FULL ){
509 //increment if a last call happened, we are not strictly enforcing interleaving, or already were in phase
510 if( d_ierCounterLastLc!=d_ierCounter_lc || !options::instWhenStrictInterleave() || d_ierCounter%d_inst_when_phase!=0 ){
511 d_ierCounter = d_ierCounter + 1;
512 d_ierCounterLastLc = d_ierCounter_lc;
513 d_ierCounter_c = d_ierCounter_c.get() + 1;
514 }
515 }else if( e==Theory::EFFORT_LAST_CALL ){
516 d_ierCounter_lc = d_ierCounter_lc + 1;
517 }
518 }else if( quant_e==QEFFORT_MODEL ){
519 if( e==Theory::EFFORT_LAST_CALL ){
520 if( !d_recorded_inst.empty() ){
521 setIncomplete = true;
522 }
523 //if we have a chance not to set incomplete
524 if( !setIncomplete ){
525 setIncomplete = false;
526 //check if we should set the incomplete flag
527 for( unsigned i=0; i<qm.size(); i++ ){
528 if( !qm[i]->checkComplete() ){
529 Trace("quant-engine-debug") << "Set incomplete because " << qm[i]->identify().c_str() << " was incomplete." << std::endl;
530 setIncomplete = true;
531 break;
532 }
533 }
534 }
535 //if setIncomplete = false, we will answer SAT, otherwise we will run at quant_e QEFFORT_LAST_CALL
536 if( !setIncomplete ){
537 break;
538 }
539 }
540 }
541 }
542 }
543 d_curr_effort_level = QEFFORT_NONE;
544 Trace("quant-engine-debug") << "Done check modules that needed check." << std::endl;
545 if( d_hasAddedLemma ){
546 //debug information
547 if( Trace.isOn("inst-per-quant-round") ){
548 for( std::map< Node, int >::iterator it = d_temp_inst_debug.begin(); it != d_temp_inst_debug.end(); ++it ){
549 Trace("inst-per-quant-round") << " * " << it->second << " for " << it->first << std::endl;
550 d_temp_inst_debug[it->first] = 0;
551 }
552 }
553 }
554 if( Trace.isOn("quant-engine") ){
555 double clSet2 = double(clock())/double(CLOCKS_PER_SEC);
556 Trace("quant-engine") << "Finished quantifiers engine, total time = " << (clSet2-clSet);
557 Trace("quant-engine") << ", added lemma = " << d_hasAddedLemma;
558 Trace("quant-engine") << std::endl;
559 }
560
561 Trace("quant-engine-debug2") << "Finished quantifiers engine check." << std::endl;
562 }else{
563 Trace("quant-engine-debug2") << "Quantifiers Engine does not need check." << std::endl;
564 }
565
566 //SAT case
567 if( e==Theory::EFFORT_LAST_CALL && !d_hasAddedLemma ){
568 if( options::produceModels() ){
569 if( usedModelBuilder ){
570 Trace("quant-engine-debug") << "Build completed model..." << std::endl;
571 d_builder->buildModel( d_model, true );
572 }else if( !d_model->isModelSet() ){
573 //use default model builder when no module built the model
574 Trace("quant-engine-debug") << "Build the model..." << std::endl;
575 d_te->getModelBuilder()->buildModel( d_model, true );
576 Trace("quant-engine-debug") << "Done building the model." << std::endl;
577 }
578 }
579 if( setIncomplete ){
580 Trace("quant-engine") << "Set incomplete flag." << std::endl;
581 getOutputChannel().setIncomplete();
582 }
583 //output debug stats
584 if( Trace.isOn("inst-per-quant") ){
585 for( std::map< Node, int >::iterator it = d_total_inst_debug.begin(); it != d_total_inst_debug.end(); ++it ){
586 Trace("inst-per-quant") << " * " << it->second << " for " << it->first << std::endl;
587 }
588 }
589 }
590 }
591
592 void QuantifiersEngine::notifyCombineTheories() {
593 //if allowing theory combination to happen at most once between instantiation rounds
594 //d_ierCounter = 1;
595 //d_ierCounterLastLc = -1;
596 }
597
598 bool QuantifiersEngine::reduceQuantifier( Node q ) {
599 BoolMap::const_iterator it = d_quants_red.find( q );
600 if( it==d_quants_red.end() ){
601 Node lem;
602 std::map< Node, Node >::iterator itr = d_quants_red_lem.find( q );
603 if( itr==d_quants_red_lem.end() ){
604 if( d_alpha_equiv ){
605 Trace("quant-engine-red") << "Alpha equivalence " << q << "?" << std::endl;
606 //add equivalence with another quantified formula
607 lem = d_alpha_equiv->reduceQuantifier( q );
608 if( !lem.isNull() ){
609 Trace("quant-engine-red") << "...alpha equivalence success." << std::endl;
610 ++(d_statistics.d_red_alpha_equiv);
611 }
612 }
613 d_quants_red_lem[q] = lem;
614 }else{
615 lem = itr->second;
616 }
617 if( !lem.isNull() ){
618 getOutputChannel().lemma( lem );
619 }
620 d_quants_red[q] = !lem.isNull();
621 return !lem.isNull();
622 }else{
623 return (*it).second;
624 }
625 }
626
627 bool QuantifiersEngine::registerQuantifier( Node f ){
628 std::map< Node, bool >::iterator it = d_quants.find( f );
629 if( it==d_quants.end() ){
630 Trace("quant") << "QuantifiersEngine : Register quantifier ";
631 Trace("quant") << " : " << f << std::endl;
632 ++(d_statistics.d_num_quant);
633 Assert( f.getKind()==FORALL );
634
635 //check whether we should apply a reduction
636 if( reduceQuantifier( f ) ){
637 d_quants[f] = false;
638 return false;
639 }else{
640 //make instantiation constants for f
641 d_term_db->makeInstantiationConstantsFor( f );
642 d_term_db->computeAttributes( f );
643 for( unsigned i=0; i<d_modules.size(); i++ ){
644 Trace("quant-debug") << "pre-register with " << d_modules[i]->identify() << "..." << std::endl;
645 d_modules[i]->preRegisterQuantifier( f );
646 }
647 QuantifiersModule * qm = getOwner( f );
648 if( qm!=NULL ){
649 Trace("quant") << " Owner : " << qm->identify() << std::endl;
650 }
651 //register with quantifier relevance
652 if( d_quant_rel ){
653 d_quant_rel->registerQuantifier( f );
654 }
655 //register with each module
656 for( unsigned i=0; i<d_modules.size(); i++ ){
657 Trace("quant-debug") << "register with " << d_modules[i]->identify() << "..." << std::endl;
658 d_modules[i]->registerQuantifier( f );
659 }
660 //TODO: remove this
661 Node ceBody = d_term_db->getInstConstantBody( f );
662 //also register it with the strong solver
663 //if( options::finiteModelFind() ){
664 // ((uf::TheoryUF*)d_te->theoryOf( THEORY_UF ))->getStrongSolver()->registerQuantifier( f );
665 //}
666 d_quants[f] = true;
667 return true;
668 }
669 }else{
670 return (*it).second;
671 }
672 }
673
674 void QuantifiersEngine::registerPattern( std::vector<Node> & pattern) {
675 for(std::vector<Node>::iterator p = pattern.begin(); p != pattern.end(); ++p){
676 std::set< Node > added;
677 getTermDatabase()->addTerm( *p, added );
678 }
679 }
680
681 void QuantifiersEngine::assertQuantifier( Node f, bool pol ){
682 if( !pol ){
683 //if not reduced
684 if( !reduceQuantifier( f ) ){
685 //do skolemization
686 if( d_skolemized.find( f )==d_skolemized.end() ){
687 Node body = d_term_db->getSkolemizedBody( f );
688 NodeBuilder<> nb(kind::OR);
689 nb << f << body.notNode();
690 Node lem = nb;
691 if( Trace.isOn("quantifiers-sk-debug") ){
692 Node slem = Rewriter::rewrite( lem );
693 Trace("quantifiers-sk-debug") << "Skolemize lemma : " << slem << std::endl;
694 }
695 getOutputChannel().lemma( lem, false, true );
696 d_skolemized[f] = true;
697 }
698 }
699 }else{
700 //assert to modules TODO : also for !pol?
701 //register the quantifier, assert it to each module
702 if( registerQuantifier( f ) ){
703 d_model->assertQuantifier( f );
704 for( int i=0; i<(int)d_modules.size(); i++ ){
705 d_modules[i]->assertNode( f );
706 }
707 addTermToDatabase( d_term_db->getInstConstantBody( f ), true );
708 }
709 }
710 }
711
712 void QuantifiersEngine::propagate( Theory::Effort level ){
713 CodeTimer codeTimer(d_statistics.d_time);
714
715 for( int i=0; i<(int)d_modules.size(); i++ ){
716 d_modules[i]->propagate( level );
717 }
718 }
719
720 Node QuantifiersEngine::getNextDecisionRequest(){
721 for( int i=0; i<(int)d_modules.size(); i++ ){
722 Node n = d_modules[i]->getNextDecisionRequest();
723 if( !n.isNull() ){
724 return n;
725 }
726 }
727 return Node::null();
728 }
729
730 quantifiers::TermDbSygus* QuantifiersEngine::getTermDatabaseSygus() {
731 return getTermDatabase()->getTermDatabaseSygus();
732 }
733
734 void QuantifiersEngine::addTermToDatabase( Node n, bool withinQuant, bool withinInstClosure ){
735 if( options::incrementalSolving() ){
736 if( d_presolve_in.find( n )==d_presolve_in.end() ){
737 d_presolve_in.insert( n );
738 d_presolve_cache.push_back( n );
739 d_presolve_cache_wq.push_back( withinQuant );
740 d_presolve_cache_wic.push_back( withinInstClosure );
741 }
742 }
743 //only wait if we are doing incremental solving
744 if( !d_presolve || !options::incrementalSolving() ){
745 std::set< Node > added;
746 getTermDatabase()->addTerm( n, added, withinQuant, withinInstClosure );
747 //maybe have triggered instantiations if we are doing eager instantiation
748 if( options::eagerInstQuant() ){
749 flushLemmas();
750 }
751 //added contains also the Node that just have been asserted in this branch
752 if( d_quant_rel ){
753 for( std::set< Node >::iterator i=added.begin(), end=added.end(); i!=end; i++ ){
754 if( !withinQuant ){
755 d_quant_rel->setRelevance( i->getOperator(), 0 );
756 }
757 }
758 }
759 }
760 }
761
762 void QuantifiersEngine::eqNotifyNewClass(TNode t) {
763 addTermToDatabase( t );
764 if( d_eq_query->getEqualityInference() ){
765 d_eq_query->getEqualityInference()->eqNotifyNewClass( t );
766 }
767 }
768
769 void QuantifiersEngine::eqNotifyPreMerge(TNode t1, TNode t2) {
770 if( d_eq_query->getEqualityInference() ){
771 d_eq_query->getEqualityInference()->eqNotifyMerge( t1, t2 );
772 }
773 }
774
775 void QuantifiersEngine::eqNotifyPostMerge(TNode t1, TNode t2) {
776
777 }
778
779 void QuantifiersEngine::eqNotifyDisequal(TNode t1, TNode t2, TNode reason) {
780 //if( d_qcf ){
781 // d_qcf->assertDisequal( t1, t2 );
782 //}
783 }
784
785 void QuantifiersEngine::computeTermVector( Node f, InstMatch& m, std::vector< Node >& vars, std::vector< Node >& terms ){
786 for( size_t i=0; i<f[0].getNumChildren(); i++ ){
787 Node n = m.get( i );
788 if( !n.isNull() ){
789 vars.push_back( f[0][i] );
790 terms.push_back( n );
791 }
792 }
793 }
794
795
796 bool QuantifiersEngine::recordInstantiationInternal( Node q, std::vector< Node >& terms, bool modEq, bool addedLem ) {
797 if( !addedLem ){
798 //record the instantiation for deletion later
799 d_recorded_inst.push_back( std::pair< Node, std::vector< Node > >( q, terms ) );
800 }
801 if( options::incrementalSolving() ){
802 Trace("inst-add-debug") << "Adding into context-dependent inst trie, modEq = " << modEq << std::endl;
803 inst::CDInstMatchTrie* imt;
804 std::map< Node, inst::CDInstMatchTrie* >::iterator it = d_c_inst_match_trie.find( q );
805 if( it!=d_c_inst_match_trie.end() ){
806 imt = it->second;
807 }else{
808 imt = new CDInstMatchTrie( getUserContext() );
809 d_c_inst_match_trie[q] = imt;
810 }
811 return imt->addInstMatch( this, q, terms, getUserContext(), modEq );
812 }else{
813 Trace("inst-add-debug") << "Adding into inst trie" << std::endl;
814 return d_inst_match_trie[q].addInstMatch( this, q, terms, modEq );
815 }
816 }
817
818 bool QuantifiersEngine::removeInstantiationInternal( Node q, std::vector< Node >& terms ) {
819 if( options::incrementalSolving() ){
820 std::map< Node, inst::CDInstMatchTrie* >::iterator it = d_c_inst_match_trie.find( q );
821 if( it!=d_c_inst_match_trie.end() ){
822 return it->second->removeInstMatch( this, q, terms );
823 }else{
824 return false;
825 }
826 }else{
827 return d_inst_match_trie[q].removeInstMatch( this, q, terms );
828 }
829 }
830
831 void QuantifiersEngine::setInstantiationLevelAttr( Node n, Node qn, uint64_t level ){
832 Trace("inst-level-debug2") << "IL : " << n << " " << qn << " " << level << std::endl;
833 //if not from the vector of terms we instantiatied
834 if( qn.getKind()!=BOUND_VARIABLE && n!=qn ){
835 //if this is a new term, without an instantiation level
836 if( !n.hasAttribute(InstLevelAttribute()) ){
837 InstLevelAttribute ila;
838 n.setAttribute(ila,level);
839 Trace("inst-level-debug") << "Set instantiation level " << n << " to " << level << std::endl;
840 }
841 Assert( n.getNumChildren()==qn.getNumChildren() );
842 for( unsigned i=0; i<n.getNumChildren(); i++ ){
843 setInstantiationLevelAttr( n[i], qn[i], level );
844 }
845 }
846 }
847
848 void QuantifiersEngine::setInstantiationLevelAttr( Node n, uint64_t level ){
849 if( !n.hasAttribute(InstLevelAttribute()) ){
850 InstLevelAttribute ila;
851 n.setAttribute(ila,level);
852 Trace("inst-level-debug") << "Set instantiation level " << n << " to " << level << std::endl;
853 }
854 for( unsigned i=0; i<n.getNumChildren(); i++ ){
855 setInstantiationLevelAttr( n[i], level );
856 }
857 }
858
859 Node QuantifiersEngine::getSubstitute( Node n, std::vector< Node >& terms ){
860 if( n.getKind()==INST_CONSTANT ){
861 Debug("check-inst") << "Substitute inst constant : " << n << std::endl;
862 return terms[n.getAttribute(InstVarNumAttribute())];
863 }else{
864 //if( !quantifiers::TermDb::hasInstConstAttr( n ) ){
865 //Debug("check-inst") << "No inst const attr : " << n << std::endl;
866 //return n;
867 //}else{
868 //Debug("check-inst") << "Recurse on : " << n << std::endl;
869 std::vector< Node > cc;
870 if( n.getMetaKind() == kind::metakind::PARAMETERIZED ){
871 cc.push_back( n.getOperator() );
872 }
873 bool changed = false;
874 for( unsigned i=0; i<n.getNumChildren(); i++ ){
875 Node c = getSubstitute( n[i], terms );
876 cc.push_back( c );
877 changed = changed || c!=n[i];
878 }
879 if( changed ){
880 Node ret = NodeManager::currentNM()->mkNode( n.getKind(), cc );
881 return ret;
882 }else{
883 return n;
884 }
885 }
886 }
887
888
889 Node QuantifiersEngine::getInstantiation( Node q, std::vector< Node >& vars, std::vector< Node >& terms, bool doVts ){
890 Node body;
891 Assert( vars.size()==terms.size() );
892 //process partial instantiation if necessary
893 if( q[0].getNumChildren()!=vars.size() ){
894 body = q[ 1 ].substitute( vars.begin(), vars.end(), terms.begin(), terms.end() );
895 std::vector< Node > uninst_vars;
896 //doing a partial instantiation, must add quantifier for all uninstantiated variables
897 for( unsigned i=0; i<q[0].getNumChildren(); i++ ){
898 if( std::find( vars.begin(), vars.end(), q[0][i] )==vars.end() ){
899 uninst_vars.push_back( q[0][i] );
900 }
901 }
902 Trace("partial-inst") << "Partially instantiating with " << vars.size() << " / " << q[0].getNumChildren() << " for " << q << std::endl;
903 Assert( !uninst_vars.empty() );
904 Node bvl = NodeManager::currentNM()->mkNode( BOUND_VAR_LIST, uninst_vars );
905 body = NodeManager::currentNM()->mkNode( FORALL, bvl, body );
906 Trace("partial-inst") << "Partial instantiation : " << q << std::endl;
907 Trace("partial-inst") << " : " << body << std::endl;
908 }else{
909 if( options::cbqi() ){
910 body = q[ 1 ].substitute( vars.begin(), vars.end(), terms.begin(), terms.end() );
911 }else{
912 //do optimized version
913 Node icb = d_term_db->getInstConstantBody( q );
914 body = getSubstitute( icb, terms );
915 if( Debug.isOn("check-inst") ){
916 Node body2 = q[ 1 ].substitute( vars.begin(), vars.end(), terms.begin(), terms.end() );
917 if( body!=body2 ){
918 Debug("check-inst") << "Substitution is wrong : " << body << " " << body2 << std::endl;
919 }
920 }
921 }
922 }
923 if( doVts ){
924 //do virtual term substitution
925 body = Rewriter::rewrite( body );
926 Trace("quant-vts-debug") << "Rewrite vts symbols in " << body << std::endl;
927 Node body_r = d_term_db->rewriteVtsSymbols( body );
928 Trace("quant-vts-debug") << " ...result: " << body_r << std::endl;
929 body = body_r;
930 }
931 return body;
932 }
933
934 Node QuantifiersEngine::getInstantiation( Node q, InstMatch& m, bool doVts ){
935 std::vector< Node > vars;
936 std::vector< Node > terms;
937 computeTermVector( q, m, vars, terms );
938 return getInstantiation( q, vars, terms, doVts );
939 }
940
941 Node QuantifiersEngine::getInstantiation( Node q, std::vector< Node >& terms, bool doVts ) {
942 Assert( d_term_db->d_vars.find( q )!=d_term_db->d_vars.end() );
943 return getInstantiation( q, d_term_db->d_vars[q], terms, doVts );
944 }
945
946 /*
947 bool QuantifiersEngine::existsInstantiation( Node f, InstMatch& m, bool modEq ){
948 if( options::incrementalSolving() ){
949 if( d_c_inst_match_trie.find( f )!=d_c_inst_match_trie.end() ){
950 if( d_c_inst_match_trie[f]->existsInstMatch( this, f, m, getUserContext(), modEq ) ){
951 return true;
952 }
953 }
954 }else{
955 if( d_inst_match_trie.find( f )!=d_inst_match_trie.end() ){
956 if( d_inst_match_trie[f].existsInstMatch( this, f, m, modEq ) ){
957 return true;
958 }
959 }
960 }
961 return false;
962 }
963 */
964
965 bool QuantifiersEngine::addLemma( Node lem, bool doCache, bool doRewrite ){
966 if( doCache ){
967 if( doRewrite ){
968 lem = Rewriter::rewrite(lem);
969 }
970 Trace("inst-add-debug") << "Adding lemma : " << lem << std::endl;
971 BoolMap::const_iterator itp = d_lemmas_produced_c.find( lem );
972 if( itp==d_lemmas_produced_c.end() || !(*itp).second ){
973 //d_curr_out->lemma( lem, false, true );
974 d_lemmas_produced_c[ lem ] = true;
975 d_lemmas_waiting.push_back( lem );
976 Trace("inst-add-debug") << "Added lemma" << std::endl;
977 return true;
978 }else{
979 Trace("inst-add-debug") << "Duplicate." << std::endl;
980 return false;
981 }
982 }else{
983 //do not need to rewrite, will be rewritten after sending
984 d_lemmas_waiting.push_back( lem );
985 return true;
986 }
987 }
988
989 bool QuantifiersEngine::removeLemma( Node lem ) {
990 std::vector< Node >::iterator it = std::find( d_lemmas_waiting.begin(), d_lemmas_waiting.end(), lem );
991 if( it!=d_lemmas_waiting.end() ){
992 d_lemmas_waiting.erase( it, it + 1 );
993 d_lemmas_produced_c[ lem ] = false;
994 return true;
995 }else{
996 return false;
997 }
998 }
999
1000 void QuantifiersEngine::addRequirePhase( Node lit, bool req ){
1001 d_phase_req_waiting[lit] = req;
1002 }
1003
1004 bool QuantifiersEngine::addInstantiation( Node q, InstMatch& m, bool mkRep, bool modEq, bool doVts ){
1005 std::vector< Node > terms;
1006 m.getTerms( q, terms );
1007 return addInstantiation( q, terms, mkRep, modEq, doVts );
1008 }
1009
1010 bool QuantifiersEngine::addInstantiation( Node q, std::vector< Node >& terms, bool mkRep, bool modEq, bool doVts ) {
1011 // For resource-limiting (also does a time check).
1012 getOutputChannel().safePoint(options::quantifierStep());
1013 Assert( !d_conflict );
1014 Assert( terms.size()==q[0].getNumChildren() );
1015 Trace("inst-add-debug") << "For quantified formula " << q << ", add instantiation: " << std::endl;
1016 std::vector< Node > rlv_cond;
1017 for( unsigned i=0; i<terms.size(); i++ ){
1018 Trace("inst-add-debug") << " " << q[0][i];
1019 Trace("inst-add-debug2") << " -> " << terms[i];
1020 if( terms[i].isNull() ){
1021 terms[i] = d_term_db->getModelBasisTerm( q[0][i].getType() );
1022 }
1023 if( mkRep ){
1024 //pick the best possible representative for instantiation, based on past use and simplicity of term
1025 terms[i] = d_eq_query->getInternalRepresentative( terms[i], q, i );
1026 }else{
1027 //ensure the type is correct
1028 terms[i] = quantifiers::TermDb::ensureType( terms[i], q[0][i].getType() );
1029 }
1030 Trace("inst-add-debug") << " -> " << terms[i] << std::endl;
1031 if( terms[i].isNull() ){
1032 Trace("inst-add-debug") << " --> Failed to make term vector, due to term/type restrictions." << std::endl;
1033 return false;
1034 }else{
1035 //get relevancy conditions
1036 if( options::instRelevantCond() ){
1037 quantifiers::TermDb::getRelevancyCondition( terms[i], rlv_cond );
1038 }
1039 }
1040 #ifdef CVC4_ASSERTIONS
1041 bool bad_inst = false;
1042 if( quantifiers::TermDb::containsUninterpretedConstant( terms[i] ) ){
1043 bad_inst = true;
1044 }else if( !terms[i].getType().isSubtypeOf( q[0][i].getType() ) ){
1045 bad_inst = true;
1046 }else if( options::cbqi() ){
1047 Node icf = quantifiers::TermDb::getInstConstAttr(terms[i]);
1048 if( !icf.isNull() ){
1049 if( icf==q ){
1050 bad_inst = true;
1051 }else{
1052 bad_inst = quantifiers::TermDb::containsTerms( terms[i], d_term_db->d_inst_constants[q] );
1053 }
1054 }
1055 }
1056 //this assertion is critical to soundness
1057 if( bad_inst ){
1058 Trace("inst")<< "***& Bad Instantiate " << q << " with " << std::endl;
1059 for( unsigned j=0; j<terms.size(); j++ ){
1060 Trace("inst") << " " << terms[j] << std::endl;
1061 }
1062 Assert( false );
1063 }
1064 #endif
1065 }
1066
1067 //check based on instantiation level
1068 if( options::instMaxLevel()!=-1 || options::lteRestrictInstClosure() ){
1069 for( unsigned i=0; i<terms.size(); i++ ){
1070 if( !d_term_db->isTermEligibleForInstantiation( terms[i], q, true ) ){
1071 return false;
1072 }
1073 }
1074 }
1075
1076 //check for positive entailment
1077 if( options::instNoEntail() ){
1078 //TODO: check consistency of equality engine (if not aborting on utility's reset)
1079 std::map< TNode, TNode > subs;
1080 for( unsigned i=0; i<terms.size(); i++ ){
1081 subs[q[0][i]] = terms[i];
1082 }
1083 if( d_term_db->isEntailed( q[1], subs, false, true ) ){
1084 Trace("inst-add-debug") << " --> Currently entailed." << std::endl;
1085 return false;
1086 }
1087 //Node eval = d_term_db->evaluateTerm( q[1], subs, false, true );
1088 //Trace("ajr-temp") << "Instantiation evaluates to : " << std::endl;
1089 //Trace("ajr-temp") << " " << eval << std::endl;
1090 }
1091
1092 //check for term vector duplication
1093 bool alreadyExists = !recordInstantiationInternal( q, terms, modEq );
1094 if( alreadyExists ){
1095 Trace("inst-add-debug") << " --> Already exists." << std::endl;
1096 ++(d_statistics.d_inst_duplicate_eq);
1097 return false;
1098 }
1099
1100 //construct the instantiation
1101 Trace("inst-add-debug") << "Constructing instantiation..." << std::endl;
1102 Assert( d_term_db->d_vars[q].size()==terms.size() );
1103 Node body = getInstantiation( q, d_term_db->d_vars[q], terms, doVts ); //do virtual term substitution
1104 Node orig_body = body;
1105 body = quantifiers::QuantifiersRewriter::preprocess( body, true );
1106 Trace("inst-debug") << "...preprocess to " << body << std::endl;
1107
1108 //construct the lemma
1109 Trace("inst-assert") << "(assert " << body << ")" << std::endl;
1110 body = Rewriter::rewrite(body);
1111 Node lem;
1112 if( rlv_cond.empty() ){
1113 lem = NodeManager::currentNM()->mkNode( kind::OR, q.negate(), body );
1114 }else{
1115 rlv_cond.push_back( q.negate() );
1116 rlv_cond.push_back( body );
1117 lem = NodeManager::currentNM()->mkNode( kind::OR, rlv_cond );
1118 }
1119 lem = Rewriter::rewrite(lem);
1120
1121 //check for lemma duplication
1122 if( addLemma( lem, true, false ) ){
1123 d_total_inst_debug[q]++;
1124 d_temp_inst_debug[q]++;
1125 d_total_inst_count_debug++;
1126 if( Trace.isOn("inst") ){
1127 Trace("inst") << "*** Instantiate " << q << " with " << std::endl;
1128 for( unsigned i=0; i<terms.size(); i++ ){
1129 if( Trace.isOn("inst") ){
1130 Trace("inst") << " " << terms[i];
1131 if( Trace.isOn("inst-debug") ){
1132 Trace("inst-debug") << ", type=" << terms[i].getType() << ", var_type=" << q[0][i].getType();
1133 }
1134 Trace("inst") << std::endl;
1135 }
1136 }
1137 }
1138 if( options::instMaxLevel()!=-1 ){
1139 if( doVts ){
1140 //virtual term substitution/instantiation level features are incompatible
1141 Assert( false );
1142 }else{
1143 uint64_t maxInstLevel = 0;
1144 for( unsigned i=0; i<terms.size(); i++ ){
1145 if( terms[i].hasAttribute(InstLevelAttribute()) ){
1146 if( terms[i].getAttribute(InstLevelAttribute())>maxInstLevel ){
1147 maxInstLevel = terms[i].getAttribute(InstLevelAttribute());
1148 }
1149 }
1150 }
1151 setInstantiationLevelAttr( orig_body, q[1], maxInstLevel+1 );
1152 }
1153 }
1154 if( d_curr_effort_level>QEFFORT_CONFLICT && d_curr_effort_level<QEFFORT_NONE ){
1155 //notify listeners
1156 for( unsigned j=0; j<d_inst_notify.size(); j++ ){
1157 if( !d_inst_notify[j]->notifyInstantiation( d_curr_effort_level, q, lem, terms, body ) ){
1158 Trace("inst-add-debug") << "...we are in conflict." << std::endl;
1159 d_conflict = true;
1160 d_conflict_c = true;
1161 Assert( !d_lemmas_waiting.empty() );
1162 break;
1163 }
1164 }
1165 }
1166 Trace("inst-add-debug") << " --> Success." << std::endl;
1167 ++(d_statistics.d_instantiations);
1168 return true;
1169 }else{
1170 Trace("inst-add-debug") << " --> Lemma already exists." << std::endl;
1171 ++(d_statistics.d_inst_duplicate);
1172 return false;
1173 }
1174 }
1175
1176 bool QuantifiersEngine::removeInstantiation( Node q, Node lem, std::vector< Node >& terms ) {
1177 //lem must occur in d_waiting_lemmas
1178 if( removeLemma( lem ) ){
1179 return removeInstantiationInternal( q, terms );
1180 }else{
1181 return false;
1182 }
1183 }
1184
1185 bool QuantifiersEngine::addSplit( Node n, bool reqPhase, bool reqPhasePol ){
1186 n = Rewriter::rewrite( n );
1187 Node lem = NodeManager::currentNM()->mkNode( OR, n, n.notNode() );
1188 if( addLemma( lem ) ){
1189 Debug("inst") << "*** Add split " << n<< std::endl;
1190 return true;
1191 }
1192 return false;
1193 }
1194
1195 bool QuantifiersEngine::addSplitEquality( Node n1, Node n2, bool reqPhase, bool reqPhasePol ){
1196 //Assert( !areEqual( n1, n2 ) );
1197 //Assert( !areDisequal( n1, n2 ) );
1198 Kind knd = n1.getType()==NodeManager::currentNM()->booleanType() ? IFF : EQUAL;
1199 Node fm = NodeManager::currentNM()->mkNode( knd, n1, n2 );
1200 return addSplit( fm );
1201 }
1202
1203 void QuantifiersEngine::markRelevant( Node q ) {
1204 d_model->markRelevant( q );
1205 }
1206
1207 bool QuantifiersEngine::getInstWhenNeedsCheck( Theory::Effort e ) {
1208 Trace("quant-engine-debug2") << "Get inst when needs check, counts=" << d_ierCounter << ", " << d_ierCounter_lc << std::endl;
1209 //determine if we should perform check, based on instWhenMode
1210 bool performCheck = false;
1211 if( options::instWhenMode()==quantifiers::INST_WHEN_FULL ){
1212 performCheck = ( e >= Theory::EFFORT_FULL );
1213 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_DELAY ){
1214 performCheck = ( e >= Theory::EFFORT_FULL ) && !getTheoryEngine()->needCheck();
1215 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_LAST_CALL ){
1216 performCheck = ( ( e==Theory::EFFORT_FULL && d_ierCounter%d_inst_when_phase!=0 ) || e==Theory::EFFORT_LAST_CALL );
1217 }else if( options::instWhenMode()==quantifiers::INST_WHEN_FULL_DELAY_LAST_CALL ){
1218 performCheck = ( ( e==Theory::EFFORT_FULL && !getTheoryEngine()->needCheck() && d_ierCounter%d_inst_when_phase!=0 ) || e==Theory::EFFORT_LAST_CALL );
1219 }else if( options::instWhenMode()==quantifiers::INST_WHEN_LAST_CALL ){
1220 performCheck = ( e >= Theory::EFFORT_LAST_CALL );
1221 }else{
1222 performCheck = true;
1223 }
1224 if( e==Theory::EFFORT_LAST_CALL ){
1225 //with bounded integers, skip every other last call,
1226 // since matching loops may occur with infinite quantification
1227 if( d_ierCounter_lc%2==0 && options::fmfBoundInt() ){
1228 performCheck = false;
1229 }
1230 }
1231 return performCheck;
1232 }
1233
1234 quantifiers::UserPatMode QuantifiersEngine::getInstUserPatMode() {
1235 if( options::userPatternsQuant()==quantifiers::USER_PAT_MODE_INTERLEAVE ){
1236 return d_ierCounter%2==0 ? quantifiers::USER_PAT_MODE_USE : quantifiers::USER_PAT_MODE_RESORT;
1237 }else{
1238 return options::userPatternsQuant();
1239 }
1240 }
1241
1242 void QuantifiersEngine::flushLemmas(){
1243 if( !d_lemmas_waiting.empty() ){
1244 //filter based on notify classes
1245 if( !d_inst_notify.empty() ){
1246 unsigned prev_lem_sz = d_lemmas_waiting.size();
1247 for( unsigned j=0; j<d_inst_notify.size(); j++ ){
1248 d_inst_notify[j]->filterInstantiations();
1249 }
1250 if( prev_lem_sz!=d_lemmas_waiting.size() ){
1251 Trace("quant-engine") << "...filtered instances : " << d_lemmas_waiting.size() << " / " << prev_lem_sz << std::endl;
1252 }
1253 }
1254 //take default output channel if none is provided
1255 d_hasAddedLemma = true;
1256 for( unsigned i=0; i<d_lemmas_waiting.size(); i++ ){
1257 Trace("qe-lemma") << "Lemma : " << d_lemmas_waiting[i] << std::endl;
1258 getOutputChannel().lemma( d_lemmas_waiting[i], false, true );
1259 }
1260 d_lemmas_waiting.clear();
1261 }
1262 if( !d_phase_req_waiting.empty() ){
1263 for( std::map< Node, bool >::iterator it = d_phase_req_waiting.begin(); it != d_phase_req_waiting.end(); ++it ){
1264 Trace("qe-lemma") << "Require phase : " << it->first << " -> " << it->second << std::endl;
1265 getOutputChannel().requirePhase( it->first, it->second );
1266 }
1267 d_phase_req_waiting.clear();
1268 }
1269 }
1270
1271 void QuantifiersEngine::printInstantiations( std::ostream& out ) {
1272 bool printed = false;
1273 for( BoolMap::iterator it = d_skolemized.begin(); it != d_skolemized.end(); ++it ){
1274 Node q = (*it).first;
1275 printed = true;
1276 out << "Skolem constants of " << q << " : " << std::endl;
1277 out << " ( ";
1278 for( unsigned i=0; i<d_term_db->d_skolem_constants[q].size(); i++ ){
1279 if( i>0 ){ out << ", "; }
1280 out << d_term_db->d_skolem_constants[q][i];
1281 }
1282 out << " )" << std::endl;
1283 out << std::endl;
1284 }
1285 if( options::incrementalSolving() ){
1286 for( std::map< Node, inst::CDInstMatchTrie* >::iterator it = d_c_inst_match_trie.begin(); it != d_c_inst_match_trie.end(); ++it ){
1287 printed = true;
1288 out << "Instantiations of " << it->first << " : " << std::endl;
1289 it->second->print( out, it->first );
1290 }
1291 }else{
1292 for( std::map< Node, inst::InstMatchTrie >::iterator it = d_inst_match_trie.begin(); it != d_inst_match_trie.end(); ++it ){
1293 printed = true;
1294 out << "Instantiations of " << it->first << " : " << std::endl;
1295 it->second.print( out, it->first );
1296 out << std::endl;
1297 }
1298 }
1299 if( !printed ){
1300 out << "No instantiations." << std::endl;
1301 }
1302 }
1303
1304 void QuantifiersEngine::printSynthSolution( std::ostream& out ) {
1305 if( d_ceg_inst ){
1306 d_ceg_inst->printSynthSolution( out );
1307 }else{
1308 out << "Internal error : module for synth solution not found." << std::endl;
1309 }
1310 }
1311
1312 void QuantifiersEngine::getInstantiations( std::map< Node, std::vector< Node > >& insts ) {
1313 if( options::incrementalSolving() ){
1314 for( std::map< Node, inst::CDInstMatchTrie* >::iterator it = d_c_inst_match_trie.begin(); it != d_c_inst_match_trie.end(); ++it ){
1315 it->second->getInstantiations( insts[it->first], it->first, this );
1316 }
1317 }else{
1318 for( std::map< Node, inst::InstMatchTrie >::iterator it = d_inst_match_trie.begin(); it != d_inst_match_trie.end(); ++it ){
1319 it->second.getInstantiations( insts[it->first], it->first, this );
1320 }
1321 }
1322 }
1323
1324 QuantifiersEngine::Statistics::Statistics()
1325 : d_time("theory::QuantifiersEngine::time"),
1326 d_num_quant("QuantifiersEngine::Num_Quantifiers", 0),
1327 d_instantiation_rounds("QuantifiersEngine::Rounds_Instantiation_Full", 0),
1328 d_instantiation_rounds_lc("QuantifiersEngine::Rounds_Instantiation_Last_Call", 0),
1329 d_instantiations("QuantifiersEngine::Instantiations_Total", 0),
1330 d_inst_duplicate("QuantifiersEngine::Duplicate_Inst", 0),
1331 d_inst_duplicate_eq("QuantifiersEngine::Duplicate_Inst_Eq", 0),
1332 d_triggers("QuantifiersEngine::Triggers", 0),
1333 d_simple_triggers("QuantifiersEngine::Triggers_Simple", 0),
1334 d_multi_triggers("QuantifiersEngine::Triggers_Multi", 0),
1335 d_multi_trigger_instantiations("QuantifiersEngine::Multi_Trigger_Instantiations", 0),
1336 d_red_alpha_equiv("QuantifiersEngine::Reductions_Alpha_Equivalence", 0),
1337 d_red_lte_partial_inst("QuantifiersEngine::Reductions_Lte_Partial_Inst", 0),
1338 d_instantiations_user_patterns("QuantifiersEngine::Instantiations_User_Patterns", 0),
1339 d_instantiations_auto_gen("QuantifiersEngine::Instantiations_Auto_Gen", 0),
1340 d_instantiations_guess("QuantifiersEngine::Instantiations_Guess", 0),
1341 d_instantiations_cbqi_arith("QuantifiersEngine::Instantiations_Cbqi_Arith", 0)
1342 {
1343 smtStatisticsRegistry()->registerStat(&d_time);
1344 smtStatisticsRegistry()->registerStat(&d_num_quant);
1345 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds);
1346 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds_lc);
1347 smtStatisticsRegistry()->registerStat(&d_instantiations);
1348 smtStatisticsRegistry()->registerStat(&d_inst_duplicate);
1349 smtStatisticsRegistry()->registerStat(&d_inst_duplicate_eq);
1350 smtStatisticsRegistry()->registerStat(&d_triggers);
1351 smtStatisticsRegistry()->registerStat(&d_simple_triggers);
1352 smtStatisticsRegistry()->registerStat(&d_multi_triggers);
1353 smtStatisticsRegistry()->registerStat(&d_multi_trigger_instantiations);
1354 smtStatisticsRegistry()->registerStat(&d_red_alpha_equiv);
1355 smtStatisticsRegistry()->registerStat(&d_red_lte_partial_inst);
1356 smtStatisticsRegistry()->registerStat(&d_instantiations_user_patterns);
1357 smtStatisticsRegistry()->registerStat(&d_instantiations_auto_gen);
1358 smtStatisticsRegistry()->registerStat(&d_instantiations_guess);
1359 smtStatisticsRegistry()->registerStat(&d_instantiations_cbqi_arith);
1360 }
1361
1362 QuantifiersEngine::Statistics::~Statistics(){
1363 smtStatisticsRegistry()->unregisterStat(&d_time);
1364 smtStatisticsRegistry()->unregisterStat(&d_num_quant);
1365 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds);
1366 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds_lc);
1367 smtStatisticsRegistry()->unregisterStat(&d_instantiations);
1368 smtStatisticsRegistry()->unregisterStat(&d_inst_duplicate);
1369 smtStatisticsRegistry()->unregisterStat(&d_inst_duplicate_eq);
1370 smtStatisticsRegistry()->unregisterStat(&d_triggers);
1371 smtStatisticsRegistry()->unregisterStat(&d_simple_triggers);
1372 smtStatisticsRegistry()->unregisterStat(&d_multi_triggers);
1373 smtStatisticsRegistry()->unregisterStat(&d_multi_trigger_instantiations);
1374 smtStatisticsRegistry()->unregisterStat(&d_red_alpha_equiv);
1375 smtStatisticsRegistry()->unregisterStat(&d_red_lte_partial_inst);
1376 smtStatisticsRegistry()->unregisterStat(&d_instantiations_user_patterns);
1377 smtStatisticsRegistry()->unregisterStat(&d_instantiations_auto_gen);
1378 smtStatisticsRegistry()->unregisterStat(&d_instantiations_guess);
1379 smtStatisticsRegistry()->unregisterStat(&d_instantiations_cbqi_arith);
1380 }
1381
1382 eq::EqualityEngine* QuantifiersEngine::getMasterEqualityEngine(){
1383 return getTheoryEngine()->getMasterEqualityEngine();
1384 }
1385
1386 void QuantifiersEngine::debugPrintEqualityEngine( const char * c ) {
1387 eq::EqualityEngine* ee = getMasterEqualityEngine();
1388 eq::EqClassesIterator eqcs_i = eq::EqClassesIterator( ee );
1389 std::map< TypeNode, int > typ_num;
1390 while( !eqcs_i.isFinished() ){
1391 TNode r = (*eqcs_i);
1392 TypeNode tr = r.getType();
1393 if( typ_num.find( tr )==typ_num.end() ){
1394 typ_num[tr] = 0;
1395 }
1396 typ_num[tr]++;
1397 bool firstTime = true;
1398 Trace(c) << " " << r;
1399 Trace(c) << " : { ";
1400 eq::EqClassIterator eqc_i = eq::EqClassIterator( r, ee );
1401 while( !eqc_i.isFinished() ){
1402 TNode n = (*eqc_i);
1403 if( r!=n ){
1404 if( firstTime ){
1405 Trace(c) << std::endl;
1406 firstTime = false;
1407 }
1408 Trace(c) << " " << n << std::endl;
1409 }
1410 ++eqc_i;
1411 }
1412 if( !firstTime ){ Trace(c) << " "; }
1413 Trace(c) << "}" << std::endl;
1414 ++eqcs_i;
1415 }
1416 Trace(c) << std::endl;
1417 for( std::map< TypeNode, int >::iterator it = typ_num.begin(); it != typ_num.end(); ++it ){
1418 Trace(c) << "# eqc for " << it->first << " : " << it->second << std::endl;
1419 }
1420 }
1421
1422
1423 EqualityQueryQuantifiersEngine::EqualityQueryQuantifiersEngine( context::Context* c, QuantifiersEngine* qe ) : d_qe( qe ), d_eqi_counter( c ), d_reset_count( 0 ){
1424 if( options::inferArithTriggerEq() ){
1425 d_eq_inference = new quantifiers::EqualityInference( c, options::inferArithTriggerEqExp() );
1426 }else{
1427 d_eq_inference = NULL;
1428 }
1429 }
1430
1431 EqualityQueryQuantifiersEngine::~EqualityQueryQuantifiersEngine(){
1432 delete d_eq_inference;
1433 }
1434
1435 bool EqualityQueryQuantifiersEngine::reset( Theory::Effort e ){
1436 d_int_rep.clear();
1437 d_reset_count++;
1438 return processInferences( e );
1439 }
1440
1441 bool EqualityQueryQuantifiersEngine::processInferences( Theory::Effort e ) {
1442 if( options::inferArithTriggerEq() ){
1443 eq::EqualityEngine* ee = getEngine();
1444 //updated implementation
1445 while( d_eqi_counter.get()<d_eq_inference->getNumPendingMerges() ){
1446 Node eq = d_eq_inference->getPendingMerge( d_eqi_counter.get() );
1447 Node eq_exp = d_eq_inference->getPendingMergeExplanation( d_eqi_counter.get() );
1448 Trace("quant-engine-ee-proc") << "processInferences : Infer : " << eq << std::endl;
1449 Trace("quant-engine-ee-proc") << " explanation : " << eq_exp << std::endl;
1450 Assert( ee->hasTerm( eq[0] ) );
1451 Assert( ee->hasTerm( eq[1] ) );
1452 if( areDisequal( eq[0], eq[1] ) ){
1453 Trace("quant-engine-ee-proc") << "processInferences : Conflict : " << eq << std::endl;
1454 if( Trace.isOn("term-db-lemma") ){
1455 Trace("term-db-lemma") << "Disequal terms, equal by normalization : " << eq[0] << " " << eq[1] << "!!!!" << std::endl;
1456 if( !d_qe->getTheoryEngine()->needCheck() ){
1457 Trace("term-db-lemma") << " all theories passed with no lemmas." << std::endl;
1458 //this should really never happen (implies arithmetic is incomplete when sharing is enabled)
1459 Assert( false );
1460 }
1461 Trace("term-db-lemma") << " add split on : " << eq << std::endl;
1462 }
1463 d_qe->addSplit( eq );
1464 return false;
1465 }else{
1466 ee->assertEquality( eq, true, eq_exp );
1467 d_eqi_counter = d_eqi_counter.get() + 1;
1468 }
1469 }
1470 Assert( ee->consistent() );
1471 }
1472 return true;
1473 }
1474
1475 bool EqualityQueryQuantifiersEngine::hasTerm( Node a ){
1476 return getEngine()->hasTerm( a );
1477 }
1478
1479 Node EqualityQueryQuantifiersEngine::getRepresentative( Node a ){
1480 eq::EqualityEngine* ee = getEngine();
1481 if( ee->hasTerm( a ) ){
1482 return ee->getRepresentative( a );
1483 }else{
1484 return a;
1485 }
1486 }
1487
1488 bool EqualityQueryQuantifiersEngine::areEqual( Node a, Node b ){
1489 if( a==b ){
1490 return true;
1491 }else{
1492 eq::EqualityEngine* ee = getEngine();
1493 if( ee->hasTerm( a ) && ee->hasTerm( b ) ){
1494 return ee->areEqual( a, b );
1495 }else{
1496 return false;
1497 }
1498 }
1499 }
1500
1501 bool EqualityQueryQuantifiersEngine::areDisequal( Node a, Node b ){
1502 if( a==b ){
1503 return false;
1504 }else{
1505 eq::EqualityEngine* ee = getEngine();
1506 if( ee->hasTerm( a ) && ee->hasTerm( b ) ){
1507 return ee->areDisequal( a, b, false );
1508 }else{
1509 return a.isConst() && b.isConst();
1510 }
1511 }
1512 }
1513
1514 Node EqualityQueryQuantifiersEngine::getInternalRepresentative( Node a, Node f, int index ){
1515 Assert( f.isNull() || f.getKind()==FORALL );
1516 Node r = getRepresentative( a );
1517 if( options::finiteModelFind() ){
1518 if( r.isConst() && quantifiers::TermDb::containsUninterpretedConstant( r ) ){
1519 //map back from values assigned by model, if any
1520 if( d_qe->getModel() ){
1521 std::map< Node, Node >::iterator it = d_qe->getModel()->d_rep_set.d_values_to_terms.find( r );
1522 if( it!=d_qe->getModel()->d_rep_set.d_values_to_terms.end() ){
1523 r = it->second;
1524 r = getRepresentative( r );
1525 }else{
1526 if( r.getType().isSort() ){
1527 Trace("internal-rep-warn") << "No representative for UF constant." << std::endl;
1528 //should never happen : UF constants should never escape model
1529 Assert( false );
1530 }
1531 }
1532 }
1533 }
1534 }
1535 if( options::quantRepMode()==quantifiers::QUANT_REP_MODE_EE ){
1536 return r;
1537 }else{
1538 TypeNode v_tn = f.isNull() ? a.getType() : f[0][index].getType();
1539 std::map< Node, Node >::iterator itir = d_int_rep[v_tn].find( r );
1540 if( itir==d_int_rep[v_tn].end() ){
1541 //find best selection for representative
1542 Node r_best;
1543 //if( options::fmfRelevantDomain() && !f.isNull() ){
1544 // Trace("internal-rep-debug") << "Consult relevant domain to mkRep " << r << std::endl;
1545 // r_best = d_qe->getRelevantDomain()->getRelevantTerm( f, index, r );
1546 // Trace("internal-rep-debug") << "Returned " << r_best << " " << r << std::endl;
1547 //}
1548 std::vector< Node > eqc;
1549 getEquivalenceClass( r, eqc );
1550 Trace("internal-rep-select") << "Choose representative for equivalence class : { ";
1551 for( unsigned i=0; i<eqc.size(); i++ ){
1552 if( i>0 ) Trace("internal-rep-select") << ", ";
1553 Trace("internal-rep-select") << eqc[i];
1554 }
1555 Trace("internal-rep-select") << " }, type = " << v_tn << std::endl;
1556 int r_best_score = -1;
1557 for( size_t i=0; i<eqc.size(); i++ ){
1558 int score = getRepScore( eqc[i], f, index, v_tn );
1559 if( score!=-2 ){
1560 if( r_best.isNull() || ( score>=0 && ( r_best_score<0 || score<r_best_score ) ) ){
1561 r_best = eqc[i];
1562 r_best_score = score;
1563 }
1564 }
1565 }
1566 if( r_best.isNull() ){
1567 Trace("internal-rep-warn") << "No valid choice for representative in eqc class." << std::endl;
1568 r_best = r;
1569 }
1570 //now, make sure that no other member of the class is an instance
1571 std::hash_map<TNode, Node, TNodeHashFunction> cache;
1572 r_best = getInstance( r_best, eqc, cache );
1573 //store that this representative was chosen at this point
1574 if( d_rep_score.find( r_best )==d_rep_score.end() ){
1575 d_rep_score[ r_best ] = d_reset_count;
1576 }
1577 Trace("internal-rep-select") << "...Choose " << r_best << " with score " << r_best_score << std::endl;
1578 Assert( r_best.getType().isSubtypeOf( v_tn ) );
1579 d_int_rep[v_tn][r] = r_best;
1580 if( r_best!=a ){
1581 Trace("internal-rep-debug") << "rep( " << a << " ) = " << r << ", " << std::endl;
1582 Trace("internal-rep-debug") << "int_rep( " << a << " ) = " << r_best << ", " << std::endl;
1583 }
1584 return r_best;
1585 }else{
1586 return itir->second;
1587 }
1588 }
1589 }
1590
1591 void EqualityQueryQuantifiersEngine::flattenRepresentatives( std::map< TypeNode, std::vector< Node > >& reps ) {
1592 //make sure internal representatives currently exist
1593 for( std::map< TypeNode, std::vector< Node > >::iterator it = reps.begin(); it != reps.end(); ++it ){
1594 if( it->first.isSort() ){
1595 for( unsigned i=0; i<it->second.size(); i++ ){
1596 Node r = getInternalRepresentative( it->second[i], Node::null(), 0 );
1597 }
1598 }
1599 }
1600 Trace("internal-rep-flatten") << "---Flattening representatives : " << std::endl;
1601 for( std::map< TypeNode, std::map< Node, Node > >::iterator itt = d_int_rep.begin(); itt != d_int_rep.end(); ++itt ){
1602 for( std::map< Node, Node >::iterator it = itt->second.begin(); it != itt->second.end(); ++it ){
1603 Trace("internal-rep-flatten") << itt->first << " : irep( " << it->first << " ) = " << it->second << std::endl;
1604 }
1605 }
1606 //store representatives for newly created terms
1607 std::map< Node, Node > temp_rep_map;
1608
1609 bool success;
1610 do {
1611 success = false;
1612 for( std::map< TypeNode, std::map< Node, Node > >::iterator itt = d_int_rep.begin(); itt != d_int_rep.end(); ++itt ){
1613 for( std::map< Node, Node >::iterator it = itt->second.begin(); it != itt->second.end(); ++it ){
1614 if( it->second.getKind()==APPLY_UF && it->second.getType().isSort() ){
1615 Node ni = it->second;
1616 std::vector< Node > cc;
1617 cc.push_back( it->second.getOperator() );
1618 bool changed = false;
1619 for( unsigned j=0; j<ni.getNumChildren(); j++ ){
1620 if( ni[j].getType().isSort() ){
1621 Node r = getRepresentative( ni[j] );
1622 if( itt->second.find( r )==itt->second.end() ){
1623 Assert( temp_rep_map.find( r )!=temp_rep_map.end() );
1624 r = temp_rep_map[r];
1625 }
1626 if( r==ni ){
1627 //found sub-term as instance
1628 Trace("internal-rep-flatten-debug") << "...Changed " << it->second << " to subterm " << ni[j] << std::endl;
1629 itt->second[it->first] = ni[j];
1630 changed = false;
1631 success = true;
1632 break;
1633 }else{
1634 Node ir = itt->second[r];
1635 cc.push_back( ir );
1636 if( ni[j]!=ir ){
1637 changed = true;
1638 }
1639 }
1640 }else{
1641 changed = false;
1642 break;
1643 }
1644 }
1645 if( changed ){
1646 Node n = NodeManager::currentNM()->mkNode( APPLY_UF, cc );
1647 Trace("internal-rep-flatten-debug") << "...Changed " << it->second << " to " << n << std::endl;
1648 success = true;
1649 itt->second[it->first] = n;
1650 temp_rep_map[n] = it->first;
1651 }
1652 }
1653 }
1654 }
1655 }while( success );
1656 Trace("internal-rep-flatten") << "---After flattening : " << std::endl;
1657 for( std::map< TypeNode, std::map< Node, Node > >::iterator itt = d_int_rep.begin(); itt != d_int_rep.end(); ++itt ){
1658 for( std::map< Node, Node >::iterator it = itt->second.begin(); it != itt->second.end(); ++it ){
1659 Trace("internal-rep-flatten") << itt->first << " : irep( " << it->first << " ) = " << it->second << std::endl;
1660 }
1661 }
1662 }
1663
1664 eq::EqualityEngine* EqualityQueryQuantifiersEngine::getEngine(){
1665 return d_qe->getMasterEqualityEngine();
1666 }
1667
1668 void EqualityQueryQuantifiersEngine::getEquivalenceClass( Node a, std::vector< Node >& eqc ){
1669 eq::EqualityEngine* ee = getEngine();
1670 if( ee->hasTerm( a ) ){
1671 Node rep = ee->getRepresentative( a );
1672 eq::EqClassIterator eqc_iter( rep, ee );
1673 while( !eqc_iter.isFinished() ){
1674 eqc.push_back( *eqc_iter );
1675 eqc_iter++;
1676 }
1677 }else{
1678 eqc.push_back( a );
1679 }
1680 //a should be in its equivalence class
1681 Assert( std::find( eqc.begin(), eqc.end(), a )!=eqc.end() );
1682 }
1683
1684 TNode EqualityQueryQuantifiersEngine::getCongruentTerm( Node f, std::vector< TNode >& args ) {
1685 return d_qe->getTermDatabase()->getCongruentTerm( f, args );
1686 }
1687
1688 //helper functions
1689
1690 Node EqualityQueryQuantifiersEngine::getInstance( Node n, const std::vector< Node >& eqc, std::hash_map<TNode, Node, TNodeHashFunction>& cache ){
1691 if(cache.find(n) != cache.end()) {
1692 return cache[n];
1693 }
1694 for( size_t i=0; i<n.getNumChildren(); i++ ){
1695 Node nn = getInstance( n[i], eqc, cache );
1696 if( !nn.isNull() ){
1697 return cache[n] = nn;
1698 }
1699 }
1700 if( std::find( eqc.begin(), eqc.end(), n )!=eqc.end() ){
1701 return cache[n] = n;
1702 }else{
1703 return cache[n] = Node::null();
1704 }
1705 }
1706
1707 //-2 : invalid, -1 : undesired, otherwise : smaller the score, the better
1708 int EqualityQueryQuantifiersEngine::getRepScore( Node n, Node f, int index, TypeNode v_tn ){
1709 if( options::cbqi() && quantifiers::TermDb::hasInstConstAttr(n) ){ //reject
1710 return -2;
1711 }else if( !n.getType().isSubtypeOf( v_tn ) ){ //reject if incorrect type
1712 return -2;
1713 }else if( options::lteRestrictInstClosure() && ( !d_qe->getTermDatabase()->isInstClosure( n ) || !d_qe->getTermDatabase()->hasTermCurrent( n, false ) ) ){
1714 return -1;
1715 }else if( options::instMaxLevel()!=-1 ){
1716 //score prefer lowest instantiation level
1717 if( n.hasAttribute(InstLevelAttribute()) ){
1718 return n.getAttribute(InstLevelAttribute());
1719 }else{
1720 return options::instLevelInputOnly() ? -1 : 0;
1721 }
1722 }else{
1723 if( options::quantRepMode()==quantifiers::QUANT_REP_MODE_FIRST ){
1724 //score prefers earliest use of this term as a representative
1725 return d_rep_score.find( n )==d_rep_score.end() ? -1 : d_rep_score[n];
1726 }else{
1727 Assert( options::quantRepMode()==quantifiers::QUANT_REP_MODE_DEPTH );
1728 return quantifiers::TermDb::getTermDepth( n );
1729 }
1730 }
1731 }