Lambda Calculus Fundamentals


Lambda Calculus Fundamentals

Michael
Learning a programming language can feel like learning two things at once: a lot of syntax, and the rules for how that syntax actually computes. The lambda calculus takes an astonishingly small approach. It has just three pieces of syntax—variables, function definitions, and function applications—and one basic computation rule, beta reduction.

Maria
And the surprising claim is that, despite being so tiny, it can express anything that any other programming language can compute. So this isn't just a minimal notation exercise. It gives us a mathematical way to study computation itself.

Michael
Let's start with what a function means here. Not necessarily a big block of code: just a mapping from an input to an output. For example, a function might map a number to its square, so two maps to four.

Maria
We don't want to list every possible input and its output, though. Instead, we can describe the general rule: take an arbitrary input called x, and return x squared. The x is a variable—a name standing in for whatever input we choose.

Michael
And the name itself doesn't matter. If we rename x to y everywhere it refers to that input, we still describe the same function. So “map x to x squared” and “map y to y squared” are alpha equivalent. The renaming process is called alpha conversion.

Maria
That gives us a compact notation for functions. We write the Greek letter lambda, then the input variable, then a dot, then the output expression: lambda x dot x squared. The lambda marks that we're defining a function, and the dot separates its input from its output. A function written this way is called a lambda abstraction.

Michael
But defining a function isn't the same as using it. To use one, we apply it to an input. Suppose our function is lambda x dot x plus one, and we apply it to three. We replace x in the output with three, giving three plus one, which is four.

Maria
That replacement is called beta reduction. We show it with an arrow marked beta. In general, applying lambda x dot M to an input N means substituting N for x in M. You can think of the slash notation as saying “substitute this term for that variable.”

Michael
So we've got our three ingredients: variables, lambda abstractions, and applications. And the one computation rule we've introduced is beta reduction. It sounds like a very primitive programming language—but the twist is that we don't need to add loops, booleans, or data structures as fundamental pieces.

Maria
The lambda calculus can encode those kinds of things using its basic ingredients. Its expressive power is tied to higher-order functions: functions can take other functions as inputs, and they can return functions as outputs. That ability gives this tiny system much more reach than you might expect.

Michael
Here's one place that higher-order behavior helps: a lambda abstraction has only one input variable, but we can still represent a function that takes two inputs. Start with a function that takes x and returns another function, one that takes y and returns x plus y.

Maria
Let's apply the outer function to one. We substitute one for x, and what comes back is a new function: lambda y dot one plus y. Apply that to two, substituting two for y, and the result is one plus two, or three.

Michael
So the two inputs arrive one after another. More generally, for lambda x dot lambda y dot L, applying it to M and then N first substitutes M for x, and then substitutes N for y. The final result is L with both substitutions made.

Maria
That technique—turning a multiple-input function into a sequence of one-input functions—is called currying, after the logician Haskell Curry. It may feel unusual if you're used to giving a function all its inputs at once, but it is a standard way to represent multiple inputs with one-input abstractions.

Michael
We can also encode booleans. In this setup, true is a function that takes two inputs and returns the first: lambda x dot lambda y dot x. False takes two inputs and returns the second: lambda x dot lambda y dot y.

Maria
Those definitions can look arbitrary until you use them. An if expression can be written as lambda b dot lambda x dot lambda y dot b applied to x and y. It takes a boolean and two possible results, then uses the boolean to choose which result to return.

Michael
If b is true, then b returns its first input, so the expression returns x. If b is false, it returns its second input, so the expression returns y. In other words, the boolean itself acts as the choice between the two branches.

Maria
Let's trace the true case. Apply the if expression to true, then M, then N. The first application supplies the boolean, the second supplies M as x, and the third supplies N as y. That leaves true applied to M and N.

Michael
Now expand the definition of true: it takes two arguments and returns the first. So after it receives M and N, it returns M. If we used false instead, it would return N. That is exactly the behavior we want from an if expression.

Maria
Once we've shown that booleans and conditionals can be represented this way, we don't have to keep unfolding their definitions every time we use them. We can treat them as familiar concepts and work at a more convenient level. For instance, not can be described as taking a boolean b and returning if b then false, else true.

Michael
That example points to an important distinction: the lambda calculus is powerful, but it isn't necessarily convenient for everyday programming. Encoding ordinary features by hand can be tedious. The point is not that this tiny notation is the best practical programming language; it is that it lets us study the foundations of programming with very few assumptions.

Maria
And the calculus can encode more than the examples we've worked through. Numbers, recursion, and data types such as pairs can also be represented. The important idea is that familiar programming concepts can be built from the small set of basic forms, even if doing so directly is not the most comfortable way to write a real program.

Michael
That mathematical viewpoint also lets us ask which expressions make sense. Consider not again: it is meant to take a boolean and switch true to false or false to true. But without a rule restricting its input, we could apply it to a number such as one.

Maria
Then the calculation would try to treat one as a boolean in an if expression. That doesn't make sense under the encodings we've defined. To prevent this kind of mismatch, we can add types: labels that describe what kind of term we're working with.

Michael
For this example, we can use the types bool and num. We write a term's type after a colon, so true colon bool says that true has type boolean. A function that takes an input of type A and returns an output of type B has type A to B, often written with an arrow between A and B.

Maria
We can mark the input type right beside the variable in a lambda abstraction. Then we only allow the function to be applied to an argument with the matching type. So not takes a boolean and returns a boolean; its type is bool to bool, and applying it to a number is not allowed.

Michael
Adding these type rules limits which expressions we can form, but it also rules out many nonsensical ones. This typed version is called the simply typed lambda calculus. Its type discipline is reminiscent of statically typed programming languages such as C or Java.

Maria
The historical context is striking. Alonzo Church developed the lambda calculus in the 1930s and 1940s, before programming and computer science were being formally studied. Church was also the doctoral adviser of Alan Turing, whose work earned him the title of a father of computer science.

Michael
The lambda calculus matters not only for programming, but also for mathematical logic. With the simply typed version, we can interpret types as propositions—statements that may be true or false—and terms as proofs of the propositions represented by their types.

Maria
For example, take a type A and view it as a proposition. A term of type A then counts as a proof that A is true. If there's no term of that type, then under this interpretation there is no proof of the proposition.

Michael
Now look at function types. A function from type A to type B corresponds to the proposition “A implies B.” If we have a proof of A, and we also have a proof of “A implies B,” applying the function to the proof of A gives us a term of type B. That term is the proof that B follows.

Maria
You can picture the reasoning as assumptions above a line and the conclusion below it. We assume A and A implies B; then function application combines the corresponding terms to produce a term of type B. The calculation in the programming language mirrors the logical step.

Michael
This relationship between types and propositions, and between programs and proofs, is called the Curry–Howard correspondence. It has a remarkable practical consequence: a programming language designed with this correspondence in mind can be used to write mathematical proofs as code.

Maria
Those systems are called proof assistants. Lean and Agda are examples. A proof assistant can check whether a proposed proof is well formed by checking its types. For example, a Lean program can express a proof that there are infinitely many prime numbers, and type-checking provides a way to verify that proof.

Michael
So the journey began with a compact way to describe functions, but it led to a mathematical model of computation, a way to reason about programs, and a connection between programming and logic. The lambda calculus isn't a replacement for practical languages so much as a foundation for understanding what computation can mean.

Maria
Exactly. A few simple rules can support a surprisingly rich theory. By studying the lambda calculus, we can explore how programs compute, how types prevent certain errors, and how code can serve as a rigorous mathematical proof.

Ask anything
We've read this whole study set, so questions about it get the best answers.