[Haskell-cafe] Is it possible to change the environment (reader) in applicative style?

Olaf Klinke olf at aatal-apotheke.de
Wed Sep 12 19:28:05 UTC 2018


Neither >>= nor 'return' nor 'join' for the reader monad change the environment, hence the name "reader". Thus I'd chime in and claim it is not possible using the Applicative or Monad combinators alone. After all, the reader monad is ignorant of what type it is reading. In other words, if the type class combinators alone could do this, then you'd have a function that worked for _every_ type Env. But updating a list of (variable,value) pairs is a rather specific task. Fixing the syntax for this post:

type Reader a = Env -> a

Since your 'let' function modifies the environment, it yields a function of type

Env -> Env = Reader Env

Say that your 'let' has the type

let :: Def -> Reader Env

for a suitable type Def, e.g. Def = (String,Int).
Suppose you have implemented 'let'. We know the Applicative and Monad type class functions won't help here. But observe that (.) can be specialized to

(.) :: Reader a -> Reader Env -> Reader a

whence you can write 

eval term . let newBinding where
  term :: Term
  eval :: Term -> Reader Int
  let :: Def -> Reader Env
  newBinding :: Def

Cheers,
Olaf


More information about the Haskell-Cafe mailing list