GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/solver_state.cpp Lines: 100 104 96.2 %
Date: 2021-11-06 Branches: 154 332 46.4 %

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
15272
SolverState::SolverState(Env& env, Valuation& v)
32
    : TheoryState(env, v),
33
      d_eeDisequalities(env.getContext()),
34
      d_pendingConflictSet(env.getContext(), false),
35
15272
      d_pendingConflict(InferenceId::UNKNOWN)
36
{
37
15272
  d_zero = NodeManager::currentNM()->mkConst(Rational(0));
38
15272
  d_false = NodeManager::currentNM()->mkConst(false);
39
15272
}
40
41
30534
SolverState::~SolverState()
42
{
43
72835
  for (std::pair<const Node, EqcInfo*>& it : d_eqcInfo)
44
  {
45
57568
    delete it.second;
46
  }
47
15267
}
48
49
13971
const context::CDList<Node>& SolverState::getDisequalityList() const
50
{
51
13971
  return d_eeDisequalities;
52
}
53
54
111193
void SolverState::addDisequality(TNode t1, TNode t2)
55
{
56
111193
  d_eeDisequalities.push_back(t1.eqNode(t2));
57
111193
}
58
59
2879382
EqcInfo* SolverState::getOrMakeEqcInfo(Node eqc, bool doMake)
60
{
61
2879382
  std::map<Node, EqcInfo*>::iterator eqc_i = d_eqcInfo.find(eqc);
62
2879382
  if (eqc_i != d_eqcInfo.end())
63
  {
64
2038406
    return eqc_i->second;
65
  }
66
840976
  if (doMake)
67
  {
68
57568
    EqcInfo* ei = new EqcInfo(d_env.getContext());
69
57568
    d_eqcInfo[eqc] = ei;
70
57568
    return ei;
71
  }
72
783408
  return nullptr;
73
}
74
75
TheoryModel* SolverState::getModel() { return d_valuation.getModel(); }
76
77
1257597
Node SolverState::getLengthExp(Node t, std::vector<Node>& exp, Node te)
78
{
79
1257597
  Assert(areEqual(t, te));
80
2515194
  Node lt = utils::mkNLength(te);
81
1257597
  if (hasTerm(lt))
82
  {
83
    // use own length if it exists, leads to shorter explanation
84
1255125
    return lt;
85
  }
86
2472
  EqcInfo* ei = getOrMakeEqcInfo(t, false);
87
4944
  Node lengthTerm = ei ? ei->d_lengthTerm : Node::null();
88
2472
  if (lengthTerm.isNull())
89
  {
90
    // typically shouldnt be necessary
91
2430
    lengthTerm = t;
92
  }
93
  else
94
  {
95
42
    lengthTerm = lengthTerm[0];
96
  }
97
4944
  Debug("strings") << "SolverState::getLengthTerm " << t << " is " << lengthTerm
98
2472
                   << std::endl;
99
2472
  if (te != lengthTerm)
100
  {
101
42
    exp.push_back(te.eqNode(lengthTerm));
102
  }
103
2472
  return rewrite(NodeManager::currentNM()->mkNode(STRING_LENGTH, lengthTerm));
104
}
105
106
1257593
Node SolverState::getLength(Node t, std::vector<Node>& exp)
107
{
108
1257593
  return getLengthExp(t, exp, t);
109
}
110
111
21798
Node SolverState::explainNonEmpty(Node s)
112
{
113
21798
  Assert(s.getType().isStringLike());
114
43596
  Node emp = Word::mkEmptyWord(s.getType());
115
21798
  if (areDisequal(s, emp))
116
  {
117
8918
    return s.eqNode(emp).negate();
118
  }
119
25760
  Node sLen = utils::mkNLength(s);
120
12880
  if (areDisequal(sLen, d_zero))
121
  {
122
12869
    return sLen.eqNode(d_zero).negate();
123
  }
124
11
  return Node::null();
125
}
126
127
1028295
bool SolverState::isEqualEmptyWord(Node s, Node& emps)
128
{
129
2056590
  Node sr = getRepresentative(s);
130
1028295
  if (sr.isConst())
131
  {
132
284378
    if (Word::getLength(sr) == 0)
133
    {
134
36826
      emps = sr;
135
36826
      return true;
136
    }
137
  }
138
991469
  return false;
139
}
140
141
841
void SolverState::setPendingMergeConflict(Node conf, InferenceId id)
142
{
143
841
  if (d_pendingConflictSet.get())
144
  {
145
    // already set conflict
146
64
    return;
147
  }
148
1554
  InferInfo iiPrefixConf(id);
149
777
  iiPrefixConf.d_conc = d_false;
150
777
  utils::flattenOp(AND, conf, iiPrefixConf.d_premises);
151
777
  setPendingConflict(iiPrefixConf);
152
}
153
154
777
void SolverState::setPendingConflict(InferInfo& ii)
155
{
156
777
  if (!d_pendingConflictSet.get())
157
  {
158
777
    d_pendingConflict = ii;
159
777
    d_pendingConflictSet.set(true);
160
  }
161
777
}
162
163
1235122
bool SolverState::hasPendingConflict() const { return d_pendingConflictSet; }
164
165
647
bool SolverState::getPendingConflict(InferInfo& ii) const
166
{
167
647
  if (d_pendingConflictSet)
168
  {
169
647
    ii = d_pendingConflict;
170
647
    return true;
171
  }
172
  return false;
173
}
174
175
10351
std::pair<bool, Node> SolverState::entailmentCheck(options::TheoryOfMode mode,
176
                                                   TNode lit)
