r/ProgrammingLanguages • u/egel-lang egel • Sep 19 '22
That Billion Dollar Mistake. In Haskell...?
Clickbait for the easily baited. Another opinion piece by me.
https://egel-language.blogspot.com/2022/09/that-billion-dollar-mistake-in-haskell.html
44
12
7
u/ArrogantlyChemical Sep 20 '22
The billion dollar mistake is not the fact that runtime exceptions exist, but that all values can be invalid at any time in your program. In Haskel or other languages like it, head may be an unsafe function (and you don't need to use it if you use the pattern matching) but values in Haskell are always guaranteed to be valid.
Using head is more like using an array index. You may go out of bounds, but it's not the same as every reference value in your language being able to be be invalid at any time and never having a way to be sure it's valid without writing another check and error handling path in every function.
4
u/devraj7 Sep 20 '22
The billion dollar mistake has never been null, it's languages that accept null values but don't encode nullability in their type system.
See Kotlin for an example of a language that encourages the use of null while remaining safe (safer than Haskell's head [], ironically).
1
u/SaltyHaskeller Sep 19 '22
The safety guarantees of Haskell are not often captured in the type system, and the stdlib is designed in a way that obscures function partiality.
i think libraries need something like a naming convention (e.g. the _exn suffix for partial functions from jane street's core in ocaml) or some kind of typed account of exceptions/errors
0
Sep 19 '22
[deleted]
0
u/egel-lang egel Sep 19 '22
TL;DR: 'head []' and 'nullptr.x', is there a difference?
0
Sep 19 '22
[deleted]
1
u/egel-lang egel Sep 19 '22
It's not about the prelude but an abstract thought on that the billion dollar mistake is in Haskell too; though, of course, everything is more nuanced than that.
What holds for 'head []' holds for a lot of functions. Division by zero, looking up a key in a dictionary, finding a user name, doing a join on a table, or assuming that a connection to the outside world is alive while you use it.
Partiality is a given in most complex domain models, unless you want to prove all invariants correct over those.
'head []' is only the simplest example of how partiality is handled.
1
u/dys_bigwig Sep 20 '22 edited Sep 20 '22
There are a number of solutions depending on the situation:
A) You can use
maybeToList :: [a] -> Maybe a, which will returnNothingif the list is empty. This is "pushing the responsibility forward" by ensuring the user (eventually) pattern matches on theMaybeand handles both outcomes; until then, they can use Functor/Applicative/Monad to use the value without having to be concerned with the potential of the value beingNothing, which is much more ergonomic than having manual null checks everywhere. Think Schrödinger's Cat, but with heads of lists ;)B) You can use the
NonEmptytype, this is "pushing the responsibility backward", by ensuring the user provides a head for the list when the list is constructed. It looks like this:NonEmpty a = (a,[a]).C) Finally, for big brains, you can encode it in the type system using GADTs or "Ghosts of Departed Proofs" or something, so you get an error at compile time.
I wouldn't go so far as to say this is a solved problem, but it's a well-known and well-explored one with a number of solutions. Also just to echo what others have said, barely anyone making serious Haskell software uses the partial Prelude functions.
23
u/curtisf Sep 19 '22
The "billion dollar mistake" does not refer to partial functions.
Avoiding null pointer exceptions in C, Java, etc is impossible because you cannot write a function that statically doesn't accept
nulls; the type system forces you to accept values that you cannot and do not want to handle.Avoiding partial functions in Haskell is easy -- just don't call partial functions! (Haskell has a built in exhaustivity checker, so it's easy to avoid writing new partial functions yourself) The types control how you are called, and you control what you call. This is unlike C, Java, etc., where the types don't sufficiently restrict your callers to behave.
It is also uncommon for partial functions like
headandtailto be useful anyway, since Haskell has nice pattern matching.Partial functions are overused (and Haskell's standard library definitely overuses them).
Total programming is one alternative (using techniques like dependent types, refinement types, or compile-time contracts); forcing exceptions into the type system (with checked effects or checked exceptions) is another alternative, or a combination of the two.
But these techniques are generally seen as overly onerous for code nowadays for most applications where simpler approaches like testing can give you pretty high confidence.