OSDN Git Service

Clean up Zipper notebook.
[joypy/Thun.git] / docs / Zipper.ipynb
1 {
2  "cells": [
3   {
4    "cell_type": "markdown",
5    "metadata": {},
6    "source": [
7     "# Traversing Datastructures with Zippers\n",
8     "This notebook is about using the \"zipper\" with joy datastructures.  See the [Zipper wikipedia entry](https://en.wikipedia.org/wiki/Zipper_%28data_structure%29) or the original paper: [\"FUNCTIONAL PEARL The Zipper\" by Gérard Huet](https://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf)\n",
9     "\n",
10     "\n",
11     "Given a datastructure on the stack we can navigate through it, modify it, and rebuild it using the \"zipper\" technique."
12    ]
13   },
14   {
15    "cell_type": "markdown",
16    "metadata": {},
17    "source": [
18     "## Trees\n",
19     "In Joypy there aren't any complex datastructures, just ints, floats, strings, Symbols (strings that are names of functions) and sequences (aka lists, aka quoted literals, aka aggregates, etc...), but we can build [trees](https://en.wikipedia.org/wiki/Tree_%28data_structure%29) out of sequences."
20    ]
21   },
22   {
23    "cell_type": "code",
24    "execution_count": 1,
25    "metadata": {},
26    "outputs": [
27     {
28      "name": "stdout",
29      "output_type": "stream",
30      "text": [
31       "[1 [2 [3 4 25 6] 7] 8]"
32      ]
33     }
34    ],
35    "source": [
36     "[1 [2 [3 4 25 6] 7] 8]"
37    ]
38   },
39   {
40    "cell_type": "markdown",
41    "metadata": {},
42    "source": [
43     "## Zipper in Joy\n",
44     "Zippers work by keeping track of the current item, the already-seen items, and the yet-to-be seen items as you traverse a datastructure (the datastructure used to keep track of these items is the zipper.)\n",
45     "\n",
46     "In Joy we can do this with the following words:\n",
47     "\n",
48     "    z-down == [] swap uncons swap\n",
49     "    z-up == swons swap shunt\n",
50     "    z-right == [swons] cons dip uncons swap\n",
51     "    z-left == swons [uncons swap] dip swap\n",
52     "\n",
53     "Let's use them to change 25 into 625.  The first time a word is used I show the trace so you can see how it works.  If we were going to use these a lot it would make sense to write Python versions for efficiency, but see below."
54    ]
55   },
56   {
57    "cell_type": "code",
58    "execution_count": 2,
59    "metadata": {},
60    "outputs": [
61     {
62      "name": "stdout",
63      "output_type": "stream",
64      "text": [
65       "[1 [2 [3 4 25 6] 7] 8]"
66      ]
67     }
68    ],
69    "source": [
70     "[z-down [] swap uncons swap] inscribe\n",
71     "[z-up swons swap shunt] inscribe\n",
72     "[z-right roll< cons swap uncons swap] inscribe\n",
73     "[z-left swons [uncons swap] dip swap] inscribe"
74    ]
75   },
76   {
77    "cell_type": "code",
78    "execution_count": 3,
79    "metadata": {},
80    "outputs": [
81     {
82      "name": "stdout",
83      "output_type": "stream",
84      "text": [
85       "   [1 [2 [3 4 25 6] 7] 8] • z-down\n",
86       "   [1 [2 [3 4 25 6] 7] 8] • [] swap uncons swap\n",
87       "[1 [2 [3 4 25 6] 7] 8] [] • swap uncons swap\n",
88       "[] [1 [2 [3 4 25 6] 7] 8] • uncons swap\n",
89       "[] 1 [[2 [3 4 25 6] 7] 8] • swap\n",
90       "[] [[2 [3 4 25 6] 7] 8] 1 • \n",
91       "\n",
92       "[] [[2 [3 4 25 6] 7] 8] 1"
93      ]
94     }
95    ],
96    "source": [
97     "[z-down] trace"
98    ]
99   },
100   {
101    "cell_type": "code",
102    "execution_count": 4,
103    "metadata": {},
104    "outputs": [
105     {
106      "name": "stdout",
107      "output_type": "stream",
108      "text": [
109       "[] [[2 [3 4 25 6] 7] 8] 1 • z-right\n",
110       "[] [[2 [3 4 25 6] 7] 8] 1 • roll< cons swap uncons swap\n",
111       "[[2 [3 4 25 6] 7] 8] 1 [] • cons swap uncons swap\n",
112       " [[2 [3 4 25 6] 7] 8] [1] • swap uncons swap\n",
113       " [1] [[2 [3 4 25 6] 7] 8] • uncons swap\n",
114       " [1] [2 [3 4 25 6] 7] [8] • swap\n",
115       " [1] [8] [2 [3 4 25 6] 7] • \n",
116       "\n",
117       "[1] [8] [2 [3 4 25 6] 7]"
118      ]
119     }
120    ],
121    "source": [
122     "[z-right] trace"
123    ]
124   },
125   {
126    "cell_type": "code",
127    "execution_count": 5,
128    "metadata": {},
129    "outputs": [
130     {
131      "name": "stdout",
132      "output_type": "stream",
133      "text": [
134       "[1] [8] [] [[3 4 25 6] 7] 2"
135      ]
136     }
137    ],
138    "source": [
139     "z-down"
140    ]
141   },
142   {
143    "cell_type": "code",
144    "execution_count": 6,
145    "metadata": {},
146    "outputs": [
147     {
148      "name": "stdout",
149      "output_type": "stream",
150      "text": [
151       "[1] [8] [2] [7] [3 4 25 6]"
152      ]
153     }
154    ],
155    "source": [
156     "z-right"
157    ]
158   },
159   {
160    "cell_type": "code",
161    "execution_count": 7,
162    "metadata": {},
163    "outputs": [
164     {
165      "name": "stdout",
166      "output_type": "stream",
167      "text": [
168       "[1] [8] [2] [7] [] [4 25 6] 3"
169      ]
170     }
171    ],
172    "source": [
173     "z-down"
174    ]
175   },
176   {
177    "cell_type": "code",
178    "execution_count": 8,
179    "metadata": {},
180    "outputs": [
181     {
182      "name": "stdout",
183      "output_type": "stream",
184      "text": [
185       "[1] [8] [2] [7] [3] [25 6] 4"
186      ]
187     }
188    ],
189    "source": [
190     "z-right"
191    ]
192   },
193   {
194    "cell_type": "code",
195    "execution_count": 9,
196    "metadata": {},
197    "outputs": [
198     {
199      "name": "stdout",
200      "output_type": "stream",
201      "text": [
202       "[1] [8] [2] [7] [4 3] [6] 25"
203      ]
204     }
205    ],
206    "source": [
207     "z-right"
208    ]
209   },
210   {
211    "cell_type": "code",
212    "execution_count": 10,
213    "metadata": {},
214    "outputs": [
215     {
216      "name": "stdout",
217      "output_type": "stream",
218      "text": [
219       "[1] [8] [2] [7] [4 3] [6] 625"
220      ]
221     }
222    ],
223    "source": [
224     "sqr"
225    ]
226   },
227   {
228    "cell_type": "code",
229    "execution_count": 11,
230    "metadata": {},
231    "outputs": [
232     {
233      "name": "stdout",
234      "output_type": "stream",
235      "text": [
236       "[1] [8] [2] [7] [4 3] [6] 625 • z-up\n",
237       "[1] [8] [2] [7] [4 3] [6] 625 • swons swap shunt\n",
238       "[1] [8] [2] [7] [4 3] [625 6] • swap shunt\n",
239       "[1] [8] [2] [7] [625 6] [4 3] • shunt\n",
240       "  [1] [8] [2] [7] [3 4 625 6] • \n",
241       "\n",
242       "[1] [8] [2] [7] [3 4 625 6]"
243      ]
244     }
245    ],
246    "source": [
247     "[z-up] trace"
248    ]
249   },
250   {
251    "cell_type": "code",
252    "execution_count": 12,
253    "metadata": {},
254    "outputs": [
255     {
256      "name": "stdout",
257      "output_type": "stream",
258      "text": [
259       "[1] [8] [2 [3 4 625 6] 7]"
260      ]
261     }
262    ],
263    "source": [
264     "z-up"
265    ]
266   },
267   {
268    "cell_type": "code",
269    "execution_count": 13,
270    "metadata": {},
271    "outputs": [
272     {
273      "name": "stdout",
274      "output_type": "stream",
275      "text": [
276       "[1 [2 [3 4 625 6] 7] 8]"
277      ]
278     }
279    ],
280    "source": [
281     "z-up"
282    ]
283   },
284   {
285    "cell_type": "markdown",
286    "metadata": {},
287    "source": [
288     "## `dip` and `infra`\n",
289     "In Joy we have the `dip` and `infra` combinators which can \"target\" or \"address\" any particular item in a Joy tree structure."
290    ]
291   },
292   {
293    "cell_type": "code",
294    "execution_count": 14,
295    "metadata": {},
296    "outputs": [
297     {
298      "name": "stdout",
299      "output_type": "stream",
300      "text": [
301       "[1 [2 [3 4 390625 6] 7] 8]"
302      ]
303     }
304    ],
305    "source": [
306     "[[[[[[sqr] dipd] infra] dip] infra] dip] infra"
307    ]
308   },
309   {
310    "cell_type": "markdown",
311    "metadata": {},
312    "source": [
313     "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",
314     "\n",
315     "## `Z`\n",
316     "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",
317     "\n",
318     "       [...] [Q] [[dip] [dip] [infra] [dip] [infra] [dip] [infra]] Z\n",
319     "    -------------------------------------------------------------------\n",
320     "         [...] [[[[[[[Q] dip] dip] infra] dip] infra] dip] infra\n",
321     "       \n",
322     "The `Z` function isn't hard to make."
323    ]
324   },
325   {
326    "cell_type": "code",
327    "execution_count": 15,
328    "metadata": {},
329    "outputs": [
330     {
331      "name": "stdout",
332      "output_type": "stream",
333      "text": [
334       "[sqr] [[dip] [dip] [infra] [dip] [infra] [dip] [infra]]"
335      ]
336     }
337    ],
338    "source": [
339     "clear\n",
340     "[sqr]\n",
341     "[[dip][dip][infra][dip][infra][dip][infra]]"
342    ]
343   },
344   {
345    "cell_type": "code",
346    "execution_count": 16,
347    "metadata": {},
348    "outputs": [
349     {
350      "name": "stdout",
351      "output_type": "stream",
352      "text": [
353       "[[[[[[[[sqr] dip] dip] infra] dip] infra] dip] infra]"
354      ]
355     }
356    ],
357    "source": [
358     "[cons] step"
359    ]
360   },
361   {
362    "cell_type": "markdown",
363    "metadata": {},
364    "source": [
365     "To use it you need to run the resulting program with the `i` combinator."
366    ]
367   },
368   {
369    "cell_type": "code",
370    "execution_count": 17,
371    "metadata": {},
372    "outputs": [
373     {
374      "name": "stdout",
375      "output_type": "stream",
376      "text": [
377       "[1 [2 [3 4 25 6] 7] 8] [[[[[[[[sqr] dip] dip] infra] dip] infra] dip] infra]"
378      ]
379     }
380    ],
381    "source": [
382     "[1 [2 [3 4 25 6] 7] 8] swap"
383    ]
384   },
385   {
386    "cell_type": "code",
387    "execution_count": 18,
388    "metadata": {},
389    "outputs": [
390     {
391      "name": "stdout",
392      "output_type": "stream",
393      "text": [
394       "[1 [2 [3 4 625 6] 7] 8]"
395      ]
396     }
397    ],
398    "source": [
399     "i"
400    ]
401   },
402   {
403    "cell_type": "markdown",
404    "metadata": {},
405    "source": [
406     "So let's define `Z` as:\n",
407     "\n",
408     "    Z == [cons] step i"
409    ]
410   },
411   {
412    "cell_type": "code",
413    "execution_count": 19,
414    "metadata": {},
415    "outputs": [
416     {
417      "name": "stdout",
418      "output_type": "stream",
419      "text": [
420       "[1 [2 [3 4 625 6] 7] 8]"
421      ]
422     }
423    ],
424    "source": [
425     "[Z [cons] step i] inscribe"
426    ]
427   },
428   {
429    "cell_type": "markdown",
430    "metadata": {},
431    "source": [
432     "And here it is doing the thing."
433    ]
434   },
435   {
436    "cell_type": "code",
437    "execution_count": 20,
438    "metadata": {},
439    "outputs": [
440     {
441      "name": "stdout",
442      "output_type": "stream",
443      "text": [
444       "[1 [2 [3 4 25 6] 7] 8] [sqr] [[dip] [dip] [infra] [dip] [infra] [dip] [infra]]"
445      ]
446     }
447    ],
448    "source": [
449     "clear\n",
450     "[1 [2 [3 4 25 6] 7] 8]\n",
451     "[sqr]\n",
452     "[[dip][dip][infra][dip][infra][dip][infra]]"
453    ]
454   },
455   {
456    "cell_type": "code",
457    "execution_count": 21,
458    "metadata": {},
459    "outputs": [
460     {
461      "name": "stdout",
462      "output_type": "stream",
463      "text": [
464       "[1 [2 [3 4 625 6] 7] 8]"
465      ]
466     }
467    ],
468    "source": [
469     "Z"
470    ]
471   },
472   {
473    "cell_type": "markdown",
474    "metadata": {},
475    "source": [
476     "## Addressing\n",
477     "Because we are only using two combinators we could replace the list with a string made from only two characters.\n",
478     "\n",
479     "       [...] [Q] 'ddididi' Zstr\n",
480     "    -------------------------------------------------------------\n",
481     "       [...] [[[[[[[Q] dip] dip] infra] dip] infra] dip] infra\n",
482     "\n",
483     "The string can be considered a name or address for an item in the subject datastructure.\n",
484     "\n",
485     "## Determining the right \"path\" for an item in a tree.\n",
486     "It's easy to read off (in reverse) the right sequence of \"d\" and \"i\" from the subject datastructure:\n",
487     "\n",
488     "    [ n [ n [ n n x ...\n",
489     "    i d i d i d d Bingo!"
490    ]
491   }
492  ],
493  "metadata": {
494   "kernelspec": {
495    "display_name": "Joypy",
496    "language": "",
497    "name": "thun"
498   },
499   "language_info": {
500    "file_extension": ".joy",
501    "mimetype": "text/plain",
502    "name": "Joy"
503   }
504  },
505  "nbformat": 4,
506  "nbformat_minor": 2
507 }