{'fraction_non_alphanumeric': 0.057620905202801144, 'fraction_numerical': 0.01337124594632702, 'mean_word_length': 3.449032932566649, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 2, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 67, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Combining Insertion and Deletion in RNA-editing Preserves Regularity

Inspired by RNA-editing as occurs in transcriptional processes in the living cell, we introduce an abstract notion of string adjustment, called guided rewriting. This formalism allows simultaneously inserting and deleting elements. We prove that guided rewriting preserves regularity: for every regular language its closure under guided rewriting is regular too. This contrasts an earlier abstraction of RNA-editing separating insertion and deletion for which it was proved that regularity is not preserved. The particular automaton construction here relies on an auxiliary notion of slice sequence which enables to sweep from left to right through a completed rewrite sequence.


Introduction
We study an elementary biologically inspired formalism of string replacement referred to as guided rewriting. Given a fixed and finite set G of strings, also called guides, a rewriting step amounts to adapting a substring towards a guide. We consider two versions of guided rewriting: guided insertion/deletion, which is close to an editing mechanism as encountered in the living cell, and general guided rewriting based on an adjustment relation, which is mathematically more amenable. For guided insertion/deletion the guide and the part of the string that is rewritten do not need to be of the same length. They are required to be equal up to occurrences of a distinguished dummy symbol. For general guided rewriting the correspondence of the guide and the substring that is rewritten is element-wise. The guide and substring are equivalent symbol-by-symbol according to a fixed equivalence relation called adjustment.
In both cases, for a finite set of guides G, only a finite set of strings can be obtained by repeatedly rewriting a given string. Starting from a language L, we may consider the extension L i/d of the language with all the rewrites obtained by guided insertion/deletion and the extension L G of the language obtained by adding all the adjustment-based guided rewrites. We address the question if regularity of L implies regularity of L i/d and of L G . The results of the paper state that in the case of guided insertion/deletion regularity is preserved if the strings of dummy symbols involved are bounded and that guided rewriting based on adjustment always preserves regularity.
The motivation for this work stems from transcriptional biology. RNA can be seen as strings over the alphabet {C, G, A,U}. Replication of the encoded information is one of the most essential mechanisms in life: strands of RNA are faithfully copied by the well-known processes of RNA-transcription. However, typical for eukaryotic cells, the synthesis of RNA does not yield an exact copy of part of the DNA, but a modification obtained by post-processing. The class of the underlying adjustment mechanisms is collectively called RNA-editing.
Abstracting away from biological details, the computational power of insertion-deletion systems for RNA-editing is studied in [14]: an insertion step is the replacement of a string uv by the string uαv taken from a particular finite set of triples u, α, v. Similarly, a deletion step replaces uαv by uv for another finite set of triples u, α, v. In [10] the restriction is considered where u and v are both empty. The approach claims full computational power, that is, they generate all recursively enumerable languages.
In the RNA-editing mechanisms occurring in nature, however, only very limited instances of these formats apply. Often only the symbol U is inserted and deleted, instead of arbitrary strings α, see e.g. [1]. Therefore, following [15], we investigate guided insertion/deletion focusing on the special role of the distinguished symbol 0, the counterpart of the RNA-base U. However, in order to prove that under this scheme regularity is preserved we extend our investigations to guided rewriting based on an abstraction adjustment relation. In fact, we prove the theorem for guided insertion/deletion by appealing to the result for guided rewriting based on adjustment.
The proof of the latter result relies on reorganizing sequences of guided rewrites into sequences of so-called slices. The point is that, since guides may overlap, each guided rewrite step adds a 'layer' on top of the previous string. In this sense guided rewriting is vertically oriented. E.g., Figure 2 in Section 5 shows six rewrite steps of the string ebcfa yielding the string fbcfb involving eight layers in total. However, in reasoning about recognition by a finite automaton a horizontal orientation is more natural. One would like to sweep from left to right, so to speak. Again referring to Figure 2, five slices can be distinguished, viz. a slice for each symbol of the string ebcfa. The technical machinery developed in this paper allows for a transition between the two orientations.
The organization of this paper is as follows. The biological background of RNA-editing is provided in Section 2. Section 3 presents the theorem on preservation of regularity for guided insertion-deletion. The notion of guided rewriting based on an adjustment relation is introduced in Section 4 and a corresponding theorem on preservation of regularity is presented. To pave the way for its proof, Section 5 introduces the notions of a rewrite sequence and of a slice sequence and establishes their relationship. Rewrite sequences record the subsequent guided rewrites that take place, slice sequences represent the cumulative effect of all rewrites at a particular position of the string being adjusted. In Section 6 we provide, given a finite automaton accepting a language L, the construction of an automaton for the extended language L G with respect to a set of guides G. Section 7 wraps up with related work and concluding remarks.

Biological motivation
This section provides a description of RNA editing from a biological perspective. In this paper we focus on the insertion and deletion of uracil in messenger RNA (mRNA) and provide abstractions of the underlying mechanism in the sequel. However, in the living cell there are different kinds of RNA editing that vary in the type of RNA that is edited and the type of editing operations. Uracil is represented by the letter U. The three other types of nucleotides for RNA, viz. adenine, guanine and cytosine are represented by the letters A, G and C, respectively.
U-insertion/deletion editing is widely studied in the mitochondrial genes of kinetoplastid protozoa [13]. Kinetoplastids are single cell organisms that include parasites like Trypanosoma brucei and Crithidia fasciculata, that can cause serious diseases in humans and/or animals. Modifications of kinetoplastid mRNA are usually made within the coding regions. These are the parts that are translated into proteins, which are the building blocks of the cells. This way coded information of the original gene can be altered and therefore expressed, i.e. translated into proteins, in a varying number of ways, depending on the environment in the cell. This provides additional flexibility as well as potential specialization of different parts of the organisms for particular functions.
Here we describe a somewhat simplified version of the mechanism for the insertion and deletion of U. More details can be found, for instance, in [13,1,3,12]. For simplicity we assume that only identical letters match with one another. In reality, the matching is based on complementarity, usually assuming the so-called Crick-Watson pairs: A matches with U and G matches with C.
In general, a single step in the editing of mRNA involves two strands of RNA, a strand of messenger RNA and a strand of guide RNA, the latter typically referred to as the guide. To explain the mechanism for the insertion of uracil, let us consider an example. See Figure 1. Assume that we start of with an mRNA fragment: u = N 1 N 2 N 3 N 4 N 5 and the guide g = N 2 N 3 UUUN 4 , where N i can be an arbitrary nucleotide A, G or C, but not U. Obviously, there is some match between u and g involving the letters N 2 , N 3 , and N 4 , which is partially 'spoiled' by the UUU sequence. By pairing of letters we have that g attaches to u; the matching substrings N 2 N 3 and N 4 serve as anchors. By chemical reactions involving special enzymes u is split open between N 3 and N 4 . The gap between the anchors is then filled by the enzyme mechanism using the guide as a template. For each letter U in the guide a U is added also in the gap. As a result the mRNA string u is transformed into N 1 N 2 N 3 UUUN 4 N 5 .
In general, one can have more than two anchors (involving only non-U letters) in which the guide and the mRNA strand match. In that case mRNA is opened between each pair of anchors and all gaps between these anchors are filled with U such that the number of Us in the guide is matched.
A similar biochemical mechanism implements the deletion of Us from a strand of mRNA. We illustrate the deletion process on the following example. Let us assume that we have the mRNA strand u = N 1 N 2 N 3 UUN 4 N 5 and the guide g = N 2 N 3 N 4 . Like in the insertion case, g initiates the editing by attaching itself to u at the matching positions N 2 ,N 3 , and N 4 . Only now the enzymatic complex removes the mismatching UU substring between N 3 and N 4 to ensure the perfect match between the substring and the guide. As a result the edited string N 1 N 2 N 3 N 4 N 5 is obtained. In general, we can have several anchoring positions on the same string. In that case, all Us between each two matching positions are removed from the mRNA.
A guide can also induce both insertions and deletions of U simultaneously. For instance the guide N 2 N 3 UUUN 4 can induce editing in parallel of the string N 1 UN 2 UN 3 UN 4 UN 5 UN 6 which results in the string N 1 UN 2 N 3 UUUN 4 UN 5 UN 6 , where the U between N 2 and N 3 has been deleted and two U's between N 3 and N 4 have been inserted. This is done by the same biochemical mechanisms that are involved in separate insertions and deletions. Analogously as above, we can have multiple insertions and deletions induced by the same guide on the original pre-edited sequence.
The net effect of all three cases considered above is that a strand u = xyz, such that y equals g up to occurrences of U, is modified by the insertion and deletion mechanism and becomes a string v = xgz. It is noteworthy that the rewriting system that we describe in the sequel also applies to another case with the same effect. For example, consider a guide g = N 2 N 3 UUUN 4 and a pre-edited mRNA u = N 1 N 2 N 3 UUN 4 N 5 N 6 . Now, to obtain the match of the guide g and a substring y of u, a U is inserted in u, resulting in the string v = N 1 N 2 N 3 UUUN 4 N 5 N 6 . If the U subsequence in y was longer though, like in the case for u = N 1 N 2 N 3 UUUN 4 N 5 N 6 and g = N 2 N 3 UUN 4 , then we have that the extra U in u is removed resulting in To summarize, the mRNA editing mechanism underlying U-insertion/deletion can be interpreted as symbolic manipulations of strings. In the sequel symbol U will be denoted by 0 and obviously plays a special role. The crucial point is that in a single step some substring y is replaced by a guide g for which y and g coincide except for the symbol 0.

Guided insertion / deletion
Inspired by the biological scheme of editing of mRNA as discussed in the previous section, we study the more abstract notion of guided insertion and deletion and guided rewriting based on an adjustment relation in the remainder of this paper. In this section we address guided insertion and deletion, turning to guided rewriting in Section 4.
More precisely, fix an alphabet Σ 0 and distinguish 0 / ∈ Σ 0 . Put Σ = Σ 0 ∪ {0}. Choose a finite set G ⊆ Σ * , with elements g also referred to as guides. Reflecting the biological mechanism, we assume that each g ∈ G has at least two letters and that the first and last letter of each g ∈ G are not equal to 0. Hence, G ⊆ Σ 0 ·Σ * ·Σ 0 . Now a guided insertion/deletion step ⇒ i/d with respect to G is given by where y ∈ Σ 0 ·Σ * ·Σ 0 , and π(y) and π(g) are obtained from y and g, respectively, by removing their 0s. Thus, π : Σ * → Σ * 0 is the homomorphism such that π(ε) = ε, π(0) = ε and π(a) = a for a ∈ Σ 0 . So, intuitively, g is anchored on the substring y of u and sequences of 0s are adjusted as prescribed by the guide g, in effect replacing the substring y by the guide g while maintaining the prefix x and suffix z. As a simple example of a single guided insertion/deletion step, for G = {g} with g = bcb000ab0c and u = a00bc00babcc00a00b we have u ⇒ i/d v for v = a00bcb000ab0cc00a00b. Here it holds that u = a00 · bc00babc · c00a00b, π(bc00babc) = bcbabc = π(bcb000ab0c) and v = a00 · bcb000ab0c · c00a00b. Note, for the string v, being the result of a rewrite with guide g itself with only one possible anchoring, only trivial steps can be taken further. So, the operation of guided insertion/deletion with the same guide g at the same position in a string is idempotent. However, anchoring may overlap. Consider the set of guides G = { aa0a, a0aa }, for example. Then the string aaa yields an infinite rewrite sequence Still, from aaa only finitely many different rewrites can be obtained by insertion/deletion steps guided by this G, viz. { aaa, aa0a, a0aa }.
The restrictions put on G exclude arbitrary deletions (possible if ε would be allowed as guide) and infinite pumping (if guides need not be delimited by symbols from Σ 0 ). As an illustration of the latter case, starting from the string abc and 'guide' 0ab, the infinite sequence abc ⇒ i/d 0abc ⇒ i/d 0 0abc ⇒ i/d 0 0 0abc . . . would be obtained. The restriction on the substring y prevents to make changes outside the scope of the guide g and forbids a0b0 0 0c ⇒ i/d ab0c by way of the guide ab.
As a first observation we show that the set L u for any finite set of guides G and any string u, is finite. Write u = a 0 0 i 1 a 1 . . . a n 1 0 i n a n where a i ∈ Σ 0 , i k 0, for some n 0. In effect, a guided insertion/deletion step only modifies the substrings 0 i k or leaves them as is. Therefore, after one or more guided insertion/deletion steps the substrings 0 i k are strings taken from the set . . a n 1 z n a n | z k ∈ Z u i/d , 1 k n }, i.e. L u i/d ⊆L u i/d . Since the set G is finite, it follows that Z u i/d is finite, thatL u i/d is finite and that L u i/d is finite as well. More generally, given a set of guides G, we define the extension by insertion/deletion L i/d of a language L over Σ by putting Casted to the biological setting of Section 2, L are the strands of messenger RNA, G are strands of guide RNA. Next, we consider the question whether regularity of the language L is inherited by the induced language L i/d . Note, despite the finiteness of the insertion/deletion scheme for a single string, it is not obvious that such would hold.
For example, consider the language corresponding to the regular expression (ab) * together with the operation sort which maps a string w over the alphabet {a, b} to the string a n b m where n = # a (w), m = # b (w). Thus sort(w) is a sorted version of w with the a's preceding the b's. Note, for w ∈ (ab) * there is only one string sort(w), as sorting is a deterministic, hence finitary operation. However, despite L ( (ab) * ), the language associated by the regular expression, is regular, the language R v } contains shuffles of the string u, i.e. all strings over {a, b} having the same number of a's and b's but are smaller lexicographically. Thus, the set R v } cannot be regular: intersection with the language of a * b * does not yield a regular language. More specifically,L ∩ L ( a * b * ) = { a n b n | n 0 }. We conclude that the question of L i/d being regular, given regularity of the language L, is not straightforward.
With the machinery of rewrite sequences and slice sequences developed in the sequel of the paper, we will be able to prove the following for guided insertion/deletion. Theorem 1. In the setting above, if L is a regular language and for some number k 0 it holds that no string of L or G contains k (or more) consecutive 0's, then the language L i/d is regular too.
We will prove Theorem 1 by applying a more general result on guided rewriting, viz. Theorem 3 formulated in the next section and ultimately proven in Section 6. As in the notion of guided rewriting as developed in the sequel, symbols are only replaced by single symbols by which lengths of strings are always preserved, a transformation is required to be able to apply Theorem 3.
Before doing so we relate our results to those of [15]. There a relation similar to ⇒ i/d was introduced, with the only difference that in a single step either 0's are deleted or inserted, but not simultaneously. One of the conclusions of [15] is that in that setting regularity is not preserved, so the opposite of the main result in the present setting.

Guided rewriting
The idea of guided rewriting is that symbols are replaced by equivalent symbols with respect to some adjustment relation ∼. The one-one correspondence of the symbols of the string u and its guided rewrite v, enjoyed by this notion of reduction, will turn out technically convenient in the sequel.
Let Σ be a finite alphabet and ∼ an equivalence relation on Σ, called the adjustment relation. If a ∼ b we say that a can be adjusted to b. For a string u ∈ Σ * we write #u for its length, use u[i] to denote its i-th element, i = 1, . . . , #u, and let u[p, q] stand for the substring u Next we define a notion of guided rewriting that involves an adjustment relation. Definition 2. We fix a finite subset G ⊆ Σ * , called the set of guides.
(a) For u, v ∈ Σ * , g ∈ G, p 0, we define u ⇒ g,p v, stating that v is the rewrite of u with guide g at position p, by for some g ∈ G and p 0. We use ⇒ * to denote the reflexive transitive closure of ⇒. A sequence u 1 ⇒ u 2 ⇒ · · · ⇒ u n is called a reduction.
(c) For a language L over Σ and a set of guides G we write So, a ⇒-step adjusts a substring to a guide in G element-wise, and L G consists of all strings that can be obtained from a string from L by any number of such adjustments. For example, if Σ = {a, b, c}, G = {bb} and a ∼ b but not a ∼ c, then by a ⇒-step two consecutive symbols not equal to c are replaced by two consecutive b's. In particular, aaacaa → bb,1 abbcaa and abbcaa → bb,0 bbbcaa. We have {aaacaa} G = { aaacaa, bbacaa, abbcaa, aaacbb, bbbcaa, abbcbb, bbacbb, bbbcbb } Next, we state the main result of this paper regarding guided rewriting as given by Definition 2.
Theorem 3. Given an equivalence relation ∼ on Σ, let G be a finite set of guides. Suppose L is a regular language. Then L G is regular too.
Before going to the proof, we first show that both finiteness of G and the requirement of ∼ being an equivalence relation are essential. Below, for a regular expression r we write L (r) for its corresponding language.
To see that finiteness of G is essential for Theorem 3 to hold, let G = { c a k c b k c | k 0 } and L = L (c a * c a * c). Let ∼ satisfy a ∼ b but not a ∼ c. Then all elements of L on which an adjustment is applicable are of the shape ca k ca k c, where the result of the adjustment is ca k cb k c, which can not be changed by any further adjustment. So is not regular. Since regularity is closed under intersection we conclude that L G cannot be regular itself.
Also equivalence properties of ∼ are essential for Theorem 3. For G = { ab } and ∼ = { (a, b), (b, a) } the only possible ⇒-steps are replacing the pattern ba by ab. Note that here ∼ is neither reflexive nor transitive. Since ba may be replaced by ab, bubble sort on a's and b's can be mimicked by ⇒ * , while on the other hand ⇒ * preserves both the number of a's and the number of b's. Hence which proves that L ((ab) * ) G is not regular, again since regularity is closed under intersection.

Rewrite sequences and slice sequences
Fix an alphabet Σ, an adjustment relation ∼, and a set of guides G.
Definition 4. A sequence ρ = (g k , p k ) r k=1 of guide-position pairs is called a guided rewrite sequence for a string u ∈ Σ * if it holds that (i) g k ∈ G, (ii) 0 p k #u − #g k , and (iii) u[p k +1, p k +#g k ] ∼ g k , for all k = 1, . . . , r.
A guide-position pair (g, p) indicates a redex for a guided rewrite with g of the string u. The position p is relative to u. For the rewrite to fit we must have p + #g #u. The first p symbols of u, i.e. the substring u [1, p], are not affected by the rewrite, as are the last #u − p + #g symbols of u, i.e. the substring u[p+#g+1, #u].
The sequence ρ induces a sequence of strings (u k ) r k=0 by putting u 0 = u and u k such that u k−1 ⇒ g k ,p k u k for k = 1, . . . , r. To conclude that u k−1 ⇒ g k ,p k u k is indeed a proper guided rewrite step, in particular that we have u k−1 [p k +1, p k +#g k ], we use the assumption u[p k +1, p k +#g k ] ∼ g k and the fact that if u ⇒ g,p v then u[p + 1, p + #g] ∼ v[p + 1, p + #g]. So we obtain u ⇒ * u r by construction. The string u r is referred to as the yield of ρ for u, notation yield(ρ). Conversely, every specific reduction from u to v gives rise to a corresponding guided rewrite sequence for u.
Definition 5. Let a ∈ Σ. A sequence s = (g i , q i ) i∈I of guide-offset pairs, for I ⊆ N a finite index set, is called a slice for a and G if it holds that (i) g i ∈ G, (ii) 1 q i #g i , and (iii) a ∼ g i [q i ], for all i ∈ I. The slice s is called a slice for a string u ∈ Σ * at position n, 1 n #u, if it is a slice of u[n].
Note that in a guide-offset pair (g, q) of a slice sequence, the offset q is relative to the guide g. Since we require 1 q #g for such a pair, the symbol g[q] is well-defined. We will reserve the use of q for offsets, indices within a guide, and the use of p for positions after which a rewrite may take place, i.e. for lengths of proper prefixes of a given string.
The goal of the notion of slice is to summarize the effect of a number of guided rewrites local to a specific position within a string. The symbol generated by the last rewrite that affected the position, i.e. the particular symbol of the last element of the slice sequence, is part of the overall outcome of the total rewrite. This symbol is called the yield of the slice. More precisely, if I = / 0, the yield of a slice s for a symbol a is defined as yield(s ) = g i max [q i max ] where i max = max(I). In case I = / 0, we put yield(s ) = a. Occasionally we write a ∼ s , as for a slice s for a symbol a it always holds that a ∼ yield(s ).
A slice s is said to be repetition-free if g i = g j ∧ q i = q j implies i = j. If we have I = / 0, the slice s is called the empty slice.
Next we consider sequences of slices, and investigate the relationship between slices on two consecutive positions in a guided rewrite sequence. Definition 6. A sequence σ = (s n ) #u n=1 is called a slice sequence for a string u if the following holds: • s n is a slice for u at position n, for n = 1, . . . , #u; • for n = 1, . . . , #u−1, putting s n = (g i , q i ) i∈I and s n+1 = (g i , q i ) i∈J , there exists a monotone partial injection γ n : I → J such that, for all i ∈ I and j ∈ J, • the slices s 1 and s #u , say s 1 = (g i , q i ) i∈I and s #u = (g j , q j ) j∈J , satisfy q i = 1, for all i ∈ I, and q j = #g j , for all j ∈ J, respectively. For the slices s n and s n+1 the mapping γ n : I → J is called the cut for s n and s n+1 . It witnesses that s n and s n+1 match in the sense that a rewrite may end at position n, may continue for its next offset at position n+1, and may start at position n+1. Since a cut γ is an order-preserving bijection from dom(γ) to rng(γ), and dom(γ) and rng(γ) are finite, it follows that for two slices s , s the cut s → s is unique. We write s s . A slice s = (g i , q i ) i∈I is called a start slice if q i = 1 for all i ∈ I. Similarly, s is called an end slice if q i = #g i for all i ∈ I. A start slice is generally associated with the first position of the string that is rewritten, an end slice with the last position. Note, a start slice as well as an end slice are allowed to be empty. The yield of the slice sequence σ is the sequence of the yield of its slices, i.e. we define yield(σ ) = yield(s 1 ) · · · yield(s #u ).
Example 7. Let ∼ be the adjustment relation with equivalence classes {a, b}, {c, d}, {e, f } and let the set of guides G be given by G = { g 1 , g 2 , g 3 } where g 1 = fb, g 2 = ace and g 3 = d. For the string u = ebcfa we consider the guided rewrite sequence ρ = ( (g 3 , 2), (g 1 , 0), (g 2 , 1), (g 1 , 0), (g 1 , 3), (g 1 , 3) ). The associated reduction looks like Recording what happens at all of the five positions of the string u yields, for this example, the slice sequence σ = (s n ) 5 n=1 given in the table at the left-hand side of Figure 2, where the slice sequence is visualized too.
Theorem 8. Let ρ = (g k , p k ) r k=1 be a guided rewrite sequence for a string u. Then there exists a slice sequence σ = (s n ) #u n=1 for u such that yield(σ ) = yield(ρ).
Proof sketch. Induction on r. If ρ is the empty rewrite sequence, we take for σ the slice sequence of n empty slices. Suppose ρ is non-empty. Let (u k ) r k=0 be the sequence of strings induced by ρ. By induction hypothesis there exists a slice sequence σ for the first r−1 steps of ρ. Suppose u r−1 ⇒ g r ,p r u r . The slice sequence σ is obtained by extending the slices of σ from position p r +1 to p r +#g r with the pairs (g r , n−p r ). Then, yield(σ ) = yield(σ [1, p r ]) · g r [1, #g r ] · yield(σ [p r +#g r +1, #u]) = u r−1 [1, p r ] · g r · u r−1 [p r +#g r +1, #u r−1 ] = u r = yield(ρ) Verification of σ being a slice sequence for u requires transitivity of ∼.
In order to show the reverse of Theorem 8 we proceed in a number of stages. First we need to relate individual guide-offset pairs in neighboring slices. For this purpose we introduce the ordering on so-called chunks.
Definition 9. Let σ = (s n ) #u n=1 be a slice sequence for u. Assume we have s n = ( g n,i , q n,i ) i∈I n , for n = 1, . . . , #u. Let γ n : I n → I n+1 be the cut for s n and s n+1 , 1 n < #u. Let X = { ( g n,i , q n,i , i, n ) | 1 n #u, i ∈ I n } be the set of chunks of σ and define the ordering on X by putting ( g, q, i, n ) ( g , q , i , n ) iff • either n n and there exist indexes 0 , h 0 , . . . , n −n , h n −n such that k , h k ∈ I n+k and k h k , 0 k n − n h k ∈ dom(γ n+k ) and γ n+k (h k ) = k+1 , 0 k < n − n -0 = i and h n −n = i • or n n and there exist indexes 0 , h 0 , . . . , n−n , h n−n such that k , h k ∈ I n +k and k h k , 0 k n − n k ∈ dom(γ n +k ) and γ n +k ( k ) = h k+1 , 0 k < n − n h 0 = i and n−n = i In the above setting with n n, we say that the sequence 0 , h 0 , 1 , h 1 , . . ., n −n , h n −n is leading from i ∈ I n up to i ∈ I n . Likewise for the case where n n.
Given a slice sequence σ , the ordering on the chunks of σ in X gives rise to a partial ordering on the set X /≡ of equivalence classes of chunks. As we will argue, the equivalence classes correspond to guides and their ordering corresponds to the relative order in which the guides occur in a rewrite sequence ρ having the same yield as the slice sequence σ . The next lemma describes the form of the equivalence class holding a chunk x = (g, q, i, n). Using the cuts, equivalent chunks can be found backwards up to position n−q+1 and forward up to position n−q+#g. These chunks together, (g, 1, i n−q+1 , n−q+1), . . ., (g, q, i n , n), . . ., (g, #g, i n−q+#g , n−q + #g) span the guide g that is to be applied, in the rewrite sequence to be constructed.
Lemma 11. Let σ = (s n ) #u n=1 be a slice sequence for a string u. Let X = { ( g n,i , q n,i , i, n ) | 1 n #u, i ∈ I n } be the set of chunks and choose x ∈ X , say x = (g, q, i, n). Put p = n−q. Then there exist j 1 ∈ I p+1 , . . . , j #g ∈ I p+#g such that [x] = { (g, s, j s , p + s) | 1 s #g }.
We are now in a position to prove the reverse of Theorem 8.
Theorem 12. Let σ be a slice sequence for a string u. Then there exists a guided rewrite sequence ρ for u such that yield(ρ) = yield(σ ).
Proof. Suppose σ = ( s n ) #u n=1 , s n = ( g i,n , q i,n ) i∈I n , for n = 1, . . . , #u, and let X = { ( g n,i , q n,i , i, n ) | 1 n #u, i ∈ I n } be the corresponding set of chunks. We proceed by induction on #X . Basis, #X = 0: In this case every slice is empty and yield(σ ) = yield( s 1 ) · · · yield( s #u ) = u[1] · · · · u[#u] = u and the empty guided rewrite sequence for u has also yield u.
Induction step, #X > 0: Clearly, X /≡ is finite and therefore we can choose, by Lemma 10, x ∈ X such that [x] is maximal in X /≡ with respect to . By Lemma 11 we can assume [x] = { (g, s, i s , p + s) | 1 s #g } for suitable p and indexes i s ∈ I p+s , for s = 1, . . . , #g. Note, by maximality of [x], the indexes i s must be the maximum of I p+s . In particular, yield( σ )[p + s] = yield( s p+s ) = g[s], for s = 1, . . . , #g. Now, consider the slice sequence σ = ( s n ) #u n=1 where s n = s n for n = 1, . . . , p and n = p+#g+1, . . . , #u ( g i,n , q i,n ) i∈I n \{i n−p } for n = p+1, . . . , p+#g So, the slice sequence σ is obtained from the slice sequence σ by leaving out the guide-offset pairs related to the particular occurrence of g. Let X be the set of chunks of σ . Then #X < #X . By induction hypothesis we can find a guided rewrite sequence ρ = ( g k , p k ) r k=1 for u such that yield( ρ ) = yield( σ ). Define the guided rewrite sequence ρ = ( g k , p k ) r+1 k=1 by g k = g k , p k = p k for k = 1, . . . , r and g r+1 = g, p r+1 = p. We have 0 p #u−#g and u[p+1, p+#g] ∼ g since s p+1 , . . . , s p+#g are slices for u[p+1], . . . , u[p+#g], respectively. So, ρ is a well-defined guided rewrite sequence for u.
For the slice sequence (s i ) 5 i=1 of Figure 2 we have the following equivalence classes of chunks: 1 . This corresponds to the rewrite sequence ebcfa ⇒ g 3 ,2 ebdfa ⇒ g 1 ,0 fbdfa ⇒ g 2 ,1 facea ⇒ g 1 ,3 facfb ⇒ g 1 ,3 facfb ⇒ g 1 ,0 fbcfb Note that the yield fbcfb of this rewrite sequence is the same as the yield of the sequence (1) of Example 7. However, here the second rewrite with g 1 of (1) has been moved to the end. This does not effect the end result as the particular rewrites do not overlap.

Guided rewriting preserves regularity
Given a language L and a set of guides G, the language L G is given as the set { v ∈ Σ * | ∃u ∈ L : u ⇒ * v }.
One of the main results of this paper, Theorem 3 formulated in Section 4, states that if L is regular than L G is regular too. We will prove the theorem by constructing a non-deterministic finite automaton accepting L G from a deterministic finite automaton accepting L. The proof exploits the correspondence of rewrite sequences and slice sequences, Theorem 8 and Theorem 12. First we need an auxiliary result to assure finiteness of the automaton for L G .
Lemma 13. Let G be a finite set of guides. Let Z = { s | s repetition-free slice for a and G, a ∈ Σ }. Then Z is finite. Moreover, for every string u and every rewrite sequence ρ for u, there exists a slice sequence σ for u consisting of slices from Z only such that yield(σ ) = yield(ρ).
Proof sketch. Finiteness of Z is immediate: there are finitely many guide-offset pairs (g, q), hence finitely many repetition-free finite sequences of them. Thus, there are only finitely many repetition-free slices. Now, let ρ be a rewrite sequence for a string u. By Theorem 8 we can choose a slice sequence σ such that yield(σ ) = yield(ρ). Suppose σ = (s n ) #u n=1 and s n = (g i,n , q i,n ) i∈I n for n = 1, . . . , #u. By Lemma 11 it follows that given a repeated guide-offset pair (g, q), say (g, q) = (g i,n , q i,n ) and (g, q) = (g j,n , q j,n ) for indexes i < j in I n , we can delete the complete equivalence class of (g i , q i , i, n) from slices s n−q+1 to s n−q+#g , while retaining a slice sequence. In fact, we are removing the 'lower' occurrence of the guide g. Moreover, the resulting slice sequence has the same yield as for all slices the topmost guideoffset pair remains untouched. The existence of a repetition-free slice sequence σ such that yield(σ ) = yield(σ ), hence yield(σ ) = yield(ρ), then follows by induction on the number of repetitions.
As a corollary we obtain that every rewrite sequence has a repetition-free equivalent, an intuitive result which requires some technicalities though to obtain directly.
We are now prepared to prove that guided rewriting preserves regularity.
Proof of Theorem 3. Without loss of generality ε / ∈ L. Let M = (Σ, Q, →, q o , F) be a DFA accepting L. We define the NFA M = (Σ, Q , → , q 0 , F ) as follows: Let q F be a fresh state. Put Q = Q ∪ (Q × Z) ∪ {q F } with Z as given by Lemma 13 Note, by Lemma 13, Q is a finite set of states. The automaton M has only one final state, viz. q F . Suppose v ∈ L G . Then there exist u = a 1 · · · a s ∈ L, a rewrite sequence ρ = (g k , p k ) r k=1 and strings u 0 , u 1 , . . . , u r such that u = u 0 , u k−1 ⇒ g k ,p k u k for k = 1, . . . , r, and v = u r . Let, by Theorem 8 and Lemma 13, σ be a slice sequence for u of repetition-free slices with yield(σ ) = yield(ρ). Say σ = (s n ) #u n=1 and s n = (g i,n , q i,n ) i∈I n for n = 1, . . . , #u. Let q 0 − → · · · q s−1 ×s s b s − → q F is an accepting computation of M . Since we have b 1 · · · b s = yield(s 1 ) · · · yield(s s ) = yield(σ ) = v, it follows that v ∈ L (M ). So, L G ⊆ L (M ).
Let v = b 1 · · · b s be a string in L (M ). Given the definition of the transition relation on M , we can find states q 0 , q 1 , . . . , q s−1 , repetition-free slices s 1 , . . . s s such that s n s n+1 for n = 1, . . . , s−1, and a computation q 0 Thus, there exist a final state q s and a computation q 0 a 1 − → q 1 · · · q s−1 a s − → q s ∈ F such that a n ∼ s s for n = 1, . . . , s, i.e. s n is a slice for a n . Put u = a 1 · · · a s . Then u ∈ L, (s n ) #u n=1 is a slice sequence for u and yield(σ ) = v. By Theorem 12 we can find a rewrite sequence ρ for u such that yield(ρ) = yield(σ ) = v. It follows that u ⇒ * v and v ∈ L G . Thus, L (M ) ⊆ L G . We conclude that L G = L (M ) and regularity of L G follows.
Since L ⊆ L G the automaton M should accept any word a 1 . . . a s ∈ L, s > 0. This can be verified as follows. Let ζ i be the empty slice for a i , i = 1 . . . s. Then a i ∼ ζ i , i.e. a i = yield(ζ i ), which holds by definition. Moreover, ζ 1 is a start slice, ζ i ζ i+1 for i = 1 . . . s−1, and ζ s is an end slice. It follows that we can turn an accepting computation of M, say q 0 We now return to a proof of Theorem 1 formulated in Section 3 for which we want to apply Theorem 3. For the latter theorem to apply we need a preparatory transformation. The point is, in the setting of guided insertion/deletion, strings are allowed to grow or shrink while guided insertions and deletions are being applied, whereas in the setting of guided rewriting the strings do not change length.
The key idea of the transformation is that every group of 0's is compressed to a single symbol. Let a language L over Σ and a number k be given by Theorem 1. So, L does not contain strings with k or more 0s. We introduce k fresh symbols 0 0 , 0 1 , . . . , 0 k−1 . Put Θ = { 0 0 , 0 1 , . . . , 0 k−1 }. For any string u over Σ not containing the substring 0 k , i.e. not containing k or more zeros, we define the stringū over the alphabet Σ = (Σ \ {0}) ∪ {0 0 , 0 1 , . . . , 0 k−1 } that is obtained from u by replacing every maximal pattern 0 i by the single symbol 0 i . Note, between two consecutive non-zero letters ab the symbol 0 0 is interspersed. For instance, for k 3, 10023 = 10 2 20 0 3. Also note, that the compression scheme constitutes a 1-1 correspondence of Σ * ∩ { w | w has no substring 0 k } and Θ · Σ 0 * · Θ.
Next, we show that the above operation of compressing groups of 0s preserves regularity using basic closure properties of the class of regular languages, cf. [7, Section 3].
Lemma 14. Let L be a language without strings containing 0 k and let L = {ū | u ∈ L}. Then L is regular if and only if L is regular.
Proof. The language L is the homomorphic image of L for h : With the above lemma in place we can give a proof of the preservation of regularity by guided insertion/deletion.
Proof of Theorem 1. Let k be as given by the statement of the theorem. Obtain L by applying the compression of strings 0 i , for i < k, changing from the alphabet Σ to Σ, as introduced above. By Lemma 14 we then have that L is regular. Let G be obtained from G, again by compression of strings 0 i , for i < k. Then G is a finite set of guides with respect to Σ. Now let the adjustment relation ∼ be the equivalence relation on Σ generated by 0 i ∼ 0 j , 0 i, j < k. By Theorem 3 we obtain that L G is regular. Next we note that if u ⇒ i/d v with respect to Σ, thenū ⇒v with respect to Σ. Vice versa, ifū ⇒v and there exist (unique) u and v such that u, v map toū,v under compression, then u ⇒ i/d v. It follows that L G and L i/d coincide. Finally, by another application of Lemma 14, we conclude that L i/d is regular.

Related work and concluding remarks
In this paper we have discussed abstract concepts of guided rewriting: a more flexible notion focusing on insertions and deletions of a dummy symbol, another more strict notion based on an equivalence relation. Given a language L we considered the extended languages L i/d and L G comprising the closure of L for the two types of guided rewriting with guides from a finite set G. In particular, as our main result we proved that these closures preserve regularity. For doing so we investigated the local effect of guided rewriting on two consecutive string positions, leading to a novel notion of a slice sequence. Finally, the theorem for adjustment-based rewriting was proved by an automaton construction exploiting a slice sequence characterization of guided rewriting. Via a compression scheme for strings of dummy symbols, the theorem for guided insertion/deletion followed. Preservation of regularity by closing a language with respect to a given notion of rewriting arises as a natural question. In Section 3 we observed that by closing the regular language L ( (ab) * ) under rewriting with respect to the single rewrite rule ba → ab the resulting language is not regular. So, by arbitrary string rewriting regularity is not necessarily preserved. A couple of specific rewrite formats have been proposed in the literature. In [6] it was proved that regularity is preserved by deleting string rewriting, where a string rewriting system is called deleting if there exists a partial ordering on its alphabet such that each letter in the right-hand side of a rule is less than some letter in the corresponding left-hand side. In [9] it was proved that regularity is preserved by so-called period expanding or period reducing string rewriting. When translated to the setting of [15], as also touched upon in Section 3, our present notion of guided insertions and deletions allows for simultaneous insertion and deletion of the dummy symbol. A phenomenon also supported by biological findings. Remarkably, the more liberal guided insertion/deletion approach preserves regularity, whereas in the more restricted mechanism of [15], not mixing insertions and deletions per rewrite step, regularity is not preserved. As another striking difference with the mechanism of [15], for that format it was shown that strings u, v of length n exist satisfying u ⇒ * v, but the length of the reduction is at least exponential in n. In our present format this is not the case: we expect that our slice characterization of guided rewriting serves to prove, that if u ⇒ * v then there is always a corresponding reduction of length linear in the length of u and v. Details have not been worked out yet.
The notion of splicing, inspired by DNA recombination, has been proposed by Head in [5]. A socalled splicing rule is a tuple r = (u 1 , v 1 ; u 2 , v 2 ). Given two words w 1 = x 1 u 1 v 1 y 1 and w 2 = x 2 u 2 v 2 y 2 the rule r produces the word w = x 1 u 1 v 2 y 2 . So, the word w 1 is split in between u 1 and v 1 , the word w 2 in between u 2 and v 2 and the resulting subwords x 1 u 1 and v 2 y 2 are recombined into the word w. For splicing a closure result, reminiscent to the one for guided rewriting considered in this paper, has been established. Casted in our terminology, if L is a regular language and S is a finite set of splicing rules, then L S is regular too, cf. [8,11]. Here, L S is the least language containing L and closed under the splicing rules of S.
The computational power of a variant of insertion-deletion systems was studied in [14]. There deletion means that a string uαv is replaced by uv for a predefined finite set of triples u, α, v, while by insertion a string uv is replaced by uαv for another predefined finite set of triples u, α, v. This notion of insertiondeletion is quite different from ours, and seems less related to biological RNA editing. In the same vein are the guided insertion/deletion systems of [2]. There a hierarchy of classes of insertion/deletion systems and related closure properties are studied. Additionally, a non-mixing insertion/deletion system that models part of the RNA-editing for kinetoplastids is given. A rather different application of term rewriting in the setting of RNA is reported in [4], where the rewrite engine of Maude is exploited to predict the occurrence of specific patterns in the spatial formation of RNA, with competitive precision compared to techniques that are more frequently used in bioinformatics.
Possible future work includes investigation of preservation of context-freedom and of lifting the bound on the number of consecutive 0's in Theorem 1. More specifically, for a context-free language L, does it hold, for a finite set of guides G, that L G is context-free too? Considering the set of guides, a generalization to regular sets G is worthwhile studying. Note that the counter-example given in Section 4 involves a non-regular set of guides. So, if L is regular and G is regular, do we have that L G is regular? Similarly for L context-free. We also plan to consider guided rewriting based on other types of adjustment relations. In particular, rather than comparing strings symbol-by-symbol, one can consider two strings compatible if they map to the same string for a chosen string homomorphism. A prime example would be the erasing of the dummy 0 in the context of Section 3 for which we conjecture a variant of Theorem 3 to hold.