GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/congruence_manager.cpp Lines: 354 411 86.1 %
Date: 2021-09-29 Branches: 703 1910 36.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Tim King, Alex Ozdemir, Andrew Reynolds
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
19
#include "theory/arith/congruence_manager.h"
20
21
#include "base/output.h"
22
#include "options/arith_options.h"
23
#include "proof/proof_node.h"
24
#include "proof/proof_node_manager.h"
25
#include "smt/smt_statistics_registry.h"
26
#include "theory/arith/arith_utilities.h"
27
#include "theory/arith/constraint.h"
28
#include "theory/arith/partial_model.h"
29
#include "theory/ee_setup_info.h"
30
#include "theory/rewriter.h"
31
#include "theory/uf/equality_engine.h"
32
#include "theory/uf/proof_equality_engine.h"
33
34
namespace cvc5 {
35
namespace theory {
36
namespace arith {
37
38
6271
ArithCongruenceManager::ArithCongruenceManager(
39
    context::Context* c,
40
    context::UserContext* u,
41
    ConstraintDatabase& cd,
42
    SetupLiteralCallBack setup,
43
    const ArithVariables& avars,
44
    RaiseEqualityEngineConflict raiseConflict,
45
6271
    ProofNodeManager* pnm)
46
    : d_inConflict(c),
47
      d_raiseConflict(raiseConflict),
48
      d_notify(*this),
49
      d_keepAlive(c),
50
      d_propagatations(c),
51
      d_explanationMap(c),
52
      d_constraintDatabase(cd),
53
      d_setupLiteral(setup),
54
      d_avariables(avars),
55
      d_ee(nullptr),
56
      d_satContext(c),
57
      d_userContext(u),
58
      d_pnm(pnm),
59
      // Construct d_pfGenEe with the SAT context, since its proof include
60
      // unclosed assumptions of theory literals.
61
      d_pfGenEe(
62
6271
          new EagerProofGenerator(pnm, c, "ArithCongruenceManager::pfGenEe")),
63
      // Construct d_pfGenEe with the USER context, since its proofs are closed.
64
      d_pfGenExplain(new EagerProofGenerator(
65
6271
          pnm, u, "ArithCongruenceManager::pfGenExplain")),
66
18813
      d_pfee(nullptr)
67
{
68
6271
}
69
70
6268
ArithCongruenceManager::~ArithCongruenceManager() {}
71
72
6256
bool ArithCongruenceManager::needsEqualityEngine(EeSetupInfo& esi)
73
{
74
6256
  Assert(!options::arithEqSolver());
75
6256
  esi.d_notify = &d_notify;
76
6256
  esi.d_name = "arithCong::ee";
77
6256
  return true;
78
}
79
80
6256
void ArithCongruenceManager::finishInit(eq::EqualityEngine* ee)
81
{
82
6256
  if (options::arithEqSolver())
83
  {
84
    // use our own copy
85
    d_allocEe.reset(
86
        new eq::EqualityEngine(d_notify, d_satContext, "arithCong::ee", true));
87
    d_ee = d_allocEe.get();
88
    if (d_pnm != nullptr)
89
    {
90
      // allocate an internal proof equality engine
91
      d_allocPfee.reset(
92
          new eq::ProofEqEngine(d_satContext, d_userContext, *d_ee, d_pnm));
93
      d_ee->setProofEqualityEngine(d_allocPfee.get());
94
    }
95
  }
96
  else
97
  {
98
6256
    Assert(ee != nullptr);
99
    // otherwise, we use the official one
100
6256
    d_ee = ee;
101
  }
102
  // set the congruence kinds on the separate equality engine
103
6256
  d_ee->addFunctionKind(kind::NONLINEAR_MULT);
104
6256
  d_ee->addFunctionKind(kind::EXPONENTIAL);
105
6256
  d_ee->addFunctionKind(kind::SINE);
106
6256
  d_ee->addFunctionKind(kind::IAND);
107
6256
  d_ee->addFunctionKind(kind::POW2);
108
  // the proof equality engine is the one from the equality engine
109
6256
  d_pfee = d_ee->getProofEqualityEngine();
110
  // have proof equality engine only if proofs are enabled
111
6256
  Assert(isProofEnabled() == (d_pfee != nullptr));
112
6256
}
113
114
6271
ArithCongruenceManager::Statistics::Statistics()
115
6271
    : d_watchedVariables(smtStatisticsRegistry().registerInt(
116
12542
        "theory::arith::congruence::watchedVariables")),
117
6271
      d_watchedVariableIsZero(smtStatisticsRegistry().registerInt(
118
12542
          "theory::arith::congruence::watchedVariableIsZero")),
119
6271
      d_watchedVariableIsNotZero(smtStatisticsRegistry().registerInt(
120
12542
          "theory::arith::congruence::watchedVariableIsNotZero")),
121
6271
      d_equalsConstantCalls(smtStatisticsRegistry().registerInt(
122
12542
          "theory::arith::congruence::equalsConstantCalls")),
123
6271
      d_propagations(smtStatisticsRegistry().registerInt(
124
12542
          "theory::arith::congruence::propagations")),
125
6271
      d_propagateConstraints(smtStatisticsRegistry().registerInt(
126
12542
          "theory::arith::congruence::propagateConstraints")),
127
6271
      d_conflicts(smtStatisticsRegistry().registerInt(
128
43897
          "theory::arith::congruence::conflicts"))
129
{
130
6271
}
131
132
6271
ArithCongruenceManager::ArithCongruenceNotify::ArithCongruenceNotify(ArithCongruenceManager& acm)
133
6271
  : d_acm(acm)
134
6271
{}
135
136
bool ArithCongruenceManager::ArithCongruenceNotify::eqNotifyTriggerPredicate(
137
    TNode predicate, bool value)
138
{
139
  Assert(predicate.getKind() == kind::EQUAL);
140
  Debug("arith::congruences")
141
      << "ArithCongruenceNotify::eqNotifyTriggerPredicate(" << predicate << ", "
142
      << (value ? "true" : "false") << ")" << std::endl;
143
  if (value) {
144
    return d_acm.propagate(predicate);
145
  }
146
  return d_acm.propagate(predicate.notNode());
147
}
148
149
573727
bool ArithCongruenceManager::ArithCongruenceNotify::eqNotifyTriggerTermEquality(TheoryId tag, TNode t1, TNode t2, bool value) {
150
573727
  Debug("arith::congruences") << "ArithCongruenceNotify::eqNotifyTriggerTermEquality(" << t1 << ", " << t2 << ", " << (value ? "true" : "false") << ")" << std::endl;
151
573727
  if (value) {
152
483123
    return d_acm.propagate(t1.eqNode(t2));
153
  } else {
154
90604
    return d_acm.propagate(t1.eqNode(t2).notNode());
155
  }
156
}
157
976
void ArithCongruenceManager::ArithCongruenceNotify::eqNotifyConstantTermMerge(TNode t1, TNode t2) {
158
976
  Debug("arith::congruences") << "ArithCongruenceNotify::eqNotifyConstantTermMerge(" << t1 << ", " << t2 << std::endl;
159
976
  d_acm.propagate(t1.eqNode(t2));
160
976
}
161
759294
void ArithCongruenceManager::ArithCongruenceNotify::eqNotifyNewClass(TNode t) {
162
759294
}
163
938329
void ArithCongruenceManager::ArithCongruenceNotify::eqNotifyMerge(TNode t1,
164
                                                                  TNode t2)
165
{
166
938329
}
167
173190
void ArithCongruenceManager::ArithCongruenceNotify::eqNotifyDisequal(TNode t1, TNode t2, TNode reason) {
168
173190
}
169
170
1103
void ArithCongruenceManager::raiseConflict(Node conflict,
171
                                           std::shared_ptr<ProofNode> pf)
172
{
173
1103
  Assert(!inConflict());
174
1103
  Debug("arith::conflict") << "difference manager conflict   " << conflict << std::endl;
175
1103
  d_inConflict.raise();
176
1103
  d_raiseConflict.raiseEEConflict(conflict, pf);
177
1103
}
178
575806
bool ArithCongruenceManager::inConflict() const{
179
575806
  return d_inConflict.isRaised();
180
}
181
182
2780114
bool ArithCongruenceManager::hasMorePropagations() const {
183
2780114
  return !d_propagatations.empty();
184
}
185
457986
const Node ArithCongruenceManager::getNextPropagation() {
186
457986
  Assert(hasMorePropagations());
187
457986
  Node prop = d_propagatations.front();
188
457986
  d_propagatations.dequeue();
189
457986
  return prop;
190
}
191
192
13189
bool ArithCongruenceManager::canExplain(TNode n) const {
193
13189
  return d_explanationMap.find(n) != d_explanationMap.end();
194
}
195
196
8675
Node ArithCongruenceManager::externalToInternal(TNode n) const{
197
8675
  Assert(canExplain(n));
198
8675
  ExplainMap::const_iterator iter = d_explanationMap.find(n);
199
8675
  size_t pos = (*iter).second;
200
8675
  return d_propagatations[pos];
201
}
202
203
409543
void ArithCongruenceManager::pushBack(TNode n){
204
409543
  d_explanationMap.insert(n, d_propagatations.size());
205
409543
  d_propagatations.enqueue(n);
206
207
409543
  ++(d_statistics.d_propagations);
208
409543
}
209
72980
void ArithCongruenceManager::pushBack(TNode n, TNode r){
210
72980
  d_explanationMap.insert(r, d_propagatations.size());
211
72980
  d_explanationMap.insert(n, d_propagatations.size());
212
72980
  d_propagatations.enqueue(n);
213
214
72980
  ++(d_statistics.d_propagations);
215
72980
}
216
void ArithCongruenceManager::pushBack(TNode n, TNode r, TNode w){
217
  d_explanationMap.insert(w, d_propagatations.size());
218
  d_explanationMap.insert(r, d_propagatations.size());
219
  d_explanationMap.insert(n, d_propagatations.size());
220
  d_propagatations.enqueue(n);
221
222
  ++(d_statistics.d_propagations);
223
}
224
225
109581
void ArithCongruenceManager::watchedVariableIsZero(ConstraintCP lb, ConstraintCP ub){
226
109581
  Assert(lb->isLowerBound());
227
109581
  Assert(ub->isUpperBound());
228
109581
  Assert(lb->getVariable() == ub->getVariable());
229
109581
  Assert(lb->getValue().sgn() == 0);
230
109581
  Assert(ub->getValue().sgn() == 0);
231
232
109581
  ++(d_statistics.d_watchedVariableIsZero);
233
234
109581
  ArithVar s = lb->getVariable();
235
219162
  TNode eq = d_watchedEqualities[s];
236
109581
  ConstraintCP eqC = d_constraintDatabase.getConstraint(
237
109581
      s, ConstraintType::Equality, lb->getValue());
238
219162
  NodeBuilder reasonBuilder(Kind::AND);
239
219162
  auto pfLb = lb->externalExplainByAssertions(reasonBuilder);
240
219162
  auto pfUb = ub->externalExplainByAssertions(reasonBuilder);
241
219162
  Node reason = safeConstructNary(reasonBuilder);
242
219162
  std::shared_ptr<ProofNode> pf{};
243
109581
  if (isProofEnabled())
244
  {
245
16
    pf = d_pnm->mkNode(
246
12
        PfRule::ARITH_TRICHOTOMY, {pfLb, pfUb}, {eqC->getProofLiteral()});
247
4
    pf = d_pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM, {pf}, {eq});
248
  }
249
250
109581
  d_keepAlive.push_back(reason);
251
219162
  Trace("arith-ee") << "Asserting an equality on " << s << ", on trichotomy"
252
109581
                    << std::endl;
253
109581
  Trace("arith-ee") << "  based on " << lb << std::endl;
254
109581
  Trace("arith-ee") << "  based on " << ub << std::endl;
255
109581
  assertionToEqualityEngine(true, s, reason, pf);
256
109581
}
257
258
147899
void ArithCongruenceManager::watchedVariableIsZero(ConstraintCP eq){
259
147899
  Debug("arith::cong") << "Cong::watchedVariableIsZero: " << *eq << std::endl;
260
261
147899
  Assert(eq->isEquality());
262
147899
  Assert(eq->getValue().sgn() == 0);
263
264
147899
  ++(d_statistics.d_watchedVariableIsZero);
265
266
147899
  ArithVar s = eq->getVariable();
267
268
  //Explain for conflict is correct as these proofs are generated
269
  //and stored eagerly
270
  //These will be safe for propagation later as well
271
295798
  NodeBuilder nb(Kind::AND);
272
  // An open proof of eq from literals now in reason.
273
147899
  if (Debug.isOn("arith::cong"))
274
  {
275
    eq->printProofTree(Debug("arith::cong"));
276
  }
277
295798
  auto pf = eq->externalExplainByAssertions(nb);
278
147899
  if (isProofEnabled())
279
  {
280
27936
    pf = d_pnm->mkNode(
281
18624
        PfRule::MACRO_SR_PRED_TRANSFORM, {pf}, {d_watchedEqualities[s]});
282
  }
283
295798
  Node reason = safeConstructNary(nb);
284
285
147899
  d_keepAlive.push_back(reason);
286
147899
  assertionToEqualityEngine(true, s, reason, pf);
287
147899
}
288
289
360251
void ArithCongruenceManager::watchedVariableCannotBeZero(ConstraintCP c){
290
720502
  Debug("arith::cong::notzero")
291
360251
      << "Cong::watchedVariableCannotBeZero " << *c << std::endl;
292
360251
  ++(d_statistics.d_watchedVariableIsNotZero);
293
294
360251
  ArithVar s = c->getVariable();
295
720502
  Node disEq = d_watchedEqualities[s].negate();
296
297
  //Explain for conflict is correct as these proofs are generated and stored eagerly
298
  //These will be safe for propagation later as well
299
720502
  NodeBuilder nb(Kind::AND);
300
  // An open proof of eq from literals now in reason.
301
720502
  auto pf = c->externalExplainByAssertions(nb);
302
360251
  if (Debug.isOn("arith::cong::notzero"))
303
  {
304
    Debug("arith::cong::notzero") << "  original proof ";
305
    pf->printDebug(Debug("arith::cong::notzero"));
306
    Debug("arith::cong::notzero") << std::endl;
307
  }
308
720502
  Node reason = safeConstructNary(nb);
309
360251
  if (isProofEnabled())
310
  {
311
6339
    if (c->getType() == ConstraintType::Disequality)
312
    {
313
2310
      Assert(c->getLiteral() == d_watchedEqualities[s].negate());
314
      // We have to prove equivalence to the watched disequality.
315
2310
      pf = d_pnm->mkNode(PfRule::MACRO_SR_PRED_TRANSFORM, {pf}, {disEq});
316
    }
317
    else
318
    {
319
8058
      Debug("arith::cong::notzero")
320
4029
          << "  proof modification needed" << std::endl;
321
322
      // Four cases:
323
      //   c has form x_i = d, d > 0     => multiply c by -1 in Farkas proof
324
      //   c has form x_i = d, d > 0     => multiply c by 1 in Farkas proof
325
      //   c has form x_i <= d, d < 0     => multiply c by 1 in Farkas proof
326
      //   c has form x_i >= d, d > 0     => multiply c by -1 in Farkas proof
327
4029
      const bool scaleCNegatively = c->getType() == ConstraintType::LowerBound
328
5019
                                    || (c->getType() == ConstraintType::Equality
329
5375
                                        && c->getValue().sgn() > 0);
330
4029
      const int cSign = scaleCNegatively ? -1 : 1;
331
8058
      TNode isZero = d_watchedEqualities[s];
332
8058
      const auto isZeroPf = d_pnm->mkAssume(isZero);
333
4029
      const auto nm = NodeManager::currentNM();
334
4029
      const auto sumPf = d_pnm->mkNode(
335
          PfRule::MACRO_ARITH_SCALE_SUM_UB,
336
          {isZeroPf, pf},
337
          // Trick for getting correct, opposing signs.
338
8058
          {nm->mkConst(Rational(-1 * cSign)), nm->mkConst(Rational(cSign))});
339
4029
      const auto botPf = d_pnm->mkNode(
340
8058
          PfRule::MACRO_SR_PRED_TRANSFORM, {sumPf}, {nm->mkConst(false)});
341
8058
      std::vector<Node> assumption = {isZero};
342
4029
      pf = d_pnm->mkScope(botPf, assumption, false);
343
4029
      Debug("arith::cong::notzero") << "  new proof ";
344
4029
      pf->printDebug(Debug("arith::cong::notzero"));
345
4029
      Debug("arith::cong::notzero") << std::endl;
346
    }
347
6339
    Assert(pf->getResult() == disEq);
348
  }
349
360251
  d_keepAlive.push_back(reason);
350
360251
  assertionToEqualityEngine(false, s, reason, pf);
351
360251
}
352
353
354
574703
bool ArithCongruenceManager::propagate(TNode x){
355
574703
  Debug("arith::congruenceManager")<< "ArithCongruenceManager::propagate("<<x<<")"<<std::endl;
356
574703
  if(inConflict()){
357
    return true;
358
  }
359
360
1149406
  Node rewritten = Rewriter::rewrite(x);
361
362
  //Need to still propagate this!
363
574703
  if(rewritten.getKind() == kind::CONST_BOOLEAN){
364
1123
    pushBack(x);
365
366
1123
    if(rewritten.getConst<bool>()){
367
147
      return true;
368
    }else{
369
      // x rewrites to false.
370
976
      ++(d_statistics.d_conflicts);
371
1952
      TrustNode trn = explainInternal(x);
372
1952
      Node conf = flattenAnd(trn.getNode());
373
976
      Debug("arith::congruenceManager") << "rewritten to false "<<x<<" with explanation "<< conf << std::endl;
374
976
      if (isProofEnabled())
375
      {
376
        auto pf = trn.getGenerator()->getProofFor(trn.getProven());
377
        auto confPf = d_pnm->mkNode(
378
            PfRule::MACRO_SR_PRED_TRANSFORM, {pf}, {conf.negate()});
379
        raiseConflict(conf, confPf);
380
      }
381
      else
382
      {
383
976
        raiseConflict(conf);
384
      }
385
976
      return false;
386
    }
387
  }
388
389
573580
  Assert(rewritten.getKind() != kind::CONST_BOOLEAN);
390
391
573580
  ConstraintP c = d_constraintDatabase.lookup(rewritten);
392
573580
  if(c == NullConstraint){
393
    //using setup as there may not be a corresponding congruence literal yet
394
11882
    d_setupLiteral(rewritten);
395
11882
    c = d_constraintDatabase.lookup(rewritten);
396
11882
    Assert(c != NullConstraint);
397
  }
398
399
1147160
  Debug("arith::congruenceManager")<< "x is "
400
1147160
                                   <<  c->hasProof() << " "
401
1147160
                                   << (x == rewritten) << " "
402
1147160
                                   << c->canBePropagated() << " "
403
573580
                                   << c->negationHasProof() << std::endl;
404
405
573580
  if(c->negationHasProof()){
406
254
    TrustNode texpC = explainInternal(x);
407
254
    Node expC = texpC.getNode();
408
127
    ConstraintCP negC = c->getNegation();
409
254
    Node neg = Constraint::externalExplainByAssertions({negC});
410
254
    Node conf = expC.andNode(neg);
411
254
    Node final = flattenAnd(conf);
412
413
127
    ++(d_statistics.d_conflicts);
414
127
    raiseConflict(final);
415
127
    Debug("arith::congruenceManager") << "congruenceManager found a conflict " << final << std::endl;
416
127
    return false;
417
  }
418
419
  // Cases for propagation
420
  // C : c has a proof
421
  // S : x == rewritten
422
  // P : c can be propagated
423
  //
424
  // CSP
425
  // 000 : propagate x, and mark C it as being explained
426
  // 001 : propagate x, and propagate c after marking it as being explained
427
  // 01* : propagate x, mark c but do not propagate c
428
  // 10* : propagate x, do not mark c and do not propagate c
429
  // 11* : drop the constraint, do not propagate x or c
430
431
573453
  if(!c->hasProof() && x != rewritten){
432
72980
    if(c->assertedToTheTheory()){
433
      pushBack(x, rewritten, c->getWitness());
434
    }else{
435
72980
      pushBack(x, rewritten);
436
    }
437
438
72980
    c->setEqualityEngineProof();
439
72980
    if(c->canBePropagated() && !c->assertedToTheTheory()){
440
441
10053
      ++(d_statistics.d_propagateConstraints);
442
10053
      c->propagate();
443
    }
444
500473
  }else if(!c->hasProof() && x == rewritten){
445
58211
    if(c->assertedToTheTheory()){
446
      pushBack(x, c->getWitness());
447
    }else{
448
58211
      pushBack(x);
449
    }
450
58211
    c->setEqualityEngineProof();
451
442262
  }else if(c->hasProof() && x != rewritten){
452
350209
    if(c->assertedToTheTheory()){
453
349064
      pushBack(x);
454
    }else{
455
1145
      pushBack(x);
456
    }
457
  }else{
458
92053
    Assert(c->hasProof() && x == rewritten);
459
  }
460
573453
  return true;
461
}
462
463
void ArithCongruenceManager::explain(TNode literal, std::vector<TNode>& assumptions) {
464
  if (literal.getKind() != kind::NOT) {
465
    d_ee->explainEquality(literal[0], literal[1], true, assumptions);
466
  } else {
467
    d_ee->explainEquality(literal[0][0], literal[0][1], false, assumptions);
468
  }
469
}
470
471
void ArithCongruenceManager::enqueueIntoNB(const std::set<TNode> s,
472
                                           NodeBuilder& nb)
473
{
474
  std::set<TNode>::const_iterator it = s.begin();
475
  std::set<TNode>::const_iterator it_end = s.end();
476
  for(; it != it_end; ++it) {
477
    nb << *it;
478
  }
479
}
480
481
9778
TrustNode ArithCongruenceManager::explainInternal(TNode internal)
482
{
483
9778
  if (isProofEnabled())
484
  {
485
304
    return d_pfee->explain(internal);
486
  }
487
  // otherwise, explain without proof generator
488
18948
  Node exp = d_ee->mkExplainLit(internal);
489
9474
  return TrustNode::mkTrustPropExp(internal, exp, nullptr);
490
}
491
492
8675
TrustNode ArithCongruenceManager::explain(TNode external)
493
{
494
8675
  Trace("arith-ee") << "Ask for explanation of " << external << std::endl;
495
17350
  Node internal = externalToInternal(external);
496
8675
  Trace("arith-ee") << "...internal = " << internal << std::endl;
497
17350
  TrustNode trn = explainInternal(internal);
498
8675
  if (isProofEnabled() && trn.getProven()[1] != external)
499
  {
500
124
    Assert(trn.getKind() == TrustNodeKind::PROP_EXP);
501
124
    Assert(trn.getProven().getKind() == Kind::IMPLIES);
502
124
    Assert(trn.getGenerator() != nullptr);
503
248
    Trace("arith-ee") << "tweaking proof to prove " << external << " not "
504
124
                      << trn.getProven()[1] << std::endl;
505
248
    std::vector<std::shared_ptr<ProofNode>> assumptionPfs;
506
248
    std::vector<Node> assumptions = andComponents(trn.getNode());
507
124
    assumptionPfs.push_back(trn.toProofNode());
508
382
    for (const auto& a : assumptions)
509
    {
510
258
      assumptionPfs.push_back(
511
516
          d_pnm->mkNode(PfRule::TRUE_INTRO, {d_pnm->mkAssume(a)}, {}));
512
    }
513
124
    auto litPf = d_pnm->mkNode(
514
248
        PfRule::MACRO_SR_PRED_TRANSFORM, {assumptionPfs}, {external});
515
248
    auto extPf = d_pnm->mkScope(litPf, assumptions);
516
124
    return d_pfGenExplain->mkTrustedPropagation(external, trn.getNode(), extPf);
517
  }
518
8551
  return trn;
519
}
520
521
void ArithCongruenceManager::explain(TNode external, NodeBuilder& out)
522
{
523
  Node internal = externalToInternal(external);
524
525
  std::vector<TNode> assumptions;
526
  explain(internal, assumptions);
527
  std::set<TNode> assumptionSet;
528
  assumptionSet.insert(assumptions.begin(), assumptions.end());
529
530
  enqueueIntoNB(assumptionSet, out);
531
}
532
533
34176
void ArithCongruenceManager::addWatchedPair(ArithVar s, TNode x, TNode y){
534
34176
  Assert(!isWatchedVariable(s));
535
536
68352
  Debug("arith::congruenceManager")
537
34176
    << "addWatchedPair(" << s << ", " << x << ", " << y << ")" << std::endl;
538
539
540
34176
  ++(d_statistics.d_watchedVariables);
541
542
34176
  d_watchedVariables.add(s);
543
544
68352
  Node eq = x.eqNode(y);
545
34176
  d_watchedEqualities.set(s, eq);
546
34176
}
547
548
1218859
void ArithCongruenceManager::assertLitToEqualityEngine(
549
    Node lit, TNode reason, std::shared_ptr<ProofNode> pf)
550
{
551
1218859
  bool isEquality = lit.getKind() != Kind::NOT;
552
2437718
  Node eq = isEquality ? lit : lit[0];
553
1218859
  Assert(eq.getKind() == Kind::EQUAL);
554
555
2437718
  Trace("arith-ee") << "Assert to Eq " << lit << ", reason " << reason
556
1218859
                    << std::endl;
557
1218859
  if (isProofEnabled())
558
  {
559
41363
    if (CDProof::isSame(lit, reason))
560
    {
561
30386
      Trace("arith-pfee") << "Asserting only, b/c implied by symm" << std::endl;
562
      // The equality engine doesn't ref-count for us...
563
30386
      d_keepAlive.push_back(eq);
564
30386
      d_keepAlive.push_back(reason);
565
30386
      d_ee->assertEquality(eq, isEquality, reason);
566
    }
567
10977
    else if (hasProofFor(lit))
568
    {
569
74
      Trace("arith-pfee") << "Skipping b/c already done" << std::endl;
570
    }
571
    else
572
    {
573
10903
      setProofFor(lit, pf);
574
10903
      Trace("arith-pfee") << "Actually asserting" << std::endl;
575
10903
      if (Debug.isOn("arith-pfee"))
576
      {
577
        Trace("arith-pfee") << "Proof: ";
578
        pf->printDebug(Trace("arith-pfee"));
579
        Trace("arith-pfee") << std::endl;
580
      }
581
      // The proof equality engine *does* ref-count for us...
582
10903
      d_pfee->assertFact(lit, reason, d_pfGenEe.get());
583
    }
584
  }
585
  else
586
  {
587
    // The equality engine doesn't ref-count for us...
588
1177496
    d_keepAlive.push_back(eq);
589
1177496
    d_keepAlive.push_back(reason);
590
1177496
    d_ee->assertEquality(eq, isEquality, reason);
591
  }
592
1218859
}
593
594
617731
void ArithCongruenceManager::assertionToEqualityEngine(
595
    bool isEquality, ArithVar s, TNode reason, std::shared_ptr<ProofNode> pf)
596
{
597
617731
  Assert(isWatchedVariable(s));
598
599
1235462
  TNode eq = d_watchedEqualities[s];
600
617731
  Assert(eq.getKind() == kind::EQUAL);
601
602
1235462
  Node lit = isEquality ? Node(eq) : eq.notNode();
603
1235462
  Trace("arith-ee") << "Assert to Eq " << eq << ", pol " << isEquality
604
617731
                    << ", reason " << reason << std::endl;
605
617731
  assertLitToEqualityEngine(lit, reason, pf);
606
617731
}
607
608
21880
bool ArithCongruenceManager::hasProofFor(TNode f) const
609
{
610
21880
  Assert(isProofEnabled());
611
21880
  if (d_pfGenEe->hasProofFor(f))
612
  {
613
74
    return true;
614
  }
615
43612
  Node sym = CDProof::getSymmFact(f);
616
21806
  Assert(!sym.isNull());
617
21806
  return d_pfGenEe->hasProofFor(sym);
618
}
619
620
10903
void ArithCongruenceManager::setProofFor(TNode f,
621
                                         std::shared_ptr<ProofNode> pf) const
622
{
623
10903
  Assert(!hasProofFor(f));
624
10903
  d_pfGenEe->mkTrustNode(f, pf);
625
21806
  Node symF = CDProof::getSymmFact(f);
626
21806
  auto symPf = d_pnm->mkNode(PfRule::SYMM, {pf}, {});
627
10903
  d_pfGenEe->mkTrustNode(symF, symPf);
628
10903
}
629
630
511243
void ArithCongruenceManager::equalsConstant(ConstraintCP c){
631
511243
  Assert(c->isEquality());
632
633
511243
  ++(d_statistics.d_equalsConstantCalls);
634
511243
  Debug("equalsConstant") << "equals constant " << c << std::endl;
635
636
511243
  ArithVar x = c->getVariable();
637
1022486
  Node xAsNode = d_avariables.asNode(x);
638
1022486
  Node asRational = mkRationalNode(c->getValue().getNoninfinitesimalPart());
639
640
  // No guarentee this is in normal form!
641
  // Note though, that it happens to be in proof normal form!
642
1022486
  Node eq = xAsNode.eqNode(asRational);
643
511243
  d_keepAlive.push_back(eq);
644
645
1022486
  NodeBuilder nb(Kind::AND);
646
1022486
  auto pf = c->externalExplainByAssertions(nb);
647
1022486
  Node reason = safeConstructNary(nb);
648
511243
  d_keepAlive.push_back(reason);
649
650
511243
  Trace("arith-ee") << "Assert equalsConstant " << eq << ", reason " << reason << std::endl;
651
511243
  assertLitToEqualityEngine(eq, reason, pf);
652
511243
}
653
654
89885
void ArithCongruenceManager::equalsConstant(ConstraintCP lb, ConstraintCP ub){
655
89885
  Assert(lb->isLowerBound());
656
89885
  Assert(ub->isUpperBound());
657
89885
  Assert(lb->getVariable() == ub->getVariable());
658
659
89885
  ++(d_statistics.d_equalsConstantCalls);
660
179770
  Debug("equalsConstant") << "equals constant " << lb << std::endl
661
89885
                          << ub << std::endl;
662
663
89885
  ArithVar x = lb->getVariable();
664
179770
  NodeBuilder nb(Kind::AND);
665
179770
  auto pfLb = lb->externalExplainByAssertions(nb);
666
179770
  auto pfUb = ub->externalExplainByAssertions(nb);
667
179770
  Node reason = safeConstructNary(nb);
668
669
179770
  Node xAsNode = d_avariables.asNode(x);
670
179770
  Node asRational = mkRationalNode(lb->getValue().getNoninfinitesimalPart());
671
672
  // No guarentee this is in normal form!
673
  // Note though, that it happens to be in proof normal form!
674
179770
  Node eq = xAsNode.eqNode(asRational);
675
179770
  std::shared_ptr<ProofNode> pf;
676
89885
  if (isProofEnabled())
677
  {
678
922
    pf = d_pnm->mkNode(PfRule::ARITH_TRICHOTOMY, {pfLb, pfUb}, {eq});
679
  }
680
89885
  d_keepAlive.push_back(eq);
681
89885
  d_keepAlive.push_back(reason);
682
683
89885
  Trace("arith-ee") << "Assert equalsConstant2 " << eq << ", reason " << reason << std::endl;
684
685
89885
  assertLitToEqualityEngine(eq, reason, pf);
686
89885
}
687
688
1974040
bool ArithCongruenceManager::isProofEnabled() const { return d_pnm != nullptr; }
689
690
124
std::vector<Node> andComponents(TNode an)
691
{
692
124
  auto nm = NodeManager::currentNM();
693
124
  if (an == nm->mkConst(true))
694
  {
695
    return {};
696
  }
697
124
  else if (an.getKind() != Kind::AND)
698
  {
699
    return {an};
700
  }
701
248
  std::vector<Node> a{};
702
124
  a.reserve(an.getNumChildren());
703
124
  a.insert(a.end(), an.begin(), an.end());
704
124
  return a;
705
}
706
707
}  // namespace arith
708
}  // namespace theory
709
22746
}  // namespace cvc5