GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/term_database_sygus.h Lines: 5 5 100.0 %
Date: 2021-09-29 Branches: 5 8 62.5 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Andres Noetzli, 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
 * Term database sygus class.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__QUANTIFIERS__TERM_DATABASE_SYGUS_H
19
#define CVC5__THEORY__QUANTIFIERS__TERM_DATABASE_SYGUS_H
20
21
#include <unordered_set>
22
23
#include "expr/dtype.h"
24
#include "smt/env_obj.h"
25
#include "theory/quantifiers/extended_rewrite.h"
26
#include "theory/quantifiers/fun_def_evaluator.h"
27
#include "theory/quantifiers/sygus/sygus_eval_unfold.h"
28
#include "theory/quantifiers/sygus/sygus_explain.h"
29
#include "theory/quantifiers/sygus/type_info.h"
30
#include "theory/quantifiers/term_database.h"
31
32
namespace cvc5 {
33
namespace theory {
34
namespace quantifiers {
35
36
class SynthConjecture;
37
38
/** role for registering an enumerator */
39
enum EnumeratorRole
40
{
41
  /** The enumerator populates a pool of terms (e.g. for PBE). */
42
  ROLE_ENUM_POOL,
43
  /** The enumerator is the single solution of the problem. */
44
  ROLE_ENUM_SINGLE_SOLUTION,
45
  /**
46
   * The enumerator is part of the solution of the problem (e.g. multiple
47
   * functions to synthesize).
48
   */
49
  ROLE_ENUM_MULTI_SOLUTION,
50
  /** The enumerator must satisfy some set of constraints */
51
  ROLE_ENUM_CONSTRAINED,
52
};
53
std::ostream& operator<<(std::ostream& os, EnumeratorRole r);
54
55
// TODO :issue #1235 split and document this class
56
class TermDbSygus : protected EnvObj
57
{
58
 public:
59
  TermDbSygus(Env& env, QuantifiersState& qs);
60
2382
  ~TermDbSygus() {}
61
  /** Finish init, which sets the inference manager */
62
  void finishInit(QuantifiersInferenceManager* qim);
63
  /** Reset this utility */
64
  bool reset(Theory::Effort e);
65
  /** Identify this utility */
66
  std::string identify() const { return "TermDbSygus"; }
67
  /** register the sygus type
68
   *
69
   * This initializes this database for sygus datatype type tn. This may
70
   * throw an assertion failure if the sygus grammar has type errors. Otherwise,
71
   * after registering a sygus type, the query function getTypeInfo can be
72
   * called for tn.
73
   *
74
   * This method returns true if tn is a sygus datatype type and false
75
   * otherwise.
76
   */
77
  bool registerSygusType(TypeNode tn);
78
79
  //------------------------------utilities
80
  /** get the explanation utility */
81
13449
  SygusExplain* getExplain() { return d_syexp.get(); }
82
  /** (recursive) function evaluator utility */
83
408
  FunDefEvaluator* getFunDefEvaluator() { return d_funDefEval.get(); }
84
  /** evaluation unfolding utility */
85
186209
  SygusEvalUnfold* getEvalUnfold() { return d_eval_unfold.get(); }
86
  //------------------------------end utilities
87
88
  //------------------------------enumerators
89
  /**
90
   * Register a variable e that we will do enumerative search on.
91
   *
92
   * conj : the conjecture that the enumeration of e is for.
93
   *
94
   * f : the synth-fun that the enumeration of e is for.Notice that enumerator
95
   * e may not be one-to-one with f in synthesis-through-unification approaches
96
   * (e.g. decision tree construction for PBE synthesis).
97
   *
98
   * erole : the role of this enumerator (see definition of EnumeratorRole).
99
   * Depending on this and the policy for actively-generated enumerators
100
   * (--sygus-active-gen), the enumerator may be "actively-generated".
101
   * For example, if --sygus-active-gen=var-agnostic, then the enumerator will
102
   * only generate values whose variables are in canonical order (only x1-x2
103
   * and not x2-x1 will be generated, assuming x1 and x2 are in the same
104
   * "subclass", see getSubclassForVar).
105
   *
106
   * An "active guard" may be allocated by this method for e based on erole
107
   * and the policies for active generation.
108
   */
109
  void registerEnumerator(Node e,
110
                          Node f,
111
                          SynthConjecture* conj,
112
                          EnumeratorRole erole);
113
  /** is e an enumerator registered with this class? */
114
  bool isEnumerator(Node e) const;
115
  /** return the conjecture e is associated with */
116
  SynthConjecture* getConjectureForEnumerator(Node e) const;
117
  /** return the function-to-synthesize e is associated with */
118
  Node getSynthFunForEnumerator(Node e) const;
119
  /** get active guard for e */
120
  Node getActiveGuardForEnumerator(Node e) const;
121
  /** are we using symbolic constructors for enumerator e? */
122
  bool usingSymbolicConsForEnumerator(Node e) const;
123
  /** is this enumerator agnostic to variables? */
124
  bool isVariableAgnosticEnumerator(Node e) const;
125
  /** is this enumerator a "basic" enumerator.
126
   *
127
   * A basic enumerator is one that does not rely on the sygus extension of the
128
   * datatypes solver. Basic enumerators enumerate all concrete terms for their
129
   * type for a single abstract value.
130
   */
131
  bool isBasicEnumerator(Node e) const;
132
  /** is this a "passively-generated" enumerator?
133
   *
134
   * A "passively-generated" enumerator is one for which the terms it enumerates
135
   * are obtained by looking at its model value only. For passively-generated
136
   * enumerators, it is the responsibility of the user of that enumerator (say
137
   * a SygusModule) to block the current model value of it before asking for
138
   * another value. By default, the Cegis module uses passively-generated
139
   * enumerators and "conjecture-specific refinement" to rule out models
140
   * for passively-generated enumerators.
141
   *
142
   * On the other hand, an "actively-generated" enumerator is one for which the
143
   * terms it enumerates are not necessarily a subset of the model values the
144
   * enumerator takes. Actively-generated enumerators are centrally managed by
145
   * SynthConjecture. The user of actively-generated enumerators are prohibited
146
   * from influencing its model value. For example, conjecture-specific
147
   * refinement in Cegis is not applied to actively-generated enumerators.
148
   */
149
  bool isPassiveEnumerator(Node e) const;
150
  /** get all registered enumerators */
151
  void getEnumerators(std::vector<Node>& mts);
152
  /** Register symmetry breaking lemma
153
   *
154
   * This function registers lem as a symmetry breaking lemma template for
155
   * subterms of enumerator e. For more information on symmetry breaking
156
   * lemma templates, see datatypes/datatypes_sygus.h.
157
   *
158
   * tn : the (sygus datatype) type that lem applies to, i.e. the
159
   * type of terms that lem blocks models for,
160
   * sz : the minimum size of terms that the lem blocks,
161
   * isTempl : if this flag is false, then lem is a (concrete) lemma.
162
   * If this flag is true, then lem is a symmetry breaking lemma template
163
   * over x, where x is returned by the call to getFreeVar( tn, 0 ).
164
   */
165
  void registerSymBreakLemma(
166
      Node e, Node lem, TypeNode tn, unsigned sz, bool isTempl = true);
167
  /** Has symmetry breaking lemmas been added for any enumerator? */
168
  bool hasSymBreakLemmas(std::vector<Node>& enums) const;
169
  /** Get symmetry breaking lemmas
170
   *
171
   * Returns the set of symmetry breaking lemmas that have been registered
172
   * for enumerator e. It adds these to lemmas.
173
   */
174
  void getSymBreakLemmas(Node e, std::vector<Node>& lemmas) const;
175
  /** Get the type of term symmetry breaking lemma lem applies to */
176
  TypeNode getTypeForSymBreakLemma(Node lem) const;
177
  /** Get the minimum size of terms symmetry breaking lemma lem applies to */
178
  unsigned getSizeForSymBreakLemma(Node lem) const;
179
  /** Returns true if lem is a lemma template, false if lem is a lemma */
180
  bool isSymBreakLemmaTemplate(Node lem) const;
181
  /** Clear information about symmetry breaking lemmas for enumerator e */
182
  void clearSymBreakLemmas(Node e);
183
  //------------------------------end enumerators
184
185
  //-----------------------------conversion from sygus to builtin
186
  /** get free variable
187
   *
188
   * This class caches a list of free variables for each type, which are
189
   * used, for instance, for constructing canonical forms of terms with free
190
   * variables. This function returns the i^th free variable for type tn.
191
   * If useSygusType is true, then this function returns a variable of the
192
   * analog type for sygus type tn (see d_fv for details).
193
   */
194
  TNode getFreeVar(TypeNode tn, int i, bool useSygusType = false);
195
  /** get free variable and increment
196
   *
197
   * This function returns the next free variable for type tn, and increments
198
   * the counter in var_count for that type.
199
   */
200
  TNode getFreeVarInc(TypeNode tn,
201
                      std::map<TypeNode, int>& var_count,
202
                      bool useSygusType = false);
203
  /** returns true if n is a cached free variable (in d_fv). */
204
  bool isFreeVar(Node n) const;
205
  /** returns the identifier for a cached free variable. */
206
  size_t getFreeVarId(Node n) const;
207
  /** returns true if n has a cached free variable (in d_fv). */
208
  bool hasFreeVar(Node n);
209
  /** get sygus proxy variable
210
   *
211
   * Returns a fresh variable of type tn with the SygusPrintProxyAttribute set
212
   * to constant c. The type tn should be a sygus datatype type, and the type of
213
   * c should be the analog type of tn. The semantics of the returned node
214
   * is "the variable of sygus datatype tn that encodes constant c".
215
   */
216
  Node getProxyVariable(TypeNode tn, Node c);
217
  /** make generic
218
   *
219
   * This function returns a builtin term f( t1, ..., tn ) where f is the
220
   * builtin op of the sygus datatype constructor specified by arguments
221
   * dt and c. The mapping pre maps child indices to the term for that index
222
   * in the term we are constructing. That is, for each i = 1,...,n:
223
   *   If i is in the domain of pre, then ti = pre[i].
224
   *   If i is not in the domain of pre, then ti = d_fv[1][ var_count[Ti ] ],
225
   *     and var_count[Ti] is incremented.
226
   * If doBetaRed is true, then lambda operators are eagerly eliminated via
227
   * beta reduction.
228
   */
229
  Node mkGeneric(const DType& dt,
230
                 unsigned c,
231
                 std::map<TypeNode, int>& var_count,
232
                 std::map<int, Node>& pre,
233
                 bool doBetaRed = true);
234
  /** same as above, but with empty var_count */
235
  Node mkGeneric(const DType& dt,
236
                 int c,
237
                 std::map<int, Node>& pre,
238
                 bool doBetaRed = true);
239
  /** same as above, but with empty pre */
240
  Node mkGeneric(const DType& dt, int c, bool doBetaRed = true);
241
  /** makes a symbolic term concrete
242
   *
243
   * Given a sygus datatype term n of type tn with holes (symbolic constructor
244
   * applications), this function returns a term in which holes are replaced by
245
   * unique variables. To track counters for introducing unique variables, we
246
   * use the var_count map.
247
   */
248
  Node canonizeBuiltin(Node n);
249
  Node canonizeBuiltin(Node n, std::map<TypeNode, int>& var_count);
250
  /** sygus to builtin
251
   *
252
   * Given a sygus datatype term n of type tn, this function returns its analog,
253
   * that is, the term that n encodes.
254
   */
255
  Node sygusToBuiltin(Node n, TypeNode tn);
256
  /** same as above, but without tn */
257
9502
  Node sygusToBuiltin(Node n) { return sygusToBuiltin(n, n.getType()); }
258
  /** evaluate builtin
259
   *
260
   * bn is a term of some sygus datatype tn. This function returns the rewritten
261
   * form of bn [ args / vars(tn) ], where vars(tn) is the sygus variable
262
   * list for type tn (see Datatype::getSygusVarList).
263
   *
264
   * If the argument tryEval is true, we consult the evaluator before the
265
   * rewriter, for performance reasons.
266
   */
267
  Node evaluateBuiltin(TypeNode tn,
268
                       Node bn,
269
                       const std::vector<Node>& args,
270
                       bool tryEval = true);
271
  /** is evaluation point?
272
   *
273
   * Returns true if n is of the form eval( x, c1...cn ) for some variable x
274
   * and constants c1...cn.
275
   */
276
  bool isEvaluationPoint(Node n) const;
277
  /** return the builtin type of tn
278
   *
279
   * The type tn should be a sygus datatype type that has been registered to
280
   * this database.
281
   */
282
  TypeNode sygusToBuiltinType(TypeNode tn);
283
  //-----------------------------end conversion from sygus to builtin
284
  /**
285
   * Get type information about sygus datatype type tn. The type tn should be
286
   * (a subfield type of) a type that has been registered to this class.
287
   */
288
  SygusTypeInfo& getTypeInfo(TypeNode tn);
289
  /**
290
   * Rewrite the given node using the utilities in this class. This may
291
   * involve (recursive function) evaluation.
292
   */
293
  Node rewriteNode(Node n) const;
294
295
  /** print to sygus stream n on trace c */
296
  static void toStreamSygus(const char* c, Node n);
297
  /** print to sygus stream n on output out */
298
  static void toStreamSygus(std::ostream& out, Node n);
299
300
 private:
301
  /** Reference to the quantifiers state */
302
  QuantifiersState& d_qstate;
303
  /** Pointer to the quantifiers inference manager */
304
  QuantifiersInferenceManager* d_qim;
305
306
  //------------------------------utilities
307
  /** sygus explanation */
308
  std::unique_ptr<SygusExplain> d_syexp;
309
  /** (recursive) function evaluator utility */
310
  std::unique_ptr<FunDefEvaluator> d_funDefEval;
311
  /** evaluation function unfolding utility */
312
  std::unique_ptr<SygusEvalUnfold> d_eval_unfold;
313
  //------------------------------end utilities
314
315
  //------------------------------enumerators
316
  /** mapping from enumerator terms to the conjecture they are associated with
317
   */
318
  std::map<Node, SynthConjecture*> d_enum_to_conjecture;
319
  /** mapping from enumerator terms to the function-to-synthesize they are
320
   * associated with
321
   */
322
  std::map<Node, Node> d_enum_to_synth_fun;
323
  /** mapping from enumerator terms to the guard they are associated with
324
   * The guard G for an enumerator e has the semantics
325
   *   if G is true, then there are more values of e to enumerate".
326
   */
327
  std::map<Node, Node> d_enum_to_active_guard;
328
  /**
329
   * Mapping from enumerators to whether we allow symbolic constructors to
330
   * appear as subterms of them.
331
   */
332
  std::map<Node, bool> d_enum_to_using_sym_cons;
333
  /** mapping from enumerators to symmetry breaking clauses for them */
334
  std::map<Node, std::vector<Node> > d_enum_to_sb_lemmas;
335
  /** mapping from symmetry breaking lemmas to type */
336
  std::map<Node, TypeNode> d_sb_lemma_to_type;
337
  /** mapping from symmetry breaking lemmas to size */
338
  std::map<Node, unsigned> d_sb_lemma_to_size;
339
  /** mapping from symmetry breaking lemmas to whether they are templates */
340
  std::map<Node, bool> d_sb_lemma_to_isTempl;
341
  /** enumerators to whether they are actively-generated */
342
  std::map<Node, bool> d_enum_active_gen;
343
  /** enumerators to whether they are variable agnostic */
344
  std::map<Node, bool> d_enum_var_agnostic;
345
  /** enumerators to whether they are basic */
346
  std::map<Node, bool> d_enum_basic;
347
  //------------------------------end enumerators
348
349
  //-----------------------------conversion from sygus to builtin
350
  /** a cache of fresh variables for each type
351
   *
352
   * We store two versions of this list:
353
   *   index 0: mapping from builtin types to fresh variables of that type,
354
   *   index 1: mapping from sygus types to fresh varaibles of the type they
355
   *            encode.
356
   */
357
  std::map<TypeNode, std::vector<Node> > d_fv[2];
358
  /** Maps free variables to the domain type they are associated with in d_fv */
359
  std::map<Node, TypeNode> d_fv_stype;
360
  /** Id count for free variables terms */
361
  std::map<TypeNode, size_t> d_fvTypeIdCounter;
362
  /**
363
   * Maps free variables to a unique identifier for their builtin type. Notice
364
   * that, e.g. free variables of type Int and those that are of a sygus
365
   * datatype type that encodes Int must have unique identifiers. This is
366
   * to ensure that sygusToBuiltin for non-ground terms maps variables to
367
   * unique variabales.
368
   */
369
  std::map<Node, size_t> d_fvId;
370
  /** recursive helper for hasFreeVar, visited stores nodes we have visited. */
371
  bool hasFreeVar(Node n, std::map<Node, bool>& visited);
372
  /** cache of getProxyVariable */
373
  std::map<TypeNode, std::map<Node, Node> > d_proxy_vars;
374
  //-----------------------------end conversion from sygus to builtin
375
  // TODO :issue #1235 : below here needs refactor
376
 public:
377
  Node d_true;
378
  Node d_false;
379
380
 private:
381
  /** computes the map d_min_type_depth */
382
  void computeMinTypeDepthInternal( TypeNode root_tn, TypeNode tn, unsigned type_depth );
383
  bool involvesDivByZero( Node n, std::map< Node, bool >& visited );
384
385
 private:
386
  /**
387
   * Maps types that we have called registerSygusType to a flag indicating
388
   * whether that type is a sygus datatype type. Sygus datatype types that
389
   * are in this map have initialized type information stored in the map below.
390
   */
391
  std::map<TypeNode, bool> d_registerStatus;
392
  /**
393
   * The type information for each sygus datatype type that has been registered
394
   * to this class.
395
   */
396
  std::map<TypeNode, SygusTypeInfo> d_tinfo;
397
  /** a cache for getSelectorWeight */
398
  std::map<TypeNode, std::map<Node, unsigned> > d_sel_weight;
399
400
 public:  // general sygus utilities
401
  bool isRegistered(TypeNode tn) const;
402
  /** get the weight of the selector, where tn is the domain of sel */
403
  unsigned getSelectorWeight(TypeNode tn, Node sel);
404
  /** get arg type */
405
  TypeNode getArgType(const DTypeConstructor& c, unsigned i) const;
406
  /** Do constructors c1 and c2 have the same type? */
407
  bool isTypeMatch(const DTypeConstructor& c1, const DTypeConstructor& c2);
408
  /** return whether n is an application of a symbolic constructor */
409
  bool isSymbolicConsApp(Node n) const;
410
  /** can construct kind
411
   *
412
   * Given a sygus datatype type tn, if this method returns true, then there
413
   * exists values of tn whose builtin analog is equivalent to
414
   * <k>( t1, ..., tn ). The sygus types of t1...tn are added to arg_types.
415
   *
416
   * For example, if:
417
   *   A -> A+A | ite( B, A, A ) | x | 1 | 0
418
   *   B -> and( B, B ) | not( B ) | or( B, B ) | A = A
419
   * - canConstructKind( A, +, ... ) returns true and adds {A,A} to arg_types,
420
   * - canConstructKind( B, not, ... ) returns true and adds { B } to arg types.
421
   *
422
   * We also may infer that operator is constructable. For example,
423
   * - canConstructKind( B, ite, ... ) may return true, adding { B, B, B } to
424
   * arg_types, noting that the term
425
   *   (and (or (not b1) b2) (or b1 b3)) is equivalent to (ite b1 b2 b3)
426
   * The argument aggr is whether we use aggressive techniques like the one
427
   * above to infer a kind is constructable. If this flag is false, we only
428
   * check if the kind is literally a constructor of the grammar.
429
   */
430
  bool canConstructKind(TypeNode tn,
431
                        Kind k,
432
                        std::vector<TypeNode>& argts,
433
                        bool aggr = false);
434
435
  Node getSygusNormalized( Node n, std::map< TypeNode, int >& var_count, std::map< Node, Node >& subs );
436
  Node getNormalized(TypeNode t, Node prog);
437
  /** involves div-by-zero */
438
  bool involvesDivByZero( Node n );
439
  /** get anchor */
440
  static Node getAnchor( Node n );
441
  static unsigned getAnchorDepth( Node n );
442
};
443
444
}  // namespace quantifiers
445
}  // namespace theory
446
}  // namespace cvc5
447
448
#endif /* CVC5__THEORY__QUANTIFIERS__TERM_DATABASE_H */