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 |
|
|
22 |
|
using namespace std; |
23 |
|
using namespace cvc5::context; |
24 |
|
using namespace cvc5::kind; |
25 |
|
|
26 |
|
namespace cvc5 { |
27 |
|
namespace theory { |
28 |
|
namespace strings { |
29 |
|
|
30 |
9459 |
SolverState::SolverState(context::Context* c, |
31 |
|
context::UserContext* u, |
32 |
9459 |
Valuation& v) |
33 |
9459 |
: TheoryState(c, u, v), d_eeDisequalities(c), d_pendingConflictSet(c, false), d_pendingConflict(InferenceId::UNKNOWN) |
34 |
|
{ |
35 |
9459 |
d_zero = NodeManager::currentNM()->mkConst(Rational(0)); |
36 |
9459 |
d_false = NodeManager::currentNM()->mkConst(false); |
37 |
9459 |
} |
38 |
|
|
39 |
18918 |
SolverState::~SolverState() |
40 |
|
{ |
41 |
30417 |
for (std::pair<const Node, EqcInfo*>& it : d_eqcInfo) |
42 |
|
{ |
43 |
20958 |
delete it.second; |
44 |
|
} |
45 |
9459 |
} |
46 |
|
|
47 |
9680 |
const context::CDList<Node>& SolverState::getDisequalityList() const |
48 |
|
{ |
49 |
9680 |
return d_eeDisequalities; |
50 |
|
} |
51 |
|
|
52 |
37676 |
void SolverState::addDisequality(TNode t1, TNode t2) |
53 |
|
{ |
54 |
37676 |
d_eeDisequalities.push_back(t1.eqNode(t2)); |
55 |
37676 |
} |
56 |
|
|
57 |
847561 |
EqcInfo* SolverState::getOrMakeEqcInfo(Node eqc, bool doMake) |
58 |
|
{ |
59 |
847561 |
std::map<Node, EqcInfo*>::iterator eqc_i = d_eqcInfo.find(eqc); |
60 |
847561 |
if (eqc_i != d_eqcInfo.end()) |
61 |
|
{ |
62 |
423922 |
return eqc_i->second; |
63 |
|
} |
64 |
423639 |
if (doMake) |
65 |
|
{ |
66 |
20958 |
EqcInfo* ei = new EqcInfo(d_context); |
67 |
20958 |
d_eqcInfo[eqc] = ei; |
68 |
20958 |
return ei; |
69 |
|
} |
70 |
402681 |
return nullptr; |
71 |
|
} |
72 |
|
|
73 |
|
TheoryModel* SolverState::getModel() { return d_valuation.getModel(); } |
74 |
|
|
75 |
442225 |
Node SolverState::getLengthExp(Node t, std::vector<Node>& exp, Node te) |
76 |
|
{ |
77 |
442225 |
Assert(areEqual(t, te)); |
78 |
884450 |
Node lt = utils::mkNLength(te); |
79 |
442225 |
if (hasTerm(lt)) |
80 |
|
{ |
81 |
|
// use own length if it exists, leads to shorter explanation |
82 |
441671 |
return lt; |
83 |
|
} |
84 |
554 |
EqcInfo* ei = getOrMakeEqcInfo(t, false); |
85 |
1108 |
Node lengthTerm = ei ? ei->d_lengthTerm : Node::null(); |
86 |
554 |
if (lengthTerm.isNull()) |
87 |
|
{ |
88 |
|
// typically shouldnt be necessary |
89 |
516 |
lengthTerm = t; |
90 |
|
} |
91 |
1108 |
Debug("strings") << "SolverState::getLengthTerm " << t << " is " << lengthTerm |
92 |
554 |
<< std::endl; |
93 |
554 |
if (te != lengthTerm) |
94 |
|
{ |
95 |
38 |
exp.push_back(te.eqNode(lengthTerm)); |
96 |
|
} |
97 |
|
return Rewriter::rewrite( |
98 |
554 |
NodeManager::currentNM()->mkNode(STRING_LENGTH, lengthTerm)); |
99 |
|
} |
100 |
|
|
101 |
442221 |
Node SolverState::getLength(Node t, std::vector<Node>& exp) |
102 |
|
{ |
103 |
442221 |
return getLengthExp(t, exp, t); |
104 |
|
} |
105 |
|
|
106 |
10568 |
Node SolverState::explainNonEmpty(Node s) |
107 |
|
{ |
108 |
10568 |
Assert(s.getType().isStringLike()); |
109 |
21136 |
Node emp = Word::mkEmptyWord(s.getType()); |
110 |
10568 |
if (areDisequal(s, emp)) |
111 |
|
{ |
112 |
3664 |
return s.eqNode(emp).negate(); |
113 |
|
} |
114 |
13808 |
Node sLen = utils::mkNLength(s); |
115 |
6904 |
if (areDisequal(sLen, d_zero)) |
116 |
|
{ |
117 |
6904 |
return sLen.eqNode(d_zero).negate(); |
118 |
|
} |
119 |
|
return Node::null(); |
120 |
|
} |
121 |
|
|
122 |
444142 |
bool SolverState::isEqualEmptyWord(Node s, Node& emps) |
123 |
|
{ |
124 |
888284 |
Node sr = getRepresentative(s); |
125 |
444142 |
if (sr.isConst()) |
126 |
|
{ |
127 |
96631 |
if (Word::getLength(sr) == 0) |
128 |
|
{ |
129 |
12519 |
emps = sr; |
130 |
12519 |
return true; |
131 |
|
} |
132 |
|
} |
133 |
431623 |
return false; |
134 |
|
} |
135 |
|
|
136 |
26882 |
void SolverState::setPendingPrefixConflictWhen(Node conf) |
137 |
|
{ |
138 |
26882 |
if (conf.isNull() || d_pendingConflictSet.get()) |
139 |
|
{ |
140 |
26658 |
return; |
141 |
|
} |
142 |
448 |
InferInfo iiPrefixConf(InferenceId::STRINGS_PREFIX_CONFLICT); |
143 |
224 |
iiPrefixConf.d_conc = d_false; |
144 |
224 |
utils::flattenOp(AND, conf, iiPrefixConf.d_premises); |
145 |
224 |
setPendingConflict(iiPrefixConf); |
146 |
|
} |
147 |
|
|
148 |
224 |
void SolverState::setPendingConflict(InferInfo& ii) |
149 |
|
{ |
150 |
224 |
if (!d_pendingConflictSet.get()) |
151 |
|
{ |
152 |
224 |
d_pendingConflict = ii; |
153 |
224 |
d_pendingConflictSet.set(true); |
154 |
|
} |
155 |
224 |
} |
156 |
|
|
157 |
412950 |
bool SolverState::hasPendingConflict() const { return d_pendingConflictSet; } |
158 |
|
|
159 |
192 |
bool SolverState::getPendingConflict(InferInfo& ii) const |
160 |
|
{ |
161 |
192 |
if (d_pendingConflictSet) |
162 |
|
{ |
163 |
192 |
ii = d_pendingConflict; |
164 |
192 |
return true; |
165 |
|
} |
166 |
|
return false; |
167 |
|
} |
168 |
|
|
169 |
4788 |
std::pair<bool, Node> SolverState::entailmentCheck(options::TheoryOfMode mode, |
170 |
|
TNode lit) |
171 |
|
{ |
172 |
4788 |
return d_valuation.entailmentCheck(mode, lit); |
173 |
|
} |
174 |
|
|
175 |
17374 |
void SolverState::separateByLength( |
176 |
|
const std::vector<Node>& n, |
177 |
|
std::map<TypeNode, std::vector<std::vector<Node>>>& cols, |
178 |
|
std::map<TypeNode, std::vector<Node>>& lts) |
179 |
|
{ |
180 |
17374 |
unsigned leqc_counter = 0; |
181 |
|
// map (length, type) to an equivalence class identifier |
182 |
34748 |
std::map<std::pair<Node, TypeNode>, unsigned> eqc_to_leqc; |
183 |
|
// backwards map |
184 |
34748 |
std::map<unsigned, std::pair<Node, TypeNode>> leqc_to_eqc; |
185 |
|
// Collection of eqc for each identifier. Notice that some identifiers may |
186 |
|
// not have an associated length in the mappings above, if the length of |
187 |
|
// an equivalence class is unknown. |
188 |
34748 |
std::map<unsigned, std::vector<Node> > eqc_to_strings; |
189 |
17374 |
NodeManager* nm = NodeManager::currentNM(); |
190 |
69503 |
for (const Node& eqc : n) |
191 |
|
{ |
192 |
52129 |
Assert(d_ee->getRepresentative(eqc) == eqc); |
193 |
104258 |
TypeNode tnEqc = eqc.getType(); |
194 |
52129 |
EqcInfo* ei = getOrMakeEqcInfo(eqc, false); |
195 |
104258 |
Node lt = ei ? ei->d_lengthTerm : Node::null(); |
196 |
52129 |
if (!lt.isNull()) |
197 |
|
{ |
198 |
52129 |
lt = nm->mkNode(STRING_LENGTH, lt); |
199 |
104258 |
Node r = d_ee->getRepresentative(lt); |
200 |
104258 |
std::pair<Node, TypeNode> lkey(r, tnEqc); |
201 |
52129 |
if (eqc_to_leqc.find(lkey) == eqc_to_leqc.end()) |
202 |
|
{ |
203 |
31582 |
eqc_to_leqc[lkey] = leqc_counter; |
204 |
31582 |
leqc_to_eqc[leqc_counter] = lkey; |
205 |
31582 |
leqc_counter++; |
206 |
|
} |
207 |
52129 |
eqc_to_strings[eqc_to_leqc[lkey]].push_back(eqc); |
208 |
|
} |
209 |
|
else |
210 |
|
{ |
211 |
|
eqc_to_strings[leqc_counter].push_back(eqc); |
212 |
|
leqc_counter++; |
213 |
|
} |
214 |
|
} |
215 |
48956 |
for (const std::pair<const unsigned, std::vector<Node> >& p : eqc_to_strings) |
216 |
|
{ |
217 |
31582 |
Assert(!p.second.empty()); |
218 |
|
// get the type of the collection |
219 |
63164 |
TypeNode stn = p.second[0].getType(); |
220 |
31582 |
cols[stn].emplace_back(p.second.begin(), p.second.end()); |
221 |
31582 |
lts[stn].push_back(leqc_to_eqc[p.first].first); |
222 |
|
} |
223 |
17374 |
} |
224 |
|
|
225 |
|
} // namespace strings |
226 |
|
} // namespace theory |
227 |
28191 |
} // namespace cvc5 |