Pro-aperiodic monoids via saturated models

We apply Stone duality and model theory to study the structure theory of free pro-aperiodic monoids. Stone duality implies that elements of the free pro-aperiodic monoid may be viewed as elementary equivalence classes of pseudofinite words. Model theory provides us with saturated words in each such class, i.e., words in which all possible factorizations are realized. We give several applications of this new approach, including a solution to the word problem for $\omega$-terms that avoids using McCammond's normal forms, as well as new proofs and extensions of other structural results concerning free pro-aperiodic monoids.


Introduction
The pseudovariety of aperiodic monoids has long played a fundamental role in finite semigroup theory and automata theory. The famous Schützenberger theorem [42] proved that the aperiodic monoids recognize precisely the star-free languages; this class was later shown by McNaughton and Papert [36] to be the class of first-order definable languages; also see Straubing's book [46]. From the point of view of semigroup decomposition theory, aperiodic monoids are important because they form one of the basic building blocks, along with the pseudovariety of finite groups, of all finite semigroups according to the Krohn-Rhodes decomposition theorem [30]. Thus aperiodic monoids play a prime role in two of the oldest open problems in automata theory: the dot-depth problem [13,38,37] and the Krohn-Rhodes complexity problem [31].
The importance of profinite monoids in automata theory and finite semigroup theory was first highlighted, starting in the late eighties, by Almeida; see his influential book [1], and the more recent monograph [40], for more background. Much of the early work focused on viewing elements of relatively free profinite monoids as limits of finite words, but early on Almeida made the important observation that the Boolean algebra of clopen subsets of the free pro-V monoid can be identified with the Boolean algebra of V-recognizable languages [1]; in other words, the free pro-V monoid is the Stone dual of the Boolean algebra of V-recognizable languages. In recent years, a number of authors have made explicit use of duality theory to redevelop and expand the foundations of the profinite approach to studying varieties of languages in the sense of Eilenberg [18]; most closely related to our work here is the work of Gehrke, Pin et al. [20,21,22,19], Bojańczyk [12], and Rhodes and the second-named author [40,Chapter 8].
Until the present work, there has been little attempt to use Stone duality to study the structure of relatively free profinite monoids. For example, very basic structural properties of the free pro-aperiodic monoid were deduced in [39] using the closure of the pseudovariety of aperiodic monoids under certain expansions. Deeper structural properties involving Green's relations on stabilizers and the equidivisibility property, which suggested that combinatorics on words can be extended in the limit to free pro-aperiodic monoids, were studied in [26] (see also [4]). Further structural properties were deduced in [44].
The strongest result about the free pro-aperiodic monoid to date is Mc-Cammond's solution to the word problem for ω-terms over aperiodic monoids (where we recall that x ω is the idempotent power of x in a finite semigroup).
McCammond gave an identity basis for the ω-terms that are equal in all finite aperiodic monoids and provided normal forms to solve the word problem. His proof is technically difficult and relies on his solution to the word problem for free Burnside semigroups of sufficiently high exponent [34], which is an even more challenging result. For this reason, a number of authors have tried to come up with other approaches to this problem. Almeida, Costa and Zeitoun [8] gave a different proof of the correctness of McCammond's solution to the word problem that avoided using free Burnside semigroups [34]. Huschenbett and Kufleitner [29] provided an alternate, more efficient algorithm for solving the word problem for ω-terms over aperiodic monoids but they required McCammond's identity basis to prove the correctness of their algorithm. Their approach, like ours, makes use of the connection between first-order logic and aperiodic monoids. Almeida, Costa and Zeitoun have since proved a number of structural results concerning free pro-aperiodic monoids, making use of McCammond normal forms and limiting techniques [6,7]. More recently, Almeida, Klma and Kunc showed that the ω-word problem is decidable for the free pro-objects in the concatenation hierarchy for aperiodic monoids [9].
In this paper, we use Stone duality and saturated models to obtain most of the known structural results about free pro-aperiodic monoids, as well as some new ones. The crucial idea is that the identification of aperiodicrecognizable languages with first-order definable languages means that we can view elements of the free pro-aperiodic monoid as ultrafilters in the Boolean algebra of first-order definable languages, which in turn can be identified with the complete theories containing the theory of finite words. By the compactness theorem of logic, this can be in turn identified with elementary equivalence classes of models of the theory of finite words, socalled pseudofinite words. These models can be concatenated in a natural way, allowing us to recover the algebraic structure as well as the topological structure from this approach.

Models of the theory of finite words over an alphabet A are examples of
A-words (words over a discrete linear order with endpoints). Each elementary equivalence class of A-words contains special models, called ω-saturated models, that can realize, in a precise sense, its factorizations up to elementary equivalence. This allows one to prove many of the structural results for free pro-aperiodic monoids, like equidivisibility, in almost the same way as they are proved for free monoids.
One of our key technical results is that if we substitute ω-saturated words into an ω-saturated word, then the result is an ω-saturated word. This allows one to analyze the factors of the image of an element under a substitution in a very similar fashion to the case of free monoids. Using this result, we can explicitly construct countable ω-saturated models of ω-terms (which turn out to be the same models considered in [29]). This allows us to give simple, transparent proofs of many of the results of [6,8], such as the fact that factors of ω-terms are ω-terms, as well as providing a new correctness proof for the word problem algorithm of [29], avoiding entirely the machinery of McCammond [34]. Moreover, many of our results extend beyond just ω-terms. For example, in [8] it is shown that factors of an ω-term are wellquasi-ordered by divisibility and the finite factors form a regular language. By showing that these properties behave well under substitution, we can both easily recover this result for ω-terms and extend it beyond them.
In [26], structural results about free pro-aperiodic monoids were used in order to give clean proofs of the decidability of aperiodic idempotentpointlikes [24] and stable pairs [25] (i.e., one-vertex inevitable graphs in the sense of Almeida [2]). It is hoped that our approach will lead to simpler proofs of other algorithmic properties, as well as proofs of those needed to decide . Further work would be to extend the theory to monadic second order logic to describe absolutely free profinite monoids. This would, in particular, require a language rich enough to talk about p-adic numbers and free profinite groups!

