Repository navigation
safe.ts: a term at a type with no finite η-long form goes out whole (#1164) - #1896
Conversation
|
Known cost:
Measured:
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 Identity-only keys don't work. They would make 🤖 Generated with Claude Code |
Lorenzobattistela
left a comment
There was a problem hiding this comment.
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 1349Could 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.
|
Thanks, all three addressed in 25ba706. 1. 2. The description now says to merge after #1892, and that only 3. Both comments now say "η-long if finite" (tree, arg_term). Checks: repo 54/54 (safe.ts 23995/24000), test 1751/1751, 🤖 Generated with Claude Code |
YidaWeng
left a comment
There was a problem hiding this comment.
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: coversom/keep/run/keep_h, finite η equations, and theW(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
--verdictinstead of overflowing — same class of outcome as #1815, and correctly called out. Dependency on #1892’s kernel for the new fixture’s--verdictis also clear (and #1892 is now onmain).
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 -> Sinkstill hanging--check-onlyremains 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.
25ba706 to
79e4a05
Compare
a304792 to
13292b7
Compare
…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]>
9e89922 to
86b6c09
Compare
Fixes #1164 together with #1892 (the kernel half, merged). Alternative to #1815.
Rebased onto
mainafter #1930 and #1945, the simplification passes that make room for it undersafe.ts's cap.What
safe.tswrites 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,-oand--verdictoverflowed the stack on terms likeom(f) = f(f),keep(f) = fandrun(g, k) = g(k).eta_finitedecides this per call (a type not headed by a ∀):Dn(Nat.sub(n, 1n))is accepted; compared as written,Dn(Nat.sub(3n, 1n))would look like it grew out ofDn(3n). If computing an argument never ends,-ohangs, as bend2 would.e.finkeeps each verdict, a ∀'s too, so the walk is linear in the number of distinct calls.e.keyskeeps each type object's key by depth, so a repeated question skips the lowering.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:
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
--verdictbecause the kernel converts without η:{f == (x => f(x)) : F}. Before this PR it overflowed. Stop recursive eta expansion in --verdict #1815 rejects it too.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)andC(i, 1n+p) = U32 -> C(1n+i, p), where one argument grows while another shrinks.Checks
tests/check/eta_recursive_type.bendcovers:om,keep,run,keep_h,keep_h3andkeep_kid : F -> Fandid : H(A) -> H(A)Endo(Endo(U32)),Fun(2n), a finite alias,W(z, z => z),P = (U32 -> U32) -> F, the written-out@-x: F -> Empty,VandCMA/MBkeep_roverDbl(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 limitgates/test.tsonly runs--checkup, sogates/safe.tsis what exercises this.--verdicton all 1792 files oftests/against the previous head (a9c5177), run locally with the same kernel: the same verdict on every file.Timing with
-o(mainin 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, onmaintoo. Before memoisingembeds,C(0n, 22n)took 16.7 s andV(U32, 16n)17 s.The test also covers
Dn(3n), a count-down throughNat.sub, andC(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.tsis 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 -> Sinkstill hangs bend2's own--check-only(#1164).🤖 Generated with Claude Code