无法为此类型派生Generic?

yai*_*chu 1 haskell metaprogramming typeclass deriving

在GHC 8.6.2上编译这个简短的片段:

{-# LANGUAGE DeriveGeneric, PolyKinds #-}
import GHC.Generics

data Foo f
    = FA
    | FB (f (Foo f))
    deriving (Generic, Generic1)
Run Code Online (Sandbox Code Playgroud)

结果出现此错误:

Can't make a derived instance of ‘Generic1 Foo’:
  Constructor ‘FB’ applies a type to an argument involving the last parameter
                   but the applied type is not of kind * -> *
Run Code Online (Sandbox Code Playgroud)

是不是可以推导出Generic这样的类型?为什么?

Li-*_*Xia 5

Generic1 Foo不能派生,因为Generic1它是为了类型* -> *,而不是(* -> *) -> *.原则上可以支持(* -> *) -> *更多的构造函数GHC.Generics,但这种方法不能很好地扩展(它带来了更多不直观的语法限制,对于更复杂的类型,你总会遇到同样的问题).

实际上,您可以Generic使用与初始预期用例重叠的普通内容做很多事情Generic1.否则,你需要一些比想象更强大的东西GHC.Generics,也许是最近发布的种类仿制品(包括纸张和黑客的链接).

  • 确实存在一些较高的kinded类型,GHC目前可以从中获得`Generic1`实例,但是这个特征非常有限,几乎不值得一提.这主要是采用原始实现的'Generic1`用于`* - >*`(已经有严重的局限性),打开`PolyKinds`,并保留任何棒,这并不多. (2认同)