Skip to content

refactor: RingHom.ker take a RingHom not a RingHomClass - #46278

Open
mcdoll wants to merge 42 commits into
leanprover-community:masterfrom
mcdoll:ringhom_ker
Open

mcdoll wants to merge 42 commits into
leanprover-community:masterfrom
mcdoll:ringhom_ker

Conversation

@mcdoll

@mcdoll mcdoll commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

The definitions RingHom.ker and Ideal.comap now take a RingHom and not an arbitrary type of class RingHomClass. Ideal.map takes a plain function.

We use the "wrong coercion" RingHom.ofClass instead of AlgHom.toRingHom, because otherwise there are tons of coercion problems. Moving everything to using AlgHom.toRingHom will be a subsequent PR.

Tech debt increase: We use erw in 3 places since the is a mix of AlgHom.toRingHom and RingHomClass.toRingHom that should be untangled when removing the coercion from the later.


@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

PR summary 28e36eb24d

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

+ Quotient.coe_mkₐ
+ coe_mk
+ coe_mkQ
+ coe_toMonoidHom
+ map_swap_prod

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.

Declarations diff (Lean)

✅ Lean-aware diff — post-build, computed from the Lean environment (commit 28e36eb).

  • +12 new declarations
  • −11 removed declarations
+Algebra.QuasiFinite.instIsArtinianRingQuotientAtPrimeIdealMapCoeRingHomAlgebraMapOfLiesOver
-Algebra.QuasiFinite.instIsArtinianRingQuotientAtPrimeIdealMapRingHomAlgebraMapOfLiesOver
+Ideal.Quotient.coe_mk
+Ideal.Quotient.coe_mkₐ
+Ideal.Quotient.instIsPrimeQuotientMapCoeRingHomAlgebraMapMkOfLiesOver
-Ideal.Quotient.instIsPrimeQuotientMapRingHomAlgebraMapMkOfLiesOver
-Ideal.comap.congr_simp
+Ideal.instAlgebraQuotientComapAlgebraMap
-Ideal.instAlgebraQuotientComapRingHomAlgebraMap
+Ideal.instIsScalarTowerQuotientHPowKerAlgebraMapOfNat
-Ideal.instIsScalarTowerQuotientHPowKerRingHomAlgebraMapOfNat
+Ideal.instIsTwoSidedMapCoeRingHomOfRingHomSurjective
-Ideal.instIsTwoSidedMapRingHomOfRingHomSurjective
-Ideal.mapHom.congr_simp
+Ideal.map_swap_prod
+IsLocalization.instQuotientIdealAlgebraMapSubmonoidMapCoeRingHomAlgebraMap
-IsLocalization.instQuotientIdealAlgebraMapSubmonoidMapRingHomAlgebraMap
-RingEquiv.ofClass.congr_simp
+RingHom.coe_toMonoidHom
-RingHom.ker.congr_simp
+Submodule.coe_mkQ
+instIsPrincipalMapCoeRingHom
-instIsPrincipalMapRingHom

Decrease in strong tech debt: (relative, absolute) = (0.80, 0.02)
Current number Change Type (strong)
485 2 erw
35 -1 disabled simpNF lints
No changes to weak technical debt.

Current commit 28e36eb24d
Reference commit aa5dbf3cb5

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/technical-debt-metrics.py 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).

@mcdoll mcdoll mentioned this pull request Nov 20, 2026
5 of 21 tasks
@github-actions github-actions 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 Nov 20, 2026
@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 Nov 21, 2026
Comment thread Mathlib/Algebra/Algebra/Hom.lean Resolved

@themathqueen themathqueen left a comment

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.

I'll have more comments, but for now the main question is whether we should wait to remove the coercion in a later PR?

@EtienneC30

Copy link
Copy Markdown
Member

When I started working on this PR I was replacing the use of the RingHomClass coercion by toRingHom, but it kind of went out of hand. At some point I was left with the conclusion that we should also refactor Submonoid.map, which seemed wild. I think changing the definitions and removing the coercions in one go is complicated because most proofs rely on the coercion.

So instead I manually inserted RingHomClass.toRingHom to break as few proofs as possible, fixing most of them by rewriting some coercions in some places. This made the PR reasonable to manage. I think it is more reasonable to keep that right now, and then in a further PR we can remove the coercion.

@EtienneC30

Copy link
Copy Markdown
Member

Basically what Moritz wrote in the PR description.

@github-actions github-actions Bot added the tech debt Fixes cross-cutting technical debt, see the "technical debt counters" stream on zulip label Nov 21, 2026
@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 Nov 27, 2026
@mathlib-merge-conflicts mathlib-merge-conflicts 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 Nov 27, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

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

@github-actions github-actions Bot removed merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) increases-technical-debt Automatically added label for PRs which increase "strong" technical debts labels Nov 27, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

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

@mathlib-merge-conflicts mathlib-merge-conflicts 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 Nov 27, 2026
@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 Nov 29, 2026
Comment on lines +393 to +405
theorem Submodule.IsPrincipal.map_ringHom (f : R →+* S) {I : Ideal R}
(hI : IsPrincipal I) : IsPrincipal (Ideal.map f I) :=
⟨⟨f (IsPrincipal.generator I), by
rw [Ideal.submodule_span_eq, ← Set.image_singleton, ← Ideal.map_span,
Ideal.span_singleton_generator]⟩⟩

theorem Ideal.IsPrincipal.of_comap (f : F) (hf : Function.Surjective f) (I : Ideal S)
theorem Ideal.IsPrincipal.of_comap (f : R →+* S) (hf : Function.Surjective f) (I : Ideal S)
[hI : IsPrincipal (I.comap f)] : IsPrincipal I := by
rw [← map_comap_of_surjective f hf I]
exact hI.map_ringHom f

/-- The surjective image of a principal ideal ring is again a principal ideal ring. -/
theorem IsPrincipalIdealRing.of_surjective [IsPrincipalIdealRing R] (f : F)
theorem IsPrincipalIdealRing.of_surjective [IsPrincipalIdealRing R] (f : R →+* 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.

Maybe we should keep using the class 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.

Yes, thanks!

@j-loreaux j-loreaux left a comment

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'm fine with the one step backward in order to make forward progress. However, I think the biggest issue is that we should use the type ascription everywhere (I've tried to mark most places) so that these get changed automatically when we switch the coercion over. However, if you want to exclude that if it appears in a proof, that's fine, but I think we should at least fix the places where we have RingHomClass.toRingHom in statements.

But overall I think this a major step in the right direction.

Comment on lines +776 to +777
@[simp]
theorem coe_toRingHom (f : R ≃+* S) : ⇑f.toRingHom = f :=

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.

Although this is what we want long term, I wonder if the fact that you changed the statement (without keeping the old version too, also marked @[simp]) is part of what caused some headaches in this PR.

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 whatever we do we will have headaches. I did change my approach to changing these things a few times and I am not claiming I did things the most optimal way, but I am not sure whether that actually matters.

def SplittingField (f : K[X]) :=
MvPolynomial (SplittingFieldAux f.natDegree f) K ⧸
RingHom.ker (MvPolynomial.aeval (R := K) id).toRingHom
RingHom.ker (RingHomClass.toRingHom <| MvPolynomial.aeval (R := K) id)

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 write this with a type ascription so that when we change the coercion, this gets changed/flagged automatically.

Comment thread Mathlib/RingTheory/Smooth/Basic.lean Resolved
@themathqueen themathqueen added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Nov 30, 2026
@j-loreaux

Copy link
Copy Markdown
Contributor

Should Ideal.map take a RingHom? I guess my question is: are we ever using it currently on a FunLike type with weaker assumptions than RingHom? If not, then I'm tempted to say we probably want it to take RingHom instead of a bare function. I realize that the definition is only in terms of the bare function though (i.e., Ideal.span (⇑f '' (I : Set R))). But it leads to weird things like Ideal.map_map. Maybe it's okay? I'm not really sure here.

@mcdoll

mcdoll commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

I am not sure either, but my thought was that we generally simplify to bare functions, so if you have a lemma that takes f : RingHom with expressions I.map f and f x, then you would need an additional simp step if you have g_bare x, but need g_ringHom x (either in the wrong direction to be able to apply the lemma or you need some have := ..; simp at this nonsense).

@mcdoll mcdoll removed the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Dec 1, 2026
@mcdoll

mcdoll commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

After fixing the issues that Monica pointed out, I think I now understand a little bit better why Ideal.map should take a bare function: FunLike provides a way to coerce to a function, whereas RingHomClass does not provide a good way of coercing to a RingHom (obviously .ofClass does it). So if you have a statement that only involves function evaluation (or definitions that apply to all functions), such as Ideal.map in this case, then you can work with FunLike and RingHomClass. If you define Ideal.map for RingHom, then using RingHomClass is bad, because the statement of the theorem would involve .ofClass, which I think we decided is bad.
This makes me wonder whether there is some merit in having a class that is almost Coe, but we can prove things about (similar to FunLike) is actually a good idea (i.e., revert to the Lean 3 days of coercions being their own definition and not just labels, but with a second class, so that we retain the benefits of the multiple types of Coe).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tech debt Fixes cross-cutting technical debt, see the "technical debt counters" stream on zulip

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants