[Haskell-cafe] Type class context propagation investigation

Paul Keir pkeir at dcs.gla.ac.uk
Wed May 27 17:10:53 EDT 2009


Hi,

How does the context of a class instance declaration affect its subclasses?

The Num class instance outlined below has its requirement for Eq and Show satisfied on the preceding lines, and the code will compile. But if I, say, add an (Eq a) constraint to the Eq instance, in preparation for a simple (==) definition, I find that the Num instance declaration is left lacking. If I add the same (Eq a) constraint now to Num, calm is restored.

data Foo a = F a

instance Eq (Foo a) where
 (==) = undefined

instance Show (Foo a) where
 show = undefined

instance Num (Foo a)
 (+) = undefined
 ... etc.

The thing that confuses me with this is that it seems like Num "knows" that an (Eq a) context has been applied, and so what it sees as a problem, is somehow also the solution. Any advice/rules of thumb? Does this situation occur elsewhere? How do these constraints propagate?

Thanks,
Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090527/decd515b/attachment.html


More information about the Haskell-Cafe mailing list