[GHC] #13324: Allow PartialTypeSignatures in the instance context of a standalone deriving declaration

GHC ghc-devs at haskell.org
Thu Feb 23 15:53:09 UTC 2017


#13324: Allow PartialTypeSignatures in the instance context of a standalone
deriving declaration
-------------------------------------+-------------------------------------
           Reporter:  RyanGlScott    |             Owner:  (none)
               Type:  feature        |            Status:  new
  request                            |
           Priority:  normal         |         Milestone:
          Component:  Compiler       |           Version:  8.0.1
  (Type checker)                     |
           Keywords:                 |  Operating System:  Unknown/Multiple
       Architecture:                 |   Type of failure:  None/Unknown
  Unknown/Multiple                   |
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:  #10607
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 Currently, if you try to use a wildcard anywhere in an instance context,
 it will fail immediately:

 {{{
 $ ghci
 GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
 Loaded GHCi configuration from /home/rgscott/.ghci
 λ> instance _ => Show (Maybe a)

 <interactive>:1:10: error:
     Wildcard ‘_’ not allowed
       in an instance declaration for ‘Show’
 }}}

 But there's one situation where we could lift this restriction: the
 context of a standalone, derived instance declaration. That is, something
 like this:

 {{{#!hs
 deriving instance _ => Show (Maybe a)
 }}}

 Why? Because GHC already has the machinery needed to infer what the
 context should be (see
 [http://git.haskell.org/ghc.git/blob/8a6b8c5fb96472d7c2e2cd1918950dd50f2fef71:/compiler/typecheck/TcDerivInfer.hs#l49
 this part] of `TcDerivInfer`), so if a user turned on
 `PartialTypeSignatures`, GHC could just fill in the wildcard with the
 inferred constraints.

 The implementation won't be //that// easy, however, since we'd also have
 to watch out for trickery such as:

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

 I only mentioned putting wildcards in a derived instance //context//,
 because I think allowing the use of wildcards elsewhere in the instance
 head might be too difficult to deal with. I mean, how would you fill in
 this, for example?

 {{{#!hs
 instance (_ a)
 }}}

 This mini-feature has a very practical application: it would allow users
 to wield the flexibility of `StandaloneDeriving` without having to
 manually type in the instance context every time. That is, users could
 type in these instances:

 {{{#!hs
 deriving instance _ => Data (T a b c)
 deriving instance _ => Eq (T a b c)
 }}}

 Instead of their fully spelled-out, more laborious counterparts. This
 would be crucial for Template Haskell, as its ability to infer these
 contexts is quite limited (see #10607 for an example where this cropped
 up).

 Idle thought: could this be generalized to work for the instance context
 of //any// instance declaration (and not just derived ones)? From an
 outside perspective, it seems like typechecking other instances would
 require inferring constraints in a fashion quite similar to that of
 derived instances. But I am not at all familiar with that part of the
 typechecker, so I might be totally off here.

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


More information about the ghc-tickets mailing list