In the official docs on Instance Chains an example is given for a typeclass “MyShow”.
But what I don’t understand is how does one add an instance to that class in another module than MyShow is defined in?
In the dissertation of the original creators of Instance Chains there’s a way to do it, but it relies on “if” constraint clauses that Purescript explicitly doesn’t support.
And in Haskell, more specific instances are used before less specific instances, so there’s no conflict between some other instance of MyShow to a type and “showA” in Haskell.
If there’s not a way to do it, I can see at least three possibilities:
• Add constraints like in the original idea of Instance Chains (the most complicated one to implement probably, and would make the code that used it much more verbose, requiring a second typeclass just for “showA”)
• Perhaps merge Haskell-style OverridableInstances with Instance Chains: if one instance is more specific (eg, an instance of a concrete Type) and the other less specific (eg, a parameter/default/catch-all instance) then the more specific one is used, and if they’re the same (or close) specificity, then the ordering given in instance chains is used.
• Consider instance chains to be providing a partial ordering on instances, so someone could add, say, “else instance showA” reusing it after an instance of MyShow to their type in another module and it conveys an explicit ordering between their type’s instance and showA (the default instance).
(allowing the reuse of instance names like “showA” here could be nice)
And an OverlappingInstances error would only be given if the partial ordering was absent or inconsistent.
• (or a combination of the last two might be nice, with it only giving an error if two instances are of equal specificity and there’s no valid explicit partial ordering between them)