Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - refactor(Algebra/Category/ModuleCat): make ModuleCat.Hom a structure #19511

Closed
wants to merge 24 commits into from

Conversation

Vierkantor
Copy link
Contributor

@Vierkantor Vierkantor commented Nov 26, 2024

This PR follows the approach of #19065 to solve painful definitional equalities problems by forbidding the identification of A ⟶ B and A →ₗ[R] B. These are not equal reducibly, so tactics get confused if one is replaced by the other.

There seem to be a few regressions caused by defeq abuse that we were able to get away with previously, but some subtle elaboration differences made it evident. Otherwise the cost is inserting many .homs and asHoms.

A few steps that might clean up the diff, but would be too much work to incorporate in this PR:

  • Make the ext tactic pick up TensorProduct.ext again.
  • Replace the defeq abuse between (restrictScalars f).obj M and M with explicit maps going back and forth.

Overall quite a few proofs could be cleaned up at the cost of more bookkeeping.


Open in Gitpod

Copy link

github-actions bot commented Nov 26, 2024

PR summary f8f8363b30

Import changes exceeding 2%

% File
+18.89% Mathlib.CategoryTheory.Limits.ConcreteCategory.WithAlgebraicStructures

Import changes for modified files

Dependency changes

File Base Count Head Count Change
Mathlib.CategoryTheory.Limits.ConcreteCategory.WithAlgebraicStructures 614 730 +116 (+18.89%)
Import changes for all files
Files Import difference
Mathlib.CategoryTheory.Limits.ConcreteCategory.WithAlgebraicStructures 116

Declarations diff

+ FGModuleCatEvaluation_apply'
+ Hom
+ Hom.hom₂
+ Hom.hom₂_ofHom₂
+ Hom.instModule
+ HomEquiv.fromRestriction_hom_apply_apply
+ HomEquiv.toRestriction_hom_apply
+ Iso.conj_hom_eq_conj
+ asHom
+ comp_apply
+ endMulEquiv
+ endMulEquiv_comp_ρ
+ endMulEquiv_symm_comp_ρ
+ forget_obj
+ homAddEquiv
+ homEquiv
+ homLinearEquiv
+ hom_action_ρ
+ hom_add
+ hom_bijective
+ hom_comp
+ hom_id
+ hom_injective
+ hom_inv_apply
+ hom_neg
+ hom_nsmul
+ hom_ofHom
+ hom_smul
+ hom_sub
+ hom_sum
+ hom_surjective
+ hom_zero
+ hom_zsmul
+ instance : Add (M ⟶ N)
+ instance : AddCommGroup (M ⟶ N)
+ instance : CoeFun (obj' f M) fun _ => S → M
+ instance : Inhabited (ModuleCat R)
+ instance : Neg (M ⟶ N)
+ instance : SMul S (M ⟶ N)
+ instance : SMul ℕ (M ⟶ N)
+ instance : SMul ℤ (M ⟶ N)
+ instance : Sub (M ⟶ N)
+ instance : Zero (M ⟶ N)
+ instance {M N : ModuleCat.{v} R} : CoeFun (M ⟶ N) (fun _ ↦ M → N)
+ inv_hom_apply
+ ofHom_apply
+ ofHom_comp
+ ofHom_hom
+ ofHom_id
+ ofHom_ρ
+ ofHom₂
+ ofHom₂_compr₂
+ ofHom₂_hom₂
+ pushforward_map_app_apply'
+ pushforward_obj_map_apply'
+ toSheafify_app_apply'
+ ρ_hom
++ hom_ext
++ ofHom
++-- of
- ModuleCat.asHom
- ModuleCat.ofHom
- ModuleCat.ofHom_apply
- coe_comp
- comp_def
- ext
- instance : CoeFun (obj' f M) fun _ => S → M where coe g := g.toFun
- instance {M N : FGModuleCat R} : FunLike (M ⟶ N) M N
- instance {M N : FGModuleCat R} : LinearMapClass (M ⟶ N) R M N
- instance {M N : ModuleCat.{v} R} : AddCommGroup (M ⟶ N) := LinearMap.addCommGroup
- instance {M N : ModuleCat.{v} R} : FunLike (M ⟶ N) M N
- instance {M N : ModuleCat.{v} R} : LinearMapClass (M ⟶ N) R M N
- instance {R : Type u₁} {S : Type u₂} [Ring R] [Ring S] {f : R →+* S}
- ofUnique

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) = (5.49, 0.03)
Current number Change Type
4981 -30 porting notes
202 -2 disabled simpNF lints
1498 -24 erw

Current commit f8f8363b30
Reference commit a38db992d0

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).

