Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Extensions to the Simply-Typed Lambda Calculus: Subtyping; Sum Types and Product Types

import Lait
#lait

\[ \newcommand{\Int}{\texttt{Int}} \newcommand{\Nat}{\texttt{Nat}} \newcommand{\Pos}{\texttt{Pos}} \]

Prelude: Correctly Handling Division

Let’s begin with an STLC.

\[ \begin{align*} e &::= x \mid n \mid e \oplus e \mid \lambda x : \tau. e \mid e\ e \\ \oplus &::= + \mid \times \mid / \\ n &::= 0 \mid 1 \mid 2 \mid \dots \end{align*} \]

Above, we write \(e \oplus e\) to make reference to a binary operator: for now, we will have addition, multiplication, and division. Here, division refers to integer division: thus 7 / 3 = 2. Crucially, if we divide by zero, this should raise a runtime error.

We will not fully specify the semantics of this language, since by now you can see how it would go. For example, multiplication would similarly need three rules: the “main” rule states what happens when we multiply two integer values \(n_1 \times n_2\), while two “helper” rules state what happens when we step \(e_1 \times e_2\) and \(n \times e\).

Division will work the same way; however, once we actually reach the rule for values, we will not let it step if the second argument is zero:

\[ \dfrac{}{n_1\ /\ n_2 \rightarrow n}\ \text{(E-Div)} \quad \text{where } n_2 \neq 0 \text{ and } n = \lfloor n_1 \div n_2 \rfloor \]

Division causes unsoundness with a naive type system. First, let’s observe that if our types are just like before: \[ \tau ::= \Nat \mid \tau \to \tau \] we can easily give types to addition and multiplication, as we have seen before. (Here, \(\Nat\) refers to the natural numbers.) Division is going to break things, though. If we had the below typing rule: \[ \dfrac{\Gamma \vdash e_1 : \Nat \quad \Gamma \vdash e_2 : \Nat} {\Gamma \vdash e_1\ /\ e_2 : \Nat}\ \text{(T-Div-Unsound)} \] Then we would now have an unsound type system. (Why?)

For the rest of today, we are going to study two solutions to this: one using subtyping, where we can prove statically that we never divide by zero; and another using sum types, where we can instead implement a “checked” division operator that enforces dynamically that division-by-zero never happens.

Subtyping

One possible response to the issue about T-Div-Unsound is that our types are too coarse: Nat is simply not informative enough to use division. To solve this, we can throw in a new type for positive integers: \[ \tau ::= \Nat \mid \Pos \mid \tau \to \tau \]

The idea now is that a given integer expression (e.g., \(1 + 2\ /\ 3\)) can actually be of one of two types: either Nat, if we don’t know that it is nonzero, or Pos, if we know that it must be nonzero. This can solve our problem, because we can say that division requires a Pos for the second argument. However, we now have a quite different-looking type system, as we now have two numeric types to cover integers, with one (Nat) a strict superset of the other (Pos). Thus, we want that every Pos is a Nat, but not vice versa.

Then, we can specify division as follows:

\[ \dfrac{\Gamma \vdash e_1 : \Nat \quad \Gamma \vdash e_2 : \Pos} {\Gamma \vdash e_1\ /\ e_2 : \Nat}\ \text{(T-Div)} \] Thus, T-Div states that given a natural number (possibly zero), and given a positive number, we can divide the first by the second. To make this work with the rest of our language, we need to answer two questions:

  • How should we type addition and multiplication?
  • How should subtyping work?

Typing Rules for Addition and Multiplication

First, let’s think about addition. When typing \(e_1 + e_2\), there are two possible types for each expression (either Nat or Pos); and thus four possible configurations of types for both. We can list out these possibilities:

  • If \(e_1\) and \(e_2\) are both Nat, we have no guarantee that the sum will be positive; thus, the result must have type Nat.
  • If either \(e_1\) or \(e_2\) have type Pos, then we know that \(e_1 + e_2\) must be Pos as well!. Indeed, since if \(n_1 > 0\) and \(n_2 \geq 0\), we have that \(n_1 + n_2 > 0\).

We can think of these two possibilities as two typing rules for addition. The first one will say that if we add Nat and Nat together, we get Nat; however, the second one will say that if we add Nat and Pos together, we get Pos. Let’s implement these:

\[ \dfrac{\Gamma \vdash e_1 : \Nat \quad \Gamma \vdash e_2 : \Nat} {\Gamma \vdash e_1 + e_2 : \Nat}\ \text{(T-Add1)} \]

