Computer Science 591i
Substitution, Free and Bound Variables


Errata

dont we need a requirement for S7 that w/=v - and consequent adjustment of proofs along the line?

We need to distinguish between conversion at the top level and conversion at all levels. RA below...

in various places we forget to use our definition of height.

RA1  E<->E
RA2  (E F) <-> (E' F')  if E <-> E' and F <-> F'
RA3  \x. E <-> \x E'  if E <-> E'

we need a lemma about E[x:=x] not changing height.


In the last lecture we introduced the -calculus as providing not only theoretical characterisation of computation but as being the basis a rigorous characterisation of a class of programming languages called functional programming languages. It was, we alleged, the case that it is possible to verify programs written in such languages in a simple way related to ordinary mathematical proof.

We can make our understanding of the role of the -calculus more precise as follows:

Thus, from the -calculus perspective, there is no essential distinction between program and data - it is only a matter of point of view.

This means in particular that if we want to prove that a program is correct, then we must prove that the corresponding -calculus expression has a particular property.

In this lecture we will develop these ideas further.

For example, suppose we want to prove that a sorting function that we have written, say merge_sort, is correct. Then, within the rigorous functional-paradigm, merge_sort should be exactly equivalent to an expression of the -calculus. What does it mean to say that merge_sort is correct? There are two criteria:


In order to understand how to implement software that can reliably support proof of facts like these we need to characterise exactly many important aspects of the -calculus. Let's start by defining -reduction so precisely that we can write a program to implement it.

Conversion, Reduction and Abstraction

Previously we defined -reduction in terms of substituting an argument given to an abstraction for the variable of the abstraction in the body of the abstraction, or, symbolically, So, for example Now we'd like to think of reduction as leaving the expression ( x.E1)E2 unchanged (or in other words, the reduced form "means the same thing" as the original. Indeed, in the example above, we would like to claim that the expression (sorted (merge_sort l)) means the same thing as the expression true. So, we are interested in the inverse operation to reduction, which we'll call abstraction. We'll write the arrow the other way round to indicate abstraction. Finally, if we consider transformation in both directions, we'll refer to this as -conversion. For example: Or we could just as well write: Since we'll meet other reduction and conversion rules, if we need to emphasise that the -rule is involved we'll subscript the arrow with a .

Bound and Free variables

When we defined -reduction, we were not very precise about what we meant by substitution. Now is the time to remedy this,

First we need to define the idea of a bound variable. This is essential to giving a precise sense to the scope of a variable, and thereby being able to avoid confusing what are essentially two different variables which happen to have the same name.


Bound and Free Occurences of a Variable

An expression E1 is said to occur in an expression E2 if it is a sub-expression of E2. There can be several occurrences of E1 in E2. Note that if E1 is a variable v, say, then if v is the variable bound in a -abstraction, this does not constitute an occurrences. For example v does not occur in ( v. 2), while x has two occurrences in (+ x x).

Let E be an expression of the -calculus, and let v be a variable. We say that an occurrence of v in E is bound if it is inside a sub-expression of E of the form v.E2. An occurrence is said to be free otherwise. Thus v occurs bound in v. x v and in (y v. v) but it occurs free in x. v x.

Note that we are speaking of an occurrence of a variable as being bound - a variable can occur both bound and free in a given expression. For example, in v v. v, the first occurrence of v is free, and the last is bound.

The FV function finds the free variables of an expression

In order to define substitution we will need to be able to operate on the set of free variables of an expression.

We can define a function FV which forms the free-variables of an expression as:

FV1
FV(v) = {v} for a variable v
FV2
FV(c) = {} for a constant c
FV3
FV(E1E2) = FV(E1) FV(E2)
FV4
FV( v .E) = FV(E) - {v}

Here FV1 says that the set of free variables of an expression that consists of a single variable is the set consisting of just that variable, while FV2 says that the set of free variables of a constant expression is empty. FV3 says that the free variables of an application E1E2 is the union of the free variables of the two expressions E1 and E2, while FV4 says that the set of free variables of a -abstraction is the set of free variables of its body minus the variable bound by the abstraction.

For example FV (' x. (f x y)') = {f,y}

An expression E is said to be closed if it has no free variables, i.e. FV(E) = {}.

-conversion

It is clear that the variable used in a -abstraction ought to be regarded as arbitrary. Thus x. + x 2 and y. + y 2 are, intuitively, the same function.

There is indeed a rule of the calculus, called -conversion, which allows the above to expressions to be treated as equivalent. It is a little tricky however, since one does not want to convert x. y x to y. y y - the rule is that we may only replace the variable bound in a -abstraction by one that does not occur free in the body. The conversion rule is thus:

provided

The Height of a Term.

We can regard a term of the -calculus as being a tree, we define the notion of the height of a term as:

The Pitfalls of Substitution

Now let's return to the problem of defining substitution.

Consider for example:

clearly we shouldn't substitute 3 for x in the inner -abstraction, since that is essentially a different x - the outer x is out-of-scope in the inner -abstraction. So our beta-reduction is On the other hand if we have an inner--abstraction whose variable is different it is correct to substitute inside the inner -abstraction, obtaining:

So we have to make sure that, as we explore the expression we are substituting inside, if we encounter an inner -abstraction that has the same variable as the one we're replacing, that we don't continue substituting inside the expression. This is embodied in rule S5 below.

However, there is yet another pitfall for the naive implementor of -reduction. Suppose we want to reduce and one of the free variables of E2 is the bound variable of a -abstraction inside E1, then we are facing a problem called variable capture. The problem is exemplified in

which does not reduce to Instead, we need to perform an -conversion on the inner -expression so that the bound-variable does not occur free in the argument of the beta-redex. and now we can go ahead, obtaining ( z. + (+ y 3) z)

The definition of substitution

Substitution, forming E1[v:=E2], `E1 with E2 substituted for v' is a straightforward matter of rewriting sub-terms except when we come to a -abstraction which binds the variable v or a variable free in E2. We can define it by cases, using v,u for variables, E,E1... for expressions, with C for the set of constants:

Comments on S1-S7

Firstly let's note that S6 does follows the definition given in Hindley & Seldin in requiring v FV(E1)
. Alternatively, we can leave this condition out, and require in addition that x w in S6.

Cases S1-S4 need no comment. In case S5 the variable we are substituting for is rebound in a -abstraction. Thus, inside the expression it no longer `means' the same thing - in some sense it is actually a different variable, so we should not substitute for it. In case S6, the -abstraction introduces a new variable u, but, since it does not occur in E there is no problem of confusing it with any variable occurring in E.

However in case S7 there is a real problem - the new variable u introduced in the -abstraction is the same as a variable occurring free in E. The solution is to perform an -conversion, replacing it throughout the -abstraction by a variable w that does not occur. We can always choose a w for S7 because we have an infinite supply of variables to choose from (and any -calculus expression only contains finitely many).


We now have our first opportunity to prove a result relevant to developing a theory of equality in the -calculus.

Lemma Sub.0

Lemma Sub.1

[A] If x FV(E) then

[B] If x FV(E) then

Proof

We proceed by induction on n, the height of E.

Base Case n=0


Inductive Step

Suppose for a given n we have for any term E for which height(E) n

Consider an expression E of height n+1 We must show our result holds for E

.

Lemma Sub.2

E[x:=x] E

Lemma Sub.3

Let E1 and E2 be expressions of the -calculus Let x be a variable, and let y FV(E1) be a variable. Then E1[x:=E2] = (E1[x:=y])[y:=E2]

Proof.

If y=x then using Lemma Sub.2

Otherwise, if x y we proceed by induction on the height of the expression being substituted in.

Base Case Let E1 be an expression of height 0. Then we have the following cases:

Inductive step:

Suppose for some n, for all i n

Consider an expression E of height n+1. We must show that our result holds for E. E can be formed by application (case S4) of the definition of substitution, or by -abstraction (cases S5-S7).

-reduction

We observed early on that the meaning of

and the meaning of (+ 5) were both the function "add 5". This observation can be generalised and encoded as a reduction rule.

For the pure -calculus, the rule can be stated:

In an impure -calculus, it is customary to restrict the -rule to the case in which E denotes a function.

-reduction and laws

When we originally defined the -calculus we specified that it could (and indeed would) contain constants, including the natural numbers 0,1,2,3.... However, we haven't said how to do anything with these constants. For example, we can form the expression (+ 2 3), but have no rules for reducing it further. Since being able to perform arithmetic is a capability built-in to all real computers it makes sense to extend the - calculus to allow expressions which correspond to standard computations that are built-in to computers to be performed. It is the role of -reduction to do this. we have to admit -reduction, and its inverse, into our rules for equality, together with the laws of the algebra(s) to which the constants belong. Thus

A Warning

It is quite easy to play fast and loose with modifications and extensions of the -calculus. However it should be stressed that doing this can change the mathematical properties. For example, a simple change in the definition of substitution (as discussed above with respect to S6 and S7) can invalidate all the proofs, which, you will observe, are quite long. In particular, it's necessary to maintain a firm distinction between the pure-calculus (with or without -reduction) and the calculus with -reduction.

Bibliography

J.Roger Hindley & Jonathan P.Seldin [196] "Introduction to Combinators and -Calculus" Cambridge University Press.