Rank2Types的目的是什么? [英] What is the purpose of Rank2Types?

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

问题描述

我不是很精通Haskell,所以这可能是一个非常简单的问题。

I am not really proficient in Haskell, so this might be a very easy question.

什么语言限制 Rank2Types 解决? Haskell中的函数已经支持多态参数吗?

What language limitation do Rank2Types solve? Don't functions in Haskell already support polymorphic arguments?

推荐答案


Haskell中的函数已经不支持多态参数?

Do not functions in Haskell already support polymorphic arguments?

它们只有一级,这意味着尽管你可以编写一个函数来接受不同类型的参数如果没有这个扩展名,你不能在同一个调用中编写一个使用它的参数作为不同类型的函数。

They do, but only of rank 1. This means that while you can write a function that takes different types of arguments without this extension, you can't write a function that uses its argument as different types in the same invocation.

例如,如果没有这个函数,以下函数不能输入因为在 f 的定义中 g 与不同的参数类型一起使用:

For example the following function can't be typed without this extension because g is used with different argument types in the definition of f:

f g = g 1 + g "lala"

请注意,将多态函数作为参数传递给另一个函数是完全可能的。所以像 map id [a,b,c] 这样的东西是完全合法的。但该函数可能仅将其用作单形。在示例 map中,map 使用 id ,就好像它有类型 String - >串。当然,您也可以传递给定类型的简单单形函数,而不是 id 。如果没有rank2types,函数就无法要求它的参数必须是一个多态函数,因此也无法将其用作多态函数。

Note that it's perfectly possible to pass a polymorphic function as an argument to another function. So something like map id ["a","b","c"] is perfectly legal. But the function may only use it as monomorphic. In the example map uses id as if it had type String -> String. And of course you can also pass a simple monomorphic function of the given type instead of id. Without rank2types there is no way for a function to require that its argument must be a polymorphic function and thus also no way to use it as a polymorphic function.

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

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