Disjunction is normally specified in Prolog by different clauses of
a predicate, but it can also be specified within a single clause by
the use of
;/2. For example,
atomic_particle(X) :- (X = proton ; X = neutron ; X = electron).
This is logically equivalent to:
atomic_particle(proton). atomic_particle(neutron). atomic_particle(electron).
Conditionals can be specified using the
In combination with
;/2, a conditional similar to ‘if-then-else’
constructs of conventional language can be constructed:
Z can be one or more
goals, means that if
X is true, then
Y will be
Z. Only the first solution of
explored, so that on backtracking, no new solutions for
X will be
tried. In addition, if
X succeeds, then the ‘else’ part,
will never be tried. If
X fails, then the ‘then’ part,
will never be tried. An example of ‘if-then-else’ is:
max(X,Y, Max) :- number(X), number(Y), (X > Y -> Max = X ; Max = Y).
Max is the bigger of the numbers
Note the use of the brackets to make the scope of the if-then-else
clear and correct.
One feature of Prolog is the equivalence of programs and data – both are
represented as terms. The predicate
program terms (i.e. data) to be treated as goals:
call(X) will cause
X to be treated as a goal and executed. Although at the time when
the predicate is executed,
X has to be instantiated, it does not
need to be instantiated (or even known) at compile time. For example, it
would in principle be
possible to define disjunction (
;) as follows:
X ; Y :- call(X). X ; Y :- call(Y).
In the pure computational model of Prolog, alternative solutions are computed one-by-one on backtracking. Only one solution is available at any time, while previous solutions disappear on backtracking:
?- weekday(X). X = mo More X = tu More X = we More ...
Sometimes it is useful to have all solution together in a list. This can be achieved by using one of the all-solutions predicates findall/3, setof/3 or bagof/3:
?- findall(X, weekday(X), List). X = X List = [mo, tu, we, th, fr, sa, su] Yes