1. Home
  2. Explore
  3. Computer Science
  4. Lambda Calculus Fundamentals

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Lambda Calculus Fundamentals

Lambda Calculus: Functions, Computation, Types, and Logic

The basic idea

The lambda calculus is a formal system for describing functions and computation. It uses a very small set of syntactic forms and one central computation rule, yet it can express any computation that can be performed by a Turing machine. This makes it a foundational model for studying programming languages and computation mathematically.

The basic forms of an untyped lambda calculus are:

  • Variables, such as xxx or yyy.

  • Lambda abstractions, which define functions.

  • Applications, which apply a function to an argument.

The calculus has no built-in numbers, booleans, loops, or data structures. Instead, these can be represented using functions.

The lambda calculus is a minimal language of functions: its simplicity makes it useful for reasoning about what computation is, even though writing practical programs directly in it can be cumbersome.


Functions and variables

A function can be understood as a mapping from inputs to outputs. For example, a function that squares its input maps 222 to 444, 333 to 999, and so on. Instead of listing every input and its output, the function can be described using an arbitrary variable:

x↦x2x \mapsto x^2x↦x2

Here, xxx stands for any input. The specific symbol chosen for the variable does not change the function. These expressions describe the same function:

x↦x2andy↦y2x \mapsto x^2 \qquad\text{and}\qquad y \mapsto y^2x↦x2andy↦y2

The variable is a placeholder for the function’s input.

Alpha equivalence and alpha conversion

Two lambda expressions are alpha equivalent when they differ only in the name of a bound input variable. For example, the functions that square xxx, yyy, or another variable are alpha equivalent, provided the variable is renamed consistently within the function.

The process of changing a bound variable’s name is called alpha conversion. Renaming must preserve which occurrences refer to the function’s input. It changes notation, not behavior.


Lambda abstractions

Lambda calculus uses a compact notation to define functions. A function that takes input xxx and produces output mmm is written:

λx. m\lambda x.\,mλx.m
  • The symbol λ\lambdaλ marks the beginning of a function definition.

  • The variable immediately after it, xxx, is the function’s input.

  • The dot separates the input variable from the function’s body, mmm.

  • The body describes the result produced by the function.

For example, a function that adds 111 to its input is:

λx. x+1\lambda x.\,x+1λx.x+1

A function written this way is called a lambda abstraction. Its body can contain the input variable, other variables, or other lambda expressions.


Function application and beta reduction

A function becomes useful when it is applied to an argument. Application is written by placing the argument to the right of the function:

(λx. x+1) 3(\lambda x.\,x+1)\ 3(λx.x+1) 3

Applying the function means substituting the argument 333 for the input variable xxx wherever xxx occurs in the function’s body. This produces 3+13+13+1, which evaluates to 444.

The general computation rule is:

(λx. m) n  →β  m[x:=n](\lambda x.\,m)\ n \;\to_\beta\; m[x:=n](λx.m) n→β​m[x:=n]

Here, m[x:=n]m[x:=n]m[x:=n] means “the expression mmm with nnn substituted for xxx.” The arrow labeled β\betaβ indicates that a beta reduction has taken place.

For example:

(λx. x+1) 3  →β  3+1  →  4(\lambda x.\,x+1)\ 3 \;\to_\beta\; 3+1 \;\to\; 4(λx.x+1) 3→β​3+1→4

Beta reduction is the central computation rule of the lambda calculus: applying a function substitutes its argument into the function’s body.


Higher-order functions and multiple inputs

Lambda abstractions take one input at a time. This does not prevent the calculus from representing functions that behave as though they take multiple inputs. The key is that a function can return another function.

A higher-order function is a function that can take functions as inputs, return functions as outputs, or both. This ability is central to the expressive power of the lambda calculus.

Currying

Consider a function that first takes xxx and returns a new function. The new function takes yyy and produces x+yx+yx+y:

λx. λy. x+y\lambda x.\,\lambda y.\,x+yλx.λy.x+y

Applying this function to 111 gives a function that adds 111 to its next input:

(λx. λy. x+y) 1  →β  λy. 1+y(\lambda x.\,\lambda y.\,x+y)\ 1 \;\to_\beta\; \lambda y.\,1+y(λx.λy.x+y) 1→β​λy.1+y

Applying the result to 222 gives:

