Lambda calculus

Lambda Calculus Syntax

Reading preferences

Optional display controls need JavaScript. All reading content and navigation work without it.

Source file content/lambda-calculus/syntax/syntax.tex

Source file content/lambda-calculus/syntax/terms.tex

Terms

The terms of the lambda calculus are built up inductively from an infinite supply of variables v0\Obj{v_0}source, v1\Obj{v_1}source, dots, the symbol “λ\lambdsource”, and parentheses. We will use xxsource, yysource, zzsource, dots to designate variables, and MMsource, NNsource, PPsource, dots to desginate terms.

Definition of lambda terms

[Terms] The set of terms of the lambda calculus is defined inductively by:

  1. If xxsource is a variable, then xxsource is a term.

  2. If xxsource is a variable and MMsource is a term, then (λx.M)(\lambd[x][M])source is a term.

  3. If both MMsource and NNsource are terms, then (MN)(MN)source is a term.

If a term (λx.M)(\lambd[x][M])source is formed according to the abstraction formation clause we say it is the result of an abstraction, and the xxsource in λx\lambd[x]source is called a parameter. A term (MN)(MN)source formed according to the application formation clause is the result of an application.

The terms defined above are fully parenthesized. This can get rather cumbersome, as the term (λx.((λx.x)(λx.(xx))))(\lambd[x][((\lambd[x][x])(\lambd[x][(xx)]))])source demnostrates. We will introduce conventions for avoiding parentheses. However, the official definition makes it easy to determine how a term is constructed according to the definition of lambda terms. For example, the last step of forming the term (λx.((λx.x)(λx.(xx))))(\lambd[x][((\lambd[x][x])(\lambd[x][(xx)]))])source must be abstraction where the parameter is xxsource. It results by abstraction from the term ((λx.x)(λx.(xx)))((\lambd[x][x])(\lambd[x][(xx)]))source, which is an application of two terms. Each of these two terms is the result of an abstraction, and so on.

Exercise on formation of a nested lambda term

Describe the formation of (λg.(λx.(g(xx)))(λx.(g(xx))))(\lambd[g][(\lambd[x][(g (x x))]) (\lambd[x][(g (x x))])])source.

Source file content/lambda-calculus/syntax/unique-readability.tex

Unique Readability

We may wonder if for each term there is a unique way of forming it, and there is. For each lambda term there is only one way to construct and interpret it. In the following discussion, a formation is the procedure of constructing a term using the formation rules (one or several times) of the definition of lambda terms.

Lemma on the first symbol of a term

A term starts with either a variable or a parenthesis.

Proof

Something counts as a term only if it is constructed according to the definition of lambda terms. If it is the result of the variable formation clause, it must be a variable. If it is the result of the abstraction formation clause or the application formation clause, it starts with a parenthesis.

Lemma on the first symbols of an application

The result of an application starts with either two parentheses or a parenthesis and a variable.

Proof

If MMsource is the result of an application, it is of the form (PQ)(PQ)source, so it begins with a parenthesis. Since PPsource is a term, by the lemma on the first symbol of a term, it begins either with a parenthesis or a variable.

Lemma on proper initial parts

No proper initial part of a term is itself a term.

Exercise proving the initial part lemma

Prove the lemma that no proper initial part is a term by induction on the length of terms.

Unique readability proposition

[Unique Readability] There is a unique formation for each term. In other words, if a term MMsource is formed by a formation, then it is the only formation that can form this term.

Proof

We prove this by induction on the formation of terms.

  1. MMsource is of the form xxsource, where xxsource is some variable. Since the results of abstractions and applications always start with parentheses, they cannot have been used to construct MMsource; Thus, the formation of MMsource must be a single step of the definition of lambda termsthe variable formation clause.

  2. MMsource is of the form (λx.N)(\lambd[x][N])source, where xxsource is some variable and NNsource is a term. It could not have been constructed according to the definition of lambda termsthe variable formation clause, because it is not a single variable. It is not the result of an application, by the lemma on the first symbols of an application. Thus MMsource can only be the result of an abstraction on NNsource. By inductive hypothesis we know that formation of NNsource is itself unique.

  3. MMsource is of the form (PQ)(PQ)source, where PPsource and QQsource are terms. Since it starts with a parentheses, it cannot also be constructed by the definition of lambda termsthe variable formation clause. By the lemma on the first symbol of a term, PPsource cannot begin with λ\lambdsource, so (PQ)(PQ)source cannot be the result of an abstraction. Now suppose there were another way of constructing MMsource by application, e.g., it is also of the form (PQ)(P'Q')source. Then PPsource is a proper initial segment of PP'source (or vice versa), and this is impossible by the lemma that no proper initial part is a term. So PPsource and QQsource are uniquely determined, and by inductive hypothesis we know that formations of PPsource and QQsource is unique.

A more readable paraphrase of the above proposition is as follows:

Three uniquely determined forms of a term

A term MMsource can only be one of the following forms:

  1. xxsource, where xxsource is a variable uniquely determined by MMsource.

  2. (λx.N)(\lambd[x][N])source, where xxsource is a variable and NNsource is another term, both of which is uniquely determined by MMsource.

  3. (PQ)(PQ)source, where PPsource and QQsource are two terms uniquely determined by MMsource.

Source file content/lambda-calculus/syntax/abbreviated-syntax.tex

Abbreviated Syntax

Terms as defined in the definition of lambda terms are sometimes cumbersome to write, so it is useful to introduce a more concise syntax. We must of course be careful to make sure that the terms in the concise notation also are uniquely readable. One widely used version called abbreviated terms is as follows.

  1. When parentheses are left out, application takes place from left to right. For example, if MMsource, NNsource, PPsource, and QQsource are terms, then MNPQMNPQsource abbreviates (((MN)P)Q)(((MN)P)Q)source.

  2. Again, when parentheses are left out, lambda abstraction is given the widest scope possible. From example, λx.MNP\lambd[x][MNP]source is read as (λx.MNP)(\lambd[x][MNP])source.

  3. A lambda can be used to abstract multiple variables. For example, λxyz.M\lambd[xyz][M]source is short for λx.λy.λz.M\lambd[x][\lambd[y][\lambd[z][M]]]source.

For example,

λxy.xxyxλz.xz\lambd[xy][xxyx \lambd[z][xz]]source

abbreviates

(λx.(λy.((((xx)y)x)(λz.(xz))))).(\lambd[x][(\lambd[y][((((xx)y)x)(\lambd[z][(xz)]))])]).source

Exercise expanding abbreviated syntax

Expand the abbreviated term λg.(λx.g(xx))λx.g(xx)\lambd[g][(\lambd[x][g (x x)]) \lambd[x][g (x x)]]source.

Source file content/lambda-calculus/syntax/free-variables.tex

Free Variables

Lambda calculus is about functions, and lambda abstraction is how functions arise. Intuitively, λx.M\lambd[x][M]source is the function with values given by MMsource when the argument to the function is assigned to xxsource. But not every occurrence of xxsource in MMsource is relevant: if MMsource contains another abstract λx.N\lambd[x][N]source then the occurrences of xxsource in NNsource are relevant to λx.N\lambd[x][N]source but not to λx.M\lambd[x][M]source. So, a lambda abstract λx\lambd[x]source inside λx.M\lambd[x][M]source binds those occurrences of xxsource in MMsource that are not already bound by another lambda abstract---the free occurrences of xxsource in MMsource.

Definition of scope as printed

[Scope] If λx.M\lambd[x][M]source occurs inside a term NNsource, then the corresponding occurrence of NNsource is the scope of the λx\lambd[x]source.

Definition of free and bound occurrence

[Free and bound occurrence] An occurrence of variable xxsource in a term MMsource is free if it is not in the scope of a λx\lambd[x]source, and bound otherwise. An occurrence of a variable xxsource in λx.M\lambd[x][M]source is bound by the initial λx\lambd[x]source iff the occurrence of xxsource in MMsource is free.

Examples distinguishing nested binders

In λx.xy\lambd[x][x y]source, both xxsource and yysource are in the scope of λx\lambd[x]source, so xxsource is bound by λx\lambd[x]source. Since yysource is not in the scope of any λy\lambd[y]source, it is free. In λx.xx\lambd[x][x x]source, both occurrences of xxsource are bound by λx\lambd[x]source, since both are free in xxxxsource. In ((λx.xx)x)((\lambd[x][xx])x)source, the last occurrence of xxsource is free, since it is not in the scope of a λx\lambd[x]source. In λx.(λx.x)x\lambd[x][(\lambd[x][x])x]source, the scope of the first λx\lambd[x]source is (λx.x)x(\lambd[x][x])xsource and the scope of the second λx\lambd[x]source is the second-to-last occurrence of xxsource. In (λx.x)x(\lambd[x][x])xsource, the last occurrence of xxsource is free, and the second-to-last is bound. Thus, the second-to-last occurrence of xxsource in λx.(λx.x)x\lambd[x][(\lambd[x][x])x]source is bound by the second λx\lambd[x]source, and the last occurrence by the first λx\lambd[x]source.

For a term PPsource, we can check all variable occurrences in it and get a set of free variables. This set is denoted by FV(P)\FV{P}source with a natural definition as follows:

Recursive definition of free variables

[Free variables of a term] The set of free variables of a term is defined inductively by:

  1. FV(x)={x}\FV{x} = \{x\}source

  2. FV(λx.N)=FV(N){x}\FV{\lambd[x][N]} = \FV{N} \setminus \{x\}source

  3. FV(PQ)=FV(P)FV(Q)\FV{PQ} = \FV{P} \cup \FV{Q}source

Exercises identifying scopes and free variables

  1. Identify the scopes of λg\lambd[g]source and the two λx\lambd[x]source in this term: λg.(λx.g(xx))λx.g(xx)\lambd[g][(\lambd[x][g (x x)]) \lambd[x][g (x x)]]source.

  2. In λg.(λx.g(xx))λx.g(xx)\lambd[g][(\lambd[x][g (x x)]) \lambd[x][g (x x)]]source, are all occurrences of variables bound? By which abstractions are they bound respectively?

  3. Give FV(λx.(λy.(λz.xy)z)y)\FV{\lambd[x][(\lambd[y][(\lambd[z][x y]) z]) y]}source

Explain

A free variable is like a reference to the outside world (the environment), and a term containing free variables can be seen as a partially specified term, since its behaviour depends on how we set up the environment. For example, in the term λx.fx\lambd[x][f x]source, which accepts an argument xxsource and returns ffsource of that argument, the variable ffsource is free. This value of the term is dependent on the environment it is in, in particular the value of ffsource in that environment.

If we apply abstraction to this term, we get λf.λx.fx\lambd[f][\lambd[x][f x]]source. This term is no longer dependent on the environment variable ffsource, because it now designates a function that accepts two arguments and returns the result of applying the first to the second. Changing ffsource in the environment won't have any effect on the behavior of this term, as the term will only use whatever is passed as an argument, and not the value of ffsource in the environment.

Definition of closed term and combinator

[Closed term, combinator] A term with no free variables is called a closed term, or a combinator.

Free variable membership lemma

  1. If yxy \neq xsource, then yFV(λx.N)y \in \FV{\lambd[x][N]}source iff yFV(N)y \in \FV{N}source.

  2. yFV(PQ)y \in \FV{PQ}source iff yFV(P)y \in \FV{P}source or yFV(Q)y \in \FV{Q}source.

Proof

Exercise.

Exercise proving free variable membership

Prove the free variable membership lemma.

Source file content/lambda-calculus/syntax/substitution.tex

Substitution

Explain

Free variables are references to environment variables, thus it makes sense to actually use a specific value in the place of a free variable. For example, we may want to replace ffsource in λx.fx\lambd[x][f x]source with a specific term, like the identity function λy.y\lambd[y][y]source. This results in λx.(λy.y)x\lambd[x][(\lambd[y][y]) x]source. The process of replacing free variables with lambda terms is called substitution.

Definition of partial substitution on terms

[Substitution] The substitution of a term NNsource for a variable xxsource in a term MMsource, M[N/x]\Subst{M}{N}{x}source, is defined inductively by:

  1. x[N/x]=N\Subst{x}{N}{x} = Nsource.

  2. y[N/x]=y\Subst{y}{N}{x} = ysource if xyx \neq ysource.

  3. PQ[N/x]=(P[N/x])(Q[N/x])\Subst{PQ}{N}{x} = (\Subst{P}{N}{x}) (\Subst{Q}{N}{x})source.

  4. (λy.P)[N/x]=λy.P[N/x]\Subst{(\lambd[y][P])}{N}{x} = \lambd[y][\Subst{P}{N}{x}]source, if xyx \neq ysource and yFV(N)y \notin \FV{N}source, otherwise undefined.

Explain

In the definition of partial substitutionthe substitution definition's abstraction clause, we require xyx \neq ysource because we don't want to replace bound occurrences of the variable xxsource in MMsource by NNsource. For example, if we compute the substitution λx.x[y/x]\Subst{\lambd[x][x]}{y}{x}source, the result should not be λx.y\lambd[x][y]source but simply λx.x\lambd[x][x]source.

When substituting NNsource for xxsource in λy.P\lambd[y][P]source, we also require that yFV(N)y \notin \FV{N}source. For example, we cannot substitute yysource for xxsource in λy.x\lambd[y][x]source, i.e., λy.x[y/x]\Subst{\lambd[y][x]}{y}{x}source, because it would result in λy.y\lambd[y][y]source, a term that stands for the function that accepts an argument and returns it directly. But the term λy.x\lambd[y][x]source stands for a function that always returns the term xxsource (or whatever xxsource refers to). So the result we actually want is a function that accepts an argument, drop it, and returns the environment variable yysource. To do this properly, we would first have to “rename” the bound variable yysource.

Exercises evaluating partial substitutions

What is the result of the following substitutions?

  1. λy.x(λw.vwx)[(uv)/x]\Subst{\lambd[y][x(\lambd[w][vwx])]}{(uv)}{x}source

  2. λy.x(λx.x)[(λy.xy)/x]\Subst{\lambd[y][x(\lambd[x][x])]}{(\lambd[y][xy])}{x}source

  3. y(λv.xv)[(λy.vy)/x]\Subst{y(\lambd[v][xv])}{(\lambd[y][vy])}{x}source

Free variables after substitution for a variable not free

If xFV(M)x \notin \FV{M}source, then FV(M[N/x])=FV(M)\FV{\Subst{M}{N}{x}} = \FV{M}source, if the left-hand side is defined.

Proof

By induction on the formation of MMsource.

  1. MMsource is a variable: exercise.

  2. MMsource is of the form (PQ)(PQ)source: exercise.

  3. MMsource is of the form λy.P\lambd[y][P]source, and since λy.P[N/x]\Subst{\lambd[y][P]}{N}{x}source is defined, it has to be λy.P[N/x]\lambd[y][\Subst{P}{N}{x}]source. Then P[N/x]\Subst{P}{N}{x}source has to be defined; also, xyx \neq ysource and xFV(Q)x \notin \FV{Q}source. Then:

    FV(λy.P[N/x])==FV(λy.P[N/x])by the substitution definition's abstraction clause=FV(P[N/x]){y}by the definition of free variablesthe free variable definition's abstraction clause=FV(P){y}by inductive hypothesis=FV(λy.P)by the definition of free variablesthe free variable definition's abstraction clause\FV{\Subst{\lambd[y][P]}{N}{x}} = \\ \begin{aligned} & = \FV{\lambd[y][\Subst{P}{N}{x}]} && \text{by \olref{defn:substitution-4}}\\ & = \FV{\Subst{P}{N}{x}} \setminus \{y\} && \text{by \olref[fv]{def:fv}\olref[fv]{def:fv2}}\\ & = \FV{P} \setminus \{y\} && \text{by inductive hypothesis} \\ & = \FV{\lambd[y][P]} && \text{by \olref[fv]{def:fv}\olref[fv]{def:fv2}} \end{aligned}source

Exercise completing the nonfree substitution proof

Complete the proof of the substitution theorem for a variable not free in the term.

Free variables after substitution for a free variable

If xFV(M))x \in \FV{M})source, then FV(M[N/x])=(FV(M){x})FV(N)\FV{\Subst{M}{N}{x}} = (\FV{M} \setminus \{x\}) \cup \FV{N}source, provided the left hand is defined.

Proof

By induction on the formation of MMsource.

  1. MMsource is a variable: exercise.

  2. MMsource is of the form PQPQsource: Since (PQ)[N/y]\Subst{(PQ)}{N}{y}source is defined, it has to be (P[N/x])(Q[N/x])(\Subst{P}{N}{x})(\Subst{Q}{N}{x})source with both substitution defined. Also, since xFV(PQ)x \in \FV{PQ}source, either xFV(P)x \in \FV{P}source or xFV(Q)x \in \FV{Q}source or both. The rest is left as an exercise.

  3. MMsource is of the form λy.P\lambd[y][P]source. Since λy.P[N/x]\Subst{\lambd[y][P]}{N}{x}source is defined, it has to be λy.P[N/x]\lambd[y][\Subst{P}{N}{x}]source, with P[N/x]\Subst{P}{N}{x}source defined, xyx \neq ysource and yFV(N)y \notin \FV{N}source; also, since yFV(λx.P)y \in \FV{\lambd[x][P]}source, we have yFV(P)y \in \FV{P}source too. Now:

    FV((λy.P)[N/x])==FV(λy.P[N/x])=FV(P[N/x]){y}=((FV(P){y})(FV(N){x})by inductive hypothesis=(FV(P){x,y})FV(N)xFV(N)=(FV(λy.P){x})FV(N)\FV{\Subst{(\lambd[y][P])}{N}{x}} = \\ \begin{aligned} & = \FV{\lambd[y][\Subst{P}{N}{x}]} \\ & = \FV{\Subst{P}{N}{x}} \setminus \{y\} \\ & = ((\FV{P} \setminus \{y\}) \cup (\FV{N} \setminus \{x\}) && \text{by inductive hypothesis} \\ & = (\FV{P} \setminus \{x, y\}) \cup \FV{N} && x \notin \FV{N} \\ & = (\FV{\lambd[y][P]} \setminus \{x\}) \cup \FV{N} \end{aligned}source

Exercise completing the free substitution proof

Complete the proof of the substitution theorem for a variable free in the term.

Theorem on removal of a free variable

xFV(M[N/x])x \notin \FV{\Subst{M}{N}{x}}source, if the right-hand side is defined and xFV(N)x \notin \FV{N}source.

Proof

Exercise.

Exercise proving removal of a free variable

Prove the theorem on removal of a free variable by substitution.

Theorem on inverse variable substitution

If M[y/x]\Subst{M}{y}{x}source is defined and yFV(M)y \notin \FV{M}source, then M[y/x][x/y]=M\Subst{\Subst{M}{y}{x}}{x}{y} = Msource.

Proof

By induction on the formation of MMsource.

  1. MMsource is a variable zzsource: Exercise.

  2. MMsource is of the form (PQ)(PQ)source. Then:

    (PQ)[y/x][x/y]=((P[y/x])(Q[y/x]))[x/y]=(P[y/x][x/y])(Q[y/x][x/y])=(PQ) by inductive hypothesis\Subst{\Subst{(PQ)}{y}{x}}{x}{y} &=\Subst{((\Subst{P}{y}{x})(\Subst{Q}{y}{x}))}{x}{y} \\ &= (\Subst{\Subst{P}{y}{x}}{x}{y})(\Subst{\Subst{Q}{y}{x}}{x}{y}) \\ &= (PQ) \text{ by inductive hypothesis}source
  3. MMsource is of the form λz.N\lambd[z][N]source. Because λz.N[y/x]\Subst{\lambd[z][N]}{y}{x}source is defined, we know that zyz \neq ysource. So:

    (λz.N)[y/x][x/y]=(λz.N[y/x])[x/y]=λz.N[y/x][x/y]=λz.N by inductive hypothesis\Subst{\Subst{(\lambd[z][N])}{y}{x}}{x}{y}\\ & = \Subst{(\lambd[z][\Subst{N}{y}{x}])}{x}{y} \\ & = \lambd[z][\Subst{\Subst{N}{y}{x}}{x}{y}] \\ &= \lambd[z][N] \text{ by inductive hypothesis}source

Exercise completing the inverse substitution proof

Complete the proof of the inverse substitution theorem.

Source file content/lambda-calculus/syntax/alpha.tex

α\alphasource-Conversion

What is the relation between λx.x\lambd[x][x]source and λy.y\lambd[y][y]source? They both represent the identity function. They are, of course, syntactically different terms. They differ only in the name of the bound variable, and one is the result of “renaming” the bound variable in the other. This is called α\alphasource-conversion.

Local definition of change of bound variable

[Change of bound variable, α\aconvonesource] If a term MMsource contains an occurrence of λx.N\lambd[x][N]source, yFV(N)y \notin \FV{N}source, and N[y/x]\Subst{N}{y}{x}source is defined, then replacing this occurrence by

λy.N[y/x]\lambd[y][\Subst{N}{y}{x}]source

resulting in MM'source is called a change of bound variable, written as MαMM \redone[\alpha] M'source.

Definition of compatible relation

[Compatibility of relation] A relation RRsource on terms is said to be compatible if it satisfies following conditions:

  1. If RNNR N N'source then Rλx.Nλx.NR \lambd[x][N] \lambd[x][N']source

  2. If RPPR P P'source then R(PQ)(PQ)R (PQ) (P'Q)source

  3. If RQQR Q Q'source then R(PQ)(PQ)R (PQ) (PQ')source

Thus let's rephrase the definition:

Compatible closure definition of bound variable change

[Change of bound variable, α\aconvonesource] Change of bound variable (α\redone[\alpha]source) is the smallest compatible relation on terms satisfying following condition:

λx.Nαλy.N[y/x]if xy, yFV(N)and N[y/x] is defined&\lambd[x][N] \redone[\alpha] \lambd[y][\Subst{N}{y}{x}] && \text{if $x \neq y$, $y \notin \FV{N}$} \\ & &&\text{and $\Subst{N}{y}{x}$ is defined}source

“Smallest” here means the relation contains only pairs that are required by compatibility and the additional condition, and nothing else. Thus this relation can also be defined as follows:

Inductive definition of one alpha change

[Change of bound variable, α\aconvonesource] Change of bound variable (α\aconvonesource) is inductively defined as follows:

  1. If NαNN \aconvone N'source then λx.Nαλx.N\lambd[x][N] \aconvone \lambd[x][N']source

  2. If PαPP \aconvone P'source then (PQ)α(PQ)(PQ) \aconvone (P'Q)source

  3. If QαQQ \aconvone Q'source then (PQ)α(PQ)(PQ) \aconvone (PQ')source

  4. If xyx \neq ysource, yFV(N)y \notin \FV{N}source and N[y/x]\Subst{N}{y}{x}source is defined, then λx.Nαλy.N[y/x]\lambd[x][N] \redone[\alpha] \lambd[y][\Subst{N}{y}{x}]source.

The definitions are equivalent, but we leave the proof as an exercise. From now on we will use the inductive definition.

Reflexive transitive closure definition of alpha conversion

[α\alphasource-conversion, α\aconvsource] α\alphasource-conversion (α\aconvsource) is the smallest reflexitive and transitive relation on terms containing α\aconvonesource.

As above, “smallest” means the relation only contains pairs required by transitivity, and α\aconvonesource, which leads to the following equivalent definition:

Inductive definition of alpha conversion

[α\alphasource-conversion, α\aconvsource] α\alphasource-conversion (α\aconvsource) is inductively defined as follows:

  1. If PαQP \aconv Qsource and QαRQ \aconv Rsource, then PαRP \aconv Rsource.

  2. If PαQP \aconvone Qsource, then PαQP \aconv Qsource.

  3. PαPP \aconv Psource.

Examples of alpha conversion and free names

λx.fx\lambd[x][f x]source α\alphasource-converts to λy.fy\lambd[y][f y]source, and conversely. Informally speaking, they are both functions that accept an argument and return ffsource of that argument, refering to the environment variable ffsource.

λx.fx\lambd[x][f x]source does not α\alphasource-convert to λx.gx\lambd[x][g x]source. Informally speaking, they refer to the environment variables ffsource and ggsource respectively, and this makes them different functions: they behave differently in environments where ffsource and ggsource are different.

Exercise deciding alpha convertibility

Are the following pairs of terms α\alphasource-convertible?

  1. λx.λy.x\lambd[x][\lambd[y][x]]source and λy.λx.y\lambd[y][\lambd[x][y]]source

  2. λx.λy.x\lambd[x][\lambd[y][x]]source and λc.λb.a\lambd[c][\lambd[b][a]]source

  3. λx.λy.x\lambd[x][\lambd[y][x]]source and λc.λb.a\lambd[c][\lambd[b][a]]source

Lemma that one alpha change preserves free variables

If PαQP \aconvone Qsource then FV(P)=FV(Q)\FV{P} = \FV{Q}source.

Proof

By induction on the derivation of PαQP \aconvone Qsource.

  1. If the last rule is the bound variable renaming clause, then PPsource is of the form λx.N\lambd[x][N]source and QQsource of the form λy.N[y/x]\lambd[y][\Subst{N}{y}{x}]source, with xyx \neq ysource, yFV(N)y \notin \FV{N}source and N[y/x]\Subst{N}{y}{x}source defined. We distinguish cases according to whether xFV(N)x \in \FV{N}source:

    1. If xFV(N)x \in FV(N)source, then:

      FV(λy.N[y/x])=FV(N[y/x]){y}=((FV(N){x}){y}){y} by the substitution theorem for a variable free in the term=FV(N){x}=FV(λx.N)\FV{\lambd[y][\Subst{N}{y}{x}]} &= \FV{\Subst{N}{y}{x}} \setminus \{y\} \\ & = ((\FV{N} \setminus \{x\}) \cup \{y\}) \setminus \{y\} && \text{ by \olref[sub]{thm:infv}} \\ & = \FV{N} \setminus \{x\} \\ & = \FV{\lambd[x][N]}source
    2. If xFV(N)x \notin FV(N)source, then:

      FV(λy.N[y/x])=FVN[y/x]{y}=FV(N){x}by the substitution theorem for a variable not free in the term=FV(λx.N).\FV{\lambd[y][\Subst{N}{y}{x}]} & = FV{\Subst{N}{y}{x}} \setminus \{y\} \\ & = \FV{N} \setminus \{x\} && \text{by \olref[sub]{thm:notinfv}} \\ & = \FV{\lambd[x][N]}.source
  2. The other three cases are left as exercises.

Exercise completing preservation of free variables

Complete the proof of the lemma that one alpha change preserves free variables.

Lemma reversing one alpha change

If PαQP \aconvone Qsource then QαPQ \aconvone Psource.

Proof

Induction on the derivation of PαQP \aconvone Qsource.

  1. If the last rule is the bound variable renaming clause, then PPsource is of the form λx.N\lambd[x][N]source and QQsource of the form λy.N[y/x]\lambd[y][\Subst{N}{y}{x}]source, where xyx \neq ysource, yFV(N)y \notin \FV{N}source and N[y/x]\Subst{N}{y}{x}source defined. First, we have yFV(N[y/x])y \notin \FV{\Subst{N}{y}{x}}source by the theorem on removal of a free variable by substitution. By the inverse substitution theorem we have that N[y/x][x/y]\Subst{\Subst{N}{y}{x}}{x}{y}source is not only defined, but also equal to NNsource. Then by the bound variable renaming clause, we have λy.N[y/x]αλx.N[y/x][x/y]=λx.N\lambd[y][\Subst{N}{y}{x}] \aconvone \lambd[x][\Subst{\Subst{N}{y}{x}}{x}{y}] = \lambd[x][N]source.

Exercise completing reversibility of alpha change

Complete the proof of the lemma reversing one change of bound variable

Theorem that alpha conversion is an equivalence relation

α\alphasource-Conversion is an equivalence relation on terms, i.e., it is reflexive, symmetric, and transitive.

Proof

  1. For each term MMsource, MMsource can be changed to MMsource by zero changes of bound variables.

  2. If PPsource is α\alphasource-converts to QQsource by a series of changes of bound variables, then from QQsource we can just inverse these changes (by the lemma reversing one change of bound variable) in opposite order to obtain PPsource.

  3. If PPsource α\alphasource-converts to QQsource by a series of changes of bound variables, and QQsource to RRsource by another series, then we can change PPsource to RRsource by first applying the first series and then the second series.

From now on we say that MMsource and NNsource are α\alphasource-equivalent, M=αNM \aeq Nsource, iff MMsource α\alphasource-converts to NNsource (which, as we've just shown, is the case iff NNsource α\alphasource-converts to MMsource).

Alpha equivalence preserves free variables

If M=αNM \aeq Nsource, then FV(M)=FV(N)\FV{M} = \FV{N}source.

Proof

Immediate from the lemma that one alpha change preserves free variables.

Lemma on an alpha equivalent replacement term

If R=αRR \aeq R'source and M[R/y]\Subst{M}{R}{y}source is defined, then M[R/y]\Subst{M}{R'}{y}source is defined and α\alphasource-equivalent to M[R/y]\Subst{M}{R}{y}source.

Proof

Exercise.

Exercise on an alpha equivalent replacement term

Prove the substitution lemma for alpha equivalent replacement terms.

Recall that in the section Substitution, substitution is undefined in some cases; however, using α\alphasource-conversion on terms, we can make substitution always defined by renaming bound variables. The result preserves α\alphasource-equivalence, as shown in this theorem:

Theorem making substitution defined by alpha conversion

For any MMsource, RRsource, and yysource, there exists MM'source such that M=αMM \aeq M'source and M[R/y]\Subst{M'}{R}{y}source is defined. Moreover, if there is another pair M=αMM'' \aeq Msource and RR''source where M[R/y]\Subst{M''}{R''}{y}source is defined and R=αRR'' \aeq Rsource, then M[R/y]=αM[R/y]\Subst{M'}{R}{y} \aeq \Subst{M''}{R''}{y}source.

Proof

By induction on the formation of MMsource:

  1. MMsource is a variable zzsource: Exercise.

  2. Suppose MMsource is of the form λx.N\lambd[x][N]source. Select a variable zzsource other than xxsource and yysource and such that zFV(N)z \notin \FV{N}source and zFV(R)z \notin \FV{R}source. By inductive hypothesis, we there is NN'source such that N=αNN' \aeq Nsource and N[z/x]\Subst{N'}{z}{x}source is defined. Then λx.N=αλx.N\lambd[x][N] \aeq \lambd[x][N']source too, by the inductive definition of one change of bound variableits abstraction compatibility clause. Now λx.N=αλz.N[z/x]\lambd[x][N'] \aeq \lambd[z][\Subst{N'}{z}{x}]source by the inductive definition of one change of bound variablethe bound variable renaming clause. We can do this because zxz \ne xsource, zFV(N)z \notin FV(N')source and N[z/x]\Subst{N'}{z}{x}source is defined. Finally, λz.N[z/x][R/y]\Subst{\lambd[z][\Subst{N'}{z}{x}]}{R}{y}source is defined, because zyz \neq ysource and zFV(R)z \notin FV(R)source.

    Moreover, if there is another NN''source and RR''source satisfying the same conditions,

    (λz.N[z/x])[R/y]==λz.N[z/x][R/y]=λz.N[z/x][R/y]by the substitution lemma for alpha equivalent replacement terms=λz.N[z/x][R/y]by inductive hypothesis=(λz.N[z/x])[R/y]\Subst{(\lambd[z][\Subst{N''}{z}{x}])}{R''}{y} =\\ \begin{aligned} &= \lambd[z][\Subst{\Subst{N''}{z}{x}}{R''}{y}] \\ &= \lambd[z][\Subst{\Subst{N''}{z}{x}}{R}{y}] && \text{by \olref{lem:sub:R}}\\ &=\lambd[z][\Subst{\Subst{N'}{z}{x}}{R}{y}] && \text{by inductive hypothesis}\\ &=\Subst{(\lambd[z][\Subst{N'}{z}{x}])}{R}{y} \end{aligned}source
  3. MMsource is of the form (PQ)(PQ)source: Exercise.

Exercise completing alpha representative substitution

Complete the proof of the theorem choosing alpha equivalent representatives for defined substitution.

Printed corollary on pairs of representatives

For any MMsource, RRsource, and yysource, there exists a pair of MM'source and RR'source such that M=αMM' \aeq Msource, R=αRR \aeq R'source and M[R/y]\Subst{M'}{R'}{y}source is defined. Moreover, if there is another pair M=αMM'' \aeq Msource and RR''source with M[R/y]\Subst{M'}{R'}{y}source defined, then M[R/y]=αM[R/y]\Subst{M'}{R'}{y} \aeq \Subst{M''}{R''}{y}source.

Proof

Immediate from the theorem choosing alpha equivalent representatives for defined substitution.

Source file content/lambda-calculus/syntax/de-bruijn.tex

The De Bruijn Index

α\alphasource-Equivalence is very natural, as terms that are α\alphasource-equivalent “mean the same.” In fact, it is possible to give a syntax for lambda terms which does not distinguish terms that can be α\alphasource-converted to each other. The best known replaces variables by their De Bruijn index.

When we write λx.M\lambd[x][M]source, we explicitly state that xxsource is the parameter of the function, so that we can use xxsource in MMsource to refer to this parameter. In the de Bruijn index, however, parameters have no name and reference to them in the function body is denoted by a number denoting the levels of abstraction between them. For example, consider the example of λx.λy.yx\lambd[x][\lambd[y][y x]]source: the outer abstraction is on binds the variable xxsource; the inner abstraction binds the variable is yysource; the sub-term yxy xsource lies in the scope of the inner abstraction: there is no abstraction between yysource and its abstract λy\lambd[y]source, but one abstract between xxsource and its abstract λx\lambd[x]source. Thus we write 010\, 1source for yxy xsource, and λ.λ.01\lambd[][\lambd[][01]]source for the entire term.

Definition of de Bruijn terms

De Bruijn terms are inductively defines as follows:

  1. nnsource, where nnsource is any natural number.

  2. PQPQsource, where PPsource and QQsource are both De Bruijn terms.

  3. λ.N\lambd[][N]source, where NNsource is a De Bruijn term.

A formalized translation from ordinary lambda terms to De Bruijn indexed terms is as follows:

Translation from named terms to de Bruijn terms

FΓ(x)=Γ(x)FΓ(PQ)=FΓ(P)FΓ(Q)FΓ(λx.N)=λ.Fx,Γ(N)F_\Gamma(x) &= \Gamma(x) \\ F_\Gamma(PQ) &= F_\Gamma(P)F_\Gamma(Q) \\ F_\Gamma(\lambd[x][N]) &= \lambd[][F_{x,\Gamma}(N)]source

where Γ\Gammasource is a list of variables indexed from zero, and Γ(x)\Gamma(x)source denotes the position of the variable xxsource in Γ\Gammasource. For example, if Γ\Gammasource is x,y,zx,y,zsource, then Γ(x)\Gamma(x)source is 00source and Γ(z)\Gamma(z)source is 22source.

x,Γx,\Gammasource denotes the list resulted from pushing xxsource to the head of Γ\Gammasource; for instance, continuing the Γ\Gammasource in last example, w,Γw,\Gammasource is w,x,y,zw,x,y,zsource.

Recovering a standard lambda term from a de Bruijn term is done as follows:

Recovery of named terms from de Bruijn terms

GΓ(n)=Γ[n]GΓ(PQ)=GΓ(P)GΓ(Q)GΓ(λ.N)=λx.Gx,Γ(N)G_\Gamma(n) &= \Gamma[n] \\ G_\Gamma(PQ) &= G_\Gamma(P) G_\Gamma(Q) \\ G_\Gamma(\lambd[][N]) &= \lambd[x][G_{x,\Gamma}(N)]source

where Γ\Gammasource is again a list of variables indexed from zero, and Γ[n]\Gamma[n]source denotes the variable in position nnsource. For example, if Γ\Gammasource is x,y,zx,y,zsource, then Γ[1]\Gamma[1]source is yysource.

The variable xxsource in last equation is chosen to be any variable that not in Γ\Gammasource.

Here we give some results without proving them:

De Bruijn translation is unchanged by alpha change

If MαMM \aconvone M'source, and Γ\Gammasource is any list containing FV(M)\FV{M}source, then FΓ(M)FΓ(M)F_\Gamma(M) \eqs F_\Gamma(M')source.

Source file content/lambda-calculus/syntax/term-revisited.tex

Terms as α\alphasource-Equivalence Classes

From now on, we will consider terms up to α\alphasource-equivalence. That means when we write a term, we mean its α\alphasource-equivalance class it is in. For example, we write λa.λb.ac\lambd[a][\lambd[b][a c]]source for the set of all terms α\alphasource-equivalent to it, such as λa.λb.ac\lambd[a][\lambd[b][a c]]source, λb.λa.bc\lambd[b][\lambd[a][b c]]source, etc.

Also, while in previous sections letters such as N,QN, Qsource are used to denote a term, from now on we use them to denote a class, and it is these classes instead of terms that will be our subjects of study in what follows. Letters such as x,yx, ysource continues to denote a variable.

We also adopt the notation M¯\rep{M}source to denote an arbitrary element of the class MMsource, and M¯0,M¯1,etc.\rep{M}[0], \rep{M}[1], etc.source if we need more than one.

We reuse the notations from terms to simplify our wording. We have following definition on classes:

Abstraction and application on alpha equivalence classes

  1. λx.N\lambd[x][N]source is defined as the class containing λx.N¯\lambd[x][\rep{N}]source.

  2. PQPQsource is defined to be the class containing P¯Q¯\rep{P}\rep{Q}source.

It is not hard to see that they are well defined, because α\alphasource-conversion is compatible.

Free variables of an alpha equivalence class

The free variables of an α\alphasource-equivalence class MMsource, or FV(M)FV(M)source, is defined to be FV(M¯)FV(\rep{M})source.

This is well defined since FV(M¯0)=FV(M¯1)FV(\rep{M}[0]) = FV(\rep{M}[1])source, as shown in the theorem that alpha equivalence preserves free variables.

We also reuse the notation for substition into classes:

Substitution on alpha equivalence classes

The substitution of RRsource for yysource in MMsource, or M[R/y]\Subst{M}{R}{y}source, is defined to be M¯[R¯/y]\Subst{\rep{M}}{\rep{R}}{y}source, for any M¯\rep{M}source and R¯\rep{R}source making the substition defined.

This is also well defined as shown in the corollary on substitution using pairs of representatives.

Note how this definition significantly simplifies our reasoning. For example:

λx.x[y/x]==λz.z[y/x]=λz.z[y/x]=λz.z\Subst{\lambd[x][x]}{y}{x} & =\ollabel{eq:1}\\ &= \Subst{\lambd[z][z]}{y}{x} \ollabel{eq:2}\\ &= \lambd[z][\Subst{z}{y}{x}] \\ &= \lambd[z][z]source

the first labelled equation, substitution into the identity abstraction on x is undefined if we still regard it as substitution on terms; but as mentioned earlier, we now consider it a substitution on classes, which is why the second labelled equation, substitution into the identity abstraction on z can happen: we can replace λx.x\lambd[x][x]source with λz.z\lambd[z][z]source because they belong to the same class.

For the same reason, from now on we will assume that the representatives we choose always satisfy the conditions needed for substitution. For example, when we see λx.N[R/y]\Subst{\lambd[x][N]}{R}{y}source, we will assume the representative λx.N\lambd[x][N]source is chosen so that xyx \neq ysource and xFV(R)x \notin FV(R)source.

Since it is a bit strange to call λx.x\lambd[x][x]source a “class”, let's call them Λ\Lambdasource-terms (or simply “terms” in the rest of the part) from now on, to distinguish them from λ\lambdasource-terms that we are familiar with.

Editorial

We cannot say goodbye to terms yet: the whole definition of Λ\Lambdasource-terms is based on λ\lambdasource-terms, and we haven't provided a method to define functions on Λ\Lambdasource-terms, which means all such functions have to be first defined on λ\lambdasource-terms, and then “projected” to Λ\Lambdasource-terms, as we did for substitutions. However we assume the reader can intuitively understand how we can define functions on Λ\Lambdasource-terms.

Source file content/lambda-calculus/syntax/beta.tex

β\betasource-reduction

When we see (λm.(λy.y)m)(\lambd[m][(\lambd[y][y]) m])source, it is natural to conjecture that it has some connection with λm.m\lambd[m][m]source, namely the second term should be the result of “simplifying” the first. The notion of β\betasource-reduction captures this intuition formally.

Definition of beta contraction

[β\betasource-contraction, β\bredonesource] The β\betasource-contraction (β\bredonesource) is the smallest compatible relation on terms satisfying the following condition:

(λx.N)QβN[Q/x](\lambd[x][N])Q \bredone \Subst{N}{Q}{x}source

We say PPsource is β\betasource-contracted to QQsource if PβQP \bredone Qsource. A term of the form (λx.N)Q(\lambd[x][N])Qsource is called a redex.

Exercise giving inductive beta contraction rules

Spell out the equivalent inductive definitions of β\betasource-contraction as we did for change of bound variable in the inductive definition of one change of bound variable.

Definition of beta reduction

[β\betasource-reduction, β\bredsource] β\betasource-reduction (β\bredsource) is the smallest reflexive, transitive relation on terms containing β\bredonesource. We say PPsource is β\betasource-reduced to QQsource if PβQP \bred Qsource.

We will write \redonesource instead of β\bredonesource, and \redsource instead of β\bredsource when context is clear.

Informally speaking, MβNM \bred Nsource if and only if MMsource can be changed to NNsource by zero or several steps of β\betasource-contraction.

Definition of beta normal term

[β\betasource-normal] A term that cannot be β\betasource-contracted any further is said to be β\betasource-normal.

If MβNM \bred Nsource and NNsource is β\betasource-normal, then we say NNsource is a normal form of MMsource. One may ask if the normal form of a term is unique, and the answer is yes, as we will see later.

Let us consider some examples.

  1. We have

    (λx.xxy)λz.z(λz.z)(λz.z)y(λz.z)yy(\lambd[x][xxy]) \lambd[z][z] & \redone (\lambd[z][z])(\lambd[z][z]) y \\ & \redone (\lambd[z][z]) y \\ & \redone ysource
  2. “Simplifying” a term can actually make it more complex:

    (λx.xxy)(λx.xxy)(λx.xxy)(λx.xxy)y(λx.xxy)(λx.xxy)yy(\lambd[x][xxy])(\lambd[x][xxy]) & \redone (\lambd[x][xxy])(\lambd[x][xxy])y \\ & \redone (\lambd[x][xxy])(\lambd[x][xxy])yy \\ & \redone \dotssource
  3. It can also leave a term unchanged:

    (λx.xx)(λx.xx)(λx.xx)(λx.xx)(\lambd[x][xx])(\lambd[x][xx]) \redone (\lambd[x][xx])(\lambd[x][xx])source
  4. Also, some terms can be reduced in more than one way; for example,

    (λx.(λy.yx)z)v(λy.yv)z(\lambd[x][(\lambd[y][yx]) z]) v \redone (\lambd[y][yv]) zsource

    by contracting the outermost application; and

    (λx.(λy.yx)z)v(λx.zx)v(\lambd[x][(\lambd[y][yx]) z]) v \redone (\lambd[x][zx]) vsource

    by contracting the innermost one. Note, in this case, however, that both terms further reduce to the same term, zvzvsource.

The final outcome in the last example is not a coincidence, but rather illustrates a deep and important property of the lambda calculus, known as the Church--Rosser property.

Digress

In general, there is more than one way to β\betasource-reduce a term, thus many reduction strategies have been invented, among which the most common is the natural strategy. The natural strategy always contracts the left-most redex, where the position of a redex is defined as its starting point in the term. The natural strategy has the useful property that a term can be reduced to a normal form by some strategy iff it can be reduced to normal form using the natural strategy. In what follows we will use the natural stratuegy unless otherwise specified.

Inductive definition of beta equivalence

[β\betasource-equivalence, =\equalsource] β\betasource-Equivalence (=\equalsource) is the relation inductively defined as follows:

  1. M=MM \equal Msource.

  2. If M=NM \equal Nsource, then N=MN \equal Msource.

  3. If M=NM \equal Nsource, N=ON \equal Osource, then M=OM \equal Osource.

  4. If M=NM \equal Nsource, then PM=PNPM \equal PNsource.

  5. If M=NM \equal Nsource, then MQ=NQMQ \equal NQsource.

  6. If M=NM \equal Nsource, then λx.M=λx.N\lambd[x][M] \equal \lambd[x][N]source.

  7. (λx.N)Q=N[Q/x](\lambd[x][N])Q \equal \Subst{N}{Q}{x}source.

The first three rules make the relation an equivalence relation; the next three make it compatible; the last ensures that it contains β\betasource-contraction.

Informally speaking, two terms are β\betasource-equivalent if and only if one of them can be changed to the other in zero or more steps of β\betasource-contraction, or “inverse” of β\betasource-contraction. The inverse of β\betasource-contraction is defined so that MMsource inverse-β\betasource-contracts to NNsource iff NNsource β\betasource-contracts to MMsource.

Besides the above rules, we will extend the relation with more rules, and denote the extended equivalence relation as =X\equal[X]source, where XXsource is the extending rule.

Source file content/lambda-calculus/syntax/eta.tex

η\etasource-conversion

There is another relation on λ\lambdasource terms. In the section Free Variables we used the example λx.(fx)\lambd[x][(fx)]source, which accepts an argument and applies ffsource to it. In other words, it is the same function as ffsource: λx.(fx)N\lambd[x][(fx)]Nsource and fNfNsource both reduce to fNfNsource. We use η\etasource-reduction (and η\etasource-extension) to capture this idea.

Definition of eta contraction

[η\etasource-contraction, η\eredonesource] η\etasource-contraction (η\eredonesource) is the smallest compatible relation on terms satisfying the following condition:

λx.MxηM provided xFV(M)\lambd[x][M x] \eredone M \text{ provided } x \notin FV(M)source

Definition of beta eta reduction

[βη\beta\etasource-reduction, βη\beredsource] βη\beta\etasource-reduction (βη\beredsource) is the smallest reflexive, transitive relation on terms containing β\bredonesource and η\eredonesource, i.e., the rules of reflexivity and transitive plus the following two rules:

  1. If MβNM \bredone Nsource then MβηNM \bered Nsource.

  2. If MηNM \eredone Nsource then MβηNM \bered Nsource.

Extending equivalence by eta conversion

We extend the equivalence relation =\equalsource with the η\etasource-conversion rule:

λx.fx=f\lambd[x][f x] \equal fsource

and denote the extended relation as =η\equal[\eta]source.

η\etasource-equivalence is important because it is related to extensionality of lambda terms:

Extensionality rule

[Extensionality] We extend the equivalence relation =\equalsource with the (ext) rule:

If Mx=NxMx \equal Nxsource then M=NM \equal Nsource, provided xFV(MN)x \notin FV(MN)source.

and denote the extended relation as =ext\equal[\ext]source.

Roughly speaking, the rule states that two terms, viewed as functions, should be considered equal if they behave the same for the same argument.

We now prove that the η\etasource rule provides exactly the extensionality, and nothing else.

Equivalence of eta and extensionality extensions

M=extNM \equal[\ext] Nsource if and only if M=ηNM \equal[\eta] Nsource.

Proof

First we prove that =η\equal[\eta]source is closed under the extensionality rule. That is, extextsource rule doesn't add anything to =η\equal[\eta]source. We then have =η\equal[\eta]source contains =ext\equal[\ext]source, and if M=extNM \equal[\ext] Nsource, then M=ηNM \equal[\eta] Nsource.

To prove =η\equal[\eta]source is closed under ext, note that for any M=NM \equal Nsource derived by the ext rule, we have Mx=ηNxMx \equal[\eta] Nxsource as premise. Then we have λx.Mx=ηλx.Nx\lambd[x][Mx] \equal[\eta] \lambd[x][Nx]source by a rule of =\equalsource, applying η\etasource on both side gives us M=ηNM \equal[\eta] Nsource.

Similarly we prove that the η\etasource rule is contained in =ext\equal[ext]source. For any λx.Mx\lambd[x][Mx]source and MMsource with xFV(M)x \notin FV(M)source, we have that (λx.Mx)x=extMx(\lambd[x][Mx])x \equal[\ext] Mxsource, giving us λx.Mx=extM\lambd[x][Mx] \equal[\ext] Msource by the ext rule.

Source disclosures