- Zipper (data structure)
"Zipper" is a
purely functional data structure used infunctional programming to solve some problems in a way using notions like “context” and “hole”. It is related to the generalization of notion “derivative” (for types). The zipper was described byGerard Huet in 1997.Zippers are multidimensional in the sense that they can be used as lists or trees by placing additional restrictions upon them. Such derived data structures are usually referred to by saying "a tree with zipper" or"a list with zipper" to give the image that the structure is a tree or a list, with a zip slider attach to it as an after thought.
Minimal Example
While zipper is a functional structure and is often used from within a functional language equipped with extensive type checking,there is no reason why it would not be suitable for use with many procedural languages as well. Many procedural languages evensupport functional programming to some degree. A minimal "list with zipper" example is described below in Python. The example is only used to present the core idea and is not provided as a guide for actually implementing a zipper.
cursor = [ [0,1,2] , 3, [4,5,6,7,8,9] ]
The structure is named "cursor" because its value is a list with focus set on value 3. Technically it is (at top level) a list with exactly three values( [0,1,2] , 3 and [4,5,6,7,8,9] ). This list starts with a list of elements ( [0,1,2] ) that precede the element with current focus, then as the second element it contains the element with focus (3) and finally it contains another list ( [4,5,6,7,8,9] ) with the elements that follow the element with current focus.
An analogy, using only sets
The ideas of zero, addition, multiplication and exponentiation known from the area of arithmetic have analogies in set theory, category theory and type theory. For example, there follows a small list of set theoretical ones:
* Theempty set (analogous to zero):
* Thecartesian product of the sets and (analogous to multiplication):
* the set of -tuples of elements of the set (analogous to "n"-power):
* the set of functions from to (analogous to exponentiation): (sometimes written as or )Also:can be defined for sets as a fruitful concept (analogous to addition). It is something similar to the disjoint union of sets, but it uses labels to achieve a partition-like construct [More precisely, it is a two-arguments case of the more general construct: where ] .There are analogous constructs for types, too (see also typeful functional programming languages), used forcase analysis .Parametric constructs
Several programming languages have parametric type constructs. They are used in a natural way in
functional programming and have firm theoretical foundations, seetype theory .If we continue using the set theoretical analogies: we have a notion of constructing from a set another set. In many specific cases, these can be grasped as polynomial constructs of sets, where multiplication, addition, power correspond to the appropriate set-theoretic operations.:“makes” an appropriate set of triples from the set given in the argument::
Derivative
Thus, we can write “polynomials” for sets. We know polynomials from many numerical branches of mathematics. A notion of "derivative" has sense in them::becomes:
Let us define the derivative here as we define it for polynomials over a ring!In our triple-building example,:becomes:and let us examine whether this step — that we have done in a mechanical way — has any sense for sets.
Maybe astonishingly, this mechanical application of a concept borrowed from a distant field of mathematics will yield a fruitful result: the resulting construct is a polynomial that builds from any set the corresponding set of triples “with a hole”. An informal explanation: if we store a pair, and at the same time make a distinction among three cases, then we have exactly the right amount of information to distinguish among cases
* first thing, second thing, hole
* first thing, hole, third thing
* hole, second thing, third thingthus we can interpret it as storing a triple with a hole.In summary, such a notion is meaningful and even fruitful, and several analogous constructs are used in functional programming.
Supported by this achievement, we can examine whether mechanical use of some
differentiation rules will produce a meaningful notion also for sets. We can build an analogouscalculus with polynomials , for sets.There are also more interesting constructs, than such polynomial ones. This notion of “derivative” can be extended also to them.This concept has practical applications (in functional programming).
Uses
The zipper is often used where there is some concept of 'focus' or of moving around in some set of data, since its semantics reflect that of moving around but in a functional non-destructive manner.
The zipper has been used in
*Xmonad , to manage focus and placement of windows
* Huet's papers cover astructural editor [ [http://www.informatik.uni-bonn.de/~ralf/publications/TheWeb.ps.gz "Functional Pearl: Weaving a web"] .] based on zippers and atheorem prover
* Afilesystem (ZipperFS) written in Haskell offering "...transactional semantics; undo of any file and directory operation; snapshots; statically guaranteed the strongest, repeatable read, isolation mode for clients; pervasive copy-on-write for files and directories; built-in traversal facility; and just the right behavior for cyclic directory references." [http://okmij.org/ftp/Computation/Continuations.html#zipper]References
Further reading
* Huet, Gerard. " [http://www.st.cs.uni-sb.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf Functional Pearl: The Zipper"] "Journal of Functional Programming" 7 (5): 549-554, September 1997.
* Hinze, Ralf, et al. [http://www.cs.uu.nl/~johanj/publications/tidata.pdf "Type-indexed data types"] . 23 July 2003External links
* [http://www.haskell.org/haskellwiki/Zipper Zipper]
* [http://sigfpe.blogspot.com/2006/09/infinitesimal-types.html Infinitesimal Types]
* [http://cgi.cse.unsw.edu.au/~dons/blog/2007/05/17 "Roll Your Own Window Manager: Tracking Focus with a Zipper"]
* [http://www.nist.gov/dads/HTML/zipper.html Definition]
* [http://www.eecs.harvard.edu/~nr/pubs/zipcfg-abstract.html "An Applicative Control-Flow Graph Based on Huet's Zipper"]
Wikimedia Foundation. 2010.