GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/proof/lazy_proof.cpp Lines: 94 97 96.9 %
Date: 2021-08-06 Branches: 168 350 48.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Aina Niemetz
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
 * Implementation of lazy proof utility.
14
 */
15
16
#include "proof/lazy_proof.h"
17
18
#include "proof/proof_ensure_closed.h"
19
#include "proof/proof_node.h"
20
#include "proof/proof_node_manager.h"
21
22
using namespace cvc5::kind;
23
24
namespace cvc5 {
25
26
268401
LazyCDProof::LazyCDProof(ProofNodeManager* pnm,
27
                         ProofGenerator* dpg,
28
                         context::Context* c,
29
268401
                         const std::string& name)
30
268401
    : CDProof(pnm, c, name), d_gens(c ? c : &d_context), d_defaultGen(dpg)
31
{
32
268401
}
33
34
322973
LazyCDProof::~LazyCDProof() {}
35
36
1589509
std::shared_ptr<ProofNode> LazyCDProof::getProofFor(Node fact)
37
{
38
1589509
  Trace("lazy-cdproof") << "LazyCDProof::mkLazyProof " << fact << std::endl;
39
  // make the proof, which should always be non-null, since we construct an
40
  // assumption in the worst case.
41
1589509
  std::shared_ptr<ProofNode> opf = CDProof::getProofFor(fact);
42
1589509
  Assert(opf != nullptr);
43
1589509
  if (!hasGenerators())
44
  {
45
667585
    Trace("lazy-cdproof") << "...no generators, finished" << std::endl;
46
    // optimization: no generators, we are done
47
667585
    return opf;
48
  }
49
  // otherwise, we traverse the proof opf and fill in the ASSUME leafs that
50
  // have generators
51
1843848
  std::unordered_set<ProofNode*> visited;
52
921924
  std::unordered_set<ProofNode*>::iterator it;
53
1843848
  std::vector<ProofNode*> visit;
54
  ProofNode* cur;
55
921924
  visit.push_back(opf.get());
56
64412742
  do
57
  {
58
65334666
    cur = visit.back();
59
65334666
    visit.pop_back();
60
65334666
    it = visited.find(cur);
61
62
65334666
    if (it == visited.end())
63
    {
64
16499002
      visited.insert(cur);
65
32998004
      Node cfact = cur->getResult();
66
16499002
      if (getProof(cfact).get() != cur)
67
      {
68
        // We don't own this proof, skip it. This is to ensure that this method
69
        // is idempotent, since it may be the case that a previous call to
70
        // getProofFor connected a proof from a proof generator as a child of
71
        // a ProofNode in the range of the map in CDProof. Thus, this ensures
72
        // we don't touch such proofs.
73
883477
        Trace("lazy-cdproof") << "...skip unowned proof" << std::endl;
74
      }
75
15615525
      else if (cur->getRule() == PfRule::ASSUME)
76
      {
77
976364
        bool isSym = false;
78
976364
        ProofGenerator* pg = getGeneratorFor(cfact, isSym);
79
976364
        if (pg != nullptr)
80
        {
81
1742806
          Trace("lazy-cdproof")
82
1742806
              << "LazyCDProof: Call generator " << pg->identify()
83
871403
              << " for assumption " << cfact << std::endl;
84
1742806
          Node cfactGen = isSym ? CDProof::getSymmFact(cfact) : cfact;
85
871403
          Assert(!cfactGen.isNull());
86
          // Do not use the addProofTo interface, instead use the update node
87
          // interface, since this ensures that we don't take ownership for
88
          // the current proof. Instead, it is only linked, and ignored on
89
          // future calls to getProofFor due to the check above.
90
1742806
          std::shared_ptr<ProofNode> pgc = pg->getProofFor(cfactGen);
91
          // If the proof was null, then the update is not performed. This is
92
          // not considered an error, since this behavior is equivalent to
93
          // if pg had provided the proof (ASSUME cfactGen). Ensuring the
94
          // proper behavior wrt closed proofs should be done outside this
95
          // method.
96
871403
          if (pgc != nullptr)
97
          {
98
1742806
            Trace("lazy-cdproof-gen")
99
871403
                << "LazyCDProof: stored proof: " << *pgc.get() << std::endl;
100
101
871403
            if (isSym)
102
            {
103
4421
              d_manager->updateNode(cur, PfRule::SYMM, {pgc}, {});
104
            }
105
            else
106
            {
107
866982
              d_manager->updateNode(cur, pgc.get());
108
            }
109
1742806
            Trace("lazy-cdproof") << "LazyCDProof: Successfully added fact for "
110
871403
                                  << cfactGen << std::endl;
111
          }
112
        }
113
        else
114
        {
115
209922
          Trace("lazy-cdproof") << "LazyCDProof: " << identify()
116
104961
                                << " : No generator for " << cfact << std::endl;
117
        }
118
        // Notice that we do not traverse the proofs that have been generated
119
        // lazily by the proof generators here.  In other words, we assume that
120
        // the proofs from provided proof generators are final and need
121
        // no further modification by this class.
122
      }
123
      else
124
      {
125
14639161
        const std::vector<std::shared_ptr<ProofNode>>& cc = cur->getChildren();
126
79051903
        for (const std::shared_ptr<ProofNode>& cp : cc)
127
        {
128
64412742
          visit.push_back(cp.get());
129
        }
130
      }
131
    }
132
65334666
  } while (!visit.empty());
133
  // we have now updated the ASSUME leafs of opf, return it
134
921924
  Trace("lazy-cdproof") << "...finished" << std::endl;
135
921924
  Assert(opf->getResult() == fact);
136
921924
  return opf;
137
}
138
139
1600592
void LazyCDProof::addLazyStep(Node expected,
140
                              ProofGenerator* pg,
141
                              PfRule idNull,
142
                              bool isClosed,
143
                              const char* ctx,
144
                              bool forceOverwrite)
145
{
146
1600592
  if (pg == nullptr)
147
  {
148
    // null generator, should have given a proof rule
149
29839
    if (idNull == PfRule::ASSUME)
150
    {
151
      Unreachable() << "LazyCDProof::addLazyStep: " << identify()
152
                    << ": failed to provide proof generator for " << expected;
153
      return;
154
    }
155
59678
    Trace("lazy-cdproof") << "LazyCDProof::addLazyStep: " << expected
156
29839
                          << " set (trusted) step " << idNull << "\n";
157
29839
    addStep(expected, idNull, {}, {expected});
158
29839
    return;
159
  }
160
3141506
  Trace("lazy-cdproof") << "LazyCDProof::addLazyStep: " << expected
161
1570753
                        << " set to generator " << pg->identify() << "\n";
162
1570753
  if (!forceOverwrite)
163
  {
164
1570753
    NodeProofGeneratorMap::const_iterator it = d_gens.find(expected);
165
1570753
    if (it != d_gens.end())
166
    {
167
      // don't overwrite something that is already there
168
664465
      return;
169
    }
170
  }
171
  // just store now
172
906288
  d_gens.insert(expected, pg);
173
  // debug checking
174
906288
  if (isClosed)
175
  {
176
165466
    Trace("lazy-cdproof-debug") << "Checking closed..." << std::endl;
177
165466
    pfgEnsureClosed(expected, pg, "lazy-cdproof-debug", ctx);
178
  }
179
}
180
181
976364
ProofGenerator* LazyCDProof::getGeneratorFor(Node fact, bool& isSym)
182
{
183
976364
  isSym = false;
184
976364
  NodeProofGeneratorMap::const_iterator it = d_gens.find(fact);
185
976364
  if (it != d_gens.end())
186
  {
187
141295
    return (*it).second;
188
  }
189
1670138
  Node factSym = CDProof::getSymmFact(fact);
190
  // could be symmetry
191
835069
  if (factSym.isNull())
192
  {
193
    // can't be symmetry, return the default generator
194
267740
    return d_defaultGen;
195
  }
196
567329
  it = d_gens.find(factSym);
197
567329
  if (it != d_gens.end())
198
  {
199
4421
    isSym = true;
200
4421
    return (*it).second;
201
  }
202
  // return the default generator
203
562908
  return d_defaultGen;
204
}
205
206
1589509
bool LazyCDProof::hasGenerators() const
207
{
208
1589509
  return !d_gens.empty() || d_defaultGen != nullptr;
209
}
210
211
155462
bool LazyCDProof::hasGenerator(Node fact) const
212
{
213
155462
  if (d_defaultGen != nullptr)
214
  {
215
    return true;
216
  }
217
155462
  NodeProofGeneratorMap::const_iterator it = d_gens.find(fact);
218
155462
  if (it != d_gens.end())
219
  {
220
1530
    return true;
221
  }
222
  // maybe there is a symmetric fact?
223
307864
  Node factSym = CDProof::getSymmFact(fact);
224
153932
  if (!factSym.isNull())
225
  {
226
13392
    it = d_gens.find(factSym);
227
  }
228
153932
  return it != d_gens.end();
229
}
230
231
29322
}  // namespace cvc5