COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A...

39
COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus 1

Transcript of COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A...

Page 1: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

COMP2111 Week 9Term 1, 2019

Introduction to Lambda Calculus

1

Page 2: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Summary

History

Functional programming

Lambda calculus

2

Page 3: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Summary

History

Functional programming

Lambda calculus

3

Page 4: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Entscheidungsproblem1928: David Hilbert asks if there is a “mechanical procedure” that,

given a finite set of first-order formulas T , and and formula ϕ,decides if

T |= ϕ

1936: Alonzo Church and Alan Turing independently show there isn’t

4

Page 5: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Entscheidungsproblem1928: David Hilbert asks if there is a “mechanical procedure” that,

given a finite set of first-order formulas T , and and formula ϕ,decides if

T |= ϕ

1936: Alonzo Church and Alan Turing independently show there isn’t

5

Page 6: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Formally defining an algorithm

Turing: Mechanical process (Turing Machines)

Church: Logical process (Lambda calculus)

Godel: General recursive functions

All approaches are equivalent!

Church-Turing thesis

Every effectively calculable function is equivalent to one computedby a Turing Machine.

6

Page 7: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Formally defining an algorithm

Turing: Mechanical process (Turing Machines)

Church: Logical process (Lambda calculus)

Godel: General recursive functions

All approaches are equivalent!

Church-Turing thesis

Every effectively calculable function is equivalent to one computedby a Turing Machine.

7

Page 8: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus in a nutshell

Everything is a function:

Booleans, numbers, ...

“Computation” is captured with function application andrewriting

Lead to the concept of Functional programming

8

Page 9: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus in a nutshell

Everything is a function:

Booleans, numbers, ...

“Computation” is captured with function application andrewriting

Lead to the concept of Functional programming

9

Page 10: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus in a nutshell

Everything is a function:

Booleans, numbers, ...

“Computation” is captured with function application andrewriting

Lead to the concept of Functional programming

10

Page 11: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus in a nutshell

Everything is a function:

Booleans, numbers, ...

“Computation” is captured with function application andrewriting

Lead to the concept of Functional programming

11

Page 12: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Summary

History

Functional programming

Lambda calculus

12

Page 13: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Functional programming

What is Functional Programming?

Programming paradigm distinct from Imperativeprogramming, Object Oriented programming

Extensively used in academia. Can be found in industry (e.g.Jane Street)

Covered in COMP3161 (others?)

Languages: Haskell, ML, OCaml, Scala

13

Page 14: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Examples

Example

leaves and internal functions from Assignment 1.A tree is either:

Empty: τ

A node with two trees as children: Node(t1, t2)

leaves defined recursively as:

leaves(τ) = 0

leaves(Node(τ, τ)) = 1

leaves(Node(t1, t2)) = leaves(t1) + leaves(t2)

internal defined recursively as:

internal(τ) = −1

internal(Node(τ, τ)) = 0

internal(Node(t1, t2)) = 1 + internal(t1) + internal(t2)

14

Page 15: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Examples

Example

leaves and internal functions from Assignment 1.

In Haskell:

Tree = Empty | Node Tree Tree

l e a v e s Empty = 0l e a v e s ( Node Empty Empty ) = 1l e a v e s ( Node t1 t2 ) = l e a v e s t1 + l e a v e s t2

i n t e r n a l Empty = −1i n t e r n a l ( Node Empty Empty ) = 0i n t e r n a l ( Node t1 t2 ) = 1 + i n t e r n a l t1

+ i n t e r n a l t2

15

Page 16: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Functional programming

Guiding principles:

Everything is a function (more-or-less)

Programs are pure (no side-effects)

Pros/cons:

Easy to prove properties: theoretically well-behaved

Interactivity is complicated: I/O, Error handling

16

Page 17: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Functional programming

Guiding principles:

Everything is a function (more-or-less)

Programs are pure (no side-effects)

Pros/cons:

Easy to prove properties: theoretically well-behaved

Interactivity is complicated: I/O, Error handling

17

Page 18: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Currying

A function of n variables can be viewed as a function of 1 variablethat returns a function of n − 1 variables

Example

Consider f : N2 → N given by f (x , y) = x + 2y .

For every x ∈ N let gx : N→ N be given by gx(y) = x + 2y

Now consider h : N→ (N→ N) given by h(x) = gx . We have:

h(x)(y) = f (x , y)

In general:(A× B → C ) ∼= (A→ (B → C ))

18

Page 19: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Currying

A function of n variables can be viewed as a function of 1 variablethat returns a function of n − 1 variables

Example

Consider f : N2 → N given by f (x , y) = x + 2y .

For every x ∈ N let gx : N→ N be given by gx(y) = x + 2y

Now consider h : N→ (N→ N) given by h(x) = gx . We have:

h(x)(y) = f (x , y)

In general:(A× B → C ) ∼= (A→ (B → C ))

19

Page 20: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Summary

History

Functional programming

Lambda calculus

20

Page 21: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus in a nutshell

Everything is a function of one variable

Booleans, numbers, ...

“Computation” is captured with function application andrewriting

21

Page 22: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus in a nutshell

Everything is a function of one variable

Booleans, numbers, ...

“Computation” is captured with function application andrewriting

22

Page 23: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: formally

SYNTAX: A λ-term is defined recursively as follows:

x is a λ-term for any variable x

(Application) If M and N are λ-terms then MN is a λ-term

(Abstraction) If M is a λ-term then λx .M is a λ-term

SEMANTICS: Intuitively:

MN corresponds to the result of passing N as the argumentto the function M (applying M to N)

λx .M is the definition of a new function that binds x to bethe (independent) variable of the function (e.g. anonymousfunctions)

23

Page 24: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: formally

SYNTAX: A λ-term is defined recursively as follows:

x is a λ-term for any variable x

(Application) If M and N are λ-terms then MN is a λ-term

(Abstraction) If M is a λ-term then λx .M is a λ-term

SEMANTICS: Intuitively:

MN corresponds to the result of passing N as the argumentto the function M (applying M to N)

λx .M is the definition of a new function that binds x to bethe (independent) variable of the function (e.g. anonymousfunctions)

24

Page 25: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: Examples

Example

The following are λ-terms:

λx .(λy .y)

λx .(λy .x)

λx .(λy .xy)

λn.λf .λx .f (nfx)

λp.(λq.(pq)p)

25

Page 26: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Reductions

Reductions are rewrite rules.

α-reductions correspond to variable refactoring:

Rename bound variables, e.g.:

λx .(λy .x)α−→ λz .(λy .z)

26

Page 27: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Reductions

β-reductions correspond to function evaluation (i.e.computation):

Only applies to λ-terms of the form M ′N where M ′ is of theform λx .M

Substitute occurrences of x with N, that is:

(λx .M)Nβ−→ M[N/x ]

For example:

(λx .xx)(λy .y)β−→ (λy .y)(λy .y)

β−→ (λy .y)

27

Page 28: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Reductions

β-reductions correspond to function evaluation (i.e.computation):

Only applies to λ-terms of the form M ′N where M ′ is of theform λx .M

Substitute occurrences of x with N, that is:

(λx .M)Nβ−→ M[N/x ]

For example:

(λx .xx)(λy .y)β−→ (λy .y)(λy .y)

β−→ (λy .y)

28

Page 29: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Reductions

β-reductions correspond to function evaluation (i.e.computation):

Only applies to λ-terms of the form M ′N where M ′ is of theform λx .M

Substitute occurrences of x with N, that is:

(λx .M)Nβ−→ M[N/x ]

For example:

(λx .xx)(λy .y)β−→ (λy .y)(λy .y)

β−→ (λy .y)

29

Page 30: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: ExamplesExample

Consider the following λ-terms:

Y = λx .(λy .y)

X = λx .(λy .x)

A = λp.(λq.(pq)p)

We have:

(AX )Y = ((λp.(λq.(pq)p))X )Yβ−→ (λq.(Xq)X ))Yβ−→ (XY )Xβ−→ ((λx .(λy .x))Y )Xβ−→ (λy .Y )Xβ−→ Y

30

Page 31: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: ExamplesExample

Consider the following λ-terms:

Y = λx .(λy .y)

X = λx .(λy .x)

A = λp.(λq.(pq)p)

We have:

(AX )Y = ((λp.(λq.(pq)p))X )Yβ−→ (λq.(Xq)X ))Yβ−→ (XY )Xβ−→ ((λx .(λy .x))Y )Xβ−→ (λy .Y )Xβ−→ Y

31

Page 32: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: ExamplesExample

Consider the following λ-terms:

Y = λx .(λy .y)

X = λx .(λy .x)

A = λp.(λq.(pq)p)

We have:

(AX )Y = ((λp.(λq.(pq)p))X )Yβ−→ (λq.(Xq)X ))Yβ−→ (XY )Xβ−→ ((λx .(λy .x))Y )Xβ−→ (λy .Y )Xβ−→ Y

32

Page 33: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: ExamplesExample

Consider the following λ-terms:

Y = λx .(λy .y)

X = λx .(λy .x)

A = λp.(λq.(pq)p)

We have:

(AX )Y = ((λp.(λq.(pq)p))X )Yβ−→ (λq.(Xq)X ))Yβ−→ (XY )Xβ−→ ((λx .(λy .x))Y )Xβ−→ (λy .Y )Xβ−→ Y

33

Page 34: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: ExamplesExample

Consider the following λ-terms:

Y = λx .(λy .y)

X = λx .(λy .x)

A = λp.(λq.(pq)p)

We have:

(AX )Y = ((λp.(λq.(pq)p))X )Yβ−→ (λq.(Xq)X ))Yβ−→ (XY )Xβ−→ ((λx .(λy .x))Y )Xβ−→ (λy .Y )Xβ−→ Y

34

Page 35: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: ExamplesExample

Consider the following λ-terms:

Y = λx .(λy .y)

X = λx .(λy .x)

A = λp.(λq.(pq)p)

We have:

(AX )Y = ((λp.(λq.(pq)p))X )Yβ−→ (λq.(Xq)X ))Yβ−→ (XY )Xβ−→ ((λx .(λy .x))Y )Xβ−→ (λy .Y )Xβ−→ Y

35

Page 36: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: Examples

Example

Consider the following λ-terms:

Y = λx .(λy .y)

X = λx .(λy .x)

A = λp.(λq.(pq)p)

We have:(AX )Y →∗ Y

Similarly we can show:

(AY )X →∗ Y (AY )Y →∗ Y (AX )X →∗ X

So A behaves like ∧ if we view X as true and Y as false

36

Page 37: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: Examples

Example

Consider the following λ-terms:

Y = λx .(λy .y)

S = λn.λf .λx .f (nfx)

It is possible to show

SY →∗ λx .(λy .xy)S(SY ) →∗ λx .(λy .x(xy))

S(S(SY )) →∗ λx .(λy .x(x(xy)))...

......

37

Page 38: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: Examples

Example

What happens if we try to reduce:

(λx .xx)(λx .xx)?

38

Page 39: COMP2111 Week 9 Term 1, 2019 Introduction to Lambda Calculus · Lambda calculus: formally SYNTAX: A -term is de ned recursively as follows: x is a -term for any variable x (Application)

Lambda calculus: Further topics

Normal forms

Typing

Combinators (e.g. defining recursion)

Combinatory logic

39