ST monad

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

ST monad

oleg-7

Bulat Ziganshin wrote:

the following code can't go through typechecking
> import Control.Monad.ST
> import Data.Array.ST
> main = print $ runST $
>            do arr <- newArray (1,10) 127
>               a <- readArray arr 1
>               writeArray arr 1 216
>               b <- readArray arr 1
>               return (a,b)


Indeed. The short answer: use
        runST (long expression)
rather than
        runST $ long expression

when it comes to higher-ranked functions such as runST.
A longer answer:
        http://www.haskell.org/pipermail/haskell-cafe/2004-December/008062.html

> let me know what i need to read to fix it myself
MLF (see Daan Leijen, A. Loeh, `Qualified types for MLF', ICFP05)


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

RE: ST monad

Ralf Lammel-2
... for the same reason as this one doesn't get through:

import Control.Monad.ST
import Data.Array.ST
main = print $ runST $
           do return ()

... but this one does:

import Control.Monad.ST
import Data.Array.ST
main = print $ runST (
           do return ())

it's all about rank-2 types; see SPJ's et al. paper on type inference
for these types. However, I guess that the jury is still out, say this
specific rank-2 behavior may be revised (and I also hope so).

HTH
Ralf


> -----Original Message-----
> From: [hidden email] [mailto:haskell-cafe-
> [hidden email]] On Behalf Of Bulat Ziganshin
> Sent: Tuesday, January 03, 2006 2:28 AM
> To: [hidden email]
> Subject: [Haskell-cafe] ST monad
>
> Hello
>
> the following code can't go through typechecking. can anyone help me
> to fix it or, better, let me know what i need to read to fix it
myself? :)

>
> import Control.Monad.ST
> import Data.Array.ST
> main = print $ runST $
>            do arr <- newArray (1,10) 127
>               a <- readArray arr 1
>               writeArray arr 1 216
>               b <- readArray arr 1
>               return (a,b)
>
>
> PS: error message is
>
> b.hs:4:15:
>     Inferred type is less polymorphic than expected
>       Quantified type variable `s' escapes
>       Expected type: ST s a -> b
>       Inferred type: (forall s1. ST s1 a) -> a
>     In the first argument of `($)', namely `runST'
>     In the second argument of `($)', namely
>         `runST
>          $ (do
>               arr <- newArray (1, 10) 127
>               a <- readArray arr 1
>               writeArray arr 1 216
>               b <- readArray arr 1
>               return (a, b))'
>
>
>
> --
> Best regards,
>  Bulat                          mailto:[hidden email]
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe