Module

Control.Monad.Reader

Package
purescript-transformers
Repository
purescript/purescript-transformers

This module defines the Reader monad.

#Reader Source

type Reader r = ReaderT r Identity

The Reader monad is a synonym for the ReaderT monad transformer, applied to the Identity monad.

#runReader Source

runReader :: forall a r. Reader r a -> r -> a

Run a computation in the Reader monad.

#mapReader Source

mapReader :: forall b a r. (a -> b) -> Reader r a -> Reader r b

Change the type of the result in a Reader monad action.

#withReader Source

withReader :: forall a r2 r1. (r2 -> r1) -> Reader r1 a -> Reader r2 a

Change the type of the context in a Reader monad action.

Re-exports from Control.Monad.Reader.Class

#MonadAsk Source

class (Monad m) <= MonadAsk r m | m -> r where

The MonadAsk type class represents those monads which support a global context that can be provided via the ask function.

An implementation is provided for ReaderT, and for other monad transformers defined in this library.

Law:

  • do { ask ; ask } = ask

Members

Instances

#MonadReader Source

class (MonadAsk r m) <= MonadReader r m | m -> r where

An extension of the MonadAsk class that introduces a function local f x that allows the value of the local context to be modified for the duration of the execution of action x.

An implementation is provided for ReaderT, and for other monad transformers defined in this library.

Laws in addition to the MonadAsk law:

  • local f ask = f <$> ask
  • local _ (pure a) = pure a
  • local f (do { a <- x ; y }) = do { a <- local f x ; local f y }

Members

  • local :: forall a. (r -> r) -> m a -> m a

Instances

#asks Source

asks :: forall a m r. MonadAsk r m => (r -> a) -> m a

Projects a value from the global context in a MonadAsk.

Re-exports from Control.Monad.Reader.Trans

#ReaderT Source

newtype ReaderT r m a

The reader monad transformer.

This monad transformer extends the base monad transformer with a global context of type r.

The MonadReader type class describes the operations supported by this monad.

Constructors

Instances

#MonadTrans Source

class MonadTrans t  where

The MonadTrans type class represents monad transformers.

A monad transformer is a type constructor of kind (* -> *) -> * -> *, which takes a Monad as its first argument, and returns another Monad.

This allows us to add additional effects to an existing monad. By iterating this process, we create monad transformer stacks, which contain all of the effects required for a particular computation.

The laws state that lift is a Monad morphism.

Laws:

  • lift (pure a) = pure a
  • lift (do { x <- m ; y }) = do { x <- lift m ; lift y }

Members

#withReaderT Source

withReaderT :: forall a m r2 r1. (r2 -> r1) -> ReaderT r1 m a -> ReaderT r2 m a

Change the type of the context in a ReaderT monad action.

#runReaderT Source

runReaderT :: forall a m r. ReaderT r m a -> (r -> m a)

Run a computation in the ReaderT monad.

#mapReaderT Source

mapReaderT :: forall b a m2 m1 r. (m1 a -> m2 b) -> ReaderT r m1 a -> ReaderT r m2 b

Change the type of the result in a ReaderT monad action.