next up previous contents
Next: Encoding arithmetic Up: The (untyped) lambda calculus Previous: The rule   Contents

Variable capture

An important issue to keep in mind when applying the rule $\beta $ is that the substitution for $x$ should not ``capture'' variables. Consider, for instance, the application $ (\lambda x.(\lambda y.x y))
y. $ Using the $\beta $ rule naively, we get $ (\lambda x.(\lambda y.x
y)) y \rightarrow _\beta \lambda y.y y $ However, the $y$ that comes in to $\lambda y.x y$ by the substitution for $x$ is a new $y$ and should not be confused with the $y$ that is used to define the inner function. Thus, we should redefine the inner function with a fresh variable before making the substitution, as follows:


\begin{displaymath}
(\lambda x.(\lambda y.x y)) y = (\lambda x. (\lambda z.x z)) y
\rightarrow _\beta \lambda z.y z
\end{displaymath}

Clearly, changing the name of the variable defining a function does not affect the definition--there is no difference between the functions $f(x) = 2x+5$ and $f(z) = 2z+5$.

To formalize this notion we have to define free and bound variables. Intuitively, a variable is free in an expression until it is ``bound'' by an abstraction. Thus, in the expression $x y$, both $x$ and $y$ are free. If we abstract $y$ as $\lambda y.x y$, $x$ remains free but $y$ becomes bound. More formally, we can define two sets $FV(M)$ (free variables of $M$) and $BV(M)$ (bound variables of $M$) inductively, as follows:

  • $FV(x) = \{x\}$, for any variable $x$
  • $FV(\lambda x.M) = FV(M) - \{x\}$
  • $FV(M M') = FV(M) \cup FV(M')$
  • $BV(x) = \emptyset$, for any variable $x$
  • $BV(\lambda x.M) = BV(M) \cup \{x\}$
  • $BV(M M') = BV(M) \cup BV(M')$

When we apply the $\beta $ rule to transform $\lambda x.M M'$ into $M\{x \leftarrow M'\}$, we need to check that no free variable in $M'$ clashes with a bound variable in $M$. In the example above,


\begin{displaymath}
(\lambda x.(\lambda y.x y)) y \rightarrow _\beta \lambda y.y y,
\end{displaymath}

$y$ is bound in the expression $M=\lambda y.x y$, while $y$ is free in the expression $M' = y$.

In the literature, several approaches have been proposed to deal with the variable renaming problem that is required for the $\beta $ rule to work properly. For simplicity, we shall just assume that we always rename the bound variables in $M$ to avoid ``capturing'' free variables from $M'$.


next up previous contents
Next: Encoding arithmetic Up: The (untyped) lambda calculus Previous: The rule   Contents
Madhavan Mukund 2004-04-29