GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/proof/proof.cpp Lines: 170 212 80.2 %
Date: 2021-08-01 Branches: 391 922 42.4 %

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 proof.
14
 */
15
16
#include "proof/proof.h"
17
18
#include "proof/proof_checker.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
1649507
CDProof::CDProof(ProofNodeManager* pnm,
27
                 context::Context* c,
28
                 const std::string& name,
29
1649507
                 bool autoSymm)
30
    : d_manager(pnm),
31
      d_context(),
32
      d_nodes(c ? c : &d_context),
33
      d_name(name),
34
1649507
      d_autoSymm(autoSymm)
35
{
36
1649507
}
37
38
1650748
CDProof::~CDProof() {}
39
40
2774938
std::shared_ptr<ProofNode> CDProof::getProofFor(Node fact)
41
{
42
5549876
  std::shared_ptr<ProofNode> pf = getProofSymm(fact);
43
2774938
  if (pf != nullptr)
44
  {
45
1944419
    return pf;
46
  }
47
  // add as assumption
48
1661038
  std::vector<Node> pargs = {fact};
49
1661038
  std::vector<std::shared_ptr<ProofNode>> passume;
50
  std::shared_ptr<ProofNode> pfa =
51
1661038
      d_manager->mkNode(PfRule::ASSUME, passume, pargs, fact);
52
830519
  d_nodes.insert(fact, pfa);
53
830519
  return pfa;
54
}
55
56
43810797
std::shared_ptr<ProofNode> CDProof::getProof(Node fact) const
57
{
58
43810797
  NodeProofNodeMap::iterator it = d_nodes.find(fact);
59
43810797
  if (it != d_nodes.end())
60
  {
61
26270363
    return (*it).second;
62
  }
63
17540434
  return nullptr;
64
}
65
66
20725492
std::shared_ptr<ProofNode> CDProof::getProofSymm(Node fact)
67
{
68
20725492
  Trace("cdproof") << "CDProof::getProofSymm: " << fact << std::endl;
69
41450984
  std::shared_ptr<ProofNode> pf = getProof(fact);
70
20725492
  if (pf != nullptr && !isAssumption(pf.get()))
71
  {
72
8457691
    Trace("cdproof") << "...existing non-assume " << pf->getRule() << std::endl;
73
8457691
    return pf;
74
  }
75
12267801
  else if (!d_autoSymm)
76
  {
77
    Trace("cdproof") << "...not auto considering symmetry" << std::endl;
78
    return pf;
79
  }
80
24535602
  Node symFact = getSymmFact(fact);
81
12267801
  if (symFact.isNull())
82
  {
83
8437304
    Trace("cdproof") << "...no possible symm" << std::endl;
84
    // no symmetry possible, return original proof (possibly assumption)
85
8437304
    return pf;
86
  }
87
  // See if a proof exists for the opposite direction, if so, add the step.
88
  // Notice that SYMM is also disallowed.
89
7660994
  std::shared_ptr<ProofNode> pfs = getProof(symFact);
90
3830497
  if (pfs != nullptr)
91
  {
92
    // The symmetric fact exists, and the current one either does not, or is
93
    // an assumption. We make a new proof that applies SYMM to pfs.
94
663429
    std::vector<std::shared_ptr<ProofNode>> pschild;
95
502199
    pschild.push_back(pfs);
96
663429
    std::vector<Node> args;
97
502199
    if (pf == nullptr)
98
    {
99
340969
      Trace("cdproof") << "...fresh make symm" << std::endl;
100
      std::shared_ptr<ProofNode> psym =
101
681938
          d_manager->mkNode(PfRule::SYMM, pschild, args, fact);
102
340969
      Assert(psym != nullptr);
103
340969
      d_nodes.insert(fact, psym);
104
340969
      return psym;
105
    }
106
161230
    else if (!isAssumption(pfs.get()))
107
    {
108
      // if its not an assumption, make the connection
109
24
      Trace("cdproof") << "...update symm" << std::endl;
110
      // update pf
111
24
      bool sret = d_manager->updateNode(pf.get(), PfRule::SYMM, pschild, args);
112
24
      AlwaysAssert(sret);
113
    }
114
  }
115
  else
116
  {
117
6656596
    Trace("cdproof") << "...no symm, return "
118
3328298
                     << (pf == nullptr ? "null" : "non-null") << std::endl;
119
  }
120
  // return original proof (possibly assumption)
121
3489528
  return pf;
122
}
123
124
6518142
bool CDProof::addStep(Node expected,
125
                      PfRule id,
126
                      const std::vector<Node>& children,
127
                      const std::vector<Node>& args,
128
                      bool ensureChildren,
129
                      CDPOverwrite opolicy)
