| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Reader
Contents
Synopsis
- type Reader r = ReaderT r Identity
- runReader :: Reader r a -> r -> a
- mapReader :: (a -> b) -> Reader r a -> Reader r b
- withReader :: (r' -> r) -> Reader r a -> Reader r' a
- newtype ReaderT r (m :: k -> *) (a :: k) :: forall k. * -> (k -> *) -> k -> * = ReaderT {
- runReaderT :: r -> m a
- mapReaderT :: (m a -> n b) -> ReaderT r m a -> ReaderT r n b
- withReaderT :: (r' -> r) -> ReaderT r m a -> ReaderT r' m a
- class Monad m => MonadReader r (m :: * -> *) | m -> r where
- class (Magnified m ~ Magnified n, MonadReader b m, MonadReader a n) => Magnify (m :: * -> *) (n :: * -> *) b a | m -> b, n -> a, m a -> n, n b -> m
- magnify :: Magnify m n b a => LensLike' (Magnified m c) a b -> m c -> n c
Reader
type Reader r = ReaderT r Identity #
The parameterizable reader monad.
Computations are functions of a shared environment.
The return function ignores the environment, while >>= passes
the inherited environment to both subcomputations.
Arguments
| :: Reader r a | A |
| -> r | An initial environment. |
| -> a |
Runs a Reader and extracts the final value from it.
(The inverse of reader.)
Arguments
| :: (r' -> r) | The function to modify the environment. |
| -> Reader r a | Computation to run in the modified environment. |
| -> Reader r' a |
Execute a computation in a modified environment
(a specialization of withReaderT).
runReader(withReaderf m) =runReaderm . f
ReaderT
newtype ReaderT r (m :: k -> *) (a :: k) :: forall k. * -> (k -> *) -> k -> * #
The reader monad transformer, which adds a read-only environment to the given monad.
The return function ignores the environment, while >>= passes
the inherited environment to both subcomputations.
Constructors
| ReaderT | |
Fields
| |
Instances
mapReaderT :: (m a -> n b) -> ReaderT r m a -> ReaderT r n b #
Transform the computation inside a ReaderT.
runReaderT(mapReaderTf m) = f .runReaderTm
Arguments
| :: (r' -> r) | The function to modify the environment. |
| -> ReaderT r m a | Computation to run in the modified environment. |
| -> ReaderT r' m a |
Execute a computation in a modified environment
(a more general version of local).
runReaderT(withReaderTf m) =runReaderTm . f
MonadReader
class Monad m => MonadReader r (m :: * -> *) | m -> r where #
See examples in Control.Monad.Reader.
Note, the partially applied function type (->) r is a simple reader monad.
See the instance declaration below.
Methods
Retrieves the monad environment.
Arguments
| :: (r -> r) | The function to modify the environment. |
| -> m a |
|
| -> m a |
Executes a computation in a modified environment.
Arguments
| :: (r -> a) | The selector function to apply to the environment. |
| -> m a |
Retrieves a function of the current environment.
Instances
Magnify
class (Magnified m ~ Magnified n, MonadReader b m, MonadReader a n) => Magnify (m :: * -> *) (n :: * -> *) b a | m -> b, n -> a, m a -> n, n b -> m #
This class allows us to use magnify part of the environment, changing the environment supplied by
many different Monad transformers. Unlike zoom this can change the environment of a deeply nested Monad transformer.
Also, unlike zoom, this can be used with any valid Getter, but cannot be used with a Traversal or Fold.
Minimal complete definition
Instances
| Magnify m n b a => Magnify (IdentityT m) (IdentityT n) b a | |
| Magnify ((->) b :: * -> *) ((->) a :: * -> *) b a |
|
Defined in Control.Lens.Zoom | |
| Monad m => Magnify (ReaderT b m) (ReaderT a m) b a | |
| (Monad m, Monoid w) => Magnify (RWST b w s m) (RWST a w s m) b a | |
| (Monad m, Monoid w) => Magnify (RWST b w s m) (RWST a w s m) b a | |
magnify :: Magnify m n b a => LensLike' (Magnified m c) a b -> m c -> n c infixr 2 #
Run a monadic action in a larger environment than it was defined in, using a Getter.
This acts like local, but can in many cases change the type of the environment as well.
This is commonly used to lift actions in a simpler Reader Monad into a Monad with a larger environment type.
This can be used to edit pretty much any Monad transformer stack with an environment in it:
>>>(1,2) & magnify _2 (+1)3
>>>flip Reader.runReader (1,2) $ magnify _1 Reader.ask1
>>>flip Reader.runReader (1,2,[10..20]) $ magnify (_3._tail) Reader.ask[11,12,13,14,15,16,17,18,19,20]
magnify::Getters a -> (a -> r) -> s -> rmagnify::Monoidr =>Folds a -> (a -> r) -> s -> r
magnify::Monoidw =>Getters t ->RWSt w st c ->RWSs w st cmagnify:: (Monoidw,Monoidc) =>Folds a ->RWSa w st c ->RWSs w st c ...