From: Mart Lubbers Date: Fri, 15 Apr 2016 08:20:47 +0000 (+0200) Subject: RWS done X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=bc9dea4b8b3d12283a6192bd688c1f2e24bd7198;p=cc1516.git RWS done --- diff --git a/RWST.dcl b/RWST.dcl index 1a5b3b2..334df7e 100644 --- a/RWST.dcl +++ b/RWST.dcl @@ -7,8 +7,16 @@ from Data.Functor import class Functor from Data.Functor.Identity import :: Identity from Data.Monoid import class Monoid, class Semigroup +// The RWS monad :: RWS r w s a :== RWST r w s Identity a +rws :: (r -> s -> (a, s, w)) -> RWS r w s a +runRWS :: (RWS r w s a) r s -> (a, s, w) +evalRWS :: (RWS r w s a) r s -> (a, w) +execRWS :: (RWS r w s a) r s -> (s, w) +mapRWS :: ((a, s, w) -> (b, s, w`)) (RWS r w s a) -> RWS r w` s b +withRWS :: (r` s -> (r, s)) (RWS r w s a) -> RWS r` w s a + // The RWST monad transformer :: RWST r w s m a = RWST (r s -> m (a, s, w)) diff --git a/RWST.icl b/RWST.icl index 4b38482..a059ac3 100644 --- a/RWST.icl +++ b/RWST.icl @@ -10,6 +10,26 @@ import Data.Monoid import Control.Monad import Control.Applicative +// The RWS monad +:: RWS r w s a :== RWST r w s Identity a + +rws :: (r -> s -> (a, s, w)) -> RWS r w s a +rws f = RWST \r s->Identity $ f r s + +runRWS :: (RWS r w s a) r s -> (a, s, w) +runRWS m r s = runIdentity $ runRWST m r s + +evalRWS :: (RWS r w s a) r s -> (a, w) +evalRWS m r s = let (a, _, w) = runRWS m r s in (a, w) + +execRWS :: (RWS r w s a) r s -> (s, w) +execRWS m r s = let (_, s`, w) = runRWS m r s in (s`, w) + +mapRWS :: ((a, s, w) -> (b, s, w`)) (RWS r w s a) -> RWS r w` s b +mapRWS f m = mapRWST (Identity o f o runIdentity) m + +withRWS :: (r` s -> (r, s)) (RWS r w s a) -> RWS r` w s a +withRWS f m = withRWST f m // The RWST monad transformer :: RWST r w s m a = RWST (r s -> m (a, s, w))