content/lambda-calculus/syntax/syntax.tex
1% Part: lambda-calculus2% Chapter: syntax34\documentclass[../../../include/open-logic-chapter]{subfiles}56\begin{document}78\olchapter{lam}{syn}{Syntax}910\olimport{terms}1112\olimport{unique-readability}1314\olimport{abbreviated-syntax}1516\olimport{free-variables}1718\olimport{substitution}1920\olimport{alpha}2122\olimport{de-bruijn}2324\olimport{term-revisited}2526\olimport{beta}2728\olimport{eta}2930\OLEndChapterHook3132\end{document}
content/lambda-calculus/syntax/terms.tex
1% Part: lambda-calculus2% Chapter: syntax3% Section: terms45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{syn}{trm}10\olsection{Terms}1112The terms of the lambda calculus are built up inductively from an13infinite supply of variables $\Obj{v_0}$, $\Obj{v_1}$, \dots, the14symbol~``$\lambd$'', and parentheses. We will use $x$, $y$, $z$, \dots{}15to designate variables, and $M$, $N$, $P$, \dots{} to desginate terms.1617\begin{defn}[Terms] \ollabel{defn:term}18The set of \emph{terms} of the lambda calculus is defined inductively by:19\begin{enumerate}20 \item \ollabel{defn:term-var} If $x$ is a variable, then $x$ is a21 term.22 \item \ollabel{defn:term-abs} If $x$ is a variable and $M$ is a23 term, then $(\lambd[x][M])$ is a term.24 \item \ollabel{defn:term-app} If both $M$ and $N$ are terms, then25 $(MN)$ is a term.26\end{enumerate}27\end{defn}2829If a term $(\lambd[x][M])$ is formed according to30\olref{defn:term-abs} we say it is the result of an31\emph{abstraction}, and the $x$ in $\lambd[x]$ is called a32\emph{!!{parameter}}. A term $(MN)$ formed according to33\olref{defn:term-app} is the result of an \emph{application}.3435The terms defined above are fully parenthesized. This can get rather36cumbersome, as the term37$(\lambd[x][((\lambd[x][x])(\lambd[x][(xx)]))])$ demnostrates. We will38introduce conventions for avoiding parentheses. However, the official39definition makes it easy to determine how a term is constructed40according to \olref{defn:term}. For example, the last step of forming41the term $(\lambd[x][((\lambd[x][x])(\lambd[x][(xx)]))])$ must be42abstraction where the !!{parameter} is~$x$. It results by abstraction43from the term $((\lambd[x][x])(\lambd[x][(xx)]))$, which is an44application of two terms. Each of these two terms is the result of an45abstraction, and so on.4647\begin{prob}48Describe the formation of $(\lambd[g][(\lambd[x][(g (x x))])49 (\lambd[x][(g (x x))])])$.50\end{prob}5152\end{document}
content/lambda-calculus/syntax/unique-readability.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: unique-readability45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{syn}{unq}10\olsection{Unique Readability}1112We may wonder if for each term there is a unique way of forming it,13and there is. For each lambda term there is only one way to construct14and interpret it. In the following discussion, a \emph{formation} is15the procedure of constructing a term using the formation rules (one or16several times) of \olref[trm]{defn:term}.1718\begin{lem}\ollabel{lem:term-start}19A term starts with either a variable or a parenthesis.20\end{lem}2122\begin{proof}23Something counts as a term only if it is constructed according to24\olref[trm]{defn:term}. If it is the result of25\olref[trm]{defn:term-var}, it must be a variable. If it is the result26of \olref[trm]{defn:term-abs} or \olref[trm]{defn:term-app}, it starts27with a parenthesis.28\end{proof}2930\begin{lem}\ollabel{lem:app-start}31The result of an application starts with either two parentheses or a32parenthesis and a variable.33\end{lem}3435\begin{proof}36If $M$ is the result of an application, it is of the form $(PQ)$, so37it begins with a parenthesis. Since $P$ is a term, by38\olref{lem:term-start}, it begins either with a parenthesis or a39variable.40\end{proof}4142\begin{lem}\ollabel{lem:initial}43No proper initial part of a term is itself a term.44\end{lem}4546\begin{prob}47Prove \olref[lam][syn][unq]{lem:initial} by induction on the length48of terms.49\end{prob}5051\begin{prop}[Unique Readability] \ollabel{prop:unq}52There is a unique formation for each term. In other words, if a term53$M$ is formed by a formation, then it is the only formation that can54form this term.55\end{prop}5657\begin{proof}58 We prove this by induction on the formation of terms. 59 \begin{enumerate}60 \item $M$ is of the form61 $x$, where $x$ is some variable. Since the results of abstractions and applications always start with parentheses, they cannot have been used to62 construct $M$; Thus, the formation of $M$ must be a single step63 of \olref[trm]{defn:term}\olref[trm]{defn:term-var}.64 \item $M$ is of the form $(\lambd[x][N])$, where $x$ is some65 variable and $N$ is a term. It could not have been constructed66 according to \olref[trm]{defn:term}\olref[trm]{defn:term-var},67 because it is not a single variable. It is not the result of an68 application, by \olref{lem:app-start}. Thus $M$ can only be the69 result of an abstraction on~$N$. By inductive hypothesis we70 know that formation of $N$ is itself unique.71 \item $M$ is of the form $(PQ)$, where $P$ and $Q$ are terms.72 Since it starts with a parentheses, it cannot also be73 constructed by74 \olref[trm]{defn:term}\olref[trm]{defn:term-var}. By75 \olref{lem:term-start}, $P$ cannot begin with $\lambd$, so76 $(PQ)$ cannot be the result of an abstraction. Now suppose77 there were another way of constructing $M$ by application, e.g.,78 it is also of the form $(P'Q')$. Then $P$ is a proper initial79 segment of $P'$ (or vice versa), and this is impossible by80 \olref{lem:initial}. So $P$ and $Q$ are uniquely81 determined, and by inductive hypothesis we know that formations82 of $P$ and $Q$ is unique.83 \end{enumerate}84\end{proof}8586A more readable paraphrase of the above proposition is as follows:87\begin{prop}88 A term $M$ can only be one of the following forms:89 \begin{enumerate}90 \item $x$, where $x$ is a variable uniquely determined by $M$.91 \item $(\lambd[x][N])$, where $x$ is a variable and $N$ is92 another term, both of which is uniquely determined by $M$.93 \item $(PQ)$, where $P$ and $Q$ are two terms uniquely94 determined by $M$.95 \end{enumerate}96\end{prop}9798\end{document}
content/lambda-calculus/syntax/abbreviated-syntax.tex
1% Part: lambda-calculus2% Chapter: syntax3% Section: abbreviated-syntax45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{syn}{abb}10\olsection{Abbreviated Syntax}1112Terms as defined in \olref[trm]{defn:term} are sometimes cumbersome to13write, so it is useful to introduce a more concise syntax. We must of14course be careful to make sure that the terms in the concise notation15also are uniquely readable. One widely used version called16\emph{abbreviated terms} is as follows.1718\begin{enumerate}19\item When parentheses are left out, application takes place from left20 to right. For example, if $M$, $N$, $P$, and~$Q$ are terms, then21 $MNPQ$ abbreviates $(((MN)P)Q)$.22\item Again, when parentheses are left out, lambda abstraction is23 given the widest scope possible. From example, $\lambd[x][MNP]$ is24 read as $(\lambd[x][MNP])$.25\item A lambda can be used to abstract multiple variables. For26 example, $\lambd[xyz][M]$ is short for27 $\lambd[x][\lambd[y][\lambd[z][M]]]$.28\end{enumerate}2930For example,31\[32\lambd[xy][xxyx \lambd[z][xz]]33\]34abbreviates35\[36(\lambd[x][(\lambd[y][((((xx)y)x)(\lambd[z][(xz)]))])]).37\]3839\begin{prob}40Expand the abbreviated term $\lambd[g][(\lambd[x][g (x x)])41 \lambd[x][g (x x)]]$.42\end{prob}4344\end{document}
content/lambda-calculus/syntax/free-variables.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: free-variables45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{syn}{fv}10\olsection{Free Variables}1112Lambda calculus is about functions, and lambda abstraction is how13functions arise. Intuitively, $\lambd[x][M]$ is the function with14values given by~$M$ when the argument to the function is assigned15to~$x$. But not every occurrence of~$x$ in~$M$ is relevant: if $M$16contains another abstract $\lambd[x][N]$ then the occurrences of $x$17in~$N$ are relevant to $\lambd[x][N]$ but not to $\lambd[x][M]$. So,18a lambda abstract $\lambd[x]$ inside $\lambd[x][M]$ \emph{binds} those19occurrences of $x$ in~$M$ that are not already bound by another lambda20abstract---the \emph{free} occurrences of~$x$ in~$M$.2122\begin{defn}[Scope]23If $\lambd[x][M]$ occurs inside a term~$N$, then the corresponding24occurrence of~$N$ is the \emph{scope} of the~$\lambd[x]$.25\end{defn}262728\begin{defn}[Free and bound occurrence]29 An occurrence of variable $x$ in a term~$M$ is \emph{free} if it is30 not in the scope of a $\lambd[x]$, and \emph{bound} otherwise. An31 occurrence of a variable~$x$ in $\lambd[x][M]$ is bound by the initial32 $\lambd[x]$ iff the occurrence of $x$ in~$M$ is free.33\end{defn}3435\begin{ex}36 In $\lambd[x][x y]$, both $x$ and $y$ are in the scope of $\lambd[x]$,37 so $x$ is bound by $\lambd[x]$. Since $y$ is not in the scope of38 any $\lambd[y]$, it is free. In $\lambd[x][x x]$, both occurrences39 of $x$ are bound by~$\lambd[x]$, since both are free in $xx$. In40 $((\lambd[x][xx])x)$, the last occurrence of~$x$ is free, since it41 is not in the scope of a $\lambd[x]$. In42 $\lambd[x][(\lambd[x][x])x]$, the scope of the first $\lambd[x]$ is43 $(\lambd[x][x])x$ and the scope of the second $\lambd[x]$ is the44 second-to-last occurrence of~$x$. In $(\lambd[x][x])x$, the last45 occurrence of $x$ is free, and the second-to-last is bound. Thus,46 the second-to-last occurrence of $x$ in $\lambd[x][(\lambd[x][x])x]$47 is bound by the second $\lambd[x]$, and the last occurrence by the48 first $\lambd[x]$.49\end{ex}5051For a term $P$, we can check all variable occurrences in it and get a52set of free variables. This set is denoted by $\FV{P}$ with a natural53definition as follows:5455\begin{defn}[Free variables of a term] \ollabel{def:fv}56 The set of \emph{free variables} of a term is defined inductively by:57 \begin{enumerate}58 \item $\FV{x} = \{x\}$ \ollabel{def:fv1} 59 \item $\FV{\lambd[x][N]} = \FV{N} \setminus \{x\}$ \ollabel{def:fv2}60 \item $\FV{PQ} = \FV{P} \cup \FV{Q}$ \ollabel{def:fv3}61 \end{enumerate}62\end{defn}6364\begin{prob}65 \begin{enumerate}66 \item Identify the scopes of $\lambd[g]$ and the two $\lambd[x]$ in67 this term: $\lambd[g][(\lambd[x][g (x x)]) \lambd[x][g (x x)]]$.68 \item In $\lambd[g][(\lambd[x][g (x x)]) \lambd[x][g (x x)]]$, are69 all occurrences of variables bound? By which abstractions are they70 bound respectively?71 \item Give $\FV{\lambd[x][(\lambd[y][(\lambd[z][x y]) z]) y]}$72 \end{enumerate}73\end{prob}7475\begin{explain}76A free variable is like a reference to the outside world (the77\emph{environment}), and a term containing free variables can be seen78as a partially specified term, since its behaviour depends on how we79set up the environment. For example, in the term $\lambd[x][f x]$, which80accepts an argument~$x$ and returns $f$ of that argument, the81variable~$f$ is free. This value of the term is dependent on the82environment it is in, in particular the value of $f$ in that83environment.8485If we apply abstraction to this term, we get $\lambd[f][\lambd[x][f86 x]]$. This term is no longer dependent on the environment variable87$f$, because it now designates a function that accepts two arguments88and returns the result of applying the first to the second. Changing89$f$ in the environment won't have any effect on the behavior of this90term, as the term will only use whatever is passed as an argument, and91not the value of $f$ in the environment.92\end{explain}9394\begin{defn}[Closed term, combinator]95 A term with no free variables is called a \emph{closed term}, or a96 \emph{combinator}.97\end{defn}9899\begin{lem}\ollabel{lem:fv}100 \begin{enumerate}101 \item \ollabel{lem:fv-abs} If $y \neq x$, then $y \in102 \FV{\lambd[x][N]}$ iff $y \in \FV{N}$.103 \item \ollabel{lem:fv-app} $y \in \FV{PQ}$ iff $y \in \FV{P}$ or $y104 \in \FV{Q}$.105 \end{enumerate}106\end{lem}107108\begin{proof}109 Exercise.110\end{proof}111112\begin{prob}113 Prove \olref[lam][syn][fv]{lem:fv}.114\end{prob}115116\end{document}
content/lambda-calculus/syntax/substitution.tex
1% Part: lambda-calculus2% Chapter: syntax3% Section: substitution45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{syn}{sub}10\olsection{Substitution}1112\begin{explain}13Free variables are references to environment variables, thus it makes14sense to actually use a specific value in the place of a free15variable. For example, we may want to replace $f$ in $\lambd[x][f x]$16with a specific term, like the identity function $\lambd[y][y]$. This17results in $\lambd[x][(\lambd[y][y]) x]$. The process of replacing18free variables with lambda terms is called substitution.19\end{explain}2021\begin{defn}[Substitution] \ollabel{defn:substitution}22 The \emph{substitution} of a term $N$ for a variable $x$ in a term23 $M$, $\Subst{M}{N}{x}$, is defined inductively by:24 \begin{enumerate}25 \item $\Subst{x}{N}{x} = N$. \ollabel{defn:substitution-1} 26 \item $\Subst{y}{N}{x} = y$ if $x \neq y$. \ollabel{defn:substitution-2} 27 \item $\Subst{PQ}{N}{x} = (\Subst{P}{N}{x}) (\Subst{Q}{N}{x})$.28 \ollabel{def:substitution-3}29 \item $\Subst{(\lambd[y][P])}{N}{x} = \lambd[y][\Subst{P}{N}{x}]$,30 if $x \neq y$ and $y \notin \FV{N}$, otherwise undefined.31 \ollabel{defn:substitution-4}32 \end{enumerate}33\end{defn}3435\begin{explain}36In \olref{defn:substitution}\olref{defn:substitution-4}, we require $x37\neq y$ because we don't want to replace \emph{bound} occurrences of38the variable~$x$ in~$M$ by~$N$. For example, if we compute the39substitution $\Subst{\lambd[x][x]}{y}{x}$, the result should not be40$\lambd[x][y]$ but simply $\lambd[x][x]$.4142When substituting $N$ for~$x$ in $\lambd[y][P]$, we also require that43$y \notin \FV{N}$. For example, we cannot substitute $y$ for $x$ in44$\lambd[y][x]$, i.e., $\Subst{\lambd[y][x]}{y}{x}$, because it would45result in $\lambd[y][y]$, a term that stands for the function that46accepts an argument and returns it directly. But the term47$\lambd[y][x]$ stands for a function that always returns the term~$x$48(or whatever $x$ refers to). So the result we actually want is a49function that accepts an argument, drop it, and returns the50environment variable~$y$. To do this properly, we would first have to51``rename'' the bound variable~$y$.52\end{explain}5354\begin{prob}55 What is the result of the following substitutions?56 \begin{enumerate}57 \item $\Subst{\lambd[y][x(\lambd[w][vwx])]}{(uv)}{x}$58 \item $\Subst{\lambd[y][x(\lambd[x][x])]}{(\lambd[y][xy])}{x}$59 \item $\Subst{y(\lambd[v][xv])}{(\lambd[y][vy])}{x}$60 \end{enumerate}61\end{prob}6263\begin{thm} \ollabel{thm:notinfv}64 If $x \notin \FV{M}$, then $\FV{\Subst{M}{N}{x}} = \FV{M}$, if65 the left-hand side is defined.66\end{thm}6768\begin{proof}69 By induction on the formation of $M$.70 \begin{enumerate}71 \item $M$ is a variable: exercise.72 \item $M$ is of the form $(PQ)$: exercise.73 \item $M$ is of the form $\lambd[y][P]$, and since74 $\Subst{\lambd[y][P]}{N}{x}$ is defined, it has to be75 $\lambd[y][\Subst{P}{N}{x}]$. Then $\Subst{P}{N}{x}$ has to be76 defined; also, $x \neq y$ and $x \notin \FV{Q}$. Then:77 \begin{multline*}78 \FV{\Subst{\lambd[y][P]}{N}{x}} = \\79 \begin{aligned}80 & = \FV{\lambd[y][\Subst{P}{N}{x}]} &&81 \text{by \olref{defn:substitution-4}}\\82 & = \FV{\Subst{P}{N}{x}} \setminus \{y\} &&83 \text{by \olref[fv]{def:fv}\olref[fv]{def:fv2}}\\84 & = \FV{P} \setminus \{y\} && \text{by inductive hypothesis} \\85 & = \FV{\lambd[y][P]} && \text{by \olref[fv]{def:fv}\olref[fv]{def:fv2}}86 \end{aligned}87 \end{multline*}88 \end{enumerate}89\end{proof}9091\begin{prob}92 Complete the proof of \olref[lam][syn][sub]{thm:notinfv}.93\end{prob}9495\begin{thm} \ollabel{thm:infv}96 If $x \in \FV{M})$, then $\FV{\Subst{M}{N}{x}} = (\FV{M} \setminus97 \{x\}) \cup \FV{N}$, provided the left hand is defined.98\end{thm}99100\begin{proof}101 By induction on the formation of $M$.102 \begin{enumerate}103 \item $M$ is a variable: exercise.104 \item $M$ is of the form $PQ$: Since105 $\Subst{(PQ)}{N}{y}$ is defined, it has to be106 $(\Subst{P}{N}{x})(\Subst{Q}{N}{x})$ with both substitution107 defined. Also, since $x \in \FV{PQ}$, either $x \in \FV{P}$ or108 $x \in \FV{Q}$ or both. The rest is left as an exercise.109 \item $M$ is of the form $\lambd[y][P]$. Since110 $\Subst{\lambd[y][P]}{N}{x}$ is defined, it has to be111 $\lambd[y][\Subst{P}{N}{x}]$, with $\Subst{P}{N}{x}$112 defined, $x \neq y$ and $y \notin \FV{N}$; also, since $y \in113 \FV{\lambd[x][P]}$, we have $y \in \FV{P}$ too. Now:114 \begin{multline*}115 \FV{\Subst{(\lambd[y][P])}{N}{x}} = \\116 \begin{aligned}117 & = \FV{\lambd[y][\Subst{P}{N}{x}]} \\118 & = \FV{\Subst{P}{N}{x}} \setminus \{y\} \\119 & = ((\FV{P} \setminus \{y\}) \cup (\FV{N} \setminus \{x\})120 && \text{by inductive hypothesis} \\121 & = (\FV{P} \setminus \{x, y\}) \cup \FV{N}122 && x \notin \FV{N} \\123 & = (\FV{\lambd[y][P]} \setminus \{x\}) \cup \FV{N}124 \end{aligned}125 \end{multline*}126 \end{enumerate}127\end{proof}128129\begin{prob}130 Complete the proof of \olref[lam][syn][sub]{thm:infv}.131\end{prob}132133\begin{thm}\ollabel{thm:clr}134 $x \notin \FV{\Subst{M}{N}{x}}$, if the right-hand side is135 defined and $x \notin \FV{N}$.136\end{thm}137138\begin{proof}139 Exercise.140\end{proof}141142\begin{prob}143 Prove \olref[lam][syn][sub]{thm:clr}.144\end{prob}145146147\begin{thm}\ollabel{thm:inv}148 If $\Subst{M}{y}{x}$ is defined and $y \notin \FV{M}$, then149 $\Subst{\Subst{M}{y}{x}}{x}{y} = M$.150\end{thm}151152\begin{proof}153 By induction on the formation of $M$.154 \begin{enumerate}155 \item $M$ is a variable $z$: Exercise.156 \item $M$ is of the form $(PQ)$. Then:157 \begin{align*}158 \Subst{\Subst{(PQ)}{y}{x}}{x}{y}159 &=\Subst{((\Subst{P}{y}{x})(\Subst{Q}{y}{x}))}{x}{y} \\160 &= (\Subst{\Subst{P}{y}{x}}{x}{y})(\Subst{\Subst{Q}{y}{x}}{x}{y}) \\161 &= (PQ) \text{ by inductive hypothesis}162 \end{align*}163 \item $M$ is of the form $\lambd[z][N]$. Because164 $\Subst{\lambd[z][N]}{y}{x}$ is defined, we know165 that $z \neq y$. So:166 \begin{align*}167 \Subst{\Subst{(\lambd[z][N])}{y}{x}}{x}{y}\\168 & = \Subst{(\lambd[z][\Subst{N}{y}{x}])}{x}{y} \\169 & = \lambd[z][\Subst{\Subst{N}{y}{x}}{x}{y}] \\170 &= \lambd[z][N] \text{ by inductive hypothesis} 171 \end{align*}172 \end{enumerate}173\end{proof}174175\begin{prob}176 Complete the proof of \olref[lam][syn][sub]{thm:inv}.177\end{prob}178179\end{document}
content/lambda-calculus/syntax/alpha.tex
1% Part: lambda-calculus2% Chapter: syntax3% Section: alpha45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{syn}{alp}10\olsection{$\alpha$-Conversion}1112What is the relation between $\lambd[x][x]$ and $\lambd[y][y]$? They13both represent the identity function. They are, of course,14syntactically different terms. They differ only in the name of the15bound variable, and one is the result of ``renaming'' the bound16variable in the other. This is called \emph{$\alpha$-conversion}.1718\begin{defn}[Change of bound variable, $\aconvone$]19 If a term $M$ contains an occurrence of $\lambd[x][N]$, $y \notin20 \FV{N}$, and $\Subst{N}{y}{x}$ is defined, then replacing this occurrence21 by 22 \begin{equation*}23 \lambd[y][\Subst{N}{y}{x}]24 \end{equation*}25 resulting in $M'$ is called a \emph{change of bound variable}, written26 as $M \redone[\alpha] M'$.27\end{defn}2829\begin{defn}[Compatibility of relation]30 A relation $R$ on terms is said to be \emph{compatible}31 if it satisfies following conditions:32 \begin{enumerate}33 \item If $R N N'$ then $R \lambd[x][N] \lambd[x][N']$34 \item If $R P P'$ then $R (PQ) (P'Q)$35 \item If $R Q Q'$ then $R (PQ) (PQ')$36 \end{enumerate}37\end{defn}3839Thus let's rephrase the definition:40\begin{defn}[Change of bound variable, $\aconvone$]41 \emph{Change of bound variable} ($\redone[\alpha]$) is 42 the smallest compatible relation on terms satisfying following43 condition:44 \begin{align*}45 &\lambd[x][N] \redone[\alpha] \lambd[y][\Subst{N}{y}{x}] && \text{if46 $x \neq y$, $y \notin \FV{N}$} \\47 & &&\text{and $\Subst{N}{y}{x}$ is defined}48 \end{align*}49\end{defn}5051``Smallest'' here means the relation contains only pairs that are required by52compatibility and the additional condition, and nothing else. Thus53this relation can also be defined as follows:5455\begin{defn}[Change of bound variable, $\aconvone$] \ollabel{defn:aconvone}56 \emph{Change of bound variable} ($\aconvone$) is inductively57 defined as follows:58 \begin{enumerate}59 \item If $N \aconvone N'$ then $\lambd[x][N] \aconvone60 \lambd[x][N']$ \ollabel{defn:aconvone1}61 \item If $P \aconvone P'$ then $(PQ) \aconvone (P'Q)$ \ollabel{defn:aconvone2}62 \item If $Q \aconvone Q'$ then $(PQ) \aconvone (PQ')$ \ollabel{defn:aconvone3}63 \item If $x \neq y$, $y \notin \FV{N}$ and $\Subst{N}{y}{x}$ is defined, then64 $\lambd[x][N] \redone[\alpha] \lambd[y][\Subst{N}{y}{x}]$.65 \ollabel{defn:aconvone4}66 \end{enumerate}67\end{defn}6869The definitions are equivalent, but we leave the proof as an70exercise. From now on we will use the inductive definition.7172\begin{defn}[$\alpha$-conversion, $\aconv$]73 \emph{$\alpha$-conversion} ($\aconv$) is the smallest reflexitive74 and transitive relation on terms containing $\aconvone$.75\end{defn}7677As above, ``smallest'' means the relation only contains pairs required78by transitivity, and $\aconvone$, which leads to the following79equivalent definition:8081\begin{defn}[$\alpha$-conversion, $\aconv$] \ollabel{defn:aconv}82 \emph{$\alpha$-conversion} ($\aconv$) is inductively defined as follows:83 \begin{enumerate}84 \item If $P \aconv Q$ and $Q \aconv R$, then $P \aconv R$.85 \ollabel{defn:aconv1}86 \item If $P \aconvone Q$, then $P \aconv Q$. \ollabel{defn:aconv2}87 \item $P \aconv P$. \ollabel{defn:aconv3}88 \end{enumerate}89\end{defn}9091\begin{ex}92 $\lambd[x][f x]$ $\alpha$-converts to $\lambd[y][f93 y]$, and conversely. Informally94 speaking, they are both functions that accept an argument and95 return $f$ of that argument, refering to the environment variable~$f$.9697 $\lambd[x][f x]$ does not $\alpha$-convert to $\lambd[x][g98 x]$. Informally speaking, they refer to the environment variables99 $f$ and~$g$ respectively, and this makes them different functions: they100 behave differently in environments where $f$ and $g$ are different.101\end{ex}102103\begin{prob}104 Are the following pairs of terms $\alpha$-convertible?105 \begin{enumerate}106 \item $\lambd[x][\lambd[y][x]]$ and $\lambd[y][\lambd[x][y]]$107 \item $\lambd[x][\lambd[y][x]]$ and $\lambd[c][\lambd[b][a]]$108 \item $\lambd[x][\lambd[y][x]]$ and $\lambd[c][\lambd[b][a]]$109 \end{enumerate}110\end{prob}111112\begin{lem}\ollabel{lem:fv-one}113 If $P \aconvone Q$ then $\FV{P} = \FV{Q}$.114\end{lem}115116\begin{proof}117 By induction on the !!{derivation} of $P \aconvone Q$.118 \begin{enumerate}119 \item If the last rule is \olref{defn:aconvone4}, then $P$ is of the120 form $\lambd[x][N]$ and $Q$ of the form121 $\lambd[y][\Subst{N}{y}{x}]$, with $x \neq y$, $y \notin \FV{N}$122 and $\Subst{N}{y}{x}$ defined. We distinguish cases according to123 whether $x \in \FV{N}$:124 \begin{enumerate}125 \item If $x \in FV(N)$, then:126 \begin{align*}127 \FV{\lambd[y][\Subst{N}{y}{x}]} &=128 \FV{\Subst{N}{y}{x}} \setminus \{y\} \\129 & = ((\FV{N} \setminus \{x\}) \cup \{y\}) \setminus \{y\}130 && \text{ by \olref[sub]{thm:infv}} \\131 & = \FV{N} \setminus \{x\} \\132 & = \FV{\lambd[x][N]}133 \end{align*}134 \item If $x \notin FV(N)$, then:135 \begin{align*}136 \FV{\lambd[y][\Subst{N}{y}{x}]}137 & = FV{\Subst{N}{y}{x}} \setminus \{y\} \\138 & = \FV{N} \setminus \{x\}139 && \text{by \olref[sub]{thm:notinfv}} \\140 & = \FV{\lambd[x][N]}.141 \end{align*}142 \end{enumerate}143 \item The other three cases are left as exercises. 144 \end{enumerate}145\end{proof}146147\begin{prob}148 Complete the proof of \olref[lam][syn][alp]{lem:fv-one}.149\end{prob}150151\begin{lem}\ollabel{lem:inv}152 If $P \aconvone Q$ then $Q \aconvone P$.153\end{lem}154155\begin{proof}156 Induction on the !!{derivation} of $P \aconvone Q$.157 \begin{enumerate}158 \item If the last rule is \olref{defn:aconvone4}, then $P$ is of the159 form $\lambd[x][N]$ and $Q$ of the form160 $\lambd[y][\Subst{N}{y}{x}]$, where $x \neq y$, $y \notin \FV{N}$161 and $\Subst{N}{y}{x}$ defined. First, we have $y \notin162 \FV{\Subst{N}{y}{x}}$ by \olref[sub]{thm:clr}. By163 \olref[sub]{thm:inv} we have that $\Subst{\Subst{N}{y}{x}}{x}{y}$ is164 not only defined, but also equal to~$N$. Then by165 \olref{defn:aconvone4}, we have $\lambd[y][\Subst{N}{y}{x}]166 \aconvone \lambd[x][\Subst{\Subst{N}{y}{x}}{x}{y}] =167 \lambd[x][N]$.168 \end{enumerate}169\end{proof}170171\begin{prob}172 Complete the proof of \olref[lam][syn][alp]{lem:inv}173\end{prob}174175\begin{thm}176 $\alpha$-Conversion is an equivalence relation on terms, i.e., it is177 reflexive, symmetric, and transitive.178\end{thm}179180\begin{proof}181 \begin{enumerate}182 \item For each term $M$, $M$ can be changed to $M$ by183 \emph{zero} changes of bound variables.184 \item If $P$ is $\alpha$-converts to $Q$ by a series of changes185 of bound variables, then from $Q$ we can just inverse these186 changes (by \olref{lem:inv}) in187 opposite order to obtain~$P$.188 \item If $P$ $\alpha$-converts to $Q$ by a series of changes of189 bound variables, and $Q$ to $R$ by another series, then we can190 change $P$ to $R$ by first applying the first series and then the191 second series.192 \end{enumerate}193\end{proof}194195From now on we say that $M$ and $N$ are \emph{$\alpha$-equivalent}, $M196\aeq N$, iff $M$ $\alpha$-converts to $N$ (which, as we've just shown,197is the case iff $N$ $\alpha$-converts to~$M$).198199\begin{thm}\ollabel{thm:fv}200 If $M \aeq N$, then $\FV{M} = \FV{N}$.201\end{thm}202203\begin{proof}204 Immediate from \olref{lem:fv-one}.205\end{proof}206207\begin{lem}\ollabel{lem:sub:R}208 If $R \aeq R'$ and $\Subst{M}{R}{y}$ is defined, then $\Subst{M}{R'}{y}$ is209 defined and $\alpha$-equivalent to $\Subst{M}{R}{y}$.210\end{lem}211212\begin{proof}213 Exercise.214\end{proof}215216\begin{prob}217 Prove \olref[lam][syn][alp]{lem:sub:R}.218\end{prob}219220Recall that in \olref[sub]{sec}, substitution is undefined in some221cases; however, using $\alpha$-conversion on terms, we can make222substitution always defined by renaming bound variables. The result223preserves $\alpha$-equivalence, as shown in this theorem:224225\begin{thm}\ollabel{thm:sub}226 For any $M$, $R$, and~$y$, there exists $M'$ such that $M \aeq M'$227 and $\Subst{M'}{R}{y}$ is defined. Moreover, if there is another228 pair $M'' \aeq M$ and $R''$ where $\Subst{M''}{R''}{y}$ is defined229 and $R'' \aeq R$, then $\Subst{M'}{R}{y} \aeq \Subst{M''}{R''}{y}$.230\end{thm}231232\begin{proof}233 By induction on the formation of $M$:234 \begin{enumerate}235 \item $M$ is a variable~$z$: Exercise.236 \item Suppose $M$ is of the form $\lambd[x][N]$. Select a variable237 $z$ other than $x$ and $y$ and such that $z \notin \FV{N}$ and $z238 \notin \FV{R}$. By inductive hypothesis, we there is $N'$ such239 that $N' \aeq N$ and $\Subst{N'}{z}{x}$ is defined. Then240 $\lambd[x][N] \aeq \lambd[x][N']$ too, by241 \olref{defn:aconvone}\olref{defn:aconvone1}. Now $\lambd[x][N']242 \aeq \lambd[z][\Subst{N'}{z}{x}]$ by243 \olref{defn:aconvone}\olref{defn:aconvone4}. We can do this244 because $z \ne x$, $z \notin FV(N')$ and $\Subst{N'}{z}{x}$ is245 defined. Finally, $\Subst{\lambd[z][\Subst{N'}{z}{x}]}{R}{y}$ is246 defined, because $z \neq y$ and $z \notin FV(R)$.247248 Moreover, if there is another $N''$ and $R''$ satisfying the same249 conditions,250 \begin{multline*}251 \Subst{(\lambd[z][\Subst{N''}{z}{x}])}{R''}{y} =\\252 \begin{aligned}253 &= \lambd[z][\Subst{\Subst{N''}{z}{x}}{R''}{y}] \\254 &= \lambd[z][\Subst{\Subst{N''}{z}{x}}{R}{y}] && \text{by255 \olref{lem:sub:R}}\\256 &=\lambd[z][\Subst{\Subst{N'}{z}{x}}{R}{y}]257 && \text{by inductive258 hypothesis}\\259 &=\Subst{(\lambd[z][\Subst{N'}{z}{x}])}{R}{y}260 \end{aligned}261 \end{multline*}262 \item $M$ is of the form $(PQ)$: Exercise.263 \end{enumerate}264\end{proof}265266\begin{prob}267 Complete the proof of \olref[lam][syn][alp]{thm:sub}.268\end{prob}269270\begin{cor}\ollabel{cor:sub}271 For any $M$, $R$, and~$y$, there exists a pair of $M'$ and $R'$272 such that $M' \aeq M$, $R \aeq R'$ and $\Subst{M'}{R'}{y}$ is273 defined. Moreover, if there is another pair $M'' \aeq M$ and $R''$274 with $\Subst{M'}{R'}{y}$ defined, then $\Subst{M'}{R'}{y} \aeq275 \Subst{M''}{R''}{y}$.276\end{cor}277278\begin{proof}279 Immediate from \olref{thm:sub}.280\end{proof}281282\end{document}283
content/lambda-calculus/syntax/de-bruijn.tex
1% Part: lambda-calculus2% Chapter: syntax3% Section: de-bruijn45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{syn}{deb}1011\olsection{The De Bruijn Index}1213$\alpha$-Equivalence is very natural, as terms that are14$\alpha$-equivalent ``mean the same.'' In fact, it is possible to give15a syntax for lambda terms which does not distinguish terms that can be16$\alpha$-converted to each other. The best known replaces variables by17their \emph{De Bruijn index}.1819When we write $\lambd[x][M]$, we explicitly state that $x$ is the20parameter of the function, so that we can use $x$ in $M$ to refer to21this parameter. In the de Bruijn index, however, parameters have no22name and reference to them in the function body is denoted by a number23denoting the levels of abstraction between them. For example, consider24the example of $\lambd[x][\lambd[y][y x]]$: the outer abstraction is on25binds the variable~$x$; the inner abstraction binds the variable26is~$y$; the sub-term $y x$ lies in the scope of the inner abstraction:27there is no abstraction between $y$ and its abstract~$\lambd[y]$, but28one abstract between $x$ and its abstract~$\lambd[x]$. Thus we write29$0\, 1$ for $y x$, and $\lambd[][\lambd[][01]]$ for the entire term.3031\begin{defn}32 De Bruijn terms are inductively defines as follows:33 \begin{enumerate}34 \item $n$, where $n$ is any natural number.35 \item $PQ$, where $P$ and $Q$ are both De Bruijn terms.36 \item $\lambd[][N]$, where $N$ is a De Bruijn term.37 \end{enumerate}38\end{defn}3940A formalized translation from ordinary lambda terms to De Bruijn41indexed terms is as follows:42\begin{defn}43 \begin{align*}44 F_\Gamma(x) &= \Gamma(x) \\45 F_\Gamma(PQ) &= F_\Gamma(P)F_\Gamma(Q) \\46 F_\Gamma(\lambd[x][N]) &= \lambd[][F_{x,\Gamma}(N)]47 \end{align*}48 where $\Gamma$ is a list of variables indexed from zero, and49 $\Gamma(x)$ denotes the position of the variable $x$ in $\Gamma$.50 For example, if $\Gamma$ is $x,y,z$, then $\Gamma(x)$ is $0$ and51 $\Gamma(z)$ is $2$.52 53 $x,\Gamma$ denotes the list resulted from pushing $x$ to the head of54 $\Gamma$; for instance, continuing the $\Gamma$ in last example,55 $w,\Gamma$ is $w,x,y,z$.56\end{defn}5758Recovering a standard lambda term from a de Bruijn term is done as59follows:6061\begin{defn}62 \begin{align*}63 G_\Gamma(n) &= \Gamma[n] \\64 G_\Gamma(PQ) &= G_\Gamma(P) G_\Gamma(Q) \\65 G_\Gamma(\lambd[][N]) &= \lambd[x][G_{x,\Gamma}(N)]66 \end{align*}67 where $\Gamma$ is again a list of variables indexed from zero, and68 $\Gamma[n]$ denotes the variable in position~$n$. For example,69 if $\Gamma$ is $x,y,z$, then $\Gamma[1]$ is $y$.7071 The variable $x$ in last equation is chosen to be any variable that72 not in $\Gamma$.73\end{defn}7475Here we give some results without proving them:7677\begin{prop}78 If $M \aconvone M'$, and $\Gamma$ is any list containing $\FV{M}$, then79 $F_\Gamma(M) \eqs F_\Gamma(M')$.80\end{prop}8182\end{document}
content/lambda-calculus/syntax/term-revisited.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: term-revisited45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{syn}{tr}10\olsection{Terms as $\alpha$-Equivalence Classes}1112From now on, we will consider terms up to $\alpha$-equivalence. That13means when we write a term, we mean its $\alpha$-equivalance class it14is in. For example, we write $\lambd[a][\lambd[b][a c]]$ for the set15of all terms $\alpha$-equivalent to it, such as $\lambd[a][\lambd[b][a16 c]]$, $\lambd[b][\lambd[a][b c]]$, etc.1718Also, while in previous sections letters such as $N, Q$ are used to19denote a term, from now on we use them to denote a class, and it is20these classes instead of terms that will be our subjects of study in21what follows. Letters such as $x, y$ continues to denote a variable.2223We also adopt the notation $\rep{M}$ to denote an arbitrary24!!{element} of the class~$M$, and $\rep{M}[0], \rep{M}[1], etc. $ if25we need more than one.2627We reuse the notations from terms to simplify our wording. We28have following definition on classes:29\begin{defn}30 \begin{enumerate}31 \item $\lambd[x][N]$ is defined as the class containing32 $\lambd[x][\rep{N}]$.33 \item $PQ$ is defined to be the class containing $\rep{P}\rep{Q}$.34 \end{enumerate}35\end{defn}3637It is not hard to see that they are well defined, because38$\alpha$-conversion is compatible.3940\begin{defn} \ollabel{def:fv}41 The \emph{free variables} of an $\alpha$-equivalence class~$M$, or42 $FV(M)$, is defined to be $FV(\rep{M})$.43\end{defn}4445This is well defined since $FV(\rep{M}[0]) =46FV(\rep{M}[1])$, as shown in \olref[alp]{thm:fv}.4748We also reuse the notation for substition into classes:49\begin{defn} \ollabel{def:sub}50 The \emph{substitution} of $R$ for $y$ in $M$, or $\Subst{M}{R}{y}$, is51 defined to be $\Subst{\rep{M}}{\rep{R}}{y}$, for any $\rep{M}$ and52 $\rep{R}$ making the substition defined.53\end{defn}5455This is also well defined as shown in \olref[alp]{cor:sub}.5657Note how this definition significantly simplifies our reasoning. For58example:59\begin{align}60 \Subst{\lambd[x][x]}{y}{x} & =\ollabel{eq:1}\\61 &= \Subst{\lambd[z][z]}{y}{x} \ollabel{eq:2}\\62 &= \lambd[z][\Subst{z}{y}{x}] \\63 &= \lambd[z][z]64\end{align}6566\olref{eq:1} is undefined if we still regard it as substitution on67terms; but as mentioned earlier, we now consider it a substitution on68classes, which is why \olref{eq:2} can happen: we can replace69$\lambd[x][x]$ with $\lambd[z][z]$ because they belong to the same70class.7172For the same reason, from now on we will assume that the73representatives we choose always satisfy the conditions needed for74substitution. For example, when we see $\Subst{\lambd[x][N]}{R}{y}$,75we will assume the representative $\lambd[x][N]$ is chosen so that76$x \neq y$ and $x \notin FV(R)$.7778Since it is a bit strange to call $\lambd[x][x]$ a ``class'', let's79call them $\Lambda$-terms (or simply ``terms'' in the rest of the80part) from now on, to distinguish them from $\lambda$-terms that we81are familiar with.8283\begin{editorial}84 We cannot say goodbye to terms yet: the whole definition of85 $\Lambda$-terms is based on $\lambda$-terms, and we haven't provided86 a method to define functions on $\Lambda$-terms, which means all87 such functions have to be first defined on $\lambda$-terms, and88 then ``projected'' to $\Lambda$-terms, as we did for substitutions.89 However we assume the reader can intuitively understand how we can90 define functions on $\Lambda$-terms.91\end{editorial}92\end{document}
content/lambda-calculus/syntax/beta.tex
1% Part: lambda-calculus2% Chapter: introduction3% Section: beta45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{int}{bet}10\olsection{$\beta$-reduction}1112When we see $(\lambd[m][(\lambd[y][y]) m])$, it is natural to13conjecture that it has some connection with $\lambd[m][m]$, namely the14second term should be the result of ``simplifying'' the first. The15notion of \emph{$\beta$-reduction} captures this intuition formally.1617\begin{defn}[$\beta$-contraction, $\bredone$] \ollabel{defn:betacontr}18 \emph{The $\beta$-contraction} ($\bredone$) is the smallest compatible19 relation on terms satisfying the following condition:20 \[21 (\lambd[x][N])Q \bredone \Subst{N}{Q}{x} 22 \]23 We say $P$ is \emph{$\beta$-contracted} to $Q$ if $P \bredone Q$. A24 term of the form $(\lambd[x][N])Q$ is called a \emph{redex}.25\end{defn}2627\begin{prob} \ollabel{prob:def}28 Spell out the equivalent inductive definitions of $\beta$-contraction as we29 did for change of bound variable in \olref[lam][syn][alp]{defn:aconvone}.30\end{prob}31 32\begin{defn}[$\beta$-reduction, $\bred$] \ollabel{defn:betared}33 \emph{$\beta$-reduction} ($\bred$) is the smallest reflexive,34 transitive relation on terms containing $\bredone$. We say $P$ is35 $\beta$-reduced to $Q$ if $P \bred Q$.36\end{defn}3738We will write $\redone$ instead of $\bredone$, and $\red$ instead of39$\bred$ when context is clear.4041Informally speaking, $M \bred N$ if and only if $M$ can be changed to42$N$ by zero or several steps of $\beta$-contraction.4344\begin{defn}[$\beta$-normal]45A term that cannot be $\beta$-contracted any further is said to be46\emph{$\beta$-normal}. 47\end{defn}4849If $M \bred N$ and $N$ is $\beta$-normal, then we say $N$ is a50\emph{normal form} of~$M$. One may ask if the normal form of a term is51unique, and the answer is yes, as we will see later.5253Let us consider some examples.54\begin{enumerate}55\item We have56\begin{align*}57(\lambd[x][xxy]) \lambd[z][z] & \redone (\lambd[z][z])(\lambd[z][z]) y \\58& \redone (\lambd[z][z]) y \\59& \redone y60\end{align*}61\item ``Simplifying'' a term can actually make it more complex:62\begin{align*}63(\lambd[x][xxy])(\lambd[x][xxy]) & \redone (\lambd[x][xxy])(\lambd[x][xxy])y \\64& \redone (\lambd[x][xxy])(\lambd[x][xxy])yy \\65& \redone \dots66\end{align*}67\item It can also leave a term unchanged:68\[69(\lambd[x][xx])(\lambd[x][xx]) \redone (\lambd[x][xx])(\lambd[x][xx])70\]71\item Also, some terms can be reduced in more than one way; for72 example,73\[74(\lambd[x][(\lambd[y][yx]) z]) v \redone (\lambd[y][yv]) z75\]76by contracting the outermost application; and77\[78(\lambd[x][(\lambd[y][yx]) z]) v \redone (\lambd[x][zx]) v79\]80by contracting the innermost one. Note, in this case, however, that81both terms further reduce to the same term, $zv$.82\end{enumerate}8384The final outcome in the last example is not a coincidence, but rather85illustrates a deep and important property of the lambda calculus, known as the86Church--Rosser property.8788\begin{digress}89 In general, there is more than one way to $\beta$-reduce a term,90 thus many reduction strategies have been invented, among which the91 most common is the \emph{natural strategy}. The natural strategy92 always contracts the \emph{left-most} redex, where the position of a93 redex is defined as its starting point in the term. The natural94 strategy has the useful property that a term can be reduced to a95 normal form by some strategy iff it can be reduced to normal form96 using the natural strategy. In what follows we will use the natural97 stratuegy unless otherwise specified.98\end{digress}99100\begin{defn}[$\beta$-equivalence, $\equal$]101 \emph{$\beta$-Equivalence} ($\equal$) is the relation inductively102 defined as follows:103 \begin{enumerate}104 \item $M \equal M$.105 \item If $M \equal N$, then $N \equal M$.106 \item If $M \equal N$, $N \equal O$, then $M \equal O$.107 \item If $M \equal N$, then $PM \equal PN$.108 \item If $M \equal N$, then $MQ \equal NQ$.109 \item If $M \equal N$, then $\lambd[x][M] \equal \lambd[x][N]$.110 \item $(\lambd[x][N])Q \equal \Subst{N}{Q}{x}$.111 \end{enumerate}112\end{defn}113114The first three rules make the relation an equivalence relation; the115next three make it compatible; the last ensures that it contains116$\beta$-contraction.117118Informally speaking, two terms are $\beta$-equivalent if and only if119one of them can be changed to the other in zero or more steps of120$\beta$-contraction, or ``inverse'' of $\beta$-contraction. The inverse of121$\beta$-contraction is defined so that $M$ inverse-$\beta$-contracts to $N$122iff $N$ $\beta$-contracts to $M$.123124Besides the above rules, we will extend the relation with more125rules, and denote the extended equivalence relation as $\equal[X]$,126where $X$ is the extending rule.127128\end{document}
content/lambda-calculus/syntax/eta.tex
1% Part: lambda-calculus2% Chapter: syntax3% Section: eta45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{syn}{eta}10\olsection{$\eta$-conversion}1112There is another relation on $\lambda$ terms. In \olref[fv]{sec} we13used the example $\lambd[x][(fx)]$, which accepts an argument and14applies $f$ to it. In other words, it is the same function as~$f$:15$\lambd[x][(fx)]N$ and $fN$ both reduce to~$fN$. We use16$\eta$-reduction (and $\eta$-extension) to capture this idea.1718\begin{defn}[$\eta$-contraction, $\eredone$]19 \ollabel{defn:beredone}20 \emph{$\eta$-contraction} ($\eredone$) is the smallest compatible relation21 on terms satisfying the following condition:22 \[23 \lambd[x][M x] \eredone M \text{ provided } x \notin FV(M)24 \]25\end{defn}2627\begin{defn}[$\beta\eta$-reduction, $\bered$] \ollabel{defn:bered}28 \emph{$\beta\eta$-reduction} ($\bered$) is the smallest reflexive,29 transitive relation on terms containing $\bredone$ and $\eredone$,30 i.e., the rules of reflexivity and transitive plus the following two31 rules:32 \begin{enumerate}33 \item If $M \bredone N$ then $M \bered N$. \ollabel{defn:bered3}34 \item If $M \eredone N$ then $M \bered N$. \ollabel{defn:bered4}35 \end{enumerate}36 37\end{defn}3839\begin{defn}40 We extend the equivalence relation $\equal$ with the $\eta$-conversion rule:41 \[42 \lambd[x][f x] \equal f43 \]44 and denote the extended relation as $\equal[\eta]$.45\end{defn}4647$\eta$-equivalence is important because it is related to extensionality48of lambda terms:4950\begin{defn}[Extensionality]51 We extend the equivalence relation $\equal$ with the (\ext) rule:52 \begin{center}53 If $Mx \equal Nx$ then $M \equal N$, provided $x \notin FV(MN)$.54 \end{center}55 and denote the extended relation as $\equal[\ext]$.56\end{defn}5758Roughly speaking, the rule states that two terms, viewed as functions,59should be considered equal if they behave the same for the same60argument.6162We now prove that the $\eta$ rule provides exactly the extensionality,63and nothing else.6465\begin{thm}66 $M \equal[\ext] N$ if and only if $M \equal[\eta] N$.67\end{thm}6869\begin{proof}70 First we prove that $\equal[\eta]$ is closed under the71 extensionality rule. That is, $ext$ rule doesn't add anything to72 $\equal[\eta]$. We then have $\equal[\eta]$ contains $\equal[\ext]$,73 and if $M \equal[\ext] N$, then $M \equal[\eta] N$.7475 To prove $\equal[\eta]$ is closed under \ext, note that for any $M76 \equal N$ derived by the \ext{} rule, we have $Mx77 \equal[\eta] Nx$ as premise. Then we have $\lambd[x][Mx]78 \equal[\eta] \lambd[x][Nx]$ by a rule of $\equal$, applying $\eta$79 on both side gives us $M \equal[\eta] N$.8081 Similarly we prove that the $\eta$ rule is contained in82 $\equal[ext]$. For any $\lambd[x][Mx]$ and $M$ with $x \notin83 FV(M)$, we have that $(\lambd[x][Mx])x \equal[\ext] Mx$,84 giving us $\lambd[x][Mx] \equal[\ext] M$ by the \ext{} rule.85\end{proof}8687\end{document}