liveBook · Manning

Chapter 14: Chapter 14. Dependent state machines: handling feedback and errors

What you will learn in chapter 14 of Type-Driven Development with Idris

This introduction reviews typed state-transition guarantees and explains why realistic state machines must account for failures and environmental responses.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction reviews typed state-transition guarantees and explains why realistic state machines must account for failures and environmental responses.

Key terms: dependent type, precondition, postcondition, state transition, operation failing, response from the environment, DoorClosed

14.1 Dealing with errors in state transitions

This section contrasts the door model’s guaranteed state transitions with the possibility of real hardware failure.

Key terms: DoorCmd, Open, DoorClosed, DoorOpen, state transitions

14.1.1 Refining the door model: representing failure

This section refines the door state machine so an operation’s resulting state can depend on its return value, including failure.

Key terms: DoorCmd, DoorResult, Open, DoorOpen, DoorClosed, OK, (>>=), state2_fn, state3_fn, input state

14.1.2 A verified, error-checking, door-protocol description

This section develops a verified door protocol that handles failed opening operations using result-dependent states.

Key terms: DoorCmd, Open, OK, Jammed, DoorOpen, DoorClosed, hole, type error, case-split, DoorResult

14.2 Security properties in types: modeling an ATM

This section introduces using state-aware operation types to enforce an ATM cash-dispensing security property.

Key terms: explicit states, type checking, security property, validated card

14.2.1 Defining states for the ATM

This section defines the ATM’s states and basic operations, including state-dependent restrictions and runtime PIN validation.

Key terms: Ready, CardInserted, Session, PIN, external service, runtime, preconditions, postconditions, EjectCard, GetPIN

14.2.2 Defining a type for the ATM

This section uses state-indexed ATM commands and type-guided refinement to describe a session that safely handles PIN outcomes and returns to Ready.

Key terms: ATMCmd, state transitions, GetAmount, Message, pinOK, PINCheck, CorrectPIN, IncorrectPIN, Session, CardInserted

14.2.3 Simulating an ATM at the console: executing ATMCmd

This section describes a console interpreter for ATMCmd and explains why ATM command descriptions are separated from their implementations.

Key terms: ATMCmd, runATM, IO, inState, outState_fn, interpreter, state transitions

14.2.4 Refining preconditions using auto-implicits

This section refines ATM card-ejection preconditions with a predicate and auto-implicits, then practices state-dependent protocols in security and vending exercises.

Key terms: EjectCard, ATMState, HasCard, CardInserted, Session, Ready, auto implicit, badATM, HasCard Ready, ShellCmd

14.3 A verified guessing game: describing rules in types

This section recaps the Hangman state model and explains why enforcing game rules requires more than precisely typed game states.

Key terms: WordState, Finished, game, GameCmd, dependent type, game state

14.3.1 Defining an abstract game state and operations

This section defines an abstract guessing-game state, its basic operations, and the conditions for declaring a win or loss.

Key terms: NotRunning, Running, guesses remaining, letters still to guess, NewGame, Guess, Won, Lost, preconditions

14.3.2 Defining a type for the game state

This section defines game commands and loop types that express guessing rules and constrain valid game implementations.

Key terms: GameCmd, GameState, Pure, (>>=), ShowState, Guess, precondition, postcondition, S guesses, S letters

14.3.3 Implementing the game

This section implements a type-checked Hangman game loop that responds to guesses and describes valid game actions.

Key terms: GameLoop, Guess, GuessResult, Correct, Incorrect, Running, NotRunning, Won, ShowState, Exit

14.3.4 Defining a concrete game state

This section describes concrete game state and result representations for executing a potentially looping game.

Key terms: GameState, Game, abstract state, concrete game state, GameResult, outstate_fn, output state, Guess, Correct, Incorrect

14.3.5 Running the game: executing GameLoop

This section explains how to execute the verified Hangman game loop and commands while handling guesses and preserving totality.

Key terms: GameCmd, GameLoop, runCmd, run, ok, InProgress, Game, Guess, vector of missing letters, ReadGuess

14.4 Summary

Concepts explained in this chapter

← Chapter 13: Chapter 13. State machines: verifying protocols in types Chapter 15: Chapter 15. Type-safe concurrent programming →

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.