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 |
15273 |
SolverState::SolverState(Env& env, Valuation& v) |
32 |
|
: TheoryState(env, v), |
33 |
|
d_eeDisequalities(env.getContext()), |
34 |
|
d_pendingConflictSet(env.getContext(), false), |
35 |
15273 |
d_pendingConflict(InferenceId::UNKNOWN) |
36 |
|
{ |
37 |
15273 |
d_zero = NodeManager::currentNM()->mkConst(Rational(0)); |
38 |
15273 |
d_false = NodeManager::currentNM()->mkConst(false); |
39 |
15273 |
} |
40 |
|
|
41 |
30536 |
SolverState::~SolverState() |
42 |
|
{ |
43 |
71547 |
for (std::pair<const Node, EqcInfo*>& it : d_eqcInfo) |
44 |
|
{ |
45 |
56279 |
delete it.second; |
46 |
|
} |
47 |
15268 |
} |
48 |
|
|
49 |
13934 |
const context::CDList<Node>& SolverState::getDisequalityList() const |
50 |
|
{ |
51 |
13934 |
return d_eeDisequalities; |
52 |
|
} |
53 |
|
|
54 |
104196 |
void SolverState::addDisequality(TNode t1, TNode t2) |
55 |
|
{ |
56 |
104196 |
d_eeDisequalities.push_back(t1.eqNode(t2)); |
57 |
104196 |
} |
58 |
|
|
59 |
2524942 |
EqcInfo* SolverState::getOrMakeEqcInfo(Node eqc, bool doMake) |
60 |
|
{ |
61 |
2524942 |
std::map<Node, EqcInfo*>::iterator eqc_i = d_eqcInfo.find(eqc); |
62 |
2524942 |
if (eqc_i != d_eqcInfo.end()) |
63 |
|
{ |
64 |
1781001 |
return eqc_i->second; |
65 |
|
} |
66 |
743941 |
if (doMake) |
67 |
|
{ |
68 |
56279 |
EqcInfo* ei = new EqcInfo(d_env.getContext()); |
69 |
56279 |
d_eqcInfo[eqc] = ei; |
70 |
56279 |
return ei; |
71 |
|
} |
72 |
687662 |
return nullptr; |
73 |
|
} |
74 |
|
|
75 |
|
TheoryModel* SolverState::getModel() { return d_valuation.getModel(); } |
76 |
|
|
77 |
1138668 |
Node SolverState::getLengthExp(Node t, std::vector<Node>& exp, Node te) |
78 |
|
{ |
79 |
1138668 |
Assert(areEqual(t, te)); |
80 |
2277336 |
Node lt = utils::mkNLength(te); |
81 |
1138668 |
if (hasTerm(lt)) |
82 |
|
{ |
83 |
|
// use own length if it exists, leads to shorter explanation |
84 |
1136220 |
return lt; |
85 |
|
} |
86 |
2448 |
EqcInfo* ei = getOrMakeEqcInfo(t, false); |
87 |
4896 |
Node lengthTerm = ei ? ei->d_lengthTerm : Node::null(); |
88 |
2448 |
if (lengthTerm.isNull()) |
89 |
|
{ |
90 |
|
// typically shouldnt be necessary |
91 |
2406 |
lengthTerm = t; |
92 |
|
} |
93 |
|
else |
94 |
|
{ |
95 |
42 |
lengthTerm = lengthTerm[0]; |
96 |
|
} |
97 |
4896 |
Debug("strings") << "SolverState::getLengthTerm " << t << " is " << lengthTerm |
98 |
2448 |
<< std::endl; |
99 |
2448 |
if (te != lengthTerm) |
100 |
|
{ |
101 |
42 |
exp.push_back(te.eqNode(lengthTerm)); |
102 |
|
} |
103 |
2448 |
return rewrite(NodeManager::currentNM()->mkNode(STRING_LENGTH, lengthTerm)); |
104 |
|
} |
105 |
|
|
106 |
1138664 |
Node SolverState::getLength(Node t, std::vector<Node>& exp) |
107 |
|
{ |
108 |
1138664 |
return getLengthExp(t, exp, t); |
109 |
|
} |
110 |
|
|
111 |
17354 |
Node SolverState::explainNonEmpty(Node s) |
112 |
|
{ |
113 |
17354 |
Assert(s.getType().isStringLike()); |
114 |
34708 |
Node emp = Word::mkEmptyWord(s.getType()); |
115 |
17354 |
if (areDisequal(s, emp)) |
116 |
|
{ |
117 |
7480 |
return s.eqNode(emp).negate(); |
118 |
|
} |
119 |
19748 |
Node sLen = utils::mkNLength(s); |
120 |
9874 |
if (areDisequal(sLen, d_zero)) |
121 |
|
{ |
122 |
9863 |
return sLen.eqNode(d_zero).negate(); |
123 |
|
} |
124 |
11 |
return Node::null(); |
125 |
|
} |
126 |
|
|
127 |
944416 |
bool SolverState::isEqualEmptyWord(Node s, Node& emps) |
128 |
|
{ |
129 |
1888832 |
Node sr = getRepresentative(s); |
130 |
944416 |
if (sr.isConst()) |
131 |
|
{ |
132 |
246176 |
if (Word::getLength(sr) == 0) |
133 |
|
{ |
134 |
34214 |
emps = sr; |
135 |
34214 |
return true; |
136 |
|
} |
137 |
|
} |
138 |
910202 |
return false; |
139 |
|
} |
140 |
|
|
141 |
796 |
void SolverState::setPendingMergeConflict(Node conf, InferenceId id) |
142 |
|
{ |
143 |
796 |
if (d_pendingConflictSet.get()) |
144 |
|
{ |
145 |
|
// already set conflict |
146 |
62 |
return; |
147 |
|
} |
148 |
1468 |
InferInfo iiPrefixConf(id); |
149 |
734 |
iiPrefixConf.d_conc = d_false; |
150 |
734 |
utils::flattenOp(AND, conf, iiPrefixConf.d_premises); |
151 |
734 |
setPendingConflict(iiPrefixConf); |
152 |
|
} |
153 |
|
|
154 |
734 |
void SolverState::setPendingConflict(InferInfo& ii) |
155 |
|
{ |
156 |
734 |
if (!d_pendingConflictSet.get()) |
157 |
|
{ |
158 |
734 |
d_pendingConflict = ii; |
159 |
734 |
d_pendingConflictSet.set(true); |
160 |
|
} |
161 |
734 |
} |
162 |
|
|
163 |
1071369 |
bool SolverState::hasPendingConflict() const { return d_pendingConflictSet; } |
164 |
|
|
165 |
617 |
bool SolverState::getPendingConflict(InferInfo& ii) const |
166 |
|
{ |
167 |
617 |
if (d_pendingConflictSet) |
168 |
|
{ |
169 |
617 |
ii = d_pendingConflict; |
170 |
617 |
return true; |
171 |
|
} |
172 |
|
return false; |
173 |
|
} |
174 |
|
|
175 |
8430 |
std::pair<bool, Node> SolverState::entailmentCheck(options::TheoryOfMode mode, |
176 |
|
TNode lit) |
177 |
|
{ |
178 |
8430 |
return d_valuation.entailmentCheck(mode, lit); |
179 |
|
} |
180 |
|
|
181 |
24957 |
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 |
24957 |
unsigned leqc_counter = 0; |
187 |
|
// map (length, type) to an equivalence class identifier |
188 |
49914 |
std::map<std::pair<Node, TypeNode>, unsigned> eqc_to_leqc; |
189 |
|
// backwards map |
190 |
49914 |
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 |
49914 |
std::map<unsigned, std::vector<Node> > eqc_to_strings; |
195 |
104800 |
for (const Node& eqc : n) |
196 |
|
{ |
197 |
79843 |
Assert(d_ee->getRepresentative(eqc) == eqc); |
198 |
159686 |
TypeNode tnEqc = eqc.getType(); |
199 |
79843 |
EqcInfo* ei = getOrMakeEqcInfo(eqc, false); |
200 |
159686 |
Node lt = ei ? ei->d_lengthTerm : Node::null(); |
201 |
79843 |
if (!lt.isNull()) |
202 |
|
{ |
203 |
159686 |
Node r = d_ee->getRepresentative(lt); |
204 |
159686 |
std::pair<Node, TypeNode> lkey(r, tnEqc); |
205 |
79843 |
if (eqc_to_leqc.find(lkey) == eqc_to_leqc.end()) |
206 |
|
{ |
207 |
50529 |
eqc_to_leqc[lkey] = leqc_counter; |
208 |
50529 |
leqc_to_eqc[leqc_counter] = lkey; |
209 |
50529 |
leqc_counter++; |
210 |
|
} |
211 |
79843 |
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 |
75486 |
for (const std::pair<const unsigned, std::vector<Node> >& p : eqc_to_strings) |
220 |
|
{ |
221 |
50529 |
Assert(!p.second.empty()); |
222 |
|
// get the type of the collection |
223 |
101058 |
TypeNode stn = p.second[0].getType(); |
224 |
50529 |
cols[stn].emplace_back(p.second.begin(), p.second.end()); |
225 |
50529 |
lts[stn].push_back(leqc_to_eqc[p.first].first); |
226 |
|
} |
227 |
24957 |
} |
228 |
|
|
229 |
|
} // namespace strings |
230 |
|
} // namespace theory |
231 |
31137 |
} // namespace cvc5 |