Notes (TILC 02/05): Arithmetic

2 Arithmetic

Thus far, everything we’ve seen with lambda calculus has been a symbol manipulation game, and not obviously meaningful. So we’re going to put some meaning into it by coming up with a way to do arithmetic.

First, we’re going to need numbers. Specifically, we’re going to need to find a coherent way to represent the natural numbers

0, 1, 2, 3, ...

as lambda expressions. The way we’re going to do this is by implementing of Peano numbers in lambda calculus.

[N.B. I will find/write a good explanation of Peano numbers, link it here, and rework this to be clearer]

First, we’re going to pick an expression for 0, which we’ll call Zero, and then a successor function succ, so that

0 = Zero
1 = succ Zero
2 = succ (succ Zero)
...

and so on. This is the standard Peano definition of the natural numbers.

But there’s a neat trick we can do in our lambda calculus implementation of Peano numbers. Notice how the Peano numbers are defined as a zero and then layers of a succesor function on top of zero. We know how to express functions in lambda calculus, so what if we rewrote the above definition of Peano numbers as functions of two parameters s and z, which will stand in for whatever expressions we pick for the successor function and the zero, respectively. We’ll call these lambda functions that return numbers n_, where n is the number they return:

Let’s start with 0_. We know that all the n_ functions are functions of s, (succ) and z (Zero), so the heads of all our n_ will be the same: \s z.

Now here’s the trick. The above functions are already lambda expressions. So while we could find other expressions that would work as numbers, we can just use the above n_ functions as numbers directly.

If we do that then we already found an expression for our zero: \sz.z. Which is just a function that throws away its argument and returns our old friend the identity function.

One way to visualize this structure is to imagine n_ as the set of functions that apply another function to an argument n times. Zero is “apply zero times” or “don’t apply”, one is “apply once”, two is “apply twice” and so on.

Now we just need a succ function. succ takes a number and returns that number plus one. But if our numbers are functions that apply another function to an argument some number of times, then succ takes a number n, a function f and an argument x then applies f to x an (n+1) number of times. In other words, succ is like an “apply once more”function.