 %%% 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{23} %% Lecture number
\title{Soundness of Typing Rules}   %% Title of lecture
\author{Stephen Chong, Prakash Linga, Andrew Myers}  %% name of scribe
\date{25 October 2004}    %% Date of lecture
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% See 611.sty for a variety of macros that will be helpful in
% typesetting the lecture
%
% quotes can be used for typewriter font
% <> can be used for angle brackets

\begin{document}

\maketitle

\section{Soundness from the Operational Perspective}
\nopagebreak
We will now look at the soundness of the $\lambda^\rightarrow$
typing rules from the operational perspective. What this means is,
\begin{center}
{\em Typing rules are sound} $\iff$ {\em no well-formed program gets stuck.}
\end{center}

Note that we use well-formed and well-typed equivalently
for this language. To be more precise,
$$
\vdash e : \tau~\wedge~e \stepsone^* e' ~~\Rightarrow ~~e' \in \nm{Value} \vee \exists e''. e' \stepsone e''
$$

To show this we will use a 2-step approach. We will show the following two lemmas:
\begin{itemize}
\item {\em Preservation}: As we evaluate a program its type is preserved at each step.
$$
\vdash e : \tau~\wedge~e \stepsone e' \Rightarrow~\vdash e' : \tau
$$
\item {\em Progress}: Every program is either a value or can be stepped into another program (and using {\em preservation} lemma this will be of the same type!)
$$
\vdash e : \tau \Rightarrow e = v \in \nm{Value} \vee \exists e''. e \stepsone e''
$$
\end{itemize}
Note that with these two lemmas soundness easily follows. Preservation
says every step preserves the type, so we use
induction on the number of steps taken in $e \stepsone^* e'$ to show
that $e'$ must have the same type as $e$. Then Progress can
be applied to $e'$ to show that evaluation isn't stuck there.
We will now set out to prove these two lemmas.

\subsection{Proof of Preservation Lemma}

Assuming $e : \tau \wedge e \stepsone e'$
we need to show that $\vdash e' : \tau$.
We will do this by well-founded induction on typing derivations. (Note
that the typing derivations are finite and therefore the relation of
subderivation is well-founded.) The property we are trying to show on
typing derivations is:

\[
P(\vdash e : \tau) \iff \forall e'. e \stepsone e' \Rightarrow \vdash e' : \tau
\]

Given that we know $e \stepsone e'$, there are three
possibilities corresponding to the three evaluation rules:

\[\begin{array}{ccc}
\infer[(L)]{e_0~e_1 \stepsone e'_0 e_1}{e_0 \stepsone e'_0} &
\infer[(R)]{v_0~e_1 \stepsone v_0 e'_1}{e_1 \stepsone e'_1} &
\infer[(\beta)]{(\lam{x\ty\tau}{e})~v \stepsone \subst{e}{v}{x}}{}
\end{array}\]

\begin{itemize}
\item Case (L): $e = e_0~e_1$.

Because we have a typing derivation for $e_0~e_1$, we know
that there are typing derivations for $e_0$ too. We must
have $\vdash e_0 : \tau_1\to\tau$ and $\vdash e_1 : \tau_1$
for some type $\tau_1$.  Because
the typing derivation for $e_0$ is a subderivation of the typing
derivation, by the induction hypothesis
the step $e_0 \stepsone e_0'$ also preserves type. So
we know $\vdash e_0' : \tau_1\to\tau$, and we now have
both of the premises necessary to construct the proof that
$e_0'~e_1$ has the desired type $\tau$.

\item Case (R): $e = v_0~e_1$

This case is symmetrical to case (L); in this case $e_1$ is
the subexpression making the step.

