Skip to content

--verdict mismatch: a λ rebuilt as a match keeps its variable's old level in its annotations (Nat where P is expected) #1669

Closed

Description

@aaaxn

What you did

bun bend2/main.ts twice.bend --check-only, then bun bend2/main.ts twice.bend --verdict

What happened

$ bun bend2/main.ts twice.bend --check-only
ALL PROOFS CHECK
Use --verdict for mathematical validity.
$ bun bend2/main.ts twice.bend --verdict
SOME PROOFS FAIL
In main:
expected: P
observed: Σx2 : <Zero, Succ> -> (Nat.arms x2)
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. This will be addressed in a future update. Meanwhile, feel free to open an issue to report this bug.

The file

import Base

type P is Type:
  P{a: Nat, b: Nat}
type Tag<-x: P> is Data:
  Tag{}
def first(p: P) -> Nat:
  match p:
    case P{a, b}:
      a
def tag(-p: P) -> Tag<p>:
  Tag{}
def use(-f: P -> Nat) -> Nat:
  0n
def main() -> Nat:
  use(p => Nat.add(first(p), {\{Tag: first(p)} : Tag<p> -> Nat}(tag(p))))

bend --version

main at e5527d5 (VERSION 2.0.37), run as bun bend2/main.ts. The result is the same on #1666 at fce97a9. I built the kernel from main's bendtt.lean with Lean 4.34.0.

uname -sm

Linux x86_64

clang --version (the first line)

No clang on PATH. clang-19 --version gives Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2). --verdict doesn't use it.

Diagnosis

use takes f erased, so bend2 lets the λ passed for it use p twice. tree sends that λ to rebuild (safe.ts:929-944). rebuild turns the λ into a λ-match on P and builds its body with p as P{a, b}, under new binders for a and b. The annotations bend2 attached to the body still name p by the level bend2 checked it at, and that level now belongs to a. From -o:

main : Nat =
  (use -λ{(,): λ{.P: λ{(,): λ+x0 => λ{(,): λ+x1 => λ{(): (Nat.add (first (.P, (x0, (x1, ())))) ({…} : ∀x2 : (Tag -x0) -> Nat} (tag -(.P, (x0, (x1, ())))))); λ{}}}}; λ{}}})

x0 is a : Nat, so Tag -x0 is ill-typed. The type should be Tag -(.P, (x0, (x1, ()))). The rebuilt λ written by hand passes --check-only and --verdict:

  use(\{P: a => b => Nat.add(first(P{a, b}), {\{Tag: first(P{a, b})} : Tag<P{a, b}> -> Nat}(tag(P{a, b})))})

When the constructors have no fields, rebuild adds no binder, so no variable in scope has the old level. --verdict then reports out of scope: main: a free variable u instead:

twice0.bend
import Base

type U is Type:
  U1{}
  U2{}
type Tag<-x: U> is Data:
  Tag{}
def code(u: U) -> Nat:
  match u:
    case U1{}:
      1n
    case U2{}:
      2n
def tag(-u: U) -> Tag<u>:
  Tag{}
def use(-f: U -> Nat) -> Nat:
  0n
def main() -> Nat:
  use(u => Nat.add(code(u), {\{Tag: code(u)} : Tag<u> -> Nat}(tag(u))))

The wrong levels are only in annotations inside a def's body, never in its type. So the bug can reject a valid proof, but it cannot make the kernel accept a wrong one. No test, demo, or bench file reaches it.

Fix

On top of #1666, the fix is one line in rebuild. It passes the λ through subst(x, s.d), as #1666 does in eta, so the body's annotations move with its binders:

-    return G.$ !== "All" ? x.f(B.Ann(B.Ctr(k, xs), F)) : B.Lam(G.k, 0, (v: HTerm) => arm(k, G.B(v), [...xs, v]));
+    return G.$ !== "All" ? (subst(x, s.d) as typeof x).f(B.Ann(B.Ctr(k, xs), F)) : B.Lam(G.k, 0, (v: HTerm) => arm(k, G.B(v), [...xs, v]));

With the fix, both files pass --verdict, and the -o of twice.bend is byte-identical to the hand-written version's. --verdict and -o don't change on any of the 1,734 test, demo, and bench files. safe.ts goes from 23,987 to 23,997 ttok (ttok==0.3). With #1657 merged as well, it would be at 24,006, over the 24,000 cap.

Related

Activity

  1. aaaxn commented on Oct 19, 2026

    @aaaxn
    ContributorAuthor

    #1666 now fixes this at the source: arm lowers each def's body once, so the one-line change to rebuild above isn't needed. #1670 is closed, and its test is in #1666.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions