This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

The costructure–cosemantics adjunction
for comodels for computational effects

Richard Garner Department of Mathematics & Statistics, Macquarie University, NSW 2109, Australia richard.garner@mq.edu.au
(Date: 31st May 2025)
Abstract.

It is well established that equational algebraic theories, and the monads they generate, can be used to encode computational effects. An important insight of Power and Shkaravska is that comodels of an algebraic theory 𝕋\mathbb{T}—i.e., models in the opposite category 𝒮etop\mathrm{\mathcal{S}et}^{\mathrm{op}}—provide a suitable environment for evaluating the computational effects encoded by 𝕋\mathbb{T}. As already noted by Power and Shkaravska, taking comodels yields a functor from accessible monads to accessible comonads on 𝒮et\mathrm{\mathcal{S}et}. In this paper, we show that this functor is part of an adjunction—the “costructure–cosemantics adjunction” of the title—and undertake a thorough investigation of its properties.

We show that, on the one hand, the cosemantics functor takes its image in what we term the presheaf comonads induced by small categories; and that, on the other, costructure takes its image in the presheaf monads induced by small categories. In particular, the cosemantics comonad of an accessible monad will be induced by an explicitly-described category called its behaviour category that encodes the static and dynamic properties of the comodels. Similarly, the costructure monad of an accessible comonad will be induced by a behaviour category encoding static and dynamic properties of the comonad coalgebras. We tie these results together by showing that the costructure–cosemantics adjunction is idempotent, with fixpoints to either side given precisely by the presheaf monads and comonads. Along the way, we illustrate the value of our results with numerous examples drawn from computation and mathematics.

2000 Mathematics Subject Classification:
Primary:
The support of Australian Research Council grants FT160100393 and DP190102432 is gratefully acknowledged.

1. Introduction

It is a well-known story that the category-theoretic approach to computational effects originates with Moggi [24]. Given a cartesian closed category 𝒞{\mathcal{C}}, providing a denotational semantics for a base notion of computation, this approach allows additional language features such as input/output, interaction with the store, or non-determinism—all falling under the general rubric of “effects”—to be encoded in terms of algebraic structure borne by objects of 𝒞{\mathcal{C}}.

In Moggi’s treatment, this structure is specified via a strong monad 𝖳\mathsf{T} on 𝒞{\mathcal{C}}; a disadvantage of this approach is that, in taking as primitive the objects T(A)T(A) of computations with effects from 𝖳\mathsf{T} and values in AA, it gives no indication of how the effects involved are to be encoded as language features. An important thread [27, 28, 29] in John Power’s work with Gordon Plotkin has sought to rectify this, by identifying a computational effect not with a strong monad per se, but rather with a set of (computationally meaningful) algebraic operations and equations that generate a strong monad, in the sense made precise by Power and Kelly in [19].

The most elementary case of the above takes 𝒞=𝒮et{\mathcal{C}}=\mathrm{\mathcal{S}et}: then a computational effect in Moggi’s sense is simply a monad on 𝒮et\mathrm{\mathcal{S}et}, while an effect in the Plotkin–Power sense is an equational algebraic theory, involving a signature of (possibly infinitary) operations and a set of equations between terms in the signature. As the same monad may admit many different presentations, the Plotkin–Power approach is more refined; but it is also slightly narrower in scope, as not every monad on 𝒮et\mathrm{\mathcal{S}et} is engendered by an algebraic theory, but only the accessible ones—also called monads with rank. (A well-known inaccessible monad is the continuation monad VV()V^{V^{({\mathord{\text{--}}})}}).

The Plotkin–Power approach makes it particularly easy to define models in any category 𝒜{\mathcal{A}} with powers: they are 𝒜{\mathcal{A}}-objects endowed with interpretations of the given operations which satisfy the given equations. When 𝒜=𝒮et{\mathcal{A}}=\mathrm{\mathcal{S}et}, such models are the same as algebras for the associated monad; computationally, these can be interpreted as sets of effectful computations which have been identified up to a notion of equivalence which respects the effect semantics.

It is another important insight of Power and Shkaravska [31] that there is also a computational interpretation of comodels. A comodel in 𝒜{\mathcal{A}} is simply a model in 𝒜op{\mathcal{A}}^{\mathrm{op}}, and again an important case is where 𝒜=𝒮et{\mathcal{A}}=\mathrm{\mathcal{S}et}. The idea is that, given an algebraic theory 𝕋\mathbb{T} for effects which interact with an “environment”, a comodel of 𝕋\mathbb{T} in 𝒮et\mathrm{\mathcal{S}et} provides the kind of environment with which such programs interact. The underlying set SS of such a comodel is the set of possible states of the environment; while each generating AA-ary operation σ\sigma of the theory—which requests an element of AA from the environment and binds it—is co-interpreted as a function σ:SA×S\llbracket\sigma\rrbracket\colon S\rightarrow A\times S which answers the request, and moves to a new state. While, in the first instance, we co-interpret only the generating 𝕋\mathbb{T}-operations, we can extend this inductively to all AA-ary computations tT(A)t\in T(A) of the associated monad; whereupon we can see the co-interpretation t:SA×S\llbracket t\rrbracket\colon S\rightarrow A\times S as a way of running (c.f. [36]) the computation tT(A)t\in T(A) starting from some state sSs\in S to obtain a return value in AA and a final state in SS.

The computational perspective on comodels is powerful, and has achieved significant traction in computer science; see, for example, [30, 23, 36, 25, 26, 3]. However, our objective in this paper is to return to the original [31] and settle some of the unanswered questions posed there. Power and Shkaravska observe that the category of comodels of any algebraic theory is comonadic over 𝒮et\mathrm{\mathcal{S}et}, so that, if we choose to identify algebraic theories with monads, then we have a process which associates to each accessible monad on 𝒮et\mathrm{\mathcal{S}et} a certain comonad on 𝒮et\mathrm{\mathcal{S}et}. This leads them to ask:

“Does the construction of a comonad on 𝒮et\mathrm{\mathcal{S}et} from a monad with rank on 𝒮et\mathrm{\mathcal{S}et} yield an interesting relationship between monads and comonads?”

We will answer this question in the affirmative, by showing that this construction provides the right adjoint part of an adjunction

(1.1) nda(𝒮et)op\textstyle{{\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Cosem\scriptstyle{\mathrm{Cosem}}Costr\scriptstyle{\mathrm{Costr}}\scriptstyle{\bot}𝒞mda(𝒮et)\textstyle{{\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})}}

between accessible monads and accessible comonads on 𝒮et\mathrm{\mathcal{S}et}. The corresponding left adjoint can be seen as taking a comonad 𝖰\mathsf{Q} on 𝒮et\mathrm{\mathcal{S}et} to the largest algebraic theory for which any 𝖰\mathsf{Q}-coalgebra is a comodel. Following [21, 10], we refer to the two functors in this adjunction as “cosemantics” and “costructure”.

In fact, the mere existence of the adjunction (1.1) is not hard to establish. Indeed, as we will see, its two directions were already described in [18], with our costructure corresponding to their dual monad of a comonad, and our cosemantics being their Sweedler dual comonad of a monad. Our real contribution is that we do not seek merely to construct (1.1), but also to understand it thoroughly and concretely.

In one direction, we will explicitly calculate the cosemantics functor; this will, among other things, answer [31]’s request that “we should very much like to be able to characterise those comonads, at least on 𝒮et\mathrm{\mathcal{S}et}, that arise from categories of comodels”. These comonads will be what we term presheaf comonads; for a small category 𝔹\mathbb{B}, the associated presheaf comonad 𝖰𝔹\mathsf{Q}_{\mathbb{B}} is that induced by the adjunction to the left in:

𝒮et𝔹𝗋𝖾𝗌J𝗋𝖺𝗇J𝒮etob(𝔹)ΣΔ𝒮etQ𝔹(A)=b𝔹c𝔹A𝔹(b,c) ,\vbox{\hbox{\lx@xy@svg{\hbox{\raise 0.0pt\hbox{\kern 11.81113pt\hbox{\ignorespaces\ignorespaces\ignorespaces\hbox{\vtop{\kern 0.0pt\offinterlineskip\halign{\entry@#!@&&\entry@@#!@\cr&&\crcr}}}\ignorespaces{\hbox{\kern-11.81113pt\raise 0.0pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\raise 0.0pt\hbox{$\textstyle{{\mathrm{\mathcal{S}et}^{\mathbb{B}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}$}}}}}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 15.2028pt\raise-9.69028pt\hbox{{}\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\hbox{\hbox{\kern 0.0pt\raise-0.82361pt\hbox{$\scriptstyle{\mathsf{res}_{J}}$}}}\kern 3.0pt}}}}}}\ignorespaces{\hbox{\kern 35.81113pt\raise-4.5pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\lx@xy@tip{1}\lx@xy@tip{-1}}}}}}{\hbox{\lx@xy@droprule}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 11.81113pt\raise 4.5pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\lx@xy@tip{1}\lx@xy@tip{-1}}}}}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 14.44446pt\raise 9.69028pt\hbox{{}\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\hbox{\hbox{\kern 0.0pt\raise-0.82361pt\hbox{$\scriptstyle{\mathsf{ran}_{J}}$}}}\kern 3.0pt}}}}}}\ignorespaces{}{\hbox{\lx@xy@droprule}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{}\ignorespaces{\hbox{\kern 21.0889pt\raise 0.0pt\hbox{\hbox{\kern 0.0pt\raise-2.43056pt\hbox{$\scriptstyle{\top}$}}}}}\ignorespaces{}{\hbox{\kern 35.81113pt\raise 0.0pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\raise 0.0pt\hbox{$\textstyle{{{\mathrm{\mathcal{S}et}^{\mathrm{ob}(\mathbb{B})}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}$}}}}}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 76.1723pt\raise-9.89166pt\hbox{{}\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\hbox{\hbox{\kern 0.0pt\raise-2.39166pt\hbox{$\scriptstyle{\Sigma}$}}}\kern 3.0pt}}}}}}\ignorespaces{\hbox{\kern 93.70007pt\raise-4.5pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\lx@xy@tip{1}\lx@xy@tip{-1}}}}}}{\hbox{\lx@xy@droprule}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 69.70007pt\raise 4.5pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\lx@xy@tip{1}\lx@xy@tip{-1}}}}}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 75.7834pt\raise 9.89166pt\hbox{{}\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\hbox{\hbox{\kern 0.0pt\raise-2.39166pt\hbox{$\scriptstyle{\Delta}$}}}\kern 3.0pt}}}}}}\ignorespaces{}{\hbox{\lx@xy@droprule}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{}\ignorespaces{\hbox{\kern 78.97784pt\raise 0.0pt\hbox{\hbox{\kern 0.0pt\raise-2.43056pt\hbox{$\scriptstyle{\top}$}}}}}\ignorespaces{}{\hbox{\kern 93.70007pt\raise 0.0pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\raise 0.0pt\hbox{$\textstyle{{\mathrm{\mathcal{S}et}}}$}}}}}}}\ignorespaces}}}}\ignorespaces}}\qquad\qquad Q_{\mathbb{B}}(A)=\sum_{b\in\mathbb{B}}\prod_{c\in\mathbb{B}}A^{\mathbb{B}(b,c)}\hbox to0.0pt{ ,\hss}

with the explicit formula as to the right. These comonads are known entities in computer science: they are precisely the interpretations of directed containers as introduced in [4], and in [7] were termed dependently typed coupdate comonads.

In fact, we do more than merely characterising the image of the cosemantics functor: we prove for each accessible monad 𝖳\mathsf{T} on 𝒮et\mathrm{\mathcal{S}et} that its image under cosemantics is the presheaf comonad of an explicitly given category 𝔹𝖳\mathbb{B}_{\mathsf{T}}, which we term the behaviour category of 𝖳\mathsf{T}. Since the category of Eilenberg–Moore 𝖰𝔹𝖳\mathsf{Q}_{\mathbb{B}_{\mathsf{T}}}-coalgebras is equivalent to the functor category [𝔹𝖳,𝒮et][\mathbb{B}_{\mathsf{T}},\mathrm{\mathcal{S}et}], we may also state this result as:

Theorem.

Given an accessible monad 𝖳\mathsf{T} with behaviour category 𝔹𝖳\mathbb{B}_{\mathsf{T}}, the category of 𝖳\mathsf{T}-comodels is equivalent to [𝔹𝕋,𝒮et][\mathbb{B}_{\mathbb{T}},\mathrm{\mathcal{S}et}] via an equivalence commuting with the forgetful functors to 𝒮et\mathrm{\mathcal{S}et}.

Our description of the behaviour category 𝔹𝖳\mathbb{B}_{\mathsf{T}} is quite intuitive. Objects β𝔹𝖳\beta\in\mathbb{B}_{\mathsf{T}} are elements of the final 𝖳\mathsf{T}-comodel in 𝒮et\mathrm{\mathcal{S}et}, which may be described in many ways; we give a novel presentation as what we term admissible behaviours of 𝖳\mathsf{T}. These comprise families (βA:T(A)A)A𝒮et(\beta_{A}\colon T(A)\rightarrow A)_{A\in\mathrm{\mathcal{S}et}} of functions, satisfying two axioms expressing that β\beta acts like a state of a comodel in providing a uniform way of running 𝖳\mathsf{T}-computations to obtain values. As for morphisms of 𝔹𝖳\mathbb{B}_{\mathsf{T}}, these will be transitions between admissible behaviours determined by 𝖳\mathsf{T}-commands, i.e., unary operations mT(1)m\in T(1). We will see that maps with domain β\beta in 𝔹𝖳\mathbb{B}_{\mathsf{T}} are 𝖳\mathsf{T}-commands identified up to an equivalence relation β\sim_{\beta} which identifies commands which act in the same way on all states of behaviour β\beta.

We also describe the action of the cosemantics functor on morphisms. Thus, given a map of monads f:𝖳1𝖳2f\colon\mathsf{T}_{1}\rightarrow\mathsf{T}_{2}—which encodes an interpretation or compilation of effects—we describe the induced map of presheaf comonads 𝖰𝔹𝖳2𝖰𝔹𝖳1\mathsf{Q}_{\mathbb{B}_{\mathsf{T}_{2}}}\rightarrow\mathsf{Q}_{\mathbb{B}_{\mathsf{T}_{1}}}. As explained in [7], maps of presheaf comonads do not correspond to functors, but rather to to so-called cofunctors [15, 2] of the corresponding categories, involving a mapping forwards at the level of objects, and mappings backwards on morphisms. We are able to give an explicit description of the cofunctor on behaviour categories induced by a monad morphism, and on doing so we obtain our second main result:

Theorem.

The functor 𝔹():nda(𝒮et)op𝒞of\mathbb{B}_{({\mathord{\text{--}}})}\colon\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}of} taking each accessible monad to its behaviour category, and each map of accessible monads to the induced cofunctor on behaviour categories yields a within-isomorphism factorisation

𝒞of\textstyle{\mathrm{\mathcal{C}of}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}𝖰()\scriptstyle{\mathsf{Q}_{({\mathord{\text{--}}})}}nda(𝒮et)op\textstyle{\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Cosem\scriptstyle{\mathrm{Cosem}}𝔹()\scriptstyle{\mathbb{B}_{({\mathord{\text{--}}})}}𝒞mda(𝒮et) .\textstyle{\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})\hbox to0.0pt{ .\hss}}

For the other direction of the adjunction (1.1), we will in an analogous manner give an explicit calculation of the image of the costructure functor. On objects, the monads in this image are what we term presheaf monads; here, for a small category 𝔹\mathbb{B}, the presheaf monad 𝖳𝔹\mathsf{T}_{\mathbb{B}} is that induced by the adjunction to the left in:

𝒮et𝔹op𝗋𝖾𝗌J𝗅𝖺𝗇J𝒮etob(𝔹)ΠΔ𝒮etT𝔹(A)=b𝔹c𝔹𝔹(b,c)×A ,\vbox{\hbox{\lx@xy@svg{\hbox{\raise 0.0pt\hbox{\kern 13.50002pt\hbox{\ignorespaces\ignorespaces\ignorespaces\hbox{\vtop{\kern 0.0pt\offinterlineskip\halign{\entry@#!@&&\entry@@#!@\cr&&\crcr}}}\ignorespaces{\hbox{\kern-13.50002pt\raise 0.0pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\raise 0.0pt\hbox{$\textstyle{{\mathrm{\mathcal{S}et}^{\mathbb{B}^{\mathrm{op}}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}$}}}}}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 16.8917pt\raise-9.69028pt\hbox{{}\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\hbox{\hbox{\kern 0.0pt\raise-0.82361pt\hbox{$\scriptstyle{\mathsf{res}_{J}}$}}}\kern 3.0pt}}}}}}\ignorespaces{\hbox{\kern 37.50002pt\raise-4.5pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\lx@xy@tip{1}\lx@xy@tip{-1}}}}}}{\hbox{\lx@xy@droprule}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 13.50002pt\raise 4.5pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\lx@xy@tip{1}\lx@xy@tip{-1}}}}}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 16.53197pt\raise 10.61389pt\hbox{{}\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\hbox{\hbox{\kern 0.0pt\raise-1.74722pt\hbox{$\scriptstyle{\mathsf{lan}_{J}}$}}}\kern 3.0pt}}}}}}\ignorespaces{}{\hbox{\lx@xy@droprule}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{}\ignorespaces{\hbox{\kern 22.77779pt\raise 0.0pt\hbox{\hbox{\kern 0.0pt\raise-2.43056pt\hbox{$\scriptstyle{\bot}$}}}}}\ignorespaces{}{\hbox{\kern 37.50002pt\raise 0.0pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\raise 0.0pt\hbox{$\textstyle{{{\mathrm{\mathcal{S}et}^{\mathrm{ob}(\mathbb{B})}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}$}}}}}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 77.76396pt\raise-9.89166pt\hbox{{}\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\hbox{\hbox{\kern 0.0pt\raise-2.39166pt\hbox{$\scriptstyle{\Pi}$}}}\kern 3.0pt}}}}}}\ignorespaces{\hbox{\kern 95.38896pt\raise-4.5pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\lx@xy@tip{1}\lx@xy@tip{-1}}}}}}{\hbox{\lx@xy@droprule}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 71.38896pt\raise 4.5pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\lx@xy@tip{1}\lx@xy@tip{-1}}}}}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces{\hbox{\kern 77.47229pt\raise 9.89166pt\hbox{{}\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\hbox{\hbox{\kern 0.0pt\raise-2.39166pt\hbox{$\scriptstyle{\Delta}$}}}\kern 3.0pt}}}}}}\ignorespaces{}{\hbox{\lx@xy@droprule}}{\hbox{\lx@xy@droprule}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces{}\ignorespaces{\hbox{\kern 80.66673pt\raise 0.0pt\hbox{\hbox{\kern 0.0pt\raise-2.43056pt\hbox{$\scriptstyle{\bot}$}}}}}\ignorespaces{}{\hbox{\kern 95.38896pt\raise 0.0pt\hbox{\hbox{\kern 0.0pt\raise 0.0pt\hbox{\hbox{\kern 3.0pt\raise 0.0pt\hbox{$\textstyle{{\mathrm{\mathcal{S}et}}}$}}}}}}}\ignorespaces}}}}\ignorespaces}}\qquad\qquad T_{\mathbb{B}}(A)=\prod_{b\in\mathbb{B}}\sum_{c\in\mathbb{B}}{\mathbb{B}(b,c)}\times A\hbox to0.0pt{ ,\hss}

with the explicit formula as to the right (note the duality with the comonad 𝖰𝖡\mathsf{Q}_{\mathsf{B}}). Much like before, we will prove for each accessible comonad 𝖰\mathsf{Q} on 𝒮et\mathrm{\mathcal{S}et} that its image under costructure is of the form 𝖳𝔹𝖰\mathsf{T}_{\mathbb{B}_{\mathsf{Q}}} for an explicitly given “behaviour category” 𝔹𝖰\mathbb{B}_{\mathsf{Q}}. The picture is perhaps less compelling in this direction, but the objects of 𝔹𝖰\mathbb{B}_{\mathsf{Q}} can again be described as “behaviours”, by which we now mean elements of the final 𝖰\mathsf{Q}-coalgebra Q(1)Q(1); while morphisms of 𝔹𝖰\mathbb{B}_{\mathsf{Q}} are uniform ways of transitioning between 𝖰\mathsf{Q}-behaviours. Like before, we also compute the costructure functor on morphisms, and again find that each comonad morphism induces a cofunctor between behaviour categories; so yielding our third main result:

Theorem.

The functor 𝔹():𝒞mda(𝒮et)𝒞of\mathbb{B}_{({\mathord{\text{--}}})}\colon\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})\rightarrow\mathrm{\mathcal{C}of} taking an accessible comonad to its behaviour category, and a map of accessible comonads to the induced cofunctor on behaviour categories yields a within-isomorphism factorisation

𝒞of\textstyle{\mathrm{\mathcal{C}of}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}𝖰()\scriptstyle{\mathsf{Q}_{({\mathord{\text{--}}})}}𝒞mda(𝒮et)\textstyle{\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Costr\scriptstyle{\mathrm{Costr}}𝔹()\scriptstyle{\mathbb{B}_{({\mathord{\text{--}}})}}nda(𝒮et)op .\textstyle{\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\hbox to0.0pt{ .\hss}}

It remains only to understand how costructure and cosemantics interact with each other. The crucial observation is that (1.1) is an example of a so-called idempotent (or Galois) adjunction. Here, an adjunction FG:𝒟𝒞F\dashv G\colon{\mathcal{D}}\rightarrow{\mathcal{C}} is idempotent if any application of FF yields a fixpoint to the left, i.e., an object of 𝒟{\mathcal{D}} at which the adjunction counit is invertible, while any application of GG yields a fixpoint to the right, i.e., an object of 𝒞{\mathcal{C}} at which the adjunction unit is invertible. In these terms, our final main result can be stated as:

Theorem.

The costructure–cosemantics adjunction (1.1) is idempotent. Its fixpoints to the left and the right are the presheaf monads and presheaf comonads.

Let us note that the results of this paper are only the first step in a larger investigation. On the one hand, to deal with recursion, we will require a comprehensive understanding of enriched versions of the costructure–cosemantics adjunction. On the other hand, even in the unenriched world of equational algebraic theories, we may be interested in understanding costructure and cosemantics for comodels in other categories than 𝒮et\mathrm{\mathcal{S}et}: for example, topological comodels, which encode information not only about behaviours of states, but also about finitistic, computable observations of such behaviour. We hope to pursue these avenues in future work.

We conclude this introduction with a brief overview of the contents of the paper. We begin in Section 2 with background material on algebraic theories, their models and comodels, and the relation to monads on 𝒮et\mathrm{\mathcal{S}et}, along with relevant examples relating to computational effects. In Section 3, we prepare the ground for our main results by investigating the classes of presheaf monads and comonads. Then in Section 4, we give the construction of the costructure–cosemantics adjunction (1.1), and explain how its two directions encapsulate constructions of [18].

In Section 5, we calcuate the values of the cosemantics functor. We begin with a general category-theoretic argument that shows that its must take values in presheaf comonads; we then give a concrete calculation of the presheaf comonad associated to a given accessible monad, in other words, to the calculation of the behaviour category of the given monad. We also describe the cofunctors between behaviour categories induced by monad morphisms.

In Section 6, we turn to the costructure functor, showing by a direct calculation that it sends each accessible comonad to the presheaf monad of an appropriate behaviour category. As before, we also describe the cofunctor on behaviour categories induced by each map of accessible comonads. Then in Section 7, we tie these results together by exhibiting the idempotency of the costructure–cosemantics adjunction, and characterising its fixpoints as the presheaf monads and comonads. Finally, Sections 8 and 9 are devoted to examples of behaviour categories and cofunctors calculated using our main results.

2. Algebraic theories and their (co)models

2.1. Algebraic theories

In this background section, we recall the definition of (possibly infinitary) algebraic theory; the notions of model and comodel in any suitable category; and the relation to monads on 𝒮et\mathrm{\mathcal{S}et}. We also recall the applications of these notions in the study of computational effects.

Definition 1 (Algebraic theory).

A signature comprises a set Σ\Sigma of function symbols, and for each σΣ\sigma\in\Sigma a set |σ||\sigma|, its arity. Given a signature Σ\Sigma and a set AA, we define the set Σ(A)\Sigma(A) of Σ\Sigma-terms with variables in AA by the inductive clauses

aAaΣ(A)andσΣtΣ(A)|σ|σ(t)Σ(A) .a\in A\implies a\in\Sigma(A)\quad\text{and}\quad\sigma\in\Sigma\text{, }t\in\Sigma(A)^{|\sigma|}\implies\sigma(t)\in\Sigma(A)\hbox to0.0pt{ .\hss}

An equation over a signature Σ\Sigma is a triple (A,t,u)(A,t,u) with AA a set and t,uΣ(A)t,u\in\Sigma(A). An algebraic theory 𝕋\mathbb{T} is a signature Σ\Sigma and a set {\mathcal{E}} of equations over it.

Definition 2 (𝕋\mathbb{T}-terms).

Given a signature Σ\Sigma and terms tΣ(A)t\in\Sigma(A) and uΣ(B)Au\in\Sigma(B)^{A}, we define the substitution t(u)Σ(B)t(u)\in\Sigma(B) recursively by:

aAa(u)=uaandσΣtΣ(A)|σ|(σ(t))(u)=σ(λi.ti(u)) .\quad a\in A\implies a(u)=u_{a}\quad\text{and}\quad\sigma\in\Sigma\text{, }t\in\Sigma(A)^{|\sigma|}\implies(\sigma(t))(u)=\sigma(\lambda i.\,t_{i}(u))\hbox to0.0pt{ .\hss}

Given an algebraic theory 𝕋=(Σ,)\mathbb{T}=(\Sigma,{\mathcal{E}}) and a set BB, we define 𝕋\mathbb{T}-equivalence to be the smallest equivalence relation 𝕋\equiv_{\mathbb{T}} on Σ(B)\Sigma(B) such that:

  1. (i)

    If (A,t,u)(A,t,u)\in{\mathcal{E}} and vΣ(B)Av\in\Sigma(B)^{A}, then t(v)𝕋u(v)t(v)\equiv_{\mathbb{T}}u(v);

  2. (ii)

    If σΣ\sigma\in\Sigma and ti𝕋uit_{i}\equiv_{\mathbb{T}}u_{i} for all i|σ|i\in|\sigma|, then σ(t)𝕋σ(u)\sigma(t)\equiv_{\mathbb{T}}\sigma(u).

The set T(A)T(A) of 𝕋\mathbb{T}-terms with variables in AA is the quotient Σ(A)/𝕋\Sigma(A)\delimiter 84079374\mathopen{}\equiv_{\mathbb{T}}.

When an algebraic theory 𝕋\mathbb{T} is thought of as specifying a computational effect, we think of T(A)T(A) as giving the set of computations with effects from 𝕋\mathbb{T} and returning a value in AA. The following standard examples illustrate this.

Example 3 (Input).

Given a set VV, the theory of VV-valued input comprises a single VV-ary function symbol 𝗋𝖾𝖺𝖽\mathsf{read}, satisfying no equations, whose action we think of as:

(t:VX)let read() be vt(v) .(t\colon V\rightarrow X)\mapsto\text{{let read$()$ be $v$.\,$t(v)$}}\hbox to0.0pt{ .\hss}

For this theory, terms tT(A)t\in T(A) are computations which can request VV-values from an external source and use them to determine a return value in AA. For example, when V=V=\mathbb{N}, the program which requests two input values and returns their sum is encoded by

(2.1) let read() be n. let read() be mn+mT() .\text{{let read$()$ be $n$.\,let read$()$ be $m$.\,$n+m$}}\ \in\ T(\mathbb{N})\hbox to0.0pt{ .\hss}

For an algebraic theory qua computational effect, it is idiomatic that its function symbols are read in continuation-passing style; so the domain of a function symbol XVXX^{V}\rightarrow X is a scope in which an element of VV is available to determine a continuation, and applying the operation binds this element to yield a continuation simpliciter.

Example 4 (Output).

Given a set VV, the theory of VV-valued output comprises an VV-indexed family of unary function symbols (𝗐𝗋𝗂𝗍𝖾v:vV)(\mathsf{write}_{v}:v\in V) subject to no equations. In the continuation-passing style, we denote the action of 𝗐𝗋𝗂𝗍𝖾v\mathsf{write}_{v} by

tlet write(v) be xtor, more simply,twrite(v); t .t\mapsto\textsf{let write$(v)$ be $x$.\,$t$}\qquad\text{or, more simply,}\qquad t\mapsto\text{{write}$(v)${;} }t\hbox to0.0pt{ .\hss}
Example 5 (Read-only state).

Given a set VV, the theory of VV-valued read-only state has a single VV-ary operation 𝗀𝖾𝗍\mathsf{get}, satisfying the equations

(2.2) 𝗀𝖾𝗍(λv.x)xand𝗀𝖾𝗍(λv.𝗀𝖾𝗍(λw.xvw))𝗀𝖾𝗍(λv.xvv) .\mathsf{get}(\lambda v.\,x)\equiv x\qquad\text{and}\qquad\mathsf{get}(\lambda v.\,\mathsf{get}(\lambda w.\,x_{vw}))\equiv\mathsf{get}(\lambda v.\,x_{vv})\hbox to0.0pt{ .\hss}

These equations express that reading from read-only state should not change that state, and that repeatedly reading the state should always yield the same answer. In universal algebra, these are the so-called “rectangular band” identities; while in another nomenclature they express that 𝗀𝖾𝗍\mathsf{get} is copyable and discardable; see [35].

Example 6 (State, [28]).

Given a set VV, the theory of VV-valued state comprises an VV-ary operation 𝗀𝖾𝗍\mathsf{get} and a VV-indexed family of unary operations 𝗉𝗎𝗍v\mathsf{put}_{v}, subject to the following equations:

𝗀𝖾𝗍(λv.𝗉𝗎𝗍v(x))x𝗉𝗎𝗍u(𝗉𝗎𝗍v(x))𝗉𝗎𝗍v(x)𝗉𝗎𝗍u(𝗀𝖾𝗍(λv.xv))𝗉𝗎𝗍u(xu) .\mathsf{get}(\lambda v.\,\mathsf{put}_{v}(x))\equiv x\quad\mathsf{put}_{u}(\mathsf{put}_{v}(x))\equiv\mathsf{put}_{v}(x)\quad\mathsf{put}_{u}(\mathsf{get}(\lambda v.\,x_{v}))\equiv\mathsf{put}_{u}(x_{u})\hbox to0.0pt{ .\hss}

Read in continuation-passing style, these axioms capture the semantics of reading and updating a store containing an element of VV.

We now describe the appropriate morphisms between algebraic theories.

Definition 7 (Category of algebraic theories).

Let 𝖳1=(Σ1,1)\mathsf{T}_{1}=(\Sigma_{1},{\mathcal{E}}_{1}) and 𝖳2=(Σ2,2)\mathsf{T}_{2}=(\Sigma_{2},{\mathcal{E}}_{2}) be algebraic theories. An interpretation f:𝕋1𝕋2f\colon\mathbb{T}_{1}\rightarrow\mathbb{T}_{2} is given by specifying, for each σΣ1\sigma\in\Sigma_{1}, a term σfΣ2(|σ|)\sigma^{f}\in\Sigma_{2}(|\sigma|) such that, on defining for each tΣ1(A)t\in\Sigma_{1}(A) the term tfΣ2(A)t^{f}\in\Sigma_{2}(A) by the recursive clauses

(2.3) aAaf=aandσΣtΣ(A)|σ|(σ(t))f=σf(λa.(ta)f) ,a\in A\Rightarrow a^{f}=a\quad\text{and}\quad\sigma\in\Sigma\text{, }t\in\Sigma(A)^{|\sigma|}\Rightarrow(\sigma(t))^{f}=\sigma^{f}(\lambda a.\,(t_{a})^{f})\hbox to0.0pt{ ,\hss}

we have that tf𝕋2uft^{f}\equiv_{\mathbb{T}_{2}}u^{f} for all (A,t,u)1(A,t,u)\in{\mathcal{E}}_{1}. With the obvious composition, we obtain a category 𝒜lg𝒯h\mathrm{\mathcal{A}lg{\mathcal{T}}h} of algebraic theories and interpretations.

An interpretation 𝕋1𝕋2\mathbb{T}_{1}\rightarrow\mathbb{T}_{2} between theories can be understood as a way of translating computations with effects from 𝕋1\mathbb{T}_{1} into ones with effects from 𝕋2\mathbb{T}_{2}.

Example 8.

Let h:VWh\colon V\rightarrow W be a function between sets, let 𝕋1\mathbb{T}_{1} be the theory of VV-valued output, and let 𝕋2\mathbb{T}_{2} be the theory of WW-valued state. We have an interpretation f:𝕋1𝕋2f\colon\mathbb{T}_{1}\rightarrow\mathbb{T}_{2} defined by 𝗐𝗋𝗂𝗍𝖾f=𝗉𝗎𝗍h(v)\mathsf{write}^{f}=\mathsf{put}_{h(v)}.

Example 9.

Let h:WVh\colon W\rightarrow V be a function between sets, let 𝕋1\mathbb{T}_{1} be the theory of VV-valued read-only state, and let 𝕋2\mathbb{T}_{2} be the theory of WW-valued state. We have an interpretation f:𝕋1𝕋2f\colon\mathbb{T}_{1}\rightarrow\mathbb{T}_{2} defined by 𝗀𝖾𝗍f=𝗀𝖾𝗍(λw.h(w))\mathsf{get}^{f}=\mathsf{get}(\lambda w.\,h(w)).

2.2. Models and comodels

We now describe the notion of model of an algebraic theory in a suitable category. Recall that a category 𝒞{\mathcal{C}} has powers if, for every X𝒞X\in{\mathcal{C}} and set AA, the AA-fold self-product (πa:XAX)aA(\pi_{a}\colon X^{A}\rightarrow X)_{a\in A} exists in 𝒞{\mathcal{C}}.

Definition 10 (Σ\Sigma-structure).

Let Σ\Sigma be a signature. A Σ\Sigma-structure 𝐗=(X,𝐗){\boldsymbol{X}}=(X,\llbracket{\mathord{\text{--}}}\rrbracket_{{\boldsymbol{X}}}) in a category 𝒞{\mathcal{C}} with powers comprises an underlying object X𝒞X\in{\mathcal{C}} and operations σ𝑿:X|σ|X\llbracket\sigma\rrbracket_{{\boldsymbol{X}}}\colon X^{|\sigma|}\rightarrow X for each σΣ\sigma\in\Sigma. Given a Σ\Sigma-structure 𝑿𝒞Σ{\boldsymbol{X}}\in{\mathcal{C}}^{\Sigma}, we define for each tΣ(A)t\in\Sigma(A) the derived operation t𝑿:XAX\llbracket t\rrbracket_{{\boldsymbol{X}}}\colon X^{A}\rightarrow X by the following recursive clauses:

(2.4) a𝑿=πaandσ(t)𝑿=XA\ext@arrow01200\rightarrowfill@(ti𝑿)i|σ|X|σ|\ext@arrow01200\rightarrowfill@σ𝑿X .\llbracket a\rrbracket_{{\boldsymbol{X}}}=\pi_{a}\qquad\text{and}\qquad\smash{\llbracket\sigma(t)\rrbracket_{{\boldsymbol{X}}}=X^{A}\ext@arrow 01{20}0\rightarrowfill@{}{(\llbracket t_{i}\rrbracket_{{\boldsymbol{X}}})_{i\in|\sigma|}}X^{|\sigma|}\ext@arrow 01{20}0\rightarrowfill@{}{\llbracket\sigma\rrbracket_{{\boldsymbol{X}}}}X}\hbox to0.0pt{ .\hss}
Definition 11 (𝕋\mathbb{T}-model).

Let 𝕋=(Σ,)\mathbb{T}=(\Sigma,{\mathcal{E}}) be an algebraic theory. A 𝕋\mathbb{T}-model in a category with powers 𝒞{\mathcal{C}} is a Σ\Sigma-structure 𝑿{\boldsymbol{X}} such that t𝑿=u𝑿:XAX\llbracket t\rrbracket_{{\boldsymbol{X}}}=\llbracket u\rrbracket_{{\boldsymbol{X}}}\colon X^{A}\rightarrow X for all (A,t,u)(A,t,u)\in{\mathcal{E}}. We write 𝒞𝕋{\mathcal{C}}^{\mathbb{T}} for the category whose objects are 𝕋\mathbb{T}-models in 𝒞{\mathcal{C}}, and whose maps 𝑿𝒀{\boldsymbol{X}}\rightarrow{\boldsymbol{Y}} are 𝒞{\mathcal{C}}-maps f:XYf\colon X\rightarrow Y such that σ𝒀f|σ|=fσ𝑿\llbracket\sigma\rrbracket_{{\boldsymbol{Y}}}\circ f^{|\sigma|}=f\circ\llbracket\sigma\rrbracket_{{\boldsymbol{X}}} for all σΣ\sigma\in\Sigma. We write U𝕋:𝒞𝕋𝒞U^{\mathbb{T}}\colon{\mathcal{C}}^{\mathbb{T}}\rightarrow{\mathcal{C}} for the obvious forgetful functor.

Dual to the notion of model is the notion of comodel. Recall that a category 𝒞{\mathcal{C}} has copowers if every AA-fold self-coproduct (νa:XAX)aA(\nu_{a}\colon X\rightarrow A\cdot X)_{a\in A} exists in 𝒞{\mathcal{C}}.

Definition 12 (Comodel).

Let 𝕋\mathbb{T} be an algebraic theory. A 𝕋\mathbb{T}-comodel in a category 𝒞{\mathcal{C}} with copowers is a model of 𝕋\mathbb{T} in 𝒞op{\mathcal{C}}^{\mathrm{op}}; it thus comprises an object X𝒞X\in{\mathcal{C}} and “co-operations” σ𝑿:X|σ|X\llbracket\sigma\rrbracket_{{\boldsymbol{X}}}\colon X\rightarrow|\sigma|\cdot X, subject to the equations of 𝕋\mathbb{T}. We write 𝒞𝕋{}^{\mathbb{T}}{\mathcal{C}} for the category of 𝕋\mathbb{T}-comodels in 𝒞{\mathcal{C}} and U𝕋:𝒞𝕋𝒞{}^{\mathbb{T}}U\colon{}^{\mathbb{T}}{\mathcal{C}}\rightarrow{\mathcal{C}} for the forgetful functor.

If we say simply “model” or “comodel”, we will by default mean model or comodel in 𝒮et\mathrm{\mathcal{S}et}. As explained in [31, 30], set-based comodels provide deterministic environments suitable for evaluating computations with effects from 𝕋\mathbb{T}.

Example 13.

A comodel 𝑺{\boldsymbol{S}} of the theory of VV-valued input is a state machine which responds to requests for VV-characters; it comprises a set of states SS and a map 𝗋𝖾𝖺𝖽𝑺=(g,n):SV×S\llbracket\mathsf{read}\rrbracket_{{\boldsymbol{S}}}=(g,n)\colon S\rightarrow V\times S assigning to each sSs\in S a character g(s)Vg(s)\in V to be read and a new state n(s)Sn(s)\in S.

Example 14.

A comodel 𝑺{{\boldsymbol{S}}} of the theory of VV-valued output is a state machine which changes its state in response to VV-characters: it comprises a set of states SS and maps 𝗐𝗋𝗂𝗍𝖾v𝑺:SS\llbracket\mathsf{write}_{v}\rrbracket_{{{\boldsymbol{S}}}}\colon S\rightarrow S for each vVv\in V, or equally, a single map p:V×SSp\colon V\times S\rightarrow S, providing for each character vVv\in V and state sSs\in S a new state p(v,s)Sp(v,s)\in S.

Example 15.

A comodel 𝑺{\boldsymbol{S}} of the theory of VV-valued read-only state comprises a set SS together with a function 𝗀𝖾𝗍𝑺=(g,n):SV×S\llbracket\mathsf{get}\rrbracket_{{\boldsymbol{S}}}=(g,n)\colon S\rightarrow V\times S satisfying

(2.5) n(s)=sand(g(s),g(n(s)),n(n(s)))=(g(s),g(s),n(s)) .n(s)=s\qquad\text{and}\qquad\bigl{(}g(s),g(n(s)),n(n(s))\bigr{)}=\bigl{(}g(s),g(s),n(s)\bigr{)}\hbox to0.0pt{ .\hss}

The first axiom allows us to ignore nn, and moreover implies the second axiom; whence a comodel amounts to nothing more than a set SS and a function g:SVg\colon S\rightarrow V.

Example 16.

A comodel of the theory of VV-valued state involves a set of states SS together with maps (g,n):SV×S(g,n)\colon S\rightarrow V\times S and p:S×VSp\colon S\times V\rightarrow S rendering commutative the diagrams

V×S\textstyle{V\times S\ignorespaces\ignorespaces\ignorespaces\ignorespaces}(g,n)\scriptstyle{(g,n)}V×S\textstyle{V\times S\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}V×p\scriptstyle{V\times p}(g,n)\scriptstyle{(g,n)}V\textstyle{V\ignorespaces\ignorespaces\ignorespaces\ignorespaces}p\scriptstyle{p}S\textstyle{S\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}p\scriptstyle{p}p\scriptstyle{p}V×S .\textstyle{V\times S\hbox to0.0pt{ .\hss}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}V×p\scriptstyle{V\times p}S\textstyle{S\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}p\scriptstyle{p}V\textstyle{V}V×(V×S)\textstyle{V\times(V\times S)\ignorespaces\ignorespaces\ignorespaces\ignorespaces}\scriptstyle{\cong}(V×V)×S\textstyle{(V\times V)\times S\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Δ×S\scriptstyle{\Delta\times S}V×S\textstyle{V\times S}V×S\textstyle{V\times S\ignorespaces\ignorespaces\ignorespaces\ignorespaces}V×π1\scriptstyle{V\times\pi_{1}}V×(V×S)\textstyle{V\times(V\times S)}

These are equally the conditions that

p(g(s),n(s))=s,n(p(v,s))=p(v,s),g(p(v,s))=vandp(v,p(v,s))=p(v,s) ,p(g(s),n(s))=s\text{,}\ \ n(p(v,s))=p(v,s)\text{,}\ \ g(p(v,s))=v\ \ \text{and}\ \ p(v^{\prime},p(v,s))=p(v^{\prime},s)\hbox to0.0pt{ ,\hss}

the first two of which imply n=idSn=\mathrm{id}_{S}. Thus, to give the comodel is to give the set SS together with maps g:SVg\colon S\rightarrow V and p:V×SSp\colon V\times S\rightarrow S satisfying the axioms

p(g(s),s)=sg(p(v,s))=vandp(v,p(v,s))=p(v,s) ;p(g(s),s)=s\qquad g(p(v,s))=v\quad\text{and}\quad p(v^{\prime},p(v,s))=p(v^{\prime},s)\hbox to0.0pt{ ;\hss}

as noted in [36], this is precisely a (very well-behaved) lens in the sense of [11].

A 𝕋\mathbb{T}-comodel allows us to evaluate 𝕋\mathbb{T}-computations by way of the derived operations of Definition 10. Indeed, given a comodel 𝑺𝒮𝕋et{\boldsymbol{S}}\in{}^{\mathbb{T}}\mathrm{\mathcal{S}et} and a term tT(A)t\in T(A), the co-operation t:SA×S\llbracket t\rrbracket\colon S\rightarrow A\times S is defined by the recursive clauses

(2.6) aA\displaystyle a\in A a(s)=(a,s)\displaystyle\implies\llbracket a\rrbracket(s)=(a,s)\qquad\ \ \,
and σΣ,tT(A)|σ|\displaystyle\text{and }\sigma\in\Sigma,t\in T(A)^{|\sigma|} σ(t)(s)=ti(s) where σ(s)=(i,s) .\displaystyle\implies\llbracket\sigma(t)\rrbracket(s)=\llbracket t_{i}\rrbracket(s^{\prime})\text{ where }\llbracket\sigma\rrbracket(s)=(i,s^{\prime})\hbox to0.0pt{ .\hss}

(Here, and henceforth, we drop the subscript 𝑺{\boldsymbol{S}} where confusion seems unlikely.) The semantics of this is clear: given a 𝕋\mathbb{T}-computation σ(t)T(A)\sigma(t)\in T(A) and starting state ss, we respond to the request for a |σ||\sigma|-element posed by the outermost operation symbol of σ(t)\sigma(t) by evaluating σ(s)\llbracket\sigma\rrbracket(s) to obtain i|σ|i\in|\sigma| along with a new state ss^{\prime}; substituting this ii into tt yields the simpler computation tit_{i} which we now run from state ss^{\prime}. When we hit a value aAa\in A we return it along with the final state reached.

Example 17.

Consider the theory of \mathbb{N}-valued input, and the term from (2.1), which we may equally write as t=𝗋𝖾𝖺𝖽(λn.𝗋𝖾𝖺𝖽(λm.n+m))T()t=\mathsf{read}(\lambda n.\,\mathsf{read}(\lambda m.\,n+m))\in T(\mathbb{N}). If 𝑺{\boldsymbol{S}} is the comodel with S={s,s,s′′}S=\{s,s^{\prime},s^{\prime\prime}\} and 𝗋𝖾𝖺𝖽:S×S\llbracket\mathsf{read}\rrbracket\colon S\rightarrow\mathbb{N}\times S given as to the left in:

s\displaystyle s (7,s)\displaystyle\mapsto(7,s^{\prime}) s\displaystyle s (18,s′′)\displaystyle\mapsto(18,s^{\prime\prime})
s\displaystyle s^{\prime} (11,s′′)\displaystyle\mapsto(11,s^{\prime\prime}) s\displaystyle s^{\prime} (24,s′′)\displaystyle\mapsto(24,s^{\prime\prime})
s′′\displaystyle s^{\prime\prime} (13,s′′)\displaystyle\mapsto(13,s^{\prime\prime}) s′′\displaystyle s^{\prime\prime} (26,s′′) ,\displaystyle\mapsto(26,s^{\prime\prime})\hbox to0.0pt{ ,\hss}

then t:S×S\llbracket t\rrbracket\colon S\rightarrow\mathbb{N}\times S is given as to the right. For example, for t(s)\llbracket t\rrbracket(s) we calculate that 𝗋𝖾𝖺𝖽(λn.𝗋𝖾𝖺𝖽(λm.n+m))(s)=𝗋𝖾𝖺𝖽(λm. 7+m)(s)=7+11(s′′)=(18,s′′)\llbracket\mathsf{read}(\lambda n.\,\mathsf{read}(\lambda m.\,n+m))\rrbracket(s)=\llbracket\mathsf{read}(\lambda m.\,7+m)\rrbracket(s^{\prime})=\llbracket 7+11\rrbracket(s^{\prime\prime})=(18,s^{\prime\prime}).

We conclude our discussion of models and comodels by describing the functoriality of the assignment 𝕋𝒞𝕋\mathbb{T}\mapsto{\mathcal{C}}^{\mathbb{T}}.

Definition 18 (Semantics and cosemantics).

For a category 𝒞{\mathcal{C}} with powers, the semantics functor 𝒮em𝒞:𝒜lg𝒯hop𝒞AT/𝒞\mathrm{\mathcal{S}em}_{\mathcal{C}}\colon\mathrm{\mathcal{A}lg{\mathcal{T}}h}^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}AT}/{\mathcal{C}} is given by 𝕋(U𝕋:𝒞𝕋𝒞)\mathbb{T}\mapsto(U^{\mathbb{T}}\colon{\mathcal{C}}^{\mathbb{T}}\rightarrow{\mathcal{C}}) on objects; while on maps, an interpretation f:𝕋1𝕋2f\colon\mathbb{T}_{1}\rightarrow\mathbb{T}_{2} is taken to the functor f:𝒞𝕋2𝒞𝕋1f^{\ast}\colon{\mathcal{C}}^{\mathbb{T}_{2}}\rightarrow{\mathcal{C}}^{\mathbb{T}_{1}} over 𝒞{\mathcal{C}} acting via (X,𝑿)(X,()f𝑿)(X,\llbracket{\mathord{\text{--}}}\rrbracket_{{\boldsymbol{X}}})\mapsto(X,\llbracket({\mathord{\text{--}}})^{f}\rrbracket_{{\boldsymbol{X}}}).

Dually, for any category 𝒞{\mathcal{C}} with copowers, we define the cosemantics functor Cosem𝒞:𝒜lg𝒯hop𝒞AT/𝒞\mathrm{Cosem}_{\mathcal{C}}\colon\mathrm{\mathcal{A}lg{\mathcal{T}}h}^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}AT}/{\mathcal{C}} by 𝕋(U𝕋:𝒞𝕋𝒞)\mathbb{T}\mapsto({}^{\mathbb{T}}U\colon{}^{\mathbb{T}}{\mathcal{C}}\rightarrow{\mathcal{C}}) on objects, and on morphisms in the same manner as above; more formally, we have Cosem𝒞=Sem𝒞op()op\mathrm{Cosem}_{\mathcal{C}}=\mathrm{Sem}_{{\mathcal{C}}^{\mathrm{op}}}({\mathord{\text{--}}})^{\mathrm{op}}.

The functoriality of (co)semantics implies that theories 𝕋\mathbb{T} and 𝕋\mathbb{T}^{\prime} which are isomorphic in 𝒜lg𝒯h\mathrm{\mathcal{A}lg{\mathcal{T}}h} have the same (co)models in any category with (co)powers 𝒞{\mathcal{C}}; we call such theories Morita equivalent.

Example 19.

Let h:VWh\colon V\rightarrow W be a function, and let f:𝕋1𝕋2f\colon\mathbb{T}_{1}\rightarrow\mathbb{T}_{2} be the interpretation of VV-valued output into WW-valued state of Example 8. For each comodel 𝑺=(S,g:SW,p:S×WS){\boldsymbol{S}}=(S,g\colon S\rightarrow W,p\colon S\times W\rightarrow S) of WW-valued state, the associated comodel f𝑺f^{\ast}{\boldsymbol{S}} of VV-valued output is (S,p(1×h):S×VS)(S,p\circ(1\times h)\colon S\times V\rightarrow S).

Example 20.

Let h:WVh\colon W\rightarrow V be a function, and let f:𝕋1𝕋2f\colon\mathbb{T}_{1}\rightarrow\mathbb{T}_{2} be the interpretation of VV-valued read-only state into WW-valued state of Example 9. For each comodel 𝑺=(S,g:SW,p:S×WS){\boldsymbol{S}}=(S,g\colon S\rightarrow W,p\colon S\times W\rightarrow S) of WW-valued state, the associated comodel f𝑺f^{\ast}{\boldsymbol{S}} of VV-valued read-only state is (S,hg:SV)(S,hg\colon S\rightarrow V).

2.3. The associated monad

Finally in this section, we recall how an algebraic theory gives rise to a monad on 𝒮et\mathrm{\mathcal{S}et}, and the manner in which this interacts with semantics. We specify our monads as Kleisli triples in the style of [22, Exercise 1.3.12].

Definition 21 (Associated monad).

The associated monad 𝖳\mathsf{T} of an algebraic theory 𝕋\mathbb{T} has action on objects AT(A)A\mapsto T(A); unit maps ηA:AT(A)\eta_{A}\colon A\rightarrow T(A) given by inclusion of variables; and Kleisli extension u:T(A)T(B)u^{\dagger}\colon T(A)\rightarrow T(B) of u:AT(B)u\colon A\rightarrow T(B) given by tt(u)t\mapsto t(u). The assignment 𝕋𝖳\mathbb{T}\mapsto\mathsf{T} is the action on objects of the associated monad functor Ass:𝒜lg𝒯hnd(𝒮et)\mathrm{Ass}\colon\mathrm{\mathcal{A}lg{\mathcal{T}}h}\rightarrow\mathrm{\mathcal{M}nd}(\mathrm{\mathcal{S}et}), which on morphisms takes an interpretation f:𝕋1𝕋2f\colon\mathbb{T}_{1}\rightarrow\mathbb{T}_{2} to the monad morphism 𝖳1𝖳2\mathsf{T}_{1}\rightarrow\mathsf{T}_{2} whose components T1(A)T2(A)T_{1}(A)\rightarrow T_{2}(A) are the assignments ttft\mapsto t^{f} defined as in (2.3).

Proposition 22.

The associated monad functor 𝒜lg𝒯hnd(𝒮et)\mathrm{\mathcal{A}lg{\mathcal{T}}h}\rightarrow\mathrm{\mathcal{M}nd}(\mathrm{\mathcal{S}et}) is full and faithful, and a monad 𝖳\mathsf{T} is in its essential image just when it is accessible.∎

Here, a monad on 𝒮et\mathrm{\mathcal{S}et} is accessible if its underlying endofunctor is accessible, in the sense of being a small colimit of representable functors. There are well-known monads on 𝒮et\mathrm{\mathcal{S}et} which are not accessible, for example the power-set monad \mathbb{P} and the continuation monad VV()V^{V^{({\mathord{\text{--}}})}}; nonetheless, we may treat any monad 𝖳\mathsf{T} on 𝒮et\mathrm{\mathcal{S}et} “as if it were induced by an algebraic theory” by adopting the following conventions: if aAa\in A, then we may write aT(A)a\in T(A) in place of ηA(a)T(A)\eta_{A}(a)\in T(A), and if tT(A)t\in T(A) and uT(B)Au\in T(B)^{A}, then we may write t(u)t(u) in place of u(t)u^{\dagger}(t).

We now discuss how the model and comodel semantics of an algebraic theory can be expressed in terms of the associated monad.

Definition 23 (𝖳\mathsf{T}-models and comodels).

Let 𝖳\mathsf{T} be a monad on 𝒮et\mathrm{\mathcal{S}et} and let 𝒞{\mathcal{C}} be a category with powers. A 𝖳\mathsf{T}-model 𝐗{\boldsymbol{X}} in 𝒞{\mathcal{C}} is an object X𝒞X\in{\mathcal{C}} together with operations t𝑿:XAX\llbracket t\rrbracket_{{\boldsymbol{X}}}\colon X^{A}\rightarrow X for every set AA and tT(A)t\in T(A), subject to the axioms

(2.7) a𝑿=πaandt(u)𝑿=XB\ext@arrow01200\rightarrowfill@(ua𝑿)aAXA\ext@arrow01200\rightarrowfill@t𝑿X .\llbracket a\rrbracket_{{\boldsymbol{X}}}=\pi_{a}\qquad\text{and}\qquad\smash{\llbracket t(u)\rrbracket_{{\boldsymbol{X}}}=X^{B}\ext@arrow 01{20}0\rightarrowfill@{}{(\llbracket u_{a}\rrbracket_{{\boldsymbol{X}}})_{a\in A}}X^{A}\ext@arrow 01{20}0\rightarrowfill@{}{\llbracket t\rrbracket_{{\boldsymbol{X}}}}X}\hbox to0.0pt{ .\hss}

for all aAa\in A and all tT(A)t\in T(A) and uT(B)Au\in T(B)^{A}. We write 𝒞𝖳{\mathcal{C}}^{\mathsf{T}} for the category of 𝖳\mathsf{T}-models in 𝒞{\mathcal{C}}, whose maps 𝑿𝒀{\boldsymbol{X}}\rightarrow{\boldsymbol{Y}} are 𝒞{\mathcal{C}}-maps f:XYf\colon X\rightarrow Y with t𝒀fA=ft𝑿\llbracket t\rrbracket_{{\boldsymbol{Y}}}\circ f^{A}=f\circ\llbracket t\rrbracket_{{\boldsymbol{X}}} for all sets AA and all tT(A)t\in T(A); we write U𝖳:𝒞𝖳𝒞U^{\mathsf{T}}\colon{\mathcal{C}}^{\mathsf{T}}\rightarrow{\mathcal{C}} for the forgetful functor.

If 𝒞{\mathcal{C}} is a category with copowers then a 𝖳\mathsf{T}-comodel in 𝒞{\mathcal{C}} is a 𝖳\mathsf{T}-model in 𝒞op{\mathcal{C}}^{\mathrm{op}}, involving co-operations t𝑿:XAX\llbracket t\rrbracket_{{\boldsymbol{X}}}\colon X\rightarrow A\cdot X subject to the dual axioms

(2.8) a𝑿=νaandt(u)𝑿=X\ext@arrow01200\rightarrowfill@t𝑿AX\ext@arrow01200\rightarrowfill@ua𝑿aABX .\llbracket a\rrbracket_{{\boldsymbol{X}}}=\nu_{a}\qquad\text{and}\qquad\smash{\llbracket t(u)\rrbracket_{{\boldsymbol{X}}}=X\ext@arrow 01{20}0\rightarrowfill@{}{\llbracket t\rrbracket_{{\boldsymbol{X}}}}A\cdot X\ext@arrow 01{20}0\rightarrowfill@{}{\langle\llbracket u_{a}\rrbracket_{{\boldsymbol{X}}}\rangle_{a\in A}}B\cdot X}\hbox to0.0pt{ .\hss}

We write U𝖳:𝒞𝖳𝒞{}^{\mathsf{T}}U\colon{}^{\mathsf{T}}{\mathcal{C}}\rightarrow{\mathcal{C}} for the forgetful functor from the category of 𝖳\mathsf{T}-comodels.

Definition 24 (Semantics and cosemantics).

For any category 𝒞{\mathcal{C}} with powers, the semantics functor Sem𝒞:nd(𝒮et)op𝒞AT/𝒞\mathrm{Sem}_{\mathcal{C}}\colon\mathrm{\mathcal{M}nd}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}AT}/{\mathcal{C}} is given by 𝖳(U𝖳:𝒞𝖳𝒞)\mathsf{T}\mapsto(U^{\mathsf{T}}\colon{\mathcal{C}}^{\mathsf{T}}\rightarrow{\mathcal{C}}) on objects; while a monad morphism f:𝖳1𝖳2f\colon\mathsf{T}_{1}\rightarrow\mathsf{T}_{2} is taken to the functor f:𝒞𝖳2𝒞𝖳1f^{\ast}\colon{\mathcal{C}}^{\mathsf{T}_{2}}\rightarrow{\mathcal{C}}^{\mathsf{T}_{1}} over 𝒞{\mathcal{C}} acting via (X,𝑿)(X,f()𝑿)(X,\llbracket{\mathord{\text{--}}}\rrbracket_{{\boldsymbol{X}}})\mapsto(X,\llbracket f({\mathord{\text{--}}})\rrbracket_{{\boldsymbol{X}}}). For a category 𝒞{\mathcal{C}} with copowers, we define the cosemantics functor by Cosem𝒞:=Sem𝒞op()op:nd(𝒮et)op𝒞AT/𝒞\mathrm{Cosem}_{\mathcal{C}}\mathrel{\mathop{:}}=\mathrm{Sem}_{{\mathcal{C}}^{\mathrm{op}}}({\mathord{\text{--}}})^{\mathrm{op}}\colon\mathrm{\mathcal{M}nd}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}AT}/{\mathcal{C}}.

The following result, which again is entirely standard, tells us that we lose no semantic information in passing from an algebraic theory to the associated monad.

Proposition 25.

For any category 𝒞{\mathcal{C}} with powers (respectively, copowers), the triangle to the left (respectively, right) below commutes to within natural isomorphism:

𝒜lg𝒯hop\textstyle{{\mathrm{\mathcal{A}lg{\mathcal{T}}h}^{\mathrm{op}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Assop\scriptstyle{\mathrm{Ass}^{\mathrm{op}}}Sem𝒞\scriptstyle{\mathrm{Sem}_{\mathcal{C}}}nd(𝒮et)op\textstyle{{\mathrm{\mathcal{M}nd}(\mathrm{\mathcal{S}et})^{\mathrm{op}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Sem𝒞\scriptstyle{\mathrm{Sem}_{\mathcal{C}}}𝒞AT/𝒞\textstyle{{\mathrm{\mathcal{C}AT}/{\mathcal{C}}}}
      
𝒜lg𝒯hop\textstyle{{\mathrm{\mathcal{A}lg{\mathcal{T}}h}^{\mathrm{op}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Assop\scriptstyle{\mathrm{Ass}^{\mathrm{op}}}Cosem𝒞\scriptstyle{\mathrm{Cosem}_{\mathcal{C}}}nd(𝒮et)op .\textstyle{{\mathrm{\mathcal{M}nd}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\hbox to0.0pt{ .\hss}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Cosem𝒞\scriptstyle{\mathrm{Cosem}_{\mathcal{C}}}𝒞AT/𝒞\textstyle{{\mathrm{\mathcal{C}AT}/{\mathcal{C}}}}

In light of this result, we will henceforth prefer to deal with accessible monads, though noting as we go along any simplifications afforded by having available a presentation via an algebraic theory.

3. Presheaf monads and comonads

3.1. Presheaf monads and comonads

In this section, we describe and study the presheaf monads and comonads which will be crucial to our main results. This is largely revision from the literature, though Propositions 33 and 36 are novel.

Definition 26 (Presheaf monad and comonad).

Let 𝔹\mathbb{B} be a small category. The presheaf monad 𝖳𝔹nda(𝒮et)\mathsf{T}_{\mathbb{B}}\in\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et}) and the presheaf comonad 𝖰𝔹𝒞mda(𝒮et)\mathsf{Q}_{\mathbb{B}}\in\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}) are the monad and comonad induced by the respective adjunctions:

(3.1) 𝒮et𝔹op\textstyle{{\mathrm{\mathcal{S}et}^{\mathbb{B}^{\mathrm{op}}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}𝗋𝖾𝗌Jop\scriptstyle{\mathsf{res}_{J^{\mathrm{op}}}}𝗅𝖺𝗇Jop\scriptstyle{\mathsf{lan}_{J^{\mathrm{op}}}}\scriptstyle{\bot}𝒮etob(𝔹)\textstyle{{{\mathrm{\mathcal{S}et}^{\mathrm{ob}(\mathbb{B})}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Π\scriptstyle{\Pi}Δ\scriptstyle{\Delta}\scriptstyle{\bot}𝒮et\textstyle{{\mathrm{\mathcal{S}et}}}𝒮et𝔹\textstyle{{\mathrm{\mathcal{S}et}^{\mathbb{B}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}𝗋𝖾𝗌J\scriptstyle{\mathsf{res}_{J}}𝗋𝖺𝗇J\scriptstyle{\mathsf{ran}_{J}}\scriptstyle{\top}𝒮etob(𝔹)\textstyle{{{\mathrm{\mathcal{S}et}^{\mathrm{ob}(\mathbb{B})}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Σ\scriptstyle{\Sigma}Δ\scriptstyle{\Delta}\scriptstyle{\top}𝒮et ,\textstyle{{\mathrm{\mathcal{S}et}}\hbox to0.0pt{ ,\hss}}

where J:ob(𝔹)𝔹J\colon\mathrm{ob}(\mathbb{B})\rightarrow\mathbb{B} is the inclusion of objects, and where res\mathrm{res}, lan\mathrm{lan} and ran\mathrm{ran} denote restriction, left Kan extension and right Kan extension. If we write 𝔹b\mathbb{B}_{b} for the set of all 𝔹\mathbb{B}-maps with domain bb, then the underlying endofunctors are given by:

T𝔹(A)=b𝔹𝔹b×AandQ𝔹(A)=b𝔹A𝔹b ;T_{\mathbb{B}}(A)=\textstyle\prod_{b\in\mathbb{B}}\mathbb{B}_{b}\times A\qquad\text{and}\qquad Q_{\mathbb{B}}(A)=\sum_{b\in\mathbb{B}}A^{\mathbb{B}_{b}}\hbox to0.0pt{ ;\hss}

the unit and multiplication for 𝖳𝔹\mathsf{T}_{\mathbb{B}} are given by:

ηA:A\displaystyle\eta_{A}\colon A b(𝔹b×A)\displaystyle\rightarrow\textstyle\prod_{b}(\mathbb{B}_{b}\times A) μA:b(𝔹b×b(𝔹b×A))\displaystyle\mu_{A}\colon\textstyle\prod_{b}(\mathbb{B}_{b}\times\prod_{b^{\prime}}(\mathbb{B}_{b^{\prime}}\times A)) b(𝔹b×A)\displaystyle\rightarrow\textstyle\prod_{b}(\mathbb{B}_{b}\times A)
a\displaystyle a λb.(1b,a)\displaystyle\mapsto\lambda b.\,(1_{b},a) λb.(fb,λb.(gbb,abb))\displaystyle\lambda b.\,(f_{b},\lambda b^{\prime}.\,(g_{bb^{\prime}},a_{bb^{\prime}})) λb.(gb,cod(fb)fb,ab,cod(fb)) .\displaystyle\mapsto\lambda b.\,(g_{b,\mathrm{cod}(f_{b})}\circ f_{b},a_{b,\mathrm{cod}(f_{b})})\text{ .}

while the counit and comultiplication for 𝖰𝔹\mathsf{Q}_{\mathbb{B}} are given by:

(3.2) εA:bA𝔹b\displaystyle\varepsilon_{A}\colon\textstyle\sum_{b}A^{\mathbb{B}_{b}} A\displaystyle\rightarrow A δA:bA𝔹b\displaystyle\quad\delta_{A}\colon\textstyle\sum_{b}A^{\mathbb{B}_{b}} b(bA𝔹b)𝔹b\displaystyle\rightarrow\textstyle\sum_{b}\bigl{(}\sum_{b^{\prime}}A^{\mathbb{B}_{b^{\prime}}}\bigr{)}^{\mathbb{B}_{b}}
(b,φ)\displaystyle(b,\varphi) φ(1b)\displaystyle\mapsto\varphi(1_{b}) (b,φ)\displaystyle(b,\varphi) (b,λf.(cod(f),λg.φ(gf))) .\displaystyle\mapsto\bigl{(}b,\lambda f.\,(\mathrm{cod}(f),\lambda g.\,\varphi(gf))\bigr{)}\hbox to0.0pt{ .\hss}

We call a general 𝖳nda(𝒮et)\mathsf{T}\in\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et}) a presheaf monad if it is isomorphic to some 𝖳𝔹\mathsf{T}_{\mathbb{B}}, and correspondingly on the comonad side.

Presheaf monads and comonads have been considered in computer science; in [7] they are termed “dependently typed update monads” and “dependently typed coupdate comonads” respectively, but both have a longer history, as we now recall.

To the comonad side, we note that the underlying endofunctor of a presheaf comonad is polynomial, i.e., a coproduct of representable functors. Such endofunctors are exactly those which arise as the interpretations of set-based containers [1], and in [4], this was enhanced to a characterisation of polynomial comonads as the interpretations of so-called directed containers. Now, as observed in [7], directed containers correspond bijectively to small categories, and so we conclude that the presheaf comonads on 𝒮et\mathrm{\mathcal{S}et} are precisely the polynomial comonads. For self-containedness, we include a short direct proof of this fact.

Proposition 27.

For a comonad 𝖰\mathsf{Q} on 𝒮et\mathrm{\mathcal{S}et}, the following conditions are equivalent:

  1. (i)

    𝖰\mathsf{Q} is a presheaf comonad;

  2. (ii)

    The underlying endofunctor QQ is a coproduct of representables;

  3. (iii)

    The underlying endofunctor QQ preserves connected limits.

Proof.

Clearly (i) \Rightarrow (ii), and (ii) \Leftrightarrow (iii) is standard category theory due to Diers [9]; so it remains to show (ii) \Rightarrow (i). Suppose, then, that Q=bB()EbQ=\sum_{b\in B}({\mathord{\text{--}}})^{E_{b}} is a coproduct of representables. By the Yoneda lemma, giving ε:Q1\varepsilon\colon Q\Rightarrow 1 is equivalent to giving the elements 1b:=εEb(b,λf.f)Eb1_{b}\mathrel{\mathop{:}}=\varepsilon_{E_{b}}(b,\lambda f.\,f)\in E_{b} for each bBb\in B. Similarly, giving δ:QQQ\delta\colon Q\Rightarrow QQ is equivalent to giving for each bBb\in B an element of QQ(Eb)QQ(E_{b}), i.e., elements α(b)B\alpha(b)\in B and λf.(c(f),ρf):Eα(b)bEbEb\lambda f.\,(c(f),\rho_{f})\colon E_{\alpha(b)}\rightarrow\sum_{b^{\prime}}E_{b}^{E_{b^{\prime}}}. Now the three comonad axioms correspond under the Yoneda lemma to the following assertions:

  • The axiom εQδ=1Q\varepsilon Q\circ\delta=1_{Q} asserts that α(b)=b\alpha(b)=b and ρf(1c(f))=f\rho_{f}(1_{c(f)})=f;

  • The axiom Qεδ=1QQ\varepsilon\circ\delta=1_{Q} asserts that c(1b)=bc(1_{b})=b and ρ1b=idEb\rho_{1_{b}}=\mathrm{id}_{E_{b}};

  • The axiom Qδδ=δQδQ\delta\circ\delta=\delta Q\circ\delta asserts that c(g)=c(ρf(g))c(g)=c(\rho_{f}(g)) and ρfρg=ρρf(g)\rho_{f}\circ\rho_{g}=\rho_{\rho_{f}(g)}.

But these are precisely the data and axioms of a small category 𝔹\mathbb{B} with object-set BB, with 𝔹b=Eb\mathbb{B}_{b}=E_{b}, with identities 1b1_{b}, with codomain map cc, and with precomposition by ff given by ρf\rho_{f}; and on defining 𝔹\mathbb{B} in this way, we clearly have 𝖰=𝖰𝔹\mathsf{Q}=\mathsf{Q}_{\mathbb{B}}. ∎

To the monad side, presheaf monads seem to have been first considered in [16, Example 8.7], in terms of a presentation as an algebraic theory. We now recall this presentation, though framing it in terms of the applications of [6].

Notation 28.

Let Σ\Sigma be a signature, and qΣ(A)q\in\Sigma(A) and t,uΣ(B)t,u\in\Sigma(B) terms where without loss of generality AA is disjoint from BB. For any iAi\in A, we write tq,iut\equiv_{q,i}u (read as “tt and uu are equal in the iith place of qq”) as an abbreviation for the equation

q(λa.{t(b)if a=iaif ai)q(λa.{u(b)if a=iaif ai)in Σ(AB{i}) .q\Bigl{(}\lambda a.\,\begin{cases}t(\vec{b})&\text{if $a=i$}\\ a&\text{if $a\neq i$}\end{cases}\Bigr{)}\equiv q\Bigl{(}\lambda a.\,\begin{cases}u(\vec{b})&\text{if $a=i$}\\ a&\text{if $a\neq i$}\end{cases}\Bigr{)}\ \qquad\text{in $\Sigma(A\cup B\setminus\{i\})$}\hbox to0.0pt{ .\hss}
Example 29 (Dependently typed update).

Let 𝔹\mathbb{B} be a small category, whose objects we view as values, and whose arrows bbb\rightarrow b^{\prime} we view as updates from bb to bb^{\prime}. The theory of 𝔹\mathbb{B}-valued dependently typed update is generated by an ob(𝔹)\mathrm{ob}(\mathbb{B})-ary operation 𝗀𝖾𝗍\mathsf{get} satifying the axioms of read-only state, together with a unary operation 𝗎𝗉𝖽f\mathsf{upd}_{f} for each morphism f:bbf\colon b\rightarrow b^{\prime} in 𝔹\mathbb{B}, subject to the equations

(3.3) 𝗎𝗉𝖽f(x)\displaystyle\mathsf{upd}_{f}(x) 𝗀𝖾𝗍,cx\displaystyle\equiv_{\mathsf{get},c}x for f:bbf\colon b\rightarrow b^{\prime} and cbc\neq b in 𝔹\mathbb{B};
(3.4) 𝗎𝗉𝖽f(𝗀𝖾𝗍(λa.xa))\displaystyle\mathsf{upd}_{f}(\mathsf{get}(\lambda a.\,x_{a})) 𝗀𝖾𝗍,b𝗎𝗉𝖽f(xb)\displaystyle\equiv_{\mathsf{get},b}\mathsf{upd}_{f}(x_{b^{\prime}}) for f:bbf\colon b\rightarrow b^{\prime} in 𝔹\mathbb{B};
(3.5) 𝗎𝗉𝖽1b(x)\displaystyle\mathsf{upd}_{1_{b}}(x) x\displaystyle\equiv x for bob(𝔹)b\in\mathrm{ob}(\mathbb{B});
(3.6) 𝗎𝗉𝖽f(𝗎𝗉𝖽g(x))\displaystyle\mathsf{upd}_{f}(\mathsf{upd}_{g}(x)) 𝗀𝖾𝗍,b𝗎𝗉𝖽gf(x)\displaystyle\equiv_{\mathsf{get},b}\mathsf{upd}_{gf}(x) for f:bbf\colon b\rightarrow b^{\prime}, g:bb′′g\colon b^{\prime}\rightarrow b^{\prime\prime} in 𝔹\mathbb{B}.

The intended semantics is that 𝗀𝖾𝗍\mathsf{get} reads a value associated to the current state; while 𝗎𝗉𝖽f\mathsf{upd}_{f}, for f:bbf\colon b\rightarrow b^{\prime} in 𝔹\mathbb{B}, attempts to update the value bb to bb^{\prime} via ff. If the value of the current state is not bb, then the update fails (the first axiom above); while if the value is bb, then we move to a new state with associated value bb^{\prime} (the second axiom). The remaining axioms assert that updates compose as expected.

We now justify our nomenclature by showing that the theory of 𝔹\mathbb{B}-valued dependently typed update generates the presheaf monad 𝖳𝔹\mathsf{T}_{\mathbb{B}}, which as we have explained, is equally an example of a dependently typed update monad as in [6].

Proposition 30.

For any small category 𝔹\mathbb{B}, the theory of 𝔹\mathbb{B}-valued dependently typed update generates the presheaf monad 𝖳𝔹\mathsf{T}_{\mathbb{B}}.

Proof.

For each set AA, we make T𝔹(A)=b(𝔹b×A)T_{\mathbb{B}}(A)=\prod_{b}(\mathbb{B}_{b}\times A) a model of the theory 𝕋\mathbb{T} of dependently-typed update by taking 𝗀𝖾𝗍(λb.(λc.gbc,λc.abc))=λb.(gbb,abb)\llbracket\mathsf{get}\rrbracket\bigl{(}\lambda b.(\lambda c.\,g_{bc},\lambda c.\,a_{bc})\bigr{)}=\lambda b.\,(g_{bb},a_{bb}) and

𝗎𝗉𝖽f(λc.(gc,ac))=λc.{(gc,ac)if cb;(gbf,ab)if c=bfor f:bb in 𝔹.\llbracket\mathsf{upd}_{f}\rrbracket(\lambda c.\,(g_{c},a_{c}))=\lambda c.\,\begin{cases}(g_{c},a_{c})&\text{if $c\neq b$;}\\ (g_{b^{\prime}}f,a_{b^{\prime}})&\text{if $c=b$}\end{cases}\qquad\text{for $f\colon b\rightarrow b^{\prime}$ in $\mathbb{B}$.}

It is easy to verify that an equation t𝗀𝖾𝗍,but\equiv_{\mathsf{get},b}u holds in T𝔹(A)T_{\mathbb{B}}(A) precisely when the interpretations t\llbracket t\rrbracket and u\llbracket u\rrbracket have the same postcomposition with the projection πb:b(𝔹b×A)𝔹b×A\pi_{b}\colon\prod_{b}(\mathbb{B}_{b}\times A)\rightarrow\mathbb{B}_{b}\times A; whence the axioms for dependently typed update are satisfied. Thus we may extend ηA:Ab(𝔹b×A)\eta_{A}\colon A\rightarrow\prod_{b}(\mathbb{B}_{b}\times A) uniquely to a homomorphism pA:T(A)b(𝔹b×A)p_{A}\colon T(A)\rightarrow\prod_{b}(\mathbb{B}_{b}\times A); we also have a function iA:b(𝔹b×A)T(A)i_{A}\colon\prod_{b}(\mathbb{B}_{b}\times A)\rightarrow T(A) given by λc.(gc,ac)𝗀𝖾𝗍(λc.𝗎𝗉𝖽gc(ac))\lambda c.\,(g_{c},a_{c})\mapsto\mathsf{get}(\lambda c.\,\mathsf{upd}_{g_{c}}(a_{c})) which we claim is also a model homomorphism. It commutes with 𝗀𝖾𝗍\mathsf{get} since 𝗀𝖾𝗍(λb.𝗀𝖾𝗍(λc.𝗎𝗉𝖽gbc(abc)))=𝗀𝖾𝗍(λc.𝗎𝗉𝖽gcc(acc))\mathsf{get}(\lambda b.\,\mathsf{get}(\lambda c.\,\mathsf{upd}_{g_{bc}}(a_{bc})))=\mathsf{get}(\lambda c.\,\mathsf{upd}_{g_{cc}}(a_{cc})). To see it commutes with 𝗎𝗉𝖽f\mathsf{upd}_{f} for f:bbf\colon b\rightarrow b^{\prime}, we observe that

𝗎𝗉𝖽f(𝗀𝖾𝗍(λc.𝗎𝗉𝖽gc(ac))\displaystyle\mathsf{upd}_{f}(\mathsf{get}(\lambda c.\,\mathsf{upd}_{g_{c}}(a_{c})) 𝗀𝖾𝗍,b𝗎𝗉𝖽f(𝗎𝗉𝖽gb(ab))𝗀𝖾𝗍,b𝗎𝗉𝖽gbf(ab)\displaystyle\equiv_{\mathsf{get},b}\mathsf{upd}_{f}(\mathsf{upd}_{g_{b^{\prime}}}(a_{b^{\prime}}))\equiv_{\mathsf{get},b}\mathsf{upd}_{g_{b}^{\prime}f}(a_{b^{\prime}})
and 𝗎𝗉𝖽f(𝗀𝖾𝗍(λc.𝗎𝗉𝖽gc(ac))\displaystyle\text{and }\mathsf{upd}_{f}(\mathsf{get}(\lambda c.\,\mathsf{upd}_{g_{c}}(a_{c})) 𝗀𝖾𝗍,c𝗀𝖾𝗍(λc.𝗎𝗉𝖽gc(ac))𝗀𝖾𝗍,c𝗎𝗉𝖽gc(ac) for cb,\displaystyle\equiv_{\mathsf{get},c}\mathsf{get}(\lambda c.\,\mathsf{upd}_{g_{c}}(a_{c}))\equiv_{\mathsf{get},c}\mathsf{upd}_{g_{c}}(a_{c})\text{ for $c\neq b$,}

from which it follows that

𝗎𝗉𝖽f(𝗀𝖾𝗍(λc.𝗎𝗉𝖽gc(ac))=𝗀𝖾𝗍(λc.{𝗎𝗉𝖽gc(ac)if cb;𝗎𝗉𝖽gbf(ab)if c=b)\mathsf{upd}_{f}(\mathsf{get}(\lambda c.\,\mathsf{upd}_{g_{c}}(a_{c}))=\mathsf{get}\Bigl{(}\lambda c.\,\begin{cases}\mathsf{upd}_{g_{c}}(a_{c})&\text{if $c\neq b$;}\\ \mathsf{upd}_{g_{b^{\prime}}f}(a_{b^{\prime}})&\text{if $c=b$}\end{cases}\Bigr{)}

as desired. Since 𝗀𝖾𝗍(λb.𝗎𝗉𝖽1b(a))𝗎𝗉𝖽1b(a)a\mathsf{get}(\lambda b.\,\mathsf{upd}_{1_{b}}(a))\equiv\mathsf{upd}_{1_{b}}(a)\equiv a, we have iA(ηA(a))=ai_{A}(\eta_{A}(a))=a, from which it follows that iApA=1T(A)i_{A}p_{A}=1_{T(A)}. On the other hand, pAiA=1p_{A}i_{A}=1 by a short calculation, and so pAp_{A} and iAi_{A} are mutually inverse. In this way, we obtain a natural isomorphism TT𝔹T\cong T_{\mathbb{B}}, which by construction is compatible with the units of the monads 𝖳\mathsf{T} and 𝖳𝔹\mathsf{T}_{\mathbb{B}}. Compatibility with the multiplications follows since:

𝗀𝖾𝗍(λb.𝗎𝗉𝖽fb(𝗀𝖾𝗍(λc.𝗎𝗉𝖽gbc(abc))))\displaystyle\mathsf{get}(\lambda b.\,\mathsf{upd}_{f_{b}}(\mathsf{get}(\lambda c.\,\mathsf{upd}_{g_{bc}}(a_{bc})))) 𝕋𝗀𝖾𝗍(λb.𝗎𝗉𝖽fb(𝗎𝗉𝖽gb,𝖼(fb)(ab,𝖼(fb))))\displaystyle\equiv_{\mathbb{T}}\mathsf{get}(\lambda b.\,\mathsf{upd}_{f_{b}}(\mathsf{upd}_{g_{b,\mathsf{c}(f_{b})}}(a_{b,\mathsf{c}(f_{b})})))
𝕋𝗀𝖾𝗍(λb.𝗎𝗉𝖽gb,𝖼(fb)fb(ab,𝖼(fb))) .\displaystyle\equiv_{\mathbb{T}}\mathsf{get}(\lambda b.\,\mathsf{upd}_{g_{b,\mathsf{c}(f_{b})}\circ f_{b}}(a_{b,\mathsf{c}(f_{b})}))\hbox to0.0pt{ .\hss}\qed

3.2. Morphisms of presheaf monads and comonads

We now examine morphisms between presheaf monads and comonads. Beginning again on the comonad side, we observe, as in [7], that comonad morphisms between presheaf comonads do not correspond to functors, but rather to cofunctors:

Definition 31 (Cofunctor).

[15, 2] A cofunctor F:𝔹F\colon\mathbb{B}\rightsquigarrow\mathbb{C} between small categories comprises an action on objects F:ob(𝔹)ob()F\colon\mathrm{ob}(\mathbb{B})\rightarrow\mathrm{ob}(\mathbb{C}) together with actions on morphisms Fb:Fb𝔹bF_{b}\colon\mathbb{C}_{Fb}\rightarrow\mathbb{B}_{b} for each b𝔹b\in\mathbb{B}, subject to the axioms:

  1. (i)

    F(cod(Fb(f)))=cod(f)F(\mathrm{cod}(F_{b}(f)))=\mathrm{cod}(f) for all fFbf\in\mathbb{C}_{Fb};

  2. (ii)

    Fb(1Fb)=1bF_{b}(1_{Fb})=1_{b} for all b𝔹b\in\mathbb{B};

  3. (iii)

    Fb(gf)=Fcod(Fbf)(g)Fb(f)F_{b}(gf)=F_{\mathrm{cod}(F_{b}f)}(g)\circ F_{b}(f) for all fFbf\in\mathbb{C}_{Fb} and gcod(f)g\in\mathbb{C}_{\mathrm{cod}(f)}.

We write 𝒞of\mathrm{\mathcal{C}of} for the category of small categories and cofunctors.

Proposition 32.

Taking presheaf comonads is the action on objects of a fully faithful functor 𝖰():𝒞of𝒞mda(𝒮et)\mathsf{Q}_{({\mathord{\text{--}}})}\colon\mathrm{\mathcal{C}of}\rightarrow\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}), which on morphisms sends a cofunctor F:𝔹F\colon\mathbb{B}\rightsquigarrow\mathbb{C} to the comonad morphism 𝖰F:𝖰𝔹𝖰\mathsf{Q}_{F}\colon\mathsf{Q}_{\mathbb{B}}\rightarrow\mathsf{Q}_{\mathbb{C}} with components

(3.7) b𝔹A𝔹b\displaystyle\textstyle\sum_{b\in\mathbb{B}}A^{\mathbb{B}_{b}} cAc\displaystyle\rightarrow\textstyle\sum_{c\in\mathbb{C}}A^{\mathbb{C}_{c}}
(b,φ)\displaystyle(b,\varphi) (Fb,φFb) .\displaystyle\mapsto(Fb,\varphi\circ F_{b})\hbox to0.0pt{ .\hss}

This result is again due to [7], but we sketch a proof for self-containedness.

Proof.

Let 𝔹\mathbb{B} and \mathbb{C} be small categories. As Q𝔹=bB()𝔹bQ_{\mathbb{B}}=\sum_{b\in B}({\mathord{\text{--}}})^{\mathbb{B}_{b}}, we see once again by the Yoneda lemma that giving a natural transformation α:Q𝔹Q\alpha\colon Q_{\mathbb{B}}\Rightarrow Q_{\mathbb{C}} is equivalent to giving elements α𝔹b(b,1b)Q(𝔹b)\alpha_{\mathbb{B}_{b}}(b,1_{b})\in Q_{\mathbb{C}}(\mathbb{B}_{b}); and if we write these elements as pairs (Fb,Fb:Fb𝔹b)(Fb\in\mathbb{C},F_{b}\colon\mathbb{C}_{Fb}\rightarrow\mathbb{B}_{b}), then α\alpha itself must have components given as in (3.7). Similar arguments to the proof of Proposition 27 now show that α\alpha commutes with the comonad counits and comultiplication precisely when the assignments b(Fb,Fb)b\mapsto(Fb,F_{b}) satisfy the axioms (i)–(iii) for a cofunctor. ∎

On the monad side, it turns out that monad morphisms between presheaf monads are also cofunctors between the corresponding categories. This is not quite as straightforward to see, and for the moment we record only the weaker statement that cofunctors induce morphisms of presheaf monads; the full claim will be proved in Proposition 87 below.

Proposition 33.

Taking presheaf monads is the action on objects of a functor 𝖳():𝒞ofnda(𝒮et)op\mathsf{T}_{({\mathord{\text{--}}})}\colon\mathrm{\mathcal{C}of}\rightarrow\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}, which on morphisms sends a cofunctor F:𝔹F\colon\mathbb{B}\rightsquigarrow\mathbb{C} to the monad morphism 𝖳F:𝖳𝖳𝔹\mathsf{T}_{F}\colon\mathsf{T}_{\mathbb{C}}\rightarrow\mathsf{T}_{\mathbb{B}} with components

(3.8) c(c×A)\displaystyle\textstyle\prod_{c\in\mathbb{C}}(\mathbb{C}_{c}\times A) b𝔹(𝔹b×A)\displaystyle\rightarrow\textstyle\prod_{b\in\mathbb{B}}(\mathbb{B}_{b}\times A)
λc.(fc,ac)\displaystyle\lambda c.\,(f_{c},a_{c}) λb.(Fb(fFb),aFb) .\displaystyle\mapsto\lambda b.\,(F_{b}(f_{Fb}),a_{Fb})\hbox to0.0pt{ .\hss}
Proof.

It is easy to check that the components (3.8) are compatible with the units and multiplications of the presheaf monads 𝖳𝔹\mathsf{T}_{\mathbb{B}} and 𝖳\mathsf{T}_{\mathbb{C}}. ∎

3.3. Semantics

We now consider the semantics associated to presheaf comonads and monads. Starting again on the comonad side, it turns out that the adjunction (3.1) inducing the presheaf comonad 𝖰𝔹\mathsf{Q}_{\mathbb{B}} is comonadic, but not strictly so; thus, 𝖰𝔹\mathsf{Q}_{\mathbb{B}}-coalgebras are not exactly presheaves 𝔹𝒮et\mathbb{B}\rightarrow\mathrm{\mathcal{S}et}, but only something equivalent:

Definition 34 (Left 𝔹\mathbb{B}-set).

Let 𝔹\mathbb{B} be a small category. A left 𝔹\mathbb{B}-set is a set XX endowed with a projection map p:Xob(𝔹)p\colon X\rightarrow\mathrm{ob}(\mathbb{B}) and an action :xX𝔹p(x)X\mathord{\ast}\colon\textstyle\sum_{x\in X}\mathbb{B}_{p(x)}\rightarrow X, notated as (x,f)fx(x,f)\mapsto f\ast x, satisfying the typing axiom p(fx)=cod(f)p(f\ast x)=\mathrm{cod}(f) and the functoriality axioms idx=x\mathrm{id}\ast x=x and g(fx)=(gf)xg\ast(f\ast x)=(g\circ f)\ast x. We write 𝔹-𝒮et\mathbb{B}\text{-}\mathrm{\mathcal{S}et} for the category of left 𝔹\mathbb{B}-sets, whose maps are functions commuting with the projections and actions. We write U𝔹:𝔹-𝒮et𝒮etU^{\mathbb{B}}\colon\mathbb{B}\text{-}\mathrm{\mathcal{S}et}\rightarrow\mathrm{\mathcal{S}et} for the forgetful functor (X,p,)X(X,p,\ast)\mapsto X.

Given a cofunctor F:𝔹F\colon\mathbb{B}\rightsquigarrow\mathbb{C} between small categories, we define the functor ΣF:𝔹-𝒮et-𝒮et\Sigma_{F}\colon\mathbb{B}\text{-}\mathrm{\mathcal{S}et}\rightarrow\mathbb{C}\text{-}\mathrm{\mathcal{S}et} over 𝒮et\mathrm{\mathcal{S}et} to act as follows, where we write fFx:=Fp(x)(f)xf\ast^{F}x\mathrel{\mathop{:}}=F_{p(x)}(f)\ast x:

(X\ext@arrow01200\rightarrowfill@pob(𝔹),ΣxX𝔹p(x)\ext@arrow01200\rightarrowfill@X)(X\ext@arrow01200\rightarrowfill@Fpob(),ΣxXF(p(x))\ext@arrow01200\rightarrowfill@FX) .(X\ext@arrow 01{20}0\rightarrowfill@{}{p}\mathrm{ob}(\mathbb{B}),\,\Sigma_{x\in X}\mathbb{B}_{p(x)}\ext@arrow 01{20}0\rightarrowfill@{}{\smash{\ast}}X)\ \mapsto\ (X\ext@arrow 01{20}0\rightarrowfill@{}{\smash{Fp}}\mathrm{ob}(\mathbb{C}),\Sigma_{x\in X}\mathbb{C}_{F(p(x))}\ext@arrow 01{20}0\rightarrowfill@{}{\smash{\ast^{F}}}X)\hbox to0.0pt{ .\hss}

In [5], what we call a left 𝔹\mathbb{B}-set was termed a coalgebraic update lens; the following result, which is immediate from the definitions, was also observed there.

Proposition 35.

For any small category 𝔹\mathbb{B}, the category of Eilenberg–Moore 𝖰𝔹\mathsf{Q}_{\mathbb{B}}-coalgebras is isomorphic to 𝔹-𝒮et\mathbb{B}\text{-}\mathrm{\mathcal{S}et} via an isomorphism commuting with the functors to 𝒮et\mathrm{\mathcal{S}et}. These isomorphisms are natural with respect to cofunctors F:𝔹F\colon\mathbb{B}\rightsquigarrow\mathbb{C}.

Turning now to the presheaf monad 𝖳𝔹\mathsf{T}_{\mathbb{B}}, it follows from the results of [16] that the category of 𝖳𝔹\mathsf{T}_{\mathbb{B}}-models is equivalent to the category of presheaves X:𝔹op𝒮etX\colon\mathbb{B}^{\mathrm{op}}\rightarrow\mathrm{\mathcal{S}et} which either have each X(b)X(b) empty, or each X(b)X(b) non-empty. However, we will be less interested in characterising the 𝖳𝔹\mathsf{T}_{\mathbb{B}}-models in 𝒮et\mathrm{\mathcal{S}et} than the 𝖳𝔹\mathsf{T}_{\mathbb{B}}-comodels. We may exploit the fact that 𝖳𝔹\mathsf{T}_{\mathbb{B}} is generated by the theory of 𝔹\mathbb{B}-valued dependently typed update to obtain such a characterisation.

Proposition 36.

For any small category 𝔹\mathbb{B}, the category of comodels of the theory 𝕋𝔹\mathbb{T}_{\mathbb{B}} of 𝔹\mathbb{B}-valued dependently-typed update is isomorphic to 𝔹-𝒮et\mathbb{B}\text{-}\mathrm{\mathcal{S}et} via a functor

(3.9)
𝔹-𝒮et\textstyle{{\mathbb{B}\text{-}\mathrm{\mathcal{S}et}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}U𝔹\scriptstyle{U^{\mathbb{B}}}𝒮𝕋𝔹et\textstyle{{{}^{\mathbb{T}_{\mathbb{B}}}\mathrm{\mathcal{S}et}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}U𝕋𝔹\scriptstyle{{}^{\mathbb{T}_{\mathbb{B}}}U}𝒮et\textstyle{{\mathrm{\mathcal{S}et}}}

which sends a left 𝔹\mathbb{B}-set (X,p,)(X,p,\mathord{\ast}) to the 𝕋𝔹\mathbb{T}_{\mathbb{B}}-comodel 𝐗=(X,𝐗){\boldsymbol{X}}=(X,\llbracket{\mathord{\text{--}}}\rrbracket_{{\boldsymbol{X}}}) with

𝗀𝖾𝗍𝑿(x)=(p(x),x)𝗎𝗉𝖽f𝑿(x)={x if p(x)dom(f);fx if p(x)=dom(f).\llbracket\mathsf{get}\rrbracket_{{\boldsymbol{X}}}(x)=(p(x),x)\qquad\llbracket\mathsf{upd}_{f}\rrbracket_{{\boldsymbol{X}}}(x)=\begin{cases}x&\text{ if $p(x)\neq\mathrm{dom}(f)$;}\\ f\ast x&\text{ if $p(x)=\mathrm{dom}(f)$.}\end{cases}
Proof.

A comodel of 𝔹\mathbb{B}-valued dependently typed state is firstly, a comodel of ob(𝔹)\mathrm{ob}(\mathbb{B})-valued read-only state, i.e., a set XX endowed with a function p:Xob(𝔹)p\colon X\rightarrow\mathrm{ob}(\mathbb{B}). On top of this, we have functions 𝗎𝗉𝖽f:XX\llbracket\mathsf{upd}_{f}\rrbracket\colon X\rightarrow X for each f:bbf\colon b\rightarrow b^{\prime} in 𝔹\mathbb{B} which satisfy the equations (3.3)–(3.6). The first forces 𝗎𝗉𝖽f\llbracket\mathsf{upd}_{f}\rrbracket to act trivially on the fibre p1(c)p^{-1}(c) for all cbc\neq b, while the second forces it to map p1(b)p^{-1}(b) into p1(b)p^{-1}(b^{\prime}). So to give the 𝗎𝗉𝖽f\llbracket\mathsf{upd}_{f}\rrbracket’s satisfying (3.3) and (3.4) is equally to give functions f():p1(b)p1(b)f\ast({\mathord{\text{--}}})\colon p^{-1}(b)\rightarrow p^{-1}(b^{\prime}) for each f:bbf\colon b\rightarrow b^{\prime} in 𝔹\mathbb{B}. Now the last two axioms (3.5) and (3.6) impose the functoriality constraints 1bx=x1_{b}\ast x=x and g(fx)=(gf)xg\ast(f\ast x)=(g\circ f)\ast x, so that, in sum, a comodel of 𝔹\mathbb{B}-valued dependently typed update can be identified with a left 𝔹\mathbb{B}-set, via the identification given in the statement of the result. ∎

4. The costructure–cosemantics adjunction

In this section, we construct the adjunction which is the main object of study of this paper. We begin by explaining how taking comodels yields a cosemantics functor from accessible monads to accessible comonads on 𝒮et\mathrm{\mathcal{S}et}. We then show that this functor has a left adjoint, as displayed below, which we term the costructure functor; and finally, we explain how this relates to the material of [18].

(4.1) nda(𝒮et)op\textstyle{{\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Cosem\scriptstyle{\mathrm{Cosem}}Costr\scriptstyle{\mathrm{Costr}}\scriptstyle{\bot}𝒞mda(𝒮et)\textstyle{{\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})}}

4.1. The cosemantics comonad of an accessible monad

Our first task is to show that the cosemantics functor of Definition 24 yields the right adjoint functor in (4.1). We begin with the basic facts about Eilenberg–Moore semantics for comonads.

Definition 37 (Eilenberg–Moore semantics).

Let 𝒞md(𝒞)\mathrm{\mathcal{C}md}({\mathcal{C}}) be the category of comonads in 𝒞{\mathcal{C}}. The Eilenberg–Moore semantics functor EM:𝒞md(𝒞)𝒞AT/𝒞\mathrm{EM}\colon\mathrm{\mathcal{C}md}({\mathcal{C}})\rightarrow\mathrm{\mathcal{C}AT}/{\mathcal{C}} sends a comonad 𝖰=(Q,ε,δ)\mathsf{Q}=(Q,\varepsilon,\delta) to the forgetful functor U𝖰:𝒞oalg(𝖰)𝒞U^{\mathsf{Q}}\colon\mathrm{\mathcal{C}oalg}(\mathsf{Q})\rightarrow{\mathcal{C}} from its category of Eilenberg–Moore coalgebras, and sends f:𝖰𝖯f\colon\mathsf{Q}\rightarrow\mathsf{P} to the functor 𝒞oalg(𝖰)𝒞oalg(𝖯)\mathrm{\mathcal{C}oalg}(\mathsf{Q})\rightarrow\mathrm{\mathcal{C}oalg}(\mathsf{P}) over 𝒞{\mathcal{C}} acting by (X,x:XQX)(X,fXx:XPX)(X,x\colon X\rightarrow QX)\mapsto(X,f_{X}\circ x\colon X\rightarrow PX).

Lemma 38.

For any category 𝒞{\mathcal{C}}, the semantics functor EM:𝒞md(𝒞)𝒞AT/𝒞\mathrm{EM}\colon\mathrm{\mathcal{C}md}({\mathcal{C}})\rightarrow\mathrm{\mathcal{C}AT}/{\mathcal{C}} is full and faithful, and its essential image comprises the strictly comonadic functors.

Proof.

The first part is [8, Theorem 6.3]; the second is easy from the definitions. ∎

Here, a functor V:𝒟𝒞V\colon{\mathcal{D}}\rightarrow{\mathcal{C}} is strictly comonadic if it has a right adjoint GG, and the canonical comparison functor from 𝒟{\mathcal{D}} to the category of coalgebras for the comonad VGVG is an isomorphism. Concrete conditions for a functor to be strictly comonadic are given by the Beck comonadicity theorem [8, Theorem 3.14].

Proposition 39.

For an accessible 𝒮et\mathrm{\mathcal{S}et}-monad 𝖳\mathsf{T}, the forgetful functor U𝖳:𝒮𝖳et𝒮et{{}^{\mathsf{T}}U\colon{}^{\mathsf{T}}\mathrm{\mathcal{S}et}\rightarrow\mathrm{\mathcal{S}et}} from the category of comodels is strictly comonadic for an accessible comonad.

In the finitary case, this is [31, Theorem 2.2]; this more general form can be proven as a routine application of the theory of locally presentable categories. We omit this, as Theorem 62 provides an independent elementary argument.

Corollary 40.

The cosemantics functor nda(𝒮et)op𝒞AT/𝒮et\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}AT}/\mathrm{\mathcal{S}et} factors as

nda(𝒮et)op\ext@arrow01200\rightarrowfill@Cosem𝒞mda(𝒮et)\ext@arrow01200\rightarrowfill@EM𝒞AT/𝒮et .\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\ext@arrow 01{20}0\rightarrowfill@{}{\mathrm{Cosem}}\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})\ext@arrow 01{20}0\rightarrowfill@{}{\mathrm{EM}}\mathrm{\mathcal{C}AT}/\mathrm{\mathcal{S}et}\hbox to0.0pt{ .\hss}

4.2. The costructure monad of an accessible comonad

We now show that the cosemantics functor nda(𝒮et)op𝒞mda(𝒮et)\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}) has a left adjoint. This will arise from the “structure–semantics adjointness” of [21, 10], which we now recall.

Definition 41 (Endomorphism monad).

Let 𝒞{\mathcal{C}} be a category with powers which is not necessarily locally small. We say that X𝒞X\in{\mathcal{C}} is tractable if, for any set AA, the collection of maps XAXX^{A}\rightarrow X form a set. For such an XX, the endomorphism monad 𝖤𝗇𝖽𝒞(X)\mathsf{End}_{\mathcal{C}}(X) on 𝒮et\mathrm{\mathcal{S}et} has action on objects A𝒞(XA,X)A\mapsto{\mathcal{C}}(X^{A},X); unit functions A𝒞(XA,X)A\rightarrow{\mathcal{C}}(X^{A},X) given by aπaa\mapsto\pi_{a}; and Kleisli extension u:𝒞(XA,X)𝒞(XB,X)u^{\dagger}\colon{\mathcal{C}}(X^{A},X)\rightarrow{\mathcal{C}}(X^{B},X) of u:A𝒞(XB,X)u\colon A\rightarrow{\mathcal{C}}(X^{B},X) given by tt(ua)aAt\mapsto t\circ(u_{a})_{a\in A}.

Note that endomorphism monads need not be accessible; for example, the endomorphism monad of V𝒮etV\in\mathrm{\mathcal{S}et} is the non-accessible continuation monad VV()V^{V^{({\mathord{\text{--}}})}}.

Lemma 42.

Let 𝒞{\mathcal{C}} be a category with powers, not necessarily locally small, and let X𝒞X\in{\mathcal{C}} be tractable. There is a bijection, natural in 𝖳\mathsf{T}, between monad morphisms 𝖳𝖤𝗇𝖽𝒞(X)\mathsf{T}\rightarrow\mathsf{End}_{\mathcal{C}}(X) and 𝖳\mathsf{T}-model structures on XX.

Proof.

To give a monad map 𝖳𝖤𝗇𝖽𝒞(X)\mathsf{T}\rightarrow\mathsf{End}_{\mathcal{C}}(X) is to give functions T(A)𝒞(XA,X)T(A)\rightarrow{\mathcal{C}}(X^{A},X) for each set AA, compatibly with units and Kleisli extensions. If we write the action of these functions as tt𝑿t\mapsto\llbracket t\rrbracket_{{\boldsymbol{X}}}, then these compatibilities are precisely the conditions (2.4) to make the t𝑿\llbracket t\rrbracket_{{\boldsymbol{X}}}’s into a 𝖳\mathsf{T}-model structure on XX. ∎

In the following result, we call a functor V:𝒜𝒞V\colon{\mathcal{A}}\rightarrow{\mathcal{C}} tractable if it is tractable as an object of the (not necessarily locally small) functor category [𝒜,𝒞][{\mathcal{A}},{\mathcal{C}}].

Proposition 43 (Structure/semantics).

Let 𝒞{\mathcal{C}} be a category with powers. The semantics functor Sem𝒞:nd(𝒮et)op𝒞AT/𝒞\mathrm{Sem}_{\mathcal{C}}\colon\mathrm{\mathcal{M}nd}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}AT}/{\mathcal{C}} of Definition 24 has a partial left adjoint at each tractable V:𝒜𝒞V\colon{\mathcal{A}}\rightarrow{\mathcal{C}}, given by the endomorphism monad 𝖤𝗇𝖽[𝒜,𝒞](V)\mathsf{End}_{[{\mathcal{A}},{\mathcal{C}}]}(V).

Proof.

Let V:𝒜𝒞V\colon{\mathcal{A}}\rightarrow{\mathcal{C}} be tractable, so that 𝖤𝗇𝖽[𝒜,𝒞](V)\mathsf{End}_{[{\mathcal{A}},{\mathcal{C}}]}(V) exists. By Lemma 42, there is a bijection, natural in 𝖳\mathsf{T}, between monad morphisms 𝖳𝖤𝗇𝖽[𝒜,𝒞](V)\mathsf{T}\rightarrow\mathsf{End}_{[{\mathcal{A}},{\mathcal{C}}]}(V) and 𝖳\mathsf{T}-model structures on VV in [𝒜,𝒞][{\mathcal{A}},{\mathcal{C}}]. Now since powers in [𝒜,𝒞][{\mathcal{A}},{\mathcal{C}}] are computed componentwise, 𝖳\mathsf{T}-model structures on VV correspond, naturally in 𝖳\mathsf{T}, with liftings

𝒞𝖳\textstyle{{\mathcal{C}}^{\mathsf{T}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}U𝖳\scriptstyle{U^{\mathsf{T}}}𝒜\textstyle{{\mathcal{A}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}V\scriptstyle{V}𝒞\textstyle{\mathcal{C}}

of VV through U𝖳U^{\mathsf{T}}, i.e., with maps VSem𝒞(𝖳)V\rightarrow\mathrm{Sem}_{\mathcal{C}}(\mathsf{T}) in 𝒞AT/𝒞\mathrm{\mathcal{C}AT}/{\mathcal{C}}. ∎

Because we are interested in comodels rather than models, we will apply this result in its dual form: thus, we speak of the cotractability of a functor V:𝒜𝒞V\colon{\mathcal{A}}\rightarrow{\mathcal{C}}, meaning that each collection [𝒜,𝒞](V,AV)[{\mathcal{A}},{\mathcal{C}}](V,A\cdot V) is a set, and the coendomorphism monad 𝖢𝗈𝖾𝗇𝖽[𝒜,𝒞](V)\mathsf{Coend}_{[{\mathcal{A}},{\mathcal{C}}]}(V) with action on objects A[𝒜,𝒞](V,AV)A\mapsto[{\mathcal{A}},{\mathcal{C}}](V,A\cdot V).

Lemma 44.

Let 𝖰\mathsf{Q} be an accessible comonad on 𝒮et\mathrm{\mathcal{S}et}. The forgetful functor from the category of Eilenberg–Moore coalgebras U𝖰:𝒞oalg(𝖰)𝒮etU^{\mathsf{Q}}\colon\mathrm{\mathcal{C}oalg}(\mathsf{Q})\rightarrow\mathrm{\mathcal{S}et} is cotractable, and the coendomorphism monad 𝖢𝗈𝖾𝗇𝖽[𝒞oalg(𝖰),𝒮et](U𝖰)\mathsf{Coend}_{[\mathrm{\mathcal{C}oalg}(\mathsf{Q}),\mathrm{\mathcal{S}et}]}(U^{\mathsf{Q}}) is accessible.

Proof.

For cotractability, we show that for each set AA, the collection of natural transformations U𝖰AU𝖰U^{\mathsf{Q}}\Rightarrow A\cdot U^{\mathsf{Q}} form a set. If we write G𝖰G^{\mathsf{Q}} for the right adjoint of U𝖰U^{\mathsf{Q}}, then transposing under the adjunction ()G𝖰()U𝖰({\mathord{\text{--}}})\circ G^{\mathsf{Q}}\dashv({\mathord{\text{--}}})\circ U^{\mathsf{Q}} yields

(4.2) [𝖰-𝒞oalg,𝒮et](U𝖰,AU𝖰)[𝒮et,𝒮et](Q,Aid) ,[\mathsf{Q}\text{-}\mathrm{\mathcal{C}oalg},\mathrm{\mathcal{S}et}](U^{\mathsf{Q}},A\cdot U^{\mathsf{Q}})\cong[\mathrm{\mathcal{S}et},\mathrm{\mathcal{S}et}](Q,A\cdot\mathrm{id})\hbox to0.0pt{ ,\hss}

whose right-hand side is a set since QQ is accessible; whence also the left-hand side.

So 𝖢𝗈𝖾𝗇𝖽(U𝖰)\mathsf{Coend}(U^{\mathsf{Q}}) exists; to show accessibility, note that a natural transformation QAidQ\Rightarrow A\cdot\mathrm{id} is equally a pair of natural transformations QΔAQ\Rightarrow\Delta A and QidQ\Rightarrow\mathrm{id}; and since 𝒮et\mathrm{\mathcal{S}et} has a terminal object, to give QΔAQ\Rightarrow\Delta A is equally to give a function Q1AQ1\rightarrow A. We conclude that Coend(U𝖰)()Q1×[𝒮et,𝒮et](Q,id) ,\mathrm{Coend}(U^{\mathsf{Q}})\cong({\mathord{\text{--}}})^{Q1}\times[\mathrm{\mathcal{S}et},\mathrm{\mathcal{S}et}](Q,\mathrm{id})\hbox to0.0pt{ ,\hss} which is a small coproduct of representable functors, and hence accessible. ∎

Proposition 45.

The functor Cosem:nda(𝒮et)op𝒞mda(𝒮et)\mathrm{Cosem}\colon\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}) of Corollary 40 admits a left adjoint Costr:𝒞mda(𝒮et)nda(𝒮et)op\mathrm{Costr}\colon\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})\rightarrow\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}, whose value at the accessible comonad 𝖰\mathsf{Q} is given by the coendomorphism monad 𝖢𝗈𝖾𝗇𝖽(U𝖰)\mathsf{Coend}(U^{\mathsf{Q}}).

Proof.

The preceding result shows that 𝖢𝗈𝖾𝗇𝖽(U𝖰)\mathsf{Coend}(U^{\mathsf{Q}}) exists and is accessible for each accessible comonad 𝖰\mathsf{Q}. Now by Lemma 38, Proposition 39 and Proposition 43, we have natural isomorphisms

𝒞mda(𝒮et)(𝖰,Cosem(𝖳))𝒞AT/𝒮et(U𝖰,U𝖳)nda(𝒮et)(𝖳,𝖢𝗈𝖾𝗇𝖽(U𝖰)) .\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})(\mathsf{Q},\mathrm{Cosem}(\mathsf{T}))\cong\mathrm{\mathcal{C}AT}/\mathrm{\mathcal{S}et}(U^{\mathsf{Q}},{}^{\mathsf{T}}U)\cong\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})(\mathsf{T},\mathsf{Coend}(U^{\mathsf{Q}}))\text{ .}\qed
Remark 46.

For future use, we record the concrete form of the adjointness isomorphisms of the costructure–cosemantics adjunction. Given a comonad morphism α:𝖰Cosem(𝖳)\alpha\colon\mathsf{Q}\rightarrow\mathrm{Cosem}(\mathsf{T}), corresponding by Lemma 38 and Proposition 39 to a functor HH as in:

𝒞oalg(𝖰)\textstyle{{\mathrm{\mathcal{C}oalg}(\mathsf{Q})}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}H\scriptstyle{H}U𝖰\scriptstyle{U^{\mathsf{Q}}}𝒮𝖳et ,\textstyle{{{}^{\mathsf{T}}\mathrm{\mathcal{S}et}}\hbox to0.0pt{ ,\hss}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}U𝖳\scriptstyle{{}^{\mathsf{T}}U}𝒮et\textstyle{{\mathrm{\mathcal{S}et}}}

the adjoint transpose α¯:𝖳𝖢𝗈𝖾𝗇𝖽(U𝖰)\bar{\alpha}\colon\mathsf{T}\rightarrow\mathsf{Coend}(U^{\mathsf{Q}}) of α\alpha sends tT(A)t\in T(A) to α¯(t):U𝖰AU𝖰\bar{\alpha}(t)\colon U^{\mathsf{Q}}\Rightarrow A\cdot U^{\mathsf{Q}} with components α¯(t)(X,x)=tH(X,x):XA×X\bar{\alpha}(t)_{(X,x)}=\llbracket t\rrbracket_{H(X,x)}\colon X\rightarrow A\times X.

4.3. Relation to duals and Sweedler duals

This completes our construction of the costructure–cosemantics adjunction (4.1); and in the rest of this section, we explain its relation to the notions of [18]. The main objects of study in loc. cit. are the interaction laws between a monad 𝖳\mathsf{T} and a comonad 𝖰\mathsf{Q} on a category with products; these are natural families of maps TX×QYX×YTX\times QY\rightarrow X\times Y which are compatible with the monad and comonad structures. In Section 3.4 of [18], the authors show that such monad–comonad interaction laws can also be expressed in terms of:

  • Monad morphisms 𝖳𝖰\mathsf{T}\rightarrow\mathsf{Q}^{\circ}, where 𝖰\mathsf{Q}^{\circ} is the dual monad of 𝖰\mathsf{Q};

  • Comonad morphisms 𝖰𝖳\mathsf{Q}\rightarrow\mathsf{T}^{\bullet}, where 𝖳\mathsf{T}^{\bullet} is the Sweedler dual comonad of 𝖳\mathsf{T}.

It may or may not be the case that the dual monad of a comonad, or the Sweedler dual comonad of a monad, exist; however, they do always exist when we are dealing with accessible monads and comonads on 𝒮et\mathrm{\mathcal{S}et}, and the definitions are as follows:

Definition 47 (Dual monad).

The dual of an accessible comonad 𝖰\mathsf{Q} on 𝒮et\mathrm{\mathcal{S}et} is the accessible monad 𝖰\mathsf{Q}^{\circ} with Q(A)=[𝒮et,𝒮et](Q,Aid)Q^{\circ}(A)=[\mathrm{\mathcal{S}et},\mathrm{\mathcal{S}et}](Q,A\cdot\mathrm{id}), with unit map ηA:AQA\eta_{A}\colon A\rightarrow Q^{\circ}A given by

aQ\ext@arrow01200\rightarrowfill@εid\ext@arrow01200\rightarrowfill@νaAida\qquad\mapsto\qquad Q\ext@arrow 01{20}0\rightarrowfill@{}{\varepsilon}\mathrm{id}\ext@arrow 01{20}0\rightarrowfill@{}{\nu_{a}}A\cdot\mathrm{id}

and with the Kleisli extension u:[𝒮et,𝒮et](Q,Aid)[𝒮et,𝒮et](Q,Bid)u^{\dagger}\colon[\mathrm{\mathcal{S}et},\mathrm{\mathcal{S}et}](Q,A\cdot\mathrm{id})\rightarrow[\mathrm{\mathcal{S}et},\mathrm{\mathcal{S}et}](Q,B\cdot\mathrm{id}) of u:A[𝒮et,𝒮et](Q,Bid)u\colon A\rightarrow[\mathrm{\mathcal{S}et},\mathrm{\mathcal{S}et}](Q,B\cdot\mathrm{id}) given by

Q\ext@arrow01200\rightarrowfill@τAidQ\ext@arrow01200\rightarrowfill@δQQ\ext@arrow01200\rightarrowfill@τQAQ\ext@arrow01200\rightarrowfill@uaaABid .Q\ext@arrow 01{20}0\rightarrowfill@{}{\tau}A\cdot\mathrm{id}\qquad\mapsto\qquad Q\ext@arrow 01{20}0\rightarrowfill@{}{\delta}QQ\ext@arrow 01{20}0\rightarrowfill@{}{\tau Q}A\cdot Q\ext@arrow 01{20}0\rightarrowfill@{}{\langle u_{a}\rangle_{a\in A}}B\cdot\mathrm{id}\hbox to0.0pt{ .\hss}

The assignment 𝖰𝖰\mathsf{Q}\mapsto\mathsf{Q}^{\circ} is the action on objects of the dual monad functor 𝒞mda(𝒮et)nda(𝒮et)op\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})\rightarrow\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}, whose action on morphisms takes a comonad map f:𝖰𝖯f\colon\mathsf{Q}\rightarrow\mathsf{P} to the monad map 𝖯𝖰\mathsf{P}^{\circ}\rightarrow\mathsf{Q}^{\circ} with components ααf\alpha\mapsto\alpha f.

Definition 48 (Sweedler dual comonad).

The Sweedler dual of an accessible monad 𝖳\mathsf{T} on 𝒮et\mathrm{\mathcal{S}et} is the accessible comonad 𝖳\mathsf{T}^{\bullet} providing the value at 𝖳\mathsf{T} of a right adjoint to the dual monad functor.

We now show that, in fact, these constructions relating accessible monads and comonads are precisely the two directions of our adjunction (4.1).

Proposition 49.

For each 𝖰𝒞mda(𝒮et)\mathsf{Q}\in\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}), there is a monad isomorphism 𝖰Costr(𝖰)\mathsf{Q}^{\circ}\cong\mathrm{Costr}(\mathsf{Q}) taking α:QAid\alpha\colon Q\Rightarrow A\cdot\mathrm{id} in QAQ^{\circ}A to α~:U𝖰AU𝖰\tilde{\alpha}\colon U^{\mathsf{Q}}\Rightarrow A\cdot U^{\mathsf{Q}} in 𝖢𝗈𝖾𝗇𝖽(U𝖰)(A)\mathsf{Coend}(U^{\mathsf{Q}})(A) with components

α~(X,x)=X\ext@arrow01200\rightarrowfill@xQX\ext@arrow01200\rightarrowfill@αXA×X .\tilde{\alpha}_{(X,x)}=X\ext@arrow 01{20}0\rightarrowfill@{}{x}QX\ext@arrow 01{20}0\rightarrowfill@{}{\alpha_{X}}A\times X\hbox to0.0pt{ .\hss}

It follows that Costr():𝒞mda(𝒮et)nda(𝒮et)op\mathrm{Costr}\cong({\mathord{\text{--}}})^{\circ}\colon\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})\rightarrow\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}} and, consequently, that Cosem():nda(𝒮et)op𝒞mda(𝒮et)\mathrm{Cosem}\cong({\mathord{\text{--}}})^{\bullet}\colon\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}).

Proof.

Consider the category 𝒳{\mathcal{X}} whose objects are endofunctors of 𝒮et\mathrm{\mathcal{S}et}, and whose morphisms FFF\rightarrow F^{\prime} are natural transformations FU𝖰FU𝖰:𝒞oalg(𝖰)𝒮etFU^{\mathsf{Q}}\Rightarrow F^{\prime}U^{\mathsf{Q}}\colon\mathrm{\mathcal{C}oalg}(\mathsf{Q})\rightarrow\mathrm{\mathcal{S}et}. It is easy to see that 𝖢𝗈𝖾𝗇𝖽(U𝖰)\mathsf{Coend}(U^{\mathsf{Q}}) is equally the coendomorphism monad of the object id𝒮et𝒳\mathrm{id}_{\mathrm{\mathcal{S}et}}\in{\mathcal{X}}. By transposing under the adjunction ()G𝖰()U𝖰({\mathord{\text{--}}})\circ G^{\mathsf{Q}}\dashv({\mathord{\text{--}}})\circ U^{\mathsf{Q}}, we see that 𝒜{\mathcal{A}} is isomorphic to the co-Kleisli category 𝒳{\mathcal{X}}^{\prime} of the comonad ()𝖰({\mathord{\text{--}}})\circ\mathsf{Q} on [𝒮et,𝒮et][\mathrm{\mathcal{S}et},\mathrm{\mathcal{S}et}], and the coendomorphism monad of id𝒮et\mathrm{id}_{\mathrm{\mathcal{S}et}} in 𝒳{\mathcal{X}}^{\prime} is easily seen to be 𝖰\mathsf{Q}^{\circ}. Thus 𝖰𝖢𝗈𝖾𝗇𝖽(U𝖰)\mathsf{Q}^{\circ}\cong\mathsf{Coend}(U^{\mathsf{Q}}), and tracing through the correspondences shows this isomorphism to be given as in the statement of the result. ∎

5. Calculating the cosemantics functor

5.1. Cosemantics is valued in presheaf comonads

In this section, we give an explicit calculation of the values of the cosemantics functor from monads to comonads. As a first step towards this, we observe that:

Proposition 50.

The cosemantics functor Cosem:nda(𝒮et)op𝒞mda(𝒮et)\mathrm{Cosem}\colon\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}) sends every monad to a presheaf comonad; whence it admits a factorisation to within isomorphism through 𝖰():𝒞of𝒞mda(𝒮et)\mathsf{Q}_{({\mathord{\text{--}}})}\colon\mathrm{\mathcal{C}of}\rightarrow\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}).

Proof.

Note that the second clause follows from the first and Proposition 32. To prove the first, let 𝖳nda(𝒮et)\mathsf{T}\in\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et}). To show that Cosem(𝖳)\mathrm{Cosem}(\mathsf{T}) is a presheaf comonad, it suffices by Proposition 27 to prove that its underlying endofunctor preserves connected limits. Since this endofunctor is engendered by U𝖳:𝒮𝖳et𝒮et{}^{\mathsf{T}}U\colon{}^{\mathsf{T}}\mathrm{\mathcal{S}et}\rightarrow\mathrm{\mathcal{S}et} and its (limit-preserving) right adjoint, it suffices to show that U𝖳{}^{\mathsf{T}}U preserves connected limits. In fact, it creates them: for indeed, since a 𝖳\mathsf{T}-comodel 𝑺{\boldsymbol{S}} in 𝒮et\mathrm{\mathcal{S}et} involves co-operations t:SA×S\llbracket t\rrbracket\colon S\rightarrow A\times S subject to suitable equations, and since each functor A×()A\times({\mathord{\text{--}}}) preserves connected limits, it follows easily that, for any connected diagram of 𝖳\mathsf{T}-comodels, the limit of the diagram of underlying sets bears a unique comodel structure making it the limit in the category of comodels. ∎

What we would like to do is to give an explicit description of the factorisation of this proposition. Thus, at the level of objects, we will describe for each accessible monad 𝖳\mathsf{T} on 𝒮et\mathrm{\mathcal{S}et} a small category 𝔹𝖳\mathbb{B}_{\mathsf{T}} such that Cosem(𝖳)𝖰𝔹𝖳\mathrm{Cosem}(\mathsf{T})\cong\mathsf{Q}_{\mathbb{B}_{\mathsf{T}}}; or equally, in light of Proposition 35, such that we have an isomorphism in 𝒞AT/𝒮et\mathrm{\mathcal{C}AT}/\mathrm{\mathcal{S}et}:

(5.1)
𝒮𝖳et\textstyle{{}^{\mathsf{T}}\mathrm{\mathcal{S}et}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}U𝕋\scriptstyle{{}^{\mathbb{T}}U}\scriptstyle{\cong}𝔹𝖳-𝒮et .\textstyle{\mathbb{B}_{\mathsf{T}}\text{-}\mathrm{\mathcal{S}et}\hbox to0.0pt{ .\hss}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}U𝔹𝖳\scriptstyle{U^{\mathbb{B}_{\mathsf{T}}}}𝒮et\textstyle{\mathrm{\mathcal{S}et}}

We term this category 𝔹𝖳\mathbb{B}_{\mathsf{T}} the behaviour category of 𝖳\mathsf{T}.

