GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/constraint.cpp Lines: 1088 1487 73.2 %
Date: 2021-11-06 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
5979320
ConstraintRule::ConstraintRule(ConstraintP con, ArithProofType pt)
53
5979320
    : d_constraint(con), d_proofType(pt), d_antecedentEnd(AntecedentIdSentinel)
54
{
55
5979320
  d_farkasCoefficients = RationalVectorCPSentinel;
56
5979320
}
57
2042201
ConstraintRule::ConstraintRule(ConstraintP con,
58
                               ArithProofType pt,
59
2042201
                               AntecedentId antecedentEnd)
60
2042201
    : d_constraint(con), d_proofType(pt), d_antecedentEnd(antecedentEnd)
61
{
62
2042201
  d_farkasCoefficients = RationalVectorCPSentinel;
63
2042201
}
64
65
3157540
ConstraintRule::ConstraintRule(ConstraintP con,
66
                               ArithProofType pt,
67
                               AntecedentId antecedentEnd,
68
3157540
                               RationalVectorCP coeffs)
69
3157540
    : d_constraint(con), d_proofType(pt), d_antecedentEnd(antecedentEnd)
70
{
71
3157540
  Assert(con->isProofProducing() || coeffs == RationalVectorCPSentinel);
72
3157540
  d_farkasCoefficients = coeffs;
73
3157540
}
74
75
/** Given a simplifiedKind this returns the corresponding ConstraintType. */
76
//ConstraintType constraintTypeOfLiteral(Kind k);
77
604241
ConstraintType Constraint::constraintTypeOfComparison(const Comparison& cmp){
78
604241
  Kind k = cmp.comparisonKind();
79
604241
  switch(k){
80
159780
  case LT:
81
  case LEQ:
82
    {
83
319560
      Polynomial l = cmp.getLeft();
84
159780
      if(l.leadingCoefficientIsPositive()){ // (< x c)
85
134386
        return UpperBound;
86
      }else{
87
25394
        return LowerBound; // (< (-x) c)
88
      }
89
    }
90
161301
  case GT:
91
  case GEQ:
92
    {
93
322602
      Polynomial l = cmp.getLeft();
94
161301
      if(l.leadingCoefficientIsPositive()){
95
135730
        return LowerBound; // (> x c)
96
      }else{
97
25571
        return UpperBound; // (> (-x) c)
98
      }
99
    }
100
141699
  case EQUAL:
101
141699
    return Equality;
102
141461
  case DISTINCT:
103
141461
    return Disequality;
104
  default: Unhandled() << k;
105
  }
106
}
107
108
752155
Constraint::Constraint(ArithVar x,
109
                       ConstraintType t,
110
                       const DeltaRational& v,
111
752155
                       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
752155
      d_produceProofs(produceProofs)
125
{
126
752155
  Assert(!initialized());
127
752155
}
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
2686327
ValueCollection::ValueCollection()
226
  : d_lowerBound(NullConstraint),
227
    d_upperBound(NullConstraint),
228
    d_equality(NullConstraint),
229
2686327
    d_disequality(NullConstraint)
230
2686327
{}
231
232
24466942
bool ValueCollection::hasLowerBound() const{
233
24466942
  return d_lowerBound != NullConstraint;
234
}
235
236
27910402
bool ValueCollection::hasUpperBound() const{
237
27910402
  return d_upperBound != NullConstraint;
238
}
239
240
4782366
bool ValueCollection::hasEquality() const{
241
4782366
  return d_equality != NullConstraint;
242
}
243
244
17876406
bool ValueCollection::hasDisequality() const {
245
17876406
  return d_disequality != NullConstraint;
246
}
247
248
5357028
ConstraintP ValueCollection::getLowerBound() const {
249
5357028
  Assert(hasLowerBound());
250
5357028
  return d_lowerBound;
251
}
252
253
5765670
ConstraintP ValueCollection::getUpperBound() const {
254
5765670
  Assert(hasUpperBound());
255
5765670
  return d_upperBound;
256
}
257
258
355840
ConstraintP ValueCollection::getEquality() const {
259
355840
  Assert(hasEquality());
260
355840
  return d_equality;
261
}
262
263
2612492
ConstraintP ValueCollection::getDisequality() const {
264
2612492
  Assert(hasDisequality());
265
2612492
  return d_disequality;
266
}
267
268
269
506435
void ValueCollection::push_into(std::vector<ConstraintP>& vec) const {
270
506435
  Debug("arith::constraint") << "push_into " << *this << endl;
271
506435
  if(hasEquality()){
272
145688
    vec.push_back(d_equality);
273
  }
274
506435
  if(hasLowerBound()){
275
229510
    vec.push_back(d_lowerBound);
276
  }
277
506435
  if(hasUpperBound()){
278
229510
    vec.push_back(d_upperBound);
279
  }
280
506435
  if(hasDisequality()){
281
145688
    vec.push_back(d_disequality);
282
  }
283
506435
}
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
4547731
bool ValueCollection::hasConstraintOfType(ConstraintType t) const{
308
4547731
  switch(t){
309
1380215
  case LowerBound:
310
1380215
    return hasLowerBound();
311
2783212
  case UpperBound:
312
2783212
    return hasUpperBound();
313
384304
  case Equality:
314
384304
    return hasEquality();
315
  case Disequality:
316
    return hasDisequality();
317
  default:
318
    Unreachable();
319
  }
320
}
321
322
251702
ArithVar ValueCollection::getVariable() const{
323
251702
  Assert(!empty());
324
251702
  return nonNull()->getVariable();
325
}
326
327
251702
const DeltaRational& ValueCollection::getValue() const{
328
251702
  Assert(!empty());
329
251702
  return nonNull()->getValue();
330
}
331
332
750396
void ValueCollection::add(ConstraintP c){
333
750396
  Assert(c != NullConstraint);
334
335
750396
  Assert(empty() || getVariable() == c->getVariable());
336
750396
  Assert(empty() || getValue() == c->getValue());
337
338
750396
  switch(c->getType()){
339
229510
  case LowerBound:
340
229510
    Assert(!hasLowerBound());
341
229510
    d_lowerBound = c;
342
229510
    break;
343
145688
  case Equality:
344
145688
    Assert(!hasEquality());
345
145688
    d_equality = c;
346
145688
    break;
347
229510
  case UpperBound:
348
229510
    Assert(!hasUpperBound());
349
229510
    d_upperBound = c;
350
229510
    break;
351
145688
  case Disequality:
352
145688
    Assert(!hasDisequality());
353
145688
    d_disequality = c;
354
145688
    break;
355
  default:
356
    Unreachable();
357
  }
358
750396
}
359
360
3557812
ConstraintP ValueCollection::getConstraintOfType(ConstraintType t) const{
361
3557812
  switch(t){
362
909038
    case LowerBound: Assert(hasLowerBound()); return d_lowerBound;
363
238616
    case Equality: Assert(hasEquality()); return d_equality;
364
2410158
    case UpperBound: Assert(hasUpperBound()); return d_upperBound;
365
    case Disequality: Assert(hasDisequality()); return d_disequality;
366
    default: Unreachable();
367
  }
368
}
369
370
750396
void ValueCollection::remove(ConstraintType t){
371
750396
  switch(t){
372
229510
  case LowerBound:
373
229510
    Assert(hasLowerBound());
374
229510
    d_lowerBound = NullConstraint;
375
229510
    break;
376
145688
  case Equality:
377
145688
    Assert(hasEquality());
378
145688
    d_equality = NullConstraint;
379
145688
    break;
380
229510
  case UpperBound:
381
229510
    Assert(hasUpperBound());
382
229510
    d_upperBound = NullConstraint;
383
229510
    break;
384
145688
  case Disequality:
385
145688
    Assert(hasDisequality());
386
145688
    d_disequality = NullConstraint;
387
145688
    break;
388
  default:
389
    Unreachable();
390
  }
391
750396
}
392
393
2754592
bool ValueCollection::empty() const{
394
  return
395
6650866
    !(hasLowerBound() ||
396
4645906
      hasUpperBound() ||
397
2268937
      hasEquality() ||
398
4273897
      hasDisequality());
399
}
400
401
503404
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
503404
  if(hasLowerBound()){
405
150244
    return d_lowerBound;
406
353160
  }else if(hasUpperBound()){
407
51188
    return d_upperBound;
408
301972
  }else if(hasEquality()){
409
301972
    return d_equality;
410
  }else if(hasDisequality()){
411
    return d_disequality;
412
  }else{
413
    return NullConstraint;
414
  }
415
}
416
417
3074515
bool Constraint::initialized() const {
418
3074515
  return d_database != NULL;
419
}
420
421
const ConstraintDatabase& Constraint::getDatabase() const{
422
  Assert(initialized());
423
  return *d_database;
424
}
425
426
750396
void Constraint::initialize(ConstraintDatabase* db, SortedConstraintMapIterator v, ConstraintP negation){
427
750396
  Assert(!initialized());
428
750396
  d_database = db;
429
750396
  d_variablePosition = v;
430
750396
  d_negation = negation;
431
750396
}
432
433
1504310
Constraint::~Constraint() {
434
  // Call this instead of safeToGarbageCollect()
435
752155
  Assert(!contextDependentDataIsSet());
436
437
752155
  if(initialized()){
438
750396
    ValueCollection& vc =  d_variablePosition->second;
439
750396
    Debug("arith::constraint") << "removing" << vc << endl;
440
441
750396
    vc.remove(getType());
442
443
750396
    if(vc.empty()){
444
506435
      Debug("arith::constraint") << "erasing" << vc << endl;
445
506435
      SortedConstraintMap& perVariable = d_database->getVariableSCM(getVariable());
446
506435
      perVariable.erase(d_variablePosition);
447
    }
448
449
750396
    if(hasLiteral()){
450
606000
      d_database->d_nodetoConstraintMap.erase(getLiteral());
451
    }
452
  }
453
752155
}
454
455
33970861
const ConstraintRule& Constraint::getConstraintRule() const {
456
33970861
  Assert(hasProof());
457
33970861
  return d_database->d_watches->d_constraintProofs[d_crid];
458
}
459
460
3804473
const ValueCollection& Constraint::getValueCollection() const{
461
3804473
  return d_variablePosition->second;
462
}
463
464
465
118391
ConstraintP Constraint::getCeiling() {
466
118391
  Debug("getCeiling") << "Constraint_::getCeiling on " << *this << endl;
467
118391
  Assert(getValue().getInfinitesimalPart().sgn() > 0);
468
469
236782
  const DeltaRational ceiling(getValue().ceiling());
470
236782
  return d_database->getConstraint(getVariable(), getType(), ceiling);
471
}
472
473
1785080
ConstraintP Constraint::getFloor() {
474
1785080
  Assert(getValue().getInfinitesimalPart().sgn() < 0);
475
476
3570160
  const DeltaRational floor(Rational(getValue().floor()));
477
3570160
  return d_database->getConstraint(getVariable(), getType(), floor);
478
}
479
480
943438
void Constraint::setCanBePropagated() {
481
943438
  Assert(!canBePropagated());
482
943438
  d_database->pushCanBePropagatedWatch(this);
483
943438
}
484
485
7100255
void Constraint::setAssertedToTheTheory(TNode witness, bool nowInConflict) {
486
7100255
  Assert(hasLiteral());
487
7100255
  Assert(!assertedToTheTheory());
488
7100255
  Assert(negationHasProof() == nowInConflict);
489
7100255
  d_database->pushAssertionOrderWatch(this, witness);
490
491
7100255
  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
7100255
}
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
14249250
bool Constraint::isInternalAssumption() const {
515
14249250
  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
14970084
bool Constraint::isAssumption() const {
542
14970084
  return getProofType() == AssumeAP;
543
}
544
545
699816
bool Constraint::hasEqualityEngineProof() const {
546
699816
  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
606000
bool Constraint::sanityChecking(Node n) const {
657
1212000
  Comparison cmp = Comparison::parseNormalForm(n);
658
606000
  Kind k = cmp.comparisonKind();
659
1212000
  Polynomial pleft = cmp.normalizedVariablePart();
660
606000
  Assert(k == EQUAL || k == DISTINCT || pleft.leadingCoefficientIsPositive());
661
606000
  Assert(k != EQUAL || Monomial::isMember(n[0]));
662
606000
  Assert(k != DISTINCT || Monomial::isMember(n[0][0]));
663
664
1212000
  TNode left = pleft.getNode();
665
1212000
  DeltaRational right = cmp.normalizedDeltaRational();
666
667
606000
  const ArithVariables& avariables = d_database->getArithVariables();
668
669
606000
  Debug("Constraint::sanityChecking") << cmp.getNode() << endl;
670
606000
  Debug("Constraint::sanityChecking") << k << endl;
671
606000
  Debug("Constraint::sanityChecking") << pleft.getNode() << endl;
672
606000
  Debug("Constraint::sanityChecking") << left << endl;
673
606000
  Debug("Constraint::sanityChecking") << right << endl;
674
606000
  Debug("Constraint::sanityChecking") << getValue() << endl;
675
606000
  Debug("Constraint::sanityChecking") << avariables.hasArithVar(left) << endl;
676
606000
  Debug("Constraint::sanityChecking") << avariables.asArithVar(left) << endl;
677
606000
  Debug("Constraint::sanityChecking") << getVariable() << endl;
678
679
680
3030000
  if(avariables.hasArithVar(left) &&
681
3030000
     avariables.asArithVar(left) == getVariable() &&
682
606000
     getValue() == right){
683
606000
    switch(getType()){
684
322602
    case LowerBound:
685
    case UpperBound:
686
      //Be overapproximate
687
322602
      return k == GT || k == GEQ ||k == LT || k == LEQ;
688
141699
    case Equality:
689
141699
      return k == EQUAL;
690
141699
    case Disequality:
691
141699
      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
3157540
bool Constraint::wellFormedFarkasProof() const {
746
3157540
  Assert(hasProof());
747
748
3157540
  const ConstraintRule& cr = getConstraintRule();
749
3157540
  if(cr.d_constraint != this){ return false; }
750
3157540
  if(cr.d_proofType != FarkasAP){ return false; }
751
752
3157540
  AntecedentId p = cr.d_antecedentEnd;
753
754
  // must have at least one antecedent
755
3157540
  ConstraintCP antecedent = d_database->d_antecedents[p];
756
3157540
  if(antecedent  == NullConstraint) { return false; }
757
758
3157540
  if (!d_produceProofs)
759
  {
760
2320035
    return cr.d_farkasCoefficients == RationalVectorCPSentinel;
761
  }
762
837505
  Assert(d_produceProofs);
763
764
837505
  if(cr.d_farkasCoefficients == RationalVectorCPSentinel){ return false; }
765
837505
  if(cr.d_farkasCoefficients->size() < 2){ return false; }
766
767
837505
  const ArithVariables& vars = d_database->getArithVariables();
768
769
1675010
  DeltaRational rhs(0);
770
1675010
  Node lhs = Polynomial::mkZero().getNode();
771
772
837505
  RationalVector::const_iterator coeffIterator = cr.d_farkasCoefficients->end()-1;
773
837505
  RationalVector::const_iterator coeffBegin = cr.d_farkasCoefficients->begin();
774
775
3268045
  while(antecedent != NullConstraint){
776
1215270
    Assert(lhs.isNull() || Polynomial::isMember(lhs));
777
778
1215270
    const Rational& coeff = *coeffIterator;
779
1215270
    int coeffSgn = coeff.sgn();
780
781
1215270
    rhs += antecedent->getValue() * coeff;
782
783
1215270
    ArithVar antVar = antecedent->getVariable();
784
1215270
    if(!lhs.isNull() && vars.hasNode(antVar)){
785
2430540
      Node antAsNode = vars.asNode(antVar);
786
1215270
      if(Polynomial::isMember(antAsNode)){
787
2430540
        Polynomial lhsPoly = Polynomial::parsePolynomial(lhs);
788
2430540
        Polynomial antPoly = Polynomial::parsePolynomial(antAsNode);
789
2430540
        Polynomial sum = lhsPoly + (antPoly * coeff);
790
1215270
        lhs = sum.getNode();
791
      }else{
792
        lhs = Node::null();
793
      }
794
    } else {
795
      lhs = Node::null();
796
    }
797
1215270
    Debug("constraints::wffp") << "running sum: " << lhs << " <= " << rhs << endl;
798
799
1215270
    switch( antecedent->getType() ){
800
375628
    case LowerBound:
801
      // fc[l] < 0, therefore return false if coeffSgn >= 0
802
375628
      if(coeffSgn >= 0){ return false; }
803
375628
      break;
804
214398
    case UpperBound:
805
      // fc[u] > 0, therefore return false if coeffSgn <= 0
806
214398
      if(coeffSgn <= 0){ return false; }
807
214398
      break;
808
625244
    case Equality:
809
625244
      if(coeffSgn == 0) { return false; }
810
625244
      break;
811
    case Disequality:
812
    default:
813
      return false;
814
    }
815
816
1215270
    if(coeffIterator == coeffBegin){ return false; }
817
1215270
    --coeffIterator;
818
1215270
    --p;
819
1215270
    antecedent = d_database->d_antecedents[p];
820
  }
821
837505
  if(coeffIterator != coeffBegin){ return false; }
822
823
837505
  const Rational& firstCoeff = (*coeffBegin);
824
837505
  int firstCoeffSgn = firstCoeff.sgn();
825
837505
  rhs += (getNegation()->getValue()) * firstCoeff;
826
837505
  if(!lhs.isNull() && vars.hasNode(getVariable())){
827
1675010
    Node firstAsNode = vars.asNode(getVariable());
828
837505
    if(Polynomial::isMember(firstAsNode)){
829
1675010
      Polynomial lhsPoly = Polynomial::parsePolynomial(lhs);
830
1675010
      Polynomial firstPoly = Polynomial::parsePolynomial(firstAsNode);
831
1675010
      Polynomial sum = lhsPoly + (firstPoly * firstCoeff);
832
837505
      lhs = sum.getNode();
833
    }else{
834
      lhs = Node::null();
835
    }
836
  }else{
837
    lhs = Node::null();
838
  }
839
840
837505
  switch( getNegation()->getType() ){
841
244757
  case LowerBound:
842
    // fc[l] < 0, therefore return false if coeffSgn >= 0
843
244757
    if(firstCoeffSgn >= 0){ return false; }
844
244757
    break;
845
262011
  case UpperBound:
846
    // fc[u] > 0, therefore return false if coeffSgn <= 0
847
262011
    if(firstCoeffSgn <= 0){ return false; }
848
262011
    break;
849
330737
  case Equality:
850
330737
    if(firstCoeffSgn == 0) { return false; }
851
330737
    break;
852
  case Disequality:
853
  default:
854
    return false;
855
  }
856
837505
  Debug("constraints::wffp") << "final sum: " << lhs << " <= " << rhs << endl;
857
  // 0 = lhs <= rhs < 0
858
2512515
  return (lhs.isNull() || (Constant::isMember(lhs) && Constant(lhs).isZero()))
859
2512515
         && rhs.sgn() < 0;
860
}
861
862
73957
ConstraintP Constraint::makeNegation(ArithVar v,
863
                                     ConstraintType t,
864
                                     const DeltaRational& r,
865
                                     bool produceProofs)
866
{
867
73957
  switch(t){
868
4008
  case LowerBound:
869
    {
870
4008
      Assert(r.infinitesimalSgn() >= 0);
871
4008
      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
4008
        Assert(r.infinitesimalSgn() == 0);
878
        // make (not (v >= r)), which is (v < r)
879
8016
        DeltaRational addInf(r.getNoninfinitesimalPart(), -1);
880
4008
        return new Constraint(v, UpperBound, addInf, produceProofs);
881
      }
882
    }
883
65722
  case UpperBound:
884
    {
885
65722
      Assert(r.infinitesimalSgn() <= 0);
886
65722
      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
65722
        Assert(r.infinitesimalSgn() == 0);
893
        // make (not (v <= r)), which is (v > r)
894
131444
        DeltaRational addInf(r.getNoninfinitesimalPart(), 1);
895
65722
        return new Constraint(v, LowerBound, addInf, produceProofs);
896
      }
897
    }
898
4227
    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
15272
ConstraintDatabase::ConstraintDatabase(Env& env,
905
                                       const ArithVariables& avars,
906
                                       ArithCongruenceManager& cm,
907
                                       RaiseConflict raiseConflict,
908
15272
                                       EagerProofGenerator* pfGen)
909
    : EnvObj(env),
910
      d_varDatabases(),
911
      d_toPropagate(context()),
912
      d_antecedents(context(), false),
913
15272
      d_watches(new Watches(context(), userContext())),
914
      d_avariables(avars),
915
      d_congruenceManager(cm),
916
      d_pfGen(pfGen),
917
15272
      d_pnm(d_env.isTheoryProofProducing() ? d_env.getProofNodeManager()
918
                                           : nullptr),
919
      d_raiseConflict(raiseConflict),
920
      d_one(1),
921
45816
      d_negOne(-1)
922
{
923
15272
}
924
925
14155595
SortedConstraintMap& ConstraintDatabase::getVariableSCM(ArithVar v) const{
926
14155595
  Assert(variableDatabaseIsSetup(v));
927
14155595
  return d_varDatabases[v]->d_constraints;
928
}
929
930
37222
void ConstraintDatabase::pushSplitWatch(ConstraintP c){
931
37222
  Assert(!c->d_split);
932
37222
  c->d_split = true;
933
37222
  d_watches->d_splitWatches.push_back(c);
934
37222
}
935
936
937
943438
void ConstraintDatabase::pushCanBePropagatedWatch(ConstraintP c){
938
943438
  Assert(!c->d_canBePropagated);
939
943438
  c->d_canBePropagated = true;
940
943438
  d_watches->d_canBePropagatedWatches.push_back(c);
941
943438
}
942
943
7100255
void ConstraintDatabase::pushAssertionOrderWatch(ConstraintP c, TNode witness){
944
7100255
  Assert(!c->assertedToTheTheory());
945
7100255
  c->d_assertionOrder = d_watches->d_assertionOrderWatches.size();
946
7100255
  c->d_witness = witness;
947
7100255
  d_watches->d_assertionOrderWatches.push_back(c);
948
7100255
}
949
950
951
11179061
void ConstraintDatabase::pushConstraintRule(const ConstraintRule& crp){
952
11179061
  ConstraintP c = crp.d_constraint;
953
11179061
  Assert(c->d_crid == ConstraintRuleIdSentinel);
954
11179061
  Assert(!c->hasProof());
955
11179061
  c->d_crid = d_watches->d_constraintProofs.size();
956
11179061
  d_watches->d_constraintProofs.push_back(crp);
957
11179061
}
958
959
2153817
ConstraintP ConstraintDatabase::getConstraint(ArithVar v, ConstraintType t, const DeltaRational& r){
960
  //This must always return a constraint.
961
962
2153817
  SortedConstraintMap& scm = getVariableSCM(v);
963
2153817
  pair<SortedConstraintMapIterator, bool> insertAttempt;
964
2153817
  insertAttempt = scm.insert(make_pair(r, ValueCollection()));
965
966
2153817
  SortedConstraintMapIterator pos = insertAttempt.first;
967
2153817
  ValueCollection& vc = pos->second;
968
2153817
  if(vc.hasConstraintOfType(t)){
969
2079860
    return vc.getConstraintOfType(t);
970
  }else{
971
73957
    ConstraintP c = new Constraint(v, t, r, options().smt.produceProofs);
972
    ConstraintP negC =
973
73957
        Constraint::makeNegation(v, t, r, options().smt.produceProofs);
974
975
73957
    SortedConstraintMapIterator negPos;
976
73957
    if(t == Equality || t == Disequality){
977
4227
      negPos = pos;
978
    }else{
979
69730
      pair<SortedConstraintMapIterator, bool> negInsertAttempt;
980
69730
      negInsertAttempt = scm.insert(make_pair(negC->getValue(), ValueCollection()));
981
69730
      Assert(negInsertAttempt.second
982
             || !negInsertAttempt.first->second.hasConstraintOfType(
983
                 negC->getType()));
984
69730
      negPos = negInsertAttempt.first;
985
    }
986
987
73957
    c->initialize(this, pos, negC);
988
73957
    negC->initialize(this, negPos, c);
989
990
73957
    vc.add(c);
991
73957
    negPos->second.add(negC);
992
993
73957
    return c;
994
  }
995
}
996
997
403796
ConstraintP ConstraintDatabase::ensureConstraint(ValueCollection& vc, ConstraintType t){
998
403796
  if(vc.hasConstraintOfType(t)){
999
396055
    return vc.getConstraintOfType(t);
1000
  }else{
1001
7741
    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
30534
ConstraintDatabase::~ConstraintDatabase(){
1012
15267
  delete d_watches;
1013
1014
30534
  std::vector<ConstraintP> constraintList;
1015
1016
380767
  while(!d_varDatabases.empty()){
1017
182750
    PerVariableDatabase* back = d_varDatabases.back();
1018
1019
182750
    SortedConstraintMap& scm = back->d_constraints;
1020
182750
    SortedConstraintMapIterator i = scm.begin(), i_end = scm.end();
1021
1195620
    for(; i != i_end; ++i){
1022
506435
      (i->second).push_into(constraintList);
1023
    }
1024
1683542
    while(!constraintList.empty()){
1025
750396
      ConstraintP c = constraintList.back();
1026
750396
      constraintList.pop_back();
1027
750396
      delete c;
1028
    }
1029
182750
    Assert(scm.empty());
1030
182750
    d_varDatabases.pop_back();
1031
182750
    delete back;
1032
  }
1033
1034
15267
  Assert(d_nodetoConstraintMap.empty());
1035
15267
}
1036
1037
15272
ConstraintDatabase::Statistics::Statistics()
1038
15272
    : d_unatePropagateCalls(smtStatisticsRegistry().registerInt(
1039
30544
        "theory::arith::cd::unatePropagateCalls")),
1040
15272
      d_unatePropagateImplications(smtStatisticsRegistry().registerInt(
1041
30544
          "theory::arith::cd::unatePropagateImplications"))
1042
{
1043
15272
}
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
182750
void ConstraintDatabase::addVariable(ArithVar v){
1054
182750
  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
182750
    Debug("arith::constraint") << "about to fail" << v << " " << d_varDatabases.size() << endl;
1073
182750
    Assert(v == d_varDatabases.size());
1074
182750
    d_varDatabases.push_back(new PerVariableDatabase(v));
1075
  }
1076
182750
}
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
752155
bool Constraint::contextDependentDataIsSet() const{
1090
752155
  return hasProof() || isSplit() || canBePropagated() || assertedToTheTheory();
1091
}
1092
1093
18611
TrustNode Constraint::split()
1094
{
1095
18611
  Assert(isEquality() || isDisequality());
1096
1097
18611
  bool isEq = isEquality();
1098
1099
18611
  ConstraintP eq = isEq ? this : d_negation;
1100
18611
  ConstraintP diseq = isEq ? d_negation : this;
1101
1102
37222
  TNode eqNode = eq->getLiteral();
1103
18611
  Assert(eqNode.getKind() == kind::EQUAL);
1104
37222
  TNode lhs = eqNode[0];
1105
37222
  TNode rhs = eqNode[1];
1106
1107
37222
  Node leqNode = NodeBuilder(kind::LEQ) << lhs << rhs;
1108
37222
  Node ltNode = NodeBuilder(kind::LT) << lhs << rhs;
1109
37222
  Node gtNode = NodeBuilder(kind::GT) << lhs << rhs;
1110
37222
  Node geqNode = NodeBuilder(kind::GEQ) << lhs << rhs;
1111
1112
37222
  Node lemma = NodeBuilder(OR) << leqNode << geqNode;
1113
1114
18611
  TrustNode trustedLemma;
1115
18611
  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
16094
    trustedLemma = TrustNode::mkTrustLemma(lemma);
1144
  }
1145
1146
18611
  eq->d_database->pushSplitWatch(eq);
1147
18611
  diseq->d_database->pushSplitWatch(diseq);
1148
1149
37222
  return trustedLemma;
1150
}
1151
1152
1212001
bool ConstraintDatabase::hasLiteral(TNode literal) const {
1153
1212001
  return lookup(literal) != NullConstraint;
1154
}
1155
1156
303000
ConstraintP ConstraintDatabase::addLiteral(TNode literal){
1157
303000
  Assert(!hasLiteral(literal));
1158
303000
  bool isNot = (literal.getKind() == NOT);
1159
606000
  Node atomNode = (isNot ? literal[0] : literal);
1160
606000
  Node negationNode  = atomNode.notNode();
1161
1162
303000
  Assert(!hasLiteral(atomNode));
1163
303000
  Assert(!hasLiteral(negationNode));
1164
606000
  Comparison posCmp = Comparison::parseNormalForm(atomNode);
1165
1166
303000
  ConstraintType posType = Constraint::constraintTypeOfComparison(posCmp);
1167
1168
606000
  Polynomial nvp = posCmp.normalizedVariablePart();
1169
303000
  ArithVar v = d_avariables.asArithVar(nvp.getNode());
1170
1171
606000
  DeltaRational posDR = posCmp.normalizedDeltaRational();
1172
1173
  ConstraintP posC =
1174
303000
      new Constraint(v, posType, posDR, options().smt.produceProofs);
1175
1176
303000
  Debug("arith::constraint") << "addliteral( literal ->" << literal << ")" << endl;
1177
303000
  Debug("arith::constraint") << "addliteral( posC ->" << posC << ")" << endl;
1178
1179
303000
  SortedConstraintMap& scm = getVariableSCM(posC->getVariable());
1180
303000
  pair<SortedConstraintMapIterator, bool> insertAttempt;
1181
303000
  insertAttempt = scm.insert(make_pair(posC->getValue(), ValueCollection()));
1182
1183
303000
  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
303000
  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
1759
    ConstraintP hit = posI->second.getConstraintOfType(posC->getType());
1191
1759
    Debug("arith::constraint") << "hit " << hit << endl;
1192
1759
    Debug("arith::constraint") << "posC " << posC << endl;
1193
1194
1759
    delete posC;
1195
1196
1759
    hit->setLiteral(atomNode);
1197
1759
    hit->getNegation()->setLiteral(negationNode);
1198
1759
    return isNot ? hit->getNegation(): hit;
1199
  }else{
1200
602482
    Comparison negCmp = Comparison::parseNormalForm(negationNode);
1201
1202
301241
    ConstraintType negType = Constraint::constraintTypeOfComparison(negCmp);
1203
602482
    DeltaRational negDR = negCmp.normalizedDeltaRational();
1204
1205
    ConstraintP negC =
1206
301241
        new Constraint(v, negType, negDR, options().smt.produceProofs);
1207
1208
301241
    SortedConstraintMapIterator negI;
1209
1210
301241
    if(posC->isEquality()){
1211
141461
      negI = posI;
1212
    }else{
1213
159780
      Assert(posC->isLowerBound() || posC->isUpperBound());
1214
1215
159780
      pair<SortedConstraintMapIterator, bool> negInsertAttempt;
1216
159780
      negInsertAttempt = scm.insert(make_pair(negC->getValue(), ValueCollection()));
1217
1218
159780
      Debug("nf::tmp") << "sdhjfgdhjkldfgljkhdfg" << endl;
1219
159780
      Debug("nf::tmp") << negC << endl;
1220
159780
      Debug("nf::tmp") << negC->getValue() << endl;
1221
1222
      //This should always succeed as the DeltaRational for the negation is unique!
1223
159780
      Assert(negInsertAttempt.second);
1224
1225
159780
      negI = negInsertAttempt.first;
1226
    }
1227
1228
301241
    (posI->second).add(posC);
1229
301241
    (negI->second).add(negC);
1230
1231
301241
    posC->initialize(this, posI, negC);
1232
301241
    negC->initialize(this, negI, posC);
1233
1234
301241
    posC->setLiteral(atomNode);
1235
301241
    negC->setLiteral(negationNode);
1236
1237
301241
    return isNot ? negC : posC;
1238
  }
1239
}
1240
1241
1242
12239246
ConstraintP ConstraintDatabase::lookup(TNode literal) const{
1243
12239246
  NodetoConstraintMap::const_iterator iter = d_nodetoConstraintMap.find(literal);
1244
12239246
  if(iter == d_nodetoConstraintMap.end()){
1245
2225466
    return NullConstraint;
1246
  }else{
1247
10013780
    return iter->second;
1248
  }
1249
}
1250
1251
5703250
void Constraint::setAssumption(bool nowInConflict){
1252
5703250
  Debug("constraints::pf") << "setAssumption(" << this << ")" << std::endl;
1253
5703250
  Assert(!hasProof());
1254
5703250
  Assert(negationHasProof() == nowInConflict);
1255
5703250
  Assert(hasLiteral());
1256
5703250
  Assert(assertedToTheTheory());
1257
1258
5703250
  d_database->pushConstraintRule(ConstraintRule(this, AssumeAP));
1259
1260
5703250
  Assert(inConflict() == nowInConflict);
1261
5703250
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1262
    Debug("constraint::conflictCommit") << "inConflict@setAssumption " << this << std::endl;
1263
  }
1264
5703250
}
1265
1266
5110899
void Constraint::tryToPropagate(){
1267
5110899
  Assert(hasProof());
1268
5110899
  Assert(!isAssumption());
1269
5110899
  Assert(!isInternalAssumption());
1270
1271
5110899
  if(canBePropagated() && !assertedToTheTheory() && !isAssumption() && !isInternalAssumption()){
1272
1108355
    propagate();
1273
  }
1274
5110899
}
1275
1276
1127789
void Constraint::propagate(){
1277
1127789
  Assert(hasProof());
1278
1127789
  Assert(canBePropagated());
1279
1127789
  Assert(!assertedToTheTheory());
1280
1127789
  Assert(!isAssumption());
1281
1127789
  Assert(!isInternalAssumption());
1282
1283
1127789
  d_database->d_toPropagate.push(this);
1284
1127789
}
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
3026928
void Constraint::impliedByUnate(ConstraintCP imp, bool nowInConflict){
1295
3026928
  Debug("constraints::pf") << "impliedByUnate(" << this << ", " << *imp << ")" << std::endl;
1296
3026928
  Assert(!hasProof());
1297
3026928
  Assert(imp->hasProof());
1298
3026928
  Assert(negationHasProof() == nowInConflict);
1299
1300
3026928
  d_database->d_antecedents.push_back(NullConstraint);
1301
3026928
  d_database->d_antecedents.push_back(imp);
1302
1303
3026928
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1304
1305
  RationalVectorP coeffs;
1306
3026928
  if (d_produceProofs)
1307
  {
1308
791875
    std::pair<int, int> sgns = unateFarkasSigns(getNegation(), imp);
1309
1310
1583750
    Rational first(sgns.first);
1311
1583750
    Rational second(sgns.second);
1312
1313
791875
    coeffs = new RationalVector();
1314
791875
    coeffs->push_back(first);
1315
791875
    coeffs->push_back(second);
1316
  }
1317
  else
1318
  {
1319
2235053
    coeffs = RationalVectorPSentinel;
1320
  }
1321
  // no need to delete coeffs the memory is owned by ConstraintRule
1322
3026928
  d_database->pushConstraintRule(ConstraintRule(this, FarkasAP, antecedentEnd, coeffs));
1323
1324
3026928
  Assert(inConflict() == nowInConflict);
1325
3026928
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1326
    Debug("constraint::conflictCommit") << "inConflict@impliedByUnate " << this << std::endl;
1327
  }
1328
1329
3026928
  if(Debug.isOn("constraints::wffp") && !wellFormedFarkasProof()){
1330
    getConstraintRule().print(Debug("constraints::wffp"), d_produceProofs);
1331
  }
1332
3026928
  Assert(wellFormedFarkasProof());
1333
3026928
}
1334
1335
502972
void Constraint::impliedByTrichotomy(ConstraintCP a, ConstraintCP b, bool nowInConflict){
1336
502972
  Debug("constraints::pf") << "impliedByTrichotomy(" << this << ", " << *a << ", ";
1337
502972
  Debug("constraints::pf") << *b << ")" << std::endl;
1338
502972
  Assert(!hasProof());
1339
502972
  Assert(negationHasProof() == nowInConflict);
1340
502972
  Assert(a->hasProof());
1341
502972
  Assert(b->hasProof());
1342
1343
502972
  d_database->d_antecedents.push_back(NullConstraint);
1344
502972
  d_database->d_antecedents.push_back(a);
1345
502972
  d_database->d_antecedents.push_back(b);
1346
1347
502972
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1348
502972
  d_database->pushConstraintRule(ConstraintRule(this, TrichotomyAP, antecedentEnd));
1349
1350
502972
  Assert(inConflict() == nowInConflict);
1351
502972
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1352
    Debug("constraint::conflictCommit") << "inConflict@impliedByTrichotomy " << this << std::endl;
1353
  }
1354
502972
}
1355
1356
1357
130612
bool Constraint::allHaveProof(const ConstraintCPVec& b){
1358
1787491
  for(ConstraintCPVec::const_iterator i=b.begin(), i_end=b.end(); i != i_end; ++i){
1359
1656879
    ConstraintCP cp = *i;
1360
1656879
    if(! (cp->hasProof())){ return false; }
1361
  }
1362
130612
  return true;
1363
}
1364
1365
1539229
void Constraint::impliedByIntTighten(ConstraintCP a, bool nowInConflict){
1366
1539229
  Debug("constraints::pf") << "impliedByIntTighten(" << this << ", " << *a << ")" << std::endl;
1367
1539229
  Assert(!hasProof());
1368
1539229
  Assert(negationHasProof() == nowInConflict);
1369
1539229
  Assert(a->hasProof());
1370
3078458
  Debug("pf::arith") << "impliedByIntTighten(" << this << ", " << a << ")"
1371
1539229
                     << std::endl;
1372
1373
1539229
  d_database->d_antecedents.push_back(NullConstraint);
1374
1539229
  d_database->d_antecedents.push_back(a);
1375
1539229
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1376
1539229
  d_database->pushConstraintRule(ConstraintRule(this, IntTightenAP, antecedentEnd));
1377
1378
1539229
  Assert(inConflict() == nowInConflict);
1379
1539229
  if(inConflict()){
1380
3141
    Debug("constraint::conflictCommit") << "inConflict impliedByIntTighten" << this << std::endl;
1381
  }
1382
1539229
}
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
130612
void Constraint::impliedByFarkas(const ConstraintCPVec& a, RationalVectorCP coeffs, bool nowInConflict){
1441
130612
  Debug("constraints::pf") << "impliedByFarkas(" << this;
1442
130612
  if (Debug.isOn("constraints::pf")) {
1443
    for (const ConstraintCP& p : a)
1444
    {
1445
      Debug("constraints::pf") << ", " << p;
1446
    }
1447
  }
1448
130612
  Debug("constraints::pf") << ", <coeffs>";
1449
130612
  Debug("constraints::pf") << ")" << std::endl;
1450
130612
  Assert(!hasProof());
1451
130612
  Assert(negationHasProof() == nowInConflict);
1452
130612
  Assert(allHaveProof(a));
1453
1454
130612
  Assert(d_produceProofs == (coeffs != RationalVectorCPSentinel));
1455
130612
  Assert(!d_produceProofs || coeffs->size() == a.size() + 1);
1456
1457
130612
  Assert(a.size() >= 1);
1458
1459
130612
  d_database->d_antecedents.push_back(NullConstraint);
1460
1787491
  for(ConstraintCPVec::const_iterator i = a.begin(), end = a.end(); i != end; ++i){
1461
1656879
    ConstraintCP c_i = *i;
1462
1656879
    Assert(c_i->hasProof());
1463
1656879
    d_database->d_antecedents.push_back(c_i);
1464
  }
1465
130612
  AntecedentId antecedentEnd = d_database->d_antecedents.size() - 1;
1466
1467
  RationalVectorCP coeffsCopy;
1468
130612
  if (d_produceProofs)
1469
  {
1470
45630
    Assert(coeffs != RationalVectorCPSentinel);
1471
45630
    coeffsCopy = new RationalVector(*coeffs);
1472
  }
1473
  else
1474
  {
1475
84982
    coeffsCopy = RationalVectorCPSentinel;
1476
  }
1477
130612
  d_database->pushConstraintRule(ConstraintRule(this, FarkasAP, antecedentEnd, coeffsCopy));
1478
1479
130612
  Assert(inConflict() == nowInConflict);
1480
130612
  if(Debug.isOn("constraint::conflictCommit") && inConflict()){
1481
    Debug("constraint::conflictCommit") << "inConflict@impliedByFarkas " << this << std::endl;
1482
  }
1483
130612
  if(Debug.isOn("constraints::wffp") && !wellFormedFarkasProof()){
1484
    getConstraintRule().print(Debug("constraints::wffp"), d_produceProofs);
1485
  }
1486
130612
  Assert(wellFormedFarkasProof());
1487
130612
}
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
276070
void Constraint::setEqualityEngineProof(){
1507
276070
  Debug("constraints::pf") << "setEqualityEngineProof(" << this;
1508
276070
  Debug("constraints::pf") << ")" << std::endl;
1509
276070
  Assert(truthIsUnknown());
1510
276070
  Assert(hasLiteral());
1511
276070
  d_database->pushConstraintRule(ConstraintRule(this, EqualityEngineAP));
1512
276070
}
1513
1514
1515
4952471
SortedConstraintMap& Constraint::constraintSet() const{
1516
4952471
  Assert(d_database->variableDatabaseIsSetup(d_variable));
1517
4952471
  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
113283
Node Constraint::externalImplication(const ConstraintCPVec& b) const{
1532
113283
  Assert(hasLiteral());
1533
226566
  Node antecedent = externalExplainByAssertions(b);
1534
226566
  Node implied = getLiteral();
1535
226566
  return antecedent.impNode(implied);
1536
}
1537
1538
1539
1534570
Node Constraint::externalExplainByAssertions(const ConstraintCPVec& b){
1540
1534570
  return externalExplain(b, AssertionOrderSentinel);
1541
}
1542
1543
22243
TrustNode Constraint::externalExplainForPropagation(TNode lit) const
1544
{
1545
22243
  Assert(hasProof());
1546
22243
  Assert(!isAssumption());
1547
22243
  Assert(!isInternalAssumption());
1548
44486
  NodeBuilder nb(Kind::AND);
1549
44486
  auto pfFromAssumptions = externalExplain(nb, d_assertionOrder);
1550
44486
  Node n = safeConstructNary(nb);
1551
22243
  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
19797
    return TrustNode::mkTrustPropExp(lit, n);
1577
  }
1578
}
1579
1580
95796
TrustNode Constraint::externalExplainConflict() const
1581
{
1582
95796
  Debug("pf::arith::explain") << this << std::endl;
1583
95796
  Assert(inConflict());
1584
191592
  NodeBuilder nb(kind::AND);
1585
191592
  auto pf1 = externalExplainByAssertions(nb);
1586
191592
  auto not2 = getNegation()->getProofLiteral().negate();
1587
191592
  auto pf2 = getNegation()->externalExplainByAssertions(nb);
1588
191592
  Node n = safeConstructNary(nb);
1589
95796
  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
82473
    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
1534570
Node Constraint::externalExplain(const ConstraintCPVec& v, AssertionOrder order){
1681
3069140
  NodeBuilder nb(kind::AND);
1682
1534570
  ConstraintCPVec::const_iterator i, end;
1683
3345541
  for(i = v.begin(), end = v.end(); i != end; ++i){
1684
1810971
    ConstraintCP v_i = *i;
1685
1810971
    v_i->externalExplain(nb, order);
1686
  }
1687
3069140
  return safeConstructNary(nb);
1688
}
1689
1690
6879964
std::shared_ptr<ProofNode> Constraint::externalExplain(
1691
    NodeBuilder& nb, AssertionOrder order) const
1692
{
1693
6879964
  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
6879964
  Assert(hasProof());
1701
6879964
  Assert(!isAssumption() || assertedToTheTheory());
1702
6879964
  Assert(!isInternalAssumption());
1703
6879964
  std::shared_ptr<ProofNode> pf{};
1704
1705
6879964
  ProofNodeManager* pnm = d_database->d_pnm;
1706
1707
6879964
  if (assertedBefore(order))
1708
  {
1709
6180148
    Debug("pf::arith::explain") << "  already asserted" << std::endl;
1710
6180148
    nb << getWitness();
1711
6180148
    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
699816
  else if (hasEqualityEngineProof())
1724
  {
1725
8008
    Debug("pf::arith::explain") << "  going to ee:" << std::endl;
1726
16016
    TrustNode exp = d_database->eeExplain(this);
1727
8008
    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
16016
    Debug("pf::arith::explain")
1749
8008
        << "    explanation: " << exp.getNode() << std::endl;
1750
8008
    if (exp.getNode().getKind() == Kind::AND)
1751
    {
1752
7097
      nb.append(exp.getNode().begin(), exp.getNode().end());
1753
    }
1754
    else
1755
    {
1756
911
      nb << exp.getNode();
1757
    }
1758
  }
1759
  else
1760
  {
1761
691808
    Debug("pf::arith::explain") << "  recursion!" << std::endl;
1762
691808
    Assert(!isAssumption());
1763
691808
    AntecedentId p = getEndAntecedent();
1764
691808
    ConstraintCP antecedent = d_database->d_antecedents[p];
1765
1383616
    std::vector<std::shared_ptr<ProofNode>> children;
1766
1767
4217644
    while (antecedent != NullConstraint)
1768
    {
1769
1762918
      Debug("pf::arith::explain") << "Explain " << antecedent << std::endl;
1770
3525836
      auto pn = antecedent->externalExplain(nb, order);
1771
1762918
      if (d_database->isProofEnabled())
1772
      {
1773
165977
        children.push_back(pn);
1774
      }
1775
1762918
      --p;
1776
1762918
      antecedent = d_database->d_antecedents[p];
1777
    }
1778
1779
691808
    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
6879964
  return pf;
1885
}
1886
1887
1988
Node Constraint::externalExplainByAssertions(ConstraintCP a, ConstraintCP b){
1888
3976
  NodeBuilder nb(kind::AND);
1889
1988
  a->externalExplainByAssertions(nb);
1890
1988
  b->externalExplainByAssertions(nb);
1891
3976
  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
819809
ConstraintP Constraint::getStrictlyWeakerLowerBound(bool hasLiteral, bool asserted) const {
1903
819809
  Assert(initialized());
1904
819809
  Assert(!asserted || hasLiteral);
1905
1906
819809
  SortedConstraintMapConstIterator i = d_variablePosition;
1907
819809
  const SortedConstraintMap& scm = constraintSet();
1908
819809
  SortedConstraintMapConstIterator i_begin = scm.begin();
1909
2786237
  while(i != i_begin){
1910
1126362
    --i;
1911
1126362
    const ValueCollection& vc = i->second;
1912
1126362
    if(vc.hasLowerBound()){
1913
315978
      ConstraintP weaker = vc.getLowerBound();
1914
1915
      // asserted -> hasLiteral
1916
      // hasLiteral -> weaker->hasLiteral()
1917
      // asserted -> weaker->assertedToTheTheory()
1918
616128
      if((!hasLiteral || (weaker->hasLiteral())) &&
1919
312771
         (!asserted || ( weaker->assertedToTheTheory()))){
1920
143148
        return weaker;
1921
      }
1922
    }
1923
  }
1924
676661
  return NullConstraint;
1925
}
1926
1927
436022
ConstraintP Constraint::getStrictlyWeakerUpperBound(bool hasLiteral, bool asserted) const {
1928
436022
  SortedConstraintMapConstIterator i = d_variablePosition;
1929
436022
  const SortedConstraintMap& scm = constraintSet();
1930
436022
  SortedConstraintMapConstIterator i_end = scm.end();
1931
1932
436022
  ++i;
1933
1283264
  for(; i != i_end; ++i){
1934
590818
    const ValueCollection& vc = i->second;
1935
590818
    if(vc.hasUpperBound()){
1936
223410
      ConstraintP weaker = vc.getUpperBound();
1937
571368
      if((!hasLiteral || (weaker->hasLiteral())) &&
1938
356180
         (!asserted || ( weaker->assertedToTheTheory()))){
1939
167197
        return weaker;
1940
      }
1941
    }
1942
  }
1943
1944
268825
  return NullConstraint;
1945
}
1946
1947
11067443
ConstraintP ConstraintDatabase::getBestImpliedBound(ArithVar v, ConstraintType t, const DeltaRational& r) const {
1948
11067443
  Assert(variableDatabaseIsSetup(v));
1949
11067443
  Assert(t == UpperBound || t == LowerBound);
1950
1951
11067443
  SortedConstraintMap& scm = getVariableSCM(v);
1952
11067443
  if(t == UpperBound){
1953
5554138
    SortedConstraintMapConstIterator i = scm.lower_bound(r);
1954
5554138
    SortedConstraintMapConstIterator i_end = scm.end();
1955
5554138
    Assert(i == i_end || r <= i->first);
1956
11130922
    for(; i != i_end; i++){
1957
4726665
      Assert(r <= i->first);
1958
4726665
      const ValueCollection& vc = i->second;
1959
4726665
      if(vc.hasUpperBound()){
1960
1938273
        return vc.getUpperBound();
1961
      }
1962
    }
1963
3615865
    return NullConstraint;
1964
  }else{
1965
5513305
    Assert(t == LowerBound);
1966
5513305
    if(scm.empty()){
1967
410563
      return NullConstraint;
1968
    }else{
1969
5102742
      SortedConstraintMapConstIterator i = scm.lower_bound(r);
1970
5102742
      SortedConstraintMapConstIterator i_begin = scm.begin();
1971
5102742
      SortedConstraintMapConstIterator i_end = scm.end();
1972
5102742
      Assert(i == i_end || r <= i->first);
1973
1974
5102742
      int fdj = 0;
1975
1976
5102742
      if(i == i_end){
1977
2279415
        --i;
1978
2279415
        Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl;
1979
2823327
      }else if( (i->first) > r){
1980
839835
        if(i == i_begin){
1981
749016
          return NullConstraint;
1982
        }else{
1983
90819
          --i;
1984
90819
          Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl;
1985
        }
1986
      }
1987
1988
      do{
1989
4824904
        Debug("getBestImpliedBound") << fdj++ << " " << r << " " << i->first << endl;
1990
4824904
        Assert(r >= i->first);
1991
4824904
        const ValueCollection& vc = i->second;
1992
1993
4824904
        if(vc.hasLowerBound()){
1994
2239733
          return vc.getLowerBound();
1995
        }
1996
1997
2585171
        if(i == i_begin){
1998
2113993
          break;
1999
        }else{
2000
471178
          --i;
2001
471178
        }
2002
      }while(true);
2003
2113993
      return NullConstraint;
2004
    }
2005
  }
2006
}
2007
8008
TrustNode ConstraintDatabase::eeExplain(const Constraint* const c) const
2008
{
2009
8008
  Assert(c->hasLiteral());
2010
8008
  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
30175509
bool ConstraintDatabase::variableDatabaseIsSetup(ArithVar v) const {
2021
30175509
  return v < d_varDatabases.size();
2022
}
2023
2024
2025
15272
ConstraintDatabase::Watches::Watches(context::Context* satContext, context::Context* userContext):
2026
  d_constraintProofs(satContext),
2027
  d_canBePropagatedWatches(satContext),
2028
  d_assertionOrderWatches(satContext),
2029
15272
  d_splitWatches(userContext)
2030
15272
{}
2031
2032
2033
606000
void Constraint::setLiteral(Node n) {
2034
606000
  Debug("arith::constraint") << "Mapping " << *this << " to " << n << std::endl;
2035
606000
  Assert(Comparison::isNormalAtom(n));
2036
606000
  Assert(!hasLiteral());
2037
606000
  Assert(sanityChecking(n));
2038
606000
  d_literal = n;
2039
606000
  NodetoConstraintMap& map = d_database->d_nodetoConstraintMap;
2040
606000
  Assert(map.find(n) == map.end());
2041
606000
  map.insert(make_pair(d_literal, this));
2042
606000
}
2043
2044
1464795
Node Constraint::getProofLiteral() const
2045
{
2046
1464795
  Assert(d_database != nullptr);
2047
1464795
  Assert(d_database->d_avariables.hasNode(d_variable));
2048
2929590
  Node varPart = d_database->d_avariables.asNode(d_variable);
2049
  Kind cmp;
2050
1464795
  bool neg = false;
2051
1464795
  switch (d_type)
2052
  {
2053
562267
    case ConstraintType::UpperBound:
2054
    {
2055
562267
      if (d_value.infinitesimalIsZero())
2056
      {
2057
159464
        cmp = Kind::LEQ;
2058
      }
2059
      else
2060
      {
2061
402803
        cmp = Kind::LT;
2062
      }
2063
562267
      break;
2064
    }
2065
239056
    case ConstraintType::LowerBound:
2066
    {
2067
239056
      if (d_value.infinitesimalIsZero())
2068
      {
2069
198449
        cmp = Kind::GEQ;
2070
      }
2071
      else
2072
      {
2073
40607
        cmp = Kind::GT;
2074
      }
2075
239056
      break;
2076
    }
2077
526476
    case ConstraintType::Equality:
2078
    {
2079
526476
      cmp = Kind::EQUAL;
2080
526476
      break;
2081
    }
2082
136996
    case ConstraintType::Disequality:
2083
    {
2084
136996
      cmp = Kind::EQUAL;
2085
136996
      neg = true;
2086
136996
      break;
2087
    }
2088
    default: Unreachable() << d_type;
2089
  }
2090
1464795
  NodeManager* nm = NodeManager::currentNM();
2091
2929590
  Node constPart = nm->mkConst<Rational>(d_value.getNoninfinitesimalPart());
2092
2929590
  Node posLit = nm->mkNode(cmp, varPart, constPart);
2093
2929590
  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
62450
void ConstraintDatabase::outputUnateInequalityLemmas(
2171
    std::vector<TrustNode>& out, ArithVar v) const
2172
{
2173
62450
  SortedConstraintMap& scm = getVariableSCM(v);
2174
62450
  SortedConstraintMapConstIterator scm_iter = scm.begin();
2175
62450
  SortedConstraintMapConstIterator scm_end = scm.end();
2176
62450
  ConstraintP prev = NullConstraint;
2177
  //get transitive unates
2178
  //Only lower bounds or upperbounds should be done.
2179
331434
  for(; scm_iter != scm_end; ++scm_iter){
2180
134492
    const ValueCollection& vc = scm_iter->second;
2181
134492
    if(vc.hasUpperBound()){
2182
64651
      ConstraintP ub = vc.getUpperBound();
2183
64651
      if(ub->hasLiteral()){
2184
64651
        if(prev != NullConstraint){
2185
28586
          implies(out, prev, ub);
2186
        }
2187
64651
        prev = ub;
2188
      }
2189
    }
2190
  }
2191
62450
}
2192
2193
62450
void ConstraintDatabase::outputUnateEqualityLemmas(std::vector<TrustNode>& out,
2194
                                                   ArithVar v) const
2195
{
2196
124900
  vector<ConstraintP> equalities;
2197
2198
62450
  SortedConstraintMap& scm = getVariableSCM(v);
2199
62450
  SortedConstraintMapConstIterator scm_iter = scm.begin();
2200
62450
  SortedConstraintMapConstIterator scm_end = scm.end();
2201
2202
331434
  for(; scm_iter != scm_end; ++scm_iter){
2203
134492
    const ValueCollection& vc = scm_iter->second;
2204
134492
    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
62450
  vector<ConstraintP>::const_iterator i, j, eq_end = equalities.end();
2213
81663
  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
81663
  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
62450
}
2246
2247
8880
void ConstraintDatabase::outputUnateEqualityLemmas(
2248
    std::vector<TrustNode>& lemmas) const
2249
{
2250
71330
  for(ArithVar v = 0, N = d_varDatabases.size(); v < N; ++v){
2251
62450
    outputUnateEqualityLemmas(lemmas, v);
2252
  }
2253
8880
}
2254
2255
8880
void ConstraintDatabase::outputUnateInequalityLemmas(
2256
    std::vector<TrustNode>& lemmas) const
2257
{
2258
71330
  for(ArithVar v = 0, N = d_varDatabases.size(); v < N; ++v){
2259
62450
    outputUnateInequalityLemmas(lemmas, v);
2260
  }
2261
8880
}
2262
2263
5557506
bool ConstraintDatabase::handleUnateProp(ConstraintP ant, ConstraintP cons){
2264
5557506
  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
5557506
  }else if(!cons->isTrue()){
2270
3021256
    ++d_statistics.d_unatePropagateImplications;
2271
3021256
    Debug("arith::unate") << "handleUnate: " << ant << " implies " << cons << endl;
2272
3021256
    cons->impliedByUnate(ant, false);
2273
3021256
    cons->tryToPropagate();
2274
3021256
    return false;
2275
  } else {
2276
2536250
    return false;
2277
  }
2278
}
2279
2280
1249942
void ConstraintDatabase::unatePropLowerBound(ConstraintP curr, ConstraintP prev){
2281
1249942
  Debug("arith::unate") << "unatePropLowerBound " << curr << " " << prev << endl;
2282
1249942
  Assert(curr != prev);
2283
1249942
  Assert(curr != NullConstraint);
2284
1249942
  bool hasPrev = ! (prev == NullConstraint);
2285
1249942
  Assert(!hasPrev || curr->getValue() > prev->getValue());
2286
2287
1249942
  ++d_statistics.d_unatePropagateCalls;
2288
2289
1249942
  const SortedConstraintMap& scm = curr->constraintSet();
2290
1249942
  const SortedConstraintMapConstIterator scm_begin = scm.begin();
2291
1249942
  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
7549034
  while(scm_i != scm_begin){
2298
3437629
    --scm_i; // move the iterator back
2299
2300
3437629
    const ValueCollection& vc = scm_i->second;
2301
2302
    //If it has the previous element, do nothing and stop!
2303
4367501
    if(hasPrev &&
2304
929872
       vc.hasConstraintOfType(prev->getType())
2305
4036256
       && vc.getConstraintOfType(prev->getType()) == prev){
2306
288083
      break;
2307
    }
2308
2309
    //Don't worry about implying the negation of upperbound.
2310
    //These should all be handled by propagating the LowerBounds!
2311
3149546
    if(vc.hasLowerBound()){
2312
1188947
      ConstraintP lb = vc.getLowerBound();
2313
1188947
      if(handleUnateProp(curr, lb)){ return; }
2314
    }
2315
3149546
    if(vc.hasDisequality()){
2316
338272
      ConstraintP dis = vc.getDisequality();
2317
338272
      if(handleUnateProp(curr, dis)){ return; }
2318
    }
2319
  }
2320
}
2321
2322
1223300
void ConstraintDatabase::unatePropUpperBound(ConstraintP curr, ConstraintP prev){
2323
1223300
  Debug("arith::unate") << "unatePropUpperBound " << curr << " " << prev << endl;
2324
1223300
  Assert(curr != prev);
2325
1223300
  Assert(curr != NullConstraint);
2326
1223300
  bool hasPrev = ! (prev == NullConstraint);
2327
1223300
  Assert(!hasPrev || curr->getValue() < prev->getValue());
2328
2329
1223300
  ++d_statistics.d_unatePropagateCalls;
2330
2331
1223300
  const SortedConstraintMap& scm = curr->constraintSet();
2332
1223300
  const SortedConstraintMapConstIterator scm_end = scm.end();
2333
1223300
  SortedConstraintMapConstIterator scm_i = curr->d_variablePosition;
2334
1223300
  ++scm_i;
2335
8687856
  for(; scm_i != scm_end; ++scm_i){
2336
3970262
    const ValueCollection& vc = scm_i->second;
2337
2338
    //If it has the previous element, do nothing and stop!
2339
4727508
    if(hasPrev &&
2340
4451773
       vc.hasConstraintOfType(prev->getType()) &&
2341
481511
       vc.getConstraintOfType(prev->getType()) == prev){
2342
237984
      break;
2343
    }
2344
    //Don't worry about implying the negation of upperbound.
2345
    //These should all be handled by propagating the UpperBounds!
2346
3732278
    if(vc.hasUpperBound()){
2347
1497511
      ConstraintP ub = vc.getUpperBound();
2348
1497511
      if(handleUnateProp(curr, ub)){ return; }
2349
    }
2350
3732278
    if(vc.hasDisequality()){
2351
306034
      ConstraintP dis = vc.getDisequality();
2352
306034
      if(handleUnateProp(curr, dis)){ return; }
2353
    }
2354
  }
2355
}
2356
2357
1223398
void ConstraintDatabase::unatePropEquality(ConstraintP curr, ConstraintP prevLB, ConstraintP prevUB){
2358
1223398
  Debug("arith::unate") << "unatePropEquality " << curr << " " << prevLB << " " << prevUB << endl;
2359
1223398
  Assert(curr != prevLB);
2360
1223398
  Assert(curr != prevUB);
2361
1223398
  Assert(curr != NullConstraint);
2362
1223398
  bool hasPrevLB = ! (prevLB == NullConstraint);
2363
1223398
  bool hasPrevUB = ! (prevUB == NullConstraint);
2364
1223398
  Assert(!hasPrevLB || curr->getValue() >= prevLB->getValue());
2365
1223398
  Assert(!hasPrevUB || curr->getValue() <= prevUB->getValue());
2366
2367
1223398
  ++d_statistics.d_unatePropagateCalls;
2368
2369
1223398
  const SortedConstraintMap& scm = curr->constraintSet();
2370
1223398
  SortedConstraintMapConstIterator scm_curr = curr->d_variablePosition;
2371
1223398
  SortedConstraintMapConstIterator scm_last = hasPrevUB ? prevUB->d_variablePosition : scm.end();
2372
1223398
  SortedConstraintMapConstIterator scm_i;
2373
1223398
  if(hasPrevLB){
2374
218514
    scm_i = prevLB->d_variablePosition;
2375
218514
    if(scm_i != scm_curr){ // If this does not move this past scm_curr, move it one forward
2376
44453
      ++scm_i;
2377
    }
2378
  }else{
2379
1004884
    scm_i = scm.begin();
2380
  }
2381
2382
3509460
  for(; scm_i != scm_curr; ++scm_i){
2383
    // between the previous LB and the curr
2384
1143031
    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
1143031
    if(vc.hasLowerBound()){
2389
430204
      ConstraintP lb = vc.getLowerBound();
2390
430204
      if(handleUnateProp(curr, lb)){ return; }
2391
    }
2392
1143031
    if(vc.hasDisequality()){
2393
374211
      ConstraintP dis = vc.getDisequality();
2394
374211
      if(handleUnateProp(curr, dis)){ return; }
2395
    }
2396
  }
2397
1223398
  Assert(scm_i == scm_curr);
2398
1223398
  if(!hasPrevUB || scm_i != scm_last){
2399
1191594
    ++scm_i;
2400
  } // hasPrevUB implies scm_i != scm_last
2401
2402
5758542
  for(; scm_i != scm_last; ++scm_i){
2403
    // between the curr and the previous UB imply the upperbounds and disequalities.
2404
2267572
    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
2267572
    if(vc.hasUpperBound()){
2409
851523
      ConstraintP ub = vc.getUpperBound();
2410
851523
      if(handleUnateProp(curr, ub)){ return; }
2411
    }
2412
2267572
    if(vc.hasDisequality()){
2413
570804
      ConstraintP dis = vc.getDisequality();
2414
570804
      if(handleUnateProp(curr, dis)){ return; }
2415
    }
2416
  }
2417
}
2418
2419
791875
std::pair<int, int> Constraint::unateFarkasSigns(ConstraintCP ca, ConstraintCP cb){
2420
791875
  ConstraintType a = ca->getType();
2421
791875
  ConstraintType b = cb->getType();
2422
2423
791875
  Assert(a != Disequality);
2424
791875
  Assert(b != Disequality);
2425
2426
791875
  int a_sgn = (a == LowerBound) ? -1 : ((a == UpperBound) ? 1 : 0);
2427
791875
  int b_sgn = (b == LowerBound) ? -1 : ((b == UpperBound) ? 1 : 0);
2428
2429
791875
  if(a_sgn == 0 && b_sgn == 0){
2430
189893
    Assert(a == Equality);
2431
189893
    Assert(b == Equality);
2432
189893
    Assert(ca->getValue() != cb->getValue());
2433
379786
    if(ca->getValue() < cb->getValue()){
2434
61176
      a_sgn = 1;
2435
61176
      b_sgn = -1;
2436
    }else{
2437
128717
      a_sgn = -1;
2438
128717
      b_sgn = 1;
2439
    }
2440
601982
  }else if(a_sgn == 0){
2441
132800
    Assert(b_sgn != 0);
2442
132800
    Assert(a == Equality);
2443
132800
    a_sgn = -b_sgn;
2444
469182
  }else if(b_sgn == 0){
2445
247988
    Assert(a_sgn != 0);
2446
247988
    Assert(b == Equality);
2447
247988
    b_sgn = -a_sgn;
2448
  }
2449
791875
  Assert(a_sgn != 0);
2450
791875
  Assert(b_sgn != 0);
2451
2452
1583750
  Debug("arith::unateFarkasSigns") << "Constraint::unateFarkasSigns("<<a <<", " << b << ") -> "
2453
791875
                                   << "("<<a_sgn<<", "<< b_sgn <<")"<< endl;
2454
791875
  return make_pair(a_sgn, b_sgn);
2455
}
2456
2457
}  // namespace arith
2458
}  // namespace theory
2459
31137
}  // namespace cvc5