GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/extf_solver.cpp Lines: 305 362 84.3 %
Date: 2021-11-07 Branches: 789 1786 44.2 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Gereon Kremer, Andres Noetzli
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 solver for extended functions of theory of strings.
14
 */
15
16
#include "theory/strings/extf_solver.h"
17
18
#include "options/strings_options.h"
19
#include "theory/strings/sequences_rewriter.h"
20
#include "theory/strings/theory_strings_preprocess.h"
21
#include "theory/strings/theory_strings_utils.h"
22
#include "util/statistics_registry.h"
23
24
using namespace std;
25
using namespace cvc5::context;
26
using namespace cvc5::kind;
27
28
namespace cvc5 {
29
namespace theory {
30
namespace strings {
31
32
15273
ExtfSolver::ExtfSolver(Env& env,
33
                       SolverState& s,
34
                       InferenceManager& im,
35
                       TermRegistry& tr,
36
                       StringsRewriter& rewriter,
37
                       BaseSolver& bs,
38
                       CoreSolver& cs,
39
                       ExtTheory& et,
40
15273
                       SequencesStatistics& statistics)
41
    : EnvObj(env),
42
      d_state(s),
43
      d_im(im),
44
      d_termReg(tr),
45
      d_rewriter(rewriter),
46
      d_bsolver(bs),
47
      d_csolver(cs),
48
      d_extt(et),
49
      d_statistics(statistics),
50
15273
      d_preproc(d_termReg.getSkolemCache(), &statistics.d_reductions),
51
      d_hasExtf(context(), false),
52
      d_extfInferCache(context()),
53
30546
      d_reduced(userContext())
54
{
55
15273
  d_extt.addFunctionKind(kind::STRING_SUBSTR);
56
15273
  d_extt.addFunctionKind(kind::STRING_UPDATE);
57
15273
  d_extt.addFunctionKind(kind::STRING_INDEXOF);
58
15273
  d_extt.addFunctionKind(kind::STRING_INDEXOF_RE);
59
15273
  d_extt.addFunctionKind(kind::STRING_ITOS);
60
15273
  d_extt.addFunctionKind(kind::STRING_STOI);
61
15273
  d_extt.addFunctionKind(kind::STRING_REPLACE);
62
15273
  d_extt.addFunctionKind(kind::STRING_REPLACE_ALL);
63
15273
  d_extt.addFunctionKind(kind::STRING_REPLACE_RE);
64
15273
  d_extt.addFunctionKind(kind::STRING_REPLACE_RE_ALL);
65
15273
  d_extt.addFunctionKind(kind::STRING_CONTAINS);
66
15273
  d_extt.addFunctionKind(kind::STRING_IN_REGEXP);
67
15273
  d_extt.addFunctionKind(kind::STRING_LEQ);
68
15273
  d_extt.addFunctionKind(kind::STRING_TO_CODE);
69
15273
  d_extt.addFunctionKind(kind::STRING_TOLOWER);
70
15273
  d_extt.addFunctionKind(kind::STRING_TOUPPER);
71
15273
  d_extt.addFunctionKind(kind::STRING_REV);
72
15273
  d_extt.addFunctionKind(kind::SEQ_UNIT);
73
15273
  d_extt.addFunctionKind(kind::SEQ_NTH);
74
75
15273
  d_true = NodeManager::currentNM()->mkConst(true);
76
15273
  d_false = NodeManager::currentNM()->mkConst(false);
77
15273
}
78
79
15268
ExtfSolver::~ExtfSolver() {}
80
81
214482
bool ExtfSolver::doReduction(int effort, Node n)
82
{
83
214482
  Assert(d_extfInfoTmp.find(n) != d_extfInfoTmp.end());
84
214482
  if (!d_extfInfoTmp[n].d_modelActive)
85
  {
86
    // n is not active in the model, no need to reduce
87
  Trace("strings-extf-debug") << "...skip due to model active" << std::endl;
88
    return false;
89
  }
90
214482
  if (d_reduced.find(n)!=d_reduced.end())
91
  {
92
    // already sent a reduction lemma
93
138236
    Trace("strings-extf-debug") << "...skip due to reduced" << std::endl;
94
138236
    return false;
95
  }
96
  // determine the effort level to process the extf at
97
  // 0 - at assertion time, 1+ - after no other reduction is applicable
98
76246
  int r_effort = -1;
99
  // polarity : 1 true, -1 false, 0 neither
100
76246
  int pol = 0;
101
76246
  Kind k = n.getKind();
102
76246
  if (n.getType().isBoolean() && !d_extfInfoTmp[n].d_const.isNull())
103
  {
104
36343
    pol = d_extfInfoTmp[n].d_const.getConst<bool>() ? 1 : -1;
105
  }
106
76246
  if (k == STRING_CONTAINS)
107
  {
108
12619
    if (pol == 1)
109
    {
110
4076
      r_effort = 1;
111
    }
112
8543
    else if (pol == -1)
113
    {
114
7053
      if (effort == 2)
115
      {
116
216
        Node x = n[0];
117
216
        Node s = n[1];
118
216
        std::vector<Node> lexp;
119
216
        Node lenx = d_state.getLength(x, lexp);
120
216
        Node lens = d_state.getLength(s, lexp);
121
116
        if (d_state.areEqual(lenx, lens))
122
        {
123
32
          Trace("strings-extf-debug")
124
16
              << "  resolve extf : " << n
125
16
              << " based on equal lengths disequality." << std::endl;
126
          // We can reduce negative contains to a disequality when lengths are
127
          // equal. In other words, len( x ) = len( s ) implies
128
          //   ~contains( x, s ) reduces to x != s.
129
16
          if (!d_state.areDisequal(x, s))
130
          {
131
            // len( x ) = len( s ) ^ ~contains( x, s ) => x != s
132
8
            lexp.push_back(lenx.eqNode(lens));
133
8
            lexp.push_back(n.negate());
134
16
            Node xneqs = x.eqNode(s).negate();
135
8
            d_im.sendInference(
136
                lexp, xneqs, InferenceId::STRINGS_CTN_NEG_EQUAL, false, true);
137
          }
138
          // this depends on the current assertions, so this
139
          // inference is context-dependent
140
16
          d_extt.markReduced(n, ExtReducedId::STRINGS_NEG_CTN_DEQ, true);
141
16
          return true;
142
        }
143
        else
144
        {
145
100
          r_effort = 2;
146
        }
147
      }
148
    }
149
  }
150
63627
  else if (k == STRING_SUBSTR)
151
  {
152
1444
    r_effort = 1;
153
  }
154
62183
  else if (k == SEQ_UNIT)
155
  {
156
    // never necessary to reduce seq.unit
157
278
    return false;
158
  }
159
61905
  else if (k != STRING_IN_REGEXP)
160
  {
161
36193
    r_effort = 2;
162
  }
163
75952
  if (effort != r_effort)
164
  {
165
68672
    Trace("strings-extf-debug") << "...skip due to effort" << std::endl;
166
    // not the right effort level to reduce
167
68672
    return false;
168
  }
169
14560
  Node c_n = pol == -1 ? n.negate() : n;
170
14560
  Trace("strings-process-debug")
171
7280
      << "Process reduction for " << n << ", pol = " << pol << std::endl;
172
7280
  if (k == STRING_CONTAINS && pol == 1)
173
  {
174
8152
    Node x = n[0];
175
8152
    Node s = n[1];
176
    // positive contains reduces to a equality
177
4076
    SkolemCache* skc = d_termReg.getSkolemCache();
178
8152
    Node eq = d_termReg.eagerReduce(n, skc, d_termReg.getAlphabetCardinality());
179
4076
    Assert(!eq.isNull());
180
4076
    Assert(eq.getKind() == ITE && eq[0] == n);
181
4076
    eq = eq[1];
182
8152
    std::vector<Node> expn;
183
4076
    expn.push_back(n);
184
4076
    d_im.sendInference(expn, expn, eq, InferenceId::STRINGS_CTN_POS, false, true);
185
8152
    Trace("strings-extf-debug")
186
4076
        << "  resolve extf : " << n << " based on positive contain reduction."
187
4076
        << std::endl;
188
8152
    Trace("strings-red-lemma") << "Reduction (positive contains) lemma : " << n
189
4076
                               << " => " << eq << std::endl;
190
    // context-dependent because it depends on the polarity of n itself
191
8152
    d_extt.markReduced(n, ExtReducedId::STRINGS_POS_CTN, true);
192
  }
193
3204
  else if (k != kind::STRING_TO_CODE)
194
  {
195
2167
    NodeManager* nm = NodeManager::currentNM();
196
2167
    Assert(k == STRING_SUBSTR || k == STRING_UPDATE || k == STRING_CONTAINS
197
           || k == STRING_INDEXOF || k == STRING_INDEXOF_RE || k == STRING_ITOS
198
           || k == STRING_STOI || k == STRING_REPLACE || k == STRING_REPLACE_ALL
199
           || k == SEQ_NTH || k == STRING_REPLACE_RE
200
           || k == STRING_REPLACE_RE_ALL || k == STRING_LEQ
201
           || k == STRING_TOLOWER || k == STRING_TOUPPER || k == STRING_REV);
202
4334
    std::vector<Node> new_nodes;
203
4334
    Node res = d_preproc.simplify(n, new_nodes);
204
2167
    Assert(res != n);
205
2167
    new_nodes.push_back(n.eqNode(res));
206
    Node nnlem =
207
4334
        new_nodes.size() == 1 ? new_nodes[0] : nm->mkNode(AND, new_nodes);
208
4334
    Trace("strings-red-lemma")
209
2167
        << "Reduction_" << effort << " lemma : " << nnlem << std::endl;
210
2167
    Trace("strings-red-lemma") << "...from " << n << std::endl;
211
4334
    Trace("strings-red-lemma")
212
4334
        << "Reduction_" << effort << " rewritten : " << rewrite(nnlem)
213
2167
        << std::endl;
214
2167
    d_im.sendInference(d_emptyVec, nnlem, InferenceId::STRINGS_REDUCTION, false, true);
215
4334
    Trace("strings-extf-debug")
216
2167
        << "  resolve extf : " << n << " based on reduction." << std::endl;
217
    // add as reduction lemma
218
2167
    d_reduced.insert(n);
219
  }
220
7280
  return true;
221
}
222
223
32020
void ExtfSolver::checkExtfReductions(int effort)
224
{
225
  // Notice we don't make a standard call to ExtTheory::doReductions here,
226
  // since certain optimizations like context-dependent reductions and
227
  // stratifying effort levels are done in doReduction below.
228
57789
  std::vector<Node> extf = d_extt.getActive();
229
64040
  Trace("strings-process") << "  checking " << extf.size() << " active extf"
230
32020
                           << std::endl;
231
240251
  for (const Node& n : extf)
232
  {
233
214482
    Assert(!d_state.isInConflict());
234
428964
    Trace("strings-extf-debug")
235
214482
        << "  check " << n
236
214482
        << ", active in model=" << d_extfInfoTmp[n].d_modelActive << std::endl;
237
214482
    bool ret = doReduction(effort, n);
238
214482
    if (ret)
239
    {
240
      // we do not mark as reduced, since we may want to evaluate
241
7296
      if (d_im.hasProcessed())
242
      {
243
12502
        return;
244
      }
245
    }
246
  }
247
}
248
249
51831
void ExtfSolver::checkExtfEval(int effort)
250
{
251
103662
  Trace("strings-extf-list")
252
51831
      << "Active extended functions, effort=" << effort << " : " << std::endl;
253
51831
  d_extfInfoTmp.clear();
254
51831
  NodeManager* nm = NodeManager::currentNM();
255
51831
  bool has_nreduce = false;
256
102909
  std::vector<Node> terms = d_extt.getActive();
257
  // the set of terms we have done extf inferences for
258
102909
  std::unordered_set<Node> inferProcessed;
259
578203
  for (const Node& n : terms)
260
  {
261
    // Setup information about n, including if it is equal to a constant.
262
527125
    ExtfInfoTmp& einfo = d_extfInfoTmp[n];
263
527125
    Assert(einfo.d_exp.empty());
264
1053497
    Node r = d_state.getRepresentative(n);
265
527125
    einfo.d_const = d_bsolver.getConstantEqc(r);
266
    // Get the current values of the children of n.
267
    // Notice that we look up the value of the direct children of n, and not
268
    // their free variables. In other words, given a term:
269
    //   t = (str.replace "B" (str.replace x "A" "B") "C")
270
    // we may build the explanation that:
271
    //   ((str.replace x "A" "B") = "B") => t = (str.replace "B" "B" "C")
272
    // instead of basing this on the free variable x:
273
    //   (x = "A") => t = (str.replace "B" (str.replace "A" "A" "B") "C")
274
    // Although both allow us to infer t = "C", it is important to use the
275
    // first kind of inference since it ensures that its subterms have the
276
    // expected values. Otherwise, we may in rare cases fail to realize that
277
    // the subterm (str.replace x "A" "B") does not currently have the correct
278
    // value, say in this example that (str.replace x "A" "B") != "B".
279
1053497
    std::vector<Node> exp;
280
1053497
    std::vector<Node> schildren;
281
527125
    bool schanged = false;
282
1722628
    for (const Node& nc : n)
283
    {
284
2391006
      Node sc = getCurrentSubstitutionFor(effort, nc, exp);
285
1195503
      schildren.push_back(sc);
286
1195503
      schanged = schanged || sc != nc;
287
    }
288
    // If there is information involving the children, attempt to do an
289
    // inference and/or mark n as reduced.
290
527125
    bool reduced = false;
291
1053497
    Node to_reduce = n;
292
527125
    if (schanged)
293
    {
294
509824
      Node sn = nm->mkNode(n.getKind(), schildren);
295
509824
      Trace("strings-extf-debug")
296
254912
          << "Check extf " << n << " == " << sn
297
254912
          << ", constant = " << einfo.d_const << ", effort=" << effort
298
254912
          << ", exp " << exp << std::endl;
299
254912
      einfo.d_exp.insert(einfo.d_exp.end(), exp.begin(), exp.end());
300
      // inference is rewriting the substituted node
301
509824
      Node nrc = rewrite(sn);
302
      // if rewrites to a constant, then do the inference and mark as reduced
303
254912
      if (nrc.isConst())
304
      {
305
142570
        if (effort < 3)
306
        {
307
142570
          d_extt.markReduced(n, ExtReducedId::STRINGS_SR_CONST);
308
285140
          Trace("strings-extf-debug")
309
142570
              << "  resolvable by evaluation..." << std::endl;
310
285140
          std::vector<Node> exps;
311
          // The following optimization gets the "symbolic definition" of
312
          // an extended term. The symbolic definition of a term t is a term
313
          // t' where constants are replaced by their corresponding proxy
314
          // variables.
315
          // For example, if lsym is a proxy variable for "", then
316
          // str.replace( lsym, lsym, lsym ) is the symbolic definition for
317
          // str.replace( "", "", "" ). It is generally better to use symbolic
318
          // definitions when doing cd-rewriting for the purpose of minimizing
319
          // clauses, e.g. we infer the unit equality:
320
          //    str.replace( lsym, lsym, lsym ) == ""
321
          // instead of making this inference multiple times:
322
          //    x = "" => str.replace( x, x, x ) == ""
323
          //    y = "" => str.replace( y, y, y ) == ""
324
285140
          Trace("strings-extf-debug")
325
142570
              << "  get symbolic definition..." << std::endl;
326
285140
          Node nrs;
327
          // only use symbolic definitions if option is set
328
142570
          if (options::stringInferSym())
329
          {
330
142570
            nrs = d_termReg.getSymbolicDefinition(sn, exps);
331
          }
332
142570
          if (!nrs.isNull())
333
          {
334
245730
            Trace("strings-extf-debug")
335
122865
                << "  rewrite " << nrs << "..." << std::endl;
336
245730
            Node nrsr = rewrite(nrs);
337
            // ensure the symbolic form is not rewritable
338
122865
            if (nrsr != nrs)
339
            {
340
              // we cannot use the symbolic definition if it rewrites
341
3836
              Trace("strings-extf-debug")
342
1918
                  << "  symbolic definition is trivial..." << std::endl;
343
1918
              nrs = Node::null();
344
            }
345
          }
346
          else
347
          {
348
39410
            Trace("strings-extf-debug")
349
19705
                << "  could not infer symbolic definition." << std::endl;
350
          }
351
285140
          Node conc;
352
142570
          if (!nrs.isNull())
353
          {
354
241894
            Trace("strings-extf-debug")
355
120947
                << "  symbolic def : " << nrs << std::endl;
356
120947
            if (!d_state.areEqual(nrs, nrc))
357
            {
358
              // infer symbolic unit
359
1717
              if (n.getType().isBoolean())
360
              {
361
1323
                conc = nrc == d_true ? nrs : nrs.negate();
362
              }
363
              else
364
              {
365
394
                conc = nrs.eqNode(nrc);
366
              }
367
1717
              einfo.d_exp.clear();
368
            }
369
          }
370
          else
371
          {
372
21623
            if (!d_state.areEqual(n, nrc))
373
            {
374
1448
              if (n.getType().isBoolean())
375
              {
376
714
                if (d_state.areEqual(n, nrc == d_true ? d_false : d_true))
377
                {
378
607
                  einfo.d_exp.push_back(nrc == d_true ? n.negate() : n);
379
607
                  conc = d_false;
380
                }
381
                else
382
                {
383
107
                  conc = nrc == d_true ? n : n.negate();
384
                }
385
              }
386
              else
387
              {
388
734
                conc = n.eqNode(nrc);
389
              }
390
            }
391
          }
392
142570
          if (!conc.isNull())
393
          {
394
6330
            Trace("strings-extf")
395
3165
                << "  resolve extf : " << sn << " -> " << nrc << std::endl;
396
3165
            InferenceId inf = effort == 0 ? InferenceId::STRINGS_EXTF : InferenceId::STRINGS_EXTF_N;
397
3165
            d_im.sendInference(einfo.d_exp, conc, inf, false, true);
398
3165
            d_statistics.d_cdSimplifications << n.getKind();
399
          }
400
        }
401
        else
402
        {
403
          // check if it is already equal, if so, mark as reduced. Otherwise, do
404
          // nothing.
405
          if (d_state.areEqual(n, nrc))
406
          {
407
            Trace("strings-extf")
408
                << "  resolved extf, since satisfied by model: " << n
409
                << std::endl;
410
            einfo.d_modelActive = false;
411
          }
412
        }
413
142570
        reduced = true;
414
      }
415
      else
416
      {
417
        // if this was a predicate which changed after substitution + rewriting
418
112342
        if (!einfo.d_const.isNull() && nrc.getType().isBoolean() && nrc != n)
419
        {
420
25132
          bool pol = einfo.d_const == d_true;
421
50264
          Node nrcAssert = pol ? nrc : nrc.negate();
422
50264
          Node nAssert = pol ? n : n.negate();
423
25132
          Assert(effort < 3);
424
25132
          einfo.d_exp.push_back(nAssert);
425
25132
          Trace("strings-extf-debug") << "  decomposable..." << std::endl;
426
50264
          Trace("strings-extf") << "  resolve extf : " << sn << " -> " << nrc
427
25132
                                << ", const = " << einfo.d_const << std::endl;
428
          // We send inferences internal here, which may help show unsat.
429
          // However, we do not make a determination whether n can be marked
430
          // reduced since this argument may be circular: we may infer than n
431
          // can be reduced to something else, but that thing may argue that it
432
          // can be reduced to n, in theory.
433
25132
          InferenceId infer =
434
25132
              effort == 0 ? InferenceId::STRINGS_EXTF_D : InferenceId::STRINGS_EXTF_D_N;
435
25132
          d_im.sendInternalInference(einfo.d_exp, nrcAssert, infer);
436
        }
437
112342
        to_reduce = nrc;
438
      }
439
    }
440
    // We must use the original n here to avoid circular justifications for
441
    // why extended functions are reduced. In particular, n should never be a
442
    // duplicate of another term considered in the block of code for
443
    // checkExtfInference below.
444
    // if not reduced and not processed
445
1438805
    if (!reduced && !n.isNull()
446
911680
        && inferProcessed.find(n) == inferProcessed.end())
447
    {
448
384555
      inferProcessed.insert(n);
449
384555
      Assert(effort < 3);
450
384555
      if (effort == 1)
451
      {
452
82936
        Trace("strings-extf")
453
41468
            << "  cannot rewrite extf : " << to_reduce << std::endl;
454
      }
455
      // we take to_reduce to be the (partially) reduced version of n, which
456
      // is justified by the explanation in einfo.
457
384555
      checkExtfInference(n, to_reduce, einfo, effort);
458
384555
      if (Trace.isOn("strings-extf-list"))
459
      {
460
        Trace("strings-extf-list") << "  * " << to_reduce;
461
        if (!einfo.d_const.isNull())
462
        {
463
          Trace("strings-extf-list") << ", const = " << einfo.d_const;
464
        }
465
        if (n != to_reduce)
466
        {
467
          Trace("strings-extf-list") << ", from " << n;
468
        }
469
        Trace("strings-extf-list") << std::endl;
470
      }
471
384555
      if (d_extt.isActive(n) && einfo.d_modelActive)
472
      {
473
384555
        has_nreduce = true;
474
      }
475
    }
476
527125
    if (d_state.isInConflict())
477
    {
478
753
      Trace("strings-extf-debug") << "  conflict, return." << std::endl;
479
753
      return;
480
    }
481
  }
482
51078
  d_hasExtf = has_nreduce;
483
}
484
485
384555
void ExtfSolver::checkExtfInference(Node n,
486
                                    Node nr,
487
                                    ExtfInfoTmp& in,
488
                                    int effort)
489
{
490
384555
  if (in.d_const.isNull())
491
  {
492
440595
    return;
493
  }
494
180557
  NodeManager* nm = NodeManager::currentNM();
495
361114
  Trace("strings-extf-infer")
496
180557
      << "checkExtfInference: " << n << " : " << nr << " == " << in.d_const
497
180557
      << " with exp " << in.d_exp << std::endl;
498
499
  // add original to explanation
500
180557
  if (n.getType().isBoolean())
501
  {
502
    // if Boolean, it's easy
503
93167
    in.d_exp.push_back(in.d_const.getConst<bool>() ? n : n.negate());
504
  }
505
  else
506
  {
507
    // otherwise, must explain via base node
508
174780
    Node r = d_state.getRepresentative(n);
509
    // explain using the base solver
510
87390
    d_bsolver.explainConstantEqc(n, r, in.d_exp);
511
  }
512
513
  // d_extfInferCache stores whether we have made the inferences associated
514
  // with a node n,
515
  // this may need to be generalized if multiple inferences apply
516
517
180557
  if (nr.getKind() == STRING_CONTAINS)
518
  {
519
32599
    Assert(in.d_const.isConst());
520
32599
    bool pol = in.d_const.getConst<bool>();
521
41562
    if ((pol && nr[1].getKind() == STRING_CONCAT)
522
65124
        || (!pol && nr[0].getKind() == STRING_CONCAT))
523
    {
524
      // If str.contains( x, str.++( y1, ..., yn ) ),
525
      //   we may infer str.contains( x, y1 ), ..., str.contains( x, yn )
526
      // The following recognizes two situations related to the above reasoning:
527
      // (1) If ~str.contains( x, yi ) holds for some i, we are in conflict,
528
      // (2) If str.contains( x, yj ) already holds for some j, then the term
529
      // str.contains( x, yj ) is irrelevant since it is satisfied by all models
530
      // for str.contains( x, str.++( y1, ..., yn ) ).
531
532
      // Notice that the dual of the above reasoning also holds, i.e.
533
      // If ~str.contains( str.++( x1, ..., xn ), y ),
534
      //   we may infer ~str.contains( x1, y ), ..., ~str.contains( xn, y )
535
      // This is also handled here.
536
3886
      if (d_extfInferCache.find(nr) == d_extfInferCache.end())
537
      {
538
1378
        d_extfInferCache.insert(nr);
539
540
1378
        int index = pol ? 1 : 0;
541
2752
        std::vector<Node> children;
542
1378
        children.push_back(nr[0]);
543
1378
        children.push_back(nr[1]);
544
4574
        for (const Node& nrc : nr[index])
545
        {
546
3200
          children[index] = nrc;
547
6396
          Node conc = nm->mkNode(STRING_CONTAINS, children);
548
3200
          conc = rewrite(pol ? conc : conc.negate());
549
          // check if it already (does not) hold
550
3200
          if (d_state.hasTerm(conc))
551
          {
552
147
            if (d_state.areEqual(conc, d_false))
553
            {
554
              // we are in conflict
555
4
              d_im.addToExplanation(conc, d_false, in.d_exp);
556
4
              d_im.sendInference(
557
                  in.d_exp, d_false, InferenceId::STRINGS_CTN_DECOMPOSE);
558
4
              Assert(d_state.isInConflict());
559
4
              return;
560
            }
561
143
            else if (d_extt.hasFunctionKind(conc.getKind()))
562
            {
563
              // can mark as reduced, since model for n implies model for conc
564
              d_extt.markReduced(conc, ExtReducedId::STRINGS_CTN_DECOMPOSE);
565
            }
566
          }
567
        }
568
      }
569
    }
570
    else
571
    {
572
114852
      if (std::find(d_extfInfoTmp[nr[0]].d_ctn[pol].begin(),
573
57426
                    d_extfInfoTmp[nr[0]].d_ctn[pol].end(),
574
114852
                    nr[1])
575
86139
          == d_extfInfoTmp[nr[0]].d_ctn[pol].end())
576
      {
577
57326
        Trace("strings-extf-debug") << "  store contains info : " << nr[0]
578
28663
                                    << " " << pol << " " << nr[1] << std::endl;
579
        // Store s (does not) contains t, since nr = (~)contains( s, t ) holds.
580
28663
        d_extfInfoTmp[nr[0]].d_ctn[pol].push_back(nr[1]);
581
28663
        d_extfInfoTmp[nr[0]].d_ctnFrom[pol].push_back(n);
582
        // Do transistive closure on contains, e.g.
583
        // if contains( s, t ) and ~contains( s, r ), then ~contains( t, r ).
584
585
        // The following infers new (negative) contains based on the above
586
        // reasoning, provided that ~contains( t, r ) does not
587
        // already hold in the current context. We test this by checking that
588
        // contains( t, r ) is not already asserted false in the current
589
        // context. We also handle the case where contains( t, r ) is equivalent
590
        // to t = r, in which case we check that t != r does not already hold
591
        // in the current context.
592
593
        // Notice that form of the above inference is enough to find
594
        // conflicts purely due to contains predicates. For example, if we
595
        // have only positive occurrences of contains, then no conflicts due to
596
        // contains predicates are possible and this schema does nothing. For
597
        // example, note that contains( s, t ) and contains( t, r ) implies
598
        // contains( s, r ), which we could but choose not to infer. Instead,
599
        // we prefer being lazy: only if ~contains( s, r ) appears later do we
600
        // infer ~contains( t, r ), which suffices to show a conflict.
601
28663
        bool opol = !pol;
602
32231
        for (unsigned i = 0, size = d_extfInfoTmp[nr[0]].d_ctn[opol].size();
603
32231
             i < size;
604
             i++)
605
        {
606
7136
          Node onr = d_extfInfoTmp[nr[0]].d_ctn[opol][i];
607
          Node concOrig =
608
7136
              nm->mkNode(STRING_CONTAINS, pol ? nr[1] : onr, pol ? onr : nr[1]);
609
7136
          Node conc = rewrite(concOrig);
610
          // For termination concerns, we only do the inference if the contains
611
          // does not rewrite (and thus does not introduce new terms).
612
3568
          if (conc == concOrig)
613
          {
614
            bool do_infer = false;
615
            conc = conc.negate();
616
            bool pol2 = conc.getKind() != NOT;
617
            Node lit = pol2 ? conc : conc[0];
618
            if (lit.getKind() == EQUAL)
619
            {
620
              do_infer = pol2 ? !d_state.areEqual(lit[0], lit[1])
621
                              : !d_state.areDisequal(lit[0], lit[1]);
622
            }
623
            else
624
            {
625
              do_infer = !d_state.areEqual(lit, pol2 ? d_true : d_false);
626
            }
627
            if (do_infer)
628
            {
629
              std::vector<Node> exp_c;
630
              exp_c.insert(exp_c.end(), in.d_exp.begin(), in.d_exp.end());
631
              Node ofrom = d_extfInfoTmp[nr[0]].d_ctnFrom[opol][i];
632
              Assert(d_extfInfoTmp.find(ofrom) != d_extfInfoTmp.end());
633
              exp_c.insert(exp_c.end(),
634
                           d_extfInfoTmp[ofrom].d_exp.begin(),
635
                           d_extfInfoTmp[ofrom].d_exp.end());
636
              d_im.sendInference(exp_c, conc, InferenceId::STRINGS_CTN_TRANS);
637
            }
638
          }
639
        }
640
      }
641
      else
642
      {
643
        // If we already know that s (does not) contain t, then n may be
644
        // redundant. However, we do not mark n as reduced here, since strings
645
        // reductions may require dependencies between extended functions.
646
        // Marking reduced here could lead to incorrect models if an
647
        // extended function is marked reduced based on an assignment to
648
        // something that depends on n.
649
50
        Trace("strings-extf-debug") << "  redundant." << std::endl;
650
      }
651
    }
652
32595
    return;
653
  }
654
655
  // If it's not a predicate, see if we can solve the equality n = c, where c
656
  // is the constant that extended term n is equal to.
657
295916
  Node inferEq = nr.eqNode(in.d_const);
658
295916
  Node inferEqr = rewrite(inferEq);
659
295916
  Node inferEqrr = inferEqr;
660
147958
  if (inferEqr.getKind() == EQUAL)
661
  {
662
    // try to use the extended rewriter for equalities
663
88414
    inferEqrr = d_rewriter.rewriteEqualityExt(inferEqr);
664
  }
665
147958
  if (inferEqrr != inferEqr)
666
  {
667
6084
    inferEqrr = rewrite(inferEqrr);
668
12168
    Trace("strings-extf-infer")
669
6084
        << "checkExtfInference: " << inferEq << " ...reduces to " << inferEqrr
670
6084
        << " with explanation " << in.d_exp << std::endl;
671
6084
    d_im.sendInternalInference(in.d_exp, inferEqrr, InferenceId::STRINGS_EXTF_EQ_REW);
672
  }
673
}
674
675
1195503
Node ExtfSolver::getCurrentSubstitutionFor(int effort,
676
                                           Node n,
677
                                           std::vector<Node>& exp)
678
{
679
1195503
  if (effort >= 3)
680
  {
681
    // model values
682
    Node mv = d_state.getModel()->getRepresentative(n);
683
    Trace("strings-subs") << "   model val : " << mv << std::endl;
684
    return mv;
685
  }
686
2391006
  Node nr = d_state.getRepresentative(n);
687
  // if the normal form is available, use it
688
1195503
  if (effort >= 1 && n.getType().isStringLike())
689
  {
690
53588
    Assert(effort < 3);
691
    // normal forms
692
53588
    NormalForm& nfnr = d_csolver.getNormalForm(nr);
693
107176
    Node ns = d_csolver.getNormalString(nfnr.d_base, exp);
694
107176
    Trace("strings-subs") << "   normal eqc : " << ns << " " << nfnr.d_base
695
53588
                          << " " << nr << std::endl;
696
53588
    if (!nfnr.d_base.isNull())
697
    {
698
53588
      d_im.addToExplanation(n, nfnr.d_base, exp);
699
    }
700
53588
    return ns;
701
  }
702
  // otherwise, we use the best content heuristic
703
2283830
  Node c = d_bsolver.explainBestContentEqc(n, nr, exp);
704
1141915
  if (!c.isNull())
705
  {
706
600776
    return c;
707
  }
708
541139
  return n;
709
}
710
711
10637
const std::map<Node, ExtfInfoTmp>& ExtfSolver::getInfo() const
712
{
713
10637
  return d_extfInfoTmp;
714
}
715
bool ExtfSolver::hasExtendedFunctions() const { return d_hasExtf.get(); }
716
717
10637
std::vector<Node> ExtfSolver::getActive(Kind k) const
718
{
719
10637
  return d_extt.getActive(k);
720
}
721
722
bool StringsExtfCallback::getCurrentSubstitution(
723
    int effort,
724
    const std::vector<Node>& vars,
725
    std::vector<Node>& subs,
726
    std::map<Node, std::vector<Node> >& exp)
727
{
728
  Trace("strings-subs") << "getCurrentSubstitution, effort = " << effort
729
                        << std::endl;
730
  for (const Node& v : vars)
731
  {
732
    Trace("strings-subs") << "  get subs for " << v << "..." << std::endl;
733
    Node s = d_esolver->getCurrentSubstitutionFor(effort, v, exp[v]);
734
    subs.push_back(s);
735
  }
736
  return true;
737
}
738
739
std::string ExtfSolver::debugPrintModel()
740
{
741
  std::stringstream ss;
742
  std::vector<Node> extf;
743
  d_extt.getTerms(extf);
744
  // each extended function should have at least one annotation below
745
  for (const Node& n : extf)
746
  {
747
    ss << "- " << n;
748
    ExtReducedId id;
749
    if (!d_extt.isActive(n, id))
750
    {
751
      ss << " :extt-inactive " << id;
752
    }
753
    if (!d_extfInfoTmp[n].d_modelActive)
754
    {
755
      ss << " :model-inactive";
756
    }
757
    if (d_reduced.find(n) != d_reduced.end())
758
    {
759
      ss << " :reduced";
760
    }
761
    ss << std::endl;
762
  }
763
  return ss.str();
764
}
765
766
}  // namespace strings
767
}  // namespace theory
768
31137
}  // namespace cvc5