Skip to content

An application is inferable only when its arguments are, so a + let of Equal.cong over a function name goes to the kernel annotated - #1136

Closed
aldeni wants to merge 1 commit into
bendlang:mainfrom
aldeni:verdict-erased-arg-annotated
Closed

aldeni wants to merge 1 commit into
bendlang:mainfrom
aldeni:verdict-erased-arg-annotated

Conversation

@aldeni

@aldeni aldeni commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #1133, on the elaborator's side.

What happens

inferable(o) in safe.ts says an application infers when its head does, whatever its arguments. But an argument of a function type goes out η-long, a lambda, which the kernel cannot infer; and a + let (or a rewrite's evidence) is inferred, not checked. So

+h2 = Equal.cong(U32 & Bool, Bool, snd_of, x, mk(n), h)

went out as !+x3 = (Equal.cong … -λx3 => (snd_of x3) …), bend2 accepted the file and the kernel answered expected: an annotated term / observed: λx3 => (snd_of x3). Without the + the let is checked against its use and the same term passes, which is why #1133's table turns on the +.

The change

One line: an application is inferable when its head and its arguments are. With that, let_term and rwt_term do what they already do for any other non-inferable value, annotate it with the type bend2 knows:

!+x3 = {(Equal.cong -(Pair -U32 -Bool) -Bool -λx3 => (snd_of x3) -x1 -(mk x0) x2) : {(snd_of x1) == (snd_of (mk x0)) : Bool}}; x3

I checked the kernel side too, in case you'd rather fix it there: on the emitted .bendtt, the same lambda annotated by hand passes, a bare -snd_of passes, and annotating only the lambda's body does not. So the kernel wants the argument inferable, and the elaborator now makes the let so.

Checks (Linux x86-64, kernel binary 7f6ef51c)

  • tests/proof/cong_lambda_argument.bend is the issue's lemma with a main. On main, bend f.bend says ALL PROOFS CHECK and --verdict says SOME PROOFS FAIL; here both say ALL PROOFS CHECK, and so does the kernel binary on the emitted .bendtt, with the function written as snd_of or as p => snd_of(p).
  • Over tests/proof, tests/check and tests/spec (338 files), --verdict answers the same on main and here, file by file.
  • The new test prints True on the interpreted, JS and C lanes; gates/repo.ts passes 47 / 47.

Not run: gates/safe.ts on a cluster, and macOS.

…f Equal.cong over a function name goes to the kernel annotated

inferable(o) said an application infers when its head does, whatever
its arguments. But an argument of a function type goes out eta-long,
a lambda, which the kernel cannot infer; and a + let, or a rewrite's
evidence, is inferred. So `+h2 = Equal.cong(A, B, snd_of, x, mk(n), h)`
went out as `!+x3 = (Equal.cong ... -λx3 => (snd_of x3) ...)`, bend2
accepted the file and the kernel answered "expected: an annotated
term / observed: λx3 => (snd_of x3)" (bendlang#1133). With the arguments in
the question, let_term and rwt_term annotate the value with the type
bend2 knows: `!+x3 = {(Equal.cong ...) : {(snd_of x1) == ... : Bool}}`.

tests/proof/cong_lambda_argument.bend is the issue's lemma: on main
`bend f.bend` says ALL PROOFS CHECK and `--verdict` SOME PROOFS FAIL;
here both say ALL PROOFS CHECK, and so does the kernel binary on the
emitted .bendtt, with the function written as `snd_of` or as
`p => snd_of(p)`. Over tests/proof, tests/check and tests/spec (338
files) `--verdict` answers the same on main and here, file by file;
the new test prints True on the interpreted, JS and C lanes;
gates/repo.ts passes 47 / 47.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@nathanneuro

Copy link
Copy Markdown

Another data point for this fix. On main b2111cf (Darwin arm64), this 3-line file passes bend --check-only and fails
--verdict with In t3: expected: an annotated term from the kernel binary:

import Base

def t3(a: Nat, e: {a == 0n : Nat}) -> {1n+a == 1n : Nat}:
  +tr = Equal.cong(Nat, Nat, x => 1n+x, a, 0n, e)
  tr

It fails the same way with {x => 1n+x : Nat -> Nat} written at the source (the λ reaches the .bendtt without the annotation).
With this PR applied, both pass --verdict, and so does a ~770-line KMP proof that had hit the same
mismatch (a + let of Equal.cong(Nat, Bool, w => .., j, 1n+J, ej)). It had previously needed every such let inlined.

@nicolas-abril nicolas-abril self-assigned this Sep 28, 2026
@nicolas-abril

nicolas-abril commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the careful report and the repro. I'm closing this because the fix works against how the elaborator is meant to relate to the kernel: inferable in safe.ts should mirror Term.infer in bendtt.lean, and it already did.

The kernel does infer this application

Term.infer on App infers the head and checks the argument against the domain (Term.check ck n c x A). So (Equal.cong -T -U -λx3 => (snd_of x3) -x1 -(mk x0) x2) infers fine. The λ argument is checked against ∀ x0 -> x1. The old inferable(o.f) was the exact mirror of that rule. With && inferable(o.x) the elaborator becomes stricter than the kernel. Some lets also newly go out of scope when bend2 has no type for them (oos("a let with no known type")). And the v.$ !== "App" test in let_term's self-call re-binding now sees Ann where it used to see App.

What actually fails: the + let's V : *2 check on an un-β-reduced type

| n + 1, c, Let q v f, T => do
    let V := Ctx.wnf ck c (← Term.infer ck n c v)
    if q == Q2 then
      Term.check ck n c V (Typ Q2)
  1. infer v returns Term.inst B x. That is plain substitution, which doesn't β-reduce, so V = {((λx3 => snd_of x3) x1) == ((λx3 => snd_of x3) (mk x0)) : Bool}. Ctx.wnf only reduces V's head (Eql).
  2. Since q = 2, the kernel checks V : *2. Eql has no check rule, so it goes through infer, which checks each side against Bool.
  3. Left side ((λ…) x1): the argument is a Var, so the elim rule (App q f (Var i)) applies. It passes.
  4. Right side ((λ…) (mk x0)): the argument is not a Var, so it goes to infer (App …), then infer (λx3 => …). That fails with expected: an annotated term.

bend2's bend.ts never meets this term: its HOAS instantiation β-reduces as it builds (term_higher's App case applies a Lam head). Its V is {snd_of(x) == snd_of(mk(n)) : Bool}.

Kernel runs on the emitted .bendtt (binary 7f6ef51c)

variant result
as emitted (!+x3 = (Equal.cong … -λx3 => (snd_of x3) -x1 -(mk x0) x2)) FAIL, observed: λx3 => (snd_of x3)
same value, !x3 (q = 1, no V : *2 check) CHECK
same value, no let CHECK
-snd_of instead of the λ (head of the redex is a Ref) CHECK
+ let of Equal.cong … -x0 -x0 {==} (both redex arguments are Vars) CHECK
+ let of Equal.cong … -(mk x0) -(mk x0) {==} FAIL, same error
a bare {{((λx1 => snd_of x1) (mk x0)) == … : Bool} : *2} in a type FAIL, same error
the same with x0 as the argument CHECK

So the argument never needed to be inferable. Annotating the whole let works only because it replaces the kernel's un-β-reduced V with bend2's reduced one. A fix should keep inferable equal to Term.infer and target this directly. For example, annotate a let whose emitted quantity is 2 (including a q=1 let let_term promotes to 2) with bend2's type.

@aldeni

aldeni commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Thank you, @nicolas-abril, that was the explanation I was missing, and it also answers a variant I couldn't account for (a variable endpoint passing with the same emitted let: the elim rule). #1159 does what you describe: inferable stays as it is, and let_term annotates a let whose emitted quantity is 2, the promoted case included, with bend2's type. The test holds both shapes reported here, @nathanneuro's Nat one included.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--verdict: the kernel rejects a +-bound Equal.cong over a pair whose endpoint is a call (an unannotated eta-expansion), and the verdict does not say why

3 participants