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