{'fraction_non_alphanumeric': 0.03805401505929032, 'fraction_numerical': 0.007168943595292844, 'mean_word_length': 4.2102419544280005, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Cospans and spans of graphs: a categorical algebra for the sequential and parallel composition of discrete systems

We develop further the algebra of cospans and spans of graphs introduced by Katis, Sabadini and Walters for the sequential and parallel composition of processes, adding here data types.


Introduction
This paper develops further the algebra for the sequential and parallel composition of systems introduced in the two papers [9], [11]. Whereas those papers dealt with the finite state control, here we add data structures. As in [11] the sequential composition is a cospan composition, the parallel a span composition.
The plan of the paper is as follows. We begin with the most abstract notion of a system with sequential and parallel interfaces. In section 3 we make simplifying assumptions arriving in the section 4 with an algebra that is, in effect, an implementable programming language of systems. The reader should be aware that the word system has an increasingly specific meaning in successive sections of the paper. The motive for proceeding in this way is to show that what may appear as arbitrary and unmotivated in section 4 actually arises in a natural way from general considerations. In addition, for some applications a different set of simplifying assumptions may be more appropriate. As examples of programs in the language we indicate in section 5 how sequential programming, classical concurrency examples, hierarchy and change of geometry may be expressed.
An important element of this paper is the matrix calculus which arises from the fact that categories of spans in an extensive category [2] have direct sums. It allows an explicit relation between programs with data types, and finite automata which express the control structure of the program.
Another important element is the role of the distributive law in various roles, including flattening hierarchy.
The work has been influenced by our study in [4], [5] of probabilistic and quantum automata.
In this paper we concentrate on the operations of the algebra, and its expressivity, rather than the equations satisfied.

Systems with sequential and parallel interfaces
We represent systems by (possibly infinite) graphs of states and transitions, to which we will be adding extra struture. By a graph G we mean here a set of states states(G) a set of transitions transitions(G) and two functions source, target : transitions(G) → states(G) which specify the source state and target state of a transition.

Sequential interfaces
In order to compose sequentially one system with another both systems must have appropriate interfaces. The idea comes from the sequential composition of automata, which occurs for example in Kleene's theorem: certain states (final states) of one automata are identified with certain states (initial states) of another. Here we replace initial and final states by graph morphisms into the graph of the system. Definition 1 A system with sequential interfaces is a cospan γ 0 : A → G ← B : γ 1 of graphs. The graph G is the graph of the system; A, and B are the graphs of the interfaces. We write this also as (G, γ 0 , γ 1 ) : A → B or even just as G A B . Composition of systems is by pushout. The category of systems with sequential interface is Cospan(Graph). A behaviour of G A B is a path in the central graph G.
Notice that in speaking of the category of cospans we should consider cospans only up to an isomorphism of the central graph of the cospan. In practice we will always consider representative cospans, and any equation we state will be true only up to isomorphism. The same proviso should be applied to our discussion later of spans, and systems.

Parallel interfaces
Similarly, to compose in communicating parallel two systems each system must have a parallel interface. The idea here comes, for example, from circuits. A circuit component has a physical boundary and transitions of the circuit component produce transitions on the physical boundary. Joining two circuit components, the transitions of the resulting system are restricted by the fact that the transitions on the common boundary must be equal. We describe the relation between transitions of the system G and the transitions on a boundary X by a graph morphism G → X. To obtain a category when we compose we require that a system has two parallel interfaces. Definition 2 A system with parallel interfaces is a span ∂ 0 : X ← G → Y : ∂ 1 of graphs. The graph G is the graph of the system; X, and Y are the graphs of the interfaces. We write this also as (G, ∂ 0 , ∂ 1 ) : X → Y or even just as G X,Y . Composition of systems is by pullback. The category of systems with sequential interface is Span(Graph). A behaviour of G X,Y is a path in the central graph G.

Combined sequential and parallel interfaces
Definition 3 A system with sequential and parallel interfaces consists of a commutative diagram of graphs and graph morphisms or more briefly, when we are not emphasizing the corner graphs, as We denote such a system very briefly as G X Y ;A,B , or even G X Y or G A,B or even just G, depending on the context. A behaviour of G X Y ;A,B is a path in the central graph G. Another useful notation is as follows: given an object O in the diagram we denote the four adjacent objects by O → , O ← , O ↓ and O ↑ ; for example, G ←↑ = G ↑← = G 0 .
Such a system may be regarded in two ways: (i) as three systems with parallel interfaces, the first X G0,G1 and third Y G2,G3 being sequential interfaces to the second G A,B ; or (ii) as three systems with sequential interfaces, two (A G0 G2 , B G1 G3 ) being parallel interfaces to the other (G X Y ). The point is that to compose in parallel a system with sequential interfaces requires that the sequential interfaces also have parallel interfaces. It is not necessary that the parallel interfaces themselves have parallel interfaces, since interfaces are identified, not composed, in the composition. A similar remark applies to sequential composition. Notice that for simplicity we have used the same symbols γ 0 , γ 1 for all the sequential interface morphisms and similarly ∂ 0 , ∂ 1 for all the parallel interface morphisms. Of course, certain corner graphs of G and H are required to be the same. This applies also in the next definition.
Definition 5 Two systems G X Y ;A,B and K Y Z;D,E admit a compositions by pushout, the sequential (or vertical) composition, denoted G X Y ;A,B • K Y Z;D,E .
satisfying appropriate (lax monoidal) coherence equations, which however is not in general an isomorphism. This reflects the fact that the left-hand expression involves more synchronization than the right.
is formed by taking the product of all the objects and arrows in G with the corresponding objects and arrows in the H; briefly C,D is formed by taking the sum of all the objects and arrows in G with the corresponding objects and arrows in the H; briefly The last part of the algebra of systems consists of a number of constants.

Definition 9
The constants of the algebra are systems constructed from the constants of the distributive category structure of Sets [18], [13], [6].
When we describe in a later section a programming language there will be of course also as constants the operations of data types; the particular language we describe has the natural numbers together with predecessor and successor.

Simplifying Assumptions
We introduce a number of simplifying assumptions with the aim of arriving at a implementable programming language for systems. As we do this we will be considering also certain important derived operations of the algebra.

Simplifying the interfaces
Assumption 1. We assume from now on that in a system with sequential and parallel interfaces G as described above the corner graphs G ←↑ , G →↑ , G ←↓ , G →↓ each have one state and no transitions, that the graphs A, B each have one state, and that the graphs X, Y have no transitions.
The idea is that in many cases the sequential interface consists only of states with no transitions, whereas the parallel interfaces are "stateless", that is, consist of transitions and one state. The assumptions are appropriate for message passing communication but not for systems in which there is communication by shared variables, since this requires that the parallel interfaces have state. It is not difficult to make assumptions for this type of communication but we prefer here to make the simpler assumption.
Given the assumption we may ignore the corner graphs of a system so that it consists of five graphs G, A, B, X, Y and the four graph morphisms Since the single states of A and B need not have a name, we may sometimes confuse A and B with transitions(A) and transitions(B) respectively. We may think of A, B, X, Y as sets, and of A and B as labels for the transitions in G (the graph morphisms ∂ 0 , ∂ 1 providing the labelling). As a consequence of the simple form of the corner graphs of a system we have the following result.

Proposition 10
The parallel composite G X Y ;A,B ||H Z W ;B,C of two systems has top sequential interface X × Z, and bottom sequential interface Y × W ; we can summarize this by the formula C,D has left parallel interface the graph with one vertex and transitions transitions(A) + transitions(C) which we denote with some abuse of notation as A + C, and similarly right parallel interface B + D; we can summarize this by the formula Notice that the class of systems we are considering is closed under sequential and parallel composition and product, but is not closed under the operation of sum since the resulting system will have parallel interfaces with two states, not one.
We now introduce two derived operations similar to the sequential composite and the sum, but which are local in the sense that the parallel interfaces are fixed. Intuitively they are sequential operations within a fixed parallel protocol.
by composing with appropriate codiagonals as follows: where the codiagonals ∇ : A + A → A, ∇ : B + B → B are codiagonals on transitions, but the identity on the single state.
C,D by composing with appropriate codiagonals as follows: Now the class of systems we are now considering is closed under the operations of parallel and sequential composition, product, local sequential and local sum.

Finiteness assumptions
In general, pushouts and pullbacks of infinite graphs are not implementable. We need to make some finiteness assumptions.
Assumption 2. We assume that in system G A B;X,Y that A and B have a finite number of transitions.
This assumption means that the pullbacks in the parallel composition are implementable. A further consequence of this assumption is that the transitions of the graph G decompose as a disjoint union where transitions(G) a,b is the set of transitions labelled by a ∈ A, b ∈ B. Denote by G a,b the graph with the same states as G but with transitions transitions(G) a,b .
The next assumption will have the effect that our systems have a finite state automata as control structure. Usually finite state automata are presented as recognizers of regular languages [7]. However the original work of McCulloch and Pitts [14] introduced automata as systems with thresholds, that is systems with infinite state spaces which decomposed into finite sums. Our finiteness assumptions are of this nature.
The first effect of this is that each of the graphs G a,b (a ∈ A, b ∈ B) breaks up as a matrix of spans of sets.
To see this notice that a graph G is just an endomorphism in Span(Sets). Further the category Span(Sets) has direct sums, the direct sum of U and V being U + V with injections the functions i X : U → U + V , i Y : V → U + V considered as spans. and projections the same functions but now considered as the opposite spans i op The commutative monoid structure on Span(Sets)(U, V ) is given by sum and the empty span. Since a graph is just an endomorphism in Span(Sets) a graph G whose state set is U + V may be represented as a 2 × 2 matrix of spans Generalizing this to the case in which the states break up into a disjoint sum of n subsets Assumption 3 implies that each of the graphs G a,b may be represented as a k × k matrix of spans, the i, jth entry of which we will denote G a,b,Ui,Uj , or even G a,b,i,j . It has a simple meaning: G a,b,Ui,Uj is the set of transitions of G labelled a, b whose sources lie in U i and whose targets lie in U j. The projections of the span G a,b,Ui,Uj are the projections onto the sources and targets.
It is easy also to expand the matrix to include the functions γ 0 : X → G, γ 1 : X → G. The resulting matrix has columns indexed by X 1 , X 2 , · · · , X k , U 1 , U 2 , · · · , U l and rows indexed by Y 1 , Y 2 , · · · , Y l , U 1 , U 2 , · · · , U m ; as an example when k = l = m = 2 the matrix has the form where 0 denotes the empty span.
Example 13 The function predecessor : N → N + 1 which returns an error if the argument is 0 but otherwise decrements, may be considered as a system with trivial parallel interfaces, top sequential interface N bottom sequential interface N + 1 and central graph having states N + N + 1, transitions N and source : (This is the usual picture of a function as a graph on the disjoint union of the domain and codomain, with edges relating domain elements and their images,) We call this system pred. The matrix is where 0 denotes the empty span and 1 denotes the identity span. The span pred N,1 is the partial function which returns error on zero, and the span pred N,N is the partial function returning n − 1 for n > 0.
We describe next a derived operation which is a minor modification of the parallel composition, in order to simplify the matrix version of the parallel composition. The mathematical fact behind the derived operation is this: in a symmetric monoidal category with direct sums, in which the tensor product distributes over the direct sums, if two arrows are represented as matrices, then via distributivity isomorphisms the matrix of the tensor product of two arrows is a tensor product of the matrices of the arrows. The precise distributivity isomorphism needs to be specified since there are many possible, resulting in different ordering of the rows and columns of the tensor product matrix.

Definition 14 Distributed parallel.
Given Composing on the top and bottom interfaces with distributivity isomorphisms we obtain a system with left interface A, right interface C, top interface X 1 ×Z 1 +X 2 ×Z 1 +· · ·+X k ×Z k ′ and bottom interface Y 1 × W 1 + Y 2 × W 1 + · · · + Y l × W l ′ The set of states of G||H may similarly be distributed to have the form We will, with an abuse of notation, denote this resulting system also as G||H.
Definition 15 Distributed product. Given Composing on the top and bottom interfaces with distributivity isomorphisms we obtain a system with left interface A×C, right interface B ×D, top interface X 1 ×Z 1 +X 2 ×Z 1 +· · ·+ X k ×Z k ′ and bottom interface Y 1 ×W 1 +Y 2 ×W 1 +· · ·+Y l ×W l ′ The set of states of G||H may similarly be distributed to have the form We will, with an abuse of notation, denote this resulting system also as G × H.
The last assumption we make has the consequence that the pushout in sequential composition is done a the level of control, not of data, and is therefore implementable.
Assumption 4. We assume that in the matrix of the system G A B;X,Y that the entries involving the sequential interfaces are either the identity span 1 or the empty span 0.

Automaton representation
Of course the matrix for G X Y,;A,B has a geometric representation as a labelled automaton, with top sequential interfaces X 1 , X 2 , · · · , X k , bottom sequential interfaces Y 1 , Y 2 , · · · , Y l , and vertices which are labelled by the sets U i and for each a ∈ A, b ∈ B edges from U i to U j labelled G a,b,Ui,Uj . As usual we will omit edges labelled with empty spans. This representation has advantages both technical and conceptual, but is less easy to typeset. We give one example, namely the automaton representation of the predecessor system described above, which however has trivial parallel interfaces.We will see further examples in section 5.

The programming language Cospan-Span
The idea of this section is to restate the notion of system we have developed, and describe the operations on systems. The reader should compare the notions described here with those described in [11] where finite state systems were considered. We describe the programming language at the same time as its semantics. The programs are the expressions in the operations and constants; an execution of a program is a path in the graph described by the expression.

Systems
Definition 16 A system G consists of (i) two finite sets A, B called the left and right parallel interfaces on G;(ii) two families of possibly infinite sets X = X 1 , X 2 , · · · , X k and Y = Y 1 , Y 2 , · · · , Y l called the top and bottom sequential interfaces; (iii) a family of possibly infinite sets U = U 1 , U 2 , · · · , U m which together constitute the internal state space of G; (iv) two functions ϕ : {1, 2, · · · , k} → {1, 2, · · · m} and ψ : {1, 2, · · · , l} → {1, 2, · · · m} called the inclusions of the sequential interfaces, with the properties that X i = U ϕ(i) and Y i = U ψ(i) ; (v) a family of spans of sets G a,b,i,j : U i → U j (a ∈ A, b ∈ B, i ∈ {1, 2, · · · , m}, j ∈ {1, 2, · · · , m} which together constitute a family of graphs G a,b (a ∈ A, b ∈ B) each with vertex set U 1 + U 2 + · · · + U m . The graph G a,b is the graph of transitions of the system when the "signals a, b occur on the parallel interfaces". We denote the system as G X Y ;A,B (U ).
It is easy to see that this is the essential concrete content of the notion of system developed in the previous section.

Operations on systems, and constants
In the following we denote families by giving a typical element.
inclusions of sequential interfaces ϕ G×H = ϕ G × ϕ H and ψ G×H = ψ G × ψ H , and finally the spans Ignoring the sequential interfaces, the matrix of the distributed product is just the tensor product of the matrices of the components.
inclusions of sequential interfaces ϕ G×H = ϕ G × ϕ H and ψ G×H = ψ G × ψ H , and finally the spans inclusions of sequential interfaces ϕ G and ψ H , and finally the spans

Programs
In our view programming languages should be presented by first describing an algebra of systems. Then programs are elements of the free algebra of the same type, generated by some basic systems. The meaning of the program is then the evaluation in the concrete algebra. The programs of the Cospan-Span language are expressions in the operations and constants of the algebra described above, and the following basic systems: pred N N +1 , succ N +1 N (defined similarly to pred N N +1 ). The evaluation of a program is a system; a behaviour is a path in the central graph of the system.

Concluding remarks
We intend in later papers to fill out details of matters sketched here, but in fact, if one examines the previous investigations in this project it will be clear that many matters discussed at the level of finite state control may now be lifted to include also data.

Turing completeness
It is not difficult to relate the Elgot automata introduced in [18], [8], [10] to the algebra of cospans of graphs. It was shown in [17] that Elgot automata based on the elementary operations of predecessor and successor for natural number are Turing complete, and hence also the algebra of this paper. We give an example which illustrates sequential programming in Cospan-Span. All the systems in the following have trivial parallel interface. In the following we use the following constants definable from distributive category operations, considered as systems with trivial parallel interface in which the centre graph has no transitions (in which case a system reduces to a span of sets):

Example 22
The following is a program which, commencing in a state of the top sequential interface, computes addition of two natural numbers, terminating in the lower interface: The system described by the program is: where p N,1 , p N,N are the partial functions arising from predecessor : N → 1+N , and s is the successor function.

Classical problems of concurrency
We have described elsewhere ( [11], [10], [9]) how in Span(Graph) classical problems of concurrency may be modelled, at the level of finite state abstraction, which is the appropriate level for controlling many properties. The current work shows how these descriptions may be extended to include also operations on the data types.
We give a simple example of a parallel composite of two systems P and Q. P has trivial left interface, and right interface {ǫ, a} whereas Q has trivial right interface and left interface {ǫ, a}. The combined system may be represented by the diagram (analogous to those [9]), in which the first part of a label is the span of sets, and the second part is the label on the parallel interface. The left system is P and the right Q. The system P repeatedly applies f and then a test t 1 until the test results false, and then P may idle, eventually (in the Italian sense) synchronizing with Q on the signal a. After this P repeats the whole sequence. Q does the same, but with a different function g and a different test t 2 , and seeks to synchronize with P .
Each of P and Q may be described by a Cospan-Span program in a similar way to the addition program above.

Hierarchy
There is an obvious relevance to hierarchical systems of the fact that systems in this algebra may be constructed by repeated parallel and sequential operations, with analogies to state charts.

Change of geometry
Already in [11] we discussed the description of changing geometry using sequential operations on parallel systems. However in that paper we considered only the local sequential composition, whereas in this paper we have a general sequential operation, which allows change of geometry with a change of parallel protocol. In that article we abstracted away data.
Studying [4] and [5] the reader will note similarities with the algebra here. In fact, this paper is the result of comparing [9] with [4] and [5].