We explain the Prolog query answer loop, which consists of a query answering session between the Prolog system and the end-user. The interpreter announces that it can accept queries by the Prolog prompt (?-):
The end-user can type in his query, terminate it by a period (.) and issue it with a carriage return. The syntax of a query is documented in the language reference manual in section 3.4. Let’s turn to our first example query. The query will display the text “Hello World!” and new line in the console:
?- write('Hello World!'), nl.
When the interpreter receives a query it starts searching for solutions. Any output produced by the query during its execution is immediately displayed. When the query finally succeeds the interpreter displays the variable bindings. When there were no variables in the query the interpreter displays “Yes”:
The interpreter now checks whether the query left some choice points. When no choice points are left the interpreter immediately returns to the Prolog prompt (?-). Let’s now turn to our second query. The second query will contain variables so that we will see how variable bindings will work:
?- X = 1; X = 2.
When the execution of the query succeeds for the first time the interpreter will show the variable bindings. Again the interpreter will check whether some choice points are left. Since in the case of this query some choice points are left, the interpreter will wait for further instructions from the end-user:
X = 1
The end-user can choose upon the following options:
EOF = Exit the current session.
; = Redo the query and possibly display more solutions.
= Don't redo the query no more solutions will be displayed.
? = Display this help text.
<Goal>. = Execute the <Goal> and prompt command again.
If the end-user chooses a goal, this goal will be executed and the end-user will be prompted again. The side effect of the goal can control the interpreter state or the goal can be used to query the interpreter state. Besides that the end-user can also use session predicates such as abort/0, break/0, etc.. or predicates he has defined on his own.
This is what happens when the end-user chooses the semicolon (;):
X = 1 ;
X = 2
The interpreter internally works with unnamed variables. Query variables are mapped to these unnamed variables. When displaying an answer the interpreter maps the unnamed variables back to the query variables. Anonymous variables which are an underscore only (“_”) are not shown to the end-user.