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

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Haniel Barbosa, Mathias Preiner
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
 * cegis
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__QUANTIFIERS__CEGIS_H
19
#define CVC5__THEORY__QUANTIFIERS__CEGIS_H
20
21
#include <map>
22
23
#include "smt/env_obj.h"
24
#include "theory/quantifiers/sygus/sygus_module.h"
25
#include "theory/quantifiers/sygus_sampler.h"
26
27
namespace cvc5 {
28
namespace theory {
29
namespace quantifiers {
30
31
class SygusEvalUnfold;
32
33
/** Cegis
34
 *
35
 * The default sygus module for synthesis, counterexample-guided inductive
36
 * synthesis (CEGIS).
37
 *
38
 * It initializes a list of sygus enumerators that are one-to-one with
39
 * candidates, and returns a list of candidates that are the model values
40
 * of these enumerators on calls to constructCandidates.
41
 *
42
 * It implements an optimization (getRefinementEvalLemmas) that evaluates all
43
 * previous refinement lemmas for a term before returning it as a candidate
44
 * in calls to constructCandidates.
45
 */
46
class Cegis : public SygusModule
47
{
48
 public:
49
  Cegis(Env& env,
50
        QuantifiersState& qs,
51
        QuantifiersInferenceManager& qim,
52
        TermDbSygus* tds,
53
        SynthConjecture* p);
54
4764
  ~Cegis() override {}
55
  /** initialize */
56
  virtual bool initialize(Node conj,
57
                          Node n,
58
                          const std::vector<Node>& candidates) override;
59
  /** get term list */
60
  virtual void getTermList(const std::vector<Node>& candidates,
61
                           std::vector<Node>& enums) override;
62
  /** construct candidate */
63
  virtual bool constructCandidates(
64
      const std::vector<Node>& enums,
65
      const std::vector<Node>& enum_values,
66
      const std::vector<Node>& candidates,
67
      std::vector<Node>& candidate_values) override;
68
  /** register refinement lemma
69
   *
70
   * This function stores lem as a refinement lemma, and adds it to lems.
71
   */
72
  virtual void registerRefinementLemma(const std::vector<Node>& vars,
73
                                       Node lem) override;
74
  /** using repair const */
75
  virtual bool usingRepairConst() override;
76
77
 protected:
78
  /** the evaluation unfold utility of d_tds */
79
  SygusEvalUnfold* d_eval_unfold;
80
  /** If SynthConjecture::d_base_inst is exists y. P( d, y ), then this is y. */
81
  std::vector<Node> d_base_vars;
82
  /**
83
   * If SynthConjecture::d_base_inst is exists y. P( d, y ), then this is the
84
   * formula P( SynthConjecture::d_candidates, y ).
85
   */
86
  Node d_base_body;
87
  //----------------------------------cegis-implementation-specific
88
  /**
89
   * Do cegis-implementation-specific initialization for this class. The return
90
   * value and behavior of this function is the same as initialize(...) above.
91
   */
92
  virtual bool processInitialize(Node conj,
93
                                 Node n,
94
                                 const std::vector<Node>& candidates);
95
  /** do cegis-implementation-specific post-processing for construct candidate
96
   *
97
   * satisfiedRl is whether all refinement lemmas are satisfied under the
98
   * substitution { enums -> enum_values }.
99
   *
100
   * The return value and behavior of this function is the same as
101
   * constructCandidates(...) above.
102
   */
103
  virtual bool processConstructCandidates(const std::vector<Node>& enums,
104
                                          const std::vector<Node>& enum_values,
105
                                          const std::vector<Node>& candidates,
106
                                          std::vector<Node>& candidate_values,
107
                                          bool satisfiedRl);
108
  //----------------------------------end cegis-implementation-specific
109
110
  //-----------------------------------refinement lemmas
111
  /** refinement lemmas */
112
  std::vector<Node> d_refinement_lemmas;
113
  /** (processed) conjunctions of refinement lemmas that are not unit */
114
  std::unordered_set<Node> d_refinement_lemma_conj;
115
  /** (processed) conjunctions of refinement lemmas that are unit */
116
  std::unordered_set<Node> d_refinement_lemma_unit;
117
  /** substitution entailed by d_refinement_lemma_unit */
118
  std::vector<Node> d_rl_eval_hds;
119
  std::vector<Node> d_rl_vals;
120
  /** all variables appearing in refinement lemmas */
121
  std::unordered_set<Node> d_refinement_lemma_vars;
122
123
  /** adds lem as a refinement lemma */
124
  void addRefinementLemma(Node lem);
125
  /** add refinement lemma conjunct
126
   *
127
   * This is a helper function for addRefinementLemma.
128
   *
129
   * This adds waiting[wcounter] to the proper vector (d_refinement_lemma_conj
130
   * or d_refinement_lemma_unit). In the case that waiting[wcounter] corresponds
131
   * to a value propagation, e.g. it is of the form:
132
   *   (eval x c1...cn) = c
133
   * then it is added to d_refinement_lemma_unit, (eval x c1...cn) -> c is added
134
   * as a substitution in d_rl_eval_hds/d_rl_eval_vals, and applied to previous
135
   * lemmas in d_refinement_lemma_conj and lemmas waiting[k] for k>wcounter.
136
   * Each lemma in d_refinement_lemma_conj that is modifed in this process is
137
   * moved to the vector waiting.
138
   */
139
  void addRefinementLemmaConjunct(unsigned wcounter,
140
                                  std::vector<Node>& waiting);
141
  /** sample add refinement lemma
142
   *
143
   * This function will check if there is a sample point in d_sampler that
144
   * refutes the candidate solution (d_quant_vars->vals). If so, it adds a
145
   * refinement lemma to the lists d_refinement_lemmas that corresponds to that
146
   * sample point, and adds a lemma to d_qim pending lemmas if cegisSample mode
147
   * is not trust.
148
   */
149
  bool sampleAddRefinementLemma(const std::vector<Node>& candidates,
150
                                const std::vector<Node>& vals);
151
152
  /** evaluates candidate values on current refinement lemmas
153
   *
154
   * This method performs techniques that ensure that
155
   * { candidates -> candidate_values } is a candidate solution that should
156
   * be checked by the solution verifier of the CEGIS loop. This method
157
   * invokes two sub-methods which may reject the current solution.
158
   * The first is "refinement evaluation", described above the method
159
   * getRefinementEvalLemmas below. The second is "evaluation unfolding",
160
   * which eagerly unfolds applications of evaluation functions (see
161
   * sygus_eval_unfold.h for details).
162
   *
163
   * If this method returns true, then { candidates -> candidate_values }
164
   * is not ready to be tried as a candidate solution. In this case, it may add
165
   * lemmas to lems.
166
   *
167
   * Notice that this method may return true without adding any lemmas to
168
   * lems, in the case that terms from candidates are "actively-generated
169
   * enumerators", since the model values of such terms are managed
170
   * explicitly within getEnumeratedValue. In this case, the owner of the
171
   * actively-generated enumerators (e.g. SynthConjecture) is responsible for
172
   * blocking the current value of candidates.
173
   */
174
  bool addEvalLemmas(const std::vector<Node>& candidates,
175
                     const std::vector<Node>& candidate_values);
176
  /** Get the node corresponding to the conjunction of all refinement lemmas. */
177
  Node getRefinementLemmaFormula();
178
  //-----------------------------------end refinement lemmas
179
180
  /** Get refinement evaluation lemmas
181
   *
182
   * This method performs "refinement evaluation", that is, it tests
183
   * whether the current solution, given by { vs -> ms },
184
   * satisfies all current refinement lemmas. If it does not, it may add
185
   * blocking lemmas L to lems which exclude (a generalization of) the current
186
   * solution.
187
   *
188
   * Given a candidate solution ms for candidates vs, this function adds lemmas
189
   * to lems based on evaluating the conjecture, instantiated for ms, on lemmas
190
   * for previous refinements (d_refinement_lemmas).
191
   *
192
   * Returns true if any such lemma exists.
193
   */
194
  bool getRefinementEvalLemmas(const std::vector<Node>& vs,
195
                               const std::vector<Node>& ms,
196
                               std::vector<Node>& lems);
197
  /** Check refinement evaluation lemmas
198
   *
199
   * This method is similar to above, but does not perform any generalization
200
   * techniques. It is used when we are using only fast enumerators for
201
   * all functions-to-synthesize.
202
   *
203
   * Returns true if a refinement lemma is false for the solution
204
   * { vs -> ms }.
205
   */
206
  bool checkRefinementEvalLemmas(const std::vector<Node>& vs,
207
                                 const std::vector<Node>& ms);
208
  /** sampler object for the option cegisSample()
209
   *
210
   * This samples points of the type of the inner variables of the synthesis
211
   * conjecture (d_base_vars).
212
   */
213
  SygusSampler d_cegis_sampler;
214
  /** cegis sample refine points
215
   *
216
   * Stores the list of indices of sample points in d_cegis_sampler we have
217
   * added as refinement lemmas.
218
   */
219
  std::unordered_set<unsigned> d_cegis_sample_refine;
220
221
  //---------------------------------for symbolic constructors
222
  /** are we using symbolic constants?
223
   *
224
   * This flag is set ot true if at least one of the enumerators allocated
225
   * by this class has been configured to allow model values with symbolic
226
   * constructors, such as the "any constant" constructor.
227
   */
228
  bool d_usingSymCons;
229
  //---------------------------------end for symbolic constructors
230
};
231
232
}  // namespace quantifiers
233
}  // namespace theory
234
}  // namespace cvc5
235
236
#endif /* CVC5__THEORY__QUANTIFIERS__CEGIS_H */