OSDN Git Service

Clean up Zipper notebook.
[joypy/Thun.git] / docs / Zipper.ipynb
index 18b6d5b..6514fe3 100644 (file)
@@ -69,7 +69,7 @@
    "source": [
     "[z-down [] swap uncons swap] inscribe\n",
     "[z-up swons swap shunt] inscribe\n",
-    "[z-right [swons] cons dip uncons swap] inscribe\n",
+    "[z-right roll< cons swap uncons swap] inscribe\n",
     "[z-left swons [uncons swap] dip swap] inscribe"
    ]
   },
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "        [] [[2 [3 4 25 6] 7] 8] 1 • z-right\n",
-      "        [] [[2 [3 4 25 6] 7] 8] 1 • [swons] cons dip uncons swap\n",
-      "[] [[2 [3 4 25 6] 7] 8] 1 [swons] • cons dip uncons swap\n",
-      "[] [[2 [3 4 25 6] 7] 8] [1 swons] • dip uncons swap\n",
-      "                               [] • 1 swons [[2 [3 4 25 6] 7] 8] uncons swap\n",
-      "                             [] 1 • swons [[2 [3 4 25 6] 7] 8] uncons swap\n",
-      "                              [1] • [[2 [3 4 25 6] 7] 8] uncons swap\n",
-      "         [1] [[2 [3 4 25 6] 7] 8] • uncons swap\n",
-      "         [1] [2 [3 4 25 6] 7] [8] • swap\n",
-      "         [1] [8] [2 [3 4 25 6] 7] • \n",
+      "[] [[2 [3 4 25 6] 7] 8] 1 • z-right\n",
+      "[] [[2 [3 4 25 6] 7] 8] 1 • roll< cons swap uncons swap\n",
+      "[[2 [3 4 25 6] 7] 8] 1 [] • cons swap uncons swap\n",
+      " [[2 [3 4 25 6] 7] 8] [1] • swap uncons swap\n",
+      " [1] [[2 [3 4 25 6] 7] 8] • uncons swap\n",
+      " [1] [2 [3 4 25 6] 7] [8] • swap\n",
+      " [1] [8] [2 [3 4 25 6] 7] • \n",
       "\n",
       "[1] [8] [2 [3 4 25 6] 7]"
      ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 10,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1] [8] [2] [7] [4 3] [6] 625"
+     ]
+    }
+   ],
    "source": [
     "sqr"
    ]
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "!= % & * + ++ - -- / // /floor < << <= <> = > >= >> ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add and app1 app2 app3 at b bool branch ccons choice clear cmp concat cond cons dip dipd dipdd disenstacken div divmod drop dup dupd dupdd dupdip eq first first_two floor floordiv fourth gcd2 ge genrec getitem gt help i id infra inscribe le loop lshift lt map max min mod modulus mul ne neg not or over parse pick pm pop popd popdd popop popopd popopdd pow pred primrec rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift second select sharing shunt sort sqrt stack step stuncons stununcons sub succ sum swaack swap swons take third times trace truthy tuck uncons unique unit unswons void warranty words x xor z-down z-left z-right z-up zip •\n",
+      "[1] [8] [2] [7] [4 3] [6] 625 • z-up\n",
+      "[1] [8] [2] [7] [4 3] [6] 625 • swons swap shunt\n",
+      "[1] [8] [2] [7] [4 3] [625 6] • swap shunt\n",
+      "[1] [8] [2] [7] [625 6] [4 3] • shunt\n",
+      "  [1] [8] [2] [7] [3 4 625 6] • \n",
       "\n",
-      "[1] [8] [2] [7] [4 3] [6] 25"
+      "[1] [8] [2] [7] [3 4 625 6]"
      ]
     }
    ],
    "source": [
-    "words"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
     "[z-up] trace"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 12,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1] [8] [2 [3 4 625 6] 7]"
+     ]
+    }
+   ],
    "source": [
     "z-up"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 13,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1 [2 [3 4 625 6] 7] 8]"
+     ]
+    }
+   ],
    "source": [
     "z-up"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 14,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1 [2 [3 4 390625 6] 7] 8]"
+     ]
+    }
+   ],
    "source": [
-    "[[[[[[sqr] dipd] infra] dip] infra] dip] [infra] trace"
+    "[[[[[[sqr] dipd] infra] dip] infra] dip] infra"
    ]
   },
   {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "If you read the trace carefully you'll see that about half of it is the `dip` and `infra` combinators de-quoting programs and \"digging\" into the subject datastructure.  Instead of maintaining temporary results on the stack they are pushed into the pending expression (continuation).  When `sqr` has run the rest of the pending expression  rebuilds the datastructure.\n",
+    "If you were to trace the program you would see that about half of it is the `dip` and `infra` combinators de-quoting programs and \"digging\" into the subject datastructure.  Instead of maintaining temporary results on the stack they are pushed into the pending expression (continuation).  When `sqr` has run the rest of the pending expression  rebuilds the datastructure.\n",
     "\n",
     "## `Z`\n",
     "Imagine a function `Z` that accepts a sequence of `dip` and `infra` combinators, a quoted program `[Q]`, and a datastructure to work on.  It would effectively execute the quoted program as if it had been embedded in a nested series of quoted programs, e.g.:\n",
     "\n",
-    "       [...] [Q] [dip dip infra dip infra dip infra] Z\n",
-    "    -------------------------------------------------------------\n",
-    "       [...] [[[[[[[Q] dip] dip] infra] dip] infra] dip] infra\n",
+    "       [...] [Q] [[dip] [dip] [infra] [dip] [infra] [dip] [infra]] Z\n",
+    "    -------------------------------------------------------------------\n",
+    "         [...] [[[[[[[Q] dip] dip] infra] dip] infra] dip] infra\n",
     "       \n",
     "The `Z` function isn't hard to make."
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 15,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[sqr] [[dip] [dip] [infra] [dip] [infra] [dip] [infra]]"
+     ]
+    }
+   ],
    "source": [
-    "clear"
+    "clear\n",
+    "[sqr]\n",
+    "[[dip][dip][infra][dip][infra][dip][infra]]"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 16,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[[[[[[[[sqr] dip] dip] infra] dip] infra] dip] infra]"
+     ]
+    }
+   ],
    "source": [
-    "[Z [[] ccons] step i] inscribe"
+    "[cons] step"
    ]
   },
   {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "Here it is in action in a simplified scenario."
+    "To use it you need to run the resulting program with the `i` combinator."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 17,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1 [2 [3 4 25 6] 7] 8] [[[[[[[[sqr] dip] dip] infra] dip] infra] dip] infra]"
+     ]
+    }
+   ],
+   "source": [
+    "[1 [2 [3 4 25 6] 7] 8] swap"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 18,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1 [2 [3 4 625 6] 7] 8]"
+     ]
+    }
+   ],
+   "source": [
+    "i"
+   ]
+  },
+  {
+   "cell_type": "markdown",
    "metadata": {},
-   "outputs": [],
    "source": [
-    "1 [2 3 4] [Z] trace"
+    "So let's define `Z` as:\n",
+    "\n",
+    "    Z == [cons] step i"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 19,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1 [2 [3 4 625 6] 7] 8]"
+     ]
+    }
+   ],
    "source": [
-    "clear"
+    "[Z [cons] step i] inscribe"
    ]
   },
   {
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 20,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1 [2 [3 4 25 6] 7] 8] [sqr] [[dip] [dip] [infra] [dip] [infra] [dip] [infra]]"
+     ]
+    }
+   ],
+   "source": [
+    "clear\n",
+    "[1 [2 [3 4 25 6] 7] 8]\n",
+    "[sqr]\n",
+    "[[dip][dip][infra][dip][infra][dip][infra]]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 21,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1 [2 [3 4 625 6] 7] 8]"
+     ]
+    }
+   ],
    "source": [
-    "[1 [2 [3 4 25 6] 7] 8] [sqr] [dip dip infra dip infra dip infra] Z"
+    "Z"
    ]
   },
   {