In the previous post, we added product type to Hatsugen.
In this post, we’ll add sum types.
A sum type is a choice between multiple types. For instance, if we have a function from which we would sometimes like to return one type, and sometimes another, we can have the function return a sum type of those two types.
Or, suppose we wish to represent sometimes having a value of some type, and sometimes having “nothing”. We can use a sum type of the actual type and the unit type, and use unit to mean “nothing”.
Surprisingly, although many programming languages have product types, not as many have sum types as we will present them here. But, many programming languages that support a “functional” style, like Haskell, OCaml, Standard ML, and Rust, do have sum types.
First we have the binary sum type, denoted with and called “either”. As the name suggests, values of this type can either be from the left type or the right type .
However, the values must be “tagged” with whether they are the left or right type. We add two new expressions, and , to do this tagging. These are sometimes called “injections”.
The sum type that is a choice between no types is written and called “never”, because we can never have a value of this type.
Although it may seem odd to have a type with no values, the never type can be useful. For instance, imagine we added the ability to “exit” a Hatsugen program with a special function. This is a common feature in programming languages:
Note that this function has the property that it never returns to the caller, since calling it exits the program.
To communicate this, we can have the return type of this function be the never type. When we see that a function returns the never type, we know that the function will indeed never return. Since, if it returned, we would have produced a value of type never, which is impossible.
Indeed, in Sorbet, the static type-checker for Ruby developed at Stripe, the equivalent of the never type is called
Finally, we add the ability to actually use a sum type with case expressions. These case expressions must handle all of the possibilities that the value of sum type could be.
For the either type, there are two cases: left or right. For the never type, there are no cases to handle.
The left injection requires its argument be the left type in the either, but the right type can be any type.
Similar with the right injection.
The empty case requires that its “head” be never type, but permits the result of the case to be any type. This is because we know it is impossible to have a value of never type, so if we achieved the impossible by producing a never, we can do anything we want.
The either case requires its head be an either type. Then, each respective case binds a variable with the contents of the head. These variables are allowed to be used when evaluating the respective subexpressions for each case. Each of these subexpressions must be the same type. Then, the whole case expression evaluates to that type.
The left injection is a value if its argument is a value.
Same with the right.
If the left injection’s argument steps, then the whole injection steps.
And same as the right.
If the empty case’s head can step, so can the whole case.
And same with the binary case.
When the binary case’s head is a value, and it is the left injection, we extract the argument, bind it to the left variable, and step into the left expression.
And similarly with the right.
Just as last time, updating the helper judgments is a mostly mechanical process.
For the binary case, we cheat a bit and re-use the definition of substitution for function literals.
This is convenient since each of the two cases behave similarly to a function, since they bind one variable and evaluate to one expression each. It also lets us re-use the logic for function literals that handles checking whether the bound variable shadows (aka is the same as) the variable to be substituted.
We write and for the type of the arguments on the functions we construct, but it doesn’t matter for the purposes of substitution.
Sum types, similarly to product types, are so named because of how the number of values in the sum or product type relates to the number of values in the other types.
For instance, :
Indeed, and .
Duality of sums and products
Sums and products are duals.
To construct a pair of two types, one must provide a value of both types. Then, when using a pair, one may get out either one of the two types.
To construct an either of two types, one may provide a value of either one of the two types. Then, when using an either, one must handle both types.
The proofs are once again on GitHub.
I think this is the end of this little series, at least for now. I’ll probably continue to write about programming language related topics, but not specifically by adding to Hatsugen.