[Haskell-cafe] State monad strictness - how?

Udo Stenzel u.stenzel at web.de
Thu Jan 11 07:50:07 EST 2007

Ross Paterson wrote:
> This (like StateT) gives you strictness in the pair, but doesn't give
> the strictness in the state that the original poster wanted.

I think the OP wanted both.  If State is lazy in the pair, a long chain
of the form (a >>= (b >>= (c >>= ... >>= z))) gets build up and blows
the stack if it finally turns out that yes, all these steps are needed.
Worse than that, there's no way to correct this without changing the
definition of (>>=).

Laziness in the state component is annoying at times, but not as bad.
You can recover strictness by writing

	put $! x
	get >>= (put $!) . f

instead of

	put x
	modify f

provided that (>>=) is already strict in the pair.  (It gets even more
ugly if the state is a Data.Map that needs to be updated strictly, in
which Data.Map.update also doesn't work, even combined with the above

The only problem with seeing too much is that it makes you insane.
                -- Phaedrus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070111/4b9dfb58/attachment.bin

More information about the Haskell-Cafe mailing list