1 /********************* */
2 /*! \file node_algorithm.h
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Andres Noetzli, Haniel Barbosa
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 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.\endverbatim
12 ** \brief Common algorithms on nodes
14 ** This file implements common algorithms applied to nodes, such as checking if
15 ** a node contains a free or a bound variable. This file should generally only
16 ** be included in source files.
19 #include "cvc4_private.h"
21 #ifndef CVC4__EXPR__NODE_ALGORITHM_H
22 #define CVC4__EXPR__NODE_ALGORITHM_H
24 #include <unordered_map>
27 #include "expr/node.h"
28 #include "expr/type_node.h"
34 * Check if the node n has a subterm t.
35 * @param n The node to search in
36 * @param t The subterm to search for
37 * @param strict If true, a term is not considered to be a subterm of itself
38 * @return true iff t is a subterm in n
40 bool hasSubterm(TNode n
, TNode t
, bool strict
= false);
43 * Check if the node n has >1 occurrences of a subterm t.
45 bool hasSubtermMulti(TNode n
, TNode t
);
48 * @param k The kind of node to check
49 * @param n The node to search in.
50 * @return true iff there is a term in n that has kind k
52 bool hasSubtermKind(Kind k
, Node n
);
55 * Check if the node n has a subterm that occurs in t.
56 * @param n The node to search in
57 * @param t The set of subterms to search for
58 * @param strict If true, a term is not considered to be a subterm of itself
59 * @return true iff there is a term in t that is a subterm in n
61 bool hasSubterm(TNode n
, const std::vector
<Node
>& t
, bool strict
= false);
64 * Returns true iff the node n contains a bound variable, that is a node of
65 * kind BOUND_VARIABLE. This bound variable may or may not be free.
66 * @param n The node under investigation
67 * @return true iff this node contains a bound variable
69 bool hasBoundVar(TNode n
);
72 * Returns true iff the node n contains a free variable, that is, a node
73 * of kind BOUND_VARIABLE that is not bound in n.
74 * @param n The node under investigation
75 * @return true iff this node contains a free variable.
77 bool hasFreeVar(TNode n
);
80 * Returns true iff the node n contains a closure, that is, a node
81 * whose kind is FORALL, EXISTS, CHOICE, LAMBDA, or any other closure currently
83 * @param n The node under investigation
84 * @return true iff this node contains a closure.
86 bool hasClosure(Node n
);
89 * Get the free variables in n, that is, the subterms of n of kind
90 * BOUND_VARIABLE that are not bound in n, adds these to fvs.
91 * @param n The node under investigation
92 * @param fvs The set which free variables are added to
93 * @param computeFv If this flag is false, then we only return true/false and
95 * @return true iff this node contains a free variable.
97 bool getFreeVariables(TNode n
,
98 std::unordered_set
<Node
, NodeHashFunction
>& fvs
,
99 bool computeFv
= true);
102 * Get all variables in n.
103 * @param n The node under investigation
104 * @param vs The set which free variables are added to
105 * @return true iff this node contains a free variable.
107 bool getVariables(TNode n
, std::unordered_set
<TNode
, TNodeHashFunction
>& vs
);
110 * For term n, this function collects the symbols that occur as a subterms
111 * of n. A symbol is a variable that does not have kind BOUND_VARIABLE.
112 * @param n The node under investigation
113 * @param syms The set which the symbols of n are added to
115 void getSymbols(TNode n
, std::unordered_set
<Node
, NodeHashFunction
>& syms
);
118 * For term n, this function collects the symbols that occur as a subterms
119 * of n. A symbol is a variable that does not have kind BOUND_VARIABLE.
120 * @param n The node under investigation
121 * @param syms The set which the symbols of n are added to
122 * @param visited A cache to be used for visited nodes.
124 void getSymbols(TNode n
,
125 std::unordered_set
<Node
, NodeHashFunction
>& syms
,
126 std::unordered_set
<TNode
, TNodeHashFunction
>& visited
);
129 * For term n, this function collects the operators that occur in n.
130 * @param n The node under investigation
131 * @param ops The map (from each type to operators of that type) which the
132 * operators of n are added to
134 void getOperatorsMap(
136 std::map
<TypeNode
, std::unordered_set
<Node
, NodeHashFunction
>>& ops
);
139 * For term n, this function collects the operators that occur in n.
140 * @param n The node under investigation
141 * @param ops The map (from each type to operators of that type) which the
142 * operators of n are added to
143 * @param visited A cache to be used for visited nodes.
145 void getOperatorsMap(
147 std::map
<TypeNode
, std::unordered_set
<Node
, NodeHashFunction
>>& ops
,
148 std::unordered_set
<TNode
, TNodeHashFunction
>& visited
);
151 * Substitution of Nodes in a capture avoiding way.
152 * If x occurs free in n and it is substituted by a term t
153 * and t includes some variable y that is bound in n,
154 * then using alpha conversion y is replaced with a fresh bound variable
155 * before the substitution.
158 Node
substituteCaptureAvoiding(TNode n
, Node src
, Node dest
);
161 * Same as substituteCaptureAvoiding above, but with a
162 * simultaneous substitution of a vector of variables.
163 * Elements in source will be replaced by their corresponding element in dest.
164 * Both vectors should have the same size.
166 Node
substituteCaptureAvoiding(TNode n
,
167 std::vector
<Node
>& src
,
168 std::vector
<Node
>& dest
);
171 * Get component types in type t. This adds all types that are subterms of t
172 * when viewed as a term. For example, if t is (Array T1 T2), then
173 * (Array T1 T2), T1 and T2 are component types of t.
174 * @param t The type node under investigation
175 * @param types The set which component types are added to.
177 void getComponentTypes(
178 TypeNode t
, std::unordered_set
<TypeNode
, TypeNodeHashFunction
>& types
);