GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/constraint.cpp Lines: 1072 1472 72.8 %
Date: 2021-05-24 Branches: 1804 6411 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 "expr/proof_node_manager.h"
26
#include "smt/smt_statistics_registry.h"
27
#include "theory/eager_proof_generator.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/rewriter.h"
33
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
483838
ConstraintType Constraint::constraintTypeOfComparison(const Comparison& cmp){
45
483838
  Kind k = cmp.comparisonKind();
46
483838
  switch(k){
47
135870
  case LT:
48
  case LEQ:
49
    {
50
271740
      Polynomial l = cmp.getLeft();
51
135870
      if(l.leadingCoefficientIsPositive()){ // (< x c)
52
111083
        return UpperBound;
53
      }else{
54
24787
        return LowerBound; // (< (-x) c)
55
      }
56
    }
57
137076
  case GT:
58
  case GEQ:
59
    {
60
274152
      Polynomial l = cmp.getLeft();
61
137076
      if(l.leadingCoefficientIsPositive()){
62
112077
        return LowerBound; // (> x c)
63
      }else{
64
24999
        return UpperBound; // (> (-x) c)
65
      }
66
    }
67
105548
  case EQUAL:
68
105548
    return Equality;
69
105344
  case DISTINCT:
70
105344
    return Disequality;
71
  default: Unhandled() << k;
72
  }
73
}
74
75
601656
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
601656
    d_variablePosition()
88
{
89
601656
  Assert(!initialized());
90
601656
}
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
1809513
ValueCollection::ValueCollection()
191
  : d_lowerBound(NullConstraint),
192
    d_upperBound(NullConstraint),
193
    d_equality(NullConstraint),
194
1809513
    d_disequality(NullConstraint)
195
1809513
{}
196
197
16148293
bool ValueCollection::hasLowerBound() const{
198
16148293
  return d_lowerBound != NullConstraint;
199
}
200
201
17272049
bool ValueCollection::hasUpperBound() const{
202
17272049
  return d_upperBound != NullConstraint;
203
}
204
205
4207350
bool ValueCollection::hasEquality() const{
206
4207350
  return d_equality != NullConstraint;
207
}
208
209
12538366
bool ValueCollection::hasDisequality() const {
210
12538366
  return d_disequality != NullConstraint;
211
}
212
213
3337842
ConstraintP ValueCollection::getLowerBound() const {
214
3337842
  Assert(hasLowerBound());
215
3337842
  return d_lowerBound;
216
}
217
218
3493530
ConstraintP ValueCollection::getUpperBound() const {
219
3493530
  Assert(hasUpperBound());
220
3493530
  return d_upperBound;
221
}
222
223
375969
ConstraintP ValueCollection::getEquality() const {
224
375969
  Assert(hasEquality());
225
375969
  return d_equality;
226
}
227
228
1770128
ConstraintP ValueCollection::getDisequality() const {
229
1770128
  Assert(hasDisequality());
230
1770128
  return d_disequality;
231
}
232
233
234
409748
void ValueCollection::push_into(std::vector<ConstraintP>& vec) const {
235
409748
  Debug("arith::constraint") << "push_into " << *this << endl;
236
409748
  if(hasEquality()){
237
110107
    vec.push_back(d_equality);
238
  }
239
409748
  if(hasLowerBound()){
240
190016
    vec.push_back(d_lowerBound);
241
  }
242
409748
  if(hasUpperBound()){
243
190016
    vec.push_back(d_upperBound);
244
  }
245
409748
  if(hasDisequality()){
246
110107
    vec.push_back(d_disequality);
247
  }
248
409748
}
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
3078526
bool ValueCollection::hasConstraintOfType(ConstraintType t) const{
273
3078526
  switch(t){
274
1050623
  case LowerBound:
275
1050623
    return hasLowerBound();
276
1622722
  case UpperBound:
277
1622722
    return hasUpperBound();
278
405181
  case Equality:
279
405181
    return hasEquality();
280
  case Disequality:
281
    return hasDisequality();
282
  default:
283
    Unreachable();
284
  }
285
}
286
287
196479
ArithVar ValueCollection::getVariable() const{
288
196479
  Assert(!empty());
289
196479
  return nonNull()->getVariable();
290
}
291
292
196479
const DeltaRational& ValueCollection::getValue() const{
293
196479
  Assert(!empty());
294
196479
  return nonNull()->getValue();
295
}
296
297
600246
void ValueCollection::add(ConstraintP c){
298
600246
  Assert(c != NullConstraint);
299
300
600246
  Assert(empty() || getVariable() == c->getVariable());
301
600246
  Assert(empty() || getValue() == c->getValue());
302
303
600246
  switch(c->getType()){
304
190016
  case LowerBound:
305
190016
    Assert(!hasLowerBound());
306
190016
    d_lowerBound = c;
307
190016
    break;
308
110107
  case Equality:
309
110107
    Assert(!hasEquality());
310
110107
    d_equality = c;
311
110107
    break;
312
190016
  case UpperBound:
313
190016
    Assert(!hasUpperBound());
314
190016
    d_upperBound = c;
315
190016
    break;
316
110107
  case Disequality:
317
110107
    Assert(!hasDisequality());
318
110107
    d_disequality = c;
319
110107
    break;
320
  default:
321
    Unreachable();
322
  }
323
600246
}
324
325
2305760
ConstraintP ValueCollection::getConstraintOfType(ConstraintType t) const{
326
2305760
  switch(t){
327
647290
    case LowerBound: Assert(hasLowerBound()); return d_lowerBound;
328
295074
    case Equality: Assert(hasEquality()); return d_equality;
329
1363396
    case UpperBound: Assert(hasUpperBound()); return d_upperBound;
330
    case Disequality: Assert(hasDisequality()); return d_disequality;
331
    default: Unreachable();
332
  }
333
}
334
335
600246
void ValueCollection::remove(ConstraintType t){
336
600246
  switch(t){
337
190016
  case LowerBound:
338
190016
    Assert(hasLowerBound());
339
190016
    d_lowerBound = NullConstraint;
340
190016
    break;
341
110107
  case Equality:
342
110107
    Assert(hasEquality());
343
110107
    d_equality = NullConstraint;
344
110107
    break;
345
190016
  case UpperBound:
346
190016
    Assert(hasUpperBound());
347
190016
    d_upperBound = NullConstraint;
348
190016
    break;
349
110107
  case Disequality:
350
110107
    Assert(hasDisequality());
351
110107
    d_disequality = NullConstraint;
352
110107
    break;
353
  default:
354
    Unreachable();
355
  }
356
600246
}
357
358
2193696
bool ValueCollection::empty() const{
359
  return
360
5303418
    !(hasLowerBound() ||
361
3665593
      hasUpperBound() ||
362
1785115
      hasEquality() ||
363
3422940
      hasDisequality());
364
}
365
366
392958
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
392958
  if(hasLowerBound()){
370
123996
    return d_lowerBound;
371
268962
  }else if(hasUpperBound()){
372
46080
    return d_upperBound;
373
222882
  }else if(hasEquality()){
374
222882
    return d_equality;
375
  }else if(hasDisequality()){
376
    return d_disequality;
377
  }else{
378
    return NullConstraint;
379
  }
