I just spent a couple of very cold weeks in Anchorage Alaska. I went out with a bunch of friends to a bar on the night prior to leaving, where I met up with an old friend (and listened to two other friends play music). I started describing the lambda calculus to him since it is related to my work and he was curious what I was up to. In any case I found myself unable to produce addition of the church numerals off the top of my head, which I found pretty disturbing since it shows that I probably didn't quite understand it in the first place. Therefor I sat down this morning to see if I couldn't reconstruct it from first principles.

It turns out that it is relatively easy. First, let us start with a bit of notation, and a description of the lambda calculus. Since wikipedia describes the lambda calculus in detail, I'll just show how one procedes with calculations. As examples let us start with the church numerals.

1 = (λf x.f x)

2 = (λf x.f f x)

3 = (λf x.f f f x)

...

n = (λf x.f

where f

The idea is that each numeral is represented by a lambda term. The lambda term describes the arguments (f and x in this case) and the "body" in which we substitute the arguments when the are passed in. An example of an "application" of a lambda term to an argument would be:

1 g = (λf x . f x) g => (λx . g x)

We pass in g, and replace every occurance of f in the body with g, and delete f from the list of variables. we can continue to apply this term to another term

(λx . g x) y => g y

Which leads us to conclude that

1 g y => g y

Ok, so now that you've seen a few calculations, let us try to construct addition. The first thing I tried to do is to construct the function +1. Clearly, we want +1 n => (n+1). n+1 is (λf x. f

(λf x. f

So now we know how to get rid of the lambda abstraction. Now we can add an f on the front, which will get us closer to n+1.

f (λf x. f

We are almost there. Now we need to have the λf x part at the beggining so that we look like a church numeral.

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

Looking great so far! Now we just need to be able to take the whole (λf x. f

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

This shows that (λa f x. f a f x) is the +1 function!

+1 = (λa f x. f a f x)

Ok, so now that we have +1 can we get a function that takes an n and returns +n? This would get us a long way towards addition. We can call this function n->+n.

Ok, so we can guess what +n will look like easily. It's going to look just like +1 except with n leading f's.

+n = (λa f x. f

So we should try to figure out how to take the f

(λf x . f

ok, but really we want

(λf x . f

Great! Look how close we are. now we just need to abstract the a,f,x and place f x following it.

(λa f x. (λf x . f

Ok, so now that we know how to take an n, and able to take the entire church numeral n as an a beta reduce it to n+1, let us abstract the n as an argument

(λb a f x. b f a f x)

Let us verify quickly that this function works.

(λb a f x. b f a f x) n =>

(λa f x. n f a f x) =>

(λa f x. (λf x. f

(λa f x. f

Now, we can verify that this works on m, to obtain n+m as the +n function should:

(λa f x. f

(λf x. f

(λf x. f

Hooray! Not only did we find n->+n, but we have obtained the function "addition" for free. Since, once we have the +n function we can apply it to m. So we now have the function:

add = (λb a f x. b f a f x)

I haven't tried multiplication and exponentiation yet, but you are welcome to try!

It turns out that it is relatively easy. First, let us start with a bit of notation, and a description of the lambda calculus. Since wikipedia describes the lambda calculus in detail, I'll just show how one procedes with calculations. As examples let us start with the church numerals.

1 = (λf x.f x)

2 = (λf x.f f x)

3 = (λf x.f f f x)

...

n = (λf x.f

^{n}x)where f

^{n}means "f f ... f" n times.The idea is that each numeral is represented by a lambda term. The lambda term describes the arguments (f and x in this case) and the "body" in which we substitute the arguments when the are passed in. An example of an "application" of a lambda term to an argument would be:

1 g = (λf x . f x) g => (λx . g x)

We pass in g, and replace every occurance of f in the body with g, and delete f from the list of variables. we can continue to apply this term to another term

(λx . g x) y => g y

Which leads us to conclude that

1 g y => g y

Ok, so now that you've seen a few calculations, let us try to construct addition. The first thing I tried to do is to construct the function +1. Clearly, we want +1 n => (n+1). n+1 is (λf x. f

^{(n+1)}x) which is also (λf x. f f^{n}x). Since n = (λf x. f^{n}x) we need to somehow add another f. The trick is to get the arguments to use the same symbols, and to remove the lambda abstraction. We can do this by applying the church numeral to the symbols we want to use, in this case f and x.(λf x. f

^{n}x) f x => f^{n}xSo now we know how to get rid of the lambda abstraction. Now we can add an f on the front, which will get us closer to n+1.

f (λf x. f

^{n}x) f x => f f^{n}xWe are almost there. Now we need to have the λf x part at the beggining so that we look like a church numeral.

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

^{n}x) f x) => (λf x. f f^{n}x) = (λf x. f^{(n+1)}x)Looking great so far! Now we just need to be able to take the whole (λf x. f

^{n}x) form as an argument. This turns out to be really easy, we just put a variable in it's place and add it to the front of the list of lambda arguments.(λa f x. f a f x) (λf x . f

^{n}x) => (λf x. f (λf x. f^{n}x) f x) => (λf x. f f^{n}x)This shows that (λa f x. f a f x) is the +1 function!

+1 = (λa f x. f a f x)

Ok, so now that we have +1 can we get a function that takes an n and returns +n? This would get us a long way towards addition. We can call this function n->+n.

Ok, so we can guess what +n will look like easily. It's going to look just like +1 except with n leading f's.

+n = (λa f x. f

^{n}a f x)So we should try to figure out how to take the f

^{n}out of a church numeral, and place it there. Well, we should apply the same trick of applying the church numeral to f and x so that we can extract the body.(λf x . f

^{n}x) f x => f^{n}xok, but really we want

*a*to follow, based on +n, so instead of using x, let us apply the form to*a*.(λf x . f

^{n}x) f a => f^{n}aGreat! Look how close we are. now we just need to abstract the a,f,x and place f x following it.

(λa f x. (λf x . f

^{n}x) f a f x) => (λa f x. f^{n}a f x)Ok, so now that we know how to take an n, and able to take the entire church numeral n as an a beta reduce it to n+1, let us abstract the n as an argument

(λb a f x. b f a f x)

Let us verify quickly that this function works.

(λb a f x. b f a f x) n =>

(λa f x. n f a f x) =>

(λa f x. (λf x. f

^{n}x) f a f x) =>(λa f x. f

^{n}a f x)Now, we can verify that this works on m, to obtain n+m as the +n function should:

(λa f x. f

^{n}a f x) m =>(λf x. f

^{n}(λf x. f^{m}x) f x) =>(λf x. f

^{n}f^{m}x) = (λf x. f^{(n+m)}x) = n+mHooray! Not only did we find n->+n, but we have obtained the function "addition" for free. Since, once we have the +n function we can apply it to m. So we now have the function:

add = (λb a f x. b f a f x)

I haven't tried multiplication and exponentiation yet, but you are welcome to try!

## Comments

## Post a Comment