attacks and attackedby
Theattacks
and attacksby
infix filters are used to determine the squares attacked by a piece or set of pieces. Each of these filters two set filters as arguments , left and right:
left attacks right left attackedby rightThe
attacks
filter matches a position if some piece on the set of squares represented by left attacks some square in the set of squares represented by right.
The attackedby
filter matches a position if some square in the set of squares represented by left is attacked by some piece in the set of squares represented by right.
Thus,
left attacks rightmatches a position if and only if
right attackedby leftmatches that position.
Here are some specific examples
A attacks k Q attackedby n . attackedby k
The sets represented by attacks and attackedby
Each ofattacks
and attackedby
are set filters.
left attacks rightrepresents the set of squares in left on which is a piece that attacks some square in right
left attackedby rightrepresents the set of squares in left that are attacked by a piece on some square in right
For example, black is in double check if
A attacks k>1matches the position. That is because
A attacks krepresents the set of white pieces attacking the black king. This set is
>1
if an only if it has more then one element, that is, if there are at least two pieces in it.
Similarly,
. attackedby krepresents the set of squares adjacent to the black King.
In a pure mate, we want each empty square around the black king to be attacked at most once. We can check this like this:
btm mate square all FlightSquare in _ attackedby k A attacks FlightSquare<=1Here,
_ attackedby k
represents the set of empty squares adjacent to the black king.
More precise definition of 'attack'
We say a piece x on a particular square attacks a square t in a position if a King of the opposite color of x on the square t would be in check from x.
In particular, a piece can attack a square even if the piece could not move to that square. For example:
- if there is a white pawn on
d4
, then that pawn attacks exactly the squaresc5
ande5
, no matter what other pieces are on the board or whose move it is. Likewise, a black pawn ong7
attacks exactly the squaresh6
andf6
, even if black is in check, or it is white's turn, or there are black pieces onh6
andf6
. - a pinned piece can attack a square even if moving to that square would be illegal due to the pin. A black knight on
b2
attacksc4
, even if the knight is pinned.
precedence
Theattacks
and attackedby
have the same precedence (left-to-right). They have stronger precedence than the set operations &
and |
. Hence,
A attacks k | nis parsed as
(A attacks k) | nand represents the set of white checking pieces and black knights. To get the white pieces attacking either the black king or the black knight, use:
A attacks (k|n)
As usual, we strongly recommend using parentheses to clarify such expressions if you have any doubt of the precedence.
Note that because attacks
and attackedby
have stronger precedence than the arithmetic
comparison operators, something like
A attacks k>1is the same as
(A attacks k)>1which is what is expected.
Examples
In a mirror mate the mated king is surrounded by empty squares, as in the following position from a study by Arestov:(found from CQL file: mirrormate.cql)
This was found by the CQL code below:
btm mate _ attackedby k == 8
In the above fragment, _ attackedby k
is the set of empty squares attacked by the black king. Equivalently, this
is the set of empty squares adjacent to the black king. Checking if set is == 8
is the same as checking that the number
of elements in the set equals 8, due to the rules for conversion of sets to numbers. Thus, that filter is the same as
#{ _ attackedby k} == 8
The diagram below shows a position where white mates with a lone knight:
(found from CQL file: loneknightmate.cql)
In the CQL file, the line N attacks k
assures that the black king
is under check from a white knight.
A attacks kwill be true in a position in which Black is in check. The associated set will be empty if the black king is not in check, and will contain the set of checking pieces otherwise.
not A attacks kwill be true in a position in which the black king is not in check.
not A attacks (_ attackedby k)will be true in a position in which no empty square in the black king's field is attacked by a white piece.
A attacks .>= 40will be true when white is attacking at least 40 squares.
The attacks
filter is used throughout the CQL examples. It is used extensively all the variations of pure mate computation, such as puremate.cql, modelmate.cql, modelstalemate.cql, idealmate.cql and so on. It is also used in clearance-delayed.cql to check whether a piece attacks a particular square.
The attackedby
filter is likewise used in the mate related examples above, notably to compute the king's field:
KingsField= . attackby kIt is also used in wurzburg-plachutta.cql to loop over all empty squares that are attacked by two different pieces:
square CriticalSquare in ( attackedby X)&( attackedby Y) ....