A built-in predicate is one that the system provides without a library. The lists below give the ISO/IEC 13211-1 predicates by group, and mark the most common ones from SWI-Prolog and the libraries of the main systems with (SWI) or (library). A predicate is written name/arity. The viewer colours a call to a name on these lists as a keyword. It is a colouring aid, not a check: it does not know whether a program defines its own predicate with the same name.
Control
| Predicate | Meaning |
|---|---|
true/0, fail/0, false/0 | succeed; fail (false is ISO Cor.2 and SWI-Prolog) |
!/0 | cut: commit to the clause and to the choices made so far in it |
,/2, ;/2, ->/2, *->/2 | conjunction, disjunction, if-then (with ; if-then-else), soft-cut if-then (not ISO) |
\+/1 | negation as failure: succeeds if the goal fails |
call/1 to call/8 | call a goal built at run time, with extra arguments added by call/N |
once/1, ignore/1 (SWI) | the first solution only; the first solution, and succeed if there is none |
repeat/0 | succeed again on every backtrack |
halt/0, halt/1 | stop the system, with an exit status |
catch/3, throw/1 | catch an exception; raise one |
forall/2 (library) | for every solution of the first goal the second succeeds |
Type checking, comparison and term inspection
| Predicate | Meaning |
|---|---|
var/1, nonvar/1 | the term is, or is not, an unbound variable |
atom/1, number/1, integer/1, float/1, atomic/1, compound/1, callable/1 | type tests; callable/1 is ISO Cor.2 |
is_list/1, ground/1, string/1 (SWI), is_dict/1 (SWI) | a proper list; no variables; a string; a dict |
=/2, \=/2 | unify; cannot be unified |
unify_with_occurs_check/2 | unify with the occurs check |
==/2, \==/2, @</2, @>/2, @=</2, @>=/2, compare/3 | identity and the standard order of terms (variables, numbers, atoms, compound terms) |
functor/3, arg/3, =../2 | name and arity of a term; one argument; term to list and back |
copy_term/2, term_variables/2 | a copy with fresh variables; the variables of a term |
Arithmetic
X is Expr evaluates Expr and unifies the result. The comparisons =:=, =\=, <, >, =< and >= evaluate both sides. A variable that is unbound in an expression raises an instantiation error.
| Functions | Notes |
|---|---|
+ - * / // mod rem div abs sign min max | / on two integers of two integers differs between systems: SWI-Prolog gives an integer when the division is exact, unless its flag iso is true |
sqrt sin cos atan atan2 exp log ** ^ float integer float_integer_part float_fractional_part truncate round ceiling floor | ISO; log/2, cot, gcd, msb, random, cputime and e, pi, inf, nan, epsilon, max_tagged_integer are extensions |
>> << /\ \/ xor \ | bit operations on integers |
succ/2, plus/3, between/3 | predicates: the next integer; addition in any direction; integers in a range (SWI-Prolog and the libraries of many systems) |
Atoms, characters and strings
| Predicate | Meaning |
|---|---|
atom_codes/2, atom_chars/2, char_code/2, atom_length/2 | conversions and length |
atom_concat/3, sub_atom/5 | join or split an atom (all ways); a part of an atom |
number_codes/2, number_chars/2, atom_number/2 (SWI) | number and text, in both directions |
upcase_atom/2, downcase_atom/2, atomic_list_concat/2,3 (SWI) | case; join a list, or split with a separator |
string_concat/3, string_chars/2, string_codes/2, string_to_atom/2, string_length/2, sub_string/5, split_string/4, number_string/2, term_to_atom/2 (SWI) | the string type; other systems provide similar predicates in libraries |
char_conversion/2, current_char_conversion/2 | the character-conversion table (ISO) |
Lists
List predicates are in libraries rather than in the ISO core, but every system provides them in some form (library(lists) in SWI-Prolog, SICStus and others).
| Predicate | Meaning |
|---|---|
append/3, append/2 | concatenate lists (in any direction) |
member/2, memberchk/2 | an element; the first match only |
length/2,reverse/2, nth0/3, nth1/3, last/2 | length, in any direction; reversal; element by index |
msort/2, sort/2, sort/4, predsort/3, keysort/2 | sorting (sort/2 removes duplicates, keysort/2 is stable) |
sum_list/2, max_list/2, min_list/2, numlist/3 | list arithmetic |
maplist/2.., foldl/4.., include/3, exclude/3, partition/4 (library) | higher-order predicates over lists |
select/3, delete/3, subtract/3, union/3, intersection/3, permutation/2, flatten/2, list_to_set/2 | other list operations |
The database, and collecting solutions
| Predicate | Meaning |
|---|---|
asserta/1, assertz/1, assert/1 (not ISO) | add a clause at the front, at the end |
retract/1, retractall/1 (ISO Cor.2), abolish/1 | remove one clause that matches; all that match; a whole predicate |
clause/2 | read the clauses of a predicate |
current_predicate/1, predicate_property/2 (SWI) | what is defined |
findall/3, findall/4 (SWI) | the list of all solutions |
bagof/3, setof/3 | solutions grouped by the free variables; ^ marks variables that are not free; setof/3 sorts and removes duplicates and fails when there is none |
aggregate_all/3 (SWI, library), nb_setval/2, nb_getval/2, b_setval/2, b_getval/2 | counting and summing; global variables |
Exceptions
throw(Ball) raises an exception, and catch(Goal, Catcher, Recovery) catches a ball that unifies with Catcher. A built-in predicate raises error(Formal, Context). ISO defines these formal terms:
| Formal term | Raised when |
|---|---|
instantiation_error | an argument is an unbound variable where a value is needed |
type_error(Type, Culprit) | an argument is of the wrong type (atom, integer, callable, evaluable and so on) |
domain_error(Domain, Culprit) | the type is right but the value is not allowed |
existence_error(Kind, What) | something does not exist (an unknown procedure, a stream, a file) |
permission_error(Action, Type, Culprit) | the action is not allowed (modifying a built-in, reading an output stream) |
representation_error(What) | an implementation limit is exceeded (a character code, a maximum arity) |
evaluation_error(What) | an arithmetic result is undefined (zero_divisor, undefined, float_overflow) |
resource_error(Resource) | the system ran out of memory or another resource |
syntax_error(Message) | text could not be read as a term |
system_error | an error in the system itself |
safe_div(X, Y, Z) :-
catch(Z is X / Y, error(evaluation_error(zero_divisor), _), Z = undefined).
read_number(N) :-
read(X),
( number(X)
-> N = X
; throw(error(type_error(number, X), read_number/1))
).
Input and output, and flags
| Predicate | Meaning |
|---|---|
read/1, read_term/2,3 | read a term from the current input, or from a stream |
write/1, writeq/1, print/1, write_canonical/1, write_term/2,3 | write a term: plain; quoted so that it reads back; canonical; with options |
nl/0, put_char/1, get_char/1, peek_char/1, put_byte/1, get_byte/1 | characters and bytes (and _code forms for codes) |
writeln/1, format/1,2,3, print_message/2 (SWI and others) | write a term and a new line; formatted output with directives such as ~w, ~a, ~d, ~n; messages |
open/3,4, close/1,2, current_input/1, current_output/1, set_input/1, set_output/1, flush_output/0,1, stream_property/2 | streams |
op/3, current_op/3 | declare operators; list them |
set_prolog_flag/2, current_prolog_flag/2 | flags: bounded, max_integer, min_integer, integer_rounding_function, char_conversion, debug, max_arity, unknown, double_quotes (ISO); many more in each system |
Not described
Known, not yet covered
- Reference: only the ISO predicates and the commonest ones of SWI-Prolog and the libraries are named; the thousands of other predicates of each system (threads, sockets, HTTP, strings, assoc and tries, constraint solvers, foreign interfaces) are not listed, and the argument modes, the errors of each predicate and the format directives are not given in full.
- Reference: the differences between systems in the behaviour of a predicate are not written out; see Dialects and versions for the large ones.
- Viewer: the keyword colouring uses one list for all systems and cannot tell a built-in from a user predicate of the same name.