Robin Milner proved that well-typed programs cannot go wrong in his 1978 paper A Theory of Type Polymorphism in Programming (Milner 1978). That is, he defined a type system and denotational semantics for the Exp language (a subset of ML) and then proved that the denotation of a well-typed program in Exp is not the “wrong” value. The “wrong” denotation signifies that a runtime type error occurred, so Milner’s theorem proves that the type system is strong enough to prevent all the runtime type errors that could occur in an Exp program. The denotational semantics used by Milner (1978) was based on the standard domain theory for an explicitly typed language with higher-order functions.
I have been exploring, over the last month, whether I can prove a similar theorem but using my new denotational semantics, and mechanize the proof in the Isabelle proof assistant. At first I tried to stay as close to Milner’s proof as possible, but in the process I learned that Milner’s proof is rather syntactic and largely consists of proving lemmas about how substitution interacts with the type system, which does not shed much light on the semantics of polymorphism.
Last week I decided to take a step back and try a more semantic approach and switch to a cleaner but more expressive setting, one with first-class polymorphism. So I wrote down a denotational semantics for System F (Reynolds 1974) extended with support for general recursion. The proof that well-typed programs cannot go wrong came together rather quickly. Today I finished the mechanization in Isabelle and it came in at just 539 lines for all the definitions, lemmas, and main proof. I’m excited to share the details of how it went! Spoiler: the heart of the proof turned out to be a lemma I call Compositionality because it looks a lot like the similarly-named lemma that shows up in proofs of parametricity.
Syntax
The types in the language include natural numbers, function types, universal types, and type variables. Regarding the variables, after some experimentation with names and locally nameless, I settled on good old DeBruijn indices to represent both free and bound type variables. i,j∈Nσ,τ::=nat∣τ→τ∣∀τ∣i So the type of the polymorphic identity function, normaly written ∀α.α→α, is instead written ∀(0→0).
The syntax of expressions is as follows. I choose to use DeBruijn indices for term variables as well, and left off all type annotations, but I don’t think that matters for our purposes here. n∈Ne::=n∣i∣λe∣ee∣Λe∣e[]∣fixe
Denotational Semantics
The values in this language, described by the below grammar, include natural numbers, functions represented by finite lookup tables, type abstractions, and wrong to represent a runtime type error. f::={(v1,v′1),…,(vn,v′n)}o::=none∣some(v)v::=n∣fun(f)∣abs(o)∣wrong A type abstraction abs(o) consists of an optional value, and not simply a value, because the body of a type abstraction might be a non-terminating computation.
We define the following information ordering on values so that we can reason about one lookup table being more or less-defined than another lookup table. We define v⊑v′ inductively as follows.
n⊑nf1⊆f2fun(f1)⊑fun(f2)wrong⊑wrongabs(none)⊑abs(none)v⊑v′abs(some(v))⊑abs(some(v′))
The denotational semantics maps an expression to a set of values. Why a set and not just a single value? A single finite lookup table is not enough to capture the meaning of a lambda, but an infinite set of finite tables is. However, dealing with sets is somewhat inconvenient, so we mitigate this issue by working in a set monad. Also, to deal with wrong we need an error monad, so we use a combined set-and-error monad.
X:=E1;E2≡{v∣∃v′.v′∈E1,v′≠wrong,v∈E2[v′/X]}∪{v∣v=wrong,wrong∈E1}return(E)≡{v∣v⊑E}X←E1;E2≡{v∣∃v′.v′∈E1,v∈E2[v′/X]}
The use of ⊑ in return is to help ensure that the meaning of an expression is downward-closed with respect to ⊑. (The need for which is explained in prior blog posts.)
Our semantics will make use of a runtime environment ρ that includes two parts, ρ1 and ρ2. The first part gives meaning to the term variables, for which we use a list of values (indexed by their DeBruijn number). The second part, for the type variables, is a list containing sets of values, as the meaning of a type will be a set of values. We define the following notation for dealing with runtime environments.
v::ρ≡(v::ρ1,ρ2)V::ρ≡(ρ1,V::ρ2)
We write ρ[i] to mean either ρ1[i] or ρ2[i], which can be disambiguated based on the context of use.
To help define the meaning of fixe, we inductively define a predicate named iterate. Its first parameter is the meaning L of the expression e, which is a mapping from an environment to a set of values. The second parameter is a runtime environment ρ and the third parameter is a value that is the result of iteration.
iterate(L,ρ,fun(∅))iterate(L,ρ,v)v′∈E(v::ρ)iterate(L,ρ,v′)
To help define the meaning of function application, we define the following apply functiion. apply(V1,V2)≡x1:=V1;x2:=V2;casex1offun(f)⇒(x′2,x′3)←f;ifx′2⊑x2thenx′3else∅∣_⇒return(wrong)
The denotational semantics is given by the following function E that maps an expression and environment to a set of values.
E[n]ρ=return(n)E[i]ρ=return(ρ[i])E[λe]ρ={v∣∃f.v=fun(f),∀v1v′2.(v1,v′2)∈f⇒∃v2.v2∈E[e](v1::ρ),v′2⊑v2}E[e1e2]ρ=apply(E[e1]ρ,E[e2]ρ)E[fixe]ρ={v∣iterate(E[e],ρ,v)}E[Λe]ρ={v∣∃v′.v=abs(some(v′)),∀V.v′∈E[e](V::ρ)}∪{v∣v=abs(none),∀V.E[e](V::ρ)=∅}E[e[]]ρ=x:=E[e]ρ;casexofabs(none)⇒∅∣abs(some(v′))⇒return(v′)∣_⇒return(wrong)
We give meaning to types with the function T, which maps a type and an environment to a set of values. For this purposes, we only need the second part of the runtime environment which gives meaning to type variables. Instead of writing ρ2 everywhere, we’ll use the letter η. It is important to ensure that T is downward closed, which requires some care either in the definition of T[∀τ]η or in the definition of T[i]η. We have chosen to do this work in the definition of T[i]η, and let the definition of T[∀τ]η quantify over any set of values V to give meaning to it’s bound type variable.
T[nat]η=NT[i]η={{v∣∃v′.v′∈η[i],v⊑v′,v≠wrong}if i<|η|∅otherwiseT[σ→τ]η={v∣∃f.v=fun(f),∀v1v′2.(v1,v′2)∈f,v1∈T[σ]η⇒∃v2.v2∈T[τ]η,v′2⊑v2}T[∀τ]η={v∣∃v′.v=abs(some(v′)),∀V.v′∈T[τ](V::η)}∪{abs(none)}
Type System
Regarding the type system, it is standard except perhaps how we deal with the DeBruijn representation of type variables. We begin with the definition of well-formed types. A type is well formed if all the type variables in it are properly scoped, which is captured by their indices being below a given threshold (the number of enclosing type variable binders, that is, Λ’s and ∀’s). More formally, we write j⊢τ to say that type τ is well-formed under threshold j, and give the following inductive definition.
j⊢natj⊢σj⊢τj⊢σ→τj+1⊢τj⊢∀τi<jj⊢i
Our representation of the type environment is somewhat unusual. Because term variables are just DeBruijn indices, we can use a list of types (instead of a mapping from names to types). However, to keep track of the type-variable scoping, we also include with each type the threshold from its point of definition. Also, we need to keep track of the current threshold, so when we write Γ, we mean a pair where Γ1 is a list and Γ2 is a number. The list consists of pairs of types and numbers, so for example, Γ1[i]1 is a type and Γ1[i]2 is a number whenever i is less than the length of Γ1. We use the following notation for extending the type environment:
τ::Γ≡((τ,Γ2)::Γ1,Γ2)∗::Γ≡(Γ1,Γ2+1)
We write ⊢ρ:Γ to say that environment ρ is well-typed according to Γ and define it inductively as follows.
⊢([],[]):([],0)⊢ρ:Γv∈T[τ]ρ2⊢v::ρ:τ::Γ⊢ρ:Γ⊢V::ρ:∗::Γ
The primary operation that we perform on a type environment is looking up the type associated with a term variable, for which we define the following function lookup that maps a type environment and DeBruijn index to a type. To make sure that the resulting type is well-formed in the current environment, we must increase all of its free type variables by the difference of the current threshold Γ2 and the threshold at its point of definition, Γ1[i]2, which is accomplished by the shift operator ↑kc(τ) (Pierce 2002). lookup(Γ,i)≡{some(↑k0(Γ1[i]1)if n<|Γ1|where k=Γ2−Γ1[i]2noneotherwise
To review, the shift operator is defined as follows.
↑kc(nat)=nat↑kc(i)={i+kif c≤iiotherwise↑kc(σ→τ)=↑kc(σ)→↑kc(τ)↑kc(∀τ)=∀↑kc+1(τ)
Last but not least, we need to define type substitution so that we can use it in the typing rule for instantiation (type application). We write [j↦τ]σ for the substitution of type τ for DeBruijn index j within type σ (Pierce 2002).
nat=nat[j↦τ]i={τif j=ii−1if j<iiotherwise[j↦τ](σ→σ′)=[j↦τ]σ→[j↦τ]σ′[j↦τ]∀σ=∀[j+1↦↑10(τ)]σ
Here is the type system for System F extended with fix.
Γ⊢n:natlookup(Γ,i)=some(τ)Γ⊢i:τΓ2⊢σσ::Γ⊢e:τΓ⊢λe:σ→τΓ⊢e:σ→τΓ⊢e′:σΓ⊢ee′:τΓ2⊢σ→τ(σ→τ)::Γ⊢e:σ→τΓ⊢fixe:σ→τ∗::Γ⊢e:τΓ⊢Λe::∀τΓ⊢e:∀τΓ⊢e[]:[0↦σ]τ
We say that a type environment Γ is well-formed if Γ2 is greater or equal to every threshold in Γ1, that is Γ1[i]2≤Γ2 for all i<|Γ1|.
Proof of well-typed programs cannot go wrong
The proof required 6 little lemmas and 4 big lemmas. (There were some itsy bitsy lemmas too that I’m not counting.)
Little Lemmas
Lemma [⊑ is a preorder]
v⊑v
If v1⊑v2 and v2⊑v3, then v1⊑v3.
[lem:less-refl] [lem:less-trans]
I proved transitivity by induction on v2.
Lemma [T is downward closed] If v∈T[τ]η and v′⊑v, then v′∈T[τ]η. [lem:T-down-closed]
The above is a straightforward induction on τ
Lemma [wrong not in T] For any τ and η, wrong∉T[τ]η. [lem:wrong-not-in-T]
The above is another straightforward induction on τ
Lemma If ⊢ρ:Γ, then Γ is a well-formed type environment. [lem:wfenv-good-ctx]
The above is proved by induction on the derivation of ⊢ρ:Γ.
Lemma T[τ](η1η3)=T[↑|η2||η1|(τ)](η1η2η3)
The above lemma is proved by induction on τ. It took me a little while to figure out the right strengthening of the statement of this lemma to get the induction to go through. The motivations for this lemma were the following corollaries.
Corollary [Lift/Append Preserves T] T[τ](η2)=T[↑|η1|0(τ)](η1η2) [lem:lift-append-preserves-T]
Corollary[Lift/Cons Preserves T] T[τ](η)=T[↑10(τ)](V::η) [lem:shift-cons-preserves-T]
Of course, two shifts can be composed into a single shift by adding the amounts.
Lemma [Compose Shift] ↑j+kc(τ)=↑jc(↑kc(τ)) [lem:compose-shift]
The proof is a straightforward induction on τ.
Big Lemmas
There are one or two big lemmas for each of the “features” in this variant of System F.
The first lemma shows that well-typed occurrences of term variables cannot go wrong.
Lemma [Lookup in Well-typed Environment]
If ⊢ρ:Γ and lookup(Γ,i)=some(τ), then ∃v.ρ1[i]=v and v∈T[τ]ρ2. [lem:lookup-wfenv]
The proof is by induction on the derivation of ⊢ρ:Γ. The first two cases were straightforward but the third case required some work and used lemmas [lem:wfenv-good-ctx], [lem:shift-cons-preserves-T], and [lem:compose-shift].
Lemma [Application cannot go wrong] If V⊆T[σ→τ]η and V′⊆T[σ]η, then apply(V,V′)⊆T[τ]η. [lem:fun-app]
The proof of this lemma is direct and does not use induction. However, it does use lemmas [lem:wrong-not-in-T] and [lem:T-down-closed].
Lemma [Compositionality] Let V=T[σ](η1η2). T[τ](η1Vη2)=T[τ[σ/|η1|]](η1η2) [lem:compositionality]
I proved the Compositionality lemma by induction on τ. All of the cases were straightforward except for τ=∀τ′. In that case I used the induction hypothesis to show that T[τ′](Vη1Sη2)=T[([|Vη1|↦↑10(σ)]τ′](Vη1η2) where S=T[↑10(σ)](Vη1η2) and I used Lemma [lem:shift-cons-preserves-T].
Lemma [Iterate cannot go wrong] If
iterate(L,ρ,v) and
for any v′, v′∈T[σ→τ]ρ2 implies L(v′::ρ)⊆T[σ→τ]ρ2,
then v∈T[σ→τ]ρ2. [lem:iterate-sound]
This was straightfroward to prove by induction on the derivation of iterate(L,ρ,v). The slightly difficult part was coming up with the definition of iterate to begin with and in formulating the second premise.
The Theorem
Theorem [Well-typed programs cannot go wrong]
If Γ⊢e:τ and ⊢ρ:Γ, then E[e]ρ⊆T[τ]ρ2. [thm:welltyped-dont-go-wrong]
The proof is by induction on the derivation of Γ⊢e:τ.
Γ⊢n:nat
This case is immediate.
lookup(Γ,i)=some(τ)Γ⊢i:τ
Lemma [lem:lookup-wfenv] tells us that ρ1[i]=v and v∈T[τ]ρ2 for some v. We conclude by Lemma [lem:T-down-closed].
Γ2⊢σσ::Γ⊢e:τΓ⊢λe:σ→τ
After unraveling some definitions, for arbitrary f,v1,v2,v′2 we can assume v1∈T[σ]ρ2, v2∈E[e](v1::ρ), and v′2⊑v2. We need to prove that v2∈T[τ](v1::ρ)2.
We can show ⊢v1::ρ:σ::Γ and therefore, by the induction hypothesis, E[e](v1::ρ)⊆T[τ](v1::ρ)2. So we conclude that v2∈T[τ](v1::ρ)2.
Γ⊢e:σ→τΓ⊢e′:σΓ⊢ee′:τ
By the induction hypothesis, we have E[e]ρ⊆T[σ→τ]ρ2 and E[e′]ρ⊆T[σ]ρ2. We conclude by Lemma [lem:fun-app].
Γ2⊢σ→τ(σ→τ)::Γ⊢e:σ→τΓ⊢fixe:σ→τ
For an arbitrary v, we may assume iterate(E[e],ρ,v) and need to show that v∈T[σ→τ]ρ2.
In preparation to apply Lemma [lem:iterate-sound], we first prove that for any v′, v′∈T[σ→τ]ρ2 implies E[e](v′::ρ)⊆T[σ→τ]ρ2. Assume v″∈E[e](v′::ρ). We need to show that v″∈T[σ→τ]ρ2. We have ⊢v′::ρ:(σ→τ)::Γ, so by the induction hypothesis E[e](v′::ρ)⊆T[σ→τ](v′::ρ). From this we conclude that v″∈T[σ→τ]ρ2.
We then apply Lemma [lem:iterate-sound] to conclude this case.
∗::Γ⊢e:τΓ⊢Λe::∀τ
After unraveling some definitions, for an arbitrary v′ and V we may assume that ∀V′.v′∈E[e](V::ρ). We need to show that v′∈T[τ](V::ρ2). We have ⊢V::ρ:∗::Γ, so by the induction hypothesis E[e](V::ρ)⊆T[τ](V::ρ)2. Also, from the assumption we have v′∈E[e](V::ρ), so we can conclude.
Γ⊢e:∀τΓ⊢e[]:[0↦σ]τ
Fix a v′∈E[e]ρ. We have three cases to consider.
v′=abs(none). This case is immediate.
v′=abs(some(v″)) for some v″. By the induction hypothesis, v′∈T[∀τ]ρ2. So we have v″∈T[τ](V::ρ2) where V=T[σ]ρ2. Then by Compositionality (Lemma [lem:compositionality]) we conclude that v″∈T[[0↦σ]τ]ρ2.
v′ is some other kind of value. This can’t happen because, by the induction hypothesis, v′∈T[∀τ]ρ2.
References
Milner, Robin. 1978. “A Theory of Type Polymorphism in Programming.” Journal of Computer and System Sciences 17 (3): 348–75.
Pierce, Benjamin C. 2002. Types and Programming Languages. MIT Press.
Reynolds, John C. 1974. “Towards a Theory of Type Structure.” In Programming Symposium: Proceedings, Colloque Sur La Programmation, 19:408–25. LNCS. Springer-Verlag.
I think this looks really interesting.
ReplyDeleteCan you also show strong normalization of System F without fix? If you can, where are you using the extra proof-theoretic power to make that possible? Proof and Types explains, more or less, that consistency of System F implies consistency of Peano Arithmetic (PA), so by Gödel's theorems you need more than PA to prove System F consistent.
Is this model parametric? The post doesn't say much about the rule for polymorphic abstractions, but it seems you're in a sense taking the intersection of all their possible types, which I think is an existing approach but not the most common one.