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 N0 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 N0
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).
List is a partial list and Length is
the variable of the partial list. (Implementation defined)
type_error(integer, []).
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 than or equal to Lower, and less than or equal to 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_1, E1s)
is true iff
E1s is a list and
for each element E1 of E1s,
call(Cont_1, E1) is true.
maplist(Cont_2, E1s, E2s)
is true iff
E1s and E2s are lists of same length
and
for each element E1 of E1s and each corresponding
element of E2,
call(Cont_2, E1, E2) is true.
maplist(Cont_n, E1s, E2s, ... Ens) is true iff
E1s, E2s up to Ens are
lists of same length
and call(Cont_n, E1_i, E2_i, ... En_i) is true for
each i where Ek_i is the i-th element of Listk.
Procedurally, maplist/2..8 is defined with the following
clauses.
maplist(_Cont_1, []). maplist(Cont_1, [E1|E1s]) :- call(Cont_1, E1), maplist(Cont_1, E1s). maplist(_Cont_2, [], []). maplist(Cont_2, [E1|E1s], [E2|E2s]) :- call(Cont_2, E1, E2), maplist(Cont_2, E1s, E2s). maplist(_Cont_3, [], [], []). maplist(Cont_3, [E1|E1s], [E2|E2s], [E3|E3s]) :- call(Cont_3, E1, E2, E3), maplist(Cont_3, E1s, E2s, E3s). ... maplist(_Cont_n, [], [], ... []). maplist(Cont_n, [E1|E1s], [E2|E2s], ... [En|Ens]) :- call(Cont_n, E1, E2, ... En), maplist(Cont_n, E1s, E2s, ... Ens).
maplist(?term, ?term)maplist(?term, ?term, ?term)maplist(?term, ?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.