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/quantifiers/quant_util.h"
24 #include "expr/attribute.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 /* 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
) = 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 */
66 namespace quantifiers
{
68 class FirstOrderModel
;
70 class InstantiationEngine
;
72 class BoundedIntegers
;
73 class QuantConflictFind
;
76 }/* CVC4::theory::quantifiers */
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 quantifiers::QuantConflictFind
;
90 friend class inst::InstMatch
;
92 typedef context::CDHashMap
< Node
, bool, NodeHashFunction
> BoolMap
;
93 /** reference to theory engine object */
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 /** instantiation engine */
106 quantifiers::InstantiationEngine
* d_inst_engine
;
108 quantifiers::ModelEngine
* d_model_engine
;
109 /** bounded integers utility */
110 quantifiers::BoundedIntegers
* d_bint
;
111 /** Conflict find mechanism for quantifiers */
112 quantifiers::QuantConflictFind
* d_qcf
;
113 /** rewrite rules utility */
114 quantifiers::RewriteEngine
* d_rr_engine
;
116 /** list of all quantifiers seen */
117 std::vector
< Node
> d_quants
;
118 /** list of all lemmas produced */
119 //std::map< Node, bool > d_lemmas_produced;
120 BoolMap d_lemmas_produced_c
;
121 /** lemmas waiting */
122 std::vector
< Node
> d_lemmas_waiting
;
123 /** has added lemma this round */
124 bool d_hasAddedLemma
;
125 /** list of all instantiations produced for each quantifier */
126 std::map
< Node
, inst::InstMatchTrie
> d_inst_match_trie
;
127 std::map
< Node
, inst::CDInstMatchTrie
* > d_c_inst_match_trie
;
129 quantifiers::TermDb
* d_term_db
;
130 /** all triggers will be stored in this trie */
131 inst::TriggerTrie
* d_tr_trie
;
132 /** extended model object */
133 quantifiers::FirstOrderModel
* d_model
;
134 /** statistics for debugging */
135 std::map
< Node
, int > d_total_inst_debug
;
136 std::map
< Node
, int > d_temp_inst_debug
;
137 int d_total_inst_count_debug
;
139 KEEP_STATISTIC(TimerStat
, d_time
, "theory::QuantifiersEngine::time");
141 QuantifiersEngine(context::Context
* c
, context::UserContext
* u
, TheoryEngine
* te
);
142 ~QuantifiersEngine();
143 /** get theory engine */
144 TheoryEngine
* getTheoryEngine() { return d_te
; }
145 /** get equality query object for the given type. The default is the
147 EqualityQueryQuantifiersEngine
* getEqualityQuery();
148 /** get instantiation engine */
149 quantifiers::InstantiationEngine
* getInstantiationEngine() { return d_inst_engine
; }
150 /** get model engine */
151 quantifiers::ModelEngine
* getModelEngine() { return d_model_engine
; }
152 /** get default sat context for quantifiers engine */
153 context::Context
* getSatContext();
154 /** get default sat context for quantifiers engine */
155 context::Context
* getUserContext();
156 /** get default output channel for the quantifiers engine */
157 OutputChannel
& getOutputChannel();
158 /** get default valuation for the quantifiers engine */
159 Valuation
& getValuation();
160 /** get relevant domain */
161 quantifiers::RelevantDomain
* getRelevantDomain() { return d_rel_dom
; }
162 /** get quantifier relevance */
163 QuantRelevance
* getQuantifierRelevance() { return d_quant_rel
; }
164 /** get phase requirement information */
165 QuantPhaseReq
* getPhaseRequirements( Node f
) { return d_phase_reqs
.find( f
)==d_phase_reqs
.end() ? NULL
: d_phase_reqs
[f
]; }
166 /** get phase requirement terms */
167 void getPhaseReqTerms( Node f
, std::vector
< Node
>& nodes
);
168 /** get bounded integers utility */
169 quantifiers::BoundedIntegers
* getBoundedIntegers() { return d_bint
; }
170 /** Conflict find mechanism for quantifiers */
171 quantifiers::QuantConflictFind
* getConflictFind() { return d_qcf
; }
175 /** check at level */
176 void check( Theory::Effort e
);
177 /** register quantifier */
178 void registerQuantifier( Node f
);
179 /** register quantifier */
180 void registerPattern( std::vector
<Node
> & pattern
);
181 /** assert universal quantifier */
182 void assertNode( Node f
);
184 void propagate( Theory::Effort level
);
185 /** get next decision request */
186 Node
getNextDecisionRequest();
188 /** compute term vector */
189 void computeTermVector( Node f
, InstMatch
& m
, std::vector
< Node
>& vars
, std::vector
< Node
>& terms
);
190 /** instantiate f with arguments terms */
191 bool addInstantiation( Node f
, std::vector
< Node
>& vars
, std::vector
< Node
>& terms
);
192 /** set instantiation level attr */
193 void setInstantiationLevelAttr( Node n
, uint64_t level
);
195 /** get instantiation */
196 Node
getInstantiation( Node f
, std::vector
< Node
>& vars
, std::vector
< Node
>& terms
);
197 /** get instantiation */
198 Node
getInstantiation( Node f
, InstMatch
& m
);
199 /** get instantiation */
200 Node
getInstantiation( Node f
, std::vector
< Node
>& terms
);
201 /** do substitution */
202 Node
getSubstitute( Node n
, std::vector
< Node
>& terms
);
203 /** exist instantiation ? */
204 bool existsInstantiation( Node f
, InstMatch
& m
, bool modEq
= true, bool modInst
= false );
206 bool addLemma( Node lem
, bool doCache
= true );
207 /** do instantiation specified by m */
208 bool addInstantiation( Node f
, InstMatch
& m
, bool mkRep
= true, bool modEq
= false, bool modInst
= false );
209 /** add instantiation */
210 bool addInstantiation( Node f
, std::vector
< Node
>& terms
, bool mkRep
= true, bool modEq
= false, bool modInst
= false );
211 /** split on node n */
212 bool addSplit( Node n
, bool reqPhase
= false, bool reqPhasePol
= true );
213 /** add split equality */
214 bool addSplitEquality( Node n1
, Node n2
, bool reqPhase
= false, bool reqPhasePol
= true );
215 /** has added lemma */
216 bool hasAddedLemma() { return !d_lemmas_waiting
.empty() || d_hasAddedLemma
; }
218 void flushLemmas( OutputChannel
* out
= NULL
);
219 /** get number of waiting lemmas */
220 int getNumLemmasWaiting() { return (int)d_lemmas_waiting
.size(); }
222 /** get number of quantifiers */
223 int getNumQuantifiers() { return (int)d_quants
.size(); }
224 /** get quantifier */
225 Node
getQuantifier( int i
) { return d_quants
[i
]; }
228 quantifiers::FirstOrderModel
* getModel() { return d_model
; }
229 /** get term database */
230 quantifiers::TermDb
* getTermDatabase() { return d_term_db
; }
231 /** get trigger database */
232 inst::TriggerTrie
* getTriggerDatabase() { return d_tr_trie
; }
233 /** add term to database */
234 void addTermToDatabase( Node n
, bool withinQuant
= false );
235 /** get the master equality engine */
236 eq::EqualityEngine
* getMasterEqualityEngine() ;
238 /** statistics class */
242 IntStat d_instantiation_rounds
;
243 IntStat d_instantiation_rounds_lc
;
244 IntStat d_instantiations
;
245 IntStat d_inst_duplicate
;
246 IntStat d_inst_duplicate_eq
;
247 IntStat d_lit_phase_req
;
248 IntStat d_lit_phase_nreq
;
250 IntStat d_simple_triggers
;
251 IntStat d_multi_triggers
;
252 IntStat d_multi_trigger_instantiations
;
255 };/* class QuantifiersEngine::Statistics */
256 Statistics d_statistics
;
257 };/* class QuantifiersEngine */
261 /** equality query object using theory engine */
262 class EqualityQueryQuantifiersEngine
: public EqualityQuery
265 /** pointer to theory engine */
266 QuantifiersEngine
* d_qe
;
267 /** internal representatives */
268 std::map
< Node
, Node
> d_int_rep
;
270 std::map
< Node
, int > d_rep_score
;
277 Node
getInstance( Node n
, std::vector
< Node
>& eqc
);
279 int getRepScore( Node n
, Node f
, int index
);
281 EqualityQueryQuantifiersEngine( QuantifiersEngine
* qe
) : d_qe( qe
), d_reset_count( 0 ), d_liberal( false ){}
282 ~EqualityQueryQuantifiersEngine(){}
285 /** general queries about equality */
286 bool hasTerm( Node a
);
287 Node
getRepresentative( Node a
);
288 bool areEqual( Node a
, Node b
);
289 bool areDisequal( Node a
, Node b
);
290 eq::EqualityEngine
* getEngine();
291 void getEquivalenceClass( Node a
, std::vector
< Node
>& eqc
);
292 /** getInternalRepresentative gets the current best representative in the equivalence class of a, based on some criteria.
293 If cbqi is active, this will return a term in the equivalence class of "a" that does
294 not contain instantiation constants, if such a term exists.
296 Node
getInternalRepresentative( Node a
, Node f
, int index
);
297 /** flatten representatives */
298 void flattenRepresentatives( std::map
< TypeNode
, std::vector
< Node
> >& reps
);
300 void setLiberal( bool l
) { d_liberal
= l
; }
301 }; /* EqualityQueryQuantifiersEngine */
303 }/* CVC4::theory namespace */
304 }/* CVC4 namespace */
306 #endif /* __CVC4__THEORY__QUANTIFIERS_ENGINE_H */