[project @ 2002-04-24 16:31:37 by simonmar]
[packages/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/core/LICENSE)
7 --
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : non-portable ( requires mulit-parameter type classes )
11 --
12 -- $Id: Monoid.hs,v 1.2 2002/04/24 16:31:38 simonmar Exp $
13 --
14 -- Declaration of the Monoid class,and instances for list and functions
15 --
16 -- Inspired by the paper
17 -- \em{Functional Programming with Overloading and
18 -- Higher-Order Polymorphism},
19 -- \A[HREF="http://www.cse.ogi.edu/~mpj"]{Mark P Jones},
20 -- Advanced School of Functional Programming, 1995.}
21 -----------------------------------------------------------------------------
22
23 module Control.Monad.Monoid (
24 Monoid(..)
25 ) where
26
27 import Prelude
28
29 -- ---------------------------------------------------------------------------
30 -- The Monoid class
31
32 class Monoid a where
33 mempty :: a
34 mappend :: a -> a -> a
35 mconcat :: [a] -> a
36
37 -- Now the default for mconcat. For most types, this
38 -- default will be used, but is included in the class definition so
39 -- that optimized version of mconcat can be provided
40 -- for specific types.
41
42 mconcat = foldr mappend mempty
43
44 -- Monoid instances.
45
46 instance Monoid [a] where
47 mempty = []
48 mappend = (++)
49
50 instance Monoid (a -> a) where
51 mempty = id
52 mappend = (.)
53
54 instance Monoid () where
55 -- Should it be strict?
56 mempty = ()
57 _ `mappend` _ = ()
58 mconcat _ = ()