DoCon and GHC

J. Garrett Morris jgmorris at
Thu Jan 3 14:10:15 CET 2013

On Thu, Jan 3, 2013 at 4:57 AM, Simon Peyton-Jones
<simonpj at> wrote:
> When matching instances, GHC does not take account of the context of
> the instance. Say you have
>         data T a = ...
>         data S1 = ...
>         data S2 = ...
> [a]     instance Num a  => Num (T a) where ...
> [b]     instance Show a => Num (T a) where ...
>         instance Num S1
> and suppose you need an instance of (Num (T S1)). Then although [a]
> and [b] overlap, you might say we should use [a], since S1 is an
> instance of Num, but not an instance of Show.  But GHC does not do
> this.  It matches instances only based on the bit after the "=>".

It seems you're making GHC seem more capricious than it is here.  Even
were GHC to consider contexts in instance selection, the choice of [a]
over [b] would still be incoherent: the compiler has no way to prove
that there is no later instance adding S1 to class Show.  Indeed, the
S1's and S2's are in some sense not relevant: because Haskell provides
no mechanism to exclude types from classes, there is no way to ever
coherently use instance [a] or [b], regardless of the argument to T.


Sent from my mail client.

More information about the Glasgow-haskell-users mailing list