4.2.3.2 Disjunction

It is sometimes convenient to use an additional operator ‘;’, standing for disjunction (or). (The precedence of ‘;’ is such that it dominates ‘,’ but is dominated by ‘:-’.) An example is the clause (A), which can be read as (B).

     grandfather(X, Z) :-
          (   mother(X, Y)
          ;   father(X, Y)
          ),
          father(Y, Z).  (A)
     
     “For any X, Y, and Z,
         X has Z as a grandfather if
         either the mother of X is Y
             or the father of X is Y,
         and the father of Y is Z.”  (B)

Such uses of disjunction can usually be eliminated by defining an extra predicate. For instance, (A) is equivalent to (C)

     grandfather(X, Z) :- parent(X, Y), father(Y, Z).
     parent(X, Y) :- mother(X, Y).
     parent(X, Y) :- father(X, Y).  (C)

For historical reasons, the token ‘|’, when used outside a list, is actually an alias for ‘;’. The aliasing is performed when terms are read in. Since release 4.3, however, ‘|’ can be defined as a proper infix operator, which then disables the aliasing. So the use of ‘|’ instead of ‘;’ for disjunction is not recommended in new code.


Send feedback on this subject.