8-1 Theory of Computation Chapter 8 Lambda-Calculus Introduction to -calculus The -calculus is a purely syntactic notation, consisting of Well-Formed Formulae (WFFs, or -expressions), defined by formal rules; and a set of Conversion Rules which enable one WFF to be converted to another. We regard two WFFs as being equivalent if one can be converted into the other by repeated use of one or more of the conversion rules. Although purely syntactic, it can be shown that the -calculus can be used to model the computable functions, i.e. it is equivalent to Turing Machines or the General Recursive Functions. This diagram commutes, that is, it doesn't matter whether a -expression is interpreted and then functional computation is used, or whether the conversion rules are applied first and then the -expression is interpreted. We shall return later to the idea of interpreting (or giving meaning to) -expressions; first we consider the -calculus as a purely syntactic system. Definition of -calculus A Well-Formed Formula (WFF) of the -calculus (also called a -expression) is one of the following: 1. A variable (a lower-case letter) e.g. x 2. The application (MN) of two WFFs M and N. When interpreted, this will be regarded as a function M applied to an argument N. 3. The abstraction ( x. M) of a WFF M where x is a variable. When interpreted, this will be regarded as a function with one formal parameter called x. The value returned by the function is M (which will probably involve x). Note Integers and arithmetic (+, - etc.) are not included in the -calculus, but they can be modelled (see later). Parentheses can often be omitted without ambiguity, in which case the following rules apply: 1. Application associates to the left, i.e. KMN – (K M) N 8-1 8-2 Theory of Computation 2. The "scope" of an abstraction is taken to extend as far as possible, consistent with parentheses (contrast this with the scope of a quantifier in predicate calculus, which is taken to be as small as possible). For example, ( x. ( y. (M N) ) ) could be written as ( x. ( y. M N ) ) or even as x. y. M N Free and Bound Variables An occurrence of variable x is said to be bound if it is in an abstraction of the form x. M, and free otherwise. Examples 1. ax 2. ( x.ax) 3. ( x.ax)x Both a and x occur free. Both occurrences of x are bound, The first two occurrences of x are a still occurs free. bound, the third is free. Substitution When representing function calls, it can be necessary to replace all the free occurrences of a given variable with a -expression. This corresponds to replacing a formal parameter with an actual parameter in programming. The notation used here is ([M/x] X). ([M/x] X) x in X. which means the expression formed when M replaces free occurrences of E.g. ([a/x] (y.x) ) – (y.a) 1. ( x . xz) a [a/x] xz converts to = az, 2. ( x . xx) a [a/x] xx converts to = aa, 3. ( x . xz) ( y . yz) converts to [( y . yz)/x] xz = ( y . yz) z, which converts to [z/y] yz = zz 8-2 8-3 Theory of Computation Evaluate ( x. y. yx) y z (which is ( ( x.( y. yx)) y) z First, evaluate ( x.( y. yx)) y Substituting y for x in y. yx seems to give ( y. yy), so the answer seems to be ( y. yy) z = zz whereas the correct result is of the form ( u. uy) z = zy The bound occurrences of y in y. yx must not be confused with the free occurrence of y in yz. Remedy: Rewrite y.yx as u.ux before the substitution is made. For, y.yx and u.ux are the same, up to the renaming of bound variables. Such name clashes may be prevented by the use of substitution rules. Conversion Rules We now give names to the rules which we have used to manipulate -expressions. -conversion (alpha-conversion) If y is not free in X then x.X cnv y.[y/x] X (Renaming of bound variables) -conversion (beta-conversion) ( x.M )N (Evaluation) cnv [N/x] M -conversion (eta-conversion) If x is not free in M, then (x.Mx) cnv M 8-3 8-4 Theory of Computation Notes: 1. -conversion permits the name of a bound variable to be changed, provided that no name clash is introduced. Compare with programming, where the name of a formal parameter can be changed provided that the new name does not clash with local or global variables. 2. Compare -conversion with the substitution of actual parameters for formal parameters in programs. 3. All rules are reversible. Each rules states that when one of the expressions (left or right) occurs, it may be replaced by the other. 4. -conversion and -conversion permit the elimination of abstractions. These are called the reduction rules, or reductions. We say that one expression may be reduced to another, and write, for example, A red B . We write A red B to mean that A may be reduced to B by a series of reduction (and possible some -conversions). A red B A cnv B An expression that can be reduced is called a redex. This for any WFFs M and N, (x.M) N is a -redex (x.Mx) is an -redex if x is not free in M We write (x.M) N red [N/x] M (x.Mx) red M An expression which contains no redexes is said to be in normal form. An expression which contains no redexes is said to be in normal form. If M red N and N is in normal form, then N is said to be the normal form of M, and may be interpreted to be the "value" of M. 8-4 8-5 Theory of Computation Examples 1. (x.y.y)ab = = (x.(y.y))ab ((x.(y.y))a)b the left) red (y.y) b red b 2. (f.x.f(fx))ab red (x.a(ax)) b red a(ab) 3. (x.xx)(x.xx) (Application binds to (x.xx) has the form (x.Mx), but is not -reducible since x occurs free in M An attempt at -reduction yields (x.xx)(x.xx) red (x.xx)(x.xx) So this expression has no normal form. Some expressions can not be reduced to normal form. Church-Rosser Theorems These theorems show that this is not possible, i.e. if two reduction sequences for the same expression both yield a normal form, then they yield the same normal form (up to renaming of bound variables). Arithmetic using the -calculus It is possible to interpret the -calculus, i.e. to assign meaning to certain -expressions. We shall consider how to represent the non-negative integers. Represent zero by – f.x.x and the successor function by succ – k.f.x.f(kfx) 8-5 8-6 Theory of Computation – succ 0 –(k.f.x.f(kfx)) (f.x.x) cnv f.x.f( (f.x.x) fx) 1 cnv cnv f.x.f( (x.x) x) f.x.fx 2– succ1 3– succ 2 cnv cnv f.x.f(fx) f.x.f(f(fx)) etc... 8-6