Lambda Calculus: Finding the Answer to +++
I. Preface⌗
What is +*+*+
?
That’s read “plus times plus times plus.”
No numbers, all operators.
This sounds like the sort of question that first graders ask each-other when they’re trying to be silly – but it’s more than that. There is an answer to be had, salient, if not simple. The problem is that you can’t use the mathematical tools that you’re most familiar with to get to it. You can’t even use the same mathematical language, really.
If we want to get to the bottom of this – if we want to find +*+*+
– we’re going to need a bit more power.
We’re going to need lambda calculus.
II. In Which You Become A Wizard⌗
Okay, so, that probably sounds scary. I get it. Calculus scares the shit outta me too. If I never had to find a derivative again, I’d be a happy man. Lets not even talk about integrals.
But I want to let you in on a little secret; the word “calculus” doesn’t mean anything. I’m serious. “Calculus” is just a word that old men with gray beards and tweed jackets put after funny-sounding, vaguely latin-or-greek sounding terms in order to convince each-other that what they’re doing is Very Complicated and Important – and, more importantly, to scare you off, because they know that you’ll become too powerful if you dare to skim their tomes and codices and whatnot. And, as luck would have it, lambda calculus is a shining example of exactly that. If you have two eyes and can read what I’m writing right now, you can learn lambda calculus. And to prove it to you, I’m going to teach you how to speak Lambda Calculese with three simple rules.
Rule The First⌗
Every letter you run across, with the exception of λ
, is a variable.
x
?
Variable.
a
?
Variable.
p
?
I’ll leave that one as an exercise to the reader.
I believe in you!
Okay, so, we now ideally know that pretty much every letter we see is a variable. But that doesn’t help you much if you don’t know what a variable is. A variable can be thought of as a box. That box can have anything you like inside: nothing, a number, a letter, a word, a name, a manuscript detailing every moment of your past, present, and future… Whatever you like.
And in much the same way that we label actual boxes to keep track of its contents, we also label these mathematical boxes.
For example, lets say I have a box (variable), and I put a two inside.
I can label the box whatever I like, so long as a remember that the label I give it corresponds with the thing I put inside.
So, lets label that box w
.
In mathematical terms, we’d say that now the variable w = 2
.
Now we can do math with these boxes.
For example, I might want to figure out what I’d have if I had two boxes instead of one, so I might try something like 2 * w
, which, as you might expect, comes out to 4
.
Or maybe I find myself in a very confusing life circumstance.
Maybe I order four boxes labeled w
, but am shipped a cube of w
boxes with side lengths of 4
instead.
And then my wife takes six of the vaunted w
boxes in the divorce, and I am forces to sell half of the remaining w
boxes to cover legal fees.
((4w)^3 - 6) / 2 = ((2 * 4)^3 - 6) / 2 = 253
How will I ever financially recover?
Regla Dos⌗
I previously mentioned that the letter λ
is not a variable in this case.
That is because it is too cool for school and does things a bit differently.
You will typically see λ
used in a shape that looks something like this: (λx._)
For those of you keeping score, x
is variables here..
In this special case, what we are doing can be likened to building a machine that has a slot for a box, and that will return something new and exciting.
In this case, the label of the box that the machine takes will replace every box inside of it named x
.
The _
in this example is a blank that we get to fill in, and what we put in there will determine how the machine changes our input, x
, before giving it back to us.
At this point I would typically provide an example of how this is helpful, but we’re missing a rather critical component. I have not actually told you how to use the machine yet. For that, we will need…
The Third Rule⌗
In order to actually have a machine – or a function, as they’re formally called – carry out its function, we need to feed it an input.
That input can be a variable, like j
, or another machine, like (λx . (+ x 1))
, or a variable that represents a machine.
I know, that sounds like a lot of indirection, but I promise that this is as complicated as it gets.
To give a machine an input, we just need to put that input after the machine, like so: y = 2, (λx . (+ x 1)) y
.
This expression is equivalent to (λy . (+ y 1))
, which becomes + y 1
, which is ultimately 3
.
You may have noticed that the + sign is out of place, and that might be confusing, but this is actually a direct consequence of this rule!
The + sign in lambda calculus is a variable that contains a machine, and machines can only be fed with things that are after them, so if we want to do addition, we have to make it look a little weird.
Why did they decide to do it that way?
For the same reason they use the word “calculus.”
A Detour⌗
In the beginning of this post, I promised that we’d find the answer to +*+*+
.
And we will!
But I’ve lied a little bit to make things simpler, and if we want to go all the way here, I need to explain some things.
Firstly, numbers do not exist in lambda calculus. I know, you just saw me use numbers, but I was using shorthand to relate the rules to things you may have seen before. In this strange new place, we only have functions and variables. So the question becomes; how do we count under these conditions?!?! The answer? We make a machine that counts up by one, and we wrap it in itself for as many times as we need to get somewhere.
So, zero?
λf.λx. x
.
One?
λf.λx. f x
.
Two?
λf.λx. f (f x)
.
Sixty?
λf.λx. f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f x)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
.
Likewise, I was not entirely honest in my usage of +
, because, as you may have now guessed, it too is shorthand for a rather gross lambda calculus expression.
Knowledge of the structure that underpins addition – and multiplication, for that matter – are an unfortunate part of our journey to enlightenment, though.
So, lets have ourselves a think.
How would one represent addition?
Well, when you boil it down, addition can be seen as counting to one number, and then counting one higher until we’ve gone up one as many times as another number.
And in lambda calculus, we count up by wrapping more bullshit around an initial machine.
So if we want to add in lambda calculus, all we really need to do is to make a machine that will wrap one number up as long as we can unwrap another number into nothingness.
Simple, right?
Here’s what that looks like in lambda calculus syntax; (λmn.(p (λn.(λfx.(f(nfx)))) q))
.
And what’s multiplication but repeated addition?
(λnms.(n(ms)))
, where n
and m
are numbers – s will be the machine what we get back.
III. The Answer⌗
Okay. Finally. We have all the pieces together. I’ve given you all the PVC, all the duct-tape, all the hot glue… Even some epoxy resin I had lying around. Shit was expensive, man.
Now, we can finally take a crack at figuring out what +*+*+
actually comes out to.
I’ll save you the work; it’s λfx.f((ms)fx)
.
In regular terms, this is actually a function f(m, n, p, q, r, s, t)
, which is defined by (((m + n) * (p + q)) * (r + s)) + t
.
As far as I am aware, there are not any known rules that actually dictate what a nonsensical series of arithmetic operators acting on each-other will yield when completely reduced, so there is no way to know if what we’ve received here is meaningfully related to our starting expression in lambda calculus.
Despite that, the function we’ve found ourselves with looks strangely similar to what we might’ve expected from doing such a strange manipulation, if we regarded the operators themselves as functions.
Certainly an area for further study.
IV. Conclusion⌗
Despite promising to keep things simple, I suppose I was a bit ambitious with our beginning question. No matter. If you retain even the basic rules of lambda calculus – or even the idea that there exists an alternate way of doing math with only variables and functions – I am a happy man. Maybe, in the future, you take it upon yourself to study the great mysteries of lambda calculus, and pry answers from behind the paradoxical veil of tedium and complexity. Or maybe you just bullshit your friends with weird, seemingly unanswerable math questions. Iunno, I’m not your dad.
Either which way, this is where I leave you. Be well.