183
Lambda Calculus Dustin Mulcahey

Lambda Calculus by Dustin Mulcahey

Embed Size (px)

DESCRIPTION

This is a friendly Lambda Calculus Introduction by Dustin Mulcahey. LISP has its syntactic roots in a formal system called the lambda calculus. After a brief discussion of formal systems and logic in general, Dustin will dive in to the lambda calculus and make enough constructions to convince you that it really is capable of expressing anything that is "computable". Dustin then talks about the simply typed lambda calculus and the Curry-Howard-Lambek correspondence, which asserts that programs and mathematical proofs are "the same thing".

Citation preview

Page 1: Lambda Calculus by Dustin Mulcahey

Lambda CalculusDustin Mulcahey

Page 2: Lambda Calculus by Dustin Mulcahey

First, a crash course in mathematicallogic...

Page 3: Lambda Calculus by Dustin Mulcahey

First, a crash course in mathematicallogic...

Page 4: Lambda Calculus by Dustin Mulcahey

First, a crash course in mathematicallogic...

Page 5: Lambda Calculus by Dustin Mulcahey

First, a crash course in mathematicallogic...

Page 6: Lambda Calculus by Dustin Mulcahey

For computer scientists, the most interesting part ofthis discussion is Hilbert’s Entscheidungsproblem.

Entscheidungsproblem: Given a mathematicalstatement, is there an algorithm that will compute aproof or a refutation of that statement?

At the time of its statement by Hilbert, there wasno formalization of “algorithm”.

Page 7: Lambda Calculus by Dustin Mulcahey

For computer scientists, the most interesting part ofthis discussion is Hilbert’s Entscheidungsproblem.

Entscheidungsproblem: Given a mathematicalstatement, is there an algorithm that will compute aproof or a refutation of that statement?

At the time of its statement by Hilbert, there wasno formalization of “algorithm”.

Page 8: Lambda Calculus by Dustin Mulcahey

For computer scientists, the most interesting part ofthis discussion is Hilbert’s Entscheidungsproblem.

Entscheidungsproblem: Given a mathematicalstatement, is there an algorithm that will compute aproof or a refutation of that statement?

At the time of its statement by Hilbert, there wasno formalization of “algorithm”.

Page 9: Lambda Calculus by Dustin Mulcahey

For computer scientists, the most interesting part ofthis discussion is Hilbert’s Entscheidungsproblem.

Entscheidungsproblem: Given a mathematicalstatement, is there an algorithm that will compute aproof or a refutation of that statement?

At the time of its statement by Hilbert, there wasno formalization of “algorithm”.

Page 10: Lambda Calculus by Dustin Mulcahey

Fast-forward to the late 1920s...

Schoenfinkel: “Bound variables are bad. (or, atleast, unnecessary)”

Page 11: Lambda Calculus by Dustin Mulcahey

Fast-forward to the late 1920s...

Schoenfinkel: “Bound variables are bad. (or, atleast, unnecessary)”

Page 12: Lambda Calculus by Dustin Mulcahey

Schoenfinkel defined the basic combinators thatform the “combinatory logic” (SKI). We’ll definethese in terms of the lambda calculus, once we’vedefined that.

Haskell Curry also formulated the concept of“combinator” in his efforts to unambiguously definesubstitution, which had been rather looselydescribed up until his time (and continues to beloosely described up to this day).

Page 13: Lambda Calculus by Dustin Mulcahey

Schoenfinkel defined the basic combinators thatform the “combinatory logic” (SKI). We’ll definethese in terms of the lambda calculus, once we’vedefined that.

Haskell Curry also formulated the concept of“combinator” in his efforts to unambiguously definesubstitution, which had been rather looselydescribed up until his time (and continues to beloosely described up to this day).

Page 14: Lambda Calculus by Dustin Mulcahey

Schoenfinkel also seems to have originated thenotion of “currying” (named after Haskell Curry).This is the idea that you can take a two argumentfunction

F (x , y)

and express it as a one argument function that isvalued in functions:

(f (x))(y)

Page 15: Lambda Calculus by Dustin Mulcahey

Finally, on to Alonzo Church!

Goal: a new formal system for logic based upon thenotion of function application. He wantedsomething “more natural” than Russell-Whiteheador ZF.

Page 16: Lambda Calculus by Dustin Mulcahey

The formal system that he developed is called thelambda calculus.Here is the identity function expressed in thelambda calculus:

λx .x

Page 17: Lambda Calculus by Dustin Mulcahey

The formal system that he developed is called thelambda calculus.Here is the identity function expressed in thelambda calculus:

λx .x

Page 18: Lambda Calculus by Dustin Mulcahey

Why use λ for function abstraction?

Whitehead and Russell used x̂ for class abstraction.If you move the hat off the x , you get ∧x .Apparently, λx was easier to print than ∧x .

At least, that’s how Church told it at one point.Later in life, he claimed that he needed a symboland he just happened to choose λ.

Page 19: Lambda Calculus by Dustin Mulcahey

Why use λ for function abstraction?

Whitehead and Russell used x̂ for class abstraction.If you move the hat off the x , you get ∧x .Apparently, λx was easier to print than ∧x .

At least, that’s how Church told it at one point.Later in life, he claimed that he needed a symboland he just happened to choose λ.

Page 20: Lambda Calculus by Dustin Mulcahey

Why use λ for function abstraction?

Whitehead and Russell used x̂ for class abstraction.If you move the hat off the x , you get ∧x .Apparently, λx was easier to print than ∧x .

At least, that’s how Church told it at one point.Later in life, he claimed that he needed a symboland he just happened to choose λ.

Page 21: Lambda Calculus by Dustin Mulcahey

In a formal system, we must give clear rules aboutwhat sequence of symbols can be produced and howthey can be transformed. It’s very similar todesigning a programming language.

Page 22: Lambda Calculus by Dustin Mulcahey

To formulate the lambda calculus, we must first fixa set of letters that we will use for variables.Typically, we denote these by x , y (we rarely needmore than two). Once this has been done, weinductively define valid lambda terms:

I If x is a variable, then x is a valid lambda term.

I If t is a valid lambda term and x is a variable,then (λx .t) is a valid lambda term. (LambdaAbstraction)

I If t, s are valid lambda terms, then (t s) is avalid lambda term. (Application)

Page 23: Lambda Calculus by Dustin Mulcahey

That’s it! We can now construct all sorts of lambdaterms:

x (variable)(λx .x) (lambda abstraction)

y (variable)(y y) (application)

((λx .x)(y y)) (application)(λy .((λx .x)(y y))) (lambda abstraction)

Page 24: Lambda Calculus by Dustin Mulcahey

While I have given the intuition behind the aboveconstructs, they are mere scribblings on paper untilwe give rules for manipulating the terms. From aproof-theoretic perspective, meaning arises from thereduction rules of the language.

This is quite different from other notions ofmeaning, such as Tarski’s definition of truth ordenotionation semantics (in fact, we shall see thatdenotational semantics turns out to be aninteresting problem for the lambda calculus).

Page 25: Lambda Calculus by Dustin Mulcahey

While I have given the intuition behind the aboveconstructs, they are mere scribblings on paper untilwe give rules for manipulating the terms. From aproof-theoretic perspective, meaning arises from thereduction rules of the language.

This is quite different from other notions ofmeaning, such as Tarski’s definition of truth ordenotionation semantics (in fact, we shall see thatdenotational semantics turns out to be aninteresting problem for the lambda calculus).

