[Haskell-cafe] Trouble with the ST monad

Ryan Ingram 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

>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 <andre at 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list