20 March 2026 · 5 min read

The pumping lemma as a model test

The pumping lemma is a doubling argument: a finite-memory recogniser that accepts a long string must accept it with the middle repeated. It tests a model for grammar versus memory.

The pumping lemma is taught as a tool for proving that a language is not regular, or not context-free, and students learn it as a ritual with five variables and a contradiction at the end. It is simpler than that and more useful. It is a doubling argument: any recogniser with a finite amount of memory that accepts a long enough string must have repeated a memory state somewhere in the middle, and if it repeated a state, it must also accept the string with that middle section repeated any number of times. That is a statement about any finite machine, and a neural network run once over an input is a finite machine. So the lemma gives a direct experimental test of whether a model learned the grammar of a task or memorised the lengths it was trained on, and it is a test I would run before believing any claim that a model parses.

The argument, without the ritual

Take a recogniser with a bounded number of internal states, and feed it a string longer than that number. By the time it has read the string, some state must have occurred twice, because there are more positions than states. Call the section between the two occurrences the middle. The machine was in the same state before and after the middle, so if you delete the middle, or repeat it, or repeat it a thousand times, the machine ends in the same state as before and gives the same answer. If the language requires the answer to change, the machine cannot be recognising it. That is the whole lemma for regular languages, and the context-free version is the same argument applied to a pushdown automaton's stack, where two pumpable sections appear instead of one because the stack has to grow and shrink in matched pairs.

A string split into five parts with the middle pumped, and what a pushdown automaton does with it against a bounded-context model Top: a string drawn as five segments u, v, w, x, y, with v and x highlighted as the pumpable sections. Below, the same string with v and x repeated twice. Left annotation: a pushdown automaton pushes during v and pops during x, so repeating both keeps the stack balanced and the string is still accepted. Right annotation: a bounded-context model that has memorised strings of the training length sees a string longer than any it has seen and its answer has no reason to stay the same. Repeat the middle; a grammar does not notice, a memoriser does THE STRING, SPLIT AS u v w x y u v w x y PUMPED TWICE: u v v w x x y u v v w x x y A pushdown automaton pushes through v, pops through x; repeat both and the stack still balances. accepted, at any pump count A bounded-context memoriser has seen strings up to one length; the pumped string is longer than any. no reason for the answer to hold The first behaviour is forced by having the grammar; the probe checks for it.
Illustrative: the standard decomposition of a long string in the context-free pumping lemma, with the two behaviours the probe distinguishes.

Two things about the argument matter for models. The first is that it is about memory, not about training. A machine with a bounded number of states cannot recognise a language that needs unbounded counting, however it was built, and more examples of the language do not add states. The second is that the argument is constructive: it tells you what string to build to expose the limit. Take a string the machine handles and repeat its middle. If the machine has the grammar, the answer is forced. If it does not, the answer will drift, and it will drift at a length that reveals how much memory the machine actually has.

What the benchmark found

Delétang and colleagues ran the closest thing to this test at scale in Neural Networks and the Chomsky Hierarchy, training 20,910 models across fifteen tasks arranged by the hierarchy and testing on inputs longer than anything seen in training. The finding is the one the lemma predicts. Recurrent networks and transformers failed to generalise on tasks above the regular level. Networks with an explicit counter, the LSTM family, handled regular tasks and counter languages. Only networks with an external memory, a stack or a tape, generalised on context-free and context-sensitive tasks. The grouping by hierarchy level forecast which architectures would generalise, independently of data or compute, which is a polite way of saying that the limits are formal properties and not training bugs.

Length generalisation by architecture and task class, as summarised from the Chomsky hierarchy benchmark A matrix with architectures as rows, RNN, Transformer, LSTM, Stack-RNN, Tape-RNN, and task classes as columns, regular, counter, context-free, context-sensitive. Filled cells mark where the paper reports generalisation to longer inputs: RNN and Transformer on regular tasks only, with the paper noting transformers struggling even there on some tasks; LSTM on regular and counter; Stack-RNN on regular, counter and context-free; Tape-RNN on all four. Memory decides the class a model can generalise on REGULAR COUNTER CONTEXT-FREE CONTEXT-SENSITIVE RNN Transformer LSTM Stack-RNN Tape-RNN generalises to longer inputs does not
Source: the findings stated in Delétang et al., Neural Networks and the Chomsky Hierarchy, 2023, summarised as a matrix; the paper's per-task accuracies are finer than this and the summary follows its abstract.

The transformer row is the one people find hardest to accept, because transformers do so much else. But a transformer run once over an input has a fixed number of layers and a fixed-width state per position, and the lemma does not care what else it can do. More data moves a transformer along its row, toward doing the regular tasks it can already do more reliably. It does not move it down the table, because down the table is a memory it does not have.

The doubling probe

The test that falls out of the lemma is what I call the doubling probe. Take a set of strings the model handles correctly, at the lengths it was trained on. Identify the pumpable middle for each, which for bracket matching is a balanced inner span and for a regular language is any section between repeated automaton states. Pump the middle two, four and eight times, and plot accuracy against the pump count.

The doubling probe curve for a memoriser and for a grammar-learner Accuracy against pump count on a log scale of one, two, four, eight, sixteen. A grammar-learner's line stays flat near one hundred percent. A memoriser's line stays high while the pumped string is within the training length, then falls off a cliff toward chance once the pumped length exceeds it. A vertical marker at the cliff is labelled as the model's effective memory. Flat means the grammar; a cliff means the lengths Accuracy on pumped strings by pump count; illustrative has the grammar memorised lengths 100% 50% 0 1 2 4 8 16 Pump count the cliff: training length, effective memory
Illustrative: the two curves the probe can produce, constructed from the definitions in the text; the position of the cliff is what the probe measures.

The curve has two readings and one number. A flat line means the model has the grammar: the pumped strings are still in the language, the model still accepts them, and the lemma's forced behaviour is what it does. A cliff means the model has the lengths: it handled the training distribution by memorising what strings of those lengths look like, and once the pumped string is longer than anything it saw, it has nothing to say. The position of the cliff is the number, because it is the length beyond which the model's memory of the task runs out, and that is its effective memory for the task in the sense the lemma uses the word.

The probe is cheap because it reuses the model's own successes. There is no need to construct adversarial strings or to search for failures; the lemma says where the failure has to be, if there is one, and the probe goes there directly. It also separates two claims that evaluation sets usually blur: that a model is accurate on a task, and that it has learned the structure the task is defined by. A model can be very accurate on a length-bounded benchmark and fail the probe at pump count two, and that model has not learned to parse, whatever the benchmark says.

One refinement makes the probe sharper. Pumping the middle keeps the string in the language, so a grammar-learner should keep saying yes; the complementary probe pumps only one of the two sections in a context-free language, which takes the string out of the language, so a grammar-learner should switch to no. A model that says yes to both has not learned the grammar; it has learned that long strings with familiar pieces are probably fine. Running both halves distinguishes a model that recognises the structure from one that recognises the vocabulary, and the second is far more common than benchmark scores suggest.

Why the proof is the point

My paper on CYK recognition includes pumping lemma proofs about the limits of context-free recognition, and relating that work to transformer-based parsing is where the probe came from: the same argument that bounds what a pushdown automaton can recognise bounds what any bounded-memory model can, and the argument is constructive enough to run. That is also the clearest example I have of what a mathematics degree teaches that a bootcamp skips. Not the lemma, which is a page in any textbook, but the habit of asking what a proof of impossibility says you can measure, and then measuring it.

The probe's limit is the one the lemma has. It detects the absence of the grammar; it cannot certify its presence, because a flat line to pump count sixteen is consistent with a cliff at thirty-two. What it can do is push the cliff out until it is beyond any input the model will see in use, and report where it was found. A model that parses in production should come with that number, and the lemma is how you get it.

Formal LanguagesGeneralisationTransformers
All writing

Written by Mohd Shayan

Get new posts by email

Occasional essays on engineering, AI, and building for the people technology leaves behind.

One email per new post. Unsubscribe any time.

Subscribe with RSS