{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UndecidableInstances #-}
-- |
-- Module      : Control.Prim.Monad.Internal
-- Copyright   : (c) Alexey Kuleshevich 2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <alexey@kuleshevi.ch>
-- Stability   : experimental
-- Portability : non-portable
--
module Control.Prim.Monad.Internal
  ( RW
  , RealWorld
  , MonadIO
  , MonadPrim(..)
  , MonadPrimBase(..)
  , MonadUnliftIO
  , MonadUnliftPrim(..)
  , ST
  , unIO
  , unIO_
  , unST
  , unST_
  , runST
  , prim_
  , primBase_
  , withRunInIO
  , withRunInPrimBase
  , runInPrimBase
  , liftIO
  , liftST
  , liftPrimBase
  , primBaseToIO
  , primBaseToST
  ) where

import GHC.Exts
import GHC.IO hiding (liftIO)
import GHC.ST hiding (liftST)
import Control.Exception (SomeException)
import Control.Prim.Monad.Throw
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Cont (ContT)
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Identity (IdentityT(..))
import Control.Monad.Trans.Maybe (MaybeT)
import Control.Monad.Trans.Reader (ReaderT(..))
import Control.Monad.Trans.RWS.Lazy as Lazy (RWST)
import Control.Monad.Trans.RWS.Strict as Strict (RWST)
import Control.Monad.Trans.State.Lazy as Lazy (StateT)
import Control.Monad.Trans.State.Strict as Strict (StateT)
import Control.Monad.Trans.Writer.Lazy as Lazy (WriterT)
import Control.Monad.Trans.Writer.Strict as Strict (WriterT)

#if MIN_VERSION_transformers(0, 5, 3)
import Control.Monad.Trans.Accum (AccumT)
import Control.Monad.Trans.Select (SelectT)
#if MIN_VERSION_transformers(0, 5, 6)
import Control.Monad.Trans.RWS.CPS as CPS (RWST)
import Control.Monad.Trans.Writer.CPS as CPS (WriterT)
#endif
#endif

-- | A shorter synonym for the magical `RealWorld`
type RW = RealWorld

type MonadIO m = MonadPrim RW m

type MonadUnliftIO m = MonadUnliftPrim RW m

class MonadThrow m => MonadPrim s m | m -> s where
  -- | Construct a primitive action
  prim :: (State# s -> (# State# s, a #)) -> m a


class MonadPrim s m => MonadUnliftPrim s m where

  withRunInST :: ((forall a. m a -> ST s a) -> ST s b) -> m b

  runInPrimBase1 ::
       (a -> m b)
    -> ( (a -> State# s -> (# State# s, b #)) -> State# s -> (# State# s, c #) )
    -> m c
  runInPrimBase1 a -> m b
m (a -> State# s -> (# State# s, b #))
-> State# s -> (# State# s, c #)
f# = (Any -> m ())
-> (a -> m b)
-> ((Any -> State# s -> (# State# s, () #))
    -> (a -> State# s -> (# State# s, b #))
    -> State# s
    -> (# State# s, c #))
-> m c
forall s (m :: * -> *) a b c d e.
MonadUnliftPrim s m =>
(a -> m b)
-> (c -> m d)
-> ((a -> State# s -> (# State# s, b #))
    -> (c -> State# s -> (# State# s, d #))
    -> State# s
    -> (# State# s, e #))
-> m e
runInPrimBase2 (\Any
_ -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) a -> m b
m (\Any -> State# s -> (# State# s, () #)
_ -> (a -> State# s -> (# State# s, b #))
-> State# s -> (# State# s, c #)
f#)
  {-# INLINE runInPrimBase1 #-}

  runInPrimBase2 ::
       (a -> m b)
    -> (c -> m d)
    -> ( (a -> State# s -> (# State# s, b #))
      -> (c -> State# s -> (# State# s, d #))
      -> State# s -> (# State# s, e #)   )
    -> m e
  runInPrimBase2 a -> m b
m1 c -> m d
m2 (a -> State# s -> (# State# s, b #))
-> (c -> State# s -> (# State# s, d #))
-> State# s
-> (# State# s, e #)
f# =
    ((forall a. m a -> ST s a) -> ST s e) -> m e
forall s (m :: * -> *) b.
MonadUnliftPrim s m =>
((forall a. m a -> ST s a) -> ST s b) -> m b
withRunInST (((forall a. m a -> ST s a) -> ST s e) -> m e)
-> ((forall a. m a -> ST s a) -> ST s e) -> m e
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> ST s a
run ->
      (State# s -> (# State# s, e #)) -> ST s e
forall s a. STRep s a -> ST s a
ST ((a -> State# s -> (# State# s, b #))
-> (c -> State# s -> (# State# s, d #))
-> State# s
-> (# State# s, e #)
f# (\a
a -> ST s b -> State# s -> (# State# s, b #)
forall s a. ST s a -> State# s -> (# State# s, a #)
unST (m b -> ST s b
forall a. m a -> ST s a
run (a -> m b
m1 a
a))) (\c
c -> ST s d -> State# s -> (# State# s, d #)
forall s a. ST s a -> State# s -> (# State# s, a #)
unST (m d -> ST s d
forall a. m a -> ST s a
run (c -> m d
m2 c
c))))
  {-# INLINE runInPrimBase2 #-}


class MonadUnliftPrim s m => MonadPrimBase s m where
  -- | Unwrap a primitive action
  primBase :: m a -> State# s -> (# State# s, a #)


instance MonadPrimBase RealWorld IO where
  primBase :: IO a -> State# RealWorld -> (# State# RealWorld, a #)
primBase (IO State# RealWorld -> (# State# RealWorld, a #)
m) = State# RealWorld -> (# State# RealWorld, a #)
m
  {-# INLINE primBase #-}

instance MonadPrimBase s (ST s) where
  primBase :: ST s a -> State# s -> (# State# s, a #)
primBase (ST State# s -> (# State# s, a #)
m) = State# s -> (# State# s, a #)
m
  {-# INLINE primBase #-}

instance MonadPrimBase s m => MonadPrimBase s (IdentityT m) where
  primBase :: IdentityT m a -> State# s -> (# State# s, a #)
primBase (IdentityT m a
m) = m a -> State# s -> (# State# s, a #)
forall s (m :: * -> *) a.
MonadPrimBase s m =>
m a -> State# s -> (# State# s, a #)
primBase m a
m
  {-# INLINE primBase #-}

runInPrimBase ::
     forall s m a b. MonadUnliftPrim s m
  => m a
  -> ((State# s -> (# State# s, a #)) -> State# s -> (# State# s, b #))
  -> m b
runInPrimBase :: m a
-> ((State# s -> (# State# s, a #))
    -> State# s -> (# State# s, b #))
-> m b
runInPrimBase m a
f (State# s -> (# State# s, a #)) -> State# s -> (# State# s, b #)
g# = (() -> m a)
-> ((() -> State# s -> (# State# s, a #))
    -> State# s -> (# State# s, b #))
-> m b
forall s (m :: * -> *) a b c.
MonadUnliftPrim s m =>
(a -> m b)
-> ((a -> State# s -> (# State# s, b #))
    -> State# s -> (# State# s, c #))
-> m c
runInPrimBase1 (m a -> () -> m a
forall a b. a -> b -> a
const m a
f) (\() -> State# s -> (# State# s, a #)
f# -> (State# s -> (# State# s, a #)) -> State# s -> (# State# s, b #)
g# (() -> State# s -> (# State# s, a #)
f# ()))
{-# INLINE runInPrimBase #-}



withRunInIO ::
     forall m b. MonadUnliftPrim RW m
  => ((forall a. m a -> IO a) -> IO b)
  -> m b
withRunInIO :: ((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (forall a. m a -> IO a) -> IO b
f = ((forall a. m a -> ST RealWorld a) -> ST RealWorld b) -> m b
forall s (m :: * -> *) b.
MonadUnliftPrim s m =>
((forall a. m a -> ST s a) -> ST s b) -> m b
withRunInST (((forall a. m a -> ST RealWorld a) -> ST RealWorld b) -> m b)
-> ((forall a. m a -> ST RealWorld a) -> ST RealWorld b) -> m b
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> ST RealWorld a
run -> IO b -> ST RealWorld b
coerce ((forall a. m a -> IO a) -> IO b
f (\m a
m -> ST RealWorld a -> IO a
coerce (m a -> ST RealWorld a
forall a. m a -> ST RealWorld a
run m a
m)))
{-# INLINE withRunInIO #-}


withRunInPrimBase ::
     (MonadUnliftPrim s m, MonadPrimBase s n)
  => ((forall a. m a -> n a) -> n b)
  -> m b
withRunInPrimBase :: ((forall a. m a -> n a) -> n b) -> m b
withRunInPrimBase (forall a. m a -> n a) -> n b
inner =
  ((forall a. m a -> ST s a) -> ST s b) -> m b
forall s (m :: * -> *) b.
MonadUnliftPrim s m =>
((forall a. m a -> ST s a) -> ST s b) -> m b
withRunInST (((forall a. m a -> ST s a) -> ST s b) -> m b)
-> ((forall a. m a -> ST s a) -> ST s b) -> m b
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> ST s a
run -> n b -> ST s b
forall s (n :: * -> *) (m :: * -> *) a.
(MonadPrimBase s n, MonadPrim s m) =>
n a -> m a
liftPrimBase ((forall a. m a -> n a) -> n b
inner (ST s a -> n a
forall s (m :: * -> *) a. MonadPrim s m => ST s a -> m a
liftST (ST s a -> n a) -> (m a -> ST s a) -> m a -> n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> ST s a
forall a. m a -> ST s a
run))
{-# INLINE withRunInPrimBase #-}



instance MonadUnliftPrim RealWorld IO where
  withRunInST :: ((forall a. IO a -> ST RealWorld a) -> ST RealWorld b) -> IO b
withRunInST (forall a. IO a -> ST RealWorld a) -> ST RealWorld b
inner = ST RealWorld b -> IO b
coerce ((forall a. IO a -> ST RealWorld a) -> ST RealWorld b
inner forall a. IO a -> ST RealWorld a
forall s (n :: * -> *) (m :: * -> *) a.
(MonadPrimBase s n, MonadPrim s m) =>
n a -> m a
liftPrimBase)
  {-# INLINE withRunInST #-}
  runInPrimBase1 :: (a -> IO b)
-> ((a -> State# RealWorld -> (# State# RealWorld, b #))
    -> State# RealWorld -> (# State# RealWorld, c #))
-> IO c
runInPrimBase1 a -> IO b
io (a -> State# RealWorld -> (# State# RealWorld, b #))
-> State# RealWorld -> (# State# RealWorld, c #)
f# = (State# RealWorld -> (# State# RealWorld, c #)) -> IO c
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((a -> State# RealWorld -> (# State# RealWorld, b #))
-> State# RealWorld -> (# State# RealWorld, c #)
f# (\a
e -> IO b -> State# RealWorld -> (# State# RealWorld, b #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO (a -> IO b
io a
e)))
  {-# INLINE runInPrimBase1 #-}
  runInPrimBase2 :: (a -> IO b)
-> (c -> IO d)
-> ((a -> State# RealWorld -> (# State# RealWorld, b #))
    -> (c -> State# RealWorld -> (# State# RealWorld, d #))
    -> State# RealWorld
    -> (# State# RealWorld, e #))
-> IO e
runInPrimBase2 a -> IO b
io1 c -> IO d
io2 (a -> State# RealWorld -> (# State# RealWorld, b #))
-> (c -> State# RealWorld -> (# State# RealWorld, d #))
-> State# RealWorld
-> (# State# RealWorld, e #)
f# = (State# RealWorld -> (# State# RealWorld, e #)) -> IO e
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((a -> State# RealWorld -> (# State# RealWorld, b #))
-> (c -> State# RealWorld -> (# State# RealWorld, d #))
-> State# RealWorld
-> (# State# RealWorld, e #)
f# (\a
e -> IO b -> State# RealWorld -> (# State# RealWorld, b #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO (a -> IO b
io1 a
e)) (\c
e -> IO d -> State# RealWorld -> (# State# RealWorld, d #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO (c -> IO d
io2 c
e)))
  {-# INLINE runInPrimBase2 #-}

instance MonadUnliftPrim s (ST s) where
  withRunInST :: ((forall a. ST s a -> ST s a) -> ST s b) -> ST s b
withRunInST (forall a. ST s a -> ST s a) -> ST s b
inner = (forall a. ST s a -> ST s a) -> ST s b
inner forall a. ST s a -> ST s a
forall s (n :: * -> *) (m :: * -> *) a.
(MonadPrimBase s n, MonadPrim s m) =>
n a -> m a
liftPrimBase
  {-# INLINE withRunInST #-}
  runInPrimBase1 :: (a -> ST s b)
-> ((a -> State# s -> (# State# s, b #))
    -> State# s -> (# State# s, c #))
-> ST s c
runInPrimBase1 a -> ST s b
st (a -> State# s -> (# State# s, b #))
-> State# s -> (# State# s, c #)
f# = (State# s -> (# State# s, c #)) -> ST s c
forall s a. STRep s a -> ST s a
ST ((a -> State# s -> (# State# s, b #))
-> State# s -> (# State# s, c #)
f# (\a
e -> ST s b -> State# s -> (# State# s, b #)
forall s a. ST s a -> State# s -> (# State# s, a #)
unST (a -> ST s b
st a
e)))
  {-# INLINE runInPrimBase1 #-}
  runInPrimBase2 :: (a -> ST s b)
-> (c -> ST s d)
-> ((a -> State# s -> (# State# s, b #))
    -> (c -> State# s -> (# State# s, d #))
    -> State# s
    -> (# State# s, e #))
-> ST s e
runInPrimBase2 a -> ST s b
st1 c -> ST s d
st2 (a -> State# s -> (# State# s, b #))
-> (c -> State# s -> (# State# s, d #))
-> State# s
-> (# State# s, e #)
f# = (State# s -> (# State# s, e #)) -> ST s e
forall s a. STRep s a -> ST s a
ST ((a -> State# s -> (# State# s, b #))
-> (c -> State# s -> (# State# s, d #))
-> State# s
-> (# State# s, e #)
f# (\a
e -> ST s b -> State# s -> (# State# s, b #)
forall s a. ST s a -> State# s -> (# State# s, a #)
unST (a -> ST s b
st1 a
e)) (\c
e -> ST s d -> State# s -> (# State# s, d #)
forall s a. ST s a -> State# s -> (# State# s, a #)
unST (c -> ST s d
st2 c
e)))
  {-# INLINE runInPrimBase2 #-}

instance MonadUnliftPrim s m => MonadUnliftPrim s (IdentityT m) where
  withRunInST :: ((forall a. IdentityT m a -> ST s a) -> ST s b) -> IdentityT m b
withRunInST (forall a. IdentityT m a -> ST s a) -> ST s b
inner = 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
$ ((forall a. m a -> ST s a) -> ST s b) -> m b
forall s (m :: * -> *) b.
MonadUnliftPrim s m =>
((forall a. m a -> ST s a) -> ST s b) -> m b
withRunInST (((forall a. m a -> ST s a) -> ST s b) -> m b)
-> ((forall a. m a -> ST s a) -> ST s b) -> m b
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> ST s a
run -> (forall a. IdentityT m a -> ST s a) -> ST s b
inner (m a -> ST s a
forall a. m a -> ST s a
run (m a -> ST s a)
-> (IdentityT m a -> m a) -> IdentityT m a -> ST s a
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 withRunInST #-}
  runInPrimBase1 :: (a -> IdentityT m b)
-> ((a -> State# s -> (# State# s, b #))
    -> State# s -> (# State# s, c #))
-> IdentityT m c
runInPrimBase1 a -> IdentityT m b
im (a -> State# s -> (# State# s, b #))
-> State# s -> (# State# s, c #)
f# = m c -> IdentityT m c
forall k (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (m c -> IdentityT m c) -> m c -> IdentityT m c
forall a b. (a -> b) -> a -> b
$ (a -> m b)
-> ((a -> State# s -> (# State# s, b #))
    -> State# s -> (# State# s, c #))
-> m c
forall s (m :: * -> *) a b c.
MonadUnliftPrim s m =>
(a -> m b)
-> ((a -> State# s -> (# State# s, b #))
    -> State# s -> (# State# s, c #))
-> m c
runInPrimBase1 (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
im) (a -> State# s -> (# State# s, b #))
-> State# s -> (# State# s, c #)
f#
  {-# INLINE runInPrimBase1 #-}
  runInPrimBase2 :: (a -> IdentityT m b)
-> (c -> IdentityT m d)
-> ((a -> State# s -> (# State# s, b #))
    -> (c -> State# s -> (# State# s, d #))
    -> State# s
    -> (# State# s, e #))
-> IdentityT m e
runInPrimBase2 a -> IdentityT m b
im1 c -> IdentityT m d
im2 (a -> State# s -> (# State# s, b #))
-> (c -> State# s -> (# State# s, d #))
-> State# s
-> (# State# s, e #)
f# =
    m e -> IdentityT m e
forall k (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (m e -> IdentityT m e) -> m e -> IdentityT m e
forall a b. (a -> b) -> a -> b
$ (a -> m b)
-> (c -> m d)
-> ((a -> State# s -> (# State# s, b #))
    -> (c -> State# s -> (# State# s, d #))
    -> State# s
    -> (# State# s, e #))
-> m e
forall s (m :: * -> *) a b c d e.
MonadUnliftPrim s m =>
(a -> m b)
-> (c -> m d)
-> ((a -> State# s -> (# State# s, b #))
    -> (c -> State# s -> (# State# s, d #))
    -> State# s
    -> (# State# s, e #))
-> m e
runInPrimBase2 (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
im1) (IdentityT m d -> m d
forall k (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT (IdentityT m d -> m d) -> (c -> IdentityT m d) -> c -> m d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> IdentityT m d
im2) (a -> State# s -> (# State# s, b #))
-> (c -> State# s -> (# State# s, d #))
-> State# s
-> (# State# s, e #)
f#
  {-# INLINE runInPrimBase2 #-}

instance MonadUnliftPrim s m => MonadUnliftPrim s (ReaderT r m) where
  withRunInST :: ((forall a. ReaderT r m a -> ST s a) -> ST s b) -> ReaderT r m b
withRunInST (forall a. ReaderT r m a -> ST s a) -> ST s b
inner = (r -> m b) -> ReaderT r m b
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((r -> m b) -> ReaderT r m b) -> (r -> m b) -> ReaderT r m b
forall a b. (a -> b) -> a -> b
$ \r
r -> ((forall a. m a -> ST s a) -> ST s b) -> m b
forall s (m :: * -> *) b.
MonadUnliftPrim s m =>
((forall a. m a -> ST s a) -> ST s b) -> m b
withRunInST (((forall a. m a -> ST s a) -> ST s b) -> m b)
-> ((forall a. m a -> ST s a) -> ST s b) -> m b
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> ST s a
run -> (forall a. ReaderT r m a -> ST s a) -> ST s b
inner (m a -> ST s a
forall a. m a -> ST s a
run (m a -> ST s a)
-> (ReaderT r m a -> m a) -> ReaderT r m a -> ST s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ReaderT r m a -> r -> m a) -> r -> ReaderT r m a -> m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT r m a -> r -> m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT r
r)
  {-# INLINE withRunInST #-}
  runInPrimBase1 :: (a -> ReaderT r m b)
-> ((a -> State# s -> (# State# s, b #))
    -> State# s -> (# State# s, c #))
-> ReaderT r m c
runInPrimBase1 a -> ReaderT r m b
rm (a -> State# s -> (# State# s, b #))
-> State# s -> (# State# s, c #)
f# =
    (r -> m c) -> ReaderT r m c
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((r -> m c) -> ReaderT r m c) -> (r -> m c) -> ReaderT r m c
forall a b. (a -> b) -> a -> b
$ \r
r -> (a -> m b)
-> ((a -> State# s -> (# State# s, b #))
    -> State# s -> (# State# s, c #))
-> m c
forall s (m :: * -> *) a b c.
MonadUnliftPrim s m =>
(a -> m b)
-> ((a -> State# s -> (# State# s, b #))
    -> State# s -> (# State# s, c #))
-> m c
runInPrimBase1 (\a
x -> ReaderT r m b -> r -> m b
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (a -> ReaderT r m b
rm a
x) r
r) (a -> State# s -> (# State# s, b #))
-> State# s -> (# State# s, c #)
f#
  {-# INLINE runInPrimBase1 #-}
  runInPrimBase2 :: (a -> ReaderT r m b)
-> (c -> ReaderT r m d)
-> ((a -> State# s -> (# State# s, b #))
    -> (c -> State# s -> (# State# s, d #))
    -> State# s
    -> (# State# s, e #))
-> ReaderT r m e
runInPrimBase2 a -> ReaderT r m b
rm1 c -> ReaderT r m d
rm2 (a -> State# s -> (# State# s, b #))
-> (c -> State# s -> (# State# s, d #))
-> State# s
-> (# State# s, e #)
f# =
    (r -> m e) -> ReaderT r m e
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((r -> m e) -> ReaderT r m e) -> (r -> m e) -> ReaderT r m e
forall a b. (a -> b) -> a -> b
$ \r
r -> (a -> m b)
-> (c -> m d)
-> ((a -> State# s -> (# State# s, b #))
    -> (c -> State# s -> (# State# s, d #))
    -> State# s
    -> (# State# s, e #))
-> m e
forall s (m :: * -> *) a b c d e.
MonadUnliftPrim s m =>
(a -> m b)
-> (c -> m d)
-> ((a -> State# s -> (# State# s, b #))
    -> (c -> State# s -> (# State# s, d #))
    -> State# s
    -> (# State# s, e #))
-> m e
runInPrimBase2 (\a
x -> ReaderT r m b -> r -> m b
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (a -> ReaderT r m b
rm1 a
x) r
r) (\c
x -> ReaderT r m d -> r -> m d
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (c -> ReaderT r m d
rm2 c
x) r
r) (a -> State# s -> (# State# s, b #))
-> (c -> State# s -> (# State# s, d #))
-> State# s
-> (# State# s, e #)
f#
  {-# INLINE runInPrimBase2 #-}


instance MonadPrim RealWorld IO where
  prim :: (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
prim = (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO
  {-# INLINE prim #-}

instance MonadPrim s (ST s) where
  prim :: (State# s -> (# State# s, a #)) -> ST s a
prim = (State# s -> (# State# s, a #)) -> ST s a
forall s a. STRep s a -> ST s a
ST
  {-# INLINE prim #-}


instance MonadPrim s m => MonadPrim s (ContT r m) where
  prim :: (State# s -> (# State# s, a #)) -> ContT r m a
prim = m a -> ContT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ContT r m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> ContT r m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}

instance (e ~ SomeException, MonadPrim s m) => MonadPrim s (ExceptT e m) where
  prim :: (State# s -> (# State# s, a #)) -> ExceptT e m a
prim = m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ExceptT e m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> ExceptT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}

instance MonadPrim s m => MonadPrim s (IdentityT m) where
  prim :: (State# s -> (# State# s, a #)) -> IdentityT m a
prim = m a -> IdentityT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> IdentityT m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> IdentityT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}

instance MonadPrim s m => MonadPrim s (MaybeT m) where
  prim :: (State# s -> (# State# s, a #)) -> MaybeT m a
prim = m a -> MaybeT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> MaybeT m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> MaybeT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}

instance MonadPrim s m => MonadPrim s (ReaderT r m) where
  prim :: (State# s -> (# State# s, a #)) -> ReaderT r m a
prim = m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ReaderT r m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> ReaderT r m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}


instance (Monoid w, MonadPrim s m) => MonadPrim s (Lazy.RWST r w st m) where
  prim :: (State# s -> (# State# s, a #)) -> RWST r w st m a
prim = m a -> RWST r w st m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> RWST r w st m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> RWST r w st m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}
instance (Monoid w, MonadPrim s m) => MonadPrim s (Strict.RWST r w st m) where
  prim :: (State# s -> (# State# s, a #)) -> RWST r w st m a
prim = m a -> RWST r w st m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> RWST r w st m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> RWST r w st m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}

instance MonadPrim s m => MonadPrim s (Lazy.StateT st m) where
  prim :: (State# s -> (# State# s, a #)) -> StateT st m a
prim = m a -> StateT st m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> StateT st m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> StateT st m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}
instance MonadPrim s m => MonadPrim s (Strict.StateT st m) where
  prim :: (State# s -> (# State# s, a #)) -> StateT st m a
prim = m a -> StateT st m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> StateT st m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> StateT st m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}

instance (Monoid w, MonadPrim s m) => MonadPrim s (Lazy.WriterT w m) where
  prim :: (State# s -> (# State# s, a #)) -> WriterT w m a
prim = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT w m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> WriterT w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}
instance (Monoid w, MonadPrim s m) => MonadPrim s (Strict.WriterT w m) where
  prim :: (State# s -> (# State# s, a #)) -> WriterT w m a
prim = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT w m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> WriterT w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}


#if MIN_VERSION_transformers(0, 5, 3)

instance (Monoid w, MonadPrim s m) => MonadPrim s (AccumT w m) where
  prim :: (State# s -> (# State# s, a #)) -> AccumT w m a
prim = m a -> AccumT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> AccumT w m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> AccumT w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}
instance MonadPrim s m => MonadPrim s (SelectT r m) where
  prim :: (State# s -> (# State# s, a #)) -> SelectT r m a
prim = m a -> SelectT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> SelectT r m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> SelectT r m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}

#if MIN_VERSION_transformers(0, 5, 6)

instance MonadPrim s m => MonadPrim s (CPS.RWST r w st m) where
  prim :: (State# s -> (# State# s, a #)) -> RWST r w st m a
prim = m a -> RWST r w st m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> RWST r w st m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> RWST r w st m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}
instance MonadPrim s m => MonadPrim s (CPS.WriterT w m) where
  prim :: (State# s -> (# State# s, a #)) -> WriterT w m a
prim = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT w m a)
-> ((State# s -> (# State# s, a #)) -> m a)
-> (State# s -> (# State# s, a #))
-> WriterT w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim
  {-# INLINE prim #-}

#endif
#endif

primBase_ :: MonadPrimBase s m => m () -> State# s -> State# s
primBase_ :: m () -> State# s -> State# s
primBase_ m ()
m State# s
s = case m () -> State# s -> (# State# s, () #)
forall s (m :: * -> *) a.
MonadPrimBase s m =>
m a -> State# s -> (# State# s, a #)
primBase m ()
m State# s
s of
                  (# State# s
s', () #) -> State# s
s'
{-# INLINE primBase_ #-}

-- | Construct a primitive action that does not return anything.
prim_ :: MonadPrim s m => (State# s -> State# s) -> m ()
prim_ :: (State# s -> State# s) -> m ()
prim_ State# s -> State# s
f = (State# s -> (# State# s, () #)) -> m ()
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim ((State# s -> (# State# s, () #)) -> m ())
-> (State# s -> (# State# s, () #)) -> m ()
forall a b. (a -> b) -> a -> b
$ \State# s
s -> (# State# s -> State# s
f State# s
s, () #)
{-# INLINE prim_ #-}

-- | Lift an `IO` action to `MonadPrim` with the `RealWorld` state token. Type restricted
-- synonym for `liftPrimBase`
liftIO :: MonadPrim RW m => IO a -> m a
liftIO :: IO a -> m a
liftIO (IO State# RealWorld -> (# State# RealWorld, a #)
m) = (State# RealWorld -> (# State# RealWorld, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim State# RealWorld -> (# State# RealWorld, a #)
m
{-# INLINE liftIO #-}

-- | Lift an `ST` action to `MonadPrim` with the same state token. Type restricted synonym
-- for `liftPrimBase`
liftST :: MonadPrim s m => ST s a -> m a
liftST :: ST s a -> m a
liftST (ST STRep s a
m) = STRep s a -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim STRep s a
m
{-# INLINE liftST #-}

-- | Lift an action from the `MonadPrimBase` to another `MonadPrim` with the same state
-- token.
liftPrimBase :: (MonadPrimBase s n, MonadPrim s m) => n a -> m a
liftPrimBase :: n a -> m a
liftPrimBase n a
m = (State# s -> (# State# s, a #)) -> m a
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (n a -> State# s -> (# State# s, a #)
forall s (m :: * -> *) a.
MonadPrimBase s m =>
m a -> State# s -> (# State# s, a #)
primBase n a
m)
{-# INLINE[0] liftPrimBase #-}
{-# RULES
 "liftPrimBase/id" liftPrimBase = id
 #-}

-- | Restrict a `MonadPrimBase` action that works with `RealWorld` to `IO`.
primBaseToIO :: MonadPrimBase RealWorld m => m a -> IO a
primBaseToIO :: m a -> IO a
primBaseToIO = m a -> IO a
forall s (n :: * -> *) (m :: * -> *) a.
(MonadPrimBase s n, MonadPrim s m) =>
n a -> m a
liftPrimBase
{-# INLINE primBaseToIO #-}

-- | Restrict a `MonadPrimBase` action that works in `ST`.
primBaseToST :: MonadPrimBase s m => m a -> ST s a
primBaseToST :: m a -> ST s a
primBaseToST = m a -> ST s a
forall s (n :: * -> *) (m :: * -> *) a.
(MonadPrimBase s n, MonadPrim s m) =>
n a -> m a
liftPrimBase
{-# INLINE primBaseToST #-}


-- | Unwrap `ST`
unST :: ST s a -> State# s -> (# State# s, a #)
unST :: ST s a -> State# s -> (# State# s, a #)
unST (ST State# s -> (# State# s, a #)
m) = State# s -> (# State# s, a #)
m
{-# INLINE unST #-}


-- | Unwrap `ST` that returns unit
unST_ :: ST s () -> State# s -> State# s
unST_ :: ST s () -> State# s -> State# s
unST_ (ST STRep s ()
m) State# s
s =
  case STRep s ()
m State# s
s of
    (# State# s
s', () #) -> State# s
s'
{-# INLINE unST_ #-}


-- | Unwrap `IO` that returns unit
unIO_ :: IO () -> State# RW -> State# RW
unIO_ :: IO () -> State# RealWorld -> State# RealWorld
unIO_ (IO State# RealWorld -> (# State# RealWorld, () #)
m) State# RealWorld
s =
  case State# RealWorld -> (# State# RealWorld, () #)
m State# RealWorld
s of
    (# State# RealWorld
s', () #) -> State# RealWorld
s'
{-# INLINE unIO_ #-}