1 /********************* */
2 /*! \file synth_conjecture.h
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Haniel Barbosa, Tim King
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 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 Class that encapsulates techniques for a single (SyGuS) synthesis
16 #include "cvc4_private.h"
18 #ifndef CVC4__THEORY__QUANTIFIERS__SYNTH_CONJECTURE_H
19 #define CVC4__THEORY__QUANTIFIERS__SYNTH_CONJECTURE_H
23 #include "theory/decision_manager.h"
24 #include "theory/quantifiers/expr_miner_manager.h"
25 #include "theory/quantifiers/sygus/ce_guided_single_inv.h"
26 #include "theory/quantifiers/sygus/cegis.h"
27 #include "theory/quantifiers/sygus/cegis_core_connective.h"
28 #include "theory/quantifiers/sygus/cegis_unif.h"
29 #include "theory/quantifiers/sygus/example_eval_cache.h"
30 #include "theory/quantifiers/sygus/example_infer.h"
31 #include "theory/quantifiers/sygus/sygus_grammar_cons.h"
32 #include "theory/quantifiers/sygus/sygus_pbe.h"
33 #include "theory/quantifiers/sygus/sygus_process_conj.h"
34 #include "theory/quantifiers/sygus/sygus_repair_const.h"
35 #include "theory/quantifiers/sygus/sygus_stats.h"
39 namespace quantifiers
{
42 class SygusStatistics
;
45 * A base class for generating values for actively-generated enumerators.
46 * At a high level, the job of this class is to accept a stream of "abstract
47 * values" a1, ..., an, ..., and generate a (possibly larger) stream of
48 * "concrete values" c11, ..., c1{m_1}, ..., cn1, ... cn{m_n}, ....
50 class EnumValGenerator
53 virtual ~EnumValGenerator() {}
54 /** initialize this class with enumerator e */
55 virtual void initialize(Node e
) = 0;
56 /** Inform this generator that abstract value v was enumerated. */
57 virtual void addValue(Node v
) = 0;
59 * Increment this value generator. If this returns false, then we are out of
60 * values. If this returns true, getCurrent(), if non-null, returns the
63 * Notice that increment() may return true and afterwards it may be the case
64 * getCurrent() is null. We do this so that increment() does not take too
65 * much time per call, which can be the case for grammars where it is
66 * difficult to find the next (non-redundant) term. Returning true with
67 * a null current term gives the caller the chance to interleave other
70 virtual bool increment() = 0;
71 /** Get the current concrete value generated by this class. */
72 virtual Node
getCurrent() = 0;
75 /** a synthesis conjecture
76 * This class implements approaches for a synthesis conjecture, given by data
78 * This includes both approaches for synthesis in Reynolds et al CAV 2015. It
79 * determines which approach and optimizations are applicable to the
80 * conjecture, and has interfaces for implementing them.
85 SynthConjecture(QuantifiersEngine
* qe
, SynthEngine
* p
, SygusStatistics
& s
);
89 /** get original version of conjecture */
90 Node
getConjecture() { return d_quant
; }
91 /** get deep embedding version of conjecture */
92 Node
getEmbeddedConjecture() { return d_embed_quant
; }
93 //-------------------------------for counterexample-guided check/refine
94 /** increment the number of times we have successfully done candidate
96 void incrementRefineCount() { d_refine_count
++; }
97 /** whether the conjecture is waiting for a call to doCheck below */
99 /** whether the conjecture is waiting for a call to doRefine below */
100 bool needsRefinement() const;
101 /** do syntax-guided enumerative check
103 * This is step 2(a) of Figure 3 of Reynolds et al CAV 2015.
105 * The method returns true if this conjecture is finished trying solutions
106 * for the given call to SynthEngine::check.
108 * Notice that we make multiple calls to doCheck on one call to
109 * SynthEngine::check. For example, if we are using an actively-generated
110 * enumerator, one enumerated (abstract) term may correspond to multiple
111 * concrete terms t1, ..., tn to check, where we make up to n calls to doCheck
112 * when each of t1, ..., tn fails to satisfy the current refinement lemmas.
114 bool doCheck(std::vector
<Node
>& lems
);
116 * This is step 2(b) of Figure 3 of Reynolds et al CAV 2015.
118 void doRefine(std::vector
<Node
>& lems
);
119 //-------------------------------end for counterexample-guided check/refine
121 * Prints the synthesis solution to output stream out. This invokes solution
122 * reconstruction if the conjecture is single invocation. Otherwise, it
123 * returns the solution found by sygus enumeration.
125 void printSynthSolution(std::ostream
& out
);
126 /** get synth solutions
128 * This method returns true if this class has a solution available to the
129 * conjecture that it was assigned.
131 * Let q be the synthesis conjecture assigned to this class.
132 * This method adds entries to sol_map[q] that map functions-to-synthesize to
133 * their builtin solution, which has the same type. For example, for synthesis
134 * conjecture exists f. forall x. f( x )>x, this function will update
135 * sol_map[q] to contain the entry:
136 * f -> (lambda x. x+1)
138 bool getSynthSolutions(std::map
<Node
, std::map
<Node
, Node
> >& sol_map
);
140 * The feasible guard whose semantics are "this conjecture is feasiable".
141 * This is "G" in Figure 3 of Reynolds et al CAV 2015.
143 Node
getGuard() const;
145 bool isGround() { return d_inner_vars
.empty(); }
146 /** are we using single invocation techniques */
147 bool isSingleInvocation() const;
148 /** preregister conjecture
149 * This is used as a heuristic for solution reconstruction, so that we
150 * remember expressions in the conjecture before preprocessing, since they
151 * may be helpful during solution reconstruction (Figure 5 of Reynolds et al
154 void preregisterConjecture(Node q
);
155 /** assign conjecture q to this class */
157 /** has a conjecture been assigned to this class */
158 bool isAssigned() { return !d_embed_quant
.isNull(); }
160 * Get model value for term n.
162 Node
getModelValue(Node n
);
164 /** get utility for static preprocessing and analysis of conjectures */
165 SynthConjectureProcess
* getProcess() { return d_ceg_proc
.get(); }
166 /** get constant repair utility */
167 SygusRepairConst
* getRepairConst() { return d_sygus_rconst
.get(); }
168 /** get example inference utility */
169 ExampleInfer
* getExampleInfer() { return d_exampleInfer
.get(); }
170 /** get the example evaluation cache utility for enumerator e */
171 ExampleEvalCache
* getExampleEvalCache(Node e
);
172 /** get program by examples module */
173 SygusPbe
* getPbe() { return d_ceg_pbe
.get(); }
174 /** get the symmetry breaking predicate for type */
175 Node
getSymmetryBreakingPredicate(
176 Node x
, Node e
, TypeNode tn
, unsigned tindex
, unsigned depth
);
177 /** print out debug information about this conjecture */
178 void debugPrint(const char* c
);
179 /** check side condition
181 * This returns false if the solution { d_candidates -> cvals } does not
182 * satisfy the side condition of the conjecture maintained by this class,
183 * if it exists, and true otherwise.
185 bool checkSideCondition(const std::vector
<Node
>& cvals
) const;
188 /** reference to quantifier engine */
189 QuantifiersEngine
* d_qe
;
190 /** pointer to the synth engine that owns this */
191 SynthEngine
* d_parent
;
192 /** reference to the statistics of parent */
193 SygusStatistics
& d_stats
;
194 /** term database sygus of d_qe */
196 /** The feasible guard. */
197 Node d_feasible_guard
;
199 * Do we have a solution in this solve context? This flag is reset to false
200 * on every call to presolve.
203 /** the decision strategy for the feasible guard */
204 std::unique_ptr
<DecisionStrategy
> d_feasible_strategy
;
205 /** single invocation utility */
206 std::unique_ptr
<CegSingleInv
> d_ceg_si
;
207 /** utility for static preprocessing and analysis of conjectures */
208 std::unique_ptr
<SynthConjectureProcess
> d_ceg_proc
;
209 /** grammar utility */
210 std::unique_ptr
<CegGrammarConstructor
> d_ceg_gc
;
211 /** repair constant utility */
212 std::unique_ptr
<SygusRepairConst
> d_sygus_rconst
;
213 /** example inference utility */
214 std::unique_ptr
<ExampleInfer
> d_exampleInfer
;
215 /** example evaluation cache utility for each enumerator */
216 std::map
<Node
, std::unique_ptr
<ExampleEvalCache
> > d_exampleEvalCache
;
218 //------------------------modules
219 /** program by examples module */
220 std::unique_ptr
<SygusPbe
> d_ceg_pbe
;
222 std::unique_ptr
<Cegis
> d_ceg_cegis
;
223 /** CEGIS UNIF module */
224 std::unique_ptr
<CegisUnif
> d_ceg_cegisUnif
;
225 /** connective core utility */
226 std::unique_ptr
<CegisCoreConnective
> d_sygus_ccore
;
227 /** the set of active modules (subset of the above list) */
228 std::vector
<SygusModule
*> d_modules
;
231 * This is the module (one of those above) that takes sole responsibility
232 * for this conjecture, determined during assign(...).
234 SygusModule
* d_master
;
235 //------------------------end modules
237 //------------------------enumerators
239 * Get model values for terms n, store in vector v. This method returns true
240 * if and only if all values added to v are non-null.
242 * The argument activeIncomplete indicates whether n contains an active
243 * enumerator that is currently not finished enumerating values, but returned
244 * null on a call to getEnumeratedValue. This value is used for determining
245 * whether we should call getEnumeratedValues again within a call to
246 * SynthConjecture::check.
248 * It removes terms from n that correspond to "inactive" enumerators, that
249 * is, enumerators whose values have been exhausted.
251 bool getEnumeratedValues(std::vector
<Node
>& n
,
252 std::vector
<Node
>& v
,
253 bool& activeIncomplete
);
255 * Get model value for term n. If n has a value that was excluded by
256 * datatypes sygus symmetry breaking, this method returns null. It sets
257 * activeIncomplete to true if there is an actively-generated enumerator whose
258 * current value is null but it has not finished generating values.
260 Node
getEnumeratedValue(Node n
, bool& activeIncomplete
);
261 /** enumerator generators for each actively-generated enumerator */
262 std::map
<Node
, std::unique_ptr
<EnumValGenerator
> > d_evg
;
264 * Map from enumerators to whether they are currently being
265 * "actively-generated". That is, we are in a state where we have called
266 * d_evg[e].addValue(v) for some v, and d_evg[e].getNext() has not yet
267 * returned null. The range of this map stores the abstract value that
268 * we are currently generating values from.
270 std::map
<Node
, Node
> d_ev_curr_active_gen
;
271 /** the current waiting value of each actively-generated enumerator, if any
273 * This caches values that are actively generated and that we have not yet
274 * passed to a call to SygusModule::constructCandidates. An example of when
275 * this may occur is when there are two actively-generated enumerators e1 and
276 * e2. Say on some iteration we actively-generate v1 for e1, the value
277 * of e2 was excluded by symmetry breaking, and say the current master sygus
278 * module does not handle partial models. Hence, we abort the current check.
279 * We remember that the value of e1 was v1 by storing it here, so that on
280 * a future check when v2 has a proper value, it is returned.
282 std::map
<Node
, Node
> d_ev_active_gen_waiting
;
283 /** the first value enumerated for each actively-generated enumerator
285 * This is to implement an optimization that only guards the blocking lemma
286 * for the first value of an actively-generated enumerator.
288 std::map
<Node
, Node
> d_ev_active_gen_first_val
;
289 //------------------------end enumerators
291 /** list of constants for quantified formula
292 * The outer Skolems for the negation of d_embed_quant.
294 std::vector
<Node
> d_candidates
;
295 /** base instantiation
296 * If d_embed_quant is forall d. exists y. P( d, y ), then
297 * this is the formula exists y. P( d_candidates, y ). Notice that
298 * (exists y. F) is shorthand above for ~( forall y. ~F ).
301 /** list of variables on inner quantification */
302 std::vector
<Node
> d_inner_vars
;
304 * The set of skolems for the current "verification" lemma, if one exists.
305 * This may be added to during calls to doCheck(). The model values for these
306 * skolems are analyzed during doRefine().
308 std::vector
<Node
> d_ce_sk_vars
;
310 * If we have already tested the satisfiability of the current verification
311 * lemma, this stores the model values of d_ce_sk_vars in the current
312 * (satisfiable, failed) verification lemma.
314 std::vector
<Node
> d_ce_sk_var_mvs
;
316 * Whether the above vector has been set. We have this flag since the above
317 * vector may be set to empty (e.g. for ground synthesis conjectures).
319 bool d_set_ce_sk_vars
;
321 /** the asserted (negated) conjecture */
324 * The side condition for solving the conjecture, after conversion to deep
327 Node d_embedSideCondition
;
328 /** (negated) conjecture after simplification */
330 /** (negated) conjecture after simplification, conversion to deep embedding */
332 /** candidate information */
337 /** list of terms we have instantiated candidates with */
338 std::vector
<Node
> d_inst
;
340 std::map
<Node
, CandidateInfo
> d_cinfo
;
342 * The first index of an instantiation in CandidateInfo::d_inst that we have
343 * not yet tried to repair.
345 unsigned d_repair_index
;
346 /** number of times we have called doRefine */
347 unsigned d_refine_count
;
348 /** get candidadate */
349 Node
getCandidate(unsigned int i
) { return d_candidates
[i
]; }
350 /** record instantiation (this is used to construct solutions later) */
351 void recordInstantiation(std::vector
<Node
>& vs
)
353 Assert(vs
.size() == d_candidates
.size());
354 for (unsigned i
= 0; i
< vs
.size(); i
++)
356 d_cinfo
[d_candidates
[i
]].d_inst
.push_back(vs
[i
]);
359 /** get synth solutions internal
361 * This function constructs the body of solutions for all
362 * functions-to-synthesize in this conjecture and stores them in sols, in
363 * order. For each solution added to sols, we add an integer indicating what
364 * kind of solution n is, where if sols[i] = n, then
365 * if status[i] = 0: n is the (builtin term) corresponding to the solution,
366 * if status[i] = 1: n is the sygus representation of the solution.
367 * We store builtin versions under some conditions (such as when the sygus
368 * grammar is being ignored).
370 * This consults the single invocation module to get synthesis solutions if
371 * isSingleInvocation() returns true.
373 * For example, for conjecture exists fg. forall x. f(x)>g(x), this function
374 * may set ( sols, status ) to ( { x+1, d_x() }, { 1, 0 } ), where d_x() is
375 * the sygus datatype constructor corresponding to variable x.
377 bool getSynthSolutionsInternal(std::vector
<Node
>& sols
,
378 std::vector
<int>& status
);
379 //-------------------------------- sygus stream
380 /** current stream guard */
381 Node d_current_stream_guard
;
382 /** the decision strategy for streaming solutions */
383 class SygusStreamDecisionStrategy
: public DecisionStrategyFmf
386 SygusStreamDecisionStrategy(context::Context
* satContext
,
387 Valuation valuation
);
389 Node
mkLiteral(unsigned i
) override
;
391 std::string
identify() const override
393 return std::string("sygus_stream");
396 std::unique_ptr
<SygusStreamDecisionStrategy
> d_stream_strategy
;
397 /** get current stream guard */
398 Node
getCurrentStreamGuard() const;
399 /** get stream guarded lemma
401 * If sygusStream is enabled, this returns ( G V n ) where G is the guard
402 * returned by getCurrentStreamGuard, otherwise this returns n.
404 Node
getStreamGuardedLemma(Node n
) const;
406 * Prints the current synthesis solution to the output stream indicated by
407 * the Options object, send a lemma blocking the current solution to the
408 * output channel, which we refer to as a "stream exclusion lemma".
410 * The argument enums is the set of enumerators that comprise the current
411 * solution, and values is their current values.
413 void printAndContinueStream(const std::vector
<Node
>& enums
,
414 const std::vector
<Node
>& values
);
415 /** exclude the current solution { enums -> values } */
416 void excludeCurrentSolution(const std::vector
<Node
>& enums
,
417 const std::vector
<Node
>& values
);
419 * Whether we have guarded a stream exclusion lemma when using sygusStream.
420 * This is an optimization that allows us to guard only the first stream
423 bool d_guarded_stream_exc
;
424 //-------------------------------- end sygus stream
425 /** expression miner managers for each function-to-synthesize
427 * Notice that for each function-to-synthesize, we enumerate a stream of
428 * candidate solutions, where each of these streams is independent. Thus,
429 * we maintain separate expression miner managers for each of them.
431 * This is used for the sygusRewSynth() option to synthesize new candidate
434 std::map
<Node
, ExpressionMinerManager
> d_exprm
;
437 } // namespace quantifiers
438 } // namespace theory
439 } /* namespace CVC4 */