`simp`: is it intended that an instance-implicit `Prop` argument goes to the discharger when instance search fails?
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
- 10/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Ambito
- compilers
Direzione di ricerca
The behaviour is in synthesizeArgs in src/Lean/Meta/Tactic/Simp/Rewrite.lean, where an unassigned instance-implicit Prop argument falls through to the discharger after instance search fails. The repro in the issue (cases 1 to 3) and the tactic.skipAssignedInstances option are the entry points. Done means a maintainer decision on whether discharging such arguments is intended, not a patch.
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
This is a question, not a bug report: is it intended that simp gives an instance-implicit argument to the discharger after instance search has failed on it?
When simp rewrites with a lemma, Lean.Meta.Simp.synthesizeArgs (src/Lean/Meta/Tactic/Simp/Rewrite.lean) fills the arguments that unification left open:
if (← instantiateMVars x).isMVar then
-- A hypothesis can be both a type class instance as well as a proposition,
-- in that case we try both TC synthesis and the discharger
-- (because we don't know whether the argument was originally explicit or instance-implicit).
if (← isClass? type).isSome then
if (← synthesizeInstance x type) then
continue
if (← isProp type) then
unless (← discharge?' thmId x type) do
return false
So an argument written [C a], with C a : Prop and no instance found, goes next to the discharger, which by default runs simp on C a with the whole simp set. The binder info bi is a loop variable here, but it is only read under tactic.skipAssignedInstances false. The code is the same on v4.37.0-rc1, nightly-2026-11-25 and master at 3740f8b515 (2026-11-26).
History, as far as we could find it:
- https://github.com/leanprover/lean4/pull/2266 (June 2023, for https://github.com/leanprover/lean4/issues/2265) added the "try both" branch and its comment. It was for arguments with a default binder: a lemma given as a term with holes (
simp [mul_div m _]) is abstracted over its metavariables, and the new binders are all explicit (elabSimpTheorem→abstractMVars, still so in 4.37). At that time an argument with an instance-implicit binder went to instance search only (if bi.isInstImplicit then unless (← synthesizeInstance x type) do return false). - https://github.com/leanprover/lean4/pull/3507 (February 2024) is titled "fix:
simpshould not try to synthesize instance implicit arguments that have been inferred by unification" and has no description. Its diff removes thebi.isInstImplicitbranch, so since then an unassigned instance-implicit argument also takes the "try both" path, whose comment was written for explicit binders. We found no statement on whether the unassigned case should be discharged. - https://github.com/leanprover/lean4/pull/3526 (two days later) brought the binder infos back into
synthesizeArgsfor the optiontactic.skipAssignedInstances.
None of these says whether discharging instance-implicit arguments was wanted or not, and we found no issue or PR about it. The discharger can also prove a class for which there is no instance (case 2 below), so any change here changes behaviour, and Mathlib relies on it in a few places (numbers below).
If it is intended, a separate question is whether a way to opt out (per class, per lemma, or an option) would make sense.
Context
We came across this in the RH formalization (https://github.com/leanprover-community/riemann-hypothesis, Lean v4.37.0-rc1 with Mathlib). Its Lib/Speed.lean has a simp lemma of priority 1001, IsEmpty α ↔ IsVoid α, where IsVoid is a synonym of IsEmpty that simp knows nothing about, so a discharge attempt on IsEmpty α ends after one rewrite. With that lemma erased in every file, the project's heartbeats go up by 3.6% (131.2M → 135.9M over the 13,553 commands of at least 1k heartbeats; trace.profiler heartbeats in maxHeartbeats units, that is raw/1000, the unit of the project's 4,000,000 limit, here and for all RH figures below). 166 commands get slower by more than 20% and 5k; the largest is Lib/Cohomology/SmoothBaseChange.lean:109, 49,319 → 254,433. One proof breaks, New/OneLift/ComparisonOperator/LineCoordinates.lean:505, because simp then closes a goal by discharge that the next tactic expected. The lemma only covers IsEmpty: in SmoothBaseChange.lean, a trace of all simp.discharge nodes (nested attempts and ordinary side conditions included) counts, in stock, IsEmpty 252, Subsingleton 103, Nonempty 48, Limits.HasZeroObject 43 and IsLocalHom 36.
In Mathlib (31ce06ea), 6,900 of the 103,072 declarations in the default simp set after import Mathlib (core and Std included) have an instance-implicit argument of a Prop-valued class that the left-hand side does not determine, over 568 classes. Among them Nontrivial 256, Nonempty 201, CharZero 121, Subsingleton 99, IsEmpty 94. That is a lower bound (declarations whose type the counting script could not open are skipped), and it only says where the path can be taken.
Steps to Reproduce
Save this as repro.lean and run lean repro.lean (no imports, no options):
/-- A `Prop`-valued class. -/
class Void (α : Sort u) : Prop where
elim : α → False
/-- A conditional rewrite whose side condition is an *instance-implicit* argument. -/
theorem forall_of_void {α : Sort u} [Void α] (p : α → Prop) : (∀ a, p a) ↔ True :=
⟨fun _ => trivial, fun _ a => (Void.elim a).elim⟩
/-- A `simp` lemma that lets the discharger make progress on `Void _` goals. -/
theorem void_iff {α : Sort u} : Void α ↔ (α → False) := ⟨fun h => h.elim, fun h => ⟨h⟩⟩
/-! ### 1. The discharger is called on `Void Nat` after instance search has failed. -/
set_option trace.Meta.Tactic.simp.discharge true in
example (p : Nat → Prop) (h : ∀ n, p n) : ∀ n, p n := by
fail_if_success simp only [forall_of_void]
exact h
/-! ### 2. The discharger can *close* such an argument: there is no instance `Void (Fin 0)`
here, and `simp` rewrites with `forall_of_void` anyway, by proving `Void (Fin 0)` itself. -/
theorem fin_zero_elim : (Fin 0 → False) ↔ True := ⟨fun _ => trivial, fun _ a => absurd a.2 (Nat.not_lt_zero _)⟩
set_option trace.Meta.Tactic.simp.discharge true in
set_option trace.Meta.Tactic.simp.rewrite true in
example (p : Fin 0 → Prop) : ∀ a, p a := by
simp only [forall_of_void, void_iff, fin_zero_elim]
/-! ### 3. With `tactic.skipAssignedInstances false` the same call stops at
the failed instance search and does not rewrite. -/
set_option tactic.skipAssignedInstances false in
set_option trace.Meta.Tactic.simp.discharge true in
example (p : Fin 0 → Prop) : ∀ a, p a := by
fail_if_success simp only [forall_of_void, void_iff, fin_zero_elim]
exact fun a => absurd a.2 (Nat.not_lt_zero _)
The output is the same on v4.37.0-rc1, nightly-2026-11-25 and nightly-2026-11-26:
- Case 1:
failed to synthesize instance Void Nat, thenforall_of_void discharge ❌️ Void Nat. - Case 2:
failed to synthesize instance Void (Fin 0), thenforall_of_void discharge ✅️ Void (Fin 0)(throughvoid_iffandfin_zero_elim, with a nested attempt that stops atmax depth), and finallyforall_of_void: ∀ (a : Fin 0), p a ==> True. There is no instanceVoid (Fin 0). - Case 3: only
failed to synthesize instance Void (Fin 0); no discharge, no rewrite.
Expected behavior: we don't know, hence the question. With the binder rule of lean4 PR 2266, cases 1 and 2 would stop at the failed instance search, as case 3 does.
Actual behavior: cases 1 and 2 above.
Cost of the extra call. cost.lean (in the gist; it imports Lean only for a tactic that reads IO.getNumHeartbeats) runs one default simp on a goal with five ∀, where forall_of_void and void_iff are @[simp]. forall_of_void matches every ∀; each time instance search fails on Void _, the discharger runs simp on Void _, void_iff turns that into another ∀, and forall_of_void matches again, down to the discharge depth limit. The rows below separate that cascade (default against no void_iff) from the single failed discharge (no void_iff against no discharger call). Raw heartbeats (divide by 1000 for maxHeartbeats units):
| v4.37.0-rc1 | nightly-2026-11-25 | |
|---|---|---|
| default | 41,093 | 41,549 |
tactic.skipAssignedInstances false (no discharger call) |
27,821 | 28,052 |
default, void_iff not in the simp set |
31,081 | 31,402 |
What Mathlib relies on
To see this, we built v4.37.0-rc1 with one change in synthesizeArgs: when instance search fails on an instance-implicit argument whose type is a Prop, return false instead of calling the discharger (the "Prop-only" variant). Each of Mathlib's 8,729 files was then elaborated on its own with that binary against the stock oleans, with Mathlib's lakefile options. A file counts as failing if it gives an error that way.
- 55 of 8,729 Mathlib files fail, with 76 errors. None of the 2,484 files of
src/Init,src/Stdandsrc/Leanfails; 7 of 802 RH files do. - Which classes (each error matched to a logged successful discharge within 12 lines; 5 of the 76 errors are unmatched):
IsEmpty45 errors in 36 files,Subsingleton11 in 8,Subgroup.Characteristic4,Nonempty3,Module.IsTorsionFree2, andLieModule.IsNilpotent,CategoryTheory.HomIsOver,CategoryTheory.Epi,Graph.Loopless,Infinite,Finiteone each. - Three patterns (from reading the sites and four traces):
IsEmptyof a proposition:IsEmpty.forall_iff [IsEmpty α] : (∀ a, p a) ↔ Truematches an implicationp → q, and the discharger provesIsEmpty pthroughisEmpty_Propand the hypotheses. For exampleMathlib/Algebra/Order/Monoid/Canonical/Basic.lean:47(IsEmpty.forall_iff discharge ✅ IsEmpty (b + a < b)). Most of the 36 files.- A class fact passed as a
simpargument:simp [characteristic_iSup, h]withh : ∀ i ∈ s, (H i).Characteristic;simp [Algebra.subsingleton R A];simp_all [Algebra.FiniteType.isNoetherianRing R]. - The
nontrivialitytactic: itssimpcall provesSubsingletongoals this way (7 of the sites, in 5 files; the error is "Could not prove goal assumingSubsingleton _").
- Every class that the discharger proved anywhere in Mathlib has instances (from 1 for
Graph.Looplessto 320 forNonempty;IsEmpty75,Subsingleton272). So a rule "discharge only classes without instances" would, by that count, keep none of these 55 files (not built).
How often the path runs
Same toolchain, with stock behaviour plus one log line per instance-implicit argument that instance search left unassigned, over all 8,729 Mathlib files (no errors):
- 5,629 files take the path at least once.
- The discharger was called 546,195 times on such a
Propargument (305,266 at discharge depth 0) and proved it 237 times (143 at depth 0), in 126 files. 71 of those 126 files elaborate without it under theProp-only variant. - The depth-0 calls took 3.41 billion raw heartbeats (3.41 million in
maxHeartbeatsunits, the unit of the RH figures), 99.7% of it in calls that failed. By class:IsEmpty89.4% (95,299 calls),Subsingleton2.3%,Nonempty1.1%,IsSMulApply0.9%,Limits.HasZeroObject0.7%. By lemma:IsEmpty.forall_iff32.2%,IsEmpty.exists_iff19.3%,Finset.univ_eq_empty11.9%. - Mathlib's total heartbeats were not measured, so there is no share of the whole.
So most of the cost and most of the reliance are both on IsEmpty.
Two variants we tried (data, not a proposal)
Both are one-line conditions after the failed synthesizeInstance (diffs against v4.37.0-rc1 in the gist). The "sketch" is if bi.isInstImplicit then return false. The Prop-only variant adds isProp type to that test. Same per-file method as above. The RH numbers are profiler heartbeats per command in maxHeartbeats units, against stock (commands of at least 1k on both sides), at RH commit 7c4ccbd64d.
| sketch | Prop-only |
|
|---|---|---|
| Mathlib files failing (of 8,729) | 133 (262 errors) | 55 (76 errors) |
files of Init/Std/Lean failing (of 2,484) |
2 | 0 |
| RH files failing (of 802) | 33 (45 errors) | 7 (7 errors) |
| RH heartbeats against stock, commands without an error in the variant | −1.95% over 13,487 commands (35 erroring commands left out) | −0.57% over 13,532 commands (8 erroring commands left out) |
| RH commands that hit the project's 4,000,000 limit | 2 | 0 |
- The sketch also returns on instance arguments that are not propositions (
Fintype ?α,Module ?R M,Monad ?m…). Stock leaves those pending and goes on, and at many sites the rewrite still succeeds. That alone accounts for 78 of the 133 Mathlib files, both core files and 26 of the 33 RH files. The 55 and the 78 Mathlib files are disjoint. In 75 of the 78 the argument's type has metavariables (the expression is not fully elaborated yet:simpa using e,mod_cast e,simp_rw … at). The two core files areInit.Data.Iterators.Lemmas.Combinators.FilterMap(9 errors) andStd.Do.Triple.SpecLemmas(1 error), next to unresolvedMonad ?m,MonadExceptOfandStd.Do.WParguments. - The two RH percentages are on different bases (each leaves out its own erroring commands), and the sketch's larger gain comes with its 26 extra failing files. The left-out commands include the two limit hits under the sketch (
PAdicGauge.lean:142, 4,531 in stock;SchwartzBruhat.lean:328, below 1k in stock), which together cost +8.0M. Neither percentage is on the base of the +3.6% in "Context". - In RH, under either variant, erasing the
IsVoidlemma moves the total by tens of heartbeats: −60 of 136,605,257 under the sketch, +23 of 130,468,091 underProp-only (all commands of at least 1k on both sides, erroring ones included; the sketch's total contains the two limit hits), and the proof that breaks without it in stock elaborates again. - With the sketch,
repro.leancase 2 no longer rewrites (simpmade no progress), andcost.lean's default case goes from 41,093 to 27,905. Lean'sctestsuite with the sketch: 221 of 221simptests pass; 4,954 of 4,956 tests in all, one skipped, and one fails because it needs the network (async/elab/async_dns.lean; not compared with an unpatched build). These were not run with theProp-only variant. - Not built: discharge only for a user-supplied discharger (
simp (disch := …)), a per-class opt-out, an option.
Limits of the method:
- Each file is elaborated alone against stock oleans; this is not a rebuild. It doesn't cover failures caused by an import that failed, nor a file that still elaborates but exports a changed definition or instance (its importers saw the stock one).
- The
Prop-only numbers come from an instrumented build that switches between the variants by an environment variable (also in the gist), not from a separate build. Its heartbeat numbers include that lookup. - The non-
Propsplit (78 files) was run on the 133 failing files only, not on all of Mathlib. - The class of each error is matched by a 12-line window, and the three patterns come from reading the sites and four traces, not from a trace of every site.
- No fixes for the failing proofs were tried, so how hard they would be to adapt is not measured.
Files (https://gist.github.com/roy7/85536d5a7c126af3603ded0e8c43ca6f): repro.lean, cost.lean, simp-instarg-sketch.patch, simp-instarg-instrumented.patch (the variants, switched by LEAN_L13; 3 is Prop-only), mathlib-prop-only-files.txt and mathlib-prop-only-errors.tsv (the 55 files and 76 errors), mathlib-sketch-errors.tsv (the 262 errors), mathlib-discharge-summary.txt (the call counts above).
Versions
v4.37.0-rc1 (0b77e129) and nightly-2026-11-25 (b8d19876fd), Linux x86_64. Mathlib 31ce06ea.
The analysis, measurements and patches were done with an AI assistant (Claude Code), and reviewed adversarially by a second AI model.
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
- 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 68/100
leanprover/lean4#16876 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
leanprover/lean4#16842 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16841 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
leanprover/lean4#16840 ·
I maintainer di solito rispondono entro 1 giorno
-
doc: stray "public" in Bool and HEq documentationForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
leanprover/lean4#16825 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di leanprover/lean4
Issue simili
-
bug core-lang
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Call-argument syntax diagnostic needs example spacing and removal of confusing trailing sentenceAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
mw00/project-maya#1368 ·
I maintainer di solito rispondono entro 2 giorni
-
test(bind-json): legacy Solidity fixture requires an unavailable solc on Linux ARM64Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
foundry-rs/foundry#18828 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno