GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/congruence_manager.h Lines: 3 3 100.0 %
Date: 2021-11-07 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 "proof/trust_node.h"
28
#include "smt/env_obj.h"
29
#include "theory/arith/arith_utilities.h"
30
#include "theory/arith/arithvar.h"
31
#include "theory/arith/callbacks.h"
32
#include "theory/arith/constraint_forward.h"
33
#include "theory/uf/equality_engine_notify.h"
34
#include "util/dense_map.h"
35
#include "util/statistics_stats.h"
36
37
namespace cvc5 {
38
39
class ProofNodeManager;
40
class EagerProofGenerator;
41
42
namespace context {
43
class Context;
44
class UserContext;
45
}
46
47
namespace theory {
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 : protected EnvObj
60
{
61
 private:
62
  context::CDRaised d_inConflict;
63
  RaiseEqualityEngineConflict d_raiseConflict;
64
65
  /**
66
   * The set of ArithVars equivalent to a pair of terms.
67
   * If this is 0 or cannot be 0, this can be signalled.
68
   * The pair of terms for the congruence is stored in watched equalities.
69
   */
70
  DenseSet d_watchedVariables;
71
  /** d_watchedVariables |-> (= x y) */
72
  ArithVarToNodeMap d_watchedEqualities;
73
74
75
15268
  class ArithCongruenceNotify : public eq::EqualityEngineNotify {
76
  private:
77
    ArithCongruenceManager& d_acm;
78
  public:
79
    ArithCongruenceNotify(ArithCongruenceManager& acm);
80
81
    bool eqNotifyTriggerPredicate(TNode predicate, bool value) override;
82
83
    bool eqNotifyTriggerTermEquality(TheoryId tag,
84
                                     TNode t1,
85
                                     TNode t2,
86
                                     bool value) override;
87
88
    void eqNotifyConstantTermMerge(TNode t1, TNode t2) override;
89
    void eqNotifyNewClass(TNode t) override;
90
    void eqNotifyMerge(TNode t1, TNode t2) override;
91
    void eqNotifyDisequal(TNode t1, TNode t2, TNode reason) override;
92
  };
93
  ArithCongruenceNotify d_notify;
94
95
  context::CDList<Node> d_keepAlive;
96
97
  /** Store the propagations. */
98
  context::CDTrailQueue<Node> d_propagatations;
99
100
  /* This maps the node a theory engine will request on an explain call to
101
   * to its corresponding PropUnit.
102
   * This is node is potentially both the propagation or
103
   * rewrite(propagation).
104
   */
105
  typedef context::CDHashMap<Node, size_t> ExplainMap;
106
  ExplainMap d_explanationMap;
107
108
  ConstraintDatabase& d_constraintDatabase;
109
  SetupLiteralCallBack d_setupLiteral;
110
111
  const ArithVariables& d_avariables;
112
113
  /** The equality engine being used by this class */
114
  eq::EqualityEngine* d_ee;
115
  /** The equality engine we allocated */
116
  std::unique_ptr<eq::EqualityEngine> d_allocEe;
117
  /** proof manager */
118
  ProofNodeManager* d_pnm;
119
  /** A proof generator for storing proofs of facts that are asserted to the EQ
120
   * engine. Note that these proofs **are not closed**; they may contain
121
   * literals from the explanation of their fact as unclosed assumptions.
122
   * This makes these proofs SAT-context depdent.
123
   *
124
   * This is why this generator is separate from the TheoryArithPrivate
125
   * generator, which stores closed proofs.
126
   */
127
  std::unique_ptr<EagerProofGenerator> d_pfGenEe;
128
  /** A proof generator for TrustNodes sent to TheoryArithPrivate.
129
   *
130
   * When TheoryArithPrivate requests an explanation from
131
   * ArithCongruenceManager, it can request a TrustNode for that explanation.
132
   * This proof generator is the one used in that TrustNode: it stores the
133
   * (closed) proofs of implications proved by the
134
   * ArithCongruenceManager/EqualityEngine.
135
   *
136
   * It is insufficient to just use the ProofGenerator from the ProofEqEngine,
137
   * since sometimes the ArithCongruenceManager needs to add some
138
   * arith-specific reasoning to extend the proof (e.g. rewriting the result
139
   * into a normal form).
140
   * */
141
  std::unique_ptr<EagerProofGenerator> d_pfGenExplain;
142
143
  /** Pointer to the proof equality engine of TheoryArith */
144
  theory::eq::ProofEqEngine* d_pfee;
145
  /** The proof equality engine we allocated */
146
  std::unique_ptr<eq::ProofEqEngine> d_allocPfee;
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(Env& env,
228
                         ConstraintDatabase&,
229
                         SetupLiteralCallBack,
230
                         const ArithVariables&,
231
                         RaiseEqualityEngineConflict raiseConflict);
232
  ~ArithCongruenceManager();
233
234
  //--------------------------------- initialization
235
  /**
236
   * Returns true if we need an equality engine, see
237
   * Theory::needsEqualityEngine.
238
   */
239
  bool needsEqualityEngine(EeSetupInfo& esi);
240
  /**
241
   * Finish initialize. This class is instructed by TheoryArithPrivate to use
242
   * the equality engine ee.
243
   */
244
  void finishInit(eq::EqualityEngine* ee);
245
  //--------------------------------- end initialization
246
247
  /**
248
   * Return the trust node for the explanation of literal. The returned
249
   * trust node is generated by the proof equality engine of this class.
250
   */
251
  TrustNode explain(TNode literal);
252
253
  void explain(TNode lit, NodeBuilder& out);
254
255
  void addWatchedPair(ArithVar s, TNode x, TNode y);
256
257
7439409
  inline bool isWatchedVariable(ArithVar s) const {
258
7439409
    return d_watchedVariables.isMember(s);
259
  }
260
261
  /** Assert an equality. */
262
  void watchedVariableIsZero(ConstraintCP eq);
263
264
  /** Assert a conjunction from lb and ub. */
265
  void watchedVariableIsZero(ConstraintCP lb, ConstraintCP ub);
266
267
  /** Assert that the value cannot be zero. */
268
  void watchedVariableCannotBeZero(ConstraintCP c);
269
270
  /** Assert that the value cannot be zero. */
271
  void watchedVariableCannotBeZero(ConstraintCP c, ConstraintCP d);
272
273
274
  /** Assert that the value is congruent to a constant. */
275
  void equalsConstant(ConstraintCP eq);
276
  void equalsConstant(ConstraintCP lb, ConstraintCP ub);
277
278
 private:
279
  class Statistics {
280
  public:
281
    IntStat d_watchedVariables;
282
    IntStat d_watchedVariableIsZero;
283
    IntStat d_watchedVariableIsNotZero;
284
285
    IntStat d_equalsConstantCalls;
286
287
    IntStat d_propagations;
288
    IntStat d_propagateConstraints;
289
    IntStat d_conflicts;
290
291
    Statistics();
292
  } d_statistics;
293
294
}; /* class ArithCongruenceManager */
295
296
std::vector<Node> andComponents(TNode an);
297
298
}  // namespace arith
299
}  // namespace theory
300
}  // namespace cvc5