GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/congruence_manager.h Lines: 3 3 100.0 %
Date: 2021-05-22 Branches: 0 0 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Alex Ozdemir, Tim King, Andrew Reynolds
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
 * [[ Add one-line brief description here ]]
14
 *
15
 * [[ Add lengthier description here ]]
16
 * \todo document this file
17
 */
18
19
#include "cvc5_private.h"
20
21
#pragma once
22
23
#include "context/cdhashmap.h"
24
#include "context/cdlist.h"
25
#include "context/cdmaybe.h"
26
#include "context/cdtrail_queue.h"
27
#include "theory/arith/arith_utilities.h"
28
#include "theory/arith/arithvar.h"
29
#include "theory/arith/callbacks.h"
30
#include "theory/arith/constraint_forward.h"
31
#include "theory/trust_node.h"
32
#include "theory/uf/equality_engine_notify.h"
33
#include "util/dense_map.h"
34
#include "util/statistics_stats.h"
35
36
namespace cvc5 {
37
38
class ProofNodeManager;
39
40
namespace context {
41
class Context;
42
class UserContext;
43
}
44
45
namespace theory {
46
47
class EagerProofGenerator;
48
struct EeSetupInfo;
49
50
namespace eq {
51
class ProofEqEngine;
52
class EqualityEngine;
53
}
54
55
namespace arith {
56
57
class ArithVariables;
58
59
class ArithCongruenceManager {
60
private:
61
  context::CDRaised d_inConflict;
62
  RaiseEqualityEngineConflict d_raiseConflict;
63
64
  /**
65
   * The set of ArithVars equivalent to a pair of terms.
66
   * If this is 0 or cannot be 0, this can be signalled.
67
   * The pair of terms for the congruence is stored in watched equalities.
68
   */
69
  DenseSet d_watchedVariables;
70
  /** d_watchedVariables |-> (= x y) */
71
  ArithVarToNodeMap d_watchedEqualities;
72
73
74
9459
  class ArithCongruenceNotify : public eq::EqualityEngineNotify {
75
  private:
76
    ArithCongruenceManager& d_acm;
77
  public:
78
    ArithCongruenceNotify(ArithCongruenceManager& acm);
79
80
    bool eqNotifyTriggerPredicate(TNode predicate, bool value) override;
81
82
    bool eqNotifyTriggerTermEquality(TheoryId tag,
83
                                     TNode t1,
84
                                     TNode t2,
85
                                     bool value) override;
86
87
    void eqNotifyConstantTermMerge(TNode t1, TNode t2) override;
88
    void eqNotifyNewClass(TNode t) override;
89
    void eqNotifyMerge(TNode t1, TNode t2) override;
90
    void eqNotifyDisequal(TNode t1, TNode t2, TNode reason) override;
91
  };
92
  ArithCongruenceNotify d_notify;
93
94
  context::CDList<Node> d_keepAlive;
95
96
  /** Store the propagations. */
97
  context::CDTrailQueue<Node> d_propagatations;
98
99
  /* This maps the node a theory engine will request on an explain call to
100
   * to its corresponding PropUnit.
101
   * This is node is potentially both the propagation or
102
   * Rewriter::rewrite(propagation).
103
   */
104
  typedef context::CDHashMap<Node, size_t> ExplainMap;
105
  ExplainMap d_explanationMap;
106
107
  ConstraintDatabase& d_constraintDatabase;
108
  SetupLiteralCallBack d_setupLiteral;
109
110
  const ArithVariables& d_avariables;
111
112
  /** The equality engine being used by this class */
113
  eq::EqualityEngine* d_ee;
114
  /** The sat context */
115
  context::Context* d_satContext;
116
  /** The user context */
117
  context::UserContext* d_userContext;
118
119
  /** proof manager */
120
  ProofNodeManager* d_pnm;
121
  /** A proof generator for storing proofs of facts that are asserted to the EQ
122
   * engine. Note that these proofs **are not closed**; they may contain
123
   * literals from the explanation of their fact as unclosed assumptions.
124
   * This makes these proofs SAT-context depdent.
125
   *
126
   * This is why this generator is separate from the TheoryArithPrivate
127
   * generator, which stores closed proofs.
128
   */
129
  std::unique_ptr<EagerProofGenerator> d_pfGenEe;
130
  /** A proof generator for TrustNodes sent to TheoryArithPrivate.
131
   *
132
   * When TheoryArithPrivate requests an explanation from
133
   * ArithCongruenceManager, it can request a TrustNode for that explanation.
134
   * This proof generator is the one used in that TrustNode: it stores the
135
   * (closed) proofs of implications proved by the
136
   * ArithCongruenceManager/EqualityEngine.
137
   *
138
   * It is insufficient to just use the ProofGenerator from the ProofEqEngine,
139
   * since sometimes the ArithCongruenceManager needs to add some
140
   * arith-specific reasoning to extend the proof (e.g. rewriting the result
141
   * into a normal form).
142
   * */
143
  std::unique_ptr<EagerProofGenerator> d_pfGenExplain;
144
145
  /** Pointer to the proof equality engine of TheoryArith */
146
  theory::eq::ProofEqEngine* d_pfee;
147
148
  /** Raise a conflict node `conflict` to the theory of arithmetic.
149
   *
150
   * When proofs are enabled, a (closed) proof of the conflict should be
151
   * provided.
152
   */
153
  void raiseConflict(Node conflict, std::shared_ptr<ProofNode> pf = nullptr);
154
  /**
155
   * Are proofs enabled? This is true if a non-null proof manager was provided
156
   * to the constructor of this class.
157
   */
158
  bool isProofEnabled() const;
159
160
 public:
161
  bool inConflict() const;
162
163
  bool hasMorePropagations() const;
164
165
  const Node getNextPropagation();
166
167
  bool canExplain(TNode n) const;
168
169
private:
170
  Node externalToInternal(TNode n) const;
171
172
  void pushBack(TNode n);
173
174
  void pushBack(TNode n, TNode r);
175
176
  void pushBack(TNode n, TNode r, TNode w);
177
178
  bool propagate(TNode x);
179
  void explain(TNode literal, std::vector<TNode>& assumptions);
180
181
  /** Assert this literal to the eq engine. Common functionality for
182
   *   * assertionToEqualityEngine(..)
183
   *   * equalsConstant(c)
184
   *   * equalsConstant(lb, ub)
185
   * If proof is off, then just asserts.
186
   */
187
  void assertLitToEqualityEngine(Node lit,
188
                                 TNode reason,
189
                                 std::shared_ptr<ProofNode> pf);
190
  /** This sends a shared term to the uninterpreted equality engine. */
191
  void assertionToEqualityEngine(bool eq,
192
                                 ArithVar s,
193
                                 TNode reason,
194
                                 std::shared_ptr<ProofNode> pf);
195
196
  /** Check for proof for this or a symmetric fact
197
   *
198
   * The proof submitted to this method are stored in `d_pfGenEe`, and should
199
   * have closure properties consistent with the documentation for that member.
200
   *
201
   * @returns whether this or a symmetric fact has a proof.
202
   */
203
  bool hasProofFor(TNode f) const;
204
  /**
205
   * Sets the proof for this fact and the symmetric one.
206
   *
207
   * The proof submitted to this method are stored in `d_pfGenEe`, and should
208
   * have closure properties consistent with the documentation for that member.
209
   * */
210
  void setProofFor(TNode f, std::shared_ptr<ProofNode> pf) const;
211
212
  /** Dequeues the delay queue and asserts these equalities.*/
213
  void enableSharedTerms();
214
  void dequeueLiterals();
215
216
  void enqueueIntoNB(const std::set<TNode> all, NodeBuilder& nb);
217
218
  /**
219
   * Determine an explaination for `internal`. That is a conjunction of theory
220
   * literals which imply `internal`.
221
   *
222
   * The TrustNode here is a trusted propagation.
223
   */
224
  TrustNode explainInternal(TNode internal);
225
226
 public:
227
  ArithCongruenceManager(context::Context* satContext,
228
                         context::UserContext* u,
229
                         ConstraintDatabase&,
230
                         SetupLiteralCallBack,
231
                         const ArithVariables&,
232
                         RaiseEqualityEngineConflict raiseConflict,
233
                         ProofNodeManager* pnm);
234
  ~ArithCongruenceManager();
235
236
  //--------------------------------- initialization
237
  /**
238
   * Returns true if we need an equality engine, see
239
   * Theory::needsEqualityEngine.
240
   */
241
  bool needsEqualityEngine(EeSetupInfo& esi);
242
  /**
243
   * Finish initialize. This class is instructed by TheoryArithPrivate to use
244
   * the equality engine ee and proof equality engine pfee.
245
   */
246
  void finishInit(eq::EqualityEngine* ee, eq::ProofEqEngine* pfee);
247
  //--------------------------------- end initialization
248
249
  /**
250
   * Return the trust node for the explanation of literal. The returned
251
   * trust node is generated by the proof equality engine of this class.
252
   */
253
  TrustNode explain(TNode literal);
254
255
  void explain(TNode lit, NodeBuilder& out);
256
257
  void addWatchedPair(ArithVar s, TNode x, TNode y);
258
259
5426876
  inline bool isWatchedVariable(ArithVar s) const {
260
5426876
    return d_watchedVariables.isMember(s);
261
  }
262
263
  /** Assert an equality. */
264
  void watchedVariableIsZero(ConstraintCP eq);
265
266
  /** Assert a conjunction from lb and ub. */
267
  void watchedVariableIsZero(ConstraintCP lb, ConstraintCP ub);
268
269
  /** Assert that the value cannot be zero. */
270
  void watchedVariableCannotBeZero(ConstraintCP c);
271
272
  /** Assert that the value cannot be zero. */
273
  void watchedVariableCannotBeZero(ConstraintCP c, ConstraintCP d);
274
275
276
  /** Assert that the value is congruent to a constant. */
277
  void equalsConstant(ConstraintCP eq);
278
  void equalsConstant(ConstraintCP lb, ConstraintCP ub);
279
280
 private:
281
  class Statistics {
282
  public:
283
    IntStat d_watchedVariables;
284
    IntStat d_watchedVariableIsZero;
285
    IntStat d_watchedVariableIsNotZero;
286
287
    IntStat d_equalsConstantCalls;
288
289
    IntStat d_propagations;
290
    IntStat d_propagateConstraints;
291
    IntStat d_conflicts;
292
293
    Statistics();
294
  } d_statistics;
295
296
};/* class ArithCongruenceManager */
297
298
std::vector<Node> andComponents(TNode an);
299
300
}  // namespace arith
301
}  // namespace theory
302
}  // namespace cvc5