



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Foster; Class: PROG ANLYS&UNDERSTANDING; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Lambda Calculus CMSC 631 – Program Analysis and Understanding Spring 2006 CMSC 631 (^2)
■ ANSI C99 standard: 538 pages ■ ANSI C++ standard: 714 pages ■ (^) Java language specification 2.0: 505 pages
CMSC 631 (^3)
■ (^) 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
CMSC 631
■ (^) Princeton Mathematician ■ Lectures of lambda calculus published in 1941 ■ Also know for
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
CMSC 631 (^7)
■ 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 (^8)
■ 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 (^9)
■ (λ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 (^10)
■ The term
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)
CMSC 631 (^19)
CMSC 631 (^20)
CMSC 631 (^21)
■ Example: Eval() from last time
■ (λx.e1) e2 → e1[e2\x] ■ (^) Where does this rule apply?
CMSC 631 (^22)
(λx.e1) e2 → e1[e2\x] e → e′ (λx.e) → (λx.e′) e1 → e1′ e1 e2 → e1′ e e2 → e2′ e1 e2 → e1 e2′
■ 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
CMSC 631 (^25)
■ 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 (^26)
CMSC 631 (^27)
■ Examples: λx.x, λx.λy.z
■ (^) Warning: All of this applies only to the pure lambda calculus with non-deterministic evaluation
CMSC 631 (^28)
■ (^) E.g., (λx.x) y → y ← (λz.λw.z) y y, so all three are beta equivalent
■ Proof: Consequence of Church-Rosser Theorem
■ Δ = λ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)
CMSC 631 (^37)
■ Has some nice theoretical properties ■ Terminates more often ■ (^) Lets you play some tricks with “infinite” objects ■ (^) Main example: Haskell
■ (^) Is generally easier to implement efficiently ■ (^) Blends more easily with side effects ■ Main examples: Most languages (C, Java, ML, etc.)
CMSC 631 (^38)
■ Lots of higher-order functions ■ No side-effects
■ But you’re supposed to avoid using them
CMSC 631 (^39)
■ (^) Haskell – Pure, lazy functional language; no side effects ■ (^) ML (SML/NJ, OCaml) – Call-by-value, with side effects
■ Disadvantage/advantage: No static type systems
CMSC 631
40 OCaml let cond p x y = if p then x else y let rec loop () = loop () let z = cond true 42 (loop ()) Haskell cond p x y = if p then x else y loop () = loop () z = cond True 42 (loop ()) infinite loop at call 3rd argument never used by cond, so never invoked CMSC 631
41 cond p x y = if p then x else y integers n = n:(integers (n+1)) take 10 (integers 0) ( infinite loop in cbv )