1 Introduction

Coinduction has been widely studied since Milner’s work on CCS [26]. In concurrency theory, it is usually exploited to define behavioural equivalences or preorders on processes and to obtain powerful proof principles. Coinduction can also be used for programming languages, to define and manipulate infinite data-structures like streams or potentially infinite trees. For instance, streams can be defined using systems of differential equations [37]. In particular, pointwise addition of two streams xy can be defined by the following equations, where \(x_0\) and \(x'\) respectively denote the head and the tail of the stream x.

$$\begin{aligned} \begin{aligned} (x \oplus y)_0&= x_0 + y_0\\ (x \oplus y)'&= x' \oplus y' \end{aligned} \end{aligned}$$
(1)

Coinduction as a proof principle for concurrent systems can nicely be presented at the abstract level of complete lattices [30, 33]: bisimilarity is the greatest fixpoint of a monotone function on the complete lattice of binary relations. In contrast, coinduction as a tool to manipulate infinite data-structures requires one more step to be presented abstractly: moving to universal coalgebra [15]. For instance, streams are the carrier of the final coalgebra of an endofunctor on \(\mathsf {Set}\), and simple systems of differential equations are just plain coalgebras.

In both cases one frequently needs enhancements of the coinduction principle [38, 39]. Indeed, rather than working with plain bisimulations, which can be rather large, one often uses “bisimulations up-to”, which are not proper bisimulations but are nevertheless contained in bisimilarity [1, 2, 10, 16, 24, 27, 40]. The situation with infinite data-structures is similar. For instance, defining the shuffle product on streams is typically done using equations of the following shape,

$$\begin{aligned} \begin{aligned} (x \otimes y)_0&= x_0 \times y_0\\ (x \otimes y)'&= x \otimes y' ~\oplus ~ x' \otimes y \end{aligned} \end{aligned}$$
(2)

which fall out of the scope of plain coinduction due to the call to pointwise addition [12, 37].

Enhancements of the bisimulation proof method have been introduced by Milner from the beginning [26], and further studied by Sangiorgi [38, 39] and then by the first author [30, 33]. Let us recall the standard formulation of coinduction in complete lattices: by Knaster-Tarski’s theorem [19, 42], any monotone function b on a complete lattice admits a greatest fixpoint \(\nu b\) that satisfies the following coinduction principle:

$$\begin{aligned} \frac{x \le y \le b(y)}{x \le \nu b}\,\,{{\textsc {coinduction}}} \end{aligned}$$
(3)

In words, to prove that some point x is below the greatest fixpoint, it suffices to exhibit a point y above x which is an invariant, i.e., a post-fixpoint of b. Enhancements, or up-to techniques, make it possible to alleviate the second requirement: instead of working with post-fixpoints of b, one might use post-fixpoints of \(b\circ f\), for carefully chosen functions f:

$$\begin{aligned} \frac{x \le y \le b(f(y))}{x \le \nu b}\,\,{{\textsc {coinduction}\; \textsc {up}\; \textsc {to}}}\,\,f \end{aligned}$$
(4)

Taking inspiration from the work of Hur et al. [13], the first author recently proposed to systematically use for f the largest compatible function [31], i.e., the largest function t such that \(t\circ b\le b \circ t\). Such a function always exists and is called the companion. It enjoys many good properties, the most important one possibly being that it is a closure operator: \(t\circ t=t\). Parrow and Weber also characterised it extensionally in terms of the final sequence of the function b [29, 31]:

$$\begin{aligned} t:x\mapsto \bigwedge _{x\le b_\alpha } b_\alpha&\text {where } {\left\{ \begin{array}{ll} b_\lambda \triangleq \bigwedge _{\alpha <\lambda } b_\alpha &{} \text {for limit ordinals}\\ b_{\alpha +1} \triangleq b(b_\alpha ) &{} \text {for successor ordinals} \end{array}\right. } \end{aligned}$$
(5)

In the present paper, we give a categorical account of these ideas, generalising them from complete lattices to universal coalgebra, in order to encompass important instances of coinduction such as solving systems of equations on infinite data-structures.

Let us first be more precise about our example on streams. We consider there the \(\mathsf {Set}\) functor \(BX=\mathbb R\times X\), whose final coalgebra is the set \(\mathbb R^\omega \) of streams over the reals. This means that any B-coalgebra \(\left( {X,f}\right) \) defines a function from X to streams. Take for instance the following coalgebra over the two-elements set \(2=\{0,1\}\): \(0 \mapsto \left( {0.3,\,1}\right) \), \(1 \mapsto \left( {0.7,\,0}\right) \). This coalgebra can be seen as a system of two equations, whose unique solution is a function from 2 to \(\mathbb R^\omega \), i.e., two streams, where the first has value 0.3 at all even positions and 0.7 at all odd positions.

In a similar manner, one can define binary operations on streams by considering coalgebras whose carrier consists of pairs of streams. For instance, the previous system of equations characterising pointwise addition (1) is faithfully represented by the following coalgebra:

$$\begin{aligned}&(\mathbb R^\omega )^2 \rightarrow B((\mathbb R^\omega )^2)\\&\left( {x,y}\right) \mapsto \left( {x_0+y_0,~\left( {x',y'}\right) }\right) \end{aligned}$$

Unfortunately, as explained above, systems of equations defining operations like shuffle product (2) cannot be represented easily in this way: we would need to call pointwise addition on streams that are not yet fully defined.

To this end, one can weaken the requirement of a B-coalgebra to that of a BF-coalgebra, when there exists a distributive law \(\lambda :FB\Rightarrow BF\) of a monad F over B [5, 12]. The proof relies on the so-called generalised powerset construction [41], and this precisely amounts to using an up-to technique. Such a use of distributive laws is actually rather standard in operational semantics [5, 17, 43]; they properly generalise the notion of compatible function. In order to follow [31], we thus focus on the largest distributive law.

Our first contribution consists in showing that if a functor B admits a final distributive law (called the companion), then (1) this distributive law is that of a monad T over B, and (2) any BT-coalgebra has a unique morphism to the final B-coalgebra, representing a solution to the system of equations modeled by the coalgebra (Sect. 3). In complete lattices, this corresponds to the facts that the companion is a closure operator and that it can be used as an up-to technique.

Then we move to conditions under which the companion exists. We start from the final sequence of the functor B, which is commonly used to obtain the existence of a final coalgebra [3, 4], and we show that the companion actually coincides with the codensity monad of this sequence, provided that this codensity monad exists and is preserved by B (Theorem 5.1). Those conditions are satisfied by all polynomial functors. This link with the final sequence of the functor makes it possible to recover Parrow and Weber’s characterisation (Eq. (5)).

We can go even further for \(\omega \)-continuous endofunctors on \(\mathsf {Set}\): the codensity monad of the final sequence can be characterised in terms of a new abstract notion of causal algebra (Definition 6.1). On streams, this notion coincides with the standard notion of causality [12]: causal algebras (on streams) correspond to operations such that the n-th value of the result only depends on the n-th first values of the arguments. For instance, pointwise addition and shuffle product are causal algebras for the functor \(SX=X^2\).

These two characterisations of the companion in terms of the codensity monad and in terms of causal algebras are the key theorems of the present paper. We study some of their consequences in Sect. 7.

First, given a causal algebra for a functor F, we get that any system of equations represented as a BF-coalgebra admits a unique solution. Such a technique makes it possible to define shuffle product in a streamlined way, without using distributive laws: using pointwise stream addition as a causal S-algebra, Eq. (2) can be represented by the following BS-coalgebra:

$$\begin{aligned}&(\mathbb R^\omega )^2 \rightarrow BS((\mathbb R^\omega )^2)\\&\left( {x,y}\right) \mapsto \left( {x_0\times y_0,~\left( {\left( {x,y'}\right) ,\,\left( {x',y}\right) }\right) }\right) \end{aligned}$$

(Intuitively, the inner pairs \(\left( {x,y'}\right) \) and \(\left( {x',y}\right) \) correspond to the corecursive calls, and thus to the shuffle products \(x\otimes y'\) and \(x'\otimes y\); in contrast, the intermediate pair \(\left( {\left( {x,y'}\right) ,\,\left( {x',y}\right) }\right) \) corresponds to a call to the causal algebra on S, i.e., in this case, pointwise addition.) In the very same way, with the functor \(BX=2\times X^A\) for deterministic automata, we immediately obtain the semantics of non-deterministic automata and context-free grammars using simple causal algebras on formal languages (Examples 7.1 and 7.2).

Second, we obtain that algebras on the final coalgebra are causal if and only if they can be defined by a distributive law. Similar results were known to hold for streams [12] and languages [35]. Our characterisation is more abstract and less syntactic; the precise relationship between those results remains to be studied.

Third, we can combine our results with some recent work [6] where we rely on (bi)fibrations to lift distributive laws on systems (e.g., automata, LTSs) to obtain up-to techniques for coinductive predicates or relations on those systems (e.g., language equivalence, bisimilarity, divergence). Doing so, we obtain that every causal algebra gives rise to a valid up-to context technique (Sect. 7.3). For instance, bisimulation up to pointwise additions and shuffle products is a valid technique for proving stream equalities coinductively.

We conclude with an expressivity result (Sect. 8): while abstract GSOS specifications [43] seem more expressive than plain distributive laws, we show that this is actually not the case: any algebra obtained from an abstract GSOS specification can actually be defined from a plain distributive law.

2 Preliminaries

A coalgebra for a functor \(B :\mathcal {C}\rightarrow \mathcal {C}\) is a pair (Xf) where X is an object in \(\mathcal {C}\) and \(f:X \rightarrow BX\) a morphism. A coalgebra homomorphism from (Xf) to (Yg) is a \(\mathcal {C}\)-morphism \(h :X \rightarrow Y\) such that \(g \circ h = Fh \circ f\). A coalgebra \((Z,\zeta )\) is called final if it is final in the category of coalgebras , i.e., for every coalgebra (Xf) there exists a unique coalgebra morphism from (Xf) to \((Z,\zeta )\).

An algebra for a functor \(F :\mathcal {D}\rightarrow \mathcal {D}\) is defined dually to a coalgebra, i.e., it is a pair (Xa) where \(a :FX \rightarrow X\), and an algebra morphism from (Xa) to (Yb) is a morphism \(h :X \rightarrow Y\) such that \(h \circ a = b \circ Fh\).

A monad is a triple \((T, \eta , \mu )\) where \(T :\mathcal {C}\rightarrow \mathcal {C}\) is a functor, and \(\eta :\mathsf {Id}\Rightarrow T\) and \(\mu :TT \Rightarrow T\) are natural transformations called unit and multiplication respectively, such that \(\mu \circ T \eta = \mathsf {id}= \mu \circ \eta T\) and \(\mu \circ \mu T = \mu \circ T \mu \).

Distributive Laws. A distributive law of a functor \(F :\mathcal {C}\rightarrow \mathcal {C}\) over a functor \(B :\mathcal {C}\rightarrow \mathcal {C}\) is a natural transformation \(\lambda :FB \Rightarrow BF\). If B has a final coalgebra \((Z,\zeta )\), then such a \(\lambda \) induces a unique algebra \(\alpha \) making the following commute.

We call \(\alpha \) the algebra induced by \(\lambda \) (on the final coalgebra).

Let \((T,\eta ,\mu )\) be a monad. A distributive law of \((T,\eta ,\mu )\) over B is a natural transformation \(\lambda :TB \Rightarrow BT\) such that \(B\eta = \lambda \circ \eta B\) and \(\lambda \circ \mu B = B\mu \circ \lambda T \circ T\lambda \).

Final Sequence. Let \(B :\mathcal {C}\rightarrow \mathcal {C}\) be an endofunctor on a complete category \(\mathcal {C}\). The final sequence is the unique ordinal-indexed sequence defined by \(B_0 = 1\) (the final object of \(\mathcal {C}\)), \(B_{i+1} = BB_i\) and \(B_j = \lim _{i < j} B_i\) for a limit ordinal j, with connecting morphisms \(B_{j,i} :B_j \rightarrow B_i\) for all \(i \le j\), satisfying \(B_{i,i} = \mathsf {id}\), \(B_{j+1,i+1} = BB_{j,i}\) and if j is a limit ordinal then \((B_{j,i})_{i < j}\) is a limit cone.

The final sequence is a standard tool for constructing final coalgebras: if there exists an ordinal k such that \(B_{k+1,k}\) is an isomorphism, then \(B_{k+1,k}^{-1} :B_k \rightarrow BB_k\) is a final B-coalgebra [4, Theorem 1.3] (and dually for initial algebras [3]). In the sequel, we shall sometimes present it as a functor \(\bar{B} :\mathsf {Ord}^\mathsf {op}\rightarrow \mathcal {C}\), given by \(\bar{B}(i) = B_i\) and \(\bar{B}(j,i) = B_{j,i}\).

Example 2.1

Consider the functor \(B :\mathsf {Set}\rightarrow \mathsf {Set}\) given by \(BX = A \times X\), whose coalgebras are stream systems. Then \(B_0 = 1\) and \(B_{i+1} = A \times B_i\) for \(0< i < \omega \). Hence, for \(i < \omega \), \(B_i\) is the set of all finite lists over A of length i. The limit \(B_\omega \) consists of the set of all streams over A. For each ij with \(i \le j\), the connecting map \(B_{j,i}\) maps a stream (if \(j=\omega \)) or a list (if \(j<\omega \)) to the prefix of length i. The set \(B_\omega \) of streams is a final B-coalgebra.

Example 2.2

For the \(\mathsf {Set}\) functor \(BX = 2 \times X^A\) whose coalgebras are deterministic automata over A, \(B_i\) is (isomorphic to) the set of languages of words over A with length below i. In particular, \(B_\omega = \mathcal {P}(A^*)\) is the set of all languages, and it is a final B-coalgebra.

A functor \(B :\mathcal {C}\rightarrow \mathcal {C}\) is called (\(\omega \))-continuous if it preserves limits of \(\omega ^{\mathsf {op}}\)-chains. For such a functor, \(B_\omega \) is the carrier of a final B-coalgebra. The functors of stream systems and automata in the above examples are both \(\omega \)-continuous.

3 Properties of the Companion

Definition 3.1

Let \(B :\mathcal {C}\rightarrow \mathcal {C}\) be a functor. The category \(\mathsf {DL}(B)\) of distributive laws is defined as follows. An object is a pair \((F, \lambda )\) where \(F :\mathcal {C}\rightarrow \mathcal {C}\) is a functor and \(\lambda :FB \Rightarrow BF\) is a natural transformation. A morphism from \((F, \lambda )\) to \((G,\rho )\) is a natural transformation \(\kappa :F \Rightarrow G\) s.t. \(\rho \circ \kappa B = B\kappa \circ \lambda \). The companion of B is the final object of \(\mathsf {DL}(B)\), if it exists.

figure a

Morphisms in \(\mathsf {DL}(B)\) are a special case of morphisms of distributive laws, see [18, 22, 34, 44]. In the remainder of this section, we assume that the companion of B exists, and we denote it by \((T,\tau )\). We first prove that it is a monad.

Theorem 3.1

There are unique \(\eta :\mathsf {Id}\Rightarrow T\) and \(\mu :TT \Rightarrow T\) such that \((T,\eta ,\mu )\) is a monad and \(\tau :TB \Rightarrow BT\) is a distributive law of this monad over B.

Proof

Define \(\eta \) and \(\mu \) as the unique morphisms from \(\mathsf {id}_B\) and \(\tau T \circ T\tau \) respectively to the companion:

By definition, they satisfy the required axioms for \(\tau \) to be a distributive law of monad over functor. The proof that \((T,\eta ,\mu )\) is indeed a monad is routine, using finality of \((T,\tau )\), see the appendix [32].    \(\square \)

A distributive law \(\lambda \) of a monad over a functor allows one to strengthen the coinduction principle obtained by finality, as observed in [5] (specifically its Corollary 4.3.6), where it is called \(\lambda \)-coiteration. This principle allows one to solve (co)recursive equations, see, e.g., loc. cit. and [14, 25]. Since the companion is a distributive law of a monad (Theorem 3.1) we obtain the following.

Corollary 3.1

Let \((Z,\zeta )\) be a final B-coalgebra. For every morphism \(f :X \rightarrow BTX\) there is a unique morphism \(f^\dagger :X \rightarrow Z\) such that the following commutes:

where \(\alpha \) is the algebra induced by the distributive law \(\tau \) of the companion.

Instantiated to the complete lattice case, this is a soundness result: any invariant up to the companion (a post-fixpoint of \(b\circ t\)) is below the greatest fixpoint (\(\nu b\)).

Now assume that \(\mathcal {C}\) has an initial object 0. One can define the final coalgebra and the algebra induced by the companion explicitly:

Theorem 3.2

The B-coalgebra \((T0,\tau _0\circ T!_{B0})\) is final, and the algebra induced on it by the companion is given by \(\mu _0\).

More generally, the algebra induced by any distributive law factors through the algebra \(\mu _0\) induced by the companion.

Proposition 3.1

Let \((T,\eta ,\mu )\) be the monad on the companion (Theorem 3.1). Let \(\lambda :FB \Rightarrow BF\) be a distributive law, and \(\alpha :FT0 \Rightarrow T0\) the algebra on the final coalgebra induced by it. Let \(\bar{\lambda } :F \Rightarrow T\) be the unique natural transformation induced by finality of the companion. Then \(\alpha = \mu _0 \circ \bar{\lambda }_{T0}\).

4 The Codensity Monad

The notion of codensity monad is a special instance of a right Kan extension, which plays a central role in the following sections. We briefly define them here; see [20, 21, 28] for a comprehensive study.

Given \(F :\mathcal {C}\rightarrow \mathcal {D}\), \(G :\mathcal {C}\rightarrow \mathcal {E}\) be two functors. Define the category \(\mathcal {K}(F,G)\) whose objects are pairs \((H, \alpha )\) of a functor \(H :\mathcal {D}\rightarrow \mathcal {E}\) and a natural transformation \(\alpha :HF \Rightarrow G\). A morphism from \((H,\alpha )\) to \((I,\beta )\) is a natural transformation \(\kappa :H \Rightarrow I\) such that \(\beta \circ \kappa F = \alpha \).

The right Kan extension of G along F is a final object \((\mathsf {Ran}_{F}G,\epsilon )\) in \(\mathcal {K}(F,G)\); the natural transformation \(\epsilon :(\mathsf {Ran}_{F}G)F\Rightarrow G\) is called its counit. A functor \(K :\mathcal {E}\rightarrow \mathcal {F}\) is said to preserve \(\mathsf {Ran}_{F}G\) if \(K \circ \mathsf {Ran}_{F}G\) is a right Kan extension of KG along F, with counit \(K\epsilon :K(\mathsf {Ran}_{F}G)F \Rightarrow KG\).

The codensity monad is a special case, with \(F=G\). Explicitly, the codensity monad of a functor \(F :\mathcal {C}\rightarrow \mathcal {D}\) consists of a functor \(\mathsf {C}_{F} :\mathcal {D}\rightarrow \mathcal {D}\) and a natural transformation \(\epsilon :\mathsf {C}_{F}F\Rightarrow F\) s.t. for every functor \(H :\mathcal {D}\rightarrow \mathcal {D}\) and natural transformation \(\alpha :HF \Rightarrow F\) there is a unique \(\hat{\alpha } :H \Rightarrow \mathsf {C}_{F}\) s.t. \(\epsilon \circ \hat{\alpha } F = \alpha \).

As the name suggests, \(\mathsf {C}_{F}\) is a monad: the unit \(\eta \) and the multiplication \(\mu \) are the unique natural transformations such that \(\epsilon \circ \eta F = \mathsf {id}\) and \(\epsilon \circ \mu F = \epsilon \circ \mathsf {C}_{F} \epsilon \). In the sequel we will abbreviate the category \(\mathcal {K}(F,F)\) as \(\mathcal {K}(F)\).

Right Kan extensions can be computed pointwise as a limit, if sufficient limits exist. For an object X in \(\mathcal {D}\), denote by \(\varDelta _X :\mathcal {C}\rightarrow \mathcal {D}\) the functor that maps every object to X. By \(\varDelta _X / F\) we denote the comma category, where an object is a pair (Yf) consisting of an object Y in \(\mathcal {C}\) and an arrow \(f :X \rightarrow FY\) in \(\mathcal {D}\), and an arrow from (Yf) to (Zg) is a map \(h :Y \rightarrow Z\) in \(\mathcal {C}\) such that \(Fh \circ f = g\). There is a forgetful functor \((\varDelta _X / F) \rightarrow \mathcal {C}\), which remains unnamed below.

Lemma 4.1

Let \(F :\mathcal {C}\rightarrow \mathcal {D}\), \(G :\mathcal {C}\rightarrow \mathcal {E}\) be functors. If, for every object X in \(\mathcal {D}\), the limit \( \lim \left( (\varDelta _X / F) \rightarrow \mathcal {C}\xrightarrow {G} \mathcal {D}\right) \) exists, then the right Kan extension \(\mathsf {Ran}_{F}G\) exists, and is given on an object X by that limit.

The codensity monad of a functor F is the right Kan extension of F along itself. Hence, Lemma 4.1 gives us a way of computing the codensity monad. The hypotheses are met in particular if \(\mathcal {C}\) is essentially small (equivalent to a category with a set of objects and a set of arrows) and \(\mathcal {D}\) is locally small and complete. The latter conditions hold for \(\mathcal {D}=\mathsf {Set}\). In that case, we have the following concrete presentation; see, e.g., [8, Sect. 2.5] for a proof.

Lemma 4.2

Let \(F :\mathcal {C}\rightarrow \mathsf {Set}\) be a functor, where \(\mathcal {C}\) is essentially small. The codensity monad \(\mathsf {C}_{F}\) is given by \(\mathsf {C}_{F}(X) = \{\alpha :(F-)^X \Rightarrow F\}\) and, for \(h :X \rightarrow Y\), \((\mathsf {C}_{F}(h)(\alpha ))_A :(FA)^Y \rightarrow FA\) is given by \(f\mapsto \alpha _A(f \circ h)\). The natural transformation \(\epsilon :\mathsf {C}_{F} F \Rightarrow F\) is given by \(\epsilon _X(\alpha :F^{FX} \Rightarrow F) = \alpha _X(\mathsf {id}_{FX})\).

5 Constructing the Companion by Codensity

It is standard in the theory of coalgebras to compute the final coalgebra of a functor B as a limit of the final sequence \(\bar{B}\), see Sect. 2. In this section, we focus on the codensity monad of the final sequence, and show that it yields—under certain conditions—the companion of B.

The codensity monad of \(\bar{B}\) is final in the category of natural transformations of the form \(F\bar{B} \Rightarrow \bar{B}\) (see Sect. 4), whereas the companion of B is final in the category of distributive laws over B. The following lemma is a first step towards connecting companion and codensity monad.

Lemma 5.1

For every \(\lambda :FB \Rightarrow BF\) there exists a unique \(\alpha :F\bar{B} \Rightarrow \bar{B}\) such that for all \(i \in \mathsf {Ord}\): \(\alpha _{i+1} = B\alpha _i \circ \lambda _{B_i}\). Moreover, if \(B_{k+1,k}\) is an isomorphism for some k, then \(\alpha _k\) is the algebra induced by \(\lambda \) on the final coalgebra.

We turn to the main result of this section: the codensity monad of \(\bar{B}\) yields the companion of B, if B preserves this codensity monad. The latter condition, as well as the concrete form of the companion computed in this manner, becomes clearer when we instantiate this result to the case where \(\mathcal {C}\) is a lattice (Sect. 5.1) and the case \(\mathcal {C}=\mathsf {Set}\) (Sect. 6).

Theorem 5.1

Let \(\bar{B} :\mathsf {Ord}^\mathsf {op}\rightarrow \mathcal {C}\) be the final sequence of an endofunctor B. If the codensity monad \(\mathsf {C}_{\bar{B}}\) exists and B preserves it (as a right Kan extension) then there is a distributive law \(\tau \) of the codensity monad \((\mathsf {C}_{\bar{B}},\eta ,\mu )\) over B such that \((\mathsf {C}_{\bar{B}},\tau )\) is the companion of B.

Proof

(Outline). The preservation assumption means that \((B\mathsf {C}_{\bar{B}}, B\epsilon )\) is a right Kan extension of \(B\bar{B}\) along \(\bar{B}\). The natural transformation \(\tau \) is defined, using the universal property of \(B\epsilon \), as the unique \(\tau :\mathsf {C}_{\bar{B}} B \Rightarrow B\mathsf {C}_{\bar{B}}\) such that \(B\epsilon _i \circ \tau _{B_i} = \epsilon _{i+1} :\mathsf {C}_{\bar{B}} B B_i \Rightarrow BB_i\) for all i. See the appendix for a full proof [33].    \(\square \)

The following result characterises the algebra induced on the final coalgebra by the distributive law of the companion, in terms of the counit \(\epsilon \) of the codensity monad of \(\bar{B}\). This plays an important role for the case \(\mathcal {C}=\mathsf {Set}\) (Sect. 7).

Proposition 5.1

Suppose B is a functor satisfying the hypotheses of Theorem 5.1. Let \((\mathsf {C}_{\bar{B}},\epsilon )\) be the codensity monad of \(\bar{B}\), with distributive law \(\tau \) and monad structure \((\mathsf {C}_{\bar{B}},\eta ,\mu )\). If \(B_{k+1,k}\) is an isomorphism for some k, then

  1. 1.

    \(\epsilon _k :\mathsf {C}_{\bar{B}} B_k \rightarrow B_k\) is the algebra induced by \(\tau \) on the final coalgebra;

  2. 2.

    if \(\mathcal {C}\) has an initial object 0 then \(\epsilon _k\) is isomorphic to \(\mu _0\).

5.1 Codensity and the Companion of a Monotone Function

Throughout this section, let \(b :L \rightarrow L\) be a monotone function on a complete lattice. By Theorem 5.1, the companion of a monotone function b (viewed as a functor on a poset category) is given by the right Kan extension of the final sequence \(\bar{b} :\mathsf {Ord}^{\mathsf {op}} \rightarrow L\) along itself. Using Lemma 4.1, we obtain the characterisation of the companion given in the Introduction (5).

Theorem 5.2

The companion t of b is given by

$$\begin{aligned} t: x\mapsto \bigwedge _{x \le b_i} b_i \end{aligned}$$

Proof

By Lemma 4.1, the codensity monad \(\mathsf {C}_{\bar{b}}\) can be computed by

$$\begin{aligned} \mathsf {C}_{\bar{b}}(x) = (\mathsf {Ran}_{\bar{b}}\bar{b})(x) = \bigwedge _{x \le b_i} b_i\,, \end{aligned}$$

a limit that exists since L is a complete lattice. We apply Theorem 5.1 to show that \(\mathsf {C}_{\bar{b}}\) is the companion of b. The preservation condition of the theorem amounts to the equality \(b \circ \mathsf {Ran}_{\bar{b}}\bar{b} = \mathsf {Ran}_{\bar{b}}(b \circ \bar{b})\) which, by Lemma 4.1, in turn amounts to

$$\begin{aligned} b(\bigwedge _{x \le b_i} b_i) = \bigwedge _{x \le b_i} b(b_i) \end{aligned}$$

for all \(x \in L\). The sequence \((b_i)_{i\in \mathsf {Ord}}\) is decreasing and stagnates at some ordinal \(\epsilon \); therefore, the two intersections collapse into their last terms, say \(b_\delta \) and \(b(b_\delta )\) (with \(\delta \) the greatest ordinal such that \(x\not \le b_{\delta +1}\), or \(\epsilon \) if such an ordinal does not exist). The equality follows.    \(\square \)

In fact, the category \(\mathcal {K}(b)\) defined in Sect. 4 instantiates to the following: an object is a monotone function \(f :L \rightarrow L\) such that \(f(b_i) \le b_i\) for all \(i \in \mathsf {Ord}\), and an arrow from f to g exists iff \(f \le g\). The companion t is final in this category. This yields the following characterisation of functions below the companion.

Proposition 5.2

Let t be the companion of b. For any monotone function f we have \(f \le t\) iff \(\forall i \in \mathsf {Ord}: f(b_i) \le b_i\).

A key intuition about up-to techniques is that they should at least preserve the greatest fixpoint (i.e., up-to context is valid only when bisimilarity is a congruence). It is however well-known that this is not a sufficient condition [38, 39]. The above proposition gives a stronger and better intuition: a technique should preserve all approximations of the greatest fixpoint (the elements of the final sequence) to be below the companion, and thus sound.

This intuition on complete lattices leads us to the abstract notion of causality we introduce in the following section.

6 Causality by Codensity

We focus on the codensity monad of the final sequence of an \(\omega \)-continuous \(\mathsf {Set}\) endofunctor B. For such a functor, \(B_\omega \) is the carrier of a final coalgebra and Lemma 4.2 provides us with a description of the codensity monad in terms of natural transformations of the form \((\bar{B}-)^X \Rightarrow \bar{B}\). We show that such natural transformations correspond to a new abstract notion which we call causal algebras. Based on this correspondence and Theorem 5.1, we will get a concrete understanding of the companion of B in Sect. 7.

Definition 6.1

Let \(B,F:\mathsf {Set}\rightarrow \mathsf {Set}\) be functors. An algebra \(\alpha :FB_\omega \rightarrow B_\omega \) is called (\(\omega \))-causal if for every set X, functions \(f,g :X \rightarrow B_\omega \) and \(i < \omega \):

Causal algebras form a category \(\mathsf {causal}(B)\): an object is a pair \((F,\alpha :FB_\omega \rightarrow B_\omega )\) where \(\alpha \) is causal, and a morphism from \((F,\alpha )\) to \((G,\beta )\) is a natural transformation \(\kappa :F \Rightarrow G\) such that \(\beta \circ \kappa _{B_\omega } = \alpha \).

An (\(\omega \))-causal function on |V| arguments is a causal algebra for the functor \((-)^V\). Equivalently, \(\alpha :(B_\omega )^V \rightarrow B_\omega \) is causal iff for every \(h,k \in (B_\omega )^V\) and every \(i < \omega \): if \(B_{\omega ,i} \circ h\) = \(B_{\omega ,i} \circ k\) then \(B_{\omega ,i} \circ \alpha (h) = B_{\omega ,i} \circ \alpha (k)\).

Example 6.1

Recall from Example 2.1 that, for the functor \(BX = A\times X\), \(B_i\) is the set of lists of length i, and in particular \(B_\omega \) is the set of streams over A. We focus first on causal functions. To this end, for \(\sigma ,\tau \in B_\omega \), we write \(\sigma \equiv _i \tau \) if \(\sigma \) and \(\tau \) are equal up to i, i.e., \(\sigma (k) = \tau (k)\) for all \(k < i\). It is easy to verify that a function of the form \(\alpha :(B_\omega )^n \rightarrow B_\omega \) is causal iff for all \(\sigma _1, \ldots , \sigma _n, \tau _1, \ldots , \tau _n\) and all \(i < \omega \): if \(\sigma _j \equiv _i \tau _j\) for all \(j \le n\) then \(\alpha (\sigma _1, \ldots , \sigma _n) \equiv _i \alpha (\tau _1,\ldots ,\tau _n)\).

For instance, taking \(n=2\), \(\mathsf {alt}(\sigma ,\tau ) = (\sigma (0),\tau (1),\sigma (2),\tau (3), \ldots )\) is causal, whereas \(\mathsf {even}(\sigma ) = (\sigma (0), \sigma (2), \ldots )\) (with \(n=1\)) is not causal. For \(A=\mathbb {R}\), standard operations from the stream calculus such as pointwise stream addition, shuffle product and shuffle product are all causal.

The above notion of causal functions (with a finite set of arguments V) agrees with the standard notion of causal stream functions (e.g., [12]). Our notion of causal algebras generalises it from single functions to algebras for arbitrary functors. This includes polynomial functors modelling a signature. For \(A = \mathbb {R}\), the algebra \(\alpha :\mathcal {P}_\omega (B_\omega ) \rightarrow B_\omega \) for the finite powerset functor \(\mathcal {P}_\omega \), defined by \(\alpha (S)(n) = \min \{\sigma (n) \mid \sigma \in S\} \) is a causal algebra which is not a causal function. The algebra \(\beta :\mathcal {P}_\omega (B_\omega ) \rightarrow B_\omega \) given by \(\beta (S)(n) = \sum _{\sigma \in S} \sigma (n)\) is not causal according to Definition 6.1. Intuitively, \(\beta (\{\sigma ,\tau \})(i)\) depends on equality of \(\sigma \) and \(\tau \), since addition of real numbers is not idempotent.

Example 6.2

For the functor \(BX = 2 \times X^A\), \(B_\omega = \mathcal {P}(A^*)\) is the set of languages over A (Example 2.2). Given languages L and K, we write \(L \equiv _i K\) if L and K contain the same words of length below i. A function \(\alpha :(\mathcal {P}(A^*))^n \rightarrow \mathcal {P}(A^*)\) is causal iff for all languages \(L_1, \ldots , L_n, K_1, \ldots , K_n\): if \(L_j \equiv _i K_j\) for all \(j \le n\) then \(\alpha (L_1, \ldots , L_n) \equiv _i \alpha (K_1,\ldots ,K_n)\). For instance, union, concatenation, Kleene star, and shuffle of languages are all causal. An example of a causal algebra that is not a causal function is \(\alpha :\mathcal {P}(\mathcal {P}(A^*)) \rightarrow \mathcal {P}(A^*)\) defined by union.

The following result connects causal algebras to natural transformations of the form \(F \bar{B} \Rightarrow \bar{B}\) (which, from Sect. 4, form a category \(\mathcal {K}(\bar{B})\)).

Theorem 6.1

Let \(B,F :\mathsf {Set}\rightarrow \mathsf {Set}\) be functors, and suppose B is \(\omega \)-continuous. The category \(\mathsf {causal}(B)\) of causal algebras is isomorphic to the category \(\mathcal {K}(\bar{B})\). Concretely, there is a one-to-one correspondence

$$\begin{aligned} \frac{\alpha :F \bar{B} \Rightarrow \bar{B}}{\alpha _\omega :FB_\omega \rightarrow B_\omega \,\, \text {causal}} \end{aligned}$$

From top to bottom, this is given by evaluation at \(\omega \). Moreover, we have \(\beta \circ \kappa {\bar{B}} = \alpha \) iff \(\beta _\omega \circ \kappa _{B_\omega } = \alpha _\omega \) for any \(\alpha :F \bar{B} \Rightarrow \bar{B}\), \(\beta :G \bar{B} \Rightarrow \bar{B}\) and \(\kappa :F \Rightarrow G\).

By the above theorem, the universal property of the codensity monad amounts to the following property of causal algebras.

Corollary 6.1

Suppose \(B :\mathsf {Set}\rightarrow \mathsf {Set}\) is \(\omega \)-continuous. Let \(\epsilon \) be the counit of \(\mathsf {C}_{\bar{B}}\). Then \(\epsilon _\omega \) is final in \(\mathsf {causal}(B)\), i.e., for every causal algebra \(\alpha :FB_\omega \rightarrow B_\omega \), there is a unique natural transformation \(\hat{\alpha } :F \Rightarrow \mathsf {C}_{\bar{B}}\) such that \(\epsilon _\omega \circ \hat{\alpha }_{B_\omega } = \alpha \).

By Lemmas 4.2 and 6.1, we obtain the following concrete description of the codensity monad \(\mathsf {C}_{\bar{B}}\) of the final sequence of a \(\mathsf {Set}\) endofunctor B, as a functor of causal functions.

Theorem 6.2

Let \(B :\mathsf {Set}\rightarrow \mathsf {Set}\) be an \(\omega \)-continuous functor. The codensity monad \(\mathsf {C}_{\bar{B}}\) of the final sequence of B is given by

$$\begin{aligned} \mathsf {C}_{\bar{B}}(X)&= \{\alpha :B_\omega ^X \rightarrow B_\omega \mid \alpha \,\, \text {is a causal function}\},\\ \mathsf {C}_{\bar{B}}(h :X \rightarrow Y)(\alpha )&= \lambda f. \,\alpha (f \circ h), \end{aligned}$$

and, for the counit \(\epsilon :\mathsf {C}_{\bar{B}}\bar{B} \Rightarrow \bar{B}\), we have \(\epsilon _\omega (\alpha :B_\omega ^{B\omega } \rightarrow B_\omega ) = \alpha (\mathsf {id}_{B_\omega })\).

Hence, the codensity monad of the final sequence of the functor \(X \mapsto A \times X\) of stream systems maps a set X to the set of all causal stream functions with |X| arguments. Similarly for the functor \(X \mapsto 2 \times X^A\): we obtain a functor of causal functions on languages.

7 Companion of a \(\mathsf {Set}\) Functor

The previous sections gives us a concrete understanding of the codensity monad of the final sequence of a \(\mathsf {Set}\) functor in terms of causal functions, and Theorem 5.1 provides us with a sufficient condition for this codensity monad to be the companion. We now focus on several applications of these results.

A rather general class of functors that satisfy the hypotheses of Theorem 5.1 is given by the polynomial functors. Automata, stream systems, Mealy and Moore machines, various kinds of trees, and many more are all examples of coalgebras for polynomial functors (e.g., [15]). A functor \(B :\mathsf {Set}\rightarrow \mathsf {Set}\) is called polynomial (in a single variable) if it is isomorphic to a functor of the form

$$\begin{aligned} X \mapsto \coprod _{a \in A} X^{B_a} \end{aligned}$$

for some A-indexed collection \((B_a)_{a \in A}\) of sets. As explained in [11, 1.18], a \(\mathsf {Set}\) functor B is polynomial if and only if it preserves connected limits. This implies existence and preservation by B of the codensity monad of \(\bar{B}\), as required by Theorem 5.1 (see the appendix for details [32]).

Lemma 7.1

If \(B :\mathsf {Set}\rightarrow \mathsf {Set}\) is polynomial, then it satisfies the hypotheses of Theorem 5.1.

As a consequence, if B is polynomial, the functor of causal functions in Theorem 6.2 is the companion of B.

7.1 Solving Equations via Causal Algebras

As explained in the introduction, a distributive law of F over B allows one to solve systems of equations, formalised in terms of BF-coalgebras, leading to an expressive coinductive definition technique. This approach is formally supported by a solution theorem, stated for the companion in Corollary 3.1. Based on the characterisation of the companion in terms of causal algebras, we obtain a new, simplified solution theorem: it does not mention distributive laws at all, but is stated purely in terms of causal algebras.

Theorem 7.1

Let \(B :\mathsf {Set}\rightarrow \mathsf {Set}\) be a polynomial functor, with final coalgebra \((B_\omega ,\zeta )\). Let \(\alpha :FB_\omega \rightarrow B_\omega \) be a causal algebra. For every \(f :X \rightarrow BFX\) there is a unique \(f^\dagger :X \rightarrow Z\) such that the following diagram commutes.

Example 7.1

For the functor \(BX = A \times X\), \(B_\omega \) is the set of streams. Take \(SX=X^2\) for F, and consider the coalgebra \(f :1 \rightarrow BS1\) with \(1=\{*\}\), defined by \(* \mapsto (1,(*,*))\). Pointwise addition is a causal function on streams, modelled by an algebra on \(B_\omega \) for the functor S. By Theorem 7.1 we obtain a unique solution \(\sigma \in B_\omega \), satisfying \(\sigma _0 = 1\) and \(\sigma ' = \sigma \oplus \sigma \). Similarly, the shuffle product of streams is causal, so that by applying Theorem 7.1 with that algebra and the same coalgebra f we obtain a unique stream \(\sigma \) satisfying \(\sigma _0 = 1\), \(\sigma ' = \sigma \otimes \sigma \).

As explained in the Introduction, this method also allows one to define functions on streams. For instance, for the shuffle product, define a BS-coalgebra \(f :(B_\omega )^2 \rightarrow BS(B_\omega )^2\), by \(f(\sigma ,\tau ) = (\sigma _0 \times \tau _0, ((\sigma ',\tau ),(\tau ,\sigma '))\). Since addition of streams is causal, by Theorem 7.1 there is a unique \(f^\dagger :B_\omega \times B_\omega \rightarrow B_\omega \) such that \(f^\dagger (\sigma ,\tau )(0) = \sigma (0) \times \tau (0)\) and \((f^\dagger (\sigma ,\tau ))' = (f^\dagger (\sigma ', \tau ) \oplus f^\dagger (\sigma ,\tau '))\), matching the definition given in the Introduction (2). Notice that not every function defined in this way is causal; for instance, it is easy to define \(\mathsf {even}\) (see Example 6.1), even with the standard coinduction principle (i.e., where \(F = \mathsf {Id}\) and \(\alpha =\mathsf {id}\)).

Example 7.2

Consider the functor \(BX = 2 \times X^A\), whose final coalgebra consists of the set \(\mathcal {P}(A^*)\) of languages. A \(B\mathcal {P}\)-coalgebra \(f :X \rightarrow 2 \times (\mathcal {P}(X))^A\) is a non-deterministic automaton. Taking the causal algebra \(\alpha :\mathcal {P}(\mathcal {P}(A^*)) \rightarrow \mathcal {P}(A^*)\) defined by union, the unique map \(f^\dagger :X \rightarrow \mathcal {P}(A^*)\) from Theorem 7.1 is the usual language semantics of non-deterministic automata.

In [45], a context-free grammar (in Greibach normal form) is modelled as a \(B\mathcal {P}^*\)-coalgebra \(f :X \rightarrow 2 \times (\mathcal {P}(X)^*)^A\), and its semantics is defined operationally by turning f into a deterministic automaton over \(\mathcal {P}(X^*)\). In [36] this operational view is related to the semantics of CFGs in terms of language equations. Consider the causal algebra \(\alpha :\mathcal {P}(\mathcal {P}(A^*)^*) \rightarrow \mathcal {P}(A^*)\) defined by union and language composition: \(\alpha (S) = \bigcup _{L_1 \ldots L_k \in S} L_1 L_2 \ldots L_k\). By Theorem 7.1, any context-free grammar f has a unique solution in languages, which is the semantics of CFGs in the usual sense. As such, we obtain an elementary coalgebraic semantics of CFGs that does not require us to relate it to an operational semantics.

7.2 Causal Algebras and Distributive Laws

Another application of the fact that the codensity monad is the companion is that the final causal algebra in Corollary 6.1 is, by Proposition 5.1, the algebra induced by a distributive law. Hence, any causal algebra is “definable” by a distributive law, in the sense that it factors as a (component of a) natural transformation followed by the algebra induced by a distributive law.

More precisely, suppose \(B :\mathsf {Set}\rightarrow \mathsf {Set}\) has a final coalgebra \((Z,\zeta )\). We say an algebra \(\alpha :FZ \rightarrow Z\) is definable by a distributive law over B if there exists a distributive law \(\lambda :GB \Rightarrow BG\) with induced algebra \(\beta :GZ \rightarrow Z\) and a natural transformation \(\kappa :F \Rightarrow G\) such that the following commutes:

Theorem 7.2

Let \(B :\mathsf {Set}\rightarrow \mathsf {Set}\) be polynomial. An algebra \(\alpha :FB_\omega \rightarrow B_\omega \) is causal if and only if it is definable by a distributive law over B.

Since the functors for stream systems and automata are polynomial, as a special case of Theorem 7.2 we obtain that a stream function, or a function on languages, is causal if and only if it is definable by a distributive law.

In [12], a similar result is shown concretely for causal stream functions, and this is extended to languages in [35]. In both cases, very specific presentations of distributive laws for the systems at hand are used to present the distributive law based on a “syntax”, which however is not too clearly distinguished from the semantics: it consists of a single operation symbol for every causal function. In our case, in the proof of Theorem 7.2, we use the companion, which consists of the actual functions rather than a syntactic representation. Indeed, the setting of Theorem 7.2 applies more abstractly to all causal algebras, not just causal functions. However, it remains an intriguing question how to obtain a concrete syntactic characterisation of a distributive law for a given causal algebra.

7.3 Soundness of Up-to Techniques

The contextual closure of an algebra is one of the most powerful up-to techniques, which allows one to exploit algebraic structure in bisimulation proofs. In [7], it is shown that the contextual closure is sound (compatible) on any bialgebra for a distributive law. Here, we move away from distributive laws and give an elementary condition for soundness of the contextual closure on the final coalgebra: that the algebra under consideration is causal. In fact, we prove that this implies that the contextual closure lies below the companion, which not only gives soundness, but also allows to combine it with other up-to techniques.

Due to space limitations, we can not fully explain the relevant definitions, and refer to [7] for details. Bisimulations on a B-coalgebra (Xf) are the post-fixed points of a monotone function \(b_f :\mathsf {Rel}_X \rightarrow \mathsf {Rel}_X\) on the lattice \(\mathsf {Rel}_X\) of relations on X, defined by \(b_f(R) = f^* \circ \mathsf {Rel}(B)(R)\). Here \(\mathsf {Rel}(B)\) is the relation lifting of B, and \(f^*\) is inverse image along \(f\times f\), see, e.g., [15]. Contextual closure \(\mathsf {ctx}_\alpha :\mathsf {Rel}_X \rightarrow \mathsf {Rel}_X\) with respect to an algebra \(\alpha :FX \rightarrow X\) is defined dually by , where is direct image along \(\alpha \times \alpha \).

Theorem 7.3

Let \(B :\mathsf {Set}\rightarrow \mathsf {Set}\) be polynomial, and \((B_\omega , \zeta )\) a final B-coalgebra. Let \(t_\zeta \) be the companion of \(b_\zeta \). For any causal algebra \(\alpha :FB_\omega \rightarrow B_\omega \): \(\mathsf {ctx}_\alpha \le t_\zeta \).

This implies that one can safely use the contextual closure for any causal algebra, such as union, concatenation and Kleene star of languages, or product and sum of streams. Endrullis et al. [9] prove the soundness of causal contexts in combination with other up-to techniques, for equality of streams. The soundness of causal algebras for streams is a special case of Theorem 7.3, but the latter provides more: being below the companion, it is possible to compose it to other such functions to obtain combined up-to techniques in a modular fashion, cf. [31].

8 Abstract GSOS

To obtain expressive specification formats, Turi and Plotkin [43] use natural transformations of the form \(\lambda :F(B \times \mathsf {Id}) \Rightarrow BF^*\), where \(F^*\) is the free monad for F. These are the so-called abstract GSOS specifications. We conclude this article by showing that they are actually equally expressive as plain distributive laws of a functor F over B.

If B has a final coalgebra \((Z,\zeta )\), then any abstract GSOS specification \(\lambda :F(B \times \mathsf {Id}) \Rightarrow BF^*\) defines an algebra \(\alpha :FZ \rightarrow Z\) on it, which is the unique algebra making the following diagram commute.

Here \(\alpha ^*\) is the Eilenberg-Moore algebra for the free monad corresponding to \(\alpha \). Intuitively, this algebra gives the interpretation of the operations defined by \(\lambda \).

Like plain distributive laws (Lemma 5.1), abstract GSOS specifications induce natural transformations of the form \(F\bar{B} \Rightarrow \bar{B}\).

Lemma 8.1

For every \(\lambda :F(B \times \mathsf {Id}) \Rightarrow BF^*\) there is a unique \(\alpha :F\bar{B} \Rightarrow \bar{B}\) such that for all \(i \in \mathsf {Ord}\): \(\alpha _{i+1} = B\alpha _i^* \circ \lambda _{B_i} \circ F\langle \mathsf {id}, B_{i+1,i}\rangle \). Moreover, if \(B_{k+1,k}\) is an isomorphism for some k, then \(\alpha _k\) is the algebra induced by \(\lambda \) on the final coalgebra.

This places abstract GSOS specifications within the framework of the companion, constructed via the codensity monad of the final sequence \(\bar{B}\). Whenever that construction applies (e.g., for polynomial functors), any algebra defined by an abstract GSOS is thus already definable by a plain distributive law over B.

Theorem 8.1

Suppose \(B :\mathcal {C}\rightarrow \mathcal {C}\) satisfies the conditions of Theorem 5.1. Every algebra induced on the final coalgebra by an abstract GSOS specification \(\lambda :F(B \times \mathsf {Id}) \Rightarrow BF^*\) is definable by a distributive law over B (cf. Sect. 7.2).

In this sense, abstract GSOS is no more expressive than plain distributive laws. Note, however, that this does involve moving to a different (larger) syntax.

Remark 8.1

Every abstract GSOS specification \(\lambda :F(B \times \mathsf {Id}) \Rightarrow BF^*\) corresponds to a unique distributive law \(\lambda ^\dagger :F^*(B \times \mathsf {Id}) \Rightarrow (B \times \mathsf {Id})F^*\) of the free monad \(F^*\) over the (cofree) copointed functor \(B \times \mathsf {Id}\), see [23]. The algebra induced by \(\lambda \) decomposes as the algebra induced by \(\lambda ^\dagger \) and the canonical natural transformation \(F \Rightarrow F^*\). This implies that every algebra induced by an abstract GSOS is definable by a distributive law over the copointed functor \(B \times \mathsf {Id}\). Theorem 8.1 strengthens this to definability by a distributive law over B.