OSDN Git Service

Some work on docs.
[joypy/Thun.git] / docs / Recursion_Combinators.ipynb
1 {
2  "cells": [
3   {
4    "cell_type": "markdown",
5    "metadata": {},
6    "source": [
7     "# Recursion Combinators\n",
8     "\n",
9     "This article describes the `genrec` combinator and how to use it, and then gives several specializations.\n",
10     "The `genrec` combinator isn't the only way to make recursive functions in Joy, you can also use the `x` combinator with a `branch` to create recursive functions.  (Because definitions aren't checked for self-reference you can also define recursive functions directly in definitions, but this, I believe, should be considered bad form.)"
11    ]
12   },
13   {
14    "cell_type": "markdown",
15    "metadata": {},
16    "source": [
17     "## General Recursive Function\n",
18     "\n",
19     "In Joy recursive functions are defined by the `genrec` combinator which accepts four quoted programs:\n",
20     "\n",
21     "    F == [if] [then] [rec1] [rec2] genrec\n",
22     "\n",
23     "This can be thought of as transforming into an \"if..then..else\" expression using the `ifte` combinator and containing a quoted copy of itself in the \"else\" branch:\n",
24     "\n",
25     "    F == [if] [then] [rec1 [F] rec2] ifte\n",
26     "\n",
27     "From [\"Recursion Theory and Joy\"](https://www.kevinalbrecht.com/code/joy-mirror/j05cmp.html) by Manfred von Thun:\n",
28     "\n",
29     "> \"The genrec combinator takes four program parameters in addition to\n",
30     "whatever data parameters it needs. Fourth from the top is an if-part,\n",
31     "followed by a then-part. If the if-part yields true, then the then-part\n",
32     "is executed and the combinator terminates. The other two parameters are\n",
33     "the rec1-part and the rec2-part. If the if-part yields false, the\n",
34     "rec1-part is executed. Following that the four program parameters and\n",
35     "the combinator are again pushed onto the stack bundled up in a quoted\n",
36     "form. Then the rec2-part is executed, where it will find the bundled\n",
37     "form. Typically it will then execute the bundled form, either with i or\n",
38     "with app2, or some other combinator.\"\n",
39     "\n",
40     "It's a fantastic paper and if you like this you should really go read that.\n",
41     "This notebook is much lighter.\n",
42     "We're going to look at things from the point-of-view of the [\"Bananas, Lenses, & Barbed Wire\"](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125) paper, all about hylomorphisms, anamorphisms, catamorphisms, etc."
43    ]
44   },
45   {
46    "cell_type": "markdown",
47    "metadata": {},
48    "source": [
49     "## [Hylomorphism](https://en.wikipedia.org/wiki/Hylomorphism_%28computer_science%29)\n",
50     "\n",
51     "A [hylomorphism](https://en.wikipedia.org/wiki/Hylomorphism_%28computer_science%29) is a recursive function `H` that converts a value of type `𝔸` into a value of type `ℂ` by means of:\n",
52     "\n",
53     "- A generator `G` from `𝔸` to `(𝔹, 𝔸)`\n",
54     "- A combiner `Q` from `(𝔹, ℂ)` to `ℂ`\n",
55     "- A predicate `P` from `𝔸` to `Bool` to detect the base case\n",
56     "- A base case value `c` of type `ℂ`, and\n",
57     "- Recursive calls (zero or more); it has a \"call stack in the form of a cons list\".\n",
58     "\n",
59     "It may be helpful to see this function implemented in pseudocode (Python).\n",
60     "\n",
61     "    def hylomorphism(c, Q, P, G):\n",
62     "        '''Return a hylomorphism function H.'''\n",
63     "\n",
64     "        def H(a):\n",
65     "            if P(a):\n",
66     "                return c\n",
67     "            b, aa = G(a)\n",
68     "            return Q(b, H(aa))\n",
69     "\n",
70     "        return H\n",
71     "\n",
72     "Note that during evaluation of `H()` the intermediate `b` values are stored in the Python call stack.  This is what is meant by \"call stack in the form of a cons list\"."
73    ]
74   },
75   {
76    "cell_type": "markdown",
77    "metadata": {},
78    "source": [
79     "## Hylomorphism in Joy\n",
80     "\n",
81     "       a H\n",
82     "    ---------\n",
83     "        c\n",
84     "\n",
85     "We can define a combinator `hylomorphism` that will make a hylomorphism combinator `H` from constituent parts. (The reason for the order of the parameters will become clear below):\n",
86     "\n",
87     "    H == [P] c [G] [Q] hylomorphism\n",
88     "\n",
89     "The function `H` is recursive, so we start with `ifte` and set the else-part to some function `J` that will contain a quoted copy of `H`.\n",
90     "The then-part just discards the leftover `a` and replaces it with the base case value `c`:\n",
91     "\n",
92     "    H == [P] [pop c] [J] ifte\n",
93     "\n",
94     "The else-part `J` gets just the argument `a` on the stack.\n",
95     "\n",
96     "    a J\n",
97     "\n",
98     "The first thing to do is use the generator `G` which produces values `b` and a new `a′`:\n",
99     "\n",
100     "        a G\n",
101     "    ----------\n",
102     "       a′ b\n",
103     "\n",
104     "So:\n",
105     "\n",
106     "    J == G J′\n",
107     "\n",
108     "Then we recur with `H` on `a′`:\n",
109     "\n",
110     "       a′ b [H] dip\n",
111     "    ------------------\n",
112     "         a′ H b\n",
113     "    ------------------\n",
114     "          c′ b\n",
115     "\n",
116     "So:\n",
117     "\n",
118     "    J′ == [H] dip J″\n",
119     "\n",
120     "And run `Q` on the result:\n",
121     "\n",
122     "       c′ b Q\n",
123     "    ------------\n",
124     "         c\n",
125     "So:\n",
126     "\n",
127     "    J″ == Q\n",
128     "\n",
129     "Summing up:\n",
130     "\n",
131     "    J  == G J′\n",
132     "    J′ == [H] dip J″\n",
133     "    J″ == Q\n",
134     "\n",
135     "This gives us a definition for `J`:\n",
136     "\n",
137     "    J == G [H] dip Q\n",
138     "\n",
139     "Plug it in and convert to genrec:\n",
140     "\n",
141     "    H == [P] [pop c] [     J     ] ifte\n",
142     "         [P] [pop c] [G [H] dip Q] ifte\n",
143     "         [P] [pop c] [G]   [dip Q] genrec\n",
144     "\n",
145     "This is the form of a hylomorphism in Joy, which nicely illustrates that\n",
146     "it is a simple specialization of the general recursion combinator.\n",
147     "\n",
148     "    [P]      c  [G]     [Q] hylomorphism\n",
149     "    [P] [pop c] [G] [dip Q] genrec"
150    ]
151   },
152   {
153    "cell_type": "markdown",
154    "metadata": {},
155    "source": [
156     "## Derivation of `hylomorphism` combinator\n",
157     "\n",
158     "Now we just need to derive a definition that builds the `genrec` arguments out of the pieces given to the `hylomorphism` combinator.\n",
159     "\n",
160     "       [P]      c  [G]     [Q] hylomorphism\n",
161     "    ------------------------------------------\n",
162     "       [P] [pop c] [G] [dip Q] genrec\n",
163     "\n",
164     "Working in reverse:\n",
165     "\n",
166     "- Use `swoncat` twice to decouple `[c]` and `[Q]`.\n",
167     "- Use `unit` to dequote `c`.\n",
168     "- Use `dipd` to untangle `[unit [pop] swoncat]` from the givens.\n",
169     "\n",
170     "    H == [P] [pop c]              [G]                  [dip Q] genrec\n",
171     "         [P] [c]    [pop] swoncat [G]        [Q] [dip] swoncat genrec\n",
172     "         [P] c unit [pop] swoncat [G]        [Q] [dip] swoncat genrec\n",
173     "         [P] c [G] [Q] [unit [pop] swoncat] dipd [dip] swoncat genrec\n",
174     "\n",
175     "At this point all of the parameters of the hylomorphism are to the left so we have a definition for `hylomorphism`:\n",
176     "\n",
177     "    hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec"
178    ]
179   },
180   {
181    "cell_type": "code",
182    "execution_count": 1,
183    "metadata": {},
184    "outputs": [
185     {
186      "name": "stdout",
187      "output_type": "stream",
188      "text": []
189     }
190    ],
191    "source": [
192     "[hylomorphism [unit [pop] swoncat] dipd [dip] swoncat genrec] inscribe"
193    ]
194   },
195   {
196    "cell_type": "markdown",
197    "metadata": {},
198    "source": [
199     "### Example: Finding [Triangular Numbers](https://en.wikipedia.org/wiki/Triangular_number)\n",
200     "Let's write a function that, given a positive integer, returns the sum of all positive integers less than that one.  (In this case the types `𝔸`, `𝔹` and `ℂ` are all `int`.)\n",
201     "\n",
202     "To sum a range of integers from 0 to *n* - 1:\n",
203     "\n",
204     "- `[P]` is `[1 <=]`\n",
205     "- `c` is `0`\n",
206     "- `[G]` is `[-- dup]`\n",
207     "- `[Q]` is `[+]`"
208    ]
209   },
210   {
211    "cell_type": "code",
212    "execution_count": 2,
213    "metadata": {},
214    "outputs": [
215     {
216      "name": "stdout",
217      "output_type": "stream",
218      "text": []
219     }
220    ],
221    "source": [
222     "[triangular_number [1 <=] 0 [-- dup] [+] hylomorphism] inscribe"
223    ]
224   },
225   {
226    "cell_type": "markdown",
227    "metadata": {},
228    "source": [
229     "Let's try it:"
230    ]
231   },
232   {
233    "cell_type": "code",
234    "execution_count": 3,
235    "metadata": {},
236    "outputs": [
237     {
238      "name": "stdout",
239      "output_type": "stream",
240      "text": [
241       "10"
242      ]
243     }
244    ],
245    "source": [
246     "5 triangular_number"
247    ]
248   },
249   {
250    "cell_type": "code",
251    "execution_count": 4,
252    "metadata": {},
253    "outputs": [
254     {
255      "name": "stdout",
256      "output_type": "stream",
257      "text": [
258       "[0 0 1 3 6 10 15 21]"
259      ]
260     }
261    ],
262    "source": [
263     "clear\n",
264     "\n",
265     "[0 1 2 3 4 5 6 7] [triangular_number] map"
266    ]
267   },
268   {
269    "cell_type": "markdown",
270    "metadata": {},
271    "source": [
272     "Neat!"
273    ]
274   },
275   {
276    "cell_type": "markdown",
277    "metadata": {},
278    "source": [
279     "In the Wikipedia entry for [hylomorphism](https://en.wikipedia.org/wiki/Hylomorphism_%28computer_science%29) it says:\n",
280     "\n",
281     "> a hylomorphism is a recursive function, corresponding to the composition of an [anamorphism](https://en.wikipedia.org/wiki/Anamorphism) (which first builds a set of results; also known as 'unfolding') followed by a [catamorphism](https://en.wikipedia.org/wiki/Catamorphism) (which then folds these results into a final return value).\n",
282     "\n",
283     "\n",
284     "\n"
285    ]
286   },
287   {
288    "cell_type": "markdown",
289    "metadata": {},
290    "source": [
291     "## Anamorphism\n",
292     "An anamorphism can be defined as a hylomorphism that uses `[]` for `c` and\n",
293     "`swons` for `Q`.  An anamorphic function builds a list of values.\n",
294     "\n",
295     "    A == [P] [] [G] [swons] hylomorphism\n",
296     "\n",
297     "An example of an anamorphism is the `range` function which generates the list of integers from $0$ to $n - 1$ given $n$.\n",
298     "\n",
299     "    range == [0 <=] [] [-- dup] [swons] hylomorphism"
300    ]
301   },
302   {
303    "cell_type": "code",
304    "execution_count": 16,
305    "metadata": {},
306    "outputs": [
307     {
308      "name": "stdout",
309      "output_type": "stream",
310      "text": []
311     }
312    ],
313    "source": [
314     "clear "
315    ]
316   },
317   {
318    "cell_type": "code",
319    "execution_count": 6,
320    "metadata": {},
321    "outputs": [
322     {
323      "name": "stdout",
324      "output_type": "stream",
325      "text": []
326     }
327    ],
328    "source": [
329     "[range [0 <=] [] [-- dup] [swons] hylomorphism] inscribe"
330    ]
331   },
332   {
333    "cell_type": "code",
334    "execution_count": 7,
335    "metadata": {
336     "scrolled": false
337    },
338    "outputs": [
339     {
340      "name": "stdout",
341      "output_type": "stream",
342      "text": [
343       "[4 3 2 1 0]"
344      ]
345     }
346    ],
347    "source": [
348     "5 range"
349    ]
350   },
351   {
352    "cell_type": "markdown",
353    "metadata": {},
354    "source": [
355     "## Catamorphism\n",
356     "A catamorphic function tears down a list term-by-term and makes some new value.\n",
357     "It can be defined as a hylomorphism that uses `[uncons swap]` for `[G]`\n",
358     "and `[[] =]` (or just `[not]`) for the predicate `[P]`.\n",
359     "\n",
360     "    C == [not] c [uncons swap] [Q] hylomorphism\n",
361     "\n",
362     "An example of a catamorphism is the sum function.\n",
363     "\n",
364     "    sum == [not] 0 [uncons swap] [+] hylomorphism"
365    ]
366   },
367   {
368    "cell_type": "code",
369    "execution_count": 8,
370    "metadata": {},
371    "outputs": [
372     {
373      "name": "stdout",
374      "output_type": "stream",
375      "text": []
376     }
377    ],
378    "source": [
379     "clear "
380    ]
381   },
382   {
383    "cell_type": "code",
384    "execution_count": 9,
385    "metadata": {},
386    "outputs": [
387     {
388      "name": "stdout",
389      "output_type": "stream",
390      "text": []
391     }
392    ],
393    "source": [
394     "[sum [not] 0 [uncons swap] [+] hylomorphism] inscribe"
395    ]
396   },
397   {
398    "cell_type": "code",
399    "execution_count": 10,
400    "metadata": {},
401    "outputs": [
402     {
403      "name": "stdout",
404      "output_type": "stream",
405      "text": [
406       "15"
407      ]
408     }
409    ],
410    "source": [
411     "[5 4 3 2 1] sum"
412    ]
413   },
414   {
415    "cell_type": "code",
416    "execution_count": 11,
417    "metadata": {},
418    "outputs": [
419     {
420      "name": "stdout",
421      "output_type": "stream",
422      "text": []
423     }
424    ],
425    "source": [
426     "clear "
427    ]
428   },
429   {
430    "cell_type": "markdown",
431    "metadata": {},
432    "source": [
433     "The [\"Bananas, Lenses, & Barbed Wire\"](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125) paper mentions the\n",
434     "\n",
435     "### \"Fusion Law\" for catamorphisms\n",
436     "\n",
437     "    f . (| b, (+) |) = (| c, (x) |)  <==  f b = c /\\ f( a (+) as ) = a (x) (f as)\n",
438     "\n"
439    ]
440   },
441   {
442    "cell_type": "markdown",
443    "metadata": {},
444    "source": [
445     "The [\"Bananas, Lenses, & Barbed Wire\"](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125) paper mentions the\n",
446     "\n",
447     "### \"Fusion Law\" for catamorphisms\n",
448     "\n",
449     "    f ∘ ⦇b,⨁⦈ = ⦇c,⨂⦈  ⇐  f b = c ∧ f(a ⨁ as) = a ⨂ (f as)\n",
450     "\n"
451    ]
452   },
453   {
454    "cell_type": "markdown",
455    "metadata": {},
456    "source": [
457     "    b [⨁] catamorphism f == c [⨂] catamorphism\n",
458     "    \n",
459     "    f == 10 *\n",
460     "    \n",
461     "    0 [+] catamorphism f\n",
462     "    \n",
463     "    0 f == 0\n",
464     "    \n",
465     "    as a + f  ==  as f a ⨂\n",
466     "    \n",
467     "    ⨂ = f +\n",
468     "    \n",
469     "    0 [+] catamorphism f  = 0 [f +] catamorphism\n",
470     "    "
471    ]
472   },
473   {
474    "cell_type": "markdown",
475    "metadata": {},
476    "source": [
477     "⨁ ⨂ ∘ ∧ ⦇ ⦈ ⇐"
478    ]
479   },
480   {
481    "cell_type": "markdown",
482    "metadata": {},
483    "source": [
484     "### The `step` combinator\n",
485     "The `step` combinator will often be easier to use than `catamorphism`."
486    ]
487   },
488   {
489    "cell_type": "code",
490    "execution_count": 21,
491    "metadata": {},
492    "outputs": [
493     {
494      "name": "stdout",
495      "output_type": "stream",
496      "text": [
497       "\n",
498       "==== Help on step ====\n",
499       "\n",
500       "Run a quoted program on each item in a sequence.\n",
501       "::\n",
502       "\n",
503       "       ... [] [Q] . step\n",
504       "    -----------------------\n",
505       "          ... .\n",
506       "\n",
507       "\n",
508       "       ... [a] [Q] . step\n",
509       "    ------------------------\n",
510       "         ... a . Q\n",
511       "\n",
512       "\n",
513       "       ... [a b c] [Q] . step\n",
514       "    ----------------------------------------\n",
515       "             ... a . Q [b c] [Q] step\n",
516       "\n",
517       "The step combinator executes the quotation on each member of the list\n",
518       "on top of the stack.\n",
519       "\n",
520       "---- end (step)\n",
521       "\n",
522       "\n"
523      ]
524     }
525    ],
526    "source": [
527     "[step] help"
528    ]
529   },
530   {
531    "cell_type": "code",
532    "execution_count": 22,
533    "metadata": {},
534    "outputs": [
535     {
536      "name": "stdout",
537      "output_type": "stream",
538      "text": []
539     }
540    ],
541    "source": [
542     "[sum 0 swap [+] step] inscribe"
543    ]
544   },
545   {
546    "cell_type": "code",
547    "execution_count": 23,
548    "metadata": {},
549    "outputs": [
550     {
551      "name": "stdout",
552      "output_type": "stream",
553      "text": [
554       "15"
555      ]
556     }
557    ],
558    "source": [
559     "[5 4 3 2 1] sum"
560    ]
561   },
562   {
563    "cell_type": "markdown",
564    "metadata": {},
565    "source": [
566     "## Hylo- is Ana- then Cata-\n",
567     "\n",
568     "    anamorphism == [] swap [swons] hylomorphism\n",
569     "    \n",
570     "    catamorphism == [[not] swap [uncons swap]] dip hylomorphism\n",
571     "\n",
572     "A hylomorphism can be thought of as the composition of an anamorphism and a catamorphism:\n",
573     "\n",
574     "    [P] [G] anamorphism c [Q] catamorphism == [P] c [G] [Q] hylomorphism\n"
575    ]
576   },
577   {
578    "cell_type": "markdown",
579    "metadata": {},
580    "source": [
581     "For example, `triangular_number` could be defined as the composition of the `range` and `sum` functions:\n",
582     "\n",
583     "    triangular_number == range sum"
584    ]
585   },
586   {
587    "cell_type": "code",
588    "execution_count": 15,
589    "metadata": {},
590    "outputs": [
591     {
592      "name": "stdout",
593      "output_type": "stream",
594      "text": [
595       "[0 0 1 3 6 10 15 21]"
596      ]
597     }
598    ],
599    "source": [
600     "clear\n",
601     "\n",
602     "[0 1 2 3 4 5 6 7] [range sum] map"
603    ]
604   },
605   {
606    "cell_type": "markdown",
607    "metadata": {},
608    "source": [
609     "However, this creates (and destroys) an intermediate list, which is a waste."
610    ]
611   },
612   {
613    "cell_type": "markdown",
614    "metadata": {},
615    "source": [
616     "## Four Specializations\n",
617     "There are (at least) four kinds of recursive combinator, depending on two choices.  The first choice is whether the combiner function `Q` should be evaluated during the recursion or pushed into the pending expression to be \"collapsed\" at the end.  The second choice is whether the combiner needs to operate on the current value of the datastructure or the generator's output, in other words, whether `Q` or `G` should run first in the recursive branch.\n",
618     "\n",
619     "    H1 ==        [P] [pop c] [G             ] [dip Q] genrec\n",
620     "    H2 == c swap [P] [pop]   [G [Q]    dip  ] [i]     genrec\n",
621     "    H3 ==        [P] [pop c] [  [G] dupdip  ] [dip Q] genrec\n",
622     "    H4 == c swap [P] [pop]   [  [Q] dupdip G] [i]     genrec\n",
623     "\n",
624     "The working of the generator function `G` differs slightly for each.  Consider the recursive branches:\n",
625     "\n",
626     "    ... a G [H1] dip Q                w/ a G == a′ b\n",
627     "    \n",
628     "    ... c a G [Q] dip H2                 a G == b  a′\n",
629     "    \n",
630     "    ... a [G] dupdip [H3] dip Q          a G == a′\n",
631     "    \n",
632     "    ... c a [Q] dupdip G H4              a G == a′\n",
633     "\n",
634     "The following four sections illustrate how these work, omitting the predicate evaluation."
635    ]
636   },
637   {
638    "cell_type": "markdown",
639    "metadata": {},
640    "source": [
641     "### `H1`\n",
642     "\n",
643     "This form builds up a pending expression (continuation) that contains the intermediate results along with the pending combiner functions.  When the base case is reached the last term is replaced by the identity value `c` and the continuation \"collapses\" into the final result using the combiner `Q`.\n",
644     "\n",
645     "    H1 == [P] [pop c] [G] [dip Q] genrec\n",
646     "\n",
647     "    ... a G [H1] dip Q\n",
648     "    ... a′ b [H1] dip Q\n",
649     "    ... a′ H1 b Q\n",
650     "       <predicate omitted>\n",
651     "    ... a′ G [H1] dip Q b Q\n",
652     "    ... a″ b′ [H1] dip Q b Q\n",
653     "    ... a″ H1 b′ Q b Q\n",
654     "       <predicate omitted>\n",
655     "    ... a″ G [H1] dip Q b′ Q b Q\n",
656     "    ... a‴ b″ [H1] dip Q b′ Q b Q\n",
657     "    ... a‴ H1 b″ Q b′ Q b Q\n",
658     "       <predicate omitted>\n",
659     "    ... a‴ pop c b″ Q b′ Q b Q\n",
660     "    ... c b″ Q b′ Q b Q\n",
661     "    ... c′ b′ Q b Q\n",
662     "    ... c″ b Q\n",
663     "    ... c‴"
664    ]
665   },
666   {
667    "cell_type": "markdown",
668    "metadata": {},
669    "source": [
670     "### `H2`\n",
671     "\n",
672     "When you can start with the identity value `c` on the stack and the combiner `Q` can operate as you go using the intermediate results immediately rather than queuing them up, use this form.\n",
673     "An important difference is that the generator function must return its results in the reverse order,\n",
674     "and both `Q` and `G` have to take into account the presence of the `c` value:\n",
675     "\n",
676     "    H2 == c swap [P] [pop] [G [Q] dip] tailrec\n",
677     "\n",
678     "    ... c a G [Q] dip H2\n",
679     "    ... c b a′ [Q] dip H2\n",
680     "    ... c b Q a′ H2\n",
681     "    ... c′ a′ H2\n",
682     "       <predicate omitted>\n",
683     "    ... c′ a′ G [Q] dip H2\n",
684     "    ... c′ b′ a″ [Q] dip H2\n",
685     "    ... c′ b′ Q a″ H2\n",
686     "    ... c″ a″ H2\n",
687     "       <predicate omitted>\n",
688     "    ... c″ a″ G [Q] dip H2\n",
689     "    ... c″ b″ a‴ [Q] dip H2\n",
690     "    ... c″ b″ Q a‴ H2\n",
691     "    ... c‴ a‴ H2\n",
692     "       <predicate omitted>\n",
693     "    ... c‴ a‴ pop\n",
694     "    ... c‴\n"
695    ]
696   },
697   {
698    "cell_type": "markdown",
699    "metadata": {},
700    "source": [
701     "### `H3`\n",
702     "\n",
703     "If you examine the traces above you'll see that the combiner `Q` only gets to operate on the results of `G`, it never \"sees\" the first value `a`.  If the combiner and the generator both need to work on the current value then `dup` must be used, and the generator must produce one item instead of two (the b is instead the duplicate of a.)\n",
704     "\n",
705     "\n",
706     "    H3 == [P] [pop c] [[G] dupdip] [dip Q] genrec\n",
707     "\n",
708     "    ... a [G] dupdip [H3] dip Q\n",
709     "    ... a G a [H3] dip Q\n",
710     "    ... a′ a [H3] dip Q\n",
711     "    ... a′ H3 a Q\n",
712     "       <predicate omitted>\n",
713     "    ... a′ [G] dupdip [H3] dip Q a Q\n",
714     "    ... a′ G a′ [H3] dip Q a Q\n",
715     "    ... a″ a′ [H3] dip Q a Q\n",
716     "    ... a″ H3 a′ Q a Q\n",
717     "       <predicate omitted>\n",
718     "    ... a″ [G] dupdip [H3] dip Q a′ Q a Q\n",
719     "    ... a″ G a″ [H3] dip Q a′ Q a Q\n",
720     "    ... a‴ a″ [H3] dip Q a′ Q a Q\n",
721     "    ... a‴ H3 a″ Q a′ Q a Q\n",
722     "       <predicate omitted>\n",
723     "    ... a‴ pop c a″ Q a′ Q a Q\n",
724     "    ... c a″ Q a′ Q a Q\n",
725     "    ... c′ a′ Q a Q\n",
726     "    ... c‴ a Q\n",
727     "    ... c‴"
728    ]
729   },
730   {
731    "cell_type": "markdown",
732    "metadata": {},
733    "source": [
734     "### `H4`\n",
735     "\n",
736     "And, last but not least, if you can combine as you go, starting with `c`, and the combiner `Q` needs to work on the current item, this is the form:\n",
737     "\n",
738     "    H4 == c swap [P] [pop] [[Q] dupdip G] primrec\n",
739     "\n",
740     "    ... c a [Q] dupdip G H4\n",
741     "    ... c a Q a G H4\n",
742     "    ... c′ a G H4\n",
743     "    ... c′ a′ H4\n",
744     "       <predicate omitted>\n",
745     "    ... c′ a′ [Q] dupdip G H4\n",
746     "    ... c′ a′ Q a′ G H4\n",
747     "    ... c‴ a′ G H4\n",
748     "    ... c‴ a″ H4\n",
749     "       <predicate omitted>\n",
750     "    ... c‴ a″ [Q] dupdip G H4\n",
751     "    ... c‴ a″ Q a″ G H4\n",
752     "    ... c‴ a″ G H4\n",
753     "    ... c‴ a‴ H4\n",
754     "       <predicate omitted>\n",
755     "    ... c‴ a‴ pop\n",
756     "    ... c‴"
757    ]
758   },
759   {
760    "cell_type": "markdown",
761    "metadata": {},
762    "source": [
763     "### `range` et. al.\n",
764     "\n",
765     "An example of an anamorphism is the `range` function which generates the list of integers from 0 to *n* - 1 given *n*.\n",
766     "\n",
767     "Each of the above variations can be used to make four slightly different `range` functions."
768    ]
769   },
770   {
771    "cell_type": "markdown",
772    "metadata": {},
773    "source": [
774     "#### `range` with `H1`\n",
775     "    H1 == [P]    [pop c]  [G]      [dip Q]     genrec\n",
776     "       == [0 <=] [pop []] [-- dup] [dip swons] genrec"
777    ]
778   },
779   {
780    "cell_type": "code",
781    "execution_count": 5,
782    "metadata": {},
783    "outputs": [
784     {
785      "name": "stdout",
786      "output_type": "stream",
787      "text": []
788     }
789    ],
790    "source": [
791     "clear "
792    ]
793   },
794   {
795    "cell_type": "code",
796    "execution_count": 6,
797    "metadata": {},
798    "outputs": [
799     {
800      "name": "stdout",
801      "output_type": "stream",
802      "text": []
803     }
804    ],
805    "source": [
806     "[range [0 <=] [] [-- dup] [swons] hylomorphism] inscribe"
807    ]
808   },
809   {
810    "cell_type": "code",
811    "execution_count": 7,
812    "metadata": {
813     "scrolled": false
814    },
815    "outputs": [
816     {
817      "name": "stdout",
818      "output_type": "stream",
819      "text": [
820       "[4 3 2 1 0]"
821      ]
822     }
823    ],
824    "source": [
825     "5 range"
826    ]
827   },
828   {
829    "cell_type": "markdown",
830    "metadata": {},
831    "source": [
832     "#### `range` with `H2`\n",
833     "    H2 == c  swap [P]    [pop] [G      [Q]     dip] tailrec\n",
834     "       == [] swap [0 <=] [pop] [-- dup [swons] dip] tailrec"
835    ]
836   },
837   {
838    "cell_type": "code",
839    "execution_count": 8,
840    "metadata": {},
841    "outputs": [
842     {
843      "name": "stdout",
844      "output_type": "stream",
845      "text": []
846     }
847    ],
848    "source": [
849     "clear "
850    ]
851   },
852   {
853    "cell_type": "code",
854    "execution_count": 9,
855    "metadata": {},
856    "outputs": [
857     {
858      "name": "stdout",
859      "output_type": "stream",
860      "text": []
861     }
862    ],
863    "source": [
864     "[range_reverse [] swap [0 <=] [pop] [-- dup [swons] dip] tailrec] inscribe"
865    ]
866   },
867   {
868    "cell_type": "code",
869    "execution_count": 10,
870    "metadata": {
871     "scrolled": true
872    },
873    "outputs": [
874     {
875      "name": "stdout",
876      "output_type": "stream",
877      "text": [
878       "[0 1 2 3 4]"
879      ]
880     }
881    ],
882    "source": [
883     "5 range_reverse"
884    ]
885   },
886   {
887    "cell_type": "markdown",
888    "metadata": {},
889    "source": [
890     "#### `range` with `H3`\n",
891     "    H3 == [P]    [pop c]  [[G]  dupdip] [dip Q]     genrec\n",
892     "       == [0 <=] [pop []] [[--] dupdip] [dip swons] genrec"
893    ]
894   },
895   {
896    "cell_type": "code",
897    "execution_count": 11,
898    "metadata": {},
899    "outputs": [
900     {
901      "name": "stdout",
902      "output_type": "stream",
903      "text": []
904     }
905    ],
906    "source": [
907     "clear "
908    ]
909   },
910   {
911    "cell_type": "code",
912    "execution_count": 12,
913    "metadata": {},
914    "outputs": [
915     {
916      "name": "stdout",
917      "output_type": "stream",
918      "text": []
919     }
920    ],
921    "source": [
922     "[ranger [0 <=] [pop []] [[--] dupdip] [dip swons] genrec] inscribe"
923    ]
924   },
925   {
926    "cell_type": "code",
927    "execution_count": 13,
928    "metadata": {
929     "scrolled": true
930    },
931    "outputs": [
932     {
933      "name": "stdout",
934      "output_type": "stream",
935      "text": [
936       "[5 4 3 2 1]"
937      ]
938     }
939    ],
940    "source": [
941     "5 ranger"
942    ]
943   },
944   {
945    "cell_type": "markdown",
946    "metadata": {},
947    "source": [
948     "#### `range` with `H4`\n",
949     "    H4 == c  swap [P]    [pop] [[Q]     dupdip G ] primrec\n",
950     "       == [] swap [0 <=] [pop] [[swons] dupdip --] primrec"
951    ]
952   },
953   {
954    "cell_type": "code",
955    "execution_count": 14,
956    "metadata": {},
957    "outputs": [
958     {
959      "name": "stdout",
960      "output_type": "stream",
961      "text": []
962     }
963    ],
964    "source": [
965     "clear "
966    ]
967   },
968   {
969    "cell_type": "code",
970    "execution_count": 15,
971    "metadata": {},
972    "outputs": [
973     {
974      "name": "stdout",
975      "output_type": "stream",
976      "text": []
977     }
978    ],
979    "source": [
980     "[ranger_reverse [] swap [0 <=] [pop] [[swons] dupdip --] tailrec] inscribe"
981    ]
982   },
983   {
984    "cell_type": "code",
985    "execution_count": 16,
986    "metadata": {
987     "scrolled": true
988    },
989    "outputs": [
990     {
991      "name": "stdout",
992      "output_type": "stream",
993      "text": [
994       "[1 2 3 4 5]"
995      ]
996     }
997    ],
998    "source": [
999     "5 ranger_reverse"
1000    ]
1001   },
1002   {
1003    "cell_type": "markdown",
1004    "metadata": {},
1005    "source": [
1006     "## Example: Factorial Function\n",
1007     "\n",
1008     "For the Factorial function:\n",
1009     "\n",
1010     "    H4 == c swap [P] [pop] [[Q] dupdip G] tailrec\n",
1011     "\n",
1012     "With:\n",
1013     "\n",
1014     "    c == 1\n",
1015     "    Q == *\n",
1016     "    G == --\n",
1017     "    P == 1 <="
1018    ]
1019   },
1020   {
1021    "cell_type": "code",
1022    "execution_count": 24,
1023    "metadata": {},
1024    "outputs": [
1025     {
1026      "name": "stdout",
1027      "output_type": "stream",
1028      "text": []
1029     }
1030    ],
1031    "source": [
1032     "clear"
1033    ]
1034   },
1035   {
1036    "cell_type": "code",
1037    "execution_count": 25,
1038    "metadata": {},
1039    "outputs": [
1040     {
1041      "name": "stdout",
1042      "output_type": "stream",
1043      "text": []
1044     }
1045    ],
1046    "source": [
1047     "[factorial 1 swap [1 <=] [pop] [[*] dupdip --] tailrec] inscribe"
1048    ]
1049   },
1050   {
1051    "cell_type": "code",
1052    "execution_count": 26,
1053    "metadata": {
1054     "scrolled": false
1055    },
1056    "outputs": [
1057     {
1058      "name": "stdout",
1059      "output_type": "stream",
1060      "text": [
1061       "120"
1062      ]
1063     }
1064    ],
1065    "source": [
1066     "5 factorial"
1067    ]
1068   },
1069   {
1070    "cell_type": "markdown",
1071    "metadata": {},
1072    "source": [
1073     "## Example: Filter Function\n",
1074     "\n",
1075     "An often-used function is `filter` which takes a list and a predicate and makes a new list with only those items from the input list that make the predicate true:\n",
1076     "\n",
1077     "       [...] [P] filter\n",
1078     "    ----------------------\n",
1079     "            [...]′\n",
1080     "\n",
1081     "Let's start by making a simpler function that just recreates the input list:\n",
1082     "\n",
1083     "    F == [not] [] [J] ifte\n",
1084     "\n",
1085     "To figure out the recursive branch `J` we start by setting a dummy list in front of it.\n",
1086     "\n",
1087     "    [a b c] J\n",
1088     "    [a b c] uncons swap J′\n",
1089     "    a [b c]        swap J′\n",
1090     "    [b c] a             J′\n",
1091     "\n",
1092     "    J == uncons swap J′\n",
1093     "\n",
1094     "We don't have the output list to which to `cons` that `a` so we recur instead and *then* `cons` (more precisely `swons`) it:\n",
1095     "\n",
1096     "    J′ == [Q] dip swons\n",
1097     "\n",
1098     "    [b c] a [Q] dip swons\n",
1099     "    [b c] Q a swons\n",
1100     "    ...\n",
1101     "    [c] Q b swons a swons\n",
1102     "    ...\n",
1103     "    [] c swons b swons a swons\n",
1104     "    ...\n",
1105     "    [a b c]\n",
1106     "\n",
1107     "So:\n",
1108     "\n",
1109     "    J == uncons swap [Q] dip swons\n",
1110     "\n",
1111     "And:\n",
1112     "\n",
1113     "    F == [not] [] [uncons swap [F] dip swons] ifte\n",
1114     "         [not] [] [uncons swap]   [dip swons] genrec\n",
1115     "\n"
1116    ]
1117   },
1118   {
1119    "cell_type": "code",
1120    "execution_count": 27,
1121    "metadata": {},
1122    "outputs": [
1123     {
1124      "name": "stdout",
1125      "output_type": "stream",
1126      "text": []
1127     }
1128    ],
1129    "source": [
1130     "clear"
1131    ]
1132   },
1133   {
1134    "cell_type": "code",
1135    "execution_count": 28,
1136    "metadata": {},
1137    "outputs": [
1138     {
1139      "name": "stdout",
1140      "output_type": "stream",
1141      "text": [
1142       "[1 2 3]"
1143      ]
1144     }
1145    ],
1146    "source": [
1147     "[1 2 3] [not] [] [uncons swap] [dip swons] genrec"
1148    ]
1149   },
1150   {
1151    "cell_type": "markdown",
1152    "metadata": {
1153     "scrolled": false
1154    },
1155    "source": [
1156     "It would seem that all we need to do now is turn the `swons` into an expression that applies the input `P` predicate to the items and only puts them on the output list if they pass.\n",
1157     "\n",
1158     "    filter == [not] [] [uncons swap] [dip W] genrec\n",
1159     "\n",
1160     "Resulting in, e.g.:\n",
1161     "\n",
1162     "    [] c W b W a W\n",
1163     "\n",
1164     "However, the input list will be on the stack just below the item, so `W` has to take account of that.  If our predicate function doesn't use any values from below the item under consideration then we're okay, but if it does then we have to deal with the input list somehow.\n",
1165     "\n",
1166     "We know `W` would be something like:\n",
1167     "\n",
1168     "    W == [...[P]...] [swons] [pop] ifte\n",
1169     "\n",
1170     "Let's examine the situation:\n",
1171     "\n",
1172     "    ... [...] item [...[P]...] [swons] [pop] ifte\n",
1173     "\n",
1174     "Since the predicate of the `ifte` combinator is applied `nullary` we can just `pop` the output list:\n",
1175     "\n",
1176     "    ... [...] item popd P\n",
1177     "\n",
1178     "And since `[P]` is already a quote:\n",
1179     "\n",
1180     "    W == [popd P] [swons] [pop] ifte\n",
1181     "\n",
1182     "Ergo:\n",
1183     "\n",
1184     "    [P] filter == [not] [] [uncons swap] [dip [popd P] [swons] [pop] ifte] genrec\n",
1185     "\n",
1186     "Getting that `P` into position is straightforward.  Working in reverse:\n",
1187     "\n",
1188     "    [not] [] [uncons swap] [dip [popd P] [swons] [pop] ifte] genrec\n",
1189     "\n",
1190     "Undip the first three terms:\n",
1191     "\n",
1192     "    [dip [popd P] [swons] [pop] ifte] [[not] [] [uncons swap]] dip genrec\n",
1193     "                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
1194     "\n",
1195     "Take off the `[dip]` prefix:\n",
1196     "\n",
1197     "    [[popd P] [swons] [pop] ifte] [dip] swoncat [[not] [] [uncons swap]] dip genrec\n",
1198     "                                  ^^^^^^^^^^^^^\n",
1199     "\n",
1200     "Uncons the term containing `P`:\n",
1201     "\n",
1202     "    [popd P] [[swons] [pop] ifte] cons [dip] swoncat [[not] [] [uncons swap]] dip genrec\n",
1203     "    ^^^^^^^^                      ^^^^\n",
1204     "\n",
1205     "And then take off the `[popd]` prefix:\n",
1206     "\n",
1207     "    [P] [popd] swoncat [[swons] [pop] ifte] cons [dip] swoncat [[not] [] [uncons swap]] dip genrec\n",
1208     "        ^^^^^^^^^^^^^^\n",
1209     "\n",
1210     "And so:\n",
1211     "\n",
1212     "    filter == [popd] swoncat [[swons] [pop] ifte] cons [dip] swoncat [[not] [] [uncons swap]] dip genrec\n"
1213    ]
1214   },
1215   {
1216    "cell_type": "code",
1217    "execution_count": 29,
1218    "metadata": {},
1219    "outputs": [
1220     {
1221      "name": "stdout",
1222      "output_type": "stream",
1223      "text": []
1224     }
1225    ],
1226    "source": [
1227     "clear"
1228    ]
1229   },
1230   {
1231    "cell_type": "code",
1232    "execution_count": 30,
1233    "metadata": {},
1234    "outputs": [
1235     {
1236      "name": "stdout",
1237      "output_type": "stream",
1238      "text": []
1239     }
1240    ],
1241    "source": [
1242     "[filter [popd] swoncat [[swons] [pop] ifte] cons [dip] swoncat [[not] [] [uncons swap]] dip genrec] inscribe"
1243    ]
1244   },
1245   {
1246    "cell_type": "code",
1247    "execution_count": 31,
1248    "metadata": {},
1249    "outputs": [
1250     {
1251      "name": "stdout",
1252      "output_type": "stream",
1253      "text": [
1254       "[1 2 3 4 5 6 7 8] [2 mod not]"
1255      ]
1256     }
1257    ],
1258    "source": [
1259     "[1 2 3 4 5 6 7 8] [2 mod not]"
1260    ]
1261   },
1262   {
1263    "cell_type": "code",
1264    "execution_count": 32,
1265    "metadata": {},
1266    "outputs": [
1267     {
1268      "name": "stdout",
1269      "output_type": "stream",
1270      "text": [
1271       "[2 4 6 8]"
1272      ]
1273     }
1274    ],
1275    "source": [
1276     "filter"
1277    ]
1278   },
1279   {
1280    "cell_type": "markdown",
1281    "metadata": {
1282     "scrolled": false
1283    },
1284    "source": [
1285     "This isn't completely satisfying.  For one thing, it would be nice to apply the predicate as we go so we are not putting items (and `W`) onto the expression for items that will fail the predicate `P`."
1286    ]
1287   },
1288   {
1289    "cell_type": "markdown",
1290    "metadata": {},
1291    "source": [
1292     "## Example: `tails`\n",
1293     "An example of a paramorphism for lists given in the [\"Bananas...\" paper](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125) is `tails` which returns the list of \"tails\" of a list.\n",
1294     "\n",
1295     "        [1 2 3] tails\n",
1296     "    --------------------\n",
1297     "       [[] [3] [2 3]]\n",
1298     "    \n",
1299     "\n",
1300     "We can build as we go, and we want `Q` to run after `G`, so we use pattern `H2`:\n",
1301     "\n",
1302     "    H2 == c swap [P] [pop] [G [Q] dip] tailrec\n",
1303     "\n",
1304     "We would use:\n",
1305     "\n",
1306     "    c == []\n",
1307     "    Q == swons\n",
1308     "    G == rest dup\n",
1309     "    P == not"
1310    ]
1311   },
1312   {
1313    "cell_type": "code",
1314    "execution_count": 33,
1315    "metadata": {},
1316    "outputs": [
1317     {
1318      "name": "stdout",
1319      "output_type": "stream",
1320      "text": []
1321     }
1322    ],
1323    "source": [
1324     "clear"
1325    ]
1326   },
1327   {
1328    "cell_type": "code",
1329    "execution_count": 34,
1330    "metadata": {},
1331    "outputs": [
1332     {
1333      "name": "stdout",
1334      "output_type": "stream",
1335      "text": []
1336     }
1337    ],
1338    "source": [
1339     "[tails [] swap [not] [pop] [rest dup [swons] dip] tailrec] inscribe"
1340    ]
1341   },
1342   {
1343    "cell_type": "code",
1344    "execution_count": 35,
1345    "metadata": {},
1346    "outputs": [
1347     {
1348      "name": "stdout",
1349      "output_type": "stream",
1350      "text": [
1351       "[[] [3] [2 3]]"
1352      ]
1353     }
1354    ],
1355    "source": [
1356     "[1 2 3] tails"
1357    ]
1358   },
1359   {
1360    "cell_type": "markdown",
1361    "metadata": {},
1362    "source": [
1363     "## Conclusion: Patterns of Recursion\n",
1364     "Our story so far...\n",
1365     "\n",
1366     "\n",
1367     "### Hylo-, Ana-, Cata-\n",
1368     "\n",
1369     "    H == [P  ] [pop c ] [G          ] [dip Q        ] genrec\n",
1370     "    A == [P  ] [pop []] [G          ] [dip swap cons] genrec\n",
1371     "    C == [not] [pop c ] [uncons swap] [dip Q        ] genrec\n",
1372     "\n",
1373     "### Para-, ?-, ?-\n",
1374     "\n",
1375     "    P == c  swap [P  ] [pop] [[Q        ] dupdip G          ] primrec\n",
1376     "    ? == [] swap [P  ] [pop] [[swap cons] dupdip G          ] primrec\n",
1377     "    ? == c  swap [not] [pop] [[Q        ] dupdip uncons swap] primrec\n"
1378    ]
1379   },
1380   {
1381    "cell_type": "markdown",
1382    "metadata": {},
1383    "source": [
1384     "## Appendix: Fun with Symbols\n",
1385     "\n",
1386     "    |[ (c, Q), (G, P) ]| == (|c, Q|) • [(G, P)]\n",
1387     "\n",
1388     "[\"Bananas, Lenses, & Barbed Wire\"](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125)\n",
1389     "\n",
1390     "    (|...|)  [(...)]  [<...>]\n",
1391     "\n",
1392     "I think they are having slightly too much fun with the symbols.  However, \"Too much is always better than not enough.\""
1393    ]
1394   }
1395  ],
1396  "metadata": {
1397   "kernelspec": {
1398    "display_name": "Joypy",
1399    "language": "",
1400    "name": "thun"
1401   },
1402   "language_info": {
1403    "file_extension": ".joy",
1404    "mimetype": "text/plain",
1405    "name": "Joy"
1406   }
1407  },
1408  "nbformat": 4,
1409  "nbformat_minor": 2
1410 }