Node:Queries, Next:, Up:Queries and Directives


The full syntax of a query is ?- followed by a sequence of goals. The top-level expects queries. This is signaled by the initial prompt | ?- . Thus a query at top-level looks like:

| ?- member(b, [a,b,c]).

Remember that Prolog terms must terminate with a full stop (., possibly followed by layout text), and that therefore Prolog will not execute anything until you have typed the full stop (and then <RET>) at the end of the query.

If the goal(s) specified in a query can be satisfied, and if there are no variables as in this example, then the system answers


and execution of the query terminates.

If variables are included in the query, then the final value of each variable is displayed (except for variables whose names begin with _). Thus the query

| ?- member(X, [a,b,c]).

would be answered by

X = a

At this point, the development system accepts one-letter commands corresponding to certain actions. To execute an action simply type the corresponding character (lower or upper case) followed by <RET>. The available commands in development systems are:

"accepts" the solution; the query is terminated and the development system responds with yes.
"rejects" the solution; the development system backtracks (see Procedural) looking for alternative solutions. If no further solutions can be found it outputs no.
invokes a recursive top-level.
In the top-level, a global printdepth is in effect for limiting the subterm nesting level when printing bindings The limit is initially 10.

This command, without arguments, resets the printdepth to 10. With an argument of n, the printdepth is set to n, treating 0 as infinity. This command works by changing the value of the toplevel_print_options Prolog flag (see State Info).

A local subterm selector, initially [], is maintained. The subterm selector provides a way of zooming in to some subterm of each binding. For example, the subterm selector [2,3] causes the 3rd subterm of the 2nd subterm of each binding to be selected.

This command, without arguments, resets the subterm selector to []. With an argument of 0, the last element of the subterm selector is removed. With an argument of n (> 0), n is added to the back of the subterm selector. With a list of arguments, the arguments are applied from left to right.

lists available commands.

While the variable bindings are displayed, all variables occurring in the values are replaced by terms of the form '$VAR'(N) to yield friendlier variable names. Such names come out as a sequence of letters and digits preceded by _. The outcome of some queries is shown below.

| ?- member(X, [tom,dick,harry]).

X = tom ;
X = dick ;
X = harry ;

| ?- member(X, [a,b,f(Y,c)]), member(X, [f(b,Z),d]).

X = f(b,c),
Y = b,
Z = c

| ?- member(X, [f(_),g]).

X = f(_A)

Directives are like queries except that:

  1. Variable bindings are not displayed if and when the directive succeeds.
  2. You are not given the chance to backtrack through other solutions.