Merge branch 'master' of github.com:tiliang/CVC4
[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 q ) = 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 QuantConflictFind;
70 class RewriteEngine;
71 class RelevantDomain;
72 }/* CVC4::theory::quantifiers */
73
74 namespace inst {
75 class TriggerTrie;
76 }/* CVC4::theory::inst */
77
78 namespace rrinst {
79 class TriggerTrie;
80 }/* CVC4::theory::inst */
81
82 class EfficientEMatcher;
83 class EqualityQueryQuantifiersEngine;
84
85 class QuantifiersEngine {
86 friend class quantifiers::InstantiationEngine;
87 friend class quantifiers::ModelEngine;
88 friend class quantifiers::RewriteEngine;
89 friend class quantifiers::QuantConflictFind;
90 friend class inst::InstMatch;
91 private:
92 typedef context::CDHashMap< Node, bool, NodeHashFunction > BoolMap;
93 /** reference to theory engine object */
94 TheoryEngine* d_te;
95 /** vector of modules for quantifiers */
96 std::vector< QuantifiersModule* > d_modules;
97 /** equality query class */
98 EqualityQueryQuantifiersEngine* d_eq_query;
99 /** for computing relevance of quantifiers */
100 QuantRelevance * d_quant_rel;
101 /** relevant domain */
102 quantifiers::RelevantDomain* d_rel_dom;
103 /** phase requirements for each quantifier for each instantiation literal */
104 std::map< Node, QuantPhaseReq* > d_phase_reqs;
105 /** efficient e-matcher */
106 EfficientEMatcher* d_eem;
107 /** instantiation engine */
108 quantifiers::InstantiationEngine* d_inst_engine;
109 /** model engine */
110 quantifiers::ModelEngine* d_model_engine;
111 /** bounded integers utility */
112 quantifiers::BoundedIntegers * d_bint;
113 /** Conflict find mechanism for quantifiers */
114 quantifiers::QuantConflictFind* d_qcf;
115 /** rewrite rules utility */
116 quantifiers::RewriteEngine * d_rr_engine;
117 private:
118 /** list of all quantifiers seen */
119 std::vector< Node > d_quants;
120 /** list of all lemmas produced */
121 //std::map< Node, bool > d_lemmas_produced;
122 BoolMap d_lemmas_produced_c;
123 /** lemmas waiting */
124 std::vector< Node > d_lemmas_waiting;
125 /** has added lemma this round */
126 bool d_hasAddedLemma;
127 /** list of all instantiations produced for each quantifier */
128 std::map< Node, inst::InstMatchTrie > d_inst_match_trie;
129 std::map< Node, inst::CDInstMatchTrie* > d_c_inst_match_trie;
130 /** term database */
131 quantifiers::TermDb* d_term_db;
132 /** all triggers will be stored in this trie */
133 inst::TriggerTrie* d_tr_trie;
134 /** all triggers for rewrite rules will be stored in this trie */
135 rrinst::TriggerTrie* d_rr_tr_trie;
136 /** extended model object */
137 quantifiers::FirstOrderModel* d_model;
138 /** statistics for debugging */
139 std::map< Node, int > d_total_inst_debug;
140 std::map< Node, int > d_temp_inst_debug;
141 int d_total_inst_count_debug;
142 private:
143 KEEP_STATISTIC(TimerStat, d_time, "theory::QuantifiersEngine::time");
144 public:
145 QuantifiersEngine(context::Context* c, context::UserContext* u, TheoryEngine* te);
146 ~QuantifiersEngine();
147 /** get instantiator for id */
148 //Instantiator* getInstantiator( theory::TheoryId id );
149 /** get theory engine */
150 TheoryEngine* getTheoryEngine() { return d_te; }
151 /** get equality query object for the given type. The default is the
152 generic one */
153 EqualityQueryQuantifiersEngine* getEqualityQuery();
154 /** get instantiation engine */
155 quantifiers::InstantiationEngine* getInstantiationEngine() { return d_inst_engine; }
156 /** get model engine */
157 quantifiers::ModelEngine* getModelEngine() { return d_model_engine; }
158 /** get default sat context for quantifiers engine */
159 context::Context* getSatContext();
160 /** get default sat context for quantifiers engine */
161 context::Context* getUserContext();
162 /** get default output channel for the quantifiers engine */
163 OutputChannel& getOutputChannel();
164 /** get default valuation for the quantifiers engine */
165 Valuation& getValuation();
166 /** get relevant domain */
167 quantifiers::RelevantDomain* getRelevantDomain() { return d_rel_dom; }
168 /** get quantifier relevance */
169 QuantRelevance* getQuantifierRelevance() { return d_quant_rel; }
170 /** get phase requirement information */
171 QuantPhaseReq* getPhaseRequirements( Node f ) { return d_phase_reqs.find( f )==d_phase_reqs.end() ? NULL : d_phase_reqs[f]; }
172 /** get phase requirement terms */
173 void getPhaseReqTerms( Node f, std::vector< Node >& nodes );
174 /** get efficient e-matching utility */
175 EfficientEMatcher* getEfficientEMatcher() { return d_eem; }
176 /** get bounded integers utility */
177 quantifiers::BoundedIntegers * getBoundedIntegers() { return d_bint; }
178 /** Conflict find mechanism for quantifiers */
179 quantifiers::QuantConflictFind* getConflictFind() { return d_qcf; }
180 public:
181 /** initialize */
182 void finishInit();
183 /** check at level */
184 void check( Theory::Effort e );
185 /** register quantifier */
186 void registerQuantifier( Node f );
187 /** register quantifier */
188 void registerPattern( std::vector<Node> & pattern);
189 /** assert universal quantifier */
190 void assertNode( Node f );
191 /** propagate */
192 void propagate( Theory::Effort level );
193 /** get next decision request */
194 Node getNextDecisionRequest();
195 private:
196 /** compute term vector */
197 void computeTermVector( Node f, InstMatch& m, std::vector< Node >& vars, std::vector< Node >& terms );
198 /** instantiate f with arguments terms */
199 bool addInstantiation( Node f, std::vector< Node >& vars, std::vector< Node >& terms );
200 /** set instantiation level attr */
201 void setInstantiationLevelAttr( Node n, uint64_t level );
202 /** do substitution */
203 Node doSubstitute( Node n, std::vector< Node >& terms );
204 public:
205 /** get instantiation */
206 Node getInstantiation( Node f, std::vector< Node >& vars, std::vector< Node >& terms );
207 /** get instantiation */
208 Node getInstantiation( Node f, InstMatch& m );
209 /** get instantiation */
210 Node getInstantiation( Node f, std::vector< Node >& terms );
211 /** exist instantiation ? */
212 bool existsInstantiation( Node f, InstMatch& m, bool modEq = true, bool modInst = false );
213 /** add lemma lem */
214 bool addLemma( Node lem, bool doCache = true );
215 /** do instantiation specified by m */
216 bool addInstantiation( Node f, InstMatch& m, bool mkRep = true, bool modEq = false, bool modInst = false );
217 /** add instantiation */
218 bool addInstantiation( Node f, std::vector< Node >& terms, bool mkRep = true, bool modEq = false, bool modInst = false );
219 /** split on node n */
220 bool addSplit( Node n, bool reqPhase = false, bool reqPhasePol = true );
221 /** add split equality */
222 bool addSplitEquality( Node n1, Node n2, bool reqPhase = false, bool reqPhasePol = true );
223 /** has added lemma */
224 bool hasAddedLemma() { return !d_lemmas_waiting.empty() || d_hasAddedLemma; }
225 /** flush lemmas */
226 void flushLemmas( OutputChannel* out = NULL );
227 /** get number of waiting lemmas */
228 int getNumLemmasWaiting() { return (int)d_lemmas_waiting.size(); }
229 public:
230 /** get number of quantifiers */
231 int getNumQuantifiers() { return (int)d_quants.size(); }
232 /** get quantifier */
233 Node getQuantifier( int i ) { return d_quants[i]; }
234 public:
235 /** get model */
236 quantifiers::FirstOrderModel* getModel() { return d_model; }
237 /** get term database */
238 quantifiers::TermDb* getTermDatabase() { return d_term_db; }
239 /** get trigger database */
240 inst::TriggerTrie* getTriggerDatabase() { return d_tr_trie; }
241 /** get rewrite trigger database */
242 rrinst::TriggerTrie* getRRTriggerDatabase() { return d_rr_tr_trie; }
243 /** add term to database */
244 void addTermToDatabase( Node n, bool withinQuant = false );
245 /** get the master equality engine */
246 eq::EqualityEngine* getMasterEqualityEngine() ;
247 public:
248 /** statistics class */
249 class Statistics {
250 public:
251 IntStat d_num_quant;
252 IntStat d_instantiation_rounds;
253 IntStat d_instantiation_rounds_lc;
254 IntStat d_instantiations;
255 IntStat d_inst_duplicate;
256 IntStat d_inst_duplicate_eq;
257 IntStat d_lit_phase_req;
258 IntStat d_lit_phase_nreq;
259 IntStat d_triggers;
260 IntStat d_simple_triggers;
261 IntStat d_multi_triggers;
262 IntStat d_multi_trigger_instantiations;
263 IntStat d_term_in_termdb;
264 IntStat d_num_mono_candidates;
265 IntStat d_num_mono_candidates_new_term;
266 IntStat d_num_multi_candidates;
267 IntStat d_mono_candidates_cache_hit;
268 IntStat d_mono_candidates_cache_miss;
269 IntStat d_multi_candidates_cache_hit;
270 IntStat d_multi_candidates_cache_miss;
271 Statistics();
272 ~Statistics();
273 };/* class QuantifiersEngine::Statistics */
274 Statistics d_statistics;
275 public:
276 /** options */
277 bool d_optInstCheckDuplicate;
278 bool d_optInstMakeRepresentative;
279 bool d_optInstAddSplits;
280 bool d_optMatchIgnoreModelBasis;
281 bool d_optInstLimitActive;
282 int d_optInstLimit;
283 };/* class QuantifiersEngine */
284
285
286
287 /** equality query object using theory engine */
288 class EqualityQueryQuantifiersEngine : public EqualityQuery
289 {
290 private:
291 /** pointer to theory engine */
292 QuantifiersEngine* d_qe;
293 /** internal representatives */
294 std::map< Node, Node > d_int_rep;
295 /** rep score */
296 std::map< Node, int > d_rep_score;
297 /** reset count */
298 int d_reset_count;
299
300 bool d_liberal;
301 private:
302 /** node contains */
303 Node getInstance( Node n, std::vector< Node >& eqc );
304 /** get score */
305 int getRepScore( Node n, Node f, int index );
306 public:
307 EqualityQueryQuantifiersEngine( QuantifiersEngine* qe ) : d_qe( qe ), d_reset_count( 0 ), d_liberal( false ){}
308 ~EqualityQueryQuantifiersEngine(){}
309 /** reset */
310 void reset();
311 /** general queries about equality */
312 bool hasTerm( Node a );
313 Node getRepresentative( Node a );
314 bool areEqual( Node a, Node b );
315 bool areDisequal( Node a, Node b );
316 eq::EqualityEngine* getEngine();
317 void getEquivalenceClass( Node a, std::vector< Node >& eqc );
318 /** getInternalRepresentative gets the current best representative in the equivalence class of a, based on some criteria.
319 If cbqi is active, this will return a term in the equivalence class of "a" that does
320 not contain instantiation constants, if such a term exists.
321 */
322 Node getInternalRepresentative( Node a, Node f, int index );
323 /** flatten representatives */
324 void flattenRepresentatives( std::map< TypeNode, std::vector< Node > >& reps );
325
326 void setLiberal( bool l ) { d_liberal = l; }
327 }; /* EqualityQueryQuantifiersEngine */
328
329 }/* CVC4::theory namespace */
330 }/* CVC4 namespace */
331
332 #endif /* __CVC4__THEORY__QUANTIFIERS_ENGINE_H */