Lambda calculus A starting point for reasoning about functions - - PowerPoint PPT Presentation

lambda calculus
SMART_READER_LITE
LIVE PREVIEW

Lambda calculus A starting point for reasoning about functions - - PowerPoint PPT Presentation

Lambda calculus A starting point for reasoning about functions The foundation of most functional programming languages, including the Lisp and ML languages v ranges over a countable number of variables e :: = v (variables) | e 1 e 2


slide-1
SLIDE 1

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Lambda calculus

  • A starting point for reasoning about functions
  • The foundation of most functional programming

languages, including the Lisp and ML languages

v ranges over a countable number of variables e ::= v (variables) | e1 e2 (function application) | λv.e (function abstraction)

slide-2
SLIDE 2

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Evaluation

λf .f 1 → λf .f 1 (λf .f ) 1 → 1 (λf .f 1) (λx.x + 1) → (λx.x + 1) 1 → 1 + 1 → 2 (λx.x x) (λy.y y) → (λy.y y) (λy.y y) → (λy.y y) (λy.y y) → (λy.y y) (λy.y y) . . .

slide-3
SLIDE 3

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Reduction

  • Program evaluation uses a substitution semantics.
  • ``To evaluate an application (λv.e1) e2, substi-

tute e2 for v in e1.''

  • Substitution: e1[e2/v] means:

– Substitute e2 for v in e1 – Replace all occurrences of v in e1 with e2

  • Sometimes written e1[v := e2].
slide-4
SLIDE 4

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Single-step evaluation

  • A single-step reduction is just a substitution
  • Called “beta-reduction”

(λv.e1) e2 →β e1[e2/v]

slide-5
SLIDE 5

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Defining substitution

  • Define substitution e1[e2/v] by induction on the

structure of e1 – e1 is a variable v′ (v′ may or may not be the same variable as v) – e1 is a function application e3 e4 – e1 is a function abstraction λv′.e3 (v′ may or may not be the same variable as v)

slide-6
SLIDE 6

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Capture-avoiding substitution

  • v′[e2/v] =
  • e2

if v = v′ v′

  • therwise
  • (e3e4)[e2/v] = (e3[e2/v] e4[e2/v])
  • (λv′.e3)[e2/v] =

   λv′.e3 if v = v′ λv′.e3[e2/v] if v′ ∈ FV (e2) λv′′.e3[v′′/v′][e2/v]

  • therwise, new v′′
slide-7
SLIDE 7

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Variables

  • An occurrence of a variable is a “binding
  • ccurrence” if it is defined by a lambda
  • An occurrence is “bound” if it is in the scope of a

binding occurrence with the same name

  • Other occurrences are “free”
slide-8
SLIDE 8

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Free variables

Free variables are defined by induction (as usual): FV (v) = {v} FV (e1 e2) = FV (e1) ∪ FV (e2) FV (λv.e) = FV (e) − {v} FV (λx.λy.x) = {} FV (λx.x + y) = {y} FV (λx.(λy.x) (λz.y)) = {y}

slide-9
SLIDE 9

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Some sensible properties

  • Any two alpha-equivalent terms have the same

free variables

  • Free variables only decrease during evaluation
  • Beta-reduction works:

If e1 =α e2 and e1 →β e3, then e2 →β e4 and e3 =α e4.

slide-10
SLIDE 10

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

More terminology

(λv.e1) e2 →β e1[e2/v]

  • The left-hand-side (λv.e1) e2 is called a redex.
  • The right-hand-side e1[e2/v] is called the contrac-

tum.

slide-11
SLIDE 11

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Rules, inductive definitions

An inference rule has a set of assumptions above a hor- izontal line, and a single conclusion below the line. assum1 · · · assumn concl dummy rule

slide-12
SLIDE 12

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Multi-step reduction

Redex reduction: (λv.e1) e2 →β e1[e2/v] redex Beta-reduction can be applied in any context: M →β M′ M N →β M′ N app fun N →β N′ M N →β M N′ app arg M →β M′ λv.M →β λv.M′ fun

slide-13
SLIDE 13

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Multi-step reduction M →∗

