Skip to content

GitLab

  • Menu
Projects Groups Snippets
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,869
    • Issues 4,869
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 456
    • Merge requests 456
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #10607
Closed
Open
Created Jul 06, 2015 by songzh@trac-songzh

Auto derive from top to bottom

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:

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:

data Person = Person Names Address
            | Student Names Address
data Names  = Names String
data Address = Address Gate
type Gate = (String,Int)

deriving topdown Generic Person
deriving topdown Eq Person

It will generate Eq instances of Person, Names and Address since Name and Address are 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.

Edited Mar 10, 2019 by songzh
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking