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

Packing Proofs into Types (`Subtype` & `Fin`)

Bundling invariants directly inside data structures with zero-byte runtime overhead.

5-Line API Friction Puzzle · External Hypotheses vs. Self-Certifying Types
-- Style A: Pass raw `Nat` everywhere and beg every caller for `(h1 : pct ≤ 100)`
def feeA (amount pct : Nat) (h : pct ≤ 100) : Nat := (amount * pct) / 100
-- Style B: Bundle the value AND its proof into a single type `BasisPct`:
abbrev Percent := { p : Nat // p ≤ 100 }
-- Question: How many bytes does the proof `p ≤ 100` occupy in compiled C/machine code?
Socratic Gate · Commit to a Prediction Unanswered · Locks on commit

When Lean compiles `Percent := { p : Nat // p ≤ 100 }` (`Subtype`) to native machine code, how much memory and CPU time does the proof field `property : p ≤ 100` cost at runtime?

Why Dependent Types Feel Natural *After* `omega`

Earlier courses throw Fin n ({ i : Nat // i < n }) at learners on Day 1, before they know omega or if h : ..., turning array indexing into cryptic compiler errors. Now that you command omega and grind, Subtype ({ x : α // P x }) is effortless: .val (or coercion) extracts the data, .property hands omega the built-in proof P x, and if h : idx < arr.size binds h right at your parsing boundary!

Zero-Bounds-Check Array Access (`arr[i]`) & Non-Zero Divisors

In Lean 4, arr[i] looks up a proof of i < arr.size automatically in your local context (using omega tactics behind the scenes!) and compiles to an unguarded pointer load lean_array_uget—memory safety without runtime bounds-check tax.

⊢ ProveIt/Ch09.lean · Parse-Don't-Validate with `Subtype` & `Fin`
Step 1 / 1
Lean Infoview Open Goal
Hands-On Exercise (ProveIt/Ch09.lean):

In `ProveIt/Ch09.lean`, build boundary parsers into `Subtype` / `Fin` types for a shard router and eliminate runtime out-of-bounds failures.

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