Deterministic two-way transducers on finite words have been shown by
Engelfriet and Hoogeboom to have the same expressive power as
$\MSO$-transductions.  We introduce a notion of aperiodicity for these
transducers and we show that aperiodic transducers correspond exactly
to $\FO$-transductions.  This lifts to transducers the classical equivalence
for languages between $\FO$-definability, recognition by aperiodic
monoids and acceptance by counter-free automata.

§ INTRODUCTION

The regularity of a language of finite words is a central notion in
theoretical computer science.  Combining several seminal results, it is
equivalent whether a language is

a) accepted by a (non-)deterministic one-way or two-way automaton
<cit.> and <cit.>,

b) described by a regular expression <cit.>,

c) defined in (Existential) Monadic Second Order ($\MSO$) logic

d) the preimage by a morphism into a finite monoid <cit.>.

Since then, the characterization of fragments of $\MSO$ has been a very
successful story.  Using this equivalence between different formalisms,
several fragments of $\MSO$ have been characterized by algebraic means and
shown to be decidable. Combining results of Schützenberger
<cit.> and of McNaughton and Papert <cit.> yields,
for instance, that a language of finite words is First Order ($\FO$) definable
if and only if all the groups contained in its syntactic monoid are trivial
(aperiodic).  From the results of Schützenberger <cit.>
and others <cit.>, it is also known that a language is First
Order definable with two variables ($\FO^2$) if and only if its syntactic
monoid belongs to the class $\mathbf{DA}$ which is easily decidable.

Automata can be equipped with output to make them compute functions and
relations.  They are then called transducers.  Note then that all variants
are no longer equivalent as they are as acceptors.  Deterministic
transducers compute a subclass of rational functions called sequential
functions <cit.>.  Two-way transducers are also more powerful
than one-way transducers (see Example <ref>).  The study of
transducers has many applications.  Transducers are used to model coding
schemes (compression schemes, convolutional coding schemes, coding schemes
for constrained channels, for instance).  They are also widely used in
computer arithmetic <cit.>, natural language processing
<cit.> and programs analysis <cit.>.

The equivalence between automata and $\MSO$ has been first lifted to
transducers and the functions they realize by Engelfriet and Hoogeboom
<cit.>.  They show that a function from words to words can be realized
by a deterministic two-way transducer if and only it is a $\MSO$-transduction.
First, this result deals surprisingly with two-way transducers rather than
one-way transducers which are much simpler.  Second, the $\MSO$-definability
used for automata is replaced by $\MSO$ graphs transductions defined by
Courcelle <cit.>.  A $\MSO$-transduction is a function where the output
graph is defined as a $\MSO$-interpretation into a fixed number of copies of
the input graph.  In the result of Engelfriet and Hoogeboom, words are seen
as linear graphs whose vertices carry the symbols.

§.§ Contribution

In this paper, we combine the approach of Engelfriet and Hoogeboom with the
one of Schützenberger, McNaughton and Papert.  We introduce a notion of
aperiodicity for two-way transducers and we show that it corresponds to
$\FO$-transductions.  By $\FO$-transduction, we mean $\MSO$-transduction
where the interpretation is done through $\FO$-formulas.  The definition of
aperiodicity is achieved by associating a transition monoid with each
two-way transducer.  The construction of this algebraic object is already
implicit in the literature <cit.>.  In order to obtain
our result, we have considered a different logical signature for
transductions from the one used in <cit.>.  In <cit.>, the
signature contains the symbol predicates to check symbols carried by
vertices and the edge predicate of the graph.  Since words are viewed as
linear graphs, this is the same as the signature with the successor
relation on words.  In our result, the signature contains the symbol
predicates and the order (of the linear graph).  This is equivalent for
$\MSO$-transductions since the order can easily be defined with the
successor by a $\MSO$-formula.  This is however not equivalent any more for
$\FO$-transductions that we consider.  With this signature, the definition
of $\FO$-transduction requires that the order on the output word can be
defined by a $\FO$-formula.  The change in the signature is necessary to
obtain the result.

§.§ Related work

The aperiodic rational functions, that is, functions realized by a one-way
transducer with an aperiodic transition monoid have already been
characterized in <cit.>.  This characterization is not
based on logic but rather on the inverse images of aperiodic languages.

The notion of aperiodic two-way transducer was already defined and studied
in <cit.>, although their model defined length-preserving functions
and the transducers had both their reading and writing heads moving
two-way.  The assumption that the function is length preserving makes the
relation between the input and the output easier to handle.

Recently, Bojanczyk, in <cit.>, also characterized first-order
definable transducers for machines using a finer but more demanding
semantic, the so-called origin semantic.

In <cit.>, Alur and Černý defined the streaming string
transducers, a one-way deterministic model equivalent to deterministic
two-way transducers and $\MSO$ transductions.  More recently, Filiot,
Krishna and Trivedi proposed in <cit.> a definition of transition
monoid for this model. They also proved that aperiodic and $1$-bounded
streaming string transducers have the same expressive power as $\FO$
transductions, which is one of the models considered by our main result.

§.§ Structure

The paper is organized as follows.  Definitions of two-way transducers and
FO-transductions are provided in Section <ref>.  The
construction of the transition monoid associated with a transducer is given
there.  The main result is stated in Section <ref>.
Section <ref> focuses on one aspect of the stability by
composition of functions realized by aperiodic two-way transducers. It is
one of the main ingredients used in the proof of the main result.  The
proof itself is sketched in Sections <ref>
and <ref>.

§ DEFINITIONS

In this section, we present the different models that will be used
throughout the article.

§.§ Two-way transducers

A transducer is an automaton equipped with outputs.  While an input word is
processed along a run by the transducer, each used transition outputs some
word.  All these output words are concatenated to form the output of the run.
The automaton might be one-way or two-way but we mainly consider two-way
transducers in this paper.  When the transducer is non-deterministic, there
might be several runs and therefore several output words for a single input
word.  All two-way transducers considered in this paper are deterministic.
For each input word, there is then at most one valid run and one output
word.  The partial function which maps each input word to the corresponding
output word is said to be realized by the transducer.  The automaton
obtained by forgetting the outputs is called the input automaton of
the transducer.

A two-way transducer is a very restricted variant of a Turing machine with
an input and an output tape.  First, the input tape is read-only. Second,
the output tape is write-only and the head on this tape only moves forwards.
Written symbols on this tape cannot be over-written later by other symbols.

[scale=0.80, initial text=,inner sep=0pt]

[state,initial,accepting where=left, initial where=left] (q) at (0,0) $1$;
[state] (r) at (4,0) $2$;
[state, accepting,accepting where=left] (s) at (2,-2) $3$;
[->] (q) edge [loop above]   node   $a|a,+1$();
() at (-0.04,1.84)  ${\vdash}|\epsilon,+1$;
[->] (q) edge [above] node $b|\epsilon,-1$(r);
() at (1.94,0.70)  ${\dashv}|\epsilon,-1$;
[->] (r) edge [loop above]   node   $a|b,-1$();
[->] (r) edge [below right] node $b|\epsilon,+1$ (s);
() at (3.57,-1.67) ${\vdash}|\epsilon,+1$;
[->] (s) edge [loop below] node $a|\epsilon,+1$ ();
[->] (s) edge [below left] node $b|\epsilon,+1$ (q);

/̆in 0/⊢,1/a,2/a,3/b,4/a,5/b,6/b,7/⊣
(u)̆ at (+̆7,1) $\utext$;
in 0,1,2,3,4,5,6,7
(p) at (+7,0) $1$;
/in 0/2,1/2,2/2,3/3,4/2,5/3,6/3,7/3
(s) at (+7,-1) $\setat$;
/in 1/3,2/3,3/2,4/3,5/2,6/2
(t) at (+7,-2) $\tetat$;
(debut) at (6,0) ;
(fin) at (15,-1) ;
/̣in 	debut/p0, p0/p1,
p3/s2,s0/t1,t1/t2,t2/s3, s3/p4,
p5/s4,t3/t4, t4/s5,
s5/p6,p6/t5, t5/s6, s6/p7,
[->] ()̣ edge node ();
/̣/in 	p1/p2/a, p2/p3/a,
s2/s1/b, s1/s0/b,
[->] ()̣ edge node [above] $\et$();
(u) at (5.5, 1) input $w$:;
() at (5,-1) run;
() at (6.8,-3) output: $f(w)=aabbab$;

A transducer and its run over $w = aababb$

Let $A$ be the alphabet $\{a, b\}$.  Let us consider, as a running
example, the function $f: A^*\to A^*$ which maps each word $w =
a^{k_0}ba^{k_1}\cdots ba^{k_n}$ to the word $f(w) =
a^{k_0}b^{k_0}a^{k_1}b^{k_1} \cdots a^{k_n}b^{k_n}$ obtained by adding
after each block of consecutive $a$ a block of consecutive $b$ of the
same length.  Since each word $w$ over $A$ can be uniquely written $w =
a^{k_0}ba^{k_1} \cdots ba^{k_n}$ with some $k_i$ being possibly equal to
zero, the function $f$ is well defined.  The word $w = aababb =
a^2ba^1ba^0ba^0$ is mapped to $f(w) = a^2b^2a^1b^1a^0b^0a^0b^0 = aabbab$.

This function is realized by the transducer depicted in
Figure <ref>.  This transducer proceeds as follows to
compute $f(w)$ from the input word $w$.  While being in state $1$ and
moving forwards, it copies a block of consecutive $a$ to the output.
While in state $2$ and moving backwards, the corresponding block of $b$
is written to the output.  While being in state $3$, the transducer moves
forwards writing nothing until it reaches the next block of
consecutive $a$.  Note that this function cannot be realized by a
one-way transducer.

Formally, a two-way transducer is defined as follows:

A (deterministic) two-way transducer $\mathcal{A}$ is a tuple
$\mathcal{A}=(Q,A,B,\delta,\gamma, q_0,F))$ defined as follows:

* $Q$ is a finite state set.

* $A$ and $B$ are the input and output alphabet.

* $\delta: Q\times (A\uplus\{{\vdash},{\dashv}\}) \to Q\times\{-1,0,+1\}$ is the transition function. Contrary to the one-way machines, the transition function also outputs an integer, corresponding to the move of the reading head.
The alphabet is enriched with two new symbols $\vdash$ and $\dashv$, which are endmarkers that are added respectively at the beginning and the end of the input word, such that for all $q\in Q$, we have ${\delta(q,\vdash)\in Q\times\{0,+1\}}$ and $\delta(q,\dashv)\in Q\times\{-1,0\}$.

* $\gamma: Q\times (A\uplus\{{\vdash},{\dashv}\}) \to B^*$ is the production function.

* $q_0\in Q$ is the initial state.

* $F \subseteq Q$ is the set of final states.

The transducer $\mathcal{A}$ processes
finite words over $A$. If at state
$p$ the symbol $a$ is processed and $\delta(p,a) = (q,d)$, then
$\mathcal{A}$ moves to state $q$, moves the reading head to the left or
right depending on $d$, and outputs $\gamma(p,a)$.

Let $w = a_1 \cdots a_n$ be a fixed finite word over $A$ and $a_0 =
{\vdash}$ and $a_{n+1} = {\dashv}$.  Whenever $\delta(p,a_m) = (q,d)$ and
$\gamma(p,a_m) = v$, we write $(p,m) \trans{\isp|v} (q,n)$ where $n =
m+d$. We do not write the input over the arrow because it is always the
symbol below the reading head, namely, $a_m$.  In this notation, the pairs
represent the current configuration of a machine with the current state and
the current position of the input head.
A run of the transducer over $w$ is a finite sequence of consecutive transitions

(p_0,m_0) |v_1 (p_1,m_1)  ⋯(p_n-1,m_n-1) |v_n (p_n,m_n)

and we write $(p_0,m_0) \trans{\isp|v} (p_n,m_n)$ where $v = v_1 v_2 \cdots
v_n$.  We also refer to finite runs over words $w$ when all positions $m_i$
in the run but the last are between $1$ and $|w|$.  The last position $m_n$
is allowed to be between $0$ and $|w|+1$.  It is $0$ if the run leaves
$w$ on the left end and it is $|w|+1$ if it leaves $|w|$ on the right end.

A run $(p_0,m_0) \trans{\isp|v} (p_n,m_n)$ over a marked word ${\vdash}
u{\dashv}$ is accepting if it starts at the first position in the
initial state and ends on the right endmarker $\dashv$ in a final state.
Then $v$ is the image of $u$ by $\mathcal{A}$, denoted

§.§ Transition monoid

In order to define a notion of aperiodicity for a transducer, we associate with
each two-way automaton a monoid called its transition monoid.  A
transducer is then called aperiodic if the transition monoid of its
input automaton is aperiodic.  Let us recall that a monoid is called
aperiodic if it contains no trivial group <cit.>.
Equivalently, a monoid $M$ is aperiodic if there exists a smallest integer $n$, called the aperiodicity index, such that
for any element $x$ of $M$, we have $x^n=x^{n+1}$.
Note first
that the transition monoid of a transducer is the transition monoid of its
input automaton and does not depend of its outputs.  Note also that our
definition is sound for either deterministic or non-deterministic
automata/transducers although we only use it for deterministic ones. Lastly, remark that it extends naturally the notion of transition monoid for one-way automata.

The transition monoid is, as usual, obtained by quotienting the free
monoid $A^*$ by a congruence which captures the fact that two words have
the same behavior in the automaton.  In an one-way
automaton $\mathcal{A}$, the behavior of a word $w$ is the set of pairs
$(p,q)$ of states such that there exists a run from $p$ to $q$
in $\mathcal{A}$.  Two words are then considered equivalent if their
respective behaviors contain the same pairs of states.  In a two-way
automaton, the behavior of a word is also characterized by the runs it
contains but since the reading head can move both ways, the behavior is
split into four behaviors called left-to-left, left-to-right, right-to-left
and right-to-right behaviors.  We only define the left-to-left
behavior $\bh_{\ell\ell}(w)$ of a word $w$.  The three other behaviors
$\bh_{\ell r}(w)$, $\bh_{r\ell}(w)$ and $\bh_{rr}(w)$ are defined analogously.

Let $\mathcal{A}$ be a two-way automaton. The left-to-left behavior
$\bh_{\ell\ell}(w)$ of $w$ in $\mathcal{A}$ is the set of pairs $(p,q)$ such that
there exists a run which starts at the first position of $w$ in state $p$
and leaves $w$ on the left end in state $q$ (see Figure <ref>).

Before defining the transition monoid, we illustrate the notion of
behavior on the transducer depicted in Figure <ref>.

Consider the transducer depicted in Figure <ref> and the
word $w = aab$.  From the run depicted in Figure <ref>, it
can be inferred that

_ℓℓ(w)     = { (1,2), (2,2) }         _rℓ(w)     = { (1,2) }

_ℓr(w)     = { (3,1) }          _rr(w)     = { (2,3), (3,1) }.

Let $\mathcal{A}=(Q,A,\delta,q_0,F)$ be a two-way automaton.  The
transition monoid of $\mathcal{A}$ is $A^*/\!\!\sim_{\mathcal{A}}$ where
$\sim_{\mathcal{A}}$ is the conjunction of the four relations $\LL$,
$\LR$, $\RL$ and $\RR$ defined for any words $w$, $w'$ of $A^*$ as follows :

* $w \LL w'$ if $\bh_{\ell\ell}(w) = \bh_{\ell\ell}(w')$.

* $w \LR w'$ if $\bh_{\ell r}(w) = \bh_{\ell r}(w')$.

* $w \RL w'$ if $\bh_{r\ell}(w) = \bh_{r\ell}(w')$.

* $w \RR w'$ if $\bh_{rr}(w) = \bh_{rr}(w')$.

The neutral element of this monoid is the class of the empty
word $\epsilon$, whose behaviors $bh_{xy}(\epsilon)$ is the identity
function if $x\neq y$, and is the empty relation otherwise.

[|-|] (0,1) – (3,1);
(u) at (1.5,1.5) $w$;
(q) at (0,0.6) $p$;
(p) at (-0.4,0) $q$;
plot [smooth] coordinates  (0.1,0.6) (2,0.6) (1,0.4) (1.4,0.2) (0,0);
[->] (0,0) – (p);

A left-to-left behavior $(p,q)$ of a word $w$.

These relations are not new and were already evoked in <cit.>
for example.  Moreover, the left-to-left behavior was already introduced
in <cit.> to prove the equivalence between one-way and two-way

For a deterministic two-way
automaton, the four behaviors
$\bh_{\ell\ell}(w)$, $\bh_{\ell r}(w)$, $\bh_{r\ell}(w)$ and $\bh_{rr}(w)$ are partial functions.
In the non-deterministic case, these four relations are not functions but relations
over the state set $Q$ because there might exist several runs with the same
starting state and different ending states.
Furthermore, for deterministic automaton,
the domains of the functions $\bh_{\ell\ell}(w)$ and $\bh_{\ell r}(w)$ (resp. $\bh_{r\ell}(w)$ and $\bh_{rr}(w)$) are disjoint,
there is a unique run starting in state $p$ at the first (resp. last) position of $w$.
Thus a run starting at the first (resp. last) position leaves $w$ either on the left or the right.
For a deterministic two-way automaton, the four behaviors
$\bh_{\ell\ell}(w)$, $\bh_{\ell r}(w)$, $\bh_{r\ell}(w)$ and $\bh_{rr}(w)$ can be seen
as a single partial function $f_w$ from $Q \times \{\ell,r\}$ to $Q \times \{\ell,r\}$
where $f_w(p,x) = (q,y)$ whenever $(p,q) \in \bh_{xy}(w)$ for any $x,y \in
\{\ell,r\}$.

Let $\mathcal{A}$ be a two-way transducer.  Then the relation
$\sim_{\mathcal{A}}$ is a congruence of finite index.

It is pure routine to check that $\sim_{\mathcal{A}}$ is indeed a
congruence.  It is of finite index since each of the four relations $\LL$,
$\LR$, $\RL$ and $\RR$ has at most $2^{|Q|^2}$ classes.  Note that the
composition of the behaviors is not as straightforward as in the case of
one-way automata, the four relations being intertwined.  For example, the
composition law of the $\bh_{\ell r}$ relation is given by the equality
$\bh_{\ell r}(uv) = \bh_{\ell
r}(u)\big(\bh_{\ell\ell}(v)\bh_{rr}(u)\big)^*\bh_{\ell r}(v)$ which
follows from the decomposition of a run in $uv$.

[b]= b

[draw] (Neutre) $^*1$;
[draw,below left=1.2em of Neutre] (a) $^*a$;
[draw, below right=1.2em of Neutre] (b) $b$;
[draw, below= 2em of a] (ab) $ab$;
[draw, below= 2em of b] (ba) $ba$;
[below =7em of Neutre] (Jclass) ;

