Skip to content

TemplateHaskell deriving strategy

It would be great for the user experience of template-haskell to have blessed support as a deriving strategy.

https://github.com/ElderEphemera/deriving-th shows how that would look in user land, as follows:

  • Define a "wired-in" type class class DeriveTH (cls :: k) where deriveTH :: Name -> Q [Dec]
  • Allow library authors of libraries such as aeson, which define class ToJSON a where ..., to provide a deriving strategy through instance DeriveTH ToJSON where deriveTH typeName = [d | instance ToJSON $(..typeName) |]
  • Now clients of aeson can write data Foo = F Int Bool deriving ToJSON via template

There's a big caveat, though: Clients have to buy into this approach by compiling and using a source plugin. In my experience, this would be cumbersome enough for me not to ever do it. Of course, I can still write

$(deriveTH @ToJSON ''Foo)

But then I might just as well do

$(Data.Aeson.deriveJSON defaultOptions ''Foo)

as today. The point about via template is the native feel, and without it the whole DeriveTH approach seems moot.

So what this issue entails is

  • Flesh the idea out as a ghc-proposal
  • Define and wire-in class DeriveTH
  • Make via template desugar to the right $(deriveTH @C ''D) invokation.

This might even be fun to a newcomer.

Edited by Sebastian Graf
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information