使用 -XGeneralizedNewtypeDeriving 和 -XMultiParamTypeClasses
Using -XGeneralizedNewtypeDeriving with -XMultiParamTypeClasses
以下代码导致错误:
{-# 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
错误:
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
所以 GHC 正在寻找一个 Module DWrapper D
实例来派生所请求的 Module D D
实例。我想这是合理的,但不是我想要的。有没有办法告诉 GHC 从哪个实例派生? GNTD 如何在 MPTC 上工作?
不幸的是,GeneralizedNewtypeDeriving
only works on the last parameter of a multi-parameter typeclass. Even with standalone deriving:
The stand-alone syntax is generalised for newtypes in exactly the same way that ordinary deriving
clauses are generalised (Section 7.5.5, “Generalised derived instances for newtypes”). For example:
newtype Foo a = MkFoo (State Int a)
deriving instance MonadState Int Foo
GHC always treats the last parameter of the instance (Foo
in this example) as the type whose instance is being derived.
(顺便说一句,我尝试搜索任何相关的 GHC trac 错误报告或功能请求,但找不到。)
以下代码导致错误:
{-# 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
错误:
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
所以 GHC 正在寻找一个 Module DWrapper D
实例来派生所请求的 Module D D
实例。我想这是合理的,但不是我想要的。有没有办法告诉 GHC 从哪个实例派生? GNTD 如何在 MPTC 上工作?
不幸的是,GeneralizedNewtypeDeriving
only works on the last parameter of a multi-parameter typeclass. Even with standalone deriving:
The stand-alone syntax is generalised for newtypes in exactly the same way that ordinary
deriving
clauses are generalised (Section 7.5.5, “Generalised derived instances for newtypes”). For example:newtype Foo a = MkFoo (State Int a) deriving instance MonadState Int Foo
GHC always treats the last parameter of the instance (
Foo
in this example) as the type whose instance is being derived.
(顺便说一句,我尝试搜索任何相关的 GHC trac 错误报告或功能请求,但找不到。)