\documentclass[11pt,twoside]{article}

\usepackage{palatino}
\usepackage{amssymb}
\usepackage{proof}
\setlength{\inferLineSkip}{4pt}
% \usepackage{code}
\usepackage{fancyhdr}
% \usepackage{xypic}
\usepackage{comment}
\usepackage[breaklinks=true,
  colorlinks=true,
  citecolor=blue,
  linkcolor=blue,
  urlcolor=blue]{hyperref}
\usepackage{graphicx}
\usepackage{stmaryrd}

\input{lmacros}

\newcommand{\lecdate}{September 13, 2018}
\newcommand{\lecnum}{4}
\newcommand{\lectitle}{Subject Reduction and Normal Forms}

\addtolength{\oddsidemargin}{30pt}
\addtolength{\evensidemargin}{-22pt}

\title{Lecture Notes on \\ \lectitle}
\author{15-814: Types and Programming Languages \\
        Frank Pfenning}
\date{Lecture \lecnum\\ \lecdate}

\begin{document}

\pagestyle{fancyplain}
\setlength{\headheight}{15pt}

\lhead[\fancyplain{}{\bfseries L\lecnum.\thepage}]%
      {\fancyplain{}{\bfseries\lectitle}}
\chead[]{}
\rhead[\fancyplain{}{\bfseries\lectitle}]%
      {\fancyplain{}{\bfseries L\lecnum.\thepage}}
\lfoot[{\small\scshape Lecture Notes}]{{\small\scshape Lecture Notes}}
\cfoot[]{}
\rfoot[{\small\scshape\lecdate}]{{\small\scshape\lecdate}}

\maketitle

\section{Introduction}

In the last lecture we proved some key aspect of a
\emph{representation theorem} for Booleans, namely that the closed
normal forms type $\alpha \arrow (\alpha \arrow \alpha)$ are either
$\mi{true} = \lambda x.\, \lambda y.\, x$ or
$\mi{false} = \lambda x.\, \lambda y.\, y$.  But is our
characterization of normal forms correct?  We would like to prove that
any expression $e$ is either a normal form (that is, satisfies
$e\; \mi{nf}$ or can be reduced.  We prove this theorem first.  Then
we show that typing is preserved under reduction, which means that if
we start with an expression $e$ of type $\tau$ and we reduce it all
the way to a normal form $e'$, the $e'$ will still have type $\tau$.
For the special case where
$\tau = \alpha \arrow (\alpha \arrow \alpha)$ which means that any
expression $e$ of type $\tau$ that has a normal form represents a
Boolean.

\section{Reduction and Normal Form}

Recall our characterization of reduction from Lecture 2, written
here as a collection of inference rules.
\[
  \begin{array}{c}
    \infer[\m{lm}]
    {\lambda x.\, e \longrightarrow \lambda x.\, e'}
    {e \longrightarrow e'}
    \hspace{2em}
    \infer[\m{ap}_1]
    {e_1\, e_2 \longrightarrow e_1'\, e_2}
    {e_1 \longrightarrow e_1'}
    \hspace{2em}
    \infer[\m{ap_2}]
    {e_1\, e_2 \longrightarrow e_1\, e_2'}
    {e_1 \longrightarrow e_2'}
    \\[1em]
    \infer[\beta]
    {(\lambda x.\, e_1)\, e_2 \longrightarrow [e_2/x]e_1}
    {\mathstrut}
  \end{array}
\]
And our characterization of normal forms:
\[
  \begin{array}{c}
    \infer[\m{nf/lam}]
    {\lambda x.\, e\; \mi{nf}}
    {e\; \mi{nf}}
    \hspace{3em}
    \infer[\m{nf/ne}]
    {e\; \mi{nf}}
    {e\; \mi{neutral}}
    \\[1em]
    \infer[\m{ne/app}]
    {e_1\; e_2\; \mi{neutral}}
    {e_1\; \mi{neutral}
      & e_2\; \mi{nf}}
    \hspace{2em}
    \infer[\m{ne/var}]
      {x\; \mi{neutral}}
      {\mathstrut}
  \end{array}
\]
The correctness of this characterization consists of two parts, of
which we will prove one: (i) every term either reduces or is a normal
form, and (ii) normal forms don't reduce.
\begin{theorem}[Reduction and normal forms]\mbox{}\newline
  For every expression $e$, either $e \longrightarrow e'$ for
  some $e'$, or $e\; \mi{nf}$.
\end{theorem}
\begin{proof}
  We are only given an expression $e$, so the proof is likely by
  induction on the structure of $e$.  Let's try!  We write
  $e \longrightarrow$ if there is some $e'$ such that
  $e \longrightarrow e'$.
  \begin{description}
  \item[Case:] $e = x$.  Then
    \begin{tabbing}
      $x\; \mi{neutral}$ \` By rule $\m{ne/var}$ \\
      $x\; \mi{nf}$ \` By rule $\m{nf/ne}$
    \end{tabbing}
  \item[Case:] $e = \lambda x.\, e_1$.  Then
    \begin{tabbing}
      Either $e_1 \longrightarrow$ or $e_1\; \mi{nf}$ \` By ind.hyp. on $e'$ \\[1em]
      \quad $e_1 \longrightarrow$ \` First subcase \\
      \quad $e = \lambda x.\, e_1 \longrightarrow$ \` By rule $\m{lm}$ \\[1em]
      \quad $e_1\; \mi{nf}$ \` Second subcase \\
      \quad $e = \lambda x.\, e_1\; \mi{nf}$ \` By rule $\m{nf/lam}$
    \end{tabbing}
  \item[Case:] $e = e_1\, e_2$.  Then
    \begin{tabbing}
      Either $e_1 \longrightarrow$ or $e_1\;\mi{nf}$ \` By ind.hyp. on $e_1$ \\[1em]
      \quad $e_1 \longrightarrow$ \` First subcase \\
      \quad $e_1\; e_2 \longrightarrow$ \` By rule $\m{ap}_1$ \\[1em]
      \quad $e_1\; \mi{nf}$ \` Second subcase \\
      \quad Either $e_1 = \lambda x.\, e_1'$ or $e_1\; \mi{neutral}$ \` By inversion on $e_1\; \mi{nf}$ \\[1em]
      \qquad $e_1 = \lambda x.\, e_1'$ \` First sub$^2$case \\
      \qquad $e = e_1\, e_2 = (\lambda x.\, e_1')\, e_2 \longrightarrow$ \` By rule $\beta$ \\[1em]
      \qquad $e_1\;\mi{neutral}$ \` Second sub$^2$case \\
      \qquad Either $e_2 \longrightarrow$ or $e_2\; \mi{nf}$ \` By ind.hyp. on $e_2$ \\[1em]
      \qquad\quad $e_2 \longrightarrow$ \` First sub$^3$case \\
      \qquad\quad $e = e_1\, e_2 \longrightarrow$ \` By rule $\m{ap}_2$ \\[1em]
      \qquad\quad $e_2\; \mi{nf}$ \` Second sub$^3$case \\
      \qquad\quad $e = e_1\, e_2\; \mi{neutral}$ \` By rule $\m{ne/app}$
    \end{tabbing}
  \end{description}
\end{proof}

The next in our quest for a representation theorem will be to show
that types are preserved under reduction.  We start with $e : \tau$
and want to know that if $e \longrightarrow^* e'$ where $e'$ is a
normal form, then $e' : \tau$.  This is almost universally proven by
showing that a single step of reduction preserves types, from which
the above follows by a simple induction over the reduction sequence.

We begin by conjecturing a version of the theorem for closed
expression of arbitrary type, because these are the expressions we are
ultimately interested in.

\begin{conjecture}[Subject reduction, v1]\mbox{}\newline
  If $\cdot \vdash e : \tau$ and $e \longrightarrow e'$
  then $\cdot \vdash e' : \tau$.
\end{conjecture}
\begin{attempt}
  In this conjecture, we are given both an expression $e$ and a
  reduction $e \longrightarrow e'$, so a priori there are three
  possible inductions: rule induction on $\cdot \vdash e : \tau$, rule
  induction on $e \longrightarrow e'$, and induction on the structure
  of $e$.  Having done this kind of proof about a gazillion times,
  I know it should go by rule induction on $e \longrightarrow e'$.

  % By rule induction on the deduction of $e \longrightarrow e'$.
  \begin{description}
  \item[Case:]
    \[
      \infer[\m{lm}]
      {\lambda x.\, e_1 \longrightarrow \lambda x.\, e_1'}
      {e_1 \longrightarrow e_1'}
    \]
    where $e = \lambda x.\, e_1'$.  We gather knowledge, and
    then apply inversion because we cannot yet apply the
    induction hypothesis.
    \begin{tabbing}
      $\cdot \vdash \lambda x.\, e_1 : \tau$ \` Assumption \\
      $x : \tau_2 \vdash e_1 : \tau_1$ and
      $\tau = \tau_2 \arrow \tau_1$ for some $\tau_1$ and $\tau_2$
      \` By inversion
    \end{tabbing}
    It looks like we are ready for an appeal to the induction
    hypothesis, but we are stuck because the context in the typing of
    $e_1$ the context is not empty!  We realize have to generalize the
    theorem to allow arbitrary contexts $\Gamma$.
  \end{description}
\end{attempt}

\begin{theorem}[Subject reduction, v2]\mbox{}\newline
  If $\Gamma \vdash e : \tau$ and $e \longrightarrow e'$
  then $\Gamma' \vdash e' : \tau$.
\end{theorem}
\begin{proof}
  By rule induction on the deduction of $e \longrightarrow e'$.
  \begin{description}
  \item[Case:] 
    \[
      \infer[\m{lm}]
      {\lambda x.\, e_1 \longrightarrow \lambda x.\, e_1'}
      {e_1 \longrightarrow e_1'}
    \]
    where $e = \lambda x.\, e_1'$.
    \begin{tabbing}
      $\Gamma \vdash \lambda x.\, e_1 : \tau$ \` Assumption \\
      $\Gamma, x : \tau_2 \vdash e_1 : \tau_1$ and
      $\tau = \tau_2 \arrow \tau_1$ for some $\tau_1$ and $\tau_2$
      \` By inversion \\
      $\Gamma, x : \tau_2 \vdash e_1' : \tau_1$
      \` By induction hypothesis \\
      $\Gamma \vdash \lambda x.\, e_1' : \tau_2 \arrow \tau_1$
      \` By rule $\m{lam}$
    \end{tabbing}
  \item[Case:]
    \[
      \infer[\m{ap}_1]
      {e_1\, e_2 \longrightarrow e_1'\, e_2}
      {e_1 \longrightarrow e_1'}
    \]
    where $e = e_1\, e_2$.  We start again by restating what
    we know in this case and then apply inversion.
    \begin{tabbing}
      $\Gamma \vdash e_1\, e_2 : \tau$ \` Assumption \\
      $\Gamma \vdash e_1 : \tau_2 \arrow \tau$ and \\
      $\Gamma \vdash e_2 : \tau_2$ for some $\tau_2$ \` By inversion \\
    \end{tabbing}
    At this point we have a type for $e_1$ and a reduction for
    $e_1$, so we can apply the induction hypothesis.
    \begin{tabbing}
      $\Gamma \vdash e_1' : \tau_2 \arrow \tau$ \` By ind.hyp.
    \end{tabbing}
    Now we can just apply the typing rule for application.
    Intuitively, in the typing for $e_1\, e_2$ we have replaced $e_1$
    by $e_1'$, which is okay since they $e_1'$ has the type of $e_1$.
    \begin{tabbing}
      $\Gamma \vdash e_1'\, e_2 : \tau$ \` By rule $\m{lam}$
    \end{tabbing}
  \end{description}
  \item[Case:]
    \[
      \infer[\m{ap}_2]
      {e_1\, e_2 \longrightarrow e_1'\, e_2}
      {e_2 \longrightarrow e_2'}
    \]
    where $e = e_1\, e_2$.  This proceeds completely analogous
    to the previous case.
  \item[Case:]
    \[
      \infer[\beta]
      {(\lambda x.\, e_1)\, e_2 \longrightarrow [e_2/x]e_1}
      {\mathstrut}
    \]
    where $e = (\lambda x.\, e_1)\, e_2$.  In this case we
    apply inversion twice, since the structure of $e$ is
    two levels deep.
    \begin{tabbing}
      $\Gamma \vdash (\lambda x.\, e_1)\, e_2 : \tau$ \` Assumption \\
      $\Gamma \vdash \lambda x.\, e_1 : \tau_2 \arrow \tau$ \\
      and $\Gamma \vdash e_2 : \tau_2$ for some $\tau_2$ \` By inversion \\
      $\Gamma, x : \tau_2 \vdash e_1 : \tau$ \` By inversion
    \end{tabbing}
    At this point we are truly stuck, because there is no
    obvious way to complete the proof.
    \begin{tabbing}
      \textbf{To Show:} $\Gamma \vdash [e_2/x]e_1 : \tau$
    \end{tabbing}
    Fortunately, the gap that presents itself is exactly the content
    of the \emph{substitution property}, stated below.  The forward
    reference here is acceptable, since the proof of the substitution
    property does not depend on subject reduction.
    \begin{tabbing}
      $\Gamma \vdash [e_2/x]e_1 : \tau$ \` By the \emph{substitution property} (\ref{prop:subst})
    \end{tabbing}
\end{proof}

\begin{theorem}[Substitution property]\mbox{}\newline
  \label{prop:subst}
  If $\Gamma \vdash e' : \tau'$ and $\Gamma, x : \tau' \vdash e : \tau$
  then $\Gamma \vdash [e'/x]e : \tau$
\end{theorem}
\begin{sketch}
  By rule induction on the deduction of
  $\Gamma, x : \tau' \vdash e : \tau$.  Intuitively, in this deduction
  we can use $x : \tau'$ only at the leaves, and there to conclude
  $x : \tau'$.  Now we replace this leaf with the given derivation of
  $\Gamma \vdash e' : \tau'$ which concludes $e' : \tau'$.  Luckily,
  $[e'/x]x = e'$, so this is the correct judgment.

  There is only a small hiccup: when we introduce a different variable
  $y : \tau''$ into the context in the $\m{lam}$ rule, the contexts of
  the two assumptions no longer match.  But we can apply
  \emph{weakening}, that is, adjoin the unused hypothesis $y : \tau''$
  to every judgment in the deduction of $\Gamma \vdash e' : \tau'$.
  After that, we can apply the induction hypothesis.
\end{sketch}

We recommend you write out the cases of the substitution property in
the style of our other proofs, just to make sure you understand the
details.

The substitution property is so critical that we may elevate it to an
intrinsic property of the turnstile ($\vdash$).  Whenever we write
$\Gamma \vdash J$ for any judgment $J$ we imply that a substitution
property for the judgments in $\Gamma$ must hold.  This is an example
of a \emph{hypothetical} and \emph{generic}
judgment~\cite{MartinLof83}.  We may return to this point in a future
lecture, especially if the property appears to be in jeopardy at some
point.  It is worth remembering that, while we may not want to prove
an explicit substitution property, we still need to make sure that the
judgments we define are hypothetical/generic judgments.

\section{Taking Stock}

Where do we stand at this point in our quest for a representation
theorems for Booleans?  We have the following:
\begin{description}
\item[Reduction and normal forms]\mbox{}\newline
  For any $e$, either $e \longrightarrow$ or $e\; \mi{nf}$ (Theorem
  L4.1)
\item[Representation of Booleans in normal form]\mbox{}\newline
  For any $e$ with
  $\cdot \vdash e : \alpha \arrow (\alpha \arrow \alpha)$ and
  $e\; \mi{nf}$, either $e = \mi{true} = \lambda x.\, \lambda y.\, x$
  or $e = \mi{false} = \lambda x.\, \lambda y.\, y$.  (Theorem
  L3.10(i))
\item[Subject reduction]\mbox{}\newline
  For any $e$ with $\Gamma \vdash e : \tau$ and $e \longrightarrow e'$
  we have $\Gamma \vdash e' : \tau$.  (Theorem L4.3)
\item[Subject reduction to normal form]\mbox{}\newline
  For any $e$ with $\Gamma \vdash e : \tau$ and $e \longrightarrow^* e'$
  with $e'\; \mi{nf}$ we have $\Gamma \vdash e' : \tau$.  (Corollary
  of subject reduction)
\end{description}
Missing at this point are the following theorems
\begin{description}
\item[Normalization]\mbox{}\newline
  If $\Gamma \vdash e : \tau$ then $e \longrightarrow^* e'$ for
  some $e'$ with $e'\; \mi{nf}$.
\item[Confluence]\mbox{}\newline
  If $e \longrightarrow^* e_1$ and $e \longrightarrow^* e_2$ then
  there exists an $e'$ such that $e_1 \longrightarrow^* e'$ and
  $e_2 \longrightarrow e'$.
\end{description}
In this context, \emph{normalization} (sometimes called
\emph{termination}) shows that any closed expression of type
$\alpha \arrow (\alpha \arrow \alpha)$ denotes a Boolean.
\emph{Confluence} (also known as the Church-Rosser property) show that
this Boolean is unique.

We could replay the whole development for the representation of
natural numbers, with some additional complications, but we will
forego this in favor of tackling more realistic programming languages.

\bibliographystyle{alpha}
\bibliography{fp,lfs}

\end{document}
