I've read a few times here on HN the saying "a well-typed program won't go wrong" or some slight variation. I don't think that can be correct, so I'm going to state my objection, and see if I'm right, or if someone can educate me.
Let's say I'm writing a recursive factorial function in Haskell. But, because it's early morning and the caffeine hasn't kicked in yet, I mess up the recursive call. I say "+ 1" instead of "- 1".
What is Haskell's type system going to say about this mistake? I believe it will say nothing at all, because the problem isn't the type of the value, it's the value of the value. But what's the program going to do? It's going to run until it exhausts memory, and then it's going to die in disgrace.
It seems to me that there are only four possibilities here:
1. Haskell's type system can in fact catch this error at compile time.
2. Despite its celebrated type system, Haskell is not a sufficient base for building a well-typed program, but some other language would be. (Coq? Agda? Some not-yet-existent language?)
3. Dying in disgrace rather than returning the correct value does not count as "going wrong".
4. The statement "a well-typed program won't go wrong" is false.
Of those options, I believe that number 4 is correct, but I am willing to listen to reasoned arguments to the contrary.
According to "Generalized Arrows"[1] by Adam Megacz Joseph the slogan "Well typed programs do not go wrong" was coined by type theorist Robin Milner and means something very specific: 'To "go wrong" means to exhibit undefined behavior. For example, programs written in strongly statically typed programming languages cannot crash, write to random memory locations, or read past the end of a buffer.' If you want precise details please read Milner's "A Theory of Type Polymorphism in Programming"[2].
Note that this does not imply that well typed functional programs do not contain bugs. Futhermore it only applies to a very specific ML-like language with a precisely defined denotational semantics. It is not known to apply to Haskell as no one has even written down the denotational semantics for Haskell. Furthermore it is trivially easy to write crashing programs in most Haskell implementations by taking advantage of any of a variety of non-standard features and functions.
Please let us not build up and knock down straw men about Haskell claiming to prevent all bugs.
[1] http://www.eecs.berkeley.edu/Pubs/TechRpts/2014/EECS-2014-13...
[2] http://tfeng.me/papers/milner78theory.pdf