@@ -129,14 +125,14 @@ morphisms `M ⟶ (ModuleCat.restrictScalars f).obj N`. -/
def semilinearMapAddEquiv {R : Type u₁} {S : Type u₂} [Ring R] [Ring S] (f : R →+* S)
(M : ModuleCat.{v} R) (N : ModuleCat.{v} S) :
(M →ₛₗ[f] N) ≃+ (M ⟶ (ModuleCat.restrictScalars f).obj N) where
toFun g :=
toFun g := asHom (X := M) (Y := (ModuleCat.restrictScalars f).obj N) <|
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having to type-ascribe asHom suggests there is some defeq abuse going on.

Comment on lines 187 to 199
have : ∀ x, _ = (0 : _ →ₗ[_] _) x := LinearMap.ext_iff.1 (ModuleCat.hom_ext_iff.mp
((linearYonedaObjResolution A).d_comp_d n (n + 1) (n + 2)))
simp only [ModuleCat.hom_comp, LinearMap.comp_apply] at this
dsimp only
simp only [d_eq, LinearEquiv.toModuleIso_inv, LinearEquiv.toModuleIso_hom, ModuleCat.coe_comp,
Function.comp_apply]
simp only [d_eq, LinearEquiv.toModuleIso_inv_hom, LinearEquiv.toModuleIso_hom_hom,
ModuleCat.hom_comp, LinearMap.comp_apply, LinearEquiv.coe_coe, ModuleCat.hom_zero]
/- Porting note: I can see I need to rewrite `LinearEquiv.coe_coe` twice to at
least reduce the need for `symm_apply_apply` to be an `erw`. However, even `erw` refuses to
rewrite the second `coe_coe`... -/
erw [LinearEquiv.symm_apply_apply, this]
exact map_zero _
erw [LinearEquiv.symm_apply_apply, this, LinearMap.zero_apply]
simp
rfl
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This proof got uglier, not sure what the concrete cause is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried a few things and it seems there was already a lot of defeq abuse that got exposed now.

Comment on lines 212 to 221
ext
simp only [ChainComplex.linearYonedaObj_X, linearYoneda_obj_obj_carrier, CochainComplex.of_x,
linearYoneda_obj_obj_isAddCommGroup, linearYoneda_obj_obj_isModule, Iso.symm_hom,
ChainComplex.linearYonedaObj_d, ModuleCat.hom_comp, linearYoneda_obj_map_hom,
Quiver.Hom.unop_op, LinearEquiv.toModuleIso_inv_hom, LinearMap.coe_comp, Function.comp_apply,
Linear.leftComp_apply, inhomogeneousCochains.d_def, d_eq, LinearEquiv.toModuleIso_hom_hom,
ModuleCat.asHom_comp, Category.assoc, LinearEquiv.comp_coe, LinearEquiv.self_trans_symm,
LinearEquiv.refl_toLinearMap, LinearMap.id_comp, LinearEquiv.coe_coe]
rfl
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This proof got a lot uglier, not sure what the concrete cause is.

@joelriou joelriou added the t-category-theory Category theory label Nov 26, 2024
@github-actions github-actions bot added the large-import Automatically added label for PRs with a significant increase in transitive imports label Nov 28, 2024
@Vierkantor Vierkantor force-pushed the ModuleCat-Hom-structure branch from 1454f64 to 800d526 Compare November 28, 2024 17:42
@Vierkantor Vierkantor marked this pull request as ready for review November 29, 2024 12:09
@chrisflav
Copy link
Collaborator

Overall this looks very good and a lot of proofs got cleaner! Did you run your magic erw -> rw tool? I could imagine there is some potential.

I hope you don't mind that I pushed some small changes to get rid of some more erws.

@chrisflav
Copy link
Collaborator

I tried to add a test file similar to the one for AlgebraCat, but I am running into this:

