Lambda Calculus Akim Demaille akim@lrde.epita.fr EPITA cole Pour - - PowerPoint PPT Presentation

lambda calculus
SMART_READER_LITE
LIVE PREVIEW

Lambda Calculus Akim Demaille akim@lrde.epita.fr EPITA cole Pour - - PowerPoint PPT Presentation

Lambda Calculus Akim Demaille akim@lrde.epita.fr EPITA cole Pour lInformatique et les Techniques Avances June 10, 2016 About these lecture notes Many of these slides are largely inspired from Andrew D. Kers lecture notes [Ker,


slide-1
SLIDE 1

Lambda Calculus

Akim Demaille akim@lrde.epita.fr

EPITA — École Pour l’Informatique et les Techniques Avancées

June 10, 2016

slide-2
SLIDE 2

About these lecture notes

Many of these slides are largely inspired from Andrew D. Ker’s lecture notes [Ker, 2005a, Ker, 2005b]. Some slides are even straightforward copies.

  • A. Demaille

Lambda Calculus 2 / 75

slide-3
SLIDE 3

Lambda Calculus

1

λ-calculus

2

Reduction

3

λ-calculus as a Programming Language

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 3 / 75

slide-4
SLIDE 4

λ-calculus

1

λ-calculus The Syntax of λ-calculus Substitution, Conversions

2

Reduction

3

λ-calculus as a Programming Language

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 4 / 75

slide-5
SLIDE 5

Why the λ-calculus?

Church, Curry A theory of functions (1930s). Turing A definition of effective computability (1930s). Brouwer, Heyting, Kolmogorov A representation of formal proofs (1920–). McCarthy, Scott, . . . A basis for functional programming languages (1960s–). Montague, . . . Semantics for natural language (1960s–).

  • A. Demaille

Lambda Calculus 5 / 75

slide-6
SLIDE 6

λ-calculus

Alonzo Church (1903–1995)

  • A. Demaille

Lambda Calculus 6 / 75

slide-7
SLIDE 7

λ-calculus

Haskell Brooks Curry (1900–1982)

  • A. Demaille

Lambda Calculus 7 / 75

slide-8
SLIDE 8

λ-calculus

Alan Mathison Turing (1912–1954)

  • A. Demaille

Lambda Calculus 8 / 75

slide-9
SLIDE 9

λ-calculus

Richard Merritt Montague (1930–1971)

  • A. Demaille

Lambda Calculus 9 / 75

slide-10
SLIDE 10

What is the λ-calculus?

A mathematical theory of functions A (functional) programming language It allows reasoning on operational semantics Mathematicians are more inclined to denotational semantics

  • A. Demaille

Lambda Calculus 10 / 75

slide-11
SLIDE 11

The Syntax of λ-calculus

1

λ-calculus The Syntax of λ-calculus Substitution, Conversions

2

Reduction

3

λ-calculus as a Programming Language

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 11 / 75

slide-12
SLIDE 12

The Pure Untyped λ-calculus

The simplest λ-calculus: Variables x, y, z... Functions λx · M Application MN No Booleans Numbers Types . . .

  • A. Demaille

Lambda Calculus 12 / 75

slide-13
SLIDE 13

The Pure Untyped λ-calculus

The simplest λ-calculus: Variables x, y, z... Functions λx · M Application MN No Booleans Numbers Types . . .

  • A. Demaille

Lambda Calculus 12 / 75

slide-14
SLIDE 14

The λ-calculus Language

The λ-terms: M ::= x | (λx · M) | (MM) Conventions: Omit outer parentheses MN = (MN) Application associates to the left MNL = (MN)L Abstraction associates to the right λx · MN = λx · (MN) Multiple arguments as syntactic sugar λxy · M = λx · λy · M (Currying EN — Currification FR)

  • A. Demaille

Lambda Calculus 13 / 75

slide-15
SLIDE 15

The λ-calculus Language

The λ-terms: M ::= x | (λx · M) | (MM) Conventions: Omit outer parentheses MN = (MN) Application associates to the left MNL = (MN)L Abstraction associates to the right λx · MN = λx · (MN) Multiple arguments as syntactic sugar λxy · M = λx · λy · M (Currying EN — Currification FR)

  • A. Demaille

Lambda Calculus 13 / 75

slide-16
SLIDE 16

The λ-calculus Language

The λ-terms: M ::= x | (λx · M) | (MM) Conventions: Omit outer parentheses MN = (MN) Application associates to the left MNL = (MN)L Abstraction associates to the right λx · MN = λx · (MN) Multiple arguments as syntactic sugar λxy · M = λx · λy · M (Currying EN — Currification FR)

  • A. Demaille

Lambda Calculus 13 / 75

slide-17
SLIDE 17

The λ-calculus Language

The λ-terms: M ::= x | (λx · M) | (MM) Conventions: Omit outer parentheses MN = (MN) Application associates to the left MNL = (MN)L Abstraction associates to the right λx · MN = λx · (MN) Multiple arguments as syntactic sugar λxy · M = λx · λy · M (Currying EN — Currification FR)

  • A. Demaille

Lambda Calculus 13 / 75

slide-18
SLIDE 18

The λ-calculus Language

The λ-terms: M ::= x | (λx · M) | (MM) Conventions: Omit outer parentheses MN = (MN) Application associates to the left MNL = (MN)L Abstraction associates to the right λx · MN = λx · (MN) Multiple arguments as syntactic sugar λxy · M = λx · λy · M (Currying EN — Currification FR)

  • A. Demaille

Lambda Calculus 13 / 75

slide-19
SLIDE 19

The λ-calculus Language

The λ-terms: M ::= x | (λx · M) | (MM) Conventions: Omit outer parentheses MN = (MN) Application associates to the left MNL = (MN)L Abstraction associates to the right λx · MN = λx · (MN) Multiple arguments as syntactic sugar λxy · M = λx · λy · M (Currying EN — Currification FR)

  • A. Demaille

Lambda Calculus 13 / 75

slide-20
SLIDE 20

Notation

Usual x → 2x + 1 λ-calculus λx · 2x + 1 Originally ˆx · 2x + 1 Inpiration ˆ x · x = y Transition Λx · 2x + 1

  • A. Demaille

Lambda Calculus 14 / 75

slide-21
SLIDE 21

Which abstract-syntax trees are correct?

app N app app x abs abs var y y x app N app M abs abs var x M x app N M abs abs var y y x app var v app var u abs abs var y y x

  • A. Demaille

Lambda Calculus 15 / 75

slide-22
SLIDE 22

Which abstract-syntax trees are correct?

app N app app x abs abs var y y x app N app M abs abs var x M x app N M abs abs var y y x app var v app var u abs abs var y y x

  • A. Demaille

Lambda Calculus 15 / 75

slide-23
SLIDE 23

Fully qualified form for λnfx · f (nfx)

(λn · (λf · (λx · (f ((nf )x))))) (λx · (λf · (λn · (f ((nf )x))))) (λn·)(λf ·)λx · (f ((nf )x)) (λx · (λf · (λn · f )))((nf )x))

  • A. Demaille

