Confusing Typing Problem
Bernd Brassel
bbr at informatik.uni-kiel.de
Fri Nov 18 05:16:03 EST 2005
I am a bit confused by GHC behaviour for the following variants of a
program:
data A = A (Int -> A)
f = A g
g _ = f
h = g 'c'
ghc:
> Error:
> Couldn't match `Int' against `Char'
The problem is that a small change ommits the error:
data A = A (Int -> A)
f = A g
where
g _ = f
h = g 'c'
ghc:
> Ok, modules loaded
But I really need the functions to be top-level, thus I added type
signatures:
data A = A (Int -> A)
f :: A
f = A g
g :: a -> A
g _ = f
h :: A
h = g 'c'
ghc:
> Ok, modules loaded
Fine, but what I really need, is this:
data A = A (Int -> A)
f :: A
f = A g
g :: Ord a => a -> A
g _ = f
h :: A
h = g 'c'
> Contexts differ in length
> The signature contexts in a mutually recursive group should all be
> identical
> ...
> Couldn't match `Int' against `Char'
Again the problem does not occur for local functions:
data A = A (Int -> A)
f :: A
f = A g
where
g :: Ord a => a -> A
g _ = f
h :: A
h = g 'c'
Why is ghc treating local and global functions differently? Is there
really a difference in decidability of the Polymorphic Type Inference?
And regarding the "contexts differ in length", I read a comment in
"Typing Haskell in Haskell":
> a throw-away comment specifying that all explicit type signatures
> in a binding group must have the same context up to renaming of
variables
> [10,Section 4.5.2]. This is a syntactic restriction that can easily
> be checked prior to type checking. Our comments here, however, suggest
> that it is unnecessarily restrictive.
Why does ghc still use that unnecessary restriction?
Cheers
Bernd
More information about the Glasgow-haskell-users
mailing list