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