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