& operator

The & filter has the syntax:
  left&right

where left and right are each filters. The left and right arguments must each be set filters or each be positionfilters.

& operator as set intersection

If the left and right arguments to the & filter are each set filters, then the & filter is itself a set filter whose value is the intersection of the sets of squares represented by its arguments.

For example,

  pin&{R attacks k}

Is the set of squares on which a pinned white rook attacks the black king. The left argument to the & filter is pin whose value is the set of squares on which there is a pinned piece. The right argumnet to the & filter is {R attacks k} , whose value is the set of squares on which there is a white rook which attacks the black king. The value of the & filter is the intersection of these two sets.

positional intersection: & used to compare two positions

If the left and right arguments to the & filter are each position filters, then the value of the & filter is the positional intersection of the positions represented by these arguments. In this case we call the & filter a positional intersection

The positional intersection of two positions is the set of squares on which the two positions have the same type and color of piece.

For example, consider the PGN file representing moves beginning from the start position of the game of chess:

  1. e4 e5
  2. Nf3 Nc6

Now, after reading this game, let

  x= position 0
  y= position 2

Thus, x is the initial position and y is the position after 1. e4 e5.

Then x&y is the set of squares on which x and y have the same contents. This is the 60 squares represented by:

  ~[e2,e4,e7,e5]

That is because on each of these 4 squares, x and y differ. The contents of x on the square e2 for example is a white pawn; but the contents of y on that square is empty, so they differ.

More generally, suppose X and Y are any variables that hold positions. Then then

  X&Y
    
  square z in .
    X:colortype z == Y:colortype z

That is because colortype z is a number expressing the type and color of the contents of the square z in the current position. Thus, X:colortype z is a number expressing the type and color of the contents of the square z in the position X (and likewise for Y:colortype z).

The square filter therefore gives the set of squares in which X and Y have the same contents, that is, X&Y

Examples

The & operator as set intersection is used throughout the CQL examples.

The positional & operator is most often used with the echo filter to compare positions. For example, in zugzwang1.cql , the filter

  source&target==.

is used to check whether the variables source and target represent the same board position. This works because the . filter rerepresentes the set of all squares, so the expression above matches the position when the source and target position have the same board contents (although not necessarily the same side to move).

The & filter is also used in contexts like movedblackpieceecho.cql where it is necessary to count the number of squares on which two positions differ. There we see the code:

  mismatches= ~(source&target)
  mismatches==2
This sets the variable mismatches to be the set of squares on which the positions source and target differ, and then requires that this set contains exactly 2 squares.

The next line of the file movedblackpieceecho.cql sets the piece variable named Mover the single black piece (if any) on that intersection:

  piece Mover=a&mismatches
Note that this use of & is not a positional ampersand. This use is just the usual set intersection, since both a and mismatches are set filters.