Link to this sectionChurch Encoding, Parametricity, and the Yoneda Lemma

I still remember the shock I felt when I first encountered functional programming years ago. That was the moment I learned that natural numbers can be built within the language itself:

data Nat = Zero | Succ Nat

I went on to learn that all computation can be expressed through functions (the lambda calculus), that recursion itself can be encoded as the mind-bending Y combinator:

Y = λf. (λx. f (x x)) (λx. f (x x))

And then there were Church numerals, where each number becomes a function:

0 = λs. λz. z
1 = λs. λz. s z
2 = λs. λz. s (s z)

Church encoding represents natural numbers as functions; each number 𝑛 takes a successor function and a starting point, then applies the successor 𝑛 times.

But what is the reasoning behind it? Why these particular lambda terms?

There is also Church encoding for lists:

nil  = λc. λn. n
cons = λx. λxs. λc. λn. c x (xs c n)

The pattern feels similar, but the details are different. For a long time I assumed this was just how things were: a clever trick, rediscovered case by case.

It is after many years of learning that I finally understand the deeper story. Church encoding manifests deep connections between data types, polymorphism, and category theory. Once you see the connections, the encoding’s shape stops being a trick and starts being inevitable.

I want to trace those connections here. We’ll start with the simplest data types, watch a pattern emerge, and build up the machinery: parametricity, algebraic data types, F-algebras, the Yoneda Lemma, until Church encoding looks less like a clever trick and more like something the math demands.

Link to this sectionSystem F

To capture the pattern behind Church encoding, we need types. The untyped lambda calculus gives us the terms, but it gives us no vocabulary to say what those terms are. That’s the Simply Typed Lambda Calculus (STLC), where every term has a type. The only type constructor in bare STLC is the function arrow 𝐴→𝐵11.To be precise, STLC also includes some base types, like 𝚄𝚗𝚒𝚝, 𝙱𝚘𝚘𝚕, or 𝙸𝚗𝚝; otherwise you cannot give the parameter of the innermost function a type. But the particular choice of base types is irrelevant to the story; we just need something in the language to talk about..

But STLC hits a wall almost immediately. A Church numeral must accept any type as its carrier: the whole point is that 2 works the same whether you’re counting apples or functions. STLC can’t say that. Every function in STLC has a fixed, monomorphic type.

This is where the experienced programmer sits up: we need generics. We need functions that are polymorphic over types. The calculus that gives us this is System F, the polymorphic lambda calculus.

In System F, a function can take a type as an argument. You write type abstraction as Λ𝑋.𝑡 and type application as 𝑓[𝑋]. Types of System F include type variables 𝑋, function types 𝐴→𝐵, and universal quantification ∀𝑋.𝐴.22.Unlike STLC, base types are not necessary in System F. The polymorphism alone gives us enough structure to build everything we need.

Our System F starts as an empty universe. In STLC, we would declare base types like 𝚄𝚗𝚒𝚝 and 𝙱𝚘𝚘𝚕 to get started. But in System F, it’s interesting that we can build these types from scratch, using only functions and polymorphism. We can encode 𝚄𝚗𝚒𝚝 and 𝙱𝚘𝚘𝚕 as polymorphic functions, without any special syntax for data types.

Let me state a principle: a data type can be equivalently represented by the ways you consume it. I can’t yet explain the logic behind this claim, but let’s set it aside and see how it works for 𝚄𝚗𝚒𝚝. 33.In the second half of this article, we’ll see the category-theoretic foundation for this principle. The Yoneda Lemma will show that the set of all ways to consume a type fully determines the type itself.

If you’re holding a value of type 𝚄𝚗𝚒𝚝, what can you do with it? The only way to consume a 𝚄𝚗𝚒𝚝 value is to ignore it and return something that has nothing to do with it.

Suppose you want to write a universal consumer: for any target type 𝑋, it turns a 𝚄𝚗𝚒𝚝 into an 𝑋. Because we know nothing about 𝑋, we cannot make up any new value of type 𝑋. The only thing we can do is to return an 𝑋 that we already have. But where do we get an 𝑋 from? We don’t have one! We should receive it as an argument. Therefore, the type signature of a universal consumer of 𝚄𝚗𝚒𝚝 is:

∀𝑋.𝑋→𝑋

Read as: “for any type 𝑋, give me an 𝑋 and I’ll give you back an 𝑋.”

What inhabits this type? It is easy to think of one: the identity function.44.Later we’ll see that this is the only possible implementation, as a consequence of parametricity. It takes an 𝑋 and returns it unchanged:

id = ΛX. λx. x

This function is the Church encoding of 𝚄𝚗𝚒𝚝.

Let’s try the same reasoning with 𝙱𝚘𝚘𝚕. 𝙱𝚘𝚘𝚕 has two constructors, 𝚃𝚛𝚞𝚎 and 𝙵𝚊𝚕𝚜𝚎.

What does consuming a 𝙱𝚘𝚘𝚕 require? You need a target type 𝑋, and you need an interpretation for 𝚃𝚛𝚞𝚎 and one for 𝙵𝚊𝚕𝚜𝚎, two values of type 𝑋. Then, depending on which constructor the actual 𝙱𝚘𝚘𝚕 value represents, you return the appropriate interpretation.

As a type signature:

∀X. X → X → X

Read as: “for any type 𝑋, give me two 𝑋s and I’ll give you back an 𝑋.”

We can find two inhabitants of this type, which correspond to the two ways to consume a 𝙱𝚘𝚘𝚕:

true  = ΛX. λx. λy. x
false = ΛX. λx. λy. y

One chooses the first argument, the other chooses the second. They correspond exactly to the two values of 𝙱𝚘𝚘𝚕.

Looking at these two examples, a pattern emerges:

The Church encoding of a data type in System F is a polymorphic function whose arguments match the constructors. It is a universal consumption interface for the type. This works perfectly for 𝚄𝚗𝚒𝚝 and 𝙱𝚘𝚘𝚕, whose constructors carry no data. But what about a type like the 𝙽𝚊𝚝? Its 𝚂𝚞𝚌𝚌 constructor has a recursive parameter. We’ll handle recursion soon. But before we do, we need to understand the mechanism that makes the pattern work in the first place.

Link to this sectionParametricity

What’s striking is that for ∀𝑋.𝑋→𝑋, we can only find a single implementation, the identity function. Likewise, for ∀𝑋.𝑋→𝑋→𝑋, we can only find two implementations, true and false. The type system of System F does not permit any other implementations.

Once you have convinced yourself of the fact, let’s move on.

To understand why the type system restricts the inhabitants so precisely, we need to examine the mechanism behind System F’s polymorphism. The polymorphism in System F is parametric: a polymorphic function gets a type argument, but it can’t inspect it. It can’t branch on “am I dealing with Int or Bool?” It must treat the type as a black box. That restriction turns out to be the source of enormous power.

John Reynolds proved the parametricity theorem (also called the abstraction theorem) in 1983. Here is a compact statement:

Let 𝑓:∀𝑋.𝜏(𝑋) be a closed term of System F, where 𝜏(𝑋) is a type expression. For any types 𝐴,𝐵 and any relation 𝑅⊆𝐴×𝐵, let 𝜏(𝑅)⊆𝜏(𝐴)×𝜏(𝐵) be the relational lifting defined recursively from the structure of 𝜏, then (𝑓[𝐴],𝑓[𝐵])∈𝜏(𝑅).

The formulation looks abstract, but the message is simple: a polymorphic function must “preserve” whatever relation you associate with the type parameter. It cannot detect the concrete structure of the type it’s instantiated with. A consequence is that you can derive equations the function must satisfy purely from its type signature, regardless of implementation. Philip Wadler later called these “theorems for free.”

We’ll start with a special case you can verify by hand.

Claim: ∀𝑋.𝑋→𝑋 has only the identity function, i.e., any 𝑓:∀𝑋.𝑋→𝑋 must satisfy 𝑓[𝐴]𝑎=𝑎 for all 𝐴 and 𝑎.

Proof: Take two arbitrary types 𝐴 and 𝐵, and pick any relation 𝑅⊆𝐴×𝐵 between them. The theorem requires us to lift 𝑅 through the type expression 𝜏(𝑋)=𝑋→𝑋. The lifting works like this: two functions ℎ𝐴:𝐴→𝐴 and ℎ𝐵:𝐵→𝐵 are related by 𝜏(𝑅) precisely when they map 𝑅-related inputs to 𝑅-related outputs, that is, (ℎ𝐴(𝑥),ℎ𝐵(𝑦))∈𝑅 whenever (𝑥,𝑦)∈𝑅. Intuitively, ℎ𝐴 and ℎ𝐵 preserve 𝑅 in lockstep.

Now, parametricity says: because 𝑓 is polymorphic, its instantiations at 𝐴 and 𝐵 must be related by this lifted relation. In symbols: (𝑓[𝐴],𝑓[𝐵])∈𝜏(𝑅). Spelling that out: for any (𝑥,𝑦)∈𝑅, we have (𝑓[𝐴]𝑥,𝑓[𝐵]𝑦)∈𝑅.

Here comes the trick. Fix a type 𝐴 and a value 𝑎:𝐴. We want to show 𝑓[𝐴]𝑎=𝑎. Define a very specific relation that pairs 𝑎 with itself and nothing else:

𝑅={(𝑎,𝑎)}⊆𝐴×𝐴

Take 𝐵=𝐴. Since (𝑎,𝑎)∈𝑅, parametricity forces:

(𝑓[𝐴]𝑎,𝑓[𝐴]𝑎)∈𝑅

But 𝑅 contains exactly one pair: (𝑎,𝑎). So 𝑓[𝐴]𝑎 must be 𝑎. Done.

Parametricity forced 𝑓 to be the identity function. 𝑓 knows nothing about the type it’s applied to; the type signature alone left it no other choice.

A similar argument works for ∀𝑋.𝑋→𝑋→𝑋. A function of this type receives two values of an unknown type 𝑋 and must return an 𝑋. With no operations available on 𝑋, the only possible behaviors are “always return the first argument” or “always return the second.” That’s true and false.55.A formal proof follows the same relational strategy, using 𝑅={(𝑎,𝑎),(𝑏,𝑏)} for two distinct values 𝑎,𝑏:𝐴.

Now let’s look at the general shape 𝑓:∀𝑋.(𝐹𝑋→𝑋)→𝑋. Here 𝐹 is any type operator; think of it as describing the structure of a data type. The free theorem we get is this: for any F-algebra homomorphism66.We’ll explain what an F-algebra homomorphism is later. ℎ:(𝐴,𝑓𝐴)→(𝐵,𝑓𝐵),

ℎ(𝑓[𝐴]𝑓𝐴)=𝑓[𝐵]𝑓𝐵

This equation will play a central role later when we prove that Church encoding is equivalent to the data type it encodes.

Link to this sectionAlgebraic Data Types and Functors

With parametricity in our toolkit, we now return to the main thread. Recall where we left off: 𝚄𝚗𝚒𝚝 and 𝙱𝚘𝚘𝚕 revealed a pattern (Church encoding is a polymorphic function whose arguments match the constructors), but we had no way to handle recursive types like 𝙽𝚊𝚝. To extend the pattern, we need a systematic language for describing the structure of data types — something that makes constructors and their arguments explicit. This is where algebraic data types (ADT) come in.

We know that tuples are product types and tagged unions are sum types. An algebraic data type is built from these two operations, fundamentally, as a sum of products. Each constructor is a product of its fields; the whole type is the sum among constructors. If a constructor carries no data, it is the empty product, i.e., the 𝚄𝚗𝚒𝚝 type.

We can make this precise with a small algebraic notation:

Under this notation:

𝚄𝚗𝚒𝚝=1𝙱𝚘𝚘𝚕=1+1𝙽𝚊𝚝=1+𝙽𝚊𝚝

The last line reads: a natural number is either 𝚉𝚎𝚛𝚘 (the 1 branch, carrying no information) or 𝚂𝚞𝚌𝚌 (wrapping another 𝙽𝚊𝚝). The equation is recursive, and we make sense of it with the 𝜇 binder:

𝙽𝚊𝚝=𝜇𝑋.1+𝑋

The same recipe works for lists:

𝙻𝚒𝚜𝚝(𝑎)=𝜇𝑋.1+𝑎×𝑋

Here, 𝙽𝚒𝚕 is the 1 branch and 𝙲𝚘𝚗𝚜 is the 𝑎×𝑋 branch.

But the algebra goes deeper than notation. Types surprisingly obey the familiar laws of algebra. For example, the distributivity law:

𝑎×(𝑏+𝑐)≅𝑎×𝑏+𝑎×𝑐

Here ≅ denotes type isomorphism: there exist functions witnessing the equivalence in both directions.

Read the law as types: (a, Either b c) is isomorphic to Either (a, b) (a, c). As evidence, we can write the following functions:

f :: (a, Either b c) -> Either (a, b) (a, c)
f (a, Left b)  = Left  (a, b)
f (a, Right c) = Right (a, c)

g :: Either (a, b) (a, c) -> (a, Either b c)
g (Left (a, b))  = (a, Left b)
g (Right (a, c)) = (a, Right c)

What about function types? Can we express them in terms of algebraic operations?

Let me declare: functions are exponents. Consider two finite types 𝐴 and 𝐵, with |𝐴| and |𝐵| inhabitants respectively. How many functions 𝐴→𝐵 exist? For each input 𝑎∈𝐴, we can independently choose any output 𝑏∈𝐵. That’s |𝐵| choices repeated |𝐴| times: |𝐵||𝐴| possibilities. This suggests writing the function type as an exponential:

𝐴→𝐵≅𝐵𝐴

Currying then becomes the familiar exponent law:

𝐶𝐴×𝐵≅(𝐶𝐵)𝐴

And functions out of sums follow their own distributive law:

𝐶𝐴+𝐵≅𝐶𝐴×𝐶𝐵

Now let’s return to the Church encoding of natural numbers. We have seen Church numerals in untyped lambda calculus:

0 = λs. λz. z
1 = λs. λz. s z
2 = λs. λz. s (s z)

We want to give them types in System F. Assume the type of z is 𝑋. We expect the type of 0 and 1 to be the same, so term s z should have the same type as z. Therefore, s must have type 𝑋→𝑋. That gives us the Church encoding type for natural numbers:

ChurchNat=∀𝑋.𝑋→(𝑋→𝑋)→𝑋

It looks somewhat different from the Church encoding types for Unit and Bool. The second argument is a function rather than a value. Is there a single shape that captures all of them?

Let’s inspect the inner part of the encoding more closely.

Currying tells us that a function 𝑋→(𝑋→𝑋)→𝑋 is equivalent to a function (𝑋,𝑋→𝑋)→𝑋. And we already know that the tuple (𝐴,𝐵) can be written as 𝐴×𝐵, and the function type 𝐴→𝐵 can be written as 𝐵𝐴. Then we can do some algebraic manipulation:

𝑋→(𝑋→𝑋)→𝑋≅𝑋×𝑋𝑋→𝑋≅𝑋1+𝑋→𝑋≅((1+𝑋)→𝑋)→𝑋

So the full Church encoding becomes:

ChurchNat≅∀𝑋.((1+𝑋)→𝑋)→𝑋

Look at the expression 1+𝑋. That is precisely the body of the recursive type definition of 𝙽𝚊𝚝=𝜇𝑋.1+𝑋. In other words, the Church encoding is of the form:

∀𝑋.(𝐹𝑋→𝑋)→𝑋where𝐹𝑋=1+𝑋

Here, 𝐹 is a functor that captures the shape of the algebraic data type. Let’s check that this holds for our earlier examples:

Here we get the general pattern: for an algebraic data type 𝑇=𝜇𝑋.𝐹𝑋, its Church encoding is ∀𝑋.(𝐹𝑋→𝑋)→𝑋.

In the next section, we’ll see that ∀𝑋.(𝐹𝑋→𝑋)→𝑋 is the type of universal consumers of F-algebras, and this is the perspective that will lead us, via parametricity and the Yoneda Lemma, to the deepest understanding of what Church encoding really is.

Link to this sectionF-Algebra

We’ve arrived at a turning point. The last section ended with a shape, ∀𝑋.(𝐹𝑋→𝑋)→𝑋, that unifies the Church encodings of 𝚄𝚗𝚒𝚝, 𝙱𝚘𝚘𝚕, and 𝙽𝚊𝚝. But the shape still feels pulled from thin air. Why this particular arrangement of symbols? To answer that, we need to give names to the pieces. Category theory enters here.

A category is a collection of objects and arrows between them, with sensible rules for composition and identity. For a programmer, the natural example is the category 𝐒𝐞𝐭: objects are types, arrows are functions, composition is function composition, and the identity arrow is id.

A functor 𝐹 is a mapping between categories. In our world, 𝐹 maps each type 𝐴 to a type 𝐹𝐴, and each function 𝑓:𝐴→𝐵 to a function 𝐹𝑓:𝐹𝐴→𝐹𝐵, preserving composition and identity:

𝐹(𝑔∘𝑓)=𝐹𝑔∘𝐹𝑓𝐹id=id

This is what the 𝐹 in 𝐹𝑋=1+𝑋 has been doing: it builds a new type from an old one, and it lifts functions to act inside that structure.77.For 𝙽𝚊𝚝𝙵, given 𝑓:𝑋→𝑌, 𝙽𝚊𝚝𝙵𝑓 maps 𝙻𝚎𝚏𝚝() to 𝙻𝚎𝚏𝚝() and 𝚁𝚒𝚐𝚑𝚝𝑥 to 𝚁𝚒𝚐𝚑𝚝(𝑓𝑥). This function-lifting is the machinery behind structural recursion; we’ll return to it when we define folds.

A natural transformation 𝜂 between two functors 𝐹 and 𝐺 is a way of turning 𝐹 shapes into 𝐺 shapes, uniformly for all types. For each type 𝑋, you have an arrow 𝜂𝑋:𝐹𝑋→𝐺𝑋, and for every function ℎ:𝑋→𝑌 the following square commutes:

That is, 𝜂𝑌∘𝐹ℎ=𝐺ℎ∘𝜂𝑋. This is naturality: the transformation must work the same way no matter which object you look at. Here’s the connection: the free theorems we got from parametricity are naturality conditions in disguise. A polymorphic function in System F is a natural transformation when viewed through the right categorical lens. Parametricity and naturality are two sides of the same coin.

With these three concepts — category, functor, natural transformation — we can finally name the pattern that has been staring at us since the very beginning. An F-algebra is a triple ⟨𝐹,𝑋,𝑓⟩ consisting of a functor 𝐹, a carrier type 𝑋, and a function 𝑓:𝐹𝑋→𝑋.

Intuitively, 𝑓:𝐹𝑋→𝑋 is an evaluator that folds the structure described by 𝐹 into a single value of type 𝑋. For example, the functor of natural numbers is 𝙽𝚊𝚝𝙵𝑋=1+𝑋. An F-algebra for this functor consists of a type 𝑋 and a function 𝑓:1+𝑋→𝑋. By algebraic manipulation, this becomes a pair of functions (𝑧:𝑋,𝑠:𝑋→𝑋), where

For 𝙱𝚘𝚘𝚕𝙵𝑋=1+1, a 𝙱𝚘𝚘𝚕𝙵-algebra is simply two values of type 𝑋: the algebra map 𝑓:1+1→𝑋 corresponds to a pair (𝑥1:𝑋,𝑥2:𝑋), where 𝑥1 evaluates true and 𝑥2 evaluates false.

The Church encoding type ∀𝑋.(𝐹𝑋→𝑋)→𝑋 now acquires a precise meaning: it is a universal consumer of F-algebras: given any F-algebra ⟨𝐹,𝑋,𝑓⟩, it takes the evaluator 𝑓:𝐹𝑋→𝑋 and runs it on the data type’s structure to produce a value of type 𝑋. It makes the “consumption interface” idea rigorous.

F-algebras for a fixed functor 𝐹 themselves form a category. An arrow in this category is called an F-algebra homomorphism. If ⟨𝐹,𝐴,𝑓⟩ and ⟨𝐹,𝐵,𝑔⟩ are two F-algebras, a homomorphism between them is a function ℎ:𝐴→𝐵 that preserves the algebraic structure:

ℎ∘𝑓=𝑔∘𝐹ℎ

In diagram form:

The condition says: evaluating a structure in 𝐴 and then translating the result to 𝐵 yields the same value as translating the sub-structure to 𝐵 first and then evaluating in 𝐵. The two paths through the square produce the same result.

For example, for 𝙽𝚊𝚝𝙵𝑋=1+𝑋, the homomorphism condition becomes:

ℎ(𝑧𝐴)=𝑧𝐵ℎ(𝑠𝐴𝑎)=𝑠𝐵(ℎ𝑎)

These two equations are exactly the pattern that any structural recursion must obey: the base case maps to the base case, and the recursive case applies ℎ to the predecessor and then the successor function.

Link to this sectionInitial Algebra

Among the F-algebras for a given functor 𝐹, one deserves special attention. In a category, an initial object satisfies the initiality property: there is a unique arrow from it to every other object. In the category of F-algebras, the initial object is called the initial algebra, written ⟨𝐹,𝜇𝐹,𝚒𝚗⟩, where 𝜇𝐹 is the carrier and 𝚒𝚗:𝐹(𝜇𝐹)→𝜇𝐹 is the structure map.

By initiality, for any F-algebra ⟨𝐹,𝑋,𝑓⟩, there is exactly one homomorphism from ⟨𝐹,𝜇𝐹,𝚒𝚗⟩ to it. We call it 𝚏𝚘𝚕𝚍𝑓:𝜇𝐹→𝑋.

To get an intuitive grasp of what the initial algebra is, let’s look at the type of 𝚏𝚘𝚕𝚍 in System F. It takes an evaluator 𝑓:𝐹𝑋→𝑋 and returns a function 𝚏𝚘𝚕𝚍𝑓:𝜇𝐹→𝑋. So its type signature is ∀𝑋.(𝐹𝑋→𝑋)→𝜇𝐹→𝑋.

Compare this with the Church encoding type: 𝙲𝚑𝚞𝚛𝚌𝚑𝙵=∀𝑋.(𝐹𝑋→𝑋)→𝑋. They are almost identical. The only difference is that 𝚏𝚘𝚕𝚍 has an extra 𝜇𝐹→ tucked before the final 𝑋. Where Church encoding takes an algebra and produces an 𝑋, fold takes an algebra and a 𝜇𝐹 value to produce an 𝑋.

This suggests a natural way to move between 𝜇𝐹 and 𝙲𝚑𝚞𝚛𝚌𝚑𝙵. Given 𝑥:𝜇𝐹, we can partially apply 𝚏𝚘𝚕𝚍 to get a Church encoding: Λ𝑋.𝜆𝑓.𝚏𝚘𝚕𝚍𝑓𝑥:𝙲𝚑𝚞𝚛𝚌𝚑𝙵. Conversely, given 𝑐:𝙲𝚑𝚞𝚛𝚌𝚑𝙵, we can feed it the initial algebra’s own structure map: 𝑐[𝜇𝐹]𝚒𝚗:𝜇𝐹. These two maps point at each other: one wraps a 𝜇𝐹 value into a universal consumer, the other unwraps a universal consumer by running it on the initial algebra itself. We’ll prove they are inverses, establishing 𝙲𝚑𝚞𝚛𝚌𝚑𝙵≅𝜇𝐹.

Before we do, we still need to understand what 𝜇𝐹 actually is. For that we return to initiality. Take the object 𝐹(𝜇𝐹) and turn it into an F-algebra by using 𝐹(𝚒𝚗):𝐹(𝐹(𝜇𝐹))→𝐹(𝜇𝐹) as the structure map. By initiality, we get a unique homomorphism from ⟨𝐹,𝜇𝐹,𝚒𝚗⟩ to this new algebra; call it 𝑔:𝜇𝐹→𝐹(𝜇𝐹). But 𝚒𝚗 itself is a homomorphism in the opposite direction: 𝐹(𝜇𝐹)→𝜇𝐹. Compose the two: 𝚒𝚗∘𝑔 is a homomorphism from 𝜇𝐹 to itself. Initiality says there is exactly one such homomorphism, namely 𝚒𝚍, so 𝚒𝚗∘𝑔=𝚒𝚍. The same argument on the other side gives 𝑔∘𝚒𝚗=𝚒𝚍. So 𝚒𝚗 and 𝑔 are inverses: 𝚒𝚗 is an isomorphism.

This is Lambek’s theorem: 𝐹(𝜇𝐹)≅𝜇𝐹. The carrier of the initial algebra is the recursive type 𝜇𝑋.𝐹𝑋. That is why we choose the notation 𝜇𝐹.

