2

I have been using haskell for a while now. I understand most/some of the concepts but I still do not understand, what exactly does haskells type system allow me to do that I cannot do in another statically typed language. I just intuitively know that haskells type system is better in every imaginable way compared to the type system in C,C++ or java, but I can't explain it logically, primarily because of a lack of in depth knowledge about the differences in type systems between haskell and other statically typed languages.

Could someone give me examples of how haskells type system is more helpful compared to a language with a static type system. Examples, that are terse and can be succinctly expressed would be nice.

3
  • 1
    Haskell uses the Hindley-Milner type inference system. It essentially means that you don't need to specify the types of every expression. The type system can deduce most of the types by itself. Oct 27, 2014 at 10:13
  • 1
    Does this thread help? programmers.stackexchange.com/questions/167975/…
    – DanielM
    Oct 27, 2014 at 10:15
  • 2nd-order polymorphism is nice Oct 27, 2014 at 13:24

3 Answers 3

3

The Haskell type system has a number of features which all exist in other languages, but are rarely combined within a single, consistent language:

  • it is a sound, static type system, meaning that a number of errors are guaranteed not to happen at runtime without needing runtime type checks (this is also the case in Caml, SML and almost the case in Java, but not in, say, Lisp, Python, C, or C++);
  • it performs static type reconstruction, meaning that the programmer doesn't need to write types unless he wants to, the compiler will reconstruct them on its own (this is also the case in Caml and SML, but not in Java or C);
  • it supports impredicative polymorphism (type variables), even at higher kinds (unlike Caml and SML, or any other production-ready language known to me);
  • it has good support for overloading (type classes) (unlike Caml and SML).

Whether any of those make Haskell a better language is open to discussion — for example, while I happen to like type classes a lot, I know quite a few Caml programmers who strongly dislike overloading and prefer to use the module system.

On the other hand, the Haskell type system lacks a few features that other languages support elegantly:

  • it has no support for runtime dispatch (unlike Java, Lisp, and Julia);
  • it has no support for existential types and GADTs (these are both GHC extensions);
  • it has no support for dependent types (unlike Coq, Agda and Idris).

Again, whether any of these are desirable features in a general-purpose programming language is open to discussion.

1
  • Also possibly worth mentioning: higher-kinded types and type variables. For example, in Java you can't write T<Integer>. Oct 27, 2014 at 18:57
0

In addition to what others have answered, it is also Haskell's type system that makes the language pure, i.e. which distinguishes between values of a certain type and effectful computations that produce a result of that type.

0

One major difference between Haskell's type system and that of most OO languages is that the ability for a function to have side effects is represented by a data type (a monad such as IO). This allows you to write pure functions that the compiler can verify are side-effect-free and referentially transparent, which generally means that they're easier to understand and less prone to bugs. It's possible to write side-effect-free code in other languages, but you don't have the compiler's help in doing so. Haskell makes you think more carefully about which parts of your program need to have side effects (such as I/O or mutable variables) and which parts should be pure.

Also, although it's not quite part of the type system itself, the fact that function definitions in Haskell are expressions (rather than lists of statements) means that more of the code is subject to type-checking. In languages like C++ and Java, it's often possible to introduce logic errors by writing statements in the wrong order, since the compiler doesn't have a way to determine that one statement must precede another. For example, you might have one line that modifies an object's state, and another line that does something important based on that state, and it's up to you to ensure that these things happen in the correct order. In Haskell, this kind of ordering dependency tends to be expressed through function composition — e.g. f (g x) means that g must run first — and the compiler can check the return type of g against the argument type of f to make sure you haven't composed them the wrong way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.