Re: [Haskell-cafe] Trouble with the ST monad
On Mon, 2008-12-29 at 14:19 -0500, Ross Mellgren wrote: The problem is that you're trying to take a STMatrix from some other ST computation and freeze it in a new ST computation. The isolation between separate computations is done via the rank-2 type variable s in all those ST functions. I guess I should go and read the rank-n types page on the wiki... Try this: freezeMatrix :: (forall s . STMatrix s a) - Matrix a freezeMatrix f :: runST (freezeMatrix f) Do you know why point-free style doesn't work here even with the type annotation? Also, instead of using an array of arrays, maybe an array with (Int, Int) as the Ix might be a bit smoother? Thanks for the suggestion. It didn't occur to me that there was an Ix instance for that. Best, Andre ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Trouble with the ST monad
On Dec 29, 2008, at 3:43 PM, Andre Nathan wrote: On Mon, 2008-12-29 at 14:19 -0500, Ross Mellgren wrote: The problem is that you're trying to take a STMatrix from some other ST computation and freeze it in a new ST computation. The isolation between separate computations is done via the rank-2 type variable s in all those ST functions. I guess I should go and read the rank-n types page on the wiki... Try this: freezeMatrix :: (forall s . STMatrix s a) - Matrix a freezeMatrix f :: runST (freezeMatrix f) Do you know why point-free style doesn't work here even with the type annotation? I'm not very good with all the type-theoretic principles involved, but I think it's because higher ranked types cannot unify with lower ranked ones. I'm sure some of the more wizardly types could explain this with an example of how doing it like that would make something that should be illegal possible and the actual principles involved. (That's longhand for it makes a kind of intuitive sense to me but I can't explain it) Also, instead of using an array of arrays, maybe an array with (Int, Int) as the Ix might be a bit smoother? Thanks for the suggestion. It didn't occur to me that there was an Ix instance for that. Best, Andre -Ross ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re[2]: [Haskell-cafe] Trouble with the ST monad
Hello Andre, Monday, December 22, 2008, 4:44:34 AM, you wrote: Is there any difference between using freeze/thaw from Data.Array.MArray versus freezeSTArray/thawSTArray from GHC.Arr? portability, at least -- Best regards, Bulatmailto:bulat.zigans...@gmail.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Trouble with the ST monad
Hello, I'm trying to write a function that would take an STArray and and shuffle its elements. I'm having trouble with the ST monad, though, and couldn't find out how fix this issue. The problem happens when I use runST to extract the shuffled array from the ST monad. I'm getting the following error: Inferred type is less polymorphic than expected Quantified type variable `s' is mentioned in the environment: a :: STArray s Int a The full code is at http://hpaste.org/13240#a1 Any help would be appreciated. Thanks, Andre ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Trouble with the ST monad
The problem is that you are trying to return a mutable array out of an ST computation. This lets the mutability of the computation escape. That's what the s type variable is for; without it, runST is just unsafePerformIO. To solve your problem, you need to eliminate any references to the state from the output of runST. I suggest looking into the function freezeSTArray: From http://haskell.org/ghc/docs/latest/html/libraries/base/GHC-Arr.html#v:freezeSTArray freezeSTArray :: Ix i = STArray s i e - ST s (Array i e) (See my annotation at http://hpaste.org/13240#a2) This allows you to remove any reference to the state from the array computation. Alternatively, if you want this to be part of a larger mutable computation, you can return the result in ST; this is what the version of shuffle you have in the paste does. -- ryan On Sun, Dec 21, 2008 at 4:14 PM, Andre Nathan an...@digirati.com.br wrote: Hello, I'm trying to write a function that would take an STArray and and shuffle its elements. I'm having trouble with the ST monad, though, and couldn't find out how fix this issue. The problem happens when I use runST to extract the shuffled array from the ST monad. I'm getting the following error: Inferred type is less polymorphic than expected Quantified type variable `s' is mentioned in the environment: a :: STArray s Int a The full code is at http://hpaste.org/13240#a1 Any help would be appreciated. Thanks, Andre ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Trouble with the ST monad
On Sun, 2008-12-21 at 16:47 -0800, Ryan Ingram wrote: The problem is that you are trying to return a mutable array out of an ST computation. This lets the mutability of the computation escape. That's what the s type variable is for; without it, runST is just unsafePerformIO. Thanks! If only I knew that was the problem... It wouldn't have costed me the whole afternoon :P Is there any difference between using freeze/thaw from Data.Array.MArray versus freezeSTArray/thawSTArray from GHC.Arr? Best, Andre ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe