Docsity

Lambda Calculus: Foundations of Functional Programming - Prof. Laura Dillon, Papers of Programming Languages

Toggle sidebar
bg1
L. Dillon, CSE 452, Fall 2008 1
Original intention•
Study foundations of mathematics (Church & Kleene) −
More successful for computable functions•
Substitution --> symbolic computation −
Church/Turing thesis −
Influenced design of Lisp, ML, other languages•
See Boost Lambda Library for C++ function objects −
Important part of CS history and foundations•
Foundations: Lambda calculus
L. Dillon, CSE 452, Fall 2008 2
Formal system with three parts•
Notation for function expressions (syntax) −
Proof system for equations −
Calculation rules called reduction −
Additional topics in lambda calculus•
Mathematical semantics (=model theor y) −
Type systems −
We will look at syntax, equations and reduction
There is more detail in the book than we will cover in class
Lambda calculus
L. Dillon, CSE 452, Fall 2008 3
Simple, uniform notation for basic PL concepts•
Free and bound variables −
Functions and application −
Declarations −
Calculation rule•
Symbolic evaluation useful for discussing programs −
Used in optimization (in-lining), macro expansion −
Correct macro processing requires variable renaming◊
Illustrates some ideas about scope of binding −
Lisp originally departed from standard lambda calculus, returned to the ◊
fold through Scheme, Common Lisp
Why study lambda calculus now?
L. Dillon, CSE 452, Fall 2008 4
Basic•
x + −1
x + −2*y - 1
Lambda expressions
L. Dillon, CSE 452, Fall 2008 5
Basic•
x + −1
x + −2*y - 1
Lambda expressions
Functions (lambda abstractions) •
m −x. (x + 1)
m −y. (x + 2*y + 1)
m −x. (my. (x + 2*y + 1)) = mx. my. (x + 2*y + 1)
Application•
( −mx. (x + 1)) (3) = mx. (x + 1) 3 = 3 + 1 = 4
( −my. (x + 2*y + 1)) (5) = my. (x + 2*y + 1) 5 = x + 11
( −mx. (my. (x + 2*y + 1))) (5) = mx. my. (x + 2*y + 1) 5
= my. (5 + 2*y + 1)
= my. (2*y + 6)
Rule for computing a value from an
argument (and, possibly, one or
more variables)
L. Dillon, CSE 452, Fall 2008 6
Basic•
x + −1
x + −2*y - 1
Lambda expressions
Functions (lambda abstractions) •
m −x. (x + 1)
m −y. (x + 2*y + 1)
m −x. (my. (x + 2*y + 1)) = mx. my. (x + 2*y + 1)
Application•
( −mx. (x + 1)) (3) = mx. (x + 1) 3 = 3 + 1 = 4
( −my. (x + 2*y + 1)) (5) = my. (x + 2*y + 1) 5 = x + 11
( −mx. (my. (x + 2*y + 1))) (5) = mx. my. (x + 2*y + 1) 5
= my. (5 + 2*y + 1)
= my. (2*y + 6)
Rule for computing a value from an
argument (and, possibly, one or
more variables)
body
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lambda Calculus: Foundations of Functional Programming - Prof. Laura Dillon and more Papers Programming Languages in PDF only on Docsity!

L. Dillon, CSE 452, Fall 2008

1

Original intention

Study foundations of mathematics (Church & Kleene)

− More successful for computable functions

Substitution --> symbolic computation −

Church/Turing thesis

− Influenced design of Lisp, ML, other languages

See Boost Lambda Library for C++ function objects

− Important part of CS history and foundations

Foundations: Lambda calculus

L. Dillon, CSE 452, Fall 2008

2

Formal system with three parts

Notation for function expressions (syntax) −

Proof system for equations −

Calculation rules called reduction

− Additional topics in lambda calculus

Mathematical semantics (=model theory) −

Type systems − We will look at syntax, equations and reductionThere is more detail in the book than we will cover in class

