Interaction-combinator.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <html lang="en">
  2. <head>
  3. <title>Interaction combinator - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="Interfaces-to-External-Code.html#Interfaces-to-External-Code" title="Interfaces to External Code">
  9. <link rel="prev" href="Have-combinator.html#Have-combinator" title="Have combinator">
  10. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  11. <meta http-equiv="Content-Style-Type" content="text/css">
  12. <style type="text/css"><!--
  13. pre.display { font-family:inherit }
  14. pre.format { font-family:inherit }
  15. pre.smalldisplay { font-family:inherit; font-size:smaller }
  16. pre.smallformat { font-family:inherit; font-size:smaller }
  17. pre.smallexample { font-size:smaller }
  18. pre.smalllisp { font-size:smaller }
  19. span.sc { font-variant:small-caps }
  20. span.roman { font-family:serif; font-weight:normal; }
  21. span.sansserif { font-family:sans-serif; font-weight:normal; }
  22. --></style>
  23. </head>
  24. <body>
  25. <div class="node">
  26. <a name="Interaction-combinator"></a>
  27. <p>
  28. Previous:&nbsp;<a rel="previous" accesskey="p" href="Have-combinator.html#Have-combinator">Have combinator</a>,
  29. Up:&nbsp;<a rel="up" accesskey="u" href="Interfaces-to-External-Code.html#Interfaces-to-External-Code">Interfaces to External Code</a>
  30. <hr>
  31. </div>
  32. <h5 class="subsubsection">2.7.16.3 Interaction combinator</h5>
  33. <p>A further combinator allows virtual code applications to interact
  34. directly with any interactive console application using the
  35. <code>expect</code> library. The mechanism is similar to that of
  36. 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.
  37. Instead of being designed as an interactive application, any virtual
  38. code application may use this combinator to spawn a shell and interact
  39. with it in order to compute some desired result.
  40. <p>The advantage of this combinator over the <code>library</code> combinator is
  41. that it requires no modification of the virtual machine to support new
  42. applications. It can also interact with applications that may reside
  43. on remote servers, that are implemented languages other than C, or
  44. <a name="index-GNU-R-380"></a>whose source code is unavailable. For example, the GNU R statistical
  45. package provides an interactive command to evaluate multivariate
  46. <a name="index-multivariate-normal-distrubution-381"></a>normal distribution functions with an arbitrary covariance matrix, but
  47. <a name="index-covariance-matrix-382"></a>the corresponding function is not provided by the <code>Rmath</code> C
  48. library (or any other free library, to the author's knowledge) because
  49. it is implemented in interpreted code. This combinator makes it
  50. callable by an <code>avram</code> virtual code application nevertheless. The
  51. disadvantage compared to the <code>library</code> combinator is that there
  52. is more overhead in spawning a process than simply making a call to a
  53. built in function, and the programming interface is more complicated.
  54. <p>The combinator takes the form
  55. <dl>
  56. <dt><em>T35</em><dd>[[<code>interact</code>]] <var>f</var> = <code>((nil,nil),(((nil,nil),nil),((nil,</code><var>f</var><code>),nil)))</code>
  57. </dl>
  58. <p class="noindent">where <var>f</var> is the virtual code for a function that
  59. follows the same protocol described in <a href="Output-From-Interactive-Applications.html#Output-From-Interactive-Applications">Output From Interactive Applications</a>,
  60. except that it does not allow file output as described in
  61. <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
  62. expression <code>(interact f) x</code> is evaluated, similarly to the way the argument
  63. is ignored in an expression like <code>(constant k) x</code>. The result returned
  64. is a transcript of the dialogue that took place between <code>f</code> and the
  65. externally spawned shell, represented as a list of lists of strings for
  66. line oriented interaction, or a list of characters alternating with lists of
  67. strings in the case of character oriented interaction.
  68. <p>The following example demonstrates a trivial use of the <code>interact</code>
  69. combinator to spawn an <code>ftp</code> client, do an <code>ls</code> command, and then
  70. <a name="index-ftp-383"></a>terminate the session.
  71. <pre class="example">
  72. eof = &lt;(nil,(nil,(((nil,nil),nil),(nil,nil))))&gt;
  73. demo =
  74. interact conditional(
  75. conditional(identity,constant false,constant true),
  76. constant(0,&lt;'ftp'&gt;,&lt;'ftp&gt; '&gt;),
  77. conditional(
  78. conditional(left,constant false,constant true),
  79. constant(1,&lt;'ls',''&gt;,&lt;'','ftp&gt; '&gt;),
  80. conditional(
  81. compose(compare,couple(left,constant 1)),
  82. constant(2,&lt;'bye',''&gt;,&lt;eof&gt;),
  83. constant nil)))
  84. </pre>
  85. <p class="noindent">Some liberties are taken with <code>silly</code> syntax in this example, in
  86. the way of using angle brackets to denote lists, and numbers to
  87. represent states.
  88. <ul>
  89. <li>The interacting transducer works by checking whether its argument is
  90. empty (via the <code>identity</code> function used as a predicate in the
  91. <code>conditional</code>, which is then negated). In that case it returns
  92. the triple containing the initial state of 0, the <code>ftp</code> shell
  93. command to spawn the client, and the <code>'ftp&gt; '</code> prompt expected
  94. when the client has been spawned, both of the latter being lists of
  95. strings.
  96. <li>If the argument is non-empty, then next it checks whether it is in the
  97. initial state of 0, (via the <code>left</code> function used as a predicate,
  98. referring to the state variable expected on the left of any given
  99. <code>(state,input)</code> pair, also negated). If so, it returns the triple
  100. containing the next state of 1, the <code>ls</code> command followed by an
  101. empty string to indicate a line break, and the expected prompt
  102. preceded by an empty string to match it only at the beginning of a
  103. line.
  104. <li>Finally, it checks for state 1, in which case it issues the
  105. <code>bye</code> command to close the session, <code>eof</code> rather than a
  106. <a name="index-eof-384"></a>prompt to wait for termination of the client, and a state of 2.
  107. <li>In the remaining state of 2, which needn't be explicitly tested
  108. because it is the only remaining possibility, the program returns a
  109. <code>nil</code> value to indicate that the computation has
  110. terminated.
  111. </ul>
  112. <p>Deadlock would be possible at any point if either party did not follow
  113. <a name="index-deadlock-385"></a>this protocol, but for this example it is not an issue. If an
  114. expression of the form <code>demo x</code> were to be evaluated, then
  115. regardless of the value of <code>x</code>, the value of the result would be
  116. as shown below.
  117. <pre class="example"> &lt;
  118. &lt;'ftp'&gt;,
  119. &lt;'ftp&gt; '&gt;,
  120. &lt;'ls',''&gt;,
  121. &lt;'ls','Not connected.','ftp&gt; '&gt;,
  122. &lt;'bye',''&gt;,
  123. &lt;'bye',''&gt;&gt;
  124. </pre>
  125. <p class="noindent">That is, it would be a list of lists of strings, alternating between the
  126. output of the interactor and the output of the <code>ftp</code> client. If
  127. the spawned application had been something non-trivial such as a
  128. computer algebra system or a command line web search utility,
  129. then it is easy to see how functions using this combinator can leverage
  130. off a wealth of available resources.
  131. </body></html>