summaryrefslogtreecommitdiff
path: root/libraries/base/Data/Monoid.hs
diff options
context:
space:
mode:
authorross <unknown>2005-11-17 15:54:17 +0000
committerross <unknown>2005-11-17 15:54:17 +0000
commit01fe96ab14e4e7d3974e7245ee5fc2c04da6f46e (patch)
tree82274ec0df7af5a9969864f29fa77bfc9066a581 /libraries/base/Data/Monoid.hs
parentc2922a39f0df56dcb8aa26cb1e4283f53fadfbb1 (diff)
downloadhaskell-01fe96ab14e4e7d3974e7245ee5fc2c04da6f46e.tar.gz
[project @ 2005-11-17 15:54:17 by ross]
add a couple of Boolean instances
Diffstat (limited to 'libraries/base/Data/Monoid.hs')
-rw-r--r--libraries/base/Data/Monoid.hs28
1 files changed, 22 insertions, 6 deletions
diff --git a/libraries/base/Data/Monoid.hs b/libraries/base/Data/Monoid.hs
index e57b909154..cba69933c2 100644
--- a/libraries/base/Data/Monoid.hs
+++ b/libraries/base/Data/Monoid.hs
@@ -20,8 +20,10 @@
module Data.Monoid (
Monoid(..),
- Endo(..),
Dual(..),
+ Endo(..),
+ All(..),
+ Any(..),
Sum(..),
Product(..)
) where
@@ -93,6 +95,13 @@ instance Monoid Ordering where
EQ `mappend` y = y
GT `mappend` _ = GT
+-- | The dual of a monoid, obtained by swapping the arguments of 'mappend'.
+newtype Dual a = Dual { getDual :: a }
+
+instance Monoid a => Monoid (Dual a) where
+ mempty = Dual mempty
+ Dual x `mappend` Dual y = Dual (y `mappend` x)
+
-- | The monoid of endomorphisms under composition.
newtype Endo a = Endo { appEndo :: a -> a }
@@ -100,12 +109,19 @@ instance Monoid (Endo a) where
mempty = Endo id
Endo f `mappend` Endo g = Endo (f . g)
--- | The dual of a monoid, obtained by swapping the arguments of 'mappend'.
-newtype Dual a = Dual { getDual :: a }
+-- | Boolean monoid under conjunction.
+newtype All = All { getAll :: Bool }
-instance Monoid a => Monoid (Dual a) where
- mempty = Dual mempty
- Dual x `mappend` Dual y = Dual (y `mappend` x)
+instance Monoid All where
+ mempty = All True
+ All x `mappend` All y = All (x && y)
+
+-- | Boolean monoid under disjunction.
+newtype Any = Any { getAny :: Bool }
+
+instance Monoid Any where
+ mempty = Any False
+ Any x `mappend` Any y = Any (x || y)
-- | Monoid under addition.
newtype Sum a = Sum { getSum :: a }