{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
#endif
#if __GLASGOW_HASKELL__ >= 706
{-# LANGUAGE PolyKinds #-}
#endif
#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE AutoDeriveTypeable #-}
#endif
module Control.Monad.Trans.Identity (
IdentityT(..),
mapIdentityT,
liftCatch,
liftCallCC,
) where
import Control.Monad.IO.Class (MonadIO(liftIO))
import Control.Monad.Signatures
import Control.Monad.Trans.Class (MonadTrans(lift))
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
import Data.Traversable (Traversable(traverse))
import Prelude hiding (foldr, foldr1, foldl, foldl1, null, length)
newtype IdentityT f a = IdentityT { forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT :: f a }
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) = (a -> b -> Bool) -> f a -> f b -> Bool
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) = (a -> b -> Ordering) -> f a -> f b -> Ordering
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 = (String -> ReadS (IdentityT f a)) -> Int -> ReadS (IdentityT f a)
forall a. (String -> ReadS a) -> Int -> ReadS a
readsData ((String -> ReadS (IdentityT f a)) -> Int -> ReadS (IdentityT f a))
-> (String -> ReadS (IdentityT f a))
-> Int
-> ReadS (IdentityT f a)
forall a b. (a -> b) -> a -> b
$
(Int -> ReadS (f a))
-> String
-> (f a -> IdentityT f a)
-> String
-> ReadS (IdentityT f a)
forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith ((Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl) String
"IdentityT" f a -> IdentityT f a
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) =
(Int -> f a -> ShowS) -> String -> Int -> f a -> ShowS
forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith ((Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
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
(==) = 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 = IdentityT f a -> IdentityT f a -> Ordering
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 = Int -> ReadS (IdentityT f a)
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 = Int -> IdentityT f a -> ShowS
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 = (m a -> m b) -> IdentityT m a -> IdentityT m b
forall {k} {k} (m :: k -> *) (a :: k) (n :: k -> *) (b :: k).
(m a -> n b) -> IdentityT m a -> IdentityT n b
mapIdentityT ((a -> b) -> m a -> m b
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) = (a -> m) -> f a -> m
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) = (a -> b -> b) -> b -> f a -> b
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) = (b -> a -> b) -> b -> f a -> b
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) = (a -> a -> a) -> f a -> a
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) = (a -> a -> a) -> f a -> a
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) = f a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null f a
t
length :: forall a. IdentityT f a -> Int
length (IdentityT f a
t) = f a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length f a
t
#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) = f b -> IdentityT f b
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (f b -> IdentityT f b) -> f (f b) -> f (IdentityT f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> f a -> f (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 = m a -> IdentityT m a
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (a -> m a
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
(<*>) = (m (a -> b) -> m a -> m 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 m (a -> b) -> m a -> m b
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
(*>) = (m a -> m b -> m 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 m a -> m b -> m b
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
(<*) = (m a -> m b -> m a)
-> 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 m a -> m b -> m a
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 = m a -> IdentityT m a
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT m a
forall (f :: * -> *) a. Alternative f => f a
empty
{-# INLINE empty #-}
<|> :: forall a. IdentityT m a -> IdentityT m a -> IdentityT m a
(<|>) = (m a -> m a -> m 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 m a -> m a -> m a
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 = m b -> IdentityT m b
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (m b -> IdentityT m b) -> m b -> IdentityT m b
forall a b. (a -> b) -> a -> b
$ IdentityT m b -> m b
forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT (IdentityT m b -> m b) -> (a -> IdentityT m b) -> a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> IdentityT m b
k (a -> m b) -> m a -> m b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IdentityT m a -> m a
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 = m a -> IdentityT m a
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (m a -> IdentityT m a) -> m a -> IdentityT m a
forall a b. (a -> b) -> a -> b
$ String -> m a
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 = m a -> IdentityT m a
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
{-# INLINE mzero #-}
mplus :: forall a. IdentityT m a -> IdentityT m a -> IdentityT m a
mplus = (m a -> m a -> m 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 m a -> m a -> m a
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 = m a -> IdentityT m a
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT ((a -> m a) -> m a
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (IdentityT m a -> m a
forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT (IdentityT m a -> m a) -> (a -> IdentityT m a) -> a -> m a
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 = m a -> IdentityT m a
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (m a -> IdentityT m a) -> (IO a -> m a) -> IO a -> IdentityT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
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 = (m a -> m b -> m c)
-> IdentityT m a -> IdentityT m b -> IdentityT m c
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 ((a -> b -> c) -> m a -> m b -> m c
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 = m a -> IdentityT m a
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 = f a' -> IdentityT f a'
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (f a' -> IdentityT f a')
-> (IdentityT f a -> f a') -> IdentityT f a -> IdentityT f a'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a' -> a) -> f a -> f a'
forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f (f a -> f a') -> (IdentityT f a -> f a) -> IdentityT f a -> f a'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IdentityT f a -> f a
forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT
{-# INLINE contramap #-}
#endif
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 = n b -> IdentityT n b
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (n b -> IdentityT n b)
-> (IdentityT m a -> n b) -> IdentityT m a -> IdentityT n b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> n b
f (m a -> n b) -> (IdentityT m a -> m a) -> IdentityT m a -> n b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IdentityT m a -> m a
forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT
{-# INLINE mapIdentityT #-}
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 = p c -> IdentityT p c
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (m a -> n b -> p c
f (IdentityT m a -> m a
forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT IdentityT m a
a) (IdentityT n b -> n b
forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT IdentityT n b
b))
{-# INLINE lift2IdentityT #-}
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 =
m a -> IdentityT m a
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (m a -> IdentityT m a) -> m a -> IdentityT m a
forall a b. (a -> b) -> a -> b
$ CallCC m a b
callCC CallCC m a b -> CallCC m a b
forall a b. (a -> b) -> a -> b
$ \ a -> m b
c -> IdentityT m a -> m a
forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT ((a -> IdentityT m b) -> IdentityT m a
f (m b -> IdentityT m b
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (m b -> IdentityT m b) -> (a -> m b) -> a -> IdentityT m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m b
c))
{-# INLINE liftCallCC #-}
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 = m a -> IdentityT m a
forall {k} (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (m a -> IdentityT m a) -> m a -> IdentityT m a
forall a b. (a -> b) -> a -> b
$ Catch e m a
f (IdentityT m a -> m a
forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT IdentityT m a
m) (IdentityT m a -> m a
forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT (IdentityT m a -> m a) -> (e -> IdentityT m a) -> e -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> IdentityT m a
h)
{-# INLINE liftCatch #-}