summaryrefslogtreecommitdiff
path: root/libraries/base/Control/Monad/Identity.hs
blob: 282eddbeaf506449badb43dc958373f80f797c7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-----------------------------------------------------------------------------
-- |
-- Module      :  Control.Monad.Identity
-- Copyright   :  (c) Andy Gill 2001,
--		  (c) Oregon Graduate Institute of Science and Technology, 2001
-- License     :  BSD-style (see the file libraries/core/LICENSE)
-- 
-- Maintainer  :  libraries@haskell.org
-- Stability   :  experimental
-- Portability :  portable
--
-- The Identity monad.
--
--	  Inspired by the paper
--	  /Functional Programming with Overloading and
--	      Higher-Order Polymorphism/, 
--	    Mark P Jones (<http://www.cse.ogi.edu/~mpj>)
--		  Advanced School of Functional Programming, 1995.
--
-----------------------------------------------------------------------------

module Control.Monad.Identity (
	Identity(..),
	runIdentity,
	module Control.Monad,
	module Control.Monad.Fix,
   ) where

import Prelude

import Control.Monad
import Control.Monad.Fix

-- ---------------------------------------------------------------------------
-- Identity wrapper
--
--	Abstraction for wrapping up a object.
--	If you have an monadic function, say:
--
--	    example :: Int -> IdentityMonad Int
--	    example x = return (x*x)
--
--      you can "run" it, using
--
--	  Main> runIdentity (example 42)
--	  1764 :: Int

newtype Identity a = Identity { runIdentity :: a }

-- ---------------------------------------------------------------------------
-- Identity instances for Functor and Monad

instance Functor Identity where
	fmap f m = Identity (f (runIdentity m))

instance Monad Identity where
	return a = Identity a
	m >>= k  = k (runIdentity m)

instance MonadFix Identity where
	mfix f = Identity (fix (runIdentity . f))