Container Combinatorics: Monads and Lax Monoidal Functors

. Abbott et al.’s containers are a “syntax” for a wide class of set functors in terms of shapes and positions. Containers whose “de-notation” carries a comonad structure can be characterized as directed containers, or containers where a shape and a position in it determine another shape, intuitively a subshape of this shape rooted by this position. In this paper, we develop similar explicit characterizations for container functors with a monad structure and container functors with a lax monoidal functor structure as well as some variations. We argue that this type of characterizations make a tool, e.g., for enumerating the monad structures or lax monoidal functors that some set functor admits. Such explorations are of interest, e.g., in the semantics of eﬀectful functional programming languages.


Introduction
Abbott et al.'s containers [1], a notational variant of polynomials, are a "syntax" for a wide class of set functors.They specify set functors in terms of shapes and positions.The idea is that an element of F X should be given by a choice of a shape and an element of X for each of the positions in this shape; e.g., an element of List X is given by a natural number (the length of the list) and a matching number of elements of X (the contents of the list).Many constructions of set functors can be carried out on the level of containers, for example the product, coproduct of functors, composition and Day convolution of functors etc.One strength of containers is their usefulness for enumerating functors with specific structure or properties or with particular properties.It should be pointed out from the outset that containers are equivalent to simple polynomials in the sense of Gambino, Hyland and Kock [9,10,13,8], except that in works on polynomials one is often mainly interested in Cartesian polynomial morphisms whereas in works on containers general container morphisms are focussed on.The normal functors of Girard [11] are more constrained: a shape can only have finitely many positions.
Ahman et al. [3,4] sought to find a characterization of those containers whose interpretation carries a comonad structure in terms of some additional structure on the container, using that comonads are comonoids in the monoidal category of set functors.This additional structure, of what they called directed containers, turned out to be very intuitive: every position in a shape determines another shape, intuitively the subshape corresponding to this position; every shape has a distinguished root position; and positions in a subshape can be translated into positions in the shape.Directed containers are in fact the same as small categories, yet directed container morphisms are not functors, but cofunctors in the sense of Aguiar [2].
In this paper, we develop similar characterizations of container functors with a monad structure and those with a lax monoidal structure.We use that both monads and lax monoidal endofunctors are monoids in the category of set endofunctors wrt.its composition resp.Day convolution monoidal structures and that both monoidal structures are available also on the category of containers and preserved by interpretation into set functors.The relevant specializations of containers, which we here call mnd-containers and lmf-containers, are very similar, whereby every mnd-container turns out to also define an lmf-container.
Our motivation for this study is from programming language semantics and functional programming.Strong monads are a generally accepted means for organizing effects in functional programming since Moggi's seminal works.That strong lax monoidal endofunctors have a similar application was noticed first by McBride and Paterson [12] who called them applicative functors.That lax monoidal functors are the same as monoids in the Day convolution monoidal structure on the category of functors (under some assumptions guaranteeing that this monoidal structure is present) was noticed in this context by Capriotti and Kaposi [7].It is sometimes of interest to find all monad or lax monoidal functor structures that a particular functor admits.Containers are a good tool for such explorations.We demonstrate this on a number of standard examples.
The paper is organized as follows.In Section 2, we review containers and directed containers as an explicit characterization of those containers whose interpretations carries a comonad structure.In Section 3, we analyze containers whose interpretation is a monad.In Section 4, we contrast this with an analysis of containers whose interpretation is a lax monoidal functor.In Section 5, we consider some specializations of monads and monoidal functors, to conclude in Section 6.
To describe our constructions on containers, we use type-theoretically inspired syntax, as we need dependent function and pair types throughout.For conciseness of presentation, we work in an informal extensional type theory, but everything we do can be formalized in intensional type theory."Minor" ("implicit") arguments of functions are indicated as subscripts in Π-types, λabstractions and applications to enhance readability (cf. the standard notation for components of natural transformations).We use pattern-matching lambdaabstractions; is a "don't care" pattern.
The paper is a write-up of material that was presented by the author at the SSGEP 2015 summer school in Oxford1 , but was not published until now.

Containers
We begin by a condensed review of containers [1].
A container is given by a set S (of shapes) and a S-indexed family P of sets (of positions in each shape).
A container morphism between two containers (S, P ) and (S , P ) is given by operations t : S → S (the shape map) and q : Π s:S .P (t s) → P s (the position map).Note that while the shape map goes in the forward direction, the position map for a given shape goes in the backward direction.
A container (S, P ) interprets into a set functor S, P c = F where F X = Σs : S. P s → X, F f = λ(s, v).(s, f • v).
A container morphism (t, q) between containers (S, P ) and (S , P ) interprets into a natural transformation t, q c = τ between S, P c and S , P c where τ (s, v) = (t s, v • q s ).
Interpretation − c is a fully-faithful functor from Cont to [Set, Set].
For example, the list functor can be represented by the container (S, P ) where S = N, because the shape of a list is a number-its length, and P s = [0..s), as a position in a list of length s is a number between 0 and s, with the latter excluded.We have S, P c X = Σs : N. [0..s) → X ∼ = List X, reflecting that to give a list amounts to choosing a length together with the corresponding number of elements.The list reversal function is represented by the container endomorphism (t, q) on (S, P ) where t s = s, because reversing a list yields an equally long list, and q s p = s − p, as the element at position p in the reversed list is the element at position s − p in the given list.But the list self-append function is represented by (t, q) where t s = s + s and q s p = p mod s.
Another monoidal category structure on Cont is symmetric.Define Hancock's tensor by (S, P ) c (S , P ) = (S × S , λ(s, s ).P s × P s).Now (Cont, Id c , c ) form a symmetric monoidal category.
Interpretation − c is a symmetric monoidal functor from (Cont, Id c , c ) to the symmetric monoidal category ([Set, Set], Id, ) where is the Day convo-

