Skip to content

safe.ts: a term at a type with no finite η-long form goes out whole (#1164) - #1896

Merged
nicolas-abril merged 4 commits into
bendlang:mainfrom
nicolas-abril:fix/safe-eta-finite
Oct 29, 2026
Merged

nicolas-abril merged 4 commits into
bendlang:mainfrom
nicolas-abril:fix/safe-eta-finite

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1164 together with #1892 (the kernel half, merged). Alternative to #1815.

Rebased onto main after #1930 and #1945, the simplification passes that make room for it under safe.ts's cap.

What

safe.ts writes every term of a function type η-long, because bend2 converts up to η and the kernel doesn't. Some types have no finite η-long form. One kind names itself again in its unfolding: F = @-x: F -> Empty, H(A) = @-x: H(A) -> A. The other keeps growing: K(n) = @-x: K(1n+n) -> U32. Before this PR, -o and --verdict overflowed the stack on terms like om(f) = f(f), keep(f) = f and run(g, k) = g(k).

eta_finite decides this per call (a type not headed by a ∀):

  • The rule: a call goes whole if it embeds a call of the same def that it was unfolded from (homeomorphic embedding). A repeat embeds itself.
  • What "embeds" compares: a variable matches any variable, a nat matches any no smaller nat, and any other literal matches a literal of its kind. It remembers each pair of nodes it has compared, so a comparison costs at most the product of the two terms' sizes.
  • Computed arguments: a call is compared at its arguments' data: constructors computed down, a nat as its literal, a datatype at its head. bend2 checks a type position dead, with no descent check, so Dn(Nat.sub(n, 1n)) is accepted; compared as written, Dn(Nat.sub(3n, 1n)) would look like it grew out of Dn(3n). If computing an argument never ends, -o hangs, as bend2 would.
  • Why the walk ends: every path that never repeats eventually reaches such a call.
  • Every other call goes η-long, but only after the calls it unfolds to are decided. So an η-long call never leads back to itself.
  • Each call is walked once: e.fin keeps each verdict, a ∀'s too, so the walk is linear in the number of distinct calls. e.keys keeps each type object's key by depth, so a repeated question skips the lowering.
  • Keys: a call is keyed by the variables it binds, with every other variable written the same way. So when the emitter later asks about a type under its own binders, it lands on the same calls.

At a type that goes whole, two things now go out whole instead of η-long: a tree leaf outside a Σ chain with no column left to take, and a call's argument.

Exported now:

keep : ∀x0 : ∀-x0 : F -> Empty -> ∀-x1 : F -> Empty =
  λx0 => λ-x1 => (x0 -x1)
keep_k : ∀x0 : (K (.Zero, ())) -> (K (.Zero, ())) =
  λx0 => λ-x1 => (x0 -x1)

The first call of a recurring or growing def still gets one η level (keep, keep_k, keep_h3), and the call it unfolds to goes whole. In a cycle through two defs (MA ↔ MB), the one asked about first goes whole.

Cost

Two kinds of equation now fail --verdict because the kernel converts without η:

  • An η equation at a type that recurs, like {f == (x => f(x)) : F}. Before this PR it overflowed. Stop recursive eta expansion in --verdict #1815 rejects it too.
  • An η equation that needs an η level past a finite count-up, like D(0n) → D(1n) → D(2n) → U32. Any rule that always stops must cut some count-ups that happen to be finite.

At other finite types, η equations still check. That includes V(A, 1n+p) = A -> V(Two(A), p) and C(i, 1n+p) = U32 -> C(1n+i, p), where one argument grows while another shrinks.

Checks

  • tests/check/eta_recursive_type.bend covers:

    • om, keep, run, keep_h, keep_h3 and keep_k
    • id : F -> F and id : H(A) -> H(A)
    • η equations at Endo(Endo(U32)), Fun(2n), a finite alias, W(z, z => z), P = (U32 -> U32) -> F, the written-out @-x: F -> Empty, V and C
    • the mutual growth MA/MB
    • keep_r over Dbl(24n) (Dbl(1n+p) = Dbl(p) -> Dbl(p)), which takes 0.5 s; a walk that repeats shared subtrees takes over 60 s and would hit the gate's 30 s limit

    gates/test.ts only runs --checkup, so gates/safe.ts is what exercises this.

  • --verdict on all 1792 files of tests/ against the previous head (a9c5177), run locally with the same kernel: the same verdict on every file.

  • Timing with -o (main in parentheses): Fun(4000n) 0.6 s, Dbl(20n) 0.06 s, C(0n, 300n) 0.18 s (0.07 s), V(U32, 16n) 2.1 s (0.58 s), V(U32, 18n) 8.6 s (2.2 s). V's η-long output grows 4× per two levels, on main too. Before memoising embeds, C(0n, 22n) took 16.7 s and V(U32, 16n) 17 s.

  • The test also covers Dn(3n), a count-down through Nat.sub, and C(0n, 60n), which the version before memoising did not finish in 120 s.

  • bun gates/safe.ts tests: 1796 files, agree 1144, unsafe 142, bend2 rejects 507, out of scope 3, false reject 0, timeout 0, infrastructure 0.

  • bun gates/_run.ts: repo 54 / 54, test 1783 / 1783, perf 123 / 124 (pass), ping 80 / 81 (pass). safe.ts is at 23900 / 24000 ttok (23991 with fix(safe): each kernel build goes in a dir of its own, renamed into place whole #1949).

  • Not run: the hub corpus.

Not fixed here: Sink = U32 -> Sink still hangs bend2's own --check-only (#1164).

🤖 Generated with Claude Code

@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Known cost: eta_finite is quadratic when a type's call arguments grow at each level

e.fin makes the walk visit each distinct call once, but each key is a model_key string whose size is the size of the call's arguments. So the cost is the sum of the key sizes along the path. That's linear for regular recursion (F, H(A), Fun(n)) and quadratic when the arguments grow at every level.

Measured:

  • Infinite non-regular recursion (H2(A) = @-x: H2(Two(A)) -> A) fails either way. main overflows in 0.5 s and this branch overflows after ~110 s, so only the time to fail changes.
  • Finite with growing arguments (Fun2(n, A) = A -> Fun2(n-1, Two(A))): the η-long form itself is exponential on main and dominates the cost. n=15 takes 0.5 s on main and 0.8 s here; n=20 takes 25 s on main and 31 s here.

Possible fix, left for later: hash-consed structural ids. Each node gets a short id, cached by object identity and built from its children's ids, so equal types share an id and each level costs O(1). This needs code for every term form and for the variables bound inside a type, and safe.ts is 15 tokens under its cap, so it would need another simplification round.

Identity-only keys don't work. They would make H2 fail as fast as on main, but the e.fin memo would miss: term_wnf rebuilds literals (Fun(2n) unfolds to a new 1n), so Fun(n) would go quadratic again.

🤖 Generated with Claude Code

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

Ran this merged onto main 0bdeb78. repo 54/54, ping 81/81, test 1774/1774, perf 124/124. safe with #1892's kernel: 1144 agree, 0 false reject. With main's kernel, eta_recursive_type aborts (SIGABRT), as the description says.

1. A proof that passes on main now fails --verdict

eta_finite starts its walk at d = 0, not at the scope's depth. model_key then can't tell the scope's variables from the ones a type binds inside itself, so two different types get the same key and a finite type is read as infinite:

import Base

def Sel(n: Nat, A: Type) -> Type:
  match n:
    case 0n: U32
    case 1n+p: A

def W(a: Nat, f: Nat -> Nat) -> Type:
  Sel(f(1n), @-x: W(a, z => a) -> U32)

def t(z: Nat, g: W(z, z => z)) -> {g == (y => g(y)) : W(z, z => z)}:
  {==}

W(z, z => z) and the W(z, z' => z) inside it both key as W(^0, λ^0.^0), so g goes out whole. --verdict prints SOME PROOFS FAIL / In t: expected: x1 observed: λ-x2 => (x1 -x2) with both kernels. On main it's ALL PROOFS CHECK. The same collision lets an e.fin entry from one scope answer for a different type in another.

Passing the depth at both call sites fixes it; with that, this file and eta_recursive_type both check with #1892's kernel:

eta_finite(e, T!, undefined, s.d)   // tree, line 778
eta_finite(e, A, undefined, s.d)    // arg_term, line 1349

Could you add it to eta_recursive_type?

2. The test passes without the fix

eta_recursive_type expects only ALL PROOFS CHECK, which main already prints, so gates/test.ts passes with or without eta_finite. Only gates/safe.ts covers the change, and on main's kernel the file is an infrastructure error until #1892 is in. So this has to merge after #1892. Worth saying so in the description.

3. Two comments still say every function term goes η-long

  • tree (line 775): "a leaf of a function type goes η-long (see the header)"
  • arg_term (line 1341): "Any other argument of a function type goes η-long at A"

Arguments and leaves at a type with no finite η-long form now go out whole, and only the file header says so. One clause in each comment would cover it.

@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Thanks, all three addressed in 25ba706.

1. eta_finite now takes the depth as a required argument, eta_finite(e, T, d), and both call sites pass s.d. Your W case is in eta_recursive_type as same_w. With #1892's kernel it fails --verdict on the previous head (expected: x1 / observed: λ-x2 => (x1 -x2)) and checks now. On the shared e.fin: the keys now keep scope variables as plain levels below d and only rename the type's own binders. So two keys can only collide if the types are the same term, and then the walk does the same thing for both, because it never looks at a variable's type.

2. The description now says to merge after #1892, and that only gates/safe.ts covers the change, because gates/test.ts passes either way.

3. Both comments now say "η-long if finite" (tree, arg_term).

Checks: repo 54/54 (safe.ts 23995/24000), test 1751/1751, safe.ts tests with #1892's kernel: 1135 agree, 0 false reject, 0 t/e, the same table as the previous head. I didn't rerun perf, ping or the hub.

🤖 Generated with Claude Code

@YidaWeng YidaWeng left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review

Solid alternative to #1815 for #1164. Using applied-type model_key (not bare def names) for the η-cycle check is the important design choice: it catches F, H(A), and G, while still η-expanding Endo(Endo(U32)) and Fun(n). Memoizing finite keys in e.fin keeps deep finite types linear, which matches the measured story.

Correctness checks I care about

  • At infinite types: leaves outside a Σ chain / with no specialized column go out whole; inside a chain or with a column, they still get a λ. Arguments whose binders differ from the domain still get one η level. That split looks right relative to the kernel’s no-η conversion.
  • Fixture eta_recursive_type.bend: covers om / keep / run / keep_h, finite η equations, and the W(z, z => z) binder-scope case that would fool a naive key. Good regression set for the name-based false positive you already hit.
  • Documented cost: η-only equations at a recursive type fail --verdict instead of overflowing — same class of outcome as #1815, and correctly called out. Dependency on #1892’s kernel for the new fixture’s --verdict is also clear (and #1892 is now on main).

Follow-ups (non-blocking)

  • Author already noted quadratic cost when call arguments grow each level (H2 / Fun2); hash-consed structural ids are the real fix and need another simplification pass under the 24k cap (file is ~23,995 here). Fine to leave for later.
  • Sink = U32 -> Sink still hanging --check-only remains out of scope, as stated.

Verdict

Looks mergeable for the stated #1164 elaborator half. The applied-key cycle test + e.fin memo are the right shape; the remaining costs are documented and acceptable.

@YidaWeng YidaWeng left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving the elaborator half of #1164: eta_finite via applied model_key + e.fin is the right design vs #1815, the fixture covers the tricky cases, and the documented costs are acceptable.

nicolas-abril and others added 4 commits October 29, 2026 19:26
…endlang#1164)

safe.ts writes every term of a function type η-long, because bend2
converts up to η and the kernel doesn't. A type whose unfolding names
itself again (F = @-x: F -> Empty, H(A) = @-x: H(A) -> A) or keeps
growing (K(n) = @-x: K(1n+n) -> U32) has no finite η-long form, so -o and
--verdict overflowed the stack on om(f) = f(f), keep(f) = f and the like.

eta_finite decides each call (a type not headed by a ∀, keyed by the
variables it binds, every other variable alike, so later questions land
on the same calls): it goes whole when it homeomorphically embeds a call
of its def it unfolds from (a repeat embeds itself, and every path that
never repeats reaches one, so the walk ends), else η-long once the calls
it unfolds to are decided, so η-long calls never lead back to
themselves. e.fin keeps each verdict, so each call is walked once. At a
whole type, a tree leaf outside a Σ chain with no column left, and a
call's argument, go out whole.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ating it; tighter comments

- each call's ancestors go to its ∀s as a new array, so no length bookkeeping
- a call's verdict is true unless a nested call already found it whole
- Call is inlined; low_kids and low_size are kids and size
- eta_finite's comment says the same in fewer words, without repeating fin's

Same -o output on all 1795 files of tests/; Fun(4000n) 0.6 s, Dbl(20n) 0.06 s, as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s embeds and ∀s, and keeps each type's key

bend2 checks a type position dead, with no descent check, so a count-down
written Dn(Nat.sub(n, 1n)) is accepted; compared as written, Dn(3n) seemed to
grow into Dn(Nat.sub(3n, 1n)) and was cut after one η level, and --verdict
failed on {f == (x => y => f(x, y)) : Dn(3n)}. A call's arguments now go in
computed: constructors down, a nat as its literal, a datatype at its head.

embeds tried every dive before the matching node and remembered nothing, so
it grew exponentially with depth: C(0n, 22n) took 16.7 s, V(U32, 16n) 17 s.
It now keeps each pair's answer. A ∀ is keyed and cached like a call (its
walk, once done, decides every call under it), and e.keys keeps each type
object's key by depth, so a repeated question skips the lowering.

-o, before -> after (main): C(0n, 300n) >50 s -> 0.18 s (0.07 s);
V(U32, 16n) 16.8 s -> 2.1 s (0.58 s); V(U32, 18n) 8.6 s (2.2 s); Fun(4000n)
and Dbl(20n) unchanged. tests/check/eta_recursive_type.bend gains Dn(3n) and
C(0n, 60n), which the previous head did not finish in 120 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
As bendlang#1945 did for the rest of the file: eta_finite's and datum's comments
say the same in fewer words, size's keeps only that a nat counts as its
value, embeds' drops what seen does, and eta's (λy => t(y)) went.

Comments only; 23900 ttok.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril
nicolas-abril merged commit 9cd5c31 into bendlang:main Oct 29, 2026
1 check passed
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.

A zero-argument recursive type definition checks, but --verdict and -o stack-overflow elaborating it

3 participants