Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

More about System F

import Lait
#lait

System F and Other Polymorphic Languages

Now, we have seen System F, and we have also been working with Lait, another polymorphic language. How do they relate?

One big difference is the lack of needing to explicitly annotate types. In Lait, we can do the following:

def calltwice f x := f (f x)

#eval calltwice (fun x => x + 1) 5>> 7

Lait then correctly infer that this function has type (a -> a) -> a -> a, which in System F would translate to \(\forall X. (X \to X) \to X \to X\). Comparing this version of calltwice to our version in System F, we see that Lait doesn’t require explicit type variable quantification or application.

What is going on here? We can see that Lait is performing type inference: the act of assigning a type to code which does not have all type information in the source. To model this formally, one can imagine a variant of System F where explicit type abstraction and application isn’t present in surface terms, but instead needs to be guessed in the type checker. Thus, under this calculus, we would instead see a judgment for calltwice like so: \[ \vdash \lambda f.\ \lambda x.\ f\ (f x) : \forall X. (X \to X) \to X \to X \]

Unfortunately, J.B. Wells in 1996 proved that type inference and type checking for un-annotated System F is undecidable.. Hence, no adequate algorithm can exist!

Why, then, can Lait infer a polymorphic type for calltwice? It’s because Lait, and other polymorphic languages like OCaml, support a more restricted form of polymorphism. Thus, they are not actually implementing all of System F, but a subset of System F that balances expressivity with the ability to do type checking efficiently.

To a very rough degree of approximation, the main restriction that makes type inference tractable is the restriction to rank-1 polymorphism. This means that while functions can be polymorphic, their arguments cannot: every function type has to have the form \(\forall \overrightarrow{X}, \tau\), where \(\overrightarrow{X}\) is a sequence of type variables, and \(\tau\) is a simple type (one that does not have a \(\forall\)).

Hence, the following type is not expressible in Lait: \[ (\forall A, A \to A) \to (\texttt{Int} \times \texttt{Bool}) \] because we have type quantification inside an arrow type. (This would be called rank-2 polymorphism since we are nesting a \(\forall\) “one layer deep” inside of a type.) First, let’s see that we can come up with a System F program with this type easily: \[ \lambda f : (\forall A, A \to A). (f\ [\texttt{Int}]\ 3, f\ [\texttt{Bool}]\ \texttt{false}) \]

This function takes in a polymorphic identity function and applies it twice: once at type Int, and once at type Bool. Let’s now try to do this in Lait:

def rank2 f := (f 3, f false)

What’s going on? Lait first reads f 3, and knows that f must have a type type of the form Int -> .... This then doesn’t line up with applying f at false. Why Int -> ?? and not a more complicated type, like (forall A, ...) -> ...? Because Lait assumes rank-1 polymorphism, which means that functions taken as input to a function cannot have quantified types.

However, Lait does allow a program similar to rank2 to work:

def poly_id x := x

def apply_poly_id := (poly_id 3, poly_id false)

This program now does type check, because we don’t take a function as input to another function, but rather define a function at the top-level, and then use it later on in multiple ways.

There is a very rich history of algorithms designed for type inference in the presence of polymorphism. The classic algorithm is called Hindley-Milner inference, which sets up a constraint system of types that needs to be solved. Another complementary approach is Bidirectional Typing, which can be throught of as a clever way of mixing type checking with type inference.

Logical Aspects of System F

Finally, let’s touch upon some of the more deep theoretical aspects of System F. In particular, the connection between type systems and logic. For more information, you can read Phil Wadler’s paper Propositions as types.

To motivate this connection, let’s look at the following logical inference rule, called modus ponens:

For all A and B, if I know that “A implies B”, and I know A, then I know B.

This can be written as an inference rule like so:

\[ \dfrac{A \implies B \quad A}{B} \]

But now, let’s think about the following System F type:

\[ \forall A, \forall B, (A \to B) \to A \to B \]

As you can see, once we have a typed programming language, then we can map logical statements onto types. That gives us the first correspondence: propositions are types.

But, if propositions are types, what are programs? For example, we can actually implement a program of the above System F type:

\[ \Lambda A. \Lambda B. \lambda f : (A \to B). \lambda x : A. f\ x \]

Under this correspondence, proofs are programs. Thus, the above is actually a proof of modus ponens!

Let’s now see how the rest of propositional logic works under this correspondence. First, since we think of inference rules as implications, then we can see already that we are “implementing” implication using function types. Hence, implication is given by function types.
Additionally, from last class, we saw that System F can encode product and sum types. Under this correspondence, these types correspond to the conjunction and disjunction of propositions. Let’s see a table that captures all of this, using our notation from last time:

PropositionType
Propositional variableType variable
Conjunction \(A \land B\)Product type \(A \times B = \texttt{Prod}_{A, B}\)
Disjunction \(A \lor B\)Sum type \(A + B = \texttt{Sum}_{A, B}\)
Implication \(A \implies B\)Function type \(A \to B\)

We can now prove a simple propositional logic theorem simply by programming. Let’s prove the following theorem:

\[ \dfrac{(((A \lor B) \land C) \implies D) \land A \land C}{D} \]

This corresponds to the following type in System F:

\[ \forall A.\ \forall B.\ \forall C.\ \forall D.\ (((A + B) \times C) \to D) \to A \to C \to D \]

We can then implement a program with this type:

\[ \Lambda A, B, C, D.\ \lambda f : ((A + B) \times C) \to D.\ \lambda a : A.\ \lambda c : C.\
f\ (\texttt{inl}\ a, c) \]

In words: we prove \(D\) by taking in a function for the proof of the implication, and two additional arguments for a proof of \(A\) and a proof of \(C\). Then, \(\texttt{inl}\ a\) is a proof of \(A \lor B\). We pass that proof, and the proof of \(c\) into \(f\) to receive the proof of \(D\).