LCOV - code coverage report
Current view: top level - buildbot/coverage/build/src/theory/uf - proof_equality_engine.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 1 100.0 %
Date: 2026-08-13 10:35:43 Functions: 2 2 100.0 %
Branches: 0 0 -

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

Generated by: LCOV version 1.14