问题描述
Looking at a kind
example from Learn You a Haskell:
data Frank a b = Frank {frankField :: b a} deriving (Show)
LYAH讨论:
在我想到一个示例时,这对我来说很有意义:Frank String Maybe = Frank {frankField :: Maybe String}
.
This makes sense to me as I think of an example: Frank String Maybe = Frank {frankField :: Maybe String}
.
但是我对如何考虑右侧的kind
感到困惑
But I'm confused as to how to think about the kind
of the right-hand side
... = Frank {frankField :: b a}
.
由于返回类型为frankField :: b a
,所以Frank
的kind
应该是:
Since the return type is frankField :: b a
, how can the kind
of Frank
be:
* -> (* -> *) -> * -- what does the right-most * represent?
为什么右侧的kind
等于*
?
推荐答案
好吧,右侧的Frank
是 value 构造函数,值没有种类,它们有类型.种类指的是左侧的Frank
,它是类型构造函数.将值构造函数重命名为MkFrank
:
Well, the Frank
on the right-hand side is a value constructor and values don't have kinds, they have types. The kind refers to the Frank
on the left-hand side, which is a type constructor. It might help to rename the value constructor to MkFrank
:
data Frank a b = MkFrank {frankField :: b a} deriving (Show)
值构造函数的类型是
MkFrank :: b a -> Frank a b
类型构造器的类型是
Frank :: * -> (* -> *) -> *
这表示Frank
接受一个(具体)类型(*
),而类型构造函数接受一个类型((* -> *)
)并为您提供类型(最后一个*
).这类似于类型为A -> (B -> C) -> D
的函数:它需要一个具体的"值和一个函数(一种获得给定另一个值的值的方法),并且为您提供一个具体的"值,区别在于我们是处理类型而不是值.
This says that Frank
takes one (concrete) type (*
) and a type constructor that takes one type ((* -> *)
) and gives you a type (the last *
). This is analogous to a function with a type A -> (B -> C) -> D
: it takes a "concrete" value and a function (a way to get a value given another value) and it gives you a "concrete" value, the difference is that we are dealing with types instead of values.
请注意,类型对类型类的作用略有不同(尽管类不包含在问题主体中).有一种叫做Constraint
的特殊种类,代表签名中可以出现在=>
左侧的东西.
As a side note, kinds work a bit differently for type classes (classes are not involved in the question body though). There's a special kind called Constraint
which represents things that can go on the left hand side of =>
in a signature.
这篇关于了解Haskell类的种类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!