# dingstheme.cql

The CQL file dingstheme.cql searches for games that manifest a particular theme inspired by a famous game Ding Liren v. Ian Nepomniatchi, 2023 FIDE Championship, round 6.

In that game, Ding reached the following position before his 41st move:

Ding-Nepomniachtchi, World Championship 2023 game 6, before 41.d5

Ding played `d5` here, shocking and confusing several of the live commentators at the time. The point of this beautiful move is protection of the `e6` square.

After `41...a2 42.Qc7 Kh7 43.Ng6 Rg8 44.Qf7` Nepomniachtchi resigned:

Ding-Nepomniachtchi, final position after 41...a2 42.Qc7 Kh7 43.Ng6 Rg8 44.Qf7 1-0

Ding's idea is clear in the variation `44...Qg5 45.Qg8+ Kg8 46.Ra8+ Kf7 47.Rf8#` where black is mated because of the protection to `e6` given provided by `d5` six moves earlier:

Ding-Nepo, variation after 44...Qg5 45.Qg8+ Kg8 46.Ra8+ Kf7 47.Rf8#

We can ask whether this idea has been explored in studies.

There are of course several ways to define a theme like this. See, e.g., two article by S. Hornecker: Ding's WCC game 6 checkmate pattern and Alternative theme definition of Ding's theme.

We will look for positions where a white pawn `T` moves and does not move again. This pawn move does not check black, nor does it attack attack a square in the `♚`'s field (the squares the `♚` could move to). Later on, the the `♚` winds up mated because `T` attacks a square in that the king's field. In the Ding-Nepo example, `T` is the pawn initially on `d4`, and `e6` is the key square.

We also will require that the mate be a modelmate, which is a mate in which every square the `♚` could flee to is either guarded exactly once by a white piece or occupied by an unattacked black piece. In other words, there is one and only one reason that the `♚` cannot move to each square in its field. A model mate also requires that each white piece, except possibly for king and pawns, participates in the mate.

# dingstheme.cql line-by-line

As usual, we start by guessing an assignment for the thematic pawn. We limit ourselves to pawns on the first four ranks so that promotion does not play an immediate role:
`◭T∊♙a-h2-4`
Recall, as in the earlier explain-rookleapcorners.cql and explain-turton.cql, that this will try to find assign `T` to some white pawn in the first 4 ranks making the next filter, the `⊢`, true. The value of `T` will be the square that that pawn is currently on.

Next, like in our earlier examples, we begin a `⊢` filter that will follow the course of our theme:

`⊢ T――□ ///"thematic pawn move"`

The first move of the theme must be a move by the thematic pawn `T` to an empty square (`□`). When we find such a move, we add a comment to the PGN file. This is a smart comment: it will only be output for move that actually begins a matching theme.

The next line might seem a bit confusing at first:

`not T→(▦←♚)`
This filter represents the idea that after the move by `T`, `T` does not attack a square in the field of the `♚`. The symbol `▦` represents all the squares on the chessboard. It's the same as `a-h1-8`. Therefore, the value of
`▦←♚`
all the squares attacked by `♚`, in other words, the black king's field.

To specify that `T` attacks the `♚` field, we just require that `T→` this value.

By putting a `not` in front of the expression, we are ensuring that the move by `T` does not initially attack the `♚` field - it does not restrict the movement of the `♚`, at least at first.

The next line `not check` just ensures that the thematic move by `T` is not a check.

Next, we express the idea that `T` does not move again. Instead, some number of moves by pieces other than `T` occur:

`~T―― +`
Here `T` is the current square of the thematic pawn, so `~T` is all the other squares, and `~T――` represents a move by a piece other than `T`. As in explain-staircase-sort-annotated.cql, the `+` indicates that the previous constituent (in this `~T――`) is repeated one or more times.

We end with the two thematic requirements. First, black must end up in model mate: `modelmate`. (In studies, White does not get mated in the mainline, so a mate in the main line as here mut be of black). Note the `///` filter after `modelmate`, which adds a comment to the pgn file indicating the mate.

Second, `T` attacks a square in the `♚` field square. This is the same condition as after the thematic pawn move, but without the `not`.

# summary

We've used CQL to express a theme in a concise way, and searched for that theme in studies. You can see an example by looking at the output of dingstheme.cql . We've introduced common CQL terms for the king's field (`▦←♚`) and for model mate (`modelmate`). We've used some smart comments to annotate the results.