130
{
131
13036284
  Trace("cdproof") << "CDProof::addStep: " << identify() << " : " << id << " "
132
6518142
                   << expected << ", ensureChildren = " << ensureChildren
133
6518142
                   << ", overwrite policy = " << opolicy << std::endl;
134
13036284
  Trace("cdproof-debug") << "CDProof::addStep: " << identify()
135
6518142
                         << " : children: " << children << "\n";
136
13036284
  Trace("cdproof-debug") << "CDProof::addStep: " << identify()
137
6518142
                         << " : args: " << args << "\n";
138
  // We must always provide expected to this method
139
6518142
  Assert(!expected.isNull());
140
141
13036284
  std::shared_ptr<ProofNode> pprev = getProofSymm(expected);
142
6518142
  if (pprev != nullptr)
143
  {
144
2223652
    if (!shouldOverwrite(pprev.get(), id, opolicy))
145
    {
146
      // we should not overwrite the current step
147
2173412
      Trace("cdproof") << "...success, no overwrite" << std::endl;
148
2173412
      return true;
149
    }
150
100480
    Trace("cdproof") << "existing proof " << pprev->getRule()
151
50240
                     << ", overwrite..." << std::endl;
152
    // we will overwrite the existing proof node by updating its contents below
153
  }
154
  // collect the child proofs, for each premise
155
8689460
  std::vector<std::shared_ptr<ProofNode>> pchildren;
156
12893096
  for (const Node& c : children)
157
  {
158
8548366
    Trace("cdproof") << "- get child " << c << std::endl;
159
17096732
    std::shared_ptr<ProofNode> pc = getProofSymm(c);
160
8548366
    if (pc == nullptr)
161
    {
162
3365070
      if (ensureChildren)
163
      {
164
        // failed to get a proof for a child, fail
165
        Trace("cdproof") << "...fail, no child" << std::endl;
166
        return false;
167
      }
168
3365070
      Trace("cdproof") << "--- add assume" << std::endl;
169
      // otherwise, we initialize it as an assumption
170
6730140
      std::vector<Node> pcargs = {c};
171
6730140
      std::vector<std::shared_ptr<ProofNode>> pcassume;
172
3365070
      pc = d_manager->mkNode(PfRule::ASSUME, pcassume, pcargs, c);
173
      // assumptions never fail to check
174
3365070
      Assert(pc != nullptr);
175
3365070
      d_nodes.insert(c, pc);
176
    }
177
8548366
    pchildren.push_back(pc);
178
  }
179
180
  // the user may have provided SYMM of an assumption
181
4344730
  if (id == PfRule::SYMM)
182
  {
183
111
    Assert(pchildren.size() == 1);
184
111
    if (isAssumption(pchildren[0].get()))
185
    {
186
      // the step we are constructing is a (symmetric fact of an) assumption, so
187
      // there is no use adding it to the proof.
188
111
      return true;
189
    }
190
  }
191
192
4344619
  bool ret = true;
193
  // create or update it
194
8689238
  std::shared_ptr<ProofNode> pthis;
195
4344619
  if (pprev == nullptr)
196
  {
197
4294379
    Trace("cdproof") << "  new node " << expected << "..." << std::endl;
198
4294379
    pthis = d_manager->mkNode(id, pchildren, args, expected);
199
4294379
    if (pthis == nullptr)
200
    {
201
      // failed to construct the node, perhaps due to a proof checking failure
202
      Trace("cdproof") << "...fail, proof checking" << std::endl;
203
      return false;
204
    }
205
4294379
    d_nodes.insert(expected, pthis);
206
  }
207
  else
208
  {
209
50240
    Trace("cdproof") << "  update node " << expected << "..." << std::endl;
210
    // update its value
211
50240
    pthis = pprev;
212
    // We return the value of updateNode here. This means this method may return
213
    // false if this call failed, regardless of whether we already have a proof
214
    // step for expected.
215
50240
    ret = d_manager->updateNode(pthis.get(), id, pchildren, args);
216
  }
217
4344619
  if (ret)
218
  {
219
    // the result of the proof node should be expected
220
4344619
    Assert(pthis->getResult() == expected);
221
222
    // notify new proof
223
4344619
    notifyNewProof(expected);
224
  }
225
226
4344619
  Trace("cdproof") << "...return " << ret << std::endl;
227
4344619
  return ret;
228
}
229
230
7227261
void CDProof::notifyNewProof(Node expected)
231
{
232
7227261
  if (!d_autoSymm)
233
  {
234
    return;
235
  }
236
  // ensure SYMM proof is also linked to an existing proof, if it is an
237
  // assumption.
238
14454522
  Node symExpected = getSymmFact(expected);
239
7227261
  if (!symExpected.isNull())
240
  {
241
2207088
    Trace("cdproof") << "  check connect symmetry " << symExpected << std::endl;
242
    // if it exists, we may need to update it
243
4414176
    std::shared_ptr<ProofNode> pfs = getProof(symExpected);
244
2207088
    if (pfs != nullptr)
245
    {
246
1404
      Trace("cdproof") << "  connect via getProofSymm method..." << std::endl;
247
      // call the get function with symmetry, which will do the update
248
1404
      std::shared_ptr<ProofNode> pfss = getProofSymm(symExpected);
249
    }
250
    else
251
    {
252
2205684
      Trace("cdproof") << "  no connect" << std::endl;
253
    }
254
  }
255
}
256
257
3276777
bool CDProof::addStep(Node expected,
258
                      const ProofStep& step,
259
                      bool ensureChildren,
260
                      CDPOverwrite opolicy)