\[ \dfrac{\Gamma \vdash e_1 : \tau_1 \quad \Gamma \vdash e_2 : \tau_2 \quad \tau_1, \tau_2 \in \{\Nat, \Pos\} \quad (\tau_1 = \Pos \lor \tau_2 = \Pos)} {\Gamma \vdash e_1 + e_2 : \Pos}\ \text{(T-Add2)} \]

The first rule is what we had before for addition. The second rule is new: it states that, if we are adding two integer types together, and one is a Pos, the result is a Pos.

We can now play the same game with multiplication:

  • If both arguments are Pos, multiplication should return a Pos.
  • If either is a Nat, multiplication should return a Nat.

Exercise: Write down our new formal typing rules for multiplication.

Typing Constants, and Using Subtyping

Now, let’s see how we should introduce numeric types; that is, how to give types to constant integers. For hardcoded program constants, we can just return the most informative type:

\[ \dfrac{\tau = \begin{cases} \Nat & n = 0 \\ \Pos & n > 0 \end{cases}} {\Gamma \vdash n : \tau}\ \text{(T-Num)} \]

However, this alone is not enough. While we can how say that \(\Gamma \vdash 5 : \Pos\), we might also want to treat \(5\) as a Nat. For example, we cannot yet type check this function: \[ (\lambda x : \Nat. x + 1)\ 5\] since Nat and Pos are not the same. To make this work, we introduce a judgment for subtyping, \( \tau_1 <: \tau_2 \), which says that \(\tau_1\) is a subtype of \(\tau_2\). Intuitively, this states that every value inside of \(\tau_1\) is also inside of \(\tau_2\). Thus, whenever we have a variable of type \(\tau_1\), it is also sound to treat it like a \(\tau_2\).

Let’s see the rules for numeric types, and the way we can use subtyping to make our example program type check:

\[ \dfrac{}{\tau <: \tau}\ \text{(Sub-Refl)} \qquad \dfrac{}{\Pos <: \Nat}\ \text{(Sub-Pos)} \qquad \dfrac{\Gamma \vdash e : \tau_1 \quad \tau_1 <: \tau_2} {\Gamma \vdash e : \tau_2}\ \text{(T-Sub)} \]

Above, (Sub-Refl) says that every type is a subtype of itself, (Sub-Pos) says that Pos is a subtype of Nat, while (T-Sub) says that expressions typed at subtypes can be reinterpreted at supertypes. Using these rules, we can now give a derivation tree to show our example is well-typed:

First, we type the body of the function:

\[ \dfrac{\dfrac{x : \Nat \in x : \Nat} {x : \Nat \vdash x : \Nat}\ \text{(T-Var)} \quad \dfrac{}{x : \Nat \vdash 1 : \Pos}\ \text{(T-Num)}} {x : \Nat \vdash x + 1 : \Pos}\ \text{(T-Add2)} \]

Then, we use this to type the whole program:

\[ \dfrac{\dfrac{x : \Nat \vdash x + 1 : \Pos} {\vdash \lambda x : \Nat.\ x + 1 : \Nat \to \Pos}\ \text{(T-Abs)} \quad \dfrac{\dfrac{}{\vdash 5 : \Pos}\ \text{(T-Num)} \quad \dfrac{}{\Pos <: \Nat}\ \text{(Sub-Pos)}} {\vdash 5 : \Nat}\ \text{(T-Sub)}} {\vdash (\lambda x : \Nat.\ x + 1)\ 5 : \Pos}\ \text{(T-App)} \]

The key step is (T-Sub): the argument \(5\) has type \(\Pos\), but the function expects a \(\Nat\), so we use subtyping to forget that \(5\) is positive.

Subtyping Functions

This is bonus material. While our subtyping judgment right now only relates numeric types, we can extend it to handle function types as well. For example, the below should type check:

\[ (\lambda f : \Nat \to \Nat.\ f\ 5)\ (\lambda x : \Nat.\ x + 1) \] Currently, this does not type check under our current rules. Why is this? Because while the first argument expects a function of type \(\Nat \to \Nat\), the right-hand side type checks under \(\Nat \to \Pos\), which is not the same. While we could force the second function to have type \(\Nat \to \Nat\), by using (T-Sub) inside of the function’s body, doing so is non-compositional; indeed, we are now changing the second function’s type just to make it work with the first one. Instead, we can make subtyping work for functions:

\[ \dfrac{\tau’_i <: \tau_i \quad \tau_o <: \tau’_o} {(\tau_i \to \tau_o) <: (\tau’_i \to \tau’_o)}\ \text{(Sub-Fun)} \]

This typing rule deserves some explanation. Recall that if \(\tau_i \to \tau_o\) is a subtype of \(\tau’_i \to \tau’_o\), then every variable of the first function type should work in the place where the second one is required. Let’s see some examples:

  • \((\Nat \to \Pos) <: (\Nat \to \Nat)\). This makes sense, because every \(\Pos\) is a \(\Nat\), and thus the function’s output can be coerced to the bigger type.
  • \((\Pos \to \Nat) <: (\Nat \to \Nat)\)? This does not make sense! Let’s see an example why. Consider the function \(\lambda y : \Pos.\ 10\ /\ y\), which has type \(\Pos \to \Nat\). If this subtyping held, then (T-Sub) would let the following program type check: \[ (\lambda g : \Nat \to \Nat.\ g\ 0)\ (\lambda y : \Pos.\ 10\ /\ y) \] But this program steps to \((\lambda y : \Pos.\ 10\ /\ y)\ 0\), and then to \(10\ /\ 0\), which is stuck. So our type system would be unsound.
  • However, \((\Nat \to \Nat) <: (\Pos \to \Nat)\) does make sense. For example, consider: \[ (\lambda g : \Pos \to \Nat.\ g\ 3)\ (\lambda x : \Nat.\ x + x) \] Any context expecting a \(\Pos \to \Nat\) function will only ever pass it \(\Pos\) arguments. Since every \(\Pos\) is a \(\Nat\), a function accepting any \(\Nat\) can handle them.

Thus, we see that when subtyping functions, we have that the output types need to subtype left-to-right, while the input types need to subtype right-to-left.

Sum Types

Let’s back up to STLC again:

\[ \tau ::= \Nat \mid \tau \to \tau \]

We will now investigate a different solution to our problem with division. Instead of using the type system to prove that division is safe, we will instead add extra types to allow division to become checked: if we try to divide, we will get an error that we can recover from. In particular, we will implement option types, making division now look like this:

divide : Nat -> Nat -> Option Nat

When we use division, we need to explicitly case split on whether division succeeded or not. For example, suppose we want to try to divide two numbers, and return 1234 if division fails. We can do it like this:

fun (x : Nat) (y : Nat) =>
    case divide x y of
    | Some v => v
    | None => 1234

To actually implement option types in the STLC, we will implement a more fundamental concept: sum types, which look like \( \tau_1 + \tau_2\). The basic idea is that a value of type \(\tau_1 + \tau_2\) is a tagged value of the form \(\texttt{inl}\ v\), where \(v \in \tau_1\), or \(\texttt{inr}\ v\), where \(v \in \tau_2\). We can implement option types using sum types, because we can also have a type for Unit (whose value is written ()), which stands for “no information”. Hence, we can then define

\[ \texttt{Option}\ \tau \mathrel{\overset{\text{def}}{=}} \tau + \texttt{Unit} \]

Then, the calculus will allow us to case split on if the value is inl (in which case it is Some), or inr (in which case it is None).

Sum Types in Lait

Whenever you use a type declaration with multiple constructors, you are using a sum type. For example: the type

type IntOrBool := | CInt (val : Int) | CBool (val : Bool)

can be represented using the sum type \(\texttt{Int} + \texttt{Bool}\). If we have three cases, it can be interpreted as a nested sum:

type OneOfThree := | First (val : Int) | Second (val : Int) | Third (val : Int) 

desugars to \(\texttt{Int} + \texttt{Int} + \texttt{Int}\). Now, if we have a simple enumerated type, such as

type Color := | Red | Green | Blue

this would be represented as a nested sum of units: \(\texttt{Unit} + \texttt{Unit} + \texttt{Unit}\).

However, note that sum types are not enough to reconstruct type defintions in Lait. If we have a recursive type, such as

type MyList := | MNil | MCons (val : Int) (res : MyList)

then we cannot represent this with a sum type alone; however, we can using a recursive type, which we may cover later.

Sum Types, Formally

Let’s now make it formal. Our types will now look like so:

\[ \tau ::= \Nat \mid \tau \to \tau \mid \tau + \tau \mid \texttt{Unit} \] while our expressions are going to include new ways of: introducing and eliminating sum types, via pattern matching; and introducing the Unit type:

\[ e ::= x \mid n \mid () \mid \lambda x : \tau.\ e \mid e\ e \mid e + e \mid e\ /\ e \mid \texttt{inl}\ e \mid \texttt{inr}\ e \mid \mathsf{case}\ e\ \mathsf{of}\ \{\texttt{inl}\ x \Rightarrow e;\ \texttt{inr}\ y \Rightarrow e\} \]

Let’s now see the new typing rules needed for Unit and sum types, before seeing how we can model checked division.

First, the unit value \(()\) always has type \(\texttt{Unit}\):

\[ \dfrac{}{\Gamma \vdash () : \texttt{Unit}}\ \text{(T-Unit)} \]

Next, we introduce sum types by tagging a value with \(\texttt{inl}\) or \(\texttt{inr}\):

\[ \dfrac{\Gamma \vdash e : \tau_1}{\Gamma \vdash \texttt{inl}\ e : \tau_1 + \tau_2}\ \text{(T-Inl)} \qquad \dfrac{\Gamma \vdash e : \tau_2}{\Gamma \vdash \texttt{inr}\ e : \tau_1 + \tau_2}\ \text{(T-Inr)} \]

Note that in (T-Inl), the type \(\tau_2\) does not appear in the premise: we are free to pick any \(\tau_2\) we like. (Similarly for \(\tau_1\) in (T-Inr).) This means that these rules are not syntax-directed; real type checkers fix this either by asking for a type annotation, or by inferring \(\tau_2\) from how the expression is used.

Finally, we eliminate sum types by case splitting. Both branches must have the same type \(\tau\), since we don’t know statically which branch will run:

\[ \dfrac{\Gamma \vdash e : \tau_1 + \tau_2 \quad \Gamma, x : \tau_1 \vdash e_1 : \tau \quad \Gamma, y : \tau_2 \vdash e_2 : \tau} {\Gamma \vdash \mathsf{case}\ e\ \mathsf{of}\ \{\texttt{inl}\ x \Rightarrow e_1;\ \texttt{inr}\ y \Rightarrow e_2\} : \tau}\ \text{(T-Case)} \]

Now, let’s see the rule for a checked division:

\[ \dfrac{\Gamma \vdash e_1 : \Nat \quad \Gamma \vdash e_2 : \Nat} {\Gamma \vdash e_1\ /\ e_2 : \Nat + \texttt{Unit}}\ \text{(T-Div-Checked)} \]

Correspondingly, division now never gets stuck at runtime. Instead, it returns \(\texttt{inl}\) of the result on success, and \(\texttt{inr}\ ()\) on division by zero:

\[ \dfrac{}{n_1\ /\ n_2 \rightarrow \texttt{inl}\ n}\ \text{(E-Div-Ok)} \quad \text{where } n_2 \neq 0 \text{ and } n = \lfloor n_1 \div n_2 \rfloor \qquad \dfrac{}{n_1\ /\ 0 \rightarrow \texttt{inr}\ ()}\ \text{(E-Div-Zero)} \]

And now we can see our example function from before, implemented in our calculus:

\[ \lambda x : \Nat.\ \lambda y : \Nat.
\mathsf{case}\ x\ /\ y\ \mathsf{of}\ \{\texttt{inl}\ v \Rightarrow v;\ \texttt{inr}\ z \Rightarrow 1234\} \]

This has type \(\Nat \to \Nat \to \Nat\): in the \(\texttt{inl}\) branch we have \(v : \Nat\), while in the \(\texttt{inr}\) branch we have \(z : \texttt{Unit}\), which we ignore.

Dynamic Semantics of Sum Types

Now, we need to give stepping rules to sum types. First, we need to let values step through inl and inr:

\[ \dfrac{e \rightarrow e’}{\texttt{inl}\ e \rightarrow \texttt{inl}\ e’}\ \text{(E-Inl)} \qquad \dfrac{e \rightarrow e’}{\texttt{inr}\ e \rightarrow \texttt{inr}\ e’}\ \text{(E-Inr)} \]

Now, when giving semantics to case, we have it behave like an if/then/else, with the exception that we also need to substitute in a new value when we pattern match.

