Sample CQL files

Allumwandlung All promotions occur

allumwandlung.cql can be downloaded here.

;; Finds all studies with Allumwandlung, including mixed, in the main line.
;; The order of position keywords inside the match keyword is not significant.
(match
 :pgn heijden.pgn
 :output out.pgn
 (position :promote [Nn])
 (position :promote [Bb])
 (position :promote [Rr])
 (position :promote [Qq])
)

Bristol searches for the Bristol theme.

bristol.cql can be downloaded here.

;;look for queen-bishop bristols
;;


(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :sequence
  ((position :movefrom Bd4
	     Q[c3,e3,e5,c5]
	     :shift)
   (position)
   (position :movefrom Q :raydiagonal (B Q))
   (position Bd4 Q[c3,e4,e5,c5] :shift)
   )
  )
 ) 

Castling

castle.cql can be downloaded here.

;;This file looks for all studies in which black or white castles in the main line
;; or in a variation (or both)
;; This is done simply by looking for positions such that the current move is 
;; a move of a King from the e file to the c or g files

(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :movefrom [Kk]e?
  :moveto .[c?,g?]
  :variations ; look also in the variations
  )
)

Caterpillar Tripled pawns creep up the board

caterpillar.cql can be downloaded here.

; In the caterpillar theme, tripled pawns creep up the board one pawn at a time

(match :pgn mega.pgn :output out.pgn 
 (position Pb2 Pb3 Pb4 :movefrom Pb4 :moveto .b5
 :gappedsequence
  ((position Pb3 Pb4 Pb5))
 :flipcolor
 :shift
 )
)

Ideal mirror mate - All pieces participate, each square in the king field attacked once.

idealmirrormate.cql can be downloaded here.

;;This finds all ideal mirror mates: studies in which the Black King 
;;is surrounded by empty squares, is attacked once,
;;such that if the Black King were to move to one of its neighbors, 
;;it would be attacked exactly once.

;;This also finds positions in the variations, since there turn out to be Bishop 
;;mates only the variations.


(match
 :pgn heijden.pgn
 :output out.pgn
 (position :and ;; the mating position must match each criterion below:
	   (
	    (position ; The king is surrounded by empty squares and is attacked once and is in mate
	     :attackcount k . 8 
	     :attackcount A k 1 
;	     :attackcount N k 0
	     :mate :btm)

;;          Check that no neighbor of the black king is attacked more than once
	    (position :not 
		      :attackcount k .e4 1
		      :attackcount A .e4 2 100
		      :shift
		      )


;;         The next two clauses remove certain pathological cases let in by the above clauses. 
;;           They can be removed
;;         if desired, and one will find a few spurious hits

;;          Check that there is no rook attacking the Black king, 
;;          such that the black king would still
;;          be attacked by the rook if it went to an empty square, e.g., Ra3, kd3, Qe8

	    (position
	     :not
	     :attackcount k .e4 1
	     :attackcount A .e4 1
	     :rayorthogonal ([QR] k .e4)
	     :shift
	     )

;;          Same as above, but for diagonal moving pieces
	    (position
	     :not
	     :attackcount A .e4 1
	     :attackcount k .e4 1
	     :raydiagonal ([QB] k .e4)
	     :shift
	     )
	    )

;;        Look in the variations, since otherwise you will not 
;;        find any ideal mates with a bishop.

	   :variations
	   )
 )

Mirror mate - King field is unoccupied

mirrormate.cql can be downloaded here.

;; Find all positions with mirror mate in the main line.
(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :attackcount k . 8
  :mate
 )
)

Nowtony - Piece moves to square where rook and bishop mutually interfere

nowotny.cql can be downloaded here.

;search for nowotny theme with rook and bishop interference:
 
;; white moves to a square that is, on the next move, moved to either 
;;by a Black rook or a Black bishop; 
;; do the same with white and black interchanged

;; We do not check that the Nowotny is "thematic" in the sense 
;;that the rook and the bishop
;; actually interfere thematically with one another. For example, 
;;this will include positions in which
;; the "nowotny" occurs on the edge of the board.

(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :moveto .b2

;; The following line, if uncommented, filters out edge
;; Nowotny's which are not true Nowotny's from occurring on the edge of the board.

;  ?b1 ?a1 ?a2 ?a3 ?b3 ?c3 ?c2 ?c1 

  :wtm
  :and
  ((position :sequence ((position :moveto .b2) (position :movefrom b :moveto Ab2)))
   (position :sequence ((position :moveto .b2) (position :movefrom r :moveto Ab2)))
   )
  :shift
  :markall
  :variations
  :flipcolor
)
)

Pinned mate

pinmate.cql can be downloaded here.

;; Find positions in the main line in which Black is in mate and in which
;; a black piece is pinned.
(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :btm
  :mate
  :rayattack (A a k)))

Queen staircase

staircase.cql can be downloaded here.

;;This filter file looks for queen staircases
;;search for a queen staircase. 
;;
;;The "shift" and the "flip" commands are used to take the given sequence
;;and make sure it can be started from any square and any direction
;;
;;The "flipcolor" command is used to make sure that it works with black queens too.

;; Because there is no :variations keyword, this only searches for staircases in the
;; main line.

;; Because of the presence of both :shift and :flip commands, 
;;this query takes a long time.

(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :sequence
    ((position Qa2) 
     (position) 
     (position Qb2) 
     (position) 
     (position Qb3) 
     (position) 
     (position Qc3)
     (position)
     (position Qc4))
    :shift 
    :flip
    :flipcolor
))

Valladao theme Castling, en-passant, and underpromotion

valladao.cql can be downloaded here.

;The Valladao task: Find studies in which en-passant, promotion, and 
;;castling all occur in the main line.


(match
 :pgn heijden.pgn
 :output out.pgn
 (position :enpassant)
 (position
  :movefrom [Kk]e?
  :moveto .[c?,g?]
  )
 (position :promote [NnBbRr])
)

World Championship 7 theme Position recurs with a missing piece

wcct7.cql can be downloaded here.

;;This checks for the world championship theme of a single missing white piece occurring
(match
 :pgn heijden.pgn
 :output out.pgn
 :result 1-0
 (position
  :markall
  :relation (:missingpiececount A 1 10)
 )
)

World Championship 9 theme Quiet queen sacrifice

wcct9.cql can be downloaded here.

; This computes the theme for the 9th World Chess Composing Tournament: 
; "In a win study, the white queen moves, without capturing, checking or refuting
; a check, to a square where she is not guarded by Whtie and where she can immediately be 
; captured by Black", (http://www.sci.fi/~stniekat/pccc/9_wcct.htm).
; It works by searching for positions where the queen moves to an empty square, and in that
; position there is no attacking piece, called $attacker, which either (a) is not pinned, or
; (b) is the only other piece on a ray with endpoints the black king and the moved white Queen.
; That is, one wants to eliminate positions where the White 
;;queen moves to a square where its only
; attackers are pinned to the black king by a piece other than that White queen. 


(match 
 :pgn heijden.pgn 
 :output out.pgn
 :result 1-0
 :forany attacker [qrbnp]
 :forany Queen Q
 (position
 :markall
  :tagmatch Queen Q
  :movefrom $Queen
  :moveto .
  :nocheck
  :sequence
  ((position) 
   (position
    :attackcount $attacker $Queen 1
    :attackcount A $Queen 0 
    :or
    ((position :rayattack (A $attacker k) 0)
     (position :ray ($Queen $attacker k)))
    :nocheck)
   )
  )
 )

Look for a Dobrescu study with a repeated piece configuration

dobrescu.cql can be downloaded here.

; This searches for studies in which the configuration Ke6 be7 be4 nd5 occurs
; shifted or flipped, but in which no pieces from the original position
; are on the same square as the pattern 

(match
  :pgn heijden.pgn
  :output out.pgn
  (position
  Ke6 be7 be4 nd5
  :shift
  :flip
   :markall ; find each occurrence
  :relation (:pattern  ; only pay attention to the pieces we found
	     :shift ; allow shift
	     :flip ; allow flip
	     :originalsamecount 0 ; disregard identity matches
	     :samesidetomove ; same side to move
	     :variations ; look in variations
	     )
  )
)

5-piece mutual zuzgwangs

mzz5.cql can be downloaded here.

; This locates all mutual zugzwang studies with exactly 5 pieces and at most one pawn
; This may not get all "true" mutual zugwang position. Rather, it gets 
;;you all the studies
; in which same position occurs, both with black and with white to move, 
;;but the white to move position
; occurs in the variation and the black to move position occurs in the main line.
(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :piececount U 5 ;; 5 pieces
  :piececount [Pp] 0 1 ;; 0 or 1 pawns
  :btm 
  :relation ( 
	      :variationsonly
	      :changesidetomove)
 )
)

Pinned stalemates: Stalemate with at least two pinned pieces.

pinstalemate.cql can be downloaded here.

;; Find positions in the main line in which a side is in stalemate and in which
;; at least two of its pieces are pinned.
(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :btm
  :stalemate
  :flipcolor
  :rayattack (A a k) 2 100))

Passed pawn search - At least three white passed pawns

passers.cql can be downloaded here.

;;Find positions with at least 6 white passed pawns.
;;Another file must be created to find black passed pawns
(match :pgn mega.pgn :output out.pgn 
       (position 
	:sumrange acc0 6 8 
	:markall
	Pd2
	:piececount [pP][d3-7] 0
	:piececount p[c3-7,e3-7] 0
	:shift
	:accumulate acc0
	)
       )

Mixed passed pawns - At least three white passed pawns and at least two black passed pawns

passers2.cql can be downloaded here.

;;At least 3 white passed pawns and at least two black passed pawns in the same position
(match :pgn mega.pgn :output out.pgn
       (position 
	:sumrange whitecount 3 8 
	:sumrange blackcount 2 8
	:markall
	:and
	((position
	  Pd2	
	  :piececount [pP][d3-7] 0
	  :piececount p[c3-7,e3-7] 0
	  :shift
	  :accumulate whitecount
	  )
	 (position
	  pd7	
	  :piececount [pP][d2-6] 0
	  :piececount P[c2-6,e2-6] 0
	  :shift
	  :accumulate blackcount
	  )
	 )
	)
       )

Smothered Mate

smotheredmate.cql can be downloaded here.

;;This looks for black smothered mates on the edge or on the center of the board
(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :mate :btm
  :attackcount k a 5 8
  :attackcount k [A.] 0
 )
)

World championship 6 theme - White moves king to empty square instead of capturing a queen.

queencapturedelayed.cql can be downloaded here.

;; This is an example of the full WCCT 6 theme. 

;; It finds studies that contains a position
;; such that from that position White moves the King to an empty 
;;square rather than capturing a queen, which 
;; the King instead captures on the next move.

(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :wtm
  :moveto .
  qb2
  :attackcount a ab2 0
  :attackcount K ab2 1 
  :sequence ((position :movefrom K) 
             (position :attackcount a ab2 0) 
	     )
  :shift
  :markall)

)

Queen sac - Sacrifice of a queen, and remains at least 9 points down in material for at least 12 moves, and then wins.

queendown.cql can be downloaded here.

;;One side sacrifices a queen, and remains at least 9 points down in material 
;;for at least 12 moves, and then wins
(match
	:pgn mega.pgn
	:output out.pgn
	(position
	  :result 1-0
	  :sequence(
	     (position  :powerdifference [Qq] -1000 -9 :powerdifference U -1000 -9)
	     (position  :powerdifference [Qq] -1000 -9)
	     (position  :powerdifference [Qq] -1000 -9)
	     (position  :powerdifference [Qq] -1000 -9)
	     (position  :powerdifference [Qq] -1000 -9)
	     (position  :powerdifference [Qq] -1000 -9)
	     (position  :powerdifference [Qq] -1000 -9)
	     (position  :powerdifference [Qq] -1000 -9)
	     (position  :powerdifference [Qq] -1000 -9)
	     (position  :powerdifference [Qq] -1000 -9)
	     (position  :powerdifference [Qq] -1000 -9)
	     (position  :powerdifference [Qq] -1000 -9)
	)
	:flipcolor
))
	

Rambling rook The rook keeps checking itself to the king, which keeps avoiding its capture.

ramblingrook.cql can be downloaded here.

;;Multiple consecutive rook checks followed by king moving to empty square; 
;;usually to avoid stalemate if rook is captured.
(match
 :pgn heijden.pgn
 :output out.pgn
 :result 1/2
 (position
  :wtm
  :sequence(
	    (position :movefrom R :moveto .)
	    (position :movefrom k :check)
	    (position :movefrom R :moveto .)
	    (position :movefrom k :check)
	    (position :movefrom R :moveto .)
	    (position :movefrom k :check)
	    (position :movefrom R :moveto .)
	    (position :movefrom k :check)
	    (position :movefrom R :moveto .)
	    (position :movefrom k :check))))

			

ZwickmuhleTorre-Lasker theme

zwickmuhle.cql can be downloaded here.

;This looks for Zwickmuhle's with bishop and knight. Specifically, we
;are looking for positions where a minor piece moves to a square,
;giving check, and then later moves from the square, giving orthogonal
;check from a rook or queen, and then again from the square giving
;check. This illustrates the use of multiple positions inside a match
;list to help increase efficiency. Games not matching the first
;position will not have to run through the much more complex second
;position.

(match
 :pgn mega.pgn
 :gamenumber 1 3000
 :output out.pgn


 (position      :matchcount 2 1000 
		:flipcolor
		:noannotate
		:sequence
		((position :movefrom [BN] )
		 (position :check :movefrom k)
		 ))


 (position  
  :flipcolor
  :shift
  :and (
	(position :sequence
		  (
		   (position :movefrom [BN] :moveto ?c6)
		   (position :check :movefrom k)))
	(position  :gappedsequence
		   (
		    (position      :sequence
				   ((position :movefrom [BN]c6)
				    (position :check :rayorthogonal ([RQ] . k)  :movefrom k)
				    )
				   )

		    (position      :sequence
				   ((position :movefrom [BN] :moveto ?c6)
				    (position :check :movefrom k)
				    )
				   )

		    )
		   )
	)
  )
 )

Like-color bishops Two bishops of the same color and same square-color.

samecolorbishops.cql can be downloaded here.

; Search for games with that have either two light-squared white
;bishops, two dark-squared white bishops, two dark-square black
;bishops. or two light-square black bishops.

;There are two ways to do this. One is to simply have a giant :or with 4
;:piececount clauses in the or:

;(position
; :or(
;   (position
;     :piececount   B[a1,a3,a5,a7,b2,b4,b6,b8,c1,c3,c5,c7,d2,d4,d6,d8,e1,e3,e5,e7,f2,f4,f6,
;;                    f8,g1,g3,g5,g7,h2,h4,h6,h8] 2 9 )
;   (position 
;     :piececount   b[a1,a3,a5,a7,b2,b4,b6,b8,c1,c3,c5,c7,d2,d4,d6,d8,e1,e3,e5,e7,f2,f4,
;;                    f6,f8,g1,g3,g5,g7,h2,h4,h6,h8] 2 9)
;    (position
;     :piececount b[a2,a4,a6,a8,b1,b3,b5,b7,c2,c4,c6,c8,d1,d3,d5,d7,e2,e4,e6,e8,f1,f3,
;;                  f5,f7,g2,g4,g6,g8,h1,h3,h5,h7] 2 9)
;   (position
;     :piececount B[a2,a4,a6,a8,b1,b3,b5,b7,c2,c4,c6,c8,d1,d3,d5,d7,e2,e4,e6,e8,f1,f3,
;;                  f5,f7,g2,g4,g6,g8,h1,h3,h5,h7] 2 9)
;  )
;)

; But a more elegant way is to use the following code, which relies on
; the fact that flipping the board horizontally changes the color of the
; squares:

(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :piececount
  B[a1,a3,a5,a7,b2,b4,b6,b8,c1,c3,c5,c7,d2,d4,d6,d8,e1,e3,e5,e7,f2,f4,f6,f8,g1,g3,g5,g7,h2,h4,h6,h8]
  2 9
  :flipcolor
  :fliphorizontal
  :markall
  )
)

Chameleon stalemates, including two ways to ignore some pieces when searching for the echo:

via :missingpieceount and :newpiececount and via :pattern in the relation specifier.

echostalemate.cql can be downloaded here.

;Find games in which there are two 
;positions P1 and P2, not the same, 
; with the same side to move, such that P1 is a stalemate, and such that
; P2 is reached from P1 by a shift or a flip (or a shift and a flip)

(match
 :pgn heijden.pgn
 :output out.pgn
 (position
  :stalemate
  :relation (:shift  ; allow shifts
	:samesidetomove  ; same side to move in P1 and P2
	:originaldifferentcount 1 100 ; disallow identical positions
	:flip) ; allow flips
  :variations ; search in variations

 )
)

Excelsior A pawn starts on its second rank and promotes.

excelsior.cql can be downloaded here.

;The excelsior theme: a pawn starts at the second rank and then promotes
(match :pgn heijden.pgn 
	:output out.pgn
	:forany pawn [Pp]
       (position
	:initial
	:tagmatch pawn P?2
	:gappedsequence((position :movefrom $pawn :promote U))
	:flipcolor)
)

Double excelsior Both a white and black pawn have excelsiors; the white pawn underpromotes

doubleexcelsior.cql can be downloaded here.

;Both a white and a black excelsior occur, where the white pawn underpromotes (in the main line)

(match :pgn heijden.pgn 
	:output out.pgn
	:forany whitepawn P
	:forany blackpawn p
       (position :initial 
		 :tagmatch whitepawn P?2
		 :tagmatch blackpawn p?7
		 )
       (position :movefrom $whitepawn :promote [RBN])
       (position :movefrom $blackpawn :promote U)
)

The same rook visits each corner

rookcorners.cql can be downloaded here.

; Find all games in which the same rook visits all four corners of the board.

(match
 :pgn heijden.pgn
 :output out.pgn
 :forany rook [rR] ; loop over the possible rooks
 (position $rook[a1]) 
 (position $rook[h1])
 (position $rook[h8])
 (position $rook[a8])
 )

The same piece delivers at least 30 checks in the game

manychecks.cql can be downloaded here.

(match
 :pgn heijden.pgn
 :output out.pgn
 :forany checker A
 (position
   :btm
   :attackcount $checker k 1
   :matchcount 30 1000)
)

A rook traverses a rectangle in this case, a 4x3 rectangle.

rookrectangle.cql can be downloaded here.

;; look for positions where a rook circles in a 4x3 rectangle
;; we use the rectangle g2, d2, d6, g6 together with shift and flip
;; 
(match 
	;:pgn tmp.pgn 
	:pgn heijden.pgn
	:output out.pgn 
	:forany piece [Rr]
       (position
	$piece[g2]
        :and (
	      (position :gappedsequence ((position :movefrom $piece[g6,d2] :moveto ?g2)))
	      (position :gappedsequence ((position :movefrom $piece[d6,g2] :moveto ?d2)))
	      (position :gappedsequence ((position :movefrom $piece[d6,g2] :moveto ?g6)))
	      (position :gappedsequence ((position :movefrom $piece[g6,d2] :moveto ?d6)))
	      )
	:shift :flip
))



	

A knight chases another knight constantly trying to sacrifice itself.

knightchase.cql can be downloaded here.

;Search for positions in which a knight chases a  knight of another color, 
;;continually offering to sacrifice itself
(match
 :pgn heijden.pgn
 :output out.pgn
 :forany chaser [nN]  ; this is the chasing knight
 :forany chasee [Nn]  ; this is the chased knight
 (position 
  :tagmatch chaser n ; make sure the chaser is really a black knight, 
                     ;not a N or a pawn that will promote to one
  :tagmatch chasee N ; make sure the chasee is really a white knight, 
                     ; and not a n or a pawn that will promote to one
	   :sequence ((position :wtm :attackcount $chaser $chasee 1 :movefrom $chasee) 
		      (position :movefrom $chaser)
                      (position :attackcount $chaser $chasee 1))
	   :matchcount 4 100
	   :flipcolor
 ))

A knight visits at least twenty different squares.

This example shows how logical connectives, gapped sequences, transforms and tagged pieces can be combined in a search.

differentsquares.cql can be downloaded here.

; Find games in which some knight has visited at least twenty different squares.
; This works by counting positions such that the knight will not again visit
; the square on which it currently sits. Squares visited by a pawn that promotes to 
; a knight are not included in the count.
;

(match :pgn heijden.pgn
       :output out.pgn
       :forany knight [Nn] ; loop for each possible knight
       (position
	:tagmatch knight [Nn]d4 ; Tagged piece is actually a knight on d4, not a pawn
	:and         ; make sure this is the last such occurrence
	((position :not :gappedsequence 
		   ((position) (position $knight[d4]))))
	:shift       ; now repeat, with other squares instead of d4
	:matchcount 20 65 ; match when at least 20 distinct squares found
	:noannotate  ; do not print "MATCH" when a match is found
	))


ChessQuery Language Manual: http://www.rbnn.com/cql