GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/skolem_cache.cpp Lines: 127 139 91.4 %
Date: 2021-09-18 Branches: 288 589 48.9 %

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
 * Implementation of a cache of skolems for theory of strings.
14
 */
15
16
#include "theory/strings/skolem_cache.h"
17
18
#include "expr/attribute.h"
19
#include "expr/bound_var_manager.h"
20
#include "expr/skolem_manager.h"
21
#include "theory/rewriter.h"
22
#include "theory/strings/arith_entail.h"
23
#include "theory/strings/theory_strings_utils.h"
24
#include "theory/strings/word.h"
25
#include "util/rational.h"
26
27
using namespace cvc5::kind;
28
29
namespace cvc5 {
30
namespace theory {
31
namespace strings {
32
33
/**
34
 * A bound variable corresponding to the universally quantified integer
35
 * variable used to range over the valid positions in a string, used
36
 * for axiomatizing the behavior of some term.
37
 */
38
struct IndexVarAttributeId
39
{
40
};
41
typedef expr::Attribute<IndexVarAttributeId, Node> IndexVarAttribute;
42
43
/**
44
 * A bound variable corresponding to the universally quantified integer
45
 * variable used to range over the valid lengths of a string, used for
46
 * axiomatizing the behavior of some term.
47
 */
48
struct LengthVarAttributeId
49
{
50
};
51
typedef expr::Attribute<LengthVarAttributeId, Node> LengthVarAttribute;
52
53
11025
SkolemCache::SkolemCache(bool useOpts) : d_useOpts(useOpts)
54
{
55
11025
  NodeManager* nm = NodeManager::currentNM();
56
11025
  d_strType = nm->stringType();
57
11025
  d_zero = nm->mkConst(Rational(0));
58
11025
}
59
60
40402
Node SkolemCache::mkSkolemCached(Node a, Node b, SkolemId id, const char* c)
61
{
62
40402
  return mkTypedSkolemCached(d_strType, a, b, id, c);
63
}
64
65
17398
Node SkolemCache::mkSkolemCached(Node a, SkolemId id, const char* c)
66
{
67
17398
  return mkSkolemCached(a, Node::null(), id, c);
68
}
69
70
40833
Node SkolemCache::mkTypedSkolemCached(
71
    TypeNode tn, Node a, Node b, SkolemId id, const char* c)
72
{
73
81666
  Trace("skolem-cache") << "mkTypedSkolemCached start: (" << id << ", " << a
74
40833
                        << ", " << b << ")" << std::endl;
75
40833
  SkolemId idOrig = id;
76
  // do not rewrite beforehand if we are not using optimizations, this is so
77
  // that the proof checker does not depend on the rewriter.
78
40833
  if (d_useOpts)
79
  {
80
38762
    a = a.isNull() ? a : Rewriter::rewrite(a);
81
38762
    b = b.isNull() ? b : Rewriter::rewrite(b);
82
  }
83
40833
  std::tie(id, a, b) = normalizeStringSkolem(id, a, b);
84
85
  // optimization: if we aren't asking for the purification skolem for constant
86
  // a, and the skolem is equivalent to a, then we just return a.
87
40833
  if (d_useOpts && idOrig != SK_PURIFY && id == SK_PURIFY && a.isConst())
88
  {
89
1708
    Trace("skolem-cache") << "...optimization: return constant " << a
90
854
                          << std::endl;
91
854
    return a;
92
  }
93
94
39979
  std::map<SkolemId, Node>::iterator it = d_skolemCache[a][b].find(id);
95
39979
  if (it != d_skolemCache[a][b].end())
96
  {
97
21936
    Trace("skolem-cache") << "...return existing " << it->second << std::endl;
98
    // already cached
99
21936
    return it->second;
100
  }
101
102
18043
  NodeManager* nm = NodeManager::currentNM();
103
18043
  SkolemManager* sm = nm->getSkolemManager();
104
36086
  Node sk;
105
18043
  switch (id)
106
  {
107
    // exists k. k = a
108
17848
    case SK_PURIFY:
109
    {
110
      // for sequences of Booleans, we may purify Boolean terms, in which case
111
      // they must be Boolean term variables.
112
35696
      int flags = a.getType().isBoolean() ? NodeManager::SKOLEM_BOOL_TERM_VAR
113
17848
                                          : NodeManager::SKOLEM_DEFAULT;
114
17848
      sk = sm->mkPurifySkolem(a, c, "string purify skolem", flags);
115
    }
116
17848
    break;
117
    // these are eliminated by normalizeStringSkolem
118
    case SK_ID_V_SPT:
119
    case SK_ID_V_SPT_REV:
120
    case SK_ID_VC_SPT:
121
    case SK_ID_VC_SPT_REV:
122
    case SK_FIRST_CTN_POST:
123
    case SK_ID_C_SPT:
124
    case SK_ID_C_SPT_REV:
125
    case SK_ID_DC_SPT:
126
    case SK_ID_DC_SPT_REM:
127
    case SK_ID_DEQ_X:
128
    case SK_ID_DEQ_Y:
129
    case SK_FIRST_CTN_PRE:
130
    case SK_PREFIX:
131
    case SK_SUFFIX_REM:
132
      Unhandled() << "Expected to eliminate Skolem ID " << id << std::endl;
133
      break;
134
195
    case SK_NUM_OCCUR:
135
    case SK_OCCUR_INDEX:
136
    default:
137
    {
138
195
      Notice() << "Don't know how to handle Skolem ID " << id << std::endl;
139
390
      Node v = nm->mkBoundVar(tn);
140
390
      Node cond = nm->mkConst(true);
141
390
      sk = sm->mkSkolem(v, cond, c, "string skolem");
142
    }
143
195
    break;
144
  }
145
18043
  Trace("skolem-cache") << "...returned " << sk << std::endl;
146
18043
  d_allSkolems.insert(sk);
147
18043
  d_skolemCache[a][b][id] = sk;
148
18043
  return sk;
149
}
150
331
Node SkolemCache::mkTypedSkolemCached(TypeNode tn,
151
                                      Node a,
152
                                      SkolemId id,
153
                                      const char* c)
154
{
155
331
  return mkTypedSkolemCached(tn, a, Node::null(), id, c);
156
}
157
158
52
Node SkolemCache::mkSkolemSeqNth(TypeNode seqType, const char* c)
159
{
160
  // Note this method is static and does not rely on any local caching.
161
  // It is used by expand definitions and by (dynamic) reductions, thus
162
  // it is centrally located here.
163
52
  Assert(seqType.isSequence());
164
52
  NodeManager* nm = NodeManager::currentNM();
165
52
  SkolemManager* sm = nm->getSkolemManager();
166
104
  std::vector<TypeNode> argTypes;
167
52
  argTypes.push_back(seqType);
168
52
  argTypes.push_back(nm->integerType());
169
104
  TypeNode elemType = seqType.getSequenceElementType();
170
104
  TypeNode ufType = nm->mkFunctionType(argTypes, elemType);
171
104
  return sm->mkSkolemFunction(SkolemFunId::SEQ_NTH_OOB, ufType);
172
}
173
174
1170
Node SkolemCache::mkSkolem(const char* c)
175
{
176
  // TODO: eliminate this
177
1170
  SkolemManager* sm = NodeManager::currentNM()->getSkolemManager();
178
1170
  Node n = sm->mkDummySkolem(c, d_strType, "string skolem");
179
1170
  d_allSkolems.insert(n);
180
1170
  return n;
181
}
182
183
4636
bool SkolemCache::isSkolem(Node n) const
184
{
185
4636
  return d_allSkolems.find(n) != d_allSkolems.end();
186
}
187
188
std::tuple<SkolemCache::SkolemId, Node, Node>
189
40833
SkolemCache::normalizeStringSkolem(SkolemId id, Node a, Node b)
190
{
191
192
40833
  NodeManager* nm = NodeManager::currentNM();
193
194
  // eliminate in terms of prefix/suffix_rem
195
40833
  if (id == SK_FIRST_CTN_POST)
196
  {
197
    // SK_FIRST_CTN_POST(x, y) --->
198
    //   SK_SUFFIX_REM(x, (+ (str.len SK_FIRST_CTN_PRE(x, y)) (str.len y)))
199
4493
    id = SK_SUFFIX_REM;
200
8986
    Node pre = mkSkolemCached(a, b, SK_FIRST_CTN_PRE, "pre");
201
13479
    b = nm->mkNode(
202
17972
        PLUS, nm->mkNode(STRING_LENGTH, pre), nm->mkNode(STRING_LENGTH, b));
203
  }
204
36340
  else if (id == SK_ID_V_SPT || id == SK_ID_C_SPT)
205
  {
206
    // SK_ID_*_SPT(x, y) ---> SK_SUFFIX_REM(x, (str.len y))
207
506
    id = SK_SUFFIX_REM;
208
506
    b = nm->mkNode(STRING_LENGTH, b);
209
  }
210
35834
  else if (id == SK_ID_V_SPT_REV || id == SK_ID_C_SPT_REV)
211
  {
212
    // SK_ID_*_SPT_REV(x, y) ---> SK_PREFIX(x, (- (str.len x) (str.len y)))
213
481
    id = SK_PREFIX;
214
1924
    b = nm->mkNode(
215
1924
        MINUS, nm->mkNode(STRING_LENGTH, a), nm->mkNode(STRING_LENGTH, b));
216
  }
217
35353
  else if (id == SK_ID_VC_SPT)
218
  {
219
    // SK_ID_VC_SPT(x, y) ---> SK_SUFFIX_REM(x, 1)
220
2623
    id = SK_SUFFIX_REM;
221
2623
    b = nm->mkConst(Rational(1));
222
  }
223
32730
  else if (id == SK_ID_VC_SPT_REV)
224
  {
225
    // SK_ID_VC_SPT_REV(x, y) ---> SK_PREFIX(x, (- (str.len x) 1))
226
3154
    id = SK_PREFIX;
227
9462
    b = nm->mkNode(
228
12616
        MINUS, nm->mkNode(STRING_LENGTH, a), nm->mkConst(Rational(1)));
229
  }
230
29576
  else if (id == SK_ID_DC_SPT)
231
  {
232
    // SK_ID_DC_SPT(x, y) ---> SK_PREFIX(x, 1)
233
    id = SK_PREFIX;
234
    b = nm->mkConst(Rational(1));
235
  }
236
29576
  else if (id == SK_ID_DC_SPT_REM)
237
  {
238
    // SK_ID_DC_SPT_REM(x, y) ---> SK_SUFFIX_REM(x, 1)
239
    id = SK_SUFFIX_REM;
240
    b = nm->mkConst(Rational(1));
241
  }
242
29576
  else if (id == SK_ID_DEQ_X)
243
  {
244
    // SK_ID_DEQ_X(x, y) ---> SK_PREFIX(y, (str.len x))
245
    id = SK_PREFIX;
246
    Node aOld = a;
247
    a = b;
248
    b = nm->mkNode(STRING_LENGTH, aOld);
249
  }
250
29576
  else if (id == SK_ID_DEQ_Y)
251
  {
252
    // SK_ID_DEQ_Y(x, y) ---> SK_PREFIX(x, (str.len y))
253
    id = SK_PREFIX;
254
    b = nm->mkNode(STRING_LENGTH, b);
255
  }
256
29576
  else if (id == SK_FIRST_CTN_PRE)
257
  {
258
    // SK_FIRST_CTN_PRE(x,y) ---> SK_PREFIX(x, indexof(x,y,0))
259
8990
    id = SK_PREFIX;
260
8990
    b = nm->mkNode(STRING_INDEXOF, a, b, d_zero);
261
  }
262
263
40833
  if (id == SK_ID_V_UNIFIED_SPT || id == SK_ID_V_UNIFIED_SPT_REV)
264
  {
265
4681
    bool isRev = (id == SK_ID_V_UNIFIED_SPT_REV);
266
9362
    Node la = nm->mkNode(STRING_LENGTH, a);
267
9362
    Node lb = nm->mkNode(STRING_LENGTH, b);
268
6700
    Node ta = isRev ? utils::mkPrefix(a, nm->mkNode(MINUS, la, lb))
269
11381
                    : utils::mkSuffix(a, lb);
270
6700
    Node tb = isRev ? utils::mkPrefix(b, nm->mkNode(MINUS, lb, la))
271
11381
                    : utils::mkSuffix(b, la);
272
4681
    id = SK_PURIFY;
273
    // SK_ID_V_UNIFIED_SPT(x,y) --->
274
    //   ite(len(x) >= len(y), substr(x,0,str.len(y)), substr(y,0,str.len(x))
275
4681
    a = nm->mkNode(ITE, nm->mkNode(GEQ, la, lb), ta, tb);
276
4681
    b = Node::null();
277
  }
278
279
  // now, eliminate prefix/suffix_rem in terms of purify
280
40833
  if (id == SK_PREFIX)
281
  {
282
    // SK_PREFIX(x,y) ---> SK_PURIFY(substr(x,0,y))
283
14496
    id = SK_PURIFY;
284
14496
    a = utils::mkPrefix(a, b);
285
14496
    b = Node::null();
286
  }
287
26337
  else if (id == SK_SUFFIX_REM)
288
  {
289
    // SK_SUFFIX_REM(x,y) ---> SK_PURIFY(substr(x,y,str.len(x)-y))
290
9493
    id = SK_PURIFY;
291
9493
    a = utils::mkSuffix(a, b);
292
9493
    b = Node::null();
293
  }
294
295
40833
  if (d_useOpts)
296
  {
297
38762
    a = a.isNull() ? a : Rewriter::rewrite(a);
298
38762
    b = b.isNull() ? b : Rewriter::rewrite(b);
299
  }
300
81666
  Trace("skolem-cache") << "normalizeStringSkolem end: (" << id << ", " << a
301
40833
                        << ", " << b << ")" << std::endl;
302
40833
  return std::make_tuple(id, a, b);
303
}
304
305
420
Node SkolemCache::mkIndexVar(Node t)
306
{
307
420
  NodeManager* nm = NodeManager::currentNM();
308
840
  TypeNode intType = nm->integerType();
309
420
  BoundVarManager* bvm = nm->getBoundVarManager();
310
840
  return bvm->mkBoundVar<IndexVarAttribute>(t, intType);
311
}
312
313
161
Node SkolemCache::mkLengthVar(Node t)
314
{
315
161
  NodeManager* nm = NodeManager::currentNM();
316
322
  TypeNode intType = nm->integerType();
317
161
  BoundVarManager* bvm = nm->getBoundVarManager();
318
322
  return bvm->mkBoundVar<LengthVarAttribute>(t, intType);
319
}
320
321
}  // namespace strings
322
}  // namespace theory
323
29574
}  // namespace cvc5