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