the%20lambda%20calculus - PowerPoint PPT Presentation

About This Presentation
Title:

the%20lambda%20calculus

Description:

there are several different ways to evaluate lambda calculus expressions ... amazingly, despite it's minimalism, the lambda calculus is Turing complete and ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 45
Provided by: csPrin7
Category:

less

Transcript and Presenter's Notes

Title: the%20lambda%20calculus


1
the lambda calculus
  • David Walker
  • CS 441

2
the (untyped) lambda calculus
  • a language of functions
  • e x \x.e e1 e2 v
    \x.e
  • there are several different ways to evaluate
    lambda calculus expressions
  • so far, weve studied left-to-right,
    call-by-value
  • amazingly, despite its minimalism, the lambda
    calculus is Turing complete and may serve as a
    foundation for all computation

(b)
(\x.e) v --gt e v/x
e2 --gt e2 v e2 --gt v e2
e1 --gt e1 e1 e2 --gt e1 e2
(app2)
(app1)
3
extending the lambda calculus
  • lambda calculus with booleans
  • e x true false test \x.e e1 e2
  • v true false test \x.e

e1 --gt e1
(test1)
test e1 e2 e3 --gt test e1 e2 e3
e2 --gt e2
e3 --gt e3
(test2)
(test3)
test v1 e2 e3 --gt test v1 e2 e3
test v1 v2 e3 --gt test v1 v2 e3
(test-false)
(test-true)
test true v2 v3 --gt v2
test false v2 v3 --gt v3
all the other lambda calculus rules
4
booleans the translation
  • from ?boolean into pure ?
  • translate(true) \t.\f. t
  • translate(false) \t.\f. f
  • translate(test) \x.\y.\z. x y z
  • translate(x) x
  • translate(\x.e) \x.(translate(e))
  • translate(e1 e2) translate(e1) translate(e2)

target language
values in the source language always get
translated into values in the target language
source language
5
boolean translation
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • example
  • translate(test true s1 s2) (\x.\y.\z.x y z)
    (\x.\y.x) v1 v2
  • (where v1 translate s1
  • and v2 translate s2)

e2 --gt e2 v e2 --gt v e2
(app2)
6
boolean translation
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • example
  • translate(test true s1 s2) (\x.\y.\z.x y z)
    (\x.\y.x) v1 v2
  • (where v1 translate s1
  • and v2 translate s2)
  • in the source language we have
  • test true s1 s2 --gt s1

e2 --gt e2 v e2 --gt v e2
(app2)
7
boolean translation
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • example
  • translate(test true s1 s2) (\x.\y.\z.x y z)
    (\x.\y.x) v1 v2
  • (where v1 translate s1
  • and v2 translate s2)
  • in the source language we have
  • test true s1 s2 --gt s1
  • so in the target we want
  • (\x.\y.\z.x y z) (\x.\y.x) v1 v2 --gt v1

e2 --gt e2 v e2 --gt v e2
(app2)
8
boolean translation
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • example
  • translate(test true s1 s2) (\x.\y.\z.x y z)
    (\x.\y.x) v1 v2
  • (where v1 translate s1
  • and v2 translate s2)
  • in the source language we have
  • test true s1 s2 --gt s1
  • so in the target we want
  • (\x.\y.\z.x y z) (\x.\y.x) v1 v2 --gt v1

e2 --gt e2 v e2 --gt v e2
(app2)
9
boolean translation
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • Theorem (Translation Preserves Semantics)
  • If
  • s1 --gt s2 (in the lambda calculus with
    booleans)
  • then
  • (translate(s1)) --gt (translate(s2)) (in
    the pure lambda calc)

e2 --gt e2 v e2 --gt v e2
(app2)
10
booleans
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • (((\x.\y.\z.x y z) (\x.\y.x)) v1) v2 --gt
    _____________

e2 --gt e2 v e2 --gt v e2
(app2)
11
booleans
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • ((\x.\y.\z.x y z) (\x.\y.x)) v1) --gt ___________
  • (((\x.\y.\z.x y z) (\x.\y.x)) v1) v2 --gt
    _____________ v2

e2 --gt e2 v e2 --gt v e2
(app2)
e1
(app1)
e1
e1
12
booleans
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • (\x.\y.\z.x y z) (\x.\y.x) --gt ___________
  • (\x.\y.\z.x y z) (\x.\y.x)) v1 --gt ___________
    v1
  • (((\x.\y.\z.x y z) (\x.\y.x)) v1) v2 --gt
    _____________ v2

