Skip to content

The BendTT kernel rejects what bend2 accepts #1138

Closed

Description

@yalexey-jcode

The BendTT kernel rejects what bend2 accepts: an affine let bound to Equal.cong with a λ motive and non-variable arguments fails --verdict with "an annotated term"

What I did

Bend 2.0.32 (Linux x86_64). A proof binds a cong step with a λ motive to a duplicable (+) let, and the motive is applied to arguments that are not variables — the natural shape for "rewrite under a function" steps (affine_cong_repro.bend):

import Base

def mini(+a: Nat) -> {Nat.add(a, Nat.add(a, a)) == Nat.add(a, Nat.add(a, a)) : Nat}:
  +p = Equal.cong(Nat, Nat, (z) => Nat.add(a, z), Nat.add(a, a), Nat.add(a, a), {==})
  p

def main() -> Nat:
  0n
bend affine_cong_repro.bend          # checks the file, runs main
bend affine_cong_repro.bend --verdict

What I expected

ALL PROOFS CHECK from both commands: bend accepts the file (it prints 0n and exits 0), so the kernel should agree — that is the point of --verdict.

What happened

$ bend affine_cong_repro.bend
0n

$ bend affine_cong_repro.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. This will be addressed in a future update. Meanwhile, feel free to open an issue to report this bug.

Running the kernel directly on the elaboration (bend affine_cong_repro.bend -o r.bendtt, then bendtt r.bendtt, where bendtt is the kernel binary the CLI caches for --verdict, or one built from bend2/bendtt.lean as shown in Verification) localizes it:

SOME PROOFS FAIL
In mini:
expected: an annotated term
observed: λx1 => ((Nat.add x0) x1)

The elaborated def is:

mini : ∀+x0 : Nat -> {(Nat.add x0 (Nat.add x0 x0)) == (Nat.add x0 (Nat.add x0 x0)) : Nat} =
  λ+x0 => !+x1 = (Equal.cong -Nat -Nat -λx2 => (Nat.add x0 x2) -(Nat.add x0 x0) -(Nat.add x0 x0) {==}); x1

A standalone kernel-level repro — the elaboration above plus its dependencies, as bendtt_affine_cong_repro.bendtt:

Nat : *2 =
  Σx0 : <Zero, Succ> -> (Nat.arms x0)

Nat.arms : ∀x0 : <Zero, Succ> -> *2 =
  λ{.Zero: <()>; λ{.Succ: Σx0 : Nat -> <()>; λ{}}}

Nat.add : ∀x0 : Nat -> ∀x1 : Nat -> Nat =
  λ{(,): λ{.Zero: λ{(): λx0 => x0; λ{}}; λ{.Succ: λ{(,): λx0 => λ{(): λx1 => (.Succ, ((Nat.add x0 x1), ())); λ{}}}; λ{}}}}

Equal.cong : ∀-x0 : *1 -> ∀-x1 : *1 -> ∀-x2 : ∀x2 : x0 -> x1 -> ∀-x3 : x0 -> ∀-x4 : x0 -> ∀x5 : {x3 == x4 : x0} -> {(x2 x3) == (x2 x4) : x1} =
  λ-x0 => λ-x1 => λ-x2 => λ-x3 => λ-x4 => λx5 => %x5 : x6, x7 => {(x2 x3) == (x2 x6) : x1}; {==}

mini : ∀+x0 : Nat -> {(Nat.add x0 (Nat.add x0 x0)) == (Nat.add x0 (Nat.add x0 x0)) : Nat} =
  λ+x0 => !+x1 = (Equal.cong -Nat -Nat -λx2 => (Nat.add x0 x2) -(Nat.add x0 x0) -(Nat.add x0 x0) {==}); x1
bendtt bendtt_affine_cong_repro.bendtt   # SOME PROOFS FAIL (In mini: an annotated term)

The control (bendtt_linear_ok.bendtt) is the same file with the let's quantity changed from !+ (duplicable) to ! (linear):

