Re: [Haskell-cafe] REALLY simple STRef examples

Hi,
The short answer: use runST (long expression) rather than runST $ long expression
when it comes to higher-ranked functions such as runST.
I suppose the same holds for runSTUArray, right? But this still gives me that same error, about being less polymorphic than expected. (+=) x y = let updateX i = do xi <- readArray x i writeArray x i (xi + y!i) in sequence_ . map updateX $ indices x sumArrays [] = error "Can't apply sumArrays to an empty list" sumArrays (x:xs) = runSTUArray result where result = do x0 <- thaw x mapM_ (x0 +=) xs x0 -- Chad Scherrer "Time flies like an arrow; fruit flies like a banana" -- Groucho Marx

Hello Chad, Thursday, July 20, 2006, 9:38:43 PM, you wrote:
I suppose the same holds for runSTUArray, right? But this still gives me that same error, about being less polymorphic than expected.
there is well-known problem with that _unboxed_ arrays aren't polymorphic. Oleg Kiselyov proposed solution to that problem that i implemented in ArrayRef library (read http://www.haskell.org/pipermail/haskell-cafe/2004-July/006400.html and http://haskell.org/haskellwiki/Library/ArrayRef) this code compiles (i've also fixed pair of other problems): import Data.ArrayBZ.IArray import Data.ArrayBZ.ST (+=) x y = let updateX i = do xi <- readArray x i writeArray x i (xi + y!i) in sequence_ . map updateX $ indices x sumArrays [] = error "Can't apply sumArrays to an empty list" sumArrays (x:xs) = runSTUArray (result x) where result x = do x0 <- thaw x mapM_ (x0 +=) xs return x0 ps: you successfully going through all the standard Haskell troubles in this area :) seems that making FAQ about using ST monad will be a good idea :) -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Ok, I see now why the return is necessary. For now I'll switch to boxed arrays until I get the rest of this down better. But why should this...
sumArrays [] = error "Can't apply sumArrays to an empty list" sumArrays (x:xs) = runSTArray (result x) where result x = do x0 <- thaw x mapM_ (x0 +=) xs return x0
work differently than this...
sumArrays' [] = error "Can't apply sumArrays to an empty list" sumArrays' (x:xs) = runSTArray result' where result' = do x0 <- thaw x mapM_ (x0 +=) xs return x0
Are the types of (result x) and result' not exactly the same? -- Chad Scherrer "Time flies like an arrow; fruit flies like a banana" -- Groucho Marx

Chad Scherrer wrote:
But why should this...
sumArrays [] = error "Can't apply sumArrays to an empty list" sumArrays (x:xs) = runSTArray (result x) where result x = do x0 <- thaw x mapM_ (x0 +=) xs return x0
work differently than this...
sumArrays' [] = error "Can't apply sumArrays to an empty list" sumArrays' (x:xs) = runSTArray result' where result' = do x0 <- thaw x mapM_ (x0 +=) xs return x0
Are the types of (result x) and result' not exactly the same?
It's the monmorphism restriction, again. Because result' doesn't look like a function, a monomorphic type is inferred for it. runST[U]Array of course doesn't want a monomorphic type. It's got nothing to do with boxed vs. unboxed arrays (I think, I can't be bothered to test it right now). There are at least four ways out: - make result' a function, either as in the first example above or by supplying a dummy () argument - declare the correct polymorphic type for result' - inline result' - (GHC only) compile with -fno-monomorphism-restriction Yes, it's a bit cumbersome. Imperative code is supposed to be cumbersome, after all. :) Udo. -- They seem to have learned the habit of cowering before authority even when not actually threatened. How very nice for authority. I decided not to learn this particular lesson. -- Richard Stallman

| ps: you successfully going through all the standard Haskell troubles in | this area :) seems that making FAQ about using ST monad will be a | good idea :) Indeed. If someone would like to start one, a good place for it would be GHC's collaborative-documentation Wiki http://haskell.org/haskellwiki/GHC#Collaborative_documentation Simon

Hello Simon, Friday, July 21, 2006, 7:46:15 AM, you wrote:
| ps: you successfully going through all the standard Haskell troubles in | this area :) seems that making FAQ about using ST monad will be a | good idea :)
Indeed. If someone would like to start one, a good place for it would be GHC's collaborative-documentation Wiki http://haskell.org/haskellwiki/GHC#Collaborative_documentation
they are not ghc-specific, afaik -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

| > | ps: you successfully going through all the standard Haskell troubles | > in | > | this area :) seems that making FAQ about using ST monad will be a | > | good idea :) | | > Indeed. If someone would like to start one, a good place for it would be | > GHC's collaborative-documentation Wiki | > http://haskell.org/haskellwiki/GHC#Collaborative_documentation | | they are not ghc-specific, afaik Some aspects are -- e.g. the behaviour of higher-rank types. But by all means attach it somewhere else S
participants (4)
-
Bulat Ziganshin
-
Chad Scherrer
-
Simon Peyton-Jones
-
Udo Stenzel