Overlapping and incoherent instances

Simon Peyton Jones simonpj at microsoft.com
Tue Aug 5 21:05:28 UTC 2014


| >>Here's one concern I have with the deprecation of
| >>-XOverlappingInstances: I don't like overlapping instances, I find
| >>them confusing and weird and prefer to use code that doesn't
| >>include them, because they violate my expectations about how type
| >>classes work. When there is a single LANGUAGE pragma, that's a
| >>simple, easily-checkable signpost of "this code uses techniques
| >>that Ben doesn't understand". When it is all controlled by pragmas
| >>I basically have to check every instance declaration individually.

I see your point.  Though you could just grep for OVERLAP!

I suppose that -XOverlappingInstances could mean "silently honour OVERLAPPABLE/OVERLAPPING pragmas", while lacking it would mean "honour OVERLAPPABLE/OVERLAPPING pragmas, but emit noisy warnings" or even "don't honour them and warn".

But that is different to the behaviour today, so we'd need a new LANGUAGE pragma.  Perhaps -XHonourOverlappingInstances or something.

My sense is that the extra faff is not worth it.

| >>On a largely unrelated note, here's another thing I don't
| >>understand: when is OVERLAPPABLE at one instance declaration
| >>preferable to using only OVERLAPPING at the instance declarations
| >>that overlap it?

It's a user decision.  GHC allows
  - OVERLAPPABLE at the instance that is being overlapped, or
  - OVERLAPPING at the instance that is doing the overlapping, or
  - both

Another possible choice would be to require both.  One or t'other wouldn't do.  But the current choice (with the LANGUAGE pragmas -XOverlappingInstances) is the either/or choice, and I had no user pressure to change that.  There *is* user pressure for the either/or semantics, so that you can *later* add an un-anticipated OVERLAPPING instance.

| >    {-# LANGUAGE FlexibleInstances #-}
| >    module M where
| >    class C a where f :: a -> a
| >    instance C a where f x = x
| >    instance C Int where f x = x + 1
| >
| >I suspect many people have the intuition that NoOverlappingInstances
| >should forbid the above, but in fact OverlappingInstances or no only
| >controls instance *resolution*. I imagine you all already knew this
| >but I did not until I carefully reread things.

It's pretty clearly stated in the manual, but I'd be delighted to add a paragraph or two, or an example, if you can draft something and say where a good place for it would be (ie where you'd have looked).

Thanks

Simon



More information about the Libraries mailing list