\item Case 3: $e = (\lam{x\ty\tau_1}{e_0'})~v_1$

The typing derivation of $\vdash e : \tau$ must look like this:

\[
   \infer{
	 \vdash(\lam{x \ty \tau_1} {e_0'})~v_1 : \tau
	 }
	 {
	 \infer {
		\vdash (\lam{x \ty \tau_1} {e_0'}) : \tau_1 \to \tau
		}
		{
		x \ty \tau_1 \vdash e_0' : \tau
		}	
	 &
	 \vdash v_1 : \tau_1
	 }
\]

We know that $e \stepsone \subst{e_0'}{v_1}{x}$, so 
we need to show that $e_0\{v_1/x\} : \tau$ using the facts
$x \ty \tau_1 \vdash e_0' : \tau$ and $\vdash v_1 : \tau_1$.
Our induction hypothesis doesn't help us here; we need to
prove this separately. It follows as a special case of a
{\em Substitution Lemma} which captures the type
preservation of $\beta$-reduction.

\end{itemize}

\subsection{{\em Substitution} lemma}

$$
\Gamma, x \ty \tau' \vdash e : \tau ~\wedge~ \vdash v : \tau' \Rightarrow \Gamma \vdash e\{v/x\} : \tau
$$

We will prove this by induction on the typing derivation of $e$.

\begin{itemize}

\item Case 1: $e = b$.
In this case the result trivially holds.

\item Case 2: a variable
\begin{itemize}
\item Case 2a: $e = x$.
Then $e\{v/x\} = v$ and $\tau' = \tau$ and hence the result holds in this case.
We have $\vdash v : \tau$ and need $\Gamma \vdash v : \tau$.
This holds because adding new things to the typing context
doesn't cause us to lose the ability to produce a typing
derivation. This property can be expressed in the following
_weakening lemma_ whose proof we leave as an exercise:
\[
\Gamma \vdash e : \tau ~\wedge~ x\not\in\FV{e}
\Rightarrow \Gamma, x\ty\tau' \vdash e: \tau
\]
Then we can get from $\emptyset \vdash v : \tau$ to $\Gamma \vdash v : \tau$
by applying the weakening lemma enough times to add in the
finite number of bindings in $\Gamma$.

\item Case 2b: $e = y \neq x$.
In this case $e\{v/x\} = e$ and therefore the result trivially holds. 
\end{itemize}

\item Case 3: $e = e_0~e_1$.
Using the definition of substitution, $\subst evx = \subst{e_0}vx~\subst{e_1}vx$.

Using the derivation of $\Gamma, x \ty \tau' \vdash e : \tau$ we have
$\Gamma, x \ty \tau' \vdash e_0 \ty \tau_1 \to \tau$ and
$\Gamma, x \ty \tau' \vdash e_1 \ty \tau_1$ :

\[
	\infer{
	\Gamma, x \ty \tau' \vdash e \ty \tau
	}
 	{
	\Gamma, x \ty \tau' \vdash e_0 \ty \tau_1 \to \tau 
	&
	\Gamma, x \ty \tau' \vdash e_1 \ty \tau_1
	}
\]

Therefore we can use the induction hypothesis to obtain $\Gamma \vdash e_0\{v/x\} \ty \tau_1 \to \tau$ and $\Gamma \vdash e_1\{v/x\} \ty \tau_1$.
This therefore implies $\Gamma \vdash e\{v/x\} \ty \tau$ via the application rule.

\item Case 4: a lambda abstraction
\begin{itemize}

\item Case 4a: $e \equiv \lambda x \ty \tau''~e_2$.

Then $e\{v/x\} = e$ and therefore the result holds trivially.
\item Case 4b: $e \equiv \lambda y \ty \tau''~e_2$.

Note that $y \notin FV[v]$, so $e\{v/x\} = \lam{y \ty \tau''}{e_2\{v/x\}}$ and the typing of $e$ looks like the following, where $\tau = \tau'' \to \tau_2$ :

\[
	\infer{
		\Gamma, x \ty \tau', \vdash (\lam{y \ty \tau''}{e_2}) \ty \tau'' \to \tau_2
	}
	{
	  \Gamma, x \ty \tau', y \ty \tau'' \vdash e_2 \ty \tau_2 
	}
\]

Use [exchange] and [app] to derive $\Gamma, y \ty \tau'', x \ty \tau'
\vdash e_2 \ty \tau_2$ and then use the inductive hypothesis to obtain
$\Gamma, y \ty \tau'' \vdash e_2\{v/x\} \ty \tau_2$. By [abs], therefore,
$\Gamma \vdash (\lam{y \ty \tau''} {e_2\{v/x\}}) \ty \tau$ and we are done.

\end{itemize}

\end{itemize}

\section{Progress Lemma}

To finish the proof of Soundness for the typed lambda calculus,
we need to prove Progress.
The Progress Lemma captures the idea that we cannot get stuck when evaluating a well-formed expression.\\

{\bf Progress Lemma}: $\Gamma\proves e:\tau\Rightarrow e\in \Set{Value} \vee \exists
e'.e\stepsone e'$

{\bf Proof}:
We use induction on the typing derivation of $e$.
Remember the definition of an expression in $\lambda^\to$:
$$e ::= b\bnf x\bnf\lam{x\ty\tau}e\bnf e_0~e_1$$
So we have four cases:

\begin{itemize}
 \item Case $e=b$: We have that $b\in \Set{Value}$.
 \item Case $e=x$: This case is not possible because we would have $\Gamma\proves x:\tau$ and from the empty environment we cannot assign any type to $x$.
 \item Case $e=\lm x{\tau_0}{e_1}$: We have that $e\in \Set{Value}$.
 \item Case $e=e_0\,e_1$: We know that there is a typing derivation for $\Gamma\proves e_0\,e_1:\tau$ and this derivation must have the form:
$$\infer{\Gamma\proves e_0\,e_1:\tau}{\Gamma\proves e_0:\tau'& \Gamma\proves e_1:\tau'\to\tau}$$
By the induction hypothesis, $e_0\in \Set{Value} \vee \exists
e_0'.e_0\stepsone e_0'$ and $e_1\in \Set{Value}
 \vee \exists e_1'.e_1\stepsone e_1'$.
We have four possibilities now:

\begin{itemize}
 \item Both $e_0$ and $e_1$ are values. Since $e_0$ is a value with
 an arrow type, it has to be an abstraction. 
Say $e_0$ is $\lm x{\tau'}{e_2}$ and $e_1$ is some value $v$.
Then 
$$e=(\lm x{\tau'}{e_2})v\stepsone e_2\{v/x\}$$
so, $e'=e_2\{v/x\}$ as desired.
  \item $e_0$ is not a value. 
Then $\exists e_0'.e_0\stepsone e_0'$ and we have
$$\infer{e_0\,e_1\stepsone e_0'e_1}{e_0\stepsone e_0'}$$
  \item $e_0$ is some value $v$, but $e_1$ is not a value.
Then $\exists e_1'.e_1\stepsone e_1'$ and we have
$$\infer{v\,e_1\stepsone v\,e_1'}{e_1\stepsone e_1'}$$
\end{itemize}
\end{itemize}
And this finishes the proof.


\end{document}
