Optimise `Identity` instances with `coerce`
[ghc.git] / libraries / base / Data / Functor / Identity.hs
1 {-# LANGUAGE AutoDeriveTypeable #-}
2 {-# LANGUAGE DeriveTraversable #-}
3
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : Data.Functor.Identity
7 -- Copyright : (c) Andy Gill 2001,
8 -- (c) Oregon Graduate Institute of Science and Technology 2001
9 -- License : BSD-style (see the file LICENSE)
10 --
11 -- Maintainer : ross@soi.city.ac.uk
12 -- Stability : experimental
13 -- Portability : portable
14 --
15 -- The identity functor and monad.
16 --
17 -- This trivial type constructor serves two purposes:
18 --
19 -- * It can be used with functions parameterized by functor or monad classes.
20 --
21 -- * It can be used as a base monad to which a series of monad
22 -- transformers may be applied to construct a composite monad.
23 -- Most monad transformer modules include the special case of
24 -- applying the transformer to 'Identity'. For example, @State s@
25 -- is an abbreviation for @StateT s 'Identity'@.
26 --
27 -- /Since: 4.8.0.0/
28 -----------------------------------------------------------------------------
29
30 module Data.Functor.Identity (
31 Identity(..),
32 ) where
33
34 import Control.Monad.Fix
35 import Data.Coerce
36 import Data.Foldable
37
38 -- | Identity functor and monad. (a non-strict monad)
39 --
40 -- /Since: 4.8.0.0/
41 newtype Identity a = Identity { runIdentity :: a }
42 deriving (Eq, Ord, Traversable)
43
44 -- | This instance would be equivalent to the derived instances of the
45 -- 'Identity' newtype if the 'runIdentity' field were removed
46 instance (Read a) => Read (Identity a) where
47 readsPrec d = readParen (d > 10) $ \ r ->
48 [(Identity x,t) | ("Identity",s) <- lex r, (x,t) <- readsPrec 11 s]
49
50 -- | This instance would be equivalent to the derived instances of the
51 -- 'Identity' newtype if the 'runIdentity' field were removed
52 instance (Show a) => Show (Identity a) where
53 showsPrec d (Identity x) = showParen (d > 10) $
54 showString "Identity " . showsPrec 11 x
55
56 -- ---------------------------------------------------------------------------
57 -- Identity instances for Functor and Monad
58
59 instance Foldable Identity where
60 foldMap = coerce
61
62 elem = (. runIdentity) #. (==)
63 foldl = coerce
64 foldl' = coerce
65 foldl1 _ = runIdentity
66 foldr f z (Identity x) = f x z
67 foldr' = foldr
68 foldr1 _ = runIdentity
69 length _ = 1
70 maximum = runIdentity
71 minimum = runIdentity
72 null _ = False
73 product = runIdentity
74 sum = runIdentity
75 toList (Identity x) = [x]
76
77 instance Functor Identity where
78 fmap = coerce
79
80 instance Applicative Identity where
81 pure = Identity
82 (<*>) = coerce
83
84 instance Monad Identity where
85 return = Identity
86 m >>= k = k (runIdentity m)
87
88 instance MonadFix Identity where
89 mfix f = Identity (fix (runIdentity . f))
90
91
92 -- | Internal (non-exported) 'Coercible' helper for 'elem'
93 --
94 -- See Note [Function coercion] in "Data.Foldable" for more details.
95 (#.) :: Coercible b c => (b -> c) -> (a -> b) -> a -> c
96 (#.) _f = coerce