TL;DR
An immutable value cannot be changed after it is created. To "change" it, you build a new value that differs from the old one, and the old value stays exactly as it was. Because nothing mutates in place, code that holds a reference to a value can trust that the value will not shift under it.
The obvious objection is cost: copying everything on every change would be slow. Persistent data structures avoid most of that cost by sharing the unchanged parts between the old and new values. This style is central to Functional Programming.
Where you'll see it: in the state management of user interfaces, where each screen is a snapshot, in functional languages such as Haskell and Clojure, and in version control systems that store each commit as an unchangeable snapshot.
Intuition
A photograph, not a whiteboard
Think of the difference between a whiteboard and a stack of photographs. If you erase a number on the whiteboard, the old number is gone, and anyone who copied it down earlier now has a wrong copy. If instead you take a new photograph after each edit, every photograph stays correct forever. You can compare the old and new photos, go back to any of them, and share them with people without worrying that they will change.
An immutable value is a photograph. A mutable value is the whiteboard.
Why the idea exists
Mutation makes programs hard to reason about. Suppose a function receives a list and returns a result. If the function also quietly changes the list, the caller may get a surprise later. When several parts of a program share one mutable object, a change made in one place can break another place that never knew about it. With several threads (independent streams of execution in one program) running at once, the problem becomes a race, covered in race conditions.
Immutability removes this whole class of bug. If a value can never change, sharing it is always safe. Any number of readers can hold it at once, and nothing needs a lock to protect it.
A small picture
Here is the basic flow. An update never touches the old value.
flowchart LR A["v1 = [1, 2, 3]"] -->|"put 0 in front"| B["v2 = [0, 1, 2, 3]"] B -.->|"new cell 0 points at the first cell of v1"| A
The new list does not copy . It is one new cell holding that points at the existing cells of . Both lists are valid, and neither can be damaged by the other.
The math
Immutability is a property of how values are used, not a formula, but it can be stated precisely. A value is immutable if no operation changes after it has been created. An operation that "updates" must return a new value , and itself must still satisfy the same description it had before.
Sharing
Let be the number of nodes in a structure and the number of nodes that differ between and . A naive copy costs new nodes for each update. A persistent structure aims to cost roughly new nodes, reusing the other nodes by reference (a reference is a pointer to an existing object rather than a copy of it). The ratio of new work to total size is the key number to watch.
Path copying
Most persistent trees use path copying. (A persistent Array is usually stored as such a tree too, which is why it can avoid copying everything on each update.) To update one node, copy only the nodes on the path from the root down to that node. Every other node is shared, because it is not on the path.
If a balanced binary tree has depth , meaning nodes lie on any root-to-leaf path, then one leaf update creates new nodes. The total number of nodes is for a complete tree, so the update creates a tiny fraction of the structure.
Worked examples
Start with the value .
- Mutable approach. Insert at the front of the list in place. The list now reads , and any other variable that pointed to it also reads .
- Immutable approach. Compute , where means "put this item in front, returning a new list". Now and still.
- Count. A naive copy of with added builds 4 new cells. A persistent linked list builds only 1 new cell (holding ) that points at the three existing cells, so 3 of the 4 cells in are shared.
- Caveat. Sharing works because the new item goes at the front. Adding at the back of a linked list would force a copy of every cell, since each cell points to the next one. That is why persistent structures are designed around which updates they make cheap.
- Interpretation. The old value is still usable, so you can keep it for undo or for comparison, and it costs almost nothing extra to keep.
Take a complete binary tree of depth . It has nodes: a root , two children and , and four leaves , , , . Now replace the value at leaf .
- The path from the root to is . These three nodes must change.
- Create new copies , and . Point at and at the old . Point at and at the old .
- Shared nodes. The old , , and are unchanged, so the new tree points to the same objects. That is four shared nodes.
- Count. The update creates new nodes and reuses .
- Interpretation. Both the old and new trees are complete, valid trees. The old one still returns the old value at .
Scale this up. A balanced tree with leaves has depth , so an update copies nodes. The tree has nodes, so the update creates about of the structure. The rest is shared.
A program has a shared counter that starts at . Two threads each want to add .
With mutation and no lock:
- Thread A reads the counter and gets .
- Thread B reads the counter and gets .
- Thread A writes .
- Thread B writes , overwriting A's write.
- Result. The counter is , but two increments happened, so the correct answer is . One update is lost.
With immutable values and an atomic swap (an operation that the hardware performs as one indivisible step), each thread computes a new value from the value it read and swaps it in only if the counter still holds the value it read. This pattern is called compare-and-swap. If the swap fails, that thread reads again and retries. The lost update cannot happen, because the swap fails for the thread that read a stale value, and that thread recomputes from the new one. Nothing is ever overwritten blindly, and an old value is never changed under a reader. The cost is retries when threads collide, which is usually cheaper than a lock held over a long computation.
This is the reason many concurrent systems favour immutable snapshots. Readers take a snapshot and work on it without coordination.
Tradeoffs and failure modes
Immutability makes code simpler to reason about, but it has real costs. It helps most when many parts of a program read the same data, and it hurts when a program does many small in-place updates to very large arrays.
| Situation | Problem | Better choice |
|---|---|---|
| Shared data read by many parts or threads | Mutation causes surprises and races | Immutable values, with snapshots for readers |
| Large array updated one element at a time | Copying the whole array on each update is slow | Mutable array inside a local scope, or a persistent vector with path copying |
| Memory-constrained program | Old versions stay alive while referenced | Release old snapshots, or rely on Garbage Collection, which reclaims values nothing refers to any more |
| Frequent updates to a single small record | Allocating new objects adds overhead | Plain mutation may be simpler and fast enough |
| Hidden mutation inside a "constant" object | The guarantee is false | Make the whole object graph immutable, or document clearly what is shared |
A common mistake is to call an object immutable while it holds a mutable list inside. The outer value cannot change, but the inner list can, so the guarantee is broken. Deep immutability needs every part of the structure to be immutable.
History
Lisp, designed by John McCarthy around 1958, built programs around lists made of linked cells, and its style of building new lists out of old ones, sharing the unchanged tails, anticipated persistent data structures. (Lisp itself still allowed in-place edits.) In the 1980s, Driscoll, Sarnak, Sleator and Tarjan studied how to make linked data structures persistent in general. Chris Okasaki's 1998 book Purely Functional Data Structures showed how to design efficient immutable structures, and it made persistent data structures a practical topic rather than a curiosity. Later languages such as Clojure, released in the late 2000s, built large parts of their standard collections on persistent structures. The idea caught on because multi-core hardware made shared mutable state harder to manage, and immutable values offered a simpler way to share data safely.
Recap
An immutable value never changes, so every update returns a new value and the old one remains valid. Persistent structures make this affordable by sharing unchanged parts through path copying, so an update to a large tree creates only a few new nodes (and a front insertion into a linked list creates one). Immutability removes a large class of bugs caused by shared mutable state, especially in concurrent code covered by race conditions. The style is central to functional programming. The tree that makes path copying concrete is the binary tree, and a linked list is the simplest persistent structure.
Check yourself
QuestionRecall: what is the difference between an immutable value and a mutable one?Show answer
An immutable value cannot be changed after it is created. An update must produce a new value, and the old one stays the same. A mutable value can be changed in place, so the same object can read differently at different times and through different references.
QuestionComputation: a complete binary tree has depth 3, so a root-to-leaf path has 4 nodes. How many new nodes does one leaf update create, and how many nodes does the tree have in total?Show answer
One leaf update creates one new copy for each node on the path, so 4 new nodes. A complete binary tree with depth 3 has nodes in total. The update therefore reuses nodes.
QuestionWhy: why does an immutable update keep the old value useful, and what does that buy a program?Show answer
The old value is never edited, so it still describes the state before the update. Programs can keep it for undo, compare it with the new value, or hand it to another thread without copying, because no one can change it. That is why readers need no locks.
QuestionApplied: a team stores a configuration object as immutable, but one of its fields is a mutable list that other code appends to at runtime. The team says the config is immutable. What is wrong, and how would you fix it?Show answer
The outer object cannot change, but the list inside it can, so the configuration can still change in ways that surprise readers. Deep immutability needs the list to be immutable too. The fix is to store an immutable list, so each addition returns a new list and the config object is rebuilt with the new list, or to make the field read-only and copy it on read.