Renamed operator CHOICE to WITNESS (#4207)
[cvc5.git] / src / expr / node_algorithm.h
1 /********************* */
2 /*! \file node_algorithm.h
3 ** \verbatim
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
11 **
12 ** \brief Common algorithms on nodes
13 **
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.
17 **/
18
19 #include "cvc4_private.h"
20
21 #ifndef CVC4__EXPR__NODE_ALGORITHM_H
22 #define CVC4__EXPR__NODE_ALGORITHM_H
23
24 #include <unordered_map>
25 #include <vector>
26
27 #include "expr/node.h"
28 #include "expr/type_node.h"
29
30 namespace CVC4 {
31 namespace expr {
32
33 /**
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
39 */
40 bool hasSubterm(TNode n, TNode t, bool strict = false);
41
42 /**
43 * Check if the node n has >1 occurrences of a subterm t.
44 */
45 bool hasSubtermMulti(TNode n, TNode t);
46
47 /**
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
51 */
52 bool hasSubtermKind(Kind k, Node n);
53
54 /**
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
60 */
61 bool hasSubterm(TNode n, const std::vector<Node>& t, bool strict = false);
62
63 /**
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
68 */
69 bool hasBoundVar(TNode n);
70
71 /**
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.
76 */
77 bool hasFreeVar(TNode n);
78
79 /**
80 * Returns true iff the node n contains a closure, that is, a node
81 * whose kind is FORALL, EXISTS, WITNESS, LAMBDA, or any other closure currently
82 * supported.
83 * @param n The node under investigation
84 * @return true iff this node contains a closure.
85 */
86 bool hasClosure(Node n);
87
88 /**
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
94 * do not add to fvs.
95 * @return true iff this node contains a free variable.
96 */
97 bool getFreeVariables(TNode n,
98 std::unordered_set<Node, NodeHashFunction>& fvs,
99 bool computeFv = true);
100
101 /**
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.
106 */
107 bool getVariables(TNode n, std::unordered_set<TNode, TNodeHashFunction>& vs);
108
109 /**
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
114 */
115 void getSymbols(TNode n, std::unordered_set<Node, NodeHashFunction>& syms);
116
117 /**
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.
123 */
124 void getSymbols(TNode n,
125 std::unordered_set<Node, NodeHashFunction>& syms,
126 std::unordered_set<TNode, TNodeHashFunction>& visited);
127
128 /**
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
133 */
134 void getOperatorsMap(
135 TNode n,
136 std::map<TypeNode, std::unordered_set<Node, NodeHashFunction>>& ops);
137
138 /**
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.
144 */
145 void getOperatorsMap(
146 TNode n,
147 std::map<TypeNode, std::unordered_set<Node, NodeHashFunction>>& ops,
148 std::unordered_set<TNode, TNodeHashFunction>& visited);
149
150 /*
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.
156 *
157 */
158 Node substituteCaptureAvoiding(TNode n, Node src, Node dest);
159
160 /**
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.
165 */
166 Node substituteCaptureAvoiding(TNode n,
167 std::vector<Node>& src,
168 std::vector<Node>& dest);
169
170 /**
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.
176 */
177 void getComponentTypes(
178 TypeNode t, std::unordered_set<TypeNode, TypeNodeHashFunction>& types);
179
180 } // namespace expr
181 } // namespace CVC4
182
183 #endif