Merge branch 'origin' of https://github.com/CVC4/CVC4.git
[cvc5.git] / src / theory / quantifiers_engine.h
1 /********************* */
2 /*! \file quantifiers_engine.h
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Morgan Deters, Tim King
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 Theory instantiator, Instantiation Engine classes
13 **/
14
15 #include "cvc4_private.h"
16
17 #ifndef __CVC4__THEORY__QUANTIFIERS_ENGINE_H
18 #define __CVC4__THEORY__QUANTIFIERS_ENGINE_H
19
20 #include <ext/hash_set>
21 #include <iostream>
22 #include <map>
23
24 #include "context/cdchunk_list.h"
25 #include "context/cdhashset.h"
26 #include "expr/attribute.h"
27 #include "options/quantifiers_modes.h"
28 #include "theory/quantifiers/inst_match.h"
29 #include "theory/quantifiers/quant_util.h"
30 #include "theory/theory.h"
31 #include "util/hash.h"
32 #include "util/statistics_registry.h"
33
34 namespace CVC4 {
35
36 class TheoryEngine;
37
38 namespace theory {
39
40 class QuantifiersEngine;
41
42 namespace quantifiers {
43 class TermDb;
44 class TermDbSygus;
45 }
46
47 class InstantiationNotify {
48 public:
49 InstantiationNotify(){}
50 virtual bool notifyInstantiation( unsigned quant_e, Node q, Node lem, std::vector< Node >& terms, Node body ) = 0;
51 virtual void filterInstantiations() = 0;
52 };
53
54 namespace quantifiers {
55 class FirstOrderModel;
56 //modules
57 class InstantiationEngine;
58 class ModelEngine;
59 class BoundedIntegers;
60 class QuantConflictFind;
61 class RewriteEngine;
62 class RelevantDomain;
63 class QModelBuilder;
64 class ConjectureGenerator;
65 class CegInstantiation;
66 class LtePartialInst;
67 class AlphaEquivalence;
68 class FunDefEngine;
69 class QuantEqualityEngine;
70 class FullSaturation;
71 class InstStrategyCbqi;
72 class InstStrategyCegqi;
73 class QuantDSplit;
74 class QuantAntiSkolem;
75 class EqualityInference;
76 class InstPropagator;
77 }/* CVC4::theory::quantifiers */
78
79 namespace inst {
80 class TriggerTrie;
81 }/* CVC4::theory::inst */
82
83 //class EfficientEMatcher;
84 class EqualityQueryQuantifiersEngine;
85
86 class QuantifiersEngine {
87 friend class quantifiers::InstantiationEngine;
88 friend class quantifiers::InstStrategyCbqi;
89 friend class quantifiers::InstStrategyCegqi;
90 friend class quantifiers::ModelEngine;
91 friend class quantifiers::RewriteEngine;
92 friend class quantifiers::QuantConflictFind;
93 friend class inst::InstMatch;
94 typedef context::CDHashMap< Node, bool, NodeHashFunction > BoolMap;
95 typedef context::CDChunkList<Node> NodeList;
96 typedef context::CDChunkList<bool> BoolList;
97 typedef context::CDHashSet<Node, NodeHashFunction> NodeSet;
98 private:
99 /** reference to theory engine object */
100 TheoryEngine* d_te;
101 /** vector of utilities for quantifiers */
102 std::vector< QuantifiersUtil* > d_util;
103 /** vector of modules for quantifiers */
104 std::vector< QuantifiersModule* > d_modules;
105 /** instantiation notify */
106 std::vector< InstantiationNotify* > d_inst_notify;
107 /** equality query class */
108 EqualityQueryQuantifiersEngine* d_eq_query;
109 /** for computing relevance of quantifiers */
110 QuantRelevance * d_quant_rel;
111 /** relevant domain */
112 quantifiers::RelevantDomain* d_rel_dom;
113 /** alpha equivalence */
114 quantifiers::AlphaEquivalence * d_alpha_equiv;
115 /** model builder */
116 quantifiers::QModelBuilder* d_builder;
117 /** utility for effectively propositional logic */
118 QuantEPR * d_qepr;
119 private:
120 /** instantiation engine */
121 quantifiers::InstantiationEngine* d_inst_engine;
122 /** model engine */
123 quantifiers::ModelEngine* d_model_engine;
124 /** bounded integers utility */
125 quantifiers::BoundedIntegers * d_bint;
126 /** Conflict find mechanism for quantifiers */
127 quantifiers::QuantConflictFind* d_qcf;
128 /** rewrite rules utility */
129 quantifiers::RewriteEngine * d_rr_engine;
130 /** subgoal generator */
131 quantifiers::ConjectureGenerator * d_sg_gen;
132 /** ceg instantiation */
133 quantifiers::CegInstantiation * d_ceg_inst;
134 /** lte partial instantiation */
135 quantifiers::LtePartialInst * d_lte_part_inst;
136 /** function definitions engine */
137 quantifiers::FunDefEngine * d_fun_def_engine;
138 /** quantifiers equality engine */
139 quantifiers::QuantEqualityEngine * d_uee;
140 /** full saturation */
141 quantifiers::FullSaturation * d_fs;
142 /** counterexample-based quantifier instantiation */
143 quantifiers::InstStrategyCbqi * d_i_cbqi;
144 /** quantifiers splitting */
145 quantifiers::QuantDSplit * d_qsplit;
146 /** quantifiers anti-skolemization */
147 quantifiers::QuantAntiSkolem * d_anti_skolem;
148 /** quantifiers instantiation propagtor */
149 quantifiers::InstPropagator * d_inst_prop;
150 private:
151 /** whether we are tracking instantiation lemmas */
152 bool d_trackInstLemmas;
153 public: //effort levels
154 enum {
155 QEFFORT_CONFLICT,
156 QEFFORT_STANDARD,
157 QEFFORT_MODEL,
158 QEFFORT_LAST_CALL,
159 //none
160 QEFFORT_NONE,
161 };
162 private: //this information is reset during check
163 /** current effort level */
164 unsigned d_curr_effort_level;
165 /** are we in conflict */
166 bool d_conflict;
167 context::CDO< bool > d_conflict_c;
168 /** has added lemma this round */
169 bool d_hasAddedLemma;
170 private:
171 /** list of all quantifiers seen */
172 std::map< Node, bool > d_quants;
173 /** quantifiers reduced */
174 BoolMap d_quants_red;
175 std::map< Node, Node > d_quants_red_lem;
176 /** list of all lemmas produced */
177 //std::map< Node, bool > d_lemmas_produced;
178 BoolMap d_lemmas_produced_c;
179 /** lemmas waiting */
180 std::vector< Node > d_lemmas_waiting;
181 /** phase requirements waiting */
182 std::map< Node, bool > d_phase_req_waiting;
183 /** list of all instantiations produced for each quantifier */
184 std::map< Node, inst::InstMatchTrie > d_inst_match_trie;
185 std::map< Node, inst::CDInstMatchTrie* > d_c_inst_match_trie;
186 /** recorded instantiations */
187 std::vector< std::pair< Node, std::vector< Node > > > d_recorded_inst;
188 /** quantifiers that have been skolemized */
189 BoolMap d_skolemized;
190 /** term database */
191 quantifiers::TermDb* d_term_db;
192 /** all triggers will be stored in this trie */
193 inst::TriggerTrie* d_tr_trie;
194 /** extended model object */
195 quantifiers::FirstOrderModel* d_model;
196 /** statistics for debugging */
197 std::map< Node, int > d_total_inst_debug;
198 std::map< Node, int > d_temp_inst_debug;
199 int d_total_inst_count_debug;
200 /** inst round counters TODO: make context-dependent? */
201 context::CDO< int > d_ierCounter_c;
202 int d_ierCounter;
203 int d_ierCounter_lc;
204 int d_ierCounterLastLc;
205 int d_inst_when_phase;
206 /** has presolve been called */
207 context::CDO< bool > d_presolve;
208 /** presolve cache */
209 NodeSet d_presolve_in;
210 NodeList d_presolve_cache;
211 BoolList d_presolve_cache_wq;
212 BoolList d_presolve_cache_wic;
213
214 public:
215 QuantifiersEngine(context::Context* c, context::UserContext* u, TheoryEngine* te);
216 ~QuantifiersEngine();
217 /** get theory engine */
218 TheoryEngine* getTheoryEngine() { return d_te; }
219 /** get equality query */
220 EqualityQueryQuantifiersEngine* getEqualityQuery();
221 /** get default sat context for quantifiers engine */
222 context::Context* getSatContext();
223 /** get default sat context for quantifiers engine */
224 context::UserContext* getUserContext();
225 /** get default output channel for the quantifiers engine */
226 OutputChannel& getOutputChannel();
227 /** get default valuation for the quantifiers engine */
228 Valuation& getValuation();
229 /** get relevant domain */
230 quantifiers::RelevantDomain* getRelevantDomain() { return d_rel_dom; }
231 /** get quantifier relevance */
232 QuantRelevance* getQuantifierRelevance() { return d_quant_rel; }
233 /** get the model builder */
234 quantifiers::QModelBuilder* getModelBuilder() { return d_builder; }
235 /** get utility for EPR */
236 QuantEPR* getQuantEPR() { return d_qepr; }
237 public: //modules
238 /** get instantiation engine */
239 quantifiers::InstantiationEngine* getInstantiationEngine() { return d_inst_engine; }
240 /** get model engine */
241 quantifiers::ModelEngine* getModelEngine() { return d_model_engine; }
242 /** get bounded integers utility */
243 quantifiers::BoundedIntegers * getBoundedIntegers() { return d_bint; }
244 /** Conflict find mechanism for quantifiers */
245 quantifiers::QuantConflictFind* getConflictFind() { return d_qcf; }
246 /** rewrite rules utility */
247 quantifiers::RewriteEngine * getRewriteEngine() { return d_rr_engine; }
248 /** subgoal generator */
249 quantifiers::ConjectureGenerator * getConjectureGenerator() { return d_sg_gen; }
250 /** ceg instantiation */
251 quantifiers::CegInstantiation * getCegInstantiation() { return d_ceg_inst; }
252 /** local theory ext partial inst */
253 quantifiers::LtePartialInst * getLtePartialInst() { return d_lte_part_inst; }
254 /** function definition engine */
255 quantifiers::FunDefEngine * getFunDefEngine() { return d_fun_def_engine; }
256 /** quantifiers equality engine */
257 quantifiers::QuantEqualityEngine * getQuantEqualityEngine() { return d_uee; }
258 /** get full saturation */
259 quantifiers::FullSaturation * getFullSaturation() { return d_fs; }
260 /** get inst strategy cbqi */
261 quantifiers::InstStrategyCbqi * getInstStrategyCbqi() { return d_i_cbqi; }
262 /** get quantifiers splitting */
263 quantifiers::QuantDSplit * getQuantDSplit() { return d_qsplit; }
264 /** get quantifiers anti-skolemization */
265 quantifiers::QuantAntiSkolem * getQuantAntiSkolem() { return d_anti_skolem; }
266 private:
267 /** owner of quantified formulas */
268 std::map< Node, QuantifiersModule * > d_owner;
269 std::map< Node, int > d_owner_priority;
270 public:
271 /** get owner */
272 QuantifiersModule * getOwner( Node q );
273 /** set owner */
274 void setOwner( Node q, QuantifiersModule * m, int priority = 0 );
275 /** considers */
276 bool hasOwnership( Node q, QuantifiersModule * m = NULL );
277 public:
278 /** initialize */
279 void finishInit();
280 /** presolve */
281 void presolve();
282 /** notify preprocessed assertion */
283 void ppNotifyAssertions( std::vector< Node >& assertions );
284 /** check at level */
285 void check( Theory::Effort e );
286 /** notify that theories were combined */
287 void notifyCombineTheories();
288 /** register quantifier */
289 bool registerQuantifier( Node f );
290 /** register quantifier */
291 void registerPattern( std::vector<Node> & pattern);
292 /** assert universal quantifier */
293 void assertQuantifier( Node q, bool pol );
294 /** propagate */
295 void propagate( Theory::Effort level );
296 /** get next decision request */
297 Node getNextDecisionRequest();
298 private:
299 /** reduceQuantifier, return true if reduced */
300 bool reduceQuantifier( Node q );
301 /** compute term vector */
302 void computeTermVector( Node f, InstMatch& m, std::vector< Node >& vars, std::vector< Node >& terms );
303 /** record instantiation, return true if it was non-duplicate */
304 bool recordInstantiationInternal( Node q, std::vector< Node >& terms, bool modEq = false, bool addedLem = true );
305 /** remove instantiation */
306 bool removeInstantiationInternal( Node q, std::vector< Node >& terms );
307 /** set instantiation level attr */
308 static void setInstantiationLevelAttr( Node n, Node qn, uint64_t level );
309 public:
310 /** flush lemmas */
311 void flushLemmas();
312 /** get instantiation */
313 Node getInstantiation( Node q, std::vector< Node >& vars, std::vector< Node >& terms, bool doVts = false );
314 /** get instantiation */
315 Node getInstantiation( Node q, InstMatch& m, bool doVts = false );
316 /** get instantiation */
317 Node getInstantiation( Node q, std::vector< Node >& terms, bool doVts = false );
318 /** do substitution */
319 Node getSubstitute( Node n, std::vector< Node >& terms );
320 /** add lemma lem */
321 bool addLemma( Node lem, bool doCache = true, bool doRewrite = true );
322 /** remove pending lemma */
323 bool removeLemma( Node lem );
324 /** add require phase */
325 void addRequirePhase( Node lit, bool req );
326 /** do instantiation specified by m */
327 bool addInstantiation( Node q, InstMatch& m, bool mkRep = false, bool modEq = false, bool doVts = false );
328 /** add instantiation */
329 bool addInstantiation( Node q, std::vector< Node >& terms, bool mkRep = false, bool modEq = false, bool doVts = false );
330 /** remove pending instantiation */
331 bool removeInstantiation( Node q, Node lem, std::vector< Node >& terms );
332 /** split on node n */
333 bool addSplit( Node n, bool reqPhase = false, bool reqPhasePol = true );
334 /** add split equality */
335 bool addSplitEquality( Node n1, Node n2, bool reqPhase = false, bool reqPhasePol = true );
336 /** add EPR axiom */
337 bool addEPRAxiom( TypeNode tn );
338 /** mark relevant quantified formula, this will indicate it should be checked before the others */
339 void markRelevant( Node q );
340 /** has added lemma */
341 bool hasAddedLemma() { return !d_lemmas_waiting.empty() || d_hasAddedLemma; }
342 /** is in conflict */
343 bool inConflict() { return d_conflict; }
344 /** get number of waiting lemmas */
345 unsigned getNumLemmasWaiting() { return d_lemmas_waiting.size(); }
346 /** get needs check */
347 bool getInstWhenNeedsCheck( Theory::Effort e );
348 /** get user pat mode */
349 quantifiers::UserPatMode getInstUserPatMode();
350 /** set instantiation level attr */
351 static void setInstantiationLevelAttr( Node n, uint64_t level );
352 public:
353 /** get model */
354 quantifiers::FirstOrderModel* getModel() { return d_model; }
355 /** get term database */
356 quantifiers::TermDb* getTermDatabase() { return d_term_db; }
357 /** get term database sygus */
358 quantifiers::TermDbSygus* getTermDatabaseSygus();
359 /** get trigger database */
360 inst::TriggerTrie* getTriggerDatabase() { return d_tr_trie; }
361 /** add term to database */
362 void addTermToDatabase( Node n, bool withinQuant = false, bool withinInstClosure = false );
363 /** notification when master equality engine is updated */
364 void eqNotifyNewClass(TNode t);
365 void eqNotifyPreMerge(TNode t1, TNode t2);
366 void eqNotifyPostMerge(TNode t1, TNode t2);
367 void eqNotifyDisequal(TNode t1, TNode t2, TNode reason);
368 /** get the master equality engine */
369 eq::EqualityEngine* getMasterEqualityEngine() ;
370 /** debug print equality engine */
371 void debugPrintEqualityEngine( const char * c );
372 public:
373 /** print instantiations */
374 void printInstantiations( std::ostream& out );
375 /** print solution for synthesis conjectures */
376 void printSynthSolution( std::ostream& out );
377 /** get instantiations */
378 void getInstantiations( Node q, std::vector< Node >& insts );
379 void getInstantiations( std::map< Node, std::vector< Node > >& insts );
380 /** get instantiated conjunction */
381 Node getInstantiatedConjunction( Node q );
382 /** get unsat core lemmas */
383 bool getUnsatCoreLemmas( std::vector< Node >& active_lemmas );
384 bool getUnsatCoreLemmas( std::vector< Node >& active_lemmas, std::map< Node, Node >& weak_imp );
385 /** get inst for lemmas */
386 void getExplanationForInstLemmas( std::vector< Node >& lems, std::map< Node, Node >& quant, std::map< Node, std::vector< Node > >& tvec );
387 /** statistics class */
388 class Statistics {
389 public:
390 TimerStat d_time;
391 TimerStat d_qcf_time;
392 TimerStat d_ematching_time;
393 IntStat d_num_quant;
394 IntStat d_instantiation_rounds;
395 IntStat d_instantiation_rounds_lc;
396 IntStat d_instantiations;
397 IntStat d_inst_duplicate;
398 IntStat d_inst_duplicate_eq;
399 IntStat d_inst_duplicate_ent;
400 IntStat d_triggers;
401 IntStat d_simple_triggers;
402 IntStat d_multi_triggers;
403 IntStat d_multi_trigger_instantiations;
404 IntStat d_red_alpha_equiv;
405 IntStat d_instantiations_user_patterns;
406 IntStat d_instantiations_auto_gen;
407 IntStat d_instantiations_guess;
408 IntStat d_instantiations_qcf;
409 IntStat d_instantiations_qcf_prop;
410 IntStat d_instantiations_fmf_exh;
411 IntStat d_instantiations_fmf_mbqi;
412 IntStat d_instantiations_cbqi;
413 IntStat d_instantiations_rr;
414 Statistics();
415 ~Statistics();
416 };/* class QuantifiersEngine::Statistics */
417 Statistics d_statistics;
418 };/* class QuantifiersEngine */
419
420
421
422 /** equality query object using theory engine */
423 class EqualityQueryQuantifiersEngine : public EqualityQuery
424 {
425 private:
426 /** pointer to theory engine */
427 QuantifiersEngine* d_qe;
428 /** quantifiers equality inference */
429 quantifiers::EqualityInference * d_eq_inference;
430 context::CDO< unsigned > d_eqi_counter;
431 /** internal representatives */
432 std::map< TypeNode, std::map< Node, Node > > d_int_rep;
433 /** rep score */
434 std::map< Node, int > d_rep_score;
435 /** reset count */
436 int d_reset_count;
437
438 /** processInferences : will merge equivalence classes in master equality engine, if possible */
439 bool processInferences( Theory::Effort e );
440 /** node contains */
441 Node getInstance( Node n, const std::vector< Node >& eqc, std::hash_map<TNode, Node, TNodeHashFunction>& cache );
442 /** get score */
443 int getRepScore( Node n, Node f, int index, TypeNode v_tn );
444 /** flatten representatives */
445 void flattenRepresentatives( std::map< TypeNode, std::vector< Node > >& reps );
446 public:
447 EqualityQueryQuantifiersEngine( context::Context* c, QuantifiersEngine* qe );
448 virtual ~EqualityQueryQuantifiersEngine();
449 /** reset */
450 bool reset( Theory::Effort e );
451 /** identify */
452 std::string identify() const { return "EqualityQueryQE"; }
453 /** general queries about equality */
454 bool hasTerm( Node a );
455 Node getRepresentative( Node a );
456 bool areEqual( Node a, Node b );
457 bool areDisequal( Node a, Node b );
458 eq::EqualityEngine* getEngine();
459 void getEquivalenceClass( Node a, std::vector< Node >& eqc );
460 TNode getCongruentTerm( Node f, std::vector< TNode >& args );
461 /** getInternalRepresentative gets the current best representative in the equivalence class of a, based on some criteria.
462 If cbqi is active, this will return a term in the equivalence class of "a" that does
463 not contain instantiation constants, if such a term exists.
464 */
465 Node getInternalRepresentative( Node a, Node f, int index );
466 /** get quantifiers equality inference */
467 quantifiers::EqualityInference * getEqualityInference() { return d_eq_inference; }
468 }; /* EqualityQueryQuantifiersEngine */
469
470 }/* CVC4::theory namespace */
471 }/* CVC4 namespace */
472
473 #endif /* __CVC4__THEORY__QUANTIFIERS_ENGINE_H */