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

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Mathias Preiner, Fabian Wolff
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
 * sygus_sampler
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__QUANTIFIERS__SYGUS_SAMPLER_H
19
#define CVC5__THEORY__QUANTIFIERS__SYGUS_SAMPLER_H
20
21
#include <map>
22
#include "theory/quantifiers/lazy_trie.h"
23
#include "theory/quantifiers/term_enumeration.h"
24
25
namespace cvc5 {
26
27
class Env;
28
29
namespace theory {
30
namespace quantifiers {
31
32
class TermDbSygus;
33
34
/** SygusSampler
35
 *
36
 * This class can be used to test whether two expressions are equivalent
37
 * by random sampling. We use this class for the following options:
38
 *
39
 * sygus-rr-synth: synthesize candidate rewrite rules by finding two terms
40
 * t1 and t2 do not rewrite to the same term, but are identical when considering
41
 * a set of sample points, and
42
 *
43
 * sygus-rr-verify: detect unsound rewrite rules by finding two terms t1 and
44
 * t2 that rewrite to the same term, but not identical when considering a set
45
 * of sample points.
46
 *
47
 * To use this class:
48
 * (1) Call initialize( tds, f, nsamples) where f is sygus datatype term.
49
 * (2) For terms n1....nm enumerated that correspond to builtin analog of sygus
50
 * term f, we call registerTerm( n1 )....registerTerm( nm ). It may be the case
51
 * that registerTerm( ni ) returns nj for some j < i. In this case, we have that
52
 * ni and nj are equivalent under the sample points in this class.
53
 *
54
 * For example, say the grammar for f is:
55
 *   A = 0 | 1 | x | y | A+A | ite( B, A, A )
56
 *   B = A <= A
57
 * If we call initialize( tds, f, 5 ), this class will generate 5 random sample
58
 * points for (x,y), say (0,0), (1,1), (0,1), (1,0), (2,2). The return values
59
 * of successive calls to registerTerm are listed below.
60
 *   registerTerm( 0 ) -> 0
61
 *   registerTerm( x ) -> x
62
 *   registerTerm( x+y ) -> x+y
63
 *   registerTerm( y+x ) -> x+y
64
 *   registerTerm( x+ite(x <= 1+1, 0, y ) ) -> x
65
 * Notice that the number of sample points can be configured for the above
66
 * options using sygus-samples=N.
67
 */
68
class SygusSampler : public LazyTrieEvaluator
69
{
70
 public:
71
  SygusSampler(Env& env);
72
3702
  ~SygusSampler() override {}
73
74
  /** initialize
75
   *
76
   * tn : the return type of terms we will be testing with this class,
77
   * vars : the variables we are testing substitutions for,
78
   * nsamples : number of sample points this class will test,
79
   * unique_type_ids : if this is set to true, then we consider each variable
80
   * in vars to have a unique "type id". A type id is a finer-grained notion of
81
   * type that is used to determine when a rewrite rule is redundant.
82
   */
83
  virtual void initialize(TypeNode tn,
84
                          const std::vector<Node>& vars,
85
                          unsigned nsamples,
86
                          bool unique_type_ids = false);
87
  /** initialize sygus
88
   *
89
   * qe : pointer to quantifiers engine,
90
   * f : a term of some SyGuS datatype type whose values we will be
91
   * testing under the free variables in the grammar of f,
92
   * nsamples : number of sample points this class will test,
93
   * useSygusType : whether we will register terms with this sampler that have
94
   * the same type as f. If this flag is false, then we will be registering
95
   * terms of the analog of the type of f, that is, the builtin type that
96
   * f's type encodes in the deep embedding.
97
   */
98
  virtual void initializeSygus(TermDbSygus* tds,
99
                               Node f,
100
                               unsigned nsamples,
101
                               bool useSygusType);
102
  /** register term n with this sampler database
103
   *
104
   * forceKeep is whether we wish to force that n is chosen as a representative
105
   * value in the trie.
106
   */
107
  virtual Node registerTerm(Node n, bool forceKeep = false);
108
  /** get number of sample points */
109
1078
  unsigned getNumSamplePoints() const { return d_samples.size(); }
110
  /** get variables, adds d_vars to vars */
111
  void getVariables(std::vector<Node>& vars) const;
112
  /** get sample point
113
   *
114
   * Appends sample point #index to the vector pt, d_vars to vars.
115
   */
116
  void getSamplePoint(unsigned index,
117
                      std::vector<Node>& pt);
118
  /** Add pt to the set of sample points considered by this sampler */
119
  void addSamplePoint(std::vector<Node>& pt);
120
  /** evaluate n on sample point index */
121
  Node evaluate(Node n, unsigned index) override;
122
  /**
123
   * Compute the variables from the domain of d_var_index that occur in n,
124
   * store these in the vector fvs.
125
   */
126
  void computeFreeVariables(Node n, std::vector<Node>& fvs);
127
  /**
128
   * Returns the index of a sample point such that the evaluation of a and b
129
   * diverge, or -1 if no such sample point exists.
130
   */
131
  int getDiffSamplePointIndex(Node a, Node b);
132
133
  //--------------------------queries about terms
134
  /** is contiguous
135
   *
136
   * This returns whether n's free variables (terms occurring in the range of
137
   * d_type_vars) are a prefix of the list of variables in d_type_vars for each
138
   * type id. For instance, if d_type_vars[id] = { x, y } for some id, then
139
   * 0, x, x+y, y+x are contiguous but y is not. This is useful for excluding
140
   * terms from consideration that are alpha-equivalent to others.
141
   */
142
  bool isContiguous(Node n);
143
  /** is ordered
144
   *
145
   * This returns whether n's free variables are in order with respect to
146
   * variables in d_type_vars for each type. For instance, if
147
   * d_type_vars[id] = { x, y } for some id, then 0, x, x+y are ordered but
148
   * y and y+x are not.
149
   */
150
  bool isOrdered(Node n);
151
  /** is linear
152
   *
153
   * This returns whether n contains at most one occurrence of each free
154
   * variable. For example, x, x+y are linear, but x+x, (x-y)+y, (x+0)+(x+0) are
155
   * non-linear.
156
   */
157
  bool isLinear(Node n);
158
  /** check variables
159
   *
160
   * This returns false if !isOrdered(n) and checkOrder is true or !isLinear(n)
161
   * if checkLinear is true, or false otherwise.
162
   */
163
  bool checkVariables(Node n, bool checkOrder, bool checkLinear);
164
  /** contains free variables
165
   *
166
   * Returns true if the free variables of b are a subset of those in a, where
167
   * we require a strict subset if strict is true. Free variables are those that
168
   * occur in the range d_type_vars.
169
   */
170
  bool containsFreeVariables(Node a, Node b, bool strict = false);
171
  //--------------------------end queries about terms
172
  /** check equivalent
173
   *
174
   * Check whether bv and bvr are equivalent on all sample points, print
175
   * an error if not. Used with --sygus-rr-verify.
176
   *
177
   * @param bv The original term
178
   * @param bvr The rewritten form of bvr
179
   * @param out The output stream to write if the rewrite was unsound.
180
   */
181
  void checkEquivalent(Node bv, Node bvr, std::ostream& out);
182
183
 protected:
184
  /** The environment we are using to evaluate terms and samples */
185
  Env& d_env;
186
  /** sygus term database of d_qe */
187
  TermDbSygus* d_tds;
188
  /** term enumerator object (used for random sampling) */
189
  TermEnumeration d_tenum;
190
  /** samples */
191
  std::vector<std::vector<Node> > d_samples;
192
  /** data structure to check duplication of sample points */
193
252774
  class PtTrie
194
  {
195
   public:
196
    /** add pt to this trie, returns true if pt is not a duplicate. */
197
    bool add(std::vector<Node>& pt);
198
199
   private:
200
    /** the children of this node */
201
    std::map<Node, PtTrie> d_children;
202
  };
203
  /** a trie for samples */
204
  PtTrie d_samples_trie;
205
  /** the sygus type for this sampler (if applicable). */
206
  TypeNode d_ftn;
207
  /** whether we are registering terms of sygus types with this sampler */
208
  bool d_use_sygus_type;
209
  /**
210
   * For each (sygus) type, a map from builtin terms to the sygus term they
211
   * correspond to.
212
   */
213
  std::map<TypeNode, std::map<Node, Node> > d_builtin_to_sygus;
214
  /** all variables we are sampling values for */
215
  std::vector<Node> d_vars;
216
  /** type variables
217
   *
218
   * We group variables according to "type ids". Two variables have the same
219
   * type id if they have indistinguishable status according to this sampler.
220
   * This is a finer-grained grouping than types. For example, two variables
221
   * of the same type may have different type ids if they occur as constructors
222
   * of a different set of sygus types in the grammar we are considering.
223
   * For instance, we assign x and y different type ids in this grammar:
224
   *   A -> B + C
225
   *   B -> x | 0 | 1
226
   *   C -> y
227
   * Type ids are computed for each variable in d_vars during initialize(...).
228
   *
229
   * For each type id, a list of variables in the grammar we are considering,
230
   * for that type. These typically correspond to the arguments of the
231
   * function-to-synthesize whose grammar we are considering.
232
   */
233
  std::map<unsigned, std::vector<Node> > d_type_vars;
234
  /**
235
   * A map all variables in the grammar we are considering to their index in
236
   * d_type_vars.
237
   */
238
  std::map<Node, unsigned> d_var_index;
239
  /**  Map from variables to the id (the domain of d_type_vars). */
240
  std::map<Node, unsigned> d_type_ids;
241
  /** constants
242
   *
243
   * For each type, a list of constants in the grammar we are considering, for
244
   * that type.
245
   */
246
  std::map<TypeNode, std::vector<Node> > d_type_consts;
247
  /** a lazy trie for each type
248
   *
249
   * This stores the evaluation of all terms registered to this class.
250
   *
251
   * Notice if we are registering sygus terms with this class, then terms
252
   * are grouped into this trie according to their sygus type, and not their
253
   * builtin type. For example, for grammar:
254
   *   A -> x | B+1
255
   *   B -> x | 0 | 1 | B+B
256
   * If we register C^B_+( C^B_x(), C^B_0() ) and C^A_x() with this class,
257
   * then x+0 is registered to d_trie[B] and x is registered to d_trie[A],
258
   * and no rewrite rule is reported. The reason for this is that otherwise
259
   * we would end up reporting many useless rewrites since the same builtin
260
   * term can be generated by multiple sygus types (e.g. C^B_x() and C^A_x()).
261
   */
262
  std::map<TypeNode, LazyTrie> d_trie;
263
  /** is this sampler valid?
264
   *
265
   * A sampler can be invalid if sample points cannot be generated for a type
266
   * of an argument to function f.
267
   */
268
  bool d_is_valid;
269
  /** initialize samples
270
   *
271
   * Adds nsamples sample points to d_samples.
272
   */
273
  void initializeSamples(unsigned nsamples);
274
  /** get random value for a type
275
   *
276
   * Returns a random value for the given type based on the random number
277
   * generator. Currently, supported types:
278
   *
279
   * Bool, Bitvector : returns a random value in the range of that type.
280
   * Int, String : returns a random string of values in (base 10) of random
281
   * length, currently by a repeated coin flip.
282
   * Real : returns the division of two random integers, where the denominator
283
   * is omitted if it is zero.
284
   */
285
  Node getRandomValue(TypeNode tn);
286
  /** get sygus random value
287
   *
288
   * Returns a random value based on the sygus type tn. The return value is
289
   * a constant in the analog type of tn. This function chooses either to
290
   * return a random value, or otherwise will construct a constant based on
291
   * a random constructor of tn whose builtin operator is not a variable.
292
   *
293
   * rchance: the chance that the call to this function will be forbidden
294
   * from making recursive calls and instead must return a value based on
295
   * a nullary constructor of tn or based on getRandomValue above.
296
   * rinc: the percentage to increment rchance on recursive calls.
297
   *
298
   * For example, consider the grammar:
299
   *   A -> x | y | 0 | 1 | +( A, A ) | ite( B, A, A )
300
   *   B -> A = A
301
   * If we call this function on A and rchance is 0.0, there are five evenly
302
   * chosen possibilities, either we return a random value via getRandomValue
303
   * above, or we choose one of the four non-variable constructors of A.
304
   * Say we choose ite, then we recursively call this function for
305
   * B, A, and A, which return constants c1, c2, and c3. Then, this function
306
   * returns the rewritten form of ite( c1, c2, c3 ).
307
   * If on the other hand, rchance was 0.5 and rand() < 0.5. Then, we force
308
   * this call to terminate by either selecting a random value via
309
   * getRandomValue, 0 or 1.
310
   */
311
  Node getSygusRandomValue(TypeNode tn,
312
                           double rchance,
313
                           double rinc,
314
                           unsigned depth = 0);
315
  /** map from sygus types to non-variable constructors */
316
  std::map<TypeNode, std::vector<unsigned> > d_rvalue_cindices;
317
  /** map from sygus types to non-variable nullary constructors */
318
  std::map<TypeNode, std::vector<unsigned> > d_rvalue_null_cindices;
319
  /** the random string alphabet */
320
  std::vector<unsigned> d_rstring_alphabet;
321
  /** map from variables to sygus types that include them */
322
  std::map<Node, std::vector<TypeNode> > d_var_sygus_types;
323
  /** map from constants to sygus types that include them */
324
  std::map<Node, std::vector<TypeNode> > d_const_sygus_types;
325
  /** register sygus type, initializes the above two data structures */
326
  void registerSygusType(TypeNode tn);
327
};
328
329
}  // namespace quantifiers
330
}  // namespace theory
331
}  // namespace cvc5
332
333
#endif /* CVC5__THEORY__QUANTIFIERS__SYGUS_SAMPLER_H */