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