GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/skolem_cache.h Lines: 1 1 100.0 %
Date: 2021-05-22 Branches: 0 0 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Andres Noetzli, Yoni Zohar
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
 * A cache of skolems for theory of strings.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__STRINGS__SKOLEM_CACHE_H
19
#define CVC5__THEORY__STRINGS__SKOLEM_CACHE_H
20
21
#include <map>
22
#include <tuple>
23
#include <unordered_set>
24
25
#include "expr/node.h"
26
#include "expr/skolem_manager.h"
27
28
namespace cvc5 {
29
namespace theory {
30
namespace strings {
31
32
/**
33
 * A cache of all string skolems generated by the TheoryStrings class. This
34
 * cache is used to ensure that duplicate skolems are not generated when
35
 * possible, and helps identify what skolems were allocated in the current run.
36
 */
37
10388
class SkolemCache
38
{
39
 public:
40
  /**
41
   * Constructor.
42
   *
43
   * useOpts determines if we aggressively share Skolems or return the constants
44
   * they are entailed to be equal to.
45
   */
46
  SkolemCache(bool useOpts = true);
47
  /** Identifiers for skolem types
48
   *
49
   * The comments below document the properties of each skolem introduced by
50
   * inference in the strings solver, where by skolem we mean the fresh
51
   * string variable that witnesses each of "exists k".
52
   *
53
   * The skolems with _REV suffixes are used for the reverse version of the
54
   * preconditions below, e.g. where we are considering a' ++ a = b' ++ b.
55
   *
56
   * All skolems assume a and b are strings unless otherwise stated.
57
   */
58
  enum SkolemId
59
  {
60
    // exists k. k = a
61
    SK_PURIFY,
62
    // a != "" ^ b = "ccccd" ^ a ++ "d" ++ a' = b ++ b' =>
63
    //    exists k. a = "cccc" ++ k
64
    SK_ID_C_SPT,
65
    SK_ID_C_SPT_REV,
66
    // a != "" ^ b = "c" ^ len(a)!=len(b) ^ a ++ a' = b ++ b' =>
67
    //    exists k. a = "c" ++ k
68
    SK_ID_VC_SPT,
69
    SK_ID_VC_SPT_REV,
70
    // a != "" ^ b != "" ^ len(a)!=len(b) ^ a ++ a' = b ++ b' =>
71
    //    exists k1 k2. len( k1 )>0 ^ len( k2 )>0 ^
72
    //                  ( a ++ k1 = b OR a = b ++ k2 )
73
    // k1 is the variable for (a,b) and k2 is the skolem for (b,a).
74
    SK_ID_V_SPT,
75
    SK_ID_V_SPT_REV,
76
    // a != "" ^ b != "" ^ len(a)!=len(b) ^ a ++ a' = b ++ b' =>
77
    //    exists k. len( k )>0 ^ ( a ++ k = b OR a = b ++ k )
78
    SK_ID_V_UNIFIED_SPT,
79
    SK_ID_V_UNIFIED_SPT_REV,
80
    // a != ""  ^ b = "c" ^ a ++ a' != b ++ b' =>
81
    //    exists k, k_rem.
82
    //         len( k ) = 1 ^
83
    //         ( ( a = k ++ k_rem ^ k != "c" ) OR ( a = "c" ++ k_rem ) )
84
    SK_ID_DC_SPT,
85
    SK_ID_DC_SPT_REM,
86
    // a != ""  ^ b != "" ^ len( a ) != len( b ) ^ a ++ a' != b ++ b' =>
87
    //    exists k_x k_y k_z.
88
    //         ( len( k_y ) = len( a ) ^ len( k_x ) = len( b ) ^ len( k_z) > 0
89
    //           ( a = k_x ++ k_z OR b = k_y ++ k_z ) )
90
    SK_ID_DEQ_X,
91
    SK_ID_DEQ_Y,
92
    // contains( a, b ) =>
93
    //    exists k_pre, k_post. a = k_pre ++ b ++ k_post ^
94
    //                          ~contains(k_pre ++ substr( b, 0, len(b)-1 ), b)
95
    //
96
    // As an optimization, these skolems are reused for positive occurrences of
97
    // contains, where they have the semantics:
98
    //
99
    //   contains( a, b ) =>
100
    //      exists k_pre, k_post. a = k_pre ++ b ++ k_post
101
    //
102
    // We reuse them since it is sound to consider w.l.o.g. the first occurrence
103
    // of b in a as the witness for contains( a, b ).
104
    SK_FIRST_CTN_PRE,
105
    SK_FIRST_CTN_POST,
106
    // For sequence a and regular expression b,
107
    // in_re(a, re.++(_*, b, _*)) =>
108
    //    exists k_pre, k_match, k_post.
109
    //       a = k_pre ++ k_match ++ k_post ^
110
    //       ~in_re(k_pre ++ substr(k_match, 0, str.len(k_match) - 1),
111
    //              re.++(_*, b, _*)) ^
112
    //       in_re(k2, y)
113
    SK_FIRST_MATCH_PRE,
114
    SK_FIRST_MATCH,
115
    SK_FIRST_MATCH_POST,
116
    // For integer b,
117
    // len( a ) > b =>
118
    //    exists k. a = k ++ a' ^ len( k ) = b
119
    SK_PREFIX,
120
    // For integer b,
121
    // b > 0 =>
122
    //    exists k. a = a' ++ k ^ len( k ) = ite( len(a)>b, len(a)-b, 0 )
123
    SK_SUFFIX_REM,
124
    // --------------- integer skolems
125
    // exists k. ( b occurs k times in a )
126
    SK_NUM_OCCUR,
127
    // --------------- function skolems
128
    // For function k: Int -> Int
129
    //   exists k.
130
    //     forall 0 <= x <= n,
131
    //       k(x) is the end index of the x^th occurrence of b in a
132
    //   where n is the number of occurrences of b in a, and k(0)=0.
133
    SK_OCCUR_INDEX,
134
    // For function k: Int -> Int
135
    //   exists k.
136
    //     forall 0 <= x < n,
137
    //       k(x) is the length of the x^th occurrence of b in a (excluding
138
    //       matches of empty strings)
139
    //   where b is a regular expression, n is the number of occurrences of b
140
    //   in a, and k(0)=0.
141
    SK_OCCUR_LEN,
142
    // For function k: ((Seq U) x Int) -> U
143
    // exists k.
144
    // forall s, n.
145
    //  k(s, n) is some undefined value of sort U
146
    SK_NTH,
147
  };
148
  /**
149
   * Returns a skolem of type string that is cached for (a,b,id) and has
150
   * name c.
151
   */
152
  Node mkSkolemCached(Node a, Node b, SkolemId id, const char* c);
153
  /**
154
   * Returns a skolem of type string that is cached for (a,[null],id) and has
155
   * name c.
156
   */
157
  Node mkSkolemCached(Node a, SkolemId id, const char* c);
158
  /** Same as above, but the skolem to construct has a custom type tn */
159
  Node mkTypedSkolemCached(
160
      TypeNode tn, Node a, Node b, SkolemId id, const char* c);
161
  /** Same as mkTypedSkolemCached above for (a,[null],id) */
162
  Node mkTypedSkolemCached(TypeNode tn, Node a, SkolemId id, const char* c);
163
  /**
164
   * Specific version for seq.nth, used for cases where the index is out of
165
   * range for sequence type seqType.
166
   */
167
  static Node mkSkolemSeqNth(TypeNode seqType, const char* c);
168
  /** Returns a (uncached) skolem of type string with name c */
169
  Node mkSkolem(const char* c);
170
  /** Returns true if n is a skolem allocated by this class */
171
  bool isSkolem(Node n) const;
172
  /** Make index variable
173
   *
174
   * This returns an integer variable of kind BOUND_VARIABLE that is used
175
   * for axiomatizing the behavior of a term or predicate t. Notice that this
176
   * index variable does *not* necessarily refer to indices in the term t
177
   * itself. Instead, it refers to indices in the relevant string in the
178
   * reduction of t. For example, the index variable for the term str.to_int(s)
179
   * is used to quantify over the positions in string term s.
180
   */
181
  static Node mkIndexVar(Node t);
182
183
 private:
184
  /**
185
   * Simplifies the arguments for a string skolem used for indexing into the
186
   * cache. In certain cases, we can share skolems with similar arguments e.g.
187
   * SK_FIRST_CTN(a, c) can be used instead of SK_FIRST_CTN((str.substr a 0 n),
188
   * c) because the first occurrence of "c" in "(str.substr a 0 n)" is also the
189
   * first occurrence of "c" in "a" (assuming that "c" appears in both and
190
   * otherwise the value of SK_FIRST_CTN does not matter).
191
   *
192
   * @param id The type of skolem
193
   * @param a The first argument used for indexing
194
   * @param b The second argument used for indexing
195
   * @return A tuple with the new skolem id, the new first, and the new second
196
   * argument
197
   */
198
  std::tuple<SkolemId, Node, Node> normalizeStringSkolem(SkolemId id,
199
                                                         Node a,
200
                                                         Node b);
201
  /** whether we are using optimizations */
202
  bool d_useOpts;
203
  /** string type */
204
  TypeNode d_strType;
205
  /** Constant node zero */
206
  Node d_zero;
207
  /** map from node pairs and identifiers to skolems */
208
  std::map<Node, std::map<Node, std::map<SkolemId, Node> > > d_skolemCache;
209
  /** the set of all skolems we have generated */
210
  std::unordered_set<Node> d_allSkolems;
211
};
212
213
}  // namespace strings
214
}  // namespace theory
215
}  // namespace cvc5
216
217
#endif /* CVC5__THEORY__STRINGS__SKOLEM_CACHE_H */