2025-06-20

Notes on MSO Transducers

Notes
This article was translated by GPT-5.2-Codex. The original is here.
Credibility

This article references information generated by AI. It is a memo for research and learning. Please verify accuracy and factuality yourself. In particular, I cannot judge whether terms actually exist or are used in Japanese, so I use the LLM-generated terms as-is.

Introduction

I touched MSO Transducers briefly as an undergraduate, but never gained intuition for them. Now that LLMs lower the learning barrier, I thought I might understand them better, so I'm revisiting the topic and taking notes within what I can understand.

First-Order Logic (FOL)

In first-order logic, we treat elements of a set as variables and can quantify over them. Here, variables and quantifiers mean:

  • First-order variables: written as lowercase letters like x,y,z,x, y, z, \dots, representing individual elements of a set
  • Quantifiers: universal/existential quantifiers using \forall and \exists meaning "for all" and "there exists"

For example, the following predicate says: "for every xx, there exists a yy such that $y = x + 1".

xy(y=x+1)\forall x \exists y (y = x + 1)

So far, this is straightforward.

Monadic Second-Order Logic (MSO)

In second-order logic (SOL), we can quantify over relations or functions of elements. In MSO, we restrict quantification to unary relations (monadic relations), i.e., sets. We can also use structure-specific predicates called atomic formulas.

For example, a graph being 2-colorable can be expressed by:

RB(v(vRvB))uv((u,v)E¬((uRvR)(uBvB)))\exists R \exists B (\forall v (v \in R \lor v \in B)) \land \forall u \forall v ((u, v) \in E \to \lnot ((u \in R \land v \in R) \lor (u \in B \land v \in B)))

Here RR (red) and BB (blue) are sets of vertices, and EE is the edge set.

It suddenly looks complex, so let's break it down:

  • RB\exists R \exists B:
    • There exist vertex sets RR and BB
  • (vRvB)\forall (v \in R \lor v \in B):
    • For every vertex vv, it is in RR or BB
  • uv((u,v)E)\forall u \forall v ((u, v) \in E \to \dots):
    • For all vertices u,vu, v, if (u,v)(u, v) is an edge, then ...
  • ¬((uRvR)(uBvB))\lnot ((u \in R \land v \in R) \lor (u \in B \land v \in B)):
    • uRvRu \in R \land v \in R: both uu and vv are in RR
    • uBvBu \in B \land v \in B: both uu and vv are in BB

With this breakdown it's understandable. Indeed, graphs satisfying this predicate are 2-colorable.

So the set of all 2-colorable graphs is definable in MSO.

I remember learning how to describe graphs with predicates in graph theory lectures, but I had forgotten. Even when researching MSO, I didn't connect it with graph theory. In class, it was presented as "graphs can be expressed with predicates," but I didn't think about the logical class involved.

Büchi–Elgot–Trakhtenbrot theorem

The Büchi–Elgot–Trakhtenbrot theorem shows that MSO over strings and regular languages have equivalent expressive power. It was proved in a paper from 1960, so it has been known for a long time.

It was an important result bridging logic and automata theory. I haven't studied automata theory comprehensively, so I didn't know it. Maybe I should read Elements of Automata Theory after all.

As an undergraduate, I read [1706.01668] One-way definability of two-way word transducers where MSO Transducers appear, but I didn't even know that regular languages are MSO-definable. So if I reread it now, I might understand more.

Courcelle's Theorem

Courcelle's Theorem states that for graphs of bounded treewidth, any property definable in MSO can be decided in linear time.

This seems related to tree automata. According to Gemini Deep Research, as with strings, the class of trees definable by regular tree languages and MSO coincide. I want to verify that later.

It's amazing that you can wait 20 minutes and have the web automatically researched for you. I wish I had that as a student.

I will leave the HTML version of the DeepResarch results here (accuracy unknown).

The rapid explosion in complexity from k=4k = 4 or k=5k = 5 reminds me of my undergraduate thesis results.

MSO Transducers

Example: String transformation

Consider transforming a string over alphabet a,b,c{a, b, c} by replacing aa with bb, bb with cc, and cc with aa. This can be expressed by:

  • Lb(p)    La(p)L_b'(p') \iff L_a(p)
  • Lc(p)    Lb(p)L_c'(p') \iff L_b(p)
  • La(p)    Lc(p)L_a'(p') \iff L_c(p)

Here LaL_a, LbL_b, LcL_c are predicates true at position pp in the input string when the character is aa, bb, cc. LaL_a', LbL_b', LcL_c' are similar predicates for position pp' in the output string. pp and pp' indicate the same position in input and output.

This is a fairly casual definition, but it is enough to capture what MSO can do. I'll try a more rigorous definition later.

Example: Complement graph generation

Consider transforming a graph G=(V,E)G = (V, E) into its complement G=(V,E)\overline{G} = (V, \overline{E}). The complement graph has the same vertex set and edges between vertices that are not connected in the original graph.

For MSO transformation, input and output are:

  • Input: graph G=(V,E)G = (V, E)
  • Output: complement graph G=(V,E)\overline{G} = (V, \overline{E})

Transformation rules

  1. Define output vertex set:
    • (vVout    vVin)\forall (v \in V_{\mathrm out} \iff v \in V_{\mathit in})
    • The output vertex set is identical to the input vertex set.
  2. Define output edge set:
    • uv((u,v)Eout    uv¬((u,v)Ein))\forall u \forall v ((u, v) \in E_{\mathit out} \iff u \neq v \land \lnot ((u, v) \in E_{\mathit in}))
    • "There is an edge between u,vu, v in the output graph iff uvu \neq v and there is no edge between them in the input graph."

