Abstract References

Effectful Mealy Machines:
Coalgebraic and Causal Traces

Filippo Bonchi ORCID Universitá di Pisa, Italy Elena Di Lavore ORCID University of Oxford, UK Mario Román ORCID University of Oxford, UK
Abstract

We introduce effectful Mealy machines – a general notion of Mealy machine with global effects – and give them semantics in terms of both bisimilarity and traces. Bisimilarity of effectful Mealy machines is characterized syntactically, via free uniform feedback. Their traces are given a novel semantic coinductive universe in terms of effectful streams. We prove that this framework generalizes standard causal processes and captures existing flavours of Mealy machine, bisimilarity, and trace.

This is an extended abstract for the manuscript Effectful Mealy Machines: Bisimulation and Trace that will appear in the proceedings of LiCS 2025 [3]; an extended version with proofs is also available (arxiv.org/abs/2410.10627[4]. We additionally characterise causal processes as lax-natural transformations.

Keywords and phrases:
Mealy machines, coinduction, copy-discard categories, premonoidal categories
Category:
Invited Talk
Funding:
Filippo Bonchi: supported by the the Italian Ministero dell’Università e della Ricerca, grant PRIN 2022, PNRR No. P2022HXNSC - RAP (Resource Awareness in Programming) and partially by the Advanced Research + Invention Agency (ARIA) Safeguarded AI Programme. This study was carried out within the National Centre on HPC, Big Data and Quantum Computing - SPOKE 10 (Quantum Computing) and received funding from the European Union Next-GenerationEU - National Recovery and Resilience Plan (NRRP) – MISSION 4 COMPONENT 2, INVESTMENT N. 1.4 – CUP N. I53C22000690001.
Elena Di Lavore: supported by the Advanced Research + Invention Agency (ARIA) Safeguarded AI Programme.
Mario Román: partially supported by the Air Force Office of Scientific Research under award number FA9550-21-1-0038 and by the Advanced Research + Invention Agency (ARIA) Safeguarded AI Programme.
Copyright and License:
[Uncaptioned image] © Filippo Bonchi, Elena Di Lavore, and Mario Román; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Categorical semantics
; Theory of computation Abstract machines
Related Version:
Full Version: https://arxiv.org/abs/2410.10627 [4]
Editors:
Corina Cîrstea and Alexander Knapp

Introduction

Mealy machines have a transition behaviour UXUY that takes the current state in U and an input in X to transition to the next state and produce an output in Y. This perspective naturally leads us to consider Mealy machines as morphisms in a category, and to define their sequential and parallel compositions [14]. Mealy machines may also be seen as coalgebras [18]. This perspective naturally leads us to interpret their behaviour as the mapping to a final coalgebra.

Learning from these two perspectives, we define effectful Mealy machines as morphisms in an appropriate category, and give them trace semantics in a final coalgebra. Homomorphisms of effectful Mealy machines generalise coalgebra morphisms, and determine bisimulations.

Effectful Mealy machines may produce effects when transitioning. These effects are determined by an effectful triple: a triple of categories, introduced by Jeffrey [13], that distinguishes between different kinds of computations. There are values, which are total and deterministic, and form a cartesian category 𝕍; there are local computations, which do not affect the global state, and form a monoidal category because local effects interchange; and there are global computations, which may affect the global state, and form a premonoidal category  [16]. These three categories share the same objects, meaning that the different types of computation affect the same resources. Formally, an effectful triple is a triple of categories, (𝕍) as above, with two identity-on-objects functors between them that strictly preserve the monoidal and premonoidal structures, respectively.

Mealy machines and bisimilarity

A Mealy machine has a transition function and an initial state. At each step, the machine takes an input and, depending on the current state, it transitions to the next state producing an output. An effectful Mealy machine follows the same pattern, but it may produce effects while transitioning.

Definition 1 (Effectful machine).

An effectful machine, taking inputs on X and producing outputs in Y, is a tuple (U,i,f) consisting of a state object Ustr, an initial state istr(I;U), and a transition morphism, fstr(U[X];U[Y]), where [X] denotes the strictification of the object X. 𝖬𝖾𝖺𝗅𝗒(X;Y) denotes the set of effectful machines with inputs in X and outputs in Y.

When the transition morphism and the initial state both belong to or to 𝕍, we call the Mealy machine monoidal or cartesian, respectively. Cartesian, monoidal and effectful Mealy machines form an effectful triple [3, Proposition III.3].

Homomorphisms of Mealy machines are value morphisms between the state spaces that do not alter the input-output behaviour.

Definition 2.

A homomorphism between two effectful machines (U,i,f),(V,j,g)Mealy(X;Y), is a cartesian morphism α𝕍str(U;V) that satisfies Equation 1.

(1)

Homomorphisms of Mealy machines determine bisimulations, and bisimilarity is the equivalence relation generated by them [3, Proposition III.6].

Definition 3.

A bisimulation between two effectful machines, M and N, with the same input and output types is a zig-zag of homomorphisms, (αi:MiNi)i=0k and (βi:Mi+1Ni)i=0k1,

M0α0N0β0M1α1βk1MkαkNk,

where M0=M and Nk=N. Two effectful machines, M and N, are bisimilar, written MN, if there is a bisimulation between them.

Bisimilarity has a universal property: effectful Mealy machines quotiented by bisimilarity form the free uniform feedback structure over the effectful triple (𝕍,,) [3, Theorem III.19].

Example 4 (Coalgebraic Mealy machines [3, Section III.C]).

A strong monad, T:𝕍𝕍, has a premonoidal Kleisli category, 𝗄𝗅(T), and we will usually consider the effectful triple (𝕍,,𝗄𝗅(T)), for some monoidal subcategory of 𝗄𝗅(T). Effectful machines on (𝕍,,𝗄𝗅(T)) with inputs in X and outputs in Y are coalgebras for the endofunctor 𝖬T:𝕍𝕍 defined by 𝖬T=(T(×Y))X, together with an initial state: indeed, a transition morphism f𝗄𝗅(T)(UX;UY) is, equivalently, a coalgebra f^:U(T(U×Y))X [18]. Similarly, machine homomorphisms are 𝖬T-coalgebra homomorphisms preserving initial states.

In particular, the transition morphisms of deterministic, non-deterministic and probabilistic machines can be expressed as coalgebras for 𝐒𝐞𝐭-endofunctors. Moreover, the notions of bisimilarity for the machines in Figure 1 in the literature [18, 19, 1, 15, 2, 6] coincide with the effectful bisimilarity of Definition 3 ([3, Corollaries III.13 and III.15]).

Deterministic U×XU×Y
Partial U×XU×Y+1
Non-deterministic U×X𝒫(U×Y)
Probabilistic [1] U×X𝒟(U×Y)
Quantum [7, 5] U×X𝖰(U×Y)
Shared state U×X(U×Y×S)S
Figure 1: Machines for different 𝐒𝐞𝐭-monads.

Coalgebraic traces

The behaviour of effectful Mealy machines is a mapping to a final coalgebra that contains the possible execution traces. Effectful Mealy machines form an effectful triple, so their traces do too and the behaviour is functorial.

An execution trace of a machine takes a stream of inputs to a stream of outputs obtained by running it. Thus, execution traces of effectful Mealy machines are effectful stream transducers, or effectful streams (Definition 7). These are streams of morphisms, as in Definition 5, quotiented by an equivalence relation that allows local transformations of the memory (Definition 6).

Definition 5 (Raw effectful stream).

A raw effectful stream, frawStream(𝐗;𝐘), with inputs in 𝐗=(X,0X. . .1) and outputs in 𝐘=(Y,0Y,1. . .) is coinductively defined as a tuple consisting of

  • Mf𝗈𝖻𝗃, the memory;

  • f(𝐗;Mf𝐘), the head;

  • f+rawStream(Mf𝐗+;𝐘+), the tail.

We depict effectful streams as in the following diagram, interpreting that each step in the stream has its own runtime.

[Uncaptioned image]

Tensoring of an object is coinductively defined by (M𝐗)=M𝐗 and (M𝐗)+=𝐗+. Tensoring extends analogously to morphisms [3, Definition IV.2].

Definition 6.

Stream dinaturality, (), is the least equivalence relating two streams (Mf,f,f+)(Mg,g,g+), whenever there exists a pure morphism r(Mg;Mf) such that g(rid)=f and rf+g+ (Equation 2).

[Uncaptioned image] (2)
Definition 7.

An effectful stream, fStream(𝐗;𝐘), with inputs in 𝐗=(X,0X,1. . .) and outputs 𝐘=(Y,0Y,1. . .), is an equivalence class of raw effectful streams under stream dinaturality.

Definition 7 can be recast in coalgebraic terms. The set Stream(𝐗;𝐘) of effectful streams from 𝐗 to 𝐘 is the final fixpoint of the functor ϕ:[(ω)𝗈𝗉×ω,𝐒𝐞𝐭][(ω)𝗈𝗉×ω,𝐒𝐞𝐭] defined by

ϕ(𝖰)(𝐗,𝐘)=M(𝐗;M𝐘)×𝖰(M𝐗+;𝐘+).

The integral sign denotes a kind of colimit, known as coend, that formalises the stream dinaturality quotient.

The effectful trace of an effectful Mealy machine is obtained by starting with the initial state and repeating the transition morphism at each time step.

Any object X in can be repeated to form a stream X, defined by X=X and X+=X. Analogously, a transition morphism f(UX;UY) can be repeated to an effectful stream f:UXY, defined as Mf=U, f=f and f+=f. The operation () attaches the initial state to the execution of f.

Definition 8.

The trace of an effectful machine (U,i,f)Mealy(X;Y) is the effectful stream defined by Trace(U,i,f)=if. Two machines are trace-equivalent if their traces coincide.

Causal traces

In some cases, effectful streams have an explicit presentation as causal processes [3, Section V]. These generalise Raney’s causal functions [17] to copy-discard categories with conditionals.

Definition 9.

For a copy-discard category (𝕍,), a causal process, f:𝐗𝐘, is a sequence of morphisms {fn:X0XnY0Yn}n in such that there exist morphisms {dn:Y0YnX0Xn+1Yn+1}n satisfying that

[Uncaptioned image]=[Uncaptioned image]. (3)

Equation 3 imposes a causality condition: the first n outputs of fn+1 can only be influenced by the first n inputs, but not by the input n+1.

Existence of conditionals is a property of a copy-discard category: it captures the notion of conditioning in categories of probabilistic processes [8, 12, 10]. Conditionals ensure that every morphism with two outputs, f:XAB, splits as its marginal fπA with some conditional c:AXB as below.

f:XABc:AXB[Uncaptioned image]=[Uncaptioned image]

Causal processes over a copy-discard category with conditionals form a monoidal category with component-wise compositions and monoidal products [3, Proposition V.7]. When the base copy-discard category, (𝕍,), additionally verifies the existence of morphism ranges [3, 9], then causal processes over (𝕍,) are monoidally isomorphic to effectful streams over (𝕍,𝖳𝗈𝗍(),) [3, Theorem V.8], where 𝖳𝗈𝗍() indicates the subcategory of of total morphisms, i.e. those that commute with the discard maps.

Examples of copy-discard categories with conditionals and ranges include the Kleisli categories of the monads described in Example 4 [3, Proposition V.5]. Causal processes in these categories give explicit descriptions for traces of deterministic, partial, non-deterministic and probabilistic Mealy machines.

We conclude with a novel characterisation of causal processes as lax-natural transformations. We fix a copy-discard category (𝕍,) with conditionals. Conditionals determine a preorder-enrichment [11, Theorem 3.3] that yields the desired lax-naturality condition.

Each infinite sequence of objects 𝐗=(X0,X1,) determines a functor 𝐗¯:(,) that maps each natural number n to the object X0Xn, and each inequality nm to the projection πn,m:X0XnX0Xm given by the copy-discard structure. A causal process f:𝐗𝐘 is a lax-natural transformation from the functor 𝐗¯ to the functor 𝐘¯. Explicitly, the components of f are morphisms fn:X0XnY0Yn, and the lax-naturality condition imposes that, for nm, fnπn,mπn,mfm.

Finally, we show that lax-naturality is equivalent to the causality condition in Equation 3.

Proposition 10.

For a copy-discard category (𝕍,) with conditionals, the monoidal category of causal processes is isomorphic to the monoidal category of functors of the form 𝐗¯, for infinite sequences 𝐗 of objects in , and lax-natural transformations between them.

Proof sketch.

Lax-natural transformations f:𝐗¯𝐘¯ and g:𝐘¯𝐙¯ compose sequentially. With the identity natural transformation, they form a category. Lax-natural transformations also have a monoidal product defined component-wise, which makes them a monoidal category.

Finally, we prove that lax-naturality is equivalent to the causality condition (Equation 3). For two morphisms f,g:XY in a copy-discard category with conditionals, the preorder fg is defined by the existence of r:YXI satisfying

[Uncaptioned image]=[Uncaptioned image].

Consider a causal process f:𝐗𝐘. By discarding the second output in Equation 3, we obtain witnesses dn[Uncaptioned image] of the inequalities fn+1πn+1,nπn+1,nfn. By transitivity, these inequalities are enough to prove that fnπn,mπn,mfm for all nm.

For the converse, consider a lax-natural transformation f:𝐗¯𝐘¯. Lax-naturality implies that fn+1πn+1,nπn+1,nfn, which means that there are morphisms rn+1:Y0YnX0Xn+1I satisfying the equation below.

[Uncaptioned image]=[Uncaptioned image]

Using (i) conditionals and (ii) lax-naturality, we find morphisms dn+1:Y0YnX0Xn+1Yn+1 that satisfy the causality condition (Equation 3).

[Uncaptioned image]=(i)[Uncaptioned image] =(ii)[Uncaptioned image]
=[Uncaptioned image]

Thus, we can take dn+1=[Uncaptioned image](rn+1cn+1).

We believe that this more abstract characterisation of causal processes, other than simplifying some proofs, may shed some light on the nature of processes that evolve in time respecting a causality condition.

References

  • [1] Christel Baier and Joost-Pieter Katoen. Principles of model checking. MIT Press, 2008.
  • [2] Richard Blute, Josée Desharnais, Abbas Edalat, and Prakash Panangaden. Bisimulation for labelled Markov processes. In Proceedings of Twelfth Annual IEEE Symposium on Logic in Computer Science, pages 149–158, 1997. doi:10.1109/LICS.1997.614943.
  • [3] Filippo Bonchi, Elena Di Lavore, and Mario Román. Effectful Mealy machines: bisimulation and trace. In Proceedings of the 40th Annual ACM/IEEE Symposium on Logic in Computer Science (to appear), 2025.
  • [4] Filippo Bonchi, Elena Di Lavore, and Mario Román. Effectful Mealy machines: bisimulation and trace (extended version), 2025. arXiv:2410.10627.
  • [5] Lorenzo Ceragioli, Elena Di Lavore, Giuseppe Lomurno, and Gabriele Tedeschi. A coalgebraic model of quantum bisimulation, 2024. to appear in the preceedings of Applied Category Theory 2024.
  • [6] Lorenzo Ceragioli, Fabio Gadducci, Giuseppe Lomurno, and Gabriele Tedeschi. Quantum bisimilarity via barbs and contexts: Curbing the power of non-deterministic observers. Proceedings of the ACM on Programming Languages, 8(POPL):1269–1297, 2023. doi:10.1145/3632885.
  • [7] Lorenzo Ceragioli, Fabio Gadducci, Giuseppe Lomurno, and Gabriele Tedeschi. Effect semantics for quantum process calculi, 2024. to appear in the preceedings of CONCUR 2024.
  • [8] Kenta Cho and Bart Jacobs. Disintegration and bayesian inversion via string diagrams. Mathematical Structures in Computer Science, 29(7):938–971, 2019. doi:10.1017/S0960129518000488.
  • [9] Elena Di Lavore, Giovanni de Felice, and Mario Román. Monoidal streams for dataflow programming. In Proceedings of the 37th Annual ACM/IEEE Symposium on Logic in Computer Science, LICS ’22, New York, NY, USA, 2022. Association for Computing Machinery. doi:10.1145/3531130.3533365.
  • [10] Elena Di Lavore and Mario Román. Evidential decision theory via partial Markov categories. In 2023 38th Annual ACM/IEEE Symposium on Logic in Computer Science (LICS), pages 1–14, 2023. doi:10.1109/LICS56636.2023.10175776.
  • [11] Elena Di Lavore, Mario Román, Paweł Sobociński, and Márk Széles. Order in partial Markov categories. In Proceedings of the 41st Conference on Mathematical Foundations of Programming Semantics (to appear), 2025.
  • [12] Tobias Fritz. A synthetic approach to Markov kernels, conditional independence and theorems on sufficient statistics. Advances in Mathematics, 370:107239, 2020.
  • [13] Alan Jeffrey. Premonoidal categories and a graphical view of programs, 1997.
  • [14] Piergiulio Katis, Nicoletta Sabadini, and Robert F. C. Walters. Bicategories of processes. Journal of Pure and Applied Algebra, 115(2):141–178, 1997. doi:10.1016/S0022-4049(96)00012-6.
  • [15] Kim Guldstrand Larsen and Arne Skou. Bisimulation through probabilistic testing. In Conference Record of the Sixteenth Annual ACM Symposium on Principles of Programming Languages, Austin, Texas, USA, January 11-13, 1989, pages 344–352. ACM Press, 1989. doi:10.1145/75277.75307.
  • [16] John Power and Edmund Robinson. Premonoidal categories and notions of computation. Math. Struct. Comput. Sci., 7(5):453–468, 1997. doi:10.1017/S0960129597002375.
  • [17] George N Raney. Sequential functions. Journal of the ACM (JACM), 5(2):177–180, 1958. doi:10.1145/320924.320930.
  • [18] Jan Rutten. Universal coalgebra: a theory of systems. Theoretical Computer Science, 249(1):3–80, 2000. Modern Algebra. doi:10.1016/S0304-3975(00)00056-6.
  • [19] Sam Staton. Relating coalgebraic notions of bisimulation. In Alexander Kurz, Marina Lenisa, and Andrzej Tarlecki, editors, Algebra and Coalgebra in Computer Science, Third International Conference, CALCO 2009, Udine, Italy, September 7-10, 2009. Proceedings, volume 5728 of Lecture Notes in Computer Science, pages 191–205. Springer, 2009. doi:10.1007/978-3-642-03741-2_14.