arXiv:2608.13433cs.FLAug 2026

Manuscript Offprint

arXiv:2608.13433

Algebraic Decomposition Theory for Transformer Length Generalization

Andy Yang, Blerta Veseli, Corentin Barloy, Michaël Cadilhac, Andreas Krebs, Charles Paperman, Howard Straubing, Michael Hahn

Abstract

Transformer-based language models are known to sometimes generalize to sequences longer than seen during training, but we lack a precise characterization of which tasks admit length generalization. It is not even known which regular languages transformers length-generalize on – and this is a foundational class of languages. Our contributions are to establish the first complete characterization of which regular languages transformers length-generalize on and provide a decision algorithm running in polynomial time in the size of the language’s syntactic monoid. These results rely on an effective characterization of the regular languages in C-RASP, a recently-established formalism that expresses which languages transformers length-generalize on. This characterization is challenging because classical tools like Krohn-Rhodes decomposition theory for finite semigroups are insufficient for C-RASP. Firstly, the basic building blocks of Krohn-Rhodes theory – flip-flop and simple groups – are not expressible in C-RASP. Secondly, the basic building block of C-RASP (unbounded counting) is not expressible by the finite semigroups of Krohn-Rhodes theory. Thus, length generalization on regular languages is controlled by an algebraic property that is invisible to classical finite decomposition theory. We generalize classical decomposition theory from finite semigroups to the infinite additive group on the integers, allowing us to characterize C-RASP in terms of iterated wreath products of the integers and derive a provable polynomial-time decision algorithm for regular language membership. Experiments across a broad test suite of regular languages confirm that our theory captures transformers’ length-generalization behavior more accurately than existing classifications.

1 Introduction

What state-tracking capabilities do transformer language models possess? To answer this question we study the regular languages on which transformers can length-generalize. First, regular languages give us a formal framework to describe the structure of state-tracking algorithms that transformers can implement. Second, probing for length-generalization gives us an empirical confirmation that the transformer learns an implementation of the underlying algorithm. Even though this is a fundamental question about an important architecture, we still lack a precise answer. For instance as shown in fig. 1, two structurally similar state-tracking tasks (recognizing (ab+bbaa)∗(ab+bbaa)^{*} and (ab+aabb)∗(ab+aabb)^{*}) may diverge sharply in terms of length-generalizability – and all existing theory fails to explain this discrepancy.

abbbaa0255075100[lminl_{min},50][51,100][101,150]

ababb0255075100[lminl_{min},50][51,100][101,150]

Figure 1: DFAs for (ab+bbaa)∗(ab+bbaa)^{*} and (ab+aabb)∗(ab+aabb)^{*} and transformer length generalization on both. Transformers were trained on strings of length [lmin,50][l_{min},50], where lminl_{min} denotes the length of the shortest valid string in the respective language, and the tested on a held-out sets of strings from bins of length up to 150150. Existing theory fails to explain why length-generalization differs between these simple regular languages with very similar structure.

This line of inquiry has a deep history in machine learning. In fact, Kleene (1956) invented regular expressions specifically for the purpose of analyzing the capabilities of McCulloch-Pitts neural networks. Regular language recognition is a fundamental task in the theory of computation that provides us a systematic method of determining the sequence-processing capabilities of a model (Sipser, 1996). The task is simple: given a sequence of inputs, each of which triggers a transition in a deterministic finite automaton (DFA), track the state of the machine after each transition. These finite state-tracking tasks arise concretely in the context of language models, which we sketch below.

Constrained decoding: A user may want a language model to generate text in a structured format, such as JSON or code, which can pose challenges (Schall & de Melo, 2025). Even as outputs grow in length, their structured formats often follow regular constraints (e.g. matching curly braces to a fixed depth in JSON).

Agentic workflows: AI agents in practice follow workflows consisting of compositional sequences of actions (Schluntz & Zhang, 2024). Keeping track of the state of the environment and deciding the next action to take can then explicitly be seen as simulating a DFA.

Natural language: Morphemes in natural languages typically follow constraints that can be modeled by regular languages (Kaplan & Kay, 1994). Natural language semantics also involves maintaining the states of different referents over time (Kim & Schuster, 2023).

Previous work provides a theoretical foundation for our study by providing characterizations of which regular languages can be expressed by the transformer architecture. Liu et al. (2023b); Merrill & Sabharwal (2025) showed transformers with log⁡(n)\log(n) depth could express all regular languages, while constant depth transformers could express all solvable regular languages. The containment of poly(n)\mathsf{poly}(n)-precision transformers within TC0\mathsf{TC}^{0} suggests transformers cannot express non-solvable regular languages (assuming TC0≠NC1\mathsf{TC}^{0}\neq\mathsf{NC}^{1}, as is common) (Merrill & Sabharwal, 2023; Chiang, 2025). Hahn (2020) showed that hard attention transformers recognized only languages in AC0\mathsf{AC}^{0}, which Yang et al. (2024) later refined to the star-free regular languages, and Jerad et al. (2025) finally refined to the R\mathcal{R}-trivial languages (when using leftmost tie breaking). Similarly, Li et al. (2024) showed that finite-precision transformers recognized only star-free languages, which Li & Cotterell (2025) refined to the R\mathcal{R}-trivial languages.

In contrast, Bhattamishra et al. (2020); Huang et al. (2025) showed that transformers can learn languages both in and outside of each class discussed above, implying that existing expressivity characterizations do not account for transformer length generalization on regular languages. In particular, strong empirical evidence show that transformers tend to length-generalize on and only on the languages expressible in C-RASP{\mathsf{C\text{-}RASP}}, a programming language defining a subclass of the languages expressible by a transformer (Huang et al., 2025; Jobanputra et al., 2025; Yang et al., 2025; Yang & Chiang, 2024). However, prior to this work, there did not exist a complete characterization of the regular languages in C-RASP{\mathsf{C\text{-}RASP}}.

Formal language theory has a vibrant tradition of producing beautiful characterizations of regular language membership for different classes. For instance, Barrington et al. (1992) showed that a regular language is in AC0\mathsf{AC}^{0} if and only if its syntactic morphism is quasi-aperiodic, thus providing a decision procedure. Simon (1975) showed that a language is piece-wise testable iff its syntactic monoid is J\mathcal{J}-trivial. On the frontier, regular language membership in TC0\mathsf{TC}^{0} is equivalent to the 4040 year old open problem of whether or not TC0≠NC1\mathsf{TC}^{0}\neq\mathsf{NC}^{1} (Barrington, 1989), and regular language membership in all levels of the dot-depth hierarchy is a 5050 year old open problem (Pin, 2017). Our work follows in this tradition, tackling the same question in the case of C-RASP{\mathsf{C\text{-}RASP}}.

This is a deep and challenging theoretical question because classical tools for characterizing regular languages – namely the Krohn-Rhodes decomposition theory of Krohn & Rhodes (1965) – are insufficient for C-RASP{\mathsf{C\text{-}RASP}}. On one front, the building blocks of Krohn-Rhodes theory (flip-flop units and simple groups) are not expressible in C-RASP{\mathsf{C\text{-}RASP}} (Huang et al., 2025). On a second front, the basic building blocks of C-RASP{\mathsf{C\text{-}RASP}} (unbounded counting units) are not expressible by the finite semigroups of Krohn-Rhodes theory. Our core innovation is to develop an analogous algebraic decomposition theory for transformers using the additive group on the integers. We effectively characterize the regular languages in C-RASP{\mathsf{C\text{-}RASP}}, and provide a polynomial-time algorithm that decides DFA membership in C-RASP{\mathsf{C\text{-}RASP}}. A simpler necessary (but not sufficient) criterion is proven via a profinite equation. Finally, we validate empirically that regular language membership in C-RASP{\mathsf{C\text{-}RASP}} predicts transformer length-generalization better than any existing characterization.

{\mathsf{C\text{-}RASP}}$${\mathbf{R}}$${\textnormal{wpc}}({\mathbf{Dy}})$${\mathbf{R}}^{\omega}$${\mathbf{A}}$${\mathbf{REG}}

(a) regular and subregular

\mathsf{TC}^{0}$$\mathsf{AC}^{0}$${\mathsf{C\text{-}RASP}}

(b) circuits

\mathcal{B}_{0}$$\mathcal{B}_{1}$$\vdots$${\mathbf{A}}$${\mathsf{C\text{-}RASP}}

(c) dot depth hierarchy

Figure 2: C-RASP{\mathsf{C\text{-}RASP}} is situated orthogonal to well-known classes of languages. C-RASP{\mathsf{C\text{-}RASP}} contains all R\mathcal{R}-trivial languages but not all star-free ones (fig. 2(a)). C-RASP{\mathsf{C\text{-}RASP}} is contained in TC0\mathsf{TC}^{0}, but incomparable to AC0\mathsf{AC}^{0} (fig. 2(b)). C-RASP{\mathsf{C\text{-}RASP}} intersects every level of the dot depth hierarchy, while remaining incomparable to the full hierarchy (fig. 2(c)).

References (39)