Kernel reduces structures to constructors before comparing applied projections
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
- 49/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- compilers
Direzione di ricerca
Start in src/kernel/type_checker.cpp, especially is_def_eq_core, lazy_delta_proj_reduction, and the projection application path described in the report. Run the supplied Kernel.lean reproduction with lean Kernel.lean and compare the bare, functionType, and applied cases. Done means the kernel accepts all three without evaluating the enormous exponent in applied; no test file is named in the issue.
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.
(Not a soundness bug: the kernel rejects a valid proof.)
Description
The kernel's definitional equality check unfolds lazily when it compares two bare projections
s.i =?= t.i, but not when it compares two applied projections s.i a =?= t.i a. For an applied
projection, the kernel reduces s and t to constructors before it compares them. If that
reduction is expensive, the kernel does the expensive work even when one delta step makes s and
t syntactically equal.
Since mathlib4#41506, Mathlib's Set is a structure with one field Mem : α → Prop, so a ∈ s
unfolds to the applied projection s.Mem a. A kernel check of a ∈ s =?= a ∈ t with s and t
syntactically different can now take this path. Before mathlib4#41506, Set α was α → Prop,
and a ∈ s unfolded to s a. Lazy delta reduction then unfolded the head constant of s or t
and compared the arguments.
Context
is_def_eq_core in src/kernel/type_checker.cpp calls lazy_delta_proj_reduction only when both
sides are projections:
if (is_proj(t_n) && is_proj(s_n) && proj_sname(t_n) == proj_sname(s_n) && proj_idx(t_n) == proj_idx(s_n)) {
For s.i a, t_n is an application whose head is a projection, so this branch does not apply.
Lazy delta reduction does not help either. If neither side has a constant head,
lazy_delta_reduction_step returns DefUnknown. Its special case for projection applications,
try_unfold_proj_app, applies only when exactly one side has a constant head. The next step,
whnf_core(t_n) with cheap_proj = false, calls whnf on the projected structure and reduces it
to a constructor. This code is the same in v4.35.0, v4.36.0 and on master.
We found this in a downstream project after a toolchain and Mathlib update. A proof about a
VCVio computation that samples a 2^32-bit value checked on Lean v4.35.0 with a Mathlib from before
mathlib4#41506. After the update, the kernel refused it. The proof compares memberships in the
support of two forms of the computation. The kernel now reduces the support set to a constructor,
which runs the computation and evaluates 2 ^ (2 ^ 32).
Steps to Reproduce
The file below has no dependencies other than Lean. It adds each theorem with addDecl, so only
the kernel checks them. Additional Information explains why it bypasses the elaborator.
import Lean
open Lean Meta Elab Term
/-- A structure with one predicate field, like Mathlib's `Set` since mathlib4#41506. -/
structure PSet (α : Type) where
Mem : α → Prop
instance : Membership α (PSet α) := ⟨fun s a => s.Mem a⟩
/-- Reducing `big n` to a constructor computes `2 ^ n`. -/
def big (n : Nat) : PSet Nat := if 2 ^ n = 0 then ⟨fun _ => True⟩ else ⟨fun a => a = 0⟩
/-- `small` unfolds to `big (2 ^ 32)` in one delta step. -/
def small : PSet Nat := big (2 ^ 32)
/-- The same construction with a function type, like Mathlib's `Set` before mathlib4#41506. -/
def FSet (α : Type) := α → Prop
instance : Membership α (FSet α) := ⟨fun s a => s a⟩
def bigF (n : Nat) : FSet Nat := if 2 ^ n = 0 then fun _ => True else fun a => a = 0
def smallF : FSet Nat := bigF (2 ^ 32)
/-- Elaborate a statement `∀ xs, P → Q` and add the theorem `fun xs h => h` with `addDecl`.
The elaborator does not unify `P` with `Q`, so only the kernel checks that they are
definitionally equal. -/
def addIdentityTheorem (name : Name) (stmt : Term) : TermElabM Unit := do
let type ← elabType stmt
synthesizeSyntheticMVarsNoPostponing
let type ← instantiateMVars type
let value ← forallTelescope type fun xs _ => mkLambdaFVars xs xs.back!
addDecl (.thmDecl { name, levelParams := [], type, value })
/-- `#add_identity_theorem name : ∀ xs, P → Q` runs `addIdentityTheorem`. -/
elab "#add_identity_theorem " name:ident " : " stmt:term : command =>
Command.liftTermElabM <| addIdentityTheorem name.getId stmt
-- Accepted: the kernel compares the bare projections `small.Mem` and `(big (2 ^ 32)).Mem` with
-- `lazy_delta_proj_reduction`, which unfolds `small` before it reduces either structure.
#add_identity_theorem bare : ∀ P : (Nat → Prop) → Prop, P small.Mem → P (big (2 ^ 32)).Mem
-- Accepted: with a function type, `smallF a` and `bigF (2 ^ 32) a` have constant heads, and lazy
-- delta reduction unfolds `smallF` to make them syntactically equal.
#add_identity_theorem functionType : ∀ a : Nat, a ∈ smallF → a ∈ bigF (2 ^ 32)
-- Rejected: `a ∈ small` unfolds to the applied projection `small.Mem a`. The kernel reduces
-- `small` to a constructor before it compares the two sides, so it evaluates `2 ^ (2 ^ 32)`:
-- "(kernel) the kernel refused to evaluate `Nat.pow` because its second argument does not fit in
-- a 32-bit unsigned integer".
#add_identity_theorem applied : ∀ a : Nat, a ∈ small → a ∈ big (2 ^ 32)
- Save the file as
Kernel.lean. - Run
lean Kernel.lean.
Expected behavior: The kernel accepts all three theorems. For applied, the kernel unfolds
small to big (2 ^ 32), as it does for bare and functionType, and the two sides become
syntactically equal.
Actual behavior: The kernel accepts bare and functionType, and rejects applied:
Kernel.lean:51:0: error: (kernel) the kernel refused to evaluate `Nat.pow` because its second argument does not fit in a 32-bit unsigned integer
The size limits only make the extra work visible. If 2 ^ 28 replaces 2 ^ 32 in the file, the
kernel accepts bare and functionType and rejects applied because "the result would exceed the
maximum numeral size". If 2 ^ 20 replaces 2 ^ 32, the kernel accepts all three theorems, and
for applied it computes 2 ^ (2 ^ 20).
Versions
4.35.04.36.04.38.0-nightly-2026-11-19
The result is the same on all three versions. OS: Linux x86_64.
Additional Information
The reproduction bypasses the elaborator because the elaborator fails on these statements for a
different reason. With the definitions above, a proof that uses a hypothesis h : a ∈ small fails
to elaborate, even when the expected type is exactly the type of h:
-- warning: exponent 4294967296 exceeds the threshold 256, exponentiation operation was not evaluated
-- error: maximum recursion depth has been reached
-- The `constructorNameAsVariable` linter also fails here (leanprover/lean4#16612).
theorem identity : ∀ a : Nat, a ∈ small → a ∈ small := fun _ h => h
The same happens with FSet and smallF, and on all three versions. So it does not depend on
the projection, and this issue does not cover it.
A downstream workaround is to avoid kernel checks of a ∈ s =?= a ∈ t where s and t differ.
Rewrite with equation lemmas, whose right-hand sides match the definition bodies exactly, and
generalize expensive arguments before applying a lemma.
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
- 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 ·