Everything in Prolog is a term: a program, its data and a query. A term is a variable, a number, an atom, a string, or a compound term: a name (the functor) with arguments, written name(arg1, arg2). The number of arguments is the arity; foo/2 names the functor foo with two arguments. A term that is written with an operator, such as 1 + 2, is the compound term +(1, 2).
Forms of terms
| Form | Written as | Notes |
|---|---|---|
| atom | foo, 'Hello world', [], {} | a name; [] is the empty list (a special constant in SWI-Prolog 7 and later) |
| compound term | point(1, 2) | no layout between the name and ( |
| list | [a, b, c], [H|T] | the compound term '.'(a, ...) in ISO, '[|]'(a, ...) in SWI-Prolog 7 and later |
| curly term | {a, b} | the term {}((a, b)); used by grammar rules and constraint systems |
| operator term | X is 1 + 2 | read with the operator table below |
| string | "text" | see the double_quotes flag below |
| dict | _{name: "x", age: 3} | SWI-Prolog 7 and later: a tag, then key-value pairs in braces, with no layout between the tag and { |
point(1, 2).
shape(circle(point(0, 0), 5)).
sum(X, Y, Z) :- Z is X + Y.
items([a, b|_]).
person(_{name: "Ann", age: 33}).
The operator table
An operator has a priority from 1 to 1200 (a lower number binds tighter) and a type. The types say where the operator stands and which side may hold an operator of the same priority: xfx, xfy and yfx are infix, fy and fx are prefix, xf and yf are postfix. In the type, f is the operator, x is an argument of lower priority than the operator and y is an argument of the same or lower priority. So a - b - c is (a - b) - c (yfx), and a , b , c is a , (b , c) (xfy). A term in an argument of a compound term or in a list is read at priority 999, so a comma there separates arguments.
The table below is the default one. SWI-Prolog extras are marked; the viewer uses a table that is wider than any single system, so that it reads the files of all of them.
| Priority | Type | Operators | Notes |
|---|---|---|---|
| 1200 | xfx | :- --> | clause and grammar rule; SWI-Prolog adds => (single-sided unification rules) and ==> (the same for grammar rules); current_op/3 in SWI-Prolog 11.0.0 does not list ?=> (the viewer still counts a clause with that neck as a single-sided rule) |
| 1200 | fx | :- ?- | directive and query |
| 1150 | fx | dynamic discontiguous initialization meta_predicate module_transparent multifile public thread_local thread_initialization volatile table | ISO has dynamic, discontiguous, initialization and multifile; the others are extensions (table is SWI-Prolog, XSB and others) |
| 1105 | xfy | | | as an infix operator, in bodies and grammar rules |
| 1100 | xfy | ; | disjunction |
| 1050 | xfy | -> *-> | if-then; soft cut (*-> is not ISO) |
| 1000 | xfy | , | conjunction |
| 800 | xfx | := | SWI-Prolog, for dicts and functions (checked against SWI-Prolog 11.0.0 with current_op/3) |
| 900 | fy | \+ | negation as failure |
| 700 | xfx | = \= == \== @< @> @=< @>= =.. is =:= =\= < > =< >= | unification, comparison, arithmetic comparison and evaluation; SWI-Prolog adds =@= \=@= as >:< :< |
| 600 | xfy | : | module qualification; the priority differs between systems (the viewer and SWI-Prolog 11.0.0 use this one, read with current_op/3; SICStus uses 550) |
| 500 | yfx | + - /\ \/ | addition, subtraction, bitwise and, or |
| 400 | yfx | * / // mod rem << >> div rdiv xor | div is in ISO Cor.2; rdiv is SWI-Prolog (rationals); xor is at this priority in SWI-Prolog 11.0.0 (the ISO text puts it at the priority of addition) |
| 200 | xfx | ** | power |
| 200 | xfy | ^ | power; also the existential marker in bagof/3 and setof/3 |
| 200 | fy | - + \ | sign and bitwise negation |
| 100 | yfx | . | dict function calls (SWI-Prolog 7 and later) |
| 1 | fx | $ | SWI-Prolog |
op/3
op(Priority, Type, Names) adds, changes or removes operators. Priority is 0 to 1200 (0 removes the operator), Type is one of the seven types, and Names is an atom or a list of atoms. The change applies from that point of the load, in the module that runs it. ISO forbids making , an operator again, making | an operator other than at priority 1001 or more as infix, and giving one name both an infix and a postfix type. A module can export operators in its export list (op(700, xfx, ===>)), which makes them visible to the files that import it.
:- op(700, xfx, ===>).
:- op(200, xfy, **).
:- op(900, fy, not).
rule(a ===> b).
The viewer applies every op/3 directive, and the operators in a module's export list, to the clauses that follow it, and lists each one in the Outline. It does not apply the operators of a module that the file imports, apart from those of library(clpfd) and library(chr) (below).
Canonical and operator syntax
Every operator term can be written in canonical form with brackets, and the two are the same term: a :- b, c is :-(a, ','(b, c)). Writing predicates (write_canonical/1, writeq/1, print/1, portray_clause/1) show a term in one of these forms. An operator used as an atom (an argument) is written in brackets when it could be misread, such as maplist((=), A, B); most systems also read maplist(=, A, B). A negative number is a number token written with a minus sign with no layout after it: - 1 is -(1), and -1 is the integer.
Lists, curly terms and strings
A list [a, b, c] is a chain of pairs ended by []; [H|T] writes the first element and the rest. A partial list ends in a variable: [a, b|_]. A curly term {T} is the compound term {}(T); grammar rules use it for goals, and constraint systems use it for constraints.
Double-quoted text is read as the flag double_quotes says:
| Value | "abc" is | Default in |
|---|---|---|
codes | the list of character codes [97, 98, 99] | ISO, GNU Prolog, SICStus, YAP |
chars | the list of one-character atoms [a, b, c] | Scryer Prolog, Trealla Prolog (not run here) |
atom | the atom abc | chosen by the program |
string | a string object | SWI-Prolog 7 and later (read from current_prolog_flag/2 in SWI-Prolog 11.0.0) |
A directive such as :- set_prolog_flag(double_quotes, codes). changes the reading of the rest of that file. The viewer follows atom (it colours a double-quoted text as an atom) and shows the value in the Overview. Back-quoted text `abc` is a list of codes in SWI-Prolog; the back_quotes flag changes that.
SWI-Prolog adds dicts and quasi quotations. A dict is Tag{Key: Value, ...}, where the tag is an atom or a variable written against the brace (SWI-Prolog 11.0.0 takes any atom there, a symbol atom included: #{a: 1} is a dict and -{a} is a syntax error, so a prefix operator is written with a space before a { term). A quasi quotation is {|Syntax||Text|}: the text between || and |} is passed to the parser named by Syntax, and the viewer colours it as one string.
Operators that libraries add
A library that is loaded can add operators to the file that loads it. The viewer knows two:
| Library | Operators |
|---|---|
library(clpfd) (CLP(FD), integer constraints) | 700 xfx #= #\= #< #> #=< #>= in ins; 450 xfx ..; 760 yfx #<==>; 750 xfy #==>; 750 yfx #<==; 740 yfx #\/; 730 yfx #\; 720 yfx #/\; 710 fy #\; 150 fx # |
library(chr) (constraint handling rules) | 1180 xfx ==> <=>; 1150 fx constraints chr_constraint chr_type chr_option; 1100 xfx \; 1200 xfx @; 1190 xfx pragma; 500 yfx # |
:- use_module(library(clpfd)).
between_ten(X) :- X #>= 1, X #=< 10.
#>= and #=< are operators and the head and body read as terms.Not described
Known, not yet covered
- Reference: the operator tables of the other systems (GNU Prolog, SICStus, ECLiPSe, XSB, Ciao) differ from this default table in places, for example in extra operators for their libraries; only the default table and the two libraries above are listed.
- Reference: the exact conditions of ISO Cor.2 on operators (for example the rules about a prefix operator as an operand) are described in outline only.
- Viewer: the table is one table for all systems and is not chosen by the dialect; operators that another loaded module declares are not applied, so a head or directive that uses one is reported as a note.
- Viewer: the body of a clause is not read as a term, so operator problems inside bodies are not found (the test suite does read whole clauses and compares them with SWI-Prolog 11.0.0's
read_term/3: see the Prolog notes of the audit). - Reference: where a prefix-operator atom or an operator that is also an atom sits next to another operator, SWI-Prolog's reader backtracks in ways the viewer's reader copies only for the common shapes (
- ^,\+ = a,a = dynamic); odd soups such asX mod ?- - 1are read leniently.