Parsing as a lifting problem and the Chomsky-Schützenberger representation theorem

Building on our work on type reﬁnement systems, we continue developing the thesis that many kinds of deductive systems may be usefully modelled as functors and derivability as a lifting problem, focusing in this work on derivability in context-free grammars. We begin by explaining how derivations in any context-free grammar may be naturally encoded by a functor of operads from a freely generated operad into a certain “operad of spliced words”. This motivates the introduction of a more general notion of context-free grammar over any category, deﬁned as a ﬁnite species S equipped with a color denoting the start symbol and a functor of operads p : Free S → W [ C ] into the operad of spliced arrows in C , generating a context-free language of arrows. We show that many standard properties of context-free grammars can be formulated within this framework, thereby admitting simpler analysis, and that usual closure properties of context-free languages generalize to context-free languages of arrows. One advantage of considering parsing as a lifting problem is that it enables a dual ﬁbrational perspective on the functor p via the notion of displayed operad , corresponding to a lax functor of operads W [ C ] → Span(Set). We show that displayed free operads admit an explicit inductive deﬁnition, using this to give a reconstruction of Leermakers’ generalization of the CYK parsing algorithm. We then turn to the Chomsky-Sch¨utzenberger Representation Theorem. We start by explaining that a non-deterministic ﬁnite state automaton over words, or more generally over arrows of a category, can be seen as a category Q equipped with a pair of objects denoting initial and accepting states and a functor of categories Q → C satisfying the unique lifting of factorizations (ULF) property and the ﬁnite ﬁber property, recognizing a regular language of arrows. Then, we explain how to extend this notion of automaton to functors of operads, which generalize tree automata, allowing us to lift an automaton over a category to an automaton over its operad of spliced arrows. We show that every context-free grammar over a category can be pulled back along a non-deterministic ﬁnite state automaton over the same category, and hence that context-free languages are closed under intersection with regular languages. The last and important ingredient is the identiﬁcation of a left adjoint C [ − ] : Operad → Cat to the operad of spliced arrows functor W [ − ] : Cat → Operad. This construction builds the contour category C [ O ] of any operad O , whose arrows have a geometric interpretation as “oriented contours” of operations. A direct consequence of the contour / splicing adjunction is that every pointed ﬁnite species induces a universal context-free grammar, generating a language of tree contour words. Finally, we prove a generalization of the Chomsky-Sch¨utzenberger Representation Theorem, establishing that any context-free language of arrows over a category C is the functorial image of the intersection of a C -chromatic tree contour language and a regular language.


Introduction
In "Functors are Type Refinement Systems" [25], we argued for the idea that rather than being modelled merely as categories, type systems should be modelled as functors p : D → T from a category D whose morphisms are typing derivations to a category T whose morphisms are the terms corresponding to the underlying subjects of those derivations.One advantage of this fibrational point of view is that the notion of typing judgment receives a simple mathematical status, as a triple (R, f, S) consisting of two objects R, S in D and a morphism f in T such that p(R) = dom(f ) and p(S) = cod(f ).The question of finding a typing derivation for a typing judgment (R, f, S) then reduces to the lifting problem of finding a morphism α : R → S such that p(α) = f .We developed this perspective in a series of papers [25,27,26], and believe that it may be usefully applied to a large variety of deductive systems, beyond type systems in the traditional sense.In this work, we focus on derivability in context-free grammars, a classic topic in formal language theory with wide applications in computer science.
To set the stage and motivate the overall approach, let us begin by quickly explaining how contextfree grammars naturally give rise to certain functors of colored operads D → T .We will assume that the reader is already familiar with context-free grammars and languages [30] as well as with operads or multicategories [23,Ch. 2].Note that "multicategory" and "colored operad" are two different names in the literature for the same concept, and in this paper we will often just use the word operad, it being implicit that operads always carry a (potentially trivial) set of colors.We write f • (g 1 , . . ., g n ) for parallel composition of operations in an operad, and f • i g for the partial composition of g into f after the first i inputs.
Classically, a context-free grammar is defined as a tuple G = (Σ, N, S, P ) consisting of a finite set Σ of terminal symbols, a finite set N of non-terminal symbols, a distinguished non-terminal S ∈ N called the start symbol, and a finite set P of production rules of the form R → σ where R ∈ N and σ ∈ (N ∪ Σ) * is a string of terminal or non-terminal symbols.Observe that any sequence σ on the right-hand side of a production can be factored as σ = w 0 R 1 w 1 . . .R n w n where w 0 , . . ., w n are words of terminals and R 1 , . . ., R n are non-terminal symbols.We will use this simple observation in order to capture derivations in context-free grammars by functors of operads D → T from an operad D whose colors are non-terminals to a certain monochromatic operad T = W[Σ] that we like to call the operad of spliced words in Σ.The n-ary operations of W[Σ] consist of sequences w 0 −w 1 − . . .−w n of n + 1 words in Σ * separated by n gaps notated with the − symbol, with composition defined simply by "splicing into the gaps" and interpreting juxtaposition by concatenation in Σ * .For example, the parallel composition of the spliced word a−b−c with the pair of spliced words d−e−f and −a is defined as (a−b−c) • (d−e−f, −a) = ad−e−f b−ac.The identity operation is given by the spliced word − , and it is routine to check that the operad axioms are satisfied.Now, to any context-free grammar G we can associate a free operad D[G] that we call the (colored) operad of derivations in G. Its colors are the non-terminal symbols R ∈ N of the grammar, while its operations are freely generated by the production rules, with each rule r ∈ P of the form R → w 0 R 1 w 1 . . .R n w n giving rise to an n-ary operation r : R 1 , . . ., R n → R.These basic operations freely generate the operad D[G] whose general operations R 1 , . . ., R n → R can be regarded as (potentially incomplete) parse trees with root label R and free leaves labelled R 1 , . . ., R n , and with each node labelled by a production rule of G.Moreover, this free operad comes equipped with an evident forgetful functor D[G] → W[Σ] that sends every non-terminal symbol R to the unique color of W[Σ], and every generating operation r : R 1 , . . ., R n → R as above to the spliced word w 0 − . . .−w n , extending to parse trees homomorphically.See Fig. 1 for an illustration.
In the rest of the paper, we will see how this point of view may be generalized to define a notion of context-free language of arrows between two objects A and B in any category C, by first introducing a certain operad W[C] of sliced arrows in C. We will see that many standard concepts and properties of context-free grammars and languages can be formulated within this framework, thereby admitting simpler analysis, and that parsing may indeed be profitably considered from a fibrational perspective, as a lifting problem along a functor from a freely generated operad.We will also develop a notion of non-deterministic finite state automaton and regular language of arrows in a category, and show that context-free languages are closed under intersection with regular languages.Finally, we will establish a categorical generalization of the Chomsky-Schützenberger representation theorem, relying on a fundamental adjunction between categories and operads that we call the contour / splicing adjunction.

