1 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 \(\mathsf {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 [8,9,10, 13], 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 Sect. 2, we review containers and directed containers as an explicit characterization of those containers whose interpretations carries a comonad structure. In Sect. 3, we analyze containers whose interpretation is a monad. In Sect. 4, we contrast this with an analysis of containers whose interpretation is a lax monoidal functor. In Sect. 5, we consider some specializations of monads and monoidal functors, to conclude in Sect. 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 \(\varPi \)-types, \(\lambda \)-abstractions and applications to enhance readability (cf. the standard notation for components of natural transformations). We use pattern-matching lambda-abstractions; \(\_\) 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 OxfordFootnote 1, but was not published until now.

2 Containers, Directed Containers

2.1 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 (SP) and \((S',P')\) is given by operations \(t : S \rightarrow S'\) (the shape map) and \(q : \varPi _{s : S}.\, P'\, (t\, s) \rightarrow 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.

The identity container morphism on (SP) is \((\mathsf {id}_S, \lambda _s.\, \mathsf {id}_{P\, s})\). The composition of container morphisms \((t, q) : (S, P) \rightarrow (S', P')\) and \((t', q') : (S', P') \rightarrow (S'', P'')\) is \((t' \circ t, \lambda _s.\, q_s \circ q'_{t\, s})\). Containers and container morphisms form a category \(\mathbf {Cont}\).

A container (SP) interprets into a set functor \([\![S, P]\!]^\mathrm {c} = F\) where \(F\, X = \varSigma s: S.\, P\, s \rightarrow X\), \(F\, f = \lambda (s, v).\, (s, f \circ v)\).

A container morphism (tq) between containers (SP) and \((S', P')\) interprets into a natural transformation \([\![t, q]\!]^\mathrm {c} = \tau \) between \([\![S, P]\!]^\mathrm {c}\) and \([\![S', P']\!]^\mathrm {c}\) where \(\tau \, (s, v) = (t\, s, v \circ q_s)\).

Interpretation \([\![-]\!]^\mathrm {c}\) is a fully-faithful functor from \(\mathbf {Cont}\) to \([\mathbf {Set}, \mathbf {Set}]\).

For example, the list functor can be represented by the container (SP) where \(S = \mathbb {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]\!]^\mathrm {c}\, X = \varSigma s : \mathbb {N}.\, [0..s) \rightarrow X \cong \mathsf {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 (tq) on (SP) 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 (tq) where \(t\, s = s + s\) and \(q_s\, p = p \mod s\).

There is an identity container defined by \(\mathsf {Id}^\mathrm {c}= (1, \lambda {*}.\, 1)\). Containers can be composed, composition is defined by \((S, P) \mathrel {\cdot ^\mathrm {c}}(S', P') = (\varSigma s : S.\, P\, s \rightarrow S', \lambda (s, v).\, \varSigma p : P\, s.\, P'\, (v\, p))\). Identity and composition of containers provide a monoidal category structure on \(\mathbf {Cont}\).

Interpretation \([\![-]\!]^\mathrm {c}\) is a monoidal functor from \((\mathbf {Cont}, \mathsf {Id}^\mathrm {c}, \mathrel {\cdot ^\mathrm {c}})\) to the strict monoidal category \(([\mathbf {Set},\mathbf {Set}], \mathsf {Id}, \cdot )\). Indeed, \(\mathsf {Id}\, X = X \cong \varSigma {*}: 1.\, 1 \rightarrow X = [\![\mathsf {Id}^\mathrm {c}]\!]^\mathrm {c}\, X\) and \(([\![S, P]\!]^\mathrm {c} \cdot [\![S', P']\!]^\mathrm {c})\, X = [\![S, P]\!]^\mathrm {c}\, ([\![S', P']\!]^\mathrm {c}\, X) \cong \varSigma s : S.\, P\, s \rightarrow \varSigma s' : S'.\, P'\, s' \rightarrow X \cong \varSigma (s, v) : (\varSigma s : S.\, P\, s \rightarrow S').\, (\varSigma p : P\, s.\, P'\, (v\, p)) \rightarrow X = [\![(S, P) \mathrel {\cdot ^\mathrm {c}}(S', P')]\!]^\mathrm {c}\, X\).

Another monoidal category structure on \(\mathbf {Cont}\) is symmetric. Define Hancock’s tensor by . Now form a symmetric monoidal category.

Interpretation \([\![-]\!]^\mathrm {c}\) is a symmetric monoidal functor from to the symmetric monoidal category where is the Day convolution defined by . Indeed,

2.2 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 (SP) with operations

  • \({\downarrow } : \varPi s : S.\, P\, s \rightarrow S\) (the subshape corresponding to a position in a shape),

  • \(\mathsf {o}: \varPi _{s : S}.\, P\, s\) (the root position), and

  • \({\oplus } : \varPi _{s : S}.\, \varPi p : P\, s.\, P\, (s \downarrow p) \rightarrow P\, s\) (translation of a position in a position’s subshape)

satisfying

  • \(s \downarrow \mathsf {o}_s = s\)

  • \(s \downarrow (p \oplus _s p') = (s \downarrow p) \downarrow p'\)

  • \(p \oplus _s \mathsf {o}_{s \downarrow p} = p\)

  • \(\mathsf {o}_s \oplus _s p = p\)

  • \((p \oplus _s p') \oplus _s p'' = p \oplus _s (p' \oplus _{s \downarrow p} p'')\)

The data \((\mathsf {o}, \oplus )\) resemble a monoid structure on P. However, P is not a set, but a family of sets, and \(\oplus \) operates across the family. Similarly, \(\downarrow \) resembles a right action of \((P, \mathsf {o}, {\oplus })\) on S. When none of \(P\, s\), \(\mathsf {o}_s\), \(p \oplus _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, {\downarrow }, \mathsf {o}, {\oplus })\) and \((S', P', {\downarrow '}, \mathsf {o}', {\oplus '})\) is a morphism (tq) between the underlying containers satisfying

  • \(t\, (s \downarrow q_{s}\, p) = t\, s \downarrow ' p\)

  • \({\mathsf {o}}_{s} = q_{s}\, \mathsf {o}'_{t\, s}\)

  • \(q_{s}\, p \mathbin {\oplus }_{s} q_{s \downarrow q_{s}\, p}\, p' = q_{s}\, (p \mathbin {\oplus '_{t\, s}} p')\)

Directed containers form a category \(\mathbf {DCont}\) whose identities and composition are inherited from \(\mathbf {Cont}\).

A directed container \((S,P,{\downarrow },\mathsf {o},{\oplus })\) interprets into a comonad \([\![S,P,{\downarrow },\mathsf {o},{\oplus }]\!]^\mathrm {dc} = (D, \varepsilon , \delta )\) where

  • \(D = [\![S, P]\!]^\mathrm {c}\)

  • \(\varepsilon \, (s, v) = v\, \mathsf {o}_s\)

  • \(\delta \, (s, v) = (s, \lambda p.\, (s \downarrow p, \lambda p'.\, v\, (p \oplus _s p')))\)

A directed container morphism (tq) between \((S,P,{\downarrow },\mathsf {o},{\oplus })\) and \((S',P',{\downarrow '},\mathsf {o}',{\oplus '})\) interprets into a comonad morphism \([\![t, q]\!]^\mathrm {dc} = [\![t, q]\!]^\mathrm {c}\) between \([\![S,P,{\downarrow },\mathsf {o},{\oplus }]\!]^\mathrm {dc}\) and \([\![S',P',{\downarrow '},\mathsf {o}',{\oplus '}]\!]^\mathrm {dc}\).

\([\![-]\!]^\mathrm {dc}\) is a fully-faithful functor between \(\mathbf {DCont}\) and \(\mathbf {Comonad}(\mathbf {Set})\). Moreover, the functor \([\![-]\!]^\mathrm {dc}\) is the pullback of the fully-faithful functor \([\![-]\!]^\mathrm {c}: \mathbf {Cont}\rightarrow [\mathbf {Set},\mathbf {Set}]\) along \(U: \mathbf {Comonad}(\mathbf {Set}) \rightarrow [\mathbf {Set},\mathbf {Set}]\) and the category \(\mathbf {DCont}\) is isomorphic to the category of comonoids in \((\mathbf {Cont}, \mathsf {Id}^\mathrm {c}, \mathrel {\cdot ^\mathrm {c}})\).

figure a

Here are some standard examples of directed containers and corresponding comonads.

Nonempty list functor (free semigroup functor). Let \(D\, X = \mathsf {NEList}\, X = \mu Z.\, X \times (1 + Z) \cong \varSigma s : \mathbb {N}.\, [0..s] \rightarrow X\). We have \(D\, X \cong [\![S, P]\!]^\mathrm {c}\, X\) for \(S = \mathbb {N}\), \(P\, s = [0..s]\).

The container (SP) carries a directed container structure given by \(s \downarrow p = s - p\), \(\mathsf {o}_s = 0\), \(p \oplus _s p' = p + p'\). Note that all three operations are well-defined: \(p \le s\) implies that \(s - p\) is well-defined; \(0 \le s\); and \(p \le s\) and \(p' \le s - p\) imply \(p + p' \le s\).

The corresponding comonad has \(\varepsilon \, (x : xs) = x\) (the head of xs), \(\delta \, [x] = [[x]]\), \(\delta \, (x : xs) = (x : xs) : \delta \, xs\) (the nonempty list of all nonempty suffixes of xs).

There are other directed container structures on (SP). One is given by \(s \downarrow p = s\), \(\mathsf {o}_s = 0\), \(p \oplus _s p' = (p + p') \mod s\). This directed container interprets into the comonad defined by \(\varepsilon \, xs = \mathsf {hd}\, xs\), \(\delta \, xs = \mathsf {shifts}\, xs\) (the nonempty list of all cyclic shifts of xs).

Exponent functor. Let \(D\, X = U \rightarrow X \cong 1 \times (U \rightarrow X)\) for some set U. We have \(D\, X \cong [\![S, P]\!]^\mathrm {c}\, X\) for \(S = 1\), \(P\, {*}= U\).

Directed container structures on \([\![S, P]\!]^\mathrm {c}\) are in a bijection with monoid structures on U. Given a monoid structure \((\mathsf {i}, \otimes )\), the corresponding directed container structure is given by \({*}\downarrow p = {*}\), \(\mathsf {o}_{*}= \mathsf {i}\), \(p \oplus _{*}p' = p \otimes p'\).

The corresponding comonad has \(\varepsilon \, f = f\, \mathsf {i}\), \(\delta \, f = \lambda p.\, \lambda p'.\ f\, (p \otimes p')\).

Via the isomorphism \(\mathsf {Str}\, X = \nu Z.\, X \times Z \cong \mathbb {N}\rightarrow X\), the special case of \((U, \mathsf {i}, \otimes ) = (\mathbb {N}, 0, +)\) corresponds to the familiar stream comonad defined by \(D\, X = \mathsf {Str}\, X\), \(\varepsilon \, xs = \mathsf {hd}\, xs\) (the head of xs), \(\delta \, xs = xs : \delta \, (\mathsf {tl}\, xs)\) (the stream of all suffixes of xs). A different special case \((U, \mathsf {i}, \otimes ) = (\mathbb {N}, 1, *)\) corresponds to a different stream comonad given by \(\varepsilon \, xs = \mathsf {hd}\, (\mathsf {tl}\, xs)\), \(\delta \, xs = \mathsf {samplings}\, xs\) (the stream of all samplings of xs, where by the sampling of a stream \([x_0, x_1, x_2, \ldots ]\) at rate p we mean the stream \([x_0, x_p, x_{p*2}, \ldots ]\)).

Product functor. Let \(D\, X = V \times X = V \times (1 \rightarrow X)\) for some set V. We have that \(T\, X \cong [\![S, P]\!]^\mathrm {c}\, X\) for \(S = V\), \(P\, \_ = 1\).

Evidently there is exactly one directed container structure on (SP); it is given by \(s \downarrow {*}= s\), \(\mathsf {o}_{s} = {*}\), \({*}\oplus _{s} {*}= {*}\).

The corresponding comonad has \(\varepsilon \, (v, x) = x\), \(\delta \, (v, x) = (v, (v, x))\).

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, {\downarrow }, \mathsf {o}, {\oplus })\) defines a small category as follows: the set of objects is S, the set of maps between s and \(s'\) is \(\varSigma p : P\, s.\, (s \downarrow p = s')\); the identities and composition are given by \(\mathsf {o}\) and \(\oplus \). 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.

3 Containers \(\cap \) 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 (SP) with operations

  • \(\mathsf {e}: S\)

  • \(\bullet : \varPi s: S.\, (P~s \rightarrow S) \rightarrow S\)

  • \(q_0 : \varPi s : S.\, \varPi v : P\, s \rightarrow S.\, P\, (s \bullet v) \rightarrow P\, s\)

where we write \(q_0\, s\, v\, p\) as and \(q_1\, s\, v\, p\) as , satisfying

  • \(s = s \bullet (\lambda \_.\, \mathsf {e})\)

  • \(\mathsf {e}\bullet (\lambda \_.\, s) = s\)

  •                            

  •                            

We can see that the data \((\mathsf {e}, \bullet )\) 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 and to be a container morphism (tq) between (SP) and \((S',P')\) such that

  • \(t\, \mathsf {e}= \mathsf {e}'\)

  • \(t\, (s \bullet v) = t\, s \bullet ' (t \circ v \circ q_s)\)

Mnd-containers form a category \(\mathbf {MCont}\) whose identity and composition are inherited from \(\mathbf {Cont}\).

Every mnd-container interprets into a monad where

  • \(T = [\![S, P]\!]^\mathrm {c}\)

  • \(\eta \, x = (\mathsf {e}, \lambda p.\, x)\)

Every mnd-container morphism (tq) between and interprets into a monad morphism \([\![t, q]\!]^\mathrm {mc} = [\![t, q]\!]^\mathrm {c}\) between and .

\([\![-]\!]^\mathrm {mc}\) is a fully-faithful functor between \(\mathbf {MCont}\) and \(\mathbf {Monad}(\mathbf {Set})\). Moreover, the functor \([\![-]\!]^\mathrm {mc}\) is the pullback of the fully-faithful functor \([\![-]\!]^\mathrm {c}: \mathbf {Cont}\rightarrow [\mathbf {Set},\mathbf {Set}]\) along \(U: \mathbf {Monad}(\mathbf {Set}) \rightarrow [\mathbf {Set},\mathbf {Set}]\) and the category \(\mathbf {MCont}\) is isomorphic to the category of monoids in \((\mathbf {Cont}, \mathsf {Id}^\mathrm {c}, \mathrel {\cdot ^\mathrm {c}})\).

figure b

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 \cong [\![S, P]\!]^\mathrm {c}\, X\) for \(S = 1 + E\), \(P\, (\mathsf {inl}\, {*}) = 1\), \(P\, (\mathsf {inr}\, \_) = 0\).

In a hypothetical mnd-container structure on (SP), we cannot have \(\mathsf {e}= \mathsf {inr}\, e_0\) for some \(e_0 : E\), since then \(P\, \mathsf {e}= 0\), but all elements of \(0 \rightarrow S \cong 1\) are equal, in particular, \(\lambda \_.\ \mathsf {inl}\, {*}= \lambda \_.\, \mathsf {inr}\, e_0 : 0 \rightarrow S\), so the 2nd mnd-container equation \(\mathsf {e}\bullet (\lambda \_.\, s) = s\) cannot hold for both \(s = \mathsf {inl}\, {*}\) and \(s = \mathsf {inr}\, e_0\).

Therefore it must be that \(\mathsf {e}= \mathsf {inl}\, {*}\). By the 2nd mnd-container equation then \(\mathsf {inl}\, {*}\bullet v = \mathsf {e}\bullet (\lambda {*}.\, v\, {*}) = v\, {*}\) (since \(P\, (\mathsf {inl}\, {*}) = 1\)) whereas \(\mathsf {inr}\, e \bullet v = \mathsf {inr}\, e \bullet (\lambda \_.\, \mathsf {e}) = \mathsf {inr}\, e\) by the 1st mnd-container equation (since \(P\, (\mathsf {inr}\, e) = 0\)).

To have \(p : P\, (s \bullet v)\) is only possible, if \(s = \mathsf {inl}\, {*}\), \(v = \lambda {*}.\, \mathsf {inl}\, {*}\). In this case, \(P\, (s \bullet v) = 1\) and \(p = {*}\), and we can define and .

This choice of satisfies all 8 equations of a mnd-container.

We see that the container (SP) carries exactly one mnd-container structure. The corresponding monad structure on T is that of the exception monad, with \(\eta \, x = \mathsf {inl}\, x\), \(\mu \, (\mathsf {inl}\, c) = c\), \(\mu \, (\mathsf {inr}\, e) = \mathsf {inr}\, e\).

List functor (free monoid functor). Let T be the list functor: \(T\, X = \mathsf {List}\, X = \mu Z.\, 1 + X \times Z \cong \varSigma s : \mathbb {N}.\, [0..s) \rightarrow X\). We have that \(T\, X \cong [\![S, P]\!]^\mathrm {c}\, X\) for \(S = \mathbb {N}\), \(P\, s = [0..s)\).

The container (SP) carries the following mnd-container structure:

  • \(\mathsf {e}= 1\)

  • \(s \bullet v = \sum _{p : [0..s)} v\, p\)

The corresponding monad structure on T is the standard list monad with \(\eta \, x = [x]\), \(\mu \, xss = \mathsf {concat}\, xss\).

This is not the only mnd-container structure available on (SP). Another is \(\mathsf {e}= 1\), \(s \bullet \lambda \_.\, 1 = s\), \(1 \bullet \lambda 0.\, s = s\), \(s \bullet v = 0\) otherwise, , , , .

The corresponding monad structure on T has \(\eta \, x = [x]\), \(\mu \, [[x_0], \ldots , [x_{v\, 0 -1}]] = [x_0, \ldots , x_{v\, 0-1}]\), \(\mu \, [xs] = xs\), \(\mu \, xss = []\) otherwise.

Exponent functor. Let \(T\, X = U \rightarrow X\) for some set U and \(S = 1\), \(P\, {*}= U\).

There is exactly one mnd-container structure on (SP) given by

  • \(\mathsf {e}= {*}\)

  • \({*}\bullet (\lambda \_.\, {*}) = {*}\)

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 \(\eta \, x = \lambda u.\, x\), \(\mu \, f = \lambda u.\, f\, u\, u\). This is the well-known reader monad.

Product functor. Let \(T\, X = V \times X\) for some set V and \(S = V\), \(P\, \_ = 1\).

Any mnd-container structure on (SP) must be of the form

  • \(\mathsf {e}= \mathsf {i}\)

  • \(s \bullet (\lambda {*}.\, s') = s \otimes s'\)

for some \(\mathsf {i}: V\) and \(\otimes : V \rightarrow V \rightarrow V\). The 1st to 3rd equations of an mnd-container reduce to the equations of a monoid while the remaining equations are trivialized by \(P\, \_ = 1\). So mnd-container structures on (SP) are in a bijective correspondence with monoid structures on V.

The corresponding monad structures on T have \(\eta \, x = (\mathsf {i}, x)\), \(\mu \, (p, (p', x)) = (p \otimes p', x)\). They are the writer monads for the different monoid structures on V.

Underlying functor of the state monad. Let \(T\, X = U \rightarrow U \times X \cong (U \rightarrow U) \times (U \rightarrow X)\) for some set U. We have \(T\, X \cong [\![S, P]\!]^\mathrm {c}\, X\) for \(S = U \rightarrow U\) and \(P\, \_ = U\).

The container (SP) admits the mnd-container structure defined by

  • \(\mathsf {e}= \lambda p.\, p\)

  • \(s \bullet v = \lambda p.\, v\, p\, (s\, p)\)

The corresponding monad structure on T is that of the state monad for U, given by \(\eta \, x = \lambda u.\, (u, x)\) and \(\mu \, f = \lambda u.\, \mathsf {let~}(u', g) \leftarrow f\, u'\mathsf {~in~}g\, u'\).

This mnd-container structure is not unique; as a simplest variation, one can alternatively choose \(s \bullet v = \lambda p.\, v\, p\, (s^n\, p)\), for some fixed \(n : \mathbb {N}\), with \(s^n\) denoting n-fold iteration of s.

Underlying functor of update monads. Let \(T\, X = U \rightarrow V \times X \cong (U \rightarrow V) \times (U \rightarrow X)\) for some sets U and V. We have \(T\, X \cong [\![S, P]\!]^\mathrm {c}\, X\) for \(S = U \rightarrow V\) and \(P\, \_ = U\).

If \((\mathsf {i}, \otimes )\) is a monoid structure on V and \(\downarrow \) its right action on U, then the container (SP) admits the mnd-container structure defined by

  • \(\mathsf {e}= \lambda \_.\, \mathsf {i}\)

  • \(s \bullet v = \lambda p.\, s\, p \otimes v\, p\, (s\, p)\)

The corresponding monad structure on T is that of the update monad [5] for U, \((V, \mathsf {i}, \otimes )\) and \(\downarrow \) given by \(\eta \, x = \lambda u. (\mathsf {i}, x)\) and \(\mu \, f = \lambda u.\, \mathsf {let~}(p, g) \leftarrow f\, u; (p', x) \leftarrow g\, (u \downarrow p)\mathsf {~in~}(p \otimes p', x)\).

It should be clear that not every monad structure on T arises from some \((\mathsf {i}, \otimes )\) and \(\downarrow \) in this manner.

The list functor example can be generalized in the following way. Let \((O, \#, \mathsf {id}, \circ )\) be some non-symmetric operad, i.e., let O be a set of operations, \(\# : O \rightarrow \mathbb {N}\) a function fixing the arity of each operation and \(\mathsf {id}: O\) and \(\circ : \varPi o:O.\, (\#\, o \rightarrow O) \rightarrow O\) an identity operation and a parallel composition operator, with \(\#\, \mathsf {id}= 1\) and \(\#\, (o \circ v) = \sum _{i:[0, \#\, o)} \#\, (v\, i)\), satisfying the equations of a non-symmetric operad. We can take \(S = O\), \(P\, o = [0..\#\, o)\), \(\mathsf {e}= \mathsf {id}\), \(\bullet = \circ \) and , as in the definition of the (standard) list mnd-container. This choice of 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 = \mathbb {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 operations may also have infinite arities and, importantly, arguments may be discarded and duplicated in composition.

Altenkirch and Pinyo [6] have proposed to think of an mnd-container as a “lax” \((1, \varSigma )\)-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, \(\mathsf {e}\) as a type 1, \(\bullet \) as a \(\varSigma \)-type former, and as first and second projections from the denotation of a \(\varSigma \)-type. The laxity is that there are no constructors for the denotations of 1 and \(\varSigma \)-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 \(\varSigma \)-types need not denote dependent products.

4 Containers \(\cap \) Lax Monoidal Functors

We proceed to analyzing containers whose interpretation carries a lax monoidal functor structure wrt. the \((1, \times )\) monoidal category structure on \(\mathbf {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 \((\mathcal {C}, I, \otimes )\) and \((\mathcal {C}', I', \otimes ')\) is defined as a functor F between \(\mathcal {C}\) and \(\mathcal {C}'\) with a map \({\mathsf {m}^0}{}: I' \rightarrow F I\) and a natural transformation with components \(\mathsf {m}_{X,Y} : F X \otimes ' F Y \rightarrow F (X \otimes Y)\) cohering with the unitors and associators of the two categories. A lax monoidal transformation between two lax monoidal functors \((F, {\mathsf {m}^0}{}, \mathsf {m})\) and \((F', {\mathsf {m}^0}{}', \mathsf {m}')\) is a natural transformation \(\tau : F \rightarrow F'\) such that \(\tau _I \circ {\mathsf {m}^0}{}= {\mathsf {m}^0}{}'\) and \(\tau _{X\otimes Y} \circ \mathsf {m}_{X,Y} = \mathsf {m}'_{X,Y} \circ \tau _X \otimes ' \tau _Y\).

We define an lmf-container as a container (SP) with operations

  • \(\mathsf {e}: S\)

  • \(\bullet : S \rightarrow S \rightarrow S\)

  • \(q_0 : \varPi s : S.\, \varPi s' : S.\, P\, (s \bullet s') \rightarrow P\, s\)

  • \(q_1 : \varPi s : S.\, \varPi s' : S.\, P\, (s \bullet s') \rightarrow P\, s'\)

where we write \(q_0\, s\, s'\, p\) as and , satisfying

  • \(\mathsf {e}\bullet s = s\)

  • \(s = s \bullet e\)

  • \((s \bullet s') \bullet s'' = s \bullet (s' \bullet s'')\)

Differently from the mnd-container case, the data \((S, \mathsf {e}, \bullet )\) of a lmf-container form a proper monoid. The data resemble a biaction of \((S, \mathsf {e}, \bullet )\).

We also define an lmf-container morphism between and to be a container morphism (tq) between (SP) and \((S',P')\) such that

  • \(t\, \mathsf {e}= \mathsf {e}'\)

  • \(t\, (s \bullet s') = t\, s \bullet ' t\, s'\)

Lmf-containers form a category \(\mathbf {LCont}\) whose identity and composition are inherited from \(\mathbf {Cont}\).

Every lmf-container interprets into a lax monoidal endofunctor on \((\mathbf {Set}, 1, \times )\) where

  • \(F = [\![S, P]\!]^\mathrm {c}\)

  • \({\mathsf {m}^0}{}\, {*}= (\mathsf {e}, \lambda \_.\, {*})\)

Every lmf-container morphism (tq) between and interprets into a lax monoidal transformation \([\![t, q]\!]^\mathrm {lc} = [\![t, q]\!]^\mathrm {c}\) between and .

\([\![-]\!]^\mathrm {lc}\) is a fully-faithful functor between \(\mathbf {LCont}\) and the category \(\mathbf {LMF}(\mathbf {Set})\) of lax endofunctors on \((\mathbf {Set}, 1, \times )\). The functor \([\![-]\!]^\mathrm {lc}\) is the pullback of the fully-faithful functor \([\![-]\!]^\mathrm {c}: \mathbf {Cont}\rightarrow [\mathbf {Set},\mathbf {Set}]\) along \(U: \mathbf {LMF}(\mathbf {Set}) \rightarrow [\mathbf {Set},\mathbf {Set}]\). The category \(\mathbf {LCont}\) is isomorphic to the category of monoids in .

figure 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 \(\lambda (c, f).\ \mu \, (T\, (\lambda x.\, T\, (\lambda y.\, (x, y))\, (f\, x))\, c) : T\, X \times (X \rightarrow T\, Y) \rightarrow T\,(X \times Y)\) and the monoidality constraint \(\mathsf {m}: F\, X \times F\, Y \rightarrow F\, (X \times Y)\).

It is immediate from the definitions that any mnd-container carries an lmf-container structure given by

  • \(\mathsf {e}' = \mathsf {e}\)

  • \(s \bullet ' s' = s \bullet (\lambda \_.\, 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 on (SP), there is also a reverse lmf-container structure given by

  • \(\mathsf {e}' = \mathsf {e}\)

  • \(s \bullet ' s' = s' \bullet s\)

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.

Coproduct functor. Let \(T\, X = X + E\) for some set E and \(S = 1 + E\), \(P\, (\mathsf {inl}\, {*}) = 1\), \(P\, (\mathsf {inr}\, \_) = 0\).

Any lmf-container structure on (SP) must have \(\mathsf {e}= \mathsf {inl}\ {*}\). Indeed, if it were the case \(\mathsf {e}= \mathsf {inr}\, e_0\) for some \(e_0 : E\), then we would have \(\mathsf {inr}\, e_0 \bullet \mathsf {inl}\, {*}= \mathsf {inl}\, {*}\) by the 1st lmf-container equation. But then \(q_0\, (\mathsf {inr}\, e_0)\, (\mathsf {inl}\, {*}) : 1 \rightarrow 0\), which cannot be.

Similarly, for all \(e_0 : E\), s : S, it must be that \(\mathsf {inr}\, e_0 \bullet s \ne \mathsf {inl}\, {*}\) and \(s \bullet \mathsf {inr}\, e_0 \ne \mathsf {inl}\, {*}\). Hence, by the 1st and 2nd lmf-container equations, it must be the case that \(\mathsf {inl}\, {*}\bullet s = s\), \(\mathsf {inr}\, e \bullet \mathsf {inl}\, {*}= \mathsf {inr}\, e\), \(\mathsf {inr}\, e \bullet \mathsf {inr}\, e' = \mathsf {inr}\, (e \otimes e')\). The 3rd lmf-container equation forces that \(\otimes \) is a semigroup structure on E. The other lmf-container equations hold trivially. Therefore, lmf-container structures on (SP) are in a bijection with semigroup structures on E.

The corresponding lax monoidal functors have \({\mathsf {m}^0}{}\, {*}= \mathsf {inl}\, {*}\), \(\mathsf {m}\, (\mathsf {inl}\, x, \mathsf {inl}\, x') = \mathsf {inl}\, (x, x')\), \(\mathsf {m}\, (\mathsf {inl}\, x, \mathsf {inr}\, e) = \mathsf {inr}\, e\), \(\mathsf {m}\, (\mathsf {inr}\, e, \mathsf {inl}\, x) = \mathsf {inr}\, e\), \(\mathsf {m}\, (\mathsf {inr}\, e, \mathsf {inr}\, e') = \mathsf {inr}\, (e \otimes e')\).

The unique mnd-container structure on (SP) corresponds to the particular case of the left zero semigroup, i.e., the semigroup where \(e \otimes e' = e\).

List functor. Let \(T\, X = \mathsf {List}\, X\) and \(S = \mathbb {N}\), \(P\, s = [0..s)\).

The standard mnd-container structure on (SP) gives this lmf-container structure:

  • \(\mathsf {e}= 1\)

  • \(s \bullet s' = s * s'\)

The corresponding lax monoidal functor structure on T is given by \({\mathsf {m}^0}{}\, {*}= [{*}]\), \(\mathsf {m}\, (xs, ys) = [ (x,y) \mid x \leftarrow xs, y \leftarrow ys]\).

The other mnd-container structure we considered gives \(\mathsf {e}= 1\), \(s \bullet 1 = s\), \(1 \bullet s = s\), \(s \bullet s' = 0\) otherwise, , , , .

The corresponding lax monoidal functor structure on T is \({\mathsf {m}^0}{}\, {*}= [{*}]\), \(\mathsf {m}\, (xs, [y]) = [ (x,y) \mid x \leftarrow xs]\), \(\mathsf {m}\, ([x], ys) = [ (x,y) \mid y \leftarrow ys]\), \(\mathsf {m}\, (xs, ys) = []\) otherwise.

But there are further lmf-container structures on (SP) that do not arise from an mnd-container structure, for example this:

  • \(\mathsf {e}= 1\)

  • \(s \bullet s' = s \min s'\)

The corresponding lax monoidal functor structure is \({\mathsf {m}^0}{}\, {*}= [{*}]\), \(\mathsf {m}\, (xs, ys) = \mathsf {zip}\, (xs, ys)\).

Exponent functor. Let \(T\, X = U \rightarrow X\) for some set U and \(S = 1\), \(P\, {*}= U\).

There is exactly one lmf-container structure on (SP) given by

  • \(\mathsf {e}= {*}\)

  • \({*}\bullet {*}= {*}\)

and that is the lmf-container given by the unique mnd-container structure.

The corresponding lax monoidal functor structure on T is given by \({\mathsf {m}^0}{}\, {*}= \lambda u.\, {*}\), \(\mathsf {m}\, (f, f') = \lambda u.\, (f\, u, f'\, u)\).

Product functor. Let \(T\, X = V \times X\) for some set V and \(S = V\), \(P\, \_ = 1\).

Any lmf-container structure on (SP) must be of the form

  • \(\mathsf {e}= \mathsf {i}\)

  • \(s \bullet s' = s \otimes s'\)

for \((\mathsf {i}, \otimes )\) 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 \({\mathsf {m}^0}{}\, {*}= (\mathsf {i}, {*})\), \(\mathsf {m}\, ((p, x), (p', x')) = (p \otimes 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.e., we have O a set of operations, \(\# : O \rightarrow \mathbb {N}\) a function fixing the arity of each operation and \(\mathsf {id}: O\) and \(\circ : O \rightarrow O \rightarrow O\) an identity operation and a parallel composition operator, with \(\#\, \mathsf {id}= 1\) and \(\#\, (o \circ o') = \#\, o * \#\, o'\), satisfying the equations of an ordinary non-symmetric operad. If we now choose \(S = O\), \(P\, o = [0..\#\, o)\), \(\mathsf {e}= \mathsf {id}\), \(\bullet = \circ \) and take , as in the definition of the standard list lmf-container, we get a non-symmetric operad in this relaxed sense.

Under the lax type universe view, an lmf-container is a lax \((1, \times )\)-universe, i.e., it is only closed under non-dependent lax \(\varSigma \)-types.

5 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\, \sigma _{X,Y} \circ \mathsf {m}_{X,Y} = \mathsf {m}_{Y,X} \circ \sigma _{FX, FY}\)) if and only if the lmf-container is identical to its reverse, i.e., it satisfies

  • \(s \bullet s' = s' \bullet s\),

In this case, the monoid \((S, \mathsf {e}, \bullet )\) 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 \bullet (\lambda \_.\, s') = s' \bullet (\lambda \_.\, 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 \(\eta \) and \(\mu \) are pullbacks) if and only if

  • the function \(\lambda \_.\, {*}: P\, \mathsf {e}\rightarrow 1\) is an isomorphism

  • for any s : S and \(v : P\, s \rightarrow S\), the function is an isomorphism.

Such mnd-containers with additional conditions are proper \((1, \varSigma )\)-type universes: 1 and \(\varSigma \)-types denote the singleton set and dependent products.

6 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 \(\mathbf {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.