1 {-# LANGUAGE CPP, NoImplicitPrelude #-}
3 -----------------------------------------------------------------------------
5 -- Module : Data.Monoid
6 -- Copyright : (c) Andy Gill 2001,
7 -- (c) Oregon Graduate Institute of Science and Technology, 2001
8 -- License : BSD-style (see the file libraries/base/LICENSE)
10 -- Maintainer : libraries@haskell.org
11 -- Stability : experimental
12 -- Portability : portable
14 -- A class for monoids (types with an associative binary operation that
15 -- has an identity) with various general-purpose instances.
16 -----------------------------------------------------------------------------
35 -- Push down the module in the dependency hierarchy.
36 #if defined(__GLASGOW_HASKELL__)
37 import GHC.Base hiding (Any)
50 import Test.QuickCheck
53 -- ---------------------------------------------------------------------------
54 -- | The class of monoids (types with an associative binary operation that
55 -- has an identity). Instances should satisfy the following laws:
57 -- * @mappend mempty x = x@
59 -- * @mappend x mempty = x@
61 -- * @mappend x (mappend y z) = mappend (mappend x y) z@
63 -- * @mconcat = 'foldr' mappend mempty@
65 -- The method names refer to the monoid of lists under concatenation,
66 -- but there are many other instances.
68 -- Minimal complete definition: 'mempty' and 'mappend'.
70 -- Some types can be viewed as a monoid in more than one way,
71 -- e.g. both addition and multiplication on numbers.
72 -- In such cases we often define @newtype@s and make those instances
73 -- of 'Monoid', e.g. 'Sum' and 'Product'.
77 -- ^ Identity of 'mappend'
78 mappend :: a -> a -> a
79 -- ^ An associative operation
82 -- ^ Fold a list using the monoid.
83 -- For most types, the default definition for 'mconcat' will be
84 -- used, but the function is included in the class definition so
85 -- that an optimized version can be provided for specific types.
87 mconcat = foldr mappend mempty
91 instance Monoid [a] where
95 instance Monoid b => Monoid (a -> b) where
97 mappend f g x = f x `mappend` g x
99 instance Monoid () where
100 -- Should it be strict?
105 instance (Monoid a, Monoid b) => Monoid (a,b) where
106 mempty = (mempty, mempty)
107 (a1,b1) `mappend` (a2,b2) =
108 (a1 `mappend` a2, b1 `mappend` b2)
110 instance (Monoid a, Monoid b, Monoid c) => Monoid (a,b,c) where
111 mempty = (mempty, mempty, mempty)
112 (a1,b1,c1) `mappend` (a2,b2,c2) =
113 (a1 `mappend` a2, b1 `mappend` b2, c1 `mappend` c2)
115 instance (Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a,b,c,d) where
116 mempty = (mempty, mempty, mempty, mempty)
117 (a1,b1,c1,d1) `mappend` (a2,b2,c2,d2) =
118 (a1 `mappend` a2, b1 `mappend` b2,
119 c1 `mappend` c2, d1 `mappend` d2)
121 instance (Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) =>
122 Monoid (a,b,c,d,e) where
123 mempty = (mempty, mempty, mempty, mempty, mempty)
124 (a1,b1,c1,d1,e1) `mappend` (a2,b2,c2,d2,e2) =
125 (a1 `mappend` a2, b1 `mappend` b2, c1 `mappend` c2,
126 d1 `mappend` d2, e1 `mappend` e2)
128 -- lexicographical ordering
129 instance Monoid Ordering where
135 -- | The dual of a monoid, obtained by swapping the arguments of 'mappend'.
136 newtype Dual a = Dual { getDual :: a }
137 deriving (Eq, Ord, Read, Show, Bounded)
139 instance Monoid a => Monoid (Dual a) where
141 Dual x `mappend` Dual y = Dual (y `mappend` x)
143 -- | The monoid of endomorphisms under composition.
144 newtype Endo a = Endo { appEndo :: a -> a }
146 instance Monoid (Endo a) where
148 Endo f `mappend` Endo g = Endo (f . g)
150 -- | Boolean monoid under conjunction.
151 newtype All = All { getAll :: Bool }
152 deriving (Eq, Ord, Read, Show, Bounded)
154 instance Monoid All where
156 All x `mappend` All y = All (x && y)
158 -- | Boolean monoid under disjunction.
159 newtype Any = Any { getAny :: Bool }
160 deriving (Eq, Ord, Read, Show, Bounded)
162 instance Monoid Any where
164 Any x `mappend` Any y = Any (x || y)
166 -- | Monoid under addition.
167 newtype Sum a = Sum { getSum :: a }
168 deriving (Eq, Ord, Read, Show, Bounded)
170 instance Num a => Monoid (Sum a) where
172 Sum x `mappend` Sum y = Sum (x + y)
174 -- | Monoid under multiplication.
175 newtype Product a = Product { getProduct :: a }
176 deriving (Eq, Ord, Read, Show, Bounded)
178 instance Num a => Monoid (Product a) where
180 Product x `mappend` Product y = Product (x * y)
183 -- To implement @find@ or @findLast@ on any 'Foldable':
186 -- findLast :: Foldable t => (a -> Bool) -> t a -> Maybe a
187 -- findLast pred = getLast . foldMap (\x -> if pred x
188 -- then Last (Just x)
189 -- else Last Nothing)
192 -- Much of Data.Map's interface can be implemented with
193 -- Data.Map.alter. Some of the rest can be implemented with a new
194 -- @alterA@ function and either 'First' or 'Last':
196 -- > alterA :: (Applicative f, Ord k) =>
197 -- > (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
199 -- > instance Monoid a => Applicative ((,) a) -- from Control.Applicative
202 -- insertLookupWithKey :: Ord k => (k -> v -> v -> v) -> k -> v
203 -- -> Map k v -> (Maybe v, Map k v)
204 -- insertLookupWithKey combine key value =
205 -- Arrow.first getFirst . alterA doChange key
207 -- doChange Nothing = (First Nothing, Just value)
208 -- doChange (Just oldValue) =
209 -- (First (Just oldValue),
210 -- Just (combine key value oldValue))
213 -- | Lift a semigroup into 'Maybe' forming a 'Monoid' according to
214 -- <http://en.wikipedia.org/wiki/Monoid>: \"Any semigroup @S@ may be
215 -- turned into a monoid simply by adjoining an element @e@ not in @S@
216 -- and defining @e*e = e@ and @e*s = s = s*e@ for all @s ∈ S@.\" Since
217 -- there is no \"Semigroup\" typeclass providing just 'mappend', we
218 -- use 'Monoid' instead.
219 instance Monoid a => Monoid (Maybe a) where
221 Nothing `mappend` m = m
222 m `mappend` Nothing = m
223 Just m1 `mappend` Just m2 = Just (m1 `mappend` m2)
226 -- | Maybe monoid returning the leftmost non-Nothing value.
227 newtype First a = First { getFirst :: Maybe a }
229 deriving (Eq, Ord, Read, Show)
230 #else /* __HADDOCK__ */
231 instance Eq a => Eq (First a)
232 instance Ord a => Ord (First a)
233 instance Read a => Read (First a)
234 instance Show a => Show (First a)
237 instance Monoid (First a) where
238 mempty = First Nothing
239 r@(First (Just _)) `mappend` _ = r
240 First Nothing `mappend` r = r
242 -- | Maybe monoid returning the rightmost non-Nothing value.
243 newtype Last a = Last { getLast :: Maybe a }
245 deriving (Eq, Ord, Read, Show)
246 #else /* __HADDOCK__ */
247 instance Eq a => Eq (Last a)
248 instance Ord a => Ord (Last a)
249 instance Read a => Read (Last a)
250 instance Show a => Show (Last a)
253 instance Monoid (Last a) where
254 mempty = Last Nothing
255 _ `mappend` r@(Last (Just _)) = r
256 r `mappend` Last Nothing = r
259 {--------------------------------------------------------------------
261 --------------------------------------------------------------------}
262 instance Arbitrary a => Arbitrary (Maybe a) where
263 arbitrary = oneof [return Nothing, Just `fmap` arbitrary]
265 prop_mconcatMaybe :: [Maybe [Int]] -> Bool
266 prop_mconcatMaybe x =
267 fromMaybe [] (mconcat x) == mconcat (catMaybes x)
269 prop_mconcatFirst :: [Maybe Int] -> Bool
270 prop_mconcatFirst x =
271 getFirst (mconcat (map First x)) == listToMaybe (catMaybes x)
272 prop_mconcatLast :: [Maybe Int] -> Bool
274 getLast (mconcat (map Last x)) == listLastToMaybe (catMaybes x)
275 where listLastToMaybe [] = Nothing
276 listLastToMaybe lst = Just (last lst)