[project @ 2004-02-05 11:58:21 by malcolm]
[ghc-base.git] / Control / Monad / Identity.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Control.Monad.Identity
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 :  portable
11 --
12 -- The Identity monad.
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
22 module Control.Monad.Identity (
23         Identity(..),
24         module Control.Monad,
25         module Control.Monad.Fix,
26    ) where
27
28 import Prelude
29
30 import Control.Monad
31 import Control.Monad.Fix
32
33 -- ---------------------------------------------------------------------------
34 -- Identity wrapper
35 --
36 --      Abstraction for wrapping up a object.
37 --      If you have an monadic function, say:
38 --
39 --          example :: Int -> IdentityMonad Int
40 --          example x = return (x*x)
41 --
42 --      you can "run" it, using
43 --
44 --        Main> runIdentity (example 42)
45 --        1764 :: Int
46
47 newtype Identity a = Identity { runIdentity :: a }
48
49 -- ---------------------------------------------------------------------------
50 -- Identity instances for Functor and Monad
51
52 instance Functor Identity where
53         fmap f m = Identity (f (runIdentity m))
54
55 instance Monad Identity where
56         return a = Identity a
57         m >>= k  = k (runIdentity m)
58
59 instance MonadFix Identity where
60         mfix f = Identity (fix (runIdentity . f))