Note to self on constructing a monad transformer. In a way this follows on from the earlier post Applicatives compose, monads do not. Literate Haskell source for this post is available here: https://github.com/carlohamalainen/playground/tree/master/haskell/transformers.

> {-# LANGUAGE ScopedTypeVariables, InstanceSigs, MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances #-}
>
>
>
> hole = undefined
> data Hole = Hole


A Reader is a data type that encapsulates an environment. The runReader function takes an environment (a Reader) and runs it, producing the result of type a.

> newtype Reader r a = Reader { runReader :: r -> a }


Note that with record syntax, the type of runReader is actually

runReader :: Reader r a -> (r -> a)


Reader that increments its environment value, returning the same type:

> reader1 :: Num r => Reader r r
> reader1 = Reader $r -> r + 1  Reader that converts its environment value into a string: > reader2 :: Show r => Reader r String > reader2 = Reader$ r -> "reader2: " ++ (show r)


> ghci> runReader reader1 100
> 101


There’s nothing magic about runReader; it is just taking the function out of the data type. We can do it manually ourselves:

> runReader' :: Reader r a -> (r -> a)

> ghci> runReader' reader1 100
> 101


> instance Monad (Reader r) where
>     return :: a -> Reader r a
>     return a = Reader $_ -> a > > (>>=) :: forall a b. Reader r a -> (a -> Reader r b) -> Reader r b > m >>= k = Reader$ r -> runReader (k (runReader m r :: a) :: Reader r b) r


The definition of >>= is relatively easy to work out using hole driven development.

Example usage:

> eg1 :: Reader Int String
> eg1 = Reader id >>= e -> return $"hey, " ++ show e  Or in the more readable do notation: > eg1' :: Reader Int String > eg1' = do e return$ "hey, " ++ show e

> ghci> runReader eg1' 100
> "hey, 100"


Note that we use id to produce a Reader that just passes its environment argument along as the output. See readerAsk later for the equivalent situation which uses return for MonadReader.

Since [] is an instance of Monad we can also do things like this:

> eg1'' :: Reader Int String
> eg1'' = do e  [] Int)
>            return $"hey, " ++ show e  > ghci> runReader eg1'' 100 > "hey, [100]"  ## Reader Transformer We’d like to use the Reader in conjunction with other monads, for example running IO actions but having access to the reader’s environment. To do this we create a transformer, which we’ll call ReaderT: > newtype ReaderT r m a = ReaderT { runReaderT :: r -> m a }  The r parameter is the reader environment as before, m is the monad (for example IO), and a is the result type, as before. Again, note the actual type of runReaderT: > runReaderT :: ReaderT r m a -> (r -> m a)  It takes a ReaderT and provides us with a function that takes a reader environment of type r and produces a monadic value. Following from the Monad instance declaration for Reader it is straightforward to write the definition for ReaderT. > instance Monad m => Monad (ReaderT r m) where > return a = ReaderT$ r -> return a
>
>     (>>=) :: forall a b. ReaderT r m a -> (a -> ReaderT r m b) -> ReaderT r m b
>     m >>= k  = ReaderT $r -> do let a' = runReaderT m r :: m a > a'' runReaderT (k a'') r :: m b  With a ReaderT as the outer monad, we would like to “lift” a monadic computation into the reader. A monadic action doesn’t know anything about the reader environment, so to lift the monadic value we just create a ReaderT with a function that ignores the environment: > liftReaderT :: m a -> ReaderT r m a > liftReaderT m = ReaderT (_ -> m)  Example usage: > egLift :: ReaderT Int IO () > egLift = do e IO Int) -- This is similar to "Reader id" earlier. > liftReaderT$ print "boo"
>             liftReaderT $print$ "value of e: " ++ show e


Note the type of return on the first line of egLift. In this context, return :: a -> m a is the equivalent of id :: a -> a from the earlier Reader example.

> ghci> runReaderT egLift 100
> "boo"
> "value of e: 100"


More generally, let’s name the “ask” function:

> readerAsk :: (Monad m) => ReaderT r m r


If we want to modify the environment, we use withReaderT which takes as its first parameter a function to modify the environment. Note that the result is of type ReaderT r’ m a so the function is of type r’ -> r which modifies the supplied reader of type ReaderT r m a.

