Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the Reader monad

I'm reading PureScript by Example and got to the part introducing the Reader monad. The example goes like this:

createUser :: Reader Permissions (Maybe User)
createUser = do
  permissions <- ask
  if hasPermission "admin" permissions
    then map Just newUser
    else pure Nothing

The confusing part for me is the ask function. The signature is:

ask   :: forall r. Reader r r

It appears as if it creates a Reader out of thin air 😐

When I was reading about the State monad, it had the same concept with its get function. And the text explained:

the state is implemented as a function argument hidden by the State monad’s data constructor, so there is no explicit reference to pass around.

I'm guessing this is the key, and the same thing is happening here with the Reader, but I don't understand how it works...

When the above example is run via runReader, how does the provided value suddenly appear as a result of ask? The Haskell docs for ask say: Retrieves the monad environment. But my confusion is from where? The way I see it, a value gets passed to runReader, gets stored somewhere, and to get it - you call ask... but that makes no sense.

While the example is PureScript, I'm guessing any Haskell-literate person would also be able to answer, hence the Haskell tag.

like image 824
kaqqao Avatar asked Oct 13 '17 21:10

kaqqao


2 Answers

I don't have a PureScript environment around currently, so I'll try to answer from a Haskell perspective, and hope it helps.

A Reader is really only a 'wrapper' around a function, so when you get a Reader r r, you really only get a reader from r to r; in other words, a function r -> r.

You can summon functions out of thin air, because, if you're a Platonist, I suppose they always exist...

When you use do notation, you're 'inside the monad', so the context r is implicit. In other words, you call a function that returns the r value, and when you use the <- arrow, you simply get that context.

like image 106
Mark Seemann Avatar answered Nov 16 '22 18:11

Mark Seemann


You can convince yourself that it works by performing a few substitutions. First look at the signature of createUser. Let's "unroll" the definition of Reader:

createUser :: Reader Permissions (Maybe User)
{- definition of Reader -}
createUser :: ReaderT Permissions Identity (Maybe User)

The ReaderT type only has one data constructor: ReaderT (r -> m a), which means createUser is a term that evaluates to a value of type ReaderT (Permissions -> Identity (Maybe User)). As you can see, it is just a function tagged with ReaderT. It does not have to create anything out of thin air, but will receive the value of type Permissions when that function is called.

Now let's look at the line you are having trouble with. You know that the do notation is just syntactic sugar, and the expression:

do permissions <- ask
   if hasPermission "admin" permissions
     then map Just newUser
     else pure Nothing

desugars to

ask >>= \permissions -> 
  if hasPermission "admin" permissions
  then map Just newUser
  else pure Nothing

To understand what this does, you will have to lookup the definition of ask, >>= and pure for ReaderT. Let's perform another round of substitutions:

ask >>= \permissions -> ...
{- definition of ask for ReaderT -}
ReaderT pure >>= \permissions -> ...
{- definition of >>= for ReaderT -}
ReaderT \r ->
  pure r >>= \a -> case (\permissions -> ...) a of ReaderT f -> f r
{- function application -}
ReaderT \r ->
  pure r >>= \a -> 
    case (if hasPermission "admin" a
          then map Just newUser
          else pure Nothing) of ReaderT f -> f r
{- definition of pure for Identity -}
ReaderT \r ->
  Identity r >>= \a -> 
    case (if hasPermission "admin" a
          then map Just newUser
          else pure Nothing) of ReaderT f -> f r
{- definition of >>= for Identity -}
ReaderT \r ->
  (\a -> 
    case (if hasPermission "admin" a
          then map Just newUser
          else pure Nothing) of ReaderT f -> f r) r
{- function application -}
ReaderT \r ->
  case (if hasPermission "admin" r
        then map Just newUser
        else pure Nothing) of ReaderT f -> f r

As you can see, createUser is clearly just a function wrapped by ReaderT that threads a value (the "environment") through your expressions. runReader unwraps the function and calls it with the provided argument:

runReader :: forall r a. Reader r a -> r -> a
runReader (ReaderT f) r = f r
like image 29
Regis Kuckaertz Avatar answered Nov 16 '22 18:11

Regis Kuckaertz