Skip to content

feat(CategoryTheory/Monoidal): to_additive for MonoidalCategory - #30150

Open
imbrem wants to merge 14 commits into
leanprover-community:masterfrom
imbrem:additive-monoidal
Open

imbrem wants to merge 14 commits into
leanprover-community:masterfrom
imbrem:additive-monoidal

Conversation

@imbrem

@imbrem imbrem commented Oct 3, 2025 •

Copy link
Copy Markdown
Contributor

Add AddMonoidalCategory, the additive version of MonoidalCategory.

To get this to work, I needed to remove the to_additive attributes in Discrete.lean, since existing code relies on the AddMonoid M → MonoidalCategory M instance. For now, we simply implement the additive variants by hand instead.


As discussed in #28718; I added an AddMonoidalCategory struct and tagged MonoidalCategory with to_additive, along with the lemmas in Category.lean.

I think this is the right approach, since under this framework the "correct" additive version of Discrete.lean would be mapping an AddMonoid to an AddMonoidalCategory.

Next steps would be to:

  • Make monoidal_coherence and coherence support AddMonoidalCategory
  • Add CocartesianMonoidalCategory extending AddMonoidalCategory

Open in Gitpod

@github-actions github-actions Bot added new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! large-import Automatically added label for PRs with a significant increase in transitive imports labels Oct 3, 2025
@github-actions

github-actions Bot commented Oct 3, 2025 •

Copy link
Copy Markdown

PR summary ef9ae2a401

Import changes exceeding 2%

% File
+3.20% Mathlib.CategoryTheory.Monoidal.Category

Import changes for modified files

Dependency changes

File Base Count Head Count Change
Mathlib.CategoryTheory.Monoidal.Category 344 355 +11 (+3.20%)
Import changes for all files
Files Import difference
11 files Mathlib.CategoryTheory.Bicategory.End Mathlib.CategoryTheory.Monoidal.Action.Basic Mathlib.CategoryTheory.Monoidal.Action.LinearFunctor Mathlib.CategoryTheory.Monoidal.Category Mathlib.CategoryTheory.Monoidal.CoherenceLemmas Mathlib.Tactic.CategoryTheory.Monoidal.Basic Mathlib.Tactic.CategoryTheory.Monoidal.Datatypes Mathlib.Tactic.CategoryTheory.Monoidal.Normalize Mathlib.Tactic.CategoryTheory.Monoidal.PureCoherence Mathlib.Tactic.CategoryTheory.MonoidalComp Mathlib.Tactic.Widget.StringDiagram
11

Declarations diff

+ AddMonoidalCategory
+ AddMonoidalCategoryStruct
+ Discrete.addMonoidal
+ Discrete.addMonoidalFunctor
+ Discrete.addMonoidalFunctorComp
+ Discrete.addMonoidalFunctorComp_isMonoidal
+ Discrete.addMonoidalFunctorMonoidal
+ Discrete.addMonoidalFunctor_obj
+ Discrete.addMonoidalFunctor_δ
+ Discrete.addMonoidalFunctor_ε
+ Discrete.addMonoidalFunctor_η
+ Discrete.addMonoidalFunctor_μ
+ Discrete.addMonoidal_tensorUnit_as

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.


No changes to technical debt.

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

@YaelDillies

Copy link
Copy Markdown
Contributor

To get this to work, I needed to remove the to_additive attributes in Discrete.lean, since otherwise the instances and lemmas within would be ill-typed (as MonoidalCategory would be replaced with AddMonoidalCategory). We simply implement the additive variants by hand instead.

Sorry, I don't get this. With your PR, the meaning of to_additive on Discrete.monoidal changes, I agree, but it changes in exactly the way you're claiming is the right one in this new framework.

@JovanGerb

Copy link
Copy Markdown
Contributor

To you know about the syntax to_additive (dont_translate := α)? Maybe it could help with additivizing only some part of the declaration, avoiding the need of removing to_additive somewhere else.

@JovanGerb

Copy link
Copy Markdown
Contributor

Also, adding the translation
tensor → add
To the additive dictionary will most likely break other things, so be careful with this kind of change.

@imbrem

imbrem commented Oct 3, 2025

Copy link
Copy Markdown
Contributor Author

I've added some imports and (hopefully) fixed CI!

@YaelDillies exactly; right now we have Discrete.monoidal and Discrete.addMonoidal as two potentially conflicting instances of MonoidalCategory (say for Discrete ℕ since ℕ is both a Monoid and an AddMonoid), whereas if we swapped things over this would not be the case.

Unfortunately, CategoryTheory/Shift/Basic.lean relies on this to define a shift indexed by an AddMonoid A as a monoidal functor from Discrete A ⥤ C ⥤ C, requiring Discrete A to have a MonoidalCategory instance.

Long term, it's probably best to remove the conflicting instances and add to_additive back in, but this changes things and breaks API, so I thought I'd for the short term just replicate the old API without the to_additive, which does not break anything. To fix HasShift, we'd then need to switch to using something like Multiplicative (Discrete A).

But I wanted to keep this PR as small as possible!

@JovanGerb indeed I was worried about breaking things, but everything still compiles so hopefully it should be OK? I don't think we'd want to mark tensor-things to_additive very often outside this categorical context, right?