The equivalence classes of the transition monoid and its $\mathcal{D}$-class representation

We illustrate the notion of a transition monoid by giving the one of the
transducer depicted in Figure <ref>.  We have omitted all
words containing one of the two endmarkers since these words cannot
contribute to a group.  The eight classes of the
congruence $\sim_{\mathcal{A}}$ for the remaining words are given in
Figure <ref> on the left.  The $\mathcal{D}$-class representation
of this monoid is also given for the aware reader on the right.  It can
be checked that this monoid is aperiodic.  The transducer of
Figure <ref> is then aperiodic.

§.§ $\FO$ graph transductions

The $\MSO$-transductions defined by Courcelle <cit.> are a variant of
the classical logical interpretation of a relational structure into another
one.  Let us recall that a relational structure $S$ has a
$\mathcal{L}$-interpretation, for some logic $\mathcal{L}$, into a
structure $T$ if it has an isomorphic copy in $T$ defined by
$\mathcal{L}$-formulas.  More precisely, this means that there exists a
$\mathcal{L}$-formula $\varphi_S$ with one first-order free variable and a
one-to-one correspondence $f$ between the domain of $S$ and the subset $T'$
of elements of $T$ satisfying $\varphi_S$.  Furthermore, for each
relation $R$ of $S$ with arity $r$, there exists a
$\mathcal{L}$-formula $\varphi_R$ with $r$ first-order free variables such
that $R$ is isomorphic via $f$ to the $r$-tuples of $T'$
satisfying $\varphi_R$.

A $\MSO$-transduction defines for each input structure a new structure
obtained by $\MSO$-interpretation into a fixed number of copies of the
input structure. In this case, the relations are the letter predicates and
the successor relation, which are of arity one and two respectively. To fit
into this framework, words are viewed as linear graphs.  Each word $w =
a_1\cdots a_n$ is viewed as a linear graph with $n$ vertices carrying the
symbols $a_1,\ldots,a_n$.  Linear means here that if the vertex set is
$\{1,2,\ldots,n\}$, the edge set is $\{ (k,k+1) : 1 \le k \le n-1\}$.

When restricted to linear graphs, the $\MSO$-transductions has been proved
to have the same expressive power as two-way transducers <cit.>.  We
are interested in this article in $\FO$ graph transductions, the
restriction to first order formulas.  Since we consider transductions whose
domain is not the set of all graphs, there is an additional closed formula
$\varphi_{dom}$ which determines whether the given graph is in the domain
of the transduction.

/̆in 1/a,3/a,5/b,7/a,9/b,11/b
[minimum width=2em,draw,shape=circle] (u)̆ at (,̆1) $\utext$;

/i̱n 1/3,3/5,5/7,7/9,9/11
[->] (u) edge   node   (u)̱;

(u) at (-0.3,1) $u$ :;
(tu) at (-0.5,-1.25) $T(u)$:;
(c1) at (12.5, -0.5) copy $1$;
(c2) at (12.5, -2) copy $2$;

/̆in 1/a,3/a,7/a
[minimum width=2em,draw,shape=circle] (t1)̆ at (,̆-0.5) $\utext$;
[minimum width=2em,draw,shape=circle] (t2)̆ at (,̆-2) $b$;

[->] (t11) edge node  (t13);
[->] (t13) edge node  (t21);
[->] (t21) edge node  (t23);
[->] (t23) edge node  (t17);
[->] (t17) edge node  (t27);

/̆in 5/b,9/b,11/b
[minimum width=2em,draw,shape=circle,color=gray!70] (t1)̆ at (,̆-0.5)  ;
[minimum width=2em,draw,shape=circle,color=gray!70] (t1)̆ at (,̆-2)  ;

The linear graph of $u=aababb$ and the output structure of $T$ over $u$.

Before giving the formal definition, we give below an example of a
$\FO$-transduction. Note that when considering $\FO$ transductions, the successor relation is replaced by the order relation.

We give here a $\FO$ graph transduction that realizes the function $f$
introduced in Example <ref>.
So let $T=(A,A,\varphi_{dom},C,\varphi
_{pos},\varphi_\leq)$ be the $\FO$ graph transduction defined as follows :

* $A=\{a,b\}$ is both the input and output alphabet,

* $C=\{1,2\}$,

* $\varphi_{dom}$ is a $\FO$ formula stating that the input is a linear graph,

* $\varphi_a^1(x)=\varphi_b^2(x)=\mathbf{a}(x)$, the other position formulas being set as $false$,

* the order formulas are defined now :

* $\varphi_\leq^{i,i}(x,y)=x\leq y$ for $i=1,2$,

* $\varphi_\leq^{1,2}(x,y)=x\leq y \vee (\forall z\ y\leq z\leq x \to \mathbf{a}(z))$,

* $\varphi_\leq^{2,1}(x,y)=\exists z\ x\leq z\leq y \wedge \mathbf{b}(z)$.

A $\FO$-graph transduction is a tuple $T=(A,B,\varphi_{dom},C,\varphi_{pos},\varphi_\leq)$ defined as follows:

* $A$ is the input alphabet.

* $B$ is the output alphabet.

* $\varphi_{dom}$ is the domain formula. A graph is accepted as input if it satisfies the domain formula.

* $C$ is a finite set, denoting the copies of the input that can exist in the output.

* $\varphi_{pos}$ is a set of formulas with one free variable $\varphi_b^c(x)$, for $b\in B$ and $c\in C$. Given $c$, the formulas $\varphi_b^c(x)$, for $b\in B$, are mutually exclusive.
The $c$ copy of a node $i$ is labelled by $b$ if, and only if, the formula $\varphi_b^c(x/i)$ is true.

* $\varphi_\leq$ is a set of formulas with two free variables $\varphi_\leq^{c,c'}(x,y)$, for $c,c'\in C$. There exists a path from the $c$ copy of a node $i$ to the $c'$ copy of a node $j$ if, and only if, the formula $\varphi_\leq^{c,c'}(x/i,y/j)$ is true.

All formulas are required to be in $\FO[<]$ and are evaluated on the input graph.

The output graph is defined as a substructure of the $C$ copies of the input linear graph, in which a node exists if it satisfies one position formula, and is labelled accordingly, and the order is defined according to the order formulas.

In this article, we are only interested in linear graph transductions,
which only accept words seen as linear graphs as input.  An input word has
an image by a $\FO$ graph transduction if the associated linear graph
satisfies its domain formula and the order relation of the output graph,
defined by the order formulas, defines a linear graph corresponding to a
If one condition fails, then the function is undefined on the given input.
One should note that the fact that a graph is linear and corresponds
to a word is $\FO$-definable.

In Figure <ref>, we give the output structure of $T$ over the linear graph $u=aababb$. Note that for the sake of readability, we do not draw the whole order relation, but simply the successor relation.

§ MAIN RESULT

We are now ready to state the main result of this article, as an extension
of the result by McNaughton and Papert <cit.> and Schützenberger
<cit.> in the context of two-way transducers and $\MSO$
transductions established by Engelfriet and Hoogeboom <cit.>.

The functions realized by aperiodic two-way transducers are exactly the
functions realized by $\FO$ graph transductions over words.

The theorem is proved in Sections <ref>
and <ref>.  The first inclusion relies on
Theorem <ref>, while the second inclusion stems from the
conjunction of Theorems <ref>, <ref>
and <ref>.
The next Section is devoted to the composition of transducers, which is a key tool of the proof.

§ COMPOSITION OF TRANSDUCERS

As transducers realize functions over words, the natural question of the
compositionality occurs.  In a generic way, this question is : given two
functions realized by some machine, can we construct a machine that
realizes the composition of these functions.  This question has been
considered in <cit.> for generic machines, and resolved positively in
the case of deterministic two-way transducers in <cit.>.

This result can also be obtained using the equivalence of two-way
transducers with $\MSO$ transductions, since these are easily proved to be
stable by composition (see <cit.>).  However, the reduction from
$\MSO$ transductions to two-way transducers established in <cit.>
makes an extensive use of a weaker version of this result, which is that
the composition of a one-way deterministic, called sequential in the following, transducer with a two-way transducer can be
done by a two-way transducer, which was first proved in <cit.>.

In this section, we follow this approach, and now prove that this result
holds for aperiodic transducers, in the sense that if the two input
transducers are aperiodic, then we can construct an aperiodic transducer
realizing the composition.

Let $\mathcal{A}$ be a sequential transducer that can be
composed with a two-way transducer $\mathcal{B}$, both deterministic and aperiodic. Then we can
effectively construct an aperiodic and deterministic two-way transducer $\mathcal{C}$ such
that $\mathcal{C}=\mathcal{B}\circ\mathcal{A}$.

§ FROM APERIODIC TWO-WAY TRANSDUCERS TO $\FO$ TRANSDUCTIONS

Let us consider a deterministic and aperiodic two-way transducer.
We aim to construct a first-order graph transduction that realizes the same function.

In order to do that, we need to define a formula $\varphi_{dom}$ for the input domain, formulas $\varphi_{pos}$ for each copies of a position and each output letter of $\mathcal{A}$, and, contrary to the generic case of $\MSO$ graph transductions where only the successor is defined, we need here to define order formulas $\varphi_\leq$ that describe the order relation on the output depending on the copies of the nodes from the input.

