Kernel reduces structures to constructors before comparing applied projections
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 49/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 領域
- compilers
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Lean
- スター
- 10.3k
- フォーク
- 1.1k
- 平均マージ
- 19時間 8分
- マージ済み PR(30日)
- 421
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
leanprover/lean4 のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
leanprover/lean4#16681 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
leanprover/lean4#16305 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
leanprover/lean4#16299 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
leanprover/lean4#16257 ·
メンテナーはふだん 1 日以内に返信
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
leanprover/lean4#15683 ·
メンテナーはふだん 1 日以内に返信
leanprover/lean4 の issue をすべて見る
似ている issue
-
bug fixed form parser medium priority tokenizer
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 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 refused対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
sagemathinc/sagebrush#1683 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
microts: a model local that every branch assigns raises unused_assignments in the generated Rustオープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
pocket-stack/pocketjs#756 ·