282eddbeaf506449badb43dc958373f80f797c7b
[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/core/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         runIdentity,
25         module Control.Monad,
26         module Control.Monad.Fix,
27    ) where
28
29 import Prelude
30
31 import Control.Monad
32 import Control.Monad.Fix
33
34 -- ---------------------------------------------------------------------------
35 -- Identity wrapper
36 --
37 --      Abstraction for wrapping up a object.
38 --      If you have an monadic function, say:
39 --
40 --          example :: Int -> IdentityMonad Int
41 --          example x = return (x*x)
42 --
43 --      you can "run" it, using
44 --
45 --        Main> runIdentity (example 42)
46 --        1764 :: Int
47
48 newtype Identity a = Identity { runIdentity :: a }
49
50 -- ---------------------------------------------------------------------------
51 -- Identity instances for Functor and Monad
52
53 instance Functor Identity where
54         fmap f m = Identity (f (runIdentity m))
55
56 instance Monad Identity where
57         return a = Identity a
58         m >>= k  = k (runIdentity m)
59
60 instance MonadFix Identity where
61         mfix f = Identity (fix (runIdentity . f))