Skip to content

Commit e31ddaf

Browse files
committed
Merge branch 'master' into topic/rwst-monad-rec
2 parents 0a0c372 + a4aa3a3 commit e31ddaf

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

docs/Control/Monad/RWS/Trans.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ instance monadReaderRWST :: (Monad m, Monoid w) => MonadReader r (RWST r w s m)
3737
instance monadStateRWST :: (Monad m, Monoid w) => MonadState s (RWST r w s m)
3838
instance monadWriterRWST :: (Monad m, Monoid w) => MonadWriter w (RWST r w s m)
3939
instance monadRWSRWST :: (Monad m, Monoid w) => MonadRWS r w s (RWST r w s m)
40+
instance monadErrorRWST :: (MonadError e m, Monoid w) => MonadError e (RWST r w s m)
4041
instance monadRecRWST :: (Monoid w, MonadRec m) => MonadRec (RWST r w s m)
4142
```
4243

docs/Control/Monad/Reader/Trans.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ instance monadReaderReaderT :: (Monad m) => MonadReader r (ReaderT r m)
3535
instance monadStateReaderT :: (MonadState s m) => MonadState s (ReaderT r m)
3636
instance monadWriterReaderT :: (Monad m, MonadWriter w m) => MonadWriter w (ReaderT r m)
3737
instance distributiveReaderT :: (Distributive g) => Distributive (ReaderT e g)
38+
instance monadRecReaderT :: (MonadRec m) => MonadRec (ReaderT r m)
3839
```
3940

4041
#### `runReaderT`

src/Control/Monad/RWS/Trans.purs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- | This module defines the reader-writer-state monad transformer, `RWST`.
22

3-
module Control.Monad.RWS.Trans
3+
module Control.Monad.RWS.Trans
44
( See(), mkSee
55
, RWST(..), runRWST, evalRWST, execRWST, mapRWST, withRWST
66
, module Control.Monad.Trans
@@ -13,13 +13,14 @@ import Data.Either
1313
import Data.Monoid
1414
import Data.Tuple
1515

16-
import Control.Monad.Trans
1716
import Control.Monad.Eff.Class
17+
import Control.Monad.Error.Class
18+
import Control.Monad.RWS.Class
1819
import Control.Monad.Rec.Class
1920
import Control.Monad.Reader.Class
20-
import Control.Monad.Writer.Class
2121
import Control.Monad.State.Class
22-
import Control.Monad.RWS.Class
22+
import Control.Monad.Trans
23+
import Control.Monad.Writer.Class
2324

2425
type See s a w =
2526
{ state :: s
@@ -94,6 +95,10 @@ instance monadWriterRWST :: (Monad m, Monoid w) => MonadWriter w (RWST r w s m)
9495

9596
instance monadRWSRWST :: (Monad m, Monoid w) => MonadRWS r w s (RWST r w s m)
9697

98+
instance monadErrorRWST :: (MonadError e m, Monoid w) => MonadError e (RWST r w s m) where
99+
throwError e = lift (throwError e)
100+
catchError m h = RWST $ \r s -> catchError (runRWST m r s) (\e -> runRWST (h e) r s)
101+
97102
instance monadRecRWST :: (Monoid w, MonadRec m) => MonadRec (RWST r w s m) where
98103
tailRecM k a = RWST \r s -> tailRecM (k' r) { writer: mempty, state: s, result: a }
99104
where

src/Control/Monad/Reader/Trans.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ import Control.Monad.Eff.Class
1616
import Control.Monad.Cont.Class
1717
import Control.Monad.Error.Class
1818
import Control.Monad.Reader.Class
19+
import Control.Monad.Rec.Class
1920
import Control.Monad.State.Class
2021
import Control.Monad.Writer.Class
2122
import Control.Monad.Trans
2223
import Control.MonadPlus
2324
import Control.Plus
2425

26+
import Data.Either
27+
2528
-- | The reader monad transformer.
2629
-- |
2730
-- | This monad transformer extends the base monad transformer with a _global context_ of
@@ -96,3 +99,10 @@ instance monadWriterReaderT :: (Monad m, MonadWriter w m) => MonadWriter w (Read
9699
instance distributiveReaderT :: (Distributive g) => Distributive (ReaderT e g) where
97100
distribute a = ReaderT \e -> collect (flip runReaderT e) a
98101
collect f = distribute <<< map f
102+
103+
instance monadRecReaderT :: (MonadRec m) => MonadRec (ReaderT r m) where
104+
tailRecM k a = ReaderT \r -> tailRecM (k' r) a
105+
where
106+
k' r a = do
107+
result <- runReaderT (k a) r
108+
return $ either Left Right result

0 commit comments

Comments
 (0)