Page 26: Lambda Calculus by Dustin Mulcahey

There are three rules for manipulating lambdaterms:

I α-equivalence: renaming variables

I β-reduction: how function application “works”

I η-conversion: two functions are “the same” ifthey do the same thing (extensionality)

Page 27: Lambda Calculus by Dustin Mulcahey

α-equivalence lets us convert λx .x to λy .y .

Makes

sense, right? They are both the identity function.Generally, α-equivalence lets us rename any bound

variables.

Page 28: Lambda Calculus by Dustin Mulcahey

α-equivalence lets us convert λx .x to λy .y . Makes

sense, right? They are both the identity function.Generally, α-equivalence lets us rename any bound

variables.

Page 29: Lambda Calculus by Dustin Mulcahey

As programmers, we use α-equivalence to reasonabout lexical scoping:

x = 0

f = function(x, y) {

return x + y;

}

print f(3,4);

Page 30: Lambda Calculus by Dustin Mulcahey

is equivalent to:

x = 0;

f = function(a, b) {

return a + b;

}

print f(3,4);

Page 31: Lambda Calculus by Dustin Mulcahey

As you can imagine, formally defining α-equivalenceis a bit tricky. We want λx .x α-equivalent to λy .y ,but we do not want λx .(λy .x) α-equivalent toλy .(λy .y).

(The first takes a value and produces the constantfunction at that value, while the second returns theidentity function no matter what’s passed to it.)

Page 32: Lambda Calculus by Dustin Mulcahey

As you can imagine, formally defining α-equivalenceis a bit tricky. We want λx .x α-equivalent to λy .y ,but we do not want λx .(λy .x) α-equivalent toλy .(λy .y).

(The first takes a value and produces the constantfunction at that value, while the second returns theidentity function no matter what’s passed to it.)

Page 33: Lambda Calculus by Dustin Mulcahey

β-reduction captures the notion of functionapplication. However, to formally define it, we runin to the substitution problem again!

Intuitively, we would like (f x) to denote theapplication of a function f to an input x . Of course,in this world, everything has the same “type”, so weare really applying one lambda term to another.

Page 34: Lambda Calculus by Dustin Mulcahey

β-reduction captures the notion of functionapplication. However, to formally define it, we runin to the substitution problem again!

Intuitively, we would like (f x) to denote theapplication of a function f to an input x . Of course,in this world, everything has the same “type”, so weare really applying one lambda term to another.

Page 35: Lambda Calculus by Dustin Mulcahey

For a simple example of β-reduction, let’s apply theidentity function to something.

