Repository navigation
refactor(comp): arm_self walks its arm itself, and term_map is gone - #2108
Conversation
Lorenzobattistela
left a comment
There was a problem hiding this comment.
Requesting changes: one blocker, found in review. The gates are all green on this PR merged onto main bc13694 (repo 56/56, test 1852/1852, perf 124/124, ping 83/83, safe: 0 false rejects), but no existing test has the shape below.
C builds crash when an arm holds an erased lambda and rebuilds the scrutinee
A Lam or Let rebuilt by arm_self keeps the original s.f, so its HOAS body has no rewrite. When arm_self strips the field lambdas (the [b, 0] return), that body still refers to the field probes, which are no longer bound. Two emitter paths call .f(...) directly instead of term_open: emit_body's erased-Lam case (x.f(t)) and emit_expr (x.f(DUMMY)). Both get the unrewritten body, and bind_pop fails:
def f(n: Nat) -> @-A: U32 -> Nat:
match n:
case 0n:
a => 0n
case 1n+p:
a => 1n+p
def main() -> IO(Unit):
IO.print(Nat.show(f(5n, 3)))
bend f.bend -o f.bin: main prints 5. This PR fails with TypeError: undefined is not an object (evaluating 'b.n').
Other shapes that pass on main and fail here:
- a Char default
a => c - a U32 default
a => x +nwitha => Nat.add(p, 1n+p)(the field is kept, but the use counts no longer match)+xwitha => U32.add(x, x)konst(a => 1n+p)inside an arm
JS output is unchanged; only C breaks.
Needed for merge
-
Give the rebuilt binder a lazy fallback, so a direct
.fcall still sees the rewrite. Nothing on the hot path calls it since #2107, so the cost stays at zero:const l = (lam ? Bend.Lam(s.k, s.i, (y: HTerm) => go(s.f(y), false), s.s, s.q) : Bend.Let(s.k, s.i, vs, (ys: HTerm[]) => go(s.f(ys), false), s.s, s.q)) as Of<"Lam"> | Of<"Let">;With this, every probe above compiles to C and prints what main prints. (Changing the two emitter paths to use
term_openwould also work, but the next direct.fcaller would hit the same bug.term_lowerandterm_samealready call.fon Let and Lam.)
Please re-check the kernel numbers with the fallback in place. -
Fix the comment "a binder keeps its own body as the same value, unrewritten". Once the fields are stripped, the original body is not the same value: it refers to binders that no longer exist.
-
Add a test (tests/reg or similar) with the snippet above, built to C, ending in
#| 5.
Optional, and already the case on main: back(ps) is rebuilt and term_samed for every non-constant Ctr visited, which for a U32 match is a chain of 32+ Ctrs per visit. Building it once when ps reaches n would avoid that.
5b8a60b to
0ac34ee
Compare
term_map was a generic rewrite with one caller. It hid a recursive pass behind a callback, and each binder it rebuilt re-ran the rewrite lazily every time its body was instantiated. arm_self now walks the arm directly, in one pass; a subterm shared in several places is walked at each. A rebuilt Lam or Let is pre-opened with its rewritten body, as let_open does, and keeps its original body as its HOAS function: the same value, without the rewrite. Co-Authored-By: Claude Opus 5.5 <[email protected]>
0ac34ee to
e37b0bd
Compare
…able A Lam or Let that arm_self rebuilt kept its original HOAS function, so a caller that applied it to its own variable got the body before the rewrite. The C emitter opens an erased lambda that way (emit_body, emit_expr, anf): when arm_self had dropped the fields, that body still named them and the build died in bind_pop. The function now reruns the rewrite on its body; term_open still returns the pre-opened one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
e37b0bd to
be3503b
Compare
Shares integration's lt_any and FL.made; brings back term_map (dropped by bendlang#2108) for def_fuse; ctr_flds takes the constructor; fun_of fuses the body closure-elim's fold (FL.cfh) left, or the def's own. Co-Authored-By: Claude Opus 5.5 <[email protected]>
); wrap a line def_fuse rewrites a body through term_map, which main removed with its other caller (arm_self). The fusion branch carried its own copy next to def_fuse; this is that copy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
term_mapwas a generic term rewrite with a single caller,arm_self. It hid a recursive pass behind a callback, and every Lam/Let it rebuilt got a lazy body ((y) => go(s.f(y))) that re-ran the whole rewrite each time the body was instantiated. That laziness is part of what made #2076's kernel build slow.arm_selfnow walks the arm itself, in one pass:Anns, are opened and recorded as before. ACtrthat rebuilds the scrutinee becomesself, and only the path to a rewrite is rebuilt.FL.opens, aslet_opendoes, soterm_opennever re-runs the rewrite.emit_body,emit_expr,anf). Whenarm_selfhad dropped the fields, the body still named them, and the build died inbind_pop.tests/reg/arm_self_erased_lam.bendcovers it. Across every test, demo and bench, that function is applied only in the new test (8 times).Checked, on
main(bc13694) + this change:gates/test.ts: 1853/1853.gates/repo.ts: 56/56.main: 65,963).13in the interpreter, under JS and under C. Without the second commit, the C build throwsundefined is not an object (evaluating 'b.n').spike/kernel.bend(bun 1.3.14), three runs each: the C is byte-identical tomain's, about 5.3 s and 2.8 GB on both. A per-arm cache ofback(ps)made no difference there either, so it is left out.🤖 Generated with Claude Code