liveBook · Manning

Chapter 13: Chapter 13. State machines: verifying protocols in types

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

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

What it builds on

Section by section

Introduction

This introduction explains how separate operation descriptions and state-machine models can help express and enforce protocols in types.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Key terms: tryAdd, stack state, StackCmd, GetStr, PutStr, runStack, IO, strToInput, command type, main loop

13.3 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 12: Chapter 12. Writing programs with state Chapter 14: Chapter 14. Dependent state machines: handling feedback and errors →

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.