1 /********************* */
2 /*! \file quantifiers_engine.h
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Morgan Deters, Tim King
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2016 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing 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 <ext/hash_set>
24 #include "context/cdchunk_list.h"
25 #include "context/cdhashset.h"
26 #include "expr/attribute.h"
27 #include "options/quantifiers_modes.h"
28 #include "theory/quantifiers/inst_match.h"
29 #include "theory/quantifiers/quant_util.h"
30 #include "theory/theory.h"
31 #include "util/hash.h"
32 #include "util/statistics_registry.h"
40 class QuantifiersEngine
;
42 namespace quantifiers
{
47 class InstantiationNotify
{
49 InstantiationNotify(){}
50 virtual bool notifyInstantiation( unsigned quant_e
, Node q
, Node lem
, std::vector
< Node
>& terms
, Node body
) = 0;
51 virtual void filterInstantiations() = 0;
54 namespace quantifiers
{
55 class FirstOrderModel
;
57 class InstantiationEngine
;
59 class BoundedIntegers
;
60 class QuantConflictFind
;
64 class ConjectureGenerator
;
65 class CegInstantiation
;
67 class AlphaEquivalence
;
69 class QuantEqualityEngine
;
71 class InstStrategyCbqi
;
72 class InstStrategyCegqi
;
74 class QuantAntiSkolem
;
75 class EqualityInference
;
77 }/* CVC4::theory::quantifiers */
81 }/* CVC4::theory::inst */
83 //class EfficientEMatcher;
84 class EqualityQueryQuantifiersEngine
;
86 class QuantifiersEngine
{
87 friend class quantifiers::InstantiationEngine
;
88 friend class quantifiers::InstStrategyCbqi
;
89 friend class quantifiers::InstStrategyCegqi
;
90 friend class quantifiers::ModelEngine
;
91 friend class quantifiers::RewriteEngine
;
92 friend class quantifiers::QuantConflictFind
;
93 friend class inst::InstMatch
;
94 typedef context::CDHashMap
< Node
, bool, NodeHashFunction
> BoolMap
;
95 typedef context::CDChunkList
<Node
> NodeList
;
96 typedef context::CDChunkList
<bool> BoolList
;
97 typedef context::CDHashSet
<Node
, NodeHashFunction
> NodeSet
;
99 /** reference to theory engine object */
101 /** vector of utilities for quantifiers */
102 std::vector
< QuantifiersUtil
* > d_util
;
103 /** vector of modules for quantifiers */
104 std::vector
< QuantifiersModule
* > d_modules
;
105 /** instantiation notify */
106 std::vector
< InstantiationNotify
* > d_inst_notify
;
107 /** equality query class */
108 EqualityQueryQuantifiersEngine
* d_eq_query
;
109 /** for computing relevance of quantifiers */
110 QuantRelevance
* d_quant_rel
;
111 /** relevant domain */
112 quantifiers::RelevantDomain
* d_rel_dom
;
113 /** alpha equivalence */
114 quantifiers::AlphaEquivalence
* d_alpha_equiv
;
116 quantifiers::QModelBuilder
* d_builder
;
117 /** utility for effectively propositional logic */
120 /** instantiation engine */
121 quantifiers::InstantiationEngine
* d_inst_engine
;
123 quantifiers::ModelEngine
* d_model_engine
;
124 /** bounded integers utility */
125 quantifiers::BoundedIntegers
* d_bint
;
126 /** Conflict find mechanism for quantifiers */
127 quantifiers::QuantConflictFind
* d_qcf
;
128 /** rewrite rules utility */
129 quantifiers::RewriteEngine
* d_rr_engine
;
130 /** subgoal generator */
131 quantifiers::ConjectureGenerator
* d_sg_gen
;
132 /** ceg instantiation */
133 quantifiers::CegInstantiation
* d_ceg_inst
;
134 /** lte partial instantiation */
135 quantifiers::LtePartialInst
* d_lte_part_inst
;
136 /** function definitions engine */
137 quantifiers::FunDefEngine
* d_fun_def_engine
;
138 /** quantifiers equality engine */
139 quantifiers::QuantEqualityEngine
* d_uee
;
140 /** full saturation */
141 quantifiers::FullSaturation
* d_fs
;
142 /** counterexample-based quantifier instantiation */
143 quantifiers::InstStrategyCbqi
* d_i_cbqi
;
144 /** quantifiers splitting */
145 quantifiers::QuantDSplit
* d_qsplit
;
146 /** quantifiers anti-skolemization */
147 quantifiers::QuantAntiSkolem
* d_anti_skolem
;
148 /** quantifiers instantiation propagtor */
149 quantifiers::InstPropagator
* d_inst_prop
;
150 public: //effort levels
159 private: //this information is reset during check
160 /** current effort level */
161 unsigned d_curr_effort_level
;
162 /** are we in conflict */
164 context::CDO
< bool > d_conflict_c
;
165 /** has added lemma this round */
166 bool d_hasAddedLemma
;
168 /** list of all quantifiers seen */
169 std::map
< Node
, bool > d_quants
;
170 /** quantifiers reduced */
171 BoolMap d_quants_red
;
172 std::map
< Node
, Node
> d_quants_red_lem
;
173 /** list of all lemmas produced */
174 //std::map< Node, bool > d_lemmas_produced;
175 BoolMap d_lemmas_produced_c
;
176 /** lemmas waiting */
177 std::vector
< Node
> d_lemmas_waiting
;
178 /** phase requirements waiting */
179 std::map
< Node
, bool > d_phase_req_waiting
;
180 /** list of all instantiations produced for each quantifier */
181 std::map
< Node
, inst::InstMatchTrie
> d_inst_match_trie
;
182 std::map
< Node
, inst::CDInstMatchTrie
* > d_c_inst_match_trie
;
183 /** recorded instantiations */
184 std::vector
< std::pair
< Node
, std::vector
< Node
> > > d_recorded_inst
;
185 /** quantifiers that have been skolemized */
186 BoolMap d_skolemized
;
188 quantifiers::TermDb
* d_term_db
;
189 /** all triggers will be stored in this trie */
190 inst::TriggerTrie
* d_tr_trie
;
191 /** extended model object */
192 quantifiers::FirstOrderModel
* d_model
;
193 /** statistics for debugging */
194 std::map
< Node
, int > d_total_inst_debug
;
195 std::map
< Node
, int > d_temp_inst_debug
;
196 int d_total_inst_count_debug
;
197 /** inst round counters TODO: make context-dependent? */
198 context::CDO
< int > d_ierCounter_c
;
201 int d_ierCounterLastLc
;
202 int d_inst_when_phase
;
203 /** has presolve been called */
204 context::CDO
< bool > d_presolve
;
205 /** presolve cache */
206 NodeSet d_presolve_in
;
207 NodeList d_presolve_cache
;
208 BoolList d_presolve_cache_wq
;
209 BoolList d_presolve_cache_wic
;
212 QuantifiersEngine(context::Context
* c
, context::UserContext
* u
, TheoryEngine
* te
);
213 ~QuantifiersEngine();
214 /** get theory engine */
215 TheoryEngine
* getTheoryEngine() { return d_te
; }
216 /** get equality query */
217 EqualityQueryQuantifiersEngine
* getEqualityQuery();
218 /** get default sat context for quantifiers engine */
219 context::Context
* getSatContext();
220 /** get default sat context for quantifiers engine */
221 context::UserContext
* getUserContext();
222 /** get default output channel for the quantifiers engine */
223 OutputChannel
& getOutputChannel();
224 /** get default valuation for the quantifiers engine */
225 Valuation
& getValuation();
226 /** get relevant domain */
227 quantifiers::RelevantDomain
* getRelevantDomain() { return d_rel_dom
; }
228 /** get quantifier relevance */
229 QuantRelevance
* getQuantifierRelevance() { return d_quant_rel
; }
230 /** get the model builder */
231 quantifiers::QModelBuilder
* getModelBuilder() { return d_builder
; }
232 /** get utility for EPR */
233 QuantEPR
* getQuantEPR() { return d_qepr
; }
235 /** get instantiation engine */
236 quantifiers::InstantiationEngine
* getInstantiationEngine() { return d_inst_engine
; }
237 /** get model engine */
238 quantifiers::ModelEngine
* getModelEngine() { return d_model_engine
; }
239 /** get bounded integers utility */
240 quantifiers::BoundedIntegers
* getBoundedIntegers() { return d_bint
; }
241 /** Conflict find mechanism for quantifiers */
242 quantifiers::QuantConflictFind
* getConflictFind() { return d_qcf
; }
243 /** rewrite rules utility */
244 quantifiers::RewriteEngine
* getRewriteEngine() { return d_rr_engine
; }
245 /** subgoal generator */
246 quantifiers::ConjectureGenerator
* getConjectureGenerator() { return d_sg_gen
; }
247 /** ceg instantiation */
248 quantifiers::CegInstantiation
* getCegInstantiation() { return d_ceg_inst
; }
249 /** local theory ext partial inst */
250 quantifiers::LtePartialInst
* getLtePartialInst() { return d_lte_part_inst
; }
251 /** function definition engine */
252 quantifiers::FunDefEngine
* getFunDefEngine() { return d_fun_def_engine
; }
253 /** quantifiers equality engine */
254 quantifiers::QuantEqualityEngine
* getQuantEqualityEngine() { return d_uee
; }
255 /** get full saturation */
256 quantifiers::FullSaturation
* getFullSaturation() { return d_fs
; }
257 /** get inst strategy cbqi */
258 quantifiers::InstStrategyCbqi
* getInstStrategyCbqi() { return d_i_cbqi
; }
259 /** get quantifiers splitting */
260 quantifiers::QuantDSplit
* getQuantDSplit() { return d_qsplit
; }
261 /** get quantifiers anti-skolemization */
262 quantifiers::QuantAntiSkolem
* getQuantAntiSkolem() { return d_anti_skolem
; }
264 /** owner of quantified formulas */
265 std::map
< Node
, QuantifiersModule
* > d_owner
;
266 std::map
< Node
, int > d_owner_priority
;
269 QuantifiersModule
* getOwner( Node q
);
271 void setOwner( Node q
, QuantifiersModule
* m
, int priority
= 0 );
273 bool hasOwnership( Node q
, QuantifiersModule
* m
= NULL
);
274 /** is finite bound */
275 bool isFiniteBound( Node q
, Node v
);
281 /** notify preprocessed assertion */
282 void ppNotifyAssertions( std::vector
< Node
>& assertions
);
283 /** check at level */
284 void check( Theory::Effort e
);
285 /** notify that theories were combined */
286 void notifyCombineTheories();
287 /** register quantifier */
288 bool registerQuantifier( Node f
);
289 /** register quantifier */
290 void registerPattern( std::vector
<Node
> & pattern
);
291 /** assert universal quantifier */
292 void assertQuantifier( Node q
, bool pol
);
294 void propagate( Theory::Effort level
);
295 /** get next decision request */
296 Node
getNextDecisionRequest( unsigned& priority
);
298 /** reduceQuantifier, return true if reduced */
299 bool reduceQuantifier( Node q
);
300 /** compute term vector */
301 void computeTermVector( Node f
, InstMatch
& m
, std::vector
< Node
>& vars
, std::vector
< Node
>& terms
);
302 /** record instantiation, return true if it was non-duplicate */
303 bool recordInstantiationInternal( Node q
, std::vector
< Node
>& terms
, bool modEq
= false, bool addedLem
= true );
304 /** remove instantiation */
305 bool removeInstantiationInternal( Node q
, std::vector
< Node
>& terms
);
306 /** set instantiation level attr */
307 static void setInstantiationLevelAttr( Node n
, Node qn
, uint64_t level
);
311 /** get instantiation */
312 Node
getInstantiation( Node q
, std::vector
< Node
>& vars
, std::vector
< Node
>& terms
, bool doVts
= false );
313 /** get instantiation */
314 Node
getInstantiation( Node q
, InstMatch
& m
, bool doVts
= false );
315 /** get instantiation */
316 Node
getInstantiation( Node q
, std::vector
< Node
>& terms
, bool doVts
= false );
317 /** do substitution */
318 Node
getSubstitute( Node n
, std::vector
< Node
>& terms
);
320 bool addLemma( Node lem
, bool doCache
= true, bool doRewrite
= true );
321 /** remove pending lemma */
322 bool removeLemma( Node lem
);
323 /** add require phase */
324 void addRequirePhase( Node lit
, bool req
);
325 /** do instantiation specified by m */
326 bool addInstantiation( Node q
, InstMatch
& m
, bool mkRep
= false, bool modEq
= false, bool doVts
= false );
327 /** add instantiation */
328 bool addInstantiation( Node q
, std::vector
< Node
>& terms
, bool mkRep
= false, bool modEq
= false, bool doVts
= false );
329 /** remove pending instantiation */
330 bool removeInstantiation( Node q
, Node lem
, std::vector
< Node
>& terms
);
331 /** split on node n */
332 bool addSplit( Node n
, bool reqPhase
= false, bool reqPhasePol
= true );
333 /** add split equality */
334 bool addSplitEquality( Node n1
, Node n2
, bool reqPhase
= false, bool reqPhasePol
= true );
336 bool addEPRAxiom( TypeNode tn
);
337 /** mark relevant quantified formula, this will indicate it should be checked before the others */
338 void markRelevant( Node q
);
339 /** has added lemma */
340 bool hasAddedLemma() { return !d_lemmas_waiting
.empty() || d_hasAddedLemma
; }
341 /** is in conflict */
342 bool inConflict() { return d_conflict
; }
343 /** get number of waiting lemmas */
344 unsigned getNumLemmasWaiting() { return d_lemmas_waiting
.size(); }
345 /** get needs check */
346 bool getInstWhenNeedsCheck( Theory::Effort e
);
347 /** get user pat mode */
348 quantifiers::UserPatMode
getInstUserPatMode();
349 /** set instantiation level attr */
350 static void setInstantiationLevelAttr( Node n
, uint64_t level
);
353 quantifiers::FirstOrderModel
* getModel() { return d_model
; }
354 /** get term database */
355 quantifiers::TermDb
* getTermDatabase() { return d_term_db
; }
356 /** get term database sygus */
357 quantifiers::TermDbSygus
* getTermDatabaseSygus();
358 /** get trigger database */
359 inst::TriggerTrie
* getTriggerDatabase() { return d_tr_trie
; }
360 /** add term to database */
361 void addTermToDatabase( Node n
, bool withinQuant
= false, bool withinInstClosure
= false );
362 /** notification when master equality engine is updated */
363 void eqNotifyNewClass(TNode t
);
364 void eqNotifyPreMerge(TNode t1
, TNode t2
);
365 void eqNotifyPostMerge(TNode t1
, TNode t2
);
366 void eqNotifyDisequal(TNode t1
, TNode t2
, TNode reason
);
367 /** get the master equality engine */
368 eq::EqualityEngine
* getMasterEqualityEngine() ;
369 /** debug print equality engine */
370 void debugPrintEqualityEngine( const char * c
);
372 /** print instantiations */
373 void printInstantiations( std::ostream
& out
);
374 /** print solution for synthesis conjectures */
375 void printSynthSolution( std::ostream
& out
);
376 /** get list of quantified formulas that were instantiated */
377 void getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
);
378 /** get instantiations */
379 void getInstantiations( Node q
, std::vector
< Node
>& insts
);
380 void getInstantiations( std::map
< Node
, std::vector
< Node
> >& insts
);
381 /** get instantiation term vectors */
382 void getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
);
383 void getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
);
384 /** get instantiated conjunction */
385 Node
getInstantiatedConjunction( Node q
);
386 /** get unsat core lemmas */
387 bool getUnsatCoreLemmas( std::vector
< Node
>& active_lemmas
);
388 bool getUnsatCoreLemmas( std::vector
< Node
>& active_lemmas
, std::map
< Node
, Node
>& weak_imp
);
389 /** get inst for lemmas */
390 void getExplanationForInstLemmas( std::vector
< Node
>& lems
, std::map
< Node
, Node
>& quant
, std::map
< Node
, std::vector
< Node
> >& tvec
);
391 /** statistics class */
395 TimerStat d_qcf_time
;
396 TimerStat d_ematching_time
;
398 IntStat d_instantiation_rounds
;
399 IntStat d_instantiation_rounds_lc
;
400 IntStat d_instantiations
;
401 IntStat d_inst_duplicate
;
402 IntStat d_inst_duplicate_eq
;
403 IntStat d_inst_duplicate_ent
;
405 IntStat d_simple_triggers
;
406 IntStat d_multi_triggers
;
407 IntStat d_multi_trigger_instantiations
;
408 IntStat d_red_alpha_equiv
;
409 IntStat d_instantiations_user_patterns
;
410 IntStat d_instantiations_auto_gen
;
411 IntStat d_instantiations_guess
;
412 IntStat d_instantiations_qcf
;
413 IntStat d_instantiations_qcf_prop
;
414 IntStat d_instantiations_fmf_exh
;
415 IntStat d_instantiations_fmf_mbqi
;
416 IntStat d_instantiations_cbqi
;
417 IntStat d_instantiations_rr
;
420 };/* class QuantifiersEngine::Statistics */
421 Statistics d_statistics
;
422 };/* class QuantifiersEngine */
426 /** equality query object using theory engine */
427 class EqualityQueryQuantifiersEngine
: public EqualityQuery
430 /** pointer to theory engine */
431 QuantifiersEngine
* d_qe
;
432 /** quantifiers equality inference */
433 quantifiers::EqualityInference
* d_eq_inference
;
434 context::CDO
< unsigned > d_eqi_counter
;
435 /** internal representatives */
436 std::map
< TypeNode
, std::map
< Node
, Node
> > d_int_rep
;
438 std::map
< Node
, int > d_rep_score
;
442 /** processInferences : will merge equivalence classes in master equality engine, if possible */
443 bool processInferences( Theory::Effort e
);
445 Node
getInstance( Node n
, const std::vector
< Node
>& eqc
, std::hash_map
<TNode
, Node
, TNodeHashFunction
>& cache
);
447 int getRepScore( Node n
, Node f
, int index
, TypeNode v_tn
);
448 /** flatten representatives */
449 void flattenRepresentatives( std::map
< TypeNode
, std::vector
< Node
> >& reps
);
451 EqualityQueryQuantifiersEngine( context::Context
* c
, QuantifiersEngine
* qe
);
452 virtual ~EqualityQueryQuantifiersEngine();
454 bool reset( Theory::Effort e
);
456 std::string
identify() const { return "EqualityQueryQE"; }
457 /** general queries about equality */
458 bool hasTerm( Node a
);
459 Node
getRepresentative( Node a
);
460 bool areEqual( Node a
, Node b
);
461 bool areDisequal( Node a
, Node b
);
462 eq::EqualityEngine
* getEngine();
463 void getEquivalenceClass( Node a
, std::vector
< Node
>& eqc
);
464 TNode
getCongruentTerm( Node f
, std::vector
< TNode
>& args
);
465 /** getInternalRepresentative gets the current best representative in the equivalence class of a, based on some criteria.
466 If cbqi is active, this will return a term in the equivalence class of "a" that does
467 not contain instantiation constants, if such a term exists.
469 Node
getInternalRepresentative( Node a
, Node f
, int index
);
470 /** get quantifiers equality inference */
471 quantifiers::EqualityInference
* getEqualityInference() { return d_eq_inference
; }
472 }; /* EqualityQueryQuantifiersEngine */
474 }/* CVC4::theory namespace */
475 }/* CVC4 namespace */
477 #endif /* __CVC4__THEORY__QUANTIFIERS_ENGINE_H */