Skip to content

refactor(comp): arm_self walks its arm itself, and term_map is gone - #2108

Merged
nicolas-abril merged 2 commits into
mainfrom
refactor/no-term-map
Nov 10, 2026
Merged

nicolas-abril merged 2 commits into
mainfrom
refactor/no-term-map

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

term_map was 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_self now walks the arm itself, in one pass:

  • The leading field lambdas, through Anns, are opened and recorded as before. A Ctr that rebuilds the scrutinee becomes self, and only the path to a rewrite is rebuilt.
  • A rebuilt Lam or Let is pre-opened with its rewritten body in FL.opens, as let_open does, so term_open never re-runs the rewrite.
  • Its HOAS function re-runs the rewrite on the original body. The first version kept the original body there, unrewritten. The C emitter opens an erased lambda by applying that function (emit_body, emit_expr, anf). When arm_self had dropped the fields, the body still named them, and the build died in bind_pop. tests/reg/arm_self_erased_lam.bend covers 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.
  • comp.ts is 65,938 ttok (main: 65,963).
  • The new test prints 13 in the interpreter, under JS and under C. Without the second commit, the C build throws undefined is not an object (evaluating 'b.n').
  • glam-in-bend's spike/kernel.bend (bun 1.3.14), three runs each: the C is byte-identical to main's, about 5.3 s and 2.8 GB on both. A per-arm cache of back(ps) made no difference there either, so it is left out.

🤖 Generated with Claude Code

@Lorenzobattistela Lorenzobattistela self-assigned this Nov 10, 2026

@Lorenzobattistela Lorenzobattistela left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
  • +n with a => Nat.add(p, 1n+p) (the field is kept, but the use counts no longer match)
  • +x with a => U32.add(x, x)
  • konst(a => 1n+p) inside an arm

JS output is unchanged; only C breaks.

Needed for merge

  1. Give the rebuilt binder a lazy fallback, so a direct .f call 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_open would also work, but the next direct .f caller would hit the same bug. term_lower and term_same already call .f on Let and Lam.)
    Please re-check the kernel numbers with the fallback in place.

  2. 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.

  3. 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.

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]>
@nicolas-abril
nicolas-abril changed the base branch from fix/compile-cache-per-unit to main November 10, 2026 14:43
…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]>
@nicolas-abril
nicolas-abril merged commit d18110b into main Nov 10, 2026
1 check passed
@nicolas-abril
nicolas-abril deleted the refactor/no-term-map branch November 24, 2026 11:44
nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Dec 1, 2026
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]>
nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Dec 1, 2026
); 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants