Chapter 13: Chapter 13. State machines: verifying protocols in types
This introduction explains how separate operation descriptions and state-machine models can help express and enforce protocols in types.
After this chapter you will be able to
- State dependent operations. Describe the database-client state machine, including which operations are valid in each state and how operations can change state.
- Protocols expressed in types. Explain why expressing a state-machine protocol in a type can help produce accurate sequences of actions, including in communicating systems.
- Door protocol transitions. Describe the door’s states and which operations are allowed in each state.
- State transitions in types. Explain how recording state transitions in command types constrains operation sequences and helps interactive development.
- Door Protocol State Tracking. Explain how tracking the door’s state in command types limits operation sequences to those allowed by the door protocol.
- Command Type Arguments. Describe the three arguments of a state-tracking door command type and interpret the start and end states of a sequence.
- Door sequence state constraints. Explain how doorProg’s input and output states constrain the sequence, and why an extra Open or a missing Close produces a type error.
- Incremental hole type inspection. Trace how inspecting the hole while building doorProg reveals the state required for the remaining actions.
- Vending machine state representation. Explain how the vending machine’s state is represented and why its possible states are not finite.
- Command state transition types. Explain what preconditions and postconditions describe for vending machine operations and how MachineCmd types express them.
- State indexed operation sequences. Explain how DoorCmd and MachineCmd describe valid sequences using abstract states before and after operations.
- Vending precondition checks. Describe why an unchecked Vend fails to type-check and how pattern matching can establish its preconditions.
- Stack access and operations. Describe how a last-in, first-out stack is accessed and what Push, Pop, and Top do, including the nonempty requirement for Pop and Top.
- Typed stack size and representation. Explain how operation preconditions and postconditions describe stack sizes and why a Vect is a suitable concrete representation.
- Stack height in types. Explain how stack commands represent changes to dependently typed state using input and output heights.
- Safe stack popping. Explain how the stack command types prevent a Pop on an empty stack, using the testAdd example.
- Executing Stack Operations. Describe how runStack executes stack operations using a Vect, and state what happens when testAdd runs on an empty stack.
- Stack Height Type Guarantees. Explain how doAdd’s input and output stack heights specify its behavior and prevent applying it to a stack that is too short.
- Calculator stack operations. Describe how entering a number or adding changes the calculator’s stack, and how tryAdd checks that addition is possible.
- Interactive stack command extensions. Explain how StackCmd and runStack are extended to support console interaction.
- Total interactive loop guarantees. Explain the separation between StackIO and StackCmd and the guarantees stated for stackCalc when it is total.
What it builds on
- Defining State and runState (chapter 12) · The definitions of State and runState
- State, a type for describing stateful operations (chapter 12) · A type can describe stateful operations.
- Sequencing Commands with do notation (chapter 11) · Sequencing commands with do notation.
- Incomplete definitions: working with holes (chapter 1) · Working with holes in incomplete definitions.
- Using implicit arguments in functions (chapter 3) · Implicit arguments can be used in functions.
- Defining vectors (chapter 4) · Vect carries its length in its type.
- Refining InfIO: introducing termination (chapter 11) · Separating interactive processes from terminating commands.
Section by section
Introduction
This introduction explains how separate operation descriptions and state-machine models can help express and enforce protocols in types.
- Descriptions and execution: Explain the separation between describing a sequence of operations and interpreting it in a particular context, using State and runState as an example.
Why it matters: Separating a description from its execution lets you make the description more precise. - State dependent operations: Describe the database-client state machine, including which operations are valid in each state and how operations can change state.
Why it matters: Tracking state helps distinguish valid operations from those attempted before their required resources are available. - Protocols expressed in types: Explain why expressing a state-machine protocol in a type can help produce accurate sequences of actions, including in communicating systems.
Why it matters: Making a protocol explicit in a type helps prevent sequences of actions that violate its rules.
Key terms: State, runState, sequences of operations, state machine, Closed, Connected, protocol, communication pattern, deadlock
13.1 State machines: tracking state in types
This section introduces tracking state transitions in types using a door protocol as an example.
- Door protocol transitions: Describe the door’s states and which operations are allowed in each state.
Why it matters: The door gives you a concrete protocol whose allowed operations depend on the current state. - State transitions in types: Explain how recording state transitions in command types constrains operation sequences and helps interactive development.
Why it matters: Putting the protocol in types lets the type checker constrain sequences to the modeled rules.
Key terms: DoorOpen, DoorClosed, state transition, doorbell, dependent types, state transitions, well-typed description, holes, interactive editing
13.1.1 Finite state machines: modeling a door as a type
This section models a door protocol by tracking command input and output states in types so that operation sequences follow the protocol.
- Door Protocol State Tracking: Explain how tracking the door’s state in command types limits operation sequences to those allowed by the door protocol.
Why it matters: State-aware types let the protocol constrain which door operations can be combined. - Command Type Arguments: Describe the three arguments of a state-tracking door command type and interpret the start and end states of a sequence.
Why it matters: Reading these arguments tells you both what a command returns and which state transition it describes. - Door Command Sequencing: Describe the roles of the sequencing and pure-value constructors in the initial DoorCmd representation and relate do notation to sequencing.
Why it matters: These constructors provide the basic vocabulary for describing sequences of door operations.
Key terms: state machine, protocol, DoorCmd, state transitions, input state, output state, type of the value produced, (>>=), Pure, do notation
13.1.2 Interactive development of sequences of door operations
This section develops a door-operation sequence incrementally, using state-indexed types and holes to check its validity.
- Door sequence state constraints: Explain how doorProg’s input and output states constrain the sequence, and why an extra Open or a missing Close produces a type error.
Why it matters: State constraints let the type checker reject invalid door-operation sequences. - Incremental hole type inspection: Trace how inspecting the hole while building doorProg reveals the state required for the remaining actions.
Why it matters: Inspecting the remaining hole guides the next operation as you build the sequence.
Key terms: DoorCmd, DoorClosed, DoorOpen, preconditions, postconditions, type error, doorProg, hole, RingBell, Open
13.1.3 Infinite states: modeling a vending machine
This section models a vending machine’s potentially infinite states and command transitions in types, alongside its user inputs.
- Vending machine state representation: Explain how the vending machine’s state is represented and why its possible states are not finite.
Why it matters: The state representation lets the model track quantities without imposing a finite set of states. - Command state transition types: Explain what preconditions and postconditions describe for vending machine operations and how MachineCmd types express them.
Why it matters: Writing transitions in command types makes the required and resulting states explicit. - User inputs and machine commands: Distinguish possible user inputs from the commands describing what the machine does, using the vending machine interface as an example.
Why it matters: Separating user actions from machine commands helps describe both the interface and the machine’s behavior.
Key terms: state, Nat, coins, chocolates, precondition, postcondition, state transitions, MachineCmd, user input, user interface
13.1.4 A verified vending machine description
This section shows how state-indexed command types verify vending-machine operation sequences and how pattern matching establishes their preconditions.
- State indexed operation sequences: Explain how DoorCmd and MachineCmd describe valid sequences using abstract states before and after operations.
Why it matters: The types let you check a protocol description before executing it. - Vending precondition checks: Describe why an unchecked Vend fails to type-check and how pattern matching can establish its preconditions.
Why it matters: The compiler needs evidence that the machine has the resources required for a vend.
Key terms: MachineCmd, DoorCmd, state transitions, precondition, valid sequences, Vend, implicit arguments, pattern matching, pounds, chocs
13.2 Dependent types in state: implementing a stack
This section introduces stack operations and relates their typed state transitions to a length-indexed Vect representation.
- Stack access and operations: Describe how a last-in, first-out stack is accessed and what Push, Pop, and Top do, including the nonempty requirement for Pop and Top.
Why it matters: These operations define the behavior that the stack’s state machine must represent. - Typed stack size and representation: Explain how operation preconditions and postconditions describe stack sizes and why a Vect is a suitable concrete representation.
Why it matters: Connecting typed state to concrete data makes stack size information useful when representing operations.
Key terms: stack, last-in, first-out, Push, Pop, Top, precondition, postcondition, state transitions, stack height, Vect
13.2.1 Representing stack operations in a state machine
This section represents stack operations with types that track stack height and prevent popping an empty stack.
- Stack height in types: Explain how stack commands represent changes to dependently typed state using input and output heights.
Why it matters: Tracking height in command types lets the type checker follow changes to the stack. - Safe stack popping: Explain how the stack command types prevent a Pop on an empty stack, using the testAdd example.
Why it matters: The types catch an attempt to pop an empty stack before the sequence runs. - Command description and execution: Distinguish describing a sequence of stack operations from running that sequence.
Why it matters: A description of stack operations is not itself the separate function that executes them.
Key terms: StackCmd, Vect, input and output heights, dependently typed mutable state, Pop, testAdd, S height, 0, Push, stateful operations
13.2.2 Implementing the stack using Vect
This section executes typed stack operations using a Vect and demonstrates how stack-height types enforce operation preconditions and postconditions.
- Executing Stack Operations: Describe how runStack executes stack operations using a Vect, and state what happens when testAdd runs on an empty stack.
Why it matters: The Vect connects the stack’s actual contents to the height required by its operations. - Stack Height Type Guarantees: Explain how doAdd’s input and output stack heights specify its behavior and prevent applying it to a stack that is too short.
Why it matters: Tracking height in the type catches stack operations that cannot safely be applied.
Key terms: runStack, Vect, testAdd, runState, doAdd, S (S height), stack height, preconditions, postconditions, type error
13.2.3 Using a stack interactively: a stack-based calculator
This section extends the typed stack with console I/O to build an interactive calculator and explains the guarantees of its total loop.
- Calculator stack operations: Describe how entering a number or adding changes the calculator’s stack, and how tryAdd checks that addition is possible.
Why it matters: These operations show how the calculator uses the stack state to make addition safe. - Interactive stack command extensions: Explain how StackCmd and runStack are extended to support console interaction.
Why it matters: The calculator needs both stack operations and a way to communicate with the user. - Calculator input processing: Describe the calculator’s main input-processing loop and the role of strToInput.
Why it matters: The loop connects user input to the calculator’s stack commands. - Total interactive loop guarantees: Explain the separation between StackIO and StackCmd and the guarantees stated for stackCalc when it is total.
Why it matters: This separation supports an indefinitely running calculator while retaining useful safety guarantees.
Key terms: tryAdd, stack state, StackCmd, GetStr, PutStr, runStack, IO, strToInput, command type, main loop
13.3 Summary
Where this is used later
- Guaranteeing responses using a state machine and Inf (chapter 15) · A state machine is used to help guarantee responses in message-passing processes.
- Refining the door model: representing failure (chapter 14) · Refines the door model to represent failure in state transitions.
- (chapter intro) (chapter 14) · The door state machine and its typed operations.
- A verified, error-checking, door-protocol description (chapter 14) · The verified door protocol builds on state-constrained door operations.
- Defining a type for the ATM (chapter 14) · Interactive development of sequences of state-machine operations.
- Refining preconditions using auto-implicits (chapter 14) · The vending machine state tracks pounds and chocolates.
- Defining states for the ATM (chapter 14) · The vending-machine example checked command validity locally.
- Defining a concrete game state (chapter 14) · The stack example relates an abstract item count to a concrete vector.
Concepts explained in this chapter
- preconditionA condition that must hold before an operation or process can be executed.
- stackA stack is the example data structure whose items are added to and removed from its top, with only the top item accessible.
- state transitionA state transition is a change from one state of a system to another as a result of an operation.
- state machineA model that describes a system's possible states and the transitions between them.
- vending machineA vending machine is a subject of the chapter’s example model for illustrating states and operations.
- dependent typeA type whose definition can depend on values, allowing it to express more precise program properties.
- type-driven developmentAn iterative development process of specifying types, implementing functions that satisfy them, and refining both as understanding grows.
- data typeA type whose values are defined by constructors that specify ways to build values of that type.
- state monadThe state monad represents computations that read and update state while producing a result.
- pattern matchingA programming technique that defines behavior by distinguishing values according to their structure or constructor.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
