An automated teller machine (ATM) provides a concrete domain for modeling operations whose legality depends on the machine’s current state. The model has three states: Ready, CardInserted, and Session. PIN validation may change the state at runtime because it relies on an external service. section 14.2.1 section 14.2.1
A state-indexed command type expresses how operations affect the ATM:
- InsertCard moves Ready to CardInserted; GetPIN is available when a card is inserted. section 14.2.2
- CheckPIN leads to Session for a valid PIN or leaves the machine in CardInserted for an invalid one. section 14.2.2
- Dispense requires Session; GetAmount and Message are valid in every state without changing it. section 14.2.2
A checked ATM session
A session inserts a card, reads and checks a PIN, then branches on the result. A valid PIN permits reading an amount and dispensing cash; either branch can eject the card, returning the ATM to Ready. section 14.2.2
import Data.Vect PIN : Type PIN = Vect 4 Char #1 data ATMState = Ready | CardInserted | Session #2 data PINCheck = CorrectPIN | IncorrectPIN #3 data ATMCmd : (ty : Type) -> ATMState -> (ty -> ATMState) -> Type where InsertCard : ATMCmd () Ready (const CardInserted) EjectCard : ATMCmd () state (const Ready) #4 GetPIN : ATMCmd PIN CardInserted (const CardInserted) CheckPIN : PIN -> ATMCmd PINCheck CardInserted (\check => case check of #5 CorrectPIN => Session IncorrectPIN => CardInserted) GetAmount : ATMCmd Nat state (const state) Dispense : (amount : Nat) -> ATMCmd () Session (const Session) #6 Message : String -> ATMCmd () state (const state) Pure : (res : ty) -> ATMCmd ty (state_fn res) state_fn (>>=) : ATMCmd a state1 state2_fn -> ((res : a) -> ATMCmd b (state2_fn res) state3_fn) -> ATMCmd b state1 state3_fn
The Idris definition shows the ATM states and commands with their state-transition types. section 14.2.2
