# Forcing-move logic and logical moves in ordinary play

Part of [`research/chess-proof`](../README.md). Scripts are in the parent folder, results in `../data/`, figures in `../figures/`.

## Forcing-move logic

A forced mate is a strategy: one White move at each White turn and an answer to every Black reply. That asymmetry is
the whole idea. **White's moves can be pruned by any rule without risking a false proof**: if a mate is found among the
moves the rule keeps, the tree still answers every Black reply, and the D0RE engine checks it as before. Black's
replies can never be pruned. What pruning costs is completeness: if the restricted search finds nothing, a pruned
move might still have won.

`mate_prover.LogicProver` keeps a White move only if it is

| class | definition |
|---|---|
| check | it gives check |
| capture | it captures |
| promotion | it promotes |
| threat | if Black could pass, White would mate at once |
| fork | the moved piece then attacks two or more targets: the king, a piece worth more, or an undefended piece |
| squeeze | Black is left at most 3 legal replies, or Black's king is bound (no legal king move, although some neighbouring square is free of Black's own pieces) |

Two exact rules go with it. If White can mate at once, only that move is tried; it wins at any depth, and it means
every threat counted afterwards is one the move creates. On White's last move only checks are tried, since only a
check can mate. The optional `deep=True` mode also tries quiet moves that would mate in two or more if Black could
pass, testing each one only after every statically forcing move has failed.

Why a winning quiet move has nowhere to hide: suppose Black could pass after it. If White would still mate, the move
is a threat. If passing would save Black, then Black is lost only because it must move: zugzwang. Zugzwang needs
Black short of moves, which is what the squeeze catches. The squeeze bound of 3 is a measured choice, not a theorem.

**The rule in Tally.** `chess_forcing_logic.tly` states the classes as named rules over named facts for every
labelled training move. The facts themselves (does the move give check, how many replies it leaves, is the king
bound) are computed in Python by `mate_prover.py` and exported as data (`export_tally_data.py`); Tally holds the rule
over them and the measurements. It then checks that the rule keeps a winning move in all 300 mates in 2, and measures what
each class is worth by removing it:

| class removed | none | check | capture | promotion | threat | fork | squeeze |
|---|---|---|---|---|---|---|---|
| positions that still keep a winning move (of 300) | 300 | 299 | 300 | 300 | 289 | 300 | 255 |
| moves kept (of 10 575) | 6 683 | 6 340 | 6 628 | 6 651 | 6 464 | 6 625 | 2 949 |

Check, threat and squeeze are each necessary: remove one and some position loses its last winning move, and Tally
checks this. Capture, promotion and fork are redundant on this data, because every winning capture, promotion or fork
here is also a check or a threat. They stay in the rule for deeper and richer positions. The squeeze prunes poorly in these
sparse endgames: with a lone Black king, almost any White move leaves at most 3 replies. In the 170 winning quiet
moves that are not threats, though, Black is left 1 reply in 148 cases and at most 3 in 169. That is zugzwang made
measurable. The odd one out, Qb6 in `k1b5/8/p7/N7/1Q6/8/7K/8 w`, binds the king and leaves 6 bishop moves, every
one of which allows mate.

**Completeness on the 360 known mates** (`logic_benchmark.py`, all with the network's move order; every tree found
verified with the D0RE engine):

| search | mates in 2 found | calls | time | mates in 3 found | calls | time |
|---|---|---|---|---|---|---|
| every White move | 300 / 300 | 1 755 | 3.3 s | 60 / 60 | 6 902 | 10.3 s |
| forcing moves | 300 / 300 | 1 728 | 1.9 s | 58 / 60 | 3 462 | 3.3 s |
| forcing moves + deep threats | 300 / 300 | 1 728 | 1.9 s | 60 / 60 | 3 747 | 4.0 s |

The two mates in 3 that the static rule misses (keys Be1 and Nc4) are quiet moves threatening mate in 2. The deep
mode finds both.

**Deeper mates** (`deep_benchmark.py`). The forcing search looked at 593 random positions, budget 20 000 calls each.
It kept the first 40 whose shortest forcing mate is 4 or 5 moves, and the D0RE engine verified all 40 trees. The
full-width search then had 100 000 calls for each:

| | positions | forcing: median calls | full width: median calls | forcing: total time | full width: total time |
|---|---|---|---|---|---|
| mate in 4 | 24 | 358 | 1 713 | 11 s | 46 s |
| mate in 5 | 16 | 2 983 | 23 820 | 55 s | 473 s |

