GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/constraint.cpp Lines: 1088 1487 73.2 %
Date: 2021-11-07 Branches: 1814 6431 28.2 %

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