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

Duncan Coutts duncan.coutts at worcester.oxford.ac.uk
Tue Jun 29 18:56:03 EDT 2004


On Tue, 2004-06-29 at 23:38, MR K P SCHUPKE wrote:
> Try this:
> 
> distString :: String -> String -> Int
> distString s0 s1 = let a = runST (difST s0 s1)
>    in a!(1,1)
> 
> difST :: MArray (STUArray s) Int (ST s) => String -> String -> ST s (UArray (Int,Int) Int)
> difST s0 s1 = do
>    b@(_,br) <- return $ (\x1 y1 -> ((0,0),(x1,y1))) (length s0) (length s1)
>    d <- newArray b 0 :: ST s (STUArray s (Int,Int) Int)
>    mdiff d s0 s1
>    -- readArray d br
>    unsafeFreeze d

However if we generalise a bit from an array of Int to an array of any
(suitable) type:

difST :: MArray (STUArray s) a (ST s) => String -> String -> ST s (UArray (Int,Int) a)

(I'm not claiming this generalisation makes sense in this example!)

Then we get the problem when we use difST in distString.

 Could not deduce (MArray (STUArray s) a (ST s)) from the context ()
      arising from use of `difST' at BuildArray.hs:55
    Probable fix:
	Add (MArray (STUArray s) a (ST s))
	to the expected type of an expression
	Or add an instance declaration for (MArray (STUArray s) a (ST s))
    In the first argument of `runST', namely `(difST s0 s1)'
    In the definition of `a': a = runST (difST s0 s1)
    In the definition of `distString':
	distString s0 s1 = let a = runST (difST s0 s1) in a ! (1, 1)

Where as there was an instance
	MArray (STUArray s) Int (ST s)
there is of course no instance for an arbitrary 'a' (only instances for
many specific unboxable types). So because there is no instance in scope
we gain an extra class constraint, but I cannot find the right place to
add an annotation.

Duncan



More information about the Haskell mailing list