Fix cegqi assertions for quantified non-linear cases. (#1999)
[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/cdhashset.h"
26 #include "context/cdlist.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 namespace quantifiers {
44 //TODO: organize this more/review this, github issue #1163
45 //TODO: include these instead of doing forward declarations? #1307
46 //utilities
47 class TermDb;
48 class TermDbSygus;
49 class TermUtil;
50 class Instantiate;
51 class Skolemize;
52 class TermEnumeration;
53 class FirstOrderModel;
54 class QuantAttributes;
55 class QuantEPR;
56 class QuantRelevance;
57 class RelevantDomain;
58 class BvInverter;
59 class InstPropagator;
60 class EqualityInference;
61 class EqualityQueryQuantifiersEngine;
62 //modules, these are "subsolvers" of the quantifiers theory.
63 class InstantiationEngine;
64 class ModelEngine;
65 class BoundedIntegers;
66 class QuantConflictFind;
67 class RewriteEngine;
68 class QModelBuilder;
69 class ConjectureGenerator;
70 class CegInstantiation;
71 class LtePartialInst;
72 class AlphaEquivalence;
73 class InstStrategyEnum;
74 class InstStrategyCbqi;
75 class InstStrategyCegqi;
76 class QuantDSplit;
77 class QuantAntiSkolem;
78 class EqualityInference;
79 }/* CVC4::theory::quantifiers */
80
81 namespace inst {
82 class TriggerTrie;
83 }/* CVC4::theory::inst */
84
85
86 class QuantifiersEngine {
87 // TODO: remove these github issue #1163
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::CDList<Node> NodeList;
97 typedef context::CDList<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 /** equality query class */
107 quantifiers::EqualityQueryQuantifiersEngine* d_eq_query;
108 /** equality inference class */
109 quantifiers::EqualityInference* d_eq_inference;
110 /** for computing relevance of quantifiers */
111 quantifiers::QuantRelevance* d_quant_rel;
112 /** relevant domain */
113 quantifiers::RelevantDomain* d_rel_dom;
114 /** inversion utility for BV instantiation */
115 quantifiers::BvInverter * d_bv_invert;
116 /** alpha equivalence */
117 quantifiers::AlphaEquivalence * d_alpha_equiv;
118 /** model builder */
119 quantifiers::QModelBuilder* d_builder;
120 /** utility for effectively propositional logic */
121 quantifiers::QuantEPR* d_qepr;
122 /** term database */
123 quantifiers::TermDb* d_term_db;
124 /** sygus term database */
125 quantifiers::TermDbSygus* d_sygus_tdb;
126 /** term utilities */
127 quantifiers::TermUtil* d_term_util;
128 /** quantifiers attributes */
129 std::unique_ptr<quantifiers::QuantAttributes> d_quant_attr;
130 /** instantiate utility */
131 std::unique_ptr<quantifiers::Instantiate> d_instantiate;
132 /** skolemize utility */
133 std::unique_ptr<quantifiers::Skolemize> d_skolemize;
134 /** term enumeration utility */
135 std::unique_ptr<quantifiers::TermEnumeration> d_term_enum;
136 /** instantiation engine */
137 quantifiers::InstantiationEngine* d_inst_engine;
138 /** model engine */
139 quantifiers::ModelEngine* d_model_engine;
140 /** bounded integers utility */
141 quantifiers::BoundedIntegers * d_bint;
142 /** Conflict find mechanism for quantifiers */
143 quantifiers::QuantConflictFind* d_qcf;
144 /** rewrite rules utility */
145 quantifiers::RewriteEngine * d_rr_engine;
146 /** subgoal generator */
147 quantifiers::ConjectureGenerator * d_sg_gen;
148 /** ceg instantiation */
149 quantifiers::CegInstantiation * d_ceg_inst;
150 /** lte partial instantiation */
151 quantifiers::LtePartialInst * d_lte_part_inst;
152 /** full saturation */
153 quantifiers::InstStrategyEnum* d_fs;
154 /** counterexample-based quantifier instantiation */
155 quantifiers::InstStrategyCbqi * d_i_cbqi;
156 /** quantifiers splitting */
157 quantifiers::QuantDSplit * d_qsplit;
158 /** quantifiers anti-skolemization */
159 quantifiers::QuantAntiSkolem * d_anti_skolem;
160 /** quantifiers instantiation propagtor */
161 quantifiers::InstPropagator * d_inst_prop;
162
163 private: //this information is reset during check
164 /** current effort level */
165 QuantifiersModule::QEffort d_curr_effort_level;
166 /** are we in conflict */
167 bool d_conflict;
168 context::CDO<bool> d_conflict_c;
169 /** has added lemma this round */
170 bool d_hasAddedLemma;
171 /** whether to use model equality engine */
172 bool d_useModelEe;
173 private:
174 /** list of all quantifiers seen */
175 std::map< Node, bool > d_quants;
176 /** quantifiers reduced */
177 BoolMap d_quants_red;
178 std::map< Node, Node > d_quants_red_lem;
179 /** list of all lemmas produced */
180 //std::map< Node, bool > d_lemmas_produced;
181 BoolMap d_lemmas_produced_c;
182 /** lemmas waiting */
183 std::vector< Node > d_lemmas_waiting;
184 /** phase requirements waiting */
185 std::map< Node, bool > d_phase_req_waiting;
186 /** all triggers will be stored in this trie */
187 inst::TriggerTrie* d_tr_trie;
188 /** extended model object */
189 quantifiers::FirstOrderModel* d_model;
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 EqualityQuery* getEqualityQuery();
211 /** get the equality inference */
212 quantifiers::EqualityInference* getEqualityInference() { return d_eq_inference; }
213 /** get default sat context for quantifiers engine */
214 context::Context* getSatContext();
215 /** get default sat context for quantifiers engine */
216 context::UserContext* getUserContext();
217 /** get default output channel for the quantifiers engine */
218 OutputChannel& getOutputChannel();
219 /** get default valuation for the quantifiers engine */
220 Valuation& getValuation();
221 /** get the logic info for the quantifiers engine */
222 const LogicInfo& getLogicInfo() const;
223 /** get relevant domain */
224 quantifiers::RelevantDomain* getRelevantDomain() { return d_rel_dom; }
225 /** get the BV inverter utility */
226 quantifiers::BvInverter * getBvInverter() { return d_bv_invert; }
227 /** get quantifier relevance */
228 quantifiers::QuantRelevance* getQuantifierRelevance() { return d_quant_rel; }
229 /** get the model builder */
230 quantifiers::QModelBuilder* getModelBuilder() { return d_builder; }
231 /** get utility for EPR */
232 quantifiers::QuantEPR* getQuantEPR() { return d_qepr; }
233 public: // modules
234 /** get instantiation engine */
235 quantifiers::InstantiationEngine* getInstantiationEngine() { return d_inst_engine; }
236 /** get model engine */
237 quantifiers::ModelEngine* getModelEngine() { return d_model_engine; }
238 /** get bounded integers utility */
239 quantifiers::BoundedIntegers * getBoundedIntegers() { return d_bint; }
240 /** Conflict find mechanism for quantifiers */
241 quantifiers::QuantConflictFind* getConflictFind() { return d_qcf; }
242 /** rewrite rules utility */
243 quantifiers::RewriteEngine * getRewriteEngine() { return d_rr_engine; }
244 /** subgoal generator */
245 quantifiers::ConjectureGenerator * getConjectureGenerator() { return d_sg_gen; }
246 /** ceg instantiation */
247 quantifiers::CegInstantiation * getCegInstantiation() { return d_ceg_inst; }
248 /** local theory ext partial inst */
249 quantifiers::LtePartialInst * getLtePartialInst() { return d_lte_part_inst; }
250 /** get full saturation */
251 quantifiers::InstStrategyEnum* getInstStrategyEnum() { return d_fs; }
252 /** get inst strategy cbqi */
253 quantifiers::InstStrategyCbqi * getInstStrategyCbqi() { return d_i_cbqi; }
254 /** get quantifiers splitting */
255 quantifiers::QuantDSplit * getQuantDSplit() { return d_qsplit; }
256 /** get quantifiers anti-skolemization */
257 quantifiers::QuantAntiSkolem * getQuantAntiSkolem() { return d_anti_skolem; }
258 private:
259 /** owner of quantified formulas */
260 std::map< Node, QuantifiersModule * > d_owner;
261 std::map< Node, int > d_owner_priority;
262 public:
263 /** get owner */
264 QuantifiersModule * getOwner( Node q );
265 /** set owner */
266 void setOwner( Node q, QuantifiersModule * m, int priority = 0 );
267 /** considers */
268 bool hasOwnership( Node q, QuantifiersModule * m = NULL );
269 /** is finite bound */
270 bool isFiniteBound( Node q, Node v );
271 public:
272 /** presolve */
273 void presolve();
274 /** notify preprocessed assertion */
275 void ppNotifyAssertions(const std::vector<Node>& assertions);
276 /** check at level */
277 void check( Theory::Effort e );
278 /** notify that theories were combined */
279 void notifyCombineTheories();
280 /** register quantifier */
281 bool registerQuantifier( Node f );
282 /** register quantifier */
283 void registerPattern( std::vector<Node> & pattern);
284 /** assert universal quantifier */
285 void assertQuantifier( Node q, bool pol );
286 /** propagate */
287 void propagate( Theory::Effort level );
288 /** get next decision request */
289 Node getNextDecisionRequest( unsigned& priority );
290 private:
291 /** reduceQuantifier, return true if reduced */
292 bool reduceQuantifier( Node q );
293 /** flush lemmas */
294 void flushLemmas();
295 public:
296 /** add lemma lem */
297 bool addLemma( Node lem, bool doCache = true, bool doRewrite = true );
298 /** remove pending lemma */
299 bool removeLemma( Node lem );
300 /** add require phase */
301 void addRequirePhase( Node lit, bool req );
302 /** split on node n */
303 bool addSplit( Node n, bool reqPhase = false, bool reqPhasePol = true );
304 /** add split equality */
305 bool addSplitEquality( Node n1, Node n2, bool reqPhase = false, bool reqPhasePol = true );
306 /** add EPR axiom */
307 bool addEPRAxiom( TypeNode tn );
308 /** mark relevant quantified formula, this will indicate it should be checked before the others */
309 void markRelevant( Node q );
310 /** has added lemma */
311 bool hasAddedLemma() { return !d_lemmas_waiting.empty() || d_hasAddedLemma; }
312 /** is in conflict */
313 bool inConflict() { return d_conflict; }
314 /** set conflict */
315 void setConflict();
316 /** get current q effort */
317 QuantifiersModule::QEffort getCurrentQEffort() { return d_curr_effort_level; }
318 /** get number of waiting lemmas */
319 unsigned getNumLemmasWaiting() { return d_lemmas_waiting.size(); }
320 /** get needs check */
321 bool getInstWhenNeedsCheck( Theory::Effort e );
322 /** get user pat mode */
323 quantifiers::UserPatMode getInstUserPatMode();
324 public:
325 /** get model */
326 quantifiers::FirstOrderModel* getModel() { return d_model; }
327 /** get term database */
328 quantifiers::TermDb* getTermDatabase() { return d_term_db; }
329 /** get term database sygus */
330 quantifiers::TermDbSygus * getTermDatabaseSygus() { return d_sygus_tdb; }
331 /** get term utilities */
332 quantifiers::TermUtil* getTermUtil() { return d_term_util; }
333 /** get quantifiers attributes */
334 quantifiers::QuantAttributes* getQuantAttributes() {
335 return d_quant_attr.get();
336 }
337 /** get instantiate utility */
338 quantifiers::Instantiate* getInstantiate() { return d_instantiate.get(); }
339 /** get skolemize utility */
340 quantifiers::Skolemize* getSkolemize() { return d_skolemize.get(); }
341 /** get term enumeration utility */
342 quantifiers::TermEnumeration* getTermEnumeration()
343 {
344 return d_term_enum.get();
345 }
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 /** get the active equality engine */
358 eq::EqualityEngine* getActiveEqualityEngine();
359 /** use model equality engine */
360 bool usingModelEqualityEngine() const { return d_useModelEe; }
361 /** debug print equality engine */
362 void debugPrintEqualityEngine( const char * c );
363 /** get internal representative */
364 Node getInternalRepresentative( Node a, Node q, int index );
365
366 public:
367 //----------user interface for instantiations (see quantifiers/instantiate.h)
368 /** print instantiations */
369 void printInstantiations(std::ostream& out);
370 /** print solution for synthesis conjectures */
371 void printSynthSolution(std::ostream& out);
372 /** get list of quantified formulas that were instantiated */
373 void getInstantiatedQuantifiedFormulas(std::vector<Node>& qs);
374 /** get instantiations */
375 void getInstantiations(Node q, std::vector<Node>& insts);
376 void getInstantiations(std::map<Node, std::vector<Node> >& insts);
377 /** get instantiation term vectors */
378 void getInstantiationTermVectors(Node q,
379 std::vector<std::vector<Node> >& tvecs);
380 void getInstantiationTermVectors(
381 std::map<Node, std::vector<std::vector<Node> > >& insts);
382 /** get instantiated conjunction */
383 Node getInstantiatedConjunction(Node q);
384 /** get unsat core lemmas */
385 bool getUnsatCoreLemmas(std::vector<Node>& active_lemmas);
386 bool getUnsatCoreLemmas(std::vector<Node>& active_lemmas,
387 std::map<Node, Node>& weak_imp);
388 /** get explanation for instantiation lemmas */
389 void getExplanationForInstLemmas(const std::vector<Node>& lems,
390 std::map<Node, Node>& quant,
391 std::map<Node, std::vector<Node> >& tvec);
392
393 /** get synth solutions
394 *
395 * This function adds entries to sol_map that map functions-to-synthesize with
396 * their solutions, for all active conjectures. This should be called
397 * immediately after the solver answers unsat for sygus input.
398 *
399 * For details on what is added to sol_map, see
400 * CegConjecture::getSynthSolutions.
401 */
402 void getSynthSolutions(std::map<Node, Node>& sol_map);
403
404 //----------end user interface for instantiations
405
406 /** statistics class */
407 class Statistics {
408 public:
409 TimerStat d_time;
410 TimerStat d_qcf_time;
411 TimerStat d_ematching_time;
412 IntStat d_num_quant;
413 IntStat d_instantiation_rounds;
414 IntStat d_instantiation_rounds_lc;
415 IntStat d_triggers;
416 IntStat d_simple_triggers;
417 IntStat d_multi_triggers;
418 IntStat d_multi_trigger_instantiations;
419 IntStat d_red_alpha_equiv;
420 IntStat d_instantiations_user_patterns;
421 IntStat d_instantiations_auto_gen;
422 IntStat d_instantiations_guess;
423 IntStat d_instantiations_qcf;
424 IntStat d_instantiations_qcf_prop;
425 IntStat d_instantiations_fmf_exh;
426 IntStat d_instantiations_fmf_mbqi;
427 IntStat d_instantiations_cbqi;
428 IntStat d_instantiations_rr;
429 Statistics();
430 ~Statistics();
431 };/* class QuantifiersEngine::Statistics */
432 Statistics d_statistics;
433 };/* class QuantifiersEngine */
434
435 }/* CVC4::theory namespace */
436 }/* CVC4 namespace */
437
438 #endif /* __CVC4__THEORY__QUANTIFIERS_ENGINE_H */