GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/synth_conjecture.h Lines: 7 7 100.0 %
Date: 2021-09-07 Branches: 0 0 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Mathias Preiner, Abdalrhman Mohamed
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 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.
11
 * ****************************************************************************
12
 *
13
 * Class that encapsulates techniques for a single (SyGuS) synthesis
14
 * conjecture.
15
 */
16
17
#include "cvc5_private.h"
18
19
#ifndef CVC5__THEORY__QUANTIFIERS__SYNTH_CONJECTURE_H
20
#define CVC5__THEORY__QUANTIFIERS__SYNTH_CONJECTURE_H
21
22
#include <memory>
23
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/enum_val_generator.h"
30
#include "theory/quantifiers/sygus/example_eval_cache.h"
31
#include "theory/quantifiers/sygus/example_infer.h"
32
#include "theory/quantifiers/sygus/sygus_process_conj.h"
33
#include "theory/quantifiers/sygus/sygus_repair_const.h"
34
#include "theory/quantifiers/sygus/sygus_stats.h"
35
#include "theory/quantifiers/sygus/synth_verify.h"
36
#include "theory/quantifiers/sygus/template_infer.h"
37
38
namespace cvc5 {
39
namespace theory {
40
namespace quantifiers {
41
42
class CegGrammarConstructor;
43
class SygusPbe;
44
class SygusStatistics;
45
class EnumValueManager;
46
47
/** a synthesis conjecture
48
 * This class implements approaches for a synthesis conjecture, given by data
49
 * member d_quant.
50
 * This includes both approaches for synthesis in Reynolds et al CAV 2015. It
51
 * determines which approach and optimizations are applicable to the
52
 * conjecture, and has interfaces for implementing them.
53
 */
54
class SynthConjecture
55
{
56
 public:
57
  SynthConjecture(QuantifiersState& qs,
58
                  QuantifiersInferenceManager& qim,
59
                  QuantifiersRegistry& qr,
60
                  TermRegistry& tr,
61
                  SygusStatistics& s);
62
  ~SynthConjecture();
63
  /** presolve */
64
  void presolve();
65
  /** get original version of conjecture */
66
4848
  Node getConjecture() const { return d_quant; }
67
  /** get deep embedding version of conjecture */
68
  Node getEmbeddedConjecture() const { return d_embed_quant; }
69
  //-------------------------------for counterexample-guided check/refine
70
  /** whether the conjecture is waiting for a call to doCheck below */
71
  bool needsCheck();
72
  /** whether the conjecture is waiting for a call to doRefine below */
73
  bool needsRefinement() const;
74
  /** do syntax-guided enumerative check
75
   *
76
   * This is step 2(a) of Figure 3 of Reynolds et al CAV 2015.
77
   *
78
   * The method returns true if this conjecture is finished trying solutions
79
   * for the given call to SynthEngine::check.
80
   *
81
   * Notice that we make multiple calls to doCheck on one call to
82
   * SynthEngine::check. For example, if we are using an actively-generated
83
   * enumerator, one enumerated (abstract) term may correspond to multiple
84
   * concrete terms t1, ..., tn to check, where we make up to n calls to doCheck
85
   * when each of t1, ..., tn fails to satisfy the current refinement lemmas.
86
   */
87
  bool doCheck();
88
  /** do refinement
89
   *
90
   * This is step 2(b) of Figure 3 of Reynolds et al CAV 2015.
91
   *
92
   * This method is run when needsRefinement() returns true, indicating that
93
   * the last call to doCheck found a counterexample to the last candidate.
94
   *
95
   * This method adds a refinement lemma on the output channel of quantifiers
96
   * engine. If the refinement lemma is a duplicate, then we manually
97
   * exclude the current candidate via excludeCurrentSolution. This should
98
   * only occur when the synthesis conjecture for the current candidate fails
99
   * to evaluate to false for a given counterexample point, but regardless its
100
   * negation is satisfiable for the current candidate and that point. This is
101
   * exclusive to theories with partial functions, e.g. (non-linear) division.
102
   *
103
   * This method returns true if a lemma was added on the output channel, and
104
   * false otherwise.
105
   */
106
  bool doRefine();
107
  //-------------------------------end for counterexample-guided check/refine
108
  /**
109
   * Prints the current synthesis solution to output stream out. This is
110
   * currently used for printing solutions for sygusStream only. We do not
111
   * enclose solutions in parentheses.
112
   */
113
  void printSynthSolutionInternal(std::ostream& out);
114
  /** get synth solutions
115
   *
116
   * This method returns true if this class has a solution available to the
117
   * conjecture that it was assigned.
118
   *
119
   * Let q be the synthesis conjecture assigned to this class.
120
   * This method adds entries to sol_map[q] that map functions-to-synthesize to
121
   * their builtin solution, which has the same type. For example, for synthesis
122
   * conjecture exists f. forall x. f( x )>x, this function will update
123
   * sol_map[q] to contain the entry:
124
   *   f -> (lambda x. x+1)
125
   */
126
  bool getSynthSolutions(std::map<Node, std::map<Node, Node> >& sol_map);
127
  /**
128
   * The feasible guard whose semantics are "this conjecture is feasiable".
129
   * This is "G" in Figure 3 of Reynolds et al CAV 2015.
130
   */
131
  Node getGuard() const;
132
  /** is ground */
133
  bool isGround() { return d_inner_vars.empty(); }
134
  /** are we using single invocation techniques */
135
  bool isSingleInvocation() const;
136
  /** preregister conjecture
137
   * This is used as a heuristic for solution reconstruction, so that we
138
   * remember expressions in the conjecture before preprocessing, since they
139
   * may be helpful during solution reconstruction (Figure 5 of Reynolds et al
140
   * CAV 2015)
141
   */
142
  void preregisterConjecture(Node q);
143
  /** assign conjecture q to this class */
144
  void assign(Node q);
145
  /** has a conjecture been assigned to this class */
146
587
  bool isAssigned() { return !d_embed_quant.isNull(); }
147
  /**
148
   * Get model value for term n.
149
   */
150
  Node getModelValue(Node n);
151
152
  /** get utility for static preprocessing and analysis of conjectures */
153
298
  SynthConjectureProcess* getProcess() { return d_ceg_proc.get(); }
154
  /** get constant repair utility */
155
110
  SygusRepairConst* getRepairConst() { return d_sygus_rconst.get(); }
156
  /** get example inference utility */
157
12709
  ExampleInfer* getExampleInfer() { return d_exampleInfer.get(); }
158
  /** get the example evaluation cache utility for enumerator e */
159
  ExampleEvalCache* getExampleEvalCache(Node e);
160
  /** get program by examples module */
161
  SygusPbe* getPbe() { return d_ceg_pbe.get(); }
162
  /** get the symmetry breaking predicate for type */
163
  Node getSymmetryBreakingPredicate(
164
      Node x, Node e, TypeNode tn, unsigned tindex, unsigned depth);
165
  /** print out debug information about this conjecture */
166
  void debugPrint(const char* c);
167
  /** check side condition
168
   *
169
   * This returns false if the solution { d_candidates -> cvals } does not
170
   * satisfy the side condition of the conjecture maintained by this class,
171
   * if it exists, and true otherwise.
172
   */
173
  bool checkSideCondition(const std::vector<Node>& cvals) const;
174
175
  /** get a reference to the statistics of parent */
176
  SygusStatistics& getSygusStatistics() { return d_stats; };
177
178
 private:
179
  /** Reference to the quantifiers state */
180
  QuantifiersState& d_qstate;
181
  /** Reference to the quantifiers inference manager */
182
  QuantifiersInferenceManager& d_qim;
183
  /** The quantifiers registry */
184
  QuantifiersRegistry& d_qreg;
185
  /** Reference to the term registry */
186
  TermRegistry& d_treg;
187
  /** reference to the statistics of parent */
188
  SygusStatistics& d_stats;
189
  /** term database sygus of d_qe */
190
  TermDbSygus* d_tds;
191
  /** The synthesis verify utility */
192
  SynthVerify d_verify;
193
  /** The feasible guard. */
194
  Node d_feasible_guard;
195
  /**
196
   * Do we have a solution in this solve context? This flag is reset to false
197
   * on every call to presolve.
198
   */
199
  bool d_hasSolution;
200
  /** the decision strategy for the feasible guard */
201
  std::unique_ptr<DecisionStrategy> d_feasible_strategy;
202
  /** single invocation utility */
203
  std::unique_ptr<CegSingleInv> d_ceg_si;
204
  /** template inference utility */
205
  std::unique_ptr<SygusTemplateInfer> d_templInfer;
206
  /** utility for static preprocessing and analysis of conjectures */
207
  std::unique_ptr<SynthConjectureProcess> d_ceg_proc;
208
  /** grammar utility */
209
  std::unique_ptr<CegGrammarConstructor> d_ceg_gc;
210
  /** repair constant utility */
211
  std::unique_ptr<SygusRepairConst> d_sygus_rconst;
212
  /** example inference utility */
213
  std::unique_ptr<ExampleInfer> d_exampleInfer;
214
  /** map from enumerators to their enumerator manager */
215
  std::map<Node, std::unique_ptr<EnumValueManager>> d_enumManager;
216
217
  //------------------------modules
218
  /** program by examples module */
219
  std::unique_ptr<SygusPbe> d_ceg_pbe;
220
  /** CEGIS module */
221
  std::unique_ptr<Cegis> d_ceg_cegis;
222
  /** CEGIS UNIF module */
223
  std::unique_ptr<CegisUnif> d_ceg_cegisUnif;
224
  /** connective core utility */
225
  std::unique_ptr<CegisCoreConnective> d_sygus_ccore;
226
  /** the set of active modules (subset of the above list) */
227
  std::vector<SygusModule*> d_modules;
228
  /** master module
229
   *
230
   * This is the module (one of those above) that takes sole responsibility
231
   * for this conjecture, determined during assign(...).
232
   */
233
  SygusModule* d_master;
234
  //------------------------end modules
235
236
  //------------------------enumerators
237
  /**
238
   * Get model values for terms n, store in vector v. This method returns true
239
   * if and only if all values added to v are non-null.
240
   *
241
   * The argument activeIncomplete indicates whether n contains an active
242
   * enumerator that is currently not finished enumerating values, but returned
243
   * null on a call to getEnumeratedValue. This value is used for determining
244
   * whether we should call getEnumeratedValues again within a call to
245
   * SynthConjecture::check.
246
   *
247
   * It removes terms from n that correspond to "inactive" enumerators, that
248
   * is, enumerators whose values have been exhausted.
249
   */
250
  bool getEnumeratedValues(std::vector<Node>& n,
251
                           std::vector<Node>& v,
252
                           bool& activeIncomplete);
253
  /**
254
   * Get or make enumerator manager for the enumerator e.
255
   */
256
  EnumValueManager* getEnumValueManagerFor(Node e);
257
  //------------------------end enumerators
258
259
  /** list of constants for quantified formula
260
   * The outer Skolems for the negation of d_embed_quant.
261
   */
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 ).
267
   */
268
  Node d_base_inst;
269
  /** list of variables on inner quantification */
270
  std::vector<Node> d_inner_vars;
271
  /**
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().
275
   */
276
  std::vector<Node> d_ce_sk_vars;
277
  /**
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.
281
   */
282
  std::vector<Node> d_ce_sk_var_mvs;
283
  /**
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).
286
   */
287
  bool d_set_ce_sk_vars;
288
289
  /** the asserted (negated) conjecture */
290
  Node d_quant;
291
  /**
292
   * The side condition for solving the conjecture, after conversion to deep
293
   * embedding.
294
   */
295
  Node d_embedSideCondition;
296
  /** (negated) conjecture after simplification */
297
  Node d_simp_quant;
298
  /** (negated) conjecture after simplification, conversion to deep embedding */
299
  Node d_embed_quant;
300
  /** candidate information */
301
1852
  class CandidateInfo
302
  {
303
   public:
304
1852
    CandidateInfo() {}
305
    /** list of terms we have instantiated candidates with */
306
    std::vector<Node> d_inst;
307
  };
308
  std::map<Node, CandidateInfo> d_cinfo;
309
  /**
310
   * The first index of an instantiation in CandidateInfo::d_inst that we have
311
   * not yet tried to repair.
312
   */
313
  unsigned d_repair_index;
314
  /** record solution (this is used to construct solutions later) */
315
  void recordSolution(std::vector<Node>& vs);
316
  /** get synth solutions internal
317
   *
318
   * This function constructs the body of solutions for all
319
   * functions-to-synthesize in this conjecture and stores them in sols, in
320
   * order. For each solution added to sols, we add an integer indicating what
321
   * kind of solution n is, where if sols[i] = n, then
322
   *   if status[i] = 0: n is the (builtin term) corresponding to the solution,
323
   *   if status[i] = 1: n is the sygus representation of the solution.
324
   * We store builtin versions under some conditions (such as when the sygus
325
   * grammar is being ignored).
326
   *
327
   * This consults the single invocation module to get synthesis solutions if
328
   * isSingleInvocation() returns true.
329
   *
330
   * For example, for conjecture exists fg. forall x. f(x)>g(x), this function
331
   * may set ( sols, status ) to ( { x+1, d_x() }, { 1, 0 } ), where d_x() is
332
   * the sygus datatype constructor corresponding to variable x.
333
   */
334
  bool getSynthSolutionsInternal(std::vector<Node>& sols,
335
                                 std::vector<int8_t>& status);
336
  //-------------------------------- sygus stream
337
  /**
338
   * Prints the current synthesis solution to the output stream indicated by
339
   * the Options object, send a lemma blocking the current solution to the
340
   * output channel, which we refer to as a "stream exclusion lemma".
341
   *
342
   * The argument enums is the set of enumerators that comprise the current
343
   * solution, and values is their current values.
344
   */
345
  void printAndContinueStream(const std::vector<Node>& enums,
346
                              const std::vector<Node>& values);
347
  /** exclude the current solution { enums -> values } */
348
  void excludeCurrentSolution(const std::vector<Node>& enums,
349
                              const std::vector<Node>& values);
350
  /**
351
   * Whether we have guarded a stream exclusion lemma when using sygusStream.
352
   * This is an optimization that allows us to guard only the first stream
353
   * exclusion lemma.
354
   */
355
  bool d_guarded_stream_exc;
356
  //-------------------------------- end sygus stream
357
  /** expression miner managers for each function-to-synthesize
358
   *
359
   * Notice that for each function-to-synthesize, we enumerate a stream of
360
   * candidate solutions, where each of these streams is independent. Thus,
361
   * we maintain separate expression miner managers for each of them.
362
   *
363
   * This is used for the sygusRewSynth() option to synthesize new candidate
364
   * rewrite rules.
365
   */
366
  std::map<Node, std::unique_ptr<ExpressionMinerManager>> d_exprm;
367
};
368
369
}  // namespace quantifiers
370
}  // namespace theory
371
}  // namespace cvc5
372
373
#endif