Template Function sequant::opt::extract_subtrees¶
Defined in File extract_subtrees.hpp
Function Documentation¶
-
template<meta::eval_node_range Range, typename Pred>
auto sequant::opt::extract_subtrees(Range &nodes, Pred pred)¶ Extract maximal
pred-matching subtrees from a forest of EvalNodes, replacing each match in place with a leaf carrying a copy of the original payload (expr, hash, canon_indices, phase, connectivity) withop_type_reset to null so the synthetic leaf reads as a primary expression.pred(n, pl, pr) -> boolis tested in post-order;pl/prare the predicate values ofn’s children (bothfalseifnis a leaf). A nodenis captured iffpred(n) ∧ (n is a root ∨ ¬pred(parent(n))). Nested pred-positive descendants of a captured subtree are inlined, not re-extracted.Example — extract intermediates that don’t involve any tensor with label “t”.
pred(leaf) = falsekeeps bare leaves out of the result (and prevents whole-tree root collapse); the per-leaf test is folded into the parent’s check viachild_ok:auto pred = [](EvalNode<EvalExpr> const& n, bool pl, bool pr) -> bool { if (n.leaf()) return false; auto child_ok = [](EvalNode<EvalExpr> const& c, bool pc) { return c.leaf() ? c->is_tensor() && c->as_tensor().label() != L”t” : pc; }; return child_ok(n.left(), pl) && child_ok(n.right(), pr); }; auto intermediates = sequant::opt::extract_subtrees(forest, pred);