GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/proof/proof_checker.h Lines: 3 4 75.0 %
Date: 2021-08-14 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 theory {
33
class RewriteDb;
34
}
35
36
/** A virtual base class for checking a proof rule */
37
class ProofRuleChecker
38
{
39
 public:
40
99595
  ProofRuleChecker() {}
41
99595
  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(uint32_t pclevel = 0, theory::RewriteDb* rdb = nullptr);
109
3768
  ~ProofChecker() {}
110
  /**
111
   * Return the formula that is proven by proof node pn, or null if pn is not
112
   * well-formed. If expected is non-null, then we return null if pn does not
113
   * prove expected.
114
   *
115
   * @param pn The proof node to check
116
   * @param expected The (optional) formula that is the expected conclusion of
117
   * the proof node.
118
   * @return The conclusion of the proof node if successful or null if the
119
   * proof is malformed, or if no checker is available for id.
120
   */
121
  Node check(ProofNode* pn, Node expected = Node::null());
122
  /** Same as above, with explicit arguments
123
   *
124
   * @param id The id of the proof node to check
125
   * @param children The children of the proof node to check
126
   * @param args The arguments of the proof node to check
127
   * @param expected The (optional) formula that is the expected conclusion of
128
   * the proof node.
129
   * @return The conclusion of the proof node if successful or null if the
130
   * proof is malformed, or if no checker is available for id.
131
   */
132
  Node check(PfRule id,
133
             const std::vector<std::shared_ptr<ProofNode>>& children,
134
             const std::vector<Node>& args,
135
             Node expected = Node::null());
136
  /**
137
   * Same as above, without conclusions instead of proof node children. This
138
   * is used for debugging. In particular, this function does not throw an
139
   * assertion failure when a proof step is malformed and can be used without
140
   * constructing proof nodes.
141
   *
142
   * @param id The id of the proof node to check
143
   * @param children The conclusions of the children of the proof node to check
144
   * @param args The arguments of the proof node to check
145
   * @param expected The (optional) formula that is the expected conclusion of
146
   * the proof node.
147
   * @param traceTag The trace tag to print debug information to
148
   * @return The conclusion of the proof node if successful or null if the
149
   * proof is malformed, or if no checker is available for id.
150
   */
151
  Node checkDebug(PfRule id,
152
                  const std::vector<Node>& cchildren,
153
                  const std::vector<Node>& args,
154
                  Node expected = Node::null(),
155
                  const char* traceTag = "");
156
  /** Indicate that psc is the checker for proof rule id */
157
  void registerChecker(PfRule id, ProofRuleChecker* psc);
158
  /**
159
   * Indicate that id is a trusted rule with the given pedantic level, e.g.:
160
   *  0: (mandatory) always a failure to use the given id
161
   *  1: (major) failure on all (non-zero) pedantic levels
162
   * 10: (minor) failure only on pedantic levels >= 10.
163
   */
164
  void registerTrustedChecker(PfRule id,
165
                              ProofRuleChecker* psc,
166
                              uint32_t plevel = 10);
167
  /** get checker for */
168
  ProofRuleChecker* getCheckerFor(PfRule id);
169
  /** get the rewrite database */
170
  theory::RewriteDb* getRewriteDatabase();
171
  /**
172
   * Get the pedantic level for id if it has been assigned a pedantic
173
   * level via registerTrustedChecker above, or zero otherwise.
174
   */
175
  uint32_t getPedanticLevel(PfRule id) const;
176
177
  /**
178
   * Is pedantic failure? If so, we return true and write a debug message on the
179
   * output stream out if enableOutput is true.
180
   */
181
  bool isPedanticFailure(PfRule id,
182
                         std::ostream& out,
183
                         bool enableOutput = true) const;
184
185
 private:
186
  /** statistics class */
187
  ProofCheckerStatistics d_stats;
188
  /** Maps proof rules to their checker */
189
  std::map<PfRule, ProofRuleChecker*> d_checker;
190
  /** Maps proof trusted rules to their pedantic level */
191
  std::map<PfRule, uint32_t> d_plevel;
192
  /** The pedantic level of this checker */
193
  uint32_t d_pclevel;
194
  /** Pointer to the rewrite database */
195
  theory::RewriteDb* d_rdb;
196
  /**
197
   * Check internal. This is used by check and checkDebug above. It writes
198
   * checking errors on out when enableOutput is true. We treat trusted checkers
199
   * (nullptr in the range of the map d_checker) as failures if
200
   * useTrustedChecker = false.
201
   */
202
  Node checkInternal(PfRule id,
203
                     const std::vector<Node>& cchildren,
204
                     const std::vector<Node>& args,
205
                     Node expected,
206
                     std::stringstream& out,
207
                     bool useTrustedChecker,
208
                     bool enableOutput);
209
};
210
211
}  // namespace cvc5
212
213
#endif /* CVC5__PROOF__PROOF_CHECKER_H */