Part I · Executable Specifications Ch 02 · 25 min · ProveIt/Ch02.lean

Illegal States & Domain Modeling

Eliminating boolean-soup authentication structs with algebraic sum and product types.

5-Line Production Bug · Boolean-Soup Session Middleware
structure RawSession where
  isAuthenticated : Bool
  isMfaPending    : Bool
  isRevoked       : Bool
  userId          : Option Nat -- 2^3 * 2 = 16 shapes; how many make sense?
Socratic Gate · Commit to a Prediction Unanswered · Locks on commit

Suppose middleware checks `if s.isAuthenticated && !s.isMfaPending then grantAccess s.userId`. Which representable `RawSession` bypasses revocation or triggers a missing-user failure?

Counting States: Product Soup vs. Sum Precision

Every extra Bool field doubles the state space of a structure. Four independent flags create 16 combinations, even when your domain lifecycle only has four real states: anonymous, mfaPending (uid : Nat), active (uid : Nat) (tokenEpoch : Nat), and revoked (reason : String).

When you model mutually exclusive lifecycle phases with inductive Session, an anonymous or revoked session physically cannot carry an active token, and an active session physically cannot omit uid : Nat. You no longer write defensive assertions for impossible combinations because no code—human or AI—can construct them.

Exhaustive Pattern Matching as Refactoring Armor

Lean's pattern matcher checks exhaustiveness at compile time. Add a fifth lifecycle state (suspended) next quarter, and every match expression across your codebase that forgets suspended fails to compile with the exact missing constructor.

Even better: your theorems over Session split cleanly into four crisp subgoals via cases s, matching the domain 1-to-1 instead of 16 messy boolean truth-table rows.

⊢ ProveIt/Ch02.lean · Algebraic Session Lifecycle
Step 1 / 1
Lean Infoview Open Goal
Hands-On Exercise (ProveIt/Ch02.lean):

In `ProveIt/Ch02.lean`, refactor a flag-ridden payment/auth record into an `inductive` state type and prove that terminal states can never emit authorization tokens.

Finished the Socratic gate, Infoview trace, and playground run?
Record completion in local browser storage to track your 20-unit progress.