Algebraic Recognition of Regular Functions

We consider regular string-to-string functions, i


Introduction
This paper is about the regular string-to-string functions (see e.g.[17]).This is a fundamental class of functions; it is one of the standard generalizations of regular languages to produce string outputs (instead of merely accepting or rejecting inputs), covering examples such as string reversal : 123 → 321 duplication : 123 → 123123 It has many equivalent descriptions, including deterministic two-way automata [22,Note 4], copyless streaming string transducers (sst) [1, Section 3] (or the earlier and very similar singleuse restricted macro tree transducers [14, Section 5]), mso transductions [13,Theorem 13], combinators [4, Section 2], a functional programming language [8, Section 6], λ-calculus with linear types [15, Theorem 3] (see also [19,Claim 6.2] and [18,Theorem 1.2.3]), decompositions à la Krohn-Rhodes [10, Theorem 18, item 4], etc.The number of equivalent characterizations clearly indicates that the class of regular functions is important and worth studying.However, from a mathematical point of view, a disappointing phenomenon is that each of the known descriptions uses syntax that is more complicated than one could wish for. 1 These complications are perhaps minor annoyances, and the corresponding models are undeniably useful.Nevertheless, it would be desirable to have a model with a short and abstract definition, similar to the definition of recognizability of regular languages by finite semigroups.
Non-empty lists.A variant of the previous example, which sends a semigroup A to the free semigroup A + that consists of non-empty lists of elements in A. Powerset.This (covariant) powerset functor maps a semigroup A to the powerset semigroup PA, whose underlying set is the family of all subsets of A, endowed with the operation We now present the central definition of this paper.
▶ Definition 2.3 (Transducer semigroup).A transducer semigroup consists of a semigroup-to-semigroup functor F, together with an output mechanism which associates to each semigroup A a function of type FA → A called the output function for A, such that for every homomorphism h : A → B, the diagram below commutes:

Fh output function for A output function for B h
In the language of category theory, a natural transformation between two semigroup-to-set functors G and K is a family of functions f A : GA → KA such that f B • Gh = Kh • f A for every semigroup homomorphism h : A → B. So in Definition 2.3, the diagram says that the output mechanism is a natural transformation of type Semigroups Sets.
[forgetful functor]•F forgetful functor (Example 2.1) Note that in a transducer semigroup, the output functions are not necessarily homomorphisms, which is why the forgetful semigroup-to-set functor appears above.This is important in view of the purpose of transducer semigroups, which is to define functions between semigroups, as explained in the following definition; asking out B to be a homomorphism would severly restrict the functions that can be recognized.
▶ Definition 2.4.We say that a function f : A → B between semigroups, not necessarily a homomorphism, is recognized by a transducer semigroup (F, out) if it can be decomposed as for some semigroup homomorphism h.
The definition discusses functions between arbitrary semigroups, with no assumption on F, but we will mainly care about the special case -treated in Section 3 -where: the function is string-to-string2 (f : Σ * → Γ * ), i.e. both the input and output semigroups are finitely generated free monoids; the functor F is finiteness-preserving, i.e. it maps finite semigroups to finite semigroups.This special case will correspond to the regular string functions.Some minor results that do not assume F is finiteness-preserving are presented in Section 2.2: we characterize all functions (Theorem 2.10) and "recognizability reflecting" string-to-string functions (Theorem 2.13).

Examples and intuitions
▶ Example 2.5.Consider the transducer semigroup in which the functor is the identity, and the output mechanism is also the identity.The functions of type A → B that are recognized by this transducer semigroup are exactly the semigroup homomorphisms from A to B. ▶ Example 2.6.Consider the transducer semigroup in which the functor is the "opposite semigroup" functor from Example 2.2; the output function maps a ∈ FA, seen as an element in A, to aa ∈ A. The functions of type A → B that are recognized by this transducer semigroup are exactly those of the form a → h(a)h(a) where h : A → B is some "anti-homomorphism", i.e. satisfies h(a 1 a 2 ) = h(a 2 )h(a 1 ) for all a 1 , a 2 ∈ A. In particular, if h is the string reversal function rev on the free monoid Σ * , which is also a semigroup, then we get the "reverse then duplicate" function that maps a string w over the alphabet Σ to rev(w) • rev(w).
The functor maps A to A × N, with the semigroup structure defined componentwise (N = {0, 1, . . .} is equipped with addition), and making the morphisms act on the left component.The output mechanism A × N → A is defined below: we handle this case separately because a 0 does not make sense in an arbitrary semigroup ▶ Example 2.8.Our last example function is This can be recognized using the functor FA = A + A 2 , equipped with a suitable semigroup operation that makes the following map h : {a, b, c} * → F({a, b} * ) a homomorphism: The output mechanism of the transducer semigroup sends any element a ∈ A -seen as belonging to the left summand of A + A 2 -to aa, and (b, c) to cb.
▶ Remark 2.9.Consider a transducer semigroup with functor F. For a semigroup S, we may often think of an element of FS as a data structure that contains elements of S (such as a pair or a list, cf.Example 2.2).Then naturality of the output mechanism expresses that, being defined "uniformly in S", it cannot "look inside"3 those elements of S (but it can combine them using the semigroup operation).In other words, the control flow may depend only on the part that is "independent of S" -and the condition that F is finiteness preserving (satisfied by all our examples except Example 2.7) somehow means that this part is finite.

Two simple characterizations
All functions.Our first theorem concerns transducer semigroups without any restrictions.
▶ Theorem 2.10.Every function between semigroups is recognized by a transducer semigroup.
Proof.We prove a slightly stronger result: for any semigroup A, there exists a transducer semigroup that recognizes all functions from A to other semigroups.The functor is FB = A × (set of all functions of type A → B, not necessarily recognizable).
The semigroup operation in FB is defined as follows: on the first coordinate, we inherit the semigroup operation from A, while on the second coordinate, we use the trivial left zero semigroup structure, in which the product of two functions is simply the first one (this is a trivial way of equipping every set with a semigroup structure).The functor is defined on morphisms as in the tuple construction from Example 2.2: the first coordinate, corresponding to A, is not changed, and the second coordinate, corresponding to the set of functions, is transformed coordinate-wise, when viewed as a tuple indexed by A. This is easily seen to be a functor.The output mechanism, which is easily seen to be natural, is function application i.e. (a, f ) → f (a).Every function f : A → B is recognized by this transducer semigroup, with the appropriate homomorphism is a ∈ A → (a, f ).◀ Recognizability reflecting functions.We now characterize functions with the property that inverse images of recognizable languages are also recognizable.We use a slightly more general setup, where instead of languages we use functions into arbitrary sets (languages can be seen as the case of functions into {yes, no}).
▶ Definition 2.11.We say that a function from a semigroup A to some set X is recognizable if it factors through some semigroup homomorphism from A to a finite semigroup.
In the rest of the paper, we shall sometimes speak of recognizable functions with infinite codomain, but note that the range of a recognizable function is always finite.A function f : B → A between semigroups is called recognizability reflecting if for every recognizable function g : A → X, the composition g • f is recognizable.
▶ Example 2.12.Consider the semigroup (N, +) of natural numbers with addition, which is isomorphic to the free monoid a * .In this semigroup, the recognizable functions are ultimately periodic colourings of numbers.A corollary is that every recognizable function gives the same answer to all factorials {1!, 2!, . ..}, with finitely many exceptions.Take any function f : N → N such that (a) every output number arises from at most finitely many input numbers; (b) every output number is a factorial.The composition of f with any recognizable function will give the same answer to all numbers with finitely many exceptions, thus being also recognizable.
In the above example, a function with conditions (a) and (b) can be chosen in uncountably many ways, even if we require that it has linear growth.Therefore, there are too many recognizability reflecting functions (even just from {a} * to itself) to allow a machine model, or some other effective syntax.The following result gives a non-effective syntax.
▶ Theorem 2.13.The following conditions are equivalent for a string-to-string function:

The regular functions
The two straightforward constructions in Theorems 2.10 and 2.13 amount to little more than symbol pushing.In this section, we present a more substantial characterization, which is the main result of this paper.This characterization concerns finiteness-preserving functors.This is a strengthening of the condition from Theorem 2.13: if the functor F in a transducer semigroup is finiteness-preserving, then for every finite semigroup A, the output function FA → A will be recognizable, since all functions from a finite semigroup are trivially recognizable.However, the condition is strictly stronger, as witnessed by Example 2.7, which is recognizability reflecting (cf.Example 2.14) but not finiteness preserving.As we will see, the stronger condition will characterize exactly the regular string-to-string functions.
The following counterexample illustrates the non-trivial interaction between naturality of the output mechanism and the requirement that the functor is finiteness preserving.▶ Example 3.1.Consider the powerset functor P from Example 2.2.It is finiteness preserving, since the powerset of a finite semigroup is also finite.One could imagine that using powersets, one could construct a transducer semigroup that recognizes functions that are not regular, e.g. because they have exponential growth (unlike regular functions, which have linear growth).It turns out that this is impossible, because there is no possible output mechanism, i.e. no natural transformation of type PA → A, as we explain below.
The issue is that the naturality condition disallows choosing elements from a subset.To see why, consider a semigroup A with two elements, with the trivial left zero semigroup structure.For this semigroup, the output mechanism of type PA → A would need to choose some element a ∈ A when given as input the full set A ∈ PA.However, none of the two choices is right, because swapping the two elements of A is an automorphism of the semigroup A, which maps the full set to itself, but does not map any element to itself.
We now state the main theorem of this paper.
▶ Theorem 3.2.The following conditions are equivalent for every string-to-string function: 1. it is a regular string-to-string function; 2. it is recognized by a transducer semigroup in which the functor is finiteness preserving; 3. it is recognized by a transducer semigroup in which the functor F maps the singleton semigroup 1 to a finite semigroup: |F1| < ∞.
(Note that (2) ⇒ (3) is immediate.)Here is the plan for the rest of this section: Section 3.1 gives a formal definition of regular functions Section 3.2 proves the easy implication in the theorem, namely (1) ⇒ (2) Section 3.3 proves the hard implication in the theorem, namely (3) ⇒ (1) Before continuing, we remark on one advantage of the characterization in item (2), namely a straightforward proof of closure under composition.In contrast, for some (but not all) existing models defining regular string-to-string functions, composition requires a non-trivial construction -examples include two-way transducers [11,Theorem 2]  Proof.This is because finiteness-preserving functors are closed under composition, natural families of output functions are also closed under composition, and naturality means by definition that the output functions "commute" in a suitable sense with functors.
More precisely, consider the following diagram: where the square commutes because the output mechanism is natural.The upper path describes the composition of two functions recognized by the transducer semigroups (F, out) and (F ′ , out ′ ), while the lower path describes a function recognized by (FF ′ , out F ′ (−) •out ′ ).◀

Defininition of streaming string transducers
In this section, we formally describe the regular functions, using a model based on streaming string transducers (sst).This model, like our proof of Theorem 3.2, covers a slightly more general case, namely string-to-semigroup functions instead of only string-to-string functions.These are functions of type Σ * → A where Σ is a finite alphabet and A is an arbitrary semigroup.The purpose of this generalization is to make notation more transparent, since the fact that the output semigroup consists of strings will not play any role in our proof.
The model uses registers to store elements of the output semigroup.We begin by describing notation for registers and their updates.Suppose that R is a finite set of register names, and A is a semigroup called the output semigroup.We consider two sets Below we show two examples of register updates, presented as assignments, using two registers X, Y and the semigroup A = a * .(The right-hand sides are the values in (A + R) + .) The crucial property is being copyless -a register update is called copyless if every register name appears in at most one right-hand side of the update, and in that right-hand side it appears at most once.The main operation on these sets is application: a register update u can be applied to a register valuation v, giving a new register valuation vu.
In our model of streaming string tranducers, the registers will be updated by a stream of register updates that is produced by a rational function, defined as follows.Intuitively speaking, a rational function corresponds to an automaton that produces one output letter for each input position, with the output letter depending on regular properties of the input position within the input string.More formally: ▶ Definition 3.4.A rational function of type Σ * → X * -where Σ is a finite alphabet but X can be any set -is a length-preserving4 function with the following property: for some family 5   f a : Σ * × Σ * equipped with componentwise multiplication → Γ for a ∈ Σ of recognizable functions, for every input a 1 . . .a n and i ∈ {1, . . ., n}, the i-th output letter is Note that the range of a rational function with codomain X * may contain only finitely many "letters" from X, so it can always be seen as a string function over finite alphabets.
Having defined register updates and rational functions, we are ready to introduce the machine model used in this paper as the reference definition of regular functions.▶ Definition 3.5.The syntax of a streaming string transducer (sst) is given by: A finite input alphabet Σ and an output semigroup A.
A finite set R of register names.All register valuations and updates below use R and A.
A designated initial register valuation, and a final output pattern in R + (that does not need to be copyless, though adding this restriction would not affect the expressive power).An update oracle, which is a rational function of type Σ * → (copyless register updates) * .The semantics of the sst is a function of type Σ * → A defined as follows.When given an input string, the sst begins in the designated initial register valuation.Next, it applies all updates produced by the update oracle, in left-to-right order.Finally, the output of the sst is obtained by combining the last register values according to the final output pattern.
▶ Example 3.6.We define an sst that computes the function of Example 2.8.It has two registers X and Y , whose initial valuation is X = Y = ε, and the final output pattern is Y X.The update associated to an input letter ℓ ∈ {a, b, c} at position i is: if the position i is part of the longest c-free prefix, then X := Xℓ, otherwise X := X; if the position i is part of the longest c-free suffix, then Y := Y ℓ, otherwise Y := Y .This sequence of updates can be produced by a rational function generated by a family of functions (f ℓ ) ℓ∈{a,b,c} that are recognized by B 2 , where B is the monoid of booleans with conjunction (rephrase the conditions as "there is no c to the left (resp.right) of i").
In a rational function, the label of the i-th output position is allowed to depend on letters of the input string that are on both sides of the i-th input position; this corresponds to regular lookaround in a streaming string transducer.Therefore, the model described above is easily seen to be equivalent to copyless ssts with regular lookaround, which are one of the equivalent models defining the regular string-to-string functions, see [3, Section IV.C].

From a regular function to a transducer semigroup
Having defined the transducer model, we prove the easy implication in Theorem 3.2.It is apparent from Definition 3.5 that every regular function can be decomposed as a rational function followed by a function computed by a streaming string transducer whose i-th register update depends only on the i-th input letter -let us call that a local sst.Thanks to closure under composition (Proposition 3.3), we only need to handle these two special cases: we show that finiteness-preserving transducer semigroups recognize all rational functions in Section 3.2.1;and all local streaming string transducers in Section 3.2.2.

Recognizing rational functions by transducer semigroups
Consider a rational function, generated by the family (f a ) a∈Σ of recognizable functions of type Σ * × Σ * → Γ.By definition of recognizability, each f a decomposes into where h a is a semigroup homomorphism and B a is finite.
One can check that every f a then factors through a monoid morphism to the finite monoid Thus, without loss of generality, we may assume for the rest of the proof that all of the above semigroups B a are equal to a common finite monoid B and that each semigroup homomorphism h a is in fact a monoid morphism.For any semigroup A, we let 6 FA = B × (B → A) × B, endowed with the following semigroup operation: The construction F is extended to morphisms by considering B → A as the set of B-indexed tuples (cf.Example 2.2) of elements of A. To get a transducer semigroup, we take the output mechanism to be (ℓ, φ, r) → φ(e) where e ∈ B is the neutral element.
Our rational function is then recognized by the unique monoid homomorphism of type Σ * → F(Γ * ) (indeed, F preserves monoids) which maps a ∈ Σ to h a (a, ε), g a , h a (ε, a) .

From a local SST to a transducer semigroup
Suppose now that a string-to-semigroup function f : Σ * → A is computed by some local streaming string transducer.In the proof below, when referring to register valuations and register updates, we refer to those that use the registers and output semigroup of the fixed transducer.We say that a register update is in normal form if, in every right-hand side, one cannot find two consecutive letters from the semigroup A. Any register update can be normalized, i.e. converted into one that is in normal form, by using the semigroup operation to merge consecutive elements of the output semigroup in the right-hand sides.Here is an example, which uses three registers X, Y, Z and the semigroup A = ({0, 1}, •): The register updates before and after normalization act in the same way on register valuations.If an update is copyless and in normal form, then the combined length of all right-hand sides is at most three times the number of registers.Therefore, if a semigroup A is finite, then the set of copyless register updates in normal form, call it UA, is also finite.(However, there are infinitely many copyful register updates even when A is finite.)This set UA can be equipped with a composition operation 6 A construction similar in spirit to the classical two-sided semidirect product [20, §6].
which is defined in the same way as applying a register update to a register valuation, except that we normalize at the end.This composition operation is associative, and compatible with applying register updates to register valuations, in the sense that (vu 1 )u 2 = v(u 1 u 2 ) holds for every valuation v and all updates u 1 and u 2 .Therefore, A → UA is a finitenesspreserving semigroup-to-semigroup functor (with the natural extension to morphisms, where the homomorphism is applied to every semigroup element in a right-hand side).The functor U described above is almost but not quite the functor that will be used in the transducer semigroup that we will define to prove the easy implication in Theorem 3.2.That functor F will also take into account the initial register valuation: endowed with the trivial left zero semigroup structure with componentwise multiplication & action on morphisms Given (u, v) ∈ FA, the output mechanism in the transducer semigroup applies the register update u to the register valuation v, and then multiplies together the register values given by the resulting valuation vu according to the final output pattern.Using this, we can recognize f via the homomorphism that sends each input letter to: the register update that this letter determines (our sst being local) in the first component; the designated initial register valuation in the second component.

