与Int列表比较的Int列表的相同种类 - >诠释? [英] Same Kinds for List of Int compared to List of Int -> Int?

查看:78
本文介绍了与Int列表比较的Int列表的相同种类 - >诠释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Why do both of these have the same kind?

ghci> :k [Int]
[Int] :: *
ghci> :k [Int -> Int]
[Int -> Int] :: *

编辑 per @

EDIT per @Gabriel Gonzalez's helpful comment.

我不明白<$ c $ c>种好,所以我没有一个很好的基础,期望上面的类型在 kind 中变化。

I don't understand kinds well, so I don't have a good basis for expecting the above types to vary in kind.

推荐答案

好的,让我们来检查一下。

Well, let's check.

Int :: *
[] :: * -> *

因此,当您应用 Int 类型到 [] 类型构造函数:

So when you apply the Int type to the [] type constructor:

[] Int :: *

这是另一种(合法的)写作方式

Which is just another (legal) way of writing

[Int] :: *

这一点非常容易。

Int :: *
(->) :: * -> * -> *
[] :: * -> *

(->) Int :: * -> *
(->) Int Int :: *

/ p>

Which is the same as

Int -> Int :: *

因此,按照与上述相同的推理,

and therefore, by the same reasoning as above,

[Int -> Int] :: *

但是这里有一个秘密。仔细看看 []

But here's a secret.. Take a closer look at the kind of [].

[] :: * -> *

这意味着在任何类型的列表中放入任何类型都是编译错误 * 。当你提供某种类型的 * 时,结果总是会有 * 。

That means that it's a compile error to put any type inside a list that isn't of kind *. And when you do provide it with something of kind *, the result will always have kind *.

您的困惑来自不保持级别分开。许多不同的类型具有相同的类型。毕竟, * 或多或少意味着此类型可以有值。 (有一些小的例外,但它们是低级的内部事物,你必须很难看清楚)。如果你可以有一个类型的值,这是一个非常好的选择,类型有 *

Your confusion comes from not keeping levels separate. Many, many different types have the same kind. After all, kind * more or less means "this type can have values". (There are some minor exceptions, but they're low-level internals things that you have to work quite hard to see.) If you can have a value of a type, it's a very good bet that the type has kind *.

这篇关于与Int列表比较的Int列表的相同种类 - &gt;诠释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