From: Andrew Reynolds Date: Fri, 21 May 2021 18:04:49 +0000 (-0500) Subject: Add utility to get all types occurring in a term (#6588) X-Git-Tag: cvc5-1.0.0~1711 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=0b7e50aa1f0f2e6a77fb7e2a1f48b6af8ce5b91d;p=cvc5.git Add utility to get all types occurring in a term (#6588) Required for external proof conversions. --- diff --git a/src/expr/node_algorithm.cpp b/src/expr/node_algorithm.cpp index 19955443a..2220caff9 100644 --- a/src/expr/node_algorithm.cpp +++ b/src/expr/node_algorithm.cpp @@ -647,6 +647,34 @@ Node substituteCaptureAvoiding(TNode n, return visited[n]; } +void getTypes(TNode n, std::unordered_set& types) +{ + std::unordered_set visited; + getTypes(n, types, visited); +} + +void getTypes(TNode n, + std::unordered_set& types, + std::unordered_set& visited) +{ + std::unordered_set::iterator it; + std::vector visit; + TNode cur; + visit.push_back(n); + do + { + cur = visit.back(); + visit.pop_back(); + it = visited.find(cur); + if (it == visited.end()) + { + visited.insert(cur); + types.insert(cur.getType()); + visit.insert(visit.end(), cur.begin(), cur.end()); + } + } while (!visit.empty()); +} + void getComponentTypes(TypeNode t, std::unordered_set& types) { std::vector toProcess; diff --git a/src/expr/node_algorithm.h b/src/expr/node_algorithm.h index b8576f787..72ea03176 100644 --- a/src/expr/node_algorithm.h +++ b/src/expr/node_algorithm.h @@ -198,6 +198,24 @@ Node substituteCaptureAvoiding(TNode n, Node src, Node dest); Node substituteCaptureAvoiding(TNode n, std::vector& src, std::vector& dest); +/** + * Collect all types in n, which adds to types all types for which a subterm + * of n has that type. Operators are not considered in the traversal. + * @param n The node under investigation + * @param types The set of types + */ +void getTypes(TNode n, std::unordered_set& types); + +/** + * Collect all types in n, which adds to types all types for which a subterm + * of n has that type. Operators are not considered in the traversal. + * @param n The node under investigation + * @param types The set of types + * @param visited A cache of nodes we have already visited + */ +void getTypes(TNode n, + std::unordered_set& types, + std::unordered_set& visited); /** * Get component types in type t. This adds all types that are subterms of t