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