1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Tim King, Alex Ozdemir, Haniel Barbosa |
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 |
|
* [[ Add one-line brief description here ]] |
14 |
|
* |
15 |
|
* [[ Add lengthier description here ]] |
16 |
|
* \todo document this file |
17 |
|
*/ |
18 |
|
#include "theory/arith/constraint.h" |
19 |
|
|
20 |
|
#include <algorithm> |
21 |
|
#include <ostream> |
22 |
|
#include <unordered_set> |
23 |
|
|
24 |
|
#include "base/output.h" |
25 |
|
#include "proof/eager_proof_generator.h" |
26 |
|
#include "proof/proof_node_manager.h" |
27 |
|
#include "smt/smt_statistics_registry.h" |
28 |
|
#include "theory/arith/arith_utilities.h" |
29 |
|
#include "theory/arith/congruence_manager.h" |
30 |
|
#include "theory/arith/normal_form.h" |
31 |
|
#include "theory/arith/partial_model.h" |
32 |
|
#include "theory/builtin/proof_checker.h" |
33 |
|
#include "theory/rewriter.h" |
34 |
|
|
35 |
|
using namespace std; |
36 |
|
using namespace cvc5::kind; |
37 |
|
|
38 |
|
namespace cvc5 { |
39 |
|
namespace theory { |
40 |
|
namespace arith { |
41 |
|
|
42 |
|
/** Given a simplifiedKind this returns the corresponding ConstraintType. */ |
43 |
|
//ConstraintType constraintTypeOfLiteral(Kind k); |
44 |
541180 |
ConstraintType Constraint::constraintTypeOfComparison(const Comparison& cmp){ |
45 |
541180 |
Kind k = cmp.comparisonKind(); |
46 |
541180 |
switch(k){ |
47 |
141947 |
case LT: |
48 |
|
case LEQ: |
49 |
|
{ |
50 |
283894 |
Polynomial l = cmp.getLeft(); |
51 |
141947 |
if(l.leadingCoefficientIsPositive()){ // (< x c) |
52 |
117000 |
return UpperBound; |
53 |
|
}else{ |
54 |
24947 |
return LowerBound; // (< (-x) c) |
55 |
|
} |
56 |
|
} |
57 |
143213 |
case GT: |
58 |
|
case GEQ: |
59 |
|
{ |
60 |
286426 |
Polynomial l = cmp.getLeft(); |
61 |
143213 |
if(l.leadingCoefficientIsPositive()){ |
62 |
118070 |
return LowerBound; // (> x c) |
63 |
|
}else{ |
64 |
25143 |
return UpperBound; // (> (-x) c) |
65 |
|
} |
66 |
|
} |
67 |
128123 |
case EQUAL: |
68 |
128123 |
return Equality; |
69 |
127897 |
case DISTINCT: |
70 |
127897 |
return Disequality; |
71 |
|
default: Unhandled() << k; |
72 |
|
} |
73 |
|
} |
74 |
|
|
75 |
669334 |
Constraint::Constraint(ArithVar x, ConstraintType t, const DeltaRational& v) |
76 |
|
: d_variable(x), |
77 |
|
d_type(t), |
78 |
|
d_value(v), |
79 |
|
d_database(NULL), |
80 |
|
d_literal(Node::null()), |
81 |
|
d_negation(NullConstraint), |
82 |
|
d_canBePropagated(false), |
83 |
|
d_assertionOrder(AssertionOrderSentinel), |
84 |
|
d_witness(TNode::null()), |
85 |
|
d_crid(ConstraintRuleIdSentinel), |
86 |
|
d_split(false), |
87 |
669334 |
d_variablePosition() |
88 |
|
{ |
89 |
669334 |
Assert(!initialized()); |
90 |
669334 |
} |
91 |
|
|
92 |
|
|
93 |
|
std::ostream& operator<<(std::ostream& o, const ArithProofType apt){ |
94 |
|
switch(apt){ |
95 |
|
case NoAP: o << "NoAP"; break; |
96 |
|
case AssumeAP: o << "AssumeAP"; break; |
97 |
|
case InternalAssumeAP: o << "InternalAssumeAP"; break; |
98 |
|
case FarkasAP: o << "FarkasAP"; break; |
99 |
|
case TrichotomyAP: o << "TrichotomyAP"; break; |
100 |
|
case EqualityEngineAP: o << "EqualityEngineAP"; break; |
101 |
|
case IntTightenAP: o << "IntTightenAP"; break; |
102 |
|
case IntHoleAP: o << "IntHoleAP"; break; |
103 |
|
default: break; |
104 |
|
} |
105 |
|
return o; |
106 |
|
} |
107 |
|
|
108 |
|
std::ostream& operator<<(std::ostream& o, const ConstraintCP c){ |
109 |
|
if(c == NullConstraint){ |
110 |
|
return o << "NullConstraint"; |
111 |
|
}else{ |
112 |
|
return o << *c; |
113 |
|
} |
114 |
|
} |
115 |
|
|
116 |
|
std::ostream& operator<<(std::ostream& o, const ConstraintP c){ |
117 |
|
if(c == NullConstraint){ |
118 |
|
return o << "NullConstraint"; |
119 |
|
}else{ |
120 |
|
return o << *c; |
121 |
|
} |
122 |
|
} |
123 |
|
|
124 |
|
std::ostream& operator<<(std::ostream& o, const ConstraintType t){ |
125 |
|
switch(t){ |
126 |
|
case LowerBound: |
127 |
|
return o << ">="; |
128 |
|
case UpperBound: |
129 |
|
return o << "<="; |
130 |
|
case Equality: |
131 |
|
return o << "="; |
132 |
|
case Disequality: |
133 |
|
return o << "!="; |
134 |
|
default: |
135 |
|
Unreachable(); |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
|
std::ostream& operator<<(std::ostream& o, const Constraint& c){ |
140 |
|
o << c.getVariable() << ' ' << c.getType() << ' ' << c.getValue(); |
141 |
|
if(c.hasLiteral()){ |
142 |
|
o << "(node " << c.getLiteral() << ')'; |
143 |
|
} |
144 |
|
return o; |
145 |
|
} |
146 |
|
|
147 |
|
std::ostream& operator<<(std::ostream& o, const ValueCollection& vc){ |
148 |
|
o << "{"; |
149 |
|
bool pending = false; |
150 |
|
if(vc.hasEquality()){ |
151 |
|
o << "eq: " << vc.getEquality(); |
152 |
|
pending = true; |
153 |
|
} |
154 |
|
if(vc.hasLowerBound()){ |
155 |
|
if(pending){ |
156 |
|
o << ", "; |
157 |
|
} |
158 |
|
o << "lb: " << vc.getLowerBound(); |
159 |
|
pending = true; |
160 |
|
} |
161 |
|
if(vc.hasUpperBound()){ |
162 |
|
if(pending){ |
163 |
|
o << ", "; |
164 |
|
} |
165 |
|
o << "ub: " << vc.getUpperBound(); |
166 |
|
pending = true; |
167 |
|
} |
168 |
|
if(vc.hasDisequality()){ |
169 |
|
if(pending){ |
170 |
|
o << ", "; |
171 |
|
} |
172 |
|
o << "de: " << vc.getDisequality(); |
173 |
|
} |
174 |
|
return o << "}"; |
175 |
|
} |
176 |
|
|
177 |
|
std::ostream& operator<<(std::ostream& o, const ConstraintCPVec& v){ |
178 |
|
o << "[" << v.size() << "x"; |
179 |
|
ConstraintCPVec::const_iterator i, end; |
180 |
|
for(i=v.begin(), end=v.end(); i != end; ++i){ |
181 |
|
ConstraintCP c = *i; |
182 |
|
o << ", " << (*c); |
183 |
|
} |
184 |
|
o << "]"; |
185 |
|
return o; |
186 |
|
} |
187 |
|
|
188 |
|
void Constraint::debugPrint() const { CVC5Message() << *this << endl; } |
189 |
|
|
190 |
2002284 |
ValueCollection::ValueCollection() |
191 |
|
: d_lowerBound(NullConstraint), |
192 |
|
d_upperBound(NullConstraint), |
193 |
|
d_equality(NullConstraint), |
194 |
2002284 |
d_disequality(NullConstraint) |
195 |
2002284 |
{} |
196 |
|
|
197 |
19129741 |
bool ValueCollection::hasLowerBound() const{ |
198 |
19129741 |
return d_lowerBound != NullConstraint; |
199 |
|
} |
200 |
|
|
201 |
20602888 |
bool ValueCollection::hasUpperBound() const{ |
202 |
20602888 |
return d_upperBound != NullConstraint; |
203 |
|
} |
204 |
|
|
205 |
4388988 |
bool ValueCollection::hasEquality() const{ |
206 |
4388988 |
return d_equality != NullConstraint; |
207 |
|
} |
208 |
|
|
209 |
13839236 |
bool ValueCollection::hasDisequality() const { |
210 |
13839236 |
return d_disequality != NullConstraint; |
211 |
|
} |
212 |
|
|
213 |
3948307 |
ConstraintP ValueCollection::getLowerBound() const { |
214 |
3948307 |
Assert(hasLowerBound()); |
215 |
3948307 |
return d_lowerBound; |
216 |
|
} |
217 |
|
|
218 |
4039871 |
ConstraintP ValueCollection::getUpperBound() const { |
219 |
4039871 |
Assert(hasUpperBound()); |
220 |
4039871 |
return d_upperBound; |
221 |
|
} |
222 |
|
|
223 |
345693 |
ConstraintP ValueCollection::getEquality() const { |
224 |
345693 |
Assert(hasEquality()); |
225 |
345693 |
return d_equality; |
226 |
|
} |
227 |
|
|
228 |
2052337 |
ConstraintP ValueCollection::getDisequality() const { |
229 |
2052337 |
Assert(hasDisequality()); |
230 |
2052337 |
return d_disequality; |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
448011 |
void ValueCollection::push_into(std::vector<ConstraintP>& vec) const { |
235 |
448011 |
Debug("arith::constraint") << "push_into " << *this << endl; |
236 |
448011 |
if(hasEquality()){ |
237 |
132097 |
vec.push_back(d_equality); |
238 |
|
} |
239 |
448011 |
if(hasLowerBound()){ |
240 |
201824 |
vec.push_back(d_lowerBound); |
241 |
|
} |
242 |
448011 |
if(hasUpperBound()){ |
243 |
201824 |
vec.push_back(d_upperBound); |
244 |
|
} |
245 |
448011 |
if(hasDisequality()){ |
246 |
132097 |
vec.push_back(d_disequality); |
247 |
|
} |
248 |
448011 |
} |
249 |
|
|
250 |
|
ValueCollection ValueCollection::mkFromConstraint(ConstraintP c){ |
251 |
|
ValueCollection ret; |
252 |
|
Assert(ret.empty()); |
253 |
|
switch(c->getType()){ |
254 |
|
case LowerBound: |
255 |
|
ret.d_lowerBound = c; |
256 |
|
break; |
257 |
|
case UpperBound: |
258 |
|
ret.d_upperBound = c; |
259 |
|
break; |
260 |
|
case Equality: |
261 |
|
ret.d_equality = c; |
262 |
|
break; |
263 |
|
case Disequality: |
264 |
|
ret.d_disequality = c; |
265 |
|
break; |
266 |
|
default: |
267 |
|
Unreachable(); |
268 |
|
} |
269 |
|
return ret; |
270 |
|
} |
271 |
|
|
272 |
3281083 |
bool ValueCollection::hasConstraintOfType(ConstraintType t) const{ |
273 |
3281083 |
switch(t){ |
274 |
1021243 |
case LowerBound: |
275 |
1021243 |
return hasLowerBound(); |
276 |
1880269 |
case UpperBound: |
277 |
1880269 |
return hasUpperBound(); |
278 |
379571 |
case Equality: |
279 |
379571 |
return hasEquality(); |
280 |
|
case Disequality: |
281 |
|
return hasDisequality(); |
282 |
|
default: |
283 |
|
Unreachable(); |
284 |
|
} |
285 |
|
} |
286 |
|
|
287 |
226481 |
ArithVar ValueCollection::getVariable() const{ |
288 |
226481 |
Assert(!empty()); |
289 |
226481 |
return nonNull()->getVariable(); |
290 |
|
} |
291 |
|
|
292 |
226481 |
const DeltaRational& ValueCollection::getValue() const{ |
293 |
226481 |
Assert(!empty()); |
294 |
226481 |
return nonNull()->getValue(); |
295 |
|
} |
296 |
|
|
297 |
667842 |
void ValueCollection::add(ConstraintP c){ |
298 |
667842 |
Assert(c != NullConstraint); |
299 |
|
|
300 |
667842 |
Assert(empty() || getVariable() == c->getVariable()); |
301 |
667842 |
Assert(empty() || getValue() == c->getValue()); |
302 |
|
|
303 |
667842 |
switch(c->getType()){ |
304 |
201824 |
case LowerBound: |
305 |
201824 |
Assert(!hasLowerBound()); |
306 |
201824 |
d_lowerBound = c; |
307 |
201824 |
break; |
308 |
132097 |
case Equality: |
309 |
132097 |
Assert(!hasEquality()); |
310 |
132097 |
d_equality = c; |
311 |
132097 |
break; |
312 |
201824 |
case UpperBound: |
313 |
201824 |
Assert(!hasUpperBound()); |
314 |
201824 |
d_upperBound = c; |
315 |
201824 |
break; |
316 |
132097 |
case Disequality: |
317 |
132097 |
Assert(!hasDisequality()); |
318 |
132097 |
d_disequality = c; |
319 |
132097 |
break; |
320 |
|
default: |
321 |
|
Unreachable(); |
322 |
|
} |
323 |
667842 |
} |
324 |
|
|
325 |
2498659 |
ConstraintP ValueCollection::getConstraintOfType(ConstraintType t) const{ |
326 |
2498659 |
switch(t){ |
327 |
652049 |
case LowerBound: Assert(hasLowerBound()); return d_lowerBound; |
328 |
247474 |
case Equality: Assert(hasEquality()); return d_equality; |
329 |
1599136 |
case UpperBound: Assert(hasUpperBound()); return d_upperBound; |
330 |
|
case Disequality: Assert(hasDisequality()); return d_disequality; |
331 |
|
default: Unreachable(); |
332 |
|
} |
333 |
|
} |
334 |
|
|
335 |
667842 |
void ValueCollection::remove(ConstraintType t){ |
336 |
667842 |
switch(t){ |
337 |
201824 |
case LowerBound: |
338 |
201824 |
Assert(hasLowerBound()); |
339 |
201824 |
d_lowerBound = NullConstraint; |
340 |
201824 |
break; |
341 |
132097 |
case Equality: |
342 |
132097 |
Assert(hasEquality()); |
343 |
132097 |
d_equality = NullConstraint; |
344 |
132097 |
break; |
345 |
201824 |
case UpperBound: |
346 |
201824 |
Assert(hasUpperBound()); |
347 |
201824 |
d_upperBound = NullConstraint; |
348 |
201824 |
break; |
349 |
132097 |
case Disequality: |
350 |
132097 |
Assert(hasDisequality()); |
351 |
132097 |
d_disequality = NullConstraint; |
352 |
132097 |
break; |
353 |
|
default: |
354 |
|
Unreachable(); |
355 |
|
} |
356 |
667842 |
} |
357 |
|
|
358 |
2456488 |
bool ValueCollection::empty() const{ |
359 |
|
return |
360 |
5922553 |
!(hasLowerBound() || |
361 |
4140342 |
hasUpperBound() || |
362 |
2018310 |
hasEquality() || |
363 |
3800521 |
hasDisequality()); |
364 |
|
} |
365 |
|
|
366 |
452962 |
ConstraintP ValueCollection::nonNull() const{ |
367 |
|
//This can be optimized by caching, but this is not necessary yet! |
368 |
|
/* "Premature optimization is the root of all evil." */ |
369 |
452962 |
if(hasLowerBound()){ |
370 |
132790 |
return d_lowerBound; |
371 |
320172 |
}else if(hasUpperBound()){ |
372 |
49082 |
return d_upperBound; |
373 |
271090 |
}else if(hasEquality()){ |
374 |
271090 |
return d_equality; |
375 |
|
}else if(hasDisequality()){ |
376 |
|
return d_disequality; |
377 |
|
}else{ |
378 |
|
return NullConstraint; |
379 |
|
} |
380 |
|
} |
381 |
|
|
382 |
2724860 |
bool Constraint::initialized() const { |
383 |
2724860 |
return d_database != NULL; |
384 |
|
} |
385 |
|
|
386 |
|
const ConstraintDatabase& Constraint::getDatabase() const{ |
387 |
|
Assert(initialized()); |
388 |
|
return *d_database; |
389 |
|
} |
390 |
|
|
391 |
667842 |
void Constraint::initialize(ConstraintDatabase* db, SortedConstraintMapIterator v, ConstraintP negation){ |
392 |
667842 |
Assert(!initialized()); |
393 |
667842 |
d_database = db; |
394 |
667842 |
d_variablePosition = v; |
395 |
667842 |
d_negation = negation; |
396 |
667842 |
} |
397 |
|
|
398 |
1338668 |
Constraint::~Constraint() { |
399 |
|
// Call this instead of safeToGarbageCollect() |
400 |
669334 |
Assert(!contextDependentDataIsSet()); |
401 |
|
|
402 |
669334 |
if(initialized()){ |
403 |
667842 |
ValueCollection& vc = d_variablePosition->second; |
404 |
667842 |
Debug("arith::constraint") << "removing" << vc << endl; |
405 |
|
|
406 |
667842 |
vc.remove(getType()); |
407 |
|
|
408 |
667842 |
if(vc.empty()){ |
409 |
448011 |
Debug("arith::constraint") << "erasing" << vc << endl; |
410 |
448011 |
SortedConstraintMap& perVariable = d_database->getVariableSCM(getVariable()); |
411 |
448011 |
perVariable.erase(d_variablePosition); |
412 |
|
} |
413 |
|
|
414 |
667842 |
if(hasLiteral()){ |
415 |
542672 |
d_database->d_nodetoConstraintMap.erase(getLiteral()); |
416 |
|
} |
417 |
|
} |
418 |
669334 |
} |
419 |
|
|
420 |
25579137 |
const ConstraintRule& Constraint::getConstraintRule() const { |
421 |
25579137 |
Assert(hasProof()); |
422 |
25579137 |
return d_database->d_watches->d_constraintProofs[d_crid]; |
423 |
|
} |
424 |
|
|
425 |
2928123 |
const ValueCollection& Constraint::getValueCollection() const{ |
426 |
2928123 |
return d_variablePosition->second; |
427 |
|
} |
428 |
|
|
429 |
|
|
430 |
77151 |
ConstraintP Constraint::getCeiling() { |
431 |
77151 |
Debug("getCeiling") << "Constraint_::getCeiling on " << *this << endl; |
432 |
77151 |
Assert(getValue().getInfinitesimalPart().sgn() > 0); |
433 |
|
|
434 |
154302 |
const DeltaRational ceiling(getValue().ceiling()); |
435 |
154302 |
return d_database->getConstraint(getVariable(), getType(), ceiling); |
436 |
|
} |
437 |
|
|
438 |
1193875 |
ConstraintP Constraint::getFloor() { |
439 |
1193875 |
Assert(getValue().getInfinitesimalPart().sgn() < 0); |
440 |
|
|
441 |
2387750 |
const DeltaRational floor(Rational(getValue().floor())); |
442 |
2387750 |
return d_database->getConstraint(getVariable(), getType(), floor); |
443 |
|
} |
444 |
|
|
445 |
846914 |
void Constraint::setCanBePropagated() { |
446 |
846914 |
Assert(!canBePropagated()); |
447 |
846914 |
d_database->pushCanBePropagatedWatch(this); |
448 |
846914 |
} |
449 |
|
|
450 |
5595444 |
void Constraint::setAssertedToTheTheory(TNode witness, bool nowInConflict) { |
451 |
5595444 |
Assert(hasLiteral()); |
452 |
5595444 |
Assert(!assertedToTheTheory()); |
453 |
5595444 |
Assert(negationHasProof() == nowInConflict); |
454 |
5595444 |
d_database->pushAssertionOrderWatch(this, witness); |
455 |
|
|
456 |
5595444 |
if(Debug.isOn("constraint::conflictCommit") && nowInConflict ){ |
457 |
|
Debug("constraint::conflictCommit") << "inConflict@setAssertedToTheTheory"; |
458 |
|
Debug("constraint::conflictCommit") << "\t" << this << std::endl; |
459 |
|
Debug("constraint::conflictCommit") << "\t" << getNegation() << std::endl; |
460 |
|
Debug("constraint::conflictCommit") << "\t" << getNegation()->externalExplainByAssertions() << std::endl; |
461 |
|
|
462 |
|
} |
463 |
5595444 |
} |
464 |
|
|
465 |
|
bool Constraint::satisfiedBy(const DeltaRational& dr) const { |
466 |
|
switch(getType()){ |
467 |
|
case LowerBound: |
468 |
|
return getValue() <= dr; |
469 |
|
case Equality: |
470 |
|
return getValue() == dr; |
471 |
|
case UpperBound: |
472 |
|
return getValue() >= dr; |
473 |
|
case Disequality: |
474 |
|
return getValue() != dr; |
475 |
|
} |
476 |
|
Unreachable(); |
477 |
|
} |
478 |
|
|
479 |
10794679 |
bool Constraint::isInternalAssumption() const { |
480 |
10794679 |
return getProofType() == InternalAssumeAP; |
481 |
|
} |
482 |
|
|
483 |
|
TrustNode Constraint::externalExplainByAssertions() const |
484 |
|
{ |
485 |
|
NodeBuilder nb(kind::AND); |
486 |
|
auto pfFromAssumptions = externalExplain(nb, AssertionOrderSentinel); |
487 |
|
Node exp = safeConstructNary(nb); |
488 |
|
if (d_database->isProofEnabled()) |
489 |
|
{ |
490 |
|
std::vector<Node> assumptions; |
491 |
|
if (exp.getKind() == Kind::AND) |
492 |
|
{ |
493 |
|
assumptions.insert(assumptions.end(), exp.begin(), exp.end()); |
494 |
|
} |
495 |
|
else |
496 |
|
{ |
497 |
|
assumptions.push_back(exp); |
498 |
|
} |
499 |
|
auto pf = d_database->d_pnm->mkScope(pfFromAssumptions, assumptions); |
500 |
|
return d_database->d_pfGen->mkTrustedPropagation( |
501 |
|
getLiteral(), safeConstructNary(Kind::AND, assumptions), pf); |
502 |
|
} |
503 |
|
return TrustNode::mkTrustPropExp(getLiteral(), exp); |
504 |
|
} |
505 |
|
|
506 |
11327939 |
bool Constraint::isAssumption() const { |
507 |
11327939 |
return getProofType() == AssumeAP; |
508 |
|
} |
509 |
|
|
510 |
513401 |
bool Constraint::hasEqualityEngineProof() const { |
511 |
513401 |
return getProofType() == EqualityEngineAP; |
512 |
|
} |
513 |
|
|
514 |
|
bool Constraint::hasFarkasProof() const { |
515 |
|
return getProofType() == FarkasAP; |
516 |
|
} |
517 |
|
|
518 |
|
bool Constraint::hasSimpleFarkasProof() const |
519 |
|
{ |
520 |
|
Debug("constraints::hsfp") << "hasSimpleFarkasProof " << this << std::endl; |
521 |
|
if (!hasFarkasProof()) |
522 |
|
{ |
523 |
|
Debug("constraints::hsfp") << "There is no simple Farkas proof because " |
524 |
|
"there is no farkas proof." |
525 |
|
<< std::endl; |
526 |
|
return false; |
527 |
|
} |
528 |
|
|
529 |
|
// For each antecdent ... |
530 |
|
AntecedentId i = getConstraintRule().d_antecedentEnd; |
531 |
|
for (ConstraintCP a = d_database->getAntecedent(i); a != NullConstraint; |
532 |
|
a = d_database->getAntecedent(--i)) |
533 |
|
{ |
534 |
|
// ... that antecdent must be an assumption OR a tightened assumption ... |
535 |
|
if (a->isPossiblyTightenedAssumption()) |
536 |
|
{ |
537 |
|
continue; |
538 |
|
} |
539 |
|
|
540 |
|
// ... otherwise, we do not have a simple Farkas proof. |
541 |
|
if (Debug.isOn("constraints::hsfp")) |
542 |
|
{ |
543 |
|
Debug("constraints::hsfp") << "There is no simple Farkas proof b/c there " |
544 |
|
"is an antecdent w/ rule "; |
545 |
|
a->getConstraintRule().print(Debug("constraints::hsfp")); |
546 |
|
Debug("constraints::hsfp") << std::endl; |
547 |
|
} |
548 |
|
|
549 |
|
return false; |
550 |
|
} |
551 |
|
return true; |
552 |
|
} |
553 |
|
|
554 |
|
bool Constraint::isPossiblyTightenedAssumption() const |
555 |
|
{ |
556 |
|
// ... that antecdent must be an assumption ... |
557 |
|
|
558 |
|
if (isAssumption()) return true; |
559 |
|
if (!hasIntTightenProof()) return false; |
560 |
|
if (getConstraintRule().d_antecedentEnd == AntecedentIdSentinel) return false; |
561 |
|
return d_database->getAntecedent(getConstraintRule().d_antecedentEnd) |
562 |
|
->isAssumption(); |
563 |
|
} |
564 |
|
|
565 |
|
bool Constraint::hasIntTightenProof() const { |
566 |
|
return getProofType() == IntTightenAP; |
567 |
|
} |
568 |
|
|
569 |
|
bool Constraint::hasIntHoleProof() const { |
570 |
|
return getProofType() == IntHoleAP; |
571 |
|
} |
572 |
|
|
573 |
|
bool Constraint::hasTrichotomyProof() const { |
574 |
|
return getProofType() == TrichotomyAP; |
575 |
|
} |
576 |
|
|
577 |
|
void Constraint::printProofTree(std::ostream& out, size_t depth) const |
578 |
|
{ |
579 |
|
if (ARITH_PROOF_ON()) |
580 |
|
{ |
581 |
|
const ConstraintRule& rule = getConstraintRule(); |
582 |
|
out << std::string(2 * depth, ' ') << "* " << getVariable() << " ["; |
583 |
|
out << getProofLiteral(); |
584 |
|
if (assertedToTheTheory()) |
585 |
|
{ |
586 |
|
out << " | wit: " << getWitness(); |
587 |
|
} |
588 |
|
out << "]" << ' ' << getType() << ' ' << getValue() << " (" |
589 |
|
<< getProofType() << ")"; |
590 |
|
if (getProofType() == FarkasAP) |
591 |
|
{ |
592 |
|
out << " ["; |
593 |
|
bool first = true; |
594 |
|
for (const auto& coeff : *rule.d_farkasCoefficients) |
595 |
|
{ |
596 |
|
if (not first) |
597 |
|
{ |
598 |
|
out << ", "; |
599 |
|
} |
600 |
|
first = false; |
601 |
|
out << coeff; |
602 |
|
} |
603 |
|
out << "]"; |
604 |
|
} |
605 |
|
out << endl; |
606 |
|
|
607 |
|
for (AntecedentId i = rule.d_antecedentEnd; i != AntecedentIdSentinel; --i) |
608 |
|
{ |
609 |
|
ConstraintCP antecdent = d_database->getAntecedent(i); |
610 |
|
if (antecdent == NullConstraint) |
611 |
|
{ |
612 |
|
break; |
613 |
|
} |
614 |
|
antecdent->printProofTree(out, depth + 1); |
615 |
|
} |
616 |
|
return; |
617 |
|
} |
618 |
|
out << "Cannot print proof. This is not a proof build." << endl; |
619 |
|
} |
620 |
|
|
621 |
542672 |
bool Constraint::sanityChecking(Node n) const { |
622 |
1085344 |
Comparison cmp = Comparison::parseNormalForm(n); |
623 |
542672 |
Kind k = cmp.comparisonKind(); |
624 |
1085344 |
Polynomial pleft = cmp.normalizedVariablePart(); |
625 |
542672 |
Assert(k == EQUAL || k == DISTINCT || pleft.leadingCoefficientIsPositive()); |
626 |
542672 |
Assert(k != EQUAL || Monomial::isMember(n[0])); |
627 |
542672 |
Assert(k != DISTINCT || Monomial::isMember(n[0][0])); |
628 |
|
|
629 |
1085344 |
TNode left = pleft.getNode(); |
630 |
1085344 |
DeltaRational right = cmp.normalizedDeltaRational(); |
631 |
|
|
632 |
542672 |
const ArithVariables& avariables = d_database->getArithVariables(); |
633 |
|
|
634 |
542672 |
Debug("Constraint::sanityChecking") << cmp.getNode() << endl; |
635 |
542672 |
Debug("Constraint::sanityChecking") << k << endl; |
636 |
542672 |
Debug("Constraint::sanityChecking") << pleft.getNode() << endl; |
637 |
542672 |
Debug("Constraint::sanityChecking") << left << endl; |
638 |
542672 |
Debug("Constraint::sanityChecking") << right << endl; |
639 |
542672 |
Debug("Constraint::sanityChecking") << getValue() << endl; |
640 |
542672 |
Debug("Constraint::sanityChecking") << avariables.hasArithVar(left) << endl; |
641 |
542672 |
Debug("Constraint::sanityChecking") << avariables.asArithVar(left) << endl; |
642 |
542672 |
Debug("Constraint::sanityChecking") << getVariable() << endl; |
643 |
|
|
644 |
|
|
645 |
2713360 |
if(avariables.hasArithVar(left) && |
646 |
2713360 |
avariables.asArithVar(left) == getVariable() && |
647 |
542672 |
getValue() == right){ |
648 |
542672 |
switch(getType()){ |
649 |
286426 |
case LowerBound: |
650 |
|
case UpperBound: |
651 |
|
//Be overapproximate |
652 |
286426 |
return k == GT || k == GEQ ||k == LT || k == LEQ; |
653 |
128123 |
case Equality: |
654 |
128123 |
return k == EQUAL; |
655 |
128123 |
case Disequality: |
656 |
128123 |
return k == DISTINCT; |
657 |
|
default: |
658 |
|
Unreachable(); |
659 |
|
} |
660 |
|
}else{ |
661 |
|
return false; |
662 |
|
} |
663 |
|
} |
664 |
|
|
665 |
|
void ConstraintRule::debugPrint() const { |
666 |
|
print(std::cerr); |
667 |
|
} |
668 |
|
|
669 |
|
ConstraintCP ConstraintDatabase::getAntecedent (AntecedentId p) const { |
670 |
|
Assert(p < d_antecedents.size()); |
671 |
|
return d_antecedents[p]; |
672 |
|
} |
673 |
|
|
674 |
|
|
675 |
|
void ConstraintRule::print(std::ostream& out) const { |
676 |
|
RationalVectorCP coeffs = ARITH_NULLPROOF(d_farkasCoefficients); |
677 |
|
out << "{ConstraintRule, "; |
678 |
|
out << d_constraint << std::endl; |
679 |
|
out << "d_proofType= " << d_proofType << ", " << std::endl; |
680 |
|
out << "d_antecedentEnd= "<< d_antecedentEnd << std::endl; |
681 |
|
|
682 |
|
if (d_constraint != NullConstraint && d_antecedentEnd != AntecedentIdSentinel) |
683 |
|
{ |
684 |
|
const ConstraintDatabase& database = d_constraint->getDatabase(); |
685 |
|
|
686 |
|
size_t coeffIterator = (coeffs != RationalVectorCPSentinel) ? coeffs->size()-1 : 0; |
687 |
|
AntecedentId p = d_antecedentEnd; |
688 |
|
// must have at least one antecedent |
689 |
|
ConstraintCP antecedent = database.getAntecedent(p); |
690 |
|
while(antecedent != NullConstraint){ |
691 |
|
if(coeffs != RationalVectorCPSentinel){ |
692 |
|
out << coeffs->at(coeffIterator); |
693 |
|
} else { |
694 |
|
out << "_"; |
695 |
|
} |
696 |
|
out << " * (" << *antecedent << ")" << std::endl; |
697 |
|
|
698 |
|
Assert((coeffs == RationalVectorCPSentinel) || coeffIterator > 0); |
699 |
|
--p; |
700 |
|
coeffIterator = (coeffs != RationalVectorCPSentinel) ? coeffIterator-1 : 0; |
701 |
|
antecedent = database.getAntecedent(p); |
702 |
|
} |
703 |
|
if(coeffs != RationalVectorCPSentinel){ |
704 |
|
out << coeffs->front(); |
705 |
|
} else { |
706 |
|
out << "_"; |
707 |
|
} |
708 |
|
out << " * (" << *(d_constraint->getNegation()) << ")"; |
709 |
|
out << " [not d_constraint] " << endl; |
710 |
|
} |
711 |
|
out << "}"; |
712 |
|
} |
713 |
|
|
714 |
2252819 |
bool Constraint::wellFormedFarkasProof() const { |
715 |
2252819 |
Assert(hasProof()); |
716 |
|
|
717 |
2252819 |
const ConstraintRule& cr = getConstraintRule(); |
718 |
2252819 |
if(cr.d_constraint != this){ return false; } |
719 |
2252819 |
if(cr.d_proofType != FarkasAP){ return false; } |
720 |
|
|
721 |
2252819 |
AntecedentId p = cr.d_antecedentEnd; |
722 |
|
|
723 |
|
// must have at least one antecedent |
724 |
2252819 |
ConstraintCP antecedent = d_database->d_antecedents[p]; |
725 |
2252819 |
if(antecedent == NullConstraint) { return false; } |
726 |
|
|
727 |
2252819 |
if (!ARITH_PROOF_ON()) |
728 |
|
{ |
729 |
1137499 |
return cr.d_farkasCoefficients == RationalVectorCPSentinel; |
730 |
|
} |
731 |
1115320 |
Assert(ARITH_PROOF_ON()); |
732 |
|
|
733 |
1115320 |
if(cr.d_farkasCoefficients == RationalVectorCPSentinel){ return false; } |
734 |
1115320 |
if(cr.d_farkasCoefficients->size() < 2){ return false; } |
735 |
|
|
736 |
1115320 |
const ArithVariables& vars = d_database->getArithVariables(); |
737 |
|
|
738 |
2230640 |
DeltaRational rhs(0); |
739 |
2230640 |
Node lhs = Polynomial::mkZero().getNode(); |
740 |
|
|
741 |
1115320 |
RationalVector::const_iterator coeffIterator = cr.d_farkasCoefficients->end()-1; |
742 |
1115320 |
RationalVector::const_iterator coeffBegin = cr.d_farkasCoefficients->begin(); |
743 |
|
|
744 |
4097064 |
while(antecedent != NullConstraint){ |
745 |
1490872 |
Assert(lhs.isNull() || Polynomial::isMember(lhs)); |
746 |
|
|
747 |
1490872 |
const Rational& coeff = *coeffIterator; |
748 |
1490872 |
int coeffSgn = coeff.sgn(); |
749 |
|
|
750 |
1490872 |
rhs += antecedent->getValue() * coeff; |
751 |
|
|
752 |
1490872 |
ArithVar antVar = antecedent->getVariable(); |
753 |
1490872 |
if(!lhs.isNull() && vars.hasNode(antVar)){ |
754 |
2981744 |
Node antAsNode = vars.asNode(antVar); |
755 |
1490872 |
if(Polynomial::isMember(antAsNode)){ |
756 |
2981744 |
Polynomial lhsPoly = Polynomial::parsePolynomial(lhs); |
757 |
2981744 |
Polynomial antPoly = Polynomial::parsePolynomial(antAsNode); |
758 |
2981744 |
Polynomial sum = lhsPoly + (antPoly * coeff); |
759 |
1490872 |
lhs = sum.getNode(); |
760 |
|
}else{ |
761 |
|
lhs = Node::null(); |
762 |
|
} |
763 |
|
} else { |
764 |
|
lhs = Node::null(); |
765 |
|
} |
766 |
1490872 |
Debug("constraints::wffp") << "running sum: " << lhs << " <= " << rhs << endl; |
767 |
|
|
768 |
1490872 |
switch( antecedent->getType() ){ |
769 |
405037 |
case LowerBound: |
770 |
|
// fc[l] < 0, therefore return false if coeffSgn >= 0 |
771 |
405037 |
if(coeffSgn >= 0){ return false; } |
772 |
405037 |
break; |
773 |
238441 |
case UpperBound: |
774 |
|
// fc[u] > 0, therefore return false if coeffSgn <= 0 |
775 |
238441 |
if(coeffSgn <= 0){ return false; } |
776 |
238441 |
break; |
777 |
847394 |
case Equality: |
778 |
847394 |
if(coeffSgn == 0) { return false; } |
779 |
847394 |
break; |
780 |
|
case Disequality: |
781 |
|
default: |
782 |
|
return false; |
783 |
|
} |
784 |
|
|
785 |
1490872 |
if(coeffIterator == coeffBegin){ return false; } |
786 |
1490872 |
--coeffIterator; |
787 |
1490872 |
--p; |
788 |
1490872 |
antecedent = d_database->d_antecedents[p]; |
789 |
|
} |
790 |
1115320 |
if(coeffIterator != coeffBegin){ return false; } |
791 |
|
|
792 |
1115320 |
const Rational& firstCoeff = (*coeffBegin); |
793 |
1115320 |
int firstCoeffSgn = firstCoeff.sgn(); |
794 |
1115320 |
rhs += (getNegation()->getValue()) * firstCoeff; |
795 |
1115320 |
if(!lhs.isNull() && vars.hasNode(getVariable())){ |
796 |
2230640 |
Node firstAsNode = vars.asNode(getVariable()); |
797 |
1115320 |
if(Polynomial::isMember(firstAsNode)){ |
798 |
2230640 |
Polynomial lhsPoly = Polynomial::parsePolynomial(lhs); |
799 |
2230640 |
Polynomial firstPoly = Polynomial::parsePolynomial(firstAsNode); |
800 |
2230640 |
Polynomial sum = lhsPoly + (firstPoly * firstCoeff); |
801 |
1115320 |
lhs = sum.getNode(); |
802 |
|
}else{ |
803 |
|
lhs = Node::null(); |
804 |
|
} |
805 |
|
}else{ |
806 |
|
lhs = Node::null(); |
807 |
|
} |
808 |
|
|
809 |
1115320 |
switch( getNegation()->getType() ){ |
810 |
329688 |
case LowerBound: |
811 |
|
// fc[l] < 0, therefore return false if coeffSgn >= 0 |
812 |
329688 |
if(firstCoeffSgn >= 0){ return false; } |
813 |
329688 |
break; |
814 |
317923 |
case UpperBound: |
815 |
|
// fc[u] > 0, therefore return false if coeffSgn <= 0 |
816 |
317923 |
if(firstCoeffSgn <= 0){ return false; } |
817 |
317923 |
break; |
818 |
467709 |
case Equality: |
819 |
467709 |
if(firstCoeffSgn == 0) { return false; } |
820 |
467709 |
break; |
821 |
|
case Disequality: |
822 |
|
default: |
823 |
|
return false; |
824 |
|
} |
825 |
1115320 |
Debug("constraints::wffp") << "final sum: " << lhs << " <= " << rhs << endl; |
826 |
|
// 0 = lhs <= rhs < 0 |
827 |
3345960 |
return (lhs.isNull() || (Constant::isMember(lhs) && Constant(lhs).isZero())) |
828 |
3345960 |
&& rhs.sgn() < 0; |
829 |
|
} |
830 |
|
|
831 |
64077 |
ConstraintP Constraint::makeNegation(ArithVar v, ConstraintType t, const DeltaRational& r){ |
832 |
64077 |
switch(t){ |
833 |
3354 |
case LowerBound: |
834 |
|
{ |
835 |
3354 |
Assert(r.infinitesimalSgn() >= 0); |
836 |
3354 |
if(r.infinitesimalSgn() > 0){ |
837 |
|
Assert(r.getInfinitesimalPart() == 1); |
838 |
|
// make (not (v > r)), which is (v <= r) |
839 |
|
DeltaRational dropInf(r.getNoninfinitesimalPart(), 0); |
840 |
|
return new Constraint(v, UpperBound, dropInf); |
841 |
|
}else{ |
842 |
3354 |
Assert(r.infinitesimalSgn() == 0); |
843 |
|
// make (not (v >= r)), which is (v < r) |
844 |
6708 |
DeltaRational addInf(r.getNoninfinitesimalPart(), -1); |
845 |
3354 |
return new Constraint(v, UpperBound, addInf); |
846 |
|
} |
847 |
|
} |
848 |
56523 |
case UpperBound: |
849 |
|
{ |
850 |
56523 |
Assert(r.infinitesimalSgn() <= 0); |
851 |
56523 |
if(r.infinitesimalSgn() < 0){ |
852 |
|
Assert(r.getInfinitesimalPart() == -1); |
853 |
|
// make (not (v < r)), which is (v >= r) |
854 |
|
DeltaRational dropInf(r.getNoninfinitesimalPart(), 0); |
855 |
|
return new Constraint(v, LowerBound, dropInf); |
856 |
|
}else{ |
857 |
56523 |
Assert(r.infinitesimalSgn() == 0); |
858 |
|
// make (not (v <= r)), which is (v > r) |
859 |
113046 |
DeltaRational addInf(r.getNoninfinitesimalPart(), 1); |
860 |
56523 |
return new Constraint(v, LowerBound, addInf); |
861 |
|
} |
862 |
|
} |
863 |
4200 |
case Equality: |
864 |
4200 |
return new Constraint(v, Disequality, r); |
865 |
|
case Disequality: |
866 |
|
return new Constraint(v, Equality, r); |
867 |
|
default: |
868 |
|
Unreachable(); |
869 |
|
return NullConstraint; |
870 |
|
} |
871 |
|
} |
872 |
|
|
873 |
9926 |
ConstraintDatabase::ConstraintDatabase(context::Context* satContext, |
874 |
|
context::Context* userContext, |
875 |
|
const ArithVariables& avars, |
876 |
|
ArithCongruenceManager& cm, |
877 |
|
RaiseConflict raiseConflict, |
878 |
|
EagerProofGenerator* pfGen, |
879 |
9926 |
ProofNodeManager* pnm) |
880 |
|
: d_varDatabases(), |
881 |
|
d_toPropagate(satContext), |
882 |
|
d_antecedents(satContext, false), |
883 |
9926 |
d_watches(new Watches(satContext, userContext)), |
884 |
|
d_avariables(avars), |
885 |
|
d_congruenceManager(cm), |
886 |
|
d_satContext(satContext), |
887 |
|
d_pfGen(pfGen), |
888 |
|
d_pnm(pnm), |
889 |
|
d_raiseConflict(raiseConflict), |
890 |
|
d_one(1), |
891 |
19852 |
d_negOne(-1) |
892 |
|
{ |
893 |
9926 |
} |
894 |
|
|
895 |
10871838 |
SortedConstraintMap& ConstraintDatabase::getVariableSCM(ArithVar v) const{ |
896 |
10871838 |
Assert(variableDatabaseIsSetup(v)); |
897 |
10871838 |
return d_varDatabases[v]->d_constraints; |
898 |
|
} |
899 |
|
|
900 |
31674 |
void ConstraintDatabase::pushSplitWatch(ConstraintP c){ |
901 |
31674 |
Assert(!c->d_split); |
902 |
31674 |
c->d_split = true; |
903 |
31674 |
d_watches->d_splitWatches.push_back(c); |
904 |
31674 |
} |
905 |
|
|
906 |
|
|
907 |
846914 |
void ConstraintDatabase::pushCanBePropagatedWatch(ConstraintP c){ |
908 |
846914 |
Assert(!c->d_canBePropagated); |
909 |
846914 |
c->d_canBePropagated = true; |
910 |
846914 |
d_watches->d_canBePropagatedWatches.push_back(c); |
911 |
846914 |
} |
912 |
|
|
913 |
5595444 |
void ConstraintDatabase::pushAssertionOrderWatch(ConstraintP c, TNode witness){ |
914 |
5595444 |
Assert(!c->assertedToTheTheory()); |
915 |
5595444 |
c->d_assertionOrder = d_watches->d_assertionOrderWatches.size(); |
916 |
5595444 |
c->d_witness = witness; |
917 |
5595444 |
d_watches->d_assertionOrderWatches.push_back(c); |
918 |
5595444 |
} |
919 |
|
|
920 |
|
|
921 |
8504318 |
void ConstraintDatabase::pushConstraintRule(const ConstraintRule& crp){ |
922 |
8504318 |
ConstraintP c = crp.d_constraint; |
923 |
8504318 |
Assert(c->d_crid == ConstraintRuleIdSentinel); |
924 |
8504318 |
Assert(!c->hasProof()); |
925 |
8504318 |
c->d_crid = d_watches->d_constraintProofs.size(); |
926 |
8504318 |
d_watches->d_constraintProofs.push_back(crp); |
927 |
8504318 |
} |
928 |
|
|
929 |
1529124 |
ConstraintP ConstraintDatabase::getConstraint(ArithVar v, ConstraintType t, const DeltaRational& r){ |
930 |
|
//This must always return a constraint. |
931 |
|
|
932 |
1529124 |
SortedConstraintMap& scm = getVariableSCM(v); |
933 |
1529124 |
pair<SortedConstraintMapIterator, bool> insertAttempt; |
934 |
1529124 |
insertAttempt = scm.insert(make_pair(r, ValueCollection())); |
935 |
|
|
936 |
1529124 |
SortedConstraintMapIterator pos = insertAttempt.first; |
937 |
1529124 |
ValueCollection& vc = pos->second; |
938 |
1529124 |
if(vc.hasConstraintOfType(t)){ |
939 |
1465047 |
return vc.getConstraintOfType(t); |
940 |
|
}else{ |
941 |
64077 |
ConstraintP c = new Constraint(v, t, r); |
942 |
64077 |
ConstraintP negC = Constraint::makeNegation(v, t, r); |
943 |
|
|
944 |
64077 |
SortedConstraintMapIterator negPos; |
945 |
64077 |
if(t == Equality || t == Disequality){ |
946 |
4200 |
negPos = pos; |
947 |
|
}else{ |
948 |
59877 |
pair<SortedConstraintMapIterator, bool> negInsertAttempt; |
949 |
59877 |
negInsertAttempt = scm.insert(make_pair(negC->getValue(), ValueCollection())); |
950 |
59877 |
Assert(negInsertAttempt.second |
951 |
|
|| !negInsertAttempt.first->second.hasConstraintOfType( |
952 |
|
negC->getType())); |
953 |
59877 |
negPos = negInsertAttempt.first; |
954 |
|
} |
955 |
|
|
956 |
64077 |
c->initialize(this, pos, negC); |
957 |
64077 |
negC->initialize(this, negPos, c); |
958 |
|
|
959 |
64077 |
vc.add(c); |
960 |
64077 |
negPos->second.add(negC); |
961 |
|
|
962 |
64077 |
return c; |
963 |
|
} |
964 |
|
} |
965 |
|
|
966 |
267522 |
ConstraintP ConstraintDatabase::ensureConstraint(ValueCollection& vc, ConstraintType t){ |
967 |
267522 |
if(vc.hasConstraintOfType(t)){ |
968 |
260872 |
return vc.getConstraintOfType(t); |
969 |
|
}else{ |
970 |
6650 |
return getConstraint(vc.getVariable(), t, vc.getValue()); |
971 |
|
} |
972 |
|
} |
973 |
|
|
974 |
|
bool ConstraintDatabase::emptyDatabase(const std::vector<PerVariableDatabase>& vec){ |
975 |
|
std::vector<PerVariableDatabase>::const_iterator first = vec.begin(); |
976 |
|
std::vector<PerVariableDatabase>::const_iterator last = vec.end(); |
977 |
|
return std::find_if(first, last, PerVariableDatabase::IsEmpty) == last; |
978 |
|
} |
979 |
|
|
980 |
19846 |
ConstraintDatabase::~ConstraintDatabase(){ |
981 |
9923 |
delete d_watches; |
982 |
|
|
983 |
19846 |
std::vector<ConstraintP> constraintList; |
984 |
|
|
985 |
340111 |
while(!d_varDatabases.empty()){ |
986 |
165094 |
PerVariableDatabase* back = d_varDatabases.back(); |
987 |
|
|
988 |
165094 |
SortedConstraintMap& scm = back->d_constraints; |
989 |
165094 |
SortedConstraintMapIterator i = scm.begin(), i_end = scm.end(); |
990 |
1061116 |
for(; i != i_end; ++i){ |
991 |
448011 |
(i->second).push_into(constraintList); |
992 |
|
} |
993 |
1500778 |
while(!constraintList.empty()){ |
994 |
667842 |
ConstraintP c = constraintList.back(); |
995 |
667842 |
constraintList.pop_back(); |
996 |
667842 |
delete c; |
997 |
|
} |
998 |
165094 |
Assert(scm.empty()); |
999 |
165094 |
d_varDatabases.pop_back(); |
1000 |
165094 |
delete back; |
1001 |
|
} |
1002 |
|
|
1003 |
9923 |
Assert(d_nodetoConstraintMap.empty()); |
1004 |
9923 |
} |
1005 |
|
|
1006 |
9926 |
ConstraintDatabase::Statistics::Statistics() |
1007 |
9926 |
: d_unatePropagateCalls(smtStatisticsRegistry().registerInt( |
1008 |
19852 |
"theory::arith::cd::unatePropagateCalls")), |
1009 |
9926 |
d_unatePropagateImplications(smtStatisticsRegistry().registerInt( |
1010 |
19852 |
"theory::arith::cd::unatePropagateImplications")) |
1011 |
|
{ |
1012 |
9926 |
} |
1013 |
|
|
1014 |
|
void ConstraintDatabase::deleteConstraintAndNegation(ConstraintP c){ |
1015 |
|
Assert(c->safeToGarbageCollect()); |
1016 |
|
ConstraintP neg = c->getNegation(); |
1017 |
|
Assert(neg->safeToGarbageCollect()); |
1018 |
|
delete c; |
1019 |
|
delete neg; |
1020 |
|
} |
1021 |
|
|
1022 |
165094 |
void ConstraintDatabase::addVariable(ArithVar v){ |
1023 |
165094 |
if(d_reclaimable.isMember(v)){ |
1024 |
|
SortedConstraintMap& scm = getVariableSCM(v); |
1025 |
|
|
1026 |
|
std::vector<ConstraintP> constraintList; |
1027 |
|
|
1028 |
|
for(SortedConstraintMapIterator i = scm.begin(), end = scm.end(); i != end; ++i){ |
1029 |
|
(i->second).push_into(constraintList); |
1030 |
|
} |
1031 |
|
while(!constraintList.empty()){ |
1032 |
|
ConstraintP c = constraintList.back(); |
1033 |
|
constraintList.pop_back(); |
1034 |
|
Assert(c->safeToGarbageCollect()); |
1035 |
|
delete c; |
1036 |
|
} |
1037 |
|
Assert(scm.empty()); |
1038 |
|
|
1039 |
|
d_reclaimable.remove(v); |
1040 |
|
}else{ |
1041 |
165094 |
Debug("arith::constraint") << "about to fail" << v << " " << d_varDatabases.size() << endl; |
1042 |
165094 |
Assert(v == d_varDatabases.size()); |
1043 |
165094 |
d_varDatabases.push_back(new PerVariableDatabase(v)); |
1044 |
|
} |
1045 |
165094 |
} |
1046 |
|
|
1047 |
|
void ConstraintDatabase::removeVariable(ArithVar v){ |
1048 |
|
Assert(!d_reclaimable.isMember(v)); |
1049 |
|
d_reclaimable.add(v); |
1050 |
|
} |
1051 |
|
|
1052 |
|
bool Constraint::safeToGarbageCollect() const{ |
1053 |
|
// Do not call during destructor as getNegation() may be Null by this point |
1054 |
|
Assert(getNegation() != NullConstraint); |
1055 |
|
return !contextDependentDataIsSet() && ! getNegation()->contextDependentDataIsSet(); |
1056 |
|
} |
1057 |
|
|
1058 |
669334 |
bool Constraint::contextDependentDataIsSet() const{ |
1059 |
669334 |
return hasProof() || isSplit() || canBePropagated() || assertedToTheTheory(); |
1060 |
|
} |
1061 |
|
|
1062 |
15837 |
TrustNode Constraint::split() |
1063 |
|
{ |
1064 |
15837 |
Assert(isEquality() || isDisequality()); |
1065 |
|
|
1066 |
15837 |
bool isEq = isEquality(); |
1067 |
|
|
1068 |
15837 |
ConstraintP eq = isEq ? this : d_negation; |
1069 |
15837 |
ConstraintP diseq = isEq ? d_negation : this; |
1070 |
|
|
1071 |
31674 |
TNode eqNode = eq->getLiteral(); |
1072 |
15837 |
Assert(eqNode.getKind() == kind::EQUAL); |
1073 |
31674 |
TNode lhs = eqNode[0]; |
1074 |
31674 |
TNode rhs = eqNode[1]; |
1075 |
|
|
1076 |
31674 |
Node leqNode = NodeBuilder(kind::LEQ) << lhs << rhs; |
1077 |
31674 |
Node ltNode = NodeBuilder(kind::LT) << lhs << rhs; |
1078 |
31674 |
Node gtNode = NodeBuilder(kind::GT) << lhs << rhs; |
1079 |
31674 |
Node geqNode = NodeBuilder(kind::GEQ) << lhs << rhs; |
1080 |
|
|
1081 |
31674 |
Node lemma = NodeBuilder(OR) << leqNode << geqNode; |
1082 |
|
|
1083 |
15837 |
TrustNode trustedLemma; |
1084 |
15837 |
if (d_database->isProofEnabled()) |
1085 |
|
{ |
1086 |
|
// Farkas proof that this works. |
1087 |
2445 |
auto nm = NodeManager::currentNM(); |
1088 |
4890 |
auto nLeqPf = d_database->d_pnm->mkAssume(leqNode.negate()); |
1089 |
2445 |
auto gtPf = d_database->d_pnm->mkNode( |
1090 |
4890 |
PfRule::MACRO_SR_PRED_TRANSFORM, {nLeqPf}, {gtNode}); |
1091 |
4890 |
auto nGeqPf = d_database->d_pnm->mkAssume(geqNode.negate()); |
1092 |
2445 |
auto ltPf = d_database->d_pnm->mkNode( |
1093 |
4890 |
PfRule::MACRO_SR_PRED_TRANSFORM, {nGeqPf}, {ltNode}); |
1094 |
2445 |
auto sumPf = d_database->d_pnm->mkNode( |
1095 |
|
PfRule::MACRO_ARITH_SCALE_SUM_UB, |
1096 |
|
{gtPf, ltPf}, |
1097 |
4890 |
{nm->mkConst<Rational>(-1), nm->mkConst<Rational>(1)}); |
1098 |
2445 |
auto botPf = d_database->d_pnm->mkNode( |
1099 |
4890 |
PfRule::MACRO_SR_PRED_TRANSFORM, {sumPf}, {nm->mkConst(false)}); |
1100 |
4890 |
std::vector<Node> a = {leqNode.negate(), geqNode.negate()}; |
1101 |
4890 |
auto notAndNotPf = d_database->d_pnm->mkScope(botPf, a); |
1102 |
|
// No need to ensure that the expected node aggrees with `a` because we are |
1103 |
|
// not providing an expected node. |
1104 |
|
auto orNotNotPf = |
1105 |
4890 |
d_database->d_pnm->mkNode(PfRule::NOT_AND, {notAndNotPf}, {}); |
1106 |
2445 |
auto orPf = d_database->d_pnm->mkNode( |
1107 |
4890 |
PfRule::MACRO_SR_PRED_TRANSFORM, {orNotNotPf}, {lemma}); |
1108 |
2445 |
trustedLemma = d_database->d_pfGen->mkTrustNode(lemma, orPf); |
1109 |
|
} |
1110 |
|
else |
1111 |
|
{ |
1112 |
13392 |
trustedLemma = TrustNode::mkTrustLemma(lemma); |
1113 |
|
} |
1114 |
|
|
1115 |
15837 |
eq->d_database->pushSplitWatch(eq); |
1116 |
15837 |
diseq->d_database->pushSplitWatch(diseq); |
1117 |
|
|
1118 |
31674 |
return trustedLemma; |
1119 |
|
} |
1120 |
|
|
1121 |
1085345 |
bool ConstraintDatabase::hasLiteral(TNode literal) const { |
1122 |
1085345 |
return lookup(literal) != NullConstraint; |
1123 |
|
} |
1124 |
|
|
1125 |
271336 |
ConstraintP ConstraintDatabase::addLiteral(TNode literal){ |
1126 |
271336 |
Assert(!hasLiteral(literal)); |
1127 |
271336 |
bool isNot = (literal.getKind() == NOT); |
1128 |
542672 |
Node atomNode = (isNot ? literal[0] : literal); |
1129 |
542672 |
Node negationNode = atomNode.notNode(); |
1130 |
|
|
1131 |
271336 |
Assert(!hasLiteral(atomNode)); |
1132 |
271336 |
Assert(!hasLiteral(negationNode)); |
1133 |
542672 |
Comparison posCmp = Comparison::parseNormalForm(atomNode); |
1134 |
|
|
1135 |
271336 |
ConstraintType posType = Constraint::constraintTypeOfComparison(posCmp); |
1136 |
|
|
1137 |
542672 |
Polynomial nvp = posCmp.normalizedVariablePart(); |
1138 |
271336 |
ArithVar v = d_avariables.asArithVar(nvp.getNode()); |
1139 |
|
|
1140 |
542672 |
DeltaRational posDR = posCmp.normalizedDeltaRational(); |
1141 |
|
|
1142 |
271336 |
ConstraintP posC = new Constraint(v, posType, posDR); |
1143 |
|
|
1144 |
271336 |
Debug("arith::constraint") << "addliteral( literal ->" << literal << ")" << endl; |
1145 |
271336 |
Debug("arith::constraint") << "addliteral( posC ->" << posC << ")" << endl; |
1146 |
|
|
1147 |
271336 |
SortedConstraintMap& scm = getVariableSCM(posC->getVariable()); |
1148 |
271336 |
pair<SortedConstraintMapIterator, bool> insertAttempt; |
1149 |
271336 |
insertAttempt = scm.insert(make_pair(posC->getValue(), ValueCollection())); |
1150 |
|
|
1151 |
271336 |
SortedConstraintMapIterator posI = insertAttempt.first; |
1152 |
|
// If the attempt succeeds, i points to a new empty ValueCollection |
1153 |
|
// If the attempt fails, i points to a pre-existing ValueCollection |
1154 |
|
|
1155 |
271336 |
if(posI->second.hasConstraintOfType(posC->getType())){ |
1156 |
|
//This is the situation where the ConstraintP exists, but |
1157 |
|
//the literal has not been associated with it. |
1158 |
1492 |
ConstraintP hit = posI->second.getConstraintOfType(posC->getType()); |
1159 |
1492 |
Debug("arith::constraint") << "hit " << hit << endl; |
1160 |
1492 |
Debug("arith::constraint") << "posC " << posC << endl; |
1161 |
|
|
1162 |
1492 |
delete posC; |
1163 |
|
|
1164 |
1492 |
hit->setLiteral(atomNode); |
1165 |
1492 |
hit->getNegation()->setLiteral(negationNode); |
1166 |
1492 |
return isNot ? hit->getNegation(): hit; |
1167 |
|
}else{ |
1168 |
539688 |
Comparison negCmp = Comparison::parseNormalForm(negationNode); |
1169 |
|
|
1170 |
269844 |
ConstraintType negType = Constraint::constraintTypeOfComparison(negCmp); |
1171 |
539688 |
DeltaRational negDR = negCmp.normalizedDeltaRational(); |
1172 |
|
|
1173 |
269844 |
ConstraintP negC = new Constraint(v, negType, negDR); |
1174 |
|
|
1175 |
269844 |
SortedConstraintMapIterator negI; |
1176 |
|
|
1177 |
269844 |
if(posC->isEquality()){ |
1178 |
127897 |
negI = posI; |
1179 |
|
}else{ |
1180 |
141947 |
Assert(posC->isLowerBound() || posC->isUpperBound()); |
1181 |
|
|
1182 |
141947 |
pair<SortedConstraintMapIterator, bool> negInsertAttempt; |
1183 |
141947 |
negInsertAttempt = scm.insert(make_pair(negC->getValue(), ValueCollection())); |
1184 |
|
|
1185 |
141947 |
Debug("nf::tmp") << "sdhjfgdhjkldfgljkhdfg" << endl; |
1186 |
141947 |
Debug("nf::tmp") << negC << endl; |
1187 |
141947 |
Debug("nf::tmp") << negC->getValue() << endl; |
1188 |
|
|
1189 |
|
//This should always succeed as the DeltaRational for the negation is unique! |
1190 |
141947 |
Assert(negInsertAttempt.second); |
1191 |
|
|
1192 |
141947 |
negI = negInsertAttempt.first; |
1193 |
|
} |
1194 |
|
|
1195 |
269844 |
(posI->second).add(posC); |
1196 |
269844 |
(negI->second).add(negC); |
1197 |
|
|
1198 |
269844 |
posC->initialize(this, posI, negC); |
1199 |
269844 |
negC->initialize(this, negI, posC); |
1200 |
|
|
1201 |
269844 |
posC->setLiteral(atomNode); |
1202 |
269844 |
negC->setLiteral(negationNode); |
1203 |
|
|
1204 |
269844 |
return isNot ? negC : posC; |
1205 |
|
} |
1206 |
|
} |
1207 |
|
|
1208 |
|
|
1209 |
9926315 |
ConstraintP ConstraintDatabase::lookup(TNode literal) const{ |
1210 |
9926315 |
NodetoConstraintMap::const_iterator iter = d_nodetoConstraintMap.find(literal); |
1211 |
9926315 |
if(iter == d_nodetoConstraintMap.end()){ |
1212 |
1948574 |
return NullConstraint; |
1213 |
|
}else{ |
1214 |
7977741 |
return iter->second; |
1215 |
|
} |
1216 |
|
} |
1217 |
|
|
1218 |
4595345 |
void Constraint::setAssumption(bool nowInConflict){ |
1219 |
4595345 |
Debug("constraints::pf") << "setAssumption(" << this << ")" << std::endl; |
1220 |
4595345 |
Assert(!hasProof()); |
1221 |
4595345 |
Assert(negationHasProof() == nowInConflict); |
1222 |
4595345 |
Assert(hasLiteral()); |
1223 |
4595345 |
Assert(assertedToTheTheory()); |
1224 |
|
|
1225 |
4595345 |
d_database->pushConstraintRule(ConstraintRule(this, AssumeAP)); |
1226 |
|
|
1227 |
4595345 |
Assert(inConflict() == nowInConflict); |
1228 |
4595345 |
if(Debug.isOn("constraint::conflictCommit") && inConflict()){ |
1229 |
|
Debug("constraint::conflictCommit") << "inConflict@setAssumption " << this << std::endl; |
1230 |
|
} |
1231 |
4595345 |
} |
1232 |
|
|
1233 |
3619430 |
void Constraint::tryToPropagate(){ |
1234 |
3619430 |
Assert(hasProof()); |
1235 |
3619430 |
Assert(!isAssumption()); |
1236 |
3619430 |
Assert(!isInternalAssumption()); |
1237 |
|
|
1238 |
3619430 |
if(canBePropagated() && !assertedToTheTheory() && !isAssumption() && !isInternalAssumption()){ |
1239 |
790672 |
propagate(); |
1240 |
|
} |
1241 |
3619430 |
} |
1242 |
|
|
1243 |
807828 |
void Constraint::propagate(){ |
1244 |
807828 |
Assert(hasProof()); |
1245 |
807828 |
Assert(canBePropagated()); |
1246 |
807828 |
Assert(!assertedToTheTheory()); |
1247 |
807828 |
Assert(!isAssumption()); |
1248 |
807828 |
Assert(!isInternalAssumption()); |
1249 |
|
|
1250 |
807828 |
d_database->d_toPropagate.push(this); |
1251 |
807828 |
} |
1252 |
|
|
1253 |
|
|
1254 |
|
/* |
1255 |
|
* Example: |
1256 |
|
* x <= a and a < b |
1257 |
|
* |= x <= b |
1258 |
|
* --- |
1259 |
|
* 1*(x <= a) + (-1)*(x > b) => (0 <= a-b) |
1260 |
|
*/ |
1261 |
2149976 |
void Constraint::impliedByUnate(ConstraintCP imp, bool nowInConflict){ |
1262 |
2149976 |
Debug("constraints::pf") << "impliedByUnate(" << this << ", " << *imp << ")" << std::endl; |
1263 |
2149976 |
Assert(!hasProof()); |
1264 |
2149976 |
Assert(imp->hasProof()); |
1265 |
2149976 |
Assert(negationHasProof() == nowInConflict); |
1266 |
|
|
1267 |
2149976 |
d_database->d_antecedents.push_back(NullConstraint); |
1268 |
2149976 |
d_database->d_antecedents.push_back(imp); |
1269 |
|
|
1270 |
2149976 |
AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1; |
1271 |
|
|
1272 |
|
RationalVectorP coeffs; |
1273 |
2149976 |
if (ARITH_PROOF_ON()) |
1274 |
|
{ |
1275 |
1066647 |
std::pair<int, int> sgns = unateFarkasSigns(getNegation(), imp); |
1276 |
|
|
1277 |
2133294 |
Rational first(sgns.first); |
1278 |
2133294 |
Rational second(sgns.second); |
1279 |
|
|
1280 |
1066647 |
coeffs = new RationalVector(); |
1281 |
1066647 |
coeffs->push_back(first); |
1282 |
1066647 |
coeffs->push_back(second); |
1283 |
|
} |
1284 |
|
else |
1285 |
|
{ |
1286 |
1083329 |
coeffs = RationalVectorPSentinel; |
1287 |
|
} |
1288 |
|
// no need to delete coeffs the memory is owned by ConstraintRule |
1289 |
2149976 |
d_database->pushConstraintRule(ConstraintRule(this, FarkasAP, antecedentEnd, coeffs)); |
1290 |
|
|
1291 |
2149976 |
Assert(inConflict() == nowInConflict); |
1292 |
2149976 |
if(Debug.isOn("constraint::conflictCommit") && inConflict()){ |
1293 |
|
Debug("constraint::conflictCommit") << "inConflict@impliedByUnate " << this << std::endl; |
1294 |
|
} |
1295 |
|
|
1296 |
2149976 |
if(Debug.isOn("constraints::wffp") && !wellFormedFarkasProof()){ |
1297 |
|
getConstraintRule().print(Debug("constraints::wffp")); |
1298 |
|
} |
1299 |
2149976 |
Assert(wellFormedFarkasProof()); |
1300 |
2149976 |
} |
1301 |
|
|
1302 |
409791 |
void Constraint::impliedByTrichotomy(ConstraintCP a, ConstraintCP b, bool nowInConflict){ |
1303 |
409791 |
Debug("constraints::pf") << "impliedByTrichotomy(" << this << ", " << *a << ", "; |
1304 |
409791 |
Debug("constraints::pf") << *b << ")" << std::endl; |
1305 |
409791 |
Assert(!hasProof()); |
1306 |
409791 |
Assert(negationHasProof() == nowInConflict); |
1307 |
409791 |
Assert(a->hasProof()); |
1308 |
409791 |
Assert(b->hasProof()); |
1309 |
|
|
1310 |
409791 |
d_database->d_antecedents.push_back(NullConstraint); |
1311 |
409791 |
d_database->d_antecedents.push_back(a); |
1312 |
409791 |
d_database->d_antecedents.push_back(b); |
1313 |
|
|
1314 |
409791 |
AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1; |
1315 |
409791 |
d_database->pushConstraintRule(ConstraintRule(this, TrichotomyAP, antecedentEnd)); |
1316 |
|
|
1317 |
409791 |
Assert(inConflict() == nowInConflict); |
1318 |
409791 |
if(Debug.isOn("constraint::conflictCommit") && inConflict()){ |
1319 |
|
Debug("constraint::conflictCommit") << "inConflict@impliedByTrichotomy " << this << std::endl; |
1320 |
|
} |
1321 |
409791 |
} |
1322 |
|
|
1323 |
|
|
1324 |
102843 |
bool Constraint::allHaveProof(const ConstraintCPVec& b){ |
1325 |
1468304 |
for(ConstraintCPVec::const_iterator i=b.begin(), i_end=b.end(); i != i_end; ++i){ |
1326 |
1365461 |
ConstraintCP cp = *i; |
1327 |
1365461 |
if(! (cp->hasProof())){ return false; } |
1328 |
|
} |
1329 |
102843 |
return true; |
1330 |
|
} |
1331 |
|
|
1332 |
1030910 |
void Constraint::impliedByIntTighten(ConstraintCP a, bool nowInConflict){ |
1333 |
1030910 |
Debug("constraints::pf") << "impliedByIntTighten(" << this << ", " << *a << ")" << std::endl; |
1334 |
1030910 |
Assert(!hasProof()); |
1335 |
1030910 |
Assert(negationHasProof() == nowInConflict); |
1336 |
1030910 |
Assert(a->hasProof()); |
1337 |
2061820 |
Debug("pf::arith") << "impliedByIntTighten(" << this << ", " << a << ")" |
1338 |
1030910 |
<< std::endl; |
1339 |
|
|
1340 |
1030910 |
d_database->d_antecedents.push_back(NullConstraint); |
1341 |
1030910 |
d_database->d_antecedents.push_back(a); |
1342 |
1030910 |
AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1; |
1343 |
1030910 |
d_database->pushConstraintRule(ConstraintRule(this, IntTightenAP, antecedentEnd)); |
1344 |
|
|
1345 |
1030910 |
Assert(inConflict() == nowInConflict); |
1346 |
1030910 |
if(inConflict()){ |
1347 |
2829 |
Debug("constraint::conflictCommit") << "inConflict impliedByIntTighten" << this << std::endl; |
1348 |
|
} |
1349 |
1030910 |
} |
1350 |
|
|
1351 |
|
void Constraint::impliedByIntHole(ConstraintCP a, bool nowInConflict){ |
1352 |
|
Debug("constraints::pf") << "impliedByIntHole(" << this << ", " << *a << ")" << std::endl; |
1353 |
|
Assert(!hasProof()); |
1354 |
|
Assert(negationHasProof() == nowInConflict); |
1355 |
|
Assert(a->hasProof()); |
1356 |
|
Debug("pf::arith") << "impliedByIntHole(" << this << ", " << a << ")" |
1357 |
|
<< std::endl; |
1358 |
|
|
1359 |
|
d_database->d_antecedents.push_back(NullConstraint); |
1360 |
|
d_database->d_antecedents.push_back(a); |
1361 |
|
AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1; |
1362 |
|
d_database->pushConstraintRule(ConstraintRule(this, IntHoleAP, antecedentEnd)); |
1363 |
|
|
1364 |
|
Assert(inConflict() == nowInConflict); |
1365 |
|
if(Debug.isOn("constraint::conflictCommit") && inConflict()){ |
1366 |
|
Debug("constraint::conflictCommit") << "inConflict impliedByIntHole" << this << std::endl; |
1367 |
|
} |
1368 |
|
} |
1369 |
|
|
1370 |
|
void Constraint::impliedByIntHole(const ConstraintCPVec& b, bool nowInConflict){ |
1371 |
|
Debug("constraints::pf") << "impliedByIntHole(" << this; |
1372 |
|
if (Debug.isOn("constraints::pf")) { |
1373 |
|
for (const ConstraintCP& p : b) |
1374 |
|
{ |
1375 |
|
Debug("constraints::pf") << ", " << p; |
1376 |
|
} |
1377 |
|
} |
1378 |
|
Debug("constraints::pf") << ")" << std::endl; |
1379 |
|
|
1380 |
|
Assert(!hasProof()); |
1381 |
|
Assert(negationHasProof() == nowInConflict); |
1382 |
|
Assert(allHaveProof(b)); |
1383 |
|
|
1384 |
|
CDConstraintList& antecedents = d_database->d_antecedents; |
1385 |
|
antecedents.push_back(NullConstraint); |
1386 |
|
for(ConstraintCPVec::const_iterator i=b.begin(), i_end=b.end(); i != i_end; ++i){ |
1387 |
|
antecedents.push_back(*i); |
1388 |
|
} |
1389 |
|
AntecedentId antecedentEnd = antecedents.size() - 1; |
1390 |
|
|
1391 |
|
d_database->pushConstraintRule(ConstraintRule(this, IntHoleAP, antecedentEnd)); |
1392 |
|
|
1393 |
|
Assert(inConflict() == nowInConflict); |
1394 |
|
if(Debug.isOn("constraint::conflictCommit") && inConflict()){ |
1395 |
|
Debug("constraint::conflictCommit") << "inConflict@impliedByIntHole[vec] " << this << std::endl; |
1396 |
|
} |
1397 |
|
} |
1398 |
|
|
1399 |
|
/* |
1400 |
|
* If proofs are off, coeffs == RationalVectorSentinal. |
1401 |
|
* If proofs are on, |
1402 |
|
* coeffs != RationalVectorSentinal, |
1403 |
|
* coeffs->size() = a.size() + 1, |
1404 |
|
* for i in [0,a.size) : coeff[i] corresponds to a[i], and |
1405 |
|
* coeff.back() corresponds to the current constraint. |
1406 |
|
*/ |
1407 |
102843 |
void Constraint::impliedByFarkas(const ConstraintCPVec& a, RationalVectorCP coeffs, bool nowInConflict){ |
1408 |
102843 |
Debug("constraints::pf") << "impliedByFarkas(" << this; |
1409 |
102843 |
if (Debug.isOn("constraints::pf")) { |
1410 |
|
for (const ConstraintCP& p : a) |
1411 |
|
{ |
1412 |
|
Debug("constraints::pf") << ", " << p; |
1413 |
|
} |
1414 |
|
} |
1415 |
102843 |
Debug("constraints::pf") << ", <coeffs>"; |
1416 |
102843 |
Debug("constraints::pf") << ")" << std::endl; |
1417 |
102843 |
Assert(!hasProof()); |
1418 |
102843 |
Assert(negationHasProof() == nowInConflict); |
1419 |
102843 |
Assert(allHaveProof(a)); |
1420 |
|
|
1421 |
102843 |
Assert(ARITH_PROOF_ON() == (coeffs != RationalVectorCPSentinel)); |
1422 |
102843 |
Assert(!ARITH_PROOF_ON() || coeffs->size() == a.size() + 1); |
1423 |
|
|
1424 |
102843 |
Assert(a.size() >= 1); |
1425 |
|
|
1426 |
102843 |
d_database->d_antecedents.push_back(NullConstraint); |
1427 |
1468304 |
for(ConstraintCPVec::const_iterator i = a.begin(), end = a.end(); i != end; ++i){ |
1428 |
1365461 |
ConstraintCP c_i = *i; |
1429 |
1365461 |
Assert(c_i->hasProof()); |
1430 |
1365461 |
d_database->d_antecedents.push_back(c_i); |
1431 |
|
} |
1432 |
102843 |
AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1; |
1433 |
|
|
1434 |
|
RationalVectorCP coeffsCopy; |
1435 |
102843 |
if (ARITH_PROOF_ON()) |
1436 |
|
{ |
1437 |
48673 |
Assert(coeffs != RationalVectorCPSentinel); |
1438 |
48673 |
coeffsCopy = new RationalVector(*coeffs); |
1439 |
|
} |
1440 |
|
else |
1441 |
|
{ |
1442 |
54170 |
coeffsCopy = RationalVectorCPSentinel; |
1443 |
|
} |
1444 |
102843 |
d_database->pushConstraintRule(ConstraintRule(this, FarkasAP, antecedentEnd, coeffsCopy)); |
1445 |
|
|
1446 |
102843 |
Assert(inConflict() == nowInConflict); |
1447 |
102843 |
if(Debug.isOn("constraint::conflictCommit") && inConflict()){ |
1448 |
|
Debug("constraint::conflictCommit") << "inConflict@impliedByFarkas " << this << std::endl; |
1449 |
|
} |
1450 |
102843 |
if(Debug.isOn("constraints::wffp") && !wellFormedFarkasProof()){ |
1451 |
|
getConstraintRule().print(Debug("constraints::wffp")); |
1452 |
|
} |
1453 |
102843 |
Assert(wellFormedFarkasProof()); |
1454 |
102843 |
} |
1455 |
|
|
1456 |
|
|
1457 |
|
void Constraint::setInternalAssumption(bool nowInConflict){ |
1458 |
|
Debug("constraints::pf") << "setInternalAssumption(" << this; |
1459 |
|
Debug("constraints::pf") << ")" << std::endl; |
1460 |
|
Assert(!hasProof()); |
1461 |
|
Assert(negationHasProof() == nowInConflict); |
1462 |
|
Assert(!assertedToTheTheory()); |
1463 |
|
|
1464 |
|
d_database->pushConstraintRule(ConstraintRule(this, InternalAssumeAP)); |
1465 |
|
|
1466 |
|
Assert(inConflict() == nowInConflict); |
1467 |
|
if(Debug.isOn("constraint::conflictCommit") && inConflict()){ |
1468 |
|
Debug("constraint::conflictCommit") << "inConflict@setInternalAssumption " << this << std::endl; |
1469 |
|
} |
1470 |
|
} |
1471 |
|
|
1472 |
|
|
1473 |
215453 |
void Constraint::setEqualityEngineProof(){ |
1474 |
215453 |
Debug("constraints::pf") << "setEqualityEngineProof(" << this; |
1475 |
215453 |
Debug("constraints::pf") << ")" << std::endl; |
1476 |
215453 |
Assert(truthIsUnknown()); |
1477 |
215453 |
Assert(hasLiteral()); |
1478 |
215453 |
d_database->pushConstraintRule(ConstraintRule(this, EqualityEngineAP)); |
1479 |
215453 |
} |
1480 |
|
|
1481 |
|
|
1482 |
4050516 |
SortedConstraintMap& Constraint::constraintSet() const{ |
1483 |
4050516 |
Assert(d_database->variableDatabaseIsSetup(d_variable)); |
1484 |
4050516 |
return (d_database->d_varDatabases[d_variable])->d_constraints; |
1485 |
|
} |
1486 |
|
|
1487 |
|
bool Constraint::antecentListIsEmpty() const{ |
1488 |
|
Assert(hasProof()); |
1489 |
|
return d_database->d_antecedents[getEndAntecedent()] == NullConstraint; |
1490 |
|
} |
1491 |
|
|
1492 |
|
bool Constraint::antecedentListLengthIsOne() const { |
1493 |
|
Assert(hasProof()); |
1494 |
|
return !antecentListIsEmpty() && |
1495 |
|
d_database->d_antecedents[getEndAntecedent()-1] == NullConstraint; |
1496 |
|
} |
1497 |
|
|
1498 |
100695 |
Node Constraint::externalImplication(const ConstraintCPVec& b) const{ |
1499 |
100695 |
Assert(hasLiteral()); |
1500 |
201390 |
Node antecedent = externalExplainByAssertions(b); |
1501 |
201390 |
Node implied = getLiteral(); |
1502 |
201390 |
return antecedent.impNode(implied); |
1503 |
|
} |
1504 |
|
|
1505 |
|
|
1506 |
1122420 |
Node Constraint::externalExplainByAssertions(const ConstraintCPVec& b){ |
1507 |
1122420 |
return externalExplain(b, AssertionOrderSentinel); |
1508 |
|
} |
1509 |
|
|
1510 |
16369 |
TrustNode Constraint::externalExplainForPropagation(TNode lit) const |
1511 |
|
{ |
1512 |
16369 |
Assert(hasProof()); |
1513 |
16369 |
Assert(!isAssumption()); |
1514 |
16369 |
Assert(!isInternalAssumption()); |
1515 |
32738 |
NodeBuilder nb(Kind::AND); |
1516 |
32738 |
auto pfFromAssumptions = externalExplain(nb, d_assertionOrder); |
1517 |
32738 |
Node n = safeConstructNary(nb); |
1518 |
16369 |
if (d_database->isProofEnabled()) |
1519 |
|
{ |
1520 |
|
// Check that the literal we're explaining via this constraint actually |
1521 |
|
// matches the constraint's canonical literal. |
1522 |
2310 |
Assert(Rewriter::rewrite(lit) == getLiteral()); |
1523 |
4620 |
std::vector<Node> assumptions; |
1524 |
2310 |
if (n.getKind() == Kind::AND) |
1525 |
|
{ |
1526 |
1404 |
assumptions.insert(assumptions.end(), n.begin(), n.end()); |
1527 |
|
} |
1528 |
|
else |
1529 |
|
{ |
1530 |
906 |
assumptions.push_back(n); |
1531 |
|
} |
1532 |
2310 |
if (getProofLiteral() != lit) |
1533 |
|
{ |
1534 |
4500 |
pfFromAssumptions = d_database->d_pnm->mkNode( |
1535 |
3000 |
PfRule::MACRO_SR_PRED_TRANSFORM, {pfFromAssumptions}, {lit}); |
1536 |
|
} |
1537 |
4620 |
auto pf = d_database->d_pnm->mkScope(pfFromAssumptions, assumptions); |
1538 |
2310 |
return d_database->d_pfGen->mkTrustedPropagation( |
1539 |
2310 |
lit, safeConstructNary(Kind::AND, assumptions), pf); |
1540 |
|
} |
1541 |
|
else |
1542 |
|
{ |
1543 |
14059 |
return TrustNode::mkTrustPropExp(lit, n); |
1544 |
|
} |
1545 |
|
} |
1546 |
|
|
1547 |
80562 |
TrustNode Constraint::externalExplainConflict() const |
1548 |
|
{ |
1549 |
80562 |
Debug("pf::arith::explain") << this << std::endl; |
1550 |
80562 |
Assert(inConflict()); |
1551 |
161124 |
NodeBuilder nb(kind::AND); |
1552 |
161124 |
auto pf1 = externalExplainByAssertions(nb); |
1553 |
161124 |
auto not2 = getNegation()->getProofLiteral().negate(); |
1554 |
161124 |
auto pf2 = getNegation()->externalExplainByAssertions(nb); |
1555 |
161124 |
Node n = safeConstructNary(nb); |
1556 |
80562 |
if (d_database->isProofEnabled()) |
1557 |
|
{ |
1558 |
13027 |
auto pfNot2 = d_database->d_pnm->mkNode( |
1559 |
26054 |
PfRule::MACRO_SR_PRED_TRANSFORM, {pf1}, {not2}); |
1560 |
26054 |
std::vector<Node> lits; |
1561 |
13027 |
if (n.getKind() == Kind::AND) |
1562 |
|
{ |
1563 |
13027 |
lits.insert(lits.end(), n.begin(), n.end()); |
1564 |
|
} |
1565 |
|
else |
1566 |
|
{ |
1567 |
|
lits.push_back(n); |
1568 |
|
} |
1569 |
13027 |
if (Debug.isOn("arith::pf::externalExplainConflict")) |
1570 |
|
{ |
1571 |
|
Debug("arith::pf::externalExplainConflict") << "Lits:" << std::endl; |
1572 |
|
for (const auto& l : lits) |
1573 |
|
{ |
1574 |
|
Debug("arith::pf::externalExplainConflict") << " : " << l << std::endl; |
1575 |
|
} |
1576 |
|
} |
1577 |
|
std::vector<Node> contraLits = {getProofLiteral(), |
1578 |
26054 |
getNegation()->getProofLiteral()}; |
1579 |
|
auto bot = |
1580 |
13027 |
not2.getKind() == Kind::NOT |
1581 |
36189 |
? d_database->d_pnm->mkNode(PfRule::CONTRA, {pf2, pfNot2}, {}) |
1582 |
51144 |
: d_database->d_pnm->mkNode(PfRule::CONTRA, {pfNot2, pf2}, {}); |
1583 |
13027 |
if (Debug.isOn("arith::pf::tree")) |
1584 |
|
{ |
1585 |
|
Debug("arith::pf::tree") << *this << std::endl; |
1586 |
|
Debug("arith::pf::tree") << *getNegation() << std::endl; |
1587 |
|
Debug("arith::pf::tree") << "\n\nTree:\n"; |
1588 |
|
printProofTree(Debug("arith::pf::tree")); |
1589 |
|
getNegation()->printProofTree(Debug("arith::pf::tree")); |
1590 |
|
} |
1591 |
26054 |
auto confPf = d_database->d_pnm->mkScope(bot, lits); |
1592 |
13027 |
return d_database->d_pfGen->mkTrustNode( |
1593 |
13027 |
safeConstructNary(Kind::AND, lits), confPf, true); |
1594 |
|
} |
1595 |
|
else |
1596 |
|
{ |
1597 |
67535 |
return TrustNode::mkTrustConflict(n); |
1598 |
|
} |
1599 |
|
} |
1600 |
|
|
1601 |
|
struct ConstraintCPHash { |
1602 |
|
/* Todo replace with an id */ |
1603 |
|
size_t operator()(ConstraintCP c) const{ |
1604 |
|
Assert(sizeof(ConstraintCP) > 0); |
1605 |
|
return ((size_t)c)/sizeof(ConstraintCP); |
1606 |
|
} |
1607 |
|
}; |
1608 |
|
|
1609 |
|
void Constraint::assertionFringe(ConstraintCPVec& v){ |
1610 |
|
unordered_set<ConstraintCP, ConstraintCPHash> visited; |
1611 |
|
size_t writePos = 0; |
1612 |
|
|
1613 |
|
if(!v.empty()){ |
1614 |
|
const ConstraintDatabase* db = v.back()->d_database; |
1615 |
|
const CDConstraintList& antecedents = db->d_antecedents; |
1616 |
|
for(size_t i = 0; i < v.size(); ++i){ |
1617 |
|
ConstraintCP vi = v[i]; |
1618 |
|
if(visited.find(vi) == visited.end()){ |
1619 |
|
Assert(vi->hasProof()); |
1620 |
|
visited.insert(vi); |
1621 |
|
if(vi->onFringe()){ |
1622 |
|
v[writePos] = vi; |
1623 |
|
writePos++; |
1624 |
|
}else{ |
1625 |
|
Assert(vi->hasTrichotomyProof() || vi->hasFarkasProof() |
1626 |
|
|| vi->hasIntHoleProof() || vi->hasIntTightenProof()); |
1627 |
|
AntecedentId p = vi->getEndAntecedent(); |
1628 |
|
|
1629 |
|
ConstraintCP antecedent = antecedents[p]; |
1630 |
|
while(antecedent != NullConstraint){ |
1631 |
|
v.push_back(antecedent); |
1632 |
|
--p; |
1633 |
|
antecedent = antecedents[p]; |
1634 |
|
} |
1635 |
|
} |
1636 |
|
} |
1637 |
|
} |
1638 |
|
v.resize(writePos); |
1639 |
|
} |
1640 |
|
} |
1641 |
|
|
1642 |
|
void Constraint::assertionFringe(ConstraintCPVec& o, const ConstraintCPVec& i){ |
1643 |
|
o.insert(o.end(), i.begin(), i.end()); |
1644 |
|
assertionFringe(o); |
1645 |
|
} |
1646 |
|
|
1647 |
1122420 |
Node Constraint::externalExplain(const ConstraintCPVec& v, AssertionOrder order){ |
1648 |
2244840 |
NodeBuilder nb(kind::AND); |
1649 |
1122420 |
ConstraintCPVec::const_iterator i, end; |
1650 |
2492670 |
for(i = v.begin(), end = v.end(); i != end; ++i){ |
1651 |
1370250 |
ConstraintCP v_i = *i; |
1652 |
1370250 |
v_i->externalExplain(nb, order); |
1653 |
|
} |
1654 |
2244840 |
return safeConstructNary(nb); |
1655 |
|
} |
1656 |
|
|
1657 |
5560380 |
std::shared_ptr<ProofNode> Constraint::externalExplain( |
1658 |
|
NodeBuilder& nb, AssertionOrder order) const |
1659 |
|
{ |
1660 |
5560380 |
if (Debug.isOn("pf::arith::explain")) |
1661 |
|
{ |
1662 |
|
this->printProofTree(Debug("arith::pf::tree")); |
1663 |
|
Debug("pf::arith::explain") << "Explaining: " << this << " with rule "; |
1664 |
|
getConstraintRule().print(Debug("pf::arith::explain")); |
1665 |
|
Debug("pf::arith::explain") << std::endl; |
1666 |
|
} |
1667 |
5560380 |
Assert(hasProof()); |
1668 |
5560380 |
Assert(!isAssumption() || assertedToTheTheory()); |
1669 |
5560380 |
Assert(!isInternalAssumption()); |
1670 |
5560380 |
std::shared_ptr<ProofNode> pf{}; |
1671 |
|
|
1672 |
5560380 |
ProofNodeManager* pnm = d_database->d_pnm; |
1673 |
|
|
1674 |
5560380 |
if (assertedBefore(order)) |
1675 |
|
{ |
1676 |
5046979 |
Debug("pf::arith::explain") << " already asserted" << std::endl; |
1677 |
5046979 |
nb << getWitness(); |
1678 |
5046979 |
if (d_database->isProofEnabled()) |
1679 |
|
{ |
1680 |
680314 |
pf = pnm->mkAssume(getWitness()); |
1681 |
|
// If the witness and literal differ, prove the difference through a |
1682 |
|
// rewrite. |
1683 |
680314 |
if (getWitness() != getProofLiteral()) |
1684 |
|
{ |
1685 |
1348884 |
pf = pnm->mkNode( |
1686 |
899256 |
PfRule::MACRO_SR_PRED_TRANSFORM, {pf}, {getProofLiteral()}); |
1687 |
|
} |
1688 |
|
} |
1689 |
|
} |
1690 |
513401 |
else if (hasEqualityEngineProof()) |
1691 |
|
{ |
1692 |
6779 |
Debug("pf::arith::explain") << " going to ee:" << std::endl; |
1693 |
13558 |
TrustNode exp = d_database->eeExplain(this); |
1694 |
6779 |
if (d_database->isProofEnabled()) |
1695 |
|
{ |
1696 |
883 |
Assert(exp.getProven().getKind() == Kind::IMPLIES); |
1697 |
1766 |
std::vector<std::shared_ptr<ProofNode>> hypotheses; |
1698 |
883 |
hypotheses.push_back(exp.getGenerator()->getProofFor(exp.getProven())); |
1699 |
883 |
if (exp.getNode().getKind() == Kind::AND) |
1700 |
|
{ |
1701 |
3004 |
for (const auto& h : exp.getNode()) |
1702 |
|
{ |
1703 |
2197 |
hypotheses.push_back( |
1704 |
4394 |
pnm->mkNode(PfRule::TRUE_INTRO, {pnm->mkAssume(h)}, {})); |
1705 |
|
} |
1706 |
|
} |
1707 |
|
else |
1708 |
|
{ |
1709 |
304 |
hypotheses.push_back(pnm->mkNode( |
1710 |
228 |
PfRule::TRUE_INTRO, {pnm->mkAssume(exp.getNode())}, {})); |
1711 |
|
} |
1712 |
1766 |
pf = pnm->mkNode( |
1713 |
883 |
PfRule::MACRO_SR_PRED_TRANSFORM, {hypotheses}, {getProofLiteral()}); |
1714 |
|
} |
1715 |
13558 |
Debug("pf::arith::explain") |
1716 |
6779 |
<< " explanation: " << exp.getNode() << std::endl; |
1717 |
6779 |
if (exp.getNode().getKind() == Kind::AND) |
1718 |
|
{ |
1719 |
6050 |
nb.append(exp.getNode().begin(), exp.getNode().end()); |
1720 |
|
} |
1721 |
|
else |
1722 |
|
{ |
1723 |
729 |
nb << exp.getNode(); |
1724 |
|
} |
1725 |
|
} |
1726 |
|
else |
1727 |
|
{ |
1728 |
506622 |
Debug("pf::arith::explain") << " recursion!" << std::endl; |
1729 |
506622 |
Assert(!isAssumption()); |
1730 |
506622 |
AntecedentId p = getEndAntecedent(); |
1731 |
506622 |
ConstraintCP antecedent = d_database->d_antecedents[p]; |
1732 |
1013244 |
std::vector<std::shared_ptr<ProofNode>> children; |
1733 |
|
|
1734 |
3302716 |
while (antecedent != NullConstraint) |
1735 |
|
{ |
1736 |
1398047 |
Debug("pf::arith::explain") << "Explain " << antecedent << std::endl; |
1737 |
2796094 |
auto pn = antecedent->externalExplain(nb, order); |
1738 |
1398047 |
if (d_database->isProofEnabled()) |
1739 |
|
{ |
1740 |
153450 |
children.push_back(pn); |
1741 |
|
} |
1742 |
1398047 |
--p; |
1743 |
1398047 |
antecedent = d_database->d_antecedents[p]; |
1744 |
|
} |
1745 |
|
|
1746 |
506622 |
if (d_database->isProofEnabled()) |
1747 |
|
{ |
1748 |
89590 |
switch (getProofType()) |
1749 |
|
{ |
1750 |
|
case ArithProofType::AssumeAP: |
1751 |
|
case ArithProofType::EqualityEngineAP: |
1752 |
|
{ |
1753 |
|
Unreachable() << "These should be handled above"; |
1754 |
|
break; |
1755 |
|
} |
1756 |
13525 |
case ArithProofType::FarkasAP: |
1757 |
|
{ |
1758 |
|
// Per docs in constraint.h, |
1759 |
|
// the 0th farkas coefficient is for the negation of the deduced |
1760 |
|
// constraint the 1st corresponds to the last antecedent the nth |
1761 |
|
// corresponds to the first antecedent Then, the farkas coefficients |
1762 |
|
// and the antecedents are in the same order. |
1763 |
|
|
1764 |
|
// Enumerate child proofs (negation included) in d_farkasCoefficients |
1765 |
|
// order |
1766 |
27050 |
std::vector<std::shared_ptr<ProofNode>> farkasChildren; |
1767 |
13525 |
farkasChildren.push_back( |
1768 |
27050 |
pnm->mkAssume(getNegation()->getProofLiteral())); |
1769 |
13525 |
farkasChildren.insert( |
1770 |
27050 |
farkasChildren.end(), children.rbegin(), children.rend()); |
1771 |
|
|
1772 |
13525 |
NodeManager* nm = NodeManager::currentNM(); |
1773 |
|
|
1774 |
|
// Enumerate d_farkasCoefficients as nodes. |
1775 |
27050 |
std::vector<Node> farkasCoeffs; |
1776 |
97630 |
for (Rational r : *getFarkasCoefficients()) |
1777 |
|
{ |
1778 |
84105 |
farkasCoeffs.push_back(nm->mkConst<Rational>(r)); |
1779 |
|
} |
1780 |
|
|
1781 |
|
// Apply the scaled-sum rule. |
1782 |
|
std::shared_ptr<ProofNode> sumPf = pnm->mkNode( |
1783 |
27050 |
PfRule::MACRO_ARITH_SCALE_SUM_UB, farkasChildren, farkasCoeffs); |
1784 |
|
|
1785 |
|
// Provable rewrite the result |
1786 |
|
auto botPf = pnm->mkNode( |
1787 |
27050 |
PfRule::MACRO_SR_PRED_TRANSFORM, {sumPf}, {nm->mkConst(false)}); |
1788 |
|
|
1789 |
|
// Scope out the negated constraint, yielding a proof of the |
1790 |
|
// constraint. |
1791 |
27050 |
std::vector<Node> assump{getNegation()->getProofLiteral()}; |
1792 |
27050 |
auto maybeDoubleNotPf = pnm->mkScope(botPf, assump, false); |
1793 |
|
|
1794 |
|
// No need to ensure that the expected node aggrees with `assump` |
1795 |
|
// because we are not providing an expected node. |
1796 |
|
// |
1797 |
|
// Prove that this is the literal (may need to clean a double-not) |
1798 |
40575 |
pf = pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM, |
1799 |
|
{maybeDoubleNotPf}, |
1800 |
27050 |
{getProofLiteral()}); |
1801 |
|
|
1802 |
13525 |
break; |
1803 |
|
} |
1804 |
69260 |
case ArithProofType::IntTightenAP: |
1805 |
|
{ |
1806 |
69260 |
if (isUpperBound()) |
1807 |
|
{ |
1808 |
67024 |
pf = pnm->mkNode( |
1809 |
134048 |
PfRule::INT_TIGHT_UB, children, {}, getProofLiteral()); |
1810 |
|
} |
1811 |
2236 |
else if (isLowerBound()) |
1812 |
|
{ |
1813 |
2236 |
pf = pnm->mkNode( |
1814 |
4472 |
PfRule::INT_TIGHT_LB, children, {}, getProofLiteral()); |
1815 |
|
} |
1816 |
|
else |
1817 |
|
{ |
1818 |
|
Unreachable(); |
1819 |
|
} |
1820 |
69260 |
break; |
1821 |
|
} |
1822 |
|
case ArithProofType::IntHoleAP: |
1823 |
|
{ |
1824 |
|
Node t = |
1825 |
|
builtin::BuiltinProofRuleChecker::mkTheoryIdNode(THEORY_ARITH); |
1826 |
|
pf = pnm->mkNode(PfRule::THEORY_INFERENCE, |
1827 |
|
children, |
1828 |
|
{getProofLiteral(), t}, |
1829 |
|
getProofLiteral()); |
1830 |
|
break; |
1831 |
|
} |
1832 |
6805 |
case ArithProofType::TrichotomyAP: |
1833 |
|
{ |
1834 |
13610 |
pf = pnm->mkNode(PfRule::ARITH_TRICHOTOMY, |
1835 |
|
children, |
1836 |
|
{getProofLiteral()}, |
1837 |
20415 |
getProofLiteral()); |
1838 |
6805 |
break; |
1839 |
|
} |
1840 |
|
case ArithProofType::InternalAssumeAP: |
1841 |
|
case ArithProofType::NoAP: |
1842 |
|
default: |
1843 |
|
{ |
1844 |
|
Unreachable() << getProofType() |
1845 |
|
<< " should not be visible in explanation"; |
1846 |
|
break; |
1847 |
|
} |
1848 |
|
} |
1849 |
|
} |
1850 |
|
} |
1851 |
5560380 |
return pf; |
1852 |
|
} |
1853 |
|
|
1854 |
1846 |
Node Constraint::externalExplainByAssertions(ConstraintCP a, ConstraintCP b){ |
1855 |
3692 |
NodeBuilder nb(kind::AND); |
1856 |
1846 |
a->externalExplainByAssertions(nb); |
1857 |
1846 |
b->externalExplainByAssertions(nb); |
1858 |
3692 |
return nb; |
1859 |
|
} |
1860 |
|
|
1861 |
|
Node Constraint::externalExplainByAssertions(ConstraintCP a, ConstraintCP b, ConstraintCP c){ |
1862 |
|
NodeBuilder nb(kind::AND); |
1863 |
|
a->externalExplainByAssertions(nb); |
1864 |
|
b->externalExplainByAssertions(nb); |
1865 |
|
c->externalExplainByAssertions(nb); |
1866 |
|
return nb; |
1867 |
|
} |
1868 |
|
|
1869 |
718350 |
ConstraintP Constraint::getStrictlyWeakerLowerBound(bool hasLiteral, bool asserted) const { |
1870 |
718350 |
Assert(initialized()); |
1871 |
718350 |
Assert(!asserted || hasLiteral); |
1872 |
|
|
1873 |
718350 |
SortedConstraintMapConstIterator i = d_variablePosition; |
1874 |
718350 |
const SortedConstraintMap& scm = constraintSet(); |
1875 |
718350 |
SortedConstraintMapConstIterator i_begin = scm.begin(); |
1876 |
2340644 |
while(i != i_begin){ |
1877 |
943427 |
--i; |
1878 |
943427 |
const ValueCollection& vc = i->second; |
1879 |
943427 |
if(vc.hasLowerBound()){ |
1880 |
259792 |
ConstraintP weaker = vc.getLowerBound(); |
1881 |
|
|
1882 |
|
// asserted -> hasLiteral |
1883 |
|
// hasLiteral -> weaker->hasLiteral() |
1884 |
|
// asserted -> weaker->assertedToTheTheory() |
1885 |
536418 |
if((!hasLiteral || (weaker->hasLiteral())) && |
1886 |
287457 |
(!asserted || ( weaker->assertedToTheTheory()))){ |
1887 |
132280 |
return weaker; |
1888 |
|
} |
1889 |
|
} |
1890 |
|
} |
1891 |
586070 |
return NullConstraint; |
1892 |
|
} |
1893 |
|
|
1894 |
380122 |
ConstraintP Constraint::getStrictlyWeakerUpperBound(bool hasLiteral, bool asserted) const { |
1895 |
380122 |
SortedConstraintMapConstIterator i = d_variablePosition; |
1896 |
380122 |
const SortedConstraintMap& scm = constraintSet(); |
1897 |
380122 |
SortedConstraintMapConstIterator i_end = scm.end(); |
1898 |
|
|
1899 |
380122 |
++i; |
1900 |
1115594 |
for(; i != i_end; ++i){ |
1901 |
504388 |
const ValueCollection& vc = i->second; |
1902 |
504388 |
if(vc.hasUpperBound()){ |
1903 |
183720 |
ConstraintP weaker = vc.getUpperBound(); |
1904 |
468483 |
if((!hasLiteral || (weaker->hasLiteral())) && |
1905 |
291422 |
(!asserted || ( weaker->assertedToTheTheory()))){ |
1906 |
136652 |
return weaker; |
1907 |
|
} |
1908 |
|
} |
1909 |
|
} |
1910 |
|
|
1911 |
243470 |
return NullConstraint; |
1912 |
|
} |
1913 |
|
|
1914 |
8510017 |
ConstraintP ConstraintDatabase::getBestImpliedBound(ArithVar v, ConstraintType t, const DeltaRational& r) const { |
1915 |
8510017 |
Assert(variableDatabaseIsSetup(v)); |
1916 |
8510017 |
Assert(t == UpperBound || t == LowerBound); |
1917 |
|
|
1918 |
8510017 |
SortedConstraintMap& scm = getVariableSCM(v); |
1919 |
8510017 |
if(t == UpperBound){ |
1920 |
4122146 |
SortedConstraintMapConstIterator i = scm.lower_bound(r); |
1921 |
4122146 |
SortedConstraintMapConstIterator i_end = scm.end(); |
1922 |
4122146 |
Assert(i == i_end || r <= i->first); |
1923 |
8564114 |
for(; i != i_end; i++){ |
1924 |
3566038 |
Assert(r <= i->first); |
1925 |
3566038 |
const ValueCollection& vc = i->second; |
1926 |
3566038 |
if(vc.hasUpperBound()){ |
1927 |
1345054 |
return vc.getUpperBound(); |
1928 |
|
} |
1929 |
|
} |
1930 |
2777092 |
return NullConstraint; |
1931 |
|
}else{ |
1932 |
4387871 |
Assert(t == LowerBound); |
1933 |
4387871 |
if(scm.empty()){ |
1934 |
310522 |
return NullConstraint; |
1935 |
|
}else{ |
1936 |
4077349 |
SortedConstraintMapConstIterator i = scm.lower_bound(r); |
1937 |
4077349 |
SortedConstraintMapConstIterator i_begin = scm.begin(); |
1938 |
4077349 |
SortedConstraintMapConstIterator i_end = scm.end(); |
1939 |
4077349 |
Assert(i == i_end || r <= i->first); |
1940 |
|
|
1941 |
4077349 |
int fdj = 0; |
1942 |
|
|
1943 |
4077349 |
if(i == i_end){ |
1944 |
1701177 |
--i; |
1945 |
1701177 |
Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl; |
1946 |
2376172 |
}else if( (i->first) > r){ |
1947 |
620958 |
if(i == i_begin){ |
1948 |
566735 |
return NullConstraint; |
1949 |
|
}else{ |
1950 |
54223 |
--i; |
1951 |
54223 |
Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl; |
1952 |
|
} |
1953 |
|
} |
1954 |
|
|
1955 |
|
do{ |
1956 |
3938527 |
Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl; |
1957 |
3938527 |
Assert(r >= i->first); |
1958 |
3938527 |
const ValueCollection& vc = i->second; |
1959 |
|
|
1960 |
3938527 |
if(vc.hasLowerBound()){ |
1961 |
1731825 |
return vc.getLowerBound(); |
1962 |
|
} |
1963 |
|
|
1964 |
2206702 |
if(i == i_begin){ |
1965 |
1778789 |
break; |
1966 |
|
}else{ |
1967 |
427913 |
--i; |
1968 |
427913 |
} |
1969 |
|
}while(true); |
1970 |
1778789 |
return NullConstraint; |
1971 |
|
} |
1972 |
|
} |
1973 |
|
} |
1974 |
6779 |
TrustNode ConstraintDatabase::eeExplain(const Constraint* const c) const |
1975 |
|
{ |
1976 |
6779 |
Assert(c->hasLiteral()); |
1977 |
6779 |
return d_congruenceManager.explain(c->getLiteral()); |
1978 |
|
} |
1979 |
|
|
1980 |
|
void ConstraintDatabase::eeExplain(ConstraintCP c, NodeBuilder& nb) const |
1981 |
|
{ |
1982 |
|
Assert(c->hasLiteral()); |
1983 |
|
// NOTE: this is not a recommended method since it ignores proofs |
1984 |
|
d_congruenceManager.explain(c->getLiteral(), nb); |
1985 |
|
} |
1986 |
|
|
1987 |
23432371 |
bool ConstraintDatabase::variableDatabaseIsSetup(ArithVar v) const { |
1988 |
23432371 |
return v < d_varDatabases.size(); |
1989 |
|
} |
1990 |
|
|
1991 |
|
|
1992 |
9926 |
ConstraintDatabase::Watches::Watches(context::Context* satContext, context::Context* userContext): |
1993 |
|
d_constraintProofs(satContext), |
1994 |
|
d_canBePropagatedWatches(satContext), |
1995 |
|
d_assertionOrderWatches(satContext), |
1996 |
9926 |
d_splitWatches(userContext) |
1997 |
9926 |
{} |
1998 |
|
|
1999 |
|
|
2000 |
542672 |
void Constraint::setLiteral(Node n) { |
2001 |
542672 |
Debug("arith::constraint") << "Mapping " << *this << " to " << n << std::endl; |
2002 |
542672 |
Assert(Comparison::isNormalAtom(n)); |
2003 |
542672 |
Assert(!hasLiteral()); |
2004 |
542672 |
Assert(sanityChecking(n)); |
2005 |
542672 |
d_literal = n; |
2006 |
542672 |
NodetoConstraintMap& map = d_database->d_nodetoConstraintMap; |
2007 |
542672 |
Assert(map.find(n) == map.end()); |
2008 |
542672 |
map.insert(make_pair(d_literal, this)); |
2009 |
542672 |
} |
2010 |
|
|
2011 |
1412318 |
Node Constraint::getProofLiteral() const |
2012 |
|
{ |
2013 |
1412318 |
Assert(d_database != nullptr); |
2014 |
1412318 |
Assert(d_database->d_avariables.hasNode(d_variable)); |
2015 |
2824636 |
Node varPart = d_database->d_avariables.asNode(d_variable); |
2016 |
|
Kind cmp; |
2017 |
1412318 |
bool neg = false; |
2018 |
1412318 |
switch (d_type) |
2019 |
|
{ |
2020 |
538099 |
case ConstraintType::UpperBound: |
2021 |
|
{ |
2022 |
538099 |
if (d_value.infinitesimalIsZero()) |
2023 |
|
{ |
2024 |
145627 |
cmp = Kind::LEQ; |
2025 |
|
} |
2026 |
|
else |
2027 |
|
{ |
2028 |
392472 |
cmp = Kind::LT; |
2029 |
|
} |
2030 |
538099 |
break; |
2031 |
|
} |
2032 |
220547 |
case ConstraintType::LowerBound: |
2033 |
|
{ |
2034 |
220547 |
if (d_value.infinitesimalIsZero()) |
2035 |
|
{ |
2036 |
178507 |
cmp = Kind::GEQ; |
2037 |
|
} |
2038 |
|
else |
2039 |
|
{ |
2040 |
42040 |
cmp = Kind::GT; |
2041 |
|
} |
2042 |
220547 |
break; |
2043 |
|
} |
2044 |
517177 |
case ConstraintType::Equality: |
2045 |
|
{ |
2046 |
517177 |
cmp = Kind::EQUAL; |
2047 |
517177 |
break; |
2048 |
|
} |
2049 |
136495 |
case ConstraintType::Disequality: |
2050 |
|
{ |
2051 |
136495 |
cmp = Kind::EQUAL; |
2052 |
136495 |
neg = true; |
2053 |
136495 |
break; |
2054 |
|
} |
2055 |
|
default: Unreachable() << d_type; |
2056 |
|
} |
2057 |
1412318 |
NodeManager* nm = NodeManager::currentNM(); |
2058 |
2824636 |
Node constPart = nm->mkConst<Rational>(d_value.getNoninfinitesimalPart()); |
2059 |
2824636 |
Node posLit = nm->mkNode(cmp, varPart, constPart); |
2060 |
2824636 |
return neg ? posLit.negate() : posLit; |
2061 |
|
} |
2062 |
|
|
2063 |
37212 |
void ConstraintDatabase::proveOr(std::vector<TrustNode>& out, |
2064 |
|
ConstraintP a, |
2065 |
|
ConstraintP b, |
2066 |
|
bool negateSecond) const |
2067 |
|
{ |
2068 |
74424 |
Node la = a->getLiteral(); |
2069 |
74424 |
Node lb = b->getLiteral(); |
2070 |
74424 |
Node orN = (la < lb) ? la.orNode(lb) : lb.orNode(la); |
2071 |
37212 |
if (isProofEnabled()) |
2072 |
|
{ |
2073 |
5911 |
Assert(b->getNegation()->getType() != ConstraintType::Disequality); |
2074 |
5911 |
auto nm = NodeManager::currentNM(); |
2075 |
5911 |
auto pf_neg_la = d_pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM, |
2076 |
11822 |
{d_pnm->mkAssume(la.negate())}, |
2077 |
23644 |
{a->getNegation()->getProofLiteral()}); |
2078 |
5911 |
auto pf_neg_lb = d_pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM, |
2079 |
11822 |
{d_pnm->mkAssume(lb.negate())}, |
2080 |
23644 |
{b->getNegation()->getProofLiteral()}); |
2081 |
5911 |
int sndSign = negateSecond ? -1 : 1; |
2082 |
|
auto bot_pf = |
2083 |
5911 |
d_pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM, |
2084 |
5911 |
{d_pnm->mkNode(PfRule::MACRO_ARITH_SCALE_SUM_UB, |
2085 |
|
{pf_neg_la, pf_neg_lb}, |
2086 |
|
{nm->mkConst<Rational>(-1 * sndSign), |
2087 |
23644 |
nm->mkConst<Rational>(sndSign)})}, |
2088 |
29555 |
{nm->mkConst(false)}); |
2089 |
11822 |
std::vector<Node> as; |
2090 |
17733 |
std::transform(orN.begin(), orN.end(), std::back_inserter(as), [](Node n) { |
2091 |
|
return n.negate(); |
2092 |
17733 |
}); |
2093 |
|
// No need to ensure that the expected node aggrees with `as` because we |
2094 |
|
// are not providing an expected node. |
2095 |
5911 |
auto pf = d_pnm->mkNode( |
2096 |
|
PfRule::MACRO_SR_PRED_TRANSFORM, |
2097 |
17733 |
{d_pnm->mkNode(PfRule::NOT_AND, {d_pnm->mkScope(bot_pf, as)}, {})}, |
2098 |
29555 |
{orN}); |
2099 |
5911 |
out.push_back(d_pfGen->mkTrustNode(orN, pf)); |
2100 |
|
} |
2101 |
|
else |
2102 |
|
{ |
2103 |
31301 |
out.push_back(TrustNode::mkTrustLemma(orN)); |
2104 |
|
} |
2105 |
37212 |
} |
2106 |
|
|
2107 |
34872 |
void ConstraintDatabase::implies(std::vector<TrustNode>& out, |
2108 |
|
ConstraintP a, |
2109 |
|
ConstraintP b) const |
2110 |
|
{ |
2111 |
69744 |
Node la = a->getLiteral(); |
2112 |
69744 |
Node lb = b->getLiteral(); |
2113 |
|
|
2114 |
69744 |
Node neg_la = (la.getKind() == kind::NOT)? la[0] : la.notNode(); |
2115 |
|
|
2116 |
34872 |
Assert(lb != neg_la); |
2117 |
34872 |
Assert(b->getNegation()->getType() == ConstraintType::LowerBound |
2118 |
|
|| b->getNegation()->getType() == ConstraintType::UpperBound); |
2119 |
34872 |
proveOr(out, |
2120 |
|
a->getNegation(), |
2121 |
|
b, |
2122 |
34872 |
b->getNegation()->getType() == ConstraintType::LowerBound); |
2123 |
34872 |
} |
2124 |
|
|
2125 |
2340 |
void ConstraintDatabase::mutuallyExclusive(std::vector<TrustNode>& out, |
2126 |
|
ConstraintP a, |
2127 |
|
ConstraintP b) const |
2128 |
|
{ |
2129 |
4680 |
Node la = a->getLiteral(); |
2130 |
4680 |
Node lb = b->getLiteral(); |
2131 |
|
|
2132 |
4680 |
Node neg_la = la.negate(); |
2133 |
4680 |
Node neg_lb = lb.negate(); |
2134 |
2340 |
proveOr(out, a->getNegation(), b->getNegation(), true); |
2135 |
2340 |
} |
2136 |
|
|
2137 |
56675 |
void ConstraintDatabase::outputUnateInequalityLemmas( |
2138 |
|
std::vector<TrustNode>& out, ArithVar v) const |
2139 |
|
{ |
2140 |
56675 |
SortedConstraintMap& scm = getVariableSCM(v); |
2141 |
56675 |
SortedConstraintMapConstIterator scm_iter = scm.begin(); |
2142 |
56675 |
SortedConstraintMapConstIterator scm_end = scm.end(); |
2143 |
56675 |
ConstraintP prev = NullConstraint; |
2144 |
|
//get transitive unates |
2145 |
|
//Only lower bounds or upperbounds should be done. |
2146 |
301151 |
for(; scm_iter != scm_end; ++scm_iter){ |
2147 |
122238 |
const ValueCollection& vc = scm_iter->second; |
2148 |
122238 |
if(vc.hasUpperBound()){ |
2149 |
58704 |
ConstraintP ub = vc.getUpperBound(); |
2150 |
58704 |
if(ub->hasLiteral()){ |
2151 |
58704 |
if(prev != NullConstraint){ |
2152 |
27027 |
implies(out, prev, ub); |
2153 |
|
} |
2154 |
58704 |
prev = ub; |
2155 |
|
} |
2156 |
|
} |
2157 |
|
} |
2158 |
56675 |
} |
2159 |
|
|
2160 |
56675 |
void ConstraintDatabase::outputUnateEqualityLemmas(std::vector<TrustNode>& out, |
2161 |
|
ArithVar v) const |
2162 |
|
{ |
2163 |
113350 |
vector<ConstraintP> equalities; |
2164 |
|
|
2165 |
56675 |
SortedConstraintMap& scm = getVariableSCM(v); |
2166 |
56675 |
SortedConstraintMapConstIterator scm_iter = scm.begin(); |
2167 |
56675 |
SortedConstraintMapConstIterator scm_end = scm.end(); |
2168 |
|
|
2169 |
301151 |
for(; scm_iter != scm_end; ++scm_iter){ |
2170 |
122238 |
const ValueCollection& vc = scm_iter->second; |
2171 |
122238 |
if(vc.hasEquality()){ |
2172 |
17888 |
ConstraintP eq = vc.getEquality(); |
2173 |
17888 |
if(eq->hasLiteral()){ |
2174 |
17888 |
equalities.push_back(eq); |
2175 |
|
} |
2176 |
|
} |
2177 |
|
} |
2178 |
|
|
2179 |
56675 |
vector<ConstraintP>::const_iterator i, j, eq_end = equalities.end(); |
2180 |
74563 |
for(i = equalities.begin(); i != eq_end; ++i){ |
2181 |
17888 |
ConstraintP at_i = *i; |
2182 |
20228 |
for(j= i + 1; j != eq_end; ++j){ |
2183 |
2340 |
ConstraintP at_j = *j; |
2184 |
|
|
2185 |
2340 |
mutuallyExclusive(out, at_i, at_j); |
2186 |
|
} |
2187 |
|
} |
2188 |
|
|
2189 |
74563 |
for(i = equalities.begin(); i != eq_end; ++i){ |
2190 |
17888 |
ConstraintP eq = *i; |
2191 |
17888 |
const ValueCollection& vc = eq->getValueCollection(); |
2192 |
17888 |
Assert(vc.hasEquality() && vc.getEquality()->hasLiteral()); |
2193 |
|
|
2194 |
17888 |
bool hasLB = vc.hasLowerBound() && vc.getLowerBound()->hasLiteral(); |
2195 |
17888 |
bool hasUB = vc.hasUpperBound() && vc.getUpperBound()->hasLiteral(); |
2196 |
|
|
2197 |
17888 |
ConstraintP lb = hasLB ? |
2198 |
17888 |
vc.getLowerBound() : eq->getStrictlyWeakerLowerBound(true, false); |
2199 |
17888 |
ConstraintP ub = hasUB ? |
2200 |
17888 |
vc.getUpperBound() : eq->getStrictlyWeakerUpperBound(true, false); |
2201 |
|
|
2202 |
17888 |
if(hasUB && hasLB && !eq->isSplit()){ |
2203 |
115 |
out.push_back(eq->split()); |
2204 |
|
} |
2205 |
17888 |
if(lb != NullConstraint){ |
2206 |
2862 |
implies(out, eq, lb); |
2207 |
|
} |
2208 |
17888 |
if(ub != NullConstraint){ |
2209 |
4983 |
implies(out, eq, ub); |
2210 |
|
} |
2211 |
|
} |
2212 |
56675 |
} |
2213 |
|
|
2214 |
7670 |
void ConstraintDatabase::outputUnateEqualityLemmas( |
2215 |
|
std::vector<TrustNode>& lemmas) const |
2216 |
|
{ |
2217 |
64345 |
for(ArithVar v = 0, N = d_varDatabases.size(); v < N; ++v){ |
2218 |
56675 |
outputUnateEqualityLemmas(lemmas, v); |
2219 |
|
} |
2220 |
7670 |
} |
2221 |
|
|
2222 |
7670 |
void ConstraintDatabase::outputUnateInequalityLemmas( |
2223 |
|
std::vector<TrustNode>& lemmas) const |
2224 |
|
{ |
2225 |
64345 |
for(ArithVar v = 0, N = d_varDatabases.size(); v < N; ++v){ |
2226 |
56675 |
outputUnateInequalityLemmas(lemmas, v); |
2227 |
|
} |
2228 |
7670 |
} |
2229 |
|
|
2230 |
4057195 |
bool ConstraintDatabase::handleUnateProp(ConstraintP ant, ConstraintP cons){ |
2231 |
4057195 |
if(cons->negationHasProof()){ |
2232 |
|
Debug("arith::unate") << "handleUnate: " << ant << " implies " << cons << endl; |
2233 |
|
cons->impliedByUnate(ant, true); |
2234 |
|
d_raiseConflict.raiseConflict(cons, InferenceId::UNKNOWN); |
2235 |
|
return true; |
2236 |
4057195 |
}else if(!cons->isTrue()){ |
2237 |
2144818 |
++d_statistics.d_unatePropagateImplications; |
2238 |
2144818 |
Debug("arith::unate") << "handleUnate: " << ant << " implies " << cons << endl; |
2239 |
2144818 |
cons->impliedByUnate(ant, false); |
2240 |
2144818 |
cons->tryToPropagate(); |
2241 |
2144818 |
return false; |
2242 |
|
} else { |
2243 |
1912377 |
return false; |
2244 |
|
} |
2245 |
|
} |
2246 |
|
|
2247 |
1043666 |
void ConstraintDatabase::unatePropLowerBound(ConstraintP curr, ConstraintP prev){ |
2248 |
1043666 |
Debug("arith::unate") << "unatePropLowerBound " << curr << " " << prev << endl; |
2249 |
1043666 |
Assert(curr != prev); |
2250 |
1043666 |
Assert(curr != NullConstraint); |
2251 |
1043666 |
bool hasPrev = ! (prev == NullConstraint); |
2252 |
1043666 |
Assert(!hasPrev || curr->getValue() > prev->getValue()); |
2253 |
|
|
2254 |
1043666 |
++d_statistics.d_unatePropagateCalls; |
2255 |
|
|
2256 |
1043666 |
const SortedConstraintMap& scm = curr->constraintSet(); |
2257 |
1043666 |
const SortedConstraintMapConstIterator scm_begin = scm.begin(); |
2258 |
1043666 |
SortedConstraintMapConstIterator scm_i = curr->d_variablePosition; |
2259 |
|
|
2260 |
|
//Ignore the first ValueCollection |
2261 |
|
// NOPE: (>= p c) then (= p c) NOPE |
2262 |
|
// NOPE: (>= p c) then (not (= p c)) NOPE |
2263 |
|
|
2264 |
6084838 |
while(scm_i != scm_begin){ |
2265 |
2736324 |
--scm_i; // move the iterator back |
2266 |
|
|
2267 |
2736324 |
const ValueCollection& vc = scm_i->second; |
2268 |
|
|
2269 |
|
//If it has the previous element, do nothing and stop! |
2270 |
3433750 |
if(hasPrev && |
2271 |
697426 |
vc.hasConstraintOfType(prev->getType()) |
2272 |
3186263 |
&& vc.getConstraintOfType(prev->getType()) == prev){ |
2273 |
215738 |
break; |
2274 |
|
} |
2275 |
|
|
2276 |
|
//Don't worry about implying the negation of upperbound. |
2277 |
|
//These should all be handled by propagating the LowerBounds! |
2278 |
2520586 |
if(vc.hasLowerBound()){ |
2279 |
939591 |
ConstraintP lb = vc.getLowerBound(); |
2280 |
939591 |
if(handleUnateProp(curr, lb)){ return; } |
2281 |
|
} |
2282 |
2520586 |
if(vc.hasDisequality()){ |
2283 |
266679 |
ConstraintP dis = vc.getDisequality(); |
2284 |
266679 |
if(handleUnateProp(curr, dis)){ return; } |
2285 |
|
} |
2286 |
|
} |
2287 |
|
} |
2288 |
|
|
2289 |
931317 |
void ConstraintDatabase::unatePropUpperBound(ConstraintP curr, ConstraintP prev){ |
2290 |
931317 |
Debug("arith::unate") << "unatePropUpperBound " << curr << " " << prev << endl; |
2291 |
931317 |
Assert(curr != prev); |
2292 |
931317 |
Assert(curr != NullConstraint); |
2293 |
931317 |
bool hasPrev = ! (prev == NullConstraint); |
2294 |
931317 |
Assert(!hasPrev || curr->getValue() < prev->getValue()); |
2295 |
|
|
2296 |
931317 |
++d_statistics.d_unatePropagateCalls; |
2297 |
|
|
2298 |
931317 |
const SortedConstraintMap& scm = curr->constraintSet(); |
2299 |
931317 |
const SortedConstraintMapConstIterator scm_end = scm.end(); |
2300 |
931317 |
SortedConstraintMapConstIterator scm_i = curr->d_variablePosition; |
2301 |
931317 |
++scm_i; |
2302 |
6163441 |
for(; scm_i != scm_end; ++scm_i){ |
2303 |
2778114 |
const ValueCollection& vc = scm_i->second; |
2304 |
|
|
2305 |
|
//If it has the previous element, do nothing and stop! |
2306 |
3293789 |
if(hasPrev && |
2307 |
3099423 |
vc.hasConstraintOfType(prev->getType()) && |
2308 |
321309 |
vc.getConstraintOfType(prev->getType()) == prev){ |
2309 |
162052 |
break; |
2310 |
|
} |
2311 |
|
//Don't worry about implying the negation of upperbound. |
2312 |
|
//These should all be handled by propagating the UpperBounds! |
2313 |
2616062 |
if(vc.hasUpperBound()){ |
2314 |
1010394 |
ConstraintP ub = vc.getUpperBound(); |
2315 |
1010394 |
if(handleUnateProp(curr, ub)){ return; } |
2316 |
|
} |
2317 |
2616062 |
if(vc.hasDisequality()){ |
2318 |
217827 |
ConstraintP dis = vc.getDisequality(); |
2319 |
217827 |
if(handleUnateProp(curr, dis)){ return; } |
2320 |
|
} |
2321 |
|
} |
2322 |
|
} |
2323 |
|
|
2324 |
977061 |
void ConstraintDatabase::unatePropEquality(ConstraintP curr, ConstraintP prevLB, ConstraintP prevUB){ |
2325 |
977061 |
Debug("arith::unate") << "unatePropEquality " << curr << " " << prevLB << " " << prevUB << endl; |
2326 |
977061 |
Assert(curr != prevLB); |
2327 |
977061 |
Assert(curr != prevUB); |
2328 |
977061 |
Assert(curr != NullConstraint); |
2329 |
977061 |
bool hasPrevLB = ! (prevLB == NullConstraint); |
2330 |
977061 |
bool hasPrevUB = ! (prevUB == NullConstraint); |
2331 |
977061 |
Assert(!hasPrevLB || curr->getValue() >= prevLB->getValue()); |
2332 |
977061 |
Assert(!hasPrevUB || curr->getValue() <= prevUB->getValue()); |
2333 |
|
|
2334 |
977061 |
++d_statistics.d_unatePropagateCalls; |
2335 |
|
|
2336 |
977061 |
const SortedConstraintMap& scm = curr->constraintSet(); |
2337 |
977061 |
SortedConstraintMapConstIterator scm_curr = curr->d_variablePosition; |
2338 |
977061 |
SortedConstraintMapConstIterator scm_last = hasPrevUB ? prevUB->d_variablePosition : scm.end(); |
2339 |
977061 |
SortedConstraintMapConstIterator scm_i; |
2340 |
977061 |
if(hasPrevLB){ |
2341 |
144720 |
scm_i = prevLB->d_variablePosition; |
2342 |
144720 |
if(scm_i != scm_curr){ // If this does not move this past scm_curr, move it one forward |
2343 |
37942 |
++scm_i; |
2344 |
|
} |
2345 |
|
}else{ |
2346 |
832341 |
scm_i = scm.begin(); |
2347 |
|
} |
2348 |
|
|
2349 |
2398557 |
for(; scm_i != scm_curr; ++scm_i){ |
2350 |
|
// between the previous LB and the curr |
2351 |
710748 |
const ValueCollection& vc = scm_i->second; |
2352 |
|
|
2353 |
|
//Don't worry about implying the negation of upperbound. |
2354 |
|
//These should all be handled by propagating the LowerBounds! |
2355 |
710748 |
if(vc.hasLowerBound()){ |
2356 |
246299 |
ConstraintP lb = vc.getLowerBound(); |
2357 |
246299 |
if(handleUnateProp(curr, lb)){ return; } |
2358 |
|
} |
2359 |
710748 |
if(vc.hasDisequality()){ |
2360 |
248229 |
ConstraintP dis = vc.getDisequality(); |
2361 |
248229 |
if(handleUnateProp(curr, dis)){ return; } |
2362 |
|
} |
2363 |
|
} |
2364 |
977061 |
Assert(scm_i == scm_curr); |
2365 |
977061 |
if(!hasPrevUB || scm_i != scm_last){ |
2366 |
947210 |
++scm_i; |
2367 |
|
} // hasPrevUB implies scm_i != scm_last |
2368 |
|
|
2369 |
4468269 |
for(; scm_i != scm_last; ++scm_i){ |
2370 |
|
// between the curr and the previous UB imply the upperbounds and disequalities. |
2371 |
1745604 |
const ValueCollection& vc = scm_i->second; |
2372 |
|
|
2373 |
|
//Don't worry about implying the negation of upperbound. |
2374 |
|
//These should all be handled by propagating the UpperBounds! |
2375 |
1745604 |
if(vc.hasUpperBound()){ |
2376 |
667919 |
ConstraintP ub = vc.getUpperBound(); |
2377 |
667919 |
if(handleUnateProp(curr, ub)){ return; } |
2378 |
|
} |
2379 |
1745604 |
if(vc.hasDisequality()){ |
2380 |
460257 |
ConstraintP dis = vc.getDisequality(); |
2381 |
460257 |
if(handleUnateProp(curr, dis)){ return; } |
2382 |
|
} |
2383 |
|
} |
2384 |
|
} |
2385 |
|
|
2386 |
1066647 |
std::pair<int, int> Constraint::unateFarkasSigns(ConstraintCP ca, ConstraintCP cb){ |
2387 |
1066647 |
ConstraintType a = ca->getType(); |
2388 |
1066647 |
ConstraintType b = cb->getType(); |
2389 |
|
|
2390 |
1066647 |
Assert(a != Disequality); |
2391 |
1066647 |
Assert(b != Disequality); |
2392 |
|
|
2393 |
1066647 |
int a_sgn = (a == LowerBound) ? -1 : ((a == UpperBound) ? 1 : 0); |
2394 |
1066647 |
int b_sgn = (b == LowerBound) ? -1 : ((b == UpperBound) ? 1 : 0); |
2395 |
|
|
2396 |
1066647 |
if(a_sgn == 0 && b_sgn == 0){ |
2397 |
286236 |
Assert(a == Equality); |
2398 |
286236 |
Assert(b == Equality); |
2399 |
286236 |
Assert(ca->getValue() != cb->getValue()); |
2400 |
572472 |
if(ca->getValue() < cb->getValue()){ |
2401 |
93916 |
a_sgn = 1; |
2402 |
93916 |
b_sgn = -1; |
2403 |
|
}else{ |
2404 |
192320 |
a_sgn = -1; |
2405 |
192320 |
b_sgn = 1; |
2406 |
|
} |
2407 |
780411 |
}else if(a_sgn == 0){ |
2408 |
171031 |
Assert(b_sgn != 0); |
2409 |
171031 |
Assert(a == Equality); |
2410 |
171031 |
a_sgn = -b_sgn; |
2411 |
609380 |
}else if(b_sgn == 0){ |
2412 |
364386 |
Assert(a_sgn != 0); |
2413 |
364386 |
Assert(b == Equality); |
2414 |
364386 |
b_sgn = -a_sgn; |
2415 |
|
} |
2416 |
1066647 |
Assert(a_sgn != 0); |
2417 |
1066647 |
Assert(b_sgn != 0); |
2418 |
|
|
2419 |
2133294 |
Debug("arith::unateFarkasSigns") << "Constraint::unateFarkasSigns("<<a <<", " << b << ") -> " |
2420 |
1066647 |
<< "("<<a_sgn<<", "<< b_sgn <<")"<< endl; |
2421 |
1066647 |
return make_pair(a_sgn, b_sgn); |
2422 |
|
} |
2423 |
|
|
2424 |
|
} // namespace arith |
2425 |
|
} // namespace theory |
2426 |
29502 |
} // namespace cvc5 |