1 /********************* */
2 /*! \file quantifiers_engine.h
4 ** Original author: ajreynol
5 ** Major contributors: none
6 ** Minor contributors (to current version): mdeters, bobot
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009-2012 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 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
) = 0;
60 };/* class QuantifiersModule */
62 namespace quantifiers
{
63 class InstantiationEngine
;
66 class FirstOrderModel
;
67 }/* CVC4::theory::quantifiers */
71 }/* CVC4::theory::inst */
75 }/* CVC4::theory::inst */
77 class EfficientEMatcher
;
78 class EqualityQueryQuantifiersEngine
;
80 class QuantifiersEngine
{
81 friend class quantifiers::InstantiationEngine
;
82 friend class quantifiers::ModelEngine
;
83 friend class inst::InstMatch
;
85 typedef context::CDHashMap
< Node
, bool, NodeHashFunction
> BoolMap
;
86 /** reference to theory engine object */
88 /** vector of modules for quantifiers */
89 std::vector
< QuantifiersModule
* > d_modules
;
90 /** instantiation engine */
91 quantifiers::InstantiationEngine
* d_inst_engine
;
93 quantifiers::ModelEngine
* d_model_engine
;
94 /** equality query class */
95 EqualityQueryQuantifiersEngine
* d_eq_query
;
96 /** for computing relevance of quantifiers */
97 QuantRelevance d_quant_rel
;
98 /** phase requirements for each quantifier for each instantiation literal */
99 std::map
< Node
, QuantPhaseReq
* > d_phase_reqs
;
100 /** efficient e-matcher */
101 EfficientEMatcher
* d_eem
;
103 /** list of all quantifiers seen */
104 std::vector
< Node
> d_quants
;
105 /** list of all lemmas produced */
106 std::map
< Node
, bool > d_lemmas_produced
;
107 BoolMap d_lemmas_produced_c
;
108 /** lemmas waiting */
109 std::vector
< Node
> d_lemmas_waiting
;
110 /** has added lemma this round */
111 bool d_hasAddedLemma
;
112 /** list of all instantiations produced for each quantifier */
113 std::map
< Node
, inst::CDInstMatchTrie
* > d_inst_match_trie
;
115 quantifiers::TermDb
* d_term_db
;
116 /** all triggers will be stored in this trie */
117 inst::TriggerTrie
* d_tr_trie
;
118 /** all triggers for rewrite rules will be stored in this trie */
119 rrinst::TriggerTrie
* d_rr_tr_trie
;
120 /** extended model object */
121 quantifiers::FirstOrderModel
* d_model
;
123 KEEP_STATISTIC(TimerStat
, d_time
, "theory::QuantifiersEngine::time");
125 QuantifiersEngine(context::Context
* c
, context::UserContext
* u
, TheoryEngine
* te
);
126 ~QuantifiersEngine();
127 /** get instantiator for id */
128 //Instantiator* getInstantiator( theory::TheoryId id );
129 /** get theory engine */
130 TheoryEngine
* getTheoryEngine() { return d_te
; }
131 /** get equality query object for the given type. The default is the
133 EqualityQuery
* getEqualityQuery();
134 /** get instantiation engine */
135 quantifiers::InstantiationEngine
* getInstantiationEngine() { return d_inst_engine
; }
136 /** get model engine */
137 quantifiers::ModelEngine
* getModelEngine() { return d_model_engine
; }
138 /** get default sat context for quantifiers engine */
139 context::Context
* getSatContext();
140 /** get default sat context for quantifiers engine */
141 context::Context
* getUserContext();
142 /** get default output channel for the quantifiers engine */
143 OutputChannel
& getOutputChannel();
144 /** get default valuation for the quantifiers engine */
145 Valuation
& getValuation();
146 /** get quantifier relevance */
147 QuantRelevance
* getQuantifierRelevance() { return &d_quant_rel
; }
148 /** get phase requirement information */
149 QuantPhaseReq
* getPhaseRequirements( Node f
) { return d_phase_reqs
.find( f
)==d_phase_reqs
.end() ? NULL
: d_phase_reqs
[f
]; }
150 /** get phase requirement terms */
151 void getPhaseReqTerms( Node f
, std::vector
< Node
>& nodes
);
152 /** get efficient e-matching utility */
153 EfficientEMatcher
* getEfficientEMatcher() { return d_eem
; }
157 /** check at level */
158 void check( Theory::Effort e
);
159 /** register quantifier */
160 void registerQuantifier( Node f
);
161 /** register quantifier */
162 void registerPattern( std::vector
<Node
> & pattern
);
163 /** assert universal quantifier */
164 void assertNode( Node f
);
166 void propagate( Theory::Effort level
);
167 /** get next decision request */
168 Node
getNextDecisionRequest();
170 /** compute term vector */
171 void computeTermVector( Node f
, InstMatch
& m
, std::vector
< Node
>& vars
, std::vector
< Node
>& terms
);
172 /** instantiate f with arguments terms */
173 bool addInstantiation( Node f
, std::vector
< Node
>& vars
, std::vector
< Node
>& terms
);
174 /** set instantiation level attr */
175 void setInstantiationLevelAttr( Node n
, uint64_t level
);
177 /** get instantiation */
178 Node
getInstantiation( Node f
, std::vector
< Node
>& vars
, std::vector
< Node
>& terms
);
179 /** get instantiation */
180 Node
getInstantiation( Node f
, InstMatch
& m
);
181 /** exist instantiation ? */
182 bool existsInstantiation( Node f
, InstMatch
& m
, bool modEq
= true, bool modInst
= false );
184 bool addLemma( Node lem
);
185 /** do instantiation specified by m */
186 bool addInstantiation( Node f
, InstMatch
& m
, bool modEq
= true, bool modInst
= false, bool mkRep
= true );
187 /** split on node n */
188 bool addSplit( Node n
, bool reqPhase
= false, bool reqPhasePol
= true );
189 /** add split equality */
190 bool addSplitEquality( Node n1
, Node n2
, bool reqPhase
= false, bool reqPhasePol
= true );
191 /** has added lemma */
192 bool hasAddedLemma() { return !d_lemmas_waiting
.empty() || d_hasAddedLemma
; }
194 void flushLemmas( OutputChannel
* out
);
195 /** get number of waiting lemmas */
196 int getNumLemmasWaiting() { return (int)d_lemmas_waiting
.size(); }
198 /** get number of quantifiers */
199 int getNumQuantifiers() { return (int)d_quants
.size(); }
200 /** get quantifier */
201 Node
getQuantifier( int i
) { return d_quants
[i
]; }
204 quantifiers::FirstOrderModel
* getModel() { return d_model
; }
205 /** get term database */
206 quantifiers::TermDb
* getTermDatabase() { return d_term_db
; }
207 /** get trigger database */
208 inst::TriggerTrie
* getTriggerDatabase() { return d_tr_trie
; }
209 /** get rewrite trigger database */
210 rrinst::TriggerTrie
* getRRTriggerDatabase() { return d_rr_tr_trie
; }
211 /** add term to database */
212 void addTermToDatabase( Node n
, bool withinQuant
= false );
213 eq::EqualityEngine
* getMasterEqualityEngine() ;
215 /** statistics class */
219 IntStat d_instantiation_rounds
;
220 IntStat d_instantiation_rounds_lc
;
221 IntStat d_instantiations
;
222 IntStat d_max_instantiation_level
;
224 IntStat d_total_inst_var
;
225 IntStat d_total_inst_var_unspec
;
226 IntStat d_inst_unspec
;
227 IntStat d_inst_duplicate
;
228 IntStat d_lit_phase_req
;
229 IntStat d_lit_phase_nreq
;
231 IntStat d_simple_triggers
;
232 IntStat d_multi_triggers
;
233 IntStat d_multi_trigger_instantiations
;
234 IntStat d_term_in_termdb
;
235 IntStat d_num_mono_candidates
;
236 IntStat d_num_mono_candidates_new_term
;
237 IntStat d_num_multi_candidates
;
238 IntStat d_mono_candidates_cache_hit
;
239 IntStat d_mono_candidates_cache_miss
;
240 IntStat d_multi_candidates_cache_hit
;
241 IntStat d_multi_candidates_cache_miss
;
244 };/* class QuantifiersEngine::Statistics */
245 Statistics d_statistics
;
248 bool d_optInstCheckDuplicate
;
249 bool d_optInstMakeRepresentative
;
250 bool d_optInstAddSplits
;
251 bool d_optMatchIgnoreModelBasis
;
252 bool d_optInstLimitActive
;
254 };/* class QuantifiersEngine */
258 /** equality query object using theory engine */
259 class EqualityQueryQuantifiersEngine
: public EqualityQuery
262 /** pointer to theory engine */
263 QuantifiersEngine
* d_qe
;
264 /** internal representatives */
265 std::map
< Node
, Node
> d_int_rep
;
267 std::map
< Node
, int > d_rep_score
;
272 Node
getInstance( Node n
, std::vector
< Node
>& eqc
);
274 int getRepScore( Node n
);
276 EqualityQueryQuantifiersEngine( QuantifiersEngine
* qe
) : d_qe( qe
), d_reset_count( 0 ){}
277 ~EqualityQueryQuantifiersEngine(){}
280 /** general queries about equality */
281 bool hasTerm( Node a
);
282 Node
getRepresentative( Node a
);
283 bool areEqual( Node a
, Node b
);
284 bool areDisequal( Node a
, Node b
);
285 eq::EqualityEngine
* getEngine();
286 void getEquivalenceClass( Node a
, std::vector
< Node
>& eqc
);
287 /** getInternalRepresentative gets the current best representative in the equivalence class of a, based on some criteria.
288 If cbqi is active, this will return a term in the equivalence class of "a" that does
289 not contain instantiation constants, if such a term exists.
291 Node
getInternalRepresentative( Node a
);
292 }; /* EqualityQueryQuantifiersEngine */
294 }/* CVC4::theory namespace */
295 }/* CVC4 namespace */
297 #endif /* __CVC4__THEORY__QUANTIFIERS_ENGINE_H */