Part III · Precision Types, Hardware & Refinement Ch 11 · 35 min · ProveIt/Ch11.lean

Pure Effects & Termination as DoS Defense

Functional cores (`Except`, `StateM`), `termination_by` proofs against infinite-loop outages, and `fun_induction`.

5-Line Outage Bug · Malformed Packet Infinite-Loop DoS
partial def parseChunks (buf : ByteArray) (offset : Nat) : List Nat :=
  if offset >= buf.size then []
  else let chunkLen := buf.get! offset |>.toNat
       chunkLen :: parseChunks buf (offset + chunkLen)
-- Attacker sends a 1-byte packet `[0x00]`. What happens on every worker thread?
Socratic Gate · Commit to a Prediction Unanswered · Locks on commit

When an attacker sends `[0x00]`, `chunkLen = 0`, so `offset + chunkLen = offset`. Why did Lean allow this CPU-pinning infinite loop to compile above?

Imperative Readability, Pure Proof Power (`do` + `Except` / `StateM`)

Real systems code uses early-return error handling and mutable-looking state updates. Lean 4's do notation compiles let mut, for, and early throw over Except Err and StateM S directly into pure functional transformers—giving you imperative ergonomics in code and pure mathematical equations in proofs (the Functional Core, Imperative Shell architecture).

`termination_by` + `fun_induction`

Once you prove a recursive parser or binary search terminates (termination_by hi - lo), Lean automatically synthesizes a custom induction principle matched to that recursion (fun_induction), eliminating manual strong-induction boilerplate.

⊢ ProveIt/Ch11.lean · DoS-Proof Chunk Parser with `termination_by` & `fun_induction`
Step 1 / 1
Lean Infoview Open Goal
Hands-On Exercise (ProveIt/Ch11.lean):

In `ProveIt/Ch11.lean`, eliminate `partial` from a recursive packet/DAG resolver, prove termination against attacker-crafted inputs, and verify its `Except`/`StateM` properties with `fun_induction`.

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