• Aucun résultat trouvé

Lambda Calculus

N/A
N/A
Protected

Academic year: 2022

Partager "Lambda Calculus"

Copied!
145
0
0

Texte intégral

(1)

Lambda Calculus

Akim Demailleakim@lrde.epita.fr

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

June 10, 2016

(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.

(3)

Lambda Calculus

1 λ-calculus

2 Reduction

3 λ-calculus as a Programming Language

4 Combinatory Logic

(4)

λ-calculus

1 λ-calculus

The Syntax ofλ-calculus Substitution, Conversions

2 Reduction

3 λ-calculus as a Programming Language

4 Combinatory Logic

(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–).

(6)

λ-calculus

(7)

λ-calculus

(8)

λ-calculus

(9)

λ-calculus

(10)

What is the λ-calculus?

A mathematical theory of functions A (functional) programming language It allows reasoning on operationalsemantics

Mathematicians are more inclined to denotationalsemantics

(11)

The Syntax of λ-calculus

1 λ-calculus

The Syntax ofλ-calculus Substitution, Conversions

2 Reduction

3 λ-calculus as a Programming Language

4 Combinatory Logic

(12)

The Pure Untyped λ-calculus

The simplestλ-calculus:

Variables x,y,z...

Functions λx·M Application MN

No

Booleans Numbers Types . . .

(13)

The Pure Untyped λ-calculus

The simplestλ-calculus:

Variables x,y,z...

Functions λx·M Application MN

No

Booleans Numbers Types . . .

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

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

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

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

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

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

(20)

Notation

Usual x 7→2x+1 λ-calculus λx·2x+1

Originally ˆx·2x+1 Inpiration xˆ·x =y Transition Λx·2x+1

(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

(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

(23)

Fully qualified form for λnfx · f (nfx)

3

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

7

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

7

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

7

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

(24)

Fully qualified form for λnfx · f (nfx)

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

(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 ∈Λ

(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 ∈Λ

(27)

Subterms

The set of subtermsofM,sub(M):

sub(x) :={x}

sub(λx·M) :={λx·M} ∪sub(M) sub(MN) :={MN} ∪sub(M)∪sub(N)

(28)

Variables

The set offree variables ofM,FV(M):

FV(x) :={x}

FV(λx·M) :=FV(M)\ {x}

FV(MN) :=FV(M)∪FV(N)

A variable isfree orbound.

A variable may have bound andfree occurrences: xλx·x.

A term with no free variable isclosed.

Acombinatoris a closed term.

(29)

Variables

The set offree variables ofM,FV(M):

FV(x) :={x}

FV(λx·M) :=FV(M)\ {x}

FV(MN) :=FV(M)∪FV(N)

A variable isfree orbound.

A variable may have bound andfree occurrences: xλx·x.

A term with no free variable isclosed.

Acombinatoris a closed term.

(30)

Variables

The set offree variables ofM,FV(M):

FV(x) :={x}

FV(λx·M) :=FV(M)\ {x}

FV(MN) :=FV(M)∪FV(N)

A variable isfree orbound.

A variable may have bound andfree occurrences: xλx·x.

A term with no free variable isclosed.

Acombinatoris a closed term.

(31)

Variables

The set offree variables ofM,FV(M):

FV(x) :={x}

FV(λx·M) :=FV(M)\ {x}

FV(MN) :=FV(M)∪FV(N)

A variable isfree orbound.

A variable may have bound andfree occurrences: xλx·x.

A term with no free variable isclosed.

Acombinatoris a closed term.

(32)

Variables

The set offree variables ofM,FV(M):

FV(x) :={x}

FV(λx·M) :=FV(M)\ {x}

FV(MN) :=FV(M)∪FV(N)

A variable isfree orbound.

A variable may have bound andfree occurrences: xλx·x.

A term with no free variable isclosed.

Acombinatoris a closed term.

(33)

Substitution, Conversions

1 λ-calculus

The Syntax ofλ-calculus Substitution, Conversions

2 Reduction

3 λ-calculus as a Programming Language

4 Combinatory Logic

(34)

α-Conversion

α-conversion

M andN 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 6≡yλy·y λx·λy·xy 6≡λx·λx·xx

From now onα-convertible terms are consideredequal.

(35)

α-Conversion

α-conversion

M andN 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 6≡yλy·y λx·λy·xy 6≡λx·λx·xx From now onα-convertible terms are consideredequal.

(36)

α-Conversion

α-conversion

M andN 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 6≡yλy·y λx·λy·xy 6≡λx·λx·xx From now onα-convertible terms are consideredequal.

(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.

(38)

Substitution

Thesubstitution ofx by M inN is denoted[M/x]N.

It is anotation, not an operation

Intuitively, all the freeoccurrences 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]

(39)

Substitution

Thesubstitution ofx by M inN is denoted[M/x]N.

It is anotation, not an operation

Intuitively, all the freeoccurrences 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]

(40)

Substitution

Thesubstitution ofx by M inN is denoted[M/x]N.

It is anotation, not an operation

Intuitively, all the freeoccurrences 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]

(41)

Substitution

Thesubstitution ofx by M inN is denoted[M/x]N.

It is anotation, not an operation

Intuitively, all the freeoccurrences 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]

(42)

Substitution

Thesubstitution ofx by M inN is denoted[M/x]N.

It is anotation, not an operation

Intuitively, all the freeoccurrences 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]

(43)

Formal Definition of the Substitution

Substitution

[M/x]x:=M

[M/x]y:=y withx 6=y

[M/x](NL) := ([M/x]N)([M/x]L)

[M/x]λy·N:=λy·[M/x]N withx 6=y and y6∈FV(M) The variable convention allows us to “require” that y 6∈FV(M).

Without it:

[M/x]λy·N:=λy·[M/x]N if x 6=y and y6∈FV(M) [M/x]λy·N:=λz·[M/x][z/y]N if x 6=y or y ∈FV(M)

(44)

Formal Definition of the Substitution

Substitution

[M/x]x:=M

[M/x]y:=y withx 6=y

[M/x](NL) := ([M/x]N)([M/x]L)

[M/x]λy·N:=λy·[M/x]N withx 6=y and y6∈FV(M) The variable convention allows us to “require” that y 6∈FV(M).

Without it:

[M/x]λy·N:=λy·[M/x]N if x 6=y and y6∈FV(M) [M/x]λy·N:=λz·[M/x][z/y]N if x 6=y or y ∈FV(M)

(45)

Formal Definition of the Substitution

Substitution

[M/x]x:=M

[M/x]y:=y withx 6=y

[M/x](NL) := ([M/x]N)([M/x]L)

[M/x]λy·N:=λy·[M/x]N withx 6=y and y6∈FV(M) The variable convention allows us to “require” that y 6∈FV(M).

Without it:

[M/x]λy·N:=λy·[M/x]N if x 6=y and y6∈FV(M) [M/x]λy·N:=λz·[M/x][z/y]N if x 6=y or y ∈FV(M)

(46)

Substitution

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

(47)

β-Conversion

β-conversion

Theβ-convertibility between two terms is the relation β defined as:

(λx·M)N β [N/x]M for any M,N∈Λ.

(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 =M0 N =N0

MN =M0N0

M =N λx·M =λx·N

(λx·M)N= [N/x]M

(49)

Reduction

1 λ-calculus

2 Reduction β-Reduction Church-Rosser Reduction Strategies

3 λ-calculus as a Programming Language

4 Combinatory Logic

(50)

β-Reduction

1 λ-calculus

2 Reduction β-Reduction Church-Rosser Reduction Strategies

3 λ-calculus as a Programming Language

4 Combinatory Logic

(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 → N N → L

(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 → N M →

R M

M →

R N N →

R L M → L

(53)

β-Reduction

β-Redex

Aβ-redexis 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 →

β.

(54)

β-Reduction

β-Redex

Aβ-redexis 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

(55)

β-Reduction

β-Redex

Aβ-redexis 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 →

β.

(56)

β-Reduction

β-Redex

Aβ-redexis 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

(57)

β-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

Ω ≡ ωω

Ωe ≡ λx·x(xx)

(58)

β-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

Ω ≡ ωω

Ωe ≡ λx·x(xx)

(59)

β-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

Ω ≡ ωω

Ωe ≡ λx·x(xx)

(60)

β-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

Ω ≡ ωω

Ωe ≡ λx·x(xx)

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

Omega Combinators

ω ≡ λx·xx

Ω ≡ ωω

Ωe ≡ λx·x(xx)

(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

Ω ≡ ωω

Ωe ≡ λx·x(xx)

(63)

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)

(64)

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)

(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) →

((λ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)

(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) →

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)

(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) →

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)

(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) →

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

Therefore

λβ`(λx·xx)((λx·x)y) = (λx·x)((λx·xx)y)

(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) →

yy

Therefore

λβ`(λx·xx)((λx·x)y) = (λx·x)((λx·xx)y)

(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

Therefore

λβ`(λx·xx)((λx·x)y) = (λx·x)((λx·xx)y)

(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

(72)

Other rules

η-reduction

λx·Mx →

η M η-expansion

M →

ηexp

λx·Mx

(73)

Church-Rosser

1 λ-calculus

2 Reduction β-Reduction Church-Rosser Reduction Strategies

3 λ-calculus as a Programming Language

4 Combinatory Logic

(74)

Normal Forms

GivenR, a relation on terms.

R-Normal Form (R-NF)

A termM is in R-Normal Form if there is noN such that M →

R N.

R-Normalizable Term

A termM is R-Normalizable(or has anR-Normal Form) if there exists a termN inR-NF such thatM →

R N.

R-Strongly Normalization Term

A termM is R-Strongly Normalizable there is no infinite one-step

reduction sequence starting from M. I.e., any one-step reduction sequence

(75)

Normal Forms

GivenR, a relation on terms.

R-Normal Form (R-NF)

A termM is in R-Normal Form if there is noN such that M →

R N.

R-Normalizable Term

A termM is R-Normalizable(or has anR-Normal Form) if there exists a termN inR-NF such thatM →

R N.

R-Strongly Normalization Term

A termM is R-Strongly Normalizable there is no infinite one-step

(76)

Normal Forms

GivenR, a relation on terms.

R-Normal Form (R-NF)

A termM is in R-Normal Form if there is noN such that M →

R N.

R-Normalizable Term

A termM is R-Normalizable(or has anR-Normal Form) if there exists a termN inR-NF such thatM →

R N.

R-Strongly Normalization Term

A termM is R-Strongly Normalizable there is no infinite one-step

reduction sequence starting from M. I.e., any one-step reduction sequence

(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Ω

(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Ω

(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Ω

(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Ω

(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Ω

(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Ω

(83)

Normalizing Relation

Normalizing Relation

R isweakly normalizingif every term isR-normalizable.

R isstrongly normalizing if every term isR-strongly normalizable.

(84)

β-Reduction

Ωis not weakly normalizable

β-reduction is not weakly normalizing!

(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 termscan be reducedifyou reduce them “properly”

Reduction Strategy

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

(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 termscan be reducedifyou reduce them “properly”

Reduction Strategy

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

(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 termscan be reducedifyou reduce them “properly”

Reduction Strategy

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

(88)

Confluence

GivenR, a relation on terms.

Diamond property

R satisfies the diamond propertyifM →

R N1,M →

R N2 implies the existence of Lsuch 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 ofLsuch that N1

R L,N2

R L.

(89)

Confluence

GivenR, a relation on terms.

Diamond property

R satisfies the diamond propertyifM →

R N1,M →

R N2 implies the existence of Lsuch 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 ofLsuch

(90)

Confluence

GivenR, a relation on terms.

Diamond property

R satisfies the diamond propertyifM →

R N1,M →

R N2 implies the existence of Lsuch 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 ofLsuch that N1

R L,N2

R L.

(91)

Confluence

GivenR, a relation on terms.

Unique Normal Form Property

R has the unique normal form propertyifM →

R N1,M →

R N2 withN1,N2 in normal form, implies N1≡N2.

(92)

Properties

The diamond property implies Church-Rosser.

If R is Church-Rosser thenM =

R N iff there exists Lsuch that M →

R LandN →

R L.

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

(93)

Properties

The diamond property implies Church-Rosser.

If R is Church-Rosser thenM =

R N iff there exists Lsuch that M →

R LandN →

R L.

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

(94)

Properties

The diamond property implies Church-Rosser.

If R is Church-Rosser thenM =

R N iff there exists Lsuch that M →

R LandN →

R L.

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

(95)

λ-calculus has the Church-Rosser Property

β-reduction is Church-Rosser.

Any term has (at most) a unique NF.

(96)

λ-calculus has the Church-Rosser Property

β-reduction is Church-Rosser.

Any term has (at most) a unique NF.

(97)

Reduction Strategies

1 λ-calculus

2 Reduction β-Reduction Church-Rosser Reduction Strategies

3 λ-calculus as a Programming Language

4 Combinatory Logic

(98)

Reduction Strategy

Reduction Strategy

Areduction strategy is a (partial)functionfrom term to term.

If → is a reduction strategy, then any term has a unique maximal reduction sequence.

(99)

Reduction Strategy

Reduction Strategy

Areduction strategy is a (partial)functionfrom term to term.

If → is a reduction strategy, then any term has a unique maximal reduction sequence.

(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

(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

(102)

Head Reduction

KIΩ→h I KΩI →h ΩI

h II

h I xIx 6→h xx Normal terms have the form:

λ~x·y~L

(103)

Leftmost Reduction

Leftmost Reduction

Theleftmost 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.

(104)

Leftmost Reduction

Leftmost Reduction

Theleftmost 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.

(105)

Leftmost Reduction

Leftmost Reduction

Theleftmost 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.

(106)

λ-calculus as a Programming Language

1 λ-calculus

2 Reduction

3 λ-calculus as a Programming Language Booleans

Natural Numbers Pairs

Recursion

4 Combinatory Logic

(107)

Booleans

1 λ-calculus

2 Reduction

3 λ-calculus as a Programming Language Booleans

Natural Numbers Pairs

Recursion

4 Combinatory Logic

(108)

Booleans

How would you code Booleans inλ-calculus?

How would you translateif M then N else L?

ifMNL

Do we need if?

What if Booleanswerethe if?

MNL

What is true?

What is false?

(109)

Booleans

How would you code Booleans inλ-calculus?

How would you translateif M then N else L?

ifMNL

Do we need if?

What if Booleanswerethe if?

MNL

What is true?

What is false?

(110)

Booleans

How would you code Booleans inλ-calculus?

How would you translateif M then N else L?

ifMNL

Do we need if?

What if Booleanswerethe if?

MNL

What is true?

What is false?

(111)

Booleans

How would you code Booleans inλ-calculus?

How would you translateif M then N else L?

ifMNL

Do we need if?

What if Booleanswerethe if?

MNL

What is true?

What is false?

(112)

Booleans

How would you code Booleans inλ-calculus?

How would you translateif M then N else L?

ifMNL

Do we need if?

What if Booleanswerethe if?

MNL

What is true?

What is false?

(113)

Booleans

How would you code Booleans inλ-calculus?

How would you translateif M then N else L?

ifMNL

Do we need if?

What if Booleanswerethe if?

MNL

What is true?

What is false?

(114)

Booleans

How would you code Booleans inλ-calculus?

How would you translateif M then N else L?

ifMNL

Do we need if?

What if Booleanswerethe if?

MNL

What is true?

What is false?

(115)

Booleans

How would you code Booleans inλ-calculus?

How would you translateif M then N else L?

ifMNL

Do we need if?

What if Booleanswerethe if?

MNL

What is true?

What is false?

(116)

Boolean Combinators

Boolean Combinators (Church Booleans) T :=λxy ·x F :=λxy ·y

(117)

Natural Numbers

1 λ-calculus

2 Reduction

3 λ-calculus as a Programming Language Booleans

Natural Numbers Pairs

Recursion

4 Combinatory Logic

(118)

Church’s Integers

Integers

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

| {z }

ntimes

x )· · ·)

| {z }

ntimes

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

(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

(120)

Pairs

1 λ-calculus

2 Reduction

3 λ-calculus as a Programming Language Booleans

Natural Numbers Pairs

Recursion

4 Combinatory Logic

(121)

Church’s pairs

Pairs

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

(122)

Recursion

1 λ-calculus

2 Reduction

3 λ-calculus as a Programming Language Booleans

Natural Numbers Pairs

Recursion

4 Combinatory Logic

(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.

(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.

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

(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 order always attempts to apply functions to normal forms, even when this is not possible.

Normal order

The leftmost, outermost redex is reduced first.

(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”.

(128)

λ-calculus as a Programming Language

Lisp (xkcd 224)

(129)

Combinatory Logic

1 λ-calculus

2 Reduction

3 λ-calculus as a Programming Language

4 Combinatory Logic

(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.

(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

(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

(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

(134)

Combinators

The Combinator I

I := (λx·x)

IX →X

SKKX →KX(KX)→X

I =SKK

(135)

Combinators

The Combinator I

I := (λx·x)

IX →X

SKKX →KX(KX)→X

I =SKK

(136)

Combinators

The Combinator I

I := (λx·x)

IX →X

SKKX →KX(KX)→X

I =SKK

(137)

Combinators

The Combinator I

I := (λx·x)

IX →X

SKKX →KX(KX)→X

(138)

Combinatory Logic

S SXYZ →XZ(YZ)

(λx·(λy·(λz·((xz)(yz))))) K KXY →X

(λx·(λy·x)) I IX →X

(λx·x)

(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.

(140)

Boolean Combinators

Boolean Combinators

T =K F =KI

TXY →X FXY →Y

KIXY = (((KI)X)Y)→IY →Y

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

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

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

(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.

(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.

Références

Documents relatifs

Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner.. The Untyped

Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner.. Typed

L’archive ouverte pluridisciplinaire HAL, est destinée au dépôt et à la diffusion de documents scientifiques de niveau recherche, publiés ou non, émanant des

The (search) rule asserts that the type of the extracted method body of n is a func- tion that accept as input argument an object, which will contains the methods m, n, together

For the sake of completeness, Figure 6 sketches the ordinary CBV translation from λ -terms (possibly with iterated applications) to proof nets (including the case for

We show that the permutative λ -calculus preserves β -strong normalisation and is Church-Rosser modulo the equivalences, the strongest possible form of confluence for a

We construct a counter-example as combination of an untyped fixpoint and a may non deterministic argument to create a term of infinite range whose behavior will not respect

Only the outermost redexes are reduced: a redex is reduced only when its right hand side has reduced to a value (variable or