GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/datatypes/sygus_extension.h Lines: 5 5 100.0 %
Date: 2021-09-29 Branches: 1 2 50.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Mathias Preiner, Gereon Kremer
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
 * The sygus extension of the theory of datatypes.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__DATATYPES__SYGUS_EXTENSION_H
19
#define CVC5__THEORY__DATATYPES__SYGUS_EXTENSION_H
20
21
#include <iostream>
22
#include <map>
23
24
#include "context/cdhashmap.h"
25
#include "context/cdhashset.h"
26
#include "context/context.h"
27
#include "expr/node.h"
28
#include "smt/env_obj.h"
29
#include "theory/datatypes/sygus_simple_sym.h"
30
#include "theory/decision_manager.h"
31
#include "theory/quantifiers/sygus_sampler.h"
32
#include "theory/quantifiers/term_database.h"
33
34
namespace cvc5 {
35
namespace theory {
36
namespace quantifiers {
37
class SynthConjecture;
38
}
39
namespace datatypes {
40
41
class InferenceManager;
42
43
/**
44
 * This is the sygus extension of the decision procedure for quantifier-free
45
 * inductive datatypes. At a high level, this class takes as input a
46
 * set of asserted testers is-C1( x ), is-C2( x.1 ), is-C3( x.2 ), and
47
 * generates lemmas that restrict the models of x, if x is a "sygus enumerator"
48
 * (see TermDbSygus::registerEnumerator).
49
 *
50
 * Some of these techniques are described in these papers:
51
 * "Refutation-Based Synthesis in SMT", Reynolds et al 2017.
52
 * "Sygus Techniques in the Core of an SMT Solver", Reynolds et al 2017.
53
 *
54
 * This class enforces two decisions stragies via calls to registerStrategy
55
 * of the owning theory's DecisionManager:
56
 * (1) Positive decisions on the active guards G of enumerators e registered
57
 * to this class. These assert "there are more values to enumerate for e".
58
 * (2) Positive bounds (DT_SYGUS_BOUND m n) for "measure terms" m (see below),
59
 * where n is a non-negative integer. This asserts "the measure of terms
60
 * we are enumerating for enumerators whose measure term m is at most n",
61
 * where measure is commonly term size, but can also be height.
62
 *
63
 * We prioritize decisions of form (1) before (2). Both kinds of decision are
64
 * critical for solution completeness, which is enforced by DecisionManager.
65
 */
66
class SygusExtension : protected EnvObj
67
{
68
  typedef context::CDHashMap<Node, int> IntMap;
69
  typedef context::CDHashMap<Node, Node> NodeMap;
70
  typedef context::CDHashMap<Node, bool> BoolMap;
71
  typedef context::CDHashSet<Node> NodeSet;
72
73
 public:
74
  SygusExtension(Env& env,
75
                 TheoryState& s,
76
                 InferenceManager& im,
77
                 quantifiers::TermDbSygus* tds);
78
  ~SygusExtension();
79
  /**
80
   * Notify this class that tester for constructor tindex has been asserted for
81
   * n. Exp is the literal corresponding to this tester. This method may send
82
   * lemmas via inference manager, for details see assertTesterInternal below.
83
   * These lemmas are sent out on the output channel of datatypes by the caller.
84
   */
85
  void assertTester(int tindex, TNode n, Node exp);
86
  /**
87
   * Notify this class that literal n has been asserted with the given
88
   * polarity. This method may send lemmas via inference manager, for instance
89
   * based on inferring consequences of (not) n. One example is if n is
90
   * (DT_SIZE_BOUND x n), we add the lemma:
91
   *   (DT_SIZE_BOUND x n) <=> ((DT_SIZE x) <= n )
92
   */
93
  void assertFact(Node n, bool polarity);
94
  /** pre-register term n
95
   *
96
   * This is called when n is pre-registered with the theory of datatypes.
97
   * If n is a sygus enumerator, then we may send lemmas via inference manager
98
   * that are used to enforce fairness regarding the size of n.
99
   */
100
  void preRegisterTerm(TNode n);
101
  /** check
102
   *
103
   * This is called at last call effort, when the current model assignment is
104
   * satisfiable according to the quantifier-free decision procedures and a
105
   * model is built. This method may send lemmas via inference manager based
106
   * on dynamic symmetry breaking techniques, based on the model values of
107
   * all preregistered enumerators.
108
   */
109
  void check();
110
111
 private:
112
  /** The theory state of the datatype theory */
113
  TheoryState& d_state;
114
  /** The inference manager of the datatype theory */
115
  InferenceManager& d_im;
116
  /** Pointer to the sygus term database */
117
  quantifiers::TermDbSygus* d_tds;
118
  /** the simple symmetry breaking utility */
119
  SygusSimpleSymBreak d_ssb;
120
  /**
121
   * Map from terms to the index of the tester that is asserted for them in
122
   * the current SAT context. In other words, if d_testers[n] = 2, then the
123
   * tester is-C_2(n) is asserted in this SAT context.
124
   */
125
  IntMap d_testers;
126
  /**
127
   * Map from terms to the tester asserted for them. In the example above,
128
   * d_testers[n] = is-C_2(n).
129
   */
130
  NodeMap d_testers_exp;
131
  /**
132
   * The set of (selector chain) terms that are active in the current SAT
133
   * context. A selector chain term S_n( ... S_1( x )... ) is active if either:
134
   * (1) n=0 and x is a sygus enumerator,
135
   *   or:
136
   * (2.1) S_{n-1}( ... S_1( x )) is active,
137
   * (2.2) is-C( S_{n-1}( ... S_1( x )) ) is asserted in this SAT context, and
138
   * (2.3) S_n is a selector for constructor C.
139
   */
140
  NodeSet d_active_terms;
141
  /**
142
   * Map from enumerators to a lower bound on their size in the current SAT
143
   * context.
144
   */
145
  IntMap d_currTermSize;
146
  /** zero */
147
  Node d_zero;
148
  /** true */
149
  Node d_true;
150
  /**
151
   * Map from terms (selector chains) to their anchors. The anchor of a
152
   * selector chain S1( ... Sn( x ) ... ) is x.
153
   */
154
  std::unordered_map<Node, Node> d_term_to_anchor;
155
  /**
156
   * Map from anchors to the conjecture they are associated with.
157
   */
158
  std::map<Node, quantifiers::SynthConjecture*> d_anchor_to_conj;
159
  /**
160
   * Map from terms (selector chains) to their depth. The depth of a selector
161
   * chain S1( ... Sn( x ) ... ) is:
162
   *   weight( S1 ) + ... + weight( Sn ),
163
   * where weight is the selector weight of Si
164
   * (see SygusTermDatabase::getSelectorWeight).
165
   */
166
  std::unordered_map<Node, unsigned> d_term_to_depth;
167
  /**
168
   * Map from terms (selector chains) to whether they are the topmost term
169
   * of their type. For example, if:
170
   *   S1 : T1 -> T2
171
   *   S2 : T2 -> T2
172
   *   S3 : T2 -> T1
173
   *   S4 : T1 -> T3
174
   * Then, x, S1( x ), and S4( S3( S2( S1( x ) ) ) ) are top-level terms,
175
   * whereas S2( S1( x ) ) and S3( S2( S1( x ) ) ) are not.
176
   */
177
  std::unordered_map<Node, bool> d_is_top_level;
178
  /**
179
   * Returns true if the selector chain n is top-level based on the above
180
   * definition, when tn is the type of n.
181
   */
182
  bool computeTopLevel( TypeNode tn, Node n );
183
private:
184
 /** This caches all information regarding symmetry breaking for an anchor. */
185
358
 class SearchCache
186
 {
187
  public:
188
358
    SearchCache(){}
189
    /**
190
     * A cache of all search terms for (types, sizes). See registerSearchTerm
191
     * for definition of search terms.
192
     */
193
    std::map< TypeNode, std::map< unsigned, std::vector< Node > > > d_search_terms;
194
    /** A cache of all symmetry breaking lemma templates for (types, sizes). */
195
    std::map<TypeNode, std::map<uint64_t, std::vector<Node>>> d_sbLemmas;
196
    /** search value
197
     *
198
     * For each sygus type, a map from a builtin term to a sygus term for that
199
     * type that we encountered during the search whose analog rewrites to that
200
     * term. The range of this map can be updated if we later encounter a sygus
201
     * term that also rewrites to the builtin value but has a smaller term size.
202
     */
203
    std::map<TypeNode, std::unordered_map<Node, Node>> d_search_val;
204
    /** the size of terms in the range of d_search val. */
205
    std::map<TypeNode, std::unordered_map<Node, unsigned>> d_search_val_sz;
206
    /** For each term, whether this cache has processed that term */
207
    std::unordered_set<Node> d_search_val_proc;
208
  };
209
  /** An instance of the above cache, for each anchor */
210
  std::map< Node, SearchCache > d_cache;
211
  //-----------------------------------traversal predicates
212
  /** pre/post traversal predicates for each type, variable
213
   *
214
   * This stores predicates (pre, post) whose semantics correspond to whether
215
   * a variable has occurred by a (pre, post) traversal of a symbolic term,
216
   * where index = 0 corresponds to pre, index = 1 corresponds to post. For
217
   * details, see getTraversalPredicate below.
218
   */
219
  std::map<TypeNode, std::map<Node, Node>> d_traversal_pred[2];
220
  /** traversal applications to Boolean variables
221
   *
222
   * This maps each application of a traversal predicate pre_x( t ) or
223
   * post_x( t ) to a fresh Boolean variable.
224
   */
225
  std::map<Node, Node> d_traversal_bool;
226
  /** get traversal predicate
227
   *
228
   * Get the predicates (pre, post) whose semantics correspond to whether
229
   * a variable has occurred by this point in a (pre, post) traversal of a term.
230
   * The type of getTraversalPredicate(tn, n, _) is tn -> Bool.
231
   *
232
   * For example, consider the term:
233
   *   f( x_1, g( x_2, x_3 ) )
234
   * and a left-to-right, depth-first traversal of this term. Let e be
235
   * a variable of the same type as this term. We say that for the above term:
236
   *   pre_{x_1} is false for e, e.1 and true for e.2, e.2.1, e.2.2
237
   *   pre_{x_2} is false for e, e.1, e.2, e.2.1, and true for e.2.2
238
   *   pre_{x_3} is false for e, e.1, e.2, e.2.1, e.2.2
239
   *   post_{x_1} is true for e.1, e.2.1, e.2.2, e.2, e
240
   *   post_{x_2} is false for e.1 and true for e.2.1, e.2.2, e.2, e
241
   *   post_{x_3} is false for e.1, e.2.1 and true for e.2.2, e.2, e
242
   *
243
   * We enforce a symmetry breaking scheme for each enumerator e that is
244
   * "variable-agnostic" (see argument isVarAgnostic in registerEnumerator)
245
   * that ensures the variables are ordered. This scheme makes use of these
246
   * predicates, described in the following:
247
   *
248
   * Let x_1, ..., x_m be variables that occur in the same subclass in the type
249
   * of e (see TermDbSygus::getSubclassForVar).
250
   * For i = 1, ..., m:
251
   *   // each variable does not occur initially in a traversal of e
252
   *   ~pre_{x_i}( e ) AND
253
   *   // for each subterm of e
254
   *   template z.
255
   *     // if this is variable x_i, then x_{i-1} must have already occurred
256
   *     is-x_i( z ) => pre_{x_{i-1}}( z ) AND
257
   *     for args a = 1...n
258
   *       // pre-definition for each argument of this term
259
   *       pre_{x_i}( z.a ) = a=0 ? pre_{x_i}( z ) : post_{x_i}( z.{a-1} ) AND
260
   *     // post-definition for this term
261
   *     post_{x_i}( z ) = post_{x_i}( z.n ) OR is-x_i( z )
262
   *
263
   * For clarity, above we have written pre and post as first-order predicates.
264
   * However, applications of pre/post should be seen as indexed Boolean
265
   * variables. The reason for this is pre and post cannot be given a consistent
266
   * semantics. For example, consider term f( x_1, x_1 ) and enumerator variable
267
   * e of the same type over which we are encoding a traversal. We have that
268
   * pre_{x_1}( e.1 ) is false and pre_{x_1}( e.2 ) is true, although the model
269
   * values for e.1 and e.2 are equal. Instead, pre_{x_1}( e.1 ) should be seen
270
   * as a Boolean variable V_pre_{x_1,e.1} indexed by x_1 and e.1. and likewise
271
   * for e.2. We convert all applications of pre/post to Boolean variables in
272
   * the method eliminateTraversalPredicates below. Nevertheless, it is
273
   * important that applications pre and post are encoded as APPLY_UF
274
   * applications so that they behave as expected under substitutions. For
275
   * example, pre_{x_1}( z.1 ) { z -> e.2 } results in pre_{x_1}( e.2.1 ), which
276
   * after eliminateTraversalPredicates is V_pre_{x_1, e.2.1}.
277
   */
278
  Node getTraversalPredicate(TypeNode tn, Node n, bool isPre);
279
  /** eliminate traversal predicates
280
   *
281
   * This replaces all applications of traversal predicates P( x ) in n with
282
   * unique Boolean variables, given by d_traversal_bool[ P( x ) ], and
283
   * returns the result.
284
   */
285
  Node eliminateTraversalPredicates(Node n);
286
  //-----------------------------------end traversal predicates
287
  /** a sygus sampler object for each (anchor, sygus type) pair
288
   *
289
   * This is used for the sygusRewVerify() option to verify the correctness of
290
   * the rewriter.
291
   */
292
  std::map<Node, std::map<TypeNode, std::unique_ptr<quantifiers::SygusSampler>>>
293
      d_sampler;
294
  /** Assert tester internal
295
   *
296
   * This function is called when the tester with index tindex is asserted for
297
   * n, exp is the tester predicate. For example, for grammar:
298
   *   A -> A+A | x | 1 | 0
299
   * when is_+( d ) is asserted,
300
   * assertTesterInternal(0, s( d ), is_+( s( d ) ),...) is called. This
301
   * function may send lemmas via inference manager.
302
   *
303
   * These lemmas are of various forms, including:
304
   * (1) dynamic symmetry breaking clauses for subterms of n (those added to
305
   * lemmas on calls to addSymBreakLemmasFor, see function below),
306
   * (2) static symmetry breaking clauses for subterms of n (those added to
307
   * lemmas on getSimpleSymBreakPred, see function below),
308
   * (3) conjecture-specific symmetry breaking lemmas, see
309
   * SynthConjecture::getSymmetryBreakingPredicate,
310
   * (4) fairness conflicts if sygusFair() is SygusFairMode::DIRECT, e.g.:
311
   *    size( d ) <= 1 V ~is-C1( d ) V ~is-C2( d.1 )
312
   * where C1 and C2 are non-nullary constructors.
313
   */
314
  void assertTesterInternal(int tindex, TNode n, Node exp);
315
  /**
316
   * This function is called when term n is registered to the theory of
317
   * datatypes. It makes the appropriate call to registerSearchTerm below,
318
   * if applicable.
319
   */
320
  void registerTerm(Node n);
321
322
  //------------------------dynamic symmetry breaking
323
  /** Register search term
324
   *
325
   * This function is called when selector chain n of the form
326
   * S_1( ... S_m( x ) ... ) is registered to the theory of datatypes, where
327
   * tn is the type of n, d indicates the depth of n (the sum of weights of the
328
   * selectors S_1...S_m), and topLevel is whether n is a top-level term
329
   * (see d_is_top_level). We refer to n as a "search term".
330
   *
331
   * The purpose of this function is to notify this class that symmetry breaking
332
   * lemmas should be instantiated for n. Any symmetry breaking lemmas that
333
   * are active for n (see description of addSymBreakLemmasFor) are added to
334
   * lemmas in this call.
335
   */
336
  void registerSearchTerm(TypeNode tn, unsigned d, Node n, bool topLevel);
337
  /** Register search value
338
   *
339
   * This function is called when a selector chain n has been assigned a model
340
   * value nv. This function calls itself recursively so that extensions of the
341
   * selector chain n are registered with all the subterms of nv. For example,
342
   * if we call this function with:
343
   *   n = x, nv = +( 1(), x() )
344
   * we make recursive calls with:
345
   *   n = x.1, nv = 1() and n = x.2, nv = x()
346
   *
347
   * a : the anchor of n,
348
   * d : the depth of n.
349
   *
350
   * This function determines if the value nv is equivalent via rewriting to
351
   * any previously registered search values for anchor a. If so, we construct
352
   * a symmetry breaking lemma template and register it in d_cache[a]. For
353
   * example, for grammar:
354
   *   A -> A+A | x | 1 | 0
355
   * Registering search value d -> x followed by d -> +( x, 0 ) results in the
356
   * construction of the symmetry breaking lemma template:
357
   *   ~is_+( z ) V ~is_x( z.1 ) V ~is_0( z.2 )
358
   * which is stored in d_cache[a].d_sbLemmas. This lemma is instantiated with
359
   * z -> t for all terms t of appropriate depth, including d.
360
   * This function strengthens blocking clauses using generalization techniques
361
   * described in Reynolds et al SYNT 2017.
362
   *
363
   * The return value of this function is an abstraction of model assignment
364
   * of nv to n, or null if we wish to exclude the model assignment nv to n.
365
   * The return value of this method is different from nv itself, e.g. if it
366
   * contains occurrences of the "any constant" constructor. For example, if
367
   * nv is C_+( C_x(), C_{any_constant}( 5 ) ), then the return value of this
368
   * function will either be null, or C_+( C_x(), C_{any_constant}( n.1.0 ) ),
369
   * where n.1.0 is the appropriate selector chain applied to n. We build this
370
   * abstraction since the semantics of C_{any_constant} is "any constant" and
371
   * not "some constant". Thus, we should consider the subterm
372
   * C_{any_constant}( 5 ) above to be an unconstrained variable (as represented
373
   * by a selector chain), instead of the concrete value 5.
374
   *
375
   * The flag isVarAgnostic is whether "a" is a variable agnostic enumerator. If
376
   * this is the case, we restrict symmetry breaking to subterms of n on its
377
   * leftmost subchain. For example, consider the grammar:
378
   *   A -> B=B
379
   *   B -> B+B | x | y | 0
380
   * Say we are registering the search value x = y+x. Notice that this value is
381
   * ordered. If a were a variable-agnostic enumerator of type A in this
382
   * case, we would only register x = y+x and x, and not y+x or y, since the
383
   * latter two terms are not leftmost subterms in this value. If we did on the
384
   * other hand register y+x, we would be prevented from solutions like x+y = 0
385
   * later, since x+y is equivalent to (the already registered value) y+x.
386
   *
387
   * If doSym is false, we are not performing symmetry breaking on n. This flag
388
   * is set to false on branches of n that are not leftmost.
389
   */
390
  Node registerSearchValue(Node a,
391
                           Node n,
392
                           Node nv,
393
                           unsigned d,
394
                           bool isVarAgnostic,
395
                           bool doSym);
396
  /** Register symmetry breaking lemma
397
   *
398
   * This function adds the symmetry breaking lemma template lem for terms of
399
   * type tn with anchor a. This is added to d_cache[a].d_sbLemmas. Notice that
400
   * we use lem as a template with free variable x, e.g. our template is:
401
   *   (lambda ((x tn)) lem)
402
   * where x = getFreeVar( tn ). For all search terms t of the appropriate
403
   * depth, we send the lemma lem{ x -> t } via the inference manager.
404
   *
405
   * The argument sz indicates the size of terms that the lemma applies to, e.g.
406
   *   ~is_+( z ) has size 1
407
   *   ~is_+( z ) V ~is_x( z.1 ) V ~is_0( z.2 ) has size 1
408
   *   ~is_+( z ) V ~is_+( z.1 ) has size 2
409
   * This is equivalent to sum of weights of constructors corresponding to each
410
   * tester, e.g. above + has weight 1, and x and 0 have weight 0.
411
   */
412
  void registerSymBreakLemma(TypeNode tn, Node lem, unsigned sz, Node a);
413
  /** Register symmetry breaking lemma for value
414
   *
415
   * This function adds a symmetry breaking lemma template for selector chains
416
   * with anchor a, that effectively states that val should never be a subterm
417
   * of any value for a.
418
   *
419
   * et : an "invariance test" (see sygus/sygus_invariance.h) which states a
420
   * criterion that val meets, which is the reason for its exclusion. This is
421
   * used for generalizing the symmetry breaking lemma template.
422
   * valr : if non-null, this states a value that should *not* be excluded by
423
   * the symmetry breaking lemma template, which is a restriction to the above
424
   * generalization.
425
   *
426
   * This function may add instances of the symmetry breaking template for
427
   * existing search terms, which are sent via the inference manager.
428
   */
429
  void registerSymBreakLemmaForValue(Node a,
430
                                     Node val,
431
                                     quantifiers::SygusInvarianceTest& et,
432
                                     Node valr,
433
                                     std::map<TypeNode, int>& var_count);
434
  /** Add symmetry breaking lemmas for term
435
   *
436
   * Sends all active symmetry breaking lemmas for selector chain t via the
437
   * inference manager. A symmetry breaking lemma L is active for t based on
438
   * three factors:
439
   * (1) the current search size sz(a) for its anchor a,
440
   * (2) the depth d of term t (see d_term_to_depth),
441
   * (3) the size sz(L) of the symmetry breaking lemma L.
442
   * In particular, L is active if sz(L) <= sz(a) - d. In other words, a
443
   * symmetry breaking lemma is active if it is intended to block terms of
444
   * size sz(L), and the maximum size that t can take in the current search,
445
   * sz(a)-d, is greater than or equal to this value.
446
   *
447
   * tn : the type of term t,
448
   * a : the anchor of term t,
449
   * d : the depth of term t.
450
   */
451
  void addSymBreakLemmasFor(TypeNode tn, TNode t, unsigned d, Node a);
452
  /** calls the above function where a is the anchor t */
453
  void addSymBreakLemmasFor(TypeNode tn, TNode t, unsigned d);
454
  //------------------------end dynamic symmetry breaking
455
456
  /** Get relevancy condition
457
   *
458
   * This returns (the negation of) a predicate that holds in the contexts in
459
   * which the selector chain n is specified. For example, the negation of the
460
   * relevancy condition for sel_{C2,1}( sel_{C1,1}( d ) ) is
461
   *    ~( is-C1( d ) ^ is-C2( sel_{C1,1}( d ) ) )
462
   * If shared selectors are enabled, this is a conjunction of disjunctions,
463
   * since shared selectors may apply to multiple constructors.
464
   */
465
  Node getRelevancyCondition( Node n );
466
  /** Cache of the above function */
467
  std::map<Node, Node> d_rlv_cond;
468
469
  //------------------------static symmetry breaking
470
  /** Get simple symmetry breakind predicate
471
   *
472
   * This function returns the "static" symmetry breaking lemma template for
473
   * terms with type tn and constructor index tindex, for the given depth. This
474
   * includes inferences about size with depth=0. Given grammar:
475
   *   A -> ite( B, A, A ) | A+A | x | 1 | 0
476
   *   B -> A = A
477
   * Examples of static symmetry breaking lemma templates are:
478
   *   for +, depth 0: size(z)=size(z.1)+size(z.2)+1
479
   *   for +, depth 1: ~is-0( z.1 ) ^ ~is-0( z.2 ) ^ F
480
   *     where F ensures the constructor of z.1 is less than that of z.2 based
481
   *     on some ordering.
482
   *   for ite, depth 1: z.2 != z.3
483
   * These templates can be thought of as "hard-coded" cases of dynamic symmetry
484
   * breaking lemma templates. Notice that the above lemma templates are in
485
   * terms of getFreeVar( tn ), hence only one is created per
486
   * (constructor, depth). A static symmetry break lemma template F[z] for
487
   * constructor C are included in lemmas of the form:
488
   *   is-C( t ) => F[t]
489
   * where t is a search term, see registerSearchTerm for definition of search
490
   * term.
491
   *
492
   * usingSymCons: whether we are using symbolic constructors for subterms in
493
   * the type tn,
494
   * isVarAgnostic: whether the terms we are enumerating are agnostic to
495
   * variables.
496
   *
497
   * The latter two options may affect the form of the predicate we construct.
498
   */
499
  Node getSimpleSymBreakPred(Node e,
500
                             TypeNode tn,
501
                             int tindex,
502
                             unsigned depth,
503
                             bool usingSymCons,
504
                             bool isVarAgnostic);
505
  /** Cache of the above function */
506
  std::map<Node,
507
           std::map<TypeNode,
508
                    std::map<int, std::map<bool, std::map<unsigned, Node>>>>>
509
      d_simple_sb_pred;
510
  /**
511
   * For each search term, this stores the maximum depth for which we have added
512
   * a static symmetry breaking lemma.
513
   *
514
   * This should be user context-dependent if sygus is updated to work in
515
   * incremental mode.
516
   */
517
  std::unordered_map<Node, unsigned> d_simple_proc;
518
  //------------------------end static symmetry breaking
519
520
  /** Get the canonical free variable for type tn */
521
  TNode getFreeVar( TypeNode tn );
522
  /** get term order predicate
523
   *
524
   * Assuming that n1 and n2 are children of a commutative operator, this
525
   * returns a symmetry breaking predicate that can be instantiated for n1 and
526
   * n2 while preserving satisfiability. By default, this is the predicate
527
   *   ( DT_SIZE n1 ) >= ( DT_SIZE n2 )
528
   */
529
  Node getTermOrderPredicate( Node n1, Node n2 );
530
531
 private:
532
  /**
533
   * Map from registered variables to whether they are a sygus enumerator.
534
   *
535
   * This should be user context-dependent if sygus is updated to work in
536
   * incremental mode.
537
   */
538
  std::map<Node, bool> d_register_st;
539
  //----------------------search size information
540
  /**
541
   * Checks whether e is a sygus enumerator, that is, a term for which this
542
   * class will track size for.
543
   *
544
   * We associate each sygus enumerator e with a "measure term", which is used
545
   * for bounding the size of terms for the models of e. The measure term for a
546
   * sygus enumerator may be e itself (if e has an active guard), or an
547
   * arbitrary sygus variable otherwise. A measure term m is one for which our
548
   * decision strategy decides on literals of the form (DT_SYGUS_BOUND m n).
549
   *
550
   * After determining the measure term m for e, if applicable, we initialize
551
   * SygusSizeDecisionStrategy for m below. This may result in lemmas sent via
552
   * the inference manager.
553
   */
554
  void registerSizeTerm(Node e);
555
  /** A decision strategy for each measure term allocated by this class */
556
502
  class SygusSizeDecisionStrategy : public DecisionStrategyFmf
557
  {
558
   public:
559
    SygusSizeDecisionStrategy(Env& env,
560
                              InferenceManager& im,
561
                              Node t,
562
                              TheoryState& s);
563
    /** the measure term */
564
    Node d_this;
565
    /**
566
     * For each size n, an explanation for why this measure term has size at
567
     * most n. This is typically the literal (DT_SYGUS_BOUND m n), which
568
     * we call the (n^th) "fairness literal" for m.
569
     */
570
    std::map< unsigned, Node > d_search_size_exp;
571
    /**
572
     * For each size, whether we have called SygusExtension::notifySearchSize.
573
     */
574
    std::map< unsigned, bool > d_search_size;
575
    /**
576
     * The current search size. This corresponds to the number of times
577
     * incrementCurrentSearchSize has been called for this measure term.
578
     */
579
    unsigned d_curr_search_size;
580
    /** the list of all enumerators whose measure term is this */
581
    std::vector< Node > d_anchors;
582
    /** get or make the measure value
583
     *
584
     * The measure value is an integer variable v that is a (symbolic) integer
585
     * value that is constrained to be less than or equal to the current search
586
     * size. For example, if we are using the fairness strategy
587
     * SygusFairMode::DT_SIZE (see options/datatype_options.h), then we
588
     * constrain: (DT_SYGUS_BOUND m n) <=> (v <= n) for all asserted fairness
589
     * literals. Then, if we are enforcing fairness based on the maximum size,
590
     * we assert: (DT_SIZE e) <= v for all enumerators e.
591
     */
592
    Node getOrMkMeasureValue();
593
    /** get or make the active measure value
594
     *
595
     * The active measure value av is an integer variable that corresponds to
596
     * the (symbolic) value of the sum of enumerators that are yet to be
597
     * registered. This is to enforce the "sum of measures" strategy. For
598
     * example, if we are using the fairness strategy SygusFairMode::DT_SIZE,
599
     * then initially av is equal to the measure value v, and the constraints
600
     *   (DT_SYGUS_BOUND m n) <=> (v <= n)
601
     * are added as before. When an enumerator e is registered, we add the
602
     * lemma:
603
     *   av = (DT_SIZE e) + av'
604
     * and update the active measure value to av'. This ensures that the sum
605
     * of sizes of active enumerators is at most n.
606
     *
607
     * If the flag mkNew is set to true, then we return a fresh variable and
608
     * update the active measure value.
609
     */
610
    Node getOrMkActiveMeasureValue(bool mkNew = false);
611
    /** Returns the s^th fairness literal for this measure term. */
612
    Node mkLiteral(unsigned s) override;
613
    /** identify */
614
1004709
    std::string identify() const override
615
    {
616
1004709
      return std::string("sygus_enum_size");
617
    }
618
619
   private:
620
    /** The inference manager we are using */
621
    InferenceManager& d_im;
622
    /** the measure value */
623
    Node d_measure_value;
624
    /** the sygus measure value */
625
    Node d_measure_value_active;
626
  };
627
  /** the above information for each registered measure term */
628
  std::map<Node, std::unique_ptr<SygusSizeDecisionStrategy>> d_szinfo;
629
  /** map from enumerators (anchors) to their associated measure term */
630
  std::map< Node, Node > d_anchor_to_measure_term;
631
  /** map from enumerators (anchors) to their active guard*/
632
  std::map< Node, Node > d_anchor_to_active_guard;
633
  /** map from enumerators (anchors) to a decision stratregy for that guard */
634
  std::map<Node, std::unique_ptr<DecisionStrategy>> d_anchor_to_ag_strategy;
635
  /** generic measure term
636
   *
637
   * This is a global term that is used as the measure term for all sygus
638
   * enumerators that do not have active guards. This class enforces that
639
   * all enumerators have size at most n, where n is the minimal integer
640
   * such that (DT_SYGUS_BOUND d_generic_measure_term n) is asserted.
641
   */
642
  Node d_generic_measure_term;
643
  /**
644
   * This increments the current search size for measure term m. This may
645
   * cause lemmas to be added to lemmas based on the fact that symmetry
646
   * breaking lemmas are now relevant for new search terms, see discussion
647
   * of how search size affects which lemmas are relevant above
648
   * addSymBreakLemmasFor.
649
   */
650
  void incrementCurrentSearchSize(TNode m);
651
  /**
652
   * Notify this class that we are currently searching for terms of size at
653
   * most s as model values for measure term m. Literal exp corresponds to the
654
   * explanation of why the measure term has size at most n. This calls
655
   * incrementSearchSize above, until the total number of times we have called
656
   * incrementSearchSize so far is at least s.
657
   */
658
  void notifySearchSize(TNode m, uint64_t s, Node exp);
659
  /** Allocates a SygusSizeDecisionStrategy object in d_szinfo. */
660
  void registerMeasureTerm( Node m );
661
  /**
662
   * Return the current search size for arbitrary term n. This is the current
663
   * search size of the anchor of n.
664
   */
665
  unsigned getSearchSizeFor( Node n );
666
  /** return the current search size for enumerator (anchor) e */
667
  unsigned getSearchSizeForAnchor(Node e);
668
  /** Get the current search size for measure term m in this SAT context. */
669
  unsigned getSearchSizeForMeasureTerm(Node m);
670
  /** get current template
671
   *
672
   * For debugging. This returns a term that corresponds to the current
673
   * inferred shape of n. For example, if the testers
674
   *   is-C1( n ) and is-C2( n.1 )
675
   * have been asserted where C1 and C2 are binary constructors, then this
676
   * method may return a term of the form:
677
   *   C1( C2( x1, x2 ), x3 )
678
   * for fresh variables x1, x2, x3. The map var_count maintains the variable
679
   * count for generating these fresh variables.
680
   */
681
  Node getCurrentTemplate( Node n, std::map< TypeNode, int >& var_count );
682
  //----------------------end search size information
683
  /** check value
684
   *
685
   * This is called when we have a model assignment vn for n, where n is
686
   * a selector chain applied to an enumerator (a search term). This function
687
   * ensures that testers have been asserted for each subterm of vn. This is
688
   * critical for ensuring that the proper steps have been taken by this class
689
   * regarding whether or not vn is a legal value for n (not greater than the
690
   * current search size and not a value that can be blocked by symmetry
691
   * breaking).
692
   *
693
   * For example, if vn = +( x(), x() ), then we ensure that the testers
694
   *   is-+( n ), is-x( n.1 ), is-x( n.2 )
695
   * have been asserted to this class. If a tester is not asserted for some
696
   * relevant selector chain S( n ) of n, then we add a lemma L for that
697
   * selector chain to lemmas, where L is the "splitting lemma" for S( n ), that
698
   * states that the top symbol of S( n ) must be one of the constructors of
699
   * its type.
700
   *
701
   * Notice that this function is a sanity check. Typically, it should be the
702
   * case that testers are asserted for all subterms of vn, and hence this
703
   * method should not ever add anything to lemmas. However, due to its
704
   * importance, we check this regardless.
705
   */
706
  bool checkValue(Node n, TNode vn, int ind);
707
  /**
708
   * Get the current SAT status of the guard g.
709
   * In particular, this returns 1 if g is asserted true, -1 if it is asserted
710
   * false, and 0 if it is not asserted.
711
   */
712
  int getGuardStatus( Node g );
713
};
714
715
}
716
}
717
}  // namespace cvc5
718
719
#endif
720