5.2. Behaviours and the final comodel

The first step is to describe the object-set of the behaviour category 𝔹𝖳\mathbb{B}_{\mathsf{T}} associated to an accessible monad 𝖳\mathsf{T}. By considering (5.1), we see that this object-set can be found as the image under U𝔹𝖳U^{\mathbb{B}_{\mathsf{T}}} of the final object of 𝔹𝖳-𝒮et\mathbb{B}_{\mathsf{T}}\text{-}\mathrm{\mathcal{S}et}: and so equally as the underlying set of the final comodel of 𝖳\mathsf{T}. While there are many possible constructions of the final comodel—see, for example, [31, Theorem 2.2] or [26, Lemma 4.6]—we would like to give a new one which fully exploits the fact that the structures we are working with are comodels.

As is well known, when looking at coalgebraic structures, characterising the final object is bound up with answering the question of when states have the same observable behaviour. For example, if (g,n):SV×S(g,n)\colon S\rightarrow V\times S and (g,n):SV×S(g^{\prime},n^{\prime})\colon S^{\prime}\rightarrow V\times S^{\prime} are comodels of the theory of VV-valued input, we may say that states sSs\in S and sSs^{\prime}\in S^{\prime} are behaviourally equivalent if they yield the same stream of values:

(g(s),g(n(s)),g(n(n(s))),)=(g(s),g(n(s)),g(n(n(s))),) .(g(s),g(n(s)),g(n(n(s))),\dots)=(g^{\prime}(s^{\prime}),g^{\prime}(n^{\prime}(s^{\prime})),g^{\prime}(n^{\prime}(n^{\prime}(s^{\prime}))),\dots)\hbox to0.0pt{ .\hss}

We may restate this property in more structural ways. Indeed, states sSs\in S and sSs^{\prime}\in S^{\prime} are behaviourally equivalent just when any of the following conditions holds:

  • They are related by some bisimulation, i.e., a relation RS×SR\subseteq S\times S^{\prime} whose projections SRSS\leftarrow R\rightarrow S^{\prime} can be lifted to a span of comodels 𝑺𝑹𝑺{\boldsymbol{S}}\leftarrow{\boldsymbol{R}}\rightarrow{\boldsymbol{S^{\prime}}}.

  • They become equal in some comodel 𝑺′′{\boldsymbol{S}}^{\prime\prime}; i.e., there are comodel homomorphisms q:𝑺𝑺′′𝑺:qq\colon{\boldsymbol{S}}\rightarrow{\boldsymbol{S}}^{\prime\prime}\leftarrow{\boldsymbol{S}}^{\prime}\colon q^{\prime} such that q(s)=q(s)q(s)=q^{\prime}(s^{\prime});

  • They become equal in the final comodel 𝑽{\boldsymbol{V^{\mathbb{N}}}}.

The correspondence between these conditions holds in much greater generality; see, for example [34]. However, for the comodels of an accessible monad 𝖳\mathsf{T}, there is a further, yet more intuitive, formulation: ss and ss^{\prime} are behaviourally equivalent if, in running any 𝖳\mathsf{T}-computation tT(A)t\in T(A), we obtain the same AA-value by running tt with 𝑺{\boldsymbol{S}} from initial state ss, as by running tt with 𝑺{\boldsymbol{S}}^{\prime} from initial state ss^{\prime}. More formally, we have the following definition, which appears to be novel—though it is closely related to [25]’s notion of comodel bisimulation.

Definition 51 (Behaviour of a state).

Let 𝖳\mathsf{T} be an accessible monad and 𝑺{\boldsymbol{S}} a 𝖳\mathsf{T}-comodel. The behaviour βs\beta_{s} of a state sSs\in S is the family of functions

(βs)A:T(A)Atπ1(t𝑺(s)) .(\beta_{s})_{A}\colon T(A)\rightarrow A\qquad\qquad t\mapsto\pi_{1}(\llbracket t\rrbracket_{{\boldsymbol{S}}}(s))\hbox to0.0pt{ .\hss}

Given 𝖳\mathsf{T}-comodels 𝑺{\boldsymbol{S}} and 𝑺{\boldsymbol{S}}^{\prime}, we say that states sSs\in S and sSs^{\prime}\in S^{\prime} are operationally equivalent (written soss\sim_{o}s^{\prime}) if βs=βs\beta_{s}=\beta_{s^{\prime}}.

We now show that operational equivalence has the same force as the other notions of behavioural equivalence listed above.

Proposition 52.

Let 𝖳\mathsf{T} be an accessible monad and let 𝐒,𝐒{\boldsymbol{S}},{\boldsymbol{S}}^{\prime} be 𝖳\mathsf{T}-comodels in 𝒮et\mathrm{\mathcal{S}et}. For any states sSs\in S and sSs^{\prime}\in S^{\prime}, the following conditions are equivalent:

  1. (i)

    ss and ss^{\prime} are operationally equivalent;

  2. (ii)

    s𝑅ss\mathrel{R}s^{\prime} for some bisimulation RS×SR\subseteq S\times S^{\prime} between 𝑺{\boldsymbol{S}} and 𝑺{\boldsymbol{S}}^{\prime};

  3. (iii)

    q(s)=q(s)q(s)=q^{\prime}(s^{\prime}) for some cospan of homomorphisms q:𝑺𝑺′′𝑺:qq\colon{\boldsymbol{S}}\rightarrow{\boldsymbol{S}}^{\prime\prime}\leftarrow{\boldsymbol{S}}^{\prime}\colon q^{\prime};

  4. (iv)

    f(s)=f(s)f(s)=f^{\prime}(s^{\prime}) for f:𝑺𝑩𝖳𝑺:ff\colon{\boldsymbol{S}}\rightarrow{{\boldsymbol{B}}}_{\mathsf{T}}\leftarrow{\boldsymbol{S}}^{\prime}\colon f^{\prime} the unique maps to the final comodel.

Proof.

For (i) \Rightarrow (ii), we show that operational equivalence o\sim_{o} is a bisimulation between 𝑺{\boldsymbol{S}} and 𝑺{\boldsymbol{S}}^{\prime}; this means showing that, if u1ou2u_{1}\sim_{o}u_{2} and tT(A)t\in T(A), then the co-operations t𝑺(s1)=(a1,s1)\llbracket t\rrbracket_{{\boldsymbol{S}}}(s_{1})=(a_{1},s_{1}^{\prime}) and t𝑺(s2)=(a2,s2)\llbracket t\rrbracket_{{\boldsymbol{S}}^{\prime}}(s_{2})=(a_{2},s_{2}^{\prime}) satisfy a1=a2Aa_{1}=a_{2}\in A and s1os2Ss_{1}^{\prime}\sim_{o}s_{2}^{\prime}\in S. We have a1=a2a_{1}=a_{2} since s1os2s_{1}\sim_{o}s_{2}. To show s1os2s_{1}^{\prime}\sim_{o}s_{2}^{\prime}, consider any term uT(B)u\in T(B), and observe by (2.6) that

t(λa.u)(s1)=u(s1)andt(λa.u)(s2)=u(s2) .\llbracket t(\lambda a.\,u)\rrbracket(s_{1})=\llbracket u\rrbracket(s_{1}^{\prime})\ \ \text{and}\ \ \llbracket t(\lambda a.\,u)\rrbracket(s_{2})=\llbracket u\rrbracket(s_{2}^{\prime})\hbox to0.0pt{ .\hss}

Since s1os2s_{1}\sim_{o}s_{2}, the left-hand sides above have the same first component; whence the same is true for the right-hand sides, so that s1os2s_{1}^{\prime}\sim_{o}s_{2}^{\prime} as desired.

The next two implications are standard. For (ii) \Rightarrow (iii), we take 𝑺𝑺′′𝑺{\boldsymbol{S}}\rightarrow{\boldsymbol{S}}^{\prime\prime}\leftarrow{\boldsymbol{S}}^{\prime} to be the pushout of 𝑺𝑹𝑺{\boldsymbol{S}}\leftarrow{\boldsymbol{R}}\rightarrow{\boldsymbol{S}}^{\prime}; and for (iii) \Rightarrow (iv), we postcompose 𝑺𝑺′′𝑺{\boldsymbol{S}}\rightarrow{\boldsymbol{S}}^{\prime\prime}\leftarrow{\boldsymbol{S}}^{\prime} with the unique comodel map 𝑺′′𝑩𝖳{\boldsymbol{S}}^{\prime\prime}\rightarrow{{\boldsymbol{B}}}_{\mathsf{T}}. Finally, for (iv) \Rightarrow (i), note by the definition of comodel homomorphism that if h:𝑺𝑺h\colon{\boldsymbol{S}}\rightarrow{\boldsymbol{S}}^{\prime} then βs=βh(s)\beta_{s}=\beta_{h(s)} for all sSs\in S. So if f(s)=f(s)f(s)=f^{\prime}(s^{\prime}) as in (iv) then βs=βf(s)=βf(s)=βs\beta_{s}=\beta_{f(s)}=\beta_{f^{\prime}(s^{\prime})}=\beta_{s^{\prime}} and so soss\sim_{o}s^{\prime} as desired. ∎

From this result, we see that a final 𝖳\mathsf{T}-comodel can have at most one element of a given behaviour β\beta. In fact, in the spirit of [20, Theorem 4], we may characterise the final comodel as having exactly one element of each behaviour β\beta which is admissible, in the sense of being the behaviour of some element of some comodel. It turns out that this requirement can be captured purely algebraically.

Notation 53.

Let 𝖳\mathsf{T} be an accessible monad. Given terms tT(A)t\in T(A) and uT(B)u\in T(B), we write t"ut\gg u for the term t(λa.u)T(B)t(\lambda a.\,u)\in T(B). Noting that "\gg is an associative operation, we may write t"u"vt\gg u\gg v for (t"u)"v=t"(u"v)(t\gg u)\gg v=t\gg(u\gg v), and so on.

The intuition is that, if tt and uu are programs returning values in AA and BB, then t"ut\gg u is the program which first performs tt, then discards the return value and continues as uu. The notation we use is borrowed from Haskell, where t >> u is used with exactly this sense.

Definition 54 (Admissible behaviour).

By an admissible behaviour for 𝖳\mathsf{T}, we mean a family of functions βA:TAA\beta_{A}\colon TA\rightarrow A, as AA ranges over sets, such that

(5.2) aAβA(a)=a,tTB,u(TA)BβA(t(u))=βA(t"uβB(t)) .a\in A\!\implies\!\beta_{A}(a)=a\text{,}\quad t\in TB,u\in(TA)^{B}\!\implies\!\beta_{A}(t(u))=\beta_{A}(t\gg u_{\beta_{B}(t)})\hbox to0.0pt{ .\hss}

We may drop the subscript in “βA\beta_{A}” where this does not lead to ambiguity.

These conditions are intuitively reasonable; for example, the second condition says that, if the result of running tT(B)t\in T(B) is bBb\in B, then the result of running t(u)T(A)t(u)\in T(A) coincides with that of running tt, discarding the return value, and then running ubu_{b}.

Remark 55.

If we have a presentation of the accessible monad 𝖳\mathsf{T} by an algebraic theory 𝕋\mathbb{T}, then we can use (5.2) and induction on the structure of 𝕋\mathbb{T}-terms to show that an admissible behaviour β\beta is determined by the values β(σ1""σn)|σn|\beta(\sigma_{1}\gg\dots\gg\sigma_{n})\in|\sigma_{n}| for each non-empty list σ1,,σn\sigma_{1},\dots,\sigma_{n} of generating operations in Σ\Sigma. This is practically useful in computing the admissible behaviours of a theory.

Proposition 56.

The final comodel 𝐁𝖳{{\boldsymbol{B}}}_{\mathsf{T}} of an accessible monad 𝖳\mathsf{T} is the set of admissible behaviours with t𝐁𝖳(β)=(β(t),tβ)\llbracket t\rrbracket_{{\boldsymbol{B}}_{\mathsf{T}}}(\beta)=(\beta(t),\partial_{t}\beta), where tβ(u)=β(t"u)\partial_{t}\beta(u)=\beta(t\gg u).

Proof.

Since every accessible monad can be presented by some algebraic theory, it follows from Remark 55 that the admissible behaviours of 𝖳\mathsf{T} do indeed form a set. We must also show t\llbracket t\rrbracket is well-defined, i.e., that if β\beta is admissible, then so is tβ\partial_{t}\beta. For this, we calculate

(tβ)(u(v))=β(t"u(v))=β((t"u)(v))=β(t"u"vβ(t"u))=(tβ)(u"vtβ(u)) .(\partial_{t}\beta)(u(v))\!=\!\beta(t\gg u(v))\!=\!\beta((t\gg u)(v))\!=\!\beta(t\gg u\gg v_{\beta(t\gg u)})\!=\!(\partial_{t}\beta)(u\gg v_{\partial_{t}\beta(u)})\hbox to0.0pt{ .\hss}

We now show 𝑩𝖳{{\boldsymbol{B}}}_{\mathsf{T}} is a comodel, i.e., that the conditions of (2.8) hold. For the first condition a𝑩𝖳=νa\llbracket a\rrbracket_{{{\boldsymbol{B}}}_{\mathsf{T}}}=\nu_{a}, we must show that (β(a),aβ)=(a,β)(\beta(a),\partial_{a}\beta)=(a,\beta) for all βB𝖳\beta\in B_{\mathsf{T}}: but β(a)=a\beta(a)=a by (5.2), while that aβ=β\partial_{a}\beta=\beta is clear since a"()a\gg({\mathord{\text{--}}}) is the identity operator. For the second condition in (2.8), we must show for any βB𝖳\beta\in B_{\mathsf{T}} that t(u)(β)=(β(t(u)),t(u)β)\llbracket t(u)\rrbracket(\beta)=(\beta(t(u)),\partial_{t(u)}\beta) is equal to

uaaA(t(β))=uaaA(β(t),tβ)=uβ(t)(tβ)=(tβ(uβ(t)),uβ(t)(tβ)) .\langle\llbracket u_{a}\rrbracket\rangle_{a\in A}(\llbracket t\rrbracket(\beta))=\langle\llbracket u_{a}\rrbracket\rangle_{a\in A}(\beta(t),\partial_{t}\beta)=\llbracket u_{\beta(t)}\rrbracket(\partial_{t}\beta)=(\partial_{t}\beta(u_{\beta(t)}),\partial_{u_{\beta(t)}}(\partial_{t}\beta))\hbox to0.0pt{ .\hss}

But in the first component β(t(u))=β(t"uβ(t))=tβ(uβ(t))\beta(t(u))=\beta(t\gg u_{\beta(t)})=\partial_{t}\beta(u_{\beta(t)}); while in the second,

t(u)β(v)\displaystyle\partial_{t(u)}\beta(v) =β(t(u)"v)=β(t(λa.ua"v))\displaystyle=\beta(t(u)\gg v)=\beta(t(\lambda a.\,u_{a}\gg v))
=β(t"uβ(t)"v)=tβ(uβ(t)"v)=(uβ(t)(tβ))(v)\displaystyle=\beta(t\gg u_{\beta(t)}\gg v)=\partial_{t}\beta(u_{\beta(t)}\gg v)=(\partial_{u_{\beta(t)}}(\partial_{t}\beta))(v)

as desired. So 𝑩𝖳{{\boldsymbol{B}}}_{\mathsf{T}} is a comodel.

We now show that, for any comodel 𝑺{\boldsymbol{S}}, there is a homomorphism β():𝑺𝑩𝖳\beta_{({\mathord{\text{--}}})}\colon{\boldsymbol{S}}\rightarrow{{\boldsymbol{B}}}_{\mathsf{T}} given by sβss\mapsto\beta_{s}. For this to be well-defined, each βs\beta_{s} must be an admissible behaviour; but for any tT(A)t\in T(A), we have t𝑺(s)=(βs(t),t"id𝑺(s))\llbracket t\rrbracket_{{\boldsymbol{S}}}(s)=(\beta_{s}(t),\llbracket t\gg\mathrm{id}\rrbracket_{{\boldsymbol{S}}}(s)), and so t(u)𝑺(s)=uβs(t)𝑺(t"id𝑺(s))=t"uβs(t)𝑺(s)\llbracket t(u)\rrbracket_{{\boldsymbol{S}}}(s)=\llbracket u_{\beta_{s}(t)}\rrbracket_{{\boldsymbol{S}}}(\llbracket t\gg\mathrm{id}\rrbracket_{{\boldsymbol{S}}}(s))=\llbracket t\gg u_{\beta_{s}(t)}\rrbracket_{{\boldsymbol{S}}}(s); now taking first components yields βs(t(u))=βs(t"uβs(t))\beta_{s}(t(u))=\beta_{s}(t\gg u_{\beta_{s}(t)}) as required. To show that β()\beta_{({\mathord{\text{--}}})} is a homomorphism 𝑺𝑩𝖳{\boldsymbol{S}}\rightarrow{{\boldsymbol{B}}}_{\mathsf{T}}, we calculate that

(1×β())t𝑺(s)\displaystyle(1\times\beta_{({\mathord{\text{--}}})})\llbracket t\rrbracket_{{\boldsymbol{S}}}(s) =(1×β())(βs(t),t"id𝑺(s))=(βs(t),βt"id𝑺(s))\displaystyle=(1\times\beta_{({\mathord{\text{--}}})})(\beta_{s}(t),\llbracket t\gg\mathrm{id}\rrbracket_{{\boldsymbol{S}}}(s))=(\beta_{s}(t),\beta_{\llbracket t\gg\mathrm{id}\rrbracket_{{\boldsymbol{S}}}(s)})
=(βs(t),t(βs))=t𝑩𝖳(βs) .\displaystyle=(\beta_{s}(t),\partial_{t}(\beta_{s}))=\llbracket t\rrbracket_{{{\boldsymbol{B}}}_{\mathsf{T}}}(\beta_{s})\hbox to0.0pt{ .\hss}

It remains to show that β()\beta_{({\mathord{\text{--}}})} is the unique homomorphism 𝑺𝑩𝖳{\boldsymbol{S}}\rightarrow{{\boldsymbol{B}}}_{\mathsf{T}}. But since t𝑩𝖳(β)=(β(t),tβ)\llbracket t\rrbracket_{{{\boldsymbol{B}}}_{\mathsf{T}}}(\beta)=(\beta(t),\partial_{t}\beta), the behaviour of any β𝑩𝖳\beta\in{{\boldsymbol{B}}}_{\mathsf{T}} is β\beta itself; and since as in Proposition 52, homomorphisms preserve behaviour, any homomorphism 𝑺𝑩𝖳{\boldsymbol{S}}\rightarrow{{\boldsymbol{B}}}_{\mathsf{T}} must necessarily send ss to βs\beta_{s}. ∎

Example 57.

While the final comodels of the algebraic theories considered so far are well-known, we illustrate the construction via admissible behaviours, exploiting Remark 55 to compute them in each case.

  • For VV-valued input, an admissible behaviour β\beta is determined by the values Wn:=β((𝗋𝖾𝖺𝖽")n𝗋𝖾𝖺𝖽)VW_{n}\mathrel{\mathop{:}}=\beta((\mathsf{read}\gg{})^{n}\mathsf{read})\in V for each nn\in\mathbb{N}. But since the theory has no equations, any such choice of values WVW\in V^{\mathbb{N}} yields an admissible behaviour. Thus the final comodel is VV^{\mathbb{N}}, and we can read off from Proposition 56 that 𝗋𝖾𝖺𝖽:VV×V\llbracket\mathsf{read}\rrbracket\colon V^{\mathbb{N}}\rightarrow V\times V^{\mathbb{N}} is given by W(W0,W)W\mapsto(W_{0},\partial W), where (W)i=Wi+1(\partial W)_{i}=W_{i+1}.

  • For VV-valued output, an admissible behaviour β\beta is determined by the trivial choices β(𝗉𝗎𝗍v1""𝗉𝗎𝗍vn)1\beta(\mathsf{put}_{v_{1}}\gg\dots\gg\mathsf{put}_{v_{n}})\in 1; whence there is a unique admissible behaviour, and the final comodel is the one-element set with the trivial co-operations.

  • For VV-valued read-only state, since 𝗀𝖾𝗍"()\mathsf{get}\gg({\mathord{\text{--}}}) is the identity operator, an admissible behaviour is uniquely determined by the value β(𝗀𝖾𝗍)V\beta(\mathsf{get})\in V. Any such choice yields an admissible behaviour, and so the final comodel is VV with the co-operation 𝗀𝖾𝗍=Δ:VV×V\llbracket\mathsf{get}\rrbracket=\Delta\colon V\rightarrow V\times V.

  • For VV-valued state, 𝗀𝖾𝗍"()\mathsf{get}\gg({\mathord{\text{--}}}) is again the identity operator, and so an admissible behaviour β\beta is determined by the values β(𝗉𝗎𝗍v1""𝗉𝗎𝗍vn"𝗀𝖾𝗍)V\beta(\mathsf{put}_{v_{1}}\gg\dots\gg\mathsf{put}_{v_{n}}\gg\mathsf{get})\in V for v1,,vnVv_{1},\dots,v_{n}\in V. When n>0n>0, the 𝗉𝗎𝗍\mathsf{put} axioms force β(𝗉𝗎𝗍v1""𝗉𝗎𝗍vn"𝗀𝖾𝗍)=vn\beta(\mathsf{put}_{v_{1}}\gg\dots\gg\mathsf{put}_{v_{n}}\gg\mathsf{get})=v_{n} and so β\beta is uniquely determined by β(𝗀𝖾𝗍)V\beta(\mathsf{get})\in V. Thus again the final comodel is VV, with the same 𝗀𝖾𝗍\llbracket\mathsf{get}\rrbracket as before, and with 𝗉𝗎𝗍=π1:V×VV\llbracket\mathsf{put}\rrbracket=\pi_{1}\colon V\times V\rightarrow V.

5.3. The behaviour category of an accessible monad

We observed in the proof of Proposition 56 that if 𝖳\mathsf{T} is an accessible monad and 𝑺{\boldsymbol{S}} is a 𝖳\mathsf{T}-comodel in 𝒮et\mathrm{\mathcal{S}et}, then t𝑺(s)=(βs(t),t"id𝑺(s))\llbracket t\rrbracket_{{\boldsymbol{S}}}(s)=(\beta_{s}(t),\llbracket t\gg\mathrm{id}\rrbracket_{{\boldsymbol{S}}}(s)) for all s𝑺s\in{\boldsymbol{S}} and tT(A)t\in T(A). Thus 𝑺{\boldsymbol{S}} is completely determined by the two functions

(5.3) S\displaystyle S B𝖳\displaystyle\rightarrow B_{\mathsf{T}} S×T(1)\displaystyle S\times T(1) S\displaystyle\rightarrow S
s\displaystyle s βs\displaystyle\mapsto\beta_{s} (s,m)\displaystyle(s,m) m𝑺(s) .\displaystyle\mapsto\llbracket m\rrbracket_{{\boldsymbol{S}}}(s)\hbox to0.0pt{ .\hss}

giving the behaviour of each state, together with what we might call the dynamics of the comodel: the right action of unary operations on states. However, these two structures are not independent. One obvious restriction is that the right action by mT(1)m\in T(1) must send elements of behaviour β\beta to elements of behaviour mβ\partial_{m}\beta. However, due to (5.2) there is a further constraint; the following definition is intended to capture this.

Definition 58 (β\beta-equivalence).

Let 𝖳\mathsf{T} be an accessible monad, and let β\beta be an admissible 𝖳\mathsf{T}-behaviour. We say that unary operations m,nT(1)m,n\in T(1) are atomically β\beta-equivalent if there exists vT(A)v\in T(A) and m,nT(1)Am^{\prime},n^{\prime}\in T(1)^{A} such that

(5.4) m=v(m)andn=v(n)andmβ(v)=nβ(v) .m=v(m^{\prime})\quad\text{and}\quad n=v(n^{\prime})\quad\text{and}\quad m^{\prime}_{\beta(v)}=n^{\prime}_{\beta(v)}\hbox to0.0pt{ .\hss}

We write β\sim_{\beta} for the smallest equivalence relation on T(1)T(1) which identifies atomically β\beta-equivalent terms. Alternatively, β\sim_{\beta} is the smallest equivalence relation such that t(m)β(t"mβ(t))t(m)\sim_{\beta}(t\gg m_{\beta(t)}) for all tT(A)t\in T(A) and mT(1)Am\in T(1)^{A}.

Remark 59.

If we have a presentation of the accessible monad 𝖳\mathsf{T} by an algebraic theory 𝕋=(Σ,)\mathbb{T}=(\Sigma,{\mathcal{E}}), then we may simplify the task of computing the equivalence relation β\sim_{\beta} by observing that, by induction on the structure of 𝕋\mathbb{T}-terms, each mT(1)m\in T(1) is β\sim_{\beta}-equivalent to σ1""σn"id\sigma_{1}\gg\dots\gg\sigma_{n}\gg\mathrm{id} for some σ1,,σnΣ\sigma_{1},\dots,\sigma_{n}\in\Sigma.

The motivation for this definition is that β\sim_{\beta} will identify two unary operations if and only if they act in the same way on any state of behaviour β\beta. The “only if” direction is part (iii) of the following lemma; the “if” will be proved in Corollary 64.

Lemma 60.

Let β\beta be an admissible behaviour of the accessible monad 𝖳\mathsf{T}, and let m,n,pT(1)m,n,p\in T(1).

  1. (i)

    If mβnm\sim_{\beta}n then m.pβn.pm.p\sim_{\beta}n.p;

  2. (ii)

    If mpβnm\sim_{\partial_{p}\beta}n then p.mβp.np.m\sim_{\beta}p.n;

  3. (iii)

    If s𝑺s\in{\boldsymbol{S}} is a state of behaviour β\beta and mβnm\sim_{\beta}n, then m𝑺(s)=n𝑺(s)\llbracket m\rrbracket_{{\boldsymbol{S}}}(s)=\llbracket n\rrbracket_{{\boldsymbol{S}}}(s).

Here, and subsequently, if mT(1)m\in T(1) and tT(A)t\in T(A), we may write m.tm.t for the substitution m(t)m(t).

Proof.

For (i), if mm and nn are atomically β\beta-equivalent via v,m,nv,m^{\prime},n^{\prime}, then m.pm.p and n.pn.p are so via vv, (ma.p:aA)(m^{\prime}_{a}.p:a\in A) and (na.p:aA)(n^{\prime}_{a}.p:a\in A); whence mβnm\sim_{\beta}n implies m(p)βn(p)m(p)\sim_{\beta}n(p). (ii) is similar, observing that if m,nm,n are atomically pβ\partial_{p}\beta-equivalent via v,m,nv,m^{\prime},n^{\prime}, then p.m)p.m) and p.np.n are atomically β\beta-equivalent via p.v,m,np.v,m^{\prime},n^{\prime}. Finally, for (iii), if m,nm,n are atomically β\beta-equivalent, then writing s=v"id(s)s^{\prime}=\llbracket v\gg\mathrm{id}\rrbracket(s) we have

m(s)=v(m)(s)=mβ(v)(s)=nβ(v)(s)=v(n)(s)=n(s) .\llbracket m\rrbracket(s)=\llbracket v(m^{\prime})\rrbracket(s)=\llbracket m^{\prime}_{\beta(v)}\rrbracket(s^{\prime})=\llbracket n^{\prime}_{\beta(v)}\rrbracket(s^{\prime})=\llbracket v(n^{\prime})\rrbracket(s)=\llbracket n\rrbracket(s)\text{ .}\qed

With this in place, we can now give the main definition of this section:

Definition 61 (Behaviour category of a monad).

Let 𝖳\mathsf{T} be an accessible monad. The behaviour category 𝔹𝖳\mathbb{B}_{\mathsf{T}} of 𝖳\mathsf{T} has admissible behaviours as objects, and hom-sets

𝔹𝖳(β,β)={mT(1)β=mβ}/β .\mathbb{B}_{\mathsf{T}}(\beta,\beta^{\prime})=\{m\in T(1)\mid\beta^{\prime}=\partial_{m}\beta\}\delimiter 84079374\mathopen{}\mathord{\sim_{\beta}}\hbox to0.0pt{ .\hss}

Identities are given by the neutral element of T(1)T(1), and the composite of m:ββm\colon\beta\rightarrow\beta^{\prime} and n:ββ′′n\colon\beta^{\prime}\rightarrow\beta^{\prime\prime} is m.n:ββ′′m.n\colon\beta\rightarrow\beta^{\prime\prime}; note this is well-defined by Lemma 60(i–ii).

Theorem 62.

Given an accessible monad 𝖳\mathsf{T} with behaviour category 𝔹𝖳\mathbb{B}_{\mathsf{T}}, the category of 𝖳\mathsf{T}-comodels is isomorphic to the category of left 𝔹𝖳\mathbb{B}_{\mathsf{T}}-sets via an isomorphism commuting with the forgetful functors to 𝒮et\mathrm{\mathcal{S}et}:

(5.5)
𝒮𝖳et\textstyle{{}^{\mathsf{T}}\mathrm{\mathcal{S}et}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}U𝖳\scriptstyle{{}^{\mathsf{T}}U}\scriptstyle{\cong}𝔹𝖳-𝒮et .\textstyle{\mathbb{B}_{\mathsf{T}}\text{-}\mathrm{\mathcal{S}et}\hbox to0.0pt{ .\hss}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}U𝔹𝖳\scriptstyle{U^{\mathbb{B}_{\mathsf{T}}}}𝒮et\textstyle{\mathrm{\mathcal{S}et}}

For a comodel 𝐒{\boldsymbol{S}}, the corresponding left 𝔹𝖳\mathbb{B}_{\mathsf{T}}-set X𝐒X_{{\boldsymbol{S}}} has underlying set SS, projection to B𝖳B_{\mathsf{T}} given by the behaviour map β():SB𝖳\beta_{({\mathord{\text{--}}})}\colon S\rightarrow B_{\mathsf{T}}, and action given by

(5.6) (s,βs\ext@arrow01200\rightarrowfill@mmβs)m𝑺(s) ;(s,\beta_{s}\ext@arrow 01{20}0\rightarrowfill@{}{m}\partial_{m}\beta_{s})\mapsto\llbracket m\rrbracket_{{\boldsymbol{S}}}(s)\hbox to0.0pt{ ;\hss}

for a left 𝔹𝖳\mathbb{B}_{\mathsf{T}}-set (X,p,)(X,p,\ast), the corresponding comodel 𝐒X{\boldsymbol{S}}_{X} has underlying set XX and

(5.7) t𝑺X:x(β(t),tx)for all xp1β\llbracket t\rrbracket_{{\boldsymbol{S}}_{X}}\colon x\mapsto(\beta(t),t^{\flat}\ast x)\qquad\text{for all $x\in p^{-1}\beta$}

where we write tt^{\flat} for t"idt\gg\mathrm{id}.

Proof.

We concentrate on giving the isomorphism of categories at the level of objects; indeed, since it is to commute with the faithful functors to 𝒮et\mathrm{\mathcal{S}et}, to obtain the isomorphism on arrows we need only check that a function lifts along U𝖳{}^{\mathsf{T}}U just when it lifts along U𝔹𝖳U^{\mathbb{B}_{\mathsf{T}}}, and we leave this to the reader.

Now, on objects, one direction is easy: the action map (5.6) of the presheaf associated to a comodel is well-defined by Lemma 60(iii), and is clearly functorial. In the converse direction, we must prove that the 𝑺X{\boldsymbol{S}}_{X} associated to a left 𝔹𝖳\mathbb{B}_{\mathsf{T}}-set XX satisfies the comodel axioms in (2.8). For the first axiom, we have that a(s)=(β(a),as)=(a,s)=νa(s)\llbracket a\rrbracket(s)=(\beta(a),a^{\flat}\ast s)=(a,s)=\nu_{a}(s) for all sp1βs\in p^{-1}\beta, since β(a)=a\beta(a)=a and a=idT(1)a^{\flat}=\mathrm{id}\in T(1). For the second, given sp1βs\in p^{-1}\beta we must show t(u)(s)=(β(t(u)),t(u)s)\llbracket t(u)\rrbracket(s)=(\beta(t(u)),t(u)^{\flat}\ast s) is equal to

uaaA(t(s))=ua(β(t),ts)=uβ(t)(ts)=(tβ(uβ(t)),uβ(t)(ts)) .\langle\llbracket u_{a}\rrbracket\rangle_{a\in A}(\llbracket t\rrbracket(s))=\langle\llbracket u_{a}\rrbracket\rangle(\beta(t),t^{\flat}\ast s)=\llbracket u_{\beta(t)}\rrbracket(t^{\flat}\ast s)=(\partial_{t}\beta(u_{\beta(t)}),u_{\beta(t)}^{\flat}\ast(t^{\flat}\ast s))\hbox to0.0pt{ .\hss}

But in the first component β(t(u))=β(t"uβ(t))=tβ(uβ(t))\beta(t(u))=\beta(t\gg u_{\beta(t)})=\partial_{t}\beta(u_{\beta(t)}); while in the second, since uβ(t)(ts)=(uβ(t)t)s=t(uβ(t))su_{\beta(t)}^{\flat}\ast(t^{\flat}\ast s)=(u_{\beta(t)}^{\flat}\circ t^{\flat})\ast s=t^{\flat}(u_{\beta(t)}^{\flat})\ast s, it suffices to show that we have t(uβ(t))=t(u):βtβt^{\flat}({u}_{\beta(t)}^{\flat})=t(u)^{\flat}\colon\beta\rightarrow\partial_{t}\beta in 𝔹𝖳\mathbb{B}_{\mathsf{T}}; but

t(uβ(t))=t(λa.uβ(t))andt(u)=t(λa.ua)t^{\flat}(u^{\flat}_{\beta(t)})=t(\lambda a.\,u^{\flat}_{\beta(t)})\quad\text{and}\quad t(u)^{\flat}=t(\lambda a.\,u^{\flat}_{a})

and these two terms are clearly atomically β\beta-equivalent, and so equal as maps in 𝔹𝖳\mathbb{B}_{\mathsf{T}}. This shows that 𝑺X{\boldsymbol{S}}_{X} is a 𝖳\mathsf{T}-comodel.

It remains to show that these two assignments are mutually inverse. For any comodel 𝑺{\boldsymbol{S}}, the comodel 𝑺X𝑺{\boldsymbol{S}}_{X_{{\boldsymbol{S}}}} clearly has the same underlying set, but also the same comodel structure, since

t𝑺X𝑺(s)=(βs(t),ts)=(βs(t),t"id𝑺(s))=t𝑺(s) .\llbracket t\rrbracket_{{\boldsymbol{S}}_{X_{{\boldsymbol{S}}}}}(s)=(\beta_{s}(t),t^{\flat}\ast s)=(\beta_{s}(t),\llbracket t\gg\mathrm{id}\rrbracket_{{\boldsymbol{S}}}(s))=\llbracket t\rrbracket_{{\boldsymbol{S}}}(s)\hbox to0.0pt{ .\hss}

On the other hand, for any 𝔹𝖳\mathbb{B}_{\mathsf{T}}-set XX, the 𝔹𝖳\mathbb{B}_{\mathsf{T}}-set X𝑺XX_{{\boldsymbol{S}}_{X}} has the same underlying set, but also the same projection to B𝖳B_{\mathsf{T}}, since (5.7) exhibits each x𝑺Xx\in{\boldsymbol{S}}_{X} as having behaviour p(x)p(x); and the same action map, since mX𝑺Xs=m𝑺X(s)=mXsm\ast_{X_{{\boldsymbol{S}}_{X}}}s=\llbracket m\rrbracket_{{\boldsymbol{S}}_{X}}(s)=m\ast_{X}s. ∎

Corollary 63.

Let 𝖳\mathsf{T} be an accessible monad. For each 𝖳\mathsf{T}-admissible behaviour β\beta, there exists a comodel 𝛃{\boldsymbol{\beta}} freely generated by a state of behaviour β\beta; it has underlying set T(1)/βT(1)\delimiter 84079374\mathopen{}\sim_{\beta}, co-operations t𝛃(m)=(β(m.t),m.t)\llbracket t\rrbracket_{{\boldsymbol{\beta}}}(m)=(\beta(m.t),m.t^{\flat}), and generating state id𝛃\mathrm{id}\in{\boldsymbol{\beta}}. Morphisms ββ\beta\rightarrow\beta^{\prime} in the behaviour category of 𝖳\mathsf{T} are in functorial bijection with comodel homomorphisms 𝛃𝛃{\boldsymbol{\beta^{\prime}}}\rightarrow{\boldsymbol{\beta}}.

Proof.

We obtain 𝜷{\boldsymbol{\beta}} as the image of the representable functor 𝔹(β,)\mathbb{B}(\beta,{\mathord{\text{--}}}) under the equivalence of Theorem 62. The final clause follows from the Yoneda lemma. ∎

We now tie up a loose end by proving the converse to Lemma 60(iii).

Corollary 64.

Let 𝖳\mathsf{T} be an algebraic theory, β\beta an admissible 𝖳\mathsf{T}-behaviour, and m,nT(1)m,n\in T(1). We have mβnm\sim_{\beta}n if, and only if, for every 𝖳\mathsf{T}-comodel 𝐒{\boldsymbol{S}} and state s𝐒s\in{\boldsymbol{S}} of behaviour β\beta, we have m𝐒(s)=n𝐒(s)\llbracket m\rrbracket_{{\boldsymbol{S}}}(s)=\llbracket n\rrbracket_{{\boldsymbol{S}}}(s).

Proof.

The “only if” direction is Lemma 60(iii). For the “if” direction, consider the 𝖳\mathsf{T}-comodel 𝜷{\boldsymbol{\beta}} classifying states of behaviour β\beta and the state id𝜷\mathrm{id}\in{\boldsymbol{\beta}}. Then we have m=m𝜷(id)=n𝜷(id)=nm=\llbracket m\rrbracket_{{\boldsymbol{\beta}}}(\mathrm{id})=\llbracket n\rrbracket_{{\boldsymbol{\beta}}}(\mathrm{id})=n in T(1)/βT(1)\delimiter 84079374\mathopen{}\sim_{\beta} and so mβnm\sim_{\beta}n as desired. ∎

Remark 65.

In computing the comodel 𝜷{\boldsymbol{\beta}} classifying an admissible behaviour β\beta, the main problem is to determine suitable equivalence-class representatives for elements of the underlying set T(1)/βT(1)\delimiter 84079374\mathopen{}\sim_{\beta}. Suppose we have a subset {id}ST(1)\{\mathrm{id}\}\subseteq S\subseteq T(1) which we believe constitutes a set of such representatives. By Corollary 63, a necessary condition for this belief to be correct is that SS underlie a comodel 𝑺{\boldsymbol{S}} with

(5.8) t𝑺(s)=(β(s.t),s)for some sS with sβs.t ;\llbracket t\rrbracket_{{\boldsymbol{S}}}(s)=(\beta(s.t),s^{\prime})\qquad\text{for some $s^{\prime}\in S$ with $s^{\prime}\sim_{\beta}s.t^{\flat}$ ;}

of course, this 𝑺{\boldsymbol{S}} will then be the desired classifier for states of behaviour β\beta.

In fact, the preceding result tells us that this necessary condition is also sufficient. Indeed, if SS bears a comodel structure satisfying (5.8), then for each mT(1)m\in T(1) we have m𝑺(id)S\llbracket m\rrbracket_{{\boldsymbol{S}}}(\mathrm{id})\in S in the β\sim_{\beta}-equivalence class of mm; moreover, since id𝑺\mathrm{id}\in{\boldsymbol{S}} clearly has behaviour β\beta, if ssSs\neq s^{\prime}\in S, then s𝑺(id)=ss=s𝑺(id)\llbracket s\rrbracket_{{\boldsymbol{S}}}(\mathrm{id})=s\neq s^{\prime}=\llbracket s^{\prime}\rrbracket_{{\boldsymbol{S}}}(\mathrm{id}), whence sβss\nsim_{\beta}s^{\prime} by Corollary 64.

Example 66.

For each of our running examples of algebraic theories, we compute the comodels classifying each admissible behaviour, and so the behaviour category. For these examples, it is simple enough to find the classifying comodels directly without exploiting Remark 65.

  • For VV-valued input, the object-set of the behaviour category is VV^{\mathbb{N}}, and for each behaviour WVW\in V^{\mathbb{N}}, the comodel 𝑾{\boldsymbol{W}} classifying this behaviour may be taken to have underlying set \mathbb{N} with co-operation 𝗋𝖾𝖺𝖽(n)=(Wn,n+1)\llbracket\mathsf{read}\rrbracket(n)=(W_{n},n+1); the universal state of behaviour WW is then 0𝑾0\in{\boldsymbol{W}}. Morphisms WWW\rightarrow W^{\prime} in the behaviour category correspond to states of 𝑾{\boldsymbol{W}} of behaviour WW^{\prime}, and these can be identified with natural numbers ii such that Wn=Wn+iW^{\prime}_{n}=W_{n+i} for all kk\in\mathbb{N}.

  • For VV-valued output, the behaviour category has a single object \ast, and the comodel 𝑽{\boldsymbol{V^{\ast}}} classifying this unique behaviour has as underlying set the free monoid VV^{\ast}, with co-operations 𝗐𝗋𝗂𝗍𝖾v(W)=Wv\llbracket\mathsf{write}_{v}\rrbracket(W)=Wv; the universal state is the empty word ε𝑽\varepsilon\in{\boldsymbol{V^{\ast}}}. Endomorphisms of \ast in the behaviour category correspond to states of 𝑽{\boldsymbol{V^{\ast}}}, so that the behaviour category is precisely the the one-object category corresponding to the monoid VV^{\ast}.

  • For VV-valued read-only state, the behaviour category has object-set VV, and the comodel classifying vVv\in V is the one-element comodel 𝒗{\boldsymbol{v}} with 𝗀𝖾𝗍()=(v,)\llbracket\mathsf{get}\rrbracket(\ast)=(v,\ast). Clearly, there are no non-identity homomorphisms between such comodels, so that the behaviour category is the discrete category on the set VV.

  • For VV-valued state, the behaviour category again has object-set VV, while the comodel 𝒗{\boldsymbol{v}} classifying any vVv\in V is the final comodel 𝑽{\boldsymbol{V}}, with universal state vv. Maps vvv\rightarrow v^{\prime} in the behaviour category thus correspond to comodel homomorphisms 𝑽𝑽{\boldsymbol{V}}\rightarrow{\boldsymbol{V}}, and since 𝑽{\boldsymbol{V}} is final, the only such is the identity. Thus the behaviour category is the codiscrete category on VV, with a unique arrow between every two objects.

5.4. Functoriality

By Theorem 62, the assignment 𝖳𝔹𝖳\mathsf{T}\mapsto\mathbb{B}_{\mathsf{T}} is the action on objects of the desired factorisation of the cosemantics functor nda(𝒮et)op𝒞mda(𝒮et)\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}) through 𝖰():𝒞of𝒞mda(𝒮et)\mathsf{Q}_{({\mathord{\text{--}}})}\colon\mathrm{\mathcal{C}of}\rightarrow\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}). We conclude this section by describing the corresponding action on morphisms.

Proposition 67.

Let 𝖳\mathsf{T} and 𝖱\mathsf{R} be accessible monads on 𝒮et\mathrm{\mathcal{S}et}. For each monad morphism f:𝖳𝖱f\colon\mathsf{T}\rightarrow\mathsf{R}, there is a cofunctor 𝔹f:𝔹𝖱𝔹𝖳\mathbb{B}_{f}\colon\mathbb{B}_{\mathsf{R}}\rightsquigarrow\mathbb{B}_{\mathsf{T}} which acts on objects by βfβ\beta\mapsto f^{\ast}\beta; and which, on morphisms, given β𝔹𝖱\beta\in\mathbb{B}_{\mathsf{R}}, acts by sending m:fβm(fβ)m\colon f^{\ast}\beta\rightarrow\partial_{m}(f^{\ast}\beta) in 𝔹𝖳\mathbb{B}_{\mathsf{T}} to (𝔹f)β(m):=f(m):βf(m)β(\mathbb{B}_{f})_{\beta}(m)\mathrel{\mathop{:}}=f(m)\colon\beta\rightarrow\partial_{f(m)}\beta in 𝔹𝖱\mathbb{B}_{\mathsf{R}}.

Proof.

f:𝔹𝖱𝔹𝖳f^{\ast}\colon\mathbb{B}_{\mathsf{R}}\rightsquigarrow\mathbb{B}_{\mathsf{T}} is well-defined on objects by the lemma below. For well-definedness on maps, we must show that mfβnm\sim_{f^{\ast}\beta}n in T(1)T(1) implies f(m)βf(n)f(m)\sim_{\beta}f(n) in R(1)R(1). Clearly it suffices to do so when mm and nn are atomically fβf^{\ast}\beta-equivalent via terms vT(A)v\in T(A) and m,nT(1)Am^{\prime},n^{\prime}\in T(1)^{A} satisfying

m=v(m)andn=v(n)andmfβ(v)=nfβ(v) .m=v(m^{\prime})\qquad\text{and}\qquad n=v(n^{\prime})\qquad\text{and}\qquad m^{\prime}_{f^{\ast}\beta(v)}=n^{\prime}_{f^{\ast}\beta(v)}\hbox to0.0pt{ .\hss}

But since fβ(v)=β(f(v))f^{\ast}\beta(v)=\beta(f(v)), it follows that f(v)R(A)f(v)\in R(A) and f(m()),f(n())R(1)Af(m^{\prime}_{({\mathord{\text{--}}})}),f(n^{\prime}_{({\mathord{\text{--}}})})\in R(1)^{A} witness f(m)f(m) and f(n)f(n) as atomically β\beta-equivalent, as required. We must also check the three cofunctor axioms. The first holds since f(f(m)β)(t)=(f(m)β)(f(t))=β(f(m).f(t))=β(f(m.t))=fβ(m.t)=m(fβ)(t)f^{\ast}(\partial_{f(m)}\beta)(t)=(\partial_{f(m)}\beta)(f(t))=\beta(f(m).f(t))=\beta(f(m.t))=f^{\ast}\beta(m.t)=\partial_{m}(f^{\ast}\beta)(t). The other two are immediate since ff preserves substitution. ∎

In the following lemma, recall from Definition 24 that each monad morphism f:𝖳𝖱f\colon\mathsf{T}\rightarrow\mathsf{R} induces a functor on comodels f:𝒮𝖱et𝒮𝖳etf^{\ast}\colon{}^{\mathsf{R}}\mathrm{\mathcal{S}et}\rightarrow{}^{\mathsf{T}}\mathrm{\mathcal{S}et}.

Lemma 68.

Let f:𝖳𝖱f\colon\mathsf{T}\rightarrow\mathsf{R} in nda(𝒮et)\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et}), and let 𝐒{\boldsymbol{S}} be a 𝖱\mathsf{R}-comodel. If s𝐒s\in{\boldsymbol{S}} has 𝖱\mathsf{R}-behaviour β\beta, then sf𝐒s\in f^{\ast}{\boldsymbol{S}} has 𝖳\mathsf{T}-behaviour fβf^{\ast}\beta where (fβ)(t)=β(f(t))(f^{\ast}\beta)(t)=\beta(f(t)).

Proof.

For any tT(A)t\in T(A), we have π1(tf𝑺(s))=π1(f(t)𝑺(s))=β(f(t))\pi_{1}(\llbracket t\rrbracket_{f^{\ast}{\boldsymbol{S}}}(s))=\pi_{1}(\llbracket f(t)\rrbracket_{{\boldsymbol{S}}}(s))=\beta(f(t)). ∎

We now prove that the cofunctor 𝔹f\mathbb{B}_{f} of Proposition 67 does indeed describe the action on morphisms of the cosemantics functor.

Theorem 69.

The functor 𝔹():nda(𝒮et)op𝒞of\mathbb{B}_{({\mathord{\text{--}}})}\colon\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\rightarrow\mathrm{\mathcal{C}of} taking each accessible monad 𝖳\mathsf{T} to its behaviour category 𝔹𝖳\mathbb{B}_{\mathsf{T}}, and each map of accessible monads f:𝖳𝖱f\colon\mathsf{T}\rightarrow\mathsf{R} to the cofunctor 𝔹f:𝔹𝖱𝔹𝖳\mathbb{B}_{f}\colon\mathbb{B}_{\mathsf{R}}\rightsquigarrow\mathbb{B}_{\mathsf{T}} of Proposition 67, yields a within-isomorphism factorisation

\textstyle{\cong}𝒞of\textstyle{\mathrm{\mathcal{C}of}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}𝖰()\scriptstyle{\mathsf{Q}_{({\mathord{\text{--}}})}}nda(𝒮et)op\textstyle{\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Cosem\scriptstyle{\mathrm{Cosem}}𝔹()\scriptstyle{\mathbb{B}_{({\mathord{\text{--}}})}}𝒞mda(𝒮et) .\textstyle{\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})\hbox to0.0pt{ .\hss}}
Proof.

It suffices to show that, for any map of accessible monads f:𝖳𝖱f\colon\mathsf{T}\rightarrow\mathsf{R}, the associated cofunctor 𝔹f:𝔹𝔹𝖳\mathbb{B}_{f}\colon\mathbb{B}_{\mathbb{R}}\rightsquigarrow\mathbb{B}_{\mathsf{T}} renders commutative the square

