Skip to content

[Merged by Bors] - chore: unify LocalizedModule and OreLocalization#31862

Closed
erdOne wants to merge 20 commits intoleanprover-community:masterfrom
erdOne:erd1/ore-module4
Closed

[Merged by Bors] - chore: unify LocalizedModule and OreLocalization#31862
erdOne wants to merge 20 commits intoleanprover-community:masterfrom
erdOne:erd1/ore-module4

Conversation

@erdOne
Copy link
Copy Markdown
Member

@erdOne erdOne commented Nov 20, 2025

We redefine LocalizedModule to be an abbrev of OreLocalization S M so that localization of a ring and localization of a module is now defeq. This is very useful to unify downstream constructions, in particular ModuleCat.tilde and Spec.structureSheaf.

Some of the declarations are switched to reducible to avoid diamonds.
This causes a significant performance regression, most notabaly in Mathlib/AlgebraicGeometry/AffineSpace.lean.

We shall investigate if there are ways to improve performances. For example by introducing
typeclasses to unify the two constructions on this and LocalizedModule, or by marking some
downstream constructions (e.g. Spec.structureSheaf) as irreducible.


Open in Gitpod

@github-actions
Copy link
Copy Markdown

github-actions bot commented Nov 20, 2025

PR summary 88c480406b

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff

+ algebraOfIsLocalization
+ instance (priority := 900) algebra' {A : Type*} [Semiring A] [Algebra R A] :
+ instance (priority := 900) {A : Type*} [CommRing A] [Algebra R A] {S : Submonoid R} :
+ instance (priority := 900) {A : Type*} [CommSemiring A] [Algebra R A] {S : Submonoid R} :
+ instance (priority := 900) {A : Type*} [Ring A] [Algebra R A] {S : Submonoid R} :
+ instance : Module ℚ≥0 (DivisibleHull M) := LocalizedModule.moduleOfIsLocalization ..
+ instance [One X] : One X[S⁻¹]
+ mk_mul_mk'
+ moduleOfIsLocalization
+ mul
+ oreEqv_eq_r
+ oreSetComm_oreDenom
+ oreSetComm_oreNum
+ smulOfIsLocalization
++ instance (U : (Opens (PrimeSpectrum.Top R))ᵒᵖ) (x : U.unop) :
++ instance (priority := 900) {A : Type*} [Semiring A] [Algebra R A] {S : Submonoid R} :
++- numeratorRingHom
- algebra'
- hasNatSMul
- instance : Add (LocalizedModule S M)
- instance : AddCommMonoid (LocalizedModule S M)
- instance : One R[S⁻¹]
- instance : SMul T (LocalizedModule S M)
- instance : Zero (LocalizedModule S M)
- instance {A : Type*} [CommRing A] [Algebra R A] {S : Submonoid R} :
- instance {A : Type*} [CommSemiring A] [Algebra R A] {S : Submonoid R} :
- instance {A : Type*} [Ring A] [Algebra R A] {S : Submonoid R} :
- instance {A : Type*} [Semiring A] [Algebra R A] : Algebra T (LocalizedModule S A)
- instance {M : Type*} [AddCommGroup M] [Module R M] : AddCommGroup (LocalizedModule S M)
- instance {M : Type*} [AddCommGroup M] [Module R M] : Neg (LocalizedModule S M)
- isModule
- isModule'
-- instance {A : Type*} [Semiring A] [Algebra R A] {S : Submonoid R} :

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


Decrease in tech debt: (relative, absolute) = (1.44, 0.03)
Current number Change Type
1499 -8 backwards compatibility flags
1471 -8 privateInPublic flags
50 -1 adaptation notes

Current commit cb5af88568
Reference commit 88c480406b

You can run this locally as

./scripts/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@erdOne erdOne requested a review from chrisflav November 21, 2025 00:53
@chrisflav
Copy link
Copy Markdown
Member

!bench

@leanprover-radar
Copy link
Copy Markdown

leanprover-radar commented Nov 21, 2025

Benchmark results for 34b2fa7 against b7543c7 are in! @chrisflav

Major changes (6)
  • build/module/Mathlib.AlgebraicGeometry.AffineSpace//instructions changed by +125.4G (🟥) compared to estimated value based on build/module/Mathlib.AlgebraicGeometry.AffineSpace//lines (+125.4G (🟥) compared to previous value).
  • build/module/Mathlib.AlgebraicGeometry.Modules.Tilde//instructions changed by +10.6G (🟥) compared to estimated value based on build/module/Mathlib.AlgebraicGeometry.Modules.Tilde//lines (+12.4G (🟥) compared to previous value).
  • build/module/Mathlib.AlgebraicGeometry.Restrict//instructions changed by -38.9G (✅) compared to estimated value based on build/module/Mathlib.AlgebraicGeometry.Restrict//lines (-38.9G (✅) compared to previous value).
  • build/module/Mathlib.GroupTheory.DivisibleHull//instructions changed by +10.2G (🟥) compared to estimated value based on build/module/Mathlib.GroupTheory.DivisibleHull//lines (+10.6G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.NormalClosure//instructions changed by +15.7G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.NormalClosure//lines (+15.7G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.RingHom.Locally//instructions changed by +10.0G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.RingHom.Locally//lines (+10.0G (🟥) compared to previous value).
Minor changes (11)
  • build/module/Mathlib.Algebra.Module.FinitePresentation//instructions changed by -9.7G (✅) compared to estimated value based on build/module/Mathlib.Algebra.Module.FinitePresentation//lines (-9.7G (✅) compared to previous value).
  • build/module/Mathlib.Algebra.Module.LocalizedModule.Basic//instructions changed by +9.0G (🟥) compared to estimated value based on build/module/Mathlib.Algebra.Module.LocalizedModule.Basic//lines (+5.0G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.DedekindDomain.Different//instructions changed by +6.9G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.DedekindDomain.Different//lines (+6.9G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.DedekindDomain.Instances//instructions changed by +5.9G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.DedekindDomain.Instances//lines (+5.9G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.Flat.Domain//instructions changed by +7.6G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.Flat.Domain//lines (+7.6G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.IntegralClosure.IntegralRestrict//instructions changed by +7.2G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.IntegralClosure.IntegralRestrict//lines (+7.2G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.LocalProperties.Exactness//instructions changed by +5.9G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.LocalProperties.Exactness//lines (+5.9G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.LocalProperties.Projective//instructions changed by +6.2G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.LocalProperties.Projective//lines (+6.2G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.LocalRing.ResidueField.Ideal//instructions changed by +6.0G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.LocalRing.ResidueField.Ideal//lines (+6.0G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.Localization.Free//instructions changed by -5.2G (✅) compared to estimated value based on build/module/Mathlib.RingTheory.Localization.Free//lines (-5.2G (✅) compared to previous value).
  • build/module/Mathlib.RingTheory.Spectrum.Prime.FreeLocus//instructions changed by +8.4G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.Spectrum.Prime.FreeLocus//lines (+8.4G (🟥) compared to previous value).

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit 34b2fa7.
There were significant changes against commit b7543c7:

  Benchmark                                  Metric         Change
  ================================================================
- ~Mathlib.AlgebraicGeometry.AffineSpace     instructions    84.2%
- ~Mathlib.AlgebraicGeometry.Modules.Tilde   instructions    21.7%
+ ~Mathlib.AlgebraicGeometry.Restrict        instructions   -33.0%
- ~Mathlib.GroupTheory.DivisibleHull         instructions    25.7%
- ~Mathlib.RingTheory.NormalClosure          instructions     9.5%

@github-actions
Copy link
Copy Markdown

File Instructions %
build +261.607⬝10⁹ (+0.15%)
lint +5.803⬝10⁹ (+0.08%)
Mathlib.AlgebraicGeometry.AffineSpace +125.150⬝10⁹ (+84.20%)
Mathlib.RingTheory.NormalClosure +15.570⬝10⁹ (+9.53%)
Mathlib.AlgebraicGeometry.Modules.Tilde +12.409⬝10⁹ (+21.66%)
Mathlib.GroupTheory.DivisibleHull +10.557⬝10⁹ (+25.66%)
Mathlib.RingTheory.RingHom.Locally +9.997⬝10⁹ (+10.36%)
Mathlib.RingTheory.Spectrum.Prime.FreeLocus +8.687⬝10⁹ (+8.48%)
2 files, Instructions +7.0⬝10⁹
File Instructions %
Mathlib.RingTheory.Flat.Domain +7.484⬝10⁹ (+14.98%)
Mathlib.RingTheory.IntegralClosure.IntegralRestrict +7.299⬝10⁹ (+2.40%)
3 files, Instructions +6.0⬝10⁹
File Instructions %
Mathlib.RingTheory.DedekindDomain.Different +6.901⬝10⁹ (+1.83%)
Mathlib.RingTheory.LocalProperties.Projective +6.189⬝10⁹ (+18.17%)
Mathlib.RingTheory.DedekindDomain.Instances +6.143⬝10⁹ (+6.04%)
3 files, Instructions +5.0⬝10⁹
File Instructions %
Mathlib.RingTheory.LocalRing.ResidueField.Ideal +5.964⬝10⁹ (+6.81%)
Mathlib.RingTheory.LocalProperties.Exactness +5.882⬝10⁹ (+9.01%)
Mathlib.Algebra.Module.LocalizedModule.Basic +5.184⬝10⁹ (+3.88%)
File Instructions %
Mathlib.NumberTheory.FunctionField +4.35⬝10⁹ (+13.34%)
5 files, Instructions +3.0⬝10⁹
File Instructions %
Mathlib.FieldTheory.RatFunc.Basic +3.878⬝10⁹ (+1.48%)
Mathlib.RingTheory.Spectrum.Prime.Polynomial +3.831⬝10⁹ (+3.58%)
Mathlib.RingTheory.Smooth.Locus +3.765⬝10⁹ (+10.47%)
Mathlib.RingTheory.Ideal.Norm.RelNorm +3.530⬝10⁹ (+2.15%)
Mathlib.RingTheory.Regular.Depth +3.64⬝10⁹ (+12.78%)
8 files, Instructions +2.0⬝10⁹
File Instructions %
Mathlib.LinearAlgebra.Dimension.Localization +2.960⬝10⁹ (+4.08%)
Mathlib.RingTheory.TensorProduct.Nontrivial +2.855⬝10⁹ (+11.74%)
Mathlib.RingTheory.Localization.BaseChange +2.638⬝10⁹ (+4.02%)
Mathlib.Algebra.Module.LocalizedModule.Submodule +2.566⬝10⁹ (+4.69%)
Mathlib.NumberTheory.ClassNumber.FunctionField +2.559⬝10⁹ (+21.31%)
Mathlib.RingTheory.WittVector.Isocrystal +2.416⬝10⁹ (+4.01%)
Mathlib.RingTheory.Localization.Module +2.314⬝10⁹ (+6.76%)
Mathlib.RingTheory.Etale.Kaehler +2.283⬝10⁹ (+1.01%)
15 files, Instructions +1.0⬝10⁹
File Instructions %
Mathlib.Algebra.Module.LocalizedModule.Exact +1.926⬝10⁹ (+21.33%)
Mathlib.RingTheory.LocalRing.Module +1.838⬝10⁹ (+1.26%)
Mathlib.RingTheory.HahnSeries.HahnEmbedding +1.678⬝10⁹ (+10.72%)
Mathlib.RingTheory.Support +1.615⬝10⁹ (+6.07%)
Mathlib.RingTheory.Localization.Finiteness +1.585⬝10⁹ (+7.01%)
Mathlib.RingTheory.PicardGroup +1.585⬝10⁹ (+1.47%)
Mathlib.AlgebraicGeometry.StructureSheaf +1.556⬝10⁹ (+1.54%)
Mathlib.AlgebraicGeometry.AffineScheme +1.454⬝10⁹ (+1.15%)
Mathlib.RingTheory.Spectrum.Prime.ChevalleyComplexity +1.339⬝10⁹ (+0.83%)
Mathlib.Algebra.Lie.TraceForm +1.276⬝10⁹ (+0.95%)
Mathlib.RingTheory.ClassGroup +1.259⬝10⁹ (+1.30%)
Mathlib.AlgebraicGeometry.ProjectiveSpectrum.Basic +1.159⬝10⁹ (+1.38%)
Mathlib.RingTheory.Unramified.Locus +1.136⬝10⁹ (+7.95%)
Mathlib.FieldTheory.SeparableDegree +1.123⬝10⁹ (+0.96%)
Mathlib.RingTheory.LocalRing.ResidueField.Fiber +1.64⬝10⁹ (+2.79%)
3 files, Instructions -2.0⬝10⁹
File Instructions %
Mathlib.Algebra.Order.Monoid.Unbundled.Basic -1.47⬝10⁹ (-2.51%)
Mathlib.Tactic.Common -1.86⬝10⁹ (-21.70%)
Mathlib.CategoryTheory.Sites.CoverLifting -1.213⬝10⁹ (-4.63%)
File Instructions %
Mathlib.RingTheory.Localization.Free -5.147⬝10⁹ (-17.44%)
Mathlib.Algebra.Module.FinitePresentation -9.770⬝10⁹ (-5.79%)
Mathlib.AlgebraicGeometry.Restrict -38.822⬝10⁹ (-32.96%)
CI run Lakeprof report

/-- The ring homomorphism from `R` to `R[S⁻¹]`, mapping `r : R` to the fraction `r /ₒ 1`. -/
@[simps!]
def numeratorRingHom : R →+* R[S⁻¹] where
abbrev numeratorRingHom : R →+* R[S⁻¹] where
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this now an abbrev? And also in the other places?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is in the algebra instance and it needs to be reducibly equal.

{U : (Opens (PrimeSpectrum.Top R))ᵒᵖ} (x : U.unop)
(r : (Spec.structureSheaf R).val.obj U)
(m : Localizations M ↑x) :
r • m = (by exact r.1 x : Localization.AtPrime _) • m := rfl
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks bad, what is going on here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some bad defeq abuse. We are getting rid of this file anyways.

Comment on lines -205 to 206
private def smul' (r₁ : R) (s₁ : S) (r₂ : X) (s₂ : S) : X[S⁻¹] :=
private abbrev smul' (r₁ : R) (s₁ : S) (r₂ : X) (s₂ : S) : X[S⁻¹] :=
oreNum r₁ s₂ • r₂ /ₒ (oreDenom r₁ s₂ * s₁)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My experiments confirm that the abbrevs in this file are the cause of the massive slowdown in AffineSpace (which makes sense, because this affects the construction of Spec).
I understand that these changes are required to make the checks work at reducible transparency, but I am not sure this is worth it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it helps, I pushed my changes here: 9a0ddc2.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think things should be solved by making the operations on structure sheaf irreducible, which I will try after the refactor of it.

Copy link
Copy Markdown
Member

@chrisflav chrisflav Nov 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But having these declarations be globally reducibly, will also have other undesired performance effects. These should never be unfolded outside of the basic API files. Maybe we can use the module system to our advantage here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But to remember the fact that the two instances are defeq we always need to unfold them.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we would have to unify the definitions itself.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to unify the instances? As I said I don't think there is an easy way to do so. I could experiment on some generalization of OreSet but I don't think this is a necessity of this PR. To be precise, I expect that generality also comes with its own performance hit and I am not sure if it is worth it or not and we would only know if it gives a net improvement with this PR merged already. OTOH this PR is needed to progress and I would argue we would want to merge it even if there are significant performance regressions.

@chrisflav chrisflav added the awaiting-author A reviewer has asked the author a question or requested changes. label Nov 23, 2025
@erdOne
Copy link
Copy Markdown
Member Author

erdOne commented Nov 23, 2025

!bench

@leanprover-radar
Copy link
Copy Markdown

leanprover-radar commented Nov 23, 2025

Benchmark results for 0239558 against b7543c7 are in! @erdOne

Major changes (6)
  • build/module/Mathlib.AlgebraicGeometry.AffineSpace//instructions changed by +125.0G (🟥) compared to estimated value based on build/module/Mathlib.AlgebraicGeometry.AffineSpace//lines (+125.0G (🟥) compared to previous value).
  • build/module/Mathlib.AlgebraicGeometry.Modules.Tilde//instructions changed by +10.6G (🟥) compared to estimated value based on build/module/Mathlib.AlgebraicGeometry.Modules.Tilde//lines (+12.4G (🟥) compared to previous value).
  • build/module/Mathlib.AlgebraicGeometry.Restrict//instructions changed by -38.9G (✅) compared to estimated value based on build/module/Mathlib.AlgebraicGeometry.Restrict//lines (-38.9G (✅) compared to previous value).
  • build/module/Mathlib.GroupTheory.DivisibleHull//instructions changed by +10.4G (🟥) compared to estimated value based on build/module/Mathlib.GroupTheory.DivisibleHull//lines (+10.6G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.NormalClosure//instructions changed by +15.8G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.NormalClosure//lines (+15.8G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.RingHom.Locally//instructions changed by +10.1G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.RingHom.Locally//lines (+10.1G (🟥) compared to previous value).
Minor changes (11)
  • build/module/Mathlib.Algebra.Module.FinitePresentation//instructions changed by -9.8G (✅) compared to estimated value based on build/module/Mathlib.Algebra.Module.FinitePresentation//lines (-9.8G (✅) compared to previous value).
  • build/module/Mathlib.Algebra.Module.LocalizedModule.Basic//instructions changed by +9.4G (🟥) compared to estimated value based on build/module/Mathlib.Algebra.Module.LocalizedModule.Basic//lines (+5.4G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.DedekindDomain.Different//instructions changed by +6.8G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.DedekindDomain.Different//lines (+6.8G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.DedekindDomain.Instances//instructions changed by +5.9G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.DedekindDomain.Instances//lines (+5.9G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.Flat.Domain//instructions changed by +7.6G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.Flat.Domain//lines (+7.6G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.IntegralClosure.IntegralRestrict//instructions changed by +7.5G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.IntegralClosure.IntegralRestrict//lines (+7.5G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.LocalProperties.Exactness//instructions changed by +5.8G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.LocalProperties.Exactness//lines (+5.8G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.LocalProperties.Projective//instructions changed by +6.1G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.LocalProperties.Projective//lines (+6.1G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.LocalRing.ResidueField.Ideal//instructions changed by +6.0G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.LocalRing.ResidueField.Ideal//lines (+6.0G (🟥) compared to previous value).
  • build/module/Mathlib.RingTheory.Localization.Free//instructions changed by -5.0G (✅) compared to estimated value based on build/module/Mathlib.RingTheory.Localization.Free//lines (-5.0G (✅) compared to previous value).
  • build/module/Mathlib.RingTheory.Spectrum.Prime.FreeLocus//instructions changed by +8.8G (🟥) compared to estimated value based on build/module/Mathlib.RingTheory.Spectrum.Prime.FreeLocus//lines (+8.8G (🟥) compared to previous value).

@erdOne erdOne added t-algebra Algebra (groups, rings, fields, etc) and removed awaiting-author A reviewer has asked the author a question or requested changes. labels Nov 23, 2025
@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit 0239558.
There were significant changes against commit b7543c7:

  Benchmark                                  Metric         Change
  ================================================================
- ~Mathlib.AlgebraicGeometry.AffineSpace     instructions    84.2%
- ~Mathlib.AlgebraicGeometry.Modules.Tilde   instructions    21.7%
+ ~Mathlib.AlgebraicGeometry.Restrict        instructions   -32.9%
- ~Mathlib.GroupTheory.DivisibleHull         instructions    25.7%
- ~Mathlib.RingTheory.NormalClosure          instructions     9.6%

@github-actions
Copy link
Copy Markdown

File Instructions %
build +249.194⬝10⁹ (+0.14%)
lint +6.581⬝10⁹ (+0.10%)
Mathlib.AlgebraicGeometry.AffineSpace +125.203⬝10⁹ (+84.24%)
Mathlib.RingTheory.NormalClosure +15.656⬝10⁹ (+9.58%)
Mathlib.AlgebraicGeometry.Modules.Tilde +12.405⬝10⁹ (+21.65%)
Mathlib.GroupTheory.DivisibleHull +10.582⬝10⁹ (+25.72%)
Mathlib.RingTheory.RingHom.Locally +9.656⬝10⁹ (+10.00%)
Mathlib.RingTheory.Spectrum.Prime.FreeLocus +8.330⬝10⁹ (+8.13%)
2 files, Instructions +7.0⬝10⁹
File Instructions %
Mathlib.RingTheory.Flat.Domain +7.494⬝10⁹ (+15.00%)
Mathlib.RingTheory.IntegralClosure.IntegralRestrict +7.435⬝10⁹ (+2.44%)
3 files, Instructions +6.0⬝10⁹
File Instructions %
Mathlib.RingTheory.DedekindDomain.Different +6.968⬝10⁹ (+1.85%)
Mathlib.RingTheory.LocalProperties.Projective +6.182⬝10⁹ (+18.15%)
Mathlib.RingTheory.DedekindDomain.Instances +6.131⬝10⁹ (+6.03%)
3 files, Instructions +5.0⬝10⁹
File Instructions %
Mathlib.RingTheory.LocalRing.ResidueField.Ideal +5.841⬝10⁹ (+6.67%)
Mathlib.Algebra.Module.LocalizedModule.Basic +5.677⬝10⁹ (+4.25%)
Mathlib.RingTheory.LocalProperties.Exactness +5.631⬝10⁹ (+8.63%)
2 files, Instructions +4.0⬝10⁹
File Instructions %
Mathlib.FieldTheory.RatFunc.Basic +4.160⬝10⁹ (+1.59%)
Mathlib.NumberTheory.FunctionField +4.30⬝10⁹ (+13.33%)
4 files, Instructions +3.0⬝10⁹
File Instructions %
Mathlib.RingTheory.Spectrum.Prime.Polynomial +3.821⬝10⁹ (+3.57%)
Mathlib.RingTheory.Smooth.Locus +3.765⬝10⁹ (+10.47%)
Mathlib.RingTheory.Ideal.Norm.RelNorm +3.554⬝10⁹ (+2.16%)
Mathlib.RingTheory.Regular.Depth +3.231⬝10⁹ (+13.47%)
8 files, Instructions +2.0⬝10⁹
File Instructions %
Mathlib.LinearAlgebra.Dimension.Localization +2.967⬝10⁹ (+4.09%)
Mathlib.RingTheory.TensorProduct.Nontrivial +2.857⬝10⁹ (+11.75%)
Mathlib.RingTheory.Localization.BaseChange +2.621⬝10⁹ (+4.00%)
Mathlib.Algebra.Module.LocalizedModule.Submodule +2.576⬝10⁹ (+4.71%)
Mathlib.NumberTheory.ClassNumber.FunctionField +2.558⬝10⁹ (+21.31%)
Mathlib.RingTheory.WittVector.Isocrystal +2.533⬝10⁹ (+4.20%)
Mathlib.RingTheory.Localization.Module +2.326⬝10⁹ (+6.80%)
Mathlib.RingTheory.Etale.Kaehler +2.216⬝10⁹ (+0.98%)
15 files, Instructions +1.0⬝10⁹
File Instructions %
Mathlib.Algebra.Module.LocalizedModule.Exact +1.925⬝10⁹ (+21.32%)
Mathlib.AlgebraicGeometry.StructureSheaf +1.859⬝10⁹ (+1.84%)
Mathlib.RingTheory.LocalRing.Module +1.827⬝10⁹ (+1.25%)
Mathlib.RingTheory.HahnSeries.HahnEmbedding +1.689⬝10⁹ (+10.79%)
Mathlib.RingTheory.Support +1.678⬝10⁹ (+6.30%)
Mathlib.RingTheory.PicardGroup +1.592⬝10⁹ (+1.48%)
Mathlib.RingTheory.Localization.Finiteness +1.580⬝10⁹ (+6.99%)
Mathlib.AlgebraicGeometry.ProjectiveSpectrum.Basic +1.556⬝10⁹ (+1.85%)
Mathlib.AlgebraicGeometry.AffineScheme +1.412⬝10⁹ (+1.11%)
Mathlib.Algebra.Lie.TraceForm +1.409⬝10⁹ (+1.05%)
Mathlib.RingTheory.Spectrum.Prime.ChevalleyComplexity +1.305⬝10⁹ (+0.80%)
Mathlib.RingTheory.ClassGroup +1.158⬝10⁹ (+1.20%)
Mathlib.RingTheory.Unramified.Locus +1.139⬝10⁹ (+7.97%)
Mathlib.FieldTheory.SeparableDegree +1.128⬝10⁹ (+0.97%)
Mathlib.RingTheory.LocalRing.ResidueField.Fiber +1.51⬝10⁹ (+2.76%)
File Instructions %
Mathlib.RingTheory.Localization.Free -5.146⬝10⁹ (-17.44%)
Mathlib.Algebra.Module.FinitePresentation -9.763⬝10⁹ (-5.78%)
Mathlib.AlgebraicGeometry.Restrict -38.797⬝10⁹ (-32.94%)
CI run Lakeprof report

@chrisflav
Copy link
Copy Markdown
Member

This PR causes quite a performance regression. The reason is that some of the components that go into the construction of the ring structure on Localization were turned from defs to abbrevs. The consequence is that some declarations take significantly longer to typecheck, because Lean is unfolding more of the ring structure. Most notably, Mathlib.AlgebraicGeometry.AffineSpace got significantly slower and all slowdowns are of this form (see 9a0ddc2 for a commit with all the abbrevs switched back to defs, the performance regression vanishes in this case).

The reason why these components need to be abbrevs is that the goal of this PR is to make the ring structure on Localization S and LocalizedModule S R be reducibly def-eq. This will allow for significant simplifications in the development of ModuleCat.tilde. Namely, it will allow us to do the common algebraic geometry lecture note trick: "Observe that the construction of the structure sheaf also works for R replaced by any R-module. So everything that holds for the structure sheaf also holds for M tilde". After this PR, the structure sheaf itself will be refactored to be M tilde where M = R. Finally, the construction of M tilde can be made irreducible and we probably gain back the performance that we are losing now.

