GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/proof/proof_node_manager.h Lines: 1 1 100.0 %
Date: 2021-09-29 Branches: 0 0 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Haniel Barbosa, 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
 * Proof node manager utility.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__PROOF__PROOF_NODE_MANAGER_H
19
#define CVC5__PROOF__PROOF_NODE_MANAGER_H
20
21
#include <vector>
22
23
#include "expr/node.h"
24
#include "proof/proof_rule.h"
25
26
namespace cvc5 {
27
28
class ProofChecker;
29
class ProofNode;
30
31
/**
32
 * A manager for proof node objects. This is a trusted interface for creating
33
 * and updating ProofNode objects.
34
 *
35
 * In more detail, we say a ProofNode is "well-formed (with respect to checker
36
 * X)" if its d_proven field is non-null, and corresponds to the formula that
37
 * the ProofNode proves according to X. The ProofNodeManager class constructs
38
 * and update nodes that are well-formed with respect to its underlying checker.
39
 *
40
 * If no checker is provided, then the ProofNodeManager assigns the d_proven
41
 * field of ProofNode based on the provided "expected" argument in mkNode below,
42
 * which must be provided in this case.
43
 *
44
 * The ProofNodeManager is used as a trusted way of updating ProofNode objects
45
 * via updateNode below. In particular, this method leaves the d_proven field
46
 * unchanged and updates (if possible) the remaining content of a given proof
47
 * node.
48
 *
49
 * Notice that ProofNode objects are mutable, and hence this class does not
50
 * cache the results of mkNode. A version of this class that caches
51
 * immutable version of ProofNode objects could be built as an extension
52
 * or layer on top of this class.
53
 */
54
class ProofNodeManager
55
{
56
 public:
57
  ProofNodeManager(ProofChecker* pc = nullptr);
58
143
  ~ProofNodeManager() {}
59
  /**
60
   * This constructs a ProofNode with the given arguments. The expected
61
   * argument, when provided, indicates the formula that the returned node
62
   * is expected to prove. If we find that it does not, based on the underlying
63
   * checker, this method returns nullptr.
64
   *
65
   * @param id The id of the proof node.
66
   * @param children The children of the proof node.
67
   * @param args The arguments of the proof node.
68
   * @param expected (Optional) the expected conclusion of the proof node.
69
   * @return the proof node, or nullptr if the given arguments do not
70
   * consistute a proof of the expected conclusion according to the underlying
71
   * checker, if both are provided. It also returns nullptr if neither the
72
   * checker nor the expected field is provided, since in this case the
73
   * conclusion is unknown.
74
   */
75
  std::shared_ptr<ProofNode> mkNode(
76
      PfRule id,
77
      const std::vector<std::shared_ptr<ProofNode>>& children,
78
      const std::vector<Node>& args,
79
      Node expected = Node::null());
80
  /**
81
   * Make the proof node corresponding to the assumption of fact.
82
   *
83
   * @param fact The fact to assume.
84
   * @return The ASSUME proof of fact.
85
   */
86
  std::shared_ptr<ProofNode> mkAssume(Node fact);
87
  /**
88
   * Make symm, which accounts for whether the child is already a SYMM
89
   * node, in which case we return its child.
90
   */
91
  std::shared_ptr<ProofNode> mkSymm(std::shared_ptr<ProofNode> child,
92
                                    Node expected = Node::null());
93
  /**
94
   * Make transitivity proof, where children contains one or more proofs of
95
   * equalities that form an ordered chain. In other words, the vector children
96
   * is a legal set of children for an application of TRANS.
97
   */
98
  std::shared_ptr<ProofNode> mkTrans(
99
      const std::vector<std::shared_ptr<ProofNode>>& children,
100
      Node expected = Node::null());
101
102
  /**
103
   * Make scope having body pf and arguments (assumptions-to-close) assumps.
104
   * If ensureClosed is true, then this method throws an assertion failure if
105
   * the returned proof is not closed. This is the case if a free assumption
106
   * of pf is missing from the vector assumps.
107
   *
108
   * For conveinence, the proof pf may be modified to ensure that the overall
109
   * result is closed. For instance, given input:
110
   *   pf = TRANS( ASSUME( x=y ), ASSUME( y=z ) )
111
   *   assumps = { y=x, y=z }
112
   * This method will modify pf to be:
113
   *   pf = TRANS( SYMM( ASSUME( y=x ) ), ASSUME( y=z ) )
114
   * so that y=x matches the free assumption. The returned proof is:
115
   *   SCOPE(TRANS( SYMM( ASSUME( y=x ) ), ASSUME( y=z ) ) :args { y=x, y=z })
116
   *
117
   * When ensureClosed is true, duplicates are eliminated from assumps. The
118
   * reason for this is due to performance, since in this method, assumps is
119
   * converted to an unordered_set to do the above check and hence it is a
120
   * convienient time to eliminate duplicate literals.
121
   *
122
   * Additionally, if both ensureClosed and doMinimize are true, assumps is
123
   * updated to contain exactly the free asumptions of pf. This also includes
124
   * having no duplicates. Furthermore, if assumps is empty after minimization,
125
   * this method is a no-op.
126
   *
127
   * In each case, the update vector assumps is passed as arguments to SCOPE.
128
   *
129
   * @param pf The body of the proof,
130
   * @param assumps The assumptions-to-close of the scope,
131
   * @param ensureClosed Whether to ensure that the proof is closed,
132
   * @param doMinimize Whether to minimize assumptions.
133
   * @param expected the node that the scope should prove.
134
   * @return The scoped proof.
135
   */
136
  std::shared_ptr<ProofNode> mkScope(std::shared_ptr<ProofNode> pf,
137
                                     std::vector<Node>& assumps,
138
                                     bool ensureClosed = true,
139
                                     bool doMinimize = false,
140
                                     Node expected = Node::null());
141
  /**
142
   * This method updates pn to be a proof of the form <id>( children, args ),
143
   * while maintaining its d_proven field. This method returns false if this
144
   * proof manager is using a checker, and we compute that the above proof
145
   * is not a proof of the fact that pn previously proved.
146
   *
147
   * @param pn The proof node to update.
148
   * @param id The updated id of the proof node.
149
   * @param children The updated children of the proof node.
150
   * @param args The updated arguments of the proof node.
151
   * @return true if the update was successful.
152
   *
153
   * Notice that updateNode always returns true if there is no underlying
154
   * checker.
155
   */
156
  bool updateNode(ProofNode* pn,
157
                  PfRule id,
158
                  const std::vector<std::shared_ptr<ProofNode>>& children,
159
                  const std::vector<Node>& args);
160
  /**
161
   * Update node pn to have the contents of pnr. It should be the case that
162
   * pn and pnr prove the same fact, otherwise false is returned and pn is
163
   * unchanged.
164
   */
165
  bool updateNode(ProofNode* pn, ProofNode* pnr);
166
  /**
167
   * Ensure that pn is checked, regardless of the proof check format.
168
   */
169
  void ensureChecked(ProofNode* pn);
170
  /** Get the underlying proof checker */
171
  ProofChecker* getChecker() const;
172
  /**
173
   * Clone a proof node, which creates a deep copy of pn and returns it. The
174
   * dag structure of pn is the same as that in the returned proof node.
175
   *
176
   * @param pn The proof node to clone
177
   * @return the cloned proof node.
178
   */
179
  std::shared_ptr<ProofNode> clone(std::shared_ptr<ProofNode> pn) const;
180
  /**
181
   * Cancel double SYMM. Returns a proof node that is not a double application
182
   * of SYMM, e.g. for (SYMM (SYMM (r P))), this returns (r P) where r != SYMM.
183
   */
184
  static ProofNode* cancelDoubleSymm(ProofNode* pn);
185
186
 private:
187
  /** The (optional) proof checker */
188
  ProofChecker* d_checker;
189
  /** the true node */
190
  Node d_true;
191
  /** Check internal
192
   *
193
   * This returns the result of proof checking a ProofNode with the provided
194
   * arguments with an expected conclusion, which may not null if there is
195
   * no expected conclusion.
196
   *
197
   * This throws an assertion error if we fail to check such a proof node, or
198
   * if expected is provided (non-null) and is different what is proven by the
199
   * other arguments.
200
   *
201
   * The flag didCheck is set to true if the underlying proof checker was
202
   * invoked. This may be false if e.g. the proof checking mode is lazy.
203
   */
204
  Node checkInternal(PfRule id,
205
                     const std::vector<std::shared_ptr<ProofNode>>& children,
206
                     const std::vector<Node>& args,
207
                     Node expected,
208
                     bool& didCheck);
209
  /**
210
   * Update node internal, return true if successful. This is called by
211
   * the update node methods above. The argument needsCheck is whether we
212
   * need to check the correctness of the rule application. This is false
213
   * for the updateNode routine where pnr is an (already checked) proof node.
214
   */
215
  bool updateNodeInternal(
216
      ProofNode* pn,
217
      PfRule id,
218
      const std::vector<std::shared_ptr<ProofNode>>& children,
219
      const std::vector<Node>& args,
220
      bool needsCheck);
221
};
222
223
}  // namespace cvc5
224
225
#endif /* CVC5__PROOF__PROOF_NODE_H */