\[ \begin{gathered} \dfrac{e \rightarrow e’} {\mathsf{case}\ e\ \mathsf{of}\ \{\texttt{inl}\ x \Rightarrow e_1;\ \texttt{inr}\ y \Rightarrow e_2\} \rightarrow \mathsf{case}\ e’\ \mathsf{of}\ \{\texttt{inl}\ x \Rightarrow e_1;\ \texttt{inr}\ y \Rightarrow e_2\}}\ \text{(E-Case)} \\[1.5em] \dfrac{} {\mathsf{case}\ (\texttt{inl}\ v)\ \mathsf{of}\ \{\texttt{inl}\ x \Rightarrow e_1;\ \texttt{inr}\ y \Rightarrow e_2\} \rightarrow e_1[x \mapsto v]}\ \text{(E-Case-Inl)} \\[1.5em] \dfrac{} {\mathsf{case}\ (\texttt{inr}\ v)\ \mathsf{of}\ \{\texttt{inl}\ x \Rightarrow e_1;\ \texttt{inr}\ y \Rightarrow e_2\} \rightarrow e_2[y \mapsto v]}\ \text{(E-Case-Inr)} \end{gathered} \]

Note that values of sum types are now \(\texttt{inl}\ v\) and \(\texttt{inr}\ v\), where \(v\) is itself a value.

Encoding Booleans via Sum Types

A central theme of this course is that many programming concepts can be implemented in more expressive concepts. Here, we will see that once we have a Unit type and sum types, we can implement booleans. To do this requires: defining the Bool type; defining expressions for true/false, and ensuring they have the correct types; as well as implementing if/then/else.

Observe that values of type Bool encode a single choice: either true or false. We can encode the same using a sum type, where both sides are Unit: \[ \texttt{Bool} \mathrel{\overset{\text{def}}{=}} \texttt{Unit} + \texttt{Unit} \] We can then define true and false accordingly:

\[ \texttt{true} \mathrel{\overset{\text{def}}{=}} \texttt{inl}\ () \qquad \texttt{false} \mathrel{\overset{\text{def}}{=}} \texttt{inr}\ () \]

Observe that, by unfolding definitions, we have that \(\vdash \texttt{true} : \texttt{Bool}\) by (T-Unit) and (T-Inl); similarly, \(\vdash \texttt{false} : \texttt{Bool}\). Finally, we can define if/then/else as follows:

\[ (\mathsf{if}\ e\ \mathsf{then}\ e_1\ \mathsf{else}\ e_2) \mathrel{\overset{\text{def}}{=}} (\mathsf{case}\ e\ \mathsf{of}\ \{\texttt{inl}\ \_ \Rightarrow e_1;\ \texttt{inr}\ \_ \Rightarrow e_2\}) \]

Observe that \((\mathsf{if}\ \texttt{true}\ \mathsf{then}\ e_1\ \mathsf{else}\ e_2) \to e_1\) by (E-Case-Inl).

By unfolding definitions, we can prove a derived typing rule for our version of if/then/else: \[ \dfrac{\Gamma \vdash e : \texttt{Bool} \quad \Gamma \vdash e_1 : \tau \quad \Gamma \vdash e_2 : \tau} {\Gamma \vdash \mathsf{if}\ e\ \mathsf{then}\ e_1\ \mathsf{else}\ e_2 : \tau}\ \text{(T-If)} \]

Product Types

Dual to sum types are product types, which let you put two pieces of information together into a single value. Given two types \(\tau_1\) and \(\tau_2\), the product type \(\tau_1 \times \tau_2\) contains all tuples of the form \( (v_1, v_2) \) where \(v_1\) is in \(\tau_1\) and \(v_2\) is in \(\tau_2\).

Product Types in Lait Observe that Lait has product types, as do most functional programming languages:

def p := (true, 3)

#eval fst p>> true
#eval snd p>> 3

Product types are the simplest way to model compound data. For example, if we have the following struct in C:

struct User {
    int id;
    char username[50];
    float balance;
}

We can think of this as being modeled (roughly) by a nested product type \(\texttt{Int} \times (\texttt{char[50]} \times \texttt{float}) \).

Product Types, Formally Hence, we will again extend our type grammar:

\[ \tau ::= \Nat \mid \tau \to \tau \mid \tau + \tau \mid \tau \times \tau \mid \texttt{Unit} \]

Then we will extend our expressions to have tuples, along with a way of reading from the first or second component of a tuple:

\[ e ::= \dots \mid (e, e) \mid \mathsf{fst}\ e \mid \mathsf{snd}\ e \]

Below are the relevant typing rules:

\[ \dfrac{\Gamma \vdash e_1 : \tau_1 \quad \Gamma \vdash e_2 : \tau_2} {\Gamma \vdash (e_1, e_2) : \tau_1 \times \tau_2}\ \text{(T-Pair)} \]

