[Haskell-cafe] State monad strictness - how?

Yitzchak Gale gale at sefer.org
Wed Jan 10 04:02:25 EST 2007

Dean Herington wrote:
> I can't seem to figure out how to achieve strictness
> in the context of the State monad.

Unfortunately, the current situation is that State is only
available as a lazy monad, and StateT is only available
as a strict monad.

It seems to me that this should clearly be considered
a serious bug in the library. It has been reported on
numerous occasions over the years, but it has still
not been fixed.

At the very least, the two should be consistent. I
would much prefer for them both to be lazy.
I have written a lot of code that depends on that;
it is the natural assumption in Haskell that everything
is lazy by default, except seq, IO, and their friends.

The obvious solution would be to have available
both a lazy and a strict version of each monad: State,
State', StateT, and State'T (or some such), with functions to
convert between them. It is trivial to implement that in
the current library.

If someone can come up with a more elegant solution
right away, that would be great. (Iavor - do you have
a solution?)

Otherwise, I think we have waited long enough. Let's
implement the simple fix. This bug is a major
inconvenience to users of this library.

> (try 1000000) overflows the stack.

In the current situation, you can use

>   where final = runIdentity $ execStateT prog (0::Int)
> tick :: (Num a, MonadState a m) => m a


More information about the Haskell-Cafe mailing list