From a transducer semigroup to a regular function
We now turn to the difficult implication (3) ⇒ (1) in Theorem 3.2.The proof is presented in a way which, if sometimes slightly verbose, makes it easier to see how it can be adapted to other algebraic structures instead of semigroups (such as forest algebras, cf.Section 4).

Polynomial functors and functorial streaming string transducers
The assumption of the implication uses an abstract model (transducer semigroups), while the conclusion uses a concrete operational model (streaming string transducers).To bridge the gap, we use an intermediate model, similar to ssts, but a bit more abstract.The abstraction arises by using polynomial functors instead of registers, as described below.
▶ Definition 3.7.By polynomial functor, we mean a semigroup-to-set functor of the form where Q is some possibly infinite set, whose elements are called components, with each component having an associated dimension in N. The symbol stands for disjoint union of sets.This functor does not take into account the semigroup structure of the input semigroup, since the output is seen only as a set.On morphisms, the functor works in the expected way, i.e. coordinate-wise.
A finite polynomial functor is a polynomial functor with finitely many componentsfor example, A → A 2 + A 2 + A. The notion of finite polynomial functor can be seen as a mild generalization of the construction which maps a semigroup A to the set A R of register valuations for some fixed finite set R of register names.In the generalization, we allow a variable number of registers, depending on some finite information (the component).
Having defined a more abstract notion of "register valuations", namely finite polynomial functors, we now define a more abstract notion of "register updates".The first condition for such updates is that they do not look inside the register contents; this condition is captured by naturality (as discussed in Remark 2.9).▶ Example 3.8.Consider the polynomial functors (where 1 represents the singleton set A 0 ) An example of a natural transformation between these two functors is the function which maps a nonempty list in A * to the product of its elements, and which maps the empty list to the unique element of 1.A non-example is the function that maps a list [a 1 , . . ., a n ] ∈ A * to the leftmost element a i that is an idempotent in the semigroup, and returns 1 if such an element does not exist.The reason why the non-example is not natural is that a semigroup homomorphism can map a non-idempotent to an idempotent.
Apart from naturality, we will want our register updates to be copyless.For the purposes of the following definition, let us call a tuple of numbers in N k a "sub-unit" if it belongs to {0, 1} k and at most one coordinate is equal to 1 -or, as an edge case, if k = 0.For a polynomial functor F, a sub-unit of FN = q N dim(q) is a sub-unit of any of the N dim(q) .▶ Definition 3.9 (Copyless natural transformation).A natural transformation between two polynomial functors F and G is called copyless if when instantiated to the semigroup 7 (N, +), the corresponding function of type FN → GN maps sub-units to sub-units.
It will be convenient to speak of natural functions f : FA → GA, where F and G are semigroup-to-set functors and A is a fixed semigroup, to refer to functions that can be extended to natural transformations (f B : FB → GB) B semigroup , with f = f A .Copyless natural functions between instantiations of polynomial functors are defined analogously.
Having defined functions that are natural and copyless, we now describe the more abstract model of ssts used in our proof.The main difference is that instead of register valuations and updates given by some finite set of register names, we have two abstract polynomial functors, one of them finite polynomial, together with an explicitly given application function.We also allow the computation to be initialized and finalized in a more liberal way, that may depend on a regular property of the input.
▶ Definition 3.10.The syntax of a functorial streaming string transducer is given by: A finite input alphabet Σ and an output semigroup A.
A finite polynomial functor R, called the register functor, and a (not necessarily finite) polynomial functor U called the update functor.
A copyless natural function of type RA × UA → RA, called application.An initial function Σ * → RA which is recognizable (and therefore has finite range).
A polynomial final data functor K, a final data function Σ * → KA which is recognizable, and a final output function of type RA × KA → A which is a natural function (but not necessarily copyless).An update oracle, which is a rational function of type Σ * → (UA) * .Analogously to Definition 3.5, the functorial sst computes the function Σ * → A obtained by the following composition, where the first map bundles together the initial function, the update oracle and the final data function: In the appendix, we prove that this model is no more expressive than usual copyless ssts.

Coproducts and views
Apart from the more abstract transducer model from Definition 3.10, the other ingredient used in the proof of the hard implication in Theorem 3.2 will be coproducts of semigroups, and some basic operations on them, as described in this section.
The coproduct8 of two semigroups A and B, denoted by A ⊕ B, is the semigroup whose elements are nonempty words over an alphabet that is the disjoint union of A and B, restricted to words that are alternating in the sense that two consecutive letters cannot belong to the same semigroup.The semigroup operation is defined in the expected way.We draw elements of a coproduct using coloured boxes, with the following picture showing the product operation in the coproduct of two copies, red and blue, of the semigroup {a, b} + : A coproduct can involve more than two semigroups; in the pictures this would correspond to more colours, subject to the condition that consecutive boxes have different colours.▶ Remark 3.12.The copyless register updates u : R → (A + R) + of ordinary ssts that are in normal form (cf. Section 3.2) can be seen as maps R → A ⊕ X∈R {X} + .
We write 1 for the semigroup that has one element.This semigroup is unique up to isomorphism and it is a terminal object in the category of semigroups, which means that it admits a unique homomorphism from every other semigroup A. This unique homomorphism will be denoted by !: A → 1. (It has no connection with the factorial function on numbers.) Consider the semigroup-to-set functors defined by (the underlying set of) a coproduct of several copies of their argument with several copies of 1, such as A → A ⊕ A ⊕ A ⊕ 1 ⊕ 1.In our proof, it will be useful to see them as polynomial functors, even though strictly speaking they are not defined as sums of products.This identification is allowed by the following observation (stated for A ⊕ 1 for convenience, but the same idea applies in general).

▶ Proposition 3.13. There is a family of bijections, natural in the semigroup A, between
A ⊕ 1 and q∈1⊕1 A dimension of q , where the dimension of q is the number of times that the first copy of 1 appears in q.
Idea.Given x ∈ A ⊕ 1, we apply !: A → 1 to the elements of A in x to determine the component q of the polynomial functor that contains the image of x by the left-to-right bijection.This operation, a special case of what is called the shape below, forgets those elements of A appearing in x, so we record them in a tuple living in A dim(q) .For example, aba ◀ The crucial property of semigroups that will be used in our proof is Lemma 3.14 below, which says that an element of a coproduct can be reconstructed based on certain partial information.This information is described using the following operations.

Merging. Consider a coproduct A 1 ⊕ • • • ⊕
A n , such that the same semigroup A appears on all coordinates from a subset I ⊆ {1, . . ., n}, and possibly on other coordinates as well.
Define merging the parts from I to be the function of type that is defined in the expected way, and explained in the following picture.In the picture, merging is applied to a coproduct of three copies of the semigroup {a, b} + , indicated using colours red, black and blue, and the merged coordinates are red and blue: the merge of red and blue is drawn in violet .

Shape. Define the shape operation to be the function of type
obtained by applying ! on every coordinate.The shape says how many alternating blocks there are, and which semigroups they come from, as explained in the following picture:

Views. The final operation is the i-th view
This operation applies ! to all coordinates other than i, and then it merges all those coordinates.Here is a picture, in which we take the view of the blue coordinate: The key observation is that an element of a coproduct is fully determined from its shape and views, as stated in the following lemma.It seems to contain the essential property of semigroups that makes the construction work.We expect our theorem to also be true for other algebraic structures for which the lemma is true; however, the lemma seems to fail in certain settings.Concrete examples will be discussed in the conclusion (Section 4).
▶ Lemma 3.14.Let A 1 , . . ., A n be semigroups.The deconstruction function of type which is obtained by combining the views for all i ∈ {1, . . ., n} and the shape, is injective.
We prove this by exhibiting an explicit partial left inverse: a reconstruction function of type such that deconstruction followed by reconstruction maps every element of The idea is to start with the shape and replace the entries from 1 with the elements appearing in the views in the right order.Rather than a formal definition, we illustrate this on an example (in the 3 views, we omit the boxes around the 1s to avoid visual cluttering): Besides proving Lemma 3.14, this reconstruction function also enjoys the following property, which can be seen from the definition and Proposition 3.13.

▶ Proposition 3.15.
When each A i is either A or 1, reconstruction can be seen as a copyless natural function between polynomial functors in A.

Factorized output
Now, consider some transducer semigroup, with the functor being F, and fix a string-tosemigroup function f : Σ * → A that decomposes as some homomorphism h : Σ * → FA followed by the output function of type FA → A.
For semigroups A 1 , . . ., A n , define the vectorial output function to be the function of type that is obtained by the composition of three functions described below (where co-projection is the function outputs a singleton list containing its input): To illustrate the definitions in this section, we use a running example with the transducer semigroup for the "reverse then duplicate" function from Example 2.6.The functor F sends a semigroup A to the opposite semigroup (cf.Example 2.2), and the output mechanism is a → aa.Our example function on {a, b} * is obtained by composing the string reversal homomorphism {a, b} * → F({a, b} * ) with the output function.Here is an example of the vectorial output function (for now, the homomorphism plays no role): The vectorial output function is natural in all of its arguments, which means that for all semigroup homomorphisms h 1 , . . ., h n , the diagram below commutes: This is because each of the three steps in the definition of the vectorial output function is itself a natural transformation, and natural transformations compose.Naturality of the first two steps is easy to check, while for the last step we use the assumption that the (non-vectorial) output function is natural.
Let us return to our function f = out A • h recognized by our transducer semigroup (F, out).For strings w 1 , . . ., w n ∈ Σ * , define the corresponding factorized output to be the result of first applying the semigroup homomorphism h : Σ * → FA to all the strings, and then applying the vectorial output function; we denote it by Here is the factorized output illustrated on our running example (we use colours to distinguish which of the three parts of the input is used): ⟨abb|ε|baaba⟩ = abaab ε bba abaab ε bba ∈ {a, b} * ⊕ {a, b} * ⊕ {a, b} * .
As we can see above, when the output semigroup is a free monoid, the factorized output morally tells us "which part of the output string comes from which part in the input string".▶ Remark 3.16.This is similar to the idea of origin semantics [5] of regular functions (see also [17,Section 5]).Indeed, our definition of factorized output is inspired by a similar tool of the same name that appears in the study of origin semantics [5, Section 2].
We also use a similar notation but with some input strings underlined, e.g. the input could be ⟨abb|ε|baaba⟩ with an underline for the first red part.In the underlined case, before applying the vectorial output function, we apply h to the non-underlined strings and (F! • h) : Σ * → F1 to the underlined strings.In our running example, we have