Outline
In Section 1, we recall the relevant notions from logic that we need, in particular, Ehrenfeucht-Fraissé games. In Section 2, we recall how Stone duality can be used to relate the free pro-aperiodic monoid to logic. In Section 3 we show how substitutions and concatenations are interpreted from the logic point of view. In Section 4 we make a brief excursion into the question of axiomatizability of the theory of finite words. In Section 5 we recall the crucial notion of saturation from model theory, adapted to our specific context of words, and use topology to prove, as our main technical result, that saturated models behave well with respect to substitutions. In Section 6 we give a few basic applications of the theory developed thus far to the structure theory of the free pro-aperiodic monoid. In Section 7 we show how our results simplify the solution of the word problem for ω-terms.
In Section 8 we use our results to analyze factor orders in our pro-aperiodic monoids. We prove in particular that several properties are stable under substitutions (and hence in particular under concatenation and ω-power), including the properties that the J -ordering on the set of factors of an element is a well-quasi-order and that the set of factors of an element is regular.
1. Logic on words 1.1. Basic definitions. Regular languages of finite words are those subsets of a finitely generated free monoid that are saturated with respect to a finite index monoid congruence. Regular languages can alternatively be characterized using finite automata or regular expressions, see, e.g., [46].
Another important characterization of regular languages uses monadic second-order logic [14]. In this paper we only consider the first-order logic fragment. We recall the necessary definitions. In the logical approach, a finite word w = a 1 · · · a n over a finite alphabet A is viewed as a relational structure, in the sense of model theory, consisting of a binary relation <, capturing the ordering on positions in the word, and a unary relation P a , for each a ∈ A, capturing the positions carrying the letter a. For example, the word W = aba over the alphabet {a, b, c} is based on the finite set {1, 2, 3} with < the order 1 < 2 < 3, P a = {1, 3}, P b = {2}, P c = ∅. Viewing words in this way, we can use logical sentences to define sets of words. For example, the first-order sentence ∃x∃y(P a (x) ∧ P b (y) ∧ x < y) defines the regular language A * aA * bA * . Formally, we adopt the following definitions and notations, cf., e.g., Straubing's book [46] for more details.
Let A be a finite alphabet. A word over A, or A-word, is a tuple W = (|W |, < W , (P W a ) a∈A ), where • |W | is a set, • the relation < W is a discrete linear order with endpoints on |W |, i.e., there are a first and a last element, every element except the last has a unique immediate < W -successor and every element except the first has a unique immediate < W -predecessor, • (P W a ) a∈A is a partition of |W |. If W is an A-word and i ∈ |W |, we write W (i) for the unique letter a such that i ∈ P W a . An A-word is called finite if |W | is finite. We denote by ε the unique A-word with |W | = ∅.
An atomic formula is an expression of the form x < y or P a (x), where, here and in what follows, x and y denote first-order variables. A firstorder formula is an expression built up from atomic formulas by inductively applying the connectives ∧, ∨, ¬, →, ∃x, and ∀x. An occurrence of a variable in a formula ϕ is called bound if it is under the scope of a quantification, the occurrence is called free otherwise. If x = x 1 , . . . , x n is a tuple of first-order variables, the notation ϕ(x) indicates that all the variables that occur freely in ϕ lie in x. A sentence is a formula without free occurrences of variables.
If W is a word, an assignment of a tuple of first-order variables x in W consists of a tuple x W of the same length as x, where each element of the tuple x W is an element of |W |.
For a first-order formula ϕ(x), W a word and x W an assignment of x in W , we inductively define the relation |= as follows: The language defined by an FO-sentence ϕ is the set L ϕ of finite words W such that W |= ϕ.
The first-order definable languages form a strict subclass of the regular languages. For example, the language (AA) * , consisting of all words of even length is regular, but not first-order definable. Indeed, recall [42,36] that a language L of finite A-words is first-order definable if and only if the syntactic monoid of L is finite and aperiodic, i.e., does not contain non-trivial subgroups. This fundamental result is the starting point for our perspective on the free pro-aperiodic monoid, cf. Section 2 below.
1.2. Quantifier depth and EF games. A very useful classification of first-order formulas uses quantifier depth. The quantifier depth of a formula ϕ is the maximum nesting depth of quantifiers in ϕ. If U and V are Awords, we write U ≡ k V if U and V satisfy exactly the same first-order sentences ϕ of quantifier depth at most k; in this case, we say that U and V are elementarily equivalent up to quantifier depth k or simply k-equivalent. We write U ≡ V if U ≡ k V for all k, i.e., U and V satisfy exactly the same first-order sentences; in this case, we say that U and V are elementarily equivalent, or simply equivalent. Importantly, for each k ≥ 0, there are only finitely many k-equivalence classes, and each such class is definable by a first-order sentence. Indeed, each k-equivalence class is defined by a gamenormal sentence of depth k, and the set Θ 0,k of such sentences is finite [28,Thm. 3.3.2], see also [46,Sec. VI.1].
Below we will prove a first fundamental lemma about k-equivalence for words, which imports the well-known technique of Ehrenfeucht-Fraissé games (EF games) into our context. We briefly summarize the idea of EF games, referring to [28,Ch. 3] for more information. Let U and V be relational structures. The k-round EF game on (U, V ) has two players, ∀ and ∃. In the i th round of the game, ∀ begins by choosing a position in one of the two structures, denoted u i or v i , and ∃ responds by choosing a position in the other structure, denoted v i or u i . After k rounds, the player ∃ wins if u i → v i is an isomorphism between the substructure u of U and the substructure v of V . The main result about EF games is that U ≡ k V if and only if player ∃ has a winning strategy in the k-round EF game played on U and V .
We introduce a few useful notations. Let U be an A-word and i ∈ |U |, we write U (<i) for the A-word obtained by restricting U to the set of positions strictly less than i (the ray left of i), and similarly we define U (>i) (the ray right of i). If i and j are positions in U , we write U (i, j) for the open interval, U [i, j] for the closed interval of U between these positions, and halfopen intervals U [i, j) and U (i, j]. Since the order on an A-word is discrete with endpoints, any (half-)open interval or ray can be written as a closed interval; we use this fact without mention in what follows. Recall that U (i) denotes the unique letter a such that i ∈ P U a . Finally, if V is another Aword and j ∈ |V |, we say that the position only if for every i ∈ |U |, there exists j ∈ |V | that k-corresponds to i, and for every j ∈ |V |, there exists i ∈ |U | that k-corresponds to j.
Proof. For the left-to-right implication, suppose that U ≡ k+1 V and i ∈ |U |. Let j ∈ |V | be the element chosen by player ∃ according to her winning strategy in the (k + 1)-round EF game on (U, V ) if player ∀ chooses i in U as his opening move. Then certainly U (i) = V (j), and moreover player ∃ can win both k-round EF games on (U (<i), V (<j)) and on (U (>i), V (>j)) by following her winning strategy for the (k+1)-round EF game on (U, V ). Thus the position j k-corresponds to i, as required. This suffices, by symmetry. For the right-to-left implication, suppose that every i in U k-corresponds to some j in V and vice versa. Then player ∃ can win the (k + 1)-round EF game on (U, V ), as follows. If ∀ chooses a position i in U in the first round, ∃ responds by choosing a position j in V k-corresponding to i, and vice versa. In the remaining k rounds, ∃ follows her strategy for either (U (<i), V (<j)) or (U (>i), V (>j)), or, if ∀ plays at i in U (respectively, j in V ), then ∃ plays at j in V (respectively, i in U ). The resulting strategy is winning, because both strategies for (U (<i), V (<j)) and (U (>i), V (>j)) are winning.
An immediate consequence of this lemma is a form of cancellability of first and last letters up to ≡ k . Corollary 1.2. Let a ∈ A and let U, V be A-words. Suppose that k ≥ 0 and aU ≡ k+1 aV . Then U ≡ k V . Dually, if U a ≡ k+1 V a, then U ≡ k V . In particular, if aU ≡ aV , then U ≡ V and, similarly, if U a ≡ V a, then U ≡ V .
Proof. If k = 0, there is nothing to prove. Assume that k ≥ 1. Since ε is only k-equivalent to itself, the position of aV that must k-correspond to the first position of aU is the first position. Thus U ≡ k V .
1.3. Pseudofinite words. For a finite alphabet A, we denote by T A the (finitely axiomatized) theory of A-words, that is, the set of first-order sentences deducible from axioms expressing that the order is a discrete linear order with endpoints, and that exactly one letter predicate holds at each position. We further let T fin A denote the theory of finite A-words, i.e., the set of first-order sentences that are true in all finite A-words. A model of the theory T fin A is called a pseudofinite A-word. 1 The theories T A and T fin A do not coincide in general; in fact, they coincide only if the alphabet A contains a single letter. In this case, both theories T A and T fin A are the theory of discrete linear orders with endpoints, with a unary predicate that is true everywhere. The following lemma about the one-letter case is well-known (cf., e.g., [46, Thm. IV.2.1]). It can also be proved easily using Lemma 1.1.
In particular, if A = {a}, then any two infinite models of T fin A are elementarily equivalent. The smallest infinite model of T fin A is N + N op . As soon as A contains at least two letters, the situation is very different. In particular, there are A-words that are not pseudofinite. ∃xP a (x) → ∃x(P a (x) ∧ ∀y(y > x → ¬P a (y)) expressing 'if there exists an a-position, then there exists a last such' is true in every finite A-word, and therefore lies in T fin A , but it fails to hold in W . Thus, W is not pseudofinite.
A useful characterization of pseudofinite words is the following.
Proof. If U is pseudofinite, let k ≥ 0, and let ϕ k be the disjunction of those game-normal sentences of depth k which define the k-equivalence class of a finite word. Then ϕ k lies in T fin A , so U satisfies it, and therefore U also satisfies one of the disjuncts. The converse is clear.
In Section 4, we discuss the axiomatizability of the theory of finite Awords. In particular, we show that the theory is not finitely axiomatizable, and we give an axiomatization of it using an axiom scheme similar to the one given by Doets [17].

Pro-aperiodic monoids
A profinite monoid is an inverse limit of finite discrete monoids in the category of topological monoids (i.e., monoids whose underlying set is equipped with a topology in which the monoid operation is continuous). Equivalently, a profinite monoid is a topological monoid whose underlying space is a Boolean or Stone space, i.e., compact, Hausdorff and zero-dimensional. Profinite monoids inherit many properties of finite monoids. Of particular interest to us is the fact that any element x in a profinite monoid has a unique idempotent, denoted x ω , in its orbit-closure {x n | n ≥ 1}. A proaperiodic monoid M is a profinite monoid in which x ω = x ω x for all x ∈ M . Equivalently, a pro-aperiodic monoid is an inverse limit of finite aperiodic monoids; here, finite monoids are equipped with the discrete topology, and the inverse limit is taken in the category of topological monoids.
The free pro-aperiodic monoid generated by a finite set A is a pro-aperiodic monoid F A (A) containing A such that any function f : A → M , with M a finite aperiodic monoid, extends uniquely to a continuous homomorphism f : F A (A) → M , where M is given the discrete topology. The free proaperiodic monoid is unique up to topological isomorphism 2 , and the same extension property still holds if in the previous sentence M is replaced by an arbitrary pro-aperiodic monoid.
We recall some basic definitions from the theory of monoids that are used throughout the paper. Let M be a monoid and u, v elements of M . Then u ≤ J v means that there exist x and y such that u = xvy, and in this case v is called a factor of u; u ≤ L v means that there exists x such that u = xv, and in this case v is called a suffix of u; u ≤ R v means that there exists y such that u = vy, and in this case v is called a prefix of u. Each of these relations is a quasi-order on M ; the equivalence relations they induce are denoted J , L and R, e.g., u J v means that u ≤ J v and v ≤ J u. We will repeatedly use the easily proved facts that any homomorphism preserves Green's relations, and that, if α 1 ≤ L α 2 and β 1 ≤ R β 2 , then α 1 uβ 1 ≤ J α 2 uβ 2 for any u. Recall that an element e in a monoid M is called idempotent if ee = e. The set of idempotent elements in M is denoted by E(M ). A (two-sided) ideal I in a monoid M is a subset which is closed under multiplication by arbitrary elements from M on both sides, i.e., if a ∈ I and u ∈ M , then ua ∈ I and au ∈ I. An ideal is idempotent if I = I 2 , i.e., for every a ∈ I, there exist x, y ∈ I such that a = xy. An ideal is prime if its complement is a submonoid.
We make use of Stone duality, which, we briefly recall, is the dual equivalence between the categories of Boolean algebras and Boolean spaces that takes a Boolean space X to its algebra K(X) of clopen sets, and a Boolean algebra B to the set of ultrafilters Spec(B) of B, which is given a Boolean 2 Here and in what follows, we use the term 'homeomorphism' to indicate an isomorphism in the category of topological spaces, and 'topological isomorphism' for an isomorphism in the category of topological monoids. topology by declaring, for each L ∈ B, the set L := {x ∈ Spec(B) | L ∈ x} to be open. Stone's duality theorem [45] says that the assignment L → L is an isomorphism from B to K(Spec(B)), and that moreover Boolean algebra homomorphisms B 1 → B 2 are in natural bijection with continuous functions Spec(B 2 ) → Spec(B 1 ).
The topological space underlying any profinite monoid is the Stone dual space of some Boolean algebra. For free objects, this Boolean algebra is particularly nice. The following result has a long history and holds in much more generality, but we do not go into this here; for more information, see, e.g., [19,Sec. 4]   We already cited in the previous section the characterization theorem [42,36] that first-order languages are exactly the aperiodic-recognizable languages. We now connect this result with Theorem 2.1. Recall that, for T a set of sentences in first-order logic, the Lindenbaum-Tarski algebra LT(T ) of T is the Boolean algebra of T -equivalence classes of first-order sentences.
Here, two first-order sentences ϕ and ψ are T -equivalent if, in any model W where all sentences in T are true, ϕ and ψ are either both true or both false.

Proposition 2.2. The Lindenbaum-Tarski algebra LT(T fin A ) of the theory T fin
A is isomorphic to the algebra Rec A (A) of aperiodic-recognizable languages of finite A-words.
Proof. Note that two first-order sentences ϕ and ψ define the same language if and only if ϕ and ψ are T fin A -equivalent. Thus, the assignment ϕ → L ϕ is a well-defined injective function from LT(T fin A ) to languages of finite A-words. It is clear from the definition of L ϕ that this is a homomorphism. The image of this homomorphism consists exactly of the aperiodic-recognizable languages [42,36].

Theorem 2.1 and Proposition 2.2 together immediately entail that F A (A) is homeomorphic to the Stone dual space of LT(T fin
A ). Stone dual spaces of Lindenbaum-Tarski algebras are well understood in logic. Proposition 2.3. Let T be a set of first-order sentences. The Stone dual space of LT(T ) is homeomorphic to the Boolean space X whose points are elementary equivalence classes of models of T , in which the clopen sets are exactly the truth sets ϕ := {x ∈ X | ϕ is true in models in the class x}, for ϕ any first-order sentence.
Proof. To any model W of T , associate the ultrafilter U W of sentences that are true in W . By definition, U W = U W ′ if and only if W and W ′ are elementarily equivalent, so this is a well-defined injective function from X to Spec LT(T ). The completeness theorem for first-order logic entails that this map is also surjective. It is clear from the definitions that it is continuous and open, and therefore a homeomorphism.
The following theorem is now an immediate application of the preceding three results.
is homeomorphic to the Boolean space of elementary equivalence classes of pseudofinite A-words, with clopens the truth sets of first-order sentences.
We will henceforth identify F A (A) with the space of classes of pseudofinite A-words. Since F A (A) is not just a topological space, but also carries a monoid multiplication, it is natural to ask how one can multiply two classes of pseudofinite A-words. This is done by simply concatenating A-words in each class and taking the class of the result, as we will see in the next section.
We will denote by Λ(A) the Stone dual space of LT(T A ). By Proposition 2.2, elements of Λ(A) are elementary equivalence classes of A-words. In view of Theorem 2.4, F A (A) can be viewed as a subspace of Λ(A), also see Proposition 3.5 below. It will often be useful to consider this larger space Λ(A) consisting of all classes of A-words, not just the pseudofinite ones.
The spaces Λ(A) and F A (A) can be usefully described as inverse limits of particular countable chains of finite discrete spaces, as follows. For each k ≥ 0, let Λ(A) k denote the set of ≡ k -classes of A-words. As noted in Section 1, the set Λ(A) k is finite. Since ≡ k+1 refines ≡ k , and ≡ refines each ≡ k , we get a diagram The Stone dual of this diagram is the chain of subalgebra inclusions where LT(T A ) k is the finite Boolean algebra of T A -equivalence classes of first-order sentences of depth at most k. Since LT(T A ) is clearly the direct limit (union) of the diagram in (2), it follows from Stone duality that Λ(A) is the inverse limit as a topological space of the diagram in (1) Note that Example 1.4 shows that F A (A) 2 is strictly contained in Λ(A) 2 , since the word W in that example fails to satisfy a depth 2 sentence that holds in all (pseudo)finite words. We will show in the next section that these remarks still hold true when the spaces are endowed with a monoid structure.
Let us denote, for each k ≥ 0, by π k the continuous projection map Λ(A) ։ Λ(A) k . With a slight abuse of notation, if u, u ′ ∈ Λ(A), we write u ≡ k u ′ to mean π k (u) = π k (u ′ ), i.e., U ≡ k U ′ for all U in the class u and U ′ in the class u ′ .
We can now draw the following useful conclusions.
(1) Clear from the fact that Λ(A) is the inverse limit of the finite discrete spaces Λ(A) k .
(2) Using (1) and the definition of product topology, f is continuous if and only if for any U , k, the set

Substitutions and concatenation
Suppose that V is a word over a finite alphabet B, and that for each b ∈ B, U b is a word over a finite alphabet A. We obtain a new word V [b/U b ] over A by substituting the word U b for each occurrence of the letter b in V ; see Figure 1.
• The underlying order of W is the lexicographic order on the disjoint union • The letter at position (i, j) in W is the letter at position j in U V (i) . There are two important special cases of substitution. If U 0 and U 1 are A-words, then the concatenation U 0 · U 1 of U 0 and U 1 is defined as the substitution of (U b ) b∈{0,1} into the {0, 1}-word 01. If U is an A-word and λ is a discrete linear order with endpoints, then the λ-power U λ of U is defined as the substitution of U b = U into the unique {b}-word with underlying order λ. Importantly, the operation of substitution respects the equivalence relations ≡ k . This is a well-known result in the model theory of labelled linear orderings, cf., e.g., [ The letters at these positions are U b (j) and U ′ b (j ′ ), which are the same because j ′ k- Notice that Y is the result of substituting the words (U b ) b∈B∪{c} into the word V (<i)c, obtained by appending a single letter c to the end of . Corollary 3.2 in particular implies that there is a well-defined binary operation of concatenation on the set Λ(A) of elementary equivalence classes of A-words. Recall from the previous section that this set is naturally a Stone space, being the dual space of the Lindenbaum-Tarski algebra of the theory T A .
Theorem 3.3. The Stone space Λ(A), equipped with the operation of concatenation up to elementary equivalence, is a pro-aperiodic monoid. The where λ is any infinite discrete linear order with endpoints.
Proof. It is straight-forward to prove that concatenation is an associative operation with neutral element the class of an empty word. Moreover, concatenation is continuous by combining Proposition 3.1 with Proposition 2.5 (2).
We prove the statement about the ω-power. Let u = [U ] ≡ ∈ Λ(A) be arbitrary and let λ be an infinite discrete linear order with endpoints. Let v := [U λ ] ≡ . To prove that v = u ω , we need to show that v is idempotent and lies in {u n | n ≥ 1}. For idempotency, notice that U λ · U λ is isomorphic to U λ+λ . Since the models λ and λ + λ are elementarily equivalent, it follows from Corollary 3.2 that U λ ≡ U λ+λ ∼ = U λ · U λ , so v is idempotent. We now prove that v lies in the orbit-closure of u. For every k ≥ 0, by Lemma 1.3, for n k := 2 k −1 we have that λ is k-equivalent to the finite linear order on n k elements. By Proposition 3.1, v ≡ k u n k . It now follows from Proposition 2.5 that v lies in the closure of {u n | n ≥ 1}.
Given the description of the operation () ω , it is easy to see that Λ(A) satisfies the equation x ω = x ω x; indeed, for any A-word U , the A-words U λ and U λ U are isomorphic.
Recall from Theorem 2.4 and the remarks following it that the free proaperiodic monoid, F A (A), is isomorphic to a subspace of Λ(A). Indeed, we can prove more.
Proposition 3.5. The set of classes of pseudofinite A-words is a topologically closed submonoid of Λ(A), which is the free pro-aperiodic monoid F A (A). Moreover, it is the complement of a prime ideal of Λ(A).
Proof. Let us denote by PF(A) the set of classes of pseudofinite A-words in Λ(A). The set PF(A) is closed because it can be written as the intersection ϕ∈T fin A ϕ. It is a submonoid (whose complement is a prime ideal) by Lemma 3.4. It now follows from Theorem 3.3 that PF(A) is a proaperiodic monoid, and by Theorem 2.4 its underlying space is homeomorphic to F A (A). The homeomorphism of Theorem 2.4 sends any finite word w in F A (A) to the elementary equivalence class of that finite word in PF(A). Thus, the homeomorphism clearly preserves multiplication of finite words. Since the finite words are a dense subset of F A (A), it is an isomorphism for the monoid structure.
Convention. In view of Proposition 3.5, we henceforth identify the free pro-aperiodic monoid F A (A) with the closed submonoid PF(A) of Λ(A) consisting of the elementary equivalence classes of pseudofinite A-words, and we will no longer make a notational distinction between the two. Moreover, throughout the paper except in Section 7, we tacitly consider A-words up to isomorphism.
Recall that in Section 2 we defined Λ(A) k to be the finite set of ≡ kequivalence classes of A-words. By Proposition 3.1, Λ(A) k carries a monoid multiplication for each k, which is given in the same way as the multiplication on Λ(A). Hence, the chain in (1) defined in the previous section, which exhibited the topological space Λ(A) as the inverse limit of finite sets Λ(A) k , now also exhibits the topological monoid Λ(A) as the inverse limit of finite monoids Λ(A) k . The same remarks apply to F A (A) and F A (A) k .
Notice that, for every k, Λ(A) k is an aperiodic monoid. This is true because Λ(A) k is a finite quotient of the pro-aperiodic monoid Λ(A), but we can say something more precise. Indeed, for any A-word W , Proposition 3.1 implies that Remark 3.6. It is essentially implicit in [46] that a formula ϕ has quantifier depth at most k if and only if the syntactic monoid of the language of ϕ belongs to a strongly bracketed iterated block product of k copies of the pseudovariety of semilattices. In fact, using Lemma 1.1 and a well known description of the free object in a block product of locally finite pseudovarieties, due to Almeida (cf. [1]), it is easy to see that F A (A) k is the relatively free monoid on A in the strongly bracketed iterated block product of k copies of the pseudovariety of semilattices. We will show in future work that, more generally, Λ(A) k belongs to the strongly bracketed iterated block product of k copies of the pseudovariety of semilattices.
Moreover, if each U b is pseudofinite, then f restricts to a map F A (B) → F A (A), and any continuous homomorphism from F A (B) to F A (A) arises in this manner.
Proof. The function f is well-defined by Corollary 3.2, it is continuous by Proposition 3.1 and Proposition 2.5.2, and it is a homomorphism because the A-words For the moreover statement, notice that if each U b is pseudofinite and V is pseudofinite, then V [b/U b ] is pseudofinite. Indeed, using Lemma 1.5, for , and the latter is a finite A-word.
If g : class g(b). The substitution arising from this B-indexed collection of A-words is a continuous homomorphism that extends g|B : B → F A (A), so it must coincide with g by the uniqueness part of the universal property of F A (B).
We call a continuous homomorphism f : Λ(B) → Λ(A) a substitution if it arises as in Proposition 3.7. We suspect that there exist continuous homomorphisms f : Λ(B) → Λ(A) that are not substitutions, but we leave it to further work to exhibit a concrete example.

Axiomatizing pseudofinite words
We saw in Example 1.4 that if the alphabet A contains at least two letters, then T A and T fin A do not coincide. Using Proposition 3.1, we can prove more. Theorem 4.1. If the alphabet A contains at least two letters, then T fin A is not finitely axiomatizable.
Proof. Generalizing Example 1.4, for each k ≥ 0, consider the word W k defined as ab 2 k ab 2 k . . . . . . ab 2 k −1 ab 2 k −1 . Then W k is not pseudofinite because it does not have a last occurrence of the factor ab 2 k .
Claim. W k is k-equivalent to the finite word (ab 2 k −1 ) 2 k −1 .
Proof of Claim. Note first that b 2 k ≡ k b 2 k −1 by Lemma 1.3, so ab 2 k ≡ k ab 2 k −1 by Proposition 3.1. Applying Proposition 3.1 to the substitution of U c = ab 2 k and U d = U ′ c = U ′ d := ab 2 k −1 into the {c, d}-word c N d N op , we get that W k is k-equivalent to (ab 2 k −1 ) N+N op . The latter, in turn, is k-equivalent to (ab 2 k −1 ) 2 k −1 since c N+N op is k-equivalent to c 2 k −1 by Lemma 1.3. Now, if S is a finite set of sentences in T fin A , let k be the maximum quantifier depth of sentences occurring in S. Then W k will satisfy all sentences in S, because it is k-equivalent to a finite word, but it is not a model of T Last ϕ : ∃xϕ(x) → ∃x(ϕ(x) ∧ ∀y(y > x → ¬ϕ(y))), which expresses that if ϕ(x) is true for some position x, then there is a last position where ϕ(x) is true. Clearly, Last ϕ is true in every finite word for every first-order formula ϕ(x).
Proposition 4.2. The theory of finite A-words, T fin A , is generated by adding to T A the sentences Last ϕ for each first-order formula ϕ(x) in one free variable.
Proof. Let U be an A-word which satisfies Last ϕ for each ϕ(x). Let k ≥ 0 be arbitrary. Let ϕ(x) be a first-order formula expressing that 'the subword U (<x) is k-equivalent to some finite word'; such a formula exists because ≡ k has finitely many classes, each of which is first-order definable, and one can relativize formulas with respect to a position, cf. Lemma A.1. Let f denote the first position of the word U . Since U (<f ) is the empty word, ϕ(f ) holds. Since U satisfies the scheme, pick the last position i where ϕ(i) holds. Pick a finite word V that is k-equivalent to U (<i). Then, by Proposition 3.1, U (≤i) ≡ k V a, where a := U (i). Since i is the last position where ϕ(i) holds, we must have that i is the last position in U , so U is k-equivalent to the finite word V a. By Lemma 1.5, since k was arbitrary, U is pseudofinite.

Saturated words
In this section we introduce the useful notions of types and saturation from model theory, and prove that substitution preserves saturation. We do not introduce the notions in their full model-theoretic generality, but apply them immediately to our context of words. We do this so that the statements of our results can be understood without knowledge of the model-theoretic background, although the proofs do rely on results from model theory. We will justify and explain our use of the model-theoretic terminology in the Appendix.
Let A be a finite alphabet and let U be an A-word. For any position i ∈ |U |, define the triple an element of the Cartesian product Λ(A) × A × Λ(A). We call t U (i) the type of i in U . We prove in Proposition A.3 that this definition of type is equivalent to the usual definition of (complete 1-)type in model theory. We refer to the product space Λ(A) × A × Λ(A), where the middle component A has the discrete topology, as the type space.
We write RT(U ) for the set of types realized in U , i.e., RT(U ) is the subset {t U (i) | i ∈ |U |} of the type space. If V is an A-word elementarily equivalent to U and j ∈ |V |, then we say the type t V (j) is consistent with U . We write CT(U ) for the set of types consistent with U , i.e., CT(U ) is the subset {t V (j) | V ≡ U, j ∈ |V |} of the type space.
The A-word U is weakly saturated if every type consistent with U is realized in U , i.e., CT(U ) = RT(U ). The A-word U is ω-saturated if every closed interval U [i, j] in U is weakly saturated. Note that a closed interval in an ω-saturated word is ω-saturated. Also, any finite A-word is ω-saturated. We prove in Proposition A.4 that this definition is equivalent to the usual definition of ω-saturation in model theory. Following usual model theoretic terminology, we say that an A-word is countably saturated if it is ω-saturated and its underlying set is countable. • (a n , a, a m ) for n, m ∈ N; • (a n , a, a ω ) for n ∈ N; • (a ω , a, a m ) for m ∈ N; • (a ω , a, a ω ). Consider the following infinite {a}-words: (1) where Q×Z denotes the Q-indexed lexicographic sum of copies of Z. The word W 1 is not weakly saturated, because the elementarily equivalent word W 2 realizes the type (a ω , a, a ω ), which is not realized in W 1 , that is, (a ω , a, a ω ) ∈ CT(W 1 ) \ RT(W 1 ). The word W 2 is weakly saturated, because it realizes all the types. However, W 2 is not ω-saturated, because the ray to the left of i, where i is any point in the summand Z, is isomorphic to W 1 , and not weakly saturated. Notice that any closed interval in the word in W 3 is either finite or isomorphic to W 3 , using the well-known fact that any open interval in the order Q is isomorphic to Q (cf. e.g., [28, p. 100]). Since finite words and W 3 are weakly saturated, the word W 3 is in fact ω-saturated, and even countably saturated.
One advantage of ω-saturated words is that they realize any finite factorization of their elementary equivalence class.
Lemma 5.2. Let W be an ω-saturated A-word and suppose that W ≡ W 1 · · · W n with W 1 , . . . , W n non-empty A-words. Then we can find posi- Proof. We proceed by induction on n (for all ω-saturated A-words). If n = 1, then there is nothing to prove. Assume it is true for n − 1 and W ≡ W 1 · · · W n . Put U = W 1 · · · W n−1 . Since W is weakly saturated, we can find i n−1 ∈ |W | such that W (<i n−1 ) ≡ U and W (≥i n−1 ) ≡ W n . Then since W (<i n−1 ) is ω-saturated, by induction we can find i 1 < i 2 < · · · < i n−2 with i n−2 < i n−1 and W (<i 1 ) ≡ W 1 and W [i j , i j+1 ) ≡ W j+1 for 1 ≤ j ≤ n − 2. This completes the proof. Proof. Notice that the function Λ(A) × A × Λ(A) → Λ(A) which sends (u, a, v) to uav is continuous, since multiplication is continuous. Therefore, CT(U ), which is the inverse image of the point [U ] ≡ under this map, is closed. To see that CT(U ) is the closure of RT(U ), let t ∈ CT(U ), and let ϕ×{a}× ψ be an arbitrary basic neighbourhood of t. By definition of CT(U ), pick V ≡ U and j ∈ |V | such that t = t V (j). Let k be the maximum of the quantifier depths of ϕ and ψ. Since in particular U ≡ k+1 V , by Lemma 1.1 pick i ∈ |U | which k-corresponds to the position j in V . Then U (i) = a, and, since V (<j) |= ϕ, we have U (<i) |= ϕ, and similarly U (>i) |= ψ. Thus, It is well-known in model theory that any elementary equivalence class contains an ω-saturated model, which typically has an uncountable underlying set; see Proposition A.5 in the Appendix.
Our main theoretical result about ω-saturated words is that they are stable under substitutions (Theorem 5.7). We first prove this for weakly saturated words, and then deduce it for ω-saturated words.
For the proof of our next theorem, some more notation will be useful. We denote by α the continuous two-sided action of Λ(A) on the type space, This observation can be written as the following equality: Now suppose that the words V and U b are weakly saturated. By Lemma 5.3 the sets RT(V ) and RT(U b ) are closed. Since ι b , f and α are continuous, and therefore closed, maps and B is finite, the equality (4) implies that is weakly saturated by Lemma 5.3.
Since concatenations and powers are particular cases of substitutions, we immediately obtain the following corollary.
Corollary 5.5. The concatenation of two weakly saturated words is weakly saturated. The power of a weakly saturated word by a weakly saturated linear order is weakly saturated.
We will now deduce an analogous result for ω-saturation. The following basic, but important, lemma analyzes intervals in substituted words. By a prefix of an A-word U we mean an A-word of the form U (<i) for some i ∈ |U |, and by a suffix of U we mean an A-word of the form U (>i) for some i ∈ |U |.  Proof. For r = 1, 2, we have k r = (i r , j r ) for some i r ∈ |V | and j r ∈ |U V (ir) |.
There are two cases.
(1) i 1 = i 2 . In this case, letting b : (2) i 1 < i 2 . Let b r := V (i r ), X the suffix of V b 1 beginning at j 1 , Y the prefix of V b 2 ending at j 2 , and Z the interval V (i 1 , i 2 ).
We prove that W is weakly saturated. By Lemma 5.6, there are two cases.
(1) If W is an interval in U b for some b, then W is weakly saturated because U b is ω-saturated by assumption. ( is weakly saturated by Theorem 5.4, and X and Y are weakly saturated since the U b are ω-saturated by assumption. By Corollary 5.5, the concatenation of weakly saturated words is weakly saturated.
As before, Theorem 5.7 has the following immediate consequence.
Corollary 5.8. The concatenation of two ω-saturated words is ω-saturated. The power of an ω-saturated word by an ω-saturated linear order is ωsaturated.
As a first application of Theorem 5.7, we will now analyze what factors of the result of a substitution can look like in Λ(A) (and hence, by Lemma 3.4, in F A (A)). (1) f (v) ≤ J w if and only if one of the following holds: (3) f (v) ≤ L w if and only if w = ε or there exist y ∈ Λ(A), c ∈ B, and z ∈ Λ(B) such that f (c) ≤ L y, v ≤ L cz, and w = yf (z).
Proof. The case w = ε is trivial, so we may henceforth assume w = ε. By Proposition A.5, for each b ∈ B pick an ω-saturated A-word U b in the class f (b), pick an ω-saturated B-word V in the class v, and also pick an . By Lemma 5.6, there are two cases.
(a) The A-word W ′ is an interval in some U b . In this case, ( (3) Follows from (2) by symmetry.
We may usefully summarize Theorem 5.10 by recalling some more notation. For any u ∈ Λ(A), write ↑ J u := {w ∈ Λ(A) | w ≥ J u} for the set of factors of u, and similarly ↑ R u for the set of prefixes of u, and ↑ L u for the set of suffixes of u. Also, for any subset L of Λ(A) and a, b ∈ A, write a −1 L := {u ∈ Λ(A) | au ∈ L} and Lb −1 := {u ∈ Λ(A) | ub ∈ L}, and a −1 Lb −1 := {u ∈ Λ(A) | aub ∈ L}. Finally, if u ∈ Λ(A), write C(u) := {a ∈ A | u ≤ J a} for the content of u. The following restates Theorem 5.10 in this notation. , Theorem 5.10 can be viewed as extension of [10,Lemma 8.2], where the special case that v is a finite word is handled in the context of the free profinite monoid.

Structure theory of Λ(A) and F A (A)
In this section, we illustrate how the model-theoretic methods developed in the previous sections can be used to derive known results about F A (A) in a simple way, as well as new ones. Moreover, since these results do not depend on the words involved being pseudofinite, we are actually able to show the same results hold in the larger pro-aperiodic monoid Λ(A).
The following properties of Λ(A) will be used in Section 8. They are wellknown for F A (A), and indeed items (1) and (2) in Lemma 6.1 are known to hold in any compact monoid. Items (3) and (4) in Lemma 6.1 say that the Land R-orders on Λ(A) and F A (A) are unambiguous. The latter result was first proved in [39].
if w ≤ L x and w ≤ L y, then x ≤ J y implies x ≤ L y, (6) if w ≤ R x and w ≤ R y, then x ≤ J y implies x ≤ R y.

The same holds true for F A (A).
Proof. (1) If w ≤ J xw, pick α, β ∈ Λ(A) such that w = αxwβ. Repeatedly substituting αxwβ for w, we see that w = (αx) n wβ n for every n. Therefore, w = (αx) ω wβ ω , by continuity. Since (αx) ω is idempotent, we get from this that w = (αx) ω w. Now, using aperiodicity, (3) Let W be a weakly saturated A-word in the class w. (5) By (3), since w ≤ L x and w ≤ L y, we either have x ≤ L y or y ≤ L x. If y ≤ L x, pick α such that y = αx. Then x ≤ J y = αx, so (1) gives x ≤ L αx = y, as required. (6) is dual to (5).
The result for F A (A) follows from the result for Λ(A) because the former is the complement of a prime ideal in the latter.
The following result on factors of a specific form will be useful in Section 8. It could be deduced from Theorem 5.10, but we prefer to give a direct proof. Lemma 6.2. Let z, z ′ be A-words and b, c ∈ A. If bzc ≤ J bz ′ c, then one of the following four statements is true: Proof. Let Z be an ω-saturated word in the class z and let Z ′ be an A-word in the class z ′ . Then V := b · Z · c is an ω-saturated word in the class bzc, by Corollary 5.8. Let ⊥ denote the first position in V and ⊤ denote the last position in V , so that There are four cases: (1) i = ⊥ and j = ⊤. In this case, (3) i = ⊥ and j = ⊤. By a proof analogous to (2), z ≤ L bz ′ .
x ∈ M such that either ux = u ′ and xv ′ = v, or u ′ x = u and xv = v ′ . The following result for F A (A) was implicitly proved in [26] (but not explicitly formulated), using limiting arguments, and explicitly proved in [4]. Proof. Let U , V , U ′ and V ′ be A-words such that U V ≡ U ′ V ′ , and choose a weakly saturated A-word W in this class. Since W is weakly saturated, pick positions i, j ∈ |W | such that W (<i) ≡ U , W (≥i) ≡ V , W (<j) ≡ U ′ and Then U X ≡ W (<j) ≡ U ′ and XV ′ ≡ W (≥i) ≡ V . Moreover, if the A-words U and U ′ are pseudofinite, then X is pseudofinite by Lemma 3.4.
We deduce a few consequences of equidivisibility.
Recall that the right stabilizer of an element u in a monoid M is the submonoid Stab M (u) := {s ∈ M | us = u}. The next result for F A (A) is from [26]. It was used there to establish the decidability of membership in a number of semidirect products of the form V * A. Corollary 6.4. For any u ∈ Λ(A), the quasi-order ≤ L on the right stabilizer Stab Λ(A) (u) is total. If u ∈ F A (A), the quasi-order ≤ L on the right stabilizer Stab F A (A) (u) is also total.
By symmetry, the dual result to Corollary 6.4 holds for left stablizers and the quasi-order ≤ R .
The following result was proved by the second-named author for F A (A) [44]. Proof. Let I be an idempotent ideal and let ab ∈ I. Pick x, y ∈ I such that ab = xy. By Proposition 6.3, we have either a ≤ R x or b ≤ L y. Therefore, one of a and b lies in I. For the 'in particular' statement, notice that the ideal generated by an idempotent element is idempotent.
In the following two propositions, we use the well-known fact that in a finite aperiodic semigroup, and hence in a pro-aperiodic semigroup, Hclasses are trivial, where H is the intersection of the equivalence relations L and R. See [1,40]. First we extend a result from [44] to Λ(A). Proposition 6.6. Suppose that u ∈ Λ(A) has finite order, i.e., generates a finite subsemigroup. Then u is an idempotent.
Proof. Since the submonoid generated by u is closed, it must be aperiodic and so u n = u n+1 for some n > 0. Therefore, u n is idempotent. But then since u n generates a prime ideal, we must have that u and u n are Jequivalent. By Lemma 6.1, we have that u and u n are H-equivalent and hence equal. Thus u is an idempotent. Proposition 6.7. If u ∈ Λ(A) and e ∈ E(Λ(A)) are such that ue = eu, then ue ∈ {u, e}.
(2) Suppose that ux = e = xu. Notice that, for every n ≥ 1, x n and e commute. By Lemma 6.1.3, for every n ≥ 1, since x n e = ex n is a common lower bound of e and x n in ≤ L , the elements e and x n are comparable in ≤ L . We further distinguish two cases.
(2a) There exists n ≥ 1 such that x n ≤ L e. Then x n lies in the ideal generated by e, which is prime by Lemma 6.5. Hence, x lies in the ideal generated by e, i.e., x ≤ J e. Since e = ux, Lemma 6.1.1 implies that x ≤ L e, and similarly, x ≤ R e. Since we already have e ≤ R x and e ≤ L x, we conclude that x is H-equivalent to e. Since Λ(A) is pro-aperiodic, we obtain x = e, and hence ue = ux = e.
(2b) For every n ≥ 1, e < L x n . Since ≤ L is a closed relation in any compact monoid [40, Proposition 3.1.9], we get e ≤ L x ω . Pick v such that e = vx ω . Since x ω is idempotent, we obtain ex ω = v(x ω ) 2 = vx ω = e. Therefore, using that x ω x = x in the pro-aperiodic monoid Λ(A), we have eu = ex ω u = ex ω xu = ex ω e = e 2 = e, as required.

The word problem for aperiodic ω-terms
In this section, we use our techniques to give an improved proof of the decidability of the word problem for ω-terms in F A (A). The original proof of decidability was due to McCammond [35], who introduced normal forms based on an inductively defined notion of rank. McCammond shows, using an infinite basis of natural identities satisfied by ω-terms, that each ω-term can be placed into normal form. The construction of the normal forms is quite technical and it is not clear how efficient it is to find the normal form of a term from the viewpoint of time complexity. By far, the most difficult part of McCammond's paper is the proof that distinct normal forms represent distinct elements of F A (A). The separation of normal forms makes use of his solution to the word problem for free Burnside semigroups of sufficiently large exponent [34], which inspired the definition of the normal forms in the first place. Of particular importance is that free Burnside semigroups of sufficiently large exponent have a system of co-finite ideals with empty intersection and so the quotients by these ideals can be used to distinguish the normal forms for ω-terms. Recently, Almeida, Costa and Zeitoun [8] have provided a new proof that distinct McCammond normal forms represent distinct ω-terms in F A (A) by introducing star-free languages associated to normal forms whose closures can be used to separate them.
Huschenbett and Kufleitner [29] have developed a new algorithm to solve the word problem for ω-terms in F A (A) using model-theoretic ideas. From the point of view of our paper, they assign an A-word to each ω-term whose elementary equivalence class represents the corresponding element of F A (A). They then prove that two such interpretations of ω-terms are elementarily equivalent to each other if and only if they are isomorphic. They use work of Bloom and Esik [11] to prove that isomorphism can be decided in exponential time in the size of the expression as an ω-term; in the conference presentation of this result they announced that this can be improved to polynomial time using recent work of Lohrey and Mathissen [32]. The catch is that Huschenbett  Our work allows us to give a direct proof of the correctness of the Huschenbett and Kufleitner algorithm, circumventing McCammond's results entirely. Our proof uses the same interpretation of ω-terms as in [29], and provides a new justification for that interpretation, in the following sense. We show (Proposition 7.1) that the interpretation of ω-terms in [29] picks out a countably saturated A-word in the elementary equivalence class represented by an ω-term. That two ω-terms, thus interpreted, represent the same element of F A (A) if and only if they are isomorphic is then an immediate consequence of the standard model-theoretic fact that elementarily equivalent countably saturated models are isomorphic.
We begin with a few definitions. Let A be a finite alphabet. An ω-term over A is a term built up from finite words by using concatenation and ω-power. If M is a profinite monoid containing the alphabet A, then any ω-term t has a natural interpretation [[t]] M in M . In the case M = F A (A), we will now inductively define, for any ω-term t, a particular A-word U t in the class [[t]] F A (A) . Let ρ denote the linear order N + Q × Z + N op , which is countably saturated (cf. Example 5.1).
• If t is a term representing a finite word, let U t be that finite word.
Proposition 7.1. For any ω-term t, the A-word U t is a countably saturated A-word in the elementary equivalence class Proof. Finite words are countably saturated. Concatenations and ρ-powers of countably saturated A-words are clearly countable, and, by Corollary 5.8, ω-saturated. An easy induction, using Theorem 3.3 for the step involving ω-power, shows that U t lies in Theorem 7.2. For any ω-terms t 1 , t 2 , the following are equivalent: Proof. (2) ⇒ (1) is clear, since isomorphic A-words are elementarily equivalent. (1) ⇒ (2). By Proposition 7.1, both U t 1 and U t 2 are countably saturated models in the same elementary equivalence class. By the uniqueness of countably saturated models (cf., e.g., [16, Thm. 2.3.9]), U t 1 and U t 2 are isomorphic.
In order to decide the word problem for ω-terms in F A (A), one can now proceed as in [29] and use a decidability procedure for isomorphism of regular words (cf. [11] or [32]) to decide isomorphism of the countably saturated Awords interpreting the ω-terms.

Factors
In this section, we exploit our model-theoretic view of F A (A) (and Λ(A)) to analyze the J -orderings on sets of factors of elements in Λ(A). We recover several of the structural results on factors of ω-terms that were obtained in [6,7] using McCammond's normal forms as special cases of more general results. Again, our proofs avoid such normal forms altogether; instead, we use Theorem 5.10 on factors of a substitution. The following result was first proved by Almeida, Costa, and Zeitoun [7, Theorem 7.4].
Theorem 8.1. Prefixes, suffixes and factors of elements in F A (A) that are interpretations of ω-terms are again interpretations of ω-terms.
Proof. We prove the statement for prefixes. The statement for suffixes then follows by symmetry, and the statement for factors, in turn, then follows because any factor is a suffix of a prefix. We write [[t]] as shorthand for [[t]] F A (A) . We will prove by induction on the complexity of an ω-term t that,   It is shown in [7, Theorem 5.1] that an interpretation of an ω-term has only finitely many regular J -classes above it. Here, recall that a J -class is regular if it contains an idempotent element. For an element w in a monoid M , we write Reg(w) := {J e | w ≤ J e, e idempotent} for the set of regular J -classes above w where, as usual, J e denotes the Jclass of e. The original proof relies on McCammond normal forms, whereas our proof is fairly elementary from Theorem 5.10. Our proof also provides a simple algorithm to compute the regular J -classes above the interpretation of an ω-term from the ω-term.
In particular, the interpretation of any ω-term has finitely many regular J -classes above it. Proof.
(1) The right-to-left inclusion is clear since Conversely, if w 1 w 2 ≤ J e for some idempotent e, then w 1 ≤ J e or w 2 ≤ J e by Lemma 6.5.
(2) The right-to-left inclusion is clear since u ≥ J u ω . Conversely, suppose that e is an idempotent such that u ω ≤ J e. By Theorem 5.10, there exist z ∈ N ∪ {ω}, x ≥ L u and y ≥ R u such that e = xu z y. If z ∈ N, then e ≥ J u z implies that e ≥ J u, by Lemma 6.5, so J e ∈ Reg(u). If z = ω, then e ≤ J u ω ≤ J e and so J e = J u ω .
The final statement follows from (1) and (2) by induction on the complexity of the ω-term defining w, noticing that, for a finite word w, Reg(w) = {J ε }.
We recall the notion of well-quasi-order (wqo), which is a quasi-order satisfying the equivalent properties in the following proposition.
Proposition 8.3. Let (Q, ) be a quasi-order. The following are equivalent: (1) there are no infinite antichains or infinite descending chains in Q; (2) for every infinite sequence (q i ) i∈ω in Q, there exist i < j such that q i q j ; (3) every infinite sequence in Q contains a non-decreasing subsequence.
We will call an element u ∈ Λ(A) well-factor-ordered (wfo) if the reverse J -order, ≥ J , is a well-quasi-order on the set ↑ J u of factors of u. Similarly, we call u ∈ Λ(A) well-prefix-ordered (wpo) if ≥ R is a wqo on ↑ R u and wellsuffix-ordered (wso) if ≥ L is a wqo on ↑ L u. Recall from Lemma 3.4 that F A (A) is upward closed in Λ(A) with respect to the J -, Rand L-orders, so that the following results apply immediately to F A (A), as well.
Almeida showed [3, Theorem 2.6] that F A (A) \ A * contains maximal elements with respect to the J -ordering and that they correspond in a sense that can be made precise to uniformly recurrent words. Moreover, it is shown in [7, Proposition 3.2] that every element of F A (A) \ A * is J -below a maximal element. Notice that ω is the unique maximal element of F A (1)\N.
Proposition 8.4. Let w be a maximal element of Λ(A) \ A * in the J -order (e.g., a J -maximal element of F A (A) \ A * ). Then w is well-factor-ordered.
Proof. Let w 1 , w 2 , . . . be an infinite sequence of factors of w. We must show that w i ≥ J w j for some i < j. If w i J w j for some i < j, then we are done so we may assume that the elements of the sequence are in distinct J -classes. By maximality of w, at most one element of the sequence does not belong to A * and so, by passing to a subsequence, we may assume that w 1 , w 2 , . . . consists of finite words, necessarily distinct since they are in distinct Jclasses. By passing to a further subsequence, we may assume that (w n ) converges in Λ(A) to an element v (by compactness of Λ(A)). Moreover, since A * is discrete in Λ(A), and the sequence w 1 , w 2 , . . . is a sequence of distinct finite words, we must have that v / ∈ A * . As ≥ J is a closed relation on any compact monoid [40, Proposition 3. 1.9], v ≥ J w and hence v J w by maximality. Therefore, w 1 is a factor of v. If w 1 = ε, then w 1 ≥ J w 2 and so we may assume that w 1 = ε.
Let ϕ be the formula stating that the finite word w 1 is a factor of an A-word. Then since ϕ is true for all models of v (because w 1 is a factor of V for any ω-saturated model V in the class v by Proposition 5.9) and w n → v, we must have that, for n large enough, w n |= ϕ. Thus we can find N > 1 with w 1 ≥ J w N . This completes the proof that w is well-factor-ordered.
Our next aim is to prove (Theorem 8.6) that a substitution of well-factorordered elements into a well-factor-ordered element gives a well-factor-ordered element. The following preliminary proposition will be used in the proof.
Proposition 8.5. Any well-factor-ordered element of Λ(A) is well-suffixordered and well-prefix-ordered.
Theorem 8.6. Let f : Λ(B) → Λ(A) be a substitution. If f (b) is well-factorordered for each b ∈ B, and v ∈ Λ(B) is well-factor-ordered, then f (v) is well-factor-ordered.
Proof. Suppose that f (b) is wfo for each b ∈ B and that v ∈ Λ(B) is wfo. Let (w i ) i∈N be a sequence in ↑ J f (v). If w i = ε for some i then we are done because ε ≥ J w for any w. If there exists b ∈ B such that f (b) ≤ J w i infinitely often, then we are done immediately because f (b) is wfo. Now assume that ε does not occur in (w i ) i∈ω and that, for each b ∈ B, f (b) ≤ J w i only finitely often. Passing to a subsequence, we may assume that f (b) J w i for every i and b ∈ B. For each i, by Theorem 5 .10.1, pick Since the alphabet B is finite, passing to a further subsequence, we may assume that all the b i are equal to one and the same letter b, and that all the c i are equal to one and the same letter c. By Proposition 8.5, (↑ L f (b), ≥ L ) and (↑ R f (c), ≥ R ) are wqo. In particular, passing to a further subsequence, we get that the sequence (x i ) i∈N is nondecreasing in (↑ L f (b), ≥ L ), (y i ) i∈ω is non-decreasing in (↑ R f (c), ≥ R ), and (bz i c) i∈ω is non-decreasing in (↑ J v, ≥ J ). Thus, in this subsequence, we have in particular that . We show that w 1 ≥ J w 2 . Since bz 2 c ≤ J bz 1 c, by Lemma 6.2, there are four cases: (1) z 1 = z 2 . In this case, using that x 2 ≤ L x 1 and y 2 ≤ R y 1 .
using that x 2 ≤ L x 1 and f (c) ≤ R y 1 . Thus, w 1 ≤ J w 2 .
Theorem 8.7. Let f : Λ(B) → Λ(A) be a substitution. If f (b) is well-prefixordered for each b ∈ B, and v ∈ Λ(B) is well-prefix-ordered, then f (v) is well-prefix-ordered.
Proof. Let (w i ) i∈ω be a sequence in ↑ R f (v). Again, we may assume that w i = ε for all i ∈ ω. By Theorem 5.10.2, for each i ∈ ω, As in the proof of Theorem 8.6, we may pass to a subsequence where all of the b i are equal to one and the same b, (x i ) i∈ω is non-decreasing in (↑f (b), ≥ R ), and (z i b) i∈ω is non-decreasing in (↑v, ≥ R ). In particular, Pick α ∈ Λ(B) such that z 1 bα = z 2 b. By Proposition 6.3, pick β ∈ Λ(B) such that either (1) z 1 bβ = z 2 and βb = α, or (2) z 2 β = z 1 b and βα = b.
Corollary 8.9. The sets of well-factor-ordered, well-prefix-ordered, and well-suffix-ordered elements in Λ(A) are closed under concatenation and ωpower. In particular, interpretations of ω-terms in F A (A) are well-factorordered.
Proof. The statement about concatenation is immediate from Theorems 8.6, 8.7 and Corollary 8.8. For the statement about ω-power, notice that ω ∈ F A (1) is well-factor-ordered (and hence also well-suffix-ordered and wellprefix-ordered). The statement about interpretations of ω-terms follows by induction, because finite words are obviously well-factor-ordered.
It follows that the smallest submonoid of F A (A) containing all finite words and J -maximal elements that is closed under the ω-power consists of wellfactor-ordered elements. Also, substituting ω-terms into J -maximal elements will provide new example of well-factor-ordered elements.
We call an element u ∈ Λ(A) factor-regular if the set F (u) := ↑ J u ∩ A * of finite factors of u is a regular language. We call u prefix-regular if P (u) := ↑ R u ∩ A * is a regular language, and suffix-regular if S(u) := ↑ L u ∩ A * is a regular language. If u ∈ Λ(A) \ A * is prefix-regular, then so is any element of uΛ(A), and dually for suffix-regular elements. We call a substitution (1) If f (b) is prefix-regular, for each b ∈ B, and v is prefix-regular, then is factor-regular, prefix-regular and suffix-regular, for each b ∈ B, and v is factor-regular, then f (v) is factor-regular.
(1) Suppose f (b) is prefix-regular for each b ∈ B, and v is prefix-regular. Using Corollary 5.11 it is straightforward to verify that As the regular languages are closed under product, intersection, right quotients and images, we conclude that P (f (v)) is regular.
(3) Using Corollary 5.11 it is straightforward to verify that and so the desired result follows from closure of regular languages under boolean operations, product, left and right quotients and homomorphic image.
For a finite alphabet A and b ∈ A, let f : Λ(A ∪ {b}) → Λ(A) be the substitution erasing b and fixing A. Note that if v ∈ Λ(A) is not prefix-regular, then b ω v is prefix-regular, but f (b ω v) = v is not. Thus the assumption in Theorem 8.10 that f is non-erasing is necessary. The next corollary recovers [7, Corollary 7.6].
Remark 8.12. The minimal ideal I of F A (A) consists of those elements containing every finite word as a factor. In particular, every element of I is factor-regular. Thus if we substitute ω-terms over B into an element of the minimal ideal of F A (A), then we obtain factor-regular elements of F A (B). It is not the case that every element of the minimal ideal of F A (A) is prefixregular or suffix-regular. In fact, it is easy to see using the pumping lemma that w ∈ Λ(A) \ A * is prefix-regular if and only if w = uv ω z with u, v finite and v = ε. A dual description holds for suffix-regular elements.

Conclusion and further work
In this paper we gave a new approach to the free pro-aperiodic monoid by viewing its elements as elementary equivalence classes of pseudofinite words. This view led us to consider F A (A) as a topologically closed submonoid of the larger monoid Λ(A) consisting of elementary equivalence classes of arbitrary A-words. The model-theoretic fact that each such class contains an ω-saturated model enabled us to analyze factors in Λ(A) combinatorially. Thus, we substantiate the claim made in [26] that one may "transfer arguments from Combinatorics on Words to the profinite context": our approach using saturated models makes this idea precise.
The newly identified pro-aperiodic monoid Λ(A) poses several interesting questions for future work. In particular, it would be interesting to study the algebraic structure of Λ(A) in more detail. Here, connections with the work of Carton, Colcombet and Puppis on algebras for words over countable linear orderings [15] are to be expected.
We also plan to explore in future work how this method might be extended to other profinite monoids. In particular, one could try to analyze the absolutely free profinite monoid in this way, but this would require replacing first-order model theory by monadic second-order model theory. In this direction, we foresee connections to Shelah's seminal work [43].
In a different direction, we hope that our approach could be useful for more easily analyzing aperiodic pointlike sets and related notions, in particular because a logical approach has recently proved useful for deciding problems in the first-order quantifier alternation hierarchy [38].
Historical remark. The second-named author first began to think about using model theory to study free pro-aperiodic monoids in late 2008 and suggested to his PhD student, David Gains, at Carleton University to work on axiomatizing the theory of finite words, describing the multiplication on pseudofinite words and proving equidivisibility from this viewpoint. Unfortunately, the project did not have a chance to go far before the second-named author left Carleton. The authors recommenced the project in January 2016 and proved the results presented here. As we began to give talks on this work, we were informed by several researchers that they had also been thinking for some time about applying model theory to studying free pro-aperiodic monoids, and had some unpublished results that could be related to ours. In particular, the preprint [5] was published on arXiv in February 2017, after we published our first paper on the topic on arXiv in September 2016, which later appeared in the conference STACS 2017 [23]. The work in [5] is related to, but different from, our work here, one important difference being our use of saturated models.
Proof. The formulas ϕ (y,z) , ϕ <y and ϕ >y are defined by induction on the complexity of ϕ. We only give the definitions for ϕ (y,z) , leaving the rest of the proof to the reader. For any atomic formula ϕ, define ϕ (y,z) to be ϕ. For any Boolean combination ϕ of formulas ϕ 1 , ϕ 2 , define ϕ (y,z) to be the same Boolean combination of ϕ (y,z) 1 and ϕ (y,z) 2 . Finally, if ϕ is of the form ∃x i ψ or ∀x i ψ, define ϕ (y,z) by, respectively, ∃x i (y < x i ∧ x i < z ∧ ψ (y,z) ), or ∀x i ((y < x i ∧ x i < z) → ψ (y,z) ).
In model theory, one considers a more general version of the equivalence relations ≡ k introduced in Section 1. For every n, k ≥ 0, there is an equivalence relation ≡ n,k between models with assignments of n firstorder variables: two such models with assignments, (U, i) and (V, j), are ≡ n,k -equivalent if they satisfy the same formulas ϕ(x 1 , . . . , x n ) of quantifier depth ≤ k; notation U, i ≡ n,k V, j. In this context, the tuples i and j are called parameters. We call the models with parameters (U, i) and (V, j) elementarily equivalent if U, i ≡ n,k V, j for every k ≥ 0; notation U, i ≡ V, j.
The k-round EF-game on (U, i) and (V, j), where i ∈ U n and j ∈ V n , is played exactly as the one without parameters, but the winning condition now requires that the map defined by u p → v p and i q → j q is an isomorphism between the substructure {u p | 1 ≤ p ≤ k} ∪ {i q | 1 ≤ q ≤ n} of U and the substructure {v p | 1 ≤ p ≤ k} ∪ {j q | 1 ≤ q ≤ n} of V . In the case of A-words, the equivalence relation ≡ n,k can be reduced to the relation ≡ k holding between intervals of the models, as follows. For an n-tuple i in an A-word U , we say an open ray or interval with endpoints in i is i-minimal if it does not contain any elements from i. Proposition A.2. Let U , V be A-words, i ∈ |U | n and j ∈ |V | n . The following are equivalent: (1) U, i ≡ n,k V, j; (2) U (i p ) = V (j p ) for all 1 ≤ p ≤ n, the order < U on i coincides with the order < V on j, and every i-minimal open interval or ray in U with endpoints in i is k-equivalent to the open interval or ray in V with corresponding endpoints in j.
Proof. For (1) implies (2), we have the same letters at i p and j p because each P a (x) is a formula of quantifier depth 0, and the orders coincide because x p < x q is of quantifier depth 0. If (i p , i q ) and (j p , j q ) are corresponding minimal open intervals, and U (i p , i q ) |= ϕ for some formula ϕ of quantifier depth k, then U, i |= ϕ (xp,xq) , so, by assumption, V, j |= ϕ (xp,xq) , and hence V (j p , j q ) |= ϕ. So U (i p , i q ) ≡ k V (j p , j q ). The proof for minimal open rays is analogous.
For (2) implies (1), we describe a winning strategy for player ∃ in the kround game on (U, i) and (V, j). If ∀ plays one of the i p and j p , ∃ responds with the corresponding parameter in the other model. If ∀ plays in U , say, at a position i ′ not in i, then there is a unique i-minimal open interval or ray with endpoints in i that contains i ′ . The winning strategy for the k-round game on this i-minimal open interval or ray then gives an element for ∃ to play in the corresponding open interval or ray in V . Since the strategies for ∃ on the i-minimal open intervals and rays are winning by assumption, and the orders on the tuples i and j coincide, the resulting submodels of U and V are isomorphic.
Note that a special case of Proposition A.2 is that i k-corresponds to j if and only if U, i ≡ 1,k V, j. Also note that Proposition A.2 implies in particular that for any A-words U 1 , . . . , U m and V 1 , . . . , V m such that U i ≡ V i for i = 1, . . . , m, we have U 1 · · · · · U m ≡ k V 1 · · · · · V m , which is a special case of Proposition 3.1.

A.2.
Comparison with notions from model theory. We defined the type t U (i) of a position i in an A-word U to be ([U (<i)] ≡ , U (i), [U (>i)] ≡ ). This corresponds to what is called a complete 1-type with respect to an empty set of parameters in model theory, which is usually defined as the set of those formulas ϑ(x) such that U, i |= ϑ(x). Indeed, t U (i) contains precisely the same information as this set, as the following proposition shows. Proposition A.3. For any A-words U, V , i ∈ |U | and j ∈ |V |, the following are equivalent: (1) t U (i) = t V (j); (2) for every k ≥ 0, i k-corresponds to j; (3) U, i ≡ V, j.
By Proposition A.3, a model is weakly saturated in our sense if and only if the model realizes all the complete 1-types without parameters that are consistent with it. We now prove that what we called ω-saturated above is equivalent to the usual model-theoretic notion by the same name; indeed, in Proposition A.4 below, (1) is our definition of ω-saturated, and (2) is well-known to be equivalent to the standard model-theoretic definition of ω-saturated, cf. [33,Proposition 4.3.2].
Proposition A.4. Let U be an A-word. The following are equivalent: (1) Every closed interval in U is weakly saturated.
(2) For any n-tuple i in U and for any model with n parameters (V, j) such that U, i ≡ V, j, if j ′ ∈ |V |, then there exists i ′ ∈ |U | such that U, ii ′ ≡ V, jj ′ .