# Zipper

The Zipper is an idiom that uses the idea of “context” to the means of manipulating locations in a data structure. Zipper monad is a monad which implements the zipper for binary trees.

## Contents

Sometimes you want to manipulate a location inside a data structure, rather than the data itself. For example, consider a simple binary tree type:

data Tree a = Fork (Tree a) (Tree a) | Leaf a


and a sample tree t:

 t = Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 3) (Leaf 4)) Each subtree of this tree occupies a certain location in the tree taken as a whole. The location consists of the subtree, along with the rest of the tree, which we think of the context of that subtree. For example, the context of

Leaf 2


in the above tree is

 Fork (Fork (Leaf 1) @) (Fork (Leaf 3) (Leaf 4)) where @ marks a hole: the spot that the subtree appears in. This is the way we shall implement a tree with a focus. One way of expressing this context is as a path from the root of the tree to the hole (to which the required subtree will be attached). To reach our subtree, we needed to go down the left branch, and then down the right one. Note that the context is essentially a way of representing the tree, “missing out” a subtree (the subtree we are interested in).

A naive implementation of the context, inspired directly by the graphical representation might be to use Tree (Maybe a) instead of Tree a. However, this would lose an essential point: in any given context, there is exactly one hole.

Therefore, we will represent a context as follows:

data Cxt a = Top | L (Cxt a) (Tree a) | R (Tree a) (Cxt a)


L c t represents the left part of a branch of which the right part was t and whose parent had context c. The R constructor is similar. Top represents the top of a tree.

Remarks:

• In fact, this is equivalent to a list, whose elements are the appropriate collateral trees, each element labeled with the information which direction was chosen:
type Context a = [(Direction, Tree a)]
data Direction = Lft | Rght
• We chose to propagate from the hole towards the root. This is an independent idea of the above considerations: it is not the unicity of the hole that forced us to do so. It is simply more efficient if we want to define operations later for a tree with a focus (move focus left, right, up).
• Note that in the original paper, Huet dealt with B-trees (ones where nodes have arbitrary numbers of branches), so at each node, a list is used instead of the two (Tree a) parameters to represent children. Later we shall see that the solution of the problem can be formalized in a general way which covers solutions for both kinds of trees, as special cases.

Using this datatype, we can rewrite the sample context above in proper Haskell:

R (Leaf 1) (L Top (Fork (Leaf 3) (Leaf 4)))


Note that the context is actually written by giving the path from the subtree to the root (rather than the other way round): $c_0$ $\begin{matrix}\mathrm{R\;(Leaf\;1)}\;\underbrace{\mathrm{(L\;Top\;(Fork\;(Leaf\;3)\;(Leaf\;4)))}}\\\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;c_1\end{matrix}$ $c_1$ $\begin{matrix}\mathrm{L}\;\underbrace{\mathrm{Top}}\;\mathrm{(Fork\;(Leaf\;3)\;(Leaf\;4))}\\\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!c_2\end{matrix}$ $c_2$ Top Or the more deconstructed representation: $\left[\begin{matrix}\mathrm{(Rght,\;Leaf\;1),\;\underbrace{\mathrm{(Lft,\;Fork\;(Leaf\;3)\;(Leaf\;4))}}}\\\underbrace{\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;c_1\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;}\\c_0\end{matrix}\right]$ where $c_0$, $c_1$ are the appropriate correspondents of the $c_0$, $c_1$ of the previous image. It is the empty list that represents $c_2$.

Now we can define a tree location:

type Loc a = (Tree a, Cxt a)


thus, a tree with a focus (drawn here as a tree with a marked subtree) shall be represented as “mounting” the focus (a tree) into the hole of the appropriate context.

Now, we can define some useful functions for manipulating locations in a tree:

left :: Loc a -> Loc a
left (Fork l r, c) = (l, L c r)

right :: Loc a -> Loc a
right (Fork l r, c) = (r, R l c)

top :: Tree a -> Loc a
top t = (t, Top)

up :: Loc a -> Loc a
up (t, L c r) = (Fork t r, c)
up (t, R l c) = (Fork l t, c)

upmost :: Loc a -> Loc a
upmost l@(t, Top) = l
upmost l = upmost (up l)

modify :: Loc a -> (Tree a -> Tree a) -> Loc a
modify (t, c) f = (f t, c)


It is instructive to look at an example of how a location gets transformed as we move from root to leaf. Recall our sample tree t. Let's name some of the relevant subtrees for brevity:

t = let tl = Fork (Leaf 1) (Leaf 2)
tr = Fork (Leaf 3) (Leaf 4)
in Fork tl tr


Then to reach the location of Leaf 2:

(right . left . top) t
= (right . left) (t, Top)
= right (tl, L Top tr)
= (Leaf 2, R (Leaf 1) (L Top tr))


To reach that location and replace Leaf 2 by Leaf 0:

modify ((right . left . top) t) (\_ -> Leaf 0)
= ...
= (Leaf 0, R (Leaf 1) (L Top tr))


Afterwards some may like to continue walking to other parts of the new tree, in which case continue applying left, right, and up.

Some others may like to retrieve the new tree (and possibly forget about locations), in which case upmost is useful:

(fst . upmost) (modify ((right . left . top) t) (\_ -> Leaf 0))
= (fst . upmost) (Leaf 0, R (Leaf 1) (L Top tr))
= fst (Fork (Fork (Leaf 1)
(Leaf 0))
tr
, Top)
= Fork (Fork (Leaf 1)
(Leaf 0))
tr


## Automation

There's a principled way to get the necessary types for contexts and the context filling functions, namely by differentiating the data structure. Some relevant papers.

For an actual implementation in Generic Haskell, see the paper Type-indexed data types by Ralf Hinze, Johan Jeuring and Andres Löh, or a similar paper Generic Haskell: Applications by Ralf Hinze and Johan Jeuring

## Alternative formulation

The dual of Huet zipper is generic zipper -- which is a derivative of a traversal function rather than that of a data structure. Unlike Huet zipper, generic zipper can be implemented once and for all data structures, in the existing Haskell. Generic Zipper and its applications

• A zipper for navigating rose trees (as found in the standard Data.Tree library) is available in the Yi code repository.
• An implementation of a zipper for navigating rose trees (as found in the standard Data.Tree library).
{"ft_lang_label":"__label__en","ft_lang_prob":0.89115965,"math_prob":0.9940181,"size":7210,"snap":"2021-31-2021-39","text_gpt3_token_len":1869,"char_repetition_ratio":0.12850402,"word_repetition_ratio":0.05107328,"special_character_ratio":0.25284326,"punctuation_ratio":0.10979021,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99202305,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-08-04T16:11:20Z\",\"WARC-Record-ID\":\"<urn:uuid:d69a3448-48b3-4718-8a71-4e3e8c9dcfeb>\",\"Content-Length\":\"40665\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a7b8e049-d838-41e0-a17c-539bfa3bebef>\",\"WARC-Concurrent-To\":\"<urn:uuid:be395d08-045e-474a-9377-1535ab16fa3c>\",\"WARC-IP-Address\":\"151.101.249.175\",\"WARC-Target-URI\":\"https://wiki.haskell.org/index.php?title=Zipper&oldid=55184\",\"WARC-Payload-Digest\":\"sha1:WQYICL6S53AICXYDAR2EC3LPCGHTPHEW\",\"WARC-Block-Digest\":\"sha1:LFWAVYEBBB75JKIDJFVSPO2SYZQWWSIZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046154878.27_warc_CC-MAIN-20210804142918-20210804172918-00419.warc.gz\"}"}