1 -----------------------------------------------------------------------------
3 -- Module : Data.Monoid
4 -- Copyright : (c) Andy Gill 2001,
5 -- (c) Oregon Graduate Institute of Science and Technology, 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : portable
12 -- The Monoid class with various general-purpose instances.
14 -- Inspired by the paper
15 -- /Functional Programming with Overloading and
16 -- Higher-Order Polymorphism/,
17 -- Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
18 -- Advanced School of Functional Programming, 1995.
19 -----------------------------------------------------------------------------
33 -- ---------------------------------------------------------------------------
34 -- | The monoid class.
35 -- A minimal complete definition must supply 'mempty' and 'mappend',
36 -- and these should satisfy the monoid laws.
40 -- ^ Identity of 'mappend'
41 mappend :: a -> a -> a
42 -- ^ An associative operation
45 -- ^ Fold a list using the monoid.
46 -- For most types, the default definition for 'mconcat' will be
47 -- used, but the function is included in the class definition so
48 -- that an optimized version can be provided for specific types.
50 mconcat = foldr mappend mempty
54 instance Monoid [a] where
58 instance Monoid b => Monoid (a -> b) where
60 mappend f g x = f x `mappend` g x
62 instance Monoid () where
63 -- Should it be strict?
68 instance (Monoid a, Monoid b) => Monoid (a,b) where
69 mempty = (mempty, mempty)
70 (a1,b1) `mappend` (a2,b2) =
71 (a1 `mappend` a2, b1 `mappend` b2)
73 instance (Monoid a, Monoid b, Monoid c) => Monoid (a,b,c) where
74 mempty = (mempty, mempty, mempty)
75 (a1,b1,c1) `mappend` (a2,b2,c2) =
76 (a1 `mappend` a2, b1 `mappend` b2, c1 `mappend` c2)
78 instance (Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a,b,c,d) where
79 mempty = (mempty, mempty, mempty, mempty)
80 (a1,b1,c1,d1) `mappend` (a2,b2,c2,d2) =
81 (a1 `mappend` a2, b1 `mappend` b2,
82 c1 `mappend` c2, d1 `mappend` d2)
84 instance (Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) =>
85 Monoid (a,b,c,d,e) where
86 mempty = (mempty, mempty, mempty, mempty, mempty)
87 (a1,b1,c1,d1,e1) `mappend` (a2,b2,c2,d2,e2) =
88 (a1 `mappend` a2, b1 `mappend` b2, c1 `mappend` c2,
89 d1 `mappend` d2, e1 `mappend` e2)
91 -- lexicographical ordering
92 instance Monoid Ordering where
98 -- | The dual of a monoid, obtained by swapping the arguments of 'mappend'.
99 newtype Dual a = Dual { getDual :: a }
100 deriving (Eq, Ord, Read, Show, Bounded)
102 instance Monoid a => Monoid (Dual a) where
104 Dual x `mappend` Dual y = Dual (y `mappend` x)
106 -- | The monoid of endomorphisms under composition.
107 newtype Endo a = Endo { appEndo :: a -> a }
109 instance Monoid (Endo a) where
111 Endo f `mappend` Endo g = Endo (f . g)
113 -- | Boolean monoid under conjunction.
114 newtype All = All { getAll :: Bool }
115 deriving (Eq, Ord, Read, Show, Bounded)
117 instance Monoid All where
119 All x `mappend` All y = All (x && y)
121 -- | Boolean monoid under disjunction.
122 newtype Any = Any { getAny :: Bool }
123 deriving (Eq, Ord, Read, Show, Bounded)
125 instance Monoid Any where
127 Any x `mappend` Any y = Any (x || y)
129 -- | Monoid under addition.
130 newtype Sum a = Sum { getSum :: a }
131 deriving (Eq, Ord, Read, Show, Bounded)
133 instance Num a => Monoid (Sum a) where
135 Sum x `mappend` Sum y = Sum (x + y)
137 -- | Monoid under multiplication.
138 newtype Product a = Product { getProduct :: a }
139 deriving (Eq, Ord, Read, Show, Bounded)
141 instance Num a => Monoid (Product a) where
143 Product x `mappend` Product y = Product (x * y)