GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/base_solver.cpp Lines: 294 375 78.4 %
Date: 2021-09-30 Branches: 619 1583 39.1 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Andres Noetzli, Tianyi Liang
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
 * Base solver for the theory of strings. This class implements term
14
 * indexing and constant inference for the theory of strings.
15
 */
16
17
#include "theory/strings/base_solver.h"
18
19
#include "expr/sequence.h"
20
#include "options/strings_options.h"
21
#include "theory/rewriter.h"
22
#include "theory/strings/theory_strings_utils.h"
23
#include "theory/strings/word.h"
24
#include "util/rational.h"
25
26
using namespace std;
27
using namespace cvc5::context;
28
using namespace cvc5::kind;
29
30
namespace cvc5 {
31
namespace theory {
32
namespace strings {
33
34
6278
BaseSolver::BaseSolver(Env& env, SolverState& s, InferenceManager& im)
35
6278
    : EnvObj(env), d_state(s), d_im(im), d_congruent(context())
36
{
37
6278
  d_false = NodeManager::currentNM()->mkConst(false);
38
6278
  d_cardSize = utils::getAlphabetCardinality();
39
6278
}
40
41
6275
BaseSolver::~BaseSolver() {}
42
43
29724
void BaseSolver::checkInit()
44
{
45
  // build term index
46
29724
  d_eqcInfo.clear();
47
29724
  d_termIndex.clear();
48
29724
  d_stringsEqc.clear();
49
50
29724
  Trace("strings-base") << "BaseSolver::checkInit" << std::endl;
51
  // count of congruent, non-congruent per operator (independent of type),
52
  // for debugging.
53
59448
  std::map<Kind, std::pair<uint32_t, uint32_t>> congruentCount;
54
29724
  eq::EqualityEngine* ee = d_state.getEqualityEngine();
55
29724
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(ee);
56
3585612
  while (!eqcs_i.isFinished())
57
  {
58
3555888
    Node eqc = (*eqcs_i);
59
3555888
    TypeNode tn = eqc.getType();
60
1777944
    if (!tn.isRegExp())
61
    {
62
3219372
      Node emps;
63
      // get the term index for type tn
64
1609686
      std::map<Kind, TermIndex>& tti = d_termIndex[tn];
65
1609686
      if (tn.isStringLike())
66
      {
67
636990
        d_stringsEqc.push_back(eqc);
68
636990
        emps = Word::mkEmptyWord(tn);
69
      }
70
3219372
      Node var;
71
1609686
      eq::EqClassIterator eqc_i = eq::EqClassIterator(eqc, ee);
72
22662824
      while (!eqc_i.isFinished())
73
      {
74
21053138
        Node n = *eqc_i;
75
10526569
        Kind k = n.getKind();
76
10526569
        Trace("strings-base") << "initialize term: " << n << std::endl;
77
        // process constant-like terms
78
10526569
        if (utils::isConstantLike(n))
79
        {
80
861502
          Node prev = d_eqcInfo[eqc].d_bestContent;
81
430751
          if (!prev.isNull())
82
          {
83
            // we have either (seq.unit x) = C, or (seq.unit x) = (seq.unit y)
84
            // where C is a sequence constant.
85
            Node cval =
86
114
                prev.isConst() ? prev : (n.isConst() ? n : Node::null());
87
114
            std::vector<Node> exp;
88
57
            exp.push_back(prev.eqNode(n));
89
114
            Node s, t;
90
57
            if (cval.isNull())
91
            {
92
              // injectivity of seq.unit
93
5
              s = prev[0];
94
5
              t = n[0];
95
            }
96
            else
97
            {
98
              // should not have two constants in the same equivalence class
99
52
              Assert(cval.getType().isSequence());
100
104
              std::vector<Node> cchars = Word::getChars(cval);
101
52
              if (cchars.size() == 1)
102
              {
103
104
                Node oval = prev.isConst() ? n : prev;
104
52
                Assert(oval.getKind() == SEQ_UNIT);
105
52
                s = oval[0];
106
52
                t = cchars[0].getConst<Sequence>().getVec()[0];
107
                // oval is congruent (ignored) in this context
108
52
                d_congruent.insert(oval);
109
              }
110
              else
111
              {
112
                // (seq.unit x) = C => false if |C| != 1.
113
                d_im.sendInference(
114
                    exp, d_false, InferenceId::STRINGS_UNIT_CONST_CONFLICT);
115
                return;
116
              }
117
            }
118
57
            if (!d_state.areEqual(s, t))
119
            {
120
              // (seq.unit x) = (seq.unit y) => x=y, or
121
              // (seq.unit x) = (seq.unit c) => x=c
122
8
              Assert(s.getType() == t.getType());
123
8
              d_im.sendInference(exp, s.eqNode(t), InferenceId::STRINGS_UNIT_INJ);
124
            }
125
          }
126
          // update best content
127
430751
          if (prev.isNull() || n.isConst())
128
          {
129
430694
            d_eqcInfo[eqc].d_bestContent = n;
130
430694
            d_eqcInfo[eqc].d_bestScore = 0;
131
430694
            d_eqcInfo[eqc].d_base = n;
132
430694
            d_eqcInfo[eqc].d_exp = Node::null();
133
          }
134
        }
135
10526569
        if (tn.isInteger())
136
        {
137
          // do nothing
138
        }
139
        // process indexing
140
8022507
        else if (n.getNumChildren() > 0)
141
        {
142
5950469
          if (k != EQUAL)
143
          {
144
1327633
            if (d_congruent.find(n) == d_congruent.end())
145
            {
146
2104112
              std::vector<Node> c;
147
2104112
              Node nc = tti[k].add(n, 0, d_state, emps, c);
148
1052056
              if (nc != n)
149
              {
150
185496
                Trace("strings-base-debug")
151
92748
                    << "...found congruent term " << nc << std::endl;
152
                // check if we have inferred a new equality by removal of empty
153
                // components
154
92748
                if (k == STRING_CONCAT && !d_state.areEqual(nc, n))
155
                {
156
7678
                  std::vector<Node> exp;
157
                  // the number of empty components of n, nc
158
3839
                  size_t count[2] = {0, 0};
159
23729
                  while (count[0] < nc.getNumChildren()
160
13784
                         || count[1] < n.getNumChildren())
161
                  {
162
                    // explain empty prefixes
163
29835
                    for (unsigned t = 0; t < 2; t++)
164
                    {
165
39780
                      Node nn = t == 0 ? nc : n;
166
30996
                      while (count[t] < nn.getNumChildren()
167
81882
                             && (nn[count[t]] == emps
168
45854
                                 || d_state.areEqual(nn[count[t]], emps)))
169
                      {
170
5553
                        if (nn[count[t]] != emps)
171
                        {
172
5553
                          exp.push_back(nn[count[t]].eqNode(emps));
173
                        }
174
5553
                        count[t]++;
175
                      }
176
                    }
177
19890
                    Trace("strings-base-debug")
178
9945
                        << "  counts = " << count[0] << ", " << count[1]
179
9945
                        << std::endl;
180
                    // explain equal components
181
9945
                    if (count[0] < nc.getNumChildren())
182
                    {
183
7429
                      Assert(count[1] < n.getNumChildren());
184
7429
                      if (nc[count[0]] != n[count[1]])
185
                      {
186
208
                        exp.push_back(nc[count[0]].eqNode(n[count[1]]));
187
                      }
188
7429
                      count[0]++;
189
7429
                      count[1]++;
190
                    }
191
                  }
192
                  // infer the equality
193
3839
                  d_im.sendInference(exp, n.eqNode(nc), InferenceId::STRINGS_I_NORM);
194
                }
195
                else
196
                {
197
                  // We cannot mark one of the terms as reduced here (via
198
                  // ExtTheory::markCongruent) since extended function terms
199
                  // rely on reductions to other extended function terms. We
200
                  // may have a pair of extended function terms f(a)=f(b) where
201
                  // the reduction of argument a depends on the term b.
202
                  // Thus, marking f(b) as reduced by virtue of the fact we
203
                  // have f(a) is incorrect, since then we are effectively
204
                  // assuming that the reduction of f(a) depends on itself.
205
                }
206
                // this node is congruent to another one, we can ignore it
207
185496
                Trace("strings-base-debug")
208
92748
                    << "  congruent term : " << n << " (via " << nc << ")"
209
92748
                    << std::endl;
210
92748
                d_congruent.insert(n);
211
92748
                congruentCount[k].first++;
212
              }
213
959308
              else if (k == STRING_CONCAT && c.size() == 1)
214
              {
215
48708
                Trace("strings-base-debug")
216
24354
                    << "  congruent term by singular : " << n << " " << c[0]
217
24354
                    << std::endl;
218
                // singular case
219
24354
                if (!d_state.areEqual(c[0], n))
220
                {
221
16958
                  Node ns;
222
16958
                  std::vector<Node> exp;
223
                  // explain empty components
224
8479
                  bool foundNEmpty = false;
225
27178
                  for (const Node& nnc : n)
226
                  {
227
18699
                    if (d_state.areEqual(nnc, emps))
228
                    {
229
10220
                      if (nnc != emps)
230
                      {
231
10220
                        exp.push_back(nnc.eqNode(emps));
232
                      }
233
                    }
234
                    else
235
                    {
236
8479
                      Assert(!foundNEmpty);
237
8479
                      ns = nnc;
238
8479
                      foundNEmpty = true;
239
                    }
240
                  }
241
8479
                  AlwaysAssert(foundNEmpty);
242
                  // infer the equality
243
8479
                  d_im.sendInference(exp, n.eqNode(ns), InferenceId::STRINGS_I_NORM_S);
244
                }
245
24354
                d_congruent.insert(n);
246
24354
                congruentCount[k].first++;
247
              }
248
              else
249
              {
250
934954
                congruentCount[k].second++;
251
              }
252
            }
253
            else
254
            {
255
275577
              congruentCount[k].first++;
256
            }
257
          }
258
        }
259
2072038
        else if (!n.isConst())
260
        {
261
1818786
          if (d_congruent.find(n) == d_congruent.end())
262
          {
263
            // We mark all but the oldest variable in the equivalence class as
264
            // congruent.
265
943069
            if (var.isNull())
266
            {
267
630069
              var = n;
268
            }
269
313000
            else if (var > n)
270
            {
271
347198
              Trace("strings-base-debug")
272
173599
                  << "  congruent variable : " << var << std::endl;
273
173599
              d_congruent.insert(var);
274
173599
              var = n;
275
            }
276
            else
277
            {
278
278802
              Trace("strings-base-debug")
279
139401
                  << "  congruent variable : " << n << std::endl;
280
139401
              d_congruent.insert(n);
281
            }
282
          }
283
        }
284
10526569
        ++eqc_i;
285
      }
286
    }
287
1777944
    ++eqcs_i;
288
  }
289
29724
  if (Trace.isOn("strings-base"))
290
  {
291
    for (const std::pair<const Kind, std::pair<uint32_t, uint32_t>>& cc :
292
         congruentCount)
293
    {
294
      Trace("strings-base")
295
          << "  Terms[" << cc.first << "] = " << cc.second.second << "/"
296
          << (cc.second.first + cc.second.second) << std::endl;
297
    }
298
  }
299
29724
  Trace("strings-base") << "BaseSolver::checkInit finished" << std::endl;
300
}
301
302
24981
void BaseSolver::checkConstantEquivalenceClasses()
303
{
304
  // do fixed point
305
24981
  size_t prevSize = 0;
306
49962
  std::vector<Node> vecc;
307
  do
308
  {
309
24981
    vecc.clear();
310
49962
    Trace("strings-base-debug")
311
24981
        << "Check constant equivalence classes..." << std::endl;
312
24981
    prevSize = d_eqcInfo.size();
313
65365
    for (std::pair<const TypeNode, std::map<Kind, TermIndex>>& tindex :
314
24981
         d_termIndex)
315
    {
316
65365
      checkConstantEquivalenceClasses(
317
130730
          &tindex.second[STRING_CONCAT], vecc, true);
318
    }
319
24981
  } while (!d_im.hasProcessed() && d_eqcInfo.size() > prevSize);
320
321
24981
  if (!d_im.hasProcessed())
322
  {
323
    // now, go back and set "most content" terms
324
24981
    vecc.clear();
325
65365
    for (std::pair<const TypeNode, std::map<Kind, TermIndex>>& tindex :
326
24981
         d_termIndex)
327
    {
328
65365
      checkConstantEquivalenceClasses(
329
130730
          &tindex.second[STRING_CONCAT], vecc, false);
330
    }
331
  }
332
24981
}
333
334
852053
void BaseSolver::checkConstantEquivalenceClasses(TermIndex* ti,
335
                                                 std::vector<Node>& vecc,
336
                                                 bool ensureConst,
337
                                                 bool isConst)
338
{
339
1704106
  Node n = ti->d_data;
340
852053
  if (!n.isNull())
341
  {
342
    // construct the constant if applicable
343
844178
    Node c;
344
422089
    if (isConst)
345
    {
346
87952
      c = utils::mkNConcat(vecc, n.getType());
347
    }
348
422089
    if (!isConst || !d_state.areEqual(n, c))
349
    {
350
334137
      if (Trace.isOn("strings-debug"))
351
      {
352
        Trace("strings-debug")
353
            << "Constant eqc : " << c << " for " << n << std::endl;
354
        Trace("strings-debug") << "  ";
355
        for (const Node& v : vecc)
356
        {
357
          Trace("strings-debug") << v << " ";
358
        }
359
        Trace("strings-debug") << std::endl;
360
      }
361
334137
      size_t countc = 0;
362
668274
      std::vector<Node> exp;
363
      // non-constant vector
364
668274
      std::vector<Node> vecnc;
365
334137
      size_t contentSize = 0;
366
1163279
      for (size_t count = 0, nchild = n.getNumChildren(); count < nchild;
367
           ++count)
368
      {
369
        // Add explanations for the empty children
370
905364
        Node emps;
371
858816
        if (d_state.isEqualEmptyWord(n[count], emps))
372
        {
373
29674
          d_im.addToExplanation(n[count], emps, exp);
374
29674
          continue;
375
        }
376
1522714
        else if (vecc[countc].isNull())
377
        {
378
723246
          Assert(!isConst);
379
          // no constant for this component, leave it as is
380
723246
          vecnc.push_back(n[count]);
381
723246
          continue;
382
        }
383
        // if we are not entirely a constant
384
76222
        if (!isConst)
385
        {
386
          // use the constant component
387
76222
          vecnc.push_back(vecc[countc]);
388
76222
          Assert(vecc[countc].isConst());
389
76222
          contentSize += Word::getLength(vecc[countc]);
390
        }
391
152444
        Trace("strings-debug")
392
76222
            << "...explain " << n[count] << " " << vecc[countc] << std::endl;
393
76222
        if (!d_state.areEqual(n[count], vecc[countc]))
394
        {
395
          Node nrr = d_state.getRepresentative(n[count]);
396
          Assert(!d_eqcInfo[nrr].d_bestContent.isNull()
397
                 && d_eqcInfo[nrr].d_bestContent.isConst());
398
          // must flatten to avoid nested AND in explanations
399
          utils::flattenOp(AND, d_eqcInfo[nrr].d_exp, exp);
400
          // now explain equality to base
401
          d_im.addToExplanation(n[count], d_eqcInfo[nrr].d_base, exp);
402
        }
403
        else
404
        {
405
76222
          d_im.addToExplanation(n[count], vecc[countc], exp);
406
        }
407
76222
        countc++;
408
      }
409
      // exp contains an explanation of n==c
410
334137
      Assert(!isConst || countc == vecc.size());
411
334137
      if (!isConst)
412
      {
413
        // no use storing something with no content
414
334137
        if (contentSize > 0)
415
        {
416
131356
          Node nr = d_state.getRepresentative(n);
417
65678
          BaseEqcInfo& bei = d_eqcInfo[nr];
418
131356
          if (!bei.d_bestContent.isConst()
419
65678
              && (bei.d_bestContent.isNull() || contentSize > bei.d_bestScore))
420
          {
421
            // The equivalence class is not entailed to be equal to a constant
422
            // and we found a better concatenation
423
104136
            Node nct = utils::mkNConcat(vecnc, n.getType());
424
52068
            Assert(!nct.isConst());
425
52068
            bei.d_bestContent = nct;
426
52068
            bei.d_bestScore = contentSize;
427
52068
            bei.d_base = n;
428
52068
            if (!exp.empty())
429
            {
430
24117
              bei.d_exp = utils::mkAnd(exp);
431
            }
432
104136
            Trace("strings-debug")
433
52068
                << "Set eqc best content " << n << " to " << nct
434
52068
                << ", explanation = " << bei.d_exp << std::endl;
435
          }
436
        }
437
      }
438
      else if (d_state.hasTerm(c))
439
      {
440
        d_im.sendInference(exp, n.eqNode(c), InferenceId::STRINGS_I_CONST_MERGE);
441
        return;
442
      }
443
      else if (!d_im.hasProcessed())
444
      {
445
        Node nr = d_state.getRepresentative(n);
446
        BaseEqcInfo& bei = d_eqcInfo[nr];
447
        if (!bei.d_bestContent.isConst())
448
        {
449
          bei.d_bestContent = c;
450
          bei.d_base = n;
451
          bei.d_exp = utils::mkAnd(exp);
452
          Trace("strings-debug")
453
              << "Set eqc const " << n << " to " << c
454
              << ", explanation = " << bei.d_exp << std::endl;
455
        }
456
        else if (c != bei.d_bestContent)
457
        {
458
          // conflict
459
          Trace("strings-debug")
460
              << "Conflict, other constant was " << bei.d_bestContent
461
              << ", this constant was " << c << std::endl;
462
          if (bei.d_exp.isNull())
463
          {
464
            // n==c ^ n == c' => false
465
            d_im.addToExplanation(n, bei.d_bestContent, exp);
466
          }
467
          else
468
          {
469
            // n==c ^ n == d_base == c' => false
470
            exp.push_back(bei.d_exp);
471
            d_im.addToExplanation(n, bei.d_base, exp);
472
          }
473
          d_im.sendInference(exp, d_false, InferenceId::STRINGS_I_CONST_CONFLICT);
474
          return;
475
        }
476
        else
477
        {
478
          Trace("strings-debug") << "Duplicate constant." << std::endl;
479
        }
480
      }
481
    }
482
  }
483
1765021
  for (std::pair<const TNode, TermIndex>& p : ti->d_children)
484
  {
485
912968
    std::map<Node, BaseEqcInfo>::const_iterator it = d_eqcInfo.find(p.first);
486
912968
    if (it != d_eqcInfo.end() && it->second.d_bestContent.isConst())
487
    {
488
274372
      vecc.push_back(it->second.d_bestContent);
489
274372
      checkConstantEquivalenceClasses(&p.second, vecc, ensureConst, isConst);
490
274372
      vecc.pop_back();
491
    }
492
638596
    else if (!ensureConst)
493
    {
494
      // can still proceed, with null
495
446951
      vecc.push_back(Node::null());
496
446951
      checkConstantEquivalenceClasses(&p.second, vecc, ensureConst, false);
497
446951
      vecc.pop_back();
498
    }
499
912968
    if (d_im.hasProcessed())
500
    {
501
      break;
502
    }
503
  }
504
}
505
506
6182
void BaseSolver::checkCardinality()
507
{
508
  // This will create a partition of eqc, where each collection has length that
509
  // are pairwise propagated to be equal. We do not require disequalities
510
  // between the lengths of each collection, since we split on disequalities
511
  // between lengths of string terms that are disequal (DEQ-LENGTH-SP).
512
12364
  std::map<TypeNode, std::vector<std::vector<Node> > > cols;
513
12364
  std::map<TypeNode, std::vector<Node> > lts;
514
6182
  d_state.separateByLength(d_stringsEqc, cols, lts);
515
7516
  for (std::pair<const TypeNode, std::vector<std::vector<Node> > >& c : cols)
516
  {
517
1334
    checkCardinalityType(c.first, c.second, lts[c.first]);
518
  }
519
6182
}
520
521
1334
void BaseSolver::checkCardinalityType(TypeNode tn,
522
                                      std::vector<std::vector<Node> >& cols,
523
                                      std::vector<Node>& lts)
524
{
525
2668
  Trace("strings-card") << "Check cardinality (type " << tn << ")..."
526
1334
                        << std::endl;
527
1334
  NodeManager* nm = NodeManager::currentNM();
528
  uint32_t typeCardSize;
529
1334
  if (tn.isString())  // string-only
530
  {
531
1268
    typeCardSize = d_cardSize;
532
  }
533
  else
534
  {
535
66
    Assert(tn.isSequence());
536
132
    TypeNode etn = tn.getSequenceElementType();
537
66
    if (!d_state.isFiniteType(etn))
538
    {
539
      // infinite cardinality, we are fine
540
60
      return;
541
    }
542
    // TODO (cvc4-projects #23): how to handle sequence for finite types?
543
6
    return;
544
  }
545
  // for each collection
546
9171
  for (unsigned i = 0, csize = cols.size(); i < csize; ++i)
547
  {
548
7903
    Node lr = lts[i];
549
15806
    Trace("strings-card") << "Number of strings with length equal to " << lr
550
7903
                          << " is " << cols[i].size() << std::endl;
551
7903
    if (cols[i].size() <= 1)
552
    {
553
      // no restriction on sets in the partition of size 1
554
6077
      continue;
555
    }
556
    // size > c^k
557
1826
    unsigned card_need = 1;
558
1826
    double curr = static_cast<double>(cols[i].size());
559
1826
    while (curr > typeCardSize)
560
    {
561
      curr = curr / static_cast<double>(typeCardSize);
562
      card_need++;
563
    }
564
3652
    Trace("strings-card")
565
1826
        << "Need length " << card_need
566
1826
        << " for this number of strings (where alphabet size is "
567
1826
        << typeCardSize << ") given type " << tn << "." << std::endl;
568
    // check if we need to split
569
1826
    bool needsSplit = true;
570
1826
    if (lr.isConst())
571
    {
572
      // if constant, compare
573
3386
      Node cmp = nm->mkNode(GEQ, lr, nm->mkConst(Rational(card_need)));
574
1693
      cmp = Rewriter::rewrite(cmp);
575
1693
      needsSplit = !cmp.getConst<bool>();
576
    }
577
    else
578
    {
579
      // find the minimimum constant that we are unknown to be disequal from, or
580
      // otherwise stop if we increment such that cardinality does not apply.
581
      // We always start with r=1 since by the invariants of our term registry,
582
      // a term is either equal to the empty string, or has length >= 1.
583
133
      unsigned r = 1;
584
133
      bool success = true;
585
133
      while (r < card_need && success)
586
      {
587
        Node rr = nm->mkConst(Rational(r));
588
        if (d_state.areDisequal(rr, lr))
589
        {
590
          r++;
591
        }
592
        else
593
        {
594
          success = false;
595
        }
596
      }
597
133
      if (r > 0)
598
      {
599
266
        Trace("strings-card")
600
133
            << "Symbolic length " << lr << " must be at least " << r
601
133
            << " due to constant disequalities." << std::endl;
602
      }
603
133
      needsSplit = r < card_need;
604
    }
605
606
1826
    if (!needsSplit)
607
    {
608
      // don't need to split
609
1826
      continue;
610
    }
611
    // first, try to split to merge equivalence classes
612
    for (std::vector<Node>::iterator itr1 = cols[i].begin();
613
         itr1 != cols[i].end();
614
         ++itr1)
615
    {
616
      for (std::vector<Node>::iterator itr2 = itr1 + 1; itr2 != cols[i].end();
617
           ++itr2)
618
      {
619
        if (!d_state.areDisequal(*itr1, *itr2))
620
        {
621
          // add split lemma
622
          if (d_im.sendSplit(*itr1, *itr2, InferenceId::STRINGS_CARD_SP))
623
          {
624
            return;
625
          }
626
        }
627
      }
628
    }
629
    // otherwise, we need a length constraint
630
    uint32_t int_k = static_cast<uint32_t>(card_need);
631
    EqcInfo* ei = d_state.getOrMakeEqcInfo(lr, true);
632
    Trace("strings-card") << "Previous cardinality used for " << lr << " is "
633
                          << ((int)ei->d_cardinalityLemK.get() - 1)
634
                          << std::endl;
635
    if (int_k + 1 > ei->d_cardinalityLemK.get())
636
    {
637
      Node k_node = nm->mkConst(Rational(int_k));
638
      // add cardinality lemma
639
      Node dist = nm->mkNode(DISTINCT, cols[i]);
640
      std::vector<Node> expn;
641
      expn.push_back(dist);
642
      for (std::vector<Node>::iterator itr1 = cols[i].begin();
643
           itr1 != cols[i].end();
644
           ++itr1)
645
      {
646
        Node len = nm->mkNode(STRING_LENGTH, *itr1);
647
        if (len != lr)
648
        {
649
          Node len_eq_lr = len.eqNode(lr);
650
          expn.push_back(len_eq_lr);
651
        }
652
      }
653
      Node len = nm->mkNode(STRING_LENGTH, cols[i][0]);
654
      Node cons = nm->mkNode(GEQ, len, k_node);
655
      cons = Rewriter::rewrite(cons);
656
      ei->d_cardinalityLemK.set(int_k + 1);
657
      if (!cons.isConst() || !cons.getConst<bool>())
658
      {
659
        d_im.sendInference(
660
            expn, expn, cons, InferenceId::STRINGS_CARDINALITY, false, true);
661
        return;
662
      }
663
    }
664
  }
665
1268
  Trace("strings-card") << "...end check cardinality" << std::endl;
666
}
667
668
3195472
bool BaseSolver::isCongruent(Node n)
669
{
670
3195472
  return d_congruent.find(n) != d_congruent.end();
671
}
672
673
2071062
Node BaseSolver::getConstantEqc(Node eqc)
674
{
675
2071062
  std::map<Node, BaseEqcInfo>::const_iterator it = d_eqcInfo.find(eqc);
676
2071062
  if (it != d_eqcInfo.end() && it->second.d_bestContent.isConst())
677
  {
678
593422
    return it->second.d_bestContent;
679
  }
680
1477640
  return Node::null();
681
}
682
683
76635
Node BaseSolver::explainConstantEqc(Node n, Node eqc, std::vector<Node>& exp)
684
{
685
76635
  std::map<Node, BaseEqcInfo>::const_iterator it = d_eqcInfo.find(eqc);
686
76635
  if (it != d_eqcInfo.end())
687
  {
688
76635
    BaseEqcInfo& bei = d_eqcInfo[eqc];
689
76635
    if (!bei.d_bestContent.isConst())
690
    {
691
      return Node::null();
692
    }
693
76635
    if (!bei.d_exp.isNull())
694
    {
695
      utils::flattenOp(AND, bei.d_exp, exp);
696
    }
697
76635
    if (!bei.d_base.isNull())
698
    {
699
76635
      d_im.addToExplanation(n, bei.d_base, exp);
700
    }
701
76635
    return bei.d_bestContent;
702
  }
703
  return Node::null();
704
}
705
706
961282
Node BaseSolver::explainBestContentEqc(Node n, Node eqc, std::vector<Node>& exp)
707
{
708
961282
  std::map<Node, BaseEqcInfo>::const_iterator it = d_eqcInfo.find(eqc);
709
961282
  if (it != d_eqcInfo.end())
710
  {
711
507740
    BaseEqcInfo& bei = d_eqcInfo[eqc];
712
507740
    Assert(!bei.d_bestContent.isNull());
713
507740
    if (!bei.d_exp.isNull())
714
    {
715
60740
      utils::flattenOp(AND, bei.d_exp, exp);
716
    }
717
507740
    if (!bei.d_base.isNull())
718
    {
719
507740
      d_im.addToExplanation(n, bei.d_base, exp);
720
    }
721
507740
    return bei.d_bestContent;
722
  }
723
724
453542
  return Node::null();
725
}
726
727
24752
const std::vector<Node>& BaseSolver::getStringEqc() const
728
{
729
24752
  return d_stringsEqc;
730
}
731
732
3598379
Node BaseSolver::TermIndex::add(TNode n,
733
                                unsigned index,
734
                                const SolverState& s,
735
                                Node er,
736
                                std::vector<Node>& c)
737
{
738
3598379
  if (index == n.getNumChildren())
739
  {
740
1052056
    if (d_data.isNull())
741
    {
742
959308
      d_data = n;
743
    }
744
1052056
    return d_data;
745
  }
746
2546323
  Assert(index < n.getNumChildren());
747
5092646
  TNode nir = s.getRepresentative(n[index]);
748
  // if it is empty, and doing CONCAT, ignore
749
2546323
  if (nir == er && n.getKind() == STRING_CONCAT)
750
  {
751
187092
    return add(n, index + 1, s, er, c);
752
  }
753
2359231
  c.push_back(nir);
754
2359231
  return d_children[nir].add(n, index + 1, s, er, c);
755
}
756
757
}  // namespace strings
758
}  // namespace theory
759
22755
}  // namespace cvc5