Opened 16 months ago

Last modified 11 months ago

#15376 new bug

GHC determine illegal kind for standalone deriving with Deriving via

Reported by: mizunashi_mana Owned by:
Priority: normal Milestone: 8.10.1
Component: Compiler Version:
Keywords: deriving Cc: Iceland_jack, RyanGlScott, kosmikus
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: #14331 Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

Happy to release GHC 8.6.1-alpha1! I used it to test new extensions, and then I met below errors:

$ ghci-8.6.0.20180627
GHCi, version 8.6.0.20180627: http://www.haskell.org/ghc/  :? for help
Prelude> :set -XDerivingVia -XStandaloneDeriving
Prelude> newtype FunctorWrapped f a = FunctorWrapped (f a)
Prelude> deriving via f instance Functor f => Functor (FunctorWrapped f)

<interactive>:3:33: error:
    • Expected kind ‘* -> *’, but ‘f’ has kind ‘*’
    • In the first argument of ‘Functor’, namely ‘f’
      In the stand-alone deriving instance for
        ‘Functor f => Functor (FunctorWrapped f)’

<interactive>:3:62: error:
    • Expected kind ‘* -> *’, but ‘f’ has kind ‘*’
    • In the first argument of ‘FunctorWrapped’, namely ‘f’
      In the first argument of ‘Functor’, namely ‘(FunctorWrapped f)’
      In the stand-alone deriving instance for
        ‘Functor f => Functor (FunctorWrapped f)’

However,

newtype FunctorWrapped f a = FunctorWrapped (f a)
    deriving Functor via f

is passed through on GHC 8.6.1-alpha1.

Is this a bug or my misunderstand?

Change History (5)

comment:1 Changed 16 months ago by Iceland_jack

Cc: Iceland_jack RyanGlScott added
Keywords: deriving added

comment:2 Changed 16 months ago by RyanGlScott

Blocked By: 14331 added
Cc: kosmikus added

This is indeed a known limitation of the way deriving declarations are typechecked.

Short answer: You can work around the issue by introducing an explicit type signature, i.e.,

deriving via (f :: * -> *) instance Functor f => Functor (FunctorWrapped f)

Long answer: Any type variables quantified by via are kind-checked in isolation, without any information that might be gleaned from bidirectionally kind-checking the instance head. This means that GHC sees:

deriving via f ...

And hastily concludes that f is of kind *. Bummer.

We (kosmikus, Iceland_jack, and I) discussed this at some length in https://github.com/RyanGlScott/ghc/issues/29, and came to the conclusion that we might be able to fix this issue by introducing fresh unification variables when kind-checking, then unifying, and then generalizing/skolemizing if there are any unfilled unification variables left. This bears a close resemblance to the algorithm described in https://ghc.haskell.org/trac/ghc/ticket/14331#comment:31, so I'm going to claim that this ticket is blocked by #14331.

comment:3 in reply to:  2 Changed 16 months ago by mizunashi_mana

Replying to RyanGlScott:

This is indeed a known limitation of the way deriving declarations are typechecked.

I understand. Thank you for your comment.

comment:4 Changed 16 months ago by bgamari

Milestone: 8.6.18.8.1

These won't be fixed for in GHC 8.6.

comment:5 Changed 11 months ago by osa1

Milestone: 8.8.18.10.1

Bumping milestones of low-priority tickets.

Note: See TracTickets for help on using tickets.