nustack

query

Tree queries -- read-only inspection of Term structures.

Module nu.tree.query.

Tree queries -- read-only inspection of Term structures.

Domain-free: predicates and counts over any Term tree.

NameCallMeaning
childrentree.children(node)The node's direct children, in slot order.
counttree.count(root, pred=None)Count nodes matching predicate. None counts all.
depthtree.depth(root)Maximum depth. A leaf has depth 0.
equaltree.equal(a, b)Structural equality: same kinds, same payloads, same children, recursively.
findtree.find(root, pred)Find all nodes matching predicate (pre-order).
find_firsttree.find_first(root, pred)Find first matching node (pre-order), or None.
payloadtree.payload(node)The node's construction data: what it holds besides its children.
sizetree.size(root)Total number of nodes.

children

The node's direct children, in slot order.

tree.children(node)

Path nu.tree.children. Defined on nu.tree.query, bound as a function. Builds tuple[Nu, ...].

Arguments

NameTypeDefaultMeaning
nodeNu

Undocumented: example.

count

Count nodes matching predicate. None counts all.

tree.count(root, pred=None)

Path nu.tree.count. Defined on nu.tree.query, bound as a function. Builds int.

Arguments

NameTypeDefaultMeaning
rootNu
predCallable[[Nu], bool] | NoneNone

Undocumented: example.

depth

Maximum depth. A leaf has depth 0.

tree.depth(root)

Path nu.tree.depth. Defined on nu.tree.query, bound as a function. Builds int.

Arguments

NameTypeDefaultMeaning
rootNu

Undocumented: example.

equal

Structural equality: same kinds, same payloads, same children, recursively.

tree.equal(a, b)

Path nu.tree.equal. Defined on nu.tree.query, bound as a function. Builds bool.

The explicit spelling of "these two trees are the same program". == on a term builds a comparison term (on a Form) or raises (on a bare term), so it never answers this. Plain values in payloads compare by type and ==; terms anywhere inside them (a body held in a payload, a case table) compare structurally.

Arguments

NameTypeDefaultMeaning
aobject
bobject

Undocumented: example.

find

Find all nodes matching predicate (pre-order).

tree.find(root, pred)

Path nu.tree.find. Defined on nu.tree.query, bound as a function. Builds list[Nu].

Arguments

NameTypeDefaultMeaning
rootNu
predCallable[[Nu], bool]

Undocumented: example.

find_first

Find first matching node (pre-order), or None.

tree.find_first(root, pred)

Path nu.tree.find_first. Defined on nu.tree.query, bound as a function. Builds Nu | None.

Arguments

NameTypeDefaultMeaning
rootNu
predCallable[[Nu], bool]

Undocumented: example.

payload

The node's construction data: what it holds besides its children.

tree.payload(node)

Path nu.tree.payload. Defined on nu.tree.query, bound as a function. Builds Mapping[str, object].

Read-only by contract. A rewrite that keeps a node's payload shares it, so two variants of one node report the same mapping.

Arguments

NameTypeDefaultMeaning
nodeNu

Undocumented: example.

size

Total number of nodes.

tree.size(root)

Path nu.tree.size. Defined on nu.tree.query, bound as a function. Builds int.

Arguments

NameTypeDefaultMeaning
rootNu

Undocumented: example.

On this page