1 -----------------------------------------------------------------------------
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)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : portable
12 -- The Identity monad.
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.
20 -----------------------------------------------------------------------------
22 module Control.Monad.Identity (
25 module Control.Monad.Fix,
31 import Control.Monad.Fix
33 -- ---------------------------------------------------------------------------
36 -- Abstraction for wrapping up a object.
37 -- If you have an monadic function, say:
39 -- example :: Int -> IdentityMonad Int
40 -- example x = return (x*x)
42 -- you can "run" it, using
44 -- Main> runIdentity (example 42)
47 newtype Identity a = Identity { runIdentity :: a }
49 -- ---------------------------------------------------------------------------
50 -- Identity instances for Functor and Monad
52 instance Functor Identity where
53 fmap f m = Identity (f (runIdentity m))
55 instance Monad Identity where
57 m >>= k = k (runIdentity m)
59 instance MonadFix Identity where
60 mfix f = Identity (fix (runIdentity . f))