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.)
? : an argument that can be used for input or output: it may be unbound, partially instantiated, or fully instantiated
+ : an input argument: it must be fully instantiated
- : an output argument: it must be unbound
false
Always fails.
true
Always succeeds.
=
?UTrue 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.
@<
+U@>
+UTests whether term T precedes or follows U in the standard order of terms:
numbers < atoms < lists, structures
Numbers are compared by value.
Atoms are compared alphabetically.
Lists are compared lexicographically: first by their first element, then their second element, and so on. (This is actually a special case of structure comparison.)
Structures are compared first by their arity (number of arguments), then by their functor name (alphabetically), then by their arguments, leftmost first.
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.
nth
1
(?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.
An arithmetic expression is a number, a variable, or a compound expression built using these operators:
+
: addition-
: subtraction*
: multiplication/
: floating-point divisiondiv
: integer division, rounding toward
negative infinitymod
: modulus, i.e. remainder after
integer division^
: exponentiationabs
(X):
absolute valuemax
(X,
Y) : maximum
of two valuesmin
(X,
Y): minimum
of two valuescos
(X), sin
(X),
tan
(X): trigonometric functionsbetween
(+I,
+J, ?K)True if all arguments are integers and I ≤ K ≤ J. This predicate can generate all values between I and J.
is
+ETrue if arithmetic expression E evaluates to N. This predicate only works from right to left.
=:=
+F
(equals)=\=
+F
(does
not equal)>
+F>=
+F<
+F=<
+FEvaluate two arithmetic expressions E and F and compare the results.
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.
#=
?F
(equals)#\=
?F
(does
not equal)#>
?F#>=
?F#<
?F#=<
?FCompare 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).
in
+DomainTrue 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.
ins
+DomainTrue 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.
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).
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.
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.
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.
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.
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.