Minor docs edits.

This commit is contained in:
Simon Forman
2018-06-06 07:59:06 -07:00
parent 37c34fc54f
commit 22f7c6da00
4 changed files with 126 additions and 426 deletions
+45 -127
View File
@@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Treating Trees I\n",
"# 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",
@@ -92,7 +92,15 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['k' 'v' [] []]\n"
]
}
],
"source": [
"J('\"v\" \"k\" Tree-new')"
]
@@ -521,7 +529,7 @@
"metadata": {},
"source": [
"## Interlude: `cmp` combinator\n",
"Instead of mucking about with nested `ifte` combinators 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",
"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",
@@ -540,43 +548,6 @@
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"from joy.library import FunctionWrapper\n",
"from joy.utils.stack import pushback\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 = pushback(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": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
@@ -592,7 +563,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -609,7 +580,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -674,7 +645,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
@@ -713,7 +684,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
@@ -722,7 +693,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 25,
"metadata": {
"scrolled": false
},
@@ -865,7 +836,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
@@ -881,7 +852,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 27,
"metadata": {},
"outputs": [
{
@@ -898,7 +869,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@@ -915,7 +886,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 29,
"metadata": {},
"outputs": [
{
@@ -940,7 +911,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 30,
"metadata": {},
"outputs": [
{
@@ -957,7 +928,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
@@ -966,7 +937,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 32,
"metadata": {},
"outputs": [
{
@@ -990,7 +961,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
@@ -999,7 +970,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 34,
"metadata": {
"scrolled": true
},
@@ -1117,7 +1088,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
@@ -1141,7 +1112,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 36,
"metadata": {},
"outputs": [
{
@@ -1160,7 +1131,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Parameterizing the `[F]` function is left as an exercise for the reader (for now.)"
"Parameterizing the `[F]` function is left as an exercise for the reader."
]
},
{
@@ -1310,7 +1281,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
@@ -1333,7 +1304,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 38,
"metadata": {},
"outputs": [
{
@@ -1350,7 +1321,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 39,
"metadata": {},
"outputs": [
{
@@ -1367,7 +1338,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 40,
"metadata": {},
"outputs": [
{
@@ -1390,7 +1361,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 41,
"metadata": {},
"outputs": [
{
@@ -1552,55 +1523,6 @@
"We have to handle three cases, so let's use `cond`."
]
},
{
"cell_type": "code",
"execution_count": 42,
"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": "markdown",
"metadata": {},
@@ -1860,7 +1782,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Minor rearrangement:\n",
"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",
@@ -1879,9 +1801,9 @@
"\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",
" 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",
@@ -1899,7 +1821,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
@@ -1917,12 +1839,13 @@
"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''', D)"
"Tree-Delete == [pop not] [pop] [R0] [R1] genrec\n",
"''', D)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 43,
"metadata": {},
"outputs": [
{
@@ -1939,7 +1862,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 44,
"metadata": {},
"outputs": [
{
@@ -1956,7 +1879,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 45,
"metadata": {},
"outputs": [
{
@@ -1973,7 +1896,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 46,
"metadata": {},
"outputs": [
{
@@ -1990,7 +1913,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 47,
"metadata": {},
"outputs": [
{
@@ -2007,7 +1930,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 48,
"metadata": {},
"outputs": [
{
@@ -2024,7 +1947,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 49,
"metadata": {
"scrolled": true
},
@@ -2099,11 +2022,6 @@
" Tree-delete == [pop not] [pop] [_Tree_delete_R0] [_Tree_delete_R1] genrec\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {