[Haskell-cafe] Language support for imperative code. Was: Re: monad subexpressions

Jules Bean jules at jellybean.co.uk
Thu Aug 9 05:41:30 EDT 2007

Brian Hulley wrote:
> Haskell is designed so 
> that any attempt at abstracting mutable local state will infect the 
> entire program (modulo use of a highly dangerous function whose 
> semantics is entirely unclear, depending on the vagaries of evaluation 
> strategy of the particular compiler) 

(Your email message is long and very interesting, and it does an a 
considerable injustice to take one sentence out of context, but...)

This echoes a misconception that I see here on haskell-cafe quite often.

Mutable local state *really* doesn't need to infect the whole program, 
and haskell is certainly not designed so it must.

We have all kinds of techniques for ensuring that the pure parts of your 
code can remain pure, and only the impure parts get 'infected' with an 
IO signature.

Additionally, if it's just refs, you can use ST, which is totally pure.

If it's literally just state, you can use the techniques of the State 
monad and the Reader monad: but you don't necessarily have to use them 
explicitly with those names. Sometimes it is actually simpler just to 
use the types s -> (a,s) and s -> a directly; only in certain 
circumstances is the extra plumbing useful.

Often different parts of your program have different needs; some parts 
actually need the ability to make fresh names (and so need STRefs) other 
parts merely read the state (and can use Reader techniques) and other 
parts alter it (and can use State techniques). You need some plumbing to 
connect the different parts together, but fortunately haskell has 
powerful abstraction and it's quite easy to slap together the 
higher-order functions (combinators!) to do this.


More information about the Haskell-Cafe mailing list