Skip to content

fix: writer monad should use an additive logging type - #35578

Open
Shreyas4991 wants to merge 4 commits into
leanprover-community:masterfrom
Shreyas4991:Writer_Additive
Open

Shreyas4991 wants to merge 4 commits into
leanprover-community:masterfrom
Shreyas4991:Writer_Additive

Conversation

@Shreyas4991

@Shreyas4991 Shreyas4991 commented Feb 20, 2026 •

Copy link
Copy Markdown
Collaborator

The Writer monad's w type is supposed to be additive, not multiplicative. This is how it is conceptually used in Haskell (as a logging type). Haskell uses Monoid because it doesn't make a distinction between AddMonoid and Monoid.

#mathlib4 > Writer should use an additive monoid @ 💬


Open in Gitpod

@github-actions

github-actions Bot commented Feb 20, 2026 •

Copy link
Copy Markdown

PR summary 2da61843c0

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

+ instance (ω) [Monad m] [AddMonoid ω] [MonadCont m] : MonadCont (WriterT ω m)
+ instance [AddMonoid ω] : Monad (WriterT ω M) := monad 0 (· + ·)
+ instance [AddMonoid ω] : MonadLift M (WriterT ω M) := WriterT.liftTell 0
+ instance [AddMonoid ω] [LawfulMonad M] : LawfulMonad (WriterT ω M) := LawfulMonad.mk'
- instance (ω) [Monad m] [Monoid ω] [MonadCont m] : MonadCont (WriterT ω m)
- instance [Monoid ω] : Monad (WriterT ω M) := monad 1 (· * ·)
- instance [Monoid ω] : MonadLift M (WriterT ω M) := WriterT.liftTell 1
- instance [Monoid ω] [LawfulMonad M] : LawfulMonad (WriterT ω M) := LawfulMonad.mk'

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

  • +3 new declarations
  • −3 removed declarations
+WriterT.instMonadLiftOfAddMonoid
-WriterT.instMonadLiftOfMonoid
+WriterT.instMonadOfAddMonoid
-WriterT.instMonadOfMonoid
+instMonadContWriterTOfMonadOfAddMonoid
-instMonadContWriterTOfMonadOfMonoid

No changes to strong technical debt.
No changes to weak technical debt.

Current commit 2da61843c0
Reference commit 8d14a32337

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

@github-actions github-actions Bot added the t-data Data (lists, quotients, numbers, etc) label Feb 20, 2026
@Shreyas4991 Shreyas4991 changed the title fix: Writer additive fix: writer monad should use an additive logging type Feb 20, 2026
@eric-wieser

Copy link
Copy Markdown
Member

An argument against this is that the log monoid operator is usually non-commutative, which is more typically a property of * than +.

@Shreyas4991

Shreyas4991 commented Feb 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

An argument against this is that the log monoid operator is usually non-commutative, which is more typically a property of * than +.

List append is not commutative. Yet it is traditionally represented as an addition operation.
Same with monoids of strings. In any case the only use of this monad appears to be in Cont monad, a CS application, and potentially now in CSLib.

@Shreyas4991

Shreyas4991 commented Feb 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

An argument against this is that the log monoid operator is usually non-commutative, which is more typically a property of * than +.

A further counter argument is that we have a separate typeclass AddCommMonoid for commutative addition and we are not using that one here. We are only using AddMonoid.

@eric-wieser eric-wieser added the awaiting-zulip There is a Zulip discussion; the author should await and report/implement the decision reached there label Feb 20, 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 Mar 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 Oct 26, 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 3, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

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

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 merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) t-data Data (lists, quotients, numbers, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants