George Russell writes:
I am finding functional dependencies confusing. (I suspect I am not alone.) Should the following code work?
class HasConverter a b | a -> b where convert :: a -> b
instance (HasConverter a b,Show b) => Show a where show value = show (convert value)
Yes. Let's assume that multi-parameter classes and overlapping instances are enabled. Without the functional dependency, the instance decl would be rejected because the type variable b occurs in the context (HasConverter a b, Show b) but not in the head (Show a). The compiler needs to generate some code for `show value', but it's not allowed to make an arbitrary substitution for b. The functional dependency provides a way to determine b uniquely once a is matched. So, the compiler can generate the code for `show value' without being arbitrary. Regards, Tom