{-# LANGUAGE TemplateHaskell #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Control.Effect.RWS.Strict
-- Copyright   :  (c) Michael Szvetits, 2020
-- License     :  BSD3 (see the file LICENSE)
-- Maintainer  :  typedbyte@qualified.name
-- Stability   :  stable
-- Portability :  portable
--
-- Strict interpretations of the 'RWS'' effect.
--
-- If you don't require disambiguation of multiple RWS effects
-- (i.e., you only have one RWS effect in your monadic context),
-- you usually need the untagged interpretations.
-----------------------------------------------------------------------------
module Control.Effect.RWS.Strict
  ( -- * Interpreter Type
    RWST
    -- * Tagged Interpretations
  , evalRWS'
  , execRWS'
  , runRWS'
    -- * Untagged Interpretations
  , evalRWS
  , execRWS
  , runRWS
  ) where

-- base
import Control.Monad (liftM)
import Data.Coerce   (coerce)

-- transformers
import qualified Control.Monad.Trans.RWS.CPS as RWS

import Control.Effect.Machinery
import Control.Effect.Reader    (Reader, Reader')
import Control.Effect.RWS       (RWS, RWS')
import Control.Effect.State     (State, State')
import Control.Effect.Writer    (Writer, Writer')

-- This is necessary until the writer CPS instances land in monad-control.
-- See: https://github.com/basvandijk/monad-control/pull/51
-- | The strict interpreter of the RWS effect. This type implements the
-- 'RWS'' type class in a strict manner.
--
-- When interpreting the effect, you usually don\'t interact with this type directly,
-- but instead use one of its corresponding interpretation functions.
newtype RWST r w s m a =
  RWST { RWST r w s m a -> RWST r w s m a
runRWST :: RWS.RWST r w s m a }
    deriving (Functor (RWST r w s m)
a -> RWST r w s m a
Functor (RWST r w s m)
-> (forall a. a -> RWST r w s m a)
-> (forall a b.
    RWST r w s m (a -> b) -> RWST r w s m a -> RWST r w s m b)
-> (forall a b c.
    (a -> b -> c)
    -> RWST r w s m a -> RWST r w s m b -> RWST r w s m c)
-> (forall a b. RWST r w s m a -> RWST r w s m b -> RWST r w s m b)
-> (forall a b. RWST r w s m a -> RWST r w s m b -> RWST r w s m a)
-> Applicative (RWST r w s m)
RWST r w s m a -> RWST r w s m b -> RWST r w s m b
RWST r w s m a -> RWST r w s m b -> RWST r w s m a
RWST r w s m (a -> b) -> RWST r w s m a -> RWST r w s m b
(a -> b -> c) -> RWST r w s m a -> RWST r w s m b -> RWST r w s m c
forall a. a -> RWST r w s m a
forall a b. RWST r w s m a -> RWST r w s m b -> RWST r w s m a
forall a b. RWST r w s m a -> RWST r w s m b -> RWST r w s m b
forall a b.
RWST r w s m (a -> b) -> RWST r w s m a -> RWST r w s m b
forall a b c.
(a -> b -> c) -> RWST r w s m a -> RWST r w s m b -> RWST r w s m c
forall r w s (m :: * -> *). Monad m => Functor (RWST r w s m)
forall r w s (m :: * -> *) a. Monad m => a -> RWST r w s m a
forall r w s (m :: * -> *) a b.
Monad m =>
RWST r w s m a -> RWST r w s m b -> RWST r w s m a
forall r w s (m :: * -> *) a b.
Monad m =>
RWST r w s m a -> RWST r w s m b -> RWST r w s m b
forall r w s (m :: * -> *) a b.
Monad m =>
RWST r w s m (a -> b) -> RWST r w s m a -> RWST r w s m b
forall r w s (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> RWST r w s m a -> RWST r w s m b -> RWST r w s 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
<* :: RWST r w s m a -> RWST r w s m b -> RWST r w s m a
$c<* :: forall r w s (m :: * -> *) a b.
Monad m =>
RWST r w s m a -> RWST r w s m b -> RWST r w s m a
*> :: RWST r w s m a -> RWST r w s m b -> RWST r w s m b
$c*> :: forall r w s (m :: * -> *) a b.
Monad m =>
RWST r w s m a -> RWST r w s m b -> RWST r w s m b
liftA2 :: (a -> b -> c) -> RWST r w s m a -> RWST r w s m b -> RWST r w s m c
$cliftA2 :: forall r w s (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> RWST r w s m a -> RWST r w s m b -> RWST r w s m c
<*> :: RWST r w s m (a -> b) -> RWST r w s m a -> RWST r w s m b
$c<*> :: forall r w s (m :: * -> *) a b.
Monad m =>
RWST r w s m (a -> b) -> RWST r w s m a -> RWST r w s m b
pure :: a -> RWST r w s m a
$cpure :: forall r w s (m :: * -> *) a. Monad m => a -> RWST r w s m a
$cp1Applicative :: forall r w s (m :: * -> *). Monad m => Functor (RWST r w s m)
Applicative, a -> RWST r w s m b -> RWST r w s m a
(a -> b) -> RWST r w s m a -> RWST r w s m b
(forall a b. (a -> b) -> RWST r w s m a -> RWST r w s m b)
-> (forall a b. a -> RWST r w s m b -> RWST r w s m a)
-> Functor (RWST r w s m)
forall a b. a -> RWST r w s m b -> RWST r w s m a
forall a b. (a -> b) -> RWST r w s m a -> RWST r w s m b
forall r w s (m :: * -> *) a b.
Functor m =>
a -> RWST r w s m b -> RWST r w s m a
forall r w s (m :: * -> *) a b.
Functor m =>
(a -> b) -> RWST r w s m a -> RWST r w s m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> RWST r w s m b -> RWST r w s m a
$c<$ :: forall r w s (m :: * -> *) a b.
Functor m =>
a -> RWST r w s m b -> RWST r w s m a
fmap :: (a -> b) -> RWST r w s m a -> RWST r w s m b
$cfmap :: forall r w s (m :: * -> *) a b.
Functor m =>
(a -> b) -> RWST r w s m a -> RWST r w s m b
Functor, Applicative (RWST r w s m)
a -> RWST r w s m a
Applicative (RWST r w s m)
-> (forall a b.
    RWST r w s m a -> (a -> RWST r w s m b) -> RWST r w s m b)
-> (forall a b. RWST r w s m a -> RWST r w s m b -> RWST r w s m b)
-> (forall a. a -> RWST r w s m a)
-> Monad (RWST r w s m)
RWST r w s m a -> (a -> RWST r w s m b) -> RWST r w s m b
RWST r w s m a -> RWST r w s m b -> RWST r w s m b
forall a. a -> RWST r w s m a
forall a b. RWST r w s m a -> RWST r w s m b -> RWST r w s m b
forall a b.
RWST r w s m a -> (a -> RWST r w s m b) -> RWST r w s m b
forall r w s (m :: * -> *). Monad m => Applicative (RWST r w s m)
forall r w s (m :: * -> *) a. Monad m => a -> RWST r w s m a
forall r w s (m :: * -> *) a b.
Monad m =>
RWST r w s m a -> RWST r w s m b -> RWST r w s m b
forall r w s (m :: * -> *) a b.
Monad m =>
RWST r w s m a -> (a -> RWST r w s m b) -> RWST r w s 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
return :: a -> RWST r w s m a
$creturn :: forall r w s (m :: * -> *) a. Monad m => a -> RWST r w s m a
>> :: RWST r w s m a -> RWST r w s m b -> RWST r w s m b
$c>> :: forall r w s (m :: * -> *) a b.
Monad m =>
RWST r w s m a -> RWST r w s m b -> RWST r w s m b
>>= :: RWST r w s m a -> (a -> RWST r w s m b) -> RWST r w s m b
$c>>= :: forall r w s (m :: * -> *) a b.
Monad m =>
RWST r w s m a -> (a -> RWST r w s m b) -> RWST r w s m b
$cp1Monad :: forall r w s (m :: * -> *). Monad m => Applicative (RWST r w s m)
Monad, Monad (RWST r w s m)
Monad (RWST r w s m)
-> (forall a. IO a -> RWST r w s m a) -> MonadIO (RWST r w s m)
IO a -> RWST r w s m a
forall a. IO a -> RWST r w s m a
forall r w s (m :: * -> *). MonadIO m => Monad (RWST r w s m)
forall r w s (m :: * -> *) a. MonadIO m => IO a -> RWST r w s m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: IO a -> RWST r w s m a
$cliftIO :: forall r w s (m :: * -> *) a. MonadIO m => IO a -> RWST r w s m a
$cp1MonadIO :: forall r w s (m :: * -> *). MonadIO m => Monad (RWST r w s m)
MonadIO)
    deriving (m a -> RWST r w s m a
(forall (m :: * -> *) a. Monad m => m a -> RWST r w s m a)
-> MonadTrans (RWST r w s)
forall r w s (m :: * -> *) a. Monad m => m a -> RWST r w s m a
forall (m :: * -> *) a. Monad m => m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> RWST r w s m a
$clift :: forall r w s (m :: * -> *) a. Monad m => m a -> RWST r w s m a
MonadTrans)
    deriving (RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s)

instance MonadBase b m => MonadBase b (RWST r w s m) where
  liftBase :: b α -> RWST r w s m α
liftBase = b α -> RWST r w s m α
forall (t :: (* -> *) -> * -> *) (b :: * -> *) (m :: * -> *) α.
(MonadTrans t, MonadBase b m) =>
b α -> t m α
liftBaseDefault
  {-# INLINE liftBase #-}

instance (MonadBaseControl b m, Monoid w) => MonadBaseControl b (RWST r w s m) where
  type StM (RWST r w s m) a = ComposeSt (RWST r w s) m a
  liftBaseWith :: (RunInBase (RWST r w s m) b -> b a) -> RWST r w s m a
liftBaseWith = (RunInBase (RWST r w s m) b -> b a) -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (b :: * -> *) (m :: * -> *) a.
(MonadTransControl t, MonadBaseControl b m) =>
(RunInBaseDefault t m b -> b a) -> t m a
defaultLiftBaseWith
  {-# INLINE liftBaseWith #-}
  restoreM :: StM (RWST r w s m) a -> RWST r w s m a
restoreM = StM (RWST r w s m) a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (b :: * -> *) (m :: * -> *) a.
(MonadTransControl t, MonadBaseControl b m) =>
ComposeSt t m a -> t m a
defaultRestoreM
  {-# INLINE restoreM #-}

instance Monoid w => MonadTransControl (RWST r w s) where
  type StT (RWST r w s) a = (a, s, w)
  liftWith :: (Run (RWST r w s) -> m a) -> RWST r w s m a
liftWith Run (RWST r w s) -> m a
f = RWST r w s m a -> RWST r w s m a
forall r w s (m :: * -> *) a. RWST r w s m a -> RWST r w s m a
RWST (RWST r w s m a -> RWST r w s m a)
-> ((r -> s -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> m (a, s, w))
-> RWST r w s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> s -> m (a, s, w)) -> RWST r w s m a
forall (m :: * -> *) w r s a.
(Functor m, Monoid w) =>
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWS.rwsT ((r -> s -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$
    \r
r s
s -> (a -> (a, s, w)) -> m a -> m (a, s, w)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ( \a
x -> (a
x, s
s, w
forall a. Monoid a => a
mempty) )
                  ( Run (RWST r w s) -> m a
f (Run (RWST r w s) -> m a) -> Run (RWST r w s) -> m a
forall a b. (a -> b) -> a -> b
$ \RWST r w s n b
t -> (RWST r w s n b -> r -> s -> n (b, s, w)
forall w r s (m :: * -> *) a.
Monoid w =>
RWST r w s m a -> r -> s -> m (a, s, w)
RWS.runRWST (RWST r w s n b -> r -> s -> n (b, s, w))
-> (RWST r w s n b -> RWST r w s n b)
-> RWST r w s n b
-> r
-> s
-> n (b, s, w)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RWST r w s n b -> RWST r w s n b
forall r w s (m :: * -> *) a. RWST r w s m a -> RWST r w s m a
runRWST) RWST r w s n b
t r
r s
s )
  {-# INLINABLE liftWith #-}
  restoreT :: m (StT (RWST r w s) a) -> RWST r w s m a
restoreT m (StT (RWST r w s) a)
mSt = RWST r w s m a -> RWST r w s m a
forall r w s (m :: * -> *) a. RWST r w s m a -> RWST r w s m a
RWST (RWST r w s m a -> RWST r w s m a)
-> ((r -> s -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> m (a, s, w))
-> RWST r w s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> s -> m (a, s, w)) -> RWST r w s m a
forall (m :: * -> *) w r s a.
(Functor m, Monoid w) =>
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWS.rwsT ((r -> s -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \r
_ s
_ -> m (a, s, w)
m (StT (RWST r w s) a)
mSt
  {-# INLINABLE restoreT #-}

-- | Runs the RWS effect and discards the final state.
evalRWS'
  :: forall tag r w s m a. (Functor m, Monoid w)
  => r
  -- ^ The initial environment.
  -> s
  -- ^ The initial state.
  -> ('[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s] `EachVia` RWST r w s) m a
  -- ^ The program whose RWS effect should be handled.
  -> m (w, a)
  -- ^ The program with its RWS effect handled, producing the final
  -- output @w@ and the result @a@.
evalRWS' :: r
-> s
-> EachVia
     '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
     (RWST r w s)
     m
     a
-> m (w, a)
evalRWS' r
r s
s = ((a, s, w) -> (w, a)) -> m (a, s, w) -> m (w, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, s, w) -> (w, a)
forall b b a. (b, b, a) -> (a, b)
reorder (m (a, s, w) -> m (w, a))
-> (EachVia
      '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
      (RWST r w s)
      m
      a
    -> m (a, s, w))
-> EachVia
     '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
     (RWST r w s)
     m
     a
-> m (w, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\RWST r w s m a
m -> RWST r w s m a -> r -> s -> m (a, s, w)
forall w r s (m :: * -> *) a.
Monoid w =>
RWST r w s m a -> r -> s -> m (a, s, w)
RWS.runRWST RWST r w s m a
m r
r s
s) (RWST r w s m a -> m (a, s, w))
-> (EachVia
      '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
      (RWST r w s)
      m
      a
    -> RWST r w s m a)
-> EachVia
     '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
     (RWST r w s)
     m
     a
-> m (a, s, w)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EachVia
  '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
  (RWST r w s)
  m
  a
-> RWST r w s m a
coerce
  where
    reorder :: (b, b, a) -> (a, b)
reorder (b
a, b
_, a
w) = (a
w, b
a)
{-# INLINE evalRWS' #-}

-- | Runs the RWS effect and discards the result of the interpreted program.
execRWS'
  :: forall tag r w s m a. (Functor m, Monoid w)
  => r
  -- ^ The initial environment.
  -> s
  -- ^ The initial state.
  -> ('[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s] `EachVia` RWST r w s) m a
  -- ^ The program whose RWS effect should be handled.
  -> m (w, s)
  -- ^ The program with its RWS effect handled, producing the final
  -- output @w@ and the final state @s@.
execRWS' :: r
-> s
-> EachVia
     '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
     (RWST r w s)
     m
     a
-> m (w, s)
execRWS' r
r s
s = ((a, s, w) -> (w, s)) -> m (a, s, w) -> m (w, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, s, w) -> (w, s)
forall a b a. (a, b, a) -> (a, b)
reorder (m (a, s, w) -> m (w, s))
-> (EachVia
      '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
      (RWST r w s)
      m
      a
    -> m (a, s, w))
-> EachVia
     '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
     (RWST r w s)
     m
     a
-> m (w, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\RWST r w s m a
m -> RWST r w s m a -> r -> s -> m (a, s, w)
forall w r s (m :: * -> *) a.
Monoid w =>
RWST r w s m a -> r -> s -> m (a, s, w)
RWS.runRWST RWST r w s m a
m r
r s
s) (RWST r w s m a -> m (a, s, w))
-> (EachVia
      '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
      (RWST r w s)
      m
      a
    -> RWST r w s m a)
-> EachVia
     '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
     (RWST r w s)
     m
     a
-> m (a, s, w)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EachVia
  '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
  (RWST r w s)
  m
  a
-> RWST r w s m a
coerce
  where
    reorder :: (a, b, a) -> (a, b)
reorder (a
_, b
s', a
w) = (a
w, b
s')
{-# INLINE execRWS' #-}

-- | Runs the RWS effect and returns the final output, the final state and the result of the interpreted program.
runRWS'
  :: forall tag r w s m a. (Functor m, Monoid w)
  => r
  -- ^ The initial environment.
  -> s
  -- ^ The initial state.
  -> ('[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s] `EachVia` RWST r w s) m a
  -- ^ The program whose RWS effect should be handled.
  -> m (w, s, a)
  -- ^ The program with its RWS effect handled, producing the final
  -- output @w@, the final state @s@ and the result @a@.
runRWS' :: r
-> s
-> EachVia
     '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
     (RWST r w s)
     m
     a
-> m (w, s, a)
runRWS' r
r s
s = ((a, s, w) -> (w, s, a)) -> m (a, s, w) -> m (w, s, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, s, w) -> (w, s, a)
forall c b a. (c, b, a) -> (a, b, c)
reorder (m (a, s, w) -> m (w, s, a))
-> (EachVia
      '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
      (RWST r w s)
      m
      a
    -> m (a, s, w))
-> EachVia
     '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
     (RWST r w s)
     m
     a
-> m (w, s, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\RWST r w s m a
m -> RWST r w s m a -> r -> s -> m (a, s, w)
forall w r s (m :: * -> *) a.
Monoid w =>
RWST r w s m a -> r -> s -> m (a, s, w)
RWS.runRWST RWST r w s m a
m r
r s
s) (RWST r w s m a -> m (a, s, w))
-> (EachVia
      '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
      (RWST r w s)
      m
      a
    -> RWST r w s m a)
-> EachVia
     '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
     (RWST r w s)
     m
     a
-> m (a, s, w)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EachVia
  '[RWS' tag r w s, Reader' tag r, Writer' tag w, State' tag s]
  (RWST r w s)
  m
  a
-> RWST r w s m a
coerce
  where
    reorder :: (c, b, a) -> (a, b, c)
reorder (c
a, b
s', a
w) = (a
w, b
s', c
a)
{-# INLINE runRWS' #-}

makeUntagged ['evalRWS', 'execRWS', 'runRWS']