Skip to content

Commit a4aa3a3

Browse files
committed
Merge pull request #55 from ethul/topic/readert-monad-rec
MonadRec instance for ReaderT
2 parents 995b266 + f7114a0 commit a4aa3a3

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

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/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)