380
}
381
382
2449008
bool Constraint::initialized() const {
383
2449008
  return d_database != NULL;
384
}
385
386
const ConstraintDatabase& Constraint::getDatabase() const{
387
  Assert(initialized());
388
  return *d_database;
389
}
390
391
600246
void Constraint::initialize(ConstraintDatabase* db, SortedConstraintMapIterator v, ConstraintP negation){
392
600246
  Assert(!initialized());
393
600246
  d_database = db;
394
600246
  d_variablePosition = v;
395
600246
  d_negation = negation;
396
600246
}
397
398
1203312
Constraint::~Constraint() {
399
  // Call this instead of safeToGarbageCollect()
400
601656
  Assert(!contextDependentDataIsSet());
401
402
601656
  if(initialized()){
403
600246
    ValueCollection& vc =  d_variablePosition->second;
404
600246
    Debug("arith::constraint") << "removing" << vc << endl;
405
406
600246
    vc.remove(getType());
407
408
600246
    if(vc.empty()){
409
409748
      Debug("arith::constraint") << "erasing" << vc << endl;
410
409748
      SortedConstraintMap& perVariable = d_database->getVariableSCM(getVariable());
411
409748
      perVariable.erase(d_variablePosition);
412
    }
413
414
600246
    if(hasLiteral()){
415
485248
      d_database->d_nodetoConstraintMap.erase(getLiteral());
416
    }
417
  }
418
601656
}
419
420
21511669
const ConstraintRule& Constraint::getConstraintRule() const {
421
21511669
  Assert(hasProof());
422
21511669
  return d_database->d_watches->d_constraintProofs[d_crid];
423
}
424
425
2693353
const ValueCollection& Constraint::getValueCollection() const{
426
2693353
  return d_variablePosition->second;
427
}
428
429
430
54375
ConstraintP Constraint::getCeiling() {
431
54375
  Debug("getCeiling") << "Constraint_::getCeiling on " << *this << endl;
432
54375
  Assert(getValue().getInfinitesimalPart().sgn() > 0);
433
434
108750
  const DeltaRational ceiling(getValue().ceiling());
435
108750
  return d_database->getConstraint(getVariable(), getType(), ceiling);
436
}
437
438
1016884
ConstraintP Constraint::getFloor() {
439
1016884
  Assert(getValue().getInfinitesimalPart().sgn() < 0);
440
441
2033768
  const DeltaRational floor(Rational(getValue().floor()));
442
2033768
  return d_database->getConstraint(getVariable(), getType(), floor);
443
}
444
445
743424
void Constraint::setCanBePropagated() {
446
743424
  Assert(!canBePropagated());
447
743424
  d_database->pushCanBePropagatedWatch(this);
448
743424
}
449
450
4827704
void Constraint::setAssertedToTheTheory(TNode witness, bool nowInConflict) {
451
4827704
  Assert(hasLiteral());
452
4827704
  Assert(!assertedToTheTheory());
453
4827704
  Assert(negationHasProof() == nowInConflict);
454
4827704
  d_database->pushAssertionOrderWatch(this, witness);
455
456
4827704
  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
4827704
}
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
8972870
bool Constraint::isInternalAssumption() const {
480
8972870
  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
9529444
bool Constraint::isAssumption() const {
507
9529444
  return getProofType() == AssumeAP;
508
}
509
510
542843
bool Constraint::hasEqualityEngineProof() const {
511
542843
  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
485248
bool Constraint::sanityChecking(Node n) const {
622
970496
  Comparison cmp = Comparison::parseNormalForm(n);
623
485248
  Kind k = cmp.comparisonKind();
624
970496
  Polynomial pleft = cmp.normalizedVariablePart();
625
485248
  Assert(k == EQUAL || k == DISTINCT || pleft.leadingCoefficientIsPositive());
626
485248
  Assert(k != EQUAL || Monomial::isMember(n[0]));
627
485248
  Assert(k != DISTINCT || Monomial::isMember(n[0][0]));
628
629
970496
  TNode left = pleft.getNode();
630
970496
  DeltaRational right = cmp.normalizedDeltaRational();
631
632
485248
  const ArithVariables& avariables = d_database->getArithVariables();
633
634
485248
  Debug("Constraint::sanityChecking") << cmp.getNode() << endl;
635
485248
  Debug("Constraint::sanityChecking") << k << endl;
636
485248
  Debug("Constraint::sanityChecking") << pleft.getNode() << endl;
637
485248
  Debug("Constraint::sanityChecking") << left << endl;
638
485248
  Debug("Constraint::sanityChecking") << right << endl;
639
485248
  Debug("Constraint::sanityChecking") << getValue() << endl;
640
485248
  Debug("Constraint::sanityChecking") << avariables.hasArithVar(left) << endl;
641
485248
  Debug("Constraint::sanityChecking") << avariables.asArithVar(left) << endl;
642
485248
  Debug("Constraint::sanityChecking") << getVariable() << endl;
643
644
645
2426240
  if(avariables.hasArithVar(left) &&
646
2426240
     avariables.asArithVar(left) == getVariable() &&
647
485248
     getValue() == right){
648
485248
    switch(getType()){
649
274152
    case LowerBound:
650
    case UpperBound:
651
      //Be overapproximate
652
274152
      return k == GT || k == GEQ ||k == LT || k == LEQ;
653
105548
    case Equality:
654
105548
      return k == EQUAL;
655
105548
    case Disequality:
656
105548
      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
1774710
bool Constraint::wellFormedFarkasProof() const {
715
1774710
  Assert(hasProof());
716
717
1774710
  const ConstraintRule& cr = getConstraintRule();
718
1774710
  if(cr.d_constraint != this){ return false; }
719
1774710
  if(cr.d_proofType != FarkasAP){ return false; }
720
721
1774710
  AntecedentId p = cr.d_antecedentEnd;
722
723
  // must have at least one antecedent
724
1774710
  ConstraintCP antecedent = d_database->d_antecedents[p];
725
1774710
  if(antecedent  == NullConstraint) { return false; }
726
727
1774710
  if (!ARITH_PROOF_ON())
728
  {
729
953436
    return cr.d_farkasCoefficients == RationalVectorCPSentinel;
730
  }
731
821274
  Assert(ARITH_PROOF_ON());
732
733
821274
  if(cr.d_farkasCoefficients == RationalVectorCPSentinel){ return false; }
734
821274
  if(cr.d_farkasCoefficients->size() < 2){ return false; }
735
736
821274
  const ArithVariables& vars = d_database->getArithVariables();
737
738
1642548
  DeltaRational rhs(0);
739
1642548
  Node lhs = Polynomial::mkZero().getNode();
740
741
821274
  RationalVector::const_iterator coeffIterator = cr.d_farkasCoefficients->end()-1;
742
821274
  RationalVector::const_iterator coeffBegin = cr.d_farkasCoefficients->begin();
743
744
3123052
  while(antecedent != NullConstraint){
745
1150889
    Assert(lhs.isNull() || Polynomial::isMember(lhs));
746
747
1150889
    const Rational& coeff = *coeffIterator;
748
1150889
    int coeffSgn = coeff.sgn();
749
750
1150889
    rhs += antecedent->getValue() * coeff;
751
752
1150889
    ArithVar antVar = antecedent->getVariable();
753
1150889
    if(!lhs.isNull() && vars.hasNode(antVar)){
754
2301778
      Node antAsNode = vars.asNode(antVar);
755
1150889
      if(Polynomial::isMember(antAsNode)){
756
2301778
        Polynomial lhsPoly = Polynomial::parsePolynomial(lhs);
757
2301778
        Polynomial antPoly = Polynomial::parsePolynomial(antAsNode);
758
2301778
        Polynomial sum = lhsPoly + (antPoly * coeff);
759
1150889
        lhs = sum.getNode();
760
      }else{
761
        lhs = Node::null();
762
      }
763
    } else {
764
      lhs = Node::null();
765
    }
766
1150889
    Debug("constraints::wffp") << "running sum: " << lhs << " <= " << rhs << endl;
767
768
1150889
    switch( antecedent->getType() ){
769
419872
    case LowerBound:
770
      // fc[l] < 0, therefore return false if coeffSgn >= 0
771
419872
      if(coeffSgn >= 0){ return false; }
772
419872
      break;
773
223053
    case UpperBound:
774
      // fc[u] > 0, therefore return false if coeffSgn <= 0
775
223053
      if(coeffSgn <= 0){ return false; }
776
223053
      break;
777
507964
    case Equality:
778
507964
      if(coeffSgn == 0) { return false; }
779
507964
      break;
780
    case Disequality:
781
    default:
782
      return false;
783
    }
784
785
1150889
    if(coeffIterator == coeffBegin){ return false; }
786
1150889
    --coeffIterator;
787
1150889
    --p;
788
1150889
    antecedent = d_database->d_antecedents[p];
789
  }
790
821274
  if(coeffIterator != coeffBegin){ return false; }
791
792
821274
  const Rational& firstCoeff = (*coeffBegin);
793
821274
  int firstCoeffSgn = firstCoeff.sgn();
794
821274
  rhs += (getNegation()->getValue()) * firstCoeff;
795
821274
  if(!lhs.isNull() && vars.hasNode(getVariable())){
796
1642548
    Node firstAsNode = vars.asNode(getVariable());
797
821274
    if(Polynomial::isMember(firstAsNode)){
798
1642548
      Polynomial lhsPoly = Polynomial::parsePolynomial(lhs);
799
1642548
      Polynomial firstPoly = Polynomial::parsePolynomial(firstAsNode);
800
1642548
      Polynomial sum = lhsPoly + (firstPoly * firstCoeff);
801
821274
      lhs = sum.getNode();
802
    }else{
803
      lhs = Node::null();
804
    }
805
  }else{
806
    lhs = Node::null();
807
  }
808
809
821274
  switch( getNegation()->getType() ){
810
198594
  case LowerBound:
811
    // fc[l] < 0, therefore return false if coeffSgn >= 0
812
198594
    if(firstCoeffSgn >= 0){ return false; }
813
198594
    break;
814
322876
  case UpperBound:
815
    // fc[u] > 0, therefore return false if coeffSgn <= 0
816
322876
    if(firstCoeffSgn <= 0){ return false; }
817
322876
    break;
818
299804
  case Equality:
819
299804
    if(firstCoeffSgn == 0) { return false; }
820
299804
    break;
821
  case Disequality:
822
  default:
823
    return false;
824
  }
825
821274
  Debug("constraints::wffp") << "final sum: " << lhs << " <= " << rhs << endl;
826
  // 0 = lhs <= rhs < 0
827
2463822
  return (lhs.isNull() || (Constant::isMember(lhs) && Constant(lhs).isZero()))
828
2463822
         && rhs.sgn() < 0;
829
}
830
831
58909
ConstraintP Constraint::makeNegation(ArithVar v, ConstraintType t, const DeltaRational& r){
832
58909
  switch(t){
833
3108
  case LowerBound:
834
    {
835
3108
      Assert(r.infinitesimalSgn() >= 0);
836
3108
      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
3108
        Assert(r.infinitesimalSgn() == 0);
843
        // make (not (v >= r)), which is (v < r)
844
6216
        DeltaRational addInf(r.getNoninfinitesimalPart(), -1);
845
3108
        return new Constraint(v, UpperBound, addInf);
846
      }
847
    }
848
51038
  case UpperBound:
849
    {
850
51038
      Assert(r.infinitesimalSgn() <= 0);
851
51038
      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
51038
        Assert(r.infinitesimalSgn() == 0);
858
        // make (not (v <= r)), which is (v > r)
859
102076
        DeltaRational addInf(r.getNoninfinitesimalPart(), 1);
860
51038
        return new Constraint(v, LowerBound, addInf);
861
      }
862
    }
863
4763
  case Equality:
864
4763
    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
9459
ConstraintDatabase::ConstraintDatabase(context::Context* satContext,
874
                                       context::Context* userContext,
875
                                       const ArithVariables& avars,
876
                                       ArithCongruenceManager& cm,
877
                                       RaiseConflict raiseConflict,
878
                                       EagerProofGenerator* pfGen,
879
9459
                                       ProofNodeManager* pnm)
880
    : d_varDatabases(),
881
      d_toPropagate(satContext),
882
      d_antecedents(satContext, false),
883
9459
      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
18918
      d_negOne(-1)
892
{
893
9459
}
894
895
8140116
SortedConstraintMap& ConstraintDatabase::getVariableSCM(ArithVar v) const{
896
8140116
  Assert(variableDatabaseIsSetup(v));
897
8140116
  return d_varDatabases[v]->d_constraints;
898
}
899
900
27692
void ConstraintDatabase::pushSplitWatch(ConstraintP c){
901
27692
  Assert(!c->d_split);
902
27692
  c->d_split = true;
903
27692
  d_watches->d_splitWatches.push_back(c);
904
27692
}
905
906
907
743424
void ConstraintDatabase::pushCanBePropagatedWatch(ConstraintP c){
908
743424
  Assert(!c->d_canBePropagated);
909
743424
  c->d_canBePropagated = true;
910
743424
  d_watches->d_canBePropagatedWatches.push_back(c);
911
743424
}
912
913
4827704
void ConstraintDatabase::pushAssertionOrderWatch(ConstraintP c, TNode witness){
914
4827704
  Assert(!c->assertedToTheTheory());
915
4827704
  c->d_assertionOrder = d_watches->d_assertionOrderWatches.size();
916
4827704
  c->d_witness = witness;
917
4827704
  d_watches->d_assertionOrderWatches.push_back(c);
918
4827704
}
919
920
921
7371596
void ConstraintDatabase::pushConstraintRule(const ConstraintRule& crp){
922
7371596
  ConstraintP c = crp.d_constraint;
923
7371596
  Assert(c->d_crid == ConstraintRuleIdSentinel);
924
7371596
  Assert(!c->hasProof());
925
7371596
  c->d_crid = d_watches->d_constraintProofs.size();
926
7371596
  d_watches->d_constraintProofs.push_back(crp);
927
7371596
}
928
929
1376873
ConstraintP ConstraintDatabase::getConstraint(ArithVar v, ConstraintType t, const DeltaRational& r){
930
  //This must always return a constraint.
931
932
1376873
  SortedConstraintMap& scm = getVariableSCM(v);
933
1376873
  pair<SortedConstraintMapIterator, bool> insertAttempt;
934
1376873
  insertAttempt = scm.insert(make_pair(r, ValueCollection()));
935
936
1376873
  SortedConstraintMapIterator pos = insertAttempt.first;
937
1376873
  ValueCollection& vc = pos->second;
938
1376873
  if(vc.hasConstraintOfType(t)){
939
1317964
    return vc.getConstraintOfType(t);
940
  }else{
941
58909
    ConstraintP c = new Constraint(v, t, r);
942
58909
    ConstraintP negC = Constraint::makeNegation(v, t, r);
943
944
58909
    SortedConstraintMapIterator negPos;
945
58909
    if(t == Equality || t == Disequality){
946
4763
      negPos = pos;
947
    }else{
948
54146
      pair<SortedConstraintMapIterator, bool> negInsertAttempt;
949
54146
      negInsertAttempt = scm.insert(make_pair(negC->getValue(), ValueCollection()));
950
54146
      Assert(negInsertAttempt.second
951
             || !negInsertAttempt.first->second.hasConstraintOfType(
952
                 negC->getType()));
953
54146
      negPos = negInsertAttempt.first;
954
    }
955
956
58909
    c->initialize(this, pos, negC);
957
58909
    negC->initialize(this, negPos, c);
958
959
58909
    vc.add(c);
960
58909
    negPos->second.add(negC);
961
962
58909
    return c;
963
  }
964
}
965
966
193968
ConstraintP ConstraintDatabase::ensureConstraint(ValueCollection& vc, ConstraintType t){
967
193968
  if(vc.hasConstraintOfType(t)){
968
187987
    return vc.getConstraintOfType(t);
969
  }else{
970
5981
    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
18918
ConstraintDatabase::~ConstraintDatabase(){
981
9459
  delete d_watches;
982
983
18918
  std::vector<ConstraintP> constraintList;
984
985
305285
  while(!d_varDatabases.empty()){
986
147913
    PerVariableDatabase* back = d_varDatabases.back();
987
988
147913
    SortedConstraintMap& scm = back->d_constraints;
989
147913
    SortedConstraintMapIterator i = scm.begin(), i_end = scm.end();
990
967409
    for(; i != i_end; ++i){
991
409748
      (i->second).push_into(constraintList);
992
    }
993
1348405
    while(!constraintList.empty()){
994
600246
      ConstraintP c = constraintList.back();
995
600246
      constraintList.pop_back();
996
600246
      delete c;
997
    }
998
147913
    Assert(scm.empty());
999
147913
    d_varDatabases.pop_back();
1000
147913
    delete back;
1001
  }
1002
1003
9459
  Assert(d_nodetoConstraintMap.empty());
1004
9459
}
1005
1006
9459
ConstraintDatabase::Statistics::Statistics()
1007
9459
    : d_unatePropagateCalls(smtStatisticsRegistry().registerInt(
1008
18918
        "theory::arith::cd::unatePropagateCalls")),
1009
9459
      d_unatePropagateImplications(smtStatisticsRegistry().registerInt(
1010
18918
          "theory::arith::cd::unatePropagateImplications"))
1011
{
1012
9459
}
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
147913
void ConstraintDatabase::addVariable(ArithVar v){
1023
147913
  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
147913
    Debug("arith::constraint") << "about to fail" << v << " " << d_varDatabases.size() << endl;
1042
147913
    Assert(v == d_varDatabases.size());
1043
147913
    d_varDatabases.push_back(new PerVariableDatabase(v));
1044
  }
1045
147913
}
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
601656
bool Constraint::contextDependentDataIsSet() const{
1059
601656
  return hasProof() || isSplit() || canBePropagated() || assertedToTheTheory();
1060
}
1061
1062
13846
TrustNode Constraint::split()
1063
{
1064
13846
  Assert(isEquality() || isDisequality());
1065
1066
13846
  bool isEq = isEquality();
1067
1068
13846
  ConstraintP eq = isEq ? this : d_negation;
1069
13846
  ConstraintP diseq = isEq ? d_negation : this;
1070
1071
27692
  TNode eqNode = eq->getLiteral();
1072
13846
  Assert(eqNode.getKind() == kind::EQUAL);
1073
27692
  TNode lhs = eqNode[0];
1074
27692
  TNode rhs = eqNode[1];
1075
1076
27692
  Node leqNode = NodeBuilder(kind::LEQ) << lhs << rhs;
1077
27692
  Node ltNode = NodeBuilder(kind::LT) << lhs << rhs;
1078
27692
  Node gtNode = NodeBuilder(kind::GT) << lhs << rhs;
1079
27692
  Node geqNode = NodeBuilder(kind::GEQ) << lhs << rhs;
1080
1081
27692
  Node lemma = NodeBuilder(OR) << leqNode << geqNode;
1082
1083
13846
  TrustNode trustedLemma;
1084
13846
  if (d_database->isProofEnabled())
1085
  {
1086
    // Farkas proof that this works.
1087
2080
    auto nm = NodeManager::currentNM();
1088
4160
    auto nLeqPf = d_database->d_pnm->mkAssume(leqNode.negate());
1089
2080
    auto gtPf = d_database->d_pnm->mkNode(
1090
4160
        PfRule::MACRO_SR_PRED_TRANSFORM, {nLeqPf}, {gtNode});
1091
4160
    auto nGeqPf = d_database->d_pnm->mkAssume(geqNode.negate());
1092
2080
    auto ltPf = d_database->d_pnm->mkNode(
1093
4160
        PfRule::MACRO_SR_PRED_TRANSFORM, {nGeqPf}, {ltNode});
1094
2080
    auto sumPf = d_database->d_pnm->mkNode(
1095
        PfRule::MACRO_ARITH_SCALE_SUM_UB,
1096
        {gtPf, ltPf},
1097
4160
        {nm->mkConst<Rational>(-1), nm->mkConst<Rational>(1)});
1098
2080
    auto botPf = d_database->d_pnm->mkNode(
1099
4160
        PfRule::MACRO_SR_PRED_TRANSFORM, {sumPf}, {nm->mkConst(false)});
1100
4160
    std::vector<Node> a = {leqNode.negate(), geqNode.negate()};
1101
4160
    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
4160
        d_database->d_pnm->mkNode(PfRule::NOT_AND, {notAndNotPf}, {});
1106
2080
    auto orPf = d_database->d_pnm->mkNode(
1107
4160
        PfRule::MACRO_SR_PRED_TRANSFORM, {orNotNotPf}, {lemma});
1108
2080
    trustedLemma = d_database->d_pfGen->mkTrustNode(lemma, orPf);
1109
  }
1110
  else
1111
  {
1112
11766
    trustedLemma = TrustNode::mkTrustLemma(lemma);
1113
  }
1114
1115
13846
  eq->d_database->pushSplitWatch(eq);
1116
13846
  diseq->d_database->pushSplitWatch(diseq);
1117
1118
27692
  return trustedLemma;
1119
}
1120
1121
970497
bool ConstraintDatabase::hasLiteral(TNode literal) const {
1122
970497
  return lookup(literal) != NullConstraint;
1123
}
1124
1125
242624
ConstraintP ConstraintDatabase::addLiteral(TNode literal){
1126
242624
  Assert(!hasLiteral(literal));
1127
242624
  bool isNot = (literal.getKind() == NOT);
1128
485248
  Node atomNode = (isNot ? literal[0] : literal);
1129
485248
  Node negationNode  = atomNode.notNode();
1130
1131
242624
  Assert(!hasLiteral(atomNode));
1132
242624
  Assert(!hasLiteral(negationNode));
1133
485248
  Comparison posCmp = Comparison::parseNormalForm(atomNode);
1134
1135
242624
  ConstraintType posType = Constraint::constraintTypeOfComparison(posCmp);
1136
1137
485248
  Polynomial nvp = posCmp.normalizedVariablePart();
1138
242624
  ArithVar v = d_avariables.asArithVar(nvp.getNode());
1139
1140
485248
  DeltaRational posDR = posCmp.normalizedDeltaRational();
1141
1142
242624
  ConstraintP posC = new Constraint(v, posType, posDR);
1143
1144
242624
  Debug("arith::constraint") << "addliteral( literal ->" << literal << ")" << endl;
1145
242624
  Debug("arith::constraint") << "addliteral( posC ->" << posC << ")" << endl;
1146
1147
242624
  SortedConstraintMap& scm = getVariableSCM(posC->getVariable());
1148
242624
  pair<SortedConstraintMapIterator, bool> insertAttempt;
1149
242624
  insertAttempt = scm.insert(make_pair(posC->getValue(), ValueCollection()));
1150
1151
242624
  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
242624
  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
1410
    ConstraintP hit = posI->second.getConstraintOfType(posC->getType());
1159
1410
    Debug("arith::constraint") << "hit " << hit << endl;
1160
1410
    Debug("arith::constraint") << "posC " << posC << endl;
1161
1162
1410
    delete posC;
1163
1164
1410
    hit->setLiteral(atomNode);
1165
1410
    hit->getNegation()->setLiteral(negationNode);
1166
1410
    return isNot ? hit->getNegation(): hit;
1167
  }else{
1168
482428
    Comparison negCmp = Comparison::parseNormalForm(negationNode);
1169
1170
241214
    ConstraintType negType = Constraint::constraintTypeOfComparison(negCmp);
1171
482428
    DeltaRational negDR = negCmp.normalizedDeltaRational();
1172
1173
241214
    ConstraintP negC = new Constraint(v, negType, negDR);
1174
1175
241214
    SortedConstraintMapIterator negI;
1176
1177
241214
    if(posC->isEquality()){
1178
105344
      negI = posI;
1179
    }else{
1180
135870
      Assert(posC->isLowerBound() || posC->isUpperBound());
1181
1182
135870
      pair<SortedConstraintMapIterator, bool> negInsertAttempt;
1183
135870
      negInsertAttempt = scm.insert(make_pair(negC->getValue(), ValueCollection()));
1184
1185
135870
      Debug("nf::tmp") << "sdhjfgdhjkldfgljkhdfg" << endl;
1186
135870
      Debug("nf::tmp") << negC << endl;
1187
135870
      Debug("nf::tmp") << negC->getValue() << endl;
1188
1189
      //This should always succeed as the DeltaRational for the negation is unique!
1190
135870
      Assert(negInsertAttempt.second);
1191
1192
135870
      negI = negInsertAttempt.first;
1193
    }
1194
1195
241214
    (posI->second).add(posC);
1196
241214
    (negI->second).add(negC);
1197
1198
241214
    posC->initialize(this, posI, negC);
1199
241214
    negC->initialize(this, negI, posC);
1200
1201
241214
    posC->setLiteral(atomNode);
1202
241214
    negC->setLiteral(negationNode);
1203
1204
241214
    return isNot ? negC : posC;
1205
  }
1206
}
1207
1208
1209
7971136
ConstraintP ConstraintDatabase::lookup(TNode literal) const{
1210
7971136
  NodetoConstraintMap::const_iterator iter = d_nodetoConstraintMap.find(literal);
1211
7971136
  if(iter == d_nodetoConstraintMap.end()){
1212
1538091
    return NullConstraint;
1213
  }else{
1214
6433045
    return iter->second;
1215
  }
1216
}
1217
1218
4119438
void Constraint::setAssumption(bool nowInConflict){
1219
4119438
  Debug("constraints::pf") << "setAssumption(" << this << ")" << std::endl;
1220
4119438
  Assert(!hasProof());
1221
4119438
  Assert(negationHasProof() == nowInConflict);
1222
4119438
  Assert(hasLiteral());
1223
4119438
  Assert(assertedToTheTheory());
1224
1225
4119438
  d_database->pushConstraintRule(ConstraintRule(this, AssumeAP));
1226
1227
4119438
  Assert(inConflict() == nowInConflict);
1228
4119438
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1229
    Debug("constraint::conflictCommit") << "inConflict@setAssumption " << this << std::endl;
1230
  }
1231
4119438
}
1232
1233
3039602
void Constraint::tryToPropagate(){
1234
3039602
  Assert(hasProof());
1235
3039602
  Assert(!isAssumption());
1236
3039602
  Assert(!isInternalAssumption());
1237
1238
3039602
  if(canBePropagated() && !assertedToTheTheory() && !isAssumption() && !isInternalAssumption()){
1239
549317
    propagate();
1240
  }
1241
3039602
}
1242
1243
559060
void Constraint::propagate(){
1244
559060
  Assert(hasProof());
1245
559060
  Assert(canBePropagated());
1246
559060
  Assert(!assertedToTheTheory());
1247
559060
  Assert(!isAssumption());
1248
559060
  Assert(!isInternalAssumption());
1249
1250
559060
  d_database->d_toPropagate.push(this);
1251
559060
}
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
1690253
void Constraint::impliedByUnate(ConstraintCP imp, bool nowInConflict){
1262
1690253
  Debug("constraints::pf") << "impliedByUnate(" << this << ", " << *imp << ")" << std::endl;
1263
1690253
  Assert(!hasProof());
1264
1690253
  Assert(imp->hasProof());
1265
1690253
  Assert(negationHasProof() == nowInConflict);
1266
1267
1690253
  d_database->d_antecedents.push_back(NullConstraint);
1268
1690253
  d_database->d_antecedents.push_back(imp);
1269
1270
1690253
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1271
1272
  RationalVectorP coeffs;
1273
1690253
  if (ARITH_PROOF_ON())
1274
  {
1275
779326
    std::pair<int, int> sgns = unateFarkasSigns(getNegation(), imp);
1276
1277
1558652
    Rational first(sgns.first);
1278
1558652
    Rational second(sgns.second);
1279
1280
779326
    coeffs = new RationalVector();
1281
779326
    coeffs->push_back(first);
1282
779326
    coeffs->push_back(second);
1283
  }
1284
  else
1285
  {
1286
910927
    coeffs = RationalVectorPSentinel;
1287
  }
1288
  // no need to delete coeffs the memory is owned by ConstraintRule
1289
1690253
  d_database->pushConstraintRule(ConstraintRule(this, FarkasAP, antecedentEnd, coeffs));
1290
1291
1690253
  Assert(inConflict() == nowInConflict);
1292
1690253
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1293
    Debug("constraint::conflictCommit") << "inConflict@impliedByUnate " << this << std::endl;
1294
  }
1295
1296
1690253
  if(Debug.isOn("constraints::wffp") && !wellFormedFarkasProof()){
1297
    getConstraintRule().print(Debug("constraints::wffp"));
1298
  }
1299
1690253
  Assert(wellFormedFarkasProof());
1300
1690253
}
1301
1302
420184
void Constraint::impliedByTrichotomy(ConstraintCP a, ConstraintCP b, bool nowInConflict){
1303
420184
  Debug("constraints::pf") << "impliedByTrichotomy(" << this << ", " << *a << ", ";
1304
420184
  Debug("constraints::pf") << *b << ")" << std::endl;
1305
420184
  Assert(!hasProof());
1306
420184
  Assert(negationHasProof() == nowInConflict);
1307
420184
  Assert(a->hasProof());
1308
420184
  Assert(b->hasProof());
1309
1310
420184
  d_database->d_antecedents.push_back(NullConstraint);
1311
420184
  d_database->d_antecedents.push_back(a);
1312
420184
  d_database->d_antecedents.push_back(b);
1313
1314
420184
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1315
420184
  d_database->pushConstraintRule(ConstraintRule(this, TrichotomyAP, antecedentEnd));
1316
1317
420184
  Assert(inConflict() == nowInConflict);
1318
420184
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1319
    Debug("constraint::conflictCommit") << "inConflict@impliedByTrichotomy " << this << std::endl;
1320
  }
1321
420184
}
1322
1323
1324
84457
bool Constraint::allHaveProof(const ConstraintCPVec& b){
1325
1271333
  for(ConstraintCPVec::const_iterator i=b.begin(), i_end=b.end(); i != i_end; ++i){
1326
1186876
    ConstraintCP cp = *i;
1327
1186876
    if(! (cp->hasProof())){ return false; }
1328
  }
1329
84457
  return true;
1330
}
1331
1332
905834
void Constraint::impliedByIntTighten(ConstraintCP a, bool nowInConflict){
1333
905834
  Debug("constraints::pf") << "impliedByIntTighten(" << this << ", " << *a << ")" << std::endl;
1334
905834
  Assert(!hasProof());
1335
905834
  Assert(negationHasProof() == nowInConflict);
1336
905834
  Assert(a->hasProof());
1337
1811668
  Debug("pf::arith") << "impliedByIntTighten(" << this << ", " << a << ")"
1338
905834
                     << std::endl;
1339
1340
905834
  d_database->d_antecedents.push_back(NullConstraint);
1341
905834
  d_database->d_antecedents.push_back(a);
1342
905834
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1343
905834
  d_database->pushConstraintRule(ConstraintRule(this, IntTightenAP, antecedentEnd));
1344
1345
905834
  Assert(inConflict() == nowInConflict);
1346
905834
  if(inConflict()){
1347
2672
    Debug("constraint::conflictCommit") << "inConflict impliedByIntTighten" << this << std::endl;
1348
  }
1349
905834
}
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
84457
void Constraint::impliedByFarkas(const ConstraintCPVec& a, RationalVectorCP coeffs, bool nowInConflict){
1408
84457
  Debug("constraints::pf") << "impliedByFarkas(" << this;
1409
84457
  if (Debug.isOn("constraints::pf")) {
1410
    for (const ConstraintCP& p : a)
1411
    {
1412
      Debug("constraints::pf") << ", " << p;
1413
    }
1414
  }
1415
84457
  Debug("constraints::pf") << ", <coeffs>";
1416
84457
  Debug("constraints::pf") << ")" << std::endl;
1417
84457
  Assert(!hasProof());
1418
84457
  Assert(negationHasProof() == nowInConflict);
1419
84457
  Assert(allHaveProof(a));
1420
1421
84457
  Assert(ARITH_PROOF_ON() == (coeffs != RationalVectorCPSentinel));
1422
84457
  Assert(!ARITH_PROOF_ON() || coeffs->size() == a.size() + 1);
1423
1424
84457
  Assert(a.size() >= 1);
1425
1426
84457
  d_database->d_antecedents.push_back(NullConstraint);
1427
1271333
  for(ConstraintCPVec::const_iterator i = a.begin(), end = a.end(); i != end; ++i){
1428
1186876
    ConstraintCP c_i = *i;
1429
1186876
    Assert(c_i->hasProof());
1430
1186876
    d_database->d_antecedents.push_back(c_i);
1431
  }
1432
84457
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1433
1434
  RationalVectorCP coeffsCopy;
1435
84457
  if (ARITH_PROOF_ON())
1436
  {
1437
41948
    Assert(coeffs != RationalVectorCPSentinel);
1438
41948
    coeffsCopy = new RationalVector(*coeffs);
1439
  }
1440
  else
1441
  {
1442
42509
    coeffsCopy = RationalVectorCPSentinel;
1443
  }
1444
84457
  d_database->pushConstraintRule(ConstraintRule(this, FarkasAP, antecedentEnd, coeffsCopy));
1445
1446
84457
  Assert(inConflict() == nowInConflict);
1447
84457
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1448
    Debug("constraint::conflictCommit") << "inConflict@impliedByFarkas " << this << std::endl;
1449
  }
1450
84457
  if(Debug.isOn("constraints::wffp") && !wellFormedFarkasProof()){
1451
    getConstraintRule().print(Debug("constraints::wffp"));
1452
  }
1453
84457
  Assert(wellFormedFarkasProof());
1454
84457
}
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
151430
void Constraint::setEqualityEngineProof(){
1474
151430
  Debug("constraints::pf") << "setEqualityEngineProof(" << this;
1475
151430
  Debug("constraints::pf") << ")" << std::endl;
1476
151430
  Assert(truthIsUnknown());
1477
151430
  Assert(hasLiteral());
1478
151430
  d_database->pushConstraintRule(ConstraintRule(this, EqualityEngineAP));
1479
151430
}
1480
1481
1482
3547858
SortedConstraintMap& Constraint::constraintSet() const{
1483
3547858
  Assert(d_database->variableDatabaseIsSetup(d_variable));
1484
3547858
  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
89267
Node Constraint::externalImplication(const ConstraintCPVec& b) const{
1499
89267
  Assert(hasLiteral());
1500
178534
  Node antecedent = externalExplainByAssertions(b);
1501
178534
  Node implied = getLiteral();
1502
178534
  return antecedent.impNode(implied);
1503
}
1504
1505
1506
819396
Node Constraint::externalExplainByAssertions(const ConstraintCPVec& b){
1507
819396
  return externalExplain(b, AssertionOrderSentinel);
1508
}
1509
1510
11316
TrustNode Constraint::externalExplainForPropagation(TNode lit) const
1511
{
1512
11316
  Assert(hasProof());
1513
11316
  Assert(!isAssumption());
1514
11316
  Assert(!isInternalAssumption());
1515
22632
  NodeBuilder nb(Kind::AND);
1516
22632
  auto pfFromAssumptions = externalExplain(nb, d_assertionOrder);
1517
22632
  Node n = safeConstructNary(nb);
1518
11316
  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
1660
    Assert(Rewriter::rewrite(lit) == getLiteral());
1523
3320
    std::vector<Node> assumptions;
1524
1660
    if (n.getKind() == Kind::AND)
1525
    {
1526
970
      assumptions.insert(assumptions.end(), n.begin(), n.end());
1527
    }
1528
    else
1529
    {
1530
690
      assumptions.push_back(n);
1531
    }
1532
1660
    if (getProofLiteral() != lit)
1533
    {
1534
3342
      pfFromAssumptions = d_database->d_pnm->mkNode(
1535
2228
          PfRule::MACRO_SR_PRED_TRANSFORM, {pfFromAssumptions}, {lit});
1536
    }
1537
3320
    auto pf = d_database->d_pnm->mkScope(pfFromAssumptions, assumptions);
1538
1660
    return d_database->d_pfGen->mkTrustedPropagation(
1539
1660
        lit, safeConstructNary(Kind::AND, assumptions), pf);
1540
  }
1541
  else
1542
  {
1543
9656
    return TrustNode::mkTrustPropExp(lit, n);
1544
  }
1545
}
1546
1547
67172
TrustNode Constraint::externalExplainConflict() const
1548
{
1549
67172
  Debug("pf::arith::explain") << this << std::endl;
1550
67172
  Assert(inConflict());
1551
134344
  NodeBuilder nb(kind::AND);
1552
134344
  auto pf1 = externalExplainByAssertions(nb);
1553
134344
  auto not2 = getNegation()->getProofLiteral().negate();
1554
134344
  auto pf2 = getNegation()->externalExplainByAssertions(nb);
1555
134344
  Node n = safeConstructNary(nb);
1556
67172
  if (d_database->isProofEnabled())
1557
  {
1558
11087
    auto pfNot2 = d_database->d_pnm->mkNode(
1559
22174
        PfRule::MACRO_SR_PRED_TRANSFORM, {pf1}, {not2});
1560
22174
    std::vector<Node> lits;
1561
11087
    if (n.getKind() == Kind::AND)
1562
    {
1563
11087
      lits.insert(lits.end(), n.begin(), n.end());
1564
    }
1565
    else
1566
    {
1567
      lits.push_back(n);
1568
    }
1569
11087
    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
22174
                                    getNegation()->getProofLiteral()};
1579
    auto bot =
1580
11087
        not2.getKind() == Kind::NOT
1581
30084
            ? d_database->d_pnm->mkNode(PfRule::CONTRA, {pf2, pfNot2}, {})
1582
43289
            : d_database->d_pnm->mkNode(PfRule::CONTRA, {pfNot2, pf2}, {});
1583
11087
    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
22174
    auto confPf = d_database->d_pnm->mkScope(bot, lits);
1592
11087
    return d_database->d_pfGen->mkTrustNode(
1593
11087
        safeConstructNary(Kind::AND, lits), confPf, true);
1594
  }
1595
  else
1596
  {
1597
56085
    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
819396
Node Constraint::externalExplain(const ConstraintCPVec& v, AssertionOrder order){
1648
1638792
  NodeBuilder nb(kind::AND);
1649
819396
  ConstraintCPVec::const_iterator i, end;
1650
1857409
  for(i = v.begin(), end = v.end(); i != end; ++i){
1651
1038013
    ConstraintCP v_i = *i;
1652
1038013
    v_i->externalExplain(nb, order);
1653
  }
1654
1638792
  return safeConstructNary(nb);
1655
}
1656
1657
4813575
std::shared_ptr<ProofNode> Constraint::externalExplain(
1658
    NodeBuilder& nb, AssertionOrder order) const
1659
{
1660
4813575
  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
4813575
  Assert(hasProof());
1668
4813575
  Assert(!isAssumption() || assertedToTheTheory());
1669
4813575
  Assert(!isInternalAssumption());
1670
4813575
  std::shared_ptr<ProofNode> pf{};
1671
1672
4813575
  ProofNodeManager* pnm = d_database->d_pnm;
1673
1674
4813575
  if (assertedBefore(order))
1675
  {
1676
4270732
    Debug("pf::arith::explain") << "  already asserted" << std::endl;
1677
4270732
    nb << getWitness();
1678
4270732
    if (d_database->isProofEnabled())
1679
    {
1680
499215
      pf = pnm->mkAssume(getWitness());
1681
      // If the witness and literal differ, prove the difference through a
1682
      // rewrite.
1683
499215
      if (getWitness() != getProofLiteral())
1684
      {
1685
961902
        pf = pnm->mkNode(
1686
641268
            PfRule::MACRO_SR_PRED_TRANSFORM, {pf}, {getProofLiteral()});
1687
      }
1688
    }
1689
  }
1690
542843
  else if (hasEqualityEngineProof())
1691
  {
1692
4340
    Debug("pf::arith::explain") << "  going to ee:" << std::endl;
1693
8680
    TrustNode exp = d_database->eeExplain(this);
1694
4340
    if (d_database->isProofEnabled())
1695
    {
1696
574
      Assert(exp.getProven().getKind() == Kind::IMPLIES);
1697
1148
      std::vector<std::shared_ptr<ProofNode>> hypotheses;
1698
574
      hypotheses.push_back(exp.getGenerator()->getProofFor(exp.getProven()));
1699
574
      if (exp.getNode().getKind() == Kind::AND)
1700
      {
1701
1876
        for (const auto& h : exp.getNode())
1702
        {
1703
1382
          hypotheses.push_back(
1704
2764
              pnm->mkNode(PfRule::TRUE_INTRO, {pnm->mkAssume(h)}, {}));
1705
        }
1706
      }
1707
      else
1708
      {
1709
320
        hypotheses.push_back(pnm->mkNode(
1710
240
            PfRule::TRUE_INTRO, {pnm->mkAssume(exp.getNode())}, {}));
1711
      }
1712
1148
      pf = pnm->mkNode(
1713
574
          PfRule::MACRO_SR_PRED_TRANSFORM, {hypotheses}, {getProofLiteral()});
1714
    }
1715
8680
    Debug("pf::arith::explain")
1716
4340
        << "    explanation: " << exp.getNode() << std::endl;
1717
4340
    if (exp.getNode().getKind() == Kind::AND)
1718
    {
1719
3757
      nb.append(exp.getNode().begin(), exp.getNode().end());
1720
    }
1721
    else
1722
    {
1723
583
      nb << exp.getNode();
1724
    }
1725
  }
1726
  else
1727
  {
1728
538503
    Debug("pf::arith::explain") << "  recursion!" << std::endl;
1729
538503
    Assert(!isAssumption());
1730
538503
    AntecedentId p = getEndAntecedent();
1731
538503
    ConstraintCP antecedent = d_database->d_antecedents[p];
1732
1077006
    std::vector<std::shared_ptr<ProofNode>> children;
1733
1734
3178613
    while (antecedent != NullConstraint)
1735
    {
1736
1320055
      Debug("pf::arith::explain") << "Explain " << antecedent << std::endl;
1737
2640110
      auto pn = antecedent->externalExplain(nb, order);
1738
1320055
      if (d_database->isProofEnabled())
1739
      {
1740
119909
        children.push_back(pn);
1741
      }
1742
1320055
      --p;
1743
1320055
      antecedent = d_database->d_antecedents[p];
1744
    }
1745
1746
538503
    if (d_database->isProofEnabled())
1747
    {
1748
74949
      switch (getProofType())
1749
      {
1750
        case ArithProofType::AssumeAP:
1751
        case ArithProofType::EqualityEngineAP:
1752
        {
1753
          Unreachable() << "These should be handled above";
1754
          break;
1755
        }
1756
11178
        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
22356
          std::vector<std::shared_ptr<ProofNode>> farkasChildren;
1767
11178
          farkasChildren.push_back(
1768
22356
              pnm->mkAssume(getNegation()->getProofLiteral()));
1769
11178
          farkasChildren.insert(
1770
22356
              farkasChildren.end(), children.rbegin(), children.rend());
1771
1772
11178
          NodeManager* nm = NodeManager::currentNM();
1773
1774
          // Enumerate d_farkasCoefficients as nodes.
1775
22356
          std::vector<Node> farkasCoeffs;
1776
74118
          for (Rational r : *getFarkasCoefficients())
1777
          {
1778
62940
            farkasCoeffs.push_back(nm->mkConst<Rational>(r));
1779
          }
1780
1781
          // Apply the scaled-sum rule.
1782
          std::shared_ptr<ProofNode> sumPf = pnm->mkNode(
1783
22356
              PfRule::MACRO_ARITH_SCALE_SUM_UB, farkasChildren, farkasCoeffs);
1784
1785
          // Provable rewrite the result
1786
          auto botPf = pnm->mkNode(
1787
22356
              PfRule::MACRO_SR_PRED_TRANSFORM, {sumPf}, {nm->mkConst(false)});
1788
1789
          // Scope out the negated constraint, yielding a proof of the
1790
          // constraint.
1791
22356
          std::vector<Node> assump{getNegation()->getProofLiteral()};
1792
22356
          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
33534
          pf = pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM,
1799
                           {maybeDoubleNotPf},
1800
22356
                           {getProofLiteral()});
1801
1802
11178
          break;
1803
        }
1804
59395
        case ArithProofType::IntTightenAP:
1805
        {
1806
59395
          if (isUpperBound())
1807
          {
1808
58034
            pf = pnm->mkNode(
1809
116068
                PfRule::INT_TIGHT_UB, children, {}, getProofLiteral());
1810
          }
1811
1361
          else if (isLowerBound())
1812
          {
1813
1361
            pf = pnm->mkNode(
1814
2722
                PfRule::INT_TIGHT_LB, children, {}, getProofLiteral());
1815
          }
1816
          else
1817
          {
1818
            Unreachable();
1819
          }
1820
59395
          break;
1821
        }
1822
        case ArithProofType::IntHoleAP:
1823
        {
1824
          pf = pnm->mkNode(PfRule::INT_TRUST,
1825
                           children,
1826
                           {getProofLiteral()},
1827
                           getProofLiteral());
1828
          break;
1829
        }
1830
4376
        case ArithProofType::TrichotomyAP:
1831
        {
1832
8752
          pf = pnm->mkNode(PfRule::ARITH_TRICHOTOMY,
1833
                           children,
1834
                           {getProofLiteral()},
1835
13128
                           getProofLiteral());
1836
4376
          break;
1837
        }
1838
        case ArithProofType::InternalAssumeAP:
1839
        case ArithProofType::NoAP:
1840
        default:
1841
        {
1842
          Unreachable() << getProofType()
1843
                        << " should not be visible in explanation";
1844
          break;
1845
        }
1846
      }
1847
    }
1848
  }
1849
4813575
  return pf;
1850
}
1851
1852
1833
Node Constraint::externalExplainByAssertions(ConstraintCP a, ConstraintCP b){
1853
3666
  NodeBuilder nb(kind::AND);
1854
1833
  a->externalExplainByAssertions(nb);
1855
1833
  b->externalExplainByAssertions(nb);
1856
3666
  return nb;
1857
}
1858
1859
Node Constraint::externalExplainByAssertions(ConstraintCP a, ConstraintCP b, ConstraintCP c){
1860
  NodeBuilder nb(kind::AND);
1861
  a->externalExplainByAssertions(nb);
1862
  b->externalExplainByAssertions(nb);
1863
  c->externalExplainByAssertions(nb);
1864
  return nb;
1865
}
1866
1867
645450
ConstraintP Constraint::getStrictlyWeakerLowerBound(bool hasLiteral, bool asserted) const {
1868
645450
  Assert(initialized());
1869
645450
  Assert(!asserted || hasLiteral);
1870
1871
645450
  SortedConstraintMapConstIterator i = d_variablePosition;
1872
645450
  const SortedConstraintMap& scm = constraintSet();
1873
645450
  SortedConstraintMapConstIterator i_begin = scm.begin();
1874
2070136
  while(i != i_begin){
1875
819688
    --i;
1876
819688
    const ValueCollection& vc = i->second;
1877
819688
    if(vc.hasLowerBound()){
1878
197240
      ConstraintP weaker = vc.getLowerBound();
1879
1880
      // asserted -> hasLiteral
1881
      // hasLiteral -> weaker->hasLiteral()
1882
      // asserted -> weaker->assertedToTheTheory()
1883
421748
      if((!hasLiteral || (weaker->hasLiteral())) &&
1884
233095
         (!asserted || ( weaker->assertedToTheTheory()))){
1885
107345
        return weaker;
1886
      }
1887
    }
1888
  }
1889
538105
  return NullConstraint;
1890
}
1891
1892
294676
ConstraintP Constraint::getStrictlyWeakerUpperBound(bool hasLiteral, bool asserted) const {
1893
294676
  SortedConstraintMapConstIterator i = d_variablePosition;
1894
294676
  const SortedConstraintMap& scm = constraintSet();
1895
294676
  SortedConstraintMapConstIterator i_end = scm.end();
1896
1897
294676
  ++i;
1898
886116
  for(; i != i_end; ++i){
1899
393600
    const ValueCollection& vc = i->second;
1900
393600
    if(vc.hasUpperBound()){
1901
136555
      ConstraintP weaker = vc.getUpperBound();
1902
341918
      if((!hasLiteral || (weaker->hasLiteral())) &&
1903
210750
         (!asserted || ( weaker->assertedToTheTheory()))){
1904
97880
        return weaker;
1905
      }
1906
    }
1907
  }
1908
1909
196796
  return NullConstraint;
1910
}
1911
1912
5997647
ConstraintP ConstraintDatabase::getBestImpliedBound(ArithVar v, ConstraintType t, const DeltaRational& r) const {
1913
5997647
  Assert(variableDatabaseIsSetup(v));
1914
5997647
  Assert(t == UpperBound || t == LowerBound);
1915
1916
5997647
  SortedConstraintMap& scm = getVariableSCM(v);
1917
5997647
  if(t == UpperBound){
1918
2939806
    SortedConstraintMapConstIterator i = scm.lower_bound(r);
1919
2939806
    SortedConstraintMapConstIterator i_end = scm.end();
1920
2939806
    Assert(i == i_end || r <= i->first);
1921
5676514
    for(; i != i_end; i++){
1922
2624121
      Assert(r <= i->first);
1923
2624121
      const ValueCollection& vc = i->second;
1924
2624121
      if(vc.hasUpperBound()){
1925
1255767
        return vc.getUpperBound();
1926
      }
1927
    }
1928
1684039
    return NullConstraint;
1929
  }else{
1930
3057841
    Assert(t == LowerBound);
1931
3057841
    if(scm.empty()){
1932
197185
      return NullConstraint;
1933
    }else{
1934
2860656
      SortedConstraintMapConstIterator i = scm.lower_bound(r);
1935
2860656
      SortedConstraintMapConstIterator i_begin = scm.begin();
1936
2860656
      SortedConstraintMapConstIterator i_end = scm.end();
1937
2860656
      Assert(i == i_end || r <= i->first);
1938
1939
2860656
      int fdj = 0;
1940
1941
2860656
      if(i == i_end){
1942
1152541
        --i;
1943
1152541
        Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl;
1944
1708115
      }else if( (i->first) > r){
1945
472039
        if(i == i_begin){
1946
431256
          return NullConstraint;
1947
        }else{
1948
40783
          --i;
1949
40783
          Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl;
1950
        }
1951
      }
1952
1953
      do{
1954
2667846
        Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl;
1955
2667846
        Assert(r >= i->first);
1956
2667846
        const ValueCollection& vc = i->second;
1957
1958
2667846
        if(vc.hasLowerBound()){
1959
1433347
          return vc.getLowerBound();
1960
        }
1961
1962
1234499
        if(i == i_begin){
1963
996053
          break;
1964
        }else{
1965
238446
          --i;
1966
238446
        }
1967
      }while(true);
1968
996053
      return NullConstraint;
1969
    }
1970
  }
1971
}
1972
4340
TrustNode ConstraintDatabase::eeExplain(const Constraint* const c) const
1973
{
1974
4340
  Assert(c->hasLiteral());
1975
4340
  return d_congruenceManager.explain(c->getLiteral());
1976
}
1977
1978
void ConstraintDatabase::eeExplain(ConstraintCP c, NodeBuilder& nb) const
1979
{
1980
  Assert(c->hasLiteral());
1981
  // NOTE: this is not a recommended method since it ignores proofs
1982
  d_congruenceManager.explain(c->getLiteral(), nb);
1983
}
1984
1985
17685621
bool ConstraintDatabase::variableDatabaseIsSetup(ArithVar v) const {
1986
17685621
  return v < d_varDatabases.size();
1987
}
1988
1989
1990
9459
ConstraintDatabase::Watches::Watches(context::Context* satContext, context::Context* userContext):
1991
  d_constraintProofs(satContext),
1992
  d_canBePropagatedWatches(satContext),
1993
  d_assertionOrderWatches(satContext),
1994
9459
  d_splitWatches(userContext)
1995
9459
{}
1996
1997
1998
485248
void Constraint::setLiteral(Node n) {
1999
485248
  Debug("arith::constraint") << "Mapping " << *this << " to " << n << std::endl;
2000
485248
  Assert(Comparison::isNormalAtom(n));
2001
485248
  Assert(!hasLiteral());
2002
485248
  Assert(sanityChecking(n));
2003
485248
  d_literal = n;
2004
485248
  NodetoConstraintMap& map = d_database->d_nodetoConstraintMap;
2005
485248
  Assert(map.find(n) == map.end());
2006
485248
  map.insert(make_pair(d_literal, this));
2007
485248
}
2008
2009
1062326
Node Constraint::getProofLiteral() const
2010
{
2011
1062326
  Assert(d_database != nullptr);
2012
1062326
  Assert(d_database->d_avariables.hasNode(d_variable));
2013
2124652
  Node varPart = d_database->d_avariables.asNode(d_variable);
2014
  Kind cmp;
2015
1062326
  bool neg = false;
2016
1062326
  switch (d_type)
2017
  {
2018
440958
    case ConstraintType::UpperBound:
2019
    {
2020
440958
      if (d_value.infinitesimalIsZero())
2021
      {
2022
136505
        cmp = Kind::LEQ;
2023
      }
2024
      else
2025
      {
2026
304453
        cmp = Kind::LT;
2027
      }
2028
440958
      break;
2029
    }
2030
195812
    case ConstraintType::LowerBound:
2031
    {
2032
195812
      if (d_value.infinitesimalIsZero())
2033
      {
2034
157547
        cmp = Kind::GEQ;
2035
      }
2036
      else
2037
      {
2038
38265
        cmp = Kind::GT;
2039
      }
2040
195812
      break;
2041
    }
2042
329387
    case ConstraintType::Equality:
2043
    {
2044
329387
      cmp = Kind::EQUAL;
2045
329387
      break;
2046
    }
2047
96169
    case ConstraintType::Disequality:
2048
    {
2049
96169
      cmp = Kind::EQUAL;
2050
96169
      neg = true;
2051
96169
      break;
2052
    }
2053
    default: Unreachable() << d_type;
2054
  }
2055
1062326
  NodeManager* nm = NodeManager::currentNM();
2056
2124652
  Node constPart = nm->mkConst<Rational>(d_value.getNoninfinitesimalPart());
2057
2124652
  Node posLit = nm->mkNode(cmp, varPart, constPart);
2058
2124652
  return neg ? posLit.negate() : posLit;
2059
}
2060
2061
38987
void ConstraintDatabase::proveOr(std::vector<TrustNode>& out,
2062
                                 ConstraintP a,
2063
                                 ConstraintP b,
2064
                                 bool negateSecond) const
