Docsity

Lambda Calculus and Functional Programming: An Introduction to CMSC 631, Study notes of Computer Science

Toggle sidebar
bg1
Lambda Calculus and Functional Programming
CMSC 631 – Program Analysis and
Understanding
Fall 2003
2
CMSC 631, Fall 2003
Space of Program Analyses
Data flow analysis Type systems
Theorem proving Model checking
3
CMSC 631, Fall 2003
• 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
4
CMSC 631, Fall 2003
• 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
5
CMSC 631, Fall 2003
• 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
6
CMSC 631, Fall 2003
•To evaluate (λx.e1) e2
■Bind x to e2
■Evaluate e1
■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
pf3
pf4
pf5

Partial preview of the text

Download Lambda Calculus and Functional Programming: An Introduction to CMSC 631 and more Study notes Computer Science in PDF only on Docsity!

Lambda Calculus and Functional Programming CMSC 631 – Program Analysis and Understanding Fall 2003 CMSC 631, Fall 2003 2

Space of Program Analyses

Data flow analysis Type systems

Theorem proving Model checking

CMSC 631, Fall 2003 3

• 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, Fall 2003 4

• 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

• 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

• 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, Fall 2003 7

• 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, Fall 2003 8

• 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, Fall 2003 9

• 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, Fall 2003 10

• 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

• Formal definition:

■ (^) 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)

• Example:

■ (λx.y x) x =α (λw.y w) x^ →β y x ■ (^) (We won’t write alpha conversion down in the future)

Substitution

CMSC 631, Fall 2003 19

• 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, Fall 2003 20

• 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, Fall 2003 21

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′ CMSC 631, Fall 2003 22

• 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

• 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, Fall 2003 25

Proof

CMSC 631, Fall 2003 26

• 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, Fall 2003 27

• 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

CMSC 631, Fall 2003 28

• 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

• 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, Fall 2003 37

• There are no side effects in the lambda calculus

■ The same expression always evaluates to the same result, regardless of the context

  • E.g., in (f x) (f x), both calls always yield the same result
  • In contrast,^ consider this C example,^ where^ a^ and^ b^ may^ differ
    • ...a=f(x);^ b=f(x);...

• This means we can reason just like we would in

mathematics, in a “pure” way

Referential Transparency

CMSC 631, Fall 2003 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

• Two main camps:

■ (^) Haskell – Pure, lazy functional language; no side effects ■ (^) ML (SML/NJ, O’Caml) – Call-by-value, with side effects

• Still around: LISP, Scheme

■ Disadvantage/advantage: No static type systems

Functional Programming Today

• Read-eval-print loop

• Basic data types (int, string)

• Let and letrec

• Lists

• Partial application (“upward funargs”)

■ A partially-applied function is called a closure

• Data structures (tagged unions)

O’Caml Tutorial