liveBook · Manning

Statefulness

As explained in Functional Design and Architecture: Examples in Haskell

A stateful application is an application whose behavior depends on information retained across operations or interactions.

Statefulness means that an application's behavior depends on information retained across operations or interactions. Functional programming does not eliminate state; it organizes state and side effects so they do not spread unpredictably through the code. section 7.0 section 7.3

A stateful design separates business logic from state-handling mechanisms. Pure logic can use immutable state passed as arguments, while an interpreter or application layer can manage mutable operational state and connect the system's layers. section 7.1.1 section 7.3

Common ways to represent and control state include:

  • Argument-passing state represents each transition with an immutable state value. section 7.3
  • State monads hide argument passing behind a composable monadic context, allowing state to be updated during a computation. section 7.3
  • Mutable mechanisms such as IORefs, MVars, vectors, and STM can improve convenience or performance when their access is carefully controlled. section 7.3
  • Concurrent state benefits from suitable granularity: independently mutable values avoid repeatedly replacing a whole shared map. section 8.2.2

Layered sandwich application

A recipe describes business behavior through an eDSL, an interpreter consumes ingredients and updates operational state, and the application layer assembles the ingredients, interpreter, and output. The recipe remains separate from the mutable state mechanism. section 7.1.1

Figure 8.8 Data structure with two layers of concurrent mutability

Inspect the two-layer concurrent structure in which an outer map locates independently mutable values, illustrating fine-grained state updates. section 8.2.2

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains immutable state, state monads, mutable mechanisms, architectural boundaries, and concurrent granularity, but defers broader persistence details.
Examples
Uses a sandwich interpreter, ingredient inventory, IORef, key-value simulation, nested MVars, and coarse- versus fine-grained maps.
What you can do after
Build stateful functional applications by separating pure logic from interpreters and controlling mutable state at explicit architectural boundaries.
How it connects
Connects statefulness to functional programming, immutability, side effects, state monads, monad transformers, interpreters, and concurrency.

Also explained in

Compare how 14 books explain statefulness →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.