@@ -7,12 +7,16 @@
77{-# LANGUAGE TypeOperators #-}
88{-# LANGUAGE GeneralizedNewtypeDeriving #-}
99module Control.Monad.Trans.Ether.Reader
10− ( ReaderT
11− , Reader
12− , runReaderT
10+ (
11+ -- * The Reader monad
12+ Reader
1313 , runReader
14− , etherReaderT
14+ -- * The ReaderT monad transformer
15+ , ReaderT
16+ , readerT
17+ , runReaderT
1518 , mapReaderT
19+ -- * Lifting other operations
1620 , liftCatch
1721 , liftCallCC
1822 ) where
@@ -25,7 +29,7 @@ import Control.Monad (MonadPlus)
2529import Control.Monad.Fix (MonadFix)
2630import Control.Monad.Trans.Class (MonadTrans, lift)
2731import Control.Monad.IO.Class (MonadIO)
28−import Control.Ether.Core
32+import Control.Ether.Tags (Tags)
2933
3034import qualified Control.Monad.Signatures as Sig
3135import qualified Control.Monad.Trans.Reader as Trans
@@ -36,29 +40,48 @@ import qualified Control.Monad.State.Class as Class
3640import qualified Control.Monad.Writer.Class as Class
3741import qualified Control.Monad.Error.Class as Class
3842
43+-- | The parameterizable reader monad.
44+--
45+-- Computations are functions of a shared environment.
46+--
47+-- The 'return' function ignores the environment, while '>>=' passes
48+-- the inherited environment to both subcomputations.
49+type Reader tag r = ReaderT tag r Identity
50+
51+-- | The reader monad transformer,
52+-- which adds a read-only environment to the given monad.
53+--
54+-- The 'return' function ignores the environment, while '>>=' passes
55+-- the inherited environment to both subcomputations.
3956newtype ReaderT tag r m a = ReaderT (Trans.ReaderT r m a)
4057 deriving ( Functor, Applicative, Alternative, Monad, MonadPlus
4158 , MonadFix, MonadTrans, MonadIO )
4259
4360type instance Tags (ReaderT tag r m) = tag ': Tags m
4461
45−type Reader tag r = ReaderT tag r Identity
46−
47−etherReaderT :: proxy tag -> (r -> m a) -> ReaderT tag r m a
48−etherReaderT _proxy = ReaderT . Trans.ReaderT
62+-- | Constructor for computations in the reader monad transformer.
63+readerT :: proxy tag -> (r -> m a) -> ReaderT tag r m a
64+readerT _proxy = ReaderT . Trans.ReaderT
4965
66+-- | Runs a 'Reader' and extracts the final value from it.
5067runReaderT :: proxy tag -> ReaderT tag r m a -> r -> m a
5168runReaderT _proxy (ReaderT (Trans.ReaderT f)) = f
5269
70+-- | Runs a 'Reader' and extracts the final value from it.
5371runReader :: proxy tag -> Reader tag r a -> r -> a
5472runReader proxy m r = runIdentity (runReaderT proxy m r)
5573
74+-- | Transform the computation inside a @ReaderT@.
75+--
76+-- * @'runReaderT' tag ('mapReaderT' tag f m) = f . 'runReaderT' tag m@
5677mapReaderT :: proxy tag -> (m a -> n b) -> ReaderT tag r m a -> ReaderT tag r n b
5778mapReaderT _proxy f m = ReaderT $ Trans.mapReaderT f (coerce m)
5879
80+-- | Lift a @catchE@ operation to the new monad.
5981liftCatch :: proxy tag -> Sig.Catch e m a -> Sig.Catch e (ReaderT tag r m) a
6082liftCatch _proxy f m h = ReaderT $ Trans.liftCatch f (coerce m) (coerce h)
6183
84+-- | Lift a @callCC@ operation to the new monad.
6285liftCallCC :: proxy tag -> Sig.CallCC m a b -> Sig.CallCC (ReaderT tag r m) a b
6386liftCallCC _proxy callCC f = ReaderT $ Trans.liftCallCC callCC (coerce f)
6487