Skip to content

deriving, wrong code: newtype T cat a = MkT ((forall xx. cat xx xx) -> a) deriving stock Functor

This fails (deriving stock Functor) (data type comes from Applicative Archery)

$ ./ghc-stage2 --interactive -ignore-dot-ghci
GHCi, version 8.7.20181230: https://www.haskell.org/ghc/  :? for help
Prelude> :set prompt "> "
> :set -XRankNTypes -XDeriveFunctor -ddump-deriv
> :set -dsuppress-idinfo -dsuppress-coercions -dsuppress-type-applications -dsuppress-uniques -dsuppress-module-prefixes
> newtype T cat a = MkT ((forall xx. cat xx xx) -> a) deriving Functor

==================== Derived instances ====================
Derived class instances:
  instance Functor (T cat) where
    fmap f (MkT a1) = MkT ((\ b2 b3 -> f (b2 ((\ b1 -> b1) b3))) a1)
    (<$) z (MkT a1)
      = MkT ((\ b3 b4 -> (\ b2 -> z) (b3 ((\ b1 -> b1) b4))) a1)


Derived type family instances:



<interactive>:4:62: error:
    • Couldn't match type ‘forall xx. cat xx xx’ with ‘cat xx0 xx0’
      Expected type: cat xx0 xx0 -> a
        Actual type: (forall xx. cat xx xx) -> a
    • In the first argument of ‘\ b2 b3
                                  -> f (b2 ((\ b1 -> b1) b3))’, namely
        ‘a1’
      In the first argument of ‘MkT’, namely
        ‘((\ b2 b3 -> f (b2 ((\ b1 -> b1) b3))) a1)’
      In the expression: MkT ((\ b2 b3 -> f (b2 ((\ b1 -> b1) b3))) a1)
      When typechecking the code for ‘fmap’
        in a derived instance for ‘Functor (T cat)’:
        To see the code I am typechecking, use -ddump-deriv
    • Relevant bindings include
        a1 :: (forall xx. cat xx xx) -> a (bound at <interactive>:4:62)
        fmap :: (a -> b) -> T cat a -> T cat b
          (bound at <interactive>:4:62)

<interactive>:4:62: error:
    • Couldn't match type ‘forall xx. cat xx xx’ with ‘cat xx1 xx1’
      Expected type: cat xx1 xx1 -> b
        Actual type: (forall xx. cat xx xx) -> b
    • In the first argument of ‘\ b3 b4
                                  -> (\ b2 -> z) (b3 ((\ b1 -> b1) b4))’, namely
        ‘a1’
      In the first argument of ‘MkT’, namely
        ‘((\ b3 b4 -> (\ b2 -> z) (b3 ((\ b1 -> b1) b4))) a1)’
      In the expression:
        MkT ((\ b3 b4 -> (\ b2 -> z) (b3 ((\ b1 -> b1) b4))) a1)
      When typechecking the code for ‘<$’
        in a derived instance for ‘Functor (T cat)’:
        To see the code I am typechecking, use -ddump-deriv
    • Relevant bindings include
        a1 :: (forall xx. cat xx xx) -> b (bound at <interactive>:4:62)
        (<$) :: a -> T cat b -> T cat a (bound at <interactive>:4:62)
>

Generates odd code

fmap .. = MkT ((\ b2 b3 -> f (b2 ((\ b1 -> b1) b3))) a1)

It works fine (in HEAD) to deriving newtype Functor (it didn't in 8.2 where I checked)

Trac metadata
Trac field Value
Version 8.7
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information