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

Types III: Type Soundness

In this lecture we will (1) more deeply explore the consequences and properties of types and (2) grow our type system. First, we will make precise what it means for a type system to be sound, and see the standard technique for proving soundness. Then, we will put all of our formal tools together to quickly prototype a new language with mutable state, and see how mutable state changes what our type system guarantees.

import Lait
#lait

Type Soundness

One of our key properties of type systems is that they are sound: a sound type checker will reject programs that cause runtime errors in our interpreters. This property is best summarized by the famous quote from Robin Milner that “well-typed programs don’t go wrong”.

How can we establish that a type checker is sound? Well, one way is to very thoroughly test our type checker implementation with lots of example programs. This can work, especially for fairly small programming languages, but as our languages get increasingly complex it starts to become very challenging to design sufficiently comprehensive test suites to cover all possible programs.

An alternative to exhaustive testing of our type checkers is to formally prove that our type checker eliminates all bad programs. This technique of proving type checkers formally sound is a pillar of type system design.

What does it mean to “formally prove a type system sound”? Well, consider IfLang. Recall its small-step semantics from the formalism lecture:

\[ \dfrac{}{n_1 + n_2 \rightarrow n}\ \text{(E-Add1)} \quad \text{where } n = n_1 + n_2 \]

\[ \dfrac{e_1 \rightarrow e_1^{\prime}}{e_1 + e_2 \rightarrow e_1^{\prime} + e_2}\ \text{(E-Add2)} \qquad \dfrac{e_2 \rightarrow e_2^{\prime}}{n + e_2 \rightarrow n + e_2^{\prime}}\ \text{(E-Add3)} \]

\[ \dfrac{}{\mathsf{if}\ \mathsf{true}\ \mathsf{then}\ e_1\ \mathsf{else}\ e_2 \rightarrow e_1}\ \text{(E-IfT)} \qquad \dfrac{}{\mathsf{if}\ \mathsf{false}\ \mathsf{then}\ e_1\ \mathsf{else}\ e_2 \rightarrow e_2}\ \text{(E-IfF)} \]

\[ \dfrac{e_1 \rightarrow e_1^{\prime}}{\mathsf{if}\ e_1\ \mathsf{then}\ e_2\ \mathsf{else}\ e_3 \rightarrow \mathsf{if}\ e_1^{\prime}\ \mathsf{then}\ e_2\ \mathsf{else}\ e_3}\ \text{(E-IfStep)} \]

and its typing rules from the previous lecture, which we now write using the same concrete syntax:

\[ \dfrac{}{\vdash n : \texttt{Num}}\ \text{(T-Num)} \qquad \dfrac{}{\vdash b : \texttt{Bool}}\ \text{(T-Bool)} \qquad \dfrac{\vdash e_1 : \texttt{Num} \quad \vdash e_2 : \texttt{Num}} {\vdash e_1 + e_2 : \texttt{Num}}\ \text{(T-Plus)} \]

\[ \dfrac{\vdash g : \texttt{Bool} \quad \vdash e_1 : \tau \quad \vdash e_2 : \tau} {\vdash \mathsf{if}\ g\ \mathsf{then}\ e_1\ \mathsf{else}\ e_2 : \tau}\ \text{(T-If)} \]

Suppose \(e\) is a well-typed IfLang program. It might take many steps before it reaches a value, \(e \rightarrow e_1 \rightarrow e_2 \rightarrow \cdots \rightarrow v\). Recall that the way that we encode errors in our formal semantics is by getting stuck, meaning that there is a term that is not a value but has no applicable small-step rule. Our goal is that no matter how many steps a well-typed program takes, it never reaches a stuck term. This leads to our notion of formal type soundness:

Theorem (Type Soundness of IfLang). Let \(e\) be an IfLang program with \(\vdash e : \tau\). Then, for any sequence of steps \(e \rightarrow e_1 \rightarrow \cdots \rightarrow e_n\) (with \(n \geq 0\)), \(e_n\) is not stuck.

Up until now, we have just given you many definitions relating to programming languages. The above theorem shows us the true power of programming languages: we can prove theorems that universally quantify over programs. Thus, no matter how large or complex the program is — it could be a million lines of code — as long as it’s well typed, it can’t get stuck.

Proving Type Soundness Syntactically

This technique for proving type soundness was introduced in: Wright, Andrew K., and Matthias Felleisen. "A syntactic approach to type soundness." Information and Computation 115.1 (1994): 38-94. If you want more details on this proof technique, see Chapters 8 and 9 of Types and Programming Languages.

Establishing the type soundness of type systems is one of the core themes of programming languages research and development, and has animated much of the formal development of languages. To prove type soundness, we will follow a particular recipe, called progress and preservation.

First, this recipe requires us to prove a progress lemma, which states that every well-typed program can step, unless it’s a value:

Lemma (Progress). For any well-typed IfLang program \(e\), either (1) \(e\) can take a single step, or (2) \(e\) is a value.

Next, we need to separately prove preservation, which says that if we step from a well-typed expression, the result is well typed:

Lemma (Preservation). If \(\vdash e : \tau\) and \(e \rightarrow e^{\prime}\), then \(\vdash e^{\prime} : \tau\).

Progress says that well-typed terms are not stuck right now. Preservation says that being well-typed is maintained by taking a step, so that we can apply progress again after taking a step. Together, these two lemmas imply type soundness, and hence establish that our small-step interpreter never encounters runtime errors.

