Lambda encoding
When functions are the only thing you have, numbers, booleans and lists have to be functions too.
●●● hard · where to see it
The idea
Alonzo Church's lambda calculus (1930s) has only three things, namely variables, functions (λx. body) and application. There are no numbers, no true, no lists. Yet it is universal.
Read the story
So data is encoded as behaviour. A number is not a value you look at; it is a function that does something that many times. A boolean is a function that picks one of two options.
This is what lets functional tarpits like Lazy K and Binary Lambda Calculus do arithmetic and I/O with nothing but functions.
How it works
The number n is "apply f n times".
Church numerals
The number n is "apply f n times".
0 = λf. λx. x
1 = λf. λx. f x
2 = λf. λx. f (f x)
3 = λf. λx. f (f (f x))
Then the operations write themselves.
- successor. Do f once more,
succ = λn. λf. λx. f (n f x); - add. Do f m times, then n times,
add = λm. λn. λf. λx. m f (n f x); - multiply. Do "f n times" m times,
mul = λm. λn. λf. m (n f).
Church booleans
A boolean chooses.
true = λa. λb. a
false = λa. λb. b
So if c then t else e is c t e. No if needed.
To test for zero, give the number a function that always returns false, and start from true. Zero applications leave true; any more turn it into false.
Run it
JavaScript arrow functions are lambda terms, so all of this can be checked directly.
const zero = f => x => x;
const succ = n => f => x => f(n(f)(x));
const add = m => n => f => x => m(f)(n(f)(x));
const mul = m => n => f => m(n(f));
const toInt = n => n(k => k + 1)(0); // decode: apply "+1" n times to 0
const two = succ(succ(zero)), three = succ(two);
console.log(toInt(add(two)(three)), toInt(mul(two)(three))); // 5 6
const TRUE = a => b => a, FALSE = a => b => b;
const isZero = n => n(_ => FALSE)(TRUE);
console.log(isZero(zero)('yes')('no'), isZero(two)('yes')('no')); // yes no
In Node it prints 5 6 and yes no.
Other encodings
- Pairs.
pair = λa. λb. λs. s a b; the first element isp true, the secondp false. - Lists are pairs of a head and a tail, or functions that fold over their elements.
- Scott encoding stores "what I am made of" instead of "how to fold me", which makes taking a number apart (predecessor) easy where Church makes it awkward.
Seen in
- Binary Lambda Calculus. Lambda terms written in bits; input and output are lists of bits, encoded as above.
- Lazy K. Bytes come in and go out as Church numerals, inside a lazy list.
- Unlambda and Iota. The same encodings after the variables are compiled away into combinators.
- Underload. Numbers are often written as Church-style programs that duplicate and concatenate a string n times.
Try this
- Write
pow = λm. λn. n min JavaScript and check that it gives 2³ = 8. (Mind the order.) - Write
and,orandnotfor Church booleans. - Hard: write the predecessor of a Church numeral. (Hint: count pairs
(k, k+1)up from(0, 0).)
Further
- esolangs.org/wiki/Lambda_calculus
- Church encoding on Wikipedia
- John Tromp's write-up of Binary Lambda Calculus and its tiny self-interpreter.
- See also combinators, fixed points, encoding tricks.