FloatingPoint: Separate out symFPU glue code. (#5492)
[cvc5.git] / src / theory / quantifiers_engine.cpp
1 /********************* */
2 /*! \file quantifiers_engine.cpp
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Morgan Deters, Mathias Preiner
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 by the authors listed in the file AUTHORS
8 ** in the top-level source directory and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
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_engine_scope.h"
20 #include "smt/smt_statistics_registry.h"
21 #include "theory/quantifiers/fmf/full_model_check.h"
22 #include "theory/quantifiers/quantifiers_modules.h"
23 #include "theory/quantifiers/quantifiers_rewriter.h"
24 #include "theory/theory_engine.h"
25 #include "theory/uf/equality_engine.h"
26
27 using namespace std;
28 using namespace CVC4::kind;
29
30 namespace CVC4 {
31 namespace theory {
32
33 QuantifiersEngine::QuantifiersEngine(TheoryEngine* te,
34 DecisionManager& dm,
35 ProofNodeManager* pnm)
36 : d_te(te),
37 d_context(te->getSatContext()),
38 d_userContext(te->getUserContext()),
39 d_decManager(dm),
40 d_masterEqualityEngine(nullptr),
41 d_eq_query(
42 new quantifiers::EqualityQueryQuantifiersEngine(d_context, this)),
43 d_tr_trie(new inst::TriggerTrie),
44 d_model(nullptr),
45 d_builder(nullptr),
46 d_qepr(nullptr),
47 d_term_util(new quantifiers::TermUtil(this)),
48 d_term_canon(new expr::TermCanonize),
49 d_term_db(new quantifiers::TermDb(d_context, d_userContext, this)),
50 d_sygus_tdb(nullptr),
51 d_quant_attr(new quantifiers::QuantAttributes(this)),
52 d_instantiate(new quantifiers::Instantiate(this, d_userContext, pnm)),
53 d_skolemize(new quantifiers::Skolemize(this, d_userContext, pnm)),
54 d_term_enum(new quantifiers::TermEnumeration),
55 d_conflict_c(d_context, false),
56 d_quants_prereg(d_userContext),
57 d_quants_red(d_userContext),
58 d_lemmas_produced_c(d_userContext),
59 d_ierCounter_c(d_context),
60 d_presolve(d_userContext, true),
61 d_presolve_in(d_userContext),
62 d_presolve_cache(d_userContext),
63 d_presolve_cache_wq(d_userContext),
64 d_presolve_cache_wic(d_userContext)
65 {
66 //---- utilities
67 d_util.push_back(d_eq_query.get());
68 // term util must come before the other utilities
69 d_util.push_back(d_term_util.get());
70 d_util.push_back(d_term_db.get());
71
72 if (options::sygus() || options::sygusInst())
73 {
74 // must be constructed here since it is required for datatypes finistInit
75 d_sygus_tdb.reset(new quantifiers::TermDbSygus(d_context, this));
76 }
77
78 d_util.push_back(d_instantiate.get());
79
80 d_curr_effort_level = QuantifiersModule::QEFFORT_NONE;
81 d_conflict = false;
82 d_hasAddedLemma = false;
83 //don't add true lemma
84 d_lemmas_produced_c[d_term_util->d_true] = true;
85
86 Trace("quant-engine-debug") << "Initialize quantifiers engine." << std::endl;
87 Trace("quant-engine-debug") << "Initialize model, mbqi : " << options::mbqiMode() << std::endl;
88
89 if( options::quantEpr() ){
90 Assert(!options::incrementalSolving());
91 d_qepr.reset(new quantifiers::QuantEPR);
92 }
93 //---- end utilities
94
95 //allow theory combination to go first, once initially
96 d_ierCounter = options::instWhenTcFirst() ? 0 : 1;
97 d_ierCounter_c = d_ierCounter;
98 d_ierCounter_lc = 0;
99 d_ierCounterLastLc = 0;
100 d_inst_when_phase = 1 + ( options::instWhenPhase()<1 ? 1 : options::instWhenPhase() );
101
102 // Finite model finding requires specialized ways of building the model.
103 // We require constructing the model and model builder here, since it is
104 // required for initializing the CombinationEngine.
105 if (options::finiteModelFind() || options::fmfBound())
106 {
107 Trace("quant-engine-debug") << "Initialize model engine, mbqi : " << options::mbqiMode() << " " << options::fmfBound() << std::endl;
108 if (options::mbqiMode() == options::MbqiMode::FMC
109 || options::mbqiMode() == options::MbqiMode::TRUST
110 || options::fmfBound())
111 {
112 Trace("quant-engine-debug") << "...make fmc builder." << std::endl;
113 d_model.reset(new quantifiers::fmcheck::FirstOrderModelFmc(
114 this, d_context, "FirstOrderModelFmc"));
115 d_builder.reset(
116 new quantifiers::fmcheck::FullModelChecker(d_context, this));
117 }else{
118 Trace("quant-engine-debug") << "...make default model builder." << std::endl;
119 d_model.reset(
120 new quantifiers::FirstOrderModel(this, d_context, "FirstOrderModel"));
121 d_builder.reset(new quantifiers::QModelBuilder(d_context, this));
122 }
123 }else{
124 d_model.reset(
125 new quantifiers::FirstOrderModel(this, d_context, "FirstOrderModel"));
126 }
127 }
128
129 QuantifiersEngine::~QuantifiersEngine() {}
130
131 void QuantifiersEngine::finishInit()
132 {
133 // Initialize the modules and the utilities here. We delay their
134 // initialization to here, since this is after TheoryQuantifiers finishInit,
135 // which has initialized the state and inference manager of this engine.
136 d_qmodules.reset(new quantifiers::QuantifiersModules);
137 d_qmodules->initialize(this, d_context, d_modules);
138 if (d_qmodules->d_rel_dom.get())
139 {
140 d_util.push_back(d_qmodules->d_rel_dom.get());
141 }
142 }
143
144 void QuantifiersEngine::setMasterEqualityEngine(eq::EqualityEngine* mee)
145 {
146 d_masterEqualityEngine = mee;
147 }
148
149 TheoryEngine* QuantifiersEngine::getTheoryEngine() const { return d_te; }
150
151 DecisionManager* QuantifiersEngine::getDecisionManager()
152 {
153 return &d_decManager;
154 }
155
156 context::Context* QuantifiersEngine::getSatContext() { return d_context; }
157
158 context::UserContext* QuantifiersEngine::getUserContext()
159 {
160 return d_userContext;
161 }
162
163 OutputChannel& QuantifiersEngine::getOutputChannel()
164 {
165 return d_te->theoryOf(THEORY_QUANTIFIERS)->getOutputChannel();
166 }
167 /** get default valuation for the quantifiers engine */
168 Valuation& QuantifiersEngine::getValuation()
169 {
170 return d_te->theoryOf(THEORY_QUANTIFIERS)->getValuation();
171 }
172
173 const LogicInfo& QuantifiersEngine::getLogicInfo() const
174 {
175 return d_te->getLogicInfo();
176 }
177
178 EqualityQuery* QuantifiersEngine::getEqualityQuery() const
179 {
180 return d_eq_query.get();
181 }
182 quantifiers::QModelBuilder* QuantifiersEngine::getModelBuilder() const
183 {
184 return d_builder.get();
185 }
186 quantifiers::QuantEPR* QuantifiersEngine::getQuantEPR() const
187 {
188 return d_qepr.get();
189 }
190 quantifiers::FirstOrderModel* QuantifiersEngine::getModel() const
191 {
192 return d_model.get();
193 }
194 quantifiers::TermDb* QuantifiersEngine::getTermDatabase() const
195 {
196 return d_term_db.get();
197 }
198 quantifiers::TermDbSygus* QuantifiersEngine::getTermDatabaseSygus() const
199 {
200 return d_sygus_tdb.get();
201 }
202 quantifiers::TermUtil* QuantifiersEngine::getTermUtil() const
203 {
204 return d_term_util.get();
205 }
206 expr::TermCanonize* QuantifiersEngine::getTermCanonize() const
207 {
208 return d_term_canon.get();
209 }
210 quantifiers::QuantAttributes* QuantifiersEngine::getQuantAttributes() const
211 {
212 return d_quant_attr.get();
213 }
214 quantifiers::Instantiate* QuantifiersEngine::getInstantiate() const
215 {
216 return d_instantiate.get();
217 }
218 quantifiers::Skolemize* QuantifiersEngine::getSkolemize() const
219 {
220 return d_skolemize.get();
221 }
222 quantifiers::TermEnumeration* QuantifiersEngine::getTermEnumeration() const
223 {
224 return d_term_enum.get();
225 }
226 inst::TriggerTrie* QuantifiersEngine::getTriggerDatabase() const
227 {
228 return d_tr_trie.get();
229 }
230
231 QuantifiersModule * QuantifiersEngine::getOwner( Node q ) {
232 std::map< Node, QuantifiersModule * >::iterator it = d_owner.find( q );
233 if( it==d_owner.end() ){
234 return NULL;
235 }else{
236 return it->second;
237 }
238 }
239
240 void QuantifiersEngine::setOwner( Node q, QuantifiersModule * m, int priority ) {
241 QuantifiersModule * mo = getOwner( q );
242 if( mo!=m ){
243 if( mo!=NULL ){
244 if( priority<=d_owner_priority[q] ){
245 Trace("quant-warn") << "WARNING: setting owner of " << q << " to " << ( m ? m->identify() : "null" ) << ", but already has owner " << mo->identify() << " with higher priority!" << std::endl;
246 return;
247 }
248 }
249 d_owner[q] = m;
250 d_owner_priority[q] = priority;
251 }
252 }
253
254 void QuantifiersEngine::setOwner(Node q, quantifiers::QAttributes& qa)
255 {
256 if (qa.d_sygus || (options::sygusRecFun() && !qa.d_fundef_f.isNull()))
257 {
258 if (d_qmodules->d_synth_e.get() == nullptr)
259 {
260 Trace("quant-warn") << "WARNING : synth engine is null, and we have : "
261 << q << std::endl;
262 }
263 // set synth engine as owner since this is either a conjecture or a function
264 // definition to be used by sygus
265 setOwner(q, d_qmodules->d_synth_e.get(), 2);
266 }
267 }
268
269 bool QuantifiersEngine::hasOwnership( Node q, QuantifiersModule * m ) {
270 QuantifiersModule * mo = getOwner( q );
271 return mo==m || mo==NULL;
272 }
273
274 bool QuantifiersEngine::isFiniteBound(Node q, Node v) const
275 {
276 quantifiers::BoundedIntegers* bi = d_qmodules->d_bint.get();
277 if (bi && bi->isBound(q, v))
278 {
279 return true;
280 }
281 TypeNode tn = v.getType();
282 if (tn.isSort() && options::finiteModelFind())
283 {
284 return true;
285 }
286 else if (d_term_enum->mayComplete(tn))
287 {
288 return true;
289 }
290 return false;
291 }
292
293 BoundVarType QuantifiersEngine::getBoundVarType(Node q, Node v) const
294 {
295 quantifiers::BoundedIntegers* bi = d_qmodules->d_bint.get();
296 if (bi)
297 {
298 return bi->getBoundVarType(q, v);
299 }
300 return isFiniteBound(q, v) ? BOUND_FINITE : BOUND_NONE;
301 }
302
303 void QuantifiersEngine::getBoundVarIndices(Node q,
304 std::vector<unsigned>& indices) const
305 {
306 Assert(indices.empty());
307 // we take the bounded variables first
308 quantifiers::BoundedIntegers* bi = d_qmodules->d_bint.get();
309 if (bi)
310 {
311 bi->getBoundVarIndices(q, indices);
312 }
313 // then get the remaining ones
314 for (unsigned i = 0, nvars = q[0].getNumChildren(); i < nvars; i++)
315 {
316 if (std::find(indices.begin(), indices.end(), i) == indices.end())
317 {
318 indices.push_back(i);
319 }
320 }
321 }
322
323 bool QuantifiersEngine::getBoundElements(RepSetIterator* rsi,
324 bool initial,
325 Node q,
326 Node v,
327 std::vector<Node>& elements) const
328 {
329 quantifiers::BoundedIntegers* bi = d_qmodules->d_bint.get();
330 if (bi)
331 {
332 return bi->getBoundElements(rsi, initial, q, v, elements);
333 }
334 return false;
335 }
336
337 void QuantifiersEngine::presolve() {
338 Trace("quant-engine-proc") << "QuantifiersEngine : presolve " << std::endl;
339 for( unsigned i=0; i<d_modules.size(); i++ ){
340 d_modules[i]->presolve();
341 }
342 d_term_db->presolve();
343 d_presolve = false;
344 //add all terms to database
345 if( options::incrementalSolving() ){
346 Trace("quant-engine-proc") << "Add presolve cache " << d_presolve_cache.size() << std::endl;
347 for( unsigned i=0; i<d_presolve_cache.size(); i++ ){
348 addTermToDatabase( d_presolve_cache[i], d_presolve_cache_wq[i], d_presolve_cache_wic[i] );
349 }
350 Trace("quant-engine-proc") << "Done add presolve cache " << std::endl;
351 }
352 }
353
354 void QuantifiersEngine::ppNotifyAssertions(
355 const std::vector<Node>& assertions) {
356 Trace("quant-engine-proc")
357 << "ppNotifyAssertions in QE, #assertions = " << assertions.size()
358 << " check epr = " << (d_qepr != NULL) << std::endl;
359 if (options::instLevelInputOnly() && options::instMaxLevel() != -1)
360 {
361 for (const Node& a : assertions)
362 {
363 quantifiers::QuantAttributes::setInstantiationLevelAttr(a, 0);
364 }
365 }
366 if (options::sygus())
367 {
368 quantifiers::SynthEngine* sye = d_qmodules->d_synth_e.get();
369 for (const Node& a : assertions)
370 {
371 sye->preregisterAssertion(a);
372 }
373 }
374 /* The SyGuS instantiation module needs a global view of all available
375 * assertions to collect global terms that get added to each grammar.
376 */
377 if (options::sygusInst())
378 {
379 quantifiers::SygusInst* si = d_qmodules->d_sygus_inst.get();
380 si->ppNotifyAssertions(assertions);
381 }
382 }
383
384 void QuantifiersEngine::check( Theory::Effort e ){
385 CodeTimer codeTimer(d_statistics.d_time);
386
387 if (!getMasterEqualityEngine()->consistent())
388 {
389 Trace("quant-engine-debug") << "Master equality engine not consistent, return." << std::endl;
390 return;
391 }
392 if (d_conflict_c.get())
393 {
394 if (e < Theory::EFFORT_LAST_CALL)
395 {
396 // this can happen in rare cases when quantifiers is the first to realize
397 // there is a quantifier-free conflict, for example, when it discovers
398 // disequal and congruent terms in the master equality engine during
399 // term indexing. In such cases, quantifiers reports a "conflicting lemma"
400 // that is, one that is entailed to be false by the current assignment.
401 // If this lemma is not a SAT conflict, we may get another call to full
402 // effort check and the quantifier-free solvers still haven't realized
403 // there is a conflict. In this case, we return, trusting that theory
404 // combination will do the right thing (split on equalities until there is
405 // a conflict at the quantifier-free level).
406 Trace("quant-engine-debug")
407 << "Conflicting lemma already reported by quantifiers, return."
408 << std::endl;
409 return;
410 }
411 // we reported what we thought was a conflicting lemma, but now we have
412 // gotten a check at LAST_CALL effort, indicating that the lemma we reported
413 // was not conflicting. This should never happen, but in production mode, we
414 // proceed with the check.
415 Assert(false);
416 }
417 bool needsCheck = !d_lemmas_waiting.empty();
418 QuantifiersModule::QEffort needsModelE = QuantifiersModule::QEFFORT_NONE;
419 std::vector< QuantifiersModule* > qm;
420 if( d_model->checkNeeded() ){
421 needsCheck = needsCheck || e>=Theory::EFFORT_LAST_CALL; //always need to check at or above last call
422 for (QuantifiersModule*& mdl : d_modules)
423 {
424 if (mdl->needsCheck(e))
425 {
426 qm.push_back(mdl);
427 needsCheck = true;
428 //can only request model at last call since theory combination can find inconsistencies
429 if( e>=Theory::EFFORT_LAST_CALL ){
430 QuantifiersModule::QEffort me = mdl->needsModel(e);
431 needsModelE = me<needsModelE ? me : needsModelE;
432 }
433 }
434 }
435 }
436
437 d_conflict = false;
438 d_hasAddedLemma = false;
439 bool setIncomplete = false;
440
441 Trace("quant-engine-debug2") << "Quantifiers Engine call to check, level = " << e << ", needsCheck=" << needsCheck << std::endl;
442 if( needsCheck ){
443 //flush previous lemmas (for instance, if was interrupted), or other lemmas to process
444 flushLemmas();
445 if( d_hasAddedLemma ){
446 return;
447 }
448
449 double clSet = 0;
450 if( Trace.isOn("quant-engine") ){
451 clSet = double(clock())/double(CLOCKS_PER_SEC);
452 Trace("quant-engine") << ">>>>> Quantifiers Engine Round, effort = " << e << " <<<<<" << std::endl;
453 }
454
455 if( Trace.isOn("quant-engine-debug") ){
456 Trace("quant-engine-debug") << "Quantifiers Engine check, level = " << e << std::endl;
457 Trace("quant-engine-debug") << " depth : " << d_ierCounter_c << std::endl;
458 Trace("quant-engine-debug") << " modules to check : ";
459 for( unsigned i=0; i<qm.size(); i++ ){
460 Trace("quant-engine-debug") << qm[i]->identify() << " ";
461 }
462 Trace("quant-engine-debug") << std::endl;
463 Trace("quant-engine-debug") << " # quantified formulas = " << d_model->getNumAssertedQuantifiers() << std::endl;
464 if( !d_lemmas_waiting.empty() ){
465 Trace("quant-engine-debug") << " lemmas waiting = " << d_lemmas_waiting.size() << std::endl;
466 }
467 Trace("quant-engine-debug")
468 << " Theory engine finished : " << !theoryEngineNeedsCheck()
469 << std::endl;
470 Trace("quant-engine-debug") << " Needs model effort : " << needsModelE << std::endl;
471 Trace("quant-engine-debug")
472 << " In conflict : " << d_conflict << std::endl;
473 }
474 if( Trace.isOn("quant-engine-ee-pre") ){
475 Trace("quant-engine-ee-pre") << "Equality engine (pre-inference): " << std::endl;
476 debugPrintEqualityEngine( "quant-engine-ee-pre" );
477 }
478 if( Trace.isOn("quant-engine-assert") ){
479 Trace("quant-engine-assert") << "Assertions : " << std::endl;
480 getTheoryEngine()->printAssertions("quant-engine-assert");
481 }
482
483 //reset utilities
484 Trace("quant-engine-debug") << "Resetting all utilities..." << std::endl;
485 for (QuantifiersUtil*& util : d_util)
486 {
487 Trace("quant-engine-debug2") << "Reset " << util->identify().c_str()
488 << "..." << std::endl;
489 if (!util->reset(e))
490 {
491 flushLemmas();
492 if( d_hasAddedLemma ){
493 return;
494 }else{
495 //should only fail reset if added a lemma
496 Assert(false);
497 }
498 }
499 }
500
501 if( Trace.isOn("quant-engine-ee") ){
502 Trace("quant-engine-ee") << "Equality engine : " << std::endl;
503 debugPrintEqualityEngine( "quant-engine-ee" );
504 }
505
506 //reset the model
507 Trace("quant-engine-debug") << "Reset model..." << std::endl;
508 d_model->reset_round();
509
510 //reset the modules
511 Trace("quant-engine-debug") << "Resetting all modules..." << std::endl;
512 for (QuantifiersModule*& mdl : d_modules)
513 {
514 Trace("quant-engine-debug2") << "Reset " << mdl->identify().c_str()
515 << std::endl;
516 mdl->reset_round(e);
517 }
518 Trace("quant-engine-debug") << "Done resetting all modules." << std::endl;
519 //reset may have added lemmas
520 flushLemmas();
521 if( d_hasAddedLemma ){
522 return;
523 }
524
525 if( e==Theory::EFFORT_LAST_CALL ){
526 ++(d_statistics.d_instantiation_rounds_lc);
527 }else if( e==Theory::EFFORT_FULL ){
528 ++(d_statistics.d_instantiation_rounds);
529 }
530 Trace("quant-engine-debug") << "Check modules that needed check..." << std::endl;
531 for (unsigned qef = QuantifiersModule::QEFFORT_CONFLICT;
532 qef <= QuantifiersModule::QEFFORT_LAST_CALL;
533 ++qef)
534 {
535 QuantifiersModule::QEffort quant_e =
536 static_cast<QuantifiersModule::QEffort>(qef);
537 d_curr_effort_level = quant_e;
538 // Force the theory engine to build the model if any module requested it.
539 if (needsModelE == quant_e)
540 {
541 Trace("quant-engine-debug") << "Build model..." << std::endl;
542 if (!d_te->buildModel())
543 {
544 // If we failed to build the model, flush all pending lemmas and
545 // finish.
546 flushLemmas();
547 break;
548 }
549 }
550 if( !d_hasAddedLemma ){
551 //check each module
552 for (QuantifiersModule*& mdl : qm)
553 {
554 Trace("quant-engine-debug") << "Check " << mdl->identify().c_str()
555 << " at effort " << quant_e << "..."
556 << std::endl;
557 mdl->check(e, quant_e);
558 if( d_conflict ){
559 Trace("quant-engine-debug") << "...conflict!" << std::endl;
560 break;
561 }
562 }
563 //flush all current lemmas
564 flushLemmas();
565 }
566 //if we have added one, stop
567 if( d_hasAddedLemma ){
568 break;
569 }else{
570 Assert(!d_conflict);
571 if (quant_e == QuantifiersModule::QEFFORT_CONFLICT)
572 {
573 if( e==Theory::EFFORT_FULL ){
574 //increment if a last call happened, we are not strictly enforcing interleaving, or already were in phase
575 if( d_ierCounterLastLc!=d_ierCounter_lc || !options::instWhenStrictInterleave() || d_ierCounter%d_inst_when_phase!=0 ){
576 d_ierCounter = d_ierCounter + 1;
577 d_ierCounterLastLc = d_ierCounter_lc;
578 d_ierCounter_c = d_ierCounter_c.get() + 1;
579 }
580 }else if( e==Theory::EFFORT_LAST_CALL ){
581 d_ierCounter_lc = d_ierCounter_lc + 1;
582 }
583 }
584 else if (quant_e == QuantifiersModule::QEFFORT_MODEL)
585 {
586 if( e==Theory::EFFORT_LAST_CALL ){
587 //sources of incompleteness
588 for (QuantifiersUtil*& util : d_util)
589 {
590 if (!util->checkComplete())
591 {
592 Trace("quant-engine-debug") << "Set incomplete because utility "
593 << util->identify().c_str()
594 << " was incomplete." << std::endl;
595 setIncomplete = true;
596 }
597 }
598 if (d_conflict_c.get())
599 {
600 // we reported a conflicting lemma, should return
601 setIncomplete = true;
602 }
603 //if we have a chance not to set incomplete
604 if( !setIncomplete ){
605 //check if we should set the incomplete flag
606 for (QuantifiersModule*& mdl : d_modules)
607 {
608 if (!mdl->checkComplete())
609 {
610 Trace("quant-engine-debug")
611 << "Set incomplete because module "
612 << mdl->identify().c_str() << " was incomplete."
613 << std::endl;
614 setIncomplete = true;
615 break;
616 }
617 }
618 if( !setIncomplete ){
619 //look at individual quantified formulas, one module must claim completeness for each one
620 for( unsigned i=0; i<d_model->getNumAssertedQuantifiers(); i++ ){
621 bool hasCompleteM = false;
622 Node q = d_model->getAssertedQuantifier( i );
623 QuantifiersModule * qmd = getOwner( q );
624 if( qmd!=NULL ){
625 hasCompleteM = qmd->checkCompleteFor( q );
626 }else{
627 for( unsigned j=0; j<d_modules.size(); j++ ){
628 if( d_modules[j]->checkCompleteFor( q ) ){
629 qmd = d_modules[j];
630 hasCompleteM = true;
631 break;
632 }
633 }
634 }
635 if( !hasCompleteM ){
636 Trace("quant-engine-debug") << "Set incomplete because " << q << " was not fully processed." << std::endl;
637 setIncomplete = true;
638 break;
639 }else{
640 Assert(qmd != NULL);
641 Trace("quant-engine-debug2") << "Complete for " << q << " due to " << qmd->identify().c_str() << std::endl;
642 }
643 }
644 }
645 }
646 //if setIncomplete = false, we will answer SAT, otherwise we will run at quant_e QEFFORT_LAST_CALL
647 if( !setIncomplete ){
648 break;
649 }
650 }
651 }
652 }
653 }
654 d_curr_effort_level = QuantifiersModule::QEFFORT_NONE;
655 Trace("quant-engine-debug") << "Done check modules that needed check." << std::endl;
656 // debug print
657 if (d_hasAddedLemma)
658 {
659 bool debugInstTrace = Trace.isOn("inst-per-quant-round");
660 if (options::debugInst() || debugInstTrace)
661 {
662 Options& sopts = smt::currentSmtEngine()->getOptions();
663 std::ostream& out = *sopts.getOut();
664 d_instantiate->debugPrint(out);
665 }
666 }
667 if( Trace.isOn("quant-engine") ){
668 double clSet2 = double(clock())/double(CLOCKS_PER_SEC);
669 Trace("quant-engine") << "Finished quantifiers engine, total time = " << (clSet2-clSet);
670 Trace("quant-engine") << ", added lemma = " << d_hasAddedLemma;
671 Trace("quant-engine") << std::endl;
672 }
673
674 Trace("quant-engine-debug2") << "Finished quantifiers engine check." << std::endl;
675 }else{
676 Trace("quant-engine-debug2") << "Quantifiers Engine does not need check." << std::endl;
677 }
678
679 //SAT case
680 if( e==Theory::EFFORT_LAST_CALL && !d_hasAddedLemma ){
681 if( setIncomplete ){
682 Trace("quant-engine") << "Set incomplete flag." << std::endl;
683 getOutputChannel().setIncomplete();
684 }
685 //output debug stats
686 d_instantiate->debugPrintModel();
687 }
688 }
689
690 void QuantifiersEngine::notifyCombineTheories() {
691 //if allowing theory combination to happen at most once between instantiation rounds
692 //d_ierCounter = 1;
693 //d_ierCounterLastLc = -1;
694 }
695
696 bool QuantifiersEngine::reduceQuantifier( Node q ) {
697 //TODO: this can be unified with preregistration: AlphaEquivalence takes ownership of reducable quants
698 BoolMap::const_iterator it = d_quants_red.find( q );
699 if( it==d_quants_red.end() ){
700 Node lem;
701 std::map< Node, Node >::iterator itr = d_quants_red_lem.find( q );
702 if( itr==d_quants_red_lem.end() ){
703 if (d_qmodules->d_alpha_equiv)
704 {
705 Trace("quant-engine-red") << "Alpha equivalence " << q << "?" << std::endl;
706 //add equivalence with another quantified formula
707 lem = d_qmodules->d_alpha_equiv->reduceQuantifier(q);
708 if( !lem.isNull() ){
709 Trace("quant-engine-red") << "...alpha equivalence success." << std::endl;
710 ++(d_statistics.d_red_alpha_equiv);
711 }
712 }
713 d_quants_red_lem[q] = lem;
714 }else{
715 lem = itr->second;
716 }
717 if( !lem.isNull() ){
718 getOutputChannel().lemma( lem );
719 }
720 d_quants_red[q] = !lem.isNull();
721 return !lem.isNull();
722 }else{
723 return (*it).second;
724 }
725 }
726
727 void QuantifiersEngine::registerQuantifierInternal(Node f)
728 {
729 std::map< Node, bool >::iterator it = d_quants.find( f );
730 if( it==d_quants.end() ){
731 Trace("quant") << "QuantifiersEngine : Register quantifier ";
732 Trace("quant") << " : " << f << std::endl;
733 unsigned prev_lemma_waiting = d_lemmas_waiting.size();
734 ++(d_statistics.d_num_quant);
735 Assert(f.getKind() == FORALL);
736 // register with utilities
737 for (unsigned i = 0; i < d_util.size(); i++)
738 {
739 d_util[i]->registerQuantifier(f);
740 }
741 // compute attributes
742 d_quant_attr->computeAttributes(f);
743
744 for (QuantifiersModule*& mdl : d_modules)
745 {
746 Trace("quant-debug") << "check ownership with " << mdl->identify()
747 << "..." << std::endl;
748 mdl->checkOwnership(f);
749 }
750 QuantifiersModule* qm = getOwner(f);
751 Trace("quant") << " Owner : " << (qm == nullptr ? "[none]" : qm->identify())
752 << std::endl;
753 // register with each module
754 for (QuantifiersModule*& mdl : d_modules)
755 {
756 Trace("quant-debug") << "register with " << mdl->identify() << "..."
757 << std::endl;
758 mdl->registerQuantifier(f);
759 // since this is context-independent, we should not add any lemmas during
760 // this call
761 Assert(d_lemmas_waiting.size() == prev_lemma_waiting);
762 }
763 Trace("quant-debug") << "...finish." << std::endl;
764 d_quants[f] = true;
765 AlwaysAssert(d_lemmas_waiting.size() == prev_lemma_waiting);
766 }
767 }
768
769 void QuantifiersEngine::preRegisterQuantifier(Node q)
770 {
771 NodeSet::const_iterator it = d_quants_prereg.find(q);
772 if (it != d_quants_prereg.end())
773 {
774 return;
775 }
776 Trace("quant-debug") << "QuantifiersEngine : Pre-register " << q << std::endl;
777 d_quants_prereg.insert(q);
778 // try to reduce
779 if (reduceQuantifier(q))
780 {
781 // if we can reduce it, nothing left to do
782 return;
783 }
784 // ensure that it is registered
785 registerQuantifierInternal(q);
786 // register with each module
787 for (QuantifiersModule*& mdl : d_modules)
788 {
789 Trace("quant-debug") << "pre-register with " << mdl->identify() << "..."
790 << std::endl;
791 mdl->preRegisterQuantifier(q);
792 }
793 // flush the lemmas
794 flushLemmas();
795 Trace("quant-debug") << "...finish pre-register " << q << "..." << std::endl;
796 }
797
798 void QuantifiersEngine::registerPattern( std::vector<Node> & pattern) {
799 for(std::vector<Node>::iterator p = pattern.begin(); p != pattern.end(); ++p){
800 std::set< Node > added;
801 getTermDatabase()->addTerm( *p, added );
802 }
803 }
804
805 void QuantifiersEngine::assertQuantifier( Node f, bool pol ){
806 if (reduceQuantifier(f))
807 {
808 // if we can reduce it, nothing left to do
809 return;
810 }
811 if( !pol ){
812 // do skolemization
813 TrustNode lem = d_skolemize->process(f);
814 if (!lem.isNull())
815 {
816 if (Trace.isOn("quantifiers-sk-debug"))
817 {
818 Node slem = Rewriter::rewrite(lem.getNode());
819 Trace("quantifiers-sk-debug")
820 << "Skolemize lemma : " << slem << std::endl;
821 }
822 getOutputChannel().trustedLemma(
823 lem, LemmaProperty::PREPROCESS | LemmaProperty::NEEDS_JUSTIFY);
824 }
825 return;
826 }
827 // ensure the quantified formula is registered
828 registerQuantifierInternal(f);
829 // assert it to each module
830 d_model->assertQuantifier(f);
831 for (QuantifiersModule*& mdl : d_modules)
832 {
833 mdl->assertNode(f);
834 }
835 addTermToDatabase(d_term_util->getInstConstantBody(f), true);
836 }
837
838 void QuantifiersEngine::addTermToDatabase( Node n, bool withinQuant, bool withinInstClosure ){
839 if( options::incrementalSolving() ){
840 if( d_presolve_in.find( n )==d_presolve_in.end() ){
841 d_presolve_in.insert( n );
842 d_presolve_cache.push_back( n );
843 d_presolve_cache_wq.push_back( withinQuant );
844 d_presolve_cache_wic.push_back( withinInstClosure );
845 }
846 }
847 //only wait if we are doing incremental solving
848 if( !d_presolve || !options::incrementalSolving() ){
849 std::set< Node > added;
850 d_term_db->addTerm(n, added, withinQuant, withinInstClosure);
851
852 if (!withinQuant)
853 {
854 if (d_sygus_tdb && options::sygusEvalUnfold())
855 {
856 d_sygus_tdb->getEvalUnfold()->registerEvalTerm(n);
857 }
858 }
859 }
860 }
861
862 void QuantifiersEngine::eqNotifyNewClass(TNode t) {
863 addTermToDatabase( t );
864 }
865
866 bool QuantifiersEngine::addLemma( Node lem, bool doCache, bool doRewrite ){
867 if( doCache ){
868 if( doRewrite ){
869 lem = Rewriter::rewrite(lem);
870 }
871 Trace("inst-add-debug") << "Adding lemma : " << lem << std::endl;
872 BoolMap::const_iterator itp = d_lemmas_produced_c.find( lem );
873 if( itp==d_lemmas_produced_c.end() || !(*itp).second ){
874 d_lemmas_produced_c[ lem ] = true;
875 d_lemmas_waiting.push_back( lem );
876 Trace("inst-add-debug") << "Added lemma" << std::endl;
877 return true;
878 }else{
879 Trace("inst-add-debug") << "Duplicate." << std::endl;
880 return false;
881 }
882 }else{
883 //do not need to rewrite, will be rewritten after sending
884 d_lemmas_waiting.push_back( lem );
885 return true;
886 }
887 }
888
889 bool QuantifiersEngine::addTrustedLemma(TrustNode tlem,
890 bool doCache,
891 bool doRewrite)
892 {
893 Node lem = tlem.getProven();
894 if (!addLemma(lem, doCache, doRewrite))
895 {
896 return false;
897 }
898 d_lemmasWaitingPg[lem] = tlem.getGenerator();
899 return true;
900 }
901
902 bool QuantifiersEngine::removeLemma( Node lem ) {
903 std::vector< Node >::iterator it = std::find( d_lemmas_waiting.begin(), d_lemmas_waiting.end(), lem );
904 if( it!=d_lemmas_waiting.end() ){
905 d_lemmas_waiting.erase( it, it + 1 );
906 d_lemmas_produced_c[ lem ] = false;
907 return true;
908 }else{
909 return false;
910 }
911 }
912
913 void QuantifiersEngine::addRequirePhase( Node lit, bool req ){
914 d_phase_req_waiting[lit] = req;
915 }
916
917 void QuantifiersEngine::markRelevant( Node q ) {
918 d_model->markRelevant( q );
919 }
920 bool QuantifiersEngine::hasAddedLemma() const
921 {
922 return !d_lemmas_waiting.empty() || d_hasAddedLemma;
923 }
924 bool QuantifiersEngine::theoryEngineNeedsCheck() const
925 {
926 return d_te->needCheck();
927 }
928
929 void QuantifiersEngine::setConflict()
930 {
931 d_conflict = true;
932 d_conflict_c = true;
933 }
934
935 bool QuantifiersEngine::getInstWhenNeedsCheck( Theory::Effort e ) {
936 Trace("quant-engine-debug2") << "Get inst when needs check, counts=" << d_ierCounter << ", " << d_ierCounter_lc << std::endl;
937 //determine if we should perform check, based on instWhenMode
938 bool performCheck = false;
939 if (options::instWhenMode() == options::InstWhenMode::FULL)
940 {
941 performCheck = ( e >= Theory::EFFORT_FULL );
942 }
943 else if (options::instWhenMode() == options::InstWhenMode::FULL_DELAY)
944 {
945 performCheck = (e >= Theory::EFFORT_FULL) && !theoryEngineNeedsCheck();
946 }
947 else if (options::instWhenMode() == options::InstWhenMode::FULL_LAST_CALL)
948 {
949 performCheck = ( ( e==Theory::EFFORT_FULL && d_ierCounter%d_inst_when_phase!=0 ) || e==Theory::EFFORT_LAST_CALL );
950 }
951 else if (options::instWhenMode()
952 == options::InstWhenMode::FULL_DELAY_LAST_CALL)
953 {
954 performCheck = ((e == Theory::EFFORT_FULL && !theoryEngineNeedsCheck()
955 && d_ierCounter % d_inst_when_phase != 0)
956 || e == Theory::EFFORT_LAST_CALL);
957 }
958 else if (options::instWhenMode() == options::InstWhenMode::LAST_CALL)
959 {
960 performCheck = ( e >= Theory::EFFORT_LAST_CALL );
961 }
962 else
963 {
964 performCheck = true;
965 }
966 return performCheck;
967 }
968
969 options::UserPatMode QuantifiersEngine::getInstUserPatMode()
970 {
971 if (options::userPatternsQuant() == options::UserPatMode::INTERLEAVE)
972 {
973 return d_ierCounter % 2 == 0 ? options::UserPatMode::USE
974 : options::UserPatMode::RESORT;
975 }
976 else
977 {
978 return options::userPatternsQuant();
979 }
980 }
981
982 void QuantifiersEngine::flushLemmas(){
983 OutputChannel& out = getOutputChannel();
984 if( !d_lemmas_waiting.empty() ){
985 //take default output channel if none is provided
986 d_hasAddedLemma = true;
987 std::map<Node, ProofGenerator*>::iterator itp;
988 // Note: Do not use foreach loop here and do not cache size() call.
989 // New lemmas can be added while iterating over d_lemmas_waiting.
990 for (size_t i = 0; i < d_lemmas_waiting.size(); ++i)
991 {
992 const Node& lemw = d_lemmas_waiting[i];
993 Trace("qe-lemma") << "Lemma : " << lemw << std::endl;
994 itp = d_lemmasWaitingPg.find(lemw);
995 if (itp != d_lemmasWaitingPg.end())
996 {
997 TrustNode tlemw = TrustNode::mkTrustLemma(lemw, itp->second);
998 out.trustedLemma(tlemw, LemmaProperty::PREPROCESS);
999 }
1000 else
1001 {
1002 out.lemma(lemw, LemmaProperty::PREPROCESS);
1003 }
1004 }
1005 d_lemmas_waiting.clear();
1006 }
1007 if( !d_phase_req_waiting.empty() ){
1008 for( std::map< Node, bool >::iterator it = d_phase_req_waiting.begin(); it != d_phase_req_waiting.end(); ++it ){
1009 Trace("qe-lemma") << "Require phase : " << it->first << " -> " << it->second << std::endl;
1010 out.requirePhase(it->first, it->second);
1011 }
1012 d_phase_req_waiting.clear();
1013 }
1014 }
1015
1016 void QuantifiersEngine::getInstantiationTermVectors( Node q, std::vector< std::vector< Node > >& tvecs ) {
1017 d_instantiate->getInstantiationTermVectors(q, tvecs);
1018 }
1019
1020 void QuantifiersEngine::getInstantiationTermVectors( std::map< Node, std::vector< std::vector< Node > > >& insts ) {
1021 d_instantiate->getInstantiationTermVectors(insts);
1022 }
1023
1024 void QuantifiersEngine::printInstantiations( std::ostream& out ) {
1025 bool printed = false;
1026 // print the skolemizations
1027 if (options::printInstMode() == options::PrintInstMode::LIST)
1028 {
1029 if (d_skolemize->printSkolemization(out))
1030 {
1031 printed = true;
1032 }
1033 }
1034 // print the instantiations
1035 if (d_instantiate->printInstantiations(out))
1036 {
1037 printed = true;
1038 }
1039 if( !printed ){
1040 out << "No instantiations" << std::endl;
1041 }
1042 }
1043
1044 void QuantifiersEngine::printSynthSolution( std::ostream& out ) {
1045 if (d_qmodules->d_synth_e)
1046 {
1047 d_qmodules->d_synth_e->printSynthSolution(out);
1048 }else{
1049 out << "Internal error : module for synth solution not found." << std::endl;
1050 }
1051 }
1052
1053 void QuantifiersEngine::getInstantiatedQuantifiedFormulas( std::vector< Node >& qs ) {
1054 d_instantiate->getInstantiatedQuantifiedFormulas(qs);
1055 }
1056
1057 QuantifiersEngine::Statistics::Statistics()
1058 : d_time("theory::QuantifiersEngine::time"),
1059 d_qcf_time("theory::QuantifiersEngine::time_qcf"),
1060 d_ematching_time("theory::QuantifiersEngine::time_ematching"),
1061 d_num_quant("QuantifiersEngine::Num_Quantifiers", 0),
1062 d_instantiation_rounds("QuantifiersEngine::Rounds_Instantiation_Full", 0),
1063 d_instantiation_rounds_lc("QuantifiersEngine::Rounds_Instantiation_Last_Call", 0),
1064 d_triggers("QuantifiersEngine::Triggers", 0),
1065 d_simple_triggers("QuantifiersEngine::Triggers_Simple", 0),
1066 d_multi_triggers("QuantifiersEngine::Triggers_Multi", 0),
1067 d_multi_trigger_instantiations("QuantifiersEngine::Multi_Trigger_Instantiations", 0),
1068 d_red_alpha_equiv("QuantifiersEngine::Reductions_Alpha_Equivalence", 0),
1069 d_instantiations_user_patterns("QuantifiersEngine::Instantiations_User_Patterns", 0),
1070 d_instantiations_auto_gen("QuantifiersEngine::Instantiations_Auto_Gen", 0),
1071 d_instantiations_guess("QuantifiersEngine::Instantiations_Guess", 0),
1072 d_instantiations_qcf("QuantifiersEngine::Instantiations_Qcf_Conflict", 0),
1073 d_instantiations_qcf_prop("QuantifiersEngine::Instantiations_Qcf_Prop", 0),
1074 d_instantiations_fmf_exh("QuantifiersEngine::Instantiations_Fmf_Exh", 0),
1075 d_instantiations_fmf_mbqi("QuantifiersEngine::Instantiations_Fmf_Mbqi", 0),
1076 d_instantiations_cbqi("QuantifiersEngine::Instantiations_Cbqi", 0),
1077 d_instantiations_rr("QuantifiersEngine::Instantiations_Rewrite_Rules", 0)
1078 {
1079 smtStatisticsRegistry()->registerStat(&d_time);
1080 smtStatisticsRegistry()->registerStat(&d_qcf_time);
1081 smtStatisticsRegistry()->registerStat(&d_ematching_time);
1082 smtStatisticsRegistry()->registerStat(&d_num_quant);
1083 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds);
1084 smtStatisticsRegistry()->registerStat(&d_instantiation_rounds_lc);
1085 smtStatisticsRegistry()->registerStat(&d_triggers);
1086 smtStatisticsRegistry()->registerStat(&d_simple_triggers);
1087 smtStatisticsRegistry()->registerStat(&d_multi_triggers);
1088 smtStatisticsRegistry()->registerStat(&d_multi_trigger_instantiations);
1089 smtStatisticsRegistry()->registerStat(&d_red_alpha_equiv);
1090 smtStatisticsRegistry()->registerStat(&d_instantiations_user_patterns);
1091 smtStatisticsRegistry()->registerStat(&d_instantiations_auto_gen);
1092 smtStatisticsRegistry()->registerStat(&d_instantiations_guess);
1093 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf);
1094 smtStatisticsRegistry()->registerStat(&d_instantiations_qcf_prop);
1095 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_exh);
1096 smtStatisticsRegistry()->registerStat(&d_instantiations_fmf_mbqi);
1097 smtStatisticsRegistry()->registerStat(&d_instantiations_cbqi);
1098 smtStatisticsRegistry()->registerStat(&d_instantiations_rr);
1099 }
1100
1101 QuantifiersEngine::Statistics::~Statistics(){
1102 smtStatisticsRegistry()->unregisterStat(&d_time);
1103 smtStatisticsRegistry()->unregisterStat(&d_qcf_time);
1104 smtStatisticsRegistry()->unregisterStat(&d_ematching_time);
1105 smtStatisticsRegistry()->unregisterStat(&d_num_quant);
1106 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds);
1107 smtStatisticsRegistry()->unregisterStat(&d_instantiation_rounds_lc);
1108 smtStatisticsRegistry()->unregisterStat(&d_triggers);
1109 smtStatisticsRegistry()->unregisterStat(&d_simple_triggers);
1110 smtStatisticsRegistry()->unregisterStat(&d_multi_triggers);
1111 smtStatisticsRegistry()->unregisterStat(&d_multi_trigger_instantiations);
1112 smtStatisticsRegistry()->unregisterStat(&d_red_alpha_equiv);
1113 smtStatisticsRegistry()->unregisterStat(&d_instantiations_user_patterns);
1114 smtStatisticsRegistry()->unregisterStat(&d_instantiations_auto_gen);
1115 smtStatisticsRegistry()->unregisterStat(&d_instantiations_guess);
1116 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf);
1117 smtStatisticsRegistry()->unregisterStat(&d_instantiations_qcf_prop);
1118 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_exh);
1119 smtStatisticsRegistry()->unregisterStat(&d_instantiations_fmf_mbqi);
1120 smtStatisticsRegistry()->unregisterStat(&d_instantiations_cbqi);
1121 smtStatisticsRegistry()->unregisterStat(&d_instantiations_rr);
1122 }
1123
1124 eq::EqualityEngine* QuantifiersEngine::getMasterEqualityEngine() const
1125 {
1126 return d_masterEqualityEngine;
1127 }
1128
1129 Node QuantifiersEngine::getInternalRepresentative( Node a, Node q, int index ){
1130 return d_eq_query->getInternalRepresentative(a, q, index);
1131 }
1132
1133 bool QuantifiersEngine::getSynthSolutions(
1134 std::map<Node, std::map<Node, Node> >& sol_map)
1135 {
1136 return d_qmodules->d_synth_e->getSynthSolutions(sol_map);
1137 }
1138
1139 void QuantifiersEngine::debugPrintEqualityEngine( const char * c ) {
1140 eq::EqualityEngine* ee = getMasterEqualityEngine();
1141 eq::EqClassesIterator eqcs_i = eq::EqClassesIterator( ee );
1142 std::map< TypeNode, int > typ_num;
1143 while( !eqcs_i.isFinished() ){
1144 TNode r = (*eqcs_i);
1145 TypeNode tr = r.getType();
1146 if( typ_num.find( tr )==typ_num.end() ){
1147 typ_num[tr] = 0;
1148 }
1149 typ_num[tr]++;
1150 bool firstTime = true;
1151 Trace(c) << " " << r;
1152 Trace(c) << " : { ";
1153 eq::EqClassIterator eqc_i = eq::EqClassIterator( r, ee );
1154 while( !eqc_i.isFinished() ){
1155 TNode n = (*eqc_i);
1156 if( r!=n ){
1157 if( firstTime ){
1158 Trace(c) << std::endl;
1159 firstTime = false;
1160 }
1161 Trace(c) << " " << n << std::endl;
1162 }
1163 ++eqc_i;
1164 }
1165 if( !firstTime ){ Trace(c) << " "; }
1166 Trace(c) << "}" << std::endl;
1167 ++eqcs_i;
1168 }
1169 Trace(c) << std::endl;
1170 for( std::map< TypeNode, int >::iterator it = typ_num.begin(); it != typ_num.end(); ++it ){
1171 Trace(c) << "# eqc for " << it->first << " : " << it->second << std::endl;
1172 }
1173 }
1174
1175 } // namespace theory
1176 } // namespace CVC4