Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * Top contributors (to current version): 3 : : * Andrew Reynolds, Aina Niemetz, Gereon Kremer 4 : : * 5 : : * This file is part of the cvc5 project. 6 : : * 7 : : * Copyright (c) 2009-2025 by the authors listed in the file AUTHORS 8 : : * in the top-level source directory and their institutional affiliations. 9 : : * All rights reserved. See the file COPYING in the top-level source 10 : : * directory for licensing information. 11 : : * **************************************************************************** 12 : : * 13 : : * Simple substitution utility. 14 : : */ 15 : : 16 : : #ifndef CVC5__EXPR__SUBS_H 17 : : #define CVC5__EXPR__SUBS_H 18 : : 19 : : #include <map> 20 : : #include <optional> 21 : : #include <vector> 22 : : 23 : : #include "expr/node.h" 24 : : 25 : : namespace cvc5::internal { 26 : : 27 : : /** 28 : : * Helper substitution class. Stores a substitution in parallel vectors 29 : : * d_vars and d_subs, both of which may be arbitrary terms, having the same 30 : : * types pairwise. 31 : : * 32 : : * Notice this class applies substitutions using Node::substitute. Furthermore, 33 : : * it does not insist that the terms in d_vars are unique. 34 : : */ 35 : : class Subs 36 : : { 37 : : public: 38 : 762886 : Subs() {} 39 : 768301 : virtual ~Subs() {} 40 : : /** Is the substitution empty? */ 41 : : bool empty() const; 42 : : /** The size of the substitution */ 43 : : size_t size() const; 44 : : /** Does the substitution contain v? */ 45 : : bool contains(Node v) const; 46 : : /** Get the substitution for v if it exists, or null otherwise */ 47 : : Node getSubs(Node v) const; 48 : : /** Find the substitution for v, or return std::nullopt */ 49 : : std::optional<Node> find(TNode v) const; 50 : : /** Add v -> k for fresh skolem of the same type as v */ 51 : : void add(Node v); 52 : : /** Add v -> k for fresh skolem of the same type as v for each v in vs */ 53 : : void add(const std::vector<Node>& vs); 54 : : /** Add v -> s to the substitution */ 55 : : void add(const Node& v, const Node& s); 56 : : /** Add vs -> ss to the substitution */ 57 : : void add(const std::vector<Node>& vs, const std::vector<Node>& ss); 58 : : /** Add eq[0] -> eq[1] to the substitution */ 59 : : void addEquality(Node eq); 60 : : /** Append the substitution s to this */ 61 : : void append(Subs& s); 62 : : /** Return the result of this substitution on n */ 63 : : Node apply(const Node& n) const; 64 : : /** Return the result of the reverse of this substitution on n */ 65 : : Node rapply(Node n) const; 66 : : /** Apply this substitution to all nodes in the range of s */ 67 : : void applyToRange(Subs& s) const; 68 : : /** Apply the reverse of this substitution to all nodes in the range of s */ 69 : : void rapplyToRange(Subs& s) const; 70 : : /** Get equality (= v s) where v -> s is the i^th position in the vectors */ 71 : : Node getEquality(size_t i) const; 72 : : /** Convert substitution to map */ 73 : : std::map<Node, Node> toMap() const; 74 : : /** Get string for this substitution */ 75 : : std::string toString() const; 76 : : /** clear the substitution */ 77 : : void clear(); 78 : : /** The data */ 79 : : std::vector<Node> d_vars; 80 : : std::vector<Node> d_subs; 81 : : }; 82 : : 83 : : /** 84 : : * Serializes a given substitution to the given stream. 85 : : * 86 : : * @param out the output stream to use 87 : : * @param s the substitution to output to the stream 88 : : * @return the stream 89 : : */ 90 : : std::ostream& operator<<(std::ostream& out, const Subs& s); 91 : : 92 : : } // namespace cvc5::internal 93 : : 94 : : #endif /* CVC5__EXPR__SUBS_H */