Sort.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <html lang="en">
  2. <head>
  3. <title>Sort - 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="List-Combinators.html#List-Combinators" title="List Combinators">
  9. <link rel="prev" href="Reduce.html#Reduce" title="Reduce">
  10. <link rel="next" href="Transfer.html#Transfer" title="Transfer">
  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="Sort"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Transfer.html#Transfer">Transfer</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Reduce.html#Reduce">Reduce</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="List-Combinators.html#List-Combinators">List Combinators</a>
  32. <hr>
  33. </div>
  34. <h5 class="subsubsection">2.7.13.4 Sort</h5>
  35. <p><a name="index-g_t_0040code_007bsort_007d-329"></a>Sorting is a frequently used operation that has the following standard
  36. representation in virtual code.
  37. <dl>
  38. <dt><em>T25</em><dd>[[<code>sort</code>]] <var>p</var> = <code>((nil,nil),((</code><var>p</var><code>,nil),(nil,nil)))</code>
  39. </dl>
  40. <p class="noindent">When an application in this form is evaluated with an operand
  41. representing a list, the result is a sorted version of the list.
  42. <p>The way a list is sorted depends on what order is of interest. For
  43. example, numbers could be sorted in ascending or descending order,
  44. character strings could be sorted lexically or by length, etc.. The
  45. value of <var>p</var> is therefore needed in sorting applications to
  46. specify the order. It contains the virtual code for a partial order
  47. relational operator. This operator can be evaluated with any pair of
  48. items selected from a list, and should have a value of true if the left
  49. one should precede the right under the ordering. For example, if numbers
  50. were to be sorted in ascending order, then <var>p</var> would compute
  51. the &ldquo;less or equal&rdquo; relation, returning true if its operand were a
  52. pair of numbers in which the left is less or equal to the right.
  53. <p>The virtual code semantics for sorting applications is given by these
  54. two properties, wherein <var>p</var> is a non-<code>nil</code> tree, and
  55. <code>insert</code> is a <code>silly</code> mnemonic to be defined next.
  56. <a name="index-g_t_0040code_007binsert_007d-330"></a>
  57. <dl>
  58. <dt><em>P31</em><dd>([[<code>sort</code>]] <var>p</var>) <code>nil</code> = <code>nil</code>
  59. <br><dt><em>P32</em><dd>([[<code>sort</code>]] <var>p</var>) <code>(</code><var>x</var><code>,</code><var>y</var><code>)</code> = ([[<code>insert</code>]] <var>p</var>)
  60. <code>(</code><var>x</var><code>,</code>([[<code>sort</code>]] <var>p</var>) <var>y</var><code>)</code>
  61. </dl>
  62. <p class="noindent">These properties say that the empty list is already sorted, and
  63. a non-empty list is sorted if its tail is sorted and the head is then
  64. inserted in the proper place. This specification of sorting has nothing
  65. to do with the sorting algorithm that <code>avram</code> really uses.
  66. <p>The meaning of insertion is convenient to specify in virtual code
  67. itself. It should satisfy these three equations.
  68. <dl>
  69. <dt><em>E4</em><dd>([[<code>insert</code>]] <var>p</var>) <code>(</code><var>x</var><code>,nil)</code> = <code>(</code><var>x</var><code>,nil)</code>
  70. <br><dt><em>E5</em><dd>([[<code>insert</code>]] <var>p</var>) <code>(</code><var>x</var><code>,(</code><var>y</var><code>,</code><var>z</var><code>))</code> =
  71. <code>(</code><var>y</var><code>,</code>([[<code>insert</code>]] <var>p</var>) <code>(</code><var>x</var><code>,</code><var>z</var><code>))</code>
  72. if <var>p</var><code>(</code><var>x</var><code>,</code><var>y</var><code>)</code> = <code>nil</code>
  73. <br><dt><em>E6</em><dd>([[<code>insert</code>]] <var>p</var>) <code>(</code><var>x</var><code>,(</code><var>y</var><code>,</code><var>z</var><code>)</code>) =
  74. <code>(</code><var>x</var><code>,(</code><var>y</var><code>,</code><var>z</var><code>))</code> if <var>p</var><code>(</code><var>x</var><code>,</code><var>y</var><code>)</code> is a non-<code>nil</code> tree
  75. </dl>
  76. <p class="noindent">Intuitively, the right argument, whether <code>nil</code> or
  77. <code>(</code><var>y</var><code>,</code><var>z</var><code>)</code>, represents a list that is already sorted. The
  78. left argument <var>x</var> therefore only needs to be compared to the
  79. head element, <var>y</var> to ascertain whether or not it belongs at
  80. the beginning. If not, it should be inserted into the tail.
  81. <p>A possible implementation of <code>insert</code> in <code>silly</code> is given in
  82. <a href="Insert.html#Insert">Insert</a>.
  83. </body></html>