Top Banner
the lambda calculus David Walker CS 441
27

the lambda calculus

Feb 13, 2016

Download

Documents

e.toile

the lambda calculus. David Walker CS 441. the lambda calculus. Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 Church says: “There may, indeed, be other applications of the system than its use as a logic.” Dave says: “I’ll say”. Reading. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: the lambda calculus

the lambda calculus

David WalkerCS 441

Page 2: the lambda calculus

the lambda calculus

• Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932– Church says: “There may, indeed, be other

applications of the system than its use as a logic.”

– Dave says: “I’ll say”

Page 3: the lambda calculus

Reading

• Pierce, Chapter 5

Page 4: the lambda calculus

functions

• essentially every full-scale programming language has some notion of function– the (pure) lambda calculus is a language

composed entirely of functions– we use the lambda calculus to study the

essence of computation– it is just as fundamental as Turing Machines

Page 5: the lambda calculus

syntax

e ::= x (a variable) | \x.e (a function; in ML: fn x => e) | e e (function application)

[ “\” will be written “” in a nice font]

Page 6: the lambda calculus

syntax

• the identity function:• \x.x

• 2 notational conventions:• applications associate to the left (like in ML): • “y z x” is “(y z) x”• the body of a lambda extends as far as possible to

the right:• “\x.x \z.x z x” is “\x.(x \z.(x z x))”

Page 7: the lambda calculus

terminology

• \x.e

• \x.x y

the scope of x is the term e

x is boundin the term \x.x y

y is free in the term \x.x y

Page 8: the lambda calculus

CBV operational semantics

• single-step, call-by-value OS: e --> e’– values are v ::= \x.e – primary rule (beta reduction):

– e [v/x] is the term in which all free occurrences of x in t are replaced with v

– this replacement operation is called substitution– we will define it carefully later in the lecture

(\x.e) v --> e [v/x]

Page 9: the lambda calculus

operational semantics

• search rules:

• notice, evaluation is left to right

e1 --> e1’e1 e2 --> e1’ e2

e2 --> e2’v e2 --> v e2’

Page 10: the lambda calculus

Example

(\x. x x) (\y. y)

Page 11: the lambda calculus

Example

(\x. x x) (\y. y)--> x x [\y. y / x]

Page 12: the lambda calculus

Example

(\x. x x) (\y. y)--> x x [\y. y / x]== (\y. y) (\y. y)

Page 13: the lambda calculus

Example

(\x. x x) (\y. y)--> x x [\y. y / x]== (\y. y) (\y. y)--> y [\y. y / y]

Page 14: the lambda calculus

Example

(\x. x x) (\y. y)--> x x [\y. y / x]== (\y. y) (\y. y)--> y [\y. y / y]== \y. y

Page 15: the lambda calculus

Another example

(\x. x x) (\x. x x)

Page 16: the lambda calculus

Another example

(\x. x x) (\x. x x)--> x x [\x. x x/x]

Page 17: the lambda calculus

Another example

(\x. x x) (\x. x x)--> x x [\x. x x/x]== (\x. x x) (\x. x x)

• In other words, it is simple to write non terminating computations in the lambda calculus

• what else can we do?

Page 18: the lambda calculus

We can do everything

• The lambda calculus can be used as an “assembly language”

• We can show how to compile useful, high-level operations and language features into the lambda calculus– Result = adding high-level operations is

convenient for programmers, but not a computational necessity

– Result = make your compiler intermediate language simpler

Page 19: the lambda calculus

Let Expressions

• It is useful to bind intermediate results of computations to variables:let x = e1 in e2

• Question: can we implement this idea in the lambda calculus?

source = lambda calculus + let

target = lambda calculus

translate/compile

Page 20: the lambda calculus

Let Expressions

• It is useful to bind intermediate results of computations to variables:let x = e1 in e2

• Question: can we implement this idea in the lambda calculus?translate (let x = e1 in e2) =

(\x.e2) e1

Page 21: the lambda calculus

Let Expressions

• It is useful to bind intermediate results of computations to variables:let x = e1 in e2

• Question: can we implement this idea in the lambda calculus?translate (let x = e1 in e2) =

(\x. translate e2) (translate e1)

Page 22: the lambda calculus

Let Expressions

• It is useful to bind intermediate results of computations to variables:let x = e1 in e2

• Question: can we implement this idea in the lambda calculus?translate (let x = e1 in e2) =

(\x. translate e2) (translate e1)translate (x) = xtranslate (\x.e) = \x.translate etranslate (e1 e2) = (translate e1) (translate e2)

Page 23: the lambda calculus

booleans• we can encode booleans

– we will represent “true” and “false” as functions named “tru” and “fls”

– how do we define these functions?– think about how “true” and “false” can be used– they can be used by a testing function:

• “test b then else” returns “then” if b is true and returns “else” if b is false

• the only thing the implementation of test is going to be able to do with b is to apply it

• the functions “tru” and “fls” must distinguish themselves when they are applied

Page 24: the lambda calculus

booleans

• the encoding:

tru = \t.\f. t

fls = \t.\f. f

test = \x.\then.\else. x then else

Page 25: the lambda calculus

booleans

tru = \t.\f. t fls = \t.\f. ftest = \x.\then.\else. x then else

eg:

test tru a b -->* (\t.\f. t) a b -->* a

Page 26: the lambda calculus

booleans

tru = \t.\f. t fls = \t.\f. fand = \b.\c. b c fls

and tru tru -->* tru tru fls -->* tru

Page 27: the lambda calculus

booleans

tru = \t.\f. t fls = \t.\f. fand = \b.\c. b c fls

and fls tru -->* fls tru fls -->* fls