Lambda calculus

L. Dillon, CSE 452, Fall 2008

Simple, uniform notation for basic PL concepts

Free and bound variables −

Functions and application −

Declarations

− Calculation rule

Symbolic evaluation useful for discussing programs −

Used in optimization (in-lining), macro expansion −

Correct macro processing requires variable renaming ◊ Illustrates some ideas about scope of binding −

Lisp originally departed from standard lambda calculus, returned to the ◊

fold through Scheme, Common Lisp

Why study lambda calculus now?

L. Dillon, CSE 452, Fall 2008

4

Basic

x + −

x + −

*y -

Lambda expressions

L. Dillon, CSE 452, Fall 2008

5

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

(x +

*y +

m

x.

m

y.

(x +

*y +

m

y.

*y +

m

y.

*y +

Rule for computing a value from anargument

(and, possibly, one or

more variables)

L. Dillon, CSE 452, Fall 2008

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

(x +

*y +

m

x.

m

y.

(x +

*y +

m

y.

*y +

m

y.

*y +

Rule for computing a value from anargument

(and, possibly, one or

more variables)

body

L. Dillon, CSE 452, Fall 2008

7

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

x +

*y +

m

x.

m

y.

(x +

*y +

m

y.

*y +

m

y.

*y +

bound variable

free variable

lambda binding

L. Dillon, CSE 452, Fall 2008

8

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

(x +

*y +

m

x.

m

y.

(x +

*y +

m

y.

*y +

m

y.

*y +

Applies the function (lambdaabstraction) to an argument

argument

L. Dillon, CSE 452, Fall 2008

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

(x +

*y +

m

x.

m

y.

(x +

*y +

m

y.

*y +

m

y.

*y +

L. Dillon, CSE 452, Fall 2008

10

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

x +

*y +

m

x.

m

y.

(x +

*y +

m

y.

*y +

m

y.

*y +

L. Dillon, CSE 452, Fall 2008

11

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

(x +

*y +

m

x.

m

y.

(x +

*y +

m

y.

*y +

m

y.

*y +

Lambda abstraction containingboth a bound variable and a freevariable

L. Dillon, CSE 452, Fall 2008

12

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

(x +

*y +

m

x.

m

y.

(x +

*y +

m

y.

*y +

m

y.

*y +

free variable

bound variable

L. Dillon, CSE 452, Fall 2008

19

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

x +

*y +

m

x.

m

y.

(x +

*y +

m

y.

*y +

m

y.

*y +

L. Dillon, CSE 452, Fall 2008

20

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

m

y.

*y +

L. Dillon, CSE 452, Fall 2008

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

m

y.

*y +

L. Dillon, CSE 452, Fall 2008

22

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

m

y.

*y +

L. Dillon, CSE 452, Fall 2008

23

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

m

y.

*y +

L. Dillon, CSE 452, Fall 2008

Basic

x + −

x + −

*y -

Lambda expressions

Functions (lambda abstractions)

m −

x.

x +

m −

y.

(x +

*y +

m −

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

Application

m

x.

x +

m

x.

(x +

m

y.

x +

*y +

m

y.

x +

*y +

x

m

x.

m

y.

x +

*y +

m

x.

m

y.

x +

*y +

m

y.

*y +

L. Dillon, CSE 452, Fall 2008

25

Bound variable is a “placeholder”

Variable −

y

is

bound

in

m

y.

x +

*y +

m −

y.

(x +

*y +

is same function as

m

z.

x +

*z +

Name

of free (unbound) variable

does

matter

Variable −

x

is

free

in

m

y.

x +

*y +

Function −

m

y.

x +

*y +

is not same as

m

y.

(m +

*y +

Compare

to

(x +

*y +

dx

(x +

*z +

dz

x.

M

x

x

z.

M

z

)^

(z

z.

N

z

)^

(z

if

M

and

N

Occurrences

y −

occurs both free and bound in

m

x

.^

m

y.

y

x

y

Free and bound variables

L. Dillon, CSE 452, Fall 2008

26

Bound variable is a “placeholder”

Variable −

y

is

bound

in

m

y.

x +

*y +

m −

y.

(x +

*y +

is same function as

m

z.

x +

*z +

Name

of free (unbound) variable

does

matter

Variable −

x

is

free

in

m

y.

x +

*y +

Function −

m

y.

x +

*y +

is not same as

m

y.

(m +

*y +

Compare

to

(x +

*y +

dx

(x +

*z +

dz

x.

M

x

x

z.

M

z

)^

(z

z.

N

z

)^

(z

if

M

and

N

Occurrences

y −

occurs both free and bound in

m

x

.^

m

y.

y

x

y

Free and bound variables

free

bound

L. Dillon, CSE 452, Fall 2008

Reduction

Basic computation rule is

b

reduction

( m

x

.^

e

)^

e

[

e2/x

]e

where substitution involves renaming bound variables as neededto prevent a free variable in

the argument (

e

) from being cap-

tured by a lambda binding in

the body

(e

substitute

e

for all free

occurrences of

x

in

e

L. Dillon, CSE 452, Fall 2008

28

Reduction

Basic computation rule is

b

reduction

( m

x

.^

e

)^

e

[

e2/x

]e

where substitution involves renaming bound variables as neededto prevent a free variable in

the argument (

e

) from being cap-

tured by a lambda binding in

the body

(e

Reduction

L. Dillon, CSE 452, Fall 2008

29

Reduction

Basic computation rule is

b

reduction

( m

x

.^

e

)^

e

[

e2/x

]e

where substitution involves renaming bound variables as neededto prevent a free variable in

the argument (

e

) from being cap-

tured by a lambda binding in

the body

(e

Reduction:

Apply basic computation rule to any subexpression −

Repeat until no more −

b

-reductions can be performed

Confluence:

Final result (if there is one) is uniquely determined −

Why might there not be a final result? −

L. Dillon, CSE 452, Fall 2008

Exercise

Exercise #1: Use lambda calculus reduction to find the shortestlambda expression possible for each of the following.a.

m

x.

x - y

b.

m

y.

x - y

c.

m

y.

m

x.

x - y

d.

m

y.

m

x.

x - y

e.

m

x.

m

y.

x - y

L. Dillon, CSE 452, Fall 2008

37

Higher order functions

Given function

f, return function

f^

f

m −

f.

m

x.

f

(^

f x

How does this work?

( m

f.

m

x.

(^

f

f x

( m

y.

*y

m

x.

m

y.

*y

m

y.

*y

x

m

x.

m

y.

*y

)^

*x

m

x.

*^

*x

m

x.

*x

L. Dillon, CSE 452, Fall 2008

38

Higher order functions

Given function

f, return function

f^

f

m −

f.

m

x.

f

(^

f x

How does this work?

( m

f.

m

x.

(^

f

f x

( m

y.

*y

m

x.

m

y.

*y

m

y.

*y

x

m

x.

m

y.

*y

)^

*x

m

x.

*x

m

x.

*x

L. Dillon, CSE 452, Fall 2008

Higher order functions

Given function

f, return function

f^

f

m −

f.

m

x.

f

(^

f x

How does this work?

( m

f.

m

x.

(^

f

f x

( m

y.

*y

m

x.

m

y.

*y

m

y.

*y

x

m

x.

m

y.

*y

)^

*x

m

x.

*x

m

x.

*x

L. Dillon, CSE 452, Fall 2008

40

Higher order functions

Given function

f, return function

f^

f

m −

f.

m

x.

f

(^

f x

How does this work?

( m

f.

m

x.

(^

f

f x

( m

y.

*y

m

x.

m

y.

*y

m

y.

*y

x

m

x.

m

y.

*y

)^

*x

m

x.

*^

*x

m

x.

*x

L. Dillon, CSE 452, Fall 2008

41

Higher order functions

Given function

f, return function

f^

f

m −

f.

m

x.

f

(^

f x

How does this work?

( m

f.

m

x.

(^

f

f x

( m

y.

*y

m

x.

m

y.

*y

m

y.

*y

x

m

x.

m

y.

*y

)^

*x

m

x.

*x

m

x.

*x

L. Dillon, CSE 452, Fall 2008

Higher order functions

Given function

f, return function

f^

f

m −

f.

m

x.

f

(^

f x

How does this work?

( m

f.

m

x.

(^

f

f x

( m

y.

*y

m

x.

m

y.

*y

m

y.

*y

x

m

x.

m

y.

*y

)^

*x

m

x.

*x

m

x.

*x

L. Dillon, CSE 452, Fall 2008

43

Higher order functions

Given function

f, return function

f^

f

m −

f.

m

x.

f

(^

f x

How does this work?

( m

f.

m

x.

(^

f

f x

( m

y.

*y

m

x.

m

y.

*y

m

y.

*y

x

m

x.

m

y.

*y

)^

*x

m

x.

*^

*x

m

x.

*x

L. Dillon, CSE 452, Fall 2008

44

Excercise: Confluence

( m

f.

m

x.

(^

f

f x

( m

y.

*y

m

x.

m

y.

*y

m

y.

*y

x

m

x.

m

y.

*y

*x

m

x.

*x

m

x.

*x

Subexpressions can be reduced in whatever order you

want.

At what step in the evaluation below could you have performed −

a different reduction?Carry out the calculation by reducing a different −

subexpression at this step.

L. Dillon, CSE 452, Fall 2008

L. Dillon, CSE 452, Fall 2008

46

Declarations

Introduce simple declarations into lambda terms as

syntactic sugar

” as follows:

let

x

e

in

e2 end

abbreviates

( m

x

.^

e

e

For example:

let

twice

m

f.

m

x.

f

(^

f x

in

let

double

m

y.

*y

in

twice double

end end A more easily understood (program-like) definition of the examplein the previous slide(s)

L. Dillon, CSE 452, Fall 2008

47

How we see these concepts in PLs

Natural parallels to concepts of declarations, scope,

function calls, etc. in programming

int x = 10;int f ( int x ) { return 2*x };int g ( int y ) { return x + y };f ( g ( 4) );

let

x =

in

let

f =

m

x.

x

in

let

g =

m

y.

x

y

in

f

g

end

end

end

L. Dillon, CSE 452, Fall 2008

How we see these concepts in PLs

Natural parallels to concepts of declarations, scope,

function calls, etc. in programming

int x = 10;int f ( int x ) { return 2*x };int g ( int y ) { return x + y };f ( g ( 4) );

m

x.

m

f.

m

g.

f

g

m

y.

x

y

m

x.

x

L. Dillon, CSE 452, Fall 2008

55

How we see these concepts in PLs

int y = 1;int f ( int x ) { return x + y };int g ( int y ) { return f(1) + y };g ( 2 ) ;int y = 1;int f ( int x ) { return x + y };int g ( int z ) { return f(1) + z };g ( 2 ) ;int y = 1;int g ( int z ) { return 1+y + z };g ( 2 ) ;

m

y.

m

f.

m

g.

g

m

y.

f^

y

( m

x.

(x+y

(^1) m y.

m

f.

m

g.

g

m

z.

f^

z

( m

x.

(x + y

(^1) m y.

m

g.

g

( m

z.

y

z

Macro expansion and reduction, may require renaming

of bound variable

L. Dillon, CSE 452, Fall 2008

Summary

m

captures “essence” of variable bindingFunction parameters

Declarations −

Bound variables can be renamed

− Succinct

function expressions

Simple

symbolic evaluator via substitution

Can

be extended with

Types −

Various functions (multiple arguments, recursion, etc.) −

Stores and side-effects −

( But we didn’t cover these )