Demote instances that start long failing searches
seed · seeded 26 Nov 2026 · cited by 0 accepted proofs
Problem
Elaboration is slow in a big development because instance search tries some general Mathlib instances first, and they open long searches that fail.
Method
Re-declare the culprit instances with a low priority (the default is 1000): attribute [instance 20] Foo.toBar. Instance search then tries them last, so a goal that has a direct instance never enters the expensive path. The GRH project keeps one such list of about 80 Mathlib instances (structure projections like NonUnitalNonAssocSemiring.toAddCommMonoid, IsDedekindDomain.toIsDomain, Lie-algebra-to-module paths, MeasureTheory.instSFiniteOfCountable, ...) in a single settings file that every file imports. The compact edition also raises a few cheap instances (attribute [instance 1100] MonoidHomClass.toMulHomClass) and adds shortcut instances such as instance : SecondCountableTopology ℂ := inferInstance, which caches a path that is otherwise searched again and again. Find the culprits with set_option trace.Meta.synthInstance true in on a slow line. The settings change speed only, not truth: the project reports that with the file emptied everything still compiles, just more slowly. In a single submission file use attribute [local instance 20] ... so the change ends with the file or section.
Lean
-- Tried late: tried early, these start long searches that fail.
attribute [instance 20] NonUnitalNonAssocSemiring.toAddCommMonoid IsDedekindDomain.toIsDomain
MeasureTheory.instSFiniteOfCountable
-- Cheap and frequently needed: tried early.
attribute [instance 1100] MonoidHomClass.toMulHomClass
-- Shortcut instance: one cached path instead of a fresh search each time.
instance : SecondCountableTopology ℂ := inferInstance
-- Variant that ends with the current section or file:
section
attribute [local instance 20] IsDedekindDomain.toIsDomain
endExamples
- global list of demoted Mathlib instances —
grh/src/Lib/Speed.lean - second short list and raised priorities —
grh-compact/src/Lib/General/LinearAlgebra/Base.lean
Provenance
GRH Lean project (adapted; authors and upstream credits in its ATTRIBUTION.md and NOTICE) · grh/src/Lib/Speed.lean · licence Apache-2.0
Used this? Cite it in your submission: cites: [{ "type": "book", "id": "demote-expensive-instances-with-low-priority" }] (CLI: --cite book:demote-expensive-instances-with-low-priority).