GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/expr/lazy_proof.cpp Lines: 94 97 96.9 %
Date: 2021-05-21 Branches: 169 352 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 "expr/lazy_proof.h"
17
18
#include "expr/proof_ensure_closed.h"
19
#include "expr/proof_node.h"
20
#include "expr/proof_node_manager.h"
21
22
using namespace cvc5::kind;
23
24
namespace cvc5 {
25
26
263805
LazyCDProof::LazyCDProof(ProofNodeManager* pnm,
27
                         ProofGenerator* dpg,
28
                         context::Context* c,
29
263805
                         std::string name)
30
263805
    : CDProof(pnm, c, name), d_gens(c ? c : &d_context), d_defaultGen(dpg)
31
{
32
263805
}
33
34
317446
LazyCDProof::~LazyCDProof() {}
35
36
1593036
std::shared_ptr<ProofNode> LazyCDProof::getProofFor(Node fact)
37
{
38
1593036
  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
1593036
  std::shared_ptr<ProofNode> opf = CDProof::getProofFor(fact);
42
1593036
  Assert(opf != nullptr);
43
1593036
  if (!hasGenerators())
44
  {
45
692812
    Trace("lazy-cdproof") << "...no generators, finished" << std::endl;
46
    // optimization: no generators, we are done
47
692812
    return opf;
48
  }
49
  // otherwise, we traverse the proof opf and fill in the ASSUME leafs that
50
  // have generators
51
1800448
  std::unordered_set<ProofNode*> visited;
52
900224
  std::unordered_set<ProofNode*>::iterator it;
53
1800448
  std::vector<ProofNode*> visit;
54
  ProofNode* cur;
55
900224
  visit.push_back(opf.get());
56
57421362
  do
57
  {
58
58321586
    cur = visit.back();
59
58321586
    visit.pop_back();
60
58321586
    it = visited.find(cur);
61
62
58321586
    if (it == visited.end())
63
    {
64
18298676
      visited.insert(cur);
65
36597352
      Node cfact = cur->getResult();
66
18298676
      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
485365
        Trace("lazy-cdproof") << "...skip unowned proof" << std::endl;
74
      }
75
17813311
      else if (cur->getRule() == PfRule::ASSUME)
76
      {
77
1057344
        bool isSym = false;
78
1057344
        ProofGenerator* pg = getGeneratorFor(cfact, isSym);
79
1057344
        if (pg != nullptr)
80
        {
81
1817208
          Trace("lazy-cdproof")
82
1817208
              << "LazyCDProof: Call generator " << pg->identify()
83
908604
              << " for assumption " << cfact << std::endl;
84
1817208
          Node cfactGen = isSym ? CDProof::getSymmFact(cfact) : cfact;
85
908604
          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
1817208
          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
908604
          if (pgc != nullptr)
97
          {
98
1817208
            Trace("lazy-cdproof-gen")
99
908604
                << "LazyCDProof: stored proof: " << *pgc.get() << std::endl;
100
101
908604
            if (isSym)
102
            {
103
8123
              d_manager->updateNode(cur, PfRule::SYMM, {pgc}, {});
104
            }
105
            else
106
            {
107
900481
              d_manager->updateNode(cur, pgc.get());
108
            }
109
1817208
            Trace("lazy-cdproof") << "LazyCDProof: Successfully added fact for "
110
908604
                                  << cfactGen << std::endl;
111
          }
112
        }
113
        else
114
        {
115
297480
          Trace("lazy-cdproof") << "LazyCDProof: " << identify()
116
148740
                                << " : 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
16755967
        const std::vector<std::shared_ptr<ProofNode>>& cc = cur->getChildren();
126
74177329
        for (const std::shared_ptr<ProofNode>& cp : cc)
127
        {
128
57421362
          visit.push_back(cp.get());
129
        }
130
      }
131
    }
132
58321586
  } while (!visit.empty());
133
  // we have now updated the ASSUME leafs of opf, return it
134
900224
  Trace("lazy-cdproof") << "...finished" << std::endl;
135
900224
  Assert(opf->getResult() == fact);
136
900224
  return opf;
137
}
138
139
1094714
void LazyCDProof::addLazyStep(Node expected,
140
                              ProofGenerator* pg,
141
                              PfRule idNull,
142
                              bool isClosed,
143
                              const char* ctx,
144
                              bool forceOverwrite)
145
{
146
1094714
  if (pg == nullptr)
147
  {
148
    // null generator, should have given a proof rule
149
25990
    if (idNull == PfRule::ASSUME)
150
    {
151
      Unreachable() << "LazyCDProof::addLazyStep: " << identify()
152
                    << ": failed to provide proof generator for " << expected;
153
      return;
154
    }
155
51980
    Trace("lazy-cdproof") << "LazyCDProof::addLazyStep: " << expected
156
25990
                          << " set (trusted) step " << idNull << "\n";
157
25990
    addStep(expected, idNull, {}, {expected});
158
25990
    return;
159
  }
160
2137448
  Trace("lazy-cdproof") << "LazyCDProof::addLazyStep: " << expected
161
1068724
                        << " set to generator " << pg->identify() << "\n";
162
1068724
  if (!forceOverwrite)
163
  {
164
1068724
    NodeProofGeneratorMap::const_iterator it = d_gens.find(expected);
165
1068724
    if (it != d_gens.end())
166
    {
167
      // don't overwrite something that is already there
168
445679
      return;
169
    }
170
  }
171
  // just store now
172
623045
  d_gens.insert(expected, pg);
173
  // debug checking
174
623045
  if (isClosed)
175
  {
176
152376
    Trace("lazy-cdproof-debug") << "Checking closed..." << std::endl;
177
152376
    pfgEnsureClosed(expected, pg, "lazy-cdproof-debug", ctx);
178
  }
179
}
180
181
1057344
ProofGenerator* LazyCDProof::getGeneratorFor(Node fact,
182
                                             bool& isSym)
183
{
184
1057344
  isSym = false;
185
1057344
  NodeProofGeneratorMap::const_iterator it = d_gens.find(fact);
186
1057344
  if (it != d_gens.end())
187
  {
188
145143
    return (*it).second;
189
  }
190
1824402
  Node factSym = CDProof::getSymmFact(fact);
191
  // could be symmetry
192
912201
  if (factSym.isNull())
193
  {
194
    // can't be symmetry, return the default generator
195
262176
    return d_defaultGen;
196
  }
197
650025
  it = d_gens.find(factSym);
198
650025
  if (it != d_gens.end())
199
  {
200
8123
    isSym = true;
201
8123
    return (*it).second;
202
  }
203
  // return the default generator
204
641902
  return d_defaultGen;
205
}
206
207
1593036
bool LazyCDProof::hasGenerators() const
208
{
209
1593036
  return !d_gens.empty() || d_defaultGen != nullptr;
210
}
211
212
388597
bool LazyCDProof::hasGenerator(Node fact) const
213
{
214
388597
  if (d_defaultGen != nullptr)
215
  {
216
    return true;
217
  }
218
388597
  NodeProofGeneratorMap::const_iterator it = d_gens.find(fact);
219
388597
  if (it != d_gens.end())
220
  {
221
1436
    return true;
222
  }
223
  // maybe there is a symmetric fact?
224
774322
  Node factSym = CDProof::getSymmFact(fact);
225
387161
  if (!factSym.isNull())
226
  {
227
11409
    it = d_gens.find(factSym);
228
  }
229
387161
  return it != d_gens.end();
230
}
231
232
27735
}  // namespace cvc5