Lambda Calculus 16 / 75

slide-24
SLIDE 24

Fully qualified form for λnfx · f (nfx)

✓ (λn · (λf · (λx · (f ((nf )x))))) ✗ (λx · (λf · (λn · (f ((nf )x))))) ✗ (λn·)(λf ·)λx · (f ((nf )x)) ✗ (λx · (λf · (λn · f )))((nf )x))

  • A. Demaille

Lambda Calculus 16 / 75

slide-25
SLIDE 25

The λ-calculus Language: Alternative Presentation

The set Λ of λ-terms: x ∈ V x ∈ Λ M ∈ Λ N ∈ Λ (MN) ∈ Λ M ∈ Λ x ∈ V (λx · M) ∈ Λ For instance x ∈ Λ (λx · x) ∈ Λ y ∈ Λ ((λx · x)y) ∈ Λ z ∈ Λ (((λx · x)y)z) ∈ Λ (λz · (((λx · x)y)z)) ∈ Λ x ∈ Λ (λz · (((λx · x)y)z))x ∈ Λ

  • A. Demaille

Lambda Calculus 17 / 75

slide-26
SLIDE 26

The λ-calculus Language: Alternative Presentation

The set Λ of λ-terms: x ∈ V x ∈ Λ M ∈ Λ N ∈ Λ (MN) ∈ Λ M ∈ Λ x ∈ V (λx · M) ∈ Λ For instance x ∈ Λ (λx · x) ∈ Λ y ∈ Λ ((λx · x)y) ∈ Λ z ∈ Λ (((λx · x)y)z) ∈ Λ (λz · (((λx · x)y)z)) ∈ Λ x ∈ Λ (λz · (((λx · x)y)z))x ∈ Λ

  • A. Demaille

Lambda Calculus 17 / 75

slide-27
SLIDE 27

Subterms

The set of subterms of M, sub(M): sub(x) := {x} sub(λx · M) := {λx · M} ∪ sub(M) sub(MN) := {MN} ∪ sub(M) ∪ sub(N)

  • A. Demaille

Lambda Calculus 18 / 75

slide-28
SLIDE 28

Variables

The set of free variables of M, FV(M): FV(x) := {x} FV(λx · M) := FV(M) \ {x} FV(MN) := FV(M) ∪ FV(N) A variable is free or bound. A variable may have bound and free occurrences: xλx · x. A term with no free variable is closed. A combinator is a closed term.

  • A. Demaille

Lambda Calculus 19 / 75

slide-29
SLIDE 29

Variables

The set of free variables of M, FV(M): FV(x) := {x} FV(λx · M) := FV(M) \ {x} FV(MN) := FV(M) ∪ FV(N) A variable is free or bound. A variable may have bound and free occurrences: xλx · x. A term with no free variable is closed. A combinator is a closed term.

  • A. Demaille

Lambda Calculus 19 / 75

slide-30
SLIDE 30

Variables

The set of free variables of M, FV(M): FV(x) := {x} FV(λx · M) := FV(M) \ {x} FV(MN) := FV(M) ∪ FV(N) A variable is free or bound. A variable may have bound and free occurrences: xλx · x. A term with no free variable is closed. A combinator is a closed term.

  • A. Demaille

Lambda Calculus 19 / 75

slide-31
SLIDE 31

Variables

The set of free variables of M, FV(M): FV(x) := {x} FV(λx · M) := FV(M) \ {x} FV(MN) := FV(M) ∪ FV(N) A variable is free or bound. A variable may have bound and free occurrences: xλx · x. A term with no free variable is closed. A combinator is a closed term.

  • A. Demaille

Lambda Calculus 19 / 75

slide-32
SLIDE 32

Variables

The set of free variables of M, FV(M): FV(x) := {x} FV(λx · M) := FV(M) \ {x} FV(MN) := FV(M) ∪ FV(N) A variable is free or bound. A variable may have bound and free occurrences: xλx · x. A term with no free variable is closed. A combinator is a closed term.

  • A. Demaille

Lambda Calculus 19 / 75

slide-33
SLIDE 33

Substitution, Conversions

1

λ-calculus The Syntax of λ-calculus Substitution, Conversions

2

Reduction

3

λ-calculus as a Programming Language

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 20 / 75

slide-34
SLIDE 34

α-Conversion

α-conversion

M and N are α-convertible, M ≡ N, iff they differ only by renaming bound variables without introducing captures. λx · x ≡ λy · y xλx · x ≡ xλy · y xλx · x ≡ yλy · y λx · λy · xy ≡ λx · λx · xx From now on α-convertible terms are considered equal.

  • A. Demaille

Lambda Calculus 21 / 75

slide-35
SLIDE 35

α-Conversion

α-conversion

M and N are α-convertible, M ≡ N, iff they differ only by renaming bound variables without introducing captures. λx · x ≡ λy · y xλx · x ≡ xλy · y xλx · x ≡ yλy · y λx · λy · xy ≡ λx · λx · xx From now on α-convertible terms are considered equal.

  • A. Demaille

Lambda Calculus 21 / 75

slide-36
SLIDE 36

α-Conversion

α-conversion

M and N are α-convertible, M ≡ N, iff they differ only by renaming bound variables without introducing captures. λx · x ≡ λy · y xλx · x ≡ xλy · y xλx · x ≡ yλy · y λx · λy · xy ≡ λx · λx · xx From now on α-convertible terms are considered equal.

  • A. Demaille

