[Haskell-cafe] A tale of Project Euler

Bulat Ziganshin bulat.ziganshin at gmail.com
Fri Nov 30 01:05:30 EST 2007

Hello Andrew,

Friday, November 30, 2007, 12:10:16 AM, you wrote:

>>> I don't understand the ST monad.

>  From what I can tell, it's not definable without using strange language
> extensions. (I don't really like using things where it's unclear why it
> works.)

this extension used only to guarantee referential transparency, so you
don't need to understand it to use ST

if you still want, it's not that hard - rank-2 forall type used here
to guarantee that code executed by runST is compatible with *any*
return type which makes it impossible to return "innards" of this
computation and therefore forces your code to be referential

runST may be defined without rank-2 type. it doesn't change anything in
its low-level working (this rank-2 type is just empty value, like
RealWorld), but allows you to break referential transparency:

main = do a <- runST (do a <- newArray
                         return a)
          x <- runST (do x <- readArray a
                         return x)

low-level implementation of all ST-bound operations is just direct
call to equivalent IO operation:

newSTArray = unsafeIOtoST newIOArray
readSTArray = unsafeIOtoST readIOArray

where unsafeIOtoST - just code-less cast operation

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list