The aim of the Prolog prologue is to avoid discussing such details and concentrate on the identification and precise definition of these commonly used predicates instead. The Prolog prologue is a possibly empty file to be included (7.4.2.7). After inclusion, the following predicates are defined. A processor may provide also some other means to include the prologue. For example, via a command line switch.
member(X, L)
is true if X
is an element of
the list L
.
More precisely,
member(X, L)
is true iff X
is an element of
a list prefix of L
.
Procedurally, member/2
is defined with the following
clauses.
member(X, [X|_L]). member(X, [_|L]) :- member(X, L).Alternatively:
member(X, [E|L]) :- ( X = E ; member(X, L) ).
member(?term, ?term).
member(X, [1,2]). Succeeds, unifying X with 1. On re-execution, succeeds, unifying Y with 2. member(1, L). Succeeds, unifying L with [1|_]. On re-execution, succeeds, unifying L with [_,1|_]. On re-execution, succeeds, unifying L with [_,_,1|_]. Ad infinitum. member(X, [Y,Z|nonlist]). Succeeds, unifying X with Y. On re-execution, succeeds, unifying X with Z. member(X, nonlist). Fails. member(X, X). Undefined. [STO 7.3.3, in many implementations succeeds, or loops, or produces an error]
append(Xs, Ys, Zs)
is true if Zs
is the
concatenation of the lists Xs
and Ys
.
More precisely,
append(Xs, Ys, Zs)
is true iff the list Xs
is a list prefix of Zs
and Ys
is
Zs
with prefix Xs
removed.
Procedurally, append/3
is defined with the following
clauses.
append([], Zs, Zs). append([X|Xs], Ys, [X|Zs]) :- append(Xs, Ys, Zs).
append(?term, ?term, ?term).
append([a,b],[c,d], Xs). Succeeds, unifying Xs with [a,b,c,d]. append([a], nonlist, Xs). Succeeds, unifying Xs with [a|nonlist]. append([a], Ys, Zs). Succeeds, unifying Zs with [a|Ys]. append(Xs, Ys, [a,b,c]). Succeeds, unifying Xs with [], and Ys with [a,b,c]. On re-execution, succeeds, unifying Xs with [a], and Ys with [b,c]. On re-execution, succeeds, unifying Xs with [a,b], and Ys with [c]. On re-execution, succeeds, unifying Xs with [a,b,c], and Ys with [].
length(List, Length)
is true iff List
is a
list of length Length
.
length(List, Length)
is executed as
follows:
List
is neither a partial list nor a list, then
the goal fails.
List
is a list, then
unifies Length
with the length of List
.
Length
is an integer, then
unifies List
with a list of length Length
with Length
distinct fresh variables as elements.
Len
of N being the integer 0.
Length
with Len
and List
with a list of
length Len
with Len
distinct fresh
variables as elements.
List
is a partial list and Length
is
a variable, chooses the next element Len
of N
and proceeds to step p.p.3.1 g.
length(List, Length)
is re-executable. On
backtracking, continue at p.p.3.1 h above.
length(?term, ?integer)
Length
is neither a variable nor an integer
type_error(integer, Length).
Length
is an integer that is less than zero
domain_error(not_less_than_zero, Length).
length([a,b,c], Length). Succeeds, unifying Length with 3. length(List, 5). Succeeds, unifying List with [_,_,_,_,_]. length(List, Length). Succeeds, unifying List with [] and Length with 0. On re-execution, succeeds, unifying List with [_] and Length with 1. On re-execution, succeeds, unifying List with [_,_] and Length with 2. Ad infinitum.
between(Lower, Upper, X)
is true iff X
is
greater or equal Lower
, and less or equal Upper
.
between(Lower, Upper, X)
is
defined with the following clauses when no error conditions apply.
between(Lower, Upper, Lower) :- Lower =< Upper. between(Lower1, Upper, X) :- Lower1 < Upper, Lower2 is Lower1 + 1, between(Lower2, Upper, X).
between(+integer,+integer,?integer)
Lower
is a variable
instantiation_error.
Upper
is a variable
instantiation_error.
Lower
is neither a variable nor an integer
type_error(integer,Lower).
Upper
is neither a variable nor an integer
type_error(integer,Upper).
X
is neither a variable nor an integer
type_error(integer,X).
between(X, X, 1)
.
between(1, 2, 0). Fails. between(1, 2, I). Succeeds, unifying I with 1. On re-execution, succeeds, unifying I with 2. between(I, I, 0). instantiation_error. between(1, I, 0). instantiation_error. between(I, 1, 0). instantiation_error. between(1, c, 0). type_error(integer,c). between(1+1,2,I). type_error(integer,1+1).
select(X, Xs, Ys)
is true if X
is an element
of the list Xs
and Ys
is the
list Xs
with one occurrence of X
removed.
Procedurally, select/3
is defined with the following
clauses.
select(E, [E|Xs], Xs). select(E, [X|Xs], [X|Ys]) :- select(E, Xs, Ys).
select(?term, ?term, ?term)
select(X, [1,2], Xs). Succeeds, unifying X with 1 and Xs with [2]. On re-execution, succeeds, unifying X with 2 and Xs with [1]. select(X, [Y|inf], Xs). Succeeds, unifying X with Y and Xs with inf. select(E, Xs, Xs). STO.
succ(X, S)
is true iff S
is
the successor of the non-negative integer X
.
succ(+integer,?integer)
succ(-integer,+integer)
X
is a variable and S
is a variable.
instantiation_error.
X
is neither a variable nor an integer
type_error(integer,X).
S
is neither a variable nor an integer
type_error(integer,S).
X
is an integer that is less than zero
domain_error(not_less_than_zero, X).
S
is an integer that is less than zero
domain_error(not_less_than_zero, S).
succ(X, X)
requires an instantiation
error although there is no solution.
succ(X, S). instantiation_error. succ(X, X). instantiation_error. succ(0, S). Succeeds, unifying S with 1. succ(1, 1+1). type_error(integer, 1+1). succ(X, 0). Fails. succ(-1, S). domain_error(not_less_than_zero, -1).
maplist(Cont, List1)
is true iff List1
is a
list and call(Cont,
X1)
is true for each element X1
of List1
.
maplist(Cont, List1, List2)
is true
iff List1
and List2
are lists of same length
and call(Cont, X1, X2)
is true for each
element X1
of List1
and each corresponding
element of X2
.
maplist(Cont, List_1, List_2, ... List_n)
is true iff
List_1
, List_2
up to List_n
are
lists of same length
and call(Cont, E_1_i, E_2_i, ... E_n_i)
is true for
each i
where E_k_i
is the i-th element of List_k
.
Procedurally, maplist/2..8
is defined with the following
clauses.
maplist(_Cont, []). maplist(Cont, [E_1|List_1]) :- call(Cont, E_1), maplist(Cont, List_1). maplist(_Cont, [], []). maplist(Cont, [E_1|List_1], [E_2|List_2]) :- call(Cont, E_1, E_2), maplist(Cont, List_1, List_2). ... maplist(_Cont, [], [], ... []). maplist(Cont, [E_1|List_1], [E_2|List_2], ... [E_n|List_n]) :- call(Cont, E_1, E_2, ... E_n), maplist(Cont, List_1, List_2, ... List_n).
maplist(?term, ?term)
maplist(?term, ?term, ?term)
maplist(?term, ?term, ?term, ?term)
maplist(>(3), [1, 2]). Succeeds. maplist(>(3), [1, 2, 3]). Fails. maplist(=(X), Xs). Succeeds, unifying Xs with []. On re-execution, succeeds, unifying Xs with [X]. On re-execution, succeeds, unifying Xs with [X, X]. On re-execution, succeeds, unifying Xs with [X, X, X]. Ad infinitum.