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