Chess In Lisp: Early sample output

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Chess In Lisp: Early sample output

Post by sje »

One of the nice things about Common Lisp is that just about every data structure that can be defined can also be written in explicit text format and then later read back correctly. This can be of great help when debugging.

Here's some sample output of the CIL structure for the full internal data representation for a position:

Code: Select all

> (setf pos (create-initial-array-pos))
#S(POS
   :POSENV
   #S(POSENV :ACT-COLOR 0 :PAS-COLOR 1 :CASTBITS 15 :EP-SQ NIL :HMVC 0 :FMVN 1
      :KING-SQ-VEC #(4 60)
      :MAIN-HASH
      #*1110101011110010000100011111100001100000000111100100110011011101
      :PAWN-HASH
      #*0000000000001010111100100101100001011100100010001010011100001101
      :PINNED-SQS NIL :FROZEN-SQS NIL)
   :POSTAB
   #S(POSTAB
      :BBDB
      #S(BBDB
         :LOC-MERGE-BB
         #*1111111111111111000000000000000000000000000000001111111111111111
         :LOC-SWEEP-BB
         #*1011010100000000000000000000000000000000000000000000000010110101
         :LOC-COLOR-BB-VEC
         #(#*1111111111111111000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000001111111111111111)
         :LOC-MAN-BB-VEC
         #(#*0000000011111111000000000000000000000000000000000000000000000000
           #*0100001000000000000000000000000000000000000000000000000000000000
           #*0010010000000000000000000000000000000000000000000000000000000000
           #*1000000100000000000000000000000000000000000000000000000000000000
           #*0001000000000000000000000000000000000000000000000000000000000000
           #*0000100000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000001111111100000000
           #*0000000000000000000000000000000000000000000000000000000001000010
           #*0000000000000000000000000000000000000000000000000000000000100100
           #*0000000000000000000000000000000000000000000000000000000010000001
           #*0000000000000000000000000000000000000000000000000000000000010000
           #*0000000000000000000000000000000000000000000000000000000000001000)
         :ATK-BY-COLOR-BB-VEC
         #(#*0111111011111111111111110000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000111111111111111101111110)
         :ATK-FR-SQ-BB-VEC
         #(#*0100000010000000000000000000000000000000000000000000000000000000
           #*0000000000010000101000000000000000000000000000000000000000000000
           #*0000000001010000000000000000000000000000000000000000000000000000
           #*0010100000111000000000000000000000000000000000000000000000000000
           #*0001010000011100000000000000000000000000000000000000000000000000
           #*0000000000001010000000000000000000000000000000000000000000000000
           #*0000000000001000000001010000000000000000000000000000000000000000
           #*0000001000000001000000000000000000000000000000000000000000000000
           #*0000000000000000010000000000000000000000000000000000000000000000
           #*0000000000000000101000000000000000000000000000000000000000000000
           #*0000000000000000010100000000000000000000000000000000000000000000
           #*0000000000000000001010000000000000000000000000000000000000000000
           #*0000000000000000000101000000000000000000000000000000000000000000
           #*0000000000000000000010100000000000000000000000000000000000000000
           #*0000000000000000000001010000000000000000000000000000000000000000
           #*0000000000000000000000100000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000010000000000000000000000
           #*0000000000000000000000000000000000000000101000000000000000000000
           #*0000000000000000000000000000000000000000010100000000000000000000
           #*0000000000000000000000000000000000000000001010000000000000000000
           #*0000000000000000000000000000000000000000000101000000000000000000
           #*0000000000000000000000000000000000000000000010100000000000000000
           #*0000000000000000000000000000000000000000000001010000000000000000
           #*0000000000000000000000000000000000000000000000100000000000000000
           #*0000000000000000000000000000000000000000000000001000000001000000
           #*0000000000000000000000000000000000000000101000000001000000000000
           #*0000000000000000000000000000000000000000000000000101000000000000
           #*0000000000000000000000000000000000000000000000000011100000101000
           #*0000000000000000000000000000000000000000000000000001110000010100
           #*0000000000000000000000000000000000000000000000000000101000000000
           #*0000000000000000000000000000000000000000000001010000100000000000
           #*0000000000000000000000000000000000000000000000000000000100000010)
         :ATK-TO-SQ-BB-VEC
         #(#*0000000000000000000000000000000000000000000000000000000000000000
           #*1000000000000000000000000000000000000000000000000000000000000000
           #*0001000000000000000000000000000000000000000000000000000000000000
           #*0000100000000000000000000000000000000000000000000000000000000000
           #*0001000000000000000000000000000000000000000000000000000000000000
           #*0000100000000000000000000000000000000000000000000000000000000000
           #*0000000100000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*1000000000000000000000000000000000000000000000000000000000000000
           #*0010000000000000000000000000000000000000000000000000000000000000
           #*0001000000000000000000000000000000000000000000000000000000000000
           #*0111100000000000000000000000000000000000000000000000000000000000
           #*0001111000000000000000000000000000000000000000000000000000000000
           #*0000100000000000000000000000000000000000000000000000000000000000
           #*0000010000000000000000000000000000000000000000000000000000000000
           #*0000000100000000000000000000000000000000000000000000000000000000
           #*0100000001000000000000000000000000000000000000000000000000000000
           #*0000000010100000000000000000000000000000000000000000000000000000
           #*0100000001010000000000000000000000000000000000000000000000000000
           #*0000000000101000000000000000000000000000000000000000000000000000
           #*0000000000010100000000000000000000000000000000000000000000000000
           #*0000001000001010000000000000000000000000000000000000000000000000
           #*0000000000000101000000000000000000000000000000000000000000000000
           #*0000001000000010000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000100000001000000
           #*0000000000000000000000000000000000000000000000001010000000000000
           #*0000000000000000000000000000000000000000000000000101000001000000
           #*0000000000000000000000000000000000000000000000000010100000000000
           #*0000000000000000000000000000000000000000000000000001010000000000
           #*0000000000000000000000000000000000000000000000000000101000000010
           #*0000000000000000000000000000000000000000000000000000010100000000
           #*0000000000000000000000000000000000000000000000000000001000000010
           #*0000000000000000000000000000000000000000000000000000000010000000
           #*0000000000000000000000000000000000000000000000000000000000100000
           #*0000000000000000000000000000000000000000000000000000000000010000
           #*0000000000000000000000000000000000000000000000000000000001111000
           #*0000000000000000000000000000000000000000000000000000000000011110
           #*0000000000000000000000000000000000000000000000000000000000001000
           #*0000000000000000000000000000000000000000000000000000000000000100
           #*0000000000000000000000000000000000000000000000000000000000000001
           #*0000000000000000000000000000000000000000000000000000000000000000
           #*0000000000000000000000000000000000000000000000000000000010000000
           #*0000000000000000000000000000000000000000000000000000000000010000
           #*0000000000000000000000000000000000000000000000000000000000001000
           #*0000000000000000000000000000000000000000000000000000000000010000
           #*0000000000000000000000000000000000000000000000000000000000001000
           #*0000000000000000000000000000000000000000000000000000000000000001
           #*0000000000000000000000000000000000000000000000000000000000000000))
      :BOARD-VEC
      #(3 1 2 4 5 2 1 3 0 0 0 0 0 0 0 0 12 12 12 12 12 12 12 12 12 12 12 12 12
        12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 6 6 6 6 6 6 6
        6 9 7 8 10 11 8 7 9)
      :CENSUS
      #S(CENSUS :COLOR-COUNT-VEC #(16 16)
         :MAN-COUNT-VEC #(8 2 2 2 1 1 8 2 2 2 1 1))))
Dann Corbit
Posts: 12797
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Chess In Lisp: Early sample output

Post by Dann Corbit »

Is this chess engine going to be pure lisp, or will it also call C++ dynamic libraries? (I ask because of the thread title).
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Chess In Lisp: Early sample output

Post by sje »

Dann Corbit wrote:Is this chess engine going to be pure lisp, or will it also call C++ dynamic libraries? (I ask because of the thread title).
Neither; the idea is to publish a new and greatly improved version of the CIL exploratory toolkit. It will likely contain a simple searcher or two, just as the original CIL had a very primitive mate-in-N search example.

The new CIL toolkit is something like the old SAN Kit; it's something to get people interested in coding chess software. So it's certainly possible that someone will incorporate non Lisp functions via the appropriate hooks that vary across environments.

----

More sample code: marking SAN file/rank disambiguation:

Code: Select all

;;; SAN file/rank disambiguation marking helper routines

(defun calc-fr-man-to-sq-matches (my-move my-moves)
  "Filter a move list based on matching from-man/to-square matches."
  (let ((result nil) (fr-man (move-fr-man my-move)) (to-sq (move-to-sq my-move)))
    (dolist (move my-moves)
      (when (and (= (move-to-sq move) to-sq) (= (move-fr-man move) fr-man))
        (push move result)))
    result))

(defun count-fr-file-matches (my-move my-moves)
  "Count the number of moves that match the from file of the given move."
  (let ((result 0) (fr-file (map-sq-to-file (move-fr-sq my-move))))
    (dolist (move my-moves)
      (when (= (map-sq-to-file (move-fr-sq move)) fr-file)
        (incf result)))
    result))

(defun count-fr-rank-matches (my-move my-moves)
  "Count the number of moves that match the from rank of the given move."
  (let ((result 0) (fr-rank (map-sq-to-rank (move-fr-sq my-move))))
    (dolist (move my-moves)
      (when (= (map-sq-to-rank (move-fr-sq move)) fr-rank)
        (incf result)))
    result))


;;; SAN file/rank disambiguation marking main routine

(defun mark-file-rank-disambiguation-flags (my-moves)
  "Calculate/apply SAN file and rank disambiguation flags on a complete list of legal moves."
  (dolist (move my-moves)
    (let ((piece (svref mc-man-to-piece-vec (move-fr-man move))))
      (cond
        ((or (= piece piece-pawn) (= piece piece-king))
          nil)
        ((or
          (= piece piece-knight) (= piece piece-bishop)
          (= piece piece-rook  ) (= piece piece-queen ))
          (let ((matches (calc-fr-man-to-sq-matches move my-moves)))
            (when (> (length matches) 1)
              (let
                (
                  (file-match-count (count-fr-file-matches move matches))
                  (rank-match-count (count-fr-rank-matches move matches))
                )
                (when
                  (or
                    (> rank-match-count 1)
                    (and (= rank-match-count 1) (= file-match-count 1)))
                  (setf (move-mfbits move) (logior (move-mfbits move) mfbit-andf)))
                (when (> file-match-count 1)
                  (setf (move-mfbits move) (logior (move-mfbits move) mfbit-andr)))))))
        (t
          (error "cond fault: mark-file-rank-disambiguation-flags")))))
  my-moves)
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Chess In Lisp: Early sample output

Post by sje »

Dann Corbit wrote:Is this chess engine going to be pure lisp, or will it also call C++ dynamic libraries? (I ask because of the thread title).
The big problem with using non Lisp routines, libraries, or whatnot with Lisp is that there is no way of doing this in a portable fashion with a fast linkage. So one can't program a decent foreign function interface to do something like single instruction 64 bit scanning; either the hookup is too brittle for interplatform deployment or a portable version is too slow.

But sometimes a portable interface using well supported filesystem pipes is suitable. Indeed, this is my plan for having multiprocess support by using two or more Lisp interpreters communicating via pipe files.
smcracraft
Posts: 737
Joined: Wed Mar 08, 2006 8:08 pm
Location: Orange County California
Full name: Stuart Cracraft

Re: Chess In Lisp: Early sample output

Post by smcracraft »

Dann Corbit wrote:Is this chess engine going to be pure lisp, or will it also call C++ dynamic libraries? (I ask because of the thread title).
The problem of a strong full (IM/GM) chess program in pure Lisp is actually, as far as I know, unsolved.

At the very least, the hardware technology curve will make it happen...
unless no one ever writes it. :-)