GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/base_solver.h Lines: 2 2 100.0 %
Date: 2021-08-01 Branches: 2 4 50.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Andres Noetzli, Mudathir Mohamed
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
 * Base solver for term indexing and constant inference for the
14
 * theory of strings.
15
 */
16
17
#include "cvc5_private.h"
18
19
#ifndef CVC5__THEORY__STRINGS__BASE_SOLVER_H
20
#define CVC5__THEORY__STRINGS__BASE_SOLVER_H
21
22
#include "context/cdhashset.h"
23
#include "context/cdlist.h"
24
#include "theory/strings/infer_info.h"
25
#include "theory/strings/inference_manager.h"
26
#include "theory/strings/normal_form.h"
27
#include "theory/strings/skolem_cache.h"
28
#include "theory/strings/solver_state.h"
29
30
namespace cvc5 {
31
namespace theory {
32
namespace strings {
33
34
/** The base solver for the theory of strings
35
 *
36
 * This implements techniques for inferring when terms are congruent in the
37
 * current context, and techniques for inferring when equivalence classes
38
 * are equivalent to constants.
39
 */
40
class BaseSolver
41
{
42
  using NodeSet = context::CDHashSet<Node>;
43
44
 public:
45
  BaseSolver(SolverState& s, InferenceManager& im);
46
  ~BaseSolver();
47
48
  //-----------------------inference steps
49
  /** check initial
50
   *
51
   * This function initializes term indices for each strings function symbol.
52
   * One key aspect of this construction is that concat terms are indexed by
53
   * their list of non-empty components. For example, if x = "" is an equality
54
   * asserted in this SAT context, then y ++ x ++ z may be indexed by (y,z).
55
   * This method may infer various facts while building these term indices, for
56
   * instance, based on congruence. An example would be inferring:
57
   *   y ++ x ++ z = y ++ z
58
   * if both terms are registered in this SAT context.
59
   *
60
   * This function should be called as a first step of any strategy.
61
   */
62
  void checkInit();
63
  /** check constant equivalence classes
64
   *
65
   * This function infers whether CONCAT terms can be simplified to constants.
66
   * For example, if x = "a" and y = "b" are equalities in the current SAT
67
   * context, then we may infer x ++ "c" ++ y is equivalent to "acb". In this
68
   * case, we infer the fact x ++ "c" ++ y = "acb".
69
   */
70
  void checkConstantEquivalenceClasses();
71
  /** check cardinality
72
   *
73
   * This function checks whether a cardinality inference needs to be applied
74
   * to a set of equivalence classes. For details, see Step 5 of the proof
75
   * procedure from Liang et al, CAV 2014.
76
   */
77
  void checkCardinality();
78
  //-----------------------end inference steps
79
80
  //-----------------------query functions
81
  /**
82
   * Is n congruent to another term in the current context that has not been
83
   * marked congruent? If so, we can ignore n.
84
   *
85
   * Note this and the functions in this block below are valid during a full
86
   * effort check after a call to checkInit.
87
   */
88
  bool isCongruent(Node n);
89
  /**
90
   * Get the constant that the equivalence class eqc is entailed to be equal
91
   * to, or null if none exist.
92
   */
93
  Node getConstantEqc(Node eqc);
94
  /**
95
   * Same as above, where the explanation for n = c is added to exp if c is
96
   * the (non-null) return value of this function, where n is a term in the
97
   * equivalence class of eqc.
98
   */
99
  Node explainConstantEqc(Node n, Node eqc, std::vector<Node>& exp);
100
  /**
101
   * Same as above, for "best content" terms.
102
   */
103
  Node explainBestContentEqc(Node n, Node eqc, std::vector<Node>& exp);
104
  /**
105
   * Get the set of equivalence classes of type string.
106
   */
107
  const std::vector<Node>& getStringEqc() const;
108
  //-----------------------end query functions
109
110
 private:
111
  /**
112
   * The information that we associated with each equivalence class.
113
   *
114
   * Example 1. Consider the equivalence class { r, x++"a"++y, x++z }, and
115
   * assume x = "" and y = "bb" in the current context. We have that
116
   *   d_bestContent = "abb",
117
   *   d_base = x++"a"++y
118
   *   d_exp = ( x = "" AND y = "bb" )
119
   *
120
   * Example 2. Consider the equivalence class { r, x++"a"++w++y, x++z }, and
121
   * assume x = "" and y = "bb" in the current context. We have that
122
   *   d_bestContent = "a" ++ w ++ "bb",
123
   *   d_bestScore = 3
124
   *   d_base = x++"a"++w++y
125
   *   d_exp = ( x = "" AND y = "bb" )
126
   *
127
   * This information is computed during checkInit and is used during various
128
   * inference schemas for deriving inferences.
129
   */
130
951750
  struct BaseEqcInfo
131
  {
132
    /**
133
     * Either a constant or a concatentation of constants and variables that
134
     * this equivalence class is entailed to be equal to. If it is a
135
     * concatenation, this is the concatenation that is currently known to have
136
     * the highest score (see `d_bestScore`).
137
     */
138
    Node d_bestContent;
139
    /**
140
     * The sum of the number of characters in the string literals of
141
     * `d_bestContent`.
142
     */
143
    size_t d_bestScore;
144
    /**
145
     * The term in the equivalence class that is entailed to be equal to
146
     * `d_bestContent`.
147
     */
148
    Node d_base;
149
    /** This term explains why `d_bestContent` is equal to `d_base`. */
150
    Node d_exp;
151
  };
152
153
  /**
154
   * A term index that considers terms modulo flattening and constant merging
155
   * for concatenation terms.
156
   */
157
3208406
  class TermIndex
158
  {
159
   public:
160
    /** Add n to this trie
161
     *
162
     * A term is indexed by flattening arguments of concatenation terms,
163
     * and replacing them by (non-empty) constants when possible, for example
164
     * if n is (str.++ x y z) and x = "abc" and y = "" are asserted, then n is
165
     * indexed by ("abc", z).
166
     *
167
     * index: the child of n we are currently processing,
168
     * s : reference to solver state,
169
     * er : the representative of the empty equivalence class.
170
     *
171
     * We store the vector of terms that n was indexed by in the vector c.
172
     */
173
    Node add(TNode n,
174
             unsigned index,
175
             const SolverState& s,
176
             Node er,
177
             std::vector<Node>& c);
178
    /** Clear this trie */
179
    void clear() { d_children.clear(); }
180
    /** The data at this node of the trie */
181
    Node d_data;
182
    /** The children of this node of the trie */
183
    std::map<TNode, TermIndex> d_children;
184
  };
185
  /**
186
   * This method is called as we are traversing the term index ti, where vecc
187
   * accumulates the list of constants in the path to ti. If ti has a non-null
188
   * data n, then we have inferred that d_data is equivalent to the
189
   * constant specified by vecc.
190
   *
191
   * @param ti The term index for string concatenations
192
   * @param vecc The list of constants in the path to ti
193
   * @param ensureConst If true, require that each element in the path is
194
   *                    constant
195
   * @param isConst If true, the path so far only includes constants
196
   */
197
  void checkConstantEquivalenceClasses(TermIndex* ti,
198
                                       std::vector<Node>& vecc,
199
                                       bool ensureConst = true,
200
                                       bool isConst = true);
201
  /**
202
   * Check cardinality for type tn. This adds a lemma corresponding to
203
   * cardinality for terms of type tn, if applicable.
204
   *
205
   * @param tn The string-like type of terms we are considering,
206
   * @param cols The list of collections of equivalence classes. This is a
207
   * partition of all string equivalence classes, grouped by those with equal
208
   * lengths.
209
   * @param lts The length of each of the collections in cols.
210
   */
211
  void checkCardinalityType(TypeNode tn,
212
                            std::vector<std::vector<Node> >& cols,
213
                            std::vector<Node>& lts);
214
  /** The solver state object */
215
  SolverState& d_state;
216
  /** The (custom) output channel of the theory of strings */
217
  InferenceManager& d_im;
218
  /** Commonly used constants */
219
  Node d_emptyString;
220
  Node d_false;
221
  /**
222
   * A congruence class is a set of terms f( t1 ), ..., f( tn ) where
223
   * t1 = ... = tn. Congruence classes are important since all but
224
   * one of the above terms (the representative of the congruence class)
225
   * can be ignored by the solver.
226
   *
227
   * This set contains a set of nodes that are not representatives of their
228
   * congruence class. This set is used to skip reasoning about terms in
229
   * various inference schemas implemented by this class.
230
   */
231
  NodeSet d_congruent;
232
  /**
233
   * Maps equivalence classes to their info, see description of `BaseEqcInfo`
234
   * for more information.
235
   */
236
  std::map<Node, BaseEqcInfo> d_eqcInfo;
237
  /** The list of equivalence classes of type string */
238
  std::vector<Node> d_stringsEqc;
239
  /** A term index for each type, function kind pair */
240
  std::map<TypeNode, std::map<Kind, TermIndex> > d_termIndex;
241
  /** the cardinality of the alphabet */
242
  uint32_t d_cardSize;
243
}; /* class BaseSolver */
244
245
}  // namespace strings
246
}  // namespace theory
247
}  // namespace cvc5
248
249
#endif /* CVC5__THEORY__STRINGS__BASE_SOLVER_H */