{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
module Data.Conduino.Lift (
stateP, runStateP, evalStateP, execStateP
, statePS, runStatePS, evalStatePS, execStatePS
, exceptP, runExceptP, runExceptP_
, readerP, runReaderP
, writerP, runWriterP, execWriterP
, writerPS, runWriterPS, execWriterPS
, rwsP, runRWSP, evalRWSP, execRWSP
, rwsPS, runRWSPS, evalRWSPS, execRWSPS
, catchP, runCatchP
) where
import Control.Monad
import Control.Monad.Catch.Pure
import Control.Monad.Trans.Class
import Control.Monad.Trans.Except
import Control.Monad.Trans.Free
import Control.Monad.Trans.Free.Church
import Control.Monad.Trans.RWS (RWST(..))
import Control.Monad.Trans.Reader
import Control.Monad.Trans.State
import Control.Monad.Trans.Writer
import Data.Conduino
import Data.Conduino.Internal
import Data.Functor
import qualified Control.Monad.Trans.RWS as RWS
import qualified Control.Monad.Trans.RWS.Strict as RWSS
import qualified Control.Monad.Trans.State.Strict as SS
import qualified Control.Monad.Trans.Writer.Strict as WS
stateP
:: Monad m
=> (s -> Pipe i o u m (a, s))
-> Pipe i o u (StateT s m) a
stateP :: forall (m :: * -> *) s i o u a.
Monad m =>
(s -> Pipe i o u m (a, s)) -> Pipe i o u (StateT s m) a
stateP s -> Pipe i o u m (a, s)
f = do
s
s <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *) s. Monad m => StateT s m s
get
(a
x, s
s') <- forall (m :: * -> *) (n :: * -> *) i o u a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Pipe i o u m a -> Pipe i o u n a
hoistPipe forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (s -> Pipe i o u m (a, s)
f s
s)
a
x forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put s
s')
execStateP
:: Monad m
=> s
-> Pipe i o u (StateT s m) a
-> Pipe i o u m s
execStateP :: forall (m :: * -> *) s i o u a.
Monad m =>
s -> Pipe i o u (StateT s m) a -> Pipe i o u m s
execStateP s
s = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) s i o u a.
Monad m =>
s -> Pipe i o u (StateT s m) a -> Pipe i o u m (a, s)
runStateP s
s
{-# INLINE execStateP #-}
evalStateP
:: Monad m
=> s
-> Pipe i o u (StateT s m) a
-> Pipe i o u m a
evalStateP :: forall (m :: * -> *) s i o u a.
Monad m =>
s -> Pipe i o u (StateT s m) a -> Pipe i o u m a
evalStateP s
s = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) s i o u a.
Monad m =>
s -> Pipe i o u (StateT s m) a -> Pipe i o u m (a, s)
runStateP s
s
{-# INLINE evalStateP #-}
statePS
:: Monad m
=> (s -> Pipe i o u m (a, s))
-> Pipe i o u (SS.StateT s m) a
statePS :: forall (m :: * -> *) s i o u a.
Monad m =>
(s -> Pipe i o u m (a, s)) -> Pipe i o u (StateT s m) a
statePS s -> Pipe i o u m (a, s)
f = do
s
s <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *) s. Monad m => StateT s m s
SS.get
(a
x, s
s') <- forall (m :: * -> *) (n :: * -> *) i o u a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Pipe i o u m a -> Pipe i o u n a
hoistPipe forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (s -> Pipe i o u m (a, s)
f s
s)
a
x forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) s. Monad m => s -> StateT s m ()
SS.put s
s')
{-# INLINE statePS #-}
execStatePS
:: Monad m
=> s
-> Pipe i o u (SS.StateT s m) a
-> Pipe i o u m s
execStatePS :: forall (m :: * -> *) s i o u a.
Monad m =>
s -> Pipe i o u (StateT s m) a -> Pipe i o u m s
execStatePS s
s = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) s i o u a.
Monad m =>
s -> Pipe i o u (StateT s m) a -> Pipe i o u m (a, s)
runStatePS s
s
{-# INLINE execStatePS #-}
evalStatePS
:: Monad m
=> s
-> Pipe i o u (SS.StateT s m) a
-> Pipe i o u m a
evalStatePS :: forall (m :: * -> *) s i o u a.
Monad m =>
s -> Pipe i o u (StateT s m) a -> Pipe i o u m a
evalStatePS s
s = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) s i o u a.
Monad m =>
s -> Pipe i o u (StateT s m) a -> Pipe i o u m (a, s)
runStatePS s
s
{-# INLINE evalStatePS #-}
exceptP
:: Monad m
=> Pipe i o u m (Either e a)
-> Pipe i o u (ExceptT e m) a
exceptP :: forall (m :: * -> *) i o u e a.
Monad m =>
Pipe i o u m (Either e a) -> Pipe i o u (ExceptT e m) a
exceptP Pipe i o u m (Either e a)
p = forall (m :: * -> *) (n :: * -> *) i o u a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Pipe i o u m a -> Pipe i o u n a
hoistPipe forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Pipe i o u m (Either e a)
p forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left e
e -> forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE e
e
Right a
x -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
{-# INLINE exceptP #-}
runExceptP
:: Monad m
=> Pipe i o u (ExceptT e m) a
-> Pipe i o u m (Either e a)
runExceptP :: forall (m :: * -> *) i o u e a.
Monad m =>
Pipe i o u (ExceptT e m) a -> Pipe i o u m (Either e a)
runExceptP (Pipe (FT forall r.
(a -> ExceptT e m r)
-> (forall x.
(x -> ExceptT e m r) -> PipeF i o u x -> ExceptT e m r)
-> ExceptT e m r
f)) = forall i o u (m :: * -> *) a.
FT (PipeF i o u) m a -> Pipe i o u m a
Pipe forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (m :: * -> *) a.
(forall r.
(a -> m r) -> (forall x. (x -> m r) -> f x -> m r) -> m r)
-> FT f m a
FT forall a b. (a -> b) -> a -> b
$ \Either e a -> m r
pr forall x. (x -> m r) -> PipeF i o u x -> m r
bd -> forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either e a -> m r
pr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left) forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (
forall r.
(a -> ExceptT e m r)
-> (forall x.
(x -> ExceptT e m r) -> PipeF i o u x -> ExceptT e m r)
-> ExceptT e m r
f (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either e a -> m r
pr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right)
(\x -> ExceptT e m r
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 x. (x -> m r) -> PipeF i o u x -> m r
bd (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either e a -> m r
pr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left) forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> ExceptT e m r
c))
)
{-# INLINE runExceptP #-}
runExceptP_
:: Monad m
=> Pipe i o u (ExceptT e m) a
-> Pipe i o u m ()
runExceptP_ :: forall (m :: * -> *) i o u e a.
Monad m =>
Pipe i o u (ExceptT e m) a -> Pipe i o u m ()
runExceptP_ = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) i o u e a.
Monad m =>
Pipe i o u (ExceptT e m) a -> Pipe i o u m (Either e a)
runExceptP
{-# INLINE runExceptP_ #-}
catchP
:: Monad m
=> Pipe i o u m (Either SomeException a)
-> Pipe i o u (CatchT m) a
catchP :: forall (m :: * -> *) i o u a.
Monad m =>
Pipe i o u m (Either SomeException a) -> Pipe i o u (CatchT m) a
catchP Pipe i o u m (Either SomeException a)
p = forall (m :: * -> *) (n :: * -> *) i o u a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Pipe i o u m a -> Pipe i o u n a
hoistPipe forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Pipe i o u m (Either SomeException a)
p forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left SomeException
e -> forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM SomeException
e
Right a
x -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
{-# INLINE catchP #-}
runCatchP
:: Monad m
=> Pipe i o u (CatchT m) a
-> Pipe i o u m (Either SomeException a)
runCatchP :: forall (m :: * -> *) i o u a.
Monad m =>
Pipe i o u (CatchT m) a -> Pipe i o u m (Either SomeException a)
runCatchP = forall (m :: * -> *) (n :: * -> *) i o u a j p v b.
(Monad m, Monad n) =>
(RecPipe i o u m a -> RecPipe j p v n b)
-> Pipe i o u m a -> Pipe j p v n b
withRecPipe forall {m :: * -> *} {f :: * -> *} {b}.
(Functor m, Functor f) =>
FreeT f (CatchT m) b -> FreeT f m (Either SomeException b)
go
where
go :: FreeT f (CatchT m) b -> FreeT f m (Either SomeException b)
go (FreeT CatchT m (FreeF f b (FreeT f (CatchT m) b))
p) = forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
FreeT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. CatchT m a -> m (Either SomeException a)
runCatchT CatchT m (FreeF f b (FreeT f (CatchT m) b))
p forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall (f :: * -> *) a b. a -> FreeF f a b
Pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left)
(\case Pure b
x -> forall (f :: * -> *) a b. a -> FreeF f a b
Pure (forall a b. b -> Either a b
Right b
x); Free f (FreeT f (CatchT m) b)
l -> forall (f :: * -> *) a b. f b -> FreeF f a b
Free (FreeT f (CatchT m) b -> FreeT f m (Either SomeException b)
go forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (FreeT f (CatchT m) b)
l))
{-# INLINE runCatchP #-}
readerP
:: Monad m
=> (r -> Pipe i o u m a)
-> Pipe i o u (ReaderT r m) a
readerP :: forall (m :: * -> *) r i o u a.
Monad m =>
(r -> Pipe i o u m a) -> Pipe i o u (ReaderT r m) a
readerP r -> Pipe i o u m a
f = forall (m :: * -> *) (n :: * -> *) i o u a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Pipe i o u m a -> Pipe i o u n a
hoistPipe forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> Pipe i o u m a
f forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
runReaderP
:: Monad m
=> r
-> Pipe i o u (ReaderT r m) a
-> Pipe i o u m a
runReaderP :: forall (m :: * -> *) r i o u a.
Monad m =>
r -> Pipe i o u (ReaderT r m) a -> Pipe i o u m a
runReaderP r
r = forall (m :: * -> *) (n :: * -> *) i o u a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Pipe i o u m a -> Pipe i o u n a
hoistPipe (forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
`runReaderT` r
r)
writerP
:: (Monad m, Monoid w)
=> Pipe i o u m (a, w)
-> Pipe i o u (WriterT w m) a
writerP :: forall (m :: * -> *) w i o u a.
(Monad m, Monoid w) =>
Pipe i o u m (a, w) -> Pipe i o u (WriterT w m) a
writerP Pipe i o u m (a, w)
p = do
(a
x, w
w) <- forall (m :: * -> *) (n :: * -> *) i o u a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Pipe i o u m a -> Pipe i o u n a
hoistPipe forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Pipe i o u m (a, w)
p
a
x forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
tell w
w)
runWriterP
:: (Monad m, Monoid w)
=> Pipe i o u (WriterT w m) a
-> Pipe i o u m (a, w)
runWriterP :: forall (m :: * -> *) w i o u a.
(Monad m, Monoid w) =>
Pipe i o u (WriterT w m) a -> Pipe i o u m (a, w)
runWriterP = forall (m :: * -> *) (n :: * -> *) i o u a j p v b.
(Monad m, Monad n) =>
(RecPipe i o u m a -> RecPipe j p v n b)
-> Pipe i o u m a -> Pipe j p v n b
withRecPipe (forall {t} {m :: * -> *} {f :: * -> *} {a}.
(Semigroup t, Functor m, Functor f) =>
t -> FreeT f (WriterT t m) a -> FreeT f m (a, t)
go forall a. Monoid a => a
mempty)
where
go :: t -> FreeT f (WriterT t m) a -> FreeT f m (a, t)
go t
w (FreeT WriterT t m (FreeF f a (FreeT f (WriterT t m) a))
p) = forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
FreeT forall a b. (a -> b) -> a -> b
$ forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
runWriterT WriterT t m (FreeF f a (FreeT f (WriterT t m) a))
p forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(FreeF f a (FreeT f (WriterT t m) a)
r, (t
w forall a. Semigroup a => a -> a -> a
<>)->t
w') ->
case FreeF f a (FreeT f (WriterT t m) a)
r of
Pure a
x -> forall (f :: * -> *) a b. a -> FreeF f a b
Pure (a
x, t
w')
Free f (FreeT f (WriterT t m) a)
l -> forall (f :: * -> *) a b. f b -> FreeF f a b
Free forall a b. (a -> b) -> a -> b
$ t -> FreeT f (WriterT t m) a -> FreeT f m (a, t)
go t
w' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (FreeT f (WriterT t m) a)
l
{-# INLINE runWriterP #-}
execWriterP
:: (Monad m, Monoid w)
=> Pipe i o u (WriterT w m) a
-> Pipe i o u m w
execWriterP :: forall (m :: * -> *) w i o u a.
(Monad m, Monoid w) =>
Pipe i o u (WriterT w m) a -> Pipe i o u m w
execWriterP = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) w i o u a.
(Monad m, Monoid w) =>
Pipe i o u (WriterT w m) a -> Pipe i o u m (a, w)
runWriterP
{-# INLINE execWriterP #-}
writerPS
:: (Monad m, Monoid w)
=> Pipe i o u m (a, w)
-> Pipe i o u (WS.WriterT w m) a
writerPS :: forall (m :: * -> *) w i o u a.
(Monad m, Monoid w) =>
Pipe i o u m (a, w) -> Pipe i o u (WriterT w m) a
writerPS Pipe i o u m (a, w)
p = do
(a
x, w
w) <- forall (m :: * -> *) (n :: * -> *) i o u a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Pipe i o u m a -> Pipe i o u n a
hoistPipe forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Pipe i o u m (a, w)
p
a
x forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
WS.tell w
w)
runWriterPS
:: (Monad m, Monoid w)
=> Pipe i o u (WS.WriterT w m) a
-> Pipe i o u m (a, w)
runWriterPS :: forall (m :: * -> *) w i o u a.
(Monad m, Monoid w) =>
Pipe i o u (WriterT w m) a -> Pipe i o u m (a, w)
runWriterPS = forall (m :: * -> *) (n :: * -> *) i o u a j p v b.
(Monad m, Monad n) =>
(RecPipe i o u m a -> RecPipe j p v n b)
-> Pipe i o u m a -> Pipe j p v n b
withRecPipe (forall {t} {m :: * -> *} {f :: * -> *} {a}.
(Semigroup t, Functor m, Functor f) =>
t -> FreeT f (WriterT t m) a -> FreeT f m (a, t)
go forall a. Monoid a => a
mempty)
where
go :: t -> FreeT f (WriterT t m) a -> FreeT f m (a, t)
go t
w (FreeT WriterT t m (FreeF f a (FreeT f (WriterT t m) a))
p) = forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
FreeT forall a b. (a -> b) -> a -> b
$ forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
WS.runWriterT WriterT t m (FreeF f a (FreeT f (WriterT t m) a))
p forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(FreeF f a (FreeT f (WriterT t m) a)
r, (t
w forall a. Semigroup a => a -> a -> a
<>)->t
w') ->
case FreeF f a (FreeT f (WriterT t m) a)
r of
Pure a
x -> forall (f :: * -> *) a b. a -> FreeF f a b
Pure (a
x, t
w')
Free f (FreeT f (WriterT t m) a)
l -> forall (f :: * -> *) a b. f b -> FreeF f a b
Free forall a b. (a -> b) -> a -> b
$ t -> FreeT f (WriterT t m) a -> FreeT f m (a, t)
go t
w' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (FreeT f (WriterT t m) a)
l
{-# INLINE runWriterPS #-}
execWriterPS
:: (Monad m, Monoid w)
=> Pipe i o u (WriterT w m) a
-> Pipe i o u m w
execWriterPS :: forall (m :: * -> *) w i o u a.
(Monad m, Monoid w) =>
Pipe i o u (WriterT w m) a -> Pipe i o u m w
execWriterPS = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) w i o u a.
(Monad m, Monoid w) =>
Pipe i o u (WriterT w m) a -> Pipe i o u m (a, w)
runWriterP
{-# INLINE execWriterPS #-}
rwsP
:: (Monad m, Monoid w)
=> (r -> s -> Pipe i o u m (a, s, w))
-> Pipe i o u (RWST r w s m) a
rwsP :: forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
(r -> s -> Pipe i o u m (a, s, w)) -> Pipe i o u (RWST r w s m) a
rwsP r -> s -> Pipe i o u m (a, s, w)
f = do
r
r <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall w (m :: * -> *) r s. (Monoid w, Monad m) => RWST r w s m r
RWS.ask
s
s <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall w (m :: * -> *) r s. (Monoid w, Monad m) => RWST r w s m s
RWS.get
(a
x, s
s', w
w) <- forall (m :: * -> *) (n :: * -> *) i o u a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Pipe i o u m a -> Pipe i o u n a
hoistPipe forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (r -> s -> Pipe i o u m (a, s, w)
f r
r s
s)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) w r s. Monad m => w -> RWST r w s m ()
RWS.tell w
w)
a
x forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall w (m :: * -> *) s r.
(Monoid w, Monad m) =>
s -> RWST r w s m ()
RWS.put s
s')
runRWSP
:: (Monad m, Monoid w)
=> r
-> s
-> Pipe i o u (RWST r w s m) a
-> Pipe i o u m (a, s, w)
runRWSP :: forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
r -> s -> Pipe i o u (RWST r w s m) a -> Pipe i o u m (a, s, w)
runRWSP r
r = forall (m :: * -> *) (n :: * -> *) i o u a j p v b.
(Monad m, Monad n) =>
(RecPipe i o u m a -> RecPipe j p v n b)
-> Pipe i o u m a -> Pipe j p v n b
withRecPipe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {t} {m :: * -> *} {f :: * -> *} {t} {a}.
(Semigroup t, Functor m, Functor f) =>
t -> t -> FreeT f (RWST r t t m) a -> FreeT f m (a, t, t)
go forall a. Monoid a => a
mempty
where
go :: t -> t -> FreeT f (RWST r t t m) a -> FreeT f m (a, t, t)
go t
w t
s (FreeT RWST r t t m (FreeF f a (FreeT f (RWST r t t m) a))
p) = forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
FreeT forall a b. (a -> b) -> a -> b
$ forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r t t m (FreeF f a (FreeT f (RWST r t t m) a))
p r
r t
s forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(FreeF f a (FreeT f (RWST r t t m) a)
q, t
s', (t
w forall a. Semigroup a => a -> a -> a
<>)->t
w') ->
case FreeF f a (FreeT f (RWST r t t m) a)
q of
Pure a
x -> forall (f :: * -> *) a b. a -> FreeF f a b
Pure (a
x, t
s', t
w')
Free f (FreeT f (RWST r t t m) a)
l -> forall (f :: * -> *) a b. f b -> FreeF f a b
Free forall a b. (a -> b) -> a -> b
$ t -> t -> FreeT f (RWST r t t m) a -> FreeT f m (a, t, t)
go t
w' t
s' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (FreeT f (RWST r t t m) a)
l
{-# INLINE runRWSP #-}
evalRWSP
:: (Monad m, Monoid w)
=> r
-> s
-> Pipe i o u (RWST r w s m) a
-> Pipe i o u m (a, w)
evalRWSP :: forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
r -> s -> Pipe i o u (RWST r w s m) a -> Pipe i o u m (a, w)
evalRWSP r
r s
s = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(a
x,s
_,w
w) -> (a
x,w
w)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
r -> s -> Pipe i o u (RWST r w s m) a -> Pipe i o u m (a, s, w)
runRWSP r
r s
s
{-# INLINE evalRWSP #-}
execRWSP
:: (Monad m, Monoid w)
=> r
-> s
-> Pipe i o u (RWST r w s m) a
-> Pipe i o u m (s, w)
execRWSP :: forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
r -> s -> Pipe i o u (RWST r w s m) a -> Pipe i o u m (s, w)
execRWSP r
r s
s = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(a
_,s
s',w
w) -> (s
s',w
w)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
r -> s -> Pipe i o u (RWST r w s m) a -> Pipe i o u m (a, s, w)
runRWSP r
r s
s
{-# INLINE execRWSP #-}
rwsPS
:: (Monad m, Monoid w)
=> (r -> s -> Pipe i o u m (a, s, w))
-> Pipe i o u (RWSS.RWST r w s m) a
rwsPS :: forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
(r -> s -> Pipe i o u m (a, s, w)) -> Pipe i o u (RWST r w s m) a
rwsPS r -> s -> Pipe i o u m (a, s, w)
f = do
r
r <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall w (m :: * -> *) r s. (Monoid w, Monad m) => RWST r w s m r
RWSS.ask
s
s <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall w (m :: * -> *) r s. (Monoid w, Monad m) => RWST r w s m s
RWSS.get
(a
x, s
s', w
w) <- forall (m :: * -> *) (n :: * -> *) i o u a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Pipe i o u m a -> Pipe i o u n a
hoistPipe forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (r -> s -> Pipe i o u m (a, s, w)
f r
r s
s)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) w r s. Monad m => w -> RWST r w s m ()
RWSS.tell w
w)
a
x forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall w (m :: * -> *) s r.
(Monoid w, Monad m) =>
s -> RWST r w s m ()
RWSS.put s
s')
runRWSPS
:: (Monad m, Monoid w)
=> r
-> s
-> Pipe i o u (RWSS.RWST r w s m) a
-> Pipe i o u m (a, s, w)
runRWSPS :: forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
r -> s -> Pipe i o u (RWST r w s m) a -> Pipe i o u m (a, s, w)
runRWSPS r
r = forall (m :: * -> *) (n :: * -> *) i o u a j p v b.
(Monad m, Monad n) =>
(RecPipe i o u m a -> RecPipe j p v n b)
-> Pipe i o u m a -> Pipe j p v n b
withRecPipe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {t} {m :: * -> *} {f :: * -> *} {t} {a}.
(Semigroup t, Functor m, Functor f) =>
t -> t -> FreeT f (RWST r t t m) a -> FreeT f m (a, t, t)
go forall a. Monoid a => a
mempty
where
go :: t -> t -> FreeT f (RWST r t t m) a -> FreeT f m (a, t, t)
go t
w t
s (FreeT RWST r t t m (FreeF f a (FreeT f (RWST r t t m) a))
p) = forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
FreeT forall a b. (a -> b) -> a -> b
$ forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
RWSS.runRWST RWST r t t m (FreeF f a (FreeT f (RWST r t t m) a))
p r
r t
s forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(FreeF f a (FreeT f (RWST r t t m) a)
q, t
s', (t
w forall a. Semigroup a => a -> a -> a
<>)->t
w') ->
case FreeF f a (FreeT f (RWST r t t m) a)
q of
Pure a
x -> forall (f :: * -> *) a b. a -> FreeF f a b
Pure (a
x, t
s', t
w')
Free f (FreeT f (RWST r t t m) a)
l -> forall (f :: * -> *) a b. f b -> FreeF f a b
Free forall a b. (a -> b) -> a -> b
$ t -> t -> FreeT f (RWST r t t m) a -> FreeT f m (a, t, t)
go t
w' t
s' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (FreeT f (RWST r t t m) a)
l
{-# INLINE runRWSPS #-}
evalRWSPS
:: (Monad m, Monoid w)
=> r
-> s
-> Pipe i o u (RWSS.RWST r w s m) a
-> Pipe i o u m (a, w)
evalRWSPS :: forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
r -> s -> Pipe i o u (RWST r w s m) a -> Pipe i o u m (a, w)
evalRWSPS r
r s
s = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(a
x,s
_,w
w) -> (a
x,w
w)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
r -> s -> Pipe i o u (RWST r w s m) a -> Pipe i o u m (a, s, w)
runRWSPS r
r s
s
{-# INLINE evalRWSPS #-}
execRWSPS
:: (Monad m, Monoid w)
=> r
-> s
-> Pipe i o u (RWSS.RWST r w s m) a
-> Pipe i o u m (s, w)
execRWSPS :: forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
r -> s -> Pipe i o u (RWST r w s m) a -> Pipe i o u m (s, w)
execRWSPS r
r s
s = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(a
_,s
s',w
w) -> (s
s',w
w)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) w r s i o u a.
(Monad m, Monoid w) =>
r -> s -> Pipe i o u (RWST r w s m) a -> Pipe i o u m (a, s, w)
runRWSPS r
r s
s
{-# INLINE execRWSPS #-}