Lambda Calculus - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Lambda Calculus

Description:

We say that M and N are delta-convertible, pronounced 'M delta reduces to N' ... An AE M is said to be 'directly convertible' to an AE N, denoted ... – PowerPoint PPT presentation

Number of Views:148
Avg rating:3.0/5.0
Slides: 27
Provided by: manuelb9
Category:

less

Transcript and Presenter's Notes

Title: Lambda Calculus


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.
  • RPALs flattener grammar use bracket tree
    notation ltroot s1 sngt
  • RPAL ? E
  • E ? lt'?' E E gt gt E E
  • ? lt'?' V E gt gt '?' V '.' E
  • ? ltidxgt gt x
  • ? ltintegerigt gt i
  • ? ltstringsgt gt s
  • ? 'true' gt 'true'
  • ? 'false' gt '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
  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).

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. An occurrence of x
    in a ?-expression is free if 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 free in ?y.M, if
    x and y are different.

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

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.xy) 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 gt? 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 gt? 8.

12
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
    gt? ? y.substy,x,M
  • substy,x,M means "substitute y for x in M".
  • Axiom Alpha used to rename the bound variable.
  • Example ?x.x3 gt? ? y.y3

13
Axiom Beta
  • Let x be a name, and M and N be AE's. Then, in
    any context, (?x.M) N gt? substN,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 substN,x,M, also denoted as N/xM,
    means
  • If M is an identifier, then
  • 1.1. if Mx, then return N.
  • 1.2. if M is not x, then return M.
  • If M is of the form X Y, then return (N/xX)
    (N/xY).

15
Definition (substN,x,M, contd)
  • If M is of the form ? y.Y, then
  • 3.1. if yx 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/xY.
  • 3.2.3. if x occurs free in Y,
  • and y occurs free in N, then
  • return ?w.N/x (w/yY),
  • 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, denoted M gt N, if one of these three
    holds
  • M gt? N, M gt? N, M gt? N.
  • Definition
  • Two AE's M and N are said to be equivalent if M
    gt 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 gt E1 ... gt 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 ltgt? 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 gt?
    substN,x,M, N is normal form, unless N is a
    ?-expression.

21
Examples
  • Normal Order
  • (? y.y)(? x.x3)2 gt? (? x.x3) 2
  • gt? (23)
  • gt? 5
  • PL order
  • (? y.y)(? x.x3)2 gt? (? y.y)(23)
  • gt? (? y.y)5
  • gt? 5

22
PL Order faster than Normal Order
  • PL Order
  • (?x.xxx) ((?y.y1)2)
  • gt? (?x.xxx) 3
  • gt? 333 Cost 2 ?s.
  • Normal Order
  • (?x.xxx) ((?y.y1)2)
  • gt? ((?y.y1)2) ((?y.y1)2)
    ((?y.y1)2)
  • gt?, ? , ? 333
  • Cost 4 ?s.

23
PL Order riskier than Normal Order
  • Normal Order
  • (?x.1) ( (?x.x x) (?x.x x) )
  • gt? 1
  • Terminates.
  • PL Order
  • (?x.1) ( (?x.x x) (?x.x x) )
  • gt? (?x.1) ( (?x.x x) (?x.x x) )
  • gt? (?x.1) ( (?x.x x) (?x.x x) )
  • . . .
  • Doesnt terminate !

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

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
Write a Comment
User Comments (0)
About PowerShow.com