The following result simply stems from the equivalence of aperiodic monoids and first order logic established in <cit.>, but is an essential step to link aperiodicity to first-order, as it is used in the next theorem, which proves that the order relation between positions is first-order definable.

Let $\mathcal{A}=(Q,A,\delta)$ be an aperiodic two-way automaton.
Then the relation classes of $\LL$, $\LR$, $\RL$, $\RR$  and consequently $\sim_\mathcal{A}$ of $\mathcal{A}$ are $\FO$-definable.

Let $\mathcal{A}$ be an aperiodic two-way automaton.
Then for any pair of states $q$ and $q'$ of $\mathcal{A}$, there exists a $\FO$-formula
$\varphi^{q,q'}(x,y)$ such that for any word $u$ in the domain of $\mathcal{A}$ and any pair of positions $i$ and $j$ of $u$,

$$u\models \varphi^{q,q'}(x/i,y/j)$$

if, and only if, the run of $\mathcal{A}$ over $u$ starting at position $i$ in state $q$ eventually reaches the position $j$ in state $q'$.

We now state the main result of this section and construct the first-order transduction that realizes $\mathcal{A}$.

Let $\mathcal{A}$ be an aperiodic two-way transducer.
Then we can effectively construct a $\FO$-graph transduction that realizes the same function as $\mathcal{A}$.

For simplicity of the proof, we consider a transducer $\mathcal{A}=(Q,A,B,\delta,\gamma,i,F)$  where the production of any transition is at most one letter.
This can be done without loss of generality, since any given transducer can be normalized this way by increasing the number of states.
We now give the formal definition of the $\FO$ transduction ${T=(A,B,\varphi_{dom},Q,\varphi_{pos},\varphi_\leq)}$ that realizes $\mathcal{A}$.

As we consider string transductions within the scope of graph transductions, the domain formula also has to ensure that the input is a linear graph. This can be done in $\FO$ by a formula stating that there is one position that has no predecessor, one position that has no successor, every other position has exactly one successor and one predecessor and every pair of positions is comparable.
Then the domain formula of $T$ is the formula describing the language recognized by the input automaton of $\mathcal{A}$ conjuncted with the linear graph formula. By Lemma <ref>, as $\mathcal{A}$ is aperiodic the domain formula is $\FO$-definable.
The order formulas are given by Lemma <ref>, where obviously $\varphi_\leq^{q,q'}(x,y)=\varphi^{q,q'}(x,y)$.

The $\varphi_b^q(x)$ formulas, where $q\in Q$ and $b\in B$,
express that the production of $\mathcal{A}$ at the position quantified by $x$ in state $q$ is $b$,
but also that the run of $\mathcal{A}$ over $u$ reaches the said position in state $q$.
Should we define $A_{b,q}=\{a\in A\mid \gamma(a,q)=b\}$, then the first condition is expressed as $\bigvee_{a\in A_{b,q}} a(x)$.
The second condition is then equivalent to saying that there exists a run from the initial state of $\mathcal{A}$ to the current position, which is expressed by the formula $\exists y \forall z\ y\leq z\wedge \varphi^{i,q}(y,x)$.
The formula $\varphi_b^q(x)$ is thus defined as the conjunction of these two formulas.

The transduction $T$ is now defined. All formulas are expressed in the first order logic, and it realizes the same function as $\mathcal{A}$, proving the theorem.

§ FROM $\FO$ TRANSDUCTIONS TO APERIODIC TWO-WAY TRANSDUCERS

The proof scheme for this inclusion is adapted from the one in <cit.> proving that $\MSO$ transductions are realized by two-way deterministic transducers.
We prove that we can construct an aperiodic two-way transducer with $\FO$ look around from a $\FO$ transduction, and that the constructions given in <cit.> suppressing the look around part preserve the aperiodicity.

We define in the next subsection the models of transducers with look-around that are used in the proof.
We then give an alternative definition of aperiodicity which can be applied to transducers with logic look-around before explaining the constructions that lead up to the result.

§.§ Transducers with look-around

Here, we define two kinds of transducers with look-around.
The first one is a restriction of two-way transducers with regular look-around, where we limit the regular languages used in the tests to Star-free languages, which is the rational characterization of first-order logic.
These transducers differ from the classic ones by their transitions, where the tests are not determined by the letter read, but also by the prefix and suffix which can be evaluated according to some regular languages.

The second extension we consider is transducers with first-order look around.
In this case, the selection of a transition, as well as the movements of the reading head, are determined by formulas.
Formal definitions are given below.

In both cases only the definition of transition is changed, the definition of run and accepting run remaining the same.

Two-way transducers with Star-Free look around are a subclass of two-way transducers with regular look around defined in <cit.>, where all languages in the tests are Star-Free.

Formally, it is a machine $\mathcal{A}=(Q,A,B,\Delta,i,F)$ where $Q$, $A$, $B$, $i$ and $F$ are the same as for two-way transducers,
and transitions and productions are regrouped in $\Delta$, and are of the form
$(q,t,q',v,m)$ where $q$ and $q'$ are states from $Q$, $v\in B^*$ is the production of the transition, $m\in\{-1,0,+1\}$ describes the movement of the reading head
and $t$ is a test of the form $(L_p,a,L_s)$ where $a$ is a letter of $A\uplus\{\vdash,\dashv\}$, and $L_p$ and $L_s$ are Star-Free languages over the same alphabet.
A test $(L_p,a,L_s)$ is satisfied if the reading head is on a position labelled by the letter $a$,
the prefix of the input word up to the position of the reading head belongs to $L_p$, and symmetrically the suffix belongs to $L_s$.

Such a machine is deterministic if the tests performed in a given state are mutually exclusives.

Two-way transducers with $\FO$ look around are a subclass of two-way transducers with $\MSO$ look around where formulas are restricted to the first-order.

Formally, it is a machine $\mathcal{A}=(Q,A,B,\Delta,i,F)$ where $Q$, $A$, $B$, $i$ and $F$ are the same as two-way transducers,
and transitions of $\Delta$ are of the form
$(q,\varphi(x),q',v,\psi(x,y))$  where $q$ and $q'$ are states from $Q$, $v\in B^*$ is the production of the transition and $\varphi(x)$ and $\psi(x,y)$ are $\FO$ formulas with respectively one and two free variables.
A transition $(q,\varphi(x),q',v,\psi(x,y))$ can be taken if the formula $\varphi(x)$ holds on the input word, where $x$ quantifies the current position $i$ of the reading head, say
$\vdash\! u\!\dashv \ \models \varphi(x/i)$.
Then the reading head moves to a position $j$ such that
$\vdash\! u\!\dashv \ \models \psi(x/i,y/j)$.

Such a machine is deterministic if the unary tests appearing in a given state are mutually exclusive, and if for any input word $u$, any movement formula $\psi(x,y)$ and any position $i$, there exists at most one position $j$ such that
${\vdash\! u\!\dashv \ \models \psi(x/i,y/j)}$.

§.§ Aperiodicity by path contexts

The reading head of transducers with logic look-around can jump several positions at a time and in any direction. Then the notion of behavior for such transducers becomes blurry, since behaviors would have to be considered starting at any position, and moreover the direction taken while exiting a word is not decided locally, but depends on the context.

We thus give an equivalent characterization of the aperiodicity of a transducer through all contexts at a time, for machines whose reading head does not move position by position.

We recall that given a (deterministic) transducer ${\mathcal{A}=(Q,A,B,\Delta,init,F)}$ and $u$ an input word of $\mathcal{A}$, the accepting path of $\mathcal{A}$ over $u$, denoted $path(u)$, is
the sequence  $(q_0,i_0)\ldots (q_n,i_n)$ of pairs from $Q\times[0,|u|+1]$ (the length of $u$ plus the endmarkers) describing the behavior of the reading head of $\mathcal{A}$ while reading $u$, as defined in Subsection <ref>.

We now define the projection of paths, as a way to highlight some information and forget the rest. It is applied to contexts in order to only retain the influence of a word on its context.

Let ${I=[i_1,\ldots,i_k]}$ be an ordered sequence of integers.
We define $path_I(u)$ as the sequence of pairs from $Q\times\{1,\ldots,k\}$ such that for any pairs $(q,j)$ and $(q',j')$,
$(q,j)$ appears before $(q',j')$ in $path_I(u)$ if, and only if,
$(q,i_j)$ appears before $(q',i_{j'})$ in $path(u)$.
Informally, this corresponds to selecting pairs whose position is in $I$
and renaming them according to the set $I$.

Abusing notations, we will note the context path $path_{vw}(vuw)=path_I(vuw)$ where $I$ is the set of positions of $v$ and $w$.

Then $path_{vw}(vuw)$ is the trace of the run over $u$ on the context $v,w$ and two words $u$ and $u'$ are $\mathcal{A}$-equivalent if for any context $v,w$, we have equality of the paths contexts $path_{vw}(vuw)=path_{vw}(vu'w)$.
Then by definition of the aperiodicity,
a transducer or an automaton $\mathcal{A}$ is aperiodic if
there exists a positive integer $n$ such that for any words $u$, $v$ and $w$ on the input alphabet of $\mathcal{A}$, the context paths
$path_{vw}(vu^nw)$ and $path_{vw}(vu^{n+1}w)$ are equals.
One should remark that on two-way transducers, this notion is equivalent to the aperiodicity of the transition monoid.
The next lemma serves as the link from the first-order logic to the aperiodicity by context paths.

Let $T$ be a $\FO$ graph transduction.
There exists a positive integer $n$ such that for any input words $u$, $v$ and $w$ such that $vu^nw$ is in the domain of $T$,
$vu^{n+1}w$ is also in the domain of $T$ and the two words satisfy the same formulas of $T$, when the free variables quantify positions of $v$ or $w$.

First consider the domain formula of $T$. Since it is a $\FO$ formula, it has an aperiodicity index $n$, in the sense that for any  words $u$, $v$ and $w$, $vu^nw$ is in the domain of $T$ if, and only if, $vu^{n+1}w$ is in the domain of $T$.

We now prove the result in the case where $i$ ranges over $v$ and $j$ ranges over $w$, but similar proofs hold for $i$ and $j$ ranging independently over $v$ and $w$.
Consider a pair $c$, $c'$ of copies in $C$, and integers $0\leq i<|v|$ and $0\leq j < |w|$.
Then a word with positions $i$ and $j$ quantified respectively by $x$ and $y$ can be seen
as a word over the alphabet $A\times\{0,1\}^2$, where all letters have $(0,0)$ as second component, except $(v_i,1,0)$ and $(w_j,0,1)$.
The formula $\varphi_\leq^{c,c'}(x,y)$ can then be equivalently seen as a closed formula over this enriched alphabet.
This formula being in $\FO$, it describes an aperiodic language, and then there exists an integer $n'$ such that
satisfies  $\varphi_\leq^{c,c'}(x/i,y/|vu^{n'}|+j)$ if, and only if,
$vu^{n'+1}w$ satisfies $\varphi_\leq^{c,c'}(x/i,y/|vu^{n'+1}|+j)$.

A similar argument also holds for the node formulas $\varphi_b^c(x)$.
As there is a finite number of formulas, there exists an integer, the maximum of the index of each formulas, such that the result holds.

This lemma means that the transducer has an aperiodicity index, in the sense that $u^n$ and $u^{n+1}$ behave the same way for the same context.
It also corresponds to the notion of aperiodicity defined earlier in this section, where the sequence ranges over pairs of copy and position.

§.§ Construction of the aperiodic transducer

We now hold all the necessary tools to prove the reduction from $\FO$ transductions to aperiodic two-way transducers.

We present the first construction, from a $\FO$ transduction to a two-way transducer with $\FO$ look around.
The construction is quite simple.
By putting the copy set of the graph transduction as the set of states of the transducer, we can use the fact that the reading head of a transducer with logic look around jumps between positions to strictly follow the output structure of the input transduction.
We then use Lemma <ref> to prove the aperiodicity of the construction.

Let $T$ be a $\FO$ graph transduction.
Then we can effectively construct an aperiodic two-way transducer with $\FO$ look around that realizes the same function over words.

We have now constructed an aperiodic two-way machine from the input $\FO$ transduction.
But even though two-way transducers with $\MSO$ look around are known to be equivalent to two-way transducers <cit.>, we need to prove that we can suppress the $\FO$ look around while preserving the aperiodicity of the construction.
This is done by the two following theorems, using Star-free look around as an intermediate step.
We show that the construction evoked in <cit.> do preserve the aperiodicity, leading to the result.

Given an aperiodic two-way transducers with $\FO$ look around, we can construct an
aperiodic two-way transducers with Star-Free look around that realizes the same function.

In order to prove this theorem, we rely on the proof of Lemma 6 from <cit.>, which proves that two-way transducers with $\MSO$ look around can be expressed by two-way transducers with regular look around.
This is done by constructing a transducer whose regular tests stem directly from the $\MSO$ formulas.
Then the reading head simulates the jumps of the reading head of the transducer with $\MSO$ look around by moving step by step up to the required position.

We aim to prove on one hand that if the formulas are defined in the first-order, then the resulting two-way transducer only uses Star-free look around,
and on the other hand that if moreover the input transducer is aperiodic, then the output transducer is also aperiodic.

The first claim is proved by noticing that the languages used in the regular look around construction are languages defined by formulas of the input transducer with $\FO$ look around with free variables. Then if the free variables are seen as an enrichment of the alphabet, similarly to what is done in the proof of Lemma <ref>, the formula remains first-order, and consequently all the languages used in look around tests are Star-free.

Now let us compare the moves of the reading head of the resulting transducer with Star-free look around with the ones of the head of the input transducer with $\FO$ look around.
The path of the resulting transducer over any word can entirely be deduced from the path of the input transducer, by adding step by step walks between the jumps of the reading head.
Then, if the input transducer is aperiodic with index $n$, given three words $u$, $v$ and $w$, the context paths $path_{vw}(vu^nw)$ and $path_{vw}(vu^{n+1}w)$ are equal, and thus the context paths for the transducer with Star-free look around, that are deduced from it, are equal too, proving the aperiodicity of the resulting transducer.

We finally prove that we can suppress the Star-free look around tests while preserving the aperiodicity, which concludes the proof of the main theorem.

Given an aperiodic two-way with Star-free look around, we can construct an aperiodic two-way transducers that realizes the same function.

Here again, we consider the construction from <cit.>, Lemma 4, proving that we can suppress the regular look around tests.
Our goal is then to prove that this operation preserves the aperiodicity when the input transducer only uses Star-free languages.

The construction relies heavily on the fact that the composition of a two-way transducer with a one-way transducer can be done by a two-way transducer.
It is used to preprocess the input by adding the result of each regular tests from the transitions at each position.
Given the test $(L_p,a,L_s)$ of a transition, a left-to-right pass simulates $L_p$ and reproduces the input where each position is enriched with the information : does its prefix belong to $L_p$.
Symmetrically, a right-to-left transducer adds the same information for $L_s$.
Let us remark that since these languages are Star-free, the input automaton of the transducers simulating these languages are aperiodic.

Then the information regarding every transition is added to the input, and lastly we can construct a two-way transducer that acts in the same way as the input transducer, but where all the look around have been suppressed and are done locally by looking at the enrichment part of the letter.
This two-way transducer without look around is aperiodic if the input transducer is aperiodic, since they share the same paths, and thus context paths.

Finally, the input transducer is given as the composition of a single aperiodic two-way transducer with a finite number of aperiodic one-way transducers.
Should we first remark that, by symmetry of the problem, Theorem <ref> also holds for right sequential transducers, through several uses of this composition result we finally obtain a unique two-way transducer that realizes the input transducer
with Star-free look around.

§ CONCLUSION

We recall that a similar work has been done for streaming string transducers by Filiot, Krishna and Trivedi <cit.>.
Then through $\FO$ transductions, this result and Theorem <ref> prove the equivalence of aperiodicity for the two models of transducers.

There exists algorithms that input a two-way transducer and construct directly an equivalent streaming string transducer (see <cit.> for example).
It would be interesting to check first if the aperiodicity is preserved through these algorithms, and secondly to compare the size and aperiodicity indexes of the two transition monoids.
Although unknown from the authors, a reciprocal procedure and its study would hold the same interest.

On a more generic note, one can ask which fragments of logic preserve their algebraic characterization in the scope of two-way transducers and $\MSO$ transductions.
For example, are $\mathcal{J}$-trivial transducers equivalent to $\mathcal{B}\Sigma_1$ transductions ?
The main challenges for this question are the stability by composition of these restricted classes of transducers on one hand, and on the other hand
the very definition of logic transductions for restricted fragments, as a fragment must retain some fundamental expressive properties, such as being able to characterize linear graphs.

Finally, we would like to point out the fact that even if we can decide if a given two-way transducer is aperiodic, it is still open to decide if the function realized by a two-way transducer can be realized by an aperiodic one.
An promising approach for this problem might be to consider machine-independent descriptions of functions, as defined recently for streaming string transducers in <cit.> for example. This was successfully done in <cit.> for machines with origin semantic.
We also think that this question could be solved by the notion of canonical object of a function over words, which has yet to be defined.

We would like to thank Antoine Durand-Gasselin, Pierre-Alain Reynier
and Jean-Marc Talbot for very fruitful discussions.

§ APPENDIX

§.§ Composition of transducers

Let $\cA$ be a one-way transducer and $\cB$ be a two-way transducer, both deterministic and aperiodic, that are composable.
Then we can effectively build a deterministic and aperiodic two-way transducer $\cC$ that realizes the function $\cB\circ\cA$.

We first describe precisely the construction evoked in <cit.>.
The point here is to give solid bases to then prove the aperiodicity of this construction.

(u) at (0,5) $u$ :;
[|-|] (1,5)–(7.8,5);
(au) at (6.8,3) $\mathcal{A}(u)$;
(buff) at (0,3) buffer :;

[dotted] (-1,4.5) rectangle (8, 2);
(bchap) at (8.3,4) $\mathcal{C}$;
(aau) at (0,1) $\mathcal{B}\circ\mathcal{A}(u)$;

(u4) at (3.7,5.2) $u_j$;

[dashed] (1,3) – (2.5,3);
[|-|] (2.5,3) – (3.5,3);
[dashed] (3.5,3) – (6,3);

(gqa) at (3.1,3.25) $\mathcal{A}(u_j)$;

[->] plot coordinates  (3.7,5) (3.7,4.75) (3.25,4.75) (3.25,3.5);

(ca) at (3,4.2) $\mathcal{A}$;

[-|,color=gray!70] (3,1) – (6,1);
[|-] (1,1) – (3,1);

(cb) at (2.3,2.5) $\mathcal{B}$;
[->] plot coordinates (2.6,3) (2.6,2.3) (3,2.3) (3,1.2);

Schematic representation of the composition transducer $\mathcal{C}$.
The buffer always contains a production of $\mathcal{A}$ which serves as input for simulating $\mathcal{B}$.

Thus let us describe the transducer $\cC$.
Its set of states will be union of sets describing working modes of the transducer. One mode will be the easy part, when the buffer is full or when we can fill the buffer easily. The second mode will occur if we need to do a backward step on the sequential transducer. It stores the possible candidates then moves back until it can determine the right state. It then switches to the third mode to move back to the required position. The second and third working modes are illustrated in Figures <ref> and <ref> respectively.

Formally, let $\cA=(Q,A,B,\delta,\gamma,q_0,F)$ and $\cB=(P,B,C,\alpha,\beta,p_0,G)$ be a required. We assume that both of the transducers are normalized.
Let now $\cC=(R,A,C,\mu,\eta,r_0,R_F)$ be defined as follows:

* $R=R_1\uplus R_2\uplus R_3$ where

* $R_1=P\times Q\times B^{\leq m+1}\times B^{\leq m+1}$ where $m$ is the maximal length of a production of $\gamma$.

* $R_2=P\times Q\times B\uplus P\times B\times  2^{Q\times Q}$. This set is two-part. The first part will be used at the first step of the backward computation, to get the candidates. The second part consists of the information we need to store, plus a relation over the states of $Q$. This relation describes the set of states that has a run from the current position to one of the candidates. By determinism of $\cA$, one state can not have a path to two different candidates.

* $R_3=P\times B\times Q\times Q$. This set consists of the same information we need to store, plus two concurrent states that will allow us to track the position we need to get back to.

* $r_0=(p_0,q_0,\epsilon,\vdash)$.

* $R_F=\{(p,q,u,v)\mid p\in F,q\in G\}$.

We now describe $\mu$ and $\eta$ in a succession of cases:

* Let $r=(p,q,u,bv)$ be a state in $R_1$, $a$ be a letter of $A$, and let $(p',d)=\alpha(p,b)$. We now treat subcases depending on $d$, $u$ and $v$:

* if $d=0$, then $\mu(r,a)=((p',q,u,bv),0)$,

* if $d=+1$ and $v\neq \epsilon$, then $\mu(r,a)=((p',q,ub,v),0)$,

* if $d=-1$ and $u=u'b'\neq \epsilon$, then $\mu(r,a)=((p',q,u',b'bv),0)$.

In these three subcases, $\eta(r,a)=\beta(p,b)$. It is the only cases where $\eta$ is non-empty. Thus in the following we will omit $\eta$.

* if $d=+1$ and $v=\epsilon$, let $q'=\delta(q,a)$ and $v'=\gamma(q,a)$. Then $\mu(r,a)=((q',p,\epsilon,bv'),+1)$.
When the buffer is empty but $\cB$ is moving forward, then we just compute $\cA$ to fill the buffer.

* if $d=-1$ and $u=\epsilon$, then $\mu(r,a)=((p,q,b),-1)$ with $(p,q,b)\in R_2$.
If we need to go backward, we enter the first part of mode 2 to compute the candidates for the previous state.

* Let $r=(p,q,b)$ be a state in $R_2$ and $a$ a letter of $A$.
Now let $Q'\subseteq Q$ be the set of states $q'$ such that $\delta(q',a)=q$.

* if $Q'=\{q'\}$, then let $v=\gamma(q',a)$ and $\mu(r,a)=((p,q',v,b),0)$,

* otherwise, let $Rel=\{(q',q')\mid q'\in Q\}$ and

We compute here the possible candidates. If there is only one, then we can decide, otherwise we register them all and start moving backward to decide.

* Let $r=(p,b,Rel)\in R_2$, and $a$ be a letter of $A$.
Let now $Nrel=\{(q_1,q_2)\mid (q_1,\delta(q_2,a))\in Rel\}$.

* if there exists only one state $q$ such that $\{q\}\times Q\cap Nrel\neq \emptyset$, then let $q'\neq q$ be such that we can find $q_1$ and $q_2$ with $(q,q_1)$ and $(q',q_2)$ in $Rel$. Then $\mu(r,a)=((p,b,q_1,q_2),+1)$ with $(p,b,q_1,q_2)\in R_3$.

We compute the new set of states that have a path to our candidates with one more step backward. If there now exists only one candidate left, we follow this path in mode 3, together with an other option, to be able to get back to the required position.

* otherwise, if $a=\vdash$, we proceed as in the previous case with $q$ such that $(q,q_0)\in Nrel$.

If we reached the beginning of the input, then the right candidate is the one with a path starting at $q_0$.

* otherwise, then $\mu(r,a)=((p,b,Nrel),-1)$.

If we can not decide now, we continue our way back.

* Let $r=(p,b,q_1,q_2)$ be a state in $R_3$ and $a$ be a letter of $A$.

* if $\delta(q_1,a)\neq \delta(q_2,a)$, then $\mu(r,a)=(p,b,\delta(q_1,a),\delta(q_2,a))$.

If the two paths do not collide next step, then we are not at the required position yet, so we continue computing the paths.

* if $\delta(q_1,a)=\delta(q_2,a)$ then let $v=\gamma(q_1,a)$ and

If they do collide, then $q_1$ is the right candidate and we can fill the buffer.

* If $r=(g,q,u,v)$ with $g\in G$ then let $(q',d)=\delta(q,a)$ and

If we finished the computation on $\cB$, we simply finish the one on $\cA$ to ensure that the function realized by $\cA$ is defined on the input.

[initial text=]

(ui) at (0,3) $u_j$;
(ui1) at (1,3) $u_{j+1}$;
() at (2,3) $\cdots$;
(uj2) at (3,3) $u_{i-2}$;
(uj1) at (4,3) $u_{i-1}$;
(uj) at (5,3) $u_i$;

(q) at (5,1) $q$;
(q1) at (4,2) $q_1$;
(q2) at (4,0) $q_r$;
[rotate=90] () at (4,1) $\cdots$;

[->] (q1) – (q);
[->] (q2) – (q);

(q11) at (3,2) $\bullet$;
(q12) at (3,1.5) $\bullet$;
[rotate=90] () at (3,0.75) $\cdots$;

(q21) at (3,-1) $\bullet$;
(q22) at (3,-0.5) $\bullet$;
(q23) at (3,0) $\bullet$;

(3,1.75)ellipse(0.2 and 0.5);
[->] (q11)+(0,0.25) – (q1);
[->] (q12)+(0,-0.25) – (q1);

(q22)ellipse(0.2 and 0.7);
[->] (q23)+(0,0.2) – (q2);
[->] (q21)+(0.1,-0.1) – (q2);

(p11) at (1,2) $\bullet$;
(p12) at (1,1.5) $\bullet$;
(p13) at (1,1) $\bullet$;
(p14) at (1,0.5) $\bullet$;

[rotate=90] () at (1,-0.2) $\cdots$;

(p21) at (1,-0.75) $\bullet$;

(p13)+(0,0.25)ellipse(0.2 and 1);

[->,dashed] (p11)+(0,0.25) – (3,2.25);
[->,dashed] (p14)+(0,-0.25) – (3,1.25);

[->,dashed] (p21)+(0.2,0)– (2.8,-0.5);

(no2) at (0,0) $\times$;
[dotted,->] (no2)+(-0.07,0) – (0.8,0);
(no) at (0,1.25) $\times$;
[dotted,->] (no)+(-0.033,0) – (0.8,1.25);
[rotate=90,scale=0.6] () at (0, 0.7) $\cdots$;
[rotate=90,scale=0.6] () at (0,-0.3) $\cdots$;

[minimum size=0cm,inner sep=0pt,outer sep=0pt]  (yay) at (0,-0.75) $\bullet$;
[->] (yay) – (0.8,-0.75);

The transducer $\mathcal{C}$ reads from position $i-1$ to $j$
calculating the runs of $\mathcal{A}$ leading to each potential state.
Here, the correct candidate is state $q_r$, decided at position $j$.

[initial text=]

(ui) at (0,3) $a_i$;
(ui1) at (1,3) $a_{i+1}$;
(dot) at (2,3) $\cdots$;
(uj1) at (3,3) $a_{j-1}$;
(uj) at (4,3) $a_j$;

(q2) at (1,2) $q_2^k$;
(dot2) at (2,2) $\cdots$;
(qp) at (3,2) $q_2$;

(trouve) at (0,1) ;
(q1) at (1,1) $q_1^k$;
(dot1) at (2,1) $\cdots$;
(q) at (3,1) $q_1$;
(origine) at (4,1) $q$;

[->] (q2) – (dot2);
[->] (dot2) – (qp);
[->] (qp) – (origine);
[->] (trouve) – (q1);
[->] (q1) – (dot1);
[->] (dot1) – (q);
[->] (q) – (origine);

Backwards moves using states from $R_3$.

Let us now prove that aperiodicity is preserved by this construction, meaning that if both $\cA$ and $\cB$ are aperiodic transducers, then the resulting $\cC$ will also be aperiodic. We denote by $n_\cA$ and $n_\cB$ the aperiodicity indexes of $\cA$ and $\cB$ respectively, and let us prove that $\cC$ is aperiodic with index $n=2n_\cA+n_\cB+1$.

We first make a few remarks:

* The transducer $\cC$ can only go forward in states of $R_1$ or $R_3$. Furthermore, if a run is in a state of $R_1$, then any further position visited by the run will be first reached in a state of $R_1$. This is due to the construction of $\cC$, the states of $R_3$ are used to go back to the last position before a position reached by a state of $R_1$.

* The transducer $\cC$ can only move backward in states of $R_1$ or $R_2$. But since moving backward in a state of $R_1$ leads to a state of $R_2$, the transducer only reaches previous positions in states of $R_2$.

We now treat the case of each congruence separately.

* $u^n\LR u^{n+1}$.
Let assume there exists a run starting at a state $r$ on the left of $u^n$ and going out of it on the right in state $r'$. Note that the following proof also holds if we consider a run over $u^{n+1}$.

* If $r$ is in $R_1$, then, by <ref>, $r'$ is also in $R_1$. Moreover, each iteration of $u$ is first reached by a state in $R_1$.
Thus there exists an underlying run of $\cA$ on $u^n$, and after $n_\cA$ iterations of $u$, the run first enters $u$ in a state with the same $Q$ component $q$.
Then, we look at the production $v$ of $\cA$ over a left-to-right run over $u$ starting and ending in state $q$.
If $v=\epsilon$, then the run is just advancing waiting to fill its buffer. Thus the same run exists for $u^{n+1}$.
Otherwise, then after $n_\cB$ more iterations, $\cB$ would have worked on $v^{n_\cB}$ and hence the $P$ component of the state is stabilized on $p$. Plus, if $v=v'b$, then the buffers will be $(v',b)$. Then at each iteration the run first exits $u$ in the same state $(p,q,v',b)$. Thus in this case we have an aperiodicity index of $n_\cA+n_\cB$.

* If $r$ and $r'$ are in $R_3$, then the run never leaves states of $R_3$. Thus it is just two parallel runs of $\cA$. Then after $n_\cA$ iterations we get aperiodicity of the run.

* If $r$ is in $R_3$ but $r'$ is in $R_1$, then after at most $n_\cA$ iterations we reached a state of $R_1$, otherwise we are in the previous case. We then are, after at most $n_\cA$ iterations of $u$, in the case where both $r$ and $r'$ are in $R_1$.
Thus by combining the two previous points, we get aperiodicity with an index of $2n_\cA+n_\cB$.

* $u^n\RL u^{n+1}$
Let assume there exists a run starting at a state $r$ on the right of $u^n$ and going out of it on the left in state $r'$. Note that the following proof also holds if we consider a run over $u^{n+1}$.
Then we can assume $r'$ to be in $R_2$. So the run is mainly a backward computation of $\cA$, with potentially computations over the buffer in between.

* If the run remains in states of $R_2$ long enough, say during $n_\cA$ iterations of $u$, then the states visited are of the form $(p,b,Rel)$, with $p$ and $b$ constants, while $Rel$ computes the sets of states having a run back to a given position. Thus in this case we know that for $n\leq n_\cA$ the $Rel$ component will always be the same of a given position of iterations of $u$. Then the same runs will exist for $u^n$ and $u^{n+1}$.

* Otherwise, it means that the run is able to compute the true candidate, and go back to the required position. Plus we know that after $n_\cA$ iterations (where we start counting from the left), the production will always be the same word $v$.
If $v$ is empty then the computation on $\cB$ does not move, thus the $P$ component stays the same, and we get aperiodicity of the run.
If $v$ is not empty, then after $n_\cB$ iterations (where we start counting from the right), $\cB$ worked on a word $v^{n_\cB}$. Then we know that far enough from the borders of the word, the run will first enters each iterations of $u$ in a same state, giving aperiodicity with index $n_\cA+n_\cB$.

* Finally, we can prove that if the relations $\LR$ and $\RL$ are aperiodic with an aperiodicity index of $n$, then the relations $\LL$ and $\RR$ are aperiodic with an aperiodicity index of $n+2$.
First remark that left-to-left and right-to-right runs over $u^{n+2}$ exists over $u^{n+3}$.
Now consider a run over an input word $u^{n+3}$.
We decompose it as follows.
We isolate one iteration of $u$ on each side of the input, and decompose the run in a succession of small runs over these isolated iterations, and runs over $u^{n+1}$.
Then we know that the transversal runs also exists over $u^n$, and the left-to-left and right-to-right runs will appear over $u^n$ plus the isolated iteration as described in Figure <ref>.

Then the transducer $\mathcal{C}$ is aperiodic, with an aperiodicity index bounded by $n_\mathcal{A}+n_\mathcal{B}+2$.

[initial text=,scale=0.65]

[minimum size=0cm] (debut) at (0,1) ;
[minimum size=0cm,inner sep=0pt,outer sep=0pt] (it1) at (2,1) $|$;
[minimum size=0cm,inner sep=0pt,outer sep=0pt] (it2) at (5,1) $|$;
[minimum size=0cm,inner sep=0pt,outer sep=0pt] (itna) at (7,1) $|$;

[minimum size=0cm,inner sep=0pt,outer sep=0pt] (itfin) at (10,1) $|$;
[minimum size=0cm,inner sep=0pt,outer sep=0pt] (itfin2) at (12,1) $|$;
[minimum size=0cm,inner sep=0pt,outer sep=0pt] (itfin3) at (14,1) ;

(u1) at (1,1.3) $u$;
(u2) at (6,1.3) $u$;
(dot) at (3.5,1.3) $\cdots$;
(una) at (11,1.3) $u$;
(dot) at (8.5,1.3) $\cdots$;
() at (13,1.3) $u$;

[|-|,thick] (debut)–(itfin3);

() at (-0.5,-2.9) $\mathcal{B}$;

[->] (0,0) – (14,0);
() at (-0.5,0) $\mathcal{A}$;

[minimum size=0cm] () at (0,0) ;
[minimum size=0cm,inner sep=0pt,outer sep=0pt] () at (2,0) $|$;
[minimum size=0cm,inner sep=0pt,outer sep=0pt] () at (5,0) $|$;
[minimum size=0cm,inner sep=0pt,outer sep=0pt] () at (7,0) $|$;
[minimum size=0cm,inner sep=0pt,outer sep=0pt] () at (10,0) $|$;
[minimum size=0cm,inner sep=0pt,outer sep=0pt] () at (12,0) $|$;

() at (0.2,-0.4) $q_1$;
() at (2,-0.4) $q_2$;
() at (5,-0.4) $q$;
() at (7,-0.4) $q$;
() at (10,-0.4) $q$;
() at (12,-0.4) $q$;
() at (14,-0.4) $q$;

(0.1,1.6) .. controls (0.2,2.5) and (2.3,1.9) .. (2.5,2.4);
(5,1.6) .. controls (4.8,2.5) and (2.7,1.9) .. (2.5,2.4);
(per) at (2.5,2.4) [above] $n_\mathcal{A}$ iterations ;

(5,-0.6) – (5,-0.8) – (7,-0.8) – (7,-0.6);
[->] (6,-0.8) – (6,-1.2);
() at (6,-1.5) $v$;

(10,-0.6) – (10,-0.8) – (12,-0.8) – (12,-0.6);
[->] (11,-0.8) – (11,-1.2);
() at (11,-1.5) $v$;

() at (8.5,-1.5) $\ldots$;

(12,-0.6) – (12,-0.8) – (14,-0.8) – (14,-0.6);
[->] (13,-0.8) – (13,-1.2);
() at (13,-1.5) $v$;

[thick,|-|] (14,-1.9) – (0,-1.9);
() at (12,-1.9) $|$;
() at (10,-1.9) $|$;
() at (7,-1.9) $|$;
() at (5,-1.9) $|$;

() at (-1.3,-1.9) $\mathcal{A}(q_1,u^{n+1})$;
() at (-0.4,1) $u^{n+1}$;

[minimum size=0cm,inner sep=0pt,outer sep=0pt] (p1) at (5,-2.7) $|$;
() [below=0 of p1] $p_1$;

[minimum size=0cm,inner sep=0pt,outer sep=0pt] (p2) at (7,-2.72) $|$;
() [below=0 of p2] $p_2$;

[minimum size=0cm,inner sep=0pt,outer sep=0pt] (pnm1) at (10,-2.75) $|$;

[minimum size=0cm,inner sep=0pt,outer sep=0pt] (pn) at (12,-3.15) $|$;
() [above=0 of pn] $p$;

[minimum size=0cm,inner sep=0pt,outer sep=0pt] (pn2) at (14,-3.55) $|$;
() [above=0 of pn2] $p$;

plot [smooth] coordinates (0,-2.4) (4,-2.5) (1.3,-2.6) (p1) (p2) (pnm1) (11.2,-2.8) (9,-3) (pn) (13,-3.2) (11,-3.4) (pn2) ;
[->] (14,-3.55) – ++(0.2,0.01);

(5,-3.6) .. controls (5.2,-4.5) and (8.3,-3.5) .. (8.5,-4.2);
(12,-3.6) .. controls (11.8,-4.5) and (8.7,-3.5) .. (8.5,-4.2);
() at (8.5,-4.4) [below] $n_\mathcal{B}$ iterations;

A left-to-right behavior of $u^n$ in $\mathcal{C}$ simulates a left-to-right behavior of $u^n$ in $\mathcal{A}$
and a left-to-right behavior of $\mathcal{A}(q_1,u^n)$ in $\mathcal{B}$.

[|-|] (0,6) – (1,6);
[-|] (1,6) – (4,6);
[-|] (4,6) – (5,6);
(u1) at (0.5,6.3) $u$;
(u2) at (2.5,6.3) $u^{n+1}$;
(u3) at (4.5,6.3) $u$;

[dashed] (1,6) – (1,3.7);
[dashed] (4,6) – (4,3.7);

(-0.3,5.5) .. controls (5.5,5.4) and (5.5,5.3) .. (2.5,5);
(2.5,5) .. controls (1,4.8) and (1,4.7) .. (2.5, 4.5);
[->] (2.5,4.5) .. controls (5.8, 4.3) and (5.9,4.2) .. (-0.3,4);

(to) at (6.2,5) $\Longrightarrow$;

[|-|] (8,6) – (9,6);
[-|] (9,6) – (12,6);
[-|] (12,6) – (13,6);
(v1) at (8.5,6.3) $u$;
(v2) at (10.5,6.3) $u^n$;
(v3) at (12.5,6.3) $u$;

(7.7,5.5) .. controls (13.5,5.4) and (13.5,5.3) .. (10.5,5);
(10.5,5) .. controls (8,4.8) and (8,4.7) .. (10.5, 4.5);
[->] (10.5,4.5) .. controls (13.8, 4.3) and (13.9,4.2) .. (7.7,4);

[dashed] (9,6) – (9,3.7);
[dashed] (12,6) – (12,3.7);

Transversal runs are shortened by aperiodicity of $\LR$ and $\RL$ and the right-to-right run overflows to the left isolated iteration.

§.§ From aperiodic two-way transducers to $\FO$ transductions

Let $\mathcal{A}$ be an aperiodic two-way automaton.
Then for any pair of states $q$ and $q'$ of $\mathcal{A}$, there exists a $\FO$-formula
$\varphi^{q,q'}(x,y)$ such that for any word $u$ in the domain of $\mathcal{A}$ and any pair of positions $i$ and $j$ of $u$,

$$u\models \varphi^{q,q'}(x/i,y/j)$$

if, and only if, the run of $\mathcal{A}$ over $u$ starting at position $i$ in state $q$ eventually reaches the position $j$ in state $q'$.

Without loss of generality we assume that $i$ is smaller or equal to $j$.
The theorem is proved by decomposing runs of $\mathcal{A}$ over $u$ in partial runs over $u[1,i-1]$, $u[i,j]$ and $u[j+1,|u|]$ as shown in Figure <ref>.
Now let us remark that if the reading head is at position $i$ in a state $p$, then the next state in which the run reaches the positions $i$ or $j$ can be decided thanks to $\bh_{\ell r}(u[i,j])$, $\bh_{\ell\ell}(u[i,j])$ and
Similarly if the reading head is at position $j$, then the next state depends on $\bh_{r\ell}(u[i,j])$, $\bh_{rr}(u[i,j])$ and
Then the equivalence classes of these three factors of $u$ decide in which states the position $j$ is visited by the run starting at position $i$ in a given state.
Thus the specification of $\varphi^{q,q'}(x,y)$, which is the fact that the run of $\mathcal{A}$ over $u$ starting at position $i$ in state $q$ eventually visits the position $j$ in state $q'$, entirely relies on the relation classes of these three factors of $u$.

Thanks to Lemma <ref>, we know that given $\mathcal{A}$ aperiodic, there exists $\FO$ formulas describing each behavior classes.
Then by guarding the quantifications of these formulas, we simultaneously select the behavior classes of the three factors.
For example, if $\varphi$ is the $\FO$ formula describing a given class of $\sim_\mathcal{A}$, then we can ensure that $u[i,j]$ is of said class by  replacing each quantification $\exists z$ (resp. $\forall z$) in $\varphi$ by
$\exists z~x\leq z\wedge z\leq y$ (resp. $\forall z~x\leq z\wedge z\leq y$), after having renamed any occurrence of $x$ and $y$.

Finally, as there exists only a finite number of classes for each behavior relation, there exists only a finite number of combinations of classes for the three factors that satisfy the specification of $\varphi^{q,q'}(x,y)$, which can thus be written as the disjunction over every compatible triplet of classes for $u[1,i-1]$, $u[i,j]$ and $u[j+1,|u|]$.

[|-|] (0,6) – (1,6);
[-|] (1,6) – (4,6);
[-|] (4,6) – (5,6);

(u) at (-0.7,6) $u$;
(i) at (1,6.3) $i$;
(j) at (4,6.3) $j$;

[dashed] (1,6) – (1,3.7);
[dashed] (4,6) – (4,3.7);

(q) at (0.8,5.5) $q$;
(qp) at (4.2, 4.1) $q'$;

(1,5.5) .. controls (3,5.5) and (3,5.1) .. (1,5.1);
(1,5.1) .. controls (0,5.1) and (0,4.7) .. (1,4.7);
(1,4.7) – (4,4.7);
(4,4.7) .. controls (5,4.7) and (5,4.4) .. (4,4.4);
(4,4.4) – (1,4.4);
(1,4.4) .. controls (2,4.27) and (2,4.23) .. (1,4.1);
(1,4.1) – (4,4.1);

A run between two positions is decomposed as a succession of partial runs, characterized by the behaviors of each factor.

§.§ From $\FO$ transductions to aperiodic two-way transducers

§.§.§ Construction of the aperiodic transducer

Let $T$ be a $\FO$ graph transduction.
Then we can effectively construct an aperiodic two-way transducer with $\FO$ look ahead that realizes the same function over words.

Let $T=(A,B,\varphi_{dom},C,\varphi_{pos},\varphi_\leq)$ be a $\FO$ transduction.
Then we construct a two-way transducer with $\FO$ look around $\mathcal{A}=(Q,A,B,\Delta,i,\{f\})$ that realizes the function $T$, and prove that
it is aperiodic.
The main idea is that from the order formulas of $T$, we can deduce the successor relation. By setting the set of states as set of copies, the successor relation corresponds to the transitions of the transducer. We add an initial and a final states that are reached on the endmarkers.
Thus, we set $Q=C\uplus \{i,f\}$.

For readability purposes, we first set $\varphi_*^{c}(x)=\bigvee_{b\in B}\varphi_b^c(x)$, a formula which is satisfied if the $c$ copy of the node quantified by $x$ exists in the output structure.
To define the transition relation, we now set, for each pair of copies, the successor formula

S^{c,c'}(x,y)= \varphi_\leq^{c,c'}(x,y)\wedge
\forall z \bigwedge_{d\in C}
\big(\varphi_*^d(z)\to\bigl(\varphi_\leq^{d,c}							(z,x)\vee\varphi_\leq^{c',d}(y,z)\bigr)\big)

Given a position $x$ and a copy $c$, there exists at most one $c'$ and one position $y$ such that $S^{c,c'}(x,y)$ is true, but only if the input word is in the domain of the transduction, and if we range over the existing nodes of the output structure.
Thus we define $\psi^{c,c'}(x,y)=S^{c,c'}(x,y)\wedge \varphi_*^c(x)\wedge\varphi_*^{c'}(y)\wedge \varphi_{dom}$.
The fact that the transducer can only move to existing nodes will ensure the determinism of the machine.
The transition relation $\Delta$ is then defined as set of tuples
$(c,\varphi_b^c(x),c',b,\psi^{c,c'}(x,y))$. Note that the transitions are mutually exclusive by definition of graph transduction, and that they also handle the production.
We also add to $\Delta$ some transitions regarding the endmarkers and the initial and the final states :

* For the initial case, we define the formula

$$first^c(y)=\varphi_*^c(y)\wedge\forall x \bigwedge_{d\in C}\big(\varphi_*^d(x)\to \varphi_\leq^{c,d}(y,x) \big)$$

that is satisfied by the first node of the output structure, and add the transition $(i,\vdash\!\!(x),c,\epsilon,first^c(y)\wedge\varphi_{dom})$ that moves the reading head from the initial position to the first node.
One should note that we consider without generating problems that the formula $first^c(y)$ is a formula $\psi(x,y)$ with two free variables, where $x$ is not used.

* For the final case, we similarly define the formula

$$last^c(y)=\varphi_*^c(y)\wedge\forall x \bigwedge_{d\in C}\big(\varphi_*^d(x)\to \varphi_\leq^{d,c}(x,y)\big) $$

and add the transitions $(c,\varphi_b^c(x)\wedge last^c(x)\wedge\varphi_{dom},f,b,\dashv\!(y))$.
Note that these transitions handle the production of the label of the last node of $T$.

Since the production of $T$ over its domain is a linear graph, there is exactly one node that satisfies a formula $first^c(x)$ and exactly one that satisfies a formula $last^c(x)$ when the input satisfies $\varphi_{dom}$.
And since any other node has exactly one successor, the resulting transducer $\mathcal{A}$ is deterministic.

Now remark that the reading head of the transducer $\mathcal{A}$ follows exactly the output structure of $T$.
Then according to Lemma <ref>, there exists an aperiodicity index $n$ such that for any words $u,v$ and $w$, if $vu^nw$ is in the domain of $T$, then $vu^{n+1}w$ is also in the domain of $T$ and the formulas of $T$ have the same truth value when the free variables range over $v$ and $w$.
Plus, as we consider first-order formulas, we know that we can choose an integer $n$ such that the words $u^n$ and $u^{n+1}$ satisfy the same formulas of $T$.
Then the moves of the input head on the words $vu^nw$ and $vu^{n+1}w$
either both are in iterations of $u$, or both are outside.
Then the context paths $path_{vw}(vu^nw)$ and $path_{vw}(vu^{n+1}w)$, which are the traces of the runs outside of $u^n$ and $u^{n+1}$ respectively, will be equal,
proving the aperiodicity of $\mathcal{A}$.