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上运作?
不幸的是,GeneralizedNewtypeDeriving只适用于多参数类型类的最后一个参数.即使使用独立派生:
独立语法对于newtypes是一般化的,与普通
deriving子句的推广完全相同(第7.5.5节"newtypes的广义派生实例").例如:Run Code Online (Sandbox Code Playgroud)newtype Foo a = MkFoo (State Int a) deriving instance MonadState Int FooGHC始终将实例的最后一个参数(
Foo在此示例中)视为其实例派生的类型.
(顺便说一句,我试图搜索任何相关的GHC trac错误报告或功能请求,但找不到任何.)
| 归档时间: |
|
| 查看次数: |
142 次 |
| 最近记录: |