{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Treating Trees\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",
    "    BTree :: [] | [key value BTree BTree]\n",
    "    \n",
    "That says that a BTree is either the empty quote `[]` or a quote with four items: a key, a value, and two BTrees representing the left and right branches of the tree."
   ]
  },
  {
   "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",
    "    BTree-iter == [not] [E] [R0] [R1] genrec\n",
    "\n",
    "And since there's nothing at this node, we just `pop` it:\n",
    "\n",
    "    BTree-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",
    "    BTree-iter == [not] [pop] [R0]            [R1] genrec\n",
    "               == [not] [pop] [R0 [BTree-iter] R1] ifte\n",
    "\n",
    "Let's look at it *in situ*:\n",
    "\n",
    "    [key value left right] R0 [BTree-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                 [BTree-iter] R1\n",
    "    [key value left right]  F  [key value left right] [BTree-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                 [BTree-iter] R1\n",
    "    [key value left right]  first  [key value left right] [BTree-iter] R1\n",
    "    key                            [key value left right] [BTree-iter] R1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Recur\n",
    "Now `R1` needs to apply `[BTree-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] [BTree-iter] R1\n",
    "    key [key value left right] [BTree-iter] [rest rest] dip\n",
    "    key [key value left right] rest rest [BTree-iter]\n",
    "    key [left right] [BTree-iter]\n",
    "\n",
    "Hmm, will `step` do?\n",
    "\n",
    "    key [left right] [BTree-iter] step\n",
    "    key left BTree-iter [right] [BTree-iter] step\n",
    "    key left-keys [right] [BTree-iter] step\n",
    "    key left-keys right BTree-iter\n",
    "    key left-keys right-keys\n",
    "\n",
    "Wow. So:\n",
    "\n",
    "    R1 == [rest rest] dip step"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Putting it together\n",
    "We have:\n",
    "\n",
    "    BTree-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",
    "    BTree-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] BTree-iter == [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\n",
    "\n",
    "Ergo:\n",
    "\n",
    "    BTree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from notebook_preamble import D, J, V, define, DefinitionWrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('BTree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "J('[] [23] BTree-iter')  #  It doesn't matter what F is as it won't be used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'tommy'\n"
     ]
    }
   ],
   "source": [
    "J('[\"tommy\" 23 [] []] [first] BTree-iter')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'tommy' 'richard' 'jenny'\n"
     ]
    }
   ],
   "source": [
    "J('[\"tommy\" 23 [\"richard\" 48 [] []] [\"jenny\" 18 [] []]] [first] BTree-iter')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23 48 18\n"
     ]
    }
   ],
   "source": [
    "J('[\"tommy\" 23 [\"richard\" 48 [] []] [\"jenny\" 18 [] []]] [second] BTree-iter')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Adding Nodes to the BTree\n",
    "Let's consider adding nodes to a BTree structure.\n",
    "\n",
    "    BTree value key BTree-add == BTree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Adding to an empty node.\n",
    "If the current node is `[]` then you just return `[key value [] []]`:\n",
    "\n",
    "    BTree-add == [popop not] [[pop] dipd BTree-new] [R0] [R1] genrec\n",
    "\n",
    "Where `BTree-new` is:\n",
    "\n",
    "    value key BTree-new == [key value [] []]\n",
    "\n",
    "    value key swap [[] []] cons cons\n",
    "    key value      [[] []] cons cons\n",
    "    key      [value [] []]      cons\n",
    "         [key value [] []]\n",
    "\n",
    "    BTree-new == swap [[] []] cons cons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('BTree-new == swap [[] []] cons cons')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                . 'v' 'k' BTree-new\n",
      "            'v' . 'k' BTree-new\n",
      "        'v' 'k' . BTree-new\n",
      "        'v' 'k' . swap [[] []] cons cons\n",
      "        'k' 'v' . [[] []] cons cons\n",
      "'k' 'v' [[] []] . cons cons\n",
      "'k' ['v' [] []] . cons\n",
      "['k' 'v' [] []] . \n"
     ]
    }
   ],
   "source": [
    "V('\"v\" \"k\" BTree-new')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(As an implementation detail, the `[[] []]` literal used in the definition of `BTree-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 `BTree-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": [
    "#### If the current node isn't empty.\n",
    "\n",
    "We now have to derive `R0` and `R1`, consider:\n",
    "\n",
    "    [key_n value_n left right] value key R0 [BTree-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 `BTree-add`, so `R0` is pretty much out of the picture.\n",
    "\n",
    "    [R0] == []"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### A predicate to compare keys.\n",
    "The first thing we need to do is compare the the key we're adding to see if it is greater than the node key and `branch` accordingly, although in this case it's easier to write a destructive predicate and then use `ifte` to apply it `nullary`:\n",
    "\n",
    "    [key_n value_n left right] value key [BTree-add] R1\n",
    "    [key_n value_n left right] value key [BTree-add] [P >] [T] [E] ifte\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",
    "    P > == pop roll> pop first >\n",
    "    P < == pop roll> pop first <\n",
    "    P   == pop roll> pop first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('P == pop roll> pop first')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                              . ['k' 'v' [] []] 'vv' 'kk' [0] P >\n",
      "              ['k' 'v' [] []] . 'vv' 'kk' [0] P >\n",
      "         ['k' 'v' [] []] 'vv' . 'kk' [0] P >\n",
      "    ['k' 'v' [] []] 'vv' 'kk' . [0] P >\n",
      "['k' 'v' [] []] 'vv' 'kk' [0] . P >\n",
      "['k' 'v' [] []] 'vv' 'kk' [0] . pop roll> pop first >\n",
      "    ['k' 'v' [] []] 'vv' 'kk' . roll> pop first >\n",
      "    'kk' ['k' 'v' [] []] 'vv' . pop first >\n",
      "         'kk' ['k' 'v' [] []] . first >\n",
      "                     'kk' 'k' . >\n",
      "                         True . \n"
     ]
    }
   ],
   "source": [
    "V('[\"k\" \"v\" [] []] \"vv\" \"kk\" [0] P >')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### If the key we're adding is greater than the node's key.\n",
    "\n",
    "Here the parantheses are meant to signify that the right-hand side (RHS) is not literal, the code in the parentheses is meant to have been evaluated:\n",
    "\n",
    "    [key_n value_n left right] value key [BTree-add] T == [key_n value_n left (BTree-add key value right)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Use `infra` on `K`.\n",
    "So how do we do this?  We know 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 `BTree-add` to apply somehow:\n",
    "\n",
    "    right left value_n key_n value key [BTree-add] K\n",
    "        ...\n",
    "    right value key BTree-add left value_n key_n\n",
    "\n",
    "Pretty easy:\n",
    "\n",
    "    right left value_n key_n value key [BTree-add] cons cons dipdd\n",
    "    right left value_n key_n [value key BTree-add]           dipdd\n",
    "    right value key BTree-add left value_n key_n\n",
    "\n",
    "So:\n",
    "\n",
    "    K == cons cons dipdd\n",
    "\n",
    "And:\n",
    "\n",
    "    [key_n value_n left right] [value key [BTree-add] K] infra\n",
    "\n",
    "#### Derive `T`.\n",
    "So now we're at getting from this to this:\n",
    "\n",
    "    [key_n value_n left right]  value key [BTree-add] T\n",
    "        ...\n",
    "    [key_n value_n left right] [value key [BTree-add] K] infra\n",
    "\n",
    "And so `T` is just:\n",
    "\n",
    "    value key [BTree-add] T == [value key [BTree-add] K]                infra\n",
    "                          T == [                      K] cons cons cons infra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('K == cons cons dipdd')\n",
    "define('T == [K] cons cons cons infra')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                              . 'r' 'l' 'v' 'k' 'vv' 'kk' [0] K\n",
      "                          'r' . 'l' 'v' 'k' 'vv' 'kk' [0] K\n",
      "                      'r' 'l' . 'v' 'k' 'vv' 'kk' [0] K\n",
      "                  'r' 'l' 'v' . 'k' 'vv' 'kk' [0] K\n",
      "              'r' 'l' 'v' 'k' . 'vv' 'kk' [0] K\n",
      "         'r' 'l' 'v' 'k' 'vv' . 'kk' [0] K\n",
      "    'r' 'l' 'v' 'k' 'vv' 'kk' . [0] K\n",
      "'r' 'l' 'v' 'k' 'vv' 'kk' [0] . K\n",
      "'r' 'l' 'v' 'k' 'vv' 'kk' [0] . cons cons dipdd\n",
      "'r' 'l' 'v' 'k' 'vv' ['kk' 0] . cons dipdd\n",
      "'r' 'l' 'v' 'k' ['vv' 'kk' 0] . dipdd\n",
      "                          'r' . 'vv' 'kk' 0 'l' 'v' 'k'\n",
      "                     'r' 'vv' . 'kk' 0 'l' 'v' 'k'\n",
      "                'r' 'vv' 'kk' . 0 'l' 'v' 'k'\n",
      "              'r' 'vv' 'kk' 0 . 'l' 'v' 'k'\n",
      "          'r' 'vv' 'kk' 0 'l' . 'v' 'k'\n",
      "      'r' 'vv' 'kk' 0 'l' 'v' . 'k'\n",
      "  'r' 'vv' 'kk' 0 'l' 'v' 'k' . \n"
     ]
    }
   ],
   "source": [
    "V('\"r\" \"l\" \"v\" \"k\" \"vv\" \"kk\" [0] K')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                    . ['k' 'v' 'l' 'r'] 'vv' 'kk' [0] T\n",
      "                  ['k' 'v' 'l' 'r'] . 'vv' 'kk' [0] T\n",
      "             ['k' 'v' 'l' 'r'] 'vv' . 'kk' [0] T\n",
      "        ['k' 'v' 'l' 'r'] 'vv' 'kk' . [0] T\n",
      "    ['k' 'v' 'l' 'r'] 'vv' 'kk' [0] . T\n",
      "    ['k' 'v' 'l' 'r'] 'vv' 'kk' [0] . [K] cons cons cons infra\n",
      "['k' 'v' 'l' 'r'] 'vv' 'kk' [0] [K] . cons cons cons infra\n",
      "['k' 'v' 'l' 'r'] 'vv' 'kk' [[0] K] . cons cons infra\n",
      "['k' 'v' 'l' 'r'] 'vv' ['kk' [0] K] . cons infra\n",
      "['k' 'v' 'l' 'r'] ['vv' 'kk' [0] K] . infra\n",
      "                    'r' 'l' 'v' 'k' . 'vv' 'kk' [0] K [] swaack\n",
      "               'r' 'l' 'v' 'k' 'vv' . 'kk' [0] K [] swaack\n",
      "          'r' 'l' 'v' 'k' 'vv' 'kk' . [0] K [] swaack\n",
      "      'r' 'l' 'v' 'k' 'vv' 'kk' [0] . K [] swaack\n",
      "      'r' 'l' 'v' 'k' 'vv' 'kk' [0] . cons cons dipdd [] swaack\n",
      "      'r' 'l' 'v' 'k' 'vv' ['kk' 0] . cons dipdd [] swaack\n",
      "      'r' 'l' 'v' 'k' ['vv' 'kk' 0] . dipdd [] swaack\n",
      "                                'r' . 'vv' 'kk' 0 'l' 'v' 'k' [] swaack\n",
      "                           'r' 'vv' . 'kk' 0 'l' 'v' 'k' [] swaack\n",
      "                      'r' 'vv' 'kk' . 0 'l' 'v' 'k' [] swaack\n",
      "                    'r' 'vv' 'kk' 0 . 'l' 'v' 'k' [] swaack\n",
      "                'r' 'vv' 'kk' 0 'l' . 'v' 'k' [] swaack\n",
      "            'r' 'vv' 'kk' 0 'l' 'v' . 'k' [] swaack\n",
      "        'r' 'vv' 'kk' 0 'l' 'v' 'k' . [] swaack\n",
      "     'r' 'vv' 'kk' 0 'l' 'v' 'k' [] . swaack\n",
      "      ['k' 'v' 'l' 0 'kk' 'vv' 'r'] . \n"
     ]
    }
   ],
   "source": [
    "V('[\"k\" \"v\" \"l\" \"r\"] \"vv\" \"kk\" [0] 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 [BTree-add] E\n",
    "    [key_n value_n left right] value key [BTree-add] [P <] [Te] [Ee] ifte\n",
    "\n",
    "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 cons cons infra\n",
    "\n",
    "This suggests an alternate factorization:\n",
    "\n",
    "    ccons == cons cons\n",
    "    T == [ccons dipdd] ccons cons infra\n",
    "    Te == [ccons dipd] ccons cons infra\n",
    "\n",
    "But whatever."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('Te == [cons cons dipd] cons cons cons infra')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                                 . ['k' 'v' 'l' 'r'] 'vv' 'kk' [0] Te\n",
      "                               ['k' 'v' 'l' 'r'] . 'vv' 'kk' [0] Te\n",
      "                          ['k' 'v' 'l' 'r'] 'vv' . 'kk' [0] Te\n",
      "                     ['k' 'v' 'l' 'r'] 'vv' 'kk' . [0] Te\n",
      "                 ['k' 'v' 'l' 'r'] 'vv' 'kk' [0] . Te\n",
      "                 ['k' 'v' 'l' 'r'] 'vv' 'kk' [0] . [cons cons dipd] cons cons cons infra\n",
      "['k' 'v' 'l' 'r'] 'vv' 'kk' [0] [cons cons dipd] . cons cons cons infra\n",
      "['k' 'v' 'l' 'r'] 'vv' 'kk' [[0] cons cons dipd] . cons cons infra\n",
      "['k' 'v' 'l' 'r'] 'vv' ['kk' [0] cons cons dipd] . cons infra\n",
      "['k' 'v' 'l' 'r'] ['vv' 'kk' [0] cons cons dipd] . infra\n",
      "                                 'r' 'l' 'v' 'k' . 'vv' 'kk' [0] cons cons dipd [] swaack\n",
      "                            'r' 'l' 'v' 'k' 'vv' . 'kk' [0] cons cons dipd [] swaack\n",
      "                       'r' 'l' 'v' 'k' 'vv' 'kk' . [0] cons cons dipd [] swaack\n",
      "                   'r' 'l' 'v' 'k' 'vv' 'kk' [0] . cons cons dipd [] swaack\n",
      "                   'r' 'l' 'v' 'k' 'vv' ['kk' 0] . cons dipd [] swaack\n",
      "                   'r' 'l' 'v' 'k' ['vv' 'kk' 0] . dipd [] swaack\n",
      "                                         'r' 'l' . 'vv' 'kk' 0 'v' 'k' [] swaack\n",
      "                                    'r' 'l' 'vv' . 'kk' 0 'v' 'k' [] swaack\n",
      "                               'r' 'l' 'vv' 'kk' . 0 'v' 'k' [] swaack\n",
      "                             'r' 'l' 'vv' 'kk' 0 . 'v' 'k' [] swaack\n",
      "                         'r' 'l' 'vv' 'kk' 0 'v' . 'k' [] swaack\n",
      "                     'r' 'l' 'vv' 'kk' 0 'v' 'k' . [] swaack\n",
      "                  'r' 'l' 'vv' 'kk' 0 'v' 'k' [] . swaack\n",
      "                   ['k' 'v' 0 'kk' 'vv' 'l' 'r'] . \n"
     ]
    }
   ],
   "source": [
    "V('[\"k\" \"v\" \"l\" \"r\"] \"vv\" \"kk\" [0] Te')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Else the keys must be equal.\n",
    "This means we must find:\n",
    "\n",
    "    [key_n value_n left right] value key [BTree-add] Ee\n",
    "        ...\n",
    "    [key value left right]\n",
    "\n",
    "This is another easy one:\n",
    "\n",
    "    Ee == pop swap roll< rest rest cons cons\n",
    "\n",
    "    [key_n value_n left right] value key [BTree-add] pop swap roll< rest rest cons cons\n",
    "    [key_n value_n left right] value key                 swap roll< rest rest cons cons\n",
    "    [key_n value_n left right] key value                      roll< rest rest cons cons\n",
    "    key value [key_n value_n left right]                            rest rest cons cons\n",
    "    key value [              left right]                                      cons cons\n",
    "              [key   value   left right]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('Ee == pop swap roll< rest rest cons cons')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                               . ['k' 'v' 'l' 'r'] 'vv' 'k' [0] Ee\n",
      "             ['k' 'v' 'l' 'r'] . 'vv' 'k' [0] Ee\n",
      "        ['k' 'v' 'l' 'r'] 'vv' . 'k' [0] Ee\n",
      "    ['k' 'v' 'l' 'r'] 'vv' 'k' . [0] Ee\n",
      "['k' 'v' 'l' 'r'] 'vv' 'k' [0] . Ee\n",
      "['k' 'v' 'l' 'r'] 'vv' 'k' [0] . pop swap roll< rest rest cons cons\n",
      "    ['k' 'v' 'l' 'r'] 'vv' 'k' . swap roll< rest rest cons cons\n",
      "    ['k' 'v' 'l' 'r'] 'k' 'vv' . roll< rest rest cons cons\n",
      "    'k' 'vv' ['k' 'v' 'l' 'r'] . rest rest cons cons\n",
      "        'k' 'vv' ['v' 'l' 'r'] . rest cons cons\n",
      "            'k' 'vv' ['l' 'r'] . cons cons\n",
      "            'k' ['vv' 'l' 'r'] . cons\n",
      "            ['k' 'vv' 'l' 'r'] . \n"
     ]
    }
   ],
   "source": [
    "V('[\"k\" \"v\" \"l\" \"r\"] \"vv\" \"k\" [0] Ee')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('E == [P <] [Te] [Ee] ifte')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Now we can define `BTree-add`\n",
    "    BTree-add == [popop not] [[pop] dipd BTree-new] [] [[P >] [T] [E] ifte] genrec\n",
    "\n",
    "Putting it all together:\n",
    "\n",
    "    BTree-new == swap [[] []] cons cons\n",
    "    P == pop roll> pop first\n",
    "    T == [cons cons dipdd] cons cons cons infra\n",
    "    Te == [cons cons dipd] cons cons cons infra\n",
    "    Ee == pop swap roll< rest rest cons cons\n",
    "    E == [P <] [Te] [Ee] ifte\n",
    "\n",
    "    BTree-add == [popop not] [[pop] dipd BTree-new] [] [[P >] [T] [E] ifte] genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('BTree-add == [popop not] [[pop] dipd BTree-new] [] [[P >] [T] [E] ifte] genrec')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 [] []]\n"
     ]
    }
   ],
   "source": [
    "J('[] 23 \"b\" BTree-add')  # Initial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 [] ['c' 88 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J('[\"b\" 23 [] []] 88 \"c\" BTree-add')  # Less than"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 ['a' 88 [] []] []]\n"
     ]
    }
   ],
   "source": [
    "J('[\"b\" 23 [] []] 88 \"a\" BTree-add')  # Greater than"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 88 [] []]\n"
     ]
    }
   ],
   "source": [
    "J('[\"b\" 23 [] []] 88 \"b\" BTree-add')  # Equal to"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 23 [] ['b' 88 [] ['c' 44 [] []]]]\n"
     ]
    }
   ],
   "source": [
    "J('[] 23 \"a\" BTree-add 88 \"b\" BTree-add 44 \"c\" BTree-add')  # Series."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "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": 25,
   "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 BTree-add] step')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('to_set == [] swap [0 swap BTree-add] step')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "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 to remove duplicate items from a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('unique == [to_set [first] BTree-iter] cons run')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "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": [
    "# `cmp` combinator\n",
    "Instead of all this mucking about with nested `ifte` let's just go whole hog and define `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\n",
    "\n",
    "We need a new non-destructive predicate `P`:\n",
    "\n",
    "    [key_n value_n left right] value key [BTree-add] P\n",
    "    [key_n value_n left right] value key [BTree-add] over [Q] nullary\n",
    "    [key_n value_n left right] value key [BTree-add] key  [Q] nullary\n",
    "    [key_n value_n left right] value key [BTree-add] key   Q\n",
    "    [key_n value_n left right] value key [BTree-add] key   popop popop first\n",
    "    [key_n value_n left right] value key                         popop first\n",
    "    [key_n value_n left right]                                         first\n",
    "     key_n\n",
    "    [key_n value_n left right] value key [BTree-add] key  [Q] nullary\n",
    "    [key_n value_n left right] value key [BTree-add] key key_n\n",
    "\n",
    "    P == over [popop popop first] nullary\n",
    "\n",
    "Here are the definitions again, pruned and renamed in some cases:\n",
    "\n",
    "    BTree-new == swap [[] []] cons cons\n",
    "    P == over [popop popop first] nullary\n",
    "    T> == [cons cons dipdd] cons cons cons infra\n",
    "    T< == [cons cons dipd] cons cons cons infra\n",
    "    E == pop swap roll< rest rest cons cons\n",
    "\n",
    "Using `cmp` to simplify [our code above at `R1`](#If-the-current-node-isn't-empty.):\n",
    "\n",
    "    [key_n value_n left right] value key [BTree-add] R1\n",
    "    [key_n value_n left right] value key [BTree-add] P [T>] [E] [T<] cmp\n",
    "\n",
    "The line above becomes one of the three lines below:\n",
    "\n",
    "    [key_n value_n left right] value key [BTree-add] T>\n",
    "    [key_n value_n left right] value key [BTree-add] E\n",
    "    [key_n value_n left right] value key [BTree-add] T<\n",
    "\n",
    "The definition is a little longer but, I think, more elegant and easier to understand:\n",
    "\n",
    "    BTree-add == [popop not] [[pop] dipd BTree-new] [] [P [T>] [E] [T<] cmp] genrec\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "from joy.library import FunctionWrapper\n",
    "from joy.utils.stack import concat\n",
    "from notebook_preamble import D\n",
    "\n",
    "\n",
    "@FunctionWrapper\n",
    "def cmp_(stack, expression, dictionary):\n",
    "    '''\n",
    "    cmp takes two values and three quoted programs on the stack and runs\n",
    "    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\n",
    "    '''\n",
    "    L, (E, (G, (b, (a, stack)))) = stack\n",
    "    expression = concat(G if a > b else L if a < b else E, expression)\n",
    "    return stack, expression, dictionary\n",
    "\n",
    "\n",
    "D['cmp'] = cmp_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "from joy.library import FunctionWrapper, S_ifte\n",
    "\n",
    "\n",
    "@FunctionWrapper\n",
    "def cond(stack, expression, dictionary):\n",
    "  '''\n",
    "  like a case statement; works by rewriting into a chain of ifte.\n",
    "\n",
    "  [..[[Bi] Ti]..[D]] -> ...\n",
    "\n",
    "\n",
    "        [[[B0] T0] [[B1] T1] [D]] cond\n",
    "  -----------------------------------------\n",
    "     [B0] [T0] [[B1] [T1] [D] ifte] ifte\n",
    "\n",
    "  '''\n",
    "  conditions, stack = stack\n",
    "  if conditions:\n",
    "    expression = _cond(conditions, expression)\n",
    "    try:\n",
    "      # Attempt to preload the args to first ifte.\n",
    "      (P, (T, (E, expression))) = expression\n",
    "    except ValueError:\n",
    "      # If, for any reason, the argument to cond should happen to contain\n",
    "      # only the default clause then this optimization will fail.\n",
    "      pass\n",
    "    else:\n",
    "      stack = (E, (T, (P, stack)))\n",
    "  return stack, expression, dictionary\n",
    "\n",
    "\n",
    "def _cond(conditions, expression):\n",
    "  (clause, rest) = conditions\n",
    "  if not rest:  # clause is [D]\n",
    "    return clause\n",
    "  P, T = clause\n",
    "  return (P, (T, (_cond(rest, ()), (S_ifte, expression))))\n",
    "\n",
    "\n",
    "\n",
    "D['cond'] = cond"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'G'\n"
     ]
    }
   ],
   "source": [
    "J(\"1 0 ['G'] ['E'] ['L'] cmp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'E'\n"
     ]
    }
   ],
   "source": [
    "J(\"1 1 ['G'] ['E'] ['L'] cmp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'L'\n"
     ]
    }
   ],
   "source": [
    "J(\"0 1 ['G'] ['E'] ['L'] cmp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "from joy.library import DefinitionWrapper\n",
    "\n",
    "\n",
    "DefinitionWrapper.add_definitions('''\n",
    "\n",
    "P == over [popop popop first] nullary\n",
    "T> == [cons cons dipdd] cons cons cons infra\n",
    "T< == [cons cons dipd] cons cons cons infra\n",
    "E == pop swap roll< rest rest cons cons\n",
    "\n",
    "BTree-add == [popop not] [[pop] dipd BTree-new] [] [P [T>] [E] [T<] cmp] genrec\n",
    "\n",
    "''', D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 [] []]\n"
     ]
    }
   ],
   "source": [
    "J('[] 23 \"b\" BTree-add')  # Initial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 [] ['c' 88 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J('[\"b\" 23 [] []] 88 \"c\" BTree-add')  # Less than"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 23 ['a' 88 [] []] []]\n"
     ]
    }
   ],
   "source": [
    "J('[\"b\" 23 [] []] 88 \"a\" BTree-add')  # Greater than"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 88 [] []]\n"
     ]
    }
   ],
   "source": [
    "J('[\"b\" 23 [] []] 88 \"b\" BTree-add')  # Equal to"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 23 [] ['b' 88 [] ['c' 44 [] []]]]\n"
     ]
    }
   ],
   "source": [
    "J('[] 23 \"a\" BTree-add 88 \"b\" BTree-add 44 \"c\" BTree-add')  # Series."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Factoring and naming\n",
    "It may seem silly, but a big part of programming in Forth (and therefore in Joy) is the idea of small, highly-factored definitions.  If you choose names carefully the resulting definitions can take on a semantic role.\n",
    "\n",
    "    get-node-key == popop popop first\n",
    "    remove-key-and-value-from-node == rest rest\n",
    "    pack-key-and-value == cons cons\n",
    "    prep-new-key-and-value == pop swap roll<\n",
    "    pack-and-apply == [pack-key-and-value] swoncat cons pack-key-and-value infra\n",
    "\n",
    "    BTree-new == swap [[] []] pack-key-and-value\n",
    "    P == over [get-node-key] nullary\n",
    "    T> == [dipdd] pack-and-apply\n",
    "    T< == [dipd] pack-and-apply\n",
    "    E == prep-new-key-and-value remove-key-and-value-from-node pack-key-and-value\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A Version of `BTree-iter` that does In-Order Traversal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you look back to the [non-empty case of the `BTree-iter` function](#Node-case-[key-value-left-right]) we can design a varient 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",
    "    BTree-iter-order == [not] [pop] [R0 [BTree-iter] R1] ifte\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        [BTree-iter-order] R1\n",
    "    [key value left right] dup third [BTree-iter-order] R1\n",
    "    [key value left right] left      [BTree-iter-order] R1\n",
    "\n",
    "Now maybe:\n",
    "\n",
    "    [key value left right] left [BTree-iter-order] [cons dip] dupdip\n",
    "    [key value left right] left [BTree-iter-order] cons dip [BTree-iter-order]\n",
    "    [key value left right] [left BTree-iter-order]      dip [BTree-iter-order]\n",
    "    left BTree-iter-order [key value left right]            [BTree-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 BTree-iter-order [key value left right] [BTree-iter-order] [[F] dupdip] dip\n",
    "    left BTree-iter-order [key value left right] [F] dupdip [BTree-iter-order]\n",
    "    left BTree-iter-order [key value left right] F [key value left right] [BTree-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 BTree-iter-order [key value left right] first [key value left right] [BTree-iter-order]\n",
    "    left BTree-iter-order key [key value left right] [BTree-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 BTree-iter-order key [key value left right] [BTree-iter-order] [rest rest rest first] dip\n",
    "    left BTree-iter-order key right [BTree-iter-order]\n",
    "\n",
    "Then, of course, we just need `i` to run `BTree-iter-order` on the right side:\n",
    "\n",
    "    left BTree-iter-order key right [BTree-iter-order] i\n",
    "    left BTree-iter-order key right BTree-iter-order"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Defining `BTree-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",
    "    BTree-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": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('BTree-iter-order == [not] [pop] [dup third] [[cons dip] dupdip [[first] dupdip] dip [rest rest rest first] dip i] genrec')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "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 BTree-iter-order')"
   ]
  },
  {
   "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 BTree-get\n",
    "    ------------------------\n",
    "            value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### The base case `[]`\n",
    "As before, the stopping predicate just has to detect the empty list:\n",
    "\n",
    "    BTree-get == [pop not] [E] [R0] [R1] genrec\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\", methinks.)\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] BTree-get\n",
    "    ---------------------------- key in tree\n",
    "               value\n",
    "\n",
    "       tree key [E] BTree-get\n",
    "    ---------------------------- key not in tree\n",
    "             tree key E\n",
    "\n",
    "Now we define:\n",
    "\n",
    "    BTree-get == [pop not] swap [R0] [R1] genrec\n",
    "\n",
    "Note that this `BTree-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 and if they differ then recurse 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\n",
    "\n",
    "So:\n",
    "\n",
    "    get-node-key == pop popop first\n",
    "    P == over [get-node-key] nullary\n",
    "\n",
    "The only difference is that `get-node-key` does one less `pop` because there's no value to discard.  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<\n",
    "\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> == right key BTree-get\n",
    "    [key_n value_n left right] key [BTree-get] T< == 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\n",
    "\n",
    "And:\n",
    "\n",
    "    [key_n value_n left right] key [BTree-get] E == value_n\n",
    "\n",
    "    E == popop second\n",
    "\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",
    "    BTree-get == [pop not] swap [] [P [T>] [E] [T<] cmp] genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "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",
    "BTree-get == [pop not] swap [] [\n",
    "  over [pop popop first] nullary\n",
    "  [[rest rest rest first] dipd i]\n",
    "  [popop second]\n",
    "  [[third] dipd i]\n",
    "  cmp\n",
    "  ] genrec\n",
    "''')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'err'\n"
     ]
    }
   ],
   "source": [
    "J('[] \"gary\" [popop \"err\"] BTree-get')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23\n"
     ]
    }
   ],
   "source": [
    "J('[\"gary\" 23 [] []] \"gary\" [popop \"err\"] BTree-get')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "J('''\n",
    "\n",
    "    [] [[0 'a'] [1 'b'] [2 'c']] [i BTree-add] step\n",
    "\n",
    "    'c' [popop 'not found'] BTree-get\n",
    "\n",
    "''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# BTree-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 BTree-delete\n",
    "    ---------------------------\n",
    "           tree\n",
    "\n",
    "\n",
    "If the key is not in tree it just returns the tree unchanged."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So:\n",
    "\n",
    "    BTree-Delete == [pop not] swap [R0] [R1] genrec\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "                 [Er] BTree-delete\n",
    "    -------------------------------------\n",
    "       [pop not] [Er] [R0] [R1] genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    [n_key n_value left right] [BTree-get] \n",
    "    [n_key n_value left right] [BTree-get] E\n",
    "    [n_key n_value left right] [BTree-get] T<"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we get to figure out the recursive case:\n",
    "\n",
    "    w/ D == [pop not] [Er] [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] R1\n",
    "    [node_key node_value left right] node_key key key        [D] R1\n",
    "\n",
    "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] cons 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": [
    "Now this:;\n",
    "\n",
    "    [node_key node_value left right] [key D] node_key key [T>] [E] [T<] cmp\n",
    "\n",
    "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] [key D] T>\n",
    "    -------------------------------------------------\n",
    "       [node_key node_value left key D right]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First:\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",
    "\n",
    "    [node_key node_value left right] [key D] E\n",
    "\n",
    "We have to handle three cases, so let's use `cond`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first two cases are symmetrical, if we only have one non-empty child node return it.\n",
    "\n",
    "    E == [\n",
    "        [[pop third not] pop fourth]\n",
    "        [[pop fourth not] pop third]\n",
    "        [default]\n",
    "    ] cond"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(If both child nodes are empty return an empty node.)"
   ]
  },
  {
   "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": [
    "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": [
    "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": [
    "Then 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",
    "    left [P] [B] while 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 has a bug, can run on `[]` so must be guarded:\n",
    "\n",
    "    if_not_empty == [] swap [] ifte\n",
    "    ?fourth == [fourth] if_not_empty\n",
    "    W.rightmost == [?fourth] [fourth] while"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The body is also `fourth`:\n",
    "\n",
    "    left [fourth] [fourth] while W'\n",
    "    rightest                     W'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "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] 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"
   ]
  },
  {
   "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": [
    "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 dipdd E'''''\n",
    "    right left         R_key R_value [R_key D]      dipdd 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 dipdd 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:\n",
    "\n",
    "    W == dup [fourth] [fourth] while uncons uncons pop over\n",
    "    E' == roll> popop rest [W] dip cons dipdd 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",
    "    E.clear_stuff == roll> popop rest\n",
    "    E.delete == cons dipdd\n",
    "    W == dup W.rightmost W.unpack over\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": 47,
   "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 dipdd\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",
    "BTree-Delete == [pop not] swap [R0] [R1] genrec''', D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 23 [] ['b' 88 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J(\"['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'c' ['Er'] BTree-Delete \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 23 [] ['c' 44 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J(\"['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'b' ['Er'] BTree-Delete \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b' 88 [] ['c' 44 [] []]]\n"
     ]
    }
   ],
   "source": [
    "J(\"['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'a' ['Er'] BTree-Delete \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 23 [] ['b' 88 [] ['c' 44 [] 'Er' 'der' []]]]\n"
     ]
    }
   ],
   "source": [
    "J(\"['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'der' ['Er'] BTree-Delete \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 23 [] ['b' 88 [] ['c' 44 [] []]]]\n"
     ]
    }
   ],
   "source": [
    "J(\"['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'der' [pop] BTree-Delete \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One bug, I forgot to put `not` in the first two clauses of the `cond`.\n",
    "\n",
    "The behavior of the `[Er]` function should maybe be different: either just silently fail, or maybe implement some sort of function that can grab the pending expression up to a sentinel value or something, allowing for a kind of \"except\"-ish control-flow?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then, once we have add, get, and delete we can see about abstracting them.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tree with node and list of trees.\n",
    "Let's consider a tree structure, similar to one described [\"Why functional programming matters\" by John Hughes](https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf), that consists of a node value and a sequence of zero or more child trees.  (The asterisk is meant to indicate the [Kleene star](https://en.wikipedia.org/wiki/Kleene_star).)\n",
    "\n",
    "    tree = [] | [node [tree*]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `treestep`\n",
    "In the spirit of `step` we are going to define a combinator `treestep` which expects a tree and three additional items: a base-case value `z`, and two quoted programs `[C]` and `[N]`.\n",
    "\n",
    "    tree z [C] [N] treestep\n",
    "\n",
    "If the current tree node is empty then just leave `z` on the stack in lieu:\n",
    "\n",
    "       [] z [C] [N] treestep\n",
    "    ---------------------------\n",
    "          z\n",
    "\n",
    "Otherwise, evaluate `N` on the node value, `map` the whole function (abbreviated here as `k`) over the child trees recursively, and then combine the result with `C`.\n",
    "\n",
    "       [node [tree*]] z [C] [N] treestep\n",
    "    --------------------------------------- w/ K == z [C] [N] treestep\n",
    "           node N [tree*] [K] map C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Derive the recursive form.\n",
    "Since this is a recursive function, we can begin to derive it by finding the `ifte` stage that `genrec` will produce.  The predicate and base-case functions are trivial, so we just have to derive `J`.\n",
    "\n",
    "    K == [not] [pop z] [J] ifte\n",
    "\n",
    "The behavior of `J` is to accept a (non-empty) tree node and arrive at the desired outcome.\n",
    "\n",
    "           [node [tree*]] J\n",
    "    ------------------------------\n",
    "       node N [tree*] [K] map C\n",
    "\n",
    "So `J` will have some form like:\n",
    "\n",
    "    J == .. [N] .. [K] .. [C] ..\n",
    "\n",
    "Let's dive in.  First, unquote the node and `dip` `N`.\n",
    "\n",
    "    [node [tree*]] i [N] dip\n",
    "     node [tree*]    [N] dip\n",
    "    node N [tree*]\n",
    "\n",
    "Next, `map` `K` over teh child trees and combine with `C`.\n",
    "\n",
    "    node N [tree*] [K] map C\n",
    "    node N [tree*] [K] map C\n",
    "    node N [K.tree*]       C\n",
    "\n",
    "So:\n",
    "\n",
    "    J == i [N] dip [K] map C\n",
    "\n",
    "Plug it in and convert to `genrec`:\n",
    "\n",
    "    K == [not] [pop z] [i [N] dip [K] map C] ifte\n",
    "    K == [not] [pop z] [i [N] dip]   [map C] genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Extract the givens to parameterize the program.\n",
    "\n",
    "    [not] [pop z] [i [N] dip]   [map C] genrec\n",
    "\n",
    "    [not] [pop z]                   [i [N] dip] [map C] genrec\n",
    "    [not] [z]         [pop] swoncat [i [N] dip] [map C] genrec\n",
    "    [not]  z     unit [pop] swoncat [i [N] dip] [map C] genrec\n",
    "    z [not] swap unit [pop] swoncat [i [N] dip] [map C] genrec\n",
    "      \\  .........TS0............./\n",
    "       \\/\n",
    "    z TS0 [i [N] dip]                       [map C] genrec\n",
    "    z     [i [N] dip]             [TS0] dip [map C] genrec\n",
    "    z       [[N] dip] [i] swoncat [TS0] dip [map C] genrec\n",
    "    z  [N] [dip] cons [i] swoncat [TS0] dip [map C] genrec\n",
    "           \\  ......TS1........./\n",
    "            \\/\n",
    "    z [N] TS1 [TS0] dip [map C]                      genrec\n",
    "    z [N]               [map C]  [TS1 [TS0] dip] dip genrec\n",
    "    z [N] [C]      [map] swoncat [TS1 [TS0] dip] dip genrec\n",
    "    z [C] [N] swap [map] swoncat [TS1 [TS0] dip] dip genrec\n",
    "\n",
    "The givens are all to the left so we have our definition."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define `treestep`\n",
    "         TS0 == [not] swap unit [pop] swoncat\n",
    "         TS1 == [dip] cons [i] swoncat\n",
    "    treestep == swap [map] swoncat [TS1 [TS0] dip] dip genrec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "DefinitionWrapper.add_definitions('''\n",
    "\n",
    "     TS0 == [not] swap unit [pop] swoncat\n",
    "     TS1 == [dip] cons [i] swoncat\n",
    "treestep == swap [map] swoncat [TS1 [TS0] dip] dip genrec\n",
    "\n",
    "''', D)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "       [] 0 [C] [N] treestep\n",
    "    ---------------------------\n",
    "          0\n",
    "\n",
    "\n",
    "          [n [tree*]] 0 [sum +] [] treestep\n",
    "       --------------------------------------------------\n",
    "           n [tree*] [0 [sum +] [] treestep] map sum +"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "J('[] 0 [sum +] [] treestep')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23\n"
     ]
    }
   ],
   "source": [
    "J('[23 []] 0 [sum +] [] treestep')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28\n"
     ]
    }
   ],
   "source": [
    "J('[23 [[2 []] [3 []]]] 0 [sum +] [] treestep')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A slight modification.\n",
    "Let's simplify the tree datastructure definition slightly by just letting the children be the `rest` of the tree:\n",
    "\n",
    "    tree = [] | [node tree*]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `J` function changes slightly.\n",
    "\n",
    "            [node tree*] J\n",
    "    ------------------------------\n",
    "       node N [tree*] [K] map C\n",
    "\n",
    "\n",
    "    [node tree*] uncons [N] dip [K] map C\n",
    "    node [tree*]        [N] dip [K] map C\n",
    "    node N [tree*]              [K] map C\n",
    "    node N [tree*]              [K] map C\n",
    "    node N [K.tree*]                    C\n",
    "\n",
    "    J == uncons [N] dip [K] map C\n",
    "\n",
    "    K == [not] [pop z] [uncons [N] dip] [map C] genrec\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('TS1 == [dip] cons [uncons] swoncat')  # We only need to redefine one word."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28\n"
     ]
    }
   ],
   "source": [
    "J('[23 [2] [3]] 0 [sum +] [] treestep')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "49\n"
     ]
    }
   ],
   "source": [
    "J('[23 [2 [8] [9]] [3] [4 []]] 0 [sum +] [] treestep')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I think these trees seem a little easier to read."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Redefining our BTree in terms of this form.\n",
    "\n",
    "    BTree = [] | [[key value] left right]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What kind of functions can we write for this with our `treestep`?  The pattern for processing a non-empty node is:\n",
    "\n",
    "    node N [tree*] [K] map C\n",
    "\n",
    "Plugging in our BTree structure:\n",
    "\n",
    "    [key value] N [left right] [K] map C\n",
    "\n",
    "\n",
    "    [key value] uncons pop [left right] [K] map i\n",
    "    key [value]        pop [left right] [K] map i\n",
    "    key                    [left right] [K] map i\n",
    "    key                    [lkey rkey ]         i\n",
    "    key                     lkey rkey\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 23 23\n"
     ]
    }
   ],
   "source": [
    "J('[[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]]   23 [i] [uncons pop] treestep')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Doesn't work because `map` extracts the `first` item of whatever its mapped function produces.  We have to return a list, rather than depositing our results directly on the stack.\n",
    "\n",
    "\n",
    "    [key value] N     [left right] [K] map C\n",
    "\n",
    "    [key value] first [left right] [K] map flatten cons\n",
    "    key               [left right] [K] map flatten cons\n",
    "    key               [[lk] [rk] ]         flatten cons\n",
    "    key               [ lk   rk  ]                 cons\n",
    "                      [key  lk   rk  ]\n",
    "\n",
    "So:\n",
    "\n",
    "    [] [flatten cons] [first] treestep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 2 9 5 4 8 6 7]\n"
     ]
    }
   ],
   "source": [
    "J('[[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]]   [] [flatten cons] [first] treestep')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There we go.\n",
    "#### In-order traversal with `treestep`.\n",
    "\n",
    "From here:\n",
    "\n",
    "    key [[lk] [rk]] C\n",
    "    key [[lk] [rk]] i\n",
    "    key  [lk] [rk] roll<\n",
    "    [lk] [rk] key swons concat\n",
    "    [lk] [key rk]       concat\n",
    "    [lk   key rk]\n",
    "\n",
    "So:\n",
    "\n",
    "    [] [i roll< swons concat] [first] treestep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "J('[[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]]   [] [i roll< swons concat] [uncons pop] treestep')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Miscellaneous Crap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Toy with it.\n",
    "Let's reexamine:\n",
    "\n",
    "    [key value left right] R0 [BTree-iter-order] R1\n",
    "        ...\n",
    "    left BTree-iter-order key value F right BTree-iter-order\n",
    "\n",
    "\n",
    "    [key value left right] unstack swap\n",
    "     key value left right               swap\n",
    "     key value right left\n",
    "\n",
    "    key value right left [BTree-iter-order] [cons dipdd] dupdip\n",
    "    key value right left [BTree-iter-order] cons dipdd [BTree-iter-order]\n",
    "    key value right [left BTree-iter-order]      dipdd [BTree-iter-order]\n",
    "    left BTree-iter-order key value right              [BTree-iter-order]\n",
    "\n",
    "    left BTree-iter-order key value   right [F] dip [BTree-iter-order]\n",
    "    left BTree-iter-order key value F right         [BTree-iter-order] i\n",
    "    left BTree-iter-order key value F right          BTree-iter-order\n",
    "\n",
    "So:\n",
    "\n",
    "    R0 == unstack swap\n",
    "    R1 == [cons dipdd [F] dip] dupdip i\n",
    "\n",
    "    [key value left right] R0                [BTree-iter-order] R1\n",
    "    [key value left right] unstack swap [BTree-iter-order] [cons dipdd [F] dip] dupdip i\n",
    "     key value right left                    [BTree-iter-order] [cons dipdd [F] dip] dupdip i\n",
    "\n",
    "     key value right left [BTree-iter-order] cons dipdd [F] dip [BTree-iter-order] i\n",
    "     key value right [left BTree-iter-order]      dipdd [F] dip [BTree-iter-order] i\n",
    "     left BTree-iter-order key value   right            [F] dip [BTree-iter-order] i\n",
    "     left BTree-iter-order key value F right                    [BTree-iter-order] i\n",
    "     left BTree-iter-order key value F right                     BTree-iter-order\n",
    "\n",
    "\n",
    "    BTree-iter-order == [not] [pop] [unstack swap] [[cons dipdd [F] dip] dupdip i] genrec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Refactor `cons cons`\n",
    "    cons2 == cons cons\n",
    "\n",
    "Refactoring:\n",
    "\n",
    "    BTree-new == swap [[] []] cons2\n",
    "    T == [cons2 dipdd] cons2 cons infra\n",
    "    Te == [cons2 dipd] cons2 cons infra\n",
    "    Ee == pop swap roll< rest rest cons2\n",
    "\n",
    "It's used a lot because it's tied to the fact that there are two \"data items\" in each node.  This point to a more general factorization that would render a combinator that could work for other geometries of trees."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A General Form for Trees\n",
    "A general form for tree data with N children per node:\n",
    "\n",
    "    [[data] [child0] ... [childN-1]]\n",
    "\n",
    "Suggests a general form of recursive iterator, but I have to go walk the dogs at the mo'.\n",
    "\n",
    "For a given structure, you would have a structure of operator functions and sort of merge them and run them, possibly in a different order (pre- post- in- y'know).  The `Cn` functions could all be the same and use the `step` trick if the children nodes are all of the right kind.  If they are heterogeneous then we need a way to get the different `Cn` into the structure in the right order.  If I understand correctly, the \"Bananas...\" paper shows how to do this automatically from a type description.  They present, if I have it right, a tiny machine that accepts [some sort of algebraic data type description and returns a function that can recusre over it](https://en.wikipedia.org/wiki/Catamorphism#General_case), I think.\n",
    "\n",
    "       [data.. [c0] [c1] ... [cN]] [F C0 C1 ... CN] infil\n",
    "    --------------------------------------------------------\n",
    "       data F [c0] C0 [c1] C1 ... [cN] CN\n",
    "       \n",
    "       "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Just make `[F]` a parameter.\n",
    "We can generalize to a sort of pure form:\n",
    "\n",
    "    BTree-iter == [not] [pop] [[F]]            [R1] genrec\n",
    "               == [not] [pop] [[F] [BTree-iter] R1] ifte\n",
    "\n",
    "Putting `[F]` to the left as a given:\n",
    "\n",
    "     [F] unit [not] [pop] roll< [R1] genrec\n",
    "    [[F]]     [not] [pop] roll< [R1] genrec\n",
    "              [not] [pop] [[F]] [R1] genrec\n",
    "\n",
    "Let's us define a parameterized form:\n",
    "\n",
    "    BTree-iter == unit [not] [pop] roll< [R1] genrec\n",
    "\n",
    "So in the general case of non-empty nodes:\n",
    "\n",
    "    [key value left right] [F] [BTree-iter] R1\n",
    "\n",
    "We just define `R1` to do whatever it has to to process the node.  For example:\n",
    "\n",
    "    [key value left right] [F] [BTree-iter] R1\n",
    "        ...\n",
    "    key value F   left BTree-iter   right BTree-iter\n",
    "    left BTree-iter   key value F   right BTree-iter\n",
    "    left BTree-iter   right BTree-iter   key value F\n",
    "\n",
    "Pre-, ??-, post-order traversals.\n",
    "\n",
    "    [key value  left right] uncons uncons\n",
    "     key value [left right]\n",
    "\n",
    "For pre- and post-order we can use the `step` trick:\n",
    "\n",
    "    [left right] [BTree-iter] step\n",
    "        ...\n",
    "    left BTree-iter right BTree-iter\n",
    "\n",
    "We worked out one scheme for ?in-order? traversal above, but maybe we can do better?\n",
    "\n",
    "    [key value left right]              [F] [BTree-iter] [unstack] dipd\n",
    "    [key value left right] unstack [F] [BTree-iter]\n",
    "     key value left right               [F] [BTree-iter]\n",
    "\n",
    "    key value left right [F] [BTree-iter] R1.1\n",
    "\n",
    "Hmm...\n",
    "\n",
    "    key value left right              [F] [BTree-iter] tuck\n",
    "    key value left right [BTree-iter] [F] [BTree-iter] \n",
    "\n",
    "\n",
    "    [key value left right]                          [F] [BTree-iter] [unstack [roll>] dip] dipd\n",
    "    [key value left right] unstack [roll>] dip [F] [BTree-iter]\n",
    "     key value left right               [roll>] dip [F] [BTree-iter]\n",
    "     key value left roll> right                     [F] [BTree-iter]\n",
    "     left key value right                           [F] [BTree-iter]\n",
    "\n",
    "    left            key value   right              [F] [BTree-iter] tuck foo\n",
    "    left            key value   right [BTree-iter] [F] [BTree-iter] foo\n",
    "        ...\n",
    "    left BTree-iter key value F right  BTree-iter\n",
    "\n",
    "We could just let `[R1]` be a parameter too, for maximum flexibility.\n",
    "\n",
    "#### Automatically deriving the recursion combinator for a data type?\n",
    "\n",
    "If I understand it correctly, the \"Bananas...\" paper talks about a way to build the processor function automatically from the description of the type.  I think if we came up with an elegant way for the Joy code to express that, it would be cool.  In Joypy the definitions can be circular because lookup happens at evaluation, not parsing.  E.g.:\n",
    "\n",
    "    A == ... B ...\n",
    "    B == ... A ...\n",
    "\n",
    "That's fine.  Circular datastructures can't be made though.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
