There are some calculations that involve recursion, and those might never terminate. We can’t just ban non-terminating functions from Haskell because distinguishing between terminating and non-terminating functions is undecidable — the famous halting problem.

That’s why computer scientists came up with a brilliant idea, or a major hack, depending on your point of view, to extend every type by one more special value called the bottom and denoted by _``*|*``_, or Unicode .

This “value” corresponds to a non-terminating computation.

So a function declared as:

f :: Bool -> Bool 

may return True, False, or |; the latter meaning that it would never terminate. Interestingly, once you accept the bottom as part of the type system, it is convenient to treat every runtime error as a bottom, and even allow functions to return the bottom explicitly. The latter is usually done using the expression undefined, as in:

f :: Bool -> Bool 
f x = undefined

Functions that may return bottom are called partial, as opposed to total functions, which return valid results for every possible argument.


Novice: “So is bottom sort of like null?”

Master: “No, they couldn’t be more different! Null is a value; bottom is not. There is one null value; there are no bottom values.”

https://chris-martin.org/2017/null-as-bottom


🌱 Back to Garden