diff options
author | Ryan Scott <ryan.gl.scott@gmail.com> | 2016-11-10 15:22:48 -0500 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2016-11-10 15:25:40 -0500 |
commit | 03e8d26fe0a8f7981a76550118f3c584cad76c47 (patch) | |
tree | 3f42d9e3e50670384fe3271d1942f575107643ad /docs/users_guide/glasgow_exts.rst | |
parent | e8ae4dc8558b5917f6c65ad196859783e90038bd (diff) | |
download | haskell-03e8d26fe0a8f7981a76550118f3c584cad76c47.tar.gz |
Prevent GND from inferring an instance context for method-less classes
When `GeneralizedNewtypeDeriving` is used with a type class that has no
methods, it will generate a redundant context, and as a result, it can
trigger warnings when compiled with `-Wredundant-constraints`. This is a
simple change in behavior to check beforehand if a class has methods
when deriving it with GND, and if it has no methods, avoid inferring the
redundant context.
Beware that the test for #6088, which used to be expected to fail, now
compiles without issue since it doesn't infer a problematic instance
context.
Thanks to Simon Peyton Jones for doing the necessary refactoring in
f05d685ae05ec293083f2fa7ec7ba057fbe64869.
Fixes #12814.
Test Plan: ./validate
Reviewers: goldfire, rwbarton, simonpj, austin, bgamari
Reviewed By: simonpj
Subscribers: thomie
Differential Revision: https://phabricator.haskell.org/D2692
GHC Trac Issues: #12814
Diffstat (limited to 'docs/users_guide/glasgow_exts.rst')
-rw-r--r-- | docs/users_guide/glasgow_exts.rst | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/docs/users_guide/glasgow_exts.rst b/docs/users_guide/glasgow_exts.rst index 38afb3b675..9cda16a970 100644 --- a/docs/users_guide/glasgow_exts.rst +++ b/docs/users_guide/glasgow_exts.rst @@ -3971,6 +3971,11 @@ Then the derived instance declaration is of the form :: instance C t1..tj t => C t1..tj (T v1...vk) +Note that if ``C`` does not contain any class methods, the instance context +is wholly unnecessary, and as such GHC will instead generate: :: + + instance C t1..tj (T v1..vk) + As an example which does *not* work, consider :: newtype NonMonad m s = NonMonad (State s m s) deriving Monad @@ -4018,7 +4023,7 @@ associated type families. Here is an example: :: The derived ``HasRing`` instance would look like :: - instance HasRing a => HasRing (L1Norm a) where + instance HasRing (L1Norm a) where type Ring (L1Norm a) = Ring a To be precise, if the class being derived is of the form :: @@ -4051,7 +4056,7 @@ then you can derive a ``C c_1 c_2 ... c_(m-1)`` instance for For the derived ``Bad Int`` instance, GHC would need to generate something like this: :: - instance Bad Int a => Bad Int (Foo a) where + instance Bad Int (Foo a) where type B Int = B ??? Now we're stuck, since we have no way to refer to ``a`` on the right-hand @@ -4088,6 +4093,10 @@ If both of these conditions are met, GHC will generate this instance: :: type Tk tk_1 tk_2 ... (N n_1 n_2 ... n_q) ... tk_p = Tk tk_1 tk_2 ... <rep-type> ... tk_p +Again, if ``C`` contains no class methods, the instance context will be +redundant, so GHC will instead generate +``instance C c_1 c_2 ... c_(m-1) (N n_1 n_2 ... n_q)``. + Beware that in some cases, you may need to enable the :ghc-flag:`-XUndecidableInstances` extension in order to use this feature. Here's a pathological case that illustrates why this might happen: :: |