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

查看:13
本文介绍了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?

他们有,但只有等级 1.这意味着虽然你可以编写一个没有这个扩展的接受不同类型参数的函数,但你不能编写一个在同一个调用中将其参数用作不同类型的函数.

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.

例如下面的函数不能在没有这个扩展的情况下输入,因为gf的定义中与不同的参数类型一起使用:

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 使用 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天全站免登陆