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 : : * Inst match trie class. 11 : : */ 12 : : 13 : : #include "cvc5_private.h" 14 : : 15 : : #ifndef CVC5__THEORY__QUANTIFIERS__INST_MATCH_TRIE_H 16 : : #define CVC5__THEORY__QUANTIFIERS__INST_MATCH_TRIE_H 17 : : 18 : : #include <map> 19 : : 20 : : #include "context/cdlist.h" 21 : : #include "context/cdo.h" 22 : : #include "expr/node.h" 23 : : #include "smt/env_obj.h" 24 : : 25 : : namespace cvc5::internal { 26 : : namespace theory { 27 : : namespace quantifiers { 28 : : 29 : : class QuantifiersState; 30 : : 31 : : /** trie for InstMatch objects 32 : : * 33 : : * This class is used for storing instantiations of a quantified formula q. 34 : : * It is a trie data structure for which entries can be added and removed. 35 : : * This class has interfaces for adding instantiations that are either 36 : : * represented by std::vectors or InstMatch objects (see inst_match.h). 37 : : */ 38 : : class InstMatchTrie 39 : : { 40 : : public: 41 : : /** index ordering */ 42 : : class ImtIndexOrder 43 : : { 44 : : public: 45 : : std::vector<unsigned> d_order; 46 : : }; 47 : : 48 : : public: 49 : 195946 : InstMatchTrie() {} 50 : 195946 : ~InstMatchTrie() {} 51 : : /** exists inst match 52 : : * 53 : : * This method considers the entry given by m, starting at the given index. 54 : : * The domain of m is the bound variables of quantified formula q. 55 : : * It returns true if (the suffix) of m exists in this trie. 56 : : */ 57 : : bool existsInstMatch(Node q, 58 : : const std::vector<Node>& m, 59 : : ImtIndexOrder* imtio = nullptr, 60 : : unsigned index = 0); 61 : : /** add inst match 62 : : * 63 : : * This method adds (the suffix of) m starting at the given index to this 64 : : * trie, and returns true if and only if m did not already occur in this trie. 65 : : * The domain of m is the bound variables of quantified formula q. 66 : : */ 67 : : bool addInstMatch(Node f, 68 : : const std::vector<Node>& m, 69 : : ImtIndexOrder* imtio = nullptr, 70 : : bool onlyExist = false, 71 : : unsigned index = 0); 72 : : /** 73 : : * Adds the instantiations for q into insts. 74 : : */ 75 : : void getInstantiations(Node q, std::vector<std::vector<Node>>& insts) const; 76 : : 77 : : /** clear the data of this class */ 78 : : void clear(); 79 : : /** print this class */ 80 : : void print(std::ostream& out, Node q) const; 81 : : /** the data */ 82 : : std::map<Node, InstMatchTrie> d_data; 83 : : 84 : : private: 85 : : /** Helper for getInstantiations.*/ 86 : : void getInstantiations(Node q, 87 : : std::vector<std::vector<Node>>& insts, 88 : : std::vector<Node>& terms) const; 89 : : /** helper for print 90 : : * terms accumulates the path we are on in the trie. 91 : : */ 92 : : void print(std::ostream& out, Node q, std::vector<TNode>& terms) const; 93 : : }; 94 : : 95 : : /** trie for InstMatch objects 96 : : * 97 : : * This is a context-dependent version of the above class. 98 : : */ 99 : : class CDInstMatchTrie 100 : : { 101 : : public: 102 : 10066 : CDInstMatchTrie(context::Context* c) : d_valid(c, false) {} 103 : : ~CDInstMatchTrie(); 104 : : 105 : : /** exists inst match 106 : : * 107 : : * This method considers the entry given by m, starting at the given index. 108 : : * The domain of m is the bound variables of quantified formula q. 109 : : * It returns true if (the suffix) of m exists in this trie. 110 : : * It additionally takes a context c, for which the entry is valid in. 111 : : */ 112 : : bool existsInstMatch(context::Context* context, 113 : : Node q, 114 : : const std::vector<Node>& m, 115 : : unsigned index = 0); 116 : : /** add inst match 117 : : * 118 : : * This method adds (the suffix of) m starting at the given index to this 119 : : * trie, and returns true if and only if m did not already occur in this trie. 120 : : * The domain of m is the bound variables of quantified formula q. 121 : : * It additionally takes a context c, for which the entry is valid in. 122 : : */ 123 : : bool addInstMatch(context::Context* context, 124 : : Node q, 125 : : const std::vector<Node>& m, 126 : : unsigned index = 0, 127 : : bool onlyExist = false); 128 : : /** 129 : : * Adds the instantiations for q into insts. 130 : : */ 131 : : void getInstantiations(Node q, std::vector<std::vector<Node>>& insts) const; 132 : : 133 : : /** print this class */ 134 : : void print(std::ostream& out, Node q) const; 135 : : 136 : : private: 137 : : /** Helper for getInstantiations.*/ 138 : : void getInstantiations(Node q, 139 : : std::vector<std::vector<Node>>& insts, 140 : : std::vector<Node>& terms) const; 141 : : /** helper for print 142 : : * terms accumulates the path we are on in the trie. 143 : : */ 144 : : void print(std::ostream& out, Node q, std::vector<TNode>& terms) const; 145 : : /** the data */ 146 : : std::map<Node, CDInstMatchTrie*> d_data; 147 : : /** is valid */ 148 : : context::CDO<bool> d_valid; 149 : : }; 150 : : 151 : : /** inst match trie ordered 152 : : * 153 : : * This is an ordered version of the context-independent instantiation match 154 : : * trie. It stores a variable order and a base InstMatchTrie. 155 : : */ 156 : : class InstMatchTrieOrdered 157 : : { 158 : : public: 159 : 0 : InstMatchTrieOrdered(InstMatchTrie::ImtIndexOrder* imtio) : d_imtio(imtio) {} 160 : 0 : ~InstMatchTrieOrdered() {} 161 : : /** get the ordering */ 162 : 0 : InstMatchTrie::ImtIndexOrder* getOrdering() { return d_imtio; } 163 : : /** get the trie data */ 164 : 0 : InstMatchTrie* getTrie() { return &d_imt; } 165 : : /** add match m for quantified formula q 166 : : * 167 : : * This method returns true if the match m was not previously added to this 168 : : * class. 169 : : */ 170 : : bool addInstMatch(Node q, const std::vector<Node>& m); 171 : : /** returns true if this trie contains m 172 : : * 173 : : * This method returns true if the match m exists in this 174 : : * class. 175 : : */ 176 : : bool existsInstMatch(Node q, const std::vector<Node>& m); 177 : : 178 : : private: 179 : : /** the ordering */ 180 : : InstMatchTrie::ImtIndexOrder* d_imtio; 181 : : /** the data of this class */ 182 : : InstMatchTrie d_imt; 183 : : }; 184 : : 185 : : } // namespace quantifiers 186 : : } // namespace theory 187 : : } // namespace cvc5::internal 188 : : 189 : : #endif /* CVC5__THEORY__QUANTIFIERS__INST_MATCH_TRIE_H */