A Structural and Nominal Syntax for Diagrams
BBob Coecke and Aleks Kissinger (Eds.):14th International Conference on Quantum Physics and Logic (QPL)EPTCS 266, 2018, pp. 71–83, doi:10.4204/EPTCS.266.4 c (cid:13)
D. R. Ghica and A. LopezThis work is licensed under theCreative Commons Attribution License.
A Structural and Nominal Syntax for Diagrams
Dan R. Ghica
University of Birmingham
Aliaume Lopez
ENS Cachan, Universit´e Paris-Saclay
The correspondence between monoidal categories and graphical languages of diagrams has beenstudied extensively, leading to applications in quantum computing and communication, systems the-ory, circuit design and more. From the categorical perspective, diagrams can be specified using(name-free) combinators which enjoy elegant equational properties. However, conventional nota-tions for diagrammatic structures, such as hardware description languages (VHDL, V
ERILOG ) orgraph languages (D OT ), use a different style, which is flat, relational, and reliant on extensive use ofnames (labels). Such languages are not known to enjoy nice syntactic equational properties. How-ever, since they make it relatively easy to specify (and modify) arbitrary diagrammatic structuresthey are more popular than the combinator style. In this paper we show how the two approachesto diagram syntax can be reconciled and unified in a way that does not change the semantics andthe existing equational theory. Additionally, we give sound and complete equational theories for thecombined syntax. Graphs and their visual representations (diagrams) are an appealing way of describing many kinds ofsystems, in particular circuits. Work originated in the study of quantum computation [1] has exploitedthe connection between various classes of graphs and monoidal categories, going back to the seminalwork of Joyal and Street [14], to add a layer of structure which makes reasoning about and with dia-grams not just intuitive but also mathematically rigorous. Subsequently this connection was extendedin many surprising and interesting directions, from computational linguistics [17], to modelling sig-nal flow [4] and synchronous [9] or asynchronous [8] circuits. New automated reasoning tools basedon diagrams, rather than the usual linear algebraic syntax, are a particularly exciting development (see http://globular.science/ ). This convenient and elegant interplay between the dual categorical anddiagrammatic methods are by now a mature and rich area of research [18].Although these developments convincingly establish the usefulness of categorical diagrammatics inreasoning about many kinds of systems, we note that little has been suggested in terms of a workablesyntax which is conceptually compatible with it, but also with conventional notations, which are unstruc-tured and relational. Examples of the latter are hardware description languages such as V
ERILOG andVHDL, or graph languages such as D OT (see ). Indeed, in the literaturethe categorical combinators are usually taken as an implicit diagram syntax. Whereas such a syntax is ex-pressive enough to describe the desired classes of graphs, it is often not as convenient as the alternatives.Although categorical combinators can be elegant and succinct in certain situation, point-free languagesof combinators generally hold little appeal as concrete syntax (e.g. APL).Conventional diagram syntax lacks structure. It is a “flat” relational description of the graph. Whereasthe categorical combinators are name-free, conventional syntax uses an abundance of names, for nodesand sometimes even for edges. Although these languages are widely used, they are broadly consideredboth inelegant and unwieldy, and purely structural alternatives have been proposed [2].2 A Structural and Nominal Syntax for Diagrams
Before diving into technicalities let us consider a simple motivating example. Consider the diagrambelow, consisting of two components f and g , the first with one input and three outputs and second withthree inputs and one output, connected in the obvious way: f g In the categorical, combinator-style, specification such a diagram would be succinctly written as the composition f ; g : 1 →
1, of f : 1 → g : 3 →
1. A VHDL-style description would look more verbose: module fg(input u; output v)begincomponent f(input x1; output y1, y2, y3);component g(input z1, z2, z3; output t1);wire y1, z1; wire y2, z2; wire y3, z3; wire u, x1; wire t1, v;end
This is the diagram annotated with all the names used above: f g u vx1 y1y2y3 z1z2z3 t1
In highly structured diagrams the categorical notation is concise and elegant. However, realisticcircuits may also use arbitrary connections. Consider a variation of the circuit above: f g y1y2y3 z1z2z3x1 t1u1u2 v1v2
The flat description can be adjusted to cope with arbitrary connector reassignments: module fg(input u1, u2; output v1, v2)begincomponent f(input x1; output y1, y2, y3);component g(input z1, z2, z3; output t1);wire y1, z1; wire y3, z3; wire y2, v2; wire u2, z2; wire u1, x1; wire t1, v1;end
The categorical style description is now more intricate, requiring a mix of sequential (;) and paral-lel ( tensor , ⊗ ) composition along with the use of structural combinators such as the single connector( identity , i ) and crossing connector ( symmetry , γ ): ( f ⊗ i ) ; ( i ⊗ γ ⊗ i ) ; ( i ⊗ i ⊗ γ ) ; ( i ⊗ γ ⊗ i ) ; ( g ⊗ i ) . The combinator-style variable-free syntax is appealing for highly structured diagrams and awkwardfor arbitrary ones, whereas the flat and unstructured syntax seems useful in the case of unstructured andunnecessarily verbose for structured graphs. Without advocating one style or the other, in this paper wepresent a syntax which shows that the two are compatible. Our contributions are therefore as follows:1. Preserving and extending the equational theory.2. Possibly eliminating the need for structural combinators.3. Preserving and conserving the expressiveness of the underlying diagrams.4. Reasoning equationally in the new syntax. . R. Ghica and A. Lopez x | − (cid:96) x : 0 → | x ≤ x − | x (cid:96) x : 1 → | x ≤ x − | − (cid:96) k : m → n | /0 Γ | ∆ (cid:96) M : m → m | ≤ Γ (cid:48) | ∆ (cid:48) (cid:96) N : n → n | ≤ (cid:48) m = n Γ (cid:93) Γ (cid:48) | ∆ (cid:93) ∆ (cid:48) (cid:96) M ; N : m → n | [ ≤ ∪ ≤ (cid:48) ∪ (( Γ (cid:93) ∆ ) × ( Γ (cid:48) (cid:93) ∆ (cid:48) )] Γ | ∆ (cid:96) M : m → m | ≤ Γ (cid:48) | ∆ (cid:48) (cid:96) N : n → n | ≤ (cid:48) Γ (cid:93) Γ (cid:48) | ∆ (cid:93) ∆ (cid:48) (cid:96) M ⊗ N : m + n → m + n | [ ≤ ∪ ≤ (cid:48) ] Γ | ∆ (cid:96) M : m → m | ≤ [ ≤ ∪ { ( x , y ) } ] is a partial order x , y ∈ Γ (cid:93) ∆Γ | ∆ (cid:96) M : m → m | [ ≤ ∪ { ( x , y ) } ] Γ , x | ∆ , y (cid:96) M : m → m | ≤ x ≤ y Γ | ∆ (cid:96) xy . M : m → m | ≤ \ { x , y } Figure 1: Uniflow diagrams typing rules
A PROP (abbreviation of products and permutations ) is a strict symmetric monoidal category whereevery object is a natural number [11]. We give a graph semantics of PROPs based on Kissinger’s framedpoint graphs [15]. Let a labelled directed acyclic graph (DAG) be a DAG ( V , E ) equipped with a partialinjection f : V (cid:42) L and a relation E ⊆ V such that the transitive and reflexive closure of E is a partialorder on V . Let a labelled interfaced DAG (LIDAG) be a labelled DAG with two distinguished lists ofunlabelled nodes representing the “input” and “output” ports. Unlabelled nodes are called wire nodes ,and edges connecting them are called wires . A wire homeomorphism [15, Sec. 5.2.1] is any insertionor removal of wire nodes along wires which does not otherwise change the shape of the graph. TwoLIDAGs are considered to be equivalent if they are graph isomorphic up to renaming vertices and wirehomeomorphisms. The quotienting of LIDAGs by this equivalence gives us framed point DAGs [15, Def.5.3.1], which we will call uniflow diagrams . We give a syntax for uniflow diagram as follows: M :: = k | i | γ | M ; M | M ⊗ M | x | xy . M , where k ∈ K are the constants and x variables. The language is essentially that of symmetrical monoidalcategorical combinators (identity, symmetry, composition, tensor) over a signature, extended with vari-ables and a binding construct we read as link x and y in M . We equip this language with a type system,where judgements are of the form Γ | ∆ (cid:96) M : m → n | R . Γ is a set of input variables , ∆ of output vari-ables and R a partial order on their (disjoint) union, called the anchor of the diagram (Fig. 1). The anchorrelation is a syntactic discipline used to prevent the inadvertent introduction of cycles. Note that i : 1 → γ : 2 → m → n represents a uniflow diagramwith m (unlabelled) inputs and n (unlabelled) outputs. Given a relation R we denote by [ R ] its transitiveand reflexive closure. For any set S , we define R \ S = { ( x , y ) ∈ R | x , y (cid:54)∈ S } . We give a concrete graph-theoretic semantics of the uniflow diagram. We may write Γ | ∆ (cid:96) M if Γ | ∆ (cid:96) M : m → n | R for some m , n ∈ N and some partial order R . The semantics is defined by inductionon the typing derivation. The meaning of a judgement Γ | ∆ (cid:96) M : m → n | R is a uniflow diagram (cid:0) V , I , O , E , f : V (cid:42) K (cid:93) A (cid:1) such that A ⊇ Γ , ∆ is a set of names and the relation R is a partial order on thevertices labelled by variables Γ (cid:93) ∆ which is compatible with E , i.e. if ( x , y ) ∈ R then there exist uniquevertices u , v such that f ( u ) = x , f ( v ) = y and there is no path in E from v back to u . We write this as (cid:74) Γ | ∆ (cid:96) M : m → n | R (cid:75) = ( V , I , O , E , f ) (cid:16) R . The structural combinators are interpreted as in Fig. 2.Let :: stand for list concatenation or cons-ing. And let the “zipping” of two lists be defined in the4
A Structural and Nominal Syntax for Diagrams (cid:74) x | − (cid:96) x : 0 → | x ≤ x (cid:75) = ( { a , b } , nil , b :: nil , { ( a , b ) } , { ( a , x ) } ) (cid:16) { ( a , a ) } = x (cid:74) − | x (cid:96) x : 1 → | x ≤ x (cid:75) = ( { a , b } , a :: nil , nil , { ( a , b ) } , { ( b , x ) } ) (cid:16) { ( b , b ) } = x (cid:74) − | − (cid:96) i : 1 → | /0 (cid:75) = ( { a , b } , a :: nil , b :: nil , { ( a , b ) } , /0 ) (cid:16) /0 = (cid:74) − | − (cid:96) γ : 2 → | /0 (cid:75) = ( { a , a (cid:48) , b , b (cid:48) } , a :: a (cid:48) :: nil , b :: b (cid:48) :: nil , { ( a , b (cid:48) ) , ( a (cid:48) , b ) } , /0 ) (cid:16) /0 = Figure 2: Interpreting structural combinatorsusual way ( zip nil nil = nil , and zip ( x :: xs ) ( y :: ys ) = ( x , y ) :: ( zip xs ys ) ). If unambiguous we may usea list to also mean the set of elements of that list.Assuming that (cid:74) Γ i | ∆ i (cid:96) M i : m i → n i | R i (cid:75) = ( V i , I i , O i , E i , f i ) (cid:16) R i , for ( i = , ) , The interpretationof the two forms of composition is: (cid:74) Γ (cid:93) Γ | ∆ (cid:93) ∆ (cid:96) M ; N : m → n | [ R ∪ R ∪ ( Γ (cid:93) ∆ ) × ( Γ (cid:93) ∆ )] (cid:75) = ( V (cid:93) V , I , O , E ∪ E ∪ ( zip O I ) , f ∪ f ) (cid:16) [ R ∪ R ∪ ( Γ (cid:93) ∆ ) × ( Γ (cid:93) ∆ )] (cid:74) Γ (cid:93) Γ | ∆ (cid:93) ∆ (cid:96) M ⊗ N : m + m → n + n | [ R ∪ R ] (cid:75) = ( V (cid:93) V , I :: I , O :: O , E ∪ E , f ∪ f ) (cid:16) [ R ∪ R ] . Note that since diagrams are defined up to relabelling nodes, these definitions are always good. In caseof name clashes nodes can be given fresh names.Constants are interpreted as below, with p i , q j labels for the input and output ports of the constant. (cid:74) − | − (cid:96) k : m → n | /0 (cid:75) = p pm q qnk The rule for strengthening the anchor is interpreted as (cid:74) Γ | ∆ (cid:96) M : m → n | [ R ∪ { ( x , y ) } ] (cid:75) = ( V , I , O , E , f ) (cid:16) [ R ∪ ( f − ( x ) , f − ( y ))] . The restriction on [ R ∪ ( f − ( x ) , f − ( y ))] being a proper partial order ensures that the interpretation of therule is well defined.Let ( f \ B (cid:48) )( x ) = f ( x ) if f ( x ) (cid:54)∈ B (cid:48) and undefined otherwise. The new construct is the link : (cid:74) Γ | ∆ (cid:96) xy . M : m → n | R \ { x , y } (cid:75) = ( V , I , O , E ∪ ( f − ( x ) , f − ( y )) , f \ { x , y } ) (cid:16) R \ { ( x , y ) } . (1)The link can be visualised as below, including the wire homeomorphism: x y . R. Ghica and A. Lopez Lemma 1 (Soundness) . Any well typed term denotes a valid uniflow diagram.
The useful connection between diagrams and monoidal categories is given by this proposition:
Proposition 2.
Given a term Γ | ∆ (cid:96) M : m → n, the diagram (cid:74) Γ | ∆ (cid:96) M : m → n (cid:75) is a morphism in thestrict free symmetric monoidal category (SMC) over signature K ∪ Γ ∪ ∆ of constants and (uninterpreted)variables. By “uninterpreted variables” we mean simply that the variables from Γ , ∆ are morphism variablesin the categorical signature with types 0 → → link construction is not definedcategorically but only concretely. However, any term (with our without link) corresponds to a diagramwhich can be described categorically. An immediate consequence of Prop. 2 is that all derivations of awell typed term produce the same uniflow diagram, possibly with a different anchoring relation. Lemma 3.
The type system of uniflow diagrams is coherent up to the anchoring relation.
Note that different derivations may require different anchoring relations, even though the underlyingdiagrams are equal as FPGs.
Lemma 4 (Definability) . Any uniflow diagram is definable just in terms of composition, tensor, link.Proof.
We introduce exactly two link nodes on each connector (a wire homeomorphism) and we labelthem with fresh variable names. Suppose there are j wires and k boxes, m inputs and n outputs. Let usdenote by u i where i = , n the labels associated with input connectors and by v i where i = , m the labelsassociated with the output connectors. Let y i , j (respectively z i , j ) be the labels of the j th input (respectivelyoutput) for each constant occurrence k i . Let (cid:126) u = (cid:78) i = , n u i ,(cid:126) v = (cid:78) i = , m v i , (cid:126) y i = (cid:78) j = , dom ( k i ) y i , j ,(cid:126) z i = (cid:78) j = , codom ( k i ) z i , j . Let M = (cid:126) u ; (cid:0) (cid:78) i = , k (cid:126) y i ; k i ; (cid:126) z i (cid:1) ; (cid:126) v . The term is M = x x (cid:48) · · · x n x (cid:48) n . M . A link x i x (cid:48) i is createdif there is an edge between the nodes labelled by x i and x (cid:48) i in M , with x i , x (cid:48) i chosen from the variablesintroduced above. M should be a closed form. It is straightforward to show that this is indeed denotesthe desired diagram. We only need to prove that this is well typed. The subterm (cid:78) i = , k (cid:126) y i ; f i ; (cid:126) z i is clearlywell typed. Moreover, we can always extend the order with any x i ≤ x (cid:48) i before linking without creatingcycles because the starting graph is a DAG.The construction in the proof is just the flat nominal notation used by conventional graph languages. Theorem 5.
For any term M there exist terms ˜ M and ˆ M such that ˜ M is link-free and ˆ M is free of structuralcombinators (identity, symmetry) and (cid:74) M (cid:75) = (cid:74) ˜ M (cid:75) = (cid:74) ˆ M (cid:75) . Example 1.
Symmetry at any type, e.g. γ , : 4 → can be defined in combinatorial or nominal style: γ , = aa (cid:48) . bb (cid:48) . cc (cid:48) . dd (cid:48) . a ⊗ b ⊗ c ⊗ d ⊗ c (cid:48) ⊗ d (cid:48) ⊗ a (cid:48) ⊗ b (cid:48) = ( i ⊗ γ ⊗ i ) ; ( γ ⊗ γ ) ; ( i ⊗ γ ⊗ i ) . Any γ m , n for m , n (cid:54) = γ , and identity id m at any non-zero type can be definedfrom i . We sometimes write the identity id m as just m . Example 2.
We can now revisit our introductory example. The most succinct description mixes variablesand structural connectors: x (cid:48) x . yy (cid:48) . ( f ⊗ y ) ; ( i ⊗ x (cid:48) ⊗ y (cid:48) ⊗ i ) ; ( g ⊗ x ) . It can be easily checked that all equational properties of the underlying PROP are carried over to thesyntax. We interpret Γ | ∆ (cid:96) M ≡ N as the equality of the uniflow diagrams represented by (cid:74) Γ | ∆ (cid:96) M (cid:75) and (cid:74) Γ | ∆ (cid:96) N (cid:75) (ignoring the anchoring order). The equations are now parametrised by the set of freevariables, as in Fig. 3, assuming integers m , n , etc. are chosen so that the terms are well-typed.6 A Structural and Nominal Syntax for Diagrams Γ | ∆ (cid:96) ( M ; M ) ; M ≡ M ; ( M ; M ) associativity of composition (2) Γ | ∆ (cid:96) id m ; M ≡ M ; id n ≡ M identity (3) Γ | ∆ (cid:96) ( M ; M (cid:48) ) ⊗ ( N ; N (cid:48) ) ≡ ( M ⊗ N ) ; ( M (cid:48) ⊗ N (cid:48) ) tensor functoriality (4) Γ | ∆ (cid:96) ( M ⊗ M ) ⊗ M ≡ M ⊗ ( M ⊗ M ) strictness (5) Γ | ∆ (cid:96) M ⊗ ≡ ⊗ M ≡ M strictness (6) Γ | ∆ (cid:96) ( M ⊗ M ) ; γ n , n ≡ γ m , m ; ( M ⊗ M ) symmetry (7) (cid:96) γ , ≡ γ , ≡ (cid:96) γ m , n + p ≡ ( γ m , n ⊗ p ) ; ( n ⊗ γ m , p ) strict symmetry (9) Γ | ∆ (cid:96) xy . M ≡ x (cid:48) y (cid:48) . M { x (cid:48) y (cid:48) / xy } x (cid:48) , y (cid:48) fresh alpha-equivalence (10) Γ | ∆ (cid:96) xy . ( M ; N ) ≡ ( xy . M ) ; N x , y (cid:54)∈ fv ( N ) scope extrusion (11) Γ | ∆ (cid:96) xy . ( M ⊗ N ) ≡ ( xy . M ) ⊗ N x , y (cid:54)∈ fv ( N ) scope extrusion (12) Γ | ∆ (cid:96) xy . ( M ; N ) ≡ M ; xy . N , x , y (cid:54)∈ fv ( M ) scope extrusion (13) Γ | ∆ (cid:96) xy . ( M ⊗ N ) ≡ M ⊗ xy . N , x , y (cid:54)∈ fv ( M ) scope extrusion (14) (cid:96) xy . x ; y ≡ xy . M isa binding construct, we expect alpha-equivalence to hold, which is reflected in a new axiom, Eqn. 10.Variable renaming M { x / y } and the set of free variables fv ( M ) are defined in the obvious way. For namemanagement we also have new equations for scope extrusion (Eqn. 11–14). Finally, the connectionbetween variables and the structural connectors, namely identity, is given in Eq. 15. Theorem 6.
The equational theory of uniflow diagrams is sound and complete for the given semantics.Proof.
The soundness is immediate. For completeness, consider two terms such that (cid:74) Γ | ∆ (cid:96) M (cid:75) = (cid:74) Γ | ∆ (cid:96) N (cid:75) . We first rename all bound variables using alpha-equivalence, then, using Eqns. 11 and 13 wemove all binders into global scope. In case the resulting terms are link-free the equation Γ | ∆ (cid:96) M ≡ N is provable because the terms denote morphism in the free strict symmetric monoidal category oversignature K extended with the uninterpreted variables Γ , ∆ (Prop. 2). Suppose that one of the terms hasan outermost link, so it has form Γ | ∆ (cid:96) xy . M . We reason diagrammatically about term Γ , x | ∆ , y (cid:96) M ,which is a morphism in the free SMC over K extended with Γ (cid:93) ∆ (cid:93) { x , y } . We can always redraw thediagram to an isomorphic diagram in which the nodes labelled by x and y are adjacent: ≈ x y x y This diagrammatic equivalence can be proved in the equational theory of the uniflow diagrams, not-ing that γ m , n and the identities at m can be defined in terms of γ and i . Moreover, the diagram on theright will correspond to a term which has subterm x ; y , with variables x , y not occurring anywhere else. . R. Ghica and A. Lopez x | − (cid:96) x : 0 → − | x , (cid:96) x : 1 → − | − (cid:96) i : 1 → − | − (cid:96) γ : 2 → Γ | ∆ (cid:96) M : m → m Γ (cid:48) | ∆ (cid:48) (cid:96) N : n → n m = n Γ (cid:93) Γ (cid:48) | ∆ (cid:93) ∆ (cid:48) (cid:96) M ; N : m → n Γ | ∆ (cid:96) M : m → m Γ (cid:48) | ∆ (cid:48) (cid:96) N : n → n Γ (cid:93) Γ (cid:48) | ∆ (cid:93) ∆ (cid:48) (cid:96) M ⊗ N : m → n Γ | ∆ (cid:96) M : m → m Γ | ∆ (cid:96) Tr i ( M ) : m − i → m − i Γ , x | ∆ , y (cid:96) M : m → m Γ | ∆ (cid:96) xy . M : m → m Figure 4: Type system for biflow diagramsSo we can apply Eqns. 11 and 13 repeatedly until the link binder only binds this subterm, xy . x ; y . Thenwe use Eqn. 15 to replace this subterm with the identity. We repeat the process until all binders areeliminated. Let us consider now strict symmetric traced monoidal categories and their associated diagrammaticlanguage, biflow diagrams [7]. Biflow diagrams are labelled directed graphs with vertices V and edges E , equipped with a partial injection f : V (cid:42) L from vertices to labels. Biflow diagrams are labelleddirected graphs with interfaces I , O , lists of unlabelled ports, equivalent up to vertex renaming and wirehomeomorphism. In other words, biflow diagrams are uniflow diagrams minus the anchoring relation,similar to Kissinger’s FPGs . The increased expressiveness of the diagrammatic language is reflected intoa relaxation of the type system. The type judgements are Γ | ∆ (cid:96) M : m → m , similar to the uniflowcase but without an anchoring relation. The rules are also similar, with the addition of new rules for trace (Fig. 4). If the trace is over the unit we omit the superscript. Traces for i > feedback edge betweenthe top input and output ports. Assuming that (cid:74) Γ | ∆ (cid:96) M : m + → n + (cid:75) = ( V , i :: I , o :: O , E , f ) , (cid:74) Γ | ∆ (cid:96) Tr ( M ) : m → n (cid:75) = ( V , I , O , E ∪ { ( o , i ) } ) . For equational completeness we also include the (trivial)trace over 0, Tr ( M ) = M . Soundness holds, immediately. Lemma 7 (Biflow definability) . Any biflow diagram is definable in terms of composition, tensor, link.
Theorem 8.
For any term M in the language of biflow diagrams, there are forms ˜ M and ˆ M such that ˜ Mis link-free and ˆ M is free of structural combinators (identity, symmetry, trace) and (cid:74) M (cid:75) = (cid:74) ˜ M (cid:75) = (cid:74) ˆ M (cid:75) . In the equational theory we inherit all the equations from the previous section, plus the trace equa-tions [12]. All the additional equations are given in Fig. 5. Surprisingly, we do not need new equationsfor link, except for scope extrusion in the presence of trace (Eqn. 23).Other relations between trace and link can be derived in the existing equational theory. The proposi-tion below is clearly sound in the model, corresponding to the two alternative descriptions of a back-link,using trace or link . Proposition 9. Γ | ∆ (cid:96) Tr ( M ) ≡ yx . ( x ⊗ m ) ; M ; ( y ⊗ n ) Proof. yx . ( x ⊗ m ) ; M ; ( y ⊗ n ) = yx . Tr (cid:0) ( x ⊗ m ) ; M ; ( y ⊗ n ) (cid:1) (vanishing) = yx . Tr (cid:0) m ; M ; ( y ; x ⊗ n ) (cid:1) (sliding)8 A Structural and Nominal Syntax for Diagrams Γ | ∆ (cid:96) Tr m (( g ⊗ n ) ; f ) ≡ g ; Tr m ( f ) left-tightening (16) Γ | ∆ (cid:96) Tr m ( f ; ( g ⊗ n )) ≡ Tr m ( f ) ; g right-tightening (17) Γ | ∆ (cid:96) Tr m ( f ; ( n ⊗ g )) ≡ Tr p (( q ⊗ g ) ; f ) sliding (18) Γ | ∆ (cid:96) Tr ( f ) ≡ f vanishing (19) Γ | ∆ (cid:96) Tr m + n ( f ) ≡ Tr m ( Tr n ( f )) vanishing (20) Γ | ∆ (cid:96) Tr m ( n ⊗ s ) ≡ n ⊗ Tr m ( f ) superposing (21) (cid:96) Tr ( γ ) ≡ Γ | ∆ (cid:96) xy . Tr i ( M ) ≡ Tr i ( xy . M ) , i ∈ { , } . scope extrusion (23)Figure 5: Additional equations for biflow diagrams = yx . Tr (cid:0) M ; ( y ; x ⊗ n ) (cid:1) (identity) = Tr (cid:0) M ; ( yx . ( y ; x ) ⊗ n ) (cid:1) (link scope) = Tr (cid:0) M ; ( ⊗ n ) (cid:1) (link) = Tr (cid:0) M (cid:1) (identity) Theorem 10.
The equational theory of biflow diagram is sound and complete for the given semantics.
Note : Diagrams corresponding to compact-closed categories can be described analogously.
In the specification of diagrams corresponding to circuits and systems two kinds of structural componentsare used quite extensively: the splitting of two connectors and the joining of two connectors. A monoid ,in this particular diagrammatic context is a pair of constant morphisms ( φ , η ) where φ : 2 → multiplication and η : 1 → unit , interpreted as the following biflow diagrams corresponding to“joining two connectors” and, respectively, to a “dangling output” connector: (cid:74) φ : 2 → (cid:75) = ●● ●● (cid:74) η : 0 → (cid:75) = ●● .Conversely, a co-monoid consist of a co-multiplication ψ : 1 → co-unit ε : 0 →
1, a “dangling input”. (cid:74) ψ : 1 → (cid:75) = ● ●● ● (cid:74) ε : 1 → (cid:75) = ● ● .The monoid should be associative, commutative and have a unit law. The corresponding diagrams are inFig. 6. The co-monoid should be co-associative, co-commutative and have a co-unit, with the equationsand diagrams the converse of the above.Other laws that apply in some contexts are Frobenius ( ( φ ⊗ ) ; ( ⊗ ψ ) = φ ; ψ ) and the special law( ψ ; φ = . R. Ghica and A. Lopez ● ●● ●●● = ● ●● ●●● ●● ●● = ●● ●● ●● ●● = ●● ●● = ● ● ( φ ⊗ ) ; φ = ( ⊗ φ ) ; φ φ = γ ; φ ( ⊗ η ) ; φ = ( η ⊗ ) ; φ = ●● ●● ●● ●● ●● ●● = ● ● ●● = ● ● If all these equations are satisfied, any structure constructed out of the multiplication, co-multiplication,unit and co-unit can be reduced to a canonical form called informally a spider diagram [6]. Informally,all such constructions will denote a diagram of shape equivalent to ● ●●●● ... ●●●● ...
Precisely how these equations are chosen and how they are incorporated into the underlying graph modelpresents many choices that must be dealt with on a case-by-case basis, depending on the intended appli-cations. In the next sequel we consider two interesting scenarios, but more variations are possible.
One natural way to add a comonoid structure to a biflow diagram is by adding constant morphisms ψ : 1 → ε : 1 → ( V (cid:93) { a } , I , O , E , f ) such that f ( a ) is undefined ( a is a wire node) is (still) homeomorphic to the FPG ( V , I , O , E \ { a } ∪ { ( b , c ) | ( b , a ) , ( a , c ) ∈ E } ) . Diagrammatically, the homeomorphism is represented as: ●●● ●● ●●●●
The associativity, co-commutativity and co-unit laws now come then directly from the new wirehomeomorphism. For example ψ ; ( ψ ⊗ ) = ψ ; ( ⊗ ψ ) is: ●● ●● ● ● = ●● ●● ● ● = ●● ●● ● = ●●● ● To reflect the fact that wire nodes have now exactly one incoming and zero or more outgoing edgeswe update the type system to allow contraction and weakening for input variables while output variablespreserve the linear discipline, as seen if Fig. 7. Even though the semantics of link remains the same,the graph invariants are different because of the presence of the co-multiplication and the co-unit. Thediagram for xy . M is still the connection of (the single) occurrence of the node labelled with x to the(zero or more) occurrences of the node(s) labelled with y , followed by the removal of the labels. Themathematical formulation is the same as before (Eqn. 1), except for the anchoring relation.0 A Structural and Nominal Syntax for Diagrams Γ , x | − (cid:96) x : 0 → Γ | x (cid:96) x : 1 → Γ | − (cid:96) → Γ | − (cid:96) γ : 2 → Γ | − (cid:96) ψ : 1 → Γ | − (cid:96) ε : 1 → Γ | ∆ (cid:96) M : m → m Γ | ∆ (cid:48) (cid:96) N : n → n m = n Γ | ∆ (cid:93) ∆ (cid:48) (cid:96) M ; N : m → n Γ | ∆ (cid:96) M : m → m Γ | ∆ (cid:48) (cid:96) N : n → n Γ | ∆ (cid:93) ∆ (cid:48) (cid:96) M ⊗ N : m → n Γ | ∆ (cid:96) M : m → m i = , Γ | ∆ (cid:96) Tr i ( M ) : m − i → m − i Γ , x | ∆ , y (cid:96) M : m → m Γ | ∆ (cid:96) xy . M : m → m Figure 7: Type system for diagrams with a co-monoid. Γ , x | ∆ (cid:96) x : 0 → Γ | x , ∆ (cid:96) x : 1 → Γ | ∆ (cid:96) → Γ | ∆ (cid:96) φ : 2 → Γ | ∆ (cid:96) γ : 2 → Γ | ∆ (cid:96) ψ : 1 → Γ | ∆ (cid:96) ε : 1 → Γ | ∆ (cid:96) η : 0 → Γ | ∆ (cid:96) M : m → m Γ | ∆ (cid:96) N : n → n m = n Γ | ∆ (cid:96) M ; N : m → n Γ , x | ∆ , y (cid:96) M : m → m Γ | ∆ (cid:96) xy . M : m → m Γ | ∆ (cid:96) M : m → m Γ | ∆ (cid:96) N : n → n Γ | ∆ (cid:96) M ⊗ N : m → n Γ | ∆ (cid:96) M : m → m i = , Γ | ∆ (cid:96) Tr i ( M ) : m − i → m − i Figure 8: Type system for biflow Frobenius diagrams
Lemma 11 (Biflow comonoid definability) . Any biflow diagram with a comonoid structure is definablein terms of composition, tensor and link.
From this it follows immediately that the link construct is enough in terms of expressiveness, makingthe explicit use of the co-unit and the co-multiplication optional.
Theorem 12.
For any term M in the language of biflow diagrams with comonoids, there are forms ˜ M and ˆ M such that ˜ M is link-free and ˆ M is free of structural combinators (identity, symmetry, trace,comultiplication, counit) and (cid:74) M (cid:75) = (cid:74) ˜ M (cid:75) = (cid:74) ˆ M (cid:75) . The equational theory of the diagrammatic language is extended by the following two new equations: ε = xy . x ψ = xy . x ⊗ y ⊗ y . Theorem 13.
The equational theory of biflow diagrams with a comonoid is sound and complete for thegiven semantics.
An analogous link syntax and equational theory for a monoid structure is obvious.
A particularly useful combination of the monoid and the comonoid structures uses the Frobenius andspecial axioms, leading to so-called spider diagrams [6]. Syntactically, besides the new constants φ and η , the monoid multiplication and unit, the type judgements now lose all linearity, with link nodes useablezero, one or more times both as input and output (Fig. 8). Theorem 14.
For any term M in the language of biflow diagrams with a Frobenius structure, there areforms ˜ M and ˆ M such that ˜ M is link-free and ˆ M is free of structural combinators (identity, symmetry,trace, (co)multiplication, (co)unit) and (cid:74) M (cid:75) = (cid:74) ˜ M (cid:75) = (cid:74) ˆ M (cid:75) . . R. Ghica and A. Lopez η = xy . y , φ = xy . x ⊗ x ⊗ y . Theorem 15.
The equational theory of biflow diagrams with a Frobenius structure is sound and completefor the given semantics.
Note that the interaction of the monoid and co-monoid is not necessarily subject to the Frobenius lawin diagrammatic languages. This is not a structure that is well studied in its own right, but it turns outto occur naturally in the treatment of digital circuits [10]. The syntax in this case is less natural and theequational properties are more elusive, but the expressiveness and convenience of the notation remainsan important attribute.
In this section we are giving an example of circuit-like system which has both structural features andarbitrary connections, namely a neural network. We will employ a free use of all the methods of spec-ification available (TMC with monoid and co-monoid and the link operation) to give a succinct andinformative description which is, in our subjective opinion, preferable to both the purely structural or thepurely relational descriptions. These will be left as reader exercises.The net we use as an example is the gated recurrent unit [5], a simplification of the popular longshort-term memory net [13] which is broadly used in wide range of applications, from machine trans-lation to image classification. The basic cell of the net has the following architecture, where σ , τ areactivation functions and × , + arithmetic operations. σσ × ! ×× + Let ψ : 2 → gru = x (cid:48) x . h (cid:48) h . ( x (cid:48) ⊗ h (cid:48) ) ; ( x ⊗ h ) ; ψ ; ( σ ⊗ h ⊗ σ ) ; ( x ⊗ × ⊗ ψ ) ; ( τ ⊗ ⊗ × ) ; ( × ⊗ ) ; += ( ψ ⊗ ) ; ( ⊗ ψ ⊗ ψ ) ; ( ⊗ ψ ⊗ ψ ) ; ( ⊗ γ ⊗ ) ; ( ⊗ σ ⊗ σ ⊗ ) ; ( ⊗ γ ⊗ ) ; ( ⊗ × ⊗ ψ ⊗ ) ; ( τ ⊗ ⊗ × ) ; ( × ⊗ ) ; + Note that in the above the x , h variables can have any width, not necessarily unit.A recurrent neural net consists of GRU cell with a recurrent link. For practical application, insteadof a recurrent link, a finite unfolding of the net is often used: gru gru gru gru The two nets can be described as Tr ( gru ; ψ ) and x (cid:48) x . x (cid:48) ; ( gru ⊗ x ) ; ( gru ⊗ x ) ; gru , respectively.2 A Structural and Nominal Syntax for Diagrams
We have shown how variables can be used within a structural framework for diagram syntax. We showedhow from the point of view of expressiveness the structural and the nominal syntax can be equivalentand complementary, if the use of variables is constrained by a suitable type system. We have highlighteda neat correspondence between non-linearity and the presence of a monoid or co-monoid structure inthe diagram. By extending the structural notation with the new link construct we showed that the ex-isting equational properties are preserved and, moreover, this new structure itself has good equationalproperties. We believe that the combination of structural and nominal syntax can sometimes improvethe readability and usability of diagram languages. Although this paper is only concerned with syntax,it is part of a larger research effort aimed at creating hardware languages more similar to programminglanguages in terms of their categorical [9] and operational semantics [10]. As further work we intend tocreate structural conservative extensions of diagram languages such as VHDL, S
IMULINK or D OT .Several theoretical aspects we have not studied, but remain subject for further work. The most in-triguing perhaps is the categorical semantics for the link structure itself. Similar diagrammatic structureshave been conjectured in the study of higher-order π -calculus, with the input and output links modelledas adjoint profunctors and a link-like operation modelled as their composition [19]. A smaller issue,but practically important, is understanding the syntax and the axiomatisation of links for diagrams withmonoid and comonoid structures in the absence of the Frobenius law. These structures occur in diagramsmodelling systems with directed flow of information, such as digital circuits, which are well motivatedby applications. Finally, the various definability results (e.g. Lem. 4) suggest the possibility of arrivingat normal forms for various classes of diagrams.In terms of the graph semantics, the frame point graphs is not the only possible starting point.Cospans of graphs [16] or hypergraphs [3] have also been used as semantics for diagrammatic monoidalcategories. These alternative presentations may arguably offer, to the theorist, more conceptual clarity.What we find attractive about FPGs is the fact that they can be presented in a concrete way, elidingmuch of the categorical apparatus. We believe this could make our work accessible to a broader audi-ence. Moreover, the link construct is particularly easy to interpret in FPGs. However, these alternativesemantic frameworks along with categorical semantics are being investigated. Acknowledgement:
This work was supported in part by EPSRC grant EP/P004490/1.
References [1] Samson Abramsky & Bob Coecke (2004):
A Categorical Semantics of Quantum Protocols . In: , pp.415–425, doi:10.1109/LICS.2004.1319636.[2] Per Bjesse, Koen Claessen, Mary Sheeran & Satnam Singh (1998):
Lava: Hardware Design in Haskell . In:
Proceedings of the third ACM SIGPLAN International Conference on Functional Programming (ICFP ’98),Baltimore, Maryland, USA, September 27-29, 1998. , pp. 174–184, doi:10.1145/289423.289440.[3] Filippo Bonchi, Fabio Gadducci, Aleks Kissinger, Pawel Sobocinski & Fabio Zanasi (2016):
Rewrit-ing modulo symmetric monoidal structure . In:
Proceedings of the 31st Annual ACM/IEEE Sympo-sium on Logic in Computer Science, LICS ’16, New York, NY, USA, July 5-8, 2016 , pp. 710–719,doi:10.1145/2933575.2935316. . R. Ghica and A. Lopez [4] Filippo Bonchi, Pawel Sobocinski & Fabio Zanasi (2015): Full Abstraction for Signal Flow Graphs . In:
Proceedings of the 42nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Lan-guages, POPL 2015, Mumbai, India, January 15-17, 2015 , pp. 515–526, doi:10.1145/2676726.2676993.[5] Kyunghyun Cho, Bart van Merrienboer, C¸ aglar G¨ulc¸ehre, Dzmitry Bahdanau, Fethi Bougares, HolgerSchwenk & Yoshua Bengio (2014):
Learning Phrase Representations using RNN Encoder-Decoder for Sta-tistical Machine Translation . In:
Proceedings of the 2014 Conference on Empirical Methods in NaturalLanguage Processing, EMNLP 2014, October 25-29, 2014, Doha, Qatar, A meeting of SIGDAT, a Spe-cial Interest Group of the ACL , pp. 1724–1734. Available at http://aclweb.org/anthology/D/D14/D14-1179.pdf .[6] Bob Coecke & Ross Duncan (2008):
Interacting Quantum Observables . In:
Automata, Languages and Pro-gramming, 35th International Colloquium, ICALP 2008, Reykjavik, Iceland, July 7-11, 2008, Proceedings,Part II - Track B: Logic, Semantics, and Theory of Programming & Track C: Security and CryptographyFoundations , pp. 298–310, doi:10.1007/978-3-540-70583-3 25.[7] V. E. C˘az˘anescu & Gheorghe Stef˘anescu (1990):
Towards a New Algebraic Foundation of Flowchart SchemeTheory . Fundam. Inf. http://dl.acm.org/citation.cfm?id=97367.97373 .[8] Dan R Ghica (2013):
Diagrammatic reasoning for delay-insensitive asynchronous circuits . In:
Computation,Logic, Games, and Quantum Foundations , Springer, pp. 52–68, doi:10.1007/978-3-642-38164-5 5.[9] Dan R. Ghica & Achim Jung (2016):
Categorical semantics of digital circuits . In Ruzica Piskac & MuralidharTalupur, editors:
Formal Methods in Computer-Aided Design (FMCAD), 2016, Mountain View, California,USA , pp. 41–49, doi:10.1109/FMCAD.2016.7886659.[10] Dan R. Ghica, Achim Jung & Aliaume Lopez (2017):
Diagrammatic Semantics for Digital Circuits . InValentin Goranko & Mads Dam, editors: , Leibniz International Proceedings in Informatics (LIPIcs)
82, Schloss Dagstuhl–Leibniz-Zentrum fuerInformatik, Dagstuhl, Germany, pp. 24:1–24:16, doi:10.4230/LIPIcs.CSL.2017.24.[11] Philip Hackney & Marcy Robertson (2015):
On the Category of Props . Applied Categorical Structures
Recursion from Cyclic Sharing: Traced Monoidal Categories and Models ofCyclic Lambda Calculi . In:
Typed Lambda Calculi and Applications, Third International Conference onTyped Lambda Calculi and Applications, TLCA ’97, Nancy, France, April 2-4, 1997, Proceedings , pp. 196–213, doi:10.1007/3-540-62688-3 37.[13] Sepp Hochreiter & J¨urgen Schmidhuber (1997):
Long Short-Term Memory . Neural Computation
The geometry of tensor calculus, I . Advances in Mathematics
Pictures of processes: automated graph rewriting for monoidal categories andapplications to quantum computing . arXiv preprint arXiv:1203.0202 . Available at https://arxiv.org/abs/1203.0202 .[16] Robert Rosebrugh, Nicoletta Sabadini & RFC Walters (2005): Generic commutative separable algebrasand cospans of graphs . Theory and applications of categories .[17] Mehrnoosh Sadrzadeh, Stephen Clark & Bob Coecke (2013):
The Frobenius anatomy of word meanings I:subject and object relative pronouns . J. Log. Comput.
A survey of graphical languages for monoidal categories . In: