3. Data that describes the possibilities
Types are a design tool. A product describes values with several components, and a sum describes values that take one of several forms. Choosing those forms makes the cases explicit before you implement the program’s behaviour.
Structures: fields together
structure Account where
owner : String
balance : Nat
deriving Repr, BEq
def opening : Account :=
{ owner := "Ada", balance := 100 }
def deposit (a : Account) (amount : Nat) : Account :=
{ a with balance := a.balance + amount }
#eval deposit opening 25
Account is a type with a constructor and projections such as Account.balance. Record update builds a new record, copying the unspecified fields from the original.
deriving Repr generates a representation for debugging. deriving BEq generates Boolean equality.
Boolean equality computes a result. Propositional equality expresses a claim you can prove.
Inductive types: one case at a time
inductive Request where
| deposit (amount : Nat)
| withdraw (amount : Nat)
| inspect
deriving Repr
def describe : Request → String
| .deposit amount => s!"deposit {amount}"
| .withdraw amount => s!"withdraw {amount}"
| .inspect => "inspect"
Each constructor describes one form of the type and carries the data specific to it. Pattern matching makes those fields available in the corresponding branch.
Exhaustiveness checking requires the function to handle every constructor. When you add one, Lean identifies which pattern matches need a new case.
A recursive inductive type can refer to itself in its constructor fields. List α, for example, is either empty or a head of type α followed by another list. This same structure will determine both recursive programs and inductive proofs.
Failure belongs in the result type
A withdrawal that leaves a balance of 0 returns some 0. A failed withdrawal returns none.
def withdraw (balance amount : Nat) : Option Nat :=
if amount ≤ balance then some (balance - amount) else none
#eval withdraw 100 30
#eval withdraw 20 30
def withdrawMessage (balance amount : Nat) : Except String Nat :=
if amount ≤ balance then
.ok (balance - amount)
else
.error "insufficient balance"
Option α has the constructors none and some value. It is useful when absence is the only failure information needed.
Except ε α has constructors error reason and ok value. It preserves an error value of type ε.
Returning 0 on failure would confuse an invalid withdrawal with a valid withdrawal that exhausts the balance. The sum type keeps these situations distinct. Callers must address the distinction when inspecting the result.
Use a small error inductive type once callers need to respond differently to several errors. Convert errors into human-readable strings at the UI boundary. Callers can pattern-match on the typed error and display a message for each case.
Exercises
- Add a
frozen : Boolfield to a new account structure and define a function that freezes it. - Define
first? : List α → Option αby pattern matching. - Define an error type containing
insufficientFundsandfrozenAccount. Write a withdrawal function returningExceptwith this error type.