GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/eqc_info.cpp Lines: 57 57 100.0 %
Date: 2021-09-18 Branches: 148 330 44.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Aina Niemetz
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 equivalence class info for the theory of strings.
14
 */
15
16
#include "theory/strings/eqc_info.h"
17
18
#include "theory/strings/theory_strings_utils.h"
19
#include "theory/strings/word.h"
20
21
using namespace std;
22
using namespace cvc5::context;
23
using namespace cvc5::kind;
24
25
namespace cvc5 {
26
namespace theory {
27
namespace strings {
28
29
30124
EqcInfo::EqcInfo(context::Context* c)
30
    : d_lengthTerm(c),
31
      d_codeTerm(c),
32
      d_cardinalityLemK(c),
33
      d_normalizedLength(c),
34
      d_prefixC(c),
35
30124
      d_suffixC(c)
36
{
37
30124
}
38
39
97170
Node EqcInfo::addEndpointConst(Node t, Node c, bool isSuf)
40
{
41
  // check conflict
42
194340
  Node prev = isSuf ? d_suffixC : d_prefixC;
43
97170
  if (!prev.isNull())
44
  {
45
164708
    Trace("strings-eager-pconf-debug") << "Check conflict " << prev << ", " << t
46
82354
                                       << " post=" << isSuf << std::endl;
47
82456
    Node prevC = utils::getConstantEndpoint(prev, isSuf);
48
82354
    Assert(!prevC.isNull());
49
82354
    Assert(prevC.isConst());
50
82354
    if (c.isNull())
51
    {
52
81331
      c = utils::getConstantEndpoint(t, isSuf);
53
81331
      Assert(!c.isNull());
54
    }
55
82354
    Assert(c.isConst());
56
82354
    bool conflict = false;
57
    // if the constant prefixes are different
58
82354
    if (c != prevC)
59
    {
60
      // conflicts between constants should be handled by equality engine
61
22210
      Assert(!t.isConst() || !prev.isConst());
62
44420
      Trace("strings-eager-pconf-debug")
63
22210
          << "Check conflict constants " << prevC << ", " << c << std::endl;
64
22210
      size_t pvs = Word::getLength(prevC);
65
22210
      size_t cvs = Word::getLength(c);
66
44274
      if (pvs == cvs || (pvs > cvs && t.isConst())
67
44274
          || (cvs > pvs && prev.isConst()))
68
      {
69
        // If equal length, cannot be equal due to node check above.
70
        // If one is fully constant and has less length than the other, then the
71
        // other will not fit and we are in conflict.
72
364
        conflict = true;
73
      }
74
      else
75
      {
76
43692
        Node larges = pvs > cvs ? prevC : c;
77
43692
        Node smalls = pvs > cvs ? c : prevC;
78
21846
        if (isSuf)
79
        {
80
11558
          conflict = !Word::hasSuffix(larges, smalls);
81
        }
82
        else
83
        {
84
10288
          conflict = !Word::hasPrefix(larges, smalls);
85
        }
86
      }
87
22210
      if (!conflict && (pvs > cvs || prev.isConst()))
88
      {
89
        // current is subsumed, either shorter prefix or the other is a full
90
        // constant
91
21504
        return Node::null();
92
      }
93
    }
94
60144
    else if (!t.isConst())
95
    {
96
      // current is subsumed since the other may be a full constant
97
60144
      return Node::null();
98
    }
99
706
    if (conflict)
100
    {
101
1208
      Trace("strings-eager-pconf")
102
604
          << "Conflict for " << prevC << ", " << c << std::endl;
103
1208
      std::vector<Node> ccs;
104
1208
      Node r[2];
105
1812
      for (unsigned i = 0; i < 2; i++)
106
      {
107
2416
        Node tp = i == 0 ? t : prev;
108
1208
        if (tp.getKind() == STRING_IN_REGEXP)
109
        {
110
404
          ccs.push_back(tp);
111
404
          r[i] = tp[0];
112
        }
113
        else
114
        {
115
804
          r[i] = tp;
116
        }
117
      }
118
604
      if (r[0] != r[1])
119
      {
120
604
        ccs.push_back(r[0].eqNode(r[1]));
121
      }
122
604
      Assert(!ccs.empty());
123
      Node ret =
124
1208
          ccs.size() == 1 ? ccs[0] : NodeManager::currentNM()->mkNode(AND, ccs);
125
1208
      Trace("strings-eager-pconf")
126
604
          << "String: eager prefix conflict: " << ret << std::endl;
127
604
      return ret;
128
    }
129
  }
130
14918
  if (isSuf)
131
  {
132
7253
    d_suffixC = t;
133
  }
134
  else
135
  {
136
7665
    d_prefixC = t;
137
  }
138
14918
  return Node::null();
139
}
140
141
}  // namespace strings
142
}  // namespace theory
143
29574
}  // namespace cvc5