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 : non-portable (requires extended type classes)
12 -- Declaration of the Monoid class, and instances for list and functions.
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 -----------------------------------------------------------------------------
26 import Data.Map ( Map )
27 import qualified Data.Map as Map hiding ( Map )
28 import Data.IntMap ( IntMap )
29 import qualified Data.IntMap as IntMap hiding ( IntMap )
30 import Data.Set ( Set )
31 import qualified Data.Set as Set hiding ( Set )
32 import Data.IntSet ( IntSet )
33 import qualified Data.IntSet as IntSet hiding ( IntSet )
35 -- ---------------------------------------------------------------------------
36 -- | The monoid class.
37 -- A minimal complete definition must supply 'mempty' and 'mappend',
38 -- and these should satisfy the monoid laws.
42 -- ^ Identity of 'mappend'
43 mappend :: a -> a -> a
44 -- ^ An associative operation
47 -- ^ Fold a list using the monoid.
48 -- For most types, the default definition for 'mconcat' will be
49 -- used, but the function is included in the class definition so
50 -- that an optimized version can be provided for specific types.
52 mconcat = foldr mappend mempty
56 instance Monoid [a] where
60 instance Monoid (a -> a) where
64 instance Monoid () where
65 -- Should it be strict?
70 instance (Monoid a, Monoid b) => Monoid (a,b) where
71 mempty = (mempty, mempty)
72 (a1,b1) `mappend` (a2,b2) =
73 (a1 `mappend` a2, b1 `mappend` b2)
75 instance (Monoid a, Monoid b, Monoid c) => Monoid (a,b,c) where
76 mempty = (mempty, mempty, mempty)
77 (a1,b1,c1) `mappend` (a2,b2,c2) =
78 (a1 `mappend` a2, b1 `mappend` b2, c1 `mappend` c2)
80 instance (Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a,b,c,d) where
81 mempty = (mempty, mempty, mempty, mempty)
82 (a1,b1,c1,d1) `mappend` (a2,b2,c2,d2) =
83 (a1 `mappend` a2, b1 `mappend` b2,
84 c1 `mappend` c2, d1 `mappend` d2)
86 instance (Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) =>
87 Monoid (a,b,c,d,e) where
88 mempty = (mempty, mempty, mempty, mempty, mempty)
89 (a1,b1,c1,d1,e1) `mappend` (a2,b2,c2,d2,e2) =
90 (a1 `mappend` a2, b1 `mappend` b2, c1 `mappend` c2,
91 d1 `mappend` d2, e1 `mappend` e2)
93 -- lexicographical ordering
94 instance Monoid Ordering where
100 instance (Ord k) => Monoid (Map k v) where
105 instance Ord a => Monoid (IntMap a) where
106 mempty = IntMap.empty
107 mappend = IntMap.union
108 mconcat = IntMap.unions
110 instance Ord a => Monoid (Set a) where
115 instance Monoid IntSet where
116 mempty = IntSet.empty
117 mappend = IntSet.union
118 mconcat = IntSet.unions