177
{
178
10351
  return d_valuation.entailmentCheck(mode, lit);
179
}
180
181
25005
void SolverState::separateByLength(
182
    const std::vector<Node>& n,
183
    std::map<TypeNode, std::vector<std::vector<Node>>>& cols,
184
    std::map<TypeNode, std::vector<Node>>& lts)
185
{
186
25005
  unsigned leqc_counter = 0;
187
  // map (length, type) to an equivalence class identifier
188
50010
  std::map<std::pair<Node, TypeNode>, unsigned> eqc_to_leqc;
189
  // backwards map
190
50010
  std::map<unsigned, std::pair<Node, TypeNode>> leqc_to_eqc;
191
  // Collection of eqc for each identifier. Notice that some identifiers may
192
  // not have an associated length in the mappings above, if the length of
193
  // an equivalence class is unknown.
194
50010
  std::map<unsigned, std::vector<Node> > eqc_to_strings;
195
106486
  for (const Node& eqc : n)
196
  {
197
81481
    Assert(d_ee->getRepresentative(eqc) == eqc);
198
162962
    TypeNode tnEqc = eqc.getType();
199
81481
    EqcInfo* ei = getOrMakeEqcInfo(eqc, false);
200
162962
    Node lt = ei ? ei->d_lengthTerm : Node::null();
201
81481
    if (!lt.isNull())
202
    {
203
162962
      Node r = d_ee->getRepresentative(lt);
204
162962
      std::pair<Node, TypeNode> lkey(r, tnEqc);
205
81481
      if (eqc_to_leqc.find(lkey) == eqc_to_leqc.end())
206
      {
207
51378
        eqc_to_leqc[lkey] = leqc_counter;
208
51378
        leqc_to_eqc[leqc_counter] = lkey;
209
51378
        leqc_counter++;
210
      }
211
81481
      eqc_to_strings[eqc_to_leqc[lkey]].push_back(eqc);
212
    }
213
    else
214
    {
215
      eqc_to_strings[leqc_counter].push_back(eqc);
216
      leqc_counter++;
217
    }
218
  }
219
76383
  for (const std::pair<const unsigned, std::vector<Node> >& p : eqc_to_strings)
220
  {
221
51378
    Assert(!p.second.empty());
222
    // get the type of the collection
223
102756
    TypeNode stn = p.second[0].getType();
224
51378
    cols[stn].emplace_back(p.second.begin(), p.second.end());
225
51378
    lts[stn].push_back(leqc_to_eqc[p.first].first);
226
  }
227
25005
}
228
229
}  // namespace strings
230
}  // namespace theory
231
31137
}  // namespace cvc5