title: Languages recognised by finite semigroups, and their generalisations to objects such as trees and graphs, with an emphasis on definability in monadic second-order logic
authors: MikoĹaj BojaĹczyk
text: 
   
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
   
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
   
  
  
  
  
  
  
  
  
  
  
  
  
  
  openquestioncounter
  
  
  
  
  
  
  
  exercisecounter
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
   
   
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
   
  
   107
  
   
  
   Languages recognised by finite semigroups, and their generalisations to objects such as trees and graphs, with an emphasis on  definability in monadic second-order logic
  
  This is the guide for authors who are preparing written,
   rather than edited, books.
   ; authored books; CUP style; cambridge7A.cls.
  
  
  
  
  
  
   
  
  
  
  
  
  CHAPTER: PREFACE
  
  
  These are lecture notes on the algebraic approach to regular languages. The present version is from December 30, 2023, while the most recent version can be found here:
  
  
    https://www.mimuw.edu.pl/bojan/2019-2020/algebraic-language-theory-2020 
    
  
  
  The classical algebraic approach is for finite words; it  uses semigroups instead of automata. However, the algebraic approach can be extended to structures beyond words,  e.g. infinite words, or trees or graphs.  The purpose of this book is to describe the algebraic approach in a way that covers these extensions.
  
  
  
  PART: 
  Words
   
  
  CHAPTER: SEMIGROUPS, MONOIDS AND THEIR STRUCTURE
  
  
  In this chapter, we define semigroups and monoids, and use them to recognise languages of finite words.
  
  
  A semigroup consists of an underlying set S together  with a binary multiplication operation
  
      (a,b) ∈ S^2   ↦   ab ∈ S,
  
  which is associative in the sense that 
  
      a (bc) = (ab)c     for all a,b,c ∈ S.
  
  
   
  The definition says that the order of evaluation in a semigroup is not important, i.e. that different ways of bracketing a sequence of elements in the semigroup will yield the same result as far as  semigroup multiplication is concerned. For example,
  
      ((ab)c)(d(ef)) = ((((ab)c)d)e)f.
  
  Therefore, it makes sense to omit the brackets and write simply
  
      abcdef.
  
  This means that  semigroup multiplication  can be seen as an operation of type S^+ → S, i.e. it is defined not just on pairs of semigroups elements, but also  on finite nonempty words consisting of semigroup elements. 
  
  A semigroup homomorphism is a function between  (underlying sets of) semigroups that preserves the structure of semigroups, i.e. a  function 
  
      h: Ssemigroup→Tsemigroup
    which  is consistent with the multiplication operation in the sense that 
  
      h(a · b) = h(a)· h(b),
  
  where the semigroup multiplication on the left is in S, and the semigroup multiplication on the right is in T.  An equivalent definition of a  semigroup homomorphism, which views semigroup multiplication as defined on entire words and not just pairs of letters, says that the following diagram must commute:
  
      S^+ [r]^h^+[d]_multiplication in S   
            T^+ [d]^multiplication in T
      
            S [r]_h    T
  
  In the above, h^+ is the natural lifting of h to words, which applies h to every letter.
  
  A monoid is the special case of a  semigroup where there  is an identity element, denoted by 1 ∈ S, which satisfies
  
      1a = a= a1     for all a ∈ S.
  
  The identity element, if it exists, must be unique. This is because if there are two candidates for the identity, then multiplying them reveals the true identity. The multiplication operation in a monoid can be thought of as having type S^* → S, with  the empty word ε being mapped to 1.  A monoid homomorphism is a semigroup homomorphism that preserves the identity element. In terms of commuting diagrams, a monoid homomorphism is a function which makes the following diagram commute:
  
      S^* [r]^h^*[d]_multiplication in S   
            T^* [d]^multiplication in T
      
            S [r]_h    T
  
  Clearly there is a pattern behind the diagrams. This pattern will be explored in the second part of this book, when talking about monads.
  
  
     Here are some examples of monoids and semigroups.
      
        
    * If Σ is a set, then the set  Σ^+ of nonempty words over Σ, equipped with concatenation, is a semigroup, called the free[The reason for this name is the following universality property. The free semigroup  is generated by Σ, and it is the biggest  semigroup generated by Σ  in the following sense. For every semigroup S that is generated by Σ, there exists a (unique) surjective semigroup homomorphism h : Σ^+ → S which is the identity on the Σ generators.    ] semigroup over generators Σ.  The free monoid is the set Σ^* of possibly empty words.  
        
    * Every group is a monoid.
        
    * For every set Q, the set of all functions  Q → Q, equipped with function composition, is a monoid. The monoid identity is the identity function.
        
    * For every set Q, the set of all binary relations on Q is a monoid, when equipped with relational composition
        
      a ∘ b =  (p,q) : there is some r ∈ Q such that (p,r) ∈ a and (r,q) ∈ b.
  
        The monoid identity is the identity function. The monoid from the previous item is a sub-monoid of this one, i.e. the inclusion map is a monoid homomorphism.
        
    * Here are all semigroups of size two, up to semigroup isomorphism:
        
      (0,1, +)addition mod 2   (0,1, min)   (0,1, π_1)(a,b) ↦ a  (0,1, π_2)(a,b) ↦ b  (0,1, (a,b) ↦ 1)all multiplications are 1
  
        The first two are monoids. 
     
  
  
  
  
    
  *Compositional functions.
  Semigroup homomorphisms are closely related with functions that are compositional in the sense defined below. Let S be a semigroup, and let X be a set (without a semigroup structure). A function 
  
      h : S → X
  
  is called compositional if for every a, b ∈ S, the value h(a · b) is uniquely determined
  
  
  
  
  
  
  
  
  
  by the values h(a) and h(b).  If X has a semigroup structure, then every semigroup homomorphism S → X is a compositional function. The following lemma shows that the converse is also true for surjective functions. 
  
  Let S be a semigroup, let X be a set, and let h : S → X be a surjective compositional function. Then there exists (a unique) semigroup structure on X which makes h into a semigroup homomorphism.	
  
  
     Saying that h(a · b) is uniquely determined by h(a) and h(b), as in the definition of compositionality, means that there is a binary operation ∘ on X, which is not yet known to be associative, that satisfies
     
      h(a · b) = h(a) ∘ h(b)     for all a,b ∈ S.
  
     The semigroup structure on X uses ∘ as the semigroup operation. It remains to prove associativity of ∘.  Consider three elements of X, which can be written as h(a), h(b), h(c) thanks to the assumption on surjectivity of h. We have
     
      (h(a) ∘ h(b)) ∘ h(c) 
         (<ref>) =
         (h(ab)) ∘ h(c) 
         (<ref>) =
         h(abc).
  
     The same reasoning shows that h(a) ∘ (h(b) ∘ h(c)) is equal to h(abc), thus establishing associativity. 
  
  
  
  
    
  *Commuting diagrams. We finish this section with  an alternative description of semigroups which uses commuting diagrams. Similar descriptions will be  frequently used in this book, e.g. for generalisations of semigroups for infinite words, so we want to start using them as early as possible.  
  
  As mentioned before,  the binary multiplication operation in a semigroup S can be extended to  an  operation of type  S^+ → S. The following lemma explains, using commuting diagrams, which operations of  type  S^+ → S arise this way. 
  
  
     An operation  μ : S^+ → S arises from some semigroup operation on S if and only if the following two diagrams commute: 
     
      S [dr]^identity[d]_ view a letter as 
        a one-letter word
      
               S^+ [r]_μ    S
                    @C=4cm
               (S^+)^+ [r]^multiplication in free semigroup S^+[d]_μ^+   
                S^+ [d]^μ
       
                S^+ [r]_μ     S
  
     In the above, μ^+ stands for the coordinate-wise lifting of μ to words of words. 
  
  
  For monoids, the same lemma holds, with + replaced by *. There is no need to add an extra diagram for the monoid identity, since the monoid identity can be defined as the image under μ of the empty word ε. The axioms 
  
      1 · a = a = a · 1
  
  can then be derived as
  
      1 · a = μ(ε) ·μ(a) = μ(ε a) = μ(a) = a,
  
  with a symmetric reasoning used for a · 1. 
  
  
  
  
  
  
  
  
  
  
  
  
  
  Show a function between two monoids that is a semigroup homomoprhism, but not a monoid homomorphism.
  
  Let M be any monoid, e.g. the trivial monoid of size one, and let M' be its extension obtained by adding an extra identity element 1. The inclusion embedding 
  
      M ↪ M'
  
  is a semigroup homomorphism, but not a monoid homomorphism.
  
  
  Show that there are exponentially many semigroups of size n.   
  Take any finite set X and any  function
  
      f : X^2 →0, true.
   
  The number of choices for f is exponential in X^2. 
  We can extend f to an associative multiplication operation on the set 
  
      S = X + 0, true
  
  by defining all multiplications outside X^2 to  give value 0.  Semigroups obtained this way correspond to languages where all words have length at most 2. 
  
  
  	Show that for every  semigroup homomorphism h : Σ^+ → S, with S finite, there exists some N ∈1,2,… such that every word of length at least N can be factorised as w = w_1 w_2 w_3 where h(w_2) is  an idempotent[This exercise can be seen as the semigroup version of the pumping lemma.].
  
  
  Show that if S is a semigroup, then the same is true for the powerset semigroup, whose elements are possibly empty subsets of S, and where  multiplication is defined coordinate-wise:
  
      A · B = a · b : a ∈ A, b ∈ B    for A,B ⊆ S.
    
  
  
   Let us view semigroups as a category, where the objects are semigroups and the morphisms are semigroup homomorphisms. What are the product and co-products of this category?For the case of products, we take S to be the Cartesian product of S_1 × S_2. This is the semigroup where underlying set is the product of the underlying sets, and the semigroup operation is defined coordinate-wise. It is not hard to check that this is a semigroup, and the two projection functions 
  
      S 
            [dl]_π_1[dr]^π_2   
      
            S_1
                    S_2
  
   are semigroup homomorphisms, which satisfy the defining property of products in a category, namely:
  
      ∀∃!        S 
            [dl]_π_1[dr]^π_2@[blue][dd]_f   
      
            S_1
            @[red][dr]_ f_1        S_2
            @[red][dl]^ f_2
          T
  
  
   The co-products are more tricky.
   Here we use S to be the following semigroup. Its elements are words over the disjoint union of alphabets S_1 + S_2, equipped with concatenation,  modulo the least  equivalence relation that contains all pairs of the form
  
      w ab v  ∼ wcv    where c ∈ S_i is the multiplication ab in S_i,
  
  and w,v are possibly empty words. This equivalence relation is semigroup congruence, and therefore it makes sense to consider the quotient semigroup. One can check that the semigroup S defined this way, along with the two natural inclusions ι_1 and ι_2, satisfies the 
   defining diagram of co-products, namely
  
      ∀∃!        S 
               
      
           S_1
           [ur]^ι_1        S_2
           [ul]_sι_2
          T 
           @[blue][uu]_f@[red][ul]^ f_1@[red][ur]_ f_2
  
  
  Let Σ be an alphabet, and let 
  
      X ⊆Σ^+ ×Σ^+
  
  be a  set of words pairs. Define ∼_X to be least congruence on Σ^+ which contains all pairs from X. This is the same as the symmetric transitive  closure of 
  
      (wxv,wyv) : w,v ∈Σ^*, (x,y)  ∈ X .
  
  Show that the following problem – which is called the word problem for semigroups – is undecidable: given finite Σ, X and w,v ∈Σ^+, decide if w ∼_X v.
  
  A Turing machine is called reversible if every configuration has at most one successor and at most one predecessor configuration. The halting problem is undecidable for reversible Turing machines; this can be shown by taking any Turing machine and extending it so that it stores its computation history. For a reversible Turing machine, one can come up with a set X such that  for every words w,v that represent configurations of the machine, we have 
  
      w ∼_X v   iff  there is a computation from w to v or from v to w
  
  
  
  Define the theory of semigroups to be the set of first-order sentences, which use one ternary relation x=y· z, that are true in every semigroup. Show that the theory of semigroups is undecidable, i.e. it is undecidable if a first-order sentence is true in all semigroups.
  
  Show that the theory of finite semigroups is different from the theory of (all) semigroups, but still undecidable. 
  
  
  § RECOGNISING LANGUAGES
  
  
  In this book, we are  interested in monoids and semigroups as an alternative to finite automata for the purpose of recognising  languages[
     The semigroup approach to languages can be credited to 
     SD_1955-1956__9__A10_0
     On page 10 of this paper, which is primarily devoted to codes, remarks that semigroups can be used to recognise languages and defines the syntactic congruence (in fact, the syntactic pre-order). Apparently, the syntactic congruence dates back to 
     dubreil1941
     but I have been unable to obtain a copy of this paper. 
     These are the  early days of automata theory, and 's paper is  contemporary to 
     [Theorem 4,]moore1956gedanken
     which is the first place that I know where  minimisation of automata appears.
     
     
     
     
     
  ]. Since  languages are usually defined for possibly empty words, we use monoids and not semigroups when recognising languages.
  
  
  
     Let Σ be a finite alphabet. 
   A language L ⊆Σ^* is recognised by a monoid homomorphism 
   
      h : Σ^* → M
  
   if the membership relation  w ∈ L is determined uniquely by h(w). In
    other words, there is an accepting subset
     F ⊆ M 
     such that 
     
      w ∈ L   iff   h(w) ∈ F 
                for every w ∈Σ^*.
  
     
  
  
  We say that a language is recognised by a monoid if it is recognised by some monoid homomorphism into that monoid. 
  
  The following theorem shows that, for the purpose of  recognising languages, finite monoids and finite automata are equivalent.
  
  
  
     
     The following  conditions are equivalent for every L ⊆Σ^*:
     
        
    * L is recognised by a finite nondeterministic automaton;
        
    * L is recognised by a finite monoid.
     
  
   
  
     
        
    2 ⇒ 1 From a monoid homomorphism  one creates a deterministic automaton, whose states are elements of the monoid, the initial state is the identity, and the transition function is 
        
      (m, a) ↦ m ·(homomorphic image of a).
  
         After reading an input word w, the state of the automaton is equal to the  homomorphic image of w under the recognising homomorphism, and therefore the accepting subset for the monoid homomorphisms can be used. This automaton computes the monoid multiplication according to the choice of parentheses illustrated  in this example:
         
      (((((ab)c)d)e)f)g.
  
        
    1 ⇒ 2 Let Q be the states of a nondeterministic automaton recognising L. Define a function[This  transformation from a nondeterministic (or deterministic) finite automaton to a monoid incurs an exponential blow-up, which is unavoidable in the worst case. ]  
        
      δ : Σ^* →monoid of binary relations on Q
  
        which sends a word w to the binary relation 
        
      (p,q) ∈ Q^2 : some run over w goes from p to q.
  
        This is a monoid homomorphism. It recognises the language:  a word is in the language if and only if its image under the homomorphism  contains at least one pair of the form (initial state, accepting state).
     
  
  
  
  
  
    
  *The syntactic monoid of a language.
  Deterministic finite automata have minimisation, i.e. for every language there is a minimal deterministic automaton, which can be found inside every  other deterministic  automaton that recognises the language. The same is true for monoids, as proved in the following theorem.
  
  
   For every language[The language need not be regular, and the alphabet need not be finite.] L ⊆Σ^* there is a surjective monoid homomorphism 
   
      h : Σ^* →  M,
  
   called the syntactic homomorphism of L, which recognises it and  is minimal in the sense explained in the following quantified diagram[Here is how to read the diagram. For every red extension of the black diagram there exists a unique blue extension which makes the diagram commute. Double headed arrows denote surjective homomorphisms, which means that ∀ quantifies over surjective homomorphisms, and the same is true for ∃!.]
   
      ∀∃!    @C=2cmΣ^* 
            @->>[r]^h 
            @->>@[red][dr]_-monoid homomorphism g    
      that recognises L    M 
          N
          @[cyan]@->>[u]_ monoid homomorphism f
  
     
  
  
  
   The proof is the same as for the Myhill-Nerode theorem about minimal automata, except that the corresponding congruence is two-sided. 
     Define the syntactic congruence of L to be the equivalence relation ∼ on Σ^* which identifies two words w,w' ∈Σ^* if 
     
      uwv ∈ L   iff   uw'v ∈ L     for all u,v ∈Σ^*.
  
     Define h to be the function that maps a word to its equivalence class under syntactic congruence. It is not hard to see that h is compositional, and therefore by (the monoid version of) Lemma <ref>, one can equip the set of equivalence classes of syntactic congruences  with a monoid structure – call M the resulting monoid – which turns h into a monoid homomorphism. 
     
     It remains to show minimality of h, as expressed by the diagram in the lemma. Let then g be as in the diagram. Because g recognises the language L, we have 
     
      g (w) =  g (w')   implies   w ∼ w',
  
     which, thanks to surjectivity of g, yields some function f from N to M, which makes the diagram commute, i.e. h =  f ∘ g. Furthermore, f must be a monoid homomorphism, because 
  
  
      f ( a_1 · a_2)  
      by surjectivity of g, each a_i can be presented as g(w_i) for some  w_i
       f ( g(w_1) · g(w_2)) 
      g is a monoid homomorphism
       f ( g(w_1 w_2))  
        the diagram commutes
      
        h(w_1 w_2) 
        h is a monoid homomorphism
      
        h(w_1) · h(w_2)
        the diagram commutes
       f( g(w_1)) · f( g(w_2))     =    
       f (a_1) · f (a_2).
  
  
  
  
  
  
  
  	Show that the translation from deterministic finite automata to monoids is exponential in the worst case. 
  For n ∈1,2,…, consider the language of words in a,b^* where  the n-th letter is a. This language is recognised by a deterministic finite automaton with (n) states. To recognise the language with a monoid homomorphism, we need to remember the first n letters of a word. 
  
  
  
     Show that the translation from (left-to-right) deterministic finite automata to monoids is exponential in the worst case, even if there is a  right-to-left deterministic automaton of same  size.
  
  An example is  words of length 2n+1, over alphabet a,b, where the middle letter is a. For the same reasons as in Exercise <ref>, this language needs an exponential size monoid to be recognised. On the other hand, the language is clearly recognised by a deterministic automaton (running in either direction), with (n) states.
  
  
  
  
  Show that a language L ⊆Σ^* is recognised by a finite commutative monoid if and only if it can be defined by a finite Boolean combination of conditions of the form “letter a appears exactly n times” or “the number of appearances of letter a is congruent to ℓ modulo n”.
  
  No surprises here – these are the commutative languages, i.e. languages L ⊆Σ^* which are commutative in the sense that 
  
      w ab v ∈ L   iff   wbav ∈ L     for all w,a,b,v ∈Σ^*.
  
  There is, however, an extended description of commutative languages, which is given below. 
  
  Consider a language  L ⊆Σ^* recognised by a monoid homomorphism
  
      h : Σ^* → M,
  
  where M is a commutative monoid. 
  By commutativity of M, we have
  
      h(w) = ∏_a ∈Σ h(a)^#_a(w)    for every w ∈Σ^*,
   
  where #_a(w) ∈0,1,… is the number of appearances of letter a ∈Σ in w. For every a ∈ M the sequence
  
      a^0, a^1, a^2, …∈ M
  
  is easily seen to be ultimately periodic, which means that after cutting of a finite prefix of the sequence we get a sequence that is periodic. This in turn implies that for every a,b ∈ M, the set
  
      i ∈0,1,… : a^i = b
  
  is defined by a formula φ(i) which is a finite  Boolean combination of formulas which have one of the following forms:
  
     
    * i = k for some k ∈0,1,…; or
     
    * i ≡ k  m for some k ∈0,1,….
  
  Putting these observations together, we see that for every b ∈ M, the set
  
      w ∈Σ^* : b = ∏_a ∈Σ h(a)^#_a(w),
  
  which is equal to the inverse image h^-1(b), 
  is defined by a finite Boolean combination of formulas of one of the following forms:
  
     
    (i) #_a(w) = k for some a ∈Σ and k ∈0,1,…; or
     
    (ii) #_a(w) ≡ k  m for some a ∈Σ and  k ∈0,1,….
  
  It follows that the following conditions are equivalent for every language:
  
     
    * recognised by a finite commutative monoid;
     
    * regular and commutative as a language;
     
    * defined by a finite Boolean combination of conditions as in (i) and (ii).
  
  
  
  
  
  
  Prove that surjectivity of g is important in Theorem <ref>. 	
  
  Consider the language (aa)^* of even length words, which is recognised by the homomorphism 
  
      h : a^* →_2  = (0,1, +_mod 2).
  
  Define _2 + to be the extension of _2 with an absorbing element . Define 
  
      g : a^* →_2 +
  
  to be the same function as h, except that the co-domain is bigger. In particular, g is not surjective.  We claim that there is no monoid homomorphism f which makes the following diagram commute
  
      a^* [r]^h
         [dr]_g    _2 
         _2 + [u]_f
  
  Since  is absorbing in _2 +, then the image of f() must be an absorbing in _2, and there are no absorbing elements in _2. It follows that there is no f which makes the diagram commute. 
  
  
     Show that for every language, not necessarily regular, its syntactic homomorphism is the function
     
      w ∈Σ^*     ↦    (q ↦ qw)state transformation 
       in the sytactic automaton,
  
     where the syntactic automaton is the deterministic finite automaton from the Myhill-Nerode theorem.
  
     The syntactic congruence identifies two words w_1 and w_2 if 
  
      uw_1v ∈ L  uw_2v ∈ L     for all u,v ∈Σ^*.
  
  To prove the exercise, we will show that two words are equivalent in the above sense if and only if they induce the same state transformations in the syntactic automaton.  Clearly if the words have the same state transformations, then they are equivalent. We are left with proving the opposite, i.e. different state transformations imply non-equivalence under the syntactic congruence.
  
     Suppose that  w_1 and w_2 have different state transformations. This  means that there is some state q of the syntactic automaton  such that  
     
      q w_1  ≠   q w_2.
  
     Like any state of the syntactic automaton, q is reached from the initial state by reading some word u. Since the states q w_1 and q w_2 are different,  there must be some word u such that exactly one of the states 
  
      (q w_1)u      (q w_2)u
  
  is accepting.  Summing up, we have found two words u,v such that exactly one of the words
  
      u w_1 v      u w_2 v
  
     is in the language, thus proving that w_1 and w_2 are not equivalent under the syntactic congruence.
  
  
  
  
      
  Let  be a class of regular languages with the following closure properties:
  
      
    *  is  closed under Boolean combinations;
      
    *  is closed under inverse images of homomorphisms h : Σ^* →Γ^*;
      
    * Let L ⊆Σ^* be a language in  . For every  u,w ∈Σ^*,  contains the inverse image of L under the following operation:
      
      v ↦ uvw.
  
  
  Show that if L belongs to , then the same is true for every language recognised by its syntactic monoid.
  
  
  
  § GREEN'S RELATIONS AND THE STRUCTURE OF FINITE SEMIGROUPS
  
  
  In this section, we describe some of the structural theory of finite semigroups. 
  This theory is based on Green's relationsgreen51, which are pre-orders in a semigroup that correspond to prefixes, suffixes and infixes.
  
  
  
  
  
  
  We begin with idempotents, which are  ubiquitous in the analysis of finite semigroups.  A semigroup element e is called  idempotent if it satisfies 
  
      ee = e.
  
  
  	In a group, there is a unique idempotent, namely the group identity. There can be several idempotents, for example  all elements are idempotent in  the semigroup 
  	
      (1,…,n, max).
  
  
  One can think of idempotents as being a relaxed version of identity elements. 
  
  
  	Let S be a finite semigroup. For every a ∈ S, there is exactly one idempotent in the set
  	
      a^1,a^2,a^3, …⊆ S.
  
  
  
  	Because the semigroup is finite, the sequence a^1,a^2,… must contain a repetition, i.e. there must exist n,k ∈1,2,… such that 
  	
      a^n = a^n+k = a^n + 2k = ⋯.
  
  	After multiplying both sides of the above equation by a^nk-n we get
  	
      a^nk = a^nk+k = a^nk + 2k = ⋯,
  
  	and therefore a^nk = a^nk+nk is an idempotent. To prove uniqueness of the idempotent, suppose n_1,n_2 ∈1,2,… are powers such that  that a^n_1 and a^n_2 are idempotent. The we have 
  	
      a^n_1 = (a^n_1)^n_2because a^n_1
       is idempotent  
      	= a^n_1 n_2 =
      	  (a^n_2)^n_1 = a^n_2because a^n_2
       is idempotent
  
  
  Finiteness is important in the above lemma. For example the infinite semigroup
  
      (1,2,…,+)
  
  contains no idempotents.
  For a ∈ S, we use the name idempotent power for the element a^n, and we use the name idempotent exponent for the number n. The idempotent power is unique, but the idempotent exponent is not. It is easy to see that there is always an idempotent exponent which is at most the size of the semigroup, and idempotent exponents are closed under multiplication. Therefore, if a semigroup has n elements, then the factorial n! is an idempotent exponent for every element of  the semigroup. This motivates the following notation:  we write a^ for the idempotent power of  a. The  notation usually used in the semigroup literature is a^ω, but we will use ω for infinite words. 
  
  
  The analysis presented in the rest of this chapter will hold in any semigroup which satisfies the  conclusion of the Idempotent Power Lemma.
  
  
  
  § GREEN'S RELATIONS
  
  We now give the main definition of this chapter.
   Let a,b be elements of a semigroup S. We say that a is a prefix of b  if there exists a  solution x of
  	
      ax =b.
  
  	The solution x can be an element of the semigroup, or  empty (i.e. a=b). Likewise we define the suffix and infix relations, but with the equations
  
      xa = bsuffix    xay = binfix.
  
  In the case of the infix relation,  one or both of x and y can be empty.
  
  
  Figure <ref> illustrates  Green's relations on the example of the monoid of partial functions on a three element set.
  The prefix, suffix and infix relations are pre-orders, i.e. they are transitive and reflexive[Another description of the prefix pre-order is  that a is a prefix of b if
  
      a S^1⊇ b S^1.
  
  In the above,  S^1 is the monoid which is obtained from S by adding an identity element, unless it was already there. The sets a S^1, bS^1 are called right ideals. Because of the description in terms of inclusion of right ideals, the semigroup literature  uses the notation
  
      a ≥_ b    aS^1⊇ bS^1
  
  for the prefix relation. Likewise, a ≥_ b is used for the suffix relation, which is defined in terms of left ideals. Also, for some mysterious reason, a ≥_ b is used for the infix relation.   We avoid this notation, because it makes longer words smaller. 
  ].  They need not be anti-symmetric, for example in a group every element is a prefix (and also a suffix and infix) of every other element. We say that two elements of a semigroup are in the same prefix class if they are prefixes of each other. Likewise we define suffix classes and infix classes. 
  
  Clearly every prefix  class is contained in some infix class, because prefixes are special cases of infixes. Therefore, every infix class is partitioned into prefix classes. For the same reasons, every infix class is partitioned into suffix classes. The following lemma describes the structure of these partitions. 
  
  
  
  
  
  
  
   The following hold in every finite semigroup.
  	 
  		
    *  all distinct prefix classes in a given infix class are incomparable: 
  		
      a,b are infix equivalent, and  a is a prefix of b ⇒ a,b are prefix equivalent
  
  		
    *  if a prefix class and a suffix class are contained in the same infix class, then they have nonempty intersection;
  		
    *  all prefix classes in the same infix class have the same size.
  	
  
  Of course, by symmetry, the lemma remains true after swapping prefixes with suffixes.
  
  	 
  	
  		
    * This item says that distinct prefix classes in the same infix class are incomparable with respect to the prefix relation. This item of the Egg-box Lemma is the one that will be used most often. 
  		
  		Suppose that a,b are infix equivalent and a is a prefix of b, as witnessed by  solutions x,y,z to the equations
  		
      b= ax        a = ybz.
  
  		As usual, each of x,y,z could be empty. This can be illustrated as 
  		
      @C=2cm
      				a
      				@/^/[r]^c ↦ cx   
      				b
      				@/^/[l]^c ↦ ycz
  
  		Consider the idempotent exponent ∈1,2,…  which arises from  Idempotent Power Lemma. We have:
  		
      b follow + times the loop around a, then go to b
      
      			y^ +   a (xz)^ +  x y^ is an idempotent
      
      			y^ a (xz)^ +  x  follow  times the loop around a
      			
      			a (xz)^ x,
  
  		which establishes that b is a prefix of a, and therefore a,b are in the same prefix class. 
  		
    * We now show that prefix and suffix classes in the same infix class must intersect. Suppose that a,b are in the same infix class, as witnessed by 
  		
      a =xby.
  
  		With respect to the infix relation, by is between b and a=xby, and therefore it must be in the same infix class as both of them.
  		We have 
  		
      xb   yb is a prefix of by^by is a suffix of xby = a,
  
  		and therefore, thanks to the previous item, by is prefix equivalent to b and suffix equivalent to a. This  witnesses that the prefix class of b and the suffix class of a have nonempty intersection.
  		
    * We now show that all prefix classes  in the same infix class have the same size. Take some two prefix classes in the same infix class, given by representatives a,b. We can assume that a,b are in the same suffix class, thanks to the previous item. Let 
  		
      a = xb      b = ya
  
  		be witnesses for the fact that a,b are in the same suffix class. The following claim implies that the two prefix classes under consideration have the same size.
  		
  			The following maps are mutually inverse bijections
  				
      @C=2cmprefix class of a@/^/[r]^c ↦ yc   prefix class of b@/^/[l]^c ↦ xc
  
  		
  		
  			Suppose that c is in the prefix class of a,  as witnessed by a decomposition c=az. If we apply sequentially both maps in the statement of the claim to c, then we get 
  			
      xyc = xyaz ya=b= xbz xb=a=  az az=c= c.
  
  			This, and a symmetric argument for the case when c is in the prefix class of b, establishes that the maps in the statement of the claim are mutually inverse. It remains to justify that the images of the maps are as in the statement of the claim, i.e. the image of the top map is the prefix class of b, and the image of the bottom map is the prefix class of a.  Because the two maps are mutually inverse, and they prepend elements to their inputs, it follows that each of the maps has its image contained in the infix class of a,b. To show that the image of the top map is in the prefix class of b (a symmetric argument works for the bottom map), we observe that every element of this image is of the form yaz, and therefore it has b=ya as a prefix, but it is still in the same infix class as a,b as we have observed before, and therefore it must be prefix equivalent to b thanks to the item <ref> of the lemma. 
  		
  	
  
  
  The Egg-box Lemma establishes that each infix class has the structure of a rectangular grid (which apparently reminded hungry author  of a box of eggs), with the rows being prefix classes and the columns being suffix classes. Let us now look at the eggs in the box:  define an -class to be an intersection of some prefix class and some suffix class, both taken from some common infix class.
  By item <ref> of the Egg-box Lemma, this intersection is nonempty. The following lemma shows that all -classes in the same infix class have the same size.
  
  
  	If  a,b are in the same infix class, then  there exist possibly empty x,y such that the following is a bijection
  		
      @C=2cm-class of a@/^/[r]^c ↦ xcy   -class of b
  
  
  Consider first the special case of the lemma, when  a and b are in the same suffix class. Take the map from Claim <ref>, which maps bijectively the prefix class of a to the prefix class of b. Since this map    preserves suffix  classes, it maps bijectively the -class of a to the -class of b. By a symmetric argument, the lemma is also true when a and b are in the same prefix class. 
  	
  	For the general case, we use item <ref> of the Egg-box Lemma, which says that there must be some intermediate element that is in the same prefix class as a and in the same suffix class as b, and we can apply the previously proved special cases to go from the -class of a to the -class of the intermediate element, and then to the -class of b.
  
  
  The following lemma shows a dichotomy for an -class: either it is a group, or the  multiplying any two elements from the -class not only  falls outside the -class, but even outside  the corresponding infix class. 
  
   The following conditions are equivalent for every -class G in a finite semigroup: 
  	
  		
    *  G contains an idempotent;
  		
    *  ab is in the same infix class as a and b for some a,b ∈ G;
  		
    *  ab ∈ G for some a,b ∈ G;
  		
    * ab ∈ G for all a,b ∈ G;
  		
    * G is a group (with multiplication inherited from the semigroup).
  	
  	
  
  
  	Implications <ref> ⇒ <ref> ⇒ <ref> in the lemma are obvious, so we focus on the remaining implications.
  	
  		
    <ref>⇒<ref> Suppose that ab is in the same infix class as a and b. Since a is a prefix of ab, and the two elements are in the same infix class, item <ref> of the Egg-box Lemma implies that ab is in the prefix class of a, which is the same as the prefix class of b. For similar reasons, ab is in the same suffix class as a and b, and therefore ab ∈ G.
  		
    <ref>⇒<ref> Suppose that there exist  a,b ∈ G with ab ∈ G.   We need to show that G  contains the multiplication of every two  elements c,d ∈ G. Since c is prefix equivalent to a there is a decomposition a=xc, and for similar reasons there is a decomposition b=dy. Therefore,  cd is an infix of 
  		
      xc^ady^b  ∈ G,
  
  		 and therefore it is in the same infix class as G. By the reasoning in the previous item, cd ∈ G. 
  		
    <ref>⇒<ref> Suppose that G is closed under multiplication, i.e. it is a subsemigroup. We will show that it is a group. By the Idempotent Power Lemma, G contains some idempotent, call it e. We claim that e is an identity element in G, in particular it is unique. Indeed, let a ∈ G. Because  a and e are in the same suffix class, it follows that a can be written as xe, and therefore 
  	
      ae = xee= xe = a.
  
  	For similar reasons, ea=a, and therefore e is the unique identity element in G. The group inverse is defined as follows. Take ∈1,2,… to be the idempotent exponent which arises from the Idempotent Power Lemma. For every  a ∈ G, the power a^ is an idempotent.  Since there is only one idempotent in G, we have a^ = e. Therefore, a^-1 is a group inverse of a.  
  	
  
  
  
  Show that for every finite monoid, the infix class of the monoid identity is a group.
  Let J be the infix class of the monoid identity 1. Since 1 is prefix of every monoid element, it follows from the Egg-box Lemma that J is equal to the prefix class of 1. For the same reasons, J is equal to the suffix class of 1. Therefore J is a single -class. Since J contains an idempotent, namely 1, it must be a group by the -class Lemma.
  
  
   Consider a finite semigroup.
  	Show that an infix class contains an idempotent if and only if it is regular, which means that there exist a,b in the infix class such that ab is also in the infix class.
  
  If an infix class contains an idempotent, then it clearly contains elements a,b such that ab is in the infix class. For the converse implication, suppose that a,b and ab are in the same infix class J. It follows that each of a,b can be decomposed as a multiplication of two elements from J. By iterating this procedure, we see that a can be decomposed as a multiplication of n elements from J, for every n ∈1,2,…. Thanks Exercise <ref>, a multiplication of n elements from J must contain an idempotent infix.
  
  
  
  
  
  	Show that if G_1,G_2 are two  -classes in the same infix class of a finite semigroup, and they are both groups, then they are isomorphic as groups[Let us combine Exercises <ref> and <ref>. By  Exercises (<ref>) and the -class lemma, an infix class is regular if and only if it  contains an  -class which is a   group.  By Exercise (<ref>), the corresponding group is unique up to isomorphism. This group is called the group of the regular infix class. ].
  
  Suppose that G_1 and G_2 are groups in the same infix class. Let e_1, e_2 be the identities in the groups G_1,G_2.  
  From Claim <ref> it follows that there exist x_1,x_2,y_1,y_2 such that 
  
      @C=2cm
      		G_1
      		@/^/[r]^g ↦ x_1gy_1   
      		G_2
      		@/^/[l]^g ↦ x_2 g y_2
  
  are mutually inverse bijections. By replacing 
  
      x_1 e_1new x_1  e_1 x_2 new x_2  e_1 y_1new y_1  y_2 e_1new y_2,
  
  we still get mutually inverse bijections between G_1 and G_2. Summing up, we can assume without loss of generality that x_1,y_2 end with e_1, while x_2,y_1 begin with e_1.
  
  The element y_1x_1 begins and ends with e_1, and it is also an infix of e_2 (and therefore also of e_1) thanks to 
  
      e_2 = e_2 e_2 = x_1x_2e_2y_2y_1_e_2x_1x_2e_2y_2y_1_e_2.
  
  	It follows from the Egg-box Lemma that y_1 x_1 is both in the prefix class and suffix class of e_1, which means that y_1 x_1 ∈ G_1. Since G_1 is a group, there must be some a,b ∈ G_1 such that
  	
      a y_1 x_1 a = e_1
  
  	Let y_1 x_1  be the group inverse of y_1 x_1, in the group G_1. Define α :G_1 → G_2 to be the composition of the following two functions 
  
      @C=2cm
      		G_1 [r]^g ↦   g y_1 x_1   
      		G_1 [r]^g ↦ x_1 g y_1    
      		G_2.
  
  The first function is a permutation of G_1, while the second function is a bijection of G_1 and G_2. It follows that α is a bijection. We now claim that α is a homomorphism:
  
      α(gh) = x_1  gh y_1 x_1 y_1 = 
      x_1 g y_1 x_1 y_1^α(g)
      x_1 g 
      y_1 x_1 y_1 x_1_e_1y_1 x_1 y_1
       x_1 h y_1 x_1 y_1 ^α(h) = 
      α(g) α(h).
  
  Summing up, α is a bijective semigroup homomorphism between the groups G_1 and G_2. It follows that these groups are isomorphic as groups, because a bijective semigroup homomorphism also preserves the group structure. 
  
  
  
   We say that semigroup is prefix trivial if its prefix classes are singletons. Show that a finite semigroup  S is prefix trivial if and only if it satisfies the identity 
  
      (xy)^ = (xy)^ x     for all x,y ∈ S.
  
  
  
  
  	Define the syntactic semigroup of a language to be the subset of the  syntactic monoid which is the image of the nonempty words under the syntactic homomorphism. The syntactic semigroup may be equal to the syntactic monoid. 	
  We say that a language L ⊆Σ^* is definite if it is a finite Boolean combination of languages of the form w Σ^*, for w ∈Σ^*. Show that a language is definite if and only if its syntactic semigroup S satisfies the identity
  
      x^ = x^ y     for all x,y ∈ S.
  
  
  
  Show two regular languages such that one is definite and the other is not, but both have isomorphic syntactic monoids.
  
  Consider semigroups  S which satisfy the following property: (*) that there is an infix class J ⊆ S such that every a ∈ S is an infix of J, or an absorbing zero element. Show that every finite semigroup is sub-semigroup of a product of finite semigroups that satisfy (*).
  
  
  Let  S be a finite semigroup, and let  a ∈ S. 
       Define  S_a to be the set obtained from S by removing all elements that are not infixes of a, and adding a fresh 0 element.  We can view this a semigroup, with multiplication defined by 
  
      w ∈ (S_a)^  ↦  
          0    if w contains at least one letter 0
      multiplication in S   otherwise.
  
  This semigroup satisfies condition (*). There is also a natural homomorphism h_a : S → S_a, which is the identity on infixes of a, and maps the remaining elements to 0. By taking combining these homomorphisms, we get  a 
  homomorphism 
  
      S →∏_a ∈ S S_a,
  
  which is injective, because each h_a is injective on infixes of a. 
   It follows that S is (isomorphic) to a sub-algebra of the product of the -semigroups S_a. 
  
  
  Show that every finite semigroup satisfies 
  
      ∀ x_1 ∀ x_2  ∃ y_1  ∃ y_2  z_1 = z_1 z_1 = z_1 z_2  z_2 = z_2 z_2 = z_2 z_1where z_i = x_i y_i,
  
  where quantifiers range over elements of the finite semigroup.
  
  
  
  
  
  Show that the following problem is decidable:
  
  	
    * Input. Two disjoint sets of variables
  	
      X = x_1,…,x_n     Y = y_1,…,y_m
  
  	and two words w,w' ∈ (X ∪ Y)^+.
  	
    * Question. Is the following true in all finite semigroups:
  	
      ∀ x_1  ⋯ ∀ x_n  ∃ y_1  ⋯∃ y_m  w = w'same multiplication
  
  
   
  
  
  
   
  
  § THE FACTORISATION FOREST THEOREM
  
  
  In this section, we show how the  multiplication of a long sequence of elements in a semigroup can be organised as a tree, so that in each node of the tree the multiplication is very simple. The most natural way to do this is to have binary tree, as in the following example, which uses the two semigroup 0,1 with addition modulo 2:
  25
  We use the name factorisation tree for structures as in the above picture. More formally, a  factorisation tree over a semigroup S is a tree, where nodes are labelled by semigroup elements, such that every node is either a leaf, or is labelled by  the semigroup multiplication of the labels of its children.  Since the semigroup in question need not be commutative, the children in a tree are ordered, i.e. there is a first child, second child, etc. 
  
  A binary factorisation tree is one where every node has zero or two children. For every word in S^+, one can find a corresponding binary factorisation tree (i.e. one where the word is obtained by reading the leaves left-to-right) whose height (i.e. the maximal number of edges on a root-to-leaf path) is logarithmic in the length of the word. 
  Binary factorisation trees are a natural data structure for several problems about regular languages. 
  
  
      Fix  a regular language L ⊆Σ^*. Consider the following dynamic problem. We begin with some word  in Σ^*. We want to build a data structure that handles efficiently the following updates and queries: 
      
          
    Query. Is the current word in L?
          
    Update. Change the label of position i to a ∈Σ.
      
      To solve this problem, as the data structure we can use a binary factorisation  tree with respect to some finite semigroup that recognises the language. If we assume that the  language is fixed and not part of the input, then the queries are processed in constant time, by checking if the root label of the factorisation tree belongs to the accepting set. The updates are processed in time proportional to the height of the factorisation tree, by updating all of the nodes on the path from the updated position to the root, as in the following picture:
      26
      If the factorisation tree is chosen to be balanced, then the updates are processed in logarithmic time. 
  
  
  
      Fix  a regular language L ⊆Σ^*. Consider the following dynamic problem. We begin with some word  in Σ^*. We want to build a data structure that handles efficiently the following  queries (there are no updates): 
      
          
    Query.  Given positions i ≤ j, does L contain the infix from i to j?
      
      Of course, one obvious solution is to pre-compute in quadratic time a table of answers to all possible queries. If we want to solve the problem with linear time pre-computation, then we can use  a binary factorisation  tree, with respect to some semigroup recognising the language. Suppose that the tree has height k. Each node of the factorisation tree corresponds to an infix of the underlying word.  The infix from i to j can be partitioned into at most 2k intervals,  each of which corresponds to a node of the tree, as in the following picture:
      27
      Therefore, the queries can be processed in time proportional to the height of the tree, which can be assumed to be logarithmic in the length of the underlying word.  
  
  
  In this section, we show a data structure which will allow constant time query processing in the problem from Example <ref>. We will also use a variant of factorisation trees, except that non-binary nodes will need to be used. The problem in Example <ref> cannot be solved in constant time[Lower bounds for this problem can be seen in[Fig. 1]frandsenMiltersenSkym97].
  
  
  
  § SIMON TREES
  
  A Simon tree is a factorisation tree which allows nodes of degree higher than 2, but these nodes must have idempotent children.  The data structure is named after Imre Simon, who introduced it[Under the name Ramseyan factorisation forests, in [69]simonFactorizationForestsFinite1990].
  
   
    Define a Simon tree (for a given semigroup) to be a factorisation tree  where every non-leaf node has one (or both) of the following types:
      
          
    binary: there are  two children; or
          
    idempotent: all children have the same label, which is an idempotent.
      
      
  
      Here is a picture of a Simon tree for the semigroup 0,1 with addition modulo 2, with idempotent nodes drawn in red:
      2
      
  
  The main result about Simon trees is that their height can be bounded by a constant that depends only on the semigroup, and not the underlying word. 
  
       Let S be a  finite semigroup. Every word in S^+ admits a Simon tree of height[
          The first version of this theorem was proved  in <cit.>, with a bound of  9|S|. The optimal bound is 3|S|, which was shown in 
          [Theorem 1]kufleitner2008
           The  proof here  is based on Kufleitner, with some optimisations removed.
      ]  <5|S|.
  
  
  The rest of this chapter is devoted to proving the theorem. 
  
  
    
  *Groups.
  We begin with  the special case of groups.
  
      Let G be a finite group. Every word in G^+ has a Simon tree of height  <3|G|.
  
  
  Define the prefix set of a word w ∈ G^+ to be the set of group elements that can be obtained by  multiplying  some nonempty prefix of w. 
  By induction on the size of the prefix set, we show  that every w ∈ G^+ has a Simon tree of height strictly less than 3 times the size of the prefix set. Since the prefix set has maximal size |G|, this proves the lemma.
  
  
  The induction base is when the prefix set is a singleton g. This means that the first letter is g, and every other letter h  satisfies gh=g. In a group, only the group identity h=1 can satisfy gh=g,  and therefore h is the group identity. In other words, if the prefix set is g, then the word is of the form
  
      g 1  ⋯ 1a certain number of times.
  
   Such a word admits a Simon tree as in the following picture:
  28
  The height of this tree is 2, which is strictly less than three times the size of the prefix set. 
  
  To prove the induction step, we show that every w ∈ G^+  admits a Simon tree, whose height is at most 3 plus the size from the induction assumption.
   Choose some g in the prefix set of w. Decompose w into factors as
      
      w = w_1 w_2 ⋯ w_n-1nonempty
       factorsw_ncould
       be empty
  
      by cutting along all prefixes that multiply to g. For the same reasons as in the induction base, every factor w_i with 1 < i < n  yields the group identity under multiplication.
      
      
          The induction assumption applies to all of w_1,…,w_n.
      
       
          For the first factor w_1,  the induction assumption applies, because its prefix set omits g. 
          For the remaining blocks, we have a similar situation, namely
          
      g ·(prefix set of w_i)⊆(prefix set of w) -  g     for i ∈2,3,…,n,
  
          where the left side of the inclusion is the image of the prefix set under the operation  x ↦ gx. Since this operation is a permutation of the group, it follows that the left size of the inclusion has smaller size than the prefix set of w, and therefore the induction assumption applies.             
      
      By the above claim, we can apply the induction assumption to compute Simon trees t_1,…,t_n for the factors w_1,…,w_n. To get a Simon tree for the whole word, we join  these trees as follows:
      8
      The gray nodes are binary, and the red node is idempotent because every w_i with 1 < i <n  evaluates to the group identity.
  
  
  
  
    
  *Smooth words. In the next step, we prove the theorem for words where all infixes have multiplication in the  same infix class. 
  We say that a word w ∈ S^+ is smooth if every  nonempty infix multiplies to  the same infix class. The following lemma constructs Simon trees for smooth words.
   If a word is smooth, and the corresponding infix class is J ⊆ S, then it has a  Simon tree of height <4|J|.
  
  
      
      
      
      
      
      Define a cut in a word to be the space between two consecutive letters; in other words this is a decomposition of the word into a nonempty prefix and a nonempty suffix. 
      For a cut, define its prefix and suffix classes as in the following picture:
      7
      For every cut,  both the prefix and suffix classes are contained in J, and therefore they have nonempty intersection thanks to item <ref> of the Egg-box Lemma. This nonempty intersection is an -class, which is defined to be the colour of the cut.
      The following claim gives the crucial property of cuts and their colours.  
      
      
          If  two cuts have the same colour H, then the infix between returns an element of  H under multiplication.
      
      
          Here is a picture of the situation:
          6
          The infix begins with a letter from the prefix class containing H.  Since the infix is still in the infix class J, by assumption on smoothness, it follows from item <ref> of the Egg-box Lemma that the result of multiplying   the infix is in the prefix class of H. For the same reason, the result is also in the suffix class of H. Therefore, it is in H.
      
      
      
      Define the colour set of a word to be the set of colours of its cuts; this is a subset of the -classes in J. Thanks to Lemma <ref>, all -classes contained in J have the same size, and therefore it makes sense to talk about  the -class size in J, without specifying which -class is concerned. 
      
      
          Every J-smooth word has a Simon tree of height at most 
          
      |colour set of w| · (3 ·-class size + 1).
  
      
  
      Since the number of possible colours is the number of -classes, the maximal height that can arise from the claim is
      
      3 · |J| + (maximal size of colour set) < 4|J|,
  
  which proves the lemma.
      It remains to prove the claim.
      
      
      Induction on the size of the colour set. The induction base is when the colour set is empty. In this case the word has no cuts, and therefore it is a single letter, which is a Simon tree of height zero.
  
      Consider the induction step. Let w be a smooth word. To prove the induction step, we will find a Simon tree whose height is at most the height from the induction assumption, plus
      
      3 ·(-class size) +1.
   Choose some colour in the colour set of w, which is an -class H. Cut  the word w along all cuts with colour H, yielding a decomposition 
      
      w = w_1  ⋯ w_n.
  
      None of the words w_1,…,w_n contain a cut with colour H, so the induction assumption can be applied to yield corresponding Simon trees t_1,…,t_n. 
  
      If n ≤ 3, then  the Simon trees from the induction assumption  can be combined  using  binary nodes, increasing the height by at most 2, and thus staying within the bounds of the claim. 
      
      Suppose now that n ≥ 4.  
      By Claim <ref>, multiplying an infix between any two cuts of colour H returns a value in H. In particular, all  w_2,…,w_n-1 yield results in H under multiplication, and the same is true for w_2w_3. It follows that H contains at least one multiplication of two elements from H, and therefore H is a group thanks to item <ref> of the -class Lemma. Therefore,  we can apply the group case from Lemma <ref> to join the trees t_2,…,t_n-1. The final Simon tree looks like this:
      10
  
  
  
  
  
    
  *General case.
  We now complete the proof of the Factorisation Forest Theorem. The proof is by induction on the infix height of the semigroup, which is defined to be the longest chain that is strictly increasing in the infix ordering.  If the infix height is one, then the semigroup is a single infix class, and we can apply Lemma <ref> since all words in S^+ are smooth. For the induction step, suppose that S has infix height at least two, and let T ⊆ S be the elements which have a proper infix. It is not hard to see that T is a subsemigroup, and its induction parameter is smaller. 
  
   Consider a  word w ∈ S^+. As in Lemma <ref>, define a cut to be a space between two letters. We say that a cut is smooth if the letters preceding and following the cut give a two-letter word that is smooth.  
  
  
      A word in S^+ is smooth if and only if all of its cuts are smooth.
  
  
      Clearly if a word is smooth, then all of its cuts must be smooth.  We prove the converse implication by induction on the length of the word. Words of length one or two are vacuously smooth. For the induction step, consider a word w  ∈ S^+  with all cuts being smooth.  Since all cuts are smooth, all letters are in the same infix class. We will show that w is also in this infix class. Decompose the word as w = vab where  a,b ∈ S are the last two letters. By induction assumption, va is smooth. Since the last cut is smooth,  a and ab are in the same infix class, and therefore they are in the same prefix class by the Egg-box Lemma. This means that there is some x such that  abx = a. We have
      
      va = vabx = wx
  
  which establishes that w is in the same infix class as va, and therefore in the same infix class as all the letters in w. 
  
  
  Take a word w ∈ S^+, and cut it along all cuts which are not smooth, yielding a factorisation
  
      w = w_1 ⋯ w_n.
  
  By Claim <ref>, all of the words w_1,…,w_n are smooth, and therefore Lemma <ref> can be applied to construct corresponding Simon trees of height strictly smaller than
  
      4 ·(maximal size of an infix class in S-T).
  
  Using binary nodes, group these trees into pairs, as in the following picture:
  29
  Each pair corresponds to a word with a non-smooth cut, and therefore multiplying each pair yields a result in T. Therefore, we can combine the paired trees into a single tree, using the induction assumption on a smaller semigroup. The resulting height is the height from the induction assumption on T, plus at most
  
      1 + 4 ·(maximal size of an infix class in S-T) < 5|S-T|,
  
      thus proving the induction step. 
  
  
  
  
      Show that for  every semigroup homomorphism
      
      h : Σ^+ → S     with S finite
  
      there is some k ∈1,2,… such that for every n ∈3,4,…, every word of length bigger than n^k can be decomposed as 
      
      w_0 w_1 ⋯ w_n w_n+1
  
      such that all of the words w_1,…,w_n are mapped by h to the same idempotent. 
  
    The number k is the bound on the height Simon trees for S, i.e. at most 5|S|. If a word w has length more than n^k, and it has a Simon tree of height k, then some node in that tree must have degree at least n. Since n ≥ 3, that node is an idempotent node, which gives the factorisation in the statement of the exercise.   
  
  Show optimality for the previous exercise, in the following sense. Show that for every k ∈1,2,… there is some semigroup homomorphism
  
      h : Σ^+ → S     with S finite
  
      such that for every  n ∈1,2,… there is a word of length at least n^k which does not admit a factorisation w_0 ⋯ w_n+1 where all of w_1,…,w_n are mapped by h to the same idempotent. 
  
  
      Consider the semigroup homomorphism
      
      h : 1,…,k^+ → (1,…,k, max)
  
      which maps a word to its maximum.
      For n ∈1,2,…, define a word w_i,n by induction on i ∈0,1,…,k as follows:
      
      w_i,n = ε   for i=0
      
                  w_i,n (i w_i,n)^n-1   for i ∈1,…,k.
  
      When going from i to i+1, the length of the word increases at least n times, and therefore w_k,n has length bigger than n^k. Nevertheless, there is no infix v of w_k,n which is a concatenation of n idempotents. To see this, consider the maximal letter i ∈1,…,k that appears in the infix v. By construction of the words, this letter appears <n times. If v is a concatenation of idempotents, then each of these idempotents must contain i, and therefore there are <n such idempotents.
  
  
  
  Let  h : Σ^* → M be a monoid homomorphism. Consider a regular expression over Σ, which does not use Kleene star L^* but only Kleene plus L^+.  Such a  regular expression is called  h-typed if every subexpression has singleton image under h, and furthermore subexpressions with Kleene plus have idempotent image.  Show that every language recognised by h is defined by finite union of h-typed expressions.
   
  
  CHAPTER: LOGICS ON FINITE WORDS, AND THE CORRESPONDING MONOIDS
  
  
  In this chapter, we show how structural properties of a monoid correspond to  the logical power needed to define languages recognised by this monoid. We consider two kinds of logic: monadic second-order logic and its fragments (notably first-order logic ), as well as linear temporal logic and its fragments. Here is a map of the results from this chapter, with horizontal  arrows being equivalences, and the vertical arrows being strict inclusions. 
  
  
  
      Section <ref>   finite
       monoids[r]    definable
       in [l]
        
      Section <ref>   [u]
        aperiodic
      finite
       monoids[r]    definable
       in [l] [r]    definable
       in [l] 
        
      Section <ref>   [u]
        [r]    definable
       in  with
       two variables[l] [r]    definable
       in [l] 
        
      Section <ref>   [u]
        suffix trivial
      finite
       monoids[rr]       definable
       in [ll]
        
      Section <ref>   [u]
        infix trivial
      finite
       monoids[r]    Boolean
       combinations 
       of ∃^*-[l]
  
  
  
  
  
  
  
  § ALL MONOIDS AND MONADIC SECOND-ORDER LOGIC
  
  
  We begin with  monadic second-order logic (), which is the 
   logic that captures exactly the class of  regular languages.
  
  
  
    
  *Logic on words. We assume that the reader is familiar with the basic notions of logic, such as formula, model, quantifier or free variable. The following description is meant to fix notation. We use the word  vocabulary to denote a set of relation names, each one with associated arity in 1,2,….   A model over a vocabulary consists of an underlying set (also called the universe of the model), together with an interpretation, which maps each relation name from the vocabulary to a relation over the universe of same arity. We allow the universe to be empty.  For example, a directed graph is the same thing as a model where the universe is the vertices and the  vocabulary  has one binary relation E(x,y) that represents the edge relation.
  
  To express properties of models, we use  first-order logic and .  Formulas of first-order logic over a given vocabulary are constructed as follows:
  
      ∀ x   ∃ xquantification over
       elements of the universe    φψ  φψ  φBoolean operations    R(x_1,…,x_n)an n-ary relation name from
       the vocabulary applied 
        to a tuple of variables    x=yequality.
  
  We  use the  notation
  
      𝔸, a_1,…,a_n  φ(x_1,…,x_n)
  
  to say that formula φ is true in the model 𝔸, assuming that  free variable x_i is mapped to a_i ∈𝔸.
  A sentence is a formula without free variables.
  
  
  Apart from first-order logic, we also use monadic second-order logic ; in fact is the central logic for this book.
  The logic extends first-order logic by allowing quantification over subsets of the universe (in other words, monadic relations over the universe, hence the name). The syntax of the logic has   two kinds of variables: lower case variables x,y,z,… describe elements of the universe as in first-order logic, while upper case variables X,Y,Z,… describe subsets of the universe. Apart from the syntactic constructions of first-order logic, also allows:
  
      ∀ X   ∃ Yquantification over
       subsets of the universe        x ∈ Xmembership.
  
  We do not use more powerful logics (e.g. full second-order logic, which can also quantify over binary relations, ternary relations, etc.). This is because more powerful logic will not be subject to compositionality methods that are discussed in this book.
  
  The following definition associates to each word a corresponding model.  With this  correspondence, we can use   logic to define properties of words.  
  
  
    For a word w ∈Σ^*, define its ordered model as follows. The universe is the set of  positions in the word, and it is equipped with the following relations: 
  
      x ≤ yposition x
       is before 
       position y    a(x)position x
       has label a_a ∈Σ.
  
    For a sentence φ of over the vocabulary used in the ordered model (this vocabulary depends only on the alphabet Σ), we  define  its language to be
     
      w ∈Σ^* : the ordered model of w satisfies φ.
  
     A language is called definable if it is of this form. If φ is in first-order logic, i.e. it does not use set quantification, then the language is called first-order definable.
  
  
  
    The language a^*b c^* ⊆a,b,c^* is first-order definable, as witnessed by the sentence:
    
      ∃ xthere is a 
       position  b(x)which has  
        label b∀ y  y < x^y ≤ x  x ≠ y⇒ a(y)and every earlier position 
        
       has label a∀ y  y > x ⇒ c(y).and every later position 
        
       has label b
  
  
   The language (aa)^*a ⊆ a^* of words of odd length is definable, as witnessed by the sentence:
    
      ∃ X there is a 
       set of
       positions  ∀ x  first(x)^∀ y  y ≥ xlast(x)^∀ y  y ≤ x⇒ x ∈ Xwhich contains the first and last positions,∀ x ∀ y  x = y+1^
             x < y 
      ∀ z  z ≤ x  y ≤ z⇒ (x ∈ X  y ∉X)and contains every second position.
  
      As we will see  in Section <ref>, this language is  not first-order definable.
  
  
  One could imagine other ways of describing a word via a model, e.g. a successor model where x ≤ y is replaced by a successor relation x+1=y. The successor relation can be defined in first-order logic in terms of order, but the converse is not true. Indeed,  there are languages that are first-order definable in the ordered model but are not first-order definable  in the successor model, see Exercise <ref>. 
  For the logic , there is no difference between successor and order, since the order can be defined in terms of successor using the logic as follows
  
      x ≤ y   iff  ∀ X 
      (x ∈ X  (∀ y  ∀ z y ∈ X  y+1=z ⇒ z ∈ X) )X contains x and is closed under successors⇒ y ∈ X.
   
  
  
  We now present the seminal Theorem, which says that  describes exactly the regular languages. 
  
    A language L ⊆Σ^* is definable  if and only if it is regular[
      This result was proved, independently, in the following papers:
       [Theorems 1 and 2]trakthenbrot1958 
       [Theorems 1 and 2]Buchi60
       [Theorem 5.3]Elgot61
       ].
  
  This result is seminal for   two reasons.
  
  The first reason is that it motivates the  search  for other correspondences 
  
      machine model  ∼  logic,
  
  which can concern either  restrictions or generalisations of the regular languages. In the case of restrictions, an important example is first-order logic; this restriction and others  will be described later in this chapter. In this book, we do not study the generalisations; we are only interested in regular languages. Nevertheless, it is worth mentioning Fagin's Theorem, which says that  np  describes exactly the languages definable  in existential second-order logic[Theorem 6]Fagin74.   
  
  The  theorem is also seminal because it generalises well to structures beyond finite words. For example, there are obvious notions of definable languages for: infinite words, finite trees, infinite trees, graphs, etc. It therefore makes sense to search for notions of regularity – e.g. based on generalisations of semigroups – which have the same expressive power as . This line of research will also be followed in this book.
  
  The rest of Section <ref> proves the Theorem.
  
  The easy part is  that every regular language is definable. Using the same idea as for the parity language in Example <ref>, the existence of a run of  nondeterministic finite automaton can be formalised in . If the automaton has n states, then the formula looks like this:
  
      ∃ X_1 ∃ X_2  ⋯∃ X_nexistential set quantification    “the sets X_1,…,X_n
       describe an accepting run”.first-order formula
  
  A corollary is that if we take any definable language, turn it into an automaton using the hard implication, and come back to using the easy implication, then we get an  sentence of the form described above.
  
  
  We now turn to the hard part, which says that every definable language is regular. This implication is proved in the rest of Section <ref>. The proof is so generic that it will be reused multiple times in future chapters, for structures such as infinite words, trees or graphs.
  
  For the definition of regularity, we use finite monoids. In other words, we will show that every definable language is  recognised by a finite monoid. 
  The idea is to construct the finite monoid by induction on formula size. In the induction, we also construct monoids for formulas with free variables, so we begin by dealing with those. 
  
  
    For an formula 
    
      φ(X_1,…,X_nall free variables are set variables)
  
    which uses the vocabulary of the ordered model for words over alphabet Σ, define its language  to be the set of words w over alphabet Σ×0,1^n such that 
    
      π_Σ(w) φ(X_1,…,X_n),
  
    where π_Σ is the projection of w onto the Σ coordinate, and X_i is the set of positions whose label has value 1 on the i-th bit of the bit vector from 0,1^n.
  
  If the formula φ in the above definition has no free variables, then the above notion of language coincides with Definition <ref>. Therefore, the hard part of the Theorem will follow immediately from Lemma <ref> below.
  
    If φ(X_1,…,X_n) is a formula of (where all free variables are set variables), then its language is recognised by a finite monoid.
  
  
    Before proving the lemma, we observe that first-order variables can be eliminated from , and therefore we can assume that in  φ and all of its sub-formulas, all free variables are set variables.  Suppose that we extend  with the following predicates that express properties of sets
    
      X ⊆ Yset inclusion        X ≤ Yx ≤ y holds for 
       every x ∈ X and 
       every y ∈ Y        X ⊆ aa(x) holds for 
       every x ∈ X.
  
    The above predicates are second-order predicates in the sense that they express properties of sets; in contrast to the first-order predicates x ≤ y and a(x) which express properties of elements.
    Using the second-order predicates, we can eliminate the first-order variables: instead of quantifying over a position x, we can quantify over a set of positions X, and then say that this set is a singleton:
    
      X ≠∅X is nonempty  ∀ Y  Y ⊆ X ⇒ Y = ∅ Y =Xand every proper subset of X is empty.
  
    Once elements are represented as singleton sets, the first-order predicates x ≤ y and a(x) can be simulated using  the second-order predicates from <ref>.
  
    Using the transformation described above, from now on we assume that has only set variables, and it uses the second-order predicates from <ref>. For such formulas, we prove the lemma by induction on formula size. 
  
    
      
    * Induction base. In the induction base, we need to show that for every atomic formula as in (<ref>), its language is recognised by a finite monoid. Consider for example the formula X ⊆ a. The language of this formula consists of words over alphabet Σ× 2 where  every position has a label that satisfies the following implication:
  
      second coordinate is 1  ⇒  first coordinate is a.
  
  This language is recognised by the homomorphism into the monoid 
  
      (0,1, min)
  
  which maps letters that satisfy the implication to 1 and other letters to 0.  Similar constructions can be done for the remaining predicates, in the case of X ≤ Y the monoid is not going to be commutative.
  
    * Boolean combinations. For negation, the language of 
  
      φ(X_1,…,X_n)
  
  is recognised by the same homomorphism as the language of φ(X_1,…,X_n), only the accepting set needs to be complemented. For conjunction
  
      φ_1(X_1,…,X_n) φ_2(X_1,…,X_n),
  
    one uses a product homomorphism 
    
      (h_1,h_2) : (Σ× 2^n)^* → M_1 × M_2   where h_i : (Σ× 2^n)^* → M_i recognises φ_i,
  
    with the accepting set consisting of pairs that are accepting on both coordinates. Disjunction  reduces to conjunction and negation using De Morgan's Laws.
    
    * Set quantification. By De Morgan's Laws, it is enough to consider existential set quantification
    
      ∃ X_n  φ(X_1,…,X_n)
  
    The language of the quantified formula uses alphabet Σ× 2^n-1. Let 
    
      h : (Σ× 2^n)^* → M
  
    be a homomorphism that recognises the language of the formula φ(X_1,…,X_n), which is obtained by induction assumption. To recognise the quantified formula, we  use a powerset construction. Define
    
      π : (Σ× 2^n)^* → (Σ× 2^n-1)^*
  
      to be the letter-to-letter homomorphism which removes the last bit from every input position, and define 
    
      H : (Σ× 2^n-1)^* → M      
        H(w) =  h(v) : π(v)=w.
  
    It is not hard to see that the function H is a homomorphism, with the monoid structure on the powerset 
  M defined by 
  
      A · B = a · b : a ∈ A, b ∈ B    for A,B ⊆ M.
  
  The powerset construction clearly preserves finiteness, although at the cost of an exponential blow up. The accepting set consists of those subsets of M which have at least one accepting element. 
    
  
  The construction in the above lemma is effective, which means that given a sentence of , we can compute in finite time a recognising monoid homomorphism with an accepting set. Therefore, it is decidable if a sentence of is true in at least one finite word: check if the image of the monoid homomorphism contains at least one accepting element.  
  
  As mentioned before, 
  the proof of the “hard” implication in the  Theorem is very generic and will work without substantial changes in other settings, such as infinite words, trees or graphs. The “easy part” will become hard part in some generalisations – e.g. for some kinds of infinite words or for graphs – because these generalisations lack a suitable automaton model.
  
  
  
  
   Define _2 to be the monoid with elements a,b,1 and multiplication
  
      xy = 
          y    if x=1
      
          x    otherwise.
  
  Show that every finite monoid can be obtained from  _2 by applying Cartesian products, quotients (under semigroup congruences), sub-semigroups, and the powerset construction from Exercise <ref>.
  
  
  For an alphabet Σ, consider the model where the universe is the set Σ^* of all finite words, and which is equipped with the following relations: 
  
      x  is a prefix of ybinary relation    the last letter of x is a ∈Σone unary relation for each a ∈Σ
  
  Show that a language L ⊆Σ^* is regular if and only if there is a first-order formula φ(x) over the above vocabulary such that L is exactly the words that satisfy φ(x) in the above structure. 
  
  
   What happens if the prefix relation in Exercise <ref> is replaced by the infix relation?Then we can describe accepting computations of Turing machines. 
  
  
  Consider the fragment of second-order logic where one can quantify over: elements, unary relations, and binary relations. (This fragment is expressively complete.) Define ≡_k to be the equivalence on Σ^* which identifies two words if they a satisfy the same sentences from the above fragment of second-order logic, up to quantifier rank k. Show that this equivalence relation has finite index, but it is not a semigroup congruence.
  
  In the proof of the Theorem, there was an exponential blowup incorred by every set set quantifier. Show that this is optimal, i.e. for every n there is an formula with (n) set quantifiers such that the smallest model of this formula is a word that has length which is a tower of n exponentials.   
  
  
  
  
  § APERIODIC SEMIGROUPS AND  FIRST-ORDER LOGIC
  
  
  Having shown that corresponds to all finite monoids, we  now begin the study of fragments of  and the corresponding restrictions on finite monoids.  The first – and arguably most important – fragment is first-order logic. This fragment will be described in  the -McNaughton-Papert-Kamp Theorem. One part of the theorem says that  a language   is first-order definable if and only if it is recognised by a finite monoid  M which  satisfies
  
      a^ = a^a   for all a ∈ Ma monoid or semigroup which satisfies this is called aperiodic,
  
  where ∈1,2,… is the idempotent exponent from  the Idempotent Power Lemma. 
  In other words, in an aperiodic monoid the sequence a, a^2, a^3, … is eventually constant, as opposed to having some non-trivial periodic behaviour.
  
      Consider the parity language (aa)^* ⊆ a^*.   We claim that this language is not recognised by any aperiodic monoid, and therefore it is not first-order definable. Of course the same is true for the complement of the language, namely the words of odd length which were discussed in Example <ref>.
  
      Suppose that the parity language is recognised by a homomorphism h into some  finite monoid M.  By  Theorem <ref> on syntactic monoids, there is a surjective homomorphism from the image of h, which is a sub-monoid of M, into the syntactic monoid. In other words, the syntactic monoid is a quotient (i.e. image under a surjective homomorphism) of a sub-monoid of M. Since the syntactic monoid is the two-element group, which is not aperiodic, and since aperiodic monoids are closed under taking quotients and  sub-monoids, it follows that M cannot be aperiodic. 
  
      The above argument shows  that  a  regular language is first-order definable if and only if its syntactic monoid  is  aperiodic. Since the syntactic monoid can be computed, and aperiodicity is clearly decidable, it follows that there is an algorithm which decides if a regular language is first-order definable. 
  
  
  
  As can be guessed from the name,  the -McNaughton-Papert-Kamp Theorem is an amalgam of several results, which consider several formalisms. Apart from first-order logic and aperiodic monoids, these formalisms include linear temporal logic and star-free regular expressions, so we begin by defining those. 
  
  
  
    
  *Linear temporal logic Linear temporal logic[
    This logic, and the theorem about its expressive completeness for first-order logic, is due to 
  [Theorem II.1.]Kamp68
  This theorem considers all words where the set of positions is a (possibly infinite) complete linear ordering, which covers the special case of finite words that is considered in this chapter.
  ] () is an alternative to first-order logic which does not use quantifiers.  The logic only makes sense for structures equipped with a linear order; hence the name. 
  
  
    Let Σ be a finite alphabet. Formulas of linear temporal logic () over Σ are defined by the following grammar:
    
      a ∈Σthe current
       position has
       label a    φψ    φψ    φ    φψφ until ψ.
  
    The semantics for formulas is a ternary relation, denoted by 
  
      w,word
       in Σ^+xposition
       in wφ formula,
  
     which is defined as follows. A formula a ∈Σ is true in positions with label a. The semantics of Boolean combinations are defined as usual. For formulas of the form φψ, the semantics[We use a variant of the until operator which is sometimes called strict until. Strict until is the variant that was originally used by Kamp, see <cit.>.
     ] are 
      
      w,x φψ    ∃ y x<ythere is some 
       position strictly
       after x w,y ψwhich
       satisfies ψ∀ z  x < z < y  ⇒ w,z φ.and such that all intermediate 
       positions satisfy φ
  
    We say that an formula is true in a word, without specifying a position,   if the formula  is true in the first position of that word; this only makes sense for nonempty words. 
    A language L ⊆Σ^* is called definable if there is an formula φ  that defines the language on nonempty words: 
    
      w ∈ L   iff   
        w φ    for every w ∈Σ^+.
  
    
  For example, the formula
  a  b defines the language Σ a^* b Σ^*. If we add the empty word to this language, then it is still defined by the formula a  b, because the notion of definable language does not take into account the empty word.
  
    To get a better feeling for , we discuss some extra operators that can be defined using until, and which will be used later in this chapter.
       We write  for any vacuously false formula, such as a  a. Likewise ⊤ denotes any vacuously true formula. Here are  some commonly used  extra operators:
      
      φ    φ,the next position satisfies φφ    ⊤φ,some strictly later position
       satisfies φφ^* ψ    ψ (φψ).non-strict until
  
      Similarly, we  define a non-strict version of the operator , with ^* φ = φφ.
      For example, the formula 
      
      ^* (a ⊤last position)
  
      says that the last position in the word has label a. 
  
  
  Almost by definition, every definable language is also first-order definable. Indeed, by unfolding the definition, one sees that for every formula there is  a first-order formula φ(x) that is true in the same positions.
  
  
  
    
  *Star-free languages. We now present the final formalism that will appear in the -McNaughton-Papert-Kamp Theorem, namely star-free expressions[These were introduced in[p. 190.]Schutzenberger65]. As the name implies, star-free expressions cannot use Kleene star. However,  in exchange they are allowed to use complementation (without star and complementation one could only define finite languages). For an alphabet Σ, the  star-free expressions are those that can be defined using the following operations on languages:
  
      a ∈Σthe language that
       contains only
       the word a    ∅empty 
       language    L Kconcatenation    L + Kunion    L.complementation 
       with respect 
       to Σ^*
  
  Note that the alphabet needs to be specified to give meaning to the complementation operation. A language is called star-free if it can be defined by a star-free expression. 
  
      Assume that the alphabet is a,b. The expression ∅̅ describes the full language a,b^*. Therefore 
      
      ∅̅· a ·∅̅
  
      describes all words with at least one a. Taking the complement of the above expression, we get a star-free expression for the language b^*. 
  
  
  Like for formulas, almost by definition every star-free expression describes a first-order definable language. This is because to every star-free expression one can associate a first-order formula φ(x,y) which selects a pair of positions x ≤ y if and only if the corresponding infix (including x and y) belongs to the language described by the  expression.
  
  
  
    
  *Equivalence of the models. The -McNaughton-Papert-Kamp  Theorem says that all of the formalisms discussed so far in this section are equivalent. 
  
  
    The following are equivalent[This theorem combines three equivalences.
  
      The equivalence of aperiodic monoids and star-free expressions was shown in [p. 190.]Schutzenberger65 The equivalence of star-free expressions and first-order logic was  shown in [Theorem 10.5.]McNaughtonPapert71   The equivalence of first-order logic and , not just for finite words, was shown  in [Theorem II.1.]Kamp68
    ] for every  L ⊆Σ^*:
    
      
    *  recognised by a finite  aperiodic monoid;
      
    *  star-free;
      
    *  first-order definable;
      
    *  definable.
    
  
  
  
  The rest of Section <ref> is devoted to proving the  theorem, according to the following plan:
  
      @C=2cmaperiodic
      monoids[d]_Section <ref>
      [r]_obvious   first-order
      logic@/^/[r]^Section <ref>[ul]_Section <ref>   star-free
       expressions@/^/[l]^obvious
  
  
  
  
  
  
   §.§ From first-order logic to aperiodic monoids and star-free expressions
  
  
  
  In this section, we prove two inclusions: first-order logic is contained in both aperiodic monoids and star-free expressions. 
  
  
  
    
  *games. In the proof, we  use games, which are described as follows. An game is played by two players, called Spoiler and Duplicator.  A configuration of the game is a pair of words (one red and one blue), each one  with a n-tuple of distinguished word positions 
  
      w,in Σ^*x_1,…,x_npositions in w     w,in Σ^*x_1,…,x_npositions in w
  
  For such a configuration and k ∈0,1,…, the k-round game is played as follows.  If there is a quantifier-free formula that distinguishes the two sides (red and blue), then Spoiler wins immediately and the game is stopped. Otherwise, the game continues as follows. If k=0, then Duplicator wins. If k > 0 then  Spoiler chooses one of the colours red or blue, and a distinguished position x_n+1 in the word of the chosen colour. Duplicator responds with a matching distinguished position in the word of the other colour, and the game continues with k-1 rounds from the configuration with n+1  distinguished positions, which is obtained by adding the new distinguished positions. This completes the definition of games.
  
  The point of games is that they characterise the expressive power of first-order logic, as stated in Theorem <ref> below. The   number of rounds in the games corresponds  to quantifier rank of a formula, which is the nesting depth of quantifiers, as illustrated in the following example
  
      ∀ x (a(x)    ⇒ (∃ y y < x  b(y))^quantifier rank 1(∃ y y > x  b(y))^quantifier rank 1)_quantifier rank 2
  
  The correspondence of logic and games is given in the following theorem:
  
      For every configuration of the game and k ∈0,1,…, Duplicator has a winning strategy in the game if and only if the two sides of the configuration satisfy the same formulas of first-order logic with quantifier rank at most k.
  
  
      Straightforward induction on k.
  
  
  
   For k ∈0,1,2,… and w,  w ∈Σ^*, we write 
  
      w  k  w
  
  if the two words satisfy the same sentences of first-order logic with quantifier rank at most k, or equivalently, Duplicator has a winning strategy in the k-round game over the two words (with no distinguished positions).
  
  
  
  
  
  
  
  The following lemma characterises  equivalence classes of k+1 in terms  of equivalence classes of k by using only Boolean combinations and concatenation.  
   For every k ∈0,1,… and finite alphabet Σ, the equivalence relation k on Σ^* has finitely many equivalence classes. Furthermore, for every words w,  w ∈Σ^* we have  w k+1 w if and only if 
   
      w ∈ LaK  w ∈ LaK
  
   holds for every a ∈Σ and  every L,K ⊆Σ^* which are  equivalence classes of k.
  
  
    Induction on k. 
  The “furthermore” part immediately implies that there are finitely many equivalence classes, since there are finitely many choices for the letter a, and also finitely many choices for the equivalence classes L,K thanks to the induction assumption. Note that the number of equivalence classes for ≡_k+1 is exponential in the number of equivalence classes for ≡_k. 
  
  It remains to prove the “furthermore” part. 
  
      For the left-to-right implication, we observe that LaK can be defined by a first-order sentence of quantifier rank k+1, which existentially quantifies over some  position x with label a and then checks (using quantifier rank k) that the part before x belongs to L and the part after x belongs to K. Therefore, if w and w satisfy the same sentences of quantifier rank k+1, they must belong to the same languages of the form LaK. 
  
      Consider now the right-to-left implication. Here it will be useful to consider variant of the game, call it the local game. Consider a configuration of the game of the form 
      
      w,x_1,…,x_n    w,x_1,…,x_n.
  
      where the red  distinguished positions are listed in strictly increasing order  x_1 < ⋯ < x_n, and  the same is true for the blue positions. Let us partition  the positions x  of w into the following 2n+1 sets, some of which may be empty: 
      
      X_0x < x_1  x_1  X_1x_1 < x < x_2  x_2  ⋯  X_n-1x_n-1 <  x < x_n  x_n  X_nx_n <  x   .
  
      Similarly, we partition the positions in the blue word w. We say that a strategy of player Spoiler is local if there is some i ∈0,…,n such that all positions chosen by Spoiler in the strategy belong to X_i (for positions in the red word w) or X_i (for positions in the blue word w). 
      
      
          If Spoiler has a winning strategy, then he also has a local one.
      
      
          There is no benefit for Spoiler in using two different blocks of the partition described in (<ref>).
      
      A corollary of this claim is that if n=1, then Spoiler has a winning strategy in the (k+1)-round game for  the configuration 
      
      w, x_1    w,x_1
  
      if and only if: (1) the  distinguished positions have different labels; or (2) Spoiler has a winning strategy in the k-round game for the parts strictly before the distinguished position; or (3) Spoiler has a winning strategy for the parts strictly after the distinguished position. This gives the right-to-left implication in the lemma.  
  
   
  
  
  We  use the lemma above to prove the inclusion of first-order logic in both star-free expressions and aperiodic monoids. 
  
  
    
    From first-order logic to star-free. It is enough to show that every equivalence class of ≡ k is star-free. This is proved by  induction on k.  For the induction base of k=0, there is only one equivalence class, namely all words, which is clearly a  star-free language. Consider now the induction step. 
    Consider an equivalence class M of k+1. Let 
     X be  the set of triples 
    
      Lequivalence
       class of k    aletter in Σ    Kequivalence
       class of k.
  
    By 
     Lemma <ref>, the  equivalence class M is equal to  the following finite Boolean combination of concatenations
    
      ⋂_(L,a,K) ∈ X
      
          M ⊆ LaK LaK 
            ∩    ⋂_(L,a,K) ∈ X
      
          LaK ∩  M = ∅ LaK .
  
    This is a star-free expression, if we assume that L and K are described by star-free expressions from the induction assumption. 
     Since every first-order definable language is a finite union of equivalence classes of k for some k, the result follows. 
     
    From first-order logic to aperiodic monoids. A corollary of Lemma <ref> is the following compositionality property for first-order logic on words. 
     
         For every alphabet Σ and k ∈0,1,…, the equivalence relation k on Σ^* is a monoid congruence with finitely many equivalence classes. 
     
     
         Induction on k. To see that there are finitely many equivalence classes, we use Lemma <ref>, which says that an equivalence class of ≡_k+1 can be viewed as a set of triples (equivalence class of ≡_k, letter from Σ, equivalence class of ≡_k), and there are finitely many possible sets of such triples. We now show that ≡_k+1 is a monoid congruence, i.e.
         
      w≡_k+1w and v≡_k+1 v     implies    wv≡_k+1wv.
  
         By Lemma <ref>, to prove the conclusion of the above implication, it is enough to show that wv and wv belong to the same languages of the form LaK as in the lemma. This follows immediately from the assumption of the implication, and the induction assumption of the lemma which  that ≡_k is a monoid congruence. (In the proof we also need the observation that ≡_k+1 refines ≡_k, which follows from the definition of ≡_k.) 
      
     By the above corollary, the function  h_k which maps a word to its equivalence class under k is a monoid homomorphism into a finite monoid. This homomorphism recognises every language that is defined by a first-order sentence of quantifier rank at most k, by definition of k. Therefore, every first-order definable language is recognised by h_k for some k.  It remains to show that the monoid used by such a homomorphism  is aperiodic. To prove this, we use Lemma <ref> and a simple induction on k to show that
     
      w^2^k-1 k w^2^k    for every  w ∈Σ^* and k ∈1,2,….
  
  
  
  
  
  
  
  
  
   §.§ From aperiodic monoids to 
  
  
  
  The last, and most important, step in the proof is constructing an formula based on an aperiodic monoid[The proof in this section is based on
  [Section 2]wilke1999
  ]. In this part of the proof, semigroups will be more convenient than monoids. We will use to define colourings, which are like languages but with possibly more than two values:  a function from Σ^+ to a finite set of colours is called definable if for every colour, the words sent that colour are an definable language. For example, a semigroup homomorphism into a finite semigroup is a colouring. 
  
  
     Let S be a finite aperiodic semigroup, and let Σ⊆ S. The colouring 
    
      w ∈Σ^+     ↦    multiplication of w
  
    is  definable.
    
    By applying the lemma to the special case of S being a monoid, and substituting each monoid element for the letters that get mapped to it in the recognising homomorphism, we immediately get the implication from finite aperiodic monoids to . 
  
  It remains to prove the lemma. The  proof is by induction on two parameters: the size of the semigroup S, and the size of the subset Σ.  These parameters are ordered lexicographically, with the size of S being more important. Without loss of generality, we assume that Σ generates S, i.e. every element of S is the multiplication of some word in Σ^+. 
  
  The induction base is treated in the following claim. 
  
    If either S or Σ has size one, then Lemma <ref> holds.
  
  
    If the  semigroup has one element, there is nothing to do, since colourings with one possible colour are clearly definable. Consider the case when the Σ contains only one element a ∈ S. By aperiodicity, the sequence
    
      a, a^2, a^3, …
  
    is eventually constant, because all powers bigger than the  threshold  give the same result. The multiplication is therefore easily seen to be an  definable colouring, because for every n ∈1,2,… the singleton language 
    
      a^n⊆a^+
  
    is definable in . For example, when n=3, then the defining formula is
    
      ⊤there are at least
       3 positions     (⊤) there are strictly 
        less than 4 positions   .
  
  
  
  We are left with the induction step. For c ∈ S, consider the function 
  
      a ∈ S ↦ ca ∈ S.
  
  
  
    If a ↦ ca is a permutation of S, then it is the identity.
  
  
    Suppose that a ↦ ca is a permutation of S, call it π. By aperiodicity, 
    
      π^∘π = π^.
  
    Since permutations form a group, we can multiply both sides by the inverse of π^ and conclude that   π is  the identity permutation.
  
  If the function a ↦ ca is the identity for every  c ∈Σ, then the multiplication  of a word is the same as its last letter; and such a colouring is clearly definable.  We are left with the case when there is some c ∈Σ such that a ↦ ca is not the identity. Fix this c for the rest of the proof.  
   Define T to be the image of the function a ↦ ca,  this is a proper subset of S by assumption on c. 
  
    T is a sub-semigroup of S. 
  
  
    Multiplying two elements with prefix c gives an element with prefix c.
  
  
  In the rest of the proof, we use the following terminology for a word w ∈Σ^+:
  30 
  We first describe the proof strategy.    
   For each black block, its multiplication can be computed in using the induction assumption on a smaller set of generators. The same is true for red blocks.  Define a red-black block 
  to be any union of a  red block  plus  the following (non-empty) black block; as illustrated below:
  31
  For red-black blocks,  the multiplication operation  can be computed in , by using multiplication for the  red and black blocks inside it. Also, for every red-black block, its multiplication is in T because it begins with c and has at least two letters.  Therefore, we can use the induction assumption on a smaller semigroup, to compute the multiplication of the union of all red-black blocks. Finally, the multiplication of the entire word is obtained by taking into account the blocks that are not part of any red-black block. 
  
  The rest of this section is devoted to formalising the above proof sketch. In the formalisation, it will be convenient to reason with word-to-word functions. 
  We say that  a function of type Σ^* →Γ^* is  an   transduction if it has the form 
  
      a_1 ⋯ a_n ∈Σ^*     ↦     f(a_1 ⋯ a_n)  f(a_2 ⋯ a_n) ⋯ f(a_n)
  
  for some definable colouring f : Σ^+ →Γ + ε. Under this definition, the output length is at most the input length for transductions.  By substituting formulas, one easily[The result would also hold for generalisation of transductions where the colouring f has type Σ^+ →Γ^*, but the proof is easier when the type is Σ^+ →Γ+ε, and only the latter case is needed here.] shows the following composition properties: 
  
      ( colourings)∘( transductions)   ⊆    colourings
      ( transductions)∘( transductions)   ⊆    transductions.
  
  
  We use transductions to decorate an input word w ∈Σ^+ with extra information that will serve towards computing its multiplication. 
  
  
    
    * For each position that precedes a block (i.e. the next position begins a new block), write  in that position the value of the next block.   For the remaining positions, do not write anything. Use two disjoint copies of S to distinguish the values of the red and black blocks. Here is a picture:
      32
      In the above picture, a_i,j denotes the multiplication of the infix i,…,j. The function described in this step is an transduction, thanks to the induction assumption on smaller alphabets[To make this formal, we need a simple closure property of  that is described in Exercise <ref>. ]. 
      
  
    * Take the output of the function in the previous step, and for each red letter (the multiplication of a red block), multiply it with the next letter (which is the multiplication of a black block). As a result, we get the values of all red-black blocks which do not begin in the first position.   Here is a  picture:
  33
  The function in this step is clearly an transduction. 
  
   By induction assumption on a smaller semigroup, the multiplication operation T^+ → T is an colouring. By composing the functions described above with the semigroup multiplication in T, we see that 
  
      w ∈Σ^+   ↦  value of the union of red-black blocks
  
  is an colouring. The values of the (at most two) blocks that do not participate in above union can also be computed using  colourings, and therefore the multiplication of the entire word can be computed. 
  
  
  
  
   Show that for every sentence of first-order logic, there is a sentence that is equivalent on finite words, and which uses at most three variables (but these variables can be repeatedly quantified).
    Translate a first-order sentence to , and then back again to first-order logic. 
  
  
  
    Show that the following  are equivalent for a finite semigroup:
    
      
    *  aperiodic;
      
    *  -trivial, which means that all -classes are singletons;
      
    *  no sub-semigroup is a non-trivial group.
    
  
  
  
        
    <ref> ⇒ <ref> Suppose that a,b are in the same -class. This means that there exist x,y such that a=xb and b=ay. We have therefore 
        
      a = x^n a y^n     for every n ∈1,2,….
  
        By <ref>, there must be some n such that y^n=y^n+1 and therefore
        
      a = x^n a y^n = x^n a y^n+1 = ay = b.
  
        
    <ref> ⇒ <ref> Every group is contained in some -class, and therefore all groups must be trivial.
        
    <ref> ⇒ <ref> Let s ∈ S, and choose n so that s^n is idempotent. It follows that 
        
      s^n, s^n+1, …, s^n+n = s^n
  
        are all in the same -class, and this -class contains an idempotent. Therefore, this -class is a group by the -class Lemma, and hence it must be trivial. This proves that s^n= s^n+1.
      
  
  
  
  
  Consider the successor model of a word w ∈Σ^*, which is defined like the ordered model, except that instead of x < y we have x+1=y. Give an example of a regular language that is first-order definable using the ordered model, but not using the successor model.
    Consider the language 
    
      a^* b a^* c a^* ⊆a,b,c^*.
  
    By induction on k, one can show that  Duplicator wins the k-round game over the successor models of 
    
      a^2^kba^2^kca^2^kin the language    a^2^kca^2^kba^2^k.not in the language
  
  
  
  Show two languages which have the same syntactic monoid, and such that only one of them is first-order definable in the successor model. In particular, one of the closure properties from Exercise <ref> must fail for this logic. 
  The languages are 
  
      abacafirst-order definable
       in the successor modela^*ba^*ca^*not first-order definable
       in the successor model
  
  
  
  
  Let Σ be a finite alphbet and let ⊢, ⊣ be fresh symbols. For k,ℓ∈0,1,…, we say that w, w' ∈Σ^* are  (k,ℓ)-locally equivalent if 
  
  
  
      ⊢ w ⊣ has at least i
      
         occurrences of infix v    iff    ⊢ w' ⊣ has at least i
      
         occurrences of infix v
  
  
  
  
  holds  for every i ∈0,…,k and every v ∈Σ^* of length at most ℓ. Show that L ⊆Σ^* is first-order definable in the successor model if and only if it is a union of equivalence classes of (k,ℓ)-local equivalence, for some k,ℓ.
  
  
    Let Γ⊆Σ and let   L ⊆Γ^*. If L is  definable in , then the same is true for 
  
      w ∈Σ^* : L contains the maximal prefix of w which uses only letters from Σ.
  
  
  
  
  
  
  
  Consider  , i.e. the fragment of where the only operator is . Show that this fragment is equal to the definite languages from Exercise <ref>.
  
  
  
  Show that if   a language  is first-order definable in the successor model, then the  syntactic semigroup  satisfies the following equality
  
      eafbecf = ecfbeaf     for all e,f,idempotents
      a,b,c.
  
  
  
  
     Show that the identity in Exercise <ref>, together with aperiodicity, is equivalent to first-order definability in the successor model.
  Any commutative language that is not first-order definable, e.g. words of even length. However, if the semigroup satisfies the equality and is aperiodic, then we get a necessary and equivalent condition.    
  
  
  Consider the following extension of with group operators.  Suppose that G is a  finite group, and let
  
      φ_g_g ∈ G,
  
  be a family of already defined formulas  such that every position in an input word is selected by  exactly one formula φ_g. Then we can create a new formula, which  is true in a word of length n if 
  
      1 = g_1 ⋯ g_n,
  
  where  g_i ∈ G is the unique group element whose corresponding formula selects  position i. Show that this logic defines all regular languages.
  
  
  
  
  § SUFFIX TRIVIAL SEMIGROUPS AND TEMPORAL LOGIC WITH F  ONLY
  
  
  In the previous section, we showed that first-order logic corresponds to the monoids without groups, which is the same thing as finite monoids with trivial -classes (Exercise <ref>). What about monoids with trivial suffix classes, prefix classes, or infix classes? Trivial infix classes will be described in Section <ref>. In this section, we give a logical characterisation of trivial suffix classes. A symmetric statement holds for trivial prefix classes.
  
  In the characterisation,  we use the fragment of where until is replaced by the following operators
  
      ⊤φ_φ    φ_φ    φφ_^* φ    ^* φ_^* φ.
  
  Since all of the above operators can be defined in terms of , we write for the resulting logic.
  
  [This theorem is based on 
    [Theorem 6.1]etessamiWilke2000
    [Theorem 4.2]CohenPerrinPin93
    The result itself is taken from <cit.>. However, the use of Green's relations in the proof is more in the spirit of <cit.>, which considers an stronger logic that is obtained from by adding a “next” operator.
    ]
    The following conditions are equivalent for  L ⊆Σ^*:
    
      
    *  is recognised by a finite suffix trivial monoid;
      
    *  is defined by a finite union of regular expressions of the form
      
      Σ_0^* a_1 Σ_1^* a_2 ⋯ a_n Σ_n^*     where a_i ∈Σ - Σ_i for i ∈1,…,n;We call such an expression  suffix unambiguous.
       A set Σ_i ⊆Σ is allowed to be empty, in which case Σ_i^* = ε.
  
      
      
    *  is defined by a Boolean combination of  formulas of the form  ^* φ.
      
      
      
      
      
      
      
      
    
  
  
  To see why the formulas in item <ref> need to be guarded by ^*, consider the formula a which defines the language “words beginning with a”. This language is not recognised by any finite suffix trivial monoid. 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
    
    
    
    <ref>⇒ <ref>  We will show that for every finite suffix trivial monoid M, and every F ⊆  M, the language
    
      w ∈ M^* : the multiplication of w is in F
  
    is defined by a finite union of suffix unambiguous expressions. It will follow that for every monoid homomorphism into M, the recognised language  is defined by a similar expression, with monoid elements substituted by the letters that map to them (such a substitution preserves suffix unambiguity). 
    
    Since our target  expressions are closed under finite unions,
    it is of course enough to consider the case when F contains only one element, call it a ∈ M. The proof is  by induction on the position of a in the  suffix ordering. 
  
    The induction base is when a is a suffix of every monoid element, which means that a is a suffix of the monoid identity. By Exercise <ref>, the infix class of the identity is a group, and a group must be trivial in a suffix trivial monoid. It follows that a word multiplies to a if and only if it belongs to a^*, which is a suffix unambiguous expression.
  
  
  
     We now prove  the induction step. Consider a word  that multiplies to a. This word must be nonempty, since otherwise it  would multiply to the identity. Let i be the maximal position in the word such that the suffix starting in i also multiplies to a. By suffix triviality, every position <i is labelled by a letter in 
     
      Σ_0  = b ∈ M : ba = a.
  
     Let b be the multiplication of the suffix that starts after i, not including i, and let c be the label of position i. By choice of i, b is a proper suffix of a and a=cb.  Summing up, words that multiply to a are defined by the expression
  
      ⋃_b,c ∈ M
      b is a proper suffix of a
      
        a=cb
      Σ_0^* c ·(words that multiply to b),
  
  Apply the induction assumption to b, yielding a finite union of suffix unambiguous expressions, and distribute the finite union across concatenation. It remains to justify that the resulting expressions are also suffix unambiguous. This is because none of the expressions that define words that multiply to b can begin with Σ_1^* with c ∈Σ_1, since otherwise we would  contradict the assumption that cb = a ≠ b. 
  
  
    <ref> ⇒ <ref> Since the formulas from item <ref>  are closed under union, it is enough to show that every suffix unambiguous expression 
  
      Σ_0^* a_1 Σ_1^* a_2 ⋯ a_n Σ_n^*
  
  can be defined by a formula as in <ref>.  For i ∈0,…,n, define L_i to be the suffix of the above expression that begins with Σ_i^*. By induction on i, starting with n and progressing down to 0, we show that L_i can be defined by a formula φ_i as in item <ref>.
  In the induction base, we use the formula
  
      φ_n =  ^* ⋁_a  ∈Σ_n aall positions have label in Σ_n.
  
  For the induction step, we first define the language a_i L_i, using a formula of (which is not in the shape from item <ref>):
  
      ψ_i =  a_i  (φ_i) ⋁_j > iφ_j.
  
   Because the expression is suffix unambiguous, the formula ψ_i selects at most one position in a given input word; this property will be used below.
  The language L_i-1 is then defined by 
  
      φ_i-1=     ^* ψ_i   ^* ((ψ_i) ⇒⋀_a ∈Σ_0 a)if a position is to the left
       of the unique position 
       satisfying ψ_i, then 
       it has label in Σ_0.
  
  
    <ref> ⇒ <ref> Define the rank of a formula in to be the nesting depth of the operator . (We assume here that  is the only temporal operator used in the formula, and the remaining operators such as  or ^* are replaced by their definitions using .) For k ∈0,1,…, define ≈_k to be the equivalence relation on Σ^+ which identifies two words if they satisfy the same formulas of  rank at most k.
    The key observation is the following pumping lemma.
  
    
      For every  k ∈0,1,2,…  we have 
      
      w (xy)^i u   ≈_k      w y (xy)^j u  
                for every w ∈Σ^+,x,y,u ∈Σ^* and i,j ≥ k.
      
    
     
      Induction on k. For k=0, we observe that  the equivalence class under ≈_0 depends only on the first letter, and the two words on both sides in the claim have the same letter because w is nonempty. 
      
      Consider now the induction step, when going from k to k+1. By unravelling the definition of ≈_k+1, we need to show that if i,j ≥ k+1, then words on both sides of the equivalence 
      
      w (xy)^i u   ≈_k+1     w y (xy)^j u
  
      have the same first letter, and for every nonempty proper suffix of a word on one side of the equivalence, there 
      there is a nonempty proper suffix on the other side of the equivalence, such that the two suffixes are equivalent under ≈_k.  Clearly the first letters are the same, because w is nonempty. Consider now the suffixes. Suppose first that  v is a nonempty proper suffix of the left side. If v is a suffix of (xy)^k+1u, then the same v is a suffix of the right side. Otherwise, we can use the induction assumption.  Consider now a nonempty proper suffix v of the right side.  Here we argue in the same way as previously, except that there is one extra case, when 
      
      v = z(xy)^iu     for some z that is a suffix of y.
  
      In this case, the ≈_k-equivalent suffix on the left side is 
      z(xy)^k u.
    
    
    By unravelling the definition of the syntactic monoid, in terms of two-sided congruences, we infer from the above claim  that for every rank k formula φ of , the syntactic monoid M of ^* φ  satisfies 
    
      (xy)^ = y(xy)^    for all x,y ∈ M.
  
    The same is also true for syntactic monoids of Boolean combinations of such formulas.  
    To finish the proof, we observe that  property (<ref>) is true in a finite monoid if and only if it is suffix trivial. Indeed, if a monoid is suffix trivial, then   (xy)^ and y(xy)^ must be in the same suffix class, and hence equal. Conversely, if a,b are in the same suffix class, then there must be some x,y such that b=xa and a=yb; it follows that 
    
      a = y(xy)^ b (<ref>) =  (xy)^ b = b.
  
   
  
  
  
    Let Σ be an alphabet and let c ∉Σ be a fresh letter. Show that L ⊆Σ^+ satisfies the conditions of Theorem <ref> if and only cL is definable in .
  
  
  
  § INFIX TRIVIAL SEMIGROUPS AND PIECEWISE TESTABLE LANGUAGES
  
  
  Having discussed monoids that are -trivial,  prefix-trivial and suffix-trivial  in the previous sections, we turn to finite monoids that are infix-trivial.  For languages recognised by finite infix trivial monoids, a prominent role will be played embeddings of words (also known as the Higman ordering).
  
  
   We say that a word w ∈Σ^* embeds in a word v ∈Σ^*, denoted by w  v,  if there is an injective function from positions in w to positions in v, which preserves the order on positions and the labels. 
  
  In other words, w embeds in v if and only if  w can be obtained from v by removing zero or more positions.  For example “ape” embeds into “example”. It is easy to see that embedding is an ordering on words: it is reflexive, transitive and anti-symmetric (although it will cease to be anti-symmetric for infinite words).
  We say that a language L ⊆Σ^* is upward closed if 
  
      v  w   v ∈ L ⇒ w ∈ L.
  
  Symmetrically, we define downward closed languages. The main result  
  about embedding is that it is a well-quasi order, as explained in the following lemma. 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
    For every upward closed  L ⊆Σ^* there is a finite subset U ⊆ L such that
    
      L = w ∈Σ^*: v  w  for some v ∈ Uwe call this the upward closure of U.
  
  
  
    Consider the set of minimal elements in L, i.e. the set
    
      U =  w ∈ L : there is no v ∈ L such that v  w and v ≠ w.
  
    Because the embedding ordering is well-founded (there are no infinite decreasing chains) it follows that L is equal to the upward closure of its minimal elements U. By definition, U is an antichain,  which means that every two elements of U are incomparable with respect to embedding. Therefore, to prove the lemma it remains to show that  antichains are finite.
  
    
      There is no infinite antichain with respect to embedding.
    
    
      Define a growth in a finite or infinite sequence w_1,w_2,… to be a pair of indices i < j such that w_i  w_j. We will show that every infinite sequence contains at least one growth. This implies that there cannot be any infinite antichains. 
      
      Suppose, toward a contradiction, that there is an infinite sequence of words without growths. 
  Define the radix ordering on finite words as follows: shorter words come before longer ones, and same length words are ordered lexicographically.    Define a sequence w_1,w_2,… by induction as follows. The word w_1 is the least word, in the radix ordering (which is well-founded, so it makes sense to talk about least words), which can be extended to an infinite sequence without growths. For n > 1, define w_n to be the least word in the radix ordering such that w_1,…,w_n can be extended to an infinite sequence without growths, in particular w_1,…,w_n has no growths. Sequences without growths are closed under limits, and therefore w_1,w_2,… has no growths.
  
  
  Consider the sequence w_1,w_2,… defined in the previous paragraph. Because the alphabet is finite, there must be some letter, call it a, such that infinitely many words in the sequence, say with indexes n_1 < n_2 < ⋯, begin with the letter a. Define a new sequence as follows:
  
      w_1,…,w_n_1 -1 , w_n_1,w_n_2,…the word w_n is
      
       is obtained from 
       the word w_n by 
       removing the 
       first letter .
  
  Since w_n_1 is shorter than w_n_1, it follows from the construction in the previous paragraph that  the above sequence must have some growth. However, it is easy to see that any growth in the above sequence would also translate to some growth in the sequence from the previous paragraph, hence a contradiction. 
    
  
  
  Here is a logical corollary of  Higman's lemma. 
  
  
    A language is upward closed if and only if it can be defined in the ordered model by an ∃^*-sentence, i.e. a sentence of the form
    
      ∃ x_1  ∃ x_2  ⋯∃ x_nonly existential quantifiers  φ(x_1,…,x_n)quantifier-free.
   
  
  
    Clearly every ∃^*-sentence defines an upward closed language.  Higman's Lemma gives the converse implication, because the upward closure of every finite set is definable by an ∃^*-sentence.   
  
  
  
  Embeddings will also play an important role in the characterisation of languages recognised by monoids that are infix trivial. Before stating the characterisation, we introduce one more definition, namely zigzags.  For languages L,K ⊆Σ^*, define  a zigzag between L and K to be a sequence
  
      w_1_∈ Lw_2_∈ Kw_3_∈ Lw_4_∈ Kw_5_∈ Lw_6_∈ K⋯.
  
  In other words, this is a sequence that is  growing with respect to embeddings, and such that odd-numbered elements are in L and even-numbered elements are in K. The zigzag does not need to be strictly growing, but it will be if  L and K are disjoint.
  
  
  We are now ready for the characterisation of infix trivial monoids.
  
  
    The following conditions are equivalent[
      Equivalence of items <ref> and <ref> was first proved in 
      [p. 220.]Simon75
      Equivalence of items <ref> and <ref> was first proved in 
      [Theorem 3.]czewinskietal2013
    ] for every L ⊆Σ^*:
    
      
    *  recognised by a finite monoid that is infix trivial;
      
    *  is a finite Boolean combination of upward closed languages;
      
    *  there is no infinite zigzag between L and its complement.
    
  
   
  
  We use the name piecewise testable for languages as in item <ref> of the above theorem.
  
  Equivalence[Both conditions <ref> and <ref>  can be checked by  algorithms. For <ref> this is immediate, while condition <ref> is discussed in Exercise <ref>. Therefore, condition <ref> would not be useful for a hypothetical person that  only cares about deciding if a regular language is piecewise testable.  ] of items <ref> and <ref> is a corollary of the following lemma, when applied to K = Σ^* - L. 
  
  
  
     Let L,K ⊆Σ^*. The following are equivalent:
    
      
    *  there are zigzags between L and K  of every finite length;
      
    *  there is an infinite zigzag between L and K;
      
    *  there is no piecewise testable  language M ⊆Σ^* such that 
      
      L ⊆ M   and    M ∩ K = ∅.we say that M  separates L and K
  
    
  
  
     
    
      
    <ref>⇒<ref> Assume that zigzags between L and K can have  arbitrarily long finite lengths. Define a directed acyclic graph G as follows. Vertices are words in L, and there is an edge w → v if 
      
      w  u  v     for some u ∈ K.
  
      For a vertex v ∈ L of this graph, define its potential 
      
      α(v) ∈0,1,…,ω
  
      to be the least upper bound on the lengths of paths in the graph that start in v. This can be either a finite number, or ω if the paths have unbounded length.  
      
      We first show that some vertex must have potential ω. By assumption on arbitrarily long zigzags, potentials have arbitrarily high values. By definition of the graph, α is monotone with respect to  (the opposite of the) embedding, in the following sense:
      
      v  v'   implies  α(v) ≥α(v')     for every v,v' ∈ L.
  
      By Higman's Lemma, the language  L, like any set of words, has finitely many minimal elements with respect to embedding. By monotonicity, one of these minimal words must therefore have potential ω.  
      
      For the same reason as above, if a word has potential ω, then one of its successors (words reachable in one step in the graph) must also have potential ω; this is because there are finitely many successors that are minimal with respect to embedding. This way, we can construct an infinite path in the graph which only sees potential ω, using the same reasoning as  in the proof of  's Lemma. 
  
      
    <ref>⇒<ref>Suppose that there is a zigzag between L and K of infinite length. Every  upward closed  set selects either no elements of the zigzag, or all but finitely many elements of the zigzag. It follows that every finite Boolean combination of upward closed sets must contain, or be disjoint with, two consecutive elements of the zigzag. Therefore, such a Boolean combination cannot  separate L from K. 
      
    <ref>⇒<ref> We prove the contra-positive: if zigzags between L and K have bounded length, then L and K can be separated by a piecewise testable language. For  w ∈ L define its potential to be the maximal length of a zigzag between L and K  that starts in w; likewise we define the potential for w ∈ K, but using  zigzags between K and L.  Define L_i ⊆ L to be the words in L with potential exactly i ∈1,2,…, likewise define K_i ⊆ K.  Our assumption is that the potential is bounded, and therefore L is a finite union of the languages L_i, likewise for K. 
      By induction on i ∈0,1,…, we will show that the languages
      
      L_1 ∪⋯∪ L_i_L_≤ i    and    K_1 ∪⋯∪ K_i_K_≤ i.
  
      can be separated by a piecewise testable language, call it M_i. 
  In the induction base, both languages are empty, and can therefore be separated by the empty language, which is clearly piecewise testable. 
  Consider  the induction step, where we go from i-1 to i. We write L_<i instead of L_≤ i-1. We will use the following sets
  
    
    * the upward closure of L_≤ i;
    
    * the downward closure of L_i;
    
    * a piecewise testable set M that contains K_<i and is disjoint with L_<i.
  
  The first two sets are piecewise testable because they are upward or downward closed, and the third set is obtained from the induction assumption.  These sets  are  depicted in the following picture, with i=3:
  36
   The separator M from the induction assumption contains K_<i and is disjoint with L_<i. Therefore, the piecewise testable language 
  
      M' = (upward closure of L_≤ i)contains L_≤ i  -  (M -  (downward closure of L_i)disjoint with L_≤ i)
  
  contains L_≤ i. We will now show that M' is disjoint with K_≤ i, thus finding a separator as required in the induction. First observe that the upward closure of  L_≤ i is disjoint   with  K_i, because otherwise there would be some words
   
      wL_≤ i    vK_i,
  
   and therefore the word w would have potential i+1, and would not belong to L_≤ i. Therefore, K_i is disjoint with M' ⊆ L_≤ i. The downward closure of   L_i is disjoint with K_<i, since otherwise there would be some words 
   
      wK_<i    vL_i,
  
    contradicting the definition of K_<i. Therefore  K_<i is contained in
    
      Mcontains K_<i - (downward closure of L_i)disjoint with K_<i,
  
    and thus K_<i is disjoint with M'. 
    
  
  
  The Zigzag Lemma proves the equivalence of the conditions about infinite zigzags and piecewise testability in  Theorem <ref>. To finish the proof of the Theorem, we show that the syntactic monoid of L is finite and  infix trivial (which is the same as saying that some recognising monoid is  finite and infix trivial) if and only if there is no infinite zigzag between L and its complement.
  
  
  Suppose first  that the syntactic monoid of L  is either infinite or finite but not infix trivial. If the syntactic monoid is infinite, then the language cannot be piecewise testable, since piecewise testable languages are necessarily regular. Assume therefore that the syntactic monoid is  finite but not infix trivial.  This means that the syntactic monoid is either not prefix trivial, or not suffix trivial. By symmetry, we only consider the case where the syntactic monoid is not suffix trivial. This means that there exist a,b in the syntactic monoid such that
  
      (ab)^≠ b(ab)^.
  
  By unravelling the definition of the syntactic monoid, the above disequality  can be easily used to create an infinite zigzag between L and its complement. 
  
  It remains to show that if the syntactic monoid of L is finite and infix trivial,  then there is no infinite zigzag between L and its complement. Let M be the syntactic monoid. For  a,b ∈ M, define a zigzag between a and b to be a zigzag between the languages
  
      w ∈ M^* : w multiplies to a    w ∈ M^* : w multiplies to b.
  
  If M recognises L, then a zigzag between L and its complement can be used, by extraction, to obtain a zigzag between some two distinct monoid elements a,b ∈ M. The following lemma shows that this cannot happen, thus completing the proof of Theorem <ref>.
  
  
    Let M be finite and infix trivial, and let a,b ∈ M. If there is an infinite zigzag between a and b, then a=b.
  
  
    The proof is by induction on  the infix ordering lifted to pairs:
    
      (x,y)≼ (a,b)         x is an infix of a and y is an infix of b.
  
    The induction base is proved the same way as the induction step.
    Suppose that we have proved the lemma for all pairs (x,y)≺ (a,b). 
   
    
      If there is an infinite zigzag between a and b, then there exists n ∈0,1,… and monoid elements a_i,b_i,c_i_i such that
      
      [     a     =         c_1         c_2           ⋯         c_n;     b     =   b_0   c_1   b_1   c_2   b_2     ⋯ b_n-1   c_n   b_n;     a     =   a_0   c_1   a_1   c_2   a_2     ⋯ a_n-1   c_n   a_n ]
  
      and for every i ∈0,…,n there is an infinite zigzag between a_i and b_i.
    
    
      Consider an infinite zigzag between a and b of the form
      
      w_1  w_2 ⋯
  
  
      Let the letters in w_1 be c_1,…,c_n ∈ M.   For  j ≥ 2, define an important position in w_j to be any position that arises by starting in some position of  w_1, and then following the embeddings 
      
      w_1  w_2 ⋯ w_j.
  
      By distinguishing the important positions in w_j, we get a factorisation
      
      w_j =  w_j,0M^*   c_1   w_j,1M^*   c_2   ⋯ c_n-1w_j,n-1M^*   c_n  w_j,nM^*.
  
      By definition of important positions, for every i ∈0,…,n  the following sequence is  growing with respect to embedding
      
      w_2,i w_3,i⋯.
  
      By extracting a subsequence, we can assume that for every i ∈0,1…,n, the above chain is a zigzag between b_i and a_i, for some b_i,a_i ∈ M. This proves the conclusion of the claim.
    
  
    
      If there is an infinite zigzag between a and b, then either a=b, or there exist c,c'  ∈ M such that a = cc' and cb=b=bc'.
      
      
        Apply Claim <ref>, yielding monoid elements which satisfy the following equalities: 
        
      [     a     =         c_1         c_2           ⋯         c_n;     b     =   b_0   c_1   b_1   c_2   b_2     ⋯ b_n-1   c_n   b_n;     a     =   a_0   c_1   a_1   c_2   a_2     ⋯ a_n-1   c_n   a_n ]
  
        For every i ∈0,…,n, we can see that (b_i,a_i) ≼ (b,a). If the inclusion is strict, then the induction assumption of the lemma yields b_i=a_i. Otherwise, the inclusion is not strict, and therefore 
        
      (a_i,b_i) = (a,b).
  
        If the inclusion is strict for all i, then the third and second rows in the conclusion of Claim <ref> are equal, thus proving a=b, and we are done. Otherwise, there is some i ∈0,…,n such that (b_i,a_i)=(b,a). By infix triviality, every interval in the second row that contains i will have multiplication b. It follows that 
        
      c_jb = bfor all j ≤ i    bc_j = bfor all j > i
  
        It is now easy to see that the conclusion of the claim holds if we  define c and c' as follows:
        
      a = c_1 ⋯ c_icc_i+1⋯ c_nc'.
  
      
    
  Apply the above claim, and a symmetric one with the roles of a and b swapped, yielding elements c,c',d,d' such that
  
      a=cc'    cb=b=bc'    b=dd'    da=a=ad'.
  
  We can now prove the conclusion of the lemma:
  
      a =(<ref>) (dc)^ (c'd')^=infix triviality
      (cd)^ (d'c')^=(<ref>)  b.
  
  
  
  
  
  
  Prove Higman's Lemma.
  
   Give a polynomial time algorithm, which inputs two nondeterministic automata, and decides if their languages can be separated by a piecwise testable language.
  
  Consider  ω-words, i.e. infinite words of the form 
      a_1 a_2 ⋯    where  a_1,a_2,…∈Σ.
  
  Embedding  naturally extends to ω-words (in fact, any labelled orders).  Show that the embedding on ω-words is also a well-quasi order, i.e. every upward closed set is the upward closure of finitely many elements.
  
  
  
  
  
  § TWO-VARIABLE FIRST-ORDER LOGIC
  
  
  We finish this chapter with one more monoid characterisation of a fragment of first-order logic. A corollary of the equivalence of first-order logic and (or of the equivalence of first-order logic and star-free expressions) is that, over finite words, first-order logic is equivalent to its three variable fragment. What about one or two variables?
  
  First-order logic with one variable defines exactly the languages which are  Boolean combinations for sentences of the form ∃ x  a(x). These languages are exactly the languages that are recognised by monoids that are aperiodic and commutative:
  
      a^ = a^+1   ab=ba     for all a,b.
  
  The more interesting case is first-order logic with  two variables, which we denote by . This logic  is characterised in the following theorem. 
  
  [
    The class of monoids from  item 2 appears, under the name Df, in 
  [p. 47,]Schtzenberger1976SurLP
  where it is used to characterise certain unambiguous regular expressions, see Exercise <ref>. 
  Subsequent articles use the name , which we use here as well. The connection with two variable first-order logic, which is the content of the theorem, is from 
  [Theorem 4.]TherienWilke98
  ]
    For a language L ⊆Σ^*, the following are equivalent:
    
      
      
    *  Definable in two variable first-order logic;
      
    *  Recognised by a finite monoid M with the following property:  M is  aperiodic, and if an infix class J ⊆ M contains an idempotent, then J is a sub-semigroup of M.  
    
  
  
  We use the name for the monoids (more generally, finite semigroups) that satisfy the property in item <ref>.
  In the exercises, we add several other equivalent conditions for the above theorem, including the temporal logic and the following fragment of first-order logic:
  
      (definable by a ∃^* ∀^*-sentence)  ∩  (definable by a ∀^* ∃^*-sentence).
  
  
  The rest of Section <ref> is devoted to proving the theorem. We begin with an equational description of , which uses the embedding ordering on words that featured prominently in the previous section. (A stronger equational description  is given in Exercise <ref>.)
  
  
    A finite monoid M is in if and only if  
    it satisfies:
  
      w^ = w^ v w^same multiplication    for all w,v ∈ M^* with v  w.
  
    
  
  
    We first prove that the identity implies that M is  . The identity clearly implies aperiodicity, by taking w=v. 
    Let e be an idempotent. We need to show that if a,b are infix equivalent to e, then the same is true for ab. Because a,b are infixes of e, and e is an idempotent, one can find a word w in S^+ which multiplies to e and contains both  a and b. In particular, ab  w. By the identity in the lemma, we know that e = eabe, and therefore ab is an infix of  e. 
  
    We now show that if M is in , then the identity is satisfied.  Let v  w be as in the identity. Let e be the multiplication of w^, and let  J be the infix class of e. This infix class is a monoid, by definition of .  For every letter a that appears in the word w, there is a suffix of w^ w^ which begins with a and has  multiplication in J. Let a' ∈ J be the multiplication of this suffix. Since J is a monoid, it follows that ea' ∈ J and therefore also ea ∈ J. Since ea ∈ J holds for every letter that appears in w, it follows that ev ∈ J, and therefore also eve ∈ J.  This means that eve is in the -class of e, and therefore e=eve by aperiodicity (which is part of the definition of ), thus establishing the identity.
  
  
  We now prove the theorem.
  
  To prove the implication <ref>⇒<ref>, we show  that for every language definable in , its syntactic monoid belongs to .  By  Lemma <ref> and unravelling  the definition of the syntactic monoid, it is enough to show that for every w_1,w_2, v, w ∈Σ^*  and  n ∈0,1,…,  if v  w then 
    the words
    
      w_1w ^n w^n w_2       w_1 w^n v w^n  w_2
  
    satisfy the same sentences of quantifier rank at most n. This is shown using 
    a simple argument.
  
  
  For the  implication  <ref>⇒<ref>, we use the following lemma. 
  
    Let M be a monoid in , and let a_1,a_2 ∈ M. Then 
    
      w ∈ M^*     ↦    a_1 ·(multiplication of w)· a_2∈ M
  
    is a colouring definable in , which means that for every c ∈ M, the inverse image of c under the colouring is a language that is definable in .
  
  If we apply the above lemma to a_1 and a_2 being the monoid identity, we conclude that the multiplication operation  is   definable in . This implies that every language recognised by the monoid  is definable in , thus proving the implication <ref> ⇐ <ref> in the theorem. It remains to prove the lemma.
  
   Induction on the following parameters, ordered lexicographically:
   
     
    * size of M;
     
    * number of elements that properly extend a_1 in the prefix ordering;
     
    * number of elements that properly extend a_2 in the suffix ordering.
   
   The induction base is when M has one element, in which case the colouring in the lemma is constant, and therefore definable in . 
  
   Let us also prove another variant of the induction base, namely when the induction parameters (2) and (3) are zero, which means that a_1 is maximal in the prefix ordering and a_2 is maximal in the suffix ordering.  It follows that
   
      -class of a_1aa_2 =   -class of a_1ba_2    for all a,b ∈ M.
  
   Since implies aperiodicity, which implies -triviality,  the colouring in the statement of the lemma is constant, and therefore definable in .
  
  It remains to prove the induction step. Because of the two kinds of induction base that were considered above, we can assume that one of the parameters (2) or (3) is nonzero. By symmetry, assume that a_1 is not maximal in the prefix ordering. 
    
    
     For every a ∈ M, the following is a sub-monoid of M: 
      
      b ∈ M : a b  is prefix equivalent to awe call this set the prefix stabiliser of a.
  
      
     The prefix stabiliser clearly   contains the monoid identity. It remains to show that it is closed under multiplication. Let b,c be in the prefix stabiliser of a.  Using the definition of the prefix stabiliser, it is easy to construct a word  w ∈ M^*, such that bc  w and   aw = a.  By Lemma <ref>, it follows that 
    
      a = aw = aw^ = aw^ bc w^ = abc w^,
  
    which establishes that bc is in the prefix stabiliser of a.
  
  
  Let N ⊆ M be the prefix stabiliser of a_1; our assumption says that N is a proper subset of M, and by the above claim it is also a sub-monoid.
    We decompose a word  w ∈ M^* into three parts, as explained in the following picture: 
    37 There is an formula which selects the central position. Since  all labels in the left part are from N,   we can use the 
     induction assumption on a smaller monoid to prove that  the colouring
    
      w     ↦    multiplication of left part
  
    is definable in . (When using the induction assumption, we restrict all quantifiers of the formulas from the induction assumption so that they quantify over positions in the left part.)  Let c be the multiplication of the prefix up to and including the central position; as we have shown above, this multiplication can be computed in . By definition of the central position, we know that a_1 is a proper prefix of a_1c, and therefore we can use the induction assumption to prove that 
    
      w     ↦     a_1 c ·(multiplication of right part)· a_2
  
    is a colouring definable in .  The conclusion of the lemma follows.
  
  
  
  
  
  
  
  
  
  
  
  
  
   Show that a  monoid belongs to if and only if it satisfies the identity 
  
      (ab)^ = (ab)^ a (ab)^    for all a,b.
  
  
  Show that has the same expressive power as , which is the extension of with the following past operator:
  
      w, x ^-1φ    ∃ y y < x    w,y φ.
  
  
  
   Define the syntactic ordering on the syntactic monoid, which depends on the accepting set F, as follows:
  
      a ≤ b     ∀ x,y∈ M xay ∈ F ⇒ xby ∈ F.
  
  Show that a language can be defined by a first-order sentence of the form 
  
      ∃ x_1  ⋯∃ x_n ∀ y_1  ⋯∀ y_m   φ(x_1,…,x_n,y_1,…,y_m)^quantifier-free
      _such a formula is called an ∃^*∀^*-sentence
  
  if and only if 
  
      w^≤ w^ v w^    for all v  wHigman ordering
  
  Hint[
    An effective characterisation of ∃^*∀^*-sentence was first given in 
    [Theorem 3.]arfi87
  The proof was simplified in 
  [Theorem 5.8]PinWeil97a
    The solution which uses Exercise <ref> is based on <cit.>. Characterisations of fragments 
  of first-order logic such as ∃^*∀^* are widely studied, see 
  placeZeitoun2019
  ]: use Exercise <ref>.
  
  
  Show that L  is definable in if and only both L and its complement can be defined using ∃^* ∀^*-sentences. 
  Both L and its complement have the same syntactic monoid, and the syntactic orderings are opposites of each other. Therefore, by Exercise <ref>, we see that L and its complement can be defined by ∃^* ∀^*-sentences if and only if the syntactic monoid satisfies
  
      w^ = w^ v w^    for all v  wHigman ordering.
  
  We will show that the above identity is equivalent to . 
  
  
  We say that a regular expression 
  
      Σ_0^* a_1 Σ_1^* ⋯Σ_n-1^* a_n Σ_n^*
  
  is unambiguous if every word w admits at most one factorisation
  
      w = w_0 a_1 w_1 ⋯ w_n-1a_n w_n     where w_i ∈Σ_i^* for all i ∈1,…,n.
  
  Show that a language is a finite disjoint union of unambiguous expressions if and only if its  syntactic monoid of L is in [This exercise is based on
  Schtzenberger1976SurLP].
  
  
   
  
  CHAPTER: INFINITE WORDS
  
  
  In this chapter, we study infinite words. 
  
      In Section <ref>,  we begin with the classical model of infinite words, namely ω-words. In an ω-word,
       the positions are ordered like the natural numbers. We show how the structure of finite semigroups described by Green's relations can be applied to prove  McNaughton's Theorem about  determinisation of ω-automata. 
  
  In Section <ref>, we move to more general infinite words, where the positions can be any countable linear order, e.g. the rational numbers. For this kind of infinite words, we define a suitable generalisation of semigroups, and show that it has the same expressive power as monadic second-order logic. 
  
  
  
  
  
  § DETERMINISATION OF AUTOMATA FOR Ω-WORDS
  
  
  An ω-word is defined to be a function from the natural numbers to some alphabet Σ. We write Σ^ω for the set of all ω-words over  alphabet Σ. 
  To recognise properties of ω-words, we use automata. These have the same syntax as nondeterministic automata on finite words, but they are used to accept or reject ω-words.
  
   The syntax of a nondeterministic automaton is the same as the syntax of a nondeterministic finite automaton for finite words, namely it consists of:
      
      Qstates    QΣQinput
       alphabet    I,F ⊆ Qinital and
       final states  δ⊆ Q ×Σ× Qtransition relation.
  
      An ω-word over the input alphabet is accepted by the automaton if there exists a run which begins in an initial state, and which satisfies the condition: some accepting state appears infinitely often in the run.
      A deterministic  automaton is the special case when 
       there is one initial state, and the transition relation is a function from Q ×Σ to Q.
  
  
  The literature on automata for ω-words has other acceptance conditions, which will not be used in this book. One example is the Muller condition, where the accepting set is a family of subsets of states, and a run is accepting if the set of states used infinitely often is a subset that belongs to the accepting family. Another example is the parity condition: there is a linear order on the states, and a subset of accepting states, and a run is accepting if the maximal state used infinitely often is accepting. 
  
  The following example shows that deterministic automata are weaker than than nondeterministic ones. 
  
      Consider the language of ω-words over alphabet a,b where letter a appears finitely often. This language is recognised by a nondeterministic automaton as in the following picture:
  38
      The idea is that the automaton nondeterministically guesses some position which will not be followed by any a letters; this guess corresponds to the horizontal transition with label b in the picture.
  
      This  language is not recognised by any deterministic automaton.  Toward a contradiction, imagine a hypothetical deterministic automaton which recognises the language. Run this automaton on b^ω. Since a appears finitely often in this ω-word, the corresponding run (unique by determinism) must use an accepting state in some finite prefix. Extend that finite prefix by appending ab^ω. Again, the word must be accepted, so an accepting  state must be eventually visited after the first a. By repeating this argument, we get a word which has infinitely many a's and where the (unique) run of the deterministic automaton sees accepting states infinitely often; a contradiction. 
  
  
  The above shows that languages recognised by deterministic automata are not closed under Boolean combinations. This turns out to be the only limitation of the model, as shown in the following theorem. 
    The following formalisms describe the same languages of ω-words:
      
          
    * nondeterministic Büchi automata;
          
    * Boolean combinations of deterministic Büchi automata[A  Boolean combination of deterministic automata is the same thing as a deterministic automaton with the Muller condition. Therefore, the  theorem is the  same McNaughton's Theorem,
          [p. 524]McNaughton66
          which says that nondeterministic automata can be determinised  into deterministic Muller automata.
          ].
      
  
  A language is called ω-regular if it satisfies either of the two equivalent conditions in the above theorem. The ω-regular languages are closed under Boolean combination thanks to the deterministic characterisation.   The original application of automata was 's proofBuchi62 that they recognise exactly the same languages of ω-words as monadic second-order logic; this application is a simple corollary of Theorem <ref>, see Exercise <ref>.
  
  
  The easier bottom-up implication in Theorem <ref> follows from the following lemma. 
  
      Languages recognised by nondeterministic automata are closed under union and intersection, and contain all languages recognised by  deterministic automata and their complements.
  
  
      Closure under union is immediate for nondeterministic automata. Consider now the intersection of two nondeterministic automata  and . A nondeterministic automaton  for the intersection is defined as follows. Take two copies of the product automaton ×. The accepting states are
      
      (accepting state, any state)first copy  ∪  (any state, accepting state)second copy
  
      Whenever  sees an accepting state as described above, it switches to the other copy. An accepting run of   must see accepting states of both copies infinitely often, and hence it recognises the intersection of the languages of  and .
  
      Since deterministic automata are a special case of nondeterministic ones, it remains to show that complements of deterministic automata can be simulated by nondeterministic automata.    The complement of the  language of a deterministic automaton consists of those words where final states are seen finitely often in the unique run. This can be checked by a nondeterministic automaton, which nondeterministically guesses the moment where accepting states of the original automaton will no longer be seen. Here is an example. Suppose that we want to complement the deterministic automaton
      109
      which checks that infinitely often, the number of a's is equal to the number of b's modulo 3. The nondeterministic automaton for the complement looks like this:
      108
      In the  simulating nondeterministic automaton, the initial states are inherited in the first copy, and the accepting states are all states in the second copy (which correspond to non-accept states in the original deterministic automaton). The above picture uses ε-transitions, which can be easily eliminated.
  
  
  We are left with the harder top-down implication in the theorem, which says that every nondeterministic automaton can be simulated by a Boolean combination of deterministic automata. 
  There are several combinatorial  proofs for the determinisation result in harder implication[Apart from McNaughton's original proof from <cit.>, another well-known construction is given in 
  [Theorem 1.]Safra88
  Another approach, which is based on a construction of Muller and Schupp, is described in
  [Section 1.]bojanczyk_automata_2018
  ]. In this section, we present 
  an algebraic proof, which leverages the  structural theory of finite semigroups described earlier in this book.
  
  Let  be a nondeterministic automaton, with states Q and input alphabet Σ. 
  
  
  
  
  
  
  
  
  
  The rest of this section is devoted to finding a Boolean combination of deterministic automata that is equivalent to . 
  For an ω-word, define its ω-type to be the set of states from which the word is accepted. We also define the type for finite words, but here we need to store a bit more information. 
  For a run of the automaton over a finite word, define the profile of the run to be  the triple (q,i,p) where q is the source state of the run, p is the target state of the run, and  
  
      i = 
          0    if the run does not use any accepting state
      
          1    if the run uses some accepting state.
  
  Here is a picture of a run with its profile:
  41
  Define the type of a finite word w ∈Σ^+ to be the set of profiles of runs over this word. It is not hard to see that the function
  
      w ∈Σ^+   ↦  type of w ∈(Q ×0,1× Q)_S
  
  is a semigroup homomorphism, with a naturally defined semigroup structure on S. 
  
  The following lemma shows that  types for finite and ω-words   are compatible with each other.
   
       If  w_i ∈Σ^+ and v_i ∈Σ^+ have the same type  for every i ∈1,2,…, then   w_1 w_2 ⋯∈Σ^ω and v_1 v_2 ⋯∈Σ^ω have the same ω-type. 
  
  
      By substituting parts of an accepting run, while preserving the condition.
  
  Thanks to the above lemma, it makes sense to talk about the ω-type  of a word  w ∈ S^ω built out of types; this is the ω-type of some (equivalently, every) ω-word that is obtained by concatenating ω-many finite words with the respective finite types. In particular, it makes sense to say whether or not a word w ∈ S^ω is accepted by , since this information is stored in the type.  A special case of this notation is ae^ω, where a,e ∈ S, which is the ω-type of the  ω-word that begins with letter a and has all other letters equal to e.  The importance of this special case is explained by the following lemma  about factorisations of ω-words[This lemma was first observed by in 
  <cit.>
  where it was used to prove that nondeterministic automata are closed under complementation, without passing through a deterministic model.
  ]
  
  
      For every w ∈ S^ω there exist a,e ∈ S, such that e is an idempotent, ae=a, and there is a   factorisation 
      
      w      =     w_0type a  w_1type e  w_2type e  w_3type e⋯
  
  
   
      Define a cut in w to be  the space between two positions. Consider an undirected edge-labelled graph, defined as follows. Vertices are cuts. For every two distinct cuts, there is an undirected edge,   labelled  by the type of the finite word that  connects the two cuts. By Ramsey's Theorem A, see Exercise <ref>, there exists a type a ∈ S and an infinite set  X of vertices, such  every two distinct vertices from X are connected by an edge with label e.  Define the decomposition from the lemma to be the result of cutting w along all cuts from X.  By assumption on X, every word w_i with i > 0 has type e. Idempotence of e follows from
      
      w_i_ew_i+1_e^e.
  
  Finally, we can assure that ae=a by joining the first two groups.
  
  
  A corollary of Lemmas <ref> and <ref> is that w ∈ L if and only if 
  
      
    (*) there is a factorisation as in Lemma <ref> such that  ae^ω∈ L.
  
  So far, we are doing the same argument as in 's original complementation proof from <cit.>. In his proof,  observed that variant of (*) with ae^ω∉L, which characterises the complement of L, can be expressed by a nondeterministic automaton, and therefore nondeterministic automata are closed under complementation. 
  
  This is the place where  we diverge from 's proof, since we are interested in determinisation, while was interested in complementation.  For determinisation, more insight into the structure of finite semigroups will be helpful. Since it is immediately  not clear how to express condition (*) using a deterministic automaton, we will  reformulate it. In the reformulation,  we say that a pair (a,b) ∈ S^2 appears infinitely often in an ω-word w ∈Σ^ω if  for every  n ∈1,2,… one can find a factorisation 
  
      w = xtype aytype b
      z
  
  such that x has length at least n. 
  
  
      An ω-word w ∈Σ^ω is accepted by  if and only if
      
          
    (**)there exist a,e ∈ S, with e idempotent, ae=a, and ae^ω∈ L,  such that both conditions below are satisfied:
          
              
    *  (a,e) appears infinitely often; and
              
    *  if (b,c) appears infinitely often, then c is an infix of e. 
          
       
  
   The top-down implication, which says that every word accepted by  must satisfy (**), is an immediate  consequence of Lemma <ref>. We are left with the bottom-up implication. Suppose that w satisfies (**), as witnessed by    a,e ∈ S.
  By condition <ref>, there is a decomposition
  
      w  = w_1 v_1 w_2 v_2 w_3 v_3 ⋯
  
      such that for every i ∈1,2,… the word v_i has type e and the prefix ending in w_i has  type a. Let a_i be the type of w_i. The ω-type of w is equal to 
  
      a_1 e a_2 e a_3 e ⋯.
  
  By condition <ref>, there is some n such that  
  
      g_i  e a_i e
  
  is an infix of e for all i ≥ n. Since g_i is begins and ends with e, it follows that g is in the -class of e for all i > n. Since this  -class, call it G, contains the idempotent e, it must be a group by the -class lemma. We now complete the proof of the lemma as follows:
     
      ω-type of wa_1 e a_2 ⋯ a_n-1 e_n-1 =a
      
                  a ea_n ea_n+1 e a_n+2 e ⋯e is idempotent and Lemma <ref>
      
                  a ea_n eea_n+1 ee a_n+2 ee ⋯definition of g_i
      
                  a g_n g_n+1 g_n+2⋯by Lemma <ref>, for some g,f ∈ G
      
                  agf^ωbecause e is the unique idempotent in G
      
                  age^ωsome power of g is the  idempotent e
      
                  ag^ωfor the same reason
      
                  ae^ω
  
          and therefore w must belong to L.
  
  
  To finish the determinisation construction in Theorem <ref>, it remains to show that condition (**) from the above lemma is a finite Boolean combination of languages recognised by deterministic automata. This will follow from the following lemma.
  
  
      For every  a,e ∈ S the property “(a,e) appears infinitely often” is recognised by a deterministic Büchi automaton.
  
  
      Let L ⊆Σ^* be the set of words which can be  decomposed as
      
      w = utype bvtype e    for some b ∈ S such that aeb=a.
  
      This is easily seen to be a regular language,
      and hence it is recognised by some finite deterministic automaton .  The deterministic automaton   recognising the property in the statement of the  lemma is defined as follows. Its space is the disjoint union of the set of types S and the states of  . 
      
      
      
      
      
      
      The initial state is the type in S of the empty word. The automaton   begins to read input letters, keeping in its state the   type of the prefix read so far in its state, until the prefix has  type ae. Then it switches to the initial state q_0 of the automaton . For states of , the  state update function of  is defined as follows:
      
      δ_(q,σ)  ↦δ_(q,σ)    if q is not accepting in 
      δ_(q_0,σ)    otherwise.
  
      The accepting states of  are the same as in . 
  
  
  
  
  This completes the proof of Theorem <ref>.
  
  
  
    
  *Semigroups for ω-words.
  There is an implicit algebraic structure in the proof of Theorem <ref>, which is formalised in the following definition.  
  
      An ω-semigroup[Section 7]PerrinPin95 consists  of:
      
          
    * two sets S and S, called the finite sort and the ω-sort, respectively.
          
    * a finite multiplication operation μ_+ : ( S)^+ → S,  which is associative  in the sense of  semigroups;
          
    * an ω-multiplication operation μ_ω :  ( S)^ω→S,  
          which is associative in the following sense:
          
      μ_ω(w_1 w_2  ⋯ ) = μ_ω(μ_+(w_1) μ_+(w_2) ⋯)     for every w_1,w_2,…∈ S^+.
  
      
  
  An example of an ω-semigroup is the  automaton types that were used in the proof of Theorem <ref>. Another example is the free ω-semigroup over  a set Σ, where the finite sort is Σ^+, the ω-sort is Σ^ω, and the two multiplication operations are defined in the natural way. The same proof as in Theorem <ref> shows that a language is ω-regular if and only if it is recognised by a homomorphism into an ω-semigroup which is finite (on both sorts). This is discussed in more detail in some of the exercises at the end of this section. 
  
  The  associativity axiom on ω-multiplication can be represented using a commuting diagram, in the same spirit as for Lemma <ref>:
     
      @C=5cm
          (( S)^+)^ω[r]^ω-multiplication in free ω-semigroup over S[d]_(μ_+)^ω   
           ( S)^ω[d]^μ_ω
       
           ( S)^ω[r]_μ_ω    S
  
   In the above diagram,  (μ_+)^ω denotes the coordinate-wise lifting of μ_+ to ω-words of finite words.
   
  
  
  
  
  
  
   
  Prove the following result, called  Ramsey's Theorem A [Theorem A]Ramsey29. Consider an infinite undirected graph, where every two 
  distinct vertices are a connected by an edge that is labelled by one of finitely many colours. Then the graph contains an infinite monochromatic clique,  which means that  there exists a colour e and an infinite set X of vertices, such that every two distinct  vertices from X are connected by an edge with colour e.  
  
      Let V_0 be all the vertices in the graph.
  Choose some vertex v_1 ∈ V_0. Since there are infinitely many vertices and finitely many colours, there must be some colour c_1 and an infinite set V_1 ⊆ V_0 -  v of vertices such that all vertices from X_1 are connected to v_1 by edges of colour c_1.  Repeat the same process for the graph restricted to vertices V_1, and keep on repeating it infinitely often, thus creating an infinite sequence
  
      V_0, v_1, c_1, V_1, v_2, c_2, ⋯
  
  By construction, for every i < j the edge connecting v_i with v_j has colour c_i. Some colour must appear infinitely often in c_1,c_2,… and if we restrict the sequence to coordinates i with c_i=c, then we get the monochromatic clique in the conclusion of the Ramsey Theorem.
  
  
  
   
  We say that an ω-word is ultimately periodic if it has the form wu^ω, for some finite words w,u ∈Σ^ω. Show that every nonempty ω-regular language contains an ultimately periodic ω-word.
    
      Take a nonempty nondeterministic automaton.
      If the automaton accepts some word, then there is an accepting state q, such that q is reachable from some initial state (via some finite word w), and q is also reachable from itself (via some finite word u). Then word wu^ω is accepted. 
  
  Show that two ω-regular languages are equal  if and only if they contain the same ultimately periodic ω-words.
      Suppose, toward a contradiction, that L and K are different languages that  are recognised by nondeterministic automata. Assume that L - K is nonempty. Thanks to Theorem <ref>, this difference is recognised by a Boolean combination of deterministic automata, and therefore by Theorem <ref> it is recognised by a nondeterministic automaton. By Exercise <ref>, the difference contains some ultimately periodic word.
  
  
  Show that an ω-word w is ultimately periodic if and only if w is an ω-regular language.The language wu^ω is easily seen to be recognised by a nondeterministic automaton, where the number of states is the total length of the words w and u. For the converse implication, we use Exercise <ref> to conclude that if a language recognised by a nondeterministic automaton contains a unique word, then that word is ultimately periodic.
  
  
   To an ω-word we associate an ordered model, in the same way as for finite words. Show that a language is definable (using the ordered model) if and only if it is ω-regular. 
  
  
  
  
  Define an ω-term  to be any tree as in the following picture:  
  39
  Every ω-term represents some ultimately periodic ω-word, but several ω-terms might represent the same ultimately periodic ω-word. 
  Show that two ω-terms represent the same ultimately periodic ω-word if and only if one can be transformed into the other using the equations:
  
      (xy)z = x(yz)      
      (xy)^ω = x(yx)^ω    (x^n)^ω = x^ωfor every n ∈1,2,…
  
  where x,y,z stand for ω-terms. 
  Using the equation (xy)z = x(yz), we can write finite words without indicating the order in which the multiplication operation is applied.  We say that an ω-term is  in normal form if has the shape wu^ω such that:
  
      
    * u is a prime word, which means that there is no decomposition of the form u = v^n for some n ∈2,3,…;
      
    * there is no suffix of w which is also a suffix of u.
  
  Using the equation (x^n)^ω = x^ω, we can convert every ω-term  into one which satisfies condition (1). Using the equation (xy)^ω = x(yx)^ω, we can convert every pair into one which satisfies condition (2), without violating condition (1). Therefore, using the identities we can convert every ω-term into one that is in normal form. For every ultimately periodic word, there is a unique ω-term in normal form that generates it. 
  
  
  
   Let L ⊆Σ^ω. Consider the following equivalence relations on Σ^+.
  
      
    * Right equivalence is defined by 
      
      w ∼ w'         wv ∈ L  w'v ∈ L  for every v ∈Σ^ω.
  
      
    * Two-sided congruence is defined by 
          
      w ∼ w'         uwv ∈ L  uw'v ∈ L  for every u ∈Σ^*, v ∈Σ^ω.
  
      
    * Arnold congruence is defined by 
              
      w ∼ w'     u(wv)^ω∈ L  u(w'v)^ω∈ L    for every u,v ∈Σ^*.
      
                          uwv ∈ L  uw'v ∈ L    for every u ∈Σ^*, v ∈Σ^ω.
           
  
  Show that the latter two, but not necessarily the first one,  are  semigroup congruences, i.e. they satisfy
  
      ⋀_i ∈1,2 w_i ∼ w'_i     implies     w_1 w_2 ∼ w'_1 w'_2.
  
  
  Consider the equivalence relations defined in Exercise <ref>.
  Prove that the arrows in the following diagram are true implications, and provide counter-examples the missing arrows:
  
      right
       congruence
       has finite index[r]    two-sided 
       congruence
       has finite index[l]
                   Arnold 
       congruence
       has finite index[l]   ω-regular[l]
  
  
  
  
  Define the Arnold semigroup of a language L ⊆Σ^ω to be the quotient of Σ^+ under Arnold congruence. Let L ⊆Σ^ω be a ω-regular.   Show that L is definable in first-order logic if and only if its Arnold semigroup is aperiodic.
  
  The temporal logic can also be used to define languages of ω-words.  Let L ⊆Σ^ω be a ω-regular. Show that  L is definable in if and only if its Arnold semigroup is suffix-trivial.
  
  
  Show an ω-regular language where the Arnold semigroup is infix trivial, but which cannot be defined by a Boolean combination of ∃^*-sentences. 
      Consider the language L ⊆0,1^ω of words which have 1 infinitely often. This Arnold semigroup of this language is (0,1,max) and is therefore infix trivial. This language 
  
  
  
  Define a safety automaton to be an automaton on ω-words with the following acceptance condition: all states in the run are  accepting. Show that deterministic and nondeterministic safety automata recognise the same languages. 
  
  
   Show that an ω-regular language of ω-words is recognised by a safety automaton (deterministic or nondeterministic, does not matter by Exercise <ref>) if and only if 
  
      uw^ v ∈ L   u(w^)^ω∈ L     for every u,w ∈Σ^+ and v ∈Σ^ω,
  
  where ∈1,2,… is the exponent obtained from the Idempotent Power Lemma as applied to the Arnold semigroup of L.
  
  
    For a finite alphabet Σ, we can view Σ^ω as metric space, where the distance between two different ω-words is defined to be
  
      1/2^(length of longest common prefix)
  
  This is indeed a distance, i.e. it satisfies the triangle inequality. 
  Let L ⊆Σ^ω be ω-regular. Show that L is recognised by a  safety automaton if and only if it is a closed set with respect to this distance.
  
  
   Find a condition on the Arnold semigroup of an ω-regular language which characterises the clopen languages (i.e. languages which are both closed and open with respect to the distance from Exercise <ref>)The clopen languages are the definite languages, and the condition is therefore a^ = a^ b.
  
  We use the topology from Exercise <ref>. Define a G_δ set to be any countable intersection of open sets. Show that every ω-regular language is a finite Boolean combination of G_δ sets. 
  
  
   Let L ⊆Σ^ω be an ω-regular language, and define    as in  Exercise <ref>. Show that L is recognised by a deterministic automaton if and only if:
      
      u (wv^)^ v^ω∈ L  ⇒ u(wv^)^ω∈ L     for every u,w,v ∈Σ^+.
  
  
  Let L ⊆Σ^ω. Define 
  an ω-congruence to be any equivalence relation ∼ on Σ^+ which is a semigroup congruence and which satisfies
      
      ⋀_i ∈1,2,… w_i ∼ w'_i     implies     w_1 w_2 ⋯∈ L  w'_1 w'_2 ⋯∈ L.
  
  Show that a language is ω-regular  if and only if it has an ω-congruence of finite index.
  
  
  Define semi-ω-congruence for a language L ⊆Σ^ω to be an equivalence relation on finite words which satisfies (<ref>), but which is not necessarily a semigroup congruence. Show that if there is a semi-ω-congruence of finite index, then there is an ω-congruence of finite index. 
  
  We say that ∼ is the syntactic ω-congruence of L ⊆Σ^ω if it is an ω-congruence, and every other ω-congruence for L refines ∼. Show that if a language is ω-regular, then  it has a syntactic ω-congruence, which is equal to the Arnold congruence. 
  
   
  Show a language of ω-words which does not have a syntactic ω-congruence. 
  
  
  
  
  
  § COUNTABLE WORDS AND CIRCLE-SEMIGROUPS
  
  
  In this section, we move to -words. These are  words where the set of  positions is a countable linear order. The positions could be some finite linear order, as in finite words, or  the natural numbers, as in ω-words, but  some dense set, like the  rational numbers.  One advantage of -words, as compared to ω-words, is that they can be concatenated, which is  useful when defining the corresponding generalisation of semigroups.
  
  For finite words, as well as for ω-words, the approach via semigroups can be seen as an alternative to existing automata models. This is no longer the case for -words. There  is no known corresponding automaton model, and therefore -semigroups are the only known model of recognisability.
  
  
      A Σ-labelled linear order consists of a set X of positions, equipped with a total order and a labelling of type X →Σ. 
  Two such objects are considered isomorphic if there is a bijection between their positions, which preserves the order and labelling. Define a   -word over  Σ  to be any isomorphism class of countable[Why do we assume countability? It turns out that  the decidability results described in this section breaks down for uncountable linear orders. In fact, the theory of the order of real numbers (ℝ, <)  is undecidable, as shown 
  [Theorem 7.]shelahMonadicTheoryOrder1975
  The  description of  -semigroups in this section is based on <cit.> and
  carton_colcombet_puppis_2018
  ] Σ-labelled linear orders. We write Σ^ for the set of -words[Formally speaking, this is not a set, because the linear orders form a class an not a set. However, without loss of generality we can use some fixed countably infinite set, e.g. the natural numbers, for the positions (but the order need not be the same as in the natural numbers). Under this restriction, the labelled linear orders become a set, and no isomorphism types are lost. For this reason, we can refer to Σ^ as a set. The same issue and the same solution appears in other places in this book, and we do not mention it explicitly any more.]. 
  
  
  
  
  Every finite word is a -word, likewise for every ω-word. Another example is labelled  countable ordinals, e.g. any -word where the positions are ω + ω. Here is a more fancy example, which uses a dense set of positions. 
  
   [Shuffles] 
      A classical exercise on linear orders is that the rational numbers are the unique – up to isomorphism – countable linear order which is dense and has no endpoints (i.e. neither a least nor greatest element).   The same argument, see below, shows that for every countable Σ there is a unique -word over Σ which has no endpoints, and which satisfies
      
      ⋀_a ∈Σ  ∀ x  ∀ y  ∃ z     x < z < y  a(z)label a is dense.
  
      We use the name shuffle of Σ for the above -word. Shuffles will play an important role in semigroups for -words. 
      
      In case the reader is not familiar with back-and-forth arguments, we explain why the shuffle is unique. 
      Define a finite partial isomorphism between two -words to be a bijection between two  finite subsets  of their  positions which respects the order and labels. Because shuffles have no endpoints and all labels are dense, we conclude the following property:
      
          
    (*) If f is finite partial isomorphism between two shuffles, and x is a position in the first (respectively, second shuffle), then f can be extended to a finite partial isomorphism whose domain (respectively, co-domain) contains x.
      
      Using the above property and countability, for every two  shuffles one can define  a sequence 
      
      f_0 ⊆  f_1 ⊆ f_2 ⊆⋯
  
      of finite partial isomorphisms such that every position is eventually covered by some f_n. The limit (set union) of these finite partial isomorphisms is an isomorphism between the two shuffles. 
  
   
  We now define the generalisation of semigroups for -words.  We use the approach to associativity via commuting diagrams that was described in Lemma <ref>.  Recall from that lemma  that a semigroup multiplication on a set S  could be defined as any operation μ : S^+ → S which makes the following diagram commute:
  
      S [dr]^identity[d]_ view a letter as 
        a one-letter word
      
               S^+ [r]_μ    S
                @C=4cm
          (S^+)^+ [r]^multiplication in the free semigroup[d]_μ^+   
           S^+ [d]^μ
       
           S^+ [r]_μ    S
  
   For -semigroups, we take the same approach: we begin by defining a free multiplication operation (which corresponds to multiplication in the free -semigroup), and then define other -semigroups in terms of that. For a set S,  define free multiplication  to be the operation (S^)^→ S^ which replaces each position in the input -word with the -word that is in its label (a formal definition uses a lexicographic product of labelled linear orders). 
  
   
  
   
       A -semigroup consists of an underlying set S  equipped with  a multiplication operation μ : S^→ S, which is  associative in the sense that the following two diagrams commute:
       
      S [dr]^identity[d]_ view a letter as 
        a one-letter -word
      
                  S^[r]_μ    S
                   @C=2cm
         (S^)^[r]^free multiplication[d]_μ^   
          S^[d]^μ
       
          S^[r]_μ    S
  
  In the above diagram, μ^ denotes the coordinate-wise lifting of μ to -words of -words.
   
  
   
       The free ∘-semigroup over alphabet Σ has Σ^∘ as its underlying set, and its multiplication operation is free multiplication. To check that this multiplication operation is associative, one needs to prove that the following diagram commutes:
       
      @C=3.5cm
              ((Σ^)^)^[r]^free multiplication  for alphabet Σ^[d]_ (free multiplication 
       for alphabet Σ)^   
               (Σ^)^[d]^ free multiplication
       
                for alphabet Σ
       
               (Σ^)^[r]_free multiplication for alphabet Σ   Σ^
  
       To prove this formally, one uses the formal definition of free multiplication, in terms of lexicographic products of linear orders (see Example <ref>). This -semigroup is called free for the usual reasons; a more formal description of these usual reasons will appear later in the book, when discussing monads. 
      
      
      
   
  
  
   
       Recall the semigroups of size two that were discussed in Example <ref>:
       
      (0,1, +)addition mod 2   (0,1, min)   (0,1, π_1)(a,b) ↦ a  (0,1, π_2)(a,b) ↦ b   (0,1, (a,b) ↦ 1)
  
          Which ones can be extended to -semigroups in at least one way?
      
          The first example, i.e. the two-element group, cannot be extended in any way, because the multiplication a of the  ω-word 1^ω would need satisfy
      
      a = μ(1^ω) =  μ(μ(1) μ(1^ω)) = μ(1a) = 1 + a.
  
      The remaining semigroups can be extended to -semigroups. As we will see in Example <ref>, the extensions are not necessarily unique.
   
  
  We use  -semigroups   to recognise languages of -words. Define a homomorphism of -semigroups to be a function h which makes the following diagram commute:
   
      S^[r]^h^[d]_multiplication in S   
             T^[d]^multiplication in T
      
             S [r]_h    T
  
  Like for semigroups, homomorphisms of -semigroup can be described in terms of compositional functions.  Suppose that S is a -semigroup and  T is a set, which is not yet known to have the structure of a -semigroup. We say that  a  function h : S → T is compositional if there exists a function μ : T^→ T which makes the following diagram commute 
  
      S^[r]^h^[d]_multiplication in S   
             T^[d]^μ
      
             S [r]_h    T
  
  Using the same proof as for Lemma <ref>, one shows that if h is a compositional and surjective, then μ is necessarily associative, thus turning  T into a -semigroup, and furthermore h is a homomorphism. A generalised version of this result, which works not just for -semigroups but also for a wider class of algebraic structures, will be proved in Part II of this book about monads. 
  
  We say that a language L ⊆Σ^ is recognised by a -semigroup S if there is a homomorphism h : Σ^→ S which recognises it, i.e.
  
      h(w)=h(w')   implies   w ∈ L  w' ∈ L     for every w,w' ∈ L.
  
  We are mainly interested in languages recognised by finite -semigroups, i.e. -semigroups where the underlying set is finite. Note that it is not immediately clear how to present the multiplication operation of a finite -semigroup in a finite way; this question will be addressed later in this section.
  
  
      Consider un-labelled countable linear orders, which can be viewed as  -words over a one letter alphabet a. 
      Consider the function 
      
      h :  a^→0,1
  
      which sends well-founded -words to 1, and the remaining -words to 0. 
      We claim that h compositional (and therefore the language of well-founded -words is recognised by a finite -semigroup). Indeed,  take some  v ∈ (a^)^ which gives w ∈ a^ under free multiplication. To prove compositionality, need to show that h^(v) uniquely determines h(w). This is because h(w)=1 if and only if the positions of v are well-founded, and every such a position is labelled by a well-founded order. All of this information can be recovered from h^(v). 
      The compositional function h induces an underlying structure of a -semigroup on 0,1. When restricted to finite multiplications, this -semigroup is the same as (0,1,min). Note that a symmetric -semigroup can be constructed, for orders which are well-founded after reversing. The symmetric -semigroup also coincides with (0,1,min) on finite words.
  
  
  
      Consider the language L ⊆a,b,1^, which contains -words where some position with label a is to the left of some position with label b. Consider the following function  
      
      w ∈a,b,1^  ↦  
              0    if w ∈ L
      
              1    if all letters are 1
      
              b    if all letters are b or 1, and there is some b
      
              ba    if both b and a appear, but w ∉L
      
              a    otherwise
  
      This function is easily seen to be compositional, and therefore its image is a -semigroup. The element 0 is absorbing, and 1 is a monoid identity. The language L is therefore recognised by the corresponding -semigroup.
     
   
  
  
  
  
  
  
  
   §.§ Monadic second-order logic on -words
   As usual in this book, we are interested in properties of -words that can be defined using . Part of the appeal of is the ease with which it can be applied to different setups (such as finite or infinite words, trees, graphs, etc.) and -words are no exception. It is immediately clar what for -words should mean. Define the ordered model of a -word  in the same way as for finite words: the universe is the positions, and the relations and their meaning are the same as for finite words. We say that a language L ⊆Σ^ is definable in if there is an sentence φ, using the vocabulary of the ordered model, such that
  
      w ∈ L     the ordered model of w satisfies φ    for every w ∈Σ^.
  
  
  
  
          Consider the language of well-founded ∘-words that was discussed in Example <ref>. This language is definable in , by simply writing in the definition of well-foundedness:
          
      ∀ Xfor every
       set of 
       positions    (∃ x ∈ X)which is nonempty⇒(∃ x  ∈ X    ∀ y ∈ X   x ≤ y)))there is a least position.
  
          Another example is the ∘-words which contain a sub-order that is dense:
          
      ∃ Xexists a
       set of 
       positions    (∃ x ∈ X)which is nonempty(∀ x ∈ X  ∀ y ∈ Y x < y  ⇒ ∃ z ∈ X  x < z < y)))and dense in itself.
  
          An -word which violates the second property, i.e. it does not have any dense sub-order, is called scattered.
  
  
  Once we have built up all the necessary ideas in the Trakhtenbrot--Elgot Theorem for finite words, it is very easy to get the extension for ∘-words. The same proof as for finite words (using a powerset construction on -semigroups) gives the following result. 
  
      If a language L ⊆Σ^ is definable in , then it is recognised by a finite -semigroup. 
  
  
  
  
  
  
  
  
  
  
  
  The  above theorem seems all too easy. Is there a catch? Yes: the theorem alone does not give an algorithm for deciding if an definable language is empty. In the case of finite words, we could remark that all of the constructions used in the proof (products and powersets) are effective, with finite semigroups represented by their multiplication tables. But, so far, we do not have any finite representation of -semigroups yet, and therefore we cannot talk about effectivity. Such finite representations, and their application to deciding , will be developed in the next section. 
  
  
  To see the difficulty in getting finite representations, consider uncountable words. Theorem <ref> remains true for uncountable words. However,  satisfiability of sentences over uncountable words (e.g. the reals)  is undecidable[Theorem 7.]shelahMonadicTheoryOrder1975. This means that for uncountable words the constructions in the lemma cannot be made effective. Hence, countability will play a crucial role in finding finite representations.
  
  Another interesting question is about the converse of the theorem: can one define in every language that is recognised by a finite -semigroup? For finite words and 
   ω-words, the answer was “obviously yes”, because one can use to formalise the acceptance by an automaton. Since we have no automata for -words, the question is harder. However, the answer is still “yes”, and it will be given in Section <ref>.
  
  
  
  
      Give a formula of which is true in some uncountable well-founded linear order, but is false in all countable well-founded linear orders.
  
      The property is that there exists some set of positions X which: (a) has order type ω; and (b) is co-final, which means that every element of the order is to the left of some element of X.  Condition (a) is expressed in by saying that X is the least set that contains some position x ∈ X, and is closed under taking successors inside X. 
  
  
  Find two countable ordinals (viewed as -words over a one letter alphabet), which have the same theory.   
  
  We write  for the reverse of ω. An ( + ω)-word is   a -word where the underlying order is the same as for the integers.
  Show that the following problem is decidable: given an sentence, decide if it is true in some bi-infinite word.   
  
  
  We say that a ( + ω)-word v is recurrent if every finite word w ∈Σ^+ appears as an infix in every prefix of v and in every suffix of v. Show that all recurrent ( + ω)-words have the same theory.
  
  
   Let Σ be an alphabet, and let x ∉Σ be a fresh letter.  For w ∈Σ^ and u ∈ (Σ∪x)^,  define u[x:=w] ∈Σ^ to be the result of substituting each occurrence of variable x in u by the argument w. For a language L ⊆Σ^, define contextual equivalence to be the equivalence relation on Σ^ defined by 
  
      w ∼ w'    iff    u[x:=w]∈ L  u[x:=w'] ∈ L  for every u ∈ (Σ∪ x)^.
  
  Show that ∼ is a -congruence (which means that the function that maps w to its equivalence class is compositional) for every language recognised by some finite -semigroup. 
  
  
  
  Give an example of a language L ⊆Σ^ where contextual equivalence is not a -congruence. 
  
  
    Show that every language recognised by a finite -semigroup has syntactic -semigroup, but there are some languages (not recognised by finite -semigroups), which do not have a syntactic -semigroup. 
  
   Consider a binary tree (every node has either zero or two children, and we distinguish left and right children), where leaves are labelled by an alphabet Σ.  The tree  might have infinite branches. Define the yield of such a tree to be the -word where the positions are leaves of the tree, the labels are inherited from the tree, and the ordering on leaves is lexicographic (for every node, its left subtree is before its right subtree). Show that every -word can be obtained as the yield of some tree.  
  
   Show that the following problems are equi-decidable:
  
      
    * given an sentence, decide if it
      is true in some -word w ∈Σ^
      
    * given an sentence, decide if its true in (,<).
  
   
  
    Assume Rabin's Theorem, which says that the theory of the complete binary tree
    
      (0,1^*, x=y0left
       child, x=y1right
       child)
  
   is decidable. Show that the problems from Exercise <ref> are decidable. (We will also prove this in the next section, without assuming Rabin's theorem.) 
  
  
  
  § FINITE REPRESENTATION OF 
  CIRCLE-SEMIGROUPS
  
   
     The multiplication operation in a finite semigroup can be seen as an operation of type S^+ → S, or as a binary operation of type S^2 → S. The binary operation has the advantage that a finite semigroup can be represented in a finite way, by giving a multiplication table of quadratic size.  In this section, we show that a similar finite representation is also possible  for -semigroups.  Apart from binary multiplication, we will use two types of ω-iteration – one forward and one backward – and a shuffle operation (which inputs a set of elements, and not a tuple of fixed length).
  
     
   For a -semigroup, define its operations[p. 109.]lauchli1966elementary to be the following four operations (with their types written in red).
      
      abbinary
       multiplication
      S^2 → S        a^ωmultiplication
       of aaa ⋯
      S→ S        a^multiplication 
       of ⋯ aaa
      S→ S        a_1,…,a_n^ηmultiplication of the
       shuffle of a_1,…,a_n
       S→ S
      
  
  
  The main result of this section is the following theorem, which says that the role played by the operations in a finite -semigroup is the same as the role played by  binary multiplication in a semigroup.
  A difference with respect to semigroups is that the operations are complete only for finite -semigroups, see Exercise <ref>. 
   
   
      The multiplication operation in a  finite -semigroup is uniquely determined by its operations.
   
  
   Another way of stating the theorem is that if S is  a finite set  equipped with  the operations, then there is at most one way of extending these operations to an associative multiplication S^→ S. We say at most one instead of exactly one, because the operations need to satisfy certain associativity axioms, such as:
  
      aa^ω = a^ω     (ab)^ω = a(ba)^ω    a_1,…,a_n^η = a_1,…,a_n^η^η
  
  Because the full list of axioms and its completeness proof are both long, we do not consider them here[They  can be found in
  [Section 7.]bloomEsik2005
  ]. This will not be a big issue,  because we will only consider  multiplication operations that arise from compositional functions – e.g. the multiplication operation on types of given quantifier rank k – and such  multiplication operations are guaranteed to be associative. 
  
   Find two  infinite -semigroups which have the same underlying set and the same operations, but  different multiplication operations. 
   
  The underlying set is + a^. 
  The first multiplication operation is
  
      μ_1(w) =     if w contains at least one 
      free multiplication of w   otherwise.
  
  The second multiplication operation μ_2 is defined in the same way, except that μ_2(w)= also holds  for all -words w which are not regular. Here a regular -words is one that can be generated using finitely many application of  the operations to finite words.
  
  
  
   §.§ Proof of Theorem <ref>
  
  
  The key idea in the proof of Theorem <ref> is that the operations are enough to generate all sub-algebras, as stated in the following lemma. 
  
   
       Let S be a finite -semigroup, and let Σ⊆ S. Then 
  
      multiplication of w : w ∈Σ^this is called the  sub-algebra generated by Σ⊆ S
  
  is equal to the smallest subset of S which contains  Σ and  is closed under the operations.
   
  
   Before proving the lemma, we use it to prove Theorem <ref>.
    Suppose that  S_1 and S_2 are two -semigroups, which have the same underlying set, and where the multiplication operations  agree on the operations. We will show that the multiplication operations are the same.  Consider the product -semigroup S_1× S_2, defined in the usual coordinate-wise way. Apply Lemma <ref>  to the diagonal 
       
      Σ = (a,a) : a ∈ S⊆ S_1 × S_2.
  
       Since the operations agree for S_1 and S_2, it follows from the  lemma that the sub-algebra generated by Σ is  also  the diagonal, which shows that the  multiplication operations of S_1 and S_2 are equal.
   
  
   The rest of Section <ref> is devoted to proving Lemma <ref>.
   Define L ⊆Σ^ to be the -words  whose multiplication  can be obtained from Σ by applying the operations.  To prove  Lemma <ref>, we need to show  L = Σ^.  This will follow immediately  from the following lemma. 
  
  
  
  
  
  
  
  
  
  
  
  
    Let S be a finite -semigroup, and let L ⊆ S^ be such that:
      
          
    *  if w_1,w_2 ∈ L then w_1 w_2 ∈ L;
          
    *  if w_1,w_2,…∈ L have the same multiplication, then w_1 w_2 ⋯∈ L;
          
    *  if w_1,w_2,…∈ L have the same multiplication, then ⋯ w_2 w_1 ∈ L;
          
    *  if w ∈ L^ is such that μ^(w) is a shuffle, then its free multiplication is in L.
      
      If L contains all letters in a subset Σ⊆ S, then L also contains Σ^.
  
  
  
  
   
      We begin with  some notation for -words. Define an interval in a -word to be any set of positions X  that is connected in the following sense:
      
      ∀ x ∈ X  ∀ y ∈ Y  ∀ z     x < z < y ⇒ y ∈ X.
  
      An infix of a -word is defined to be any -word that is  obtained by restricting the positions  to some interval.   For example, the rational numbers – viewed as a -word w over a one letter alphabet a^ –  have uncountably many intervals, but five possible infixes, namely a, w, aw, wa and awa.
      
  
      We now proceed with the proof of the lemma. Suppose that  L has all of the closure properties in the  assumption of the lemma. We say that   w ∈Σ^ is  simple if not only w, but also all of its infixes are in L.  We will show that  every -word in   Σ^ is simple, thus proving L=Σ^. For the sake of contradiction, suppose that w ∈Σ^ is not simple. 
         Define ∼ to be the binary relation on positions in w, which identifies positions if they are equal, or  the infix corresponding to the interval 
         
      z : x < z ≤ yan open-closed interval
  
         is  simple (where x is the smaller position and y is the bigger position).
         
      
          The relation  ∼ is an equivalence relation,  every equivalence class is an interval, and this interval induces a simple -word.
      
      
          The relation ∼ is symmetric and reflexive by definition. Transitivity holds because  simple words are closed under binary concatenation. This establishes that ∼ is an equivalence relation. Because  simple -words are closed under infixes by definition,   every equivalence class of ∼ is an interval. 
          
          It remains to show  that every (infix induced by an) equivalence class is  simple. Here we use countability and items <ref>–<ref> from the assumption of the lemma.  Consider an equivalence class X.  Choose some  position x ∈ X. We will show that both intervals
          
      y ∈ X : y > xY    y ∈ X : y ≤ xZ
  
          are simple, and therefore X itself is simple thanks to binary concatenation. We only consider the first interval Y, and Z is treated with a symmetric argument. 
  
          If Y has a last position, then it is simple  by definition of ∼. If there is no last position, than thanks to  countability we can decompose Y as a union
          
      Y = Y_0 ∪ Y_1 ∪ Y_2 ∪⋯,
  
          of consecutive open-closed intervals.  By definition of ∼, each interval Y_n is simple. Since L is closed under binary concatenation by assumption <ref>, also every finite union of consecutive intervals Y_n is simple. Therefore, by  the Ramsey Theorem, we can assume without loss of generality that all of the intervals Y_1,Y_2,… (but not necessarily Y_0) have the same multiplication  a ∈ S. It follows that Y_1 ∪ Y_2 ∪⋯ is simple, thanks to assumption <ref> about closure of L under ω-concatenation of -words with same multiplication. Finally, Y_0 can be added using binary concatenation, thus proving that Y is simple.
      
      Since the equivalence classes of ∼ are intervals, they can be viewed  as an ordered set, with the order inherited from the original order on positions in w.  Because  simple words are closed under binary concatenation, the order on equivalence classes is dense, since otherwise two consecutive equivalence classes would need to be merged into a single one.  Define w_∼∈ S^ to be the result of replacing every equivalence class of ∼ by its multiplication in S.  By assumption that w is not simple, ∼ has more than one equivalence class, and therefore the positions of w_∼ are an infinite dense linear order. 
  
          Some infix of  w_∼ is  a shuffle.
      
      
       Take some   a∈ S. If there is some infinite infix of w_∼ where no position is labelled by  a, then we can continue working in that infix (its positions  are still an infinite dense linear order). Otherwise, positions with label a are dense. By iterating this argument for all finitely many elements of S, we  find an infinite infix where every a ∈ S either does not appear at all, or is dense. This infix is a shuffle.  
      
         
       By the closure of L under shuffles, the free multiplication  of the  infix from the above claim is simple. It follows that the corresponding interval should have been a single equivalence class of ∼, contradicting the assumption.
  
  
  
  
  
   §.§ Decidability of MSO
  
  
  Thanks to Theorem <ref>, a finite -semigroup can be represented in a finite way, by giving its underlying set and the multiplication tables for its operations. We will use this representation to give decision procedure for on -words.
  
  Recall the proof of Theorem <ref>, which showed that every definable language is recognised by a finite -semigroup. We will show that the constructions in the proof can be made effective, with finite -semigroups being represented using  the operations.  In the proof of Theorem <ref>, we inductively transformed the formula into a recognising -semigroup,  starting from  -semigroups corresponding to the atomic relations in , and then  by applying products S_1 × S_2 and powersets S. It is not hard to find representations (in terms of operations) for the  -semigroups that correspond to the atomic relations. It is also easy to see that given representations of -semigroups S_1 and S_2, one can compute a representation of the product -semigroup S_1 × S_2, because the operations work coordinate-wise. The interesting case is the powerset construction, which is treated in the following lemma.
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
      Given a representation (using operations) of a finite -semigroup S, one can compute a representation of the   powerset -semigroup S. 
  
  
      In the proof, we  use lower-case letters a,b,c for elements of S, and we use  upper-case letters A,B,C for elements of the powerset S. We only show how to compute the multiplication table for the shuffle operation
      
      A_1,…,A_n  ↦  A_1,…,A_n^η
  
          in the powerset -semigroup S.
      The remaining operations are treated in a similar way.
  
      By definition of the powerset -semigroup, an element belongs to the set A_1,…,A_n^η if and only if it can be obtained as follows: take the -word 
      
      shuffle of A_1,…,A_n  ∈ ( S)^,
  
         choose for each position an element of its label, and then apply the multiplication operation of S. In other words, a belongs to A_1,…,A_n^η if and only if  there  exists a word 
          
      v ∈ (S × S)^η
  
          which satisfies the following properties:
          
              
    (a) after projecting v to the first coordinate and multiplying in S, the result is a;
              
    (b) in every letter of v, the first coordinate belongs to the second coordinate;
              
    (c) after projecting v to the second coordinate, the result is (<ref>).
          
           We will construct a homomorphism
          
      h : (S × S)^→ T
  
          that recognises the set of -words which satisfies conditions (b) and (c) above. The homomorphism h maps a -word v to the following information: (i) is condition (b) satisfied; (ii) is v an infix of some -word that satisfies (c); (iii) if v is a single letter, then what is the letter; and (iv) does v have a first/last position. The function h defined this way is compositional – and therefore it is a homomorphism – and  the operations on its image T can be computed. Also, the accepting set F ⊆ T can be computed, it consists of elements where the answers to questions (i) and (ii) are both “yes”, the answer to question (iii) is “not a single letter”, and the answer to question (iv) is “there is neither a first nor last position”. 
  
          By the above discussion, the set A_1,…,A_n^η that we want to compute consists of those elements a ∈ S that can be obtained by taking some v ∈ h^-1(F), projecting to the first coordinate, and then applying the multiplication operation of S. Here is alternative description of this set: take the subalgebra of S × T that is generated by 
          
      (b,h(b,B)) : b ∈ B ⊆ S  ⊆ S × T,
  
          keep only the pairs from this subalgebra where the second coordinate belongs to the accepting set F, and then project these pairs to the first coordinate. The alternative description  can be computed, because we can compute a representation of  the product -semigroup S × T, and we can compute  subalgebras  by saturating with respect to the operations thanks to Theorem <ref>. 
  
  
  
  Using the above lemma, we can  deduce decidability of over -words. 
  
  
      The following problem is decidable:
      
          
    Input. An sentence φ, which defines a language L ⊆Σ^.
          
    Question.  Is the language L nonempty?
      
  
  
      By induction on formula size, we compute for each formula of (possibly with free variables), a homomorphism into a finite -semigroup that recognises its language, together with an accepting subset of the -semigroup. The -semigroup is represented using the operations, and the homomorphism is represented by its images for the letters of the alphabet. In the induction step, we use Lemma <ref> to compute a finite representation of a powerset -semigroup.
  
  
  
  
  
  
  
  
   Let Σ be a finite alphabet, and let w be the shuffle of all letters in Σ. Show a finite -semigroup which recognises 
  the singleton language w. 
  
      The elements of the   -semigroup  are:    
      
      S = Σ∪ 0 ∪open,closed×open,closed.
  
  The idea is that we remember the following information: the entire word if  it is just a single letter, 0 if the word  cannot be extended to w, and otherwise  we only remember if there are left and right endpoints. This is formalised by the following function:
      
      v ↦
              a    if v consists of just the letter a
      (open, open)   if v=w
      (closed, open)   if v=aw for some a ∈Σ
      (open, closed)   if v=wa for some a ∈Σ
      (closed, closed)   if v=awb for some a,b ∈Σ
      
              0    otherwise.
  
  It is not hard to see that this function is compositional, and therefore  S  can be seen as -semigroup. The operations of S are defined as follows. The binary multiplication returns 0 for all arguments, with the following exception:
  
      (x,y)(x'one of these must not be “closed”,y') ↦ (x,y')     for x,y,x',y' ∈open, closed.
  
  The ω-power returns 0 for all arguments, with the following exception:
  
      (x,yone of these must not be “closed”)^ω = (x,open)      for x,y ∈open, closed.
  
  Reverse ω-power is defined symmetrically. Finally, the shuffle power is
  
      X^η = 
          0    if 0 ∈ X or X ⊊Σ
      (open,open)   otherwise.
  
  
  
  A -word w is called regular if the singleton language w is recognised by a finite -semigroup. Show that w is regular if and only if  it can  be constructed from the letters by using the operations.
  
  
   Show that every  nonempty definable language L ⊆Σ^ contains some regular -word.
  
  
  Show that if w is a regular -word, then w is definable (without invoking Theorem <ref>).
  
  
  Show that for every finite alphabet Σ there exists a -word w ∈Σ^ such that 
  
      h(wvw)=h(w)      for everyh: Σ^→ Shomomorphism into
       a finite -semigroupand v ∈Σ^.
  
  Shuffle of all regular -words in Σ^.
  
      
  For a countable linear order X, let a,b^X ⊆a,b^ be the set of  -words with  with positions X. We can equip this set with a probabilistic measure,  where for each position x ∈ X, the label is selected independently, with a and b  both having probability half. We say that X has a zero-one law if for every definable language L,  
  the probability of φ∩a,b^X is either zero or one. For which of the following X = , , is there a zero-one law?
  
  
  A countable linear order can be viewed as a -word over a one-letter alphabet. Among these, we can distinguish the countable linear orders that are regular, i.e. generated by the operations, see Exercise <ref>.  
      Give an algorithm, which inputs a 
  an countable linear order that is regular in the above sense, and decides if it has a zero-one law (in the sense of Exercise <ref>).
  
  
  Show that every definable language of -words belongs  to the least class of languages which:
          
              
    * contains the following  two languages over alphabet a,b,c:
              
      ∃ x a(x)some a    ∃ x  ∃ y   a(x)  b(y)  x < ya before b
  
              
    * is closed under  Boolean combinations;
              
    * is closed under images and inverse images of letter-to-letter homomorphisms.
          
      
  
  
  
  
  
  
  
  
  
  
  
  
   
  
  
  
  
  
  We say that a binary tree (possibly infinite) is regular if it has finitely many non-isomorphic sub-trees. Show that a -word is regular (in the sense  of Exercise <ref>) if and only if it is the yield (in the sense of Exercise <ref>) of some regular tree.
  
  
  
  
  [91]Consider the embedding ordering (Higman ordering)  w  v on -words. Show that for every -words w there is a regular -word v such that  w  v and v  w.  Hint: use Lemma <ref>. 
  
  
  Suppose that we are given a language L ⊆Σ^, represented by a finite -semigroup S,  a homomorphism h : Σ^→ S, and  an accepting set F ⊆ S.  
  Give a algorithm which computes the syntactic -semigroup (which exists by Exercise <ref>).
  
  
  
  
      
  Let  be a class of languages, such that  satisfies the following conditions:
  
      
    * every language in  is recognised by a finite -semigroup;
      
    *  is  closed under Boolean combinations;
      
    *  is closed under inverse images of homomorphisms h : Σ^→Γ^;
      
    * Let L ⊆Σ^ be a language in  . For every  w,w_1,…,w_n ∈Σ^,  contains the inverse image of L under the following operations: 
      
      v ↦ wv    v ↦ vw     v↦ v^ω   v ↦ v^    v ↦shuffle of w_1,…,w_n,v.
  
  
  Show that if L belongs to , then the same is true for every language recognised by its syntactic -semigroup.
  
  
  
  
      Let Σ be an alphabet and let c ∉Σ be a fresh letter. We say that  L ⊆Σ^ is definable in if there is a formula of which defines the language cL, see  Exercise <ref>. Give an algorithm which inputs the finite syntactic -semigroup of a language  L ⊆Σ^, and answers if the language  is definable in . Hint:  the -semigroup must be suffix trivial,  but this is  not sufficient. 
  Condition X says that the -semigroup is suffix trivial and satisfies the following identities 
      
      a^ = a^ω a= aa^     a_1a_1,…,a_n^η a_n = (a_1 ⋯ a_n)^
  
      
  
  
  
  Give an algorithm which inputs the finite syntactic -semigroup of a language  L ⊆Σ^, and answers if the language  is definable in two-variable first-order logic . Hint:  the -semigroup must be in ,  but this is  not sufficient.
  Condition X is the following identity: 
  
      (a_1 ⋯ a_n)^ = (a_1 ⋯ a_n)^ w (a_1 ⋯ a_n)^ω    for every w ∈a_1,…,a_n^.
  
  This identity can be effectively checked, since the possible multiplications of -words w can be computed using Lemma <ref>.
  
  
  
  
  Show that aperiodicity is not sufficient for first-order definability for -words: give an example of a language L ⊆Σ^ that is recognised by a finite aperiodic -semigroup, but which is not definable in first-order logic.  
  
  
  
  
   
  
  
  
  
  
  
  § FROM CIRCLE-SEMIGROUPS TO  MSO
  
  
  In Theorem <ref> we  have shown that if a language of -words is definable in , then it is recognised by a finite -semigroup. We now show that the converse implication is also true. 
  
   If a  language of -words is recognised by a finite -semigroup, then it is  definable in [This theorem was first shown in 
      [Theorem 5.1.]carton_colcombet_puppis_2018
  The proof presented here is different, and it is based on the proof in 
  [p. 192]Schutzenberger65 
  which shows that every aperiodic monoid recognises a star-free language. We use the different proof because, after suitable modifications, it allows us to characterise star-free languages of -words, see Exercise <ref>.].
  
  
  As mentioned before in this chapter, the theorem would be easy if there was an automaton model, which would assign states to positions, and where the acceptance condition could be formalised in . Unfortunately, no such automaton model is known. Therefore, we need a different proof for the theorem. The rest of Section <ref> is devoted to such a proof.
  
  We begin by defining regular expressions for -words. For a finite family  of languages  of -words, define the  shuffle of  to be the   -words which can be partitioned into intervals so that: (a) every interval induces a word from L for some L ∈; (b) the order type on the intervals is that of the rational numbers; and (c) for every L ∈, the intervals from L are dense. 
  
      Languages definable in are closed under Boolean combinations and the following kinds of concatenation:
      
      LK       L^+      L^ω     L^    shuffle of a finite family 
       of languages
  
  
  
      For the Boolean operations, there is nothing to do, since Boolean operations are part of the logical syntax. For the remaining operations, which are all variants of concatenation, we observe that can quantify over factorisations, as described below. 
      
      Define a factorisation of a -word  to be a partition of its positions into intervals, which are called blocks. For a factorisation,   define a compatible colouring to be any colouring of positions that uses  two colours, such that all blocks are monochromatic, and such that for every two distinct blocks with the same colour, there is a block between them  with a different colour. A compatible colouring always exists (there could be uncountably many choices). A factorisation can be recovered from any compatible colouring: two positions are in the same block if and only if the interval connecting them is monochromatic. A compatible colouring can be represented using a single set – namely the positions with one of the two colours. This representation can be formalised by an  formula φ(x,y,X) which says that positions x and y are in the same block of the factorisation (i.e. they have the same colour and they are not separated by any position with a different colour).
  
  
      Using the above representation, we show closure of under the concatenations in the lemma. For LK, we simply say that there exists  a factorisation with two blocks, where the first block is in L and the second block is in K. (To say that a block is in L or K, we observe that sentences can be relativised to a given interval.) For L^+, we say that there exists a factorisation with finitely many blocks, where all blocks are in L. Here is how we express  that there are finitely many blocks: there are first and last blocks, and there is no proper subset of positions that  contains the first block and is closed under adding successor blocks. For L^ω, we do the same, except that there is no last block. For L^, we use a symmetric approach. For the shuffle, we say that the blocks are dense and there is no first or last block.
  
  
  
  In the proof of Theorem <ref>, we will only use the closure properties of from the  above lemma. In particular, it will follow that every language recognised by a finite -semigroup can be defined by a regular expression which uses single letters and the closure operations from the lemma.
  
  To prove Theorem <ref>, we will show that the multiplication operation of every finite -semigroup can be defined in , in the following sense. 
  Let S be a finite -semigroup. We will show that  for every  a ∈ S, the language 
  
      L_a =  w ∈ S^ : w   has multiplication a
  
  is definable. This will immediately imply that every language recognised by a homomorphism into S is definable, thus proving the theorem.
  
  The proof is by induction on the  position of a in the infix ordering. Fix for the rest of this section an infix class J ⊆ S.  We partition S into two parts: 
  
      easy elementsproper prefixes of J  ∪  hard elementsthe rest.
  
  The induction hypothesis says L_a is definable for every easy  a ∈ S. We will prove the same thing for every  a ∈ J. 
  
  
  We begin with an observation about smooth multiplications, which follows from the Ramsey argument that was used in Theorem <ref>. We say that w ∈ S^ is J-smooth if the multiplication of every finite infix  w is  in J. This is a lifting to infinite words of the notion of smoothness that was used in Section <ref>. By the same proof as in  Claim <ref> from that section, a -word is J-smooth if and only if all of its infixes of length at most two are J-smooth.   The following lemma describes the multiplication of certain J-smooth words. 
  
    Let   e ∈ J be idempotent, and let  w ∈ J^ be J-smooth. If w is an ω-word, then its  multiplication is ae^ω, where a depends only on  e and the prefix class of  the first letter in w. If w is an   ( + ω)-word, i.e. its positions are ordered like the integers,  then  its multiplication is e^ e^ω. 
      
      Since the lemma is true for every choice of idempotent e ∈ J, it follows that e^ e^ω does not depend on the choice of e. In particular, 
      
      e^ e^ω = f^ f^ω
  
      holds for every two idempotents e,f in the same infix class.
         The main observation is the following claim. 
  
          
             If w is an ω-word that is  J-smooth and has first letter e, then its multiplication is  e^ω. 
          
          
              By  Lemma <ref>, the  multiplication of w is equal to  af^ω, for some  a,f. Since w is   J-smooth,    a and f belong to J. Since the first letter of w is e, we have ea=a.  Since f is infix equivalent to e, it admits a decomposition  f=xeey. Therefore 
              
      af^ω = ea(xeey)^ω = eaxe_g(eyxe_h)^ω.
  
              We now continue as in the proof of Lemma <ref>: because g,h,e are in the same group, then g^ω = e^ω = h^ω, and therefore gh^ω =e^ω.
          
  
      The claim immediately proves the lemma. Indeed, consider a J-smooth ω-word with first letter b. The first letter admits a decomposition b=aex for some a,x ∈ J, and furthemore a depends only on the prefix class of b.  By the above claim,  the multiplication of every J-smooth ω-word that begins with b  is equal to ae^ω. A similar argument works when the positions are ordered as the integers: every J-smooth ( + ω)-word has the same multiplication as a smooth ( + ω)-word with an infix ee, and the latter has multiplication e^ e^ω thanks to the claim and its symmetric version for .
      
  
      We say that a colouring
      λ : S^→ C which uses a fintie set C of colours 
       is definable on a subset L ⊆ S^ if there exists an definable colouring that agrees with λ on inputs from L.
      The strategy for the rest of the proof is as follows.
      Define L_J ⊆ S^ to be the -words that have multiplication in J. We first show in Lemma <ref> that the colouring
      
      w ∈ S^  ↦  prefix class of the multiplication of w
  
          is definable on L_J. Next, in Lemma <ref>,  we use this result about prefixes and a symmetric one for suffixes  to show  that the multiplication operation is definable  on L_J. Finally, in Lemma <ref> we show  that the language  L_J  is definable in . We  then conclude as follows: a -word has multiplication a ∈ J if and only if it belongs to L_J, and the colouring from Lemma <ref>  maps it to a.  It remains to prove the lemmas.
      
      
  
  
      The following colouring is definable on L_J: 
      
      w ∈ S^  ↦  prefix class of the multiplication of w.
  
  
  
      We write H ⊆ S^ for the -words which multiply to a  hard element, i.e. an element that is at least as big as J in the infix ordering, or incomparable with J. This language is definable in , as the complement of the language of -words that multiply to an easy element, which is definable by induction assumption. 
      For an interval in w, define its multiplication to be the multiplication of the infix of w that is induced by the interval. An interval is called easy if its multiplication is easy, otherwise it is called hard. The family of easy intervals is closed under subsets. By the induction assumption, we can check in if an interval is easy or hard.  An interval is called almost easy if it all of its proper sub-intervals are easy. 
      
          The multiplication operation is definable on almost easy intervals.
      
      
          If there is a last position, then the multiplication can be easily computed: remove the last position, compute the multiplication, and then add the last position. Otherwise, if there is no last position,  then we can use Lemma <ref> to see that  an almost easy interval has multiplication b ∈ S if and only if it belongs to 
          
      L_a (L_e)^ω    for some easy a,e such that ae^ω = a.
  
          The above condition can be formalised in thanks to the induction assumption and Lemma <ref>.
      
  
      Define a prefix interval to be a   interval that is downward closed in the ordering of positions. We will compute in the prefix class of some nonempty hard prefix interval; if the -word is in L_J then this hard prefix has the same  prefix class as  w. 
      Define X to be the union of all easy prefix intervals; if there is no easy prefix interval then this union is empty. This union is an almost easy interval, and therefore its multiplication, call it a, can be computed using Claim <ref>. If a is hard, then we are done, since a ∈ J by the assumption that w ∈ L_J, and therefore thanks to the Egg-box lemma we can conclude that the prefix class for the  multiplication of w is the same as for a.
  
      Suppose now that a is not hard. Define Y to be the suffix interval which is the complement of X.   By definition of a, if b is the multiplication of some prefix of Y, then ab is hard, and therefore by the same argument as in the previous paragraph, the prefix class of the multiplication of w is the same as that for ab. Therefore, it remains to compute in the multiplication of some (does not matter which one) prefix of Y.  If Y has a first position, then we can simply use the letter in that position. Otherwise, by the Ramsey Theorem, Y can be decomposed as a union of consecutive intervals 
      
      Y = ⋯∪ Y_2 ∪ Y_1 ∪ Y_0
  
      such that all of the intervals Y_1,Y_2,… have the same multiplication, call it c.
      If c is easy, which can be defined in thanks to the induction assumption and the closure properties from Lemma <ref>, we know that Y has a prefix which multiplies to c^. Otherwise, c is hard, and therefore by Lemma <ref>, we know that Y has a prefix which multiplies to e^, where e is some arbitrarily chosen idempotent from J.
  
  In the above lemma, we have shown how to compute in the prefix class of a -word, conditionally under the assumption that its multiplication is in J. A symmetric argument works for suffix classes. Now we use that result to compute the actual value, still conditionally under the assumption that the multiplication is in J.
  
      The multiplication operation of S is definable on L_J.
  
  
      Let w ∈ L_J. We use the terminology about intervals from the proof of Lemma <ref> .
      
      
      
      
          There exists a factorisation w = w_1 w_2 w_3 such that: 
          
              
    * w_1 is either empty or in H^ω;
              
    * w_2 is a finite concatenation of almost easy -words;
              
    * w_3 is either empty or in H^.
          
      
      Here is a picture of the factorisation, in the case when w_1 and w_3 are nonempty:
      40
      
          Define a  limit prefix of w to be any prefix interval which induces a -word in H^ω. 
          Limit prefixes are closed under (possibly infinite) unions. If there is a limit prefix, then there is a maximal one, namely the  union of all limit prefixes. Define w_1 ∈ H^ω to be the maximal limit prefix of w (if no limit prefix exists, then w_1 is empty).
          Remove  the prefix w_1, and to the remaining part of the word apply a symmetric process, yielding a suffix w_3 ∈ H^ and a remaining part w_2.  This is the factorisation in the statement of the claim. 
          
          It remains to show that w_2 is a finite concatenation of almost easy -words.
          By construction, the remaining part w_2 does not have any prefix in H^ω, nor does it have any suffix in H^.       Take the union of all easy prefixes of w_2 (this union is nonempty, because there must be some nonempty easy prefix of w_2 thanks to the assumption that w_2 has no  suffix in H^), and cut it off. After repeating this process a finite number of times, we must exhaust all of w_2, since otherwise there would be a prefix in H^ω.  Therefore, w_2 is a finite concatenation of almost easy -words.
      
      
      Let w_1,w_2,w_3 be as in the above claim. By Lemma <ref>, the multiplication of w_1 is uniquely determined by its prefix class (under the assumption that the entire -word belongs to L_J). Therefore, thanks to Lemma <ref>, we can compute in the  multiplication of the  w_1. Symmetrically,  we can compute the multiplication of w_3. It remains to compute the multiplication of w_2.  This is done in the following claim. 
      
      
          If a -word is a finite concatenation almost easy -words, then its  multiplication  can be computed in . 
      
      
          By the Kleene theorem about regular expressions being equivalent to finite automata, the set of finite concatenations of almost easy intervals can be described using a regular expression, where the atomic expressions describe almost easy words of that multiply to a given element. Such a regular expression can be formalised in thanks to Lemma <ref>
      
      
  
  
  
  
      The language L_J is definable.   
  
  
      Define I ⊆ S to be the hard elements which are not in J. This is an ideal in the -semigroup S, i.e. if w ∈ S^  has at least one letter in I, then its multiplication is in I. Define L_I to be the -words that multiply to an element of  I. Again, this is an ideal, this time in the free -semigroup S^. We will show how to define L_I in ; it will follow that L_J is definable as 
      
      L_J = H - L_I.
  
      The key is the following characterisation of L_I. Define an error to be a -word in S^ which satisfies at least one of the following  conditions:
      
          
    * binary error: belongs to   L_a L_b for some a,b ∈ S-I such that ab ∈ I; 
          
    * ω-error: belongs to   (L_a)^ω, for some a ∈ S-I such that a^ω∈ I; 
          
    * -error: belongs to (L_a)^, for some a ∈ S-I such that a^∈ I; 
          
    * shuffle error: is in the shuffle of   L_a_a ∈ A for some A ⊆ S-I such that A^η∈ I.
      
      Note that in the above definition, we can use languages L_a for a ∈ J. These languages are not yet known to be definable in , but they are conditionally definable in the sense used by Lemma <ref>.
  
       
          A -word belongs to L_I if and only if  it has an error  infix.
      
      
          Clearly every error is in L_I, and since L_I is an ideal, it follows that   every -word with an error infix is in L_I. We are left with the converse implication: every -word in L_I contains an error infix. 
          To prove this   implication, we will show that the language 
      
      L = w ∈ S^ : if w ∈ L_I  then w  has an error infix
  
      satisfies the assumptions of Lemma <ref>, with λ being the multiplication operation in S. The conclusion of Lemma <ref> will then say that  L  is equal to S^, thus showing that every -word in L_I has an error infix.
      
      The first assumption of Lemma <ref> says that L is closed under binary concatenation. Suppose that u,v ∈ L. We need to show that uv ∈ L. Suppose that uv ∈ L_I. If u ∈ L_I, then it has an error infix by assumption on u ∈ L, and therefore also uv has an error infix. We argue similarly if v ∈ L_I. Finally, if both u,v multiply to elements in S-I, then uv is a binary  error. 
  
      The remaining assumptions of Lemma <ref> are checked the same way. 
      
  
      As remarked before Claim <ref>, the definition of errors refers to languages L_a with a ∈ J, which are not yet known to be definable in . We deal with this issue now. 
      By Lemma <ref>, for every a ∈ J there an  definable language which contains all  -words that have multiplication a, and does not contain any -words that have  multiplication in J-  a. By removing the  -words with easy multiplications from that language, we get an definable language K_a with
      
      L_a ⊆ K_a ⊆ L_a ∪ L_I.
  
       Define a weak error in the same way as an error, except that K_a is used instead of L_a for a ∈ J. Since K_a is obtained from L_a by adding some words from the ideal L_I, it follows from Claim <ref> that a -word is in L_I if and only if it has an infix that is a weak error. Finally, weak errors can be defined by an expression which uses definable languages and the closure operators from Lemma <ref>, and therefore weak errors are definable. It follows that L_I is definable, and therefore L_J is definable.
  
  
  As we have already remarked when describing the proof strategy, the  above lemma completes the proof of the induction step in Theorem <ref>. Indeed, a -word has multiplication  a ∈ J  if and only if it belongs to L_J and it is assigned a by the colouring from Lemma <ref>.
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  The syntax of  star-free expression for -words is the same as for finite words, except that the complementation operation is interpreted as Σ^ - L instead of Σ^* - L. Define a -star-free language to be a language L ⊆Σ^ that is defined by a star-free expression. Show that if L is -star-free, then its syntactic -semigroup is aperiodic, but the converse implication fails.
  
  
  
  
  
  
  
  
  What is the modification for -star-free expressions that is needed to get  first-order logic (over the ordered model)?
      Instead of concatenation LK, we need to use marked concatenation LaK where a is a letter.
  
  
   
      Show that if L ⊆Σ^ is -star-free, then the same is true for every language recognised by  its syntactic -semigroup.
  
  
   Show that if L ⊆Σ^ is -star-free, then the same is true for L^ω. 
  
  
    Show that if S is aperiodic, then the constructions from Lemmas <ref> and <ref> can be done using -star-free expressions. 
  
  
   Show that  L ⊆Σ^ is -star-free if and only if its syntactic -semigroup is finite, aperiodic and satisfies[This exercise is based on 
  [Theorem 2, item 2.]colcombetSreejith2015
  ]:
  
      e^ = e = e^ω  ⇒   e =  e^η    for every idempotent e.
  
  Hint: use Exercises <ref> and <ref>.
  
  Show that languages of -words definable in first-order logic (in the ordered model) are not closed under concatenation LK. The singleton languages a^ω and a^ are definable in first-order logic, but their concatenation is not.
  
   We say that a multiplication operation μ : S^→ S is regular-associative if it satisfies the associativity condition from Definition <ref>, but with the diagrams restricted so that  only 
  
      S^∙ = w ∈ S^ : w is regular
  
  is used instead of S^. Show that if S  finite and μ : S^→ S is definable and regular-associative, then μ is associative. 
  
  
  
  Show that if S is finite and μ : S^∙→ S is regular associative, then it can be extended uniquely to an associative multiplication μ̅: S^→ S. Hint: the formulas  defined in the   proof of Theorem <ref> depend only on the operations of the -semigroup S.
  
      The formulas  defined in the   proof of Theorem <ref> depend only on the operations of the -semigroup S. Therefore, the construction in the theorem allows us to define, for  every μ : S^∙→ S, an definable colouring  λ : S^→ S. (Furthermore, if μ arose from an associative multiplication operation by restricting it to S^∙, then λ is equal to that multiplication operation.) 
  
      We claim that if μ was regular-associative, then λ agrees with μ on regular inputs. To prove this claim, we simply follow the proof of Theorem <ref>, but we use regular -words instead of all -words. 
  
      Once we know that λ agrees with μ on regular inputs, and μ is regular-associative, we can apply Exercise <ref>.
   
  
   
  
  PART: 
  Monads
  
  
    
  
  CHAPTER: MONADS
  
  
  As discussed in Chapter <ref>, instead of viewing a semigroup as having a binary multiplication operation, one could think of a semigroup as a set  S equipped with a  multiplication operation μ : S^+ → S, which is associative in the sense that the following two diagrams commute:
  
      S [dr]^identity[d]_ view a letter as 
        a one-letter word
      
             S^+ [r]_μ    S
                  @C=4cm
             (S^+)^+ [r]^multiplication in free semigroup S^+[d]_μ^+   
              S^+ [d]^μ
       
              S^+ [r]_μ     S
  
  The same is true for monoids, with * used instead of +, and  for -semigroups, with  used instead of +. In this chapter, we examine the common pattern behind these constructions, which is that they are the Eilenberg-Moore algebras for the monads of +-words, *-words and -words, respectively.
  
  From the perspective of this book, the idea behind monads is the following. Instead of first defining  not necessarily free algebras (e.g. semigroups) and then defining free algebras (e.g. the  free semigroup) as a special case, an opposite approach is used. We begin with the free algebra (which is  the monad), and then other, not necessarily free, algebras are defined as a derived notion (which is  the Eilenberg-Moore algebras of the monad). This opposite approach is  useful for less standard algebras such as graphs, where axiomatising the not necessarily free algebras is possible but tedious and not intuitive, while the free algebra is very natural, because it consists of graphs with a certain substitution structure. 
  
  
  
  
  § MONADS AND THEIR EILENBERG-MOORE ALGEBRAS
  
  This section, presents the basic definitions for monads and their algebras. These notions make sense for arbitrary categories. However,  for simplicity we use the category of sets and functions, because this is where most of our examples live. In later chapters we will consider multi-sorted sets (e.g. sets with sorts +,ω for ω-semigroups, or sets with sorts 0,1,… for hypergraphs), but multi-sorted sets  is as far as we go with respect to the choice of categories.
  
  
       A monad in the category of sets[The same definition can be applied to any other category, by using “object” instead of “set”, and  “morphism” instead of “function”.
       ]  consists of the following ingredients:
  
      
    * Structures:  for every set  X, a set   X;
      
    * Substitution:   for every function f : X → Y, a   function f :  X → Y;
      
    * Unit and free multiplication: for every set  X, two functions
      
      X : X → Xthe unit of X    _X :  X → Xfree  multiplication on X.
  
  
  These ingredients are subject to six axioms 
  
      (<ref>) is a functor        (<ref>)  (<ref>)unit and multiplication
       are natural transformations        (<ref>)  (<ref>)  (<ref>)X with free  multiplication
       is an Eilenberg-Moore algebra,
      
       and one more associativity axiom
  
  which will be described later in this section. 
  
  
  
  
  
  
  
  Before describing the monad axioms, we discuss some examples, and define Eilenberg-Moore algebras.  The purpose of the monad axioms  is to ensure that  Eilenberg-Moore algebras are well-behaved, and therefore it is easier to  see the monad axioms after the definition of  Eilenberg-Moore algebras. But even before that, we begin with  an example of the monad of finite words, where the Eilenberg-Moore algebras are monoids, to  illustrates what we want to do with monads.
  
  [Monad of finite words]
      The monad of finite words is defined as follows. The structures are defined by   X = X^*. For a function f : X → Y, its corresponding substitution  
      
      f :  X → Ycan also be written as 
      f^* : X^* → Y^*
       for this particular monad
  
      is defined by applying f to every letter in the input word.  For a set X, the unit operation of type
      
      X → XX → X^*
       for this particular monad,
  
      maps a  letter  to the one-letter word  consisting of this letter. Free  multiplication, which is a function of type 
      
      X → X(X^*)^* → X^*
       for this particular monad
      
      flattens a word of words into a  word. 
   The monad of -words is defined  in the same way, except that it uses  instead of *.
  
  
  For this book, the key notion for monads is Eilenberg-Moore algebras. The idea is that X describes the free algebra, while the Eilenberg-Moore algebras are the algebras that are not necessarily free.
  
  
       An Eilenberg-Moore algebra in a monad , also called a -algebra,  consists of an underlying set A and a multiplication operation μ :  A → A, subject to the following associativity axioms:
  
      A [dr]^identity[d]_ A
              
       A [r]_μ    A
                @R=2pc @C=6pc  A [r]^free multiplication on A[d]_μ    A [d]^μ
       A [r]_μ    A
  
  
  The above definition also makes sense for categories other than the category of sets, with A being an object in the category and μ being a morphism. In the definition, the reader will recognise, of course, the diagramatic definitions of semigroups, monoids and -semigroups. Also, the diagramatic definition of ω-semigroups will fall under the scope of the above definition, if we think about ω-semigroups as living in the category of sets with two sorts +,ω.
  
  By abuse of notation, we  use the same letter to denote a -algebra and its underlying set, assuming that the multiplication operation is clear from the context. Also, if the monad  is clear from the context, we will say algebra instead of -algebra.
  
  
  [Group monad]
      The free group over a set X is defined to be 
      
      ( X +  Xtwo copies of X,
       one blue, and one red)^*
  
       modulo the identities
  
      x  x =  x  x = ε    for every x ∈ X,
  
  where ε represents the empty word, while x and x represent the blue and red copies of x.
  The identities can be applied in any context, for example
      
      z  x      z    y   y   x       z   z   z  x  y    y,
  
      represent the same element of the free group.
      Define  to be the monad where X is the free group over X, and  the remaining  monad structure is defined similarly  as for finite words, except that we have the two copies of the alphabet, and the identities. The unit operation maps an element to its blue copy.
      
      An algebra over this monad is the same thing as a group.
  Indeed, if G is an  algebra over this monad, with multiplication μ, then the group structure is recovered as follows:
  
      1 μ(ε)group identity    x^-1μ( x)group inverse    x · y μ(xy)group operation
  
  The axioms of a group are easily checked, e.g. the axiom x · x^-1 is proved as follows:
  
      x · x^-1definition of inverse
      
          x ·μ( x) unit followed by multiplication is the identity, i.e. axiom <ref>
      μ( x) ·μ( x) definition of the group operation
      μ(μ( x)μ( x)) associativity of multiplication, i.e. axiom <ref>
      μ( x  x) equality in the free group
      μ(ε) definition of group identity
      
          1
  
  For the converse, we observe that for every group G, its group multiplication can be extended uniquely to an operation of type G → G, and the resulting operation will be associative in the sense required by Eilenberg-Moore algebras. 
  
  
  
  
  
  
   §.§ Axioms of a monad
  
  
  Having described some intuition behind monads and their Eilenberg-Moore algebras, we now describe the axioms of a monad.
  
  
  
  
  
  
    
  *Functoriality. The first group of axioms says that the first two ingredients (the structures and substitutions) of a monad are a functor in the sense of category theory. This means that substitutions  preserve the identity and composition of functions. Preserving the identity means that if  we apply  to the identity function on X, then the  result is the identity function on X. Preserving composition means that the composition of substitutions is the same as the substitution of their composition, i.e. for every functions f : X → Y and g : Y → Z, the following diagram commutes 
  
      @R=2pc  X [r]^ f[dr]_ (g ∘ f)    Y [d]^ g
          Z
  
   
  
  
    
  *Naturality.   The naturality  axioms say that for every function f :  X → Y, the following diagrams commute. 
  
      @R=2pc  X [r]^f[d]_ X    Y [d]^ Y
       X [r]_ f    Y
  
  
      @R=2pc  X [r]^ f[d]_free multiplication on X    Y [d]^free multiplication on Y
       X [r]_ f    Y
  
  In the language of category theory, this means that the unit and free multiplication are natural transformations. Also, as we will see later on, the second naturality axiom (naturality of free multiplication) says that the substitution  f is a homomorphism between the free algebras X and Y. 
  
  
  
    
  *Associativity. We now turn to the most important monad axioms, which ensure the  Eilenberg-Moore algebras are well behaved.  The main associativity axiom says  that for every set X, the set X equipped with free multiplication  on X is a -algebra (we call this the free -algebra over X, or simply free algebra if the monad is clear from the context). By unravelling the definitions, this means that the following two diagrams commute:
  
      @C=7pc X [dr]^identity[d]_ X
       X [r]_free multiplication on X    X
  
  
      @R=2pc @C=7pc  X [r]^free multiplication on X[d]_(free multiplication on X)    X [d]^free multiplication on X
       X [r]_free multiplication on X    X
  
  Apart from the above two, there is one more associativity axiom, namely:
  
      @C=7pc X [dr]^identity[d]_( X)
              
       X [r]_free multiplication on X    X
  
  
  
  
  
  
  This completes the axioms of a monad, and the definition of a monad.
  
  
   Consider a monad  in the category of sets. For a binary relation R on a set X, define 
  
      R^⊆ ( X) × ( X)
  
  to be the binary relation on X that is defined by
  
      R^ =    ((π_1)(t), (π_2)(t)) : t ∈ R  where π_i : X × X → X is the i-th projection.
  
  Does transitivity of R imply transitivity of R^?
  
  
  
  
   §.§ Homomorphisms and recognisable languages 
  
  
  A homomorphism between two -algebras is any function between their underlying sets which is consistent with  the multiplication operation, as formalised in the following definition. 
   Let  be a monad. A -homomorphism is a  function h : A → B on the  underlying sets of two -algebras A and B,  which makes the following diagram commute 
      
      @C=3pc  A [r]^ h[d]_multiplication in A    B [d]^multiplication in B
      
              A [r]_h    B
   
  
  When the monad is clear from the context, we simply write homomorphism, instead of -homomorphism.
  Again, the reader will recognise the notion of homomorphism for semigroups, monoids and -semigroups. 
  
  In the rest of this section, we describe some basic properties of homomorphisms.
  
  
      Homomorphisms are closed under composition.
  
  
      Consider two homomorphisms 
      
      A [r]^g     B [r]^h     C.
  
      Saying that the composition h ∘ g is a homomorphism is the same as saying that the perimeter of the following diagram commutes:
      
      @C=6pc A [dd]_multiplication in A[rr]^ (h ∘ g)[dr]_  g       C  [dd]^multiplication in C
          B [ur]_ h[d]_ multiplication in B
      
              A   [r]^g    B [r]^h    C
  
        The upper triangular face commutes because of the functoriality axioms (substitutions are compatible with composition). The left and right  triangular faces commute by assumption that g and h are homomorphisms.
  
  
  
  Recall that we defined the free algebra over a set X to be X equipped with  the free multiplication operation of type X → X. The monad axioms say that this is indeed an algebra. It is called free because of the universal property given in the following lemma.
  
  
      For every set X, the free algebra X has the following universal property:
      
      ∀∃!    @C=4cm 
                 X
                 @[red][r]^function on sets[dr]_- X   algebra A
          X@[blue][u]_homomorphism of algebras
  
  
  
  We begin by showing that there is at least one blue homomorphism h for each red function f; later we show that this homomorphisms is unique.  Let μ :  A → A be  multiplication in the algebra A. 
  Define h to be the composition of the following functions:
  
      X [r]^ f    A [r]^μ    A.
  
  The axiom on naturality of free multiplication says that f is a homomorphism from the free algebra X to the free algebra A. The associativity axiom in the definition of an Eilenberg-Moore algebra says that  multiplication μ is a homomorphism from the free algebra A to the algebra A. Thanks to Lemma <ref>, h is a homomorphism, as the composition of two homomorphisms f and μ. 
  
  We now show uniqueness – every  homomorphism h which makes the diagram must be equal to the one described above.  Consider the following diagram:
  
      @C=8pc     X [ddl]_ f[ddr]^identity[dd]_  X
      
       A [dr]_multiplication in A         X [l]_ h[r]^-free multiplication on X             X [dl]^ h
          A
  
  The upper left triangular  face commutes by applying   to the assumption that h extends f. (Applying   preserves commuting diagrams, because of the functoriality axioms.)  The upper right triangular face commutes by the first associativity axiom. The lower four-sided face commutes, because it says that  h is a homomorphism. Therefore, the perimeter of the diagram commutes. The perimeter says that  says that h must be equal to f followed by multiplication in A, and therefore h is unique. 
  
  
  
  
    
  *Compositional functions. Fix a monad . Suppose that A is an algebra, while B is a set, which is not (yet) equipped with a multiplication operation. We say that a function h : A → B on sets is compositional if 
  
      ∃μ    @C=4cm A  
             [r]^ h[d]_multiplication in A    B
             @[red][d]^μ
      
             A 
             [r]_h
              B
  
  This is the same notion of compositionality as was used for monoids, semigroups and -semigroups in part I of the book. For the same reason as before, surjective compositional functions are equivalent to surjective  homomorphisms, as stated in the following lemma.
  
  
      If A is an algebra, B is a set, and h : A → B is compositional and surjective, then there is a (unique) multiplication operation on B which turns it into an algebra and h into a homomorphism.
  
  
      The multiplication operation – no surprises here – is μ from the definition of a compositional function. 
      The diagram in the  definition of a compositional function is the same diagram as in the definition of a homomorphism, and therefore if  B equipped with μ is an algebra, then h is a homomorphism. It remains to show that B equipped with μ is indeed an algebra. We only prove the more interesting of the two  associativity diagrams, namely the one with a rectangular diagram.
  
      We first observe that  preserves surjectivity of functions[This part of the argument is true for the category of sets, and also for multi-sorted sets, but fails in general, since functors do not need to preserve epimorphisms in general categories.]. Indeed, if a function  h : A → B is surjective, then it has a one-sided inverse, i.e. a function  h^-1 : B → A such that h ∘ h^-1 is the identity on B. By the functoriality axioms,  h^-1 is a one-sided inverse for h, and therefore h is also surjective. This argument justifies the  surjectivity  annotation (double-headed arrows) in the  following diagram.
      
      @C=1.8cm B 
              [ddd]_μ[rrrr]^free multiplication on B             B[ddd]^μ
          A 
              @->>[ul]_ h[d]_ (multiplication in A)[rr]^free multiplication on A       A 
              [d]^multiplication in A@->>[ur]^ h
          A 
              [rr]_multiplication in A@->>[dl]^ h      
              A 
              @->>[dr]^h
              
       B 
              [rrrr]_μ            
              B
  
      The central  rectangular face commutes by the assumption that A is an algebra. 
  	The upper trapezoid face commutes by naturality of free multiplication.  The right and lower trapezoid faces commute by definition of a compositional function, and the left trapezoid face commutes by the same definition with   applied to it. It follows that all paths that begin in A and end in B denote the same function. Since h is surjective, it follows that the  perimeter of the diagram commutes. This proves the second of the associativity diagrams in the definition of an  Eilenberg-Moore algebra.
  
  
  
  
    
  *Recognisable colourings and languages.
  In this book, we are most interested in the Eilenberg-Moore algebras as recognisers of languages.  A language is a subset L of a free algebra Σ. (Typically we are interested in the case where the alphabet Σ is finite, but this assumption does not seem to play a role in the results that we care about, so we omit it.)   A language is called recognisable[
      The definition of recognisable languages for monads appears first in 
      [Section 11.]eilenbergAutomataGeneralAlgebras
      The above paper uses Lawvere theories, which correspond to finitary monads (see Section <ref>). The main result of <cit.>, Theorem III,  concerns free Lawvere theories, which correspond to the  monads described in Example <ref>, and says that recognisable languages for such monads can be described using least fix-points. With the exception of Example <ref>, none of the monads studied in this book are free.  
  ] if it is recognised by a finite algebra, as explained in the following definition (which uses a slightly more general notion of language, called colourings).
  
  
  
      Fix a monad . An algebra colouring is defined to be any function from an algebra to  a  set of colours[For some monads, it would be more useful to deviate from this definition. For example, in the monad from  Example <ref> that deals with vector spaces, a more useful notion of colouring is a linear map to the underlying field. Therefore, one could think of a parametrised  notion of recognisability, where  the notion of “algebra colouring” is taken as a parameter. Nevertheless, for all monads that are studied in more detail in this book, Definition <ref> is good enough.]. A  finite algebra is an algebra where the underlying set is finite[Like for algebra colourings, sometimes this notion of finite algebra is not the right one. In the monad from Example refex:monad-algebra-over-field, the more useful notion is that a finite algebra is one where the underlying set is a vector space of finite dimension. Again, one could think of the notion of “finite algebra” as being a parameter.]. An algebra  colouring L : A → U is called recognisable if it factors through a homomorphism into a finite algebra, as expressed in the following diagram:
      
      ∃    @C=2cm 
                 A
                 [r]^L 
                 @[red][dr]_-homomorphism     
      into a  finite algebra     U 
          B
               @[red][u]_algebra colouring
  
        Note that a recognisable colouring will necessarily use finitely many colours.
  
  
  
  A language can be viewed as the  special case of an algebra 
    where the algebra is a free algebra  and there are  two colours “yes” and “no”. For languages, we prefer set notation, e.g. we can talk about the complement of a language, or use Boolean operations for  languages.  The above definition is easily seen to coincide with the notions of recognisability for semigroups, monoids and -semigroups that were discussed in the first part of this book. In the next section, we give more examples.
   
  
  
  
  
  
  
  
  
  
  
  
  
  
      
  For an algebra A with multiplication operation μ :  A → A, define its powerset as follows: the underlying set is the powerset A, and multiplication is defined by 
  
      t ∈ A   ↦  μ(s) : s ∈^ t,
  
  where ∈^ is defined as in Exercise <ref>. Show an example of a monad  where this construction does not yield an algebra.
  
  
   Does the group monad satisfy the following implication:
  
      
    (*) If L ⊆Σ is recognisable, and h : Σ→Γ is a homomorphism, then h(L) is recognisable.
  
   What about surjective homomorphisms?
  
  
  
  
  Consider the 
  implication in the previous exercise. Show that even if we restrict h to functions of the form f for some surjective f : Σ→Γ, then the implication can still be false in some monads.
  
  
  
  
  
  § A ZILLION EXAMPLES
  
  
  Monads have an abundance of interesting examples. This section is devoted to   a collection of such examples, with an emphasis on the  algebras arising from the monads, and the languages  recognised by the finite algebras.
  
  
  
  
   §.§ Monads for words
  
  
   We begin with several examples of monads that study words, both finite and infinite. We have already discussed finite words in Example <ref>. The following example discusses infinite words, up to a fixed cardinality.
      
  
  [Chains]
      Define a chain  over a set X to be a  linear order with positions labelled by X, modulo isomorphism of labelled linear orders. 
      For an infinite cardinal  κ, define a monad     as follows. The set X consists of chains over X, which have cardinality strictly less than κ. For example, if κ is the first infinite cardinal ℵ_0 then the monad describes finite words, and if κ is the first uncountable cardinal then the monad describes -words. 
      The monad structure is defined in the same way as for finite words and  -words. Nevertheless, we give  a more exact description below. 
      
      For a function f, the corresponding substitution f is defined by applying f to the labels in the input chain and leaving the positions and ordering unchanged. The unit maps a letter  to the chain with one position labelled by that letter. 
      The free multiplication operation is defined using lexicographic products, as follows.
      Suppose that w ∈ X.  The positions in the free multiplication of w  are pairs (i,j) such that i is a position of w and j is a position in the label of position i in the chain w, call this label   w(i) ∈ X. The label of such a position is inherited from j, and the ordering is lexicographic. The cardinality of the resulting chain is at most   κ, since every infinite cardinal satisfies κ = κ^2.
      
  This is a monad.
      We only prove one of the monad axioms, namely
      
      @R=2pc @C=7pc  X [r]^free multiplication on X[d]_(free multiplication on X)    X [d]^free multiplication on X
       X [r]_free multiplication on X    X
  
      Let w ∈ X. If, in the diagram above, we first go right and then down, then the resulting linear order will have positions of the form ((i,j),k), where i is a position in w, j is a position in w(i), and k is a position in w(i)(j). If, in the diagram, we first go down and then right, then we get positions of the form (i,(j,k)), where i,j,k satisfy the same conditions as above. In both cases, the tuples of positions are ordered lexicographically, and the label is inherited from k. Therefore 
      
      ((i,j),k) ↦ (i,(j,k))
  
      is an isomorphism of labelled linear orders, and hence the two outcomes are equal as chains.
  
      
      
      If we take κ to be the first cardinal bigger than the continuum cardinal 𝔠,  then   describes chains of cardinality at most 𝔠. In this case, we have the following phenomenon. 
      Recall the  powerset construction that was described in Section <ref>. This construction also makes sense for chains of size at most 𝔠. Define  to be the least class of -algebras which contains the syntactic algebra of the language “every a is before every b”, and which is closed under  products and the powerset construction.  Using the same proof as in the Trakhtenbrot--Elgot Theorem and in   Theorem <ref>, one can show that every definable language L ⊆Σ is recognised by an algebra from . As we have mentioned on page <ref>,  satisfiability for over the reals is undecidable, and therefore there  is no finite way of representing algebras from . This means that the powerset construction over finite -algebras is not computable.
  
  
  In the above example, we consider all chains of given cardinality. One can also consider subclasses of chains, subject to some condition on the underlying linear order, as described in the following example.
  
  
      Consider a set  of linear orders which is closed under free multiplication as defined in the previous example, when  viewed as chains over a one letter alphabet. If we restrict  the monad from the previous example to chains where the underlying linear order is in , then we also get a monad. This construction yields the following monads (in all cases, we assume some fixed upper bound on κ on the cardinality, e.g. we can require countability):
  
      
    * well-founded words (the class of well-founded linear orders);
      
    * scattered words (the class of scattered orders, i.e. those into which one cannot embed the rational numbers)[Algebras for the monad of countable scattered words are studied in 
      rispalComplementationRationalSets2005
      ];
      
    * dense words (the class which contains two orders: a singleton order for units, and the rational numbers).
  
  
  
  
  [ω-semigroups]
      We now describe a monad that corresponds to ω-semigroups, see Definition <ref>. Since an ω-semigroup has two sorts, we leave the category of sets, and use instead the category  
      
      ^+,ω
  
      of sets with two sorts + and ω. 
      An object in this category is a set, where every element is assigned  exactly one of two sorts, called + and ω. We use the name sorted set for the  objects, for the purpose of this example.   A morphism in this category is any sort-preserving function between sorted sets.   We also use the following notation for sorts:
      
      Xa sorted set   =   X[+]elements
       of sort +  ∪   X[ω]elements
       of sort ω.
  
       Define a monad  over this category as follows. For a sorted set X, the sorted set  X is defined by:
      
      ( X)[+] = ( X[+])^+     ∪    
          ( X)[ω] = ( X[+])^* ( X[ω]) ∪ ( X[+])^ω.
  
      For a morphism f : X → Y, the substitution morphism f is defined in the natural way, by applying f to every letter. The unit and free multiplication are defined in the natural way as well. (An element of sort + in X is simply a finite nonempty word of finite nonempty words over X[+], and we can use free multiplication from the monad of finite nonempty words. On sort ω, there are more cases to consider, but the definition is natural as well. ) An Eilenberg-Moore algebra over this monad is the same thing as an ω-semigroup, as defined at the end of Section <ref>.
  
  
  
  
   §.§ Other monads
  
  We now present two monads – finite multisets and finite sets – which can be viewed as finite words modulo some equalities. Because these monads arise by imposing equalities on finite words,  their Eilenberg-Moore algebras for these monads are going to be special cases of monoids.
  [Finite multisets]
      Define X to be the finite multisets over X. We write finite multisets using red brackets like this
      
      x,x,y,y,y,z.
  
      A multiset is finite if it has finitely many elements, and each element appears finitely many times.  Functions are lifted to multisets point-wise, e.g.
      
      x_1,…,x_n     f↦    f(x_1),…,f(x_n).
  
      Another perspective on finite multisets is that they are finite words modulo commutativity xy=yx. 
      The unit is x ↦ x, and free multiplication is simply removing nested brackets, e.g.
      
      x,y, z↦x,y,z.
  
      This is a monad. An algebra over this monad is the same thing as commutative monoid. Recognisable languages over this monad are the same things are regular languages – in the usual sense – which are commutative, see Exercise <ref>.
      
      If we lift the restriction on finite supports, then we do not get a monad. The problem is with the substitutions: if  f : X → a is the constant function with an infinite domain, then there is no way to define 
      
      ( f) x_1,x_2,…infinitely many distinct elements.
  
      The problem is that the output multiset should contain a infinitely many times. To overcome this problem, we could allow multisets with infinitely many copies of an element. 
  
  
  [Idempotent finite words]
      Define X to be finite words X^*, modulo the equation ww=w. For example,
      
      abcababc = abc(ab)^2c = abcabc = (abc)^2 = abc = (ab)^2c = ababc.
  
      The remaining ingredients of the monad are defined in the natural way. An algebra over this monad is the same thing as an idempotent monoid, i.e. a monoid where all elements are idempotent.  Green and Rees show  that if X is a   finite set, then  X is finite[p. 35]green_rees_1952. It follows that for every finite alphabet, there are finitely many  languages over this alphabet, and all of them  are recognisable.
  
  [Powersets]
      The powerset monad, and its variant the finite powerset monad,  are defined in the same way as the multiset monad, except that we use sets (or finite sets) instead of multisets. The substitutions are defined via images (in the language of category theory, we use the co-variant powerset functor, as opposed to the contra-variant powerset functor, which uses inverse images):
      
      A ⊆ X      f↦    f(x) : x ∈ A ⊆ Y.
   
      Algebras over the finite powerset monad are the same thing as  monoids that are commutative and idempotent. If  X is a  finite set, then both powerset monads generate  finite sets; and therefore all languages over finite alphabets are recognisable. 
      
  
      
          [Terms]  Fix a ranked set Σ, i.e. a set where every element has an associated arity in 0,1,…. For example, we could have 
          
      Σ = aarity 2, barity 1, carity 0.
  
          Based on Σ, we define a monad _Σ as follows.
          Define _Σ X to be the terms over Σ with variables X, i.e. an element of _Σ X is a tree that looks like this:
          47
          The unit operation maps x ∈ X to a term which consists only of x. The substitution _Σ f is defined by applying f to the variables and leaving the remaining part of the term unchanged. Finally, free multiplication replaces each variable with the corresponding term. It is a simple exercise to check that an algebra over the monad _Σ  is the same thing as an algebra of type Σ, in the sense of universal algebra, i.e. it consists of an underlying set equipped,  with one operation for every letter in Σ[Definition 1.3]sankappanavar1981course. In the terminology of automata theory, both of these notions are the same as deterministic bottom-up tree automata over finite trees, where Σ is the input alphabet[Section 2]thatcherGeneralizedFiniteAutomata1968. From the above observation it follows that a language L ⊆ X is recognisable  in the sense of Definition <ref>  if and only if it is a regular tree language in the sense of automata theory[This monad describes finite trees. Finding an algebraic account for languages of infinite trees remains an open problem. This problem is discussed in the following papers:
          DBLP:journals/corr/abs-1808-03559
  DBLP:journals/lmcs/BojanczykK19
          ] , where the  input alphabet is  obtained from Σ by adding one letter of arity 0 for each element of X. If the ranked set Σ contains only letters of arity exactly one, then a -algebra can be seen as a deterministic word automaton with input alphabet Σ, without distinguished initial and final states.
      
  
  
  [Vector spaces]
      In this example, we discuss vector spaces over some field. For the sake of concreteness, we use the field of rational numbers.
      Define X to be the vector space, over the  field of rational numbers, where the basis is X. In other words, elements of X are finite linear combinations of elements from X with rational coefficients. For example, 
      
      3x + 7y - 0.5z ∈x,y,z.
  
      The action of  on functions is defined by 
  
      q_1 x_1 + ⋯ + q_n  x_n      f↦     q_1 f(x_1) + ⋯ + q_n f(x_n).
  
  The unit operation maps x ∈ X to the corresponding basis vector, and free multiplication is defined in the natural way, as illustrated in the following example:
  
      3(4x + 0.5y) - 0.2(5x -0.1y)    ↦   12 x + 1.5y - x + 0.02y = 11x + 1.52y.
  
  An algebra A over this monad, with multiplication μ, is also equipped with the structure of a vector space, because we can add elements
  
      a + b μ(a+b)
  
  and multiply them by scalars q from the field of rational numbers:
  
      qa μ(qa).
  
  If B ⊆ A is a basis for the vector space A, then the algebra A is isomorphic to B. Therefore,  over this monad,  every algebra is isomorphic to a free algebra. 
  
  
  
  [Algebra over a field]
      
      Define X to be finite linear combinations of words in X^*, with rational coefficients. For example, 
      
      2xyx + -2xx + 0.5xyz ∈x,y,z.
  
      We can view elements of this monad as polynomials with non-commuting variables.
      In other words, X =  (X^*), where  is the monad of vector spaces from Example <ref> and X^* is the monad of finite words[This is an example of a composite monad that arises via a distributive law of two monads. This type of construction was first described in
      [Chapter on distributive laws]appelgate1969seminar
       ].
      On functions, the monad acts as follows
      
      q_1 x_1 + ⋯ + q_n  x_n      f↦     q_1 f^*(x_1) + ⋯ + q_n f^*(x_n),
  
          where f^* is the substitutions in the monad of finite words. The unit maps x to the linear combination which has the one-letter word x with coefficient 1. Free multiplication is defined like for polynomials, but the variables are non-commuting, e.g.:
          
      3(4x -2y)(2xy+yy)     ↦   24xxy + 12xyy - 12yxythis is not 0 - 6yyy.
  
       Every algebra over this monad has the structure of a vector space over the rationals, but there is more structure (e.g. one can multiply two elements of the algebra)[Algebras over this monad are  known as “algebras over the field of rational numbers”, but we avoid this terminology due to the over-loading of  “algebra over”.]. 
  
       What is a recognisable colouring over this monad? In the  context of this monad (and also  the simpler monad of vector spaces from Example <ref>), it is more useful to work with different notions of “finite algebra” and “algebra colouring”: instead of finite algebras, one should consider finite dimensional algebras (i.e. those  where the underlying vector space has finite dimension), and instead of algebra colourings one should consider  linear maps to vector spaces. Under these adapted definitions, the algebra colourings recognised by finite algebras are exactly those which are recognised by  weighted automata, see Exercise <ref>. 
  
  
  
  
   Consider the monad _Σ from Example <ref>, where Σ is some ranked set (possibly infinite). Let X be some possibly infinite set of variables, and consider a set
  
      ⊆ (_Σ X) × (_Σ X)elements of this set will be called identities.
  
  For a set Y, define ∼ to be the least congruence on _Σ Y that satisfies 
  
      (_Σ f)(t_1) ∼ (_Σ f)(t_2)     for every (t_1,t_2)∈ and f : X → Y.
  
  (This congruence can be obtained by intersecting all congruences with the above property.) Define a new monad as follows: Y is equal to _Σ Y modulo ∼, and the remaining components of the monad are defined in the natural way. Show that this is a monad.
  
  
  
  
  
  For monads  and , define a monad morphism from  to  to be a family of 
  functions
  
      δ_X :  X → X_X is a set
  
  which is subject to the axioms in Figure <ref>. Using the monads from  Section <ref>, give  five examples of monad morphisms, and five examples of pairs of monads which do not allow a monad morphism.
  
  
   We say that w ∈Σ  is regular  if   w is a recognisable language. Find a monad where there are no regular elements. (Hint: it appears in this section.) 
  Groups. Consider a  homomorphism h from the free group Σ to some finite group G. If n ∈1,2,… is the order of the group G, then 
  
      h(w) = h(w^n+1)     for every w ∈Σ,
  
  and therefore h cannot recognise any singleton language.
  
  
  What is the monad for rings (commutative and non-commutative)? Semirings? 
  
  Consider the following monad . The set X is the set of ω-words X^ω, and the substitution f :  X → Y is defined coordinate-wise. The unit is x ↦ x^ω, and free multiplication  is defined by 
  
      w ∈ X     ↦     (i ↦(w[i])[i]i-th letter of
       of i-th letter of w).
  
  Show that this is a monad. Also, show that a language  L ⊆Σ is recognisable if and only if it is clopen in the sense of Exercise <ref>.
  
  
   Let  be the monad of countable well founded chains. Show that a finite algebra with universe S  is uniquely determined by the operations
  
      abbinary
       multiplication
      S^2 → S    a^ωmultiplication of
      aaa ⋯
      S→ S
    
  
  
   Consider 
  the monad from Exercise <ref>. Show that a language L ⊆Σ is definable in first-order logic (in the ordered model) if and only if it is recognised by a finite -algebra S where the underlying semigroup is aperiodic. 
  
  
  
  
  
   Consider 
  the monad from Excercise <ref>. Consider regular expressions defined by the usual operators, plus L^ω. Show that these expressions do not describe all recognisable languages. 
  
  The set of all countable well-founded orders, i.e. the full language a,  is not described by any regular expression (of the form in the exercise). The issue is that the regular expressions have bounded nesting of ω, which is not true for countable well-founded orders. More precisely, define words as follows:
  
      w_0 = a      w_n+1= (w_n)^ω.
  
  By induction on n, one shows that there is no regular expression with ω-height n which contains w_n+1. Here, the ω-height is the nesting depth of ω (the other operators are not counted).
  
  
  Consider the monad and regular expressions from Exercise <ref>. Give an effective condition on finite algebras which corresponds exactly to the Boolean combinations of regular expressions. 
  There is a ranking function r : A →0,1,… such that r(a)=0 if and only if a is an absorbing zero element, and 
  
      r(a^ω) < r(a)     for all a with nonzero rank.
  
  
  
    Let  be the monad of countable scattered chains. Show that a finite algebra with universe S  is uniquely determined by the operations
  
      abbinary
       multiplication
      S^2 → S    a^ωmultiplication of
      aaa ⋯
      S→ S    a^multiplication of
      ⋯ aaa
      S→ S
    
  
  
  Consider the monads from Examples <ref> and <ref>. In which of these monads is first-order logic (over ordered models) equivalent to star-free expressions?
  
  
  Consider the monad from Example <ref>. Which class of languages corresponds to aperiodicity (of the semigroup underlying the -algebra)?
  
  
   A weighted automaton over the rationals consists of:
  
      Σinput alphabet,
       which is a 
       finite set    Sstate space,
       which is a 
       vector space of
       finite dimension    s_0 ∈ Sinitial state    δ_a : S → S_a ∈Σstate updates,
       which are 
       linear maps    F : S →final function,
       which is a  
       linear map
  
  The semantics of this automaton is a function of type Σ^* → defined as follows. Given an input word, do the following: start with the initial state, apply the state update for the first letter, then the state update for the second letter, and so on for all letters, and at the end apply the final function.  The semantics of a weighted automaton can also be naturally extended to finite  linear combinations of words over Σ, i.e. to elements of the Σ as in Example <ref>. 
  
  
  Show that  L : Σ→ is recognised by a weighted automaton if  and only if it is recognised by a finite dimensional -algebra.
  For the left-to-right implication, suppose that  is a weighted automaton, with state space S.  Define A to be the set of linear maps S → S, viewed as matrices. This is a -algebra, with the multiplication defined in the natural way (using composition of linear maps and multiplying them by rational scalars).  It has finite dimension, namely the square of the dimension of S. Consider the homomorphism 
  
      h : Σ→ A,
  
  which is the unique extension of a ∈Σ↦δ_a. This homomorphism recognises the language L, because 
  
      L(w) =   F(h(w)(initial state of ))a linear map, assuming that the argument is h(w)
  
  
  We now prove the right-to-left implication. Suppose that L : Σ→ is recognised by  a homomorphism of -algebras 
  
      Σ[dr]_L[r]^h    A[d]^F
  
  A weighted automaton is defined as follows. The state space is A. The initial state is the multiplication of the empty empty word in A. The final function is F.
  
  
  
  
  
    
  
  
  
  
  § SYNTACTIC ALGEBRAS
  
  
  In this section, we show that if an algebra colouring is recognisable, then it has a syntactic homomorphism, i.e. a recognising homomorphism that stores the minimal amount of information[The results of this section, with the exception of Section <ref>, are based on 
  [Part I.]bojanczykRecognisableLanguagesMonads2015
  ]. 
  
  
   Fix a monad  in the category of sets[This definition also makes sense for monads in other categories, assuming that one  one interprets “surjective functions” as “epimorphisms”. However, the results in this book about the existence of syntactic homomorphisms will depend on the category of sets, and its generalisation to sorted sets.].
       The syntactic homomorphism of an algebra colouring L : A → U is any   surjective  homomorphism 
      
      h :A →  B
  
       which recognises L and which  is minimal in the sense explained in the following quantified diagram
      
      ∀∃!     @C=2cm 
               A
               @->>[r]^h 
               @->>@[red][dr]_-surjective    
      homomorphism 
      that recognises L    B 
          C
             @[blue]@->>[u]_homomorphism
  
  
  
  The algebra used by the syntactic  homomorphism is called the syntactic algebra.  The syntactic algebra, if it exists,  is unique up to isomorphism of algebras. Also the syntactic homomorphism is unique in the following sense: every two syntactic homomorphisms will have the same kernel (equivalence relation on A that identifies two elements with the same homomorphic image). This unique kernel is called the syntactic congruence of L. 
  
  We are mainly interested in the case where the algebra colouring describes a language, i.e. A is a free algebra, and there are two colours “yes” and “no”.
  
  There are two main results in this section. The first one, Theorem <ref>, says that if an algebra colouring  is recognisable,   then it has a syntactic homomorphism.  In general, colourings that are not recognisable need not have syntactic homomorphisms. The second one, Theorem <ref>, says that a monad is finitary (roughly speaking, this means that all structures described by the monad are finite) if and only if every (not necessarily recognisable) algebra colouring has a syntactic homomorphism.  To illustrate these theorems, we begin with an example of an algebra colouring that does not have a syntactic homomorphism. In light of Theorems <ref> and <ref>, the example uses a colouring that  is  not recognisable and a  monad that is not finitary.
   
      Consider the monad of -words. (As we will see later, this monad is not finitary.) Define L to be the set of  -words over a one letter alphabet a which contain every finite word as an infix. More formally, 
      
      L=   w ∈a^ : a^n is an infix of w for every n ∈0,1,….
  
       This language is not recognisable, because all finite words must have different images under any recognising homomorphism (we leave this as an exercise for the reader).  We will show that L does not have a syntactic homomorphism.
      For n ∈1,2,…, define 
      
      w_n = (shuffle of a)· a^n ·(shuffle of a).
  
      Define  h_n to be the function 
          
      w ∈a^  ↦  
                  w_n    if w=w_n+1
      
                  w    otherwise.
  
          This function is  compositional for every n, and therefore it can be viewed as a homomorphism.
      If there would be a syntactic homomorphism, then it would need to factor through h_n. Since h_n gives the same result for w_n and w_n+1, therefore the same would have to be true for the syntactic homomorphism.   Therefore, the syntactic homomorphism h, if it existed, would need to give the same result for all -words w_1,w_2,….
          By associativity, we would have 
      
      h(w_1 w_1 w_1 ⋯∉L)=h(w_1 w_2 w_3 ⋯∈ L).
  
       Therefore the syntactic homomorphism does not exist.
  
  
  
  
  
  
  
  
  
   §.§ Terms and congruences
  
  
  To construct the syntactic homomorphism, we will use classical notions from universal algebra, such as terms and congruences, adapted to the monad setting. These notions and their basic properties are described below.
  
  
  
  
    
  *Terms. If X is a set, possibly infinite, then  a term over variables X is defined simply to be any element of X. Given an algebra A, a term t ∈ X is interpreted as the following operation
      
      η∈ A^X   ↦  multiply (η) (t) in Awe write t A for this operation.
  
      Arguments of t^A are called  variable valuations. An operation of the form t^A, for some t, is called a term operation[
          If we are working in a category other than the category of sets, then the term operation t^A : A^X → A is not necessarily a morphism in the category. For example, in  a category of sorted sets with at least two sorts, the set A^X is not a sorted set.
      ] in the algebra A. We distinguish between a term (which can be viewed as syntax) and the term operation that it generates in a given algebra (which can be viewed as semantics). If a term uses a finite set  of  n variables with some implicit ordering, then  we write 
      
      t A (a_1,…,a_n)
  
      for the result of applying t A to the variable  valuation which maps the i-th variable to a_i. 
      
  
      Consider the monad of finite words. The word xy is a term, and the term operation induced it in an algebra (which is the same thing as a monoid) is binary multiplication. The operation induced by the term ε, which has an empty set of variables, is the constant that represents the monoid identity. Another example of a term operation is squaring, which is given by the term xx. A non-example is the idempotent power operation a ↦ a^. This is not a term operation, because the number  depends on the algebra at hand (also, this number does not  exist in some infinite algebras).
      
      In the monad of -chains, the operations of ω-power and -power are term operations which arise from the univariate terms  x^ω and x^. To model  shuffling, we use an infinite family of terms, with the n-th one being the shuffle of x_1,…,x_n.
  
  
  Term operations commute with homomorphisms, as shown below.
  
      If h : A → B is a homomorphism, and t ∈ X is a term, then the following diagram commutes:
      
      @C=1.5cm
              A^X
              [r]^η↦ h ∘η[d]_t^A   
              B^X
              [d]^t^B
      
              A [r]_h     
              B
          .
  
  
   Consider a term t ∈ X and a valuation η∈ A^X. We show below that if we start in a valuation η∈ A^X, and follow the down-right and the right-down paths in the diagram from the statement, then we get the same element of B.
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      down-right path applied to η    =
      
          h(t^A(η)) 
          definition of t^A
      
          h( (multiplication in A)(η))
          h is a homomorphism
      (multiplication in B)(h ∘η))
           is a functor
      (multiplication in B)((h ∘η))
          definition of t^B
      
          t^B(h ∘η)     =
      right-down path applied to η.
  
  
  
  
  
  
    
  *Congruences. Define a  congruence in an algebra A to be an equivalence on the underlying set that satisfies any of the equivalent conditions in the following lemma.
  
      
      Let A be an algebra. For every equivalence relation ∼ on its underlying set, the following conditions are equivalent:
      
          
    *  the function which maps a ∈ A to its equivalence class is compositional;
           
    * ∼ is the kernel of some homomorphism from A to some algebra B;
          
    *  ∼ commutes with  every term operation,  which means that:
          
      η_1 ∼η_2η_1(x) ∼η_2(x)
       for every x ∈ X  ⇒   t^A (η_1) ∼ t^A (η_2)      for every t ∈ X and η_1,η_2 ∈ A^X.
  
      
  
  
      The implication <ref>⇒ <ref> follows from Lemma <ref> which says that compositional functions are the same as homomorphisms. The implication <ref>⇒<ref> follows from Lemma <ref>, which says that term operations commute with homomorphisms. The implication <ref>⇒<ref> follows from the definition of compositional functions. 
  
   By condition <ref>, every congruence induces a  quotient algebra, where the universe is equivalence classes. 
  
      
  
      
  
  
  
       Fix a monad in the category of sets.  Consider a set of terms . We say that  is a term basis if for every finite algebra A and subset Γ⊆ A, the sub-algebra generated by Γ is equal to the least subset of A that contains Γ and which is closed under applying term operations corresponding to terms  from .  Show that if  is a term basis, then a finite algebra A is uniquely determined by its -multiplication tables, which  is the family of term operations t^A_t ∈.
  
  
  
  
  
  Let  be a monad which has a finite term basis .  Show that given the -multiplication tables in an algebra A, one  can compute the -multiplication tables of the powerset algebra A (as defined in Exercise <ref>, assuming that A is indeed an algebra). 
  
  Find a notion of computable term basis which generalises the previous exercise so as to capture the operations in the monad of -words.
  
  
  
      
      Consider a monad with a term basis . Show that ∼ is an equivalence relation on the underlying set of a finite algebra, then ∼ is a congruence if and only if it commutes with all term operations from the term basis . 
  
      Let  be the term basis, let  A be a finite algebra, and let ∼ be an equivalence relation on its universe. Define 
      
      B ⊆ A × A
  
      to be the subalgebra of A × A that is generated by ∼ when viewed as a set of pairs. By definition of subalgebras,  B is the set of values of the form 
      
      t^A × A(η),
  
      where t ranges over terms of the form t ∈ X for some set of variables X, and η ranges over valuations from X to the the set of pairs ∼.   For i ∈1,2, let 
      
      π_i : A × A → A
  
      be the projection onto the i-th coordinate. Since this is a homomorphism, and term operations commute with homomorphisms thanks to Lemma <ref>, we have 
      
      π_i(t^A × A)(η) = t^A(π_i ∘η).
  
      Therefore, B is equal to the set of pairs
      
      (t^A(π_1 ∘η), t^A(π_2 ∘η))
  
      where the ranges of  t and η are the same as when describing B. Since every pair of valuations
      
      η_1, η_2 : X → A
  
      which are pointwise equivalent under ∼ can be described as π_1 ∘η and π_2 ∘η for some η, it follows that 
      
      B = (t^A(η_1),t^A(η_2)): t ∈ X  and η_1 ∼η_2 ∈ A^X.
  
      If we use the definition of congruences as equivalence relations that commute with all term operations, then ∼ is a congruence if and only if B ⊆∼.   
      
      Since  is a term basis, it follows that B is also the smallest subset of A × A which contains all pairs from ∼ and which is closed under applying all term operations from . By assumption that ∼ commutes all term operations from the basis, it follows that B ⊆∼. 
  
  
  
   Define a vectorial term to be any function
  
      f : 
       Youtput 
       variables→Xinput 
       variables.
  
  For  an algebra A and a vectorial term f as above, define
  
      f^A : A^X → A^Ysuch a function is called 
       a vectorial term operation
  
  to be the function which maps  η∈ A^X to the  following function:
  
      Y 
              [r]^f    X
              [r]^η    A
              [r]^μ   
          A.
  
  Show that  vectorial term operations are closed under composition.
  
  
      Closure under composition is proved in the following claim.
  
    
      For every vectorial terms 
      
      g : Z → Y      f : Y → X
  
      there is a vectorial term 
      
      g · f  : Z → Xthis is called the Kleisli composition of g and f
  
      such that the following diagram commutes for every algebra A:
      
      A^X 
              [r]^f^A[dr]_(g · f)^A   
              A^Y 
              [d]^g^A
          A^Z
  
      
  
  
      The Kleisli composition g · f from the statement of the claim is defined to be the composition of the following functions:
      
      Z [r]^g
                  Y
              [r]^ f    X
              [rr]^free multiplication       X
  
  Consider a valuation η : Z → A. To prove that applying (g · f)^A to η gives the same result as applying first f^A and then g^A, as required in the statement of the claim, consider the following diagram. 
  
      @C=1.7cm
          
              X 
              [r]^g
              [drr]_g · f    Y
               [r]^ f@/^3.0pc/[rrr]^ (f^A(η))    X 
              [r]^η[d]^ free 
      
              multiplication    A 
              [r]^μ[d]^ free 
       multiplication    A 
              [d]^μ
             X
              [r]_η    A
              [r]_μ    
              A
  
  The bottom-most path from X to A describes the result of applying (g · f)^A to the valuation η, while the top-most path describes the result of applying first f^A and then g^A. Therefore, to prove the lemma, it is enough to show that the diagram commutes.  The top face in the diagram (incident with the curving arrow) describes the definition of f^A, with the functor  applied to it. The left-most face is the definition of Kleisli composition.  The left rectangular face  is naturality of free multiplication, which is axiom (<ref>) of monads. The right rectangular face is the associativity axiom for Eilenberg Moore algebras. 
  
  
  
  
  
      
  
   §.§ Syntactic homomorphisms for  recognisable  colourings
  
      In this section, we prove  the first result about syntactic homomorphisms, which says that they always exist for  algebra colourings that are recognisable. 
       
          Let  be a monad in the category of sets. Every recognisable algebra colouring has a  syntactic homomorphism.   
      
  
  The proof is based on congruences. The main result is (a strengthening of) the observation that congruences of finite index in a given algebra, ordered by inclusion when viewed as sets of pairs, form a lattice. This means that   every two congruences of finite index have a least upper bound and greatest lower bound. For the greatest lower bound (which is called the  meet in the terminology of  lattices), the observation is straightforward: if ∼_1 and ∼_2 are congruences, not necessarily of finite index, then their intersection (when viewed as a set of pairs) clearly commutes with all term operations, and is therefore a congruence. Since every lower bound must be contained in the intersection, it follows that the intersection is the greatest lower bound.
  
  The least upper bound (which is called the join in the terminology of lattices) is more interesting. Here, we use the assumption on finite index (it is enough that one of the congruences has finite index), see Exercise <ref> for why this assumption is needed.
  
  
  
      Let A be an algebra. If ∼_1 and ∼_2 are congruences and ∼_1 has finite index,  then they have a join congruence, i.e. a least upper bound among all congruences in A.
  
  
  
      Define the join to be the transitive closure of the union of ∼_1 and ∼_2.
          In other words, two elements of the algebra are related by the join if one can be reached from the other using a finite number of steps which use either one of the congruences ∼_1 or ∼_2.  This is the same as the join in the lattice of equivalence relations, and therefore  every congruence that contains both ∼_1 and ∼_2 must contain the join defined above. It remains to prove that this join is in fact a congruence, and not just an equivalence relation. To prove this, we 
       use the definition of congruences which says that they  commutes with all term operations in the algebra. We begin with the special case of term operations with  finitely many variables.
  
          
              The join commutes with all term operations  that have finitely many variables. 
          
           Consider a term operation 
              
      t^A(x_1,…,x_n)
  
              with finitely many variables. We need to show that  every two outputs 
              
      t^A(a_1,…,a_n)     and      t^A(b_1,…,b_n)
  
              are equivalent under the join, assuming that the inputs are pairwise equivalent under the join. This is proved using a finite number of steps, where in each step we use commutation of t^A with either ∼_1 or ∼_2.
          
          Using the above claim, and the assumption that ∼_1 has finite index, we show that the join commutes with all term operations, even those with infinitely many variables.  Consider a term t ∈ X and two valuations 
          
      η_1,η_2 : X → A
  
          which are pointwise equivalent with respect to the join. We need to show that applying t^A to both of these valuations gives outputs that are equivalent with respect to the join. We begin with the special case when the two valuations have finite images.
          
              If  η_1,η_2 have finite image B ⊆ A, then t^A(η_1) = t^A(η_2). 
          
          
              Define Y to be the set of pairs in B × B which are equivalent under ∼ and let η : X → Y  be the function which maps an variable from X to the pair of its images under η_1 and η_2. Each valuation η_i can be decomposed as first applying η, and then taking the i-th projection, as  in the following diagram:
              
      X 
                      [dl]_η_1[dr]^η_2[d]_η
      
                      A    
                      Y 
                      [l]^π_1[r]_π_2   
                      A
  
              Applying the term operation t^A to a valuation η_i is the same as applying the term operation (η(t))^A to the valuation π_i, which is proved by chasing the following diagram:
              
      X 
                      [rr]^t ↦ t^A(η_i)[dd]_η[dr]^t ↦ t^A(π_i)      
                      A 
          A
                      [ur]_μ
       Y
                       [ur]_π_i@/_3pc/[uurr]_t ↦ t^A(π_i)
  
              Since the  term operation (η(t))^A uses a finite set of variables Y, and since the valuations π_1 and π_2 are equivalent under ∼, the statement of this claim follows from Claim <ref>.
          
          Using the above claim, and the assumption that ∼_1 has finite index, we conclude the proof of the lemma. 
          Choose a function 
          
      α : A →  A
  
          that maps every element of A to some chosen element in its equivalence class under ∼_1. This function has finite image, because ∼_1 has finite index. We now conclude the proof of the lemma as follows:
          
      t^A(η_1) 
              ∼_1∼_1 is a congruence
      
              t^A(α∘η_1)
              ∼α has finite image and Claim <ref>
      
              t^A(α∘η_2)
              ∼_1∼_1 is a congruence
      
              t^A(η_2)
  
  
  Using the above lemma, we complete the proof of Theorem <ref>.
  
      Consider algebra colouring L : A → U that is recognisable. Let  be the  set of congruences in A that recognise the colouring in the following sense
  
      a ∼ b     implies     L(a)=L(b).
  
  Because the colouring is recognisable, the there is at least one congruence  ≈ of finite index in .  Define _≈⊆ to be the congruences which contain ≈. This is a finite set, since every congruence in _≈ is obtained by merging some of the finitely many equivalence classes in ≈. By Lemma <ref>, _≈ is a finite lattice, and therefore it has a greatest element, call it ∼. Again by Lemma <ref>, every congruence in  has an upper bound in _≈, and therefore ∼ is the greatest element also of . We will prove that the 
  quotient homomorphism
  
      h : A → A_/∼,
  
  is the syntactic homomorphism of L.
  
  By translating maximality of ∼ into  the language of homomorphisms, it follows that  that every surjective  homomorphism g : A → B that recognises L must factor through h,
   i.e. must be  some function f such that g=f ∘ h.  The last thing to show is that f is in fact a homomorphism, and not just any function on the underlying sets of the algebras. This is shown in the following claim, with C being the quotient A_/∼.
  
  
      Let A,B,C be algebras, let g,h be  surjective homomorphisms, and let f be a function which makes the following diagram commute. 
      
      @R=0.6cm@C=2cm    B [dd]^f
      
                  A [ur]^g [dr]_h 
          C
  
       Then f is a homomorphism.
  
  
      Consider the following diagram:
      
      @C=2cm B 
              [rrr]^multiplication in B[dd]_ f          
               B
               [dd]^f
          A 
              [ul]^ g[dl]_ h[r]^multiplication in A   
              A 
              [ur]^g
              [dr]_h
              
       C
              [rrr]_multiplication in C         
              C
  
      The upper and lower faces commute because g and h are homomorphisms, and the left and right faces commute by definition of f. Since all arrows in the diagram are surjective, it follows that the perimeter of the diagram commutes, which means that f is a homomorphism.
  
  
  
  
  
  
  
  
  
  
   
  
  
  
  
      
      Show that the assumption on finite index of ∼_1 in Lemma <ref> is needed.
  
      Consider the monad from Example <ref>.  Show that if   λ : Σ→ is recognised by a weighted automaton, then the same is true for the syntactic homomorphism.
  
  
  This exercise can be seen as a variant 
  of Moore's algorithm for computing the syntactic congruence.  Consider a monad in the category of sets, together with a term basis, see Exercise <ref>. 
  Consider an algebra colouring λ : A → U where A is a finite algebra.  Show that the syntactic congruence of λ is the greatest (coarsest) equivalence relation on A recognises λ and which  is stable under all term operations from the term basis. 
  
  
  
  
  
  
  
  
  
  
  
  
  
      
  
  
  
   §.§ Finitary monads
  
  
  In the monad of finite words, every language –  not just  recognisable ones – has a syntactic homomorphism. For example, in the monad of finite words, the syntactic homomorphism of the non-recognisable language “the number of a letters is equal to the number of b letters”
  maps a word to the difference (number of a's - number of b's).
  In the monad of -words, some non-recognisable languages do not have syntactic homomorphisms, as witnessed by Example <ref>. What is the difference?
  
  The difference, as will be shown in Theorem <ref> below, is that every finite word uses only a finite subset of the alphabet, which is no longer true for  -words. This is made precise by the following definition.
  
   Let  be a monad in the category of sets.
      We say that an element t ∈ X is finitary if
  
      t = ( f)(t)     for some f : X → X with finite image.
  
  We say that  is  finitary if for every X, all elements of X are finitary.
  
   
  For example, the monad of finite words is finitary, while the monads of -words is not. 
  The following theorem shows that finitary monads are exactly those monads where all algebra colourings have syntactic homomorphisms. 
  
  
   Let  be a monad in the category of sets. Then  is finitary if and only if every algebra colouring has a syntactic homomorphism[This theorem is unpublished work of Gordon Plotkin and the author.].
      
      
      
      
      
  
  
      For the left-to-right implication, we use the same proof as for Theorem <ref>. Define the join of a possibly infinite set of congruences to be the transitive closure of their union. By the same reasoning as in Claim <ref>, the join commutes with all term operations that have finitely many variables. Because the monad is finitary, all term operations are like this, and therefore the join commutes with all term operations, and is therefore a congruence. (We have thus shown that for finitary monads, the congruences in an algebra form a complete lattice.) If we now take the join of all congruences that recognise a given algebra colouring, then we get the syntactic congruence, and the quotient homomorphism is the syntactic homomorphism.
  
      
      
      We now prove the converse implication. Suppose that every algebra colouring has a syntactic homomorphism.
      Fix some set X. We will show that all elements of X are finitary. Let X be a disjoint copy of X.  For a finite subset Y ⊆ X, define 
      
      f_Y :  X +  X → X +  X
  
      to be the function which maps each element to itself, with the exception of the red copies of elements from Y, which are mapped to their corresponding black copies. Define ∼ to be the equivalence relation on (X +  X) which identifies two elements if, for some finite Y ⊆ X, they have the same image under f_Y.  
  
      
          ∼ is a congruence on (X +  X).
      
      
          We first argue that ∼ is  an equivalence relation.  Transitivity argued as follows:
      
      w_1∼as witnessed 
       by Y_1 ⊆ X w_2  
            and   
          w_2∼as witnessed 
       by Y_2 ⊆ X w_3
              implies    
          w_1∼as witnessed 
       by Y_1 ∪ Y_2 ⊆ X w_3.
  
      Consider the syntactic homomorphism of ∼, which exists by the assumption  that syntactic congruences exist. Let ≈ be the kernel of the syntactic homomorphism, which means that (a) ≈ is a congruence that is contained in  ∼ when viewed as a set of pairs, and (b)  ≈ contains  every  congruence that is contained in ∼.
      We will show that ∼ is actually equal to ≈, and therefore ∼ is a congruence. In light of (a), it is enough to show that ∼ is contained in ≈. Indeed, suppose that two elements are equivalent under ∼. By definition, this means that they have the same image under f_Y for some finite Y. Since f_Y is a  homomorphism that   recognises ∼, it follows by (b) that the two elements are equivalent under ≈. 
  
      
      Consider the functions 
      
      f, f :  X → X +  X
  
          such that f maps each argument to its red copy, and f is the identity.
  For every  x ∈ X, its unit is mapped by f and f to elements which
   have the same image under f_ x, and therefore are equivalent under ∼.  Since ∼ is a congruence, and all units in X are mapped by f and f to elements equivalent under ∼,  it follows that for every w ∈ X, its images under f and f are equivalent under ∼.  By definition of ∼, this means that for every w ∈ X  there must be some finite Y ⊆ X such that 
  
      ( (f_Y ∘ f))(w) =
      (  (f_Y ∘ f))(w)
   
  We now complete the proof that every element of X is finitary. 
  Let w ∈ X, and let Y be such that the above equivalence holds.  Choose an element  y ∈ Y and consider the function
  
      g :     X + X → X
  
       which is the identity on X and maps all red letters to y.   
  We have 
  
      w because g ∘  f is the identity on X
       
          ( (g ∘  f ))(w) because f_ Y is the identity on black letters
      
          ( (g ∘ f_ Y∘   f ))(w) by (<ref>)
      
          ( (g ∘ f_Y∘ f ))(w ).
  
  The image of the function g ∘ f_ Y∘ f is contained in Y, and therefore we have established that w is finitary. 
  
  
  
  
  
  Give an example of a monad which is not finitary, but where every language L ⊆Σ with a finite alpahbet Σ has a syntactic homomorphism. The (not necessarily finite) powerset monad.
  
  
  
  
  
  
  
  
  
  Let  be a monad in the category of sets. Show that if every algebra colouring with two colours has a syntatic homomorphism, then every algebra colouring with an arbitrary number of colours has a syntactic homomorphism.
  
  Give an example of a monad  which is not finitary, but such that all elements of X are finitary for countable X.
  
  
  
  Let S be a finite set of sort names, and consider the category 
  
      𝖲𝖾𝗍^S
  
  of S-sorted sets with sort-preserving functions.  Prove  Theorem <ref>  for monads over this category.  
  
  Consider a category of sorted sets, as in the previous exercise, but with infinitely many sort names. Define a finite algebra to be one that is finite on every sort. Show that Theorem <ref> fails.
  
  
  Show that a monad  in the category of sets is finitary if and only if it arises as a result of the construction described in Exercise <ref>.
  
  
  
  Recall the notion of regular elements from Exercise <ref>. Show that if t is regular, then it is finitary. 
  
  
  Assume that the regular elements, as considered in the previous exercise, are closed under free multiplication in the following sense: if t ∈ X is a regular term operation, and η : X → Y is a valuation of its variables that uses only regular elements, then t^ Y(η) is a regular element. Under these assumptions, define a monad of regular elements.
  
  
    
  
  
  § THE EILENBERG VARIETY THEOREM
  
  
  In Chapter <ref>, we proved several theorems of the kind
  
      class of languages    ∼    class of semigroups.
  
  For example, a language of finite words is definable in first-order logic if and only if it is recognised by an aperiodic semigroup. In this section  we prove that every class of languages with good closure properties will correspond to a class of algebras with good closure properties. The theorem was originally proved by Eilenberg for monoids[Theorem 13.2]Eilenberg76, but with some extra care one can make the proof work in the abstract setting of monads.  
  
  
    
  
  
  
   §.§ Unary polynomials
  
  Before stating and proving the theorem, we describe unary polynomials, which are used in the definition of language varieties. 
  For an algebra A, define a unary polynomial[The terminology of “terms” and “polynomials” comes from universal algebra, see
  [Definition 13.3.]sankappanavar1981course
  This terminology  can be explained – or at least more easily remembered –  as follows. Consider the ring of the reals
  (ℝ, +, -, ×, 0, 1).
  A term  operation in this ring can only use the constants 0 and 1 which are given in the ring as an algebra, and therefore term operations correspond to polynomials with integer coefficients. If we want to get all polynomials, we need to  allow the terms to use arbitrary elements of ℝ as constants.
  ] to be any function of the form 
  
      a ∈ A   ↦   t^A(a,c_1,…,c_n) ∈ A,
  
  which is obtained for some choice of n ∈0,1,…, some  term t with n+1 variables[A more principled definition, which allows more variables and infinitely many constants, is discussed in Exercise <ref>. Since we use unary polynomials mainly for finite algebras, the more elementary definition given here is enough. ] and some parameters c_1,…,c_n ∈ A. 
  
      In every algebra,  unary polynomials are closed under composition.
  
  
      Consider two unary polynomials 
      
      a ∈ A      ↦      t^A(a,c_1,…,c_n)
      
      a ∈ A       ↦      s^A(a,d_1,…,d_m).
  
      To prove that the composition of the above two unary polynomials is also a unary polynomial, we will show that there is a term u with 1+m+n variables which satisfies the following equality:
  
      t^A(s^A(a,d_1,…,d_m),c_1,…,c_n)a composition of two unary polynomials
       = 
       u^A(a,d_1,…,d_m,c_1,…,c_n)a single unary polynomial.
  
  For the purposes of this proof, we treat a number such as n as a set which has n elements.
  Define the term u to be the result of applying the term operation  
  
      t^(1+m +n) : ((1+m+n))^1+n→(1+m+n)
  
  to the valuation f 
  which maps the variable in 1 to the term s (seen as a term over a larger set of variables that does not use the last n variables), and which maps the variables in n to their corresponding units.  The equality (<ref>) follows from the following claim, in the case where the variables X are  1 + n, the variables   Y are 1 + m + n, and the valuation η∈ A^Y is 
  
      (a,d_1,…,d_m,c_1,…,c_n) ∈ A^1+m+n.
  
  
          
              For every f : X → Y and  t ∈ X, the term 
              
      u  t^ Y(f)
  
              makes the following diagram commute for  every algebra A:
              
      A^Y 
                      [dr]^u^A[d]_η∈ A^Y ↦  x∈ X  ↦  (f(x))^A(η)
      
                      A^X [r]_t^A   
                      A
  
          
          
              This claim is the same as Exercise <ref>.
              Let η∈ A^Y be a valuation. If we apply the function in the vertical arrow from the diagram to η, then we get the valuation ρ∈ A^X that is the composition of the following functions:
              
      X [r]^f    Y 
                           @/^2pc/[rr]^t ↦ t^A(η)[r]^η    A
                           [r]^μ   
                           A,
  
               where μ is the multiplication operation of the algebra A. The diagram in the statement of the claim says that 
               
      t^A(ρ) = u^A(η).
  
               To prove this, consider the following diagram:
              
      @C=2cm X 
                      [rr]^ρ[d]_ f      A[dd]^μ
       Y
                      [r]^η[d]_free multiplication    A
                      [d]_free multiplication[ur]^η
       Y 
                      [r]_μ    A
                      [r]_μ   
                      A
  
              The upper-left face commutes by definition of ρ. The lower-left face commutes by naturality of free multiplication, and the lower-right face commutes by associativity of μ. Therefore, the entire diagram commutes. If we apply the  top-most path  from X to A in the diagram to the term t ∈ X, then we get the result t^A(ρ), while if we apply the bottom-most path to the same term, then we get the result u^A(η). Since the diagram commutes, these results are equal, thus proving the claim. 
          
  
  Another result about unary polynomials that will be used in the proof of the Eilenberg Variety Theorem is the following characterisation of congruences in finite algebras. The finiteness assumption is important, see Exercise <ref>.
  
      An equivalence relation ∼ in a finite algebra A is a congruence if and only if it commutes with all unary polynomials, in the sense that
      
      a ∼ b   implies   f(a) ∼ f(b)
  
      holds for every a,b ∈ A and every unary polynomial f : A → A.
  
  
      The  left-to-right implication is immediate, and does not need the assumption on finiteness of the algebra. If ∼ is a congruence, then it  commutes with all term operations, and so it must also commute with  unary polynomials, which are term operations with some arguments fixed. 
      
      The   right-to-left implication is proved similarly to Lemma <ref> about joins of congruences. Suppose that ∼ commutes with all unary polynomials. By the same argument as in Claim <ref>, where arguments are replaced one by one in finitely many steps, it follows that ∼ commutes with all term operations that have finitely many variables. Since the algebra A is finite, all valuations for term operations have finite image, and therefore we can use  Claim <ref> to prove that ∼ commutes with all term operations, and therefore it is a congruence. 
  
  
  
  
  Show that the finiteness assumption in Lemma <ref> is  needed. Hint: use Example <ref>.
  
      Consider the language L from Example <ref>. We show that contextual equivalence for this language (viewed as a colouring with values “yes” and “no”) is not a congruence. 
      A unary polynomial  in the free algebra a^  corresponds to  a -word over alphabet a,x. From the point of view of L, there are two kinds of unary polynomials. If f is a unary polynomial such that the corresponding -word over a,x contains a^n as an infix for every n, then   f(w) ∈ L  for every w ∈ a ^. Otherwise, in the corresponding word there is some bound n_0∈0,1,… such that words a^n with n>n_0 do not appear as infixes; in this case  f(w) ∈ L w ∈ L. These observations imply that contextual equivalence for L has two equivalence classes: namely L and its complement. In particular,  contextual equivalence is not a congruence, since otherwise  L would be recognisable.
  
  
  
  Define a vectorial polynomial in an algebra A to be any operation 
  
      f : A^X → A^Y,
  
  for some sets X and Y, 
  which arises as follows: (a) transform an input valuation A^X to a larger valuation A^X+Z by mapping the variables from Z to some fixed constants; and then (b) apply a vectorial term operation A^X+Z→ A^Y, as described in Exercise <ref>. Show that vectorial polynomials are closed under composition.
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
   §.§ Varieties
  
  The classes with good closure properties will be called varieties, in analogy with the  varieties that appear in Birkhoff's theorem from universal algebra.  
  In this section, we define varieties, and give several examples of them. 
  There will be two kinds of varieties: for algebras and for languages.  We begin with the algebras. In the following definition, a quotient of an algebra is any image of that algebra under a surjective homomorphism. In other words, a quotient is a quotient under some congruence.
  
  
  
  
    Fix a   in the category of sets.  An algebra  variety is a class  of finite -algebras  with the following closure properties:
      
          
    * Quotients. If  contains A, then it contains every quotient of A.
          
    * Sub-algebras. If  contains A, then it contains  every sub-algebra of A.
          
    * Products. If  contains A and B, then it contains A × B.
      
      
  
  
      Consider the monad of finite words, where algebras are monoids. Examples algebra varieties include: finite groups, finite aperiodic monoids,  finite infix trivial monoids, or finite prefix trivial monoids.
  
  
  
      Here is a non-example. Consider the monad of nonempty finite words, where algebras are semigroups. The class of monoids (i.e. semigroups which have an identity element) is not an algebra variety, because it is not closed under sub-algebras.     
  
  
  
      
      Consider a monad . Define an identity to be a  pair of terms s,t ∈ X over a common set of variables X. An algebra A is said to satisfy the identity if 
      
      s^A(η) = t^A(η)     for every η∈ A^X.
  
      The class of finite algebras that satisfy a given identity (more generally, all identities in a given set of identities) is easily seen to be an algebra variety.  For example, the algebra variety of commutative semigroups arises from the identity
      
      xy = yx
     
      in the monad of nonempty finite words.
  Some algebra varieties do not arise this way. For example, the varieties discussed in Example <ref> do not arise from (even possibly infinite sets of) identities. Identities will be discussed in more detail in Section <ref>.
  
  
  We now describe language varieties. In Eilenberg's original formulation, this is a class of regular languages that is closed under Boolean combinations, inverse images of homomorphisms, and inverse images of operations of the form 
  
      w ∈Σ^+ ↦ v_1 w v_2 ∈Σ^+     for  fixed v_1,v_2 ∈Σ^*.
  
  In the more abstract setting of monads, the role of these operations will be played by unary polynomials, as described in the following definition.
  
  In the following definition, by recognisable languages we mean recognisable subsets of free algebras.
   Let  be a monad in the category of sets.  A language variety is a class  of recognisable languages with the following closure properties:
      
          
    * Boolean combinations.  is closed under Boolean combinations, including complementation.
          
    * Inverses of homomorphisms. If h : Σ→Γ is a homomorphism of free algebras, then  is closed under inverse images of h. 
          
    * Inverses of unary polynomials. If f : Σ→Σ is a unary polynomial in a free algebra Σ, then  is closed under inverse images of f. 
      
  
  
  
      Consider the monad of finite words, where algebras are monoids. We will that  languages definable in first-order logic are a language variety. Closure under Boolean combinations is immediate, because we are dealing with a logic. Closure under inverse images of homomorphism or unary polynomials can be proved using games: if f is either a homomorphism or a unary polynomial, then a strategy copying argument shows  that
       
      Duplicator wins the 
      k round game on w and w'     implies      Duplicator wins the 
      k round game on f(w) and f(w').
  
       This implies that first-order definable languages are closed under inverse images of homomorphisms and unary polynomials.
       The same is true for first-order logic on -words.
  
  
  
  
      Consider again the monad of finite words, where algebras are monoids. The definite languages from Example <ref> are not a variety, because the class of definite languages is not closed under inverse images of  the homomorphisms. Indeed, the language 
      
      aa,b^* ⊆a,b^*words that being with a
  
      is definite. If we take the inverse image under the homomorphism 
      
      h: a,b,c^* →a,b^*,
  
      which erases the c letters, then we get the language 
      
      c^* a a,b,c^* ⊆a,b,c^*,words that begin with a if c is erased
  
      which is not definite. The problem is with homomorphism that erase letters. If we would consider the same class of languages but in the monad of nonempty finite words, where algebras are semigroups, then we would get a variety. 
      
  
  
  
  
   Consider the monad of finite words. Show that a class of languages  is a variety if and only if it is closed under Boolean combinations, inverse images under homomorphisms, and inverse images of unary polynomials of the form:
  
      w ↦ v_1 w v_2      for every choice of parameters v_1,v_2 ∈Σ^*.
  
  
  
   Consider the monad of finite words. Show that there are uncountably many algebra varieties.
  In particular, for some algebra varieties, the membership problem A  ? ∈ is undecidable.
  
  Let P be a set of prime numbers. Consider the class of finite groups, where the order is a number where all divisors are from P. This is an algebra variety.
  
  
  
   Consider the monad of -words. Show that a class of languages  is a variety if and only if it is closed under Boolean combinations, inverse images under homomorphisms, and inverse images of unary polynomials of the following forms:
  
      w ↦ v_1 w v_2        for every choice of parameters v_1,v_2 ∈Σ^
      
           w ↦ w^ω
      
           w ↦ w^
      
           w ↦shuffle of w,v_1,…,v_n      for every choice of parameters v_1,…,v_n ∈Σ^.
  
  
  
   Consider the  monad  from Example <ref>, where X describes terms over a fixed ranked set Σ with variables X. We view term t ∈Γ as a model, where the elements are the nodes of the corresponding tree, there is a binary ancestor relation x ≤ y, and for every σ∈Σ + Γ there is a unary relation σ(x) which selects nodes with label σ. Show that the class of languages definable in first-order logic is not a variety. Hint: read the exercises in Chapter <ref>.   
  
  
  
  
   §.§ Algebra varieties are the same as language varieties 
  
  In this section we prove that the two notions of variety are equivalent.
   
      Let  be a monad in the category of sets.
      Then the maps in the following diagram are mutually inverse bijections.
      
      @C=6cmalgebra 
      
              varieties@/^2pc/[r]^ ↦ languages recognised by at least one algebra from    language 
      
              varieties@/^2pc/[l]^ ↦ finite algebras which recognise only  languages from
  
  
  
  
  Let us write  for the left-to-right map, and  for the right-to-left map. 
    We first show that each of these two maps take varieties to varieties, and then we show that the two maps are mutual inverses. 
  
  
      
    * We first show that if the input to  satisfies a weaker assumption than being an algebra variety, namely it is closed under products, then the output    is a language variety.  
      
      We begin with Boolean combinations. If L is recognised by an algebra A ∈, then its complement is recognised by the same algebra. If furthermore  K is recognised by B ∈, then  L ∪ K and L ∩ K are both recognised by the product A × B, which belongs to  by closure under products.  
      
      Consider now the inverse images. Let  L be a language that is  recognised by a homomorphism
      
      h : Σ→ A ∈.
  
      We need to show that  contains all inverse images of L under homomorphisms and unary polynomials.  Consider first the homomorphisms: let  g : Γ→Σ be a homomorphism, and consider  the inverse image of L under g, which can be written as L ∘ g if we view L as a function with outputs “yes” and “no”. This inverse image is recognised by the homomorphism  h ∘ g, which uses the algebra A, and therefore it belongs to . The  same kind argument applies to unary polynomials. Consider a unary polynomial  f : Σ→Σ. As we have remarked in the proof of  Lemma <ref>, congruences  commute with unary polynomials, which means that h ∘ f = f ∘ h, and therefore  h also recognises the inverse image L ∘ f.
      
    * Similarly to the first step, we show that if the input to  satisfies a weaker condition than being a language variety, namely it is closed under under unions and intersections, then the output is an algebra variety[The first two steps of this proof establish that the maps  and  form  what is known as a Galois connection, between 
  
      
    * classes of finite algebras closed under products; and
      
    * classes of recognisable languages closed under unions and intersections.
  
  In the terminology of Galois connections, the  varieties of both kinds are the closed sets, with respect to this Galois connection.
  ].   Every language recognised by a sub-algebra of A is also recognised by A, and the same is true for quotients, and therefore  is closed under sub-algebras and quotients of A. Consider now products. Suppose that a language L is recognised by a homomorphism
  
      h : Σ→ A × B     with A,B ∈.
  
  For every a ∈ A, the inverse image 
  
      L_a = h^-1(  a × B)
  
  is recognised by the homomorphism
  
      h_A : Σ→ A,
  
  which is the 
  composition of h with the projection to A. Since the latter homomorphism has domain A, it follows that L_a ∈.  For similar reasons, if b ∈ B then  contains the language 
  
      L_b = h^-1(A × b).
  
  The intersection L_a ∩ L_b is the inverse image under h of the pair  (a,b).  Every  language recognised by h is a finite union of such languages; and therefore it belongs to  by closure under unions and intersections.
  
  
    * We now show that the maps  and  are mutual inverses. We first show that  every algebra variety  satisfies
  
      = ,
  
  with the dual equality being proved in the next step. The above equality  is the same as showing that A ∈ if and only if 
  
      
    (*) every language recognised by A is recognised by some algebra in .
  
  Clearly every algebra  A ∈ satisfies (*). We now prove the converse implication. Suppose that an algebra A satisfies (*).  The multiplication operation 
  
      μ :  A → A
  
  in the algebra A is a homomorphism  from the free algebra A to A. By the assumption that A satisfies (*),  every language recognised by this homomorphism is recognised by some algebra from  . In particular, for every a ∈ A the language μ^-1(a) is  recognised by some homomorphism 
  
      h_a :  A → B_a ∈.
  
  Consider the product homomorphism
  
      h :  A →∏_a ∈ AB_a      t ↦ (h_a(t))_a ∈ A.
  
  Define B to be the image of h. The algebra B is a sub-algebra of  a product of algebras from , and therefore it belongs to  . From now on,  we view h as surjective homomorphism onto its image B. This  homomorphism  recognises all languages μ^-1(a), and therefore μ factors through h, i.e. there is some function f which makes the following diagram commute:
  
      A [r]^μ[dr]_h    A 
          B [u]_f
  
  By Lemma <ref>, f is not just a function but also  a homomorphism of algebras. This means that A is the image of B under a surjective homomorphism. In other words, A is a quotient of B, and therefore A ∈. 
  
  
    * In the final step, we  show that every language variety  satisfies
  
      = .
  
  This is the same as showing that L ∈ if and only if 
  
      
    (*) L is recognised by an algebra that only recognises languages from .
  
  Clearly (*) implies L ∈, so we focus on the converse implication. Suppose that L ∈, and its syntactic homomorphism, which exists by Theorem <ref>, is 
  
      h : Σ→ A.
  
  To prove (*), we will show that all languages recognised by the syntactic algebra A belong to  .  
  
  
  
      Let F : A →“yes”, “no” be the accepting set in the syntactic algebra, which means that L is equal to F ∘ h. Then  two elements of A are equal  if and only if they have the same values under  F ∘ f for every unary polynomial f : A → A.
  
  
      Let ∼  be the equivalence relation on A  which identifies two elements that have the same image under F ∘ f for every unary polynomial f.  Because unary polynomials are closed under composition, it follows that ∼ commutes with all unary polynomials, and therefore it is a congruence by Lemma <ref>. 
       Because the identity is a special case of a unary polynomial, elements that are equivalent under ∼ have the same value under  F. This means that the quotient homomorphism of ∼ recognises F, and therefore ∼ must be the identity since otherwise A would not be the syntactic algebra of L. 
  
  The following claim shows that unary polynomials in A can be pulled back, along the homomorphism h, to unary polynomials in Σ. 
  
      For every unary polynomial f : A → A there is a unary polynomial f^h : Σ→Σ which makes the following diagram commute: 
      
      Σ[r]^f^h[d]_h    Σ[d]^h
      
              A 
              [r]_f    
              A
  
  
  
      Consider a unary polynomial f : A → A of the form 
      
      a ∈ A   ↦    t^A(a,c_1,…,c_n).
  
      Because the syntactic homomorphism is surjective, for each i ∈1,…,n there must be some s_i ∈Σ which is mapped to c_i by h. Since term operations commute with homomorphisms by Lemma <ref>, the diagram in the claim commutes if we choose f^h to be 
      
      s ∈Σ  ↦    t^Σ(s,s_1,…,s_n).
  
  
  We are now ready to show that  contains all languages recognised by the syntactic algebra A.
  
  We first show that  contains all languages recognised by the syntactic homomorphism h : Σ→ A, and then we generalise this result to other homomorphisms into A. 
  By Claim <ref> and finiteness of the algebra A, there is a finite set 
  
      ⊆ A → A
  
  of unary polynomials in the algebra A such that two elements are equal if and only if they  have the same values for all functions from the set
  
      F ∘ f : f ∈.
  
  Putting this together with Claim <ref>, it follows that  two elements of Σ have the same image under h if and only if they belong to the same sets from the finite family
  
      L ∘ f_ha language that belongs to 
       as the inverse image of L
       under the unary polynomial f_h : f ∈ F.
  
  In other words, every inverse image h^-1(a) is a finite Boolean combination of languages from the above family, and therefore it belongs to  by closure under Boolean combinations. This in turn means that all languages recognised by h are in .
  
  
  
  We now prove that not only does  contain every language recognised by the syntactic homomorphism h, as we have already shown,  but it also contains every language recognised by a homomorphism 
  
      g : Γ→ A
  
  which uses the same target algebra of the syntactic homomorphism. By surjectivity of the syntactic homomorphism and the universal property of the free algebra Γ, we can choose some homomorphism f which makes the following diagram commute
  
      Γ[dr]^g [d]_f 
      Σ[r]_h     A
  
  By the above diagram, every language recognised by g is an inverse image, under f, of some language recognised by h. Since we have already proved that every language recognised by h is in , and  is closed under inverse images of homomorphisms such as f, we see that every language recognised by g is in .
  
  
  
  
  
  
  
  
  Let S be a finite set, and consider the category 
  
      𝖲𝖾𝗍^S
  
  of S-sorted sets with sort-preserving functions. 
  State and prove the Eilenberg Variety Theorem for monads over this category.
  
  
      
  Consider the monad 
  from Example <ref>, which corresponds to weighted automata. We adapt to varieties to the weighted setting as follows.  Define an algebra variety to be class of finite-dimensional algebras which is closed under sub-algebras, quotients and products. Define a language variety to be a class  of linear maps Σ→, recognised by finite-dimensional algebras, which is closed under inverse images of homomorphisms and polynomials, and which is closed under combinations in the following sense: if  contains 
  
      λ_i : Σ→ U_i_i ∈1,2,
  
  and f : ^2 → is a linear map, then  contains also 
  
      w ↦ f(λ_1(w),λ_2(w)).
  
  Show that the Eilenberg Variety Theorem holds for varieties understood in this way. 
  
  
  
      Consider the weighted varieties from the previous example. 
      What is the weighted analogue of star-free languages? Hint: consider the concatenation of two linear maps
      
      λ_1,λ_2 : Σ→ U
  
  to be the linear map which is defined as follows on Σ^*
      
      (λ_1 ·λ_2) (a_1 ⋯ a_n) = ∑_i ∈0,…,nλ_1(a_1 ⋯ a_i) ·λ_2(a_i+1⋯ a_n),
  
  and which is extended to Σ by linearity.
      
  
  
  
  
   For an algebra colouring L : A → U, define contextual equivalence to be the equivalence relation on A which identifies two elements of A if they have the same image under L ∘ f for every unary polynomial f : A→ A. Show that if L is recognisable, then contextual equivalence is the syntactic congruence of L.
  
  
  Show that contextual equivalence, as defined in the previous exercise, need not be a congruence for algebra colourings that are not recognisable.Consider the language L from Example <ref>. We show that contextual equivalence for this language (viewed as a colouring with values “yes” and “no”) is not a congruence. 
  A unary polynomial  in the free algebra a^  corresponds to  a -word over alphabet a,x. From the point of view of L, there are two kinds of unary polynomials. If f is a unary polynomial such that the corresponding -word over a,x contains a^n as an infix for every n, then   f(w) ∈ L  for every w ∈ a ^. Otherwise, in the corresponding word there is some bound n_0∈0,1,… such that words a^n with n>n_0 do not appear as infixes; in this case  f(w) ∈ L w ∈ L. These observations imply that contextual equivalence for L has two equivalence classes: namely L and its complement. In particular,  contextual equivalence is not a congruence, since otherwise  L would be recognisable.
  
  
  
  
  
  § IDENTITIES AND BIRKHOFF'S THEOREM
  
  
  In this section we return to the identities that were described in Example <ref>.  Recall that an  identity is a pair of  terms over a common set of variables. 
  We say that an algebra A satisfies an identity consisting of terms s,t ∈ X if 
  
      h(s)=h(t)     for every homomorphism h :  X → A.
  
  This is equivalent to the definition given in Example <ref>, which said that  an algebra A satisfies the identity if the two term operations s^A and t^A are equal.
  
  Below we present two theorems about classes of algebras that can be defined using identities.  There will be two theorems,  one for not necessarily finite algebras, and one for finite algebras.
  
  
  
   §.§ Identities for varieties not necessarily finite algebras
  
  We begin with the first theorem about identities, which is a monad variant of Birkhoff's  Theorem from universal algebra[Theorem 10.]Birkhoff35. The theorem says that a class of not necessarily finite algebras can be described by identities if and only if it  is a Birkhoff  variety, which means that it is closed under images of surjective homomorphisms, subalgebras and (not necessarily finite) products. Traditionally, Birkhoff  varieties are called simply algebra varieties, but the latter name has already been used in  this book for  classes of finite algebras that are described in Definition <ref>. To avoid confusion, for the purposes of this section where the two kinds of algebra varieties are used, we use the name Eilenberg  variety for the varieties of finite algebras.
  
  
  We say that a class of not necessarily finite algebras is defined by a set of identities  if the algebras in the class are exactly those that satisfy all identities from . 
  
  
      Let  be a monad in the category of sets. 
      A class  of not necessarily finite algebras is a Birkhoff variety if and only if it can be defined by some set of identities.
  
  
  We will prove a slightly stronger result, which establishes a duality between algebras and identities. Under this duality, Birkhoff varieties will correspond to sets of identities that are closed under consequences, as described below.
  
  A consequence of  a set of identities  is defined to be  any identity that is satisfied in every algebra that  satisfies all identities from . We say that a set of identities  is closed if it contains all of its consequences.  In symbols, a set of identities  is closed if it satisfies the following:
  
      ∀ efor every
       identity (∀ Afor every 
       algebraA ⇒ A  eif A satisfies all 
          
       identities 
           in ,
       then it satisfies e) ⇒ e ∈.
  
  
  By taking the right and then left arrow in the following theorem, we immediately get the Birkhoff's Theorem. 
  
   
      Let  be a monad in the category of sets.
      Then the maps in the following diagram are mutually inverse bijections.
      
      @C=6cmBirkhoff
       
              varieties@/^2pc/[r]^ ↦ identities satisfied by all algebras in     closed sets
       of identities@/^2pc/[l]^ ↦ algebras that satisfy all identities in
  
  
  
      Let us write  for the left-to-right map in the diagram from the theorem, and  for the right-to-left map. Almost by definition, applying the map  to any set of algebras will produce a set of identities that is closed. It is also not hard to see that the map  produces Birkhoff varieties, because algebras satisfying a given identity are closed under surjective homomorphic images, subalgebras and possibly infinite products. 
      It remains to show that the maps are mutually inverse, which corresponds to the following two equalities:
      
      = for every closed set of identities and  = for every Birkhoff variety .
  
      The first equality says that an identity belongs to  if and only if it is satisfied by all algebras that satisfy all identities in . This equality is simply the definition of a closed set of identities, and the equalities holds for closed sets. We are left with the second equality, which says that an algebra belongs to  if and only if (*) it satisfies all identities that are true in all algebras from . Clearly every algebra from  satisfies (*). The converse implication follows from the following lemma, and the closure properties of Birkhoff varieties.
      
      
      
  
       Let   set of algebras. 
      If an algebra B satisfies all identities that are true in all algebras from , then B is a homomorphic image of a subalgebra of a (possibly infinite) product of algebras from . 
      
  
   The key observation is that satisfying an identity can be interpreted in terms of homomorphisms, in the following way: an identity consisting of two terms B is true in all algebras from  if and only if the two terms have the same image  under  every homomorphism
  
      h :  B → A ∈.
  
  Take the product of all possible homomorphisms h as above, and  restrict the resulting homomorphism to its image,  yielding  a surjective   homomorphism
  
      H :  B → A ∈subalgebrasbecause we restricted to the imageof products of algebras in ,
  
  such that two terms in B are an identity true in all algebras from  if and only if they have the same image under H.   If two terms  s,t ∈ B have the same image under H, then they form an  identity that is true in all algebras from , and therefore also an identity that is true in the algebra B, by assumption on B. If an identity is true in B, then the two terms in the identity must have the same result under the multiplication operation of the algebra B, since the latter is an example of a homomorphism of type B → B. Summing up, we have shown that if two terms in B have the same result under H, then they have the same multiplication. 
  This means that the multiplication operation of the algebra B factors through the surjective homomorphism H. 
  
      ∃fa function 
       on underlying 
       sets    @C=2cm B
             @->>[r]^H 
             @->>[dr]_- multiplication of B        A 
          @[red]@->>[d]^ f
          B
  
  By Claim <ref>, the function f is actually a homomorphism. Therefore, B is  is the image of A under some surjective homomorphism.  
  
  
  
  
  
  
   §.§ Identities for varieties of finite algebras
  
  We now turn to identities that characterise varieties of finite algebras, the same varieties that were use in the Eilenberg Variety Theorem. To avoid confusion with the Birkhoff varieties of possibly infinite algebras that are also discussed in this chapter, we use the name Eilenberg varieties for varieties of finite algebras. 
  
  [This theorem is based on 
      [Theorem 1]eilenberg1975pseudovarieties
      The theorem cited above differs in two ways from our Theorem <ref>: (a) our theorem works for any monad subject to the assumption on countably many finite algebras; (b) the characterisation in terms of identities from <cit.> is different, because it gives a sequence of identities (and not sets of identities), and it requires satisfying all but finitely many identities from the sequence. Eliminating difference (b) seems to require some extra assumptions on the monad.
      ]
      Let  be a monad in the category of sets, such that there are countably many finite algebras up to isomorphism. The following conditions are equivalent for every class  of finite algebras:
      
          
    *   is an Eilenberg variety, i.e. it is closed under images of surjective homomorphisms, subalgebras and finite products;
          
    *  there is a sequence of sets of identities
           
      _1 ⊇_2 ⊇⋯each _n is a set of identities
  
              such that a finite algebra belongs to  if and only if for some n ∈1,2,… it satisfies all identities in _n. 
      
  
  
      We begin with the implication <ref>⇒<ref>. If an algebra satisfies all identities in _n, then the same is true for all of its subalgebras and images under surjective homomorphic images. For binary products the argument is the same: if an algebra satisfies all identities in _n and another algebra satisfies all identities in _m, then their product satisfies all identities in _max(m,n). This establishes that every class of algebras satisfying condition <ref> has the closure properties required of an Eilenberg variety.
  
      Consider now the converse implication <ref>⇒<ref>. Define _n to the first n algebras from , with respect to the countable enumeration from the assumption in the theorem. Define _n  to be the identities that are satisfied by all algebras in _n. We claim that an algebra B belongs to  if and only if for some n ∈1,2,… it satisfies all identities in _n. The left-to-right implication is immediate. For the right-to-left implication, we can apply Lemma <ref> to conclude that B is the homomorphic image of a subalgebra of a product of algebras from _n. Furthermore, if we inspect the proof of Lemma <ref>, we will see that the product is finite, because the homomorphisms from B to algebras in _n, as used in (<ref>), can be chosen in finitely many ways. 
  
  
   Show that Theorems <ref> and <ref> are  also true for monads in categories  of sorted sets, even with infinitely many sorts.
  
   Give an example of a monad which violates the assumption on countably many finite algebras from Theorem <ref>. Hint: see Section <ref>.
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
          
  
  
  
  
  
   
  
  PART: 
  Trees and graphs
  
   
  
  CHAPTER: FOREST ALGEBRA
  
  
  
  In this chapter, we present a monad that  models  trees[
      This section is based on
      bojanczykForestAlgebras2008
  ].  The trees are  finite, node labelled, unranked (no restriction on the number children for a given node), and without a sibling order. Other kinds of trees can be modelled by other monads. 
  
  
  
  § THE FOREST MONAD
  
  
  In fact,  the monad will represent  slightly more general objects, namely forests (multisets of trees) and contexts (which are forests with a port that is meant to be replaced by a forest or context).
  The algebras are going to be two-sorted, with the sort names being “forest” and “context”. For the rest of this chapter, define a two-sorted set to be a set together with a partition into elements of forest sort and elements of context sort.  We use a convention where forest-sorted elements are written in red, context-sorted elements are written in blue, and black is used for elements  whose sort is not known or which come from a set without sorts.  
  
      A forest over a two-sorted set Σ consists of a set of nodes;  a partial parent from  nodes to nodes, and   a labelling from nodes to Σ. Here is a picture:
       65
  The parent function must be acyclic, and the labelling function must respect the following constraint: leaves (nodes that are not parents of any other node) have labels of sort “forest”, while non-leaves have labels of sort “context”. 
  
  
  We assume that  forests  are  nonempty, i.e. there is at least one node.  
  Note that there is no order on siblings in our definition of forests. The definition 
  
  
  We use the usual tree terminology, such as root (a node without a parent), ancestor (transitive reflexive closure of the parent relation), child (opposite of the parent relation), descendant (opposite of ancestor) and sibling (nodes with the same parent). We assume that all roots are siblings.
  
  Apart from forests, the forest monad will also talk about contexts, which are  forests with an extra dangling edge that is attached to a node with a context label, as in the following picture:
  66
  
  
   
  
    
  *The forest monad. We now define a monad structure on  forests and contexts.
  
   
   
  
      Define the forest monad as follows. 
      
          
    * The underlying category  is two-sorted sets, where objects are two-sorted sets (with sorts “forest” and “context”) and the morphisms are  sort-preserving functions between two-sorted sets. 
          
    * For a two-sorted set Σ, the forest-sorted elements in Σ are forests over Σ, while the context-sorted elements  are    contexts over Σ. A sort-preserving function f : Σ→Γ is lifted to a sort-preserving function f : Σ→Γ by  applying f to the label of every node and leaving the rest of the structure unchanged.
          
    * The unit operation maps a label a ∈Σ to the unique forest or context that has one node with  label a, as in the following pictures:
          70
          
    * Free multiplication is the operation of type Σ→Σ that is illustrated in Figure <ref>. More formally, the free multiplication of  t ∈Σ is defined as follows.  The nodes are pairs (u,v) such that u is a node of t and v is a node in the tree or context which is the label of u. The label is inherited from v, while the parent of a node (u,v) is defined as follows (in the following t_u ∈Σ is the label of node u in t):
          
      (u,t_u-parent of v)    if v is not a root in t_u;
      
                      (t-parent of u,port of t-parent of u)    if v is a root in t_u and u is not a root in t;
      undefined   otherwise
  
          If t is a context, then the port in the free multiplication is defined to  be the port of the context that labels the  port of t.
      
  
  We leave it as an exercise for the reader to check that the monad axioms are satisfied by the above definition. We use the name forest algebras for Eilenberg-Moore algebras over this monad.  
  
  
  
  § RECOGNISABLE LANGUAGES
  
  The rest of this chapter is devoted to a study of the languages recognised by forest algebras. We care mainly about languages recognised by  finite forest algebras, which are forest algebras that have finitely many elements on both sorts. We begin with some examples.
  
  
  The notion of compositional function and Lemma <ref> about compositional functions corresponding to homomorphisms is also true for monads in the category of sorted sets used by forest algebra. Therefore, we will mainly describe  homomorphisms using the terminology of compositional functions. 
  
    
      Let  Γ⊆Σ be two-sorted alphabets. We can view 
      
      Γ⊆Σ
  
      as a language, which only contains those  forests and context over alphabet Σ where all labels are from Γ. Here is a homomorphisms into a finite algebra that recognises this homomorphisms  Consider the function  which inputs a forest or context in Σ, and outputs the following information: (a) is it a forest or context; (b) are all labels from Γ?  This function is easily seen to compositional, and  therefore h it can be viewed as a homomorphism of forest algebras.  The co-domain of the homomorphism h is a forest algebra with two elements on the forest sort, and two elements on the context sort.
  
  
  
      Let Σ be a two-sorted alphabet, let n ∈1,2,…. Consider the function h which inputs a forest or context in Σ, and outputs the following information: (a) is it a forest or context; (b) what is the number of nodes modulo n. This function is compositional, and therefore h can be viewed as a homomorphism of forest algebras. This homomorphism recognises the language of forests or contexts where the number of nodes is divisible by n. 
  
  
  
      Consider an alphabet Σ where all letters have context type. In this case, there are no forests over Σ, because there can be no leaves. For the same reason, every context over Σ looks like this:
      71
      In other words, 
      Σ is empty on the forest sort, and is isomorphic to the free semigroup Σ^+ on the context sort. Since the monad structure of the free semigroup agrees with the monad structure of the forest monad, it follows that a forest algebra with an empty forest sort is the same thing as a semigroup.
  
  
  
  
  Show that recognisable languages in the forest monad  are closed under images of (not necessarily letter-to-letter) homomorphisms 
  
      h : Σ→Γ.
  
  
  
  Consider an alphabet Σ which has one forest-sorted letter a and no context-sorted letters. There are no contexts over this alphabets and the forests look like this:
  
      a +  ⋯ + a
  
  Consider the homomorphism h which maps a to a+b, where b is some new forest-sorted letter. The image of Σ under this homomorphism consists of forests where the number of a's is equal to the number of b's.
  
  
  
  Consider a variant of the forest monad, where we allow contexts where the port is a root, like in the following example:
  99
  Show that in this variant, recognisable languages are not closed under images of homomorphisms, but are closed under images of letter-to-letter homomorphisms.
  
  
  
  
  
  
  
  
   §.§ A finite representation
  
  
  As usual with the monad approach, one needs to explain how  algebras can be finitely represented. 
  Even if the underlying sorted set is finite, the multiplication operation 
  
      μ :  A → A
  
  is in principle an infinite object. We show below a finite representation for the multiplication operation, in analogy to semigroups, where one only needs to define  multiplication  for inputs of length two. When discussing this finite representation, we use as much as possible the abstract language of monads; this will allow us to see analogies with other finite representations in this book. 
  
  
  
    
  *A term basis. Like for any monad, a term in the forest monad is defined to be an element of X for some two-sorted set of variables X. Here is a picture of a term:
  68 A difference with respect to terms for monads in the category of sets is that in the forest monad – which lives in the category of two-sorted sets – the variables are sorted, which means that there are forest variables, and context variables. Also, the term itself has a sort (call this the output sort). 
   When interpreted in an algebra A, a term t ∈ X induces a term operation t^A defined by
  
      η∈ A^X   ↦  multiplication in A applied to (η)(t).
  
  The input to the term operation is a sort-preserving valuation of the variables, while the output is an element of the algebra whose sort is the output  sort of the term. For example, the term
  110
   induces a term operation which inputs a context sorted x and a forest-sorted y, and outputs a forest sorted element. Note that term operations are not morphisms in the category of two-sorted sets, if only because there is no clear way of assigning a sort to the input valuation.
  
  We distinguish the following terms in forest algebra. 
  
  
      Define the basic forest algebra terms to be the following terms:
  72
  (These happen to be all  terms with exactly two nodes, modulo renaming variables.)
  The basic operations in  a forest algebra A are defined to be  the  term operations that are induced in A by these terms. We also use the following notation for the basic operations, listed in the order from the picture above (the colour of an operator is the colour of the output sort): 
  
      x · xinputs a 
       context x
       and forest x
       and outputs
       a forest     x · yinputs a 
       context x
       and context y
       and outputs
       a context     x  +  yinputs a 
       forest x
       and forest y
       and outputs
       a forest     x  +  xinputs a 
       forest x
       and context x
       and outputs
       a context     x ⊕ xinputs a 
       forest x
       and context x
       and outputs
       a context
  
  
  
  
  
  
  
      The multiplication operation in a forest algebra is uniquely determined by the basic operations.
  
  
      Every forest or context can be constructed from the units by applying the basic  operations. 
  
  
  The forest algebra in the above theorem  does not  need to be finite. If it is  finite, then it can be finitely represented by giving the multiplication tables for the basic operations. Using this representation, we can talk about algorithms that process finite forest algebras.
  
  
  
  We can also give simple list of axioms forest algebra, see Figure <ref>. These axioms are sound (they are satisfied by the basic operations in every forest algebra) and complete (if one gives five operations on a two sorted set A that satisfy the axioms, then these operations can be extended to a  forest algebra multiplication μ :  A → A). Using this  axiomatisation, we can effectively check if a finite representation  of a forest algebra is correct, i.e. it comes from some forest algebra.
  
  Show that for every t ∈Σ there is a decomposition 
  
      t = f(t_1,…,t_n)
  
  such that  f is a term of size at most 4 (and therefore the number of arguments n is at most 4), and all arguments t_1,…,t_n have at most half the size (number of nodes) of t. 
  
      Define a factor in t to be a set of nodes that is obtained by choosing some set of siblings, and taking all of their descendants. 
      
          There must be a  factor X which contains between one third and two thirds of all nodes.
      
       Take a forest factor  X which contains at least half  of all nodes, and which is inclusion-wise minimal for this property.  Let x_1,…,x_n be the minimal nodes in X, these are all siblings. Define X_i to be x_i and its descendants.  By minimality, each of the sets X_1,…,X_n has at most half of the nodes. If some X_i contains at least one third of the nodes, then we are done. Otherwise,  choose the smallest i ∈1,…,n such that
          
      X_1 ∪⋯∪ X_i
   
          contains at half of all nodes; because each of the  sets X_1,…,X_i has at most one third of all nodes it follows that the above union has at most two thirds of all nodes.
      
      Let X be the set from the above claim, and define t_X ∈Σ to be the result of restricting t to the nodes of X. If X contains the port then t_X is a context, otherwise t_X is a forest. 
  
  
  Fix some language L ⊆Σ that is recognised by a finite forest algebra. Suppose that we begin with some forest t ∈Σ and then we receive a stream of updates and queries. Each update changes a label of some node (the set of nodes and the parent function are  not changed by updates). Each query asks if the current forest belongs to L. Show that one can compute in linear time a data structure (at the beginning, when the first forest t is given), such that updates can be processed in logarithmic time and queries can be processed in constant time. 
  
  
  
  Prove completeness for the axioms (F1)–(F6).
  
  
  
  
   §.§ Syntactic algebras
  
  
  We now discuss syntactic algebras must necessarily exist in the forest monad. This is shown by a minor adaptation of the results from Section <ref>.
  As mentioned in Section <ref>, syntactic homomorphisms also make sense in other categories, such as the category of two-sorted sets used by the forest monad.
  
  In the forest monad, an algebra colouring is a sort-preserving function from the underlying two-sorted set in a forest algebra to some two-sorted set of colours. A subset L ⊆ A  can be seen as  special case of algebra colouring which uses four colours
  
      yes, noforest sort∪yes, nocontext sort.
   
   For the category of two-sorted sets, surjective functions are those which are surjective on both sorts.
  
  The results on existence of syntactic homomorphisms from Section <ref> can be easily adapted to the forest monad – more generally, to every monad in every category of sorted sets –   as explained in the following theorem and its proof.
   Let  be a monad in a category of sorted sets (there could be more than two sorts, even infinitely many).
      
          
    * If  is finitary, then every algebra colouring has a syntactic homomorphism;
          
    * If the monad is not necessarily finitary, but there are finitely many sort names, then every algebra colouring recognised by a finite algebra (finite on every sort) has a syntactic homomorphism.
      
       
  
  
      For item (1) we use the same proof as in the left-to-right implication for Theorem <ref>, while for item (2) we use the same proof as in Theorem <ref>. The only difference is that the variables in term operations have sorts. In item (2), the assumption on finitely many sort names is used[This assumption is indeed necessary, which can  be proved using ideas from DBLP:journals/lmcs/BojanczykK19
      ] in the extension of Lemma <ref> to say that there are finitely many equivalence classes of altogether of a congruence that has finitely many equivalence classes on each sort. 
      Apart from this difference, the rest of the proof is the same.
  
  
  
  In particular, since the forest monad is finitary, it follows that every language L ⊆Σ in the forest monad has a syntactic algebra.  As discussed in the exercises, the syntactic algebra for a language recognised by a finite forest algebra can be computed. 
  
  Also, the Eilenberg Variety Theorem holds for the forest monad. In the statement, the unary polynomial operations are the sorted version that is described in the proof of Theorem <ref>, apart from this change the statement of the theorem and its proof are the same as in Section <ref>. More generally, the Eilenberg Variety Theorem works for every monad in every category of sorted sets, assuming that there are finitely many sorts. When generalising the proof  of the Eilenberg Variety Theorem to multi-sorted algebras, we use the assumption on finitely many sorts in step (4) of the proof, to show that there are finitely many possible unary polynomial operations in a finite algebra.
  
  
  
  
  
  Show that the syntactic algebra can be computed for a language L ⊆Σ that is recognised by a finite forest algebra.  The input to the algorithm is a homomorphism
  
      h : Σ→ A
  
  into a finite forest algebra, together with an accepting set F ⊆ A. The forest algebra is represented using its basic operations, as in Theorem <ref>, and the homomorphism is represented by its values on the units. 
  
      
         We assume that the homomorphism is surjective, since the image under h can be computed as follows: take the images of the units, and close this subset under applying the basic operations. 
          
              An equivalence relation in the underlying set of a forest algebra is a congruence if and only if it is compatible with the basic operations:
              
      a ∼ a'    b ∼ b'    implies    a + b ∼ a' + b
  
          
      
  
  
  
  
  
  
  
   §.§ Infinite trees
  
  
  Define a monad  in the same way as the monad , except that we allow the forests and contexts to be countably infinite. A node might have infinitely many children, and there might be infinite branches.  This monad is no longer finitary. 
  
  We do not discuss this monad in more detail, apart from the following example, which shows that it is not clear what a “finite algebra” should be for this monad.
  
  
  
      Consider  the two-sorted alphabet Σ =  a,  b. Even though there are no forest-sorted letters, it is still possible to construct an infinite forest over this alphabet, because there is no need for leaves.   We say that a language of ω-words is prefix-independent if it is stable under removing or adding a single letter as a prefix. Consider some  prefix-independent language L of ω-words over the alphabet Σ, not necessarily regular. For example 
      
      L =  b^n_1 a  b^n_2 a ⋯ : the sequence n_1,n_2,… contains infinitely many primes.
  
      Define a branch in a forest to be a set of nodes that is linearly ordered by the descendant relation, and which is maximal inclusion-wise for this property. An L-branch is a branch where the sequence of labels, starting from the root, belongs to L. 
      
      
      Define L' ⊆Σ to be the set of infinite forests where  every node has at least two children and every node belongs to some L-branch. 
      We claim that L' is recognised by a finite algebra, with at most 36 elements, regardless of the choice of L (as long as it is prefix independent). Since there are uncountably many possible choices for L, it follows that there is no finite way of representing algebras in this monad that have at most 36 elements. In particular, “finite on every sort” is not a reasonable choice of “finite algebra” for this monad.
  
        Define a function h from Σ to a finite two-sorted set as follows. For forests, the function h gives the  answers to the following questions:
  
      
    1. is the forest in L'?
      
    2. are there are at least two roots?
  
  For contexts, the function h gives the  answers to the following questions:
      
          
    1. is it possible to fill the port with some forest so that the result is in L'?
          
    2. are there are at least two roots?
          
    3. does the  port have a sibling?
          
    4. is the context equal to the unit of a? 
          
    5. is  the context equal to the unit of  b?
      
      The red questions have at most 4 possible answers, and the blue questions have at most 32 possible answers, hence the number 36. In fact, this number can easily be reduced; for example in case of a “no” answer to question 1, there is no need to store the answers for the remaining questions. 
  We leave it as an exercise for the reader to check that the  function h is compositional. It follows that the image of the function h, call it A, is a finite algebra for the monad . 
  
  
  
  
  
  
   Consider the monad . We say that a forest or context in this monad is thin if it has countably many branches. Define Σ⊆Σ to be thin forests or contexts. Show that this is a monad.
  
  
  
  
   
  Show that a countable forest or context is thin, in the sense of Exercise <ref>, if and only if one can assign countable ordinal numbers to its children so that if a node is labelled by  ordinal number α, then all of its children are labelled by ordinal numbers ≤α, and at most one child is labelled by α. 
  
  
  
  Consider the monad  from Exercise <ref>. Show that a finite algebra over this monad is determined uniquely by its forest algebra operations (as in Theorem <ref>) plus the following two term operations:
  79
  
  
  
  
  
  § LOGICS FOR FOREST ALGEBRA
  
  In Chapter <ref>, we presented many examples of logics on finite words that could be characterised using structural properties of recognising monoids. In this section, we present some results of this type for forest algebra. Unfortunately, there are fewer interesting examples in the case of forest algebra, since the algebraic theory of forest languages is still not properly understood. A notable gap in our logic is first-order logic on trees, which is not known to have an algebraic characterisation, as will be discussed in Section <ref>.
  
  
  
   §.§ Monadic second-order logic
   
  We begin with monadic second-order logic.  The idea is as usual: to each forest or context we associate a model, and then we  use monadic second-order  logic to describe properties of that model. There is one twist: because siblings in a forest or context are not ordered, we will need to extend with modulo counting in order to make it expressively complete for all recognisable languages.
  
  
      Define the ordered model of a forest or context as follows: the universe is the nodes, and it is equipped with the following relations:
      
      x ≤ yancestor    a(x)x has label a ∈Σ    port(x)x is the port
  
      The arguments to the relations are x,y, while the letter a is a parameter. Each choice of a ∈Σ gives a different relation. 
  
  
  
  By using different logics on the ordered model, we get different classes of languages. 
  We begin with monadic second-order logic.
  A language L ⊆Σ is called definable if it can be defined by a formula of monadic second-order logic using the ordered model.  The logic   is not enough to define all recognisable languages, because it cannot count the number of nodes modulo two (or three, etc.). The problem is that there is no order on the siblings, so if we get a forest
  
      a + ⋯ + a
  
  that consists of n nodes that are both roots and leaves, then we cannot use the usual trick of  selecting even-numbered nodes  to count parity. (A more formal argument will be given below.) For these reasons, we  extend with modulo counting. In this extension – called counting  – for every set variable X and numbers n ∈2,3,… and ℓ∈0,…,n-1  we can write a formula
  
      |X| ≡ℓ n
  
  which says that the size of the set X is congruent to ℓ modulo n. This extension is expressively complete for the recognisable languages, as shown in the following theorem. 
  
  
      A language L ⊆Σ is recognised by a finite forest algebra if and only if it is definable in counting .  
  
   Both implications in the theorem are proved in a similar way as for finite words, so wo only give a proof sketch. 
      
  
    
  *From counting MSO to a finite forest algebra. Same proof as for finite words: we remove the first-order variables (by coding them as singleton sets), and then we show by induction that for every formula of (possibly with free variables), its corresponding language is recognised by a finite forest algebra.  In the induction steps we use products and powersets, both of which are finiteness preserving constructions for forest algebras. 
  
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
  
    
  *From a finite forest algebra to counting MSO.
       Suppose that L ⊆Σ is recognised by a homomorphism
      
      h : Σ→ A
  
      into a finite forest algebra.     
      The idea is the same as for finite words: the defining formula  inductively computes the value under h for every subtree in the input forest or context. The induction corresponds to a bottom-up pass through the input[This idea works for objects such as finite words or forest algebra, because they have a canonical way of parsing (left-to-right for words, or bottom-up for forest algebra), which can be defined in . For -words, we do not know  any simple  parsing method, which is the reason why  the implication from finite algebras to logic in Section <ref> was hard. A similar phenomenon will appear for graphs, which will be discussed in the next chapter, which also do not have any simple definable canonical way of parsing. ]. 
      Define the subtree of a node in a tree or context as explained in the following picture:
      83
      In a forest, all subtrees are trees, while in a context some subtrees are trees and others are contexts.
       Define the type of a node  to be the image under h of its subtree.   The following claim shows that the type of a node can be inferred from the types of its children using counting.
      
       For every t ∈Σ and every node x in t, the type of x  depends only on the answers to the following questions:
          
              
    * what is the  label of node x?
              
    * are there exactly n children of x type a?
              
    * does n divide the number of children of x with type a? 
          
          where a ranges over elements of the forest algebra A and n ∈0,…,|A|.
      
      
          Let a_1,…,a_m be the types of the subtrees of the children of x. At most one of these types is a context, because there is at most one port. We only consider the case where  all of the types are forests (and hence they will be written in red below); the case when one type is a context  is treated similarly. If a is the label of node x, then the type of x is equal to 
          
      a · (a_1 + ⋯ + a_m).
  
          The label a is known, while the red part is multiplication in the forest semigroup of A, which is a commutative semigroup. In a commutative semigroup, the result of multiplication depends only on the number of times each argument is used. Furthermore, since the forest semigroup has size at most  |A|, then the number of times an argument is used  needs to be remembered only up to threshold |A| and modulo some number that is at most |A|, see Exercise <ref>.
      
      Consider some enumeration A = a_1,…,a_n of the elements in the algebra. Some of these elments have forest sort and some have context sort. Using the above claim, we can write a formula 
      
      φ(X_1,…,X_n)
  
      of counting which holds if and only if for every i ∈1,…,n, the set X_i is exactly the set of nodes with type a_i. The formula simply checks that the types for each node are consistent with the types of its children, as described in the  claim. Finally, the image h(t) of a forest or context can be computed in counting by guessing the sets X_1,…,X_n that satisfy the formula φ above, and then inferring h(t) from the types of the root nodes (with the same argument as in the above claim). Since the image h(t) determines membership  of t in the language, it follows that the language itself is definable in counting .
  
  
  The construction of an algebra in the above theorem is effective: given a sentence of , we can construct a recognising homomorphism 
  
      h : Σ→ A
  
  into a finite forest algebra (and compute an accepting set F ⊆ A). The finite forest algebra is represented by its basic operations, as discussed in Section <ref>, and the homomorphism is represented by its images for the units.
  
   The exact role of counting is explained in the following theorem.
  
  
  
      A language L ⊆Σ is definable in (without counting) if and only if it is recognised by a forest algebra where the forest semigroup (forests equipped with +) is aperiodic. 
  
  A corollary of this theorem is that modulo counting is needed to define the language “even number of nodes”, since this language cannot be defined by a forest algebra with an aperiodic forest semigroup.
   For the left-to-right implication, we use the same proof as in the left-to-right implication of Theorem <ref>. The only difference is that in Claim <ref> we do not need modulo counting. This is because for every commutative aperiodic semigroup, the outcome of multiplication depends only on the number of times that each argument is used up to some finite threshold, without modulo counting. 
  
  Consider now the right-to-left implication, which says that if a language is definable in without counting, then it is recognised by a finite forest algebra with an aperiodic forest semigroup. Here, again, we use the same proof as in Theorem <ref>, where a recognising forest algebra is constructed by starting with some atomic forest algebras, and then applying products and the powerset construction. Since we do not need the relation 
  
      |X| ≡ℓ n
  
  from the set model, all of the atomic forest algebras have forest semigroups that are aperiodic. Products clearly preserve aperiodicity of the forest semigroup, and the same is true powersets, as explained in the following lemma.
  
  
      If S is a commutative[Commutativity is important in the proof of the  lemma. For example, in Exercise <ref> we showed that every finite semigroup, not necessarily aperiodic, can  be obtained by applying products and powersets to an aperiodic semigroup. ] aperiodic semigroup, then the same is true for its powerset semigroup S.
  
  
      In this proof, we use multiplicative notation for the semigroup operation.
      By aperiodicity of S, there is some   ∈1,2,…  such that every element  of b ∈ S satisfies b^ = b^ b. To establish aperiodicity of the powerset semigroup, we will show that every element A ⊆ S of the power set semigroup satisfies 
      
      A^n = A^n+1
  
      where n is the size of S times +1. We only show the inclusion A^n+1⊆ A^n, the same proof can be used to establish the opposite inclusion.   Let 
      
      a = a_1 ⋯ a_n+1∈ A^n+1.
  
      By the pigeon-hole principle and choice of n,  some  b ∈ A must appear at least +1 times in the sequence a_1,…,a_n+1. By commutativity and aperiodicity of S, one extra occurrence of b can be eliminated from the multiplication, proving  A^n+1⊆ A^n.
  
  
  
  
      A language is definable in without counting if and only if its syntactic forest algebra is finite and has an aperiodic forest semigroup.
  
  
  Aperiodicity of the forest semigroup is preserved when taking subalgebras and quotients (images under surjective homomorphisms). Since the  syntactic forest algebra can be obtained from any recognising forest algebra by taking a subalgebra and then a quotient, the result follows from Theorem <ref>.
  
  Since the syntactic forest algebra can be computed for recognisable languages, it follows that given a sentence of counting , we can decide if there is a  sentence of which does not use counting and which is equivalent on forests and contexts. 
  
  
  
  
  
  
  
   §.§ First-order logic
  
  
  For finite words, the king of algebraic characterisations was  the -McNaughton-Papert-Kamp Theorem, which described the languages of finite words that can be defined in first-order logic (using the ordered model). Unfortunately, finding a generalisation of this theorem to forest algebra (or any other algebra modelling trees) remains an open problem[This is in contrast to first-order logic on trees with the child relation (and not the descendant ordering), which has an algebraic characterisation, see 
  [Theorem 1.]benediktSegoufin2009
  ].  Our discussion of first-order logic in the forest monad is limited to some remarks and one example. 
  
  As discussed in Section <ref>, 
  The Eilenberg Variety Theorem works also for the forest monad. One can show that, in the forest monad, the class of languages definable in first-order logic is a language variety, see Exercise <ref>. Therefore,  from the Eilenberg Variety Theorem  it follows that whether or not a language L ⊆Σ is definable in first-order logic depends only on the syntactic algebra of the language. However, it is not known if the corresponding property of syntactic algebras is decidable.  Here is an example  which shows that aperiodicity – which characterised the syntactic algebras for first-order definable languages of finite words – is not enough for forest algebra. 
  
    Consider an alphabet
      
      Σ = , context sort,    0,  1forest sort.
  
      A forest over this alphabet is the same thing as a multiset of positive Boolean formulas, as in the following picture:
      73
      We define the value of a node in a forest over this alphabet to be the value of the  Boolean formula in the subtree of the node. 
      Consider the language 
      
      L = t ∈Σ : t is a forest where all roots have value 1
  
      If we look at the syntactic forest algebra of this language, then both the forest semigroup and the context semigroup are aperiodic (in fact, they are idempotent). Nonetheless, the language is not definable in first-order logic, see Exercise <ref>.
  
  
  
  
  
  
  
   Prove that first-order logic, as discussed in Section <ref>, is a variety in the sense of the Eilenberg variety theorem.
  
  Show that the language of forests where some leaf has even depth is not definable in first-order logic.
  
   Show that the language from Example <ref> is not definable in first-order logic. 
  
   Consider a two-sorted alphabet 
  
      Σ = left, right, left, right.
  
  A binary tree over this alphabet is a tree where every node is either a leaf, or it has exactly two children, with labels “left” and “right” in the appropriate sort. There are no constraints on the root label. 
  Define L ⊆Σ to be the set of binary trees where all leaves are at even depth. Show that this language is first-order definable. Hint: show first that there is a first-order language which separates L from the set of binary trees where all leaves are at odd depth.
  
  
  Define anti-chain logic to be the variant of where set quantification is restricted to anti-chains, i.e. sets of nodes that are pairwise incomparable with respect to the descendant relation. Show that anti-chain logic can define all recognisable languages that contain only binary trees, as defined in Example <ref>.
  
  
  A unary node in a forest or context is defined to be a node with exactly one child. 
      Show that anti-chain logic with modulo counting can define every recognisable language where every element has no unary nodes.
  
  
  
   §.§ Branch languages
  
  In this section, we discuss  languages which are defined only by looking at branches in a forest or context. We say that a forest or context is a  branch if all nodes are linearly ordered by the ancestor relation. Here is a picture: 
  75
  A branch can be viewed as a word, consisting of the labels of the nodes in the branch, listed in root-to-leaf order. 
   For a forest or context t, define a branch of t to be any branch that can be obtained from  selecting some x which is either the port or a leaf, and restricting t to the ancestors of x. The branch is a context if x is the port, otherwise the branch is a forest. Here is a picture:
   74
  The following theorem gives a characterisation  of languages that are determined by only looking at branches. 
   
      For every language    L ⊆Σ,  not necessarily recognisable, the following conditions are equivalent
      
          
    (1) membership t ∈ L depends only on the set of branches in t;
          
    (2)  the syntactic forest algebra  of L satisfies the identities 
          
      a · (b + c) = ( a · b)  + ( a · c)distributivity  and  b + b  = b
                   idempotence of 
       the forest semigroup
                   for every a, b, cthe colour indicates the 
       sort of the variable∈ A.
  
      
      If L is recognisable, then the above conditions are also equivalent to:
      
          
    (3) L is a finite Boolean combination of languages of the form “for some branch, the corresponding word is in K⊆Σ^*”, where K is regular. Different languages K can be used in different parts of the Boolean combination.
      
  
   
      
          
    * (1)⇒(2) In the free algebra Σ, applying the identities from condition (2)  does not affect the set of branches.  
  
    * (2) ⇒ (1) For a forest or context  t ∈Σ, define its branch normal form to be the forest or context that is the union of all branches in t, as described in the following picture:
  76
  If the syntactic algebra satisfies the  distributivity identity  in the theorem, then a forest or context has the same image under the syntactic homomorphism as its branch normal form. Since the  branch normal form is determined uniquely by the multiset of branches, it follows that the image under the  syntactic homomorphism depends only on the multiset of branches[One could think that the  distributivity  identity alone (without the identity for idempotence) characterises exactly the languages where membership depends only on the multiset (and not just the set) of branches. This is not true, see Exercise <ref>.]. Thanks to the idempotence identity, it is only the set of branches that matters for membership in the language, and therefore the language must be branch testable.
  
    * (3) ⇔ (1) for recognisable languages.  Clearly (3) implies (1). Consider now the converse implication.  Let h be the syntactic homomorphism of a recognisable language.
  By condition (1) and the definition of a syntactic homomorphism,  membership t ∈ L depends only on the set 
  
      H(t) = h(s) : s is a branch in t.
  
  For every a in the syntactic algebra, define K_a ⊆Σ^+ to be the words that correspond to branches which have value a under the syntactic homomorphism. This language is recognised by a finite semigroup (which is easily constructed from the syntactic  forest algebra), and therefore it is regular. Finally, a ∈ H(t) if and only if for some branch the corresponding word is in K_a. Therefore, H(t) can be described using a finite Boolean combination of languages of the form K_a. 
      
  
  
  Condition (2) in the above theorem can be effectively checked given the syntactic algebra. Since the syntactic algebra can be computed for recognisable languages, it follows that one can decide if a   recognisable language satisfies any of the conditions in the above theorem. 
  
   Give an example of a language L ⊆Σ where membership depends only on the multiset of branches, but where the syntactic algebra violates the distributivity identity from Theorem <ref>.
  
  
  Give an algorithm which decides if a recognisable  language L ⊆Σ is of the form: “for some branch, the corresponding word is in K ⊆Σ^*”, for some regular K.  
  
      Let  L ⊆Σ be a language, not necessarily recognisable, which contains only forests. The following conditions are equivalent:
      
          
    *  There is some language K ⊆Σ^* such that 
          
      L = t ∈Σ : t is a forest and all of its path words belong to K.
  
          
    *  Every context a ∈Σ and forests b,  c ∈Σ satisfy the following equivalence:
       
      a· (b + c)∈ L   iff  a· b∈ L  and  a · b∈ L.
  
      
      Furthermore, if L is recognisable then one can check if the above conditions hold, and if they do, then K can be chosen so that it is a regular word language. 
  
  
      The implication <ref> ⇒ <ref> is immediate, since the same paths words appear on both sides of the equivalence.  Consider now the converse implication <ref> ⇒ <ref>.
      Clearly every universal path language is closed under eliminating subtrees,  because eliminating a subtree makes the set of path words smaller.
  
      For the converse implication, suppose that L contains only forests and is closed under eliminating subtrees. Define a linear tree to be a forest with one leaf.  Consider the following word language 
      
      K = unique path word of t : t is a linear tree in L⊆Σ^*.
  
      Because L is closed under eliminating subtrees, we have 
  
      L = ∀ K.
  
  
      A root-to-leaf path in a forest t can be seen as a linear tree which is obtained from t by keeping only the nodes that appear on the path. 
  
  
  
  
  
   §.§ Modal logic
  
  
  We finish  this section with a  discussion of tree variants for some of the temporal logics that  were discussed in Chapter <ref>. When working with trees and forests, we  use the terminology of modal logic, described as follows. 
  
  Define a Kripke model to be a directed graph with vertices labelled by some alphabet Σ. Here is a picture of a Kripke model:
  77
  Vertices of the Kripke model are called worlds, and the edge relation is called accessibility. Accessibility does not need to be  transitive. In this section, we  only study Kripke models where accessibility is acyclic. 
  To express properties of worlds in  Kripke models, we use modal logic,
  whose formulas are constructed as follows:
  
      athe current 
       world has
        label a ∈Σ    φsome 
       accessible 
       world 
       satisfies φ    φevery 
       accessible 
       world 
       satisfies φ    φ  φψ  φψBoolean combinations.
  
  We use the following notation for the semantics of modal logic:
  
      MKripke 
       model, vworld
       of Mφformula 
       of modal 
       logic.
  
  We  use modal logic to define properties of forests, by assigning a Kripke model to each forest, as explained in the following picture:
  78
  One could also assign a Kripke model to a context, by doing the same construction, except with a special marker for the port node. We choose not to do this, without any deeper reasons,  and therefore in what follows we only discuss languages that contain only forests. 
  
  
  
      We say that a formula of modal logic is true in a forest if it is true in the initial world of its Kripke model. 
      A  forest language is called definable in modal logic if there is a formula of modal logic that is true in exactly the forests from the language.
  
  
  The following theorem characterises  modal logic in terms of two identities. A corollary of the theorem is that one can decide if a language is definable in  modal logic, because it suffices to check if the identities hold in the syntactic algebra of a language. 
  
  
      Let L ⊆Σ be a language that contains only forests. Then  L  is definable in modal logic if and only if its syntactic forest algebra  is finite and satisfies the identities
      
      a + a = a    c^· a  =
          c^· b     for all a,  b,  c
          ,
  
      where ∈1,2,… is the idempotent exponent of the context semigroup.
  
   The rough idea is that the identities say that the membership in the language is invariant under bisimulation (the first identity) and depends only on nodes at constant depth (the second identity). These are exactly the properties that characterise modal logic.  A more detailed proof is given below.
  
      Define the modal rank of a formula to be the nesting depth of the modal operators  and . Here are some examples:
      
      amodal rank 0    ( a)  ( b)modal rank 1    ( (a  b)  ( b)modal rank 2.
  
      When the alphabet is finite and fixed, then there are finitely many formulas of given modal rank, up to logical equivalence. This is because, up to logical equivalence, there are finitely many Boolean combinations of formulas from a given set.
      To prove the theorem, we use a slightly more refined result, in the following claim, which characterises  the expressive power of modal logic of given modal rank. 
      
          A forest language can be defined by a formula of modal rank n ∈0,1,… if and only if its syntactic algebra satisfies the identities
      
      a + a = a     c^n· a  =
              c^n· b
  
      
      
          We say that a Kripke model is tree-shaped  if the accessibility relation gives a finite tree, with edges directed away from the root (this is the case for the Kripke models that we assign to forests).  We say that two tree-shaped Kripke models are bisimilar if one can be transformed into the other by applying the identity a+a=a, i.e. duplicating or de-duplicating identical sibling subtrees[For tree-shaped Kripke models this notion coincides with the usual notion of bisimulation for general Kripke models.]. For n ∈0,1,…, we say that two  tree-shaped  Kripke models are n-bisimilar if,  after removing all worlds that are separated by more than n edges from the root,  they  are bisimilar.  By induction on n one shows that every equivalence class of n-bisimilarity can be defined by a formula of modal logic with modal rank n; and conversely formulas of modal rank n are invariant under n-bisimilarity. The identities in the statement of the claim say that the forest language is invariant under n-bisimilarity, and hence the claim follows.  
      
      The theorem follows immediately from the above claim. Indeed, if the identities in the theorem are satisfied, then the language can be defined by a formula of modal logic with modal rank . Conversely, if the language is defined by a formula of nesting depth n, then membership in the language is not affected by nodes which are more than n edges away from the root, and therefore the syntactic algebra must satisfy
      
      c^ a  because c^ is idempotent
      c^ n a Claim <ref>
      c^ n b  
                because c^ is idempotent
      c^ b.
  
  
  
  
  
    
  *Transitive modal logic.  A formula of  modal logic as discussed above can only talk about nodes that are at some constant distance from the root. We now discuss a variant of modal logic which can talk about arbitrarily deep nodes. The formulas stay the same, only the interpretation of forests as Kripke structures changes.
  
  For a forest, define its  transitive Kripke model  in the same way as the Kripke model, except that the accessibility relation now describes the transitive closure of the child relation. In other words, accessibility now represents the proper descendant relation. 
  
  
      A   language that contains only forests  is called definable in transitive modal logic[In the terminology of  temporal logic, this logic is also called 𝖤𝖥, which refers to the “exists finally” operator of (branching time) temporal logic. Theorem <ref> is 
      [Theorem 5.3]bojanczykForestAlgebras2008.
      ] if there is a formula of modal logic that is true in (the initial world) of exactly the forests from the language.
  
  
  The following theorem characterises transitive modal logic in terms of two identities. A corollary of the theorem is that one can decide if a language is definable in transitive modal logic.
  
      Let L ⊆Σ be a language that contains only forests. Then  L  is definable in transitive modal logic if and only if its syntactic forest algebra is finite and  satisfies the identities
          
      a + a = a     c· a  =
               (c· a) + a    for all  a,  c.
  
  
  
      It is easy to see that the identities  must be true in the syntactic algebra of every  language  definable in transitive modal logic. The first identity says that the language must be invariant under bisimulation, which is clearly true for transitive modal logic. For the second identity, we observe that going from c · a to ( c · a)+a does not affect the transitive Kripke model, up to bisimulation.
  
      The rest of this proof is devoted to the right-to-left implication. Let      
      h : Σ→ A
  
      be a homomorphism into an algebra A that satisfies the identities.  By induction on the size of A, we will show that for every forest-sorted a ∈ A, the inverse image h^-1( a) is definable in transitive temporal logic (we say that such a is definable in the rest of the proof).  This immediately yields the right-to-left implication. 
      In the rest of the proof, we define the type of an element of Σ to be its  image under h. 
  
      In the proof, we  use  a reachability ordering on the algebra A  defined as follows. We say that a ∈ A is reachable from b ∈ A, denoted by a ≥ b, if there is some t ∈ A which uses b at least once, and which gives a under the multiplication operation of A.  (Reachability can be seen as the  forest algebra variant of the infix relation for semigroups.)  Reachability is easily seen to be a pre-order, i.e. it is transitive and reflexive.   We draw the reachability ordering in red when comparing forest-sorted elements. 
       Thanks to the identities in the theorem, reachability is  anti-symmetric when restricted to the forest sort, as explained in the following claim. 
      
          If  forest-sorted a,  b ∈ A are reachable from each other, then a = b.
      
      
          For forest-sorted a,  b ∈ A, reachability a ≥  b is equivalent to 
      
      a =  c· bfor some context-sorted c    or     a =  c +bfor some forest-sorted c    or     a = b.
   In the presence of the identities from the assumption of the theorem, all three conditions above imply  a =a+b. For the same reason, a ≤ b implies  b=a+b, and therefore a=b.
      
  
  In every finite forest algebra there is a maximal forest-sorted element with respect to reachability, because every two forests can be combined using +, into a forest that is bigger than both of them in the reachability ordering. 
  By Claim <ref>, the maximal element is unique. Fix the maximal element a for the rest of the proof.  
  
  The following claim uses the induction assumption on algebra size to give a sufficient condition for definability. 
  
      
          Let b∈ A be non-maximal and forest-sorted. Every  forest-sorted c ∈ A that is not reachable from b is definable. 
      
       Let I be the set of elements in A that are reachable from b.  This is an ideal, which means that if  t ∈ A contains at least one letter from I, then its multiplication is in I. Furthermore, this ideal contains at least two forest-sorted elements, by assumption that b is non-maximal. Define ∼ to be the equivalence relation on A which identifies two elements if they are equal, or both have the same sort and belong to the ideal I. Because I is an ideal, ∼ is a  congruence. Because b is non-maximal, the congruence is non-trivial, every forest-sorted equivalence class is definable thanks to the  induction assumption on algebra size. Because c is not reachable from b, it does not not belong to the ideal I, and thus its equivalence class  consists of c only, and hence c is definable. 
  
      We  use the above claim to show that, with at most two exceptions, all forest-sorted elements of A are definable. 
      Call an element b sub-maximal if it is not maximal and  c  >  b implies that c is maximal.   If c is neither maximal nor sub-maximal then  it is definable by the above claim, because  there is some sub-maximal b from which c is not reachable. For the same reason, if there are at least two sub-maximal elements, then all sub-maximal elements are definable.  In particular, if there are at least two sub-maximal elements, then all elements are definable: the non-maximal elements are all definable, and the maximal element is definable as the complement of the  remaining elements. 
  
      We are left with the case when there is exactly one sub-maximal element, call it b. We will show that the maximal element a is definable, and therefore b is definable (as the complement of the remaining elements).  Define the descendant forest of a node  x to be the forest that is obtained by keeping only the proper descendants of x.  Since we do not allow empty forests, the descendant forest is defined only when x is not a leaf.   
      
          A forest has type a if and only if it contains a node x, with label σ∈Σ, such that  one of the following conditions hold:
          
              
    * x is not a leaf, its  descendant  forest  has type d < b, and  h(σ) · d   =  a;  or
              
    * x is not a leaf, its descendant forest has type  d ≥ b, and h(σ) · b   =  a; or
              
    * the node x is a leaf and h(σ) =  a.
          
      
      
          To prove the bottom-up implication, we observe  that each of the conditions (1, 2, 3) implies that the subtree of x has type a; by  maximality of a the entire forest must then also have type  a.  For conditions (1, 3) the observation is immediate. For condition  (2), there are two cases to consider: either  the descendant forest has type a and the subtree of x has type a by maximality, or the descendant forest  has type b and   the subtree of x has type  a by h(σ) · b   =  a. 
          
          We now prove the top-down implication. 
          We show that if every node in a forest violates all of the conditions   (1, 2, 3), then the forest has type  ≤ b. This is proved by induction on the number of nodes. If the forest has only one node, then we use condition (3). The second case is when  the forest has at least two trees, i.e. it can be decomposed as 
             t = t_1 + t_2.
          By induction assumption, both t_1 and t_2 have types b_1,b_2 ≤ b. By the identity  in the statement of the  theorem, we get 
          
      b =  b +  b_1 + b_2,
  
          which implies that b_1 + b_2 ≤ b.  The final case is when t is a tree, whose root x has label σ and descendant forest s.  By induction assumption, s has a type d ≤ b.  If d < b then we use condition (1) to infer that t has type ≤ b, otherwise we use condition (2).
      
      To finish the proof of the theorem, it remains to show that the conditions in  the above claim can be expressed using transitive modal logic.  Condition (3) can easily be checked. In condition (1), the element d is definable because it is neither maximal nor sub-maximal. Therefore, 
       there is a formula of modal logic which is true in the world corresponding to a node  x (in the descendant Kripke model) if and only  if the descendant forest of x has type d. Therefore, there is a formula of modal logic which is true in the world corresponding to x if and only if it satisfies (1). For similar reasons, we can define condition (2), since the union of the languages for a and b is definable, by taking the complement of the remaining definable languages. 
  
  
  
  
  Consider transitive modal logic for the monad  of infinite forests and contexts, as discussed in Section <ref>. Show that for infinite forests, the equations from Theorem <ref> are sound (i.e. if a language is definable in transitive modal logic, then the syntactic algebra satisfies the equations) but not complete (i.e. the converse implication to soundness fails). 
  
    
  
  CHAPTER: HYPERGRAPHS OF BOUNDED TREEWIDTH
  
  
  
  In this chapter, we study  algebras for graphs. Although in principle the algebras can describe arbitrary graphs, the more interesting results will assume bounded treewidth.
  
  
  
  § GRAPHS, LOGIC, AND TREEWIDTH
  
  We begin by discussing graphs, but later we will move to a slightly more general notion, called hypergraphs,  which will provide the necessary structure to define a monad. In this chapter, the graphs and hypergraphs are assumed to be finite. 
  
  
      A graph consists of a set of a finite set of vertices, together with a binary symmetric edge relation. 
  
  Here is a picture of a graph, with dots representing vertices and lines representing edges:
  49
  
  We  use logic, mainly , to define properties of graphs, with graphs represented as models according to the following definition.
  
      Define the incidence model of a graph as follows. The universe is the disjoint union of the vertices and the edges, and there is a  binary incidence relation, which is interpreted as 
      
      (v,e) : vertex v is incident with edge e.
  
  
  The two kinds of elements in the universe of the incidence model – vertices and edges – can be distinguished using first-order logic: an edge is an element of the universe that is incident to some vertex, the remaining elements of the universe are vertices.
  
  
  Monadic second-order logic over the incidence model defined above, which is the main logic of interest in this chapter, is sometimes called  . A related logic is monadic second-order logic over a representation of graphs where the universe consists only of the vertices, and there is a binary relation for the edges. The related logic is sometimes called . The difference is that can quantify over sets of vertices and edges, while can only quantify over sets of vertices. (For first-order logic, the two ways of representing graphs as models does not affect the expressive power, since on first-order quantification over edges can be replaced by two first-order quantifications over vertices.)   
  The difference between and is explained in the following example.
  
      A clique is a   graph where every two vertices are connected by an edge.
  A  rectangular grid is a graph that looks like this:
      63
      Both cliques and rectangular grids can be defined both in  and in . Consider now the set of graphs which are cliques of prime size. A clique has prime size if and only if it satisfies the following property: (*) one cannot remove edges so as to get a rectangular grid which has at least two rows and at least two columns. Property (*) can  be directly expressed in , but it cannot be expressed in , see Exercise <ref>.
  
  
  
  In this chapter, we are mainly interested in monadic second-order logic. First-order logic can only define properties that are local[The notion of locality is made precise by the Gaifman Theorem, see
  [Theorem 2.5.1]ebbinghausFlumFinite
  ], e.g. the existence of  a cycle of length three:
  
      ∃ u ∃ v  ∃ v       E(u,v)    E(v,w)  E(w,u).
  
  A classical example of a property that is non-local, and therefore cannot be expressed in first-order logic, is graph connectivity.  Using an argument, one can show that a sentence of first-order logic cannot distinguish between a large cycle and a disjoint union of two large cycles:
  50
  On the other hand, connectivity  can be expressed in monadic second-order logic, already in the model,  as witnessed by the following sentence
  
      ∃ Xthere is a set
       of vertices,    (∃ v v ∈ X)  (∃ v   v ∉X) which is neither empty nor full, 
              (∀ v  ∀ w   E(v,w)  v ∈ X  ⇒ w ∈ X).and which is closed under taking edges
  
  
  
  Already first-order logic is undecidable on graphs, in the following sense: it is undecidable whether or not a sentence of first-order logic is true in some graph. This undecidability is explained in the following example.
  
      Consider directed graphs with coloured vertices and edges. These extra features can be easily encoded, using  first-order logic, in the undirected and unlabelled graphs that are discussed in this section, see the exercises.  A computation of a Turing machine can be visualised as a coloured rectangular grid, where each vertex represents a tape cell in a given moment of the computation, as in the  following picture:
      51
      By formalising the definition of a computation of a Turing machine,  one can write a sentence of first-order logic, which is true in a connected graph if and only if it represents an accepting computation of a given Turing machine. From this, one can deduce that the  halting problem reduces to satisfiability for first-order logic over finite graphs, see Exercise <ref>.  
  
  
  We will no longer discuss first-order logic for graphs. Also, from now on, when talking about , we mean the variant that uses the incidence model from Definition <ref>[This difference is not so important in the context of this chapter. This because we will be mainly interested in graphs of bounded treewidth, and for bounded treewidth the logics and models are equivalent, see Exercise <ref>].
  
  
  
   Show that for graphs without edges, and has the same expressive power as first-order logic.
  
   Show that the set of cliques of prime size from Example <ref> cannot be defined in . 
  
  
  
   For the purposes of this exercise, we consider directed graphs with two types of edges, blue and red. For such a graph, the associated model has the vertices as the universe, and two binary predicates for the red and blue edges. 
  Show that rectangular  grids, as described in Example <ref>, can be defined in first-order logic. We assume that the input graph has one connected component.
  
  
  Show that the following problem is undecidable: given a sentence of first-order logic, decide if it is true in at least one finite graph.Take the sentence from Exercise <ref>, and add a further requirement that there is at least one corner. 
  
   Unlike for the rest of this chapter, this exercise and the next one consider possibly infinite graphs.
      Consider two decision problems: (a) is a sentence of first-order logic true in at least one  finite graph; (b) is a sentence of first-order logic true in at least one  possibly infinite graph. Show that (a) is recursively enumerable (there is a Turing machine that accepts yes-instances in finite time, and does not halt on no-instances), while (b) is a co-recursively enumerable (there is a Turing machine that does not halt on yes-instances, and rejects no-instances in finite time).
  
      Show that for every k ∈1,2,… the following property of graphs is definable in using the incidence model: “the graph is connected, infinite, and has degree at most k”. Show that “the graph is connected and infinite” is not definable. 
  
  
  Show that the existence of a Hamiltonian cycle cannot be expressed in ,  using the representation of graphs as models.[Hamiltonian cycles]
      Using over the incidence model, we can say that a graph has a Hamiltonian cycle, i.e. a  cycle that visits every vertex exactly once. A Hamiltonian cycle can be seen as a set of edges such that: (a) every vertex in the graph is incident to exactly two edges  one incoming and one outgoing edge from the set; (b) the subgraph induced by the chosen edges is  connected. 
      The incidence model is important here. In the model, where quantification over sets of edges is not allowed, one cannot express the existence of a Hamiltonian path[Proposition 5.13]courcelleGraphStructureMonadic2012.
  
  
  
  Show that the existence of an Euler cycle (every edge is visited exactly once) cannot be expressed in , using the representation of graphs as  models.
  
  
  
  Consider graphs which allow parallel edges (i.e. multiple edges connecting the same two vertices). The incidence model makes sense for such graphs as well. For ℓ∈1,2,… define the ℓ-reduction of a graph to be the result the following operation: for each pair of vertices v,w we only keep the first ℓ edges that go from v to w. Show that for every sentence φ there is some ℓ such that φ is true in a graph (with parallel edges) if and only if it is true in its ℓ-reduction.
  
  
  
  
   §.§ Treewidth
  
  The undecidability problems described in Example <ref> are avoided if we consider graphs that are similar to trees. The notion of similarity that we care about is treewidth, as defined below[For an introduction to treewidth, including a brief history, see 
  [Section 12.]diestel
  ].
  
      A tree decomposition  consists of:
      
          
    * a graph, called the underlying graph;
          
    * a set of nodes, equipped with a tree ordering (i.e. there is a least node called the root, and for every node x, the set of nodes <x is totally ordered);
          
    * for each node, an associated nonempty set of vertices called its bag.
      
      These should satisfy the following constraints:
          
              
               
    *   every edge in the underlying graph is covered by some bag, i.e. there is some bag that contains  both endpoints of the edge;
              
    *  every vertex v of the underlying graph is introduced in exactly one node, which means there is exactly one node x such that v is in the bag of x and either x is  the root or v is not in the bag of the parent of x.
  
  Here is a picture of a tree decomposition:
  112
  In the picture, the gray circles are bags, and the dotted lines connect appearances of the same vertex in several bags.
  The width of a decomposition is defined to be the maximal size of bags, minus one. For example, the tree decomposition in the above picture has width two, because its maximal bag size is three. The treewidth of a graph is the minimal width of a tree decomposition for the graph. 
  
  The reason for the minus one in the definition of width is so that trees, where the bags in the natural have tree decomposition have size two, get assigned treewidth one. This is illustrated in the following picture:
  55
   Forests (i.e. disjoint unions of trees) are the only graphs of  treewidth one. 
  
  Cycles have treewidth two, as illustrated in the following example:
  56
  The tree decomposition in the above picture is a path decomposition, i.e. every node in the tree decomposition has at most one child. Path decompositions will play an important role in Section <ref>.
  
  If a graph has k+1 vertices, then it has treewidth at most k, since one can always use a trivial tree decomposition where all vertices of the graph are in the same bag. For cliques, the trivial tree decomposition is optimal, as explained in the following example.
  
      
           We show that for cliques, every tree  decomposition must have a bag which contains all vertices. Consider a tree decomposition of a clique. If two vertices in a tree decomposition are connected by an edge, then the nodes which introduce these two vertices must be related by the ancestor relation (if they would be unrelated, then there could be no bag that contains both vertices). Therefore, in a tree  decomposition of a clique,  the nodes that introduce the clique vertices  must be linearly ordered by the ancestor relation. The maximal, i.e. furthest from the root, node in this linear order must have all vertices of the clique in its bag.
      
      
  
      Another example of graphs with unbounded treewidth is rectangular grids, see the exercises. In fact, the Grid Theorem[
          For a recent paper about the Grid Theorem, see
          ChuzhoyT19
      ], which is stated but not proved in the exercises, says that a class of graphs has unbounded treewidth if and only if it contains all rectangular grids as minors. 
      We will show later in this chapter that for  every k ∈1,2,…, the class of graphs of treewidth at most k has a decidable theory. In the exercises we also discuss a corollary of the Grid Theorem, which says that decidability of for bounded treewidth is  optimal:  if the theory of a class of graphs is decidable, then the class has bounded treewidth. 
      
      
  
  
  
  
  
  
  
      We say that a graph G is a minor of a graph H if one can find a family of disjoint vertex sets 
      
      X_v ⊆vertices of H_v ∈vertices of G
  
      such that every set of vertices U in G satisfies:
      
      U is connected in Ga subset of vertices is connected if 
       the induced subgraph is connected    implies     (⋃_v ∈ U X_v)  is connected in H.
  
      (It is enough to check the implication for sets U with at most two vertices; and we assume that one vertex sets are connected).
      Show that if L is a property of  graphs that is definable in using the incidence model (we use the incidence model for the remaining exercises), then the same is true for  “some minor satisfies L”.
  
  
  
  
  The Grid Theorem says that if a class of  graphs has unbounded treewidth, then for every n ∈1,2,… there is some graph in the class which has an n× n grid as a minor. Using the Grid Theorem, prove that if a class of  graphs has decidable theory, then it has bounded treewidth.
  
  
  
  Show a class of graphs that has undecidable theory and bounded treewidth.  Graphs where the number of vertices belongs to some undecidable set of numbers.
  Show that the n × n grid has treewidth at least n.
  
  Show that for every language L ⊆ a^* recognised in linear time by a (possibly nondeterministic) Turing machine, the language 
  
      G : G is an n × n grid such that a^n ∈ L
  
  is definable in .
  
  Show that if a graph has treewidth at most k, then one can choose an orientation of its edges so that every vertex has at most k+1 outgoing edges. Take a tree decomposition, and direct each edge  so that it points toward the vertex that is introduced closer to the root. Under this orientation, the edges that point away from a vertex v must necessarily point to vertices that are in the bag when v is introduced, and there are at most k+1 such vertices (including v).
   Show that for every k ∈1,2,…, the logics and have the same expressive power for graphs of bounded treewidth.
  
  
  
  
  § THE HYPERGRAPH MONAD AND COURCELLE'S THEOREM
  
  In this section, we introduce algebras for graphs. These algebras are defined in terms of  a monad  that describes graphs[This monad is based on the hyperedge replacement algebras of Courcelle. A discussion of these algebras can be found in
  [Section 2.3.]courcelleGraphStructureMonadic2012
  The presentation of  hyperedge replacement that uses monads is based on
  bojanczykTwoMonadsGraphs2018
  .]. In order to define the monad, we will need to add more structure to graphs, namely labels, directed hyperedges (i.e. edges that connect a number of vertices that is not necessarily two), and distinguished vertices called ports.  We use the name hypergraph for graphs with such extra structure. 
  
    Like any monad, the hypergraph monad will allow us to talk about  algebras, homomorphisms, terms, recognisable languages, syntactic algebras, etc. The main result of this section is  Courcelle's Theorem, which says that every graph property definable in is necessarily recognisable. In the next Section <ref>, we prove a converse to Courcelle's Theorem, which  says that for bounded treewidth, recognisability implies definability in .
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  We begin with a formal definition of hypergraphs.
  
      A hypergraph consists of:
      
          
          
    * A set V of vertices.
          
    * A set E of hyperedges. Each hyperedge has an associated arity in 0,1,….
          
    * A  set Σ  of labels. Each label has an associated arity in 0,1,….
          
    * A non-repeating sequence of distinguished vertices called ports;
          
    * For each hyperedge e, an associated  label in Σ of same  arity, and a non-repeating sequence of incident vertices whose length is the arity of e.
      
  
  In the end, we care mainly about hypergraphs that have no ports, i.e. the sequence of ports is empty, but the ports will appear when decomposing hypergraphs into parts.  We use the name  non-port vertices for  vertices that are not ports.
   For a hyperedge e of arity n, we write 
  e[1],…,e[n] for the sequence of incident vertices, and use the name incidence list for this sequence.
  In this chapter, all hypergraphs are assumed to be finite, which means that there are finitely many vertices and hyperedges.  
  
  
  We draw hypergraphs  like this:
      42
  To avoid clutter in the pictures,  we  skip the gray numbers on the edges and the numbers of the ports, in situations where  they are not important for the picture or implicit from the context. 
  
  
  
  
  
  
  
          A  graph can be represented  as a  hypergraph. The representing hypergraph has no ports, and the vertices are the same as in the graph. Each edge of the graph is represented by two binary hyperedges (with some fixed label), one in each direction.   Here is a picture:
          48
          Directed graphs can be represented in the same way, but with the hyperedges not necessarily using both opposing directions. 
          
  
    
  *The hypergraph monad. We now describe the monad structure of hypergraphs. The main idea behind free multiplication is that  a hyperedge can be replaced by a hypergraph of matching arity[This is the reason why Courcelle uses the name hyperedge replacement for the corresponding algebras. ].  This replacement, which will be the free multiplication in the monad, is illustrated in the Figure <ref>.
  
  
          
  
  
  
   The hypergraph monad, denoted by , is defined as follows.
      
          
    * The underlying category is the category of ranked sets
          
      𝖲𝖾𝗍^0,1,…,
  
          which is the category of sorted sets, where the sort names are natural numbers.
          Objects  in this category are ranked sets, i.e. sets where every element has an associated arity in 0,1,…. Morphisms are arity-preserving functions between ranked sets.
  
          
    * For a ranked set , the ranked set   consists of finite hypergraphs labelled by , modulo isomorphism. The arity of a hypergraph is  the number of ports.
          
    * For a  function f : Σ→Γ,  the function f : Σ→Γ
  applies f to the  labels, without changing the rest of the hypergraph structure.
  
    * The unit operation in the monad associates to every  letter a ∈ of arity n  a  hypergraph  which has n ports, no other vertices, and one    hyperedge labelled by a which is incident to all ports (in increasing order).  Here is a picture:
  64
  
    * Let G ∈ be a hypergraph labelled by hypergraphs. Its free multiplication  is defined as follows. The vertices are vertices of G, plus pairs (e,v) such that e is a hyperedge of G and v is a non-port vertex in the hypergraph G_e that is the label of the hyperedge e.  The  hyperedges are pairs (e,f), where e is a hyperedge of G and f is a hyperedge in G_e. The arities and labels of hyperedges are inherited from the second coordinate, while the incidence lists are defined by 
  
      (e,f)[i] = 
          f[i]    if f[i] is a non-port vertex
      
          e[j]    if f[i] is the j-th port.
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  We leave it as an exercise for the reader to check that the above definition satisfies the monad axioms. This completes the definition of the hypergraph monad. 
  
  The hypergraph monad generalises the forest monad, as shown in the following example.
  
       A forest can be represented as a hypergraph of arity one, as explained in the following picture:
  80    
  Nodes of forest type in the forest are represented by hyperedges of arity one, while nodes of context type are represented by hyperedges of arity two.
  
  A context can be represented as a hypergraph of arity two:
  81
  This representation is consistent with the monad structures of the forest monad and the context monad. Therefore, we can think of the forest monad as being a sub-monad of the hypergraph monad (when we identify the forest sort with arity 1, and the context sort with arity 2). In particular, from every algebra of the hypergraph monad we can extract an algebra of the forest monad.
  
  
  
  The rest of this section is devoted to discussing the algebraic notions that arise from the hypergraph monad, such as   algebras, homomorphisms, recognisability, and terms. 
  
  
  
  
  Show that  satisfies the monad axioms.
  
  Let us focus on associativity of free multiplication:
  
      @R=2pc @C=7pc [r]^free multiplication on [d]_(free multiplication on )   [d]^free multiplication on 
      [r]_free multiplication on
  
  Let G ∈. If we apply to G the functions on the down-right path  in the above diagram, then the resulting  hypergraph will have hyperedges  of the form
  
      (e,(f,g)),
  
  where e is a hyperedge of G, f is a hyperedge in the label of e, and g is a hyperedge in the label of f. If we apply the functions on the right-down path, then the resulting   hypergraph will have hyperedges of the form
  
      ((e,f),g),
  
  where the conditions on e,f,g are the same as above. We will show that 
  
      (e,(f,g)) ↦ ((e,f),g)
  
  is going to be an isomorphism between the two portd hypergraphs. 
  
  
  Show that connected hypergraphs are also a monad.
  
  
  
  
   §.§ Recognisable languages
  
  In this chapter, we are mainly interested in languages recognised by algebras in the hypergraph monad. 
  We use the name hypergraph algebra for such algebras. We  are especially  interested in languages recognised by hypergraph algebras that are finite in the sense that they are finite on every arity. 
  
  
  
      Let M be a commutative monoid.  Define a hypergraph  algebra as follows. The underlying ranked set  A has a copy of M on each arity, i.e. the underlying set is 
      
      M ×0,1,…the arity.
  
      The multiplication operation in the hypergraph algebra inputs  a hypergraph  in A and outputs the multiplication – in the monoid M – of all the labels of its hyperedges. Because the monoid is commutative, the order of multiplication is not important.  The result of this multiplication is viewed as an element of the copy of M that corresponds to the arity of the input hypergraph.
      It is not hard to see that this operation is associative, i.e. it satisfies the axioms of  Eilenberg-Moore algebras.
      
      The hypergraph algebra constructed this way can be used to  recognise some simple languages of hypergraphs.   Apply the above construction to   the commutative  monoid
      
      M =  (0,1,),
  
      yielding a hypergraph algebra  A. This  algebra  recognises the  language 
      
      G ∈Σ : some  hyperedge has label in Γ    for ranked sets Γ⊆Σ.
  
      The homomorphism maps a hypergraph to 1 if it belongs to the language, and to 0 otherwise, with the number stored in the copy of the monoid that matches the arity of the input graph.
      Another application of this construction is recognising  the language of hypergraphs with an even number of hyperedges;  here the appropriate monoid is the two element group.
  
  
  The hypergraph algebras in the above example are infinite, but finite on every arity.  This is the best we can do in the hypergraph monad, because it is impossible for a hypergraph algebra to have an underlying set that is finite altogether. The reason is that the multiplication operation μ :  A → A in a hypergraph algebra is arity-preserving, and  A is nonempty on every arity (as witnessed by hypergraphs without hyperedges). Therefore,  the underlying set of a hypergraph algebra must be  nonempty on every arity. 
   In the following definition, and for the rest of this chapter, we assume that  “finite hypergraph algebras” are those which have finitely many elements for each arity. 
  
      We say that a language L ⊆ is recognisable  if it is recognised by a homomorphism into a hypergraph algebra which has finitely many elements on every arity.
  
  This definition will turn out to be not restrictive enough, as far as general hypergraphs are concerned, see Example <ref>. In fact, no entirely satisfactory definition of “finite algebra” for general hypergraphs is known, and possibly does not exist. However, for hypergraphs of  bounded treewidth, hypergraph algebras that are finite on every sort will  be a satisfactory definition that is equivalent to , as we will see in Section <ref>.
  
   In Example <ref>, we already saw some examples of recognisable languages of hypergraphs. Here are some more  examples.
  
  
      Define a path in a hypergraph to be sequence of the form
      
      v_0 e_1→ v_1 e_2→⋯e_n-1→ v_n-1e_n→ v_n,
  
      where v_0,…,v_n are vertices  and e_1,…,e_n are hyperedges, such that each hyperedge e_i is incident with both v_i-1 and v_i. Note that the notion of path does not depend on the order of the incidence lists for the hyperedges. The source of the path is the vertex  v_0, its target is  the vertex v_n, and we say that  the path connects the source with the target. A hypergraph is called connected if every vertex  can be connected to every other vertex  via a path. Define h to be the function 
      which maps a hypergraph to the following information: (a) its arity; (b) is there a pair of non-connected  vertices such that at least one of them is not a port; and (c) which pairs of ports are connected.   One can check that this function is compositional, and therefore its image can be equipped with the structure of a hypergraph algebra so that h is a homomorphism.  The corresponding hypergraph algebra is finite on every arity. Therefore, the language of connected hypergraphs is recognisable.
  
  
  
      
      In this example, we show that the language of k-colourable hypergraphs is recognisable.
      Define a k-colouring of  a  hypergraph  to be a function from vertices to 1,…,k 
      such that no hyperedge has an incidence list that   uses some  colour twice. (In particular,  all  hyperedges have arity at most k.) Define h to be the function which maps a hypergraph to the following information: (a) its arity; (b) which functions from the ports to 1,…,k can be extended to k-colourings. If the hypergraph has arity zero, then (b) is just one bit of information: is there a k-colouring or not. This function is compositional, and has finitely many values for each arity, and therefore the language of k-colourable hypergraphs is recognisable. 
  
  
  The following example illustrates a problem with of our notion of recognisability, which is that it allows for too many algebras, at least as long as hypergraphs of unbounded treewidth are allowed.
  
      
      We say that a hypergraph is a clique if every two vertices are adjacent (i.e. connected by some hyperedge).  Let 
      
      P ⊆0,1,…
  
      be any set of natural numbers, possibly undecidable.  We will show that  the  language “cliques with no ports, where the number of vertices is in P” is recognisable.  
  Let h be the function which maps a hypergraph to the following information: (a) its arity; (b) is there a pair of non-adjacent vertices such that at least one of them is not a port; (c) which ports are adjacent.  If the arity is zero, then h also stores: (d) is the number of vertices in P. This function is compositional, and has finitely many values for each arity, and therefore the language  “cliques whose size is in P” is recognisable.  
  
  As we will see later on, the problem from the above example will disappear once we restrict attention to hypergraphs of bounded treewidth. 
  
  
  
  Show that every recognisable language in the hypergraph monad has a syntactic algebra. 
  
  
  
   §.§ Terms and tree decompositions.
   Tree decompositions and treewidth can be naturally extended to hypergraphs, as formalised in Definition <ref> below, and illustrated in the following picture:
  111
  In this section we discuss an alternative perspective on treewidth, which is defined using monad terminology.
   
  
  
       Tree decompositions are defined for hypergraphs in the same way as for graphs, with the following differences: (a) for every hyperedge there must be some bag which contains  its entire incidence list (we say that such a bag covers the hyperedge); (b)  every port  of the hypergraph appears in the root bag.
  
  
    As before, the width of a tree decomposition is the maximal bag size minus one, and the treewidth of a hypergraph is the minimal width of a tree decomposition.  For hypergraphs which represent graphs (i.e. no ports, and every edge is represented by two binary hyperedges in opposing directions), the above  definition coincides with Definition <ref>.
  
  A bag in a tree decomposition can cover an unbounded number of hyperedges. This will not be a problem for our intended applications, since the properties of hypergraphs that we study will not depend in an important way on parallel hyperedges (i.e. hyperedges with the same incidence lists).
  
  
  
    
  *Tree decompositions as terms.
    The algebraic structure of the hypergraph monad can be used to give an alternative description of treewidth.  
  Recall the notion of terms from  Section <ref>: a term  over variables X is any element of X. As was the case for the forest monad,  terms in the  hypergraph monad are sorted, which means that each variable used by the term has an arity, and the term itself has an arity. If A is a hypergraph algebra, then a term t ∈ X  induces a term operation t^A§ and defined by
  
      η∈ A^Xan arity-preserving
       valuation of the variables    ↦    multiplication in A applied to (η)(t).an element of the hypergraph algebra A, 
       whose arity is the same as the arity of t
  
  §§As was the case for forest algebra,   term operations  are in general  not   arity-preserving, if only because their inputs do not have a well-defined arity.
  
  Since a term is a hypergraph, it has some treewidth. The following lemma shows that hypergraphs of treewidth at most k are closed under applying (term operations induced by) terms of treewidth at most k. The  hypergraph algebra used in the lemma is the  free hypergraph algebra.
  
      Let t ∈ X be a term and let η∈ (Σ)^X be a valuation of its variables. If the hypergraphs t and η(x)_x ∈ X have treewidth at most k, then the same is true for the result of applying the term operation t^Σ  to η.
  
  
      Take a tree decomposition for the term t. For every hyperedge e which is labelled by a variable x, find a node of the tree decomposition whose bag contains the incidence list of the hyperedge, remove the hyperedge,  and add a child to this node with a tree decomposition of η(x).
  
  
  A corollary of the above lemma is that there is a well-defined monad for hypergraphs of treewidth at most k. 
  
  
  
  
  
  
  This monad, call it _k, uses only hypergraphs with treewidth at most k, with all the monad structure inherited from .  The underlying category is ranked sets with arities at most k+1; since hypergraphs with bigger arities will have treewidth at least k+1. 
  
  
  
    
  *The treewidth terms. We now show a family of terms which can be used to generate all hypergraphs of given treewidth.  Define the treewidth terms to be the terms[There is an  inconsistency in our use of the words “introduce” and “forget”. When we say that a node in a tree decomposition introduces a vertex, we take a top-down perspective on tree decompositions. On the other hand, the name of the “forget” term in Figure <ref> is based on a bottom-up perspective of the same phenomenon. ] from Figure <ref>. For  a hypergraph algebra, §define its treewidth k operations to be the term operations induced in the algebra by treewidth terms that have treewidth at most k. 
  The following theorem shows that the treewidth terms can be used to generate all hypergraphs of given treewidth.
  
  
  
  
  
  
  
  
  
   
  
  
  
  
  
  
  
  
  
    Let k ∈1,2,…. A hypergraph has treewidth  at most k if and only if it can be generated (in the free hypergraph algebra) from  hypergraphs with no vertices and at most k+1 ports,  by applying treewidth k operations.
  
  
      The right-to-left implication follows from Lemma <ref>.
      
      Consider now the left-to-right implication.
      Every tree decomposition can easily be modified, without affecting its width, into a tree decomposition which satisfies: (*) the root bag contains the ports and no other vertices, and if a node has at least two children, then the node and all of its children have the same bag.  To ensure condition (*), we can insert an extra node on every parent-child edge which has the same bag as the parent. By a simple induction on the size number of nodes, one shows that for every width k tree decomposition satisfying (*), the underlying hypergraph can be generated using the treewidth k operations as in the statement of the lemma. 
  
  
  Using the above theorem, and the same  argument as in Theorem <ref>, we get the following corollary, which gives a finite representation for algebras in the monad _k of hypergraphs of treewidth at most k. 
  
  
      Let k ∈1,2,… and consider the monad _k of hypergraphs with treewidth at most k. The multiplication operation in an algebra over this monad is uniquely determined by its treewidth k operations.
  
  
  
  
  
  
   §.§ Courcelle's Theorem
  
  In this section, we prove Courcelle's Theorem[Theorem 4.4]courcelleMonadicSecondorderLogic1990, which says that all languages definable in are recognisable. 
  To define properties of hypergraphs in , we use a hypergraph version of  the incidence model,  defined as follows. 
   The incidence model of a hypergraph is defined as follows. The universe is vertices and hyperedges, 
      and it is equipped with the following relations:
      
      e[i]=vv is the i-th
       vertex incident to e    port_i(v)v is the 
      i-th port    a(e)hyperedge e
       has label a..
  
      The arguments of the relations are e and v, while i ∈1,2,… and a ∈Σ are parameters. Each choice of parameters gives a different relation.
  
  As was the case for forest algebra, recognisability holds also for counting , which extends by allowing the following form of modulo counting: for every n ∈2,3,… and every ℓ∈0,1,…,n-1 there is a predicate
  
      |X| ≡ℓ n,
  
  which inputs a set and says if the size of this set is congruent to ℓ modulo n.  The modulo counting predicate is a second-order predicate, since it inputs a subset of the universe, and not an element (or tuple of elements) in the universe. 
  Counting is more powerful than  without counting, e.g. “the number of vertices is even” can be defined in counting but not in , see Exercise <ref>.   As we will show in Section <ref>, counting is enough to describe all recognisable properties of hypergraphs, assuming bounded treewidth.
  
  
  
      If a language  L ⊆Σ is definable in counting , over the incidence model, then it is recognisable, i.e. recognised by a homomorphism
      into a hypergraph algebra that is finite on every arity.
  
  
  
  We use the same construction as in previous chapters. The main step of the proof, which deals with set quantification, is presented in the following lemma.
  
      The recognisable languages images  under  functions of the form[In Exercise <ref> we show that the assumption on letter-to-letter homomorphisms is important.] 
      
      f : Σ→Γ    for f : Σ→Γsuch functions are called letter-to-letter homomorphisms.
  
  
  
      We  use a powerset construction for hypergraph algebras. Since we have already used powerset constructions before, we  take this opportunity     to discuss powerset constructions  in more detail and  generality, so that we can  think about the kinds of monads that allow a powerset construction  (these are not all monads, e.g. the group monad does not have a powerset construction).
  
      
      
      For a ranked set X, define its powerset to be the ranked set X where elements of arity n are sets of  elements from A that have arity n. For an arity-preserving function f : X → Y  on ranked sets, define 
      
      f :  X → Y
  
      to be the arity-preserving function that maps a set to its image[In the language of category theory,  is the co-variant powerset functor, as opposed to the contra-variant powerset functor which uses inverse images instead of forward images.]. For a ranked set X, define distribution on X to be the function of type 
  
      X → X
  
  which inputs a hypergraph G, and outputs the set of hypergraphs that can be obtained from G by choosing for each edge an element of its label.  
  
  Although trivial in the hypergraph monad, the following  claim is not true in general for all monads, e.g. it would be false in the group monad for a naturally defined distribution.  
  
       Distribution is a natural transformation, which means that the following diagram commutes for every arity-preserving function f : X → Y
      
      X
              [r]^ f[d]_distribute on X    Y
              [d]^distribute on Y
       X 
              [r]_ f    Y
  
  
  
       The right-down path corresponds to the following procedure: for each hyperedge, choose an element of its label, and then apply f. The down-right path corresponds to the following procedure: for each hyperedge, take the image under f of its label, and then choose an element.  The two procedures give the same result. This is true thanks to the following  property of distribution for hypergraphs: if we apply distribution on X to some hypergraph, then every hypergraph in the resulting set will have the same vertices, ports and hyperedges as the original hypergraph. This property would not hold, for example, in the group monad, and  the claim would be false in the group monad[In fact, there is no powerset construction for algebras in the group monad. Nevertheless, by a proof that does not use powerset algebras, one can show that in the group monad the recognisable languages are closed under images of letter-to-letter homomorphisms.].
  
  
  
  We  use the powerset and distribution to prove the lemma. 
      Suppose that a language L is recognised by a homomorphism
      
      h : Σ→ A,
  
      and consider a letter-to-letter homomorphism
      
      f : Σ→Γ.
  
      Define g to be the composition of the following two functions:
      
      @C=3cmΣ[r]^distribute on Σ   Σ[r]^ h    A.
  
      
          The function g is compositional.
      
       Consider the following diagram, with  red letters being labels of  faces:
          
      @C=3cmΣ[rr]^ g[dddd]_ free 
       multiplication
       on Σ[dr]_(distribute  on Σ)           A
                      [dd]^ distribute 
        on A
         Σ u A
                      [ur]_ h[d]_distribute on Σ   
       r B
                         Σur C
                      dr D
                      [d]_(free multiplication on Σ)[r]_ h    A
                      [dd]^(multiplication 
       in the algebra A)
         Σ[dr]_ h d E
                         
      Σ[rr]_g
                      [ur]^distribute on Σ           A
  
              If we prove that the perimeter of the diagram commutes, then we will prove that g is compositional (the composition of the two arrows on the right-most side will be the multiplication operation in the powerset algebra A). Faces A and E commute by definition of g. Face C commutes by naturality of distribution from Claim <ref>, and face D commutes because h is a homomorphism. It remains to show that face B commutes. This again, is proved via simple check[In the language of category theory,   face B is the main axiom of a distributive law of a monad over a functor. ], similarly to Claim <ref>.
      
      
      Like for any compositional function, the image of g can be equipped with a multiplication operation which turns g into a homomorphism.  (As mentioned in the proof of the claim above, this multiplication operation is the composition of the two arrows on the right-most side side from the diagram.) We will use the homomorphism g to recognise the image of L under f.
      Consider the following diagram:
      
      @C=5cmΓ[r]^ (inverse image under f)[d]_inverse image under f   Σ[dl]_distribute on Σ    [d]^g
      Σ[r]_ h    A
  
      The top-left  face in the diagram commutes by definition of distribution, and the bottom-right  face  commutes by definition of g.  A hypergraph G ∈Γ belongs to the image of the language  L under the function f if and only if applying the function on the down-right path in the diagram gives a set that intersects the image h(L).  Since the diagram commutes, it follows that the right-down path in the diagram recognises the image of the language L under the function L. The right-down path is a homomorphism, as a composition of two homomorphisms. Also, A is finite on every arity, because finiteness on every arity is preserved by powersets. 
  
  
  The above lemma implies that recognisable languages are closed under quantification of sets of hyperedges (since a subset of the hyperedges can be seen as a colouring of hyperedges with two colours “yes” and “no”). This motivates the following logic.
  
  
  
  
      Define hyperedge  counting  to be the following variant of . There is no first-order quantification, and set quantifiers range over sets of hyperedges. The logic allows the following relations on sets of hyperedges:
  
      X ⊆ Yset inclusion    
              X ⊆ a
          every 
       hyperedge  
       in X has 
       label a ∈Σ    
              i ∈ X[j]
          there exists a 
       hyperedge 
      e ∈ X 
          such 
       that e[j] is
        the i-th port    
              X[i] ∩ Y[j] ≠∅there exist hyperedges 
      e ∈ X and f ∈ Y
       such that e[i]=f[j]    |X| ≡ 0  n.the number of hyperedges 
       in X  is dvisible by n
  
  In the above relations, the arguments are the sets X, Y. The  labels a ∈Σ and numbers i,j,n ∈1,2,… are parameters. Each choice of parameters gives a different relation.
  
  
  Our usual proof of the translation of to algebras shows the following result, which is almost Courcelle's theorem, except that the logic is hyperedge instead of counting . The minor difference between the two logics – which boils down to isolated vertices – will be treated later on.
  
      If a language L ⊆Σ is definable in hyperedge counting , then it is recognisable.
  
  
      Same proof as for the monads for words and forests.  Consider a  formula of hyperedge counting 
      φ(X_1,…,X_nthe free variables represent
       ses of hyperedges),
  
      where Σ is the ranked set of labels used by the underlying hypergraphs. Define the  language of this formula  to be the set hypergraphs over an extended alphabet that consists of  2^n disjoint copies of Σ. This language is defined in the same way as for words and forests: for each hyperedge, the bits from 2^n in its label determine which of the sets X_1,…,X_n contain the hyperedge. By induction on formula size, we prove that every formula has a  recognisable language. 
      The induction step is proved in the same way as for words and forests: for Boolean combinations we use homomorphisms into product algebras, while for the quantifiers we use the powerset construction from  Lemma <ref>.
      
      We are left with the induction base. 
      For the formulas  X ⊆ Y and X ⊆ a, the corresponding hypergraph language is of the form “every hyperedge has a label in Γ⊆Σ”. Such languages were shown to be recognisable in Example <ref>. In the same example, we showed how to count hyperedges modulo some number, thus showing recognisability of the modulo counting relation. Consider now the language which corresponds to the  relation
      
      i ∈ X[j].
  
      Let h be the function h which maps a hypergraph to the following information: (a) its arity; (b) which ports belong to X[j]. This function is easily seen to be compositional, and it has finite image on every arity, and therefore it witnesses recognisability of the language corresponding to i ∈ X[j]. A similar argument works for 
  
      X[i] ∩ Y[j] ≠∅.
  
  
  
  
      For every sentence of counting , there is a sentence of hypergraph counting which gives the same results on hypergraphs without isolated vertices.
  
  
      Let n be the maximal arity of letters in the finite alphabet.  Every set X of  non-isolated vertices can be represented by n sets of hyperedges as 
      
      X = X_1[1] ∪⋯∪ X_n[n],
  
      where X_i is the set of hyperedges whose i-th incident vertex is in X. Using this representation, we can quantify over sets of non-isolated vertices by using quantification over sets of hyperedges. 
  
  
  A corollary of Lemmas <ref> and <ref> is that for every language definable in counting , there is a recognisable language that agrees with it on hypergraphs without isolated vertices. To finish the proof of Courcelle's Theorem, we need to take into account the isolated vertices, which is a minor inconvenience that is left as an exercise for the reader, see Exercises <ref> and <ref>.
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
   
  
  
      This completes the proof of Courcelle's Theorem. 
  
      
  
  
      Consider graphs (not hypergraphs). Show that the existence of an Eulerian cycle can be defined in counting , but not in .
  
  
  Show that the existence of a Hamiltonian cycle cannot be defined in counting with set quantification restricted to sets of vertices (and not hyperedges).
  
   Show  that Lemma <ref> ceases to be true if we allow homomorphisms that are not necessarily letter-to-letter.
  
  
  Show that for every formula φ(X) with one free set variable, the following problem can be solved in linear time: 
  
      
    * Input. A tree decomposition T;
      
    * Output. The maximal size of a set of vertices X, such that φ(X) is true in the underlying hypergraph.
  
  
  
  
  
  
  
  For a hypergraph G, define two hypergraphs  α(G) and β(G) as follows:
  
      
    * α(G): remove all isolated vertices;
      
    * β(G): remove all hyperedges and non-isolated vertices.
  
  The functions α and β are not arity-preserving, since the arity of G is equal to the sum of arities of α(G) and β(G).
  Show that every sentence of counting is equivalent to a finite Boolean combination of sentences of counting , each of  which talks about only   α(G) or β(G). 
  
  
   Recall the functions α and β from the previous exercise.  Show that if L is a language of hypergraphs that is definable in , then the same is true for the languages
  
      G : α(G) ∈ L     and    G : β(G) ∈ L .
  
  Together with Exercise <ref>, this observation completes the proof of Courcelle's Theorem.
  
      Consider a language defined by the logic in the 
  , we observe that it can be defined by checking an ultimately periodic property of the numbers of ports and isolated vertices. For a hypergraph G ∈Σ, define γ(G) to be the word a^n b^m where n is the number of ports and m is the number of isolated vertices.  This  word is roughly the same thing as β(G),  except that it has an order. Therefore, for every sentence φ of counting on graphs, one can easily find a sentence ψ of counting over finite words which makes the following diagram commute:
  
      Σ[r]^-γ[d]_β   
          a^* b^*
          [d]^ψ
      Σ[r]_φ   yes, no
  
  For finite words, counting has the same expressive power as , because modulo counting can be expressed using the order of the word. Since on finite words can only define regular languages, it follows that ψ defines a regular language contained in a^* b^*. Every such regular language is defined as the intersection of a^*b^* with   a finite Boolean combination of constraints of the form 
  
      #_σ = nletter σ∈a,b appears
       exactly n times         #_σ≡ k  nthe number of 
       appearances of  σ∈a,b
       is congruent to k modulo p
  
  It follows that L_β is a finite Boolean combination of languages of the form “exactly n ports”, “exactly n isolated vertices”, “the number of ports is congruent to k modulo n”, “the number of isolated vertices is congruent to k modulo n”. All of these languages are easily seen to be recognisable, using a construction similar to Example <ref>.
  
  
  
  
  
  
   §.§ Satisfiability for bounded treewidth
  
      We finish this section with an algorithm for deciding satisfiability of counting , assuming bounded treewidth. Recall that already first-order logic on graphs has undecidable satisfiability, and this  undecidability carries over to the more general setting of hypergraphs and counting .
      We recover decidability if we restrict attention to hypergraphs of bounded treewidth.
  
  
  
      The following problem is decidable:
      
          
    * Input. A sentence of counting and k ∈1,2,….
          
    * Question. Is the sentence true in  some hypergraph of treewidth at most k?
      
  
   We use the proof of Courcelle's Theorem, with an emphasis on computability.
      We say that a ranked set is computable if its elements can be represented in a finite way, and there is an algorithm which inputs an arity k and either outputs the finite list of all elements of arity k (if there are finitely many), or starts enumerating these elements (if there are infinitely many). The algorithm also says if there are finitely many elements of arity k or not. 
      We say that a hypergraph algebra A is computable if its underlying ranked set is computable, and its multiplication operation is also computable (the inputs to the multiplication are finite hypergraphs, which can be represented in a finite way).  
      
      Free hypergraph algebras over computable alphabets are computable,  all of the hypergraph algebras that we used as recognisers for the atomic relations in the  proof of Courcelle's Theorem are computable, and computability is preserved under the products and the powerset construction. Therefore, we get the following computable strengthening of Courcelle's Theorem: given a sentence of counting , which defines a property of hypergraphs over a finite alphabet Σ, we can compute a recognising homomorphism 
      
      h : Σ→ A
  
      into a computable hypergraph algebra. The hypergraph algebra, homomorphism, and accepting set are  represented by the corresponding algorithms.
  
      Let A_k ⊆ A be the image under h of all hypergraphs with treewidth at most k.  By Theorem <ref>, A_k is equal to the smallest subset of A that contains the letters and which is closed under applying the treewidth k operations in the hypergraph algebra A.  Since A_k is contained in the finite part of A which has arity at most k+1, and there are finitely many treewidth k operations, it follows that A_k can be computed. Finally, we check if A_k contains at least one element of the accepting set. 
  
  
  Show that the following problem is decidable: given a first-order formula, decide if it is true in some rectangular grid. Here we are talking about unlabelled rectangular grids as in Example <ref>, and not labelled rectangular grids as in Example <ref>.
  
   Show that if a hypergraph language L has bounded treewidth and is definable in counting , then its syntactic algebra is computable.
  
  Show that the following problem is decidable: given k ∈1,2,… and an sentence φ, decide if φ is true in infinitely many hypergraphs of treewidth at most k.
   
  
  
  
  § DEFINABLE TREE DECOMPOSITIONS
  
  
  
  In this section[The results of this section are based in bojanczykDefinabilityEqualsRecognizability2016a], we show that for hypergraphs of bounded treewidth, tree decompositions can be defined in . One application of this result is going to be a converse of Courcelle's Theorem for bounded treewidth: every recognisable property is definable in counting for hypergraphs of bounded treewidth. 
  
  
  
   We begin by explaining how a tree decomposition can be defined in  . 
  This is split into two ingredients: in Definition <ref> we represent a  tree decomposition  using a  binary relation on vertices, and then in Definition <ref> we show how such a binary relation can be defined in . 
  
  
  
    
  *The introduction ordering. We represent a tree decomposition using  the order in which vertices of the underlying hypergraph are introduced.
  
    Define the introduction ordering of a tree decomposition to be the following binary relation on vertices in the underlying hypergraph:
          
      v is  introduced in the same node as, or an ancestor of, the  node    introducing  w.
                   we say that  v is introduced before vertex w of the tree decomposition
  
  
  
  The introduction ordering is a pre-order, i.e. it is transitive and reflexive. It need not be anti-symmetric, because several vertices might be introduced in the same node. 
  
  
      Consider a hypergraph which has no ports or hyperedges, but only isolated vertices, like in the following picture:
      120
      One tree decomposition for this hypergraph has a node for each vertex, with the bag containing only that vertex, and with the nodes ordered left-to-right. Its introduction ordering is  in the following picture:
      121
      An alternative tree decomposition, has the same nodes and bags. However, this time we have some chosen root, and the remaining nodes are its children. Here is the introduction ordering for the alternative tree decomposition:
      123
      
  In both pictures above, the introduction ordering is anti-symmetric, because each node of the tree decomposition introduces a single vertex. Here is a picture of an  introduction ordering  which is not anti-symmetric (and has two components):
      122
  
  
  We now explain how a tree decomposition can be recovered from its introduction ordering. To do this, we  use two mild assumptions on tree decompositions (in the following, we say that a hyperedge is introduced in node x if x is the least node that contains the incidence list of the hyperedge):
  
      
    (A)every node introduces at least one vertex;
      
    (B) if a vertex v is in the bag of node x, then it is incident to some  hyperedge that is introduced in x or its descendants. 
  
  Every tree decomposition can be transformed into a tree decomposition that satisfies (A) and (B), without increasing the width. In order to satisfy (A), we merge every node that does not introduce any vertices with its parent. In order to satisfy (B), we remove a vertex v from all bags that violate condition (B).
  
  If a tree decomposition satisfies (A) and (B), then it can be recovered from its introduction ordering as follows. Thanks to condition (A),  the nodes of the  tree decomposition  are equivalence  classes of vertices with respect to the equivalence “v is introduced before w and vice versa”,  and the tree order on nodes is the inherited from the introduction ordering. Thanks to condition (B), a vertex v is present in the bag of a node x if and only if there is hyperedge that is incident to v and a vertex that is introduced in x or its descendants. The way that we recover a tree decomposition from its introduction ordering can be formalised  in .
  
  For the rest of Section <ref>, we only consider tree decompositions that satisfy (A) and (B). 
  
  
  
  
    
  *Relations definable using set parameters.
  To represent the introduction ordering of a tree decomposition, we will use a formula of that is equipped with extra set parameters, as described in the following definition. In the definition, when evaluating an formula in a hypergraph, we use the incidence model from Definition <ref>, where the universe is both vertices and hyperedges. 
  
  
  
     An formula with set parameters is  an formula of the form 
          
      φ(
                  
              Y_1,…,Y_nset variables 
       called
      set parameters
              ,      
                  x_1,…,x_melement variables 
       called
      arguments
                  ).
  
          We say that an m-ary relation R in a hypergraph  is definable  by φ if 
          
      ∃ Y_1 ⋯∃ Y_nthere is a choice 
       of set parameters ∀ x_1  ⋯∀ x_m     R(x_1,…,x_m) φ(Y_1,…,Y_n,x_1,…,x_m).such that after fixing these set parameters in φ, 
       we get exactly the relation R
  
          We say that a tree decomposition is definable by φ if its introduction ordering is definable by φ in the underlying graph. We say that a set of hypergraphs has definable tree decompositions of bounded width if there is an formula φ with set parameters, and a width k ∈0,1,…, such that every hypergraph from the set has a tree decomposition that is definable by φ and has width at most k. 
  
  
  Since the above definition uses the  incidence model for hypergraph,  the set parameters can use hyperedges, even though the introduction ordering itself uses only vertices.  Note also that the definition uses  , and not counting . We will show that bounded treewidth implies definable tree decompositions of bounded width; not using counting will make the result stronger.
  
  
    Define an independent set to be a hypergraph that has  only vertices and no ports or hyperedges, as discussed in Example <ref>. We will show that independent sets have definable tree decompositions of bounded width. There is a minor difficulty, which is that we need to avoid the path decompositions where the introduction ordering looks like this: 121 
      The reason is that there is no single formula with set parameters that can define a linear order on every   independent set, see Exercise <ref>. The solution is to consider tree decompositions of depth two, where the introduction ordering looks like this:
      123
  The introduction ordering for such a tree decomposition is definable by an formula, which has one set parameter that describes the root.
  
   In this example, we show that cycles have definable tree decompositions of bounded width. By a cycle, we mean a hypergraph that looks like this:
  117
  As in Example <ref>, we need careful with the choice of decomposition. Consider first  a tree decomposition that looks like this:
  86
  The introduction ordering for the above tree  decomposition looks like this:
  119
  Note how the successor relation of this introduction ordering connects vertices which are far away in the cycle. For this reason, in order to define this introduction ordering, we would need an with set parameters whose size would depend on the length of the cycle. 
  
  To get definable tree decompositions for cycles, we use tree decompositions where the introduction ordering looks like this:
  118
  The idea behind such a tree decomposition is that that nodes of the tree decomposition correspond to a clockwise traversal of the cycle, with all bags containing the first vertex  (in the above picture, the first vertex is the bottom-left corner). The introduction ordering for this tree decomposition can be defined by an formula with two set parameters, one to indicate the first vertex, and another one to indicate the direction (clockwise or not) of the traversal. 
  
  
  
  
  
  
  
  
  
  
  
      Suppose that φ_1,φ_2 are two formulas with set parameters, with the same number of arguments. There is a single formula φ with set parameters which defines every relation definable by either φ_1 or φ_2.
   Let the formulas from the assumption be
      
      φ_i(Y_1,…,Y_n_i, x_1,…,x_m)_i ∈1,2.
  
       The formula from the conclusion adds an extra set parameter Y_0, whose emptiness determines which of the two formulas φ_1 or φ_2 should be used:
      
      (Y_0= ∅⇒φ_1)if Y_0 is empty, then use φ_1    
              (Y_0 ≠∅⇒φ_2).otherwise use φ_2
  
      Since there is no need to use separate set parameters for φ_1 and φ_2, 
      the number of parameters is one plus the maximal number of parameters in φ_1 and φ_2.
  
  
  Define to be the variant of where set quantification is restricted to sets of vertices (and not hyperedges). Show that for hypergraphs of bounded treewidth, has the same expressive power as . 
  
  
  Let k ∈0,1,…. Show that there is an formula φ with set parameters, such that for every hypergraph of treewidth at most k,  every unary relation (i.e. a set of vertices and hyperedges) is definable by φ using set parameters that contain only vertices.  
  
  
  
  
  
     Show that there is no formula φ with set parameters, such that  every hypergraph has  a linear order  definable by φ. Hint: consider independent sets.  
  
  
  
  Show that a formula as in Exercise <ref> can be found, if we want the linear order only for connected hypergraphs with  degree at most k (every vertex is adjacent to at most k hyperedges).
  
  Show that a formula as in Exercise <ref> can be found, if we want a  spanning forest instead of a linear order.
  
  
   
  We say that a set of hypergraphs L has bounded treedepth if there is some ℓ such that every hypergraph in L has a tree  decomposition of width and height at most ℓ (the height is the maximal depth of nodes). Without using Theorem <ref>, show  that if  L is recognisable and has bounded treedepth, then it is definable in counting .
  
  
  
  
  
   §.§ Bounded treewidth implies definable tree decompositions
  
  We are now ready to state the main result of Section <ref>.
  
  
      If L ⊆Σ   has bounded treewidth, then it has definable tree decompositions of bounded width. 
      
      
      
      
      
      
  
  
  The width of the tree decompositions in the assumption and in the conclusion of the above theorem will be different. An analysis of the proof would show that if all hypergraphs in L  have treewidth at most k, then the definable  tree decompositions from the conclusion of the theorem will have width at most  doubly exponential in k. With more care in the proof, we could produce optimal width tree decompositions[
      Definability of  tree decompositions of optimal width is shown in
      [Theorem 2.]bojanczykOptimizingTreeDecompositions2017a
  ], but the sub-optimal width will be enough for our intended application,  which is the converse of Courcelle's Theorem that will be presented in Section <ref>.
  
  
  
  
  
  
  When defining  tree decompositions in , we will not care about the labelling relation “hyperedge e has label a ∈Σ”. For this reason, we will not specify the alphabet Σ for the rest of this section. 
  
  
  
  
  Here is a plan for the rest of  Section <ref>:
  
  
      
    * In Section <ref>, page sec:nesting-lemma, we state and  prove the Merging Lemma, which shows how a  definable tree decomposition of definable  tree decompositions can be merged into a single definable tree decomposition.
      
    * In Section <ref>, page sec:bounded-pathwidth, we prove a  special case of Theorem <ref>, which says that   hypergraphs of bounded pathwidth have  definable tree decompositions.
      
    * In Section <ref>, page sec:general-case-treewidth,  we complete the proof of the theorem.
      
    * In Section <ref>, page sec:application-to-recognisability, we apply the theorem to get a converse of Courcelle's Theorem for hypergraph of bounded treewidth. 
  
  
  
  
  Before proceeding with the proof, we define torsos.  Torsos will  be used frequently in the proof. 
  
  
    
  Torsos. Torsos are used restrict a tree decomposition to the hypergraph corresponding to some subset of the nodes.  The subsets  that we care about  are called factors, and are  defined in the following picture (where dots indicate nodes of a tree decomposition):
  116
  Suppose that T is a tree decomposition and X is a factor. We  define below a hypergraph, called the torso of X in T. The torso will contain vertices and hyperedges of the underlying  hypergraph  of T that appear in the factor, plus extra hyperedges corresponding to the border of the factor. Before defining torsos formally, we need to overcome one more difficulty. In the torso, we will need a linear ordering for its ports, and for  incidence lists in the  extra hyperedges (because ports are ordered, and incidence lists are also ordered). To get such linear orders, we will use a local colouring of the underlying hypergraph, as defined below.
  
  
  
      Define a local colouring of a tree decomposition of width k to be a colouring of vertices in the underlying hypergraph with colours 0,…,k such that in every bag, all vertices have different colours. 
  
  
  Every tree decomposition has a local colouring, which can be obtained in a greedy way by colouring the root bag, then colouring the bags of the children, and so on.  If a tree decomposition is equipped with a local colouring, then every bag has an implicit linear order, from the smallest colour to the biggest colour.   We also assume that the local colouring is chosen so that the implicit linear order is consistent with the ordering of the ports, i.e. the colours of the ports are increasing.
  
  For a node x in a tree decomposition, define its adhesion to be the vertices from the bag of x that are  either ports of the underlying hypergraph, or which appear also in parent of x.
  
  
  
      
  
  
  
       
      Let T be a tree decomposition, together with a local colouring, and let X be a factor.
      The torso of X in T, denoted by T/X, is defined to be the following hypergraph. The vertices and hyperedges are those which  are introduced in nodes from X, plus the adhesion of the root node of the factor. 
      The ports are the adhesion factor's root, ordered according to the local colouring.  Furthermore, for  every node x in the border of X, we add  a hyperedge (called a border hyperedge) whose incidence list is the adhesion of x, ordered  according to the local colouring.
  
  
  If a factor is a subtree, i.e. its border is empty, then the torso will have no border hyperedges.
  For the rest of Section <ref>, we assume that every tree decomposition comes with an implicit local colouring. This way, we can simply talking about torsos in a tree decomposition, without indicating explicitly the local colouring which is needed to define the torsos.
  
  
  
  
   §.§ The Merging Lemma
  
  
  In this section we state and prove the Merging Lemma, which  is based on the following simple idea. Suppose that we have an “external”  tree decomposition, possibly of unbounded width,   where every bag has an accompanying “internal” tree decomposition,  of  width at most k.   We will show that these tree decompositions can be merged, in an definable way, into a single tree decomposition of width at most k. 
   This lemma will be used several times in the proof, with the internal tree decompositions typically  obtained by applying some kind of induction assumption. 
   
   The internal tree decompositions are formalised using  torsos for factors with one node. Torsos for  larger factors will be used later, in Section <ref>.
  
  
  
   Let k ∈1,2,….
      Let T be a tree decomposition (call it external) such that for every  every node x, the torso T/ x has a tree decomposition T_x (call it internal) of width at most k.  Then:
      
          
    *  The underlying hypergraph of T has a tree decomposition of width at most k.
          
    *  Suppose that  ψ, φ are formulas with set parameters, such that  the external tree decomposition are definable by ψ and all internal tree decompositions are definable  by φ. Then the tree decomposition from <ref>  is definable by an formula with set parameters, which depends only on ψ,φ and k, and which does  not depend on the external and internal tree decompositions.
      
  
     
      Here is a picture of the external and internal tree decompositions.  
  113
  In the picture above, red circles are used for  border hyperedges of the torsos, and blue circles are used  for the remaining hyperedges. Note that the red border hyperedges are only present in the torsos, and not  in the underlying hypergraph of the external tree decomposition.
  
  To prove item <ref>, we define a merged tree decomposition of width k as follows.  First, take the disjoint union of the internal tree decompositions, which gives a forest. Next,  convert this forest into a tree, by selecting parents for roots in the following way. Suppose that  x is a root  node in this forest, which corresponds to the root node of some internal tree decomposition T_y. If y has a defined parent z in the external tree decomposition, then the parent of x in the merged tree decomposition is defined to be the node in the internal tree decomposition T_z that introduces the border hyperedge of the torso T/ z which corresponds to node y. Otherwise, if y is the root of  the external tree decomposition, then x is the root of the merged tree decomposition. Here is a picture of the merged tree decomposition:
  114
  
  
  We leave it as an exercise for the reader to check that the merged tree decomposition defined this way is indeed a tree decomposition.     Because   bags of the merged tree decomposition are inherited from  bags of the internal tree decompositions, its width is at most k, thus proving item <ref>. 
      
      We now prove item <ref> about definability.  Let G be the underlying hypergraph of the external tree decomposition. An inspection of the definition of the merged tree decomposition shows that its introduction ordering can be defined in  G using formulas that refer to the following relations:  (a) the introduction ordering of the external tree decomposition; and (b) the following  ternary relations on vertices of G that uniformly describes all of the introduction orderings for the  internal tree decompositions:
      
      I(u,v,w)      u is introduced in a node xin the external tree decompositionsuch thatv is introduced before  win the internal tree decomposition T_x.
  
      The relations from (a) are definable  by the assumption of <ref>. Therefore to prove definability of the  merged tree decomposition, it remains to show definability of the  ternary relation I from item (b). This is done in the remainder of this proof. 
      
      Let   φ be the formula with set parameters, which defines  the internal tree decompositions. Suppose that φ  has set parameters Y_1,…,Y_ℓ.  For   every node x of the external tree decomposition, there is a choice of set parameters
      
      Y_1,x,…,Y_ℓ,x⊆vertices and hyperedges in the torso T/ x,
  
      such that the introduction ordering in the internal tree decomposition T_x is obtained by  fixing this choice of set parameters and  evaluating the formula φ in the torso T/ x. The main step in the proof will be showing how each family  
      Y_i,x_x
      can be represented in the hypergraph G using a constant size formula of with set parameters. 
      
      
      
      
      
      An issue  is that the set parameters might use border hyperedges, which are not present in the hypergraph G.  To solve this issue, we represent border hyperedges using vertices in the following way. We say that a vertex v of G represents  a border hyperedge e in a torso T/ x if:   v is introduced by the external tree decomposition in a node y such that y is the  child node of x that corresponds to the border hyperedge e in the torso T/ x. Every vertex represents at most one border hyperedge, and every border hyperedge is represented by some vertex.
      
      
      
      Let i ∈1,…,ℓ.
      Using the above representation, we will show in the following claim  that the set parameters used to define the internal tree decompositions can be represented in a uniform way in the hypergraph G. 
      
          Let i ∈1,…,ℓ. Each of the following  relations is definable by an formula with set parameters, which depends only on k:
              
      A_i(u,y)u is a vertex and 
      y is a vertex or
       hyperedge of G          ∃ x node of the 
       external tree 
       decomposition  u is introduced in x and y ∈ Y_i,x
      
                   B_i(u,v)u,v are  
       vertices 
       of G          ∃ x node of the 
       external tree 
       decomposition  u is introduced in x and the border 
       hyperedge represented by y is in Y_i,x
  
      
      Before proving the claim, we show how it implies definability of I, and therefore finishes the proof of the Merging Lemma. 
      In order to check   I(u,v,w),  we  run the formula φ in the torso corresponding to the node that introduces vertex u, with calls to the i-th set parameter replaced by calls to its representation in terms of A_i and B_i. Therefore, definability of  A_i and B_i implies definability of I. The remainder of the proof of the Merging Lemma is devoted to proving the claim.
  
      We begin with B_i. The key observation is that, when restricted to border hyperedges, the sets in the family Y_i,x_x are disjoint, because  each border hyperedge belongs to exactly one torso T/ x. For this reason,  B_i can  be viewed as a subset of the  border hyperedges, and is thus definable. More formally,  B_i can be defined in using a single extra set parameter, because  B_i(u,v) holds if and only if 
      
      u is introduced in the node whose torso 
        contains  the border hyperedge  represented by vdefinable in  using the  external tree decomposition and 
          v ∈v : B_i(u,v)  for some ua single extra  set parameter.
  
  
      We now turn to  A_i.  Here the difficulty is that a vertex might  belong  to several  sets from the family  Y_i,x_x. This will happen for  vertices from the adhesions in the external tree decomposition.  To solve this difficulty, we will use a colouring of the adhesions that uses at most k+1 colours. 
  
      Partition the  binary relation A_i  into two parts as follows:
      
      C_i
                  pairs (u,v) ∈ A_i such that
      v is in the adhesion of 
       the node represented by u        ∪        D_i
              pairs (u,y) ∈ A_i such that
      y is a vertex or hyperedge 
       that is  introduced in 
       the node represented by u.
  
      The part D_i does not raise any problems, since every vertex or hyperedge is introduced in exactly one node of the external tree decomposition, and therefore we can use the same argument as for B_i to prove that D_i can be defined in using a single extra set parameter. 
      
      We 
      are left with  C_i.     
       Choose a local colouring for the merged  tree decomposition, which maps vertices of G to colours 0,…,k so that no colour is used twice in a  bag of the merged tree decomposition. We will show:
      
          
    (*) for every u, all vertices that v that satisfy C_i(u,v) have different colours.
      
      To see why (*) is true, suppose that in the external tree decomposition, vertex   v is in the adhesion of the node x that introduces u. It follows that  v is a port  of the torso T/x, and therefore it must be  in the root bag of the internal tree decomposition T_x. This bag is also a bag of  the merged tree decomposition, and hence all vertices in this bag must have different colours under the local colouring, thus proving (*).  
  
      We now use (*) to finish the proof of the claim.
      For a colour c ∈0,…,k and a vertex u, define f_c(u) to be the vertex of colour c in the  adhesion of the node that introduces u. There is at most one such vertex by (*), and hence f_c can be viewed as a partial function.  The relation C_i can be defined in using k+1 extra set parameters, because C_i(u,v) holds if and only 
      
      ⋁_c ∈0,…,k    v = f_c(u)can be defined in 
        using the external
       tree decomposition and
       its local colouring  and    u ∈u : C_i(u,f_c(u)) an extra set parameter
       for each colour c.
  
  
  
  
  
  
  
  
  
  
  
   A cut hyperedge in a hypergraph is a hyperedge e such that for some two vertices, every path connecting them must pass through e.  Let L,K be sets of hypergraphs, such that for every G ∈ K, if all cut hyperedges are removed from G, then the resulting hypergraph is in L. Show that if L has definable tree decompositions, then the same is true for K. 
  
  
  
  
  
  
  
  
  
   §.§ Application to recognisability
  
  
  A corollary of  Theorem <ref>  is that the converse of Courcelle's Theorem holds for hypergraphs of bounded treewidth, as stated below. 
  
  
  
      Let   L ⊆Σ  be a set of hypergraphs of  bounded treewidth. Then L is recognisable if and only if it is definable in counting . 
  
  
  
  
  
     The right-to-left implication, even without the assumption on  bounded treewidth, is Courcelle's Theorem. 
      
      The left-to-right implication is proved using the definable tree decompositions from Theorem <ref>.  The  formula defining the language L guesses the  tree decomposition,  by existentially  quantifying over the set parameters needed to define its introduction order adjacent bag relation, and then does a bottom-up pass through the decomposition to compute the value of the hypergraph with respect to the recognising homomorphism. Suppose that L ⊆Σ has bounded treewidth and is recognised by a homomorphism
      
      h : Σ→ A
  
      into a hypergraph algebra that is finite on every arity. We need to show that L is definable in counting . 
      By Theorem <ref> there is some k ∈0,1,… and  an formula φ 
      with set parameters, such that every hypergraph in L has a tree decomposition  T of width at most k that can  be defined by φ. 
  
      The formula of counting that defines the language L works as follows.
      Let G ∈ L and let T be a tree tree decomposition which can be defined by some choice of set parameters in the formula φ. For a node x of this tree decomposition, define  G_x to be the torso of the subtree of x, and define the type of x to  be  the image of G_x under the recognising homomorphism h. 
      The formula defining L  uses existential set quantification to guess the following sets:
      
          
    * set parameters for the formula φ which define the introduction order of T;
          
    * set parameters for the formula φ which define the bag relation of T;
          
    * k+1 sets which represent a local colouring;
          
    * sets which represent the types of the nodes in the  tree decomposition.
      
      The types from item (d) are  represented by storing the  type  of node  x in every vertex v of the underlying hypergraph that is introduced in node x. 
      Note that the arity of a type is at most  k+1, since each torso has at most k+1 ports, and therefore there are finitely many possibilities for the types, which ensures  that they can be  represented a bounded number of  sets (number of elements in the algebra that have arity at most k+1).  Next, the formula checks that the sets guessed in (a, b, c) indeed describe a tree decomposition of width at most k together with a local colouring; this can be easily done by expressing the appropriate definitions in . The rest of this proof is devoted to explaining how a formula of counting can check that the types  from item (d) were guessed correctly. Once we know this, membership in the language boils down to checking that the type of the  root node in the tree decomposition  belongs to the accepting set for the homomorphism.
  
      To check that the  types  from item (d)  are guessed correctly, we examine  every node x of the tree decomposition, and check if its guessed type is consistent with the guessed types of its children.  This is done as follows. 
      For a node x in the tree decomposition, decompose the torso  G_x into the following parts:
      
          
    * For a hyperedge e that is introduced in  x, define E_e to be the hypergraph obtained from G_x by restricting it so that (a)  the  vertices  are the bag of x; (b) the only hyperedge is e; (c) all vertices are ports.
          
    * For a child y node of x, define H_y to be the hypergraph obtained from G_x by  restricting it so that: (a) the vertices are the bag of x plus vertices introduced in y and its  descendants of y; (b) the hyperedges are those that are introduced in y and its descendants; (c) the ports are the bag of x.
      
      
      
      Recall the fusion, forget and rearrangement operations from Figure <ref>.
      Apart from the ports, the parts E_e and H_y defined above  are disjoint, and together they represent all vertices and hyperedges in the torso G_x. 
      Therefore,  G_x can be recovered from these hypergraphs by fusing  these  and then forgetting the vertices from the bag of x which are not in the adhesion of the subtree of x, as expressed in the following equality:
      
      G_x       =     f (
              ∑_y H_yfusion ranging
       over children  of x  +fusion  ∑_e E_efusion ranging
       over hyperedges 
       introduced in x)the operation f restricts the ports
       to the adhesion of the subtree of x.
  
      Although fusion is in principle a binary operation, it is associative and commutative when the number of ports is fixed, and therefore unordered sums in the above expression are meaningful. Both fusion and f are special cases of term operations in hypergraph algebra, which means that they commute with the homomorphism h.
      
       
      The  hypergraph H_y is obtained from G_y by forgetting some ports (namely the ports that are not in the bag of x), and then adding some new vertices as ports (namely the vertices that are in the bag of x but not in the bag of y). This corresponds to a term operation, call it f_y, which  transforms H_y into G_y is a term operation. Summing up, we have the following equation for every node x in the tree decomposition: 
          
      G_x = f(∑_y f_y(G_y)_H_y + ∑_e E_e).
  
          The above equation is in the free algebra Σ.
          Since h is a homomorphism, and homomorphisms commute with term operations such as fusion and f_y, we have the following equation in the algebra A:
          
      h(G_x)type of x
               = f(∑_y f_y(h(G_y)type of y) + ∑_e h(H_e)),
  
          where both fusion and the term operations f and f_y are now interpreted in the algebra A.  If the guessed types satisfy the equality in (<ref>) for every node x, then they are equal to the actual types.  
          Therefore, it remains to show that a formula of counting can check if the guessed types satisfy the equality (<ref>) for every node x.
  
          Define A^x and A_x to be the elements of the algebra A whose arity is equal to sizes of, respectively, the adhesion and the bag of x. The term operations in (<ref>) have the following types:
          
      A_x → A^xf    A_x × A_x → A_x+    A^y → A_xf_y.
  
          Since fusion + is  associative and commutative, it follows that the fusion from (<ref>)  corresponds to multiplication in a  finite commutative semigroup, and such multiplication can be computed in counting . The result follows, since the term operation f_y can be determined in based on the node y (given by a vertex introduced in it), and the same is true for h(H_e). 
  
  
  
  
  
      We say that a hypergraph algebra A is aperiodic if for every n ∈0,1,… the semigroup
      
      (elements of A with arity n, fusion)
  
      is aperiodic. Show that if L is a set of hypergraphs of bounded treewidth, then L is definable in without counting if and only if it is recognised by a finite aperiodic hypergraph algebra.
  
  
  Suppose that L has bounded treedepth, as described in Exercise <ref>. Show that if L is recognised by an aperiodic hypergraph algebra, then it is definable  in first-order logic.
  
  
  
  
  
   
   
  
  
  
  
  
  
   toc 
  
   
  
   
  
   
  
  
  
  
  
  
   
  
  
  
  
  
  
  
  
  
  
   authorsAuthor index
   subjectSubject index
   