Directives: Questions and Commands

KEY When Prolog is at top-level (signified by an initial prompt of "| ?- ", with continuation lines prompted with "| " - ie indented out from the left margin) it reads in terms and treats them as directives to the interpreter to try and satisfy some goals. These directives are called questions. Remember that Prolog terms must terminate with a full-stop ("."), and that therefore Prolog will not execute anything for you until you have typed the full-stop (and then <return>) at the end of the directive. Suppose list membership has been defined by: member(X,[X|_]). member(X,[_|L]) :- member(X,L). Note the use of anonymous variables written "_". If the goal(s) specified in a question can be satisfied, and if there are no variables as in this example: | ?- member(b,[a,b,c]). then the system answers yes and execution of the question terminates. If variables are included in the question, then the final value of each variable is displayed (except for anonymous variables). Thus the question | ?- member(X,[a,b,c]). would be answered by X = a redo? At this point the interpreter is waiting for you to indicate whether or not that solution is sufficient, or whether you want to backtrack to see if there are any further solutions. Simply typing <return> terminates the question, while typing "y" followed by <return> causes the system to backtrack looking for alternative solutions. If no further solutions can be found it outputs no The outcome of some questions is shown below, where a number preceded by "_" is a system-generated name for a variable. | ?- member(X,[tom,dick,harry]). X = tom redo? y X = dick redo? y X = harry redo? y no | ?- member(X,[a,b,f(Y,c)]),member(X,[f(b,Z),d]). Y = b, X = f(b,c), Z = c redo ? % Just <return> typed here yes | ?- member(X,[f(_),g]). X = f(_1728) redo? yes | ?- When Prolog reads terms from file (or from the terminal following a call to [user]), it treats them all as program clauses. In order to get the interpreter to execute directives from a file they must be preceded by '?-', for questions, or ':-', for commands. Commands are like questions except that they do not cause results or answers to be printed out. They always start with the symbol ":-". At top level this is simply written after the prompted "?-" which is then effectively overridden. Any required output must be programmed explicitly; e.g. the command: :- member(3,[1,2,3]), write(ok). directs the system to check whether 3 belongs to the list [1,2,3], and to output "ok" if so. Execution of a command terminates when all the goals in the command have been successfully executed. Other alternative solutions are not sought (one may imagine an implicit "cut" at the end of the command). If no solution can be found, the system gives: ? as a warning. The principle use for commands (as opposed to questions) is to allow files to contain directives which call various procedures, but for which you don't want to have the answers printed out and the "redo?" question asked. In such cases you only want to call the procedures for effect, ie you don't want terminal interaction in the middle of consulting the file. A useful example would be the use of a directive in a file which consults a whole list of other files, e.g. :-([ bits, bobs, mainpart, testcases, data, junk ]). (NB note that the extra parentheses, with the :- immediately next to them, are currently essential due to a problem with prefix operators (like :-) and lists. They are not required for commands that do not contain lists. This restriction will eventually be removed.) If this directive was contained in the file 'program' then typing the the following at top-level would be a quick way of loading your entire program: | ?- [program]. When simply interacting with the top-level of the Prolog interpreter this distinction between questions and commands is not normally very important. At top-level you should just type questions normally. In a file, if you wish to execute some goals then you should use a command. I.e. To execute a directive in a file it must be preceded by ":-", otherwise it will be treated as a clause.