Hacker News new | past | comments | ask | show | jobs | submit login

Partially, but not the the same level as Haskell. It's pretty easy to get broken code past the compiler in C# for example.



Could you give some concrete examples?


First example is nullability. Anders says ~50% of production issues with c# involve null deference errors. Haskell tags in compile time which types may or may not be null.

In c# if your method is supposed to return one thing and set an object attribute of another, and you forget setting the attribute, the compiler won't help.

In Haskell, you don't use mutability much, instead you return both things. If you forget to return it you get help from the compiler.

Another example: in Haskell, you write a sort function but you forget to handle the base case (always returning an empty list instead). You get an inferred type like:

  sort :: Ord a => [a] -> [b]
Which tells you you forgot to actually return the elements.

Another example, in Haskell you can represent a red black/avl/b tree while also type encoding all the invariants.

For rbtree this type level encoding is about 7 short lines long.

Then all the dozens or hundreds of lines of code implementing the tree are guaranteed to maintain the invariants, no unit testing needed at all.

Another example, ST is a monad that let's you write deterministic imperative computations that can be wrapped with a pure interface (hiding the imperative innards). A small RankNType and a phantom type are used to tag both the ST computations and the mutable data, giving a compile time guarantee that mutable data doesn't leak between different computations. This guarantee means you can trust ST computations to actually be independent and thus the pure interface is safe.


Thanks! Some of those I knew from OCaml.


NullReferences in C# (and lots of other languages).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: