🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #767

Re: Compilation of Disjuncts

#767
From: Tim Lindholm
Date: Wed, 02 Sep 1992 17:18
48 lines
2856 bytes
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.

-- Tim

Tim Lindholm				Manager, Prolog Technology Group
Quintus Corporation                     InterNet:       tim@quintus.com
2100 Geng Road                          FAX:            415 494 7608
Palo Alto, California 94303             Phone:          415 813 3844
Technical Details
Message-ID: <1992Sep2.171837.3377@quintus.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!infonode!ingr!capalo!quintus!quintus!tim
References: <1992Sep1.160505.6828@irisa.fr>

Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.prolog
Path: arc1-ca.pugleaf.net!sparky!uunet!infonode!ingr!capalo!quintus!quintus!tim
From: tim@quintus.com (Tim Lindholm)
Subject: Re: Compilation of Disjuncts
Message-ID: <1992Sep2.171837.3377@quintus.com>
Sender: news@quintus.com (USENET news account)
Nntp-Posting-Host: gladys
Organization: Quintus Corporation, Palo Alto, CA
References:  <1992Sep1.160505.6828@irisa.fr>
Date: Wed, 2 Sep 1992 17:18:37 GMT
Lines: 48