[Haskell-cafe] confusion about 'instance'....

Miguel Mitrofanov miguelimo38 at yandex.ru
Thu Jan 10 08:30:18 EST 2008


> class A a
> type T = (forall x.Num x=>x)
> instance A T

"type" declares a synonym, like #define in C - but working only on types. So, essentially, you wrote

instance A (forall x.Num x => x)

which is not very Haskelly.

> I am simply trying to state that all members of typeclass Num are of
> typeclass A....

You can't do that. But, if there would not be any other instances of A, then you don't need it at all, you can just use Num class. And if there are some, for example

data D = D
instance A D

then it can happen (well, it's unlikely, but possible) that you or some other developer working on your code would declare

instance Num D where ...

After that, you would have two instances of A for the type D, one defined explicitly and one derived from the Num instance. That's not a problem for this empty class, but if class A is not empty, say

class A x where a :: x

then the compiler would be unable to decide which instance (and which "a") to choose. So allowing that leads to non-obvious bugs.

However, if you trust yourself enough, you can do what you want to in this way:

{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-}
class A a
instance Num a => A a


More information about the Haskell-Cafe mailing list