(λy. 1+y) 2  →β  1+2  →  3(\lambda y.\,1+y)\ 2 \;\to_\beta\; 1+2 \;\to\; 3(λy.1+y) 2→β​1+2→3

The two arguments are supplied sequentially. More generally, applying λx.λy. m\lambda x.\lambda y.\,mλx.λy.m to aaa and then bbb substitutes aaa for xxx and bbb for yyy:

(λx. λy. m) a b  →β  m[x:=a, y:=b](\lambda x.\,\lambda y.\,m)\ a\ b \;\to_\beta\; m[x:=a,\ y:=b](λx.λy.m) a b→β​m[x:=a, y:=b]

Representing a multi-input function as a chain of one-input functions is called currying, named after the logician Haskell Curry. In a curried function, supplying the first argument produces a function that is still waiting for the next argument.


Representing booleans and conditionals

Because the basic calculus has no built-in boolean values, truth and falsity can be represented by functions.

Define true to select its first argument:

true≡λx. λy. x\text{true} \equiv \lambda x.\,\lambda y.\,xtrue≡λx.λy.x

Define false to select its second argument:

false≡λx. λy. y\text{false} \equiv \lambda x.\,\lambda y.\,yfalse≡λx.λy.y

A conditional can then be represented as:

if≡λb. λx. λy. b x y\text{if} \equiv \lambda b.\,\lambda x.\,\lambda y.\,b\ x\ yif≡λb.λx.λy.b x y

This function takes three inputs:

  1. A boolean bbb.

  2. A result to use when the boolean is true, xxx.

  3. A result to use when the boolean is false, yyy.

The boolean chooses between the two possible results.

Evaluating a conditional with true

Applying the conditional to true, mmm, and nnn gives:

if true m n  →β  true m n  →β  m\text{if}\ \text{true}\ m\ n \;\to_\beta\; \text{true}\ m\ n \;\to_\beta\; mif true m n→β​true m n→β​m

The true encoding selects its first argument, so the result is mmm.

Evaluating a conditional with false

Applying the conditional to false, mmm, and nnn gives:

if false m n  →β  false m n  →β  n\text{if}\ \text{false}\ m\ n \;\to_\beta\; \text{false}\ m\ n \;\to_\beta\; nif false m n→β​false m n→β​n

The false encoding selects its second argument, so the result is nnn.

The representations may look unlike ordinary boolean values, but their behavior gives them the role of truth values. A function such as logical negation can be written in terms of the encoded concepts:

not≡λb. if b false true\text{not} \equiv \lambda b.\,\text{if}\ b\ \text{false}\ \text{true}not≡λb.if b false true

It swaps true and false.


Expressiveness and the Church–Turing thesis

The untyped lambda calculus has only variables, abstractions, applications, and beta reduction. Despite this, it can represent computation as powerful as that of a Turing machine. This is one form of the connection associated with the Church–Turing thesis: the lambda calculus and Turing machines capture the same broad class of effectively computable procedures.

This does not mean that every practical programming feature appears as a built-in construct. Rather, features such as numbers, booleans, conditionals, recursion, and data structures can be encoded from the calculus’s basic forms. The calculus is therefore computationally powerful but often inconvenient for direct, everyday programming.

Encodings and abstraction

Once a feature has been defined as an encoding, it can be treated as a meaningful object without repeatedly expanding its low-level definition. For instance, after defining booleans and conditionals, a programmer can reason about an expression as an if statement rather than tracing every beta reduction that implements it.

This separation between a feature’s representation and its use is an important programming idea: an implementation can be detailed while the interface presented to the programmer remains simple.


Types and the simply typed lambda calculus

In the untyped calculus, any expression can be applied to any other expression, even if the result is meaningless. For example, applying a boolean operation to a number may produce an expression that has no sensible interpretation.

A type system attaches labels to expressions to describe what kinds of values they represent and restricts how those expressions can be used. The simply typed lambda calculus commonly begins with basic types such as:

  • bool\text{bool}bool for boolean values.

  • num\text{num}num for numeric values.

A type is written after an expression with a colon. For example:

true:bool\text{true} : \text{bool}true:bool

This says that the encoded value true has boolean type.

Function types

If a function takes an input of type AAA and returns an output of type BBB, its type is written:

A→BA \to BA→B

