{-# LANGUAGE CPP #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UndecidableInstances #-}

module Control.Monad.Trans.Compose where

import Control.Monad.Base
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.Trans.Control
import Control.Monad.Trans.Control.Identity
import Control.Monad.Trans.Elevator
import Data.Kind

#if defined(VERSION_exceptions)
import Control.Monad.Catch qualified as Exceptions
import Control.Monad.Catch.Pure qualified as Exceptions.T
#endif

#if defined(VERSION_mtl)
import Control.Monad.Accum qualified as Mtl
import Control.Monad.Accum.OrphanInstances qualified as Mtl ()
import Control.Monad.Cont.Class qualified as Mtl
import Control.Monad.Error.Class qualified as Mtl
import Control.Monad.Reader.Class qualified as Mtl
import Control.Monad.RWS.Class qualified as Mtl (MonadRWS)
import Control.Monad.Select qualified as Mtl
import Control.Monad.Select.OrphanInstances qualified as Mtl ()
import Control.Monad.State.Class qualified as Mtl
import Control.Monad.Trans.Accum qualified as Mtl.T
import Control.Monad.Trans.Cont qualified as Mtl.T
import Control.Monad.Trans.Except qualified as Mtl.T
import Control.Monad.Trans.RWS.CPS qualified as Mtl.CPST
import Control.Monad.Trans.RWS.Lazy qualified as Mtl.LT
import Control.Monad.Trans.RWS.Strict qualified as Mtl.ST
import Control.Monad.Trans.Reader qualified as Mtl.T
import Control.Monad.Trans.Select qualified as Mtl.T
import Control.Monad.Trans.State.Lazy qualified as Mtl.LT
import Control.Monad.Trans.State.Strict qualified as Mtl.ST
import Control.Monad.Trans.Writer.CPS qualified as Mtl.CPST
import Control.Monad.Trans.Writer.Lazy qualified as Mtl.LT
import Control.Monad.Trans.Writer.Strict qualified as Mtl.ST
import Control.Monad.Writer.Class qualified as Mtl
import Data.Functor.Identity qualified as Mtl
#endif

#if defined(VERSION_primitive)
import Control.Monad.Primitive qualified as Primitive
#endif

#if defined(VERSION_resourcet)
import Control.Monad.Trans.Resource qualified as ResourceT
#endif

#if defined(VERSION_unliftio_core)
import Control.Monad.IO.Unlift qualified as UnliftIO
#endif

-- * 'ComposeT'
--
-- $composet
--
-- 'ComposeT' can be used in monad transformer stacks to derive instances.
--
-- This also allows the usage of these instances, while in the middle of the transformer stack.
-- This proves particularly useful, when writing a runner for a transformer stack.

-- | A newtype wrapper for two stacked monad transformers.
--
-- Access instances of the intermediate monad @(t2 m)@, whenever @t1@ implements 'MonadTrans' \/
-- 'MonadTransControl' \/ 'MonadTransControlIdentity'.
--
-- __Type level arguments:__
--
-- [@t1 :: ('Type' -> 'Type') -> 'Type' -> 'Type'@] outer monad transformer
-- [@t2 :: ('Type' -> 'Type') -> 'Type' -> 'Type'@] inner monad transformer
-- [@m :: 'Type' -> 'Type'@] monad
-- [@a :: 'Type'@] value
type ComposeT :: ((Type -> Type) -> Type -> Type) -- ^ @t1@
              -> ((Type -> Type) -> Type -> Type) -- ^ @t2@
              -> (Type -> Type) -- ^ @m@
              -> Type -- ^ @a@
              -> Type
newtype ComposeT t1 t2 m a = ComposeT { forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
ComposeT t1 t2 m a -> t1 (t2 m) a
deComposeT :: t1 (t2 m) a }
  deriving newtype (forall a. a -> ComposeT t1 t2 m a
forall a b.
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m a
forall a b.
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m b
forall a b.
ComposeT t1 t2 m (a -> b)
-> ComposeT t1 t2 m a -> ComposeT t1 t2 m b
forall a b c.
(a -> b -> c)
-> ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
forall {t1 :: (* -> *) -> * -> *} {t2 :: (* -> *) -> * -> *}
       {m :: * -> *}.
Applicative (t1 (t2 m)) =>
Functor (ComposeT t1 t2 m)
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
Applicative (t1 (t2 m)) =>
a -> ComposeT t1 t2 m a
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Applicative (t1 (t2 m)) =>
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m a
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Applicative (t1 (t2 m)) =>
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m b
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Applicative (t1 (t2 m)) =>
ComposeT t1 t2 m (a -> b)
-> ComposeT t1 t2 m a -> ComposeT t1 t2 m b
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b c.
Applicative (t1 (t2 m)) =>
(a -> b -> c)
-> ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m c
<* :: forall a b.
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m a
$c<* :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Applicative (t1 (t2 m)) =>
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m a
*> :: forall a b.
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m b
$c*> :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Applicative (t1 (t2 m)) =>
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m b
liftA2 :: forall a b c.
(a -> b -> c)
-> ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m c
$cliftA2 :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b c.
Applicative (t1 (t2 m)) =>
(a -> b -> c)
-> ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m c
<*> :: forall a b.
ComposeT t1 t2 m (a -> b)
-> ComposeT t1 t2 m a -> ComposeT t1 t2 m b
$c<*> :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Applicative (t1 (t2 m)) =>
ComposeT t1 t2 m (a -> b)
-> ComposeT t1 t2 m a -> ComposeT t1 t2 m b
pure :: forall a. a -> ComposeT t1 t2 m a
$cpure :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
Applicative (t1 (t2 m)) =>
a -> ComposeT t1 t2 m a
Applicative, forall a b. a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m a
forall a b. (a -> b) -> ComposeT t1 t2 m a -> ComposeT t1 t2 m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Functor (t1 (t2 m)) =>
a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m a
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Functor (t1 (t2 m)) =>
(a -> b) -> ComposeT t1 t2 m a -> ComposeT t1 t2 m b
<$ :: forall a b. a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m a
$c<$ :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Functor (t1 (t2 m)) =>
a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m a
fmap :: forall a b. (a -> b) -> ComposeT t1 t2 m a -> ComposeT t1 t2 m b
$cfmap :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Functor (t1 (t2 m)) =>
(a -> b) -> ComposeT t1 t2 m a -> ComposeT t1 t2 m b
Functor, forall a. a -> ComposeT t1 t2 m a
forall a b.
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m b
forall a b.
ComposeT t1 t2 m a
-> (a -> ComposeT t1 t2 m b) -> ComposeT t1 t2 m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
forall {t1 :: (* -> *) -> * -> *} {t2 :: (* -> *) -> * -> *}
       {m :: * -> *}.
Monad (t1 (t2 m)) =>
Applicative (ComposeT t1 t2 m)
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
Monad (t1 (t2 m)) =>
a -> ComposeT t1 t2 m a
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Monad (t1 (t2 m)) =>
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m b
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Monad (t1 (t2 m)) =>
ComposeT t1 t2 m a
-> (a -> ComposeT t1 t2 m b) -> ComposeT t1 t2 m b
return :: forall a. a -> ComposeT t1 t2 m a
$creturn :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
Monad (t1 (t2 m)) =>
a -> ComposeT t1 t2 m a
>> :: forall a b.
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m b
$c>> :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Monad (t1 (t2 m)) =>
ComposeT t1 t2 m a -> ComposeT t1 t2 m b -> ComposeT t1 t2 m b
>>= :: forall a b.
ComposeT t1 t2 m a
-> (a -> ComposeT t1 t2 m b) -> ComposeT t1 t2 m b
$c>>= :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a b.
Monad (t1 (t2 m)) =>
ComposeT t1 t2 m a
-> (a -> ComposeT t1 t2 m b) -> ComposeT t1 t2 m b
Monad)

instance (MonadTrans t1, MonadTrans t2) => MonadTrans (ComposeT t1 t2) where
  lift :: forall (m :: * -> *) a. Monad m => m a -> ComposeT t1 t2 m a
lift = forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
t1 (t2 m) a -> ComposeT t1 t2 m a
ComposeT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift

instance (MonadTransControl t1, MonadTransControl t2) => MonadTransControl (ComposeT t1 t2) where
  type StT (ComposeT t1 t2) a = StT t2 (StT t1 a)
  liftWith :: forall (m :: * -> *) a.
Monad m =>
(Run (ComposeT t1 t2) -> m a) -> ComposeT t1 t2 m a
liftWith Run (ComposeT t1 t2) -> m a
f = forall (m :: * -> *) (n' :: (* -> *) -> * -> *)
       (n :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, Monad (n' m), MonadTransControl n,
 MonadTransControl n') =>
(forall b. n (n' m) b -> t m b)
-> (forall (o :: * -> *) b. t o b -> n (n' o) b)
-> (RunDefault2 t n n' -> m a)
-> t m a
defaultLiftWith2 forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
t1 (t2 m) a -> ComposeT t1 t2 m a
ComposeT forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
ComposeT t1 t2 m a -> t1 (t2 m) a
deComposeT forall a b. (a -> b) -> a -> b
$ \ RunDefault2 (ComposeT t1 t2) t1 t2
x -> Run (ComposeT t1 t2) -> m a
f RunDefault2 (ComposeT t1 t2) t1 t2
x
  restoreT :: forall (m :: * -> *) a.
Monad m =>
m (StT (ComposeT t1 t2) a) -> ComposeT t1 t2 m a
restoreT = forall (m :: * -> *) (n' :: (* -> *) -> * -> *)
       (n :: (* -> *) -> * -> *) a (t :: (* -> *) -> * -> *).
(Monad m, Monad (n' m), MonadTransControl n,
 MonadTransControl n') =>
(n (n' m) a -> t m a) -> m (StT n' (StT n a)) -> t m a
defaultRestoreT2 forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
t1 (t2 m) a -> ComposeT t1 t2 m a
ComposeT

instance (MonadTransControlIdentity t1, MonadTransControlIdentity t2) => MonadTransControlIdentity (ComposeT t1 t2) where
  liftWithIdentity :: forall (m :: * -> *) a.
Monad m =>
((forall x. ComposeT t1 t2 m x -> m x) -> m a)
-> ComposeT t1 t2 m a
liftWithIdentity (forall x. ComposeT t1 t2 m x -> m x) -> m a
inner = forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
t1 (t2 m) a -> ComposeT t1 t2 m a
ComposeT forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControlIdentity t, Monad m) =>
((forall x. t m x -> m x) -> m a) -> t m a
liftWithIdentity forall a b. (a -> b) -> a -> b
$ \ forall x. t1 (t2 m) x -> t2 m x
runId1 ->
    forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControlIdentity t, Monad m) =>
((forall x. t m x -> m x) -> m a) -> t m a
liftWithIdentity forall a b. (a -> b) -> a -> b
$ \ forall x. t2 m x -> m x
runId2 -> (forall x. ComposeT t1 t2 m x -> m x) -> m a
inner forall a b. (a -> b) -> a -> b
$ forall x. t2 m x -> m x
runId2 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. t1 (t2 m) x -> t2 m x
runId1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
ComposeT t1 t2 m a -> t1 (t2 m) a
deComposeT

-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
  instance
    ( MonadIO m
    , MonadTrans (ComposeT t1 t2)
    ) => MonadIO (ComposeT t1 t2 m)

-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
  instance
    ( MonadBase b m
    , MonadTrans (ComposeT t1 t2)
    ) => MonadBase b (ComposeT t1 t2 m)

-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
  instance
    ( MonadBaseControl b m
    , MonadTransControl (ComposeT t1 t2)
    ) => MonadBaseControl b (ComposeT t1 t2 m)

-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
  instance
    ( MonadBaseControlIdentity b m
    , MonadTransControlIdentity (ComposeT t1 t2)
    ) => MonadBaseControlIdentity b (ComposeT t1 t2 m)

#if defined(VERSION_exceptions)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
  instance {-# OVERLAPPABLE #-}
    ( Exceptions.MonadThrow (t2 m)
    , MonadTrans t1
    ) => Exceptions.MonadThrow (ComposeT t1 t2 m)

-- | Set by 'Exceptions.T.CatchT'.
deriving via Exceptions.T.CatchT (t2 (m :: Type -> Type))
  instance
    ( Monad (t2 m)
    ) => Exceptions.MonadThrow (ComposeT Exceptions.T.CatchT t2 m)

-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
  instance {-# OVERLAPPABLE #-}
    ( Exceptions.MonadCatch (t2 m)
    , MonadTransControl t1
    ) => Exceptions.MonadCatch (ComposeT t1 t2 m)

-- | Set by 'Exceptions.T.CatchT'.
deriving via Exceptions.T.CatchT (t2 (m :: Type -> Type))
  instance
    ( Monad (t2 m)
    ) => Exceptions.MonadCatch (ComposeT Exceptions.T.CatchT t2 m)
#endif

#if defined(VERSION_mtl)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
  instance {-# OVERLAPPABLE #-}
    ( Mtl.MonadAccum w (t2 m)
    , MonadTrans t1
    ) => Mtl.MonadAccum w (ComposeT t1 t2 m)

-- | Set by 'Mtl.T.AccumT'.
deriving via Mtl.T.AccumT w (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadAccum w (ComposeT (Mtl.T.AccumT w) t2 m)

-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
  instance {-# OVERLAPPABLE #-}
    ( Mtl.MonadCont (t2 m)
    , MonadTransControl t1
    ) => Mtl.MonadCont (ComposeT t1 t2 m)

-- | Set by 'Mtl.T.ContT'.
deriving via Mtl.T.ContT r (t2 (m :: Type -> Type))
  instance Mtl.MonadCont (ComposeT (Mtl.T.ContT r) t2 m)

-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
  instance {-# OVERLAPPABLE #-}
    ( Mtl.MonadError e (t2 m)
    , MonadTransControl t1
    ) => Mtl.MonadError e (ComposeT t1 t2 m)

-- | Set by 'Mtl.T.ExceptT'.
deriving via Mtl.T.ExceptT e (t2 (m :: Type -> Type))
  instance
    ( Monad (t2 m)
    ) => Mtl.MonadError e (ComposeT (Mtl.T.ExceptT e) t2 m)

-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
  instance {-# OVERLAPPABLE #-}
    ( Mtl.MonadReader r (t2 m)
    , MonadTransControl t1
    ) => Mtl.MonadReader r (ComposeT t1 t2 m)

-- | Set by 'Mtl.T.ReaderT'.
deriving via Mtl.T.ReaderT r (t2 (m :: Type -> Type))
  instance
    ( Monad (t2 m)
    ) => Mtl.MonadReader r (ComposeT (Mtl.T.ReaderT r) t2 m)

-- | Set by 'Mtl.CPST.RWST'.
deriving via Mtl.CPST.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadReader r (ComposeT (Mtl.CPST.RWST r w s) t2 m)

-- | Set by 'Mtl.LT.RWST'.
deriving via Mtl.LT.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadReader r (ComposeT (Mtl.LT.RWST r w s) t2 m)

-- | Set by 'Mtl.ST.RWST'.
deriving via Mtl.ST.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadReader r (ComposeT (Mtl.ST.RWST r w s) t2 m)

-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
  instance {-# OVERLAPPABLE #-}
    ( Mtl.MonadRWS r w s (t2 m)
    , MonadTransControl t1
    ) => Mtl.MonadRWS r w s (ComposeT t1 t2 m)

-- | Set by 'Mtl.CPST.RWST'.
deriving via Mtl.CPST.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadRWS r w s (ComposeT (Mtl.CPST.RWST r w s) t2 m)

-- | Set by 'Mtl.LT.RWST'.
deriving via Mtl.LT.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadRWS r w s (ComposeT (Mtl.LT.RWST r w s) t2 m)

-- | Set by 'Mtl.ST.RWST'.
deriving via Mtl.ST.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadRWS r w s (ComposeT (Mtl.ST.RWST r w s) t2 m)

-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
  instance {-# OVERLAPPABLE #-}
    ( Mtl.MonadSelect r (t2 m)
    , MonadTrans t1
    ) => Mtl.MonadSelect r (ComposeT t1 t2 m)

-- | Set by 'Mtl.T.SelectT'.
deriving via Mtl.T.SelectT r (t2 (m :: Type -> Type))
  instance
    ( MonadBaseControlIdentity Mtl.Identity (t2 m)
    ) => Mtl.MonadSelect r (ComposeT (Mtl.T.SelectT r) t2 m)

-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
  instance {-# OVERLAPPABLE #-}
    ( Mtl.MonadState s (t2 m)
    , MonadTrans t1
    ) => Mtl.MonadState s (ComposeT t1 t2 m)

-- | Set by 'Mtl.LT.StateT'.
deriving via Mtl.LT.StateT s (t2 (m :: Type -> Type))
  instance
    ( Monad (t2 m)
    ) => Mtl.MonadState s (ComposeT (Mtl.LT.StateT s) t2 m)

-- | Set by 'Mtl.ST.StateT'.
deriving via Mtl.ST.StateT s (t2 (m :: Type -> Type))
  instance
    ( Monad (t2 m)
    ) => Mtl.MonadState s (ComposeT (Mtl.ST.StateT s) t2 m)

-- | Set by 'Mtl.CPST.RWST'.
deriving via Mtl.CPST.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadState s (ComposeT (Mtl.CPST.RWST r w s) t2 m)

-- | Set by 'Mtl.LT.RWST'.
deriving via Mtl.LT.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadState s (ComposeT (Mtl.LT.RWST r w s) t2 m)

-- | Set by 'Mtl.ST.RWST'.
deriving via Mtl.ST.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadState s (ComposeT (Mtl.ST.RWST r w s) t2 m)

-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
  instance {-# OVERLAPPABLE #-}
    ( Mtl.MonadWriter w (t2 m)
    , MonadTransControl t1
    ) => Mtl.MonadWriter w (ComposeT t1 t2 m)

-- | Set by 'Mtl.CPST.WriterT'.
deriving via Mtl.CPST.WriterT w (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadWriter w (ComposeT (Mtl.CPST.WriterT w) t2 m)

-- | Set by 'Mtl.LT.WriterT'.
deriving via Mtl.LT.WriterT w (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadWriter w (ComposeT (Mtl.LT.WriterT w) t2 m)

-- | Set by 'Mtl.ST.WriterT'.
deriving via Mtl.ST.WriterT w (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadWriter w (ComposeT (Mtl.ST.WriterT w) t2 m)

-- | Set by 'Mtl.CPST.RWST'.
deriving via Mtl.CPST.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadWriter w (ComposeT (Mtl.CPST.RWST r w s) t2 m)

-- | Set by 'Mtl.LT.RWST'.
deriving via Mtl.LT.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadWriter w (ComposeT (Mtl.LT.RWST r w s) t2 m)

-- | Set by 'Mtl.ST.RWST'.
deriving via Mtl.ST.RWST r w s (t2 (m :: Type -> Type))
  instance
    ( Monoid w
    , Monad (t2 m)
    ) => Mtl.MonadWriter w (ComposeT (Mtl.ST.RWST r w s) t2 m)
#endif

#if defined(VERSION_primitive)
-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
  instance
    ( Primitive.PrimMonad m
    , MonadTrans (ComposeT t1 t2)
    ) =>
    Primitive.PrimMonad (ComposeT t1 t2 m)
#endif

#if defined(VERSION_resourcet)
-- TODO: `MonadIO m` and `MonadTrans t2` should not be required for this instance
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
  instance {-# OVERLAPPABLE #-}
    ( ResourceT.MonadResource (t2 m)
    , MonadTrans t1
    , MonadIO m
    , MonadTrans t2
    ) => ResourceT.MonadResource (ComposeT t1 t2 m)

-- TODO: `MonadIO m` and `MonadTrans t2` should not be required for this instance
-- | Set by 'ResourceT.ResourceT'.
deriving via ResourceT.ResourceT (t2 (m :: Type -> Type))
  instance
    ( MonadIO (t2 m)
    , MonadIO m
    , MonadTrans t2
    ) => ResourceT.MonadResource (ComposeT ResourceT.ResourceT t2 m)
#endif

#if defined(VERSION_unliftio_core)
-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
  instance
    ( UnliftIO.MonadUnliftIO m
    , MonadTransControlIdentity (ComposeT t1 t2)
    ) =>
    UnliftIO.MonadUnliftIO (ComposeT t1 t2 m)
#endif


-- ** Run 'ComposeT'
--
-- $runComposet
--
-- You have to run the composed monad transformers to get back into the base monad at some point.

-- | Run two stacked monad transformers.
--
-- This function takes the two individual monad transformer runners as arguments.
runComposeT :: (forall a. t1 (t2 m) a -> t2 m (StT t1 a)) -- ^ run @t1@
            -> (forall a. t2 m a -> m (StT t2 a)) -- ^ run @t2@
            -> (forall a. ComposeT t1 t2 m a -> m (StT t2 (StT t1 a)))
runComposeT :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *).
(forall a. t1 (t2 m) a -> t2 m (StT t1 a))
-> (forall a. t2 m a -> m (StT t2 a))
-> forall a. ComposeT t1 t2 m a -> m (StT t2 (StT t1 a))
runComposeT forall a. t1 (t2 m) a -> t2 m (StT t1 a)
runT1 forall a. t2 m a -> m (StT t2 a)
runT2 = forall a. t2 m a -> m (StT t2 a)
runT2 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. t1 (t2 m) a -> t2 m (StT t1 a)
runT1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
ComposeT t1 t2 m a -> t1 (t2 m) a
deComposeT

-- | Equivalent to 'runComposeT', but discards the monadic state 'StT'.
-- This is a simple approach when your monad transformer stack doesn't carry monadic state.
--
-- @
-- 'StT' ('ComposeT' t1 t2) a ~ a
-- @
--
-- This can be used to improve error messages when modifying a monad transformer stack.
runComposeT' :: (t1 (t2 m) a -> t2 m a) -- ^ run @t1@
             -> (t2 m a -> m a) -- ^ run @t2@
             -> (ComposeT t1 t2 m a -> m a)
runComposeT' :: forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(t1 (t2 m) a -> t2 m a)
-> (t2 m a -> m a) -> ComposeT t1 t2 m a -> m a
runComposeT' t1 (t2 m) a -> t2 m a
runT1 t2 m a -> m a
runT2 = t2 m a -> m a
runT2 forall b c a. (b -> c) -> (a -> b) -> a -> c
. t1 (t2 m) a -> t2 m a
runT1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
       (m :: * -> *) a.
ComposeT t1 t2 m a -> t1 (t2 m) a
deComposeT

-- * Examples

-- ** Example 1: Create a new type class
--
-- $example1
--
-- When creating a new type class that supports 'ComposeT', you want to add recursive instances for
-- 'ComposeT'.
--
-- @
-- class 'Monad' m => MonadCustom m where
--   simpleMethod :: a -> m a
--   complicatedMethod :: (a -> m a) -> m a
-- @
--
-- You can easily derive those instances, after implementing an instance for 'Elevator'.
-- This is explained in "Control.Monad.Trans.Elevator".
--
-- Then it's possible to derive the recursive instance.
-- This is an /OVERLAPPABLE/ instance, because we want to be able to add new "base-case" instances
-- through transformers in a stack.
--
-- @
-- deriving via 'Elevator' t1 (t2 (m :: * -> *))
--   instance {-\# OVERLAPPABLE \#-}
--     ( 'Monad' (t1 (t2 m))
--     , 'MonadTransControl' t1
--     , MonadCustom (t2 m)
--     ) => MonadCustom ('ComposeT' t1 t2 m)
-- @

-- ** Example 2: Add an instance
--
-- $example2
--
-- Add a type class instance for a new monad transformer, when there already is a recursive instance
-- for 'ComposeT'.
--
-- @
-- newtype CustomT m a = CustomT { unCustomT :: t'Control.Monad.Trans.Identity.IdentityT' m a }
--   deriving newtype ('Functor', 'Applicative', 'Monad')
--   deriving newtype ('MonadTrans', 'MonadTransControl', 'MonadTransControlIdentity')
-- @
--
-- First we need the regular instance.
-- The method implementations are 'undefined' here, because they would only distract from
-- 'ComposeT'.
--
-- @
-- instance 'Monad' m => MonadCustom (CustomT m) where
--   simpleMethod = 'undefined'
--   complicatedMethod = 'undefined'
-- @
--
-- To add a "base-case" instance, that takes priority over the recursive instance,
-- /FlexibleInstances/ are required.
--
-- @
-- deriving via CustomT (t2 (m :: * -> *))
--   instance 'Monad' (t2 m) => MonadCustom ('ComposeT' CustomT t2 m)
-- @

-- ** Example 3: Build a transformer stack
--
-- $example3
--
-- Create a monad transformer stack and wrap it using a newtype.
--
-- @
-- type AppStackT = t'Control.Monad.Trans.Compose.Transparent.TransparentT' t'Control.Monad.Trans.Compose.Infix..|>' 'Control.Monad.Trans.Reader.ReaderT' 'Bool' t'Control.Monad.Trans.Compose.Infix..|>' CustomT t'Control.Monad.Trans.Compose.Infix..|>' 'Control.Monad.Trans.Reader.ReaderT' 'Char' t'Control.Monad.Trans.Compose.Infix..|>' 'Control.Monad.Trans.State.Lazy.StateT' 'Int'
-- newtype AppT m a = AppT { unAppT :: AppStackT m a }
--   deriving newtype ('Functor', 'Applicative', 'Monad')
-- @
--
-- Using t'Control.Monad.Trans.Compose.Infix..|>' we can write @AppStackT@ in the order of initialization.
-- We are adding t'Control.Monad.Trans.Compose.Transparent.TransparentT' to the bottom of the stack,
-- so that all the other transformer instances actually end up in the stack.
-- Now we can simply derive just the instances, that we want.
--
-- @
--   deriving newtype ('MonadTrans', 'MonadTransControl')
--   deriving newtype ('Control.Monad.State.Class.MonadState' 'Int')
--   deriving newtype MonadCustom
-- @
--
-- We can even access instances, that would have been shadowed in a regular transformer stack.
--
-- @
--   deriving newtype ('Control.Monad.Reader.Class.MonadReader' 'Bool')
-- @

-- ** Example 4: Run a transformer stack
--
-- $example4
--
-- This is the part, that actually contains your application logic.
-- Because of the setup with 'ComposeT', we won't have to worry about 'lift'ing during the
-- initialization.
-- With 'Control.Monad.Trans.Compose.Infix...>' we can use the order of initialization again.
--
-- @
-- runAppT :: AppT m a -> m ('StT' AppT a)
-- runAppT appTma =
--   'Control.Monad.Trans.Compose.Transparent.runTransparentT'
--     'Control.Monad.Trans.Compose.Infix../>' (\\ tma -> 'T.runReaderT' tma 'True')
--     'Control.Monad.Trans.Compose.Infix../>' runCustomT
--     'Control.Monad.Trans.Compose.Infix../>' runReaderT'
--     'Control.Monad.Trans.Compose.Infix../>' runStateT'
--     $ unAppT appTma
--  where
--   runReaderT' :: 'Control.Monad.Reader.Class.MonadReader' 'Bool' m => 'Control.Monad.Trans.Reader.ReaderT' 'Char' m a -> m a
--   runReaderT' tma = do
--     bool <- 'Control.Monad.Reader.Class.ask'
--     let char = if bool then \'Y\' else \'N\'
--     'Control.Monad.Trans.Reader.runReaderT' tma char
--
--   runStateT' :: 'Control.Monad.Reader.Class.MonadReader' 'Char' m => 'Control.Monad.Trans.State.Lazy.StateT' 'Int' m a -> m (a, 'Int')
--   runStateT' tma = do
--     char <- 'Control.Monad.Reader.Class.ask'
--     let num = 'fromEnum' char
--     'Control.Monad.Trans.State.Lazy.runStateT' tma num
-- @