`PANIC at Lean.Meta.whnfEasyCases … loose bvar in expression` from `trace.Elab.app.propagateExpectedType` (delaborator `getParamKinds`)

Aperta
#16,869 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
compilers

Direzione di ricerca

Start with src/Lean/Elab/App.lean at the two trace.Elab.app.propagateExpectedType calls and Lean.PrettyPrinter.Delaborator.getParamKinds, where pretty printing reaches whnf and forallTelescopeReducing on types with loose bound variables. Read how these expressions are formatted and compare the proposed guards with printing the type after opening skipped binders. Reproduce with the provided repro.lean and projection example; done means tracing completes without a panic and the projection trace line is preserved.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Prerequisites
  • Check that your issue is not already filed:
    https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case.
    Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example on
    https://live.lean-lang.org/#project=lean-nightly
    (You can also use the settings there to switch to “Lean nightly”)
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    If in doubt, please reach out on [email protected] for responsible disclosure.
Description

With set_option trace.Elab.app.propagateExpectedType true, Lean prints PANIC at Lean.Meta.whnfEasyCases Lean.Meta.WHNF:434:22: loose bvar in expression on stderr while it formats the trace message. The file still compiles (exit code 0); with LEAN_ABORT_ON_PANIC=1 the process aborts.

Two trace calls in getResultingTypeCore? (src/Lean/Elab/App.lean, lines 475 and 498 at v4.37.0-rc1) print a type that still has loose bound variables, one for each argument that is not elaborated yet: "propogation postponed, resulting type depends on arguments that have not yet been elaborated: {fType'}" (line 475, spelled that way) and "propagation postponed, resulting type depends on arguments that have not yet been elaborated: {fType}" (line 498). The delaborator prints loose bound variables as #n, but Lean.PrettyPrinter.Delaborator.getParamKinds calls whnf (in its loop over the arguments) and forallTelescopeReducing (at its end) on the type of the applied function after instantiating it with the arguments. When that type is a loose bound variable (below: the type of @id #1 #0 is #1), whnfEasyCases reaches its panic!. The docstring of getParamKinds says that it "captures errors (except for panics)".

The #n in the trace output is the delaborator finishing its job. The problem reported here is the PANIC, and that LEAN_ABORT_ON_PANIC=1 aborts a compile that otherwise succeeds.

Context

Found while tracing expected-type propagation in a Mathlib-based project, where one file prints the panic 7 times, each with a backtrace of about 100 frames. The cases seen are applications whose function's result type is one of the not-yet-elaborated arguments, for example a coercion to a function ⇑f x whose codomain is such an argument. Not every such application panics (see the projection example below).

The reproduction and analysis were done with an AI assistant (Claude Code), and this text was reviewed adversarially by a second AI model before posting.

Steps to Reproduce
  1. Save as repro.lean (no imports):
    theorem ev (β : Type) (x : β) : id x = id x := rfl
    
    set_option trace.Elab.app.propagateExpectedType true in
    example : id 0 = id 0 := ev Nat 0
    
  2. Run lean repro.lean.
  3. The same panic without the trace option, by pretty printing a term with loose bound variables directly:
    import Lean
    open Lean Meta
    
    #eval show MetaM Unit from do
      logInfo m!"{mkApp3 (mkConst ``id [1]) (.bvar 1) (.bvar 0) (mkNatLit 0)}"
    

Expected behavior: The trace is printed without a panic.

Actual behavior: stderr has, twice for step 2,

PANIC at Lean.Meta.whnfEasyCases Lean.Meta.WHNF:434:22: loose bvar in expression
backtrace:
…
…(Lean.Meta.whnfEasyCases spec at Lean.Meta.whnfImp+0x…)
…(lean_whnf+0x…)
…(Lean.Meta.forallTelescopeReducingAux [arity↓, private]+0x…)
…(Lean.Meta.forallTelescopeReducing [arity↓] spec at Lean.PrettyPrinter.Delaborator.getParamKinds+0x…)
…(Lean.PrettyPrinter.Delaborator.getParamKinds+0x…)
…(Lean.PrettyPrinter.Delaborator.delabAppCore+0x…)
…(Lean.PrettyPrinter.Delaborator.delabApp+0x…)

For step 3 it has three panics, from the whnf call in the loop of getParamKinds (the frame above lean_whnf is WellFounded.opaqueFix₃ [arity↓] spec at Lean.PrettyPrinter.Delaborator.getParamKinds).

In step 2 the trace itself is still printed:

[Elab.app.propagateExpectedType] ❌️ resulting type: <not-available>
  [Elab.app.propagateExpectedType] numArgs: 2, namedArgs.length: 0
  [Elab.app.propagateExpectedType] fType: ∀ (β : Type) (x : β), id x = id x
  [Elab.app.propagateExpectedType] propagation postponed, resulting type depends on arguments that have not yet been elaborated: @id
          #1 #0 =
        @id #1 #0

The exit code is 0. With LEAN_ABORT_ON_PANIC=1 the process aborts (exit code 134).

Versions

Lean (version 4.38.0-nightly-2026-11-26, x86_64-unknown-linux-gnu, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
Also v4.37.0-rc1 (WHNF:429:22), v4.36.0 (WHNF:392:22), v4.35.0 and v4.33.1 (WHNF:391:22).
Manjaro Linux, kernel 6.18.

Additional Information

getParamKinds is the same on v4.37.0-rc1 and on master at 80362eeeada90bd5e5b0aae9d1d15dd95978a6d4. One possible fix is to stop in getParamKinds when the type to reduce has loose bound variables:

       unless fnType.isForall do
-        fnType ← withTransparency .all <| whnf (fnType.instantiateRevRange j i args)
+        fnType := fnType.instantiateRevRange j i args
+        if fnType.hasLooseBVars then return result
+        fnType ← withTransparency .all <| whnf fnType
         j := i
…
     fnType := fnType.instantiateRevRange j args.size args
+    if fnType.hasLooseBVars then return result
     forallTelescopeReducing fnType fun xs _ => do

Limits of this sketch: it was not built into Lean. A copy of the function with these two guards, run as a MetaM program, does not panic on the inputs above and returns the same parameter names as the original on closed inputs. Any loose bound variable stops the scan, also where whnf would not panic (a type such as ∀ _ : #0, Nat), so parameters that forallTelescopeReducing reports today are then missing. And where the panic is caught today the function returns #[], while the guards return the kinds collected so far, so the printed form of such terms can change (for example where @ is inserted).

The other place to fix it is the two trace calls: open the skipped binders into local free variables and print that closed type. (The arguments are not elaborated yet, so the binders cannot be instantiated with them.) That would also cover a second effect of printing these types, which the guards do not repair: with a structure projection in place of id, the trace line is lost.

structure Hom (α β : Type) where
  toFun : α → β

theorem ev (β : Type) (h : Hom Nat β) : h.toFun 0 = h.toFun 0 := rfl

set_option trace.Elab.app.propagateExpectedType true in
example (h : Hom Nat Nat) : h.toFun 0 = h.toFun 0 := ev Nat h
  [Elab.app.propagateExpectedType] propagation postponed, resulting type depends on arguments that have not yet been elaborated: failed to pretty print expression (use 'set_option pp.rawOnError true' for raw representation)

Both applications of ev in the example lose their line this way, and there is one panic, so the lost line does not come from the panic. With set_option pp.rawOnError true the second one reads:

  [Elab.app.propagateExpectedType] propagation postponed, resulting type depends on arguments that have not yet been elaborated: [Error pretty printing expression: unexpected bound variable #0. Falling back to raw printer.]
      Eq.{1} Nat (Hom.toFun Nat Nat #0 (OfNat.ofNat.{0} Nat 0 (instOfNatNat 0))) (Hom.toFun Nat Nat #0 (OfNat.ofNat.{0} Nat 0 (instOfNatNat 0)))
Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Lingua principale
Lean
Stelle
10.3k
Fork
1.1k
Merge medio
16h 45m
PR unite (30g)
361

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.