Previous: , Up: Queries and Directives   [Contents][Index]


3.4.2 Directives

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.
  3. Some directives are special in that they are not valid as queries.

Directives start with the symbol ‘:-’. Any required output must be programmed explicitly; e.g. the directive:

:- memb(3, [1,2,3]), write(ok).

asks the system to check whether 3 belongs to the list [1,2,3]. Execution of a directive terminates when all the goals in the directive have been successfully executed. Other alternative solutions are not sought. If no solution can be found, then the system prints:

* Goal - goal failed

as a warning.

The principal use for directives (as opposed to queries) is to allow files to contain directives that call various predicates, but for which you do not want to have the answers printed out. In such cases you only want to call the predicates for their effect, i.e. you do not want terminal interaction in the middle of loading the file. A useful example would be the use of a directive in a file that loads a whole list of other files, e.g.:

:- ensure_loaded([ bits, bobs, main, tests, data, junk ]).

(ensure_loaded/1 is yet another way that Prolog code can be loaded.)

If a directive like this were contained in the file myprog, then typing the following at top level would be a quick way of reading in your entire program:

| ?- [myprog].

When simply interacting with the top level, this distinction between queries and directives is not normally very important. At top level you should just type queries normally. In a file, queries are in fact treated as clauses, i.e. if you wish to execute some goals, then the directive in the file must be preceded by ‘:-’ or ‘?-’; otherwise, it would be treated as a clause.



Send feedback on this subject.