liveBook · Manning

Automated teller machine

As explained in Type-Driven Development with Idris

An automated teller machine is a self-service banking machine used as an example domain for type-checked state changes.

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

How Type-Driven Development with Idris covers it

How deep it goes
Defines states and command types, then works through a checked session; leaves EjectCard’s type simplified.
Examples
Demonstrates card insertion, PIN checking, cash dispensing, and card ejection.
What you can do after
Build a state-indexed ATM command type and describe a session whose operations depend on PIN-check results.
How it connects
Connects state machines with dependent types and runtime PIN validation.

This explainer was generated by Manning's knowledge graph from the text of Type-Driven Development with Idris. The section links point to the parts of the book it draws on.