e2 --gt e2 v e2 --gt v e2
(app2)
(b)
(app1)
(app1)
13
booleans
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • \x.\y.\z.x y z (\x.\y.x) --gt \y.\z.x y z
    \x.\y.x / x
  • ((\x.\y.\z.x y z) (\x.\y.x)) v1) --gt
    v1
  • (((\x.\y.\z.x y z) (\x.\y.x)) v1) v2 --gt
    v2

e2 --gt e2 v e2 --gt v e2
(app2)
(b)
(app1)
(app1)
14
booleans
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • \x.\y.\z.x y z (\x.\y.x) --gt \y.\z.(\x.\y.x) y
    z
  • ((\x.\y.\z.x y z) (\x.\y.x)) v1) --gt
    v1
  • (((\x.\y.\z.x y z) (\x.\y.x)) v1) v2 --gt
    v2

e2 --gt e2 v e2 --gt v e2
(app2)
(b)
(app1)
(app1)
15
booleans
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • \x.\y.\z.x y z (\x.\y.x) --gt \y.\z.(\x.\y.x) y
    z
  • ((\x.\y.\z.x y z) (\x.\y.x)) v1) --gt
    (\y.\z.(\x.\y.x) y z) v1
  • (((\x.\y.\z.x y z) (\x.\y.x)) v1) v2 --gt
    v2

e2 --gt e2 v e2 --gt v e2
(app2)
(b)
(app1)
(app1)
16
booleans
(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • (\x.\y.\z.x y z) (\x.\y.x) --gt \y.\z.(\x.\y.x)
  • ((\x.\y.\z.x y z) (\x.\y.x)) v1) --gt
    \y.\z.(\x.\y.x) v1
  • (((\x.\y.\z.x y z) (\x.\y.x)) v1) v2 --gt
    \y.\z.(\x.\y.x) v1 v2

e2 --gt e2 v e2 --gt v e2
(app2)
(b)
(app1)
(app1)
17
booleans
  • translate(true) \t.\f. t
  • translate(false) \t.\f. f
  • translate(and) \b.\c. b c (translate(false))
  • translate(x) ...
  • translate(and true true) (\b.\c. b c (\t.\f.f))
    (\t.\f.t) (\t.\f.t)

18
booleans
  • translate(true) \t.\f. t
  • translate(false) \t.\f. f
  • translate(and) \b.\c. b c (translate(false))
  • translate(x) ...
  • translate(and true true) (\b.\c. b c (\t.\f.f))
    (\t.\f.t) (\t.\f.t)
  • gt wow is that ever tough to read ...

19
booleans
  • tru \t.\f. t fls \t.\f. f
    a \b.\c. b c fls
  • translate(and true true)
  • a tru tru
  • (\b.\c. b c fls) tru tru

target language expression (ie a and tru
are just meta-variables Im using to
abbreviate and make more readable the
translated term)
20
booleans
  • tru \t.\f. t fls \t.\f. f
    a \b.\c. b c fls
  • a tru tru
  • (\b.\c. b c fls) tru tru
  • --gt (\c.tru c fls) tru

