content/lambda-calculus/introduction/introduction.tex
1% Part: lambda-calculus2% Chapter: introduction34\documentclass[../../../include/open-logic-chapter]{subfiles}56\begin{document}78\olchapter{lam}{int}{Introduction}910\begin{editorial}11This chapter consists of Jeremy's original concise notes on the lambda12calculus. The sections need to be combined, and the material on lambda13definability merged with the material in the separate, more detailed14chapter on lambda definability.15\end{editorial}1617\olimport{overview}1819\olimport{syntax}2021\olimport{reduction}2223\olimport{church-rosser}2425\olimport{currying}2627\olimport{lambda-definability}2829\olimport{lambda-computable}3031\olimport{computable-lambda}3233\olimport{basic-pr-lambda}3435\olimport{composition}3637\olimport{primitive-recursion}3839\olimport{fixed-point-combinator}4041\olimport{minimization}4243\OLEndChapterHook4445\end{document}
content/lambda-calculus/introduction/overview.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: overview45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{ovr}10\olsection{Overview}1112The lambda calculus was originally designed by Alonzo Church in the13early 1930s as a basis for constructive logic, and \emph{not} as a14model of the computable functions. But it was soon shown to be15equivalent to other definitions of computability, such as the Turing16computable functions and the partial recursive functions. The fact17that this initially came as a small surprise makes the18characterization all the more interesting.1920Lambda notation is a convenient way of referring to a function21directly by a symbolic expression which defines it, instead of22defining a name for it. Instead of saying ``let $f$ be the function23defined by $f(x) = x + 3$,'' one can say, ``let $f$ be the function24$\lambd[x][(x + 3)]$.'' In other words, $\lambd[x][(x+3)]$ is just a25\emph{name} for the function that adds three to its argument. In this26expression, $x$ is a dummy variable, or a placeholder: the same27function can just as well be denoted by $\lambd[y][(y + 3)]$. The28notation works even with other parameters around. For example, suppose29$g(x, y)$ is a function of two variables, and $k$ is a natural30number. Then $\lambd[x][g(x,k)]$ is the function which maps any~$x$31to~$g(x, k)$.3233This way of defining a function from a symbolic expression is known as34\emph{lambda abstraction}. The flip side of lambda abstraction is35\emph{application}: assuming one has a function $f$ (say, defined on36the natural numbers), one can \emph{apply} it to any value, like 2. In37conventional notation, of course, we write~$f(2)$ for the result.3839What happens when you combine lambda abstraction with application?40Then the resulting expression can be simplified, by ``plugging'' the41applicand in for the abstracted variable. For example,42\[43(\lambd[x][(x + 3)])(2)44\]45can be simplified to~$2 + 3$.4647Up to this point, we have done nothing but introduce new notations for48conventional notions. The lambda calculus, however, represents a more49radical departure from the set-theoretic viewpoint. In this framework:50\begin{enumerate}51\item Everything denotes a function.52\item Functions can be defined using lambda abstraction.53\item Anything can be applied to anything else.54\end{enumerate}55For example, if $F$ is a term in the lambda calculus, $F(F)$ is always56assumed to be meaningful. This liberal framework is known as the57\emph{untyped} lambda calculus, where ``untyped'' means ``no58restriction on what can be applied to what.''5960\begin{digress}61There is also a \emph{typed} lambda calculus, which is an important62variation on the untyped version. Although in many ways the typed63lambda calculus is similar to the untyped one, it is much easier to64reconcile with a classical set-theoretic framework, and has some very65different properties.6667Research on the lambda calculus has proved to be central in68theoretical computer science, and in the design of programming69languages. LISP, designed by John McCarthy in the 1950s, is an early70example of a language that was influenced by these ideas.71\end{digress}7273\end{document}
content/lambda-calculus/introduction/syntax.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: syntax45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{syn}10\olsection{The Syntax of the Lambda Calculus}1112One starts with a sequence of variables $x$, $y$, $z$,~\dots and some13constant symbols $a$, $b$, $c$,~\dots. The set of terms is defined14inductively, as follows:15\begin{enumerate}16\item Each variable is a term.17\item Each constant is a term.18\item If $M$ and $N$ are terms, so is $(MN)$.19\item If $M$ is a term and $x$ is a variable, then $(\lambd[x][M])$ is a20 term.21\end{enumerate}22Terms of the form $(MN)$ are called \emph{applications} and those of23the form $(\lambd[x][M])$ \emph{abstractions}.2425The system without any constants at all is called the \emph{pure}26lambda calculus. We'll mainly be working in the pure27$\lambd$-calculus, so all lowercase letters will stand for variables.28We use uppercase letters ($M$, $N$, etc.) to stand for terms of the29$\lambd$-calculus.3031We will follow a few notational conventions:3233\begin{conv}34\begin{enumerate}35\item When parentheses are left out, application takes place from left36 to right. For example, if $M$, $N$, $P$, and $Q$ are terms, then37 $MNPQ$ abbreviates $(((MN)P)Q)$.38\item Again, when parentheses are left out, lambda abstraction is to39 be given the widest scope possible. From example, $\lambd[x][MNP]$ is40 read $(\lambd[x][((MN)P)])$.41\item A lambda can be used to abstract multiple variables. For42 example, $\lambd[xyz][M]$ is short for43 $\lambd[x][\lambd[y][\lambd[z][M]]]$.44\end{enumerate}45\end{conv}4647For example,48\[49\lambd[xy][xxyx \lambd[z][xz]]50\]51abbreviates52\[53\lambd[x][\lambd[y][((((xx)y)x)(\lambd[z][(xz)]))]].54\]55You should memorize these conventions. They will drive you crazy at56first, but you will get used to them, and after a while they will57drive you less crazy than having to deal with a morass of parentheses.5859Two terms that differ only in the names of the bound variables are60called $\alpha$-equivalent; for example, $\lambd[x][x]$ and61$\lambd[y][y]$. It will be convenient to think of these as being the62``same'' term; in other words, when we say that $M$ and $N$ are the63same, we also mean ``up to renamings of the bound variables.''64Variables that are in the scope of a $\lambd$ are called ``bound'',65while others are called ``free.'' There are no free variables in the66previous example; but in67\[68(\lambd[z][yz])x69\]70$y$ and $x$ are free, and $z$ is bound.7172\end{document}
content/lambda-calculus/introduction/reduction.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: reduction45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{red}10\olsection{Reduction of Lambda Terms}1112What can one do with lambda terms? Simplify them. If $M$ and $N$ are13any lambda terms and $x$ is any variable, we can use $\Subst{M}{N}{x}$ to14denote the result of substituting $N$ for~$x$ in~$M$, after renaming15any bound variables of $M$ that would interfere with the free16variables of~$N$ after the substitution. For example,17\[18\Subst{(\lambd[w][xxw])}{yyz}{x} = \lambd[w][(yyz)(yyz)w].19\]2021\begin{digress}22Alternative notations for substitution are $[N/x]M$, $[x/N]M$, and23also $M[x/N]$. Beware!24\end{digress}2526Intuitively, $(\lambd[x][M])N$ and $\Subst{M}{N}{x}$ have the same27meaning; the act of replacing the first term by the second is called28\emph{$\beta$-contraction}. $(\lambd[x][M])N$ is called a \emph{redex}29and $\Subst{M}{N}{x}$ its \emph{contractum}. Generally, if it is30possible to change a term $P$ to~$P'$ by $\beta$-contraction of some31subterm, we say that $P$ \emph{$\beta$-reduces to $P'$ in one step},32and write $P \redone P'$. If from $P$ we can obtain~$P'$ with some33number of one-step reductions (possibly none), then $P$34\emph{$\beta$-reduces} to~$P'$; in symbols, $P \red P'$. A term that35cannot be $\beta$-reduced any further is called36\emph{$\beta$-irreducible}, or \emph{$\beta$-normal}. We will say37``reduces'' instead of ``$\beta$-reduces,'' etc., when the context is38clear.3940Let us consider some examples.41\begin{enumerate}42\item We have43\begin{align*}44(\lambd[x][xxy]) \lambd[z][z] & \redone (\lambd[z][z])(\lambd[z][z]) y \\45& \redone (\lambd[z][z]) y \\46& \redone y.47\end{align*}48\item ``Simplifying'' a term can make it more complex:49\begin{align*}50(\lambd[x][xxy])(\lambd[x][xxy]) & \redone (\lambd[x][xxy])(\lambd[x][xxy])y \\51& \redone (\lambd[x][xxy])(\lambd[x][xxy])yy \\52& \redone \dots53\end{align*}54\item It can also leave a term unchanged:55\[56(\lambd[x][xx])(\lambd[x][xx]) \redone (\lambd[x][xx])(\lambd[x][xx]).57\]58\item Also, some terms can be reduced in more than one way; for59 example,60\[61(\lambd[x][(\lambd[y][yx]) z)] v \redone (\lambd[y][yv]) z62\]63by contracting the outermost application; and64\[65(\lambd[x][(\lambd[y][yx]) z)] v \redone (\lambd[x][zx]) v66\]67by contracting the innermost one. Note, in this case, however, that68both terms further reduce to the same term, $zv$.69\end{enumerate}7071The final outcome in the last example is not a coincidence, but rather72illustrates a deep and important property of the lambda calculus, known as the73``Church--Rosser property.''7475\end{document}
content/lambda-calculus/introduction/church-rosser.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: church-rosser45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{cr}10\olsection{The Church--Rosser Property}1112\begin{thm}13\ollabel{thm:church-rosser}14Let $M$, $N_1$, and $N_2$ be terms, such that $M \red N_1$ and $M \red15N_2$. Then there is a term $P$ such that $N_1 \red P$ and $N_2 \red P$.16\end{thm}1718\begin{cor}19Suppose $M$ can be reduced to normal form. Then this normal form is20unique.21\end{cor}2223\begin{proof}24If $M \red N_1$ and $M \red N_2$, by the previous theorem there is a25term~$P$ such that $N_1$ and $N_2$ both reduce to~$P$. If $N_1$26and~$N_2$ are both in normal form, this can only happen if $N_1 \ident P \ident27N_2$.28\end{proof}2930Finally, we will say that two terms $M$ and~$N$ are31\emph{$\beta$-equivalent}, or just \emph{equivalent}, if they reduce32to a common term; in other words, if there is some $P$ such that $M33\red P$ and $N \red P$. This is written $M \equal[\beta] N$. Using34\olref{thm:church-rosser}, you can check that $\equal[\beta]$ is an35equivalence relation, with the additional property that for every $M$36and~$N$, if $M \red N$ or $N \red M$, then $M \equal[\beta] N$. (In fact, one37can show that $\equal[\beta]$ is the \emph{smallest} equivalence relation38having this property.)3940\end{document}41
content/lambda-calculus/introduction/currying.tex
1% Part: lambda-calculus2% Chapter: representability3% Section: currying45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{rep}{cur} 10\olsection{Currying}1112A $\lambd$-abstract $\lambd[x][M]$ represents a function of one13argument, which is quite a limitation when we want to define function14accepting multiple arguments. One way to do this would be by15extending the $\lambd$-calculus to allow the formation of pairs,16triples, etc., in which case, say, a three-place function17$\lambd[x][M]$ would expect its argument to be a triple. However, it18is more convenient to do this by \emph{Currying}.1920Let's consider an example. We'll pretend for a moment that we have a21$+$ operation in the $\lambd$-calculus. The addition function is22$2$-place, i.e., it takes two arguments. But a $\lambd$-abstract only23gives us functions of one argument: the syntax does not allow24expressions like $\lambd[(x,y)][(x+y)]$. However, we can consider the25one-place function~$f_x(y)$ given by $\lambd[y][(x+y)]$, which adds26$x$ to its single argument~$y$. Actually, this is not a single27function, but a family of different functions ``add $x$,'' one for28each number~$x$. Now we can define another one-place function~$g$ as29$\lambd[x][f_x]$. Applied to argument $x$, $g(x)$ returns the30function~$f_x$---so its values are other functions. Now if we apply31$g$ to $x$, and then the result to~$y$ we get: $(g(x))y = f_x(y) =32x+y$. In this way, the one-place function~$g$ can do the same job as33the two-place addition function. ``Currying'' simply refers to this34trick for turning two-place functions into one place functions (whose35values are one-place functions).3637Here is an example properly in the syntax of the $\lambd$-calculus.38How do we represent the function $f(x,y) = x$? If we want to define a39function that accepts two arguments and returns the first, we can40write $\lambd[x][\lambd[y][x]]$, which literally is a function that41accepts an argument~$x$ and returns the function~$\lambd[y][x]$. The42function $\lambd[y][x]$ accepts another argument~$y$, but drops it,43and always returns~$x$. Let's see what happens when we apply44$\lambd[x][\lambd[y][x]]$ to two arguments:45\begin{align*}46 (\lambd[x][\lambd[y][x]])MN 47 \bredone & (\lambd[y][M])N \\48 \bredone & M49\end{align*}5051In general, to write a function with parameters $x_1$, \dots,~$x_n$52defined by some term~$N$, we can write53$\lambd[x_1][\lambd[x_2][\ldots\lambd[x_n][N]]]$. If we apply $n$ arguments54to it we get:55\begin{multline*}56 (\lambd[x_1][\lambd[x_2][\ldots\lambd[x_n][N]]]) M_1 \dots M_n \bredone\\57 \begin{aligned}58 \bredone {} & (\Subst{(\lambd[x_2][\ldots\lambd[x_n][N]])}{M_1}{x_1}) M_259 \dots M_n\\60 \eqs {} & (\lambd[x_2][\ldots\lambd[x_n][\Subst{N}{M_1}{x_1}]]) M_261 \dots M_n \\62 \vdots & \\63 \bredone {} &\Subst{\Subst{P}{M_1}{x_1}\ldots}{M_n}{x_n}64 \end{aligned}65\end{multline*}66The last line literally means substituting $M_i$ for $x_i$ in the body67of the function definition, which is exactly what we want when68applying multiple arguments to a function.69\end{document}70
content/lambda-calculus/introduction/lambda-definability.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: lambda-definability45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{rep}10\olsection{\usetoken{S}{lambda definable} Arithmetical Functions}1112How can the lambda calculus serve as a model of computation? At first,13it is not even clear how to make sense of this statement. To talk14about computability on the natural numbers, we need to find a suitable15representation for such numbers. Here is one that works surprisingly16well.1718\begin{defn}19For each natural number~$n$, define the \emph{Church numeral}20$\num{n}$ to be the lambda term $\lambd[x][\lambd[y][(x(x(x(\dots21x(y)))))]]$, where there are $n$ $x$'s in all.22\end{defn}2324The terms $\num{n}$ are ``iterators'': on input $f$, $\num{n}$ returns25the function mapping $y$ to $f^n(y)$. Note that each numeral is26normal. We can now say what it means for a lambda term to ``compute''27a function on the natural numbers.2829\begin{defn}30Let $f(x_0, \dots, x_{k-1})$ be an $n$-ary partial function from $\Nat$31to $\Nat$. We say a $\lambd$-term~$F$ \emph{!!{lambda define}s}~$f$ iff for every32sequence of natural numbers $n_0$, \dots,~$n_{k-1}$,33\[34F\, \num{n_0}\, \num{n_1} \dots \num{n_{k-1}} \red \num{f(n_0, n_1, \dots,35 n_{k-1})}36\]37if $f(n_0, \dots, n_{k-1})$ is defined, and $F, \num{n_0}\, \num{n_1}38\dots \num{n_{k-1}}$ has no normal form otherwise.39\end{defn}4041\begin{thm}42\ollabel{thm:lambda-def}43A function $f$ is a partial computable function if and only if it is44!!{lambda defined} by a lambda term.45\end{thm}4647\begin{explain}48This theorem is somewhat striking. As a model of computation, the49lambda calculus is a rather simple calculus; the only operations are50lambda abstraction and application!{} From these meager resources,51however, it is possible to implement any computational procedure.52\end{explain}5354\end{document}
content/lambda-calculus/introduction/lambda-computable.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: lambda-computable45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{cmp}10\olsection{\usetoken{S}{lambda definable} Functions are Computable}1112\begin{thm}13\ollabel{thm:lambda-computable}14If a partial function $f$ is !!{lambda defined} by a lambda term, it is15computable.16\end{thm}1718\begin{proof}19Suppose a function~$f$ is !!{lambda defined} by a lambda term~$X$. Let us20describe an informal procedure to compute~$f$. On input $m_0$,21\dots,~$m_{n-1}$, write down the term $X \num m_0 \ldots \num22m_{n-1}$. Build a tree, first writing down all the one-step reductions23of the original term; below that, write all the one-step reductions of24those (i.e., the two-step reductions of the original term); and keep25going. If you ever reach a numeral, return that as the answer;26otherwise, the function is undefined.2728An appeal to Church's thesis tells us that this function is29computable. A better way to prove the theorem would be to give a30recursive description of this search procedure. For example, one31could define a sequence primitive recursive functions and relations,32``$\fn{IsASubterm}$,'' ``$\fn{Substitute}$,''33``$\fn{ReducesToInOneStep}$,'' ``$\fn{ReductionSequence}$,''34``$\fn{Numeral}$,'' etc. The partial recursive procedure for computing35$f(m_0, \dots, m_{n-1})$ is then to search for a sequence of one-step36reductions starting with $X \num{m_0} \dots \num{m_{n-1}}$ and ending37with a numeral, and return the number corresponding to that numeral.38The details are long and tedious but otherwise routine.39\end{proof}4041\end{document}
content/lambda-calculus/introduction/computable-lambda.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: computable-lambda45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{lrp}10\olsection{Computable Functions are \usetoken{S}{lambda definable}}1112\begin{thm}13\ollabel{thm:computable-lambda}14Every computable partial function is !!{lambda definable}.15\end{thm}1617\begin{proof}18We need to show that every partial computable function~$f$ is19!!{lambda defined} by a lambda term~$F$. By Kleene's normal form20theorem, it suffices to show that every primitive recursive function21is !!{lambda defined} by a lambda term, and then that the functions22!!{lambda definable} are closed under suitable compositions and23unbounded search. To show that every primitive recursive function is24!!{lambda defined} by a lambda term, it suffices to show that the25initial functions are !!{lambda definable}, and that the partial26functions that are !!{lambda definable} are closed under27composition, primitive recursion, and unbounded search.28\end{proof}2930We will use a more conventional notation to make the rest of the proof31more readable. For example, we will write $M(x, y, z)$ instead of32$Mxyz$. While this is suggestive, you should remember that terms in33the untyped lambda calculus do not have associated arities; so, for34the same term~$M$, it makes just as much sense to write $M(x,y)$ and35$M(x,y,z,w)$. But using this notation indicates that we are treating36$M$ as a function of three variables, and helps make the intentions37behind the definitions clearer. In a similar way, we will say ``define38$M$ by $M(x,y,z) = \dots$'' instead of ``define $M$ by $M =39\lambd[x][\lambd[y][\lambd[z][\dots]]]$.''4041\end{document}
content/lambda-calculus/introduction/basic-pr-lambda.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: basic-pr-lambda45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{bas}10\olsection{The Basic Primitive Recursive Functions are \usetoken{S}{lambda definable}}1112\begin{lem}13The functions $\Zero$, $\Succ$, and $\Proj{n}{i}$ are !!{lambda definable}.14\end{lem}1516\begin{proof}17$\Zero$ is just18$\lambd[x][\lambd[y][y]]$.1920The successor function~$\Succ$, is21defined by $\fn{Succ}(u) = \lambd[x][\lambd[y][x(uxy)]]$. You should22think about why this works; for each numeral $\num{n}$, thought of as23an iterator, and each function~$f$, $\fn{Succ}(\num{n},f)$ is a function that,24on input~$y$, applies $f$ $n$ times starting with~$y$, and then25applies it once more.2627There is nothing to say about projections: $\fn{Proj}^n_i(x_0, \dots,28x_{n-1}) = x_i$. In other words, by our conventions, $\fn{Proj}^n_i$ is29the lambda term $\lambd[x_0][\dots \lambd[x_{n-1}][x_i]]$.30\end{proof}3132\end{document}
content/lambda-calculus/introduction/composition.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: composition45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{com}10\olsection{The \usetoken{S}{lambda definable} Functions are Closed under Composition}1112\begin{lem}13The !!{lambda definable} functions are closed under composition.14\end{lem}1516\begin{proof}17Suppose $f$ is defined by composition from $h$, $g_0,$18\dots,~$g_{k-1}$. Assuming $h$, $g_0$, \dots,~$g_{k-1}$ are19!!{lambda defined} by $H$, $G_0$, \dots,~$G_{k-1}$,20respectively, we need to find a term~$F$ that !!{lambda define}s~$f$. But we21can simply define~$F$ by22\[23F(x_0, \dots, x_{l-1}) = H(G_0(x_0, \dots, x_{l-1}),24\dots, G_{k-1}(x_0, \dots, x_{l-1})).25\]26In other words, the language of the lambda calculus is well suited to27represent composition.28\end{proof}2930\end{document}
content/lambda-calculus/introduction/primitive-recursion.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: primitive-recursion45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{pr}10\olsection{\usetoken{S}{lambda definable} Functions are Closed under Primitive Recursion}1112When it comes to primitive recursion, we finally need to do some13work. We will have to proceed in stages. As before, on the assumption14that we already have terms $G$ and $H$ that !!{lambda define} functions15$g$ and~$h$, respectively, we want a term $H$ that !!{lambda define}s the16function~$f$ defined by17\begin{align*}18f(0, \vec z) & = g(\vec z) \\19f(x+1, \vec z) & = h(z, f(x,\vec z), \vec z).20\end{align*}21So, in general, given lambda terms $G'$ and~$H'$, it suffices to find22a term~$F$ such that23\begin{align*}24F(\num{0}, \vec z) & \equiv G(\vec z) \\25F(\overline{n+1}, \vec z) & \equiv H(\num{n}, F(\num{n}, \vec z), \vec z)26\end{align*}27for every natural number~$n$; the fact that $G'$ and~$H'$ !!{lambda define}28$g$ and~$h$ means that whenever we plug in numerals $\num{\vec m}$29for $\vec z$, $F(\num{n+1}, \num{\vec m})$ will normalize to the30right answer.3132But for this, it suffices to find a term~$F$ satisfying33\begin{align*}34F(\num 0) & \equiv G \\35F(\overline {n+1}) & \equiv H(\num{n},F(\num{n}))36\intertext{for every natural number $n$, where}37G & = \lambd[\vec z][G'(\vec z)] \text{ and}\\38H(u,v) & = \lambd[\vec z][H'(u,v(u,\vec z),\vec z)].39\end{align*}40In other words, with lambda trickery, we can avoid having to worry41about the extra parameters $\vec z$---they just get absorbed in the42lambda notation.4344Before we define the term $F$, we need a mechanism for handling45ordered pairs. This is provided by the next lemma.4647\begin{lem}48There is a lambda term $D$ such that for each pair of lambda terms $M$49and~$N$, $D(M,N)(\num{0}) \red M$ and $D(M,N)(\num{1}) \red N$.50\end{lem}5152\begin{proof}53First, define the lambda term $K$ by54\[55K(y) = \lambd[x][y].56\]57In other words, $K$ is the term $\lambd[y][\lambd[x][y]]$. Looking at it58differently, for every $M$, $K(M)$ is a constant function that59returns~$M$ on any input.6061Now define $D(x,y,z)$ by $D(x,y,z) = z (K(y))x$. Then we have62\begin{align*}63D(M,N,\num 0) & \red \num 0 (K(N)) M \red M \text{ and}\\64D(M,N,\num 1) & \red \num 1 (K(N)) M \red K(N) M \red N,65\end{align*}66as required.67\end{proof}6869The idea is that $D(M,N)$ represents the pair $\tuple{M, N}$, and if70$P$ is assumed to represent such a pair, $P(\num 0)$ and $P(\num 1)$71represent the left and right projections, $(P)_0$ and $(P)_1$. We will72use the latter notations.7374\begin{lem}75The !!{lambda definable} functions are closed under primitive recursion.76\end{lem}7778\begin{proof}79We need to show that given any terms, $G$ and $H$, we can find a term80$F$ such that81\begin{align*}82F(\num{0}) & \equiv G \\83F(\num{n+1}) & \equiv H(\num{n}, F(\num{n}))84\end{align*}85for every natural number~$n$. The idea is roughly to compute sequences86of \emph{pairs}87\[88\tuple{\num 0, F(\num 0)}, \tuple{\num 1, F(\num 1)}, \dots,89\]90using numerals as iterators. Notice that the first pair is just91$\tuple{\num 0, G}$. Given a pair $\tuple{\num{n}, F(\num{n})}$, the92next pair, $\tuple{\num{n+1}, F(\num{n+1})}$ is supposed to93be equivalent to $\tuple{\num{n+1}, H(\num{n}, F(\num{n}))}$. We94will design a lambda term~$T$ that makes this one-step transition.9596The details are as follows. Define $T(u)$ by97\[98T(u) = \tuple{S((u)_0), H((u)_0,(u)_1)}.99\]100Now it is easy to verify that for any number~$n$,101\[102T(\tuple{\num{n}, M}) \red \tuple{\num{n+1}, H(\num{n}, M)}.103\]104As suggested above, given $G$ and $H$, define~$F(u)$ by105\[106F(u) = (u(T,\tuple{\num 0, G}))_1.107\]108In other words, on input~$\num{n}$, $F$ iterates $T$ $n$ times on109$\tuple{\num 0, G}$, and then returns the second component. To start110with, we have111\begin{enumerate}112\item $\num{0} (T, \tuple{\num 0, G}) \equiv \tuple{\num{0}, G}$113\item $F(\num{0}) \equiv G$114\end{enumerate}115By induction on~$n$, we can show that for each natural number one has116the following:117\begin{enumerate}118\item $\num{n+1}(T, \tuple{\num{0}, G}) \equiv \tuple{\num{n+1}, F(\num{n+1})}$119\item $F(\num{n+1}) \equiv H(\num{n}, F(\num{n}))$120\end{enumerate}121For the second clause, we have122\begin{align*}123F(\num{n+1}) & \red (\num{n+1}(T, \tuple{\num 0, G}))_1 \\124& \equiv (T(\num{n} (T, \tuple{\num 0, G})))_1 \\125& \equiv (T(\tuple{\num{n}, F(\num{n})}))_1 \\126& \equiv (\tuple{\num{n+1}, H(\num{n}, F(\num{n}))})_1 \\127& \equiv H(\num{n}, F(\num{n})).128\end{align*}129Here we have used the induction hypothesis on the second-to-last130line. For the first clause, we have131\begin{align*}132\num{n+1} (T, \tuple{\num 0, G}) &133\equiv T(\num{n} (T, \tuple{\num 0, G})) \\134& \equiv T( \tuple{\num{n}, F(\num{n})}) \\135& \equiv \tuple{\num{n+1}, H(\num{n}, F(\num{n}))} \\136& \equiv \tuple{\num{n+1}, F(\num{n+1})}.137\end{align*}138Here we have used the second clause in the last line. So we have shown139$F(\num 0) \equiv G$ and, for every $n$, $F(\num {n+1}) \equiv H(\num140n, F(\num{n}))$, which is exactly what we needed.141\end{proof}142143\end{document}144
content/lambda-calculus/introduction/fixed-point-combinator.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: fixed-point-combinator45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{fix}10\olsection{Fixed-Point Combinators}1112Suppose you have a lambda term $g$, and you want another term $k$ with13the property that $k$ is $\beta$-equivalent to $gk$. Define terms14\[15\fn{diag}(x) = xx16\]17and18\[19l(x) = g(\fn{diag}(x))20\]21using our notational conventions; in other words, $l$ is the term22$\lambd[x][g(xx)]$. Let $k$ be the term $ll$. Then we have23\begin{align*}24k & = (\lambd[x][g(xx)])(\lambd[x][g(xx)]) \\25& \red g((\lambd[x][g(xx)])(\lambd[x][g(xx)])) \\26& = gk.27\end{align*}28If one takes29\[30Y = \lambd[g][((\lambd[x][g(xx)])(\lambd[x][g(xx)]))]31\]32then $Yg$ and $g(Yg)$ reduce to a common term; so $Yg \equiv_\beta33g(Yg)$. This is known as ``Curry's combinator.'' If instead one takes34\[35Y = (\lambd[xg][g(xxg)])(\lambd[xg][g(xxg)])36\]37then in fact $Yg$ reduces to $g(Yg)$, which is a stronger statement.38This latter version of $Y$ is known as ``Turing's combinator.''3940\end{document}
content/lambda-calculus/introduction/minimization.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: minimization45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{min}10\olsection{The \usetoken{S}{lambda definable} Functions are Closed under Minimization}1112\begin{lem}13Suppose $f(x,y)$ is !!{lambda definable}. Let $g$ be defined by14\[15g(x) \simeq \umin{y}{f(x,y)}.16\]17Then $g$ is !!{lambda definable}.18\end{lem}1920\begin{proof}21The idea is roughly as follows. Given $x$, we will use the fixed-point22lambda term $Y$ to define a function $h_x(n)$ which searches for a~$y$23starting at~$n$; then $g(x)$ is just $h_x(0)$. The function~$h_x$ can24be expressed as the solution of a fixed-point equation:25\[26h_x(n) \simeq27\begin{cases}28n & \text{if $f(x,n) = 0$} \\29h_x(n+1) & \text{otherwise.}30\end{cases}31\]3233Here are the details. Since $f$ is primitive recursive, it is34!!{lambda defined} by some term $F$. Remember that we also have a lambda35term~$D$, such that $D(M, N, \bar{0}) \red M$ and $D(M, N, \bar{1})36\red N$. Fixing $x$ for the moment, to !!{lambda define} $h_x$ we want to37find a term~$H$ (depending on~$x$) satisfying38\[39H(\num{n}) \equiv D(\num{n}, H(S(\num{n})), F(x, \num{n})).40\]41We can do this using the fixed-point term~$Y$. First, let $U$ be the42term43\[44\lambd[h][\lambd[z][D(z,(h(Sz)),F(x,z))]],45\]46and then let $H$ be the term~$YU$. Notice that the only free variable47in~$H$ is~$x$. Let us show that $H$ satisfies the equation above.4849By the definition of $Y$, we have50\[51H = YU \equiv U(YU) = U(H).52\]53In particular, for each natural number~$n$, we have54\begin{align*}55H(\num{n}) & \equiv U(H, \num{n}) \\56& \red D(\num{n}, H(S(\num{n})), F(x, \num{n})),57\end{align*}58as required. Notice that if you substitute a numeral $\num{m}$ for~$x$59in the last line, the expression reduces to $\num{n}$ if $F(\num{m},60\num{n})$ reduces to $\num{0}$, and it reduces to $H(S(\num{n}))$ if61$F(\num{m}, \num{n})$ reduces to any other numeral.6263To finish off the proof, let $G$ be $\lambd[x][H(\num 0)]$. Then $G$64!!{lambda define}s~$g$; in other words, for every~$m$, $G(\num m)$65reduces to~$\overline {g(m)}$, if $g(m)$ is defined, and has no normal66form otherwise.67\end{proof}6869\end{document}