next up previous
Next: Evaluating numerical expressions Up: No Title Previous: Unification

Expressing disjunctive subgoals

You have already seen how to represent alternative ways of satisfying a particular goal by giving more than one clause which could match that goal. For example, we could write a program to decide whether or not any number was between two other numbers using the following two clauses:

between(N, N1, N2):-
    N > N1,
    N < N2.
between(N, N1, N2):-
    N < N1,
    N > N2.

This program states that N is between two numbers N1 and N2 if either N is greater than N1 and N is less than N2 or N is less than N1 or N is greater than N2. This could be stated more succinctly using the special disjunction operator, `;', which is often interpreted in English as "or".

between(N, N1, N2):-
    (N > N1,
     N < N2) ;
    (N < N1,
     N > N2).

The brackets round the conjunction of inequalities on each side of the disjunction operator aren't really necessary. We have included them to emphasise the nested structure of the term. This new definition of between/3 using the `;' operator is identical in meaning to the original definition using two separate clauses.

Dave Stuart Robertson
Tue Jul 7 10:44:26 BST 1998