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