Excessive explanation, part eighteen

Over the last two episodes we gave the rules of the deductive system. The paper now gives an example of using that deductive system to derive the type of a complicated expression. The way it is laid out on the page makes it difficult to reproduce here and to annotate, so I’ll take some small editorial liberties and “linearize” it a bit.

The following example of a derivation is organised as a tree, in 
which each node follows from those immediately above it by an 
inference rule.

TAUT: --------------------
       x:α ⊢ x:α
ABS:  --------------------
       ⊢ (λx.x):α → α
GEN:  -------------------- 
       ⊢ (λx.x):∀α(α → α)   (1)

The (1) indicates that I’m going to use this fact that we’ve derived later.

Let’s go through this carefully.

We start with a tautology, and therefore need no facts. We logically deduce that if we make the assumption that x:α we can logically deduce that x:α, unsurprisingly.

Now this becomes the fact that is fed into the next rule, ABS. Notice that below the second line we just have a turnstile with nothing to the left of it. This means that we can derive this conclusion from no assumptions whatsoever. Read the ABS rule carefully to make sure that you understand why this is.

This conclusion should make sense: we don’t need any assumptions about the types of any variables to know that the identity lambda is a function from α to α.

And now perhaps you understand why I said last time that the GEN rule is a bit subtle. Here we have α free in the type, but not free in the set of assumptions. (The set of assumptions is empty, so plainly there are no free type variables in it!) It is perfectly valid for us to turn the free type variable into a quantified type variable, and deduce that yes, the identity function is a function from α to α for any α you care to name.

All right, let’s start over. We won’t use any of the facts we’ve just deduced in this next derivation. We’ll start over from scratch.

TAUT: -----------------------------------   
       i:∀α(α → α) ⊢ i:∀α(α → α)  
INST: -----------------------------------
       i:∀α(α → α) ⊢ i:(α → α) → (α → α)   (2)

The tautology says that if i is a function from α to α, then i is a function from α to α. I hope we agree that is true!

Now we can make that more specific if we want to by using the INST rule. Since that is true for any α, it is in particular true for the type (α → α). These are different alphas than the alphas in ∀α(α → α)! We could have just as easily said i:(β → β) → (β → β), and it would have been more clear.

This is one of the things that is most confusing about academic papers: that they seem to deliberately use the same symbols to mean different things on the same line, like symbols are really expensive or something. I push back in code review when people do that in programming languages, and I wish they would not do it in papers either.

All right, so far we’ve deduced (1) (λx.x) is ∀α(α → α), and (2) if we have a proof that i is ∀α(α → α) then we can use i somewhere that an (α → α) → (α → α) is needed. Now let’s make a third deduction starting again from nothing:

TAUT: -----------------------------------   
       i:∀α(α → α) ⊢ i:∀α(α → α)  
INST: -----------------------------------
       i:∀α(α → α) ⊢ i:α → α   (3)

Wait, isn’t that the derivation we just made over again?

Not quite. It’s subtly different. Here we’re saying that i:∀α(α → α) implies that i:α → α. Again these are different alphas!

Now we can apply the COMB rule to facts (2) and (3). Go back and read the COMB rule and make sure you understand why facts (2) and (3) can be used here.

          (2)   (3)
COMB: --------------------------
       i:∀α(α → α) ⊢ i i:α → α   (4)

This says that if i is a function from α to α, then so is (i i).

Finally, we can put facts (1) and (4) together using the LET rule to derive the type of a complicated expression:

       (1)   (4)
LET:  --------------------------------- 
       ⊢ (let i = (λx.x) in i i):α → α

Here we have an expression that combines the four kinds of expression in Exp: let, lambda, application and identifier. And we derive that from no assumptions whatsoever, we can figure out the type of this complex expression; it’s a function from α to α.

This should not be a surprise: if you pass an identity function to itself, you get an identity function, and an identity function is of type α → α. But it is pretty neat that we can make a set of logical deductions that gets us starting from nothing, and ending with this correct conclusion.

But how, you might ask, did we even come up with that derivation? What we need is an algorithm that takes an expression and produces a derivation that the expression is of a particular type; such an algorithm is a type inference algorithm!

Next time we’ll discuss the “semantic soundness” of the algorithm, and give a sketch proof.


3 thoughts on “Excessive explanation, part eighteen

  1. I think fifth paragraph should read “we just have a turnstile with nothing to the left of it” not “…right of it”

  2. I get the meaning of ∀α(α → α) which is basically saying a generic function (a function for “all” types α). But for just α → α with a free α, is that saying something like a specific yet undetermined function from some α to α (a function for “any” one type α).

    So then i:∀α(α → α) ⊢ i:α → α says: if i is a function α → α for all types α, then we can say that i could be a function α → α for any particular type α.

    I remember this kind of “so simple it hurts” stuff from taking logic courses.

    • [several theorems about how substituting propositions for variables in a formula results in a proposisiotn, and substituting formulae for variables in an another formula results in a formula, and that substituting equivalent formulae into equivalent formulae results in equivalent formulae]
      “Proof. The listener interested in long and boring proofs may consult Kleene, S. “Mathematical Logic”. QED.” 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s