-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReaderT.hs
38 lines (28 loc) · 937 Bytes
/
ReaderT.hs
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
36
37
38
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module ReaderT where
import Control.Monad
newtype MyReaderT r m a = MyReaderT
{ runMyReaderT :: r -> m a
}
class MonadTrans t where
lift :: m a -> t m a
class Monad m =>
(MonadReader r m) where
ask :: m r
asks :: (r -> a) -> m a
instance Monad m => Functor (MyReaderT r m) where
fmap = liftM
instance Monad m => Applicative (MyReaderT r m) where
pure a = MyReaderT $ \_ -> return a
(<*>) = ap
instance Monad m => Monad (MyReaderT r m) where
return = pure
(MyReaderT a) >>= b = MyReaderT $ \r -> a r >>= flip runMyReaderT r . b
instance MonadTrans (MyReaderT r) where
lift m = MyReaderT $ \_ -> m
instance Monad m => MonadReader r (MyReaderT r m) where
ask = MyReaderT $ \r -> return r
asks a = MyReaderT $ \r -> return $ a r