Make --var-elim-quant true by default. Add rewrite engine to quantifiers module.
[cvc5.git] / src / theory / quantifiers_engine.h
1 /********************* */
2 /*! \file quantifiers_engine.h
3 ** \verbatim
4 ** Original author: Morgan Deters
5 ** Major contributors: Andrew Reynolds
6 ** Minor contributors (to current version): Francois Bobot
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2013 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** 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 "theory/theory.h"
21 #include "util/hash.h"
22 #include "theory/quantifiers/inst_match.h"
23 #include "theory/rewriterules/rr_inst_match.h"
24 #include "theory/quantifiers/quant_util.h"
25
26 #include "util/statistics_registry.h"
27
28 #include <ext/hash_set>
29 #include <iostream>
30 #include <map>
31
32 namespace CVC4 {
33
34 class TheoryEngine;
35
36 namespace theory {
37
38 class QuantifiersEngine;
39
40 class QuantifiersModule {
41 protected:
42 QuantifiersEngine* d_quantEngine;
43 public:
44 QuantifiersModule( QuantifiersEngine* qe ) : d_quantEngine( qe ){}
45 virtual ~QuantifiersModule(){}
46 //get quantifiers engine
47 QuantifiersEngine* getQuantifiersEngine() { return d_quantEngine; }
48 /** initialize */
49 virtual void finishInit() {}
50 /* whether this module needs to check this round */
51 virtual bool needsCheck( Theory::Effort e ) { return e>=Theory::EFFORT_LAST_CALL; }
52 /* Call during quantifier engine's check */
53 virtual void check( Theory::Effort e ) = 0;
54 /* Called for new quantifiers */
55 virtual void registerQuantifier( Node n ) = 0;
56 virtual void assertNode( Node n ) = 0;
57 virtual void propagate( Theory::Effort level ){}
58 virtual Node getNextDecisionRequest() { return TNode::null(); }
59 virtual Node explain(TNode n) { return TNode::null(); }
60 };/* class QuantifiersModule */
61
62 namespace quantifiers {
63 class TermDb;
64 class FirstOrderModel;
65 //modules
66 class InstantiationEngine;
67 class ModelEngine;
68 class BoundedIntegers;
69 class RewriteEngine;
70 }/* CVC4::theory::quantifiers */
71
72 namespace inst {
73 class TriggerTrie;
74 }/* CVC4::theory::inst */
75
76 namespace rrinst {
77 class TriggerTrie;
78 }/* CVC4::theory::inst */
79
80 class EfficientEMatcher;
81 class EqualityQueryQuantifiersEngine;
82
83 class QuantifiersEngine {
84 friend class quantifiers::InstantiationEngine;
85 friend class quantifiers::ModelEngine;
86 friend class inst::InstMatch;
87 private:
88 typedef context::CDHashMap< Node, bool, NodeHashFunction > BoolMap;
89 /** reference to theory engine object */
90 TheoryEngine* d_te;
91 /** vector of modules for quantifiers */
92 std::vector< QuantifiersModule* > d_modules;
93 /** equality query class */
94 EqualityQueryQuantifiersEngine* d_eq_query;
95 /** for computing relevance of quantifiers */
96 QuantRelevance d_quant_rel;
97 /** phase requirements for each quantifier for each instantiation literal */
98 std::map< Node, QuantPhaseReq* > d_phase_reqs;
99 /** efficient e-matcher */
100 EfficientEMatcher* d_eem;
101 /** instantiation engine */
102 quantifiers::InstantiationEngine* d_inst_engine;
103 /** model engine */
104 quantifiers::ModelEngine* d_model_engine;
105 /** bounded integers utility */
106 quantifiers::BoundedIntegers * d_bint;
107 /** rewrite rules utility */
108 quantifiers::RewriteEngine * d_rr_engine;
109 private:
110 /** list of all quantifiers seen */
111 std::vector< Node > d_quants;
112 /** list of all lemmas produced */
113 std::map< Node, bool > d_lemmas_produced;
114 BoolMap d_lemmas_produced_c;
115 /** lemmas waiting */
116 std::vector< Node > d_lemmas_waiting;
117 /** has added lemma this round */
118 bool d_hasAddedLemma;
119 /** list of all instantiations produced for each quantifier */
120 std::map< Node, inst::CDInstMatchTrie* > d_inst_match_trie;
121 /** term database */
122 quantifiers::TermDb* d_term_db;
123 /** all triggers will be stored in this trie */
124 inst::TriggerTrie* d_tr_trie;
125 /** all triggers for rewrite rules will be stored in this trie */
126 rrinst::TriggerTrie* d_rr_tr_trie;
127 /** extended model object */
128 quantifiers::FirstOrderModel* d_model;
129 /** statistics for debugging */
130 std::map< Node, int > d_total_inst_debug;
131 std::map< Node, int > d_temp_inst_debug;
132 int d_total_inst_count_debug;
133 private:
134 KEEP_STATISTIC(TimerStat, d_time, "theory::QuantifiersEngine::time");
135 public:
136 QuantifiersEngine(context::Context* c, context::UserContext* u, TheoryEngine* te);
137 ~QuantifiersEngine();
138 /** get instantiator for id */
139 //Instantiator* getInstantiator( theory::TheoryId id );
140 /** get theory engine */
141 TheoryEngine* getTheoryEngine() { return d_te; }
142 /** get equality query object for the given type. The default is the
143 generic one */
144 EqualityQuery* getEqualityQuery();
145 /** get instantiation engine */
146 quantifiers::InstantiationEngine* getInstantiationEngine() { return d_inst_engine; }
147 /** get model engine */
148 quantifiers::ModelEngine* getModelEngine() { return d_model_engine; }
149 /** get default sat context for quantifiers engine */
150 context::Context* getSatContext();
151 /** get default sat context for quantifiers engine */
152 context::Context* getUserContext();
153 /** get default output channel for the quantifiers engine */
154 OutputChannel& getOutputChannel();
155 /** get default valuation for the quantifiers engine */
156 Valuation& getValuation();
157 /** get quantifier relevance */
158 QuantRelevance* getQuantifierRelevance() { return &d_quant_rel; }
159 /** get phase requirement information */
160 QuantPhaseReq* getPhaseRequirements( Node f ) { return d_phase_reqs.find( f )==d_phase_reqs.end() ? NULL : d_phase_reqs[f]; }
161 /** get phase requirement terms */
162 void getPhaseReqTerms( Node f, std::vector< Node >& nodes );
163 /** get efficient e-matching utility */
164 EfficientEMatcher* getEfficientEMatcher() { return d_eem; }
165 /** get bounded integers utility */
166 quantifiers::BoundedIntegers * getBoundedIntegers() { return d_bint; }
167 public:
168 /** initialize */
169 void finishInit();
170 /** check at level */
171 void check( Theory::Effort e );
172 /** register quantifier */
173 void registerQuantifier( Node f );
174 /** register quantifier */
175 void registerPattern( std::vector<Node> & pattern);
176 /** assert universal quantifier */
177 void assertNode( Node f );
178 /** propagate */
179 void propagate( Theory::Effort level );
180 /** get next decision request */
181 Node getNextDecisionRequest();
182 private:
183 /** compute term vector */
184 void computeTermVector( Node f, InstMatch& m, std::vector< Node >& vars, std::vector< Node >& terms );
185 /** instantiate f with arguments terms */
186 bool addInstantiation( Node f, std::vector< Node >& vars, std::vector< Node >& terms );
187 /** set instantiation level attr */
188 void setInstantiationLevelAttr( Node n, uint64_t level );
189 public:
190 /** get instantiation */
191 Node getInstantiation( Node f, std::vector< Node >& vars, std::vector< Node >& terms );
192 /** get instantiation */
193 Node getInstantiation( Node f, InstMatch& m );
194 /** exist instantiation ? */
195 bool existsInstantiation( Node f, InstMatch& m, bool modEq = true, bool modInst = false );
196 /** add lemma lem */
197 bool addLemma( Node lem );
198 /** do instantiation specified by m */
199 bool addInstantiation( Node f, InstMatch& m, bool modEq = true, bool modInst = false, bool mkRep = true );
200 /** split on node n */
201 bool addSplit( Node n, bool reqPhase = false, bool reqPhasePol = true );
202 /** add split equality */
203 bool addSplitEquality( Node n1, Node n2, bool reqPhase = false, bool reqPhasePol = true );
204 /** has added lemma */
205 bool hasAddedLemma() { return !d_lemmas_waiting.empty() || d_hasAddedLemma; }
206 /** flush lemmas */
207 void flushLemmas( OutputChannel* out );
208 /** get number of waiting lemmas */
209 int getNumLemmasWaiting() { return (int)d_lemmas_waiting.size(); }
210 public:
211 /** get number of quantifiers */
212 int getNumQuantifiers() { return (int)d_quants.size(); }
213 /** get quantifier */
214 Node getQuantifier( int i ) { return d_quants[i]; }
215 public:
216 /** get model */
217 quantifiers::FirstOrderModel* getModel() { return d_model; }
218 /** get term database */
219 quantifiers::TermDb* getTermDatabase() { return d_term_db; }
220 /** get trigger database */
221 inst::TriggerTrie* getTriggerDatabase() { return d_tr_trie; }
222 /** get rewrite trigger database */
223 rrinst::TriggerTrie* getRRTriggerDatabase() { return d_rr_tr_trie; }
224 /** add term to database */
225 void addTermToDatabase( Node n, bool withinQuant = false );
226 /** get the master equality engine */
227 eq::EqualityEngine* getMasterEqualityEngine() ;
228 public:
229 /** statistics class */
230 class Statistics {
231 public:
232 IntStat d_num_quant;
233 IntStat d_instantiation_rounds;
234 IntStat d_instantiation_rounds_lc;
235 IntStat d_instantiations;
236 IntStat d_max_instantiation_level;
237 IntStat d_splits;
238 IntStat d_total_inst_var;
239 IntStat d_total_inst_var_unspec;
240 IntStat d_inst_unspec;
241 IntStat d_inst_duplicate;
242 IntStat d_lit_phase_req;
243 IntStat d_lit_phase_nreq;
244 IntStat d_triggers;
245 IntStat d_simple_triggers;
246 IntStat d_multi_triggers;
247 IntStat d_multi_trigger_instantiations;
248 IntStat d_term_in_termdb;
249 IntStat d_num_mono_candidates;
250 IntStat d_num_mono_candidates_new_term;
251 IntStat d_num_multi_candidates;
252 IntStat d_mono_candidates_cache_hit;
253 IntStat d_mono_candidates_cache_miss;
254 IntStat d_multi_candidates_cache_hit;
255 IntStat d_multi_candidates_cache_miss;
256 Statistics();
257 ~Statistics();
258 };/* class QuantifiersEngine::Statistics */
259 Statistics d_statistics;
260 public:
261 /** options */
262 bool d_optInstCheckDuplicate;
263 bool d_optInstMakeRepresentative;
264 bool d_optInstAddSplits;
265 bool d_optMatchIgnoreModelBasis;
266 bool d_optInstLimitActive;
267 int d_optInstLimit;
268 };/* class QuantifiersEngine */
269
270
271
272 /** equality query object using theory engine */
273 class EqualityQueryQuantifiersEngine : public EqualityQuery
274 {
275 private:
276 /** pointer to theory engine */
277 QuantifiersEngine* d_qe;
278 /** internal representatives */
279 std::map< int, std::map< Node, Node > > d_int_rep;
280 /** rep score */
281 std::map< Node, int > d_rep_score;
282 /** reset count */
283 int d_reset_count;
284
285 bool d_liberal;
286 private:
287 /** node contains */
288 Node getInstance( Node n, std::vector< Node >& eqc );
289 /** get score */
290 int getRepScore( Node n, Node f, int index );
291 /** choose rep based on sort inference */
292 bool optInternalRepSortInference();
293 public:
294 EqualityQueryQuantifiersEngine( QuantifiersEngine* qe ) : d_qe( qe ), d_reset_count( 0 ), d_liberal( false ){}
295 ~EqualityQueryQuantifiersEngine(){}
296 /** reset */
297 void reset();
298 /** general queries about equality */
299 bool hasTerm( Node a );
300 Node getRepresentative( Node a );
301 bool areEqual( Node a, Node b );
302 bool areDisequal( Node a, Node b );
303 eq::EqualityEngine* getEngine();
304 void getEquivalenceClass( Node a, std::vector< Node >& eqc );
305 /** getInternalRepresentative gets the current best representative in the equivalence class of a, based on some criteria.
306 If cbqi is active, this will return a term in the equivalence class of "a" that does
307 not contain instantiation constants, if such a term exists.
308 */
309 Node getInternalRepresentative( Node a, Node f, int index );
310
311 void setLiberal( bool l ) { d_liberal = l; }
312 }; /* EqualityQueryQuantifiersEngine */
313
314 }/* CVC4::theory namespace */
315 }/* CVC4 namespace */
316
317 #endif /* __CVC4__THEORY__QUANTIFIERS_ENGINE_H */