[Haskell-beginners] why do classes require the type variable in type signatures?
peterjoel at gmail.com
Wed Apr 23 20:27:55 UTC 2014
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?
On 23 April 2014 09:23, Dimitri DeFigueiredo <defigueiredo at ucdavis.edu>wrote:
> 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
> that explains the translation that GHC does to turn type classes into core.
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners