GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/datatypes/sygus_extension.h Lines: 5 5 100.0 %
Date: 2021-09-18 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
447
 class SearchCache
186
 {
187
  public:
188
447
    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, quantifiers::SygusSampler>> d_sampler;
293
  /** Assert tester internal
294
   *
295
   * This function is called when the tester with index tindex is asserted for
296
   * n, exp is the tester predicate. For example, for grammar:
297
   *   A -> A+A | x | 1 | 0
298
   * when is_+( d ) is asserted,
299
   * assertTesterInternal(0, s( d ), is_+( s( d ) ),...) is called. This
300
   * function may send lemmas via inference manager.
301
   *
302
   * These lemmas are of various forms, including:
303
   * (1) dynamic symmetry breaking clauses for subterms of n (those added to
304
   * lemmas on calls to addSymBreakLemmasFor, see function below),
305
   * (2) static symmetry breaking clauses for subterms of n (those added to
306
   * lemmas on getSimpleSymBreakPred, see function below),
307
   * (3) conjecture-specific symmetry breaking lemmas, see
308
   * SynthConjecture::getSymmetryBreakingPredicate,
309
   * (4) fairness conflicts if sygusFair() is SygusFairMode::DIRECT, e.g.:
310
   *    size( d ) <= 1 V ~is-C1( d ) V ~is-C2( d.1 )
311
   * where C1 and C2 are non-nullary constructors.
312
   */
313
  void assertTesterInternal(int tindex, TNode n, Node exp);
314
  /**
315
   * This function is called when term n is registered to the theory of
316
   * datatypes. It makes the appropriate call to registerSearchTerm below,
317
   * if applicable.
318
   */
319
  void registerTerm(Node n);
320
321
  //------------------------dynamic symmetry breaking
322
  /** Register search term
323
   *
324
   * This function is called when selector chain n of the form
325
   * S_1( ... S_m( x ) ... ) is registered to the theory of datatypes, where
326
   * tn is the type of n, d indicates the depth of n (the sum of weights of the
327
   * selectors S_1...S_m), and topLevel is whether n is a top-level term
328
   * (see d_is_top_level). We refer to n as a "search term".
329
   *
330
   * The purpose of this function is to notify this class that symmetry breaking
331
   * lemmas should be instantiated for n. Any symmetry breaking lemmas that
332
   * are active for n (see description of addSymBreakLemmasFor) are added to
333
   * lemmas in this call.
334
   */
335
  void registerSearchTerm(TypeNode tn, unsigned d, Node n, bool topLevel);
336
  /** Register search value
337
   *
338
   * This function is called when a selector chain n has been assigned a model
339
   * value nv. This function calls itself recursively so that extensions of the
340
   * selector chain n are registered with all the subterms of nv. For example,
341
   * if we call this function with:
342
   *   n = x, nv = +( 1(), x() )
343
   * we make recursive calls with:
344
   *   n = x.1, nv = 1() and n = x.2, nv = x()
345
   *
346
   * a : the anchor of n,
347
   * d : the depth of n.
348
   *
349
   * This function determines if the value nv is equivalent via rewriting to
350
   * any previously registered search values for anchor a. If so, we construct
351
   * a symmetry breaking lemma template and register it in d_cache[a]. For
352
   * example, for grammar:
353
   *   A -> A+A | x | 1 | 0
354
   * Registering search value d -> x followed by d -> +( x, 0 ) results in the
355
   * construction of the symmetry breaking lemma template:
356
   *   ~is_+( z ) V ~is_x( z.1 ) V ~is_0( z.2 )
357
   * which is stored in d_cache[a].d_sbLemmas. This lemma is instantiated with
358
   * z -> t for all terms t of appropriate depth, including d.
359
   * This function strengthens blocking clauses using generalization techniques
360
   * described in Reynolds et al SYNT 2017.
361
   *
362
   * The return value of this function is an abstraction of model assignment
363
   * of nv to n, or null if we wish to exclude the model assignment nv to n.
364
   * The return value of this method is different from nv itself, e.g. if it
365
   * contains occurrences of the "any constant" constructor. For example, if
366
   * nv is C_+( C_x(), C_{any_constant}( 5 ) ), then the return value of this
367
   * function will either be null, or C_+( C_x(), C_{any_constant}( n.1.0 ) ),
368
   * where n.1.0 is the appropriate selector chain applied to n. We build this
369
   * abstraction since the semantics of C_{any_constant} is "any constant" and
370
   * not "some constant". Thus, we should consider the subterm
371
   * C_{any_constant}( 5 ) above to be an unconstrained variable (as represented
372
   * by a selector chain), instead of the concrete value 5.
373
   *
374
   * The flag isVarAgnostic is whether "a" is a variable agnostic enumerator. If
375
   * this is the case, we restrict symmetry breaking to subterms of n on its
376
   * leftmost subchain. For example, consider the grammar:
377
   *   A -> B=B
378
   *   B -> B+B | x | y | 0
379
   * Say we are registering the search value x = y+x. Notice that this value is
380
   * ordered. If a were a variable-agnostic enumerator of type A in this
381
   * case, we would only register x = y+x and x, and not y+x or y, since the
382
   * latter two terms are not leftmost subterms in this value. If we did on the
383
   * other hand register y+x, we would be prevented from solutions like x+y = 0
384
   * later, since x+y is equivalent to (the already registered value) y+x.
385
   *
386
   * If doSym is false, we are not performing symmetry breaking on n. This flag
387
   * is set to false on branches of n that are not leftmost.
388
   */
389
  Node registerSearchValue(Node a,
390
                           Node n,
391
                           Node nv,
392
                           unsigned d,
393
                           bool isVarAgnostic,
394
                           bool doSym);
395
  /** Register symmetry breaking lemma
396
   *
397
   * This function adds the symmetry breaking lemma template lem for terms of
398
   * type tn with anchor a. This is added to d_cache[a].d_sbLemmas. Notice that
399
   * we use lem as a template with free variable x, e.g. our template is:
400
   *   (lambda ((x tn)) lem)
401
   * where x = getFreeVar( tn ). For all search terms t of the appropriate
402
   * depth, we send the lemma lem{ x -> t } via the inference manager.
403
   *
404
   * The argument sz indicates the size of terms that the lemma applies to, e.g.
405
   *   ~is_+( z ) has size 1
406
   *   ~is_+( z ) V ~is_x( z.1 ) V ~is_0( z.2 ) has size 1
407
   *   ~is_+( z ) V ~is_+( z.1 ) has size 2
408
   * This is equivalent to sum of weights of constructors corresponding to each
409
   * tester, e.g. above + has weight 1, and x and 0 have weight 0.
410
   */
411
  void registerSymBreakLemma(TypeNode tn, Node lem, unsigned sz, Node a);
412
  /** Register symmetry breaking lemma for value
413
   *
414
   * This function adds a symmetry breaking lemma template for selector chains
415
   * with anchor a, that effectively states that val should never be a subterm
416
   * of any value for a.
417
   *
418
   * et : an "invariance test" (see sygus/sygus_invariance.h) which states a
419
   * criterion that val meets, which is the reason for its exclusion. This is
420
   * used for generalizing the symmetry breaking lemma template.
421
   * valr : if non-null, this states a value that should *not* be excluded by
422
   * the symmetry breaking lemma template, which is a restriction to the above
423
   * generalization.
424
   *
425
   * This function may add instances of the symmetry breaking template for
426
   * existing search terms, which are sent via the inference manager.
427
   */
428
  void registerSymBreakLemmaForValue(Node a,
429
                                     Node val,
430
                                     quantifiers::SygusInvarianceTest& et,
431
                                     Node valr,
432
                                     std::map<TypeNode, int>& var_count);
433
  /** Add symmetry breaking lemmas for term
434
   *
435
   * Sends all active symmetry breaking lemmas for selector chain t via the
436
   * inference manager. A symmetry breaking lemma L is active for t based on
437
   * three factors:
438
   * (1) the current search size sz(a) for its anchor a,
439
   * (2) the depth d of term t (see d_term_to_depth),
440
   * (3) the size sz(L) of the symmetry breaking lemma L.
441
   * In particular, L is active if sz(L) <= sz(a) - d. In other words, a
442
   * symmetry breaking lemma is active if it is intended to block terms of
443
   * size sz(L), and the maximum size that t can take in the current search,
444
   * sz(a)-d, is greater than or equal to this value.
445
   *
446
   * tn : the type of term t,
447
   * a : the anchor of term t,
448
   * d : the depth of term t.
449
   */
450
  void addSymBreakLemmasFor(TypeNode tn, TNode t, unsigned d, Node a);
451
  /** calls the above function where a is the anchor t */
452
  void addSymBreakLemmasFor(TypeNode tn, TNode t, unsigned d);
453
  //------------------------end dynamic symmetry breaking
454
455
  /** Get relevancy condition
456
   *
457
   * This returns (the negation of) a predicate that holds in the contexts in
458
   * which the selector chain n is specified. For example, the negation of the
459
   * relevancy condition for sel_{C2,1}( sel_{C1,1}( d ) ) is
460
   *    ~( is-C1( d ) ^ is-C2( sel_{C1,1}( d ) ) )
461
   * If shared selectors are enabled, this is a conjunction of disjunctions,
462
   * since shared selectors may apply to multiple constructors.
463
   */
464
  Node getRelevancyCondition( Node n );
465
  /** Cache of the above function */
466
  std::map<Node, Node> d_rlv_cond;
467
468
  //------------------------static symmetry breaking
469
  /** Get simple symmetry breakind predicate
470
   *
471
   * This function returns the "static" symmetry breaking lemma template for
472
   * terms with type tn and constructor index tindex, for the given depth. This
473
   * includes inferences about size with depth=0. Given grammar:
474
   *   A -> ite( B, A, A ) | A+A | x | 1 | 0
475
   *   B -> A = A
476
   * Examples of static symmetry breaking lemma templates are:
477
   *   for +, depth 0: size(z)=size(z.1)+size(z.2)+1
478
   *   for +, depth 1: ~is-0( z.1 ) ^ ~is-0( z.2 ) ^ F
479
   *     where F ensures the constructor of z.1 is less than that of z.2 based
480
   *     on some ordering.
481
   *   for ite, depth 1: z.2 != z.3
482
   * These templates can be thought of as "hard-coded" cases of dynamic symmetry
483
   * breaking lemma templates. Notice that the above lemma templates are in
484
   * terms of getFreeVar( tn ), hence only one is created per
485
   * (constructor, depth). A static symmetry break lemma template F[z] for
486
   * constructor C are included in lemmas of the form:
487
   *   is-C( t ) => F[t]
488
   * where t is a search term, see registerSearchTerm for definition of search
489
   * term.
490
   *
491
   * usingSymCons: whether we are using symbolic constructors for subterms in
492
   * the type tn,
493
   * isVarAgnostic: whether the terms we are enumerating are agnostic to
494
   * variables.
495
   *
496
   * The latter two options may affect the form of the predicate we construct.
497
   */
498
  Node getSimpleSymBreakPred(Node e,
499
                             TypeNode tn,
500
                             int tindex,
501
                             unsigned depth,
502
                             bool usingSymCons,
503
                             bool isVarAgnostic);
504
  /** Cache of the above function */
505
  std::map<Node,
506
           std::map<TypeNode,
507
                    std::map<int, std::map<bool, std::map<unsigned, Node>>>>>
508
      d_simple_sb_pred;
509
  /**
510
   * For each search term, this stores the maximum depth for which we have added
511
   * a static symmetry breaking lemma.
512
   *
513
   * This should be user context-dependent if sygus is updated to work in
514
   * incremental mode.
515
   */
516
  std::unordered_map<Node, unsigned> d_simple_proc;
517
  //------------------------end static symmetry breaking
518
519
  /** Get the canonical free variable for type tn */
520
  TNode getFreeVar( TypeNode tn );
521
  /** get term order predicate
522
   *
523
   * Assuming that n1 and n2 are children of a commutative operator, this
524
   * returns a symmetry breaking predicate that can be instantiated for n1 and
525
   * n2 while preserving satisfiability. By default, this is the predicate
526
   *   ( DT_SIZE n1 ) >= ( DT_SIZE n2 )
527
   */
528
  Node getTermOrderPredicate( Node n1, Node n2 );
529
530
 private:
531
  /**
532
   * Map from registered variables to whether they are a sygus enumerator.
533
   *
534
   * This should be user context-dependent if sygus is updated to work in
535
   * incremental mode.
536
   */
537
  std::map<Node, bool> d_register_st;
538
  //----------------------search size information
539
  /**
540
   * Checks whether e is a sygus enumerator, that is, a term for which this
541
   * class will track size for.
542
   *
543
   * We associate each sygus enumerator e with a "measure term", which is used
544
   * for bounding the size of terms for the models of e. The measure term for a
545
   * sygus enumerator may be e itself (if e has an active guard), or an
546
   * arbitrary sygus variable otherwise. A measure term m is one for which our
547
   * decision strategy decides on literals of the form (DT_SYGUS_BOUND m n).
548
   *
549
   * After determining the measure term m for e, if applicable, we initialize
550
   * SygusSizeDecisionStrategy for m below. This may result in lemmas sent via
551
   * the inference manager.
552
   */
553
  void registerSizeTerm(Node e);
554
  /** A decision strategy for each measure term allocated by this class */
555
542
  class SygusSizeDecisionStrategy : public DecisionStrategyFmf
556
  {
557
   public:
558
    SygusSizeDecisionStrategy(Env& env,
559
                              InferenceManager& im,
560
                              Node t,
561
                              TheoryState& s);
562
    /** the measure term */
563
    Node d_this;
564
    /**
565
     * For each size n, an explanation for why this measure term has size at
566
     * most n. This is typically the literal (DT_SYGUS_BOUND m n), which
567
     * we call the (n^th) "fairness literal" for m.
568
     */
569
    std::map< unsigned, Node > d_search_size_exp;
570
    /**
571
     * For each size, whether we have called SygusExtension::notifySearchSize.
572
     */
573
    std::map< unsigned, bool > d_search_size;
574
    /**
575
     * The current search size. This corresponds to the number of times
576
     * incrementCurrentSearchSize has been called for this measure term.
577
     */
578
    unsigned d_curr_search_size;
579
    /** the list of all enumerators whose measure term is this */
580
    std::vector< Node > d_anchors;
581
    /** get or make the measure value
582
     *
583
     * The measure value is an integer variable v that is a (symbolic) integer
584
     * value that is constrained to be less than or equal to the current search
585
     * size. For example, if we are using the fairness strategy
586
     * SygusFairMode::DT_SIZE (see options/datatype_options.h), then we
587
     * constrain: (DT_SYGUS_BOUND m n) <=> (v <= n) for all asserted fairness
588
     * literals. Then, if we are enforcing fairness based on the maximum size,
589
     * we assert: (DT_SIZE e) <= v for all enumerators e.
590
     */
591
    Node getOrMkMeasureValue();
592
    /** get or make the active measure value
593
     *
594
     * The active measure value av is an integer variable that corresponds to
595
     * the (symbolic) value of the sum of enumerators that are yet to be
596
     * registered. This is to enforce the "sum of measures" strategy. For
597
     * example, if we are using the fairness strategy SygusFairMode::DT_SIZE,
598
     * then initially av is equal to the measure value v, and the constraints
599
     *   (DT_SYGUS_BOUND m n) <=> (v <= n)
600
     * are added as before. When an enumerator e is registered, we add the
601
     * lemma:
602
     *   av = (DT_SIZE e) + av'
603
     * and update the active measure value to av'. This ensures that the sum
604
     * of sizes of active enumerators is at most n.
605
     *
606
     * If the flag mkNew is set to true, then we return a fresh variable and
607
     * update the active measure value.
608
     */
609
    Node getOrMkActiveMeasureValue(bool mkNew = false);
610
    /** Returns the s^th fairness literal for this measure term. */
611
    Node mkLiteral(unsigned s) override;
612
    /** identify */
613
1046403
    std::string identify() const override
614
    {
615
1046403
      return std::string("sygus_enum_size");
616
    }
617
618
   private:
619
    /** The inference manager we are using */
620
    InferenceManager& d_im;
621
    /** the measure value */
622
    Node d_measure_value;
623
    /** the sygus measure value */
624
    Node d_measure_value_active;
625
  };
626
  /** the above information for each registered measure term */
627
  std::map<Node, std::unique_ptr<SygusSizeDecisionStrategy>> d_szinfo;
628
  /** map from enumerators (anchors) to their associated measure term */
629
  std::map< Node, Node > d_anchor_to_measure_term;
630
  /** map from enumerators (anchors) to their active guard*/
631
  std::map< Node, Node > d_anchor_to_active_guard;
632
  /** map from enumerators (anchors) to a decision stratregy for that guard */
633
  std::map<Node, std::unique_ptr<DecisionStrategy>> d_anchor_to_ag_strategy;
634
  /** generic measure term
635
   *
636
   * This is a global term that is used as the measure term for all sygus
637
   * enumerators that do not have active guards. This class enforces that
638
   * all enumerators have size at most n, where n is the minimal integer
639
   * such that (DT_SYGUS_BOUND d_generic_measure_term n) is asserted.
640
   */
641
  Node d_generic_measure_term;
642
  /**
643
   * This increments the current search size for measure term m. This may
644
   * cause lemmas to be added to lemmas based on the fact that symmetry
645
   * breaking lemmas are now relevant for new search terms, see discussion
646
   * of how search size affects which lemmas are relevant above
647
   * addSymBreakLemmasFor.
648
   */
649
  void incrementCurrentSearchSize(TNode m);
650
  /**
651
   * Notify this class that we are currently searching for terms of size at
652
   * most s as model values for measure term m. Literal exp corresponds to the
653
   * explanation of why the measure term has size at most n. This calls
654
   * incrementSearchSize above, until the total number of times we have called
655
   * incrementSearchSize so far is at least s.
656
   */
657
  void notifySearchSize(TNode m, uint64_t s, Node exp);
658
  /** Allocates a SygusSizeDecisionStrategy object in d_szinfo. */
659
  void registerMeasureTerm( Node m );
660
  /**
661
   * Return the current search size for arbitrary term n. This is the current
662
   * search size of the anchor of n.
663
   */
664
  unsigned getSearchSizeFor( Node n );
665
  /** return the current search size for enumerator (anchor) e */
666
  unsigned getSearchSizeForAnchor(Node e);
667
  /** Get the current search size for measure term m in this SAT context. */
668
  unsigned getSearchSizeForMeasureTerm(Node m);
669
  /** get current template
670
   *
671
   * For debugging. This returns a term that corresponds to the current
672
   * inferred shape of n. For example, if the testers
673
   *   is-C1( n ) and is-C2( n.1 )
674
   * have been asserted where C1 and C2 are binary constructors, then this
675
   * method may return a term of the form:
676
   *   C1( C2( x1, x2 ), x3 )
677
   * for fresh variables x1, x2, x3. The map var_count maintains the variable
678
   * count for generating these fresh variables.
679
   */
680
  Node getCurrentTemplate( Node n, std::map< TypeNode, int >& var_count );
681
  //----------------------end search size information
682
  /** check value
683
   *
684
   * This is called when we have a model assignment vn for n, where n is
685
   * a selector chain applied to an enumerator (a search term). This function
686
   * ensures that testers have been asserted for each subterm of vn. This is
687
   * critical for ensuring that the proper steps have been taken by this class
688
   * regarding whether or not vn is a legal value for n (not greater than the
689
   * current search size and not a value that can be blocked by symmetry
690
   * breaking).
691
   *
692
   * For example, if vn = +( x(), x() ), then we ensure that the testers
693
   *   is-+( n ), is-x( n.1 ), is-x( n.2 )
694
   * have been asserted to this class. If a tester is not asserted for some
695
   * relevant selector chain S( n ) of n, then we add a lemma L for that
696
   * selector chain to lemmas, where L is the "splitting lemma" for S( n ), that
697
   * states that the top symbol of S( n ) must be one of the constructors of
698
   * its type.
699
   *
700
   * Notice that this function is a sanity check. Typically, it should be the
701
   * case that testers are asserted for all subterms of vn, and hence this
702
   * method should not ever add anything to lemmas. However, due to its
703
   * importance, we check this regardless.
704
   */
705
  bool checkValue(Node n, TNode vn, int ind);
706
  /**
707
   * Get the current SAT status of the guard g.
708
   * In particular, this returns 1 if g is asserted true, -1 if it is asserted
709
   * false, and 0 if it is not asserted.
710
   */
711
  int getGuardStatus( Node g );
712
};
713
714
}
715
}
716
}  // namespace cvc5
717
718
#endif
719