






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: Paper; Class: PROG ANLYS&UNDERSTANDING; Subject: Computer Science; University: University of Maryland; Term: Spring 2009;
Typology: Papers
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Lambda Calculus CMSC 631 – Program Analysis and Understanding Spring 2009 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
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
! (^) 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
! (^) 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 (^13)
! 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 (^14)
! e1[e2\x] ! e1[x!e2] ! (^) [x/e2]e ! (^) and more...
! (^) E.g., a function of two arguments "(x, y).e
! (^) "x."y.e ! This is a function that, given argument x, returns a function that, given argument y, returns e ! (^) ("x."y.e) a b # ("y.e[a\x]) b # e[a\x][b\y]
! (^) if true then b else c # ("x."y.x) b c #("y.b) c # b ! (^) if false then b else c # ("x."y.y) b c #("y.y) c # c
CMSC 631 (^17)
! So true and false are both combinators
! (^) I = "x.x ! (^) S = "x."y.x ! (^) K = "x."y."z.x z (y z) ! (^) Can also define calculi in terms of combinators
CMSC 631 (^18)
! (^) fst (a, b) #* a ! (^) snd (a, b) #* b
CMSC 631 (^25)
! (^) Examples: "x.x, "x."y.z
! Warning: All of this applies only to the pure lambda calculus with non-deterministic evaluation
CMSC 631 (^26)
! (^) 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 (^29)
! 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 (^30)
! G needs to have a “base case” to ensure termination
! Different combinator(s) for call-by-value
! Much more efficient ! Much easier to perform program analysis on and avoid silly mistakes with
! (^) Lazy: Given ("x.e1) e2, do not evaluate e2 if x does not “need” e
CMSC 631 (^37)
! 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 (^38)
! (^) Garbage collection was first designed with Lisp; most languages often rely on a GC today ! Generics in Java/C++ came from polymorphism in ML and from type classes in Haskell ! (^) Higher-order functions and closures (used widely in Ruby; proposed extension to Java) are pervasive in all functional languages ! Many data abstraction principles of OO came from ML’s module system ! (^) … CMSC 631
39 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
40 cond p x y = if p then x else y integers n = n:(integers (n+1)) take 10 (integers 0) ( infinite loop in cbv )