



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Lambda Calculus and Functional Programming CMSC 631 – Program Analysis and Understanding Fall 2003 CMSC 631, Fall 2003 2
CMSC 631, Fall 2003 3
■ ANSI C99 standard: 538 pages ■ ANSI C++ standard: 714 pages ■ (^) Java language specification 2.0: 505 pages
CMSC 631, Fall 2003 4
■ (^) The essential features ■ (^) No overlapping constructs ■ (^) And none of the cruft
■ (^) Standard core language for single-threaded procedural programming ■ (^) Often with added features (e.g., state); we’ll see that later
e ::= x variable | λx.e function abstraction | e e function application
■ Functions take functions as arguments and return functions as results ■ I.e., the lambda calculus supports higher-order functions
■ Bind x to e ■ Evaluate e ■ Return the result of the evaluation
■ (λx.e1) e2^ →β e1[e2\x] ■ (^) (λx.e1) e2 is called a redex ■ We’ll usually omit the beta
CMSC 631, Fall 2003 7
■ (^) let x = e1 in e2 is short for (λx.e2) e
■ λx.λy.x y is λx.(λy.(x y))
■ (^) x y z is (x y) z
CMSC 631, Fall 2003 8
■ (^) (λx.e1) e2 → e1[e2\x] ■ what if there are multiple x’s?
■ (^) let x = a in let y = λz.x in let x = b in y x ■ which x’s are bound to a, and which to b?
CMSC 631, Fall 2003 9
■ (^) The term
CMSC 631, Fall 2003 10
FV(x) = {x} FV(λx.e) = FV(e) - {x} FV(e1 e2) = FV(e1) FV(e2)
■ (^) λx.e = λy.(e[y\x]) if y FV(e)
■ (^) x[e\x] = e ■ (^) z[e\x] = z if z ≠ x ■ (^) (e1 e2)[e\x] = (e1[e\x] e2[e\x]) ■ (^) (λz.e1)[e\x] = λz.(e1[e\x]) if z ≠ x and z FV(e)
■ (λx.y x) x =α (λw.y w) x^ →β y x ■ (^) (We won’t write alpha conversion down in the future)
CMSC 631, Fall 2003 19
CMSC 631, Fall 2003 20
■ Example: Eval() from last time
■ (^) (λx.e1) e2 → e1[e2\x] ■ (^) Where does this rule apply?
CMSC 631, Fall 2003 21
(λx.e1) e2 → e1[e2\x] e → e′ (λx.e) → (λx.e′) e1 → e1′ e1 e2 → e1′ e e2 → e2′ e1 e2 → e1 e2′ CMSC 631, Fall 2003 22
■ (^) Read: If hypotheses H1 through Hn hold, then conclusion C holds ■
H1 H2 ... Hn C
■ (^) (λx.(λy.y) x ((λz.w) x) → λx.(x ((λz.w) x) → λx.x w ■ (^) (λx.(λy.y) x ((λz.w) x) → λx.(λy.y x (w)) → λx.x w ■
■ (^) If a → b and a → c, there there exists d such that b →* d and c →* d
■ If a →* b and a →* c, there there exists d such that b →* d and c →* d
CMSC 631, Fall 2003 25
CMSC 631, Fall 2003 26
■ (^) Examples: λx.x, λx.λy.z
■ Warning: All of this applies only to the pure lambda calculus with non-deterministic evaluation
CMSC 631, Fall 2003 27
■ (^) E.g., (λx.x) y → y ← (λz.λw.z) y y, so all three are beta equivalent ■
■ (^) Proof: Consequence of Church-Rosser Theorem ■
CMSC 631, Fall 2003 28
■ (^) ∆ = λx.x x ■ (^) Then ∆ ∆ → ∆ ∆ →(((
■ ...which is good if we want recursion
■ (^) Y = λf.(λx.f (x x)) (λx.f (x x)) ■ Note: There are many versions of this combinator
■ (^) Y F = (λf.(λx.f (x x)) (λx.f (x x))) F ■ → (λx.F (x x)) (λx.F (x x)) ■ (^) → F ((λx.F (x x)) (λx.F (x x))) ■ (^) ← F (Y F)
■ (^) I.e., G = λf. λn.if n = 0 then 1 else n*f(n-1)
■ =β (λf.λn.if n = 0 then 1 else nf(n-1)) (Y G) 1 ■ =β if 1 = 0 then 1 else 1((Y G) 0) ■ =β if 1 = 0 then 1 else 1(G (Y G) 0) ■ =β if 1 = 0 then 1 else 1(λf.λn.if n = 0 then 1 else nf(n-1) (Y G) 0) ■ =β if 1 = 0 then 1 else 1(if 0 = 0 then 1 else 0((Y G) 0) ■ =β 11 = 1
CMSC 631, Fall 2003 37
■ The same expression always evaluates to the same result, regardless of the context
CMSC 631, Fall 2003 38
■ (^) Lots of higher-order functions ■ (^) No side-effects
■ But you’re supposed to avoid using them
■ (^) Haskell – Pure, lazy functional language; no side effects ■ (^) ML (SML/NJ, O’Caml) – Call-by-value, with side effects
■ Disadvantage/advantage: No static type systems
■ A partially-applied function is called a closure