TL;DR
We know transformers are great at natural language, but what kinds of formal computation can they actually learn? Instead of testing neural networks on a handful of hand-picked toy problems, this paper introduces SoL (Sets of Languages)—a benchmark that randomly samples languages from across four classic formal language classes—and finds that while LSTMs dominate across the board, transformers hit a hard ceiling at star-free languages and struggle with high-sensitivity problems.
What Problem Does This Paper Address?
If you want to know whether a neural network can solve a class of problems—say, tracking whether a C code snippet is inside or outside a string literal—formal language theory gives you a rigorous yardstick. Theoretical work has tried to map neural architectures to formal language classes, but those proofs rely on idealized simplifications (like hard attention or strict future masking) that don't match real-world models.
On the flip side, empirical studies test actual, unsimplified models, but they usually evaluate them on a tiny set of hand-picked languages. That leaves vast regions of each language class unexplored. The authors bridge this gap by asking: What happens when we test real neural networks on languages sampled randomly, with full support, across entire formal language classes?
Methods
The authors study a nested hierarchy of four formal language classes:
- $\mathscr{R}$-trivial languages (recognized by partially-ordered deterministic finite automata, or PODFAs)
- Star-free languages (regular expressions with complementation, but no Kleene star)
- Regular languages (recognized by general DFAs)
- Context-free languages (generated by context-free grammars in Chomsky normal form)
To sample fairly from each class without accidentally falling into a smaller, simpler class, they design four custom sampling algorithms with full support—meaning every language in the target class has a non-zero chance of being picked. They also calibrate the sampling parameters using a 3-gram baseline model so that the classes have comparable baseline difficulty, and filter out languages with overly high density (which would make generating negative examples painfully slow).
For string generation, they introduce faster dynamic programming algorithms to sample strings of a desired length from weighted DFAs and context-free grammars, alongside a GPU-parallelized CKY parser for context-free membership testing.
They train four architectures from scratch on binary classification (accept/reject) without chain-of-thought:
- Transformer (SinPE): Causally-masked encoder with sinusoidal positional encodings.
- Transformer (NoPE): Same transformer, but with no positional encodings.
- Simple RNN: Multi-layer Elman RNN with $\tanh$ activations.
- LSTM: Multi-layer LSTM with decoupled input and forget gates.
For each language and architecture, they train 10 models with random hyperparameters (and random widths/depths targeting 10k–100k parameters) and report the best test accuracy.
Datasets
The authors introduce SoL (Sets of Languages), a benchmark of 30 randomly sampled languages per class (120 languages total). For each language, they generate balanced datasets of positive and negative strings (including "hard" negatives created by editing positive strings):
- Training set: 10,000 strings of length $[0, 40]$
- Validation set: 1,000 strings of length $[0, 80]$
- Test set: 5,010 strings of length $[0, 500]$
Testing on strings up to length 500—far longer than anything seen during training—forces models to learn the underlying algorithm rather than memorizing short patterns.
Key Insights and Ideas
Venn diagrams of language classes and which languages each architecture learned perfectly
- Transformers hit the star-free ceiling: Transformers (both SinPE and NoPE) learn some $\mathscr{R}$-trivial and star-free languages perfectly, but zero non-star-free regular languages. This aligns with Yang et al.'s (2024) star-free upper bound, but contradicts Li & Cotterell's (2025) tighter $\mathscr{R}$-trivial upper bound (possibly because 500-token strings aren't long enough for that bound's asymptotic behavior to kick in).
- LSTMs still rule formal languages: Despite transformers' dominance in natural language processing, LSTMs and simple RNNs recognize almost a proper superset of the languages transformers learn, and LSTMs are the top-performing architecture in every single class.
- Sensitivity hurts transformers: Using a generalized sensitivity metric (how often flipping one symbol changes language membership) and a linear mixed-effects model, the authors confirm Hahn & Rofin's (2024) finding: transformers' accuracy drops significantly on high-sensitivity languages relative to simple RNNs.
- Approximation vs. perfection: On context-free languages, all models achieve 0.999 mean accuracy, yet none can reliably solve them perfectly (100% test accuracy), showing a sharp gap between strong statistical approximation and exact algorithmic generalization.
Key Results
Across 30 languages per class, the proportion of languages learned perfectly (100% test accuracy) tells the clearest story:
| Architecture | $\mathscr{R}$-Trivial | Star-Free | Regular | Context-Free |
|---|---|---|---|---|
| Transformer (SinPE) | 0.40 | 0.37 | 0.00 | 0.27 |
| Transformer (NoPE) | 0.50 | 0.33 | 0.00 | 0.43 |
| Simple RNN | 0.67 | 0.80 | 0.23 | 0.50 |
| LSTM | 0.97 | 0.90 | 0.80 | 0.63 |
| 3-Gram Classifier | 0.00 | 0.20 | 0.00 | 0.00 |
In terms of mean test accuracy, LSTMs score 1.000 on $\mathscr{R}$-trivial and star-free languages, 0.991 on regular languages, and 0.999 on context-free languages. Transformers lag behind on star-free (0.917 SinPE / 0.939 NoPE) and especially non-star-free regular languages (0.774 SinPE / 0.783 NoPE)—performing worse than the 3-gram baseline (0.791) on regular languages.
Limitations and Future Work
- Undecidability in CFGs: Checking whether an arbitrary context-free grammar is actually regular is undecidable, so some sampled context-free languages may secretly be regular (though the authors mitigate this by injecting self-embedding rule chains).
- Length and training limits: Test strings only go up to length 500, which may be too short to observe theoretical limits that only emerge at extreme lengths. Also, failing to learn a language could reflect optimization hiccups rather than a strict lack of expressivity.
- Future directions: Because the sampling algorithms have full support, expanding SoL is simply a matter of scale. The authors release their code and Docker environment so others can test new architectures (e.g., state-space models) against this benchmark.
Summary updated