You're viewing the readable version of this site. The interactive extras (search, diagrams, read-aloud) need JavaScript and a current browser. Enable JavaScript; if it is already enabled, update your browser.

Learn · Hidden Machines · Part VI

budding

A Cursor Carries Its Path

Zippers and structural navigation.

zipper, persistent-data, trees, editors, learn

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.

Reconstruction — identify what an immutable cursor must retain.

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

  1. Draw the context after moving left then right in a binary tree.
  2. Distinguish a zipper from an integer index into an array.
  3. 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

  1. Gérard Huet, “The Zipper”.” — the original functional pearl.