You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
def_inst reuses template specializations across unrelated functions due to un-scoped term_key collisions #1204
term_lower(a) strips context and typing information, lowering terms to LTerm relative to the local telescope depth.
term_key (bend.ts:1192-1194) serializes the stripped AST: JSON.stringify(tm, (k, v) => k === "s" ? undefined : v).
In g(x: Nat) and h(x: Bool), the variable x is lowered to Var("x", 0). Because term_key contains neither the variable's type, the enclosing definition name (lhs.def), nor lexical origin, both template arguments produce the identical JSON string.
book.tmps is a global table keyed only by template name (tm.k, e.g. "app"). Because is[key] was already populated during the check of g, h encounters a cache hit and reuses app~0!
Distinction
This demonstrates that the template caching subsystem has an independent hygiene flaw: cache keys lack lexical qualification, causing cross-function specialization collisions whenever two callers construct structurally identical template arguments.
Suggested fix
Incorporate the caller's definition name or fully typed telescope context into the cache key in def_inst, or include variable types in term_key.
Confirmed still alive on HEAD 01875127 (2026-09-29), and I can sharpen the root cause: the "closed ~arguments" guard is domain-asymmetric, which is what makes the collision reachable at all.
Minimal pair (only the ~f domain differs, the defaulted arm x is identical):
Rejected (Bool domain — x free in the default arm caught):
So a free run-time variable in a defaulted arm passes the guard when the domain is a single-label ADT (where the default is unreachable at run time) and fails when the domain has more labels — the guard seems to key on arm reachability rather than on the argument actually being closed. The term_key collision in my report above then rides on top of this: both g and h submit "open-but-accepted" arguments that serialize identically, and h reuses g's app~0 without its own instantiation ever being checked.
Verdict behavior of the collision repro (same file as above, plus a main): --verdict on HEAD returns the auto-confessed mismatch:
SOME PROOFS FAIL
Sorry - this is a mismatch between the TypeScript implementation, and the formalized BendTT kernel.
i.e. the kernel refuses the emitted book that carries the dangling x — so the leak currently lands on the safe side, but the specialization cache itself is unsound: a Bool value flows through a Nat-typed spine (app(~f: Unit -> Nat) applied with an argument whose default arm is x : Bool yields h(True{}) computed as True{} at type Nat). A fix scoped only to term_key (types in the key / caller scoping) would keep the guard asymmetry; fixing both closes the family.
Activity
quimmedes commentedon Sep 30, 2026
Confirmed still alive on HEAD
01875127(2026-09-29), and I can sharpen the root cause: the "closed ~arguments" guard is domain-asymmetric, which is what makes the collision reachable at all.Minimal pair (only the
~fdomain differs, the defaulted armxis identical):Rejected (Bool domain —
xfree in the default arm caught):Accepted (
--check-onlygreen): Unit domain, same open default arm:So a free run-time variable in a defaulted arm passes the guard when the domain is a single-label ADT (where the default is unreachable at run time) and fails when the domain has more labels — the guard seems to key on arm reachability rather than on the argument actually being closed. The
term_keycollision in my report above then rides on top of this: bothgandhsubmit "open-but-accepted" arguments that serialize identically, andhreusesg'sapp~0without its own instantiation ever being checked.Verdict behavior of the collision repro (same file as above, plus a
main):--verdicton HEAD returns the auto-confessed mismatch:i.e. the kernel refuses the emitted book that carries the dangling
x— so the leak currently lands on the safe side, but the specialization cache itself is unsound: aBoolvalue flows through aNat-typed spine (app(~f: Unit -> Nat)applied with an argument whose default arm isx : Boolyieldsh(True{})computed asTrue{}at typeNat). A fix scoped only toterm_key(types in the key / caller scoping) would keep the guard asymmetry; fixing both closes the family.A ~ argument that reads a caller's variable in any arm is open, by th…
A fallback past a type's last constructor goes to --verdict as \{}, a…
A fallback past a type's last constructor goes to --verdict as \{}, a…
A fallback past a type's last constructor goes to --verdict as \{}, a…