2065
{
2066
77974
  Node la = a->getLiteral();
2067
77974
  Node lb = b->getLiteral();
2068
77974
  Node orN = (la < lb) ? la.orNode(lb) : lb.orNode(la);
2069
38987
  if (isProofEnabled())
2070
  {
2071
6433
    Assert(b->getNegation()->getType() != ConstraintType::Disequality);
2072
6433
    auto nm = NodeManager::currentNM();
2073
6433
    auto pf_neg_la = d_pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM,
2074
12866
                                   {d_pnm->mkAssume(la.negate())},
2075
25732
                                   {a->getNegation()->getProofLiteral()});
2076
6433
    auto pf_neg_lb = d_pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM,
2077
12866
                                   {d_pnm->mkAssume(lb.negate())},
2078
25732
                                   {b->getNegation()->getProofLiteral()});
2079
6433
    int sndSign = negateSecond ? -1 : 1;
2080
    auto bot_pf =
2081
6433
        d_pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM,
2082
6433
                      {d_pnm->mkNode(PfRule::MACRO_ARITH_SCALE_SUM_UB,
2083
                                     {pf_neg_la, pf_neg_lb},
2084
                                     {nm->mkConst<Rational>(-1 * sndSign),
2085
25732
                                      nm->mkConst<Rational>(sndSign)})},
2086
32165
                      {nm->mkConst(false)});
2087
12866
    std::vector<Node> as;
2088
19299
    std::transform(orN.begin(), orN.end(), std::back_inserter(as), [](Node n) {
2089
      return n.negate();
2090
19299
    });
2091
    // No need to ensure that the expected node aggrees with `as` because we
2092
    // are not providing an expected node.
2093
6433
    auto pf = d_pnm->mkNode(
2094
        PfRule::MACRO_SR_PRED_TRANSFORM,
2095
19299
        {d_pnm->mkNode(PfRule::NOT_AND, {d_pnm->mkScope(bot_pf, as)}, {})},
2096
32165
        {orN});
2097
6433
    out.push_back(d_pfGen->mkTrustNode(orN, pf));
2098
  }
2099
  else
2100
  {
2101
32554
    out.push_back(TrustNode::mkTrustLemma(orN));
2102
  }
2103
38987
}
2104
2105
36953
void ConstraintDatabase::implies(std::vector<TrustNode>& out,
2106
                                 ConstraintP a,
2107
                                 ConstraintP b) const
2108
{
2109
73906
  Node la = a->getLiteral();
2110
73906
  Node lb = b->getLiteral();
2111
2112
73906
  Node neg_la = (la.getKind() == kind::NOT)? la[0] : la.notNode();
2113
2114
36953
  Assert(lb != neg_la);
2115
36953
  Assert(b->getNegation()->getType() == ConstraintType::LowerBound
2116
         || b->getNegation()->getType() == ConstraintType::UpperBound);
2117
36953
  proveOr(out,
2118
          a->getNegation(),
2119
          b,
2120
36953
          b->getNegation()->getType() == ConstraintType::LowerBound);
2121
36953
}
2122
2123
2034
void ConstraintDatabase::mutuallyExclusive(std::vector<TrustNode>& out,
2124
                                           ConstraintP a,
2125
                                           ConstraintP b) const
