GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/proof/lazy_proof_chain.cpp Lines: 111 159 69.8 %
Date: 2021-09-29 Branches: 163 530 30.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Haniel Barbosa, Andrew Reynolds, Gereon Kremer
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_chain.h"
17
18
#include "options/proof_options.h"
19
#include "proof/proof.h"
20
#include "proof/proof_ensure_closed.h"
21
#include "proof/proof_node.h"
22
#include "proof/proof_node_algorithm.h"
23
#include "proof/proof_node_manager.h"
24
25
namespace cvc5 {
26
27
2605
LazyCDProofChain::LazyCDProofChain(ProofNodeManager* pnm,
28
                                   bool cyclic,
29
                                   context::Context* c,
30
                                   ProofGenerator* defGen,
31
                                   bool defRec,
32
2605
                                   const std::string& name)
33
    : d_manager(pnm),
34
      d_cyclic(cyclic),
35
      d_defRec(defRec),
36
      d_context(),
37
      d_gens(c ? c : &d_context),
38
      d_defGen(defGen),
39
2605
      d_name(name)
40
{
41
2605
}
42
43
2891
LazyCDProofChain::~LazyCDProofChain() {}
44
45
const std::map<Node, std::shared_ptr<ProofNode>> LazyCDProofChain::getLinks()
46
    const
47
{
48
  std::map<Node, std::shared_ptr<ProofNode>> links;
49
  for (const std::pair<const Node, ProofGenerator* const>& link : d_gens)
50
  {
51
    Assert(link.second);
52
    std::shared_ptr<ProofNode> pfn = link.second->getProofFor(link.first);
53
    Assert(pfn);
54
    links[link.first] = pfn;
55
  }
56
  return links;
57
}
58
59
2822
std::shared_ptr<ProofNode> LazyCDProofChain::getProofFor(Node fact)
60
{
61
5644
  Trace("lazy-cdproofchain")
62
2822
      << "LazyCDProofChain::getProofFor " << fact << "\n";
63
  // which facts have had proofs retrieved for. This is maintained to avoid
64
  // cycles. It also saves the proof node of the fact
65
5644
  std::unordered_map<Node, std::shared_ptr<ProofNode>> toConnect;
66
  // leaves of proof node links in the chain, i.e. assumptions, yet to be
67
  // expanded
68
  std::unordered_map<Node, std::vector<std::shared_ptr<ProofNode>>>
69
5644
      assumptionsToExpand;
70
  // invariant of the loop below, the first iteration notwithstanding:
71
  //   visit = domain(assumptionsToExpand) \ domain(toConnect)
72
5644
  std::vector<Node> visit{fact};
73
5644
  std::unordered_map<Node, bool> visited;
74
5644
  Node cur;
75
12563
  do
76
  {
77
15385
    cur = visit.back();
78
15385
    visit.pop_back();
79
15385
    auto itVisited = visited.find(cur);
80
    // pre-traversal
81
15385
    if (itVisited == visited.end())
82
    {
83
24424
      Trace("lazy-cdproofchain")
84
12212
          << "LazyCDProofChain::getProofFor: check " << cur << "\n";
85
12212
      Assert(toConnect.find(cur) == toConnect.end());
86
12212
      bool rec = true;
87
12212
      ProofGenerator* pg = getGeneratorForInternal(cur, rec);
88
12381
      if (!pg)
89
      {
90
338
        Trace("lazy-cdproofchain")
91
169
            << "LazyCDProofChain::getProofFor: nothing to do\n";
92
        // nothing to do for this fact, it'll be a leaf in the final proof
93
        // node, don't post-traverse.
94
169
        visited[cur] = true;
95
9233
        continue;
96
      }
97
24086
      Trace("lazy-cdproofchain")
98
24086
          << "LazyCDProofChain::getProofFor: Call generator " << pg->identify()
99
12043
          << " for chain link " << cur << "\n";
100
15191
      std::shared_ptr<ProofNode> curPfn = pg->getProofFor(cur);
101
12043
      if (curPfn == nullptr)
102
      {
103
        Trace("lazy-cdproofchain")
104
            << "LazyCDProofChain::getProofFor: No proof found, skip\n";
105
        visited[cur] = true;
106
        continue;
107
      }
108
      // map node whose proof node must be expanded to the respective poof node
109
12043
      toConnect[cur] = curPfn;
110
20938
      if (!rec)
111
      {
112
        // we don't want to recursively connect this proof
113
8895
        visited[cur] = true;
114
8895
        continue;
115
      }
116
6296
      Trace("lazy-cdproofchain-debug")
117
3148
          << "LazyCDProofChain::getProofFor: stored proof: " << *curPfn.get()
118
3148
          << "\n";
119
      // retrieve free assumptions and their respective proof nodes
120
6296
      std::map<Node, std::vector<std::shared_ptr<ProofNode>>> famap;
121
3148
      expr::getFreeAssumptionsMap(curPfn, famap);
122
3148
      if (Trace.isOn("lazy-cdproofchain"))
123
      {
124
        unsigned alreadyToVisit = 0;
125
        Trace("lazy-cdproofchain")
126
            << "LazyCDProofChain::getProofFor: " << famap.size()
127
            << " free assumptions:\n";
128
        for (auto fap : famap)
129
        {
130
          Trace("lazy-cdproofchain")
131
              << "LazyCDProofChain::getProofFor:  - " << fap.first << "\n";
132
          alreadyToVisit +=
133
              std::find(visit.begin(), visit.end(), fap.first) != visit.end()
134
                  ? 1
135
                  : 0;
136
        }
137
        Trace("lazy-cdproofchain")
138
            << "LazyCDProofChain::getProofFor: " << alreadyToVisit
139
            << " already to visit\n";
140
      }
141
      // mark for post-traversal if we are controlling cycles
142
3148
      if (d_cyclic)
143
      {
144
6296
        Trace("lazy-cdproofchain") << "LazyCDProofChain::getProofFor: marking "
145
3148
                                   << cur << " for cycle check\n";
146
3148
        visit.push_back(cur);
147
3148
        visited[cur] = false;
148
      }
149
      else
150
      {
151
        visited[cur] = true;
152
      }
153
      // enqueue free assumptions to process
154
9745
      for (const std::pair<const Node, std::vector<std::shared_ptr<ProofNode>>>&
155
3148
               fap : famap)
156
      {
157
        // check cycles
158
9745
        if (d_cyclic)
159
        {
160
          // cycles are assumptions being *currently* expanded and seen again,
161
          // i.e. in toConnect and not yet post-visited
162
9745
          auto itToConnect = toConnect.find(fap.first);
163
10075
          if (itToConnect != toConnect.end() && !visited[fap.first])
164
          {
165
            // Since we have a cycle with an assumption, this fact will be an
166
            // assumption in the final proof node produced by this
167
            // method. Thus we erase it as something to be connected, which
168
            // will keep it as an assumption.
169
660
            Trace("lazy-cdproofchain") << "LazyCDProofChain::getProofFor: "
170
330
                                          "removing cyclic assumption "
171
330
                                       << fap.first << " from expansion\n";
172
330
            continue;
173
          }
174
        }
175
        // We always add assumptions to visit so that their last seen occurrence
176
        // is expanded (rather than the first seen occurrence, if we were not
177
        // adding assumptions, say, in assumptionsToExpand). This is so because
178
        // in the case where we are checking cycles this is necessary (and
179
        // harmless when we are not). For example the cycle
180
        //
181
        //                 a2
182
        //                ...
183
        //               ----
184
        //                a1
185
        //               ...
186
        //             --------
187
        //   a0   a1    a2
188
        //       ...
189
        //  ---------------
190
        //       n
191
        //
192
        // in which a2 has a1 as an assumption, which has a2 an assumption,
193
        // would not be captured if we did not revisit a1, which is an
194
        // assumption of n and this already occur in assumptionsToExpand when
195
        // it shows up again as an assumption of a2.
196
9415
        visit.push_back(fap.first);
197
        // add assumption proof nodes to be updated
198
18830
        assumptionsToExpand[fap.first].insert(
199
18830
            assumptionsToExpand[fap.first].end(),
200
            fap.second.begin(),
201
28245
            fap.second.end());
202
      }
203
3148
      if (d_cyclic)
204
      {
205
3148
        Trace("lazy-cdproofchain") << push;
206
3148
        Trace("lazy-cdproofchain-debug") << push;
207
      }
208
    }
209
3173
    else if (!itVisited->second)
210
    {
211
3148
      visited[cur] = true;
212
3148
      Trace("lazy-cdproofchain") << pop;
213
3148
      Trace("lazy-cdproofchain-debug") << pop;
214
6296
      Trace("lazy-cdproofchain")
215
3148
          << "LazyCDProofChain::getProofFor: post-visited " << cur << "\n";
216
    }
217
    else
218
    {
219
50
      Trace("lazy-cdproofchain")
220
25
          << "LazyCDProofChain::getProofFor: already fully processed " << cur
221
25
          << "\n";
222
    }
223
15385
  } while (!visit.empty());
224
  // expand all assumptions marked to be connected
225
12043
  for (const std::pair<const Node, std::shared_ptr<ProofNode>>& npfn :
226
2822
       toConnect)
227
  {
228
12043
    auto it = assumptionsToExpand.find(npfn.first);
229
14841
    if (it == assumptionsToExpand.end())
230
    {
231
2798
      Assert(npfn.first == fact);
232
2798
      continue;
233
    }
234
9245
    Assert(npfn.second);
235
18490
    Trace("lazy-cdproofchain")
236
9245
        << "LazyCDProofChain::getProofFor: expand assumption " << npfn.first
237
9245
        << "\n";
238
18490
    Trace("lazy-cdproofchain-debug")
239
9245
        << "LazyCDProofChain::getProofFor: ...expand to " << *npfn.second.get()
240
9245
        << "\n";
241
    // update each assumption proof node
242
18539
    for (std::shared_ptr<ProofNode> pfn : it->second)
243
    {
244
9294
      d_manager->updateNode(pfn.get(), npfn.second.get());
245
    }
246
  }
247
  // final proof of fact
248
2822
  auto it = toConnect.find(fact);
249
2822
  if (it == toConnect.end())
250
  {
251
24
    return nullptr;
252
  }
253
2798
  return it->second;
254
}
255
256
3690
void LazyCDProofChain::addLazyStep(Node expected, ProofGenerator* pg)
257
{
258
3690
  Assert(pg != nullptr);
259
7380
  Trace("lazy-cdproofchain") << "LazyCDProofChain::addLazyStep: " << expected
260
3690
                             << " set to generator " << pg->identify() << "\n";
261
  // note this will replace the generator for expected, if any
262
3690
  d_gens.insert(expected, pg);
263
3690
}
264
265
void LazyCDProofChain::addLazyStep(Node expected,
266
                                   ProofGenerator* pg,
267
                                   const std::vector<Node>& assumptions,
268
                                   const char* ctx)
269
{
270
  Assert(pg != nullptr);
271
  Trace("lazy-cdproofchain") << "LazyCDProofChain::addLazyStep: " << expected
272
                             << " set to generator " << pg->identify() << "\n";
273
  // note this will rewrite the generator for expected, if any
274
  d_gens.insert(expected, pg);
275
  // check if chain is closed if eager checking is on
276
  if (options::proofCheck() == options::ProofCheckMode::EAGER)
277
  {
278
    Trace("lazy-cdproofchain")
279
        << "LazyCDProofChain::addLazyStep: Checking closed proof...\n";
280
    std::shared_ptr<ProofNode> pfn = pg->getProofFor(expected);
281
    std::vector<Node> allowedLeaves{assumptions.begin(), assumptions.end()};
282
    // add all current links in the chain
283
    for (const std::pair<const Node, ProofGenerator* const>& link : d_gens)
284
    {
285
      allowedLeaves.push_back(link.first);
286
    }
287
    if (Trace.isOn("lazy-cdproofchain"))
288
    {
289
      Trace("lazy-cdproofchain") << "Checking relative to leaves...\n";
290
      for (const Node& n : allowedLeaves)
291
      {
292
        Trace("lazy-cdproofchain") << "  - " << n << "\n";
293
      }
294
      Trace("lazy-cdproofchain") << "\n";
295
    }
296
    pfnEnsureClosedWrt(pfn.get(), allowedLeaves, "lazy-cdproofchain", ctx);
297
  }
298
}
299
300
204
bool LazyCDProofChain::hasGenerator(Node fact) const
301
{
302
204
  return d_gens.find(fact) != d_gens.end();
303
}
304
305
ProofGenerator* LazyCDProofChain::getGeneratorFor(Node fact)
306
{
307
  bool rec = true;
308
  return getGeneratorForInternal(fact, rec);
309
}
310
311
12212
ProofGenerator* LazyCDProofChain::getGeneratorForInternal(Node fact, bool& rec)
312
{
313
12212
  auto it = d_gens.find(fact);
314
12212
  if (it != d_gens.end())
315
  {
316
2557
    return (*it).second;
317
  }
318
  // otherwise, if no explicit generators, we use the default one
319
9655
  if (d_defGen != nullptr)
320
  {
321
9486
    rec = d_defRec;
322
9486
    return d_defGen;
323
  }
324
169
  return nullptr;
325
}
326
327
2393
std::string LazyCDProofChain::identify() const { return d_name; }
328
329
22746
}  // namespace cvc5