Have-combinator.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
  2. <html>
  3. <!-- Created on November 8, 2012 by texi2html 1.82
  4. texi2html was written by:
  5. Lionel Cons <[email protected]> (original author)
  6. Karl Berry <[email protected]>
  7. Olaf Bachmann <[email protected]>
  8. and many others.
  9. Maintained by: Many creative people.
  10. Send bugs and suggestions to <[email protected]>
  11. -->
  12. <head>
  13. <title>avram - a virtual machine code interpreter: 2.7.16.2 Have combinator</title>
  14. <meta name="description" content="avram - a virtual machine code interpreter: 2.7.16.2 Have combinator">
  15. <meta name="keywords" content="avram - a virtual machine code interpreter: 2.7.16.2 Have combinator">
  16. <meta name="resource-type" content="document">
  17. <meta name="distribution" content="global">
  18. <meta name="Generator" content="texi2html 1.82">
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <style type="text/css">
  21. <!--
  22. a.summary-letter {text-decoration: none}
  23. blockquote.smallquotation {font-size: smaller}
  24. pre.display {font-family: serif}
  25. pre.format {font-family: serif}
  26. pre.menu-comment {font-family: serif}
  27. pre.menu-preformatted {font-family: serif}
  28. pre.smalldisplay {font-family: serif; font-size: smaller}
  29. pre.smallexample {font-size: smaller}
  30. pre.smallformat {font-family: serif; font-size: smaller}
  31. pre.smalllisp {font-size: smaller}
  32. span.roman {font-family:serif; font-weight:normal;}
  33. span.sansserif {font-family:sans-serif; font-weight:normal;}
  34. ul.toc {list-style: none}
  35. -->
  36. </style>
  37. </head>
  38. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  39. <a name="Have-combinator"></a>
  40. <table cellpadding="1" cellspacing="1" border="0">
  41. <tr><td valign="middle" align="left">[<a href="Library-combinator.html#Library-combinator" title="Previous section in reading order"> &lt; </a>]</td>
  42. <td valign="middle" align="left">[<a href="Interaction-combinator.html#Interaction-combinator" title="Next section in reading order"> &gt; </a>]</td>
  43. <td valign="middle" align="left"> &nbsp; </td>
  44. <td valign="middle" align="left">[<a href="Virtual-Machine-Specification.html#Virtual-Machine-Specification" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  45. <td valign="middle" align="left">[<a href="Interfaces-to-External-Code.html#Interfaces-to-External-Code" title="Up section"> Up </a>]</td>
  46. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Next chapter"> &gt;&gt; </a>]</td>
  47. <td valign="middle" align="left"> &nbsp; </td>
  48. <td valign="middle" align="left"> &nbsp; </td>
  49. <td valign="middle" align="left"> &nbsp; </td>
  50. <td valign="middle" align="left"> &nbsp; </td>
  51. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  52. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  53. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  54. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  55. </tr></table>
  56. <hr size="1">
  57. <a name="Have-combinator-1"></a>
  58. <h4 class="subsubsection">2.7.16.2 Have combinator</h4>
  59. <p>As virtual machine interfaces to external libraries accumulate faster
  60. than they can be documented and may vary from one installation to
  61. another, it is helpful to have a way of interrogating the virtual
  62. machine for an up to date list of the installed libraries and
  63. functions. A combinator called <code>have</code> can be used to test for the
  64. availability of a library function. It takes the form
  65. </p>
  66. <dl compact="compact">
  67. <dt> <em>T34</em></dt>
  68. <dd><p>[[<code>have</code>]] (<code><var>x</var></code>,<code><var>y</var></code>) = <code>((nil,nil),((nil,<var>x</var>),(nil,<var>y</var>)))</code>
  69. </p></dd>
  70. </dl>
  71. <p>where <var>x</var> is the name of a library and <var>y</var> is the name of a
  72. function within the library encoded as character strings. For example,
  73. if <var>x</var> is <code>'mtwist'</code> and <var>y</var> is <code>'u_disc'</code> (for the
  74. natural random number generator function in the Mersenne twistor
  75. library) then <code>have(<var>x</var>,<var>y</var>)</code> is a function that returns
  76. a non-empty value if an only if that library is installed and that
  77. function is available within it. The actual argument to the function
  78. is ignored as the result depends only on the installed virtual machine
  79. configuration. In this sense, it acts like a <code>constant</code> combinator.
  80. </p>
  81. <p>One way for this combinator to be used is in code of the form
  82. </p>
  83. <table><tr><td>&nbsp;</td><td><pre class="example"> portable_rng =
  84. conditional(
  85. have('mtwist','u_disc'),
  86. library('mtwist','u_disc'),
  87. some_replacement_function)
  88. </pre></td></tr></table>
  89. <p>which will use the library function if available but otherwise use a
  90. replacement function. Code in this form makes the decision at run
  91. time, but it is also possible to express the function such that the
  92. check for library presence is made at compile time, as the following
  93. example shows, which will imply a slight improvement in performance.
  94. </p>
  95. <table><tr><td>&nbsp;</td><td><pre class="example"> non_portable_rng =
  96. apply(
  97. conditional(
  98. have('mtwist','u_disc'),
  99. constant library('mtwist','u_disc'),
  100. constant some_replacement_function),
  101. 0)
  102. </pre></td></tr></table>
  103. <p>This program would be non-portable in the sense that it would need to
  104. be recompiled for each installation if there were a chance that some
  105. of them might have the <code>mtwist</code> library and some might not,
  106. whereas the previous example would be binary compatible across all of
  107. them. <a name="DOCF2" href="avram_fot.html#FOOT2">(2)</a>
  108. </p>
  109. <p>The actual value returned by a function <code>have(foo,bar)</code> is the
  110. list of pairs of strings <code>&lt;(foo,bar)&gt;</code> if the function is
  111. available, or the empty list otherwise. A non-empty list is
  112. represented as a pair <code>(head,tail)</code>, and an empty list as
  113. <code>nil</code>. The angle bracket notation <code>&lt;a,b,c...&gt;</code> used here is
  114. an abbreviation for <code>(a,(b,(c...nil)))</code>.
  115. </p>
  116. <p>Either or both arguments to the <code>have</code> combinator can be a
  117. wildcard, which is the string containing a single asterisk,
  118. <a name="index-wild-cards"></a>
  119. <code>'*'</code>. In that case, the list of all available matching library
  120. names and function names will be returned. This feature can be used to
  121. find out what library functions are available without already knowing
  122. their names.
  123. </p>
  124. <p>If a library had a function named <code>'*'</code>, which clashes with
  125. the wild card string, the interpretation as a wild card would take
  126. precedence.
  127. </p>
  128. <hr size="1">
  129. <table cellpadding="1" cellspacing="1" border="0">
  130. <tr><td valign="middle" align="left">[<a href="Library-combinator.html#Library-combinator" title="Previous section in reading order"> &lt; </a>]</td>
  131. <td valign="middle" align="left">[<a href="Interaction-combinator.html#Interaction-combinator" title="Next section in reading order"> &gt; </a>]</td>
  132. <td valign="middle" align="left"> &nbsp; </td>
  133. <td valign="middle" align="left">[<a href="Virtual-Machine-Specification.html#Virtual-Machine-Specification" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  134. <td valign="middle" align="left">[<a href="Interfaces-to-External-Code.html#Interfaces-to-External-Code" title="Up section"> Up </a>]</td>
  135. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Next chapter"> &gt;&gt; </a>]</td>
  136. <td valign="middle" align="left"> &nbsp; </td>
  137. <td valign="middle" align="left"> &nbsp; </td>
  138. <td valign="middle" align="left"> &nbsp; </td>
  139. <td valign="middle" align="left"> &nbsp; </td>
  140. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  141. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  142. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  143. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  144. </tr></table>
  145. <p>
  146. <font size="-1">
  147. This document was generated on <i>November 8, 2012</i> using <a href="http://www.nongnu.org/texi2html/"><i>texi2html 1.82</i></a>.
  148. </font>
  149. <br>
  150. </p>
  151. </body>
  152. </html>