Bound and free variables

In this chapter we look at some fairly trivial things, but they will be very useful in the material that follows. Let us introduce the notion of free variables, which we denote FVFV. Here are a few examples of applying this notion to all the terms we already know.

FV(x)\displaystyle \mathrm{FV}(x) ={x},FV(λx.M)\displaystyle = \{x\}, \qquad \mathrm{FV}(\lambda x.\,M) =FV(M){x},FV(MN)\displaystyle = \mathrm{FV}(M)\setminus\{x\}, \qquad \mathrm{FV}(M\,N) =FV(M)FV(N)\displaystyle = \mathrm{FV}(M)\cup\mathrm{FV}(N)
(1.2)

A single variable xx has xx itself free. In an abstraction λx.M\lambda x.\,M the variable xx is bound by the nearest lambda, so it must be removed from the free variables of the body MM. In an application MNM\,N the free variables are taken from both parts: everything free in MM plus everything free in NN. A term with no free variables is called closed, or a combinator; note that one and the same letter may occur both free and bound, for example, in x(λx.x)x\,(\lambda x.\,x).

Recall that a term is a tree, and variables are its leaves. Now, into those leaves one can in turn substitute subtrees, that is, terms. This operation is called substitution. Here are a few examples of such substitutions:

x[x:=N]\displaystyle x[x{:=}N] =N,y[x:=N]\displaystyle = N, \qquad y[x{:=}N] =y    (yx)\displaystyle = y \;\; (y \neq x)
(1.3)
(M1M2)[x:=N]\displaystyle (M_1\,M_2)[x{:=}N] =M1[x:=N]  M2[x:=N]\displaystyle = M_1[x{:=}N]\;M_2[x{:=}N]
(1.4)
(λy.M)[x:=N]\displaystyle (\lambda y.\,M)[x{:=}N] =λy.M[x:=N],y\displaystyle = \lambda y.\,M[x{:=}N], \qquad y x,  y\displaystyle \neq x,\; y FV(N)\displaystyle \notin \mathrm{FV}(N)
(1.5)

As you can see, some restrictions have already appeared. But why? There is an important subtlety here. For instance, if the condition yy FV(N)\notin \mathrm{FV}(N) is violated, a free variable gets “captured” by a foreign abstraction:

(λy.x)[x:=y]\displaystyle (\lambda y.\,x)[x{:=}y] λy.y— the meaning has changed!\displaystyle \neq \lambda y.\,y \quad\text{— the meaning has changed!}

The cure is α-conversion, but more on that a little later.