1 / 26

Lambda Calculus

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.

milly
Télécharger la présentation

Lambda Calculus

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lambda Calculus Programming Language Principles Lecture 11 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida

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

  3. First, need some theory. RPAL’s flattener grammar: use bracket tree notation: <‘root’ s1 … sn> RPAL → E E → <'' E E > => E E → <'' V E > => '' V '.' E → <id:x> => x → <integer:i> => i → <string:s> => s → 'true' => 'true' → 'false' => 'false' . . . end RPAL.

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

  5. Disambiguation Rules • Function application is left associative. • 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).

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

  7. Definition • Let M and N be-expressions. Anoccurrence of x in a-expression is freeif it can be proved so via the following three rules: • The occurrence of x in-expression "x" is free. • Any free occurrence of x in either M or N is free in M N. • Any free occurrence of x in M is freeiny.M, if x and y are different.

  8. Practical, Equivalent Definition • An occurrence of x in M is free if thepath 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 boundif it is not free in M.

  9. Examples: a - a occurs free x - x occurs free a 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.

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

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

  12. Axiom Alpha Let x and y be names, and M be an AE with no free occurrences of y. Then, in anycontext, 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

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

  14. 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: • If M is an identifier, then 1.1. if M=x, then return N. 1.2.if M is not x, then return M. • If M is of the form X Y, then return ([N/x]X) ([N/x]Y).

  15. Definition (subst[N,x,M], cont’d) • If M is of the form y.Y, then 3.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.

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

  17. Definition • An AE M is said to be "directly convertible“ to an AE N, denotedM => N, if one of these three holds: • M => N, M => N, M => N. • Definition: • Two AE's M and N are said to beequivalent if M =>* N.

  18. Definition • An AE M is in normal form if either • M does not contain any ’s, or • 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.

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

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

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

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

  23. 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 !

  24. Theorem (Church-Rosser) • All sequences of reductions on an AE that terminate, do so on congruent AE's. • If there exists a sequence of reductions on an AE that terminates, then reduction in normal order also terminates.

  25. Conclusions • Some AE's can be reduced to normal form, but some cannot. • Still stuck with the classic halting problem. • If an AE can be reduced to normal form, then that normal form is unique to within a choice of bound variables. • If a normal form exists, a reduction to (some) normal form can be obtained in a finite number of steps using normal order.

  26. Lambda Calculus Programming Language Principles Lecture 11 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida

More Related