[Haskell-cafe] Re: Monad.Reader with updates

Jules Bean jules at jellybean.co.uk
Thu Nov 6 07:40:25 EST 2008

Mauricio wrote:
>>> Is there some abstraction in current ghc library
>>> that implements something like Reader, but where
>>> the value of the environment is updated at every
>>> "step"?
>> do-it-yourself? you can start from reader definition and add what you
>> need. you just need to make "initial state" consisting from state
>> itself and update function so `run` will have just one initialization 
>> argument
> Sure. I've done a few versions, trying to change
> the way (>>=) is defined, and learned a lot with
> that. But I wanted to know if there's already the
> "right way to do it" instead of my "newbie way to
> do it" :)

It doesn't quite make sense, because one "step" isn't well defined.

How many "steps" is "return (f x)" ? how about "return x >>= \y -> 
return (f y)" ?

Because the monad laws guarantee those two things should be the same, 
and yet the first is zero steps and the second is one step, going by the 
crude "counting >>=s" method I'm guess you were thinking of.

So I think you'd have to make the steps explicit.

You could do this with a custom version of (>>) and (>>=) which 
automatically do a step, for example.


advance :: m () -- your primitive which changes the environment

a >>* b = a >> advance >> b
a >>*= f = do { r <- a; advance; f r }

Does that help?


More information about the Haskell-Cafe mailing list