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, so that (D) is read as if it were (E) thus you can use ‘;’ instead of ‘|’ for disjunction if you like.

     a :- b | c. (D)
     a :- b ; c. (E)

Send feedback on this subject.