gpt4 book ai didi

haskell - 将 -XGeneralizedNewtypeDeriving 与 -XMultiParamTypeClasses 结合使用

转载 作者:行者123 更新时间:2023-12-02 17:16:29 24 4
gpt4 key购买 nike

以下代码会导致错误:

{-# 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仅适用于多参数类型类的最后参数。即使有 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 错误报告或功能请求,但找不到任何内容。)

关于haskell - 将 -XGeneralizedNewtypeDeriving 与 -XMultiParamTypeClasses 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33245788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com