import Mathlib
open CategoryTheory ModuleCat
variable (R : Type u) [CommRing R]

/-
ambiguous, possible interpretations 
  ↟f : of R X ⟶ of R Y
  
  ↾⇑f : X ⟶ Y
-/
example {X Y : Type v} [AddCommGroup X] [Module R X] [AddCommGroup Y] [Module R Y] (f : X →ₗ[R] Y)
    (x : X) : (asHom f) x = f x := by simp

Do you understand this?

@Vierkantor
Copy link
Contributor Author

I think the testcase fails because there is also the definition CategoryTheory.asHom which is ModuleCat.asHom but for the category of types. So we either apply ModuleCat.asHom and then coerce back to a function, or we coerce f to a function, apply CategoryTheory.asHom to make it a morphism, and then coerce it back to a plain function. And since both of the end results have the same types and applicability, Lean can't choose and throws an error.

@chrisflav
Copy link
Collaborator

I think the testcase fails because there is also the definition CategoryTheory.asHom which is ModuleCat.asHom but for the category of types. So we either apply ModuleCat.asHom and then coerce back to a function, or we coerce f to a function, apply CategoryTheory.asHom to make it a morphism, and then coerce it back to a plain function. And since both of the end results have the same types and applicability, Lean can't choose and throws an error.

Ah, silly me, I didn't think of CategoryTheory.asHom and was not aware of the notation. Replacing the asHom with ModuleCat.asHom makes everything work.

@chrisflav
Copy link
Collaborator

I put the tests in 554ba63, if you like them feel free to cherry-pick (or tell me to push it here).

@Vierkantor
Copy link
Contributor Author

Thank you! Feel free to push your commit directly to this branch. :)

@Vierkantor
Copy link
Contributor Author

Overall this looks very good and a lot of proofs got cleaner! Did you run your magic erw -> rw tool? I could imagine there is some potential.

I hope you don't mind that I pushed some small changes to get rid of some more erws.

I did run it on an earlier version of the branch, let me see if anything got cleared up in the meantime.

(It should be as easy as merging the try_this-erw branch into the current branch, and then cherry-picking any of the warnings it produces.)

@Vierkantor
Copy link
Contributor Author

No free erw eliminations found on this branch, but I did find a few in other bits of Mathlib so time to make a little PR for that :D

@Vierkantor Vierkantor force-pushed the ModuleCat-Hom-structure branch from 867bc82 to 44e4f79 Compare December 3, 2024 10:28
@leanprover-community-bot-assistant leanprover-community-bot-assistant 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 3, 2024
It seems to be only with `restrictScalars` or definitions using `restrictScalars` such as in `AlgebraicGeometry/Modules/Tilde.lean`
Vierkantor added a commit that referenced this pull request Dec 3, 2024
…eCat.ofHom`

It seems the choice between `ofHom` and `asHom` is somewhat inconsistent in concrete categories, but `ofHom` wins. In particular, we want to be consistent with the newly refactored `AlgebraCat.ofHom` and the constructor for objects `ModuleCat.of`.

I have not touched `asHomLeft` and `asHomRight` since #19511 will replace these with `ofHom` everywhere anyway.

As discussed in the comments for #19511.
@mathlib4-dependent-issues-bot mathlib4-dependent-issues-bot added the blocked-by-other-PR This PR depends on another PR to Mathlib (this label is automatically managed by a bot) label Dec 3, 2024
mathlib-bors bot pushed a commit that referenced this pull request Dec 3, 2024
…eCat.ofHom` (#19705)

As discussed in the comments for #19511.

It seems the choice between `ofHom` and `asHom` is somewhat inconsistent in concrete categories, but `ofHom` wins. In particular, we want to be consistent with the newly refactored `AlgebraCat.ofHom` and the constructor for objects `ModuleCat.of`.

The choice between `asHom` and `ofHom` was only made a few months ago: #17476. It's a bit unfortunate to go back on something that was deprecated so recently, but I think the result is worth the pain.

I have not touched `asHomLeft` and `asHomRight` since #19511 will replace these with `ofHom` everywhere anyway.
@mathlib4-dependent-issues-bot mathlib4-dependent-issues-bot removed the blocked-by-other-PR This PR depends on another PR to Mathlib (this label is automatically managed by a bot) label Dec 3, 2024
@mathlib4-dependent-issues-bot
Copy link
Collaborator

@leanprover-community-bot-assistant leanprover-community-bot-assistant 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 3, 2024
@leanprover-community-bot-assistant leanprover-community-bot-assistant 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 4, 2024
@jcommelin
Copy link
Member

!bench

@leanprover-bot
Copy link
Collaborator

Here are the benchmark results for commit f8f8363.
There were significant changes against commit a38db99:

  Benchmark                                                    Metric         Change
  ==================================================================================
- ~Mathlib.Algebra.Category.ModuleCat.Algebra                  instructions   227.1%
- ~Mathlib.Algebra.Category.ModuleCat.Basic                    instructions    46.7%
+ ~Mathlib.Algebra.Category.ModuleCat.ChangeOfRings            instructions   -30.3%
- ~Mathlib.Algebra.Category.ModuleCat.Differentials.Presheaf   instructions    15.2%
+ ~Mathlib.Algebra.Category.ModuleCat.Presheaf                 instructions   -16.9%
- ~Mathlib.Algebra.Category.ModuleCat.Presheaf.ChangeOfRings   instructions    28.3%
+ ~Mathlib.Algebra.Category.ModuleCat.Presheaf.Monoidal        instructions   -16.7%
- ~Mathlib.Algebra.Category.ModuleCat.Presheaf.Pushforward     instructions    19.1%
+ ~Mathlib.Algebra.Category.ModuleCat.Presheaf.Sheafify        instructions   -13.4%
- ~Mathlib.Algebra.Category.ModuleCat.Sheaf.Colimits           instructions    61.8%
- ~Mathlib.Algebra.Homology.ShortComplex.ModuleCat             instructions    71.3%
- ~Mathlib.AlgebraicGeometry.Modules.Tilde                     instructions    24.1%
- ~Mathlib.CategoryTheory.Linear.Yoneda                        instructions    19.8%
+ ~Mathlib.CategoryTheory.Monoidal.Internal.Module             instructions    -9.2%
- ~Mathlib.RepresentationTheory.Rep                            instructions    12.1%

@jcommelin
Copy link
Member

Hmmm, we still don't get the helpful report... Don't know why not 😢

@kim-em
Copy link
Contributor

kim-em commented Dec 5, 2024

bors d+

@mathlib-bors
Copy link
Contributor

mathlib-bors bot commented Dec 5, 2024

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

@Vierkantor
Copy link
Contributor Author

I checked again and am happy with the PR, so let's get this merged!

bors merge

@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the ready-to-merge This PR has been sent to bors. label Dec 5, 2024
mathlib-bors bot pushed a commit that referenced this pull request Dec 5, 2024
#19511)

This PR follows the approach of #19065 to solve painful definitional equalities problems by forbidding the identification of `A ⟶ B` and `A →ₗ[R] B`. These are not equal reducibly, so tactics get confused if one is replaced by the other.

There seem to be a few regressions caused by defeq abuse that we were able to get away with previously, but some subtle elaboration differences made it evident. Otherwise the cost is inserting many `.hom`s and `asHom`s.

A few steps that might clean up the diff, but would be too much work to incorporate in this PR:
 * Make the `ext` tactic pick up `TensorProduct.ext` again.
 * Replace the defeq abuse between `(restrictScalars f).obj M` and `M` with explicit maps going back and forth.

Overall quite a few proofs could be cleaned up at the cost of more bookkeeping.



Co-authored-by: Christian Merten <[email protected]>
@mathlib-bors
Copy link
Contributor

mathlib-bors bot commented Dec 5, 2024

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title refactor(Algebra/Category/ModuleCat): make ModuleCat.Hom a structure [Merged by Bors] - refactor(Algebra/Category/ModuleCat): make ModuleCat.Hom a structure Dec 5, 2024
@mathlib-bors mathlib-bors bot closed this Dec 5, 2024
@mathlib-bors mathlib-bors bot deleted the ModuleCat-Hom-structure branch December 5, 2024 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
delegated large-import Automatically added label for PRs with a significant increase in transitive imports ready-to-merge This PR has been sent to bors. t-category-theory Category theory
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants