Lambda Calculus: Programming Language Principles
Lambda Calculus: Programming Language Principles
Prepared by
Lambda Calculus
To obtain the "value" of an RPAL program: 1. Transduce RPAL source to an AST. 2. Standardize the AST into ST. 3. Linearize the ST into a lambdaexpression. 4. Evaluate the lambda-expression, using the CSE machine (more later)
end RPAL.
Disambiguation Rules
1. Function application is left associative. 2. If an expression of the form x.M occurs in a larger expression, then M is extended as far as possible (i.e. to the end of the entire expression or to the next unmatched right parenthesis).
Example:
x. y.+xy 2 3 is equivalent to x.( y.+xy 2 3) Must be parenthesized to obtain the intended expression: ( x. y.+xy) 2 3.
Definition
Let M and N be -expressions. An occurrence of x in a -expression is free if it can be proved so via the following three rules: 1. The occurrence of x in -expression "x" is free. 2. Any free occurrence of x in either M or N is free in M N. 3. Any free occurrence of x in M is free in y.M, if x and y are different.
Examples:
a x ax ( x.ax)x a occurs free x occurs free a and x both occur free a occurs free; x occurs both free and bound first occurrence of y is not free, second occurrence is free, in the entire expression.
( x. y.x+y) y 3 -
More definitions
Definition: In an expression of the form x.M, x is the bound variable, and M is the body. Definition: The scope of an identifier x, in an expression of the form x.M, consists of all free occurrences of x in M.
Axiom Delta
Let M and N be AE's that do not contain -expressions. Then M => N if Val(M) = Val(N).
Axiom Alpha
Let x and y be names, and M be an AE with no free occurrences of y. Then, in any context, x.M => y.subst[y,x,M] subst[y,x,M] means "substitute y for x in M". Axiom Alpha used to rename the bound variable. Example: x.+x3 => y.+y3
Axiom Beta
Let x be a name, and M and N be AE's. Then, in any context, (x.M) N => subst[N,x,M]. Called a "beta-reduction", used to apply a function to its argument. Pronounced beta reduces to. Need to define the subst function.
Definition (subst):
Let M and N be AE's, and x be a name. Then subst[N,x,M], also denoted as [N/x]M, means: 1. If M is an identifier, then 1.1. if M=x, then return N. 1.2. if M is not x, then return M.
Examples
[3/x]( x.+x2) [3/x]( y.y) [3/x]( y.+xy) = x.+x2 (by 3.1) = y.y (by 3.2.1) = y.[3/x](+xy) = y.+3y (by 3.2.2 and 2) = z.[y/x]([z/y](+xy)) = z.[y/x](+xz) = z.+yz (by 3.2.3, 2, and 2)
[y/x]( y.+xy)
Definition
An AE M is said to be "directly convertible to an AE N, denoted M => N, if one of these three holds: M => N, M => N, M => N. Definition: Two AE's M and N are said to be equivalent if M =>* N.
Definition
An AE M is in normal form if either
1. M does not contain any s, or 2. M contains at least one , and 2.1. M is of the form X Y, X and Y are in normal form, and X is not a -expression. 2.2. M is of the form x.N, and N is in normal form.
Shorter version: M is in normal form if no beta-reductions apply.
Definition
Given an AE M, a reduction sequence on M is a finite sequence of AE's E0 , E1 , ..., En, such that M = E0 => E1 ... => En.
Definition:
A reduction sequence is said to terminate if its last AE is in normal form.
Definitions
Two AE's M and N are be congruent if M <=>* N. A reduction sequence is in normal order if in each reduction, the left-most is reduced. A reduction sequence is PL order if for each beta-reduction of the form ( x.M) N => subst[N,x,M], N is normal form, unless N is a -expression.
Examples
Normal Order: ( y.y)[( x.+x3)2] => ( x.+x3) 2 => (+23) => 5 PL order: ( y.y)[( x.+x3)2] => ( y.y)(+23) => ( y.y)5 => 5
PL Order: (x.1) ( (x.x x) (x.x x) ) => (x.1) ( (x.x x) (x.x x) ) => (x.1) ( (x.x x) (x.x x) ) ... Doesnt terminate !
Theorem (Church-Rosser)
1. All sequences of reductions on an AE that terminate, do so on congruent AE's. 2. If there exists a sequence of reductions on an AE that terminates, then reduction in normal order also terminates.
Conclusions
1. Some AE's can be reduced to normal form, but some cannot. Still stuck with the classic halting problem. 2. If an AE can be reduced to normal form, then that normal form is unique to within a choice of bound variables. 3. If a normal form exists, a reduction to (some) normal form can be obtained in a finite number of steps using normal order.
Lambda Calculus
Programming Language Principles Lecture 11
Prepared by