infered type does not typecheck
Duncan Coutts
duncan.coutts at worcester.oxford.ac.uk
Tue Jun 29 20:32:38 EDT 2004
Hi,
This was posted to the Haskell list earlier,
http://www.haskell.org//pipermail/haskell/2004-June/014286.html
but just to record what appears to be a typechecker bug.
I have a function with this type:
buildUArray' :: (Ix i, MArray (array s) a (ST s)) =>
(i,i) -> (i -> a) -> ST s (array s i a)
Then I ask ghci (6.2.1 and 6.3 recent cvs) the type of the following
function:
buildUArray bounds f = do arr <- buildUArray' bounds f
unsafeFreeze arr
ghci says:
buildUArray :: forall a i b s array.
(MArray (array s) a (ST s), Ix i, IArray b a) =>
(i, i) -> (i -> a) -> ST s (b i a)
However if I add this annotation back in the script, ghc complains:
Could not deduce (MArray (array1 s) a (ST s))
from the context (MArray (array s) a (ST s), Ix i, IArray b a)
arising from use of `unsafeFreeze' at BuildArray.hs:25
Probable fix:
Add (MArray (array1 s) a (ST s))
to the type signature(s) for `buildUArray'
Or add an instance declaration for (MArray (array1 s) a (ST s))
In the result of a 'do' expression: unsafeFreeze arr
In the definition of `buildUArray':
buildUArray bounds f
= do
arr <- buildUArray' bounds f
unsafeFreeze arr
We can get something that checks ok if we make the type of buildUArray'
a bit more specific, (specialise 'array' tvar to 'STUArray s'):
buildUArray' :: (Ix i, MArray (STUArray s) a (ST s)) =>
(i,i) -> (i -> a) -> ST s (STUArray s i a)
Then ghc infers and happily accepts the following type for buildUArray:
buildUArray :: forall a i b s.
(MArray (STUArray s) a (ST s), Ix i, IArray b a) =>
(i, i) -> (i -> a) -> ST s (b i a)
I originally got this error message when using runST on buildUArray:
foo bounds f = runST (buildUArray bounds f)
The error message says:
Probable fix:
Add (MArray (STUArray s) a (ST s))
to the expected type of an expression
However it is not clear where to add an annotation that will properly
express the class constraint on the 's' variable (which is of course
bound by a universal quantifier not at the top level).
Duncan
More information about the Glasgow-haskell-bugs
mailing list