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