种类级别的括号是什么意思?
What do parentheses at the kind level mean?
我了解的有以下几种:
String :: *
[] :: * -> *
(->) :: * -> * -> *
(,) :: * -> * -> *
但是这个 kind 是什么意思,它可能代表什么类型?
? :: (* -> *) -> *
? :: (* -> *) -> *
意味着如果你给 ?
某种东西 * -> *
,你会得到一个类型(某种东西 *
)。举个具体的例子:
newtype IntContainer f = IC { getContainer :: f Int }
这意味着包含 Int
s。我可以使用列表、集合或任何我想要的(* -> *
类型)作为基础数据结构来制作 IntContainer
s。问题是 f
在这里不是一种类型——它需要在它成为一种类型之前应用另一种类型。所以:IntContainer
需要对其应用一些东西,而这又需要对其应用一个类型。
ghci> :kind IntContainer
IntContainer :: (* -> *) -> *
我可以通过将种类 * -> *
应用到 IntContainer
来创建具体类型:
ghci> ic1 = IC [1,2,3]
ic1 :: IntContainer [] -- [] :: * -> *
ghci> ic2 = IC (Data.Set.fromList [1,2,3])
ic2 :: IntContainer Set -- Set :: * -> *
ghci> ic3 = IC (Data.Sequence.fromList [1,2,3])
ic3 :: IntContainer Seq -- Seq :: * -> *
我了解的有以下几种:
String :: *
[] :: * -> *
(->) :: * -> * -> *
(,) :: * -> * -> *
但是这个 kind 是什么意思,它可能代表什么类型?
? :: (* -> *) -> *
? :: (* -> *) -> *
意味着如果你给 ?
某种东西 * -> *
,你会得到一个类型(某种东西 *
)。举个具体的例子:
newtype IntContainer f = IC { getContainer :: f Int }
这意味着包含 Int
s。我可以使用列表、集合或任何我想要的(* -> *
类型)作为基础数据结构来制作 IntContainer
s。问题是 f
在这里不是一种类型——它需要在它成为一种类型之前应用另一种类型。所以:IntContainer
需要对其应用一些东西,而这又需要对其应用一个类型。
ghci> :kind IntContainer
IntContainer :: (* -> *) -> *
我可以通过将种类 * -> *
应用到 IntContainer
来创建具体类型:
ghci> ic1 = IC [1,2,3]
ic1 :: IntContainer [] -- [] :: * -> *
ghci> ic2 = IC (Data.Set.fromList [1,2,3])
ic2 :: IntContainer Set -- Set :: * -> *
ghci> ic3 = IC (Data.Sequence.fromList [1,2,3])
ic3 :: IntContainer Seq -- Seq :: * -> *