将-XGeneralizedNewtypeDeriving与-XMultiParamTypeClasses一起使用

cro*_*eea 2 haskell ghc derived-instances

以下代码导致错误:

{-# LANGUAGE GeneralizedNewtypeDeriving, MultiParamTypeClasses, StandaloneDeriving #-}

class Module a b where
    (*>) :: a -> b -> b

data D
newtype DWrapper = DW D    
instance Module D D    
deriving instance Module DWrapper DWrapper
Run Code Online (Sandbox Code Playgroud)

错误:

No instance for (Module DWrapper D) arising from a use of ‘Main.*>’
In the first argument of ‘GHC.Prim.coerce’, namely
  ‘(Main.*>) :: DWrapper -> D -> D’
In the expression:
    GHC.Prim.coerce ((Main.*>) :: DWrapper -> D -> D) ::
      DWrapper -> DWrapper -> DWrapper
In an equation for ‘*>’:
    (*>)
      = GHC.Prim.coerce ((Main.*>) :: DWrapper -> D -> D) ::
          DWrapper -> DWrapper -> DWrapper
When typechecking the code for  ‘Main.*>’
  in a derived instance for ‘Module DWrapper DWrapper’:
  To see the code I am typechecking, use -ddump-deriv
Run Code Online (Sandbox Code Playgroud)

所以GHC正在寻找一个Module DWrapper D实例来派生所请求的Module D D实例.我猜这是合理的,但不是我想要的.有没有办法告诉GHC从哪个实例派生出来?GNTD如何在MPTC上运作?

Ørj*_*sen 5

不幸的是,GeneralizedNewtypeDeriving只适用于多参数类型类的最后一个参数.即使使用独立派生:

(顺便说一句,我试图搜索任何相关的GHC trac错误报告或功能请求,但找不到任何.)

  • 没有门票,因为它不是一个bug.在没有歧义的情况下执行此操作的唯一方法是允许程序员指定派生自哪个实例. (3认同)