Class instance specificity order (was Re: Fundeps and type equality)

Simon Peyton-Jones simonpj at
Fri Jan 11 19:55:58 CET 2013

|  The -XOverlappingInstances flag instructs GHC to allow more than one
|  instance to match, provided there is a most specific one. For example,
|  the constraint C Int [Int] matches instances (A), (C) and (D), but the
|  last is more specific, and hence is chosen. If there is no most-specific
|  match, the program is rejected."

|  What it doesn't says is how "most-specific match" is computed.  

An instance declaration (A) is "more specific" than another (B) iff the head of (A) is a substitution instance of (B). For example

(A)	instance context1 => C [Maybe a]
(B)	instance context2 => C [b]

Here (A) is more specific than (B) because we can get (A) from (B) by substituting b:=Maybe a.

Does that make it clear?  If so I will add it to the manual.

|  not cases are so clear.  For example, which of
|  instance context1 => C Int b
|  instance context2 => C a   [[b]]
|  does C Int [[Int]] match best against?

Neither is instance is more specific than the other, so C Int [[Int]] is rejected.

|  If there isn't currently a good
|  way to resolve this, I would like to suggest the type-shape measure I
|  proposed in that paper I wrote up awhile back could be used.

I think the current design is reasonable, and I don't want to complicate it unless there is a very compelling reason to do so.


More information about the Glasgow-haskell-users mailing list