{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Treating Trees I: Ordered Binary Trees\n",
    "\n",
    "Although any expression in Joy can be considered to describe a [tree](https://en.wikipedia.org/wiki/Tree_structure) with the quotes as compound nodes and the non-quote values as leaf nodes, in this page I want to talk about [ordered binary trees](https://en.wikipedia.org/wiki/Binary_search_tree) and how to make and use them.\n",
    "\n",
    "The basic structure, in a [crude type notation](https://en.wikipedia.org/wiki/Algebraic_data_type), is:\n",
    "\n",
    "    Tree :: [] | [key value Tree Tree]\n",
    "    \n",
    "That says that a Tree is either the empty quote `[]` or a quote with four items: a key, a value, and two Trees representing the left and right branches of the tree."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We're going to derive some recursive functions to work with such datastructures:\n",
    "\n",
    "    Tree-add\n",
    "    Tree-delete\n",
    "    Tree-get\n",
    "    Tree-iter\n",
    "    Tree-iter-order\n",
    "\n",
    "Once these functions are defined we have a new \"type\" to work with, and the Sufficiently Smart Compiler can be modified to use an optimized implementation under the hood.  (Where does the \"type\" come from?  It has a contingent existence predicated on the disciplined use of these functions on otherwise undistinguished Joy datastructures.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from notebook_preamble import D, J, V, define, DefinitionWrapper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding Nodes to the Tree\n",
    "Let's consider adding nodes to a Tree structure.\n",
    "\n",
    "       Tree value key Tree-add\n",
    "    -----------------------------\n",
    "                Tree′"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Adding to an empty node.\n",
    "If the current node is `[]` then you just return `[key value [] []]`:\n",
    "\n",
    "    Tree-add == [popop not] [[pop] dipd Tree-new] [R0] [R1] genrec\n",
    "\n",
    "#### `Tree-new`\n",
    "Where `Tree-new` is defined as:\n",
    "\n",
    "       value key Tree-new\n",
    "    ------------------------\n",
    "       [key value [] []]\n",
    "\n",
    "Example:\n",
    "\n",
    "    value key swap [[] []] cons cons\n",
    "    key value      [[] []] cons cons\n",
    "    key      [value [] []]      cons\n",
    "         [key value [] []]\n",
    "\n",
    "Definition:\n",
    "\n",
    "    Tree-new == swap [[] []] cons cons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('Tree-new == swap [[] []] cons cons')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['k' 'v' [] []]\n"
     ]
    }
   ],
   "source": [
    "J('\"v\" \"k\" Tree-new')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(As an implementation detail, the `[[] []]` literal used in the definition of `Tree-new` will be reused to supply the *constant* tail for *all* new nodes produced by it.  This is one of those cases where you get amortized storage \"for free\" by using [persistent datastructures](https://en.wikipedia.org/wiki/Persistent_data_structure).  Because the tail, which is `((), ((), ()))` in Python, is immutable and embedded in the definition body for `Tree-new`, all new nodes can reuse it as their own tail without fear that some other code somewhere will change it.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Adding to a non-empty node.\n",
    "\n",
    "We now have to derive `R0` and `R1`, consider:\n",
    "\n",
    "    [key_n value_n left right] value key R0 [Tree-add] R1\n",
    "\n",
    "In this case, there are three possibilites: the key can be greater or less than or equal to the node's key.  In two of those cases we will need to apply a copy of `Tree-add`, so `R0` is pretty much out of the picture.\n",
    "\n",
    "    [R0] == []"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### A predicate to compare keys.\n",
    "\n",
    "    [key_n value_n left right] value key [BTree-add] R1\n",
    "\n",
    "The first thing we need to do is compare the the key we're adding to the node key and `branch` accordingly:\n",
    "\n",
    "    [key_n value_n left right] value key [BTree-add] [P] [T] [E] ifte\n",
    "\n",
    "That would suggest something like:\n",
    "\n",
    "    [key_n value_n left right] value key [BTree-add] P\n",
    "    [key_n value_n left right] value key [BTree-add] pop roll> pop first >\n",
    "    [key_n value_n left right] value key                 roll> pop first >\n",
    "    key [key_n value_n left right] value                 roll> pop first >\n",
    "    key key_n                                                            >\n",
    "    Boolean\n",
    "\n",
    "Let's abstract the predicate just a little to let us specify the comparison operator:\n",
    "\n",
    "    P > == pop roll> pop first >\n",
    "    P < == pop roll> pop first <\n",
    "    P   == pop roll> pop first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('P == pop roll> pop first')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'new_key' 'old_key'\n"
     ]
    }
   ],
   "source": [
    "J('[\"old_key\" 23 [] []] 17 \"new_key\" [\"...\"] P')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### If the key we're adding is greater than the node's key.\n",
    "\n",
    "Here the parentheses are meant to signify that the expression is not literal, the code in the parentheses is meant to have been evaluated:\n",
    "\n",
    "       [key_n value_n left right] value key [Tree-add] T\n",
    "    -------------------------------------------------------\n",
    "       [key_n value_n left (Tree-add key value right)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So how do we do this?  We're going to want to use `infra` on some function `K` that has the key and value to work with, as well as the quoted copy of `Tree-add` to apply somehow.  Considering the node as a stack:\n",
    "\n",
    "       right left value_n key_n value key [Tree-add] K\n",
    "    -----------------------------------------------------\n",
    "       right value key Tree-add left value_n key_n\n",
    "\n",
    "Pretty easy:\n",
    "\n",
    "    right left value_n key_n value key [Tree-add] cons cons dipdd\n",
    "    right left value_n key_n [value key Tree-add]           dipdd\n",
    "    right value key Tree-add left value_n key_n\n",
    "\n",
    "So:\n",
    "\n",
    "    K == cons cons dipdd\n",
    "\n",
    "Looking at it from the point-of-view of the node as node again:\n",
    "\n",
    "    [key_n value_n left right] [value key [Tree-add] K] infra\n",
    "    \n",
    "Expand `K` and evaluate a little:\n",
    "\n",
    "    [key_n value_n left right] [value key [Tree-add] K] infra\n",
    "    [key_n value_n left right] [value key [Tree-add] cons cons dipdd] infra\n",
    "    [key_n value_n left right] [[value key Tree-add]           dipdd] infra\n",
    "    \n",
    "Then, working backwards:\n",
    "\n",
    "    [key_n value_n left right] [[value key Tree-add]           dipdd]      infra\n",
    "    [key_n value_n left right] [value key Tree-add]           [dipdd] cons infra\n",
    "    [key_n value_n left right] value key [Tree-add] cons cons [dipdd] cons infra\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And so `T` is just:\n",
    "\n",
    "    T == cons cons [dipdd] cons infra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('T == cons cons [dipdd] cons infra')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['old_k' 'old_value' 'left' 'Tree-add' 'new_key' 'new_value' 'right']\n"
     ]
    }
   ],
   "source": [
    "J('[\"old_k\" \"old_value\" \"left\" \"right\"] \"new_value\" \"new_key\" [\"Tree-add\"] T')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### If the key we're adding is less than the node's key.\n",
    "This is very very similar to the above:\n",
    "\n",
    "    [key_n value_n left right] value key [Tree-add] E\n",
    "    [key_n value_n left right] value key [Tree-add] [P <] [Te] [Ee] ifte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('E == [P <] [Te] [Ee] ifte')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this case `Te` works that same as `T` but on the left child tree instead of the right, so the only difference is that it must use `dipd` instead of `dipdd`:\n",
    "\n",
    "    Te == cons cons [dipd] cons infra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('Te == cons cons [dipd] cons infra')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['old_k' 'old_value' 'Tree-add' 'new_key' 'new_value' 'left' 'right']\n"
     ]
    }
   ],
   "source": [
    "J('[\"old_k\" \"old_value\" \"left\" \"right\"] \"new_value\" \"new_key\" [\"Tree-add\"] Te')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Else the keys must be equal.\n",
    "This means we must find:\n",
    "\n",
    "       [key old_value left right] new_value key [Tree-add] Ee\n",
    "    ------------------------------------------------------------\n",
    "       [key new_value left right]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is another easy one:\n",
    "\n",
    "    Ee == pop swap roll< rest rest cons cons"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example:\n",
    "\n",
    "    [key old_value left right] new_value key [Tree-add] pop swap roll< rest rest cons cons\n",
    "    [key old_value left right] new_value key                swap roll< rest rest cons cons\n",
    "    [key old_value left right] key new_value                     roll< rest rest cons cons\n",
    "    key new_value [key old_value left right]                           rest rest cons cons\n",
    "    key new_value [              left right]                                     cons cons\n",
    "                  [key new_value left right]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('Ee == pop swap roll< rest rest cons cons')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['k' 'new_value' 'left' 'right']\n"
     ]
    }
   ],
   "source": [
    "J('[\"k\" \"old_value\" \"left\" \"right\"] \"new_value\" \"k\" [\"Tree-add\"] Ee')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Now we can define `Tree-add`\n",
    "    Tree-add == [popop not] [[pop] dipd Tree-new] [] [[P >] [T] [E] ifte] genrec\n",
    "\n",
    "Putting it all together:\n",
    "\n",
    "    Tree-new == swap [[] []] cons cons\n",
    "    P == pop roll> pop first\n",
    "    T == cons cons [dipdd] cons infra\n",
    "    Te == cons cons [dipd] cons infra\n",
    "    Ee == pop swap roll< rest rest cons cons\n",
    "    E == [P <] [Te] [Ee] ifte\n",
    "    R == [P >] [T] [E] ifte\n",
    "\n",
    "    Tree-add == [popop not] [[pop] dipd Tree-new] [] [R] genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('Tree-add == [popop not] [[pop] dipd Tree-new] [] [[P >] [T] [E] ifte] genrec')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 [] []]\n"
     ]
    }
   ],
   "source": [
    "J('[] 23 \"b\" Tree-add')  # Initial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 [] ['c' 88 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J('[\"b\" 23 [] []] 88 \"c\" Tree-add')  # Greater than"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 ['a' 88 [] []] []]\n"
     ]
    }
   ],
   "source": [
    "J('[\"b\" 23 [] []] 88 \"a\" Tree-add')  # Less than"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 88 [] []]\n"
     ]
    }
   ],
   "source": [
    "J('[\"b\" 23 [] []] 88 \"b\" Tree-add')  # Equal to"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 ['a' 88 [] []] ['c' 44 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J('[] 23 \"b\" Tree-add 88 \"a\" Tree-add 44 \"c\" Tree-add')  # Series."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 ['a' 88 [] []] ['c' 44 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J('[] [[23 \"b\"] [88 \"a\"] [44 \"c\"]] [i Tree-add] step')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Interlude: `cmp` combinator\n",
    "Instead of mucking about with nested `ifte` combinators let's use `cmp` which takes two values and three quoted programs on the stack and runs one of the three depending on the results of comparing the two values:\n",
    "\n",
    "       a b [G] [E] [L] cmp\n",
    "    ------------------------- a > b\n",
    "            G\n",
    "\n",
    "       a b [G] [E] [L] cmp\n",
    "    ------------------------- a = b\n",
    "                E\n",
    "\n",
    "       a b [G] [E] [L] cmp\n",
    "    ------------------------- a < b\n",
    "                    L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'G'\n"
     ]
    }
   ],
   "source": [
    "J(\"1 0 ['G'] ['E'] ['L'] cmp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'E'\n"
     ]
    }
   ],
   "source": [
    "J(\"1 1 ['G'] ['E'] ['L'] cmp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'L'\n"
     ]
    }
   ],
   "source": [
    "J(\"0 1 ['G'] ['E'] ['L'] cmp\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Redefine `Tree-add`\n",
    "We need a new non-destructive predicate `P`:\n",
    "\n",
    "       [node_key node_value left right] value key [Tree-add] P\n",
    "    ------------------------------------------------------------------------\n",
    "       [node_key node_value left right] value key [Tree-add] key node_key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's start with `over` to get a copy of the key and then apply some function `Q` with the `nullary` combinator so it can dig out the node key (by throwing everything else away):\n",
    "\n",
    "    P == over [Q] nullary\n",
    "\n",
    "    [node_key node_value left right] value key [Tree-add] over [Q] nullary\n",
    "    [node_key node_value left right] value key [Tree-add] key  [Q] nullary"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And `Q` would be:\n",
    "\n",
    "    Q == popop popop first\n",
    "\n",
    "    [node_key node_value left right] value key [Tree-add] key Q\n",
    "    [node_key node_value left right] value key [Tree-add] key popop popop first\n",
    "    [node_key node_value left right] value key                      popop first\n",
    "    [node_key node_value left right]                                      first\n",
    "     node_key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or just:\n",
    "\n",
    "    P == over [popop popop first] nullary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('P == over [popop popop first] nullary')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using `cmp` to simplify [our code above at `R1`](#Adding-to-a-non-empty-node.):\n",
    "\n",
    "    [node_key node_value left right] value key [Tree-add] R1\n",
    "    [node_key node_value left right] value key [Tree-add] P [T] [E] [Te] cmp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The line above becomes one of the three lines below:\n",
    "\n",
    "    [node_key node_value left right] value key [Tree-add] T\n",
    "    [node_key node_value left right] value key [Tree-add] E\n",
    "    [node_key node_value left right] value key [Tree-add] Te"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The definition is a little longer but, I think, more elegant and easier to understand:\n",
    "\n",
    "    Tree-add == [popop not] [[pop] dipd Tree-new] [] [P [T] [Ee] [Te] cmp] genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('Tree-add == [popop not] [[pop] dipd Tree-new] [] [P [T] [Ee] [Te] cmp] genrec')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 ['a' 88 [] []] ['c' 44 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J('[] 23 \"b\" Tree-add 88 \"a\" Tree-add 44 \"c\" Tree-add')  # Still works."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A Function to Traverse this Structure\n",
    "Let's take a crack at writing a function that can recursively iterate or traverse these trees."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Base case `[]`\n",
    "The stopping predicate just has to detect the empty list:\n",
    "\n",
    "    Tree-iter == [not] [E] [R0] [R1] genrec\n",
    "\n",
    "And since there's nothing at this node, we just `pop` it:\n",
    "\n",
    "    Tree-iter == [not] [pop] [R0] [R1] genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Node case `[key value left right]`\n",
    "Now we need to figure out `R0` and `R1`: \n",
    "\n",
    "    Tree-iter == [not] [pop] [R0]           [R1] genrec\n",
    "              == [not] [pop] [R0 [Tree-iter] R1] ifte\n",
    "\n",
    "Let's look at it *in situ*:\n",
    "\n",
    "    [key value left right] R0 [Tree-iter] R1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Processing the current node.\n",
    "\n",
    "`R0` is almost certainly going to use `dup` to make a copy of the node and then `dip` on some function to process the copy with it:\n",
    "\n",
    "    [key value left right] [F] dupdip                 [Tree-iter] R1\n",
    "    [key value left right]  F  [key value left right] [Tree-iter] R1\n",
    "\n",
    "For example, if we're getting all the keys `F` would be `first`:\n",
    "\n",
    "    R0 == [first] dupdip\n",
    "\n",
    "    [key value left right] [first] dupdip                 [Tree-iter] R1\n",
    "    [key value left right]  first  [key value left right] [Tree-iter] R1\n",
    "    key                            [key value left right] [Tree-iter] R1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Recur\n",
    "Now `R1` needs to apply `[Tree-iter]` to `left` and `right`.  If we drop the key and value from the node using `rest` twice we are left with an interesting situation:\n",
    "\n",
    "    key [key value left right] [Tree-iter] R1\n",
    "    key [key value left right] [Tree-iter] [rest rest] dip\n",
    "    key [key value left right] rest rest [Tree-iter]\n",
    "    key [left right] [Tree-iter]\n",
    "\n",
    "Hmm, will `step` do?\n",
    "\n",
    "    key [left right] [Tree-iter] step\n",
    "    key left Tree-iter [right] [Tree-iter] step\n",
    "    key left-keys [right] [Tree-iter] step\n",
    "    key left-keys right Tree-iter\n",
    "    key left-keys right-keys\n",
    "\n",
    "Neat. So:\n",
    "\n",
    "    R1 == [rest rest] dip step"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Putting it together\n",
    "We have:\n",
    "\n",
    "    Tree-iter == [not] [pop] [[F] dupdip] [[rest rest] dip step] genrec\n",
    "\n",
    "When I was reading this over I realized `rest rest` could go in `R0`:\n",
    "\n",
    "    Tree-iter == [not] [pop] [[F] dupdip rest rest] [step] genrec\n",
    "\n",
    "(And `[step] genrec` is such a cool and suggestive combinator!)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Parameterizing the `F` per-node processing function.\n",
    "\n",
    "                    [F] Tree-iter\n",
    "    ------------------------------------------------------\n",
    "       [not] [pop] [[F] dupdip rest rest] [step] genrec\n",
    "\n",
    "Working backward:\n",
    "\n",
    "    [not] [pop] [[F] dupdip rest rest]            [step] genrec\n",
    "    [not] [pop] [F]       [dupdip rest rest] cons [step] genrec\n",
    "    [F] [not] [pop] roll< [dupdip rest rest] cons [step] genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `Tree-iter`\n",
    "\n",
    "    Tree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('Tree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "J('[] [foo] Tree-iter')  #  It doesn't matter what F is as it won't be used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'b' 'a' 'c'\n"
     ]
    }
   ],
   "source": [
    "J(\"['b' 23 ['a' 88 [] []] ['c' 44 [] []]] [first] Tree-iter\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23 88 44\n"
     ]
    }
   ],
   "source": [
    "J(\"['b' 23 ['a' 88 [] []] ['c' 44 [] []]] [second] Tree-iter\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Interlude: A Set-like Datastructure\n",
    "We can use this to make a set-like datastructure by just setting values to e.g. 0 and ignoring them.  It's set-like in that duplicate items added to it will only occur once within it, and we can query it in [$O(\\log_2 N)$](https://en.wikipedia.org/wiki/Binary_search_tree#cite_note-2) time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 0 [2 0 [] []] [9 0 [5 0 [4 0 [] []] [8 0 [6 0 [] [7 0 [] []]] []]] []]]\n"
     ]
    }
   ],
   "source": [
    "J('[] [3 9 5 2 8 6 7 8 4] [0 swap Tree-add] step')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('to_set == [] swap [0 swap Tree-add] step')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 0 [2 0 [] []] [9 0 [5 0 [4 0 [] []] [8 0 [6 0 [] [7 0 [] []]] []]] []]]\n"
     ]
    }
   ],
   "source": [
    "J('[3 9 5 2 8 6 7 8 4] to_set')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And with that we can write a little program `unique` to remove duplicate items from a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('unique == [to_set [first] Tree-iter] cons run')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7 6 8 4 5 9 2 3]\n"
     ]
    }
   ],
   "source": [
    "J('[3 9 3 5 2 9 8 8 8 6 2 7 8 4 3] unique')  # Filter duplicate items."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A Version of `Tree-iter` that does In-Order Traversal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you look back to the [non-empty case of the `Tree-iter` function](#Node-case-[key-value-left-right]) we can design a variant that first processes the left child, then the current node, then the right child.  This will allow us to traverse the tree in sort order.\n",
    "\n",
    "    Tree-iter-order == [not] [pop] [R0] [R1] genrec\n",
    "\n",
    "To define `R0` and `R1` it helps to look at them as they will appear when they run:\n",
    "\n",
    "    [key value left right] R0 [BTree-iter-order] R1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Process the left child.\n",
    "Staring at this for a bit suggests `dup third` to start:\n",
    "\n",
    "    [key value left right] R0        [Tree-iter-order] R1\n",
    "    [key value left right] dup third [Tree-iter-order] R1\n",
    "    [key value left right] left      [Tree-iter-order] R1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now maybe:\n",
    "\n",
    "    [key value left right] left [Tree-iter-order] [cons dip] dupdip\n",
    "    [key value left right] left [Tree-iter-order]  cons dip [Tree-iter-order]\n",
    "    [key value left right] [left Tree-iter-order]       dip [Tree-iter-order]\n",
    "    left Tree-iter-order [key value left right]             [Tree-iter-order]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Process the current node.\n",
    "So far, so good.  Now we need to process the current node's values:\n",
    "\n",
    "    left Tree-iter-order [key value left right] [Tree-iter-order] [[F] dupdip] dip\n",
    "    left Tree-iter-order [key value left right] [F] dupdip [Tree-iter-order]\n",
    "    left Tree-iter-order [key value left right] F [key value left right] [Tree-iter-order]\n",
    "\n",
    "If `F` needs items from the stack below the left stuff it should have `cons`'d them before beginning maybe?  For functions like `first` it works fine as-is.\n",
    "\n",
    "    left Tree-iter-order [key value left right] first [key value left right] [Tree-iter-order]\n",
    "    left Tree-iter-order key [key value left right] [Tree-iter-order]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Process the right child.\n",
    "First ditch the rest of the node and get the right child:\n",
    "\n",
    "    left Tree-iter-order key [key value left right] [Tree-iter-order] [rest rest rest first] dip\n",
    "    left Tree-iter-order key right [Tree-iter-order]\n",
    "\n",
    "Then, of course, we just need `i` to run `Tree-iter-order` on the right side:\n",
    "\n",
    "    left Tree-iter-order key right [Tree-iter-order] i\n",
    "    left Tree-iter-order key right Tree-iter-order"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Defining `Tree-iter-order`\n",
    "The result is a little awkward:\n",
    "\n",
    "    R1 == [cons dip] dupdip [[F] dupdip] dip [rest rest rest first] dip i\n",
    "\n",
    "Let's do a little semantic factoring:\n",
    "\n",
    "    fourth == rest rest rest first\n",
    "\n",
    "    proc_left == [cons dip] dupdip\n",
    "    proc_current == [[F] dupdip] dip\n",
    "    proc_right == [fourth] dip i\n",
    "\n",
    "    Tree-iter-order == [not] [pop] [dup third] [proc_left proc_current proc_right] genrec\n",
    "\n",
    "Now we can sort sequences."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "#define('Tree-iter-order == [not] [pop] [dup third] [[cons dip] dupdip [[first] dupdip] dip [rest rest rest first] dip i] genrec')\n",
    "\n",
    "\n",
    "DefinitionWrapper.add_definitions('''\n",
    "\n",
    "fourth == rest rest rest first\n",
    "\n",
    "proc_left == [cons dip] dupdip\n",
    "proc_current == [[first] dupdip] dip\n",
    "proc_right == [fourth] dip i\n",
    "\n",
    "Tree-iter-order == [not] [pop] [dup third] [proc_left proc_current proc_right] genrec\n",
    "\n",
    "''', D)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 3 4 5 6 7 8 9\n"
     ]
    }
   ],
   "source": [
    "J('[3 9 5 2 8 6 7 8 4] to_set Tree-iter-order')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Parameterizing the `[F]` function is left as an exercise for the reader."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting values by key\n",
    "Let's derive a function that accepts a tree and a key and returns the value associated with that key.\n",
    "\n",
    "       tree key Tree-get\n",
    "    -----------------------\n",
    "            value\n",
    "\n",
    "But what do we do if the key isn't in the tree?  In Python we might raise a `KeyError` but I'd like to avoid exceptions in Joy if possible, and here I think it's possible.  (Division by zero is an example of where I think it's probably better to let Python crash Joy.  Sometimes the machinery fails and you have to \"stop the line\", I think.)\n",
    "\n",
    "Let's pass the buck to the caller by making the base case a given, you have to decide for yourself what `[E]` should be.\n",
    "\n",
    "\n",
    "       tree key [E] Tree-get\n",
    "    ---------------------------- key in tree\n",
    "               value\n",
    "\n",
    "       tree key [E] Tree-get\n",
    "    ---------------------------- key not in tree\n",
    "             [] key E"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The base case `[]`\n",
    "As before, the stopping predicate just has to detect the empty list:\n",
    "\n",
    "    Tree-get == [pop not] [E] [R0] [R1] genrec\n",
    "\n",
    "So we define:\n",
    "\n",
    "    Tree-get == [pop not] swap [R0] [R1] genrec\n",
    "\n",
    "Note that this `Tree-get` creates a slightly different function than itself and *that function* does the actual recursion.  This kind of higher-level programming is unusual in most languages but natural in Joy.\n",
    "\n",
    "    tree key [E] [pop not] swap [R0] [R1] genrec\n",
    "    tree key [pop not] [E] [R0] [R1] genrec\n",
    "\n",
    "The anonymous specialized recursive function that will do the real work.\n",
    "\n",
    "    [pop not] [E] [R0] [R1] genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Node case `[key value left right]`\n",
    "Now we need to figure out `R0` and `R1`: \n",
    "\n",
    "    [key value left right] key R0 [BTree-get] R1\n",
    "\n",
    "We want to compare the search key with the key in the node, and if they are the same return the value, otherwise recur on one of the child nodes.  So it's very similar to the above funtion, with `[R0] == []` and `R1 == P [T>] [E] [T<] cmp`:\n",
    "\n",
    "    [key value left right] key [BTree-get] P [T>] [E] [T<] cmp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Predicate\n",
    "\n",
    "    P == over [get-node-key] nullary\n",
    "    get-node-key == pop popop first\n",
    "    \n",
    "The only difference is that `get-node-key` does one less `pop` because there's no value to discard."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Branches\n",
    "Now we have to derive the branches:\n",
    "\n",
    "    [key_n value_n left right] key [BTree-get] T>\n",
    "    [key_n value_n left right] key [BTree-get] E\n",
    "    [key_n value_n left right] key [BTree-get] T<"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Greater than and less than\n",
    "The cases of `T>` and `T<` are similar to above but instead of using `infra` we have to discard the rest of the structure:\n",
    "\n",
    "       [key_n value_n left right] key [BTree-get] T>\n",
    "    ---------------------------------------------------\n",
    "                           right  key  BTree-get\n",
    "\n",
    "And:\n",
    "\n",
    "       [key_n value_n left right] key [BTree-get] T<\n",
    "    ---------------------------------------------------\n",
    "                      left        key  BTree-get\n",
    "\n",
    "So:\n",
    "    \n",
    "    T> == [fourth] dipd i\n",
    "    T< == [third] dipd i\n",
    "\n",
    "E.g.:\n",
    "\n",
    "    [key_n value_n left right]        key [BTree-get] [fourth] dipd i\n",
    "    [key_n value_n left right] fourth key [BTree-get]               i\n",
    "                        right         key [BTree-get]               i\n",
    "                        right         key  BTree-get"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Equal keys\n",
    "Return the node's value:\n",
    "\n",
    "    [key_n value_n left right] key [BTree-get] E == value_n\n",
    "\n",
    "    E == popop second"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `Tree-get`\n",
    "So:\n",
    "\n",
    "    fourth == rest rest rest first\n",
    "    get-node-key == pop popop first\n",
    "    P == over [get-node-key] nullary\n",
    "    T> == [fourth] dipd i\n",
    "    T< == [third] dipd i\n",
    "    E == popop second\n",
    "\n",
    "    Tree-get == [pop not] swap [] [P [T>] [E] [T<] cmp] genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# I don't want to deal with name conflicts with the above so I'm inlining everything here.\n",
    "# The original Joy system has \"hide\" which is a meta-command which allows you to use named\n",
    "# definitions that are only in scope for a given definition.  I don't want to implement\n",
    "# that (yet) so...\n",
    "\n",
    "\n",
    "define('''\n",
    "Tree-get == [pop not] swap [] [\n",
    "  over [pop popop first] nullary\n",
    "  [[fourth] dipd i]\n",
    "  [popop second]\n",
    "  [[third] dipd i]\n",
    "  cmp\n",
    "  ] genrec\n",
    "''')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'mike not in tree'\n"
     ]
    }
   ],
   "source": [
    "J('[\"gary\" 23 [] []] \"mike\" [popd \" not in tree\" +] Tree-get')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23\n"
     ]
    }
   ],
   "source": [
    "J('[\"gary\" 23 [] []] \"gary\" [popop \"err\"] Tree-get')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "J('''\n",
    "\n",
    "    [] [[0 'a'] [1 'b'] [2 'c']] [i Tree-add] step\n",
    "\n",
    "    'c' [popop 'not found'] Tree-get\n",
    "\n",
    "''')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'not found'\n"
     ]
    }
   ],
   "source": [
    "J('''\n",
    "\n",
    "    [] [[0 'a'] [1 'b'] [2 'c']] [i Tree-add] step\n",
    "\n",
    "    'd' [popop 'not found'] Tree-get\n",
    "\n",
    "''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tree-delete\n",
    "\n",
    "Now let's write a function that can return a tree datastructure with a key, value pair deleted:\n",
    "\n",
    "       tree key Tree-delete\n",
    "    ---------------------------\n",
    "              tree\n",
    "\n",
    "If the key is not in tree it just returns the tree unchanged."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Base case\n",
    "Same as above.\n",
    "\n",
    "    Tree-Delete == [pop not] [pop] [R0] [R1] genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Recur\n",
    "Now we get to figure out the recursive case.  We need the node's key to compare and we need to carry the key into recursive branches.  Let `D` be shorthand for `Tree-Delete`:\n",
    "\n",
    "    D == Tree-Delete == [pop not] [pop] [R0] [R1] genrec\n",
    "\n",
    "    [node_key node_value left right] key R0                   [D] R1\n",
    "    [node_key node_value left right] key over  first swap dup [D] cons R1′\n",
    "    [node_key node_value left right] key [...] first swap dup [D] cons R1′\n",
    "    [node_key node_value left right] key node_key    swap dup [D] cons R1′\n",
    "    [node_key node_value left right] node_key key         dup [D] cons R1′\n",
    "    [node_key node_value left right] node_key key key         [D] cons R1′\n",
    "    [node_key node_value left right] node_key key         [key D]      R1′"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And then:\n",
    "\n",
    "    [node_key node_value left right] node_key key [key D] R1′\n",
    "    [node_key node_value left right] node_key key [key D] roll> [T>] [E] [T<] cmp\n",
    "    [node_key node_value left right] node_key key [key D] roll> [T>] [E] [T<] cmp\n",
    "    [node_key node_value left right] [key D] node_key key       [T>] [E] [T<] cmp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So:\n",
    "\n",
    "    R0 == over first swap dup\n",
    "    R1 == cons roll> [T>] [E] [T<] cmp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compare Keys\n",
    "The last line above:\n",
    "\n",
    "    [node_key node_value left right] [key D] node_key key [T>] [E] [T<] cmp\n",
    "\n",
    "Then becomes one of these three:\n",
    "\n",
    "    [node_key node_value left right] [key D] T>\n",
    "    [node_key node_value left right] [key D] E\n",
    "    [node_key node_value left right] [key D] T<"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Greater than case and less than case\n",
    "\n",
    "       [node_key node_value left right] [F] T>\n",
    "    -------------------------------------------------\n",
    "       [node_key node_value (left F) right]\n",
    "\n",
    "\n",
    "       [node_key node_value left right] [F] T<\n",
    "    -------------------------------------------------\n",
    "       [node_key node_value left (right F)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, treating the node as a stack:\n",
    "\n",
    "    right left       node_value node_key [key D] dipd\n",
    "    right left key D node_value node_key\n",
    "    right left'      node_value node_key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ergo:\n",
    "\n",
    "    [node_key node_value left right] [key D] [dipd] cons infra"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So:\n",
    "\n",
    "    T> == [dipd] cons infra\n",
    "    T< == [dipdd] cons infra"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The else case\n",
    "We have found the node in the tree where `key` equals `node_key`.  We need to replace the current node with something\n",
    "\n",
    "       [node_key node_value left right] [key D] E\n",
    "    ------------------------------------------------\n",
    "                        tree\n",
    "\n",
    "We have to handle three cases, so let's use `cond`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### One or more child nodes are `[]`\n",
    "The first two cases are symmetrical: if we only have one non-empty child node return it.  If both child nodes are empty return an empty node.\n",
    "\n",
    "    E == [\n",
    "        [[pop third not] pop fourth]\n",
    "        [[pop fourth not] pop third]\n",
    "        [default]\n",
    "    ] cond"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Both child nodes are non-empty.\n",
    "If both child nodes are non-empty, we find the highest node in our lower sub-tree, take its key and value to replace (delete) our own, then get rid of it by recursively calling delete() on our lower sub-node with our new key.\n",
    "\n",
    "(We could also find the lowest node in our higher sub-tree and take its key and value and delete it. I only implemented one of these two symmetrical options. Over a lot of deletions this might make the tree more unbalanced.  Oh well.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The initial structure of the default function:\n",
    "\n",
    "    default == [E′] cons infra\n",
    "\n",
    "    [node_key node_value left right] [key D] default\n",
    "    [node_key node_value left right] [key D] [E′] cons infra\n",
    "    [node_key node_value left right] [[key D] E′]      infra\n",
    "\n",
    "    right left node_value node_key [key D] E′"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First things first, we no longer need this node's key and value:\n",
    "\n",
    "    right left node_value node_key [key D] roll> popop E″\n",
    "    right left [key D] node_value node_key       popop E″\n",
    "    right left [key D]                                 E″"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### We have to we find the highest (right-most) node in our lower (left) sub-tree:\n",
    "\n",
    "    right left [key D] E″"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ditch the key:\n",
    "\n",
    "    right left [key D] rest E‴\n",
    "    right left     [D]      E‴"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Find the right-most node:\n",
    "\n",
    "    right left        [D] [dup W] dip E⁗\n",
    "    right left dup  W [D]             E⁗\n",
    "    right left left W [D]             E⁗"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider:\n",
    "\n",
    "    left W"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We know left is not empty:\n",
    "\n",
    "    [L_key L_value L_left L_right] W"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We want to keep extracting the right node as long as it is not empty:\n",
    "\n",
    "    W.rightmost == [P] [B] while\n",
    "\n",
    "    left W.rightmost W′"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The predicate:\n",
    "\n",
    "    [L_key L_value L_left L_right] P\n",
    "    [L_key L_value L_left L_right] fourth\n",
    "                          L_right\n",
    "\n",
    "This can run on `[]` so must be guarded:\n",
    "\n",
    "    ?fourth ==  [] [fourth] [] ifte"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(\n",
    "    if_not_empty == [] swap [] ifte\n",
    "    ?fourth == [fourth] if_not_empty\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The body is just `fourth`:\n",
    "\n",
    "    left [?fourth] [fourth] while W′\n",
    "    rightest                      W′"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So:\n",
    "\n",
    "    W.rightmost == [?fourth] [fourth] while"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Found right-most node in our left sub-tree\n",
    "We know rightest is not empty:\n",
    "\n",
    "    [R_key R_value R_left R_right] W′\n",
    "    [R_key R_value R_left R_right] W′\n",
    "    [R_key R_value R_left R_right] uncons uncons pop\n",
    "    R_key [R_value R_left R_right]        uncons pop\n",
    "    R_key R_value [R_left R_right]               pop\n",
    "    R_key R_value\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So:\n",
    "\n",
    "    W == [?fourth] [fourth] while uncons uncons pop"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And:\n",
    "\n",
    "    right left left W        [D] E⁗\n",
    "    right left R_key R_value [D] E⁗"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Replace current node key and value, recursively delete rightmost\n",
    "Final stretch.  We want to end up with something like:\n",
    "\n",
    "    right left [R_key D] i R_value R_key\n",
    "    right left  R_key D    R_value R_key\n",
    "    right left′            R_value R_key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we adjust our definition of `W` to include `over` at the end:\n",
    "\n",
    "    W == [fourth] [fourth] while uncons uncons pop over"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That will give us:\n",
    "\n",
    "    right left R_key R_value R_key [D] E⁗\n",
    "\n",
    "    right left         R_key R_value R_key [D] cons dipd E⁗′\n",
    "    right left         R_key R_value [R_key D]      dipd E⁗′\n",
    "    right left R_key D R_key R_value                     E⁗′\n",
    "    right left′        R_key R_value                     E⁗′\n",
    "    right left′        R_key R_value                     swap\n",
    "    right left′ R_value R_key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So:\n",
    "\n",
    "    E′ == roll> popop E″\n",
    "\n",
    "    E″ == rest E‴\n",
    "\n",
    "    E‴ == [dup W] dip E⁗\n",
    "\n",
    "    E⁗ == cons dipdd swap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Substituting:\n",
    "\n",
    "    W == [fourth] [fourth] while uncons uncons pop over\n",
    "    E′ == roll> popop rest [dup W] dip cons dipd swap\n",
    "    E == [\n",
    "        [[pop third not] pop fourth]\n",
    "        [[pop fourth not] pop third]\n",
    "        [[E′] cons infra]\n",
    "    ] cond"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Minor rearrangement, move `dup` into `W`:\n",
    "\n",
    "    W == dup [fourth] [fourth] while uncons uncons pop over\n",
    "    E′ == roll> popop rest [W] dip cons dipd swap\n",
    "    E == [\n",
    "        [[pop third not] pop fourth]\n",
    "        [[pop fourth not] pop third]\n",
    "        [[E′] cons infra]\n",
    "    ] cond"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Refactoring\n",
    "\n",
    "    W.rightmost == [fourth] [fourth] while\n",
    "    W.unpack == uncons uncons pop\n",
    "    W == dup W.rightmost W.unpack over\n",
    "    E.clear_stuff == roll> popop rest\n",
    "    E.delete == cons dipd\n",
    "    E.0 == E.clear_stuff [W] dip E.delete swap\n",
    "    E == [\n",
    "        [[pop third not] pop fourth]\n",
    "        [[pop fourth not] pop third]\n",
    "        [[E.0] cons infra]\n",
    "    ] cond\n",
    "    T> == [dipd] cons infra\n",
    "    T< == [dipdd] cons infra\n",
    "    R0 == over first swap dup\n",
    "    R1 == cons roll> [T>] [E] [T<] cmp\n",
    "    BTree-Delete == [pop not] swap [R0] [R1] genrec\n",
    "\n",
    "By the standards of the code I've written so far, this is a *huge* Joy program."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "DefinitionWrapper.add_definitions('''\n",
    "first_two == uncons uncons pop\n",
    "fourth == rest rest rest first\n",
    "?fourth == [] [fourth] [] ifte\n",
    "W.rightmost == [?fourth] [fourth] while\n",
    "E.clear_stuff == roll> popop rest\n",
    "E.delete == cons dipd\n",
    "W == dup W.rightmost first_two over\n",
    "E.0 == E.clear_stuff [W] dip E.delete swap\n",
    "E == [[[pop third not] pop fourth] [[pop fourth not] pop third] [[E.0] cons infra]] cond\n",
    "T> == [dipd] cons infra\n",
    "T< == [dipdd] cons infra\n",
    "R0 == over first swap dup\n",
    "R1 == cons roll> [T>] [E] [T<] cmp\n",
    "Tree-Delete == [pop not] [pop] [R0] [R1] genrec\n",
    "''', D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 23 [] ['b' 88 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J(\"['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'c' Tree-Delete \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 23 [] ['c' 44 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J(\"['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'b' Tree-Delete \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 88 [] ['c' 44 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J(\"['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'a' Tree-Delete \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 23 [] ['b' 88 [] ['c' 44 [] []]]]\n"
     ]
    }
   ],
   "source": [
    "J(\"['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'der' Tree-Delete \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 0 [2 0 [1 0 [] []] [3 0 [] []]] [6 0 [5 0 [] []] [7 0 [] []]]]\n"
     ]
    }
   ],
   "source": [
    "J('[] [4 2 3 1 6 7 5 ] [0 swap Tree-add] step')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 0 [2 0 [1 0 [] []] []] [6 0 [5 0 [] []] [7 0 [] []]]]\n"
     ]
    }
   ],
   "source": [
    "J(\"[4 0 [2 0 [1 0 [] []] [3 0 [] []]] [6 0 [5 0 [] []] [7 0 [] []]]] 3 Tree-Delete \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 0 [2 0 [1 0 [] []] []] [6 0 [5 0 [] []] [7 0 [] []]]]\n"
     ]
    }
   ],
   "source": [
    "J(\"[4 0 [2 0 [1 0 [] []] [3 0 [] []]] [6 0 [5 0 [] []] [7 0 [] []]]] 4 Tree-Delete \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Appendix: The source code.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "    fourth == rest_two rest first\n",
    "    ?fourth == [] [fourth] [] ifte\n",
    "    first_two == uncons uncons pop\n",
    "    ccons == cons cons\n",
    "    cinf == cons infra\n",
    "    rest_two == rest rest\n",
    "\n",
    "    _Tree_T> == [dipd] cinf\n",
    "    _Tree_T< == [dipdd] cinf\n",
    "\n",
    "    _Tree_add_P == over [popop popop first] nullary\n",
    "    _Tree_add_T> == ccons _Tree_T<\n",
    "    _Tree_add_T< == ccons _Tree_T>\n",
    "    _Tree_add_Ee == pop swap roll< rest_two ccons\n",
    "    _Tree_add_R == _Tree_add_P [_Tree_add_T>] [_Tree_add_Ee] [_Tree_add_T<] cmp\n",
    "    _Tree_add_E == [pop] dipd Tree-new\n",
    "\n",
    "    _Tree_iter_order_left == [cons dip] dupdip\n",
    "    _Tree_iter_order_current == [[F] dupdip] dip\n",
    "    _Tree_iter_order_right == [fourth] dip i\n",
    "    _Tree_iter_order_R == _Tree_iter_order_left _Tree_iter_order_current _Tree_iter_order_right\n",
    "\n",
    "    _Tree_get_P == over [pop popop first] nullary\n",
    "    _Tree_get_T> == [fourth] dipd i\n",
    "    _Tree_get_T< == [third] dipd i\n",
    "    _Tree_get_E == popop second\n",
    "    _Tree_get_R == _Tree_get_P [_Tree_get_T>] [_Tree_get_E] [_Tree_get_T<] cmp\n",
    "\n",
    "    _Tree_delete_rightmost == [?fourth] [fourth] while\n",
    "    _Tree_delete_clear_stuff == roll> popop rest\n",
    "    _Tree_delete_del == dip cons dipd swap\n",
    "    _Tree_delete_W == dup _Tree_delete_rightmost first_two over\n",
    "    _Tree_delete_E.0 == _Tree_delete_clear_stuff [_Tree_delete_W] _Tree_delete_del\n",
    "    _Tree_delete_E == [[[pop third not] pop fourth] [[pop fourth not] pop third] [[_Tree_delete_E.0] cinf]] cond\n",
    "    _Tree_delete_R0 == over first swap dup\n",
    "    _Tree_delete_R1 == cons roll> [_Tree_T>] [_Tree_delete_E] [_Tree_T<] cmp\n",
    "\n",
    "    Tree-new == swap [[] []] ccons\n",
    "    Tree-add == [popop not] [_Tree_add_E] [] [_Tree_add_R] genrec\n",
    "    Tree-iter == [not] [pop] roll< [dupdip rest_two] cons [step] genrec\n",
    "    Tree-iter-order == [not] [pop] [dup third] [_Tree_iter_order_R] genrec\n",
    "    Tree-get == [pop not] swap [] [_Tree_get_R] genrec\n",
    "    Tree-delete == [pop not] [pop] [_Tree_delete_R0] [_Tree_delete_R1] genrec\n",
    "\n"
   ]
  }
 ],
 "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
}