Proof of Type Soundness. Using Progress and Preservation, we can prove our Type Soundness theorem.

  • Suppose \(\vdash e : \tau\) and \(e \rightarrow e_1 \rightarrow \cdots \rightarrow e_n\). Our job is to show that \(e_n\) is not stuck.
  • First, show that \(\vdash e_n : \tau\) — that is, \(e_n\) is well-typed. This is by induction on \(n\).
    • Base case (\(n = 0\)): No steps were taken, so \(e_n = e\). By assumption, \(\vdash e : \tau\).
    • Inductive case (\(n = k + 1\)): Our inductive hypothesis says that \(\vdash e_k : \tau\). Since \(e_k \rightarrow e_{k+1}\), preservation tells us that \(\vdash e_{k+1} : \tau\).
  • Now, Progress tells us immediately that \(e_n\) is either a value or can take a step; thus, it is not stuck by definition.

Proof of the Progress Lemma

Type soundness isn't everything. There are many widely-used type systems in the wild today that are not type sound, including TypeScript and Java. Nonetheless, type soundness has proven to be a useful "north star" for type systems and has been influential in designing practical type systems. There are also richer notions of type safety than the syntactic notion of type safety that we discuss here: see this blog post.

Let’s see an example of why one of these two lemmas is true: we will prove the progress lemma. Our argument will be by induction on the typing derivation tree of \(\vdash e : \tau\). We do case analysis on the last rule used in the derivation; in each case, our inductive hypothesis says that the progress lemma holds for every premise of that rule. Our goal is to show that in every case \(e\) is either a value or it can take a step:

  • (T-Num). In this case, \(e\) must be a number, which is a value.
  • (T-Bool). In this case, \(e\) must be a Boolean, which is a value.
  • (T-If). In this case, \(e = \mathsf{if}\ g\ \mathsf{then}\ e_1\ \mathsf{else}\ e_2\), and \(\vdash g : \texttt{Bool}\). Our inductive hypothesis says that \(g\) is either a value or can take a step. We do case analysis on these two possibilities:
    • Case 1: \(g\) is a value. By the Boolean canonical forms theorem, since \(\vdash g : \texttt{Bool}\), \(g\) is either \(\mathsf{true}\) or \(\mathsf{false}\). If it is \(\mathsf{true}\), \(e\) steps using the (E-IfT) rule; if it is \(\mathsf{false}\), \(e\) steps using the (E-IfF) rule.
    • Case 2: \(g\) can step, \(g \rightarrow g^{\prime}\). In this case, \(e\) steps using the (E-IfStep) rule.

Notice that we never needed the inductive hypotheses for the branches \(e_1\) and \(e_2\): the semantics of if never evaluates the branches until the guard has been evaluated.

The proof for (T-Plus) is very similar to the case for (T-If), so we leave it as an exercise.

Proof of Preservation

While we won’t fully prove preservation in class, we can at least see how the proof would go. Recall that preservation says that if \(\vdash e : \tau\) and \(e \rightarrow e’\), then \(\vdash e’ : \tau\).

The proof is also by induction on the typing derivation for \(\vdash e : \tau\). In each case, we then additionally have the information that \(e \rightarrow e’\). Since we are inducting on the typing judgment, this will tell what \(e\) is, and hence what \(e’\) can be.

Preservation: T-Plus

Let’s look at the case for addition (T-Plus); thus, we have that \(\vdash e_1 + e_2 : \texttt{Num}\), and thus that \(\vdash e_1 : \texttt{Num}\) and separately \(\vdash e_2 : \texttt{Num}\). By induction, we also have the progress lemma for \(e_1\) and \(e_2\). Thus, we also have:

  • If \(e_1 \rightarrow e_1’\), then \(\vdash e_1’ : \texttt{Num}\); and
  • If \(e_2 \rightarrow e_2’\), then \(\vdash e_2’ : \texttt{Num}\).

We then need to proceed by all of the possible ways that \(e = e_1 + e_2\) can step.

  • Case (E-Add1): We learn that \(e_1 = n_1\) and \(e_2 = n_2\), and \(e’ = n_3\) where \(n_3 = n_1 + n_2\). We have that \(\vdash e’ : \texttt{Num}\) by (T-Num).
  • Case (E-Add2): We learn that \(e_1 \rightarrow e_1’\), and \(e’ = e_1’ + e_2\). We have that \(\vdash e_1’ : \texttt{Num}\) by our inductive hypothesis, and \(\vdash e_2 : \texttt{Num}\) by assumption. Thus, by (T-Add), we have that \(\vdash e’ : \texttt{Num}\).
  • Case (E-Add3): We learn that \(e_1 = n\) and \(e_2 \rightarrow e_2’\), and \(e’ = n + e_2’\). We have that \(\vdash n : : \texttt{Num}\) by (T-Num) and \(\vdash e_2 : \texttt{Num}\) by our inductive hypothesis; thus, \(\vdash e’ : \texttt{Num}\) by (T-Add).

Exercise. Prove the preservation lemma for the case where \(e = \mathsf{if}\ g\ \mathsf{then}\ e_1\ \mathsf{else}\ e_2\). (Hint: do case analysis on which step rule was used to derive \(e \rightarrow e^{\prime}\).)