Opened 7 years ago

Closed 6 years ago

Last modified 3 years ago

#7346 closed feature request (duplicate)

Allow the use of `deriving` for GHC generics

Reported by: kosmikus Owned by: dreixel
Priority: normal Milestone: 7.8.1
Component: Compiler Version: 7.7
Keywords: Generics Cc: leather@…, hvr@…, v.dijk.bas@…, cgaebel@…, pho@…, carter.schonwald@…, gregmainland@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #5462 Differential Rev(s):
Wiki Page:

Description (last modified by simonpj)

Currently, a class that makes use of generic default methods (via the DefaultSignatures) extension can be instantiated by providing an empty instance declaration.

I propose to allow the use of deriving as well:

  1. Standalone deriving should be usable for a class not only for the specific set of classes supported by GHC now, but in addition for any class, if (
    • there's at least one generic default given for a method of the class, and
    • there are generic or normal default implementations for *all* methods of the class.

There are a number of advantages of this solution over the empty instance declaration: we make it explicit that something generic is going on here; we ensure at compile time that we're not missing an implementation of a method; and we come syntactically closer to built-in derivable classes.

In cases where a conflict arises between current GHC semantics and the proposed semantics (for example, when newtype-deriving is involved, I guess), I propose to stick with current GHC semantics, but I'm open for other suggestions.

  1. I'd also like for normal deriving to be useful under the same conditions as above. For normal deriving GHC has to figure out the class context automatically. I propose that if normal deriving is used, GHC uses the same heuristic for figuring out the class context that it uses for Eq in the case of *-kinded classes, and for Functor in the case of * -> *-kinded classes. That may not be optimal or even wrong. But in such cases, standalone deriving can still be used.

Change History (12)

comment:1 Changed 7 years ago by dreixel

Owner: set to dreixel

Sounds good to me. I'm happy to implement this, if there are no objections. We'll probably need another extension name, though.

comment:2 Changed 7 years ago by spl

Cc: leather@… added

comment:3 Changed 7 years ago by simonpj

Description: modified (diff)
difficulty: Unknown

Some examples of what you have in mind would be useful. Perhaps update and elaborate the Wiki page about generic programming features in GHC?

But it sounds plausible.


Last edited 6 years ago by simonpj (previous) (diff)

comment:4 Changed 7 years ago by hvr

Cc: hvr@… added

comment:5 Changed 7 years ago by basvandijk

Cc: v.dijk.bas@… added

comment:6 Changed 7 years ago by cgaebel

Cc: cgaebel@… added

comment:7 Changed 7 years ago by PHO

Cc: pho@… added

comment:8 Changed 6 years ago by carter

Cc: carter.schonwald@… added

comment:9 Changed 6 years ago by igloo

Milestone: 7.8.1

comment:10 Changed 6 years ago by ghorn

Cc: gregmainland@… added

comment:11 Changed 6 years ago by dreixel

Resolution: duplicate
Status: newclosed

Probably best to focus the discussion on #5462.

comment:12 Changed 3 years ago by RyanGlScott

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