Lambda Calculus

Post on 06-Feb-2016

51 views 0 download

Tags:

description

Lambda Calculus. Programming Language Principles Lecture 11. Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida. Lambda Calculus. To obtain the "value" of an RPAL program: 1.Transduce RPAL source to an AST. 2.Standardize the AST into ST. - PowerPoint PPT Presentation

Transcript of Lambda Calculus

Lambda Calculus

Prepared by

Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida

Programming Language PrinciplesLecture 11

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 lambda-

expression.4. Evaluate the lambda-expression, using

the CSE machine (more later)

First, need some theory.

RPAL’s flattener grammar: use bracket tree notation: <‘root’ s1 … sn>

RPAL → EE → <'' E E > => E E

→ <'' V E > => '' V '.' E→ <id:x> => x

→ <integer:i>=> i→ <string:s> => s→ 'true' => 'true'→ 'false' => 'false'

. . .end RPAL.

Result of Tree Flattening: a string

• An Applicative Expression (AE), or well-formed formula (WFF).

• The interpretation of the AE is ambiguous.

• Need parentheses to disambiguate.

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.

Practical, Equivalent Definition

• An occurrence of x in M is free if the path from x to the root of M includes no node with x on its left.

• Definition:• An occurrence of x in a -expression

M is said to be bound if it is not free in M.

Examples:

a - a occurs freex - x occurs freea x - a and x both occur free( x.ax)x - a occurs free;

x occurs both free and bound( x. y.x+y) y 3

- first occurrence of y is not free,second occurrence is free,in the entire expression.

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).

We say that M and N are delta-convertible, pronounced “M delta reduces to N”.

Val: Value obtained from ordinary evaluation.

•Example: +35 => 8.

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, then1.1. if M=x, then return N.1.2. if M is not x, then return M.

2. If M is of the form X Y, then return ([N/x]X) ([N/x]Y).

Definition (subst[N,x,M], cont’d)

3. If M is of the form y.Y, then3.1. if y=x then return y.Y.3.2. if y is not x then

3.2.1. if x does not occur free in Y, then return y.Y.3.2.2. if y does not occur free in N,

then return y.[N/x]Y.3.2.3. if x occurs free in Y, and y occurs free in N, then return w.[N/x] ([w/y]Y), for any w that does not occur free in either N or Y.

Examples

• [3/x]( x.+x2) = x.+x2 (by 3.1)• [3/x]( y.y) = y.y (by 3.2.1)• [3/x]( y.+xy) = y.[3/x](+xy) = y.+3y (by 3.2.2 and

2)• [y/x]( y.+xy) = z.[y/x]([z/y](+xy)) = z.[y/x](+xz) = z.+yz (by 3.2.3, 2,

and 2)

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, or2. 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 “faster” than Normal Order

• PL Order: (x.x+x+x) ((y.y+1)2)

=> (x.x+x+x) 3

=> 3+3+3 Cost: 2 ’s.

• Normal Order: (x.x+x+x) ((y.y+1)2)

=> ((y.y+1)2) + ((y.y+1)2) + ((y.y+1)2) =>, , 3+3+3

Cost: 4 ’s.

PL Order “riskier” than Normal Order

Normal Order: (x.1) ( (x.x x) (x.x x) )

=> 1 Terminates.

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) ) . . .

Doesn’t 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

Prepared by

Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida

Programming Language PrinciplesLecture 11