𝒮𝖱et\textstyle{{{}^{\mathsf{R}}\mathrm{\mathcal{S}et}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}\scriptstyle{\cong}f\scriptstyle{f^{\ast}}𝔹𝖱-𝒮et\textstyle{{\mathbb{B}_{\mathsf{R}}\text{-}\mathrm{\mathcal{S}et}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Σ𝔹f\scriptstyle{\Sigma_{\mathbb{B}_{f}}}𝒮𝖳et\textstyle{{{}^{\mathsf{T}}\mathrm{\mathcal{S}et}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}\scriptstyle{\cong}𝔹𝖳-𝒮et\textstyle{{\mathbb{B}_{\mathsf{T}}\text{-}\mathrm{\mathcal{S}et}}}

whose horizontal edges are the isomorphisms of Theorem 62. But indeed, given an 𝖱\mathsf{R}-comodel 𝑺{\boldsymbol{S}}, its image around the lower composite is by Lemma 68 the 𝔹𝖳\mathbb{B}_{\mathsf{T}}-set with underlying set SS and projection and action maps

S\displaystyle S B𝖳\displaystyle\rightarrow B_{\mathsf{T}} sST(1)/f(βs)\displaystyle\textstyle\sum_{s\in S}T(1)\delimiter 84079374\mathopen{}\sim_{f^{\ast}(\beta_{s})} S\displaystyle\rightarrow S
s\displaystyle s f(βs)\displaystyle\mapsto f^{\ast}(\beta_{s}) (s,m)\displaystyle(s,m) f(m)𝑺(s) .\displaystyle\mapsto\llbracket f(m)\rrbracket_{{\boldsymbol{S}}}(s)\hbox to0.0pt{ .\hss}

On the other hand, the upper composite first sends 𝑺{\boldsymbol{S}} to the 𝔹𝖱\mathbb{B}_{\mathsf{R}}-set with underlying set SS and projection and action maps

S\displaystyle S B𝖱\displaystyle\rightarrow B_{\mathsf{R}} sSR(1)/βs\displaystyle\textstyle\sum_{s\in S}R(1)\delimiter 84079374\mathopen{}\sim_{\beta_{s}} S\displaystyle\rightarrow S
s\displaystyle s βs\displaystyle\mapsto\beta_{s} (s,n)\displaystyle(s,n) n𝑺(s) ;\displaystyle\mapsto\llbracket n\rrbracket_{{\boldsymbol{S}}}(s)\hbox to0.0pt{ ;\hss}

and then applies Σ𝔹f\Sigma_{\mathbb{B}_{f}}, which, by the definition of Σ()\Sigma_{({\mathord{\text{--}}})} and of 𝔹f\mathbb{B}_{f}, yields the same 𝔹𝖳\mathbb{B}_{\mathsf{T}}-set as above. ∎

Example 70.

Let h:VWh\colon V\rightarrow W be a function, and let f:𝕋1𝕋2f\colon\mathbb{T}_{1}\rightarrow\mathbb{T}_{2} be the associated interpretation of VV-valued output into WW-valued state of Example 8. The induced cofunctor f:𝔹𝕋2𝔹𝕋1f^{\ast}\colon\mathbb{B}_{\mathbb{T}_{2}}\rightarrow\mathbb{B}_{\mathbb{T}_{1}} has as domain the codiscrete category on WW, and as codomain, the monoid VV^{\ast} seen as a one-object category. On objects, ff^{\ast} acts in the unique possible way; while on morphisms, given w𝔹𝕋2w\in\mathbb{B}_{\mathbb{T}_{2}} and a map f(w)f^{\ast}(w)\rightarrow\ast in 𝔹𝕋1\mathbb{B}_{\mathbb{T}_{1}}—corresponding to an element v1vnVv_{1}\dots v_{n}\in V^{\ast}—we have fw(v1vn)f^{\ast}_{w}(v_{1}\dots v_{n}) in 𝔹𝕋2\mathbb{B}_{\mathbb{T}_{2}} given by the unique map wvnw\rightarrow v_{n}.

Example 71.

Let h:WVh\colon W\rightarrow V be a function between sets, and let f:𝕋1𝕋2f\colon\mathbb{T}_{1}\rightarrow\mathbb{T}_{2} be the associated interpretation of VV-valued read-only state into WW-valued state of Example 9. The induced cofunctor f:𝔹𝕋2𝔹𝕋1f^{\ast}\colon\mathbb{B}_{\mathbb{T}_{2}}\rightarrow\mathbb{B}_{\mathbb{T}_{1}} has as domain the codiscrete category on WW, and as codomain the discrete category on VV. On objects, ff^{\ast} acts by wh(v)w\mapsto h(v), while on morphisms it acts in the unique possible way.

6. Calculating the costructure functor

6.1. The behaviour category of an accessible comonad

In this section, we give an explicit calculation of the costructure functor from comonads to monads. Much as for the cosemantics functor, we will see that cosemantics takes its values in presheaf monads, and will explicitly associate to each accessible comonad 𝖰\mathsf{Q} a small category 𝔹𝖰\mathbb{B}_{\mathsf{Q}}, the behaviour category, such that Costr(𝖰)\mathrm{Costr}(\mathsf{Q}) is the presheaf monad of 𝔹𝖰\mathbb{B}_{\mathsf{Q}}. We begin with some preliminary observations.

Notation 72.

Let 𝖰\mathsf{Q} be an accessible comonad on 𝒮et\mathrm{\mathcal{S}et}. For each xQ1x\in Q1, we write ιx:QxQ\iota_{x}\colon Q_{x}\rightarrow Q for the inclusion of the subfunctor with

Qx(A)={aQA:(Q!)(a)=x in Q1} .Q_{x}(A)=\{a\in QA:(Q!)(a)=x\text{ in }Q1\}\hbox to0.0pt{ .\hss}

We also write εx:Qx1\varepsilon_{x}\colon Q_{x}\rightarrow 1 and δx:QxQxQ\delta_{x}\colon Q_{x}\rightarrow Q_{x}Q for the natural transformations such that εx=ειx\varepsilon_{x}=\varepsilon\circ\iota_{x} and ιxQδx=διx\iota_{x}Q\circ\delta_{x}=\delta\circ\iota_{x}; to see that διx\delta\circ\iota_{x} does indeed factor through ιxQ\iota_{x}Q, we note that, for any aQxAa\in Q_{x}A, the element δ(a)QQA\delta(a)\in QQA satisfies (Q!)(δ(a))=(Q!)(Qε(δ(a)))=(Q!)(a)=x(Q!)(\delta(a))=(Q!)(Q\varepsilon(\delta(a)))=(Q!)(a)=x so that δ(a)QxQA\delta(a)\in Q_{x}QA as desired.

Lemma 73.

Let 𝖰\mathsf{Q} be an accessible comonad on 𝒮et\mathrm{\mathcal{S}et}.

  1. (i)

    The inclusions ιx:QxQ\iota_{x}\colon Q_{x}\rightarrow Q exhibit QQ as the coproduct xQ1Qx\sum_{x\in Q1}Q_{x};

  2. (ii)

    Any natural transformation f:QxiFif\colon Q_{x}\rightarrow\sum_{i}F_{i} into a coproduct factors through exactly one coproduct injection νi:FiiFi\nu_{i}\colon F_{i}\rightarrow\sum_{i}F_{i}.

Proof.

(i) holds as each QAQA is the coproduct of the QxAQ_{x}A’s, and coproducts in [𝒮et,𝒮et][\mathrm{\mathcal{S}et},\mathrm{\mathcal{S}et}] are componentwise. For (ii), the component f1:{x}iIFi1f_{1}\colon\{x\}\rightarrow\sum_{i\in I}F_{i}1 of ff clearly factors through just one νi\nu_{i}; now naturality of ff with respect to the unique maps !:A1!\colon A\rightarrow 1 shows that each fAf_{A} factors through just the same νi\nu_{i}. ∎

Definition 74 (Behaviour category of a comonad).

Let 𝖰\mathsf{Q} be an accessible comonad on 𝒮et\mathrm{\mathcal{S}et}. The behaviour category of 𝖰\mathsf{Q} is the small category 𝔹𝖰\mathbb{B}_{\mathsf{Q}} in which:

  • Objects are elements of Q1Q1;

  • Maps with domain xQ1x\in Q1 are natural transformations τ:Qxid\tau\colon Q_{x}\rightarrow\mathrm{id}, and the codomain of such a τ\tau is determined by the following factorisation, whose (unique) existence is asserted by Lemma 73:

    (6.1) Qx\textstyle{Q_{x}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}δx\scriptstyle{\delta_{x}}τ\scriptstyle{\tau^{\sharp}}Qcod(τ)\textstyle{Q_{\mathrm{cod}(\tau)}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}ιcod(τ)\scriptstyle{\iota_{\mathrm{cod}(\tau)}}QxQ\textstyle{Q_{x}Q\ignorespaces\ignorespaces\ignorespaces\ignorespaces}τQ\scriptstyle{\tau Q}Q ;\textstyle{Q\hbox to0.0pt{ ;\hss}}
  • The identity on xQ1x\in Q1 is εx:Qxid\varepsilon_{x}\colon Q_{x}\rightarrow\mathrm{id};

  • Binary composition is given as follows, where τ\tau^{\sharp} is the factorisation in (6.1):

    (Qcod(τ)\ext@arrow01200\rightarrowfill@υid)(Qx\ext@arrow01200\rightarrowfill@τid)=(Qx\ext@arrow01200\rightarrowfill@τQcod(τ)\ext@arrow01200\rightarrowfill@υid) .\smash{(Q_{\mathrm{cod}(\tau)}\ext@arrow 01{20}0\rightarrowfill@{}{\upsilon}\mathrm{id})\circ(Q_{x}\ext@arrow 01{20}0\rightarrowfill@{}{\tau}\mathrm{id})=(Q_{x}\ext@arrow 01{20}0\rightarrowfill@{}{\tau^{\sharp}}Q_{\mathrm{cod}(\tau)}\ext@arrow 01{20}0\rightarrowfill@{}{\upsilon}\mathrm{id})}\hbox to0.0pt{ .\hss}

The axioms expressing unitality and associativity of composition follow from the familiar and easily-checked identities εcod(τ)τ=τ\varepsilon_{\mathrm{cod}(\tau)}\circ\tau^{\sharp}=\tau, εx=1Qx\varepsilon_{x}^{\sharp}=1_{Q_{x}} and (υτ)=υτ(\upsilon\circ\tau^{\sharp})^{\sharp}=\upsilon^{\sharp}\circ\tau^{\sharp}.

We now show that the costructure monad associated to an accessible comonad 𝖰\mathsf{Q} is isomorphic to the presheaf monad 𝖳𝔹𝖰\mathsf{T}_{\mathbb{B}_{\mathsf{Q}}}. In light of Proposition 49, it suffices to construct an isomorphism between 𝖳𝔹𝖰\mathsf{T}_{\mathbb{B}_{\mathsf{Q}}} and the dual monad 𝖰\mathsf{Q}^{\circ} of Definition 47.

Proposition 75.

Let 𝖰\mathsf{Q} be an accessible comonad. For any τ:QAid\tau\colon Q\rightarrow A\cdot\mathrm{id} and any xQ1x\in Q1, there is a unique axAa_{x}\in A and τx:Qxid\tau_{x}\colon Q_{x}\rightarrow\mathrm{id} for which we have a factorisation

(6.2) Qx\textstyle{Q_{x}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}ιx\scriptstyle{\iota_{x}}τx\scriptstyle{\tau_{x}}id\textstyle{\mathrm{id}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}νax\scriptstyle{\nu_{a_{x}}}Q\textstyle{Q\ignorespaces\ignorespaces\ignorespaces\ignorespaces}τ\scriptstyle{\tau}Aid .\textstyle{A\cdot\mathrm{id}\hbox to0.0pt{ .\hss}}

In this way, we obtain a monad isomorphism θ:𝖰𝖳𝔹𝖰\theta\colon\mathsf{Q}^{\circ}\cong\mathsf{T}_{\mathbb{B}_{\mathsf{Q}}} with components

(6.3) θA:[𝒮et,𝒮et](Q,Aid)\displaystyle\theta_{A}\colon[\mathrm{\mathcal{S}et},\mathrm{\mathcal{S}et}](Q,A\cdot\mathrm{id}) x𝔹𝖰(A×(𝔹𝖰)x)\displaystyle\rightarrow\textstyle\prod_{x\in\mathbb{B}_{\mathsf{Q}}}(A\times(\mathbb{B}_{\mathsf{Q}})_{x})
τ\displaystyle\tau λx.(ax,τx) .\displaystyle\mapsto\lambda x.\,(a_{x},\tau_{x})\hbox to0.0pt{ .\hss}
Proof.

The existence and uniqueness of the factorisation (6.2) is a consequence of Lemma 73(ii); now that the induced maps (6.3) constitute a natural isomorphism follows from Lemma 73(i). It remains to show that these maps are the components of a monad isomorphism 𝖰𝖳𝔹𝖰\mathsf{Q}^{\circ}\cong\mathsf{T}_{\mathbb{B}_{\mathsf{Q}}}.

For compatibility with units, we have on the one hand that ηA𝖳𝔹𝖰(a)=λx.(a,εx)\eta^{\mathsf{T}_{\smash{\mathbb{B}_{\mathsf{Q}}}}}_{A}(a)=\lambda x.\,(a,\varepsilon_{x}). On the other hand, ηA𝖰(a)=νaε:QidAid\eta^{\mathsf{Q}^{\circ}}_{A}(a)=\nu_{a}\circ\varepsilon\colon Q\rightarrow\mathrm{id}\rightarrow A\cdot\mathrm{id}, whose factorisation as in (6.2) is clearly ηA𝖰(a)ιx=νaεx\eta^{\mathsf{Q}^{\circ}}_{A}(a)\circ\iota_{x}=\nu_{a}\circ\varepsilon_{x}, so that θA(ηA𝖰(a))=λx.(a,εx)\theta_{A}(\eta^{\mathsf{Q}^{\circ}}_{A}(a))=\lambda x.\,(a,\varepsilon_{x}) as desired.

We now show compatibility with multiplication. To this end, consider an element σ:QQAid\sigma\colon Q\rightarrow Q^{\circ}A\cdot\mathrm{id} of QQAQ^{\circ}Q^{\circ}A. For each xQ1x\in Q1, we have an element τxQA\tau_{x}\in Q^{\circ}A and a natural transformation σx:Qx1\sigma_{x}\colon Q_{x}\rightarrow 1 rendering commutative the square to the left in

(6.4) Qx\textstyle{Q_{x}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}σx\scriptstyle{\sigma_{x}}ιx\scriptstyle{\iota_{x}}id\textstyle{\mathrm{id}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}ντx\scriptstyle{\nu_{\tau_{x}}}Qy\textstyle{Q_{y}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}τxy\scriptstyle{\tau_{xy}}ι\scriptstyle{\iota}id\textstyle{\mathrm{id}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}νaxy\scriptstyle{\nu_{a_{xy}}}Q\textstyle{Q\ignorespaces\ignorespaces\ignorespaces\ignorespaces}σ\scriptstyle{\sigma}QAid\textstyle{Q^{\circ}A\cdot\mathrm{id}}Q\textstyle{Q\ignorespaces\ignorespaces\ignorespaces\ignorespaces}τx\scriptstyle{\tau_{x}}Aid .\textstyle{A\cdot\mathrm{id}\hbox to0.0pt{ .\hss}}

Considering now τxQA\tau_{x}\in Q^{\circ}A, we have for each yQ1y\in Q1 an element axyAa_{xy}\in A and τxy:Qy1\tau_{xy}\colon Q_{y}\rightarrow 1 rendering commutative the square above right. With this notation, the composite μA𝖳𝔹𝖰(θθ)A\mu_{A}^{\mathsf{T}_{\smash{\mathbb{B}_{\mathsf{Q}}}}}\circ(\theta\theta)_{A} acts on σQQA\sigma\in Q^{\circ}Q^{\circ}A via

σ(θθ)Aλx.(σx,λy.(τxy,axy))μA𝖳𝔹𝖰λx.(τx,cod(σx)σx,ax,cod(σx)) .\smash{\sigma\ \xmapsto{\ \ (\theta\theta)_{A}\ \ }\ \lambda x.\,(\sigma_{x},\lambda y.\,(\tau_{xy},a_{xy}))\ \xmapsto{\ \ \mu_{A}^{\mathsf{T}_{\smash{\mathbb{B}_{\mathsf{Q}}}}}\ \ }\ \lambda x.\,(\tau_{x,\mathrm{cod}(\sigma_{x})}\circ\sigma_{x}^{\sharp},a_{x,\mathrm{cod}(\sigma_{x})})\hbox to0.0pt{ .\hss}}

We must show that this is equal to the image of σ\sigma under the composite θAμA𝖰\theta_{A}\circ\mu^{\mathsf{Q}^{\circ}}_{A}. From the description of 𝖰\mathsf{Q}^{\circ} in Definition 47, we can read off that μA𝖰(σ)QA\mu^{\mathsf{Q^{\circ}}}_{A}(\sigma)\in Q^{\circ}A is the lower composite in the diagram

Qx\textstyle{Q_{x}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}σx\scriptstyle{\sigma_{x}^{\sharp}}δx\scriptstyle{\delta_{x}}ιx\scriptstyle{\iota_{x}}Qcod(σx)\textstyle{Q_{\mathrm{cod}(\sigma_{x})}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}ιcod(σx)\scriptstyle{\iota_{\mathrm{cod}(\sigma_{x})}}τx,cod(σx)\scriptstyle{\tau_{x,\mathrm{cod}(\sigma_{x})}}id\textstyle{\mathrm{id}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}νax,cod(σx)\scriptstyle{\nu_{a_{x,\mathrm{cod}(\sigma_{x})}}}Q\textstyle{Q\ignorespaces\ignorespaces\ignorespaces\ignorespaces}δ\scriptstyle{\delta}QxQ\textstyle{Q_{x}Q\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}σxQ\scriptstyle{\sigma_{x}Q}ιxQ\scriptstyle{\iota_{x}Q}Q\textstyle{Q\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}ντx\scriptstyle{\nu_{\tau_{x}}}τx\scriptstyle{\tau_{x}}Qid .\textstyle{Q\cdot\mathrm{id}\hbox to0.0pt{ .\hss}}QQ\textstyle{QQ\ignorespaces\ignorespaces\ignorespaces\ignorespaces}σQ\scriptstyle{\sigma Q}QAQ\textstyle{Q^{\circ}A\cdot Q\ignorespaces\ignorespaces\ignorespaces\ignorespaces}𝖾𝗏\scriptstyle{\mathsf{ev}}

where 𝖾𝗏\mathsf{ev} is unique such that 𝖾𝗏ντ=τ\mathsf{ev}\circ\nu_{\tau}=\tau for all τQA\tau\in Q^{\circ}A. To calculate the image of μA𝖰(σ)\mu^{\mathsf{Q^{\circ}}}_{A}(\sigma) under θA\theta_{A}, we observe that, in the displayed diagram, the far left region is the definition of δx\delta_{x}, the two upper squares are instances of (6.1) and (6.2), the lower square is ()Q({\mathord{\text{--}}})Q of another instance of (6.2), and the triangle is definition of 𝖾𝗏\mathsf{ev}. So by unicity in (6.2), θA(μA𝖰(σ))=λx.(τx,cod(σx)σx,ax,cod(σx))\theta_{A}(\mu^{\mathsf{Q^{\circ}}}_{A}(\sigma))=\lambda x.\,(\tau_{x,\mathrm{cod}(\sigma_{x})}\circ\sigma_{x}^{\sharp},a_{x,\mathrm{cod}(\sigma_{x})}) as required. ∎

6.2. Functoriality

We now describe the manner in which the passage from an accessible comonad to its behaviour category is functorial.

Notation 76.

Let f:𝖯𝖰f\colon\mathsf{P}\rightarrow\mathsf{Q} be a morphism of accessible comonads on 𝒮et\mathrm{\mathcal{S}et} and let xQ1x\in Q1. We write fx:PxQfxf_{x}\colon P_{x}\rightarrow Q_{fx} for the unique natural transformation (whose unique existence follows from Lemma 73) such that fιx=ιfxfx:PxQf\circ\iota_{x}=\iota_{fx}\circ f_{x}\colon P_{x}\rightarrow Q.

Proposition 77.

Each morphism f:𝖯𝖰f\colon\mathsf{P}\rightarrow\mathsf{Q} of accessible comonads on 𝒮et\mathrm{\mathcal{S}et} induces a cofunctor 𝔹f:𝔹𝖯𝔹𝖰\mathbb{B}_{f}\colon\mathbb{B}_{\mathsf{P}}\rightsquigarrow\mathbb{B}_{\mathsf{Q}} on behaviour categories with action on objects f1:P1Q1f_{1}\colon P1\rightarrow Q1, and with, for each xP1x\in P1, the action on homs (𝔹𝖰)fx(𝔹𝖯)x(\mathbb{B}_{\mathsf{Q}})_{fx}\rightarrow(\mathbb{B}_{\mathsf{P}})_{x} given by ττfx\tau\mapsto\tau\circ f_{x}.

Proof.

We first dispatch axiom (ii) for a cofunctor, which follows by the calculation that εfx𝖰fx=ε𝖰ιfxfx=ε𝖰fιx=ε𝖯ιx=εx𝖯:Pxid\varepsilon^{\mathsf{Q}}_{fx}\circ f_{x}=\varepsilon^{\mathsf{Q}}\circ\iota_{fx}\circ f_{x}=\varepsilon^{\mathsf{Q}}\circ f\circ\iota_{x}=\varepsilon^{\mathsf{P}}\circ\iota_{x}=\varepsilon_{x}^{\mathsf{P}}\colon P_{x}\rightarrow\mathrm{id}. We next deal with axiom (i). Let τ:Qfxid\tau\colon Q_{fx}\rightarrow\mathrm{id} be an element of (𝔹𝖰)fx(\mathbb{B}_{\mathsf{Q}})_{fx} with image τfx\tau\circ f_{x} in (𝔹𝖯)x(\mathbb{B}_{\mathsf{P}})_{x}. We must show that y:=cod(τfx)y\mathrel{\mathop{:}}=\mathrm{cod}(\tau\circ f_{x}) is sent by ff to z:=cod(τ)z\mathrel{\mathop{:}}=\mathrm{cod}(\tau). To this end, consider the diagram to the left in:

Px\textstyle{P_{x}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}(τfx)\scriptstyle{(\tau\circ f_{x})^{\sharp}}δx\scriptstyle{\delta_{x}}fx\scriptstyle{f_{x}}Py\textstyle{P_{y}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}ιy\scriptstyle{\iota_{y}}Qfx\textstyle{Q_{fx}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}τ\scriptstyle{\tau^{\sharp}}δfx\scriptstyle{\delta_{fx}}Qz\textstyle{Q_{z}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}ιz\scriptstyle{\iota_{z}}PxP\textstyle{P_{x}P\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}(τfx)P\scriptstyle{(\tau\circ f_{x})P}fxf\scriptstyle{f_{x}f}P\textstyle{P\ignorespaces\ignorespaces\ignorespaces\ignorespaces}f\scriptstyle{f}QfxQ\textstyle{Q_{fx}Q\ignorespaces\ignorespaces\ignorespaces\ignorespaces}τQ\scriptstyle{\tau Q}Q\textstyle{Q}
             Px\textstyle{P_{x}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}(τfx)\scriptstyle{(\tau\circ f_{x})^{\sharp}}fx\scriptstyle{f_{x}}Py\textstyle{P_{y}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}fy\scriptstyle{f_{y}}ιy\scriptstyle{\iota_{y}}P\textstyle{P\ignorespaces\ignorespaces\ignorespaces\ignorespaces}f\scriptstyle{f}Qfx\textstyle{Q_{fx}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}τ\scriptstyle{\tau^{\sharp}}Qz\textstyle{Q_{z}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}ιz\scriptstyle{\iota_{z}}Q .\textstyle{Q\hbox to0.0pt{ .\hss}}

The front and back faces are instances of (6.1), the left face commutes since ff is a comonad morphism, and the bottom face commutes by naturality. We can thus read off that the outside of the diagram to the right commutes; as such, its upper composite (clearly) factors through ιz\iota_{z}, but also through ιfy\iota_{fy}, since fιy=ιfyfyf\circ\iota_{y}=\iota_{fy}\circ f_{y}: whence by Lemma 73(ii) we have z=f(y)z=f(y), giving the first cofunctor axiom.

Finally, we address cofunctor axiom (iii). Note that we can now insert fyf_{y} into the diagram right above; whereupon the right square commutes by definition of fyf_{y}, and the left square since it does so on postcomposition by the monic ιz\iota_{z}. Postcomposing this left-hand square with some σ:Qzid\sigma\colon Q_{z}\rightarrow\mathrm{id} yields the final cofunctor axiom. ∎

Proposition 78.

For each morphism f:𝖯𝖰f\colon\mathsf{P}\rightarrow\mathsf{Q} of accessible comonads, we have a commuting square of monad morphisms:

(6.5) 𝖰\textstyle{\mathsf{Q}^{\circ}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}θ\scriptstyle{\theta}f\scriptstyle{f^{\circ}}𝖳𝔹𝖰\textstyle{\mathsf{T}_{\mathbb{B}_{\mathsf{Q}}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}𝖳𝔹f\scriptstyle{\mathsf{T}_{\mathbb{B}_{f}}}𝖯\textstyle{\mathsf{P}^{\circ}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}θ\scriptstyle{\theta}𝖳𝔹𝖯 .\textstyle{\mathsf{T}_{\mathbb{B}_{\mathsf{P}}}\hbox to0.0pt{ .\hss}}
Proof.

For each τ:QAid\tau\colon Q\rightarrow A\cdot\mathrm{id} in QAQ^{\circ}A, and each xP1x\in P1, we have a diagram

Px\textstyle{{P_{x}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}fx\scriptstyle{f_{x}}ιx\scriptstyle{\iota_{x}}Qfx\textstyle{{Q_{fx}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}ιfx\scriptstyle{\iota_{fx}}τfx\scriptstyle{\tau_{fx}}id\textstyle{\mathrm{id}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}νafx\scriptstyle{\nu_{a_{fx}}}P\textstyle{{P}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}f\scriptstyle{f}Q\textstyle{{Q}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}τ\scriptstyle{\tau}Aid\textstyle{A\cdot\mathrm{id}}

whose right square is as in Proposition 75, and whose left square is the definition of fxf_{x}. It thus follows that the image of τ\tau under (T𝔹f)AθA𝖰(T_{\mathbb{B}_{f}})_{A}\circ\theta_{A}^{\mathsf{Q}} is λx.(afx,τfxfx)\lambda x.\,(a_{fx},\tau_{fx}\circ f_{x}). On the other hand, by unicity in Proposition 75, the image of f(τ)=τfPAf^{\circ}(\tau)=\tau\circ f\in P^{\circ}A under θA𝖯:PAT𝔹𝖯A\theta^{\mathsf{P}}_{A}\colon P^{\circ}A\rightarrow T_{\mathbb{B}_{\mathsf{P}}}A is also λx.(afx,τfxfx)\lambda x.\,(a_{fx},\tau_{fx}\circ f_{x}), as desired. ∎

Combining this result with Proposition 49, we obtain:

Theorem 79.

The functor 𝔹():𝒞mda(𝒮et)𝒞of\mathbb{B}_{({\mathord{\text{--}}})}\colon\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})\rightarrow\mathrm{\mathcal{C}of} taking an accessible comonad 𝖰\mathsf{Q} to its behaviour category 𝔹𝖰\mathbb{B}_{\mathsf{Q}}, and a map of accessible comonads f:𝖯𝖰f\colon\mathsf{P}\rightarrow\mathsf{Q} to the cofunctor 𝔹f:𝔹𝖯𝔹𝖰\mathbb{B}_{f}\colon\mathbb{B}_{\mathsf{P}}\rightsquigarrow\mathbb{B}_{\mathsf{Q}} of Proposition 78, yields a within-isomorphism factorisation

\textstyle{\cong}𝒞of\textstyle{\mathrm{\mathcal{C}of}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}𝖰()\scriptstyle{\mathsf{Q}_{({\mathord{\text{--}}})}}𝒞mda(𝒮et)\textstyle{\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Costr\scriptstyle{\mathrm{Costr}}𝔹()\scriptstyle{\mathbb{B}_{({\mathord{\text{--}}})}}nda(𝒮et)op .\textstyle{\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}\hbox to0.0pt{ .\hss}}

7. Idempotency of costructure–cosemantics

So far, we have seen that cosemantics takes values in presheaf comonads, and costructure takes values in presheaf monads; to complete our understanding of the costructure–cosemantics adjunction, we now show that further application of either adjoint simply interchanges a presheaf monad with its corresponding presheaf comonad. More precisely, we will show that the costructure–cosemantics adjunction is idempotent, with the presheaf monads and comonads as fixpoints to either side.

7.1. Idempotent adjunctions

We begin by recalling standard category-theoretic background on fixpoints and idempotency for adjunctions. To motivate this, recall that any adjunction between posets induces an isomorphism between the sub-posets of fixpoints to each side. Similarly, any adjunction of categories restricts to an adjoint equivalence between the full subcategories of fixpoints in the following sense:

Definition 80 (Fixpoints).

Let LR:𝒟𝒞L\dashv R\colon{\mathcal{D}}\rightarrow{\mathcal{C}} be an adjunction. A fixpoint to the left is an object X𝒟X\in{\mathcal{D}} at which the counit map εX:LRXX\varepsilon_{X}\colon LRX\rightarrow X is invertible; a fixpoint to the right is Y𝒞Y\in{\mathcal{C}} for which ηY:YRLY\eta_{Y}\colon Y\rightarrow RLY is invertible. We write ix(LR)\mathrm{\mathcal{F}ix}(LR) and ix(RL)\mathrm{\mathcal{F}ix(RL)} for the full subcategories of fixpoints to the left and right.

In the posetal case, the fixpoints to the left and the right are respectively coreflective and reflective in the whole poset. This is not true in general for adjunctions between categories, but it is true in the following situation:

Definition 81 (Idempotent adjunction).

An adjunction LR:𝒟𝒞L\dashv R\colon{\mathcal{D}}\rightarrow{\mathcal{C}} is called idempotent if it satisfies any one of the following equivalent conditions:

  1. (i)

    Each RXRX is a fixpoint;

  2. (ii)

    RR inverts each counit component;

  3. (iii)

    The monad RLRL is idempotent;

  4. (iv)

    Each LYLY is a fixpoint;

  5. (v)

    LL inverts each unit component;

  6. (vi)

    The comonad LRLR is idempotent.

The equivalence of these conditions is straightforward and well-known; for the reader who has not seen it, we leave the proof as an instructive exercise. Equally straightforward are the following consequences of the definition:

Proposition 82.

If the adjunction LR:𝒟𝒞L\dashv R\colon{\mathcal{D}}\rightarrow{\mathcal{C}} is idempotent, then:

  1. (i)

    X𝒟X\in{\mathcal{D}} is a fixpoint if and only if it is in the essential image of LL;

  2. (ii)

    Y𝒞Y\in{\mathcal{C}} is a fixpoint if and only if it is in the essential image of RR;

  3. (iii)

    The fixpoints to the left are coreflective in 𝒟{\mathcal{D}} via XLRXX\mapsto LRX.

  4. (iv)

    The fixpoints to the right are reflective in 𝒞{\mathcal{C}} via YRLYY\mapsto RLY;

7.2. Presheaf monads and presheaf comonads are fixpoints

We aim to show that the costructure–cosemantics adjunction (4.1) is idempotent, with the presheaf monads and comonads as the fixpoints. We first show that costructure and cosemantics interchange a presheaf monad with the corresponding presheaf comonad.

Proposition 83.

We have isomorphisms of comonads, natural in 𝔹\mathbb{B}, of the form

(7.1) α𝔹:𝖰𝔹Cosem(𝖳𝔹)\alpha_{\mathbb{B}}\colon\mathsf{Q}_{\mathbb{B}}\rightarrow\mathrm{Cosem}(\mathsf{T}_{\mathbb{B}})

characterised by the fact that they induce on categories of Eilenberg–Moore coalgebras the functor 𝔹-𝒮et𝒮𝖳𝔹et\mathbb{B}\text{-}\mathrm{\mathcal{S}et}\rightarrow{{}^{\mathsf{T}_{\mathbb{B}}}\mathrm{\mathcal{S}et}} sending the left 𝔹\mathbb{B}-set (X,p,)(X,p,\ast) to the 𝖳𝔹\mathsf{T}_{\mathbb{B}}-comodel 𝐗{\boldsymbol{X}} with

(7.2) λb.(fb,ab)𝑿:X\displaystyle\llbracket\lambda b.\,(f_{b},a_{b})\rrbracket_{{\boldsymbol{X}}}\colon X A×X\displaystyle\rightarrow A\times X
x\displaystyle x (ap(x),fp(x)x) .\displaystyle\mapsto(a_{p(x)},f_{p(x)}\ast x)\hbox to0.0pt{ .\hss}

In the statement of this result, we identify 𝒞oalg(𝖰𝔹)\mathrm{\mathcal{C}oalg}(\mathsf{Q_{\mathbb{B}}}) with 𝔹-𝒮et\mathbb{B}\text{-}\mathrm{\mathcal{S}et} by Proposition 35, and 𝒞oalg(Cosem(𝖳𝔹))\mathrm{\mathcal{C}oalg}(\mathrm{Cosem}(\mathsf{T}_{\mathbb{B}})) with 𝒮𝖳𝔹et{}^{\mathsf{T}_{\mathbb{B}}}\mathrm{\mathcal{S}et} by Proposition 39.

Proof.

By Proposition 30, the associated monad of the theory of 𝔹\mathbb{B}-valued dependently typed update is the presheaf monad 𝖳𝔹\mathsf{T}_{\mathbb{B}}; so by Proposition 25, we have an isomorphism over 𝒮et\mathrm{\mathcal{S}et} of categories of comodels 𝒮𝕋𝔹et𝒮𝖳𝔹et{}^{\mathbb{T}_{\mathbb{B}}}\mathrm{\mathcal{S}et}\cong{}^{\mathsf{T}_{\mathbb{B}}}\mathrm{\mathcal{S}et}, sending the 𝕋𝔹\mathbb{T}_{\mathbb{B}}-comodel 𝑿{\boldsymbol{X}} to the 𝖳𝔹\mathsf{T}_{\mathbb{B}}-comodel structure on XX with λb.(fb,ab)=𝗀𝖾𝗍(λb.𝗎𝗉𝖽fb(ab))\llbracket\lambda b.\,(f_{b},a_{b})\rrbracket=\llbracket\mathsf{get}(\lambda b.\,\mathsf{upd}_{f_{b}}(a_{b}))\rrbracket. Composing this isomorphism with the invertible (3.9) yields an invertible functor 𝔹-𝒮et𝒮𝖳𝔹et\mathbb{B}\text{-}\mathrm{\mathcal{S}et}\rightarrow{}^{\mathsf{T}_{\mathbb{B}}}\mathrm{\mathcal{S}et} over 𝒮et\mathrm{\mathcal{S}et}, which by inspection has the formula (7.2). We conclude by the full fidelity of the Eilenberg–Moore semantics functor (Lemma 38). ∎

Proposition 84.

For any small category 𝔹\mathbb{B}, the monad morphism

(7.3) α¯𝔹:𝖳𝔹Costr(𝖰𝔹)\bar{\alpha}_{\mathbb{B}}\colon\mathsf{T}_{\mathbb{B}}\rightarrow\mathrm{Costr}(\mathsf{Q}_{\mathbb{B}})

found as the adjoint transpose of the isomorphism (7.1), is itself an isomorphism.

Proof.

By Remark 46 and (7.2), we see that α¯\bar{\alpha} sends the element (f,a)=λb.(fb,ab)(f,a)=\lambda b.\,(f_{b},a_{b}) of T𝔹(A)=b(𝔹b×A)T_{\mathbb{B}}(A)=\prod_{b}(\mathbb{B}_{b}\times A) to the transformation α¯(f,a):U𝔹AU𝔹:𝔹-𝒮et𝒮et\bar{\alpha}(f,a)\colon U^{\mathbb{B}}\Rightarrow A\cdot U^{\mathbb{B}}\colon\mathbb{B}\text{-}\mathrm{\mathcal{S}et}\rightarrow\mathrm{\mathcal{S}et} whose component at a 𝔹\mathbb{B}-set (X,p,)(X,p,\cdot) is given by the function

(7.4) α¯(f,a)(X,p,):X\displaystyle\bar{\alpha}(f,a)_{(X,p,\cdot)}\colon X A×X\displaystyle\rightarrow A\times X
x\displaystyle x (ap(x),fp(x)x) .\displaystyle\mapsto(a_{p(x)},f_{p(x)}\cdot x)\hbox to0.0pt{ .\hss}

We must show every γ:U𝔹AU𝔹\gamma\colon U^{\mathbb{B}}\Rightarrow A\cdot U^{\mathbb{B}} takes this form for a unique (f,a)T𝔹(A)(f,a)\in T_{\mathbb{B}}(A). For each b𝔹b\in\mathbb{B}, we have the representable left 𝔹\mathbb{B}-set y(b)y(b) with underlying set 𝔹b\mathbb{B}_{b}, projection to ob(𝔹)\mathrm{ob}(\mathbb{B}) given by codomain, and action given by composition in 𝔹\mathbb{B}. The component of γ\gamma at y(b)y(b) is a function 𝔹bA×𝔹b\mathbb{B}_{b}\rightarrow A\times\mathbb{B}_{b}, which, if we are to have γ=α¯(f,a)\gamma=\bar{\alpha}(f,a), must by (7.4) have its value at 1b𝔹b1_{b}\in\mathbb{B}_{b} given by (ab,fb)(a_{b},f_{b}). Thus, if we define (ab,fb)(a_{b},f_{b}) to be γy(b)(1b)\gamma_{y(b)}(1_{b}) for each b𝔹b\in\mathbb{B}, then it remains only to verify that indeed γ=α¯(f,a)\gamma=\bar{\alpha}(f,a). But for any 𝔹\mathbb{B}-set (X,p,)(X,p,\ast) and any xXx\in X, there is by the Yoneda lemma a unique map of 𝔹\mathbb{B}-sets x~:y(p(x))X\tilde{x}\colon y(p(x))\rightarrow X sending 1px1_{px} to xx; and now naturality of γ\gamma ensures that

γ(X,p,)(x)=γ(X,p,)(x~(1px))=(A×x~)(γy(px)(1px))=(ab,x~(fpx))=(apx,fpxx)\gamma_{(X,p,\ast)}(x)=\gamma_{(X,p,\ast)}(\tilde{x}(1_{px}))=(A\times\tilde{x})(\gamma_{y(px)}(1_{px}))=(a_{b},\tilde{x}(f_{px}))=(a_{px},f_{px}\ast x)

so that γ=α¯(f,a)\gamma=\bar{\alpha}(f,a) as desired. ∎

Given the tight relationship between (7.1) and (7.3), it is now easy to conclude that presheaf monads and comonads are fixpoints.

Proposition 85.

Each presheaf monad is a fixpoint on the left of the costructure–cosemantics adjunction, while each presheaf comonad is a fixpoint on the right.

Proof.

For each small category 𝔹\mathbb{B} we have a commuting triangle

𝖰𝔹\textstyle{\mathsf{Q}_{\mathbb{B}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}α\scriptstyle{\alpha}η𝖰𝔹\scriptstyle{\eta_{\mathsf{Q}_{\mathbb{B}}}}Cosem(Costr(𝖰𝔹))\textstyle{\mathrm{Cosem}(\mathrm{Costr}(\mathsf{Q}_{\mathbb{B}}))\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Cosem(α¯)\scriptstyle{\mathrm{Cosem}(\bar{\alpha})}Cosem(𝖳𝔹)\textstyle{\mathrm{Cosem}(\mathsf{T}_{\mathbb{B}})}

where η𝖰𝔹\eta_{\mathsf{Q}_{\mathbb{B}}} is the unit of (4.1) and α\alpha and α¯\bar{\alpha} are as in (7.1) and (7.3). Since both α\alpha and α¯\bar{\alpha} are invertible, it follows that η𝖰𝔹\eta_{\mathsf{Q}_{\mathbb{B}}} is too; and since every presheaf comonad is isomorphic to some 𝖰𝔹\mathsf{Q}_{\mathbb{B}}, it follows that every presheaf comonad is a fixpoint on the right. The dual argument shows each presheaf monad is a fixpoint on the left. ∎

7.3. Idempotency of the costructure–cosemantics adjunction

As an immediate consequence of the preceding result, we have:

Theorem 86.

The costructure–cosemantics adjunction (4.1) is idempotent. Its fixpoints to the left are the presheaf monads, while those to the right are the presheaf comonads.

Proof.

Each Cosem(𝖳)\mathrm{Cosem}(\mathsf{T}) is a presheaf comonad by Proposition 50, and each presheaf comonad is a fixpoint to the right by Proposition 85; thus Definition 81(i) is satisfied and the adjunction is idempotent. For the remaining claims, one direction is Proposition 85; while the other follows on noting that, by the preceding result and Proposition 84, the composite CosemCostr\mathrm{Cosem}\circ\mathrm{Costr} sends each comonad to a presheaf comonad, while CostrCosem\mathrm{Costr}\circ\mathrm{Cosem} sends each monad to a presheaf monad. ∎

We may use this result to resolve some unfinished business:

Proposition 87.

The presheaf monad functor 𝖳():𝒞ofnda(𝒮et)op\mathsf{T}_{({\mathord{\text{--}}})}\colon\mathrm{\mathcal{C}of}\rightarrow\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}} of Proposition 33 is full and faithful.

Proof.

Since the costructure–cosemantics adjunction is idempotent, the functor Costr:𝒞mda(𝒮et)nda(𝒮et)op\mathrm{Costr}\colon\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})\rightarrow\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}} is fully faithful when restricted to the subcategory of presheaf comonads; and since 𝖰()\mathsf{Q}_{({\mathord{\text{--}}})} takes its image in this subcategory, we see that Costr𝖰():𝒞ofnda(𝒮et)op\mathord{\mathrm{Costr}}\circ\mathord{\mathsf{Q}_{({\mathord{\text{--}}})}}\colon\mathrm{\mathcal{C}of}\rightarrow\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}} is fully faithful. Now transporting the values of this composite functor along the isomorphisms α¯𝔹:𝖳𝔹Costr(𝖰𝔹)\bar{\alpha}_{\mathbb{B}}\colon\mathsf{T}_{\mathbb{B}}\cong\mathrm{Costr}(\mathsf{Q}_{\mathbb{B}}) of Proposition 84 yields a fully faithful functor 𝒞ofMnda(𝒮et)op\mathrm{\mathcal{C}of}\rightarrow\mathrm{Mnd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}} which acts on objects by 𝔹𝖳𝔹\mathbb{B}\mapsto\mathsf{T}_{\mathbb{B}}, and on morphisms by F(α¯𝔹)1Costr(𝖰F)α¯F\mapsto(\bar{\alpha}_{\mathbb{B}})^{-1}\circ\mathrm{Costr}(\mathsf{Q}_{F})\circ\bar{\alpha}_{\mathbb{C}}. Now direct calculation shows this action on morphisms to be precisely that of (5.5). ∎

It follows from this and Proposition 32 that the full embeddings 𝖰():𝒞of𝒞mda(𝒮et)\mathsf{Q}_{({\mathord{\text{--}}})}\colon\mathrm{\mathcal{C}of}\rightarrow\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}) and 𝖳():𝒞ofnda(𝒮et)op\mathsf{T}_{({\mathord{\text{--}}})}\colon\mathrm{\mathcal{C}of}\rightarrow\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}} exhibit 𝒞of\mathrm{\mathcal{C}of} as equivalent to the full subcategories of fixpoints to the left and to the right; from which it follows that:

Proposition 88.

The presheaf monad and presheaf comonad functors of Propositions 33 and 32, together with the behaviour functors of Theorems 69 and 79, participate in adjunctions

𝒞of\textstyle{{\mathrm{\mathcal{C}of}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}𝖳()\scriptstyle{\mathsf{T}_{({\mathord{\text{--}}})}}𝔹()\scriptstyle{\mathbb{B}_{({\mathord{\text{--}}})}}\scriptstyle{\top}nda(𝒮et)op\textstyle{{\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et})^{\mathrm{op}}}}𝒞of\textstyle{{\mathrm{\mathcal{C}of}}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}𝖰()\scriptstyle{\mathsf{Q}_{({\mathord{\text{--}}})}}𝔹()\scriptstyle{\mathbb{B}_{({\mathord{\text{--}}})}}\scriptstyle{\bot}𝒞mda(𝒮et)\textstyle{{\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et})}}

exhibiting the full subcategories of presheaf monads, respectively presheaf comonads, as reflective in nda(𝒮et)\mathrm{\mathcal{M}nd}_{a}(\mathrm{\mathcal{S}et}), respectively 𝒞mda(𝒮et)\mathrm{\mathcal{C}md}_{a}(\mathrm{\mathcal{S}et}).

We can describe the units of these reflections explicitly. On the one hand, if 𝖰\mathsf{Q} is an accessible comonad on 𝒮et\mathrm{\mathcal{S}et}, then its reflection in the full subcategory of presheaf comonads is the presheaf comonad of the behaviour category 𝔹𝖰\mathbb{B}_{\mathsf{Q}}, and the reflection map 𝖰𝖰𝔹𝖰\mathsf{Q}\rightarrow\mathsf{Q}_{\mathbb{B}_{\mathsf{Q}}} has components

ηA:Q(A)\displaystyle\eta_{A}\colon Q(A) xQ1A[𝒮et,𝒮et](Qx,id)\displaystyle\rightarrow\textstyle\sum_{x\in Q1}A^{[\mathrm{\mathcal{S}et},\mathrm{\mathcal{S}et}](Q_{x},\mathrm{id})}
a\displaystyle a (Q!(a),λτ.τA(a)) .\displaystyle\mapsto(Q!(a),\lambda\tau.\,\tau_{A}(a))\hbox to0.0pt{ .\hss}

On the other hand, if 𝖳\mathsf{T} is an accessible monad on 𝒮et\mathrm{\mathcal{S}et}, then its reflection into the full subcategory of presheaf monads is the presheaf monad of the behaviour category 𝔹𝖳\mathbb{B}_{\mathsf{T}}, and the reflection map η:𝖳𝖳𝔹𝖳\eta\colon\mathsf{T}\rightarrow\mathsf{T}_{\mathbb{B}_{\mathsf{T}}} has components

ηA:T(A)\displaystyle\eta_{A}\colon T(A) βB𝖳(A×T(1)/β)\displaystyle\rightarrow\textstyle\prod_{\beta\in B_{\mathsf{T}}}(A\times T(1)\delimiter 84079374\mathopen{}\sim_{\beta})
t\displaystyle t λβ.(β(t),t~) .\displaystyle\mapsto\lambda\beta.\,(\beta(t),\tilde{t})\hbox to0.0pt{ .\hss}

In fact, this reflection map exhibits 𝖳𝔹𝖳\mathsf{T}_{\mathbb{B}_{\mathsf{T}}} as the result of adjoining to 𝖳\mathsf{T} a new B𝖳B_{\mathsf{T}}-ary operation 𝖻𝖾𝗁\mathsf{beh} satisfying the axioms of read-only state and the axioms

t(u)𝖻𝖾𝗁,βt"uβ(t)t(u)\equiv_{\mathsf{beh},\beta}t\gg u_{\beta(t)}

