Stuff Central

> home > non-fiction > lambda-calculus

the lambda calculus

the lambda calculus has been one of my favourite topics for a few years now, ever since an elective college class. one might call it my hyperfixation, one might also say i talk about it too much, and im gonna talk about it more i really like it. if you'll allow me, i'd like to introduce this computational model to you so you can enjoy it with me :3

the structure

every lambda calculus term is built like a binary tree, with three types of nodes:

  1. applications (_ _)
    they always have two kids, the left one we call its function, the right one its argument
  2. abstractions λ_._
    they also have two kids, the left one is its bound variable, and is always a variable, the right is its body
  3. variables x
    these are always a leaf, and have an identifier of some sort, usually a lowercase letter.

usually lambda terms are written down using text, but i think its pretty unintuitive at first so i've written a nice visualizer for you, you can build up a little intuition for how terms look, and how they're written down i hope. also i made it link all the bound variables together cause it looks pretty.

oh also, because the terms get really big usually, there's some shorthand you should know. λx.λy.M can be shortened to λx y.M, note the space between the variables. ((X Y) Z)can be shortened to just X Y Z - application is left-associative - and ofcourse we omit the brackets if they're obvious. oh also you can use \ instead of λ

play around with this a bit, then continue :3

beta reduction

you might've noticed that some of the applications visualized are filled with orange. you might've clicked them too, which made the term change suddenly. what you've just done is called beta reduction ! this represents applying function, basically the same as you're used to in math: if you have some function f(x) := x^2 - x + 6, and you evaluate it on input 3, you get f(3) = 3^2 - 3 + 6, all the x's got replaced with a copy of the argument.

beta reduction works the exact same way. if you have an application, whose left kid is an abstraction, (\x.B) A, you can reduce it into B[x/A], i.e. a version of B where all x's are replaced with A. its called beta reduction, because there is another one, called alpha reduction, that one is about renaming variables so that there's no duplicates. that also makes sense, you dont want a function like f(x, x) = 2x - 3x, you don't know which x is which.


look back at the example with c's and d's, and think about what both of the terms are doing. λc.c c takes in some argument, creates two copies, and applies one to the other. λd. d simply takes in an argument and simply returns it. so, what happens when you apply them ? simple, two copies of the one with d's gets created: (λd.d) (λd.d). apply again, one of them eats the other, returns it back unchanged, we're left with λd.d. if we were doing alpha reduction, we would rename one of the d's to something else, lets say e, so we would have (λd.d) (λe.e), but our visualizer doesn't need it

boolean logic

i know you're getting bored, but we're now done with the theory, so let's do some actual work !

we want to represent boolean logic somehow, so we need to define booleans true and false. because there's no real way to have "data" (yet), instead of thinking about what a boolean is, think about what it does. well usually, its used to decide between two options, if b: x, else y, so we can represent a boolean as a function that takes in two options, and returns one or the other, depending on if its true or false. TRUE := λt f.t, and FALSE := λt f.f. note that these shorthands we're defining are really just shorthand.

next we want some logic gates. a NOT is the easiest, it returns whatever the oposite of the input is. we can make the gate like a function that takes a boolean, and gives it false, then true. NOT (long version) = λb.b FALSE TRUE = λb.b (λt f.f) (λt f.t). we can simplify this a bit, instead of returning whole new copy of the boolean, we can construct it: NOT := \b.\t f.b f t. the input b will return one of the other variables, and create either a true or false for us !

an AND gate is just as simple. it takes two booleans. if the first is false, return false. otherwise return the first boolean ! AND := λa b.a b (λt f.f). we can actually simplify this a bit; if a is false, we don't have to return a different false, we can just return a itself: AND := λa b.a b a.

go play around with it a bit ! this time i'll let you type your own expressions. remember \ is the same as λ.


i took the liberty of defining OR and XOR, both using composition, and making them directly. obviously the direct versions are smaller, and in some way "better", but you shouldn't think about it like that, this system isn't meant to be efficient.

pairs and lists

i'll also quickly mention, booleans let us create pairs. if we create a term like λb.b A B then passing in a boolean we can get out either the first, or second element of the pair. we can also build arbitrarily long lists by nesting pairs. a list is a pair like (element, rest-of-list), though there isn't a way to tell how long it is. not without some modifications, anyway.

numbers

let's do some numbers ! like we've seen with booleans, we need to think about what a number does, not just what it is. usually, natural numbers are used in loops to indicate how many times they should run. or in other words, how many times they should apply a function. so, we'll say that a number N takes a function and an argument, and applies the function N times in a row before returning the output: 0 := λf x.x, 1 := λf x.f x, 2 := λf x.f(f x), 3 := λf x.f(f(f x)) etc.

right. how do we define adition. well imagine we figure out how to increment a number, then to add X to Ywe could simply do X INC Y, i.e. increment Y X times ! incrementing itself isnt too hard, we'll make it how we did the NOT function, where it builds it from the inside: INC := λn.λf x.n f (f x). the argument it'll give to the number will have f pre-applied once, so as a result it'll be applied n+1 times.

we already know how to add, increment repeatedly: ADD := λa b.a INC b. it doesnt take much to figure out how to multiply: add repeatedly ! MULT := λa b.a (ADD b) 0, and exponents will be multiplying repeatedly ofcourse, EXP := λa b.a (MULT b) 1. there are smaller definitions of these, MULT can be defined as λa b.λf x.a(b f)xexponents as simply as λa b.b a

subtraction

i want to give this its own sub-section because its pretty complicated and the whole point is that it should't.

we start with a simple idea: if we give a number n the function INC and starting argument 0, it will increment it n times and return back itself, simple. if we want the result to be one lower, we want one of those increments to fail, or be consumed somehow. we could try to come up with some kind of starting term, call it P, such that INC P would reduce into a 0. this sounds good at first, but this isn't really doable with our definition of INC.

instead of directly giving the argument to INC, we'll wrap it in another function, that'll give INC to the argument itself, so it can decide what to do with it ! λn.n INC. next, we'll define P as a term that takes this INC, ignores it, and returns 0, just like we wanted: P := λg.0, now (λn.n INC) P reduces to 0.

small issue, λn.n INC doesn't expect a number, it expects a term that takes INC and does something with it, so we actually want P to reduce to something like λg.g 0, or more generally we want the output to reduce to λg.g N for some N when it runs. that's a simple fix, and finally it gives us our proper term for f: WRAP := λn.λg.g (n INC). ofcourse we also want to convert these special terms back into numbers at the end, we can simply pass λi.i into them. the special terms will take it, give it to their number and it will simply be returned. if we give it to P - which will happen only if we try to decrementz zero - it ignores the input and returns zero, so we're basically saying that the predecesor of zero is zero. which is... fine. it's fine, its certainly better than the term breaking. we like that the output is a number. we've then sucessfuly defined decrementing as WRAP P (λi.i). or, if we write it out a bit, DEC := λn.n (λh.λg.g (h INC)) (λg.0) (λi.i).

like with multiplication and exponents before, this can be simplified even more. particularly we can replace INC and 0 with f and x, since the structure we get out is already basically on point, but its not necesarry, if we wanted efficient math we'd be working with C++ haha.

sorry, im still working the next sections x] you'll have to give me a bit more time

fixed point theorem

encoding terms, fixed point theorem, its significance and proof, connection to halting problem