Typing problems with polymorphic recursion and typeclasses
[Commenting of the problem in the message "GHC doesn't like its own type?", Iavor Diatchki wrote]
i remeber some time ago levent erkok run intro a simillar problem. take a look at this post: http://www.mail-archive.com/haskell@haskell.org/msg06754.html
The problems seem different. However, Levent Erkok's problem as well as the problem in the follow-up message to categorical maxima point out that there is something else goes on besides the explicit typing. Let us use Levent Erkok's problem as it is simpler:
class X a where u :: a b -> a b
f :: X a => b -> a b f = f
g :: X a => a b -> b g = g
-- h :: X a => b -> a c h b = f (g (h b))
This file loads and checks both in GHC and Hugs. The problem is that if we uncomment the explicit type declaration of h (which is identical to the inferred type reported by the compiler), typechecker complains. Let us keep the function h without the explicit type declaration. Let us apply what I thought as an equivalent transformation:
h = \b -> f (g (h b))
Hugs now reports ERROR "/tmp/b.hs":12 - Unresolved top-level overloading *** Binding : h *** Outstanding context : X b and GHCi reports /tmp/b.hs:12: Ambiguous type variable(s) `a' in the constraint `X a' arising from use of `f' at /tmp/b.hs:12 In a lambda abstraction: f (g (h b)) It is interesting that the error message is exactly the same as when we used the explicit type declaration for the old "h b=..." definition. Something seems to be going on...
[...]
Let us keep the function h without the explicit type declaration. Let us apply what I thought as an equivalent transformation:
h = \b -> f (g (h b))
Hugs now reports ERROR "/tmp/b.hs":12 - Unresolved top-level overloading *** Binding : h *** Outstanding context : X b and GHCi reports /tmp/b.hs:12: Ambiguous type variable(s) `a' in the constraint `X a' arising from use of `f' at /tmp/b.hs:12 In a lambda abstraction: f (g (h b))
It is interesting that the error message is exactly the same as when we used the explicit type declaration for the old "h b=..." definition.
Something seems to be going on...
Isn't this just the monomorphism restriction. In GHCi, Prelude> let k = (<) <interactive>:1: Ambiguous type variable(s) `a' in the constraint `Ord a' arising from use of `<' at <interactive>:1 In the definition of `k': (<) as opposed to Prelude> let k a b = a < b Prelude> and further, Prelude> let k = \ a b -> a < b <interactive>:1: Ambiguous type variable(s) `a' in the constraint `Ord a' arising from use of `<' at <interactive>:1 In a lambda abstraction: a < b
participants (2)
-
Derek Elkins -
oleg@pobox.com