3

I am trying to run following code from N-Queens Problem‍​..How far can we go? to find solutions to n-queens problem:

generate([],_).
generate([H|T],N) :- H in 1..N , generate(T,N).
lenlist(L,N) :- lenlist(L,0,N).
lenlist([],N,N).
lenlist([_|T],P,N) :- P1 is P+1 , lenlist(T,P1,N).

queens(N,L) :-
        generate(L,N),lenlist(L,N),
        safe(L),!,
        labeling([ffc],L).

notattack(X,Xs) :- notattack(X,Xs,1).
notattack(X,[],N).
notattack(X,[Y|Ys],N) :- X #\= Y,
                         X #\= Y - N,
                         X #\= Y + N,
                         N1 is N + 1,
                         notattack(X,Ys,N1).

safe([]).
safe([F|T]) :- notattack(F,T), safe(T).

I have swi-prolog installed on Debian-9 (stable) Linux and I am running above using command "swipl -f nqueens.pl". On loading, I get an error:

Syntax error: operator expected (probably on 2nd code line)

Where is the problem and how can this be solved? Thanks for your help.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
rnso
  • 23,686
  • 25
  • 112
  • 234

2 Answers2

2

The question actually mentions that it is writting in CLPFD (A Constraint Logic Programming tool over Finite Domains). You have to import this library:

:- use_module(library(clpfd)).

generate([],_).
generate([H|T],N) :- H in 1..N , generate(T,N).
lenlist(L,N) :- lenlist(L,0,N).
lenlist([],N,N).
lenlist([_|T],P,N) :- P1 is P+1 , lenlist(T,P1,N).

queens(N,L) :-
        generate(L,N),lenlist(L,N),
        safe(L),!,
        labeling([ffc],L).

notattack(X,Xs) :- notattack(X,Xs,1).
notattack(X,[],N).
notattack(X,[Y|Ys],N) :- X #\= Y,
                         X #\= Y - N,
                         X #\= Y + N,
                         N1 is N + 1,
                         notattack(X,Ys,N1).

safe([]).
safe([F|T]) :- notattack(F,T), safe(T).

Then it works, and produces for instance:

?- queens(5,L).
L = [1, 3, 5, 2, 4] ;
L = [1, 4, 2, 5, 3] ;
L = [2, 4, 1, 3, 5] ;
L = [2, 5, 3, 1, 4] ;
L = [3, 1, 4, 2, 5] ;
L = [3, 5, 2, 4, 1] ;
L = [4, 1, 3, 5, 2] ;
L = [4, 2, 5, 3, 1] ;
L = [5, 2, 4, 1, 3] ;
L = [5, 3, 1, 4, 2].
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
0

When I look at line 2, as the error message suggests, the most probable cause is the H in 1..N. I would have written that as between(1, H, N). I haven't done anything in Prolog lately, though.

There are several different implementations of Prolog, and they differ in these little details. Try searching for guidelines for writing portable Prolog code.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • That error did disappear but same error also occurs on line "notattack(X,[Y|Ys],N) :- X #\= Y," which is still persisting. And from one line there is a warning: "Singleton variables: [X,N]". On running, there is error: "between/3: Arguments are not sufficiently instantiated" – rnso Sep 11 '17 at 08:49