PROGRAMMING PARADIGMS · PART 3 · SESSION 11
Advanced Type Systems I: Polymorphism
Subtyping, Overloading, Parametricity
based on lecture materials by Jean-Christophe Filliâtre given at ENS Paris and École Polytechnique
Léon Gondelman · Aalborg University Copenhagen · 2026
session 10: monomorphic typing of mini-ML
| expressions | \(e\) | \(::=\) | \(x \mid c \mid op \mid \efun{x}{e} \mid e~e \mid (e,e) \mid \elet{x}{e}{e}\) |
| simple types | \(\tau\) | \(::=\) | \(\mathtt{int} \mid \mathtt{bool} \mid \dots \mid \tau\rightarrow\tau \mid \tau\times\tau\) |
typing judgment \(\Gamma \vdash e : \tau\): “in the environment \(\Gamma\), the expression \(e\) has type \(\tau\)”
\[ \infr{x\in\mathrm{dom}(\Gamma)}{\Gamma\vdash x:\Gamma(x)} \hspace{3em} \infr{\Gamma+x:\tau_1\vdash e:\tau_2}{\Gamma\vdash\efun{x}{e}:\tau_1\rightarrow\tau_2} \hspace{3em} \infr{\Gamma\vdash e_1:\tau'\rightarrow\tau \quad \Gamma\vdash e_2:\tau'}{\Gamma\vdash e_1~e_2:\tau} \]
\[ \infr{\Gamma\vdash e_1:\tau_1 \quad \Gamma\vdash e_2:\tau_2}{\Gamma\vdash(e_1,e_2):\tau_1\times\tau_2} \hspace{3em} \infr{\Gamma\vdash e_1:\tau_1 \quad \Gamma+x:\tau_1\vdash e_2:\tau_2}{\Gamma\vdash\elet{x}{e_1}{e_2}:\tau_2} \]
untypable expressions
with these rules, we cannot type the program \(1~2\)
\[ \infr{\Gamma\vdash \red{1:\tau'\rightarrow\tau} \qquad \Gamma\vdash 2:\tau'}{\Gamma\vdash 1~2:\tau} \]
nor the program \(\efun{x}{x~x}\)
\[ \infr{\infr{\Gamma+x:\tau_1\vdash x:\tau_3\rightarrow\tau_2 \qquad \Gamma+x:\tau_1\vdash x:\tau_3}{\Gamma+x:\tau_1\vdash x~x:\tau_2}}{\Gamma\vdash\efun{x}{x~x}:\tau_1\rightarrow\tau_2} \]
since \(\tau_1=\tau_1\rightarrow\tau_2\) has no solution (types are finite)
several possible types
we can show that
\[ \emptyset\vdash\efun{x}{x} : \mathtt{int} \rightarrow \mathtt{int} \]
but also
\[ \emptyset\vdash\efun{x}{x} : \mathtt{bool} \rightarrow \mathtt{bool} \]
beware: this is not polymorphism
for a given occurrence of \(\efun{x}{x}\) we must choose a type
several possible types
thus, the term \(\elet{f}{\efun{x}{x}}{(f~1,f~\mathtt{true})}\) is not typable
because there is no type \(\tau\) such that
\[ f : \tau\rightarrow\tau \vdash (f~1, f~\mathtt{true}) : \tau_1\times\tau_2 \]
on the other hand,
\[ \left((\efun{x}{x}) ~ (\efun{x}{x})\right) ~ 42 \]
is typable. Exercise (exercise 2, lab warm-up): show it
Exercise (exercise 3, lab warm-up): untypable expressions
solution 2 (PDF) · solution 3 (PDF)
primitives
in particular, we cannot give a single type that is satisfactory for a primitive such as fst; we would have to choose between
| \(\mathtt{int} \times \mathtt{int} \rightarrow \mathtt{int}\) |
| \(\mathtt{int} \times \mathtt{bool} \rightarrow \mathtt{int}\) |
| \(\mathtt{bool} \times \mathtt{int} \rightarrow \mathtt{bool}\) |
| \((\mathtt{int}\rightarrow\mathtt{int})\times \mathtt{int} \rightarrow \mathtt{int} \rightarrow \mathtt{int}\) |
| etc. |
but we can instead give a typing rule for the application of fst:
\[ \infr{\Gamma\vdash e:\tau_1\times\tau_2}{\Gamma\vdash \mathtt{fst}~e:\tau_1} \]
polymorphism
it is restrictive to give a unique type to \(\efun{x}{x}\) in
\[ \elet{f}{\efun{x}{x}}{\dots} \]
likewise, we would like to be able to give “several types” to primitives such as fst or snd
one solution: parametric polymorphism
parametric polymorphism
we extend the algebra of types:
| \(\tau\) | \(::=\) | \(\mathtt{int} ~|~ \mathtt{bool} ~|~ \dots\) | base types |
| \(|\) | \(\tau \rightarrow \tau\) | type of a function |
| \(|\) | \(\tau\times\tau\) | type of a pair |
| \(|\) | \(\alpha\) | type variable |
| \(|\) | \(\forall\alpha.\tau\) | polymorphic type |
free type variables
we write \(\ftv(\tau)\) for the set of free type variables in \(\tau\), defined by
\[ \begin{array}{rcl}
\ftv(\mathtt{int}) & = & \emptyset \\
\ftv(\alpha) & = & \{\alpha\} \\
\ftv(\tau_1\rightarrow\tau_2) & = & \ftv(\tau_1)\cup\ftv(\tau_2) \\
\ftv(\tau_1\times\tau_2) & = & \ftv(\tau_1)\cup\ftv(\tau_2) \\
\ftv(\forall\alpha.\tau) & = & \ftv(\tau)\,\backslash\,\{\alpha\}
\end{array} \]
for an environment \(\Gamma\), we also define
\[ \ftv(\Gamma) = \bigcup_{x\in dom(\Gamma)}\ftv(\Gamma(x)) \]
compute the set of free type variables for each of the following types
| a) | \(\alpha \to \mathtt{int}\) |
| b) | \(\alpha \times \beta \to \alpha\) |
| c) | \(\forall\alpha.\;\alpha \to \beta\) |
| d) | \(\forall\alpha.\;\forall\beta.\;\alpha \times \beta \to \alpha\) |
| e) | \((\forall\alpha.\;\alpha \to \alpha) \to \beta\) |
solution (PDF)
type substitution
we write \(\tau[\alpha\leftarrow\tau']\) for the substitution of \(\alpha\) by \(\tau'\) in \(\tau\), defined by
\[ \begin{array}{rcl}
\mathtt{int}[\alpha\leftarrow\tau'] & = & \mathtt{int} \\
\alpha[\alpha\leftarrow\tau'] & = & \tau' \\
\beta[\alpha\leftarrow\tau'] & = & \beta \quad\text{if }\beta\not=\alpha\\
(\tau_1\rightarrow\tau_2)[\alpha\leftarrow\tau'] & = & \tau_1[\alpha\leftarrow\tau'] \rightarrow \tau_2[\alpha\leftarrow\tau']\\
(\tau_1\times\tau_2)[\alpha\leftarrow\tau'] & = & \tau_1[\alpha\leftarrow\tau'] \times \tau_2[\alpha\leftarrow\tau'] \\
(\forall\alpha.\tau)[\alpha\leftarrow\tau'] & = & \forall\alpha.\tau \\
(\forall\beta.\tau)[\alpha\leftarrow\tau'] & = & \forall\beta.\tau[\alpha\leftarrow\tau'] \quad\text{if }\beta\not=\alpha
\end{array} \]
compute the following substitutions
| f) | \((\alpha \to \mathtt{int})[\alpha \leftarrow \mathtt{bool}]\) |
| g) | \((\alpha \times \beta \to \alpha)[\alpha \leftarrow \mathtt{int} \times \beta]\) |
| h) | \((\forall\alpha.\;\alpha \to \beta)[\beta \leftarrow \alpha \to \alpha]\) |
| i) | \((\forall\alpha.\;\alpha \to \beta)[\alpha \leftarrow \mathtt{int}]\) |
h) What do you observe about the resulting type? Are \((\forall\alpha.\;\alpha \to \beta)\) and \((\forall\gamma.\;\gamma \to \beta)\) essentially the same type?
What would then happen if we do substitution \((\forall\gamma.\;\gamma \to \beta)[\beta \leftarrow \alpha \to \alpha]\)? Compare resulting types of both substitutions: are they equivalent?
solution (PDF)
system F
the rules are exactly the same as before, plus
\[ \infr{\Gamma\vdash e:\tau \qquad \alpha\not\in\ftv(\Gamma)}{\Gamma\vdash e:\forall\alpha.\tau} \]
and
\[ \infr{\Gamma\vdash e : \forall\alpha.\tau}{\Gamma\vdash e : \tau[\alpha\leftarrow\tau']} \]
the resulting system is called system F (J.-Y. Girard / J. C. Reynolds)
example
\[ \infr{\infr{\infr{x:\alpha\vdash x:\alpha}{\vdash\efun{x}{x}:\alpha\rightarrow\alpha}}{\vdash\efun{x}{x}:\forall\alpha.\alpha\rightarrow\alpha} \quad \infr{\infr{\infr{...\vdash f:\forall\alpha.\alpha\rightarrow\alpha}{...\vdash f:\mathtt{int}\rightarrow\mathtt{int}} \quad \vdots}{...\vdash f~1:\mathtt{int}} \quad \infr{\vdots}{...\vdash f~\mathtt{true}:\mathtt{bool}}}{f:\forall\alpha.\alpha\rightarrow\alpha\vdash (f~1,f~\mathtt{true}):\mathtt{int}\times\mathtt{bool}}}{\vdash\elet{f}{\efun{x}{x}}{(f~1,f~\mathtt{true}):\mathtt{int}\times\mathtt{bool}}} \]
primitives
we can now give a satisfactory type to primitives
\[ \begin{array}{rl}
\mathit{fst} :& \forall\alpha.\forall\beta.\alpha\times\beta\rightarrow\alpha \\[1em]
\mathit{snd} :& \forall\alpha.\forall\beta.\alpha\times\beta\rightarrow\beta
\end{array} \]
exercise
we can build a derivation of
\[ \Gamma\vdash\efun{x}{x~x} : (\forall\alpha.\alpha\rightarrow\alpha) \rightarrow (\forall\alpha.\alpha\rightarrow\alpha) \]
Exercise (exercise 5, lab warm-up): do it
solution (PDF)
remark
the condition \(\alpha\not\in\ftv(\Gamma)\) in the rule
\[ \infr{\Gamma\vdash e:\tau \qquad \red{\alpha\not\in\ftv(\Gamma)}}{\Gamma\vdash e:\forall\alpha.\tau} \]
is crucial
without it, we would have
\[ \infr{\infr{\infr{\Gamma+x:\alpha\vdash x:\alpha}{\Gamma+x:\alpha\vdash x:\forall\alpha.\alpha}}{\Gamma\vdash\efun{x}{x} : \alpha\rightarrow\forall\alpha.\alpha}}{\Gamma\vdash\efun{x}{x} : \forall\alpha.\alpha\rightarrow\forall\alpha.\alpha} \]
and we would in particular accept the program
\[ (\efun{x}{x}) ~ 1 ~ 2 \]
bad news
for terms without annotations, the two problems
- inference: given \(e\), does there exist \(\tau\) such that \(\vdash e:\tau\)?
- checking: given \(e\) and \(\tau\), do we have \(\vdash e:\tau\)?
are not decidable
J. B. Wells. Typability and type checking in the second-order lambda-calculus are equivalent and undecidable, 1994.
the Hindley–Milner system
to obtain a decidable type inference, we are going to restrict the power of system F
\(\Rightarrow\) the Hindley–Milner system, used in OCaml, SML, Haskell, etc.
the Hindley–Milner system
we restrict the \(\forall\) quantification to the head of types (prenex quantification)
| \(\tau\) | \(::=\) | \(\mathtt{int} ~|~ \mathtt{bool} ~|~ \dots\) | base types |
| \(|\) | \(\tau \rightarrow \tau\) | type of a function |
| \(|\) | \(\tau\times\tau\) | type of a pair |
| \(|\) | \(\alpha\) | type variable |
| \(\sigma\) | \(::=\) | \(\tau\) | schemes |
| \(|\) | \(\forall\alpha.\sigma\) | |
the environment \(\Gamma\) associates a type scheme with each identifier
and the typing relation now has the form \(\Gamma\vdash e:\sigma\)
examples
in Hindley–Milner, the following types are still accepted
\[ \begin{array}{l}
\forall\alpha. \alpha \rightarrow \alpha \\
\forall\alpha. \forall\beta. \alpha\times\beta \rightarrow \alpha \\
\forall\alpha. \mathtt{bool}\times\alpha\times\alpha\rightarrow\alpha \\
\forall\alpha. (\alpha\rightarrow\alpha)\rightarrow\alpha
\end{array} \]
but not types such as
\[ (\forall\alpha.\alpha\rightarrow\alpha) \rightarrow (\forall\alpha.\alpha\rightarrow\alpha) \]
notation in OCaml
note: in OCaml syntax, prenex quantification is implicit
# fst;;
- : 'a * 'b -> 'a = <fun>
\[ \forall\alpha. \forall\beta. \alpha\times\beta \to \alpha \]
# List.fold_left;;
- : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = <fun>
\[ \forall\alpha. \forall\beta. (\alpha\to\beta\to\alpha)\to\alpha\to \beta~\texttt{list} \to \alpha \]
the Hindley–Milner system
\[ \infr{x\in\mathit{dom}(\Gamma)}{\Gamma\vdash x:\Gamma(x)} \qquad \infr{}{\Gamma\vdash n:\mathtt{int}}... \qquad \infr{}{\Gamma\vdash +:\mathtt{int}\times\mathtt{int}\rightarrow\mathtt{int}} ... \]
\[ \infr{\Gamma+x:\tau_1\vdash e:\tau_2}{\Gamma\vdash\efun{x}{e}:\tau_1\rightarrow\tau_2} \qquad \infr{\Gamma\vdash e_1:\tau'\rightarrow\tau \quad \Gamma\vdash e_2:\tau'}{\Gamma\vdash e_1~e_2:\tau} \]
\[ \infr{\Gamma\vdash e_1:\tau_1 \quad \Gamma\vdash e_2:\tau_2}{\Gamma\vdash(e_1,e_2):\tau_1\times\tau_2} \qquad \infr{\Gamma\vdash e_1:\sigma_1 \quad \Gamma+x:\sigma_1\vdash e_2:\sigma_2}{\Gamma\vdash\elet{x}{e_1}{e_2}:\sigma_2} \]
\[ \infr{\Gamma\vdash e:\sigma \qquad \alpha\not\in\ftv(\Gamma)}{\Gamma\vdash e:\forall\alpha.\sigma} \qquad \infr{\Gamma\vdash e : \forall\alpha.\sigma}{\Gamma\vdash e : \sigma[\alpha\leftarrow\tau]} \]
the Hindley–Milner system
we note that only the let construct makes it possible to introduce a polymorphic type into the environment
\[ \infr{\Gamma\vdash e_1:\sigma_1 \qquad \Gamma+\red{x:\sigma_1}\vdash e_2:\sigma_2}{\Gamma\vdash\elet{x}{e_1}{e_2}:\sigma_2} \]
in particular, we can always type
\[ \elet{f}{\efun{x}{x}}{(f~1,f~\mathtt{true})} \]
with \(f:\forall\alpha.\alpha\rightarrow\alpha\) in the context in order to type \((f~1,f~\mathtt{true})\)
the Hindley–Milner system
on the other hand, the typing rule
\[ \infr{\Gamma+x:\tau_1\vdash e:\tau_2}{\Gamma\vdash\efun{x}{e}:\tau_1\rightarrow\tau_2} \]
does not introduce a polymorphic type (otherwise \(\tau_1\rightarrow\tau_2\) would be ill-formed)
in particular, we can no longer type
\[ \efun{x}{x~x} \]
algorithmic considerations
to program type checking or type inference, we proceed by induction on the structure of the program
but for a given expression, three rules can apply: the rule of the monomorphic system, the rule
\[ \infr{\Gamma\vdash e:\sigma \qquad \alpha\not\in\ftv(\Gamma)}{\Gamma\vdash e:\forall\alpha.\sigma} \]
or else the rule
\[ \infr{\Gamma\vdash e : \forall\alpha.\sigma}{\Gamma\vdash e : \sigma[\alpha\leftarrow\tau]} \]
how do we choose? will we have to proceed by trial and error?
syntax-directed Hindley–Milner system
we are going to modify the presentation of the Hindley–Milner system so that it is syntax-directed, i.e., for every expression, at most one rule applies
the rules have the same expressive power: every closed term is typable in one system if and only if it is typable in the other
syntax-directed Hindley–Milner system
\[ \infr{x\in\mathit{dom}(\Gamma) \quad \red{\tau\le\Gamma(x)}}{\Gamma\vdash x:\tau} \hspace{3em}
\infr{}{\Gamma\vdash n:\mathtt{int}}... \hspace{3em}
\infr{\red{\tau\le\mathit{type}(\mathit{op})}}{\Gamma\vdash\mathit{op}:\tau} \]
\[ \infr{\Gamma+x:\tau_1\vdash e:\tau_2}{\Gamma\vdash\efun{x}{e}:\tau_1\rightarrow\tau_2} \hspace{3em}
\infr{\Gamma\vdash e_1:\tau'\rightarrow\tau \quad \Gamma\vdash e_2:\tau'}{\Gamma\vdash e_1~e_2:\tau} \]
\[ \infr{\Gamma\vdash e_1:\tau_1 \quad \Gamma\vdash e_2:\tau_2}{\Gamma\vdash(e_1,e_2):\tau_1\times\tau_2} \hspace{3em}
\infr{\Gamma\vdash e_1:\tau_1 \quad \Gamma+\red{x:\Gen(\tau_1,\Gamma)}\vdash e_2:\tau_2}{\Gamma\vdash\elet{x}{e_1}{e_2}:\tau_2} \]
syntax-directed Hindley–Milner system
two operations appear
- instantiation, in the rule
\[ \infr{x\in\mathit{dom}(\Gamma) \quad \tau\le\Gamma(x)}{\Gamma\vdash x:\tau} \]
the relation \(\tau\le\sigma\) is read « \(\tau\) is an instance of \(\sigma\) » and is defined by
\[ \tau\le\forall\alpha_1...\alpha_n.\tau' \quad\mbox{iff}\quad \exists\tau_1...\exists\tau_n. ~ \tau=\tau'[\alpha_1\leftarrow\tau_1,...,\alpha_n\leftarrow\tau_n] \]
example: \(\mathtt{int}\times\mathtt{bool}\rightarrow\mathtt{int} \le \forall\alpha.\forall\beta.\alpha\times\beta\rightarrow\alpha\)
syntax-directed Hindley–Milner system
- and generalization, in the rule
\[ \infr{\Gamma\vdash e_1:\tau_1 \qquad \Gamma+x:\Gen(\tau_1,\Gamma)\vdash e_2:\tau_2}{\Gamma\vdash\elet{x}{e_1}{e_2}:\tau_2} \]
where
\[ \Gen(\tau_1,\Gamma) ~\eqdef~ \forall\alpha_1...\forall\alpha_n.\tau_1 \quad\mbox{where}\quad \{\alpha_1,...,\alpha_n\}=\ftv(\tau_1) \backslash \ftv(\Gamma) \]
| a) | is \(\mathtt{int} \to \mathtt{int}\) an instance of \(\forall\alpha.\;\alpha \to \alpha\)? if yes, give the instantiation |
| b) | is \(\mathtt{int} \to \mathtt{bool}\) an instance of \(\forall\alpha.\;\alpha \to \alpha\)? |
| c) | is \(\mathtt{int} \times \mathtt{bool} \to \mathtt{int}\) an instance of \(\forall\alpha.\;\forall\beta.\;\alpha \times \beta \to \alpha\)? |
| d) | compute \(\Gen(\alpha \to \alpha,\; \emptyset)\) |
| e) | compute \(\Gen(\alpha \to \beta,\; \{x : \alpha\})\) |
solution (PDF)
we saw that
\[ \elet{f}{\efun{x}{x}}{(f~1,f~\mathtt{true})} \]
is not typable in the monomorphic system
using the syntax-directed Hindley–Milner rules, build a complete typing derivation showing that this expression is typable; state the type scheme assigned to \(f\) and the overall type of the expression
solution (PDF)
example (1/2)
\[ \infr{\infr{\infr{\alpha\le\alpha}{x:\alpha\vdash x:\alpha}}{\emptyset\vdash\efun{x}{x}:\alpha\rightarrow\alpha} \quad
\Gamma\vdash (f~1,f~\mathtt{true}):\mathtt{int}\times\mathtt{bool}}{\emptyset\vdash\elet{f}{\efun{x}{x}}{(f~1,f~\mathtt{true}):\mathtt{int}\times\mathtt{bool}}} \]
| with | \(\Gamma\) | \(\eqdef\) | \(\emptyset+f:\Gen(\alpha\rightarrow\alpha,\emptyset)\) |
| | \(=\) | \(f:\forall\alpha.\alpha\rightarrow\alpha\) |
example (2/2)
\[ \infr{\infr{\infr{\mathtt{int}\rightarrow\mathtt{int} \le \forall\alpha.\alpha\rightarrow\alpha}{\Gamma\vdash f:\mathtt{int}\rightarrow\mathtt{int}} \quad \vdots}{\Gamma\vdash f~1:\mathtt{int}}
\quad
\infr{\infr{\mathtt{bool}\rightarrow\mathtt{bool} \le \forall\alpha.\alpha\rightarrow\alpha}{\Gamma\vdash f:\mathtt{bool}\rightarrow\mathtt{bool}} \quad \vdots}{\Gamma\vdash f~\mathtt{true}:\mathtt{bool}}}{\Gamma\vdash (f~1,f~\mathtt{true}):\mathtt{int}\times\mathtt{bool}} \]
| with | \(\Gamma\) | \(\eqdef\) | \(\emptyset+f:\Gen(\alpha\rightarrow\alpha,\emptyset)\) |
| | \(=\) | \(f:\forall\alpha.\alpha\rightarrow\alpha\) |
type inference for mini-ML
to infer the type of an expression, problems remain
- in \(\efun{x}{e}\), what type should we give to \(x\)?
- for a variable \(x\), which instance of \(\Gamma(x)\) should we choose?
there is a solution: algorithm W (Milner, Damas)
algorithm W
two ideas
- we use fresh type variables to represent unknown types
- for the type of \(x\) in \(\efun{x}{e}\)
- to instantiate the variables of the scheme \(\Gamma(x)\)
- we determine the values of these variables later, by unification of types at the time of typing an application
unification
let \(\tau_1\) and \(\tau_2\) be two types containing type variables \(\alpha_1,\dots,\alpha_n\)
does there exist an instantiation \(f\) of the variables \(\alpha_i\) such that \(f(\tau_1) = f(\tau_2)\)?
this is called unification
example 1:
| \(\tau_1 = \alpha\times\beta \rightarrow \mathtt{int}\) |
| \(\tau_2 = \mathtt{int}\times\mathtt{bool} \rightarrow \gamma\) |
| solution: \(\alpha=\mathtt{int} ~\land~ \beta=\mathtt{bool} ~\land~ \gamma=\mathtt{int}\) |
example 2:
| \(\tau_1 = \alpha\times\mathtt{int} \rightarrow \alpha\times\mathtt{int}\) |
| \(\tau_2 = \gamma \rightarrow \gamma\) |
| solution: \(\gamma=\alpha\times\mathtt{int}\) |
unification
example 3:
| \(\tau_1 = \alpha \rightarrow \mathtt{int}\) |
| \(\tau_2 = \beta\times\gamma\) |
| no solution |
example 4:
| \(\tau_1 = \alpha \rightarrow \mathtt{int}\) |
| \(\tau_2 = \alpha\) |
| no solution |
unification
\(\unifier(\tau_1,\tau_2)\) determines whether there exists an instance of the type variables of \(\tau_1\) and \(\tau_2\) such that \(\tau_1=\tau_2\)
| \(\unifier(\tau, \tau)\) | \(=\) | success |
| \(\unifier(\tau_1\rightarrow\tau'_1, \tau_2\rightarrow\tau'_2)\) | \(=\) | \(\unifier(\tau_1,\tau_2) ~;~ \unifier(\tau'_1,\tau'_2)\) |
| \(\unifier(\tau_1\times\tau'_1, \tau_2\times\tau'_2)\) | \(=\) | \(\unifier(\tau_1,\tau_2) ~;~ \unifier(\tau'_1,\tau'_2)\) |
| \(\unifier(\alpha,\tau)\) | \(=\) | if \(\alpha\not\in\ftv(\tau)\), replace \(\alpha\) by \(\tau\) everywhere otherwise, failure |
| \(\unifier(\tau,\alpha)\) | \(=\) | \(\unifier(\alpha,\tau)\) |
| \(\unifier(\tau_1,\tau_2)\) | \(=\) | failure in all other cases |
(the lab session is there for you to implement the algorithm W in OCaml)
idea of algorithm W on an example
let us consider the expression \(\efun{x}{+(\mathit{fst}~x,1)}\)
- we give \(x\) the type \(\alpha_1\), a fresh type variable
- the primitive \(+\) has type \(\mathtt{int}\times\mathtt{int}\rightarrow\mathtt{int}\)
- let us type the expression \((\mathit{fst}~x,1)\)
- \(\mathit{fst}\) has the type scheme \(\forall\alpha.\forall\beta.\alpha\times\beta\rightarrow\alpha\)
- we therefore give it the type \(\alpha_2\times\beta_1\rightarrow\alpha_2\) for two new variables
- the application \(\mathit{fst}~x\) forces us to unify \(\alpha_1\) and \(\alpha_2\times\beta_1\), \(\Rightarrow\) \(\alpha_1 = \alpha_2\times\beta_1\)
- \((\mathit{fst}~x,1)\) therefore has type \(\alpha_2\times\mathtt{int}\)
- the application \(+(\mathit{fst}~x,1)\) unifies \(\mathtt{int}\times\mathtt{int}\) and \(\alpha_2\times\mathtt{int}\), \(\Rightarrow\) \(\alpha_2=\mathtt{int}\)
in the end, we obtain the type \(\mathtt{int}\times\beta_1\rightarrow\mathtt{int}\), and if we generalize it (in a let) we obtain \(\forall\beta.\mathtt{int}\times\beta\rightarrow\mathtt{int}\)
algorithm W
we define a function \(W\) that takes as arguments an environment \(\Gamma\) and an expression \(e\) and returns the inferred type for \(e\)
- if \(e\) is a primitive \(op\),
return a trivial instance of its type
- if \(e\) is a pair \((e_1,e_2)\),
compute \(\tau_1 = W(\Gamma,e_1)\)
compute \(\tau_2 = W(\Gamma,e_2)\)
return \(\tau_1\times\tau_2\)
algorithm W
- if \(e\) is an application \(e_1~e_2\),
compute \(\tau_1 = W(\Gamma,e_1)\)
compute \(\tau_2 = W(\Gamma,e_2)\)
let \(\alpha\) be a fresh variable
unifier(\(\tau_1\), \(\tau_2\rightarrow\alpha\))
return \(\alpha\)
results
Theorem (Damas, Milner, 1982)
Algorithm W is correct, complete and determines the principal type:
- if \(W(\emptyset,e) = \tau\) then \(\emptyset \vdash e : \tau\)
- if \(\emptyset\vdash e:\tau\) then \(\tau \le \forall\bar{\alpha}.W(\emptyset,e)\)
Theorem (type safety)
The Hindley–Milner system is safe.
(If \(\emptyset\vdash e:\tau\), then the reduction of \(e\) is infinite or terminates on a value.)
algorithmic considerations (1/2)
there are several ways to implement unification
algorithmic considerations (2/2)
there are also several ways to code algorithm W
- with explicit schemes and by computing \(\Gen(\tau,\Gamma)\)
type schema = { tvars: TVset.t; typ: typ; }
- with levels
\[ \infr{\Gamma\vdash_{n+1}e_1:\tau_1 \qquad \Gamma+x:(\tau_1,n)\vdash_n e_2:\tau_2}{\Gamma\vdash_n\elet{x}{e_1}{e_2}:\tau_2} \]
see tutorial
extensions
we can extend mini-ML in many ways
- constructed types (\(n\)-tuples, lists, sum and product types)
- references (see the slides below)
- recursion (not shown in this slides)
constructed types
we have already seen pairs
lists pose no difficulty
\[ \begin{array}{rl} \texttt{[]} :& \forall\alpha.\, \alpha~\texttt{list} \\ \texttt{::} :& \forall\alpha.\, \alpha \times \alpha~\texttt{list} \rightarrow \alpha~\texttt{list} \end{array} \]
\[ \infr{\Gamma\vdash e_1 :\tau~\texttt{list} \qquad \Gamma\vdash e_2 : \tau_1 \qquad \Gamma+x:\tau+y:\tau~\texttt{list}\vdash e_3 : \tau_1}{\Gamma\vdash \mathtt{match}~e_1~\mathtt{with}~\texttt{[]}\rightarrow e_2~|~\texttt{::}(x,y)\rightarrow e_3 : \tau_1} \]
this generalizes easily to sum and product types
references
for references, we might naively think that it is enough to add the primitives
\[ \begin{array}{rl} \texttt{ref} :& \forall\alpha.\, \alpha\rightarrow\alpha~\texttt{ref} \\ \texttt{!} :& \forall\alpha.\, \alpha~\texttt{ref} \rightarrow \alpha \\ \texttt{:=}\, :& \forall\alpha.\, \alpha~\texttt{ref} \rightarrow \alpha \rightarrow \texttt{unit} \end{array} \]
references
unfortunately, this is incorrect!
| \(\elet{r}{\texttt{ref}~(\efun{x}{x})}{}\) | \(\red{r:\forall\alpha.(\alpha\rightarrow\alpha)~\texttt{ref}}\) |
| \(\elet{\_}{r ~ \texttt{:=} ~ (\efun{x}{x~1})}{}\) | |
| \(!r ~ \texttt{true}\) | boom! |
this is the problem of polymorphic references
polymorphic references
to avoid this problem, there is an extremely simple solution, namely a syntactic restriction on the let construct
Definition (value restriction, Wright 1995)
A program satisfies the value restriction criterion if every let subexpression whose type is generalized is of the form
\[ \elet{x}{v_1}{e_2} \]
where \(v_1\) is a value.
polymorphic references
in practice, we continue to write
\[ \elet{r}{\texttt{ref}~(\efun{x}{x})}{\dots} \]
but the type of \(r\) is not generalized
as if we had written
\[ (\efun{r}{\dots})~(\texttt{ref}~(\efun{x}{x})) \]
polymorphic references
in OCaml, a nongeneralized variable has the form '_a
# let x = ref (fun x -> x);;
val x : ('_a -> '_a) ref
the value restriction is also slightly relaxed to allow safe expressions such as applications of constructors
# let l = [fun x -> x];;
val l : ('a -> 'a) list = [<fun>]
polymorphic references
a few small annoyances remain
# let id x = x;;
val id : 'a -> 'a = <fun>
# let f = id id;;
val f : '_a -> '_a = <fun>
# f 1;;
- : int = 1
# f true;;
This expression has type bool but is here used with type int
# f;;
- : int -> int = <fun>
polymorphic references
the solution: expand so as to make a function appear, i.e., a value
# let f x = id id x;;
val f : 'a -> 'a = <fun>
(this is called \(\eta\)-expansion)
polymorphic references
in the presence of the module system, reality is even more complex
given a module M
module M : sig
type 'a t
val create : int -> 'a t
end
am I allowed to generalize the type of M.create 17?
the answer depends on the nature of the type 'a t: no for an array, yes for a list, etc.
in OCaml, a variance indication makes it possible to distinguish the two
type +'a t (* we can generalize *)
type 'a u (* we cannot *)
read Relaxing the value restriction, J. Garrigue, 2004
reading
two books related to this course
- Benjamin C. Pierce, Types and Programming Languages
(also related to lecture 2)
- Xavier Leroy and Pierre Weis, Le langage Caml
(the last chapter explains inference with levels)