[project @ 2003-06-03 22:26:44 by diatchki]
[ghc-base.git] / Control / Monad / X / 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.X.Identity (
23         Identity,   
24         runIdentity
25    ) where
26
27 import Prelude(Functor(..),Monad(..))
28 import Monad(liftM)
29
30
31 -- ---------------------------------------------------------------------------
32 -- Identity wrapper
33 --
34 --      Abstraction for wrapping up a object.
35 --      If you have an monadic function, say:
36 --
37 --          example :: Int -> Identity Int
38 --          example x = return (x*x)
39 --
40 --      you can "run" it, using
41 --
42 --        Main> runIdentity (example 42)
43 --        1764 :: Int
44
45
46 newtype Identity a    = I { unI :: a }
47
48 instance Functor Identity where
49   fmap = liftM
50
51 instance Monad Identity where
52   return    = I
53   m >>= k   = k (unI m)
54
55 runIdentity = unI
56