{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE Trustworthy #-}
module Data.Functor.Identity (
Identity(..),
) where
import Control.Monad.Fix
import Data.Bits (Bits, FiniteBits)
import Data.Coerce
import Data.Foldable
import Data.Functor.Utils ((#.))
import Foreign.Storable (Storable)
import GHC.Arr (Ix)
import GHC.Base ( Applicative(..), Eq(..), Functor(..), Monad(..)
, Semigroup, Monoid, Ord(..), ($), (.) )
import GHC.Enum (Bounded, Enum)
import GHC.Float (Floating, RealFloat)
import GHC.Generics (Generic, Generic1)
import GHC.Num (Num)
import GHC.Read (Read(..), lex, readParen)
import GHC.Real (Fractional, Integral, Real, RealFrac)
import GHC.Show (Show(..), showParen, showString)
import GHC.Types (Bool(..))
newtype Identity a = Identity { Identity a -> a
runIdentity :: a }
deriving ( Bits
, Bounded
, Enum
, Eq
, FiniteBits
, Floating
, Fractional
, Generic
, Generic1
, Integral
, Ix
, Semigroup
, Monoid
, Num
, Ord
, Real
, RealFrac
, RealFloat
, Storable
)
instance (Read a) => Read (Identity a) where
readsPrec :: Int -> ReadS (Identity a)
readsPrec d :: Int
d = Bool -> ReadS (Identity a) -> ReadS (Identity a)
forall a. Bool -> ReadS a -> ReadS a
readParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 10) (ReadS (Identity a) -> ReadS (Identity a))
-> ReadS (Identity a) -> ReadS (Identity a)
forall a b. (a -> b) -> a -> b
$ \ r :: String
r ->
[(a -> Identity a
forall a. a -> Identity a
Identity a
x,String
t) | ("Identity",s :: String
s) <- ReadS String
lex String
r, (x :: a
x,t :: String
t) <- Int -> ReadS a
forall a. Read a => Int -> ReadS a
readsPrec 11 String
s]
instance (Show a) => Show (Identity a) where
showsPrec :: Int -> Identity a -> ShowS
showsPrec d :: Int
d (Identity x :: a
x) = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString "Identity " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec 11 a
x
instance Foldable Identity where
foldMap :: (a -> m) -> Identity a -> m
foldMap = (a -> m) -> Identity a -> m
forall a b. Coercible a b => a -> b
coerce
elem :: a -> Identity a -> Bool
elem = ((a -> Bool) -> (Identity a -> a) -> Identity a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity a -> a
forall a. Identity a -> a
runIdentity) ((a -> Bool) -> Identity a -> Bool)
-> (a -> a -> Bool) -> a -> Identity a -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
foldl :: (b -> a -> b) -> b -> Identity a -> b
foldl = (b -> a -> b) -> b -> Identity a -> b
forall a b. Coercible a b => a -> b
coerce
foldl' :: (b -> a -> b) -> b -> Identity a -> b
foldl' = (b -> a -> b) -> b -> Identity a -> b
forall a b. Coercible a b => a -> b
coerce
foldl1 :: (a -> a -> a) -> Identity a -> a
foldl1 _ = Identity a -> a
forall a. Identity a -> a
runIdentity
foldr :: (a -> b -> b) -> b -> Identity a -> b
foldr f :: a -> b -> b
f z :: b
z (Identity x :: a
x) = a -> b -> b
f a
x b
z
foldr' :: (a -> b -> b) -> b -> Identity a -> b
foldr' = (a -> b -> b) -> b -> Identity a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
foldr1 :: (a -> a -> a) -> Identity a -> a
foldr1 _ = Identity a -> a
forall a. Identity a -> a
runIdentity
length :: Identity a -> Int
length _ = 1
maximum :: Identity a -> a
maximum = Identity a -> a
forall a. Identity a -> a
runIdentity
minimum :: Identity a -> a
minimum = Identity a -> a
forall a. Identity a -> a
runIdentity
null :: Identity a -> Bool
null _ = Bool
False
product :: Identity a -> a
product = Identity a -> a
forall a. Identity a -> a
runIdentity
sum :: Identity a -> a
sum = Identity a -> a
forall a. Identity a -> a
runIdentity
toList :: Identity a -> [a]
toList (Identity x :: a
x) = [a
x]
instance Functor Identity where
fmap :: (a -> b) -> Identity a -> Identity b
fmap = (a -> b) -> Identity a -> Identity b
forall a b. Coercible a b => a -> b
coerce
instance Applicative Identity where
pure :: a -> Identity a
pure = a -> Identity a
forall a. a -> Identity a
Identity
<*> :: Identity (a -> b) -> Identity a -> Identity b
(<*>) = Identity (a -> b) -> Identity a -> Identity b
forall a b. Coercible a b => a -> b
coerce
liftA2 :: (a -> b -> c) -> Identity a -> Identity b -> Identity c
liftA2 = (a -> b -> c) -> Identity a -> Identity b -> Identity c
forall a b. Coercible a b => a -> b
coerce
instance Monad Identity where
m :: Identity a
m >>= :: Identity a -> (a -> Identity b) -> Identity b
>>= k :: a -> Identity b
k = a -> Identity b
k (Identity a -> a
forall a. Identity a -> a
runIdentity Identity a
m)
instance MonadFix Identity where
mfix :: (a -> Identity a) -> Identity a
mfix f :: a -> Identity a
f = a -> Identity a
forall a. a -> Identity a
Identity ((a -> a) -> a
forall a. (a -> a) -> a
fix (Identity a -> a
forall a. Identity a -> a
runIdentity (Identity a -> a) -> (a -> Identity a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Identity a
f))