GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/constraint.cpp Lines: 1072 1473 72.8 %
Date: 2021-09-10 Branches: 1801 6403 28.1 %

Line Exec Source
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
540324
ConstraintType Constraint::constraintTypeOfComparison(const Comparison& cmp){
45
540324
  Kind k = cmp.comparisonKind();
46
540324
  switch(k){
47
142254
  case LT:
48
  case LEQ:
49
    {
50
284508
      Polynomial l = cmp.getLeft();
51
142254
      if(l.leadingCoefficientIsPositive()){ // (< x c)
52
117291
        return UpperBound;
53
      }else{
54
24963
        return LowerBound; // (< (-x) c)
55
      }
56
    }
57
143466
  case GT:
58
  case GEQ:
59
    {
60
286932
      Polynomial l = cmp.getLeft();
61
143466
      if(l.leadingCoefficientIsPositive()){
62
118307
        return LowerBound; // (> x c)
63
      }else{
64
25159
        return UpperBound; // (> (-x) c)
65
      }
66
    }
67
127416
  case EQUAL:
68
127416
    return Equality;
69
127188
  case DISTINCT:
70
127188
    return Disequality;
71
  default: Unhandled() << k;
72
  }
73
}
74
75
668114
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
668114
    d_variablePosition()
88
{
89
668114
  Assert(!initialized());
90
668114
}
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
1999508
ValueCollection::ValueCollection()
191
  : d_lowerBound(NullConstraint),
192
    d_upperBound(NullConstraint),
193
    d_equality(NullConstraint),
194
1999508
    d_disequality(NullConstraint)
195
1999508
{}
196
197
19555938
bool ValueCollection::hasLowerBound() const{
198
19555938
  return d_lowerBound != NullConstraint;
199
}
200
201
20767981
bool ValueCollection::hasUpperBound() const{
202
20767981
  return d_upperBound != NullConstraint;
203
}
204
205
4353440
bool ValueCollection::hasEquality() const{
206
4353440
  return d_equality != NullConstraint;
207
}
208
209
13901139
bool ValueCollection::hasDisequality() const {
210
13901139
  return d_disequality != NullConstraint;
211
}
212
213
4073939
ConstraintP ValueCollection::getLowerBound() const {
214
4073939
  Assert(hasLowerBound());
215
4073939
  return d_lowerBound;
216
}
217
218
4076943
ConstraintP ValueCollection::getUpperBound() const {
219
4076943
  Assert(hasUpperBound());
220
4076943
  return d_upperBound;
221
}
222
223
334344
ConstraintP ValueCollection::getEquality() const {
224
334344
  Assert(hasEquality());
225
334344
  return d_equality;
226
}
227
228
1974033
ConstraintP ValueCollection::getDisequality() const {
229
1974033
  Assert(hasDisequality());
230
1974033
  return d_disequality;
231
}
232
233
234
447856
void ValueCollection::push_into(std::vector<ConstraintP>& vec) const {
235
447856
  Debug("arith::constraint") << "push_into " << *this << endl;
236
447856
  if(hasEquality()){
237
131392
    vec.push_back(d_equality);
238
  }
239
447856
  if(hasLowerBound()){
240
201945
    vec.push_back(d_lowerBound);
241
  }
242
447856
  if(hasUpperBound()){
243
201945
    vec.push_back(d_upperBound);
244
  }
245
447856
  if(hasDisequality()){
246
131392
    vec.push_back(d_disequality);
247
  }
248
447856
}
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
3347033
bool ValueCollection::hasConstraintOfType(ConstraintType t) const{
273
3347033
  switch(t){
274
1080338
  case LowerBound:
275
1080338
    return hasLowerBound();
276
1896274
  case UpperBound:
277
1896274
    return hasUpperBound();
278
370421
  case Equality:
279
370421
    return hasEquality();
280
  case Disequality:
281
    return hasDisequality();
282
  default:
283
    Unreachable();
284
  }
285
}
286
287
225443
ArithVar ValueCollection::getVariable() const{
288
225443
  Assert(!empty());
289
225443
  return nonNull()->getVariable();
290
}
291
292
225443
const DeltaRational& ValueCollection::getValue() const{
293
225443
  Assert(!empty());
294
225443
  return nonNull()->getValue();
295
}
296
297
666674
void ValueCollection::add(ConstraintP c){
298
666674
  Assert(c != NullConstraint);
299
300
666674
  Assert(empty() || getVariable() == c->getVariable());
301
666674
  Assert(empty() || getValue() == c->getValue());
302
303
666674
  switch(c->getType()){
304
201945
  case LowerBound:
305
201945
    Assert(!hasLowerBound());
306
201945
    d_lowerBound = c;
307
201945
    break;
308
131392
  case Equality:
309
131392
    Assert(!hasEquality());
310
131392
    d_equality = c;
311
131392
    break;
312
201945
  case UpperBound:
313
201945
    Assert(!hasUpperBound());
314
201945
    d_upperBound = c;
315
201945
    break;
316
131392
  case Disequality:
317
131392
    Assert(!hasDisequality());
318
131392
    d_disequality = c;
319
131392
    break;
320
  default:
321
    Unreachable();
322
  }
323
666674
}
324
325
2542015
ConstraintP ValueCollection::getConstraintOfType(ConstraintType t) const{
326
2542015
  switch(t){
327
690646
    case LowerBound: Assert(hasLowerBound()); return d_lowerBound;
328
239029
    case Equality: Assert(hasEquality()); return d_equality;
329
1612340
    case UpperBound: Assert(hasUpperBound()); return d_upperBound;
330
    case Disequality: Assert(hasDisequality()); return d_disequality;
331
    default: Unreachable();
332
  }
333
}
334
335
666674
void ValueCollection::remove(ConstraintType t){
336
666674
  switch(t){
337
201945
  case LowerBound:
338
201945
    Assert(hasLowerBound());
339
201945
    d_lowerBound = NullConstraint;
340
201945
    break;
341
131392
  case Equality:
342
131392
    Assert(hasEquality());
343
131392
    d_equality = NullConstraint;
344
131392
    break;
345
201945
  case UpperBound:
346
201945
    Assert(hasUpperBound());
347
201945
    d_upperBound = NullConstraint;
348
201945
    break;
349
131392
  case Disequality:
350
131392
    Assert(hasDisequality());
351
131392
    d_disequality = NullConstraint;
352
131392
    break;
353
  default:
354
    Unreachable();
355
  }
356
666674
}
357
358
2450908
bool ValueCollection::empty() const{
359
  return
360
5911296
    !(hasLowerBound() ||
361
4131708
      hasUpperBound() ||
362
2014888
      hasEquality() ||
363
3794476
      hasDisequality());
364
}
365
366
450886
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
450886
  if(hasLowerBound()){
370
132656
    return d_lowerBound;
371
318230
  }else if(hasUpperBound()){
372
48266
    return d_upperBound;
373
269964
  }else if(hasEquality()){
374
269964
    return d_equality;
375
  }else if(hasDisequality()){
376
    return d_disequality;
377
  }else{
378
    return NullConstraint;
379
  }
