Counting type inhabitants.

Last modified on April 20, 2019 by Alex


For Scala readers: Haskell’s Void type can be assumed to be roughly equivalent to Nothing. Neither has any inhabitants other than diverging or erroneous expressions that fail at runtime. Maybe is the same as Option, except its empty case is called Nothing and its non-empty case is called Just.

For Haskell readers: Any in Scala is roughly equivalent to

the least informative existential type.

I will write forall interchangeably with .

Type inhabitants

An inhabitant of a type T is any expression e : T of type T. It is of importance to be able to ascertain the number of such inhabitants for a variety of types in pure functional languages since:

  • Knowing that a complicated polymorphic type has only a small number of inhabitants (e.g. forall a. a -> a having only one inhabitant) means that we can partially or fully understand its behavior based on the type alone without referring to a particular implementation.
  • Knowing that a T has only one inhabitant e : T allows us to simplify any complicated complicated_expression : T and replace it with e : T.
  • If we can find the number of inhabitants, we can likely also enumerate them. This allows for type-based auto-completion a la
  • Proving that a type P has inhabitants is equivalent to proving the corresponding theorem P.
  • If we can automatically prove that a type has no inhabitants, we can save the developer some time:

There might be some confusion regarding what exactly we mean by “inhabitants”, e.g. are 1 + 1 and 2 different inhabitants of type Int? This is a rather deep topic (that I will cover in a future blog post), but for now we will treat two expressions of type a to be separate inhabitants of a if and only if there is some predicate f : a -> Bool that distinguishes between then. So all of the following definitions define equal inhabitants of their respective types:

while these are different

There are a few interesting examples of seemingly different expressions being the same inhabitants of a type that might seem completely counter-intuitive at first, such as:

Our notion of equivalence between type inhabitants is observational equivalence. We will denote equality in the above sense as a = b. a and b must be of the same type but they might be either values or expressions.


We will define an isomorphism between two types a and b as two functions, to : a -> b and from : b -> a such that to . from = id and from . to = id. The existence of an isomorphism between two types will be denoted as a ~ b.

When we say that a ~ b is witnessed by to and from, we mean that to and from form one particular isomorphism between a and b. Ideally we should also prove that to . from = id and from . to = id, but we will usually omit such proofs. Proving these equalities in polymorphic lambda calculus often relies on parametricity.

Isomorphisms are the tool for counting type inhabitants since isomorphic types have the same number of type inhabitants. For example, Either () () ~ Bool, where we can witness the isomophism by

Using isomorphisms for counting necessitates having types with specific inhabitant counts. For example Either () Bool is isomorphic to a type with 3 inhabitants that we could define as data Three = One | Two | Three. Dependently typed languages like Idris allow us to write Fin 3 in place of Three. Neither option is quite satisfactory since we will be dealing with such types all the time and “Fin 3” is 4 characters too many. We will denote such types simply as 3 (3 : Type). Scala developers should be careful not to confuse that with singleton type 3 <: Int with Singleton, which has only 1 inhabitant in Scala.

Coproducts, “Either-like” types

We have already established that Either () () ~ Bool ~ 2, but can we generalize this to an arbitrary Either a b? e : Either a b is either a value of type a or a value of type b, which seems to imply that the total number of inhabitants should be the sum of the number of inhabitants of both types a and b, e.g.

As an exercise, see if you can find a witness for the last isomorphism:

We arrive at the following formula…

but what do we mean by + here? Surely you can not just add types! Clearly, we need to update our notion of types to allow such frivolities.

Temporary notation

First, we’ll introduce a new notation |_| : Type ⟶ ℕ, which maps types to their inhabitant counts. Note that this notation is part of our meta-language and not of the programming language. We will also need [_] : ℕ ⟶ Type, which maps natural numbers to our newly introduced types 1, 2, etc.

Now we can write our formula more rigorously:

We will revise our notation at the end of this section, but for now it will do.

Products, a.k.a. tuples

Let’s look at tuples now.

As an exercise, convince yourself that all of the above isomorphisms are “true” (can be witnessed by some functions to and from).

An e : (a, b) is a tuple of some x : a and some y : b. If there are |a| ways to choose such an x and |b| ways to choose a y, the total number of possible e is |a| * |b|, or in other words:

Algebraic data types

An arbitrary ADT such as

can be represented in the “coproduct of products” form, Foo ~ Either () (Either Bool Int) (write a witness as an exercise).

This means that

This also works for types taking type parameters, Maybe a ~ Either () a,


A function f : a -> b assigns a single value y : b for every possible value x : a, so it is equivalent to a very large tuple of b’s, with a total of |a| b’s. This means that the number of inhabitants of a -> b is |b| * |b | * ... * |b| (|a| times) or in other words:

As an exercise, see if you can write the corresponding witness (in Idris):

As we will see later, a -> b is a little bit different from coproducts and products in the way it interacts with forall a. when we have (parametric) type polymorphism. The above formula can be used only if a and b are free from any universally quantified variables.

Simpler notation

From this point on, we will write type a to mean any type x such that x ~ a. In other words, we will conflate a and the equivalence class of a with respect to ~. Types like Bool, 2, and Either () () will be treated as if they are the same.

This notational trick allows us to write Either a b ~ a + b or (a, b) ~ a * b without explicitly lifting and unlifting a and b to the realm of natural numbers.

We will write a > 0 to mean that a has at least one inhabitant.

Currying and uncurrying

Another two useful isomorphisms on functions are


A somewhat similar isomorphism holds for coproducts:


Introduction rules

These might seem rather pointless but they will turn out to be invaluable once we get to polymorphic lambda calculus:

and as corollaries

Distributing * over +

Why are structs and enums in C/C++ are inadequate for representing types? Why do functional languages use coproducts of products representation (algebraic data types) of data types rather than something more complicated?

We can answer these questions by pointing out the following isomorphism:

This isomorphism allows us to distribute * (products) over + (coproducts), which means that we can take any complicated type consisting of any combination of products and coproducts and arrive at a flattened representation that is isomorphic to the original type:

If you are familiar with mathematical logic, ADTs are the disjunctive normal form of types.

Combining it all together

Please see Any is a final object for an explanation of this one.

Sets and non-regular types [1]

Consider the type of Set a, an abstract type of sets containing objects of type a. Neither Haskell nor Scala have precisely the type we want, since neither language use the correct notion of equality (observational equality), but both come close enough for our purposes.

In order to calculate |Set a|, we first note that a set is either a set of 0 elements, or a set of 1 element, or a set of 2 elements, and so on.

To derive the last formula, consider how many ways there are to choose the first element x : a of a set (|a|) and how many ways to choose the second element y : a (|a| - 1). Then notice that both { x, y } and { y, x } are the same set but we counted them twice.

As an exercise see if you can prove that in general:

What is |Setₙ a| for n > |a|? It must be zero since there are not enough elements x : a to make such a set, a set must have all of its elements be unique.

Now, imagine that we have some type b ~ 1 + a, which has exactly one more element than a. How are |Set b| and |Set a| related? Let’s say that b = Maybe a, and that we have some x : |Set b|. There are possible cases, either Nothing ∈ x or Nothing ∉ x. There are |Set a| ways to fill the rest of the set with elements Just (z : a), which means that |Set b| = 2 |Set a|.

We conclude that |Set [1 + |a|]| = 2 |Set a|, or in simplified notation Set (1 + n) ~ 2 * Set n, and from |Set 0| = 1, we can arrive at the final expression |Set n| = 2^n.

But wait a second, 2^n ~ n -> 2 ~ n -> Bool. Turns out that we spent all this time trying to derive the number of inhabitants of a set when we could’ve used a shortcut: a set is isomorphic to a predicate that tells us whether a particular value is in a set or not.

Double negation translation

I could write a whole another blog post about this topic, but here is the gist: x > 0 if and only if (x -> 0) -> 0 ~ 1 and x ~ 0 if and only if x -> 0 > 0.

We will first prove the latter:

  • If x ~ 0, then x -> 0 ~ 0 -> 0 ~ 1.
  • Suppose x -> 0 > 0 but x > 0, then there is some f : x -> 0 and some a : x. This means that f a : 0 and 0 > 0, contradiction.

Note that x -> 0 can not have more than one inhabitant. Suppose we could distinguish between two a, b : x -> 0 with a predicate p : (x -> 0) -> Bool, then p would need to somehow extract a bit of information out of x -> 0, but the only way to probe x -> 0 is to supply it with an argument x. Above we have proved that if there is an argument that we could supply to x -> 0, then x -> 0 ~ 0, which contradicts a : x -> 0.

Since x -> 0 can have at most one inhabitant, we can conclude that:

  • If x ~ 0 iff x -> 0 ~ 1.
  • If x > 0 iff x -> 0 ~ 0.

Now we can prove that x > 0 iff (x -> 0) -> 0 ~ 1:

  • If x > 0, then x -> 0 ~ 0, and (x -> 0) -> 0 ~ 1.
  • If (x -> 0) -> 0 ~ 1, then x -> 0 ~ 0, and x > 0.

As an exercise, prove that (x -> 0) -> 0 is a covariant functor (exercise) and a monad. Prove that x -> 0 is a contravariant functor.

Counting inhabitants of rank-1 simple-kinded types [1]

In this section we will finally get into polymorphic lambda calculus. Our universe of types expands to encompass types such as forall a. a -> a and forall b. (forall a. (a, b) -> a) -> a -> b.

Distributivity of universal quantification

Universal quantification distributes over products:

This can be witnessed by (Idris code):

The corresponding distributivity law for coproducts,

is not nearly as obvious, although it may appear trivial at first sight. Consider how you would derive e : (∀ x. f x) + (∀ x. g x):

The only way to construct an Either is to use its Left or Right constructor but which one? To determine that we first need to instantiate ∀ x. f x + g x with some type:

Consider the first case. We have a f (), but we need to construct ∀ x. f x… and evaluating fg with a different type parameter might give us a different result, couldn’t it?

Well, this is where we have to rely on parametricity and purity of fg. If we didn’t have either one, this isomorphism would not hold!

Parametricity implies that this isomorphism holds, but in Haskell (or Scala) there is no way to write its witness without using coercion or a partial case expression (asInstanceOf or partial match).

Distributivity of ∀ over functions

As we will see later, the interaction between and is surprisingly rich, but there are some simple cases where ∀ x. f x -> g x can be simplified quite easily. If f is a constant type constructor, meaning that f x is always the same type a, then:

which can be witnessed by

You might want to check out Wikipedia’s page on universal quantification to learn other similar isomorphisms.

Yoneda lemma

If f is a covariant functor,

In order to show that there is indeed an isomorphism, we first construct two functions going between these types:

As an exercise I would recommend proving that to . from = id and from . to = id. One direction will prove very easy, the other will seem impossible. AFAIU the proof must rely on parametricity, so the equality would hold in relational model of polymorphic lambda calculus. I suggest going all the way back to Reynolds’ papers on polymorphic lambda calculus for information.

Yoneda lemma for contravariant functors

If f is a contravariant functor,


If f is a covariant functor,

If f is a contravariant functor,

If f is a phantom functor (x is free in f x),

Simple examples

Representable functors and containers

Yoneda lemma tells us that ∀ x. (a -> x) -> g x ~ g a, but what if instead of a -> x we have some arbitrary f x, and a natural transformation between functors ∀ x. f x -> g x.

There is a class of functors f x that are isomorphic to a -> x for some a, called representable functors. Some examples include:

()        ~ 0 -> x
x         ~ 1 -> x
(x, x, x) ~ 3 -> x
-- basically, anything that looks like a tuple

If f is a representable functor, f x ~ a -> x, and g is a covariant functor, then

That’s a nice shortcut, but it is pretty limiting! What if f = Maybe, or say, more generally, f x ~ f₁ x + f₂ x, where f₁ x ~ a₁ -> x and f₂ x ~ a₂ -> x (in the case of Maybe, a₁ ~ 0 and a₂ ~ 1):

In the case of ∀ x. Maybe x -> Maybe x, we get:

Much simpler than what we had to do before!

Now, there is another class of functors called containers, where f x ~ Σᵢ aᵢ -> x, and i can range either over some finite range or over .

Almost every “container-like” type (a type with a functor instance) you can imagine is a container:

The only exception that I know of is (x -> 0) -> 0 and similar constructions.

Now we can generalize our previous result to an arbitrary container:

A different view on free theorems

Consider some function f that has the same signature as map on lists:

We can see that a function with map’s signature is isomorphic to a function that takes the list’s length n : Nat and then produces a list of indices of elements from the original list. It may duplicate some indices and skip some, but that’s about it! Compare that with the free theorem for m : ∀ a b. (a -> b) -> [a] -> [b]:

I don’t know about you, but I find the free theorem to be less intuitive than our approach of finding a neat isomorphism.

Similarly if take a look at filter’s signature,

It takes the list’s length and a set of indices and produces a list of indices. We can see exactly how much freedom the signature leaves us! For example, a function that takes every element that comes after one that satisfies the predicate and then shuffles them somehow based on the length of the list is a valid function satisfying the signature ∀ a. (a -> Bool) -> [a] -> [a].

Recursive types

But what to do about ∀ a. (a -> a) -> (a -> a)? Intuitively, we know that the answer is ℵ₀ (a countable number of inhabitants) as \f -> f, \f -> f . f, \f -> f . f . f, etc are all valid expressions with that type. Can we make this proof more rigorous and less syntax-dependent?

Well, time for big guns,

From this we can conclude that the number of inhabitants of ∀ a. (a -> a) -> a -> a is countable, and it is isomorphic to the set of Peano (natural) numbers.

Checking that μ x. f x is inhabited

Is there a mechanical way to check if μ x. f x is inhabited? Note that if μ x. f x is inhabited, μ x. f x > 0, then

and we conclude that μ x. f x > 0 implies f 0 > 0 (proof by mniip).

On the other hand,

Hence, μ x. f x is inhabited if and only if f 0 is inhabited and f is a covariant functor.

Checking that μ x. f x has infinite inhabitants

If f 0 ~ ℵ₀, then following the same logic as in our proof of inhabitance, we get:

But what if 0 < f 0 < ℵ₀? Let’s assume that f is a container, f x ~ (Σᵢ aᵢ -> x) + r, where aᵢ > 0, r > 0, and i ranges over a non-empty set I. Then f x ~ 1 + (a₀ -> x) + ((r - 1) + Σᵢ aᵢ -> x), where Σᵢ now ranges over I ∖ {0}.

In other words, f x ~ 1 + (a₀ -> x) + R. As an exercise see how you would construct an infinite sequence of elements of μ x. 1 + (a₀ -> x) + R, proving that μ x. f x ~ ℵ₀.

What is not free about Functor laws?

Suppose that F :: * -> * is a functor, we write some implementation of fmap :: forall a b. (a -> b) -> F a -> F b for it, what should we look out for?

So fmap ~ F :~> F, meaning that the only freedom in implementing fmap that we have is modifying the context (F) without modifying the contents (a) of F a. This explains why fmap id = id is crucially important, if this law were to be omitted, we could write an fmap that combines a natural transformation of F :~> F with lifting of a -> b to F a -> F b.

Another way to derive theorems for free?

This section is still WIP, but here is something to think about: can every free theorem be expressed as a sufficient condition for some F :~> G to be a natural transformation?

Counting inhabitants for rank-N and higher-kinded types.

This is really tricky and we’ll have to take a pause and talk about…

Bounding the number of inhabitants above?

Consider any polymorphic type ∀ a. f a, can we say anything about the number of its inhabitants by instantiating a with different types?

Can the number of instances increase because of instantiation? Most certainly!

Can the number of instances decrease because of instantiation? Yes:

Well, I will figure it out later.

Counting inhabitants of types given laws.

Is hard!


  • mniip for his comments and the proof μ x. f x > 0 ⟹ f 0 > 0.
  • Melissa for pointing out my ordinal / cardinal confusion.
  • emilypi for her comments.