[Haskell-cafe] Class annotation on datatype ineffective in function

Brian Hulley brianh at metamilk.com
Tue Dec 19 14:42:55 EST 2006

Reto Kramer wrote:
> The code below does not compile unless the "bar" function is
> annotated with a suitable constraint on the class of the formal
> parameter.
>> class (C a)
>> data (C foo) => XY foo = X foo | Y foo
>> bar :: a -> XY a
>> bar aFoo = X aFoo
> As suggested, this works:
>> bar :: (C a) => a -> XY a
> Can someone explain to me why the compiler can not infer that "a" (in
> bar) must be (C a) from the bar result type "XY a" (by way of the "C
> class" provided for the datatype)?

Hi Reto -
If you'd not given any signature at all the compiler would have inferred the 
correct type for bar, but since you gave an explicit signature, the compiler 
had no option but to complain that you missed out the C a constraint. (ie if 
you decide to provide a signature you must give the full signature including 
constraints since the compiler won't add anything to them - partial 
signatures are not (yet) supported)

Regards, Brian.

More information about the Haskell-Cafe mailing list