Now that we know 𝜇𝐹 is a recursive type, the role of the structure map 𝚒𝚗 becomes clear. It is a constructor that builds the recursive type from its components. Take 𝙽𝚊𝚝 as an example, 𝚒𝚗:1+𝙽𝚊𝚝→𝙽𝚊𝚝 has two cases:

The fold equation now also becomes concrete. An algebra ⟨𝐹,𝑋,𝑓⟩ expands to a pair (𝑧:𝑋,𝑠:𝑋→𝑋), and the equation 𝚏𝚘𝚕𝚍𝑓∘𝚒𝚗=𝑓∘𝐹(𝚏𝚘𝚕𝚍𝑓) becomes:

𝚏𝚘𝚕𝚍𝑓(𝚉𝚎𝚛𝚘)=𝑧𝚏𝚘𝚕𝚍𝑓(𝚂𝚞𝚌𝚌𝑛)=𝑠(𝚏𝚘𝚕𝚍𝑓𝑛)

This is structural recursion: replace every 𝚉𝚎𝚛𝚘 with 𝑧, every 𝚂𝚞𝚌𝚌 with an application of 𝑠 to the recursively processed predecessor. The fold walks through the value from the bottom up, substituting each constructor with the algebra’s interpretation.

One more consequence of initiality: 𝚏𝚘𝚕𝚍𝚒𝚗=𝚒𝚍. Since 𝚒𝚍 is a homomorphism from the initial algebra to itself, and initiality says there can only be one, the fold at the initial algebra’s own structure map must be the identity. This equation unlocks the proof.

Stepping back: 𝚒𝚗 builds values from nothing. 𝚏𝚘𝚕𝚍 takes those values and interprets them in any algebra you choose, uniquely. One end generates; the other end consumes. We call this a universal producer.

The Church encoding 𝙲𝚑𝚞𝚛𝚌𝚑𝙵=∀𝑋.(𝐹𝑋→𝑋)→𝑋 is the mirror image: it takes any algebra and returns a value: a universal consumer. Universal producer meets universal consumer.

They are.

Let’s prove 𝙲𝚑𝚞𝚛𝚌𝚑𝙵≅𝜇𝐹. We define two functions between them in System F, and show that they are inverses of each other:

fromChurch : ChurchF → μ F
fromChurch c = c [μ F] in

toChurch : μ F → ChurchF
toChurch x = ΛX. λ(f : F X → X). fold f x

𝚏𝚛𝚘𝚖𝙲𝚑𝚞𝚛𝚌𝚑 feeds the initial algebra to the universal consumer. 𝚝𝚘𝙲𝚑𝚞𝚛𝚌𝚑 takes a concrete value and turns it into a consumer by folding over any algebra you give it.

Step one: 𝚏𝚛𝚘𝚖𝙲𝚑𝚞𝚛𝚌𝚑∘𝚝𝚘𝙲𝚑𝚞𝚛𝚌𝚑=𝚒𝚍. For any 𝑥:𝜇𝐹:

𝚏𝚛𝚘𝚖𝙲𝚑𝚞𝚛𝚌𝚑(𝚝𝚘𝙲𝚑𝚞𝚛𝚌𝚑𝑥)=𝚝𝚘𝙲𝚑𝚞𝚛𝚌𝚑𝑥[𝜇𝐹]𝚒𝚗=𝚏𝚘𝚕𝚍𝚒𝚗𝑥=𝑥

Step two: 𝚝𝚘𝙲𝚑𝚞𝚛𝚌𝚑∘𝚏𝚛𝚘𝚖𝙲𝚑𝚞𝚛𝚌𝚑=𝚒𝚍.

We need to show that for any 𝑐:𝙲𝚑𝚞𝚛𝚌𝚑𝙵, any 𝑋, and any 𝑓:𝐹𝑋→𝑋:

𝚏𝚘𝚕𝚍𝑓(𝑐[𝜇𝐹]𝚒𝚗)=𝑐[𝑋]𝑓

This is where parametricity pays off. Recall the free theorem: for any F-algebra homomorphism ℎ:(𝐴,𝑓𝐴)→(𝐵,𝑓𝐵),

ℎ(𝑐[𝐴]𝑓𝐴)=𝑐[𝐵]𝑓𝐵

Let 𝐴=𝜇𝐹, 𝑓𝐴=𝚒𝚗, 𝐵=𝑋, 𝑓𝐵=𝑓, and ℎ=𝚏𝚘𝚕𝚍𝑓. Since 𝚏𝚘𝚕𝚍𝑓 is an algebra homomorphism, the free theorem applies directly:

𝚏𝚘𝚕𝚍𝑓(𝑐[𝜇𝐹]𝚒𝚗)=𝑐[𝑋]𝑓

That’s the equality we needed. Both directions are inverses, so 𝙲𝚑𝚞𝚛𝚌𝚑𝙵≅𝜇𝐹.

Two independent ideas: the uniqueness of the fold from the initial algebra and the free theorem from parametricity, interlocked to deliver the isomorphism.

We’ve proved the isomorphism. But a question lingers: why this shape? Why ∀𝑋.(𝐹𝑋→𝑋)→𝑋? We arrived at it by following our noses through 𝚄𝚗𝚒𝚝, 𝙱𝚘𝚘𝚕, 𝙽𝚊𝚝, and a bit of algebra. That’s a fine derivation, but it doesn’t explain why the shape must be what it is. For that, we turn to what is arguably the most beautiful theorem in category theory.

Link to this sectionThe Yoneda Lemma

Before stating the lemma, one more piece of vocabulary. The Hom-functor 𝖧𝗈𝗆(𝐴,−) embodies “the view from 𝐴.” For any object 𝑋, 𝖧𝗈𝗆(𝐴,𝑋) is the set of all arrows from 𝐴 to 𝑋, which in 𝐒𝐞𝐭 is just the function type 𝐴→𝑋. Given a function 𝑔:𝑋→𝑌, the Hom-functor maps it by post-composition: 𝖧𝗈𝗆(𝐴,𝑔)=𝜆ℎ.𝑔∘ℎ. Formally: “everything you can do with an 𝐴.”

Now the Yoneda Lemma:

For any (locally small) category 𝒞︀, any object 𝐴 in 𝒞︀, and any 𝐒𝐞𝐭-valued functor 𝐹:𝒞︀→𝚂𝚎𝚝, there exists a bijection:

𝖭𝖺𝗍(𝖧𝗈𝗆(𝐴,−),𝐹)≅𝐹(𝐴)

The left side is the set of natural transformations from 𝖧𝗈𝗆(𝐴,−) to 𝐹; the right side is the value of 𝐹 at 𝐴. This bijection is natural in both 𝐴 and 𝐹.

Beneath the formalism, the idea is simple. A natural transformation from 𝖧𝗈𝗆(𝐴,−) to 𝐹 is completely determined by a single choice: where you send 𝚒𝚍𝐴. Pick any element of 𝐹(𝐴) as the image of 𝚒𝚍𝐴, and naturality does the rest — it uniquely transports that choice to every other object. Conversely, given a natural transformation, applying it to 𝚒𝚍𝐴 picks out an element of 𝐹(𝐴). That’s the whole correspondence.

The Hom-functor captures everything you can arrow out of 𝐴 — the “outward view” of 𝐴. Yoneda says this outward view completely determines 𝐴 itself. Sound familiar? It’s the categorical incarnation of our consumption-interface principle: a type is fully characterized by how you can consume it.

Let’s make this concrete. Set 𝐹 to the identity functor 𝙸𝚍 where 𝙸𝚍(𝑋)=𝑋 and 𝒞︀ to the 𝐒𝐞𝐭 category. The Yoneda lemma specializes to:

𝖭𝖺𝗍(𝖧𝗈𝗆(𝐴,−),𝙸𝚍)≅𝐴

Unpacking the left side: 𝖧𝗈𝗆(𝐴,𝑋) is 𝐴→𝑋. A natural transformation 𝜂:𝖧𝗈𝗆(𝐴,−)→𝙸𝚍 is a family of functions 𝜂𝑋:(𝐴→𝑋)→𝑋, one for each 𝑋. So 𝜂 is necessarily a polymorphic function in System F. Its type is ∀𝑋.(𝐴→𝑋)→𝑋. So ∀𝑋.(𝐴→𝑋)→𝑋≅𝐴. Yoneda gives us the isomorphism directly.

For 𝐴=𝚄𝚗𝚒𝚝, this says ∀𝑋.(𝚄𝚗𝚒𝚝→𝑋)→𝑋≅𝚄𝚗𝚒𝚝, which simplifies to ∀𝑋.𝑋→𝑋≅𝚄𝚗𝚒𝚝. For 𝐴=𝙱𝚘𝚘𝚕, we get ∀𝑋.𝑋→𝑋→𝑋≅𝙱𝚘𝚘𝚕. These are exactly the Church encodings we derived by hand. Now they fall out of Yoneda for free.

But there’s a catch. This only works for non-recursive types: 𝐴 is a fixed type, not a recursive definition. To capture 𝙽𝚊𝚝 and 𝙻𝚒𝚜𝚝, we need to level up.

Instead of the category of types, we work in the category of F-algebras. The objects are F-algebras ⟨𝐹,𝑋,𝑓⟩, and the arrows are homomorphisms. In this category, the initial algebra ⟨𝐹,𝜇𝐹,𝚒𝚗⟩ is a distinguished object. Its Hom-functor 𝖧𝗈𝗆(⟨𝐹,𝜇𝐹,𝚒𝚗⟩,−) maps any algebra ⟨𝐹,𝑋,𝑓⟩ to the set of homomorphisms from the initial algebra to it. By initiality, that set contains exactly one element: 𝚏𝚘𝚕𝚍𝑓.

Now introduce the forgetful functor 𝑈, which takes an algebra ⟨𝐹,𝑋,𝑓⟩ and discards the structure, keeping only the carrier 𝑋. It’s a functor from the F-algebra category to 𝐒𝐞𝐭 category.

Apply Yoneda with 𝒞︀ as the category of F-algebras, 𝐴=⟨𝐹,𝜇𝐹,𝚒𝚗⟩, and 𝐹=𝑈:

