GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/cegis_unif.h Lines: 5 5 100.0 %
Date: 2021-05-22 Branches: 13 26 50.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Haniel Barbosa, Andres Noetzli
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 with unification techinques.
14
 */
15
#include "cvc5_private.h"
16
17
#ifndef CVC5__THEORY__QUANTIFIERS__SYGUS__CEGIS_UNIF_H
18
#define CVC5__THEORY__QUANTIFIERS__SYGUS__CEGIS_UNIF_H
19
20
#include <map>
21
#include <vector>
22
23
#include "theory/decision_strategy.h"
24
#include "theory/quantifiers/sygus/cegis.h"
25
#include "theory/quantifiers/sygus/sygus_unif_rl.h"
26
27
namespace cvc5 {
28
namespace theory {
29
namespace quantifiers {
30
31
/** Cegis Unif Enumerators Decision Strategy
32
 *
33
 * This class enforces a decision strategy that limits the number of
34
 * unique values given to the set of heads of evaluation points and conditions
35
 * enumerators for these points, which are variables of sygus datatype type that
36
 * are introduced by CegisUnif.
37
 *
38
 * It maintains a set of guards, call them G_uq_1 ... G_uq_n, where the
39
 * semantics of G_uq_i is "for each type, the heads of evaluation points of that
40
 * type are interpreted as a value in a set whose cardinality is at most i".
41
 * We also enforce that the number of condition enumerators for evaluation
42
 * points is equal to (n-1).
43
 *
44
 * To enforce this, we introduce sygus enumerator(s) of the same type as the
45
 * heads of evaluation points and condition enumerators registered to this class
46
 * and add lemmas that enforce that these terms are equal to at least one
47
 * enumerator (see registerEvalPtAtSize).
48
 */
49
1529
class CegisUnifEnumDecisionStrategy : public DecisionStrategyFmf
50
{
51
 public:
52
  CegisUnifEnumDecisionStrategy(QuantifiersState& qs,
53
                                QuantifiersInferenceManager& qim,
54
                                TermDbSygus* tds,
55
                                SynthConjecture* parent);
56
  /** Make the n^th literal of this strategy (G_uq_n).
57
   *
58
   * This call may add new lemmas of the form described above
59
   * registerEvalPtAtValue on the output channel of d_qe.
60
   */
61
  Node mkLiteral(unsigned n) override;
62
  /** identify */
63
55423
  std::string identify() const override
64
  {
65
55423
    return std::string("cegis_unif_num_enums");
66
  }
67
68
  /** initialize candidates
69
   *
70
   * Notify this class that it will be managing enumerators for the vector
71
   * of strategy points es. This function should only be called once.
72
   *
73
   * Each strategy point in es should be such that we are using a
74
   * synthesis-by-unification approach for its candidate.
75
   */
76
  void initialize(const std::vector<Node>& es,
77
                  const std::map<Node, Node>& e_to_cond,
78
                  const std::map<Node, std::vector<Node>>& strategy_lemmas);
79
80
  /*
81
   * Do not hide the zero-argument version of initialize() inherited from the
82
   * base class
83
   */
84
  using DecisionStrategy::initialize;
85
86
  /** get the current set of enumerators for strategy point e
87
   *
88
   * Index 0 adds the set of return value enumerators to es, index 1 adds the
89
   * set of condition enumerators to es.
90
   */
91
  void getEnumeratorsForStrategyPt(Node e,
92
                                   std::vector<Node>& es,
93
                                   unsigned index) const;
94
  /** register evaluation point for candidate
95
   *
96
   * This notifies this class that eis is a set of heads of evaluation points
97
   * for strategy point e, where e was passed to initialize in the vector es.
98
   *
99
   * This may add new lemmas of the form described above
100
   * registerEvalPtAtSize on the output channel of d_qe.
101
   */
102
  void registerEvalPts(const std::vector<Node>& eis, Node e);
103
104
 private:
105
  /** Reference to the quantifiers inference manager */
106
  QuantifiersInferenceManager& d_qim;
107
  /** sygus term database of d_qe */
108
  TermDbSygus* d_tds;
109
  /** reference to the parent conjecture */
110
  SynthConjecture* d_parent;
111
  /**
112
   * Whether we are using condition pool enumeration (Section 4 of Barbosa et al
113
   * FMCAD 2019). This is determined by option::sygusUnifPi().
114
   */
115
  bool d_useCondPool;
116
  /** whether this module has been initialized */
117
  bool d_initialized;
118
  /** null node */
119
  Node d_null;
120
  /** information per initialized type */
121
9
  class StrategyPtInfo
122
  {
123
   public:
124
9
    StrategyPtInfo() {}
125
    /** strategy point for this type */
126
    Node d_pt;
127
    /** the set of enumerators we have allocated for this strategy point
128
     *
129
     * Index 0 stores the return value enumerators, and index 1 stores the
130
     * conditional enumerators. We have that
131
     *   d_enums[0].size()==d_enums[1].size()+1.
132
     */
133
    std::vector<Node> d_enums[2];
134
    /** the type of conditional enumerators for this strategy point  */
135
    TypeNode d_ce_type;
136
    /**
137
     * The set of evaluation points of this type. In models, we ensure that
138
     * each of these are equal to one of d_enums[0].
139
     */
140
    std::vector<Node> d_eval_points;
141
    /** symmetry breaking lemma template for this strategy point
142
     *
143
     * Each pair stores (the symmetry breaking lemma template, argument (to be
144
     * instantiated) of symmetry breaking lemma template).
145
     *
146
     * Index 0 stores the symmetry breaking lemma template for return values,
147
     * index 1 stores the template for conditions.
148
     */
149
    std::pair<Node, Node> d_sbt_lemma_tmpl[2];
150
  };
151
  /** map strategy points to the above info */
152
  std::map<Node, StrategyPtInfo> d_ce_info;
153
  /** the "virtual" enumerator
154
   *
155
   * This enumerator is used for enforcing fairness. In particular, we relate
156
   * its size to the number of conditions allocated by this class such that:
157
   *    ~G_uq_i => size(d_virtual_enum) >= floor( log2( i-1 ) )
158
   * In other words, if we are using (i-1) conditions in our solution,
159
   * the size of the virtual enumerator is at least the floor of the log (base
160
   * two) of (i-1). Due to the default fairness scheme in the quantifier-free
161
   * datatypes solver (if --sygus-fair-max is enabled), this ensures that other
162
   * enumerators are allowed to have at least this size. This affect other
163
   * fairness schemes in an analogous fashion. In particular, we enumerate
164
   * based on the tuples for (term size, #conditions):
165
   *   (0,0), (0,1)                                             [size 0]
166
   *   (0,2), (0,3), (1,1), (1,2), (1,3)                        [size 1]
167
   *   (0,4), ..., (0,7), (1,4), ..., (1,7), (2,0), ..., (2,7)  [size 2]
168
   *   (0,8), ..., (0,15), (1,8), ..., (1,15), ...              [size 3]
169
   */
170
  Node d_virtual_enum;
171
  /** Registers an enumerator and adds symmetry breaking lemmas
172
   *
173
   * The symmetry breaking lemmas are generated according to the stored
174
   * information from the enumerator's respective strategy point and whether it
175
   * is a condition or return value enumerator. For the latter we add symmetry
176
   * breaking lemmas that force enumerators to consider values in an increasing
177
   * order of size.
178
   */
179
  void setUpEnumerator(Node e, StrategyPtInfo& si, unsigned index);
180
  /** register evaluation point at size
181
   *
182
   * This sends a lemma of the form:
183
   *   G_uq_n => ei = d1 V ... V ei = dn
184
   * on the output channel of d_qe, where d1...dn are sygus enumerators of the
185
   * same type as e and ei, and ei is an evaluation point of strategy point e.
186
   */
187
  void registerEvalPtAtSize(Node e, Node ei, Node guq_lit, unsigned n);
188
};
189
190
/** Synthesizes functions in a data-driven SyGuS approach
191
 *
192
 * Data is derived from refinement lemmas generated through the regular CEGIS
193
 * approach. SyGuS is used to generate terms for classifying the data
194
 * (e.g. using decision tree learning) and thus generate a candidates for
195
 * functions-to-synthesize.
196
 *
197
 * This approach is inspired by the divide and conquer synthesis through
198
 * unification approach by Alur et al. TACAS 2017, by ICE-based invariant
199
 * synthesis from Garg et al. CAV 2014 and POPL 2016, and Padhi et al. PLDI 2016
200
 *
201
 * This module mantains a set of functions-to-synthesize and a set of term
202
 * enumerators. When new terms are enumerated it tries to learn new candidate
203
 * solutions, which are verified outside this module. If verification fails a
204
 * refinement lemma is generated, which this module sends to the utility that
205
 * learns candidates.
206
 */
207
class CegisUnif : public Cegis
208
{
209
 public:
210
  CegisUnif(QuantifiersState& qs,
211
            QuantifiersInferenceManager& qim,
212
            TermDbSygus* tds,
213
            SynthConjecture* p);
214
  ~CegisUnif() override;
215
  /** Retrieves enumerators for constructing solutions
216
   *
217
   * Non-unification candidates have themselves as enumerators, while for
218
   * unification candidates we add their conditonal enumerators to enums if
219
   * their respective guards are set in the current model
220
   */
221
  void getTermList(const std::vector<Node>& candidates,
222
                   std::vector<Node>& enums) override;
223
224
  /** Communicates refinement lemma to unification utility and external modules
225
   *
226
   * For the lemma to be sent to the external modules it adds a guard from the
227
   * parent conjecture which establishes that if the conjecture has a solution
228
   * then it must satisfy this refinement lemma
229
   *
230
   * For the lemma to be sent to the unification utility it purifies the
231
   * arguments of the function-to-synthensize such that all of its applications
232
   * are over concrete values. E.g.:
233
   *   f(f(f(0))) > 1
234
   * becomes
235
   *   f(0) != c1 v f(c1) != c2 v f(c2) > 1
236
   * in which c1 and c2 are concrete integer values
237
   *
238
   * Note that the lemma is in the deep embedding, which means that the above
239
   * example would actually correspond to
240
   *   eval(d, 0) != c1 v eval(d, c1) != c2 v eval(d, c2) > 1
241
   * in which d is the deep embedding of the function-to-synthesize f
242
   */
243
  void registerRefinementLemma(const std::vector<Node>& vars,
244
                               Node lem,
245
                               std::vector<Node>& lems) override;
246
247
 private:
248
  /** do cegis-implementation-specific initialization for this class */
249
  bool processInitialize(Node conj,
250
                         Node n,
251
                         const std::vector<Node>& candidates,
252
                         std::vector<Node>& lemmas) override;
253
  /** Tries to build new candidate solutions with new enumerated expressions
254
   *
255
   * This function relies on a data-driven unification-based approach for
256
   * constructing solutions for the functions-to-synthesize. See SygusUnifRl for
257
   * more details.
258
   *
259
   * Calls to this function are such that terms is the list of active
260
   * enumerators (returned by getTermList), and term_values are their current
261
   * model values. This function registers { terms -> terms_values } in
262
   * the database of values that have been enumerated, which are in turn used
263
   * for constructing candidate solutions when possible.
264
   *
265
   * This function also excludes models where (terms = terms_values) by adding
266
   * blocking clauses to lems. For example, for grammar:
267
   *   A -> A+A | x | 1 | 0
268
   * and a call where terms = { d } and term_values = { +( x, 1 ) }, it adds:
269
   *   ~G V ~is_+( d ) V ~is_x( d.1 ) V ~is_1( d.2 )
270
   * to lems, where G is active guard of the enumerator d (see
271
   * TermDatabaseSygus::getActiveGuardForEnumerator). This blocking clause
272
   * indicates that d should not be given the model value +( x, 1 ) anymore,
273
   * since { d -> +( x, 1 ) } has now been added to the database of this class.
274
   */
275
  bool processConstructCandidates(const std::vector<Node>& enums,
276
                                  const std::vector<Node>& enum_values,
277
                                  const std::vector<Node>& candidates,
278
                                  std::vector<Node>& candidate_values,
279
                                  bool satisfiedRl,
280
                                  std::vector<Node>& lems) override;
281
  /** communicate condition values to solution building utility
282
   *
283
   * for each unification candidate and for each strategy point associated with
284
   * it, set in d_sygus_unif the condition values (unif_cvalues) for respective
285
   * condition enumerators (unif_cenums)
286
   */
287
  void setConditions(const std::map<Node, std::vector<Node>>& unif_cenums,
288
                     const std::map<Node, std::vector<Node>>& unif_cvalues,
289
                     std::vector<Node>& lems);
290
  /** set values of condition enumerators based on current enumerator assignment
291
   *
292
   * enums and enum_values are the enumerators registered in getTermList and
293
   * their values retrieved by the parent SynthConjecture module, respectively.
294
   *
295
   * unif_cenums and unif_cvalues associate the conditional enumerators of each
296
   * strategy point of each unification candidate with their respective model
297
   * values
298
   *
299
   * This function also generates inter-enumerator symmetry breaking for return
300
   * values, such that their model values are ordered by size
301
   *
302
   * returns true if no symmetry breaking lemmas were generated for the return
303
   * value enumerators, false otherwise
304
   */
305
  bool getEnumValues(const std::vector<Node>& enums,
306
                     const std::vector<Node>& enum_values,
307
                     std::map<Node, std::vector<Node>>& unif_cenums,
308
                     std::map<Node, std::vector<Node>>& unif_cvalues,
309
                     std::vector<Node>& lems);
310
311
  /**
312
   * Whether we are using condition pool enumeration (Section 4 of Barbosa et al
313
   * FMCAD 2019). This is determined by option::sygusUnifPi().
314
   */
315
  bool usingConditionPool() const;
316
  /**
317
   * Sygus unif utility. This class implements the core algorithm (e.g. decision
318
   * tree learning) that this module relies upon.
319
   */
320
  SygusUnifRl d_sygus_unif;
321
  /** enumerator manager utility */
322
  CegisUnifEnumDecisionStrategy d_u_enum_manager;
323
  /* The null node */
324
  Node d_null;
325
  /** the unification candidates */
326
  std::vector<Node> d_unif_candidates;
327
  /** the non-unification candidates */
328
  std::vector<Node> d_non_unif_candidates;
329
  /** list of strategy points per candidate */
330
  std::map<Node, std::vector<Node>> d_cand_to_strat_pt;
331
  /** map from conditional enumerators to their strategy point */
332
  std::map<Node, Node> d_cenum_to_strat_pt;
333
}; /* class CegisUnif */
334
335
}  // namespace quantifiers
336
}  // namespace theory
337
}  // namespace cvc5
338
339
#endif