261
{
262
9830331
  return addStep(expected,
263
3276777
                 step.d_rule,
264
                 step.d_children,
265
                 step.d_args,
266
                 ensureChildren,
267
6553554
                 opolicy);
268
}
269
270
18168
bool CDProof::addSteps(const ProofStepBuffer& psb,
271
                       bool ensureChildren,
272
                       CDPOverwrite opolicy)
273
{
274
18168
  const std::vector<std::pair<Node, ProofStep>>& steps = psb.getSteps();
275
165680
  for (const std::pair<Node, ProofStep>& ps : steps)
276
  {
277
147512
    if (!addStep(ps.first, ps.second, ensureChildren, opolicy))
278
    {
279
      return false;
280
    }
281
  }
282
18168
  return true;
283
}
284
285
2882642
bool CDProof::addProof(std::shared_ptr<ProofNode> pn,
286
                       CDPOverwrite opolicy,
287
                       bool doCopy)
288
{
289
2882642
  if (!doCopy)
290
  {
291
    // If we aren't doing a deep copy, we either store pn or link its top
292
    // node into the existing pointer
293
5765284
    Node curFact = pn->getResult();
294
5765284
    std::shared_ptr<ProofNode> cur = getProofSymm(curFact);
295
2882642
    if (cur == nullptr)
296
    {
297
      // Assert that the checker of this class agrees with (the externally
298
      // provided) pn. This ensures that if pn was checked by a different
299
      // checker than the one of the manager in this class, then it is double
300
      // checked here, so that this class maintains the invariant that all of
301
      // its nodes in d_nodes have been checked by the underlying checker.
302
2286431
      Assert(d_manager->getChecker() == nullptr
303
             || d_manager->getChecker()->check(pn.get(), curFact) == curFact);
304
      // just store the proof for fact
305
2286431
      d_nodes.insert(curFact, pn);
306
    }
307
596211
    else if (shouldOverwrite(cur.get(), pn->getRule(), opolicy))
308
    {
309
      // We update cur to have the structure of the top node of pn. Notice that
310
      // the interface to update this node will ensure that the proof apf is a
311
      // proof of the assumption. If it does not, then pn was wrong.
312
      if (!d_manager->updateNode(
313
              cur.get(), pn->getRule(), pn->getChildren(), pn->getArguments()))
314
      {
315
        return false;
316
      }
317
    }
318
    // also need to connect via SYMM if necessary
319
2882642
    notifyNewProof(curFact);
320
2882642
    return true;
321
  }
322
  std::unordered_map<ProofNode*, bool> visited;
323
  std::unordered_map<ProofNode*, bool>::iterator it;
324
  std::vector<ProofNode*> visit;
325
  ProofNode* cur;
326
  Node curFact;
327
  visit.push_back(pn.get());
328
  bool retValue = true;
329
  do
330
  {
331
    cur = visit.back();
332
    curFact = cur->getResult();
333
    visit.pop_back();
334
    it = visited.find(cur);
335
    if (it == visited.end())
336
    {
337
      // visit the children
338
      visited[cur] = false;
339
      visit.push_back(cur);
340
      const std::vector<std::shared_ptr<ProofNode>>& cs = cur->getChildren();
341
      for (const std::shared_ptr<ProofNode>& c : cs)
342
      {
343
        visit.push_back(c.get());
344
      }
345
    }
346
    else if (!it->second)
347
    {
348
      // we always call addStep, which may or may not overwrite the
349
      // current step
350
      std::vector<Node> pexp;
351
      const std::vector<std::shared_ptr<ProofNode>>& cs = cur->getChildren();
352
      for (const std::shared_ptr<ProofNode>& c : cs)
353
      {
354
        Assert(!c->getResult().isNull());
355
        pexp.push_back(c->getResult());
356
      }
357
      // can ensure children at this point
358
      bool res = addStep(
359
          curFact, cur->getRule(), pexp, cur->getArguments(), true, opolicy);
360
      // should always succeed
361
      Assert(res);
362
      retValue = retValue && res;
363
      visited[cur] = true;
364
    }
365
  } while (!visit.empty());
366
367
  return retValue;
368
}
369
370
1320858
bool CDProof::hasStep(Node fact)
371
{
372
2641716
  std::shared_ptr<ProofNode> pf = getProof(fact);
373
1320858
  if (pf != nullptr && !isAssumption(pf.get()))
374
  {
375
930445
    return true;
376
  }
377
390413
  else if (!d_autoSymm)
378
  {
379
    return false;
380
  }
381
780826
  Node symFact = getSymmFact(fact);
382
390413
  if (symFact.isNull())
383
  {
384
374551
    return false;
385
  }
386
15862
  pf = getProof(symFact);
387
15862
  if (pf != nullptr && !isAssumption(pf.get()))
388
  {
389
    return true;
390
  }
391
15862
  return false;
392
}
393
394
99086
ProofNodeManager* CDProof::getManager() const { return d_manager; }
395
396
2819863
bool CDProof::shouldOverwrite(ProofNode* pn, PfRule newId, CDPOverwrite opol)
397
{
398
2819863
  Assert(pn != nullptr);
399
  // we overwrite only if opol is CDPOverwrite::ALWAYS, or if
400
  // opol is CDPOverwrite::ASSUME_ONLY and the previously
401
  // provided proof pn was an assumption and the currently provided step is not
402
  return opol == CDPOverwrite::ALWAYS
403
2870103
         || (opol == CDPOverwrite::ASSUME_ONLY && isAssumption(pn)
404
3278775
             && newId != PfRule::ASSUME);
405
}
406
407
13519662
bool CDProof::isAssumption(ProofNode* pn)
408
{
409
13519662
  PfRule rule = pn->getRule();
410
13519662
  if (rule == PfRule::ASSUME)
411
  {
412
1551600
    return true;
413
  }
414
11968062
  else if (rule == PfRule::SYMM)
415
  {
416
332708
    const std::vector<std::shared_ptr<ProofNode>>& pc = pn->getChildren();
417
332708
    Assert(pc.size() == 1);
418
332708
    return pc[0]->getRule() == PfRule::ASSUME;
419
  }
420
11635354
  return false;
421
}
422
423
943236
bool CDProof::isSame(TNode f, TNode g)
424
{
425
943236
  if (f == g)
426
  {
427
510079
    return true;
428
  }
429
433157
  Kind fk = f.getKind();
430
433157
  Kind gk = g.getKind();
431
433157
  if (fk == EQUAL && gk == EQUAL && f[0] == g[1] && f[1] == g[0])
432
  {
433
    // symmetric equality
434
169460
    return true;
435
  }
436
313867
  if (fk == NOT && gk == NOT && f[0].getKind() == EQUAL
437
284118
      && g[0].getKind() == EQUAL && f[0][0] == g[0][1] && f[0][1] == g[0][0])
438
  {
439
    // symmetric disequality
440
582
    return true;
441
  }
442
263115
  return false;
443
}
444
445
21671785
Node CDProof::getSymmFact(TNode f)
446
{
447
21671785
  bool polarity = f.getKind() != NOT;
448
43343570
  TNode fatom = polarity ? f : f[0];
449
21671785
  if (fatom.getKind() != EQUAL || fatom[0] == fatom[1])
450
  {
451
14580665
    return Node::null();
452
  }
453
14182240
  Node symFact = fatom[1].eqNode(fatom[0]);
454
7091120
  return polarity ? symFact : symFact.notNode();
455
}
456
457
21276958
std::string CDProof::identify() const { return d_name; }
458
459
29280
}  // namespace cvc5