TITLE: How do lambda calculus most basic definitions work?
QUESTION [12 upvotes]: Good afternoon,
I'm trying to learn lambda calculus, and I do understand the notation (it's not hard, $f=\lambda a_1.\cdots\lambda a_n.x=\lambda a_1\cdots a_n.x\Leftrightarrow f(a_1;\cdots;a_n)=x$), but not what is defined by it. For an example of something I don't understand, take the Church numerals:
$$
0=\lambda f.\lambda x.x\\
1=\lambda fx.f\ x\\
2=\lambda fx.f(f(x))\\
\cdots\\
n=\lambda nfx.f^n\ x
$$
(where $f^n$ is $f$ composed with itself $n$ times)
I see $0$'s definition is to not apply a function (apply it zero times), and $1$'s to apply it once, and $n$'s to apply it $n$ times. This kind of makes sense; however, what I don't get is how, formally, can a function be turned into a number. Shouldn't $\lambda f.\lambda x\ x$ be a function with two arguments, one of which is a function? Is it a function or is it a number? Wouldn't one have to pass values of to $f$ and $x$ to it for it to be a value? Is it a representation of the number? How does that really work?
Also, with true ($\lambda xy.x$) and false's ($\lambda xy.y$) definitions? How are those boolean values? They seem two argument functions that yield the first and second argument, respectively. Again, are they values or are they functions? Please help me out here, it's just so confusing to me. I've seen stuff like this, but I'm still clueless. I know some programming, so any working program showing this would be most welcome.

REPLY [6 votes]: To address directly your question "how can, formally, a function be turned into a number?", let me first address a different question: "how can, formally, a string of digits be turned into a number?"
As you are surely aware, a string of digits is not a number. A string of digits is just a sequence of characters. And yet we speak without problems about "the number 120".
The point is that we use strings of digits to represent numbers. That is, we have a mapping between strings of digits and numbers, which allows us to identify any number we want with a string of digits. We do so because we cannot write down numbers, but we can write down strings of digits.
Now there are different ways to translate a number into a string of digits. The Romans would have written the number 120 (that is, the number which we denote by the digit string 120) as CXX. Now the Roman number system was impractical for multiplication, and only moderately practical for larger numbers, which is why we switched to the more practical decimal system. Here we can give easy rules on how to manipulate strings of digits in order to find sums, differences, products and quotients of numbers.
So how is all this related to Church numbers? Well, here we face basically the same problem: We want to represent numbers, but all we have are functions. So we need to define functions which somehow encode numbers. Now there are infinitely many ways how we could do it, however some are more practical than others. Church integers are a way that's quite practical for addition, multiplication, and doing loops (that is, doing an operation $n$ times). They are not a particularly good representation for subtraction, BTW.
Note that the analogous problem also occurs in set theory, where you also want to represent the natural numbers, but all you have are sets. So here the standard construction is to start with the empty set for $0$, and represent $n+1$ by $n\cup\{n\}$.
So what are natural numbers, after all? Are they digit strings? Are they functions? Are they sets?
They are in some sense all of those, and in another sense neither. What natural numbers are is basically fixed by the Peano axioms. Whenever you have something which fulfils the Peano axioms, you have a model (a representation) of the natural numbers. The digit strings are, basically because the digit string 0 stands for the number $0$, and we have rules that give the digit string for $n+1$ whenever we have a digit string for $n$ (and all digit strings correspond to some $n$). The church integers do because we have a representation for $0$ (namely $\lambda f.\lambda x.x$) and a function which gives us $n+1$ given $n$ (namely $\lambda n.\lambda f.\lambda x.f (n f x)$). And the special sets do, again, because we have $0$ ($\emptyset$) and $n\mapsto n+1$ ($n\cup\{n\}$). But neither are "the" natural numbers, as each of them is an equally valid representation of the natural numbers (just like base 2, base 8 and base 10 representations are equally valid representations). You cannot point to one representation and say those are the true natural numbers, and all others are not.