Theoremcore
Type soundness
Also called progress and preservation, well-typed programs do not go wrong.
A well-typed closed term never reaches a stuck state. For the simply typed lambda calculus it follows from progress (a value or a step) and preservation (a step keeps the type).
Type soundness is the theorem that a well-typed program cannot reach a state that the semantics leaves undefined. Milner (1978) stated it as "well-typed expressions do not go wrong" for a denotational semantics. Wright and Felleisen (1994) gave the syntactic form that most papers now use: it splits soundness into progress and preservation, two lemmas proved by induction on typing derivations. This page states and proves the theorem for one system, the simply typed lambda calculus with call-by-value small steps. Other systems follow the same plan with more lemmas.
Statement
The system is the one defined on the simply typed lambda calculus page: base types and , the arrow, conditionals and integer addition, with the typing rules T-Var, T-Abs, T-App, T-True, T-False, T-If, T-Int and T-Add, and the call-by-value small-step rules E-App1, E-App2, E-AppAbs, E-IfTrue, E-IfFalse, E-If, E-Add1, E-Add2 and E-AddInt. The values are the abstractions, , and the integer literals. A closed term is stuck if it is not a value and no rule gives it a next step.
Theorem (type soundness). Let be a closed term.
- Progress. If , then is a value or there is a with .
- Preservation. If and , then .
- Safety. If and , then is not stuck. If is a value, then .
In plain words
Think of the program as a machine that rewrites itself one small step at a time. It can fail in only one way: it reaches a point where the next operation gets the wrong kind of value, such as adding a truth value to a number. The theorem says that this never happens to a program that passes the type checker. The proof has two halves. The first says that a typed program is either finished or can make one more step. The second says that a step never changes the type. Put the two together, step by step, and the program stays typed and never gets stuck. Both halves depend on the exact rules for running the program, so the guarantee is only as strong as those rules.
Proof
The proof file has the full proof. It uses four lemmas, in the order of Section 9.3 of Pierce's book.
- Inversion. The rules are syntax directed, so the form of a typed term fixes the last rule of its derivation and the types of its parts. For example, if , then and for some .
- Canonical forms. A closed value of type is or , a closed value of type is a literal, and a closed value of type is an abstraction . This holds because the three kinds of values get three different kinds of types.
- Permutation and weakening. Reordering the context, or adding a fresh variable to it, keeps every typing.
- Substitution. If and , then .
Progress goes by induction on the derivation of . T-Var cannot occur in the empty context. The value rules give values. For T-App, the induction hypothesis makes the function either step (E-App1) or a value, then the argument either step (E-App2) or a value, and in the last case canonical forms make the function an abstraction, so E-AppAbs applies. T-If and T-Add are the same, with canonical forms for and .
Preservation goes by induction on the derivation of , with a case for each step rule. The congruence rules (E-App1, E-App2, E-If, E-Add1, E-Add2) use the induction hypothesis on the part that steps. E-IfTrue, E-IfFalse and E-AddInt are direct. The one real case is E-AppAbs: inversion gives and , and the substitution lemma gives .
Safety follows by induction on the number of steps: preservation keeps the type along , and progress applied to says it is a value or steps.
Example of the E-AppAbs case. The term steps to . Inversion on the typing of the redex gives , and . The substitution lemma gives . The trace of on the STLC page has six steps, and each of the seven terms in it has type .
Consequences
- Erasure of run-time checks. A compiler for a sound language need not test the kind of a value before an operation. Canonical forms says the test would always pass.
- Termination is separate. Soundness says nothing about termination. In the STLC every run ends, by strong normalization (see the STLC page), so a closed term of type ends in a value of type . With a fixed-point operator, a well-typed term may run forever, and soundness still holds, because an infinite run never gets stuck.
- Other systems. The same plan proves soundness for many richer systems, each with extra lemmas.
- Subtyping: preservation may lower the type to a subtype, and canonical forms needs inversion lemmas for the subtype relation (Pierce, Chapter 15).
- System F and Hindley-Milner: a lemma on type substitution, , handles type application and instantiation.
- References: the typing judgment gets a store typing , and preservation says the new store typing extends the old one (Pierce, Chapter 13). With let-polymorphism, references break preservation unless generalization is restricted, which is the value restriction.
- Exceptions: progress allows a third outcome, an uncaught exception (Pierce, Chapter 14).
- Recursive types: canonical forms for types needs fold and unfold, or a coinductive type equality.
- Mechanized proofs. Because the proof is a set of inductions on derivations, it is a standard benchmark for proof assistants, and full languages have mechanized soundness proofs, for example the definition of Standard ML in Twelf and the WebAssembly specification.
- A link to program logic. Soundness says that the type of a term is an invariant of its run. Program logics such as the Hoare triple prove invariants that the programmer chooses. A type system proves one fixed, weak invariant for all programs, and it does so without help.
Tightness
Soundness depends on the semantics and on the meaning of "stuck". Add integer division with a rule T-Div that gives , and a step rule E-DivInt for only when . Then has type , it is not a value, and no rule steps it. A script with a stepper for this extension confirms that is stuck. So progress fails. There are two standard repairs. One adds a rule and lets progress allow the outcome , as with exceptions. The other leaves the term stuck and accepts that the theorem no longer covers it. Most real languages take the first repair, so a sound language can still fail at run time, with a defined error. The theorem rules out only the failures that the chosen semantics calls stuck.
Progress needs closed terms. The term has type in the context , but it is not a value and does not step.
A sound system rejects safe programs. The term steps to the value in one step and never gets stuck. But it has no type, because T-If needs both branches to have the same type. No sound and decidable type system for a Turing-complete language can accept exactly the safe programs (see type system). The same example shows that the converse of preservation, subject expansion, fails: has type , but the term that stepped to it has no type.
Each rule matters. If T-If did not require , the stuck term would be typed, and progress would fail. If let-bound references were generalized without the value restriction, preservation would fail. Real languages have had soundness bugs of this kind. Amin and Tate (2016) gave Java and Scala programs that pass the type checker and then fail with a cast error where none was written, by combining null with wildcard or path-dependent types.
Proof: Progress and preservation for the simply typed lambda calculus with Booleans and integersfull
This file proves the statement on the page for the system defined on the simply typed lambda calculus page: types , and ; terms , , , , , , integer literals and ; the typing rules T-Var, T-Abs, T-App, T-True, T-False, T-If, T-Int and T-Add; and the call-by-value rules E-App1, E-App2, E-AppAbs, E-IfTrue, E-IfFalse, E-If, E-Add1, E-Add2 and E-AddInt. The values are , , and the literals .
Terms are taken up to the renaming of bound variables. We use the variable convention: in any term, context or rule instance under discussion, the bound variables are chosen different from each other and from the free variables and the variables of the context. Substitution is capture avoiding. A context is a list of distinct variables with types, and we write for membership.
The proof follows the order of Pierce, Types and Programming Languages, Section 9.3.
Lemma 1 (inversion)
- If , then .
- If , then for some with .
- If , then there is a with and .
- If or , then .
- If , then , and .
- If , then .
- If , then , and .
Proof. The typing relation is the smallest relation closed under the rules, so each derivation ends in some rule whose conclusion matches the term. For each form of term exactly one rule has a conclusion of that form: T-Var for variables, T-Abs for abstractions, T-App for applications, T-True and T-False for the two constants, T-If for conditionals, T-Int for literals and T-Add for sums. Each claim is the premise list of that rule, read off with the conclusion matched to .
Lemma 2 (canonical forms)
Let be a value.
- If , then is or .
- If , then is a literal .
- If , then for some and .
Proof. A value has one of four forms. By Lemma 1, an abstraction has an arrow type, and have type , and a literal has type . The three types , and are pairwise different, so in case 1 only the Boolean constants are possible, in case 2 only literals, and in case 3 only abstractions. In case 3, Lemma 1.2 also gives that the annotation is .
Theorem 1 (progress)
If (in the empty context), then is a value or there is a with .
Proof. By induction on the derivation of . Consider the last rule.
- T-Var. Impossible: the context is empty, so no premise holds.
- T-Abs, T-True, T-False, T-Int. The term is a value.
- T-App. Here with and . By the induction hypothesis on , either , and then E-App1 gives ; or is a value. In the second case apply the induction hypothesis to : either , and E-App2 gives ; or is a value too. In that last case, Lemma 2.3 gives , and E-AppAbs gives .
- T-If. Here with . By induction, either and E-If applies, or is a value. Then Lemma 2.1 gives or , and E-IfTrue or E-IfFalse applies.
- T-Add. Here with both parts of type . If steps, E-Add1 applies. Otherwise is a value. If steps, E-Add2 applies. Otherwise both are values, Lemma 2.2 makes them literals and , and E-AddInt gives with .
Every case gives a value or a step.
Lemma 3 (permutation and weakening)
- If and is a permutation of , then .
- If and is not in the domain of , then for every .
Proof. Both go by induction on the derivation. The only rule that reads the context is T-Var, and its premise holds in any context with the same bindings, or with more bindings. T-Abs extends the context with its bound variable. By the variable convention, that variable differs from and from every variable of , so the extended context is still a list of distinct variables, and the induction hypothesis applies to the premise. Every other rule passes the context unchanged to its premises.
Lemma 4 (substitution)
If and , then .
Proof. By induction on the term , for all and at once. Induction on the term, and not on the derivation, lets us use Lemma 3 on a premise and still apply the induction hypothesis, because the permuted derivation is a derivation for the same subterm. By Lemma 1, the last rule of the derivation is fixed by the form of .
- T-Var, with . If , then because the context binds once, and , which has type under by assumption. If , then and , so T-Var gives .
- T-Abs, with and . By the variable convention, and is not free in , so . By Lemma 3.1, . By Lemma 3.2, . The induction hypothesis gives , and T-Abs gives .
- T-App, . The premises are and . The induction hypothesis gives the same judgments for and under , and T-App gives , which is .
- T-If and T-Add are the same as T-App: substitute into each premise by induction and apply the rule again.
- T-True, T-False, T-Int. The term has no variables, so , and the same rule types it under .
Theorem 2 (preservation)
If and , then .
Proof. By induction on the derivation of . Consider the last rule.
- T-Var, T-Abs, T-True, T-False, T-Int. The term is a variable or a value. No evaluation rule has a variable or a value on its left side, so is impossible, and the claim holds vacuously.
- T-App, with and . Three rules can give .
- E-App1: with . The induction hypothesis gives , and T-App gives .
- E-App2: with . The same argument on .
- E-AppAbs: , is a value, and . Lemma 1.2 applied to gives and . Lemma 4 with gives .
- T-If, with , and . For E-IfTrue, , and for E-IfFalse, ; both have type by the premises. For E-If, with ; the induction hypothesis gives , and T-If gives .
- T-Add, with . For E-Add1 and E-Add2, use the induction hypothesis on the part that steps and apply T-Add again. For E-AddInt, is a literal, and T-Int gives .
Corollary (safety)
If and , then is not stuck: is a value of type , or for some .
Proof. By induction on the number of steps in , Theorem 2 gives : for this is the assumption, and for apply Theorem 2 to the last step. Then Theorem 1 says is a value or steps. If it is a value, it has type .
Remarks on the proof
- Progress needs the empty context. The open term has type under , but it is not a value and does not step. Preservation holds in any context.
- Progress uses canonical forms, and canonical forms use the fact that the types of the different kinds of values are different. If a rule gave the type , Lemma 2.2 would fail, and the term would be well typed and stuck.
- Preservation uses substitution only in the case E-AppAbs. That is the step where computation happens. In systems with type variables, a second substitution lemma, for types, is needed in the case of type application.
- The proofs do not use the order of evaluation. With a call-by-name rule for every , the same Lemma 4 gives preservation, and progress holds with the matching changes.
History
Curry and Feys (1958) proved the subject reduction theorem for typed combinatory logic: reduction keeps the type. Milner (1978) proved the first soundness theorem for a programming language with polymorphism. He gave a denotational semantics in which ill-typed operations produce a special value wrong, and he proved that the meaning of a well-typed expression is never wrong. That style needs a domain-theoretic model, and it is hard to extend to references and control. Wright and Felleisen (1994) replaced it with the syntactic method: a small-step semantics where errors are stuck terms, subject reduction for each step, and a separate lemma that typed terms are not stuck. They applied it to ML with references, exceptions and continuations. Pierce's book named the two lemmas progress and preservation and made this the standard form.
References
- Andrew K. Wright and Matthias Felleisen, A syntactic approach to type soundness, Information and Computation 115(1), 1994, pages 38 to 94, the syntactic method (soundness as subject reduction plus a progress lemma for a small-step semantics with stuck states), applied to ML with references, exceptions and continuations
- Robin Milner, A theory of type polymorphism in programming, Journal of Computer and System Sciences 17(3), 1978, pages 348 to 375, Section 3 (the semantic soundness theorem for a denotational semantics with the value "wrong")
- Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Section 8.3 (safety as progress plus preservation, Theorems 8.3.2 and 8.3.3), Section 9.3 (Lemma 9.3.1 inversion, Lemma 9.3.4 canonical forms, Theorem 9.3.5 progress, Lemmas 9.3.6 to 9.3.8 permutation, weakening and substitution, Theorem 9.3.9 preservation), Chapters 13 and 14 (references and exceptions)
- Haskell B. Curry and Robert Feys, Combinatory Logic, Volume I, North-Holland, 1958, the chapter on functionality (the subject reduction theorem for typed combinatory terms)
- Nada Amin and Ross Tate, Java and Scala's type systems are unsound: the existential crisis of null pointers, OOPSLA 2016, the counterexamples that combine null with path-dependent or wildcard types
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag ZB8M1Q Edit this page History Report an error Markdown