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