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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{4}                  %% Lecture number
\title{$\lambda$ Encodings and Recursion}   %% Title of lecture
%\author{Bryant Adams}        %% name of scribe
\date{6 September 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{Encoding Booleans, Natural Numbers, and Data Structures}

Even though the pure $\lambda$-calculus consists only of $\lambda$-terms, we can represent and manipulate common data objects like integers, Boolean values, lists, and trees.  All these things can be encoded as $\lambda$-terms.

\subsection{Encoding Booleans}

The Booleans are the easiest to encode, so let us start with them.  We would like to define the Boolean constants \nm{TRUE} and \nm{FALSE} and the Boolean operators \nm{IF}, \nm{AND}, \nm{OR}, \nm{NOT}, etc.\ so that they behave in the expected way.  There are many reasonable encodings.  One good one is to define \nm{TRUE} and \nm{FALSE} by:
\begin{eqnarray*}
\nm{TRUE} &\definedas& \lam{xy}{x}\\
\nm{FALSE} &\definedas& \lam{xy}{y}.
\end{eqnarray*}

Now we would like to define the conditional test \nm{IF}.  We would like \nm{IF} to take three arguments $b,t,f$, where $b$ is a Boolean value and $t,f$ are arbitrary $\lambda$-terms.  The function should return $t$ if $b=\nm{TRUE}$ and $f$ if $b=\nm{FALSE}$.
\begin{eqnarray*}
\nm{IF} &=& \lam{b\,tf}{\left\{\begin{array}{ll}
t, & \mbox{if $b=\nm{TRUE}$},\\
f, & \mbox{if $b=\nm{FALSE}$}.
\end{array}\right.}
\end{eqnarray*}
Now the reason for defining \nm{TRUE} and \nm{FALSE} the way we did becomes clear.  Since $\nm{TRUE}\;t\;f\rightarrow t$ and $\nm{FALSE}\;t\;f\rightarrow f$, all $\nm{IF}$ has to do is apply its Boolean argument to the other two arguments:
\begin{eqnarray*}
\nm{IF} &\definedas& \lam{b\,tf}{b\;t\,f}
\end{eqnarray*}

The other Boolean operators can be defined from \nm{IF}:
\begin{eqnarray*}
\nm{AND} &\definedas& \lam{b_1\,b_2}{\nm{IF}\;b_1\;b_2\;\nm{FALSE}}\\
\nm{OR} &\definedas& \lam{b_1\,b_2}{\nm{IF}\;b_1\;\nm{TRUE}\;b_2}\\
\nm{NOT} &\definedas& \lam{b_1}{\nm{IF}\;b_1\;\nm{FALSE}\;\nm{TRUE}}
\end{eqnarray*}

Whereas these operators work correctly when given Boolean values as we have defined them, all bets are off if they are applied to any other $\lambda$-term.  There is no guarantee of any kind of reasonable behavior.  Basically, with the untyped $\lambda$-calculus, it is _garbage in, garbage out_.

\subsection{Encoding Integers}

We will encode natural numbers $\mathbb{N}$ using _Church numerals_.  This is the same encoding that Alonzo Church used, although there are other reasonable encodings.  The Church numeral for the number $n\in\mathbb{N}$ is denoted $\overline n$.  It is the $\lambda$-term $\lam{fx}{f^n\;x}$, where $f^n$ denotes the $n$-fold composition of $f$ with itself:
\begin{eqnarray*}
\overline 0 &\definedas& \lam{fx}{f^0\;x}\ \ =\ \ \lam{fx}{x}\\
\overline 1 &\definedas& \lam{fx}{f^1\;x}\ \ =\ \ \lam{fx}{f\;x}\\
\overline 2 &\definedas& \lam{fx}{f^2\;x}\ \ =\ \ \lam{fx}{f(f\;x)}\\
\overline 3 &\definedas& \lam{fx}{f^3\;x}\ \ =\ \ \lam{fx}{f(f(f\;x))}\\
&\vdots\\
\overline n &\definedas& \lam{fx}{f^n\;x}\ \ =\ \ \lam{fx}{\underbrace{f(f(\ldots(f}_n\;x)\ldots)}
\end{eqnarray*}

We can define the successor function $s$ as
\begin{eqnarray*}
\nm{s} &\definedas& \lam{nfx}{f(n\;f\;x)}.
\end{eqnarray*}
That is, $s$ on input $\overline n$ returns a function that takes a function $f$ as input, applies $\overline n$ to it to get the $n$-fold composition of $f$ with itself, then composes that with one more $f$ to get the $(n+1)$-fold composition of $f$ with itself.  Then
\begin{eqnarray*}
s\;\overline n &=& (\lam{nfx}{f(n\;f\;x)})\;\overline n\\
&\rightarrow& \lam{fx}{f(\overline n\;f\;x)}\\
&\rightarrow& \lam{fx}{f(f^n\; x)}\\
&=& \lam{fx}{f^{n+1}\; x}\\
&=& \overline{n+1}.
\end{eqnarray*}

We can perform basic arithmetic with Church numerals.  For addition, we might define
\begin{eqnarray*}
\nm{ADD} &\definedas& \lam{m\,n\,f\,x}{m\;f\;(n\;f\;x)}.
\end{eqnarray*}
On input $\overline m$ and $\overline n$, this function returns
\begin{eqnarray*}
(\lam{m\,n\,f\,x}{m\;f\;(n\;f\;x)})\;\overline m\;\overline n
&\rightarrow& \lam{f\;x}{\overline m\;f\;(\overline n\;f\;x)}\\
&\rightarrow& \lam{f\;x}{f^{m}\;(f^{n}\;x)}\\
&=& \lam{f\;x}{f^{m+n}\;x}\\
&=& \overline{m+n}.
\end{eqnarray*}
Here we are composing $f^{m}$ with $f^{n}$ to get $f^{m+n}$.

Alternatively, recall that Church numerals act on a function to apply that function repeatedly, and addition can be viewed as repeated application of the successor function, so we could define
\begin{eqnarray*}
\nm{ADD} &\definedas& \lam{m\,n}{m\;s\;n}.
\end{eqnarray*}

Similarly, multiplication is just iterated addition, and exponentiation is iterated multiplication:
\begin{eqnarray*}
\nm{MUL}\ \ \definedas\ \ \lam{m\,n}{m\;(ADD\;n)\;\overline 0} &\quad& \nm{EXP}\ \ \definedas\ \ \lam{m\,n}{m\;(MUL\;n)\;\overline 1}.
\end{eqnarray*}

\subsection{Pairing and Projections}

Logic and arithmetic are good places to start, but we still are lacking any useful data structures.  For example, consider ordered pairs.  It would be nice to have a pairing function \nm{PAIR} with projections \nm{FIRST} and \nm{SECOND} that obeyed the following equational specifications:
\begin{eqnarray*} 
\nm{FIRST}~(\nm{PAIR}~e_1~e_2) &=& e_1\\
\nm{SECOND}~(\nm{PAIR}~e_1~e_2) &=& e_2\\
\nm{PAIR}~(\nm{FIRST}~p)~(\nm{SECOND}~p) &=& p,
\end{eqnarray*}
provided $p$ is a pair.  We can take a hint from \nm{IF}.  Recall that \nm{IF} selects one of its two branch options depending on its Boolean argument.  \nm{PAIR} can do something similar, wrapping its two arguments for later extraction by some function $f$:
\begin{eqnarray*} 
\nm{PAIR} &\definedas& \lam{abf}{f\;a\;b}.
\end{eqnarray*}
Thus $\nm{PAIR}~e_1~e_2 \rightarrow \lam{f}{f\;e_1\;e_2}$.
To get $e_1$ back out, we can just apply this to \nm{TRUE}: $(\lam{f}{f\;e_1\;e_2})~\nm{TRUE} \rightarrow \nm{TRUE}~e_1~e_2\rightarrow e_1$, and similarly applying it to \nm{FALSE} extracts $e_2$.  Thus we can define
\begin{eqnarray*}
\nm{FIRST}\ \ \definedas\ \ \lam{p}{p~\nm{TRUE}} &\quad& \nm{SECOND}\ \ \definedas\ \ \lam{p}{p~\nm{FALSE}}.
\end{eqnarray*}
Again, if $p$ isn't a term of the form $\nm{PAIR}~a~b$, expect the unexpected. 

\section{Recursion and the \nm{Y} Combinator}

With an encoding for \nm{IF}, we have some control over the flow of a program.  We can also write simple \texttt{for} loops using the Church numerals $\overline n$.  However, we do not yet have the ability to write an unbounded \texttt{while} loop or a recursive function.

In ML, we can write the factorial function recursively as
\begin{center}
"fun fact(n) = if n $\le$ 1 then 1 else n*fact(n-1)"
\end{center}
But how can we write this in the $\lambda$-calculus, where all the functions are anonymous?  We must somehow construct a $\lambda$-term \nm{FACT} that satisfies the equation
\begin{eqnarray}
\nm{FACT} &=& \lam{n}{\nm{IF}~(n \leq 1)~1~(\nm{MUL}~n~(\nm{FACT}~(\nm{SUB}~n~1)))}\label{eqn:fact}
\end{eqnarray}
Equivalently, we must construct a \emph{fixpoint} of the map $F$ defined by
\begin{eqnarray*}
\nm{F} &\definedas& \lam{f}{\lam{n}{\nm{IF}~(n \leq 1)~1~(\nm{MUL}~n~(f~(\nm{SUB}~n~1)))}};
\end{eqnarray*}
that is, a $\lambda$-term \nm{FACT} such that $F(\nm{FACT})=\nm{FACT}$.  Any solution of (\ref{eqn:fact}) will do; different solutions may disagree on non-integers, but one can show inductively that any solution of (\ref{eqn:fact}) will yield $\overline{n!}$ on input $\overline n$.  Thus it is only a question of existence.

Now consider the $\lambda$-term
\[
(\lam x{F\;(x\;x)})~(\lam x{F\;(x\;x)}).
\]
This is a fixpoint of $F$, since
\begin{eqnarray*}
(\lam x{F\;(x\;x)})~(\lam x{F\;(x\;x)}) &\rightarrow& F~((\lam x{F\;(x\;x)})~(\lam x{F\;(x\;x)})).
\end{eqnarray*}
Moreover, this construction does not depend on the nature of $F$, so we can define
\begin{eqnarray*}
Y &\definedas& \lam f{(\lam x{f\;(x\;x)})~(\lam x{f\;(x\;x)})}.
\end{eqnarray*}
Then for any $f$, we have that $Y\,f$ is a fixpoint of $f$; that is, $Y\,f=f\,(Y\,f)$.

This $Y$ is the infamous \emph{$Y$ combinator}, a closed $\lambda$-term that constructs solutions to recursive equations in a uniform way.

Curiously, although \emph{every} $\lambda$-term is a fixpoint of the identity map $\lam xx$, the $Y$ combinator produces a particularly unfortunate one, namely the divergent $\lambda$-term $\Omega$ introduced in Lecture 2.

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

