Interaction-combinator.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
  2. <html>
  3. <!-- Created on December 10, 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.3 Interaction combinator</title>
  14. <meta name="description" content="avram - a virtual machine code interpreter: 2.7.16.3 Interaction combinator">
  15. <meta name="keywords" content="avram - a virtual machine code interpreter: 2.7.16.3 Interaction 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="Interaction-combinator"></a>
  40. <table cellpadding="1" cellspacing="1" border="0">
  41. <tr><td valign="middle" align="left">[<a href="Have-combinator.html#Have-combinator" title="Previous section in reading order"> &lt; </a>]</td>
  42. <td valign="middle" align="left">[<a href="Vacant-Address-Space.html#Vacant-Address-Space" 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="Interaction-combinator-1"></a>
  58. <h4 class="subsubsection">2.7.16.3 Interaction combinator</h4>
  59. <p>A further combinator allows virtual code applications to interact
  60. directly with any interactive console application using the
  61. <code>expect</code> library. The mechanism is similar to that of
  62. interactive applications documented in the <a href="Output-From-Interactive-Applications.html#Output-From-Interactive-Applications">Output From Interactive Applications</a>, but attempts to be more convenient.
  63. Instead of being designed as an interactive application, any virtual
  64. code application may use this combinator to spawn a shell and interact
  65. with it in order to compute some desired result.
  66. </p>
  67. <p>The advantage of this combinator over the <code>library</code> combinator is
  68. that it requires no modification of the virtual machine to support new
  69. applications. It can also interact with applications that may reside
  70. on remote servers, that are implemented languages other than C, or
  71. <a name="index-GNU-R"></a>
  72. whose source code is unavailable. For example, the GNU R statistical
  73. package provides an interactive command to evaluate multivariate
  74. <a name="index-multivariate-normal-distrubution"></a>
  75. normal distribution functions with an arbitrary covariance matrix, but
  76. <a name="index-covariance-matrix"></a>
  77. the corresponding function is not provided by the <code>Rmath</code> C
  78. library (or any other free library, to the author&rsquo;s knowledge) because
  79. it is implemented in interpreted code. This combinator makes it
  80. callable by an <code>avram</code> virtual code application nevertheless. The
  81. disadvantage compared to the <code>library</code> combinator is that there
  82. is more overhead in spawning a process than simply making a call to a
  83. built in function, and the programming interface is more complicated.
  84. </p>
  85. <p>The combinator takes the form
  86. </p>
  87. <dl compact="compact">
  88. <dt> <em>T35</em></dt>
  89. <dd><p>[[<code>interact</code>]] <var>f</var> = <code>((nil,nil),(((nil,nil),nil),((nil,<var>f</var>),nil)))</code>
  90. </p></dd>
  91. </dl>
  92. <p>where <var>f</var> is the virtual code for a function that
  93. follows the same protocol described in <a href="Output-From-Interactive-Applications.html#Output-From-Interactive-Applications">Output From Interactive Applications</a>,
  94. except that it does not allow file output as described in
  95. <a href="Mixed-Modes-of-Interaction.html#Mixed-Modes-of-Interaction">Mixed Modes of Interaction</a>. The argument <code>x</code> is ignored when the
  96. expression <code>(interact f) x</code> is evaluated, similarly to the way the argument
  97. is ignored in an expression like <code>(constant k) x</code>. The result returned
  98. is a transcript of the dialogue that took place between <code>f</code> and the
  99. externally spawned shell, represented as a list of lists of strings for
  100. line oriented interaction, or a list of characters alternating with lists of
  101. strings in the case of character oriented interaction.
  102. </p>
  103. <p>The following example demonstrates a trivial use of the <code>interact</code>
  104. combinator to spawn an <code>ftp</code> client, do an <code>ls</code> command, and then
  105. <a name="index-ftp"></a>
  106. terminate the session.
  107. </p>
  108. <table><tr><td>&nbsp;</td><td><pre class="example">
  109. eof = &lt;(nil,(nil,(((nil,nil),nil),(nil,nil))))&gt;
  110. demo =
  111. interact conditional(
  112. conditional(identity,constant false,constant true),
  113. constant(0,&lt;'ftp'&gt;,&lt;'ftp&gt; '&gt;),
  114. conditional(
  115. conditional(left,constant false,constant true),
  116. constant(1,&lt;'ls',''&gt;,&lt;'','ftp&gt; '&gt;),
  117. conditional(
  118. compose(compare,couple(left,constant 1)),
  119. constant(2,&lt;'bye',''&gt;,&lt;eof&gt;),
  120. constant nil)))
  121. </pre></td></tr></table>
  122. <p>Some liberties are taken with <code>silly</code> syntax in this example, in
  123. the way of using angle brackets to denote lists, and numbers to
  124. represent states.
  125. </p>
  126. <ul>
  127. <li>
  128. The interacting transducer works by checking whether its argument is
  129. empty (via the <code>identity</code> function used as a predicate in the
  130. <code>conditional</code>, which is then negated). In that case it returns
  131. the triple containing the initial state of 0, the <code>ftp</code> shell
  132. command to spawn the client, and the <code>'ftp&gt; '</code> prompt expected
  133. when the client has been spawned, both of the latter being lists of
  134. strings.
  135. </li><li>
  136. If the argument is non-empty, then next it checks whether it is in the
  137. initial state of 0, (via the <code>left</code> function used as a predicate,
  138. referring to the state variable expected on the left of any given
  139. <code>(state,input)</code> pair, also negated). If so, it returns the triple
  140. containing the next state of 1, the <code>ls</code> command followed by an
  141. empty string to indicate a line break, and the expected prompt
  142. preceded by an empty string to match it only at the beginning of a
  143. line.
  144. </li><li>
  145. Finally, it checks for state 1, in which case it issues the
  146. <code>bye</code> command to close the session, <code>eof</code> rather than a
  147. <a name="index-eof"></a>
  148. prompt to wait for termination of the client, and a state of 2.
  149. </li><li>
  150. In the remaining state of 2, which needn&rsquo;t be explicitly tested
  151. because it is the only remaining possibility, the program returns a
  152. <code>nil</code> value to indicate that the computation has
  153. terminated.
  154. </li></ul>
  155. <p>Deadlock would be possible at any point if either party did not follow
  156. <a name="index-deadlock-1"></a>
  157. this protocol, but for this example it is not an issue. If an
  158. expression of the form <code>demo x</code> were to be evaluated, then
  159. regardless of the value of <code>x</code>, the value of the result would be
  160. as shown below.
  161. </p>
  162. <table><tr><td>&nbsp;</td><td><pre class="example">&lt;
  163. &lt;'ftp'&gt;,
  164. &lt;'ftp&gt; '&gt;,
  165. &lt;'ls',''&gt;,
  166. &lt;'ls','Not connected.','ftp&gt; '&gt;,
  167. &lt;'bye',''&gt;,
  168. &lt;'bye',''&gt;&gt;
  169. </pre></td></tr></table>
  170. <p>That is, it would be a list of lists of strings, alternating between the
  171. output of the interactor and the output of the <code>ftp</code> client. If
  172. the spawned application had been something non-trivial such as a
  173. computer algebra system or a command line web search utility,
  174. then it is easy to see how functions using this combinator can leverage
  175. off a wealth of available resources.
  176. </p>
  177. <hr size="1">
  178. <table cellpadding="1" cellspacing="1" border="0">
  179. <tr><td valign="middle" align="left">[<a href="Have-combinator.html#Have-combinator" title="Previous section in reading order"> &lt; </a>]</td>
  180. <td valign="middle" align="left">[<a href="Vacant-Address-Space.html#Vacant-Address-Space" title="Next section in reading order"> &gt; </a>]</td>
  181. <td valign="middle" align="left"> &nbsp; </td>
  182. <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>
  183. <td valign="middle" align="left">[<a href="Interfaces-to-External-Code.html#Interfaces-to-External-Code" title="Up section"> Up </a>]</td>
  184. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Next chapter"> &gt;&gt; </a>]</td>
  185. <td valign="middle" align="left"> &nbsp; </td>
  186. <td valign="middle" align="left"> &nbsp; </td>
  187. <td valign="middle" align="left"> &nbsp; </td>
  188. <td valign="middle" align="left"> &nbsp; </td>
  189. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  190. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  191. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  192. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  193. </tr></table>
  194. <p>
  195. <font size="-1">
  196. This document was generated on <i>December 10, 2012</i> using <a href="http://www.nongnu.org/texi2html/"><i>texi2html 1.82</i></a>.
  197. </font>
  198. <br>
  199. </p>
  200. </body>
  201. </html>