GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/uf/proof_equality_engine.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, Gereon Kremer, Haniel Barbosa
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 proof-producing equality engine.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__UF__PROOF_EQUALITY_ENGINE_H
19
#define CVC5__THEORY__UF__PROOF_EQUALITY_ENGINE_H
20
21
#include <vector>
22
23
#include "context/cdhashmap.h"
24
#include "context/cdhashset.h"
25
#include "expr/node.h"
26
#include "proof/assumption_proof_generator.h"
27
#include "proof/buffered_proof_generator.h"
28
#include "proof/eager_proof_generator.h"
29
#include "proof/lazy_proof.h"
30
31
namespace cvc5 {
32
33
class Env;
34
class ProofNode;
35
class ProofNodeManager;
36
37
namespace theory {
38
namespace eq {
39
40
class EqualityEngine;
41
42
/**
43
 * A layer on top of an EqualityEngine. The goal of this class is manage the
44
 * use of an EqualityEngine object in such a way that the proper proofs are
45
 * internally constructed, and can be retrieved from this class when
46
 * necessary.
47
 *
48
 * Notice that this class is intended to be a *partial layer* on top of
49
 * equality engine. A user of this class should still issue low-level calls
50
 * (getRepresentative, areEqual, areDisequal, etc.) on the underlying equality
51
 * engine directly. The methods that should *not* be called directly on the
52
 * underlying equality engine are:
53
 * - assertEquality/assertPredicate [*]
54
 * - explain
55
 * Instead, the user should use variants of the above methods provided by
56
 * the public interface of this class.
57
 *
58
 * [*] the exception is that assertions from the fact queue (who are their own
59
 * explanation) should be sent directly to the underlying equality engine. This
60
 * is for the sake of efficiency.
61
 *
62
 * This class tracks the reason for why all facts are added to an EqualityEngine
63
 * in a SAT-context dependent manner in a context-dependent (CDProof) object.
64
 * It furthermore maintains an internal FactProofGenerator class for managing
65
 * proofs of facts whose steps are explicitly provided (those that are given
66
 * concrete PfRule, children, and args). Call these "simple facts".
67
 *
68
 * Overall, this class is an eager proof generator (theory/proof_generator.h),
69
 * in that it stores (copies) of proofs for lemmas at the moment they are sent
70
 * out.
71
 *
72
 * A theory that is proof producing and uses the equality engine may use this
73
 * class to manage proofs that are justified by its underlying equality engine.
74
 * In particular, the following interfaces are available for constructing
75
 * a TrustNode:
76
 * - assertConflict, when the user of the equality engine has discovered that
77
 * false can be derived from the current state,
78
 * - assertLemma, for lemmas/conflicts that can be (partially) explained in the
79
 * current state,
80
 * - explain, for explaining why a literal is true in the current state.
81
 * Details on these methods can be found below.
82
 */
83
class ProofEqEngine : public EagerProofGenerator
84
{
85
  typedef context::CDHashSet<Node> NodeSet;
86
  typedef context::CDHashMap<Node, std::shared_ptr<ProofNode>> NodeProofMap;
87
88
 public:
89
  /**
90
   * @param env The environment
91
   * @param ee The equality engine this is layered on
92
   */
93
  ProofEqEngine(Env& env, EqualityEngine& ee);
94
127728
  ~ProofEqEngine() {}
95
  //-------------------------- assert fact
96
  /**
97
   * Assert the literal lit by proof step id, given explanation exp and
98
   * arguments args. This fact is
99
   *
100
   * @param lit The literal to assert to the equality engine
101
   * @param id The proof rule of the proof step concluding lit
102
   * @param exp The premises of the proof step concluding lit. These are also
103
   * the premises that are used when calling explain(lit).
104
   * @param args The arguments to the proof step concluding lit.
105
   * @return true if this fact was processed by this method. If lit already
106
   * holds in the equality engine, this method returns false.
107
   */
108
  bool assertFact(Node lit,
109
                  PfRule id,
110
                  const std::vector<Node>& exp,
111
                  const std::vector<Node>& args);
112
  /** Same as above but where exp is (conjunctive) node */
113
  bool assertFact(Node lit, PfRule id, Node exp, const std::vector<Node>& args);
114
  /**
115
   * Multi-step version of assert fact via a proof step buffer. This method
116
   * is similar to above, but the justification for lit may have multiple steps.
117
   * In particular, we assume that psb has a list of proof steps where the
118
   * proof step concluding lit has free assumptions exp.
119
   *
120
   * For example, a legal call to this method is such that:
121
   *   lit: A
122
   *   exp: B
123
   *   psb.d_steps: { A by (step id1 {B,C} {}), C by (step id2 {} {}) )
124
   * In other words, A holds by a proof step with rule id1 and premises
125
   * B and C, and C holds by proof step with rule id2 and no premises.
126
   *
127
   * @param lit The literal to assert to the equality engine.
128
   * @param exp The premises of the proof steps concluding lit. These are also
129
   * the premises that are used when calling explain(lit).
130
   * @param psb The proof step buffer containing the proof steps.
131
   * @return true if this fact was processed by this method. If lit already
132
   * holds in the equality engine, this method returns false.
133
   */
134
  bool assertFact(Node lit, Node exp, ProofStepBuffer& psb);
135
  /**
136
   * Assert fact via generator pg. This method asserts lit with explanation exp
137
   * to the equality engine of this class. It must be the case that pg can
138
   * provide a proof for lit in terms of exp. More precisely, pg should be
139
   * prepared in the remainder of the SAT context to respond to a call to
140
   * ProofGenerator::getProofFor(lit), and return a proof whose free
141
   * assumptions are a subset of the conjuncts of exp.
142
   *
143
   * @param lit The literal to assert to the equality engine.
144
   * @param exp The premises of the proof concluding lit. These are also
145
   * the premises that are used when calling explain(lit).
146
   * @param pg The proof generator that can provide a proof concluding lit
147
   * from free asumptions in exp.
148
   * @return true if this fact was processed by this method. If lit already
149
   * holds in the equality engine, this method returns false.
150
   */
151
  bool assertFact(Node lit, Node exp, ProofGenerator* pg);
152
  //-------------------------- assert conflicts
153
  /**
154
   * This method is called when the equality engine of this class is
155
   * inconsistent (false has been proven) by a contradictory literal lit. This
156
   * returns the trust node corresponding to the current conflict.
157
   *
158
   * @param lit The conflicting literal, which must rewrite to false.
159
   * @return The trust node capturing the fact that this class can provide a
160
   * proof for this conflict.
161
   */
162
  TrustNode assertConflict(Node lit);
163
  /**
164
   * Get proven conflict from contradictory facts. This method is called when
165
   * the proof rule with premises exp and arguments args implies a contradiction
166
   * by proof rule id.
167
   *
168
   * This method returns the TrustNode containing the corresponding conflict
169
   * resulting from adding this step, and ensures that a proof has been stored
170
   * internally so that this class may respond to a call to
171
   * ProofGenerator::getProof(...).
172
   */
173
  TrustNode assertConflict(PfRule id,
174
                           const std::vector<Node>& exp,
175
                           const std::vector<Node>& args);
176
  /** Generator version, where pg has a proof of false from assumptions exp */
177
  TrustNode assertConflict(const std::vector<Node>& exp, ProofGenerator* pg);
178
  //-------------------------- assert lemma
179
  /**
180
   * Called when we have concluded conc, typically via theory specific
181
   * reasoning. The purpose of this method is to construct a TrustNode of
182
   * kind TrustNodeKind::LEMMA or TrustNodeKind::CONFLICT corresponding to the
183
   * lemma or conflict to be sent on the output channel of the Theory.
184
   *
185
   * The user provides the explanation of conc in two parts:
186
   * (1) (exp \ noExplain), which are literals that hold in the equality engine
187
   * of this class,
188
   * (2) noExplain, which do not necessarily hold in the equality engine of this
189
   * class.
190
   * Notice that noExplain is a subset of exp.
191
   *
192
   * The proof for conc follows from exp by proof rule with the given
193
   * id and arguments.
194
   *
195
   * This call corresponds to a conflict if conc is false and noExplain is
196
   * empty.
197
   *
198
   * This returns the TrustNode corresponding to the formula corresonding to
199
   * the call to this method [*], for which a proof can be provided by this
200
   * generator in the remainder of the user context.
201
   *
202
   * [*]
203
   * a. If this call does not correspond to a conflict, then this formula is:
204
   *   ( ^_{e in exp \ noExplain} <explain>(e) ^ noExplain ) => conc
205
   * where <explain>(e) is a conjunction of literals L1 ^ ... ^ Ln such that
206
   * L1 ^ ... ^ Ln entail e, and each Li was passed as an explanation to a
207
   * call to assertFact in the current SAT context. This explanation method
208
   * always succeeds, provided that e is a literal that currently holds in
209
   * the equality engine of this class. Notice that if the antecedant is empty,
210
   * the formula above is assumed to be conc itself. The above formula is
211
   * intended to be valid in Theory that owns this class.
212
   * b. If this call is a conflict, then this formula is:
213
   *   ^_{e in exp} <explain>(e)
214
   * The formula can be queried via TrustNode::getProven in the standard way.
215
   */
216
  TrustNode assertLemma(Node conc,
217
                        PfRule id,
218
                        const std::vector<Node>& exp,
219
                        const std::vector<Node>& noExplain,
220
                        const std::vector<Node>& args);
221
  /** Generator version, where pg has a proof of conc */
222
  TrustNode assertLemma(Node conc,
223
                        const std::vector<Node>& exp,
224
                        const std::vector<Node>& noExplain,
225
                        ProofGenerator* pg);
226
  //-------------------------- explain
227
  /**
228
   * Explain literal conc. This calls the appropriate methods in the underlying
229
   * equality engine of this class to construct the explanation of why conc
230
   * currently holds.
231
   *
232
   * It returns a trust node of kind TrustNodeKind::PROP_EXP whose node
233
   * is the explanation of conc (a conjunction of literals that implies it).
234
   * The proof that can be proven by this generator is then (=> exp conc), see
235
   * TrustNode::getPropExpProven(conc,exp);
236
   *
237
   * @param conc The conclusion to explain
238
   * @return The trust node indicating the explanation of conc and the generator
239
   * (this class) that can prove the implication.
240
   */
241
  TrustNode explain(Node conc);
242
243
 private:
244
  /** Assert internal */
245
  bool assertFactInternal(TNode pred, bool polarity, TNode reason);
246
  /** holds */
247
  bool holds(TNode pred, bool polarity);
248
  /**
249
   * Ensure proof for fact. This is called by the above method after we have
250
   * determined the final set of assumptions used for showing conc. This
251
   * method is used for lemmas, conflicts, and explanations for propagations.
252
   * The argument tnk is the kind of trust node to return.
253
   */
254
  TrustNode ensureProofForFact(Node conc,
255
                               const std::vector<TNode>& assumps,
256
                               TrustNodeKind tnk,
257
                               ProofGenerator* curr);
258
  /**
259
   * This ensures the proof of the literals that are in exp but not in
260
   * noExplain have been added to curr. This additionally adds the
261
   * explanation of exp to assumps. It updates tnk to LEMMA if there
262
   * are any literals in exp that are not in noExplain.
263
   */
264
  void explainVecWithProof(TrustNodeKind& tnk,
265
                           std::vector<TNode>& assumps,
266
                           const std::vector<Node>& exp,
267
                           const std::vector<Node>& noExplain,
268
                           LazyCDProof* curr);
269
  /** Explain
270
   *
271
   * This adds to assumps the set of facts that were asserted to this
272
   * class in the current SAT context that are required for showing lit.
273
   *
274
   * This additionally registers the equality proof steps required to
275
   * regress the explanation of lit in curr.
276
   */
277
  void explainWithProof(Node lit,
278
                        std::vector<TNode>& assumps,
279
                        LazyCDProof* curr);
280
  /** Reference to the equality engine */
281
  eq::EqualityEngine& d_ee;
282
  /** The default proof generator (for simple facts) */
283
  BufferedProofGenerator d_factPg;
284
  /** The no-explain proof generator */
285
  AssumptionProofGenerator d_assumpPg;
286
  /** common nodes */
287
  Node d_true;
288
  Node d_false;
289
  /** the proof node manager */
290
  ProofNodeManager* d_pnm;
291
  /** The SAT-context-dependent proof object */
292
  LazyCDProof d_proof;
293
  /**
294
   * The keep set of this class. This set is maintained to ensure that
295
   * facts and their explanations are reference counted. Since facts and their
296
   * explanations are SAT-context-dependent, this set is also
297
   * SAT-context-dependent.
298
   */
299
  NodeSet d_keep;
300
};
301
302
}  // namespace eq
303
}  // namespace theory
304
}  // namespace cvc5
305
306
#endif /* CVC5__THEORY__STRINGS__PROOF_MANAGER_H */