\[ \dfrac{\Gamma \vdash e : \tau_1 \times \tau_2} {\Gamma \vdash \mathsf{fst}\ e : \tau_1}\ \text{(T-Fst)} \qquad \dfrac{\Gamma \vdash e : \tau_1 \times \tau_2} {\Gamma \vdash \mathsf{snd}\ e : \tau_2}\ \text{(T-Snd)} \]

The dynamic semantics of products will encode that \( (v_1, v_2) \) is a value whenever \( v_1\) and \(v_2\) are both values. Below are the most important rules for products:

\[ \dfrac{}{\mathsf{fst}\ (v_1, v_2) \rightarrow v_1}\ \text{(E-Fst)} \qquad \dfrac{}{\mathsf{snd}\ (v_1, v_2) \rightarrow v_2}\ \text{(E-Snd)} \]

The rest of the stepping rules (e.g., what happens if we have a general \((e_1, e_2)\) follow our recipe from before.

Exercise. Write down the rest of the stepping rules for products. We will get two extra rules for stepping tuples, and one extra rule each for \(\mathsf{fst}\) and \(\mathsf{snd}\).

The Algebra of Algebraic Types

Put together, sum types and product types form a very simple model of algebraic data types, which is the way that Lait models data types. For example, let’s now consider the following type in Lait:

type Shape := | Rect (width : Int) (height : Int) | Circle (radius : Int) 

This can be modeled via the following type: \[ (\texttt{Int} \times \texttt{Int}) + (\texttt{Int}) \]

You might be wondering: why are they called sum and product types? There is a very fun correspondence between sum and product types and ordinary sums and products on numbers. Let’s have a very minimal type grammar:

\[ \tau ::= \texttt{Unit} \mid \tau \to \tau \mid \tau + \tau \mid \tau \times \tau \]

We will consider an ordinary STLC that has unit, sums, products, and functions:

\[ e ::= x \mid () \mid \lambda x:\tau. e \mid e\ e \mid (e, e) \mid \mathsf{fst}\ e \mid \mathsf{snd}\ e \mid \mathsf{inl}\ e \mid \mathsf{inr}\ e \mid \mathsf{case}\ e\ \mathsf{of}\ \{\texttt{inl}\ x \Rightarrow e;\ \texttt{inr}\ y \Rightarrow e\} \]

Now, let’s think about the possible closed programs \(e\) that inhabit each type; that is, given a type \(\tau\), which programs \(e\) are such that \(\vdash e : \tau\)? A remarkable fact is that, up to observational equivalence (discussed in Lecture 5), there are only finitely many closed terms of each type! Moreover, we can count them: for each type \(\tau\), we can compute its cardinality \(|\tau|\).

First, consider Unit. Up to the number of steps the program takes, there is really only one program, (), that inhabits Unit. Other programs, such as (fun x : Int => ()) 5 also have type Unit, but they are observationally equivalent: meaning that the only observable difference between them is the number of steps they take to reach a value. Put another way, in our calculus the cardinality of the Unit type is just 1:

\[ |\texttt{Unit}| = 1 \]

Now, consider \(\tau_1 \times \tau_2\). How many programs inhabit \(\tau_1 \times \tau_2\)? Well, values of this type are pairs of programs that inhabit \(\tau_1\) and \(\tau_2\), respectively. This means that the cardinality of the product type is the product of the cardinalities:

\[ |\tau_1 \times \tau_2| = |\tau_1| \times |\tau_2|\]

Next, consider \(\tau_1 + \tau_2\). This is where we can think about nontrivial cardinalities. Consider Unit + (Unit + Unit). How many values are there here? There are three: inl (), inr (inl ()), and inr (inr ()). This means that the cardinality of the sum is the sum of the cardinalities:

\[ |\tau_1 + \tau_2| = |\tau_1| + |\tau_2|\]

Exercise. Remember that we can define Bool = Unit + Unit. What is the cardinality of Bool?

Exercise. What is the cardinality of (Bool + Bool) * Bool? Can you write down all of the values of this type?

Finally, consider function types. It turns out that, in our minimal calculus with finite types, the cardinality of the function type is the exponent of the cardinalities:

\[ |\tau_1 \to \tau_2| = |\tau_2|^{|\tau_1|} \]

To gain intuition for this, consider enumerating all of the possible functions from Bool + Unit to Bool. For each value in the domain, we can either assign it false or true. Hence, there are \(2^3 = 8\) possible functions total.

Exercise. What is the cardinality of (Bool -> Bool) -> Bool?