Kernel: recursor binder names depend on object sharing in the input (`is_eqp` in `is_def_eq_core`)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 15/100
Direzione di ricerca
Start in src/kernel/type_checker.cpp at the two is_eqp tests in is_def_eq_core (~lines 1168 and 1194), and read try_unfold_proj_app in the same file for the value-comparison form the reporter suggests. Then read whnf_core's m_whnf_core cache to understand why a hit can return a value-equal copy. Run the Repro.lean in the issue with lean --run Repro.lean; done means the shared and unshared runs print the same binder name for f_ih while the control is unchanged and kernel tests still pass.
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.
- Test your test case against the latest release (v4.37.0-rc1) and
mastersource (5b6d9d25ed, 2026-11-20). - When reporting a perceived soundness bug: does it affect
lake check --paranoid?
Not a soundness bug: every accept/reject answer agrees. Only binder names differ.
Description
The kernel's output for an inductive declaration is not a function of the declaration's
value. Two Declarations that are equal (==) but differ in whether one subterm is
shared in memory produce recursors with different binder names.
The cause is the two pointer-identity tests in type_checker::is_def_eq_core
(src/kernel/type_checker.cpp, master):
:1168 if (!is_eqp(t_n, t) || !is_eqp(s_n, s)) { ... quick_is_def_eq(t_n, s_n) ... }
:1194 if (!is_eqp(t_n_n, t_n) || !is_eqp(s_n_n, s_n))
return is_def_eq_core(t_n_n, s_n_n);
whnf_core can return a m_whnf_core cache hit whose stored object is a value-equal copy
of its argument. In practice these are the (p, p) entries for projections already in normal
form. is_eqp then reports "changed" when nothing changed, and line 1194 re-runs
is_def_eq_core from the top on the post-lazy-delta terms. That re-run's
is_def_eq_proof_irrel infers the type of a different term than the first pass did, and this
adds new m_whnf entries. A later whnf of an alpha-equivalent term then hits one of those
entries, and the cached term's binder names leak into the result. Here that result is the
whnf of a constructor field type in add_inductive_fn, so the leaked names end up in
I.rec.
Steps to Reproduce
Save the program in "Full reproducer" below as Repro.lean and run lean --run Repro.lean. It builds a small environment through
Environment.addDeclCore and adds the same inductive I twice:
structure Box2 : Type 2 := mk :: (val : Type 1) (extra : Type -> Type)
def NatA : Box2.val (Box2.mk Type (fun A => (x : Nat) -> A)) := Nat
structure S : Type := mk :: (f1 : NatA); axiom q : S
def F : Nat -> Type := fun _ => NatA
def c' : (n : Nat) -> F n := fun _ => q.1 -- this q.1 is the object Q
def G : Nat -> Nat := fun _ => 0
inductive I : Type
| mk : (y : Nat) -> (h : G (Nat.succ Q'') = G (c' y)) ->
(f : @Eq.rec Nat (G (Nat.succ Q'')) (fun _ _ => Type -> Type)
(Box2.extra (Box2.mk Type (fun A => (z : Nat) -> A))) (G (c' y)) h I) -> I
In the first run Q'' is the same object as Q. In the second it is a fresh, equal S.1 q
node. The program prints the binder name of f's inductive hypothesis in I.rec.
Expected behavior: the same name in both runs.
Actual behavior (Lean 4.37.0-rc1, also 4.35.0):
counterexample (a deterministic kernel prints the same name twice):
shared=true (Q'' and Q same object: true, equal values: true) -> binder of f_ih in I.rec: (some z)
shared=false (Q'' and Q same object: false, equal values: true) -> binder of f_ih in I.rec: (some x)
control, NatA : Type:
shared=true (Q'' and Q same object: true, equal values: true) -> binder of f_ih in I.rec: (some z)
shared=false (Q'' and Q same object: false, equal values: true) -> binder of f_ih in I.rec: (some z)
In the control, NatA : Type, so no reduction is needed in its type. It gives z both times.
Impact
- Not a soundness issue: binder names are not part of the theory.
- The kernel's output depends on the in-memory sharing of its input, which is not part of the
value. Sharing differs between elaboration,.oleanloading (compacted regions), and
replay/export tools (lean4checker,comparator,Lean.Replay), so the same declaration can
produce recursors that print differently. Tools that compare kernel output structurally
including binder names (e.g. comparator challenge-vs-solution checks) may see spurious
differences. - Formally,
whnf/is_def_eqcannot be specified as functions of their input values, which
blocks verification efforts (found while verifying the kernel with VeriFast).
Suggested fix
Compare by value at both sites. operator!= on expr (expr_eq_fn, which ignores binder
names) keeps the is_eqp fast path. try_unfold_proj_app already uses this form:
if (t_n != t || s_n != s) { ... }
...
if (t_n_n != t_n || s_n_n != s_n)
return is_def_eq_core(t_n_n, s_n_n);
When the objects are identical, the behaviour is unchanged. When they are only equal, the
kernel takes the "nothing changed" path, as it already does for the shared input. Both
inputs then follow the same path.
Versions
Lean (version 4.37.0-rc1, x86_64-unknown-linux-gnu, commit 0b77e12952ad, Release). Same code on
master at 5b6d9d25ed.
Additional Information
This is related to, but distinct from, #15706 (order dependence via is_def_eq_binding and
m_eager_reduce). Here a single query on fresh checker state depends on pointer identity, not
on the order of earlier queries.
Full reproducer (Repro.lean)
Repro.lean
/-
Kernel: the binder names of a generated recursor depend on whether two value-equal
subterms of the declaration are the same object in memory (`is_eqp` in `is_def_eq_core`).
Run: lean --run Repro.lean (tested on Lean v4.37.0-rc1)
The same inductive `I` is added twice. The two `Declaration`s are equal as values; only the
sharing of one `S.1 q` node differs. The binder name of the inductive hypothesis in `I.rec`
comes out `z` in one run and `x` in the other. The control (`NatA : Type`, so nothing has to
be reduced in its type) prints `z` both times.
-/
import Lean
open Lean
def add (e : Environment) (d : Declaration) (what : String) : IO Environment := do
match e.addDeclCore 0 0 d none with
| .ok e' => pure e'
| .error ex =>
IO.println s!" REJECTED {what}: {← (ex.toMessageData {}).toString}"
throw (IO.userError "stop")
/-- A fresh `S.1 q` node; `i` is 0 at run time, so the compiler cannot share the result. -/
@[noinline] def mkQ (i : Nat) : Expr := .proj `S i (mkConst `q)
def nat : Expr := mkConst ``Nat
def ty0 : Expr := .sort (.succ .zero) -- Type
def ty1 : Expr := .sort (.succ (.succ .zero)) -- Type 1
def ty2 : Expr := .sort (.succ (.succ (.succ .zero))) -- Type 2
def defn (n : Name) (t v : Expr) : Declaration :=
.defnDecl { name := n, levelParams := [], type := t, value := v,
hints := .regular 1, safety := .safe }
/-- `Box2.mk Type (fun (A : Type) => (bn : Nat) -> A)` -/
def mkM (bn : Name) : Expr :=
mkApp2 (mkConst `Box2.mk) ty0 (.lam `A ty0 (.forallE bn nat (.bvar 1) .default) .default)
/-- Builds the environment and the inductive `I`. `shared`: the `S.1 q` in `I.mk`'s type is the
very object stored in `c'`'s value. `control`: `NatA : Type` instead of
`NatA : Box2.val (...)`. Prints the binder name of `I.mk`'s inductive hypothesis in `I.rec`. -/
unsafe def run (control shared : Bool) (z0 z1 : Nat) : IO Unit := do
let env0 ← importModules #[{ module := `Init }] {} 0
-- structure Box2 : Type 2 := mk :: (val : Type 1) (extra : Type -> Type)
let mut env ← add env0 (.inductDecl [] 0 [{ name := `Box2, type := ty2, ctors := [{ name := `Box2.mk, type := .forallE `val ty1 (.forallE `extra (.forallE `a ty0 ty0 .default) (mkConst `Box2) .default) .default }] }] false) "Box2"
-- def NatA : Box2.val (Box2.mk Type (fun A => (x : Nat) -> A)) := Nat (control: NatA : Type)
env ← add env (defn `NatA (if control then ty0 else .proj `Box2 0 (mkM `x)) nat) "NatA"
-- structure S : Type := mk :: (f1 : NatA); axiom q : S
env ← add env (.inductDecl [] 0 [{ name := `S, type := ty0, ctors := [{ name := `S.mk, type := .forallE `f1 (mkConst `NatA) (mkConst `S) .default }] }] false) "S"
env ← add env (.axiomDecl { name := `q, levelParams := [], type := mkConst `S, isUnsafe := false }) "q"
-- def F : Nat -> Type := fun _ => NatA
env ← add env (defn `F (.forallE `n nat ty0 .default) (.lam `n nat (mkConst `NatA) .default)) "F"
-- def c' : (n : Nat) -> F n := fun _ => q.1 (Q : the object stored here)
let Q := mkQ z0
env ← add env (defn `c' (.forallE `n nat (mkApp (mkConst `F) (.bvar 0)) .default) (.lam `n nat Q .default)) "c'"
-- def G : Nat -> Nat := fun _ => 0
env ← add env (defn `G (.forallE `n nat nat .default) (.lam `n nat (mkRawNatLit 0) .default)) "G"
-- inductive I : Type | mk : (y : Nat) -> (h : G (Nat.succ Q'') = G (c' y)) ->
-- (f : @Eq.rec Nat (G (succ Q'')) (fun _ _ => Type -> Type)
-- (Box2.extra (Box2.mk Type (fun A => (z : Nat) -> A))) (G (c' y)) h I) -> I
let Q2 := if shared then Q else mkQ z1
let a := mkApp (mkConst `G) (mkApp (mkConst ``Nat.succ) Q2)
let b := mkApp (mkConst `G) (mkApp (mkConst `c') (.bvar 0)) -- y = #0 under (y)
let hTy := mkApp3 (mkConst ``Eq [.succ .zero]) nat a b
let motive := Expr.lam `w nat (.lam `e (mkApp3 (mkConst ``Eq [.succ .zero]) nat a (.bvar 0)) (.forallE `a ty0 ty0 .default) .default) .default
let minor := Expr.proj `Box2 1 (mkM `z)
-- under (y) (h): y = #1, h = #0
let b' := mkApp (mkConst `G) (mkApp (mkConst `c') (.bvar 1))
let D := mkAppN (mkConst ``Eq.rec [.succ (.succ .zero), .succ .zero]) #[nat, a, motive, minor, b', .bvar 0, mkConst `I]
let mkTy := Expr.forallE `y nat (.forallE `h hTy (.forallE `f D (mkConst `I) .default) .default) .default
env ← add env (.inductDecl [] 0 [{ name := `I, type := ty0, ctors := [{ name := `I.mk, type := mkTy }] }] false) "I"
let some ci := env.find? `I.rec | throw (IO.userError "no I.rec")
-- I.rec : {motive} -> (mk : (y : Nat) -> (h : _) -> (f : _) -> (f_ih : (BN : Nat) -> motive (f BN)) -> _) -> ...
let rec ihName (t : Expr) (k : Nat) : Option Name :=
match t, k with
| .forallE _ _ b _, k+1 => ihName b k
| .forallE _ d _ _, 0 => some d.bindingName!
| _, _ => none
let mkMinor := ci.type.bindingBody!.bindingDomain!
IO.println s!" shared={shared} (Q'' and Q same object: {ptrAddrUnsafe Q2 == ptrAddrUnsafe Q}, equal values: {Q2 == Q}) -> binder of f_ih in I.rec: {ihName mkMinor 3}"
unsafe def main : IO Unit := do
IO.println s!"Lean {Lean.versionString}"
let z0 := (← IO.monoMsNow) * 0
let z1 := (← IO.monoMsNow) * 0
IO.println "counterexample (a deterministic kernel prints the same name twice):"
run false true z0 z1
run false false z0 z1
IO.println "control, NatA : Type:"
run true true z0 z1
run true false z0 z1
- Lingua principale
- Lean
- Stelle
- 10.3k
- Fork
- 1.1k
- Merge medio
- 19h 8m
- PR unite (30g)
- 421
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di leanprover/lean4
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16681 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
leanprover/lean4#16305 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
leanprover/lean4#16299 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16257 ·
I maintainer di solito rispondono entro 1 giorno
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
leanprover/lean4#15683 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di leanprover/lean4
Issue simili
-
bug fixed form parser medium priority tokenizer
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
swc-project/swc-next-issues#817 ·
-
Magma mode: div:=, mod:= and the other keyword assignments (join:=, meet:=, diff:=, sdiff:=, and:=, or:=, xor:=) are refusedForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
sagemathinc/sagebrush#1683 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
I maintainer di solito rispondono entro 1 giorno
-
microts: a model local that every branch assigns raises unused_assignments in the generated RustApertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 80/100
pocket-stack/pocketjs#756 ·