# The forced-mate prover and the Tally move ranker

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

## Method

**Prover.** B(s, n) asks whether White can force mate within n White moves. At a White turn the prover tries every
legal move. After a move it needs a forced continuation against every Black reply. A line fails for White on
stalemate, insufficient material, or a draw Black can claim. A move-ordering score changes only the order in which
White's moves are tried, so it affects speed, never the answer (D0RE C8's rule). Proof trees are written as JSON:
`{"move": "h3h1", "mate": true}` or `{"move": ..., "replies": {black_move: subtree, ...}}`.

**Verifier.** `verify_proof.py` uses none of python-chess. For every node it regenerates legal moves with the D0RE
engine and checks four things: the White move is legal; a node marked mate really is checkmate; the replies listed
are exactly Black's legal moves; and Black cannot claim a draw at that point. It rejects any tree with a reply missing.

**Network.** A move is described by eight named integers:

| feature | meaning |
|---|---|
| `gives_check` | 1 if the move checks |
| `captured_value` | value of the captured piece (pawn 1, minor 3, rook 5, queen 9) |
| `mover_value` | value of the moving piece |
| `promotion` | 1 if the move promotes |
| `distance_to_king` | distance from the destination square to Black's king |
| `king_moves_after` | legal moves Black's king has after the move |
| `replies_after` | legal replies Black has after the move (capped at 30) |
| `king_zone_attacked` | squares next to Black's king that White attacks after the move |

The score is Σ weight × feature. Training is a ranking perceptron in exact integers. On each training position, if
the top-scored move is not a winning one, the weights move toward the first winning move and away from the top move.
The network used is the averaged perceptron: the sum of the weights after every position, over 6 passes. Tally runs
it with no floating point and no unnamed values (about 100 s), and `benchmark.py` re-derives the same weights in
Python and refuses to run unless they match exactly.

## Results

### The trained weights

| feature | weight |
|---|---|
| `promotion` | +20 457 |
| `replies_after` | −9 022 per reply |
| `king_zone_attacked` | +6 019 per square |
| `king_moves_after` | −4 537 per king move |
| `gives_check` | −1 459 |
| `captured_value` | −1 154 per pawn of value |
| `distance_to_king` | −757 per square |
| `mover_value` | −411 per pawn of value |

Because `replies_after` runs up to 30, it dominates. The network learned to try first the move that leaves Black the
fewest replies, the same principle as proof-number search: a position with fewer replies is cheaper to prove.
The features are correlated. A check usually cuts Black's replies, and that is already credited to `replies_after`
and `king_moves_after`. So the small negative weight on checks does not mean checks are bad. Read individual signs
with that in mind.

On the 300 training positions the averaged network puts a winning move first in 134 of them. The plain (unaveraged)
perceptron does so in 88, and python-chess's move order in 13.

### Benchmark: 60 held-out forced mates in 3

All three move orders prove all 60 mates, and all 180 strategy trees pass the D0RE-engine verifier. The network's
trees are in `proofs/` (402 nodes in total, 31 at most).

| move order | prover calls (total) | median per position | time for all 60 | network needs fewer calls in |
|---|---|---|---|---|
| python-chess generation order | 39 379 | 482 | 14.4 s | 59 of 60 |
| hand-written rule: checks, then captures, then promotions | 23 537 | 243 | 26.7 s | 49 of 60 |
| **Tally network** | **6 902** | **56** | **9.9 s** | — |

The network uses 5.7× fewer calls than python-chess's order and 3.4× fewer than the hand-written rule. Times are from
a single run and include computing the features, which is why the hand-written rule is slow despite fewer calls. The
network's worst case: in 11 positions the hand-written rule needs as few calls or fewer (for example
`8/8/8/1K2p3/1P6/1k4pQ/8/7R w`: 58 calls against the network's 224).

### Starting position: no forced mate for White within 13 plies

`initial_position.py` asks whether White can force mate within n White moves (2n − 1 plies), trying every White move
at every White turn. For this bound Black gets no draw claims (`Prover(claims=False)`). Taking those away only helps
White, so "no mate even then" implies "no mate under the full rules". It also makes the failure cache exact, because
the relaxed game no longer depends on move history.

`initial_position_d0re.py` repeats the search with the D0RE C3 engine alone (`verify_proof.attacker_can_mate`),
sharing no code with python-chess. It splits the work across White's 20 first moves on 3 cores.

| White moves | plies | forced mate for White | python-chess: prover calls | python-chess: time | D0RE engine: time |
|---|---|---|---|---|---|
| 1 | 1 | no | 1 | 0.0 s | 0.0 s |
| 2 | 3 | no | 21 | 0.0 s | 0.0 s |
| 3 | 5 | no | 466 | 0.0 s | 0.1 s |
| 4 | 7 | no | 7 218 | 0.7 s | 2.1 s |
| 5 | 9 | no | 93 025 | 7.7 s | 31.8 s |
| 6 | 11 | no | 1 024 746 | 76.8 s | 558 s |
| 7 | 13 | no | 9 808 024 | 684 s | not run (about 2.5 h) |

The 7- and 9-ply rows reproduce D0RE's R1 and C7. The 11-ply row goes past them and is confirmed by both engines. The
13-ply row rests on python-chess alone. D0RE's records cover both sides; this covers White only, which is what the
question asked.

The network plays no part in these rows. To prove *no* mate the prover has to try every White move anyway, so move
order cannot help. That is R1's point: ordering is not what breaks this wall. The speed comes from python-chess's
move generation, from the exact cache, and, on the D0RE engine, from two exact shortcuts: moves already known to be
legal skip re-validation, and at White's last move only checking moves are tested for mate. Each extra White move
costs about 9–17× more, so 15 plies would take hours and every further move another order of magnitude. A forced
win, if one existed, would lie far beyond this.
