Prolog Library Quick Reference

This quick reference lists commonly useful predicates. For a complete reference, see the sections Built‑in Predicates and The SWI-Prolog library in the SWI-Prolog documentation.

table of contents

mode indicators
equality
comparing terms
list operations
arithmetic expressions
arithmetic predicates
integer constraints
real constraints
operators
higher-order predicates
finding all solutions
strings
association lists
input and output
runtime statistics

mode indicators

In this documentation, predicate arguments include the following mode indicators. (These are not part of the Prolog language; they are merely a documentation convention.)

equality

?T = ?U
True if terms T and U can be unified.
dif(?T, ?U)
True if terms T and U cannot be unified.

comparing terms

+T @< +U
+T @> +U
Tests whether term T precedes or follows U in the standard order of terms:
?T =.. ?L
True if L is a list whose head is the functor of term T, and whose remaining values are the arguments of T. For example, point(3, 4) =.. [point, 3, 4] is true.

list operations

append(?L, ?M, ?N)
True if N is the concatenation of L and M.
append(+LL, ?L)
Concatenate a list of lists LL to make L. (This is a single-level flatten operation.)
intersection(+L, +M, ?N)
Compute the intersection of two lists without duplicate elements.
length(?L, ?K)
True if K is the number of elements in list L.
member(?X, ?L)
True if X is a member of list L.
msort(+L, ?M)
Sort list L by the standard order of terms to produce M, keeping duplicate elements.
nextto(?X, ?Y, ?L)
True if Y directly follows X in list L.
nth0(?K, ?L, ?X)
True if the K'th element of list L is X. The first element has index 0.
nth1(?K, ?L, ?X)
True if the K'th element of list L is X. The first element has index 1.
numlist(+I, +J, ?L)
True if L is the list [I, I + 1, …, J]. Fails if J < I.
permutation(?L, ?M)
True if list L is a permutation of list M.
prefix(?L, ?M)
True if list L is a prefix of list M.
reverse(?L, ?M)
True if list L contains the same elements as list M in reverse order.
same_length(?L, ?M)
True if lists L and M have the same number of elements.
select(?X, ?L, ?M)
True when list L, with X removed, results in M.
select(?X, ?L, ?Y, ?M)
True if lists L and M are identical except for a single element, which is X in L and is Y in M.
sort(+L, ?M)
Sort list L by the standard order of terms to produce M, removing duplicate elements.
subset(+L, +M)
True if all elements of list L are present in M.
subtract(+L, +M, ?N)
True if N is the list L minus any elements that are present in M.
transpose(+M, ?N)
Transpose the matrix M (a list of lists) to form N.
union(+L, +M, ?N)
Compute the union of two lists without duplicate elements.

arithmetic expressions

An arithmetic expression is a number, a variable, or a compound expression built using these operators and functions:

arithmetic predicates

between(+I, +J, ?K)
True if all arguments are integers and I ≤ K ≤ J. This predicate can generate all values between I and J.
?N is +E
True if arithmetic expression E evaluates to N.
+E =:= +F (equals)
+E =\= +F (does not equal)
+E > +F
+E >= +F
+E < +F
+E =< +F
Evaluate two arithmetic expressions E and F and compare the results.

integer constraints

To use these constraints, you must import the clpfd module. I recommend putting this command into your SWI-Prolog initialization file so that it will automatically run at the beginning of each session:

:- use_module(library(clpfd)).

(Note that you must include the :- prefix at the beginning of the line!). The initialization file is typically at

The location of the file might be somewhat system-dependent. If the path listed above does not work on your system, run this Prolog command to determine the path of the directory where you should place init.pl:

?- absolute_file_name(app_config(.), Dir, [solutions(all)]), !.
?E #= ?F (equals)
?E #\= ?F (does not equal)
?E #> ?F
?E #>= ?F
?E #< ?F
?E #=< ?F
Compare arithmetic expressions E and F. These are true relations and will work in all directions.
?Var in +Domain
True if Var belongs to Domain. A domain can be either of these:
?Vars ins +Domain
True if all variables in the list Vars are elements of Domain.
#\ P
Negate the constraint P. For example, #\ (X #= Y) means that X does not equal Y, and is hence the same as X #\= Y.
P #\/ Q
P #/\ Q
Compute the boolean OR (\/) or AND (/\) of two constraints.
P #<==> Q
True if P and Q are equivalent. Each of P and Q can be either a constraint or a variable with value 0 (false) or 1 (true).
all_distinct(+Vars)
True if all of the variables in the list Vars are distinct.
label(+Vars)
Assign a value to each variable in Vars.
labeling(+Options, +Vars)
Assign a value to each variable in Vars, using a search strategy defined by the given options. Options include

real constraints

To use these constraints, you must import the clpr module:

:- use_module(library(clpr)).

I recommend putting the preceding command into your SWI-Prolog initialization file (as described in the previous section).

{ +Constraints }
Specify one or more real constraints. Each constraint contains two arithmetic expressions separated by a relational operator (=, =\=, <, =<, >, or >=). Multiple constraints are separated by commas.
minimize(+Expression)
Ask the constraint solver to choose variable values that minimize the value of the given expression.
maximize(+Expression)
Ask the constraint solver to choose variable values that maximize the value of the given expression.

operators

Here is a table listing some of Prolog's predefined operators. Many, but not all, of these operators have specific meanings in arithmetic expressions. The table lists operators from lowest to highest precedence; operators on the same line have equal precedence. All operators are binary unless otherwise specified.

higher-order predicates

call(+P, +ExtraArg1, ...)
Invoke P as a goal, appending extra arguments ExtraArg1, ExtraArg2, … to the argument list.
exclude(+P, +L, ?M)
Generate a list M containing all elements of L for which the predicate P fails, i.e. is false.
foldl(+P, ?L, ?A, ?R)
foldl(+P, ?L, ?M, ?A, ?R)
foldl(+P, ?L, ?M, ?N, ?A, ?R)
Fold list L from the left, applying predicate P to each element and an accumulator argument that is initially A. The final result is R. For example, foldl(P, [x, y, z], A, R) will apply
P(x, A, A1),
P(y, A1, A2),
P(z, A2, R).
The versions with extra arguments are similar, but operate on pairs or triples of elements from multiple lists.
include(+P, +L, ?M)
Generate a list M containing all elements of L for which the predicate P succeeds.
maplist(+P, ?L)
maplist(+P, ?L, ?M)
maplist(+P, ?L, ?M, ?N)
True if predicate P can successfully be applied to all elements of list L.
The versions with extra arguments are similar, but operate on pairs or triples of elements from multiple lists.

finding all solutions

findall(+Template, :Goal, -Result)
Backtrack over all possible solutions of Goal; for each one, compute the value of Template. The returned Result is a list of these values. Often Template will be a single variable that is present in Goal.

strings

string_chars(?String, ?Chars)
Convert between a string and a list of characters, represented as atoms.

association lists

An association list is a mapping from keys to values. It is implemented as a balanced binary tree, so its operations will run in O(log N) in the worst case.

empty_assoc(?A)
True if A is an empty association list.
get_assoc(+K, +A, ?V)
Look up a key in an association list, returning a value. If K is not present, the predicate fails, i.e. is false.
put_assoc(+K, +A, +V, ?A2)
Return an association list A2 that is like A, except that K maps to V. You can use this predicate to insert a new key-value pair into an association list, or to replace the value of an existing key.

input and output

display(+T)
Write a term T in expanded form, displaying operators as ordinary functors. For example, display(2 + 3 + 4) writes "+(+(2,3),4)".
nl
Write a newline to standard output.
read(?T)
Read a term from standard input. The input term must end with a period.
write(+T)
Write term T to standard output.
writeln(+T)
Write term T to standard output, followed by a newline.

runtime statistics

time(+G)
Run a goal G and print the CPU time it used and other statistics.