2126
{
2127
4068
  Node la = a->getLiteral();
2128
4068
  Node lb = b->getLiteral();
2129
2130
4068
  Node neg_la = la.negate();
2131
4068
  Node neg_lb = lb.negate();
2132
2034
  proveOr(out, a->getNegation(), b->getNegation(), true);
2133
2034
}
2134
2135
56612
void ConstraintDatabase::outputUnateInequalityLemmas(
2136
    std::vector<TrustNode>& out, ArithVar v) const
2137
{
2138
56612
  SortedConstraintMap& scm = getVariableSCM(v);
2139
56612
  SortedConstraintMapConstIterator scm_iter = scm.begin();
2140
56612
  SortedConstraintMapConstIterator scm_end = scm.end();
2141
56612
  ConstraintP prev = NullConstraint;
2142
  //get transitive unates
2143
  //Only lower bounds or upperbounds should be done.
2144
311294
  for(; scm_iter != scm_end; ++scm_iter){
2145
127341
    const ValueCollection& vc = scm_iter->second;
2146
127341
    if(vc.hasUpperBound()){
2147
62055
      ConstraintP ub = vc.getUpperBound();
2148
62055
      if(ub->hasLiteral()){
2149
62055
        if(prev != NullConstraint){
2150
29762
          implies(out, prev, ub);
2151
        }
2152
62055
        prev = ub;
2153
      }
2154
    }
2155
  }
2156
56612
}
2157
2158
56612
void ConstraintDatabase::outputUnateEqualityLemmas(std::vector<TrustNode>& out,
2159
                                                   ArithVar v) const
2160
{
2161
113224
  vector<ConstraintP> equalities;
2162
2163
56612
  SortedConstraintMap& scm = getVariableSCM(v);
2164
56612
  SortedConstraintMapConstIterator scm_iter = scm.begin();
2165
56612
  SortedConstraintMapConstIterator scm_end = scm.end();
2166
2167
311294
  for(; scm_iter != scm_end; ++scm_iter){
2168
127341
    const ValueCollection& vc = scm_iter->second;
2169
127341
    if(vc.hasEquality()){
2170
16223
      ConstraintP eq = vc.getEquality();
2171
16223
      if(eq->hasLiteral()){
2172
16223
        equalities.push_back(eq);
2173
      }
2174
    }
2175
  }
2176
2177
56612
  vector<ConstraintP>::const_iterator i, j, eq_end = equalities.end();
2178
72835
  for(i = equalities.begin(); i != eq_end; ++i){
2179
16223
    ConstraintP at_i = *i;
2180
18257
    for(j= i + 1; j != eq_end; ++j){
2181
2034
      ConstraintP at_j = *j;
2182
2183
2034
      mutuallyExclusive(out, at_i, at_j);
2184
    }
2185
  }
2186
2187
72835
  for(i = equalities.begin(); i != eq_end; ++i){
2188
16223
    ConstraintP eq = *i;
2189
16223
    const ValueCollection& vc = eq->getValueCollection();
2190
16223
    Assert(vc.hasEquality() && vc.getEquality()->hasLiteral());
2191
2192
16223
    bool hasLB = vc.hasLowerBound() && vc.getLowerBound()->hasLiteral();
2193
16223
    bool hasUB = vc.hasUpperBound() && vc.getUpperBound()->hasLiteral();
2194
2195
16223
    ConstraintP lb = hasLB ?
2196
16223
      vc.getLowerBound() : eq->getStrictlyWeakerLowerBound(true, false);
2197
16223
    ConstraintP ub = hasUB ?
2198
16223
      vc.getUpperBound() : eq->getStrictlyWeakerUpperBound(true, false);
2199
2200
16223
    if(hasUB && hasLB && !eq->isSplit()){
2201
115
      out.push_back(eq->split());
2202
    }
2203
16223
    if(lb != NullConstraint){
2204
2788
      implies(out, eq, lb);
2205
    }
2206
16223
    if(ub != NullConstraint){
2207
4403
      implies(out, eq, ub);
2208
    }
2209
  }
2210
56612
}
2211
2212
7362
void ConstraintDatabase::outputUnateEqualityLemmas(
2213
    std::vector<TrustNode>& lemmas) const
2214
{
2215
63974
  for(ArithVar v = 0, N = d_varDatabases.size(); v < N; ++v){
2216
56612
    outputUnateEqualityLemmas(lemmas, v);
2217
  }
2218
7362
}
2219
2220
7362
void ConstraintDatabase::outputUnateInequalityLemmas(
2221
    std::vector<TrustNode>& lemmas) const
2222
{
2223
63974
  for(ArithVar v = 0, N = d_varDatabases.size(); v < N; ++v){
2224
56612
    outputUnateInequalityLemmas(lemmas, v);
2225
  }
2226
7362
}
2227
2228
3512142
bool ConstraintDatabase::handleUnateProp(ConstraintP ant, ConstraintP cons){
2229
3512142
  if(cons->negationHasProof()){
2230
    Debug("arith::unate") << "handleUnate: " << ant << " implies " << cons << endl;
2231
    cons->impliedByUnate(ant, true);
2232
    d_raiseConflict.raiseConflict(cons, InferenceId::UNKNOWN);
2233
    return true;
2234
3512142
  }else if(!cons->isTrue()){
2235
1685416
    ++d_statistics.d_unatePropagateImplications;
2236
1685416
    Debug("arith::unate") << "handleUnate: " << ant << " implies " << cons << endl;
2237
1685416
    cons->impliedByUnate(ant, false);
2238
1685416
    cons->tryToPropagate();
2239
1685416
    return false;
2240
  } else {
2241
1826726
    return false;
2242
  }
2243
}
2244
2245
1017227
void ConstraintDatabase::unatePropLowerBound(ConstraintP curr, ConstraintP prev){
2246
1017227
  Debug("arith::unate") << "unatePropLowerBound " << curr << " " << prev << endl;
2247
1017227
  Assert(curr != prev);
2248
1017227
  Assert(curr != NullConstraint);
2249
1017227
  bool hasPrev = ! (prev == NullConstraint);
2250
1017227
  Assert(!hasPrev || curr->getValue() > prev->getValue());
2251
2252
1017227
  ++d_statistics.d_unatePropagateCalls;
2253
2254
1017227
  const SortedConstraintMap& scm = curr->constraintSet();
2255
1017227
  const SortedConstraintMapConstIterator scm_begin = scm.begin();
2256
1017227
  SortedConstraintMapConstIterator scm_i = curr->d_variablePosition;
2257
2258
  //Ignore the first ValueCollection
2259
  // NOPE: (>= p c) then (= p c) NOPE
2260
  // NOPE: (>= p c) then (not (= p c)) NOPE
2261
2262
5899423
  while(scm_i != scm_begin){
2263
2654337
    --scm_i; // move the iterator back
2264
2265
2654337
    const ValueCollection& vc = scm_i->second;
2266
2267
    //If it has the previous element, do nothing and stop!
2268
3443381
    if(hasPrev &&
2269
789044
       vc.hasConstraintOfType(prev->getType())
2270
3155567
       && vc.getConstraintOfType(prev->getType()) == prev){
2271
213239
      break;
2272
    }
2273
2274
    //Don't worry about implying the negation of upperbound.
2275
    //These should all be handled by propagating the LowerBounds!
2276
2441098
    if(vc.hasLowerBound()){
2277
912226
      ConstraintP lb = vc.getLowerBound();
2278
912226
      if(handleUnateProp(curr, lb)){ return; }
2279
    }
2280
2441098
    if(vc.hasDisequality()){
2281
180025
      ConstraintP dis = vc.getDisequality();
2282
180025
      if(handleUnateProp(curr, dis)){ return; }
2283
    }
2284
  }
2285
}
2286
2287
941139
void ConstraintDatabase::unatePropUpperBound(ConstraintP curr, ConstraintP prev){
2288
941139
  Debug("arith::unate") << "unatePropUpperBound " << curr << " " << prev << endl;
2289
941139
  Assert(curr != prev);
2290
941139
  Assert(curr != NullConstraint);
2291
941139
  bool hasPrev = ! (prev == NullConstraint);
2292
941139
  Assert(!hasPrev || curr->getValue() < prev->getValue());
2293
2294
941139
  ++d_statistics.d_unatePropagateCalls;
2295
2296
941139
  const SortedConstraintMap& scm = curr->constraintSet();
2297
941139
  const SortedConstraintMapConstIterator scm_end = scm.end();
2298
941139
  SortedConstraintMapConstIterator scm_i = curr->d_variablePosition;
2299
941139
  ++scm_i;
2300
6264387
  for(; scm_i != scm_end; ++scm_i){
2301
2812685
    const ValueCollection& vc = scm_i->second;
2302
2303
    //If it has the previous element, do nothing and stop!
2304
3288702
    if(hasPrev &&
2305
3109854
       vc.hasConstraintOfType(prev->getType()) &&
2306
297169
       vc.getConstraintOfType(prev->getType()) == prev){
2307
151061
      break;
2308
    }
2309
    //Don't worry about implying the negation of upperbound.
2310
    //These should all be handled by propagating the UpperBounds!
2311
2661624
    if(vc.hasUpperBound()){
2312
1028780
      ConstraintP ub = vc.getUpperBound();
2313
1028780
      if(handleUnateProp(curr, ub)){ return; }
2314
    }
2315
2661624
    if(vc.hasDisequality()){
2316
190861
      ConstraintP dis = vc.getDisequality();
2317
190861
      if(handleUnateProp(curr, dis)){ return; }
2318
    }
2319
  }
2320
}
2321
2322
649366
void ConstraintDatabase::unatePropEquality(ConstraintP curr, ConstraintP prevLB, ConstraintP prevUB){
2323
649366
  Debug("arith::unate") << "unatePropEquality " << curr << " " << prevLB << " " << prevUB << endl;
2324
649366
  Assert(curr != prevLB);
2325
649366
  Assert(curr != prevUB);
2326
649366
  Assert(curr != NullConstraint);
2327
649366
  bool hasPrevLB = ! (prevLB == NullConstraint);
2328
649366
  bool hasPrevUB = ! (prevUB == NullConstraint);
2329
649366
  Assert(!hasPrevLB || curr->getValue() >= prevLB->getValue());
2330
649366
  Assert(!hasPrevUB || curr->getValue() <= prevUB->getValue());
2331
2332
649366
  ++d_statistics.d_unatePropagateCalls;
2333
2334
649366
  const SortedConstraintMap& scm = curr->constraintSet();
2335
649366
  SortedConstraintMapConstIterator scm_curr = curr->d_variablePosition;
2336
649366
  SortedConstraintMapConstIterator scm_last = hasPrevUB ? prevUB->d_variablePosition : scm.end();
2337
649366
  SortedConstraintMapConstIterator scm_i;
2338
649366
  if(hasPrevLB){
2339
108316
    scm_i = prevLB->d_variablePosition;
2340
108316
    if(scm_i != scm_curr){ // If this does not move this past scm_curr, move it one forward
2341
26574
      ++scm_i;
2342
    }
2343
  }else{
2344
541050
    scm_i = scm.begin();
2345
  }
2346
2347
1848272
  for(; scm_i != scm_curr; ++scm_i){
2348
    // between the previous LB and the curr
2349
599453
    const ValueCollection& vc = scm_i->second;
2350
2351
    //Don't worry about implying the negation of upperbound.
2352
    //These should all be handled by propagating the LowerBounds!
2353
599453
    if(vc.hasLowerBound()){
2354
222298
      ConstraintP lb = vc.getLowerBound();
2355
222298
      if(handleUnateProp(curr, lb)){ return; }
2356
    }
2357
599453
    if(vc.hasDisequality()){
2358
202150
      ConstraintP dis = vc.getDisequality();
2359
202150
      if(handleUnateProp(curr, dis)){ return; }
2360
    }
2361
  }
2362
649366
  Assert(scm_i == scm_curr);
2363
649366
  if(!hasPrevUB || scm_i != scm_last){
2364
628202
    ++scm_i;
2365
  } // hasPrevUB implies scm_i != scm_last
2366
2367
2912858
  for(; scm_i != scm_last; ++scm_i){
2368
    // between the curr and the previous UB imply the upperbounds and disequalities.
2369
1131746
    const ValueCollection& vc = scm_i->second;
2370
2371
    //Don't worry about implying the negation of upperbound.
2372
    //These should all be handled by propagating the UpperBounds!
2373
1131746
    if(vc.hasUpperBound()){
2374
442465
      ConstraintP ub = vc.getUpperBound();
2375
442465
      if(handleUnateProp(curr, ub)){ return; }
2376
    }
2377
1131746
    if(vc.hasDisequality()){
2378
333337
      ConstraintP dis = vc.getDisequality();
2379
333337
      if(handleUnateProp(curr, dis)){ return; }
2380
    }
2381
  }
2382
}
2383
2384
779326
std::pair<int, int> Constraint::unateFarkasSigns(ConstraintCP ca, ConstraintCP cb){
2385
779326
  ConstraintType a = ca->getType();
2386
779326
  ConstraintType b = cb->getType();
2387
2388
779326
  Assert(a != Disequality);
2389
779326
  Assert(b != Disequality);
2390
2391
779326
  int a_sgn = (a == LowerBound) ? -1 : ((a == UpperBound) ? 1 : 0);
2392
779326
  int b_sgn = (b == LowerBound) ? -1 : ((b == UpperBound) ? 1 : 0);
2393
2394
779326
  if(a_sgn == 0 && b_sgn == 0){
2395
162735
    Assert(a == Equality);
2396
162735
    Assert(b == Equality);
2397
162735
    Assert(ca->getValue() != cb->getValue());
2398
325470
    if(ca->getValue() < cb->getValue()){
2399
59018
      a_sgn = 1;
2400
59018
      b_sgn = -1;
2401
    }else{
2402
103717
      a_sgn = -1;
2403
103717
      b_sgn = 1;
2404
    }
2405
616591
  }else if(a_sgn == 0){
2406
130178
    Assert(b_sgn != 0);
2407
130178
    Assert(a == Equality);
2408
130178
    a_sgn = -b_sgn;
2409
486413
  }else if(b_sgn == 0){
2410
200294
    Assert(a_sgn != 0);
2411
200294
    Assert(b == Equality);
2412
200294
    b_sgn = -a_sgn;
2413
  }
2414
779326
  Assert(a_sgn != 0);
2415
779326
  Assert(b_sgn != 0);
2416
2417
1558652
  Debug("arith::unateFarkasSigns") << "Constraint::unateFarkasSigns("<<a <<", " << b << ") -> "
2418
779326
                                   << "("<<a_sgn<<", "<< b_sgn <<")"<< endl;
2419
779326
  return make_pair(a_sgn, b_sgn);
2420
}
2421
2422
}  // namespace arith
2423
}  // namespace theory
2424
28191
}  // namespace cvc5