Proof of (3) ⇒ (1) in Theorem 3.2
We have now collected all necessary ingredients to prove this hard direction of the equivalence.Therefore, our goal is now to show that the function f : Σ * → A that we have previously fixed is computed by some functorial streaming string transducer as in Definition 3.10, assuming that F1 is finite.
The idea is that we want the functorial sst to maintain the following invariant: after processing the first i letters in an input string a 1 • • • a n , the register valuation is equal to the factorized output This way, after processing all input letters, the last valuation ⟨a 1 • • • a n |ε⟩ is very close to the output; indeed, if we see A as a 1-ary coproduct, then f (a 1 . . .a n ) = ⟨a 1 . . .a n ⟩ ∈ A.
The naive choice for the register functor is then R ′ : A → A ⊕ 1, since ⟨w|v⟩ ∈ A ⊕ 1 for all w, v ∈ Σ * by definition.However, while R ′ can be seen as a polynomial semigroup-to-set functor, whose set of components is 1 ⊕ 1 (cf.Proposition 3.13), it is not finite polynomial (the set 1 ⊕ 1 is infinite).That said, we have by naturality of vectorial output: 99 Let us give some details.From Proposition 3.13, we see that the component in 1 ⊕ 1 of an element in A ⊕ 1 is obtained by applying !⊕ 1.So it suffices to show that Expanding the definitions, our goal can be rewritten as This is a direct consequence of the naturality of the vectorial output function, that can be expressed as follows: for every semigroup homomorphism g : A → B, • vectorial output for A (take g = !and apply both sides of the equality to (h(w), F! • h(v)) to get the desired Claim 3.17).
▷ Claim 3.17.The component for ⟨w|v⟩ ∈ This index is determined by definition by the values of (F! • h) : Σ * → F1 on w and v, where h : Σ * → FA is the homomorphism used to recognize f .Since F1 is finite, the ⟨w|v⟩ for w, v ranging over Σ * live in finitely many components.We take our register functor RA ⊂ R ′ A to be the finite polynomial functor consisting of these "useful" components, plus the unique component that does not use A (it will serve as a "null value").
To design the register updates, the key is the following lemma.It shall be proved later using the machinery of views on coproducts that we have introduced for this very purpose.

▶ Lemma 3.18. There are two copyless natural functions
such that, for every pair of strings w, v ∈ Σ * and every letter a ∈ Σ, Again, to make "copyless natural" meaningful in this context, we invoke Proposition 3.13 to see δ and κ as functions between polynomial functors in A. This leads us to use the update functor U : A → 1 ⊕ A ⊕ 1 and to define the application of updates to registers, of type RA × UA → RA, to be δ followed by the map (A ⊕ 1) + 1 → RA which sends the components of A ⊕ 1 that are in RA to themselves, and everything else to the "null value".As an direct consequence of the lemma, the desired invariant holds using the initial function w → ⟨ε|w⟩, and the update oracle a 1 . . .a n → ⟨ε|a 1 |a 2 . . .a n ⟩ . . .⟨a 1 . . .a n−1 |a n |ε⟩.To fit Definition 3.10, we have to check that the initial function is recognizable and that the update oracle is a rational function; by definition, the latter amounts to saying that for any a ∈ Σ, the function (w, v) ∈ (Σ * ) 2 → ⟨w|a|v⟩ is recognizable.According to the definition of factorized output, the initial function factors through the semigroup homomorphism F! • h, whose codomain F1 is finite; therefore, the initial function is recognizable.The other recognizability condition holds for a similar reason.
To finish building our functorial streaming string transducer, we use the function κ from Lemma 3.18.Thanks to our invariant and to the equation concerning κ, it is immediate that the following choices lead to a functorial sst that indeed computes f .We take: the final data functor K : A → (1 ⊕ A) × A, the final data function w ∈ Σ * → (⟨w|ε⟩, some arbitrary fixed value in A) -once again, it is recognizable because F1 is finite, and the final output function RA × KA → A that proceeds as follows: first, it applies κ to get some value in (A + 1) × A; if the left half of the pair is in A, it returns it; otherwise, it returns the right half.This being done, let us discharge our only remaining subgoal.
Proof of Lemma 3.18.We cover here the part concerning δ; for κ, the arguments are similar and a bit simpler.We use the following claim, which is proved using mechanical diagram chasing (as detailed in the appendix).Recall that the merging, shape and view operations were introduced just before Lemma 3.14.
▷ Claim 3.19.⟨wa|v⟩ is obtained from ⟨w|a|v⟩ by merging the first two parts in A ⊕ A ⊕ 1.
The above claim shows that the factorized output ⟨wa|v⟩ is obtained from ⟨w|a|v⟩ by a copyless natural function.In turn, ⟨w|a|v⟩ is the image by the reconstruction function -which is copyless natural (Proposition 3.15) -of the following four items (the equalities below are proved similarly to Claims 3.17 and 3.19): 1.First view of ⟨w|a|v⟩, which is equal to ⟨w|av⟩ -this is the first argument which is passed, in the lemma statement, to the function δ that we want to define.2. Second view of ⟨w|a|v⟩, which is obtained by merging the first and third parts in ⟨w|a|v⟩.3. Third view of ⟨w|a|v⟩, which is equal to ⟨wa|v⟩.4. Shape of ⟨w|a|v⟩, which is equal to ⟨w|a|v⟩.
To complete the proof, it remains to justify that the last three items above can be collectively obtained from the second argument given to δ, namely ⟨w|a|v⟩, by applying some copyless natural function.Each item is obtained separately by applying a natural function.Furthermore, the second item is obtained in a copyless way, while the last two items do not use A at all, and therefore they are obtained in a copyless way for trivial reasons, even when combined with the second item.◀

Conclusions
In this paper, we have exhibited a concise algebraic characterization of the regular string-tostring functions, in the style of the definition of regular languages using recognizability by finite semigroups.To perform this extension from languages to functions, we have relied on the basic concepts of category theory: categories, functors, natural transformations.
It should be noted that our use of categories is quite different in spirit from many of the works that take a categorical perspective on automata-theoretic results -see for instance [12], whose introduction points to many further references.In such works, the correspondence between concrete automata models and their rephrasing as suitable (co)algebras or functors tends to be straightforward, with the technical focus lying elsewhere (typically, in generalizing constructions such as determinisation or minimisation).On the contrary, we define a truly new transducer model whose equivalence with the preexisting copyless streaming string transducers requires a non-trivial proof.
An advantage of our characterization of the regular string functions is that, as one would expect from an abstract result, it lends itself to generalizations.
Semigroup-to-semigroup functions.The notion of recognition by a finiteness-preserving transducer semigroup makes sense for functions between arbitrary semigroups.Furthermore, such functions are closed under composition (the proof of Proposition 3.3 works as it is).To check their robustness, it would be desirable to have a more concrete, machine-like model capturing the same function class; possibly a variant of streaming string transducers where the underlying finite automaton is morally "replaced" by a finite semigroup.

