{"action":"opened","issue":{"url":"https://api.github.com/repos/guevara/read-it-later/issues/1611","repository_url":"https://api.github.com/repos/guevara/read-it-later","labels_url":"https://api.github.com/repos/guevara/read-it-later/issues/1611/labels{/name}","comments_url":"https://api.github.com/repos/guevara/read-it-later/issues/1611/comments","events_url":"https://api.github.com/repos/guevara/read-it-later/issues/1611/events","html_url":"https://github.com/guevara/read-it-later/issues/1611","id":386041652,"node_id":"MDU6SXNzdWUzODYwNDE2NTI=","number":1611,"title":"How to design co-programs","user":{"login":"guevara","id":228309,"node_id":"MDQ6VXNlcjIyODMwOQ==","avatar_url":"https://avatars1.githubusercontent.com/u/228309?v=4","gravatar_id":"","url":"https://api.github.com/users/guevara","html_url":"https://github.com/guevara","followers_url":"https://api.github.com/users/guevara/followers","following_url":"https://api.github.com/users/guevara/following{/other_user}","gists_url":"https://api.github.com/users/guevara/gists{/gist_id}","starred_url":"https://api.github.com/users/guevara/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guevara/subscriptions","organizations_url":"https://api.github.com/users/guevara/orgs","repos_url":"https://api.github.com/users/guevara/repos","events_url":"https://api.github.com/users/guevara/events{/privacy}","received_events_url":"https://api.github.com/users/guevara/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-11-30T05:18:37Z","updated_at":"2018-11-30T05:18:37Z","closed_at":null,"author_association":"OWNER","body":"How to design co-programs<br>\n<br>\nhttps://ift.tt/2R2mT5p<br>\n<br>\njeremygibbons<br>\n<br>\n<div>\n<div>\n<p>I recently attended the <em>Matthias Felleisen Half-Time Show</em>, a symposium held in Boston on 3rd November in celebration of Matthias&rsquo;s 60th birthday. I was honoured to be able to give a talk there; this post is a record of what I (wished I had) said.</p>\n<h2>Matthias Felleisen</h2>\n<p>Matthias is known for many contributions to the field of Programming Languages. He received the <a href=\"http://www.sigplan.org/Awards/Achievement/\">SIGPLAN Programming Languages Achievement Award</a> in 2012, the citation for which states:</p>\n<blockquote>\n<p>He introduced evaluation contexts as a notation for specifying operational semantics, and progress-and-preservation proofs of type safety, both of which are used in scores of research papers each year, often without citation. His other contributions include small-step operational semantics for control and state, A-normal form, delimited continuations, mixin classes and mixin modules, a fully-abstract semantics for Sequential PCF, web programming techniques, higher-order contracts with blame, and static typing for dynamic languages.</p>\n</blockquote>\n<p>Absent from this list, perhaps because it wasn&rsquo;t <a href=\"https://doi.org/10.1145/2503778.2503791\">brought back into the collective memory until 2013</a>, is a very early presentation of the idea of <a href=\"https://doi.org/10.1007/3-540-57887-0_99\">using effects and handlers for extensible language design</a>.</p>\n<p>However, perhaps most prominent among Matthias&rsquo;s contributions to our field is a long series of projects on teaching introductory programming, from <a href=\"https://teach-scheme.org/\">TeachScheme!</a> through <a href=\"https://programbydesign.org/\">Program By Design</a> to a latest incarnation in the form of the <a href=\"https://htdp.org/\">How to Design Programs</a> textbook (&ldquo;HtDP&rdquo;), co-authored with Robby Findler, Matthew Flatt, and Shriram Krishnamurthi, and now in its second edition. The HtDP book is my focus in this post; I have access only the First Edition, but the <a href=\"https://htdp.org/2018-01-06/Book/\">text of the Second Edition</a> is online. I make no apologies for using Haskell syntax in my examples, but at least that gave Matthias something to shout at!</p>\n<h2>Design Recipes</h2>\n<p>One key aspect of HtDP is the emphasis on <em>design recipes</em> for solving programming tasks. A design recipe is a template for the solution to a problem: a contract for the function, analogous to a type signature (but HtDP takes an untyped approach, so this signature is informal); a statement of purpose; a function header; example inputs and outputs; and a skeleton of the function body. Following the design recipe entails completing the template&mdash;filling in a particular contract, etc&mdash;then fleshing out the function body from its skeleton, and finally testing the resulting program against the initial examples.</p>\n<p>The primary strategy for problem solving in the book is via analysis of the structure of the input. When the input is composite, like a record, the skeleton should name the available fields as likely ingredients of the solution. When the input has &ldquo;mixed data&rdquo;, such as a union type, the skeleton should enumerate the alternatives, leading to a case analysis in the solution. When the input is of a recursive type, the skeleton encapsulates <em>structural recursion</em>&mdash;a case analysis between the base case and the inductive case, the latter case entailing recursive calls.</p>\n<p>So, the <a href=\"https://htdp.org/2003-09-26/Book/curriculum-Z-H-13.html#node_fig_Temp_64\">design recipe for structural recursion</a> looks like this:</p>\n<blockquote>\n<table align=\"center\">\n<tr>\n<td align=\"left\"><b>Phase</b></td>\n<td align=\"left\"><b>Goal</b></td>\n<td align=\"left\"><b>Activity</b></td>\n</tr>\n<tr>\n<td align=\"left\">Data Analysis and Design</td>\n<td align=\"left\">to formulate a data definition</td>\n<td align=\"left\">develop a data definition for mixed data with at least two alternatives; one alternative must not refer to the definition; explicitly identify all self-references in the data definition</td>\n</tr>\n<tr>\n<td align=\"left\">Contract Purpose and Header</td>\n<td align=\"left\">to name the function; to specify its classes of input data and its class of output data; to describe its purpose; to formulate a header</td>\n<td align=\"left\">name the function, the classes of input data, the class of output data, and specify its purpose:\n<p>;; name : in1 in2 &hellip; &ndash;&gt; out</p>\n<p>;; to compute &hellip; from x1 &hellip;</p>\n<p>(define (name x1 x2 &hellip;) &hellip;)</p>\n</td>\n</tr>\n<tr>\n<td align=\"left\">Examples</td>\n<td align=\"left\">to characterize the input-output relationship via examples</td>\n<td align=\"left\">create examples of the input-output relationship; make sure there is at least one example per subclass</td>\n</tr>\n<tr>\n<td align=\"left\">Template</td>\n<td align=\"left\">to formulate an outline</td>\n<td align=\"left\">develop a cond-expression with one clause per alternative; add selector expressions to each clause; annotate the body with natural recursions; Test: the self-references in this template and the data definition match!</td>\n</tr>\n<tr>\n<td align=\"left\">Body</td>\n<td align=\"left\">to define the function</td>\n<td align=\"left\">formulate a Scheme expression for each simple cond-line; explain for all other cond-clauses what each natural recursion computes according to the purpose statement</td>\n</tr>\n<tr>\n<td align=\"left\">Test</td>\n<td align=\"left\">to discover mistakes (&ldquo;typos&rdquo; and logic)</td>\n<td align=\"left\">apply the function to the inputs of the examples; check that the outputs are as predicted</td>\n</tr>\n</table>\n</blockquote>\n<p>The motivating example for structural recursion is <a href=\"https://htdp.org/2003-09-26/Book/curriculum-Z-H-16.html#node_chap_12\">Insertion Sort</a>: recursing on the tail of a non-empty list and inserting the head into the sorted subresult.</p>\n<blockquote>\n<p><img src=\"https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7B%40%7B%7Dll%7D+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7Binsertsort%7D+%3A%3A+%5B%5Cmathit%7BInteger%7D%5D+%5Crightarrow+%5B%5Cmathit%7BInteger%7D%5D%7D+%5C%5C+%5Cmathit%7Binsertsort%7D%5C%3B%5B%5C%2C%5D+%26%3D+%5B%5C%2C%5D+%5C%5C+%5Cmathit%7Binsertsort%7D%5C%3B%28a%3Ax%29+%26%3D+%5Cmathit%7Binsert%7D%5C%3Ba%5C%3B%28%5Cmathit%7Binsertsort%7D%5C%3Bx%29+%5Cmedskip+%5C%5C+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7Binsert%7D+%3A%3A+%5Cmathit%7BInteger%7D+%5Crightarrow+%5B%5Cmathit%7BInteger%7D%5D+%5Crightarrow+%5B%5Cmathit%7BInteger%7D%5D%7D+%5C%5C+%5Cmathit%7Binsert%7D%5C%3Bb%5C%3B%5B%5C%2C%5D+%26%3D+%5Bb%5D+%5C%5C+%5Cmathit%7Binsert%7D%5C%3Bb%5C%3B%28a%3Ax%29+%5C%5C+%5Cqquad+%5Cmid+b+%5Cle+a+%26%3D+b+%3A+a+%3A+x+%5C%5C+%5Cqquad+%5Cmid+b+%3E+a+%26%3D+a+%3A+%5Cmathit%7Binsert%7D%5C%3Bb%5C%3Bx+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{insertsort} :: [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{insertsort}\\;[\\,] &amp;= [\\,] \\\\ \\mathit{insertsort}\\;(a:x) &amp;= \\mathit{insert}\\;a\\;(\\mathit{insertsort}\\;x) \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{insert} :: \\mathit{Integer} \\rightarrow [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{insert}\\;b\\;[\\,] &amp;= [b] \\\\ \\mathit{insert}\\;b\\;(a:x) \\\\ \\qquad \\mid b \\le a &amp;= b : a : x \\\\ \\qquad \\mid b &gt; a &amp;= a : \\mathit{insert}\\;b\\;x \\end{array} \" title=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{insertsort} :: [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{insertsort}\\;[\\,] &amp;= [\\,] \\\\ \\mathit{insertsort}\\;(a:x) &amp;= \\mathit{insert}\\;a\\;(\\mathit{insertsort}\\;x) \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{insert} :: \\mathit{Integer} \\rightarrow [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{insert}\\;b\\;[\\,] &amp;= [b] \\\\ \\mathit{insert}\\;b\\;(a:x) \\\\ \\qquad \\mid b \\le a &amp;= b : a : x \\\\ \\qquad \\mid b &gt; a &amp;= a : \\mathit{insert}\\;b\\;x \\end{array} \" class=\"latex\"></p>\n</blockquote>\n<p>A secondary, more advanced, strategy is to use <em>generative recursion</em>, otherwise known as divide-and-conquer. The skeleton in this design recipe incorporates a test for triviality; in the non-trivial cases, it splits the problem into subproblems, recursively solves the subproblems, and assembles the subresults into an overall result. The motivating example for generative recursion is <a href=\"https://htdp.org/2003-09-26/Book/curriculum-Z-H-32.html#node_sec_25.2\">QuickSort</a> (but not the fast in-place version): dividing a non-empty input list into two parts using the head as the pivot, recursively sorting both parts, and concatenating the results with the pivot in the middle.</p>\n<blockquote>\n<p><img src=\"https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7B%40%7B%7Dll%7D+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7Bquicksort%7D+%3A%3A+%5B%5Cmathit%7BInteger%7D%5D+%5Crightarrow+%5B%5Cmathit%7BInteger%7D%5D%7D+%5C%5C+%5Cmathit%7Bquicksort%7D%5C%3B%5B%5C%2C%5D+%26+%3D+%5B%5C%2C%5D+%5C%5C+%5Cmathit%7Bquicksort%7D%5C%3B%28a%3Ax%29+%26%3D+%5Cmathit%7Bquicksort%7D%5C%3By+%5Cmathbin%7B%7B%2B%7D%5C%21%5C%21%5C%21%7B%2B%7D%7D+%5Ba%5D+%5Cmathbin%7B%7B%2B%7D%5C%21%5C%21%5C%21%7B%2B%7D%7D+%5Cmathit%7Bquicksort%7D%5C%3Bz+%5C%5C+%26+%5Cqquad+%5Cmathbf%7Bwhere%7D%5C%3B+%5Cbegin%7Barray%7D%5Bt%5D%7B%40%7B%7Dll%7D+y+%26%3D+%5B+b+%5Cmid+b+%5Cleftarrow+x%2C+b+%5Cle+a+%5D+%5C%5C+z+%26%3D+%5B+b+%5Cmid+b+%5Cleftarrow+x%2C+b+%3E+a+%5D+%5Cend%7Barray%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{quicksort} :: [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{quicksort}\\;[\\,] &amp; = [\\,] \\\\ \\mathit{quicksort}\\;(a:x) &amp;= \\mathit{quicksort}\\;y \\mathbin{{+}\\!\\!\\!{+}} [a] \\mathbin{{+}\\!\\!\\!{+}} \\mathit{quicksort}\\;z \\\\ &amp; \\qquad \\mathbf{where}\\; \\begin{array}[t]{@{}ll} y &amp;= [ b \\mid b \\leftarrow x, b \\le a ] \\\\ z &amp;= [ b \\mid b \\leftarrow x, b &gt; a ] \\end{array} \\end{array} \" title=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{quicksort} :: [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{quicksort}\\;[\\,] &amp; = [\\,] \\\\ \\mathit{quicksort}\\;(a:x) &amp;= \\mathit{quicksort}\\;y \\mathbin{{+}\\!\\!\\!{+}} [a] \\mathbin{{+}\\!\\!\\!{+}} \\mathit{quicksort}\\;z \\\\ &amp; \\qquad \\mathbf{where}\\; \\begin{array}[t]{@{}ll} y &amp;= [ b \\mid b \\leftarrow x, b \\le a ] \\\\ z &amp;= [ b \\mid b \\leftarrow x, b &gt; a ] \\end{array} \\end{array} \" class=\"latex\"></p>\n</blockquote>\n<p>As far as I can see, no other program structures than structural recursion and generative recursion are considered in HtDP. (Other design recipes are considered, in particular accumulating parameters and imperative features. But these do not determine the gross structure of the resulting program. In fact, I believe that the imperative recipe has been dropped in the Second Edition.)</p>\n<h2>Co-programs</h2>\n<p>My thesis is that HtDP has missed an opportunity to reinforce its core message, that <b>data structure determines program structure</b>. Specifically, I believe that the next design recipe to consider after structural recursion, in which the shape of the program is determined by the shape of the input, should be <em>structural corecursion</em>, in which the shape of the program is determined instead by the shape of the output.</p>\n<p>More concretely, a function that generates &ldquo;mixed output&rdquo;&mdash;whether that is a union type, or simply a boolean&mdash;might be defined by case analysis over the output. A function that generates a record might be composed of subprograms that generate each of the fields of that record. A function that generates a recursive data structure from some input data might be defined with a case analysis as to whether the result is trivial, and for non-trivial cases with recursive calls to generate substructures of the result. HtDP should present explicit design recipes to address these possibilities, as it does for program structures determined by the input data.</p>\n<p>For an example of mixed output, consider a program that may fail, such as division, guarded so as to return an alternative value in that case:</p>\n<blockquote>\n<p><img src=\"https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7B%40%7B%7Dlll%7D+%5Cmulticolumn%7B3%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7BsafeDiv%7D+%3A%3A+%5Cmathit%7BInteger%7D+%5Crightarrow+%5Cmathit%7BInteger%7D+%5Crightarrow+%5Cmathsf%7BMaybe%7D%5C%3B%5Cmathit%7BInteger%7D%7D+%5C%5C+%5Cmathit%7BsafeDiv%7D%5C%3Bx%5C%3By+%26+%5Cmid+y+%3D+0+%26+%3D+%5Cmathit%7BNothing%7D+%5C%5C+%26+%5Cmid+%5Cmathbf%7Botherwise%7D+%26+%3D+%5Cmathit%7BJust%7D%5C%3B%28x+%5Cmathbin%7B%5Cunderline%7B%5Csmash%7B%5Cmathit%7Bdiv%7D%7D%7D%7D+y%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"\\displaystyle \\begin{array}{@{}lll} \\multicolumn{3}{@{}l}{\\mathit{safeDiv} :: \\mathit{Integer} \\rightarrow \\mathit{Integer} \\rightarrow \\mathsf{Maybe}\\;\\mathit{Integer}} \\\\ \\mathit{safeDiv}\\;x\\;y &amp; \\mid y = 0 &amp; = \\mathit{Nothing} \\\\ &amp; \\mid \\mathbf{otherwise} &amp; = \\mathit{Just}\\;(x \\mathbin{\\underline{\\smash{\\mathit{div}}}} y) \\end{array} \" title=\"\\displaystyle \\begin{array}{@{}lll} \\multicolumn{3}{@{}l}{\\mathit{safeDiv} :: \\mathit{Integer} \\rightarrow \\mathit{Integer} \\rightarrow \\mathsf{Maybe}\\;\\mathit{Integer}} \\\\ \\mathit{safeDiv}\\;x\\;y &amp; \\mid y = 0 &amp; = \\mathit{Nothing} \\\\ &amp; \\mid \\mathbf{otherwise} &amp; = \\mathit{Just}\\;(x \\mathbin{\\underline{\\smash{\\mathit{div}}}} y) \\end{array} \" class=\"latex\"></p>\n</blockquote>\n<p>The program performs a case analysis, and of course the analysis depends on the input data; but the analysis is not determined by the <em>structure</em> of the input, only its <em>value</em>. So a better explanation of the program structure is that it is determined by the structure of the output data.</p>\n<p>For an example of generating composite output, consider the problem of extracting a date, represented as a record:</p>\n<blockquote>\n<p><img src=\"https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathbf%7Bdata%7D%5C%3B%5Cmathit%7BDate%7D+%3D+%5Cmathit%7BDate%7D+%5C%7B+%5Cmathit%7Bday%7D+%3A%3A+%5Cmathit%7BDay%7D%2C+%5Cmathit%7Bmonth%7D+%3A%3A+%5Cmathit%7BMonth%7D%2C+%5Cmathit%7Byear%7D+%3A%3A+%5Cmathit%7BYear%7D+%5C%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"\\displaystyle \\mathbf{data}\\;\\mathit{Date} = \\mathit{Date} \\{ \\mathit{day} :: \\mathit{Day}, \\mathit{month} :: \\mathit{Month}, \\mathit{year} :: \\mathit{Year} \\} \" title=\"\\displaystyle \\mathbf{data}\\;\\mathit{Date} = \\mathit{Date} \\{ \\mathit{day} :: \\mathit{Day}, \\mathit{month} :: \\mathit{Month}, \\mathit{year} :: \\mathit{Year} \\} \" class=\"latex\"></p>\n</blockquote>\n<p>from a formatted string. The function is naturally structured to match the output type:</p>\n<blockquote>\n<p><img src=\"https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7B%40%7B%7Dll%7D+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7BreadDate%7D+%3A%3A+%5Cmathit%7BString%7D+%5Crightarrow+%5Cmathit%7BDate%7D%7D+%5C%5C+%5Cmathit%7BreadDate%7D%5C%3Bs+%26+%3D+%5Cmathit%7BDate%7D%5C%3B%5C%7B+%5Cmathit%7Bday%7D+%3D+d%2C+%5Cmathit%7Bmonth%7D+%3D+m%2C+%5Cmathit%7Byear%7D+%3D+y+%5C%7D+%5C%5C+%26+%5Cqquad+%5Cmathbf%7Bwhere%7D%5C%3B+%5Cbegin%7Barray%7D%5Bt%5D%7B%40%7B%7Dll%7D+d+%26+%3D+...+s+...+%5C%5C+m+%26+%3D+...+s+...+%5C%5C+y+%26+%3D+...+s+...+%5Cend%7Barray%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{readDate} :: \\mathit{String} \\rightarrow \\mathit{Date}} \\\\ \\mathit{readDate}\\;s &amp; = \\mathit{Date}\\;\\{ \\mathit{day} = d, \\mathit{month} = m, \\mathit{year} = y \\} \\\\ &amp; \\qquad \\mathbf{where}\\; \\begin{array}[t]{@{}ll} d &amp; = ... s ... \\\\ m &amp; = ... s ... \\\\ y &amp; = ... s ... \\end{array} \\end{array} \" title=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{readDate} :: \\mathit{String} \\rightarrow \\mathit{Date}} \\\\ \\mathit{readDate}\\;s &amp; = \\mathit{Date}\\;\\{ \\mathit{day} = d, \\mathit{month} = m, \\mathit{year} = y \\} \\\\ &amp; \\qquad \\mathbf{where}\\; \\begin{array}[t]{@{}ll} d &amp; = ... s ... \\\\ m &amp; = ... s ... \\\\ y &amp; = ... s ... \\end{array} \\end{array} \" class=\"latex\"></p>\n</blockquote>\n<p>For an example of corecursion, consider the problem of &ldquo;zipping&rdquo; together two input lists to a list of pairs&mdash;taking <img src=\"https://s0.wp.com/latex.php?latex=%7B%5B1%2C2%2C3%5D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{[1,2,3]}\" title=\"{[1,2,3]}\" class=\"latex\"> and <img src=\"https://s0.wp.com/latex.php?latex=%7B%5B4%2C5%2C6%5D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{[4,5,6]}\" title=\"{[4,5,6]}\" class=\"latex\"> to <img src=\"https://s0.wp.com/latex.php?latex=%7B%5B%281%2C4%29%2C%282%2C5%29%2C%283%2C6%29%5D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{[(1,4),(2,5),(3,6)]}\" title=\"{[(1,4),(2,5),(3,6)]}\" class=\"latex\">, and for simplicity let&rsquo;s say pruning the result to the length of the shorter input. One can again solve the problem by case analysis on the input, but the fact that there are two inputs makes that a bit awkward&mdash;whether to do case analysis on one list in favour of the other, or to analyse both in parallel. For example, here is the outcome of case analysis on the first input, followed if that is non-empty by a case analysis on the second input:</p>\n<blockquote>\n<p><img src=\"https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7B%40%7B%7Dll%7D+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7Bzip%7D+%3A%3A+%5B%5Calpha%5D+%5Crightarrow+%5B%5Cbeta%5D+%5Crightarrow+%5B%28%5Calpha%2C%5Cbeta%29%5D%7D+%5C%5C+%5Cmathit%7Bzip%7D%5C%3Bx%5C%3By+%26%3D+%5Cbegin%7Barray%7D%5Bt%5D%7B%40%7B%7Dl%7D+%5Cmathbf%7Bif%7D%5C%3B+%5Cmathit%7Bnull%7D%5C%3Bx+%5C%3B%5Cmathbf%7Bthen%7D%5C%3B+%5B%5C%2C%5D+%5C%3B%5Cmathbf%7Belse%7D+%5C%5C+%5Cqquad+%5Cmathbf%7Bif%7D%5C%3B+%5Cmathit%7Bnull%7D%5C%3By+%5C%3B%5Cmathbf%7Bthen%7D%5C%3B+%5B%5C%2C%5D+%5C%3B%5Cmathbf%7Belse%7D+%5C%5C+%5Cqquad+%5Cqquad+%28%5Cmathit%7Bhead%7D%5C%3Bx%2C%5Cmathit%7Bhead%7D%5C%3By%29+%3A+%5Cmathit%7Bzip%7D%5C%3B%28%5Cmathit%7Btail%7D%5C%3Bx%29%5C%3B%28%5Cmathit%7Btail%7D%5C%3By%29+%5Cend%7Barray%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{zip} :: [\\alpha] \\rightarrow [\\beta] \\rightarrow [(\\alpha,\\beta)]} \\\\ \\mathit{zip}\\;x\\;y &amp;= \\begin{array}[t]{@{}l} \\mathbf{if}\\; \\mathit{null}\\;x \\;\\mathbf{then}\\; [\\,] \\;\\mathbf{else} \\\\ \\qquad \\mathbf{if}\\; \\mathit{null}\\;y \\;\\mathbf{then}\\; [\\,] \\;\\mathbf{else} \\\\ \\qquad \\qquad (\\mathit{head}\\;x,\\mathit{head}\\;y) : \\mathit{zip}\\;(\\mathit{tail}\\;x)\\;(\\mathit{tail}\\;y) \\end{array} \\end{array} \" title=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{zip} :: [\\alpha] \\rightarrow [\\beta] \\rightarrow [(\\alpha,\\beta)]} \\\\ \\mathit{zip}\\;x\\;y &amp;= \\begin{array}[t]{@{}l} \\mathbf{if}\\; \\mathit{null}\\;x \\;\\mathbf{then}\\; [\\,] \\;\\mathbf{else} \\\\ \\qquad \\mathbf{if}\\; \\mathit{null}\\;y \\;\\mathbf{then}\\; [\\,] \\;\\mathbf{else} \\\\ \\qquad \\qquad (\\mathit{head}\\;x,\\mathit{head}\\;y) : \\mathit{zip}\\;(\\mathit{tail}\\;x)\\;(\\mathit{tail}\\;y) \\end{array} \\end{array} \" class=\"latex\"></p>\n</blockquote>\n<p>Case analysis on both inputs would lead to four cases rather than three, which would not be an improvement. One can instead solve the problem by case analysis on the output&mdash;and it is arguably more natural to do so, because there is only one output rather than two. When is the output empty? (When either input is empty.) If it isn&rsquo;t empty, what is the head of the output? (The pair of input heads.) And from what data is the tail of the output recursively constructed? (The pair of input tails.)</p>\n<blockquote>\n<p><img src=\"https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7B%40%7B%7Dll%7D+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7Bzip%7D+%3A%3A+%5B%5Calpha%5D+%5Crightarrow+%5B%5Cbeta%5D+%5Crightarrow+%5B%28%5Calpha%2C%5Cbeta%29%5D%7D+%5C%5C+%5Cmathit%7Bzip%7D%5C%3Bx%5C%3By+%26%3D+%5Cbegin%7Barray%7D%5Bt%5D%7B%40%7B%7Dl%7D+%5Cmathbf%7Bif%7D%5C%3B+%5Cmathit%7Bnull%7D%5C%3Bx+%5Clor+%5Cmathit%7Bnull%7D%5C%3By+%5C%3B%5Cmathbf%7Bthen%7D%5C%3B+%5B%5C%2C%5D+%5C%3B%5Cmathbf%7Belse%7D+%5C%5C+%5Cqquad+%28%5Cmathit%7Bhead%7D%5C%3Bx%2C%5Cmathit%7Bhead%7D%5C%3By%29+%3A+%5Cmathit%7Bzip%7D%5C%3B%28%5Cmathit%7Btail%7D%5C%3Bx%29%5C%3B%28%5Cmathit%7Btail%7D%5C%3By%29+%5Cend%7Barray%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{zip} :: [\\alpha] \\rightarrow [\\beta] \\rightarrow [(\\alpha,\\beta)]} \\\\ \\mathit{zip}\\;x\\;y &amp;= \\begin{array}[t]{@{}l} \\mathbf{if}\\; \\mathit{null}\\;x \\lor \\mathit{null}\\;y \\;\\mathbf{then}\\; [\\,] \\;\\mathbf{else} \\\\ \\qquad (\\mathit{head}\\;x,\\mathit{head}\\;y) : \\mathit{zip}\\;(\\mathit{tail}\\;x)\\;(\\mathit{tail}\\;y) \\end{array} \\end{array} \" title=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{zip} :: [\\alpha] \\rightarrow [\\beta] \\rightarrow [(\\alpha,\\beta)]} \\\\ \\mathit{zip}\\;x\\;y &amp;= \\begin{array}[t]{@{}l} \\mathbf{if}\\; \\mathit{null}\\;x \\lor \\mathit{null}\\;y \\;\\mathbf{then}\\; [\\,] \\;\\mathbf{else} \\\\ \\qquad (\\mathit{head}\\;x,\\mathit{head}\\;y) : \\mathit{zip}\\;(\\mathit{tail}\\;x)\\;(\\mathit{tail}\\;y) \\end{array} \\end{array} \" class=\"latex\"></p>\n</blockquote>\n<p>And whereas Insertion Sort is a structural recursion over the input list, inserting elements one by one into a sorted intermediate result, <em>Selection Sort</em> is a structural corecursion towards the output list, repeatedly extracting the minimum remaining element as the next element of the output: When is the output empty? (When the input is empty.) If the output isn&rsquo;t empty, what is its head? (The minimum of the input.) And from what data is the tail recursively generated? (The input without this minimum element.)</p>\n<blockquote>\n<p><img src=\"https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7B%40%7B%7Dll%7D+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7BselectSort%7D+%3A%3A+%5B%5Cmathit%7BInteger%7D%5D+%5Crightarrow+%5B%5Cmathit%7BInteger%7D%5D%7D+%5C%5C+%5Cmathit%7BselectSort%7D%5C%3Bx+%26+%3D+%5Cmathbf%7Bif%7D%5C%3B+%5Cmathit%7Bnull%7D%5C%3Bx+%5C%3B%5Cmathbf%7Bthen%7D%5C%3B+%5B%5C%2C%5D+%5C%3B%5Cmathbf%7Belse%7D%5C%3B+a+%3A+%5Cmathit%7BselectSort%7D%5C%3B%28x+%5Cmathbin%7B%5Cbackslash%5C%21%5Cbackslash%7D+%5Ba%5D%29+%5C%5C+%26+%5Cqquad+%5Cmathbf%7Bwhere%7D%5C%3B+a+%3D+%5Cmathit%7Bminimum%7D%5C%3Bx+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{selectSort} :: [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{selectSort}\\;x &amp; = \\mathbf{if}\\; \\mathit{null}\\;x \\;\\mathbf{then}\\; [\\,] \\;\\mathbf{else}\\; a : \\mathit{selectSort}\\;(x \\mathbin{\\backslash\\!\\backslash} [a]) \\\\ &amp; \\qquad \\mathbf{where}\\; a = \\mathit{minimum}\\;x \\end{array} \" title=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{selectSort} :: [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{selectSort}\\;x &amp; = \\mathbf{if}\\; \\mathit{null}\\;x \\;\\mathbf{then}\\; [\\,] \\;\\mathbf{else}\\; a : \\mathit{selectSort}\\;(x \\mathbin{\\backslash\\!\\backslash} [a]) \\\\ &amp; \\qquad \\mathbf{where}\\; a = \\mathit{minimum}\\;x \\end{array} \" class=\"latex\"></p>\n</blockquote>\n<p>(here, <img src=\"https://s0.wp.com/latex.php?latex=%7Bx+%5Cmathbin%7B%5Cbackslash%5C%21%5Cbackslash%7D+y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{x \\mathbin{\\backslash\\!\\backslash} y}\" title=\"{x \\mathbin{\\backslash\\!\\backslash} y}\" class=\"latex\"> denotes list <img src=\"https://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{x}\" title=\"{x}\" class=\"latex\"> with the elements of list <img src=\"https://s0.wp.com/latex.php?latex=%7By%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{y}\" title=\"{y}\" class=\"latex\"> removed).</p>\n<p>I&rsquo;m not alone in making this assertion. Norman Ramsey wrote a nice paper <a href=\"http://doi.acm.org/10.1145/2628136.2628137\">On Teaching HtDP</a>; his experience led him to the lesson that</p>\n<blockquote>\n<p>Last, and rarely, you could design a function&rsquo;s template around the introduction form for the result type. When I teach [HtDP] again, I will make my students aware of this decision point in the construction of a function&rsquo;s template: should they use elimination forms, function composition, or an introduction form? They should use elimination forms usually, function composition sometimes, and an introduction form rarely.</p>\n</blockquote>\n<p>He also elaborates on test coverage:</p>\n<blockquote>\n<p>Check functional examples to be sure every choice of input is represented. Check functional examples to be sure every choice of <em>output</em> is represented. This activity is especially valuable for functions returning Booleans.</p>\n</blockquote>\n<p>(his emphasis). We should pay attention to output data structure as well as to input data structure.</p>\n<h2>Generative recursion, aka divide-and-conquer</h2>\n<p>Only once this dual form of program structure has been explored should students be encouraged to move on to generative recursion, because this exploits both structural recursion and structural corecursion. For example, the QuickSort algorithm that is used as the main motivating example is really structured as a corecursion <img src=\"https://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bbuild%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{\\mathit{build}}\" title=\"{\\mathit{build}}\" class=\"latex\"> to construct an intermediate tree, followed by structural recursion <img src=\"https://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bflatten%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{\\mathit{flatten}}\" title=\"{\\mathit{flatten}}\" class=\"latex\"> over that tree to produce the resulting list:</p>\n<blockquote>\n<p><img src=\"https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7B%40%7B%7Dll%7D+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7Bquicksort%7D+%3A%3A+%5B%5Cmathit%7BInteger%7D%5D+%5Crightarrow+%5B%5Cmathit%7BInteger%7D%5D%7D+%5C%5C+%5Cmathit%7Bquicksort%7D+%26+%3D+%5Cmathit%7Bflatten%7D+%5Ccdot+%5Cmathit%7Bbuild%7D+%5Cmedskip+%5C%5C+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathbf%7Bdata%7D%5C%3B%5Cmathit%7BTree%7D+%3D+%5Cmathit%7BEmpty%7D+%5Cmid+%5Cmathit%7BNode%7D%5C%3B%5Cmathit%7BTree%7D%5C%3B%5Cmathit%7BInteger%7D%5C%3B%5Cmathit%7BTree%7D%7D+%5Cmedskip%5C%5C+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7Bbuild%7D+%3A%3A+%5B%5Cmathit%7BInteger%7D%5D+%5Crightarrow+%5Cmathit%7BTree%7D%7D+%5C%5C+%5Cmathit%7Bbuild%7D%5C%3B%5B%5C%2C%5D+%26+%3D+%5Cmathit%7BEmpty%7D+%5C%5C+%5Cmathit%7Bbuild%7D%5C%3B%28a%3Ax%29+%26%3D+%5Cmathit%7BNode%7D%5C%3B%28%5Cmathit%7Bbuild%7D%5C%3By%29%5C%3Ba%5C%3B%28%5Cmathit%7Bbuild%7D%5C%3Bz%29+%5C%5C+%26+%5Cqquad+%5Cmathbf%7Bwhere%7D%5C%3B+%5Cbegin%7Barray%7D%5Bt%5D%7B%40%7B%7Dll%7D+y+%26%3D+%5B+b+%5Cmid+b+%5Cleftarrow+x%2C+b+%5Cle+a+%5D+%5C%5C+z+%26%3D+%5B+b+%5Cmid+b+%5Cleftarrow+x%2C+b+%3E+a+%5D+%5Cend%7Barray%7D+%5Cmedskip+%5C%5C+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7Bflatten%7D+%3A%3A+%5Cmathit%7BTree%7D+%5Crightarrow+%5B%5Cmathit%7BInteger%7D%5D%7D+%5C%5C+%5Cmathit%7Bflatten%7D%5C%3B%5Cmathit%7BEmpty%7D+%26+%3D+%5B%5C%2C%5D+%5C%5C+%5Cmathit%7Bflatten%7D%5C%3B%28%5Cmathit%7BNode%7D%5C%3Bt%5C%3Ba%5C%3Bu%29+%26%3D+%5Cmathit%7Bflatten%7D%5C%3Bt+%5Cmathbin%7B%7B%2B%7D%5C%21%5C%21%5C%21%7B%2B%7D%7D+%5Ba%5D+%5Cmathbin%7B%7B%2B%7D%5C%21%5C%21%5C%21%7B%2B%7D%7D+%5Cmathit%7Bflatten%7D%5C%3Bu+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{quicksort} :: [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{quicksort} &amp; = \\mathit{flatten} \\cdot \\mathit{build} \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathbf{data}\\;\\mathit{Tree} = \\mathit{Empty} \\mid \\mathit{Node}\\;\\mathit{Tree}\\;\\mathit{Integer}\\;\\mathit{Tree}} \\medskip\\\\ \\multicolumn{2}{@{}l}{\\mathit{build} :: [\\mathit{Integer}] \\rightarrow \\mathit{Tree}} \\\\ \\mathit{build}\\;[\\,] &amp; = \\mathit{Empty} \\\\ \\mathit{build}\\;(a:x) &amp;= \\mathit{Node}\\;(\\mathit{build}\\;y)\\;a\\;(\\mathit{build}\\;z) \\\\ &amp; \\qquad \\mathbf{where}\\; \\begin{array}[t]{@{}ll} y &amp;= [ b \\mid b \\leftarrow x, b \\le a ] \\\\ z &amp;= [ b \\mid b \\leftarrow x, b &gt; a ] \\end{array} \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{flatten} :: \\mathit{Tree} \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{flatten}\\;\\mathit{Empty} &amp; = [\\,] \\\\ \\mathit{flatten}\\;(\\mathit{Node}\\;t\\;a\\;u) &amp;= \\mathit{flatten}\\;t \\mathbin{{+}\\!\\!\\!{+}} [a] \\mathbin{{+}\\!\\!\\!{+}} \\mathit{flatten}\\;u \\end{array} \" title=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{quicksort} :: [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{quicksort} &amp; = \\mathit{flatten} \\cdot \\mathit{build} \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathbf{data}\\;\\mathit{Tree} = \\mathit{Empty} \\mid \\mathit{Node}\\;\\mathit{Tree}\\;\\mathit{Integer}\\;\\mathit{Tree}} \\medskip\\\\ \\multicolumn{2}{@{}l}{\\mathit{build} :: [\\mathit{Integer}] \\rightarrow \\mathit{Tree}} \\\\ \\mathit{build}\\;[\\,] &amp; = \\mathit{Empty} \\\\ \\mathit{build}\\;(a:x) &amp;= \\mathit{Node}\\;(\\mathit{build}\\;y)\\;a\\;(\\mathit{build}\\;z) \\\\ &amp; \\qquad \\mathbf{where}\\; \\begin{array}[t]{@{}ll} y &amp;= [ b \\mid b \\leftarrow x, b \\le a ] \\\\ z &amp;= [ b \\mid b \\leftarrow x, b &gt; a ] \\end{array} \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{flatten} :: \\mathit{Tree} \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{flatten}\\;\\mathit{Empty} &amp; = [\\,] \\\\ \\mathit{flatten}\\;(\\mathit{Node}\\;t\\;a\\;u) &amp;= \\mathit{flatten}\\;t \\mathbin{{+}\\!\\!\\!{+}} [a] \\mathbin{{+}\\!\\!\\!{+}} \\mathit{flatten}\\;u \\end{array} \" class=\"latex\"></p>\n</blockquote>\n<p>The structure of both functions <img src=\"https://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bbuild%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{\\mathit{build}}\" title=\"{\\mathit{build}}\" class=\"latex\"> and <img src=\"https://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bflatten%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{\\mathit{flatten}}\" title=\"{\\mathit{flatten}}\" class=\"latex\"> is determined by the structure of the intermediate <img src=\"https://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BTree%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{\\mathit{Tree}}\" title=\"{\\mathit{Tree}}\" class=\"latex\"> datatype, the first as structural corecursion and the second as structural recursion. A similar explanation applies to any divide-and-conquer algorithm; for example, MergeSort is another divide-and-conquer sorting algorithm, with the same intermediate tree shape, but this time with a simple splitting phase and all the comparisons in the recombining phase:</p>\n<blockquote>\n<p><img src=\"https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7B%40%7B%7Dll%7D+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7Bmergesort%7D+%3A%3A+%5B%5Cmathit%7BInteger%7D%5D+%5Crightarrow+%5B%5Cmathit%7BInteger%7D%5D%7D+%5C%5C+%5Cmathit%7Bmergesort%7D+%26+%3D+%5Cmathit%7BmergeAll%7D+%5Ccdot+%5Cmathit%7BsplitUp%7D+%5Cmedskip+%5C%5C+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7BsplitUp%7D+%3A%3A+%5B%5Cmathit%7BInteger%7D%5D+%5Crightarrow+%5Cmathit%7BTree%7D%7D+%5C%5C+%5Cmathit%7BsplitUp%7D%5C%3B%5B%5C%2C%5D+%26+%3D+%5Cmathit%7BEmpty%7D+%5C%5C+%5Cmathit%7BsplitUp%7D%5C%3B%28a%3Ax%29+%26%3D+%5Cmathit%7BNode%7D%5C%3B%28%5Cmathit%7BsplitUp%7D%5C%3By%29%5C%3Ba%5C%3B%28%5Cmathit%7BsplitUp%7D%5C%3Bz%29+%5C%5C+%26+%5Cqquad+%5Cmathbf%7Bwhere%7D%5C%3B+%28y%2Cz%29+%3D+%5Cmathit%7Bhalve%7D%5C%3Bx+%5Cmedskip+%5C%5C+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7Bhalve%7D+%3A%3A+%5B%5Cmathit%7BInteger%7D%5D+%5Crightarrow+%28%5B%5Cmathit%7BInteger%7D%5D%2C%5B%5Cmathit%7BInteger%7D%5D%29%7D+%5C%5C+%5Cmathit%7Bhalve%7D%5C%3B%5B%5C%2C%5D+%26+%3D+%28+%5B%5C%2C%5D%2C+%5B%5C%2C%5D%29+%5C%5C+%5Cmathit%7Bhalve%7D%5C%3B%5Ba%5D+%26+%3D+%28+%5Ba%5D%2C+%5B%5C%2C%5D%29+%5C%5C+%5Cmathit%7Bhalve%7D%5C%3B%28a%3Ab%3Ax%29+%26+%3D+%28a%3Ay%2Cb%3Az%29+%5C%3B%5Cmathbf%7Bwhere%7D%5C%3B+%28y%2Cz%29+%3D+%5Cmathit%7Bhalve%7D%5C%3Bx+%5Cmedskip+%5C%5C+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7BmergeAll%7D+%3A%3A+%5Cmathit%7BTree%7D+%5Crightarrow+%5B%5Cmathit%7BInteger%7D%5D%7D+%5C%5C+%5Cmathit%7BmergeAll%7D%5C%3B%5Cmathit%7BEmpty%7D+%26+%3D+%5B%5C%2C%5D+%5C%5C+%5Cmathit%7BmergeAll%7D%5C%3B%28%5Cmathit%7BNode%7D%5C%3Bt%5C%3Ba%5C%3Bu%29+%26%3D+%5Cmathit%7Bmerge%7D%5C%3B%28%5Cmathit%7BmergeAll%7D%5C%3Bt%29%5C%3B%28%5Cmathit%7Bmerge%7D%5C%3B%5Ba%5D%5C%3B%28%5Cmathit%7BmergeAll%7D%5C%3Bu%29%29+%5Cmedskip+%5C%5C+%5Cmulticolumn%7B2%7D%7B%40%7B%7Dl%7D%7B%5Cmathit%7Bmerge%7D+%3A%3A+%28%5B%5Cmathit%7BInteger%7D%5D%2C%5B%5Cmathit%7BInteger%7D%5D%29+%5Crightarrow+%5B%5Cmathit%7BInteger%7D%5D%7D+%5C%5C+%5Cmathit%7Bmerge%7D%5C%3B%5B%5C%2C%5D%5C%3By+%26+%3D+y+%5C%5C+%5Cmathit%7Bmerge%7D%5C%3Bx%5C%3B%5B%5C%2C%5D+%26+%3D+x+%5C%5C+%5Cmathit%7Bmerge%7D%5C%3B%28a%3Ax%29%5C%3B%28b%3Ay%29+%26+%3D+%5Cbegin%7Barray%7D%5Bt%5D%7B%40%7B%7Dl%40%7B%7Dl%40%7B%7Dl%7D+%5Cmathbf%7Bif%7D%5C%3B+a+%5Cle+b%5C%3B+%26+%5Cmathbf%7Bthen%7D%5C%3B+%26+a+%3A+%5Cmathit%7Bmerge%7D%5C%3Bx%5C%3B%28b%3Ay%29+%5C%5C+%26+%5Cmathbf%7Belse%7D%5C%3B+%26+b+%3A+%5Cmathit%7Bmerge%7D%5C%3B%28a%3Ax%29%5C%3By+%5Cend%7Barray%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{mergesort} :: [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{mergesort} &amp; = \\mathit{mergeAll} \\cdot \\mathit{splitUp} \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{splitUp} :: [\\mathit{Integer}] \\rightarrow \\mathit{Tree}} \\\\ \\mathit{splitUp}\\;[\\,] &amp; = \\mathit{Empty} \\\\ \\mathit{splitUp}\\;(a:x) &amp;= \\mathit{Node}\\;(\\mathit{splitUp}\\;y)\\;a\\;(\\mathit{splitUp}\\;z) \\\\ &amp; \\qquad \\mathbf{where}\\; (y,z) = \\mathit{halve}\\;x \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{halve} :: [\\mathit{Integer}] \\rightarrow ([\\mathit{Integer}],[\\mathit{Integer}])} \\\\ \\mathit{halve}\\;[\\,] &amp; = ( [\\,], [\\,]) \\\\ \\mathit{halve}\\;[a] &amp; = ( [a], [\\,]) \\\\ \\mathit{halve}\\;(a:b:x) &amp; = (a:y,b:z) \\;\\mathbf{where}\\; (y,z) = \\mathit{halve}\\;x \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{mergeAll} :: \\mathit{Tree} \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{mergeAll}\\;\\mathit{Empty} &amp; = [\\,] \\\\ \\mathit{mergeAll}\\;(\\mathit{Node}\\;t\\;a\\;u) &amp;= \\mathit{merge}\\;(\\mathit{mergeAll}\\;t)\\;(\\mathit{merge}\\;[a]\\;(\\mathit{mergeAll}\\;u)) \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{merge} :: ([\\mathit{Integer}],[\\mathit{Integer}]) \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{merge}\\;[\\,]\\;y &amp; = y \\\\ \\mathit{merge}\\;x\\;[\\,] &amp; = x \\\\ \\mathit{merge}\\;(a:x)\\;(b:y) &amp; = \\begin{array}[t]{@{}l@{}l@{}l} \\mathbf{if}\\; a \\le b\\; &amp; \\mathbf{then}\\; &amp; a : \\mathit{merge}\\;x\\;(b:y) \\\\ &amp; \\mathbf{else}\\; &amp; b : \\mathit{merge}\\;(a:x)\\;y \\end{array} \\end{array} \" title=\"\\displaystyle \\begin{array}{@{}ll} \\multicolumn{2}{@{}l}{\\mathit{mergesort} :: [\\mathit{Integer}] \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{mergesort} &amp; = \\mathit{mergeAll} \\cdot \\mathit{splitUp} \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{splitUp} :: [\\mathit{Integer}] \\rightarrow \\mathit{Tree}} \\\\ \\mathit{splitUp}\\;[\\,] &amp; = \\mathit{Empty} \\\\ \\mathit{splitUp}\\;(a:x) &amp;= \\mathit{Node}\\;(\\mathit{splitUp}\\;y)\\;a\\;(\\mathit{splitUp}\\;z) \\\\ &amp; \\qquad \\mathbf{where}\\; (y,z) = \\mathit{halve}\\;x \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{halve} :: [\\mathit{Integer}] \\rightarrow ([\\mathit{Integer}],[\\mathit{Integer}])} \\\\ \\mathit{halve}\\;[\\,] &amp; = ( [\\,], [\\,]) \\\\ \\mathit{halve}\\;[a] &amp; = ( [a], [\\,]) \\\\ \\mathit{halve}\\;(a:b:x) &amp; = (a:y,b:z) \\;\\mathbf{where}\\; (y,z) = \\mathit{halve}\\;x \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{mergeAll} :: \\mathit{Tree} \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{mergeAll}\\;\\mathit{Empty} &amp; = [\\,] \\\\ \\mathit{mergeAll}\\;(\\mathit{Node}\\;t\\;a\\;u) &amp;= \\mathit{merge}\\;(\\mathit{mergeAll}\\;t)\\;(\\mathit{merge}\\;[a]\\;(\\mathit{mergeAll}\\;u)) \\medskip \\\\ \\multicolumn{2}{@{}l}{\\mathit{merge} :: ([\\mathit{Integer}],[\\mathit{Integer}]) \\rightarrow [\\mathit{Integer}]} \\\\ \\mathit{merge}\\;[\\,]\\;y &amp; = y \\\\ \\mathit{merge}\\;x\\;[\\,] &amp; = x \\\\ \\mathit{merge}\\;(a:x)\\;(b:y) &amp; = \\begin{array}[t]{@{}l@{}l@{}l} \\mathbf{if}\\; a \\le b\\; &amp; \\mathbf{then}\\; &amp; a : \\mathit{merge}\\;x\\;(b:y) \\\\ &amp; \\mathbf{else}\\; &amp; b : \\mathit{merge}\\;(a:x)\\;y \\end{array} \\end{array} \" class=\"latex\"></p>\n</blockquote>\n<p>(Choosing this particular tree type is a bit clunky for Merge Sort, because of the two calls to <img src=\"https://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmerge%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{\\mathit{merge}}\" title=\"{\\mathit{merge}}\" class=\"latex\"> required in <img src=\"https://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BmergeAll%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"{\\mathit{mergeAll}}\" title=\"{\\mathit{mergeAll}}\" class=\"latex\">. It would be neater to use non-empty externally labelled binary trees, with elements at the leaves and none at the branches:</p>\n<blockquote>\n<p><img src=\"https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7B%40%7B%7Dl%7D+%5Cmathbf%7Bdata%7D%5C%3B%5Cmathit%7BTree%7D_2+%3D+%5Cmathit%7BTip%7D%5C%3B%5Cmathit%7BInteger%7D+%5Cmid+%5Cmathit%7BBin%7D%5C%3B%5Cmathit%7BTree%7D_2%5C%3B%5Cmathit%7BTree%7D_2+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0\" alt=\"\\displaystyle \\begin{array}{@{}l} \\mathbf{data}\\;\\mathit{Tree}_2 = \\mathit{Tip}\\;\\mathit{Integer} \\mid \\mathit{Bin}\\;\\mathit{Tree}_2\\;\\mathit{Tree}_2 \\end{array} \" title=\"\\displaystyle \\begin{array}{@{}l} \\mathbf{data}\\;\\mathit{Tree}_2 = \\mathit{Tip}\\;\\mathit{Integer} \\mid \\mathit{Bin}\\;\\mathit{Tree}_2\\;\\mathit{Tree}_2 \\end{array} \" class=\"latex\"></p>\n</blockquote>\n<p>Then you define the main function to work only for non-empty lists, and provide a separate case for sorting the empty list. This clunkiness is a learning opportunity: to realise the problem, come up with a fix (no node labels in the tree), rearrange the furniture accordingly, then replay the development and compare the results.)</p>\n<p>Having identified the two parts, structural recursion and structural corecursion, they may be studied separately; separation of concerns is a crucial lesson in introductory programming. Moreover, the parts may be put together in different ways. The divide-and-conquer pattern is known in the MPC community as a <a href=\"https://doi.org/10.1007/3540543961_7\">hylomorphism</a>, an unfold to generate a call tree followed by a fold to consume that tree. As the QuickSort example suggests, the tree can always be <a href=\"https://doi.org/10.1007/3-540-19027-9_23\">deforested</a>&mdash;it is a <a href=\"https://doi.org/10.1007/3-540-57499-9_26\">virtual data structure</a>. But the converse pattern, of a fold from some structured input to some intermediate value, followed by an unfold to a different structured output, is also interesting&mdash;you can see this as a change of structured representation, so I called it a <a href=\"http://www.cs.ox.ac.uk/publications/publication380-abstract.html\">metamorphism</a>. One simple application is to convert a number from an input base (a sequence of digits in that base), via an intermediate representation (the represented number), to an output base (a different sequence of digits). More interesting applications include encoding and data compression algorithms, such as <a href=\"https://patternsinfp.wordpress.com/2017/12/05/arithmetic-coding/\">arithmetic coding</a>.</p>\n<h2>Laziness</h2>\n<p>Although I have used Haskell as a notation, nothing above depends on laziness; it would all work as well in ML or Scheme. It is true that the mathematical structures underlying structural recursion and structural corecursion are prettier when you admit infinite data structures&mdash;the final coalgebra of the base functor for lists is the datatype of finite <em>and infinite</em> lists, and without admitting the infinite ones some recursive definitions have no solution. But that sophistication is beyond the scope of introductory programming, and it suffices to restrict attention to finite data structures.</p>\n<p>HtDP already stipulates a termination argument in the design recipe for generative recursion; the same kind of argument should be required for structural corecursion (and is easy to make for the sorting examples given above). Of course, structural recursion over finite data structures is necessarily terminating. Laziness is unnecessary for co-programming.</p>\n<h2>Structured programming</h2>\n<p>HtDP actually draws on a long tradition on relating data structure and program structure, which is a theme dear to my own heart (and indeed, the motto of this blog). Tony Hoare wrote in his <a href=\"https://dl.acm.org/citation.cfm?id=1243382\">Notes on Data Structuring</a> in 1972:</p>\n<blockquote>\n<p>There are certain close analogies between the methods used for structuring data and the methods for structuring a program which processes that data.</p>\n</blockquote>\n<p>Fred Brooks put it pithily in <a href=\"https://en.wikipedia.org/wiki/The_Mythical_Man-Month\">The Mythical Man-Month</a> in 1975:</p>\n<blockquote>\n<p>Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won&rsquo;t usually need your flowcharts; they&rsquo;ll be obvious.</p>\n</blockquote>\n<p>which was modernized by Eric Raymond in his 1997 essay <a href=\"https://en.wikipedia.org/wiki/The_Cathedral_and_the_Bazaar\">The Cathedral and the Bazaar</a> to:</p>\n<blockquote>\n<p>Show me your code and conceal your data structures, and I shall continue to be mystified. Show me your data structures, and I won&rsquo;t usually need your code; it&rsquo;ll be obvious.</p>\n</blockquote>\n<p>HtDP credits <a href=\"https://en.wikipedia.org/wiki/Jackson_structured_programming\">Jackson Structured Programming</a> as partial inspiration for the design recipe approach. As <a href=\"https://archive.org/details/principlesofprog00jack\">Jackson wrote, also in 1975</a>:</p>\n<blockquote>\n<p>The central theme of this book has been the relationship between data and program structures. The data provides a model of the problem environment, and by basing our program structures on data structures we ensure that our programs will be intelligible and easy to maintain.</p>\n</blockquote>\n<p>and</p>\n<blockquote>\n<p>The structure of a program must be based on the structures of <em>all</em> of the data it processes.</p>\n</blockquote>\n<p>(my emphasis). In a <a href=\"http://mcs.open.ac.uk/mj665/JSPPers1.pdf\">retrospective lecture in 2001</a>, he clarified:</p>\n<blockquote>\n<p>program structure should be dictated by the structure of its input and output data streams</p>\n</blockquote>\n<p>&mdash;the JSP approach was designed for processing sequential streams of input records to similar streams of output records, and the essence of the approach is to identify the structures of each of the data files (input and output) in terms of sequences, selections, and iterations (that is, as regular languages), to refine them all to a common structure that matches all of them simultaneously, and to use that common data structure as the program structure. So even way back in 1975 it was clear that we need to pay attention to the structure of output data as well as to that of input data.</p>\n<h2>Ad-hockery</h2>\n<p>HtDP apologizes that generative recursion (which it identifies with the field of algorithm design)</p>\n<blockquote>\n<p>is much more of an ad hoc activity than the data-driven design of structurally recursive functions. Indeed, it is almost better to call it inventing an algorithm than designing one. Inventing an algorithm requires a new insight&mdash;a &ldquo;eureka&rdquo;.</p>\n</blockquote>\n<p>It goes on to suggest that mere programmers cannot generally be expected to have such algorithmic insights:</p>\n<blockquote>\n<p>In practice, new complex algorithms are often developed by mathematicians and mathematical computer scientists; programmers, though, must th[o]roughly understand the underlying ideas so that they can invent the simple algorithms on their own and communicate with scientists about the others.</p>\n</blockquote>\n<p>I say that this defeatism is a consequence of not following through on the core message, that data structure determines program structure. QuickSort and MergeSort are not ad hoc. Admittedly, they do require some insight in order to identify structure that is not present in either the input or the output. But having identified that structure, there is no further mystery, and no ad-hockery required.</p>\n<p id=\"atatags-370373-5bf8019588e08\"></p>\n</div>\n</div>\n<br>\n<br>\n<br>\n<br>\n<br>\n<br>\nvia Patterns in Functional Programming https://ift.tt/1of8G6g<br>\n<br>\nNovember 22, 2018 at 02:57AM"}}
{"actor":{"display_login":"guevara"}}