Lambda Calculus 21 / 75

slide-37
SLIDE 37

The Variable Convention

To avoid nasty capture issues, we will always silently α-convert terms so that no bound variable of a term is a variable (bound or free) of another.

  • A. Demaille

Lambda Calculus 22 / 75

slide-38
SLIDE 38

Substitution

The substitution of x by M in N is denoted [M/x]N. It is a notation, not an operation Intuitively, all the free occurrences of x are replaced by M. For instance [λz · zz/x]λy · xy = λy · (λz · zz)y. There are many notations for substitution: [M/x]N N[M/x] N[x := M] N[x ← M] and even N[x/M]

  • A. Demaille

Lambda Calculus 23 / 75

slide-39
SLIDE 39

Substitution

The substitution of x by M in N is denoted [M/x]N. It is a notation, not an operation Intuitively, all the free occurrences of x are replaced by M. For instance [λz · zz/x]λy · xy = λy · (λz · zz)y. There are many notations for substitution: [M/x]N N[M/x] N[x := M] N[x ← M] and even N[x/M]

  • A. Demaille

Lambda Calculus 23 / 75

slide-40
SLIDE 40

Substitution

The substitution of x by M in N is denoted [M/x]N. It is a notation, not an operation Intuitively, all the free occurrences of x are replaced by M. For instance [λz · zz/x]λy · xy = λy · (λz · zz)y. There are many notations for substitution: [M/x]N N[M/x] N[x := M] N[x ← M] and even N[x/M]

  • A. Demaille

Lambda Calculus 23 / 75

slide-41
SLIDE 41

Substitution

The substitution of x by M in N is denoted [M/x]N. It is a notation, not an operation Intuitively, all the free occurrences of x are replaced by M. For instance [λz · zz/x]λy · xy = λy · (λz · zz)y. There are many notations for substitution: [M/x]N N[M/x] N[x := M] N[x ← M] and even N[x/M]

  • A. Demaille

Lambda Calculus 23 / 75

slide-42
SLIDE 42

Substitution

The substitution of x by M in N is denoted [M/x]N. It is a notation, not an operation Intuitively, all the free occurrences of x are replaced by M. For instance [λz · zz/x]λy · xy = λy · (λz · zz)y. There are many notations for substitution: [M/x]N N[M/x] N[x := M] N[x ← M] and even N[x/M]

  • A. Demaille

Lambda Calculus 23 / 75

slide-43
SLIDE 43

Formal Definition of the Substitution

Substitution

[M/x]x := M [M/x]y := y with x = y [M/x](NL) := ([M/x]N)([M/x]L) [M/x]λy · N := λy · [M/x]N with x = y and y ∈ FV(M) The variable convention allows us to “require” that y ∈ FV(M). Without it: [M/x]λy · N := λy · [M/x]N if x = y and y ∈ FV(M) [M/x]λy · N := λz · [M/x][z/y]N if x = y or y ∈ FV(M)

  • A. Demaille

Lambda Calculus 24 / 75

slide-44
SLIDE 44

Formal Definition of the Substitution

Substitution

[M/x]x := M [M/x]y := y with x = y [M/x](NL) := ([M/x]N)([M/x]L) [M/x]λy · N := λy · [M/x]N with x = y and y ∈ FV(M) The variable convention allows us to “require” that y ∈ FV(M). Without it: [M/x]λy · N := λy · [M/x]N if x = y and y ∈ FV(M) [M/x]λy · N := λz · [M/x][z/y]N if x = y or y ∈ FV(M)

  • A. Demaille

Lambda Calculus 24 / 75

slide-45
SLIDE 45

Formal Definition of the Substitution

Substitution

[M/x]x := M [M/x]y := y with x = y [M/x](NL) := ([M/x]N)([M/x]L) [M/x]λy · N := λy · [M/x]N with x = y and y ∈ FV(M) The variable convention allows us to “require” that y ∈ FV(M). Without it: [M/x]λy · N := λy · [M/x]N if x = y and y ∈ FV(M) [M/x]λy · N := λz · [M/x][z/y]N if x = y or y ∈ FV(M)

  • A. Demaille

Lambda Calculus 24 / 75

slide-46
SLIDE 46

Substitution

[yy/z](λxy · zy) ≡ λxu · (yy)u

  • A. Demaille

Lambda Calculus 25 / 75

slide-47
SLIDE 47

β-Conversion

β-conversion

The β-convertibility between two terms is the relation β defined as: (λx · M)N β [N/x]M for any M, N ∈ Λ.

  • A. Demaille

Lambda Calculus 26 / 75

slide-48
SLIDE 48

The λβ Formal System

It is the “standard” theory of λ-calculus.

The λβ Formal System

M = M M = N N = M M = N N = L M = L M = M′ N = N′ MN = M′N′ M = N λx · M = λx · N (λx · M)N = [N/x]M

  • A. Demaille

Lambda Calculus 27 / 75

slide-49
SLIDE 49

Reduction

1

λ-calculus

2

Reduction β-Reduction Church-Rosser Reduction Strategies

3

λ-calculus as a Programming Language

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 28 / 75

slide-50
SLIDE 50

β-Reduction

1

λ-calculus

2

Reduction β-Reduction Church-Rosser Reduction Strategies

3

λ-calculus as a Programming Language

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 29 / 75

slide-51
SLIDE 51

Reduction

One step R-Reduction from a relation R

The relation →

R is the smallest relation such that:

(M, N) ∈ R M →

R N

M →

R N

ML →

R NL

M →

R N

LM →

R LN

M →

R N

λx · M →

R λx · N

R-Reduction: transitive, reflexive closure

The relation

R is the smallest relation such that:

M →

R N

M

R N

M

R M

M

R N

N

R L

M

R L

  • A. Demaille

Lambda Calculus 30 / 75

slide-52
SLIDE 52

Reduction

One step R-Reduction from a relation R

The relation →

R is the smallest relation such that:

(M, N) ∈ R M →

R N

M →

R N

ML →

R NL

M →

R N

LM →

R LN

M →

R N

λx · M →

R λx · N

R-Reduction: transitive, reflexive closure

The relation

R is the smallest relation such that:

M →

R N

M

R N

M

R M

M

R N

N

