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

  1. Newsgroups: comp.lang.prolog
  2. Path: sparky!uunet!mcsun!Germany.EU.net!ecrc!acrab6!micha
  3. From: micha@ecrc.de (Micha Meier)
  4. Subject: Re: Compilation of Disjuncts
  5. Message-ID: <1992Sep3.080802.21788@ecrc.de>
  6. Sender: news@ecrc.de
  7. Reply-To: micha@ecrc.de
  8. Organization: European Computer-Industry Research Centre
  9. References: <MATSC.92Sep2141625@vishnu.sics.se>
  10. Date: Thu, 3 Sep 1992 08:08:02 GMT
  11. Lines: 61
  12.  
  13. In article 92Sep2141625@vishnu.sics.se, matsc@sics.se (Mats Carlsson) writes:
  14. >   do you do anything special about this construct, or do you just
  15. >   consider it as an ordinary predicate with a definition like:
  16. >   P ;_Q :- P.
  17. >   _P; Q :- Q.
  18. >
  19. >SICStus Prolog does nothing special, and considers it as an ordinary
  20. >predicate as you describe.
  21.  
  22. I guess this is slightly understated, because that would mean that all goals
  23. inside a disjunction are metacalled, which is not the case in SICStus.
  24.  
  25. There are two basic ways to compile the disjunctive constructs, one is with
  26. auxiliary procedures and the other in-line. If you have a clause
  27.  
  28.     p(X, Y) :- q, (r(X); s(Y)), t.
  29.  
  30. then the former method (used e.g. in SICStus, KCM, ...) is to transform it into
  31.  
  32.     p(X, Y) :- q, aux(X, Y), t.
  33.  
  34.     aux(X, _) :- r(X).
  35.     aux(_, Y) :- s(Y).
  36.  
  37. (in case there is a cut involved, one might need to pass additional information
  38. to aux/2).
  39.  
  40. The other method is done at the WAM level, the WAM code looks something like
  41.  
  42.     allocate
  43.     get_variable Y1, A1
  44.     get_variable Y2, A2
  45.     call q/0
  46.     try_me_else L1
  47.     put_value Y1, A1
  48.     call r/1
  49.     branch L2
  50. L1:    trust_me
  51.     put_value Y2, A1
  52.     call s/1
  53. L2:    deallocate
  54.     execute t/0
  55.  
  56. This method is used e.g. by (as far as I know) Quintus and Sepia and others.
  57. Both methods have advantages and disadvantages. I think the latter is more
  58. efficient, because no additional environment is created for the auxiliary
  59. procedures and the disjunction choice point has always arity zero. There might as well
  60. be less branches to make. On the other hand, the former method can be easily
  61. done on the Prolog level with a preprocessor, so it is quite easy (well, modulo
  62. cut and the like) to do. The in-line expansion, especially if one wants to
  63. improve special cases like not/1, once/1 and if-then-else with a simple condition,
  64. can very quickly turn into a dangerous adventure.
  65.  
  66. --Micha
  67.  
  68. ---
  69. Micha Meier            ------------------------------------------------
  70. ECRC, Arabellastr. 17        The opinions expressed above are private
  71. D-8000 Munich 81        and may not reflect those of my employer.
  72. micha@ecrc.de, Tel. +49-89-92699-108, Fax  +49-89-92699-170
  73.  
  74.