\section{Introduction}

There is a remarkable divide in the field of logic in Computer Science, between two distinct strands: one focusing on semantics and compositionality (``Structure''), the other on expressiveness and complexity (``Power''). 
It is remarkable because these two fundamental aspects of our field are studied using almost disjoint technical languages and methods, by almost disjoint research communities.
We believe that bridging this divide is a major issue in Computer Science, and may hold the key to fundamental advances in the field.

In this paper, we develop a novel approach to relating categorical semantics, which exemplifies the first strand, to finite model theory, which exemplifies the second. It builds on the ideas introduced in \cite{abramsky2017pebbling}, but goes much further, showing clearly that there is a strong and robust connection, which can serve as a basis for many further developments. These developments were first introduced in the conference version of this paper \cite{DBLP:conf/csl/AbramskyS18}. In this journal version, we have added more detailed proofs, and several additional sections demonstrating how game comonads generalize constructions in finite model theory. We have also introduced a new and more satisfactory unified account of back-and-forth equivalences, in terms of open pathwise embeddings, which capture a general notion of \emph{property-preserving bisimulation}.

\subsection*{Note on the presentation}
Since this work aims at connecting what are at present largely disjoint research areas and communities, we have attempted to provide enough background exposition in the paper to make it reasonably self-contained.
In the following section, we provide some background on model comparison games, and their correspondence with logical equivalences, to assist readers mainly coming from the categorical semantics side. For those whose background is mainly in finite model theory and descriptive complexity, we assume only some prior exposure to the basic notions of category, functor, and natural transformation.
Notions relating to comonads are presented in a self-contained way.

We thank the journal referees for their encouragement of, and indeed insistence on, our efforts in this regard.

\subsection*{Acknowledgements} 
The authors are grateful for the support of EPSRC grant EP/T00696X/1, and the 
Department of Computer Science, University of Oxford. They benefited from
very helpful comments from Dan Marsden.
They also acknowledge the valuable comments of the journal referees. In particular, they appreciate the detailed comments made by the first reviewer through several rounds of revisions, which led to a number of improvements in the presentation. 

\subsection*{The setting}
Relational structures and the homomorphisms between them play a fundamental r\^{o}le in finite model theory, constraint satisfaction and database theory. The existence of a homomorphism $A \rightarrow B$ is an equivalent formulation of constraint satisfaction, and also equivalent to the preservation of existential positive sentences \cite{chandra1977optimal}. This setting also generalizes what has become a central perspective in graph theory \cite{hell2004graphs}.
\subsection*{Model theory and deception}
In a sense, the purpose of model theory is ``deception''.  It allows us to see structures not ``as they really are'', \textit{i.e.}~ up to isomorphism, but only up to \emph{definable properties}, where definability is relative to a logical language $\mathcal{L}$. The key notion is \emph{logical equivalence} $\equiv^{\LL}$. Given structures $\mathscr{A}$, $\mathscr{B}$ over the same vocabulary:
\[ \mathscr{A} \equiv^{\LL} \mathscr{B} \;\; \stackrel{\Delta}{\IFF} \;\; \forall \varphi \in \mathcal{L}. \; \mathscr{A} \models \varphi \; \Longleftrightarrow \; \mathscr{B} \models \varphi . \]
If a class of structures $\mathcal{K}$ is definable in $\mathcal{L}$, then it must be saturated under $\equiv^{\LL}$. Moreover, for a wide class of cases of interest in finite model theory, the converse holds \cite{kolaitis1992infinitary}.

The idea of syntax-independent characterizations of logical equivalence is quite a classical one in model theory, exemplified by the Keisler-Shelah theorem \cite{shelah1971every}.
It acquires additional significance in finite model theory, where model comparison games such as Ehrenfeucht-Fra\"{i}ss\'{e}~ games, pebble games and bisimulation games play a central role \cite{Libkin2004}.

We offer a new perspective on these ideas. We shall study these games, not as external artefacts, but as semantic constructions in their own right.
Each model-theoretic comparison game encodes ``deception'' in terms of limited access to the structure. These limitations are indexed by a parameter which quantifies the resources which control this access. For Ehrenfeucht-Fra\"{i}ss\'{e}~ games and bisimulation games, this is the number of rounds; for pebble games, the number of pebbles.

\subsection*{Main Results}
We now give a conceptual overview of our main results. Technical details will be provided in the following sections. 

We shall consider three forms of model comparison game: Ehrenfeucht-Fra\"{i}ss\'{e}~ games, pebble games and bisimulation games \cite{Libkin2004}.
For each of these notions of game $\mathsf{G}$, and value of the resource parameter $k$, we shall define a corresponding \emph{comonad} $\mathbb{C}_{k}$ on the category of relational structures and homomorphisms  over some relational vocabulary. For each structure $\mathscr{A}$, $\mathbb{C}_{k} \mathscr{A}$ is another structure over the same vocabulary, which encodes the limited access to $\mathscr{A}$ afforded by playing the game on $\mathscr{A}$ with $k$ resources. There is always an associated homomorphism $\epsilon_{\As} : \mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{A}$ (the \emph{counit} of the comonad), so that $\mathbb{C}_{k} \mathscr{A}$ ``covers'' $\mathscr{A}$. Moreover, given a homomorphism $h : \mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{B}$, there is a \emph{Kleisli coextension} homomorphism $h^* : \mathbb{C}_{k} \mathscr{A} \rightarrow \mathbb{C}_{k} \mathscr{B}$. This allows us to form the \emph{coKleisli category} $\mathsf{Kl}(\mathbb{C}_{k})$ for the comonad. The objects are relational structures, while the morphisms from $\mathscr{A}$ to $\mathscr{B}$ in $\mathsf{Kl}(\mathbb{C}_{k})$ are exactly the homomorphisms of the form $\mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{B}$. Composition of these morphisms uses the Kleisli coextension. The connection between this construction and the corresponding form of game $\mathsf{G}$ is expressed  by the following result:
\begin{theorem}
The following are equivalent:
\begin{enumerate}
\item There is a coKleisli morphism $\mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{B}$
\item Duplicator has a winning strategy for the existential $\mathsf{G}$-game with $k$ resources, played from $\mathscr{A}$ to $\mathscr{B}$.
\end{enumerate}
\end{theorem}
The existential form of the game has only a ``forth'' aspect, without the ``back''. This means that Spoiler can only play in $\mathscr{A}$, while Duplicator only plays in $\mathscr{B}$. This corresponds to the asymmetric form of the coKleisli morphisms $\mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{B}$. Intuitively, Spoiler plays in $\mathbb{C}_{k} \mathscr{A}$, which gives them limited access to $\mathscr{A}$, while Duplicator plays in $\mathscr{B}$. The Kleisli coextension guarantees that Duplicator's strategies can always be lifted to $\mathbb{C}_{k} \mathscr{B}$; while we can always compose a strategy $\mathbb{C}_{k} \mathscr{A} \rightarrow \mathbb{C}_{k} \mathscr{B}$ with the counit on $\mathscr{B}$ to obtain a coKleisli morphism.

This asymmetric form may seem to limit the scope of this approach, but in fact this is not the case. For each of these comonads $\mathbb{C}_{k}$, we have the following equivalences:
\begin{itemize}
\item $\mathscr{A} \rightleftarrows_{k} \mathscr{B}$ iff there are coKleisli morphisms $\mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{B}$ and $\mathbb{C}_{k} \mathscr{B} \rightarrow \mathscr{A}$. Note that there need be no relationship between these morphisms.
\item $\mathscr{A} \cong_{\Kl(\Ck)} \mathscr{B}$ iff $\mathscr{A}$ and $\mathscr{B}$ are isomorphic in the coKleisli category $\mathsf{Kl}(\mathbb{C}_{k})$. This means that there are morphisms $\mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{B}$ and $\mathbb{C}_{k} \mathscr{B} \rightarrow \mathscr{A}$ which are inverses of each other in $\mathsf{Kl}(\mathbb{C}_{k})$.
\end{itemize}
Clearly, $\cong_{\Kl(\Ck)}$ strictly implies $\rightleftarrows_{k}$.
We can also define an intermediate ``back-and-forth'' equivalence $\leftrightarrow_{k}$, in terms of the existence of a suitable kind of span of coKleisli morphisms.

