Instance-typed mvar fallback from #14624 often runs a nested instance search that cannot succeed
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 42/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 領域
- compilers
調査の方向性
Start with Lean/Meta/ExprDefEq.lean, especially checkTypesAndAssign and synthInstanceTypedMVarAndUnify, then read the regression context in tests/elab/9077.lean. Run the provided fbmini2.lean reproduction with instance tracing and compare the trace across Lean versions. Done means the unnecessary nested Modu Q A search is avoided while the existing regression test and a focused test for this case pass.
索引モデルが 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.
Description
Since #14624 (the fix for #9077), when assigning an instance-argument metavariable fails its type check, checkTypesAndAssign (Lean/Meta/ExprDefEq.lean) falls back to synthInstanceTypedMVarAndUnify, which runs synthPending on the metavariable. In practice, most of those nested searches cannot succeed: the candidate being unified still has one of its own instance arguments open, and instance search has already failed for that argument (it is in the type class cache as a failure). The nested search then runs to failure anyway, and stuck results are not cached, so it can repeat.
The check itself is right, and this is not about reverting it: it would be enough to skip the nested search in that one situation and reject the assignment directly.
Context
We hit this while working on the RH cleanup (https://github.com/leanprover-community/riemann-hypothesis). Several slow instance searches happen there and in Mathlib itself, turning out to be in the nested search that #14624's fallback starts. Per my AI it was 1.5% of all heartbeats and 13% of instance search. We first suspected instance priorities, but switching the fallback off (as a measurement only) removed most of the cost, which is what led to the reproduction below.
Steps to Reproduce
/-!
Import-free copy of the shape behind the slow `map_sub` (Mathlib's `AddMonoidHomClass` for a ring hom).
* `AMHC F M` plays `AddMonoidHomClass`; `LMC F R M` plays `SemilinearMapClass` (the scalars `R` are an `outParam`,
and its `Modu R M` instance argument is therefore still a metavariable while `LMC` is searched);
* `fixedQ` plays `RingHomClass.toLinearMapClassNNRat`: over the fixed scalars `Q`, for a `DivSr` only;
* `A` is an `Sr` but not a `DivSr`, so `fixedQ` cannot apply, and `Modu Q A` does not exist.
`grep -c "new goal Modu Q A"` on the output counts the searches for `Modu Q A` that instance search starts while
trying `fixedQ`. No instance needs `Modu Q A` as a premise here: any such search comes from the unifier.
-/
set_option autoImplicit false
class AddMon (M : Type) where
zero : M
class Sr (R : Type) extends AddMon R where
one : R
class DivSr (R : Type) extends Sr R where
inv : R → R
class Modu (R M : Type) [Sr R] [AddMon M] where
smul : R → M → M
class LMC (F : Type) (R : outParam Type) (M : Type) [Sr R] [AddMon M] [Modu R M] : Prop where
ax : True
class AMHC (F M : Type) [AddMon M] : Prop where
ax : True
class RHC (F A : Type) [Sr A] : Prop where
ax : True
/-- The direct route; declared first, so tried last. -/
instance RHC.toAMHC (F A : Type) [Sr A] [RHC F A] : AMHC F A := ⟨trivial⟩
/-- The generic route. -/
instance LMC.toAMHC (F : Type) {R : Type} (M : Type) [Sr R] [AddMon M] [Modu R M] [LMC F R M] : AMHC F M :=
⟨trivial⟩
def Q : Type := Nat
instance : Sr Q := { zero := (0 : Nat), one := (1 : Nat) }
instance DivSr.toMod (A : Type) [DivSr A] : Modu Q A := ⟨fun _ a => a⟩
instance fixedQ (F A : Type) [DivSr A] [RHC F A] : LMC F Q A := ⟨trivial⟩
/-- Some ways to a `Modu Q _`, so that the failing search is visible in the trace. -/
class Tower (n : Nat) (A : Type) : Prop
instance modOfTower (A : Type) [Sr A] [Tower 0 A] : Modu Q A := ⟨fun _ a => a⟩
instance towerStep0 (A : Type) [Tower 1 A] : Tower 0 A := ⟨⟩
instance towerStep1 (A : Type) [Tower 2 A] : Tower 1 A := ⟨⟩
instance towerStep2 (A : Type) [Tower 3 A] : Tower 2 A := ⟨⟩
instance towerStep3 (A : Type) [Tower 4 A] : Tower 3 A := ⟨⟩
instance towerStep4 (A : Type) [Tower 5 A] : Tower 4 A := ⟨⟩
instance towerStep5 (A : Type) [Tower 6 A] : Tower 5 A := ⟨⟩
instance towerEnd (A : Type) [DivSr A] : Tower 6 A := ⟨⟩
def A : Type := Nat
instance : Sr A := { zero := (0 : Nat), one := (1 : Nat) }
def F : Type := A → A
instance : RHC F A := ⟨trivial⟩
set_option trace.Meta.synthInstance true in
example : AMHC F A := inferInstance
Save it as fbmini2.lean, run lean fbmini2.lean, then grep -c "new goal Modu Q A" on the trace output: 0 on v4.33.1, 1 on v4.35.0 through nightly-2026-11-25.
Expected behavior: no search for Modu Q A (no instance has it as a premise, so only the fallback can start one). That's how it is on v4.33.1.
Actual behavior: one such search on v4.35.0, v4.36.0, v4.37.0-rc1 and nightly-2026-11-25 (146 trace lines instead of 61).
In Mathlib, map_sub for a ring hom of a nested polynomial ring takes 9,611 heartbeats, of which almost all is three such nested searches (Module ℚ≥0/ℚ/ℂ on the polynomial ring). With the fallback switched off it is 1,151.
Across a large project (the RH formalization, 801 files), the fallback costs about 1.5% of all heartbeats and 13% of instance search.
A possible fix: skip the nested search when the value or the metavariable's type contains an unassigned instance-argument metavariable whose (metavariable-free) type is already a cached failure, and reject the assignment directly. We tried this as a prototype. It is AI-written, so please treat it as a sketch: https://gist.github.com/roy7/053f2c82baa046da20b7d7e27198b69f
tests/elab/9077.leanstill passes, and so does a new test in the patch. Lean's test suite: 4,955 of 4,957 tests pass (one skipped; one Lake cache test fails the same way without the patch).- With logging, the nested search never succeeded in that situation, in about 59,000 uses across Mathlib and the RH project. At Lean's default
maxSynthPendingDepth 1it never succeeded there either (about 42,000 uses in the RH project). - Every constant in both comes out identical, and most of the cost goes away (
map_sub9,611 → 1,226).
It isn't a proof that the skip is always safe: in principle unification could still fill such an argument some other way.
cc @datokrat (author of #14624)
Versions
v4.37.0-rc1 (0b77e129) and nightly-2026-11-25, Linux x86_64.
The analysis and prototype were done with an AI assistant (Claude Code). I checked the results with adversarial review by a separate model.
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
- 平均マージ
- 16時間 45分
- マージ済み PR(30日)
- 361
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
leanprover/lean4 のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
leanprover/lean4#16876 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
leanprover/lean4#16842 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
leanprover/lean4#16841 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
leanprover/lean4#16840 ·
メンテナーはふだん 1 日以内に返信
-
doc: stray "public" in Bool and HEq documentation再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
leanprover/lean4#16825 ·
メンテナーはふだん 1 日以内に返信
leanprover/lean4 の issue をすべて見る
似ている issue
-
bug core-lang
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
Call-argument syntax diagnostic needs example spacing and removal of confusing trailing sentenceオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
mw00/project-maya#1368 ·
メンテナーはふだん 2 日以内に返信
-
test(bind-json): legacy Solidity fixture requires an unavailable solc on Linux ARM64対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
foundry-rs/foundry#18828 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信