{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from notebook_preamble import D, DefinitionWrapper, J, V, define" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Recursion Combinators\n", "\n", "This article describes the `genrec` combinator, how to use it, and several generic specializations.\n", "\n", " [if] [then] [rec1] [rec2] genrec\n", " ---------------------------------------------------------------------\n", " [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte\n", "\n", "\n", "From \"Recursion Theory and Joy\" (j05cmp.html) by Manfred von Thun:\n", "\n", "> \"The genrec combinator takes four program parameters in addition to\n", "whatever data parameters it needs. Fourth from the top is an if-part,\n", "followed by a then-part. If the if-part yields true, then the then-part\n", "is executed and the combinator terminates. The other two parameters are\n", "the rec1-part and the rec2-part. If the if-part yields false, the\n", "rec1-part is executed. Following that the four program parameters and\n", "the combinator are again pushed onto the stack bundled up in a quoted\n", "form. Then the rec2-part is executed, where it will find the bundled\n", "form. Typically it will then execute the bundled form, either with i or\n", "with app2, or some other combinator.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Designing Recursive Functions\n", "The way to design one of these is to fix your base case and \n", "test and then treat `R1` and `R2` as an else-part \"sandwiching\"\n", "a quotation of the whole function.\n", "\n", "For example, given a (general recursive) function `F`:\n", "\n", " F == [I] [T] [R1] [R2] genrec\n", " == [I] [T] [R1 [F] R2] ifte\n", "\n", "If the `[I]` predicate is false you must derive `R1` and `R2` from:\n", "\n", " ... R1 [F] R2\n", "\n", "Set the stack arguments in front and figure out what `R1` and `R2`\n", "have to do to apply the quoted `[F]` in the proper way." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Primitive Recursive Functions\n", "Primitive recursive functions are those where `R2 == i`.\n", "\n", " P == [I] [T] [R] primrec\n", " == [I] [T] [R [P] i] ifte\n", " == [I] [T] [R P] ifte" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Hylomorphism](https://en.wikipedia.org/wiki/Hylomorphism_%28computer_science%29)\n", "A [hylomorphism](https://en.wikipedia.org/wiki/Hylomorphism_%28computer_science%29) is a recursive function `H :: A -> C` that converts a value of type `A` into a value of type `C` by means of:\n", "\n", "- A generator `G :: A -> (B, A)`\n", "- A combiner `F :: (B, C) -> C`\n", "- A predicate `P :: A -> Bool` to detect the base case\n", "- A base case value `c :: C`\n", "- Recursive calls (zero or more); it has a \"call stack in the form of a cons list\".\n", "\n", "It may be helpful to see this function implemented in imperative Python code." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def hylomorphism(c, F, P, G):\n", " '''Return a hylomorphism function H.'''\n", "\n", " def H(a):\n", " if P(a):\n", " result = c\n", " else:\n", " b, aa = G(a)\n", " result = F(b, H(aa)) # b is stored in the stack frame during recursive call to H().\n", " return result\n", "\n", " return H" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cf. [\"Bananas, Lenses, & Barbed Wire\"](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125)\n", "\n", "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\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hylomorphism in Joy\n", "We can define a combinator `hylomorphism` that will make a hylomorphism combinator `H` from constituent parts.\n", "\n", " H == [P] c [G] [F] hylomorphism\n", "\n", "The function `H` is recursive, so we start with `ifte` and set the else-part to\n", "some function `J` that will contain a quoted copy of `H`. (The then-part just\n", "discards the leftover `a` and replaces it with the base case value `c`.)\n", "\n", " H == [P] [pop c] [J] ifte\n", "\n", "The else-part `J` gets just the argument `a` on the stack.\n", "\n", " a J\n", " a G The first thing to do is use the generator G\n", " aa b which produces b and a new aa\n", " aa b [H] dip we recur with H on the new aa\n", " aa H b F and run F on the result.\n", "\n", "This gives us a definition for `J`.\n", "\n", " J == G [H] dip F\n", "\n", "Plug it in and convert to genrec.\n", "\n", " H == [P] [pop c] [G [H] dip F] ifte\n", " H == [P] [pop c] [G] [dip F] genrec\n", "\n", "This is the form of a hylomorphism in Joy, which nicely illustrates that\n", "it is a simple specialization of the general recursion combinator.\n", "\n", " H == [P] c [G] [F] hylomorphism == [P] [pop c] [G] [dip F] genrec" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Derivation of `hylomorphism` combinator\n", "\n", "Now we just need to derive a definition that builds the `genrec` arguments\n", "out of the pieces given to the `hylomorphism` combinator.\n", "\n", " [P] c [G] [F] hylomorphism\n", " ------------------------------------------\n", " [P] [pop c] [G] [dip F] genrec\n", "\n", "Working in reverse:\n", "\n", "- Use `swoncat` twice to decouple `[c]` and `[F]`.\n", "- Use `unit` to dequote `c`.\n", "- Use `dipd` to untangle `[unit [pop] swoncat]` from the givens.\n", "\n", "So:\n", "\n", " H == [P] [pop c] [G] [dip F] genrec\n", " [P] [c] [pop] swoncat [G] [F] [dip] swoncat genrec\n", " [P] c unit [pop] swoncat [G] [F] [dip] swoncat genrec\n", " [P] c [G] [F] [unit [pop] swoncat] dipd [dip] swoncat genrec\n", "\n", "At this point all of the arguments (givens) to the hylomorphism are to the left so we have\n", "a definition for `hylomorphism`:\n", "\n", " hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "define('hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example: Finding [Triangular Numbers](https://en.wikipedia.org/wiki/Triangular_number)\n", "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 `A`, `B` and `C` are all `int`.)\n", "\n", "To sum a range of integers from 0 to *n* - 1:\n", "\n", "- `[P]` is `[1 <=]`\n", "- `c` is `0`\n", "- `[G]` is `[-- dup]`\n", "- `[F]` is `[+]`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "define('triangular_number == [1 <=] 0 [-- dup] [+] hylomorphism')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's try it:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "J('5 triangular_number')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 0 1 3 6 10 15]\n" ] } ], "source": [ "J('[0 1 2 3 4 5 6] [triangular_number] map')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Four Specializations\n", "There are at least four kinds of recursive combinator, depending on two choices. The first choice is whether the combiner function `F` 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 `F` or `G` should run first in the recursive branch.\n", "\n", " H1 == [P] [pop c] [G ] [dip F] genrec\n", " H2 == c swap [P] [pop] [G [F] dip ] [i] genrec\n", " H3 == [P] [pop c] [ [G] dupdip ] [dip F] genrec\n", " H4 == c swap [P] [pop] [ [F] dupdip G] [i] genrec\n", "\n", "The working of the generator function `G` differs slightly for each. Consider the recursive branches:\n", "\n", " ... a G [H1] dip F w/ a G == a′ b\n", " \n", " ... c a G [F] dip H2 a G == b a′\n", " \n", " ... a [G] dupdip [H3] dip F a G == a′\n", " \n", " ... c a [F] dupdip G H4 a G == a′\n", "\n", "The following four sections illustrate how these work, omitting the predicate evaluation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `H1`\n", "\n", " H1 == [P] [pop c] [G] [dip F] genrec\n", "\n", "Iterate n times.\n", "\n", " ... a G [H1] dip F\n", " ... a′ b [H1] dip F\n", " ... a′ H1 b F\n", " ... a′ G [H1] dip F b F\n", " ... a″ b′ [H1] dip F b F\n", " ... a″ H1 b′ F b F\n", " ... a″ G [H1] dip F b′ F b F\n", " ... a‴ b″ [H1] dip F b′ F b F\n", " ... a‴ H1 b″ F b′ F b F\n", " ... a‴ pop c b″ F b′ F b F\n", " ... c b″ F b′ F b F\n", " ... d b′ F b F\n", " ... d′ b F\n", " ... d″\n", "\n", "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 `F`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `H2`\n", "When you can start with the identity value `c` on the stack and the combiner `F` can operate as you go using the intermediate results immediately rather than queuing them up, use this form. An important difference is that the generator function must return its results in the reverse order.\n", "\n", " H2 == c swap [P] [pop] [G [F] dip] primrec\n", "\n", " ... c a G [F] dip H2\n", " ... c b a′ [F] dip H2\n", " ... c b F a′ H2\n", " ... d a′ H2\n", " ... d a′ G [F] dip H2\n", " ... d b′ a″ [F] dip H2\n", " ... d b′ F a″ H2\n", " ... d′ a″ H2\n", " ... d′ a″ G [F] dip H2\n", " ... d′ b″ a‴ [F] dip H2\n", " ... d′ b″ F a‴ H2\n", " ... d″ a‴ H2\n", " ... d″ a‴ pop\n", " ... d″\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `H3`\n", "If you examine the traces above you'll see that the combiner `F` 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", "\n", "\n", " H3 == [P] [pop c] [[G] dupdip] [dip F] genrec\n", "\n", " ... a [G] dupdip [H3] dip F\n", " ... a G a [H3] dip F\n", " ... a′ a [H3] dip F\n", " ... a′ H3 a F\n", " ... a′ [G] dupdip [H3] dip F a F\n", " ... a′ G a′ [H3] dip F a F\n", " ... a″ a′ [H3] dip F a F\n", " ... a″ H3 a′ F a F\n", " ... a″ [G] dupdip [H3] dip F a′ F a F\n", " ... a″ G a″ [H3] dip F a′ F a F\n", " ... a‴ a″ [H3] dip F a′ F a F\n", " ... a‴ H3 a″ F a′ F a F\n", " ... a‴ pop c a″ F a′ F a F\n", " ... c a″ F a′ F a F\n", " ... d a′ F a F\n", " ... d′ a F\n", " ... d″" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `H4`\n", "And, last but not least, if you can combine as you go, starting with `c`, and the combiner `F` needs to work on the current item, this is the form:\n", "\n", " H4 == c swap [P] [pop] [[F] dupdip G] primrec\n", "\n", " ... c a [F] dupdip G H4\n", " ... c a F a G H4\n", " ... d a G H4\n", " ... d a′ H4\n", " ... d a′ [F] dupdip G H4\n", " ... d a′ F a′ G H4\n", " ... d′ a′ G H4\n", " ... d′ a″ H4\n", " ... d′ a″ [F] dupdip G H4\n", " ... d′ a″ F a″ G H4\n", " ... d″ a″ G H4\n", " ... d″ a‴ H4\n", " ... d″ a‴ pop\n", " ... d″" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Anamorphism\n", "An anamorphism can be defined as a hylomorphism that uses `[]` for `c` and\n", "`swons` for `F`. An anamorphic function builds a list of values.\n", "\n", " A == [P] [] [G] [swons] hylomorphism" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `range` et. al.\n", "An example of an anamorphism is the `range` function which generates the list of integers from 0 to *n* - 1 given *n*.\n", "\n", "Each of the above variations can be used to make four slightly different `range` functions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `range` with `H1`\n", " H1 == [P] [pop c] [G] [dip F] genrec\n", " == [0 <=] [pop []] [-- dup] [dip swons] genrec" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "define('range == [0 <=] [] [-- dup] [swons] hylomorphism')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4 3 2 1 0]\n" ] } ], "source": [ "J('5 range')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `range` with `H2`\n", " H2 == c swap [P] [pop] [G [F] dip] primrec\n", " == [] swap [0 <=] [pop] [-- dup [swons] dip] primrec" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "define('range_reverse == [] swap [0 <=] [pop] [-- dup [swons] dip] primrec')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4]\n" ] } ], "source": [ "J('5 range_reverse')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `range` with `H3`\n", " H3 == [P] [pop c] [[G] dupdip] [dip F] genrec\n", " == [0 <=] [pop []] [[--] dupdip] [dip swons] genrec" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "define('ranger == [0 <=] [pop []] [[--] dupdip] [dip swons] genrec')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5 4 3 2 1]\n" ] } ], "source": [ "J('5 ranger')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `range` with `H4`\n", " H4 == c swap [P] [pop] [[F] dupdip G ] primrec\n", " == [] swap [0 <=] [pop] [[swons] dupdip --] primrec" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "define('ranger_reverse == [] swap [0 <=] [pop] [[swons] dupdip --] primrec')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5]\n" ] } ], "source": [ "J('5 ranger_reverse')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hopefully this illustrates the workings of the variations. For more insight you can run the cells using the `V()` function instead of the `J()` function to get a trace of the Joy evaluation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Catamorphism\n", "A catamorphism can be defined as a hylomorphism that uses `[uncons swap]` for `[G]`\n", "and `[[] =]` (or just `[not]`) for the predicate `[P]`. A catamorphic function tears down a list term-by-term and makes some new value.\n", "\n", " C == [not] c [uncons swap] [F] hylomorphism" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "define('swuncons == uncons swap') # Awkward name." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An example of a catamorphism is the sum function.\n", "\n", " sum == [not] 0 [swuncons] [+] hylomorphism" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "define('sum == [not] 0 [swuncons] [+] hylomorphism')" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n" ] } ], "source": [ "J('[5 4 3 2 1] sum')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The `step` combinator\n", "The `step` combinator will usually be better to use than `catamorphism`." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Run a quoted program on each item in a sequence.\n", "::\n", "\n", " ... [] [Q] . step\n", " -----------------------\n", " ... .\n", "\n", "\n", " ... [a] [Q] . step\n", " ------------------------\n", " ... a . Q\n", "\n", "\n", " ... [a b c] [Q] . step\n", " ----------------------------------------\n", " ... a . Q [b c] [Q] step\n", "\n", "The step combinator executes the quotation on each member of the list\n", "on top of the stack.\n", "\n" ] } ], "source": [ "J('[step] help')" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "define('sum == 0 swap [+] step')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n" ] } ], "source": [ "J('[5 4 3 2 1] sum')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example: Factorial Function\n", "\n", "For the Factorial function:\n", "\n", " H4 == c swap [P] [pop] [[F] dupdip G] primrec\n", "\n", "With:\n", "\n", " c == 1\n", " F == *\n", " G == --\n", " P == 1 <=" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "define('factorial == 1 swap [1 <=] [pop] [[*] dupdip --] primrec')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "120\n" ] } ], "source": [ "J('5 factorial')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example: `tails`\n", "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", "\n", " [1 2 3] tails\n", " --------------------\n", " [[] [3] [2 3]]\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can build as we go, and we want `F` to run after `G`, so we use pattern `H2`:\n", "\n", " H2 == c swap [P] [pop] [G [F] dip] primrec" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We would use:\n", "\n", " c == []\n", " F == swons\n", " G == rest dup\n", " P == not" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "define('tails == [] swap [not] [pop] [rest dup [swons] dip] primrec')" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[] [3] [2 3]]\n" ] } ], "source": [ "J('[1 2 3] tails')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conclusion: Patterns of Recursion\n", "Our story so far...\n", "\n", "\n", "### Hylo-, Ana-, Cata-\n", "\n", " H == [P ] [pop c ] [G ] [dip F ] genrec\n", " A == [P ] [pop []] [G ] [dip swap cons] genrec\n", " C == [not] [pop c ] [uncons swap] [dip F ] genrec\n", "\n", "### Para-, ?-, ?-\n", "\n", " P == c swap [P ] [pop] [[F ] dupdip G ] primrec\n", " ? == [] swap [P ] [pop] [[swap cons] dupdip G ] primrec\n", " ? == c swap [not] [pop] [[F ] dupdip uncons swap] primrec\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Appendix: Fun with Symbols\n", "\n", " |[ (c, F), (G, P) ]| == (|c, F|) • [(G, P)]\n", "\n", "[\"Bananas, Lenses, & Barbed Wire\"](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125)\n", "\n", " (|...|) [(...)] [<...>]\n", "\n", "I think they are having slightly too much fun with the symbols. However, \"Too much is always better than not enough.\"" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15" } }, "nbformat": 4, "nbformat_minor": 2 }