GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/smt/proof_post_processor.h Lines: 1 1 100.0 %
Date: 2021-11-07 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
 * The module for processing proof nodes.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__SMT__PROOF_POST_PROCESSOR_H
19
#define CVC5__SMT__PROOF_POST_PROCESSOR_H
20
21
#include <map>
22
#include <sstream>
23
#include <unordered_set>
24
25
#include "proof/proof_node_updater.h"
26
#include "smt/env_obj.h"
27
#include "smt/proof_final_callback.h"
28
#include "smt/witness_form.h"
29
#include "theory/inference_id.h"
30
#include "util/statistics_stats.h"
31
32
namespace cvc5 {
33
34
namespace rewriter {
35
class RewriteDb;
36
}
37
38
namespace smt {
39
40
/**
41
 * A callback class used by SolverEngine for post-processing proof nodes by
42
 * connecting proofs of preprocessing, and expanding macro PfRule applications.
43
 */
44
class ProofPostprocessCallback : public ProofNodeUpdaterCallback, protected EnvObj
45
{
46
 public:
47
  ProofPostprocessCallback(Env& env,
48
                           ProofGenerator* pppg,
49
                           rewriter::RewriteDb* rdb,
50
                           bool updateScopedAssumptions);
51
7989
  ~ProofPostprocessCallback() {}
52
  /**
53
   * Initialize, called once for each new ProofNode to process. This initializes
54
   * static information to be used by successive calls to update.
55
   */
56
  void initializeUpdate();
57
  /**
58
   * Set eliminate rule, which adds rule to the list of rules we will eliminate
59
   * during update. This adds rule to d_elimRules. Supported rules for
60
   * elimination include MACRO_*, SUBS and REWRITE. Otherwise, this method
61
   * has no effect.
62
   */
63
  void setEliminateRule(PfRule rule);
64
  /** Should proof pn be updated? */
65
  bool shouldUpdate(std::shared_ptr<ProofNode> pn,
66
                    const std::vector<Node>& fa,
67
                    bool& continueUpdate) override;
68
  /** Update the proof rule application. */
69
  bool update(Node res,
70
              PfRule id,
71
              const std::vector<Node>& children,
72
              const std::vector<Node>& args,
73
              CDProof* cdp,
74
              bool& continueUpdate) override;
75
76
 private:
77
  /** Common constants */
78
  Node d_true;
79
  /** Pointer to the proof node manager */
80
  ProofNodeManager* d_pnm;
81
  /** The preprocessing proof generator */
82
  ProofGenerator* d_pppg;
83
  /** The witness form proof generator */
84
  WitnessFormGenerator d_wfpm;
85
  /** The witness form assumptions used in the proof */
86
  std::vector<Node> d_wfAssumptions;
87
  /** Kinds of proof rules we are eliminating */
88
  std::unordered_set<PfRule, PfRuleHashFunction> d_elimRules;
89
  /** Whether we post-process assumptions in scope. */
90
  bool d_updateScopedAssumptions;
91
  //---------------------------------reset at the begining of each update
92
  /** Mapping assumptions to their proof from preprocessing */
93
  std::map<Node, std::shared_ptr<ProofNode> > d_assumpToProof;
94
  //---------------------------------end reset at the begining of each update
95
  /**
96
   * Expand rules in the given application, add the expanded proof to cdp.
97
   * The set of rules we expand is configured by calls to setEliminateRule
98
   * above. This method calls update to perform possible post-processing in the
99
   * rules it introduces as a result of the expansion.
100
   *
101
   * @param id The rule of the application
102
   * @param children The children of the application
103
   * @param args The arguments of the application
104
   * @param cdp The proof to add to
105
   * @return The conclusion of the rule, or null if this rule is not eliminated.
106
   */
107
  Node expandMacros(PfRule id,
108
                    const std::vector<Node>& children,
109
                    const std::vector<Node>& args,
110
                    CDProof* cdp);
111
  /**
112
   * Update the proof rule application, called during expand macros when
113
   * we wish to apply the update method. This method has the same behavior
114
   * as update apart from ignoring the continueUpdate flag.
115
   */
116
  bool updateInternal(Node res,
117
                      PfRule id,
118
                      const std::vector<Node>& children,
119
                      const std::vector<Node>& args,
120
                      CDProof* cdp);
121
  /**
122
   * Add proof for witness form. This returns the equality t = toWitness(t)
123
   * and ensures that the proof of this equality has been added to cdp.
124
   * Notice the proof of this fact may have open assumptions of the form:
125
   *   k = toWitness(k)
126
   * where k is a skolem. Furthermore, note that all open assumptions of this
127
   * form are available via d_wfpm.getWitnessFormEqs() in the remainder of
128
   * the lifetime of this class.
129
   */
130
  Node addProofForWitnessForm(Node t, CDProof* cdp);
131
  /**
132
   * Apply transivity if necessary for the arguments. The nodes in
133
   * tchildren have been ordered such that they are legal arguments to TRANS.
134
   *
135
   * Returns the conclusion of the transitivity step, which is null if
136
   * tchildren is empty. Also note if tchildren contains a single element,
137
   * then no TRANS step is necessary to add to cdp.
138
   *
139
   * @param tchildren The children of a TRANS step
140
   * @param cdp The proof to add the TRANS step to
141
   * @return The conclusion of the TRANS step.
142
   */
143
  Node addProofForTrans(const std::vector<Node>& tchildren, CDProof* cdp);
144
  /**
145
   * Add proof for substitution step. Some substitutions are derived based
146
   * on viewing a formula as a Boolean assignment (see MethodId::SB_LITERAL for
147
   * example). This method ensures that the proof of var == subs exists
148
   * in cdp, where var, subs were derived from BuiltinProofRuleChecker's
149
   * getSubstitution method.
150
   *
151
   * @param var The variable of the substitution
152
   * @param subs The substituted term
153
   * @param assump The formula the substitution was derived from
154
   * @param cdp The proof to add to
155
   * @return var == subs, the conclusion of the substitution step.
156
   */
157
  Node addProofForSubsStep(Node var, Node subs, Node assump, CDProof* cdp);
158
  /** Add eq (or its symmetry) to transivity children, if not reflexive */
159
  bool addToTransChildren(Node eq,
160
                          std::vector<Node>& tchildren,
161
                          bool isSymm = false);
162
163
  /**
164
   * When given children and args lead to different sets of literals in a
165
   * conclusion depending on whether macro resolution or chain resolution is
166
   * applied, the literals that appear in the chain resolution result, but not
167
   * in the macro resolution result, from now on "crowding literals", are
168
   * literals removed implicitly by macro resolution. For example
169
   *
170
   *      l0 v l0 v l0 v l1 v l2    ~l0 v l1   ~l1
171
   * (1)  ----------------------------------------- MACRO_RES
172
   *                 l2
173
   *
174
   * but
175
   *
176
   *      l0 v l0 v l0 v l1 v l2    ~l0 v l1   ~l1
177
   * (2)  ---------------------------------------- CHAIN_RES
178
   *                l0 v l0 v l1 v l2
179
   *
180
   * where l0 and l1 are crowding literals in the second proof.
181
   *
182
   * There are two views for how MACRO_RES implicitly removes the crowding
183
   * literal, i.e., how MACRO_RES can be expanded into CHAIN_RES so that
184
   * crowding literals are removed. The first is that (1) becomes
185
   *
186
   *  l0 v l0 v l0 v l1 v l2  ~l0 v l1  ~l0 v l1  ~l0 v l1  ~l1  ~l1  ~l1  ~l1
187
   *  ---------------------------------------------------------------- CHAIN_RES
188
   *                                 l2
189
   *
190
   * via the repetition of the premise responsible for removing more than one
191
   * occurrence of the crowding literal. The issue however is that this
192
   * expansion is exponential. Note that (2) has two occurrences of l0 and one
193
   * of l1 as crowding literals. However, by repeating ~l0 v l1 two times to
194
   * remove l0, the clause ~l1, which would originally need to be repeated only
195
   * one time, now has to be repeated two extra times on top of that one. With
196
   * multiple crowding literals and their elimination depending on premises that
197
   * themselves add crowding literals one can easily end up with resolution
198
   * chains going from dozens to thousands of premises. Such examples do occur
199
   * in practice, even in our regressions.
200
   *
201
   * The second way of expanding MACRO_RES, which avoids this exponential
202
   * behavior, is so that (1) becomes
203
   *
204
   *      l0 v l0 v l0 v l1 v l2
205
   * (4)  ---------------------- FACTORING
206
   *      l0 v l1 v l2                       ~l0 v l1
207
   *      ------------------------------------------- CHAIN_RES
208
   *                   l1 v l1 v l2
209
   *                  ------------- FACTORING
210
   *                     l1 v l2                   ~l1
211
   *                    ------------------------------ CHAIN_RES
212
   *                                 l2
213
   *
214
   * This method first determines what are the crowding literals by checking
215
   * what literals occur in clauseLits that do not occur in targetClauseLits
216
   * (the latter contains the literals from the original MACRO_RES conclusion
217
   * while the former the literals from a direct application of CHAIN_RES). Then
218
   * it builds a proof such as (4) and adds the steps to cdp. The final
219
   * conclusion is returned.
220
   *
221
   * Note that in the example the CHAIN_RES steps introduced had only two
222
   * premises, and could thus be replaced by a RESOLUTION step, but since we
223
   * general there can be more than two premises we always use CHAIN_RES.
224
   *
225
   * @param clauseLits literals in the conclusion of a CHAIN_RESOLUTION step
226
   * with children and args[1:]
227
   * @param clauseLits literals in the conclusion of a MACRO_RESOLUTION step
228
   * with children and args
229
   * @param children a list of clauses
230
   * @param args a list of arguments to a MACRO_RESOLUTION step
231
   * @param cdp a CDProof
232
   * @return The resulting node of transforming MACRO_RESOLUTION into
233
   * CHAIN_RESOLUTION according to the above idea.
234
   */
235
  Node eliminateCrowdingLits(const std::vector<Node>& clauseLits,
236
                             const std::vector<Node>& targetClauseLits,
237
                             const std::vector<Node>& children,
238
                             const std::vector<Node>& args,
239
                             CDProof* cdp);
240
};
241
242
/**
243
 * The proof postprocessor module. This postprocesses the final proof
244
 * produced by an SolverEngine. Its main two tasks are to:
245
 * (1) Connect proofs of preprocessing,
246
 * (2) Expand macro PfRule applications.
247
 */
248
class ProofPostproccess : protected EnvObj
249
{
250
 public:
251
  /**
252
   * @param env The environment we are using
253
   * @param pppg The proof generator for pre-processing proofs
254
   * @param updateScopedAssumptions Whether we post-process assumptions in
255
   * scope. Since doing so is sound and only problematic depending on who is
256
   * consuming the proof, it's true by default.
257
   */
258
  ProofPostproccess(Env& env,
259
                    ProofGenerator* pppg,
260
                    rewriter::RewriteDb* rdb,
261
                    bool updateScopedAssumptions = true);
262
  ~ProofPostproccess();
263
  /** post-process */
264
  void process(std::shared_ptr<ProofNode> pf);
265
  /** set eliminate rule */
266
  void setEliminateRule(PfRule rule);
267
268
 private:
269
  /** The post process callback */
270
  ProofPostprocessCallback d_cb;
271
  /**
272
   * The updater, which is responsible for expanding macros in the final proof
273
   * and connecting preprocessed assumptions to input assumptions.
274
   */
275
  ProofNodeUpdater d_updater;
276
  /** The post process callback for finalization */
277
  ProofFinalCallback d_finalCb;
278
  /**
279
   * The finalizer, which is responsible for taking stats and checking for
280
   * (lazy) pedantic failures.
281
   */
282
  ProofNodeUpdater d_finalizer;
283
};
284
285
}  // namespace smt
286
}  // namespace cvc5
287
288
#endif