(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
e2 --gt e2 v e2 --gt v e2
(app2)
21
booleans
  • tru \t.\f. t fls \t.\f. f
    a \b.\c. b c fls
  • a tru tru
  • (\b.\c. b c fls) tru tru
  • --gt (\c.tru c fls) tru
  • --gt tru tru fls

(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
e2 --gt e2 v e2 --gt v e2
(app2)
22
booleans
  • tru \t.\f. t fls \t.\f. f
    a \b.\c. b c fls
  • a tru tru
  • (\b.\c. b c fls) tru tru
  • --gt (\c.tru c fls) tru
  • --gt tru tru fls
  • (\t.\f. t) tru fls

(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
e2 --gt e2 v e2 --gt v e2
(app2)
23
booleans
  • tru \t.\f. t fls \t.\f. f
    a \b.\c. b c fls
  • a tru tru
  • (\b.\c. b c fls) tru tru
  • --gt (\c.tru c fls) tru
  • --gt tru tru fls
  • (\t.\f. t) tru fls
  • --gt(\f. tru) fls

(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
e2 --gt e2 v e2 --gt v e2
(app2)
24
booleans
  • tru \t.\f. t fls \t.\f. f
    a \b.\c. b c fls
  • a tru tru
  • (\b.\c. b c fls) tru tru
  • --gt (\c.tru c fls) tru
  • --gt tru tru fls
  • (\t.\f. t) tru fls
  • --gt(\f. tru) fls
  • --gt tru

(b)
(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
e2 --gt e2 v e2 --gt v e2
(app2)
25
if statements
(b)
  • e x v e1 e2
  • if e1 then e2 else e3
  • v \x.e true false

(\x.e) v --gt e v/x
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
e2 --gt e2 v e2 --gt v e2
(app2)
e1 --gt e1
(if1)
if e1 then e2 else e3 --gt if e1 then e2 else e3
(if2)
if true then e2 else e3 --gt e2
(if3)
if false then e2 else e3 --gt e3
26
An Example
  • loop (\x.x x) (\x.x x)
  • if (\x.x) true then \x.x else loop
  • --gt if true then \x.x else loop
  • --gt \x.x

27
booleans
  • what is wrong with the following translation?
  • translate true tru
  • translate false fls
  • translate (if e1 then e2 else e3)
  • test (translate e1) (translate e2)
    (translate e3)
  • translate (\x.e) \x.(translate e)
  • translate (e1 e2) (translate e1) (translate e2)

28
booleans
pure lambda terms tru \t.\f.t fls
\t.\f.f tst \x.\y.\z. x y z
  • what is wrong with the following translation?
  • translate true tru
  • translate false fls
  • translate (if e1 then e2 else e3)
  • tst (translate e1) (translate e2)
    (translate e3)
  • ...

-- e2 and e3 will both be evaluated regardless of
whether e1 is true or false -- the target program
might not terminate in some cases when the
source program would
29
example
(b)
(\x.e) v --gt e v/x
e2 --gt e2 v e2 --gt v e2
(app2)
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • translate (if (\x.x) true then \x.x else loop)
  • tst translate((\x.x) true) (translate(\x.x))
    (translate(loop))
  • tst ((\x.x) tru) (\x.x) (loop)
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x))

30
example
(b)
(\x.e) v --gt e v/x
e2 --gt e2 v e2 --gt v e2
(app2)
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • translate (if (\x.x) true then \x.x else loop)
  • tst translate((\x.x) true) (translate(\x.x))
    (translate(loop))
  • tst ((\x.x) tru) (\x.x) (loop)
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x))
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x)) --gt
    tst tru (\x.x) ((\x.x x) (\x.x x))

31
example
(b)
(\x.e) v --gt e v/x
e2 --gt e2 v e2 --gt v e2
(app2)
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • translate (if (\x.x) true then \x.x else loop)
  • tst translate((\x.x) true) (translate(\x.x))
    (translate(loop))
  • tst ((\x.x) tru) (\x.x) (loop)
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x))
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x)) --gt
    tst tru (\x.x) ((\x.x x) (\x.x x))
  • (((\x.\y.\z. x y z) tru) (\x.x)) ((\x.x x) (\x.x
    x)) --gt

32
example
(b)
(\x.e) v --gt e v/x
e2 --gt e2 v e2 --gt v e2
(app2)
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • translate (if (\x.x) true then \x.x else loop)
  • tst translate((\x.x) true) (translate(\x.x))
    (translate(loop))
  • tst ((\x.x) tru) (\x.x) (loop)
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x))
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x)) --gt
    tst tru (\x.x) ((\x.x x) (\x.x x))
  • (((\x.\y.\z. x y z) tru) (\x.x)) ((\x.x x) (\x.x
    x)) --gt
  • ((\y.\z. tru y z) (\x.x)) ((\x.x x)
    (\x.x x)) --gt

33
example
(b)
(\x.e) v --gt e v/x
e2 --gt e2 v e2 --gt v e2
(app2)
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • translate (if (\x.x) true then \x.x else loop)
  • tst translate((\x.x) true) (translate(\x.x))
    (translate(loop))
  • tst ((\x.x) tru) (\x.x) (loop)
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x))
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x)) --gt
    test tru (\x.x) ((\x.x x) (\x.x x))
  • (((\x.\y.\z. x y z) tru) (\x.x)) ((\x.x x) (\x.x
    x)) --gt
  • ((\y.\z. tru y z) (\x.x)) ((\x.x x)
    (\x.x x)) --gt
  • (( \z. tru (\x.x) z) ) ((\x.x x)
    (\x.x x)) --gt

34
example
(b)
(\x.e) v --gt e v/x
e2 --gt e2 v e2 --gt v e2
(app2)
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • translate (if (\x.x) true then \x.x else loop)
  • tst translate((\x.x) true) (translate(\x.x))
    (translate(loop))
  • tst ((\x.x) tru) (\x.x) (loop)
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x))
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x)) --gt
    tst tru (\x.x) ((\x.x x) (\x.x x))
  • (((\x.\y.\z. x y z) tru) (\x.x)) ((\x.x x) (\x.x
    x)) --gt
  • ((\y.\z. tru y z) (\x.x)) ((\x.x x)
    (\x.x x)) --gt
  • (( \z. tru (\x.x) z) ) ((\x.x x)
    (\x.x x)) --gt
  • (( \z. tru (\x.x) z) ) ((\x.x x)
    (\x.x x)) --gt

