GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/extf_solver.cpp Lines: 301 358 84.1 %
Date: 2021-08-14 Branches: 779 1754 44.4 %

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