GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/skolem_cache.cpp Lines: 127 139 91.4 %
Date: 2021-08-01 Branches: 288 591 48.7 %

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
11747
SkolemCache::SkolemCache(bool useOpts) : d_useOpts(useOpts)
54
{
55
11747
  NodeManager* nm = NodeManager::currentNM();
56
11747
  d_strType = nm->stringType();
57
11747
  d_zero = nm->mkConst(Rational(0));
58
11747
}
59
60
33801
Node SkolemCache::mkSkolemCached(Node a, Node b, SkolemId id, const char* c)
61
{
62
33801
  return mkTypedSkolemCached(d_strType, a, b, id, c);
63
}
64
65
15520
Node SkolemCache::mkSkolemCached(Node a, SkolemId id, const char* c)
66
{
67
15520
  return mkSkolemCached(a, Node::null(), id, c);
68
}
69
70
34227
Node SkolemCache::mkTypedSkolemCached(
71
    TypeNode tn, Node a, Node b, SkolemId id, const char* c)
72
{
73
68454
  Trace("skolem-cache") << "mkTypedSkolemCached start: (" << id << ", " << a
74
34227
                        << ", " << b << ")" << std::endl;
75
34227
  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
34227
  if (d_useOpts)
79
  {
80
30077
    a = a.isNull() ? a : Rewriter::rewrite(a);
81
30077
    b = b.isNull() ? b : Rewriter::rewrite(b);
82
  }
83
34227
  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
34227
  if (d_useOpts && idOrig != SK_PURIFY && id == SK_PURIFY && a.isConst())
88
  {
89
1514
    Trace("skolem-cache") << "...optimization: return constant " << a
90
757
                          << std::endl;
91
757
    return a;
92
  }
93
94
33470
  std::map<SkolemId, Node>::iterator it = d_skolemCache[a][b].find(id);
95
33470
  if (it != d_skolemCache[a][b].end())
96
  {
97
15862
    Trace("skolem-cache") << "...return existing " << it->second << std::endl;
98
    // already cached
99
15862
    return it->second;
100
  }
101
102
17608
  NodeManager* nm = NodeManager::currentNM();
103
17608
  SkolemManager* sm = nm->getSkolemManager();
104
35216
  Node sk;
105
17608
  switch (id)
106
  {
107
    // exists k. k = a
108
17404
    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
34808
      int flags = a.getType().isBoolean() ? NodeManager::SKOLEM_BOOL_TERM_VAR
113
17404
                                          : NodeManager::SKOLEM_DEFAULT;
114
17404
      sk = sm->mkPurifySkolem(a, c, "string purify skolem", flags);
115
    }
116
17404
    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
17608
  Trace("skolem-cache") << "...returned " << sk << std::endl;
146
17608
  d_allSkolems.insert(sk);
147
17608
  d_skolemCache[a][b][id] = sk;
148
17608
  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
1110
Node SkolemCache::mkSkolem(const char* c)
175
{
176
  // TODO: eliminate this
177
1110
  SkolemManager* sm = NodeManager::currentNM()->getSkolemManager();
178
1110
  Node n = sm->mkDummySkolem(c, d_strType, "string skolem");
179
1110
  d_allSkolems.insert(n);
180
1110
  return n;
181
}
182
183
4640
bool SkolemCache::isSkolem(Node n) const
184
{
185
4640
  return d_allSkolems.find(n) != d_allSkolems.end();
186
}
187
188
std::tuple<SkolemCache::SkolemId, Node, Node>
189
34227
SkolemCache::normalizeStringSkolem(SkolemId id, Node a, Node b)
190
{
191
192
34227
  NodeManager* nm = NodeManager::currentNM();
193
194
  // eliminate in terms of prefix/suffix_rem
195
34227
  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
3514
    id = SK_SUFFIX_REM;
200
7028
    Node pre = mkSkolemCached(a, b, SK_FIRST_CTN_PRE, "pre");
201
10542
    b = nm->mkNode(
202
14056
        PLUS, nm->mkNode(STRING_LENGTH, pre), nm->mkNode(STRING_LENGTH, b));
203
  }
204
30713
  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
497
    id = SK_SUFFIX_REM;
208
497
    b = nm->mkNode(STRING_LENGTH, b);
209
  }
210
30216
  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
487
    id = SK_PREFIX;
214
1948
    b = nm->mkNode(
215
1948
        MINUS, nm->mkNode(STRING_LENGTH, a), nm->mkNode(STRING_LENGTH, b));
216
  }
217
29729
  else if (id == SK_ID_VC_SPT)
218
  {
219
    // SK_ID_VC_SPT(x, y) ---> SK_SUFFIX_REM(x, 1)
220
2015
    id = SK_SUFFIX_REM;
221
2015
    b = nm->mkConst(Rational(1));
222
  }
223
27714
  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
3027
    id = SK_PREFIX;
227
9081
    b = nm->mkNode(
228
12108
        MINUS, nm->mkNode(STRING_LENGTH, a), nm->mkConst(Rational(1)));
229
  }
230
24687
  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
24687
  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
24687
  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
24687
  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
24687
  else if (id == SK_FIRST_CTN_PRE)
257
  {
258
    // SK_FIRST_CTN_PRE(x,y) ---> SK_PREFIX(x, indexof(x,y,0))
259
7032
    id = SK_PREFIX;
260
7032
    b = nm->mkNode(STRING_INDEXOF, a, b, d_zero);
261
  }
262
263
34227
  if (id == SK_ID_V_UNIFIED_SPT || id == SK_ID_V_UNIFIED_SPT_REV)
264
  {
265
3232
    bool isRev = (id == SK_ID_V_UNIFIED_SPT_REV);
266
6464
    Node la = nm->mkNode(STRING_LENGTH, a);
267
6464
    Node lb = nm->mkNode(STRING_LENGTH, b);
268
4547
    Node ta = isRev ? utils::mkPrefix(a, nm->mkNode(MINUS, la, lb))
269
7779
                    : utils::mkSuffix(a, lb);
270
4547
    Node tb = isRev ? utils::mkPrefix(b, nm->mkNode(MINUS, lb, la))
271
7779
                    : utils::mkSuffix(b, la);
272
3232
    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
3232
    a = nm->mkNode(ITE, nm->mkNode(GEQ, la, lb), ta, tb);
276
3232
    b = Node::null();
277
  }
278
279
  // now, eliminate prefix/suffix_rem in terms of purify
280
34227
  if (id == SK_PREFIX)
281
  {
282
    // SK_PREFIX(x,y) ---> SK_PURIFY(substr(x,0,y))
283
12247
    id = SK_PURIFY;
284
12247
    a = utils::mkPrefix(a, b);
285
12247
    b = Node::null();
286
  }
287
21980
  else if (id == SK_SUFFIX_REM)
288
  {
289
    // SK_SUFFIX_REM(x,y) ---> SK_PURIFY(substr(x,y,str.len(x)-y))
290
7727
    id = SK_PURIFY;
291
7727
    a = utils::mkSuffix(a, b);
292
7727
    b = Node::null();
293
  }
294
295
34227
  if (d_useOpts)
296
  {
297
30077
    a = a.isNull() ? a : Rewriter::rewrite(a);
298
30077
    b = b.isNull() ? b : Rewriter::rewrite(b);
299
  }
300
68454
  Trace("skolem-cache") << "normalizeStringSkolem end: (" << id << ", " << a
301
34227
                        << ", " << b << ")" << std::endl;
302
34227
  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
29280
}  // namespace cvc5