[Haskell-cafe] Help in understanding a type error involving forall and class constraints

Duncan Coutts duncan.coutts at worcester.oxford.ac.uk
Wed Jun 30 08:14:25 EDT 2004


On Wed, 2004-06-30 at 12:54, Duncan Coutts wrote:
> To restate the question for Haskell-Cafe readers:
>         Is it possible to return an arbitrary unboxed array that was
>         constructed in the ST monad (as an STUArray)?
>         
>         The issue is that you end up with a MArray class constraint that
>         involves the state thread's 's' parameter, but this type
>         variable gets 'hidden' by runST which universally quantifies
>         over it. 	runST :: forall a. (forall s. ST s a) -> a

I'm inclined to believe it is impossible. The author of ghc's mutable
array libraries (probably Simon M) notes in a comment in the code:

-- I don't know how to write a single rule for listUArrayST, because
-- the type looks like constrained over 's', which runST doesn't
-- like. In fact all MArray (STUArray s) instances are polymorphic
-- wrt. 's', but runST can't know that.

For that reason the modules contains rewrite rules for each unboxable
type rather than a single polymorphic rule:

{-# RULES
"listArray/UArray/Bool"      listArray = \lu (es :: [Bool])        ->
    runST (listUArrayST lu es >>= unsafeFreezeSTUArray)
"listArray/UArray/Char"      listArray = \lu (es :: [Char])        ->
    runST (listUArrayST lu es >>= unsafeFreezeSTUArray)
"listArray/UArray/Int"       listArray = \lu (es :: [Int])         ->
    runST (listUArrayST lu es >>= unsafeFreezeSTUArray)
etc...

Duncan



More information about the Haskell-Cafe mailing list