Skip to content

BendTT: a comparison is done when both sides are already the same, and first runs with every def rigid - #1892

Merged
Lorenzobattistela merged 3 commits into
bendlang:mainfrom
nicolas-abril:fix/kernel-fits-same
Oct 28, 2026
Merged

Lorenzobattistela merged 3 commits into
bendlang:mainfrom
nicolas-abril:fix/kernel-fits-same

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Collaborator

Part of #1164 (the kernel half; the elaborator half is a separate PR). Alternative to #1815's kernel change.

What

Term.fits unfolded both sides to their weak heads before comparing, with no equality check. A type def whose unfolding names itself, like F = ∀-x:F -> <>, therefore never finished comparing to itself: F against F unfolds to the same ∀, compares the domains, F against F again, until the stack overflows. Even id : ∀x0 : F -> F = λx0 => x0 overflows on main. Term.conv doesn't have this problem because it checks a = b first; fits didn't.

This brings the kernel's comparison closer to bend2's term_compare:

  • fits checks U = T before unfolding, as conv already did with a = b. When both sides unfold to the same term, the comparison one level down starts with the same check, so there is no second check after unfolding.
  • Term.rigid f runs a comparison first against Book.nil (the empty book, where no def unfolds), then against the real book, each pass from the same fuel (so a comparison can now spend up to 2 × FUEL). It's used where the checker starts a comparison (Ctx.fit, and the Rfl check of an equation), as bend2 runs its rigid pass at the top of term_compare.

The new check only turns a failure into a success, and the full pass starts from the same fuel, so --verdict can't reject anything main accepted.

Compared with #1815's sameRef shortcut, the U = T check also covers applied recursive types with identical arguments: for H(A) = ∀-x:H(A) -> A, λx => x : H(x0) -> H(x0) still overflows #1815's kernel and checks here. Arguments that differ only by unfolding (H(U1) vs H(U2) with U2 = U1) still overflow; that needs comparing arguments before unfolding, left for a follow-up.

Proofs

  • The new equality branch in fits_sound is crefl.
  • sees_nil: Book.nil is seen by any book.
  • rigid_sound reuses fits_sound and conv_sound unchanged for both passes.

lean bend2/bendtt.lean: no errors, no sorry, the same 62 warnings as main.

Checks

Hand-written .bendtt files on the built kernel (main overflows on all the accepted ones):

file result
om : ∀x0 : ∀-x0:F -> <> -> <> = λx0 => (x0 -x0), and its η-long form checks
keep, run from #1164, η-long and not checks
id : F -> F, id : F -> ∀-x0:F -> <> checks
id : ∀-x0:*1 -> ∀x1:(H x0) -> (H x0) = λ-x0 => λx1 => x1 checks
boom : <> = (om om) rejected (quantity)
bad2 : ∀x0:F -> ∀x1:F -> <> = λx0 => x0 rejected (quantity)
F against a separate but identical-looking G = ∀-x0:G -> <> still overflows (needs comparing infinite unfoldings; out of scope)

--verdict over all 1763 files in tests/, main's kernel vs this one (both with main's elaborator): identical output on every file (1134 check, 629 fail), total time 465 s vs 461 s, no file more than 2 s slower. On the cluster, merged onto main fad0bb9 (87f93dd): repo 54/54, test 1771/1771, ping 81/81, perf passes (it doesn't run the kernel), safe 1143 agree / 0 false reject. The hub corpus wasn't run.

No .bend test here: on main, every .bend file with a recursive function type overflows in safe.ts's η-long elaboration before reaching the kernel. The #1164 examples become --verdict tests in the elaborator PR, which needs this one to pass them.

bun gates/repo.ts: 54 / 54.

🤖 Generated with Claude Code

…d first runs with every def rigid, as bend2's term_compare

fits unfolded both sides before comparing, with no equality check, so a
type def whose unfolding names itself (F = ∀-x:F -> <>) never finished
comparing to itself: F against F unfolded to the same ∀ and compared the
domains, F against F again, until the stack overflowed. Even
id : F -> F = λx => x failed this way.

fits now checks U = T before unfolding and again after, and conv checks
a = b after unfolding too (it already did before). Ctx.fit and the Rfl
check run the comparison first against an empty book, where no def
unfolds, then against the real one, each from the same fuel, the way
bend2's term_compare runs a rigid pass first. Each check only turns a
failure into a success, so --verdict rejects nothing it accepted.

Proofs: the new equality branches are crefl or a join of the two weak
heads; sees_nil (an empty book is seen by any) lets rigid_sound reuse
fits_sound and conv_sound for both passes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

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

Thanks, this looks right to me. The proofs hold, each new branch only turns a failure into a success, and I ran it merged onto main 757180e: repo 54/54, test 1752/1752, perf 124/124, ping 81/81, safe 1135 agree / 0 false reject. The 53 --verdict tests take about the same time on both kernels (6.46 s vs 6.19 s locally).

A few points, none blocking:

  1. The shortcut only fires when the two sides are the same term. An applied recursive type whose arguments differ by one unfolding still overflows on both kernels:

    U1 : *(.Q2) =
      <A>
    
    U2 : *(.Q2) =
      U1
    
    H : ∀-x0 : *(.Q1) -> *(.Q1) =
      λ-x0 => ∀-x1 : (H -x0) -> x0
    
    id : ∀x0 : (H -U1) -> (H -U2) =
      λx0 => x0
    

    fits unfolds H(U1) and H(U2) to ∀s whose domains are H(U2) vs H(U1) again, and those are never equal. The rigid pass doesn't help, because U1 vs U2 compares two different refs. With (H -U1) -> (H -U1) it checks here and overflows on main, so the fix is specific to identical sides. Comparing same-head applications argument by argument before unfolding (lazy delta at each node, as bend2 does) would also cover this. Fine as a follow-up if you'd rather keep this PR small, but the description's "applied recursive types" claim should say it needs identical arguments.

  2. Related and already on main: fuel doesn't bound stack depth, so any comparison that never ends (this one, or the F vs G case you mention) aborts the bendtt process instead of giving a clean "out of fuel" rejection. Not this PR's job, but worth an issue.

  3. Small cleanups:

    • Term.parts's last arm (| a, b => if a = b then some [] else none) can no longer return some [], since conv now checks a = b just before calling it. It could return none instead.
    • The if a = b / if U = T after unfolding repeats a full structural walk even when unfolding took no step.
    • Book.of [] is rebuilt on every Term.rigid call. A named Book.nil, reused by sees_nil, would mirror bend2's RIGID.
  4. Nothing pins id : F -> F today, so a later refactor could drop the U = T check with every gate green. Please make sure the elaborator PR adds the #1164 cases as --verdict tests.

@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Thanks. Points 3 and 4:

3. 87f93dd

  • Removed the equality check after unfolding from both fits and conv, instead of guarding it. Once both sides unfold to the same term, parts reaches the existing equality check one level down. This is also how bend2's compare_go works: an equality check at entry, nothing after wnf.
  • Term.parts keeps the last arm's if a = b then some []. With the conv check gone, that arm can be reached again, and making it none would reject equal leaves after unfolding.
  • Book.nil is now a named definition, used by both Term.rigid and sees_nil, like bend2's RIGID.
  • lean bend2/bendtt.lean gives no errors and no sorry, with the same warnings as before. bendtt.lean is 63,027 tokens, under its 64k cap. Repo gate 54/54. --verdict over all 1,763 files in tests/ gives the same output as 7f0119c on every file.

4. #1896 already adds tests/check/eta_recursive_type.bend (om, keep, run, keep_h). 1175721 on #1896 adds the literal id_f(x: F) -> F and id_h(-A: Type, x: H(A)) -> H(A). With #1896 and this kernel it gives ALL PROOFS CHECK. Main's kernel, and this kernel without the U = T check in fits, both abort on the file, and the safe gate counts that as a failure. This means #1896 has to merge after this PR.

Points 1 and 2 aren't addressed yet. The cluster, safe and hub gates weren't run.

…t Book.nil unfolds no def

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Lorenzobattistela
Lorenzobattistela merged commit b9b1582 into bendlang:main Oct 28, 2026
1 check passed
@nicolas-abril
nicolas-abril deleted the fix/kernel-fits-same branch October 28, 2026 12:59
nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Oct 28, 2026
…endlang#1164)

A type def whose unfolding names itself (F = @-x: F -> Empty, G's domain
G -> U32, H(A) = @-x: H(A) -> A) has an infinite η-long form: η-expanding
a term of it binds a variable of the same type, which η-expands again.
--verdict and -o overflowed the stack writing om(f) = f(f) at F.

eta_finite walks a type's ∀s, domains and codomains, and fails when a def
(also at the head of an application) appears inside its own unfolding. A
tree leaf outside a Σ chain, and a call's argument, go η-long only at a
finite type; at an infinite one the term goes out whole. The kernel needs
bendlang#1892 to compare such types; with it om, keep, run and keep_h check.

An equation bend2 closes by η at such a type, {f == (x => f(x)) : F},
now fails --verdict instead of overflowing.

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