%%% 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}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{26}                  %% Lecture number
\title{Strong Normalization}   %% Title of lecture
\author{Michael O'Connor and Jeffrey Zhang}       %% name of scribe
\date{31 October, 2005}     %% 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!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
In the last class, we proved that each term in the simply typed lambda-calculus
would never get stuck.  Today, we want to show that it will actually terminate
(this property is known as ``strong normalization'').

Formally, what we want to prove is that $\vdash e:\tau \Rightarrow e\Downarrow$.
We will prove this by induction, but we will need a more sophisticated induction
hypothesis than we have been using so far.  Observe that we couldn't just do induction
naively: in, for example, the application case if $e_1$ and $e_2$ terminate,
that doesn't allow us to conclude that $e = e_1~e_2$ does; this isn't even true in
untyped lambda calculus: take $e_1=e_2=\lam x{xx}$.  As an aside, the method which
we will use can also be used to prove the adequacy of the denotational semantics;
i.e., $\vdash e: B \wedge e\longrightarrow^* b \Leftrightarrow [\![\vdash
e: B]\!]\emptyset = b$, where $B$ is a base type.

The form of the proof will be the following: we will define a relation $R_\tau$
for each type $\tau$, so that $R_\tau(e)\Rightarrow e\Downarrow$ for any $e$.
Informally, we will prove that if $\vdash e:\tau$, then $R_\tau(e)$ holds
using an inductive hypothesis assuming that $R_\tau(e')$ holds for $e'$
appearing in the derivation of the typing judgement $\vdash e:\tau$ (actually
our inductive hypothesis will have a few other elements in it to take care of
free variables).

So, what is $R_\tau$? We will define it by indcution on $\tau$.  We have
\begin{displaymath}
R_B(e)\Leftrightarrow \vdash e: B \wedge e \Downarrow
\end{displaymath}
for base types $B$.  For arrow types, we have
\begin{displaymath}
R_{\tau_1\to\tau_2}(e)\Leftrightarrow \vdash e: \tau_1\to\tau_2 \wedge
e\Downarrow \wedge \forall e'[R_{\tau_1}(e')\Rightarrow R_{\tau_2}(e~e')]
\end{displaymath}

Let's prove a couple of lemmas about this relation. 

\textbf{Lemma 1.} $R_\tau(e)\Rightarrow e\Downarrow$ 

Proof: Lemma 1 follows simply because $e\Downarrow$ is one of the conjuncts in the definition
of $R_\tau$, for any $\tau$.

\textbf{Lemma 2.} $(\vdash e:\tau \wedge e\longrightarrow e' \wedge R_\tau(e'))
\Rightarrow R_\tau(e)$

Proof: We will prove this by structural induction on $\tau$.
Since we know by preservation that  $\vdash e:\tau$ and 
$e\longrightarrow e'$ implies that $\vdash e':\tau$, this lemma follows immediately
for base types $B$ since $R_\tau(e)$ reduces to the assertion that $e$ terminates,
and if $e'$ terminates and $e'$ is one step away from $e$, then $e$ terminates.

Now, let $\vdash e:\tau_1\rightarrow\tau_2$, $e\longrightarrow e'$ and
$R_\tau(e')$ hold.  Since $R_\tau(e')$, $e'$ terminates, so $e$ terminates just
as above.  So, to prove that $R_\tau(e)$, we need only prove that
$\forall e''[R_{\tau_1}(e'')\Rightarrow R_{\tau_2}(e~e'')]$.  So
let $e''$ be such that $R_{\tau_1}{e''}$.  

Since $e\longrightarrow e'$, we have $e~e'' \longrightarrow e'~e''$.  Since
$R_\tau(e')$, we have that $R_{\tau_2}(e'~e'')$. Thus, by inductive
hypothesis we have $R_{\tau_2}(e~e'')$.  We are done.

Note that we also have:

\textbf{Lemma 2.5} $(\vdash e:\tau \wedge e\longrightarrow e' \wedge R_\tau(e))
\Rightarrow R_\tau(e')$

Proof: The case when $\tau$ is a base type is the same as before, since
$e\Downarrow$ implies that any application of rewrite rules to $e$ always
terminates.

Now, let $\vdash e:\tau_1\rightarrow\tau_2$, $e\longrightarrow e'$ and
$R_\tau(e)$ hold.  Since $R_\tau(e)$, $e$ terminates, so $e'$ terminates just
as above.  So, to prove that $R_\tau(e')$, we need only prove that
$\forall e''[R_{\tau_1}(e'')\Rightarrow R_{\tau_2}(e'~e'')]$.  So
let $e''$ be such that $R_{\tau_1}{e''}$.  

Since $e\longrightarrow e'$, we have $e~e'' \longrightarrow e'~e''$.  Since
$R_\tau(e)$, we have that $R_{\tau_2}(e~e'')$. Thus, by inductive
hypothesis we have $R_{\tau_2}(e'~e'')$.  We are done.


We need one more construct before we prove our main theorem.  Let $\gamma$
be a finite substitution $\{x_1 \mapsto v_1,\ldots x_n\mapsto v_n\}$,
where each $x_i$ is a variable and each $v_i$ is a value.  Given
a set of typing assumptions $\Gamma$ ($= \{x_1:\tau_1,\ldots, x_n:\tau_n$), define 
\begin{displaymath}
\gamma\models\Gamma \Leftrightarrow \dom(\gamma)=\dom(\Gamma) \wedge
R_{\Gamma(x)}(\gamma(x))
\end{displaymath}
If $e$ is an expression with
free variables in $\dom(\gamma)$, we define $\gamma(e)=e\{\gamma(x_i)/x_i\}_{1\leq i\leq n}$.

\textbf{Lemma 3.} $\Gamma\vdash e:\tau \wedge \gamma\models \Gamma \Rightarrow
\vdash \gamma(e):\tau$.

Proof: This is immediate from the definitions.

\textbf{Main Theorem} If $\vdash e:\tau$, $e\Downarrow$.

Proof:
We will prove strong normalization by proving the following by induction on the
derivation of the typing judgement:
\begin{displaymath}
\Gamma\vdash e:\tau \wedge \gamma \models \Gamma \Rightarrow R_\tau(\gamma(e))
\end{displaymath}
Note that this will give what we want by taking $\Gamma=\gamma=\emptyset$.

If $e=b$, then $\gamma(e) = b$ for any $\gamma$, and $e$ is already a value. Thus,
this case is immediate.

If $e = x$ (where $x$ is a variable), then $\gamma(e) = \gamma(x)$ and
$R_{\Gamma(x)}(\gamma(x))$ by the assumption that $\gamma \models \Gamma$. 
(Note that if $\Gamma \vdash e:\tau$, then all free variables in $e$ must
be in $\Gamma$.)

Assume $e = e_0~e_1$ and let $\Gamma\vdash e_0~e_1:\tau$ and $\gamma\models \Gamma$.
We must have $\Gamma \vdash e_0:\tau'\to\tau$ and $\Gamma \vdash e_1:\tau'$.
By induction, then, we have $R_{\tau'\to\tau}(\gamma(e_0))$ and
$R_{\tau'}(\gamma(e_1))$.  Since $R_{\tau'\to\tau}(\gamma(e_0))$,
we have $\forall e' [R_\tau'(e')\Rightarrow R_{\tau}(\gamma(e_0)~e')]$.
So, we have $R_{\tau}(\gamma(e_0)~\gamma(e_1))$.  Since $\gamma(e_0)~\gamma(e_1)
= \gamma(e_0~e_1)$, this case is done.

Now consider the case $e = \lam {x:\tau_1}e_2$ where the type of 
$e$ is $\tau = \tau_1\to\tau_2$ and the type of $e_2$ is $\tau_2$.
We want to show $R_{\tau_1\to\tau_2}(\gamma(e))$, so we must show
that $\vdash \gamma(e):\tau$, $\gamma(e)\Downarrow$ and that $\forall e' [R_{\tau_1}(e')\Rightarrow
R_{\tau_2}(\gamma(e)~e')]$.  We have that $\gamma(e)\Downarrow$ simply because
$\gamma(e)$ is already a value, and the fact that $\vdash \gamma(e):\tau$ follows
from Lemma 3.

So let $e'$ be such that $R_{\tau_1}(e')$.

Since $\Gamma \vdash \lam {x:\tau_1}e_2 : \tau$, we must
have $\Gamma, \{x\mapsto \tau_1\} \vdash e_2 :\tau_2$.
Thus, by induction, if $v$ is a value such that 
$R_{\tau_1}(v)$, then we have $R_{\tau_2}((\gamma[x\mapsto v])(e_2))$.
Since $R_{\tau_1}(e')$, $e'$ terminates in a value $v'$.
By Lemma 2.5, $R_{\tau_1}(v')$. Thus we have
$R_{\tau_2}((\gamma[x\mapsto v'])(e_2)$.  So,
by Lemma 2, we have $R_{\tau_2}(\gamma(e))$.
We are done.

In Winskell, section 11.4 there is an adequacy proof along these lines.



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