Related work.
The functorial perspective on context-free grammars that we just sketched and take as a starting point for this article ( §2) is very similar to that of Walters in his brief "note on context-free languages" [32], with the main difference that we generalize it to context-free languages over any category by considering the operad of sliced arrows construction.It is also closely related to de Groote's treatment of CFGs in his paper introducing abstract categorial grammars [8] and in a later article with Pogodalla [9], which were developed within a λ-calculus framework rather than a categorical / operadic one.The contour category construction and the contour / splicing adjunction between operads and categories is fundamental to our treatment of the Chomsky-Schützenberger representation theorem ( §4), and provides an unexpected geometric lens on context-free grammars, evocative of the geometry of interaction [13].Although the adjunction is not identified, this geometric perspective is also apparent in Slavnov's recent work [31], inspired both by abstract categorial grammars and by proof-nets for classical linear logic [12], wherein he constructs a compact closed monoidal category of word cobordisms reminiscent of the operad of spliced words.
The fibrational perspective on non-deterministic finite state automata as finitary ULF functors that we take in the middle of this article ( §3) is also similar in spirit to (and roughly dual to) Colcombet and Petrişan's proposal [6] for modelling various forms of automata as functors.Our approach is motivated by the desire to place both context-free grammars and non-deterministic finite state automata within a common framework, facilitating for example taking the intersection of a context-free language with a regular language.Our main goal is to develop a unified framework for type systems and other deductive systems, which would benefit from the classical body of work on context-free languages and automata theory, and in a future article we intend to consider parsing from left to right [17,10].

Context-free languages of arrows in a category
In this section we explain how the functorial formulation of context-free grammars discussed in the Introduction extends naturally to context-free grammars over any category, which at the same time leads to a simplification of the classical treatment of context-free languages while also providing a useful generalization.Example 2.2 Words w ∈ Σ * may be regarded as the arrows w : * → * of a one-object category that we notate B Σ , with sequential composition of arrows in B Σ given by concatenation.The operad W[Σ] of spliced words in Σ described in the Introduction is identical to the operad W[B Σ ] of spliced arrows in B Σ , and more generally, any monoid seen as a one-object category induces a corresponding operad of spliced words of that monoid.
Remark 2.3 Although the one-object category B Σ is a free category (being freely generated by the arrows a : * → * ranging over letters a ∈ Σ), this property of being freely generated does not extend to its operad of spliced words.Indeed, an operad of spliced arrows W[C] is almost never a free operad.That's because any pair of objects A and B induces a binary operation id A −id A −id B : (A, A), (A, B) → (A, B), and any arrow w : A → B of C induces a corresponding constant w : (A, B).Since id A −id A −id B • (id A , w) = w, W[C] cannot be a free operad except in the trivial case where C has no objects and no arrows.
Example 2.4 The ordinal sum [20] of two categories C and D may be constructed as a category C + σ D whose objects are the disjoint union of the objects of both categories, and whose arrows are the disjoint union of the arrows of both categories with an additional arrow A → B freely adjoined for every pair of objects A ∈ C, B ∈ D. The operations of the spliced arrows operad W[C + σ D] may be described accordingly as consisting of a (possibly empty) sequence of arrows of C followed by a (possibly empty) sequence of arrows of D. As a special case, consider spliced arrows over the ordinal sum B Σ = B Σ + σ 1, which is the two-object category obtained from B Σ by freely adjoining an object and an arrow $ : * → .The operad W[B Σ ] includes operations of the form f = w 0 − . . .−w n $ : ( * , * ), . . ., ( * , * ) → ( * , ) which may be seen as spliced words with an explicit "end of input" marker, since it is impossible to concatenate anything after the last word w n using only substitution in the operad.(See Example 2.8 below for an application of this construction.)Remark 2.5 The operad W [1] of spliced arrows over the terminal category is isomorphic to the terminal operad, with a single color ( * , * ), and a single n-ary operation id− . . .−id : ( * , * ), . . ., ( * , * ) → ( * , * ) of every arity n.Likewise, the operad of spliced arrows over the product of two categories decomposes as a product of spliced arrow operads . This might suggest that the functor W[−] : Cat → Operad is a right adjoint, and we will see in §4.2 that this is indeed the case.

Context-free grammars and context-free derivations over a category
We already sketched in the Introduction how an ordinary context-free grammar G = (Σ, N, P, S) gives rise to a freely generated operad D[G] equipped with a functor to the operad of spliced words W[Σ], where D[G] has the set of non-terminals N as objects and operations freely generated by the productions in P .To make this more precise and to generalize to context-free grammars over arbitrary categories, we first need to recall the notion of a (colored non-symmetric) species, and how one gives rise to a free operad.
A colored non-symmetric species, which we abbreviate to "species"3 for short, is a tuple with the following interpretation: C is a set of "colors", V is a set of "nodes", and the functions i : V → C * and o : V → C return respectively the list of input colors and the unique output color of each node.Adopting the same notation as we use for operations of an operad, we write However, it should be emphasized that a species by itself only contains bare coloring information about the nodes, and does not say how to compose them as operations.
We say that a species is finite (also called polynomial [16]) just in case both sets C and V are finite.
A map of species φ : Equivalently, overloading φ for both φ C and φ V , every node x : R 1 , . . ., R n → R of S must be sent to a node Every operad O has an underlying species with the same colors and whose nodes are the operations of O, and this extends to a forgetful functor Forget : Operad → Species from the category of operads and functors of operads to the category of species and maps of species.Moreover, this forgetful functor has a left adjoint which sends any species S to an operad Free S with the same set of colors and whose operations are freely generated from the nodes of S. By the universal property of the adjoint pair, there is a natural isomorphism of hom-sets Operad(Free S, O) ∼ = Species(S, Forget O) placing functors of operads p : Free S → O and maps of species φ : S → Forget O in one-to-one correspondence.In the sequel, we will leave the action of the forgetful functor implicit, writing O for both an operad and its underlying species Forget O.
We are now ready to introduce the main definitions of this section.
As suggested in the Introduction and by Example 2.2, every context-free grammar in the classical sense G = (Σ, N, S, P ) corresponds to a context-free grammar over B Σ .For instance, for the grammar in Fig. 1, the corresponding species S has three colors and four nodes, and the functor p is uniquely defined by the action on the generators in S displayed in the middle of the figure.Conversely, any finite species S equipped with a color S ∈ S and a functor of operads p : Free S → W[B Σ ] uniquely determines a contextfree grammar over the alphabet Σ.Indeed, the colors of S give the non-terminals of the grammar and S the distinguished start symbol, while the nodes of S together with the functor p give the production rules of the grammar, with each node Proposition 2.7 A language L ⊆ Σ * is context-free in the classical sense if and only if it is the language of arrows of a context-free grammar over B Σ .
An interesting feature of the general notion of context-free grammar of arrows G = (C, S, S, p) is that the non-terminals of the grammar are sorted in the sense that every color of S is mapped by p to a unique color of W[C], corresponding to a pair of objects of C. Adapting the conventions from our work on type refinement systems, we sometimes write R (A, B) to indicate that p(R) = (A, B) and say that R refines the "gap type" (A, B).The language L G generated by a grammar with start symbol S (A, B) is a subset of the hom-set C(A, B).
Example 2.8 To illustrate some of the versatility afforded by the more general notion of context-free grammar of arrows, consider a CFG over the category B Σ from Example 2.4.Such a grammar may include production rules that can only be applied upon reaching the end of the input, which is useful in practice, albeit usually modelled in an ad hoc fashion.For example, the grammar of arithmetic expressions defined by Knuth in the original paper on LR parsing [17, example (27)] may be naturally described as a grammar over B Σ , which in addition to having three "classical" non-terminals E, T, P ( * , * ) contains a distinguished non-terminal S ( * , ).Knuth's production 0 : S → E$ is then just a unary node 0 : E → S in S, mapped by p to the operation More significant examples of context-free languages of arrows over categories with more than one object will be given in §4, including context-free grammars over the runs of finite-state automata.Finally, let us remark that context-free grammars of arrows may be organized into a comma category, observing that a grammar G may be equivalently considered as a triple of a pointed finite species (S, S), a bipointed finitely generated category (C, A, B), and a map of pointed operads p : (Free S, S) → (W[C], (A, B)).Since the operad of spliced arrows construction lifts to a functor W[−] : Cat •,• → Operad • sending a category C equipped with a pair of objects A and B to the operad W[C] equipped with the color (A, B), and likewise the free / forgetful adjunction (1) lifts to an adjunction between pointed species and pointed operads, a CFG can therefore be considered as an object of the comma category Free ↓ W.Although we will not explore this perspective further here, let us mention that it permits another way of understanding the language of arrows generated by a grammar G: as constants

Properties of a context-free grammar and its associated language
Standard properties of context-free grammars [30,Ch. 4], considered as CFGs of arrows G = (B Σ , S, S, p), may be reformulated as properties of either the species S, the operad Free S, or the functor p : Free S → W[B Σ ], with varying degrees of naturality: • G is linear just in case S only has nodes of arity ≤ 1.It is left-linear (respectively, right-linear ) just in case it is linear and every unary node x of S is mapped by p to an operation of the form −w (resp.p(x) = w− ).[19,22]) iff S only has nodes of arity ≤ 2.
• G is unambiguous iff for any pair of constants α, β : S in Free S, if p(α) = p(β) then α = β.Note that if p is faithful then G is unambiguous, although faithfulness is a stronger condition in general.
• A non-terminal R of G is nullable if there exists a constant α : R of Free S such that p(α) = .
• A non-terminal R of G is useful if there exists a pair of a constant α : R and a unary operation β : R → S.
Note that if G has no useless non-terminals then G is unambiguous iff p is faithful.
Observe that almost all of these properties can be immediately translated to express properties of contextfree grammars of arrows over any category C. Basic closure properties of classical context-free languages also generalize easily to context-free languages of arrows.
We will see in §4.1 that other classical closure properties also generalize to context-free languages of arrows.Finally, we can state a translation principle that two grammars G 1 = (C, S 1 , S 1 , p 1 ) and G 2 = (C, S 2 , S 2 , p 2 ) over the same category have the same language whenever there is a fully faithful functor of operads T : Free S 1 → Free S 2 such that p 1 = T p 2 and T (S 1 ) = S 2 .

A fibrational view of parsing as a lifting problem
We have seen how any context-free grammar G = (C, S, S, p) gives rise to a language L G = { p(α) | α : S }, corresponding to the arrows of C which, seen as constants of W[C], are in the image of some constant of color S of the free operad Free S.However, beyond characterizing the language defined by a grammar, in practice one is often confronted with a dual problem, namely that of parsing: given a word w, we want to compute the set of all its parse trees, or at least determine all of the non-terminals which derive it.In our functorial formulation of context-free derivations, this amounts to computing the inverse image of w along the functor p, i.e., the set of constants p −1 (w) = { α | p(α) = w }, or alternatively the set of colors in the image of p −1 (w) along the output-color function.
To better understand this view of parsing as a lifting problem along a functor of operads, we find it helpful to first recall the correspondence between functors of categories p : D → T and lax functors F : T → Span(Set), where Span(Set) is the bicategory whose objects are sets, whose 1-cells S : X −→ | Y are spans X ← S → Y , and whose 2-cells are morphisms of spans.Suppose given such a functor p : D → T .To every object A of T there is an associated "fiber" F A = p −1 (A) of objects in D living over A, while to every arrow w : A → B of T there is an associated fiber F w = p −1 (w) of arrows in D living over w, equipped with a pair of projection functions F A ← F w → F B mapping any lifting α : R → S of w : A → B to its source R ∈ F A and target S ∈ F B .Moreover, given a pair of composable arrows u : A → B and v : B → C in T , there is a morphism of spans from the composite of the spans F u : from the identity span for all triples of composable arrows u : A → B, v : B → C and w : C → D, and therefore that this collection of data defines what is called a lax functor F : T → Span(Set).In general it is only lax, in the sense that the 2-cells F u F v ⇒ F uv and id F A ⇒ F id A are not necessarily invertible.Conversely, starting from the data provided by a lax functor F : T → Span(Set), we can define a category noted ∫ F together with a functor π : ∫ F → T .The category ∫ F has objects the pairs (A, R) of an object A in T and an element R ∈ F A , and arrows (w, α) : (A, R) → (B, S) the pairs of an arrow w : A → B in T and an element α ∈ F w mapped to R ∈ F A and S ∈ F B by the respective legs of the span F A ← F w → F B .The composition and identity of the category ∫ F are then given by the morphisms of spans F u F v ⇒ F uv and id F A ⇒ F id A witnessing the lax functoriality of F : T → Span(Set).The functor π : ∫ F → T is given by the first projection.This construction of a category ∫ F equipped with a functor π : ∫ F → T starting from a lax functor F : T → Span(Set) is a mild variation of Bénabou's construction of the same starting from a lax normal functor F : T op → Dist [3, §7], which is itself a generalization of the well-known Grothendieck construction of a fibration starting from a pseudofunctor F : T op → Cat.One can show that given a functor of categories p : D → T , the construction applied to the associated lax functor F : T → Span(Set) induces a category ∫ F isomorphic to D, in such a way that p coincides with the isomorphism composed with π.Recently, Ahrens and Lumsdaine [1] have introduced the useful terminology "displayed category" to refer to this way of presenting a category D equipped with a functor D → T as a lax functor T → Span(Set), with their motivations coming from computer formalization of mathematics.
The constructions which turn a functor of categories p : D → T into a lax functor F : T → Span(Set) and back into a functor π : ∫ F → T can be adapted smoothly to functors of operads, viewing Span(Set) as a 2-categorical operad whose n-ary operations S : X 1 , . . ., X n −→ | Y are multi-legged-spans and with the same notion of 2-cell.We will follow Ahrens and Lumsdaine's suggestion and refer to the data of such a lax functor F : T → Span(Set) representing an operad D ∼ = ∫ F equipped with a functor p : D → T as a displayed operad.

An inductive formula for displayed free operads
It is folklore that the free operad over a species S = (C, V, i, o) may be described concretely as a certain family of trees: operations of Free S are interpreted as rooted planar trees whose edges are colored by the elements of C and whose nodes are labelled by the elements of V , subject to the constraints imposed by the functions i : V → C * and o : V → C. The formal construction of the free operad may be viewed as a free monoid construction, adapted to a situation where the ambient monoidal product (in this case, the composition product of species) is only distributive on the left, see [24, II.1.9]and [2, Appendix B].
From the perspective of programming semantics, it is natural to consider the underlying species of Free S as an inductive data type, corresponding to the initial algebra for the endofunctor W S on C-colored species defined by W S = R → I + S • R where + denotes the coproduct of C-colored species which is constructed by taking the disjoint union of operations, while • and I denote respectively the composition product of C-colored species and the identity species, defined as follows.Given two C-colored species S and R, the n-ary nodes R 1 , . . ., R n → R of S • R are formal composites g • (f 1 , . . ., f k ) consisting of a node g : S 1 , . . ., S k → S of S and of a tuple of nodes f 1 : Γ 1 → S 1 , . . ., f k : Γ k → S k of R, such that the concatenation of the lists of colors Γ 1 , . . ., Γ k is equal to the list R 1 , . . ., R n .The unit I is the C-colored species with a single unary node * R : R → R for every color R ∈ C, and no other nodes.
As the initial W S -algebra, the free operad over S is equipped with a map of species I + S • Free S −→ Free S, which by the Lambek lemma is invertible, with the following interpretation: any operation of Free S is either an identity operation, or the parallel composition of a node of S with a list of operations of Free S. Note that this interpretation also corresponds to a canonical way of decomposing trees labelled by the species S, also known as S-rooted trees where we write • for composition in the operad O and • for formal composition of nodes in S with operations in Free S. Specializing the formula to constant operations, the left summand disappears and (4) simplifies to:

Application to parsing
Instantiating (5) with the underlying functor p : Free S → W[C] of a context-free grammar of arrows generated by a map of species φ : S → W[C], we immediately obtain the following characteristic formula for the family of sets of parse trees F w of an arrow w in C, seen as liftings of the constant w in W[C] to a constant in Free S: Taking the image along the function returning the root label of a parse tree (i.e., the underlying color of the constant in Free S), we get that the family of sets of non-terminals N w deriving w is the least family of sets closed under the following inference rule: This inference rule is essentially the characteristic formula expressed by Leermakers [22] for the defining relation of the "C-parser", which generalizes the well-known Cocke-Younger-Kasami (CYK) algorithm.Presentations of the CYK algorithm are usually restricted to grammars in Chomsky normal form (cf. [19]), but as observed by Leermakers, the relation N w defined by ( 7) can be solved effectively for any context-free grammar G and given word w = a 1 . . .a n by building up a parse matrix N i,j indexed by the subwords w i,j = a i+1 . . .a j for all 1 ≤ i ≤ j ≤ n, yielding a cubic complexity algorithm in the case that G is bilinear (cf.§2.3).Moreover, by adding non-terminals, it is always possible to transform a CFG into a bilinear CFG that generates the same language, even preserving the original derivations up to isomorphism.
Proposition 2.11 For any context-free grammar of arrows G = (C, S, S, p), there is a bilinear context-free grammar of arrows G bin = (C, S bin , S, p bin ) together with a fully faithful functor of operads B : Free S → Free S bin such that p = Bp bin .In particular, L G = L G bin by the translation principle.
3 Non-deterministic finite state automata as finitary ULF functors over categories and operads

Warmup: non-deterministic word automata as finitary ULF functors over categories
Classically, a non-deterministic finite state automaton M = (Σ, Q, δ, q 0 , F ) consists of a finite alphabet Σ, a finite set Q of states, a function δ : Tran → Q × Σ × Q from a finite set Tran of transitions, an initial state q 0 ∈ Q and a finite set of accepting states F ⊆ Q.We will focus first on the underlying "bare" automaton M = (Σ, Q, δ) where the initial and the accepting states have been removed.Every such bare automaton M induces a functor of categories p : Q → B Σ where Q is the category with the states of the automaton as objects, and with arrows freely generated by arrows of the form t : q → q for any transition t ∈ Tran such that δ(t) = (q, a, q ); and where the functor p : Q → B Σ transports every transition t : q → q with δ(t) = (q, a, q ) to the arrow a : * → * representing the letter a ∈ Σ in the category B Σ .Under this formulation, every arrow α : q 0 → q f of the category Q describes a run of the automaton M over the word w = p(α) : * → * which starts in state q 0 ∈ Q and ends in state q f ∈ Q, as depicted below: One distinctive property of the functor p : Q → B Σ is that it has the unique lifting of factorizations (ULF) property in the sense of Lawvere and Menni [21].Recall that a functor of categories p : D → T has the ULF property when: For any arrow α of the category D, if p(α) = uv for some pair of arrows u and v of the category T , there exists a unique pair of arrows β and γ in D such that α = βγ and p(β) = u and p(γ) = v.Note that a functor p : D → T has the ULF property precisely when the structure maps of the corresponding lax functor F : T → Span(Set) discussed in §2.4 are invertible, i.e., F is a pseudofunctor.The ULF property implies an important structural property of non-deterministic finite state automata: that every arrow α : q 0 → q f lying above some arrow p(α) = w corresponding to a run of the automaton can be factored uniquely as a sequence of transitions along the letters of the word w.Conversely, we can easily establish that Proposition 3.1 A ULF functor p : Q → B Σ corresponds to a bare non-deterministic finite state automaton precisely when the fiber p −1 ( * ) as well as the fiber p −1 (w) is finite for all words w : * → * .This leads us to the following definitions.Definition 3.2 We say that a functor p : Q → C is finitary if either of the following equivalent conditions hold: • the fiber p −1 (A) as well as the fiber p −1 (w) is finite for every object A and arrow w in the category C; • the associated lax functor F : C → Span(Set) factors via Span(FinSet).Definition 3.3 A non-deterministic finite state automaton over a category is given by a tuple M = (C, Q, p : Q → C, q 0 , q f ) consisting of two categories C and Q, a finitary ULF functor p : Q → C, and a pair q 0 , q f of objects of Q.An object of Q is then called a state and an arrow of Q is called a run of the automaton.The regular language of arrows L M recognized by the automaton is the set of arrows w in C that can be lifted along p to an arrow α : Note that the regular language of arrows L M recognized by an automaton M is a subset of the hom-set C(A, B), where A = p(q 0 ) and B = p(q f ).Remark 3.4 Any non-deterministic finite state automaton M in the standard sense may be converted into an automaton with a single accepting state (and without -transitions) that accepts the same language, except in the case that the language contains and is not closed under concatenation.The usual construction defines a new automaton M with an additional state q f and the same transitions as M , except that every transition q → q to an accepting state q ∈ F of the old automaton is replaced by a transition q → q f in the new automaton.The problem arises when the initial state q 0 is also accepting, in which case the language accepted by M will be closed under concatenation.
Observe that this issue goes away if we instead consider the automaton obtained by transformation of M as an automaton M = (B Σ , Q , p , q 0 , q f ) over the two-object category B Σ = B Σ + σ 1 defined in Example 2.4.Indeed, we can take Q and p to be defined from Q and p by adjoining a single object q f lying over , together with a single arrow q → q f lying over $ : * → for every accepting state q ∈ F of M .Since arrows of type * → do not compose, the aforementioned problem does not arise.Proposition 3.5 A language L ⊆ Σ * is regular in the classical sense if and only if L$ is the regular language of arrows of a non-deterministic finite state automaton over B Σ .
3.2 Non-deterministic tree automata as finitary ULF functors over operads One nice aspect of the fibrational approach to non-deterministic finite state automata based on finitary ULF functors is that it adapts smoothly when one shifts from word automata to tree automata.As a first step in that direction, we first describe how the ULF and finite fiber properties may be extended to functors of operads.Definition 3.7 We say that a functor of operads p : Q → O is finitary if either of the following equivalent conditions hold: • the fiber p −1 (A) as well as the fiber p −1 (f ) is finite for every color A and operation f of the operad O; • the associated lax functor of operads F : O → Span(Set) factors via Span(FinSet).
One can check that the underlying bare automaton M = (Σ, Q, δ) of any non-deterministic finite state tree automaton [7] gives rise to a finitary ULF functor of operads p : Q → Free Σ, where Free Σ is the free operad generated by the ranked alphabet Σ (which may be seen as an uncolored non-symmetric species), where the operad Q has states of the automaton as colors, and operations freely generated by n-ary nodes of the form t : q 1 , . . ., q n → q for every transition t ∈ Tran of the form δ(t) = (q 1 , . . ., q n , a, q) where a is an n-ary letter in Σ, and where p transports every such n-ary transition t : q 1 , . . ., q n → q to the underlying n-ary letter a : * , . . ., * → * .This motivates us to proceed as for word automata and propose a more general notion of finite state automaton over an arbitrary operad: Definition 3.8 A non-deterministic finite state automaton over an operad is given by a tuple M = (O, Q, p : Q → O, q) consisting of two operads O and Q, a finitary ULF functor of operads p : Q → O, and a color q of Q.A color of Q is called a state, and an operation of Q is called a run tree of the automaton p : Q → O.The regular language of constants L M recognized by the automaton is the set of constants c in O that can be lifted along p to a constant α :

From a word automaton to a tree automaton on spliced words
We now state a simple property of ULF functors establishing a useful connection between word and tree automata.
Proposition 3.9 Suppose that p : Q → C is a functor of categories.Then, if p is ULF functor, so is the functor of operads W From this it follows that every non-deterministic finite state automaton M = (C, Q, p, q 0 , q f ) over a given category C induces a non-deterministic finite state automaton W , (q 0 , q f )) over grammar that may be seen as a specialization of G to the word w, with non-terminals (i, R, j) representing the fact that the subword w i,j = a i+1 . . .a j parses as R (cf.§2.6).This example generalizes to context-free grammars of arrows over any category C with the property that every arrow w has only finitely many factorizations w = uv of length 2, by observing that the underlying bare automaton of M w is isomorphic to the interval category [21] of w.In general, for any arrow w : A → B of a category C, the interval category Iw is defined by taking objects to be triples (X, u, v) of an object X ∈ C and a pair of arrows u : A → X, v : X → B such that w = uv, and arrows (X, u, v) → (X , u , v ) to be arrows x : X → X such that u = ux and v = xv .The interval category Iw has an initial object (id A , w) and a terminal object (w, id B ), and it comes equipped with an evident forgetful functor Iw → C, which is always ULF, and moreover finitary by the stated condition on C. The tuple M w = (C, Iw, Iw → C, (id A , w), (w, id B )) therefore defines a finite-state automaton, and any CFG of arrows over C can be pulled back along M w to obtain a CFG specialized to the arrow w. • objects are given by oriented colors R consisting of a color R of O and an orientation ∈ { u, d } ("up" or "down"); • arrows are generated by pairs (f, i) of an operation f : R 1 , . . ., R n → R of O and an index 0 ≤ i ≤ n, defining an arrow R d i → R u i+1 under the conventions that R d 0 = R u and R u n+1 = R d ; subject to the conditions that id R u = (id R , 0) and id R d = (id R , 1) as well as the following equations: whenever the left-hand side is well-formed, for every operation f , operation g of positive arity m > 0, constant c, and indices i and j in the appropriate range.We refer to each generating arrow (f, i) of the contour category C[O] as a sector of the operation f .See Fig. 3 for a graphical interpretation of sectors and of the equations on contours seen as compositions of sectors.We refer to the language of arrows L Univ S,S , also noted L S,S , as a tree contour language, and to its arrows as tree contour words, since they describe the contours of S-rooted trees with root color S, see left side of Fig. 4 for an illustration.The factorization above shows that the context-free grammar G is the functorial image of the universal grammar Univ S,S along the functor of categories q G , whose purpose is to transport each corner of a node in S to the corresponding arrow in C as determined by the grammar G.
At the level of languages, we have L G = q G L S,S .
Remark 4.9 The notion of tree contour language makes sense even for non-finitary pointed species (S, S), although in that case the resulting universal grammar Univ S,S is no longer context-free, having infinitely many non-terminals.Still, it may be an interesting object of study.In particular, the tree contour language Univ N, * generated by the terminal species N with one color and a single operation of every arity appears to be of great combinatorial interest, with words in the language describing the shapes of rooted planar trees with arbitrary node degrees.

Representation theorem
The achievement of the Chomsky-Schützenberger representation theorem [5, §5] is to separate any contextfree grammar G = (Σ, N, S, P ) into two independent components: a context-free grammar Dyck n with only one non-terminal over an alphabet Σ 2n = { [ 1 , ] 1 , . . ., [ n , ] n } of size 2n (for some n), which generates Dyck words of balanced brackets describing the shapes of parse trees with nodes labelled by production rules of principle, we deduce that C[φ colors ] L S,S = L φ C S,(A,B) ∩ L M colors .Finally, using that G is the image of the universal grammar Univ S,S and considering the commutative diagram (12), we conclude: L G = q G L S,S = q G nodes C[φ colors ] L S,S = q G nodes (L φ C S,(A,B) ∩ L M colors ).Theorem 4.13 Every context-free language of arrows of a category C is the functorial image of the intersection of a C-chromatic context-free tree contour language with a regular language.
The original statement of the Chomsky-Schützenberger theorem can be recovered by relying on the fact that any tree contour word can be faithfully translated to a Dyck word, via an easy translation that doubles the number of letters, and which also has a geometric interpretation that involves decomposing each corner of the contour into alternating actions of walking along an edge and turning around a node, see right side of Fig. 4. Intriguingly, this decomposition suggests the existence of an embedding of the contour category C[S] into a bipartite contour category, where each object R has been replaced by a pair of objects R 0 and R 1 , in a way that is analogous to the embedding of the "oriented cartographic group" used to represent maps on oriented surfaces into the cartographic group for maps on not necessarily orientable surfaces (cf.[29,14]).

Fig. 1 .
Fig. 1.Example of a context-free grammar and the corresponding functor D[G] → W[Σ], indicating the action of the functor on the generating operations of D[G] as well the induced action on a closed derivation.
First, we need to explain how the operad W[Σ] of spliced words mentioned in the Introduction generalizes to define an operad W[C] of spliced arrows over any category C.

Definition 2 . 6 A
context-free grammar of arrows is a tuple G = (C, S, S, φ) consisting of a finitely generated category C, a finite species S equipped with a distinguished color S ∈ S called the start symbol, and a functor of operads p : Free S → W[C].A color of S is then called a non-terminal while an operation of Free S is called a derivation.The context-free language of arrows L G generated by the grammar G is the subset of arrows in C which, seen as constants of W[C], are in the image of constants of color S in Free S, that is, of W[C] are in bijection with arrows of C, we have a natural isomorphism el •W[−] ∼ = hom for the evident functors hom : Cat •,• → Set and el : Operad • → Set, and L G is precisely the image of the function el(p) : el(Free S, S) → el(W[C], (A, B)) ∼ = C(A, B).

