[commit: ghc] master: base: rewrite Monoid module docs (ae68f32)
git at git.haskell.org
git at git.haskell.org
Fri Aug 17 18:54:05 UTC 2018
Repository : ssh://git@git.haskell.org/ghc
On branch : master
Link : http://ghc.haskell.org/trac/ghc/changeset/ae68f32ada5e97fa76268119294f12ec5c50e73c/ghc
>---------------------------------------------------------------
commit ae68f32ada5e97fa76268119294f12ec5c50e73c
Author: Tobias Pflug <tobias.pflug at holidaycheck.com>
Date: Wed Aug 8 23:21:02 2018 +0200
base: rewrite Monoid module docs
>---------------------------------------------------------------
ae68f32ada5e97fa76268119294f12ec5c50e73c
libraries/base/Data/Monoid.hs | 39 +++++++++++++++++++++++++++++++++++++--
1 file changed, 37 insertions(+), 2 deletions(-)
diff --git a/libraries/base/Data/Monoid.hs b/libraries/base/Data/Monoid.hs
index 29b5ddb..2fec717 100644
--- a/libraries/base/Data/Monoid.hs
+++ b/libraries/base/Data/Monoid.hs
@@ -16,8 +16,43 @@
-- Stability : experimental
-- Portability : portable
--
--- A class for monoids (types with an associative binary operation that
--- has an identity) with various general-purpose instances.
+-- A type @a@ is a 'Monoid' if it provides an associative function ('<>')
+-- that lets you combine any two values of type @a@ into one, and a neutral
+-- element (`mempty`) such that
+--
+-- > a <> mempty == mempty <> a == a
+--
+-- A 'Monoid' is a 'Semigroup' with the added requirement of a neutral element.
+-- Thus any 'Monoid' is a 'Semigroup', but not the other way around.
+--
+-- ==== __Examples__
+--
+-- The 'Sum' monoid is defined by the numerical addition operator and `0` as neutral element:
+--
+-- >>> mempty :: Sum Int
+-- Sum 0
+-- >>> Sum 1 <> Sum 2 <> Sum 3 <> Sum 4 :: Sum Int
+-- Sum {getSum = 10}
+--
+-- We can combine multiple values in a list into a single value using the `mconcat` function.
+-- Note that we have to specify the type here since 'Int' is a monoid under several different
+-- operations:
+--
+-- >>> mconcat [1,2,3,4] :: Sum Int
+-- Sum {getSum = 10}
+-- >>> mconcat [] :: Sum Int
+-- Sum {getSum = 0}
+--
+-- Another valid monoid instance of 'Int' is 'Product' It is defined by multiplication
+-- and `1` as neutral element:
+--
+-- >>> Product 1 <> Product 2 <> Product 3 <> Product 4 :: Product Int
+-- Product {getProduct = 24}
+-- >>> mconcat [1,2,3,4] :: Product Int
+-- Product {getProduct = 24}
+-- >>> mconcat [] :: Product Int
+-- Product {getProduct = 1}
+--
--
-----------------------------------------------------------------------------
More information about the ghc-commits
mailing list