-
Notifications
You must be signed in to change notification settings - Fork 346
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): turn homs in AlgebraCat
into a structure
#19065
Conversation
PR summary 04c436374eImport changes for modified filesNo significant changes to the import graph Import changes for all files
|
Current number | Change | Type |
---|---|---|
5090 | -6 | porting notes |
219 | -1 | disabled simpNF lints |
1547 | -6 | erw |
Current commit 04c436374e
Reference commit 54d12203eb
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 therelative
value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).
!bench |
Here are the benchmark results for commit 58ebaf4. |
|
!bench |
Here are the benchmark results for commit 11940cd. Benchmark Metric Change
===================================================================
- ~Mathlib.Algebra.Category.AlgebraCat.Limits instructions 24.8% |
|
!bench |
Here are the benchmark results for commit ebd530e. Benchmark Metric Change
===================================================================
- ~Mathlib.Algebra.Category.AlgebraCat.Limits instructions 24.8% |
|
!bench |
Here are the benchmark results for commit 73bd116. Benchmark Metric Change
===================================================================
- ~Mathlib.Algebra.Category.AlgebraCat.Limits instructions 32.5% |
|
So apparently, using the anonymous constructor even made it worse? |
This reverts commit 73bd116.
Can you update your PR description to explain the changes and reasoning for posterity? Let's also do one more run of benchmarking. |
!bench |
Updated the PR description. |
Here are the benchmark results for commit 24c84a4. Benchmark Metric Change
==================================================================
- ~Mathlib.Algebra.Category.AlgebraCat.Basic instructions 73.0% |
|
It seems like instructions have crept up again. |
This reverts commit affefe9.
I think this is mostly because we now use |
!bench |
Here are the benchmark results for commit 04c4363. Benchmark Metric Change
==================================================================
- ~Mathlib.Algebra.Category.AlgebraCat.Basic instructions 69.1% |
|
What is needed to get this merged? For the slowdown: When replacing every |
I agree that the slowdown is a worthy tradeoff. So I still stand by my desire to merge this. @mattrobball, what do you think? |
Since Matt gave this a thumbs up, let's get this merged. Thank you so much for this work and your nice results! bors r+ |
#19065) In the current design of concrete categories in mathlib, there are two sources of `erw`s: - Def-Eq abuse of `A ⟶ B` and `A →ₐ [R] B`: The type of `A ⟶ B` is by definition `A →ₐ[R] B`, but not at reducible transparency. So it happens that one `rw` lemma transforms a term in a form, where the next `rw` expects `A →ₐ B`. By supplying explicit `show` lines (copying the output of the pretty-printer), Lean correctly re-synthesizes the type to be `A →ₐ B` and the `rw` succeeds. This def-eq abuse hence always causes issues in the case where a lemma from the non-category theoretic part of the library should be applied. An insightful example is the following proof excerpt from current master (proof of `AlgebraCat.adj`): ```lean import Mathlib.Algebra.Category.AlgebraCat.Basic open CategoryTheory AlgebraCat variable {R : Type u} [CommRing R] set_option pp.analyze true example : free.{u} R ⊣ forget (AlgebraCat.{u} R) := Adjunction.mkOfHomEquiv { homEquiv := fun _ _ => (FreeAlgebra.lift _).symm homEquiv_naturality_left_symm := by intro X Y Z f g apply FreeAlgebra.hom_ext ext x simp only [Equiv.symm_symm, Function.comp_apply, free_map] /- Eq (α := Z) (((FreeAlgebra.lift R) (f ≫ g) : Prefunctor.obj (Functor.toPrefunctor (free R)) X ⟶ Z) (FreeAlgebra.ι R x) : ↑Z) _ -/ erw [FreeAlgebra.lift_ι_apply] sorry homEquiv_naturality_right := sorry } ``` The `simp` lemma `FreeAlgebra.lift_ι_apply` expects an `AlgHom`, but as the `pp.analyze` shows, the type is synthesized as a `⟶`. With a show line before the `erw`, Lean correctly synthesizes the type as an `AlgHom` again and the `rw` succeeds. The solution is to strictly distinguish between `A ⟶ B` and `A →ₐ[R] B`: We define a new `AlgebraCat.Hom` structure with a single field `hom : A →ₐ[R] B`. The above proof is mostly solved by `ext; simp` then, except for the `ext` lemma `FreeAlgebra.hom_ext` not applying. This is because now the type is `AlgebraCat.of R (FreeAlgebra R X) →ₐ[R] _` and Lean can't see through the `AlgebraCat.of` at reducible transparency. Hence the solution is to make `AlgebraCat.of` an `abbrev`. Finally, the proof is `by ext; simp` or even `by aesop`. - `FunLike`: An intermediate design adapted the changes from the first point, but with keeping a `FunLike` and an `AlgHomClass` instance on `A ⟶ B`. This lead to many additional `coe` lemmas for composition of morphisms, where it mattered which of the three appearing terms of `AlgebraCat` where of the form `AlgebraCat.of R A`. Eric Wieser suggested to replace the `FunLike` by a `CoeFun` which is inlined, so an invocation of `f x` turns into `f.hom x`. This has then worked very smoothly. So the key points are: - Homs in concrete categories should be one-field structures to maintain a strict type distinction. - Use `CoeFun` instead of `FunLike`, since the latter is automatically inlined. - Make `.of` constructors an `abbrev` to obtain the def-eq `↑(AlgebraCat.of R A) = A` at reducible transparency. For more details, see the [Zulip discussion](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Fixing.20the.20.60erw.60.20hell.20around.20concrete.20categories).
Pull request successfully merged into master. Build succeeded: |
AlgebraCat
into a structureAlgebraCat
into a structure
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. Currently the PR consists mostly of me playing "type tetris" to get everything to compile again: I haven't looked for which proofs can be cleaned up as a result. This commit is not quite finished, please see the Zulip thread for more details.
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. Currently the PR consists mostly of me playing "type tetris" to get everything to compile again: I haven't looked for which proofs can be cleaned up as a result. This commit is not quite finished, please see the Zulip thread for more details.
#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]>
In the current design of concrete categories in mathlib, there are two sources of
erw
s:A ⟶ B
andA →ₐ [R] B
: The type ofA ⟶ B
is by definitionA →ₐ[R] B
, but not at reducible transparency. So it happens that onerw
lemma transforms a term in a form, where the nextrw
expectsA →ₐ B
. By supplying explicitshow
lines (copying the output of the pretty-printer), Lean correctly re-synthesizes the type to beA →ₐ B
and therw
succeeds. This def-eq abuse hence always causes issues in the case where a lemma from the non-category theoretic part of the library should be applied. An insightful example is the following proof excerpt from current master (proof ofAlgebraCat.adj
):The
simp
lemmaFreeAlgebra.lift_ι_apply
expects anAlgHom
, but as thepp.analyze
shows, the type is synthesized as a⟶
. With a show line before theerw
, Lean correctly synthesizes the type as anAlgHom
again and therw
succeeds. The solution is to strictly distinguish betweenA ⟶ B
andA →ₐ[R] B
: We define a newAlgebraCat.Hom
structure with a single fieldhom : A →ₐ[R] B
. The above proof is mostly solved byext; simp
then, except for theext
lemmaFreeAlgebra.hom_ext
not applying. This is because now the type isAlgebraCat.of R (FreeAlgebra R X) →ₐ[R] _
and Lean can't see through theAlgebraCat.of
at reducible transparency. Hence the solution is to makeAlgebraCat.of
anabbrev
. Finally, the proof isby ext; simp
or evenby aesop
.FunLike
: An intermediate design adapted the changes from the first point, but with keeping aFunLike
and anAlgHomClass
instance onA ⟶ B
. This lead to many additionalcoe
lemmas for composition of morphisms, where it mattered which of the three appearing terms ofAlgebraCat
where of the formAlgebraCat.of R A
. Eric Wieser suggested to replace theFunLike
by aCoeFun
which is inlined, so an invocation off x
turns intof.hom x
. This has then worked very smoothly.So the key points are:
CoeFun
instead ofFunLike
, since the latter is automatically inlined..of
constructors anabbrev
to obtain the def-eq↑(AlgebraCat.of R A) = A
at reducible transparency.For more details, see the Zulip discussion.