mini : ∀+x0 : Nat -> {(Nat.add x0 (Nat.add x0 x0)) == (Nat.add x0 (Nat.add x0 x0)) : Nat} =
  λ+x0 => !x1 = (Equal.cong -Nat -Nat -λx2 => (Nat.add x0 x2) -(Nat.add x0 x0) -(Nat.add x0 x0) {==}); x1
bendtt bendtt_linear_ok.bendtt            # ALL PROOFS CHECK

It needs all three parts: a + on the let (the linear control above checks), a λ motive, and non-variable arguments (hoisting them into their own lets also checks — the elim rule's variable arm applies; see Workaround).

Where it comes from

Reading bend2/bendtt.lean at v2.0.32:

  1. Term.infer's App rule instantiates the function's codomain with Term.inst B x, without β-reducing, so instantiating cong's result type {(x2 x3) == (x2 x4) : x1} at a λ motive leaves β-redexes inside the type — (λx2 => …) (Nat.add x0 x0). That is fine for wnf/conv/fit, which β-reduce.
  2. The Let rule rechecks V : *2 when q == Q2 (the + case), and checking an Eql type rechecks its two sides.
  3. Checking a redex App q (λ…) (t…) falls through to the catch-all infer, and infer has no rule for a λ-headed application — only check mode can type a λ, against an expected type. Hence "an annotated term".
  4. The check-mode elim rule that would handle it, App q f (Var i), only fires when the argument is syntactically a variable — which is why the variable-argument variant of the same proof passes.

So the TS elaborator produces a term the TS checker accepts, but the kernel's algorithmic rules cannot re-derive its declarative typing, and the verdict reports a mismatch.

A tempting one-line fix — teach infer to β-reduce (infer (App _ (Lam _ g) x) := infer (inst g x)) — is unsound against the declarative semantics: it would accept redexes whose argument is untyped garbage whenever the λ's binder is unused, while the declarative app rule types both sides. I tried it; theorem chk cannot be proven for it.

Workaround

Bind the cong step once per use (two linear lets instead of one duplicable let), or hoist the non-variable arguments into their own lets so the elim rule's variable arm applies. Both check under --verdict today; the attached real-world proof (2.8k lines, 12 laws of an mpn arithmetic library) ships with the split applied.

Proposed fix

Patch against v2.0.32, bend2/bendtt.lean (also attached as bendtt_elim_patch.diff). It generalizes the elim rule from variable arguments to any argument:

--- a/bend2/bendtt.lean
+++ b/bend2/bendtt.lean
@@ -252,6 +252,16 @@
   | Efq       => true
   | _         => false
 
+-- the elim rule's motive: a variable argument abstracts the goal by pick
+-- (the goal may depend on it); any other argument cannot be named by
+-- the goal, which takes it as its constant codomain, lifted
+def Term.elim_P (x : Term) (T : Term) : Term :=
+  match x with
+  | Var i => Term.ren (Ren.pick i) T
+  | Ref _ | Ann _ _ | Let _ _ _ | Typ _ | All _ _ _ | Lam _ _ | App _ _ _
+  | Sig _ _ _ | Tup _ _ _ | Lab _ | Prj _ | Mat _ _ _ | Efq | Eql _ _ _
+  | Enu _ | Rfl | Rwt _ _ _ => Term.ren Nat.succ T
+
 -- a former whose parts may bind a variable
 def Term.binds : Term → Bool
   | All .. | Lam .. | Sig .. | Rwt .. => true
@@ -1025,9 +1035,12 @@
 -- ------------------------------------------------------- rwt
 -- Γ ⊢ %e : x, h => P; f : T
 --
--- Γ ⊢ x : A   Γ ⊢ f : ∀q s:A -> T[x := s]    f's head is a λ or a λ-match
--- -------------------------------------------------------------- elim
+-- Γ ⊢ x : A   Γ ⊢ f : ∀q s:A -> P   f's head is a λ or a λ-match
+-- ------------------------------------------------- elim
 -- Γ ⊢ (f q x) : T
+-- (P is T with x abstracted when x is a variable, so T may depend on
+-- it; any other argument cannot be named by T, so P is T lifted, its
+-- constant codomain)
 --
 -- Γ ⊢ x : U   U ≤ T
 -- ----------------- any
@@ -1106,13 +1119,12 @@
       Ctx.fit ck c (Term.inst (Term.inst P (Term.ren Nat.succ e)) b) T
       Term.check ck n c f (Term.inst (Term.inst P Rfl) a)
     | E => Ctx.fail c "an equation" E
-  | n + 1, c, App q f (Var i), T =>
+  | n + 1, c, App q f x, T =>
     if Term.takes (Term.unspine f []).1 then do
-      let A ← Term.infer ck n c (Var i)
-      let P := Term.ren (Ren.pick i) T
-      Term.check ck n c f (All q A P)
+      let A ← Term.infer ck n c x
+      Term.check ck n c f (All q A (Term.elim_P x T))
     else do
-      let U ← Term.infer ck n c (App q f (Var i))
+      let U ← Term.infer ck n c (App q f x)
       Ctx.fit ck c U T
   | n + 1, c, t, T => do
     let U ← Term.infer ck n c t
@@ -1657,6 +1669,15 @@
     funext j; by_cases h : j = i <;> simp [Ren.pick, Subst.one, h]
   rw [Term.inst, sub_ren, this, sub_var]
 
+-- a lifted type is its own constant codomain: instantiating it with any
+-- argument gives it back, so the elim rule can check a λ-headed redex
+-- (App q f x, x not a variable) against T by checking f against
+-- ∀q _:A -> (lift T), with A the inferred type of x
+theorem lift_inst : Term.inst (Term.ren Nat.succ T) x = T := by
+  have : Subst.one x ∘ Nat.succ = Var := by
+    funext j; cases j <;> rfl
+  rw [Term.inst, sub_ren, this, sub_var]
+
 theorem unspine_spine : Term.unspine (Term.spine t xs) [] = Term.unspine t xs := by
   induction xs generalizing t with
   | nil => rfl
@@ -2206,6 +2227,24 @@
   have ⟨_, h1, h2⟩ := e' ▸ wnf_conv (c := c) (T := D) hcl
   .conv (.conv h (.inl ⟨_, pars_all h2, pars_all h1⟩)) (wnf_fits hcl e).1
 
+-- the elim rule, declaratively: f checked against ∀q _:A -> elim_P x T
+-- and x checked against A type the application (App q f x) with T, be x
+-- a variable (pick, so the goal may depend on it) or any other term (the
+-- goal's lift, its constant codomain)
+theorem elim_sound {bk : Book} {σ : Subst} {Γ : List Term} {q : Quan} {A T f x}
+    (hf : Typed bk Γ (Term.sub σ f)
+      (All q (Term.sub σ A) (Term.sub (Subst.up σ) (Term.elim_P x T))))
+    (hx : Typed bk Γ (Term.sub σ x) (Term.sub σ A)) :
+    Typed bk Γ (Term.sub σ (App q f x)) (Term.sub σ T) := by
+  have e : Term.inst (Term.sub (Subst.up σ) (Term.elim_P x T)) (Term.sub σ x)
+      = Term.sub σ T := by
+    cases x <;> simp only [Term.elim_P] <;> first
+      | (rw [← inst_sub]; exact congrArg (Term.sub σ) pick_inst)
+      | (rw [sub_succ, lift_inst])
+  have h := Typed.app hf hx
+  rw [e] at h
+  exact h
+
 -- each checker rule lands on its Typed rule, through Ctx.drop
 theorem chk (hcl : Sees ck bk) (n : Nat) : ∀ c t T, Ctx.ok bk c →
     (Term.infer ck n c t = .ok T → Chk bk c t T) ∧
@@ -2259,18 +2298,8 @@
     · rename_i e
       obtain ⟨hp, h3, h2⟩ := h
       exact snd_wnf2 hcl e rfl (.lam hp (fun e => C _ _ (h3 e)) (B h2))
-    · rename_i x
-      cases x <;> simp only [Term.check] at h
-      case Var i =>
-        split at h
-        · simp at h
-          obtain ⟨A, h1, h2⟩ := h
-          have e := congrArg (Term.sub (Ctx.drop c)) (pick_inst (T := T) (i := i))
-          rw [inst_sub] at e
-          rw [Chk, ← e]
-          exact .app (C _ _ h2) (I _ _ h1)
-        · exact A h
-      all_goals exact A h
+    · obtain ⟨A, h1, h2⟩ := h
+      exact elim_sound (C _ _ h2) (I _ _ h1)
     · rename_i e
       obtain ⟨h1, h2⟩ := h
       exact .conv (.sig (C _ _ h1) (B h2)) (wnf_fits hcl e).1
  • Term.elim_P x T — the motive: Term.ren (Ren.pick i) T when x is Var i (unchanged behavior, the goal may depend on it), and the lift Term.ren Nat.succ T otherwise — the goal cannot depend on a term it does not name, so the lift is its constant codomain.
  • The rule App q f x checks f against ∀q _:A -> elim_P x T, with A inferred from x.
  • lift_inst (inst (ren succ T) x = T) and elim_sound carry the soundness argument, and theorem chk still compiles — the kernel's own soundness proof accepts the new rule.

The old variable case is byte-for-byte the same rule, so nothing that checked before can change verdict (the takes-false arm is also unchanged).

Verification

case bend2 (TS) v2.0.32 kernel patched kernel
affine_cong_repro.bend ok mismatch ALL PROOFS CHECK
bendtt_affine_cong_repro.bendtt (kernel repro above) ok FAIL — "In mini: an annotated term" ALL PROOFS CHECK
bendtt_linear_ok.bendtt (linear control above) ok checks checks
same source proof, cong args hoisted into lets (Workaround, second form) ok checks under --verdict —
real 2.8k-line proof, workaround reverted ok fails in Wb.limb_mul_eq, same message ALL PROOFS CHECK
real proof, workaround kept; mpn.bend, mpn_par.bend, sym.bend ok check check
demos/proof_numerics ok checks checks
155 elaborated files from tests/{check,proof,base} — identical verdicts, 0 differences identical verdicts, 0 differences
lean compile of bendtt.lean incl. theorem chk — clean clean

Build the patched kernel with the elan toolchain the CLI already uses:

lean -c bendtt.c bendtt.lean && leanc -O3 -DNDEBUG bendtt.c -o bendtt
BENDTT=./bendtt bend affine_cong_repro.bend --verdict

Attachments

  • bendtt_elim_patch.diff — the patch above as a standalone file, for git apply
  • bendtt_patched.lean — the full patched bend2/bendtt.lean, for building directly
  • PROOF_workaround_reverted.bend — the real 2,789-line proof with the workaround reverted: the stock kernel fails in Wb.limb_mul_eq with the same message, the patched kernel reports ALL PROOFS CHECK
  • affine_cong_repro.bend, bendtt_affine_cong_repro.bendtt, bendtt_linear_ok.bendtt — the repros, also inline above

Side note: an environment failure reports the same "mismatch"

While triaging this on a 3 GB machine I first hit a different failure with the same message: kernel_check sets LEAN_STACK_SIZE_KB: "4194304" unconditionally, the kernel binary then aborts at startup (lean::exception: failed to create thread: Resource temporarily unavailable, exit 134), and the CLI reports it as a mismatch between the implementations. A spawn failure or empty kernel output is an environment problem, not a TS/kernel disagreement; distinguishing those (or catching the thread error) would save users a misleading diagnosis. This note is separate from the bug above — with enough memory the repro's mismatch is real and deterministic.

Activity

  1. added
    bug
    on Sep 27, 2026
  2. yalexey-jcode commented on Sep 27, 2026

    @yalexey-jcode
    Author

    It's similar to #1133

  3. nicolas-abril commented on Sep 28, 2026

    @nicolas-abril
    Collaborator

    Fixed by #1158, which also closed #1133, the same bug. The kernel now puts a λ argument into the type annotated with its domain, so the calls of it there infer. On main (3378e62) your affine_cong_repro.bend prints 0n and --verdict says ALL PROOFS CHECK. Thanks for the detailed report!

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