Skip to content

Commit

Permalink
Merge pull request #55 from ethul/topic/readert-monad-rec
Browse files Browse the repository at this point in the history
MonadRec instance for ReaderT
  • Loading branch information
paf31 committed Sep 3, 2015
2 parents 995b266 + f7114a0 commit a4aa3a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/Control/Monad/Reader/Trans.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ instance monadReaderReaderT :: (Monad m) => MonadReader r (ReaderT r m)
instance monadStateReaderT :: (MonadState s m) => MonadState s (ReaderT r m)
instance monadWriterReaderT :: (Monad m, MonadWriter w m) => MonadWriter w (ReaderT r m)
instance distributiveReaderT :: (Distributive g) => Distributive (ReaderT e g)
instance monadRecReaderT :: (MonadRec m) => MonadRec (ReaderT r m)
```

#### `runReaderT`
Expand Down
10 changes: 10 additions & 0 deletions src/Control/Monad/Reader/Trans.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import Control.Monad.Eff.Class
import Control.Monad.Cont.Class
import Control.Monad.Error.Class
import Control.Monad.Reader.Class
import Control.Monad.Rec.Class
import Control.Monad.State.Class
import Control.Monad.Writer.Class
import Control.Monad.Trans
import Control.MonadPlus
import Control.Plus

import Data.Either

-- | The reader monad transformer.
-- |
-- | This monad transformer extends the base monad transformer with a _global context_ of
Expand Down Expand Up @@ -96,3 +99,10 @@ instance monadWriterReaderT :: (Monad m, MonadWriter w m) => MonadWriter w (Read
instance distributiveReaderT :: (Distributive g) => Distributive (ReaderT e g) where
distribute a = ReaderT \e -> collect (flip runReaderT e) a
collect f = distribute <<< map f

instance monadRecReaderT :: (MonadRec m) => MonadRec (ReaderT r m) where
tailRecM k a = ReaderT \r -> tailRecM (k' r) a
where
k' r a = do
result <- runReaderT (k a) r
return $ either Left Right result

0 comments on commit a4aa3a3

Please sign in to comment.