%% n-queens problem %% Domain predicate "d" specifies the number of queens and the dimensions %% of the board. The main predicate is the predicate "q". Atom "q(X,Y)" %% represents the fact that there is a queen in the position (X,Y). % Domain predicate: the dimension of the board. d(1..n). % Exactly one queen in each row 1 { q(X,Y):d(Y) } 1 :- d(X). % Exactly one queen in each column 1 { q(X,Y):d(X) } 1 :- d(Y). % No two queens on an diagonal :- d(X;Y;X1;Y1), q(X,Y), q(X1,Y1), X < X1, Y != Y1, abs(X - X1) == abs(Y - Y1).