home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / prolog / 1608 < prev    next >
Encoding:
Text File  |  1992-08-26  |  3.3 KB  |  86 lines

  1. Newsgroups: comp.lang.prolog
  2. Path: sparky!uunet!mcsun!sunic!sics.se!matsc
  3. From: matsc@sics.se (Mats Carlsson)
  4. Subject: Re: SICStus prolog problem
  5. In-Reply-To: lhe@sics.se's message of Wed, 26 Aug 1992 14:53:53 GMT
  6. Message-ID: <MATSC.92Aug27090609@vishnu.sics.se>
  7. Sender: news@sics.se
  8. Organization: Swedish Institute of Computer Science, Kista
  9. References: <714691472.12060@minster.york.ac.uk> <714823911.21006@minster.york.ac.uk>
  10.     <1992Aug26.145353.21444@sics.se>
  11. Date: Thu, 27 Aug 1992 08:06:08 GMT
  12. Lines: 72
  13.  
  14. In article <1992Aug26.145353.21444@sics.se> lhe@sics.se (Lars-Henrik Eriksson) writes:
  15.  
  16.    In article <714823911.21006@minster.york.ac.uk> cjhs@minster.york.ac.uk writes:
  17.    >: I have a program in SICStus prolog that makes extensive use of the wait
  18.    >: declaration. Program (137 lines) supplied to anyone who is curious.
  19.    >: 
  20.    >: The problem is that it runs ok interpreted, but fails when compiled.
  21.    >: Insertion of a diagnostic in a critical place makes the compiled
  22.    >: version behave like the interpreted version.
  23.  
  24.    I suspect that the problem you have run into is the same one that I
  25.    once encountered and that caused me much grief before I got an
  26.    explanation and a workaround.
  27.  
  28.    If you have a clause body that looks like this:
  29.  
  30.        .... baz(...), !, ....
  31.  
  32.    and the call to baz causes blocked goals to run, the compiler executes
  33.    the cut *before* running the blocked goals. This may cause the program
  34.    to fail even if it works correctly under the interpreter.
  35.  
  36.    The following program illustrates the point:
  37.  
  38.        :- block bar(-). /* :- wait bar/1 before SICStus 2.0 /*
  39.        foo :- bar(X), baz(X), !.
  40.        bar(2).
  41.        baz(X) :- X=1.
  42.        baz(X) :- X=2.
  43.  
  44.    The reason baz/1 is written in a slightly odd way is to prevent
  45.    indexing. It is essential that a choicepoint is created.
  46.  
  47. You _will_ get indexing with this formulation too, but that
  48. doesn't change anything for your example.  There will be a choicepoint
  49. since X is unbound.
  50.  
  51.    Calling foo/0 as a goal succeeds under the interpreter, but fails
  52.    under the compiler.
  53.  
  54.    The compiled code calls baz(X), which succeeds, binding X to 1. The
  55.    call to bar/1 is now unblocked, but *not* run.  First Sicstus cuts
  56.    away the choice point in baz/1, *then* calls the unblocked goal. Of
  57.    course bar(1) will fail, and since the choicepoint in baz/1 has been
  58.    cut away, the call to foo/0 will also fail.
  59.  
  60.    The interpreter will do the cut only *after* calling bar/1, so in that
  61.    case foo/0 will succeed.
  62.  
  63.    [...]
  64.  
  65.    The solution is to insert a dummy goal (likely the diagnostic in your
  66.    case!) before the cut, e.g.
  67.  
  68.        foo :- bar(X), baz(X), true, !.
  69.  
  70.    Now the compiled code runs as expected.
  71.    The manual should really state clearly that this can happen. 
  72.    [...]
  73.  
  74. But it does, witness page 110 in the User's Manual for version 2.1.
  75.  
  76. The check for unblocked goals is done at procedure calls.  To fix the
  77. interpreter/compiler discrepancy, nearly all built-in predicates that
  78. compile in-line would have to check.  Also, the compiled code would
  79. have to include information to make it possible to safely "escape"
  80. from the current execution and invoke the unblocked goals.
  81. We haven't yet figured out a way to do this without undue overhead.
  82. --
  83. Mats Carlsson
  84. SICS, PO Box 1263, S-164 28  KISTA, Sweden    Internet: matsc@sics.se
  85. Tel: +46 8 7521543     Ttx: 812 6154 7010 SICS     Fax: +46 8 7517230
  86.