[Haskell-cafe] State monad strictness - how?
Iavor Diatchki
iavor.diatchki at gmail.com
Wed Jan 10 13:02:36 EST 2007
Hello,
> Unfortunately, the current situation is that State is only
> available as a lazy monad, and StateT is only available
> as a strict monad.
There is no such distinction in monadLib. The state transformer
inherits its behavior from the underlying monad. For example: StateT
Int IO is strict, but StatT Int Id is lazy. One way to get a strict
state monad with monadLib is like this:
import MonadLib
data Lift a = Lift { runLift :: a }
instance Monad Lift where
return x = Lift x
Lift x >>= f = f x
strict = runLift $ runStateT 2 $
do undefined
return 5
lazy = runId $ runStateT 2 $
do undefined
return 5
The difference between those two is that "strict == undefined", while
"lazy = (5,undefined)".
Unfortunately the monad "Lift" is not part of monadLib at the moment
so you have to define it on your own, like I did above, but I think
that this is a good example of when it is useful, so I will probably
add it to the next release.
-Iavor
More information about the Haskell-Cafe
mailing list