OSDN Git Service

Clean up Zipper notebook.
[joypy/Thun.git] / docs / Zipper.ipynb
index 719c7f1..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]"
      ]
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "[1 [2 [3 4 625 6] 7] 8] [[[[[[sqr] dipd] infra] dip] infra] dip] • infra\n",
-      "                                           8 [2 [3 4 625 6] 7] 1 • [[[[[sqr] dipd] infra] dip] infra] dip [] swaack\n",
-      "        8 [2 [3 4 625 6] 7] 1 [[[[[sqr] dipd] infra] dip] infra] • dip [] swaack\n",
-      "                                             8 [2 [3 4 625 6] 7] • [[[[sqr] dipd] infra] dip] infra 1 [] swaack\n",
-      "                  8 [2 [3 4 625 6] 7] [[[[sqr] dipd] infra] dip] • infra 1 [] swaack\n",
-      "                                                 7 [3 4 625 6] 2 • [[[sqr] dipd] infra] dip [8] swaack 1 [] swaack\n",
-      "                            7 [3 4 625 6] 2 [[[sqr] dipd] infra] • dip [8] swaack 1 [] swaack\n",
-      "                                                   7 [3 4 625 6] • [[sqr] dipd] infra 2 [8] swaack 1 [] swaack\n",
-      "                                      7 [3 4 625 6] [[sqr] dipd] • infra 2 [8] swaack 1 [] swaack\n",
-      "                                                       6 625 4 3 • [sqr] dipd [7] swaack 2 [8] swaack 1 [] swaack\n",
-      "                                                 6 625 4 3 [sqr] • dipd [7] swaack 2 [8] swaack 1 [] swaack\n",
-      "                                                           6 625 • sqr 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
-      "                                                           6 625 • dup * 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
-      "                                                       6 625 625 • * 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
-      "                                                        6 390625 • 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
-      "                                                      6 390625 4 • 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
-      "                                                    6 390625 4 3 • [7] swaack 2 [8] swaack 1 [] swaack\n",
-      "                                                6 390625 4 3 [7] • swaack 2 [8] swaack 1 [] swaack\n",
-      "                                                7 [3 4 390625 6] • 2 [8] swaack 1 [] swaack\n",
-      "                                              7 [3 4 390625 6] 2 • [8] swaack 1 [] swaack\n",
-      "                                          7 [3 4 390625 6] 2 [8] • swaack 1 [] swaack\n",
-      "                                          8 [2 [3 4 390625 6] 7] • 1 [] swaack\n",
-      "                                        8 [2 [3 4 390625 6] 7] 1 • [] swaack\n",
-      "                                     8 [2 [3 4 390625 6] 7] 1 [] • swaack\n",
-      "                                      [1 [2 [3 4 390625 6] 7] 8] • \n",
-      "\n",
       "[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."
    ]
     {
      "name": "stdout",
      "output_type": "stream",
-     "text": []
+     "text": [
+      "[sqr] [[dip] [dip] [infra] [dip] [infra] [dip] [infra]]"
+     ]
     }
    ],
    "source": [
-    "clear"
+    "clear\n",
+    "[sqr]\n",
+    "[[dip][dip][infra][dip][infra][dip][infra]]"
    ]
   },
   {
     {
      "name": "stdout",
      "output_type": "stream",
-     "text": []
+     "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."
    ]
   },
   {
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "               1 [2 3 4] • Z\n",
-      "               1 [2 3 4] • [[] ccons] step i\n",
-      "    1 [2 3 4] [[] ccons] • step i\n",
-      "          1 2 [[] ccons] • i [3 4] [[] ccons] step i\n",
-      "                     1 2 • [] ccons [3 4] [[] ccons] step i\n",
-      "                  1 2 [] • ccons [3 4] [[] ccons] step i\n",
-      "                   [1 2] • [3 4] [[] ccons] step i\n",
-      "             [1 2] [3 4] • [[] ccons] step i\n",
-      "  [1 2] [3 4] [[] ccons] • step i\n",
-      "      [1 2] 3 [[] ccons] • i [4] [[] ccons] step i\n",
-      "                 [1 2] 3 • [] ccons [4] [[] ccons] step i\n",
-      "              [1 2] 3 [] • ccons [4] [[] ccons] step i\n",
-      "               [[1 2] 3] • [4] [[] ccons] step i\n",
-      "           [[1 2] 3] [4] • [[] ccons] step i\n",
-      "[[1 2] 3] [4] [[] ccons] • step i\n",
-      "  [[1 2] 3] 4 [[] ccons] • i i\n",
-      "             [[1 2] 3] 4 • [] ccons i\n",
-      "          [[1 2] 3] 4 [] • ccons i\n",
-      "           [[[1 2] 3] 4] • i\n",
-      "                         • [[1 2] 3] 4\n",
-      "               [[1 2] 3] • 4\n",
-      "             [[1 2] 3] 4 • \n",
-      "\n",
-      "[[1 2] 3] 4"
+      "[1 [2 [3 4 25 6] 7] 8] [[[[[[[[sqr] dip] dip] infra] dip] infra] dip] infra]"
      ]
     }
    ],
    "source": [
-    "1 [2 3 4] [Z] trace"
+    "[1 [2 [3 4 25 6] 7] 8] swap"
    ]
   },
   {
     {
      "name": "stdout",
      "output_type": "stream",
-     "text": []
+     "text": [
+      "[1 [2 [3 4 625 6] 7] 8]"
+     ]
     }
    ],
    "source": [
-    "clear"
+    "i"
    ]
   },
   {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "And here it is doing the thing."
+    "So let's define `Z` as:\n",
+    "\n",
+    "    Z == [cons] step i"
    ]
   },
   {
     }
    ],
    "source": [
-    "[1 [2 [3 4 25 6] 7] 8] [sqr] [dip dip infra dip infra dip infra] Z"
+    "[Z [cons] step i] inscribe"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "And here it is doing the thing."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 20,
+   "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": [
+    "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": [
+    "Z"
    ]
   },
   {