[Haskell-cafe] Instance match surprise

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Mon Feb 1 20:29:51 UTC 2016

On Mon, Feb 01, 2016 at 07:53:32PM +0100, Wojtek Narczyński wrote:
> Why does the first instance match? ANY is neither Eq nor Typeable. I
> thought I had some basic understanding of type classes, and now
> this...

> minimatch.hs:21:6: error:
>     • Overlapping instances for Match A130 ANY
>         arising from a use of ‘match’
>       Matching instances:
>         instance (Eq a, Eq q, Typeable a, Typeable q, AtmAcct a,
>                   Against q) =>
>                  Match a q
>           -- Defined at minimatch.hs:18:10
>         instance AtmAcct a => Match a ANY -- Defined at minimatch.hs:19:10
>     • In the expression: match A130 ANY
>       In an equation for ‘m1’: m1 = match A130 ANY

The way instance resolution works is somewhat counterintuitive, and it took
me a long time to get my head around it.  The upshot is that the instance
context is *absolutely irrelevant* when looking for a matching instance.

The type is

    Match A130 ANY

The instances in scope are 

    (Eq a, Eq q, Typeable a, Typeable q, AtmAcct a, Against q) => Match a q


    AtmAcct a => Match a ANY

But because the instance contexts are irrelevant, these are just

    ... => Match a q


    ... => Match a ANY

Both of these match 'Match A123 ANY', and thus you have overlap.

If you want to know *why* the instance contexts are ignored then you'll have
to ask someone who knows more about Prolog :)


More information about the Haskell-Cafe mailing list