R L

M

R L

  • A. Demaille

Lambda Calculus 30 / 75

slide-53
SLIDE 53

β-Reduction

β-Redex

A β-redex is term under the form (λx · M)N.

One step β-Reduction

(λx · M)N →

β [N/x]M

· · ·

β-Reduction

The relation

β is the transitive, reflexive closure of → β .

β-Conversion

The relation ≡

β is the transitive, reflexive, symmetric closure of → β .

  • A. Demaille

Lambda Calculus 31 / 75

slide-54
SLIDE 54

β-Reduction

β-Redex

A β-redex is term under the form (λx · M)N.

One step β-Reduction

(λx · M)N →

β [N/x]M

· · ·

β-Reduction

The relation

β is the transitive, reflexive closure of → β .

β-Conversion

The relation ≡

β is the transitive, reflexive, symmetric closure of → β .

  • A. Demaille

Lambda Calculus 31 / 75

slide-55
SLIDE 55

β-Reduction

β-Redex

A β-redex is term under the form (λx · M)N.

One step β-Reduction

(λx · M)N →

β [N/x]M

· · ·

β-Reduction

The relation

β is the transitive, reflexive closure of → β .

β-Conversion

The relation ≡

β is the transitive, reflexive, symmetric closure of → β .

  • A. Demaille

Lambda Calculus 31 / 75

slide-56
SLIDE 56

β-Reduction

β-Redex

A β-redex is term under the form (λx · M)N.

One step β-Reduction

(λx · M)N →

β [N/x]M

· · ·

β-Reduction

The relation

β is the transitive, reflexive closure of → β .

β-Conversion

The relation ≡

β is the transitive, reflexive, symmetric closure of → β .

  • A. Demaille

Lambda Calculus 31 / 75

slide-57
SLIDE 57

β-Reductions

(λx · x)y →

  • A. Demaille

Lambda Calculus 32 / 75

slide-58
SLIDE 58

β-Reductions

(λx · x)y → y (λx · xx)y →

  • A. Demaille

Lambda Calculus 32 / 75

slide-59
SLIDE 59

β-Reductions

(λx · x)y → y (λx · xx)y → yy (λx · xx)(λx · xx) →

  • A. Demaille

Lambda Calculus 32 / 75

slide-60
SLIDE 60

β-Reductions

(λx · x)y → y (λx · xx)y → yy (λx · xx)(λx · xx) → (λx · xx)(λx · xx) (λx · x(xx))(λx · x(xx)) →

  • A. Demaille

Lambda Calculus 32 / 75

slide-61
SLIDE 61

β-Reductions

(λx · x)y → y (λx · xx)y → yy (λx · xx)(λx · xx) → (λx · xx)(λx · xx) (λx · x(xx))(λx · x(xx)) → (λx · x(xx))

  • (λx · x(xx))(λx · x(xx))
  • A. Demaille

Lambda Calculus 32 / 75

slide-62
SLIDE 62

β-Reductions

(λx · x)y → y (λx · xx)y → yy (λx · xx)(λx · xx) → (λx · xx)(λx · xx) (λx · x(xx))(λx · x(xx)) → (λx · x(xx))

  • (λx · x(xx))(λx · x(xx))
  • Omega Combinators

ω ≡ λx · xx Ω ≡ ωω

≡ λx · x(xx)

  • A. Demaille

Lambda Calculus 32 / 75

slide-63
SLIDE 63

More β-Reductions

(λx · xyx)λz · z →

  • A. Demaille

Lambda Calculus 33 / 75

slide-64
SLIDE 64

More β-Reductions

(λx · xyx)λz · z → (λz · z)y(λz · z) (λx · x)((λy · y)x) →

  • A. Demaille

Lambda Calculus 33 / 75

slide-65
SLIDE 65

More β-Reductions

(λx · xyx)λz · z → (λz · z)y(λz · z) (λx · x)((λy · y)x) → (λx · x)(x) (λx · x)((λy · y)x) →

  • A. Demaille

Lambda Calculus 33 / 75

slide-66
SLIDE 66

More β-Reductions

(λx · xyx)λz · z → (λz · z)y(λz · z) (λx · x)((λy · y)x) → (λx · x)(x) (λx · x)((λy · y)x) → ((λy · y)x) (λx · x)((λy · y)x)

  • A. Demaille

Lambda Calculus 33 / 75

slide-67
SLIDE 67

More β-Reductions

(λx · xyx)λz · z → (λz · z)y(λz · z) (λx · x)((λy · y)x) → (λx · x)(x) (λx · x)((λy · y)x) → ((λy · y)x) (λx · x)((λy · y)x)

→ x (λx · xx)((λx · xx)y)

  • A. Demaille

Lambda Calculus 33 / 75

slide-68
SLIDE 68

More β-Reductions

(λx · xyx)λz · z → (λz · z)y(λz · z) (λx · x)((λy · y)x) → (λx · x)(x) (λx · x)((λy · y)x) → ((λy · y)x) (λx · x)((λy · y)x)

→ x (λx · xx)((λx · xx)y)

→ yy(yy) (λx · xx)((λx · x)y)

  • A. Demaille

Lambda Calculus 33 / 75

slide-69
SLIDE 69

More β-Reductions

(λx · xyx)λz · z → (λz · z)y(λz · z) (λx · x)((λy · y)x) → (λx · x)(x) (λx · x)((λy · y)x) → ((λy · y)x) (λx · x)((λy · y)x)

→ x (λx · xx)((λx · xx)y)

→ yy(yy) (λx · xx)((λx · x)y)

→ yy (λx · x)((λx · xx)y)

  • A. Demaille

Lambda Calculus 33 / 75

slide-70
SLIDE 70

More β-Reductions

(λx · xyx)λz · z → (λz · z)y(λz · z) (λx · x)((λy · y)x) → (λx · x)(x) (λx · x)((λy · y)x) → ((λy · y)x) (λx · x)((λy · y)x)

→ x (λx · xx)((λx · xx)y)

→ yy(yy) (λx · xx)((λx · x)y)

→ yy (λx · x)((λx · xx)y)

→ yy

  • A. Demaille

Lambda Calculus 33 / 75

slide-71
SLIDE 71

More β-Reductions

(λx · xyx)λz · z → (λz · z)y(λz · z) (λx · x)((λy · y)x) → (λx · x)(x) (λx · x)((λy · y)x) → ((λy · y)x) (λx · x)((λy · y)x)

→ x (λx · xx)((λx · xx)y)

→ yy(yy) (λx · xx)((λx · x)y)

→ yy (λx · x)((λx · xx)y)

→ yy Therefore λβ ⊢ (λx · xx)((λx · x)y) = (λx · x)((λx · xx)y)

  • A. Demaille

Lambda Calculus 33 / 75

slide-72
SLIDE 72

Other rules

η-reduction

λx · Mx →

η M

η-expansion

M →

ηexp λx · Mx

  • A. Demaille

Lambda Calculus 34 / 75

slide-73
SLIDE 73

Church-Rosser

1

λ-calculus

2

Reduction β-Reduction Church-Rosser Reduction Strategies

3

λ-calculus as a Programming Language

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 35 / 75

slide-74
SLIDE 74

Normal Forms

Given R, a relation on terms.

R-Normal Form (R-NF)

A term M is in R-Normal Form if there is no N such that M →

R N.

R-Normalizable Term

A term M is R-Normalizable (or has an R-Normal Form) if there exists a term N in R-NF such that M

R N.

R-Strongly Normalization Term

A term M is R-Strongly Normalizable there is no infinite one-step reduction sequence starting from M. I.e., any one-step reduction sequence starting from M ends (on a R-NF term).

  • A. Demaille

Lambda Calculus 36 / 75

slide-75
SLIDE 75

Normal Forms

Given R, a relation on terms.

R-Normal Form (R-NF)

A term M is in R-Normal Form if there is no N such that M →

R N.

R-Normalizable Term

A term M is R-Normalizable (or has an R-Normal Form) if there exists a term N in R-NF such that M

R N.

R-Strongly Normalization Term

A term M is R-Strongly Normalizable there is no infinite one-step reduction sequence starting from M. I.e., any one-step reduction sequence starting from M ends (on a R-NF term).

  • A. Demaille

Lambda Calculus 36 / 75

slide-76
SLIDE 76

Normal Forms

Given R, a relation on terms.

R-Normal Form (R-NF)

A term M is in R-Normal Form if there is no N such that M →

R N.

R-Normalizable Term

A term M is R-Normalizable (or has an R-Normal Form) if there exists a term N in R-NF such that M

R N.

R-Strongly Normalization Term

A term M is R-Strongly Normalizable there is no infinite one-step reduction sequence starting from M. I.e., any one-step reduction sequence starting from M ends (on a R-NF term).

  • A. Demaille

Lambda Calculus 36 / 75

slide-77
SLIDE 77

β-Normal Terms

I = λx · x is in β-NF II has a β-NF β-reduces to I II is β-strongly normalizing Ω is not (weakly) normalizable Ω = (λx · xx)(λx · xx) → (λx · xx)(λx · xx) = Ω KIΩ is weakly normalizable (K = λx · (λy · x)) KIΩ → I KIΩ is not strongly normalizable KIΩ → KIΩ

  • A. Demaille

Lambda Calculus 37 / 75

slide-78
SLIDE 78

β-Normal Terms

I = λx · x is in β-NF II has a β-NF β-reduces to I II is β-strongly normalizing Ω is not (weakly) normalizable Ω = (λx · xx)(λx · xx) → (λx · xx)(λx · xx) = Ω KIΩ is weakly normalizable (K = λx · (λy · x)) KIΩ → I KIΩ is not strongly normalizable KIΩ → KIΩ

  • A. Demaille

Lambda Calculus 37 / 75

slide-79
SLIDE 79

β-Normal Terms

I = λx · x is in β-NF II has a β-NF β-reduces to I II is β-strongly normalizing Ω is not (weakly) normalizable Ω = (λx · xx)(λx · xx) → (λx · xx)(λx · xx) = Ω KIΩ is weakly normalizable (K = λx · (λy · x)) KIΩ → I KIΩ is not strongly normalizable KIΩ → KIΩ

  • A. Demaille

Lambda Calculus 37 / 75

slide-80
SLIDE 80

β-Normal Terms

I = λx · x is in β-NF II has a β-NF β-reduces to I II is β-strongly normalizing Ω is not (weakly) normalizable Ω = (λx · xx)(λx · xx) → (λx · xx)(λx · xx) = Ω KIΩ is weakly normalizable (K = λx · (λy · x)) KIΩ → I KIΩ is not strongly normalizable KIΩ → KIΩ

  • A. Demaille

Lambda Calculus 37 / 75

slide-81
SLIDE 81

β-Normal Terms

I = λx · x is in β-NF II has a β-NF β-reduces to I II is β-strongly normalizing Ω is not (weakly) normalizable Ω = (λx · xx)(λx · xx) → (λx · xx)(λx · xx) = Ω KIΩ is weakly normalizable (K = λx · (λy · x)) KIΩ → I KIΩ is not strongly normalizable KIΩ → KIΩ

  • A. Demaille

Lambda Calculus 37 / 75

slide-82
SLIDE 82

β-Normal Terms

I = λx · x is in β-NF II has a β-NF β-reduces to I II is β-strongly normalizing Ω is not (weakly) normalizable Ω = (λx · xx)(λx · xx) → (λx · xx)(λx · xx) = Ω KIΩ is weakly normalizable (K = λx · (λy · x)) KIΩ → I KIΩ is not strongly normalizable KIΩ → KIΩ

  • A. Demaille

Lambda Calculus 37 / 75

slide-83
SLIDE 83

Normalizing Relation

Normalizing Relation

R is weakly normalizing if every term is R-normalizable. R is strongly normalizing if every term is R-strongly normalizable.

  • A. Demaille

Lambda Calculus 38 / 75

slide-84
SLIDE 84

β-Reduction

Ω is not weakly normalizable β-reduction is not weakly normalizing!

  • A. Demaille

Lambda Calculus 39 / 75

slide-85
SLIDE 85

Reduction Strategy

With a weakly normalizing relation that is not strongly normalizing: some terms are not weakly normalizable but not strongly i.e., some terms can be reduced if you reduce them “properly”

Reduction Strategy

A reduction strategy is a function specifying what is the next one-step reduction to perform.

  • A. Demaille

Lambda Calculus 40 / 75

slide-86
SLIDE 86

Reduction Strategy

With a weakly normalizing relation that is not strongly normalizing: some terms are not weakly normalizable but not strongly i.e., some terms can be reduced if you reduce them “properly”

Reduction Strategy

A reduction strategy is a function specifying what is the next one-step reduction to perform.

  • A. Demaille

Lambda Calculus 40 / 75

slide-87
SLIDE 87

Reduction Strategy

With a weakly normalizing relation that is not strongly normalizing: some terms are not weakly normalizable but not strongly i.e., some terms can be reduced if you reduce them “properly”

Reduction Strategy

A reduction strategy is a function specifying what is the next one-step reduction to perform.

  • A. Demaille

Lambda Calculus 40 / 75

slide-88
SLIDE 88

Confluence

Given R, a relation on terms.

Diamond property

R satisfies the diamond property if M → R N1, M → R N2 implies the

existence of L such that N1 →

R L, N2 → R L.

Church-Rosser

R is Church-Rosser if ∗

R satisfies the diamond property.

R is Church-Rosser if M ∗

R N1, M ∗

R N2 implies the existence of L such

that N1

R L, N2 ∗

R L.

  • A. Demaille

Lambda Calculus 41 / 75

slide-89
SLIDE 89

Confluence

Given R, a relation on terms.

Diamond property

R satisfies the diamond property if M → R N1, M → R N2 implies the

existence of L such that N1 →

R L, N2 → R L.

Church-Rosser

R is Church-Rosser if ∗

R satisfies the diamond property.

R is Church-Rosser if M ∗

R N1, M ∗

R N2 implies the existence of L such

that N1

R L, N2 ∗

R L.

  • A. Demaille

Lambda Calculus 41 / 75

slide-90
SLIDE 90

Confluence

Given R, a relation on terms.

Diamond property

R satisfies the diamond property if M → R N1, M → R N2 implies the

existence of L such that N1 →

R L, N2 → R L.

Church-Rosser

R is Church-Rosser if ∗

R satisfies the diamond property.

R is Church-Rosser if M ∗

R N1, M ∗

R N2 implies the existence of L such

that N1

R L, N2 ∗

R L.

  • A. Demaille

Lambda Calculus 41 / 75

slide-91
SLIDE 91

Confluence

Given R, a relation on terms.

Unique Normal Form Property

R has the unique normal form property if M ∗

R N1, M ∗

R N2 with N1, N2

in normal form, implies N1 ≡ N2.

  • A. Demaille

Lambda Calculus 42 / 75

slide-92
SLIDE 92

Properties

The diamond property implies Church-Rosser. If R is Church-Rosser then M =

R N iff there exists L such that M ∗

R L and N ∗

R L.

If R is Church-Rosser then it has the unique normal form property.

  • A. Demaille

Lambda Calculus 43 / 75

slide-93
SLIDE 93

Properties

The diamond property implies Church-Rosser. If R is Church-Rosser then M =

R N iff there exists L such that M ∗

R L and N ∗

R L.

If R is Church-Rosser then it has the unique normal form property.

  • A. Demaille

Lambda Calculus 43 / 75

slide-94
SLIDE 94

Properties

The diamond property implies Church-Rosser. If R is Church-Rosser then M =

R N iff there exists L such that M ∗

R L and N ∗

R L.

If R is Church-Rosser then it has the unique normal form property.

  • A. Demaille

Lambda Calculus 43 / 75

slide-95
SLIDE 95

λ-calculus has the Church-Rosser Property

β-reduction is Church-Rosser. Any term has (at most) a unique NF.

  • A. Demaille

Lambda Calculus 44 / 75

slide-96
SLIDE 96

λ-calculus has the Church-Rosser Property

β-reduction is Church-Rosser. Any term has (at most) a unique NF.

  • A. Demaille

Lambda Calculus 44 / 75

slide-97
SLIDE 97

Reduction Strategies

1

λ-calculus

2

Reduction β-Reduction Church-Rosser Reduction Strategies

3

λ-calculus as a Programming Language

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 45 / 75

slide-98
SLIDE 98

Reduction Strategy

Reduction Strategy

A reduction strategy is a (partial) function from term to term. If → is a reduction strategy, then any term has a unique maximal reduction sequence.

  • A. Demaille

Lambda Calculus 46 / 75

slide-99
SLIDE 99

Reduction Strategy

Reduction Strategy

A reduction strategy is a (partial) function from term to term. If → is a reduction strategy, then any term has a unique maximal reduction sequence.

  • A. Demaille

Lambda Calculus 46 / 75

slide-100
SLIDE 100

Head Reduction

Head Reduction

The head reduction

h

→ on terms is defined by: λ x · (λy · M)N L h → λ x · [N/y]M L λx1 . . . xn · (λy · M)NL1 . . . Lm

h

→ λx1 . . . xn · [N/y]ML1 . . . Lm n, m ≥ 0 Note that any term has one of the following forms: λ x · (λy · M) L λ x · y L

  • A. Demaille

Lambda Calculus 47 / 75

slide-101
SLIDE 101

Head Reduction

Head Reduction

The head reduction

h

→ on terms is defined by: λ x · (λy · M)N L h → λ x · [N/y]M L λx1 . . . xn · (λy · M)NL1 . . . Lm

h

→ λx1 . . . xn · [N/y]ML1 . . . Lm n, m ≥ 0 Note that any term has one of the following forms: λ x · (λy · M) L λ x · y L

  • A. Demaille

Lambda Calculus 47 / 75

slide-102
SLIDE 102

Head Reduction

KIΩ h → I KΩI

h

→ ΩI

h

→ II

h

→ I xIx h → xx Normal terms have the form: λ x · y L

  • A. Demaille

Lambda Calculus 48 / 75

slide-103
SLIDE 103

Leftmost Reduction

Leftmost Reduction

The leftmost reduction

l

→ performs a single step of β-conversion on the leftmost λx · M. Any head reduction is a leftmost reduction (but not conversly). Leftmost reduction is normalizing.

  • A. Demaille

Lambda Calculus 49 / 75

slide-104
SLIDE 104

Leftmost Reduction

Leftmost Reduction

The leftmost reduction

