Overlapping and incoherent instances
Simon Peyton Jones
simonpj at microsoft.com
Thu Aug 7 20:46:43 UTC 2014
| overlappable? In other words, does OVERLAPPABLE instruct the
| type checker to expect overlapping instances even if none of
| them are visible?
No, not at the moment.
Suppose we have
class C a where
op :: a -> String
instance {-# OVERLAPPABLE #-} C a => C [a] where
op x = "[a]"
instance {-# OVERLAPPING #-} C [Int] where
op x = "[Int]"
foo :: C a => a -> String
foo x = op [x] ++ "urk"
Then we (rightly) get an overlapping instance error when we try to solve the (C [a]) constraint arising from op [a] in foo's RHS.
But if we omit the type signature for foo, then GHC will (as before) not simplify the (C [a]) constraint, for the same reason, but because it's unsolved GHC will abstract over it to give the inferred type
foo :: C [a] => a -> String
But this only happens if the overlapping instance is visible here. If the C [Int] instance is in some other module, then GHC will infer foo :: C a => a -> String.
Your point is that if the C [a] instance is marked OVERLAPPABLE, perhaps GHC should decline to simplify it, on the grounds that a more specific instance might appear. But then the (C [a]) instance would never, ever be used! (On the grounds that it might be overlapped by some as-yet-unseen instance. So I don't think this would work.
In any case, if the current behaviour doesn't seem precisely defined by the user manual, could you suggest some words that would make it clearer?
Simon
| -----Original Message-----
| From: Glasgow-haskell-users [mailto:glasgow-haskell-users-
| bounces at haskell.org] On Behalf Of Bertram Felgenhauer
| Sent: 07 August 2014 16:25
| To: glasgow-haskell-users at haskell.org
| Subject: Re: Overlapping and incoherent instances
|
| Simon Peyton Jones wrote:
| > | >>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
|
| I'm curious how this affects simplification of contexts. If I have
|
| class Foo a
| instance Foo a => Foo [a]
|
| then GHC will simplify
|
| foo :: Foo [a] => a -> ()
|
| to
|
| foo :: Foo a => a -> ()
|
| Would this be prevented by declaring the Foo [a] instance as
| overlappable? In other words, does OVERLAPPABLE instruct the
| type checker to expect overlapping instances even if none of
| them are visible?
|
| Cheers,
|
| Bertram
| _______________________________________________
| Glasgow-haskell-users mailing list
| Glasgow-haskell-users at haskell.org
| http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
More information about the Glasgow-haskell-users
mailing list