GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/theory_inference_manager.cpp Lines: 219 244 89.8 %
Date: 2021-11-07 Branches: 359 844 42.5 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Gereon Kremer, Mathias Preiner
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
 * An inference manager for Theory.
14
 */
15
16
#include "theory/theory_inference_manager.h"
17
18
#include "smt/smt_statistics_registry.h"
19
#include "theory/output_channel.h"
20
#include "theory/rewriter.h"
21
#include "theory/theory.h"
22
#include "theory/theory_state.h"
23
#include "theory/uf/equality_engine.h"
24
#include "theory/uf/proof_equality_engine.h"
25
26
using namespace cvc5::kind;
27
28
namespace cvc5 {
29
namespace theory {
30
31
183276
TheoryInferenceManager::TheoryInferenceManager(Env& env,
32
                                               Theory& t,
33
                                               TheoryState& state,
34
                                               const std::string& statsName,
35
183276
                                               bool cacheLemmas)
36
    : EnvObj(env),
37
      d_theory(t),
38
      d_theoryState(state),
39
183276
      d_out(t.getOutputChannel()),
40
      d_ee(nullptr),
41
      d_decManager(nullptr),
42
      d_pfee(nullptr),
43
      d_cacheLemmas(cacheLemmas),
44
      d_keep(context()),
45
183276
      d_lemmasSent(userContext()),
46
      d_numConflicts(0),
47
      d_numCurrentLemmas(0),
48
      d_numCurrentFacts(0),
49
183276
      d_conflictIdStats(statisticsRegistry().registerHistogram<InferenceId>(
50
366552
          statsName + "inferencesConflict")),
51
183276
      d_factIdStats(statisticsRegistry().registerHistogram<InferenceId>(
52
366552
          statsName + "inferencesFact")),
53
183276
      d_lemmaIdStats(statisticsRegistry().registerHistogram<InferenceId>(
54
1099656
          statsName + "inferencesLemma"))
55
{
56
  // don't add true lemma
57
366552
  Node truen = NodeManager::currentNM()->mkConst(true);
58
183276
  d_lemmasSent.insert(truen);
59
183276
}
60
61
183216
TheoryInferenceManager::~TheoryInferenceManager()
62
{
63
183216
}
64
65
183276
void TheoryInferenceManager::setEqualityEngine(eq::EqualityEngine* ee)
66
{
67
183276
  d_ee = ee;
68
  // if proofs are enabled, also make a proof equality engine to wrap ee
69
  // if it is non-null. If its proof equality engine has already been assigned,
70
  // use it. This is to ensure that all theories use the same proof equality
71
  // engine when in ee-mode=central.
72
183276
  if (isProofEnabled() && d_ee != nullptr)
73
  {
74
58580
    d_pfee = d_ee->getProofEqualityEngine();
75
58580
    if (d_pfee == nullptr)
76
    {
77
58492
      d_pfeeAlloc = std::make_unique<eq::ProofEqEngine>(d_env, *d_ee);
78
58492
      d_pfee = d_pfeeAlloc.get();
79
58492
      d_ee->setProofEqualityEngine(d_pfee);
80
    }
81
  }
82
183276
}
83
84
183276
void TheoryInferenceManager::setDecisionManager(DecisionManager* dm)
85
{
86
183276
  d_decManager = dm;
87
183276
}
88
89
863545
bool TheoryInferenceManager::isProofEnabled() const
90
{
91
863545
  return d_env.isTheoryProofProducing();
92
}
93
94
3917122
void TheoryInferenceManager::reset()
95
{
96
3917122
  d_numConflicts = 0;
97
3917122
  d_numCurrentLemmas = 0;
98
3917122
  d_numCurrentFacts = 0;
99
3917122
}
100
101
2736403
bool TheoryInferenceManager::hasSent() const
102
{
103
5472806
  return d_theoryState.isInConflict() || d_numCurrentLemmas > 0
104
5461018
         || d_numCurrentFacts > 0;
105
}
106
107
eq::ProofEqEngine* TheoryInferenceManager::getProofEqEngine() { return d_pfee; }
108
109
15372
void TheoryInferenceManager::conflictEqConstantMerge(TNode a, TNode b)
110
{
111
15372
  if (!d_theoryState.isInConflict())
112
  {
113
30404
    TrustNode tconf = explainConflictEqConstantMerge(a, b);
114
15202
    trustedConflict(tconf, InferenceId::EQ_CONSTANT_MERGE);
115
  }
116
15372
}
117
118
103964
void TheoryInferenceManager::conflict(TNode conf, InferenceId id)
119
{
120
207928
  TrustNode tconf = TrustNode::mkTrustConflict(conf, nullptr);
121
207928
  return trustedConflict(tconf, id);
122
}
123
124
138742
void TheoryInferenceManager::trustedConflict(TrustNode tconf, InferenceId id)
125
{
126
138742
  d_conflictIdStats << id;
127
138742
  resourceManager()->spendResource(id);
128
277484
  Trace("im") << "(conflict " << id << " " << tconf.getProven() << ")"
129
138742
              << std::endl;
130
138742
  d_out.trustedConflict(tconf);
131
138742
  ++d_numConflicts;
132
138742
}
133
134
8
void TheoryInferenceManager::conflictExp(InferenceId id,
135
                                         PfRule pfr,
136
                                         const std::vector<Node>& exp,
137
                                         const std::vector<Node>& args)
138
{
139
8
  if (!d_theoryState.isInConflict())
140
  {
141
    // make the trust node
142
16
    TrustNode tconf = mkConflictExp(pfr, exp, args);
143
    // send it on the output channel
144
8
    trustedConflict(tconf, id);
145
  }
146
8
}
147
148
8
TrustNode TheoryInferenceManager::mkConflictExp(PfRule id,
149
                                                const std::vector<Node>& exp,
150
                                                const std::vector<Node>& args)
151
{
152
8
  if (d_pfee != nullptr)
153
  {
154
    // use proof equality engine to construct the trust node
155
2
    return d_pfee->assertConflict(id, exp, args);
156
  }
157
  // version without proofs
158
12
  Node conf = mkExplainPartial(exp, {});
159
6
  return TrustNode::mkTrustConflict(conf, nullptr);
160
}
161
162
4090
void TheoryInferenceManager::conflictExp(InferenceId id,
163
                                         const std::vector<Node>& exp,
164
                                         ProofGenerator* pg)
165
{
166
4090
  if (!d_theoryState.isInConflict())
167
  {
168
    // make the trust node
169
7758
    TrustNode tconf = mkConflictExp(exp, pg);
170
    // send it on the output channel
171
3879
    trustedConflict(tconf, id);
172
  }
173
4090
}
174
175
5831
TrustNode TheoryInferenceManager::mkConflictExp(const std::vector<Node>& exp,
176
                                                ProofGenerator* pg)
177
{
178
5831
  if (d_pfee != nullptr)
179
  {
180
485
    Assert(pg != nullptr);
181
    // use proof equality engine to construct the trust node
182
485
    return d_pfee->assertConflict(exp, pg);
183
  }
184
  // version without proofs
185
10692
  Node conf = mkExplainPartial(exp, {});
186
5346
  return TrustNode::mkTrustConflict(conf, nullptr);
187
}
188
189
13342708
bool TheoryInferenceManager::propagateLit(TNode lit)
190
{
191
  // If already in conflict, no more propagation
192
13342708
  if (d_theoryState.isInConflict())
193
  {
194
2384
    return false;
195
  }
196
  // Propagate out
197
13340324
  bool ok = d_out.propagate(lit);
198
13340324
  if (!ok)
199
  {
200
57658
    d_theoryState.notifyInConflict();
201
  }
202
13340324
  return ok;
203
}
204
205
226053
TrustNode TheoryInferenceManager::explainLit(TNode lit)
206
{
207
226053
  if (d_pfee != nullptr)
208
  {
209
33746
    return d_pfee->explain(lit);
210
  }
211
192307
  if (d_ee != nullptr)
212
  {
213
384614
    Node exp = d_ee->mkExplainLit(lit);
214
192307
    return TrustNode::mkTrustPropExp(lit, exp, nullptr);
215
  }
216
  Unimplemented() << "Inference manager for " << d_theory.getId()
217
                  << " was asked to explain a propagation but doesn't have an "
218
                     "equality engine or implement the "
219
                     "TheoryInferenceManager::explainLit interface!";
220
}
221
222
15202
TrustNode TheoryInferenceManager::explainConflictEqConstantMerge(TNode a,
223
                                                                 TNode b)
224
{
225
30404
  Node lit = a.eqNode(b);
226
15202
  if (d_pfee != nullptr)
227
  {
228
1254
    return d_pfee->assertConflict(lit);
229
  }
230
13948
  if (d_ee != nullptr)
231
  {
232
27896
    Node conf = d_ee->mkExplainLit(lit);
233
13948
    return TrustNode::mkTrustConflict(conf, nullptr);
234
  }
235
  Unimplemented() << "Inference manager for " << d_theory.getId()
236
                  << " mkTrustedConflictEqConstantMerge";
237
}
238
239
2141967
bool TheoryInferenceManager::lemma(TNode lem, InferenceId id, LemmaProperty p)
240
{
241
4283933
  TrustNode tlem = TrustNode::mkTrustLemma(lem, nullptr);
242
4283933
  return trustedLemma(tlem, id, p);
243
}
244
245
2553381
bool TheoryInferenceManager::trustedLemma(const TrustNode& tlem,
246
                                          InferenceId id,
247
                                          LemmaProperty p)
248
{
249
  // if the policy says to cache lemmas, check the cache and return false if
250
  // we are a duplicate
251
2553381
  if (d_cacheLemmas)
252
  {
253
2492641
    if (!cacheLemma(tlem.getNode(), p))
254
    {
255
2188257
      return false;
256
    }
257
  }
258
365124
  d_lemmaIdStats << id;
259
365124
  resourceManager()->spendResource(id);
260
365124
  Trace("im") << "(lemma " << id << " " << tlem.getProven() << ")" << std::endl;
261
  // shouldn't send trivially true or false lemmas
262
365124
  Assert(!rewrite(tlem.getProven()).isConst());
263
365124
  d_numCurrentLemmas++;
264
365126
  d_out.trustedLemma(tlem, p);
265
365122
  return true;
266
}
267
268
bool TheoryInferenceManager::lemmaExp(Node conc,
269
                                      InferenceId id,
270
                                      PfRule pfr,
271
                                      const std::vector<Node>& exp,
272
                                      const std::vector<Node>& noExplain,
273
                                      const std::vector<Node>& args,
274
                                      LemmaProperty p)
275
{
276
  // make the trust node
277
  TrustNode trn = mkLemmaExp(conc, pfr, exp, noExplain, args);
278
  // send it on the output channel
279
  return trustedLemma(trn, id, p);
280
}
281
282
386
TrustNode TheoryInferenceManager::mkLemmaExp(Node conc,
283
                                             PfRule id,
284
                                             const std::vector<Node>& exp,
285
                                             const std::vector<Node>& noExplain,
286
                                             const std::vector<Node>& args)
287
{
288
386
  if (d_pfee != nullptr)
289
  {
290
    // make the trust node from the proof equality engine
291
51
    return d_pfee->assertLemma(conc, id, exp, noExplain, args);
292
  }
293
  // otherwise, not using proofs, explain and make trust node
294
670
  Node ant = mkExplainPartial(exp, noExplain);
295
670
  Node lem = NodeManager::currentNM()->mkNode(kind::IMPLIES, ant, conc);
296
335
  return TrustNode::mkTrustLemma(lem, nullptr);
297
}
298
299
bool TheoryInferenceManager::lemmaExp(Node conc,
300
                                      InferenceId id,
301
                                      const std::vector<Node>& exp,
302
                                      const std::vector<Node>& noExplain,
303
                                      ProofGenerator* pg,
304
                                      LemmaProperty p)
305
{
306
  // make the trust node
307
  TrustNode trn = mkLemmaExp(conc, exp, noExplain, pg);
308
  // send it on the output channel
309
  return trustedLemma(trn, id, p);
310
}
311
312
25507
TrustNode TheoryInferenceManager::mkLemmaExp(Node conc,
313
                                             const std::vector<Node>& exp,
314
                                             const std::vector<Node>& noExplain,
315
                                             ProofGenerator* pg)
316
{
317
25507
  if (d_pfee != nullptr)
318
  {
319
    // make the trust node from the proof equality engine
320
4982
    return d_pfee->assertLemma(conc, exp, noExplain, pg);
321
  }
322
  // otherwise, not using proofs, explain and make trust node
323
41050
  Node ant = mkExplainPartial(exp, noExplain);
324
41050
  Node lem = NodeManager::currentNM()->mkNode(kind::IMPLIES, ant, conc);
325
20525
  return TrustNode::mkTrustLemma(lem, nullptr);
326
}
327
328
173980
bool TheoryInferenceManager::hasCachedLemma(TNode lem, LemmaProperty p)
329
{
330
347960
  Node rewritten = rewrite(lem);
331
347960
  return d_lemmasSent.find(rewritten) != d_lemmasSent.end();
332
}
333
334
uint32_t TheoryInferenceManager::numSentLemmas() const
335
{
336
  return d_numCurrentLemmas;
337
}
338
339
698665
bool TheoryInferenceManager::hasSentLemma() const
340
{
341
698665
  return d_numCurrentLemmas != 0;
342
}
343
344
14513
bool TheoryInferenceManager::assertInternalFact(TNode atom,
345
                                                bool pol,
346
                                                InferenceId id,
347
                                                TNode exp)
348
{
349
43539
  return processInternalFact(
350
43539
      atom, pol, id, PfRule::UNKNOWN, {exp}, {}, nullptr);
351
}
352
353
30662
bool TheoryInferenceManager::assertInternalFact(TNode atom,
354
                                                bool pol,
355
                                                InferenceId id,
356
                                                PfRule pfr,
357
                                                const std::vector<Node>& exp,
358
                                                const std::vector<Node>& args)
359
{
360
30662
  Assert(pfr != PfRule::UNKNOWN);
361
30662
  return processInternalFact(atom, pol, id, pfr, exp, args, nullptr);
362
}
363
364
553459
bool TheoryInferenceManager::assertInternalFact(TNode atom,
365
                                                bool pol,
366
                                                InferenceId id,
367
                                                const std::vector<Node>& exp,
368
                                                ProofGenerator* pg)
369
{
370
553459
  return processInternalFact(atom, pol, id, PfRule::ASSUME, exp, {}, pg);
371
}
372
373
598634
bool TheoryInferenceManager::processInternalFact(TNode atom,
374
                                                 bool pol,
375
                                                 InferenceId iid,
376
                                                 PfRule id,
377
                                                 const std::vector<Node>& exp,
378
                                                 const std::vector<Node>& args,
379
                                                 ProofGenerator* pg)
380
{
381
598634
  d_factIdStats << iid;
382
598634
  resourceManager()->spendResource(iid);
383
  // make the node corresponding to the explanation
384
1197268
  Node expn = NodeManager::currentNM()->mkAnd(exp);
385
1197268
  Trace("im") << "(fact " << iid << " " << (pol ? Node(atom) : atom.notNode())
386
598634
              << " " << expn << ")" << std::endl;
387
  // call the pre-notify fact method with preReg = false, isInternal = true
388
598634
  if (d_theory.preNotifyFact(atom, pol, expn, false, true))
389
  {
390
    // Handled in a theory-specific way that doesn't require equality engine,
391
    // notice we return true, indicating that the fact was processed.
392
    return true;
393
  }
394
598634
  Assert(d_ee != nullptr);
395
1197268
  Trace("infer-manager") << "TheoryInferenceManager::assertInternalFact: "
396
1197268
                         << (pol ? Node(atom) : atom.notNode()) << " from "
397
598634
                         << expn << " / " << iid << " " << id << std::endl;
398
598634
  if (Configuration::isAssertionBuild())
399
  {
400
    // check that all facts hold in the equality engine, to ensure that we
401
    // aren't processing a stale fact
402
1197268
    std::vector<Node> expc = exp;
403
1622303
    for (size_t i = 0; i < expc.size(); i++)
404
    {
405
1976711
      Node e = expc[i];
406
1023669
      bool epol = e.getKind() != NOT;
407
1976711
      Node eatom = epol ? e : e[0];
408
2047338
      Trace("infer-manager")
409
1023669
          << "...check " << eatom << " " << epol << std::endl;
410
1023669
      if (eatom.getKind() == AND)
411
      {
412
70627
        Assert(epol);
413
492951
        for (const Node& ea : eatom)
414
        {
415
422324
          expc.push_back(ea);
416
        }
417
70627
        continue;
418
      }
419
953042
      else if (eatom.getKind() == EQUAL)
420
      {
421
357031
        Assert(d_ee->hasTerm(eatom[0]));
422
357031
        Assert(d_ee->hasTerm(eatom[1]));
423
357031
        Assert(!epol || d_ee->areEqual(eatom[0], eatom[1]));
424
357031
        Assert(epol || d_ee->areDisequal(eatom[0], eatom[1], false));
425
      }
426
      else
427
      {
428
596011
        Assert(d_ee->hasTerm(eatom));
429
596011
        Assert(d_ee->areEqual(eatom, NodeManager::currentNM()->mkConst(epol)));
430
      }
431
    }
432
  }
433
598634
  d_numCurrentFacts++;
434
  // Now, assert the fact. How to do so depends on whether proofs are enabled.
435
598634
  bool ret = false;
436
598634
  if (d_pfee == nullptr)
437
  {
438
575419
    Trace("infer-manager") << "...assert without proofs..." << std::endl;
439
575419
    if (atom.getKind() == kind::EQUAL)
440
    {
441
482043
      ret = d_ee->assertEquality(atom, pol, expn);
442
    }
443
    else
444
    {
445
93376
      ret = d_ee->assertPredicate(atom, pol, expn);
446
    }
447
    // Must reference count the equality and its explanation, which is not done
448
    // by the equality engine. Notice that we do *not* need to do this for
449
    // external assertions, which enter as facts in theory check. This is also
450
    // not done if we are asserting to the proof equality engine, which does
451
    // this caching itself within ProofEqEngine::assertFact.
452
575419
    d_keep.insert(atom);
453
575419
    d_keep.insert(expn);
454
  }
455
  else
456
  {
457
23215
    Assert(id != PfRule::UNKNOWN);
458
23215
    Trace("infer-manager") << "...assert with proofs..." << std::endl;
459
    // Note that we reconstruct the original literal lit here, since both the
460
    // original literal is needed for bookkeeping proofs. It is possible to
461
    // optimize this so that a few less nodes are created, but at the cost
462
    // of a more verbose interface to proof equality engine.
463
46430
    Node lit = pol ? Node(atom) : atom.notNode();
464
23215
    if (pg != nullptr)
465
    {
466
      // use the proof generator interface
467
21625
      ret = d_pfee->assertFact(lit, expn, pg);
468
    }
469
    else
470
    {
471
      // use the explict proof step interface
472
1590
      ret = d_pfee->assertFact(lit, id, expn, args);
473
    }
474
  }
475
  // call the notify fact method with isInternal = true
476
598634
  d_theory.notifyFact(atom, pol, expn, true);
477
1197268
  Trace("infer-manager")
478
598634
      << "TheoryInferenceManager::finished assertInternalFact, ret=" << ret
479
598634
      << std::endl;
480
598634
  return ret;
481
}
482
483
48714
void TheoryInferenceManager::explain(TNode n, std::vector<TNode>& assumptions)
484
{
485
48714
  if (n.getKind() == kind::AND)
486
  {
487
    for (const Node& nc : n)
488
    {
489
      d_ee->explainLit(nc, assumptions);
490
    }
491
  }
492
  else
493
  {
494
48714
    d_ee->explainLit(n, assumptions);
495
  }
496
48714
}
497
498
Node TheoryInferenceManager::mkExplain(TNode n)
499
{
500
  std::vector<TNode> assumptions;
501
  explain(n, assumptions);
502
  return NodeManager::currentNM()->mkAnd(assumptions);
503
}
504
505
26212
Node TheoryInferenceManager::mkExplainPartial(
506
    const std::vector<Node>& exp, const std::vector<Node>& noExplain)
507
{
508
52424
  std::vector<TNode> assumps;
509
79827
  for (const Node& e : exp)
510
  {
511
58516
    if (std::find(noExplain.begin(), noExplain.end(), e) != noExplain.end())
512
    {
513
4901
      if (std::find(assumps.begin(), assumps.end(), e) == assumps.end())
514
      {
515
        // a non-explained literal
516
4901
        assumps.push_back(e);
517
      }
518
4901
      continue;
519
    }
520
    // otherwise, explain it
521
48714
    explain(e, assumps);
522
  }
523
52424
  return NodeManager::currentNM()->mkAnd(assumps);
524
}
525
526
uint32_t TheoryInferenceManager::numSentFacts() const
527
{
528
  return d_numCurrentFacts;
529
}
530
531
93315
bool TheoryInferenceManager::hasSentFact() const
532
{
533
93315
  return d_numCurrentFacts != 0;
534
}
535
536
2492641
bool TheoryInferenceManager::cacheLemma(TNode lem, LemmaProperty p)
537
{
538
4985282
  Node rewritten = rewrite(lem);
539
2492641
  if (d_lemmasSent.find(rewritten) != d_lemmasSent.end())
540
  {
541
2188257
    return false;
542
  }
543
304384
  d_lemmasSent.insert(rewritten);
544
304384
  return true;
545
}
546
547
20158
DecisionManager* TheoryInferenceManager::getDecisionManager()
548
{
549
20158
  return d_decManager;
550
}
551
552
36154
void TheoryInferenceManager::requirePhase(TNode n, bool pol)
553
{
554
36154
  return d_out.requirePhase(n, pol);
555
}
556
557
void TheoryInferenceManager::spendResource(Resource r)
558
{
559
  d_out.spendResource(r);
560
}
561
562
181789
void TheoryInferenceManager::safePoint(Resource r)
563
{
564
181789
  d_out.safePoint(r);
565
181789
}
566
567
5013
void TheoryInferenceManager::setIncomplete(IncompleteId id)
568
{
569
5013
  d_out.setIncomplete(id);
570
5013
}
571
572
1109690
void TheoryInferenceManager::notifyInConflict()
573
{
574
1109690
  d_theoryState.notifyInConflict();
575
1109690
}
576
577
}  // namespace theory
578
31137
}  // namespace cvc5