> withReaderT :: (r' -> r) -> ReaderT r m a -> ReaderT r' m a
> withReaderT f rt = ReaderT $(runReaderT rt) . f  Lastly, it is convenient to apply a function to the current environment. > readerReader :: Monad m => (r -> a) -> ReaderT r m a > readerReader f = ReaderT$ r -> return (f r)


This is almost the same as readerAsk except that we create a reader that returns f r instead of f. In other words:

> readerAsk' :: (Monad m) => ReaderT r m r


> class (Monad m) => MonadReader r m | m -> r where
>     -- Retrieve the monad environment.
>
>     -- Execute the computation in the modified environment.
>     local :: (r -> r) -> m a -> m a
>
>     -- Retrieves a function of the current environment.
>     reader :: (r -> a) -> m a


An instance declaration for our ReaderT type:

> instance Monad m => MonadReader r (ReaderT r m) where
>     local  = withReaderT  :: (r -> r) -> ReaderT r m a -> ReaderT r m a


Now we can write fairly succinct code as follows. Use the IO monad as the inner monad in a ReaderT, with an Int environment and String result type.

> eg2 :: ReaderT Int IO String
> eg2 = do
>     -- Get the reader environment.
>     e
>     -- Run an IO action; we have to use liftReaderT since we are currently
>     liftReaderT $print$ "I'm in the eg2 function and the environment is: " ++ (show e)
>
>     -- Final return value, a string.
>     return $"returned value: " ++ show e  > ghci> result "I'm in the eg2 function and the environment is: 100" > > ghci> result > "returned value: 100"  All of the above is available from Control.Monad.Reader and Control.Monad.Trans. ## StateT, ReaderT, and ask The State monad encapsulates a modifiable state. It has a transformer StateT as one would expect. Yet we are able to call ask inside a StateT monad. Here is an example (the raw code is here): > import Control.Monad.Reader > import Control.Monad.State > > inside0 :: ReaderT String IO Float > inside0 = do > e > liftIO$ putStrLn $"inside0, e: " ++ show e > > return 1.23 > > inside1 :: StateT [Int] (ReaderT String IO) Float > inside1 = do > e s > liftIO$ putStrLn $"inside1, e: " ++ show e > liftIO$ putStrLn $"inside1, s: " ++ show s > > put [1, 1, 1] > > return 1.23 > > run0 :: IO () > run0 = do let layer1 = runReaderT inside0 "reader environment, hi" > > result > print$ "result: " ++ show result
>
> run1 :: IO ()
> run1 = do let layer1 = runStateT inside1 [0]
>
>           (result, finalState)
>           print $"final state: " ++ show finalState > > print$ "result: " ++ show result


The function inside0 has the IO monad nested inside a Reader, while inside1 has a StateT with the ReaderT inside. Yet in both we can write e <- ask.

Inspecting the types using ghcmod-vim we find that

>   -- in inside0
>   e
>   -- in inside1


so there must be a type class that provides the ask function for StateT.

First, inspect the type of ask using ghci (here we are using the definitions from Control.Monad.Reader and Control.Monad.State, not the implementation in this file).

ghci> :t ask


ghci> :i StateT
(lots of stuff)
(lots of stuff)


> instance MonadReader r m => MonadReader r (Lazy.StateT s m) where
>     local = Lazy.mapStateT . local


The lift function comes from Monad.Trans.Class, and looking there we see:

> class MonadTrans t where
>     -- | Lift a computation from the argument monad to the constructed monad.
>     lift :: Monad m => m a -> t m a


So actually we are after the MonadTrans type class. Again looking at :i on StateT we see:

ghci> :i StateT
(lots of stuff)
-- Defined in Control.Monad.Trans.State.Lazy'
(lots of stuff)


So off we go to Control.Monad.Trans.State.Lazy where we finally get the answer:

> instance MonadTrans (StateT s) where
>     lift m = StateT $s -> do > a return (a, s)  This shows that for StateT, the lift function takes a monadic action and produces a state transformer that takes the current state, runs the action, and returns the result of the action along with the unmodified state. This makes sense in that the underlying action should not modify the state. (There are some laws that monad transformers must satisfy.) If we did not have the MonadTrans type class then we would have to embed the ask call manually: > inside1' :: StateT [Int] (ReaderT String IO) Float > inside1' = do > e do a return (a, s) > > s > liftIO$ putStrLn $"inside1, e: " ++ show e > liftIO$ putStrLn $"inside1, s: " ++ show s > > put [1, 1, 1] > > return 1.23  Obviously this is laborious and error-prone. In this case, Haskell’s type class system lets us implement a few classes so that ask, get, put, etc, can be used seamlessly no matter which monad transformer we are in. The downside is that reading Haskell code can be nontrivial. In our case we had to follow a trail through a few files to see where ask was actually implemented, and finding the right definition relied on us being able to infer the correct types of certain sub-expressions. Personally I am finding more and more that plain vim and ghci does not cut it for Haskell development, and something richer like ghcmod-vim is a real necessity. Shameless self plug: ghc-imported-from is also very useful 🙂 Archived Comments Date: 2015-07-17 16:38:33.037482 UTC Author: Marco Faustinellli Quite an interesting page. My compliments. Here’s my question to you: would you say that the ReaderT must always be the outer layer of the transformers’ stack? As a learning exercise I have implemented a transformers library (https://github.com/Muzietto/transformerz) and I am experimenting with the mutual position of ErrorT and ReaderT (which in my case are called respectively ET and RT). As you can see at row 122 of https://github.com/Muzietto/transformerz/blob/master/haskell/nilsson/Nilsson_03.hs moving ReaderT inside the monad stack causes trouble with the lifting of local (really ugly code with loss of error control functionalities). I wonder whether ReaderT is free to move, because all examples I know put it always at the outer layer. Date: 2015-07-18 10:58:58.401802 UTC Author: Carlo Hi Marco, You’re right in that it doesn’t really matter where the Reader goes, but it changes how the rest of the stack behaves as you noticed with error handling. Here’s a small example based on your file Nilsson_03.hs: module Main where import Control.Monad.Except import Control.Monad.Reader -- IO then Except then Reader: type IoExceptReader = ReaderT Int (ExceptT String IO) dostuff :: ReaderT Int (ExceptT String IO) Int dostuff = do -- Can use ask here (ReaderT): r <- ask if r == 42 -- Can also use throwError here (ExceptT) then throwError "Got 42, oh no" else return$ r + 1000


In dostuff we can use ask and throwError, and using local is easy because the Reader is at the top level:

blah :: IoExceptReader Int
blah = local (+1) dostuff


In contrast, if we put the Except at the top level then the inner computation readerThing runs in a Reader with
no Except capabilities, while the top level blah’ has Reader and Except.

-- IO then Reader then Error:

-- Can only use ask here, no ExceptT stuff, unlike dostuff.
r  ExceptT String (ReaderT Int IO) Int
thisLift = lift


Using each version:

main :: IO ()
main = do
e  putStrLn $"Error!? " ++ err Right x -> putStrLn$ "Got value: " ++ show x

e'  putStrLn $"Error!? " ++ err' Right x' -> putStrLn$ "Got value: " ++ show x'

print ()