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

Stefan Holdermans stefan at cs.uu.nl
Tue Dec 19 02:09:40 EST 2006


You gave us a code snippet:

> The code below does not compile unless the "bar" function is  
> annotated with a suitable constraint on the class of the formal  
> parameter.
> > module Main where
> >
> > class (C a)
> > data (C foo) => XY foo = X foo | Y foo
> >
> > bar :: a -> XY a
> > bar aFoo = X aFoo
> >
> > main = return ()

And asked:

> 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)?

Well, the compiler can infer this type, but it does not even try to  
do so, because you yourself explicitly gave a type signature for bar.  
So, then the compiler only checks whether bar indeed has the claimed  
type. Here, it does not, because the type you gave is too general. If  
you would have omit the signature, the compiler would have inferred  
the right type, i.e., including the class constraint.

If you look at type signatures as machine-checkable documentation,  
then the compiler here pointed you at a flaw in your documentation.



More information about the Haskell-Cafe mailing list