Eight queens

Place eight queens on a chessboard so that none can attack another.

queens.js · output · proof · check · try it in the playground


The question

In chess, a queen attacks along its row, its column and both diagonals. It is a classic puzzle to place eight queens on an 8×8 board so that no two attack each other; finding one answer by hand takes patience.

Can you place eight queens on an 8×8 board so that no two attack each other?


Historical background

Max Bezzel posed the eight-queens puzzle in the German chess journal Schachzeitung in 1848. The question generalizes to an n-by-n board. The rules in this example use the board size as an input and place queens one row at a time. Schachzeitung, 1848 volume


How the board is written

One queen per row, so a placement is just a list of eight column numbers. The first number is the column of the queen in row 1, the second that of the queen in row 2, and so on, both counted from 1.

Because each column is used exactly once, rows and columns are safe automatically. Only the diagonals still need checking: two queens that are d rows apart are on the same diagonal when their columns differ by exactly d.


What we tell eyemind

implied_by(
  queens(N, Columns),
  [is_int(N), ge(N, 0), columns(1, N, Available), place(Available, [], Columns)],
)
// columns(1, N, Available) lists the columns 1 to N.
implied_by(columns(I, N, []), gt(I, N))
implied_by(columns(I, N, [I, ...Rest]), [le(I, N), is(Next, add(I, 1)), columns(Next, N, Rest)])
// select(X, List, Rest) picks one element X of List, leaving Rest.
fact(select(X, [X, ...Rest], Rest))
implied_by(select(X, [Y, ...Rest], [Y, ...Remaining]), select(X, Rest, Remaining))
// Each row takes a column not used yet, so columns never repeat; safe checks
// the diagonals against the queens placed so far, nearest row first.
fact(place([], _, []))
implied_by(
  place(Available, Placed, [Column, ...Rest]),
  [
    select(Column, Available, Remaining),
    safe(Column, Placed, 1),
    place(Remaining, [Column, ...Placed], Rest),
  ],
)
// A queen Distance rows away attacks along a diagonal when the columns
// differ by exactly Distance.
fact(safe(_, [], _))
implied_by(
  safe(Column, [Other, ...Rest], Distance),
  [
    ne(Column, add(Other, Distance)),
    ne(Column, sub(Other, Distance)),
    is(Next, add(Distance, 1)),
    safe(Column, Rest, Next),
  ],
)
query(once(queens(8, Columns)))

What eyemind concludes

// Answers: each instance of a query() that holds.
once(queens(8, [1, 5, 8, 6, 3, 7, 2, 4]))
Q . . . . . . .
. . . . Q . . .
. . . . . . . Q
. . . . . Q . .
. . Q . . . . .
. . . . . . Q .
. Q . . . . . .
. . . Q . . . .

The queen of row 1 is in column 1, that of row 2 in column 5, and so on. No two share a row, a column or a diagonal.


The proof, line by line

eyemind --proof writes down how it reached the answer. The proof records the search of the question one call at a time: it holds the claim and 13,886 call tables; see the full proof. Here are its claim and a few of its tables:

// Answers: each instance of a query() that holds.
once(queens(8, [1, 5, 8, 6, 3, 7, 2, 4]))

// Certificate: the derived facts and every answer of each call the questions make.
// … the table of the question, called(queens(8, A), [...]), with all 92 solutions
called(columns(1, 8, B), [columns(1, 8, [1, 2, 3, 4, 5, 6, 7, 8])])
called(columns(2, 8, C), [columns(2, 8, [2, 3, 4, 5, 6, 7, 8])])
called(columns(3, 8, D), [columns(3, 8, [3, 4, 5, 6, 7, 8])])
called(columns(4, 8, E), [columns(4, 8, [4, 5, 6, 7, 8])])
called(columns(5, 8, F), [columns(5, 8, [5, 6, 7, 8])])
called(columns(6, 8, G), [columns(6, 8, [6, 7, 8])])
called(columns(7, 8, H), [columns(7, 8, [7, 8])])
called(columns(8, 8, I), [columns(8, 8, [8])])
called(columns(9, 8, J), [columns(9, 8, [])])
// … the tables of place and select
called(safe(1, [], 1), [safe(1, [], 1)])
called(safe(2, [1], 1), [])
called(safe(3, [1], 1), [safe(3, [1], 1)])
called(safe(3, [], 2), [safe(3, [], 2)])
// … and the rest of the 13,886 tables

It has two parts:

There are no steps: steps record the firings of forward rules, and this program has only backward rules and facts, so everything the answer rests on is a search, which the tables show.

The tables shown say:

The place and select tables record, for each partial placement, every column that can be chosen and every way the rest can be completed, dead ends included: a table with no answers says that branch fails.


Checked: what the report says

A proof is only worth something if someone else can check it. eyemind -c generates the proof and checks it against the program; --check-proof checks a saved proof. The checker never reasons by itself: it reads the clauses from the program and confirms the proof against them in one procedure:

This is the complete report:

// Checked answers: these follow from the input program and are every answer of its questions.
once(queens(8, [1, 5, 8, 6, 3, 7, 2, 4]))

verdict('checked')

The verdict is checked: the placement really is a solution, and it is the answer the question asks for, the first solution in the program’s search order. Checking it confirms the whole search, one call at a time, which is why the proof records every branch, not just the winning one.


Try it

eyemind examples/queens.js
eyemind --goal "queens(4, Columns)" examples/queens.js

The 4×4 board has two solutions, [2, 4, 1, 3] and [3, 1, 4, 2]. Without once, --goal "queens(8, C)" lists all 92 solutions for the 8×8 board, and queens(6, C) finds 4.

Or open it in the playground.


Takeaway

Search can be long and messy, but its result need not be. eyemind hands back a certificate for the answer it found, every call of the search with all its answers, and anyone can check it one call at a time without a solver.