Assignment.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <html lang="en">
  2. <head>
  3. <title>Assignment - 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="Recursion.html#Recursion" title="Recursion">
  10. <link rel="next" href="Predicates.html#Predicates" title="Predicates">
  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="Assignment"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Predicates.html#Predicates">Predicates</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Recursion.html#Recursion">Recursion</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.10 Assignment</h4>
  35. <p><a name="index-assignment-303"></a><a name="index-imperative-programming-304"></a>In an imperative programming paradigm, a machine consists partly of an
  36. ensemble of addressable storage locations, whose contents are changed
  37. over time by assignment statements. An assignment statement includes
  38. some computable function of the global machine state, and the address of
  39. the location whose contents will be overwritten with the value computed
  40. from the function when it is evaluated.
  41. <p>Compiling a language containing assignment statements into virtual
  42. machine code suitable for <code>avram</code> might be facilitated by
  43. exploiting the following property.
  44. <dl>
  45. <dt><em>P16</em><dd>([[<code>assign</code>]] <code>(</code><var>p</var><code>,</code><var>f</var><code>)</code>) <var>x</var> = [[<code>replace</code>]] <code>((</code><var>p</var><code>,</code><var>f</var> <var>x</var><code>),</code><var>x</var><code>)</code>
  46. </dl>
  47. <p class="noindent">The identifier <code>assign</code> is used in <code>silly</code> to express a
  48. virtual code fragment having the form shown below, and <code>replace</code>
  49. corresponds to a further operation to be explained presently.
  50. <a name="index-g_t_0040code_007bassign_007d-305"></a>
  51. <dl>
  52. <dt><em>T18</em><dd>[[<code>assign</code>]] <code>(</code><var>p</var><code>,</code><var>f</var><code>)</code> = <code>(((</code><var>p</var><code>,</code><var>f</var><code>),nil),nil)</code>
  53. </dl>
  54. <p>This feature simulates assignment statements in the following way. The
  55. variable <var>x</var> in <em>P16</em> corresponds intuitively to the set
  56. of addressable locations in the machine. The variable <var>f</var>
  57. corresponds to the function whose value will be stored in the location
  58. addressed by <var>p</var>. The result of a function expressed using
  59. <code>assign</code> is a new store similar to the argument <var>x</var>, but
  60. with the part of it in location <var>p</var> replaced by <var>f</var>
  61. <var>x</var>. A source text with a sequence of assignment statements could
  62. therefore be translated directly into a functional composition of trees
  63. in this form.
  64. <p><a name="index-storage-locations-306"></a>The way storage locations are modeled in virtual code using this feature
  65. would be as nested pairs, and the address <var>p</var> of a location
  66. is a tree interpreted similarly to the trees used as operands to the
  67. <code>field</code> operator described in <a href="Field.html#Field">Field</a>, to specify
  68. deconstructions. In fact, <code>replace</code> can be defined as a minimal
  69. solution to the following equation.
  70. <a name="index-g_t_0040code_007breplace_007d-307"></a>
  71. <dl>
  72. <dt><em>E0</em><dd>([[<code>field</code>]] <var>p</var>) [[<code>replace</code>]] <code>((</code><var>p</var><code>,</code><var>y</var><code>),</code><var>x</var><code>)</code> = <var>y</var>
  73. </dl>
  74. <p>This equation regrettably does
  75. not lend itself to inferring the <code>silly</code> source for <code>replace</code>
  76. <a name="index-g_t_0040code_007bisolate_007d-308"></a>using the <code>isolate</code> algorithm in <a href="Variable-Freedom.html#Variable-Freedom">Variable Freedom</a>, so an explicit
  77. construction is given in <a href="Replace.html#Replace">Replace</a>. This construction need not concern a
  78. reader who considers the equation a sufficiently precise specification
  79. in itself.
  80. <p>In view of the way addresses for deconstruction are represented as
  81. trees, it would be entirely correct to infer from this equation that a
  82. tuple of values computed together can be assigned to a tuple of
  83. locations. The locations don't even have to be &ldquo;contiguous&rdquo;, but could
  84. be anywhere in the tree representing the store, and the function is
  85. computed from the contents of all of them prior to the update. Hence,
  86. this simulation of assignment fails to capture the full inconvenience of
  87. imperative programming except in the special case of a single value
  88. assigned to a single location, but fortunately this case is the only one
  89. most languages allow.
  90. <p>There is another benefit to this feature besides running languages with
  91. assignment statements in them, which is the support of abstract or
  92. opaque data structures. A function that takes an abstract data structure
  93. as an argument and returns something of the same type can be coded in a
  94. way that is independent of the fields it doesn't use. For example, a
  95. data structure with three fields having the field identifiers
  96. <code>foo</code>, <code>bar</code>, and <code>baz</code> in some source language might be
  97. represented as a tuple <code>((</code><var>foo contents</var><code>,</code><var>bar
  98. contents</var><code>),</code><var>baz contents</var><code>)</code> on the virtual code level. Compile time
  99. constants like <code>bar = ((nil,(nil,nil)),nil)</code> could be defined in an
  100. effort to hide the details of the representation, so that the virtual
  101. code <code>field bar</code> is used instead of <code>compose(right,left)</code>.
  102. Using field identifiers appropriately, a function that transforms such a
  103. structure by operating on the <code>bar</code> field could have the virtual
  104. <a name="index-g_t_0040code_007bfield_007d-309"></a>code <code>couple(couple(field foo,compose(f,field bar)),field
  105. baz)</code>. However, this code does not avoid depending on the representation
  106. of the data structure, because it relies on the assumption of the <code>foo</code>
  107. field being on the left of the left, and the <code>baz</code> field being on
  108. the right. On the other hand, the code <code>assign(bar,compose(f,field
  109. bar))</code> does the same job without depending on anything but the position
  110. of the <code>bar</code> field. Furthermore, if this position were to change
  111. relative to the others, the code maintenance would be limited to a
  112. recompilation.
  113. </body></html>