\documentclass[11pt,twoside]{article}
\usepackage[authoryear,semicolon]{natbib}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textgreek}
\usepackage[color=yellow]{todonotes}
\setlength{\marginparwidth}{1.25in}
\usepackage{xifthen}
\usepackage{fullpage}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{stmaryrd}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{code}
\usepackage{proof}
\usepackage{colonequals}
\usepackage{comment}
\usepackage{textcomp}
\usepackage[us]{optional}
\usepackage{color}
\usepackage{url}
\usepackage{verbatim}
\usepackage{graphics}
\usepackage{mathpartir}
\usepackage{import}
\usepackage{stackengine}
\usepackage{scalerel}

\allowdisplaybreaks[1]       %mildly permissible to break up displayed equations

\input{notes-defns}

\begin{document}
\title{How to (Re)Invent Tait's Method%
  \footnote{Copyright \copyright{} Robert Harper.  All Rights Reserved.}
}
\author{Robert Harper}
\date{Spring Semester, 2021}

\nocite{pfpl2}

\maketitle{}

\section{Introduction}

Two of the most important developments in type theory were the invention, by W.~W. Tait, of
\emph{Tait's Method} for function types, which was later extended by $J.Y. Girard$ to \emph{Girard's
  Method} for type quantification, both of which were incorporated into a general theory of
\emph{logical relations} for a wide range of type theories.

The problem considered by Tait was to prove that $\beta$-reduction for the simply typed
$\lambda$-calculus is \emph{strongly normalizing}, which is usually defined to mean that there are
\emph{no} infinite $\beta$-reduction sequences starting with a well-typed term:
$M=M_{0}\rightarrow_{\beta} M_{1}\rightarrow \dots$. A better definition, of more immediate utility, is the validity of
\emph{transfinite induction on reduction}, stated as follows: for any property $\mathcal{P}$ of
typed $\lambda$-terms, to show that $\mathcal{P}$ holds for every such term, it is enough to show, for
every well-typed typed term $M$, if all of its immediate $\beta$-reducts satisfy $\mathcal{P}$, then so
does $\mathcal{P}$. More succinctly, \begin{displaymath}
  (\forall M{:}\tau (\forall N{:}\tau. M\rightarrow_{\beta} N\supset \mathcal{P}(N))\supset\mathcal{P}(M))  \supset \forall M{:}\tau.\mathcal{P}(M).
\end{displaymath}
The importance of strong normalization lies exactly in the utility afforded by this principle for
proving other properties. For example, using transfinite induction on reduction it is possible to
prove that weak confluence (every one-step split can be reconciled) implies confluence (transitivity
of the ``has a common reduct'' relation.) This statement is false for untyped terms; it is precisely
the strong normalization property that ensures its validity for well-typed terms.

The question considered here is a related, but technically much simpler, problem, the
termination of a deterministic head reduction strategy for a simply typed
$\lambda$-calculus. The simplification compared to Tait's original proof is that head reduction
is defined only for closed terms, avoiding the need to consider open terms in the
development of Tait's method. The type system considered here has unit, product, and
function types, augmented with a two-element type of \emph{observables}, corresponding to
the familiar ``accept/reject'' formulation in the study of abstract machines.

The method can be generalized in numerous ways, all of which are of central importance and
unparalleled utility in type theory. In one direction it can be extended to account for
open terms, using \emph{pre-sheaves} of logical relations, which are also known as
\emph{Kripke models}. In another it can be extended to binary relations as a means of
studying \emph{equality} of typed terms. In another direction it can be extended to
\emph{dependent types}, which consider type-indexed families of types; this was chiefly
developed by Per Martin-L\"{o}f.

\section{Simple Types}

\newcommand{\onety}{\mathbf{1}}
\newcommand{\ansty}{\mathbf{2}}
\newcommand{\prodty}[2]{{#1}\times{#2}}
\newcommand{\funty}[2]{{#1}\to{#2}}
\newcommand{\natty}{\mathbf{N}}

\newcommand{\yesex}{\mathbf{\mathsf{Y}}}
\newcommand{\noex}{\mathbf{\mathsf{N}}}
\newcommand{\unitex}{\mathbf{\mathsf{\star}}}
\newcommand{\pairex}[2]{\langle {#1}\mathbin{,} {#2}\rangle}
\newcommand{\lftex}[1]{{#1}\cdot {\mathbf{\mathsf{1}}}}
\newcommand{\rhtex}[1]{{#1}\cdot\mathbf{\mathsf{2}}}
\newcommand{\lamex}[3]{\lambda_{#1}({#2}\mathbin{.}{#3})}
\newcommand{\appex}[2]{\mathbf{\mathsf{ap}}(#1,#2)}

The syntax of the language considered here is given by the following grammar:
\begin{align*}
  A & \bnfdef \onety \bnfalt \ansty \bnfalt \prodty{A_{1}}{A_{2}} \bnfalt \funty{A_{1}}{A_{2}} \\
  M & \bnfdef x \bnfalt \yesex \bnfalt \noex \bnfalt \unitex \bnfalt \pairex{M_{1}}{M_{2}} \bnfalt
      \lftex{M} \bnfalt \rhtex{M} \bnfalt \lamex{A}{x}{M} \bnfalt \appex{M_{1}}{M_{2}}
\end{align*}

The statics is entirely standard, defining the typing judgment $\Gamma\vdash M:A$, in such a way
that the structural properties are admissible. Contraction and exchange are accounted for
by treating the typing context $\Gamma$ as a finite set of variable typings
$x_{1}{:} A_{1},\dots,x_{n}{:}A_{n}$ in which $x_{i}\not=x_{j}$ whenever $i\not=j$.
Weakening is built-in by stating all rules with an ambient typing context $\Gamma$ that goes
along for the ride.  See Figure~\ref{fig:statics} for the definition of typing.
Substitution (transitivity), which states that if
$\Gamma,\IsOfVar{x}{A}\vdash\IsOf{N}{B}$, and $\Gamma\vdash\IsOf{M}{A}$, then $\Gamma\vdash\IsOf{\Subst{M}{x}{N}}{B}$,
is readily proved by induction on the first premise.

\begin{figure}[tp]
  
  \begin{mathpar}

    \inferrule[var]
    {\strut}
    {\Gamma,x{:}A\vdash x : A}

    \inferrule[yes]
    {\strut}
    {\Gamma\vdash\yesex:\ansty}

    \inferrule[no]
    {\strut}
    {\Gamma\vdash\noex:\ansty}

    \inferrule[unit]
    {\strut}
    {\Gamma\vdash\unitex:\onety}

    \inferrule[pair]
    {\Gamma\vdash M_1:A_1 \\ \Gamma\vdash M_2:A_2}
    {\Gamma\vdash\pairex{M_1}{M_2}:\prodty{A_1}{A_2}}

    \inferrule[lft]
    {\Gamma\vdash M:\prodty{A_1}{A_2}}
    {\Gamma\vdash\lftex{M}:A_1}
    
    \inferrule[rht]
    {\Gamma\vdash M:\prodty{A_1}{A_2}}
    {\Gamma\vdash\rhtex{M}:A_2}
    
    \inferrule[lam]
    {\Gamma,x{:}A_1\vdash M_2:A_2}
    {\Gamma\vdash\lamex{A_1}{x}{M_2}:\funty{A_1}{A_2}}

    \inferrule[app]
    {\Gamma\vdash M_1:\funty{A_2}{A} \\ \Gamma\vdash M_2:A_2}
    {\Gamma\vdash \appex{M_1}{M_2}:A}

  \end{mathpar}

  \caption{Typed $\lambda$-Calculus Statics}
  \label{fig:statics}

\end{figure}

The dynamics is given by a transition system $M\StepsTo M'$ between closed $\lambda$-terms of some type.
Any closed typed term is a valid initial state. Final states are defined along with transition in
Figure~\ref{fig:dynamics}.

\begin{figure}[tp]
  
  \begin{mathpar}
    
    \inferrule[yes]
    {\strut}
    {\IsFinal{\yesex}}

    \inferrule[no]
    {\strut}
    {\IsFinal{\noex}}

    \inferrule[unit]
    {\strut}
    {\IsFinal{\unitex}}

    \inferrule[pair]
    {\strut}
    {\IsFinal{\pairex{M_1}{M_2}}}

    \inferrule[lam]
    {\strut}
    {\IsFinal{\lamex{A_1}{x}{M_2}}}

    \inferrule[lft]
    {M\StepsTo M'}
    {\lftex{M}\StepsTo\lftex{M'}}
    
    \inferrule[rht]
    {M\StepsTo M'}
    {\rhtex{M}\StepsTo\rhtex{M'}}

    \inferrule[lft-pair]
    {\strut}
    {\lftex{\pairex{M_1}{M_2}}\StepsTo M_1}

    \inferrule[rht-pair]
    {\strut}
    {\rhtex{\pairex{M_1}{M_2}}\StepsTo M_2}

    \inferrule[app]
    {M_1\StepsTo M_1'}
    {\appex{M_1}{M_2}\StepsTo\appex{M_1'}{M_2}}

    \inferrule[app-lam]
    {\strut}
    {\appex{\lamex{A_2}{x}{M}}{M_2}\StepsTo \Subst{M_2}{x}{M}}

  \end{mathpar}

  \caption{Typed $\lambda$-Calculus Dynamics}
  \label{fig:dynamics}

\end{figure}

\begin{theorem}[Preservation]
  If $M:A$ and $M\StepsTo M'$, then $M':A$.
\end{theorem}
\begin{proof}
  By induction on transition.
\end{proof}

\section{Termination Proof}

The goal is to prove termination for terms of observable type:
\begin{theorem}[Termination]
  If $M:\ansty$, then either $M\MultiStepsTo{} \yesex$ or $M\MultiStepsTo{}\noex$.
\end{theorem}
That is, any complete program either accepts or rejects.

Given the statement of the theorem, practically the only move available is to proceed by induction
on typing.  Let us consider some cases.
\begin{description}
\item[\textsc{var}] Does not apply to closed terms.
\item[\textsc{yes}] Immediate, as $\IsFinal{\yesex}$.
\item[\textsc{no}] Immediate, as $\IsFinal{\noex}$.
\item[\textsc{unit}] Does not apply, not of type $\ansty$. 
\item[\textsc{pair}] Does not apply, not of type $\ansty$.
\item[\textsc{lft}] By induction, \emph{um} \dots.
\item[\textsc{rht}] By induction, \emph{um} \dots.
\item[\textsc{fun}] Does not apply, not of type $\ansty$. 
\item[\textsc{app}] By induction applied to the first premise, \emph{um} \dots.
\end{description}
All cases are trivial, or completely unclear.

Well, because the subterms of a term of type $\ansty$ need not have type $\ansty$, it
seems clear that it is necessary to strengthen the theorem to say something about terms of
any type.
\begin{lemma}
  If $M:A$, then there exists $N$ such that $\IsFinal{N}$ and $M\MultiStepsTo{} N$.
\end{lemma}
The lemma suffices for the theorem because of the definition of finality for terms of type
$\ansty$.  Let us consider the proof of this lemma.
\begin{description}
\item[\textsc{var}] Does not apply to closed terms.
\item[\textsc{yes}] Immediate, as $\IsFinal{\yesex}$.
\item[\textsc{no}] Immediate, as $\IsFinal{\noex}$.
\item[\textsc{unit}] Immediate, as $\IsFinal{\unitex}$.
\item[\textsc{pair}] Immediate, as $\IsFinal{\pairex{M_{1}}{M_{2}}}$.
\item[\textsc{lft}] By induction there exists $N$ such that $\IsFinal{N}$ and $M\MultiStepsTo{} N$.  By
  preservation and the definition of finality $N$ must be of the form $\pairex{N_{1}}{N_{2}}$.
  By the definition of transition
  \[
    \lftex{M}\MultiStepsTo{}\lftex{\pairex{N_{1}}{N_{2}}}\StepsTo N_{1}.
  \]
  But now what?
\item[\textsc{rht}] Analogous, what to do with $N_{2}$?
\item[\textsc{fun}] Immediate, as $\IsFinal{\lamex{A_{1}}{x}{M_{2}}}$.
\item[\textsc{app}] By induction applied to the first premise there exists $N_{1}$ such
  that $\IsFinal{N_{1}}$ and $M_{1}\MultiStepsTo{} N_{1}$.  By preservation and the
  definition of finality $N_{1}$ must have the form $\lamex{A_{2}}{x}{M}$.  By the
  definition of transition
  \[
    \appex{M_{1}}{M_{2}}\MultiStepsTo{}\appex{\lamex{A_{2}}{x}{M}}{M_{2}}\StepsTo\Subst{M_{2}}{x}{M}.
  \]
  But now what?
\end{description}

In the projection cases the components of the pair are general terms about which nothing
is known.  In the application case the value of the first argument is a
$\lambda$-abstraction whose body is an open term (with free variable $x$) about which nothing is
known.  This suggests strengthening the lemma by proving a property called
\emph{hereditary termination}, which is stronger than mere termination.  It should have
the following characteristics in order to push through the proof of the strengthened lemma
below:
\begin{enumerate}
\item A hereditarily terminating expression of type $\onety$ should be terminating, and
  hence
  transition to $\unitex$.

\item A hereditarily terminating expression of type $\ansty$ should be terminating, and
  hence transition to either $\yesex$ or $\noex$.

\item A hereditarily terminating expression of type $\prodty{A_{1}}{A_{2}}$ should
  terminate with a pair $\pairex{N_{1}}{N_{2 }}$ such that both $N_{1}$ and $N_{2}$ are
  hereditarily terminating.

\item A hereditarily terminating expression of type $\funty{A_{2}}{A}$ should
  terminate with a function $\lamex{A_{2}}{x}{M}$ such that if $M_{2}$ is hereditarily
  terminating of type $A_{2}$, then $\Subst{M_{2}}{x}{M}$ should be hereditarily
  terminating at type $A$.
\end{enumerate}
These conditions constitute a \emph{definition} of the property \emph{$M$ is hereditarily
  terminating at type $A$}, which is defined for closed $M:A$. The first two cases are
given outright; the others rely on hereditary termination at constituent types of a
compound type. \emph{Thus, hereditary termination at a type is defined by induction on the
  structure of the type}.\footnote{This move is precluded in the second-order case, which
  is what gave rise to Girard's Method, for which see~\cite{harper:girard}.  The issue is
  an unavoidable circularity in any definition of hereditary termination for polymorphic
  types.}

\begin{lemma}
  If $M:A$, then $M$ is hereditarily terminating at type $A$.
\end{lemma}

The proof proceeds as before by induction on typing.  The problematic cases are handled by
the definition of hereditary termination.  The cases for the constants are immediate by
the definition of hereditary termination at base type.  But what about the pair and
function cases?
\begin{description}
\item[\textsc{pair}] By induction $M_{1}$ is hereditarily terminating at $A_{1}$ and
  $M_{2}$ is hereditarily terminating at type $A_{2}$; the goal is to show that
  $\pairex{M_{1}}{M_{2}}$ is hereditarily terminating at type $\prodty{A_{1}}{A_{2}}$.  A
  pair is already a value (final state), so an appeal to the inductive hypothesis suffices
  to finish the proof!
\item[\textsc{fun}] To show that $\lamex{A_{1}}{x}{M_{2}}$ is hereditarily terminating at
  $\funty{A_{1}}{A_{2}}$, show that whenever $M_{1}$ is hereditarily terminating at
  $A_{1}$, then $\Subst{M_{1}}{x}{M_{2}}$ is hereditarily terminating at $A_{2}$.  But
  what to do?
\end{description}

The problem now is that in the function case there is no inductive hypothesis available to
give us the necessary information about the \emph{open} term $M$, which has one free
variable, $x$, in it.  The lemma must be strengthened once more to account for open terms,
even though the desired property applies only to closed terms!

To state the required result, define $\gamma:\Gamma$ to mean that $\gamma$ is a map assigning to each
variable $x$ declared in $\Gamma$ a term of the type specified in $\Gamma$.  The action
$\hat{\gamma}(M)$ of a substitution $\gamma$ is defined inductively on the structure of
$M$ to replace each free variable $x$ with the term assigned to it by $\gamma$.

A substitution $\gamma$ is hereditarily terminating at $\Gamma$ iff whenever
$\Gamma\vdash\IsOf{x}{A}$, then $\gamma(x)$ is hereditarily terminating at type $A$.

Define $\Gamma\gg M\in A$ to mean that if $\gamma$ is hereditarily terminating at $\Gamma$, then $\hat{\gamma}(M)$
is hereditarily terminating at type $A$.  We may then state the main lemma as follows:
\begin{lemma}
  If $\Gamma\vdash M:A$, then $\Gamma\gg M\in A$.
\end{lemma}
The proof is by induction on typing derivations.  The critical case is the last one in the
preceding attempt, for which the strengthened statement provides precisely what is needed
to push the proof through.  The other cases require a bit more care in handling the
application of $\gamma$ to the terms in question, but there are no further obstacles to worry
about.

\medskip

And that is Tait's Method!

\newcommand{\zerocst}{\textsf{zero}}
\newcommand{\succcst}[1]{\textsf{succ}(#1)}
\begin{exercise}
  Extend the termination proof to account for the type $\natty$ of natural numbers,
  generated by zero and successor, and interpreted by iteration, under a lazy dynamics
  whereby any successor is a value, regardless of the form of the predecessor.  Define
  hereditary termination at type $\natty$ as the strongest property $\mathcal{P}$ of
  $\IsOf{M}{\natty}$ such that
  \begin{enumerate}
  \item If $M\MultiStepsTo{} \zerocst$, then $\mathcal{P}(N)$, and
  \item If $M\MultiStepsTo{} \succcst{N}$ and $\mathcal{P}(N)$, then $\mathcal{P}(M)$.
  \end{enumerate}
  From this definition derive a suitable induction principle to use in the proof of
  termination by Tait's method.
\end{exercise}

\bibliographystyle{plainnat}
\bibliography{notes}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% fill-column: 90
%%% auto-fill-mode: t
%%% End:
