Lecture 15: Polymorphism

October 27
We finish with the three interesting cases for preservation from before.

Inversion principles

Case (Σ' = (Σ', l = e1), e = ref e1, and e' = l)

    e1 value
    l fresh
    --------------------------------- e-ref
    (S,ref e1) -> ((S,l = e1), l)

We start with [Σ]ref e1 : τ, so we have τ = τ' ref and [Σ]e1 : τ' by inversion.

We need to show that there exists Σ' such that [Σ']l : τ' ref and [Σ'](S, l = e1):Σ' and Σ' extends Σ.

We will show that Σ' = (Σ, l = τ'). Then we have [Σ, l = τ']l : τ' by rule, (Σ, l = τ') extends Σ by definition, and [Σ, l = τ'](S, l = e1) : (Σ, l = τ') by monotonicity.

(Leaving out the other cases - two lemmas are needed to handle monotonicity of update and lookup)

Polymorphism

THROW EVERYTHING OUT (we don't need sums, products, unit, void, bool, nat...). Keep functions. We love functions.

τ ::= α | τ -> τ | ∀α.&tau
e ::= λx:τ.e | e e | Λα.e | e[τ]

So we want to write a function that, for ANY type, acts as the identity, becuase it's a little dumb to have to write λx:nat.x, λx:nat ref.x, λx:(void -> nat).x, λx: unit.x.

We will give this function type ∀α.α -> α. The rule for typing a type abstraction Λα.e creates a new variable type and throws it into the context.

    α : type |- e : τ
    ------------------ t-polylam
    Λα.e : ∀α.τ
We introduced hypotheses of the form α : type. Well, we actually need to create a whole defintion of τ : type:
    α : type |- τ : type
    ------------------ wf-forall
    ∀α.τ : type
    τ : type
    τ' : type
    ------------------ wf-lam
    τ -> τ' : type
Now we can write the rest of the rules - note that the typing rule for lambdas has a new premise, because we could have a "poorly formed" type if it mentioned a variable that didn't exist anywhere.
    τ : type
    x : τ |- e : τ'
    ------------------ t-lam
    λx:τ.e : τ -> τ'
    e1 : τ' -> τ
    e2 : τ'
    ------------------ t-app
    e1 e2 : τ
    e : ∀α.τ
    τ' : type
    ------------------ t-polyapp
    e[τ'] : τ

... therefore, we can write the polymorphic function as Λα.λx:α.x, and give a typing derivation.

--------------------- hyp  --------------------------- hyp
α : type |- α : type        α : type, x : α |- x : α
----------------------------------------------------- t-lam
α : type |- λx:α.x : α -> α
---------------------------- t-polylam
Λα.λx:α.x : ∀α.α -> α

Programming With Polymorphism

This is pretty cool.

Booleans

Natural Numbers

...
$LastChangedDate: 2008-11-10 11:52:21 -0500 (Mon, 10 Nov 2008) $
$Author: rjsimmon $
$Rev: 1029 $