Effectful Mealy Machines:
Coalgebraic and Causal Traces
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 categoriesCategory:
Invited TalkFunding:
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.Copyright and License:
2012 ACM Subject Classification:
Theory of computation Categorical semantics ; Theory of computation Abstract machinesEditors:
Corina Cîrstea and Alexander KnappSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
Introduction
Mealy machines have a transition behaviour that takes the current state in and an input in to transition to the next state and produce an output in . 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 and producing outputs in , is a tuple consisting of a state object , an initial state , and a transition morphism, , where denotes the strictification of the object . denotes the set of effectful machines with inputs in and outputs in .
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 , is a cartesian morphism 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, and , with the same input and output types is a zig-zag of homomorphisms, and ,
where and . Two effectful machines, and , are bisimilar, written , 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, , has a premonoidal Kleisli category, , and we will usually consider the effectful triple , for some monoidal subcategory of . Effectful machines on with inputs in and outputs in are coalgebras for the endofunctor defined by , together with an initial state: indeed, a transition morphism is, equivalently, a coalgebra [18]. Similarly, machine homomorphisms are -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]).
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, , with inputs in and outputs in is coinductively defined as a tuple consisting of
-
, the memory;
-
, the head;
-
, the tail.
We depict effectful streams as in the following diagram, interpreting that each step in the stream has its own runtime.
Tensoring of an object is coinductively defined by and . Tensoring extends analogously to morphisms [3, Definition IV.2].
Definition 6.
Stream dinaturality, , is the least equivalence relating two streams , whenever there exists a pure morphism such that and (Equation 2).
| (2) |
Definition 7.
An effectful stream, , with inputs in and outputs , is an equivalence class of raw effectful streams under stream dinaturality.
Definition 7 can be recast in coalgebraic terms. The set of effectful streams from to is the final fixpoint of the functor defined by
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 in can be repeated to form a stream , defined by and . Analogously, a transition morphism can be repeated to an effectful stream , defined as , and . The operation attaches the initial state to the execution of .
Definition 8.
The trace of an effectful machine is the effectful stream defined by . 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, , is a sequence of morphisms in such that there exist morphisms satisfying that
| (3) |
Equation 3 imposes a causality condition: the first outputs of can only be influenced by the first inputs, but not by the input .
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, , splits as its marginal with some conditional as below.
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 determines a functor that maps each natural number to the object , and each inequality to the projection given by the copy-discard structure. A causal process is a lax-natural transformation from the functor to the functor . Explicitly, the components of are morphisms , and the lax-naturality condition imposes that, for , .
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 and 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 in a copy-discard category with conditionals, the preorder is defined by the existence of satisfying
Consider a causal process . By discarding the second output in Equation 3, we obtain witnesses of the inequalities . By transitivity, these inequalities are enough to prove that for all .
For the converse, consider a lax-natural transformation . Lax-naturality implies that , which means that there are morphisms satisfying the equation below.
Using (i) conditionals and (ii) lax-naturality, we find morphisms that satisfy the causality condition (Equation 3).
Thus, we can take .
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.
