re-add old modules
[darcs-mirrors/transformers.git] / oldsrc709 / Data / Functor / Identity.hs
1 {-# LANGUAGE CPP #-}
2 #if __GLASGOW_HASKELL__ >= 612
3 {-# LANGUAGE DeriveDataTypeable #-}
4 #endif
5 #if __GLASGOW_HASKELL__ >= 702
6 {-# LANGUAGE DeriveGeneric #-}
7 {-# LANGUAGE Safe #-}
8 #endif
9 #if __GLASGOW_HASKELL__ >= 708
10 {-# LANGUAGE AutoDeriveTypeable #-}
11 {-# LANGUAGE DataKinds #-}
12 {-# LANGUAGE PolyKinds #-}
13 #endif
14 -----------------------------------------------------------------------------
15 -- |
16 -- Module : Data.Functor.Identity
17 -- Copyright : (c) Andy Gill 2001,
18 -- (c) Oregon Graduate Institute of Science and Technology 2001
19 -- License : BSD-style (see the file LICENSE)
20 --
21 -- Maintainer : ross@soi.city.ac.uk
22 -- Stability : experimental
23 -- Portability : portable
24 --
25 -- The identity functor and monad.
26 --
27 -- This trivial type constructor serves two purposes:
28 --
29 -- * It can be used with functions parameterized by functor or monad classes.
30 --
31 -- * It can be used as a base monad to which a series of monad
32 -- transformers may be applied to construct a composite monad.
33 -- Most monad transformer modules include the special case of
34 -- applying the transformer to 'Identity'. For example, @State s@
35 -- is an abbreviation for @StateT s 'Identity'@.
36 -----------------------------------------------------------------------------
37
38 module Data.Functor.Identity (
39 Identity(..),
40 ) where
41
42 import Control.Applicative
43 import Control.Monad.Fix
44 #if MIN_VERSION_base(4,4,0)
45 import Control.Monad.Zip (MonadZip(mzipWith, munzip))
46 #endif
47 import Data.Foldable (Foldable(foldMap))
48 import Data.Traversable (Traversable(traverse))
49 #if __GLASGOW_HASKELL__ >= 612
50 import Data.Data
51 #endif
52 #if __GLASGOW_HASKELL__ >= 702
53 import GHC.Generics
54 #endif
55
56 -- | Identity functor and monad. (a non-strict monad)
57 newtype Identity a = Identity { runIdentity :: a }
58 deriving ( Eq, Ord
59 #if __GLASGOW_HASKELL__ >= 612
60 , Data, Typeable
61 #endif
62 #if __GLASGOW_HASKELL__ >= 702
63 , Generic
64 #endif
65 #if __GLASGOW_HASKELL__ >= 706
66 , Generic1
67 #endif
68 )
69
70 -- These instances would be equivalent to the derived instances of the
71 -- newtype if the field were removed.
72
73 instance (Read a) => Read (Identity a) where
74 readsPrec d = readParen (d > 10) $ \ r ->
75 [(Identity x,t) | ("Identity",s) <- lex r, (x,t) <- readsPrec 11 s]
76
77 instance (Show a) => Show (Identity a) where
78 showsPrec d (Identity x) = showParen (d > 10) $
79 showString "Identity " . showsPrec 11 x
80
81 -- ---------------------------------------------------------------------------
82 -- Identity instances for Functor and Monad
83
84 instance Functor Identity where
85 fmap f m = Identity (f (runIdentity m))
86
87 instance Foldable Identity where
88 foldMap f (Identity x) = f x
89
90 instance Traversable Identity where
91 traverse f (Identity x) = Identity <$> f x
92
93 instance Applicative Identity where
94 pure a = Identity a
95 Identity f <*> Identity x = Identity (f x)
96
97 instance Monad Identity where
98 return a = Identity a
99 m >>= k = k (runIdentity m)
100
101 instance MonadFix Identity where
102 mfix f = Identity (fix (runIdentity . f))
103
104 #if MIN_VERSION_base(4,4,0)
105 instance MonadZip Identity where
106 mzipWith f (Identity x) (Identity y) = Identity (f x y)
107 munzip (Identity (a, b)) = (Identity a, Identity b)
108 #endif