on master help sign in
Add ReaderT.
commitb92d2a91fd9bb4f4519d430bca23b15dad9ee44b authorGeorge Leontiev <folone@gmail.com> dateThu 31 Oct 2013, 09:33 (13 years ago) parent7e8466f
2 files changed+42−1
lib/Control/Monad/Reader.idr+41 lib/base.ipkg+1−1
lib/Control/Monad/Reader.idrnew file+41
@@ -0,0 +1,41 @@
1+module Control.Monad.Reader
2+
3+import Builtins
4+import Control.Monad.Identity
5+import Prelude.Monad
6+import Prelude.Applicative
7+import Prelude.Functor
8+
9+%access public
10+
11+class Monad m => MonadReader r (m : Type -> Type) where
12+ ask : m r
13+
14+record ReaderT : Type -> (Type -> Type) -> Type -> Type where
15+ RD : {m : Type -> Type} ->
16+ (runReaderT : r -> m a) -> ReaderT r m a
17+
18+liftReaderT : {m : Type -> Type} -> m a -> ReaderT r m a
19+liftReaderT m = RD $ const m
20+
21+instance Functor f => Functor (ReaderT r f) where
22+ map f (RD g) = RD $ (map f) . g
23+
24+instance Applicative m => Applicative (ReaderT r m) where
25+ pure = liftReaderT . pure
26+ (RD f) <$> (RD v) = RD $ \r => f r <$> v r
27+
28+instance Alternative m => Alternative (ReaderT r m) where
29+ empty = liftReaderT empty
30+ (RD m) <|> (RD n) = RD $ \r => m r <|> n r
31+
32+instance Monad m => Monad (ReaderT r m) where
33+ (RD f) >>= k = RD $ \r => do a <- f r
34+ let RD ka = k a
35+ ka r
36+
37+instance Monad m => MonadReader r (ReaderT r m) where
38+ ask = RD return
39+
40+Reader : Type -> Type -> Type
41+Reader r a = ReaderT r Identity a
lib/base.ipkg+1−1
@@ -29,6 +29,6 @@ modules = Builtins, Prelude, IO, System,
2929 Data.Vect, Data.HVect, Data.Vect.Quantifiers,
3030
3131 Control.Monad.Identity, Control.Monad.State, Control.Category,
32− Control.Monad.Writer,
32+ Control.Monad.Writer, Control.Monad.Reader,
3333 Control.Arrow,
3434 Control.Catchable, Control.IOExcept