[Haskell-cafe] A tale of Project Euler [ST]
Andrew Coppin
andrewcoppin at btinternet.com
Fri Nov 30 14:36:38 EST 2007
Bulat Ziganshin wrote:
> 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
> transparent
>
> 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
>
Mmm, so basically it's rank-2 types I don't understand. ;-)
Well anyway, given the multiple replies I've had, I now have some idea
how this stuff works...
More information about the Haskell-Cafe
mailing list