These definitions are in MSO, so complement graph generation is an MSO transformation.

A more formal definition

The earlier examples were too informal, so let's define MSO transformations more formally.

Formal definition of string structures

A Σ\Sigma-string structure is defined as a tuple S=D,<,(Pc)cΣS = \langle D, <, (P_c)_{c \in \Sigma} \rangle where:

  • DD: Domain, a finite set of positions.
  • <<: a total order on DD.
  • (Pc)cΣ(P_c)_{c \in \Sigma}: a set of labels. For each cΣc \in \Sigma, PcP_c is a subset of DD representing positions where character cc occurs.
    • Pa(p)P_a(p) means "the character at position pp is $a".

Formal definition of MSO transformations

An MSO transformation TT is defined as a relation from input structure SinS_{\mathit in} to output structure SoutS_{\mathit out}. The relation is specified by a single MSO formula φT\varphi_T.

The formula φT\varphi_T is written over the input structure (Din,<in,Pc)(D_{\mathit in}, <_{\mathit in}, P_c) and output structure (Dout,<out,Pc)(D_{\mathit out}, <_{\mathit out}, P_c').

A pair (Sin,Sout)(S_{\mathit in}, S_{\mathit out}) belongs to TT iff it satisfies φT\varphi_T:

(Sin,Sout)T    (Sin,Sout)φT(S_{\mathit in}, S_{\mathit out}) \in T \iff (S_{\mathit in}, S_{\mathit out}) \vDash \varphi_T

Example

Let's revisit the transformation that maps aba \to b, bcb \to c, cac \to a, now with a formal definition.

First define input and output structures:

  • Input: Sin=Din,<in,Pa,Pb,PcS_{\mathit in} = \langle D_{\mathit in}, <_{\mathit in}, P_a, P_b, P_c \rangle
  • Output: Sout=Dout,<out,Pa,Pb,PcS_{\mathit out} = \langle D_{\mathit out}, <_{\mathit out}, P_a', P_b', P_c' \rangle

Next define a formula expressing that the input and output structures are isomorphic when labels are ignored:

φiso(Din=Dout)(xy(x<inyx<outy))\varphi_{\mathit iso} \equiv (D_{\mathit in} = D_{\mathit out}) \land (\forall x \forall y (x <_{\mathit in} y \leftrightarrow x <_{\mathit out} y))

This says the domains are equal (same string length) and the order of positions is the same.

Define the relabeling rules:

φrelabelxDin((Pa(x)Pb(x))(Pb(x)Pc(x))(Pc(x)Pa(x)))\varphi_{\mathit relabel} \equiv \forall x \in D_{\mathit in} \left( \begin{align*} (P_a(x) \leftrightarrow P_b'(x)) \land \\ (P_b(x) \leftrightarrow P_c'(x)) \land \\ (P_c(x) \leftrightarrow P_a'(x)) \end{align*} \right)

This refers to the labels at the same position.

Finally, the overall transformation formula is:

φTφisoφrelabel\varphi_T \equiv \varphi_{\mathit iso} \land \varphi_{\mathit relabel}

Thus the transformation is expressible in MSO logic.

Execution example

Let's check the behavior for input abacabac.

  1. Input structure SinS_{\mathit in}
    • Din={1,2,3,4}D_{\mathit in} = \{1, 2, 3, 4\}
    • <in={(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)}<_{\mathit in} = \{(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)\}
    • Pa={1,3}P_a = \{1, 3\}, Pb={2}P_b = \{2\}, Pc={4}P_c = \{4\}
  2. Output structure SoutS_{\mathit out}
    • Dout={1,2,3,4}D_{\mathit out} = \{1, 2, 3, 4\}
    • <out=<in<_{\mathit out} = <_{\mathit in}
    • Output labels from relabeling rules:
      • PaPbP_a \leftrightarrow P_b': Pa={1,3}P_a = \{1, 3\} so Pb={1,3}P_b' = \{1, 3\}
      • PbPcP_b \leftrightarrow P_c': Pb={2}P_b = \{2\} so Pc={2}P_c' = \{2\}
      • PcPaP_c \leftrightarrow P_a': Pc={4}P_c = \{4\} so Pa={4}P_a' = \{4\}

Interpreting SoutS_{\mathit out} as a string yields bcbabcba.

Personally, it feels a bit strange that you can define a transformation this way, but it reminds me of writing programs in Prolog.

MSO has variants

It seems MSO has multiple variants. By adding predicates to the logic, the class of definable transformations changes.

For graphs: MSO1\mathrm{MSO}_1 can use adjacency adj(u,v)\mathit{adj}(u, v), MSO2\mathrm{MSO}_2 can use incidence inc(v,e)\mathit{inc}(v, e), CMSO\mathrm{CMSO} can use cardp,q(X)\mathit{card}_{p,q}(X) to check cardinality modulo qq and pp, and there are extensions like EMSO\mathrm{EMSO} / LinEMSOL\mathrm{LinEMSOL} that add predicates for minimal/maximal size of sets satisfying an MSO property.

Conclusion

I reviewed MSO Transducers with examples. When thinking about structures or transformations, I naturally imagine constructive definitions, so defining a structure and then expressing transformations via logic over that structure still doesn't feel natural. This is probably part of proofs, but I feel it's hard to define a transformation purely in logical form unless you have practiced converting constructive definitions into logical ones. Properties like 2-colorability or complement graphs are clear enough that logical definitions come naturally, but arbitrary transformations are harder.

Next, I want to look into proofs of the theorems mentioned here and related theory.