• No results found

Informally, as we have seen, a species is a family of labelled shapes. Crucially, the actual labels used shouldn’t matter: for example, we should get the “same” family of binary trees no matter what labels we want to use. This intuition is made precise in the formal definition of combinatorial species as functors. In fact, one of the reasons Joyal’s work was so groundbreaking was that it brought category theory to bear on combinatorics, showing that many specific combinatorial insights could be modeled abstractly using the language of categories.

3.2.1

Species as functors

Definition 3.2.1 (Species [Joyal, 1981]). A species is a functor F :B→Set. Recall that B is the groupoid of finite sets whose morphisms are bijections, and

Set is the category of sets and (total) functions.

It is worth spelling out this definition in more detail, which will also give an op- portunity to explain some intuition and terminology. Even for those who are very

d b e a c 4 3 2 0 1 d 4 b 3 e 2 c 1 a 0 Figure 3.9: Relabelling

comfortable with category theory, it may be hard to grasp the intuition for the ab- stract definition right away.

Definition 3.2.2. A species F is a pair of mappings which

• sends any finite setL (of labels) to a set F L(of shapes), and

• sends any bijection on finite sets σ : L −→∼ L0 (a relabelling) to a function

F σ:F L→F L0 (illustrated in Figure 3.9), satisfying the following functoriality conditions:

• F idL=idF L, and • F (σ◦τ) =F σ◦F τ.

We callF Lthe set of “F-shapes with labels drawn fromL”, or simply “F-shapes on L”, or even (when L is clear from context) just “F-shapes”.1 F σ is called the

“transport ofσ along F”, or sometimes the “relabelling ofF-shapes byσ”.

The functoriality of a speciesF means that the actual labels used don’t matter; the resulting family of shapes is independent of the particular labels used. We might say that species areparametric in label sets of a given size. In particular,F’s action on all label sets of sizenis determined by its action on any particular such set: if|L1|=|L2|

and we know F L1, we can determineF L2 by lifting an arbitrary bijection between

L1 andL2. More formally, although Definitions 3.2.1 and 3.2.2 say only that a species

F sends a bijection σ:L−→∼ L0 to afunction F σ:F L →F L0, the functoriality of

F guarantees thatF σ is a bijection as well. In particular, (F σ)−1 =F (σ−1), since

F σ◦F (σ−1) = F (σ◦σ−1) = F id =id, and similarly F (σ−1)◦F σ =id. Thus,

up to isomorphism, a functor F must do the same thing for any two label sets of the

same size.

We may therefore take the finite set of natural numbers [n] ={0, . . . , n−1}asthe

canonical label set of sizen, and writeF n (instead of F [n]) for the set ofF-shapes

1Margaret Readdy’s translation of Bergeron et al. [1998] uses the word “structure” instead of

“shape”, but that word is likely to remind computer scientists of “data structures”, which is, again,

the wrong association: data structures containdata, whereas species shapes contain only labels. I try

to consistently use the word “shape” to refer to the elements of a species, and reserve “structure” for the labelled data structures to be introduced in Chapter 6.

built from this set. In fact, since B and P are equivalent, we may formally take the definition of a species to be a functorP→Set(or an anafunctor, if we wish to avoid AC; see§2.3), which amounts to the same thing.

Remark. Typically, the sets of shapes F L are required to be finite, that is, species

are defined as functorsB→FinSetinto the category offinite sets. Of course, this is important if the goal is to count things! However, nothing in the present work hinges on this restriction, so it is simpler to drop it.

It should be noted, however, that requiring finiteness in this way would be no great restriction: requiring each particular set of shapes F L to be finite is not at all the same thing as requiring theentire family of shapes, ]n∈NF n, to be finite. Typically,

even in the cases that programmers care about, each individual F n is finite but the entire family is not—that is, a type may have infinitely many inhabitants but only finitely many of a given size.

Remark. In my experience, computer scientists tend to have a bit of trouble with

these definitions, because their first instinct is to think of a functor B → Set from

a computational point of view: i.e. a species F : B → Set, given some set of labels

L∈B, computes some family of shapes having those labels.

However, I find this intuition unhelpful, since it places too much emphasis on analyzing the “input” set of labels, making case distinctions on the size of the set, and so on. Instead of thinking of functors B →Set as computational, it is better to think of them as descriptive. We begin with some entire family of labelled shapes, and want to classify them according to the labels that they use. A functorB →Set

is then a convenient technical device for organizing such a classification: it describes a family of labelled shapesindexed by their labels.

Given this shift in emphasis, one might think it more natural to define a set of labelled shapes along with a function mapping shapes to the set of labels contained in them (indeed, down this path lie the notions of containers [Abbott et al., 2003a, 2004, 2005, Morris and Altenkirch, 2009] andstuff types [Baez and Dolan, 2000, Byrne, 2006]). Species can be seen as roughly dual to these shapes-to-labels mappings, giving

the fiber of each label set. This is parallel to the equivalence between the functor cat-

egory SetN and the slice categorySet/

N (see the discussion under functor categories

in§1.4.1). However, sinceB is not discrete, there is not an equivalence betweenSetB

and Set/B; this seems to account for the fact that species and containers (and, more generally, operads and stuff types/clubs [Kelly, 2005, p. 2]) seem so closely related but without a simply expressible relationship.

Remark. Historically, Joyal’s first paper [1981] defined species as endofunctors B →

B. Given a restriction to finite families of shapes, and the observation that functors preserve isomorphisms, this is essentially equivalent to B → FinSet, which is the definition used in Joyal’s second paper [1986] as well as, later, by Bergeron et al. [1998]. It can be argued, however, that this second formulation is more natural, especially

when one wishes to make the connection to functors FinSet → FinSet (or Set → Set); see Chapter 6.

3.2.2

Cardinality restriction

For any species F and natural numbern, we may define

Fn L:=

(

F L if #L=n

∅ otherwise

.

That is,Fnis the restriction ofF to label sets of size exactly n. For example,E is the

species of sets of any size; E4 is the species of sets of size 4. This is well defined since

the action of a species is determined independently on label sets of each size. More abstractly, as noted previously,B(andP) are disconnected categories, so functors out of them are equivalent to a disjoint union of individual functors out of each connected component; replacing the component functors at individual sizes will always result in another valid overall functor.

More generally, we can “kill” any subset of sizes using arbitrary predicates. For example, F6n is the species of F-shapes of size n or less; similarly, F>n is the species

ofF-shapes of sizen or greater. We also writeF+as a shorthand, and say “nonempty

F”, for F>1, the species F restricted to nonempty sets of labels.

3.2.3

The category of species

Recall thatCDdenotes thefunctor category whose objects are functors and whose morphisms are natural transformations between functors. We may thus consider the

category of species,Spe:= (B⇒Set), where the objects are species, and morphisms

between species are label-preserving mappings which commute with relabelling—that is, mappings which are entirely “structural” and do not depend on the labels in any way. For example, an in-order traversal constitutes such a mapping from the species of binary trees to the species of lists, as illustrated in Figure 3.10: computing an in- order traversal and then relabelling yields the same list as first relabelling and then doing the traversal.

It turns out that functor categories have a lot of interesting structure. For example, as we will see, B ⇒ Set has (at least) six different monoidal structures! Much of Chapter 4 is dedicated to exploring and generalizing this structure.

3.2.4

Species in HoTT

We now turn to porting the category of species from set theory into HoTT. Recall that B denotes the h-groupoid with objects

g f e d c b a 6 5 4 3 2 1 0 6 4 5 0 1 3 2 g e f a b d c

Figure 3.10: Inorder traversal is natural where

isFinite(A) := k(n:N)×(A 'Finn)k

and with morphisms given by paths.

Definition 3.2.3. Aconstructive species, orh-species, is anh-functorF :B → S. We use Spe=B ⇒ S to refer to theh-category of constructive species, the same name as the category B ⇒ Set of set-theoretic species; while technically ambiguous, this should not cause confusion since it should always be clear from the context whether we are working in set theory or in HoTT. Likewise, when working in the context of HoTT we will often simply say “species” instead of “constructive species”.

The above definition corresponds directly to the definition of species in set theory. However, it is more specific than necessary. In fact, in HoTT, any function of type

B → S (that is, a function from objects of B to objects of S) is automatically an h-functor. Since the morphisms in B are just paths, functoriality corresponds to transport. Thus, as hinted in Chapter 1, within HoTT it is simply impossible to write down an invalid species. This is a strong argument for working within type theory in general and HoTT in particular: it provides exactly the right sort of type system which allows expressing only valid species.

Nevertheless, it is still not perfectly clear whether this is the right encoding of species within homotopy type theory. It cannot be directly justified by showing that

B ⇒ Set and B ⇒ S are categorically equivalent; this does not even make sense since they live in entirely different foundational frameworks. Rather, a justification

Figure 3.11: Permutations of size three

must be extensional, in the sense of showing that the two definitions have similar properties and support similar operations. In a sense, much of Chapter 4 is precisely such an extensional justification.