Although Idris is a pure language with immutable values, the chapter explains that stateful programming is still very natural when it is made explicit in types. The core idea is that a type can describe not just a value, but a sequence of operations that read and write state, much like earlier chapters used types to describe interactive programs. The chapter first motivates this with a tree-labeling example, where a depth-first traversal must thread a stream of labels through recursive calls, showing why state is useful for both local algorithms and whole-program behavior.
The first major technique presented is representing mutable state by hand as a pair: a computation returns both its result and the updated state for the next step. This works for tree traversal, but it is awkward and error-prone because the programmer must manually pass the right updated state through recursive calls. The chapter then introduces the library’s State type, which packages this pattern into an abstraction with operations like get, put, and runState. With State, the tree-labeling code becomes clearer and easier to reason about, and the same pattern is shown to support related helpers such as evalState and execState.
The later sections broaden the idea by showing how to implement State from scratch and how to equip it with Functor, Applicative, and Monad, enabling familiar do-notation and generic library functions. The chapter then scales up from local state to a complete interactive application by revisiting the arithmetic quiz: the game’s score and difficulty are stored in nested records, updated with concise record syntax, and accessed through projection functions. By extending the command language to include game-state operations and by writing runners that thread both random numbers and game state, the chapter demonstrates a general pattern for building precise, type-safe programs that combine interaction and mutable state while remaining purely functional at the implementation level.
Figure 12.1. Labeling a tree, depth first. Each node is labeled with an integer.
Figure 12.2. Labeling subtrees. The left subtree is labeled from 1 to 3, and the right subtree is labeled from 5 to 6.
Figure 12.3. Syntax for returning a new record with a field updated
Summary
Many algorithms read and write state. For example, when labeling the nodes of a tree in depth-first order, you can keep track of the state of the labels while traversing the tree.
You can manage state by using the generic State type to describe operations on the state along with a runState function to execute those operations.
You can define your own State type as a sequence of Get and Put operations.
Defining Functor, Applicative, and Monad for the State type gives you access to several generic functions from the Idris library.
When writing interactive programs with state, you can define a Command type to describe an interface consisting of console I/O and state management operations.
Record types can represent more complex nested state.
Idris provides a concise syntax for assigning new values to fields in nested records.
Idris also provides a syntax (using $=) for updating a field in a nested record by applying a function to the value in that field.
FAQ
What is the main idea of writing programs with state in Idris?Idris is a pure language, so state is not updated in place. Instead, programs describe stateful computations in their types, then run them separately. This keeps programs pure while still allowing mutable-style algorithms.Why does the chapter use a tree-labeling example to explain state?The tree example shows a case where local state is useful: labeling a tree depth-first from left to right requires remembering which label comes next as traversal proceeds. It demonstrates why state must sometimes be threaded through recursive computations.Why is manually threading state through a recursive function considered error-prone?Because you must explicitly pass the updated state through each recursive call, and it is easy to use the wrong intermediate value. The algorithm also becomes harder to read because state management is mixed with the core logic.What does the State type represent?State stateType ty represents a sequence of operations that can read and write a state of type stateType, producing a result of type ty. It is a way to describe stateful computation as a value.How do get and put work in the State API?get reads the current state and returns it as the result. put replaces the current state with a new one and returns unit ().What is the role of runState?runState executes a stateful computation given an initial state. It returns a pair containing the computation’s result and the final state after all operations have been performed.How does the chapter reimplement tree labeling using State?The label stream is stored as the state. The function labels the left subtree, reads the next label from the state, updates the state to the remaining stream, and then labels the right subtree. This hides the manual passing of state.Why are Functor, Applicative, and Monad important for State?Implementing these interfaces lets State use convenient syntax like do notation and enables generic library functions such as when and traverse. It makes stateful code more readable and reusable.Why does the chapter introduce records for the quiz program’s state?Because the quiz has multiple pieces of state, such as score and difficulty. Records provide a clean way to organize that data, access fields with projection functions, and update nested fields concisely.How does the quiz program combine console I/O with state?The chapter extends the command type so it can read and write both console input/output and game state. Then the runner threads random numbers and game state through the program while executing the commands, allowing the quiz to remain declarative and total.
pro $24.99 per month
access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!