Learn · Hidden Machines · Part VI
budding
A Cursor Carries Its Path
Zippers and structural navigation.
You will derive a zipper from repeated root searches, move focus locally, and rebuild an immutable tree without mutable backlinks.
The bookmark remembers the route
An editor stores only a node identifier. Moving to a sibling searches from the root; editing either mutates backlinks or rebuilds ancestry by another scan. Instead split the tree into focus and context. Each context frame records the ancestor constructor and untouched siblings needed to move back up.
Moving down pushes a context frame. Moving up fills its hole with the current focus. Editing replaces only the focus; returning to the root reconstructs the path and shares every untouched subtree. The zipper is the structural derivative intuition made concrete: one value removed from a shape leaves the context required to put a value back.
Where the model stops
A zipper is excellent for one local focus. Many simultaneous cursors, arbitrary cross-links, or identity-preserving mutable graphs may need another structure. Do not expose the context representation when navigation operations form the deeper interface.
Lessons
- Focus without context cannot move locally in an immutable tree.
- Context frames record a surrounding node with one hole.
- Edits rebuild only the path and share untouched structure.
- One-focus locality is the refusal boundary.
Practice
- Draw the context after moving left then right in a binary tree.
- Distinguish a zipper from an integer index into an array.
- Transfer the model to breadcrumbs in a syntax editor.
Navigation gives locations meaning through context. The next chapter applies the same idea to names and conversations.
References
- “Gérard Huet, “The Zipper”.” — the original functional pearl.