在Haskell中进行类型擦除? [英] Type erasure in Haskell?

查看:77
本文介绍了在Haskell中进行类型擦除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读讲义在Haskell上遇到此段时:

I was reading a lecture note on Haskell when I came across this paragraph:

此不关心"是参数多态性中的参数"的含义.所有Haskell函数的类型参数必须参数化;功能不得在乎或根据这些参数的选择做出决定.当a为Int时,函数不能做一件事,而当a为Bool时,函数不能做另一件事. Haskell简单地不提供编写此类操作的便利.语言的这种属性称为参数性.

This "not caring" is what the "parametric" in parametric polymorphism means. All Haskell functions must be parametric in their type parameters; the functions must not care or make decisions based on the choices for these parameters. A function can't do one thing when a is Int and a different thing when a is Bool. Haskell simply provides no facility for writing such an operation. This property of a langauge is called parametricity.

参数化有许多深远的影响.结果就是所谓的类型擦除.由于正在运行的Haskell程序永远无法基于类型信息做出决策,因此可以在编译期间删除所有类型信息.尽管在编写Haskell代码时类型是多么重要,但是在运行Haskell代码时它们是完全无关的.与其他需要在运行时保持类型不变的语言(例如Python)相比,此属性使Haskell的速度大大提高. (类型擦除并不是使Haskell更快的唯一原因,但是Haskell的时钟有时比Python快20倍.)

There are many deep and profound consequences of parametricity. One consequence is something called type erasure. Because a running Haskell program can never make decisions based on type information, all the type information can be dropped during compilation. Despite how important types are when writing Haskell code, they are completely irrelevant when running Haskell code. This property gives Haskell a huge speed boost when compared to other languages, such as Python, that need to keep types around at runtime. (Type erasure is not the only thing that makes Haskell faster, but Haskell is sometimes clocked at 20x faster than Python.)

我不了解的是所有Haskell函数"参数?在Haskell中不是键入显式/静态的吗?我也不太了解类型擦除如何改善编译时间运行时?

What I don't understand is how are "all Haskell functions" parametric? Aren't types explicit/static in Haskell? Also I don't really understand how type erasure improves compiling time runtime?

抱歉,如果这些问题确实很基础,那么我是Haskell的新手.

Sorry if these questions are really basic, I'm new to Haskell.

另一个问题:为什么作者说尽管类型在编写Haskell代码时有多重要,但在运行Haskell代码时却完全不相关"?

One more question: why does the author say that "Despite how important types are when writing Haskell code, they are completely irrelevant when running Haskell code"?

推荐答案

我不了解所有Haskell函数"是如何参数化的?

What I don't understand is how are "all Haskell functions" parametric?

并没有说所有的Haskell函数都是参数化的,而是说:

It doesn't say all Haskell functions are parametric, it says:

所有Haskell函数的类型参数必须为参数 .

Haskell函数不需要任何类型参数.

A Haskell function need not have any type parameters.

还有一个问题:为什么作者说尽管在编写Haskell代码时类型非常重要,但在运行Haskell代码时它们却完全无关"?

One more question: why does the author say that "Despite how important types are when writing Haskell code, they are completely irrelevant when running Haskell code"?

与动态类型语言不同,在动态语言中,您需要在运行时检查(例如)两个事物是否是数字,然后再尝试将它们加在一起,而运行的Haskell程序知道,如果您试图将它们加在一起,则它们必须是数字,因为编译器已事先确定了数字.

Unlike a dynamically typed language where you need to check at run time if (for example) two things are numbers before trying to add them together, your running Haskell program knows that if you're trying to add them together, then they must be numbers because the compiler made sure of it beforehand.

在Haskell中不是键入显式/静态的吗?

Aren't types explicit/static in Haskell?

Haskell中的类型通常可以推断出来,在这种情况下,它们不必是显式的.但是您说的对,它们是静态的,这是正确的,这实际上就是它们在运行时无所谓的原因,因为静态意味着编译器确保一切都具有程序执行之前应该具有的类型.

Types in Haskell can often be inferred, in which case they don't need to be explicit. But you're right that they're static, and that is actually why they don't matter at run time, because static means that the compiler makes sure everything has the type that it should before your program ever executes.

这篇关于在Haskell中进行类型擦除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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