The eight queens puzzle is the problem of putting eight chess queens on an 8×8 chessboard such that none of them is able to capture any other using the standard chess queen's moves. The colour of the queens is meaningless in this puzzle, and any queen is assumed to be able to attack any other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general n queens puzzle of placing n queens on an n×n chessboard.

sel(X, [X|Y], Y).

sel(U, [X|Y], [X|V]) :- sel(U,Y,V).

safe([ ]).

safe([X|Y]) :- check(X,Y), safe(Y).

check(_,[ ]).

check(P, [Q|R]) :- 
    not_on_diag(P,Q), check(P,R).

not_on_diag(p(X1,Y1),p(X2,Y2)) :-

    DX is X1-X2, DY is Y1-Y2, 
    MDY is Y2-Y1, DX=\=DY, DX=\=MDY.

queens(Rows, [Col|RestCols], Points):-

    sel(Row,Rows,RestRows),
    safe([p(Row,Col) | Points]),
    queens(RestRows,RestCols,
        [p(Row,Col) | Points]).

queens( [ ], [ ], Points) :-
    print('Solution: '),print(Points),nl.

?- queens([1,2,3,4,5,6,7,8],
[1,2,3,4,5,6,7,8], []), fail.