Proposition 2 .
9 (i) If L 1 , L 2 ⊆ C(A, B) are context-free languages of arrows, so is their union L 1 ∪L 2 ⊆ C(A, B). (ii) If L 1 ⊆ C(A 1 , B 1 ), . . ., L n ⊆ C(A n , B n ) are context-free languages of arrows, and w 0 −w 1 − . . .−w n : (A 1 , B 1 ), . . ., (A n , B n ) → (A, B) is an operation of W[C], then the "spliced concatenation" language of arrows in a category C and F : C → D is a functor of categories, then the functorial image F (L) ⊆ D(F (A), F (B)) is also context-free.Proof.The proofs of (i) and (ii) are just refinements of the standard proofs for context-free languages of words, keeping track of the underlying gap types.For (iii), suppose given a grammar G = (C, S, S, p) and a functor of categories F : C → D. Then the grammar F (G) generating the language F (L G ) is defined by postcomposing p with W[F ] : W[C] → W[D] while keeping the species S and start symbol S the same, to u : A → B and v : B → C to the span F uv : F A −→ | F C associated to the composite arrow uv : A → C.This morphism of spans is realized using composition in the category D, namely by the function taking any pair of a lifting α : R → S of u and a lifting β : S → T of v to the composite αβ : R → T , which is a lifting of uv by functoriality p(αβ) = p(α)p(β).Similarly, the identity arrows in the category D define, for every object A of the category T , a morphism of spans

[ 4 ,
§3.2].It is possible to derive an analogous inductive characterization of functors p : Free S → O from a free operad into an arbitrary operad O considered as displayed free operads, i.e., as lax functors F : O → Span(Set) generated by an underlying map of species φ : S → O. Two subtleties arise.First, that the species S and the operad O may in general have a different set of colors, related by the change-of-color function φ C .To account for this, rather than restricting the operations +, •, I to the category of C-colored species, one should consider them as global functors +, • : Species × Set Species → Species I : Set → Species on the "polychromatic" category of species, which respect the underlying sets of colors in a functorial way.Second, and more significantly, the above functor W S transports a species R living over O to a species living over I + O • O, so that in order to obtain again a species living over O (and thus define an endofunctor) one needs to "push forward" along the canonical W O -algebra [e, m] : I + O • O −→ O that encodes the operad structure of O, seen as a monoid in (Species, •, I).A detailed proof is beyond the scope of this paper, but we nevertheless state the following: Proposition 2.10 Let φ : S → O be a map of species from a species S into an operad O, and let p : Free S → O be the corresponding functor from the free operad.Then the associated lax functor F : O → Span(Set) computing the fibers of p is given by F A = φ −1 (A) on colors of O, and by the least family of sets F f indexed by operations f : A 1 , . . ., A n → A of O such that

Definition 3 . 6 A
functor of operads p : D → T has the unique lifting of factorizations property (or is ULF) if any of the following equivalent conditions hold:(i) for any operation α of D, if p(α) = g • (h 1 , . . ., h n ) for some operation g and list of operations h 1 , . . ., h n of T , there exists a unique operation β and list of operations γ 1 , . . ., γ n of D such that α = β • (γ 1 , . . ., γ n ) and p(β) = g and p(γ 1 ) = h 1 , . . ., p(γ n ) = h n ; (ii) for any operation α of D, if p(α) = g • i h for some operations g and h of T and index i, there exists a unique pair of operations β and γ of D such that α = β • i γ and p(β) = g and p(γ) = h; (iii) the structure maps of the associated lax functor of operads F : T → Span(Set) discussed in §2.4 are invertible.

4. 2 Definition 4 . 5
The contour category of an operad and the contour / splicing adjunction In §2.1, we saw how to construct a functor W[−] : Cat → Operad transforming any category C into an operad W[C] of spliced arrows of arbitrary arity, which played a central role in our definition of context-free language of arrows in a category.We construct now a left adjoint functor C[−] : Operad → Cat which extracts from any given operad O a category C[O] whose arrows correspond to "oriented contours" along the boundary of the operations of the operad.The contour category C[O] of an operad O is defined as a quotient of the following free category:

Fig. 4 .
Fig. 4. Left: an S-rooted tree of root color 1 and its corresponding contour word a0b0a1c0d0c1e0c2a2f0g0f1a3 : 1 u → 1 d .Right: the corresponding Dyck word obtained by first decomposing each corner of the contour into alternating actions of walking along an edge and turning around a node, and then annotating each arrow both by the orientation (with u = [, d = ]) and the node-edge pair of its target.Proposition 4.8 If ψ : S → R is a map of species, then C[p] : C[S] → C[R] is a ULF functor of categories.
• G is in Chomsky normal form if S only has nodes of arity 2 or 0, the color S does not appear as the input of any node, every binary node is mapped by p to − − in W[B Σ ], and every nullary node is mapped to a letter a ∈ Σ, unless R = S in which case it is possible that p(x) = .(This last condition can be made more natural by considering G as a context-free grammar over B Σ with S ( * , ), see Example 2.8 above.)• G is bilinear (a generalization of Chomsky normal form