attacks and attackedby (deprecated

The attacks and attacksby filters are deprecated. Use and instead. The attacks 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 right
The 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 right
matches a position if and only if
right attackedby left
matches that position.

Here are some specific examples

      A attacks k 
      Q attackedby n
      . attackedby k

The sets represented by attacks and attackedby

Each of attacks and attackedby are set filters.
  left attacks right
represents the set of squares in left on which is a piece that attacks some square in right

  left attackedby right
represents 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>1
matches the position. That is because
A attacks k
represents 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 k
represents 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<=1
Here, _ 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 squares c5 and e5, no matter what other pieces are on the board or whose move it is. Likewise, a black pawn on g7 attacks exactly the squares h6 and f6, even if black is in check, or it is white's turn, or there are black pieces on h6 and f6.
  • 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 attacks c4, even if the knight is pinned.

precedence

The attacks and attackedby have the same precedence (left-to-right). They have stronger precedence than the set operations & and |. Hence,
A attacks k | n
is parsed as
(A attacks k) | n
and 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>1
is the same as
(A attacks k)>1
which 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:
Arestov 2013, after 10. h8=N#
(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:

Bazlov 2008
(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 k
will 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  attacks 
will be true in a position in which the black king is not in check.
 not  attacks ( )
will be true in a position in which no empty square in the black king's field is attacked by a white piece.
  attackedby >= 40
will be true when white is attacking at least 40 squares.

The attackedby filter is likewise used in the mate related examples above, notably to compute the king's field:

KingsField=  attackedby 
It is also used in wurzburg-plachutta.cql to loop over all empty squares that are attacked by two different pieces:
       square C in ( attackedby X)&( attackedby Y)
            ....