GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/proof/lazy_proof.cpp Lines: 96 99 97.0 %
Date: 2021-09-09 Branches: 173 356 48.6 %

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
278551
LazyCDProof::LazyCDProof(ProofNodeManager* pnm,
27
                         ProofGenerator* dpg,
28
                         context::Context* c,
29
278551
                         const std::string& name)
30
278551
    : CDProof(pnm, c, name), d_gens(c ? c : &d_context), d_defaultGen(dpg)
31
{
32
278551
}
33
34
335091
LazyCDProof::~LazyCDProof() {}
35
36
1615697
std::shared_ptr<ProofNode> LazyCDProof::getProofFor(Node fact)
37
{
38
1615697
  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
1615697
  std::shared_ptr<ProofNode> opf = CDProof::getProofFor(fact);
42
1615697
  Assert(opf != nullptr);
43
1615697
  if (!hasGenerators())
44
  {
45
662867
    Trace("lazy-cdproof") << "...no generators, finished" << std::endl;
46
    // optimization: no generators, we are done
47
662867
    return opf;
48
  }
49
  // otherwise, we traverse the proof opf and fill in the ASSUME leafs that
50
  // have generators
51
1905660
  std::unordered_set<ProofNode*> visited;
52
952830
  std::unordered_set<ProofNode*>::iterator it;
53
1905660
  std::vector<ProofNode*> visit;
54
  ProofNode* cur;
55
952830
  visit.push_back(opf.get());
56
57650191
  do
57
  {
58
58603021
    cur = visit.back();
59
58603021
    visit.pop_back();
60
58603021
    it = visited.find(cur);
61
62
58603021
    if (it == visited.end())
63
    {
64
12527466
      visited.insert(cur);
65
25054932
      Node cfact = cur->getResult();
66
12527466
      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
892410
        Trace("lazy-cdproof") << "...skip unowned proof" << std::endl;
74
      }
75
11635056
      else if (cur->getRule() == PfRule::ASSUME)
76
      {
77
1018404
        bool isSym = false;
78
1018404
        ProofGenerator* pg = getGeneratorFor(cfact, isSym);
79
1018404
        if (pg != nullptr)
80
        {
81
1787180
          Trace("lazy-cdproof")
82
1787180
              << "LazyCDProof: Call generator " << pg->identify()
83
893590
              << " for assumption " << cfact << std::endl;
84
1787180
          Node cfactGen = isSym ? CDProof::getSymmFact(cfact) : cfact;
85
893590
          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
1787180
          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
893590
          if (pgc != nullptr)
97
          {
98
1787180
            Trace("lazy-cdproof-gen")
99
893590
                << "LazyCDProof: stored proof: " << *pgc.get() << std::endl;
100
101
893590
            if (isSym)
102
            {
103
4870
              if (pgc->getRule() == PfRule::SYMM)
104
              {
105
2
                d_manager->updateNode(cur, pgc->getChildren()[0].get());
106
              }
107
              else
108
              {
109
4868
                d_manager->updateNode(cur, PfRule::SYMM, {pgc}, {});
110
              }
111
            }
112
            else
113
            {
114
888720
              d_manager->updateNode(cur, pgc.get());
115
            }
116
1787180
            Trace("lazy-cdproof") << "LazyCDProof: Successfully added fact for "
117
893590
                                  << cfactGen << std::endl;
118
          }
119
        }
120
        else
121
        {
122
249628
          Trace("lazy-cdproof") << "LazyCDProof: " << identify()
123
124814
                                << " : No generator for " << cfact << std::endl;
124
        }
125
        // Notice that we do not traverse the proofs that have been generated
126
        // lazily by the proof generators here.  In other words, we assume that
127
        // the proofs from provided proof generators are final and need
128
        // no further modification by this class.
129
      }
130
      else
131
      {
132
10616652
        const std::vector<std::shared_ptr<ProofNode>>& cc = cur->getChildren();
133
68266843
        for (const std::shared_ptr<ProofNode>& cp : cc)
134
        {
135
57650191
          visit.push_back(cp.get());
136
        }
137
      }
138
    }
139
58603021
  } while (!visit.empty());
