summaryrefslogtreecommitdiff
path: root/Haskell-book/26/MaybeT/src/Reader.hs
blob: 38fded9fbccd9647d4a333edb86c8fe395606e90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Reader where

import MonadIO
import MonadTrans

newtype ReaderT r m a =
    ReaderT { runReaderT :: r -> m a }

instance (Functor m)
      => Functor (ReaderT r m) where
    fmap f (ReaderT rma) =
        ReaderT $ (fmap . fmap) f rma

instance (Applicative m)
      => Applicative (ReaderT r m) where
    pure a = ReaderT (pure (pure a))

    (ReaderT fmab) <*> (ReaderT rma) =
        ReaderT $ (<*>) <$> fmab <*> rma

instance (Monad m)
      => Monad (ReaderT r m) where
    return = pure

    (ReaderT rma) >>= f =
        ReaderT $ \r -> do
            a <- rma r
            runReaderT (f a) r

instance MonadTrans (ReaderT r) where
    lift = ReaderT . const

instance (MonadIO m)
  => MonadIO (ReaderT r m) where
      liftIO = lift . liftIO