1

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?

false
  • 10,264
  • 13
  • 101
  • 209
  • 1
    This depends on the interpreter. You can probably change this behaviour a bit with settings. – Tomas By Jun 21 '19 at 16:12
  • @TomasBy SWI Prolog –  Jun 21 '19 at 16:12
  • It would be odd (and sad) if this has any relevance for points on exam. – Tomas By Jun 21 '19 at 16:13
  • @TomasBy I retagged the question; however; I think I have already said in the question that it was SWI Prolog? –  Jun 21 '19 at 16:15
  • @TomasBy `Then I run SWI Prolog to see if I was correct.` –  Jun 21 '19 at 16:16
  • Yes, that's fine. – Tomas By Jun 21 '19 at 16:17
  • I think by the way you are asking the question you are interpreting the word `form` incorrectly. If I were taking this test I would interpret form to be that the the MGU values are given and that true or false or something similar is given. Based on your answer if I were the teacher I would mark it as correct. Just because a specific interpreter gives the answer in one format doesn't mean a different format is wrong. Take for example the number `1`. `1` represents 1, `0001` represents 1, `1.0` represents 1, etc. – Guy Coder 5 mins ago – Guy Coder Jun 21 '19 at 16:28
  • If you get "points off" for this kind of stuff, then your Prolog exam is not your biggest problem. Either way, you should go with this question to whoever is going to grade your exam. What does it matter what is mine or anyone else's _opinion_ on the topic? – User9213 Jun 21 '19 at 16:57
  • @User9213 I'm not asking for your or anyone else's opinion on the topic. I'm asking in what cases does SWI Prolog break lines when presenting its output, which is a hard fact and not an opinion. –  Jun 21 '19 at 17:00
  • So why did you bring up the whole exam thing at all? If your question is "how does SWI-Prolog print solutions on the top level", then, the fact that you have an exam is completely out of place. Either remove any mentions of an exam from your question; or, alternatively, accept the comments you are getting and don't act like a wise-ass. ;-) – User9213 Jun 21 '19 at 17:32
  • @User9213 Here you are - no mention about no exam here. That said, I honestly believed though that SO liked questions with context, for example to avoid the XY problem. –  Jun 21 '19 at 17:41
  • Yes, you are correct. But then you got answer to Y and this somehow made you unhappy.... – User9213 Jun 21 '19 at 18:07
  • @User9213 I know (because I could check) when does Prolog break lines *in this particular case*. However, I'm interested in knowing the general case - where *in general*, not *in this particular case*, does Prolog break lines. To this I have no answers. Since I cannot type all possible facts and queries to check all possible Prolog's answers. Is it inappropriate to ask for the general case, even if I know a particular case? –  Jun 21 '19 at 18:10
  • I honestly do not understand what exactly you are after at the moment. Not with your question, with the comments. – User9213 Jun 21 '19 at 18:33
  • 2
    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 Jun 21 '19 at 19:10

1 Answers1

2

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.

Jan Wielemaker
  • 1,670
  • 10
  • 18