Directed Containers
Next we review directed containers as a characterization those containers whose interpretation carries a comonad structure; we rely on [3,4].
A directed container is defined as a container (S, P ) with operations -↓ : Πs : S. P s → S (the subshape corresponding to a position in a shape), -o : Π s:S .P s (the root position), and -⊕ : Π s:S .Πp : P s.P (s ↓ p) → P s (translation of a position in a position's subshape) satisfying The data (o, ⊕) resemble a monoid structure on P .However, P is not a set, but a family of sets, and ⊕ operates across the family.Similarly, ↓ resembles a right action of (P, o, ⊕) on S. When none of P s, o s , p ⊕ s p depends on s, these data form a proper monoid structure and a right action.
A directed container morphism between two directed containers (S, P, ↓, o, ⊕) and (S , P , ↓ , o , ⊕ ) is a morphism (t, q) between the underlying containers satisfying Directed containers form a category DCont whose identities and composition are inherited from Cont.
− dc is a fully-faithful functor between DCont and Comonad(Set).Moreover, the functor − dc is the pullback of the fully-faithful functor Here are some standard examples of directed containers and corresponding comonads.

Nonempty list functor (free semigroup functor)
The container (S, P ) carries a directed container structure given by s ↓ p = s − p, o s = 0, p ⊕ s p = p + p .Note that all three operations are well-defined: p ≤ s implies that s − p is well-defined; 0 ≤ s; and p ≤ s and p ≤ s − p imply p + p ≤ s.
The corresponding comonad has ε (x :

the nonempty list of all nonempty suffixes of xs).
There are other directed container structures on (S, P ).One is given by s ↓ p = s, o s = 0, p ⊕ s p = (p + p ) mod s.This directed container interprets into the comonad defined by ε xs = hd xs, δ xs = shifts xs (the nonempty list of all cyclic shifts of xs).

Exponent functor Let
Directed container structures on S, P c are in a bijection with monoid structures on U .Given a monoid structure (i, ⊗), the corresponding directed container structure is given by * The corresponding comonad has

Product functor Let
Evidently there is exactly one directed container structure on (S, P ); it is given by s ↓ * = s, o s = * , * ⊕ s * = * .
The corresponding comonad has ε We defined directed containers as containers with specific additional structure.But they are in a bijection (up to isomorphism) with something much more familiar-small categories.Indeed, a directed container (S, P, ↓, o, ⊕) defines a small category as follows: the set of objects is S, the set of maps between s and s is Σp : P s. (s ↓ p = s ); the identities and composition are given by o and ⊕.Any small category arises from a directed container uniquely in this fashion.The free category on a set V of objects (the discrete category with V as the set of objects), for example, arises from the directed container for the product comonad for V .However, directed container morphisms do not correspond to functors, since the shape map and position map of a container morphism go in opposite directions.A directed container morphism is reminiscent of a split opcleavage, except that, instead of a functor, it relies on an object mapping without an accompanying functorial action and accordingly the lift maps cannot be required to be opCartesian.A directed container morphism is a cofunctor (in the opposite direction) in the sense of Aguiar [2].The category of directed containers is equivalent to the opposite of the category of small categories and cofunctors.

Containers ∩ Monads
There is no reason why the analysis of container functors with comonad structure could not be repeated for other types of functors with structure, the most obvious next candidate target being monads.The additional structure on containers corresponding to monads was sketched already in the original directed containers work [3].Here we discuss the same characterization in detail.
We define an mnd-container to be a container (S, P ) with operations e : S -• : Πs : S. (P s → S) → S q 0 : Πs : S. Πv : P s → S. P (s • v) → P s q 1 : Πs : S. Πv : P s → S. Πp : where we write q 0 s v p as v s p and q 1 s v p as p v s, satisfying We can see that the data (e, •) are like a monoid structure on S modulo the 2nd argument of the multiplication being not an element of S, but a function from P s to S where s is the 1st argument.Similarly, introducing the visual , notation for the data q 0 , q 1 helps us see that they are reminiscent of a biaction (a pair of agreeing right and left actions) of this monoid-like structure on P .But a further difference is also that P is not a set, but a S-indexed family of sets.
We also define an mnd-container morphism between (S, P, e, •, , ) and (S , P , e , • , , ) to be a container morphism (t, q) between (S, P ) and (S , P ) such that Mnd-containers form a category MCont whose identity and composition are inherited from Cont.
− mc is a fully-faithful functor between MCont and Monad(Set).Moreover, the functor − mc is the pullback of the fully-faithful functor − c : Cont → [Set, Set] along U : Monad(Set) → [Set, Set] and the category MCont is isomorphic to the category of monoids in (Cont, Id c , • c ).
We consider as examples some containers interpreting into functors with a monad structure used in programming language semantics or functional programming.
Coproduct functor Let T X = X + E for some set E. We have that T X ∼ = S, P c X for S = 1 + E, P (inl * ) = 1, P (inr ) = 0.
In a hypothetical mnd-container structure on (S, P ), we cannot have e = inr e 0 for some e 0 : E, since then P e = 0, but all elements of 0 → S ∼ = 1 are equal, in particular, λ .inl * = λ .inr e 0 : 0 → S, so the 2nd mnd-container equation e • (λ .s) = s cannot hold for both s = inl * and s = inr e 0 .
Therefore it must be that e = inl * .By the 2nd mnd-container equation then inr e by the 1st mnd-container equation (since P (inr e) = 0).
To have p : In this case, P (s • v) = 1 and p = * , and we can define v s p = * and p v s = * .
This choice of (e, •, , ) satisfies all 8 equations of a mnd-container.We see that the container (S, P ) carries exactly one mnd-container structure.The corresponding monad structure on T is that of the exception monad, with η x = inl x, µ (inl c) = c, µ (inr e) = inr e.
The corresponding monad structure on T has η Exponent functor Let T X = U → X for some set U and S = 1, P * = U .
There is exactly one mnd-container structure on (S, P ) given by Indeed, first note that the 1st to 3rd equations of an mnd-container are trivialized by S = 1.Further, S = 1 and the 4th and 5th equations force the definitions of and and the remaining equations hold.The corresponding monad structure on T is given by η x = λu.x, µ f = λu.f u u.This is the well-known reader monad.
Product functor Let T X = V × X for some set V and S = V , P = 1.
Any mnd-container structure on (S, P ) must be of the form for some i : V and ⊗ : V → V → V .The 1st to 3rd equations of an mndcontainer reduce to the equations of a monoid while the remaining equations are trivialized by P = 1.So mnd-container structures on (S, P ) are in a bijective correspondence with monoid structures on V .The corresponding monad structures on T have η x = (i, x), µ (p, (p , x)) = (p ⊗ p , x).They are the writer monads for the different monoid structures on V .

Underlying functor of the state monad
for some set U .We have T X ∼ = S, P c X for S = U → U and P = U .
The container (S, P ) admits the mnd-container structure defined by The corresponding monad structure on T is that of the state monad for U , given by η x = λu.(u, x) and µ f = λu.let (u , g) ← f u in g u .This mnd-container structure is not unique; as a simplest variation, one can alternatively choose s • v = λp.v p (s n p), p v s = s n p for some fixed n : N, with s n denoting n-fold iteration of s.

Underlying functor of update monads
for some sets U and V .We have T X ∼ = S, P c X for S = U → V and P = U .
If (i, ⊗) is a monoid structure on V and ↓ its right action on U , then the container (S, P ) admits the mnd-container structure defined by The corresponding monad structure on T is that of the update monad [5] for U , (V, i, ⊗) and ↓ given by η x = λu.(i,x) and It should be clear that not every monad structure on T arises from some (i, ⊗) and ↓ in this manner.and , as in the definition of the (standard) list mnd-container.This choice of (S, P, e, •, , ) gives an mnd-container.The list mnd-container corresponds to a special case where there is exactly one operation for every arity, in which situation we can w.l.o.g.take O = N, # o = o.Keeping this generalization of the list monad example in mind, we can think of mnd-containers as a version of non-symmetric operads where the argument places of an operation are identified nominally rather than positionally and operations may also have infinite arities.
Altenkirch and Pinyo [6] have proposed to think of an mnd-container (S, P, e, •, , ) as a "lax" (1, Σ)-type universe à la Tarski, namely, to view S as a set of types ("codes for types"), P as an assignment of a set to each type, e as a type 1, • as a Σ-type former, and as first and second projections from the denotation of a Σ-type.The laxity is that there are no constructors for the denotations of 1 and Σ-types, and of course the equations governing the interaction of the constructors and the eliminators are then not enforced either.Thus 1 need not really denote the singleton set and Σ-types need not denote dependent products.

Containers ∩ Lax Monoidal Functors
We proceed to analyzing containers whose interpretation carries a lax monoidal functor structure wrt. the (1, ×) monoidal category structure on Set.We will see that the corresponding additional structure on containers is very similar to that for monads, but simpler.
Recall that a lax monoidal functor between monoidal categories (C, I, ⊗) and (C , I , ⊗ ) is defined as a functor F between C and C with a map m 0 : I → F I and a natural transformation with components m X,Y : F X ⊗ F Y → F (X ⊗ Y ) cohering with the unitors and associators of the two categories.A lax monoidal transformation between two lax monoidal functors (F, m 0 , m) and (F , m 0 , m ) is a natural transformation τ : We define an lmf-container as a container (S, P ) with operations e : S -• : S → S → S q 0 : Πs : S. Πs : S. P (s • s ) → P s q 1 : Πs : S. Πs : S. P (s • s ) → P s where we write q 0 s s p as s s p and q 1 s s p as p s s, satisfying Differently from the mnd-container case, the data (S, e, •) of a lmf-container form a proper monoid.The data ( , ) resemble a biaction of (S, e, •).
We also define an lmf-container morphism between (S, P, e, •, , ) and (S , P , e , • , , ) to be a container morphism (t, q) between (S, P ) and (S , P ) such that Lmf-containers form a category LCont whose identity and composition are inherited from Cont.
− lc is a fully-faithful functor between LCont and the category LMF(Set) of lax endofunctors on (Set, 1, ×).The functor − lc is the pullback of the fullyfaithful functor The category LCont is isomorphic to the category of monoids in (Cont, Id c , c ).
The similarity between the additional structures on containers for monads and lax monoidal functors may at first appear unexpected, but the reasons become clearer, if one compares the types of the "accumulating" Kleisli extension λ(c, f ).µ (T (λx.T (λy.(x, y)) It is immediate from the definitions that any mnd-container (S, P, e, •, , ) carries an lmf-container structure (e , • , , ) given by -e = e s • s = s • (λ .s ) s s p = (λ .s ) s p p s s = p λ .s s This is in agreement with the theorem that any strong monad defines a strong lax monoidal functor.Since any set functor is uniquely strong and all natural transformations between set functors are strong, the strength assumption and conclusion trivialize in our setting.
Another immediate observation is that, for any lmf-container structure (e, •, , ) on (S, P ), there is also a reverse lmf-container structure (e , • , , ) given by The corresponding statement about lax monoidal functors holds for any symmetric monoidal category.
Let us now revisit our example containers and see which lmf-container structures they admit.
Any lmf-container structure on (S, P ) must have e = inl * .Indeed, if it were the case e = inr e 0 for some e 0 : E, then we would have inr e 0 • inl * = inl * by the 1st lmf-container equation.But then q 0 (inr e 0 ) (inl * ) : 1 → 0, which cannot be.
Similarly, for all e 0 : E, s : S, it must be that inr e 0 • s = inl * and s • inr e 0 = inl * .Hence, by the 1st and 2nd lmf-container equations, it must be the case that inl * • s = s, inr e • inl * = inr e, inr e • inr e = inr (e ⊗ e ).The 3rd lmf-container equation forces that ⊗ is a semigroup structure on E. The other lmf-container equations hold trivially.Therefore, lmf-container structures on (S, P ) are in a bijection with semigroup structures on E.
The unique mnd-container structure on (S, P ) corresponds to the particular case of the left zero semigroup, i.e., the semigroup where e ⊗ e = e.
List functor Let T X = List X and S = N, P s = [0..s).
The standard mnd-container structure on (S, P ) gives this lmf-container structure: The corresponding lax monoidal functor structure on T is given by m The other mnd-container structure we considered gives e = 1, s • 1 = s, 1 • s = s, s • s = 0 otherwise, 1 s p = p, s 1 p = 0, p 1 s = 0, p s 1 = p.
The corresponding lax monoidal functor structure on T is m But there are further lmf-container structures on (S, P ) that do not arise from an mnd-container structure, for example this: The corresponding lax monoidal functor structure is m 0 * = [ * ], m (xs, ys) = zip (xs, ys).
Exponent functor Let T X = U → X for some set U and S = 1, P * = U .
There is exactly one lmf-container structure on (S, P ) given by and that is the lmf-container given by the unique mnd-container structure.
Product functor Let T X = V × X for some set V and S = V , P = 1.
Any lmf-container structure on (S, P ) must be of the form for (i, ⊗) a monoid structure on V , so the only lmf-container structures are those given by mnd-structures.The corresponding lax monoidal functor structures on T are given by m 0 * = (i, * ), m ((p, x), (p , x )) = (p ⊗ p , (x, x )).
Similarly to the monad case, we can generalize the list functor example.Now we are interested in relaxation of non-symmetric operads where parallel composition is only defined when the given n operations composed with the given n-ary operation are all the same, i.Under the lax type universe view, an lmf-container is a lax (1, ×)-universe, i.e., it is only closed under non-dependent lax Σ-types.

Further specializations
There are numerous special types of monads and lax monoidal functors that can be analyzed similarly.Here are some examples.
The lax monoidal functor interpreting an lmf-container is symmetric (i.e., satisfies F σ X,Y • m X,Y = m Y,X • σ F X,F Y ) if and only if the lmf-container is identical to its reverse, i.e., it satisfies s • s = s • s, s s p = p s s In this case, the monoid (S, e, •) is commutative and each of the two action-like operations , determines the other.
The monad interpreting an mnd-container is commutative (which reduces to the corresponding lax monoidal functor being symmetric) if and only if s • (λ .s ) = s • (λ .s) -(λ .s ) s p = p λ .s s Note that, in this case, and are constrained, but not to the degree of fully determining each other.
The monad interpreting an mnd-container is Cartesian (which means that all naturality squares of η and µ are pullbacks) if and only if the function λ .* : P e → 1 is an isomorphism for any s : S and v : P s → S, the function λp.(v s p, p v s) : P (s • v) → Σp : P s.P (v p) is an isomorphism.
Such mnd-containers with additional conditions are proper (1, Σ)-type universes: 1 and Σ-types denote the singleton set and dependent products.

Conclusion
We showed that the containers whose interpretation into a set functor carries a monad or a lax monoidal functor structure admit explicit characterizations similar to the directed container (or small category) characterization of those containers whose interpretation is a comonad.It was not surprising that such characterizations are possible, as we could build on the very same observations that were used in the analysis of the comonad case.But the elaboration of the characterizations is, we believe, novel.We also believe that it provides useful insights into the nature of monad or lax monoidal functor structures on container functors.In particular, it provides some clues on why monads and lax monoidal functors on Set and, more generally, in the situation of canonical strengths enjoy analogous properties.In future work, we would like to reach a better understanding of the connections of containers to operads.
The list functor example can be generalized in the following way.Let (O, #, id, •) be some non-symmetric operad, i.e., let O be a set of operations, # : O → N a function fixing the arity of each operation and id : O and • : Πo : O. (# o → O) → O an identity operation and a parallel composition operator, with # id = 1 and # (o•v) = i:[0,# o) # (v i), satisfying the equations of a non-symmetric operad.We can take S = O, P o = [0..# o), e = id, • = • e., we have O a set of operations, # : O → N a function fixing the arity of each operation and id : O and • : O → O → O an identity operation and a parallel composition operator, with # id = 1 and # (o • o ) = # o * # o , satisfying the equations of an ordinary non-symmetric operad.If we now choose S = O, P o = [0..# o), e = id, • = • and take , as in the definition of the standard list lmf-container, we get a non-symmetric operad in this relaxed sense.