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 : : * Alpha equivalence checking. 11 : : */ 12 : : 13 : : #include "cvc5_private.h" 14 : : 15 : : #ifndef CVC5__ALPHA_EQUIVALENCE_H 16 : : #define CVC5__ALPHA_EQUIVALENCE_H 17 : : 18 : : #include "expr/term_canonize.h" 19 : : #include "proof/eager_proof_generator.h" 20 : : #include "smt/env_obj.h" 21 : : #include "theory/quantifiers/quant_util.h" 22 : : 23 : : namespace cvc5::internal { 24 : : namespace theory { 25 : : namespace quantifiers { 26 : : 27 : : /** 28 : : * This trie stores a trie for each multi-set of types. For each term t 29 : : * registered to this node, we store t in the appropriate 30 : : * AlphaEquivalenceTypeNode trie. For example, if t contains 2 free variables of 31 : : * type T1 and 3 free variables of type T2, then it is stored at 32 : : * d_children[T1][2].d_children[T2][3]. 33 : : */ 34 : : class AlphaEquivalenceTypeNode 35 : : { 36 : : using NodeMap = context::CDHashMap<Node, Node>; 37 : : 38 : : public: 39 : : AlphaEquivalenceTypeNode(context::Context* c); 40 : : /** children of this node */ 41 : : std::map<std::pair<TypeNode, size_t>, 42 : : std::unique_ptr<AlphaEquivalenceTypeNode>> 43 : : d_children; 44 : : /** 45 : : * map from canonized quantifier bodies to a quantified formula whose 46 : : * canonized body is that term. 47 : : */ 48 : : NodeMap d_quant; 49 : : /** register node 50 : : * 51 : : * This registers term q to this trie. The term t is the canonical form of 52 : : * q, typs/typCount represent a multi-set of types of free variables in t. 53 : : */ 54 : : Node registerNode(context::Context* c, 55 : : Node q, 56 : : Node t, 57 : : std::vector<TypeNode>& typs, 58 : : std::map<TypeNode, size_t>& typCount); 59 : : }; 60 : : 61 : : /** 62 : : * Stores a database of quantified formulas, which computes alpha-equivalence. 63 : : */ 64 : : class AlphaEquivalenceDb 65 : : { 66 : : public: 67 : : AlphaEquivalenceDb(context::Context* c, 68 : : expr::TermCanonize* tc, 69 : : bool sortCommChildren); 70 : : /** adds quantified formula q to this database 71 : : * 72 : : * This function returns a quantified formula q' that is alpha-equivalent to 73 : : * q. If q' != q, then q' was previously added to this database via a call 74 : : * to addTerm. 75 : : */ 76 : : Node addTerm(Node q); 77 : : /** 78 : : * Add term with substitution, which additionally finds a set of terms such 79 : : * that q' * subs is alpha-equivalent (possibly modulo rewriting) to q, where 80 : : * q' is the return quantified formula. 81 : : */ 82 : : Node addTermWithSubstitution(Node q, 83 : : std::vector<Node>& vars, 84 : : std::vector<Node>& subs); 85 : : 86 : : private: 87 : : /** 88 : : * Add term to the trie, where t is the canonized form of the body of 89 : : * quantified formula q. Returns the quantified formula, if any, that already 90 : : * had been added to this class, or q otherwise. 91 : : */ 92 : : Node addTermToTypeTrie(Node t, Node q); 93 : : /** The context we depend on */ 94 : : context::Context* d_context; 95 : : /** a trie per # of variables per type */ 96 : : AlphaEquivalenceTypeNode d_ae_typ_trie; 97 : : /** pointer to the term canonize utility */ 98 : : expr::TermCanonize* d_tc; 99 : : /** whether to sort children of commutative operators during canonization. */ 100 : : bool d_sortCommutativeOpChildren; 101 : : /** 102 : : * Maps quantified formulas to variables map, used for tracking substitutions 103 : : * in addTermWithSubstitution. The range in d_bvmap[q] contains the mapping 104 : : * from canonical free variables to variables in q. 105 : : */ 106 : : std::map<Node, std::map<Node, TNode>> d_bvmap; 107 : : }; 108 : : 109 : : /** 110 : : * A quantifiers module that computes reductions based on alpha-equivalence, 111 : : * using the above utilities. 112 : : */ 113 : : class AlphaEquivalence : protected EnvObj 114 : : { 115 : : public: 116 : : AlphaEquivalence(Env& env); 117 : 87978 : ~AlphaEquivalence() {} 118 : : /** reduce quantifier 119 : : * 120 : : * If non-null, its return value is a trust node containing the lemma 121 : : * justifying why q is reducible. This lemma is of the form ( q = q' ) where 122 : : * q' is a quantified formula that was previously registered to this class via 123 : : * a call to reduceQuantifier, and q and q' are alpha-equivalent. 124 : : */ 125 : : TrustNode reduceQuantifier(Node q); 126 : : 127 : : private: 128 : : /** 129 : : * Add the alpha equivalence step f = f { vars -> subs } to cdp, where we 130 : : * ensure that f does not contain vars. Return the conclusion of this step. 131 : : */ 132 : : Node addAlphaEquivStep(CDProof& cdp, 133 : : const Node& f, 134 : : const std::vector<Node>& vars, 135 : : const std::vector<Node>& subs); 136 : : /** a term canonizer */ 137 : : expr::TermCanonize d_termCanon; 138 : : /** the database of quantified formulas registered to this class */ 139 : : AlphaEquivalenceDb d_aedb; 140 : : /** An eager proof generator storing alpha equivalence proofs.*/ 141 : : std::unique_ptr<EagerProofGenerator> d_pfAlpha; 142 : : /** Are proofs enabled for this object? */ 143 : : bool isProofEnabled() const; 144 : : }; 145 : : 146 : : } // namespace quantifiers 147 : : } // namespace theory 148 : : } // namespace cvc5::internal 149 : : 150 : : #endif