140
  // we have now updated the ASSUME leafs of opf, return it
141
952830
  Trace("lazy-cdproof") << "...finished" << std::endl;
142
952830
  Assert(opf->getResult() == fact);
143
952830
  return opf;
144
}
145
146
1632405
void LazyCDProof::addLazyStep(Node expected,
147
                              ProofGenerator* pg,
148
                              PfRule idNull,
149
                              bool isClosed,
150
                              const char* ctx,
151
                              bool forceOverwrite)
152
{
153
1632405
  if (pg == nullptr)
154
  {
155
    // null generator, should have given a proof rule
156
29421
    if (idNull == PfRule::ASSUME)
157
    {
158
      Unreachable() << "LazyCDProof::addLazyStep: " << identify()
159
                    << ": failed to provide proof generator for " << expected;
160
      return;
161
    }
162
58842
    Trace("lazy-cdproof") << "LazyCDProof::addLazyStep: " << expected
163
29421
                          << " set (trusted) step " << idNull << "\n";
164
29421
    addStep(expected, idNull, {}, {expected});
165
29421
    return;
166
  }
167
3205968
  Trace("lazy-cdproof") << "LazyCDProof::addLazyStep: " << expected
168
1602984
                        << " set to generator " << pg->identify() << "\n";
169
1602984
  if (!forceOverwrite)
170
  {
171
1602984
    NodeProofGeneratorMap::const_iterator it = d_gens.find(expected);
172
1602984
    if (it != d_gens.end())
173
    {
174
      // don't overwrite something that is already there
175
668505
      return;
176
    }
177
  }
178
  // just store now
179
934479
  d_gens.insert(expected, pg);
180
  // debug checking
181
934479
  if (isClosed)
182
  {
183
170807
    Trace("lazy-cdproof-debug") << "Checking closed..." << std::endl;
184
170807
    pfgEnsureClosed(expected, pg, "lazy-cdproof-debug", ctx);
185
  }
186
}
187
188
1018404
ProofGenerator* LazyCDProof::getGeneratorFor(Node fact, bool& isSym)
189
{
190
1018404
  isSym = false;
191
1018404
  NodeProofGeneratorMap::const_iterator it = d_gens.find(fact);
192
1018404
  if (it != d_gens.end())
193
  {
194
144687
    return (*it).second;
195
  }
196
1747434
  Node factSym = CDProof::getSymmFact(fact);
197
  // could be symmetry
198
873717
  if (factSym.isNull())
199
  {
200
    // can't be symmetry, return the default generator
201
257352
    return d_defaultGen;
202
  }
203
616365
  it = d_gens.find(factSym);
204
616365
  if (it != d_gens.end())
205
  {
206
4870
    isSym = true;
207
4870
    return (*it).second;
208
  }
209
  // return the default generator
210
611495
  return d_defaultGen;
211
}
212
213
1615697
bool LazyCDProof::hasGenerators() const
214
{
215
1615697
  return !d_gens.empty() || d_defaultGen != nullptr;
216
}
217
218
155418
bool LazyCDProof::hasGenerator(Node fact) const
219
{
220
155418
  if (d_defaultGen != nullptr)
221
  {
222
    return true;
223
  }
224
155418
  NodeProofGeneratorMap::const_iterator it = d_gens.find(fact);
225
155418
  if (it != d_gens.end())
226
  {
227
1535
    return true;
228
  }
229
  // maybe there is a symmetric fact?
230
307766
  Node factSym = CDProof::getSymmFact(fact);
231
153883
  if (!factSym.isNull())
232
  {
233
13413
    it = d_gens.find(factSym);
234
  }
235
153883
  return it != d_gens.end();
236
}
237
238
29505
}  // namespace cvc5