l

→ performs a single step of β-conversion on the leftmost λx · M. Any head reduction is a leftmost reduction (but not conversly). Leftmost reduction is normalizing.

  • A. Demaille

Lambda Calculus 49 / 75

slide-105
SLIDE 105

Leftmost Reduction

Leftmost Reduction

The leftmost reduction

l

→ performs a single step of β-conversion on the leftmost λx · M. Any head reduction is a leftmost reduction (but not conversly). Leftmost reduction is normalizing.

  • A. Demaille

Lambda Calculus 49 / 75

slide-106
SLIDE 106

λ-calculus as a Programming Language

1

λ-calculus

2

Reduction

3

λ-calculus as a Programming Language Booleans Natural Numbers Pairs Recursion

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 50 / 75

slide-107
SLIDE 107

Booleans

1

λ-calculus

2

Reduction

3

λ-calculus as a Programming Language Booleans Natural Numbers Pairs Recursion

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 51 / 75

slide-108
SLIDE 108

Booleans

How would you code Booleans in λ-calculus? How would you translate if M then N else L? ifMNL Do we need if? What if Booleans were the if? MNL What is true? What is false?

  • A. Demaille

Lambda Calculus 52 / 75

slide-109
SLIDE 109

Booleans

How would you code Booleans in λ-calculus? How would you translate if M then N else L? ifMNL Do we need if? What if Booleans were the if? MNL What is true? What is false?

  • A. Demaille

Lambda Calculus 52 / 75

slide-110
SLIDE 110

Booleans

How would you code Booleans in λ-calculus? How would you translate if M then N else L? ifMNL Do we need if? What if Booleans were the if? MNL What is true? What is false?

  • A. Demaille

Lambda Calculus 52 / 75

slide-111
SLIDE 111

Booleans

How would you code Booleans in λ-calculus? How would you translate if M then N else L? ifMNL Do we need if? What if Booleans were the if? MNL What is true? What is false?

  • A. Demaille

Lambda Calculus 52 / 75

slide-112
SLIDE 112

Booleans

How would you code Booleans in λ-calculus? How would you translate if M then N else L? ifMNL Do we need if? What if Booleans were the if? MNL What is true? What is false?

  • A. Demaille

Lambda Calculus 52 / 75

slide-113
SLIDE 113

Booleans

How would you code Booleans in λ-calculus? How would you translate if M then N else L? ifMNL Do we need if? What if Booleans were the if? MNL What is true? What is false?

  • A. Demaille

Lambda Calculus 52 / 75

slide-114
SLIDE 114

Booleans

How would you code Booleans in λ-calculus? How would you translate if M then N else L? ifMNL Do we need if? What if Booleans were the if? MNL What is true? What is false?

  • A. Demaille

Lambda Calculus 52 / 75

slide-115
SLIDE 115

Booleans

How would you code Booleans in λ-calculus? How would you translate if M then N else L? ifMNL Do we need if? What if Booleans were the if? MNL What is true? What is false?

  • A. Demaille

Lambda Calculus 52 / 75

slide-116
SLIDE 116

Boolean Combinators

Boolean Combinators (Church Booleans)

T := λxy · x F := λxy · y

  • A. Demaille

Lambda Calculus 53 / 75

slide-117
SLIDE 117

Natural Numbers

1

λ-calculus

2

Reduction

3

λ-calculus as a Programming Language Booleans Natural Numbers Pairs Recursion

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 54 / 75

slide-118
SLIDE 118

Church’s Integers

Integers

n := λf · λx · f nx = λf · λx · (f · · · (f

n times

x ) · · · )

n times

2 = λf · λx · f (fx) 3 = λf · λx · f (f (fx))

  • A. Demaille

Lambda Calculus 55 / 75

slide-119
SLIDE 119

Church’s Integers

Operations

succ

succ := λn · λf · λx · f (nfx)

plus

plus := λm · λn · λf · λx · mf (nfx) plus := λm · λn · n succ m plus := λn · n succ

  • A. Demaille

Lambda Calculus 56 / 75

slide-120
SLIDE 120

Pairs

1

λ-calculus

2

Reduction

3

λ-calculus as a Programming Language Booleans Natural Numbers Pairs Recursion

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 57 / 75

slide-121
SLIDE 121

Church’s pairs

Pairs

pair := λxy · λf · fxy first := λp · pT second := λp · pF

  • A. Demaille

Lambda Calculus 58 / 75

slide-122
SLIDE 122

Recursion

1

λ-calculus

2

Reduction

3

λ-calculus as a Programming Language Booleans Natural Numbers Pairs Recursion

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 59 / 75

slide-123
SLIDE 123

Fixed point Combinators

Curry’s Y Combinator

Y := λf · (λx · f (xx))(λx · f (xx))

Turing’s Θ Combinator

Θ := (λxy · y(xxy))(λxy · y(xxy)) There are infinitely many fixed-point combinators.

  • A. Demaille

Lambda Calculus 60 / 75

slide-124
SLIDE 124

Fixed point Combinators

Curry’s Y Combinator

Y := λf · (λx · f (xx))(λx · f (xx))

Turing’s Θ Combinator

Θ := (λxy · y(xxy))(λxy · y(xxy)) There are infinitely many fixed-point combinators.

  • A. Demaille

Lambda Calculus 60 / 75

slide-125
SLIDE 125

Fixed point Combinators

Curry’s Y Combinator

Y := λf · (λx · f (xx))(λx · f (xx)) Y g = (λf · (λx · f (xx))(λx · f (xx))) g →β (λx · g(xx))(λx · g(xx)) →β g((λx · g(xx))(λx · g(xx))) g(Y g) →β g(λf · ((λx · f (xx))(λx · f (xx)))g) →β g(λf · ((λx · f (xx))(λx · f (xx)))g)

  • A. Demaille

Lambda Calculus 61 / 75

slide-126
SLIDE 126

Reduction strategies in Programming Languages

Full beta reductions Reduce any redex. Applicative order The leftmost, innermost redex is always reduced first. Intuitively reduce function “arguments” before the function itself. Applicative

  • rder always attempts to apply functions to normal forms, even when

this is not possible. Normal order The leftmost, outermost redex is reduced first.

  • A. Demaille

Lambda Calculus 62 / 75

slide-127
SLIDE 127

Reduction strategies in Programming Languages

Call by name As normal order, but no reductions are performed inside abstractions. λx · (λx · x)x is in NF. Call by value Only the outermost redexes are reduced: a redex is reduced only when its right hand side has reduced to a value (variable or lambda abstraction). Call by need As normal order, but function applications that would duplicate terms instead name the argument, which is then reduced only “when it is needed”. Called in practical contexts “lazy evaluation”.

  • A. Demaille

Lambda Calculus 63 / 75

slide-128
SLIDE 128

λ-calculus as a Programming Language

Lisp (xkcd 224)

  • A. Demaille

Lambda Calculus 64 / 75

slide-129
SLIDE 129

Combinatory Logic

1

λ-calculus

2

Reduction

3

λ-calculus as a Programming Language

4

Combinatory Logic

  • A. Demaille

Lambda Calculus 65 / 75

slide-130
SLIDE 130

Moses Ilyich Schönfinkel (1889–1942)

Russian logician and mathematician. Member of David Hilbert’s group at the University of Göttingen. Mentally ill and in a sanatorium in 1927. His papers were burned by his neighbors for heating.

  • A. Demaille

Lambda Calculus 66 / 75

slide-131
SLIDE 131

Combinatory Logic

λ-reduction

is complex its implementation is full of subtle pitfalls invented in 1936 by Alonzo Church

Combinatory Logic

a simpler alternative invented by Moses Schönfinkel in 1920’s developed by Haskell Curry in 1925

  • A. Demaille

Lambda Calculus 67 / 75

slide-132
SLIDE 132

Combinatory Logic

λ-reduction

is complex its implementation is full of subtle pitfalls invented in 1936 by Alonzo Church

Combinatory Logic

a simpler alternative invented by Moses Schönfinkel in 1920’s developed by Haskell Curry in 1925

  • A. Demaille

Lambda Calculus 67 / 75

slide-133
SLIDE 133

Combinators

Classic Combinators

S := (λx · (λy · (λz · ((xz)(yz))))) K := (λx · (λy · x)) I := (λx · x) We no longer need λ! SXYZ → XZ(YZ) KXY → X IX → X

  • A. Demaille

Lambda Calculus 68 / 75

slide-134
SLIDE 134

Combinators

The Combinator I

I := (λx · x) IX → X SKKX → KX(KX) → X

  • A. Demaille

Lambda Calculus 69 / 75

slide-135
SLIDE 135

Combinators

The Combinator I

I := (λx · x) IX → X SKKX → KX(KX) → X

  • A. Demaille

Lambda Calculus 69 / 75

slide-136
SLIDE 136

Combinators

The Combinator I

I := (λx · x) IX → X SKKX → KX(KX) → X

  • A. Demaille

Lambda Calculus 69 / 75

slide-137
SLIDE 137

Combinators

The Combinator I

I := (λx · x) IX → X SKKX → KX(KX) → X I = SKK

  • A. Demaille

Lambda Calculus 69 / 75

slide-138
SLIDE 138

Combinatory Logic

S SXYZ → XZ(YZ) (λx · (λy · (λz · ((xz)(yz))))) K KXY → X (λx · (λy · x)) I IX → X (λx · x)

  • A. Demaille

Lambda Calculus 70 / 75

slide-139
SLIDE 139

Combinatory Logic

Combination is left-associative: SKKX = (((SK)K)X) → KX(KX) → X I.e., I = SKK: two symbols and two rules suffice. Same expressive power as λ-calculus.

  • A. Demaille

Lambda Calculus 71 / 75

slide-140
SLIDE 140

Boolean Combinators

Boolean Combinators

T = K F = KI TXY → X FXY → Y KIXY = (((KI)X)Y ) → IY → Y

  • A. Demaille

Lambda Calculus 72 / 75

slide-141
SLIDE 141

The Y Combinator in SKI

Y = S(K(SII))(S(S(KS)K)(K(SII))) The simplest fixed point combinator in SK Y = SSK(S(K(SS(S(SSK))))K by Jan Willem Klop: Yk = (LLLLLLLLLLLLLLLLLLLLLLLLLLLL) where: L = λabcdefghijklmnopqstuvwxyzr(r(thisisafixedpointcombinator))

  • A. Demaille

Lambda Calculus 73 / 75

slide-142
SLIDE 142

The Y Combinator in SKI

Y = S(K(SII))(S(S(KS)K)(K(SII))) The simplest fixed point combinator in SK Y = SSK(S(K(SS(S(SSK))))K by Jan Willem Klop: Yk = (LLLLLLLLLLLLLLLLLLLLLLLLLLLL) where: L = λabcdefghijklmnopqstuvwxyzr(r(thisisafixedpointcombinator))

  • A. Demaille

Lambda Calculus 73 / 75

slide-143
SLIDE 143

The Y Combinator in SKI

Y = S(K(SII))(S(S(KS)K)(K(SII))) The simplest fixed point combinator in SK Y = SSK(S(K(SS(S(SSK))))K by Jan Willem Klop: Yk = (LLLLLLLLLLLLLLLLLLLLLLLLLLLL) where: L = λabcdefghijklmnopqstuvwxyzr(r(thisisafixedpointcombinator))

  • A. Demaille

Lambda Calculus 73 / 75

slide-144
SLIDE 144

Bibliography Notes

[Ker, 2005a] Complete and readable lecture notes on λ-calculus. Uses conventions different from ours. [Ker, 2005b] Additional information, including slides. [Barendregt and Barendsen, 2000] A classical introduction to λ-calculus.

  • A. Demaille

Lambda Calculus 74 / 75

slide-145
SLIDE 145

Bibliography I

Barendregt, H. and Barendsen, E. (2000). Introduction to lambda calculus. http: //www.cs.ru.nl/~erikb/onderwijs/T3/materiaal/lambda.pdf. Ker, A. D. (2005a). Lambda calculus and types. http://web.comlab.ox.ac.uk/oucl/work/andrew.ker/ lambda-calculus-notes-full-v3.pdf. Ker, A. D. (2005b). Lambda calculus notes. http://web.comlab.ox.ac.uk/oucl/work/andrew.ker/.

  • A. Demaille

Lambda Calculus 75 / 75