[jhc] Monad.ST

Isaac Dupree ml at isaac.cedarswampstudios.org
Sun Nov 15 16:53:07 EST 2009


Henning Thielemann wrote:
> 
> On Sun, 15 Nov 2009, Isaac Dupree wrote:
> 
>> well, it would be nice for such a "portable" implementation to exist...
> 
> Voila
>   http://darcs.haskell.org/packages/statethread/
> 
> It compiles, but I have not tested any program that uses it. Even with 
> simplest examples using storablevector it depends largely on fortune 
> whether they run or crash. So no chance to make reliable statements 
> about something more complex.

hmm, I looked at the code. I don't quite understand lazy ST, but I don't 
think that version you made is correct.  The writes (and reads) really 
may not be re-ordered based on which data is demanded when.  I think the 
distinctive thing about lazy ST is that the whole computation doesn't 
have to be executed before returning a result (so you can return an 
infinite list/computation, like, do{ a <- something; as <- recur; return 
(a:as) } ).  But I can't figure out how to implement the correct 
behavior... (or if I'm just confused)...

>> However, unsafePerformIO is often slower than an ST implementation has 
>> the potential to be (e.g. in GHC unsafePerformIO contains lots of 
>> safe-guards, each of which has been added after painful experience but 
>> which are not needed for ST itself).
> 
> unsafePerformIO is needed only once per ST block, namely for runST. That 
> should not be too often.

ah, right.  (unsafeInterleaveIO isn't perfect either, but, first to 
consider correctness, as above)


More information about the jhc mailing list