GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/proof/proof_checker.h Lines: 3 4 75.0 %
Date: 2021-09-29 Branches: 0 0 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   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
 * Proof checker utility.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__PROOF__PROOF_CHECKER_H
19
#define CVC5__PROOF__PROOF_CHECKER_H
20
21
#include <map>
22
23
#include "expr/node.h"
24
#include "proof/proof_rule.h"
25
#include "util/statistics_stats.h"
26
27
namespace cvc5 {
28
29
class ProofChecker;
30
class ProofNode;
31
32
namespace rewriter {
33
class RewriteDb;
34
}
35
36
/** A virtual base class for checking a proof rule */
37
class ProofRuleChecker
38
{
39
 public:
40
63339
  ProofRuleChecker() {}
41
63308
  virtual ~ProofRuleChecker() {}
42
  /**
43
   * This checks a single step in a proof.
44
   *
45
   * Return the formula that is proven by a proof node with the given id,
46
   * premises and arguments, or null if such a proof node is not well-formed.
47
   *
48
   * Note that the input/output of this method expects to be terms in *Skolem
49
   * form*, which is passed to checkInternal below. Rule checkers may
50
   * convert premises to witness form when necessary.
51
   *
52
   * @param id The id of the proof node to check
53
   * @param children The premises of the proof node to check. These are nodes
54
   * corresponding to the conclusion (ProofNode::getResult) of the children
55
   * of the proof node we are checking in Skolem form.
56
   * @param args The arguments of the proof node to check
57
   * @return The conclusion of the proof node, in Skolem form, if successful or
58
   * null if such a proof node is malformed.
59
   */
60
  Node check(PfRule id,
61
             const std::vector<Node>& children,
62
             const std::vector<Node>& args);
63
64
  /** get an index from a node, return false if we fail */
65
  static bool getUInt32(TNode n, uint32_t& i);
66
  /** get a Boolean from a node, return false if we fail */
67
  static bool getBool(TNode n, bool& b);
68
  /** get a Kind from a node, return false if we fail */
69
  static bool getKind(TNode n, Kind& k);
70
  /** Make a Kind into a node */
71
  static Node mkKindNode(Kind k);
72
73
  /** Register all rules owned by this rule checker into pc. */
74
  virtual void registerTo(ProofChecker* pc) {}
75
76
 protected:
77
  /**
78
   * This checks a single step in a proof.
79
   *
80
   * @param id The id of the proof node to check
81
   * @param children The premises of the proof node to check. These are nodes
82
   * corresponding to the conclusion (ProofNode::getResult) of the children
83
   * of the proof node we are checking.
84
   * @param args The arguments of the proof node to check
85
   * @return The conclusion of the proof node if successful or null if such a
86
   * proof node is malformed.
87
   */
88
  virtual Node checkInternal(PfRule id,
89
                             const std::vector<Node>& children,
90
                             const std::vector<Node>& args) = 0;
91
};
92
93
/** Statistics class */
94
class ProofCheckerStatistics
95
{
96
 public:
97
  ProofCheckerStatistics();
98
  /** Counts the number of checks for each kind of proof rule */
99
  HistogramStat<PfRule> d_ruleChecks;
100
  /** Total number of rule checks */
101
  IntStat d_totalRuleChecks;
102
};
103
104
/** A class for checking proofs */
105
class ProofChecker
106
{
107
 public:
108
  ProofChecker(bool eagerCheck,
109
               uint32_t pclevel = 0,
110
               rewriter::RewriteDb* rdb = nullptr);
111
143
  ~ProofChecker() {}
112
  /**
113
   * Return the formula that is proven by proof node pn, or null if pn is not
114
   * well-formed. If expected is non-null, then we return null if pn does not
115
   * prove expected.
116
   *
117
   * @param pn The proof node to check
118
   * @param expected The (optional) formula that is the expected conclusion of
119
   * the proof node.
120
   * @return The conclusion of the proof node if successful or null if the
121
   * proof is malformed, or if no checker is available for id.
122
   */
123
  Node check(ProofNode* pn, Node expected = Node::null());
124
  /** Same as above, with explicit arguments
125
   *
126
   * @param id The id of the proof node to check
127
   * @param children The children of the proof node to check
128
   * @param args The arguments of the proof node to check
129
   * @param expected The (optional) formula that is the expected conclusion of
130
   * the proof node.
131
   * @return The conclusion of the proof node if successful or null if the
132
   * proof is malformed, or if no checker is available for id.
133
   */
134
  Node check(PfRule id,
135
             const std::vector<std::shared_ptr<ProofNode>>& children,
136
             const std::vector<Node>& args,
137
             Node expected = Node::null());
138
  /**
139
   * Same as above, without conclusions instead of proof node children. This
140
   * is used for debugging. In particular, this function does not throw an
141
   * assertion failure when a proof step is malformed and can be used without
142
   * constructing proof nodes.
143
   *
144
   * @param id The id of the proof node to check
145
   * @param children The conclusions of the children of the proof node to check
146
   * @param args The arguments of the proof node to check
147
   * @param expected The (optional) formula that is the expected conclusion of
148
   * the proof node.
149
   * @param traceTag The trace tag to print debug information to
150
   * @return The conclusion of the proof node if successful or null if the
151
   * proof is malformed, or if no checker is available for id.
152
   */
153
  Node checkDebug(PfRule id,
154
                  const std::vector<Node>& cchildren,
155
                  const std::vector<Node>& args,
156
                  Node expected = Node::null(),
157
                  const char* traceTag = "");
158
  /** Indicate that psc is the checker for proof rule id */
159
  void registerChecker(PfRule id, ProofRuleChecker* psc);
160
  /**
161
   * Indicate that id is a trusted rule with the given pedantic level, e.g.:
162
   *  0: (mandatory) always a failure to use the given id
163
   *  1: (major) failure on all (non-zero) pedantic levels
164
   * 10: (minor) failure only on pedantic levels >= 10.
165
   */
166
  void registerTrustedChecker(PfRule id,
167
                              ProofRuleChecker* psc,
168
                              uint32_t plevel = 10);
169
  /** get checker for */
170
  ProofRuleChecker* getCheckerFor(PfRule id);
171
  /** get the rewrite database */
172
  rewriter::RewriteDb* getRewriteDatabase();
173
  /**
174
   * Get the pedantic level for id if it has been assigned a pedantic
175
   * level via registerTrustedChecker above, or zero otherwise.
176
   */
177
  uint32_t getPedanticLevel(PfRule id) const;
178
179
  /**
180
   * Is pedantic failure? If so, we return true and write a debug message on the
181
   * output stream out if enableOutput is true.
182
   */
183
  bool isPedanticFailure(PfRule id,
184
                         std::ostream& out,
185
                         bool enableOutput = true) const;
186
187
 private:
188
  /** statistics class */
189
  ProofCheckerStatistics d_stats;
190
  /** Maps proof rules to their checker */
191
  std::map<PfRule, ProofRuleChecker*> d_checker;
192
  /** Maps proof trusted rules to their pedantic level */
193
  std::map<PfRule, uint32_t> d_plevel;
194
  /** Whether we check for pedantic failures eagerly */
195
  bool d_eagerCheck;
196
  /** The pedantic level of this checker */
197
  uint32_t d_pclevel;
198
  /** Pointer to the rewrite database */
199
  rewriter::RewriteDb* d_rdb;
200
  /**
201
   * Check internal. This is used by check and checkDebug above. It writes
202
   * checking errors on out when enableOutput is true. We treat trusted checkers
203
   * (nullptr in the range of the map d_checker) as failures if
204
   * useTrustedChecker = false.
205
   */
206
  Node checkInternal(PfRule id,
207
                     const std::vector<Node>& cchildren,
208
                     const std::vector<Node>& args,
209
                     Node expected,
210
                     std::stringstream& out,
211
                     bool useTrustedChecker,
212
                     bool enableOutput);
213
};
214
215
}  // namespace cvc5
216
217
#endif /* CVC5__PROOF__PROOF_CHECKER_H */