Conditionally expose Data.Functor.* modules
[darcs-mirrors/transformers.git] / legacy / pre709 / 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 Trustworthy #-}
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.Monoid (Monoid(mempty, mappend))
49 import Data.Traversable (Traversable(traverse))
50 #if __GLASGOW_HASKELL__ >= 612
51 import Data.Data
52 #endif
53 #if __GLASGOW_HASKELL__ >= 702
54 import GHC.Generics
55 #endif
56
57 -- | Identity functor and monad. (a non-strict monad)
58 newtype Identity a = Identity { runIdentity :: a }
59 deriving ( Eq, Ord
60 #if __GLASGOW_HASKELL__ >= 612
61 , Data, Typeable
62 #endif
63 #if __GLASGOW_HASKELL__ >= 702
64 , Generic
65 #endif
66 #if __GLASGOW_HASKELL__ >= 706
67 , Generic1
68 #endif
69 )
70
71 instance (Monoid a) => Monoid (Identity a) where
72 mempty = Identity mempty
73 mappend (Identity x) (Identity y) = Identity (mappend x y)
74
75 -- These instances would be equivalent to the derived instances of the
76 -- newtype if the field were removed.
77
78 instance (Read a) => Read (Identity a) where
79 readsPrec d = readParen (d > 10) $ \ r ->
80 [(Identity x,t) | ("Identity",s) <- lex r, (x,t) <- readsPrec 11 s]
81
82 instance (Show a) => Show (Identity a) where
83 showsPrec d (Identity x) = showParen (d > 10) $
84 showString "Identity " . showsPrec 11 x
85
86 -- ---------------------------------------------------------------------------
87 -- Identity instances for Functor and Monad
88
89 instance Functor Identity where
90 fmap f m = Identity (f (runIdentity m))
91
92 instance Foldable Identity where
93 foldMap f (Identity x) = f x
94
95 instance Traversable Identity where
96 traverse f (Identity x) = Identity <$> f x
97
98 instance Applicative Identity where
99 pure a = Identity a
100 Identity f <*> Identity x = Identity (f x)
101
102 instance Monad Identity where
103 return a = Identity a
104 m >>= k = k (runIdentity m)
105
106 instance MonadFix Identity where
107 mfix f = Identity (fix (runIdentity . f))
108
109 #if MIN_VERSION_base(4,4,0)
110 instance MonadZip Identity where
111 mzipWith f (Identity x) (Identity y) = Identity (f x y)
112 munzip (Identity (a, b)) = (Identity a, Identity b)
113 #endif