home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / prolog / 1646 < prev    next >
Encoding:
Text File  |  1992-09-03  |  2.7 KB  |  61 lines

  1. Newsgroups: comp.lang.prolog
  2. Path: sparky!uunet!infonode!ingr!capalo!quintus!quintus!tim
  3. From: tim@quintus.com (Tim Lindholm)
  4. Subject: Re: Compilation of Disjuncts
  5. Message-ID: <1992Sep2.171837.3377@quintus.com>
  6. Sender: news@quintus.com (USENET news account)
  7. Nntp-Posting-Host: gladys
  8. Organization: Quintus Corporation, Palo Alto, CA
  9. References:  <1992Sep1.160505.6828@irisa.fr>
  10. Date: Wed, 2 Sep 1992 17:18:37 GMT
  11. Lines: 48
  12.  
  13. In article <1992Sep1.160505.6828@irisa.fr>, serge@irisa.fr (Serge Lehuitouze) writes:
  14. > I have some knowledge (only "theoretic", though) about the WAM, but I have
  15. > never seen anything about the compilation of the disjunction ';' (and
  16. > also things like '->').
  17. > You, WAM gurus, do you do anything special about this construct, or do you just
  18. > consider it as an ordinary predicate with a definition like:
  19. > P ;_Q :- P.
  20. > _P; Q :- Q.
  21.  
  22. Quintus Prolog compiles disjunction and if-then-else in line rather than
  23. calling out to a predicate, although to a first approximation it comes down
  24. to the same thing -- pushing a choice point allowing you to backtrack to the
  25. second horn of the disjunction, then calling the first horn.  Doing so in
  26. line has a few advantages like avoiding the cost of the call and leaving you 
  27. free to customize the choice point to best suit disjunction.  
  28.  
  29. However, when compiling disjunction and if-then-else you also need to worry 
  30. about the scope of cuts.  For instance, the nonsensical program:
  31.  
  32.     go :- ( write('first horn'), nl,
  33.             !,
  34.         fail
  35.       ; write('second horn'), nl
  36.           ).
  37.     go.
  38.  
  39. writes "first horn" and *fails* in Quintus Prolog, the cut cutting away the
  40. 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-
  41. called goal, cutting neither the choice point for ;/2 nor go/0.
  42.  
  43. Quintus compiles general if-then-elses similarly, except that it optimizes
  44. many cases where failure of an "if" test doesn't need all the power of
  45. backtracking to recover and get to the "then".  These include things like
  46. term type tests, arithmetic and term comparisons (==/2), etc.  Calls to user-
  47. defined predicates are never optimized like this, as compilation doesn't know
  48. what all they are going to do (e.g. create structure, bind variables) when
  49. they are called.
  50.  
  51. Knowing how your Prolog handles disjunction and if-then-else, and whether
  52. there are cases that it optimizes, you can often greatly reduce the number of time- and space-expensive choice points you push.
  53.  
  54. -- Tim
  55.  
  56. Tim Lindholm                Manager, Prolog Technology Group
  57. Quintus Corporation                     InterNet:       tim@quintus.com
  58. 2100 Geng Road                          FAX:            415 494 7608
  59. Palo Alto, California 94303             Phone:          415 813 3844
  60.