A source file is a sequence of clauses, each a term that ends with the end token. When the system loads the file it reads the terms one after another, and for each it does what its form says: adds a clause to the database, translates a grammar rule, or runs a directive. A set of clauses with the same name and arity is a predicate; parent/2 is the predicate parent with two arguments.
Forms of clause
| Form | Written as | Meaning |
|---|---|---|
| fact | Head. | the head is true |
| rule | Head :- Body. | the head is true if the body is; the body is goals joined by , (and), ; (or), -> (if-then) and \+ (not) |
| grammar rule | Head --> Body. | a rule over lists, translated to a rule with two more arguments |
| single-sided rule | Head => Body. | SWI-Prolog 9: the head is matched without binding the call, and the first matching rule commits; Head, Guard => Body adds a guard |
| directive | :- Goal. | run Goal when the file loads |
| query | ?- Goal. | run Goal like a question; allowed in a file by some systems, which print the answer |
The head is an atom or a compound term. A variable or a number as the head is an error, and the viewer lists it. The head of a clause may be qualified with a module: lists:append(...) :- ... adds a clause to a predicate in another module.
parent(tom, bob).
parent(bob, ann).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).
classify(N, small) :- N < 10, !.
classify(_, large).
max_of(X, Y, M) :- ( X >= Y -> M = X ; M = Y ).
Grammar rules (DCG)
A definite clause grammar rule a --> b, c. describes a list as a sequence of parts. The system translates it into a clause whose head has two more arguments, the list before and the list after. A body may hold non-terminals (name), lists of terminals ([hello]), strings where the flag allows it ("hello"), goals in braces ({ X > 0 }, which run without reading the list), the cut !, alternatives with ; and |, the negation \+, and calls with call//N. A pushback is written head, [x] --> body. The arity in the Outline of the viewer is shown as name//N, for the non-terminal that has N visible arguments.
greeting --> [hello], name.
name --> [world].
name --> [prolog].
digits([D|T]) --> digit(D), digits(T).
digits([D]) --> digit(D).
digit(D) --> [D], { code_type(D, digit) }.
The first rule becomes, in effect, greeting(S0, S) :- S0 = [hello|S1], name(S1, S).; the exact translation (the way a terminal in the head is moved into the body, and the use of call//N) is fixed by the standard draft and differs a little between systems.
Directives
A directive :- Goal. runs Goal at the moment it is read; its effect is on the rest of the load. If the goal fails or raises an error the system prints a warning and carries on. These directives are the ones this site lists in the Outline.
| Directive | Effect |
|---|---|
module(Name, Exports) | the file is a module; Exports lists name/arity items and op(P, T, N) terms |
use_module(File), use_module(File, Imports) | load a module and import its exports (see Modules and libraries) |
ensure_loaded(File), consult(File), [File], include(File), load_files(Files, Options) | load another file, once, again, or as if its text were here (include) |
op(Priority, Type, Names) | declare operators |
set_prolog_flag(Flag, Value) | change a flag such as double_quotes |
dynamic Specs | the predicates can be changed while the program runs |
discontiguous Specs | clauses of the predicates may be apart; without it, separated clauses give a warning |
multifile Specs | clauses may come from several files |
table Specs | tabling (SWI-Prolog, XSB, YAP, B-Prolog and others): results are remembered, which makes left recursion finish |
meta_predicate Specs, module_transparent, public, thread_local, volatile | declarations about how goals are passed, about visibility and about threads; system specific |
initialization(Goal), initialization(Goal, When) | run Goal after the file is loaded (ISO), or at another time: now, after_load, prepare_foreign, restore, program, main (SWI-Prolog) |
A specification is name/arity (or name//arity for a grammar rule), written alone or as a comma list or a list: :- dynamic counter/1, flag/2.
:- module(counter, [next/1, reset/0]).
:- use_module(library(lists)).
:- dynamic count/1.
:- discontiguous log/1.
:- initialization(reset).
count(0).
next(N) :- retract(count(N0)), N is N0 + 1, assertz(count(N)).
reset :- retractall(count(_)), assertz(count(0)).
Conditional compilation and queries
ISO Cor.2 and several systems read :- if(Goal)., :- elif(Goal)., :- else. and :- endif. as directives that keep or skip the clauses between them, by running Goal at load time. A skipped part still has to be read as terms by most systems, so it must be valid text. This site does not run the goal, so it reads every branch as code.
:- if(current_prolog_flag(bounded, false)).
big(N) :- N > 1000000000000.
:- else.
big(_) :- fail.
:- endif.
A query ?- Goal. asks the system to find a proof of Goal. In a file it is rare; it appears in tutorials and in the text a program prints.
What the viewer checks for each clause
- Brackets: a closer with no opener, a closer that does not match, an opener that is not closed before the end token.
- Singleton variables: a name that occurs once in a clause and does not start with
_is listed as a note. Directives and queries are not checked. - Discontiguous clauses: a predicate whose clauses are split by clauses of another predicate, and that is not declared
discontiguousordynamic, is listed as a note with the line of its first clause. - The head: a variable, a number or a string as a head is an error; a head that does not read as a term is a note.
Not described
Known, not yet covered
- Reference: the exact DCG translation of each body form (terminals after a cut, pushback with a list,
call//N) and the differences between systems are not written out. - Reference: the directives specific to one system (SICStus
block,modeanduse_moduleoptions, SWI-Prologlicense,predicate_optionsandset_stream, XSBimportandexport, ECLiPSeexport,libandmode) are not listed. - Viewer: the clause bodies are not read as terms; the declarations are listed from their text, and
:- ifbranches are not evaluated. - Viewer: a clause is judged only by its own tokens: the viewer does not know which predicates exist in other files, so it cannot say that a call is to an undefined predicate.