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
higher-order predicates
finding all solutions
input and output
debugging/tracing

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:

list operations

append(?L, ?M, ?LM)
True if LM 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.)
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.
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.
subset(+S, +T)
True if all elements of list S are present in T.
subtract(+S, +D, ?R)
The return value R is the list S minus any elements that are present in D.

arithmetic expressions

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

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:

?- use_module(library(clpfd)).

I recommend putting the preceding command into your SWI-Prolog initialization file so that it will automatically run at the beginning of each session. (Note that you must include the ?- prefix at the beginning of the line!). The initialization file is

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 swi-prolog directory where you should place init.pl:

?- absolute_file_name(app_config(.), Dir, [file_type(directory)]).
?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.
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.
#\ P
Negate the integer constraint P. For example, #\ (X #= Y)means that X does not equal Y, and is hence the same as X #\= Y.
P #\/ Q (OR)
P #/\ Q (AND)
Compute the boolean OR or AND of two integer 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).

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.

higher-order predicates

call(+Goal, +ExtraArg1, ...)
Invoke Goal as a goal, appending extra arguments ExtraArg1, ExtraArg2, … to the argument list.
foldl(+Goal, ?L, ?V0, ?V)
foldl(+Goal, ?L, ?M, ?V0, ?V)
Fold list L from the left, applying Goal to each element and an accumulator argument that is initially A0. For example, foldl(G, [x, y, z], A0, A) will apply
G(x, A0, A1),
G(y, A1, A2),
G(z, A2, A).
The versions with extra arguments are similar, but operate on pairs or triples of elements from multiple lists.
maplist(+Goal, ?L)
maplist(+Goal, ?L, ?M)
maplist(+Goal, ?L, ?M, ?N)
True if Goal 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.

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.

debugging/tracing

nodebug
Stop debugging.
trace(+Pred)
trace(+Pred, +Ports)
Put a trace point on the given predicate. Ports is a single tracing port or a list of ports; the available ports are (call, redo, exit, fail). If you omit the port list, all ports are traced.
See more hints on debugging/tracing in the lecture notes.