Add a list-based notion of finite types

6d229cb5Claude · 6d agoparent 3415679efiles
Lean core has no Fintype. Machines need a finite control and a finite
alphabet, so IsFinite says some list enumerates the type. Instances for
Bool, Unit, Fin n, Option, Sum and Prod.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Diff

2 files changed +61 -1
M PvsNP.lean +3 -1
@@ -1,3 +1,5 @@
1+import PvsNP.Basic.Finite
2+
13 /-! # P versus NP in Lean 4
24
3-Root module of the `PvsNP` library. Each module is imported here as it lands. -/
5+Root module of the `PvsNP` library: importing it imports everything. -/
@@ -0,0 +1,58 @@
1+/-!
2+# Finite types
3+
4+Lean core has no `Fintype`. A Turing machine needs a *finite* control and a *finite* alphabet: without
5+finiteness a single state (or symbol) could carry an unbounded amount of information, and time bounds
6+would mean nothing. We use the weakest workable notion: a type is finite when some list contains every
7+element.
8+-/
9+
10+namespace PvsNP
11+
12+universe u v
13+
14+/-- `IsFinite α`: some list enumerates every element of `α` (duplicates allowed). -/
15+class IsFinite (α : Type u) : Prop where
16+ exists_list : ∃ l : List α, ∀ a, a ∈ l
17+
18+namespace IsFinite
19+
20+instance instBool : IsFinite Bool :=
21+ ⟨⟨[false, true], by intro a; cases a <;> simp⟩⟩
22+
23+instance instUnit : IsFinite Unit :=
24+ ⟨⟨[()], by intro a; simp⟩⟩
25+
26+instance instFin (n : Nat) : IsFinite (Fin n) :=
27+ ⟨⟨List.finRange n, fun a => List.mem_finRange a⟩⟩
28+
29+instance instOption {α : Type u} [h : IsFinite α] : IsFinite (Option α) := by
30+ obtain ⟨l, hl⟩ := h.exists_list
31+ refine ⟨⟨none :: l.map some, ?_⟩⟩
32+ intro a
33+ cases a with
34+ | none => simp
35+ | some a => simp [hl a]
36+
37+instance instSum {α : Type u} {β : Type v} [ha : IsFinite α] [hb : IsFinite β] :
38+ IsFinite (α ⊕ β) := by
39+ obtain ⟨l₁, h₁⟩ := ha.exists_list
40+ obtain ⟨l₂, h₂⟩ := hb.exists_list
41+ refine ⟨⟨l₁.map Sum.inl ++ l₂.map Sum.inr, ?_⟩⟩
42+ intro a
43+ cases a with
44+ | inl a => simp [h₁ a]
45+ | inr b => simp [h₂ b]
46+
47+instance instProd {α : Type u} {β : Type v} [ha : IsFinite α] [hb : IsFinite β] :
48+ IsFinite (α × β) := by
49+ obtain ⟨l₁, h₁⟩ := ha.exists_list
50+ obtain ⟨l₂, h₂⟩ := hb.exists_list
51+ refine ⟨⟨l₁.flatMap (fun a => l₂.map (Prod.mk a)), ?_⟩⟩
52+ intro ⟨a, b⟩
53+ simp only [List.mem_flatMap, List.mem_map, Prod.mk.injEq]
54+ exact ⟨a, h₁ a, b, h₂ b, rfl, rfl⟩
55+
56+end IsFinite
57+
58+end PvsNP