[GHC] #16323: Cannot deduce X error with X provided in TypeFamilies

GHC ghc-devs at haskell.org
Fri Feb 15 14:20:41 UTC 2019


#16323: Cannot deduce X error with X provided in TypeFamilies
-------------------------------------+-------------------------------------
        Reporter:  pjljvdlaar        |                Owner:  (none)
            Type:  bug               |               Status:  closed
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.2.2
      Resolution:  duplicate         |             Keywords:  Deriving
Operating System:  Windows           |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:  #11008, #15868    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------
Changes (by RyanGlScott):

 * status:  new => closed
 * resolution:   => duplicate
 * related:   => #11008, #15868


Comment:

 This is by design. Per the
 [https://downloads.haskell.org/~ghc/8.6.3/docs/html/users_guide/glasgow_exts.html
 #inferred-context-for-deriving-clauses users' guide section] on
 `deriving`:

 > The Haskell Report is vague about exactly when a deriving clause is
 legal. For example:
 >
 > {{{#!hs
 > data T0 f a = MkT0 a         deriving( Eq )
 > data T1 f a = MkT1 (f a)     deriving( Eq )
 > data T2 f a = MkT2 (f (f a)) deriving( Eq )
 > }}}
 >
 > The natural generated Eq code would result in these instance
 declarations:
 >
 > {{{#!hs
 > instance Eq a         => Eq (T0 f a) where ...
 > instance Eq (f a)     => Eq (T1 f a) where ...
 > instance Eq (f (f a)) => Eq (T2 f a) where ...
 > }}}
 >
 > The first of these is obviously fine. The second is still fine, although
 less obviously. The third is not Haskell 98, and risks losing termination
 of instances.
 >
 > GHC takes a conservative position: it accepts the first two, but not the
 third. The rule is this: each constraint in the inferred instance context
 must consist only of type variables, with no repetitions.
 >
 > This rule is applied regardless of flags. If you want a more exotic
 context, you can write it yourself, using the standalone deriving
 mechanism.

 A similar situation is happening in the code that you are trying to
 derive. For instance, this:

 {{{#!hs
 data RefMap a = RefMap (Map (Ref a) a) deriving Eq
 }}}

 Would require generating the following code:

 {{{#!hs
 instance (Eq a, Eq (Ref a)) => Eq (RefMap a) where ...
 }}}

 The constraint `Eq (Ref a)` has an occurrence of `Ref` underneath the
 class `Eq`, which isn't a type variable. Therefore, GHC conservatively
 backs out and refuses to infer it. In order to write this, GHC requires
 that you use `StandaloneDeriving`, like so:

 {{{#!hs
 deriving instance (Eq a, Eq (Ref a)) => Eq (RefMap a)
 }}}

 #11008 and #15868 are about this same issue, so I'll close this ticket as
 a duplicate.

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


More information about the ghc-tickets mailing list