Opened 7 years ago

Closed 7 years ago

Last modified 3 years ago

#7487 closed bug (fixed)

Deriving Generic1 for a type containing Either

Reported by: spl Owned by: dreixel
Priority: normal Milestone:
Component: Compiler Version: 7.7
Keywords: Generics Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

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.

Change History (5)

comment:1 Changed 7 years ago by dreixel

Version: 7.6.17.7

Thanks for the report. Those error messages are wrong, and the deriving mechanism should just generate an instance for those types. I'll look into it.

comment:2 Changed 7 years ago by jpm@…

commit 31d8900dbcf87fc32ac9e3de2ea0ed2039e72ee4

Author: Jose Pedro Magalhaes <jpm@cs.ox.ac.uk>
Date:   Fri Dec 7 16:24:06 2012 +0000

    Fix #7487
    
    Signed-off-by: Jose Pedro Magalhaes <jpm@cs.ox.ac.uk>

 compiler/typecheck/TcGenGenerics.lhs |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)

comment:3 Changed 7 years ago by dreixel

Status: newmerge

Fixed in HEAD, testcase added in GenCanDoRep1.

comment:4 Changed 7 years ago by igloo

difficulty: Unknown
Resolution: fixed
Status: mergeclosed

comment:5 Changed 3 years ago by RyanGlScott

Keywords: Generics added
Note: See TracTickets for help on using tickets.