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_unif.h"
28 #include "theory/quantifiers/sygus/sygus_grammar_cons.h"
29 #include "theory/quantifiers/sygus/sygus_pbe.h"
30 #include "theory/quantifiers/sygus/sygus_process_conj.h"
31 #include "theory/quantifiers/sygus/sygus_repair_const.h"
35 namespace quantifiers
{
38 * A base class for generating values for actively-generated enumerators.
39 * At a high level, the job of this class is to accept a stream of "abstract
40 * values" a1, ..., an, ..., and generate a (possibly larger) stream of
41 * "concrete values" c11, ..., c1{m_1}, ..., cn1, ... cn{m_n}, ....
43 class EnumValGenerator
46 virtual ~EnumValGenerator() {}
47 /** initialize this class with enumerator e */
48 virtual void initialize(Node e
) = 0;
49 /** Inform this generator that abstract value v was enumerated. */
50 virtual void addValue(Node v
) = 0;
52 * Increment this value generator. If this returns false, then we are out of
53 * values. If this returns true, getCurrent(), if non-null, returns the
56 virtual bool increment() = 0;
57 /** Get the current concrete value generated by this class. */
58 virtual Node
getCurrent() = 0;
61 /** a synthesis conjecture
62 * This class implements approaches for a synthesis conecjture, given by data
64 * This includes both approaches for synthesis in Reynolds et al CAV 2015. It
65 * determines which approach and optimizations are applicable to the
66 * conjecture, and has interfaces for implementing them.
71 SynthConjecture(QuantifiersEngine
* qe
);
75 /** get original version of conjecture */
76 Node
getConjecture() { return d_quant
; }
77 /** get deep embedding version of conjecture */
78 Node
getEmbeddedConjecture() { return d_embed_quant
; }
79 //-------------------------------for counterexample-guided check/refine
80 /** increment the number of times we have successfully done candidate
82 void incrementRefineCount() { d_refine_count
++; }
83 /** whether the conjecture is waiting for a call to doCheck below */
85 /** whether the conjecture is waiting for a call to doRefine below */
86 bool needsRefinement() const;
87 /** do syntax-guided enumerative check
89 * This is step 2(a) of Figure 3 of Reynolds et al CAV 2015.
91 * The method returns true if this conjecture is finished trying solutions
92 * for the given call to SynthEngine::check.
94 * Notice that we make multiple calls to doCheck on one call to
95 * SynthEngine::check. For example, if we are using an actively-generated
96 * enumerator, one enumerated (abstract) term may correspond to multiple
97 * concrete terms t1, ..., tn to check, where we make up to n calls to doCheck
98 * when each of t1, ..., tn fails to satisfy the current refinement lemmas.
100 bool doCheck(std::vector
<Node
>& lems
);
102 * This is step 2(b) of Figure 3 of Reynolds et al CAV 2015.
104 void doRefine(std::vector
<Node
>& lems
);
105 //-------------------------------end for counterexample-guided check/refine
107 * Prints the synthesis solution to output stream out. This invokes solution
108 * reconstruction if the conjecture is single invocation. Otherwise, it
109 * returns the solution found by sygus enumeration.
111 void printSynthSolution(std::ostream
& out
);
112 /** get synth solutions
114 * This returns a map from function-to-synthesize variables to their
115 * builtin solution, which has the same type. For example, for synthesis
116 * conjecture exists f. forall x. f( x )>x, this function may return the map
117 * containing the entry:
118 * f -> (lambda x. x+1)
120 void getSynthSolutions(std::map
<Node
, Node
>& sol_map
);
122 * The feasible guard whose semantics are "this conjecture is feasiable".
123 * This is "G" in Figure 3 of Reynolds et al CAV 2015.
125 Node
getGuard() const;
127 bool isGround() { return d_inner_vars
.empty(); }
128 /** are we using single invocation techniques */
129 bool isSingleInvocation() const;
130 /** preregister conjecture
131 * This is used as a heuristic for solution reconstruction, so that we
132 * remember expressions in the conjecture before preprocessing, since they
133 * may be helpful during solution reconstruction (Figure 5 of Reynolds et al
136 void preregisterConjecture(Node q
);
137 /** assign conjecture q to this class */
139 /** has a conjecture been assigned to this class */
140 bool isAssigned() { return !d_embed_quant
.isNull(); }
142 * Get model value for term n.
144 Node
getModelValue(Node n
);
146 /** get utility for static preprocessing and analysis of conjectures */
147 SynthConjectureProcess
* getProcess() { return d_ceg_proc
.get(); }
148 /** get constant repair utility */
149 SygusRepairConst
* getRepairConst() { return d_sygus_rconst
.get(); }
150 /** get program by examples module */
151 SygusPbe
* getPbe() { return d_ceg_pbe
.get(); }
152 /** get the symmetry breaking predicate for type */
153 Node
getSymmetryBreakingPredicate(
154 Node x
, Node e
, TypeNode tn
, unsigned tindex
, unsigned depth
);
155 /** print out debug information about this conjecture */
156 void debugPrint(const char* c
);
157 /** check side condition
159 * This returns false if the solution { d_candidates -> cvals } does not
160 * satisfy the side condition of the conjecture maintained by this class,
161 * if it exists, and true otherwise.
163 bool checkSideCondition(const std::vector
<Node
>& cvals
) const;
166 /** reference to quantifier engine */
167 QuantifiersEngine
* d_qe
;
168 /** term database sygus of d_qe */
170 /** The feasible guard. */
171 Node d_feasible_guard
;
173 * Do we have a solution in this user context? This is user-context dependent
174 * to enable use cases of sygus in incremental mode.
176 context::CDO
<bool> d_hasSolution
;
177 /** the decision strategy for the feasible guard */
178 std::unique_ptr
<DecisionStrategy
> d_feasible_strategy
;
179 /** single invocation utility */
180 std::unique_ptr
<CegSingleInv
> d_ceg_si
;
181 /** utility for static preprocessing and analysis of conjectures */
182 std::unique_ptr
<SynthConjectureProcess
> d_ceg_proc
;
183 /** grammar utility */
184 std::unique_ptr
<CegGrammarConstructor
> d_ceg_gc
;
185 /** repair constant utility */
186 std::unique_ptr
<SygusRepairConst
> d_sygus_rconst
;
188 //------------------------modules
189 /** program by examples module */
190 std::unique_ptr
<SygusPbe
> d_ceg_pbe
;
192 std::unique_ptr
<Cegis
> d_ceg_cegis
;
193 /** CEGIS UNIF module */
194 std::unique_ptr
<CegisUnif
> d_ceg_cegisUnif
;
195 /** the set of active modules (subset of the above list) */
196 std::vector
<SygusModule
*> d_modules
;
199 * This is the module (one of those above) that takes sole responsibility
200 * for this conjecture, determined during assign(...).
202 SygusModule
* d_master
;
203 //------------------------end modules
205 //------------------------enumerators
207 * Get model values for terms n, store in vector v. This method returns true
208 * if and only if all values added to v are non-null.
210 * The argument activeIncomplete indicates whether n contains an active
211 * enumerator that is currently not finished enumerating values, but returned
212 * null on a call to getEnumeratedValue. This value is used for determining
213 * whether we should call getEnumeratedValues again within a call to
214 * SynthConjecture::check.
216 * It removes terms from n that correspond to "inactive" enumerators, that
217 * is, enumerators whose values have been exhausted.
219 bool getEnumeratedValues(std::vector
<Node
>& n
,
220 std::vector
<Node
>& v
,
221 bool& activeIncomplete
);
223 * Get model value for term n. If n has a value that was excluded by
224 * datatypes sygus symmetry breaking, this method returns null. It sets
225 * activeIncomplete to true if there is an actively-generated enumerator whose
226 * current value is null but it has not finished generating values.
228 Node
getEnumeratedValue(Node n
, bool& activeIncomplete
);
229 /** enumerator generators for each actively-generated enumerator */
230 std::map
<Node
, std::unique_ptr
<EnumValGenerator
> > d_evg
;
232 * Map from enumerators to whether they are currently being
233 * "actively-generated". That is, we are in a state where we have called
234 * d_evg[e].addValue(v) for some v, and d_evg[e].getNext() has not yet
235 * returned null. The range of this map stores the abstract value that
236 * we are currently generating values from.
238 std::map
<Node
, Node
> d_ev_curr_active_gen
;
239 /** the current waiting value of each actively-generated enumerator, if any
241 * This caches values that are actively generated and that we have not yet
242 * passed to a call to SygusModule::constructCandidates. An example of when
243 * this may occur is when there are two actively-generated enumerators e1 and
244 * e2. Say on some iteration we actively-generate v1 for e1, the value
245 * of e2 was excluded by symmetry breaking, and say the current master sygus
246 * module does not handle partial models. Hence, we abort the current check.
247 * We remember that the value of e1 was v1 by storing it here, so that on
248 * a future check when v2 has a proper value, it is returned.
250 std::map
<Node
, Node
> d_ev_active_gen_waiting
;
251 /** the first value enumerated for each actively-generated enumerator
253 * This is to implement an optimization that only guards the blocking lemma
254 * for the first value of an actively-generated enumerator.
256 std::map
<Node
, Node
> d_ev_active_gen_first_val
;
257 //------------------------end enumerators
259 /** list of constants for quantified formula
260 * The outer Skolems for the negation of d_embed_quant.
262 std::vector
<Node
> d_candidates
;
263 /** base instantiation
264 * If d_embed_quant is forall d. exists y. P( d, y ), then
265 * this is the formula exists y. P( d_candidates, y ). Notice that
266 * (exists y. F) is shorthand above for ~( forall y. ~F ).
269 /** list of variables on inner quantification */
270 std::vector
<Node
> d_inner_vars
;
272 * The set of skolems for the current "verification" lemma, if one exists.
273 * This may be added to during calls to doCheck(). The model values for these
274 * skolems are analyzed during doRefine().
276 std::vector
<Node
> d_ce_sk_vars
;
278 * If we have already tested the satisfiability of the current verification
279 * lemma, this stores the model values of d_ce_sk_vars in the current
280 * (satisfiable, failed) verification lemma.
282 std::vector
<Node
> d_ce_sk_var_mvs
;
284 * Whether the above vector has been set. We have this flag since the above
285 * vector may be set to empty (e.g. for ground synthesis conjectures).
287 bool d_set_ce_sk_vars
;
289 /** the asserted (negated) conjecture */
292 * The side condition for solving the conjecture, after conversion to deep
295 Node d_embedSideCondition
;
296 /** (negated) conjecture after simplification */
298 /** (negated) conjecture after simplification, conversion to deep embedding */
300 /** candidate information */
305 /** list of terms we have instantiated candidates with */
306 std::vector
<Node
> d_inst
;
308 std::map
<Node
, CandidateInfo
> d_cinfo
;
310 * The first index of an instantiation in CandidateInfo::d_inst that we have
311 * not yet tried to repair.
313 unsigned d_repair_index
;
314 /** number of times we have called doRefine */
315 unsigned d_refine_count
;
316 /** get candidadate */
317 Node
getCandidate(unsigned int i
) { return d_candidates
[i
]; }
318 /** record instantiation (this is used to construct solutions later) */
319 void recordInstantiation(std::vector
<Node
>& vs
)
321 Assert(vs
.size() == d_candidates
.size());
322 for (unsigned i
= 0; i
< vs
.size(); i
++)
324 d_cinfo
[d_candidates
[i
]].d_inst
.push_back(vs
[i
]);
327 /** get synth solutions internal
329 * This function constructs the body of solutions for all
330 * functions-to-synthesize in this conjecture and stores them in sols, in
331 * order. For each solution added to sols, we add an integer indicating what
332 * kind of solution n is, where if sols[i] = n, then
333 * if status[i] = 0: n is the (builtin term) corresponding to the solution,
334 * if status[i] = 1: n is the sygus representation of the solution.
335 * We store builtin versions under some conditions (such as when the sygus
336 * grammar is being ignored).
338 * This consults the single invocation module to get synthesis solutions if
339 * isSingleInvocation() returns true.
341 * For example, for conjecture exists fg. forall x. f(x)>g(x), this function
342 * may set ( sols, status ) to ( { x+1, d_x() }, { 1, 0 } ), where d_x() is
343 * the sygus datatype constructor corresponding to variable x.
345 bool getSynthSolutionsInternal(std::vector
<Node
>& sols
,
346 std::vector
<int>& status
);
347 //-------------------------------- sygus stream
348 /** current stream guard */
349 Node d_current_stream_guard
;
350 /** the decision strategy for streaming solutions */
351 class SygusStreamDecisionStrategy
: public DecisionStrategyFmf
354 SygusStreamDecisionStrategy(context::Context
* satContext
,
355 Valuation valuation
);
357 Node
mkLiteral(unsigned i
) override
;
359 std::string
identify() const override
361 return std::string("sygus_stream");
364 std::unique_ptr
<SygusStreamDecisionStrategy
> d_stream_strategy
;
365 /** get current stream guard */
366 Node
getCurrentStreamGuard() const;
367 /** get stream guarded lemma
369 * If sygusStream is enabled, this returns ( G V n ) where G is the guard
370 * returned by getCurrentStreamGuard, otherwise this returns n.
372 Node
getStreamGuardedLemma(Node n
) const;
374 * Prints the current synthesis solution to the output stream indicated by
375 * the Options object, send a lemma blocking the current solution to the
376 * output channel, which we refer to as a "stream exclusion lemma".
378 * The argument enums is the set of enumerators that comprise the current
379 * solution, and values is their current values.
381 void printAndContinueStream(const std::vector
<Node
>& enums
,
382 const std::vector
<Node
>& values
);
383 /** exclude the current solution { enums -> values } */
384 void excludeCurrentSolution(const std::vector
<Node
>& enums
,
385 const std::vector
<Node
>& values
);
387 * Whether we have guarded a stream exclusion lemma when using sygusStream.
388 * This is an optimization that allows us to guard only the first stream
391 bool d_guarded_stream_exc
;
392 //-------------------------------- end sygus stream
393 /** expression miner managers for each function-to-synthesize
395 * Notice that for each function-to-synthesize, we enumerate a stream of
396 * candidate solutions, where each of these streams is independent. Thus,
397 * we maintain separate expression miner managers for each of them.
399 * This is used for the sygusRewSynth() option to synthesize new candidate
402 std::map
<Node
, ExpressionMinerManager
> d_exprm
;
405 } // namespace quantifiers
406 } // namespace theory
407 } /* namespace CVC4 */