%%% This is the scribe notes template for CS611
%%% There are several comments preceded by CS611: and boxed in %%%%'s 
%%% which indicate where macros should be altered to set up the header
%%% for the paper.  Your Notes should go at the comment SCRIBE NOTES GO HERE!.

%%% In the various .sty files that accompany this .tex file you will    
%%% find LaTeX macros that make it easier to typeset inference rules    
%%% and programming language constructs.  You must make sure that the   
%%% file proof.sty is in a path searched by LaTeX when you try to       
%%% use this file.  Take a look to see what macros are available--it    
%%% will save you time and make the notes look better.  Feel free to    
%%% extend the set of macros--post them to the newsgroup and contact    
%%% the course staff if you come up with some good ones so they can be  
%%% added to the template.                                              

%%% This template includes examples of how to use some of the macros
%%% to give you an idea of how they work.  (Delete the examples when
%%% you do your scribing.)

\documentclass{article} 

\usepackage{611-lecture}
\usepackage{amsmath,amssymb,amsthm,amsfonts}
\usepackage{url}

\renewcommand\emptyset\varnothing
\newcommand{\inL}{\ensuremath{\mathsf{inL}}}
\newcommand{\inR}{\ensuremath{\mathsf{inR}}}
\newcommand\caseof[3]{\mathsf{case}~{#1}~\mathsf{of}~{#2}~|~{#3}}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{definition}[theorem]{Definition}

\newcommand{\Z}{\mathbb{Z}}
\newcommand{\group}[1]{\left\langle{#1}\right\rangle}

%\theoremstyle{definition}
%\newtheorem*{defn}{Definition}
\newcommand{\nondet}{\left[\!\kern1pt\right]}
\renewcommand\phi\varphi
\renewcommand\wp[2]{\mathsf{wp}~{#1}~{#2}}
\newcommand\wlp[2]{\mathsf{wlp}~{#1}~{#2}}
\renewcommand\({\begin{eqnarray*}}
\renewcommand\){\end{eqnarray*}}

\newcommand\LOOKUP[2]{\mathrm{LOOKUP}~{#1}~{#2}}
\newcommand\UPDATE[3]{\mathrm{UPDATE}~{#1}~{#2}~{#3}}
\newcommand\MALLOC[2]{\mathrm{MALLOC}~{#1}~{#2}}
\newcommand\EMPTY{\mathrm{EMPTY\mbox{-}STORE}}
\renewcommand\dom[1]{\mathrm{dom}\,{#1}}
\newcommand\p[2]{\langle{#1},\,{#2}\rangle}
\newcommand\bigcdot{\mathrel{\raisebox{1pt}{$\scriptscriptstyle\bullet$}}}
\newcommand\holed[1]{[\,#1\,]}
\newcommand\hole{\holed\bigcdot}
\newcommand\context[1]{E\kern1pt\holed{#1}}
\newcommand\contextHole{\context\bigcdot}
\newcommand\goesto[2]{\underset{#2}{\overset{#1}\to}}
\newcommand\ifthenelse[3]{\mathsf{if\ }#1\mathsf{\ then\ }#2\mathsf{\ else\ }#3}
\newcommand\ifpthenelse[3]{\mathsf{ifp\ }#1\mathsf{\ then\ }#2\mathsf{\ else\ }#3}
\newcommand\whiledo[2]{\mathsf{while\ }#1\mathsf{\ do\ }#2}
\newcommand\letin[3]{\mathsf{let\ }#1 = #2\mathsf{\ in\ }#3}
\newcommand\letrec[5]{\mathsf{letrec\ }#1 = #2\mathsf{\ and\ \ldots\ and\ }#3 = #4\mathsf{\ in\ }#5}
\newcommand\letrecone[3]{\mathsf{letrec\ }#1 = #2\mathsf{\ in\ }#3}
\newcommand\true{\ensuremath{\mathsf{true}}}
\newcommand\false{\ensuremath{\mathsf{false}}}
\newcommand\error{\ensuremath{\mathsf{error}}}
\newcommand\pca[3]{\{#1\}\kern1pt{#2}\kern1pt\{#3\}}
\newcommand\states{\Set{St}}
\newcommand\rtc{^{\textstyle *}}
\newcommand\sat\vDash
\newcommand\force\vdash

\newcommand\hyphen{\mbox{-}}
\newcommand\lookup[2]{\nm{LOOKUP}~#1~\mquote{#2}}
\newcommand\update[3]{\nm{UPDATE}~#1~\mquote{#2}~#3}
\newcommand\SBk[1]{\SB{#1}k}
\newcommand\fix[1]{\mathsf{fix}\,{#1}}

\newlength\reasonwidth
\setlength\reasonwidth{3cm}
\newcommand\reasoning[1]{\def\longest{#1}\settowidth{\reasonwidth}{$\displaystyle\longest$}\addtolength{\reasonwidth}{5mm}}
\newcommand\reason[2]{\makebox[\reasonwidth][l]{$\displaystyle{#1}$}\mbox{#2}}

\renewcommand\inj[1]{\mathsf{in}_{#1}}
\newcommand\proj[1]{\pi_{#1}}
\newcommand{\dlt}{\sqsubseteq}
\newcommand\floor[1]{\lfloor{#1}\rfloor}
\newcommand\cf[1]{[\kern1pt{#1}\kern1pt]}
\newcommand\SBpr[1]{\SB{#1}\,\phi\,\rho}
\renewcommand\C[3]{\Tr C{#1}\kern1pt{#2}\kern1pt{#3}}
\renewcommand\Cr[1]{\C{#1}\Gamma\rho}

\newcommand\judge[3]{{#1}\force{#2}:{#3}}
\newcommand\Gjudge[2]{\judge\Gamma{#1}{#2}}%
\newcommand\forceUSN{\mathrel{\makebox[2pt][l]{$\force$}\raisebox{-3pt}[0pt][0pt]{\tiny{\textit{USN}}}}}
\newcommand\judgeUSN[3]{{#1}\forceUSN{#2}:{#3}}
\renewcommand\C[3]{\Tr C{#1}\kern1pt{#2}\kern1pt{#3}}
\renewcommand\Cr[1]{\C{#1}\Gamma\rho}
\newcommand\Irred[1]{\ensuremath{\mathrm{Irred}(#1)}}

\newcommand\seq[3]{#1_{#2},\ldots,#1_{#3}}
\newcommand\substtwo[5]{\subst{#1}{#2}{#3,\,#4/#5}}
\newcommand\substlist[5]{\subst{#1}{#2}{#3,\ldots,#4/#5}}
\newcommand\Unify[1]{\mathrm{Unify}(#1)}

\newenvironment{proofof}[1]{\addtolength{\topsep}{1mm}\begin{trivlist}\item[]\hspace{\parindent}{\em Proof of #1.}}{\qed\end{trivlist}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{28}                  %% Lecture number
\title{The Polymorphic $\lambda$-Calculus}   %% Title of lecture
%\author{Kevin Markman and Ryan Peterson}       %% name of scribe
\date{8 November 2006}     %% Date of lecture, e.g., 1 January 2001
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% See 611.sty for a variety of macros that will be helpful in
% typesetting the lecture. Here are a few of particular interest:
%
% "x"	 	x in keyword font (e.g., "if", "#t")
% _x_	 	x in italics
% \nm{n}   	n in slanted font (used for abbreviations)
% <e> 	 	e in angle brackets
% \lt 	 	less-than sign
% \gt 	 	greater-than sign
% \SB{x}	x in semantic brackets
% \Tr x{y} 	x[[y]] with x in calligraphic font
%          	(if x is more than a single character, use \Tr{x}{y})

\begin{document}
\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: SCRIBE NOTES GO HERE!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Recap}

Last lecture we saw how to unify types.
\(
\Unify{\emptyset} &\definedas& I\\
\Unify{\alpha=\alpha, E} &\definedas& \Unify E\\
\Unify{\alpha=\tau, E} &\definedas& \{\tau/\alpha\}\cdot\Unify{E\{\tau/\alpha\}},\quad\alpha\notin\FV\tau\\
\Unify{\sigma_1\to\tau_1=\sigma_2\to\tau_2, E} &\definedas& \Unify{\sigma_1=\sigma_2,\tau_1=\tau_2, E}
\)
where $I$ is the identity substitution $\alpha\mapsto\alpha$.  Substitutions are applied from left to right, so the composition $S~T$ means: do $S$ first, then do $T$.

\section{Type Checker}

SML code for typechecking is provided on the web site:\\
\url{http://www.cs.cornell.edu/Courses/cs611/2006fa/lectures/typeCheck.sml}\\
Feel free to experiment with it.

\section{Polymorphic $\lambda$-Calculus}

Suppose we have base types "Int" and "Bool".
The problem with the simple type inference mechanism that we have presented
is that we do not have quite as much _polymorphism_\footnote{Greek for ``many forms''}
as we would like.
For example, consider a program that binds a variable to the identity
function, then applies it to an "Int" and also to a "Bool".
\begin{equation}
\begin{array}l
\letin f{\lam xx}{}\\
\hspace{1em}\ifthenelse{(f~"true")}{(f~3)}{(f~4)}
\end{array}\label{eqn:polyexample}
\end{equation}
The type checker encounters the "Bool" first and says
that the function is of type $"Bool\to Bool"$, then gives
an error when it sees the "Int"
parameter, whereas we really want it to be interpreted
as type $"Bool\to Bool"$ when applied to a "Bool"
parameter and $"Int\to Int"$ when applied to an "Int"
parameter.

\newcommand\fa[2]{\forall{#1}\kern1pt.\kern1pt{#2}}%
We can handle this by introducing a new type constructor that quantifies over types.
\begin{eqnarray}
\tau &::=& "Int" \bnf "Bool" \bnf \alpha \bnf \sigma\to\tau \bnf \fa\alpha\tau\label{eqn:polytypes}
\end{eqnarray}
The type $\fa\alpha\tau$ can be viewed as a _polymorphic type_
or _type schema_, a pattern with type variables that can be instantiated
to obtain actual types.  For example, the polymorphic type of the identity
function will be the type schema
\[
\fa\alpha{\alpha\to\alpha}
\]
and the type of the $K$ combinator $\lam{xy}x$ will be
\[
\fa\alpha{\fa\beta{\alpha\to\beta\to\alpha}}.
\]
There will be rules that allow us to delay the instantiation of the type
variables until the function is applied.  Thus we can interpret the identity
function as $"Int\to Int"$ or $"Bool\to Bool"$ depending on context.

The resulting language is called the \emph{polymorphic $\lambda$-calculus}.
In this new language, the terms and evaluation rules are the same, but the
types are defined by (\ref{eqn:polytypes}).  All the terms that were previously
well-typed will still be well-typed, but there will be more well-typed terms
than before; for example, (\ref{eqn:polyexample}).

\section{Typing Rules}

In addition to the old typing rules
\[
\begin{array}c
\judge\Gamma n{"Int"}\quad\mbox{(and similarly for other constants)} \qquad
\judge{\Gamma,\,x:\tau}x\tau\\[1em]
\dfrac{\judge\Gamma e{\sigma\to\tau}\quad\judge\Gamma d\sigma}{\judge\Gamma{e~d}\tau} \qquad
\dfrac{\judge{\Gamma,\,x:\sigma}e\tau}{\judge\Gamma{\lam xe}{\sigma\to\tau}}
\end{array}
\]
we add the following two new rules for polymorphic types:
\[
\dfrac{\judge\Gamma e\tau\quad\alpha\notin\FV\Gamma}{\judge\Gamma e{\fa\alpha\tau}} \qquad
\dfrac{\judge\Gamma e{\fa\alpha\tau}}{\judge\Gamma e{\subst\tau\sigma\alpha}}
\]
These are called the _generalization rule_ and the _instantiation rule_, respectively.

The notation $\subst\tau\sigma\alpha$ refers to the safe substitution of the type $\sigma$ for the type variable $\alpha$ in $\tau$.  Here the binding operator $\forall\alpha$ binds the type variable $\alpha$ in the same way that $\lambda x$ binds the variable $x$ in $\lambda$-terms, and the notions of scope, free and bound variables are the same.  In particular, one can $\alpha$-convert type variables as necessary to avoid the capture of free type variables when performing substitutions.

The premise of the generalization rule includes the proviso $\alpha\notin\FV\Gamma$.  The idea here is that the type judgement $\judge\Gamma e\tau$ must hold without any assumptions involving $\alpha$; if so, then we can conclude that $\alpha$ could have been any type $\sigma$, and the type judgement $\judge\Gamma e{\subst\tau\sigma\alpha}$ would also hold.

\section{Examples}

Here is a derivation of the polymorphic type of $K$ in this system.
\begin{center}
\mbox{}
\infer{\judge{}{\lam x{\lam yx}}{\fa\alpha{\fa\beta{\alpha\to\beta\to\alpha}}}}
      {\infer{\judge{}{\lam x{\lam yx}}{\fa\beta{\alpha\to\beta\to\alpha}}}
             {\infer{\judge{}{\lam x{\lam yx}}{\alpha\to\beta\to\alpha}}
                    {\infer{\judge{x:\alpha}{\lam yx}{\beta\to\alpha}}
                           {\judge{x:\alpha,\,y:\beta}x\alpha}}}}
\end{center}
Starting from $\judge{x:\alpha,\,y:\beta}x\alpha$, two applications of the abstraction rule yield $\judge{}{\lam x{\lam yx}}{\alpha\to\beta\to\alpha}$, then two applications of the generalization rule yield $\judge{}{\lam x{\lam yx}}{\fa\alpha{\fa\beta{\alpha\to\beta\to\alpha}}}$.

Some terms are typable in this system that were not typable before.
For example, the term $\lam x{xx}$ is typable:
\begin{center}
\mbox{}
\infer{\judge{}{\lam x{xx}}{\fa\beta{(\fa\alpha\alpha)\to\beta}}}
      {\infer{\judge{}{\lam x{xx}}{(\fa\alpha\alpha)\to\beta}}
            {\infer{\judge{x:\fa\alpha\alpha}{xx}{\beta}}
                  {\infer{\judge{x:\fa\alpha\alpha}x{\alpha\to\beta}}
                        {\judge{x:\fa\alpha\alpha}x{\fa\alpha\alpha}}
                  &
                  \infer{\judge{x:\fa\alpha\alpha}x{\alpha}}
                        {\judge{x:\fa\alpha\alpha}x{\fa\alpha\alpha}}}}}
\end{center}
Unfortunately, this type is not too meaningful, because _nothing_ has type $\fa\alpha\alpha$.  This type is said to be _uninhabited_, and we give it a name: "Void".  However, by a similar argument, we can show that $\lam x{xx}$ also has type $\fa\beta{(\fa\alpha{\alpha\to\alpha})\to{(\beta\to\beta)}}$, which _is_ meaningful.
\begin{center}
\mbox{}
\infer{\judge{}{\lam x{xx}}{\fa\beta{(\fa\alpha{\alpha\to\alpha})\to{(\beta\to\beta)}}}}
      {\infer{
            \judge{}{\lam x{xx}}{(\fa\alpha{\alpha\to\alpha})\to{(\beta\to\beta)}}}
            {\infer{\judge{x:\fa\alpha{\alpha\to\alpha}}{xx}{{\beta\to\beta}}}
                  {\infer{\judge{x:\fa\alpha{\alpha\to\alpha}}x{{(\beta\to\beta)}\to{(\beta\to\beta)}}}
                        {\judge{x:\fa\alpha{\alpha\to\alpha}}x{\fa\alpha{\alpha\to\alpha}}}
                  &
                  \infer{\judge{x:\fa\alpha{\alpha\to\alpha}}x{\beta\to\beta}}
                        {\judge{x:\fa\alpha{\alpha\to\alpha}}x{\fa\alpha{\alpha\to\alpha}}}}}}
\end{center}

Although $\lam x{xx}$ is typable, the paradoxical combinator $\Omega = (\lam x{xx})~(\lam x{xx})$ is not, and neither is the $Y$ combinator.  This is because the language is still strongly normalizing.  This means that the polymorphic $\lambda$-calculus is not Turing complete, that is, it cannot simulate arbitrary Turing machines.

Worse, types inference is undecidable, so the programmer must sometimes provide types.

\section{Let-Polymorphism}

We can regain decidability of type inference by placing some restrictions on the use of the type quantifier $\forall\alpha$.  Specifically, we will only allow it at the top level; that is, we will only allow polymorphic type expressions of the form $\fa{\alpha_1\ldots\forall\alpha_n}\tau$, where $\tau$ is quantifier-free:
\[
\begin{array}{r@{\hspace{2em}}rcl}
\mbox{quantifier-free terms} & \tau &::=& "Int" \bnf "Bool" \bnf \alpha \bnf \tau_1\to\tau_2\\
\mbox{polymorphic terms} & \pi &::=& \tau \bnf \fa\alpha\pi
\end{array}
\]
We will also modify our rules so that it can only be introduced in the context of a "let" statement.  Thus we will modify our definition of terms to include a "let" statement:
\(
e &::=& \cdots \bnf \letin x{e_1}{e_2}
\)
and replace the generalization rule with the "let" rule
\(
\dfrac{\judge\Gamma d\sigma\qquad\judge{\Gamma,\,x:\fa{\alpha_1\ldots\forall\alpha_n}\sigma}e\tau\qquad\{\alpha_1,\ldots,\alpha_n\}=\FV\sigma-\FV\Gamma}{\judge\Gamma{\letin xde}\tau}
\)
So type schemas are only used to type "let" expressions.
For this reason, this approach is called \emph{let-polymorphism}.

\section{Let-Polymorphism and ML}

The type systems of ML and Haskell are based on let-polymorphism.  We previously
considered $\letin xde$ to be equivalent to $(\lam xe)~d$, but in SML,
the former may be typable in some cases when the latter is not:

\begin{code}
- let val f = fn x $\Rightarrow$ x in if (f true) then (f 3) else (f 4) end;
val it = 3 : int
- (fn f $\Rightarrow$ if (f true) then (f 3) else (f 4)) (fn x $\Rightarrow$ x);
stdIn:17.27-17.32 Error: operator and operand don't agree [literal]
  operator domain: bool
  operand:         int
  in expression:
    f 3
stdIn:17.38-17.43 Error: operator and operand don't agree [literal]
  operator domain: bool
  operand:         int
  in expression:
    f 4
\end{code}
In theory, let-polymorphism can cause the type checker to run in exponential time, but in practice
this is not a problem.

\section{System F}

In the Church-style simply-typed $\lambda$-calculus, we annotated binding
occurrences of variables with their types.  The corresponding version
of the polymorphic $\lambda$-calculus is called _System F_.  Here we
explicitly abstract terms with respect to types and explicitly instantiate
by applying an abstracted term to a type.  We augment the syntax with
new terms and types:
\(
e\ \ ::=\ \ \cdots \bnf \Lam\alpha e \bnf e~\tau
&\qquad&
\tau\ \ ::=\ \ b \bnf \tau_1 \to \tau_2 \bnf \alpha \bnf \fa\alpha\tau
\)
where $b$ are the base types (e.g., "Int" and "Bool").
The new terms are _type abstraction_ and _type
application_, respectively.  Operationally, we have
\(
(\Lam\alpha e)~\tau &\to& \subst e\tau\alpha.
\)
This just gives the rule for instantiating a type schema. Since these
reductions only affects the types, they can be performed at compile time.

The typing rules for these constructs need a notion of well-formed
type.  We introduce a new environment $\Delta$ that maps type variables
to their _kinds_ (for now, there is only one kind: "type").  So
$\Delta$ is a partial function with finite domain mapping types to $\{"type"\}$.
Since the range is only a singleton, all $\Delta$ does for right now
is to specify a set of types, namely $\dom\Delta$ (it will get more
complicated later).  As before, we use the notation $\Delta,\,\alpha:"type"$
for the partial function $\Delta["type"/\alpha]$.
For now, we just abbreviate this by $\Delta,\,\alpha$.

\newcommand\Judge[4]{\judge{#1;\,#2}{#3}{#4}}

We have two classes of type judgements:
\(
\judge\Delta\tau{"type"} &\qquad& \Judge\Delta\Gamma e\tau
\)
For now, we just abbreviate the former by $\Delta\force\tau$.  These judgements
just determine when $\tau$ is well-formed under the assumptions $\Delta$.
The typing rules for this class of judgements are:
\[
\begin{array}{c@{\hspace{1cm}}c@{\hspace{1cm}}c@{\hspace{1cm}}c}
\Delta,\,\alpha\force\alpha
&
\Delta\force b
&
\dfrac{\Delta\force\sigma\quad\Delta\force\tau}{\Delta\force\sigma\to\tau}
&
\dfrac{\Delta,\,\alpha\force\tau}{\Delta\force\fa\alpha\tau}
\end{array} 
\]
Right now, all these rules do is use $\Delta$ to keep track
of free type variables.  One can show that $\Delta\force\tau$ iff
$\FV\tau\subseteq\dom\Delta$.

The typing rules for the second class of judgements are:
\[
\begin{array}c
\begin{array}{c@{\hspace{1cm}}c@{\hspace{1cm}}c}
\infer
{\Judge\Delta{\Gamma,\,x:\tau}x\tau}
{\Delta\force\tau}
&
\infer
{\Judge\Delta\Gamma{(e_0~e_1)}\tau}
{\Judge\Delta\Gamma{e_0}{\sigma\to\tau} & \Judge\Delta\Gamma{e_1}{\sigma}}
&
\infer
{\Judge\Delta\Gamma{(\lam{x:\sigma}e)}{\sigma\to\tau}}
{\Judge\Delta{\Gamma,\,x:\sigma}e{\tau} & \Delta\force\sigma}
\end{array}\\[14pt]
\begin{array}{c@{\hspace{1cm}}c}
\infer
{\Judge\Delta\Gamma{(e~\sigma)}{\subst\tau\sigma\alpha}}
{\Judge\Delta\Gamma e{\fa\alpha\tau} & \Delta\force\sigma}
&
\infer{\Judge\Delta\Gamma{(\Lam\alpha e)}{\fa\alpha\tau}}
{\Judge{\Delta,\,\alpha}\Gamma e\tau & \alpha\notin\FV\Gamma}
\end{array}
\end{array}
\]
One can show that if $\Judge\Delta\Gamma e\tau$ is derivable, then $\tau$ and all types occurring in annotations in $e$ are well-formed.  In particular, $\judge{}e\tau$ only if $e$ is a closed term and $\tau$ is a closed type, and all type annotations in $e$ are closed types.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}



