1 /********************* */
2 /*! \file quantifiers_engine.h
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Morgan Deters, Haniel Barbosa
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 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
12 ** \brief Theory instantiator, Instantiation Engine classes
15 #include "cvc4_private.h"
17 #ifndef CVC4__THEORY__QUANTIFIERS_ENGINE_H
18 #define CVC4__THEORY__QUANTIFIERS_ENGINE_H
21 #include <unordered_map>
23 #include "context/cdhashset.h"
24 #include "context/cdlist.h"
25 #include "expr/attribute.h"
26 #include "expr/term_canonize.h"
27 #include "theory/quantifiers/ematching/trigger.h"
28 #include "theory/quantifiers/equality_query.h"
29 #include "theory/quantifiers/first_order_model.h"
30 #include "theory/quantifiers/fmf/model_builder.h"
31 #include "theory/quantifiers/instantiate.h"
32 #include "theory/quantifiers/quant_epr.h"
33 #include "theory/quantifiers/quant_util.h"
34 #include "theory/quantifiers/quantifiers_attributes.h"
35 #include "theory/quantifiers/skolemize.h"
36 #include "theory/quantifiers/sygus/term_database_sygus.h"
37 #include "theory/quantifiers/term_database.h"
38 #include "theory/quantifiers/term_enumeration.h"
39 #include "theory/quantifiers/term_util.h"
40 #include "util/statistics_registry.h"
48 class QuantifiersEnginePrivate
;
50 // TODO: organize this more/review this, github issue #1163
51 class QuantifiersEngine
{
52 typedef context::CDHashMap
< Node
, bool, NodeHashFunction
> BoolMap
;
53 typedef context::CDList
<Node
> NodeList
;
54 typedef context::CDList
<bool> BoolList
;
55 typedef context::CDHashSet
<Node
, NodeHashFunction
> NodeSet
;
58 QuantifiersEngine(context::Context
* c
, context::UserContext
* u
, TheoryEngine
* te
);
60 //---------------------- external interface
61 /** get theory engine */
62 TheoryEngine
* getTheoryEngine() const { return d_te
; }
63 /** get default sat context for quantifiers engine */
64 context::Context
* getSatContext();
65 /** get default sat context for quantifiers engine */
66 context::UserContext
* getUserContext();
67 /** get default output channel for the quantifiers engine */
68 OutputChannel
& getOutputChannel();
69 /** get default valuation for the quantifiers engine */
70 Valuation
& getValuation();
71 /** get the logic info for the quantifiers engine */
72 const LogicInfo
& getLogicInfo() const;
73 //---------------------- end external interface
74 //---------------------- utilities
75 /** get the master equality engine */
76 eq::EqualityEngine
* getMasterEqualityEngine() const;
77 /** get equality query */
78 EqualityQuery
* getEqualityQuery() const;
79 /** get the model builder */
80 quantifiers::QModelBuilder
* getModelBuilder() const;
81 /** get utility for EPR */
82 quantifiers::QuantEPR
* getQuantEPR() const;
84 quantifiers::FirstOrderModel
* getModel() const;
85 /** get term database */
86 quantifiers::TermDb
* getTermDatabase() const;
87 /** get term database sygus */
88 quantifiers::TermDbSygus
* getTermDatabaseSygus() const;
89 /** get term utilities */
90 quantifiers::TermUtil
* getTermUtil() const;
91 /** get term canonizer */
92 expr::TermCanonize
* getTermCanonize() const;
93 /** get quantifiers attributes */
94 quantifiers::QuantAttributes
* getQuantAttributes() const;
95 /** get instantiate utility */
96 quantifiers::Instantiate
* getInstantiate() const;
97 /** get skolemize utility */
98 quantifiers::Skolemize
* getSkolemize() const;
99 /** get term enumeration utility */
100 quantifiers::TermEnumeration
* getTermEnumeration() const;
101 /** get trigger database */
102 inst::TriggerTrie
* getTriggerDatabase() const;
103 //---------------------- end utilities
106 * Maps quantified formulas to the module that owns them, if any module has
107 * specifically taken ownership of it.
109 std::map
< Node
, QuantifiersModule
* > d_owner
;
111 * The priority value associated with the ownership of quantified formulas
112 * in the domain of the above map, where higher values take higher
115 std::map
< Node
, int > d_owner_priority
;
118 QuantifiersModule
* getOwner( Node q
);
120 * Set owner of quantified formula q to module m with given priority. If
121 * the quantified formula has previously been assigned an owner with
122 * lower priority, that owner is overwritten.
124 void setOwner( Node q
, QuantifiersModule
* m
, int priority
= 0 );
125 /** set owner of quantified formula q based on its attributes qa. */
126 void setOwner(Node q
, quantifiers::QAttributes
& qa
);
128 bool hasOwnership( Node q
, QuantifiersModule
* m
= NULL
);
129 /** does variable v of quantified formula q have a finite bound? */
130 bool isFiniteBound(Node q
, Node v
) const;
131 /** get bound var type
133 * This returns the type of bound that was inferred for variable v of
134 * quantified formula q.
136 BoundVarType
getBoundVarType(Node q
, Node v
) const;
138 * Get the indices of bound variables, in the order they should be processed
139 * in a RepSetIterator.
141 * For details, see BoundedIntegers::getBoundVarIndices.
143 void getBoundVarIndices(Node q
, std::vector
<unsigned>& indices
) const;
147 * This gets the (finite) enumeration of the range of variable v of quantified
148 * formula q and adds it into the vector elements in the context of the
149 * iteration being performed by rsi. It returns true if it could successfully
150 * determine this range.
152 * For details, see BoundedIntegers::getBoundElements.
154 bool getBoundElements(RepSetIterator
* rsi
,
158 std::vector
<Node
>& elements
) const;
163 /** notify preprocessed assertion */
164 void ppNotifyAssertions(const std::vector
<Node
>& assertions
);
165 /** check at level */
166 void check( Theory::Effort e
);
167 /** notify that theories were combined */
168 void notifyCombineTheories();
169 /** preRegister quantifier
171 * This function is called after registerQuantifier for quantified formulas
172 * that are pre-registered to the quantifiers theory.
174 void preRegisterQuantifier(Node q
);
175 /** register quantifier */
176 void registerPattern( std::vector
<Node
> & pattern
);
177 /** assert universal quantifier */
178 void assertQuantifier( Node q
, bool pol
);
180 /** (context-indepentent) register quantifier internal
182 * This is called when a quantified formula q is pre-registered to the
183 * quantifiers theory, and updates the modules in this class with
184 * context-independent information about how to handle q. This includes basic
185 * information such as which module owns q.
187 void registerQuantifierInternal(Node q
);
188 /** reduceQuantifier, return true if reduced */
189 bool reduceQuantifier(Node q
);
195 bool addLemma( Node lem
, bool doCache
= true, bool doRewrite
= true );
196 /** remove pending lemma */
197 bool removeLemma( Node lem
);
198 /** add require phase */
199 void addRequirePhase( Node lit
, bool req
);
200 /** mark relevant quantified formula, this will indicate it should be checked before the others */
201 void markRelevant( Node q
);
202 /** has added lemma */
203 bool hasAddedLemma() const;
204 /** theory engine needs check
206 * This is true if the theory engine has more constraints to process. When
207 * it is false, we are tentatively going to terminate solving with
208 * sat/unknown. For details, see TheoryEngine::needCheck.
210 bool theoryEngineNeedsCheck() const;
211 /** is in conflict */
212 bool inConflict() { return d_conflict
; }
215 /** get current q effort */
216 QuantifiersModule::QEffort
getCurrentQEffort() { return d_curr_effort_level
; }
217 /** get number of waiting lemmas */
218 unsigned getNumLemmasWaiting() { return d_lemmas_waiting
.size(); }
219 /** get needs check */
220 bool getInstWhenNeedsCheck( Theory::Effort e
);
221 /** get user pat mode */
222 options::UserPatMode
getInstUserPatMode();
225 /** add term to database */
226 void addTermToDatabase( Node n
, bool withinQuant
= false, bool withinInstClosure
= false );
227 /** notification when master equality engine is updated */
228 void eqNotifyNewClass(TNode t
);
229 /** debug print equality engine */
230 void debugPrintEqualityEngine( const char * c
);
231 /** get internal representative
233 * Choose a term that is equivalent to a in the current context that is the
234 * best term for instantiating the index^th variable of quantified formula q.
235 * If no legal term can be found, we return null. This can occur if:
236 * - a's type is not a subtype of the type of the index^th variable of q,
237 * - a is in an equivalent class with all terms that are restricted not to
238 * appear in instantiations of q, e.g. INST_CONSTANT terms for counterexample
239 * guided instantiation.
241 Node
getInternalRepresentative( Node a
, Node q
, int index
);
244 //----------user interface for instantiations (see quantifiers/instantiate.h)
245 /** print instantiations */
246 void printInstantiations(std::ostream
& out
);
247 /** print solution for synthesis conjectures */
248 void printSynthSolution(std::ostream
& out
);
249 /** get list of quantified formulas that were instantiated */
250 void getInstantiatedQuantifiedFormulas(std::vector
<Node
>& qs
);
251 /** get instantiations */
252 void getInstantiations(Node q
, std::vector
<Node
>& insts
);
253 void getInstantiations(std::map
<Node
, std::vector
<Node
> >& insts
);
254 /** get instantiation term vectors */
255 void getInstantiationTermVectors(Node q
,
256 std::vector
<std::vector
<Node
> >& tvecs
);
257 void getInstantiationTermVectors(
258 std::map
<Node
, std::vector
<std::vector
<Node
> > >& insts
);
259 /** get instantiated conjunction */
260 Node
getInstantiatedConjunction(Node q
);
261 /** get unsat core lemmas */
262 bool getUnsatCoreLemmas(std::vector
<Node
>& active_lemmas
);
263 bool getUnsatCoreLemmas(std::vector
<Node
>& active_lemmas
,
264 std::map
<Node
, Node
>& weak_imp
);
265 /** get explanation for instantiation lemmas */
266 void getExplanationForInstLemmas(const std::vector
<Node
>& lems
,
267 std::map
<Node
, Node
>& quant
,
268 std::map
<Node
, std::vector
<Node
> >& tvec
);
270 /** get synth solutions
272 * This method returns true if there is a synthesis solution available. This
273 * is the case if the last call to check satisfiability originated in a
274 * check-synth call, and the synthesis engine module of this class
275 * successfully found a solution for all active synthesis conjectures.
277 * This method adds entries to sol_map that map functions-to-synthesize with
278 * their solutions, for all active conjectures. This should be called
279 * immediately after the solver answers unsat for sygus input.
281 * For details on what is added to sol_map, see
282 * SynthConjecture::getSynthSolutions.
284 bool getSynthSolutions(std::map
<Node
, std::map
<Node
, Node
> >& sol_map
);
286 //----------end user interface for instantiations
288 /** statistics class */
292 TimerStat d_qcf_time
;
293 TimerStat d_ematching_time
;
295 IntStat d_instantiation_rounds
;
296 IntStat d_instantiation_rounds_lc
;
298 IntStat d_simple_triggers
;
299 IntStat d_multi_triggers
;
300 IntStat d_multi_trigger_instantiations
;
301 IntStat d_red_alpha_equiv
;
302 IntStat d_instantiations_user_patterns
;
303 IntStat d_instantiations_auto_gen
;
304 IntStat d_instantiations_guess
;
305 IntStat d_instantiations_qcf
;
306 IntStat d_instantiations_qcf_prop
;
307 IntStat d_instantiations_fmf_exh
;
308 IntStat d_instantiations_fmf_mbqi
;
309 IntStat d_instantiations_cbqi
;
310 IntStat d_instantiations_rr
;
313 };/* class QuantifiersEngine::Statistics */
314 Statistics d_statistics
;
317 /** reference to theory engine object */
319 /** vector of utilities for quantifiers */
320 std::vector
<QuantifiersUtil
*> d_util
;
321 /** vector of modules for quantifiers */
322 std::vector
<QuantifiersModule
*> d_modules
;
323 //------------- quantifiers utilities
324 /** equality query class */
325 std::unique_ptr
<quantifiers::EqualityQueryQuantifiersEngine
> d_eq_query
;
326 /** all triggers will be stored in this trie */
327 std::unique_ptr
<inst::TriggerTrie
> d_tr_trie
;
328 /** extended model object */
329 std::unique_ptr
<quantifiers::FirstOrderModel
> d_model
;
331 std::unique_ptr
<quantifiers::QModelBuilder
> d_builder
;
332 /** utility for effectively propositional logic */
333 std::unique_ptr
<quantifiers::QuantEPR
> d_qepr
;
334 /** term utilities */
335 std::unique_ptr
<quantifiers::TermUtil
> d_term_util
;
336 /** term utilities */
337 std::unique_ptr
<expr::TermCanonize
> d_term_canon
;
339 std::unique_ptr
<quantifiers::TermDb
> d_term_db
;
340 /** sygus term database */
341 std::unique_ptr
<quantifiers::TermDbSygus
> d_sygus_tdb
;
342 /** quantifiers attributes */
343 std::unique_ptr
<quantifiers::QuantAttributes
> d_quant_attr
;
344 /** instantiate utility */
345 std::unique_ptr
<quantifiers::Instantiate
> d_instantiate
;
346 /** skolemize utility */
347 std::unique_ptr
<quantifiers::Skolemize
> d_skolemize
;
348 /** term enumeration utility */
349 std::unique_ptr
<quantifiers::TermEnumeration
> d_term_enum
;
350 //------------- end quantifiers utilities
352 * The private utility, which contains all of the quantifiers modules.
354 std::unique_ptr
<QuantifiersEnginePrivate
> d_private
;
355 //------------- temporary information during check
356 /** current effort level */
357 QuantifiersModule::QEffort d_curr_effort_level
;
358 /** are we in conflict */
360 context::CDO
<bool> d_conflict_c
;
361 /** has added lemma this round */
362 bool d_hasAddedLemma
;
363 //------------- end temporary information during check
365 /** list of all quantifiers seen */
366 std::map
<Node
, bool> d_quants
;
367 /** quantifiers pre-registered */
368 NodeSet d_quants_prereg
;
369 /** quantifiers reduced */
370 BoolMap d_quants_red
;
371 std::map
<Node
, Node
> d_quants_red_lem
;
372 /** list of all lemmas produced */
373 // std::map< Node, bool > d_lemmas_produced;
374 BoolMap d_lemmas_produced_c
;
375 /** lemmas waiting */
376 std::vector
<Node
> d_lemmas_waiting
;
377 /** phase requirements waiting */
378 std::map
<Node
, bool> d_phase_req_waiting
;
379 /** inst round counters TODO: make context-dependent? */
380 context::CDO
<int> d_ierCounter_c
;
383 int d_ierCounterLastLc
;
384 int d_inst_when_phase
;
385 /** has presolve been called */
386 context::CDO
<bool> d_presolve
;
387 /** presolve cache */
388 NodeSet d_presolve_in
;
389 NodeList d_presolve_cache
;
390 BoolList d_presolve_cache_wq
;
391 BoolList d_presolve_cache_wic
;
392 };/* class QuantifiersEngine */
394 }/* CVC4::theory namespace */
395 }/* CVC4 namespace */
397 #endif /* CVC4__THEORY__QUANTIFIERS_ENGINE_H */