Zipper (data structure)A zipper is a technique of representing an aggregate data structure so that it is convenient for writing programs that traverse the structure arbitrarily and update its contents, especially in purely functional programming languages. The zipper was described by Gérard Huet in 1997.[1] It includes and generalizes the gap buffer technique sometimes used with arrays. The zipper technique is general in the sense that it can be adapted to lists, trees, and other recursively defined data structures. Such modified data structures are usually referred to as "a tree with zipper" or "a list with zipper" to emphasize that the structure is conceptually a tree or list, while the zipper is a detail of the implementation. A layperson's explanation for a tree with zipper would be an ordinary computer filesystem with operations to go to parent (often Example: Bidirectional list traversalMany common data structures in computer science can be expressed as the structure generated by a few primitive constructor operations or observer operations. These include the structure of finite lists, which can be generated by two operations:
A list such as To be clear, a location in the list is not just the number of A list-zipper always represents the entire data structure. However, this information is from the perspective of a specific location within that data structure. Consequently, a list-zipper is a pair consisting of both the location as a context or starting point, and a recording or path that permits reconstruction from that starting location. In particular, the list-zipper of With the list represented this way, it is easy to define relatively efficient operations on immutable data structures such as Lists and Trees at arbitrary locations. In particular, applying the zipper transform to a tree makes it easy to insert or remove values at any particular location in the tree. Contexts and differentiationThe type of a zipper's contexts can be constructed via an operation on the original type that is closely related to the derivative of calculus through decategorification. The recursive types that zippers are formed from can be viewed as the least fixed point of a unary type constructor of kind . For example, with a higher-order type constructor that constructs the least fixed point of its argument, an unlabeled binary tree can be represented as and an unlabeled list may take the form . Here, the notation of exponentiation, multiplication, and addition correspond to function types, product types, and sum types respectively, whilst the natural numbers label the finite types; in this way, the type constructors resemble polynomial functions.[2] The derivative of a type constructor can therefore be formed through this syntactic analogy, and the zipper of the type constructor is the derivative paired with its element type. In this way, the derivative can be viewed as a zipper with a hole in it, where a value could be placed. Consider the type of singly-linked lists. The list data structure can be defined as . That is, is a type constructor which takes an element type and produces the type of lists of that element type. The two addends correspond to the two data constructors for a list. The Under this representation, the derivative of in terms of is . That is, if we have a zipper-with-a-hole for a list, then either (I) the hole is the very first element, and the rest of the list is an ordinary (non-zipper) list , or (II) the first element is present () and the hole is somewhere else further down the tail of the list (). Then the formal type of a zipper for a linked list is , or . Put another way, a zipper for a linked list consists of an element in that list and instructions on where to put it in a partially-built list. For another example, consider the recursive data structure of a binary tree with nodes that are either sentinel nodes of type or which are leaves containing a value of some type . We can represent this type algebraically as . The derivative in terms of is . We can read this algebraic notation as a zipper with a hole: A zipper for a tree either has the "missing value" at the very root of the tree, leaving the two branches as ordinary trees (the case), or it has the missing value on one of the two branches (). In the latter case, the Boolean type indicates which branch (left or right) contains the hole, and the zipper contains a value for the root, a for the complete branch, and a for the branch that's missing a value of type . The complete type of a zipper for this binary tree structure, then, is , or . In general, a zipper consists of two parts: a collection of contexts for the current node and each of its ancestors up until the root node, and the value that the current node contains. UsesThe zipper is often used where there is some concept of focus or a cursor used to navigate 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
Alternatives and extensionsDirect modificationIn a non-purely-functional programming language, it may be more convenient to simply traverse the original data structure and modify it in place (perhaps after deep cloning it, to avoid affecting other code that might hold a reference to it). Generic zipperThe generic zipper[6][7][8] is a technique to achieve the same goal as the conventional zipper by capturing the state of the traversal in a continuation while visiting each node. (The Haskell code given in the reference uses generic programming to generate a traversal function for any data structure, but this is optional – any suitable traversal function can be used.) However, the generic zipper involves inversion of control, so some uses of it require a state machine (or equivalent) to keep track of what to do next. References
Further reading
External links |
Portal di Ensiklopedia Dunia