首页 > 解决方案 > 为什么 Haskell 的 'Generic' 类类型族 'Rep a' 被注释为类型构造函数,而不是类型?

问题描述

考虑 Haskell 的Generic课程:

class Generic a where
  -- | Generic representation type
  type Rep a :: * -> *
  -- | Convert from the datatype to its representation
  from  :: a -> (Rep a) x
  -- | Convert from the representation to the datatype
  to    :: (Rep a) x -> a

我很好奇为什么它没有写成如下:

class Generic a where
  -- | Generic representation type
  type Rep a :: *
  -- | Convert from the datatype to its representation
  from  :: a -> Rep a
  -- | Convert from the representation to the datatype
  to    :: Rep a -> a

更具体地说,类型变量x在标准定义中代表什么?

标签: haskell

解决方案


这样做是为了允许GenericGeneric1类共享它们的大部分表示类型。这是否真的是一个好主意是值得商榷的。尽量忽略多余的参数。


推荐阅读