multistate-0.2.0.0: like mtl's ReaderT/StateT, but more than one contained value/type.

Safe HaskellNone
LanguageHaskell2010

Control.Monad.MultiReader

Contents

Description

The multi-valued version of mtl's Reader / ReaderT / MonadReader

Synopsis

MultiReaderT

newtype MultiReaderT x m a Source

A Reader transformer monad patameterized by:

  • x - The list of types constituting the environment / input (to be read),
  • m - The inner monad.

MultiReaderT corresponds to mtl's ReaderT, but can contain a heterogenous list of types.

This heterogenous list is represented using Types.Data.List, i.e:

  • Null - The empty list,
  • Cons a b - A list where a is an arbitrary type and b is the rest list.

For example,

MultiReaderT (Cons Int (Cons Bool Null)) :: (* -> *) -> (* -> *)

is a Reader wrapper containing the types [Int,Bool].

Constructors

MultiReaderT 

Fields

runMultiReaderTRaw :: StateT (HList x) m a
 

Instances

type MultiReaderTNull = MultiReaderT [] Source

A MultiReader transformer carrying an empty state.

type MultiReader x = MultiReaderT x Identity Source

A reader monad parameterized by the list of types x of the environment / input to carry.

Similar to Reader r = ReaderT r Identity

MonadMultiReader class

class Monad m => MonadMultiReader a m where Source

All methods must be defined.

The idea is: Any monad stack is instance of MonadMultiReader a, iff the stack contains a MultiReaderT x with a element of x.

Methods

mAsk Source

Arguments

:: m a

Access to a specific type in the environment.

Instances

(MonadTrans t, Monad (t m), MonadMultiReader a m) => MonadMultiReader a (t m) 
(Monad m, ContainsType a c) => MonadMultiReader a (MultiReaderT c m) 

functions

mAskRaw :: Monad m => MultiReaderT a m (HList a) Source

A raw extractor of the contained HList (i.e. the complete environment).

For a possible usecase, see withMultiReaders.

withMultiReader :: Monad m => x -> MultiReaderT (x : xs) m a -> MultiReaderT xs m a Source

Adds an element to the environment, thereby transforming a MultiReaderT carrying an environment with types (x:xs) to a a MultiReaderT with xs.

Think "Execute this computation with this additional value as environment".

withMultiReaders :: Monad m => HList xs -> MultiReaderT (Append xs ys) m a -> MultiReaderT ys m a Source

Adds a heterogenous list of elements to the environment, thereby transforming a MultiReaderT carrying an environment with values over types xs++ys to a MultiReaderT over ys.

Similar to recursively adding single values with withMultiReader.

Note that ys can be Null; in that case the return value can be evaluated further using evalMultiReaderT.

evalMultiReaderT :: Monad m => MultiReaderT [] m a -> m a Source

Evaluate a computation over an empty environment.

Because the environment is empty, it does not need to be provided.

If you want to evaluate a computation over any non-Null environment, either use

evalMultiReaderTWithInitial Source

Arguments

:: Monad m 
=> HList a

The initial state

-> MultiReaderT a m b

The computation to evaluate

-> m b 

Evaluate a reader computation with the given environment.

mapMultiReaderT :: (m (a, HList w) -> m' (a', HList w)) -> MultiReaderT w m a -> MultiReaderT w m' a' Source

Map both the return value and the environment of a computation using the given function.

Note that there is a difference to mtl's ReaderT, where it is not possible to modify the environment.