concept Reader in category haskell

appears as: Reader, Reader
Haskell in Depth

This is an excerpt from Manning's book Haskell in Depth.

  • The Reader monad
  • The idea of the Reader monad is to pass a configuration implicitly (meaning we don’t have to do that explicitly) and provide an interface to consult it whenever required. An implicit configuration passing is implemented by the Monad instance for Reader. The interface is described by the following type class:

    class Monad m => MonadReader r m | m -> r where
        ask    :: m r
        local  :: (r -> r) -> m a -> m a
        reader :: (r -> a) -> m a

    Note the head of the type class definition: we define multiparametric type class MonadReader r m where r refers to the type of configuration (something that can be read inside the computation) and m refers to some monad. The Reader monad implements an instance of the MonadReader type class, there are other implementations as well.