𝖭𝖺𝗍(𝖧𝗈𝗆(⟨𝐹,𝜇𝐹,𝚒𝚗⟩,−),𝑈)≅𝑈(⟨𝐹,𝜇𝐹,𝚒𝚗⟩)=𝜇𝐹

Let’s decode the left side. A natural transformation here gives, for each F-algebra ⟨𝐹,𝑋,𝑓⟩, a function from 𝖧𝗈𝗆(⟨𝐹,𝜇𝐹,𝚒𝚗⟩,⟨𝐹,𝑋,𝑓⟩) to 𝑋. But that Hom-set is a singleton, it contains only 𝚏𝚘𝚕𝚍𝑓. So specifying this function is equivalent to picking a value of type 𝑋 for each algebra. Let’s call it 𝑐[𝑋]𝑓. The naturality condition, with the algebra homomorphisms as arrows, becomes exactly:

For any algebra homomorphism ℎ:⟨𝐹,𝑋,𝑓⟩→⟨𝐹,𝑌,𝑔⟩, ℎ(𝑐[𝑋]𝑓)=𝑐[𝑌]𝑔.

So the inhabitants of 𝙲𝚑𝚞𝚛𝚌𝚑𝙵 correspond precisely to the natural transformations on the left side of the Yoneda bijection. By Yoneda, they are in one-to-one correspondence with 𝜇𝐹. Therefore 𝙲𝚑𝚞𝚛𝚌𝚑𝙵≅𝜇𝐹. The universal shape of Church encoding, ∀𝑋.(𝐹𝑋→𝑋)→𝑋, is the Yoneda representation of the initial algebra over the forgetful functor.

So the Church encoding is Yoneda in disguise. ∀𝑋.(𝐹𝑋→𝑋)→𝑋 is the left side of the Yoneda bijection for the initial algebra over the forgetful functor, and Yoneda guarantees it’s isomorphic to 𝜇𝐹.

Parametricity and Yoneda are the same idea in two different languages. The free theorem ℎ(𝑐[𝐴]𝑓𝐴)=𝑐[𝐵]𝑓𝐵 is the Yoneda naturality square, instantiated in the category of F-algebras. The key proof step 𝚏𝚘𝚕𝚍𝚒𝚗=𝚒𝚍 is Yoneda’s central move: determining the entire natural transformation from a single choice at 𝚒𝚍.88.The two proofs run in parallel: 𝚏𝚘𝚕𝚍𝑓(𝑐[𝜇𝐹]𝚒𝚗)=𝑐[𝑋]𝑓 is the naturality square applied to the homomorphism 𝚏𝚘𝚕𝚍𝑓; 𝚏𝚘𝚕𝚍𝚒𝚗=𝚒𝚍 corresponds to Yoneda’s key step where 𝚒𝚍 determines the entire transformation. One path lives inside the type system; the other sits at a higher categorical level. They’re two views of the same structure. Parametricity is the type-theoretic shadow of categorical naturality.

Link to this sectionThe Final Recap

Church encoding begins with a simple intuition: a data type can be replaced by the way you consume it. From 𝚄𝚗𝚒𝚝 and 𝙱𝚘𝚘𝚕, this yields types like ∀𝑋.𝑋→𝑋 and ∀𝑋.𝑋→𝑋→𝑋. To handle recursive types, we introduce functors 𝐹 that describe a data type’s one-layer structure, unifying all Church encodings under a single shape: ∀𝑋.(𝐹𝑋→𝑋)→𝑋.

The inner part 𝐹𝑋→𝑋 is an F-algebra, and the Church encoding is the universal consumer of F-algebras: it takes any algebra and produces a value. Among all F-algebras, the initial algebra 𝜇𝐹 (the recursive data type itself) is the key one: its unique fold operation gives us 𝚏𝚘𝚕𝚍𝚒𝚗=𝚒𝚍. Meanwhile, parametricity forces polymorphic functions to behave as natural transformations, yielding the free theorem ℎ(𝑐[𝐴]𝑓𝐴)=𝑐[𝐵]𝑓𝐵. These two facts — one from initiality, one from parametricity — interlock to prove that Church encoding is isomorphic to the original data type: ∀𝑋.(𝐹𝑋→𝑋)→𝑋≅𝜇𝐹.

The Yoneda Lemma tells us why. Applied in the category of F-algebras, Yoneda states that the natural transformations from the Hom-functor out of the initial algebra to the forgetful functor are isomorphic to 𝜇𝐹 itself, and those natural transformations are precisely the inhabitants of ∀𝑋.(𝐹𝑋→𝑋)→𝑋. The Church encoding is a direct consequence of one of category theory’s most fundamental results. Parametricity and Yoneda are two views of the same structure: both rest on naturality, and one is the type-theoretic shadow of the other.

Returning to the Church numeral 𝜆𝑠.𝜆𝑧.𝑠(𝑠𝑧): it is the Church representation of the initial algebra for the functor 𝙽𝚊𝚝𝙵. Church encoding is an elegant tool in programming, and its shape can be traced along a repeatable path from the consumption intuition all the way to the deep structure of category theory.