((λx .x)(λy .(y y))

ought to reduce to

(λy .(y y))

Page 36: Lambda Calculus by Dustin Mulcahey

For a simple example of β-reduction, let’s apply theidentity function to something.

((λx .x)(λy .(y y))

ought to reduce to

(λy .(y y))

Page 37: Lambda Calculus by Dustin Mulcahey

How about the other way around?

((λy .(y y))(λx .x)

((λx .x)(λx .x))(λx .x)

Page 38: Lambda Calculus by Dustin Mulcahey

How about the other way around?

((λy .(y y))(λx .x)((λx .x)(λx .x))

(λx .x)

Page 39: Lambda Calculus by Dustin Mulcahey

How about the other way around?

((λy .(y y))(λx .x)((λx .x)(λx .x))

(λx .x)

Page 40: Lambda Calculus by Dustin Mulcahey

We define β-reduction as follows. Let

((λx .t) s)

be a valid lambda term with t and s lambda termsand x a variable. The above reduces to

t[s/x ]

where t[s/x ] denotes the result of replacing everyoccurrence of x in t by s.

Page 41: Lambda Calculus by Dustin Mulcahey

Problem: what if our usage of variables is a bit tooincestuous?

Example:

t = (λz .(x y))s = z

Now apply β-reduction:

((λx .t) s)((λx .(λz .(x y))) z)

(λz .(z y))

Page 42: Lambda Calculus by Dustin Mulcahey

Problem: what if our usage of variables is a bit tooincestuous?

Example:

t = (λz .(x y))s = z

Now apply β-reduction:

((λx .t) s)((λx .(λz .(x y))) z)

(λz .(z y))

Page 43: Lambda Calculus by Dustin Mulcahey

Problem: what if our usage of variables is a bit tooincestuous?

Example:

t = (λz .(x y))s = z

Now apply β-reduction:

((λx .t) s)((λx .(λz .(x y))) z)

(λz .(z y))

Page 44: Lambda Calculus by Dustin Mulcahey

Whereas if we first did α-equivalence:

t = (λw .(x y))s = z

And then apply β-reduction:

((λx .t) s)((λx .(λw .(x y))) z)

(λw .(z y))

The function on the previous slide applies itsparameter to the free variable y , whereas thefunction on this slide does nothing with itsparameter!

Page 45: Lambda Calculus by Dustin Mulcahey

Whereas if we first did α-equivalence:

t = (λw .(x y))s = z

And then apply β-reduction:

((λx .t) s)((λx .(λw .(x y))) z)

(λw .(z y))

The function on the previous slide applies itsparameter to the free variable y , whereas thefunction on this slide does nothing with itsparameter!

Page 46: Lambda Calculus by Dustin Mulcahey

Whereas if we first did α-equivalence:

t = (λw .(x y))s = z

And then apply β-reduction:

((λx .t) s)((λx .(λw .(x y))) z)

(λw .(z y))

The function on the previous slide applies itsparameter to the free variable y , whereas thefunction on this slide does nothing with itsparameter!

Page 47: Lambda Calculus by Dustin Mulcahey

So, obviously some care is needed when definingsubstitution.

We need to ensure that in

((λx .t) s)

that s does not contain a free variable that becomesbound when s is substituted for x in t.

Page 48: Lambda Calculus by Dustin Mulcahey

The next and final reduction expresses themathematical principle of extensionality.

Informally, we say that two functions areextensionally equal if they do the same thing.That is,

f (x) = x + 2g(x) = x + 1 + 1

are two different functions as I have written them,but extensionally equal.

Page 49: Lambda Calculus by Dustin Mulcahey

The next and final reduction expresses themathematical principle of extensionality.Informally, we say that two functions areextensionally equal if they do the same thing.

That is,

f (x) = x + 2g(x) = x + 1 + 1

are two different functions as I have written them,but extensionally equal.

Page 50: Lambda Calculus by Dustin Mulcahey

The next and final reduction expresses themathematical principle of extensionality.Informally, we say that two functions areextensionally equal if they do the same thing.That is,

f (x) = x + 2g(x) = x + 1 + 1

are two different functions as I have written them,but extensionally equal.

Page 51: Lambda Calculus by Dustin Mulcahey

However (as an aside),

f (x) = x2−4x−2

g(x) = x + 2

Are neither equal nor extensionally equal, butalgebraically reduce to the same thing.

Page 52: Lambda Calculus by Dustin Mulcahey

η-conversion captures this notion by stating that, forlambda expressions f not containing the variable x ,

(λx .(f x))

is equivalent to

f

Page 53: Lambda Calculus by Dustin Mulcahey

That’s enough math! Let’s do some programming.

Page 54: Lambda Calculus by Dustin Mulcahey

Well, I can do what any beginning (or intermediate,or advanced) programmer does:

((λx .(x x))(λx .(x x)))

Let’s apply β-reduction:

((λx .(x x))(λx .(x x)))

Yay! An infinite loop!So we see that the true strength of the lambdacalculus is the speed at which we can write downinfinite computations.

Page 55: Lambda Calculus by Dustin Mulcahey

Well, I can do what any beginning (or intermediate,or advanced) programmer does:

((λx .(x x))(λx .(x x)))

Let’s apply β-reduction:

((λx .(x x))(λx .(x x)))

Yay! An infinite loop!So we see that the true strength of the lambdacalculus is the speed at which we can write downinfinite computations.

Page 56: Lambda Calculus by Dustin Mulcahey

Well, I can do what any beginning (or intermediate,or advanced) programmer does:

((λx .(x x))(λx .(x x)))

Let’s apply β-reduction:

((λx .(x x))(λx .(x x)))

Yay! An infinite loop!So we see that the true strength of the lambdacalculus is the speed at which we can write downinfinite computations.

Page 57: Lambda Calculus by Dustin Mulcahey

Or, even better:

(λx .((x x) x)(λx .((x x) x)(((λx .((x x) x) (λx .((x x) x)) (λx .((x x) x))

((((λx .((x x) x) (λx .((x x) x)) (λx .((x x) x)) (λx .((x x) x))...

This example shows that not all lambda termsnormalize. That is, given a lambda term, you can’talways just whack it with β-reduction until it settlesinto something!

Page 58: Lambda Calculus by Dustin Mulcahey

Or, even better:

(λx .((x x) x)(λx .((x x) x)

(((λx .((x x) x) (λx .((x x) x)) (λx .((x x) x))((((λx .((x x) x) (λx .((x x) x)) (λx .((x x) x)) (λx .((x x) x))

...

This example shows that not all lambda termsnormalize. That is, given a lambda term, you can’talways just whack it with β-reduction until it settlesinto something!

Page 59: Lambda Calculus by Dustin Mulcahey

Or, even better:

(λx .((x x) x)(λx .((x x) x)(((λx .((x x) x) (λx .((x x) x)) (λx .((x x) x))

((((λx .((x x) x) (λx .((x x) x)) (λx .((x x) x)) (λx .((x x) x))...

This example shows that not all lambda termsnormalize. That is, given a lambda term, you can’talways just whack it with β-reduction until it settlesinto something!

Page 60: Lambda Calculus by Dustin Mulcahey

Or, even better:

(λx .((x x) x)(λx .((x x) x)(((λx .((x x) x) (λx .((x x) x)) (λx .((x x) x))

((((λx .((x x) x) (λx .((x x) x)) (λx .((x x) x)) (λx .((x x) x))

...

This example shows that not all lambda termsnormalize. That is, given a lambda term, you can’talways just whack it with β-reduction until it settlesinto something!

Page 61: Lambda Calculus by Dustin Mulcahey

Or, even better:

(λx .((x x) x)(λx .((x x) x)(((λx .((x x) x) (λx .((x x) x)) (λx .((x x) x))

((((λx .((x x) x) (λx .((x x) x)) (λx .((x x) x)) (λx .((x x) x))...

This example shows that not all lambda termsnormalize. That is, given a lambda term, you can’talways just whack it with β-reduction until it settlesinto something!

Page 62: Lambda Calculus by Dustin Mulcahey

To make things that are more interesting thannon-terminating programs, we need to define somebasic things. I will now define the following:

I numbers

I booleans and conditionals

I recursion

Page 63: Lambda Calculus by Dustin Mulcahey

To make things that are more interesting thannon-terminating programs, we need to define somebasic things. I will now define the following:

I numbers

I booleans and conditionals

I recursion

Page 64: Lambda Calculus by Dustin Mulcahey

To make things that are more interesting thannon-terminating programs, we need to define somebasic things. I will now define the following:

I numbers

I booleans and conditionals

I recursion

Page 65: Lambda Calculus by Dustin Mulcahey

The standard formulation of the natural numbers iscalled the system of Church Numerals.

Intuition: The number n is n-fold composition.

(Speaking of non-termination...)

Page 66: Lambda Calculus by Dustin Mulcahey

The standard formulation of the natural numbers iscalled the system of Church Numerals.

Intuition: The number n is n-fold composition.

(Speaking of non-termination...)

Page 67: Lambda Calculus by Dustin Mulcahey

The standard formulation of the natural numbers iscalled the system of Church Numerals.

Intuition: The number n is n-fold composition.

(Speaking of non-termination...)

Page 68: Lambda Calculus by Dustin Mulcahey

Less cyclic: The number n is a function that takes afunction and returns the nth-fold composite of thatfunction.

(Hmm, still looks cyclic to me.)That is,

n(f ) = f ◦ f ◦ f ◦ . . . ◦ f

which we can denote as f ◦n.

Page 69: Lambda Calculus by Dustin Mulcahey

Less cyclic: The number n is a function that takes afunction and returns the nth-fold composite of thatfunction.

(Hmm, still looks cyclic to me.)

That is,

n(f ) = f ◦ f ◦ f ◦ . . . ◦ f

which we can denote as f ◦n.

Page 70: Lambda Calculus by Dustin Mulcahey

Less cyclic: The number n is a function that takes afunction and returns the nth-fold composite of thatfunction.

(Hmm, still looks cyclic to me.)That is,

n(f ) = f ◦ f ◦ f ◦ . . . ◦ f

which we can denote as f ◦n.

Page 71: Lambda Calculus by Dustin Mulcahey

Less cyclic: The number n is a function that takes afunction and returns the nth-fold composite of thatfunction.

(Hmm, still looks cyclic to me.)That is,

n(f ) = f ◦ f ◦ f ◦ . . . ◦ f

which we can denote as f ◦n.

Page 72: Lambda Calculus by Dustin Mulcahey

Formally,

0 ≡ f 7→ id ≡ λf .(λx .x)

1 ≡ f 7→ f ≡ λf .(λx .(f x))2 ≡ f 7→ f ◦ f ≡ λf .(λx .(f (f x))

and so on...

Page 73: Lambda Calculus by Dustin Mulcahey

Formally,

0 ≡ f 7→ id ≡ λf .(λx .x)1 ≡ f 7→ f ≡ λf .(λx .(f x))

2 ≡ f 7→ f ◦ f ≡ λf .(λx .(f (f x))and so on...

Page 74: Lambda Calculus by Dustin Mulcahey

Formally,

0 ≡ f 7→ id ≡ λf .(λx .x)1 ≡ f 7→ f ≡ λf .(λx .(f x))2 ≡

f 7→ f ◦ f ≡ λf .(λx .(f (f x))and so on...

Page 75: Lambda Calculus by Dustin Mulcahey

Formally,

0 ≡ f 7→ id ≡ λf .(λx .x)1 ≡ f 7→ f ≡ λf .(λx .(f x))2 ≡ f 7→ f ◦ f

≡ λf .(λx .(f (f x))and so on...

Page 76: Lambda Calculus by Dustin Mulcahey

Formally,

0 ≡ f 7→ id ≡ λf .(λx .x)1 ≡ f 7→ f ≡ λf .(λx .(f x))2 ≡ f 7→ f ◦ f ≡ λf .(λx .(f (f x))

and so on...

Page 77: Lambda Calculus by Dustin Mulcahey

Formally,

0 ≡ f 7→ id ≡ λf .(λx .x)1 ≡ f 7→ f ≡ λf .(λx .(f x))2 ≡ f 7→ f ◦ f ≡ λf .(λx .(f (f x))

and so on...

Page 78: Lambda Calculus by Dustin Mulcahey

There are two rules for constructing naturalnumbers:

0 ≡ λf .(λx .x)

and if n is a natural number, then

n + 1 ≡ succ(n) ≡ λf .λx .(f ((n f ) x)

Page 79: Lambda Calculus by Dustin Mulcahey

There are two rules for constructing naturalnumbers:

0 ≡ λf .(λx .x)

and if n is a natural number, then

n + 1 ≡ succ(n) ≡ λf .λx .(f ((n f ) x)

Page 80: Lambda Calculus by Dustin Mulcahey

There are two rules for constructing naturalnumbers:

0 ≡ λf .(λx .x)

and if n is a natural number, then

n + 1 ≡

succ(n) ≡ λf .λx .(f ((n f ) x)

Page 81: Lambda Calculus by Dustin Mulcahey

There are two rules for constructing naturalnumbers:

0 ≡ λf .(λx .x)

and if n is a natural number, then

n + 1 ≡ succ(n) ≡

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

Page 82: Lambda Calculus by Dustin Mulcahey

There are two rules for constructing naturalnumbers:

0 ≡ λf .(λx .x)

and if n is a natural number, then

n + 1 ≡ succ(n) ≡ λf .λx .(f ((n f ) x)

Page 83: Lambda Calculus by Dustin Mulcahey

Is this definition consistent with what I’ve shownyou?

1 ≡

succ(0)λf .λx .(f ((0 f ) x))λf .λx .(f ((λf .λx .x) f ) x))λf .λx .(f ((λx .x) x))λf .λx .(f x)

Page 84: Lambda Calculus by Dustin Mulcahey

Is this definition consistent with what I’ve shownyou?

1 ≡ succ(0)

λf .λx .(f ((0 f ) x))λf .λx .(f ((λf .λx .x) f ) x))λf .λx .(f ((λx .x) x))λf .λx .(f x)

Page 85: Lambda Calculus by Dustin Mulcahey

Is this definition consistent with what I’ve shownyou?

1 ≡ succ(0)λf .λx .(f ((0 f ) x))

λf .λx .(f ((λf .λx .x) f ) x))λf .λx .(f ((λx .x) x))λf .λx .(f x)

Page 86: Lambda Calculus by Dustin Mulcahey

Is this definition consistent with what I’ve shownyou?

1 ≡ succ(0)λf .λx .(f ((0 f ) x))λf .λx .(f ((λf .λx .x) f ) x))

λf .λx .(f ((λx .x) x))λf .λx .(f x)

Page 87: Lambda Calculus by Dustin Mulcahey

Is this definition consistent with what I’ve shownyou?

1 ≡ succ(0)λf .λx .(f ((0 f ) x))λf .λx .(f ((λf .λx .x) f ) x))λf .λx .(f ((λx .x) x))

λf .λx .(f x)

Page 88: Lambda Calculus by Dustin Mulcahey

Is this definition consistent with what I’ve shownyou?

1 ≡ succ(0)λf .λx .(f ((0 f ) x))λf .λx .(f ((λf .λx .x) f ) x))λf .λx .(f ((λx .x) x))λf .λx .(f x)

Page 89: Lambda Calculus by Dustin Mulcahey

You’ll notice that I’ve suddenly started using thesymbol ≡

If a ≡ b, I’m declaring by the powers of notationthat wherever you write a, you can also write b (andvice versa).

Page 90: Lambda Calculus by Dustin Mulcahey

You’ll notice that I’ve suddenly started using thesymbol ≡If a ≡ b, I’m declaring by the powers of notationthat wherever you write a, you can also write b (andvice versa).

Page 91: Lambda Calculus by Dustin Mulcahey

Also note that succ is itself a lambda term:

succ ≡ λn.λf .λx .(f ((n f ) x))

Here, n is not boldface because I’m using it as avariable. The user of our succ function could putanything there! Of course, we only gaurantee goodbehavior on an input that is equivalent to a naturalnumber (as we have defined them).

Page 92: Lambda Calculus by Dustin Mulcahey

Okay, we have natural numbers. How aboutaddition?

Intuition: n + m takes a function and composes itn + m times. Strategy: Let’s write a lambda termthat applies f m times, “and then”applies it ntimes. In the world of functions, “and then” meanscomposition! So addition corresponds tocomposition.

add ≡ (λn.λm.λf .λx .((n f ) ((m f ) x)))

Page 93: Lambda Calculus by Dustin Mulcahey

Okay, we have natural numbers. How aboutaddition?

Intuition: n + m takes a function and composes itn + m times.

Strategy: Let’s write a lambda termthat applies f m times, “and then”applies it ntimes. In the world of functions, “and then” meanscomposition! So addition corresponds tocomposition.

add ≡ (λn.λm.λf .λx .((n f ) ((m f ) x)))

Page 94: Lambda Calculus by Dustin Mulcahey

Okay, we have natural numbers. How aboutaddition?

Intuition: n + m takes a function and composes itn + m times. Strategy: Let’s write a lambda termthat applies f m times, “and then”applies it ntimes.

In the world of functions, “and then” meanscomposition! So addition corresponds tocomposition.

add ≡ (λn.λm.λf .λx .((n f ) ((m f ) x)))

Page 95: Lambda Calculus by Dustin Mulcahey

Okay, we have natural numbers. How aboutaddition?

Intuition: n + m takes a function and composes itn + m times. Strategy: Let’s write a lambda termthat applies f m times, “and then”applies it ntimes. In the world of functions, “and then” meanscomposition! So addition corresponds tocomposition.

add ≡ (λn.λm.λf .λx .((n f ) ((m f ) x)))

Page 96: Lambda Calculus by Dustin Mulcahey

Okay, we have natural numbers. How aboutaddition?

Intuition: n + m takes a function and composes itn + m times. Strategy: Let’s write a lambda termthat applies f m times, “and then”applies it ntimes. In the world of functions, “and then” meanscomposition! So addition corresponds tocomposition.

add ≡ (λn.λm.λf .λx .((n f ) ((m f ) x)))

Page 97: Lambda Calculus by Dustin Mulcahey

Theorem: ((add 2) 2) is equivalent to 4Proof: (I’m going to use a mixture of definitionalequality and reductions)

((add 2) 2)

(((λn.λm.λf .λx .((n f ) ((m f ) x))) 2) 2)((λm.λf .λx .((2 f ) ((m f ) x))) 2)

(λf .λx .((2 f ) ((2 f ) x)))(λf .λx .(((λf .λx .(f (f x)) f ) ((λf .(λx .(f (f x)) f ) x)))

(λf .λx .((λx .(f (f x)) (λx .(f (f x)) x)))(λf .λx .((λx .(f (f x)) (f (f x))))

(λf .λx .(λf .(f (f (f (f x))))))4

Page 98: Lambda Calculus by Dustin Mulcahey

Theorem: ((add 2) 2) is equivalent to 4Proof: (I’m going to use a mixture of definitionalequality and reductions)

((add 2) 2)(((λn.λm.λf .λx .((n f ) ((m f ) x))) 2) 2)

((λm.λf .λx .((2 f ) ((m f ) x))) 2)(λf .λx .((2 f ) ((2 f ) x)))

(λf .λx .(((λf .λx .(f (f x)) f ) ((λf .(λx .(f (f x)) f ) x)))(λf .λx .((λx .(f (f x)) (λx .(f (f x)) x)))

(λf .λx .((λx .(f (f x)) (f (f x))))(λf .λx .(λf .(f (f (f (f x))))))

4

Page 99: Lambda Calculus by Dustin Mulcahey

Theorem: ((add 2) 2) is equivalent to 4Proof: (I’m going to use a mixture of definitionalequality and reductions)

((add 2) 2)(((λn.λm.λf .λx .((n f ) ((m f ) x))) 2) 2)

((λm.λf .λx .((2 f ) ((m f ) x))) 2)

(λf .λx .((2 f ) ((2 f ) x)))(λf .λx .(((λf .λx .(f (f x)) f ) ((λf .(λx .(f (f x)) f ) x)))

(λf .λx .((λx .(f (f x)) (λx .(f (f x)) x)))(λf .λx .((λx .(f (f x)) (f (f x))))

(λf .λx .(λf .(f (f (f (f x))))))4

Page 100: Lambda Calculus by Dustin Mulcahey

Theorem: ((add 2) 2) is equivalent to 4Proof: (I’m going to use a mixture of definitionalequality and reductions)

((add 2) 2)(((λn.λm.λf .λx .((n f ) ((m f ) x))) 2) 2)

((λm.λf .λx .((2 f ) ((m f ) x))) 2)(λf .λx .((2 f ) ((2 f ) x)))

(λf .λx .(((λf .λx .(f (f x)) f ) ((λf .(λx .(f (f x)) f ) x)))

(λf .λx .((λx .(f (f x)) (λx .(f (f x)) x)))(λf .λx .((λx .(f (f x)) (f (f x))))

(λf .λx .(λf .(f (f (f (f x))))))4

Page 101: Lambda Calculus by Dustin Mulcahey

Theorem: ((add 2) 2) is equivalent to 4Proof: (I’m going to use a mixture of definitionalequality and reductions)

((add 2) 2)(((λn.λm.λf .λx .((n f ) ((m f ) x))) 2) 2)

((λm.λf .λx .((2 f ) ((m f ) x))) 2)(λf .λx .((2 f ) ((2 f ) x)))

(λf .λx .(((λf .λx .(f (f x)) f ) ((λf .(λx .(f (f x)) f ) x)))(λf .λx .((λx .(f (f x)) (λx .(f (f x)) x)))

(λf .λx .((λx .(f (f x)) (f (f x))))(λf .λx .(λf .(f (f (f (f x))))))

4

Page 102: Lambda Calculus by Dustin Mulcahey

Theorem: ((add 2) 2) is equivalent to 4Proof: (I’m going to use a mixture of definitionalequality and reductions)

((add 2) 2)(((λn.λm.λf .λx .((n f ) ((m f ) x))) 2) 2)

((λm.λf .λx .((2 f ) ((m f ) x))) 2)(λf .λx .((2 f ) ((2 f ) x)))

(λf .λx .(((λf .λx .(f (f x)) f ) ((λf .(λx .(f (f x)) f ) x)))(λf .λx .((λx .(f (f x)) (λx .(f (f x)) x)))

(λf .λx .((λx .(f (f x)) (f (f x))))

(λf .λx .(λf .(f (f (f (f x))))))4

Page 103: Lambda Calculus by Dustin Mulcahey

Theorem: ((add 2) 2) is equivalent to 4Proof: (I’m going to use a mixture of definitionalequality and reductions)

((add 2) 2)(((λn.λm.λf .λx .((n f ) ((m f ) x))) 2) 2)

((λm.λf .λx .((2 f ) ((m f ) x))) 2)(λf .λx .((2 f ) ((2 f ) x)))

(λf .λx .(((λf .λx .(f (f x)) f ) ((λf .(λx .(f (f x)) f ) x)))(λf .λx .((λx .(f (f x)) (λx .(f (f x)) x)))

(λf .λx .((λx .(f (f x)) (f (f x))))(λf .λx .(λf .(f (f (f (f x))))))

4

Page 104: Lambda Calculus by Dustin Mulcahey

As you can see, doing arithmetic with Churchnumerals is both simple and fun.

What about multiplication?

Page 105: Lambda Calculus by Dustin Mulcahey

Intuition: (n ∗m) takes a function and returns then ∗mth fold composite of the function with itself.Strategy: Make the mth composite of f n times.

mult = λn.λm.λf .λx .((n (m f )) x)

Page 106: Lambda Calculus by Dustin Mulcahey

Intuition: (n ∗m) takes a function and returns then ∗mth fold composite of the function with itself.Strategy: Make the mth composite of f n times.

mult = λn.λm.λf .λx .((n (m f )) x)

Page 107: Lambda Calculus by Dustin Mulcahey

Theorem: ((mult 2) 2) is equivalent to 4

Proof: This is left as an exercise for the reader.

Page 108: Lambda Calculus by Dustin Mulcahey

Theorem: ((mult 2) 2) is equivalent to 4Proof: This is left as an exercise for the reader.

Page 109: Lambda Calculus by Dustin Mulcahey

Exponentiation is also straightforward:Strategy: To get mn, apply n to m. Remember thatm takes a function and returns the mth foldcomposite. So now we take the nth fold compositeof the function that takes a function and returns themth fold composite. So now we have a functionthat takes a function and returns the mnth foldcomposite.

Clear, right? How about this:

(n m)f = (m ◦m ◦ · · · ◦m)f

(Remember that composition corresponds toaddition.)

Page 110: Lambda Calculus by Dustin Mulcahey

Exponentiation is also straightforward:Strategy: To get mn, apply n to m. Remember thatm takes a function and returns the mth foldcomposite. So now we take the nth fold compositeof the function that takes a function and returns themth fold composite. So now we have a functionthat takes a function and returns the mnth foldcomposite. Clear, right? How about this:

(n m)f = (m ◦m ◦ · · · ◦m)f

(Remember that composition corresponds toaddition.)

Page 111: Lambda Calculus by Dustin Mulcahey

Exponentiation is also straightforward:Strategy: To get mn, apply n to m. Remember thatm takes a function and returns the mth foldcomposite. So now we take the nth fold compositeof the function that takes a function and returns themth fold composite. So now we have a functionthat takes a function and returns the mnth foldcomposite. Clear, right? How about this:

(n m)f = (m ◦m ◦ · · · ◦m)f

(Remember that composition corresponds toaddition.)

Page 112: Lambda Calculus by Dustin Mulcahey

Exponentiation is also straightforward:Strategy: To get mn, apply n to m. Remember thatm takes a function and returns the mth foldcomposite. So now we take the nth fold compositeof the function that takes a function and returns themth fold composite. So now we have a functionthat takes a function and returns the mnth foldcomposite. Clear, right? How about this:

(n m)f = (m ◦m ◦ · · · ◦m)f

(Remember that composition corresponds toaddition.)

Page 113: Lambda Calculus by Dustin Mulcahey

In lambda form:

exp ≡ λm.λn.λf λx .(((n m) f ) x)

Page 114: Lambda Calculus by Dustin Mulcahey

Subtraction is much trickier. The mostunderstandable way to do it (that I know of) is touse pairing.

Idea: Instead of incrementing x to x + 1, let’s takethe pair (n,m) to the pair (m,m + 1). If we start at(0, 0), we’ll get the following sequence:

(0, 0) 7→ (0, 1) 7→ (1, 2) 7→ (2, 3) · · ·

So, to get the predecessor of n, we just do theabove process n times and then take the firstcoordinate of the result. How’s that for efficiency?

Page 115: Lambda Calculus by Dustin Mulcahey

Subtraction is much trickier. The mostunderstandable way to do it (that I know of) is touse pairing.

Idea: Instead of incrementing x to x + 1, let’s takethe pair (n,m) to the pair (m,m + 1). If we start at(0, 0), we’ll get the following sequence:

(0, 0) 7→ (0, 1) 7→ (1, 2) 7→ (2, 3) · · ·

So, to get the predecessor of n, we just do theabove process n times and then take the firstcoordinate of the result. How’s that for efficiency?

Page 116: Lambda Calculus by Dustin Mulcahey

Subtraction is much trickier. The mostunderstandable way to do it (that I know of) is touse pairing.

Idea: Instead of incrementing x to x + 1, let’s takethe pair (n,m) to the pair (m,m + 1). If we start at(0, 0), we’ll get the following sequence:

(0, 0) 7→ (0, 1) 7→ (1, 2) 7→ (2, 3) · · ·

So, to get the predecessor of n, we just do theabove process n times and then take the firstcoordinate of the result. How’s that for efficiency?

Page 117: Lambda Calculus by Dustin Mulcahey

Okay, how do we make pairs?

Well, it will help to first define booleans andconditionals.

Page 118: Lambda Calculus by Dustin Mulcahey

Okay, how do we make pairs?

Well, it will help to first define booleans andconditionals.

Page 119: Lambda Calculus by Dustin Mulcahey

A few definitions:

true ≡ λx .λy .x

false ≡ λx .λy .y

cond ≡ λc .λt.λf .((c t) f

Page 120: Lambda Calculus by Dustin Mulcahey

A few definitions:

true ≡ λx .λy .x

false ≡ λx .λy .y

cond ≡ λc .λt.λf .((c t) f

Page 121: Lambda Calculus by Dustin Mulcahey

A few definitions:

true ≡ λx .λy .x

false ≡ λx .λy .y

cond ≡ λc .λt.λf .((c t) f

Page 122: Lambda Calculus by Dustin Mulcahey

A few definitions:

true ≡ λx .λy .x

false ≡ λx .λy .y

cond ≡ λc .λt.λf .((c t) f

Page 123: Lambda Calculus by Dustin Mulcahey

A few definitions:

true ≡ λx .λy .x

false ≡ λx .λy .y

cond ≡ λc .λt.λf .((c t) f

Page 124: Lambda Calculus by Dustin Mulcahey

To make a pair of lambda terms, we will store themboth in a cond. To get the first, we apply cond totrue. To get the second, we apply cond to false.

pair ≡ λf .λs.λc .(((cond c)s)t)

Page 125: Lambda Calculus by Dustin Mulcahey

To make a pair of lambda terms, we will store themboth in a cond. To get the first, we apply cond totrue. To get the second, we apply cond to false.

pair ≡ λf .λs.λc .(((cond c)s)t)

Page 126: Lambda Calculus by Dustin Mulcahey

To make a pair of lambda terms, we will store themboth in a cond. To get the first, we apply cond totrue. To get the second, we apply cond to false.

pair ≡ λf .λs.λc .(((cond c)s)t)

Page 127: Lambda Calculus by Dustin Mulcahey

What about my pair increment function?

paircrement ≡ λp.((pair (p false))(succ (p true)))

Page 128: Lambda Calculus by Dustin Mulcahey

So, the predecessor function looks like:

pred ≡ λn.(((n paircrement) ((pair 0) 0) true)

Page 129: Lambda Calculus by Dustin Mulcahey

So, the predecessor function looks like:

pred ≡ λn.(((n paircrement) ((pair 0) 0) true)

Page 130: Lambda Calculus by Dustin Mulcahey

Also, we can detect when something is zero:

isZero ≡ λn.((n(λx . false)) true)

Page 131: Lambda Calculus by Dustin Mulcahey

Phew! We now have conditionals and arithmetic.

... and with pairs, we could go ahead and define therationals right now. But I’m not going to.

Instead, I want to plunge into recursion!

Page 132: Lambda Calculus by Dustin Mulcahey

Phew! We now have conditionals and arithmetic.

... and with pairs, we could go ahead and define therationals right now. But I’m not going to.

Instead, I want to plunge into recursion!

Page 133: Lambda Calculus by Dustin Mulcahey

Phew! We now have conditionals and arithmetic.

... and with pairs, we could go ahead and define therationals right now. But I’m not going to.

Instead, I want to plunge into recursion!

Page 134: Lambda Calculus by Dustin Mulcahey

Okay, to do recursion, I need a function to callitself.

Except in our formal system of lambda

calculus, there is no notion of variable binding. Allwe have are ways of constructing lambda terms andways of reducing them to other lambda terms. Howdo we do this?

Page 135: Lambda Calculus by Dustin Mulcahey

Okay, to do recursion, I need a function to callitself. Except in our formal system of lambda

calculus, there is no notion of variable binding. Allwe have are ways of constructing lambda terms andways of reducing them to other lambda terms.

Howdo we do this?

Page 136: Lambda Calculus by Dustin Mulcahey

Okay, to do recursion, I need a function to callitself. Except in our formal system of lambda

calculus, there is no notion of variable binding. Allwe have are ways of constructing lambda terms andways of reducing them to other lambda terms. Howdo we do this?

Page 137: Lambda Calculus by Dustin Mulcahey

Yes, this is where we start talking about the Ycombinator.

There are a bunch of explanations of this thing, andwhat follows is one of them.

Page 138: Lambda Calculus by Dustin Mulcahey

Yes, this is where we start talking about the Ycombinator.

There are a bunch of explanations of this thing, andwhat follows is one of them.

Page 139: Lambda Calculus by Dustin Mulcahey

Let’s start with a recursive function:

fact ≡ λn. ((((cond (isZero n)) 1) ((mult n) (fact(pred n))))

This would only make sense if we could makerecursive definitional equalities. But, if you thinkabout it, if we could, then we would just be writingforever...

Page 140: Lambda Calculus by Dustin Mulcahey

Let’s start with a recursive function:

fact ≡ λn. ((((cond (isZero n)) 1) ((mult n) (fact(pred n))))

This would only make sense if we could makerecursive definitional equalities. But, if you thinkabout it, if we could, then we would just be writingforever...

Page 141: Lambda Calculus by Dustin Mulcahey

Well, we can’t refer to a function by name (exceptin the very limited sense of ≡). But what if wecould pass a function to itself?

fact ≡ λf . λn. ((((cond (isZero n)) 1) ((mult n) (f(pred n))))

Well, it wouldn’t make much sense to reduce (factfact), since we would have to reduce (fact (pred n)),which doesn’t make sense.

Page 142: Lambda Calculus by Dustin Mulcahey

But what if we had a magic function g such that gis equivalent to (fact g)?

Then, the following would happen (for example):

((fact g) 4)((λf . λn. ((((cond (isZero n)) 1) ((mult n) (f (pred

n)))) g) 4)λn. ((((cond (isZero n)) 1) ((mult n) (g (pred n))))

4)((((cond (isZero 4)) 1) ((mult n) (g (pred 4))))

((mult n) (g (pred 4)))((mult n) (g 3))

((mult n) ((fact g) 3))

Page 143: Lambda Calculus by Dustin Mulcahey

But what if we had a magic function g such that gis equivalent to (fact g)?

Then, the following would happen (for example):

((fact g) 4)

((λf . λn. ((((cond (isZero n)) 1) ((mult n) (f (predn)))) g) 4)

λn. ((((cond (isZero n)) 1) ((mult n) (g (pred n))))4)

((((cond (isZero 4)) 1) ((mult n) (g (pred 4))))((mult n) (g (pred 4)))

((mult n) (g 3))((mult n) ((fact g) 3))

Page 144: Lambda Calculus by Dustin Mulcahey

But what if we had a magic function g such that gis equivalent to (fact g)?

Then, the following would happen (for example):

((fact g) 4)((λf . λn. ((((cond (isZero n)) 1) ((mult n) (f (pred

n)))) g) 4)

λn. ((((cond (isZero n)) 1) ((mult n) (g (pred n))))4)

((((cond (isZero 4)) 1) ((mult n) (g (pred 4))))((mult n) (g (pred 4)))

((mult n) (g 3))((mult n) ((fact g) 3))

Page 145: Lambda Calculus by Dustin Mulcahey

But what if we had a magic function g such that gis equivalent to (fact g)?

Then, the following would happen (for example):

((fact g) 4)((λf . λn. ((((cond (isZero n)) 1) ((mult n) (f (pred

n)))) g) 4)λn. ((((cond (isZero n)) 1) ((mult n) (g (pred n))))

4)

((((cond (isZero 4)) 1) ((mult n) (g (pred 4))))((mult n) (g (pred 4)))

((mult n) (g 3))((mult n) ((fact g) 3))

Page 146: Lambda Calculus by Dustin Mulcahey

But what if we had a magic function g such that gis equivalent to (fact g)?

Then, the following would happen (for example):

((fact g) 4)((λf . λn. ((((cond (isZero n)) 1) ((mult n) (f (pred

n)))) g) 4)λn. ((((cond (isZero n)) 1) ((mult n) (g (pred n))))

4)((((cond (isZero 4)) 1) ((mult n) (g (pred 4))))

((mult n) (g (pred 4)))((mult n) (g 3))

((mult n) ((fact g) 3))

Page 147: Lambda Calculus by Dustin Mulcahey

But what if we had a magic function g such that gis equivalent to (fact g)?

Then, the following would happen (for example):

((fact g) 4)((λf . λn. ((((cond (isZero n)) 1) ((mult n) (f (pred

n)))) g) 4)λn. ((((cond (isZero n)) 1) ((mult n) (g (pred n))))

4)((((cond (isZero 4)) 1) ((mult n) (g (pred 4))))

((mult n) (g (pred 4)))

((mult n) (g 3))((mult n) ((fact g) 3))

Page 148: Lambda Calculus by Dustin Mulcahey

But what if we had a magic function g such that gis equivalent to (fact g)?

Then, the following would happen (for example):

((fact g) 4)((λf . λn. ((((cond (isZero n)) 1) ((mult n) (f (pred

n)))) g) 4)λn. ((((cond (isZero n)) 1) ((mult n) (g (pred n))))

4)((((cond (isZero 4)) 1) ((mult n) (g (pred 4))))

((mult n) (g (pred 4)))((mult n) (g 3))

((mult n) ((fact g) 3))

Page 149: Lambda Calculus by Dustin Mulcahey

But what if we had a magic function g such that gis equivalent to (fact g)?

Then, the following would happen (for example):

((fact g) 4)((λf . λn. ((((cond (isZero n)) 1) ((mult n) (f (pred

n)))) g) 4)λn. ((((cond (isZero n)) 1) ((mult n) (g (pred n))))

4)((((cond (isZero 4)) 1) ((mult n) (g (pred 4))))

((mult n) (g (pred 4)))((mult n) (g 3))

((mult n) ((fact g) 3))

Page 150: Lambda Calculus by Dustin Mulcahey

Such a magic g is the fixed point of fact.

A fixed point of a function f is a value x such thatf (x) = x

For example: if f (x) = x2 then 0, 1 are the fixedpoints of f .

Page 151: Lambda Calculus by Dustin Mulcahey

Such a magic g is the fixed point of fact.

A fixed point of a function f is a value x such thatf (x) = x

For example: if f (x) = x2 then 0, 1 are the fixedpoints of f .

Page 152: Lambda Calculus by Dustin Mulcahey

Such a magic g is the fixed point of fact.

A fixed point of a function f is a value x such thatf (x) = x

For example: if f (x) = x2 then 0, 1 are the fixedpoints of f .

Page 153: Lambda Calculus by Dustin Mulcahey

In the lambda calculus, there is a lambda term thatwill compute the fixed point of any other lambdaterm. This is referred to as the Y -combinator.

Note that there are several flavors of Y combinator.

Page 154: Lambda Calculus by Dustin Mulcahey

Here’s one:

Y = λf .((λx .(f (x x)))(λx .(f (x x))))

Page 155: Lambda Calculus by Dustin Mulcahey

Theorem: for any lambda term h, (Y h) isequivalent to (h (Y h)).

Proof:

(Y h)(λ f. ((λ x. (f (x x))) (λ x. (f (x x)))) h)

((λ x. (h (x x))) (λ x. (h (x x)))(h ((λ x. (h (x x)) (λ x. (h (x x))))

(h (Y h))

Page 156: Lambda Calculus by Dustin Mulcahey

Theorem: for any lambda term h, (Y h) isequivalent to (h (Y h)).

Proof:

(Y h)

(λ f. ((λ x. (f (x x))) (λ x. (f (x x)))) h)((λ x. (h (x x))) (λ x. (h (x x)))

(h ((λ x. (h (x x)) (λ x. (h (x x))))(h (Y h))

Page 157: Lambda Calculus by Dustin Mulcahey

Theorem: for any lambda term h, (Y h) isequivalent to (h (Y h)).

Proof:

(Y h)(λ f. ((λ x. (f (x x))) (λ x. (f (x x)))) h)

((λ x. (h (x x))) (λ x. (h (x x)))(h ((λ x. (h (x x)) (λ x. (h (x x))))

(h (Y h))

Page 158: Lambda Calculus by Dustin Mulcahey

Theorem: for any lambda term h, (Y h) isequivalent to (h (Y h)).

Proof:

(Y h)(λ f. ((λ x. (f (x x))) (λ x. (f (x x)))) h)

((λ x. (h (x x))) (λ x. (h (x x)))

(h ((λ x. (h (x x)) (λ x. (h (x x))))(h (Y h))

Page 159: Lambda Calculus by Dustin Mulcahey

Theorem: for any lambda term h, (Y h) isequivalent to (h (Y h)).

Proof:

(Y h)(λ f. ((λ x. (f (x x))) (λ x. (f (x x)))) h)

((λ x. (h (x x))) (λ x. (h (x x)))(h ((λ x. (h (x x)) (λ x. (h (x x))))

(h (Y h))

Page 160: Lambda Calculus by Dustin Mulcahey

Theorem: for any lambda term h, (Y h) isequivalent to (h (Y h)).

Proof:

(Y h)(λ f. ((λ x. (f (x x))) (λ x. (f (x x)))) h)

((λ x. (h (x x))) (λ x. (h (x x)))(h ((λ x. (h (x x)) (λ x. (h (x x))))

(h (Y h))

Page 161: Lambda Calculus by Dustin Mulcahey

So really, factorial is defined in two steps:

fact’ ≡ λf . λn. ((((cond (isZero n)) 1) ((mult n) (f(pred n))))

fact ≡ (Y fact’)

Page 162: Lambda Calculus by Dustin Mulcahey

Which is definitionally equivalent to this:

((λf . ((λx . (f (x x))) (λx . (f (x x)))) (λf . λn.((((λc .λt.λf . ((c t) f (λn. ((n (λx . (λx .λy .y)))(λx .λy .x)) n)) (λf .λx .(fx))) (((λn.λm.λf . (n (m(f )))) n) (f ((λn. (((n (λp. (((λf .λs.λc .((((λc .λt.λf .((c t) f ) c) s) t)) (p (λx .λy .y)))((λf .λx . (f ((n f ) x)) (p (λx .λy .x))))))(((λf .λs.λc . ((((λc .λt.λf .((c t) f ) c) s) t))(λf .λx .x)) (λf .λx .x)) (λx .λy .x))) n))))))

Page 163: Lambda Calculus by Dustin Mulcahey

Now that we’ve defined the lambda calculus andwritten a program in it, I want to discuss someproperties of the system as a whole.

Page 164: Lambda Calculus by Dustin Mulcahey

The Church-Turing Thesis

Any algorithm that performs a computation can beexpressed in the λ-calculus, or by a Turing machine,or by a recursive function (in the sense of recursiontheory).

Page 165: Lambda Calculus by Dustin Mulcahey

Undecidability of EquivalenceThere does not exist an algorithm that decideswhether or not two arbitrary lambda terms areequivalent.

Page 166: Lambda Calculus by Dustin Mulcahey

The Church-Rosser Theorem

In the λ-calculus, given terms t1 and t2 gotten froma common term t by a sequence of reductions, thereexists a term s that t1 and t2 both reduce to.

t

��

// t1

��t2 // s

Page 167: Lambda Calculus by Dustin Mulcahey

The Church-Rosser Theorem

In the λ-calculus, given terms t1 and t2 gotten froma common term t by a sequence of reductions, thereexists a term s that t1 and t2 both reduce to.

t

��

// t1

��t2 // s

Page 168: Lambda Calculus by Dustin Mulcahey

Equivalence of the λ-calculus and combinatory logic.

Define combinators:

I = λx .x

K = λx .λy .x

S = λx .λy .λz .((x z) (y z))

Then these combinators suffice to construct anylambda term, up to equivalence.

For example,

Y = S (K (S I I)) (S (S (K S) K) (K (S I I)))

Page 169: Lambda Calculus by Dustin Mulcahey

Equivalence of the λ-calculus and combinatory logic.

Define combinators:

I = λx .x

K = λx .λy .x

S = λx .λy .λz .((x z) (y z))

Then these combinators suffice to construct anylambda term, up to equivalence.For example,

Y = S (K (S I I)) (S (S (K S) K) (K (S I I)))

Page 170: Lambda Calculus by Dustin Mulcahey

Correspondence between SK and propositional logicConsider the axiom of propositional logic:

a =⇒ (b =⇒ a)

Now look at the K combinator again:

λa.λb.a

Now repeat this to yourself:“If I have a proof of a, then given a proof of b, I stillhave a proof of a”

Page 171: Lambda Calculus by Dustin Mulcahey

Correspondence between SK and propositional logicConsider the axiom of propositional logic:

a =⇒ (b =⇒ a)

Now look at the K combinator again:

λa.λb.a

Now repeat this to yourself:“If I have a proof of a, then given a proof of b, I stillhave a proof of a”

Page 172: Lambda Calculus by Dustin Mulcahey

Correspondence between SK and propositional logicConsider the axiom of propositional logic:

a =⇒ (b =⇒ a)

Now look at the K combinator again:

λa.λb.a

Now repeat this to yourself:“If I have a proof of a, then given a proof of b, I stillhave a proof of a”

Page 173: Lambda Calculus by Dustin Mulcahey

Now consider the axiom:

(a =⇒ (b =⇒ c)) =⇒ ((a =⇒ b) =⇒ (a =⇒ c))

Now look at the S combinator again:

λf .λg .λa.((f a)(g a))

Now, repeat this to yourself:“If I have a way f of turning proofs of a into proofsthat b implies c, then given a proof g that a impliesb, I can make a proof that a implies c.”

Page 174: Lambda Calculus by Dustin Mulcahey

Now consider the axiom:

(a =⇒ (b =⇒ c)) =⇒ ((a =⇒ b) =⇒ (a =⇒ c))

Now look at the S combinator again:

λf .λg .λa.((f a)(g a))

Now, repeat this to yourself:“If I have a way f of turning proofs of a into proofsthat b implies c, then given a proof g that a impliesb, I can make a proof that a implies c.”

Page 175: Lambda Calculus by Dustin Mulcahey

Now consider the axiom:

(a =⇒ (b =⇒ c)) =⇒ ((a =⇒ b) =⇒ (a =⇒ c))

Now look at the S combinator again:

λf .λg .λa.((f a)(g a))

Now, repeat this to yourself:“If I have a way f of turning proofs of a into proofsthat b implies c, then given a proof g that a impliesb, I can make a proof that a implies c.”

Page 176: Lambda Calculus by Dustin Mulcahey

Really, the only sane way to think about this stuff isto appeal to category theory.

The proposition

a =⇒ (b =⇒ a)

Corresponds to an object (“function space”). Thinkof A as the set of proofs of the proposition a.

(AB)A

Which, in nice categories is isomorphic to

A(A×B)

(All I’ve done here is uncurry.)

Page 177: Lambda Calculus by Dustin Mulcahey

Really, the only sane way to think about this stuff isto appeal to category theory.The proposition

a =⇒ (b =⇒ a)

Corresponds to an object (“function space”). Thinkof A as the set of proofs of the proposition a.

(AB)A

Which, in nice categories is isomorphic to

A(A×B)

(All I’ve done here is uncurry.)

Page 178: Lambda Calculus by Dustin Mulcahey

Really, the only sane way to think about this stuff isto appeal to category theory.The proposition

a =⇒ (b =⇒ a)

Corresponds to an object (“function space”). Thinkof A as the set of proofs of the proposition a.

(AB)A

Which, in nice categories is isomorphic to

A(A×B)

(All I’ve done here is uncurry.)

Page 179: Lambda Calculus by Dustin Mulcahey

Really, the only sane way to think about this stuff isto appeal to category theory.The proposition

a =⇒ (b =⇒ a)

Corresponds to an object (“function space”). Thinkof A as the set of proofs of the proposition a.

(AB)A

Which, in nice categories is isomorphic to

A(A×B)

(All I’ve done here is uncurry.)

Page 180: Lambda Calculus by Dustin Mulcahey

Really, the only sane way to think about this stuff isto appeal to category theory.The proposition

a =⇒ (b =⇒ a)

Corresponds to an object (“function space”). Thinkof A as the set of proofs of the proposition a.

(AB)A

Which, in nice categories is isomorphic to

A(A×B)

(All I’ve done here is uncurry.)

Page 181: Lambda Calculus by Dustin Mulcahey

The latter function space contains the firstprojection (which looks an awful lot like K). Theexistence of this first projection shows that the typeAA×B is inhabited, and thus the original propositiona =⇒ (b =⇒ a) is valid.

Page 182: Lambda Calculus by Dustin Mulcahey

The correspondence between lambda expression,logical formulas, and objects in categories is calledthe Curry-Howard-Lambek correspondence.

Page 183: Lambda Calculus by Dustin Mulcahey

Thanks!