All in all, I think this PR is a major improvement over the status quo and we should be able to recover from this performance regression when the refactor is complete.

So, thanks!
maintainer merge?

@mattrobball
Copy link
Copy Markdown
Contributor

Why abbrev and not reducible?

@erdOne
Copy link
Copy Markdown
Member Author

erdOne commented Nov 28, 2025

I didn't know there was a difference. Should I be using reducible instead? Why is that?

@mattrobball
Copy link
Copy Markdown
Contributor

I don't know if inline affects the elaborator (and it very well might not). I can't determine this myself at the moment.

@mattrobball
Copy link
Copy Markdown
Contributor

Cursory look says it's just for the compile but I probably shouldn't be trusted given the constraints I'm working under

Copy link
Copy Markdown
Contributor

@mattrobball mattrobball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole dependence on Algebra for localization seems strange.

Ideally we can seal OreLocalization after things are designed correctly. But I am not sure if this possible.

abbrev _root_.LocalizedModule : Type max u v :=
OreLocalization S M

example {R} [CommSemiring R] (S : Submonoid R) : Localization S = LocalizedModule S R := by
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to a test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a test file. But these example are not tests but to showcase to the reader of this file that this defeq is holds by design, and that there are potential defeq diamonds so if one wants to find lemmas about the operations they should also look into the results about Localization. Hence I have kept them in this file as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be a comment if in the file itself. Unless the behavior is independent of the import choices (almost nothing), it needs to succeed with full imports if you care about it.

Please remove these.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your point of having a test file so I added it, but I don't understand the reason of removing the examples. They are illustrative examples to have. Comments are strictly worse as people cannot easily inspect the type of the mentioned declarations nor can they jump to the definition. Comments are also more prone to be out of sync with the actual declaration names.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless the behavior is independent of the import choices (almost nothing), it needs to succeed with full imports if you care about it.

Does this mean that I should switch to import Mathlib in the test file?

Copy link
Copy Markdown
Member Author

@erdOne erdOne Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example's create extra burden for linter authors for example. When there is nothing in the environment to look for, how do I make sure the example conforms or at least doesn't mess things up.

Would you mind expanding on this? I am failing to understand this, probably because I don't have a clue how linters work. What does linter look for in the environment? To what should the example conform? How are examples different from regular lemmas in this regard?

I don't see the value of them intramodule in any dimension to be honest.

To me they are just type-checked comments with infoview and they serve the same purposes as comments.

When do you want the defeq to hold? If for all users, then import Mathlib

Yes. I'll change this later.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you sell me on the value for your workflow?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you advocating for removing the example keyword from Lean? Or should it only be used in test files? No burden for linter authors if they're in test files?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you sell me on the value for your workflow?

So when I see a comment like "The multiplication on the localized module.
Note that this gives a diamond with the instance on R[S⁻¹] (which does not require commutativity),
but is defeq to it under with_reducible_and_instances.", I don't have an algorithm to find where the other instance on R[S⁻¹] is, and I have no info what the other instance requires as inputs etc. But if it is included in an example below, I can just inspect the type of it in the info view and find the definition of it very easily.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's valuable for certain purposes like demonstration or unit tests. In production code, it is less valuable. I don't think it should be removed.

def r (a b : M × S) : Prop :=
∃ u : S, u • b.2 • a.1 = u • a.2 • b.1

lemma oreEqv_eq_r : (OreLocalization.oreEqv S M).r = r S M := by
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make this the setoid?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OreLocalization is indeed defined as the quotient of the setoid of OreLocalization.oreEqv.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remind me why r exists again. On mobile so can't explore

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r used to be the relation defining LocalizedModule but we are switching to OreLocalization.oreEqv in this PR.

simp only [one_mul, smul_smul, ← mul_assoc] }
simp only [one_mul, smul_smul, ← mul_assoc, mul_right_comm] }

example : OreLocalization.instMonoid = LocalizedModule.instMonoid (A := R) (S := S) := by
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put this in a test.

See https://github.com/leanprover-community/mathlib4/pull/25671 for an approach to generalize this
but it requires right `R` actions on `R`-algebras.
-/
protected def mul {A : Type*} [Semiring A] [Algebra R A] {S : Submonoid R}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it strange we ever talk about Mul in something named module.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably just unfortunate naming of LocalizedModule but when the module is an algebra then the LocalizedModule is also an algebra.

