View source with formatted comments or as raw
    1nqueens(N,Q) :-
    2    hide_nqueens(N,N,[],Q).
    3
    4hide_nqueens(X,N,Qi,Qo) :-
    5    X>0,
    6    hide_pickqueen(X,Y,N),
    7    not(hide_attack(X,Y,Qi)),
    8    X1 is X-1,
    9    hide_nqueens(X1,N,[q(X,Y)|Qi],Qo).
   10hide_nqueens(0,_V1,Q,Q).
   11
   12hide_pickqueen(X,Y,Y) :-
   13    Y>0,
   14    q(X,Y).
   15hide_pickqueen(X,Y,N) :-
   16    N>1,
   17    N1 is N-1,
   18    hide_pickqueen(X,Y,N1).
   19
   20hide_attack(X,_V1,[q(X,_V2)|_V3]).
   21hide_attack(_V1,Y,[q(_V2,Y)|_V3]).
   22hide_attack(X,Y,[q(X2,Y2)|_V1]) :-
   23    Xd is X2-X,
   24    hide_abs(Xd,Xd2),
   25    Yd is Y2-Y,
   26    hide_abs(Yd,Yd2),
   27    Xd2=Yd2.
   28hide_attack(X,Y,[_V1|T]) :-
   29    hide_attack(X,Y,T).
   30
   31q(X,Y) :-
   32    not(hide_negq(X,Y)).
   33
   34hide_negq(X,Y) :-
   35    not(q(X,Y)).
   36
   37hide_abs(X,X) :-
   38    X>=0.
   39hide_abs(X,Y) :-
   40    X<0,
   41    Y is X* -1.
   42
   43not(_hide_negq1(X,Y)) :-
   44    q(X,Y).
   45
   46not(hide_negq(_X0,_X1)) :-
   47    not(_hide_negq1(_X0,_X1)).
   48
   49not(_hide_abs1(X,_X1)) :-
   50    _X1\=X.
   51not(_hide_abs1(X,_X1)) :-
   52    _X1=X,
   53    X<0.
   54
   55not(_hide_abs2(X,Y)) :-
   56    X>=0.
   57not(_hide_abs2(X,Y)) :-
   58    X<0,
   59    not(Y is X* -1).
   60
   61not(hide_abs(_X0,_X1)) :-
   62    not(_hide_abs1(_X0,_X1)),
   63    not(_hide_abs2(_X0,_X1)).
   64
   65not(_q1(X,Y)) :-
   66    hide_negq(X,Y).
   67
   68not(q(_X0,_X1)) :-
   69    not(_q1(_X0,_X1)).
   70
   71not(_hide_attack1(X,_V1,_Z0,_V2,_V3)) :-
   72    _Z0\=[q(X,_V2)|_V3].
   73
   74not(_hide_attack1(X,_V1,_Z0)) :-
   75    forall(_V2,forall(_V3,not(_hide_attack1(X,_V1,_Z0,_V2,_V3)))).
   76
   77not(_hide_attack2(_V1,Y,_Z0,_V2,_V3)) :-
   78    _Z0\=[q(_V2,Y)|_V3].
   79
   80not(_hide_attack2(_V1,Y,_Z0)) :-
   81    forall(_V2,forall(_V3,not(_hide_attack2(_V1,Y,_Z0,_V2,_V3)))).
   82
   83not(_hide_attack3(X,Y,_Z0,X2,Y2,_V1,Xd,Xd2,Yd,Yd2)) :-
   84    _Z0\=[q(X2,Y2)|_V1].
   85not(_hide_attack3(X,Y,_Z0,X2,Y2,_V1,Xd,Xd2,Yd,Yd2)) :-
   86    _Z0=[q(X2,Y2)|_V1],
   87    not(Xd is X2-X).
   88not(_hide_attack3(X,Y,_Z0,X2,Y2,_V1,Xd,Xd2,Yd,Yd2)) :-
   89    _Z0=[q(X2,Y2)|_V1],
   90    Xd is X2-X,
   91    not(hide_abs(Xd,Xd2)).
   92not(_hide_attack3(X,Y,_Z0,X2,Y2,_V1,Xd,Xd2,Yd,Yd2)) :-
   93    _Z0=[q(X2,Y2)|_V1],
   94    Xd is X2-X,
   95    hide_abs(Xd,Xd2),
   96    not(Yd is Y2-Y).
   97not(_hide_attack3(X,Y,_Z0,X2,Y2,_V1,Xd,Xd2,Yd,Yd2)) :-
   98    _Z0=[q(X2,Y2)|_V1],
   99    Xd is X2-X,
  100    hide_abs(Xd,Xd2),
  101    Yd is Y2-Y,
  102    not(hide_abs(Yd,Yd2)).
  103not(_hide_attack3(X,Y,_Z0,X2,Y2,_V1,Xd,Xd2,Yd,Yd2)) :-
  104    _Z0=[q(X2,Y2)|_V1],
  105    Xd is X2-X,
  106    hide_abs(Xd,Xd2),
  107    Yd is Y2-Y,
  108    hide_abs(Yd,Yd2),
  109    Xd2\=Yd2.
  110
  111not(_hide_attack3(X,Y,_Z0)) :-
  112    forall(X2,forall(Y2,forall(_V1,forall(Xd,forall(Xd2,forall(Yd,forall(Yd2,not(_hide_attack3(X,Y,_Z0,X2,Y2,_V1,Xd,Xd2,Yd,Yd2))))))))).
  113
  114not(_hide_attack4(X,Y,_Z0,_V1,T)) :-
  115    _Z0\=[_V1|T].
  116not(_hide_attack4(X,Y,_Z0,_V1,T)) :-
  117    _Z0=[_V1|T],
  118    not(hide_attack(X,Y,T)).
  119
  120not(_hide_attack4(X,Y,_Z0)) :-
  121    forall(_V1,forall(T,not(_hide_attack4(X,Y,_Z0,_V1,T)))).
  122
  123not(hide_attack(_X0,_X1,_X2)) :-
  124    not(_hide_attack1(_X0,_X1,_X2)),
  125    not(_hide_attack2(_X0,_X1,_X2)),
  126    not(_hide_attack3(_X0,_X1,_X2)),
  127    not(_hide_attack4(_X0,_X1,_X2)).
  128
  129not(_hide_pickqueen1(X,Y,_X2)) :-
  130    _X2\=Y.
  131not(_hide_pickqueen1(X,Y,_X2)) :-
  132    _X2=Y,
  133    Y=<0.
  134not(_hide_pickqueen1(X,Y,_X2)) :-
  135    _X2=Y,
  136    Y>0,
  137    not(q(X,Y)).
  138
  139not(_hide_pickqueen2(X,Y,N,N1)) :-
  140    N=<1.
  141not(_hide_pickqueen2(X,Y,N,N1)) :-
  142    N>1,
  143    not(N1 is N-1).
  144not(_hide_pickqueen2(X,Y,N,N1)) :-
  145    N>1,
  146    N1 is N-1,
  147    not(hide_pickqueen(X,Y,N1)).
  148
  149not(_hide_pickqueen2(X,Y,N)) :-
  150    forall(N1,not(_hide_pickqueen2(X,Y,N,N1))).
  151
  152not(hide_pickqueen(_X0,_X1,_X2)) :-
  153    not(_hide_pickqueen1(_X0,_X1,_X2)),
  154    not(_hide_pickqueen2(_X0,_X1,_X2)).
  155
  156not(_hide_nqueens1(X,N,Qi,Qo,Y,X1)) :-
  157    X=<0.
  158not(_hide_nqueens1(X,N,Qi,Qo,Y,X1)) :-
  159    X>0,
  160    not(hide_pickqueen(X,Y,N)).
  161not(_hide_nqueens1(X,N,Qi,Qo,Y,X1)) :-
  162    X>0,
  163    hide_pickqueen(X,Y,N),
  164    hide_attack(X,Y,Qi).
  165not(_hide_nqueens1(X,N,Qi,Qo,Y,X1)) :-
  166    X>0,
  167    hide_pickqueen(X,Y,N),
  168    not(hide_attack(X,Y,Qi)),
  169    not(X1 is X-1).
  170not(_hide_nqueens1(X,N,Qi,Qo,Y,X1)) :-
  171    X>0,
  172    hide_pickqueen(X,Y,N),
  173    not(hide_attack(X,Y,Qi)),
  174    X1 is X-1,
  175    not(hide_nqueens(X1,N,[q(X,Y)|Qi],Qo)).
  176
  177not(_hide_nqueens1(X,N,Qi,Qo)) :-
  178    forall(Y,forall(X1,not(_hide_nqueens1(X,N,Qi,Qo,Y,X1)))).
  179
  180not(_hide_nqueens2(_X0,_V1,Q,_X3)) :-
  181    _X0\=0.
  182not(_hide_nqueens2(_X0,_V1,Q,_X3)) :-
  183    _X0=0,
  184    _X3\=Q.
  185
  186not(hide_nqueens(_X0,_X1,_X2,_X3)) :-
  187    not(_hide_nqueens1(_X0,_X1,_X2,_X3)),
  188    not(_hide_nqueens2(_X0,_X1,_X2,_X3)).
  189
  190not(_nqueens1(N,Q)) :-
  191    not(hide_nqueens(N,N,[],Q)).
  192
  193not(nqueens(_X0,_X1)) :-
  194    not(_nqueens1(_X0,_X1)).
  195
  196not(_false).
  197
  198_nmr_check.
  199
  200NMR Check:
  201[_nmr_check]
  202
  203Query
  204[nqueens(4,Q)]