For each of our three types of game, there are corresponding  fragments $\mathcal{L}_{k}$ of first-order logic:
\begin{itemize}
\item For Ehrenfeucht-Fra\"{i}ss\'{e}~ games, $\mathcal{L}_{k}$ is the fragment of quantifier-rank $\leq k$.
\item For pebble games, $\mathcal{L}_{k}$ is the $k$-variable fragment.
\item For bisimulation games over relational vocabularies with symbols of arity at most 2, $\mathcal{L}_{k}$ is the modal fragment \cite{andreka1998modal} with modal depth $\leq k$.
\end{itemize}
In each case, we write $\exists\mathcal{L}_{k}$ for the existential positive fragment of $\mathcal{L}_{k}$, and $\mathcal{L}_{k}^{\#}$ for the extension of $\mathcal{L}_{k}$ with counting quantifiers \cite{Libkin2004}.

We can now state our first main result, in a suitably generic form.
\begin{theorem}
For finite structures $\mathscr{A}$ and $\mathscr{B}$:

\begin{tabular}{llcl}
(1) & $\mathscr{A} \equiv^{\ELk} \mathscr{B}$ & $\; \Longleftrightarrow \;$ & $\mathscr{A} \rightleftarrows_{k} \mathscr{B}$. \\
(2) & $\mathscr{A} \equiv^{\Lk} \mathscr{B}$ & $\; \Longleftrightarrow \;$ & $\mathscr{A} \leftrightarrow_{k} \mathscr{B}$. \\
(3) & $\mathscr{A} \equiv^{\Lck} \mathscr{B}$ & $\; \Longleftrightarrow \;$ & $\mathscr{A} \cong_{\Kl(\Ck)} \mathscr{B}$.
\end{tabular}
\end{theorem}
Note that this is really a family of three theorems, one for each type of game $\mathsf{G}$. Thus in each case, we capture the salient logical equivalences in syntax-free, categorical form.

We now turn to the significance of indexing by the resource parameter $k$. When $k \leq l$, we have a natural inclusion morphism $\mathbb{C}_{k} \mathscr{A} \rightarrow \mathbb{C}_{l} \mathscr{A}$, since playing with $k$ resources is a special case of playing with $l \geq k$ resources. This tells us that the smaller $k$ is, the easier it is to find a morphism $\mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{B}$. Intuitively, the more we restrict Spoiler's abilities to access the structure of $\mathscr{A}$, the easier it is for Duplicator to win the game.

The contrary analysis applies to morphisms $\mathscr{A} \rightarrow \mathbb{C}_{k} \mathscr{B}$. The smaller $k$ is, the \emph{harder} it is find such a morphism. Note, however, that if $\mathscr{A}$ is a finite structure of cardinality $k$, then $\mathscr{A} \rightleftarrows_{k} \mathbb{C}_{k} \mathscr{A}$. In this case, with $k$ resources we can access the whole of $\mathscr{A}$. What can we say when $k$ is strictly smaller than the cardinality of $A$?

It turns out that there is a beautiful connection between these indexed comonads and combinatorial invariants of structures. This is mediated by the notion of \emph{coalgebra}, another fundamental (and completely general) aspect of comonads. A coalgebra for a comonad $\mathbb{C}_{k}$ on a structure $\mathscr{A}$ is a morphism $\mathscr{A} \rightarrow \mathbb{C}_{k} \mathscr{A}$ satisfying certain properties. We define the \emph{coalgebra number} of a structure $\mathscr{A}$, with respect to the indexed family of comonads $\mathbb{C}_{k}$, to be the least $k$ such that there is a $\mathbb{C}_{k}$-coalgebra  on $\mathscr{A}$.

We now come to our second main result.
\begin{theorem}
\begin{enumerate}
\item For the Ehrenfeucht-Fra\"{i}ss\'{e}~ comonad, the coalgebra number of $\mathscr{A}$ corresponds precisely to the \emph{tree-depth} of $\mathscr{A}$ \cite{nevsetvril2006tree}.
\item For the pebbling comonad, the coalgebra number of $\mathscr{A}$ corresponds precisely to the \emph{tree-width} of $\mathscr{A}$.
\item For the modal comonad, the coalgebra number of $\mathscr{A}$ corresponds precisely to the \emph{modal unfolding depth} of $\mathscr{A}$. 
\end{enumerate}
\label{thm:introCoalgebra}
\end{theorem}
The main idea behind these results is that coalgebras on $\mathscr{A}$ are in bijective correspondence with decompositions of $\mathscr{A}$ of the appropriate form. We thus obtain categorical characterizations of these key combinatorial parameters.

These two results appeared in the first version of this paper \cite{DBLP:conf/csl/AbramskyS18}, and  unify logical resources with combinatorial resources. In this expanded version of the paper, we provide two new families of results that can be seen as further aspects of this unification. The first result utilizes a general fact about comonads to link the logical resource characterized by coKleisli morphisms $\mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{B}$ to the combinatorial resource characterized by the coalgebas $\mathscr{A} \rightarrow \mathbb{C}_{k}\mathscr{A}$. 
For each game comonad $\mathbb{C}_{k}$, we can define a preorder $\to_{k}^{\mathbb{C}}$ over the category of structures, where
$\mathscr{A} \to_{k}^{\mathbb{C}} \mathscr{B}$ iff for all structures $\mathscr{C}$ with combinatorial resource parameter $\leq k$,  
$\mathscr{C} \rightarrow \mathscr{A} \; \Rightarrow \; \mathscr{C} \rightarrow \mathscr{B}$.\footnote{We use the notation $\mathscr{C} \rightarrow \mathscr{A}$ to mean that there exists a morphism with domain $\mathscr{C}$ and codomain $\mathscr{A}$.}
This preorder, when instantiated to the case of Ehrenfeucht-{Fra\"{i}ss\'{e}~}games and tree-depth, is used extensively in \cite{Rossman2008}.
We demonstrate that this preorder is characterized by the coKleisli category of $\mathbb{C}_{k}$.
\begin{proposition}
$\mathscr{A} \to_{k}^{\mathbb{C}} \mathscr{B} \; \Longleftrightarrow \; \mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{B}$.
\end{proposition}


For the second result, we show how coalgebras can be interpreted syntactically. We show that a coalgebra $\mathscr{A} \rightarrow \mathbb{C}_{k}\mathscr{A}$ encodes a witness  to the fact that the canonical conjunctive query $Q^{\As}$ of $\mathscr{A}$ is equivalent to a sentence in the logical fragment corresponding to the comonad.
\begin{theorem}
  $\mathscr{A}$ has a $\mathbb{C}_{k}$-coalgebra iff $Q^{\As}$ can be rewritten into a formula $\varphi$, where $\varphi$ has logical resource at most $k$.
\end{theorem}
The formula $\varphi$ can be read out from the coalgebra map.

We also show how to rephrase the coalgebra results of Theorem~\ref{thm:introCoalgebra} in the language of adjunctions. 
\begin{theorem}
Each game comonad arises from an adjunction between the category of structures and a category of tree-ordered structures:
\begin{enumerate}
  \item For the Ehrenfeucht-{Fra\"{i}ss\'{e}~} comonad, the associated category is $k$-height tree-ordered structures.
  \item For the pebbling comonad, the associated category is $k$-pebble tree-ordered structures. 
  \item For the modal comonad, the associated category is $k$-height tree-ordered Kripke structures. 
\end{enumerate}
In each case, the left adjoint is the evident forgetful functor which forgets the order. The right adjoint is given by the functor part of the comonad.
\end{theorem}

We have also put the notion of $I$-morphism introduced in \cite{abramsky2017pebbling}, and used extensively in \cite{DBLP:conf/csl/AbramskyS18}, on a more systematic basis, in terms of \emph{relative comonads} \cite{altenkirch2010monads}.

Finally, an important contribution of this expanded version of the paper is to give a unified, general treatment of back-and-forth equivalences, which play a central role in finite model theory. To do this, we use a refined version of the well-known notion of open map bisimulation \cite{joyal1993bisimulation}, in which the maps in the span witnessing a bisimulation are required not only to be open, but to be \emph{pathwise embeddings}.
This supports a general notion of \emph{property-preserving bisimulation}, which specialises to yield all the back-and-forth equivalences we study here, as well as many other examples, e.g.~\emph{guarded bisimulation} \cite{abramsky2017pebbling}.

\section{Background}

We shall now review some background on logics, model comparison games, and comonads.

\subsection{Notational preliminaries}
A relational vocabulary $\sigma$ is a set of relation symbols $R$, each with a specified positive integer arity.
A $\sigma$-structure $\mathscr{A}$ is given by a set $A$, the universe of the structure, and for each $R$ in $\sigma$ with arity $n$, a relation $R^{\As} \subseteq A^n$. A homomorphism $h : \mathscr{A} \rightarrow \mathscr{B}$ is a function $h : A \rightarrow B$ such that, for each relation symbol $R$ of arity $n$ in $\sigma$, for all $a_1, \ldots , a_n$ in $A$:
$R^{\As}(a_1,\ldots , a_n) \; \Rightarrow \; R^{\Bs}(h(a_1), \ldots , h(a_n))$. We write $\mathcal{R}(\sg)$ for the category of $\sigma$-structures and homomorphisms.
 
A homomorphism $h : \mathscr{A} \to \mathscr{B}$ is \emph{strong} if for $n$-ary $R$ in $\sigma$ and $a_1, \ldots , a_n \in A$, $R^{\mathscr{B}}(h(a_1), \ldots , h(a_n)) \Rightarrow R^{\mathscr{A}}(a_1, \ldots , a_n)$.
An \emph{embedding}  of relational structures is an injective strong homomorphism.  It is easily verified that the embeddings in $\mathcal{R}(\sg)$ are exactly the extremal monomorphisms; those monomorphisms $m$ such that, whenever $m = f \circ e$ for an epimorphism $e$, $e$ is an isomorphism.

A \emph{partial homomorphism} from $\mathscr{A}$ to $\mathscr{B}$ is a finite relation $r \subseteq A \times B$ which is single-valued on its domain $A_0 := \pi_1(r)$, \textit{i.e.}~~$(a, b), (a, b') \in r \; \Rightarrow \; b = b'$, and which is a homomorphism on the substructure of $\mathscr{A}$ determined by $A_0$.
A \emph{partial isomorphism} from $\mathscr{A}$ to $\mathscr{B}$ is a partial homomorphism $r$ such that the converse relation $r^{c} \subseteq B \times A$ is a partial homomorphism from $\mathscr{B}$ to $\mathscr{A}$.

We shall write $A^{\leq k}$ for the set of non-empty sequences of length $\leq k$ on a set $A$. We use list notation $[a_1,\ldots , a_j]$ for such sequences, and indicate concatenation by juxtaposition, i.e. $ss'$ is the concatenation of the sequences $s$ and $s'$. We write $s \sqsubseteq t$ for the prefix ordering on sequences. If $s \sqsubseteq t$, there is a unique $s'$ such that $ss' = t$, which we refer to as the suffix of $s$ in $t$. For each positive integer $n$, we define $\mathbf{n} := \{ 1, \ldots , n\}$. 

We shall need a few notions on posets. The comparability relation on a poset  $(P, {\leq})$ is $x {\uparrow} y$ iff  $x \leq y$ or $y \leq x$.  A chain in a poset $(P, {\leq})$ is  a subset $C \subseteq P$ such that, for all $x, y \in C$, $x {\uparrow} y$. A \emph{forest} is a poset $(F, {\leq})$ such that, for all $x \in F$, the  set of predecessors ${\downarrow}(x) \, := \, \{ y \in F \mid y \leq x\}$ is a finite chain. The height $\mathsf{ht}(F)$ of a forest $F$ is $\sup_{C} | C |$, where $C$ ranges over chains in $F$.
A \emph{tree} is a forest with a least element $\bot$ (the root).
We write the covering relation for a poset as $\prec$; thus $x \prec y$ iff $x \leq y$, $x \neq y$, and for all $z$, $x \leq z \leq y$ implies $z = x$ or $z = y$.

\subsection{Logic fragments}
\label{fragssec}

We shall be considering logics $\mathcal{L}$ which arise as fragments of $\mathcal{L}_{\infty,\omega}$, the extension of first-order logic with infinitary conjunctions and disjunctions, but where formulas contain only finitely many variables. In particular, we will consider the fragments $\mathcal{L}_{k}$, of formulas with quantifier rank $\leq k$, and $\mathcal{L}^{k}$, the $k$-variable fragment. These play a fundamental r\^ole in finite model theory.

We shall also consider two variants for each of these fragments $\mathcal{L}$. One is the existential positive fragment $\exists\mathcal{L}$, which contains only those formulas of $\mathcal{L}$ built using existential quantifiers, conjunction and disjunction. The other is $\mathcal{L}(\Count)$, the extension of $\mathcal{L}$ with counting quantifiers. These have the form $\exists_{\leq n}$, $\exists_{\geq n}$, where the semantics of $\mathscr{A} \models \exists_{\geq n} x. \, \psi$ is that there exist at least $n$ distinct elements of $A$ satisfying $\psi$.

For modal logic, we will  consider $\mathcal{M}_{k}$, the \emph{modal fragment} of modal depth $\leq k$. This arises from the standard translation of (multi)modal logic into $\mathcal{L}_{\infty,\omega}$ \cite{blackburn2002modal}. Let us fix a relational vocabulary $\sigma$ with symbols of arity $\leq 2$. For each unary symbol $P$, there will be a corresponding propositional variable $p$. Formulas are built from these propositional variables by propositional connectives, and modalities $\Box_{\alpha}$, $\Diamond_{\alpha}$ corresponding to 
relations $R_{\alpha}$  indexed by the binary relation symbols from the given alphabet.
Modal formulas $\varphi$ then admit a translation into formulas $\llbracket \varphi \rrbracket = \psi(x)$ in one free variable. The translation sends propositional variables $p$ to $P(x)$, commutes with the propositional connectives, and  sends $\Diamond_{\alpha} \varphi$ to $\exists y. \, R_{\alpha}(x,y) \, \wedge \, \psi(y)$, and $\Box_{\alpha} \varphi$ to $\forall y. \, R_{\alpha}(x, y) \rightarrow \psi(y)$, where $\psi(x) = \llbracket \varphi \rrbracket$. This translation is semantics-preserving: given  a $\sigma$-structure $\mathscr{A}$ and $a \in A$, then $\mathscr{A}, a \models \varphi$ in the sense of Kripke semantics iff $\mathscr{A} \models \psi(a)$ in the standard model-theoretic sense, where $\psi(x) = \llbracket \varphi \rrbracket$.

We define the modal depth of a modal formula $\varphi$ as the maximum nesting depth of modalities occurring in $\varphi$. $\mathcal{M}_{k}$ is then the image of the translation of modal formulas of modal depth $\leq k$. The existential positive fragment $\exists\mathcal{M}_{k}$ arises from the modal sublanguage in which formulas are built from propositional variables using only conjunction, disjunction and the diamond modalities $\Diamond_{\alpha}$.

Extensions of the modal language with counting capabilities have been studied in the form of \emph{graded modalities} \cite{Rijke2000}. These have the form $\Diamond_{\alpha}^n$, $\Box_{\alpha}^n$, where $\mathscr{A}, a \models \Diamond_{\alpha}^n \varphi$ if there are at least $n$ $R_{\alpha}$-successors of $a$ which satisfy $\varphi$. 
We define $\mathcal{M}_{k}(\#)$ to be the extension of the modal fragment with graded modalities.



\subsection{Model comparison games}

We review the model comparison games we will be concerned with in this paper. We are given structures $\mathscr{A}$ and $\mathscr{B}$.
Each game is a 2-person game, played between Spoiler, who is trying show that the structures are different, and Duplicator, who is trying to show they are the same. Each game is played in a number of rounds:
\begin{itemize}
    \item In the Ehrenfeucht-Fra\"{i}ss\'{e}~ game, in the $i$'th round Spoiler chooses an element in one of the structures, and Duplicator then chooses an element in the other structure. Thus after $k$ rounds have been played, we have sequences $[a_1, \ldots , a_k]$ and $[b_1, \ldots b_k]$ of elements from $\mathscr{A}$ and $\mathscr{B}$ respectively. Duplicator wins  this play if $r := \{ (a_i,b_i) \mid 1 \leq i \leq k \}$ is a partial isomorphism. Duplicator wins the $k$-round game if they have a winning strategy in the usual sense. The resource parameter here is $k$, the number of rounds.
    
    \item In the pebble game, each player has $k$ pebbles available, which can be ``placed'' on elements of either structure. We label the pebbles by integers in $\mathbf{k}$. In the $i$'th round, Spoiler places some pebble $p_i$ on an element of one of the structures. Note that if $p_i$ was previously placed on some other element, the effect is to remove it from that element, and place it on the newly chosen element.
    Duplicator then places their corresponding pebble $p_i$ on an element of the other structure.
     Thus the current positions of the pebbles determine  ``windows'' of  size bounded by $k$ onto the structures. These windows can slide around over different parts of the structures as moves are played.
     
    After $n$ rounds of the game have been played, we have sequences $[(p_1,a_1), \ldots , (p_n, a_n)]$ and $[(p_1, b_1), \ldots (p_n, b_n)]$ which record the placings of pebbles on elements during the play. The \emph{current placing} of pebble $p$ is the last element in the sequence with first component $p$.  Duplicator wins this play if the relation $r$ determined by the current placings of the pebbles is a partial isomorphism. Duplicator wins the $k$-pebble game if they have a strategy which is winning after $n$ rounds, for all $n$. The resource parameter here is $k$, the number of pebbles.
    
    \item Finally, in the bisimulation game, we have structures over a relational signature with symbols of arity at most 2. The game is played between ``pointed structures'' $(\mathscr{A}, a)$, $(\mathscr{B}, b)$, with specified elements $a \in A$ and $b \in B$. We start with elements $(a_0, b_0)$, where $a_0 = a$ and $b_0 = b$. At each round $i+1$, where the current elements are $(a_i, b_i)$, Spoiler chooses one of the structures, e.g.~$\mathscr{A}$, one of the binary relations $R_i$, and an element $a_{i+1}$ such that $R_i^{\mathscr{A}}(a_i, a_{i+1})$. Duplicator must respond in the other structure, in our example in $\mathscr{B}$ with $b_{i+1}$, such that $R_i^{\mathscr{B}}(b_i, b_{i+1})$. If Duplicator has no such response available, they lose. Duplicator wins after $k$ rounds if, for all unary predicates $P$ in the signature, we have $P^{\mathscr{A}}(a_{i}) \Longleftrightarrow P^{\mathscr{B}}(b_i)$ for all $i$ with $0 \leq i \leq k$. The resource parameter here is the number of rounds $k$.
\end{itemize}
There are classical theorems (Ehrenfeucht-Fra\"{i}ss\'{e}~ \cite{fraisse1955quelques,ehrenfeucht1961application}, Kolaitis-Vardi \cite{kolaitis1992infinitary}, Hennessy-Milner \cite{hennessy1980observing}) which characterize various logical equivalences in terms of these games. These can be summarized as follows:
\begin{theorem}
\label{classicgamesthm}
Let $\mathscr{A}$ and $\mathscr{B}$ be structures over a finite vocabulary.\footnote{In the modal case, this means that there  finite numbers both of modalities, and of propositional atoms.}
\begin{enumerate}
    \item Duplicator has a winning strategy in the $k$-round Ehrenfeucht-Fra\"{i}ss\'{e}~ game from $\mathscr{A}$ to $\mathscr{B}$ iff $\mathscr{A}$ and $\mathscr{B}$ satisfy the same sentences of quantifier rank $\leq k$.
    
    \item Duplicator has a winning strategy in the $k$-pebble game from $\mathscr{A}$ to $\mathscr{B}$ iff $\mathscr{A}$ and $\mathscr{B}$ satisfy the same sentences of $k$-variable logic.
    
    \item Duplicator has a winning strategy in the $k$-round bisimulation game from $(\mathscr{A}, a)$ to $(\mathscr{B}, b)$ iff $(\mathscr{A}, a)$ and $(\mathscr{B}, b)$ satisfy the same modal formulas of depth $\leq k$.
\end{enumerate}
\end{theorem}
We refer to \cite{Libkin2004,ebbinghaus2005finite,blackburn2002modal} for textbook accounts and extensive bibliographies.

The above games are symmetric or ``back-and-forth'', since Spoiler can choose which structure to play in at each round.
There are also asymmetric (forth only) or \emph{existential} versions, in which Spoiler always plays in $\mathscr{A}$, and Duplicator always responds in $\mathscr{B}$. The winning conditions are modified for these existential versions:
\begin{itemize}
    \item For the existential Ehrenfeucht-Fra\"{i}ss\'{e}~ game, the winning condition is that $r$ is a partial homomorphism.
    \item Similarly for the existential $k$-pebble game.
    \item For the $k$-round \emph{simulation game}, as the existential version of the bisimulation game is called, the winning condition is that, for all unary predicates $P$, $P(a_i) \; \Rightarrow \; P(b_i)$ for all $i$ with $0 \leq i \leq k$.
\end{itemize}
For these existential games, we obtain a corresponding version of Theorem~\ref{classicgamesthm}.
\begin{theorem}
\label{classicposgamesthm}
Let $\mathscr{A}$ and $\mathscr{B}$ be structures over a finite vocabulary.
\begin{enumerate}
    \item Duplicator has a winning strategy in the existential $k$-round Ehrenfeucht-Fra\"{i}ss\'{e}~ game from $\mathscr{A}$ to $\mathscr{B}$ iff for every existential-positive sentence $\varphi$ of quantifier rank $\leq k$, $\mathscr{A} \models \varphi$ implies $\mathscr{B} \models \varphi$. 
    
    \item Duplicator has a winning strategy in the existential $k$-pebble game from $\mathscr{A}$ to $\mathscr{B}$ iff for every existential-positive sentence $\varphi$ of $k$-variable logic, $\mathscr{A} \models \varphi$ implies $\mathscr{B} \models \varphi$.
    
    \item Duplicator has a winning strategy in the $k$-round simulation game from $(\mathscr{A}, a)$ to $(\mathscr{B}, b)$ iff for every existential-positive sentence $\varphi$ of the modal fragment with modal depth $\leq k$, $\mathscr{A} \models \varphi$ implies $\mathscr{B} \models \varphi$.
\end{enumerate}
\end{theorem}

\subsection{Comonads}
\label{comonadsec}

We recall that a comonad $(G, \varepsilon, \delta)$ on a category $\mathcal{C}$ is given by a functor $G : \mathcal{C} \rightarrow \mathcal{C}$, and natural transformations $\varepsilon : G \Rightarrow I$ (the counit), and $\delta : G \Rightarrow G^2$ (the comultiplication), subject to the conditions that the following diagrams commute, for all objects $A$ of $\mathcal{C}$:
\begin{center}
\begin{tikzcd}
G A \ar[r, "\delta_A"]  \ar[d, "\delta_A"']
& G G A \ar[d, "G \delta_A"] \\
G G A \ar[r, "\delta_{G A}"]  
& G G G A
\end{tikzcd}
$\qquad \qquad$
\begin{tikzcd}
G A \ar[r, "\delta_A"]  \ar[rd,equal] \ar[d, "\delta_A"'] 
& G G A \ar[d, "G \epsilon_{\As}"] \\
G G A \ar[r, "\varepsilon_{GA}"] 
& G A
\end{tikzcd}
\end{center}

An equivalent formulation is  \emph{comonad in Kleisli form} \cite{manes2012algebraic}. This is given by an object map $G$, arrows $\varepsilon_A : GA \rightarrow A$ for every object $A$ of $\mathcal{C}$, and a Kleisli coextension operation which takes $f : GA \rightarrow B$ to $f^* : GA \rightarrow GB$. These must satisfy the following equations:
\[ \varepsilon_{A}^* = \mathsf{id}_{G A}, \qquad \varepsilon_{B} \circ f^* = f, \qquad (g \circ f^*)^* = g^* \circ f^* , \]
where $g : GB \to C$.
We can then extend $G$ to a functor by $G f = (f \circ \varepsilon_{A})^*$; and if we define the comultiplication $\delta : G \Rightarrow G^2$ by $\delta_{A} = \mathsf{id}_{GA}^*$, then $(G, \varepsilon, \delta)$ is a comonad in the standard sense.
Conversely, given a comonad $(G, \varepsilon, \delta)$, we can define the coextension by $f^* = Gf \circ \delta_A$.
This allows us to define the coKleisli category $\mathsf{Kl}(G)$, with objects the same as those of $\mathcal{C}$, and morphisms from $A$ to $B$ given by the morphisms in $\mathcal{C}$ of the form $GA \rightarrow B$. Kleisli composition of $f : GA \rightarrow B$ with $g : GB \rightarrow C$ is given by $g \bullet f \, := \, g \circ f^*$.

\section{Game Comonads}

We shall now define the three comonads we shall study, corresponding to the three forms of model comparison game described in the previous section.

\subsection{The Ehrenfeucht-Fra\"{i}ss\'{e}~ Comonad}

We shall define a comonad $\mathbb{E}_{k}$ on $\mathcal{R}(\sg)$ for each positive integer $k$. It will be convenient to define $\mathbb{E}_{k}$ in Kleisli form.
For each structure $\mathscr{A}$, we define a new structure $\mathbb{E}_{k} \mathscr{A}$, with universe $\mathbb{E}_{k} A \, := \, A^{\leq k}$. We define the map $\epsilon_{\As} : \mathbb{E}_{k} A \rightarrow A$ by $\epsilon_{\As} [a_1, \ldots , a_j ] = a_j$. For each relation symbol $R$ of arity $n$, we define $R^{\Ek \As}$ to be the set of $n$-tuples $(s_1, \ldots , s_n)$ of sequences which are pairwise comparable in the prefix ordering, and such that $R^{\As}(\epsilon_{\As} s_1, \ldots , \epsilon_{\As} s_n)$. Finally, we define the coextension. Given a homomorphism $f : \mathbb{E}_{k} \mathscr{A} \rightarrow \mathscr{B}$, we define $f^* : A^{\leq k} \rightarrow B^{\leq k}$ by $f^* [a_1, \ldots , a_j ] = [b_1, \ldots , b_j]$, where $b_i = f [a_1, \ldots , a_i]$, $1 \leq i \leq j$.

\begin{proposition}\label{efcomonprop}
The triple $(\mathbb{E}_{k}, \varepsilon, (\cdot)^*)$ is a comonad in Kleisli form.
\end{proposition}
\begin{proof}
It is well known that non-empty lists form a comonad on $\mathbf{Set}$ \cite{ahman2012container}. This is easily adapted to showing that non-empty lists of length $\leq k$ form a comonad. To lift this to $\mathcal{R}(\sg)$, we must show that each $\varepsilon_{\mathscr{A}}$ is a homomorphism, and that if $f : \mathbb{E}_{k} \mathscr{A} \to \mathscr{B}$ is a homomorphism, so is $f^*$. The first follows directly from the definition of $R^{\Ek \As}$ for each $R$ in $\sigma$.
For the second, if $R^{\Ek \As}(s_1,\ldots,s_n)$, then $s_i {\uparrow} s_j$ implies $f^*(s_i) {\uparrow} f^*(s_j)$, and since $f$ is a homomorphism, $R^{\Ek \As}( s_1, \ldots , s_n)$ implies $R^{\Bs}(\varepsilon f^*(s_1),\ldots , \varepsilon f^*(s_n))$, so $R^{\mathbb{E}_{k} \mathscr{B}}(f^*(s_1),\ldots , f^*(s_n))$ as required.
\end{proof}
Intuitively, an element of $A^{\leq k}$ represents a play in $\mathscr{A}$ of length $\leq k$. A coKleisli morphism $\mathbb{E}_{k} \mathscr{A} \rightarrow \mathscr{B}$ represents a Duplicator strategy for the existential Ehrenfeucht-Fra\"{i}ss\'{e}~ game with $k$ rounds, where Spoiler plays only in $\mathscr{A}$, and $b_i = f [a_1, \ldots , a_i]$ represents Duplicator's response in $\mathscr{B}$ to the $i$'th move by Spoiler. The winning condition for Duplicator in this game is that, after $k$ rounds have been played, 
the \textit{induced relation} $\{ (a_i, b_i) \mid 1 \leq i \leq k \}$ is a partial homomorphism from $\mathscr{A}$ to $\mathscr{B}$.

These intuitions are confirmed by the following result.
\begin{theorem}
\label{EFgamethm}
The following are equivalent:
\begin{enumerate}
\item There is a homomorphism $f : \mathbb{E}_{k} \mathscr{A} \rightarrow \mathscr{B}$.
\item Duplicator has a winning strategy for the existential Ehrenfeucht-Fra\"{i}ss\'{e}~ game with $k$ rounds, played from $\mathscr{A}$ to $\mathscr{B}$.
\end{enumerate}
\end{theorem}
\begin{proof}
 $(1) \Rightarrow (2)$. Suppose Spoiler plays $a_{1},\dots,a_{k}$, then for each $i \in \{1,\dots,k\}$, Duplicator responds with $b_i = b_j$ if $a_j = a_i$ for some $j < i$ \footnote{We could eliminate this case by imposing an additional \textit{I-morphism} condition on the homomorphisms of type $\mathbb{E}_{k} \mathscr{A} \rightarrow \mathscr{B}$. This condition is discussed in Section~\ref{Imorsec}.} or $b_i = f[a_1,\dots,a_i]$ otherwise. We must show that $\gamma = \{(a_{i},b_{i}) \mid 1 \leq i \leq k \}$ is a partial homomorphism. By construction, $b_i = b_j$ if $a_i = a_j$, so $\gamma$ is a well-defined partial function.  Let $R \in \sigma$ and suppose $R^{\mathscr{A}}(a_{i_1},\dots,a_{i_n})$ with $i_{j}  \in \{1,\dots,k\}$ for all $j \leq n$. Let $s_{i_{j}}$ be the minimal subsequence of $s = [a_1,\dots, a_k]$ that ends in $a_{i_{j}}$. Since each $s_{i_{j}}$ is a subsequence of $s$, each pair of $s_{i_{j}}$ is prefix comparable and by definition, $\epsilon_{\As}(s_{i_{j}}) = a_{i_{j}}$, so $R^{\mathbb{E}_{k}\mathscr{A}}(s_{i_{1}},\dots,s_{i_{n}})$. By $f$ being a homomorphism, $R^{\mathscr{B}}(b_{i_{1}},\dots,b_{i_{n}})$, so $\gamma$ is a partial homomorphism.

$(2) \Rightarrow (1)$. Conversely, suppose Duplicator has a winning strategy in the $n$-round Ehrenfeucht-Fra\"{i}ss\'{e}~ game (with $n \leq k$). For every possible set of Spoiler moves in the $n$-round game, i.e. for every sequence $s = [a_1,\dots,a_n]$, there exists a sequence $t = [b_1,\dots,b_n]$ such that $\gamma = \{(a_i,b_i) \mid 1 \leq i \leq n \}$ is a partial homomorphism. Let $\gamma_{s}$ be the partial homomorphism resulting from Duplicator's responses to Spoiler's play $s$. We define $f:\mathbb{E}_{k}\mathscr{A} \rightarrow \mathscr{B}$ by $f(s_i) = \gamma_s(a_i)$ where $s_i \sqsubseteq s$ ending in $a_i$. Since $\gamma_s$ is a partial homomorphism for all $s \in \mathbb{E}_{k}\mathscr{A}$, $f$ is a homomorphism. Namely, suppose $s_1,\dots,s_m $ are such that $R^{\mathbb{E}_{k}\mathscr{A}}(s_1,\dots,s_m)$. By the pairwise comparability condition, there exists some $\sqsubseteq$-greatest $s = [a_1,\dots,a_{l}]$ with $l \leq n$ amongst the $s_i$. Since $s_i \sqsubseteq s$, the last move of $s_i$, $\varepsilon_{A}(s_i)$ must be in $\{a_1,\dots,a_{l}\}$. By the compatiblity condition, we have that $R^{\mathscr{A}}(\varepsilon_{A}(s_1),\dots,\varepsilon_{A}(s_m))$. Hence, since $\gamma_{s}$ is a partial homomorphism, $R^{\mathscr{B}}(\gamma_{s}(\epsilon_{\As}(s_1)),\dots,\gamma_{s}(\epsilon_{\As}(s_m)))$. Since $f(s_i) = \gamma_{s}(\epsilon_{\As}(s_i))$, we have that $R^{\mathscr{B}}(f(s_1),\dots,f(s_m))$.  
\end{proof}

\subsection{The Pebbling Comonad}

We now turn to the case of pebble games. The following construction appeared in \cite{abramsky2017pebbling}.
Given a structure $\mathscr{A}$, we define $\mathbb{P}_k \mathscr{A}$, which will represent plays of the $k$-pebble game on $\mathscr{A}$.\footnote{In \cite{abramsky2017pebbling} we used the notation $\mathbb{T}_k$ for this comonad.} The universe is  $(\mathbf{k} \times A)^{+}$, the set of finite non-empty sequences of moves $(p, a)$, where $p \in \mathbf{k}$ is a pebble index, and $a \in A$. We shall use the notation $s = [ (p_1, a_1), \ldots , (p_n, a_n)]$ for these sequences, which may be of arbitrary length. Thus the universe of $\mathbb{P}_k \mathscr{A}$ is always infinite, even if $\mathscr{A}$ is a finite structure. This is unavoidable, by 
\cite[Theorem 7]{abramsky2017pebbling}, which shows that no finite construction is equivalent to $\mathbb{P}_k$, even at the level of the preorder collapse of the coKleisli category. We  define $\epsilon_{\As} : \mathbb{P}_k A \rightarrow A$ to send a play  $[ (p_1, a_1), \ldots , (p_n, a_n)]$ to $a_n$, the $A$-component of its last move.

Given an $n$-ary relation $R \in \sigma$, we define $R^{\mathbb{P}_k \mathscr{A}}(s_1, \ldots , s_n)$ iff (1) the $s_i$ are pairwise comparable in the prefix ordering;
(2) the pebble index of the last move in each $s_i$ does not appear in the suffix of $s_i$ in $s_j$ for any $s_j \sqsupseteq s_i$; and (3) $R^{\mathscr{A}}(\epsilon_{\As}(s_1), \ldots , \epsilon_{\As}(s_n))$.

Note that this differs from the definition of $R^{\Ek \As}$ just in condition (2). Intuitively, Spoiler moves by placing one from a fixed set of pebbles on an element of $A$; Duplicator responds by placing their matching pebble on an element of the other structure.
Thus there is a ``sliding window'' on the structures, of fixed size. It is this size which bounds the resource, not the length of the play. In particular, placing a pebble on an element implies removing it from the element on which it was previously placed. Condition (2) enforces that only the \emph{current} position of a pebble is taken into account when deciding whether elements are related.


Finally, given a homomorphism $f : \mathbb{P}_k \mathscr{A} \rightarrow \mathscr{B}$, we define $f^* : \mathbb{P}_k A \rightarrow \mathbb{P}_k B$ by \\
$f^*  [ (p_1, a_1), \ldots , (p_j, a_j)] = [(p_1, b_1), \ldots , (p_j, b_j)]$,
where $b_i = f [(p_1, a_1), \ldots , (p_i, a_i)]$, $1 \leq i \leq j$.

\begin{proposition}\label{pebbcomonprop}
The triple $(\mathbb{P}_k, \varepsilon, (\cdot)^*)$ is a comonad in Kleisli form.
\end{proposition}

This is \cite[Theorem 4]{abramsky2017pebbling}. The following is  \cite[Theorem 13]{abramsky2017pebbling}.
\begin{theorem}
\label{strmorth}
The following are equivalent:
\begin{enumerate}
\item There is a homomorphism $\mathbb{P}_k \mathscr{A} \rightarrow \mathscr{B}$.
\item There is a winning strategy for Duplicator in the existential $k$-pebble game from $\mathscr{A}$ to $\mathscr{B}$.
\end{enumerate}
\end{theorem}


\subsection{The Modal Comonad}
 
For the modal case, we assume that the relational vocabulary $\sigma$ contains only symbols of arity at most 2. We can thus regard a $\sigma$-structure as a Kripke structure for a multi-modal logic, where the universe is thought of as a set of worlds, each binary relation symbol $R_{\alpha}$ gives the accessibility relation for one of the modalities, and each unary relation symbol $P$ give the valuation for a corresponding propositional variable. If there are no unary symbols, such structures are exactly the labelled transition systems widely studied in concurrency \cite{milner1989communication}.

Modal logic localizes its notion of satisfaction in a structure to a world. We shall reflect this by using the category of \emph{pointed relational structures} $\mathcal{R}_{\star}(\sg)$. Objects of this category are pairs $(\mathscr{A}, a)$ where $\mathscr{A}$ is a $\sigma$-structure and $a \in A$. Morphisms $h : (\mathscr{A}, a) \rightarrow (\mathscr{B}, b)$ are homomorphisms $h : \mathscr{A} \rightarrow \mathscr{B}$ such that $h(a) = b$. Of course, the same effect could be achieved by expanding the vocabulary $\sigma$ with a constant, but pointed categories appear in many mathematical contexts.

For each $k \geq 0$, we shall define a comonad $\mathbb{M}_{k}$, where $\mathbb{M}_{k} (\mathscr{A}, a)$ corresponds to unravelling the structure $\mathscr{A}$, starting from $a$, to depth $k$. 
The universe of $\mathbb{M}_{k} (\mathscr{A}, a)$ comprises the unit sequence $[a]$, which is the distinguished element,  together with all sequences of the form $[a_0, \alpha_1, a_1, \ldots , \alpha_j, a_{j}]$, where $a = a_0$, $1 \leq j  \leq k$, and $R^{\As}_{\alpha_i}(a_i, a_{i+1})$, $0 \leq i < j$. Note that the universe of $\mathbb{M}_{0} (\mathscr{A}, a)$ is $\{ [a] \}$.

The map $\epsilon_{\As} : \mathbb{M}_{k} (A, a) \rightarrow  (A, a)$ sends a sequence to its last element. Unary relation symbols $P$ are interpreted by $P^{\Mk (\As, a)}(s)$ iff $P^{\As}(\epsilon_{\As} s)$. For binary relations $R_{\alpha}$, the interpretation is $R^{\Mk (\As, a)}_{\alpha}(s,t)$ iff for some $a' \in A$, $t = s[\alpha, a']$.
 Given a homomorphism $f : \mathbb{M}_{k} (\mathscr{A}, a) \rightarrow (\mathscr{B}, b)$, we define $f^* : \mathbb{M}_{k} (\mathscr{A}, a) \rightarrow \mathbb{M}_{k} (\mathscr{B}, b)$ recursively by $f^*[a] = [b]$,
 $f^*(s[\alpha, a']) = f^*(s)[\alpha, b']$ where $b' = f(s[\alpha, a'])$.



\begin{proposition}
The triple $(\mathbb{M}_{k}, \varepsilon, (\cdot)^*)$ is a comonad in Kleisli form on $\mathcal{R}_{\star}(\sg)$.
\end{proposition}
\begin{proof}
This follows very similar lines to the proofs of Propositions~\ref{efcomonprop} and~\ref{pebbcomonprop}.
\end{proof}


\noindent
We recall the notion of \emph{simulation} between Kripke structures \cite{blackburn2002modal}. Given structures $\mathscr{A}$, $\mathscr{B}$, we define relations ${\preceq_k} \; \subseteq \; A \times B$, $k \geq 0$, by induction on $k$: $a \preceq_{k+1} b$ iff
        \begin{enumerate}
          \item for all unary $P$, $P^{\As}(a)$ implies $P^{\Bs}(b)$ \label{sim:1}
          \item for all $R_{\alpha}$, if $R_{\alpha}^{\As}(a, a')$, then for some $b'$, $R_{\alpha}^{\Bs}(b, b')$ and $a' \preceq_k b'$. \label{sim:2}
        \end{enumerate}
The base case $a \preceq_{0} b$ holds whenever the first condition is satisfied. It is standard that these relations are equivalently formulated in terms of a modified existential Ehrenfeucht-Fra\"{i}ss\'{e}~ game \cite{blackburn2002modal,gradel2014freedoms}.

\begin{theorem}
\label{simthm}
Let $\mathscr{A}$, $\mathscr{B}$ be Kripke structures, with $a \in A$ and $b \in B$, and $k \geq 0$.
The following are equivalent:
\begin{enumerate}
\item There is a homomorphism $f : \mathbb{M}_{k} (\mathscr{A}, a) \rightarrow (\mathscr{B}, b)$.
\item $a \preceq_k b$.
\item There is a winning strategy for Duplicator in the $k$-round simulation game from $(\mathscr{A}, a)$ to $(\mathscr{B}, b)$.
\end{enumerate}
\end{theorem}
\begin{proof}
(1) $\Rightarrow$ (2) We prove this by induction on $k$. For the base case $k = 0$, by (1), there exists a homomorphism, $f:\mathbb{M}_{0}(\mathscr{A},a) \rightarrow (\mathscr{B},b)$. Hence, for all unary $P$, $P^{\As}(a) \Rightarrow P^{\mathbb{M}_1(\mathscr{A},a)}([a]) \Rightarrow P^{\Bs}(f[a]) \Rightarrow P^{\Bs}(b)$, so $a \preceq_{0} b$. For the inductive step, let $k' = k+1$ and assume there is a homomorphism $f:\mathbb{M}_{k'}(\mathscr{A},a) \rightarrow (\mathscr{B},b)$. Suppose that $R_{\alpha}^{\As}(a,a')$, then there exists a $b' = f[a,\alpha,a']$ such that $R_{\alpha}^{\Bs}(b,b')$. Consider the homomorphism $f_{\alpha,a'}:\mathbb{M}_{k}(\mathscr{A},a') \rightarrow (\mathscr{B},b')$ given by $f[a'] = b'$ and $f_{\alpha,a'}(s) = f([a,\alpha,a']s)$. Applying the inductive hypothesis, we obtain that $a' \preceq_k b'$, verifying condition 2 for a simulation. To verify condition 1, by the definition of $\mathbb{M}_{k'}(\mathscr{A},a)$ and $f$ being a homomorphism we have:  $P^{\As}(a) \Rightarrow P^{\mathbb{M}_{k'}(\mathscr{A},a)}([a]) \Rightarrow P^{\Bs}(f[a]) \Rightarrow P^{\Bs}(b)$. Therefore, $a \preceq_{k+1} b$ as desired. 

(2) $\Rightarrow$ (3) This is a standard result, and is detailed in section 2.7 of \cite{blackburn2002modal}.

(3) $\Rightarrow$ (1) We construct $f_{i}$ for $i \leq k$ by induction. For the base case $k = 0$, define $f_{0}:\mathbb{M}_{0}(\mathscr{A},a) \rightarrow (\mathscr{B},b)$ as $f_{0}[a] = b$. 
This is trivially a homomorphism.
Suppose, by the inductive hypothesis, $f_{k}:\mathbb{M}_{k}(\mathscr{A},a) \rightarrow (\mathscr{B},b)$ has been constructed. Let $k' = k+1$ and we define $f_{k'}:\mathbb{M}_{k'}(\mathscr{A},a) \rightarrow (\mathscr{B},b)$ by splitting into two cases. For the first case, consider $t \in \mathbb{M}_{k'}(\mathscr{A},a)$ such that $t = s[\alpha,a']$ for some $s \in \mathbb{M}_{k}(\mathscr{A},a)$, $R_{\alpha} \in \sigma$ and $R_{\alpha}^{\As}(a,a')$. We can take $s$ as the record of the Spoiler's moves in the first $k$ rounds of the simulation game. In this case, we let $f_{k'}(t) = b'$ where $b'$ is Duplicator's response to Spoiler choosing the $\alpha$-transition to $a'$ in the $k'$-th round. For the second case, consider $t \in \mathbb{M}_{k}(\mathscr{A},a) \subseteq \mathbb{M}_{k'}(\mathscr{A},a)$. In this case, define $f_{k'}(t) = f_k(t)$. We need to verify $f_{k'}$ is indeed a homomorphism. Suppose $s,t \in \mathbb{M}_{k'}(\mathscr{A},a)$ are such that $R_{\alpha}^{\mathbb{M}_{k'} (\mathscr{A}, a)}(s,t)$. If $s,t \in \mathbb{M}_{k}(\mathscr{A},a) \subseteq \mathbb{M}_{k'}(\mathscr{A},a)$, then $f_{k'}(s) = f_{k}(s)$ and $f_{k'}(t) = f_{k}(t)$. Therefore, by the inductive hypothesis that $f_{k}$ is a homomorphism, $R_{\alpha}^{\Bs}(f_{k'}(s),f_{k'}(t))$,
and moreover, for each unary predicate $P$, $P^{\mathbb{M}_{k}(\mathscr{A}, a)}(t)$ implies $P^{(\mathscr{B}, b)}(f_{k'}(t))$. By the definition of $R_{\alpha}^{\mathbb{M}_{k'}(\mathscr{A},a)}$, the only other case is $t = s[\alpha,a']$. Since $f_{k'}(t)$ is Duplicator's response to the $k'$-round in her winning strategy, we can conclude that $R_{\alpha}^{\Bs}(f_{k'}(s),f_{k'}(t))$, and
for each unary predicate $P$, $P^{\mathbb{M}_{k}(\mathscr{A}, a)}(t)$ implies $P^{(\mathscr{B}, b)}(f_{k'}(t))$.
\end{proof}

\section{$I$-morphisms}
\label{Imorsec}

Before turning to logical equivalences, there is a technical issue which needs to be addressed.

A coKleisli morphism $f : \mathbb{E}_{k} \mathscr{A} \to \mathscr{B}$ is an \emph{$I$-morphism} if $s \sqsubseteq t$ and $\epsilon_{\As}(s) = \epsilon_{\As}(t)$ implies that $f(s) = f(t)$. 
\begin{proposition}
Consider $\sigma^{+} = \sigma \cup \{I\}$, where $I$ is a binary relation symbol not in $\sigma$. If we interpret $I^{\mathscr{A}}$ and $I^{\mathscr{B}}$ as the identity relations on $A$ and $B$, then $f : \mathbb{E}_{k} \mathscr{A} \to \mathscr{B}$ is an $I$-morphism iff it is a $\sigma^{+}$-homomorphism.
\end{proposition}
\begin{proof}
Suppose $f$ is an $I$-morphism, and $I^{\mathbb{E}_{k} \mathscr{A}}(s,t)$.
By definition, this means that $s {\uparrow} t$  and $I^{\mathscr{A}}(\epsilon_{\As}(s), \epsilon_{\As}(t))$, \textit{i.e.}~ $\epsilon_{\As}(s) = \epsilon_{\As}(t)$. By the $I$-morphism definition, this implies $f(s) = f(t)$, and by the interpretation of $I^{\mathscr{B}}$ as the identity relation, $I^{\mathscr{B}}(f(s),f(t))$. Hence, $f$ preserves $I$, and is therefore a $\sigma^{+}$-homomorphism. The proof of the converse is similar.  
\end{proof}
The significance of the $I$-morphism condition becomes apparent from the following proposition.
\begin{proposition}
\label{Imorpisoprop}
If $f : \mathbb{E}_{k} \mathscr{A} \to \mathscr{B}$ and $g : \mathbb{E}_{k} \mathscr{B} \to \mathscr{A}$ are $I$-morphisms, then $f^*(s) =t$, $g^*(t) = s$ imply that $(s,t)$ defines a partial isomorphism from $\mathscr{A}$ to $\mathscr{B}$.
\end{proposition}
\begin{proof}
Let $s = [a_1,\dots,a_n]$ and $t = [b_1,\dots,b_n]$ with $n \leq k$, then the partial function defined by $(s,t)$ is $\gamma:a_i \mapsto b_i$. Suppose $a_j = a_l$ for some $j,l \leq n$ and let $s_j,s_l$ be the subsequences of $s$ ending in $a_j,a_l$ (respectively). Since $s_j {\uparrow} s_l$ and $\epsilon_{A}(s_j) = a_j = a_l = \epsilon_{A}(s_l)$, by the $I$-morphism condition, $f(s_j) = f(s_l)$. By definition of $f^*$ and $\gamma$, $b_j = b_l$. Hence, $\gamma$ is well-defined. Suppose $R \in \sigma$ is an $m$-ary relation symbol with $R^{\As}(a_{i_1},\dots,a_{i_m})$ for $a_{i_j} \in s$ and let $s_{i_j}$ denote the prefix of $s$ such that $\epsilon_{A}(s_{i_j}) = a_{i_j}$. Since each $s_{i_j}$ is a prefix of $s$, the set of all $s_{i_j}$ is pairwise comparable, so $R^{\Ek \As}(s_{i_1},\dots,s_{i_m})$ implying that $R^{\Bs}(f(s_{i_1}),\dots,f(s_{i_m}))$ by $f$ being a homomorphism. This yields that $R^{\Bs}(b_{i_1},\dots,b_{i_m})$, since $f(s_{i_j}) = b_{i_j}$ follows from $f^*(s) = t$. Hence, $\gamma$ preserves relations, and therefore is a partial homomorphism. Similarly, define $\gamma^{-1}:b_i \mapsto a_i$, which is also well-defined and a partial homomorphism. By construction, $\gamma$ and $\gamma^{-1}$ are inverses, so $(s,t)$ defines a partial isomorphism from $\mathscr{A}$ to $\mathscr{B}$.
\end{proof}

In the light of this result, the reader may wonder why we do not simply work with a sub-comonad of $\mathbb{E}_{k}$, in which only non-repeating sequences are allowed. The problem with this is that the functorial action of $\mathbb{E}_{k}$ on non-injective maps will produce repeating sequences. The same problem arises with the coKleisli extension.

A similar notion of $I$-morphism applies to the pebbling comonad \cite{abramsky2017pebbling}. 
Note that, if $I^{\mathscr{A}}$ is the identity relation on $\mathscr{A}$, $I^{\mathbb{P}_{k} \mathscr{A}}(s, t)$ holds if $s = s'[(p,a)]$, $t = t'[(p', a)]$, $s$ and $t$ are comparable in the prefix order, say $s \sqsubseteq t$, and the placing of $p$ in $s$ is still current in $t$. If $p \neq p'$, this means that two different pebbles are placed on the same element of $\mathscr{A}$. We call such a sequence \emph{duplicating}. Duplicating sequences are to $\mathbb{P}_{k}$ what repeating sequences are to $\mathbb{E}_{k}$.

Dan Marsden and Tom\'a\v{s} Jakl have shown\footnote{Personal communication} that it is possible to define a retract of the $\mathbb{E}_{k}$ comonad in a more complicated fashion, which does eliminate repeating sequences. However, it is not clear how this can be extended to the pebbling comonad.

We shall develop an alternative, general approach, based on \emph{relative comonads}
\cite{altenkirch2010monads}. 
We shall not in fact need the full generality of relative comonads. What we shall use is the following special case, mentioned in \cite{altenkirch2010monads}. Given a functor $J : \mathcal{C} \to \mathcal{D}$, and a comonad $G$ on $\mathcal{D}$, we  obtain a relative comonad on $\mathcal{C}$ whose coKleisli category is defined as follows.
A morphism from $A$ to $B$, for objects $A$, $B$ of $\mathcal{C}$, is a $\mathcal{D}$-arrow $G J A \to J B$. The counit at $A$ is $\varepsilon_{J A}$, using the counit of $G$ at $J A$. Given $f : G J A \to J B$, the coKleisli extension $f^* : G J A \to G J B$ is the coKleisli extension of $G$.
Since $G$ is a comonad, these operations satisfy the equations for a comonad in Kleisli form.
It is in fact simpler to describe relative comonads in coKleisli form, as explained in \cite{altenkirch2010monads}.

To apply this to our setting,
we can take advantage of the fact that our comonads are defined uniformly in the relational vocabulary.
Given a relational vocabulary $\sigma$, there is a full and faithful embedding $J : \mathcal{R}(\sg) \to \mathcal{R}(\sigma^{+})$ such that $I^{J \mathscr{A}}$ is the identity on $A$.
Moreover, we have a comonad $\mathbb{E}_{k}^{I}$, which is our $\mathbb{E}_{k}$ construction applied to $\mathcal{R}(\sigma^{+})$.
We use this data to obtain the $J$-relative comonad $\mathbb{E}_{k}^{+}$ on $\mathcal{R}(\sg)$ as defined above.
Explicitly, we have the following coKleisli category: objects are those of $\mathcal{R}(\sg)$, morphisms from $\mathscr{A}$ to $\mathscr{B}$ are $\mathcal{R}(\sg)$-morphisms $\mathbb{E}_{k}^{I} J \mathscr{A} \to J \mathscr{B}$.
The counit at $\mathscr{A}$ is $\varepsilon_{J\mathscr{A}} : \mathbb{E}_{k}^{I} J \mathscr{A} \to J \mathscr{A}$, the counit for $\mathbb{E}_{k}^{I}$ at $J \mathscr{A}$. The coKleisli extension of $f : \mathbb{E}_{k}^{I} J \mathscr{A} \to J \mathscr{B}$ is $f^* : \mathbb{E}_{k}^{I} J \mathscr{A} \to \mathbb{E}_{k}^{I} J \mathscr{B}$, defined using the coKleisli extension for $\mathbb{E}_{k}^{I}$. 

The important point is that the morphisms in this $J$-relative coKleisli category are exactly the $I$-morphisms in the coKleisli category for the comonad $\mathbb{E}_{k}$ on $\mathcal{R}(\sg)$.

This construction is perfectly general.
In particular, it can be carried out in exactly the same fashion for the pebbling comonad. In this case, it is the \emph{duplicating} sequences which are problematic, \textit{i.e.}~ those in which different  pebbles are placed on the same element \cite{abramsky2017pebbling}.
This condition is discussed in more detail in Section~\ref{logeqIIsec}.
The relative comonad approach provides a uniform solution.

\section{Logical Equivalences I}

We now show how our game comonads can be used to give syntax-free characterizations of a range of logical equivalences, which play a central r\^ole in finite model theory and modal logic.

Two equivalences can be defined uniformly for any indexed family of comonads $\mathbb{C}_{k}$:
\begin{itemize}
\item $\mathscr{A} \rightleftarrows_{k}^{\mathbb{C}} \mathscr{B}$ iff there are coKleisli morphisms $\mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{B}$ and $\mathbb{C}_{k} \mathscr{B} \rightarrow \mathscr{A}$. Note that there need be no relationship between these morphisms.
This is simply the equivalence induced by the preorder collapse of the coKleisli category.
\item $\mathscr{A} \cong_{k}^{\mathbb{C}} \mathscr{B}$ iff $\mathscr{A}$ and $\mathscr{B}$ are isomorphic in the coKleisli category $\mathsf{Kl}(\mathbb{C}_{k})$. This means that there are morphisms $\mathbb{C}_{k} \mathscr{A} \rightarrow \mathscr{B}$ and $\mathbb{C}_{k} \mathscr{B} \rightarrow \mathscr{A}$ which are inverses of each other in $\mathsf{Kl}(\mathbb{C}_{k})$.
\end{itemize}
Clearly, $\cong_{k}^{\mathbb{C}}$ strictly implies $\rightleftarrows_{k}^{\mathbb{C}}$.

We introduced a number of logic fragments in Section~\ref{fragssec}.
Each of these logics $\mathcal{L}$ induces an equivalence on structures in $\mathcal{R}(\sg)$:
\[ \mathscr{A} \equiv^{\LL} \mathscr{B} \;\; \stackrel{\Delta}{\IFF} \;\; \forall \varphi \in \mathcal{L}. \; \mathscr{A} \models \varphi \; \Longleftrightarrow \; \mathscr{B} \models \varphi . \]
Our aim is to characterize these equivalences in terms of our game comonads, and more specifically, to use morphisms in the coKleisli categories as witnesses for these equivalences. 

We shall use the two equivalences $\rightleftarrows_{k}^{\mathbb{C}}$ and $\cong_{k}^{\mathbb{C}}$ induced by comonads as described above to characterize the logical equivalences induced by the existential-positive and counting versions of these logic fragments, $\exists\mathcal{L}_{k}$ and $\mathcal{L}_{k}^{\#}$, respectively. We shall use the relative versions $\mathbb{E}_{k}^{+}$ and $\mathbb{P}_{k}^{+}$ of the Ehrenfeucht-Fra\"{i}ss\'{e}~ and pebbling comonads, as described in the previous section. These impose the condition of being $I$-morphisms, which will be important to get a precise match with the winning conditions of the corresponding logical games.

We shall defer the treatment of back-and-forth equivalences to Section~\ref{backandforthsec}, as it will build on material to be developed later. 

We now turn to a detailed study of each of our comonads in turn.

\subsection{The Ehrenfeucht-Fra\"{i}ss\'{e}~ comonad}
\label{logeqsecEF}
\label{sec}

We begin by using the $I$-morphism condition enforced by $\mathbb{E}_{k}^{+}$ to sharpen Theorem~\ref{EFgamethm} from an equivalence to a bijective correspondence.
\begin{theorem}
\label{EFgameeqthm}
There is a bijective correspondence between:
\begin{enumerate}
\item $\mathbb{E}_{k}^{+}$ coKleisli morphisms from $\mathscr{A}$ to $\mathscr{B}$.
\item Winning strategies for Duplicator for the existential Ehrenfeucht-Fra\"{i}ss\'{e}~ game with $k$ rounds, played from $\mathscr{A}$ to $\mathscr{B}$.
\end{enumerate}
\end{theorem}
\begin{proof}
The proof proceeds as for Theorem~\ref{EFgamethm}, but we now use the fact that the $I$-morphism condition ensures that if $f^*(s) = t$, the correspondence $\epsilon_{\As} s_i \mapsto \varepsilon_{\Bs} t_i$ is single-valued, and hence satisfies the partial homomorphism winning condition for the existential game. Indeed, if $\epsilon_{\As} s_i = \epsilon_{\As} s_j$, then $I^{\mathbb{E}_{k} \mathscr{A}}(s_i,s_j)$.
Since $f$ is an $I$-morphism, this implies $I^{\mathbb{E}_{k} \mathscr{B}}(t_i,t_j)$, and hence $\varepsilon_{B} t_i = \varepsilon_{B} t_j$.
Conversely, a winning strategy will
produce pairs $(s,t)$ which satisfy the partial homomorphism condition, and in particular single-valuedness. Hence it induces a coKleisli map satisfying the $I$-morphism condition.
\end{proof}


We now recall the \emph{bijection game} \cite{Hella1996}. In this variant of the Ehrenfeucht-Fra\"{i}ss\'{e}~ game, Spoiler wins if the two structures have different cardinality. Otherwise, at round $i$, Duplicator chooses a bijection $\psi_i$ between $A$ and $B$, and Spoiler chooses an element $a_i$ of $A$. This determines the choice by Duplicator of $b_i = \psi_i(a_i)$. Duplicator wins after $k$ rounds if the relation $\{ (a_i, b_i) \mid 1 \leq i \leq k \}$ is a partial isomorphism.

\begin{proposition}
\label{bijgameprop}
The following are equivalent, for finite structures $\mathscr{A}$ and $\mathscr{B}$:
\begin{enumerate}
\item $\mathscr{A} \cong_{k}^{\mathbb{E}} \mathscr{B}$.
\item There is a winning strategy for Duplicator in the $k$-round bijection game.
\end{enumerate}
\end{proposition}
\begin{proof}
Assuming (1), we have $I$-morphisms $f : \mathbb{E}_{k} \mathscr{A} \to \mathscr{B}$ and $g : \mathbb{E}_{k} \mathscr{B} \to \mathscr{A}$ with $g^* = {f^*}^{-1}$. 
For each $s \in \{ []\} \cup A^{<k}$, where $[]$ is the empty sequence, and $A^{<k}$ is the set of sequences over $A$ of length strictly less than $k$, we can define a map $\psi_s : A \to B$, by $\psi_s (a) = f(s[a])$. This is a bijection, with inverse $\chi_{t}$, $t = f^*(s)$, defined similarly from $g$. These bijections provide a strategy for Duplicator. Since by Proposition~\ref{Imorpisoprop}, each $(s, f^*(s))$ is a partial isomorphism, this is a winning strategy.

Conversely, a winning strategy provides bijections $\psi_s$, which we can use to define $f$ by $f(s[a]) = \psi_s(a)$. The winning conditions imply that this is an $I$-isomorphism in the coKleisli category.
\end{proof}


We can now state our main result on logical equivalences for the Ehrenfeucht-Fra\"{i}ss\'{e}~ comonad.
\begin{theorem}
\begin{enumerate}
\item For all structures $\mathscr{A}$ and $\mathscr{B}$: $\mathscr{A}  \equiv^{\ELk} \mathscr{B} \; \Longleftrightarrow \; \mathscr{A} \rightleftarrows_{k}^{\mathbb{E}} \mathscr{B}$.
\item For all finite structures $\mathscr{A}$ and $\mathscr{B}$: $\mathscr{A} \equiv^{\Lck} \mathscr{B} \; \Longleftrightarrow \; \mathscr{A} \cong_{k}^{\mathbb{E}} \mathscr{B}$.
\end{enumerate}
\label{thm:mainEF}
\end{theorem}
\begin{proof}
(1) In \cite{kolaitis1990expressive}, it is shown that $\mathscr{A}  \equiv^{\ELk} \mathscr{B}$ iff Duplicator has a winning strategy in the $k$-round existential Ehrenfeucht-Fra\"{i}ss\'{e}~ game from $\mathscr{A}$ to $\mathscr{B}$.
Combining this with Theorem~\ref{EFgamethm} yields the result. \\
(2) In \cite{Hella1996} (see also the exposition in \cite[Theorem 8.13]{Libkin2004})  it is shown that $\mathscr{A} \equiv^{\Lck} \mathscr{B}$ iff Duplicator has a winning strategy in the $k$-round bijection game.
Combining this with Proposition~\ref{bijgameprop} yields the result.
\end{proof}


\subsection{The Pebbling Comonad}




We now state the following result, characterizing the equivalences induced by finite-variable logics $\mathcal{L}^{k}$.
\begin{theorem}
\begin{enumerate}
\item For all structures $\mathscr{A}$ and $\mathscr{B}$: $\mathscr{A}  \equiv^{\ELvk} \mathscr{B} \; \Longleftrightarrow \; \mathscr{A} \rightleftarrows_{k}^{\mathbb{P}} \mathscr{B}$.
\item For all finite structures $\mathscr{A}$ and $\mathscr{B}$: $\mathscr{A} \equiv^{\Lvck} \mathscr{B} \; \Longleftrightarrow \; \mathscr{A} \cong_{k}^{\mathbb{P}} \mathscr{B}$.
\end{enumerate}
\label{thm:mainPebble}
\end{theorem}
\begin{proof}
This follows from Theorems 14 and 18  of \cite{abramsky2017pebbling}.
\end{proof}


\subsection{The Modal Comonad}










Corresponding to the graded modal logic described in Section~\ref{fragssec}, a notion of \emph{graded bisimulation} is given in  \cite{Rijke2000}. This is in turn related to \emph{resource bisimulation} \cite{corradini1999graded}, which has been introduced in the concurrency setting. The two notions are shown to coincide for image-finite Kripke structures in \cite{aceto2010resource}, where it is also shown that they can be presented in a simplified form.
We recall that a Kripke structure $\mathscr{A}$ is image-finite if for all $a \in A$ and $R_{\alpha}$, $R_{\alpha}^{\mathscr{A}}(a) \, := \, \{ a' \mid R^{\As}(a,a') \}$  is finite.

Adapting the results in \cite{aceto2010resource}, we define approximants $\sim^{\mathsf{g}}_k$ for graded bisimulation, by induction on $k \geq 0$. We define  $a \sim^{\mathsf{g}}_{k+1} b$ to hold iff: 
  \begin{enumerate}
    \item for all $P$, $P^{\As}(a)$ iff $P^{\Bs}(b)$
    \item for all $R_{\alpha}$, there is a bijection $\theta : R^{\As}_{\alpha}(a) \cong R^{\Bs}_{\alpha}(b)$ such that, for all $a' \in R^{\As}_{\alpha}(a)$, $a' \sim^{\mathsf{g}}_k \theta(a')$.
  \end{enumerate}
The base case, $a \sim^{\mathsf{g}}_{0} b$, holds whenever only the first condition is satisfied. We can also define a corresponding graded bisimulation game between $(\mathscr{A}, a)$ and $(\mathscr{B}, b)$. 
\begin{itemize}
\item At round 0, the elements $a_0 = a$ and $b_0 = b$ are chosen. Duplicator wins if for all $P$, $P^{\As}(a)$ iff $P^{\Bs}(b)$, otherwise Spoiler wins.
\item  At round $i+1$, Spoiler chooses some $R_{\alpha}$, and Duplicator chooses a bijection $\theta_i : R_{\alpha}^{\As}(a_i) \cong R_{\alpha}^{\Bs}(b_i)$. If there is no such bijection, Spoiler wins. Otherwise, Spoiler then chooses $a_{i+1} \in R^{\As}(a_i)$, and $b_{i+1} := \theta_i(a_{i+1})$. Duplicator wins this round if for all $P$, $P^{\As}(a_{i+1})$ iff $P^{\Bs}(b_{i+1})$, otherwise Spoiler wins.
\end{itemize}
This game is evidently analogous to the bijection game we encountered previously.
\begin{proposition}
\label{gradedbisimprop}
The following are equivalent:
\begin{enumerate}
\item There is a winning strategy for Duplicator in the $k$-round graded bisimulation game between $(\mathscr{A}, a)$ and $(\mathscr{B}, b)$.
\item $a \sim^{\mathsf{g}}_k b$.
\item $(\mathscr{A}, a) \cong_{k}^{\mathbb{M}} (\mathscr{B}, b)$.
\end{enumerate}
\end{proposition}
\begin{proof}
(1) $\Rightarrow$ (2) We prove the statement by induction on $k$. For the base case $k = 0$, by (1), Duplicator has a winning strategy for the $0$-round game, so for all $P$, $P^{\As}(a)$ iff $P^{\Bs}(b)$, hence $a \sim^{\mathsf{g}}_0 b$. Let $k' = k+1$ and assume that Duplicator has a winning strategy in the $k'$-round graded bisimulation game between $(\mathscr{A},a)$ and $(\mathscr{B},b)$. By the winning condition of this strategy, for all $P$, $P^{\As}(a)$ iff $P^{\Bs}(b)$. Moreover, for all $R_{\alpha}$, Duplicator responds to Spoiler choosing $R_{\alpha}$ in the first round of the game with a bijection $\theta_{1}:R_{\alpha}^{\As}(a) \cong R_{\alpha}^{\Bs}(b)$. It follows that for all $a' \in R_{\alpha}^{\As}(a)$, Duplicator has a winning strategy in the $k$-round game from $(\mathscr{A},a')$ to $(\mathscr{B},\theta_{1}(a'))$. This fact together with the inductive hypothesis shows that $a' \sim^{\mathsf{g}}_k \theta_1(a')$. Hence, $a \sim^{\mathsf{g}}_{k'} b$. 

(2) $\Rightarrow$ (3) We prove the statement by induction on $k$. For the base case $k = 0$, we construct the coextension morphisms $f^{*}:\mathbb{M}_{0}(\mathscr{A},a) \rightarrow \mathbb{M}_{0}(\mathscr{B},b)$ by $f^{*}[a] = [b]$ and $g^{*}:\mathbb{M}_{0}(\mathscr{B},b) \rightarrow \mathbb{M}_{0}(\mathscr{A},a)$ by $g^{*}[b] = [a]$. By (2), $a \sim^{\mathsf{g}}_0 b$, so $f$ and $g$ preserve unary relations $P$. Binary relations $R_{\alpha}$ are vacuously preserved since $\mathbb{M}_{0}(\mathscr{A},a)$ and $\mathbb{M}_{0}(\mathscr{B},b)$ only have one element with no self-transitions. Using these definitions, $f^{*} \circ g^{*}[b] = [b]$ and $g^{*} \circ f^{*}[a] = [a]$. Therefore, $f$ and $g$ are inverses in the coKleisli category. Let $k' = k+1$ and assume that $a \sim^{\mathsf{g}}_{k'} b$. We need to define coextension morphisms $f^{*}:\mathbb{M}_{k'}(\mathscr{A},a) \rightarrow \mathbb{M}_{k'}(\mathscr{B},b)$ and $g^{*}:\mathbb{M}_{k'}(\mathscr{B},b) \rightarrow \mathbb{M}_{k'}(\mathscr{A},a)$. Since $a \sim^{\mathsf{g}}_{k'} b$, for every $R_{\alpha}$, there exists a bijection $\theta:R_{\alpha}^{\As}(a) \cong R_{\alpha}^{\Bs}(b)$ such that for every $a' \in R_{\alpha}^{\As}(a)$, $a' \sim^{\mathsf{g}}_k \theta(a')$. For every $a' \in R_{\alpha}^{\As}(a)$, let $b' = \theta(a')$. We can conclude $(\mathscr{A}, a') \cong_{k}^{\mathbb{M}} (\mathscr{B}, b')$ from the inductive hypothesis and $a' \sim^{\mathsf{g}}_k b'$. Hence, there exists a coKleisli isomorphism given by the pair $f^{*}_{a',\alpha}:\mathbb{M}_{k}(\mathscr{A},a') \rightarrow \mathbb{M}_{k}(\mathscr{B},b')$ and  $g^{*}_{b',\alpha}:\mathbb{M}_{k}(\mathscr{B},b') \rightarrow \mathbb{M}_{k}(\mathscr{A},a')$. For every $R_{\alpha}$ and $a' \in R_{\alpha}^{\As}(a)$, we define $f^{*}$ as $f^{*}([a,\alpha]s) = [b,\alpha]f^{*}_{a',\alpha}(s)$ (by $s \in \mathbb{M}_{k}(\mathscr{A},a')$, $a'$ is the first element of $s$). Similarly, for $\theta(a') = b'$ we define $g^{*}$ as $g^{*}([b,\alpha]t) = [a,\alpha]g^{*}_{b',\alpha}(t)$. Finally, let $f^{*}[a] = [b]$ and $g^{*}[b] = [a]$. To verify that $f^{*}$ is a homomorphism, suppose $s \in \mathbb{M}_{k'}(\mathscr{A},a)$. We have two cases, either $s = [a,\alpha]t, s' = [a,\alpha]t'$ for some $t,t' \in \mathbb{M}_{k}(\mathscr{A},a')$ and $a' \in R_{\alpha}^{\As}(a)$ or $s = [a]$ and $s' = [a,\alpha,a']$. Since the interpretations of these relations only depend on the end of the sequence, suppose $P^{\Mkp (\As, a)}([a,\alpha]s)$ and $R^{\Mkp (\As, a)}([a,\alpha]s,[a,\alpha]s')$ for $s,s' \in \mathbb{M}_{k}(\mathscr{A},a')$, then we have the following chains of equivalences resulting from $f^{*}_{a',\alpha}$ being a homomorphism:
\begin{align*}
P^{\Mkp (\As, a)}([a,\alpha]s) &\Leftrightarrow P^{\Mk (\As, a')}(s) \\
&\Leftrightarrow P^{\Mk (\Bs, b')}(f^{*}_{a',\alpha}(s)) \\ 
&\Leftrightarrow P^{\Mkp (\Bs, b)}([b,\alpha]f^{*}_{a',\alpha}(s)) \\ 
&\Leftrightarrow P^{\Mkp (\Bs, b)}(f^{*}([a,\alpha]s)) \\ 
R^{\Mkp (\As, a)}([a,\alpha]s,[a,\alpha]s') &\Leftrightarrow R^{\Mk (\As, a')}(s,s')  \\
&\Leftrightarrow R^{\Mk (\Bs, b')}(f^{*}_{a',\alpha}(s),f^{*}_{a',\alpha}(s'))  \\
&\Leftrightarrow R^{\Mkp (\Bs, b)}(f^{*}([a,\alpha]s),f^{*}([a,\alpha]s'))
\end{align*}
 If $s = [a]$, then by the first condition of $a \sim^{\mathsf{g}}_{k'} b$ and the definitions of $P^{\Mkp (\As, a)}$,$P^{\Mkp (\Bs, b)}$, we obtain the following chain of equivalences: $P^{\Mkp (\As, a)}([a]) \Leftrightarrow P^{\As}(a) \Leftrightarrow P^{\Bs}(b) \Leftrightarrow P^{\Mkp (\Bs, b)}([b]) \Leftrightarrow P^{\Mkp (\Bs, b)}(f^{*}([a]))$. If $s = [a]$ and $s' = [a,\alpha,a']$, then $R_{\alpha}^{\As}(a,a')$. Therefore, $a' \in R_{\alpha}^{\As}(a)$ and $b' = \theta(a')$ where $\theta$ is the bijection $R_{\alpha}^{\As}(a) \cong R_{\alpha}^{\Bs}(b)$. Hence, $R_{\alpha}^{\Bs}(b,b')$ and $R^{\Mkp (\Bs, b)}([b],[b,\alpha,b'])$. A similar argument holds for verifying that $g^{*}$ is a homomorphism. We must show that $f^{*}$ and $g^{*}$ are inverses. To show that $g^{*}(f^{*}(s)) = s$ we consider two cases:
\begin{enumerate}
\item Suppose $s = [a]$, then $g^{*} \circ f^{*}[a] = g^{*}[b] = [a]$ 
\item Suppose $s = [a,\alpha]s'$ for some $R_{\alpha}$, $a' \in R_{\alpha}^{\As}(a)$, and $\theta(a') = b'$:
\begin{align*}
g^{*} \circ f^{*}([a,\alpha]s') &= g^{*}([b,\alpha]f_{a',\alpha}^{*}(s')) \\
&= [a,\alpha](g^{*}_{b',\alpha}(f_{a',\alpha}^{*}(s')) \\
&= [a,\alpha](\mathsf{id}_{\mathbb{M}_{k}(\mathscr{A},a')}(s')) \\
&= [a,\alpha]s'
\end{align*}
\end{enumerate}
Hence, $g^{*} \circ f^{*} = \mathsf{id}_{\mathbb{M}_{k'}(\mathscr{A},a)}$. The proof that $f^{*} \circ g^{*} = \mathsf{id}_{\mathbb{M}_{k'}(\mathscr{B},b)}$ is similar.  Therefore, $f$ is a coKleisli isomorphism, so $(\mathscr{A}, a) \cong_{k'}^{\mathbb{M}} (\mathscr{B}, b)$.

(3) $\Rightarrow$ (1) Assume $f:\mathbb{M}_{k}(\mathscr{A},a) \rightarrow (\mathscr{B},b)$ is a coKleisli isomorphism.  Let $a_0 = a$, $s_0 = [a]$ and $b_0 = b$. The fact that $P^{\As}(a_0) \Leftrightarrow P^{\Bs}(b_0)$ follows from $f$ being a coKleisli isomorphism. At round $i+1$, if Spoiler chooses $R_{\alpha}$, Duplicator chooses the bijection $\theta_{s_i,\alpha}:R_{\alpha}^{\As}(a_i) \cong R_{\alpha}^{\Bs}(b_i)$ defined by $\theta_{s_i,\alpha}(a') = f(s_i[\alpha,a'])$. Spoiler then chooses an $a_{i+1} \in R_{\alpha}^{\As}(a_i)$; define $b_{i+1} = \theta_{s_i,\alpha}(a_{i+1})$ and $s_{i+1} = s_{i} [\alpha,a_{i+1}]$. We must show that this determines a winning move for Duplicator, i.e. for all unary $P \in \sigma$, $P^{\As}(a_{i+1}) \Leftrightarrow P^{\Bs}(b_{i+1})$. By $f$ being a coKleisli isomorphism and the definition of $P^{\Mk (\As, a)}$, we obtain the following chain of equivalences $P^{\As}(a_{i+1}) \Leftrightarrow P^{\Mk (\As, a)}(s_i[\alpha,a_{i+1}]) \Leftrightarrow P^{\Bs}(f(s_i[\alpha,a_{i+1}])) \Leftrightarrow P^{\Bs}(\theta_{s_i,\alpha}(a_{i+1})) \Leftrightarrow P^{\Bs}(b_{i+1})$.
\end{proof}

\begin{theorem}
\begin{enumerate}
\item For all Kripke structures $(\mathscr{A}, a)$ and $(\mathscr{B}, b)$: 
\[ (\mathscr{A}, a)  \equiv^{\exists\mathcal{M}_{k}} (\mathscr{B}, b) \;\; \Longleftrightarrow \;\; (\mathscr{A}, a) \rightleftarrows_{k}^{\mathbb{M}} (\mathscr{B},b). \]
\item For all image-finite Kripke structures $(\mathscr{A},a)$ and $(\mathscr{B},b)$: 
\[ (\mathscr{A},a) \equiv^{\MLck} (\mathscr{B},b) \;\; \Longleftrightarrow \;\; (\mathscr{A},a) \cong_{k}^{\mathbb{M}} (\mathscr{B},b). \]
\end{enumerate}
\end{theorem}
\begin{proof}
(1) It is a standard result (see e.g.~\cite[Theorem 2.78]{blackburn2002modal}) that $(\mathscr{A},a)  \equiv^{\exists\mathcal{M}_{k}} (\mathscr{B},b)$ iff $a \preceq_k b$ and $b \preceq_k a$.
Combining this with Proposition~\ref{simthm} yields the result. \\
(2) It is shown in \cite{Rijke2000} and \cite[Proposition 4.11]{aceto2010resource} that $(\mathscr{A},a) \equiv^{\MLck} (\mathscr{B},b)$ iff $a \sim^{\mathsf{g}}_k b$.
Combining this with Proposition~\ref{gradedbisimprop} yields the result.
\end{proof}

\section{Coalgebras and combinatorial parameters}
\label{coalgsec}
Another fundamental aspect of comonads is that they have an associated notion of \emph{coalgebra}. A coalgebra for a comonad $(G, \varepsilon, \delta)$ is a morphism $\alpha : A \to G A$ such that the following diagrams commute:
\begin{center}
\begin{tikzcd}
A  \ar[r, "\alpha"] \ar[d, "\alpha"']
& G A \ar[d,  "\delta_{A}"] \\
G A  \ar[r, "G \alpha"] 
& G^2 A
\end{tikzcd}  
$\qquad \qquad$
\begin{tikzcd}
A \ar[r, "\alpha"] \ar[rd, "\mathsf{id}_A"']
& G A \ar[d, "\epsilon_{\As}"] \\
& A
\end{tikzcd}
\end{center}
Given $G$-coalgebras $\alpha : A \to GA$ and $\beta : B \to GB$, a coalgebra morphism from $\alpha$ to $\beta$ is a morphism $h : A \to B$ such that the following diagram commutes:
\begin{center}
\begin{tikzcd}
A \ar[r, "\alpha"] \ar[d, "h"']
& GA \ar[d, "Gh"] \\
B \ar[r, "\beta"'] & GB
\end{tikzcd}
\end{center}
This gives a category of coalgebras and coalgebra morphisms, denoted by $\mathsf{EM}(G)$, the \textit{Eilenberg-Moore category} of $G$.

Our use of indexed comonads $\mathbb{C}_{k}$ opens up a new kind of question for coalgebras. Given a structure $\mathscr{A}$, we can ask: what is the least value of $k$ such that a $\mathbb{C}_{k}$-coalgebra exists on $\mathscr{A}$?  We call this the \emph{coalgebra number} of $\mathscr{A}$. We shall find that for each of our comonads, the coalgebra number is a significant combinatorial parameter of the structure.

\subsection{The Ehrenfeucht-Fra\"{i}ss\'{e}~ comonad and tree-depth}


A graph is $G = (V, {\frown})$, where $V$ is the set of vertices, and $\frown$ is the adjacency relation, which is symmetric and irreflexive.
A \emph{forest cover} for $G$ is a forest $(F, {\leq})$ such that $V \subseteq F$, and if $v \frown v'$, then $v {\uparrow} v'$.
The \emph{tree-depth} $\mathsf{td}(G)$ is defined to be $\min_{F} \mathsf{ht}(F)$, where $F$ ranges over forest covers of $G$.\footnote{We formulate this notion in order-theoretic rather than graph-theoretic language, but it is equivalent to the definition in \cite{nevsetvril2006tree}.} It is clear that we can restrict  to forest covers of the form $(V, {\leq})$, since given a forest cover $(F, {\leq})$ of $G = (V, {\frown})$, $(V, \, {\leq} \cap V^2)$ is also a forest cover of $G$, and $\mathsf{ht}(V) \leq \mathsf{ht}(F)$. Henceforth, by forest covers of $G$ we shall mean those with universe $V$.

Given a $\sigma$-structure $\mathscr{A}$, the \emph{Gaifman graph} $\mathcal{G}(\mathscr{A})$ is $(A, \frown)$, where $a \frown a'$ iff for some relation $R \in \sigma$, for some $(a_1, \ldots , a_n) \in R^{\As}$, $a = a_i$, $a' = a_j$, $a \neq a'$. The \emph{tree-depth of $\mathscr{A}$} is $\mathsf{td}(\mathcal{G}(\mathscr{A}))$.

\begin{theorem}
\label{tdth}
Let $\mathscr{A}$ be a finite $\sigma$-structure, and $k>0$. There is a bijective correspondence between
\begin{enumerate}
\item $\mathbb{E}_{k}$-coalgebras $\alpha : \mathscr{A} \rightarrow \mathbb{E}_{k} \mathscr{A}$.
\item Forest covers of $\mathcal{G}(\mathscr{A})$ of height $\leq k$.
\end{enumerate}
\end{theorem}
\begin{proof}
        Suppose that $\alpha : \mathscr{A} \to \mathbb{E}_{k} \mathscr{A}$ is a coalgebra. For $a \in A$, let $\alpha(a) = [a_1, \ldots , a_j]$, then the action of the comultiplication $\delta_{\mathscr{A}}$ on $\alpha(a)$ is $[[a_1],[a_1,a_2],\dots,[a_1,\ldots,a_j]]$. Hence, the first coalgebra equation states that $\alpha(a_i) = [a_1, \ldots , a_i]$, $1 \leq i \leq j$. The second states that $a_j = a$. Thus $\alpha : A \to A^{\leq k}$ is an injective map whose image is a prefix-closed subset of $A^{\leq k}$. Defining $a \leq a'$ iff $\alpha(a) \sqsubseteq \alpha(a')$ yields a forest order on $A$, of height $\leq k$. If $a \frown a'$ in $\mathcal{G}(\mathscr{A})$, then for some $a_1, \ldots , a_n$ with $a = a_i$, $a' = a_j$, we have $R^{\As}(a_1, \ldots , a_n)$. Since $\alpha$ is a homomorphism, we must have $R^{\mathbb{E}_{k} \mathscr{A}}(\alpha(a_1), \ldots , \alpha(a_n))$. By the pairwise comparability condition in the definition of $R^{\mathbb{E}_{k} \mathscr{A}}$, $\alpha(a_i) {\uparrow} \alpha(a_j)$, and so $a_i {\uparrow} a_j$. Thus $(A, {\leq})$ is a forest cover of $\mathcal{G}(\mathscr{A})$, of height $\leq k$.

Conversely, given such a forest cover $(A, {\leq})$, for each $a \in A$, its predecessors form a chain $a_1 < \cdots < a_j$, with $a_j = a$, and $j \leq k$.
We define $\alpha(a) = [a_1, \ldots , a_j]$, which yields a map $\alpha : A \to A^{\leq k}$, which evidently satisfies the coalgebra equations. If $R^{\As}(a_1, \ldots , a_n)$, then since $(A, {\leq})$ is a forest cover, we must have $a_i {\uparrow} a_j$ for all $i, j$, and hence $\alpha(a_i) {\uparrow} \alpha(a_j)$.
Thus $R^{\mathbb{E}_{k} \mathscr{A}}(\alpha(a_1), \ldots , \alpha(a_n))$, and $\alpha$ is a homomorphism.
\end{proof}

\noindent We write $\kappa^{\mathbb{E}}(\mathscr{A})$ for the coalgebra number of $\mathscr{A}$ with respect to the Ehrenfeucht-Fra\"{i}ss\'{e}~ comonad.

\begin{theorem}
For all finite structures $\mathscr{A}$: $\mathsf{td}(\mathscr{A}) \, = \, \kappa^{\mathbb{E}}(\mathscr{A})$.
\end{theorem}
\begin{proof}
By Theorem~\ref{tdth}, for all  $k>0$, $\mathsf{td}(\mathscr{A}) \leq k$ iff $\kappa^{\mathbb{E}}(\mathscr{A}) \leq k$.
\end{proof}

\subsection{The pebbling comonad and tree-width}

We review the notions of tree decompositions and tree-width. A tree $(T, {\leq})$ is a forest with a least element (the root). A tree is easily seen to be a meet-semilattice: every pair of elements $x, x'$ has a greatest lower bound $x \wedge x'$ (the greatest common ancestor). The path from $x$ to $x'$ is  the set
$\mathsf{path}(x, x') := [x \wedge x', x] \cup [x \wedge x', x']$, where we use interval notation: $[y, y'] := \{ z \in T \mid y \leq z \leq y' \}$. 

A \textit{tree-decomposition} of a graph $G = (V, {\frown})$ is a tree $(T, {\leq})$ together with a labelling function $\lambda : T \rightarrow \mathscr{P}(V)$ satisfying the following conditions: 
\begin{itemize}
\item (TD1) for all $v \in V$, for some $x \in T$, $v \in \lambda(x)$; 
\item (TD2) if $v \frown v'$, then for some $x \in T$, $\{ v, v' \} \subseteq \lambda(x)$; 
\item (TD3) if $v \in \lambda(x) \cap \lambda(x')$, then for all $y \in \mathsf{path}(x, x')$, $v \in \lambda(y)$. 
\end{itemize}
The \emph{width} of a tree decomposition is given by $\max_{x \in T} |\lambda(x)| -1$. We define the \textit{tree-width} $\mathsf{tw}(G)$ of a graph $G$ as $\min_{T} \mathsf{width}(T)$, where $T$ ranges over tree decompositions of $G$.
\begin{remark}\label{ordernicerem}
 We say that a tree decomposition is \emph{orderly} if it has the following property: for all $x \in T$, there is at most one $v \in \lambda(x)$ such that for all $x' < x$, $v \not\in \lambda(x')$.
By  \cite[Definition~13.1.4]{kloks1994treewidth}, \emph{nice} tree decompositions are orderly. Moreover, by  \cite[Lemma 13.1.2]{kloks1994treewidth}, the existence of a tree decomposition implies the existence of a nice tree decomposition of the same width. Hence, the existence of a tree decomposition implies the existence of an orderly one of the same width.
This observation will prove useful for our next result.
\end{remark}

We shall now give an alternative formulation of tree-width which will provide a useful bridge to the coalgebraic characterization. It is also interesting in its own right: it clarifies the relationship between tree-width and tree-depth, and shows how pebbling arises naturally in connection with tree-width.

A \textit{$k$-pebble forest cover} for a graph $G = (V, {\frown})$ is a forest cover $(V, {\leq})$ together with a pebbling function $p : V \to \mathbf{k}$ such that, if $v \frown v'$ with $v \leq v'$, then for all $w \in (v,v']$, $p(v) \neq p(w)$.

The following result is implicit in \cite{abramsky2017pebbling}, but it seems worthwhile to set it out more clearly.

\begin{theorem}
Let $G$ be a finite graph. The following are equivalent:
\begin{enumerate}
\item $G$ has a tree decomposition of width $< k$.
\item $G$ has a $k$-pebble forest cover.
\end{enumerate}
\end{theorem}
\begin{proof}
$(1) \Rightarrow (2)$. Assume that $G = (V, \frown)$ has a tree decomposition $(T, {\leq}, \lambda)$ of width $< k$. 
As explained in Remark~\ref{ordernicerem},  without loss of generality we can assume that the given tree decomposition is orderly.

For any $v \in V$, the set of $x \in T$ such that $v \in \lambda(x)$ is non-empty by (TD1), and closed under meets by (TD3). Since $T$ is a tree, this implies that this set has a least element $\tau(v)$. This defines a function $\tau : V \to T$. The fact that the tree decomposition is orderly implies that $\tau$ is injective. We can define an order on $V$ by $v \leq v'$ iff $\tau(v) \leq \tau(v')$. This is isomorphic to a sub-poset of $T$, and hence is a forest order.

We define $p : V \to \mathbf{k}$ by induction on this order. Assuming $p(v')$ is defined for all $v' < v$, we consider $\tau(v)$. Since the tree decomposition is orderly, this means in particular that $p(v')$ is defined for all $v' \in S  :=  \lambda(\tau(v)) \setminus \{v\}$. Since the decomposition is of width $< k$, we must have $|S| < k$. We set $p(v) := \min (\mathbf{k} \setminus \{ p(v') \mid v' \in S \})$.

To verify that $(V, {\leq})$ is a forest cover, suppose that $v \frown v'$. By (TD2), for some $x \in T$, $\{ v, v' \} \subseteq \lambda(x)$. We have $\tau(v) \leq x \geq \tau(v')$, and since $T$ is a tree, we must have $\tau(v) \, {\uparrow} \, \tau(v')$, whence $v \, {\uparrow} \, v'$.

Finally, we must verify the condition on the pebbling function $p$. Suppose that $v \frown v'$, and $v < w \leq v'$. 
Since $v \frown v'$, for some $x$, $\{ v, v' \} \subseteq \lambda(x)$. But then $\tau(v) < \tau(w) \leq \tau(v') \leq x$. Since $v \in \lambda(\tau(v)) \cap \lambda(x)$, by (TD3), $v \in \lambda(\tau(w))$. By construction of the pebbling function, this implies $p(v) \neq p(w)$.

$(2) \Rightarrow (1)$. Suppose that $(V, {\leq}, p)$ is a $k$-pebble forest cover of $G$. We define a tree $T = V_{\bot}$ by adjoining a least element $\bot$  to $V$. We say that $v'$ is an active predecessor of $v$ if $v' \leq v$, and for all $w \in (v', v]$, $p(v') \neq p(w)$. We define the labelling function by setting $\lambda(v)$ to be the set of active predecessors of $v$; $\lambda(\bot) := \varnothing$. Since $p |_{\lambda(v)}$ is injective, $|\lambda(v)| \leq k$.

We verify the tree decomposition conditions. (TD1) holds, since $v \in \lambda(v)$. (TD2) If $v \frown v'$, then $v {\uparrow} v'$. Suppose $v \leq v'$.
Then $v$ is an active predecessor of $v'$, and $\{ v, v' \} \subseteq \lambda(v')$.
(TD3) Suppose $v \in \lambda(v_1) \cap \lambda(v_2)$. Then $v$ is an active predecessor of both $v_1$ and $v_2$. This implies that for all $w \in \mathsf{path}(v_1,v_2)$, $v$ is an active predecessor of $w$, and hence $v \in \lambda(w)$.
\end{proof}

\begin{theorem}
\label{kpfcth}
Let $\mathscr{A}$ be a finite $\sigma$-structure. There is a bijective correspondence between:
\begin{enumerate}
\item $\mathbb{P}_{k}$-coalgebras $\alpha : \mathscr{A} \to \mathbb{P}_{k} \mathscr{A}$
\item $k$-pebble forest covers of $\mathcal{G}(\mathscr{A})$.
\end{enumerate}
\end{theorem}
\begin{proof}
See \cite[Theorem 6]{abramsky2017pebbling}.
\end{proof}
\noindent We write $\kappa^{\mathbb{P}}(\mathscr{A})$ for the coalgebra number of $\mathscr{A}$ with respect to the pebbling comonad.

\begin{theorem}
For all finite structures $\mathscr{A}$: $\mathsf{tw}(\mathscr{A}) \, = \, \kappa^{\mathbb{P}}(\mathscr{A}) - 1$.
\end{theorem}

\subsection{The modal comonad and synchronization tree height}
Let $\mathscr{A}$ be a Kripke structure. It will be convenient to write labelled transitions $a \labar{\alpha} a'$ for $R_{\alpha}(a, a')$.
Given $a \in A$, the submodel generated by $a$, denoted $\mathscr{S}_{a}$, is obtained by restricting the universe to the set $S_a$ of $a'$ such that there is a path $a \labar{\alpha_1} \cdots \labar{\alpha_k} a'$. This submodel forms a \emph{synchronization tree} \cite{milner1980calculus} if for all $a'$, there is a unique such path. The height of such a tree is the maximum length of any path from the root $a$.

\begin{proposition}\label{modalcoalgprop}
Let $\mathscr{A}$ be a Kripke structure, with $a \in A$. The following are equivalent:
\begin{enumerate}
\item There is a coalgebra $\gamma : (\mathscr{S}_{a},a) \to \mathbb{M}_{k} (\mathscr{S}_{a}, a)$.
\item $\mathscr{S}_{a}$ is a synchronization tree of height $\leq k$.
\end{enumerate}
\label{prop:modalCoalg}
\end{proposition}
\begin{proof}
        Suppose there is a coalgebra $\gamma:(\mathscr{S}_{a},a) \rightarrow \mathbb{M}_{k}(\mathscr{S}_{a},a)$. For $a' \in S_a$, let $\gamma(a') = [a_0,\alpha_{1},a_{1},\dots,\alpha_{j},a_j]$ where $a_0 = a$. The first coalgebra equation states that $\gamma(a_{i}) = [a_0,\alpha_{1}\dots,\alpha_{i},a_{i}]$ for $0 \leq i \leq j$. The second coalgebra equation states that $a_j = a'$. Therefore, $\gamma$ is injective. By injectivity and since $
        \gamma$ is a homomorphism and thus preserves transitions, the path of transitions determined by $\gamma(a')$, i.e. $a \labar{\alpha_{1}} a_1 \cdots \labar{\alpha_{j}} a'$ is unique. Hence, every $a' \in S_a$ has a unique path from $a$, so the submodel generated by $a$ is a synchronization tree. Since $j \leq k$, the height of the tree is at most $k$.

        Conversely, suppose $\mathscr{S}_{a}$ is a synchronization tree of height $\leq k$, then for every $a'$ in this submodel, there exists a unique path of transitions $a \labar{\alpha_{1}} \cdots \labar{\alpha_{j}} a'$. We can define the morphism $\gamma:(\mathscr{S}_{a},a) \rightarrow \mathbb{M}_{k}(\mathscr{S}_{a},a)$ as $\gamma(a') = [a,\alpha_{1},a_1,\dots,\alpha_{j},a']$. Since the path of transitions is unique, the first coalgebra equation is satisfied. Moreover, since the last element of $\gamma(a')$ is $a'$, the second coalgebra equation is satisfied.  
\end{proof}

\noindent We define the modal depth $\mathsf{md}(\mathscr{A}, a) = k$ if the submodel $\mathscr{S}_{a}$ generated by $a$ is a synchronization tree of height $k$.

\begin{theorem}
Let $\mathscr{A}$ be a Kripke structure, and $a \in A$ be such that the submodel generated by $a$ is a synchronization tree of finite height.
Then $\mathsf{md}(\mathscr{A}, a) = \kappa^{\mathbb{M}}(\mathscr{A}, a)$.
\end{theorem}

Note the conditional nature of this result, which contrasts with those for the other comonads. The modal comonad is defined in such a way that the universe $\mathbb{M}_{k} (A, a)$ reflects information about the possible transitions. Thus having a coalgebra at all, regardless of the value of the resource parameter, is a strong constraint on the structure of the transition system.

\section{Characterization of Rossman-type equivalences}
\label{sec:Rossman}

As a simple application of the results of the previous section, we show how they yield characterizations of some approximation preorders on structures, and their associated equivalences.

We begin with a completely general result. Given objects $A$, $B$ of a category $\mathcal{C}$, we shall use the notation $A \to B$ to mean that there exists a morphism from $A$ to $B$ in $\mathcal{C}$.

\begin{proposition}
\label{coalgapproxprop}
Let $G$ be a comonad on a category $\mathcal{C}$. For all objects $A$, $B$ of $\mathcal{C}$, the following are equivalent: 
\begin{enumerate}
    \item $GA \to B$
    \item For all $G$-coalgebras $\alpha : C \to GC$, $C \to A \; \Rightarrow \; C \to B$
\end{enumerate}
\end{proposition}
\begin{proof}
Suppose $f : GA \to B$, $\alpha : C \to GC$ is a coalgebra, and $g : C \to A$. Then
$f \circ Gg \circ \alpha : C \to B$.

Conversely, suppose that for all coalgebras $\alpha : C \to GC$, $C \to A$ implies $C \to B$. Then since $\delta_A : GA \to GGA$ is a coalgebra, and $\varepsilon_A : GA \to A$, we conclude that $GA \to B$.
\end{proof}

Rossman defined the following preorder in \cite{Rossman2008}: $\mathscr{A} \to_{k}^{\mathbb{E}} \mathscr{B}$ iff for all structures $\mathscr{C}$ with $\mathsf{td}(\mathscr{C}) \leq k$, $\mathscr{C} \to \mathscr{A} \; \Rightarrow \; \mathscr{C} \to \mathscr{B}$. The associated equivalence $\rightleftarrows_{k}$ plays a major role in  \cite{Rossman2008}, as a resource-bounded approximation to homomorphism equivalence.

We can similarly define $\mathscr{A} \to_{k}^{\mathbb{P}} \mathscr{B}$ in terms of treewidth: $\mathscr{A} \to_{k}^{\mathbb{P}} \mathscr{B}$ iff for all structures $\mathscr{C}$ with $\mathsf{tw}(\mathscr{C}) < k$, $\mathscr{C} \to \mathscr{A} \; \Rightarrow \; \mathscr{C} \to \mathscr{B}$ \footnote{We use strict inequality, in this case, due to the fact that tree-width is one less than the $\mathbb{P}_{k}$-coalgebra number.}.

Finally, we can define $(\mathscr{A}, a) \to_{k}^{\mathbb{M}} (\mathscr{B}, b)$ iff for all synchronization trees $(\mathscr{C}, c)$ with $\mathsf{md}(\mathscr{C}, c) \leq k$, $(\mathscr{C}, c) \to (\mathscr{A}, a) \; \Rightarrow \; (\mathscr{C}, c) \to (\mathscr{B}, b)$.


As an immediate consequence of Proposition~\ref{coalgapproxprop} and the results of the previous section, we obtain:

\begin{proposition}
For all structures $A$, $B$ in $\mathcal{R}(\sg)$:
\begin{enumerate}
\item $\mathscr{A} \to_{k}^{\mathbb{E}} \mathscr{B} \;\; \Longleftrightarrow \;\; \mathbb{E}_{k} \mathscr{A} \to \mathscr{B}$
\item $\mathscr{A} \to_{k}^{\mathbb{P}} \mathscr{B} \;\; \Longleftrightarrow \;\; \mathbb{P}_{k} \mathscr{A} \to \mathscr{B}$
\item $(\mathscr{A}, a) \to_{k}^{\mathbb{M}} (\mathscr{B}, b) \;\; \Longleftrightarrow \;\; \mathbb{M}_{k} (\mathscr{A}, a) \to (\mathscr{B}, b)$ (when $\mathscr{A},\mathscr{B}$ are Kripke structures)
\end{enumerate}

\end{proposition}


\section{Coalgebras and conjunctive queries}
\label{sec:conjunctive}

We shall now connect coalgebras with logic. We will show that they are closely related to \emph{conjunctive queries}.
We shall focus on boolean conjunctive queries, \textit{i.e.}~ closed formulas built from atomic formulae using only conjunction and existential quantification. We allow the empty conjunction $\top$.
Given a finite structure $\mathscr{A}$ with universe $A = \{ a_1, \ldots , a_n\}$, the canonical conjunctive query for $\mathscr{A}$ is a formula $Q^{\As} := \exists v_1 , \ldots , v_n. \bigwedge \{ R(\vec{v}) \mid R \in \sigma, \vec{a} \in R^{\As}\}$.
Here the correspondence $v_i \leftrightarrow a_i$ uses the  linear ordering on the universe implicitly given by the enumeration.

The key property of the canonical conjunctive query is the following \cite{chandra1977optimal}:
\begin{theorem}
For all finite structures $\mathscr{A}$, $\mathscr{B}$, $\mathscr{A} \to \mathscr{B}$ iff $\mathscr{B} \models Q^{\As}$.
\end{theorem}

We now consider a number of rewrite rules on formulas:
\[ \begin{array}{ll}
(R1) & \mbox{Associative-Commutative-Identity rewriting of conjunctions} \\
(R2) & \exists v.\, (\varphi \, \wedge \, \psi) \, \rightsquigarrow \, (\exists v.\, \varphi) \, \wedge \, \psi\;\;(v \not\in \mathsf{FV}(\psi)) \\
(R3) & \exists v. \, \exists w. \, \varphi \, \rightsquigarrow \, \exists w. \, \exists v. \, \varphi \\
(R4) & \exists v.\, \varphi \, \rightsquigarrow \, \exists w.\, \phi[w/v]
\end{array}
\]
Note that $(R1)$ amounts to taking formulas as a commutative monoid under the binary operation of conjunction, with $\top$ as the identity.

\subsection{The Ehrenfeucht-Fra\"{i}ss\'{e}~ case}
As usual, we can use a rule $\theta \, \rightsquigarrow \, \chi$ to rewrite a formula $C[\theta]$ to $C[\chi]$. We write $\varphi \twoheadrightarrow^{\mathbb{E}} \psi$ if $\varphi$ can be rewritten to 
$\psi$ in some finite number of steps using rules (R1)--(R3).

\begin{theorem}
Let $\mathscr{A}$ be a finite structure, and $Q^{\As}$ its canonical conjunctive query. The following are equivalent:
\begin{enumerate}
\item $\mathscr{A}$ has a coalgebra $\mathscr{A} \to \mathbb{E}_{k} \mathscr{A}$
\item $Q^{\As} \twoheadrightarrow^{\mathbb{E}} \varphi$, where $\varphi$ has quantifier rank $\leq k$.
\end{enumerate}
\label{thm:efRewrite}
\end{theorem}
\begin{proof}
Firstly, we note that given any conjunctive query $\chi$ satisfied by $\mathscr{A}$, with variable $v_i$ interpreted by $a_i$, there is a corresponding forest order on $A$, with $a_i < a_j$ if $v_j$ occurs in the scope of $\exists v_i$ in $\chi$.

Now assuming (2), there is a  forest order on $A$ corresponding to $\varphi$.  Since the quantifier rank of $\varphi$ is $\leq k$, so is the height of this order.
Moreover, the order induced by $Q^{\As}$ is obviously a forest cover of the Gaifman graph $\mathcal{G}(A)$, and application of the rules (R1)--(R3) preserves that property of the orders induced by conjunctive queries.  In particular, in applying (R2), the free variable condition implies that no elements which become incomparable in the corresponding order are adjacent in the Gaifman graph. By induction on the length of the rewrite sequence, we conclude that the order induced by $\varphi$ is a forest cover, and hence, by Theorem~\ref{tdth}, determines an $\mathbb{E}_{k}$-coalgebra on $\mathscr{A}$.

For the converse, we argue by induction on the cardinality of $A$, or equivalently on the number $n$ of variables in $Q^{\As}$. 
We prove the following statement $\Phi(n)$ by induction on $n$:

For all finite relational vocabularies $\sigma$, and for all $\sigma$-structures $\mathscr{A}$ of cardinality $n$, if $\mathscr{A}$ has a forest cover of height $k$, then $Q^{\As} \twoheadrightarrow^{\mathbb{E}} \varphi$, where the quantifier rank of $\varphi$ is $\leq k$.

The base case $n=1$ is trivial. Now consider 
\[Q^{\As} = \exists v_1 , \ldots , v_j. \exists v_{j+1}, \ldots , v_n . \bigwedge_{i \in I} R_i(\vec{v}_i) . \]
We assume that the variables are in an order which linearizes the given forest order on $A$. In particular, $v_{j+1}, \ldots , v_n$ are the leaves (maximal elements) of maximum height $k$ in the forest order. Note that if $v$ and $v'$ are leaves, they cannot be adjacent in the Gaifman graph of $\mathscr{A}$, since this would violate the forest cover condition. Hence we can partition $I$ as $I = I_{j+1} \sqcup \cdots I_n \sqcup L$, where $I_p$ is the set of indices labelling atomic formulas in which $v_p$ occurs, $p = j+1, \ldots , n$. Using (R1) and (R2), we can rewrite $Q^{\As}$ to 
\[ \varphi \, := \, \exists v_1 , \ldots , v_j. \, (\bigwedge_{p=j+1}^n \chi_p) \wedge \theta \]
where $\chi_p := \exists v_p. \, \bigwedge_{j \in I_p} R_j(\vec{v}_j)$ and $\theta := \bigwedge_{l \in L} R_l(\vec{v}_l)$.
We now define 
\[ \varphi' := \exists v_1 , \ldots , v_j. (\bigwedge_{p=j+1}^n R_p(\vec{w})) \wedge \theta , \]
where $R_p$ is a new relation symbol, and $\vec{w}_p$ lists all the variables other than $v_p$ occurring in $\chi_p$. 
This formula $\varphi'$ is the canonical conjunctive query for a new structure $\mathscr{A}'$, of cardinality $j<n$. There are many new adjacencies in the Gaifman graph of $\mathscr{A}'$ compared with that for $\mathscr{A}$. However, note that, since (the element of $A$ labelled by) each $v_p$ was maximal in the given forest cover of $\mathscr{A}$, the set of variables adjacent to $v_p$ must be linearly ordered in that forest cover. Hence the restriction of the forest cover on $\mathscr{A}$ to $\{ v_1, \ldots , v_j \}$ is a forest cover on $\mathscr{A}'$, of height $k-1$.
Our induction hypothesis can be applied to this forest cover on $\mathscr{A}'$ as a $\sigma'$-structure, where $\sigma' = \sigma \cup \{R_p \mid p = j+1, \ldots , n \}$, yielding a rewrite $\varphi' \twoheadrightarrow^{\mathbb{E}} \psi'$, where $\psi'$ has quantifier rank $ m < k$.
We can perform the same rewrite steps to obtain $\varphi \twoheadrightarrow^{\mathbb{E}} \psi$, 
where $\psi$ results from $\psi'$ by replacing each $R_p(\vec{w})$ by $\chi_p$. We have $Q^{\As} \twoheadrightarrow^{\mathbb{E}} \varphi \twoheadrightarrow^{\mathbb{E}} \psi$. Moreover, the quantifier rank of $\psi$ is $m+1 \leq k$, as required.
\end{proof}
We can think of (R1)--(R3) as encoding a non-deterministic algorithm for computing the minimum quantifier rank for $Q^{\As}$ (and hence the tree-depth of $\mathscr{A}$). We use (R3) to guess an order on the variables, and then apply (R1) and (R2) to the quantifier prefix from the inside out. The above argument shows that this algorithm does, for some choice of order, find the minimum quantifier rank.

\subsection{The pebbling case}
We can give a very similar analysis for the pebbling case. We define $\varphi \twoheadrightarrow^{\mathbb{P}} \psi$ if $\varphi$ can be rewritten to 
$\psi$ in some finite number of steps using rules (R1), (R2), and (R4). This set of rules is widely used in query optimization \cite{dalmau2002constraint,ullman1989database}.
\begin{theorem}
Let $\mathscr{A}$ be a finite structure, and $Q^{\As}$ its canonical conjunctive query. The following are equivalent:
\begin{enumerate}
\item $\mathscr{A}$ has a coalgebra $\mathscr{A} \to \mathbb{P}_{k} \mathscr{A}$
\item $Q^{\As} \twoheadrightarrow^{\mathbb{P}} \varphi$, where $\varphi$ has number of variables $\leq k$.
\end{enumerate}
\end{theorem}
\begin{proof}
This is the statement of Theorem 27 in \cite{abramsky2017pebbling}.
\end{proof}

\subsection{The modal case}
For the modal case, we move to the category of pointed modal structures $\mathcal{R}_{\star}(\sg)$. 
Given a pointed modal structure $(\mathscr{A}, a)$, there is a natural notion of \emph{canonical modal conjunctive query} from $a$, defined recursively as follows:
\[ MQ^{(\As,a)} \; \equiv \; \bigwedge \{ p \mid \mathscr{A}, a \models p \} \wedge \bigwedge \{ \Diamond_{\alpha} MQ^{(\As,b)} \mid R_{\alpha}(a,b) \} 
\]
Note that, even if $A$ is finite, this definition may not be well-founded if there are transition cycles. However, if the submodel $\mathscr{S}_{a}$ generated by $a$ is finite and acyclic, then this definition yields a well-defined finite modal formula.
In particular, this holds if $\mathscr{S}_{a}$ is a finite synchronization tree.\footnote{Note that if $\mathscr{S}_{a}$ is finite and acyclic, it is bisimilar to a finite synchronization tree \cite[Proposition 2.15]{blackburn2002modal}.}
This may be regarded as the ``existential-conjunctive half'' of a modal form of Scott sentence for the structure \cite{scott2014logic}.
Under the modal translation, this formula will map into a first-order formula $\psi(x)$ in one free variable, whose quantifier rank matches the modal depth of the formula. 

To relate this to the rewrite rules,
we  modify the standard canonical conjunctive query $Q^{\Sa}$ to have one free variable; we write this as $Q^{a}(x)$. Note that we can rearrange the block of existential quantifiers in $Q^{\Sa}$ using rule (R3) so that $\exists x$ is the leftmost existential quantifier. Hence, we could write $Q^{\Sa}$ as $\exists x Q^{a}(x)$, so $(\mathscr{S}_{a},a) \vDash Q^{a}(x) \Longleftrightarrow \mathscr{S}_{a} \vDash Q^{\Sa}$. 

We write $\varphi(x) \twoheadrightarrow^{\mathbb{M}} \psi(x)$ if $\varphi(x)$ can be rewritten to $\psi(x)$ in some finite number of steps using rules (R1)--(R4).

\begin{theorem}
Let $(\mathscr{A}, a)$ be a finite modal structure. 
If $\mathscr{S}_{a}$ has a coalgebra 
$(\mathscr{S}_{a},a) \rightarrow \mathbb{M}_{k}(\mathscr{S}_{a},a)$,
then 
$Q^{a}(x) \twoheadrightarrow^{\mathbb{M}} \llbracket MQ^{(\As,a)} \rrbracket$, where $MQ^{(\As,a)}$ is the canonical modal conjunctive query for $(\mathscr{A}, a)$, with modal depth $k$.
\end{theorem}
\begin{proof}
By Proposition \ref{prop:modalCoalg}, it suffices to show that: for every $a \in \mathscr{A}$, if $\mathscr{S}_{a}$ is a synchronization tree of height $\leq k$, then $Q^{a}(x) \twoheadrightarrow^{\mathbb{M}} \phi(x)$ where $\phi(x) = \llbracket MQ^{(\As,a)} \rrbracket$ and $MQ^{(\As,a)}$ has modal depth $\leq k$. We prove this statement by induction on $k$. For the base case $k = 0$, we can conclude that $\mathscr{S}_{a}$ is such that $R_{\alpha}^{\mathscr{S}_{a}}$ is empty for every $R_{\alpha} \in \sigma$. Therefore, $Q^{a}(x)$ is a conjunction of unary predicates $P \in \sigma$ that $a$ satisfies. We can take $\phi(x) = Q^{a}(x)$ and $MQ^{(\As,a)}$ would be a conjunction of the propositional variables corresponding to the unary relations satisfied by $a$. For the inductive step, suppose $\mathscr{S}_{a}$ is a synchronization tree of height $\leq k+1$, then we can successively apply (R1) and (R2), to obtain a conjunction of formulas of two forms: 
\begin{enumerate}
\item $\exists y (R_{\beta}(x,y) \wedge Q^{b}(y))$, for some $y$ corresponding to $b$ such that $R_{\beta}^{\As}(a,b)$.
\item $P(x)$ for some unary $P \in \sigma$ such that $P^{\As}(a)$.
\end{enumerate}
Applying the inductive hypothesis to $\mathscr{S}_{b}$, and replacing $Q^{b}(y)$ with its rewrite $\chi(y) = \llbracket MQ^{(\As,b)} \rrbracket$ of modal depth $\leq k$ yields the desired $\phi(x)$.

\end{proof}

Note that, by our previous discussion, the converse to this result holds ``up to bisimulation''.
That is, if $(\mathscr{A},a)$ has a canonical modal conjunctive query $MQ^{(\As,a)}$ of modal depth $k$, then $\mathscr{S}_{a}$ is bisimilar to a synchronization tree $(T,a)$ of depth $k$. By Proposition~\ref{modalcoalgprop}, $T$ has a coalgebra 
$(T,a) \rightarrow \mathbb{M}_{k}(T,a)$.

\section{Characterization of Eilenberg-Moore categories}
\label{sec:adjoints}
Monads and comonads are closely related to \emph{adjunctions}. We recall that an adjunction
\[ \begin{tikzcd}
\mathcal{C} \arrow[r, bend left=25, ""{name=U, below}, "L"]
\arrow[r, leftarrow, bend right=25, ""{name=D}, "R"{below}]
& \mathcal{D}
\arrow[phantom, "\bot", from=U, to=D] 
\end{tikzcd}
\]
between categories $\mathcal{C}$ and $\mathcal{D}$ is given by functors $L : \mathcal{C} \to \mathcal{D}$ (the \emph{left adjoint}) and $R : \mathcal{D} \to \mathcal{C}$ (the \emph{right adjoint}), together with natural transformations $\eta_A : A \to RL A$ (the \emph{unit} of the adjunction), and $\varepsilon_{B} : LR B \to B$ (the \emph{counit} of the adjunction), such that the maps
$\theta_{A,B} : \mathcal{C}(A, RB) \to \mathcal{D}(LA, B)$, and $\theta'_{A,B} : \mathcal{D}(LA, B) \to \mathcal{C}(A, RB)$, defined by
\[ \theta_{A,B}(f) = \varepsilon_B \circ Lf, \qquad \theta'_{A,B}(g) = Rg \circ \eta_{A}, \]
are mutually inverse.

Each such adjunction gives rise to a monad on $\mathcal{C}$, and a comonad on $\mathcal{D}$. The comonad is $(G, \varepsilon, \delta)$, where $G = LR$, and $\delta_B : LRB \to LRLRB$ is given by $\delta_B = L(\eta_{RB})$.

Conversely, every comonad arises from an adjunction, known as a \emph{resolution} of the comonad, in this way. In fact, there is a category of such resolutions for a given comonad $G$. The minimal (initial) resolution is the adjunction associated with the coKleisli category $\mathsf{Kl}(G)$, while the maximal (terminal) resolution arises from the category of coalgebras of $G$, also known as the \emph{Eilenberg-Moore category} $\mathsf{EM}(G)$ \cite{eilenberg1965adjoint}.

We have already studied the structure of the coalgebras for our game comonads. We shall now complete this analysis, by characterising the Eilenberg-Moore categories in terms independent of the comonads which give rise to them. This will give a new perspective on these constructions, as universal solutions to the problem of building various kinds of resource-bounded tree-structured covers of a given relational structure.

\subsection{The Ehrenfeucht-Fra\"{i}ss\'{e}~ adjunction}

We define a \emph{tree-ordered $\sigma$-structure} $(\mathscr{A}, {\leq})$ to be a $\sigma$-structure $\mathscr{A}$ with a forest order $\leq$ on $A$, satisfying the following condition: 
\begin{center}
(E) if $a \frown b$ in $\mathcal{G}(\mathscr{A})$, the Gaifman graph of $\mathscr{A}$ as a $\sigma$-structure, then $a {\uparrow} b$.   
\end{center}
Note that such a structure is the same thing as a forest cover of $\mathcal{G}(\mathscr{A})$ with universe $A$.
We write the covering relation of $\leq$ as $\prec$.
A morphism of tree-ordered $\sigma$-structures $f : (\mathscr{A}, {\leq}) \to (\mathscr{B}, {\leq'})$ is a $\sigma$-homomorphism $f : \mathscr{A} \to \mathscr{B}$ which maps roots to roots, and preserves the covering relation. This determines a category $\mathcal{R}^{E}(\sigma)$. For each $k>0$, if we restrict to forest orders of height $\leq k$, we get a sub-category $\mathcal{R}^{E}_{k}(\sigma)$.


\begin{theorem}
\label{EFadj}
For each $k >0$, the Eilenberg-Moore category $\mathsf{EM}(\mathbb{E}_{k})$ is isomorphic to $\mathcal{R}^{E}_{k}(\sigma)$.
\end{theorem}
\begin{proof}
By Theorem~\ref{tdth}, the objects of the two categories are in bijective correspondence.
Thus it remains to show that a $\sigma$-homomorphism $f : \mathscr{A} \to \mathscr{B}$ is an $\mathbb{E}_{k}$-coalgebra morphism iff it is an 
$\mathcal{R}^{E}_{k}(\sigma)$ morphism. From the analysis in Theorem~\ref{tdth}, if $\alpha : \mathscr{A} \to \mathbb{E}_{k} \mathscr{A}$ is a coalgebra, then
$\alpha(a) = [a_1, \ldots , a_k]$ iff $a_1 \prec \cdots \prec a_k = a$ is the covering chain of predecessors of $a$ in the forest order. Thus a coalgebra morphism preserves covering chains, and in particular preserves roots and the covering relation. Conversely, if a morphism preserves roots and the covering relation, then it preserves covering chains, and hence is a coalgebra morphism.
\end{proof}

As an immediate consequence of this result, we can describe the canonical resolution of $\mathbb{E}_{k}$ given by the adjunction between $\mathcal{R}(\sg)$ and $\mathsf{EM}(\mathbb{E}_{k})$ in terms of $\mathcal{R}^{E}_{k}(\sigma)$. 
There is an evident  forgetful functor $U_k : \mathcal{R}^{E}_{k}(\sigma) \to \mathcal{R}(\sg)$, which simply forgets the forest order.

\begin{theorem}
The functor $U_k$ has a right adjoint given by $G_k$, where $G_k(\mathscr{A}) = (\mathbb{E}_{k} \mathscr{A}, {\sqsubseteq})$.
The comonad arising from this adjunction is $\mathbb{E}_{k}$.
\end{theorem}


\subsection{The pebbling adjunction}
We define a \emph{$k$-pebble tree-ordered $\sigma$-structure} $(\mathscr{A}, {\leq}, p)$ to be a $\sigma$-structure $\mathscr{A}$ with a forest order $\leq$ on $A$, and a pebbling function $p: A \to \mathbf{k}$. In addition to condition (E), it must also satisfy the following condition: 
\begin{center}
(P) if $a \frown b$ in $\mathcal{G}_{\sg}(\mathscr{A})$, and $a < b$ in the forest order, then for all $x \in (a, b]$, $p(a) \neq p(x)$.
\end{center}
Morphisms of these structures are morphisms of tree-ordered structures which additionally preserve the pebbling function. These define a category $\mathcal{R}^{P}_{k}(\sigma)$ (where $k$ bounds  the number of pebbles, rather than the height of the forest order), and there is an evident forgetful functor $V_k : \mathcal{R}^{P}_{k}(\sigma) \to \mathcal{R}(\sg)$.

\begin{theorem}
For each $k >0$, the Eilenberg-Moore category $\mathsf{EM}(\mathbb{P}_{k})$ is isomorphic to $\mathcal{R}^{P}_{k}(\sigma)$.
\end{theorem}
\begin{proof}
The proof proceeds on similar lines to that of Theorem~\ref{EFadj}. 
By Theorem~\ref{kpfcth}, the objects of the two categories are in bijective correspondence.
Thus to complete the argument, we must show that coalgebra morphisms coincide with $\mathcal{R}^{P}_{k}(\sigma)$-morphisms.
Coalgebra morphisms preserve covering chains, and also preserve pebble indices, and this is equivalent to the conditions for $\mathcal{R}^{P}_{k}(\sigma)$-morphisms.
\end{proof}

Once again, there is an immediate corollary to this result.
\begin{theorem}
The functor $V_k$ has a right adjoint $H_k$, where $H_k(\mathscr{A}) = (\mathbb{P}_{k} \mathscr{A}, {\sqsubseteq}, p)$, where $p([(p_1, a_1), \ldots , (p_j, a_j)]) = p_j$. Moreover, $\mathbb{P}_{k}$ is the comonad arising from this adjunction.
\end{theorem}

\subsection{The modal adjunction}

Let $\sigma$ be a modal vocabulary. We define a modal $\sigma$-structure $(\mathscr{A}, a, {\leq})$ to be a $\sigma$-structure $\mathscr{A}$ with a partial order $\leq$ on $A$, such that ${\uparrow}(a) := \{ a' \in A \mid a \leq a' \}$ is a tree order. This must satisfy the following condition: 
\begin{center} 
(M) for $x, y \in {\uparrow}(a)$,  $x \prec y$ iff for some unique $R_{\alpha}$, $R_{\alpha}(x,y)$. 
\end{center}
A morphism of such structures is a $\sigma$-homomorphism preserving the root and the covering relation. For each $k>0$, there is a category $\mathcal{R}^{M}_{k}(\sigma)$, with $k$ bounding the height of the tree order, and a forgetful functor $W_k : \mathcal{R}^{M}_{k}(\sigma) \to \mathcal{R}_{\star}(\sg)$, which sends $(\mathscr{A}, a, {\leq})$ to $(\mathscr{A}, a)$.

\begin{theorem}
For each $k >0$, the Eilenberg-Moore category $\mathsf{EM}(\mathbb{M}_{k})$ is isomorphic to $\mathcal{R}^{M}_{k}(\sigma)$.
\end{theorem}

\begin{theorem}
For each $k >0$, the functor $W_k$ has a right adjoint, which sends $(\mathscr{A}, a)$ to $(\mathbb{M}_{k} \mathscr{A}, [a], {\sqsubseteq})$. Moreover, $\mathbb{M}_{k}$ is the comonad arising from this adjunction.
\end{theorem}

We have stated the result in this fashion for uniformity with the other cases, but it is worth noting that a simpler analysis applies in this case.\footnote{The authors are indebted to Dan Marsden for pointing this out.} The modal comonad is idempotent, \textit{i.e.}~ the comultiplication is an isomorphism.
This means that it encodes a coreflective subcategory.
The category of trees $\mathcal{R}^{M}_{k}(\sigma)$ is coreflective in the category of all pointed Kripke structures (see e.g. \cite{winskel1995models}), and the modal comonad arises from this coreflection.

\subsection{$I$-morphisms and relative adjunctions}
\label{Imoreladjsec}

To fit $I$-morphisms, as discussed in section~\ref{Imorsec}, into this picture, we shall use \emph{relative adjunctions} \cite{ulmer1968properties}.
Given functors $J : \mathcal{B} \to \mathcal{D}$ and $L : \mathcal{C} \to \mathcal{D}$, we say that $L$ has a $J$-right adjoint $R : \mathcal{B} \to \mathcal{C}$ if there is a natural isomorphism
\[ \hom_{\mathcal{D}}(L(-), J(-)) \; \cong \; \hom_{\mathcal{C}}(-, R(-)) . \]
In particular, if $L$ has a \textit{bona fide} right adjoint $R : \mathcal{D} \to \mathcal{C}$, we obtain a $J$-right adjoint by composing $R$ with $J$.

In our case, we have the functor $J : \mathcal{R}(\sg) \to \mathcal{R}(\sigma^{+})$, and we obtain relative versions of the Ehrenfeucht-Fra\"{i}ss\'{e}~ and pebbling adjunctions, again using the fact that our constructions are uniform in the relational vocabulary, and hence can be applied to $\mathcal{R}(\sigma^{+})$. These relative right adjoints give rise to the relative comonads $\mathbb{E}_{k}^{+}$ and $\mathbb{P}_{k}^{+}$.

\section{Logical Equivalences II: Open pathwise embeddings and back-and-forth equivalences}
\label{backandforthsec}

For each comonad $\mathbb{C}_{k}$, we shall now define a ``back-and-forth'' equivalence $\leftrightarrow_{k}^{\mathbb{C}}$, intermediate between $\rightleftarrows_{k}^{\mathbb{C}}$ and $\cong_{k}^{\mathbb{C}}$, and use it to characterize the logical equivalences induced by $\mathcal{L}_{k}$.
These back-and-forth equivalences will be more specific to ``game comonads'' defined on  $\mathcal{R}(\sg)$, but they will still be defined and shown to have the appropriate properties in considerable generality. We shall use a variant of the well-known notion of \emph{open map bisimulation} of Joyal, Nielsen and Winskel \cite{joyal1993bisimulation}. 

Although it is folklore that Ehrenfeucht-Fra\"{i}ss\'{e}~ equivalence is ``essentially'' a form of bisimulation, to our knowledge this is the first time that graded elementary equivalences have been captured in a precise common format with open map bisimulations. The key novel ingredient in our approach is the use of embeddings, and the notion of \emph{pathwise embedding}.
This allows us to capture a general notion of \emph{property-preserving bisimulation}, which specializes to capture all the notions of interest for model comparison games.

Referring to our study of adjunctions characterizing the game comonads in Section~\ref{sec:adjoints},
we can extract the following common structure. For each comonad $\mathbb{C}_{k}$ (variously $\mathbb{E}_{k}$, $\mathbb{P}_{k}$, or $\mathbb{M}_{k}$), we have a category $\mathcal{R}^{\mathbb{C}}_{k}$, and a commuting diagram of categories and functors:
\[ \begin{tikzcd}
& \mathcal{R}^{\mathbb{C}}_{k} \arrow[dl] \arrow[dr, "U_k"] \\
\mathsf{Trees} \arrow[dr] & & \mathcal{R}(\sg) \arrow[dl] \\
& \mathbf{Set} &
\end{tikzcd}
\]
Here $\mathsf{Trees}$ is the category of tree orders and maps which preserve the root and the covering relation. The unlabelled arrows are the evident faithful forgetful functors. The faithful functor $U_k$ has a right adjoint $F_k$, and $\mathbb{C}_{k}$ arises from this adjunction.

Note that the Ehrenfeucht-Fra\"{i}ss\'{e}~ and pebbling comonads produce \emph{forests} of non-empty sequences rather than trees. However, it is always possible, and often convenient, e.g.~for the base case of an inductive proof, to turn the forests into trees by adding the empty sequence. This is justified by the fact that $\mathsf{Forests}$, the category of forest orders and maps which take roots to roots, and preserve the covering relation, is equivalent to $\mathsf{Trees}$.

The above description applies directly to the Ehrenfeucht-Fra\"{i}ss\'{e}~ and pebbling cases. For the modal case, we should use $\mathcal{R}_{\star}(\sg)$ rather than $\mathcal{R}(\sg)$.
In this case, the construction produces a tree directly.

We recall the notion of $I$-morphism from Section~\ref{Imorsec}.
We shall build this into the current picture by working with the $J$-relative versions of the Ehrenfeucht-Fra\"{i}ss\'{e}~ and pebbling adjunctions, as in section~\ref{Imoreladjsec}. This means that objects of $\mathcal{R}^{\mathbb{C}}_{k}$ carry an interpretation of the $I$-relation, which morphisms of $\mathcal{R}^{\mathbb{C}}_{k}$ must preserve. The right adjoint functor $F_k$ produces a relation $I^{F_k \mathscr{A}}$ by the functorial action of $F_k$ on the identity relation $I^{\mathscr{A}}$.

A morphism $e$ in $\mathcal{R}^{\mathbb{C}}_{k}$ is an \emph{embedding} if
$U_k(e)$ is an extremal mono in $\mathcal{R}(\sg)$, \textit{i.e.}~ an embedding of relational structures. We write  $e: \mathscr{A} \rightarrowtail \mathscr{B}$ to indicate that $e$ is an embedding.
 
We define a subcategory $\mathsf{Paths}$ of $\mathcal{R}^{\mathbb{C}}_{k}$ whose objects have tree orders which are linear, so they comprise a single branch. If $P$ is a path, then $I^{P}$ is the identity relation. Morphisms of paths are embeddings. More generally, we say that $e : P \rightarrowtail \mathscr{A}$ is a \emph{path embedding} if $P$ is a path. A morphism $f : \mathscr{A} \to \mathscr{B}$ in $\mathcal{R}^{\mathbb{C}}_{k}$ is a \emph{pathwise embedding} if for any path embedding $e : P \rightarrowtail \mathscr{A}$, $f \circ e$ is a path embedding.

We can now define what it means for a morphism $f : \mathscr{A} \to \mathscr{B}$ in $\mathcal{R}^{\mathbb{C}}_{k}$ to be \emph{open}.
This holds if, whenever we have a diagram
\[ \begin{tikzcd}
  P \arrow[r, rightarrowtail] \arrow[d,rightarrowtail]
    & Q \arrow[d, rightarrowtail] \\
  \mathscr{A} \arrow[r,  "f"']
&\mathscr{B} 
\end{tikzcd}
\]
where $P$ and $Q$ are paths, there is an embedding $Q \rightarrowtail \mathscr{A}$ such that
\[ \begin{tikzcd}
  P \arrow[r, rightarrowtail] \arrow[d,rightarrowtail]
    & Q \arrow[dl, rightarrowtail] \arrow[d, rightarrowtail] \\
  \mathscr{A} \arrow[r,  "f"']
&\mathscr{B} 
\end{tikzcd}
\]
This is often referred to as the \emph{path-lifting property}. If we think of $f$ as witnessing a simulation of $\mathscr{A}$ by $\mathscr{B}$, path-lifting means that if we extend a given behaviour in $\mathscr{B}$ (expressed by extending the path $P$ to $Q$), then we can find a matching behaviour in $\mathscr{A}$ to ``cover'' this extension. Thus it expresses an abstract form of the notion of  ``p-morphism'' from modal logic \cite{blackburn2002modal}, or of functional bisimulation.

We can now define the \emph{back-and-forth equivalence} $\mathscr{A} \leftrightarrow_{k}^{\mathbb{C}} \mathscr{B}$ between structures in $\mathcal{R}(\sg)$. This holds if there is a structure $\mathscr{R}$ in $\mathcal{R}^{\mathbb{C}}_{k}$, and a span of open pathwise embeddings
\[ \begin{tikzcd}
& \mathscr{R} \arrow[dl] \arrow[dr] \\
F_k\mathscr{A} & & F_k \mathscr{B}
\end{tikzcd}
\]
This gives us a general, structural description of back-and-forth equivalence. At the same level of generality, we shall now define a \emph{back-and-forth game} $\mathcal{G}_k(\mathscr{A},\mathscr{B})$ played between the structures $\mathscr{A}$ and $\mathscr{B}$, corresponding to the comonad $\mathbb{C}_{k}$. Positions of the game are pairs $(s,t) \in \mathbb{C}_{k} A \times \mathbb{C}_{k} B$. 

We define a relation  $\mathsf{W}(\As,\Bs) \, \subseteq \, \mathbb{C}_{k} A \times \mathbb{C}_{k} B$ as follows. A pair $(s,t)$ is in $\mathsf{W}(\As,\Bs)$ iff for some path $P$, path embeddings $e_1 : P \rightarrowtail \mathbb{C}_{k} \mathscr{A}$ and $e_2 : P \rightarrowtail \mathbb{C}_{k} \mathscr{B}$, and $p \in P$, $s = e_1(p)$ and $t = e_2(p)$. The intention is that $\mathsf{W}(\As,\Bs)$ picks out the winning positions for Duplicator.

At the start of each round of the game, the position is specified by $(s, t) \in \mathbb{C}_{k} A \times \mathbb{C}_{k} B$. The initial position is $(\bot, \bot)$.  The round proceeds as follows. Either Spoiler chooses some $s' \succ s$, and Duplicator must respond with $t' \succ t$, resulting in a new position $(s', t')$; or Spoiler chooses some $t'' \succ t$ and Duplicator must respond with $s'' \succ s$, resulting in $(s'',t'')$. Duplicator wins the round if they are able to respond, and  the new position is in $\mathsf{W}(\As,\Bs)$.

\begin{theorem}
\label{pisoprop}
The following are equivalent:
\begin{enumerate}
\item $\mathscr{A} \leftrightarrow_{k}^{\mathbb{C}} \mathscr{B}$.
\item There is a winning strategy for Duplicator in the $\mathcal{G}_k(\mathscr{A},\mathscr{B})$ game.
\end{enumerate}
\end{theorem}
\begin{proof}
$(1) \; \Rightarrow \; (2)$. Firstly, consider an open pathwise embedding $q : \mathscr{R} \to F_k \mathscr{B}$. Since $q$ is a morphism in $\mathcal{R}^{\mathbb{C}}_{k}$, any covering chain $\bot \prec r_1 \prec \cdots \prec r_i$ in $\mathscr{R}$ is mapped to a covering chain $\bot \prec s_1 \prec \cdots \prec s_i$ in $F_k \mathscr{B}$.
Since $\bot \prec r_1 \prec \cdots \prec r_i$ is the image of a path embedding $P \rightarrowtail \mathscr{R}$, and $q$ is a pathwise embedding, for each $j$ with $1 \leq j \leq i$, $(r_j,s_j) \in \mathsf{W}(\As,\Bs)$.

\textbf{Claim} For any $s_{i+1} \succ s_i \in F_k B$, there is $r_{i+1} \succ r_i \in R$ such that $q(r_{i+1}) = s_{i+1}$.

To prove this, note that there are path embeddings $e_1 : P \rightarrowtail \mathscr{R}$ and $e_2 : Q \rightarrowtail F_k \mathscr{B}$ with images
$\bot \prec r_1 \prec \cdots \prec r_i$ and $\bot \prec s_1 \prec \cdots \prec s_i \prec s_{i+1}$ respectively. Writing $Q_{\leq i}$ for the truncation of $Q$ to  the preimage of $\bot \prec s_1 \prec \cdots \prec s_i$, $\mathscr{R}_i$ for the image of $e_1$, and $(F_k \mathscr{B})_i$ for the image of $e_2 |_{Q_{\leq i}}$, we have an embedding $e : P \cong \mathscr{R}_i \cong (F_k \mathscr{B})_i \cong Q_{\leq i}\rightarrowtail Q$, such that $q \circ e_1 = e_2 \circ e$. Since $q$ is open, there is an embedding $Q \rightarrowtail \mathscr{R}$ such that
\[ \begin{tikzcd}
  P \arrow[r, rightarrowtail, "e"] \arrow[d,rightarrowtail, "e_1"']
    & Q \arrow[dl, rightarrowtail] \arrow[d, rightarrowtail, "e_2"] \\
  \mathscr{R} \arrow[r,  "q"']
& F_k \mathscr{B} 
\end{tikzcd}
\]
We define $r_{i+1}$ to be the image of the greatest element of $Q$ under this embedding. Since $Q$ is a chain, $r_{i+1} \succ r_i$. By the commutativity of the above diagram, $q(r_{i+1}) = s_{i+1}$. 

Now suppose we have a span of open pathwise embeddings
\[ \begin{tikzcd}
& \mathscr{R} \arrow[dl, "p"'] \arrow[dr, "q"] \\
F_k\mathscr{A} & & F_k \mathscr{B}
\end{tikzcd}
\]
For any path embedding $P \rightarrowtail \mathscr{R}$ with image $\bot \prec r_1 \prec \cdots \prec r_i$, 
we have a pair $(s, t) = (p(r_i), q(r_i))$ in $\mathbb{C}_{k} \mathscr{A} \times \mathbb{C}_{k} \mathscr{B}$. Since $p$ and $q$ are pathwise embeddings, $(s, t) \in \mathsf{W}(\As,\Bs)$.

Moreover, for any $t' \succ t$, we can find $r_{i+1} \succ r_i$ and $s' \succ s$ such that $(p(r_{i+1}), q(r_{i+1})) = (s',t')$ by the above Claim. A symmetric argument applies to yield an extension for any $s' \succ s$. Thus the set of such pairs $(s, t)$ yields a winning strategy for Duplicator in the $\mathcal{G}_k(\mathscr{A}, \mathscr{B})$ game.

$(2) \; \Rightarrow \; (1)$. Suppose we have a winning strategy for Duplicator. This determines a set $R  \subseteq \mathbb{C}_{k} A \times \mathbb{C}_{k} B$ of the plays following this strategy. 

$R$ is a down-closed subset of a tree, and hence forms a tree in the induced order. It determines a substructure of the product relational structure $F_k \mathscr{A} \times F_k \mathscr{B}$, and hence an object $\mathscr{R}$ in $\mathcal{R}^{\mathbb{C}}_{k}$. Restricting the projections to $R$ determines morphisms $p : \mathscr{R} \to F_k \mathscr{A}$, $q : \mathscr{R} \to F_k \mathscr{B}$.

Firstly, we show that $p$ and $q$ are pathwise embeddings. Given a path embedding $e : P \rightarrowtail \mathscr{R}$, $p \circ e$ is an injective homomorphism, since it is a morphism in $\mathcal{R}^{\mathbb{C}}_{k}$, and preserves the covering relation. 
It remains to show that $p \circ e$, or equivalently the restriction of $p$ to the image of $e$, is strong.
Given $(s_1,t_1), \ldots , (s_n,t_n)$ in the image of $e$, let $s$ be the maximum of the $s_i$, which exists since the image of a path forms a chain. If $s = s_j$, then $t_j$ is the maximum of the $t_i$. Now since $(s,t) \in \mathsf{W}(\As,\Bs)$, there is a path $Q$, and embeddings $e_1 : Q \rightarrowtail F_k \mathscr{A}$, $e_2 : Q \rightarrowtail F_k \mathscr{B}$, with $s = e_1(r)$ and $t = e_2(r)$ for some $r \in Q$.
Since $e_1$ and $e_2$ preserve covering chains, they map predecessors of $r$ bijectively to prefixes of $s$ and $t$ respectively, in a height-preserving fashion.\footnote{The height of an element of a forest is the length of its unique covering chain from a root.} Thus for each $i$, there is a unique
element $r_i \in Q$ such that $e_1(r_i) = s_i$ and $e_2(r_i) = t_i$. For an $n$-ary relation $R$ in $\sigma$, since $e_1$ and $e_2$ are embeddings, we have 
\[ R^{F_k \mathscr{A}}(s_1, \ldots , s_n) \Longleftrightarrow R^{Q}(r_1, \ldots , r_n) \Longleftrightarrow R^{F_k \mathscr{B}}(t_1, \ldots , t_n) . \]
Hence 
\[ R^{F_k \mathscr{A}}(s_1, \ldots , s_n) \Longleftrightarrow (R^{F_k \mathscr{A}}(s_1, \ldots , s_n) \, \wedge \, R^{F_k \mathscr{B}}(t_1, \ldots , t_n)) \Longleftrightarrow R^{\mathscr{R}}((s_1,t_1), \ldots , (s_n,t_n)), \]
and thus $p \circ e$ is a strong homomorphism, as required. The verification that $q$ is a pathwise embedding is entirely similar.

Finally, we must show that these morphisms are open.
Suppose we are given a diagram
\[ \begin{tikzcd}
  P \arrow[r, rightarrowtail] \arrow[d,rightarrowtail]
    & Q \arrow[d, rightarrowtail] \\
  \mathscr{R} \arrow[r,  "p"']
&F_k \mathscr{A} 
\end{tikzcd}
\]
We consider the case where $Q$ extends $P$ by one element; the general case will then follow by induction. 

Let the image of the maximum element of $P$ be $(s, t)$, and the image of the maximum element of $Q$ be $s' \succ s$. Since $R$ encodes the plays of a winning strategy for Duplicator, for some $t' \succ t$, we have $(s', t') \in R$, and moreover $(s', t') \in \mathsf{W}(\As,\Bs)$. This implies that the map which extends the embedding $P \rightarrowtail \mathscr{R}$ by sending the maximum element of $Q$ to $(s',t')$ determines an embedding $Q \rightarrowtail \mathscr{R}$, which makes the path-lifting diagram commute.

The fact that $q$ is open is shown by a symmetric argument.
\end{proof}

For each of our three comonads, we will show that the game $\mathcal{G}_k$ specializes to exactly the expected concrete game: Ehrenfeucht-Fra\"{i}ss\'{e}~, pebbling, and modal bisimulation respectively.
The connections with the corresponding logical equivalences will follow as immediate corollaries.

\subsection{The Ehrenfeucht-Fra\"{i}ss\'{e}~ comonad}

We now show that the generic game $\mathcal{G}_k$, when instantiated to the Ehrenfeucht-Fra\"{i}ss\'{e}~ adjunction and comonad, yields exactly the standard Ehrenfeucht-Fra\"{i}ss\'{e}~ game.

We begin with two observations we will use.
\begin{lemma}
\label{nrisolemm}
Consider $s = [a_1, \ldots , a_i] \in \mathbb{E}_{k} \mathscr{A}$.
If $s$ is non-repeating, then the substructure of $\mathbb{E}_{k} \mathscr{A}$ determined by the prefixes of $s$ is isomorphic to the substructure of $\mathscr{A}$ determined by $\{ a_1, \ldots , a_i \}$.
\end{lemma}
\begin{proof}
Since the elements of the first set form a chain, this is immediate from the definition of $\mathbb{E}_{k} \mathscr{A}$, given that the correspondence $[a_1, \ldots , a_j] \mapsto a_j$ is bijective, which holds if $s$ is non-repeating.
\end{proof}

\begin{lemma}
\label{nrembedlemm}
The image of a path embedding $e : P \rightarrowtail F_k \mathscr{A}$ can only contain non-repeating sequences.
\end{lemma}
\begin{proof}
Since $e$ is an $I$-morphism, it must reflect the $I$ relation, while at the same time, it is injective. This implies that $I^{F_k \mathscr{A}}$ must be the identity relation on the image of $e$, or equivalently, that the image of $e$ must contain only non-repeating sequences.
\end{proof}

\begin{theorem}
\label{EFgeneric}
Given $\sigma$-structures $\mathscr{A}$ and $\mathscr{B}$, the $\mathcal{G}_k(\mathscr{A},\mathscr{B})$ game for the Ehrenfeucht-Fra\"{i}ss\'{e}~ comonad is equivalent to the Ehrenfeucht-Fra\"{i}ss\'{e}~ game between $\mathscr{A}$ and $\mathscr{B}$.

\end{theorem}
\begin{proof}
Since $s' \succ s$ in $\mathbb{E}_{k} \mathscr{A}$ iff $s' = s[a]$ for some $a \in A$, we see from the definition of $\mathcal{G}_k(\mathscr{A},\mathscr{B})$ for the Ehrenfeucht-Fra\"{i}ss\'{e}~ comonad
that it coincides with the Ehrenfeucht-Fra\"{i}ss\'{e}~ game, provided we can show, for $(s,t) \in \mathbb{E}_{k} \mathscr{A} \times \mathbb{E}_{k} \mathscr{B}$, that $(s,t) \in \mathsf{W}(\As,\Bs)$ iff $(s,t)$ satisfies the winning condition for the Ehrenfeucht-Fra\"{i}ss\'{e}~ game.
We recall that this winning condition is that the relation $s_i \mapsto t_i$ is a partial isomorphism between $\mathscr{A}$ and $\mathscr{B}$.

Firstly, suppose that $(s,t) \in \mathsf{W}(\As,\Bs)$, via path embeddings $e_1 : P \rightarrowtail \mathbb{E}_{k} \mathscr{A}$, $e_2 : P \rightarrowtail \mathbb{E}_{k} \mathscr{B}$.
This implies that the image of $e_1$, consisting of the prefixes of $s$, is isomorphic to the image of $e_2$, consisting of the prefixes of $t$. 
Applying Lemmas~\ref{nrisolemm} and \ref{nrembedlemm}, we conclude that $(s,t)$ determine a partial isomorphism between $\mathscr{A}$ and $\mathscr{B}$, as required.

For the converse, note that for any $(s, t)$ following a winning strategy for Duplicator in the Ehrenfeucht-Fra\"{i}ss\'{e}~ game, the elementwise correspondence $s_i \mapsto t_i$ must be a partial isomorphism, and in particular a bijection. Hence Duplicator must always give the same response to repetitions of a given move by Spoiler. It follows that the behaviour of the strategy is determined by its restriction to non-repeating sequences, and without loss of generality, we can restrict the strategy to non-repeating sequences $(s,t)$. 
The prefixes of $s$ form an induced substructure of $\mathbb{E}_{k} \mathscr{A}$ which, applying lemma~\ref{nrisolemm}, is isomorphic to the substructure of $\mathbb{E}_{k} \mathscr{B}$ induced by the prefixes of $t$. Hence these substructures are the images of path embeddings with a common domain, and $(s,t) \in \mathsf{W}(\As,\Bs)$.
\end{proof}



\begin{theorem}
\label{EFlogicequivthm}
For all structures $\mathscr{A}$ and $\mathscr{B}$: $\mathscr{A} \equiv^{\Lk} \mathscr{B} \; \Longleftrightarrow \; \mathscr{A} \leftrightarrow_{k}^{\mathbb{E}} \mathscr{B}$.
\end{theorem}
\begin{proof}
This is an immediate corollary to Theorems~\ref{classicgamesthm}(1), \ref{pisoprop},  and~\ref{EFgeneric}.
\end{proof}

\begin{remark}
If we drop the $I$-morphism requirement on embeddings, we obtain a weaker notion of equivalence, in which the winning condition is that we have a \emph{partial correspondence} rather than a partial isomorphism. This yields 
a characterization of elementary equivalence for equality-free logic \cite{Casanovas1996}.
\end{remark}

\subsection{The pebbling comonad}
\label{logeqIIsec}

The analysis for the pebbling comonad is largely similar to that for the Ehrenfeucht-Fra\"{i}ss\'{e}~ comonad.

Firstly, given $s = [(p_1, a_1), \ldots , (p_i, a_i)] \in \mathbb{P}_{k} \mathscr{A}$, and $p \in \{ p_1, \ldots , p_i \}$, we define
$\mathsf{last}_{p}(s) = a$, where $s = s_1[(p,a)]s_2$, and $p$ does not occur in $s_2$.
We say that $s$ is \emph{non-duplicating} if for all $s' \sqsubseteq s$, $\mathsf{last}_{p}(s') = \mathsf{last}_{p'}(s') \; \Rightarrow \; p = p'$. Thus we never have two pebbles placed on the same element.

Given $s = [(p_1, a_1), \ldots , (p_i, a_i)] \in \mathbb{P}_{k} \mathscr{A}$ and $t = [(p_1, b_1), \ldots , (p_i, b_i)] \in \mathbb{P}_{k} \mathscr{B}$, we define $\gamma(s,t) := \{ (\mathsf{last}_{p}(s), \mathsf{last}_{p}(t)) \mid p \in \{ p_1, \ldots , p_i \} \} \subseteq A \times B$. The prefix of $s$ (respectively $t$) of length $j$ is denoted $s_j$ (respectively $t_j$). The winning condition on plays $(s,t) \in \mathbb{P}_{k} A \times \mathbb{P}_{k} B$ for the pebbling game is that for each $j$, $\gamma(s_j,t_j)$ is a partial isomorphism between $\mathscr{A}$ and $\mathscr{B}$.

The following results play the role for the pebbling comonad of Lemmas~\ref{nrisolemm} and~\ref{nrembedlemm} for the Ehrenfeucht-Fra\"{i}ss\'{e}~ comonad.


\begin{lemma}
\label{nonduplemm}
If $s$ and $t$ are non-duplicating, then the following are equivalent:
\begin{enumerate}
    \item For all $j$, $\gamma(s_j,t_j)$ is a partial isomorphism from $\mathscr{A}$ to $\mathscr{B}$.
   \item  The substructure $P_s$ of $\mathbb{P}_{k} \mathscr{A}$ determined by the prefixes of $s$ is isomorphic to the substructure $P_t$ of $\mathbb{P}_{k} \mathscr{B}$ determined by the prefixes of $t$.
\end{enumerate}
\end{lemma}
\begin{proof}
If $s$ and $t$ are non-duplicating, then each $\gamma(s_j,t_j)$ is a partial bijection.
Also, the correspondence $\beta : s_j \mapsto t_j$ is a bijection between $P_s$ and $P_t$.
For $s_1, \ldots , s_n \in P_s$, since they are all prefixes of $s$ and hence comparable,
$R^{\mathbb{P}_{k} \mathscr{A}}(s_1, \ldots , s_n)$ iff $R^{\mathscr{A}}(\epsilon_{\As} s_1, \ldots , \epsilon_{\As} s_n)$ and $\{ \epsilon_{\As} s_1, \ldots , \epsilon_{\As} s_n \} \subseteq \mathrm{dom} \, \gamma(s',t')$, where $s'$ is the maximum in the prefix ordering among $s_1, \ldots , s_n$. Similarly for relation instances in $P_t$.
Hence preservation of relation instances by $\beta$ is equivalent to preservation by $\gamma(s_j,t_j)$ for all $j$, and similarly for their inverses.
\end{proof}

\begin{lemma}
\label{ndupembedlemm}
The image of an embedding $e : P \rightarrowtail G_k \mathscr{A}$ can only contain non-duplicating sequences.
\end{lemma}
\begin{proof}
Since $e$ is an $I$-morphism, it must reflect the $I$ relation, while at the same time, it is injective. This implies that $I^{G_k \mathscr{A}}$ must be the identity relation, or equivalently, that the image of $e$ must contain only non-duplicating sequences.
\end{proof}

\begin{theorem}
\label{Pgeneric}
Given $\sigma$-structures $\mathscr{A}$ and $\mathscr{B}$, the $\mathcal{G}_k(\mathscr{A},\mathscr{B})$ game for the pebbling comonad is equivalent to the pebbling game between $\mathscr{A}$ and $\mathscr{B}$.
\end{theorem}
\begin{proof}
The proof proceeds essentially identically to that of Theorem~\ref{EFgeneric}, except that the appeal to 
Lemmas~\ref{nrisolemm} and~\ref{nrembedlemm} is replaced by one to Lemmas~\ref{nonduplemm} and~\ref{ndupembedlemm}.
\end{proof}

\begin{theorem}
\label{Plogicequivthm}
For all structures $\mathscr{A}$ and $\mathscr{B}$: $\mathscr{A} \equiv^{\Lvk} \mathscr{B} \; \Longleftrightarrow \; \mathscr{A} \leftrightarrow_{k}^{\mathbb{P}} \mathscr{B}$.
\end{theorem}
\begin{proof}
This is an immediate corollary to Theorems~\ref{classicgamesthm}(2), \ref{pisoprop}, and~\ref{Pgeneric}.
\end{proof}

\subsection{The modal comonad}
The key notion of equivalence in modal logic is bisimulation \cite{blackburn2002modal,sangiorgi2009origins}. We shall define the finite approximants to bisimulation \cite{hennessy1980observing}.\footnote{Our focus on finite approximants in this paper is for uniformity, and because they are relevant in resource terms. We can extend the comonadic semantics beyond the finite levels. We shall return to this point in the final section.}
Given Kripke structures $\mathscr{A}$ and $\mathscr{B}$, we define a family of relations $\sim_k \; \subseteq \; A \times B$: $a \sim_{k+1} b$ iff 
\begin{enumerate}
    \item for all unary $P$, $P^{\As}(a)$ iff $P^{\Bs}(b)$
    \item for all binary $R_{\alpha}$, $R_{\alpha}^{\As}(a, a')$ implies for some $b'$, $R_{\alpha}^{\Bs}(b,b')$ and $a' \sim_k b'$, and $R_{\alpha}^{\Bs}(b,b')$ implies for some $a'$, $R_{\alpha}^{\As}(a, a')$ and $a' \sim_k b'$.
\end{enumerate}
The base case $a \sim_{0} b$ holds whenever only the first condition is satisfied.

Plays in the \emph{modal bisimulation game} between pointed structures $(\mathscr{A},a)$ and $(\mathscr{B},b)$ are represented by positions $(s,t) \in \mathbb{M}_{k} (A, a) \times \mathbb{M}_{k} (B, b)$. 
A position $(s, t)$ satisfies the winning condition if $s = [a_0, \alpha_1, a_1, \ldots \alpha_i, a_i]$,
$t = [b_0, \alpha_1, b_1, \ldots \alpha_i, b_i]$, $a_0 = a$, $b_0 = b$, and for all $0 \leq j \leq i$, and unary predicates $P$, $P^{\As}(a_j)$ iff $P^{\Bs}(b_j)$. 
Note that there is no bijection requirement. This is why $I$-morphisms are not needed in the modal case.

The initial position is $([a], [b])$. If we have reached position $(s,t)$ after $k$ rounds, then round $k+1$ proceeds as follows:
Spoiler either chooses $s' \succ s$, and Duplicator must respond with $t' \succ t$, producing the new position $(s',t')$; or Spoiler chooses $t'' \succ t$, and Duplicator must respond with $s'' \succ s$, producing the new position $(s'',t'')$. Duplicator wins the round if they are able to respond, and the new position satisfies the winning condition.

Note that $s' \succ s$ iff $s' = s[\alpha, a']$ for a transition relation $R_{\alpha}$ and $a' \in A$ such that  
$R_{\alpha}^{\As}(a, a')$, where $a = \epsilon_{\As}(s)$.


The following standard result is essentially immediate from the definitions.
\begin{proposition}
Given pointed structures $(\mathscr{A}, a)$ and $(\mathscr{B}, b)$, the following are equivalent:
\begin{enumerate}
    \item $a \sim_k b$
    \item Duplicator has a winning strategy for the $k$-round modal bisimulation game.
\end{enumerate}
\end{proposition}

\begin{theorem}
\label{Mgeneric}
Given pointed structures $(\mathscr{A}, a)$ and $(\mathscr{B}, b)$, the $\mathcal{G}_k(\mathscr{A},\mathscr{B})$ game for the modal comonad is equivalent to the modal bisimulation game between $(\mathscr{A}, a)$ and $(\mathscr{B}, b)$.
\end{theorem}
\begin{proof}
Given the way we have defined the modal bisimulation game, this immediately reduces to verifying that the winning conditions coincide.
To see this, note that there is a path embedding $e : (R, r) \rightarrowtail \mathbb{M}_{k} (\mathscr{A}, a)$ with $e(r_i) = s_i$ iff 
$r = r_0 \lt{\alpha_1} r_1 \lt{\alpha_2} \ldots , \lt{\alpha_i} r_i$, 
$s_i = [a_0, \alpha_1, a_1, \alpha_2, a_2, \ldots, \alpha_i, a_i]$, with $a = a_0$, and for all unary $P$, and
$0 \leq j \leq i$, $P^{Q}(r_i) \Longleftrightarrow P^{\mathscr{A}}(a_i)$.
Chaining these equivalences for a span of path embeddings shows
the equivalence of the winning conditions for $\mathcal{G}_k(\mathscr{A},\mathscr{B})$ and the modal bisimulation game.
\end{proof}

\begin{theorem}
\label{Mlogicequivthm}
For all pointed structures $(\mathscr{A}, a)$ and $(\mathscr{B},b)$: $\mathscr{A} \equiv^{\mathcal{M}_{k}} \mathscr{B} \; \Longleftrightarrow \; \mathscr{A} \leftrightarrow_{k}^{\mathbb{M}} \mathscr{B}$.
\end{theorem}
\begin{proof}
This is an immediate corollary to Theorems~\ref{classicgamesthm}(3), \ref{pisoprop}, and~\ref{Mgeneric}.
\end{proof}

\section{Further Directions}


From the categorical perspective, there is considerable additional structure which we have not needed for the results in this paper, but which may be useful for further investigations.

\textbf{Coequaliser requirements}
In Moggi's work on computational monads, there is an ``equaliser requirement'' \cite{moggi1991notions}. The dual version for a comonad $(G, \varepsilon, \delta)$ is that for every object $A$, the following diagram is a coequaliser:
\begin{center}
\begin{tikzcd}
G^2 A \ar[r, "G \epsilon_{\As}", yshift=0.9ex] \ar[r, "\varepsilon_{GA}"', yshift=-0.6ex]
& GA \ar[r, "\epsilon_{\As}"]
& A
\end{tikzcd}
\end{center}
This says in particular that the counit is a regular epi, and hence  $GA$ ``covers'' $A$ in a strong sense.

This coequaliser requirement holds for all our comonads. For $\mathbb{E}_{k}$, this is basically the observation that, given a sequence of sequences $[s_1, \ldots , s_j]$, we have $\varepsilon[\varepsilon s_1, \ldots , \varepsilon s_j] \, = \, \varepsilon s_j$. The other cases are similar.


\textbf{Indexed and graded structure}
Our comonads $\mathbb{E}_{k}$, $\mathbb{P}_{k}$, $\mathbb{M}_{k}$ are not merely discretely indexed by the resource parameter. In each case, there is a functor $(\mathbb{Z}^{+}, {\leq}) \to \mathsf{Comon}(\mathcal{R}(\sg))$
from the poset category of the positive integers to the category of comonads on $\mathcal{R}(\sg)$. Thus if $k \leq l$ there is a natural transformation with components $i^{k,l}_A : \mathbb{E}_{k} \mathscr{A} \to \mathbb{E}_{l} \mathscr{A}$, which preserves the counit and comultiplication; and similarly for the other comonads. Concretely, this is just including the plays of up to $k$ rounds in the plays of up to $l$ rounds, $k \leq l$.

Another way of parameterizing comonads by resource information is grading \cite{gaboardi2016combining}. Recall that comonads on $\mathcal{C}$ are exactly the comonoids in the strict monoidal category $([\mathcal{C}, \mathcal{C}], {\circ}, I)$ of endofunctors on $\mathcal{C}$ \cite{mac2013categories}. Generalizing this description, a graded comonad is an oplax monoidal functor $G : (M, {\cdot}, 1) \to ([\mathcal{C}, \mathcal{C}], {\circ}, I)$ from a monoid of grades into this endofunctor category. This means that for each $m \in M$, there is an endofunctor $G_m$, there is a graded counit natural transformation $\varepsilon : G_{1} \Rightarrow I$, and for all $m, m' \in M$, there is a graded comultiplication $\delta^{m, m'} : G_{m\cdot m'} \Rightarrow G_m G_{m'}$.

The two notions can obviously be combined. We can see our comonads as (trivially) graded, by viewing them as oplax monoidal functors $(\mathbb{Z}^{+}_{\infty}, {\leq}, \min, \infty) \to ([\mathcal{C}, \mathcal{C}], {\circ}, I)$. Given $k \leq l$, we have e.g. $\mathbb{E}_{k} \Rightarrow \mathbb{E}_{k} \mathbb{E}_{k} \Rightarrow \mathbb{E}_{k} \mathbb{E}_{l}$. The unit is interpreted using the $\omega$-colimit $\mathbb{E}_{\omega}$, as described in the following paragraph.

The question is whether there are  interesting graded structures which arise naturally in considering richer logical and computational situations in our setting.

\textbf{Colimits and infinite behaviour}
In this paper, we have dealt exclusively with finite resource levels. However, there is an elegant means of passing to infinite levels. We shall illustrate this with the modal comonad. Using the inclusion morphisms described in the  previous discussion of indexed structure, for each pointed structure $(\mathscr{A}, a)$ we have a diagram
\[ \mathbb{M}_1 (\mathscr{A}, a) \to \mathbb{M}_2 (\mathscr{A}, a) \to \cdots \to \mathbb{M}_{k} (\mathscr{A}, a) \to \cdots \]
By taking the colimits of these diagrams, we obtain a comonad $\MM_{\omega}$, which corresponds to the usual unfolding of a Kripke structure to all finite levels. 
Similar constructions can be applied to the other comonads. 


\textbf{Relations between fragments and parameters}
We can define morphisms between the different comonads we have discussed, which yield proofs about the relationships between the logical fragments they characterize. This categorical perspective avoids the cumbersome syntactic translations  in the standard proofs of these results. For illustration, there is a comonad morphism $t: \mathbb{E}_{k} \Rightarrow \mathbb{P}_{k}$ with components $t_A: \mathbb{E}_{k} \mathscr{A} \rightarrow \mathbb{P}_{k} \mathscr{A}$ given by $[a_{1},\dots,a_{j}] \mapsto [(1,a_{1}),\dots,(j,a_{j})]$. Together with  theorems \ref{thm:mainPebble} and \ref{thm:mainEF}, this shows that $\exists\mathcal{L}_{k} \subseteq \exists\mathcal{L}^{k}$ and $\mathcal{L}_{k}^{\#} \subseteq \mathcal{L}^{k}(\Count)$. Moreover, composing $t$ with a coalgebra $\mathscr{A} \rightarrow \mathbb{E}_{k} \mathscr{A}$ yields a coalgebra $A \rightarrow \mathbb{P}_{k} \mathscr{A}$, demonstrating that $\mathsf{tw}(\mathscr{A}) + 1 \leq \mathsf{td}(\mathscr{A})$. 

Another morphism $\MM_{\omega} \Rightarrow \mathbb{P}_2$ shows that modal logic can be embedded into $2$-variable logic. More precisely, we can lift $\mathbb{P}_2$ to $\mathcal{R}_{\star}(\sg)$ by defining the distinguished element of $\mathbb{P}_2 (\mathscr{A}, a)$ to be $[(1,a)]$. The component of the morphism at $(\mathscr{A}, a)$ sends 
$[a_0, \alpha_1, a_1, \ldots , \alpha_j, a_{j}]$ to $[(1, a_0), (2, a_1), (1, a_2), \ldots ((j \mod 2) + 1, a_j)]$. This captures the ``hand-over-hand'' reuse of variables in a syntax-free fashion.

   
\subsection*{Concluding remarks}
Our comonadic constructions for the three major forms of model comparison games show a striking unity, on the one hand, but also some very interesting differences. For the latter, we note the different forms of logical ``deception'' associated with each comonad, 
the finite character of $\mathbb{E}_{k}$ and $\mathbb{M}_{k}$ and the non-finite character of $\mathbb{P}_{k}$,
and the different combinatorial parameters which arise in each case.

One clear direction for future work is to gain a deeper understanding of what makes these constructions work. Another is to understand how widely the comonadic analysis of resources can be applied. We are currently investigating the guarded fragment \cite{andreka1998modal,gradel2014freedoms}; other natural candidates include existential second-order logic, and branching quantifiers and dependence logic \cite{vaananen2007dependence}.

Since comonads arise naturally in type theory and functional programming \cite{uustalu2008comonadic,orchard2014programming}, can we connect the study of finite model theory made here with a suitable type theory? Can this lead, via the Curry-Howard correspondence, to the systematic derivation of some significant meta-algorithms, such as decision procedures  for guarded logics based on the tree model property \cite{gradel1999decision}, or  algorithmic metatheorems such as Courcelle's theorem \cite{courcelle1990monadic}?

Another intriguing direction is to connect these ideas with the graded quantum monad studied in \cite{abramskyquantum}, which provides a basis for the study of quantum advantage in $\mathcal{R}(\sg)$. This may lead to a form of quantum finite model theory.

\subsection*{Current and ongoing work}
The first author of the present paper and Anuj Dawar have recently begun a U.K. EPSRC-funded joint project on ``Resources and Co-Resources: a junction between categorical semantics and descriptive complexity'' to pursue the ideas introduced in \cite{abramsky2017pebbling} and the present paper. Project participants include the second author of the present paper, Tom Paine, Adam \'O Conghaile, Daniel Marsden, Tom\'a\v{s} Jakl and Luca Reggio.  A categorical analysis of Rossman's Equirank HPT Theorem appears in \cite{abramsky2020whither}, and a significant refinement of this result, involving a combination of the pebbling and Ehrenfeucht-Fra\"{i}ss\'{e}~ comonads, is presented in \cite{Paine2020}.
Current work in progress includes comonadic treatments of guarded fragments \cite{abramsky2021comonadic} and of generalized quantifiers \cite{conghaile2020game}, and coalgebraic characterizations of further combinatorial invariants such as clique-width.

\bibliographystyle{amsplain}
