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