β M ref

M′ →∗

β M

M →∗

β M′ sym

M →∗

β M′

M′ →∗

β M′′

M →∗

β M′′

trans

slide-14
SLIDE 14

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Problems

  • How is a program evaluated?
  • What are the values (the result of a program

evaluation)

slide-15
SLIDE 15

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Normal forms

  • An expression is in normal form if it contains no

redexes.

  • Head normal form: λv1 . . . λvn.ye1 · · · em
  • Weak head normal form: ye1 · · · em
slide-16
SLIDE 16

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Programs and normal forms

֓ ≡ (λx.x x) (λy.y y) Y ≡ (λf .(λx.f (xx)) (λx.f (xx)) Y f →∗

β

f (Y f ) Y fact ≡ Y (λf .λi. if i = 0 then 1 else i ∗ (f (i − 1))) → λi. if i = 0 then 1 else i ∗ (Y fact (i − 1))

slide-17
SLIDE 17

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Normal forms

  • It is probably best to stop as soon as a functional

form is reached

  • What about evaluation order?
  • Does evaluation order matter?

(λx.1) ֓ → (λx.1) ֓ → (λx.1) ֓ → (λx.1) ֓ → 1

slide-18
SLIDE 18

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Church-Rosser

Church-Rosser: evaluation order doesn't matter.

  • Theorem: if M →∗

β N1 and M →∗ β N2 then there is

a term P such that N1 →∗

β P and N2 →∗ β P.

  • Corollary: if a term has a normal form, the normal

form is unique.

  • Proof: hard.
slide-19
SLIDE 19

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Natural semantics

  • While beta-reduction is sufficient for reasoning

about evaluation, it is not a very good method for automated evaluation

– “Pick any redex in any subterm, reduce it, and continue”

  • Natural semantics: define a procedure for

program evaluation

  • Also called “big-step” semantics (beta-reduction is

“small-step” semantics)

slide-20
SLIDE 20

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Natural semantics

  • Use v to represent a value (in weak head normal

form)

  • If e ↓ v, we say ``e evaluates to the value v''
  • Evaluation rules:

x ↓ x var (λx.e) ↓ (λx.e) abs

slide-21
SLIDE 21

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Natural semantics of application

  • Two forms

– Eager: evaluate the argument first – Lazy: do not evaluate the argument first

e1 ↓ (λx.e3) e2 ↓ v1 e3[v1/x] ↓ v2 e1 e2 ↓ v2 eager e1 ↓ (λx.e3) e3[e2/x] ↓ v2 e1 e2 ↓ v2 lazy

slide-22
SLIDE 22

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Natural semantics

  • Soundness:

Theorem If e ↓ v then e →∗

β v.

Proof techniques:

  • 1. Structural induction.
  • 2. Proof induction.
slide-23
SLIDE 23

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Structural induction

Structural induction is a method for proving properties

  • f all λ terms.

The induction principle is: assume that the induction principle holds for all smaller terms, and prove it for the term. There are three cases to prove a property P(e): var Prove P(x) (base case) abs Prove P(λx.e) from P(e) app Prove P(e1 e2) from P(e1) and P(e2)

slide-24
SLIDE 24

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Proof induction

Proof induction is induction on the length of a proof. To prove a property P(t) for an arbitrary judgment, for each rule s1 · · · sn t prove P(t) from P(s1), . . . , P(sn).

slide-25
SLIDE 25

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Soundness of natural semantics

Theorem If e ↓ v then e →∗

β v.

Proof For trivial cases,

  • If x ↓ x var, then x →∗

β x.

  • If (λx.e) ↓ (λx.e) abs, then (λx.e) →∗

β (λx.e)

slide-26
SLIDE 26

Computation, Computers, and Programs Lambda calculus http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Induction step

Otherwise, suppose the last rule was for an application: e1 ↓ (λx.e3) e3[e2/x] ↓ v2 e1 e2 ↓ v2 lazy Chain these together: e1 e2 Initial program →∗

β

(λx.e3) e2 by induction →∗

β

e3[e2/x] beta reduction →∗

β

v2 by induction