# Square filter

A square filter consists of the keyword `square`, then an optional keyword `all`, then a variable, then the keyword `in`, then a set filter, then an arbitrary filter:
```  square variable in in_filter body
square all variable in in_filter body```

Here, variable is a set variable; in_filter is a set filter; and body is any filter.

For example,

```  square x in R
x attacks k

square z in a1-8
a attacks z

square all z in A
power z<2```

In the first example above, variable is `x`; in_filter is `R`; and body is `x attacks k`.

The operation of the square filter when the the optional `all` parameter is absent is as follows:

1. The set in represented by in_filter in the current position is computed
2. For each square s in in,
• the variable x is set to s.
• (*) body is evaluated in the current position
3. The `square` filter represents the set of s in in for which body was true in step (*)

The operation of the square filter when the `all` parameter is present is similar:

1. The set in represented by in_filter in the current position is computed
2. For each square s in in,
• the variable x is set to s.
• (*) body is evaluated in the current position
3. The `square` filter matches the position if body is never false in step (*): for each s in in, the body is true

Thus, a `square` filter is a set filter only if the `all` parameter is absent.

## Example of a square filter

Suppose the current position is set to the following position:

Now let's compute what happens when the following `square` filter is evaluted on that position:

```square x in h1-8
R attacks x
x attacks K```

To determine the set of squares represented by this square filter in the diagram, the variable `x` will be successively set to each square in the set represented by the following the word `in`, namely `h1-8`.

The square filter will match an assignment of `x` to a particular square only if a white rook attacks that square and if that square attacks the white King. In the current position, note that:

`qh3` attacks the white king's square and is attacked by a white rook - match!
`nh6` attacks the white king's square but is NOT attacked by the white rook - no match
`Nh4` attacks the white king's square but is NOT attacked by the white rook - no match
`Bh7` attacks the white king's square and is attacked by Ra7 - match!

all other squares on the h-file fail both clauses The result is that filter matches when `x` is either `h3` or `h7`.

Therefore, the square filter represents the set of squares consisting of `h3` and `h7`. In particular, the square filter will match the current position.