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 (
26 module Control.Monad.Fix,
32 import Control.Monad.Fix
34 -- ---------------------------------------------------------------------------
37 -- Abstraction for wrapping up a object.
38 -- If you have an monadic function, say:
40 -- example :: Int -> IdentityMonad Int
41 -- example x = return (x*x)
43 -- you can "run" it, using
45 -- Main> runIdentity (example 42)
48 newtype Identity a = Identity { runIdentity :: a }
50 -- ---------------------------------------------------------------------------
51 -- Identity instances for Functor and Monad
53 instance Functor Identity where
54 fmap f m = Identity (f (runIdentity m))
56 instance Monad Identity where
58 m >>= k = k (runIdentity m)
60 instance MonadFix Identity where
61 mfix f = Identity (fix (runIdentity . f))