Zipper (data structure)

Zipper (data structure)

"Zipper" is a purely functional data structure used in functional 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 by Gerard 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:
* The empty set (analogous to zero):empty
* The cartesian product of the sets A and B (analogous to multiplication):A imes B
* the set of n-tuples of elements of the set A (analogous to "n"-power):A^n;forall ninmathbb N
* the set of functions from A to B (analogous to exponentiation):B^A, (sometimes written as A o B or mathrm{Hom}(A,B),)Also:A + B,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:sum_Lambda vec A = left{leftlanglelambda, a ight angle in Lambda imes igcupmathcal A mid lambdainLambda land a in vec A_lambda ight} where vec A : Lambda o mathcal A] .There are analogous constructs for types, too (see also typeful functional programming languages), used for case 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, see type 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.:F(X) = X^3“makes” an appropriate set of triples from the set given in the argument::Fleft(leftlbrace,0, 1, dots, 9, ight brace ight) = leftlbrace,leftlangle0,0,0 ight angle, leftlangle0,0,1 ight angle, dots, leftlangle9,9,9 ight angle , ight brace

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::f(x)=a_n x^n + a_{n-1} x^{n-1} + cdots + a_1 x + a_0becomes:f'(x)=n a_n x^{n-1} + (n-1) a_{n-1} x^{n-2} + cdots + a_1

Let us define the derivative here as we define it for polynomials over a ring!In our triple-building example,:F(X) = X^3becomes:F'(X) = X^2 + X^2 + X^2and 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 analogous calculus 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 a structural editor [ [http://www.informatik.uni-bonn.de/~ralf/publications/TheWeb.ps.gz "Functional Pearl: Weaving a web"] .] based on zippers and a theorem prover
* A filesystem (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 2003

External 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.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Zipper (disambiguation) — A zipper is a device for temporarily joining two edges of fabric together.Zipper(s) may also refer to: *Zipper (BDSM), a sexual practice which involves zipping the skin *Zipper (data structure), also known as Huet s zipper, a data structure that… …   Wikipedia

  • List of terms relating to algorithms and data structures — The [http://www.nist.gov/dads/ NIST Dictionary of Algorithms and Data Structures] is a reference work maintained by the U.S. National Institute of Standards and Technology. It defines a large number of terms relating to algorithms and data… …   Wikipedia

  • Protein quaternary structure — In biochemistry, quaternary structure is the arrangement of multiple folded protein or coiling protein molecules in a multi subunit complex. Contents 1 Description and examples 2 Nomenclature of quaternary structures 3 Determination of qua …   Wikipedia

  • Xmonad — Infobox Software name = xmonad caption = xmonad in tiling mode author = Spencer Janssen, Don Stewart, Jason Creighton released = latest release version = 0.8 [ [http://www.haskell.org/pipermail/xmonad/2008 September/006254.html ANNOUNCE: xmonad 0 …   Wikipedia

  • Derivative (generalizations) — Derivative is a fundamental construction of differential calculus and admits many possible generalizations within the fields of mathematical analysis, combinatorics, algebra, and geometry. Derivatives in analysis In real, complex, and functional… …   Wikipedia

  • Список терминов, относящихся к алгоритмам и структурам данных —   Это служебный список статей, созданный для координации работ по развитию темы.   Данное предупреждение не устанавливается на информационные списки и глоссарии …   Википедия

  • Список терминов — Список терминов, относящихся к алгоритмам и структурам данных   Это сл …   Википедия

  • Coders at work — Coders at Work: Reflections on the Craft of Programming   Author(s) Peter Seibel …   Wikipedia

  • Gérard Huet — Gérard Huet, born in Bourges on July 7 1947, is a French computer scientist.Graduated from: * Université Denis Diderot (Paris VII) * Case Western Reserve University * Université de Paris * His specialties are: * Software architecture * Design of… …   Wikipedia

  • Protein domain — Pyruvate kinase, a protein from three domains (PDB 1pkn) A protein domain is a part of protein sequence and structure that can evolve, function, and exist independently of the rest of the protein chain. Each domain forms a compact three… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”