a088dfec64e124f57f0e340c4b607c53f197cd57
[cvc5.git] / src / theory / quantifiers_engine.h
1 /********************* */
2 /*! \file quantifiers_engine.h
3 ** \verbatim
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
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 <ext/hash_set>
21 #include <iostream>
22 #include <map>
23
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"
33
34 namespace CVC4 {
35
36 class TheoryEngine;
37
38 namespace theory {
39
40 class QuantifiersEngine;
41
42 namespace quantifiers {
43 class TermDb;
44 class TermDbSygus;
45 }
46
47 class InstantiationNotify {
48 public:
49 InstantiationNotify(){}
50 virtual bool notifyInstantiation( unsigned quant_e, Node q, Node lem, std::vector< Node >& terms, Node body ) = 0;
51 };
52
53 namespace quantifiers {
54 class FirstOrderModel;
55 //modules
56 class InstantiationEngine;
57 class ModelEngine;
58 class BoundedIntegers;
59 class QuantConflictFind;
60 class RewriteEngine;
61 class RelevantDomain;
62 class QModelBuilder;
63 class ConjectureGenerator;
64 class CegInstantiation;
65 class LtePartialInst;
66 class AlphaEquivalence;
67 class FunDefEngine;
68 class QuantEqualityEngine;
69 class FullSaturation;
70 class InstStrategyCbqi;
71 class InstStrategyCegqi;
72 class QuantDSplit;
73 class QuantAntiSkolem;
74 class EqualityInference;
75 class InstPropagator;
76 }/* CVC4::theory::quantifiers */
77
78 namespace inst {
79 class TriggerTrie;
80 }/* CVC4::theory::inst */
81
82 //class EfficientEMatcher;
83 class EqualityQueryQuantifiersEngine;
84
85 class QuantifiersEngine {
86 friend class quantifiers::InstantiationEngine;
87 friend class quantifiers::InstStrategyCegqi;
88 friend class quantifiers::ModelEngine;
89 friend class quantifiers::RewriteEngine;
90 friend class quantifiers::QuantConflictFind;
91 friend class inst::InstMatch;
92 typedef context::CDHashMap< Node, bool, NodeHashFunction > BoolMap;
93 typedef context::CDChunkList<Node> NodeList;
94 typedef context::CDChunkList<bool> BoolList;
95 typedef context::CDHashSet<Node, NodeHashFunction> NodeSet;
96 private:
97 /** reference to theory engine object */
98 TheoryEngine* d_te;
99 /** vector of utilities for quantifiers */
100 std::vector< QuantifiersUtil* > d_util;
101 /** vector of modules for quantifiers */
102 std::vector< QuantifiersModule* > d_modules;
103 /** instantiation notify */
104 std::vector< InstantiationNotify* > d_inst_notify;
105 /** equality query class */
106 EqualityQueryQuantifiersEngine* d_eq_query;
107 /** for computing relevance of quantifiers */
108 QuantRelevance * d_quant_rel;
109 /** relevant domain */
110 quantifiers::RelevantDomain* d_rel_dom;
111 /** alpha equivalence */
112 quantifiers::AlphaEquivalence * d_alpha_equiv;
113 /** model builder */
114 quantifiers::QModelBuilder* d_builder;
115 /** instantiation engine */
116 quantifiers::InstantiationEngine* d_inst_engine;
117 /** model engine */
118 quantifiers::ModelEngine* d_model_engine;
119 /** bounded integers utility */
120 quantifiers::BoundedIntegers * d_bint;
121 /** Conflict find mechanism for quantifiers */
122 quantifiers::QuantConflictFind* d_qcf;
123 /** rewrite rules utility */
124 quantifiers::RewriteEngine * d_rr_engine;
125 /** subgoal generator */
126 quantifiers::ConjectureGenerator * d_sg_gen;
127 /** ceg instantiation */
128 quantifiers::CegInstantiation * d_ceg_inst;
129 /** lte partial instantiation */
130 quantifiers::LtePartialInst * d_lte_part_inst;
131 /** function definitions engine */
132 quantifiers::FunDefEngine * d_fun_def_engine;
133 /** quantifiers equality engine */
134 quantifiers::QuantEqualityEngine * d_uee;
135 /** full saturation */
136 quantifiers::FullSaturation * d_fs;
137 /** counterexample-based quantifier instantiation */
138 quantifiers::InstStrategyCbqi * d_i_cbqi;
139 /** quantifiers splitting */
140 quantifiers::QuantDSplit * d_qsplit;
141 /** quantifiers anti-skolemization */
142 quantifiers::QuantAntiSkolem * d_anti_skolem;
143 /** quantifiers instantiation propagtor */
144 quantifiers::InstPropagator * d_inst_prop;
145 public: //effort levels
146 enum {
147 QEFFORT_CONFLICT,
148 QEFFORT_STANDARD,
149 QEFFORT_MODEL,
150 QEFFORT_LAST_CALL,
151 //none
152 QEFFORT_NONE,
153 };
154 private: //this information is reset during check
155 /** current effort level */
156 unsigned d_curr_effort_level;
157 /** are we in conflict */
158 bool d_conflict;
159 /** number of lemmas we actually added this round (for debugging) */
160 unsigned d_num_added_lemmas_round;
161 /** has added lemma this round */
162 bool d_hasAddedLemma;
163 private:
164 /** list of all quantifiers seen */
165 std::map< Node, bool > d_quants;
166 /** quantifiers reduced */
167 std::map< Node, bool > d_quants_red;
168 /** list of all lemmas produced */
169 //std::map< Node, bool > d_lemmas_produced;
170 BoolMap d_lemmas_produced_c;
171 /** lemmas waiting */
172 std::vector< Node > d_lemmas_waiting;
173 /** phase requirements waiting */
174 std::map< Node, bool > d_phase_req_waiting;
175 /** list of all instantiations produced for each quantifier */
176 std::map< Node, inst::InstMatchTrie > d_inst_match_trie;
177 std::map< Node, inst::CDInstMatchTrie* > d_c_inst_match_trie;
178 /** quantifiers that have been skolemized */
179 BoolMap d_skolemized;
180 /** term database */
181 quantifiers::TermDb* d_term_db;
182 /** all triggers will be stored in this trie */
183 inst::TriggerTrie* d_tr_trie;
184 /** extended model object */
185 quantifiers::FirstOrderModel* d_model;
186 /** statistics for debugging */
187 std::map< Node, int > d_total_inst_debug;
188 std::map< Node, int > d_temp_inst_debug;
189 int d_total_inst_count_debug;
190 /** inst round counters TODO: make context-dependent? */
191 context::CDO< int > d_ierCounter_c;
192 int d_ierCounter;
193 int d_ierCounter_lc;
194 int d_ierCounterLastLc;
195 int d_inst_when_phase;
196 /** has presolve been called */
197 context::CDO< bool > d_presolve;
198 /** presolve cache */
199 NodeSet d_presolve_in;
200 NodeList d_presolve_cache;
201 BoolList d_presolve_cache_wq;
202 BoolList d_presolve_cache_wic;
203
204 public:
205 QuantifiersEngine(context::Context* c, context::UserContext* u, TheoryEngine* te);
206 ~QuantifiersEngine();
207 /** get theory engine */
208 TheoryEngine* getTheoryEngine() { return d_te; }
209 /** get equality query */
210 EqualityQueryQuantifiersEngine* getEqualityQuery();
211 /** get default sat context for quantifiers engine */
212 context::Context* getSatContext();
213 /** get default sat context for quantifiers engine */
214 context::UserContext* getUserContext();
215 /** get default output channel for the quantifiers engine */
216 OutputChannel& getOutputChannel();
217 /** get default valuation for the quantifiers engine */
218 Valuation& getValuation();
219 /** get relevant domain */
220 quantifiers::RelevantDomain* getRelevantDomain() { return d_rel_dom; }
221 /** get quantifier relevance */
222 QuantRelevance* getQuantifierRelevance() { return d_quant_rel; }
223 /** get the model builder */
224 quantifiers::QModelBuilder* getModelBuilder() { return d_builder; }
225 public: //modules
226 /** get instantiation engine */
227 quantifiers::InstantiationEngine* getInstantiationEngine() { return d_inst_engine; }
228 /** get model engine */
229 quantifiers::ModelEngine* getModelEngine() { return d_model_engine; }
230 /** get bounded integers utility */
231 quantifiers::BoundedIntegers * getBoundedIntegers() { return d_bint; }
232 /** Conflict find mechanism for quantifiers */
233 quantifiers::QuantConflictFind* getConflictFind() { return d_qcf; }
234 /** rewrite rules utility */
235 quantifiers::RewriteEngine * getRewriteEngine() { return d_rr_engine; }
236 /** subgoal generator */
237 quantifiers::ConjectureGenerator * getConjectureGenerator() { return d_sg_gen; }
238 /** ceg instantiation */
239 quantifiers::CegInstantiation * getCegInstantiation() { return d_ceg_inst; }
240 /** local theory ext partial inst */
241 quantifiers::LtePartialInst * getLtePartialInst() { return d_lte_part_inst; }
242 /** function definition engine */
243 quantifiers::FunDefEngine * getFunDefEngine() { return d_fun_def_engine; }
244 /** quantifiers equality engine */
245 quantifiers::QuantEqualityEngine * getQuantEqualityEngine() { return d_uee; }
246 /** get full saturation */
247 quantifiers::FullSaturation * getFullSaturation() { return d_fs; }
248 /** get inst strategy cbqi */
249 quantifiers::InstStrategyCbqi * getInstStrategyCbqi() { return d_i_cbqi; }
250 /** get quantifiers splitting */
251 quantifiers::QuantDSplit * getQuantDSplit() { return d_qsplit; }
252 /** get quantifiers anti-skolemization */
253 quantifiers::QuantAntiSkolem * getQuantAntiSkolem() { return d_anti_skolem; }
254 private:
255 /** owner of quantified formulas */
256 std::map< Node, QuantifiersModule * > d_owner;
257 std::map< Node, int > d_owner_priority;
258 public:
259 /** get owner */
260 QuantifiersModule * getOwner( Node q );
261 /** set owner */
262 void setOwner( Node q, QuantifiersModule * m, int priority = 0 );
263 /** considers */
264 bool hasOwnership( Node q, QuantifiersModule * m = NULL );
265 public:
266 /** initialize */
267 void finishInit();
268 /** presolve */
269 void presolve();
270 /** check at level */
271 void check( Theory::Effort e );
272 /** notify that theories were combined */
273 void notifyCombineTheories();
274 /** register quantifier */
275 bool registerQuantifier( Node f );
276 /** register quantifier */
277 void registerPattern( std::vector<Node> & pattern);
278 /** assert universal quantifier */
279 void assertQuantifier( Node q, bool pol );
280 /** propagate */
281 void propagate( Theory::Effort level );
282 /** get next decision request */
283 Node getNextDecisionRequest();
284 private:
285 /** reduceQuantifier, return true if reduced */
286 bool reduceQuantifier( Node q );
287 /** compute term vector */
288 void computeTermVector( Node f, InstMatch& m, std::vector< Node >& vars, std::vector< Node >& terms );
289 /** record instantiation, return true if it was non-duplicate */
290 bool recordInstantiationInternal( Node q, std::vector< Node >& terms, bool modEq = false, bool modInst = false, bool addedLem = true );
291 /** remove instantiation */
292 bool removeInstantiationInternal( Node q, std::vector< Node >& terms );
293 /** set instantiation level attr */
294 static void setInstantiationLevelAttr( Node n, Node qn, uint64_t level );
295 /** flush lemmas */
296 void flushLemmas();
297 public:
298 /** get instantiation */
299 Node getInstantiation( Node q, std::vector< Node >& vars, std::vector< Node >& terms, bool doVts = false );
300 /** get instantiation */
301 Node getInstantiation( Node q, InstMatch& m, bool doVts = false );
302 /** get instantiation */
303 Node getInstantiation( Node q, std::vector< Node >& terms, bool doVts = false );
304 /** do substitution */
305 Node getSubstitute( Node n, std::vector< Node >& terms );
306 /** add lemma lem */
307 bool addLemma( Node lem, bool doCache = true, bool doRewrite = true );
308 /** remove pending lemma */
309 bool removeLemma( Node lem );
310 /** add require phase */
311 void addRequirePhase( Node lit, bool req );
312 /** do instantiation specified by m */
313 bool addInstantiation( Node q, InstMatch& m, bool mkRep = true, bool modEq = false, bool modInst = false, bool doVts = false );
314 /** add instantiation */
315 bool addInstantiation( Node q, std::vector< Node >& terms, bool mkRep = true, bool modEq = false, bool modInst = false, bool doVts = false );
316 /** remove pending instantiation */
317 bool removeInstantiation( Node q, Node lem, std::vector< Node >& terms );
318 /** split on node n */
319 bool addSplit( Node n, bool reqPhase = false, bool reqPhasePol = true );
320 /** add split equality */
321 bool addSplitEquality( Node n1, Node n2, bool reqPhase = false, bool reqPhasePol = true );
322 /** mark relevant quantified formula, this will indicate it should be checked before the others */
323 void markRelevant( Node q );
324 /** has added lemma */
325 bool hasAddedLemma() { return !d_lemmas_waiting.empty() || d_hasAddedLemma; }
326 /** is in conflict */
327 bool inConflict() { return d_conflict; }
328 /** get number of waiting lemmas */
329 unsigned getNumLemmasWaiting() { return d_lemmas_waiting.size(); }
330 /** get number of waiting lemmas */
331 unsigned getNumLemmasAddedThisRound() { return d_num_added_lemmas_round; }
332 /** get needs check */
333 bool getInstWhenNeedsCheck( Theory::Effort e );
334 /** get user pat mode */
335 quantifiers::UserPatMode getInstUserPatMode();
336 /** set instantiation level attr */
337 static void setInstantiationLevelAttr( Node n, uint64_t level );
338 public:
339 /** get model */
340 quantifiers::FirstOrderModel* getModel() { return d_model; }
341 /** get term database */
342 quantifiers::TermDb* getTermDatabase() { return d_term_db; }
343 /** get term database sygus */
344 quantifiers::TermDbSygus* getTermDatabaseSygus();
345 /** get trigger database */
346 inst::TriggerTrie* getTriggerDatabase() { return d_tr_trie; }
347 /** add term to database */
348 void addTermToDatabase( Node n, bool withinQuant = false, bool withinInstClosure = false );
349 /** notification when master equality engine is updated */
350 void eqNotifyNewClass(TNode t);
351 void eqNotifyPreMerge(TNode t1, TNode t2);
352 void eqNotifyPostMerge(TNode t1, TNode t2);
353 void eqNotifyDisequal(TNode t1, TNode t2, TNode reason);
354 /** get the master equality engine */
355 eq::EqualityEngine* getMasterEqualityEngine() ;
356 /** debug print equality engine */
357 void debugPrintEqualityEngine( const char * c );
358 public:
359 /** print instantiations */
360 void printInstantiations( std::ostream& out );
361 /** print solution for synthesis conjectures */
362 void printSynthSolution( std::ostream& out );
363 /** get instantiations */
364 void getInstantiations( std::map< Node, std::vector< Node > >& insts );
365 /** statistics class */
366 class Statistics {
367 public:
368 TimerStat d_time;
369 IntStat d_num_quant;
370 IntStat d_instantiation_rounds;
371 IntStat d_instantiation_rounds_lc;
372 IntStat d_instantiations;
373 IntStat d_inst_duplicate;
374 IntStat d_inst_duplicate_eq;
375 IntStat d_triggers;
376 IntStat d_simple_triggers;
377 IntStat d_multi_triggers;
378 IntStat d_multi_trigger_instantiations;
379 IntStat d_red_alpha_equiv;
380 IntStat d_red_lte_partial_inst;
381 IntStat d_instantiations_user_patterns;
382 IntStat d_instantiations_auto_gen;
383 IntStat d_instantiations_guess;
384 IntStat d_instantiations_cbqi_arith;
385 Statistics();
386 ~Statistics();
387 };/* class QuantifiersEngine::Statistics */
388 Statistics d_statistics;
389 };/* class QuantifiersEngine */
390
391
392
393 /** equality query object using theory engine */
394 class EqualityQueryQuantifiersEngine : public EqualityQuery
395 {
396 private:
397 /** pointer to theory engine */
398 QuantifiersEngine* d_qe;
399 /** quantifiers equality inference */
400 quantifiers::EqualityInference * d_eq_inference;
401 context::CDO< unsigned > d_eqi_counter;
402 /** internal representatives */
403 std::map< TypeNode, std::map< Node, Node > > d_int_rep;
404 /** rep score */
405 std::map< Node, int > d_rep_score;
406 /** reset count */
407 int d_reset_count;
408
409 /** processInferences : will merge equivalence classes in master equality engine, if possible */
410 bool processInferences( Theory::Effort e );
411 /** node contains */
412 Node getInstance( Node n, const std::vector< Node >& eqc, std::hash_map<TNode, Node, TNodeHashFunction>& cache );
413 /** get score */
414 int getRepScore( Node n, Node f, int index, TypeNode v_tn );
415 /** flatten representatives */
416 void flattenRepresentatives( std::map< TypeNode, std::vector< Node > >& reps );
417 public:
418 EqualityQueryQuantifiersEngine( context::Context* c, QuantifiersEngine* qe );
419 virtual ~EqualityQueryQuantifiersEngine();
420 /** reset */
421 bool reset( Theory::Effort e );
422 /** identify */
423 std::string identify() const { return "EqualityQueryQE"; }
424 /** general queries about equality */
425 bool hasTerm( Node a );
426 Node getRepresentative( Node a );
427 bool areEqual( Node a, Node b );
428 bool areDisequal( Node a, Node b );
429 eq::EqualityEngine* getEngine();
430 void getEquivalenceClass( Node a, std::vector< Node >& eqc );
431 TNode getCongruentTerm( Node f, std::vector< TNode >& args );
432 /** getInternalRepresentative gets the current best representative in the equivalence class of a, based on some criteria.
433 If cbqi is active, this will return a term in the equivalence class of "a" that does
434 not contain instantiation constants, if such a term exists.
435 */
436 Node getInternalRepresentative( Node a, Node f, int index );
437 /** get quantifiers equality inference */
438 quantifiers::EqualityInference * getEqualityInference() { return d_eq_inference; }
439 }; /* EqualityQueryQuantifiersEngine */
440
441 }/* CVC4::theory namespace */
442 }/* CVC4 namespace */
443
444 #endif /* __CVC4__THEORY__QUANTIFIERS_ENGINE_H */