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

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
1459268
CDProof::CDProof(ProofNodeManager* pnm,
27
                 context::Context* c,
28
                 const std::string& name,
29
1459268
                 bool autoSymm)
30
    : d_manager(pnm),
31
      d_context(),
32
      d_nodes(c ? c : &d_context),
33
      d_name(name),
34
1459268
      d_autoSymm(autoSymm)
35
{
36
1459268
}
37
38
1460513
CDProof::~CDProof() {}
39
40
2541614
std::shared_ptr<ProofNode> CDProof::getProofFor(Node fact)
41
{
42
5083228
  std::shared_ptr<ProofNode> pf = getProofSymm(fact);
43
2541614
  if (pf != nullptr)
44
  {
45
1700729
    return pf;
46
  }
47
  // add as assumption
48
1681770
  std::vector<Node> pargs = {fact};
49
1681770
  std::vector<std::shared_ptr<ProofNode>> passume;
50
  std::shared_ptr<ProofNode> pfa =
51
1681770
      d_manager->mkNode(PfRule::ASSUME, passume, pargs, fact);
52
840885
  d_nodes.insert(fact, pfa);
53
840885
  return pfa;
54
}
55
56
45385246
std::shared_ptr<ProofNode> CDProof::getProof(Node fact) const
57
{
58
45385246
  NodeProofNodeMap::iterator it = d_nodes.find(fact);
59
45385246
  if (it != d_nodes.end())
60
  {
61
27171004
    return (*it).second;
62
  }
63
18214242
  return nullptr;
64
}
65
66
21043424
std::shared_ptr<ProofNode> CDProof::getProofSymm(Node fact)
67
{
68
21043424
  Trace("cdproof") << "CDProof::getProofSymm: " << fact << std::endl;
69
42086848
  std::shared_ptr<ProofNode> pf = getProof(fact);
70
21043424
  if (pf != nullptr && !isAssumption(pf.get()))
71
  {
72
9218945
    Trace("cdproof") << "...existing non-assume " << pf->getRule() << std::endl;
73
9218945
    return pf;
74
  }
75
11824479
  else if (!d_autoSymm)
76
  {
77
    Trace("cdproof") << "...not auto considering symmetry" << std::endl;
78
    return pf;
79
  }
80
23648958
  Node symFact = getSymmFact(fact);
81
11824479
  if (symFact.isNull())
82
  {
83
7552289
    Trace("cdproof") << "...no possible symm" << std::endl;
84
    // no symmetry possible, return original proof (possibly assumption)
85
7552289
    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
8544380
  std::shared_ptr<ProofNode> pfs = getProof(symFact);
90
4272190
  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
669803
    std::vector<std::shared_ptr<ProofNode>> pschild;
95
507891
    pschild.push_back(pfs);
96
669803
    std::vector<Node> args;
97
507891
    if (pf == nullptr)
98
    {
99
345979
      Trace("cdproof") << "...fresh make symm" << std::endl;
100
      std::shared_ptr<ProofNode> psym =
101
691958
          d_manager->mkNode(PfRule::SYMM, pschild, args, fact);
102
345979
      Assert(psym != nullptr);
103
345979
      d_nodes.insert(fact, psym);
104
345979
      return psym;
105
    }
106
161912
    else if (!isAssumption(pfs.get()))
107
    {
108
      // if its not an assumption, make the connection
109
22
      Trace("cdproof") << "...update symm" << std::endl;
110
      // update pf
111
22
      bool sret = d_manager->updateNode(pf.get(), PfRule::SYMM, pschild, args);
112
22
      AlwaysAssert(sret);
113
    }
114
  }
115
  else
116
  {
117
7528598
    Trace("cdproof") << "...no symm, return "
118
3764299
                     << (pf == nullptr ? "null" : "non-null") << std::endl;
119
  }
120
  // return original proof (possibly assumption)
121
3926211
  return pf;
122
}
123
124
6567233
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
13134466
  Trace("cdproof") << "CDProof::addStep: " << identify() << " : " << id << " "
132
6567233
                   << expected << ", ensureChildren = " << ensureChildren
133
6567233
                   << ", overwrite policy = " << opolicy << std::endl;
134
13134466
  Trace("cdproof-debug") << "CDProof::addStep: " << identify()
135
6567233
                         << " : children: " << children << "\n";
136
13134466
  Trace("cdproof-debug") << "CDProof::addStep: " << identify()
137
6567233
                         << " : args: " << args << "\n";
138
  // We must always provide expected to this method
139
6567233
  Assert(!expected.isNull());
140
141
13134466
  std::shared_ptr<ProofNode> pprev = getProofSymm(expected);
142
6567233
  if (pprev != nullptr)
143
  {
144
2153903
    if (!shouldOverwrite(pprev.get(), id, opolicy))
145
    {
146
      // we should not overwrite the current step
147
2103135
      Trace("cdproof") << "...success, no overwrite" << std::endl;
148
2103135
      return true;
149
    }
150
101536
    Trace("cdproof") << "existing proof " << pprev->getRule()
151
50768
                     << ", 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
8928196
  std::vector<std::shared_ptr<ProofNode>> pchildren;
156
12860705
  for (const Node& c : children)
157
  {
158
8396607
    Trace("cdproof") << "- get child " << c << std::endl;
159
16793214
    std::shared_ptr<ProofNode> pc = getProofSymm(c);
160
8396607
    if (pc == nullptr)
161
    {
162
2245244
      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
2245244
      Trace("cdproof") << "--- add assume" << std::endl;
169
      // otherwise, we initialize it as an assumption
170
4490488
      std::vector<Node> pcargs = {c};
171
4490488
      std::vector<std::shared_ptr<ProofNode>> pcassume;
172
2245244
      pc = d_manager->mkNode(PfRule::ASSUME, pcassume, pcargs, c);
173
      // assumptions never fail to check
174
2245244
      Assert(pc != nullptr);
175
2245244
      d_nodes.insert(c, pc);
176
    }
177
8396607
    pchildren.push_back(pc);
178
  }
179
180
  // the user may have provided SYMM of an assumption
181
4464098
  if (id == PfRule::SYMM)
182
  {
183
119
    Assert(pchildren.size() == 1);
184
119
    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
119
      return true;
189
    }
190
  }
191
192
4463979
  bool ret = true;
193
  // create or update it
194
8927958
  std::shared_ptr<ProofNode> pthis;
195
4463979
  if (pprev == nullptr)
196
  {
197
4413211
    Trace("cdproof") << "  new node " << expected << "..." << std::endl;
198
4413211
    pthis = d_manager->mkNode(id, pchildren, args, expected);
199
4413211
    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
4413211
    d_nodes.insert(expected, pthis);
206
  }
207
  else
208
  {
209
50768
    Trace("cdproof") << "  update node " << expected << "..." << std::endl;
210
    // update its value
211
50768
    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
50768
    ret = d_manager->updateNode(pthis.get(), id, pchildren, args);
216
  }
217
4463979
  if (ret)
218
  {
219
    // the result of the proof node should be expected
220
4463979
    Assert(pthis->getResult() == expected);
221
222
    // notify new proof
223
4463979
    notifyNewProof(expected);
224
  }
225
226
4463979
  Trace("cdproof") << "...return " << ret << std::endl;
227
4463979
  return ret;
228
}
229
230
8000567
void CDProof::notifyNewProof(Node expected)
231
{
232
8000567
  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
16001134
  Node symExpected = getSymmFact(expected);
239
8000567
  if (!symExpected.isNull())
240
  {
241
2695469
    Trace("cdproof") << "  check connect symmetry " << symExpected << std::endl;
242
    // if it exists, we may need to update it
243
5390938
    std::shared_ptr<ProofNode> pfs = getProof(symExpected);
244
2695469
    if (pfs != nullptr)
245
    {
246
1382
      Trace("cdproof") << "  connect via getProofSymm method..." << std::endl;
247
      // call the get function with symmetry, which will do the update
248
1382
      std::shared_ptr<ProofNode> pfss = getProofSymm(symExpected);
249
    }
250
    else
251
    {
252
2694087
      Trace("cdproof") << "  no connect" << std::endl;
253
    }
254
  }
255
}
256
257
3159239
bool CDProof::addStep(Node expected,
258
                      const ProofStep& step,
259
                      bool ensureChildren,
260
                      CDPOverwrite opolicy)
261
{
262
9477717
  return addStep(expected,
263
3159239
                 step.d_rule,
264
                 step.d_children,
265
                 step.d_args,
266
                 ensureChildren,
267
6318478
                 opolicy);
268
}
269
270
19302
bool CDProof::addSteps(const ProofStepBuffer& psb,
271
                       bool ensureChildren,
272
                       CDPOverwrite opolicy)
