WorkDifferential testing
A chess engine made of regular expressions, checked move by move
Nicholas Carlini's regex-chess plays chess by applying 84,690 regular expressions to a text board. We compared it with python-chess on real games and on positions built to reach rare rules, and wrote down every difference with a minimal position, a patch and a test that fails without it.
Make a legal move.
Watch what happens.
Move this pawn one square forward.
The same move will run in both programs.
Original
Downloaded program · committed JSON
With our patches
Promotion, castling and stalemate fixes
What just happened?
Inspect the actual engine output
Original
With our patches
These are live runs of Carlini’s regex rules, with and without patches 0001, 0002 and 0006. The rules load when you play. Source & patches · GPL-3.0-or-later
Research summary
- Problem
- Where does a chess engine implemented as regular expressions disagree with legal chess moves and game outcomes?
- Result
- Differential checks against python-chess produced minimal positions, patches and regression tests for a losing pawn move, rejected promotions, stalemate handling and move-generation gaps.
- How to check it
- Reproduce the standalone checks and patch tests in the README against the stated upstream snapshot. Compare the committed rules, regenerated rules and patched rules separately.
- Limits
- The findings apply to the recorded upstream version and test scope. Some exclusions are deliberate, including upstream tests that skip underpromotion. Aggregate experiment counts are reports, not a proof that all chess positions were checked.
- Research question
- Do the patches preserve legal behaviour over a broader set of positions and all supported promotion variants?
What a player sees
Carlini built regex-chess as an experiment: a game played by substitutions on a text board. The file a player runs matters. The three builds differ in where a human pawn becomes a queen.
| Program version | a2a3 | A human promotion |
|---|---|---|
Committed regex-chess.jsonRun by python main.py | Rejected: the game is lost | Accepted onto a8 only |
Regenerated sourceAt commit 5401b16 | Accepted | Rejected on all of a8 to h8: the game is lost |
The article's web builddata.js.gz, 2025-01-07 | Accepted | Accepted except onto c8 |
With patch 0001, the regenerated source accepts promotion onto each of a8 to h8. Source and reproduction.
What we found
-
A promotion is rejected, and the game is lost
The source lists promotion squares on the first rank, with
c2among them. A white pawn that reaches the eighth rank stays a pawn, and the program rejects the move. In the web build, promotion and capture onto c8 lose the game.Finding A1 · Patch 0001
-
The checked-in program predates the fix
The fix for issue #1 changed the source but did not regenerate
regex-chess.json. The committed file still treats a1 to a8 as promotion squares. The opening movea2a3loses the game.Finding A2 · Regenerate the JSON after applying 0001
-
A stalemate is announced as checkmate
With no legal reply, the engine announces checkmate even when its king is not in check. This is the engine side of issue #3. A mated or stalemated human gets
You Lose.only after the next input; no draw is announced.Play
b5b6from this position:k7/2K5/8/1Q6/8/8/8/8 w - - 0 1Finding A3 · Patch 0006 fixes the engine side only
-
Four rare gaps in the move generator
The generator differs from python-chess for four independent reasons. Each has a minimal position that isolates it.
The first 100 candidates only
The program stops before checking every candidate for legality. In this position it misses
a1b1.7k/Q7/3Q4/8/4Q3/1Q6/8/K7 w - - 0 1A shared bound for bishops, rooks and queens
MAX_BISHOPS = 6counts all three together. A seventh piece is ignored, so its moves or attacks can go unseen.7R/4QR2/k2n2QQ/8/8/2R2Q2/3K4/8 w - - 0 1Castling keeps an old en-passant square
After
e1g1, the en-passant field still saysa6. It should have been cleared.4k3/8/8/p7/8/8/8/4K2R w K a6 0 1Underpromotion is not generated
The program generates promotion to a queen only. Its own tests skip promotion to a rook, bishop or knight. A related bound ignores knights beyond the second; only underpromotion can create one.
7k/P7/8/8/8/8/8/K7 w - - 0 1Minimal positions, patches and evidence. Minimal means no single deletion preserves a valid position with the same difference; it does not mean globally shortest.
How often it matters
In 46,211 positions of 500 Carlsen games, 552 (1.2%) differ, all through two causes: underpromotion and the stale en-passant square.
That is how often two known configurations occur, not a rate of wrong play.
White moves a pawn onto the a-file in 5,543 of the 7,818 games of the file (70.9%).
How often people playing against the program encounter it has not been measured.
The patches
Take first · +23 rules
Promotion, castling and stalemate
Apply 0001 (promotion), 0002 (clear en passant after castling) and 0006 (report engine-side stalemate). Then regenerate regex-chess.json.
Recommended set · +8,011 rules in total
Give the generator more room
Add 0004, then 0003: raise the candidate bound to 256 and the shared bishop, rook and queen bound to 13. With the first set, that is 9.5% more rules and replies about 1–4% slower. The candidate bound covers every position found so far; it is a margin, not a proof.
Optional · 0004b, 0005, 0005b
The reply walk and underpromotions
0004b raises the bound for the walk that scores replies. 0005 adds underpromotion; 0005b supports the extra knights it can create. Omitting the latter two leaves those known gaps.
Every patch except 0004b has a unit test that fails without it. With the full set, all 50,000 positions in the regression corpus match python-chess.
Check it yourself
Clone the findings repository, then check out the upstream version these findings refer to. Set REPO to your local findings directory.
git clone https://github.com/carlini/regex-chess && cd regex-chess
git checkout 5401b16
REPO=/path/to/regex-chess-findings
Try the opening move
printf 'a2a3\n' | python3 main.pyThe committed file prints *Illegal Move* You Lose. The following EOFError comes from the game loop reading input once more.
Check the game loop
python3 $REPO/findings/upstream/standalone_checks.pyAbout 5 seconds, standard library only. Reports the opening move, promotions and stalemate.
Compare the move generator
python3 $REPO/findings/repro.py .About 30 seconds. Needs python-chess (pip install chess); exits with status 1 while a case still differs.
Status
Sent so far: the promotion fix and the regenerated regex-chess.json, as pull request #5 (2026-11-17). Nothing else has been sent.
Read pull request #5. No reply is expected.
Scope
- This is differential testing against python-chess, not a proof of correctness.
- The regression corpus was built to reach the defects. Its counts do not measure how often errors occur in play.
- We did not evaluate which reply the engine chooses.
- Some differences reflect the experiment's chosen limits. The author's own tests skip underpromotion.
Sources
regex-chess-findings · GPL-3.0-or-later · ← Back to Formalized