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