Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/v2/00_core.dag
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,27 @@ fn map_children(node: Node, transform: fn(Node) -> Node) -> Node {
}
}

// =========================================================================
// Node catamorphism -- the structural fold over a Node tree.
//
// fold_node is the derived-operation primitive for "compute a value by
// recursing the children spine". Hand-rolled any-descendant / collect /
// search recursions over node.children dissolve onto it (Practice 10).
// Mirrors src/v4/std/node.dag fold_node; v2 Node carries children directly
// as List<Node> with no Edge indirection.
// =========================================================================

type NodeFold<R> {
init: fn(Node) -> R
step: fn(R, Node, R) -> R
}

fn fold_node<R>(n: Node, algebra: NodeFold<R>) -> R {
fold(n.children, init: algebra.init(n), f: (acc, child) =>
algebra.step(acc, child, fold_node(n: child, algebra: algebra))
)
}

// =========================================================================
// Self-call detection -- Node expression walkers
//
Expand All @@ -1316,13 +1337,20 @@ fn map_children(node: Node, transform: fn(Node) -> Node) -> Node {
// is_self_recursive and has_non_tail_self_call on Node.
// =========================================================================

// "Does this call site target fn_name anywhere in the tree" -- a fold_node
// disjunction over the per-node "is this a self-call site" predicate.
// expr_has_non_tail_self_call below stays a hand-written recursion: it is
// genuinely irregular (the call graph is not the data graph -- tail position
// flips per expr kind), so it is not a catamorphism.
fn expr_has_self_call(texpr: Node, fn_name: String, source_indices: Map<String, NewlineIndex>) -> Bool {
match texpr.expr_data {
ExprCall { call_semantics: _, descent_evidence: _ } =>
if expr_call_func_at(texpr: texpr, source_indices: source_indices) == fn_name { true }
else { texpr.children |> any(child => expr_has_self_call(texpr: child, fn_name: fn_name, source_indices: source_indices)) }
_ => texpr.children |> any(child => expr_has_self_call(texpr: child, fn_name: fn_name, source_indices: source_indices))
}
fold_node(n: texpr, algebra: NodeFold {
init: n => match n.expr_data {
ExprCall { call_semantics: _, descent_evidence: _ } =>
expr_call_func_at(texpr: n, source_indices: source_indices) == fn_name
_ => false
},
step: (acc, child, child_result) => acc || child_result
})
}

fn expr_has_non_tail_self_call(texpr: Node, fn_name: String, in_tail: Bool, source_indices: Map<String, NewlineIndex>) -> Bool {
Expand Down
Loading