[Haskell-cafe] Trouble with the ST monad
ryani.spam at gmail.com
Sun Dec 21 19:47:01 EST 2008
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
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 :: 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.
On Sun, Dec 21, 2008 at 4:14 PM, Andre Nathan <andre at digirati.com.br> wrote:
> 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
> Any help would be appreciated.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe