Program Analysis and Understanding - Lambda Calculus | CMSC 631, Study notes of Computer Science

Material Type: Notes; Class: PROG ANLYS&UNDERSTANDING; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-8ik
koofers-user-8ik 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Lambda Calculus
CMSC 631 – Program Analysis and
Understanding
Fall 2006
2
CMSC 631
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
3
CMSC 631
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
4
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
Tim
Fraser
5
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
6
CMSC 631
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
pf3
pf4
pf5

Partial preview of the text

Download Program Analysis and Understanding - Lambda Calculus | CMSC 631 and more Study notes Computer Science in PDF only on Docsity!

Lambda Calculus CMSC 631 – Program Analysis and Understanding Fall 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 4 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 Tim Fraser CMSC 631 5 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 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 CMSC 631 11
  • 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 and Bound Variables CMSC 631 12
  • 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

A Nonderministic Small-Step

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′

  • Why are these semantics non-deterministic? CMSC 631 22 - 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 23
  • 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
  • Church-Rosser is also called confluence The Church-Rosser Theorem CMSC 631 24 Proof

CMSC 631 25

  • A term is in normal form if it cannot be reduced ■ Examples: λx.x, λx.λy.z ■ Some normal forms referred to as values: the “legal” end results of programs
  • By Church Rosser Theorem, every term reduces

to at most one normal form

  • Notice that for our application rule, the

argument need not be a normal form

Normal Form CMSC 631 26

  • Let =β be the reflexive, symmetric , and

transitive closure of →

■ Usually we think only of reduction; adding symmetry extends this to equivalence ■ E.g., (λx.x) y → y ← (λz.λw.z) y y, so all three are beta equivalent

  • If a =β b, then ∃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 CMSC 631 27

  • 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 CMSC 631 28
  • 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) for any 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 29
  • Fact n = if n = 0 then 1 else n * fact(n-1)
  • Let G = λf. ■ I.e., G = λf. λn.if n = 0 then 1 else n*f(n-1)
  • Y G 1 =β G (YG) 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 Example CMSC 631 30
  • The Y combinator “unrolls” or “unfolds” its

argument an infinite number of times

■ Y G = G (Y G) = G (G (Y G) = G (G (G (Y G))) = ...

■ G needs to have a “base case” to ensure termination

  • We can use this trick to encode arbitrary

recursion

  • Notice that this only works because we’re call-

by-name

In Other Words

CMSC 631 37

  • 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 38 Call-by-Name Example 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 39 Two Cool Things to Do with CBN
  • Build control structures with functions
  • “Infinite” data structures cond p x y = if p then x else y integers n = n:(integers (n+1)) take 10 (integers 0) ( infinite loop in cbv )