Skip to content

--verdict certifies a different main (kernel 1, bend2 0n): a let passed to a self-call is built at the let, so a convoy captures it (#1168 via let_term) #1326

Closed

Description

@Vacrin

What you did

bend upstream.bend (run, --check-only, --verdict, -o k.bendtt); then appended probe : {main == N : Nat} = {==} to two copies of k.bendtt (N = 0 and 1 in the kernel's encoding) and ran the kernel --verdict builds on each: ~/.bend/bendtt//bendtt k.bendtt

What happened

$ bend upstream.bend
0n
$ bend upstream.bend --check-only
ALL PROOFS CHECK
Use --verdict for mathematical validity.
$ bend upstream.bend --verdict
ALL PROOFS CHECK
$ bend upstream.bend -o k.bendtt          (exit 0, nothing out of scope; go's Succ arm)
λx0 => λ{(): λx1 => !x2 = {λx2 => (.Succ, ((.Zero, ()), ())) : ∀x2 : Nat -> Nat};
  ({λx3 => (λ{(,): λ{.True: λ{(): λx4 => (go x0 λx2 => (x4 (.Zero, ()))); λ{}};
                    λ{.False: λ{(): λx4 => (x4 (.Zero, ())); λ{}}; λ{}}}} x3 x2) : ∀x3 : Bool -> Nat} (.True, ()))

Appended to a copy of k.bendtt (probe is just a def name; the kernel's 0 is (.Zero, ()) and its 1 is
(.Succ, ((.Zero, ()), ()))), then ~/.bend/bendtt/<hash>/bendtt on it:
probe : {main == (.Zero, ()) : Nat} = {==}
  -> SOME PROOFS FAIL
     In probe:
     expected: main
     observed: (.Zero, ())                 (exit 1)
probe : {main == (.Succ, ((.Zero, ()), ())) : Nat} = {==}
  -> ALL PROOFS CHECK                      (exit 0)

The source passes at0 = (f => f(0n)). The text passes λx2 => (x4 0), and x4 is `one`, carried into the
arm by the convoy: λ_. one(0). bend2 runs main to 0n; in the book --verdict passed, main is 1: the kernel
checks main == 1 and rejects main == 0.
Adding `def main_is_0() -> {main() == 0n : Nat}: {==}` makes bend2 accept and --verdict FAIL (the
MISMATCH message).

The file

# bend2 runs main to 0n, yet --verdict passes a kernel text whose main is 1
import Base

def go(n: Nat, g: (Nat -> Nat) -> Nat) -> Nat:
  match n:
    case Zero{}:
      g(x => 0n)
    case Succ{p}:
      at0 = {(f => f(0n)) : (Nat -> Nat) -> Nat}
      one = {(x => 1n) : Nat -> Nat}
      {\{True: go(p, at0); False: one(0n)} : Bool -> Nat}(True{})

def main() -> Nat:
  go(1n, (f => 0n))

bend --version

bend 2.0.35 (main)

uname -sm

Linux x86_64 (WSL2)

clang --version (the first line)

No response

This is not WONTFIX's "the checker skips dead code".

  • bend2 checks both arms of the λ-match: a False arm of the wrong type is rejected.
  • The wrong value comes through the True arm, which runs. The literal True{} only keeps the file
    short.
  • With the scrutinee a Bool parameter, the result is the same: bend2 still runs 0n, --verdict
    still passes, and the kernel's main is still 1. That variant has go(n, g, b), go(p, at0, False{})
    in the arm, the λ-match applied to (b), and main = go(1n, (f => 0n), True{}).

01875127 fixed #1168 and #1178 for a specialized argument, which now goes out where it is used
(safe.ts#L837).
let_term has the same pattern, and the commit left it alone. A let that a self-call takes is inlined as
a kernel term built at the let. It is stored in the scope with no kernel binder of its own
(safe.ts#L1205-L1209,
on the retry at #L1220-L1222):

const at = { ...s, D: s2.D, kq: s2.kq };
const v = term(e, at, x.v[j], live && q > 0);
const V = open(x.v[j])[1];
if (v.$ === "Var" || put.has(j)) {
  s2 = scope_bind(s2, v, V, false);

So at0's λf takes level 2, and so does the next kernel binder, the let one. The λ-match in the body
goes through tree:

  • Convoy choice. uses counts f's bound occurrence as a use of level 2
    (#L1310),
    so one counts as used twice and rides a convoy
    (#L663-L666).
  • The rename. convoy_bind's scope_move renames every Var at level 2 in every scope entry,
    bound or not
    (#L542-L547),
    so the stored λx2 => (x2 0) becomes λx2 => (x4 0).
  • The parse. The kernel's parser resolves x4 innermost-first, to the convoy's binder. The text is
    well typed, and it means another program.

bend2 forbids match after a let, so the convoy comes from a \{..} λ-match in the let body. That is
also how a program branches on a computed value at that point.

It is not limited to this shape. Each case below gets an all-green --verdict PASS on main whose
certified value differs from bend2's.

The colliding binder need not be a let. It can be:

  • a λ written after the let;
  • a λ-match field;
  • an outer convoy binder;
  • a default arm's tag/field pair. This one captures a Data value too: bend2 computes 105n, the kernel
    107.

The other direction. A convoyed free variable of the value can land on a level one of the value's
own binders took. Here c = {Succ{app2((u => (w => m(u))))} : Nat} goes out as
(app2 λx3 => λx4 => (x4 x3)): m became w. bend2 computes 6n, the kernel 51:

import Base

def app2(f: Nat -> (Nat -> Nat) -> Nat) -> Nat:
  f(1n)((z => 50n))

def fc(n: Nat, m: Nat -> Nat, acc: Nat) -> Nat:
  match n:
    case Zero{}:
      acc
    case Succ{p}:
      c = {Succ{app2((u => (w => m(u))))} : Nat}
      {\{True: fc(p, (x => x), c); False: c} : Bool -> Nat}(True{})

def main() -> Nat:
  fc(1n, (x => 5n), 0n)

Ordinary code.

  • A CPS builder over Base's Array,
    k2 = {(a => k(ANode{a, ALeaf{1n}})) : Array<Nat> -> Array<Nat>} plus a default array, certifies
    a sum of 10 for bend2's 1n.
  • A higher-order transformer, t2 = {(h => (x => h(Nat.mul(x, 2n)))) : (Nat -> Nat) -> Nat -> Nat}
    plus a default function, certifies 100 for 1n.

A theorem's statement. A -> Type def with this shape makes def thm() -> TT(1n, (z => z)): {==}
pass --verdict as {2 == 2}, where bend2 states {1 == 1}.

The other files (n1_array_cps, f2_type_def, a5_default_pair)
# ---- n1_array_cps.bend ---- (bend2 1n; the kernel's main is 10)
import Base

def asum(a: Array<Nat>) -> Nat:
  match a:
    case ALeaf{v}:
      v
    case ANode{xs, ys}:
      Nat.add(asum(xs), asum(ys))

def fill(n: Nat, k: Array<Nat> -> Array<Nat>, more: Bool) -> Array<Nat>:
  match n:
    case Zero{}:
      k(ALeaf{0n})
    case Succ{p}:
      k2 = {(a => k(ANode{a, ALeaf{1n}})) : Array<Nat> -> Array<Nat>}
      z = {ALeaf{9n} : Array<Nat>}
      {\{True: fill(p, k2, False{}); False: z} : Bool -> Array<Nat>}(more)

def main() -> Nat:
  asum(fill(1n, (a => a), True{}))
# ---- f2_type_def.bend ---- (the kernel accepts {(TT 1 λx0 => x0) == {2 == 2 : Nat} : *1} and rejects it at {1 == 1})
import Base

type Bx is Type:
  MkBx{f: Nat -> Nat}

def run(b: Bx) -> Nat:
  match b:
    case MkBx{f}:
      f(0n)

def apT(f: Bool -> Type, b: Bool) -> Type:
  f(b)

def EqT(b: Bx) -> Type:
  {run(b) == 0n : Nat}

def TT(n: Nat, g: Bx -> Bx) -> Type:
  match n:
    case Zero{}:
      {run(g(MkBx{(x => 1n)})) == run(g(MkBx{(x => 1n)})) : Nat}
    case Succ{p}:
      c = {(z => z) : Bx -> Bx}
      d = {MkBx{(x => 2n)} : Bx}
      apT(\{True: TT(p, c); False: EqT(d)}, True{})

def thm() -> TT(1n, (z => z)):
  {==}
# ---- a5_default_pair.bend ---- (bend2 105n; the kernel's main is 107)
import Base

def ap(f: Bool -> Nat, b: Bool) -> Nat:
  f(b)

def apn(f: Nat -> Nat, x: Nat) -> Nat:
  f(x)

def k(n: Nat, g: Nat -> Nat) -> Nat:
  match n:
    case Zero{}:
      g(5n)
    case Succ{p}:
      c = {\{Zero: 0n; m => Nat.add(m, 100n)} : Nat -> Nat}
      apn(\{Zero: 1n; m => ap(\{True: k(p, c); False: m}, True{})}, 7n)

def main() -> Nat:
  k(1n, (x => x))

No test file on main (0 of 1525, instrumented) and no Base def reaches the retry at L1220. That is why no
test caught this.

Suggested fix (tested). Do for a put value what 01875127 did for a specialized argument: bind its
bend2 term, so that term's Var case builds it at each use, at that use's depth and mode.

   for (let j = 0; j < x.k.length; j++) {
     const q = quant(x.q[j]);
+    const V = open(x.v[j])[1];
+    // an inlined value goes out at each use, at its depth (as a
+    // specialized argument does): built here, its binders would take
+    // levels a later binder takes too
+    if (put.has(j)) {
+      s2 = scope_bind(s2, { $: "Efq" }, V, false, x.v[j]);
+      continue;
+    }
     // a parallel let's value sees s's variables, below the lets before it
     const at = { ...s, D: s2.D, kq: s2.kq };
     const v = term(e, at, x.v[j], live && q > 0);
-    const V = open(x.v[j])[1];
-    if (v.$ === "Var" || put.has(j)) {
+    if (v.$ === "Var") {
       s2 = scope_bind(s2, v, V, false);

The fix also updates two comments: Bind's at L79-81 ("the term a specialized one (or an inlined let)
stands for") and let_term's at L1196-1198 ("…, which goes out at each use").

With it, go's arm is (go x0 λx3 => (x3 (.Zero, ()))) and the kernel's main is 0. Results:

  • Repros. Over our local repros (182 distinct programs), every PASS whose kernel main differed
    from bend2's now agrees with it. Each was probed at bend2's value and at another value. No PASS becomes
    a FAIL.
  • Adversarial programs. 21 programs written against the fix find no wrong-value PASS. Two stay false
    FAILs with the same text as on main: when inlining copies a captured function variable, the kernel says
    expected: *2 / observed: *1.
  • Tests. All 1525 test files on main give the same -o result with the fix, and the 1039 that bend2
    accepts give byte-identical texts. None of them reaches the retry, so this shows only that nothing else
    moved.
  • A side effect. A let-bound constructor passed to a self-call is now built in self-call mode, so it
    stays η-short. c = {MkBx{f} : Bx}; k(c, t) then passes the kernel's descent check, where on main it
    fails with affine live code, calls that descend:
letctor.bend
import Base

type L is Data:
  LNil{}
  LCons{h: U32, t: L}

type Bx is Type:
  MkBx{f: U32 -> U32}

def k(a: Bx, b: L) -> U32:
  match a:
    case MkBx{f}:
      match b:
        case LNil{}:
          0
        case LCons{h, t}:
          c = {MkBx{f} : Bx}
          k(c, t)

Making scope_move/uses stop at binders closes the first example but not the fc one (its text is
unchanged), so it is not a substitute for the fix above.

What the kernel can check. Nothing in the text is ill-typed, so the type check cannot see this. The
parser can.

  • The shape. Each capture reuses a binder name that is already bound where the binder is introduced.
    In go's arm, the value's λx2 sits under the let !x2. In fc, the value's λx3 => λx4 sits
    under the convoy's λx4.
  • Why it matters. parse_name resolves a name to the innermost binder, so the reused name silently
    rebinds.
  • Why it is safe to refuse. The kernel's own printer (Term.show) names each binder by its depth and
    never reuses a name in scope. With the fix, safe.ts doesn't either: every binder in the texts it emits
    for the tests is named x<depth>.

The parser could refuse such a binder, as parse_enum refuses a repeated label:

--- a/bend2/bendtt.lean
+++ b/bend2/bendtt.lean
@@ -589,6 +589,14 @@
   set (s.drop k.length)
   pure (String.ofList k)
 
+-- a binder's name: not bound where it is introduced, since a name
+-- resolves to the innermost binder (Term.show never reuses one)
+def Parse.fresh (vs : List String) : Parse String := do
+  let k ← Parse.name
+  if vs.contains k then
+    Parse.fail "a fresh binder name"
+  pure k
+
 -- a label name: a name, or ()
 def Parse.label : Parse String := do
   if ← Parse.take "()" then
@@ -658,7 +666,7 @@
 partial def Term.parse_let (vs : List String) : Parse Term := do
   Parse.eat "!"
   let q ← Quan.parse
-  let k ← Parse.name
+  let k ← Parse.fresh vs
   Parse.eat "="
   let v ← Term.parse vs
   Parse.eat ";"
@@ -668,7 +676,7 @@
 partial def Term.parse_bind (vs : List String) (c : String) : Parse Term := do
   Parse.eat c
   let q ← Quan.parse
-  let k ← Parse.name
+  let k ← Parse.fresh vs
   Parse.eat ":"
   let A ← Term.parse vs
   Parse.eat "->"
@@ -681,7 +689,7 @@
     Term.parse_match vs
   else
     let q ← Quan.parse
-    let k ← Parse.name
+    let k ← Parse.fresh vs
     Parse.eat "=>"
     let f ← Term.parse (k :: vs)
     return Lam q f
@@ -751,9 +759,9 @@
   Parse.eat "%"
   let e ← Term.parse vs
   Parse.eat ":"
-  let x ← Parse.name
+  let x ← Parse.fresh vs
   Parse.eat ","
-  let h ← Parse.name
+  let h ← Parse.fresh (x :: vs)
   Parse.eat "=>"
   let P ← Term.parse (h :: x :: vs)
   Parse.eat ";"

We ran main's kernel with this change:

A law on main's value in the emitted book, as tests/check/spec_arg_depth.bend has, is still the
regression test. A test in that style (go + fc, law let_put_depth_result: {main() == 6n : Nat},
#|6n) fails --verdict on main, where the kernel's main is 52, and passes with the fix:

# a let a self-call takes goes out at each use, at that depth: built where
# it was bound, its binders took the levels the next kernel binders take,
# and carrying such a binder into a λ-match's arms renamed the value too.
# In go, at0's body f(0n) went out as one(0n), so go(1n, g) was 1n; in
# fc, the free m moved onto the value's own w, so fc(1n, ..) was 51n. The
# law checks main's value in the emitted book too, where a wrong but
# well-typed body would still pass
import Base

def go(n: Nat, g: (Nat -> Nat) -> Nat) -> Nat:
  match n:
    case Zero{}:
      g(x => 0n)
    case Succ{p}:
      at0 = {(f => f(0n)) : (Nat -> Nat) -> Nat}
      one = {(x => 1n) : Nat -> Nat}
      {\{True: go(p, at0); False: one(0n)} : Bool -> Nat}(True{})

def app2(f: Nat -> (Nat -> Nat) -> Nat) -> Nat:
  f(1n)((z => 50n))

def fc(n: Nat, m: Nat -> Nat, acc: Nat) -> Nat:
  match n:
    case Zero{}:
      acc
    case Succ{p}:
      c = {Succ{app2((u => (w => m(u))))} : Nat}
      {\{True: fc(p, (x => x), c); False: c} : Bool -> Nat}(True{})

def main() -> Nat:
  Nat.add(go(1n, (f => 0n)), fc(1n, (x => 5n), 0n))

law let_put_depth_result:
  {main() == 6n : Nat}

def let_put_depth_result():
  {==}

#|6n

This is not a consistency problem: the kernel checked the text it was given. It is #1168's mechanism with
#1178's symptom: --verdict certifies a program other than the one bend2 checked and runs. So a bend2
checker error in a def of this shape would pass --verdict, and a true law about its value fails it
(main_is_0 above). #1178's thread took the convoy drift to be gone after 01875127. Through let_term
it is not, and it still reaches a type (the TT case).

Activity

  1. added
    bug
    on Oct 4, 2026
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