home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / compiler / 2228 < prev    next >
Encoding:
Text File  |  1993-01-22  |  4.2 KB  |  102 lines

  1. Newsgroups: comp.compilers
  2. Path: sparky!uunet!world!iecc!compilers-sender
  3. From: Peter Ludemann <ludemann@quintus.com>
  4. Subject: Re: Different Strokes for Different Folks (Was: Assessing a language)
  5. Reply-To: Peter Ludemann <ludemann@quintus.com>
  6. Organization: Compilers Central
  7. Date: Fri, 22 Jan 1993 03:21:54 GMT
  8. Approved: compilers@iecc.cambridge.ma.us
  9. Message-ID: <93-01-156@comp.compilers>
  10. Keywords: prolog, functional
  11. References: <93-01-082@comp.compilers>
  12. Sender: compilers-sender@iecc.cambridge.ma.us
  13. Lines: 87
  14.  
  15. eifrig@beanworld.cs.jhu.edu (Jonathan Eifrig) wrote:
  16. >    Surprisingly, there hasn't been much work in developing
  17. >heterogenous programming environments, to support a sort of "mix and
  18. >match" approach to programming.  Such tools would go a long way to
  19. >alleviating the language holy wars, I think.
  20.  
  21. This isn't true of some Prolog implementations.  From the user's point of
  22. view, the major integration problem is writing the declarations for the C
  23. functions if calling from Prolog to C; the call from C to Prolog is
  24. usually done through a single procedure call, plus some routines to
  25. manipulate the Prolog structures.
  26.  
  27. The tricky part from an implementation point of view is dealing with all
  28. the various flavors of object code formats and the "features" of loaders
  29. (dare I say "bugs"?).  If nice dynamic loading isn't desired, the
  30. implementation becomes simpler.  But a major advantage of using more
  31. powerful languages such as Prolog is the rapid edit/load/run cycle, so
  32. it's nice to have dynamic loading of the foreign code.
  33.  
  34. One of the interesting questions is how to handle the mismatch of
  35. functionality between the two languages.  Data structures are fairly easy
  36. to handle (for example, transforming between Prolog lists and C arrays); a
  37. larger question is how to handle things like backtracking (from Prolog) or
  38. continuations and functional composition (in Scheme or ML).
  39.  
  40. As an example of handling backtracking in Prolog, we could provide
  41. functions to create backtrack points from C; or we could instead insist
  42. that the C function be wrapped in a Prolog which does the backtracking.
  43. As an example of the latter, here is some (horrible) C code, which
  44. generates successive values between i_first and i_last, giving a return
  45. code of 1 for success and 0 for failure:
  46.  
  47. int i_first, i_last; /* global variables */
  48.  
  49. gener_init(first, last) 
  50. int first, last;
  51. {   i_first = first;
  52.     i_last = last;
  53. }
  54.  
  55. int gener(i) 
  56. int *i;
  57. {   if ( i_first > i_last ) {
  58.         *i = i_last;
  59.         return 0;
  60.     } else {
  61.         *i = i_first++;
  62.         return 1;
  63.     }    
  64. }
  65.  
  66. Then, we can write in Quintus Prolog a predicate to generate successive
  67. values by the following:
  68.  
  69. foreign(gener_init, c, gener_init(+integer, +integer)). /*declare C routine */
  70. foreign(gener,      c, gener(-integer, [-integer])).    /*declare C routine */
  71. foreign_file('gener.o', [gener_init, gener]).       /* where to find the .o */
  72. :- load_foreign_files('gener.o', []).                   /*load it */
  73.  
  74. between(First, Last, I) :- 
  75.     gener_init(First, Last),   /* call C routine to initialize */
  76.     gener(I0, RC),             /* get first result */
  77.     between2(RC, I0, I).       /* check result */
  78.  
  79. between2(1, I, I).             /* result OK */
  80. between2(1, _, I) :-           /* backtrack */
  81.     gener(I0, RC),             /* gener new value from C */
  82.     between2(RC, I0, I).       /* and check it */
  83.  
  84. The alternative to this is to allow declaring a backtrack point from
  85. inside C, which requires exposing more of the Prolog system's internals to
  86. the interface.
  87.  
  88. The above example isn't very useful, but it indicates how to build an
  89. interface to a database, getting successive rows by backtracking.
  90.  
  91. Two Prolog implementations that have good foreign language interfaces are
  92. IBM Prolog (CMS, MVS, OS/2) and Quintus Prolog (Unix, DOS).  No doubt
  93. there are others.
  94. -----
  95. Peter Ludemann                 Manager, Large Applications Consulting
  96. Quintus Corporation                     InterNet:       ludemann@quintus.com
  97. 2100 Geng Road                          FAX:            +1-415-494-7608
  98. Palo Alto, California 94303             Phone:          +1-415-813-3819
  99. -- 
  100. Send compilers articles to compilers@iecc.cambridge.ma.us or
  101. {ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.
  102.