nustack
ReferenceNutree

rewrite

Generic tree rewrites -- structural Term-to-Term operations.

Module nu.tree.rewrite.

Generic tree rewrites -- structural Term-to-Term operations.

Transforms are Nu -> Nu functions that change tree shape. All operations are non-mutating (return new trees) and domain-free -- they touch only ._children and ._with_children, so they apply to any Term tree. Child reconstruction goes through with_children, so there is no type dispatch.

NameCallMeaning
applytree.apply(root)Apply transforms in order to root.
composetree.compose()Compose transforms left-to-right.
conditional_wraptree.conditional_wrap(root, pred, wrapper)Wrap each matching child, bottom-up.
grafttree.graft(root, target, subtree)Replace target node with subtree (identity comparison).
map_childrentree.map_children(node, fn)Apply fn to each direct child, reconstruct via with_children.
map_nodestree.map_nodes(root, fn, order='bottom_up')Apply fn to every node in the tree.
prunetree.prune(root, pred)Remove subtrees matching pred. Returns None if root matches.
replacetree.replace(root, pred, replacement)Replace nodes matching pred with replacement(node). Bottom-up.
unwraptree.unwrap(root, pred)Remove single-child wrapper nodes matching pred, splicing child up.
wraptree.wrap(root, pred, wrapper)Wrap nodes matching pred: node -> wrapper(node). Bottom-up.

apply

Apply transforms in order to root.

tree.apply(root)

Path nu.tree.apply. Defined on nu.tree.rewrite, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
rootNu

Undocumented: example.

compose

Compose transforms left-to-right.

tree.compose()

Path nu.tree.compose. Defined on nu.tree.rewrite, bound as a function. Builds Transform.

compose(f, g)(x) == g(f(x)).

Undocumented: example.

conditional_wrap

Wrap each matching child, bottom-up.

tree.conditional_wrap(root, pred, wrapper)

Path nu.tree.conditional_wrap. Defined on nu.tree.rewrite, bound as a function. Builds Nu.

At each node, matching children are wrapped individually via wrapper(child). Non-matching children are recursed into.

Matching children are not recursed into -- they are claimed whole by the nearest non-matching ancestor, giving the biggest matching subtree at each level.

Arguments

NameTypeDefaultMeaning
rootNu
predCallable[[Nu], bool]
wrapperCallable[[Nu], Nu]

Undocumented: example.

graft

Replace target node with subtree (identity comparison).

tree.graft(root, target, subtree)

Path nu.tree.graft. Defined on nu.tree.rewrite, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
rootNu
targetNu
subtreeNu

Undocumented: example.

map_children

Apply fn to each direct child, reconstruct via with_children.

tree.map_children(node, fn)

Path nu.tree.map_children. Defined on nu.tree.rewrite, bound as a function. Builds Nu.

Shallow (one level). For deep transforms, use map_nodes.

Arguments

NameTypeDefaultMeaning
nodeNu
fnCallable[[Nu], Nu]

Undocumented: example.

map_nodes

Apply fn to every node in the tree.

tree.map_nodes(root, fn, order='bottom_up')

Path nu.tree.map_nodes. Defined on nu.tree.rewrite, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
rootNuTree root.
fnCallable[[Nu], Nu]Function applied to each node.
orderLiteral['bottom_up', 'top_down']'bottom_up'"bottom_up" (default) transforms children first, "top_down" transforms parent first.

Undocumented: example.

prune

Remove subtrees matching pred. Returns None if root matches.

tree.prune(root, pred)

Path nu.tree.prune. Defined on nu.tree.rewrite, bound as a function. Builds Nu | None.

Preserves unchanged subtrees by identity.

Arguments

NameTypeDefaultMeaning
rootNu
predCallable[[Nu], bool]

Undocumented: example.

replace

Replace nodes matching pred with replacement(node). Bottom-up.

tree.replace(root, pred, replacement)

Path nu.tree.replace. Defined on nu.tree.rewrite, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
rootNu
predCallable[[Nu], bool]
replacementCallable[[Nu], Nu]

Undocumented: example.

unwrap

Remove single-child wrapper nodes matching pred, splicing child up.

tree.unwrap(root, pred)

Path nu.tree.unwrap. Defined on nu.tree.rewrite, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
rootNu
predCallable[[Nu], bool]

Undocumented: example.

wrap

Wrap nodes matching pred: node -> wrapper(node). Bottom-up.

tree.wrap(root, pred, wrapper)

Path nu.tree.wrap. Defined on nu.tree.rewrite, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
rootNu
predCallable[[Nu], bool]
wrapperCallable[[Nu], Nu]

Undocumented: example.

On this page