why do classes require the type variable in type signatures?

Hello All, Why does this compile class Special a where isSpecial :: a -> Bool whereas, GHC 7.6 complains about this class AlsoSpecial a where isAlsoSpecial :: b -> Bool This is the error message I get: The class method `isAlsoSpecial' mentions none of the type variables of the class AlsoSpecial a When checking the class method: isAlsoSpecial :: forall b. b -> Bool In the class declaration for `AlsoSpecial' My question is: Why must I use the type variable of the class declaration (i.e. *a*) in the type signature for the associated method? Is there a fundamental reason for this or is it just a GHC specific limitation? I didn't see a need for it when watching this video http://channel9.msdn.com/posts/MDCC-TechTalk-Classes-Jim-but-not-as-we-know-... that explains the translation that GHC does to turn type classes into core. Thanks! Dimitri

If you have a class like this
class AlsoSpecial a where isAlsoSpecial :: b -> Bool
then 'isAlsoSpecial' would get a type like this isAlsoSpecial :: AlsoSpecial a => b -> Bool There may be several instances of class 'AlsoSpecial' with different implementations. GHC has the task of figuring out which of the implementations to use in a specific situation. It does so by looking at the context in which it is used. But if, like here, the type itself does not mention the class variable, then learning about the context won't help. For example, if we use the function like this isAlsoSpecial 'x' then GHC learns that 'b' must be a 'Char', but it still does not know anything about 'a'. In fact, there's absolutely no way to establish information about 'a' at all. This is the problem, and this is why the class method is rejected in the first place. (The translation to Core is still possible. In Core itself, every type application is explicit, so in Core there'd be a syntax to manually resolve the ambiguity by explicitly selecting which 'a' we want to use and passing the appropriate dictionary. But Haskell's surface language doesn't provide this option, and it never has.) Cheers, Andres -- Andres Löh, Haskell Consultant Well-Typed LLP, http://www.well-typed.com Registered in England & Wales, OC335890 250 Ice Wharf, 17 New Wharf Road, London N1 9RF, England

Consider two instances of your class:
data Foo = Foo
data Bar = Bar
instance AlsoSpecial Foo where
-- This implementation *must* be constant True or False because
-- we don't constrain b at all, so it has to work for all b.
isAlsoSpecial b = True
instance AlsoSpecial Bar where
isAlsoSpecial b = False
Now what would you expect this to output?
isAlsoSpecial "hello"
Peter
On 23 April 2014 09:23, Dimitri DeFigueiredo
Hello All,
Why does this compile
class Special a where isSpecial :: a -> Bool
whereas, GHC 7.6 complains about this
class AlsoSpecial a where isAlsoSpecial :: b -> Bool
This is the error message I get:
The class method `isAlsoSpecial' mentions none of the type variables of the class AlsoSpecial a When checking the class method: isAlsoSpecial :: forall b. b -> Bool In the class declaration for `AlsoSpecial'
My question is: Why must I use the type variable of the class declaration (i.e. *a*) in the type signature for the associated method? Is there a fundamental reason for this or is it just a GHC specific limitation? I didn't see a need for it when watching this video
http://channel9.msdn.com/posts/MDCC-TechTalk-Classes- Jim-but-not-as-we-know-them
that explains the translation that GHC does to turn type classes into core.
Thanks!
Dimitri _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Andres Löh
-
Dimitri DeFigueiredo
-
Peter Hall