35
example
(b)
(\x.e) v --gt e v/x
e2 --gt e2 v e2 --gt v e2
(app2)
e1 --gt e1 e1 e2 --gt e1 e2
(app1)
  • translate (if (\x.x) true then \x.x else loop)
  • tst translate((\x.x) true) (translate(\x.x))
    (translate(loop))
  • tst ((\x.x) tru) (\x.x) (loop)
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x))
  • tst ((\x.x) tru) (\x.x) ((\x.x x) (\x.x x)) --gt
    tst tru (\x.x) ((\x.x x) (\x.x x))
  • (((\x.\y.\z. x y z) tru) (\x.x)) ((\x.x x) (\x.x
    x)) --gt
  • ((\y.\z. tru y z) (\x.x)) ((\x.x x)
    (\x.x x)) --gt
  • (( \z. tru (\x.x) z) ) ((\x.x x)
    (\x.x x)) --gt
  • (( \z. tru (\x.x) z) ) ((\x.x x)
    (\x.x x)) --gt
  • (( \z. tru (\x.x) z) ) ((\x.x x)
    (\x.x x)) --gt ...

36
booleans
  • we saw this translation didnt work
  • translate (if e1 then e2 else e3)
  • tst (translate e1) (translate e2) (translate
    e3)
  • we need to delay execution of e2, e3 until the
    right time
  • translate (true) \x.\y.(x (\x.x))
  • translate (false) \x.\y.(y (\x.x))
  • translate (if e1 then e2 else e3)
  • (\x.\y.\z. x y z) (translate e1)
    (\w.translate e2) (\w.translate e3)
  • ... other cases ...

37
pairs
  • would like to encode the operations
  • create e1 e2
  • fst p
  • sec p
  • pairs will be functions
  • when the function is used in the fst or sec
    operation it should reveal its first or second
    component respectively

38
pairs
  • create \x.\y.\b. b x y
  • fst \p. p tru tru
    \x.\y.x
  • sec \p. p fls fls
    \x.\y.y

39
pairs
  • create \x.\y.\b. b x y
  • fst \p. p tru tru
    \x.\y.x
  • sec \p. p fls fls
    \x.\y.y
  • fst (create tru fls)
  • fst ((\x.\y.\b. b x y) tru fls)

40
pairs
  • create \x.\y.\b. b x y
  • fst \p. p tru tru
    \x.\y.x
  • sec \p. p fls fls
    \x.\y.y
  • fst (create tru fls)
  • fst ((\x.\y.\b. b x y) tru fls)
  • --gt fst (\b. b tru fls)

41
pairs
  • create \x.\y.\b. b x y
  • fst \p. p tru tru
    \x.\y.x
  • sec \p. p fls fls
    \x.\y.y
  • fst (create tru fls)
  • fst ((\x.\y.\b. b x y) tru fls)
  • --gt fst (\b. b tru fls)
  • (\p.p tru) (\b. b tru fls)

42
pairs
  • create \x.\y.\b. b x y
  • fst \p. p tru tru
    \x.\y.x
  • sec \p. p fls fls
    \x.\y.y
  • fst (create tru fls)
  • fst ((\x.\y.\b. b x y) tru fls)
  • --gt fst (\b. b tru fls)
  • (\p.p tru) (\b. b tru fls)
  • --gt (\b. b tru fls) tru

43
pairs
  • create \x.\y.\b. b x y
  • fst \p. p tru tru
    \x.\y.x
  • sec \p. p fls fls
    \x.\y.y
  • fst (create tru fls)
  • fst ((\x.\y.\b. b x y) tru fls)
  • --gt fst (\b. b tru fls)
  • (\p.p tru) (\b. b tru fls)
  • --gt (\b. b tru fls) tru
  • --gt tru tru fls
  • (\x.\y.x) tru fls
  • --gt (\y.tru) fls
  • --gt tru

44
and we can go on...
  • numbers
  • arithmetic expressions (, -, ,...)
  • lists, trees and datatypes
  • exceptions, loops, ...
  • ...
  • the general trick
  • values will be functions construct these
    functions so that they return the appropriate
    information when called by an operation
Write a Comment
User Comments (0)
About PowerShow.com