Different results in swi-prolog and yap
Asked Answered
P

1

7

The sample program enumerates and counts the number of 8-queen solutions. (sorry if the code is hard to read; this is machine-generated from an S-expression. The original code is https://www.cpp.edu/~jrfisher/www/prolog_tutorial/2_11.html)

rules:

[user].
(perm([X|Y],Z) :- (perm(Y,W),takeout(X,Z,W))).
perm([],[]).
takeout(X,[X|R],R).
(takeout(X,[F|R],[F|S]) :- (takeout(X,R,S))).
(solve(P) :- (perm([1,2,3,4,5,6,7,8],P),combine([1,2,3,4,5,6,7,8],P,S,D),alldiff(S),alldiff(D))).
(combine([X1|X],[Y1|Y],[S1|S],[D1|D]) :- (is(S1,+(X1,Y1)),is(D1,-(X1,Y1)),combine(X,Y,S,D))).
combine([],[],[],[]).
(alldiff([X|Y]) :- (\+ member(X,Y),alldiff(Y))).
alldiff([X]).
end_of_file.

query:

(setof(P,solve(P),Set),length(Set,L),write(L),write('\n'),fail).

swipl returns 92; while yap returns 40320. Also, when I query solve(P), swipl only returns two solutions (which also contradicts 92); yap returns much more (possibly 40320 of them). So why the difference? Is there such a serious compatibility issue?

Versions:

  • YAP 6.2.2 (x86_64-linux): Sat Sep 17 13:59:03 UTC 2016
  • SWI-Prolog version 7.2.3 for amd64
Phosphate answered 10/11, 2017 at 13:13 Comment(6)
Could you still put the code into a readable form?Idiot
there is the original one in the linkPhosphate
Next time, please to include here the readable version. That's the way SO works.Idiot
I was afraid since the original code does not belong to me, and the license was unclear.Phosphate
I don't think the license applies to the formatting. It applies to the content.Avatar
SWI Provided 92 solutions when I tried it. You must have done something wrong in that case.Avatar
I
8

In older versions of YAP, a query for an undefined predicate simply failed. In the case above, it is member/2 which is not defined in YAP. And for this reason your test alldif/1 always succeeds - thus the large number you get.

The behavior for this is governed by the Prolog flag unknown whose default value should be error. In YAP 6.2 the default was (incorrectly) fail. This was corrected in 6.3. Say

:- set_prolog_flag(unknown, error).

to get a clean error for undefined predicates. Then, you would need to define member/2.

Idiot answered 10/11, 2017 at 13:29 Comment(4)
thank you, so member is not the built-ins... but those "prologues" (compatibility layer, sort of) sounds good. I can make the lisp-to-prolog library to load it whenever the process is launched.Phosphate
hmm, by the way, just cloned yap 6.3.3, after the .configure it doesn't build because of make: *** No rule to make target 'H/dglobals.h', needed by 'yap'. Stop. ...Phosphate
Wondering why the standardization is going so bad compared to the other CSP community. Minizinc is a common format in CSP and is doing the competition annually. SAT has DIMACS and SMT has one as well.Phosphate
@Asai: The transition from 6.2->6.3 is rather a case of confluence.Idiot

© 2022 - 2024 — McMap. All rights reserved.