2

I would like to teach GHC to derive new typeclasses. Is it possible to do this externally without using Generic or TemplateHaskell? Like, with source plugins or something similar. So I can write:

data MyType = ...
    deriving MyTypeClass

UPDATE: Relevant GHC ticket with discussion:

Shersh
  • 9,019
  • 3
  • 33
  • 61
  • 1
    What is the difference between a TemplateHaskell script and a "source plugin"? – Dominique Devriese Sep 11 '18 at 09:40
  • @DominiqueDevriese Syntax sugar. I would to write `data MyType = ... deriving (Eq, Ord, MyClass)` instead of `data MyType = ... deriving (Eq, Ord); deriveMyClass ''MyType`. – Shersh Sep 11 '18 at 11:48
  • 1
    Why not use `Generic`? If you implement a default `Generic` interface you can use the `DeriveAnyClass` extension to get the syntax you want. – DarthFennec Sep 11 '18 at 17:05
  • `Generic` introduces performance overhead. Because you first convert to `Generic` representation and only then you work on that `Generic`. This is the reason why people use `TemplateHaskell` to derive `ToJSON/FromJSON` instances instead of using `DeriveAnyClass`. – Shersh Sep 12 '18 at 02:00
  • @K.A.Buhr Sorry, I don't see why it's preposterous. Could you, please, elaborate more on the cons of this approach? GHC doesn't know how to generate efficient instances for my typeclass if I write `deriving (MyClass)`. So there should be a way to teach GHC how to do so. Of course, I can fork GHC and add my logic there (in theory), but it's much better if this functionality can be added externally to the compiler in the opt-in way via some plugin for example. Writing this a compiler plugin allows to work with GHC AST directly instead of using `TemplateHaskell`. – Shersh Sep 17 '18 at 02:47

0 Answers0