Chapter 14. Dependent state machines: handling feedback and errors
The chapter shows how dependent types can describe not just valid state transitions, but also the ways those transitions may fail or depend on outside responses. Building on earlier door and vending-machine examples, it refines the idea of a command type so that an operation’s output state can be computed from its return value, letting the type system model uncertainty such as a jammed door, a rejected coin, or a failed network response. This makes protocols more realistic and more precise, because a program can only continue when it has handled the result that determines the next allowed state.
The first major example revisits the door protocol and introduces a command type where opening the door returns a result that determines whether the door ends up open or remains closed. This forces code to inspect the result of a potentially failing operation before performing later actions that depend on it. The chapter uses interactive development with holes to debug type errors and gradually refine a door program until the types line up, showing how type checking guides the programmer toward explicit handling of success and failure. It also demonstrates pattern-matching bindings as a concise way to branch on outcomes while keeping the protocol safe.
The second half applies the same idea to an ATM and then to a guessing game. For the ATM, the types enforce the security property that cash can be dispensed only after a valid card and PIN, while an interpreter maps the typed description to console I/O. The model is then tightened further with an auto-implicit predicate so that card ejection is only permitted in states where a card is actually present. Finally, the chapter generalizes the approach to Hangman by separating an abstract game protocol from its concrete execution, using dependent commands, a game loop, and a fuel-based interpreter to ensure that only legal moves are possible and that the implementation faithfully follows the rules, even when user input may be invalid or the game may terminate.
Figure 14.1. A state transition diagram showing the states and operations on a door
Figure 14.2. A state transition diagram showing the states and operations on a door, where opening the door might fail
Figure 14.3. New type for DoorCmd, where the output state of an operation is computed from the return value of the operation
Figure 14.4. A state machine describing the states and operations on an ATM. This omits operations (such as GetAmount) that are valid in all states.
Figure 14.5. State transition diagram for Hangman. The Running state also holds the number of letters and guesses remaining. Won requires the number of letters to be zero, and Lost requires the number of guesses to be zero.
Summary
You can get feedback from the environment by using the result of an operation to compute the output state of a command.
A system might be in a different state after running a command, depending on whether the command was successful.
Defining preconditions on operations allows you to express security properties in types, such as when it’s valid for an ATM to dispense cash.
A system might change state according to whether a user’s input is valid in the current environment, such as whether a PIN or password is correct.
Predicates and auto implicits help you describe valid input states of operations precisely.
You can describe the rules of a game precisely in a type, so that a function that type-checks must be a valid implementation of the rules.
You use an abstract state type to describe what operations do, and a concrete state, depending on the abstract state, to describe their corresponding implementations.
FAQ
```html
What is the main idea of dependent state machines in this chapter?Dependent state machines use types to describe valid states and transitions, so a program can only type-check if it follows the allowed protocol. This chapter extends that idea to handle failures, user input, and environment-dependent transitions by making the next state depend on an operation’s result.Why does the door example need to be refined from the previous chapter?The original door model assumed operations always succeeded, but in reality opening a door may fail if it is jammed. The refined model represents this possibility in the type, so the output state depends on whether Open returns OK or Jammed.How is a failed state transition represented in the refined DoorCmd type?The return type of an operation is given a name, and the output state is computed by a function of that return value. For example, Open returns DoorResult, and its postcondition is a function that maps OK to DoorOpen and Jammed to DoorClosed.Why is const useful in these state-machine types?const lets you express that an operation’s output state does not depend on its result. It ignores its second argument and always returns the same state, which is useful for operations like Close, RingBell, or Display that do not change the state in a result-dependent way.Why does doorProg need to check the result of Open?Because after Open, the next state is determined by the returned DoorResult. You cannot safely call Close unless the result shows the door is actually open, so the type forces you to inspect the result before continuing.How do holes help when debugging a type error in a dependent state machine?Replacing a problematic expression with a hole lets Idris show the expected type and local variables in scope. This makes it easier to see how the output state depends on earlier results and guides you toward the correct case split or pattern-match.What security property does the ATM example guarantee?The ATM model guarantees that cash can only be dispensed when a valid card is present and the PIN has been successfully checked. Since the state machine’s types enforce the required transitions, any well-typed program must respect that rule.Why is EjectCard refined using a predicate like HasCard?The original type allowed ejecting a card from any state, even when no card was inserted. The HasCard predicate restricts the operation to states where a card is actually present, such as CardInserted or Session.How does auto simplify the use of EjectCard?By making the proof argument implicit, Idris can search for a suitable witness automatically. This means you can write EjectCard without manually providing HasCard evidence, as long as the current state makes such evidence available.What is the purpose of the Hangman example at the end of the chapter?It shows that the same dependent state-machine approach can describe game rules precisely, not just devices or protocols. The game’s states, transitions, and allowed endings are encoded in types so that a correct implementation must follow the rules of Hangman.
```
pro $24.99 per month
access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!