Type Inference (Part II)


Contents


Overview

In this set of notes we will define a formal system for type inference: a set of axioms and inference rules. By definition, an ML expression e has type t iff there exists a proof in this system.

To handle recursive function definitions more cleanly, we will make a slight change to the syntax. Instead of:

we'll use: where the two ids are the same; for example:

Using this new syntax, a recursive function definition is a sub-case of a general let expression; e.g., the definition of length is of the form

where exp1 is

Definitions

  1. Axioms and rules of inference are defined using sequents, which are of the form:
      A |- e:τ
    Note that:
    • A is a set of assumptions. For the purposes of type inference, A will always be a set of bindings of identifiers and/or literals to types (i.e., A is a type environment like the ones we used in our informal type-inference algorithm). For example, { x:int, y:α, 3:int, true:bool } is a type environment that says that x has type int, and y has type α, and 3 has type int, and true has type bool.
    • The symbol "|-" is called turnstile.
    • e is an expression and τ is a type.
    • A |- e:τ means "if we know A, then we can deduce that expression e has type τ". For example, if we know that L has type int-list, then we can deduce that the expression car(L) has type int.

  2. A.x:τ means A minus (x:*) union {x:τ} (i.e., if A already contains an assumption about the type of x, then remove that assumption, and add the new one).

  3. t1[t2/α] means t1 with all free occurrences of α replaced with t2. Both t1 and t2 are type expressions, and α is a type variable. (Recall that an α is free in t1 iff it is not inside a quantifier, "∀ α".)

  4. A rule of inference is of the form:

      X1 X2 ... Xn

      Y

      where each Xk as well as Y is a sequent. A rule of inference means "if you know X1 and X2 and ... and Xn, then you can conclude Y", or "to prove Y you must prove X1 and X2 and ... and Xn".

The goal of type inference for ML is to prove that a given ML expression e has type t. To provide a proof using our formal system, we must create a proof tree such that:

To create a proof tree, we will start with the root (what we want to prove), and work down to the leaves. At each step, we will use the rules of inference to add, as children of the current node, nodes that represent facts that we still need to prove.

Axioms and Rules of Inference

The axioms will all be of the form:

For example, the following are both axioms:

The rules of inference are as follows: