[GHC] #10607: Auto derive from top to bottom

GHC ghc-devs at haskell.org
Mon Jul 6 13:01:44 UTC 2015


#10607: Auto derive from top to bottom
-------------------------------------+-------------------------------------
              Reporter:  songzh      |             Owner:
                  Type:  feature     |            Status:  new
  request                            |         Milestone:
              Priority:  normal      |           Version:  7.11
             Component:  Compiler    |  Operating System:  Unknown/Multiple
              Keywords:  deriving,   |   Type of failure:  None/Unknown
  typeclass, auto                    |        Blocked By:
          Architecture:              |   Related Tickets:
  Unknown/Multiple                   |
             Test Case:              |
              Blocking:              |
Differential Revisions:              |
-------------------------------------+-------------------------------------
 It is very good to see that Template Haskell types are now `Generic` and
 `DeriveAnyClass` have been implemented. I suppose that this auto-deriving
 thing can go even further. The problem is like the following:

 When we manipulate a complex composite JSON object, we need to define a
 dozen of data types for each of the dozen we may need to derive `Eq`,
 `Show`, `Generic` etc.

 Another situaition can be manipulating the AST of a language, there may be
 dozens of data type definitions, also, for each of them, we need to derive
 `Show`, `Eq`, `Generic`.

 Take the old Template Haskell types without `Generic` as an example, I
 want to make them instances of `Generic` type class, I wrote:

 {{{#!hs
 deriving instance Generic FixityDirection
 deriving instance Generic Inline
 deriving instance Generic RuleBndr
 deriving instance Generic Match
 deriving instance Generic Name
 deriving instance Generic RuleMatch
 deriving instance Generic Pred
 deriving instance Generic Phases
 deriving instance Generic Con
 deriving instance Generic Module
 deriving instance Generic AnnTarget
 deriving instance Generic Type
 deriving instance Generic TyVarBndr
 deriving instance Generic TyLit
 deriving instance Generic Exp
 deriving instance Generic Lit
 deriving instance Generic Pat
 deriving instance Generic Dec
 deriving instance Generic Clause
 deriving instance Generic FunDep
 deriving instance Generic Foreign
 deriving instance Generic Fixity
 deriving instance Generic Pragma
 deriving instance Generic FamFlavour
 ...
 }}}

 How can we use `Generic`? Say we want to serialize Template Haskell data
 type with `Binary` type class, Of course, by writing `Binary` empty
 instances or just derive it with `DeriveAnyClass` extension. However, the
 problem is that we still need to write deriving Binary dozens of times, as
 many as the trivial `Generic` deriving declaration above.

 What if GHC can attemp to derive the instance from the top of the tree
 down to the leaf in the data type declarations? For example:

 {{{#!hs
 data Person = Person Names Address
             | Student Names Address deriving Generic
 data Names  = Names String deriving Generic
 data Address = Address Gate  deriving Generic
 type Gate = (String,Int)

 deriving topdown Eq Person
 }}}
 It will generate `Eq` instances of `Person`, `Names` and `Address`, since
 it is in the Person data declaration tree.

 I tried to implement a prototype see https://github.com/HaskellZhangSong
 /derive-topdown

 It is rather an experiment tryout than a formal implementation. If you
 think this feature is attractive, I would like to discuss it.

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


More information about the ghc-tickets mailing list