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