Template Function sequant::opt::extract_subtrees

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) with op_type_ reset to null so the synthetic leaf reads as a primary expression.

pred(n, pl, pr) -> bool is tested in post-order; pl/pr are the predicate values of n’s children (both false if n is a leaf). A node n is captured iff pred(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) = false keeps bare leaves out of the result (and prevents whole-tree root collapse); the per-leaf test is folded into the parent’s check via child_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);