The forcing search needed fewer calls in all 40 positions: 76 487 in total against 571 582 (median ratio 5.5×,
largest 36.6×). The full-width search ran out of budget on one mate in 5. Every time it found a mate, the mate had the
same length as the forcing one, so these forcing mates are shortest mates. The advantage grows with depth, about
5× at mate in 4 and 8× at mate in 5, because the pruning compounds at every White turn. These positions were chosen
because the forcing search found them; how often a mate is missed is measured on the known mates above, not here.

**The starting position.** None of White's 20 first moves is forcing by this definition. After 1.e4 e5 2.Nf3 Nc6
3.Bc4 Nf6, White has two forcing moves, Bxf7+ and Nxe5. After 1.e4 e5 2.Qh5 Nc6 3.Bc4 Nf6??, the only candidate is
Qxf7#. Forcing logic has no grip until the pieces make contact. A forced win from the start, if one existed, would have
to begin with quiet moves, and through that quiet phase the proof must answer every one of Black's 20–40 replies at
every turn. Choosing White's move perfectly halves the exponent; it does not remove the exponential. That is why
pruning White's moves makes mates provable but cannot by itself decide the starting position. Checkers was solved (a
draw) in 2007 by a search that examined about 10¹⁴ of its 5 × 10²⁰ positions. Chess has about 4.8 × 10⁴⁴.

## Logical moves in ordinary play, graded by Stockfish

The question: how few moves does a player need to consider, chosen by named logic alone, and still play 90%+
accuracy games? Accuracy is lichess's measure, computed from Stockfish's evaluations.

**Games** (`play_games.py`). 48 games of Stockfish 16 self-play at 50 000 nodes a move, each starting with 6 random
plies among the moves within 50 centipawns of the best. In every position after those plies, every legal move is
scored by one depth-12 multi-PV search (5 013 positions), so the players' own search is not what grades them.
Accuracy uses lichess's formulas. A move's accuracy is 103.1668·e^(−0.04354·Δ) − 3.1669, where Δ is the drop in win%
from the best move, and win% = 50 + 50·(2/(1 + e^(−0.00368208·cp)) − 1). A side's game accuracy is the mean of the
arithmetic and harmonic means of its move accuracies; lichess also weights by volatility, which is left out here. All
48 games reach 90%+ for both sides (the lowest side is 97.3%), so every game is used: 32 for fitting and validation,
16 held out for testing.

**Facts** (`play_logic.py`). Sixteen named counts per move: check, material captured, value of the moving piece, piece
put en prise, piece rescued, other piece left en prise, threat created, promotion, castling, development, centre, king
walk, pawn rank, the moved piece's activity, attacks next to the enemy king, and the opponent's legal replies. "En
prise" means attacked and either undefended or attacked by a cheaper piece. Attackers are not counted, so a double
attack on a defended piece does not register; that is a known simplification.

**Cancellation** (`reduce_logic.py`), in the spirit of the photon reduction (`../tally/PHOTON_MINIMUM.md`): keep only
what the measurement can tell apart. The measurement is Stockfish's verdict. The observable is the game accuracy of a
player who considers only a rule's top k moves (k = 2–5) and picks the best of them. Integer weights, starting from a
hand-written rule, are tuned one at a time on the fit games. Then every fact is cancelled in turn and the rest retuned,
and a fact goes for good if the validation games cannot tell the difference (no k loses more than 0.3 points). The
tuning zeroed check, moving-piece value, activity and king-zone attacks. Cancellation then removed centre, promotion,
king walk, rescue and castling. Seven facts survive:

| fact | weight |
|---|---|
| material captured | +23 per pawn of value |
| piece put en prise | −18 per pawn of value |
| piece left en prise | −6 per pawn of value |
| knight or bishop developed | +8 |
| threat created | +1 per pawn of value |
| pawn rank reached | +1 per rank |
| opponent's legal replies | −1 per reply |

In words: win material, don't hang it, develop, and leave the opponent as few replies as possible. Check went out
because "few replies" already carries it, since a check leaves only a handful. That is the chess version of a gauge
copy: two descriptions of the same thing. `../tally/chess/chess_play_logic.tly` re-derives the rule's counts on the
1 867 held-out test positions: a good move (accuracy 90+) is the rule's first choice in 1 084, in its top 3 in 1 507,
and in its top 5 in 1 646. Python's counts are required to match exactly.

**Held-out test games** (16 games, 32 sides, 26.7 legal moves on average; the restricted choice is scored in the
original game's positions):

| ordering | k = 1 | k = 2 | k = 3 | k = 4 | k = 6 | sides at 90%+ with k = 3 / 4 / 6 |
|---|---|---|---|---|---|---|
| python-chess's order | 44.6 | 53.1 | 59.6 | 63.6 | 71.8 | 0 / 0 / 1 |
| hand-written rule | 69.9 | 81.6 | 86.9 | 89.8 | 93.1 | 9 / 19 / 29 |
| Tally perceptron (16 facts) | 69.4 | 79.7 | 84.3 | 87.6 | 90.7 | 8 / 13 / 21 |
| **reduced rule (7 facts)** | **73.4** | **87.1** | **92.6** | **94.6** | **96.3** | **26 / 30 / 32** |

Mean game accuracy in percent. The perceptron (`../tally/chess/chess_play_network.tly`, weights identical in Tally and
Python) is the weakest learned option. It optimizes the top move, while the observable is whether a good move survives
in the top k, so the reduced rule tunes on the observable itself.

**Real games** (`logic_games.py`). Here the restricted player lives with its own choices. It ranks its moves with the
reduced rule, keeps the top k, and lets Stockfish (50 000 nodes) choose only among those k. With k = 1 it plays the
rule's first choice with no search. Its opponent is unrestricted Stockfish at the same budget, the openings are random
as above, and colours alternate. Every move it makes is graded at depth 12.

| k (moves considered / legal, means) | games | games at 90%+ | mean accuracy | lowest | points for the logic player |
|---|---|---|---|---|---|
| 1, the rule alone, no search (1.0 / 23.7) | 8 | 2 | 86.8 | 73.4 | 0 |
| 3 (2.9 / 26.8) | 20 | 15 | 91.0 | 60.2 | 0 |
| 4 (3.8 / 26.2) | 12 | 11 | 92.9 | 87.7 | 0 |
| **5 (4.6 / 24.6)** | **12** | **12** | **94.3** | **91.4** | 1 |
| 6 (5.5 / 25.8) | 20 | 20 | 94.9 | 92.3 | 0.5 |
| 10 (8.6 / 22.5) | 8 | 8 | 95.5 | 91.1 | 1 |
| every legal move, the control (28.1) | 8 | 8 | 98.6 | 96.6 | 3.5 |

From k = 5 on, every game is 90%+: all 32 games at k = 5 and 6, considering 4.6–5.5 of about 25 legal moves. At k = 3
the mean is 91%, but 5 of 20 games fall below 90. The control shows the harness is fair: unrestricted Stockfish scores
3.5 of 8 against itself. A game still unfinished at ply 140 counts as half a point. An earlier version of this harness
used one Stockfish process for both players and the grader. Through the shared hash table, the opponent inherited the
grader's depth-12 analysis, which made the restricted player look worse. Every number here is from the corrected
harness, with three separate processes.

## What the play results show

- **The goal's bar is met.** A player that considers only its 5 most logical moves (4.6 of about 25 legal, on
  average) played 90%+ accuracy, by Stockfish's measure, in all 12 of its games; with 6, in all 20. In held-out
  positions the rule's top 3 contain a move of 90+ accuracy 81% of the time, and its top 6 bring every test side to
  90%+. The rule that picks them is seven named facts.
- **90% accuracy is not a solution.** Restricted to k ≥ 3 logical moves, the player scored 2.5 points from 72 games
  against unrestricted Stockfish, which scored 3.5 of 8 against itself. Even at 95% accuracy, a strong opponent
  converts the few misses. A forced result needs every move to keep the game's value, not 90% of the accuracy budget. At about 4 in 5 positions per move, a 40-move game almost never goes without a miss.
- **What logic buys a proof.** In a proof of a win, the winning side's choice can be narrowed by any rule without
  risking a false proof. Narrowing 26.7 candidates to 3 shrinks the search for the winning move by about 9× at every
  one of that side's turns, compounding with depth. The defender's replies still cannot be pruned. And when the
  narrowing misses the one good move, the proof search must fall back to the rest.
- **Stockfish already works this way.** Its search does not explore every move fully either. It reduces late moves,
  prunes futile ones, and searches a few deeply. The rule here is a transparent, seven-fact version of that idea, not
  a replacement for search.
