1
votes

Consider the following example program in Prolog:

p(0).
p(1).

b1(T) :-
   T = tri(X, Y, Z), p(X), p(Y), c(T), !, p(Z).

c(tri(X, X, _Z)).

SWI Prolog gives some interesting answers for certain queries:\

?- b1(tri(0, Y, Z)).
Y = Z, Z = 0 ;
Y = 0,
Z = 1.

Note that it did break the line in the second answer, but not in the first one.

This makes me wonder, what are the exact rules of line-breaking? When does SWI Prolog break the lines and when does it not? What does this depend on?

1
This depends on the interpreter. You can probably change this behaviour a bit with settings. - Tomas By
@TomasBy SWI Prolog - user4385532
It would be odd (and sad) if this has any relevance for points on exam. - Tomas By
@TomasBy I retagged the question; however; I think I have already said in the question that it was SWI Prolog? - user4385532
The exact algorithm SWI uses is not known to me, but I know in this case the reason why the first two variables are on the first line is because they have the same value. SWI wants you to see that and think "Y = Z = 0." The rest are on separate lines because they do not have the same value. Of course, when you ask for another solution, it must begin new lines. There is of course no difference between these variations (your answer is not more or less correct than SWIs, or vice versa) and I think this is a special feature of SWI (GNU does not do this, I don't think). - Daniel Lyons

1 Answers

2
votes

All bindings (Var = Value) appear on their own line, except when two or more variables are bound to the same value. In that case it uses the following syntax on a single line.

V1 = V2, V2 = V3, ..., Vn-1 = Vn, Vn = value.

It does this because it is valuable to know two variables have the same value. The answer in SWI-Prolog is printed as a valid Prolog program. There are no further promises and the layout, ordering, etc. may change without notice between versions. If you want a machine to read results, do not use the toplevel.