From 01fe96ab14e4e7d3974e7245ee5fc2c04da6f46e Mon Sep 17 00:00:00 2001 From: ross Date: Thu, 17 Nov 2005 15:54:17 +0000 Subject: [project @ 2005-11-17 15:54:17 by ross] add a couple of Boolean instances --- libraries/base/Data/Monoid.hs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'libraries/base/Data/Monoid.hs') 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 } -- cgit v1.2.1