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