Next: , Up: Queries and Directives


3.4.1 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:

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

Remember that Prolog terms must terminate with a full stop (‘.’, possibly followed by whitespace), 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, the system answers

     yes

and execution of the query terminates.

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

     | ?- memb(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:

<RET>
y
“accepts” the solution; the query is terminated and the development system responds with ‘yes’.


;
n
“rejects” the solution; the development system backtracks (see ref-sem) looking for alternative solutions. If no further solutions can be found it outputs ‘no’.


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

^
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 end of the subterm selector. With multiple arguments separated by whitespace, the arguments are applied from left to right.


h
?
lists available commands.

While the variable bindings are displayed, all variables occurring in the values are replaced by friendlier variable names. Such names come out as a sequence of letters and digits preceded by ‘_’. The outcome of some queries is shown below.

     | ?- memb(X, [tom,dick,harry]).
     
     X = tom ;
     X = dick ;
     X = harry ;
     
     no
     | ?- memb(X, [a,b,f(Y,c)]), memb(X, [f(b,Z),d]).
     
     X = f(b,c),
     Y = b,
     Z = c
     
     | ?- memb(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.

Send feedback on this subject.