[Haskell-cafe] Custom monad using ST

Yves Parès limestrael at gmail.com
Thu Mar 10 10:23:06 CET 2011


> In practice if you want to actually _use_ ST you'll find you'll need to
let the world escape into your type. Otherwise you won't be able to create
and pass around any STRefs or arrays and use them later.
> The universal quantification inside of MyST's definition will keep you
from holding on to them.

Okay, what you are saying is that two MyST action declared separately will
not be compatible, right?


I have another problem. One of my goal is to be able to alter an STRef when
it is accessed. To do so, I use the following type:
STRef s (ST s a).
So the actual variable 'a' contained by my STRef is wrapped inside an ST
action which goal is to modify the STRef and then return the value of type
'a'.
My problem is that the STRef is not modfied, it always returns the same
value.

Example of this with IORefs, it is simpler to test:

selfAlteringRef :: Int -> IO (IORef (IO Int))
selfAlteringRef init = mfix $ \ref ->
  newIORef $ do
    writeIORef ref (return 0)
    return init


2011/3/10 Edward Kmett <ekmett at gmail.com>

> On Wed, Mar 9, 2011 at 6:21 PM, Yves Parès <limestrael at gmail.com> wrote:
>
>> Well, I want to hide the fact that I'm using ST, so if I can hide the
>> existential type 's' it is better.
>>
>
> In practice if you want to actually _use_ ST you'll find you'll need to let
> the world escape into your type. Otherwise you won't be able to create and
> pass around any STRefs or arrays and use them later. The universal
> quantification inside of MyST's definition will keep you from holding on to
> them.
>
> BTW, does someone know why the ST default implementation (the one exposed
>> by Control.Monad.ST) is strict, whereas those of State et Writer are
>> lazy?
>>
>
> Mostly because of the principle of least surprise. It makes it act more
> like IO.
>
> -Edward
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110310/2d67e7ae/attachment.htm>


More information about the Haskell-Cafe mailing list