273
{
274
19302
  const std::vector<std::pair<Node, ProofStep>>& steps = psb.getSteps();
275
169610
  for (const std::pair<Node, ProofStep>& ps : steps)
276
  {
277
150308
    if (!addStep(ps.first, ps.second, ensureChildren, opolicy))
278
    {
279
      return false;
280
    }
281
  }
282
19302
  return true;
283
}
284
285
3536588
bool CDProof::addProof(std::shared_ptr<ProofNode> pn,
286
                       CDPOverwrite opolicy,
287
                       bool doCopy)
288
{
289
3536588
  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
7073176
    Node curFact = pn->getResult();
294
7073176
    std::shared_ptr<ProofNode> cur = getProofSymm(curFact);
295
3536588
    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
2855601
      Assert(d_manager->getChecker() == nullptr
303
             || d_manager->getChecker()->check(pn.get(), curFact) == curFact);
304
      // just store the proof for fact
305
2855601
      d_nodes.insert(curFact, pn);
306
    }
307
680987
    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
3536588
    notifyNewProof(curFact);
320
3536588
    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
858244
bool CDProof::hasStep(Node fact)
371
{
372
1716488
  std::shared_ptr<ProofNode> pf = getProof(fact);
373
858244
  if (pf != nullptr && !isAssumption(pf.get()))
374
  {
375
698140
    return true;
376
  }
377
160104
  else if (!d_autoSymm)
378
  {
379
    return false;
380
  }
381
320208
  Node symFact = getSymmFact(fact);
382
160104
  if (symFact.isNull())
383
  {
384
143187
    return false;
385
  }
386
16917
  pf = getProof(symFact);
387
16917
  if (pf != nullptr && !isAssumption(pf.get()))
388
  {
389
    return true;
390
  }
391
16917
  return false;
392
}
393
394
100959
ProofNodeManager* CDProof::getManager() const { return d_manager; }
395
396
2834890
bool CDProof::shouldOverwrite(ProofNode* pn, PfRule newId, CDPOverwrite opol)
397
{
398
2834890
  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
2885658
         || (opol == CDPOverwrite::ASSUME_ONLY && isAssumption(pn)
404
3296837
             && newId != PfRule::ASSUME);
405
}
406
407
14037446
bool CDProof::isAssumption(ProofNode* pn)
408
{
409
14037446
  PfRule rule = pn->getRule();
410
14037446
  if (rule == PfRule::ASSUME)
411
  {
412
1527568
    return true;
413
  }
414
12509878
  else if (rule == PfRule::SYMM)
415
  {
416
334444
    const std::vector<std::shared_ptr<ProofNode>>& pc = pn->getChildren();
417
334444
    Assert(pc.size() == 1);
418
334444
    return pc[0]->getRule() == PfRule::ASSUME;
419
  }
420
12175434
  return false;
421
}
422
423
948379
bool CDProof::isSame(TNode f, TNode g)
424
{
425
948379
  if (f == g)
426
  {
427
521517
    return true;
428
  }
429
426862
  Kind fk = f.getKind();
430
426862
  Kind gk = g.getKind();
431
426862
  if (fk == EQUAL && gk == EQUAL && f[0] == g[1] && f[1] == g[0])
432
  {
433
    // symmetric equality
434
170843
    return true;
435
  }
436
306248
  if (fk == NOT && gk == NOT && f[0].getKind() == EQUAL
437
276499
      && g[0].getKind() == EQUAL && f[0][0] == g[0][1] && f[0][1] == g[0][0])
438
  {
439
    // symmetric disequality
440
586
    return true;
441
  }
442
255433
  return false;
443
}
444
445
21549604
Node CDProof::getSymmFact(TNode f)
446
{
447
21549604
  bool polarity = f.getKind() != NOT;
448
43099208
  TNode fatom = polarity ? f : f[0];
449
21549604
  if (fatom.getKind() != EQUAL || fatom[0] == fatom[1])
450
  {
451
13511185
    return Node::null();
452
  }
453
16076838
  Node symFact = fatom[1].eqNode(fatom[0]);
454
8038419
  return polarity ? symFact : symFact.notNode();
455
}
456
457
21950521
std::string CDProof::identify() const { return d_name; }
458
459
29340
}  // namespace cvc5