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

GHC ghc-devs at haskell.org
Thu Feb 23 14:48:03 UTC 2017


#10607: Auto derive from top to bottom
-------------------------------------+-------------------------------------
        Reporter:  songzh            |                Owner:  (none)
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  7.11
      Resolution:                    |             Keywords:  deriving,
                                     |  typeclass, auto
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------
Changes (by RyanGlScott):

 * cc: RyanGlScott (added)


Comment:

 I'm chiming in on this ticket pretty late, but nevertheless, here's my
 take on this situation.

 I agree that this feature shouldn't be baked into the language, and this
 should ideally be something that Template Haskell could handle. And Song
 certainly tried to make it work in TH, but quickly discovered a problem
 that has bitten `singletons` and many other TH libraries—TH is just plain
 terrible at type inference. Stated in a more focused way, if you are
 trying to use TH to splice in something like:

 {{{#!hs
 deriving instance ??? => C (T a b c)
 }}}

 Then trying to use TH to come up with `???` is really hard in the general
 case. In fact, I don't think there'd be any way to Do It Right without the
 full power of GHC's type inference engine, which is certainly something
 I'm not eager to bake into TH.

 I talked to Song about this at ICFP 2016, and we brainstormed some ways to
 get around the second point. I pointed out that `StandaloneDeriving`
 currently has a glaring flaw. While `StandaloneDeriving` is quite flexible
 and lets you derive instances wherever you want, it always requires that
 you provide the instance context in full. But `deriving` clauses don't
 have this onerous restriction, as you can just type:

 {{{#!hs
 data T a b c = ...
   deriving C
 }}}

 And GHC will figure out the instance context for you. What I feel like we
 need here is the ability to combine `StandaloneDeriving`'s capability to
 be used anywhere with `deriving` clauses' type inference.

 So what I will suggest (and hopefully articulate later in a proper GHC
 proposal) is extending `StandaloneDeriving` in the following way: if you
 type this:

 {{{#!hs
 deriving instance => C (T a b c)
 }}}

 Then GHC will try to fill in the instance context as if you had written
 `data T a b c = ... deriving C`. (Syntax bikeshedding is welcome.) Now the
 `derive-topdown` library's job is much easier: it just has to splice in a
 bunch of instances of the form:

 {{{#!hs
 deriving instance => Generic (A a b)
 deriving instance => Data (B a b)
 -- etc.
 }}}

 And GHC will do the rest! This requires very little in the way of fancy TH
 footwork, and moreover, it makes `StandaloneDeriving` more convenient to
 use.

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


More information about the ghc-tickets mailing list