Suppose I have the following class:

class P a where
  nameOf :: a -> String

I would like to declare that all instances of this class are automatically instances of Show. My first attempt would be the following:

instance P a => Show a where
  show = nameOf

My first attempt to go this way yesterday resulted in a rabbit warren of language extensions: I was first told to switch on flexible instances, then undecidable instances, then overlapping instances, and finally getting an error about overlapping instance declarations. I gave up and returned to repeating the code. However, this fundamentally seems like a very simple demand, and one that should be easily satisfied.

So, two questions:

  1. Is there a trivially easy way to do this that I've just missed?
  2. Why do I get an overlapping instances problem? I can see why I might need UndecidableInstances, since I seem to be violating the Paterson condition, but there are no overlapping instances around here: there are no instances of P, even. Why does the typechecker believe there are multiple instances for Show Double (as seems to be the case in this toy example)?
Edit
Report