{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
{-# LANGUAGE DeriveGeneric #-}
#endif
#if __GLASGOW_HASKELL__ >= 706
{-# LANGUAGE PolyKinds #-}
#endif
#if __GLASGOW_HASKELL__ >= 710 && __GLASGOW_HASKELL__ < 802
{-# LANGUAGE AutoDeriveTypeable #-}
#endif
-----------------------------------------------------------------------------
-- |
-- Module      :  Control.Monad.Trans.Identity
-- Copyright   :  (c) 2007 Magnus Therning
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  R.Paterson@city.ac.uk
-- Stability   :  experimental
-- Portability :  portable
--
-- The identity monad transformer.
--
-- This is useful for functions parameterized by a monad transformer.
-----------------------------------------------------------------------------

module Control.Monad.Trans.Identity (
    -- * The identity monad transformer
    IdentityT(..),
    mapIdentityT,
    -- * Lifting other operations
    liftCatch,
    liftCallCC,
  ) where

import Control.Monad.IO.Class (MonadIO(liftIO))
import Control.Monad.Signatures
import Control.Monad.Trans.Class (MonadTrans(lift))
#if MIN_VERSION_base(4,18,0)
import Data.Foldable1 (Foldable1(foldMap1))
#endif
import Data.Functor.Classes
#if MIN_VERSION_base(4,12,0)
import Data.Functor.Contravariant
#endif

import Control.Applicative
import Control.Monad (MonadPlus(mzero, mplus))
#if MIN_VERSION_base(4,9,0)
import qualified Control.Monad.Fail as Fail
#endif
import Control.Monad.Fix (MonadFix(mfix))
#if MIN_VERSION_base(4,4,0)
import Control.Monad.Zip (MonadZip(mzipWith))
#endif
import Data.Foldable
#if !(MIN_VERSION_base(4,8,0))
import Data.Traversable (Traversable(traverse))
#endif
import Prelude hiding (foldr, foldr1, foldl, foldl1, null, length)
#if __GLASGOW_HASKELL__ >= 704
import GHC.Generics
#endif

-- | The trivial monad transformer, which maps a monad to an equivalent monad.
newtype IdentityT f a = IdentityT { forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT :: f a }
#if __GLASGOW_HASKELL__ >= 710
    deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (f :: k -> *) (a :: k) x.
Rep (IdentityT f a) x -> IdentityT f a
forall k (f :: k -> *) (a :: k) x.
IdentityT f a -> Rep (IdentityT f a) x
$cto :: forall k (f :: k -> *) (a :: k) x.
Rep (IdentityT f a) x -> IdentityT f a
$cfrom :: forall k (f :: k -> *) (a :: k) x.
IdentityT f a -> Rep (IdentityT f a) x
Generic, forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
forall k (f :: k -> *) (a :: k).
Rep1 (IdentityT f) a -> IdentityT f a
forall k (f :: k -> *) (a :: k).
IdentityT f a -> Rep1 (IdentityT f) a
$cto1 :: forall k (f :: k -> *) (a :: k).
Rep1 (IdentityT f) a -> IdentityT f a
$cfrom1 :: forall k (f :: k -> *) (a :: k).
IdentityT f a -> Rep1 (IdentityT f) a
Generic1)
#elif __GLASGOW_HASKELL__ >= 704
    deriving (Generic)
#endif

instance (Eq1 f) => Eq1 (IdentityT f) where
    liftEq :: forall a b.
(a -> b -> Bool) -> IdentityT f a -> IdentityT f b -> Bool
liftEq a -> b -> Bool
eq (IdentityT f a
x) (IdentityT f b
y) = forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq f a
x f b
y
    {-# INLINE liftEq #-}

instance (Ord1 f) => Ord1 (IdentityT f) where
    liftCompare :: forall a b.
(a -> b -> Ordering) -> IdentityT f a -> IdentityT f b -> Ordering
liftCompare a -> b -> Ordering
comp (IdentityT f a
x) (IdentityT f b
y) = forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
comp f a
x f b
y
    {-# INLINE liftCompare #-}

instance (Read1 f) => Read1 (IdentityT f) where
    liftReadsPrec :: forall a.
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (IdentityT f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl = forall a. (String -> ReadS a) -> Int -> ReadS a
readsData forall a b. (a -> b) -> a -> b
$
        forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith (forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl) String
"IdentityT" forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT

instance (Show1 f) => Show1 (IdentityT f) where
    liftShowsPrec :: forall a.
(Int -> a -> ShowS)
-> ([a] -> ShowS) -> Int -> IdentityT f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d (IdentityT f a
m) =
        forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith (forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl) String
"IdentityT" Int
d f a
m

instance (Eq1 f, Eq a) => Eq (IdentityT f a) where == :: IdentityT f a -> IdentityT f a -> Bool
(==) = forall (f :: * -> *) a. (Eq1 f, Eq a) => f a -> f a -> Bool
eq1
instance (Ord1 f, Ord a) => Ord (IdentityT f a) where compare :: IdentityT f a -> IdentityT f a -> Ordering
compare = forall (f :: * -> *) a. (Ord1 f, Ord a) => f a -> f a -> Ordering
compare1
instance (Read1 f, Read a) => Read (IdentityT f a) where readsPrec :: Int -> ReadS (IdentityT f a)
readsPrec = forall (f :: * -> *) a. (Read1 f, Read a) => Int -> ReadS (f a)
readsPrec1
instance (Show1 f, Show a) => Show (IdentityT f a) where showsPrec :: Int -> IdentityT f a -> ShowS
showsPrec = forall (f :: * -> *) a. (Show1 f, Show a) => Int -> f a -> ShowS
showsPrec1

instance (Functor m) => Functor (IdentityT m) where
    fmap :: forall a b. (a -> b) -> IdentityT m a -> IdentityT m b
fmap a -> b
f = forall {k} {k} (m :: k -> *) (a :: k) (n :: k -> *) (b :: k).
(m a -> n b) -> IdentityT m a -> IdentityT n b
mapIdentityT (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
    {-# INLINE fmap #-}

instance (Foldable f) => Foldable (IdentityT f) where
    foldMap :: forall m a. Monoid m => (a -> m) -> IdentityT f a -> m
foldMap a -> m
f (IdentityT f a
t) = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f f a
t
    {-# INLINE foldMap #-}
    foldr :: forall a b. (a -> b -> b) -> b -> IdentityT f a -> b
foldr a -> b -> b
f b
z (IdentityT f a
t) = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f b
z f a
t
    {-# INLINE foldr #-}
    foldl :: forall b a. (b -> a -> b) -> b -> IdentityT f a -> b
foldl b -> a -> b
f b
z (IdentityT f a
t) = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl b -> a -> b
f b
z f a
t
    {-# INLINE foldl #-}
    foldr1 :: forall a. (a -> a -> a) -> IdentityT f a -> a
foldr1 a -> a -> a
f (IdentityT f a
t) = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 a -> a -> a
f f a
t
    {-# INLINE foldr1 #-}
    foldl1 :: forall a. (a -> a -> a) -> IdentityT f a -> a
foldl1 a -> a -> a
f (IdentityT f a
t) = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 a -> a -> a
f f a
t
    {-# INLINE foldl1 #-}
#if MIN_VERSION_base(4,8,0)
    null :: forall a. IdentityT f a -> Bool
null (IdentityT f a
t) = forall (t :: * -> *) a. Foldable t => t a -> Bool
null f a
t
    length :: forall a. IdentityT f a -> Int
length (IdentityT f a
t) = forall (t :: * -> *) a. Foldable t => t a -> Int
length f a
t
#endif

#if MIN_VERSION_base(4,18,0)
instance (Foldable1 m) => Foldable1 (IdentityT m) where
    foldMap1 f (IdentityT t) = foldMap1 f t
    {-# INLINE foldMap1 #-}
#endif

instance (Traversable f) => Traversable (IdentityT f) where
    traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> IdentityT f a -> f (IdentityT f b)
traverse a -> f b
f (IdentityT f a
a) = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f f a
a
    {-# INLINE traverse #-}

instance (Applicative m) => Applicative (IdentityT m) where
    pure :: forall a. a -> IdentityT m a
pure a
x = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
    {-# INLINE pure #-}
    <*> :: forall a b. IdentityT m (a -> b) -> IdentityT m a -> IdentityT m b
(<*>) = forall {k} {k} {k} (m :: k -> *) (a :: k) (n :: k -> *) (b :: k)
       (p :: k -> *) (c :: k).
(m a -> n b -> p c)
-> IdentityT m a -> IdentityT n b -> IdentityT p c
lift2IdentityT forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)
    {-# INLINE (<*>) #-}
    *> :: forall a b. IdentityT m a -> IdentityT m b -> IdentityT m b
(*>) = forall {k} {k} {k} (m :: k -> *) (a :: k) (n :: k -> *) (b :: k)
       (p :: k -> *) (c :: k).
(m a -> n b -> p c)
-> IdentityT m a -> IdentityT n b -> IdentityT p c
lift2IdentityT forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
    {-# INLINE (*>) #-}
    <* :: forall a b. IdentityT m a -> IdentityT m b -> IdentityT m a
(<*) = forall {k} {k} {k} (m :: k -> *) (a :: k) (n :: k -> *) (b :: k)
       (p :: k -> *) (c :: k).
(m a -> n b -> p c)
-> IdentityT m a -> IdentityT n b -> IdentityT p c
lift2IdentityT forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
(<*)
    {-# INLINE (<*) #-}

instance (Alternative m) => Alternative (IdentityT m) where
    empty :: forall a. IdentityT m a
empty = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT forall (f :: * -> *) a. Alternative f => f a
empty
    {-# INLINE empty #-}
    <|> :: forall a. IdentityT m a -> IdentityT m a -> IdentityT m a
(<|>) = forall {k} {k} {k} (m :: k -> *) (a :: k) (n :: k -> *) (b :: k)
       (p :: k -> *) (c :: k).
(m a -> n b -> p c)
-> IdentityT m a -> IdentityT n b -> IdentityT p c
lift2IdentityT forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
    {-# INLINE (<|>) #-}

instance (Monad m) => Monad (IdentityT m) where
#if !(MIN_VERSION_base(4,8,0))
    return = IdentityT . return
    {-# INLINE return #-}
#endif
    IdentityT m a
m >>= :: forall a b. IdentityT m a -> (a -> IdentityT m b) -> IdentityT m b
>>= a -> IdentityT m b
k = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT forall a b. (a -> b) -> a -> b
$ forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> IdentityT m b
k forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT IdentityT m a
m
    {-# INLINE (>>=) #-}
#if !(MIN_VERSION_base(4,13,0))
    fail msg = IdentityT $ fail msg
    {-# INLINE fail #-}
#endif

#if MIN_VERSION_base(4,9,0)
instance (Fail.MonadFail m) => Fail.MonadFail (IdentityT m) where
    fail :: forall a. String -> IdentityT m a
fail String
msg = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => String -> m a
Fail.fail String
msg
    {-# INLINE fail #-}
#endif

instance (MonadPlus m) => MonadPlus (IdentityT m) where
    mzero :: forall a. IdentityT m a
mzero = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT forall (m :: * -> *) a. MonadPlus m => m a
mzero
    {-# INLINE mzero #-}
    mplus :: forall a. IdentityT m a -> IdentityT m a -> IdentityT m a
mplus = forall {k} {k} {k} (m :: k -> *) (a :: k) (n :: k -> *) (b :: k)
       (p :: k -> *) (c :: k).
(m a -> n b -> p c)
-> IdentityT m a -> IdentityT n b -> IdentityT p c
lift2IdentityT forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus
    {-# INLINE mplus #-}

instance (MonadFix m) => MonadFix (IdentityT m) where
    mfix :: forall a. (a -> IdentityT m a) -> IdentityT m a
mfix a -> IdentityT m a
f = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> IdentityT m a
f))
    {-# INLINE mfix #-}

instance (MonadIO m) => MonadIO (IdentityT m) where
    liftIO :: forall a. IO a -> IdentityT m a
liftIO = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
    {-# INLINE liftIO #-}

#if MIN_VERSION_base(4,4,0)
instance (MonadZip m) => MonadZip (IdentityT m) where
    mzipWith :: forall a b c.
(a -> b -> c) -> IdentityT m a -> IdentityT m b -> IdentityT m c
mzipWith a -> b -> c
f = forall {k} {k} {k} (m :: k -> *) (a :: k) (n :: k -> *) (b :: k)
       (p :: k -> *) (c :: k).
(m a -> n b -> p c)
-> IdentityT m a -> IdentityT n b -> IdentityT p c
lift2IdentityT (forall (m :: * -> *) a b c.
MonadZip m =>
(a -> b -> c) -> m a -> m b -> m c
mzipWith a -> b -> c
f)
    {-# INLINE mzipWith #-}
#endif

instance MonadTrans IdentityT where
    lift :: forall (m :: * -> *) a. Monad m => m a -> IdentityT m a
lift = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT
    {-# INLINE lift #-}

#if MIN_VERSION_base(4,12,0)
instance (Contravariant f) => Contravariant (IdentityT f) where
    contramap :: forall a' a. (a' -> a) -> IdentityT f a -> IdentityT f a'
contramap a' -> a
f = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT
    {-# INLINE contramap #-}
#endif

-- | Lift a unary operation to the new monad.
mapIdentityT :: (m a -> n b) -> IdentityT m a -> IdentityT n b
mapIdentityT :: forall {k} {k} (m :: k -> *) (a :: k) (n :: k -> *) (b :: k).
(m a -> n b) -> IdentityT m a -> IdentityT n b
mapIdentityT m a -> n b
f = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> n b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT
{-# INLINE mapIdentityT #-}

-- | Lift a binary operation to the new monad.
lift2IdentityT ::
    (m a -> n b -> p c) -> IdentityT m a -> IdentityT n b -> IdentityT p c
lift2IdentityT :: forall {k} {k} {k} (m :: k -> *) (a :: k) (n :: k -> *) (b :: k)
       (p :: k -> *) (c :: k).
(m a -> n b -> p c)
-> IdentityT m a -> IdentityT n b -> IdentityT p c
lift2IdentityT m a -> n b -> p c
f IdentityT m a
a IdentityT n b
b = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (m a -> n b -> p c
f (forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT IdentityT m a
a) (forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT IdentityT n b
b))
{-# INLINE lift2IdentityT #-}

-- | Lift a @callCC@ operation to the new monad.
liftCallCC :: CallCC m a b -> CallCC (IdentityT m) a b
liftCallCC :: forall (m :: * -> *) a b. CallCC m a b -> CallCC (IdentityT m) a b
liftCallCC CallCC m a b
callCC (a -> IdentityT m b) -> IdentityT m a
f =
    forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT forall a b. (a -> b) -> a -> b
$ CallCC m a b
callCC forall a b. (a -> b) -> a -> b
$ \ a -> m b
c -> forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT ((a -> IdentityT m b) -> IdentityT m a
f (forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m b
c))
{-# INLINE liftCallCC #-}

-- | Lift a @catchE@ operation to the new monad.
liftCatch :: Catch e m a -> Catch e (IdentityT m) a
liftCatch :: forall {k} e (m :: k -> *) (a :: k).
Catch e m a -> Catch e (IdentityT m) a
liftCatch Catch e m a
f IdentityT m a
m e -> IdentityT m a
h = forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT forall a b. (a -> b) -> a -> b
$ Catch e m a
f (forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT IdentityT m a
m) (forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> IdentityT m a
h)
{-# INLINE liftCatch #-}