[Haskell] Newbie help with type-classes

Derek Elkins derek.a.elkins at gmail.com
Fri May 11 08:45:23 EDT 2007


Ryan Ingram wrote:

haskell-cafe at haskell.org is better for this type of question.  Follow-up is set 
to it.

> Here's a test case for the problem I'm having; I'm using runhaskell from 
> ghc v6.6.
>  
> Problem #1) Without -fallow-undecidable-instances, I get the following 
> error:
>     Constraint is no smaller than the instance head
This is bad.

> Problem #2) With -fallow-undecidable-instances, I get this error instead:
>     Overlapping instances for ConvertToIntList ()

> I don't understand why there is an overlapping instances error; () is 
> not an instance of ConvertToInt so how could that instance ever apply?

I write anywhere, instance ConvertToInt () where ...
Now it is overlapping.

> Is there something basic about type-classes that I'm not understanding 
> here?  
They are open (Open World Assumption).  I can add a new instance anywhere at any 
time.

> Code below:
> {-# OPTIONS -fglasgow-exts -fallow-undecidable-instances #-}
>  
> module TestCase
> where
>  
> class ConvertToInt a where
>    conv :: a -> Int
>  
> class ConvertToIntList a where
>    convl :: [a] -> [Int]
>  
> instance ConvertToInt Int where
>    conv = id
>  
> instance ConvertToInt a => ConvertToIntList a where
This is what it's complaining about in the first error; this doesn't work.

>    convl = map conv
>  
> instance ConvertToIntList () where
>    convl x = []


More information about the Haskell mailing list