mk a₁ s₁ * mk a₂ s₂ = mk (a₁ * a₂) (s₁ * s₂) := by rw [mk_mul_mk', mul_comm s₁ s₂]

instance {A : Type*} [Semiring A] [Algebra R A] {S : Submonoid R} :
instance (priority := 900) {A : Type*} [Semiring A] [Algebra R A] {S : Submonoid R} :
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are we avoiding?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prefer the OreLocalization.instSemiring instance on Localization S. I have added a comment explaining this.

change _ • mk _ _ = mk _ _ * mk _ _
rw [mk_mul_mk, smul'_mk, Algebra.smul_def, one_mul]

example : (algebra' : Algebra R (LocalizedModule S R)) = OreLocalization.instAlgebra := by
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again it should be a test.

lemma smul_section_apply (r : R) (U : Opens (PrimeSpectrum.Top R))
(s : (tildeInModuleCat M).obj (op U)) (x : U) :
(r • s).1 x = r • (s.1 x) := rfl
(r • s).1 x = r • (s.1 x) :=
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be salvaged later?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. Probably yes. But this isn't a really useful defeq to have.

@jcommelin jcommelin removed the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Dec 4, 2025
@mathlib4-merge-conflict-bot mathlib4-merge-conflict-bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Dec 19, 2025
@mathlib4-merge-conflict-bot
Copy link
Copy Markdown
Collaborator

This pull request has conflicts, please merge master and resolve them.

@jcommelin
Copy link
Copy Markdown
Member

Please turn the examples into (private) lemma example_foo : ....

bors d+

@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Dec 19, 2025

✌️ erdOne can now approve this pull request. To approve and merge a pull request, simply reply with bors r+. More detailed instructions are available here.

@ghost ghost added the delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). label Dec 19, 2025
@github-actions github-actions bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Dec 28, 2025
@erdOne
Copy link
Copy Markdown
Member Author

erdOne commented Dec 28, 2025

bors merge

mathlib-bors bot pushed a commit that referenced this pull request Dec 28, 2025
We redefine `LocalizedModule` to be an abbrev of `OreLocalization S M` so that localization of a ring and localization of a module is now defeq. This is very useful to unify downstream constructions, in particular `ModuleCat.tilde` and `Spec.structureSheaf`. 

Some of the declarations are switched to reducible to avoid diamonds.
This causes a significant performance regression, most notabaly in `Mathlib/AlgebraicGeometry/AffineSpace.lean`.

We shall investigate if there are ways to improve performances. For example by introducing
typeclasses to unify the two constructions on this and `LocalizedModule`, or by marking some
downstream constructions (e.g. `Spec.structureSheaf`) as irreducible.
@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Dec 28, 2025

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title chore: unify LocalizedModule and OreLocalization [Merged by Bors] - chore: unify LocalizedModule and OreLocalization Dec 28, 2025
@mathlib-bors mathlib-bors bot closed this Dec 28, 2025
kim-em pushed a commit to kim-em/mathlib4 that referenced this pull request Jan 6, 2026
…unity#31862)

We redefine `LocalizedModule` to be an abbrev of `OreLocalization S M` so that localization of a ring and localization of a module is now defeq. This is very useful to unify downstream constructions, in particular `ModuleCat.tilde` and `Spec.structureSheaf`. 

Some of the declarations are switched to reducible to avoid diamonds.
This causes a significant performance regression, most notabaly in `Mathlib/AlgebraicGeometry/AffineSpace.lean`.

We shall investigate if there are ways to improve performances. For example by introducing
typeclasses to unify the two constructions on this and `LocalizedModule`, or by marking some
downstream constructions (e.g. `Spec.structureSheaf`) as irreducible.
goliath-klein pushed a commit to PrParadoxy/mathlib4 that referenced this pull request Jan 24, 2026
…unity#31862)

We redefine `LocalizedModule` to be an abbrev of `OreLocalization S M` so that localization of a ring and localization of a module is now defeq. This is very useful to unify downstream constructions, in particular `ModuleCat.tilde` and `Spec.structureSheaf`. 

Some of the declarations are switched to reducible to avoid diamonds.
This causes a significant performance regression, most notabaly in `Mathlib/AlgebraicGeometry/AffineSpace.lean`.

We shall investigate if there are ways to improve performances. For example by introducing
typeclasses to unify the two constructions on this and `LocalizedModule`, or by marking some
downstream constructions (e.g. `Spec.structureSheaf`) as irreducible.
@YaelDillies YaelDillies deleted the erd1/ore-module4 branch April 3, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). t-algebra Algebra (groups, rings, fields, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.