Skip to content

Resolve BendTT recursive groups by membership (#1212) - #1215

Merged
Lorenzobattistela merged 7 commits into
bendlang:mainfrom
oxura:fix/verdict-group-membership
Oct 2, 2026
Merged

Lorenzobattistela merged 7 commits into
bendlang:mainfrom
oxura:fix/verdict-group-membership

Conversation

@oxura

@oxura oxura commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1212.

--verdict packs mutually recursive defs into one BendTT def, a group, because the kernel has no mutual recursion. safe.ts found groups up front through refs, which collected every name in a body, including those in erased arguments. A dead forward reference (keep(L(n), n) with keep(-p: Nat, x: Nat)) could therefore form a group, choose its lead, or route a non-member's call through a group's selector. That gave the three faces in the issue: a TypeError crash, a false MISMATCH, and (with the #1131 name collision) a PASS for a book that computes a different value.

Groups are now found by emission itself, which already knows what is live:

  • item_ref emits a live name at once, before its caller, and queues a dead one. A live name that meets an item still going out is a live cycle, exactly the forward reference the kernel would reject, and only there does regroup form (or extend) a group. A dead reference never forms one.
  • The item_ref that began the cycle's outermost member undoes what was emitted since, and its caller redoes the call through the group.
  • A pass in which some mention named a member on its own before its group was found, or which grew a group already found, runs again with the groups known from the start, so every mention of a member goes through its final group whatever the discovery order. Across the tests, 3 files take a second pass.
  • The lead is the member filled last (the law its helpers call back); back gives its shared parameters as before. A cycle that adds no member (one def, or one group, at other specialized arguments) is out of scope. No separate walk decides liveness.

tests/proof/group_dead_forward.bend covers a non-member's erased reference and a def whose first later reference is dead and unrelated. Its laws make main report a mismatch, and this branch checks.

Every existing test elaborates byte-identically under -o (outputs, exit codes and out-of-scope reasons), so base's groups are exactly as before; whole-corpus elaboration time is unchanged. No changes to bend2/bend.ts; no token caps changed.

Gates at 3579b74 (each alone, on the cluster): repo PASS 49/49, test PASS 1527/1527, ping PASS 46/46, perf 110/117 (main: 108/117; this PR touches no runtime or compiler code), safe tests 981 agree / 0 out of scope / 1 false reject (agree_32, as on main).

@oxura

oxura commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

Joint verification of #1215, #1216, #1218, #1219 and #1220 against main commit 0187512:

  • All five independent PR branches merge cleanly in one octopus merge. Documentation hunks were separated and shared wording shortened so the combined guide stays within the original cap.
  • Combined bun gates/repo.ts: PASS 49/49; no caps changed.
  • Real Lean 4.34.0-built BendTT kernel accepts all three new positive proof regressions, unchanged vec_safe_lookup, and the five reported grouping/label/model-search witnesses.
  • Four runtime regressions produce 5n, 6n, 7n, 204 on interpreter, emitted JS and native C. All four C programs were exercised with AddressSanitizer and UndefinedBehaviorSanitizer; no diagnostics.
  • The merged emitted fb3 value passes an added {main == (.False, ()) : Bool} oracle. The corresponding .True oracle fails in the actual kernel, so successful type-checking is not the only evidence of value preservation.
  • Kind-only and empty-datatype unsafe dependencies still reject with exit 1 and the expected dependency sets.
  • The genuinely required unavailable model rejects promptly; -o explicitly reports no model for reflexive~n as out of scope. Search-limit exhaustion remains fail-closed, not an unchecked placeholder.

The cluster-wide corpus gates were not run; this evidence is the local repository gate, actual CLI/compiler runs and actual kernel checks.

@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Thanks for the careful write-up. The crash on g4 is real, and this PR does fix it, but I feel like the new group_of patches a symptom instead of the cause.

The cause. refs (safe.ts:1111) collects every Ref in a body, including those inside erased arguments. Only later and group_new call it, so a dead reference can form a group, pick a group's leader, or reroute a call. The candidate loop in group_of and the group_new in item_emit both work around that. Neither stops dead references from shaping groups.

The new test shows this. Its header says dead references "neither add group members nor select a group's leader". Yet h names L only inside keep's erased -p, and it still ends up a member: L.group gets an .h arm, and the member law relies on that.

I was thinking on something like make grouping count only live references. That is option 3 in #1212. A dead forward reference doesn't need a group: item_ref already emits a dead name lazily, and the kernel accepts a dead forward reference. So only live cycles should form groups, like base's String.cmp / String.cmp.fin. Then later(k)[0] can't be an unrelated def, group_of can go back to its one-liner, and you don't need the candidate loop or the item_emit change.

I'm not completely sure this is the best option, but I think it's a good exploration path.

What has to happen:

  • refs (or a sibling used only for grouping) skips erased argument positions. Those are the quantity-0 args of an App and anything under an erased binder, the same positions args treats as dead.
  • group_of returns to group_new(e, later(e, k)[0] ?? k). If a case remains where k isn't in the group it gets, raise oos(...) there. Don't pick a group silently.

Guide: Please drop the GUIDE.md line. It describes how safe.ts works inside, not the language. Placed after "may call a def written below it", it also reads as if forward calls were allowed in checked code, and they aren't. I think it doesn't need a guide line on the topic.

Test: The regression test doesn't guard the fix as written. Without the fix, bend still prints 5n and the check still accepts, so gates/test.ts passes. gates/safe.ts scores the TypeError as "bend2 rejects", not as a mismatch. Reverting safe.ts turns no gate red. Suggestions:

  • Fix the header comment so it matches the live-only rule.
  • Make the test fail without the fix. One way is a law whose verdict depends on the right group, so the old code MISMATCHes instead of crashing. Or fb3's value divergence, if you can make it work without the BendTT name escaping merges distinct constructor labels across module paths #1131 name collision.
  • Keep g4's shape: a member whose first later reference is dead and points at an unrelated def.

Erased references must not choose recursive groups or shorten their lead.
Use checked argument quantities for membership and back-call traversal,
retain live lambda and datatype arguments, and reject inconsistent groups.
Restore direct group lookup and remove the implementation-only guide line.

Replace the crash-only fixture with a semantic law that produces a real
safe-gate mismatch on main and agrees after the fix. Integrate tested main
57b6c15 without changing human-written bend.ts or token caps.

Verified with the Lean 4.34 kernel, interpreter, JS, C, repo gate 49/49,
independent liveness controls and the 1,538-case local verdict corpus.
One existing conversion case remains resource-limited; cluster gates are
unavailable and existing TypeScript diagnostics are unchanged.
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Thanks, both blockers are fixed, and the aggregate run against the parent commit is exactly the check I'd want.

One thing the io_strerror fallback exposes: file_size.js calls io_sys().mac in its success path, inside the try. Under node that throws, the catch turns it into io_fail(5), and since io_fail no longer crashes, File.size on a perfectly good file now answers Fail (5, "errno 5") with exit 0. Before, it crashed with Cannot find module 'bun:ffi'. I checked: a small open-then-size program prints size 737 under bun and fail 5 errno 5 under node on this branch. The bug was already there, but this PR makes it silent, so please fix it here: const over = process.platform === "darwin" ? 84 : 75; matches what io_sys().mac checks.

Nit: the io_strerror comment names four effects, but 18 go through io_fail. I'd drop the list so it doesn't go stale.

…f, so -o no longer finds no group for it

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Lorenzobattistela
Lorenzobattistela force-pushed the fix/verdict-group-membership branch from 62b0173 to 9baf299 Compare October 2, 2026 14:34
Lorenzobattistela and others added 3 commits October 2, 2026 11:48
…g each field, so a deep literal chain elaborates as fast as on main

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…t, so grouping reads liveness from the emitter alone

A live cycle is found by item_ref, undone and redone through the group;
a pass in which a member was named alone before its group went out runs
again with the groups known. calls, later and the liveness-walking refs
are gone; back again gives the lead.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ope, a group's lead is always its member filled last, and growing a group runs the pass again

Also one Reroute catch in args, the undo mark only where an item goes
out, the template instances built once, and the Safe fields described.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@oxura

oxura commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

The File.size follow-up is now fixed on current main (565d7fd): file_size.js uses process.platform for EOVERFLOW and the strerror comment no longer enumerates effects. I exercised a real File.open -> File.size -> File.close program with a five-byte file under both Bun 1.3.13 and Node 22.23.1; both print size 5 and exit 0, without an FFI shim. The existing file_size_no_ffi fixture also passes its Bun lane. No additional patch is needed for that review point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants