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 -----------------------------------------------------------------------------
31 -- ---------------------------------------------------------------------------
32 -- | The monoid class.
33 -- A minimal complete definition must supply 'mempty' and 'mappend',
34 -- and these should satisfy the monoid laws.
38 -- ^ Identity of 'mappend'
39 mappend :: a -> a -> a
40 -- ^ An associative operation
43 -- ^ Fold a list using the monoid.
44 -- For most types, the default definition for 'mconcat' will be
45 -- used, but the function is included in the class definition so
46 -- that an optimized version can be provided for specific types.
48 mconcat = foldr mappend mempty
52 instance Monoid [a] where
56 instance Monoid b => Monoid (a -> b) where
58 mappend f g x = f x `mappend` g x
60 instance Monoid () where
61 -- Should it be strict?
66 instance (Monoid a, Monoid b) => Monoid (a,b) where
67 mempty = (mempty, mempty)
68 (a1,b1) `mappend` (a2,b2) =
69 (a1 `mappend` a2, b1 `mappend` b2)
71 instance (Monoid a, Monoid b, Monoid c) => Monoid (a,b,c) where
72 mempty = (mempty, mempty, mempty)
73 (a1,b1,c1) `mappend` (a2,b2,c2) =
74 (a1 `mappend` a2, b1 `mappend` b2, c1 `mappend` c2)
76 instance (Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a,b,c,d) where
77 mempty = (mempty, mempty, mempty, mempty)
78 (a1,b1,c1,d1) `mappend` (a2,b2,c2,d2) =
79 (a1 `mappend` a2, b1 `mappend` b2,
80 c1 `mappend` c2, d1 `mappend` d2)
82 instance (Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) =>
83 Monoid (a,b,c,d,e) where
84 mempty = (mempty, mempty, mempty, mempty, mempty)
85 (a1,b1,c1,d1,e1) `mappend` (a2,b2,c2,d2,e2) =
86 (a1 `mappend` a2, b1 `mappend` b2, c1 `mappend` c2,
87 d1 `mappend` d2, e1 `mappend` e2)
89 -- lexicographical ordering
90 instance Monoid Ordering where
96 -- | The monoid of endomorphisms under composition.
97 newtype Endo a = Endo { appEndo :: a -> a }
99 instance Monoid (Endo a) where
101 Endo f `mappend` Endo g = Endo (f . g)
103 -- | The dual of a monoid, obtained by swapping the arguments of 'mappend'.
104 newtype Dual a = Dual { getDual :: a }
106 instance Monoid a => Monoid (Dual a) where
108 Dual x `mappend` Dual y = Dual (y `mappend` x)
110 -- | Monoid under addition.
111 newtype Sum a = Sum { getSum :: a }
113 instance Num a => Monoid (Sum a) where
115 Sum x `mappend` Sum y = Sum (x + y)
117 -- | Monoid under multiplication.
118 newtype Product a = Product { getProduct :: a }
120 instance Num a => Monoid (Product a) where
122 Product x `mappend` Product y = Product (x * y)