Variable-Freedom.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <html lang="en">
  2. <head>
  3. <title>Variable Freedom - 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="Virtual-Code-Semantics.html#Virtual-Code-Semantics" title="Virtual Code Semantics">
  9. <link rel="prev" href="How-_0040code_007bavram_007d-Thinks.html#How-_0040code_007bavram_007d-Thinks" title="How @code{avram} Thinks">
  10. <link rel="next" href="Metrics-and-Maintenance.html#Metrics-and-Maintenance" title="Metrics and Maintenance">
  11. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <style type="text/css"><!--
  14. pre.display { font-family:inherit }
  15. pre.format { font-family:inherit }
  16. pre.smalldisplay { font-family:inherit; font-size:smaller }
  17. pre.smallformat { font-family:inherit; font-size:smaller }
  18. pre.smallexample { font-size:smaller }
  19. pre.smalllisp { font-size:smaller }
  20. span.sc { font-variant:small-caps }
  21. span.roman { font-family:serif; font-weight:normal; }
  22. span.sansserif { font-family:sans-serif; font-weight:normal; }
  23. --></style>
  24. </head>
  25. <body>
  26. <div class="node">
  27. <a name="Variable-Freedom"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Metrics-and-Maintenance.html#Metrics-and-Maintenance">Metrics and Maintenance</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="How-_003ccode_003eavram_003c_002fcode_003e-Thinks.html#How-_003ccode_003eavram_003c_002fcode_003e-Thinks">How &lt;code&gt;avram&lt;/code&gt; Thinks</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="Virtual-Code-Semantics.html#Virtual-Code-Semantics">Virtual Code Semantics</a>
  32. <hr>
  33. </div>
  34. <h4 class="subsection">2.7.6 Variable Freedom</h4>
  35. <p>The virtual code semantics is easier to specify using the
  36. <code>silly</code> language than it would be without it, but still awkward in
  37. some cases. An example is the following declaration from the standard
  38. library,
  39. <a name="index-g_t_0040code_007bhired_007d-275"></a>
  40. <pre class="example"> hired = compose(
  41. compose,
  42. couple(
  43. constant compose,
  44. compose(couple,couple(constant,constant couple))))
  45. </pre>
  46. <p class="noindent">which is constructed in such a way as to imply the following theorem,
  47. provable by routine computation.
  48. <dl>
  49. <dt><em>T9</em><dd><code>(</code>[[<code>hired</code>]] <var>h</var><code>) (</code><var>f</var><code>,</code><var>g</var><code>)</code> = [[<code>compose</code>]]<code>(</code><var>h</var><code>,</code>[[<code>couple</code>]]<code>(</code><var>f</var><code>,</code><var>g</var><code>))</code>
  50. </dl>
  51. <p class="noindent">Intuitively, <code>hired</code> represents a function that takes a given
  52. function to a higher order function. For example, if <code>f</code> were a
  53. function that adds two real numbers, <code>hired f</code> would be a function that
  54. takes two real valued functions to their pointwise sum.
  55. <p>Apart from its cleverness, such an opaque way of defining a function has
  56. little to recommend it. The statement of the theorem about the function
  57. is more readable than the function definition itself, probably because
  58. the theorem liberally employs mathematical variables, whereas the
  59. <code>silly</code> language is variable free. On the other hand, it is not
  60. worthwhile to linger over further enhancements to the language, such as
  61. adding variables to it. The solution will be to indicate informally a
  62. general method of inferring a variable free function definition from an
  63. expression containing variables, and hereafter omit the more
  64. cumbersome definitions.
  65. <p><a name="index-g_t_0040code_007bisolate_007d-276"></a><a name="index-variables-277"></a>An algorithm called <code>isolate</code> does the job.
  66. The input to <code>isolate</code> is a pair <code>(</code><var>e</var><code>,</code><var>x</var><code>)</code>, where
  67. <var>e</var> is a syntactically correct <code>silly</code> expression in
  68. which the identifier <var>x</var> may occur, but no other identifiers
  69. dependent on <var>x</var> may occur (or else it's
  70. garbage-in/garbage-out). Output is a syntactically correct <code>silly</code>
  71. expression <var>f</var> in which the identifier <var>x</var> does
  72. not occur, such that [[<var>e</var>]] = [[<var>f</var> <var>x</var>]].
  73. The algorithm is as follows,
  74. <pre class="display">
  75. if <var>e</var> = <var>x</var> then
  76. return <code>identity</code>
  77. else if <var>e</var> is of the form <code>(</code><var>u</var><code>,</code><var>v</var><code>)</code>
  78. return <code>couple(isolate(</code><var>u</var><code>,</code><var>x</var><code>),isolate(</code><var>v</var><code>,</code><var>x</var><code>))</code>
  79. else if <var>e</var> is of the form <var>u</var> <var>v</var>
  80. return <code>(hired apply)(isolate(</code><var>u</var><code>,</code><var>x</var><code>),isolate(</code><var>v</var><code>,</code><var>x</var><code>))</code>
  81. else
  82. return <code>constant </code><var>e</var>
  83. </pre>
  84. <p class="noindent"><a name="index-equality-278"></a>where equality is by literal comparison of expressions, and the
  85. definition of <code>apply</code> is
  86. <a name="index-g_t_0040code_007bapply_007d-279"></a>
  87. <pre class="example"> apply = (hired meta)((hired compose)(left,constant right),right)
  88. </pre>
  89. <p class="noindent">which represents a function that does the same thing as the invisible
  90. operator.
  91. <dl>
  92. <dt><em>T10</em><dd>[[<code>apply</code>]] <code>(</code><var>f</var><code>,</code><var>x</var><code>)</code> = <var>f</var> <var>x</var>
  93. </dl>
  94. <p>The <code>isolate</code> algorithm can be generalized to functions of
  95. arbitrarily many variables, but in this document we will need
  96. only a unary and a binary version. The latter takes an expression
  97. <var>e</var> and a pair of identifiers <code>(</code><var>x</var><code>,</code><var>y</var><code>)</code> as
  98. input, and returns an expression <var>f</var> such that
  99. [[<var>e</var>]] = [[<var>f</var><code> (</code><var>x</var><code>,</code><var>y</var><code>)</code>]].
  100. <pre class="display">
  101. if <var>e</var> = <var>x</var> then
  102. return <code>left</code>
  103. else if <var>e</var> = <var>y</var> then
  104. return <code>right</code>
  105. else if <var>e</var> is of the form <code>(</code><var>u</var><code>,</code><var>v</var><code>)</code>
  106. return <code>couple(isolate(</code><var>u</var><code>,(</code><var>x</var><code>,</code><var>y</var><code>)),isolate(</code><var>v</var><code>,(</code><var>x</var><code>,</code><var>y</var><code>)))</code>
  107. else if <var>e</var> is of the form <var>u</var> <var>v</var>
  108. return <code>(hired apply)(isolate(</code><var>u</var><code>,(</code><var>x</var><code>,</code><var>y</var><code>)),isolate(</code><var>v</var><code>,(</code><var>x</var><code>,</code><var>y</var><code>)))</code>
  109. else
  110. return <code>constant </code><var>e</var>
  111. </pre>
  112. <p>It might be noted in passing that something similar to these algorithms
  113. would be needed in a compiler targeted to <code>avram</code> if the source
  114. were a functional language with variables.
  115. </body></html>