[GHC] #14243: GHC doesn't add constraint when deriving

GHC ghc-devs at haskell.org
Tue Sep 19 17:56:46 UTC 2017


#14243: GHC doesn't add constraint when deriving
-------------------------------------+-------------------------------------
        Reporter:  Iceland_jack      |                Owner:  (none)
            Type:  bug               |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.2.1
      Resolution:                    |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:  #11008            |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------
Changes (by RyanGlScott):

 * related:   => #11008


Comment:

 This is expected behavior. Here's a much simpler demonstration of this
 phenomenon:

 {{{#!hs
 {-# LANGUAGE TypeFamilies #-}

 type family Foo
 data Bar = Bar Foo deriving Show
 }}}
 {{{
 Bug.hs:4:29: error:
     • No instance for (Show Foo)
         arising from the first field of ‘Bar’ (type ‘Foo’)
       Possible fix:
         use a standalone 'deriving instance' declaration,
           so you can specify the instance context yourself
     • When deriving the instance for (Show Bar)
   |
 4 | data Bar = Bar Foo deriving Show
   |                             ^^^^
 }}}

 As you noted, GHC could theoretically add this inferred `Show Foo`
 constraint to the derived instance context, but it chooses not to. The
 reasoning is outlined in
 [https://downloads.haskell.org/~ghc/8.2.1/docs/html/users_guide/glasgow_exts.html
 #inferred-context-for-deriving-clauses this section] of the users' guide.
 Essentially, GHC has a metric for determining whether an inferred context
 is "exotic", and if a constraint is considered too exotic, GHC will give
 up and demand that you write the context yourself using
 `StandaloneDeriving`.

 There are a couple of reasons why GHC does this. One reason is that exotic
 constraints are often not Haskell98/2010, so inferring them would be
 troublesome. But perhaps a more compelling reason is it can sometimes
 catch mistakes, like in this code:

 {{{#!hs
 data X a b = MkX (a -> b) deriving Eq
 }}}

 This fails, complaining about a missing `Eq (a -> b)` instance. This is
 almost certainly the desired behavior, since you probably didn't mean to
 use an `Eq` instance for function types in the first place! If you're
 determined that this is the right thing to do, however, GHC gives you a
 manual override in the form of `StandaloneDeriving`.

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


More information about the ghc-tickets mailing list