Grokking the Reader Monad: a Visual Explanation

I’m still figuring out the best way to present images on my blog.

data flow flow flow

Definition

data Reader config result = Reader (config -> result)

runReader :: Reader config result -> config -> result
runReader (Reader f) config =
  f config

instance Monad (Reader config) where
  return x         = Reader $ \config -> x
  (Reader f) >>= g = Reader $ \config ->
      let x = f config
          y = runReader (g x) config
      in  y

ask :: Reader config config
ask = Reader $ \config -> config