Type systems
CS 421
First, we specify a subset of the OCaml grammar subset. Associativity and precedence are not described here, and a description can be found in the Bison grammar picoml-stepper/interpreter/picoml.y.
This note will describe the static semantics of our language. In particular, each expression has a type \(\tau\) of possible data values. Our (sound) type system will a type for each expression, such that the dynamic value will always be in the type of \(\tau\).
Towards this, the main rule is the type judgment.
where \(\Gamma\) is our type environment (a context mapping variables to types), \(e\) is an expression, and \(\tau\) is some type. Then, under the type environemnt and a set of preconditions (above the bar), then we prove that \(e\) has type \(\tau\). A sound program has expressions for which the judgement \(\emptyset \vdash e : \tau\) is derivable. We will use \(\Gamma_1 + \Gamma_2\) to denote \(\Gamma_1 \cup (\Gamma_2 \setminus \Gamma_1)\) (note that this is not commutative).
Note that this is closely related to the idea of a logic system. Types correspond to propositions, and terms correspond to proofs. This is known as the Curry-Howard isomorphism; we will see that functions correspond to implication, and application corresponds to modus ponens.
A monomorphic type system
First, we give the rules for a very simple type system, in which each variable provably has a unique type. In contrast, a polymorphic type system can give a variable distinct types, as we will see later.
Under our type system, we will assign \(x\) the type \(\textsf{int} \mapsto \textsf{int}\). Informally, if \(x\) is the type \(\textsf{int} \mapsto \textsf{int}\), then the binder \(y\) gets type \(\textsf{int}\), and the \(+\) operator will also return an integer. This is an example of a type derivation, and we will now provide judgements for proving the type of an expression (though we do not show soundness or completeness).
First, we can specify boolean constants
In the above rule, \(\Gamma\) is a meta-variable. Rather than writing a rule for every possible type environment, we write a schema in terms of \(\Gamma\). Note that the rules themselves are still in terms of the concrete syntax as before.
As another example of a meta-variable, say \(n\) is an integral constant, so that the rule says that \(1\) is an integer, \(2\) is an integer, and so on.
Deriving the type of a variable involves looking in the type context.
Another interesting example is to determine the type of a pair \((e_1, e_2)\). This rule has a precondition, that \(e_1: \tau_1\) and \(e_2: \tau_2\), and once these are set, then \((e_1, e_2)\) has type \(\tau_1 * \tau_2\).
So to derive the type of \((1, \texttt{false})\), then we have to first derive the type of both components \(1\) and \(\texttt{false}\). Indeed, it is typed is \(\textsf{int} * \textsf{bool}\).
and we can write a derivation for this as follows:
Writing out the rules for the rest of our expression constructors is straightforward and intuitive.
At this point, it is prudent to prove properties about the system we have thus given. One important property is that each \(x: \tau \in \Gamma\) has a unique \(\tau\). We will not prove it here, but intead illustrate these with a more in-depth example, showing (almost) all of our judgements.
Examples
We want to prove that this expression has type \(\textsf{int}\). We work backwards from the conclusion; our judgment rules are syntax driven and so there is only one possible rule to apply at each step.
First, the has two premises. The second is straightforward to derive: we first derive the types of both terms of the application \(f\) and \(3\) using the variable and constant rules respectively, before applying the application rule.
The other premise is a bit more involved. There are the function rules, and then the conditional, in which the three expressions have to be typed. The base case is trivial.
Let \(\Gamma_1 = \left\{ x: \textsf{int}, f: \textsf{int} \to \textsf{int} \right\}\).
As far as the predicate of the \(\textsf{if}\), we can indeed verify it types to a \(\textsf{bool}\).
And as for the other side, we verify that it matches with the true branch of the if, namely that it also evaluates to an \(\textsf{int}\).
A polymorphic type system
The type system described above is very limited. Consider for example, the following expression:
What type is \(\texttt{id}\)? For the first element of the pair, it seems like we need \(\texttt{id}: \textsf{int} \to \textsf{int}\), but for the second element, it seems like we need \(\texttt{id}: \textsf{bool} \to \textsf{bool}\). We need a more powerful type system to express the possibility for \(\texttt{id}\) to be both!
We see that OCaml gives \(\texttt{id}\) the type signature 'a -> 'a It is instructive to think of this as sugar for the universally quantified type \(\forall(\alpha . \alpha \to \alpha)\). We read this as the type is now a function taking a type as an argument, and in general denote as \(\forall(t . \tau)\).
In this system, beyond our basic and compound types in the monomorphic type system, we also introduce type variables (in OCaml, prefixed with a ', and in our mathematics, we use lowercase greek letters). Finally, every type has a universal quantifier dictating the bound variables.
In particular, consider the grammar for the types:
Now, type judgements are still over monotypes
But, as type environments are now mappings from variables to polytypes, they will hold more power. With this, we can consider the first judgment that must change: variables.
where \(\varphi\) is a function replacing each \(\alpha_i\) with a monotype. For example, if the type environment is \(\left\{ x: \alpha \right\}\), then \(x: \textsf{int}\) can be derived, and \(x: \textsf{bool}\) too.
The second judgment that must change is in assigning new variables in a let (i.e. modifying the type environment); we need to be able to create polytypes from monotypes. To do this, consider a new function
\[ \mathbf{Gen}(\tau, \Gamma): \textsf{Monotype} \times \textsf{Environment} \to \textsf{Polytype} \]
which will bind all the free variables in \(\tau\) that are not in \(\Gamma\). Namely, the free variables of monomorphic types are the type variables, of polymorphic types must remove the quantified vars, and for an environment, all free variables over the range.
Now, the modifications to the grammar are simple, with only the following rules changing.
Examples
To illustrate this, we shall consider three examples.
We can derive the type as follows. In the second premise of LetIn, \(f\) is bound to \(\mathbf{Gen}(\beta \to \alpha, \left\{ y: \forall . \alpha \right\}) = \forall \beta . \beta \to \alpha\); only \(\beta\) is quantified, since \(\alpha\) is free in the environment.
Note that the derived type is \(\alpha * \alpha\); the output of \(f\) must be the same type even though it is a polytype. In contrast, consider the following, whose output pair type is \(\alpha * \beta\).
We can derive the type as follows.
Where in the first premise, the type environment is \(\Gamma_1 = \left\{ f: \forall . \delta \to \varepsilon, y: \forall . \alpha \right\}\).
and in the second premise, let \(\Gamma_2 = \left\{ f: \forall \delta, \varepsilon . \delta \to \varepsilon, y: \forall . \alpha \right\}\).
And for the final example, we note that the following does not type check.
Indeed, we fail at the argument of the recursive call.
The type of \(f\) is not quantified inside its own body, so its input type is fixed to \(\alpha\). The premise \(\left\{ x: \forall . \alpha, f: \forall . \alpha \to \beta \right\} \vdash (x, x) : \alpha\) has no derivation: the pair rule only derives \((x, x) : \alpha * \alpha\), and no monotype \(\alpha\) is equal to \(\alpha * \alpha\).
The above is a case of our type system not allowing polymorphic recursion. However, OCaml does allow this when the types are explicitly written. Apparently, polymorphic recursion makes type inference undecidable.
Indeed, this can be very powerful. Consider the following representation of a perfect binary tree, with values at the leaves.
We might want to find the depth for a perfect tree. A typical way might be to use mutual recursion for the two cases. However, it is more elegant to use the same depth function that is polymorphic, from a 'a perfect to ('a * 'a) perfect in the recursive call.