More string functions.
Another direction is characterizing other classes of string-to-string functions, such as the rational functions or the polyregular functions [7].In this paper, we have discovered that, somewhat mysteriously, combining two conditions -naturality and preserving finiteness -characterizes exactly the regular functions, which have linear growth.Perhaps there is some way of tweaking the definitions to describe, say, some class with polynomial growth.For instance, the squaring function (Example 2.7) seems to be recognized by a mixed-variance functor A → (A → A) × A with a dinatural output mechanism.
Functions on other free algebras.The definition of a transducer semigroup can applied to other algebras, and not just semigroups.This may be done by taking some monad T and considering functions that can be decomposed, for some endofunctor F of the category of Eilenberg-Moore algebras for the monad T and some natural transformation out, as An example of this approach is forest algebras [6, Section 5], which are algebras for describing trees.Preliminary work shows that, in the case of forest algebras, the suitable version of Theorem 3.2 also holds, i.e. the finiteness-preserving functors lead to a characterization of the standard notion of regular tree-to-tree functions, namely mso transductions (see [14,9]).We believe that these results apply even further, namely for graphs of bounded treewidth, modeled using suitable monads [6,Section 6].The crucial property is that Lemma 3.14, about reconstructing a coproduct from its views, holds for other monads than just the nonempty list monad for semigroups.Unfortunately, this lemma fails for some monads, such as the monad of formal linear combinations of strings that corresponds to weighted automata.In the future, we intend to conduct a more systematic investigation of the extent to which the characterizations from this paper can be generalized to other algebraic structures.

A of Theorem 2.13
We prove a slight strengthening of the theorem, which concerns not only string-to-string functions, but also functions f : A → B between semigroups, not necessarily homomorphisms, such that the target semigroup B is finitely generated.(The free monoid of strings Σ * is generated as a semigroup by {ε} ∪ Σ, which is a finite set since we always assume that our strings are over a finite alphabet.) (1) ⇒ (2).We use a similar construction as in the proof of Theorem 2.10.Let f : A → B be recognizability reflecting.Define a functor as follows: Similarly to Theorem 2.10, the semigroup operation on the first coordinate of FC is inherited from A, and on the second coordinate we use the left zero semigroup structure, where the product of g and h is g.On morphisms, the functor is defined as in the proof of Theorem 2.10.The output mechanism is (a, g) → g(f (a)) -think of it as function application with f inserted as an interface.The transducer semigroup thus defined recognizes the function f via the homomorphism a ∈ A → (a, id).
We now argue that the output mechanism for a finite semigroup C is a recognizable function.To do so, we show that the inverse image of every c ∈ C is a recognizable subset of FC.This inverse image is Each of the sets in this union is recognizable, by the assumption that f is recognizability reflecting.By the assumptions that B is finitely generated and that C is finite, there are finitely semigroup homomorphisms B → C, and therefore the union is finite.This implies that the inverse image of c is recognizable, as a finite union of recognizable subsets of FC.
(1) ⇐ (2).Take a function f : A → B that satisfies (2), i.e. it is a composition where h is some homomorphism.We want to show that f is recognizability reflecting.To prove this, let us consider some recognizable function from the output semigroup where C is a finite semigroup and h ′ is a homomorphism.We want to show that its precomposition f is also recognizable.Consider the following diagram.
The triangle, with D finite, describes the assumption that the output function out C is recognizable when C is finite.The upper path from A to X describes the precomposition by f .The rectangle commutes by naturality of the output mechanism, and therefore the upper path describes the same function as the lower path from A to X.The lower path is a recognizable function, since the first three arrows are homomorphisms and D is finite.

B Proof of Lemma 3.11
In this part of the appendix, we prove that the models defined in Definitions 3.5 and 3.10 define the same string-to-semigroup functions.The easy implication is left-to-right.A streaming string transducer as in Definition 3.5 can be seen as a special case of a functorial sst as in Definition 3.10, because the sets of register valuations and register updates in normal form (cf. Section 3.2) are constructed using finite polynomial functors, and the application operation is a copyless natural function.
The rest of this section is devoted to the harder right-to-left implication, that is, to translating functorial ssts into ordinary ssts.

B.1 A syntactic description
The main step in the proof of this harder direction is an analysis of what can be done by copyless natural functions between polynomial functors.
We begin with monomial functors, i.e. polynomial functors with one component.Consider two monomial functors, say A → A k and A → A ℓ , for some k, ℓ ∈ N = {0, 1, . ..}.One way of specifying a natural transformation between these two functors is to start with a function α : {1, . . ., ℓ} → {1, . . ., k} + , ( which we call a syntactic description, and to then define the natural transformation as follows.Proof.Concretely, the syntactic description may be recovered from the natural transformation instantiated at the free semigroup {1, . . ., k} + by applying it to (1, . . ., k) ∈ ({1, . . ., k} + ) k .While it can be checked by hand that this works, more conceptually, it is an instance of the Yoneda lemma.Indeed, there is a natural bijection A k ∼ = A {1,...,k} ∼ = {semigroup homomorphisms {1, . . ., k} + → A} so monomial functors are representable.The Yoneda lemma gives us a bijection between natural transformations A k → A ℓ and homomorphisms {1, . . ., ℓ} + → {1, . . ., k} + .◁ The advantage of the syntactic description, which is unique, is that it allows us to define the copyless restriction in a more syntactic way, reminiscent of the definition used in Definition 3.5: a natural function between two monomial functors is copyless if and only if its syntactic description has the following property: (*) concatenating all ℓ output strings gives a string where each letter from {1, . . ., k} appears at most once.
To extend this definition of copylessness to a natural transformation η between general polynomial functors we that if x ∈ FA is in the input component q, then its image η A (x) ∈ GA lives in some output component p determined by q and η independently of A. This can be seen from the naturality diagram below for the terminal morphism !: A → 1: Thus, the restriction of η to the component q of its domain is a natural transformation of monomial functors A dim(q) → A dim(p) .We then have: A natural transformation between polynomial functors is copyless in the sense of Definition 3.9 if and only if for every input component, the corresponding natural transformation between monomial functors is copyless.
Proof.By considering the naturality diagrams for the homomorphisms h : {1, . . ., k} + → N such that h(1) 2 Proof of the left-to-right implication in Lemma 3.11.
Simplifying the initialization/finalization.In Definition 3.10, we allow the initial register valuation to depend in a recognizable way on the input, and similarly for some "final data" that is used by the final output function.We sketch here a translation from this model to one where the initial valuation is a constant (as in Definition 3.5), and the output is determined by a copyless natural function without auxiliary data.The idea is that the new functorial sst will simulate in parallel the old functorial sst for all possible initial valuations, and then decide after reading the whole word which of the computations to keep.Let R be the register functor of our functorial sst, U be the update functor and K be the final data functor.Let h : Σ * → S be a homomorphism from input words to a finite semigroup that recognizes both the initial valuation function and the final data function; that is, these functions can be decomposed respectively as init • h and findat • h.Our simplified functorial sst will use the register functor A → (RA) S × (KA) S × S & the update functor A → (UA) S × S The initial register valuation of our simplified device is (init(s)) s∈S , (findat(s)) s∈S , h(ε) For each input position, the corresponding update in (UA) S × S is a pair consisting of the constant S-indexed tuple whose components are equal to the update for this position in the original functorial sst, and of the image of the input letter by h.The application of an update ((u s ) s∈S , s) to a register valuation (r s ) s∈S , (d s ) s∈S , s is We shall convert it to an equivalent ordinary sst using the syntactic descriptions introduced in the previous subsection.For an input string a 1 • • • a n , consider the sequence of register valuations v 1 , . . ., v n ∈ RA, such that v i arises by applying the first i register updates produced by the update oracle.Let k be the maximal dimension of the components in Q (here we use the assumption that R is a finite polynomial functor).Define a register valuation w i ∈ A k+1 as follows: take the register valuation used by v i , and pad it to a tuple of length k + 1 using some distinguished element a 0 ∈ A. In particular, since k is the maximal dimension of Q, we are guaranteed that the last coordinate with index k + 1 stores the distinguished element a 0 .We will build a streaming string transducer, as in Definition 3.5, in which the set of register names is {1, . . ., k}.
We begin by looking at the components.Let q i ∈ Q be the component of the register valuation v i .The first observation is that q i depends only on q i−1 and the i-th register update.Therefore, the sequence q 1 • • • q n can be produced by a rational function.The next observation is that, once we know the components q i−1 and q i , and the register update u i ∈ UA that would be applied in the original transducer to from v i−1 to v i , then we can create a copyless register update that transforms w i−1 into w i .This is done by using the syntactic descriptions of natural functions that were described above.Once we have the register valuations w 1 , . . ., w n , the output of the transducer can be easily obtained.

C Proof of Claim 3.19
The property that we want on factorized outputs reduces immediately to the following lemma on vectorial outputs, by taking C = 1.which is an elementary property of the coproduct ⊕ in any category (indeed, the generic categorical definition of merging uses a co-paring, which "cancels out" with the co-projection).
There remains to check that the first of the two diagrams combined by × is also commutative; towards this purpose, we have added some parts in the middle: semigroup operation F(co-proj) semigroup op The upper right trapeze commutes because F applied to the co-projection of a into A ⊕ C is a homomorphism.The lower triangle commutes because F(merge) is a homomorphism.Finally, the small upper left triangle can be shown to commute using the functoriality of F followed by properties of the coproduct.◀ or copyless streaming string transducers [2, Theorem 1].▶ Proposition 3.3.Functions recognized by finiteness-preserving transducer semigroups are closed under composition.

(
apply u s to r s ) s∈S , (d s ) s∈S , s s Finally, the final output function of the simplified functorial sst is (r s ) s∈S , (d s ) s∈S , s → apply original final input function to r s and d s Translating a functorial SST.Now, consider a simplified functorial streaming string transducer, which uses the register and update functors RA = q∈Q A dim q UA = p∈P A dim p .

▶ Lemma C. 1 .FA 2 F(A ⊕ A ⊕ C) 2 F 3 (merge
Let A and C be semigroups.The following diagram commutes: By expanding the definition of the vectorial output functions, and by using the associativity of three-fold multiplication, we see that the diagram in the lemma statement corresponds to the outer rectangle below:FA × FA × FC × FC F(A ⊕ A ⊕ C) 3 F(A ⊕ C) (A ⊕ A ⊕ C) F(A ⊕ C) A ⊕ A ⊕ C A ⊕ C F(co-projection)The lower rectangle commutes by naturality of the output mechanism.The middle trapeze commutes because merging is a semigroup homomorphism.Concerning the upper trapeze, it can be decomposed as the bifunctor × applied to two diagrams that we can analyze independently.We start by looking at the second of those, which is simpler:FC FC F(A ⊕ A ⊕ C) F(A ⊕ C) F(A ⊕ A ⊕ C) F(co-projection) id C F(co-projection) id F (merge) By functoriality of F, the commutativity of this diagram reduces to merge • (co-projection of C into A ⊕ A ⊕ C) = co-projection of C into A ⊕ C 1. it is recognizability reflecting.2. it is recognized by a transducer semigroup such that for every finite semigroup A, the corresponding output function of type FA → A is recognizable.is a congruence of finite index.Extending this slightly to handle the case (a, 0) → a, one can show that the output mechanism out A of Example 2.7 is recognizable whenever A is finite.Therefore, the squaring function is recognizability reflecting.