[GHC] #15650: Add (or document if already exist) ability to derive custom typeclasses via source plugins

GHC ghc-devs at haskell.org
Mon Sep 17 03:37:42 UTC 2018


#15650: Add (or document if already exist) ability to derive custom typeclasses via
source plugins
-------------------------------------+-------------------------------------
           Reporter:  chshersh       |             Owner:  (none)
               Type:  feature        |            Status:  new
  request                            |
           Priority:  normal         |         Milestone:  8.6.1
          Component:  Compiler       |           Version:  8.6.1-beta1
           Keywords:  source         |  Operating System:  Unknown/Multiple
  plugins,deriving,typeclass         |
       Architecture:                 |   Type of failure:  None/Unknown
  Unknown/Multiple                   |
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 == Problem

 Suppose, I have some custom typeclass `Foo` defined in some library `foo`:

 {{{#!hs
 class Foo a where
     ... some methods ...
 }}}

 I would like to be able to derive instances of this typeclass for any
 possible data type using `deriving` clause just like GHC already does for
 typeclasses `Eq`, `Ord`, `Show`, `Read`, `Enum`, etc.:

 {{{#!hs
 data Bar = Bar | Baz
     deriving (Eq, Ord, Foo)
 }}}

 There're already two possible ways to derive instances of custom
 typeclasses:

 1. `anyclass` deriving strategy (usually involves `Generic`)
 2. `-XTemplateHaskell` solution.

 But I would like to have source-plugin-based solution for this problem so
 I can just add `-fplugin=Foo.Plugin` and enjoy deriving capabilities.

 == Advantage over existing approaches

 Solution with `-XTemplateHaskell` is not that pleasant to write and easy
 to maintain (you need to use libraries like
 http://hackage.haskell.org/package/th-abstraction to support multiple GHC
 versions),involves scoping restriction and is syntactically uglier.
 Compare:

 {{{#!hs
 {-# LANGUAGE TemplateHaskell #-}

 data Bar = Bar | Baz
     deriving (Eq, Ord)

 deriveFoo ''Bar
 }}}

 Solution with something like `Generic` introduces performance overhead
 (required for to/from generic representation conversion). This might not
 be significant for something like ''parsing CLI arguments'' but it's more
 important if you want to have efficient binary serialisation.

 Also, it's known that deriving typeclasses is a relatively slow
 compilation process
 (https://github.com/tfausak/tfausak.github.io/issues/127) so there's
 slight chance that deriving typeclass manually can be slightly faster than
 deriving `Generic + MyClass`. Especially when maintainers of plugins can
 experiment with some caching strategies for deriving typeclasses.

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


More information about the ghc-tickets mailing list