[Haskell-cafe] I don't understand how ST works

Silvio Frischknecht silvio.frischi at gmail.com
Fri Jun 8 02:47:56 CEST 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> Now comes my question: in the impure values there is always that
> "s". I was thinking that the whole structure should have s as a
> parameter:

Yes

> 
>> data MList s = MList { mlVec :: MVector s Move, mlNextPh :: MList
>> ->
> ST s (Maybe (MList s)) }

you probably meant:

    data MList s = MList { ... , mlNextPh :: Mlist s -> ... }

Now I'm not sure about your exact problem since the following compiles
for me.

    import Data.Vector
    import Data.Vector.Mutable
    import Control.Monad.ST

    type Move = ()
    data MList s = MList {
        mvVec :: MVector s Move,
        mlNextPh :: MList s -> ST s (Maybe (MList s)) }

    splitMove :: MList s -> ST s (Maybe (Move, MList s))
    splitMove ml = do
        m <- unsafeRead (mvVec ml) 0
        undefined

Something you always have to watch out for when dealing with ST is not
to return something that depends on s in the last statement (the one
you use runST on). In other words, if you want to return a vector you
have to freeze it, so it's not mutable anymore.

If you still can't figure it out paste some complete example that
doesn't work.

silvio
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJP0Uu5AAoJEDLsP+zrbatWKFoP+wYdmAwO3aKPIibOydDwPlcu
GmwWLCDoylhBsA1swskPGZTlBevFFeS0kzDMAhZ2dtR18HHf0TVLFCL6mljgQGhu
YLsT8a2Y5eepPd7CC0wHD7qLH0t6ln/urRhWNnVEGryVHmsIDCBzuKBzopshaaOm
8awNeEbmZApki193r/YJ21Zsxidx4N2tSGCd712ka9Wr7l19RzBukonTy/wNCTtN
1sj54xCKap3MpnQe4L68nep6WjMovnwn5ucPWlouPP5N99/2umiEPDwX3y9moD/Q
VkbYe0HzZtvSX7JJaDM/hJ2dWKHsg5CLdO/aW7Uz3HttTy0/FmvwhxaNAzkmQimw
L4uakvyuw1EJuSAwB5XRfeUL6LDpka165jb8V8Iy2gjYg3aGMwf9VVmObjEAA93s
nvQd+iH1lDe38cbfz8dfQdTakDVYtFNnYL+kXIF1Z7DiS25IThtS0RJRH//E+CZg
MpOtW2LBfa3vwP9NqVryGTAhWFtWHXOtpXfCXOa0+pQNn1zHkTXtIDJ4XoT5qkmd
6GDwFyGfkPZO01qNMoXwj/wBz/eaSa4Vj0qb73jNdNH2MbJ13Ws9Jlp4jwcxbG4a
m/fYV0/6LmPEiV8H9+4cG8nhUP2ie2DJqo8tzdjiaZ7C7TEym9jd6gsljMQ8qiAG
Q7aAmMed/DBlY/Anh2xY
=X9CL
-----END PGP SIGNATURE-----



More information about the Haskell-Cafe mailing list