Coherence may also arise because of an ambiguous type.
Here's the classic example.
class Read a where read :: String - a
class Show a where show :: a - String
f s = show (read s)
f has type String-String, therefore we can pick
some arbitrary Read/Show classes.
If you want to know
I believe that GHC's overlapping instance extensions
effectively uses inequalities.
Why do you think that 'inequalities' model 'best-fit'?
instance C Int -- (1)
instance C a-- (2)
under a 'best-fit' instance reduction strategy
we would resolve C a by using (2).
'best-fit' should
| I believe that GHC's overlapping instance extensions
| effectively uses inequalities.
I tried to write down GHC's rules in the manual:
http://haskell.org/ghc/dist/current/docs/users_guide/type-extensions.htm
l#instance-decls
The short summary is:
- find candidate instances that match
- if
Thank you Martin.
Coherence (roughly) means that the program's semantics is independent
of the program's typing.
In case of your example below, I could type the program
either use the first or the second instance (assuming
g has type Int-Int). That's clearly bound.
If g has type Int-Int, it
one can force GHC to choose the less specific instance (if one
confuses GHC well enough): see the example below.
your second example doesn't really do that, though it may look that way.
class D a b | a - b where g :: a - b
instance D Int Bool where g x = True
instance TypeCast Int b = D a b