以下代码导致错误:
{-# 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是如何在MPTCs上工作的?
发布于 2015-10-21 00:32:31
不幸的是,GeneralizedNewtypeDeriving只对多参数类型的最后一个参数起作用。即使是独立派生
deriving子句的泛化(第7.5.5节,“新类型的泛化派生实例”)完全相同。例如:
新类型Foo a= MkFoo (State )派生实例MonadState Int
GHC总是将实例的最后一个参数(本例中的Foo)视为其实例正在派生的类型。(顺便说一句,我试图搜索任何相关的GHC bug报告或特性请求,但都找不到。)
https://stackoverflow.com/questions/33245788
复制相似问题