prologprolog-toplevel

Why does Prolog predicate member/2 print an extra whitespace in output?


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!


Solution

  • 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.