Skip to content

Deriving Generic1 for a type containing Either

With the following code:

{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data T a = C (Either Int a) deriving Generic1

I get the error message:

Main.hs:3:38:
    Can't make a derived instance of `Generic1 T':
      must not apply type constructors that cannot be represented with `Rep1'
      (such as `Either') to arguments that involve the last type parameter
    In the data declaration for `T'

But when I look at the instances of Generic1, I see the following (summarized):

*Main> :i Generic1
...
instance Generic1 (Either a) -- Defined in `GHC.Generics'
...

So, it seems that Either a can be represented with Rep1. Consequently, the error message is a bit confusing.

Similarly, I can define my own version of Either -- e.g. data U a b = A a | B b deriving Generic1 -- replace Either with U and still get the message. I also get this problem using U as data U a b = U a b deriving Generic1 but not with T as data T a = C (Int, a) deriving Generic1.

Lastly, I try this:

data T a = C (Either Int (T a)) deriving Generic1

but the error is even stranger here:

Main.hs:3:38:
    Can't make a derived instance of `Generic1 T':
      must not apply type constructors that cannot be represented with `Rep1'
      (such as `T') to arguments that involve the last type parameter
    In the data declaration for `T'

What's the general problem with these examples?

  • Can the error message be improved?
  • Can the deriving mechanism be extended for these types?

I'm assuming I can write my own Generic1 instance, though I haven't actually tried.

Trac metadata
Trac field Value
Version 7.6.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information