Skip to content

Commit 9f33bee

Browse files
committed
MonadRec instance for ReaderT
1 parent 3c44f0d commit 9f33bee

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/bower_components/
55
/node_modules/
66
/output/
7+
/tmp/

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ 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+
import Data.Tuple
28+
2529
-- | The reader monad transformer.
2630
-- |
2731
-- | This monad transformer extends the base monad transformer with a _global context_ of
@@ -96,3 +100,12 @@ instance monadWriterReaderT :: (Monad m, MonadWriter w m) => MonadWriter w (Read
96100
instance distributiveReaderT :: (Distributive g) => Distributive (ReaderT e g) where
97101
distribute a = ReaderT \e -> collect (flip runReaderT e) a
98102
collect f = distribute <<< map f
103+
104+
instance monadRecReaderT :: (MonadRec m) => MonadRec (ReaderT r m) where
105+
tailRecM k a = ReaderT \r -> tailRecM k' (Tuple a r)
106+
where
107+
k' (Tuple a r) = do
108+
result <- runReaderT (k a) r
109+
return case result of
110+
Left a -> Left (Tuple a r)
111+
Right b -> Right b

0 commit comments

Comments
 (0)