1

I really don't understand why Prolog prints a single whitespace after the member/2 predicate only. The following is the text from my console.

?- member(1, [1, 2, 3]).
true .

?- string("why").
true.

This is really driving me crazy!

false
  • 10,264
  • 13
  • 101
  • 209
Jimbo
  • 444
  • 2
  • 7
  • 22
  • 2
    In the first case, `true` is a result and a prompt awaiting you to type something to tell Prolog what to do next. I assume the period you show is because you pressed `.`, but you could have pressed `;` or some other valid command it expects. In the second case, it's just a result with no input being requested. It's pretty clear. No need to panic. Just breathe... – lurker Nov 30 '17 at 15:33
  • Thank you very much for this explanation! I was writing a parser and I thought this could really mess with characters parsing, but fortunately this is not the case! – Jimbo Nov 30 '17 at 15:42
  • Yes, here you're just dealing with an artifact of the human interaction. The result you are seeing is not literally returned by the predicates when called in a program. They just visually indicate success or failure of the predicate call for the user. – lurker Nov 30 '17 at 15:50

1 Answers1

4

This is an intended feature to show that you have actually aborted a query at the toplevel. Let's consider a more explicit case where you initially get:

?- member(1,[1,1]).
   true█

Here, Prolog confirms that this is true but still awaits your response. If you ask for more with SPACE or ; you will get the next solution:

?- member(1,[1,1]).
   true
;  true.

However, if you hit Return or ., Prolog will abort your query. To show this on the screen SWI inserts an extra space:

?- member(1,[1,1]).
   true .

In Scryer and Trealla this is more explicit:

?- member(1,[1,1]).
   true
;  false.

Traditionally, many implementations never asked for alternatives upon ground queries — when the query itself did not contain any variable. This "optimization" often hid unexpected loops.

In your example, Prolog did not know whether or not a further answer is possible, so it prompts for the moment. If you then ask for more, you get:

?- member(1, [1, 2, 3]).
   true
;  false.

which says that there is no further solution.

false
  • 10,264
  • 13
  • 101
  • 209