For example, a function that takes a boolean and returns a boolean has type:

bool→bool\text{bool} \to \text{bool}bool→bool

The type of a lambda abstraction includes the type of its input. A typed function can be written with the input type beside its variable, as in:

λb:bool. if b false true\lambda b:\text{bool}.\,\text{if}\ b\ \text{false}\ \text{true}λb:bool.if b false true

This function takes a boolean and returns a boolean, so it has type bool→bool\text{bool}\to\text{bool}bool→bool.

Type checking prevents invalid applications

A typed function can be applied only to an argument with the input type it requires. Since the not function expects a boolean, applying it to a number is rejected:

  • The input type required by not is bool\text{bool}bool.

  • The type of a numeric input is num\text{num}num.

  • Since these types do not match, the application is not allowed.

Type checking prevents many nonsensical expressions from being formed. It does not make every program meaningful or correct, but it rules out classes of errors before evaluation.

The simply typed lambda calculus is the lambda calculus equipped with this basic type discipline. Its explicit types resemble those in statically typed languages such as C or Java. The underlying lambda calculus was developed by Alonzo Church in the 1930s and 1940s, before programming languages and computer science had been formally established as modern disciplines.


The Curry–Howard correspondence: types as propositions

Types can be interpreted not only as classifications of program values but also as propositions—statements that may be provable. Under this interpretation:

  • A type represents a proposition.

  • A term of that type represents a proof of the proposition.

  • Constructing a term of a type corresponds to constructing a proof of the associated statement.

For example, if AAA is a type interpreted as a proposition, then a term of type AAA can be understood as evidence that AAA is true. If no term can be constructed, then the proposition has no proof in the system. This means “unprovable in the system,” not necessarily false in every possible interpretation.

Function types correspond to implication

The function type A→BA\to BA→B corresponds to the logical proposition “if AAA, then BBB,” written A⇒BA\Rightarrow BA⇒B.

A function of type A→BA\to BA→B can be viewed as a method for transforming evidence of AAA into evidence of BBB. If a proof of AAA is supplied to that function, its output is a proof of BBB.

For example, let:

  • AAA mean “it is raining.”

  • BBB mean “the ground is wet.”

Then A⇒BA\Rightarrow BA⇒B means “if it is raining, then the ground is wet.” A proof of this implication can be represented by a function that takes evidence that it is raining and returns evidence that the ground is wet.

Function application corresponds to modus ponens

In logic, modus ponens is the rule that from AAA and A⇒BA\Rightarrow BA⇒B, one may conclude BBB.

The typed lambda calculus represents this directly:

  1. Suppose there is a term nnn of type AAA.

  2. Suppose there is a function fff of type A→BA\to BA→B.

  3. Applying fff to nnn produces a term of type BBB.

f:A→Bn:Af n:Bf : A\to B \qquad n : A \qquad f\ n : Bf:A→Bn:Af n:B

Thus, applying a function to an argument mirrors using an implication together with its premise to derive a conclusion.

The relationship between types and propositions, and between programs and proofs, is called the Curry–Howard correspondence.


Proof assistants and formal verification

A programming language designed around the Curry–Howard correspondence can be used to express mathematical propositions and construct proofs as programs. A proof assistant checks whether the constructed term has the required type. If it does, the proof satisfies the system’s formal rules.

A proof assistant is software that supports the creation and checking of formal proofs. In this approach:

  • A theorem is represented by a type.

  • A proof is represented by a term of that type.

  • Type checking verifies that the term is a valid proof according to the formal system.

This provides a way to verify mathematical arguments and computational constructions rigorously. A proof assistant can confirm that a proof is well-formed within its logic, though the result still depends on the correctness of the system and its foundations.


Why the lambda calculus matters

The lambda calculus is not generally convenient as a standalone language for ordinary software development. Simple features often require elaborate encodings. Its importance lies instead in what its simplicity makes possible:

  • It provides a precise mathematical account of functions and computation.

  • It shows how rich computational behavior can arise from a small number of rules.

  • It offers a framework for reasoning about programs and programming-language design.

  • With types, it connects program construction to logical proof construction.

  • It underlies ideas used in functional programming and formal verification.

The lambda calculus demonstrates that a compact formal system can serve both as a model of computation and as a bridge between programming languages and mathematical logic.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent