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



Queries

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

yes

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 system is waiting for input of either just a <RET> or else a ; followed by <RET>. Simply typing <RET> terminates the query; the system responds with yes. However, typing ; causes the system to backtrack (see Procedural) looking for alternative solutions. If no further solutions can be found it outputs no.

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 ;

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

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

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

X = f(_A)

yes
| ?-

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.