[clean-list] More generic instance always preferred instead of most specialized

John van Groningen johnvg at cs.ru.nl
Thu Jul 2 11:51:39 MEST 2009


Vag wrote:
>why
>
>Start = (C (D '1'),C (D 1))
>:: D a = D a
>:: T :== D Char
>instance C (D a) where C (D a) = D a
>instance C T where C _ = D '_'
>class C a :: a -> a
>
>and
>
>Start = (C (D '1'),C (D 1))
>:: D a = D a
>:: T :== D Char
>instance C T where C _ = D '_'
>instance C (D a) where C (D a) = D a
>class C a :: a -> a
>
>both gives ((D '1'),(D 1)) but not ((D '_'),(D 1))?

The compiler should reject both programs because the types of the instances
overlap. The instances are sorted by the compiler based on the type of the
instance, so changing the order of the instances in the program will
not change the instances that are selected by the compiler.

Maybe this is what you want:

:: D a = D a

class C a :: a->a

class C_D a :: (D a) -> D a

instance C (D a) | C_D a where
	C a = C_D a

instance C_D Int where
	C_D (D a) = D (a+1)

instance C_D Real where
	C_D (D a) = D (a+0.1)

Start = (C (D 1),C (D 1.0))

Kind regards,

John van Groningen


More information about the clean-list mailing list