To allow deriving poly-kinded Generic1 instances

Ryan Scott ryan.gl.scott at gmail.com
Wed Oct 13 12:53:36 UTC 2021


Thanks for posting an example—that's very helpful to figure out what is
going on.

> Currently, GHC rejects the following code:
>
> {-# LANGUAGE DeriveGeneric #-}
> {-# LANGUAGE PolyKinds #-}
> import GHC.Generics
>
> data HKD f = Foo (f Int) (f Double)
>     | Bar (f Bool)
>     deriving Generic1
> The compilation error is
>
> • Can't make a derived instance of ‘Generic1 HKD’:
>     Constructor ‘Foo’ applies a type to an argument involving the last
parameter
>                       but the applied type is not of kind * -> *, and
>     Constructor ‘Foo’ applies a type to an argument involving the last
parameter
>                       but the applied type is not of kind * -> *, and
>     Constructor ‘Bar’ applies a type to an argument involving the last
parameter
>                       but the applied type is not of kind * -> *
> • In the data declaration for ‘HKD’

I see. That error message could be worded better, in my opinion. The issue
really isn't so much about the kind of `f`. If you had written `data HKD (f
:: Type -> Type) = Baz (Proxy f)`, for instance, I would expect it to work.
The real issue is _where_ `f` appears in the data constructors. In `Bar`,
for instance, you have:

> Bar (f Bool)

`Generic1` is limited to data types where the last type parameter only
appears as the last type argument in any field. This means that a field
like `Proxy f` would be fine, as that would be represented as `Rec1 Proxy`
in a `Rep1` instance. `f Bool`, on the other hand, is problematic.
`GHC.Generics` doesn't have a representation type that simultaneously
allows representing this field while also "focusing" on the last type
parameter like `Rec1`, `Par1`, etc. would allow.

The `Foo` constructor has similar issues. The error-reporting machinery for
`DeriveGeneric` essentially just accumulates every issue it encounters and
reports everything at once, which is why there is a duplicate error message
involving `Foo`. Needless to say, this kind of error message could be
improved.

> Although it is possible to define a hand-rolled instance of Generic1

Really? I'm not sure how you would define a correct `Generic1` instance for
`HKD` at all. What did you have in mind?

Best,

Ryan

On Wed, Oct 13, 2021 at 8:33 AM Fumiaki Kinoshita <fumiexcel at gmail.com>
wrote:

> Currently, GHC rejects the following code:
>
> {-# LANGUAGE DeriveGeneric #-}
> {-# LANGUAGE PolyKinds #-}
> import GHC.Generics
>
> data HKD f = Foo (f Int) (f Double)
>     | Bar (f Bool)
>     deriving Generic1
>
> The compilation error is
>
>     • Can't make a derived instance of ‘Generic1 HKD’:
>         Constructor ‘Foo’ applies a type to an argument involving the last
> parameter
>                           but the applied type is not of kind * -> *, and
>         Constructor ‘Foo’ applies a type to an argument involving the last
> parameter
>                           but the applied type is not of kind * -> *, and
>         Constructor ‘Bar’ applies a type to an argument involving the last
> parameter
>                           but the applied type is not of kind * -> *
>     • In the data declaration for ‘HKD’
>   |
> 7 |     deriving Generic1
>
> Although it is possible to define a hand-rolled instance of Generic1,
> DeriveGeneric is still restricted to Type -> Type.
>
> 2021年10月13日(水) 21:17 Ryan Scott <ryan.gl.scott at gmail.com>:
>
>> Hello,
>>
>> I'm not quite sure I understand the issue you're hitting. Generic1 is
>> poly-kinded, so I would expect it to be able to handle data types where the
>> last type parameter has differing kinds. Can you post a complete example of
>> the program you expect to typecheck, but doesn't?
>>
>> Best,
>>
>> Ryan
>> _______________________________________________
>> ghc-devs mailing list
>> ghc-devs at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20211013/acd21b3b/attachment.html>


More information about the ghc-devs mailing list