Haskell擦除类型? [英] Does haskell erase types?

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

问题描述

Haskell擦除类型,如果是这样,它与Java中类型擦除类似/不同?

>

警告:经验+推理。请教在这两个编译器上为The Truth工作的人。

从类型检查在编译时完成的意义上说,类型系统的几个复杂特性被减少到很多更简单的语言结构,是的,但是与Java的方式不同。



类型签名不会产生运行时间开销。 Haskell编译器擅长于程序转换(它具有更多的余地,因为运行顺序在许多情况下并未由程序员指定),并自动内联适当的定义并将haskell-polymorhpic(= java-generic)函数专门化为特定类型如果它看起来合适的话,如果它有帮助的话。这与Java类型的擦除类似,但更多的方面。



Haskell本质上没有需要类型转换来确保类型安全性,因为Haskell被设计为类型安全从头开始。我们不会将所有东西都变成一个Object,并且我们不会将它们转换回来,因为多态(泛型)函数真的可以在任何数据类型上工作,不管它是什么,指针类型或拆箱整数,它只是工作,没有欺骗。因此,与Java不同,转换不是编译多态(通用)代码的功能。 Haskell的人会倾向于觉得,如果你正在进行类型转换,那么你就会说再见,以保证类型安全。



关于如何确保代码的静态类型正确性的可爱示例在编译时可以避免运行时间的开销,Haskell中有一个 newtype 构造,它是现有类型的类型安全包装,并且完全被编译掉 - 所有构造和销毁根本不会在运行时发生。类型系统确保在编译时使用正确,除了使用(类型检查)访问器函数之外,它不能在运行时获得。


$ b $ p

多态(泛型)函数没有多态开销。 Haskell重载函数(Java接口实例方法)有一个数据开销,因为有一个隐含的函数字典用于看起来像Java程序员迟到的绑定,但实际上它又是在编译时确定的。



总结:是的,甚至比在Java中还要好,不,它们在运行时永远不会被擦除。


Does Haskell erase types, and if so, in what ways is this similar/dissimilar to the type erasure that occurs in Java?

解决方案

Warning: experience+inference. Consult someone who works on both compilers for The Truth.

In the sense that type checking is done at compile time, and several complex features of the type system are reduced to much simpler language constructs, yes, but in a rather different way to Java.

A type signature creates no runtime overhead. The Haskell compiler is good at program transformation (it has more leeway, because the running order is in many cases not specified by the programmer), and automatically inlines appropriate definitions and specialises haskell-polymorhpic (=java-generic) functions to a particular type etc, as it sees fit, if it helps. That's a similar to Java type erasure, but more-so aspect.

There are in essence no type casts needed in Haskell to ensure type safety, because Haskell is designed to be type-safe from the ground up. We don't resort to turning everything into an Object, and we don't cast them back, because a polymorphic(generic) function genuinely does work on any data type, no matter what, pointer types or unboxed integers, it just works, without trickery. So unlike Java, casting is not a feature of compiling polymorphic(generic) code. Haskell folk tend to feel that if you're doing type casting, you said goodbye to type safety anyway.

For a lovely example of how ensuring the code's static type-correctness at compile time can avoid runtime overhead, there's a newtype construct in Haskell which is a type-safe wrapper for an existing type, and it's completely compiled away - all the construction and destruction simply doesn't happen at runtime. The type system ensures at compile time it's used correctly, it can't be got at at runtime except using (type-checked) accessor functions.

Polymorphic(generic) functions don't have polymorphic overheads. Haskell-overloaded functions (Java-interface-instance methods) have a data overhead in the sense that there's an implicit dictionary of functions used for what appears to be late binding to Java programmers, but is in fact, again, determined at compile time.

Summary: yes, even more so than in Java, and no, they were never there at runtime to erase anyway.

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

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