9

I am trying to understand when to use the reader monad, but I haven’t found a good usage example. I am pretty sure I have limited knowledge on this topic.

Consider this example code:

import Control.Monad.Reader

data Env = Env
         { eInt :: Int
         , eStr :: String
         }

calculateR :: Reader Env Int
calculateR = do
    e <- ask
    return $ eInt e

calculate :: Env -> Int
calculate = eInt

main :: IO ()
main = do
    let env = Env { eInt = 1, eStr = "hello"}
    let a = runReader calculateR env
    let b = calculate env
    print (a,b)

If I want to pass around the global Env to be able to access it from any function, should I use the reader monad for this purpose?

In comparison to just passing the Env directly to the function, is there any benefit?

If I understand correctly, both calculate and calculateR are pure (in the sense that they won't be able to make any change to env), and both have Env in their type signature telling that they might read the value form env to use in their computation.

1
  • 1
    For this simple task, the reader monad seems to make everything more cumbersome. Only in more complex examples it can save you from the hassle of passing around the environment every time. By the way, I think your monadic definition can be simplified to calculateR = asks eInt or calculateR = eInt <$> ask to reduce the syntactic overhead
    – chi
    Mar 14, 2017 at 10:14

2 Answers 2

17

The advantage comes when you want to compose two functions.

f :: a -> Reader Env b
g :: b -> Reader Env c

g <=< f

or in the do notation

\a -> do
    b <- f a
    g b

vs

f :: a -> Env -> b
g :: b -> Env -> c

\a e -> g (f a e) e

in the latter you have to keep passing the environment around by yourself, while the monad instance provides you with a composition that does it for you. The manual composition will quickly become unnecessarily verbose and complex as more functions are involved.

Reader then provides you with local too.

1
  • This demonstrates that a contrived example can actually be very helpful. +1
    – user6445533
    Jan 19, 2018 at 16:55
6

In my experience it's most useful when either:

  1. You have a bunch of functions with deeply needed call graphs, and some stuff deep on the call graph needs the environment but lots of intermediate functions don't. Then the reader monad handles all the plumbing for you. Especially when you're using a monad transformer stack anyway (especially especially with a type or newtype alias for your stack) so you were already writing in monadic style; in that case you can probably realise late that you need the environment, change the definition of the monad stack, change the entry point to pass the environment, change the use sites to refer to it, and not have to touch the code of any intermediate functions (maybe not even their types if you had a name for the stack).

  2. You're providing an interface where lots of client code will pass functions to you, and you want them to have the ability to access the environment, but expect that many of them won't actually need it. Again, the benefits are greater when you already had a monad transformer stack you can just bolt a reader into; existing client code was already in monadic style and may work unchanged by just ignoring the environment, while new functions can be written that use it.

But for anything small enough to fit into a stack overflow post, ordinary functions are almost always more appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.