什么是“n”在RankNTypes中 [英] What is "n" in RankNTypes

查看:198
本文介绍了什么是“n”在RankNTypes中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 forall 是如何让我们编写多态函数的。

根据章节,我们通常写的正常函数是Rank 1类型。此功能属于Rank 2类型:

  foo ::(a。a  - > a) - > (Char,Bool)
foo f =(f'c',f True)

它是这样解释的:

lockquote
一般来说,rank-n类型是一个函数,它至少有一个
rank-( n-1)的参数,但没有更高级别的参数。


实际上排名参数意味着什么?



有人可以给出类似于上述 foo 函数的Rank 3类型的示例。



  

(等级T)
等级(T→U)=最大(如果等级T = 0则0等级等级T + 1)(等级U)
等级(a)= 0

请注意箭头左侧的增加量。所以:

 等级0:Int 
等级1:forall a。 a - > Int
等级2:(forall a。a - > Int) - > Int
等级3 :((aa a。a - > Int) - > Int) - > Int

等等。


I understand how forall enables us to write polymorphic function.

According to this chapter, the normal function which we generally write are Rank 1 types. And this function is of Rank 2 type:

foo :: (forall a. a -> a) -> (Char,Bool)
foo f = (f 'c', f True)

It explains like this:

In general, a rank-n type is a function that has at least one rank-(n-1) argument but no arguments of even higher rank.

What does it actually mean by rank argument ?

Can somebody give an example of Rank 3 type which is similar to the above foo function.

解决方案

Rank is defined inductively on the structure of types:

rank (forall a. T) = max 1 (rank T)
rank (T -> U)      = max (if rank T = 0 then 0 else rank T + 1) (rank U)
rank (a)           = 0

Note how it increases by one on the left-hand side of an arrow. So:

Rank 0: Int
Rank 1: forall a. a -> Int
Rank 2: (forall a. a -> Int) -> Int
Rank 3: ((forall a. a -> Int) -> Int) -> Int

and so on.

这篇关于什么是“n”在RankNTypes中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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