In article <1992Sep1.160505.6828@irisa.fr>, serge@irisa.fr (Serge Lehuitouze) writes:
> I have some knowledge (only "theoretic", though) about the WAM, but I have
> never seen anything about the compilation of the disjunction ';' (and
> also things like '->').
>
> You, WAM gurus, do you do anything special about this construct, or do you just
> consider it as an ordinary predicate with a definition like:
> P ;_Q :- P.
> _P; Q :- Q.
Quintus Prolog compiles disjunction and if-then-else in line rather than
calling out to a predicate, although to a first approximation it comes down
to the same thing -- pushing a choice point allowing you to backtrack to the
second horn of the disjunction, then calling the first horn. Doing so in
line has a few advantages like avoiding the cost of the call and leaving you
free to customize the choice point to best suit disjunction.
However, when compiling disjunction and if-then-else you also need to worry
about the scope of cuts. For instance, the nonsensical program:
go :- ( write('first horn'), nl,
!,
fail
; write('second horn'), nl
).
go.
writes "first horn" and *fails* in Quintus Prolog, the cut cutting away the
choice point for go/0 as well as the choice point for the disjunction. If you defined ;/2 as above, the cut in the first horn would only affect the meta-
called goal, cutting neither the choice point for ;/2 nor go/0.
Quintus compiles general if-then-elses similarly, except that it optimizes
many cases where failure of an "if" test doesn't need all the power of
backtracking to recover and get to the "then". These include things like
term type tests, arithmetic and term comparisons (==/2), etc. Calls to user-
defined predicates are never optimized like this, as compilation doesn't know
what all they are going to do (e.g. create structure, bind variables) when
they are called.
Knowing how your Prolog handles disjunction and if-then-else, and whether
there are cases that it optimizes, you can often greatly reduce the number of time- and space-expensive choice points you push.