GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/solver_state.cpp Lines: 102 106 96.2 %
Date: 2021-08-01 Branches: 158 338 46.7 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Tianyi Liang, Mathias Preiner
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 the solver state of the theory of strings.
14
 */
15
16
#include "theory/strings/solver_state.h"
17
18
#include "theory/rewriter.h"
19
#include "theory/strings/theory_strings_utils.h"
20
#include "theory/strings/word.h"
21
#include "util/rational.h"
22
23
using namespace std;
24
using namespace cvc5::context;
25
using namespace cvc5::kind;
26
27
namespace cvc5 {
28
namespace theory {
29
namespace strings {
30
31
9838
SolverState::SolverState(context::Context* c,
32
                         context::UserContext* u,
33
9838
                         Valuation& v)
34
9838
    : TheoryState(c, u, v), d_eeDisequalities(c), d_pendingConflictSet(c, false), d_pendingConflict(InferenceId::UNKNOWN)
35
{
36
9838
  d_zero = NodeManager::currentNM()->mkConst(Rational(0));
37
9838
  d_false = NodeManager::currentNM()->mkConst(false);
38
9838
}
39
40
19676
SolverState::~SolverState()
41
{
42
36582
  for (std::pair<const Node, EqcInfo*>& it : d_eqcInfo)
43
  {
44
26744
    delete it.second;
45
  }
46
9838
}
47
48
11065
const context::CDList<Node>& SolverState::getDisequalityList() const
49
{
50
11065
  return d_eeDisequalities;
51
}
52
53
64552
void SolverState::addDisequality(TNode t1, TNode t2)
54
{
55
64552
  d_eeDisequalities.push_back(t1.eqNode(t2));
56
64552
}
57
58
1659631
EqcInfo* SolverState::getOrMakeEqcInfo(Node eqc, bool doMake)
59
{
60
1659631
  std::map<Node, EqcInfo*>::iterator eqc_i = d_eqcInfo.find(eqc);
61
1659631
  if (eqc_i != d_eqcInfo.end())
62
  {
63
776013
    return eqc_i->second;
64
  }
65
883618
  if (doMake)
66
  {
67
26744
    EqcInfo* ei = new EqcInfo(d_context);
68
26744
    d_eqcInfo[eqc] = ei;
69
26744
    return ei;
70
  }
71
856874
  return nullptr;
72
}
73
74
TheoryModel* SolverState::getModel() { return d_valuation.getModel(); }
75
76
581167
Node SolverState::getLengthExp(Node t, std::vector<Node>& exp, Node te)
77
{
78
581167
  Assert(areEqual(t, te));
79
1162334
  Node lt = utils::mkNLength(te);
80
581167
  if (hasTerm(lt))
81
  {
82
    // use own length if it exists, leads to shorter explanation
83
578699
    return lt;
84
  }
85
2468
  EqcInfo* ei = getOrMakeEqcInfo(t, false);
86
4936
  Node lengthTerm = ei ? ei->d_lengthTerm : Node::null();
87
2468
  if (lengthTerm.isNull())
88
  {
89
    // typically shouldnt be necessary
90
2426
    lengthTerm = t;
91
  }
92
4936
  Debug("strings") << "SolverState::getLengthTerm " << t << " is " << lengthTerm
93
2468
                   << std::endl;
94
2468
  if (te != lengthTerm)
95
  {
96
42
    exp.push_back(te.eqNode(lengthTerm));
97
  }
98
  return Rewriter::rewrite(
99
2468
      NodeManager::currentNM()->mkNode(STRING_LENGTH, lengthTerm));
100
}
101
102
581163
Node SolverState::getLength(Node t, std::vector<Node>& exp)
103
{
104
581163
  return getLengthExp(t, exp, t);
105
}
106
107
13748
Node SolverState::explainNonEmpty(Node s)
108
{
109
13748
  Assert(s.getType().isStringLike());
110
27496
  Node emp = Word::mkEmptyWord(s.getType());
111
13748
  if (areDisequal(s, emp))
112
  {
113
5303
    return s.eqNode(emp).negate();
114
  }
115
16890
  Node sLen = utils::mkNLength(s);
116
8445
  if (areDisequal(sLen, d_zero))
117
  {
118
8425
    return sLen.eqNode(d_zero).negate();
119
  }
120
20
  return Node::null();
121
}
122
123
658774
bool SolverState::isEqualEmptyWord(Node s, Node& emps)
124
{
125
1317548
  Node sr = getRepresentative(s);
126
658774
  if (sr.isConst())
127
  {
128
182493
    if (Word::getLength(sr) == 0)
129
    {
130
27415
      emps = sr;
131
27415
      return true;
132
    }
133
  }
134
631359
  return false;
135
}
136
137
85123
void SolverState::setPendingPrefixConflictWhen(Node conf)
138
{
139
85123
  if (conf.isNull() || d_pendingConflictSet.get())
140
  {
141
84648
    return;
142
  }
143
950
  InferInfo iiPrefixConf(InferenceId::STRINGS_PREFIX_CONFLICT);
144
475
  iiPrefixConf.d_conc = d_false;
145
475
  utils::flattenOp(AND, conf, iiPrefixConf.d_premises);
146
475
  setPendingConflict(iiPrefixConf);
147
}
148
149
475
void SolverState::setPendingConflict(InferInfo& ii)
150
{
151
475
  if (!d_pendingConflictSet.get())
152
  {
153
475
    d_pendingConflict = ii;
154
475
    d_pendingConflictSet.set(true);
155
  }
156
475
}
157
158
843574
bool SolverState::hasPendingConflict() const { return d_pendingConflictSet; }
159
160
400
bool SolverState::getPendingConflict(InferInfo& ii) const
161
{
162
400
  if (d_pendingConflictSet)
163
  {
164
400
    ii = d_pendingConflict;
165
400
    return true;
166
  }
167
  return false;
168
}
169
170
5971
std::pair<bool, Node> SolverState::entailmentCheck(options::TheoryOfMode mode,
171
                                                   TNode lit)
172
{
173
5971
  return d_valuation.entailmentCheck(mode, lit);
174
}
175
176
19595
void SolverState::separateByLength(
177
    const std::vector<Node>& n,
178
    std::map<TypeNode, std::vector<std::vector<Node>>>& cols,
179
    std::map<TypeNode, std::vector<Node>>& lts)
180
{
181
19595
  unsigned leqc_counter = 0;
182
  // map (length, type) to an equivalence class identifier
183
39190
  std::map<std::pair<Node, TypeNode>, unsigned> eqc_to_leqc;
184
  // backwards map
185
39190
  std::map<unsigned, std::pair<Node, TypeNode>> leqc_to_eqc;
186
  // Collection of eqc for each identifier. Notice that some identifiers may
187
  // not have an associated length in the mappings above, if the length of
188
  // an equivalence class is unknown.
189
39190
  std::map<unsigned, std::vector<Node> > eqc_to_strings;
190
19595
  NodeManager* nm = NodeManager::currentNM();
191
80470
  for (const Node& eqc : n)
192
  {
193
60875
    Assert(d_ee->getRepresentative(eqc) == eqc);
194
121750
    TypeNode tnEqc = eqc.getType();
195
60875
    EqcInfo* ei = getOrMakeEqcInfo(eqc, false);
196
121750
    Node lt = ei ? ei->d_lengthTerm : Node::null();
197
60875
    if (!lt.isNull())
198
    {
199
60875
      lt = nm->mkNode(STRING_LENGTH, lt);
200
121750
      Node r = d_ee->getRepresentative(lt);
201
121750
      std::pair<Node, TypeNode> lkey(r, tnEqc);
202
60875
      if (eqc_to_leqc.find(lkey) == eqc_to_leqc.end())
203
      {
204
40222
        eqc_to_leqc[lkey] = leqc_counter;
205
40222
        leqc_to_eqc[leqc_counter] = lkey;
206
40222
        leqc_counter++;
207
      }
208
60875
      eqc_to_strings[eqc_to_leqc[lkey]].push_back(eqc);
209
    }
210
    else
211
    {
212
      eqc_to_strings[leqc_counter].push_back(eqc);
213
      leqc_counter++;
214
    }
215
  }
216
59817
  for (const std::pair<const unsigned, std::vector<Node> >& p : eqc_to_strings)
217
  {
218
40222
    Assert(!p.second.empty());
219
    // get the type of the collection
220
80444
    TypeNode stn = p.second[0].getType();
221
40222
    cols[stn].emplace_back(p.second.begin(), p.second.end());
222
40222
    lts[stn].push_back(leqc_to_eqc[p.first].first);
223
  }
224
19595
}
225
226
}  // namespace strings
227
}  // namespace theory
228
29280
}  // namespace cvc5