Skip to content

Commit 276a5e5

Browse files
Expand on Monoid's documentation to explain purpose behind newtypes
1 parent 9464e86 commit 276a5e5

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/Data/Monoid.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ import Type.Data.RowList (RLProxy(..))
2828
-- | `Monoid`s are commonly used as the result of fold operations, where
2929
-- | `<>` is used to combine individual results, and `mempty` gives the result
3030
-- | of folding an empty collection of elements.
31+
-- |
32+
-- | Some types (e.g. `Int`, `Boolean`) can implement multiple law-abiding
33+
-- | instances for `Monoid`. For example, `<>` could be `+` and `mempty` could
34+
-- | be `0`. Likewise, `<>` could be `*` and `mempty` could be `1`.
35+
-- | To workaround these ambiguous situations, one should use newtypes
36+
-- | to specify which instance should be used:
37+
-- | - `Additive`: use `Semiring`'s `plus`/`+` and `zero` for `<>` and `mempty`.
38+
-- | - `Multiplicative`: use `Semiring`'s `mul`/`*` and `one` for `<>` and `mempty`.
39+
-- | - `Conj`: use `HeytingAlgebra`'s `conj`/`&&` and `tt` for `<>` and `mempty`.
40+
-- | - `Disj`: use `HeytingAlgebra`'s `disj`/`||` and `ff` for `<>` and `mempty`.
41+
-- | - `Endo`: use `Category`'s `compose`/`<<<` and `identity` for `<>` and `mempty`.
42+
-- | - `Dual`: use `Category`'s `composeFlipped`/`>>>` and `identity` for `<>` and `mempty`.
3143
class Semigroup m <= Monoid m where
3244
mempty :: m
3345

0 commit comments

Comments
 (0)