{-# 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 { 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 d = readParen (d > 10) $ \ r ->
[(Identity x,t) | ("Identity",s) <- lex r, (x,t) <- readsPrec 11 s]
instance (Show a) => Show (Identity a) where
showsPrec d (Identity x) = showParen (d > 10) $
showString "Identity " . showsPrec 11 x
instance Foldable Identity where
foldMap = coerce
elem = (. runIdentity) #. (==)
foldl = coerce
foldl' = coerce
foldl1 _ = runIdentity
foldr f z (Identity x) = f x z
foldr' = foldr
foldr1 _ = runIdentity
length _ = 1
maximum = runIdentity
minimum = runIdentity
null _ = False
product = runIdentity
sum = runIdentity
toList (Identity x) = [x]
instance Functor Identity where
fmap = coerce
instance Applicative Identity where
pure = Identity
(<*>) = coerce
liftA2 = coerce
instance Monad Identity where
m >>= k = k (runIdentity m)
instance MonadFix Identity where
mfix f = Identity (fix (runIdentity . f))