Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does haskell erase types?

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

like image 807
Bittercoder Avatar asked Sep 18 '12 00:09

Bittercoder


2 Answers

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.

like image 119
AndrewC Avatar answered Oct 16 '22 03:10

AndrewC


C and Pascal have type erasure. Java lets you inspect classes at run-time - even dynamically loaded ones!

What Haskell does is much closer to Pascal than to Java.

like image 1
MathematicalOrchid Avatar answered Oct 16 '22 05:10

MathematicalOrchid