In the previous post, we added functions to Hatsugen.
In this post, we’ll add product types. These are often called “structs”, “records”, or “tuples” in real programming languages.
A product type is a combination of multiple types. For instance, if we want to return multiple values from a function, we can have the function’s return type be a product type.
A product type that combines two types is called “pair” and denoted with τ1×τ2. The expression ⟨e1,e2⟩ is a pair literal expression.
Note that by combining pair types with other pair types, we can effectively construct an product type combining n types for any n>2.
To use a pair, we must be able to extract the values inside. For that, we add the projection expressions e⋅L and e⋅R. When e is a pair, these projections extract the left and right value out of the pair respectively.
We will also introduce a product type that combines no other types, called “unit” and denoted with 1. There is just 1 value of this type, also often called “unit”, and it is written ⟨⟩.
Because the unit type has only one value, it may not seem very useful. However, it can be useful when we want to return “nothing” from a function.
For instance, in most programming languages, functions can perform side effects. Side effects are anything the function does other than return a value, like modify files or access the Internet.
In fact, sometimes functions are only useful because of the side effects they perform, and they don’t actually need to return anything useful. In these cases, it is convenient to have these functions return unit.
Different languages call unit different things:
- C, C++, Java has
void
- Python has
None
- JavaScript has
undefined
- Ruby has
nil
τ::= ∣ ∣ e::= ∣ ∣ ∣ ∣ …1τ1×τ2…⟨⟩⟨e1,e2⟩e⋅Le⋅R
The unit value has unit type.
Γ⊢⟨⟩:1Given two expressions each with their own type, we can make a pair of those types by assembling the expressions together.
Γ⊢⟨e1,e2⟩:τ1×τ2Γ⊢e1:τ1Γ⊢e2:τ2We can then project the left or right part out of the pair.
Γ⊢e⋅L:τ1Γ⊢e:τ1×τ2Γ⊢e⋅R:τ2Γ⊢e:τ1×τ2
The unit is a value.
⟨⟩ valA pair is a value when both constituent expressions are values.
⟨e1,e2⟩ vale1 vale2 valIf the left expression in a pair can step, so can the entire pair.
⟨e1,e2⟩↦⟨e1′,e2⟩e1↦e1′After the left expression in a pair is a value, we may step the right expression if possible.
⟨e1,e2⟩↦⟨e1,e2′⟩e1 vale2↦e2′For the projections, we must first step the pair to a value. Then, once it is a value, it will be a pair literal, and we may step to either the left or right value in the pair.
e⋅L↦e′⋅Le↦e′⟨e1,e2⟩⋅L↦e1⟨e1,e2⟩ vale⋅R↦e′⋅Re↦e′⟨e1,e2⟩⋅R↦e2⟨e1,e2⟩ val
The helper judgments must also be updated. We can update them rather mechanically.
[x↦e]⟨⟩=⟨⟩[x↦e]⟨e1,e2⟩=⟨e1′,e2′⟩[x↦e]e1=e1′[x↦e]e2=e2′[x↦e]e1⋅L=e1′⋅L[x↦e]e1=e1′[x↦e]e1⋅R=e1′⋅R[x↦e]e1=e1′
fv(⟨⟩)=∅fv(⟨e1,e2⟩)=s1∪s2fv(e1)=s1fv(e2)=s2fv(e⋅L)=sfv(e)=sfv(e⋅R)=sfv(e)=s
Before we conclude, let us consider the etymology of “product type”.
Product types are so named because the number of values in a product type is the product of the number of values in the constituent types.
For instance, consider the type Bool. It has 2 values, true and false.
Now consider the type Bool×Bool. It has 2×2=4 values:
- ⟨true,true⟩
- ⟨true,false⟩
- ⟨false,true⟩
- ⟨false,false⟩
Consider also the type Bool×1, the product of Bool and unit. It has 2×1=2 values:
- ⟨true,⟨⟩⟩
- ⟨false,⟨⟩⟩
This is why it makes sense that the unit type is written 1. It is the identity of the operation written ×.
For the integers, we have that for all integers a,
a×1=awhere × denotes multiplication.
Then similarly, if we write ∣τ∣ to mean “the number of values in the type τ”, we have that for all types τ,
∣τ×1∣=∣τ∣where × denotes a product type.
And more generally, for all types τ1,τ2, we have
∣τ1×τ2∣=∣τ1∣×∣τ2∣where on the left, × denotes a product type, and on the right, it denotes multiplication.
The proofs are once again on GitHub.
In the next post, we’ll add sum types, also known as tagged unions.