Is it possible to change the environment (reader) in applicative style?

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Is it possible to change the environment (reader) in applicative style?

Olaf Klinke
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

Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
Only members subscribed via the mailman list are allowed to post.