Download Lambda Calculus - Program Analysis and Understanding - Slides Notes | CMSC 631 and more Study notes Computer Science in PDF only on Docsity!
Lambda Calculus CMSC 631 – Program Analysis and Understanding Spring 2006 CMSC 631 (^2)
• Commonly-used programming languages are
large are complex
■ ANSI C99 standard: 538 pages ■ ANSI C++ standard: 714 pages ■ (^) Java language specification 2.0: 505 pages
• Not good vehicles for understanding language
features or explaining program analysis
Motivation
CMSC 631 (^3)
• Develop a “core language” that has
■ (^) The essential features ■ (^) No overlapping constructs ■ And none of the cruft
- Extra features of full language can be defined in terms of the core language (“syntactic sugar”)
• Lambda calculus
■ Standard core language for single-threaded procedural programming ■ (^) Often with added features (e.g., state); we’ll see that later
Goal
CMSC 631
Lambda Calculus is Practical!
- An 8-bit microcontroller (Zilog Z8 encore board w/4KB SRAM) computing 1 + 1 using Church numerals in the Lambda calculus 4 Tim Fraser CMSC 631
Origins of Lambda Calculus
• Invented in 1936 by Alonzo Church (1903-1995)
■ (^) Princeton Mathematician ■ Lectures of lambda calculus published in 1941 ■ Also know for
- Church’s Thesis
- All effective computation is expressed by recursive (decidable) functions, i.e., in the lambda calculus
- Church’s Theorem
- First order logic is undecidable 5 CMSC 631^6
• Syntax:
e ::= x variable | λx.e function abstraction | e e function application
• Only constructs in pure lambda calculus
■ (^) Functions take functions as arguments and return functions as results ■ (^) I.e., the lambda calculus supports higher-order functions
Lambda Calculus
CMSC 631 (^7)
• To evaluate (λx.e1) e
■ Bind x to e ■ Evaluate e ■ Return the result of the evaluation
• This is called “beta-reduction”
■ (λx.e1) e2^ →β e1[e2\x] ■ (^) (λx.e1) e2 is called a redex ■ We’ll usually omit the beta
Semantics
CMSC 631 (^8)
• Syntactic sugar for local declarations
■ let x = e1 in e2 is short for (λx.e2) e
• Scope of λ extends as far to the right as possible
■ (^) λx.λy.x y is λx.(λy.(x y))
• Function application is left-associative
■ (^) x y z is (x y) z
Three Conveniences
CMSC 631 (^9)
• Beta-reduction is not yet precise
■ (λx.e1) e2 → e1[e2\x] ■ (^) what if there are multiple x’s?
• Example:
■ (^) 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?
Scoping and Parameter Passing
CMSC 631 (^10)
• Just like most languages, a variable refers to the
closest definition
• Make this precise using variable renaming
■ The term
- let x = a in let y = λz.x in let x = b in y x ■ (^) is “the same” as
- let x = a in let y = λz.x in let w = b in y w ■ (^) Variable names don’t matter
Static (Lexical) Scope
• The set of free variables of a term is
FV(x) = {x} FV(λx.e) = FV(e) - {x} FV(e1 e2) = FV(e1) FV(e2)
• A term e is closed if FV(e) =
• A variable that is not free is bound
Free Variables and Alpha Conversion
• Terms are equivalent up to renaming of bound
variables
■ λx.e = λy.(e[y\x]) if y FV(e)
• This is often called alpha conversion , and we will
use it implicitly whenever we need to avoid
capturing variables when we perform
substitution
Alpha Conversion
CMSC 631 (^19)
• 0 = λx.λy.y
• 1 = λx.λy.x y
• 2 = λx.λy.x(x y)
• i.e., n = λx.λy.
• succ = λz.λx.λy.x(z x y)
• iszero = λz.z (λy.false) true
Natural Numbers (Church)
CMSC 631 (^20)
• 0 = λx.λy.x
• 1 = λx.λy.y 0
• 2 = λx.λy.y 1
• I.e., n = λx.λy.y (n-1)
• succ = λz.λx.λy.y z
• pred = λz.z 0 (λx.x)
• iszero = λz.z true (λx.false)
Natural Numbers (Scott)
CMSC 631 (^21)
• An operational semantics is a series of rules for
evaluating (“running”) a program
■ Example: Eval() from last time
• So far we’ve defined one operational semantic
rule, but it’s still not precise
■ (λx.e1) e2 → e1[e2\x] ■ (^) Where does this rule apply?
- Current answer: Anywhere within a term
Operational Semantics
CMSC 631 (^22)
A Nonderministic Semantics
(λx.e1) e2 → e1[e2\x] e → e′ (λx.e) → (λx.e′) e1 → e1′ e1 e2 → e1′ e e2 → e2′ e1 e2 → e1 e2′
• The rules are a small-step semantics
■ It takes many →’s before we reach a normal form
• These are natural deduction style rules
■ Read: If hypotheses H1 through Hn hold, then conclusion C holds
• The rules are axioms that define something, in
this case what → means
• We will use this style of rule extensively
Natural Deduction
H1 H2 ... Hn C
• We can apply reduction anywhere in a term
■ (λ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
• Does the order of evaluation matter?
Example
CMSC 631 (^25)
• Lemma (The Diamond Property):
■ If a → b and a → c, there there exists d such that b →* d and c →* d
• Church-Rosser Theorem:
■ (^) If a →* b and a →* c, there there exists d such that b →* d and c →* d
• Proof: By diamond property
The Church-Rosser Theorem
CMSC 631 (^26)
Proof
CMSC 631 (^27)
• A term is in normal form if it cannot be reduced
■ Examples: λx.x, λx.λy.z
• By Church-Rosser Theorem, every term reduces
to at most one normal form
■ (^) Warning: All of this applies only to the pure lambda calculus with non-deterministic evaluation
Normal Form
CMSC 631 (^28)
• Let^ =β be the reflexive, symmetric, and transitive
closure of →
■ (^) E.g., (λx.x) y → y ← (λz.λw.z) y y, so all three are beta equivalent
• If^ a =β b, then there exists^ c^ such that^ a^ →*^ c
and b →* c
■ Proof: Consequence of Church-Rosser Theorem
• In particular, if^ a =β b^ and both are normal
forms, then they are equal
Beta-Equivalence
• Consider
■ Δ = λx.x x ■ Then Δ Δ → Δ Δ →···
• In general, self application leads to loops
■ ...which is good if we want recursion
Not Every Term Has a Normal Form
• Also called a paradoxical combinator
■ Y = λf.(λx.f (x x)) (λx.f (x x)) ■ (^) Note: There are many versions of this combinator
• Then^ Y F =β F (Y F)
■ 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)
A Fixpoint Combinator
CMSC 631 (^37)
• Lazy evaluation (call by name, call by need)
■ Has some nice theoretical properties ■ Terminates more often ■ (^) Lets you play some tricks with “infinite” objects ■ (^) Main example: Haskell
• Eager evaluation (call by value)
■ (^) Is generally easier to implement efficiently ■ (^) Blends more easily with side effects ■ Main examples: Most languages (C, Java, ML, etc.)
Lazy vs. Eager in Practice
CMSC 631 (^38)
• The λ calculus is a prototypical functional
programming language:
■ Lots of higher-order functions ■ No side-effects
• In practice, many functional programming
languages are “impure” and permit side-effects
■ But you’re supposed to avoid using them
Functional Programming
CMSC 631 (^39)
• Two main camps:
■ (^) Haskell – Pure, lazy functional language; no side effects ■ (^) ML (SML/NJ, OCaml) – Call-by-value, with side effects
• Still around: LISP, Scheme
■ Disadvantage/advantage: No static type systems
Functional Programming Today
CMSC 631
Call-by-Name Example
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
Two Cool Things to Do with CBN
• Build control structures with functions
• “Infinite” data structures
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 )