A profunctor is a categorical construct that takes relations to a new level. It is an embodiment of a proof-relevant relation.

We don’t talk enough about relations. We talk about domesticated relations — functions; or captive ones — equalities; but we don’t talk enough about relations in the wild. And, as is often the case in category theory, a less constrained construct may have more interesting properties and may lead to better insights.

## Relations

A relation between two sets is defined as a set of pairs. The first element of each pair is from the first set, and the second from the second. In other words, it’s a subset of the cartesian product of two sets.

This definition may be extended to categories. If *C* and *D* are small categories, we can define a relation between objects as a set of pairs of objects. In general, such pairs are themselves objects in the product category *C×D*. We could define a relation between categories as a subcategory of *C×D*. This works as long as we ignore morphisms or, equivalently, work with discrete categories.

There is another way of defining relations using a characteristic function. You can define a function on the cartesian product of two sets — a function that assigns zero (or false) to those pairs that are not in a relation, and one (or true) to those which are.

Extending this to categories, we would use a functor rather than a function. We could, for instance, define a relation as a functor from *C×D* to **Set** — a functor that maps pairs of objects to either an empty set or a singleton set. The (somewhat arbitrary) choice of **Set** as the target category will make sense later, when we make connection between relations and hom-sets.

But a functor is not just a mapping of objects — it must map morphisms as well. Here, since we are dealing with a product category, our characteristic functor must map pairs of morphisms to functions between sets. We only worry about the empty set and the singleton set, so there aren’t that many functions to chose from.

The next question is: Should we map the two morphisms in a pair covariantly, or maybe map one of them contravariantly? To see which possibility makes more sense, let’s consider the case when *D* is the same as *C*. In other words, let’s look at relations between objects of the same category. There are actually categories that are based on relations, for instance preorders. In a preorder, two objects are in a relation if there is a morphism between them; and there can be at most one morphism between any two objects. A hom-set in a preorder can only be an empty set or a singleton set. Empty set — no relation. Singleton set — the objects are related.

But that’s exactly how we defined a relation in the first place — a mapping from a pair of objects to **Set** (how’s that for foresight). In a preorder setting, this mapping is nothing but the hom-functor itself. And we know that hom-functors are contravariant in the first argument and covariant in the second:

C(-,=) :: C^{op}×C -> Set

That’s an argument in favor of choosing mixed variance for the characteristic functor defining a relation.

A preorder is also called a thin category — a category where there’s at most one morphism per hom-set. Therefore a hom-functor in any thin category defines a relation.

Let’s dig a little deeper into why contravariance in the first argument makes sense in defining a relation. Suppose two objects `a`

and `b`

are related, i.e., the characteristic functor `R`

maps the pair `<a, b>`

to a singleton set. In the hom-set interpretation, where `R`

is the hom-functor `C(-, =)`

, it means that there is a single morphism `r`

:

r :: a -> b

Now let’s pick a morphism in *C ^{op}×C* that goes from

`<a, b>`

to some `<s, t>`

. A morphism in *C*is a pair of morphisms in

^{op}×C*C*:

f :: s -> a g :: b -> t

The composition of morphisms `g ∘ r ∘ f`

is a morphism from `s`

to `t`

. That means the hom-set `C(s, t)`

is not empty — therefore `s`

and `t`

are related.

And they should be related. That’s because the functor `R`

acting on `<f, g>`

must yield a function from the set `C(a, b)`

to the set `C(s, t)`

. There’s no function from a non-empty set to the empty set. So, if the former is non-empty, the latter cannot be empty. In other words, if `b`

is related to `a`

and there is a morphism from `<a, b>`

to `<s, t>`

then `t`

is related to `s`

. We were able to “transport” the relation along a morphism. By making the characteristic functor `R`

contravariant in the first argument and covariant in the second, we automatically make the relation compatible with the structure of the category.

In general, hom-sets are not constrained to empty and singleton sets. In an arbitrary category *C*, we can still think of hom-sets as defining some kind of generalized relation between objects. The empty hom-set still means no relation. Non-empty hom-sets can be seen as multiple “proofs” or “witnesses” to the relation.

Now that we know that we can imitate relations using hom-sets, let’s take a step back. I can think of two reasons why we would like to separate relations from hom-sets: One is that relations defined by hom-sets are always reflexive because of identity morphisms. The other reason is that we might want to define various relations on top of an existing category, a category that has its own hom-sets. It turns out that a profunctor is just the answer.

## Profunctors

A profunctor assigns sets to pairs of objects — possibly objects taken from two different categories — and it does it in a way that’s compatible with the structure of these categories. In particular, it’s a functor that’s contravariant in its first argument and covariant in the second:

C^{op}× D -> Set

Interpreting elements of such sets as individual “proofs” of a relation, makes a profunctor a kind of *proof-relevant relation*. (This is very much in the spirit of Homotopy Type Theory (HoTT), where one considers proof-relevant equalities.)

In Haskell, we substitute all three categories in the definition of the profunctor with the category of types and functions; which is essentially **Set**, if you ignore the bottom values. So a profunctor is a functor from **Set**^{op}×**Set** to **Set**. It’s a mapping of types — a two-argument type constructor `p`

, and a mapping of morphisms. A morphism in **Set**^{op}×**Set** is a pair of functions going between pairs of sets `(a, b)`

and `(s, t)`

. Because of contravariance, the first function goes in the opposite direction:

(s -> a, b -> t)

A profunctor lifts this pair to a single function:

p a b -> p s t

The lifting is done by the function `dimap`

, which is usually written in the curried form:

class Profunctor p where dimap :: (s -> a) -> (b -> t) -> p a b -> p s t

## Profunctor Composition

As with any construction in category theory, we would like to know if profunctors are composable. But how do you compose something that has two inputs that are objects in different categories and one output that is a set? Just like with a Tetris block, we have to turn it on its side. Profunctors generalize relations between categories, so let’s compose them like relations.

Suppose we have a relation `P`

from *C* to *X* and another relation `Q`

from *X* to *D*. How do we build a composite relation between *C* and *D*? The standard way is to find an object in *X* that can serve as a bridge. We need an object `x`

that is in a relation `P`

with `c`

(we’ll write it as `c P x`

), and with which `d`

in a relation `Q`

(denoted as `x Q d`

). If such an object exists, we say that `d`

is in a relation with `c`

— the relation being the composition of `P`

and `Q`

.

We’ll base the composition of profunctors on the same idea. Except that a profunctor produces a whole set of proofs of a relation. We not only have to provide an `x`

that is related to both `c`

and `d`

, but also compose the proofs of these relations.

By convention, a profunctor from `x`

to `c`

, `p x c`

, is interpreted as a relation from `c`

to `x`

(what we denoted `c P x`

). So the first step in the composition is finding an `x`

such that `p x c`

is a non-empty set *and* for which `q d x`

is also a non-empty set. This not only establishes the two relations, but also generates their proofs — elements of sets. The proof that both relations are in force is simply a pair of proofs (a logical conjunction, in terms of propositions as types). The set of such pairs, or the cartesian product of `p x c`

and `q d x`

, for some `x`

, defines the composition of profunctors.

Have a look at this Haskell definition (in Data.Profunctor.Composition):

data Procompose p q d c where Procompose :: p x c -> q d x -> Procompose p q d c

Here, the cartesian product `(p x c, q d x)`

is curried, and the existential quantifier over `x`

is implicit in the use of the GADT.

This Haskell definition is a special case of a more general definition of the composition of profunctors that relate arbitrary categories. The existential quantifier in this case is represented by a coend:

(p ∘ q) d c = ∫^{x}(p x c) × (q d x)

Since profunctors can be composed, it’s natural to ask if they form a category. It turns out that, rather than being a traditional category (it’s not, because profunctor composition is associative and unital only up to an isomorphism), they form a bicategory called **Prof**. The objects in that category are categories, morphisms are profunctors, and the role of the identity morphism is played by the hom-functor `C(-,=)`

— our prototypical profunctor.

The fact that the hom-functor is the unit of profunctor composition follows from the so-called ninja Yoneda lemma. This can be also explained in terms of relations. The hom-functor establishes a relation between any two objects that are connected by at least one morphism. As I mentioned before, this relation is reflexive. It follows that if we have a “proof” of `p d c`

, we can immediately compose it with the trivial “proof” of `C(d, d)`

, which is `id`

and get the proof of the composition_{d}

(p ∘ C(-, =)) d c

Conversely, if this composition exists, it means that there is a non-empty hom-set `C(d, x)`

and a proof of `p x c`

. We can then take the element of `C(d, x)`

:

f :: d -> x

pair it with an identity at `c`

, and lift the pair:

<f, id_{c}>

using `p`

to transform `p x c`

to `p d c`

— the proof that `d`

is in relation with `c`

. The fact that the relation defined by a profunctor is compatible with the structure of the category (it can be “transported” using a morphism in the product category `C`

) is crucial for this proof.^{op}×D

## Acknowledgments

I’m grateful to Gershom Bazerman for useful comments and to André van Meulebrouck for checking the grammar and spelling.

February 23, 2017 at 11:26 pm

Great article as always, I learn so much from you! I just wanted to state that the ‘=’ in the notation

`C(-,=) :: C`

really confused me for a while. It’s very hard to resist interpreting ‘=’ as equality, and that’s very confusing. For a while I thought it somehow meant the same object as the first ‘-‘. It was a relief when I realized it meant “the object from the second category”, so two ‘-‘s on top of each other.^{op}×C -> SetMay 3, 2017 at 3:52 pm

“mixed covariance” -> “mixed contra/covariance” or just “mixed variance”?

November 1, 2018 at 10:45 pm

[…] Перевод статьи Бартоша Милевски «Profunctors as Relations» (исходный текст расположен по адресу — Текст оригинальной статьи). […]