GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/skolem_cache.cpp Lines: 127 139 91.4 %
Date: 2021-08-17 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
12096
SkolemCache::SkolemCache(bool useOpts) : d_useOpts(useOpts)
54
{
55
12096
  NodeManager* nm = NodeManager::currentNM();
56
12096
  d_strType = nm->stringType();
57
12096
  d_zero = nm->mkConst(Rational(0));
58
12096
}
59
60
39700
Node SkolemCache::mkSkolemCached(Node a, Node b, SkolemId id, const char* c)
61
{
62
39700
  return mkTypedSkolemCached(d_strType, a, b, id, c);
63
}
64
65
16188
Node SkolemCache::mkSkolemCached(Node a, SkolemId id, const char* c)
66
{
67
16188
  return mkSkolemCached(a, Node::null(), id, c);
68
}
69
70
40126
Node SkolemCache::mkTypedSkolemCached(
71
    TypeNode tn, Node a, Node b, SkolemId id, const char* c)
72
{
73
80252
  Trace("skolem-cache") << "mkTypedSkolemCached start: (" << id << ", " << a
74
40126
                        << ", " << b << ")" << std::endl;
75
40126
  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
40126
  if (d_useOpts)
79
  {
80
35034
    a = a.isNull() ? a : Rewriter::rewrite(a);
81
35034
    b = b.isNull() ? b : Rewriter::rewrite(b);
82
  }
83
40126
  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
40126
  if (d_useOpts && idOrig != SK_PURIFY && id == SK_PURIFY && a.isConst())
88
  {
89
1580
    Trace("skolem-cache") << "...optimization: return constant " << a
90
790
                          << std::endl;
91
790
    return a;
92
  }
93
94
39336
  std::map<SkolemId, Node>::iterator it = d_skolemCache[a][b].find(id);
95
39336
  if (it != d_skolemCache[a][b].end())
96
  {
97
20240
    Trace("skolem-cache") << "...return existing " << it->second << std::endl;
98
    // already cached
99
20240
    return it->second;
100
  }
101
102
19096
  NodeManager* nm = NodeManager::currentNM();
103
19096
  SkolemManager* sm = nm->getSkolemManager();
104
38192
  Node sk;
105
19096
  switch (id)
106
  {
107
    // exists k. k = a
108
18892
    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
37784
      int flags = a.getType().isBoolean() ? NodeManager::SKOLEM_BOOL_TERM_VAR
113
18892
                                          : NodeManager::SKOLEM_DEFAULT;
114
18892
      sk = sm->mkPurifySkolem(a, c, "string purify skolem", flags);
115
    }
116
18892
    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
204
    case SK_NUM_OCCUR:
135
    case SK_OCCUR_INDEX:
136
    default:
137
    {
138
204
      Notice() << "Don't know how to handle Skolem ID " << id << std::endl;
139
408
      Node v = nm->mkBoundVar(tn);
140
408
      Node cond = nm->mkConst(true);
141
408
      sk = sm->mkSkolem(v, cond, c, "string skolem");
142
    }
143
204
    break;
144
  }
145
19096
  Trace("skolem-cache") << "...returned " << sk << std::endl;
146
19096
  d_allSkolems.insert(sk);
147
19096
  d_skolemCache[a][b][id] = sk;
148
19096
  return sk;
149
}
150
323
Node SkolemCache::mkTypedSkolemCached(TypeNode tn,
151
                                      Node a,
152
                                      SkolemId id,
153
                                      const char* c)
154
{
155
323
  return mkTypedSkolemCached(tn, a, Node::null(), id, c);
156
}
157
158
58
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
58
  Assert(seqType.isSequence());
164
58
  NodeManager* nm = NodeManager::currentNM();
165
58
  SkolemManager* sm = nm->getSkolemManager();
166
116
  std::vector<TypeNode> argTypes;
167
58
  argTypes.push_back(seqType);
168
58
  argTypes.push_back(nm->integerType());
169
116
  TypeNode elemType = seqType.getSequenceElementType();
170
116
  TypeNode ufType = nm->mkFunctionType(argTypes, elemType);
171
116
  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
4648
bool SkolemCache::isSkolem(Node n) const
184
{
185
4648
  return d_allSkolems.find(n) != d_allSkolems.end();
186
}
187
188
std::tuple<SkolemCache::SkolemId, Node, Node>
189
40126
SkolemCache::normalizeStringSkolem(SkolemId id, Node a, Node b)
190
{
191
192
40126
  NodeManager* nm = NodeManager::currentNM();
193
194
  // eliminate in terms of prefix/suffix_rem
195
40126
  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
5094
    id = SK_SUFFIX_REM;
200
10188
    Node pre = mkSkolemCached(a, b, SK_FIRST_CTN_PRE, "pre");
201
15282
    b = nm->mkNode(
202
20376
        PLUS, nm->mkNode(STRING_LENGTH, pre), nm->mkNode(STRING_LENGTH, b));
203
  }
204
35032
  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
499
    id = SK_SUFFIX_REM;
208
499
    b = nm->mkNode(STRING_LENGTH, b);
209
  }
210
34533
  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
479
    id = SK_PREFIX;
214
1916
    b = nm->mkNode(
215
1916
        MINUS, nm->mkNode(STRING_LENGTH, a), nm->mkNode(STRING_LENGTH, b));
216
  }
217
34054
  else if (id == SK_ID_VC_SPT)
218
  {
219
    // SK_ID_VC_SPT(x, y) ---> SK_SUFFIX_REM(x, 1)
220
2065
    id = SK_SUFFIX_REM;
221
2065
    b = nm->mkConst(Rational(1));
222
  }
223
31989
  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
3106
    id = SK_PREFIX;
227
9318
    b = nm->mkNode(
228
12424
        MINUS, nm->mkNode(STRING_LENGTH, a), nm->mkConst(Rational(1)));
229
  }
230
28883
  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
28883
  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
28883
  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
28883
  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
28883
  else if (id == SK_FIRST_CTN_PRE)
257
  {
258
    // SK_FIRST_CTN_PRE(x,y) ---> SK_PREFIX(x, indexof(x,y,0))
259
10192
    id = SK_PREFIX;
260
10192
    b = nm->mkNode(STRING_INDEXOF, a, b, d_zero);
261
  }
262
263
40126
  if (id == SK_ID_V_UNIFIED_SPT || id == SK_ID_V_UNIFIED_SPT_REV)
264
  {
265
3573
    bool isRev = (id == SK_ID_V_UNIFIED_SPT_REV);
266
7146
    Node la = nm->mkNode(STRING_LENGTH, a);
267
7146
    Node lb = nm->mkNode(STRING_LENGTH, b);
268
5095
    Node ta = isRev ? utils::mkPrefix(a, nm->mkNode(MINUS, la, lb))
269
8668
                    : utils::mkSuffix(a, lb);
270
5095
    Node tb = isRev ? utils::mkPrefix(b, nm->mkNode(MINUS, lb, la))
271
8668
                    : utils::mkSuffix(b, la);
272
3573
    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
3573
    a = nm->mkNode(ITE, nm->mkNode(GEQ, la, lb), ta, tb);
276
3573
    b = Node::null();
277
  }
278
279
  // now, eliminate prefix/suffix_rem in terms of purify
280
40126
  if (id == SK_PREFIX)
281
  {
282
    // SK_PREFIX(x,y) ---> SK_PURIFY(substr(x,0,y))
283
15556
    id = SK_PURIFY;
284
15556
    a = utils::mkPrefix(a, b);
285
15556
    b = Node::null();
286
  }
287
24570
  else if (id == SK_SUFFIX_REM)
288
  {
289
    // SK_SUFFIX_REM(x,y) ---> SK_PURIFY(substr(x,y,str.len(x)-y))
290
9437
    id = SK_PURIFY;
291
9437
    a = utils::mkSuffix(a, b);
292
9437
    b = Node::null();
293
  }
294
295
40126
  if (d_useOpts)
296
  {
297
35034
    a = a.isNull() ? a : Rewriter::rewrite(a);
298
35034
    b = b.isNull() ? b : Rewriter::rewrite(b);
299
  }
300
80252
  Trace("skolem-cache") << "normalizeStringSkolem end: (" << id << ", " << a
301
40126
                        << ", " << b << ")" << std::endl;
302
40126
  return std::make_tuple(id, a, b);
303
}
304
305
415
Node SkolemCache::mkIndexVar(Node t)
306
{
307
415
  NodeManager* nm = NodeManager::currentNM();
308
830
  TypeNode intType = nm->integerType();
309
415
  BoundVarManager* bvm = nm->getBoundVarManager();
310
830
  return bvm->mkBoundVar<IndexVarAttribute>(t, intType);
311
}
312
313
182
Node SkolemCache::mkLengthVar(Node t)
314
{
315
182
  NodeManager* nm = NodeManager::currentNM();
316
364
  TypeNode intType = nm->integerType();
317
182
  BoundVarManager* bvm = nm->getBoundVarManager();
318
364
  return bvm->mkBoundVar<LengthVarAttribute>(t, intType);
319
}
320
321
}  // namespace strings
322
}  // namespace theory
323
29337
}  // namespace cvc5