[project @ 2002-05-09 13:16:29 by simonmar]
[ghc-base.git] / Control / Monad / Monoid.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Control.Monad.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)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  experimental
10 -- Portability :  non-portable ( requires mulit-parameter type classes )
11 --
12 -- Declaration of the Monoid class,and instances for list and functions
13 --
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 -----------------------------------------------------------------------------
20
21 module Control.Monad.Monoid (
22         Monoid(..)
23   ) where
24
25 import Prelude
26
27 -- ---------------------------------------------------------------------------
28 -- The Monoid class
29
30 class Monoid a where
31         mempty  :: a
32         mappend :: a -> a -> a
33         mconcat :: [a] -> a
34
35 -- Now the default for mconcat.  For most types, this
36 -- default will be used, but is included in the class definition so
37 -- that optimized version of mconcat can be provided
38 -- for specific types.
39
40         mconcat = foldr mappend mempty
41
42 -- Monoid instances.
43
44 instance Monoid [a] where
45         mempty  = []
46         mappend = (++)
47
48 instance Monoid (a -> a) where
49         mempty  = id
50         mappend = (.)
51
52 instance Monoid () where
53         -- Should it be strict?
54         mempty        = ()
55         _ `mappend` _ = ()
56         mconcat _     = ()