Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * This file is part of the cvc5 project. 3 : : * 4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS 5 : : * in the top-level source directory and their institutional affiliations. 6 : : * All rights reserved. See the file COPYING in the top-level source 7 : : * directory for licensing information. 8 : : * **************************************************************************** 9 : : * 10 : : * Congruence manager, the interface to the equality engine from the 11 : : * linear arithmetic solver 12 : : */ 13 : : 14 : : #include "cvc5_private.h" 15 : : 16 : : #pragma once 17 : : 18 : : #include "context/cdhashmap.h" 19 : : #include "context/cdlist.h" 20 : : #include "context/cdmaybe.h" 21 : : #include "context/cdtrail_queue.h" 22 : : #include "proof/trust_node.h" 23 : : #include "smt/env_obj.h" 24 : : #include "theory/arith/arith_utilities.h" 25 : : #include "theory/arith/linear/arithvar.h" 26 : : #include "theory/arith/linear/arithvar_node_map.h" 27 : : #include "theory/arith/linear/callbacks.h" 28 : : #include "theory/arith/linear/constraint_forward.h" 29 : : #include "theory/uf/equality_engine_notify.h" 30 : : #include "util/dense_map.h" 31 : : #include "util/statistics_stats.h" 32 : : 33 : : namespace cvc5::internal { 34 : : 35 : : class ProofNodeManager; 36 : : class EagerProofGenerator; 37 : : 38 : : namespace theory { 39 : : 40 : : namespace eq { 41 : : class ProofEqEngine; 42 : : class EqualityEngine; 43 : : } // namespace eq 44 : : 45 : : namespace arith::linear { 46 : : 47 : : class ArithVariables; 48 : : 49 : : class ArithCongruenceManager : protected EnvObj 50 : : { 51 : : public: 52 : : ArithCongruenceManager(Env& env, 53 : : ConstraintDatabase&, 54 : : SetupLiteralCallBack, 55 : : const ArithVariables&, 56 : : RaiseEqualityEngineConflict raiseConflict); 57 : : ~ArithCongruenceManager(); 58 : : 59 : : //--------------------------------- initialization 60 : : /** 61 : : * Finish initialize. This class is instructed by TheoryArithPrivate to use 62 : : * the equality engine ee. 63 : : */ 64 : : void finishInit(eq::EqualityEngine* ee); 65 : : //--------------------------------- end initialization 66 : : 67 : : /** 68 : : * Return the trust node for the explanation of literal. The returned 69 : : * trust node is generated by the proof equality engine of this class. 70 : : */ 71 : : TrustNode explain(TNode literal); 72 : : 73 : : void addWatchedPair(ArithVar s, TNode x, TNode y); 74 : : 75 : 12743362 : bool isWatchedVariable(ArithVar s) const 76 : : { 77 : 12743362 : return d_watchedVariables.isMember(s); 78 : : } 79 : : 80 : : /** Assert an equality. */ 81 : : void watchedVariableIsZero(ConstraintCP eq); 82 : : 83 : : /** Assert a conjunction from lb and ub. */ 84 : : void watchedVariableIsZero(ConstraintCP lb, ConstraintCP ub); 85 : : 86 : : /** Assert that the value cannot be zero. */ 87 : : void watchedVariableCannotBeZero(ConstraintCP c); 88 : : 89 : : /** Assert that the value cannot be zero. */ 90 : : void watchedVariableCannotBeZero(ConstraintCP c, ConstraintCP d); 91 : : 92 : : /** Assert that the value is congruent to a constant. */ 93 : : void equalsConstant(ConstraintCP eq); 94 : : void equalsConstant(ConstraintCP lb, ConstraintCP ub); 95 : : 96 : : bool inConflict() const; 97 : : 98 : : bool hasMorePropagations() const; 99 : : 100 : : const Node getNextPropagation(); 101 : : 102 : : bool canExplain(TNode n) const; 103 : : 104 : : /** 105 : : * Propagate. Called when the equality engine has inferred literal x. 106 : : */ 107 : : bool propagate(TNode x); 108 : : 109 : : private: 110 : : context::CDRaised d_inConflict; 111 : : RaiseEqualityEngineConflict d_raiseConflict; 112 : : 113 : : /** 114 : : * The set of ArithVars equivalent to a pair of terms. 115 : : * If this is 0 or cannot be 0, this can be signalled. 116 : : * The pair of terms for the congruence is stored in watched equalities. 117 : : */ 118 : : DenseSet d_watchedVariables; 119 : : /** d_watchedVariables |-> (= x y) */ 120 : : ArithVarToNodeMap d_watchedEqualities; 121 : : 122 : : context::CDList<Node> d_keepAlive; 123 : : 124 : : /** Store the propagations. */ 125 : : context::CDTrailQueue<Node> d_propagatations; 126 : : 127 : : /* This maps the node a theory engine will request on an explain call to 128 : : * to its corresponding PropUnit. 129 : : * This is node is potentially both the propagation or 130 : : * rewrite(propagation). 131 : : */ 132 : : using ExplainMap = context::CDHashMap<Node, size_t>; 133 : : ExplainMap d_explanationMap; 134 : : 135 : : ConstraintDatabase& d_constraintDatabase; 136 : : SetupLiteralCallBack d_setupLiteral; 137 : : 138 : : const ArithVariables& d_avariables; 139 : : 140 : : /** The equality engine being used by this class */ 141 : : eq::EqualityEngine* d_ee; 142 : : /** proof manager */ 143 : : ProofNodeManager* d_pnm; 144 : : /** A proof generator for storing proofs of facts that are asserted to the EQ 145 : : * engine. Note that these proofs **are not closed**; they may contain 146 : : * literals from the explanation of their fact as unclosed assumptions. 147 : : * This makes these proofs SAT-context depdent. 148 : : * 149 : : * This is why this generator is separate from the TheoryArithPrivate 150 : : * generator, which stores closed proofs. 151 : : */ 152 : : std::unique_ptr<EagerProofGenerator> d_pfGenEe; 153 : : /** A proof generator for TrustNodes sent to TheoryArithPrivate. 154 : : * 155 : : * When TheoryArithPrivate requests an explanation from 156 : : * ArithCongruenceManager, it can request a TrustNode for that explanation. 157 : : * This proof generator is the one used in that TrustNode: it stores the 158 : : * (closed) proofs of implications proved by the 159 : : * ArithCongruenceManager/EqualityEngine. 160 : : * 161 : : * It is insufficient to just use the ProofGenerator from the ProofEqEngine, 162 : : * since sometimes the ArithCongruenceManager needs to add some 163 : : * arith-specific reasoning to extend the proof (e.g. rewriting the result 164 : : * into a normal form). 165 : : * */ 166 : : std::unique_ptr<EagerProofGenerator> d_pfGenExplain; 167 : : 168 : : /** Pointer to the proof equality engine of TheoryArith */ 169 : : theory::eq::ProofEqEngine* d_pfee; 170 : : 171 : : /** Raise a conflict node `conflict` to the theory of arithmetic. 172 : : * 173 : : * When proofs are enabled, a (closed) proof of the conflict should be 174 : : * provided. 175 : : */ 176 : : void raiseConflict(Node conflict, std::shared_ptr<ProofNode> pf = nullptr); 177 : : /** 178 : : * Are proofs enabled? This is true if a non-null proof manager was provided 179 : : * to the constructor of this class. 180 : : */ 181 : : bool isProofEnabled() const; 182 : : 183 : : Node externalToInternal(TNode n) const; 184 : : 185 : : void pushBack(TNode n); 186 : : 187 : : void pushBack(TNode n, TNode r); 188 : : 189 : : void pushBack(TNode n, TNode r, TNode w); 190 : : 191 : : /** Assert this literal to the eq engine. Common functionality for 192 : : * * assertionToEqualityEngine(..) 193 : : * * equalsConstant(c) 194 : : * * equalsConstant(lb, ub) 195 : : * If proof is off, then just asserts. 196 : : */ 197 : : void assertLitToEqualityEngine(Node lit, 198 : : TNode reason, 199 : : std::shared_ptr<ProofNode> pf); 200 : : /** This sends a shared term to the uninterpreted equality engine. */ 201 : : void assertionToEqualityEngine(bool eq, 202 : : ArithVar s, 203 : : TNode reason, 204 : : std::shared_ptr<ProofNode> pf); 205 : : 206 : : /** Check for proof for this or a symmetric fact 207 : : * 208 : : * The proof submitted to this method are stored in `d_pfGenEe`, and should 209 : : * have closure properties consistent with the documentation for that member. 210 : : * 211 : : * @returns whether this or a symmetric fact has a proof. 212 : : */ 213 : : bool hasProofFor(TNode f) const; 214 : : /** 215 : : * Sets the proof for this fact and the symmetric one. 216 : : * 217 : : * The proof submitted to this method are stored in `d_pfGenEe`, and should 218 : : * have closure properties consistent with the documentation for that member. 219 : : * */ 220 : : void setProofFor(TNode f, std::shared_ptr<ProofNode> pf) const; 221 : : 222 : : /** Dequeues the delay queue and asserts these equalities.*/ 223 : : void enableSharedTerms(); 224 : : void dequeueLiterals(); 225 : : 226 : : /** 227 : : * Determine an explaination for `internal`. That is a conjunction of theory 228 : : * literals which imply `internal`. 229 : : * 230 : : * The TrustNode here is a trusted propagation. 231 : : */ 232 : : TrustNode explainInternal(TNode internal); 233 : : 234 : : class Statistics 235 : : { 236 : : public: 237 : : IntStat d_watchedVariables; 238 : : IntStat d_watchedVariableIsZero; 239 : : IntStat d_watchedVariableIsNotZero; 240 : : 241 : : IntStat d_equalsConstantCalls; 242 : : 243 : : IntStat d_propagations; 244 : : IntStat d_propagateConstraints; 245 : : IntStat d_conflicts; 246 : : 247 : : Statistics(StatisticsRegistry& sr); 248 : : } d_statistics; 249 : : 250 : : }; /* class ArithCongruenceManager */ 251 : : 252 : : } // namespace arith::linear 253 : : } // namespace theory 254 : : } // namespace cvc5::internal