380
}
381
382
2732827
bool Constraint::initialized() const {
383
2732827
  return d_database != NULL;
384
}
385
386
const ConstraintDatabase& Constraint::getDatabase() const{
387
  Assert(initialized());
388
  return *d_database;
389
}
390
391
666674
void Constraint::initialize(ConstraintDatabase* db, SortedConstraintMapIterator v, ConstraintP negation){
392
666674
  Assert(!initialized());
393
666674
  d_database = db;
394
666674
  d_variablePosition = v;
395
666674
  d_negation = negation;
396
666674
}
397
398
1336228
Constraint::~Constraint() {
399
  // Call this instead of safeToGarbageCollect()
400
668114
  Assert(!contextDependentDataIsSet());
401
402
668114
  if(initialized()){
403
666674
    ValueCollection& vc =  d_variablePosition->second;
404
666674
    Debug("arith::constraint") << "removing" << vc << endl;
405
406
666674
    vc.remove(getType());
407
408
666674
    if(vc.empty()){
409
447856
      Debug("arith::constraint") << "erasing" << vc << endl;
410
447856
      SortedConstraintMap& perVariable = d_database->getVariableSCM(getVariable());
411
447856
      perVariable.erase(d_variablePosition);
412
    }
413
414
666674
    if(hasLiteral()){
415
541764
      d_database->d_nodetoConstraintMap.erase(getLiteral());
416
    }
417
  }
418
668114
}
419
420
25420583
const ConstraintRule& Constraint::getConstraintRule() const {
421
25420583
  Assert(hasProof());
422
25420583
  return d_database->d_watches->d_constraintProofs[d_crid];
423
}
424
425
3004884
const ValueCollection& Constraint::getValueCollection() const{
426
3004884
  return d_variablePosition->second;
427
}
428
429
430
86815
ConstraintP Constraint::getCeiling() {
431
86815
  Debug("getCeiling") << "Constraint_::getCeiling on " << *this << endl;
432
86815
  Assert(getValue().getInfinitesimalPart().sgn() > 0);
433
434
173630
  const DeltaRational ceiling(getValue().ceiling());
435
173630
  return d_database->getConstraint(getVariable(), getType(), ceiling);
436
}
437
438
1190236
ConstraintP Constraint::getFloor() {
439
1190236
  Assert(getValue().getInfinitesimalPart().sgn() < 0);
440
441
2380472
  const DeltaRational floor(Rational(getValue().floor()));
442
2380472
  return d_database->getConstraint(getVariable(), getType(), floor);
443
}
444
445
849758
void Constraint::setCanBePropagated() {
446
849758
  Assert(!canBePropagated());
447
849758
  d_database->pushCanBePropagatedWatch(this);
448
849758
}
449
450
5611510
void Constraint::setAssertedToTheTheory(TNode witness, bool nowInConflict) {
451
5611510
  Assert(hasLiteral());
452
5611510
  Assert(!assertedToTheTheory());
453
5611510
  Assert(negationHasProof() == nowInConflict);
454
5611510
  d_database->pushAssertionOrderWatch(this, witness);
455
456
5611510
  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
5611510
}
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
10770047
bool Constraint::isInternalAssumption() const {
480
10770047
  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
11283373
bool Constraint::isAssumption() const {
507
11283373
  return getProofType() == AssumeAP;
508
}
509
510
497064
bool Constraint::hasEqualityEngineProof() const {
511
497064
  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
541764
bool Constraint::sanityChecking(Node n) const {
622
1083528
  Comparison cmp = Comparison::parseNormalForm(n);
623
541764
  Kind k = cmp.comparisonKind();
624
1083528
  Polynomial pleft = cmp.normalizedVariablePart();
625
541764
  Assert(k == EQUAL || k == DISTINCT || pleft.leadingCoefficientIsPositive());
626
541764
  Assert(k != EQUAL || Monomial::isMember(n[0]));
627
541764
  Assert(k != DISTINCT || Monomial::isMember(n[0][0]));
628
629
1083528
  TNode left = pleft.getNode();
630
1083528
  DeltaRational right = cmp.normalizedDeltaRational();
631
632
541764
  const ArithVariables& avariables = d_database->getArithVariables();
633
634
541764
  Debug("Constraint::sanityChecking") << cmp.getNode() << endl;
635
541764
  Debug("Constraint::sanityChecking") << k << endl;
636
541764
  Debug("Constraint::sanityChecking") << pleft.getNode() << endl;
637
541764
  Debug("Constraint::sanityChecking") << left << endl;
638
541764
  Debug("Constraint::sanityChecking") << right << endl;
639
541764
  Debug("Constraint::sanityChecking") << getValue() << endl;
640
541764
  Debug("Constraint::sanityChecking") << avariables.hasArithVar(left) << endl;
641
541764
  Debug("Constraint::sanityChecking") << avariables.asArithVar(left) << endl;
642
541764
  Debug("Constraint::sanityChecking") << getVariable() << endl;
643
644
645
2708820
  if(avariables.hasArithVar(left) &&
646
2708820
     avariables.asArithVar(left) == getVariable() &&
647
541764
     getValue() == right){
648
541764
    switch(getType()){
649
286932
    case LowerBound:
650
    case UpperBound:
651
      //Be overapproximate
652
286932
      return k == GT || k == GEQ ||k == LT || k == LEQ;
653
127416
    case Equality:
654
127416
      return k == EQUAL;
655
127416
    case Disequality:
656
127416
      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
2197321
bool Constraint::wellFormedFarkasProof() const {
715
2197321
  Assert(hasProof());
716
717
2197321
  const ConstraintRule& cr = getConstraintRule();
718
2197321
  if(cr.d_constraint != this){ return false; }
719
2197321
  if(cr.d_proofType != FarkasAP){ return false; }
720
721
2197321
  AntecedentId p = cr.d_antecedentEnd;
722
723
  // must have at least one antecedent
724
2197321
  ConstraintCP antecedent = d_database->d_antecedents[p];
725
2197321
  if(antecedent  == NullConstraint) { return false; }
726
727
2197321
  if (!ARITH_PROOF_ON())
728
  {
729
1360767
    return cr.d_farkasCoefficients == RationalVectorCPSentinel;
730
  }
731
836554
  Assert(ARITH_PROOF_ON());
732
733
836554
  if(cr.d_farkasCoefficients == RationalVectorCPSentinel){ return false; }
734
836554
  if(cr.d_farkasCoefficients->size() < 2){ return false; }
735
736
836554
  const ArithVariables& vars = d_database->getArithVariables();
737
738
1673108
  DeltaRational rhs(0);
739
1673108
  Node lhs = Polynomial::mkZero().getNode();
740
741
836554
  RationalVector::const_iterator coeffIterator = cr.d_farkasCoefficients->end()-1;
742
836554
  RationalVector::const_iterator coeffBegin = cr.d_farkasCoefficients->begin();
743
744
3243840
  while(antecedent != NullConstraint){
745
1203643
    Assert(lhs.isNull() || Polynomial::isMember(lhs));
746
747
1203643
    const Rational& coeff = *coeffIterator;
748
1203643
    int coeffSgn = coeff.sgn();
749
750
1203643
    rhs += antecedent->getValue() * coeff;
751
752
1203643
    ArithVar antVar = antecedent->getVariable();
753
1203643
    if(!lhs.isNull() && vars.hasNode(antVar)){
754
2407286
      Node antAsNode = vars.asNode(antVar);
755
1203643
      if(Polynomial::isMember(antAsNode)){
756
2407286
        Polynomial lhsPoly = Polynomial::parsePolynomial(lhs);
757
2407286
        Polynomial antPoly = Polynomial::parsePolynomial(antAsNode);
758
2407286
        Polynomial sum = lhsPoly + (antPoly * coeff);
759
1203643
        lhs = sum.getNode();
760
      }else{
761
        lhs = Node::null();
762
      }
763
    } else {
764
      lhs = Node::null();
765
    }
766
1203643
    Debug("constraints::wffp") << "running sum: " << lhs << " <= " << rhs << endl;
767
768
1203643
    switch( antecedent->getType() ){
769
363798
    case LowerBound:
770
      // fc[l] < 0, therefore return false if coeffSgn >= 0
771
363798
      if(coeffSgn >= 0){ return false; }
772
363798
      break;
773
212913
    case UpperBound:
774
      // fc[u] > 0, therefore return false if coeffSgn <= 0
775
212913
      if(coeffSgn <= 0){ return false; }
776
212913
      break;
777
626932
    case Equality:
778
626932
      if(coeffSgn == 0) { return false; }
779
626932
      break;
780
    case Disequality:
781
    default:
782
      return false;
783
    }
784
785
1203643
    if(coeffIterator == coeffBegin){ return false; }
786
1203643
    --coeffIterator;
787
1203643
    --p;
788
1203643
    antecedent = d_database->d_antecedents[p];
789
  }
790
836554
  if(coeffIterator != coeffBegin){ return false; }
791
792
836554
  const Rational& firstCoeff = (*coeffBegin);
793
836554
  int firstCoeffSgn = firstCoeff.sgn();
794
836554
  rhs += (getNegation()->getValue()) * firstCoeff;
795
836554
  if(!lhs.isNull() && vars.hasNode(getVariable())){
796
1673108
    Node firstAsNode = vars.asNode(getVariable());
797
836554
    if(Polynomial::isMember(firstAsNode)){
798
1673108
      Polynomial lhsPoly = Polynomial::parsePolynomial(lhs);
799
1673108
      Polynomial firstPoly = Polynomial::parsePolynomial(firstAsNode);
800
1673108
      Polynomial sum = lhsPoly + (firstPoly * firstCoeff);
801
836554
      lhs = sum.getNode();
802
    }else{
803
      lhs = Node::null();
804
    }
805
  }else{
806
    lhs = Node::null();
807
  }
808
809
836554
  switch( getNegation()->getType() ){
810
246272
  case LowerBound:
811
    // fc[l] < 0, therefore return false if coeffSgn >= 0
812
246272
    if(firstCoeffSgn >= 0){ return false; }
813
246272
    break;
814
257247
  case UpperBound:
815
    // fc[u] > 0, therefore return false if coeffSgn <= 0
816
257247
    if(firstCoeffSgn <= 0){ return false; }
817
257247
    break;
818
333035
  case Equality:
819
333035
    if(firstCoeffSgn == 0) { return false; }
820
333035
    break;
821
  case Disequality:
822
  default:
823
    return false;
824
  }
825
836554
  Debug("constraints::wffp") << "final sum: " << lhs << " <= " << rhs << endl;
826
  // 0 = lhs <= rhs < 0
827
2509662
  return (lhs.isNull() || (Constant::isMember(lhs) && Constant(lhs).isZero()))
828
2509662
         && rhs.sgn() < 0;
829
}
830
831
63895
ConstraintP Constraint::makeNegation(ArithVar v, ConstraintType t, const DeltaRational& r){
832
63895
  switch(t){
833
3284
  case LowerBound:
834
    {
835
3284
      Assert(r.infinitesimalSgn() >= 0);
836
3284
      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
3284
        Assert(r.infinitesimalSgn() == 0);
843
        // make (not (v >= r)), which is (v < r)
844
6568
        DeltaRational addInf(r.getNoninfinitesimalPart(), -1);
845
3284
        return new Constraint(v, UpperBound, addInf);
846
      }
847
    }
848
56407
  case UpperBound:
849
    {
850
56407
      Assert(r.infinitesimalSgn() <= 0);
851
56407
      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
56407
        Assert(r.infinitesimalSgn() == 0);
858
        // make (not (v <= r)), which is (v > r)
859
112814
        DeltaRational addInf(r.getNoninfinitesimalPart(), 1);
860
56407
        return new Constraint(v, LowerBound, addInf);
861
      }
862
    }
863
4204
  case Equality:
864
4204
    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
9913
ConstraintDatabase::ConstraintDatabase(context::Context* satContext,
874
                                       context::Context* userContext,
875
                                       const ArithVariables& avars,
876
                                       ArithCongruenceManager& cm,
877
                                       RaiseConflict raiseConflict,
878
                                       EagerProofGenerator* pfGen,
879
9913
                                       ProofNodeManager* pnm)
880
    : d_varDatabases(),
881
      d_toPropagate(satContext),
882
      d_antecedents(satContext, false),
883
9913
      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
19826
      d_negOne(-1)
892
{
893
9913
}
894
895
11141864
SortedConstraintMap& ConstraintDatabase::getVariableSCM(ArithVar v) const{
896
11141864
  Assert(variableDatabaseIsSetup(v));
897
11141864
  return d_varDatabases[v]->d_constraints;
898
}
899
900
31694
void ConstraintDatabase::pushSplitWatch(ConstraintP c){
901
31694
  Assert(!c->d_split);
902
31694
  c->d_split = true;
903
31694
  d_watches->d_splitWatches.push_back(c);
904
31694
}
905
906
907
849758
void ConstraintDatabase::pushCanBePropagatedWatch(ConstraintP c){
908
849758
  Assert(!c->d_canBePropagated);
909
849758
  c->d_canBePropagated = true;
910
849758
  d_watches->d_canBePropagatedWatches.push_back(c);
911
849758
}
912
913
5611510
void ConstraintDatabase::pushAssertionOrderWatch(ConstraintP c, TNode witness){
914
5611510
  Assert(!c->assertedToTheTheory());
915
5611510
  c->d_assertionOrder = d_watches->d_assertionOrderWatches.size();
916
5611510
  c->d_witness = witness;
917
5611510
  d_watches->d_assertionOrderWatches.push_back(c);
918
5611510
}
919
920
921
8482306
void ConstraintDatabase::pushConstraintRule(const ConstraintRule& crp){
922
8482306
  ConstraintP c = crp.d_constraint;
923
8482306
  Assert(c->d_crid == ConstraintRuleIdSentinel);
924
8482306
  Assert(!c->hasProof());
925
8482306
  c->d_crid = d_watches->d_constraintProofs.size();
926
8482306
  d_watches->d_constraintProofs.push_back(crp);
927
8482306
}
928
929
1526681
ConstraintP ConstraintDatabase::getConstraint(ArithVar v, ConstraintType t, const DeltaRational& r){
930
  //This must always return a constraint.
931
932
1526681
  SortedConstraintMap& scm = getVariableSCM(v);
933
1526681
  pair<SortedConstraintMapIterator, bool> insertAttempt;
934
1526681
  insertAttempt = scm.insert(make_pair(r, ValueCollection()));
935
936
1526681
  SortedConstraintMapIterator pos = insertAttempt.first;
937
1526681
  ValueCollection& vc = pos->second;
938
1526681
  if(vc.hasConstraintOfType(t)){
939
1462786
    return vc.getConstraintOfType(t);
940
  }else{
941
63895
    ConstraintP c = new Constraint(v, t, r);
942
63895
    ConstraintP negC = Constraint::makeNegation(v, t, r);
943
944
63895
    SortedConstraintMapIterator negPos;
945
63895
    if(t == Equality || t == Disequality){
946
4204
      negPos = pos;
947
    }else{
948
59691
      pair<SortedConstraintMapIterator, bool> negInsertAttempt;
949
59691
      negInsertAttempt = scm.insert(make_pair(negC->getValue(), ValueCollection()));
950
59691
      Assert(negInsertAttempt.second
951
             || !negInsertAttempt.first->second.hasConstraintOfType(
952
                 negC->getType()));
953
59691
      negPos = negInsertAttempt.first;
954
    }
955
956
63895
    c->initialize(this, pos, negC);
957
63895
    negC->initialize(this, negPos, c);
958
959
63895
    vc.add(c);
960
63895
    negPos->second.add(negC);
961
962
63895
    return c;
963
  }
964
}
965
966
275752
ConstraintP ConstraintDatabase::ensureConstraint(ValueCollection& vc, ConstraintType t){
967
275752
  if(vc.hasConstraintOfType(t)){
968
269127
    return vc.getConstraintOfType(t);
969
  }else{
970
6625
    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
19820
ConstraintDatabase::~ConstraintDatabase(){
981
9910
  delete d_watches;
982
983
19820
  std::vector<ConstraintP> constraintList;
984
985
340144
  while(!d_varDatabases.empty()){
986
165117
    PerVariableDatabase* back = d_varDatabases.back();
987
988
165117
    SortedConstraintMap& scm = back->d_constraints;
989
165117
    SortedConstraintMapIterator i = scm.begin(), i_end = scm.end();
990
1060829
    for(; i != i_end; ++i){
991
447856
      (i->second).push_into(constraintList);
992
    }
993
1498465
    while(!constraintList.empty()){
994
666674
      ConstraintP c = constraintList.back();
995
666674
      constraintList.pop_back();
996
666674
      delete c;
997
    }
998
165117
    Assert(scm.empty());
999
165117
    d_varDatabases.pop_back();
1000
165117
    delete back;
1001
  }
1002
1003
9910
  Assert(d_nodetoConstraintMap.empty());
1004
9910
}
1005
1006
9913
ConstraintDatabase::Statistics::Statistics()
1007
9913
    : d_unatePropagateCalls(smtStatisticsRegistry().registerInt(
1008
19826
        "theory::arith::cd::unatePropagateCalls")),
1009
9913
      d_unatePropagateImplications(smtStatisticsRegistry().registerInt(
1010
19826
          "theory::arith::cd::unatePropagateImplications"))
1011
{
1012
9913
}
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
165117
void ConstraintDatabase::addVariable(ArithVar v){
1023
165117
  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
165117
    Debug("arith::constraint") << "about to fail" << v << " " << d_varDatabases.size() << endl;
1042
165117
    Assert(v == d_varDatabases.size());
1043
165117
    d_varDatabases.push_back(new PerVariableDatabase(v));
1044
  }
1045
165117
}
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
668114
bool Constraint::contextDependentDataIsSet() const{
1059
668114
  return hasProof() || isSplit() || canBePropagated() || assertedToTheTheory();
1060
}
1061
1062
15847
TrustNode Constraint::split()
1063
{
1064
15847
  Assert(isEquality() || isDisequality());
1065
1066
15847
  bool isEq = isEquality();
1067
1068
15847
  ConstraintP eq = isEq ? this : d_negation;
1069
15847
  ConstraintP diseq = isEq ? d_negation : this;
1070
1071
31694
  TNode eqNode = eq->getLiteral();
1072
15847
  Assert(eqNode.getKind() == kind::EQUAL);
1073
31694
  TNode lhs = eqNode[0];
1074
31694
  TNode rhs = eqNode[1];
1075
1076
31694
  Node leqNode = NodeBuilder(kind::LEQ) << lhs << rhs;
1077
31694
  Node ltNode = NodeBuilder(kind::LT) << lhs << rhs;
1078
31694
  Node gtNode = NodeBuilder(kind::GT) << lhs << rhs;
1079
31694
  Node geqNode = NodeBuilder(kind::GEQ) << lhs << rhs;
1080
1081
31694
  Node lemma = NodeBuilder(OR) << leqNode << geqNode;
1082
1083
15847
  TrustNode trustedLemma;
1084
15847
  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
13402
    trustedLemma = TrustNode::mkTrustLemma(lemma);
1113
  }
1114
1115
15847
  eq->d_database->pushSplitWatch(eq);
1116
15847
  diseq->d_database->pushSplitWatch(diseq);
1117
1118
31694
  return trustedLemma;
1119
}
1120
1121
1083529
bool ConstraintDatabase::hasLiteral(TNode literal) const {
1122
1083529
  return lookup(literal) != NullConstraint;
1123
}
1124
1125
270882
ConstraintP ConstraintDatabase::addLiteral(TNode literal){
1126
270882
  Assert(!hasLiteral(literal));
1127
270882
  bool isNot = (literal.getKind() == NOT);
1128
541764
  Node atomNode = (isNot ? literal[0] : literal);
1129
541764
  Node negationNode  = atomNode.notNode();
1130
1131
270882
  Assert(!hasLiteral(atomNode));
1132
270882
  Assert(!hasLiteral(negationNode));
1133
541764
  Comparison posCmp = Comparison::parseNormalForm(atomNode);
1134
1135
270882
  ConstraintType posType = Constraint::constraintTypeOfComparison(posCmp);
1136
1137
541764
  Polynomial nvp = posCmp.normalizedVariablePart();
1138
270882
  ArithVar v = d_avariables.asArithVar(nvp.getNode());
1139
1140
541764
  DeltaRational posDR = posCmp.normalizedDeltaRational();
1141
1142
270882
  ConstraintP posC = new Constraint(v, posType, posDR);
1143
1144
270882
  Debug("arith::constraint") << "addliteral( literal ->" << literal << ")" << endl;
1145
270882
  Debug("arith::constraint") << "addliteral( posC ->" << posC << ")" << endl;
1146
1147
270882
  SortedConstraintMap& scm = getVariableSCM(posC->getVariable());
1148
270882
  pair<SortedConstraintMapIterator, bool> insertAttempt;
1149
270882
  insertAttempt = scm.insert(make_pair(posC->getValue(), ValueCollection()));
1150
1151
270882
  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
270882
  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
1440
    ConstraintP hit = posI->second.getConstraintOfType(posC->getType());
1159
1440
    Debug("arith::constraint") << "hit " << hit << endl;
1160
1440
    Debug("arith::constraint") << "posC " << posC << endl;
1161
1162
1440
    delete posC;
1163
1164
1440
    hit->setLiteral(atomNode);
1165
1440
    hit->getNegation()->setLiteral(negationNode);
1166
1440
    return isNot ? hit->getNegation(): hit;
1167
  }else{
1168
538884
    Comparison negCmp = Comparison::parseNormalForm(negationNode);
1169
1170
269442
    ConstraintType negType = Constraint::constraintTypeOfComparison(negCmp);
1171
538884
    DeltaRational negDR = negCmp.normalizedDeltaRational();
1172
1173
269442
    ConstraintP negC = new Constraint(v, negType, negDR);
1174
1175
269442
    SortedConstraintMapIterator negI;
1176
1177
269442
    if(posC->isEquality()){
1178
127188
      negI = posI;
1179
    }else{
1180
142254
      Assert(posC->isLowerBound() || posC->isUpperBound());
1181
1182
142254
      pair<SortedConstraintMapIterator, bool> negInsertAttempt;
1183
142254
      negInsertAttempt = scm.insert(make_pair(negC->getValue(), ValueCollection()));
1184
1185
142254
      Debug("nf::tmp") << "sdhjfgdhjkldfgljkhdfg" << endl;
1186
142254
      Debug("nf::tmp") << negC << endl;
1187
142254
      Debug("nf::tmp") << negC->getValue() << endl;
1188
1189
      //This should always succeed as the DeltaRational for the negation is unique!
1190
142254
      Assert(negInsertAttempt.second);
1191
1192
142254
      negI = negInsertAttempt.first;
1193
    }
1194
1195
269442
    (posI->second).add(posC);
1196
269442
    (negI->second).add(negC);
1197
1198
269442
    posC->initialize(this, posI, negC);
1199
269442
    negC->initialize(this, negI, posC);
1200
1201
269442
    posC->setLiteral(atomNode);
1202
269442
    negC->setLiteral(negationNode);
1203
1204
269442
    return isNot ? negC : posC;
1205
  }
1206
}
1207
1208
1209
9816517
ConstraintP ConstraintDatabase::lookup(TNode literal) const{
1210
9816517
  NodetoConstraintMap::const_iterator iter = d_nodetoConstraintMap.find(literal);
1211
9816517
  if(iter == d_nodetoConstraintMap.end()){
1212
1891995
    return NullConstraint;
1213
  }else{
1214
7924522
    return iter->second;
1215
  }
1216
}
1217
1218
4605355
void Constraint::setAssumption(bool nowInConflict){
1219
4605355
  Debug("constraints::pf") << "setAssumption(" << this << ")" << std::endl;
1220
4605355
  Assert(!hasProof());
1221
4605355
  Assert(negationHasProof() == nowInConflict);
1222
4605355
  Assert(hasLiteral());
1223
4605355
  Assert(assertedToTheTheory());
1224
1225
4605355
  d_database->pushConstraintRule(ConstraintRule(this, AssumeAP));
1226
1227
4605355
  Assert(inConflict() == nowInConflict);
1228
4605355
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1229
    Debug("constraint::conflictCommit") << "inConflict@setAssumption " << this << std::endl;
1230
  }
1231
4605355
}
1232
1233
3588679
void Constraint::tryToPropagate(){
1234
3588679
  Assert(hasProof());
1235
3588679
  Assert(!isAssumption());
1236
3588679
  Assert(!isInternalAssumption());
1237
1238
3588679
  if(canBePropagated() && !assertedToTheTheory() && !isAssumption() && !isInternalAssumption()){
1239
800048
    propagate();
1240
  }
1241
3588679
}
1242
1243
814449
void Constraint::propagate(){
1244
814449
  Assert(hasProof());
1245
814449
  Assert(canBePropagated());
1246
814449
  Assert(!assertedToTheTheory());
1247
814449
  Assert(!isAssumption());
1248
814449
  Assert(!isInternalAssumption());
1249
1250
814449
  d_database->d_toPropagate.push(this);
1251
814449
}
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
2094567
void Constraint::impliedByUnate(ConstraintCP imp, bool nowInConflict){
1262
2094567
  Debug("constraints::pf") << "impliedByUnate(" << this << ", " << *imp << ")" << std::endl;
1263
2094567
  Assert(!hasProof());
1264
2094567
  Assert(imp->hasProof());
1265
2094567
  Assert(negationHasProof() == nowInConflict);
1266
1267
2094567
  d_database->d_antecedents.push_back(NullConstraint);
1268
2094567
  d_database->d_antecedents.push_back(imp);
1269
1270
2094567
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1271
1272
  RationalVectorP coeffs;
1273
2094567
  if (ARITH_PROOF_ON())
1274
  {
1275
790608
    std::pair<int, int> sgns = unateFarkasSigns(getNegation(), imp);
1276
1277
1581216
    Rational first(sgns.first);
1278
1581216
    Rational second(sgns.second);
1279
1280
790608
    coeffs = new RationalVector();
1281
790608
    coeffs->push_back(first);
1282
790608
    coeffs->push_back(second);
1283
  }
1284
  else
1285
  {
1286
1303959
    coeffs = RationalVectorPSentinel;
1287
  }
1288
  // no need to delete coeffs the memory is owned by ConstraintRule
1289
2094567
  d_database->pushConstraintRule(ConstraintRule(this, FarkasAP, antecedentEnd, coeffs));
1290
1291
2094567
  Assert(inConflict() == nowInConflict);
1292
2094567
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1293
    Debug("constraint::conflictCommit") << "inConflict@impliedByUnate " << this << std::endl;
1294
  }
1295
1296
2094567
  if(Debug.isOn("constraints::wffp") && !wellFormedFarkasProof()){
1297
    getConstraintRule().print(Debug("constraints::wffp"));
1298
  }
1299
2094567
  Assert(wellFormedFarkasProof());
1300
2094567
}
1301
1302
411959
void Constraint::impliedByTrichotomy(ConstraintCP a, ConstraintCP b, bool nowInConflict){
1303
411959
  Debug("constraints::pf") << "impliedByTrichotomy(" << this << ", " << *a << ", ";
1304
411959
  Debug("constraints::pf") << *b << ")" << std::endl;
1305
411959
  Assert(!hasProof());
1306
411959
  Assert(negationHasProof() == nowInConflict);
1307
411959
  Assert(a->hasProof());
1308
411959
  Assert(b->hasProof());
1309
1310
411959
  d_database->d_antecedents.push_back(NullConstraint);
1311
411959
  d_database->d_antecedents.push_back(a);
1312
411959
  d_database->d_antecedents.push_back(b);
1313
1314
411959
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1315
411959
  d_database->pushConstraintRule(ConstraintRule(this, TrichotomyAP, antecedentEnd));
1316
1317
411959
  Assert(inConflict() == nowInConflict);
1318
411959
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1319
    Debug("constraint::conflictCommit") << "inConflict@impliedByTrichotomy " << this << std::endl;
1320
  }
1321
411959
}
1322
1323
1324
102754
bool Constraint::allHaveProof(const ConstraintCPVec& b){
1325
1480967
  for(ConstraintCPVec::const_iterator i=b.begin(), i_end=b.end(); i != i_end; ++i){
1326
1378213
    ConstraintCP cp = *i;
1327
1378213
    if(! (cp->hasProof())){ return false; }
1328
  }
1329
102754
  return true;
1330
}
1331
1332
1052859
void Constraint::impliedByIntTighten(ConstraintCP a, bool nowInConflict){
1333
1052859
  Debug("constraints::pf") << "impliedByIntTighten(" << this << ", " << *a << ")" << std::endl;
1334
1052859
  Assert(!hasProof());
1335
1052859
  Assert(negationHasProof() == nowInConflict);
1336
1052859
  Assert(a->hasProof());
1337
2105718
  Debug("pf::arith") << "impliedByIntTighten(" << this << ", " << a << ")"
1338
1052859
                     << std::endl;
1339
1340
1052859
  d_database->d_antecedents.push_back(NullConstraint);
1341
1052859
  d_database->d_antecedents.push_back(a);
1342
1052859
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1343
1052859
  d_database->pushConstraintRule(ConstraintRule(this, IntTightenAP, antecedentEnd));
1344
1345
1052859
  Assert(inConflict() == nowInConflict);
1346
1052859
  if(inConflict()){
1347
2398
    Debug("constraint::conflictCommit") << "inConflict impliedByIntTighten" << this << std::endl;
1348
  }
1349
1052859
}
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
102754
void Constraint::impliedByFarkas(const ConstraintCPVec& a, RationalVectorCP coeffs, bool nowInConflict){
1408
102754
  Debug("constraints::pf") << "impliedByFarkas(" << this;
1409
102754
  if (Debug.isOn("constraints::pf")) {
1410
    for (const ConstraintCP& p : a)
1411
    {
1412
      Debug("constraints::pf") << ", " << p;
1413
    }
1414
  }
1415
102754
  Debug("constraints::pf") << ", <coeffs>";
1416
102754
  Debug("constraints::pf") << ")" << std::endl;
1417
102754
  Assert(!hasProof());
1418
102754
  Assert(negationHasProof() == nowInConflict);
1419
102754
  Assert(allHaveProof(a));
1420
1421
102754
  Assert(ARITH_PROOF_ON() == (coeffs != RationalVectorCPSentinel));
1422
102754
  Assert(!ARITH_PROOF_ON() || coeffs->size() == a.size() + 1);
1423
1424
102754
  Assert(a.size() >= 1);
1425
1426
102754
  d_database->d_antecedents.push_back(NullConstraint);
1427
1480967
  for(ConstraintCPVec::const_iterator i = a.begin(), end = a.end(); i != end; ++i){
1428
1378213
    ConstraintCP c_i = *i;
1429
1378213
    Assert(c_i->hasProof());
1430
1378213
    d_database->d_antecedents.push_back(c_i);
1431
  }
1432
102754
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1433
1434
  RationalVectorCP coeffsCopy;
1435
102754
  if (ARITH_PROOF_ON())
1436
  {
1437
45946
    Assert(coeffs != RationalVectorCPSentinel);
1438
45946
    coeffsCopy = new RationalVector(*coeffs);
1439
  }
1440
  else
1441
  {
1442
56808
    coeffsCopy = RationalVectorCPSentinel;
1443
  }
1444
102754
  d_database->pushConstraintRule(ConstraintRule(this, FarkasAP, antecedentEnd, coeffsCopy));
1445
1446
102754
  Assert(inConflict() == nowInConflict);
1447
102754
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1448
    Debug("constraint::conflictCommit") << "inConflict@impliedByFarkas " << this << std::endl;
1449
  }
1450
102754
  if(Debug.isOn("constraints::wffp") && !wellFormedFarkasProof()){
1451
    getConstraintRule().print(Debug("constraints::wffp"));
1452
  }
1453
102754
  Assert(wellFormedFarkasProof());
1454
102754
}
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
214812
void Constraint::setEqualityEngineProof(){
1474
214812
  Debug("constraints::pf") << "setEqualityEngineProof(" << this;
1475
214812
  Debug("constraints::pf") << ")" << std::endl;
1476
214812
  Assert(truthIsUnknown());
1477
214812
  Assert(hasLiteral());
1478
214812
  d_database->pushConstraintRule(ConstraintRule(this, EqualityEngineAP));
1479
214812
}
1480
1481
1482
4110806
SortedConstraintMap& Constraint::constraintSet() const{
1483
4110806
  Assert(d_database->variableDatabaseIsSetup(d_variable));
1484
4110806
  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
110746
Node Constraint::externalImplication(const ConstraintCPVec& b) const{
1499
110746
  Assert(hasLiteral());
1500
221492
  Node antecedent = externalExplainByAssertions(b);
1501
221492
  Node implied = getLiteral();
1502
221492
  return antecedent.impNode(implied);
1503
}
1504
1505
1506
1137985
Node Constraint::externalExplainByAssertions(const ConstraintCPVec& b){
1507
1137985
  return externalExplain(b, AssertionOrderSentinel);
1508
}
1509
1510
15939
TrustNode Constraint::externalExplainForPropagation(TNode lit) const
1511
{
1512
15939
  Assert(hasProof());
1513
15939
  Assert(!isAssumption());
1514
15939
  Assert(!isInternalAssumption());
1515
31878
  NodeBuilder nb(Kind::AND);
1516
31878
  auto pfFromAssumptions = externalExplain(nb, d_assertionOrder);
1517
31878
  Node n = safeConstructNary(nb);
1518
15939
  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
2308
    Assert(Rewriter::rewrite(lit) == getLiteral());
1523
4616
    std::vector<Node> assumptions;
1524
2308
    if (n.getKind() == Kind::AND)
1525
    {
1526
1402
      assumptions.insert(assumptions.end(), n.begin(), n.end());
1527
    }
1528
    else
1529
    {
1530
906
      assumptions.push_back(n);
1531
    }
1532
2308
    if (getProofLiteral() != lit)
1533
    {
1534
4497
      pfFromAssumptions = d_database->d_pnm->mkNode(
1535
2998
          PfRule::MACRO_SR_PRED_TRANSFORM, {pfFromAssumptions}, {lit});
1536
    }
1537
4616
    auto pf = d_database->d_pnm->mkScope(pfFromAssumptions, assumptions);
1538
2308
    return d_database->d_pfGen->mkTrustedPropagation(
1539
2308
        lit, safeConstructNary(Kind::AND, assumptions), pf);
1540
  }
1541
  else
1542
  {
1543
13631
    return TrustNode::mkTrustPropExp(lit, n);
1544
  }
1545
}
1546
1547
78919
TrustNode Constraint::externalExplainConflict() const
1548
{
1549
78919
  Debug("pf::arith::explain") << this << std::endl;
1550
78919
  Assert(inConflict());
1551
157838
  NodeBuilder nb(kind::AND);
1552
157838
  auto pf1 = externalExplainByAssertions(nb);
1553
157838
  auto not2 = getNegation()->getProofLiteral().negate();
1554
157838
  auto pf2 = getNegation()->externalExplainByAssertions(nb);
1555
157838
  Node n = safeConstructNary(nb);
1556
78919
  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
65892
    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
1137985
Node Constraint::externalExplain(const ConstraintCPVec& v, AssertionOrder order){
1648
2275970
  NodeBuilder nb(kind::AND);
1649
1137985
  ConstraintCPVec::const_iterator i, end;
1650
2556528
  for(i = v.begin(), end = v.end(); i != end; ++i){
1651
1418543
    ConstraintCP v_i = *i;
1652
1418543
    v_i->externalExplain(nb, order);
1653
  }
1654
2275970
  return safeConstructNary(nb);
1655
}
1656
1657
5550932
std::shared_ptr<ProofNode> Constraint::externalExplain(
1658
    NodeBuilder& nb, AssertionOrder order) const
1659
{
1660
5550932
  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
5550932
  Assert(hasProof());
1668
5550932
  Assert(!isAssumption() || assertedToTheTheory());
1669
5550932
  Assert(!isInternalAssumption());
1670
5550932
  std::shared_ptr<ProofNode> pf{};
1671
1672
5550932
  ProofNodeManager* pnm = d_database->d_pnm;
1673
1674
5550932
  if (assertedBefore(order))
1675
  {
1676
5053868
    Debug("pf::arith::explain") << "  already asserted" << std::endl;
1677
5053868
    nb << getWitness();
1678
5053868
    if (d_database->isProofEnabled())
1679
    {
1680
680245
      pf = pnm->mkAssume(getWitness());
1681
      // If the witness and literal differ, prove the difference through a
1682
      // rewrite.
1683
680245
      if (getWitness() != getProofLiteral())
1684
      {
1685
1348632
        pf = pnm->mkNode(
1686
899088
            PfRule::MACRO_SR_PRED_TRANSFORM, {pf}, {getProofLiteral()});
1687
      }
1688
    }
1689
  }
1690
497064
  else if (hasEqualityEngineProof())
1691
  {
1692
6291
    Debug("pf::arith::explain") << "  going to ee:" << std::endl;
1693
12582
    TrustNode exp = d_database->eeExplain(this);
1694
6291
    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
12582
    Debug("pf::arith::explain")
1716
6291
        << "    explanation: " << exp.getNode() << std::endl;
1717
6291
    if (exp.getNode().getKind() == Kind::AND)
1718
    {
1719
5580
      nb.append(exp.getNode().begin(), exp.getNode().end());
1720
    }
1721
    else
1722
    {
1723
711
      nb << exp.getNode();
1724
    }
1725
  }
1726
  else
1727
  {
1728
490773
    Debug("pf::arith::explain") << "  recursion!" << std::endl;
1729
490773
    Assert(!isAssumption());
1730
490773
    AntecedentId p = getEndAntecedent();
1731
490773
    ConstraintCP antecedent = d_database->d_antecedents[p];
1732
981546
    std::vector<std::shared_ptr<ProofNode>> children;
1733
1734
3276109
    while (antecedent != NullConstraint)
1735
    {
1736
1392668
      Debug("pf::arith::explain") << "Explain " << antecedent << std::endl;
1737
2785336
      auto pn = antecedent->externalExplain(nb, order);
1738
1392668
      if (d_database->isProofEnabled())
1739
      {
1740
153419
        children.push_back(pn);
1741
      }
1742
1392668
      --p;
1743
1392668
      antecedent = d_database->d_antecedents[p];
1744
    }
1745
1746
490773
    if (d_database->isProofEnabled())
1747
    {
1748
89562
      switch (getProofType())
1749
      {
1750
        case ArithProofType::AssumeAP:
1751
        case ArithProofType::EqualityEngineAP:
1752
        {
1753
          Unreachable() << "These should be handled above";
1754
          break;
1755
        }
1756
13524
        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
27048
          std::vector<std::shared_ptr<ProofNode>> farkasChildren;
1767
13524
          farkasChildren.push_back(
1768
27048
              pnm->mkAssume(getNegation()->getProofLiteral()));
1769
13524
          farkasChildren.insert(
1770
27048
              farkasChildren.end(), children.rbegin(), children.rend());
1771
1772
13524
          NodeManager* nm = NodeManager::currentNM();
1773
1774
          // Enumerate d_farkasCoefficients as nodes.
1775
27048
          std::vector<Node> farkasCoeffs;
1776
97627
          for (Rational r : *getFarkasCoefficients())
1777
          {
1778
84103
            farkasCoeffs.push_back(nm->mkConst<Rational>(r));
1779
          }
1780
1781
          // Apply the scaled-sum rule.
1782
          std::shared_ptr<ProofNode> sumPf = pnm->mkNode(
1783
27048
              PfRule::MACRO_ARITH_SCALE_SUM_UB, farkasChildren, farkasCoeffs);
1784
1785
          // Provable rewrite the result
1786
          auto botPf = pnm->mkNode(
1787
27048
              PfRule::MACRO_SR_PRED_TRANSFORM, {sumPf}, {nm->mkConst(false)});
1788
1789
          // Scope out the negated constraint, yielding a proof of the
1790
          // constraint.
1791
27048
          std::vector<Node> assump{getNegation()->getProofLiteral()};
1792
27048
          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
40572
          pf = pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM,
1799
                           {maybeDoubleNotPf},
1800
27048
                           {getProofLiteral()});
1801
1802
13524
          break;
1803
        }
1804
69236
        case ArithProofType::IntTightenAP:
1805
        {
1806
69236
          if (isUpperBound())
1807
          {
1808
67000
            pf = pnm->mkNode(
1809
134000
                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
69236
          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
6802
        case ArithProofType::TrichotomyAP:
1833
        {
1834
13604
          pf = pnm->mkNode(PfRule::ARITH_TRICHOTOMY,
1835
                           children,
1836
                           {getProofLiteral()},
1837
20406
                           getProofLiteral());
1838
6802
          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
5550932
  return pf;
1852
}
1853
1854
1776
Node Constraint::externalExplainByAssertions(ConstraintCP a, ConstraintCP b){
1855
3552
  NodeBuilder nb(kind::AND);
1856
1776
  a->externalExplainByAssertions(nb);
1857
1776
  b->externalExplainByAssertions(nb);
1858
3552
  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
729925
ConstraintP Constraint::getStrictlyWeakerLowerBound(bool hasLiteral, bool asserted) const {
1870
729925
  Assert(initialized());
1871
729925
  Assert(!asserted || hasLiteral);
1872
1873
729925
  SortedConstraintMapConstIterator i = d_variablePosition;
1874
729925
  const SortedConstraintMap& scm = constraintSet();
1875
729925
  SortedConstraintMapConstIterator i_begin = scm.begin();
1876
2362377
  while(i != i_begin){
1877
945655
    --i;
1878
945655
    const ValueCollection& vc = i->second;
1879
945655
    if(vc.hasLowerBound()){
1880
252712
      ConstraintP weaker = vc.getLowerBound();
1881
1882
      // asserted -> hasLiteral
1883
      // hasLiteral -> weaker->hasLiteral()
1884
      // asserted -> weaker->assertedToTheTheory()
1885
523109
      if((!hasLiteral || (weaker->hasLiteral())) &&
1886
280701
         (!asserted || ( weaker->assertedToTheTheory()))){
1887
129429
        return weaker;
1888
      }
1889
    }
1890
  }
1891
600496
  return NullConstraint;
1892
}
1893
1894
378503
ConstraintP Constraint::getStrictlyWeakerUpperBound(bool hasLiteral, bool asserted) const {
1895
378503
  SortedConstraintMapConstIterator i = d_variablePosition;
1896
378503
  const SortedConstraintMap& scm = constraintSet();
1897
378503
  SortedConstraintMapConstIterator i_end = scm.end();
1898
1899
378503
  ++i;
1900
1111433
  for(; i != i_end; ++i){
1901
505769
    const ValueCollection& vc = i->second;
1902
505769
    if(vc.hasUpperBound()){
1903
187578
      ConstraintP weaker = vc.getUpperBound();
1904
478086
      if((!hasLiteral || (weaker->hasLiteral())) &&
1905
297613
         (!asserted || ( weaker->assertedToTheTheory()))){
1906
139304
        return weaker;
1907
      }
1908
    }
1909
  }
1910
1911
239199
  return NullConstraint;
1912
}
1913
1914
8783991
ConstraintP ConstraintDatabase::getBestImpliedBound(ArithVar v, ConstraintType t, const DeltaRational& r) const {
1915
8783991
  Assert(variableDatabaseIsSetup(v));
1916
8783991
  Assert(t == UpperBound || t == LowerBound);
1917
1918
8783991
  SortedConstraintMap& scm = getVariableSCM(v);
1919
8783991
  if(t == UpperBound){
1920
4263884
    SortedConstraintMapConstIterator i = scm.lower_bound(r);
1921
4263884
    SortedConstraintMapConstIterator i_end = scm.end();
1922
4263884
    Assert(i == i_end || r <= i->first);
1923
8832740
    for(; i != i_end; i++){
1924
3694789
      Assert(r <= i->first);
1925
3694789
      const ValueCollection& vc = i->second;
1926
3694789
      if(vc.hasUpperBound()){
1927
1410361
        return vc.getUpperBound();
1928
      }
1929
    }
1930
2853523
    return NullConstraint;
1931
  }else{
1932
4520107
    Assert(t == LowerBound);
1933
4520107
    if(scm.empty()){
1934
311709
      return NullConstraint;
1935
    }else{
1936
4208398
      SortedConstraintMapConstIterator i = scm.lower_bound(r);
1937
4208398
      SortedConstraintMapConstIterator i_begin = scm.begin();
1938
4208398
      SortedConstraintMapConstIterator i_end = scm.end();
1939
4208398
      Assert(i == i_end || r <= i->first);
1940
1941
4208398
      int fdj = 0;
1942
1943
4208398
      if(i == i_end){
1944
1811437
        --i;
1945
1811437
        Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl;
1946
2396961
      }else if( (i->first) > r){
1947
679296
        if(i == i_begin){
1948
615358
          return NullConstraint;
1949
        }else{
1950
63938
          --i;
1951
63938
          Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl;
1952
        }
1953
      }
1954
1955
      do{
1956
4024886
        Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl;
1957
4024886
        Assert(r >= i->first);
1958
4024886
        const ValueCollection& vc = i->second;
1959
1960
4024886
        if(vc.hasLowerBound()){
1961
1825930
          return vc.getLowerBound();
1962
        }
1963
1964
2198956
        if(i == i_begin){
1965
1767110
          break;
1966
        }else{
1967
431846
          --i;
1968
431846
        }
1969
      }while(true);
1970
1767110
      return NullConstraint;
1971
    }
1972
  }
1973
}
1974
6291
TrustNode ConstraintDatabase::eeExplain(const Constraint* const c) const
1975
{
1976
6291
  Assert(c->hasLiteral());
1977
6291
  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
24036661
bool ConstraintDatabase::variableDatabaseIsSetup(ArithVar v) const {
1988
24036661
  return v < d_varDatabases.size();
1989
}
1990
1991
1992
9913
ConstraintDatabase::Watches::Watches(context::Context* satContext, context::Context* userContext):
1993
  d_constraintProofs(satContext),
1994
  d_canBePropagatedWatches(satContext),
1995
  d_assertionOrderWatches(satContext),
1996
9913
  d_splitWatches(userContext)
1997
9913
{}
1998
1999
2000
541764
void Constraint::setLiteral(Node n) {
2001
541764
  Debug("arith::constraint") << "Mapping " << *this << " to " << n << std::endl;
2002
541764
  Assert(Comparison::isNormalAtom(n));
2003
541764
  Assert(!hasLiteral());
2004
541764
  Assert(sanityChecking(n));
2005
541764
  d_literal = n;
2006
541764
  NodetoConstraintMap& map = d_database->d_nodetoConstraintMap;
2007
541764
  Assert(map.find(n) == map.end());
2008
541764
  map.insert(make_pair(d_literal, this));
2009
541764
}
2010
2011
1410488
Node Constraint::getProofLiteral() const
2012
{
2013
1410488
  Assert(d_database != nullptr);
2014
1410488
  Assert(d_database->d_avariables.hasNode(d_variable));
2015
2820976
  Node varPart = d_database->d_avariables.asNode(d_variable);
2016
  Kind cmp;
2017
1410488
  bool neg = false;
2018
1410488
  switch (d_type)
2019
  {
2020
538315
    case ConstraintType::UpperBound:
2021
    {
2022
538315
      if (d_value.infinitesimalIsZero())
2023
      {
2024
145761
        cmp = Kind::LEQ;
2025
      }
2026
      else
2027
      {
2028
392554
        cmp = Kind::LT;
2029
      }
2030
538315
      break;
2031
    }
2032
220186
    case ConstraintType::LowerBound:
2033
    {
2034
220186
      if (d_value.infinitesimalIsZero())
2035
      {
2036
178662
        cmp = Kind::GEQ;
2037
      }
2038
      else
2039
      {
2040
41524
        cmp = Kind::GT;
2041
      }
2042
220186
      break;
2043
    }
2044
515950
    case ConstraintType::Equality:
2045
    {
2046
515950
      cmp = Kind::EQUAL;
2047
515950
      break;
2048
    }
2049
136037
    case ConstraintType::Disequality:
2050
    {
2051
136037
      cmp = Kind::EQUAL;
2052
136037
      neg = true;
2053
136037
      break;
2054
    }
2055
    default: Unreachable() << d_type;
2056
  }
2057
1410488
  NodeManager* nm = NodeManager::currentNM();
2058
2820976
  Node constPart = nm->mkConst<Rational>(d_value.getNoninfinitesimalPart());
2059
2820976
  Node posLit = nm->mkNode(cmp, varPart, constPart);
2060
2820976
  return neg ? posLit.negate() : posLit;
2061
}
2062
2063
37023
void ConstraintDatabase::proveOr(std::vector<TrustNode>& out,
2064
                                 ConstraintP a,
2065
                                 ConstraintP b,
2066
                                 bool negateSecond) const
2067
{
2068
74046
  Node la = a->getLiteral();
2069
74046
  Node lb = b->getLiteral();
2070
74046
  Node orN = (la < lb) ? la.orNode(lb) : lb.orNode(la);
2071
37023
  if (isProofEnabled())
2072
  {
2073
5910
    Assert(b->getNegation()->getType() != ConstraintType::Disequality);
2074
5910
    auto nm = NodeManager::currentNM();
2075
5910
    auto pf_neg_la = d_pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM,
2076
11820
                                   {d_pnm->mkAssume(la.negate())},
2077
23640
                                   {a->getNegation()->getProofLiteral()});
2078
5910
    auto pf_neg_lb = d_pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM,
2079
11820
                                   {d_pnm->mkAssume(lb.negate())},
2080
23640
                                   {b->getNegation()->getProofLiteral()});
2081
5910
    int sndSign = negateSecond ? -1 : 1;
2082
    auto bot_pf =
2083
5910
        d_pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM,
2084
5910
                      {d_pnm->mkNode(PfRule::MACRO_ARITH_SCALE_SUM_UB,
2085
                                     {pf_neg_la, pf_neg_lb},
2086
                                     {nm->mkConst<Rational>(-1 * sndSign),
2087
23640
                                      nm->mkConst<Rational>(sndSign)})},
2088
29550
                      {nm->mkConst(false)});
2089
11820
    std::vector<Node> as;
2090
17730
    std::transform(orN.begin(), orN.end(), std::back_inserter(as), [](Node n) {
2091
      return n.negate();
2092
17730
    });
2093
    // No need to ensure that the expected node aggrees with `as` because we
2094
    // are not providing an expected node.
2095
5910
    auto pf = d_pnm->mkNode(
2096
        PfRule::MACRO_SR_PRED_TRANSFORM,
2097
17730
        {d_pnm->mkNode(PfRule::NOT_AND, {d_pnm->mkScope(bot_pf, as)}, {})},
2098
29550
        {orN});
2099
5910
    out.push_back(d_pfGen->mkTrustNode(orN, pf));
2100
  }
2101
  else
2102
  {
2103
31113
    out.push_back(TrustNode::mkTrustLemma(orN));
2104
  }
2105
37023
}
2106
2107
34683
void ConstraintDatabase::implies(std::vector<TrustNode>& out,
2108
                                 ConstraintP a,
2109
                                 ConstraintP b) const
2110
{
2111
69366
  Node la = a->getLiteral();
2112
69366
  Node lb = b->getLiteral();
2113
2114
69366
  Node neg_la = (la.getKind() == kind::NOT)? la[0] : la.notNode();
2115
2116
34683
  Assert(lb != neg_la);
2117
34683
  Assert(b->getNegation()->getType() == ConstraintType::LowerBound
2118
         || b->getNegation()->getType() == ConstraintType::UpperBound);
2119
34683
  proveOr(out,
2120
          a->getNegation(),
2121
          b,
2122
34683
          b->getNegation()->getType() == ConstraintType::LowerBound);
2123
34683
}
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
56227
void ConstraintDatabase::outputUnateInequalityLemmas(
2138
    std::vector<TrustNode>& out, ArithVar v) const
2139
{
2140
56227
  SortedConstraintMap& scm = getVariableSCM(v);
2141
56227
  SortedConstraintMapConstIterator scm_iter = scm.begin();
2142
56227
  SortedConstraintMapConstIterator scm_end = scm.end();
2143
56227
  ConstraintP prev = NullConstraint;
2144
  //get transitive unates
2145
  //Only lower bounds or upperbounds should be done.
2146
298439
  for(; scm_iter != scm_end; ++scm_iter){
2147
121106
    const ValueCollection& vc = scm_iter->second;
2148
121106
    if(vc.hasUpperBound()){
2149
58143
      ConstraintP ub = vc.getUpperBound();
2150
58143
      if(ub->hasLiteral()){
2151
58143
        if(prev != NullConstraint){
2152
26843
          implies(out, prev, ub);
2153
        }
2154
58143
        prev = ub;
2155
      }
2156
    }
2157
  }
2158
56227
}
2159
2160
56227
void ConstraintDatabase::outputUnateEqualityLemmas(std::vector<TrustNode>& out,
2161
                                                   ArithVar v) const
2162
{
2163
112454
  vector<ConstraintP> equalities;
2164
2165
56227
  SortedConstraintMap& scm = getVariableSCM(v);
2166
56227
  SortedConstraintMapConstIterator scm_iter = scm.begin();
2167
56227
  SortedConstraintMapConstIterator scm_end = scm.end();
2168
2169
298439
  for(; scm_iter != scm_end; ++scm_iter){
2170
121106
    const ValueCollection& vc = scm_iter->second;
2171
121106
    if(vc.hasEquality()){
2172
17878
      ConstraintP eq = vc.getEquality();
2173
17878
      if(eq->hasLiteral()){
2174
17878
        equalities.push_back(eq);
2175
      }
2176
    }
2177
  }
2178
2179
56227
  vector<ConstraintP>::const_iterator i, j, eq_end = equalities.end();
2180
74105
  for(i = equalities.begin(); i != eq_end; ++i){
2181
17878
    ConstraintP at_i = *i;
2182
20218
    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
74105
  for(i = equalities.begin(); i != eq_end; ++i){
2190
17878
    ConstraintP eq = *i;
2191
17878
    const ValueCollection& vc = eq->getValueCollection();
2192
17878
    Assert(vc.hasEquality() && vc.getEquality()->hasLiteral());
2193
2194
17878
    bool hasLB = vc.hasLowerBound() && vc.getLowerBound()->hasLiteral();
2195
17878
    bool hasUB = vc.hasUpperBound() && vc.getUpperBound()->hasLiteral();
2196
2197
17878
    ConstraintP lb = hasLB ?
2198
17878
      vc.getLowerBound() : eq->getStrictlyWeakerLowerBound(true, false);
2199
17878
    ConstraintP ub = hasUB ?
2200
17878
      vc.getUpperBound() : eq->getStrictlyWeakerUpperBound(true, false);
2201
2202
17878
    if(hasUB && hasLB && !eq->isSplit()){
2203
115
      out.push_back(eq->split());
2204
    }
2205
17878
    if(lb != NullConstraint){
2206
2862
      implies(out, eq, lb);
2207
    }
2208
17878
    if(ub != NullConstraint){
2209
4978
      implies(out, eq, ub);
2210
    }
2211
  }
2212
56227
}
2213
2214
7657
void ConstraintDatabase::outputUnateEqualityLemmas(
2215
    std::vector<TrustNode>& lemmas) const
2216
{
2217
63884
  for(ArithVar v = 0, N = d_varDatabases.size(); v < N; ++v){
2218
56227
    outputUnateEqualityLemmas(lemmas, v);
2219
  }
2220
7657
}
2221
2222
7657
void ConstraintDatabase::outputUnateInequalityLemmas(
2223
    std::vector<TrustNode>& lemmas) const
2224
{
2225
63884
  for(ArithVar v = 0, N = d_varDatabases.size(); v < N; ++v){
2226
56227
    outputUnateInequalityLemmas(lemmas, v);
2227
  }
2228
7657
}
2229
2230
3993500
bool ConstraintDatabase::handleUnateProp(ConstraintP ant, ConstraintP cons){
2231
3993500
  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
3993500
  }else if(!cons->isTrue()){
2237
2089651
    ++d_statistics.d_unatePropagateImplications;
2238
2089651
    Debug("arith::unate") << "handleUnate: " << ant << " implies " << cons << endl;
2239
2089651
    cons->impliedByUnate(ant, false);
2240
2089651
    cons->tryToPropagate();
2241
2089651
    return false;
2242
  } else {
2243
1903849
    return false;
2244
  }
2245
}
2246
2247
1083751
void ConstraintDatabase::unatePropLowerBound(ConstraintP curr, ConstraintP prev){
2248
1083751
  Debug("arith::unate") << "unatePropLowerBound " << curr << " " << prev << endl;
2249
1083751
  Assert(curr != prev);
2250
1083751
  Assert(curr != NullConstraint);
2251
1083751
  bool hasPrev = ! (prev == NullConstraint);
2252
1083751
  Assert(!hasPrev || curr->getValue() > prev->getValue());
2253
2254
1083751
  ++d_statistics.d_unatePropagateCalls;
2255
2256
1083751
  const SortedConstraintMap& scm = curr->constraintSet();
2257
1083751
  const SortedConstraintMapConstIterator scm_begin = scm.begin();
2258
1083751
  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
6424407
  while(scm_i != scm_begin){
2265
2901405
    --scm_i; // move the iterator back
2266
2267
2901405
    const ValueCollection& vc = scm_i->second;
2268
2269
    //If it has the previous element, do nothing and stop!
2270
3652717
    if(hasPrev &&
2271
751312
       vc.hasConstraintOfType(prev->getType())
2272
3384921
       && vc.getConstraintOfType(prev->getType()) == prev){
2273
231077
      break;
2274
    }
2275
2276
    //Don't worry about implying the negation of upperbound.
2277
    //These should all be handled by propagating the LowerBounds!
2278
2670328
    if(vc.hasLowerBound()){
2279
1000485
      ConstraintP lb = vc.getLowerBound();
2280
1000485
      if(handleUnateProp(curr, lb)){ return; }
2281
    }
2282
2670328
    if(vc.hasDisequality()){
2283
282406
      ConstraintP dis = vc.getDisequality();
2284
282406
      if(handleUnateProp(curr, dis)){ return; }
2285
    }
2286
  }
2287
}
2288
2289
960544
void ConstraintDatabase::unatePropUpperBound(ConstraintP curr, ConstraintP prev){
2290
960544
  Debug("arith::unate") << "unatePropUpperBound " << curr << " " << prev << endl;
2291
960544
  Assert(curr != prev);
2292
960544
  Assert(curr != NullConstraint);
2293
960544
  bool hasPrev = ! (prev == NullConstraint);
2294
960544
  Assert(!hasPrev || curr->getValue() < prev->getValue());
2295
2296
960544
  ++d_statistics.d_unatePropagateCalls;
2297
2298
960544
  const SortedConstraintMap& scm = curr->constraintSet();
2299
960544
  const SortedConstraintMapConstIterator scm_end = scm.end();
2300
960544
  SortedConstraintMapConstIterator scm_i = curr->d_variablePosition;
2301
960544
  ++scm_i;
2302
6436568
  for(; scm_i != scm_end; ++scm_i){
2303
2902708
    const ValueCollection& vc = scm_i->second;
2304
2305
    //If it has the previous element, do nothing and stop!
2306
3425114
    if(hasPrev &&
2307
3227854
       vc.hasConstraintOfType(prev->getType()) &&
2308
325146
       vc.getConstraintOfType(prev->getType()) == prev){
2309
164696
      break;
2310
    }
2311
    //Don't worry about implying the negation of upperbound.
2312
    //These should all be handled by propagating the UpperBounds!
2313
2738012
    if(vc.hasUpperBound()){
2314
1056518
      ConstraintP ub = vc.getUpperBound();
2315
1056518
      if(handleUnateProp(curr, ub)){ return; }
2316
    }
2317
2738012
    if(vc.hasDisequality()){
2318
221182
      ConstraintP dis = vc.getDisequality();
2319
221182
      if(handleUnateProp(curr, dis)){ return; }
2320
    }
2321
  }
2322
}
2323
2324
958083
void ConstraintDatabase::unatePropEquality(ConstraintP curr, ConstraintP prevLB, ConstraintP prevUB){
2325
958083
  Debug("arith::unate") << "unatePropEquality " << curr << " " << prevLB << " " << prevUB << endl;
2326
958083
  Assert(curr != prevLB);
2327
958083
  Assert(curr != prevUB);
2328
958083
  Assert(curr != NullConstraint);
2329
958083
  bool hasPrevLB = ! (prevLB == NullConstraint);
2330
958083
  bool hasPrevUB = ! (prevUB == NullConstraint);
2331
958083
  Assert(!hasPrevLB || curr->getValue() >= prevLB->getValue());
2332
958083
  Assert(!hasPrevUB || curr->getValue() <= prevUB->getValue());
2333
2334
958083
  ++d_statistics.d_unatePropagateCalls;
2335
2336
958083
  const SortedConstraintMap& scm = curr->constraintSet();
2337
958083
  SortedConstraintMapConstIterator scm_curr = curr->d_variablePosition;
2338
958083
  SortedConstraintMapConstIterator scm_last = hasPrevUB ? prevUB->d_variablePosition : scm.end();
2339
958083
  SortedConstraintMapConstIterator scm_i;
2340
958083
  if(hasPrevLB){
2341
141264
    scm_i = prevLB->d_variablePosition;
2342
141264
    if(scm_i != scm_curr){ // If this does not move this past scm_curr, move it one forward
2343
33987
      ++scm_i;
2344
    }
2345
  }else{
2346
816819
    scm_i = scm.begin();
2347
  }
2348
2349
2296737
  for(; scm_i != scm_curr; ++scm_i){
2350
    // between the previous LB and the curr
2351
669327
    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
669327
    if(vc.hasLowerBound()){
2356
232019
      ConstraintP lb = vc.getLowerBound();
2357
232019
      if(handleUnateProp(curr, lb)){ return; }
2358
    }
2359
669327
    if(vc.hasDisequality()){
2360
216553
      ConstraintP dis = vc.getDisequality();
2361
216553
      if(handleUnateProp(curr, dis)){ return; }
2362
    }
2363
  }
2364
958083
  Assert(scm_i == scm_curr);
2365
958083
  if(!hasPrevUB || scm_i != scm_last){
2366
932287
    ++scm_i;
2367
  } // hasPrevUB implies scm_i != scm_last
2368
2369
4157185
  for(; scm_i != scm_last; ++scm_i){
2370
    // between the curr and the previous UB imply the upperbounds and disequalities.
2371
1599551
    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
1599551
    if(vc.hasUpperBound()){
2376
595858
      ConstraintP ub = vc.getUpperBound();
2377
595858
      if(handleUnateProp(curr, ub)){ return; }
2378
    }
2379
1599551
    if(vc.hasDisequality()){
2380
388479
      ConstraintP dis = vc.getDisequality();
2381
388479
      if(handleUnateProp(curr, dis)){ return; }
2382
    }
2383
  }
2384
}
2385
2386
790608
std::pair<int, int> Constraint::unateFarkasSigns(ConstraintCP ca, ConstraintCP cb){
2387
790608
  ConstraintType a = ca->getType();
2388
790608
  ConstraintType b = cb->getType();
2389
2390
790608
  Assert(a != Disequality);
2391
790608
  Assert(b != Disequality);
2392
2393
790608
  int a_sgn = (a == LowerBound) ? -1 : ((a == UpperBound) ? 1 : 0);
2394
790608
  int b_sgn = (b == LowerBound) ? -1 : ((b == UpperBound) ? 1 : 0);
2395
2396
790608
  if(a_sgn == 0 && b_sgn == 0){
2397
191017
    Assert(a == Equality);
2398
191017
    Assert(b == Equality);
2399
191017
    Assert(ca->getValue() != cb->getValue());
2400
382034
    if(ca->getValue() < cb->getValue()){
2401
61467
      a_sgn = 1;
2402
61467
      b_sgn = -1;
2403
    }else{
2404
129550
      a_sgn = -1;
2405
129550
      b_sgn = 1;
2406
    }
2407
599591
  }else if(a_sgn == 0){
2408
133679
    Assert(b_sgn != 0);
2409
133679
    Assert(a == Equality);
2410
133679
    a_sgn = -b_sgn;
2411
465912
  }else if(b_sgn == 0){
2412
248408
    Assert(a_sgn != 0);
2413
248408
    Assert(b == Equality);
2414
248408
    b_sgn = -a_sgn;
2415
  }
2416
790608
  Assert(a_sgn != 0);
2417
790608
  Assert(b_sgn != 0);
2418
2419
1581216
  Debug("arith::unateFarkasSigns") << "Constraint::unateFarkasSigns("<<a <<", " << b << ") -> "
2420
790608
                                   << "("<<a_sgn<<", "<< b_sgn <<")"<< endl;
2421
790608
  return make_pair(a_sgn, b_sgn);
2422
}
2423
2424
}  // namespace arith
2425
}  // namespace theory
2426
29502
}  // namespace cvc5