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