{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "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",
    "\n",
    "Given a datastructure on the stack we can navigate through it, modify it, and rebuild it using the \"zipper\" technique."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Preamble"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from notebook_preamble import J, V, define"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Trees\n",
    "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."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 [2 [3 4 25 6] 7] 8]\n"
     ]
    }
   ],
   "source": [
    "J('[1 [2 [3 4 25 6] 7] 8]')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Zipper in Joy\n",
    "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",
    "\n",
    "In Joy we can do this with the following words:\n",
    "\n",
    "    z-down == [] swap uncons swap\n",
    "    z-up == swons swap shunt\n",
    "    z-right == [swons] cons dip uncons swap\n",
    "    z-left == swons [uncons swap] dip swap\n",
    "\n",
    "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."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('z-down == [] swap uncons swap')\n",
    "define('z-up == swons swap shunt')\n",
    "define('z-right == [swons] cons dip uncons swap')\n",
    "define('z-left == swons [uncons swap] dip swap')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          . [1 [2 [3 4 25 6] 7] 8] z-down\n",
      "   [1 [2 [3 4 25 6] 7] 8] . z-down\n",
      "   [1 [2 [3 4 25 6] 7] 8] . [] swap uncons swap\n",
      "[1 [2 [3 4 25 6] 7] 8] [] . 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",
      "[] [[2 [3 4 25 6] 7] 8] 1 . \n"
     ]
    }
   ],
   "source": [
    "V('[1 [2 [3 4 25 6] 7] 8] z-down')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "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 z-right\n",
      "          [] [[2 [3 4 25 6] 7] 8] . 1 z-right\n",
      "        [] [[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 . swap cons [[2 [3 4 25 6] 7] 8] uncons swap\n",
      "                             1 [] . cons [[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"
     ]
    }
   ],
   "source": [
    "V('[] [[2 [3 4 25 6] 7] 8] 1 z-right')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] [8] [] [[3 4 25 6] 7] 2\n"
     ]
    }
   ],
   "source": [
    "J('[1] [8] [2 [3 4 25 6] 7] z-down')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] [8] [2] [7] [3 4 25 6]\n"
     ]
    }
   ],
   "source": [
    "J('[1] [8] [] [[3 4 25 6] 7] 2 z-right')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] [8] [2] [7] [] [4 25 6] 3\n"
     ]
    }
   ],
   "source": [
    "J('[1] [8] [2] [7] [3 4 25 6] z-down')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] [8] [2] [7] [3] [25 6] 4\n"
     ]
    }
   ],
   "source": [
    "J('[1] [8] [2] [7] [] [4 25 6] 3 z-right')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] [8] [2] [7] [4 3] [6] 25\n"
     ]
    }
   ],
   "source": [
    "J('[1] [8] [2] [7] [3] [25 6] 4 z-right')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] [8] [2] [7] [4 3] [6] 625\n"
     ]
    }
   ],
   "source": [
    "J('[1] [8] [2] [7] [4 3] [6] 25 sqr')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                              . [1] [8] [2] [7] [4 3] [6] 625 z-up\n",
      "                          [1] . [8] [2] [7] [4 3] [6] 625 z-up\n",
      "                      [1] [8] . [2] [7] [4 3] [6] 625 z-up\n",
      "                  [1] [8] [2] . [7] [4 3] [6] 625 z-up\n",
      "              [1] [8] [2] [7] . [4 3] [6] 625 z-up\n",
      "        [1] [8] [2] [7] [4 3] . [6] 625 z-up\n",
      "    [1] [8] [2] [7] [4 3] [6] . 625 z-up\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] [6] 625 . swap cons swap shunt\n",
      "[1] [8] [2] [7] [4 3] 625 [6] . cons 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"
     ]
    }
   ],
   "source": [
    "V('[1] [8] [2] [7] [4 3] [6] 625 z-up')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] [8] [2 [3 4 625 6] 7]\n"
     ]
    }
   ],
   "source": [
    "J('[1] [8] [2] [7] [3 4 625 6] z-up')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 [2 [3 4 625 6] 7] 8]\n"
     ]
    }
   ],
   "source": [
    "J('[1] [8] [2 [3 4 625 6] 7] z-up')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `dip` and `infra`\n",
    "In Joy we have the `dip` and `infra` combinators which can \"target\" or \"address\" any particular item in a Joy tree structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                                                . [1 [2 [3 4 25 6] 7] 8] [[[[[[sqr] dipd] infra] dip] infra] dip] infra\n",
      "                                         [1 [2 [3 4 25 6] 7] 8] . [[[[[[sqr] dipd] infra] dip] infra] dip] infra\n",
      "[1 [2 [3 4 25 6] 7] 8] [[[[[[sqr] dipd] infra] dip] infra] dip] . infra\n",
      "                                           8 [2 [3 4 25 6] 7] 1 . [[[[[sqr] dipd] infra] dip] infra] dip [] swaack\n",
      "        8 [2 [3 4 25 6] 7] 1 [[[[[sqr] dipd] infra] dip] infra] . dip [] swaack\n",
      "                                             8 [2 [3 4 25 6] 7] . [[[[sqr] dipd] infra] dip] infra 1 [] swaack\n",
      "                  8 [2 [3 4 25 6] 7] [[[[sqr] dipd] infra] dip] . infra 1 [] swaack\n",
      "                                                 7 [3 4 25 6] 2 . [[[sqr] dipd] infra] dip [8] swaack 1 [] swaack\n",
      "                            7 [3 4 25 6] 2 [[[sqr] dipd] infra] . dip [8] swaack 1 [] swaack\n",
      "                                                   7 [3 4 25 6] . [[sqr] dipd] infra 2 [8] swaack 1 [] swaack\n",
      "                                      7 [3 4 25 6] [[sqr] dipd] . infra 2 [8] swaack 1 [] swaack\n",
      "                                                       6 25 4 3 . [sqr] dipd [7] swaack 2 [8] swaack 1 [] swaack\n",
      "                                                 6 25 4 3 [sqr] . dipd [7] swaack 2 [8] swaack 1 [] swaack\n",
      "                                                           6 25 . sqr 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
      "                                                           6 25 . dup mul 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
      "                                                        6 25 25 . mul 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
      "                                                          6 625 . 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
      "                                                        6 625 4 . 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
      "                                                      6 625 4 3 . [7] swaack 2 [8] swaack 1 [] swaack\n",
      "                                                  6 625 4 3 [7] . swaack 2 [8] swaack 1 [] swaack\n",
      "                                                  7 [3 4 625 6] . 2 [8] swaack 1 [] swaack\n",
      "                                                7 [3 4 625 6] 2 . [8] swaack 1 [] swaack\n",
      "                                            7 [3 4 625 6] 2 [8] . swaack 1 [] swaack\n",
      "                                            8 [2 [3 4 625 6] 7] . 1 [] swaack\n",
      "                                          8 [2 [3 4 625 6] 7] 1 . [] swaack\n",
      "                                       8 [2 [3 4 625 6] 7] 1 [] . swaack\n",
      "                                        [1 [2 [3 4 625 6] 7] 8] . \n"
     ]
    }
   ],
   "source": [
    "V('[1 [2 [3 4 25 6] 7] 8] [[[[[[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",
    "\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",
    "       \n",
    "The `Z` function isn't hard to make."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "define('Z == [[] cons cons] step i')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here it is in action in a simplified scenario."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                             . 1 [2 3 4] Z\n",
      "                           1 . [2 3 4] Z\n",
      "                   1 [2 3 4] . Z\n",
      "                   1 [2 3 4] . [[] cons cons] step i\n",
      "    1 [2 3 4] [[] cons cons] . step i\n",
      "          1 2 [[] cons cons] . i [3 4] [[] cons cons] step i\n",
      "                         1 2 . [] cons cons [3 4] [[] cons cons] step i\n",
      "                      1 2 [] . cons cons [3 4] [[] cons cons] step i\n",
      "                       1 [2] . cons [3 4] [[] cons cons] step i\n",
      "                       [1 2] . [3 4] [[] cons cons] step i\n",
      "                 [1 2] [3 4] . [[] cons cons] step i\n",
      "  [1 2] [3 4] [[] cons cons] . step i\n",
      "      [1 2] 3 [[] cons cons] . i [4] [[] cons cons] step i\n",
      "                     [1 2] 3 . [] cons cons [4] [[] cons cons] step i\n",
      "                  [1 2] 3 [] . cons cons [4] [[] cons cons] step i\n",
      "                   [1 2] [3] . cons [4] [[] cons cons] step i\n",
      "                   [[1 2] 3] . [4] [[] cons cons] step i\n",
      "               [[1 2] 3] [4] . [[] cons cons] step i\n",
      "[[1 2] 3] [4] [[] cons cons] . step i\n",
      "  [[1 2] 3] 4 [[] cons cons] . i i\n",
      "                 [[1 2] 3] 4 . [] cons cons i\n",
      "              [[1 2] 3] 4 [] . cons cons i\n",
      "               [[1 2] 3] [4] . cons i\n",
      "               [[[1 2] 3] 4] . i\n",
      "                             . [[1 2] 3] 4\n",
      "                   [[1 2] 3] . 4\n",
      "                 [[1 2] 3] 4 . \n"
     ]
    }
   ],
   "source": [
    "V('1 [2 3 4] Z')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And here it is doing the main thing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 [2 [3 4 625 6] 7] 8]\n"
     ]
    }
   ],
   "source": [
    "J('[1 [2 [3 4 25 6] 7] 8] [sqr] [dip dip infra dip infra dip infra] Z')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Addressing\n",
    "Because we are only using two combinators we could replace the list with a string made from only two characters.\n",
    "\n",
    "       [...] [Q] 'ddididi' Zstr\n",
    "    -------------------------------------------------------------\n",
    "       [...] [[[[[[[Q] dip] dip] infra] dip] infra] dip] infra\n",
    "\n",
    "The string can be considered a name or address for an item in the subject datastructure.\n",
    "\n",
    "## Determining the right \"path\" for an item in a tree.\n",
    "It's easy to read off (in reverse) the right sequence of \"d\" and \"i\" from the subject datastructure:\n",
    "\n",
    "    [ n [ n [ n n x ...\n",
    "    i d i d i d d Bingo!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "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.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
