[GHC] #15868: Standard deriving should be less conservative when `UndecidableInstances` is enabled

GHC ghc-devs at haskell.org
Wed Nov 7 17:55:27 UTC 2018


#15868: Standard deriving should be less conservative when `UndecidableInstances`
is enabled
-------------------------------------+-------------------------------------
        Reporter:  edsko             |                Owner:  (none)
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.6.1
      Resolution:                    |             Keywords:  deriving
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------
Changes (by RyanGlScott):

 * keywords:   => deriving


Comment:

 I have mixed feelings on this. I'm reluctant to turn off this validity
 check entirely when `UndecidableInstances` is enabled since there are
 several situations where this can catch you when you're writing utterly
 bogus programs, such as this one:

 {{{#!hs
 data NotAShowInstance
 data Foo = MkFoo Int NotAShowInstance deriving Show
 }}}
 {{{
 Bug.hs:2:48: error:
     • No instance for (Show NotAShowInstance)
         arising from the second field of ‘MkFoo’ (type ‘NotAShowInstance’)
       Possible fix:
         use a standalone 'deriving instance' declaration,
           so you can specify the instance context yourself
     • When deriving the instance for (Show Foo)
   |
 2 | data Foo = MkFoo Int NotAShowInstance deriving Show
   |                                                ^^^^
 }}}

 This is a relatively common mistake to make, and it's one that's caught by
 this validity check. If this check were relaxed, then GHC would generate
 this instance:

 {{{#!hs
 instance Show NotAShowInstance => Show Foo where ...
 }}}

 Which is almost surely //not// what you'd want.

 At the same time, I can certainly understand wanting to relax this
 restriction when type families or fancy types (e.g., `Show (f a b (g a b)
 (h a b))`) get involved. Perhaps there's a way to write up a specification
 that permits `T2` in your example but rejects `Foo` in my example. I'm not
 sure what such a specification would be yet, however.

-- 
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/15868#comment:3>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list