for all tT(A)t\in T(A) and uT(B)Au\in T(B)^{A}. From a computational perspective, we understand the new operation 𝖻𝖾𝗁\mathsf{beh} as an “oracle” which allows the user to request complete information about the future behaviour of the external system with which we are interacting. Of course, since this future behaviour is typically wildly non-computable, we immediately leave the realm of computationally meaningful theories. In future work, we will see how to rectify this, to some degree, by considering an adjunction between accessible monads on 𝒮et\mathrm{\mathcal{S}et} and suitably accessible comonads on the category of topological spaces. In this refined setting, we will see that the passage from monad to comonad and back adjoins new operations which observe only finite amounts of information about the future behaviour of the system.

8. Examples and applications: cosemantics

In the final two sections of this paper, we give a range of examples illustrating our main results. In this section, we calculate the behaviour category, and the comodels classifying admissible behaviours, for a range of examples of algebraic theories for computational effects, and calculate some examples of cofunctors between behaviour categories induced by computationally interesting interpretations of algebraic theories.

8.1. Reversible input

Given a set VV, the theory of VV-valued reversible input (first considered for |V|=2|V|=2 in [17]) is generated by a VV-ary operation 𝗋𝖾𝖺𝖽\mathsf{read}, and a VV-indexed family of unary operations 𝗎𝗇𝗋𝖾𝖺𝖽v\mathsf{unread}_{v}, satisfying the equations

(8.1) 𝗎𝗇𝗋𝖾𝖺𝖽v(𝗋𝖾𝖺𝖽(x))xvand𝗋𝖾𝖺𝖽(λu.𝗎𝗇𝗋𝖾𝖺𝖽u(x))x .\mathsf{unread}_{v}(\mathsf{read}(x))\equiv x_{v}\qquad\text{and}\qquad\mathsf{read}(\lambda u.\,\mathsf{unread}_{u}(x))\equiv x\hbox to0.0pt{ .\hss}

If 𝗋𝖾𝖺𝖽\mathsf{read} is thought of as reading the next value from an input stream, then 𝗎𝗇𝗋𝖾𝖺𝖽v\mathsf{unread}_{v} returns the value vv to the front of that stream. A comodel of this theory comprises the data of a set SS, a function 𝗋𝖾𝖺𝖽=(g,n):SV×S\llbracket\mathsf{read}\rrbracket=(g,n)\colon S\rightarrow V\times S and functions 𝗎𝗇𝗋𝖾𝖺𝖽v:SS\llbracket\mathsf{unread}_{v}\rrbracket\colon S\rightarrow S, or equally a single function p:V×SSp\colon V\times S\rightarrow S; while the equations force (g,n)(g,n) and pp to be inverse to each other. Thus comodels of VV-valued reversible input are equally well comodels of VV-valued input whose structure map 𝗋𝖾𝖺𝖽:SV×S\llbracket\mathsf{read}\rrbracket\colon S\rightarrow V\times S is invertible. Since, in particular, this is true for the final comodel 𝑽{\boldsymbol{V^{\mathbb{N}}}} of VV-valued input by the well-known Lambek lemma, we conclude that this is also the final comodel of VV-valued reversible input.

We now calculate the comodel associated to an admissible behaviour WVW\in V^{\mathbb{N}}. We begin with some calculations relating to W\sim_{W}-equivalence. First, by Remark 59, any unary term is W\sim_{W}-equivalent to one of the form σ1""σn"id\sigma_{1}\gg\dots\gg\sigma_{n}\gg\mathrm{id} where each σi\sigma_{i} is either 𝗋𝖾𝖺𝖽\mathsf{read} or some 𝗎𝗇𝗋𝖾𝖺𝖽v\mathsf{unread}_{v}. Now the first equation in (8.1) implies that 𝗎𝗇𝗋𝖾𝖺𝖽v"𝗋𝖾𝖺𝖽"()\mathsf{unread}_{v}\gg\mathsf{read}\gg({\mathord{\text{--}}}) is the identity operator, and so any unary term is W\sim_{W}-equivalent to one of the form

[n,vm,,v1]:=𝗋𝖾𝖺𝖽""𝗋𝖾𝖺𝖽n"𝗎𝗇𝗋𝖾𝖺𝖽vm""𝗎𝗇𝗋𝖾𝖺𝖽v1"id[n,v_{m},\dots,v_{1}]\mathrel{\mathop{:}}=\smash{\overbrace{\mathsf{read}\gg\dots\gg\mathsf{read}}^{n}}\gg\mathsf{unread}_{v_{m}}\gg\dots\gg\mathsf{unread}_{v_{1}}\gg\mathrm{id}

for some nn\in\mathbb{N} and vm,,v1Vv_{m},\dots,v_{1}\in V. Since the behaviour WW satisfies W(𝗋𝖾𝖺𝖽)=W0W(\mathsf{read})=W_{0}, we have 𝗋𝖾𝖺𝖽"𝗎𝗇𝗋𝖾𝖺𝖽W0=𝗋𝖾𝖺𝖽(λu.𝗎𝗇𝗋𝖾𝖺𝖽W0)W𝗋𝖾𝖺𝖽(λu.𝗎𝗇𝗋𝖾𝖺𝖽u)=id\mathsf{read}\gg\mathsf{unread}_{W_{0}}=\mathsf{read}(\lambda u.\,\mathsf{unread}_{W_{0}})\sim_{W}\mathsf{read}(\lambda u.\,\mathsf{unread}_{u})=\mathrm{id}; whence by Lemma 60, also [n+1,Wn,vm,,v1]W[n,vm,,v1][n+1,W_{n},v_{m},\dots,v_{1}]\sim_{W}[n,v_{m},\dots,v_{1}] for any nn\in\mathbb{N} and vm,,v1Vv_{m},\dots,v_{1}\in V. Consequently, each unary term is W\sim_{W}-equivalent to an element of the set SWS_{W} given by

(8.2) {[n,vm,,v1]:n,m,viV and Wn1vm if n,m>0} .\{[n,v_{m},\dots,v_{1}]:n,m\in\mathbb{N},v_{i}\in V\text{ and $W_{n-1}\neq v_{m}$ if $n,m>0$}\}\hbox to0.0pt{ .\hss}

We claim that SWS_{W} is in fact a set of W\sim_{W}-equivalence class representatives. For this, it suffices by Remark 65 to endow SWS_{W} with a comodel structure 𝑺W{\boldsymbol{S}}_{W} satisfying (5.8)—which will then make it the comodel classifying states of behaviour WW. We do so by taking 𝗋𝖾𝖺𝖽𝑺W\llbracket\mathsf{read}\rrbracket_{{\boldsymbol{S}}_{W}} to be given by

