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.

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

boolean constants

false

Always fails.

true

Always succeeds.

comparing terms

?T = ?U

True if terms T and U can be unified.

dif(?T, ?U)

True if terms T and U cannot be unified. This is a constraint that belongs to the pure language.

+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.

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.

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 ≤ KJ. This predicate can generate all values between I and J.

?N is +E

True if arithmetic expression E evaluates to N. This predicate only works from right to left.

+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!) On SWI-Prolog 8.0 and earlier, this file is .swiplrc in your home directory. If you are using SWI-Prolog 8.1 or later, it is in a different location.

?E #= ?F (equals)
?E
#\= ?F (does not equal)
?E
#> ?F
?E
#>= ?F
?E
#< ?F
?E
#=< ?F

Compare arithmetic expressions E and F. Unlike the predicates in the previous section, these are true relations and will work in all directions.

#\ 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).

?Var in +Domain

True if Var belongs to Domain. A domain can be either of these:

Lower .. Upper : A range of integers. In a range, inf and sup represent negative and positive infinity. I recommend that you use this domain notation only when you have a fixed numeric range, since Prolog requires that the lower and upper bounds be known. If the upper or lower bound is a variable, it is better to use #>= or #=< instead.

Domain1 \/ Domain2 : The union of two domains.

?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.

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 V0. For example, foldl(G, [a, b, c], V0, V) will apply

G(a, V0, V1),
G(b, V1, V2),
G(c, V2, V).

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.

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.

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.