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