(8.3) [n,vm,,v1]{(Wn,[n+1]) if m=0;(v1,[n,vm,,v2]) if m>0,[n,v_{m},\dots,v_{1}]\mapsto\begin{cases}(W_{n},[n+1])&\text{ if $m=0$;}\\ (v_{1},[n,v_{m},\dots,v_{2}])&\text{ if $m>0$,}\\ \end{cases}

and taking 𝗎𝗇𝗋𝖾𝖺𝖽v𝑺W\llbracket\mathsf{unread}_{v}\rrbracket_{{\boldsymbol{S}}_{W}} to be given by

(8.4) [n,v1,,vm]{[n1] if m=0n>0v=Wn1;[n,vm,,v1,v] otherwise.[n,v_{1},\dots,v_{m}]\mapsto\begin{cases}[n-1]&\text{ if $m=0$, $n>0$, $v=W_{n-1}$;}\\ [n,v_{m},\dots,v_{1},v]&\text{ otherwise.}\end{cases}

We may now use the above calculations to identify maps WWW\rightarrow W^{\prime} in the behaviour category. These correspond to comodel homomorphisms 𝑺W𝑺W{\boldsymbol{S}}_{W^{\prime}}\rightarrow{\boldsymbol{S}}_{W} and so to states of 𝑺W{\boldsymbol{S}}_{W} of behaviour WW^{\prime}. Since the state [n,vm,,v1]𝑺W[n,v_{m},\dots,v_{1}]\in{\boldsymbol{S}}_{W} has behaviour given by the stream of values v1vmWnWn+1Wn+2v_{1}\dots v_{m}W_{n}W_{n+1}W_{n+2}\dots, we conclude that morphisms WWW\rightarrow W^{\prime} in the behaviour category are states of the form [n,Wm1,,W0][n,W^{\prime}_{m-1},\dots,W^{\prime}_{0}] where Wk=Wk+nmW^{\prime}_{k}=W_{k+n-m} for all kmk\geqslant m but Wm1Wn1W^{\prime}_{m-1}\neq W_{n-1}. Such a state is clearly uniquely determined by the integer i=nmi=n-m, and so we arrive at:

Proposition 89.

The behaviour category of the theory of VV-valued reversible input has object-set VV^{\mathbb{N}}; morphisms WWW\rightarrow W^{\prime} are integers ii such that, for some NN\in\mathbb{N}, we have Wk=Wk+iW^{\prime}_{k}=W_{k+i} for all k>Nk>N; and composition is addition of integers. The comodel classifying states of behaviour WVW\in V^{\mathbb{N}} has underlying set (8.2), and co-operations as in (8.3) and (8.4).

Note that this behaviour category is a groupoid; in fact, it is not hard to show that it is the free groupoid on the behaviour category for VV-valued input. This groupoid is well-known in the study of Cuntz CC^{\ast}-algebras: for example, for finite VV it appear already in [32, Definition III.2.1]. In this context, it is important that the groupoid is not just as a groupoid of sets, but a topological groupoid; in a sequel to this paper, we will explain how this topology arises very naturally via comodels.

8.2. Stack

Given a set VV, the theory of a VV-valued stack—introduced for a finite VV in [12]—is generated by a V+{}V+\{\bot\}-ary operation 𝗉𝗈𝗉\mathsf{pop}, whose arguments we group into an VV-ary part and a unary part; and a VV-indexed family of unary operations 𝗉𝗎𝗌𝗁v\mathsf{push}_{v} for vVv\in V, satisfying the equations

𝗉𝗎𝗌𝗁v(𝗉𝗈𝗉(x,y))xv𝗉𝗈𝗉(λv.𝗉𝗎𝗌𝗁v(x),x)x𝗉𝗈𝗉(x,𝗉𝗈𝗉(y,z))𝗉𝗈𝗉(x,z) .\mathsf{push}_{v}(\mathsf{pop}(x,y))\equiv x_{v}\quad\mathsf{pop}(\lambda v.\,\mathsf{push}_{v}(x),x)\equiv x\quad\mathsf{pop}(x,\mathsf{pop}(y,z))\equiv\mathsf{pop}(x,z)\hbox to0.0pt{ .\hss}

This theory captures the semantics of a stack of elements from VV: we read 𝗉𝗎𝗌𝗁v(x)\mathsf{push}_{v}(x) as “push vv on the stack and continue as xx”, and 𝗉𝗈𝗉(x,y)\mathsf{pop}(x,y) as “if the stack is non-empty, pop its top element vv and continue as xvx_{v}; else continue as yy”.

Note the similarities with the theory of VV-valued reversible input; indeed, this latter theory could equally well be seen as the theory of a VV-valued infinite stack. We can formalise this via an interpretation of the theory of VV-valued stack into VV-valued reversible input which maps 𝗉𝗎𝗌𝗁u\mathsf{push}_{u} to 𝗎𝗇𝗋𝖾𝖺𝖽u\mathsf{unread}_{u} and 𝗉𝗈𝗉(x,y)\mathsf{pop}(x,y) to 𝗋𝖾𝖺𝖽(x)\mathsf{read}(x).

A comodel of the theory of a VV-valued stack comprises a set SS with functions (g,n):S(V+{})×S(g,n)\colon S\rightarrow(V+\{\bot\})\times S (modelling 𝗉𝗈𝗉\mathsf{pop}) and p:V×SSp\colon V\times S\rightarrow S (modelling the 𝗉𝗎𝗌𝗁v\mathsf{push}_{v}’s) subject to conditions corresponding to the three equations above:

  1. (1)

    g(p(v,s))=vg(p(v,s))=v and n(p(v,s))=sn(p(v,s))=s;

  2. (2)

    If g(s)={}g(s)=\{\bot\} then n(s)=sn(s)=s, while if g(s)=vg(s)=v then p(v,n(s))=sp(v,n(s))=s;

  3. (3)

    If g(s)={}g(s)=\{\bot\} then g(n(s))={}g(n(s))=\{\bot\} and n(n(s))=n(s)n(n(s))=n(s) (this is implied by (2)).

Writing E={sS:g(s)=}E=\{s\in S:g(s)=\bot\} for the set of “states in which the stack is empty”, and j:ESj\colon E\rightarrow S for the inclusion, (1) implies that p:V×SSp\colon V\times S\rightarrow S is an injection whose image is disjoint from that of jj, and (2) that every sSs\in S lies either in EE or in the image of pp. So we have a coproduct diagram

V×S\textstyle{V\times S\ignorespaces\ignorespaces\ignorespaces\ignorespaces}p\scriptstyle{p}E .\textstyle{E\hbox to0.0pt{ .\hss}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}j\scriptstyle{j}S\textstyle{S}

In fact, any such coproduct diagram comes from a comodel: we may recover (g,n):S(V+{})×S(g,n)\colon S\rightarrow(V+\{\bot\})\times S as the unique map whose composites with pp and jj are λ(v,s).(v,s)\lambda(v,s).\,(v,s) and λe.(,j(e))\lambda e.\,(\bot,j(e)) respectively. Thus a comodel structure on a set SS is equivalently given by a set EE and a coproduct diagram V×SSEV\times S\rightarrow S\leftarrow E.

The final comodel of this theory is the set VωV^{\leqslant\omega} of partial functions V\mathbb{N}\rightharpoonup V which are defined on some initial segment of \mathbb{N}, under the comodel structure corresponding to the coproduct diagram

V×Vω\textstyle{V\times V^{\leqslant\omega}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}(v,W)v.W\scriptstyle{(v,W)\mapsto v.W\,\,\,\,}{} .\textstyle{\{\ast\}\hbox to0.0pt{ .\hss}\ignorespaces\ignorespaces\ignorespaces\ignorespaces}ε\scriptstyle{\mathord{\ast}\,\mapsto\varepsilon}S\textstyle{S}

Here, we write ε\varepsilon for the everywhere-undefined element of VωV^{\leqslant\omega}, and write v.WVωv.W\in V^{\leqslant\omega} for the element with (v.W)0=v(v.W)_{0}=v and (v.W)i+1Wi(v.W)_{i+1}\simeq W_{i}111 We use Kleene equality aba\simeq b, meaning that aa is defined just when bb is defined, and they are then equal.. In terms of the generating co-operations, this final comodel is given by:

𝗉𝗎𝗌𝗁v(W)=v.W𝗉𝗈𝗉(v.W)=(v,W)and𝗉𝗈𝗉(ε)=(,ε) .\llbracket\mathsf{push}_{v}\rrbracket(W)=v.W\qquad\llbracket\mathsf{pop}\rrbracket(v.W)=(v,W)\qquad\text{and}\qquad\llbracket\mathsf{pop}\rrbracket(\varepsilon)=(\bot,\varepsilon)\hbox to0.0pt{ .\hss}

We now calculate the comodel associated to an admissible behaviour WVωW\in V^{\leqslant\omega}. Given the similarity with the theory of VV-valued reversible input, we may argue as in the previous section to see that any unary term is W\sim_{W}-equivalent to one

[n,vm,,v1]:=𝗉𝗈𝗉""𝗉𝗈𝗉n"𝗉𝗎𝗌𝗁vm""𝗉𝗎𝗌𝗁v1"id[n,v_{m},\dots,v_{1}]\mathrel{\mathop{:}}=\underbrace{\mathsf{pop}\gg\dots\gg\mathsf{pop}}_{n}\gg\mathsf{push}_{v_{m}}\gg\dots\gg\mathsf{push}_{v_{1}}\gg\mathrm{id}

for some nn\in\mathbb{N} and vm,,v1Vv_{m},\dots,v_{1}\in V. Now, if W0W_{0} is undefined, then W(𝗉𝗈𝗉)=W(\mathsf{pop})=\bot, and so 𝗉𝗈𝗉"m=𝗉𝗈𝗉(λv.m,m)W𝗉𝗈𝗉(λv.𝗉𝗎𝗌𝗁v(m),m)=m\mathsf{pop}\gg m=\mathsf{pop}(\lambda v.\,m,m)\sim_{W}\mathsf{pop}(\lambda v.\,\mathsf{push}_{v}(m),m)=m for any mT(1)m\in T(1). By Lemma 60, it follows that [n+1,vm,,v1]W[n,vm,,v1][n+1,v_{m},\dots,v_{1}]\sim_{W}[n,v_{m},\dots,v_{1}] whenever WnW_{n} is undefined, and so we conclude that each unary term is W\sim_{W}-equivalent to some [n,vm,,v1][n,v_{m},\dots,v_{1}] for which WW is defined at all k<nk<n. At this point, by repeating the arguments of the preceding section, mutatis mutandis, we may show that any unary term is W\sim_{W}-equivalent to an element of the set

(8.5) {[n,vm,,v1]:n,m,viV,W defined at all k<n, and Wn1vm if n,m>0} .\{[n,v_{m},\dots,v_{1}]:n,m\in\mathbb{N},v_{i}\in V,W\text{ defined at all $k<n$,}\\ \text{ and $W_{n-1}\neq v_{m}$ if $n,m>0$}\}\hbox to0.0pt{ .\hss}

We now show, like before, that this is a set of W\sim_{W}-equivalence class representatives, by making it into a comodel satisfying (5.8); again, this comodel will then classifying states of behaviour WW. This time, we take 𝗉𝗈𝗉\llbracket\mathsf{pop}\rrbracket to be given by

(8.6) [n,vm,,v1]{(Wn,[n+1]) if m=0 and Wn defined;(,[n]) if m=0 and Wn undefined;(v1,[n,vm,,v2]) if m>0,[n,v_{m},\dots,v_{1}]\mapsto\begin{cases}(W_{n},[n+1])&\text{ if $m=0$ and $W_{n}$ defined;}\\ (\bot,[n])&\text{ if $m=0$ and $W_{n}$ undefined;}\\ (v_{1},[n,v_{m},\dots,v_{2}])&\text{ if $m>0$,}\\ \end{cases}

and take 𝗉𝗎𝗌𝗁v\llbracket\mathsf{push}_{v}\rrbracket to be given exactly as in (8.4). Transcribing the calculations of the preceding section, we arrive at:

Proposition 90.

The behaviour category of the theory of a VV-valued stack has object-set VωV^{\leqslant\omega}; morphisms WWW\rightarrow W^{\prime} are integers ii such that, for some NN\in\mathbb{N}, we have WkWk+iW^{\prime}_{k}\simeq W_{k+i} for all k>Nk>N; and composition is addition of integers. The comodel classifying states of behaviour WVωW\in V^{\leqslant\omega} has underlying set (8.5), and co-operations as in (8.6) and (8.4).

In fact, it is easy to see that the behaviour category of a VV-valued stack is the disjoint union of the behaviour category for VV^{\ast}-valued state (modelling a finite stack) and for VV-valued reversible input (modelling an infinite stack). The cofunctor on behaviour categories induced by the interpretation of the theory of a VV-valued stack into that of VV-valued reversible input is simply the connected component inclusion.

8.3. Dyck words

A Dyck word is a finite list W{𝖴,𝖣}W\in\{\mathsf{U},\mathsf{D}\}^{\ast} with the same number of U’s as D’s, and with the property that the iith U in the list always precedes the iith D. Here, U and D stand for “up” and “down”, and the idea is that a Dyck word records a walk on the natural numbers \mathbb{N} with steps ±1\pm 1 which starts and ends at 0. More generally, we can encode walks from nn\in\mathbb{N} to mm\in\mathbb{N} by “affine Dyck words”:

Definition 91 (Affine Dyck words).

Given n,mn,m\in\mathbb{N}, an affine Dyck word from nn to mm is a word W{𝖴,𝖣}W\in\{\mathsf{U},\mathsf{D}\}^{\ast} such that #{D’s in W}#{U’s in W}=nm\#\{\text{{D}'s in $W$}\}-\#\{\text{{U}'s in $W$}\}=n-m, and such that the iith U precedes the (i+n)(i+n)th D for all suitable ii. We may extend this notation by declaring any word W{𝖴,𝖣}W\in\{\mathsf{U},\mathsf{D}\}^{\ast} to be an affine Dyck word from \infty to \infty. If n,m{}n,m\in\mathbb{N}\cup\{\infty\}, then we write W:nmW\colon n\rightsquigarrow m to indicate that WW is an affine Dyck word from nn to mm.

For example:

  • UUDUDD is a Dyck word, but also an affine Dyck word nnn\rightsquigarrow n for any nn;

  • UDDUUU is an affine Dyck word 131\rightsquigarrow 3 and 242\rightsquigarrow 4, but not 020\rightsquigarrow 2.

We now describe an algebraic theory which encodes the dynamics of the walks encoded by affine Dyck words. It has two unary operations 𝖴\mathsf{U} and 𝖣\mathsf{D}; and an \mathbb{N}-indexed family of binary operations 𝗁𝗍>n\mathsf{ht}_{>n} each satisfying the axioms of read-only state; all subject to the following axioms:

𝗁𝗍>n(x,𝗁𝗍>m(y,z))𝗁𝗍>m(𝗁𝗍>n(x,y),z)\displaystyle\mathsf{ht}_{>n}(x,\mathsf{ht}_{>m}(y,z))\equiv\mathsf{ht}_{>m}(\mathsf{ht}_{>n}(x,y),z)
𝗁𝗍>0(x,𝖣(x))x𝖴(𝗁𝗍>0(x,y))𝖴(x)\displaystyle\mathsf{ht}_{>0}(x,\mathsf{D}(x))\equiv x\qquad\mathsf{U}(\mathsf{ht}_{>0}(x,y))\equiv\mathsf{U}(x)
𝖴(𝗁𝗍>n+1(x,y))𝗁𝗍>n(𝖴(x),𝖴(y))𝖣(𝗁𝗍>n(x,y))𝗁𝗍>n+1(𝖣(x),𝖣(y))\displaystyle\mathsf{U}(\mathsf{ht}_{>n+1}(x,y))\equiv\mathsf{ht}_{>n}(\mathsf{U}(x),\mathsf{U}(y))\qquad\mathsf{D}(\mathsf{ht}_{>n}(x,y))\equiv\mathsf{ht}_{>n+1}(\mathsf{D}(x),\mathsf{D}(y))

for all mnm\leqslant n\in\mathbb{N}. The theory of affine Dyck words provides an interface for accessing a state machine with an internal “height” variable hh\in\mathbb{N}, which responds to two commands U and D which respectively increase and decrease hh by one, with the proviso that D should do nothing when applied in a state with h=0h=0. With this understanding, we read the primitive 𝗁𝗍>n(x,y)\mathsf{ht}_{>n}(x,y) as “if h>nh>n then continue as xx, else continue as yy”; read 𝖴(x)\mathsf{U}(x) as “perform U and continue as xx”; and read 𝖣(x)\mathsf{D}(x) as “perform D (so long as h>0h>0) and continue as xx”.

Rather than compute the comodels by hand, we pass directly to a calculation of the behaviour category. We begin by finding the admissible behaviours. By Remark 55 and the fact that 𝗁𝗍>n"()\mathsf{ht}_{>n}\gg({\mathord{\text{--}}}) is the identity operator, an admissible behaviour β\beta is uniquely determined by the values β(σ1""σk"𝗁𝗍>n)\beta(\sigma_{1}\gg\dots\gg\sigma_{k}\gg\mathsf{ht}_{>n}) where each σi\sigma_{i} is either 𝖴\mathsf{U} or 𝖣\mathsf{D}. Now, the last three axioms imply that these values are determined in turn by the values β(𝗁𝗍>n){𝗍𝗍,𝖿𝖿}\beta(\mathsf{ht}_{>n})\in\{\mathsf{tt},\mathsf{ff}\} for each nn. Finally, by the first axiom, β(𝗁𝗍>m)=𝗍𝗍\beta(\mathsf{ht}_{>m})=\mathsf{tt} implies β(𝗁𝗍>n)=𝗍𝗍\beta(\mathsf{ht}_{>n})=\mathsf{tt} whenever mnm\leqslant n. So the possibilities are either that there is a least nn with β(𝗁𝗍>n)=𝖿𝖿\beta(\mathsf{ht}_{>n})=\mathsf{ff}, or that β(𝗁𝗍>n)=𝗍𝗍\beta(\mathsf{ht}_{>n})=\mathsf{tt} for all nn\in\mathbb{N}.

In fact, each of these possibilities for β\beta does yield an admissible behaviour. Indeed, identifying these possibilities with elements of the set {}\mathbb{N}\cup\{\infty\}, we can try to make this set into a comodel via the formulae of Proposition 56, by taking:

𝗁𝗍>n(k)={(𝗍𝗍,k)if k>n;(𝖿𝖿,k)otherwise,𝖴(k)=k+1 ,𝖣(k)={0if k=0;n1otherwise,\llbracket\mathsf{ht}_{>n}\rrbracket(k)=\begin{cases}(\mathsf{tt},k)&\text{if $k>n$;}\\ (\mathsf{ff},k)&\text{otherwise,}\end{cases}\quad\llbracket\mathsf{U}\rrbracket(k)=k+1\text{ ,}\quad\llbracket\mathsf{D}\rrbracket(k)=\begin{cases}0&\text{if $k=0$;}\\ n-1&\text{otherwise,}\end{cases}

where we take >n\infty>n for any nn\in\mathbb{N}, and +1==1\infty+1=\infty=\infty-1. It is not hard to check that these co-operations do in fact yield a comodel, which is then of necessity the final comodel of the theory of affine Dyck words.

We now compute the comodel 𝒌{\boldsymbol{k}} associated to a behaviour k{}k\in\mathbb{N}\cup\{\infty\}. Because each operator 𝗁𝗍>n"()\mathsf{ht}_{>n}\gg({\mathord{\text{--}}}) is the identity, each unary operation is k\sim_{k}-equivalent to one in the submonoid generated by 𝖴,𝖣T(1)\mathsf{U},\mathsf{D}\in T(1). Further, by the second axiom we have 𝖣0id\mathsf{D}\sim_{0}\mathrm{id}, and so by Lemma 60, also W𝖣WkWWW\mathsf{D}W^{\prime}\sim_{k}WW^{\prime} for any kk\in\mathbb{N}, any W{𝖴,𝖣}W^{\prime}\in\{\mathsf{U},\mathsf{D}\}^{\ast} and any affine Dyck word W:k0W\colon k\rightsquigarrow 0 from kk to 0. Applying this rewrite rule repeatedly, we find that any unary term is k\sim_{k} equivalent to an element of the set

(8.7) {W{𝖴,𝖣}:W:k for some {}} ,\{\,W\in\{\mathsf{U},\mathsf{D}\}^{\ast}:W\colon k\rightsquigarrow\ell\text{ for some }\ell\in\mathbb{N}\cup\{\infty\}\,\}\hbox to0.0pt{ ,\hss}

and we may apply Remark 65 to see that there are in fact no further relations. Indeed, we may make (8.7) into a classifying comodel 𝒌{\boldsymbol{k}} satisfying (5.8) by taking

(8.8) 𝗁𝗍>n(W)={(𝗍𝗍,W)if W:k with >n;(𝖿𝖿,W)otherwise,𝖴(W)=W𝖴 ,𝖣(𝖶)={Wif W:k0;W𝖣otherwise.\begin{gathered}\llbracket\mathsf{ht}_{>n}\rrbracket(W)=\begin{cases}(\mathsf{tt},W)&\text{if $W\colon k\rightsquigarrow\ell$ with $\ell>n$;}\\ (\mathsf{ff},W)&\text{otherwise,}\end{cases}\\ \llbracket\mathsf{U}\rrbracket(W)=W\mathsf{U}\text{ ,}\qquad\llbracket\mathsf{D}\rrbracket(\mathsf{W})=\begin{cases}W&\text{if $W\colon k\rightsquigarrow 0$;}\\ W\mathsf{D}&\text{otherwise.}\end{cases}\end{gathered}

From the preceding calculations, we can now read off:

Proposition 92.

The behaviour category of the theory of affine Dyck words has object-set {}\mathbb{N}\cup\{\infty\}, and morphisms from nn to mm given by affine Dyck words W:nmW\colon n\rightsquigarrow m. Composition is given by concatenation of words. The comodel classifying the behaviour k{}k\in\mathbb{N}\cup\{\infty\} has underlying set (8.7), and co-operations as in (8.8).

The set of Dyck words of length 2n2n is well-known to have the cardinality of the nnth Catalan number Cn=1n+1(2nn)C_{n}=\tfrac{1}{n+1}{2n\choose n}. On the other hand, CnC_{n} also enumerates the set of well-bracketed expressions, such as ((aa)a)(aa)((aa)a)(aa), composed of n+1n+1 aa’s. In fact, there is a bijection between Dyck words of length 2n2n and well-bracketed expressions of (n+1)(n+1) aa’s which can be obtained by interpreting a Dyck word WW as a set of instructions for a stack machine, as follows:

  1. (1)

    Begin with a stack containing the single element aa;

  2. (2)

    Read the next element of the Dyck word WW:

    • If it is 𝖴\mathsf{U}, push an aa onto the stack;

    • If it is 𝖣\mathsf{D}, pop the top two elements x,yx,y of the stack and push (xy)(xy) onto the stack.

  3. (3)

    When WW is consumed, return the single element remaining on the stack.

While the terms on which this stack machine operates are the well-bracketed expressions of aa’s, we can do something similar for stacks of elements of any set VV endowed with a constant aa and a binary operation \ast, obtaining for each Dyck word WW an element of VV built from aa’s and \ast’s. We can understand this in terms of an interpretation f:𝕋𝖣𝗒𝖼𝗄𝕋Stackf\colon\mathbb{T}_{\mathsf{Dyck}}\rightarrow\mathbb{T}_{\mathrm{Stack}} of the theory of affine Dyck words into the theory of a VV-valued stack, given as follows:

(𝗁𝗍>0)f(x,y)=𝗉𝗈𝗉(λv.𝗉𝗎𝗌𝗁v(x),y)\displaystyle(\mathsf{ht}_{>0})^{f}(x,y)=\mathsf{pop}(\lambda v.\,\mathsf{push}_{v}(x),y)
(𝗁𝗍>n+1)f(x,y)=𝗉𝗈𝗉(λv.(𝗁𝗍>n)f(𝗉𝗎𝗌𝗁v(x),𝗉𝗎𝗌𝗁v(y)),y)\displaystyle(\mathsf{ht}_{>n+1})^{f}(x,y)=\mathsf{pop}(\lambda v.\,(\mathsf{ht}_{>n})^{f}(\mathsf{push}_{v}(x),\mathsf{push}_{v}(y)),y)
𝖴f(x)=𝗉𝗎𝗌𝗁a(x)𝖣f(x,y)=𝗉𝗈𝗉(λv.𝗉𝗈𝗉(λw.𝗉𝗎𝗌𝗁vw(x),x),y)\displaystyle\mathsf{U}^{f}(x)=\mathsf{push}_{a}(x)\qquad\mathsf{D}^{f}(x,y)=\mathsf{pop}(\lambda v.\,\mathsf{pop}(\lambda w.\,\mathsf{push}_{v\ast w}(x),x),y)

Here, for the (recursively defined) interpretation of the predicates 𝗁𝗍>n\mathsf{ht}_{>n}, we attempt to pop n+1n+1 elements from the top of our stack of VV’s; if this succeeds, then we undo our pushes and return 𝗍𝗍\mathsf{tt}, while if it at any point fails, then we undo our pushes and return 𝖿𝖿\mathsf{ff}. For the interpretation of 𝖴\mathsf{U} we simply push our constant aVa\in V onto the stack; while for the interpretation of 𝖣(x,y)\mathsf{D}(x,y), we attempt to pop the top two elements v,wv,w from the stack and push vwv\ast w back on. If this succeeds, we continue as xx, but some care is needed if it fails. By the fifth affine Dyck word equation, if our stack contains exactly one element, then 𝖣f\mathsf{D}^{f} should yield a stack with no elements and continue as xx; while by the second equation, if our stack is empty, then 𝖣f\mathsf{D}^{f} should do nothing and continue as yy. The forces the definition given above.

The dynamics of the interpretation of (affine) Dyck words as stack operations is captured by the induced cofunctor 𝔹f:𝔹Stack𝔹Dyck\mathbb{B}_{f}\colon\mathbb{B}_{\mathrm{Stack}}\rightarrow\mathbb{B}_{\mathrm{Dyck}}. It is an easy calculation to see that this is given as follows:

  • On objects, we map SVωS\in V^{\leqslant\omega} to the cardinality |S|{}|S|\in\mathbb{N}\cup\{\infty\} of the initial segment of \mathbb{N} on which SS is defined.

  • On morphisms, given SVωS\in V^{\leqslant\omega} and an affine Dyck word W:|S|kW\colon|S|\rightsquigarrow k, we return the morphism SSS\rightarrow S^{\prime} which updates the stack SS via the sequence of 𝖴\mathsf{U}’s and 𝖣\mathsf{D}’s which specifies WW.

In particular, we may consider the case where (V,,a)(V,\ast,a) is the set of well-bracketed expressions of aa’s under concatenation. Now give a Dyck word WW, we may regard it as an affine Dyck word W:11W\colon 1\rightsquigarrow 1; and now updating the singleton stack aa via W:1=|a|1W\colon 1=|a|\rightsquigarrow 1 yields precisely the well-bracketed expression of aa’s which corresponds to the given Dyck word WW.

8.4. Store

Given a set LL of locations and a family V=(V:L)\vec{V}=(V_{\ell}:\ell\in L) of value sets, the theory of V\vec{V}-valued store comprises a copy of the theory of VV_{\ell}-valued state for each L\ell\in L—with operations (𝗉𝗎𝗍v():vV)(\mathsf{put}_{v}^{(\ell)}:v\in V_{\ell}) and 𝗀𝖾𝗍()\mathsf{get}^{(\ell)}, say—together with, for all kL\ell\neq k\in L, all vVv\in V_{\ell} and all wVkw\in V_{k}, the commutativity axiom:

(8.9) 𝗉𝗎𝗍v()(𝗉𝗎𝗍w(k)(x))𝗉𝗎𝗍w(k)(𝗉𝗎𝗍v()(x)) .\mathsf{put}^{(\ell)}_{v}(\mathsf{put}^{(k)}_{w}(x))\equiv\mathsf{put}^{(k)}_{w}(\mathsf{put}^{(\ell)}_{v}(x))\hbox to0.0pt{ .\hss}

By the argument of [14, Lemma 3.21 and 3.22], these equations also imply the commutativity conditions 𝗉𝗎𝗍v()(𝗀𝖾𝗍(k)(λw.xw))𝗀𝖾𝗍(k)(λw.𝗉𝗎𝗍v()(xw))\mathsf{put}^{(\ell)}_{v}(\mathsf{get}^{(k)}(\lambda w.\,x_{w}))\equiv\mathsf{get}^{(k)}(\lambda w.\,\mathsf{put}^{(\ell)}_{v}(x_{w})) and 𝗀𝖾𝗍()(λv.𝗀𝖾𝗍(k)(λw.xvw))𝗀𝖾𝗍(k)(λw.𝗀𝖾𝗍()(λv.xvw))\mathsf{get}^{(\ell)}(\lambda v.\,\mathsf{get}^{(k)}(\lambda w.\,x_{vw}))\equiv\mathsf{get}^{(k)}(\lambda w.\,\mathsf{get}^{(\ell)}(\lambda v.\,x_{vw})).

A set-based comodel of this theory is a set SS endowed with an LL-indexed family of lens structures (g():SV,p():V×SS)(g^{(\ell)}\colon S\rightarrow V_{\ell},\,p^{(\ell)}\colon V_{\ell}\times S\rightarrow S) which commute in the sense that

p(k)(v,p()(u,s))=p()(v,p(k)(u,s))p^{(k)}(v,p^{(\ell)}(u,s))=p^{(\ell)}(v,p^{(k)}(u,s))

for all ,kL\ell,k\in L, uVu\in V_{\ell} and vVkv\in V_{k}. When LL is finite and each VV_{\ell} is the same set VV, this is the notion of array given in [31, §4]. The final comodel of this theory is the set LV\prod_{\ell\in L}V_{\ell}, under the operations

𝗀𝖾𝗍()(v)=(v,v)𝗉𝗎𝗍v()(v)=v[v/v] .\llbracket\mathsf{get}^{(\ell)}\rrbracket(\vec{v})=(v_{\ell},\vec{v})\qquad\llbracket\mathsf{put}_{v}^{(\ell)}\rrbracket(\vec{v})=\vec{v}[v/v_{\ell}]\hbox to0.0pt{ .\hss}

By similar arguments to those of the preceding sections, we may now show that:

Proposition 93.

The behaviour category 𝔹V\mathbb{B}_{\vec{V}} of V\vec{V}-valued store has set of objects LV\prod_{\ell\in L}V_{\ell}, while a morphism vw\vec{v}\rightarrow\vec{w} is unique when it exists, and exists just when v\vec{v} and w\vec{w} differ in only finitely many positions. The comodel classifying the behaviour vLV\vec{v}\in\prod_{\ell\in L}V_{\ell} is the sub-comodel of the final comodel on the set

{wLV:v and w differ in only finitely many positions} .\{\vec{w}\in\textstyle\prod_{\ell\in L}V_{\ell}:\vec{v}\text{ and }\vec{w}\text{ differ in only finitely many positions}\}\hbox to0.0pt{ .\hss}

For each L\ell\in L, there is an obvious interpretation of the theory of VV_{\ell}-valued state into the theory of V\vec{V}-valued store, and this induces a cofunctor on behaviour categories 𝔹VV\mathbb{B}_{\vec{V}}\rightarrow\nabla V_{\ell} which:

  • On objects, maps v𝔹V\vec{v}\in\mathbb{B}_{\vec{V}} to its component vVv_{\ell}\in\nabla V_{\ell};

  • On morphisms, for each v𝔹V\vec{v}\in\mathbb{B}_{\vec{V}}, sends vvv_{\ell}\rightarrow v^{\prime}_{\ell} in V\nabla V_{\ell} to the unique map vv[v/v]\vec{v}\rightarrow\vec{v}[v^{\prime}_{\ell}/v_{\ell}] in 𝔹V\mathbb{B}_{\vec{V}}.

This captures exactly the “view update” paradigm in database theory: on the one hand, projecting from the state v\vec{v} to its component vv_{\ell} provides a view on the data encoded by v\vec{v}; while lifting the morphism vvv_{\ell}\rightarrow v^{\prime}_{\ell} to one vv[v/v]\vec{v}\rightarrow\vec{v}[v^{\prime}_{\ell}/v_{\ell}] encodes updating the state in light of the given update of the view. The pleasant feature here is that all of this is completely automatic once we specify the way in which VV_{\ell}-valued state is to be interpreted into V\vec{V}-valued store.

8.5. Tape

Our final example is a variant on a particular case of the previous one; it was introduced qua monad in [13], with the presentation given here due to [14] and, independently, [26]. Given a set VV, we consider the constant \mathbb{Z}-indexed family of sets V()=(V:)V^{(\mathbb{Z})}=(V:\ell\in\mathbb{Z}). The theory of a VV-valued tape is obtained by augmenting the theory of V()V^{(\mathbb{Z})}-valued store with two new unary operations 𝗋𝗂𝗀𝗁𝗍\mathsf{right} and 𝗋𝗂𝗀𝗁𝗍1\mathsf{right}^{-1} satisfying the axioms 𝗋𝗂𝗀𝗁𝗍1(𝗋𝗂𝗀𝗁𝗍(x))\mathsf{right}^{-1}(\mathsf{right}(x)), 𝗋𝗂𝗀𝗁𝗍(𝗋𝗂𝗀𝗁𝗍1(x))x\mathsf{right}(\mathsf{right}^{-1}(x))\equiv x, and 𝗋𝗂𝗀𝗁𝗍(𝗉𝗎𝗍u()(x))𝗉𝗎𝗍u(+1)(𝗋𝗂𝗀𝗁𝗍(x))\mathsf{right}(\mathsf{put}_{u}^{(\ell)}(x))\equiv\mathsf{put}_{u}^{(\ell+1)}(\mathsf{right}(x)) for all \ell\in\mathbb{Z}. By arguing much as before, we see that this last axiom implies also that 𝗋𝗂𝗀𝗁𝗍(𝗀𝖾𝗍()(x))𝗀𝖾𝗍(+1)(λv.𝗋𝗂𝗀𝗁𝗍(xv))\mathsf{right}(\mathsf{get}^{(\ell)}(x))\equiv\mathsf{get}^{(\ell+1)}(\lambda v.\,\mathsf{right}(x_{v})).

This theory encapsulates interaction with an doubly-infinite tape, each of whose locations \ell\in\mathbb{Z} contains a value in VV which can be read or updated via 𝗀𝖾𝗍\mathsf{get} and 𝗉𝗎𝗍\mathsf{put}, and whose head position may be moved right or left via 𝗋𝗂𝗀𝗁𝗍\mathsf{right} and 𝗋𝗂𝗀𝗁𝗍1\mathsf{right}^{-1}. A comodel structure on a set SS comprises a \mathbb{Z}-indexed family of commuting lens structures (g():SV,p():V×SS)(g^{(\ell)}\colon S\rightarrow V,p^{(\ell)}\colon V\times S\rightarrow S) together with a bijection r:SSr\colon S\rightarrow S such that r(p(+1)(u,v))=p()(u,r(v))r(p^{(\ell+1)}(u,\vec{v}))=p^{(\ell)}(u,r(\vec{v})) for each \ell\in\mathbb{Z}. It is easy to see that the final comodel of this theory is the final comodel 𝑽{\boldsymbol{V^{\mathbb{Z}}}} of V\vec{V}-valued store, augmented with the co-operations 𝗋𝗂𝗀𝗁𝗍(v)k=vk+1\llbracket\mathsf{right}\rrbracket(\vec{v})_{k}=v_{k+1} and 𝗋𝗂𝗀𝗁𝗍1(v)k=vk1\llbracket\mathsf{right}^{-1}\rrbracket(\vec{v})_{k}=v_{k-1}. By similar calculations to before, we now find that:

Proposition 94.

The behaviour category of the theory of VV-valued tape has object-set VV^{\mathbb{Z}}, while a map vw\vec{v}\rightarrow\vec{w} is an integer ii such that v()+i\vec{v}_{({\mathord{\text{--}}})+i} and w\vec{w} differ in only finitely many places. The comodel classifying the behaviour vV\vec{v}\in V^{\mathbb{Z}} has underlying set

{(i,wV):v and w differ in only finitely many positions}\{(i\in\mathbb{Z},\vec{w}\in V^{\mathbb{Z}}):\vec{v}\text{ and }\vec{w}\text{ differ in only finitely many positions}\}

with operations 𝗋𝗂𝗀𝗁𝗍±1(i,w)=(i±1,w)\llbracket\mathsf{right}^{\pm 1}\rrbracket(i,\vec{w})=(i\pm 1,\vec{w}), 𝗀𝖾𝗍()(i,w)=(wi+,(i,w))\llbracket\mathsf{get}^{(\ell)}\rrbracket(i,\vec{w})=(w_{i+\ell},(i,\vec{w})), and 𝗉𝗎𝗍v()(i,w)=(i,w[v/wi+])\llbracket\mathsf{put}^{(\ell)}_{v}\rrbracket(i,\vec{w})=(i,\vec{w}[v/w_{i+\ell}]).

If the set VV comes endowed with a bijective pairing function ,:V×VV\langle{\mathord{\text{--}}},{\mathord{\text{--}}}\rangle\colon V\times V\rightarrow V, say with inverse (p,q):VV×V(p,q)\colon V\rightarrow V\times V, then there is an interpretation ff of the theory of VV-valued reversible input into the theory of VV-valued tape, given by

𝗋𝖾𝖺𝖽f(λv.xv):=𝗀𝖾𝗍(0)(λw.𝗉𝗎𝗍p(w)(0)(𝗋𝗂𝗀𝗁𝗍(xq(w))))\displaystyle\mathsf{read}^{f}(\lambda v.\,x_{v})\mathrel{\mathop{:}}=\mathsf{get}^{(0)}(\lambda w.\,\mathsf{put}^{(0)}_{p(w)}(\mathsf{right}(x_{q(w)})))
(𝗎𝗇𝗋𝖾𝖺𝖽u)f(x):=𝗅𝖾𝖿𝗍(𝗀𝖾𝗍(0)(λw.𝗉𝗎𝗍w,u(0)(x))) .\displaystyle(\mathsf{unread}_{u})^{f}(x)\mathrel{\mathop{:}}=\mathsf{left}(\mathsf{get}^{(0)}(\lambda w.\,\mathsf{put}^{(0)}_{\langle w,u\rangle}(x)))\hbox to0.0pt{ .\hss}

This induces a cofunctor on behaviour categories which acts as follows.

  • On objects, for each vV\vec{v}\in V^{\mathbb{Z}} in the behaviour category for a VV-valued tape, we produce the object f(v)Vf^{\ast}(\vec{v})\in V^{\mathbb{N}} given by f(v)n=q(vn)f^{\ast}(\vec{v})_{n}=q(v_{n});

  • On morphisms, if we are given vV\vec{v}\in V^{\mathbb{Z}} and a map i:f(v)Wi\colon f^{\ast}(\vec{v})\rightarrow W in the behaviour category for VV-valued reversible input then our cofunctor lifts this to the morphism i:vwi\colon\vec{v}\rightarrow\vec{w} in the behaviour category of VV-valued tape where

    wk={vk+i if k<i or k>N;p(vk+i) if ik<0;p(vk+i),Wk if 0k.w_{k}=\begin{cases}v_{k+i}&\text{ if $k<-i$ or $k>N$;}\\ p(v_{k+i})&\text{ if $-i\leqslant k<0$;}\\ \langle p(v_{k+i}),W_{k}\rangle&\text{ if $0\leqslant k$.}\end{cases}

9. Examples and applications: costructure

In this final section, we illustrate our understanding of the costructure functor by providing some sample calculations of the behaviour categories associated to comonads on 𝒮et\mathrm{\mathcal{S}et}.

9.1. Coalgebras for polynomial endofunctors

For any endofunctor F:𝒮et𝒮etF\colon\mathrm{\mathcal{S}et}\rightarrow\mathrm{\mathcal{S}et}, we can consider the category F-coalgF\text{-}\mathrm{coalg} of FF-coalgebras, i.e., sets XX endowed with a map ξ:XFX\xi\colon X\rightarrow FX. As is well-known, for suitable choices of FF, such coalgebras can model diverse kinds of automaton and transition system; see [33] for an overview.

If FF is accessible, then the forgetful functor F-coalg𝒮etF\text{-}\mathrm{coalg}\rightarrow\mathrm{\mathcal{S}et} will have a right adjoint and be strictly comonadic, meaning that we can identify F-coalgF\text{-}\mathrm{coalg} with the category of Eilenberg–Moore coalgebras of the induced comonad 𝖰F\mathsf{Q}_{F} on 𝒮et\mathrm{\mathcal{S}et}; in light of this, we call 𝖰F\mathsf{Q}_{F} the cofree comonad on the endofunctor FF. Explicitly, the values of the cofree comonad can be described via the greatest fixpoint formula

(9.1) QF(V)=νX.V×F(X) .Q_{F}(V)=\nu X.\,V\times F(X)\hbox to0.0pt{ .\hss}

The objective of this section is to calculate the behaviour categories of cofree comonads 𝖰F\mathsf{Q}_{F} for some natural choices of FF. To begin with, let us assume that FF is polynomial in the sense of Section 3.1, meaning that it is can be written as a coproduct of representables

(9.2) F(X)=σΣX|σ|F(X)=\textstyle\sum_{\sigma\in\Sigma}X^{|\sigma|}

for some set Σ\Sigma and family of sets (|σ|:σΣ)(|\sigma|:\sigma\in\Sigma). In this case, as is well-known, the fixpoint (9.1) can be described as a set of trees.

Definition 95 (FF-trees).

Let FF be a polynomial endofunctor (9.2).

  • An FF-path of length kk is a sequence P=σ0e1σ1ekσkP=\sigma_{0}e_{1}\sigma_{1}\cdots e_{k}\sigma_{k} where each σiΣ\sigma_{i}\in\Sigma and each ei|σi1|e_{i}\in|\sigma_{i-1}|.

  • An FF-tree is a subset TT of the set of FF-paths such that:

    1. (i)

      TT contains a unique path of length 0, written T\ast\in T;

    2. (ii)

      If TT contains σ0e1ekσkek+1σk+1\sigma_{0}e_{1}\cdots e_{k}\sigma_{k}e_{k+1}\sigma_{k+1}, then it contains σ0e1ekσk\sigma_{0}e_{1}\cdots e_{k}\sigma_{k};

    3. (iii)

      If TT contains σ0e1ekσk\sigma_{0}e_{1}\cdots e_{k}\sigma_{k}, then for each ek+1|σk|e_{k+1}\in|\sigma_{k}|, it contains a unique path of the form σ0e1ekσkek+1σk+1\sigma_{0}e_{1}\cdots e_{k}\sigma_{k}e_{k+1}\sigma_{k+1}.

  • If VV is a set, then a VV-labelling for an FF-tree TT is a function :TV\ell\colon T\rightarrow V.

  • If TT is an FF-tree and P=σ0e1ekσkTP=\sigma_{0}e_{1}\cdots e_{k}\sigma_{k}\in T, then TPT_{P} is the FF-tree

    TP={σkek+1emσm:σ0e1emσmT} .T_{P}=\{\sigma_{k}e_{k+1}\cdots e_{m}\sigma_{m}:\sigma_{0}e_{1}\cdots e_{m}\sigma_{m}\in T\}\hbox to0.0pt{ .\hss}

    If :TV\ell\colon T\rightarrow V is a labelling for TT, then P:TPV\ell_{P}\colon T_{P}\rightarrow V is the labelling with P(σkek+1emσm)=(σ0e1emσm)\ell_{P}(\sigma_{k}e_{k+1}\cdots e_{m}\sigma_{m})=\ell(\sigma_{0}e_{1}\cdots e_{m}\sigma_{m}).

Lemma 96.

The cofree comonad 𝖰F\mathsf{Q}_{F} on a polynomial FF as in (9.2) is given as follows:

  • QF(V)Q_{F}(V) is the set of VV-labelled FF-trees;

  • The counit εV:QF(V)V\varepsilon_{V}\colon Q_{F}(V)\rightarrow V sends (T,)(T,\ell) to ()V\ell(\ast)\in V;

  • The comultiplication δV:QF(V)QFQF(V)\delta_{V}\colon Q_{F}(V)\rightarrow Q_{F}Q_{F}(V) sends (T,)(T,\ell) to (T,)(T,\ell^{\sharp}), where :TQF(V)\ell^{\sharp}\colon T\rightarrow Q_{F}(V) sends PP to (TP,P)(T_{P},\ell_{P}).∎

We may use this result to calculate the behaviour category 𝔹F\mathbb{B}_{F} of the comonad 𝖰F\mathsf{Q}_{F}. Clearly, objects of 𝔹F\mathbb{B}_{F} are elements of QF(1)Q_{F}(1), i.e., (unlabelled) FF-trees. Morphisms of 𝔹F\mathbb{B}_{F} with domain TT are, by definition, natural transformations QTidQ_{T}\Rightarrow\mathrm{id}; but the functor QTQ_{T} is visibly isomorphic to the representable functor ()T({\mathord{\text{--}}})^{T}, so that by the Yoneda lemma, maps in 𝔹F\mathbb{B}_{F} with domain TT correspond bijectively with elements PTP\in T. Given this, we may easily read off the remainder of the structure in Definition 74 to obtain:

Proposition 97.

Let FF be a polynomial endofunctor of 𝒮et\mathrm{\mathcal{S}et}. The behaviour category 𝔹F\mathbb{B}_{F} of the cofree comonad 𝖰F\mathsf{Q}_{F} has:

  • Objects given by FF-trees TT;

  • Morphisms P:TTP\colon T\rightarrow T^{\prime} are elements PTP\in T such that TP=TT_{P}=T^{\prime};

  • Identities are given by 1T=:TT1_{T}=\ast\colon T\rightarrow T;

  • Composition is given by (σkek+1emσm)(σ0e1ekσk)=σ0e1emσm(\sigma_{k}e_{k+1}\cdots e_{m}\sigma_{m})\circ(\sigma_{0}e_{1}\cdots e_{k}\sigma_{k})=\sigma_{0}e_{1}\cdots e_{m}\sigma_{m}.

It is not hard to see that 𝔹F\mathbb{B}_{F} is, in fact, the free category on a graph: the generating morphisms are those of the form σ0e1σ1\sigma_{0}e_{1}\sigma_{1}.

Remark 98.

When FF is polynomial, the cofree comonad QFQ_{F} is again polynomial: indeed, we have QF(V)TF-treeVTQ_{F}(V)\cong\sum_{T\in F\text{-}\mathrm{tree}}V^{T}. Thus 𝖰F\mathsf{Q}_{F} is a presheaf comonad, and it will follow from Proposition 88 below that it is in fact the presheaf comonad of the behaviour category 𝔹F\mathbb{B}_{F}. Thus, we arrive at the (not entirely obvious) conclusion that, for FF polynomial, the category of FF-coalgebras is equivalent to the presheaf category [𝔹F,𝒮et][\mathbb{B}_{F},\mathrm{\mathcal{S}et}].

Example 99.

Let EE be an alphabet. A deterministic automaton over EE is a set SS of states together with a function (t,h):SSE×{,}(t,h)\colon S\rightarrow S^{E}\times\{\top,\bot\}. For a state sSs\in S, the value h(s)h(s) indicates whether or not hh is an accepting state; while t(s)(e)St(s)(e)\in S gives the state reached from ss by transition along eEe\in E.

Deterministic automata are FF-coalgebras for the polynomial functor F(X)=a{,}XEF(X)=\sum_{a\in\{\bot,\top\}}X^{E}. It is easy to see that, in this case, the set of FF-trees can be identified with the power-set 𝒫(E){\mathcal{P}}(E^{\ast}) via the assignment

T{e1enE:σ0e1σn1enT} .T\mapsto\{e_{1}\cdots e_{n}\in E^{\ast}:\sigma_{0}e_{1}\dots\sigma_{n-1}e_{n}\top\in T\}\hbox to0.0pt{ .\hss}

In these terms, the behaviour category 𝔹F\mathbb{B}_{F} can be identified with the free category on the graph whose vertices are subsets of EE^{\ast}, and whose edges are e:LeLe\colon L\rightarrow\partial_{e}L for each LEL\subseteq E^{\ast} eEe\in E, where eL={e1enE:ee1enL}\partial_{e}L=\{e_{1}\cdots e_{n}\in E^{\ast}:ee_{1}\cdots e_{n}\in L\}. Note that this is precisely the transition graph of the final deterministic automaton over EE.

9.2. Coalgebras for non-polynomial endofunctors

When we consider cofree comonads over non-polynomial endofunctors FF, things become more delicate. To illustrate this, let us consider the case of the finite multiset endofunctor

M(X)=nXn/𝔖n .M(X)=\textstyle\sum_{n\in\mathbb{N}}X^{n}/\mathfrak{S}_{n}\hbox to0.0pt{ .\hss}

An FF-coalgebra can be seen as a kind of non-deterministic transition system. As in the preceding section, we have a description of the associated cofree comonad in terms of trees:

Definition 100 (Symmetric trees).

A symmetric tree TT is a diagram of finite sets and functions

\ext@arrow01200\rightarrowfill@Tn\ext@arrow01200\rightarrowfill@\ext@arrow01200\rightarrowfill@T1\ext@arrow01200\rightarrowfill@T0\cdots\ext@arrow 01{20}0\rightarrowfill@{}{\partial}T_{n}\ext@arrow 01{20}0\rightarrowfill@{}{\partial}\cdots\ext@arrow 01{20}0\rightarrowfill@{}{\partial}T_{1}\ext@arrow 01{20}0\rightarrowfill@{}{\partial}T_{0}

where T0={}T_{0}=\{\ast\}. We may write |T||T| for the set kTk\sum_{k}T_{k}. A VV-labelling for a symmetric tree TT is a function :|T|V\ell\colon|T|\rightarrow V. Given a VV-labelled tree (T,)(T,\ell) and tTkt\in T_{k}, we write (Tt,t)(T_{t},\ell_{t}) for the labelled tree with (Tt)n={uTn+k:k(u)=t}(T_{t})_{n}=\{u\in T_{n+k}:\partial^{k}(u)=t\}, and with \partial’s and labelling inherited from TT. An isomorphism θ:(T,)(T,)\theta\colon(T,\ell)\rightarrow(T^{\prime},\ell^{\prime}) of VV-labelled trees is a family of functions θn:TnTn\theta_{n}\colon T_{n}\rightarrow T^{\prime}_{n} commuting with the \partial’s and the maps to VV.

Lemma 101.

The cofree comonad on the finite multiset endofunctor MM has:

  • QM(V)Q_{M}(V) given by the set of isomorphism-classes of VV-labelled symmetric trees;

  • The counit εV:QM(V)V\varepsilon_{V}\colon Q_{M}(V)\rightarrow V given by (T,)()(T,\ell)\mapsto\ell(\ast);

  • The comultiplication δV:QM(V)QMQM(V)\delta_{V}\colon Q_{M}(V)\rightarrow Q_{M}Q_{M}(V) given by (T,)(T,)(T,\ell)\mapsto(T,\ell^{\sharp}), where :|T|QM(V)\ell^{\sharp}\colon|T|\rightarrow Q_{M}(V) sends tTkt\in T_{k} to (Tt,t)(T_{t},\ell_{t}).∎

Given a symmetric tree TT, we call tTkt\in T_{k} rigid if any automorphism of TT fixes tt.

Proposition 102.

The behaviour category of the cofree comonad 𝖰M\mathsf{Q}_{M} has:

  • Objects given by isomorphism-class representatives of symmetric trees TT;

  • Morphisms t:TTt\colon T\rightarrow T^{\prime} are rigid elements tTt\in T such that TtTT_{t}\cong T^{\prime};

  • The identity on TT is :TT\ast\colon T\rightarrow T;

  • The composite of t:TTt\colon T\rightarrow T^{\prime} and u:TT′′u\colon T^{\prime}\rightarrow T^{\prime\prime} is θ(u):TT′′\theta(u)\colon T\rightarrow T^{\prime\prime}, where θ\theta is any tree isomorphism TTtT^{\prime}\rightarrow T_{t}.

Proof.

Let us write 𝖰=𝖰M\mathsf{Q}=\mathsf{Q}_{M}. Clearly the object-set Q(1)Q(1) of the behaviour category can be identified with a set of isomorphism-class representatives of symmetric trees. Now, for any such representative TT, the subfunctor QTQQ_{T}\subseteq Q sends a set VV to the set of all isomorphism-classes of VV-labellings of TT, which is easily seen to be the quotient V|T|/Aut(T)V^{|T|}\delimiter 84079374\mathopen{}\mathrm{Aut}(T) of V|T|V^{|T|} by the evident action of the group of tree automorphisms of TT. Thus, by the Yoneda lemma, the set of natural transformations QTidQ_{T}\Rightarrow\mathrm{id} can be identified with the set of elements t|T|t\in|T| which are fixed by the Aut(T)\mathrm{Aut}(T) action, i.e., the rigid elements of TT. For a given rigid element t|T|t\in|T|, the corresponding QTidQ_{T}\Rightarrow\mathrm{id} sends a VV-labelling :|T|V\ell\colon|T|\rightarrow V in QT(V)Q_{T}(V) to (t)V\ell(t)\in V; and it follows that the unique factorisation in (6.1) is of the form QTQTQ_{T}\Rightarrow Q_{T^{\prime}} where TTtT^{\prime}\cong T_{t}. Tracing through the remaining aspects of the definition of behaviour category yields the result. ∎

For a similar example in this vein, we may calculate the behaviour category of the cofree comonad generated by the finite powerset functor PfP_{f}. In this case, things are even more degenerate: the behaviour category turns out to be the discrete category on the final PfP_{f}-coalgebra.

9.3. Local homeomorphisms

For our final example, we compute the behaviour category of the comonad classifying local homeomorphisms over a topological space.

Definition 103 (Reduced power).

If A,XA,X are sets and {\mathcal{F}} is a filter of subsets of XX, then two maps φ,ψ:XA\varphi,\psi\colon X\rightarrow A are {\mathcal{F}}-equivalent when {xX:φ(x)=ψ(x)}\{x\in X:\varphi(x)=\psi(x)\}\in{\mathcal{F}}. The reduced power AA^{\mathcal{F}} is the quotient of AXA^{X} by {\mathcal{F}}-equivalence.

Definition 104 (Sheaf comonad).

Let XX be a topological space. The sheaf comonad 𝖰X\mathsf{Q}_{X} is the accessible comonad on 𝒮et\mathrm{\mathcal{S}et} induced by the adjunction

(9.3) h/X\textstyle{{\mathrm{\mathcal{L}h}/X}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}U\scriptstyle{U}C\scriptstyle{C}\scriptstyle{\top}𝒮et/X\textstyle{{\mathrm{\mathcal{S}et}/X}\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces\ignorespaces}Σ\scriptstyle{\Sigma}Δ\scriptstyle{\Delta}\scriptstyle{\top}𝒮et ,\textstyle{{\mathrm{\mathcal{S}et}}\hbox to0.0pt{ ,\hss}}

where h/X\mathrm{\mathcal{L}h}/X is the category of local homeomorphisms over XX, where UU is the evident forgetful functor, and where CC sends p:AXp\colon A\rightarrow X to the space of germs of partial sections of pp. If we write 𝒩x{\mathcal{N}}_{x} for the filter of open neighbourhoods of xXx\in X, then then this comonad has QX(A)=xXA𝒩xQ_{X}(A)=\sum_{x\in X}A^{{\mathcal{N}}_{x}}, and counit and comultiplication

(9.4) εA:xA𝒩x\displaystyle\varepsilon_{A}\colon\textstyle\sum_{x}A^{{\mathcal{N}}_{x}} A\displaystyle\rightarrow A δA:xA𝒩x\displaystyle\quad\delta_{A}\colon\textstyle\sum_{x}A^{{\mathcal{N}}_{x}} x(xA𝒩x)𝒩x\displaystyle\rightarrow\textstyle\sum_{x}\bigl{(}\sum_{x^{\prime}}A^{{\mathcal{N}}_{x^{\prime}}}\bigr{)}^{{\mathcal{N}}_{x}}
(x,φ)\displaystyle(x,\varphi) φ(x)\displaystyle\mapsto\varphi(x) (x,φ)\displaystyle(x,\varphi) (x,λy.(y,φ)) .\displaystyle\mapsto\bigl{(}x,\lambda y.\,(y,\varphi)\bigr{)}\hbox to0.0pt{ .\hss}

The adjunction in (9.3) is in fact strictly comonadic, so that we can identify the category of 𝖰X\mathsf{Q}_{X}-coalgebras with the category of local homeomorphisms (=sheaves) over XX.

Proposition 105.

Let XX be a topological space. The behaviour category of the sheaf comonad 𝖰X\mathsf{Q}_{X} is the poset (X,)(X,\leqslant) of points of XX under the specialisation order: thus xyx\leqslant y just when every open set containing xx also contains yy.

Proof.

Writing 𝖰\mathsf{Q} for 𝖰X\mathsf{Q}_{X}, we clearly have Q(1)=XQ(1)=X, so that objects of the behaviour category are points of XX. To characterise the morphisms with domain xXx\in X, we observe that the subfunctor QxQQ_{x}\subseteq Q is the reduced power functor ()𝒩x({\mathord{\text{--}}})^{{\mathcal{N}}_{x}}, which can also be written as the directed colimit of representable functors colimU𝒩x()U\mathrm{colim}_{U\in{\mathcal{N}}_{x}}({\mathord{\text{--}}})^{U}. Thus, by the Yoneda lemma, the set of natural transformations QxidQ_{x}\Rightarrow\mathrm{id} can be identified with the filtered intersection U𝒩xU\bigcap_{U\in{\mathcal{N}}_{x}}U, i.e., with the upset {yX:xy}\{y\in X:x\leqslant y\} of xx for the specialisation order. Given yxy\geqslant x, the corresponding natural transformation QxidQ_{x}\Rightarrow\mathrm{id} has components φφ(y)\varphi\mapsto\varphi(y); whence the unique factorisation in (6.1) is of the form QxQyQ_{x}\Rightarrow Q_{y}. By definition of behaviour category, we conclude that 𝔹𝖰\mathbb{B}_{\mathsf{Q}} is the specialisation poset of XX. ∎

What we learn from this is that a local homeomorphism p:SXp\colon S\rightarrow X can be seen as a coalgebraic structure with set of “states” SS, with the “behaviour” associated to a state ss given by p(s)p(s), and with the possibility of transitioning uniformly from a state ss of behaviour xx to a state ss^{\prime} of behaviour yy whenever xyx\leqslant y. This is intuitively easy to see: given sSs\in S of behaviour xx, we pick an open neighbourhood USU\subseteq S mapping homeomorphically onto an open V=p(U)XV=p(U)\subseteq X. Since x=p(s)Vx=p(s)\in V and xyx\leqslant y, also yVy\in V and so we may define ss^{\prime} of behaviour yy to be (p|U)1(y)U(\left.{p}\right|_{U})^{-1}(y)\in U.

References

  • [1] Abbott, M., Altenkirch, T., and Ghani, N. Containers: constructing strictly positive types. Theoretical Computer Science 342, 1 (2005), 3–27.
  • [2] Aguiar, M. Internal categories and quantum groups. PhD thesis, Cornell University, 1997.
  • [3] Ahman, D., and Bauer, A. Runners in action. In Programming Languages and Systems (2020), vol. 12075 of Lecture Notes in Computer Science, Springer, pp. 29–55.
  • [4] Ahman, D., Chapman, J., and Uustalu, T. When is a container a comonad? In Foundations of software science and computational structures, vol. 7213 of Lecture Notes in Comput. Sci. Springer, Heidelberg, 2012, pp. 74–88.
  • [5] Ahman, D., and Uustalu, T. Coalgebraic update lenses. In Proceedings of the 30th Conference on the Mathematical Foundations of Programming Semantics (MFPS XXX) (2014), vol. 308 of Electron. Notes Theor. Comput. Sci., Elsevier Sci. B. V., Amsterdam, pp. 25–48.
  • [6] Ahman, D., and Uustalu, T. Update monads: cointerpreting directed containers. In 19th International Conference on Types for Proofs and Programs, vol. 26 of LIPIcs. Leibniz Int. Proc. Inform. Schloss Dagstuhl. Leibniz-Zent. Inform., Wadern, 2014, pp. 1–23.
  • [7] Ahman, D., and Uustalu, T. Taking updates seriously. In Proceedings of the 6th International Workshop on Bidirectional Transformations (2017), CEUR Workshop Proceedings, pp. 59–73.
  • [8] Barr, M., and Wells, C. Toposes, triples and theories, vol. 278 of Grundlehren der Mathematischen Wissenschaften. Springer, 1985.
  • [9] Diers, Y. Spectres et localisations relatifs à un foncteur. Comptes rendus hebdomadaires des séances de l’Académie des sciences 287 (1978), 985–988.
  • [10] Dubuc, E. J. Kan extensions in enriched category theory, vol. 145 of Lecture Notes in Mathematics. Springer, 1970.
  • [11] Foster, J. N., Greenwald, M. B., Moore, J. T., Pierce, B. C., and Schmitt, A. Combinators for bidirectional tree transformations: A linguistic approach to the view-update problem. ACM Transactions on Programming Languages and Systems 29, 3 (2007), 17–es.
  • [12] Goncharov, S. Trace semantics via generic observations. In Algebra and coalgebra in computer science, vol. 8089 of Lecture Notes in Comput. Sci. Springer, Heidelberg, 2013, pp. 158–174.
  • [13] Goncharov, S., Milius, S., and Silva, A. Towards a coalgebraic chomsky hierarchy. In TCS 2014, Rome (2014), vol. 8705 of Lecture Notes in Computer Science, Springer, pp. 265–280.
  • [14] Goncharov, S., Milius, S., and Silva, A. Toward a uniform theory of effectful state machines. ACM Transactions on Computational Logic 21, 3 (2020).
  • [15] Higgins, P. J., and Mackenzie, K. C. H. Duality for base-changing morphisms of vector bundles, modules, Lie algebroids and Poisson structures. Mathematical Proceedings of the Cambridge Philosophical Society 114, 3 (1993), 471–488.
  • [16] Johnstone, P. T. Collapsed toposes and Cartesian closed varieties. Journal of Algebra 129, 2 (1990), 446–480.
  • [17] Jónsson, B., and Tarski, A. On two properties of free algebras. Mathematica Scandinavica 9 (1961), 95–101.
  • [18] Katsumata, S., Rivas, E., and Uustalu, T. Interaction laws of monads and comonads. Preprint, available as arXiv:1912.13477, 2019.
  • [19] Kelly, G. M., and Power, A. J. Adjunctions whose counits are coequalizers, and presentations of finitary enriched monads. Journal of Pure and Applied Algebra 89 (1993), 163–179.
  • [20] Kupke, C., and Leal, R. A. Characterising behavioural equivalence: three sides of one coin. In Algebra and coalgebra in computer science, vol. 5728 of Lecture Notes in Computer Science. Springer, 2009, pp. 97–112.
  • [21] Lawvere, F. W. Functorial semantics of algebraic theories. PhD thesis, Columbia University, 1963. Also Proc. Nat. Acad. Sci. U.S.A. 50 (1963), 869–872. Republished as: Reprints in Theory and Applications of Categories 5 (2004).
  • [22] Manes, E. Algebraic theories, vol. 26 of Graduate Texts in Mathematics. Springer, 1976.
  • [23] Møgelberg, R. E., and Staton, S. Linear usage of state. Logical Methods in Computer Science 10, 1 (2014), 1:17, 52.
  • [24] Moggi, E. Notions of computation and monads. Information and Computation 93 (1991), 55–92.
  • [25] Pattinson, D., and Schröder, L. Sound and complete equational reasoning over comodels. In The 31st Conference on the Mathematical Foundations of Programming Semantics (MFPS XXXI), vol. 319 of Electronic Notes in Theoretical Computer Science. Elsevier, 2015, pp. 315–331.
  • [26] Pattinson, D., and Schröder, L. Program equivalence is coinductive. In Proceedings of the 31st Annual ACM-IEEE Symposium on Logic in Computer Science (LICS 2016) (2016), ACM, p. 10.
  • [27] Plotkin, G., and Power, J. Adequacy for algebraic effects. In Foundations of software science and computation structures (Genova, 2001), vol. 2030 of Lecture Notes in Comput. Sci. Springer, Berlin, 2001, pp. 1–24.
  • [28] Plotkin, G., and Power, J. Notions of computation determine monads. In Foundations of software science and computation structures (Grenoble, 2002), vol. 2303 of Lecture Notes in Computer Science. Springer, Berlin, 2002, pp. 342–356.
  • [29] Plotkin, G., and Power, J. Algebraic operations and generic effects. vol. 11. 2003, pp. 69–94. On formal description of computations—refined structures and new trends (Cork, 2000).
  • [30] Plotkin, G., and Power, J. Tensors of comodels and models for operational semantics. Electronic Notes in Theoretical Computer Science 218 (2008), 295–311.
  • [31] Power, J., and Shkaravska, O. From comodels to coalgebras: state and arrays. In Proceedings of the Workshop on Coalgebraic Methods in Computer Science (2004), vol. 106 of Electron. Notes Theor. Comput. Sci., Elsevier Sci. B. V., Amsterdam, pp. 297–314.
  • [32] Renault, J. A groupoid approach to CC^{\ast}-algebras, vol. 793 of Lecture Notes in Mathematics. Springer, Berlin, 1980.
  • [33] Rutten, J. J. M. M. Universal coalgebra: a theory of systems. vol. 249. 2000, pp. 3–80. Modern algebra and its applications (Nashville, TN, 1996).
  • [34] Rutten, J. J. M. M., and Turi, D. On the foundations of final semantics: nonstandard sets, metric spaces, partial orders. In Semantics: foundations and applications (Beekbergen, 1992), vol. 666 of Lecture Notes in Comput. Sci. Springer, Berlin, 1993, pp. 477–530.
  • [35] Thielecke, H. Categorical structure of continuation passing style. PhD thesis, University of Edinburgh, 1997.
  • [36] Uustalu, T. Stateful runners of effectful computations. In The 31st Conference on the Mathematical Foundations of Programming Semantics (MFPS XXXI), vol. 319 of Electron. Notes Theor. Comput. Sci. Elsevier Sci. B. V., Amsterdam, 2015, pp. 403–421.