GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/congruence_manager.cpp Lines: 359 411 87.3 %
Date: 2021-11-07 Branches: 729 1926 37.9 %

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