CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and...
-
Upload
jason-conley -
Category
Documents
-
view
214 -
download
0
Transcript of CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and...
![Page 1: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/1.jpg)
CSE 230 The -Calculus
![Page 2: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/2.jpg)
Background
Developed in 1930’s by Alonzo ChurchStudied in logic and computer science
Test bed for procedural and functional PLsSimple, Powerful, Extensible
“Whatever the next 700 languages turn out to be, they will surely be variants of lambda calculus.”
(Landin ’66)
![Page 3: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/3.jpg)
Syntax
Three kinds of expressions (terms): e ::= x Variables | x.e Functions (-abstraction) | e1 e2 Application
![Page 4: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/4.jpg)
Application associates to the left x y z means (x y) z
Abstraction extends as far right as possible: x. x y. x y z means x.(x (y. ((x y) z)))
Syntax
![Page 5: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/5.jpg)
Examples of Lambda Expressions
Identity function
I =def lx. x
A function that always returns the identity fun
ly. (lx. x)
A function that applies arg to identity function:
lf. f (lx. x)
![Page 6: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/6.jpg)
Scope of an Identifier (Variable)
“part of program where variable is accessible”
![Page 7: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/7.jpg)
Free and Bound Variables
lx. E Abstraction binds variable x in E x is the newly introduced variable E is the scope of x x is bound in lx. E
![Page 8: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/8.jpg)
y is free in E if it occurs not bound in EFree(x) = {x} Free( E1 E2) = Free(E1) È Free(E2)
Free( lx. E) = Free(E) - { x }
e.g: Free( lx. x (ly. x y z) ) = { z }
Free and Bound Variables
![Page 9: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/9.jpg)
Renaming Bound Variables-renamingl-terms after renaming bound variablesConsidered identical to original
Example: lx. x == ly. y == lz. z
Rename bound variables so names uniquelx. x (ly.y) x instead of lx. x (lx.x) xEasy to see the scope of bindings
![Page 10: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/10.jpg)
Substitution
[E’/x] E : Substitution of E’ for x in E 1. Uniquely rename bound vars in E and E’ 2. Do textual substitution of E’ for x in E
Example: [y (lx. x)/x] ly. (lx. x) y x3. After renaming: [y (lv. v)/x] lz. (lu. u) z x4. After substitution: lz. (lu. u) z (y (lv. v))
![Page 11: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/11.jpg)
Semantics (“Evaluation”)
The evaluation of (x. e) e’1. binds x to e’2. evaluates e with the new binding3. yields the result of this evaluation
![Page 12: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/12.jpg)
Semantics: Beta-Reduction
(x. e) e’ [e’/x]e
![Page 13: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/13.jpg)
Semantics (“Evaluation”)
The evaluation of (x. e) e’1. binds x to e’2. evaluates e with the new binding3. yields the result of this evaluation
Example: (f. f (f e)) g g (g e)
![Page 14: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/14.jpg)
Another View of Reduction
APP
x.
x x x
e e’e’ e’ e’
e
Terms can grow substantially by reduction
![Page 15: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/15.jpg)
Examples of Evaluation
Identity function (lx. x) E ® [E / x] x = E
![Page 16: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/16.jpg)
Examples of Evaluation
… yet again (lf. f (lx. x)) (lx. x) ® [lx. x / f] f (lx. x) = [(lx. x) / f] f (ly. y) = (lx. x) (ly. y) ® [ly. y /x] x = ly. y
![Page 17: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/17.jpg)
Examples of Evaluation
(lx. x x)(ly. y y) ® [ly. y y / x] x x = (ly. y y)(ly. y y)= (lx. x x)(ly. y y) ® …A non-terminating evaluation !
![Page 18: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/18.jpg)
Review
A calculus of functions: e := x | lx. e | e1 e2
Eval strategies = “Where to reduce” ?Normal, Call-by-name, Call-by-value
Church-Rosser TheoremRegardless of strategy, upto one “normal
form”
![Page 19: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/19.jpg)
Programming with the -calculus
-calculus vs. “real languages” ?Local variables?Bools , If-then-else ?Records? Integers ?Recursion ?Functions: well, those we have …
![Page 20: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/20.jpg)
Local Variables (Let Bindings)
let x = e1 in e2 is just
(x. e2) e1
![Page 21: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/21.jpg)
Programming with the -calculus
-calculus vs. “real languages” ?Local variables (YES!)Bools , If-then-else ?Records? Integers ?Recursion ?Functions: well, those we have …
![Page 22: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/22.jpg)
Encoding Booleans in -calculus
What can we do with a boolean? Make a binary choice
How can you view this as a “function” ?Bool is a fun that takes two choices, returns one
![Page 23: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/23.jpg)
Encoding Booleans in -calculus
Bool = fun, that takes two choices, returns one
true =def lx. ly. x
false =def lx. ly. y
if E1 then E2 else E3 =def E1 E2 E3
Example: “if true then u else v” is
(lx. ly. x) u v (ly. u) v u
![Page 24: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/24.jpg)
Boolean Operations: Not, Or
Boolean operations: notFunction takes b:
returns function takes x,y: returns “opposite” of b’s return
not =def lb.(lx.y. b y x)
Boolean operations: or Function takes b1, b2:
returns function takes x,y: returns (if b1 then x else (if b2 then x else y))
or =def lb1.b2.(lx.y. b1 x (b2 x y))
![Page 25: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/25.jpg)
Programming with the -calculus
-calculus vs. “real languages” ?Local variables (YES!)Bools , If-then-else (YES!)Records? Integers ?Recursion ?Functions: well, those we have …
![Page 26: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/26.jpg)
Encoding Pairs (and so, Records)
What can we do with a pair ?Select one of its elements
Pair = function takes a bool, returns the left or the right element
mkpair e1 e2 =def lb. b e1 e
= “function-waiting-for-bool”
fst p =def p true
snd p =def p false
![Page 27: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/27.jpg)
Encoding Pairs (and so, Records)
mkpair e1 e2 =def lb. b e1 e
fst p =def p true
snd p =def p false
Examplefst (mkpair x y) (mkpair x y) true true x y x
![Page 28: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/28.jpg)
Programming with the -calculus
-calculus vs. “real languages” ?Local variables (YES!)Bools , If-then-else (YES!)Records (YES!)Integers ?Recursion ?Functions: well, those we have …
![Page 29: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/29.jpg)
Encoding Natural Numbers
What can we do with a natural number ? Iterate a number of times over some function
n = function that takes fun f, starting value s, returns: f applied to s “n” times
0 =def lf. ls. s
1 =def lf. ls. f s
2 =def lf. ls. f (f s)
Called Church numerals (Unary Representation) (n f s) = apply f to s “n” times, i.e. fn(s)
![Page 30: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/30.jpg)
Operating on Natural Numbers
Testing equality with 0 iszero n =def n (lb. false) true
iszero =def n.( b.false) true
Successor function succ n =def lf. ls. f (n f s)
succ =def n. lf. ls. f (n f s)
Addition add n1 n2 =def n1 succ n2
add =def n1.n2. n1 succ n2
Multiplication mult n1 n2 =def n1 (add n2) 0
mult =def n1.n2. n1 (add n2) 0
![Page 31: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/31.jpg)
Example: Computing with Naturals
What is the result of add 0 ?
(ln1. ln2. n1 succ n2) 0
ln2. 0 succ n2 =
ln2. (lf. ls. s) succ n2
ln2. n2 =
lx. x
![Page 32: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/32.jpg)
mult 2 2 2 (add 2) 0 (add 2) ((add 2) 0) 2 succ (add 2 0) 2 succ (2 succ 0) succ (succ (succ (succ 0))) succ (succ (succ (lf. ls. f (0 f s)))) succ (succ (succ (lf. ls. f s))) succ (succ (lg. ly. g ((lf. ls. f s) g y))) succ (succ (lg. ly. g (g y))) * lg. ly. g (g (g (g y))) = 4
Example: Computing with Naturals
![Page 33: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/33.jpg)
Programming with the -calculus
-calculus vs. “real languages” ?Local variables (YES!)Bools , If-then-else (YES!)Records (YES!)Integers (YES!)Recursion ?Functions: well, those we have …
![Page 34: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/34.jpg)
Encoding Recursion
Write a function find:IN : predicate P, number n OUT: smallest num >= n s.t. P(n)=True
![Page 35: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/35.jpg)
Encoding Recursion
find satisfies the equation: find p n = if p n then n else find p (succ n)
• Define: F = lf.lp.ln.(p n) n (f p (succ n))
• A fixpoint of F is an x s.t. x = F x
• find is a fixpoint of F !– as find p n = F find p n – so find = F find
Q: Given -term F, how to write its fixpoint ?
![Page 36: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/36.jpg)
The Y-CombinatorFixpoint Combinator Y =def lF. (ly.F(y y)) (lx. F(x x))
Earns its name as …Y F (ly.F (y y)) (lx. F (x x))
F ((lx.F (x x))(lz. F (z z))) F (Y F)
So, for any -calculus function F get Y F is fixpoint!Y F = F (Y F)
![Page 37: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/37.jpg)
Whoa!
Define: F = lf.lp.ln.(p n) n (f p (succ n))and: find = Y F
Whats going on ?find p n =b Y F p n =b F (Y F) p n =b F find p n =b (p n) n (find p (succ n))
![Page 38: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/38.jpg)
Y-Combinator in Practice
fac n = if n<1 then 1 else n * fac (n-1)
is justfac = \n->if n<1 then 1 else n * fac (n-1)
is justfac = Y (\f n->if n<1 then 1 else n*f(n-1))All Recursion Factored Into Y
![Page 39: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/39.jpg)
Many other fixpoint combinators
Including Klop’s Combinator:
Yk =def (L L L L L L L L L L L L L L L L L L L L L L L L L L)
where:L =def abcdefghIjklmnopqstuvwxyzr.
r (t h i s i s a f i x p o i n t c o m b i n a t o r)
![Page 40: CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.](https://reader036.fdocuments.us/reader036/viewer/2022070403/56649f335503460f94c4f52f/html5/thumbnails/40.jpg)
Expressiveness of -calculus
Encodings are funProgramming in pure -calculus is not!
We know -calculus encodes them So add 0,1,2,…,true,false,if-then-else to PL
Next, types…