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