Also, @YaelDillies, is it a wise move to start on some PRs depending on this one to:

  • Add premonoidal categories, which should also have all the to_additive machinery. Start with just the definitions
  • Add cocartesian categories
  • Port basic lemmas and the coherence tactic to AddMonoidalCategory
  • Port basic lemmas and some of the coherence tactic to PremonoidalCategory

If we get all that done, then that should be everything I need to get my thesis on the latest mathlib since the rest is orthogonal, and I'll try to upstream as much of the rest as possible if anyone's interested (e.g. Freyd categories, copy-delete categories, Elgot categories...).

@imbrem
imbrem force-pushed the additive-monoidal branch from b8f29fa to 54970ab Compare October 3, 2025 17:48

@YaelDillies YaelDillies 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.

Okay, that makes sense. I've clarified the PR description accordingly.

I think you can start working on the next PRs, but conversely this one is probably pretty close to merging. What's left to do is mostly to acquire consensus on the new names and notations. I suspect this will be done most efficiently on Zulip. Mind starting a topic?


## References
* Tensor categories, Etingof, Gelaki, Nikshych, Ostrik,
* Tensor categories, Etingof, Gelaki, Nikshych, Ostrik,\

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.

Accident? Out of scope change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Accident

Comment on lines +421 to +426
attribute [to_additive (attr := simp)]
whiskerLeft_comp id_whiskerLeft tensor_whiskerLeft comp_whiskerRight whiskerRight_id
whiskerRight_tensor whisker_assoc
attribute [to_additive]
whiskerLeft_comp_assoc id_whiskerLeft_assoc tensor_whiskerLeft_assoc comp_whiskerRight_assoc
whiskerRight_id_assoc whiskerRight_tensor_assoc whisker_assoc_assoc

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 do this inline? Is it because reassoc and to_additive are incompatible? Does @[reassoc (attr := to_additive (attr := simp))] work by any chance?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The issue is that would tag the associated versions as simp lemmas; here, we want only the non-associated versions as simp lemmas since the originals are tagged with @[reassoc, simp] rather than @[reassoc (attr := simp)]. If we do what you suggest we get stuck in infinite simp loops and timeout (I did it by accident!).

@YaelDillies YaelDillies added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Oct 4, 2025
@imbrem

imbrem commented Oct 4, 2025

Copy link
Copy Markdown
Contributor Author

@YaelDillies here's a link to the Zulip thread I made: #mathlib4 > to_additive on MonoidalCategory

@imbrem
imbrem force-pushed the additive-monoidal branch from d9f714b to 4778d23 Compare October 4, 2025 18:15
@YaelDillies YaelDillies added awaiting-zulip There is a Zulip discussion; the author should await and report/implement the decision reached there and removed awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. labels Oct 5, 2025
@imbrem

imbrem commented Oct 5, 2025

Copy link
Copy Markdown
Contributor Author

Hello! Hopefully I didn't break anything (we'll see); I just realized to_additive was turning e.g. pentagon_inv into pentagon_neg. I went and fixed that using fixAbbreviation, and ended up adding rules to turn hom_neg into hom_inv and neg_hom into inv_hom.

Looking at Mathlib, all lemmas of the form hom_neg aren't related to to_additive, whereas the only lemmas of the form neg_hom are e.g. AddGrp.neg_hom_apply, which is the output of to_additive but should actually be called inv_hom_apply (there's no negation in the theorem statement!)

So I consider this a bugfix, rather than a change, especially since it doesn't break anything, but let me know.

@YaelDillies

Copy link
Copy Markdown
Contributor

Hmm, all those special cases look like a maintainability issue.

@YaelDillies YaelDillies closed this Oct 5, 2025
@YaelDillies

Copy link
Copy Markdown
Contributor

Whoops, wrong button!

@YaelDillies YaelDillies reopened this Oct 5, 2025
@imbrem

imbrem commented Oct 5, 2025 •

Copy link
Copy Markdown
Contributor Author

That was scary! I think we can swap to n "neg"s before/after "hom" later without breaking anything if necessary; the other cases are unlikely to trigger for anything other than their specific lemma (triangle_assoc_neg) or are actually what we want ("when I say addAssociator_neg I mean addAssociator_inv since in general how do I negate an associator")

Anyways I figured this out when porting the coherence tactic and realising neg was appearing everywhere; PR for that coming up!

@imbrem

imbrem commented Oct 6, 2025

Copy link
Copy Markdown
Contributor Author

Oops; that last commit was meant for #30260, undid!

@joelriou joelriou added t-meta Tactics, attributes or user commands t-category-theory Category theory labels Oct 19, 2025
@imbrem

imbrem commented Oct 27, 2025

Copy link
Copy Markdown
Contributor Author

Any updates?

@YaelDillies

Copy link
Copy Markdown
Contributor

This is awaiting-zulip, so you should ask on the corresponding Zulip thread

@mathlib4-merge-conflict-bot

Copy link
Copy Markdown

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

@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 Nov 13, 2025

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

awaiting-zulip There is a Zulip discussion; the author should await and report/implement the decision reached there large-import Automatically added label for PRs with a significant increase in transitive imports merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! t-category-theory Category theory t-meta Tactics, attributes or user commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants