Skip to content

--verdict fails on programs bend2 accepts: safe.ts splices terms at stale binder levels, so scope_move captures and uses over-counts #1168

Closed

Description

@Vacrin

What you did

bend capture_reject.bend --check-only | bend capture_reject.bend --verdict

What happened

$ bun bend2/main.ts capture_reject.bend --check-only
ALL PROOFS CHECK
Use --verdict for mathematical validity.

$ bun bend2/main.ts capture_reject.bend --verdict
SOME PROOFS FAIL
Sorry - this is a mismatch between the TypeScript implementation, and the formalized BendTT kernel. Your proofs may or may not be correct, and we cannot validate them yet. [...]

The file

import Base

def qof2(f: Nat -> Nat) -> Quant:
  &1

def k(f: Nat -> Nat, g: Nat -> Nat, -A: Kind(qof2(f)), b: Bool) -> Nat:
  match b:
    case True{}:  f(g(0n))
    case False{}: f(g(1n))

def main() -> Nat:
  k((y => y), (z => z), Unit, True{})

Root cause: k's -A: Kind(qof2(f)) specializes f, so safe.ts elaborates k as a per-instance tree and splices the column argument (y => y). The splice happens in arm: the column value is elaborated by the term(e, si, v, false) call at safe.ts:1067, at a stale binder level, then spliced under more binders. Two level-based passes then misfire:

scope_move's mv (safe.ts:534-539) renames every Var at a level, bound or free, so convoying g rewrites the identity's body: f(g(0)) is emitted as (λ_.g)(g(0)).
uses (safe.ts:1277) counts a λ's own bound var as a use of an outer level (the Lam case never checks o.l === l); lams (safe.ts:1269) and let_term (safe.ts:1196) then promote the binder to +, and a + binder of type Nat -> Nat fails the kernel's *2 check.
Currently masked — and un-masking it turns this into a wrong-program PASS

On this file, --verdict FAILs today because of a sibling bug: a root instance references its root constant before that constant is emitted. Fix that emit-ordering bug in isolation and this capture stops failing and instead passes --verdict with a body that computes something different from what bend2 checked, at the (still-correct) declared type. The minimal value-changing form needs no λ-headed redex. So this should be fixed together with the emit-ordering bug, not independently of it.

Severity / soundness

This is a fidelity/completeness bug, not a soundness hole. The capture corrupts a def's body, never its declared type: declared types are elaborated on a separate, capture-free path (def_emit, safe.ts:296), which scope_move never touches. Since the kernel re-typechecks each body against that faithful declared type and canonicity forbids an inhabitant of an uninhabited type, there is no path from this bug to a proof of Empty — the worst case is a well-typed body that computes the wrong thing (the "wrong-program PASS" above, once the masking bug is fixed).

Fix direction

Elaborate a spliced term at the depth where it lands, or re-level it on entry. Guarding mv/uses at binders with l === a (and Rwt's l/l+1) is only a partial fix. ~ arguments avoid the problem entirely because arm substitutes them before elaboration (safe.ts:1055-1057).

Related: distinct root cause from #1138 (kernel rejects what bend2 accepts) — not a duplicate; this pinpoints the splice/relevel mechanism.

bend --version

bend 2.0.32

uname -sm

Linux x86_64

clang --version (the first line)

No response

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions