import Mathlib.InformationTheory.Coding.KraftMcMillan import Mathlib.Analysis.SpecialFunctions.Log.Base import Mathlib.Analysis.Convex.SpecificFunctions.Basic import Mathlib.Analysis.Convex.Jensen import Mathlib.Topology.Algebra.InfiniteSum.Order import Mathlib.Data.Nat.Bitwise import Mathlib.Data.Nat.Log /-! # Pillar 1: Prefix-Free Codes, Constructive Kraft–McMillan, Exact Chaitin Incompressibility, # Prefix-vs-Plain Gap, and Chaitin's Halting Probability Ω_U Self-contained Lean 4 formalization establishing exact, sharp, non-asymptotic results for: 1. Binary prefix-free codes, exact integer & real Kraft inequalities, exact full-tree Kraft equality characterization, countable Kraft (`tsum`), countable McMillan for uniquely decodable codes, and the **Constructive Dyadic Kraft Converse** (explicitly building a prefix-free binary code from any nondecreasing length sequence satisfying Kraft). 2. Prefix-free machines, plain machines, Elias logarithmic self-delimiting prefix coding, explicit Universal Prefix Machine construction, and **Exact Additive Invariance**. 3. **Exact Counting of Compressible Strings**: sharp plain bound `≤ 2^(n-c) - 1` (with exact equality witness) vs. strictly sharper prefix-free bound `≤ 2^(n-c-1)` (factor-2 tighter, with exact equality witness), plus the **Universal Non-Asymptotic Prefix-vs-Plain Gap** (`≥ ⌈log₂ N⌉` for EVERY prefix machine over lengths `< N`, matched by Elias `O(log n)`). 4. **Chaitin's Halting Probability `Ω_U`**: partial-sum monotonicity & convergence to `Ω_U`, the **Exact Chaitin Halting-Capture Theorem**, **Chaitin Prefix-Incompressibility** (`K_U(Ω_U ↾ n) ≥ n + 1 - c_D`), and **Exact Finite-Horizon Algorithmic Randomness Deficit** bounds (average prefix-complexity lower bound `n + log₂(1/Ω_U)` via Jensen + exponential tail). -/ namespace AlgorithmicInformation open Finset BigOperators Filter Topology /-! ## Section 1: Prefix-Free Sets & Exact Finite / Countable Kraft–McMillan -/ /-- A set of binary words is prefix-free if no codeword is a prefix of a distinct codeword. -/ def IsPrefixFree (S : Set (List Bool)) : Prop := ∀ x ∈ S, ∀ y ∈ S, x <+: y → x = y /-- Binary words of exact length `M`, as a `Finset (List Bool)` via `List.ofFn`. -/ def binWords (M : ℕ) : Finset (List Bool) := Finset.univ.image (fun f : Fin M → Bool => List.ofFn f) lemma mem_binWords {M : ℕ} {w : List Bool} : w ∈ binWords M ↔ w.length = M := by constructor · intro hw rcases Finset.mem_image.mp hw with ⟨f, _, rfl⟩ simp · intro hlen rw [binWords, Finset.mem_image] refine ⟨fun i => w.get (i.cast hlen.symm), Finset.mem_univ _, ?_⟩ apply List.ext_getElem · simp [hlen] · intro i hi1 hi2 simp lemma card_binWords (M : ℕ) : (binWords M).card = 2 ^ M := by rw [binWords, Finset.card_image_of_injective] · simp · intro f g h exact List.ofFn_injective h /-- Cylinder of length-`M` extensions of a word `w` (assuming `w.length ≤ M`). -/ def extCyl (M : ℕ) (w : List Bool) : Finset (List Bool) := (binWords (M - w.length)).image (fun suf => w ++ suf) lemma mem_extCyl {M : ℕ} {w u : List Bool} (hw : w.length ≤ M) : u ∈ extCyl M w ↔ u.length = M ∧ w <+: u := by constructor · intro hu rcases Finset.mem_image.mp hu with ⟨suf, hsuf, rfl⟩ have hlen := mem_binWords.mp hsuf exact ⟨by simp [hlen, Nat.add_sub_of_le hw], ⟨suf, rfl⟩⟩ · rintro ⟨hlen, suf, rfl⟩ rw [extCyl, Finset.mem_image] refine ⟨suf, mem_binWords.mpr ?_, rfl⟩ simp at hlen omega lemma card_extCyl {M : ℕ} {w : List Bool} : (extCyl M w).card = 2 ^ (M - w.length) := by rw [extCyl, Finset.card_image_of_injective] · exact card_binWords (M - w.length) · intro s1 s2 h exact List.append_cancel_left h lemma extCyl_subset_binWords {M : ℕ} {w : List Bool} (hw : w.length ≤ M) : extCyl M w ⊆ binWords M := by intro u hu exact mem_binWords.mpr ((mem_extCyl hw).mp hu).1 lemma extCyl_disjoint_of_prefixFree {S : Finset (List Bool)} (hpf : IsPrefixFree (S : Set (List Bool))) {M : ℕ} (hM : ∀ w ∈ S, w.length ≤ M) : Set.PairwiseDisjoint (S : Set (List Bool)) (extCyl M) := by intro w1 hw1 w2 hw2 hne change Disjoint (extCyl M w1) (extCyl M w2) rw [Finset.disjoint_left] intro u hu1 hu2 have h1 := ((mem_extCyl (hM w1 hw1)).mp hu1).2 have h2 := ((mem_extCyl (hM w2 hw2)).mp hu2).2 rcases h1 with ⟨s1, rfl⟩ rcases h2 with ⟨s2, heq⟩ have hcomp : w1 <+: w2 ∨ w2 <+: w1 := by by_cases hlen : w1.length ≤ w2.length · left refine ⟨(w2.drop w1.length), ?_⟩ have htake : (w1 ++ s1).take w2.length = w2 := by rw [← heq, List.take_left] have htake' : (w1 ++ s1).take w1.length = w1 := List.take_left have hsub : w1 = w2.take w1.length := by rw [← htake, List.take_take, Nat.min_eq_left hlen, htake'] conv_rhs => rw [← List.take_append_drop w1.length w2] rw [← hsub] · right have hlen' : w2.length ≤ w1.length := Nat.le_of_not_le hlen refine ⟨(w1.drop w2.length), ?_⟩ have htake : (w2 ++ s2).take w1.length = w1 := by rw [heq, List.take_left] have htake' : (w2 ++ s2).take w2.length = w2 := List.take_left have hsub : w2 = w1.take w2.length := by rw [← htake, List.take_take, Nat.min_eq_left hlen', htake'] conv_rhs => rw [← List.take_append_drop w2.length w1] rw [← hsub] rcases hcomp with h12 | h21 · exact hne (hpf w1 hw1 w2 hw2 h12) · exact hne (hpf w2 hw2 w1 hw1 h21).symm /-- **Exact Integer Binary Kraft Inequality**: for any finite prefix-free `S` with all lengths `≤ M`, `∑ w ∈ S, 2 ^ (M - w.length) ≤ 2 ^ M`. -/ theorem kraft_nat_exact {S : Finset (List Bool)} (hpf : IsPrefixFree (S : Set (List Bool))) {M : ℕ} (hM : ∀ w ∈ S, w.length ≤ M) : ∑ w ∈ S, 2 ^ (M - w.length) ≤ 2 ^ M := by have hdisj := extCyl_disjoint_of_prefixFree hpf hM have hsub : S.biUnion (extCyl M) ⊆ binWords M := by intro u hu rcases Finset.mem_biUnion.mp hu with ⟨w, hw, huw⟩ exact extCyl_subset_binWords (hM w hw) huw calc ∑ w ∈ S, 2 ^ (M - w.length) = ∑ w ∈ S, (extCyl M w).card := by refine Finset.sum_congr rfl fun w _ => ?_ exact card_extCyl.symm _ = (S.biUnion (extCyl M)).card := (Finset.card_biUnion hdisj).symm _ ≤ (binWords M).card := Finset.card_le_card hsub _ = 2 ^ M := card_binWords M /-- **Exact Full-Tree Kraft Equality Characterization**: a finite prefix-free code `S` achieves exact Kraft equality `∑ 2^(M - |w|) = 2^M` iff every length-`M` binary string has a prefix in `S`. -/ theorem kraft_nat_eq_iff_complete {S : Finset (List Bool)} (hpf : IsPrefixFree (S : Set (List Bool))) {M : ℕ} (hM : ∀ w ∈ S, w.length ≤ M) : (∑ w ∈ S, 2 ^ (M - w.length) = 2 ^ M) ↔ (∀ u : List Bool, u.length = M → ∃ w ∈ S, w <+: u) := by have hdisj := extCyl_disjoint_of_prefixFree hpf hM have hsub : S.biUnion (extCyl M) ⊆ binWords M := by intro u hu rcases Finset.mem_biUnion.mp hu with ⟨w, hw, huw⟩ exact extCyl_subset_binWords (hM w hw) huw have hcard : ∑ w ∈ S, 2 ^ (M - w.length) = (S.biUnion (extCyl M)).card := by rw [Finset.card_biUnion hdisj] exact Finset.sum_congr rfl fun w _ => card_extCyl.symm rw [hcard, ← card_binWords M] constructor · intro heq u hlen have hEqSet : S.biUnion (extCyl M) = binWords M := Finset.eq_of_subset_of_card_le hsub (le_of_eq heq.symm) have hu : u ∈ S.biUnion (extCyl M) := hEqSet ▸ mem_binWords.mpr hlen rcases Finset.mem_biUnion.mp hu with ⟨w, hw, huw⟩ exact ⟨w, hw, ((mem_extCyl (hM w hw)).mp huw).2⟩ · intro hcomp have hEqSet : S.biUnion (extCyl M) = binWords M := by apply Finset.Subset.antisymm hsub intro u hu obtain ⟨w, hw, hpre⟩ := hcomp u (mem_binWords.mp hu) exact Finset.mem_biUnion.mpr ⟨w, hw, (mem_extCyl (hM w hw)).mpr ⟨mem_binWords.mp hu, hpre⟩⟩ rw [hEqSet] /-- **Exact Real Binary Kraft Inequality** for any finite prefix-free set `S` (including `{[]}`). -/ theorem kraft_finset_real {S : Finset (List Bool)} (hpf : IsPrefixFree (S : Set (List Bool))) : ∑ w ∈ S, (2 : ℝ)⁻¹ ^ w.length ≤ 1 := by set M := S.sup List.length have hM : ∀ w ∈ S, w.length ≤ M := fun w hw => Finset.le_sup (f := List.length) hw have hnat : (∑ w ∈ S, (2 : ℝ) ^ (M - w.length)) ≤ (2 : ℝ) ^ M := by exact_mod_cast kraft_nat_exact hpf hM have hpos : (0 : ℝ) < (2 : ℝ) ^ M := by positivity have hmul : (∑ w ∈ S, (2 : ℝ)⁻¹ ^ w.length) * (2 : ℝ) ^ M = ∑ w ∈ S, (2 : ℝ) ^ (M - w.length) := by rw [Finset.sum_mul] refine Finset.sum_congr rfl fun w hw => ?_ have hle := hM w hw rw [inv_pow] have hpow : (2 : ℝ) ^ M = (2 : ℝ) ^ w.length * (2 : ℝ) ^ (M - w.length) := by rw [← pow_add, Nat.add_sub_of_le hle] rw [hpow] field_simp nlinarith /-- **Countable Binary Kraft Inequality**: any (possibly countably infinite) prefix-free set `S : Set (List Bool)` is summable under `w ↦ 2⁻¹^|w|` with `tsum ≤ 1`. -/ theorem kraft_countable_summable {S : Set (List Bool)} (hpf : IsPrefixFree S) : Summable (fun p : S => (2 : ℝ)⁻¹ ^ p.1.length) := by refine summable_of_sum_le (c := 1) (fun p => by positivity) ?_ intro F let S' : Finset (List Bool) := F.image Subtype.val have hpf' : IsPrefixFree (S' : Set (List Bool)) := by intro x hx y hy hxy have hx' : x ∈ S := by rcases Finset.mem_image.mp hx with ⟨p, _, rfl⟩ exact p.2 have hy' : y ∈ S := by rcases Finset.mem_image.mp hy with ⟨q, _, rfl⟩ exact q.2 exact hpf x hx' y hy' hxy have hsum : ∑ p ∈ F, (2 : ℝ)⁻¹ ^ p.1.length = ∑ w ∈ S', (2 : ℝ)⁻¹ ^ w.length := by rw [Finset.sum_image] intro a _ b _ h exact Subtype.ext h rw [hsum] exact kraft_finset_real hpf' /-- **Countable Binary Kraft `tsum` Bound**: `∑' p : S, (2⁻¹)^|p| ≤ 1`. -/ theorem kraft_countable_tsum_le_one {S : Set (List Bool)} (hpf : IsPrefixFree S) : ∑' p : S, (2 : ℝ)⁻¹ ^ p.1.length ≤ 1 := by have hsum := kraft_countable_summable hpf refine hsum.tsum_le_of_sum_le ?_ intro F let S' : Finset (List Bool) := F.image Subtype.val have hpf' : IsPrefixFree (S' : Set (List Bool)) := by intro x hx y hy hxy rcases Finset.mem_image.mp hx with ⟨p, _, rfl⟩ rcases Finset.mem_image.mp hy with ⟨q, _, rfl⟩ exact hpf _ p.2 _ q.2 hxy have hsum_eq : ∑ p ∈ F, (2 : ℝ)⁻¹ ^ p.1.length = ∑ w ∈ S', (2 : ℝ)⁻¹ ^ w.length := by rw [Finset.sum_image] intro a _ b _ h exact Subtype.ext h rw [hsum_eq] exact kraft_finset_real hpf' /-- Every prefix-free code not containing the empty word is uniquely decodable. -/ theorem isPrefixFree_uniquelyDecodable {S : Set (List Bool)} (hpf : IsPrefixFree S) (hne : [] ∉ S) : InformationTheory.UniquelyDecodable S := by intro l₁ l₂ hl₁ hl₂ hflat induction l₁ generalizing l₂ with | nil => cases l₂ with | nil => rfl | cons y ys => have hy : y ∈ S := hl₂ y (by simp) have hnil : y ++ ys.flatten = [] := by simpa using hflat.symm have hy0 : y = [] := (List.append_eq_nil_iff.mp hnil).1 exact (hne (hy0 ▸ hy)).elim | cons x xs ih => cases l₂ with | nil => have hx : x ∈ S := hl₁ x (by simp) have hnil : x ++ xs.flatten = [] := by simpa using hflat have hx0 : x = [] := (List.append_eq_nil_iff.mp hnil).1 exact (hne (hx0 ▸ hx)).elim | cons y ys => have hx : x ∈ S := hl₁ x (by simp) have hy : y ∈ S := hl₂ y (by simp) have hflat' : x ++ xs.flatten = y ++ ys.flatten := by simpa using hflat have hxy : x = y := by by_cases hlen : x.length ≤ y.length · have hpre : x <+: y := by refine ⟨y.drop x.length, ?_⟩ have h1 : (x ++ xs.flatten).take y.length = y := by rw [hflat', List.take_left] have h2 : (x ++ xs.flatten).take x.length = x := List.take_left have h3 : x = y.take x.length := by rw [← h1, List.take_take, Nat.min_eq_left hlen, h2] conv_rhs => rw [← List.take_append_drop x.length y] rw [← h3] exact hpf x hx y hy hpre · have hlen' : y.length ≤ x.length := Nat.le_of_not_le hlen have hpre : y <+: x := by refine ⟨x.drop y.length, ?_⟩ have h1 : (y ++ ys.flatten).take x.length = x := by rw [← hflat', List.take_left] have h2 : (y ++ ys.flatten).take y.length = y := List.take_left have h3 : y = x.take y.length := by rw [← h1, List.take_take, Nat.min_eq_left hlen', h2] conv_rhs => rw [← List.take_append_drop y.length x] rw [← h3] exact (hpf y hy x hx hpre).symm subst hxy have htail : xs.flatten = ys.flatten := List.append_cancel_left hflat' have hxs : xs = ys := ih ys (fun z hz => hl₁ z (List.mem_cons_of_mem _ hz)) (fun z hz => hl₂ z (List.mem_cons_of_mem _ hz)) htail rw [hxs] /-- **Countable D-ary McMillan Inequality** for any uniquely decodable code `S : Set (List α)` over any finite nonempty alphabet `α`. -/ theorem mcmillan_countable_tsum_le_one {α : Type*} [Fintype α] [Nonempty α] {S : Set (List α)} (hud : InformationTheory.UniquelyDecodable S) : Summable (fun p : S => (1 / (Fintype.card α : ℝ)) ^ p.1.length) ∧ ∑' p : S, (1 / (Fintype.card α : ℝ)) ^ p.1.length ≤ 1 := by have hfin : ∀ F : Finset S, ∑ p ∈ F, (1 / (Fintype.card α : ℝ)) ^ p.1.length ≤ 1 := by intro F classical let S' : Finset (List α) := F.image Subtype.val have hud' : InformationTheory.UniquelyDecodable (S' : Set (List α)) := by intro l1 l2 hl1 hl2 hflat refine hud l1 l2 ?_ ?_ hflat · intro w hw have hw' : w ∈ S' := hl1 w hw rcases Finset.mem_image.mp hw' with ⟨p, _, rfl⟩ exact p.2 · intro w hw have hw' : w ∈ S' := hl2 w hw rcases Finset.mem_image.mp hw' with ⟨p, _, rfl⟩ exact p.2 have hsum : ∑ p ∈ F, (1 / (Fintype.card α : ℝ)) ^ p.1.length = ∑ w ∈ S', (1 / (Fintype.card α : ℝ)) ^ w.length := by rw [Finset.sum_image] intro a _ b _ h exact Subtype.ext h rw [hsum] exact InformationTheory.kraft_mcmillan_inequality hud' have hsum : Summable (fun p : S => (1 / (Fintype.card α : ℝ)) ^ p.1.length) := summable_of_sum_le (c := 1) (fun p => by positivity) hfin exact ⟨hsum, hsum.tsum_le_of_sum_le hfin⟩ /-! ## Section 2: Constructive Dyadic Kraft Converse -/ /-- Big-endian `k`-bit binary representation of a natural number `n` (`n < 2^k`). -/ def toBitsBE (k n : ℕ) : List Bool := List.ofFn (fun i : Fin k => (n / 2 ^ (k - 1 - i.1)) % 2 = 1) lemma toBitsBE_length (k n : ℕ) : (toBitsBE k n).length = k := by simp [toBitsBE] /-- Decode big-endian bit list back to `ℕ`. -/ def fromBitsBE : List Bool → ℕ | [] => 0 | b :: bs => (if b then 1 else 0) * 2 ^ bs.length + fromBitsBE bs lemma fromBitsBE_append (u v : List Bool) : fromBitsBE (u ++ v) = fromBitsBE u * 2 ^ v.length + fromBitsBE v := by induction u with | nil => simp [fromBitsBE] | cons b bs ih => simp only [List.cons_append, fromBitsBE, List.length_append, ih, pow_add] ring lemma fromBitsBE_lt (u : List Bool) : fromBitsBE u < 2 ^ u.length := by induction u with | nil => simp [fromBitsBE] | cons b bs ih => simp only [fromBitsBE, List.length_cons, pow_succ] cases b <;> simp <;> omega lemma toBitsBE_succ_eq (k n : ℕ) : toBitsBE (k + 1) n = (decide ((n / 2 ^ k) % 2 = 1)) :: toBitsBE k n := by apply List.ext_getElem · simp [toBitsBE] · intro i hi1 hi2 cases i with | zero => simp [toBitsBE] | succ j => simp only [toBitsBE, List.getElem_ofFn, List.getElem_cons_succ] have hsub : k + 1 - 1 - (j + 1) = k - 1 - j := by omega rw [hsub] lemma toBitsBE_mod (k n : ℕ) : toBitsBE k (n % 2 ^ k) = toBitsBE k n := by apply List.ext_getElem · simp [toBitsBE] · intro i hi1 hi2 simp only [toBitsBE, List.getElem_ofFn] have hlt : k - 1 - i < k := by simp [toBitsBE] at hi1 omega have hdiv : (n % 2 ^ k) / 2 ^ (k - 1 - i) % 2 = (n / 2 ^ (k - 1 - i)) % 2 := by set r := k - 1 - i have hdecomp : 2 ^ k = 2 ^ r * (2 * 2 ^ (k - 1 - r)) := by rw [← mul_assoc, ← pow_succ, ← pow_add] congr 1 omega have hmod_step : ∀ a b c : ℕ, 0 < b → 0 < c → (a % (b * c)) / b = (a / b) % c := by intro a b c hb hc have hq : a = b * c * (a / (b * c)) + a % (b * c) := (Nat.div_add_mod a (b * c)).symm have h1 : a / b = c * (a / (b * c)) + (a % (b * c)) / b := by have hmul : b * c * (a / (b * c)) = b * (c * (a / (b * c))) := by ring conv_lhs => rw [hq, hmul] rw [Nat.mul_add_div hb] have hlt_c : (a % (b * c)) / b < c := by rw [Nat.div_lt_iff_lt_mul hb, mul_comm c b] exact Nat.mod_lt a (Nat.mul_pos hb hc) rw [h1, Nat.mul_add_mod, Nat.mod_eq_of_lt hlt_c] rw [hdecomp, hmod_step n (2 ^ r) (2 * 2 ^ (k - 1 - r)) (by positivity) (by positivity)] rw [Nat.mod_mul_right_mod] simp [hdiv] lemma fromBitsBE_toBitsBE (k n : ℕ) (hn : n < 2 ^ k) : fromBitsBE (toBitsBE k n) = n := by induction k generalizing n with | zero => simp at hn simp [toBitsBE, fromBitsBE, hn] | succ k ih => rw [toBitsBE_succ_eq, fromBitsBE, toBitsBE_length] rw [← toBitsBE_mod k n, ih (n % 2 ^ k) (Nat.mod_lt n (by positivity))] have hdiv_lt : n / 2 ^ k < 2 := by rw [Nat.div_lt_iff_lt_mul (by positivity)] rw [pow_succ] at hn linarith have hbit : (if decide ((n / 2 ^ k) % 2 = 1) = true then 1 else 0) = n / 2 ^ k := by cases hq : n / 2 ^ k with | zero => simp | succ q' => have hq0 : q' = 0 := by omega subst hq0; simp rw [hbit] have hrem := Nat.div_add_mod n (2 ^ k) linarith /-- Prefix criterion for `toBitsBE`: if `toBitsBE l₁ v₁ <+: toBitsBE l₂ v₂` with `v₁ < 2^l₁` and `v₂ < 2^l₂`, then `v₁ * 2^(l₂ - l₁) ≤ v₂ < (v₁ + 1) * 2^(l₂ - l₁)`. -/ lemma toBitsBE_prefix_bounds {l₁ l₂ v₁ v₂ : ℕ} (hv₁ : v₁ < 2 ^ l₁) (hv₂ : v₂ < 2 ^ l₂) (hpre : toBitsBE l₁ v₁ <+: toBitsBE l₂ v₂) : v₁ * 2 ^ (l₂ - l₁) ≤ v₂ ∧ v₂ < (v₁ + 1) * 2 ^ (l₂ - l₁) := by rcases hpre with ⟨suf, hsuf⟩ have hlen : l₁ + suf.length = l₂ := by have := congrArg List.length hsuf simp [toBitsBE_length] at this exact this have hsuf_len : suf.length = l₂ - l₁ := by omega have hdec := congrArg fromBitsBE hsuf rw [fromBitsBE_append, fromBitsBE_toBitsBE l₁ v₁ hv₁, fromBitsBE_toBitsBE l₂ v₂ hv₂, hsuf_len] at hdec have hlt := fromBitsBE_lt suf rw [hsuf_len] at hlt constructor <;> nlinarith /-- Constructive dyadic prefix-free code for a monotone length function `ℓ : Fin m → ℕ`. -/ def kraftCode {m : ℕ} (ℓ : Fin m → ℕ) (L : ℕ) (i : Fin m) : List Bool := let acc := ∑ j ∈ Finset.univ.filter (fun j : Fin m => j.1 < i.1), 2 ^ (L - ℓ j) toBitsBE (ℓ i) (acc / 2 ^ (L - ℓ i)) /-- **Constructive Dyadic Kraft Converse**: for any nondecreasing sequence of lengths `ℓ : Fin m → ℕ` bounded by `L` satisfying the Kraft inequality `∑ i, 2^(L - ℓ i) ≤ 2^L`, `kraftCode ℓ L` is an explicit injective prefix-free binary code of exact lengths `ℓ`. -/ theorem kraft_converse_constructive {m : ℕ} (ℓ : Fin m → ℕ) (L : ℕ) (hmono : Monotone ℓ) (hL : ∀ i, ℓ i ≤ L) (hkraft : ∑ i : Fin m, 2 ^ (L - ℓ i) ≤ 2 ^ L) : (∀ i, (kraftCode ℓ L i).length = ℓ i) ∧ Function.Injective (kraftCode ℓ L) ∧ IsPrefixFree (Set.range (kraftCode ℓ L)) := by have hlen : ∀ i, (kraftCode ℓ L i).length = ℓ i := fun i => toBitsBE_length _ _ -- Helper: `acc i` is divisible by `2 ^ (L - ℓ i)` because `ℓ j ≤ ℓ i` for `j < i`. have hdvd : ∀ i : Fin m, 2 ^ (L - ℓ i) ∣ ∑ j ∈ Finset.univ.filter (fun j : Fin m => j.1 < i.1), 2 ^ (L - ℓ j) := by intro i apply Finset.dvd_sum intro j hj have hji : j ≤ i := Fin.le_def.mpr (le_of_lt (Finset.mem_filter.mp hj).2) have hℓ : ℓ j ≤ ℓ i := hmono hji exact Nat.pow_dvd_pow 2 (by omega) have hacc_lt : ∀ i : Fin m, (∑ j ∈ Finset.univ.filter (fun j : Fin m => j.1 < i.1), 2 ^ (L - ℓ j)) + 2 ^ (L - ℓ i) ≤ 2 ^ L := by intro i have hmem : i ∉ Finset.univ.filter (fun j : Fin m => j.1 < i.1) := by simp calc (∑ j ∈ Finset.univ.filter (fun j : Fin m => j.1 < i.1), 2 ^ (L - ℓ j)) + 2 ^ (L - ℓ i) = ∑ j ∈ insert i (Finset.univ.filter (fun j : Fin m => j.1 < i.1)), 2 ^ (L - ℓ j) := by rw [Finset.sum_insert hmem, add_comm] _ ≤ ∑ j : Fin m, 2 ^ (L - ℓ j) := Finset.sum_le_univ_sum_of_nonneg (fun _ => Nat.zero_le _) _ ≤ 2 ^ L := hkraft have hv_lt : ∀ i : Fin m, (∑ j ∈ Finset.univ.filter (fun j : Fin m => j.1 < i.1), 2 ^ (L - ℓ j)) / 2 ^ (L - ℓ i) < 2 ^ ℓ i := by intro i rw [Nat.div_lt_iff_lt_mul (by positivity)] have hpow : 2 ^ ℓ i * 2 ^ (L - ℓ i) = 2 ^ L := by rw [← pow_add, Nat.add_sub_of_le (hL i)] rw [hpow] have h1 := hacc_lt i have hpos : 0 < 2 ^ (L - ℓ i) := by positivity omega -- Key separation lemma: if `i < k`, then `kraftCode i` is NOT a prefix of `kraftCode k`, -- and `kraftCode k` is NOT a prefix of `kraftCode i`. have hnot_pre : ∀ i k : Fin m, i.1 < k.1 → ¬ (kraftCode ℓ L i <+: kraftCode ℓ L k) ∧ ¬ (kraftCode ℓ L k <+: kraftCode ℓ L i) := by intro i k hik set acc_i := ∑ j ∈ Finset.univ.filter (fun j : Fin m => j.1 < i.1), 2 ^ (L - ℓ j) set acc_k := ∑ j ∈ Finset.univ.filter (fun j : Fin m => j.1 < k.1), 2 ^ (L - ℓ j) have hℓik : ℓ i ≤ ℓ k := hmono (Fin.le_def.mpr (le_of_lt hik)) have hacc_gap : acc_i + 2 ^ (L - ℓ i) ≤ acc_k := by have hsub : insert i (Finset.univ.filter (fun j : Fin m => j.1 < i.1)) ⊆ Finset.univ.filter (fun j : Fin m => j.1 < k.1) := by intro j hj rcases Finset.mem_insert.mp hj with rfl | hj' · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, hik⟩ · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, lt_trans (Finset.mem_filter.mp hj').2 hik⟩ have hni : i ∉ Finset.univ.filter (fun j : Fin m => j.1 < i.1) := by simp calc acc_i + 2 ^ (L - ℓ i) = ∑ j ∈ insert i (Finset.univ.filter (fun j : Fin m => j.1 < i.1)), 2 ^ (L - ℓ j) := by rw [Finset.sum_insert hni, add_comm] _ ≤ acc_k := Finset.sum_le_sum_of_subset hsub have hmul_i : (acc_i / 2 ^ (L - ℓ i)) * 2 ^ (L - ℓ i) = acc_i := Nat.div_mul_cancel (hdvd i) have hmul_k : (acc_k / 2 ^ (L - ℓ k)) * 2 ^ (L - ℓ k) = acc_k := Nat.div_mul_cancel (hdvd k) constructor · intro hpre have hb := (toBitsBE_prefix_bounds (hv_lt i) (hv_lt k) hpre).2 -- Multiply `hb` by `2 ^ (L - ℓ k)` have hpow : 2 ^ (ℓ k - ℓ i) * 2 ^ (L - ℓ k) = 2 ^ (L - ℓ i) := by rw [← pow_add] congr 1 have := hL k omega have hmul_lt : (acc_k / 2 ^ (L - ℓ k)) * 2 ^ (L - ℓ k) < ((acc_i / 2 ^ (L - ℓ i)) + 1) * 2 ^ (ℓ k - ℓ i) * 2 ^ (L - ℓ k) := Nat.mul_lt_mul_of_pos_right hb (by positivity) rw [hmul_k, mul_assoc, hpow, add_mul, one_mul, hmul_i] at hmul_lt omega · intro hpre have hlen_le : ℓ k ≤ ℓ i := by have := List.IsPrefix.length_le hpre simpa [hlen] using this have hℓeq : ℓ k = ℓ i := le_antisymm hlen_le hℓik have hveq : acc_k / 2 ^ (L - ℓ k) = acc_i / 2 ^ (L - ℓ i) := by have hb := toBitsBE_prefix_bounds (hv_lt k) (hv_lt i) hpre have hsub0 : ℓ i - ℓ k = 0 := by omega rw [hsub0, pow_zero, mul_one, mul_one] at hb exact le_antisymm hb.1 (Nat.lt_succ_iff.mp hb.2) have hEq : acc_k = acc_i := by rw [← hmul_k, ← hmul_i, hveq, hℓeq] have hpos : 0 < 2 ^ (L - ℓ i) := by positivity omega refine ⟨hlen, ?_, ?_⟩ · intro i k heq by_contra hne rcases lt_or_gt_of_ne (Fin.val_ne_of_ne hne) with hik | hki · exact (hnot_pre i k hik).1 (by rw [heq]) · exact (hnot_pre k i hki).1 (by rw [heq]) · intro x hx y hy hxy rcases hx with ⟨i, rfl⟩ rcases hy with ⟨k, rfl⟩ by_cases heq : i = k · rw [heq] · rcases lt_or_gt_of_ne (Fin.val_ne_of_ne heq) with hik | hki · exact ((hnot_pre i k hik).1 hxy).elim · exact ((hnot_pre k i hki).2 hxy).elim /-! ## Section 3: Prefix-Free Machines, Elias Prefix Coding & Universal Invariance -/ /-- A plain machine maps binary programs to optional binary outputs. -/ abbrev PlainMachine := List Bool → Option (List Bool) /-- A prefix-free machine is a partial machine whose halting domain is prefix-free. -/ structure PrefixMachine where run : List Bool → Option (List Bool) prefix_free : IsPrefixFree {p : List Bool | (run p).isSome} /-- Plain Kolmogorov complexity `C_M(x) ∈ ℕ∞`. -/ noncomputable def plainK (M : PlainMachine) (x : List Bool) : ℕ∞ := ⨅ (p : List Bool) (_ : M p = some x), (p.length : ℕ∞) /-- Prefix-free Kolmogorov complexity `K_U(x) ∈ ℕ∞`. -/ noncomputable def prefixK (U : PrefixMachine) (x : List Bool) : ℕ∞ := ⨅ (p : List Bool) (_ : U.run p = some x), (p.length : ℕ∞) lemma plainK_le_of_eq {M : PlainMachine} {p x : List Bool} (h : M p = some x) : plainK M x ≤ p.length := iInf₂_le p h lemma prefixK_le_of_eq {U : PrefixMachine} {p x : List Bool} (h : U.run p = some x) : prefixK U x ≤ p.length := iInf₂_le p h lemma exists_program_of_plainK_le {M : PlainMachine} {x : List Bool} {k : ℕ} (h : plainK M x ≤ k) : ∃ p : List Bool, M p = some x ∧ p.length ≤ k := by by_contra hall push Not at hall have hge : ((k + 1 : ℕ) : ℕ∞) ≤ plainK M x := by refine le_iInf₂ fun p hp => ?_ exact_mod_cast hall p hp have hlt : (k : ℕ∞) < ((k + 1 : ℕ) : ℕ∞) := by exact_mod_cast Nat.lt_succ_self k exact not_lt_of_ge (le_trans hge h) hlt lemma exists_program_of_prefixK_le {U : PrefixMachine} {x : List Bool} {k : ℕ} (h : prefixK U x ≤ k) : ∃ p : List Bool, U.run p = some x ∧ p.length ≤ k := by by_contra hall push Not at hall have hge : ((k + 1 : ℕ) : ℕ∞) ≤ prefixK U x := by refine le_iInf₂ fun p hp => ?_ exact_mod_cast hall p hp have hlt : (k : ℕ∞) < ((k + 1 : ℕ) : ℕ∞) := by exact_mod_cast Nat.lt_succ_self k exact not_lt_of_ge (le_trans hge h) hlt /-- Unary prefix-free code of a natural number `n`: `n` ones followed by a single zero. -/ def unaryCode (n : ℕ) : List Bool := List.replicate n true ++ [false] lemma unaryCode_length (n : ℕ) : (unaryCode n).length = n + 1 := by simp [unaryCode] lemma unaryCode_prefix_cancel {a b : ℕ} {s t : List Bool} (h : unaryCode a ++ s = unaryCode b ++ t) : a = b ∧ s = t := by induction a generalizing b with | zero => cases b with | zero => simpa [unaryCode] using h | succ b' => simp [unaryCode, List.replicate_succ] at h | succ a' ih => cases b with | zero => simp [unaryCode, List.replicate_succ] at h | succ b' => simp only [unaryCode, List.replicate_succ, List.cons_append, List.cons.injEq] at h obtain ⟨rfl, rfl⟩ := ih h.2 exact ⟨rfl, rfl⟩ /-- **Elias-Gamma Self-Delimiting Prefix Code** of a natural number `n`: `unaryCode (Nat.log 2 (n + 1)) ++ toBitsBE (Nat.log 2 (n + 1)) (n + 1 - 2 ^ Nat.log 2 (n + 1))`, of exact length `2 * Nat.log 2 (n + 1) + 1`. -/ def eliasNat (n : ℕ) : List Bool := let k := Nat.log 2 (n + 1) unaryCode k ++ toBitsBE k (n + 1 - 2 ^ k) lemma eliasNat_length (n : ℕ) : (eliasNat n).length = 2 * Nat.log 2 (n + 1) + 1 := by simp only [eliasNat, List.length_append, unaryCode_length, toBitsBE_length] omega lemma eliasNat_prefix_cancel {m n : ℕ} {s t : List Bool} (h : eliasNat m ++ s = eliasNat n ++ t) : m = n ∧ s = t := by simp only [eliasNat, List.append_assoc] at h obtain ⟨hk, hrest⟩ := unaryCode_prefix_cancel h set k := Nat.log 2 (m + 1) have hk' : Nat.log 2 (n + 1) = k := hk.symm rw [hk'] at hrest have htake := congrArg (List.take k) hrest rw [List.take_left' (toBitsBE_length k _), List.take_left' (toBitsBE_length k _)] at htake have hdrop := congrArg (List.drop k) hrest rw [List.drop_left' (toBitsBE_length k _), List.drop_left' (toBitsBE_length k _)] at hdrop have hm_lt : m + 1 - 2 ^ k < 2 ^ k := by have hlt := Nat.lt_pow_succ_log_self (by norm_num : 1 < 2) (m + 1) have hle : 2 ^ k ≤ m + 1 := Nat.pow_log_le_self 2 (Nat.succ_ne_zero m) rw [pow_succ] at hlt change m + 1 < 2 ^ k * 2 at hlt omega have hn_lt : n + 1 - 2 ^ k < 2 ^ k := by have hlt := Nat.lt_pow_succ_log_self (by norm_num : 1 < 2) (n + 1) have hle : 2 ^ k ≤ n + 1 := by rw [← hk'] exact Nat.pow_log_le_self 2 (Nat.succ_ne_zero n) rw [hk', pow_succ] at hlt omega have hval := congrArg fromBitsBE htake rw [fromBitsBE_toBitsBE k _ hm_lt, fromBitsBE_toBitsBE k _ hn_lt] at hval have hm_ge : 2 ^ k ≤ m + 1 := Nat.pow_log_le_self 2 (Nat.succ_ne_zero m) have hn_ge : 2 ^ k ≤ n + 1 := by rw [← hk'] exact Nat.pow_log_le_self 2 (Nat.succ_ne_zero n) exact ⟨by omega, hdrop⟩ /-- **Elias Self-Delimiting Encoding of Binary Strings**: `eliasDelim w = eliasNat |w| ++ w`, with exact length `|w| + 2 * ⌊log₂(|w| + 1)⌋ + 1`. -/ def eliasDelim (w : List Bool) : List Bool := eliasNat w.length ++ w lemma eliasDelim_length (w : List Bool) : (eliasDelim w).length = w.length + 2 * Nat.log 2 (w.length + 1) + 1 := by simp only [eliasDelim, List.length_append, eliasNat_length] omega theorem eliasDelim_prefixFree : IsPrefixFree (Set.range eliasDelim) := by rintro _ ⟨w1, rfl⟩ _ ⟨w2, rfl⟩ ⟨suf, hsuf⟩ rw [eliasDelim, eliasDelim, List.append_assoc] at hsuf obtain ⟨hlen, hrest⟩ := eliasNat_prefix_cancel hsuf have htake := congrArg (List.take w1.length) hrest rw [List.take_left, hlen, List.take_length] at htake rw [htake] /-- Explicit prefix-free machine implementing Elias self-delimiting identity decoding. -/ noncomputable def eliasIdentityMachine : PrefixMachine where run p := by classical exact if h : ∃ w : List Bool, eliasDelim w = p then some (Classical.choose h) else none prefix_free := by classical intro p1 hp1 p2 hp2 hpre change ((if h : ∃ w : List Bool, eliasDelim w = p1 then some (Classical.choose h) else none).isSome = true) at hp1 change ((if h : ∃ w : List Bool, eliasDelim w = p2 then some (Classical.choose h) else none).isSome = true) at hp2 have h1 : ∃ w, eliasDelim w = p1 := by by_contra hc; simp [hc] at hp1 have h2 : ∃ w, eliasDelim w = p2 := by by_contra hc; simp [hc] at hp2 exact eliasDelim_prefixFree p1 h1 p2 h2 hpre /-- Explicit **Elias Upper Bound** on prefix-free complexity: `K_{elias}(x) ≤ |x| + 2 ⌊log₂(|x| + 1)⌋ + 1` for every `x`. -/ theorem eliasIdentityMachine_prefixK_le (x : List Bool) : prefixK eliasIdentityMachine x ≤ x.length + 2 * Nat.log 2 (x.length + 1) + 1 := by classical have hrun : eliasIdentityMachine.run (eliasDelim x) = some x := by have hex : ∃ w, eliasDelim w = eliasDelim x := ⟨x, rfl⟩ change (if h : ∃ w : List Bool, eliasDelim w = eliasDelim x then some (Classical.choose h) else none) = some x rw [dif_pos hex] congr 1 have hspec : eliasDelim (Classical.choose hex) = eliasDelim x := Classical.choose_spec hex have hpre : eliasNat (Classical.choose hex).length ++ Classical.choose hex = eliasNat x.length ++ x := hspec exact (eliasNat_prefix_cancel hpre).2 calc prefixK eliasIdentityMachine x ≤ (eliasDelim x).length := prefixK_le_of_eq hrun _ = x.length + 2 * Nat.log 2 (x.length + 1) + 1 := by exact_mod_cast eliasDelim_length x /-- **Explicit Universal Prefix Machine** simulating any countable family `M : ℕ → PrefixMachine` via Elias-gamma program indexing `eliasNat i ++ q ↦ (M i).run q`. -/ noncomputable def univPrefixMachine (M : ℕ → PrefixMachine) : PrefixMachine where run p := by classical exact if h : ∃ i q, p = eliasNat i ++ q ∧ ((M i).run q).isSome then (M (Classical.choose h)).run (Classical.choose (Classical.choose_spec h)) else none prefix_free := by classical intro p1 hp1 p2 hp2 hpre change ((if h : ∃ i q, p1 = eliasNat i ++ q ∧ ((M i).run q).isSome then (M (Classical.choose h)).run (Classical.choose (Classical.choose_spec h)) else none).isSome = true) at hp1 change ((if h : ∃ i q, p2 = eliasNat i ++ q ∧ ((M i).run q).isSome then (M (Classical.choose h)).run (Classical.choose (Classical.choose_spec h)) else none).isSome = true) at hp2 have h1 : ∃ i q, p1 = eliasNat i ++ q ∧ ((M i).run q).isSome := by by_contra hc; simp [hc] at hp1 have h2 : ∃ i q, p2 = eliasNat i ++ q ∧ ((M i).run q).isSome := by by_contra hc; simp [hc] at hp2 rcases h1 with ⟨i1, q1, rfl, hq1⟩ rcases h2 with ⟨i2, q2, rfl, hq2⟩ rcases hpre with ⟨suf, hsuf⟩ rw [List.append_assoc] at hsuf obtain ⟨rfl, hrest⟩ := eliasNat_prefix_cancel hsuf have hqpre : q1 <+: q2 := ⟨suf, hrest⟩ have hqeq : q1 = q2 := (M i1).prefix_free q1 hq1 q2 hq2 hqpre rw [hqeq] /-- **Exact Universal Prefix-Machine Additive Invariance Theorem**: `K_U(x) ≤ K_{M_i}(x) + (2 ⌊log₂(i + 1)⌋ + 1)` with the exact Elias machine constant. -/ theorem universal_prefix_invariance (M : ℕ → PrefixMachine) (i : ℕ) (x : List Bool) : prefixK (univPrefixMachine M) x ≤ prefixK (M i) x + (2 * Nat.log 2 (i + 1) + 1) := by classical have hle_all : ∀ q : List Bool, (M i).run q = some x → prefixK (univPrefixMachine M) x ≤ (q.length : ℕ∞) + (2 * Nat.log 2 (i + 1) + 1 : ℕ) := by intro q hq have hrun : (univPrefixMachine M).run (eliasNat i ++ q) = some x := by have hex : ∃ j r, eliasNat i ++ q = eliasNat j ++ r ∧ ((M j).run r).isSome := ⟨i, q, rfl, by simp [hq]⟩ change (if h : ∃ j r, eliasNat i ++ q = eliasNat j ++ r ∧ ((M j).run r).isSome then (M (Classical.choose h)).run (Classical.choose (Classical.choose_spec h)) else none) = some x rw [dif_pos hex] have hspec := Classical.choose_spec (Classical.choose_spec hex) obtain ⟨hj, hr⟩ := eliasNat_prefix_cancel hspec.1 have hrun_eq : ∀ (j' : ℕ) (r' : List Bool), i = j' → q = r' → (M j').run r' = some x := by rintro _ _ rfl rfl; exact hq exact hrun_eq _ _ hj hr calc prefixK (univPrefixMachine M) x ≤ (eliasNat i ++ q).length := prefixK_le_of_eq hrun _ = (q.length : ℕ∞) + (2 * Nat.log 2 (i + 1) + 1 : ℕ) := by simp only [List.length_append, eliasNat_length] norm_cast omega by_cases hex : ∃ q : List Bool, (M i).run q = some x · rcases hex with ⟨q0, hq0⟩ have hfin : prefixK (M i) x ≠ ⊤ := ne_top_of_le_ne_top (by simp) (prefixK_le_of_eq hq0) set m := (prefixK (M i) x).toNat have hm_eq : prefixK (M i) x = m := (ENat.natCast_toNat hfin).symm obtain ⟨q, hq, hqlen⟩ := exists_program_of_prefixK_le (le_of_eq hm_eq) rw [hm_eq] calc prefixK (univPrefixMachine M) x ≤ (q.length : ℕ∞) + (2 * Nat.log 2 (i + 1) + 1 : ℕ) := hle_all q hq _ ≤ (m : ℕ∞) + (2 * Nat.log 2 (i + 1) + 1 : ℕ) := by gcongr · have htop : prefixK (M i) x = ⊤ := by simp only [prefixK, iInf_eq_top] intro q hq exact (hex ⟨q, hq⟩).elim rw [htop] simp /-! ## Section 4: Exact Counting of Compressible Strings & Prefix-vs-Plain Gap -/ /-- Plain compressible strings of length `n` with `C_M(x) < k`. -/ noncomputable def plainCompressible (M : PlainMachine) (n k : ℕ) : Finset (List Bool) := by classical exact (binWords n).filter (fun x => plainK M x < k) /-- Prefix-free compressible strings of length `n` with `K_U(x) < k`. -/ noncomputable def prefixCompressible (U : PrefixMachine) (n k : ℕ) : Finset (List Bool) := by classical exact (binWords n).filter (fun x => prefixK U x < k) /-- **Exact Plain Incompressibility Counting Bound**: `|{x ∈ {0,1}^n : C_M(x) < k}| ≤ 2^k - 1 < 2^k` for every plain machine `M`. -/ theorem plain_incompressibility_card_le (M : PlainMachine) (n k : ℕ) : (plainCompressible M n k).card ≤ 2 ^ k - 1 := by classical -- All binary strings of length `< k` let P : Finset (List Bool) := (Finset.range k).biUnion binWords have hP_card : P.card = 2 ^ k - 1 := by rw [Finset.card_biUnion] · have hsum : ∑ j ∈ Finset.range k, (binWords j).card = ∑ j ∈ Finset.range k, 2 ^ j := Finset.sum_congr rfl fun j _ => card_binWords j rw [hsum] have hgeom : ∀ m : ℕ, ∑ j ∈ Finset.range m, 2 ^ j = 2 ^ m - 1 := by intro m induction m with | zero => simp | succ m ih => rw [Finset.sum_range_succ, ih, pow_succ] have hpos : 1 ≤ 2 ^ m := Nat.one_le_pow m 2 (by norm_num) omega exact hgeom k · intro a _ b _ hab change Disjoint (binWords a) (binWords b) rw [Finset.disjoint_left] intro w hwa hwb exact hab ((mem_binWords.mp hwa).symm.trans (mem_binWords.mp hwb)) -- Choose a witness program of length `< k` for each compressible `x` have hex : ∀ x ∈ plainCompressible M n k, ∃ p ∈ P, M p = some x := by intro x hx have hlt : plainK M x < k := (Finset.mem_filter.mp hx).2 cases k with | zero => simp at hlt | succ k' => have hfin : plainK M x ≠ ⊤ := ne_top_of_lt hlt have hle : plainK M x ≤ k' := by rw [← ENat.natCast_toNat hfin] at hlt ⊢ have hlt_nat : (plainK M x).toNat < k' + 1 := by exact_mod_cast hlt exact_mod_cast (by omega : (plainK M x).toNat ≤ k') obtain ⟨p, hp, hplen⟩ := exists_program_of_plainK_le hle refine ⟨p, Finset.mem_biUnion.mpr ⟨p.length, Finset.mem_range.mpr (by omega), mem_binWords.mpr rfl⟩, hp⟩ choose f hfP hfM using hex have hle_card : (plainCompressible M n k).card ≤ P.card := by let g : List Bool → List Bool := fun x => if hx : x ∈ plainCompressible M n k then f x hx else [] refine Finset.card_le_card_of_injOn g (fun x hx => by have hx' : x ∈ plainCompressible M n k := hx change (if hx : x ∈ plainCompressible M n k then f x hx else []) ∈ P rw [dif_pos hx']; exact hfP x hx') ?_ intro x hx y hy hxy have hx' : x ∈ plainCompressible M n k := hx have hy' : y ∈ plainCompressible M n k := hy change (if h : x ∈ plainCompressible M n k then f x h else []) = (if h : y ∈ plainCompressible M n k then f y h else []) at hxy rw [dif_pos hx', dif_pos hy'] at hxy have h1 := hfM x hx' have h2 := hfM y hy' rw [hxy] at h1 exact Option.some.inj (h1.symm.trans h2) omega /-- **Exact Sharpness Witness for Plain Counting**: for every `n` and `k ≤ n`, there exists a plain machine achieving `|{x ∈ {0,1}^n : C_M(x) < k}| = 2^k - 1`. -/ theorem plain_incompressibility_sharp_witness (n k : ℕ) (hk : k ≤ n) : ∃ M : PlainMachine, (plainCompressible M n k).card = 2 ^ k - 1 := by classical -- Map each short string `p` (`|p| < k`) injectively to a distinct length-`n` string via -- `p ↦ List.replicate (n - 1 - |p|) false ++ [true] ++ p` (unique leading-1 position!). let enc : List Bool → List Bool := fun p => List.replicate (n - 1 - p.length) false ++ (true :: p) let M : PlainMachine := fun p => if p.length < k then some (enc p) else none refine ⟨M, le_antisymm (plain_incompressibility_card_le M n k) ?_⟩ let P : Finset (List Bool) := (Finset.range k).biUnion binWords have hP_card : P.card = 2 ^ k - 1 := by rw [Finset.card_biUnion] · have hsum : ∑ j ∈ Finset.range k, (binWords j).card = ∑ j ∈ Finset.range k, 2 ^ j := Finset.sum_congr rfl fun j _ => card_binWords j rw [hsum] have hgeom : ∀ m : ℕ, ∑ j ∈ Finset.range m, 2 ^ j = 2 ^ m - 1 := by intro m; induction m with | zero => simp | succ m ih => rw [Finset.sum_range_succ, ih, pow_succ] have : 1 ≤ 2 ^ m := Nat.one_le_pow m 2 (by norm_num); omega exact hgeom k · intro a _ b _ hab change Disjoint (binWords a) (binWords b) rw [Finset.disjoint_left] intro w hwa hwb exact hab ((mem_binWords.mp hwa).symm.trans (mem_binWords.mp hwb)) have hinj : Set.InjOn enc (P : Set (List Bool)) := by intro p1 hp1 p2 hp2 heq have h1 : p1.length < k := by rcases Finset.mem_biUnion.mp hp1 with ⟨j, hj, hmem⟩ rw [mem_binWords.mp hmem]; exact Finset.mem_range.mp hj have h2 : p2.length < k := by rcases Finset.mem_biUnion.mp hp2 with ⟨j, hj, hmem⟩ rw [mem_binWords.mp hmem]; exact Finset.mem_range.mp hj -- Count leading `false`s (first `true` is at index `n - 1 - |p|`) have hrep : ∀ a b : ℕ, ∀ u v : List Bool, List.replicate a false ++ (true :: u) = List.replicate b false ++ (true :: v) → a = b ∧ u = v := by intro a induction a with | zero => intro b u v h cases b with | zero => simpa using h | succ b' => simp [List.replicate_succ] at h | succ a' ih => intro b u v h cases b with | zero => simp [List.replicate_succ] at h | succ b' => simp only [List.replicate_succ, List.cons_append, List.cons.injEq] at h obtain ⟨rfl, rfl⟩ := ih b' u v h.2 exact ⟨rfl, rfl⟩ exact (hrep _ _ _ _ heq).2 have hmaps : Set.MapsTo enc (P : Set (List Bool)) (plainCompressible M n k : Set (List Bool)) := by intro p hp rcases Finset.mem_biUnion.mp hp with ⟨j, hj, hmem⟩ have hpj : p.length = j := mem_binWords.mp hmem have hpk : p.length < k := hpj ▸ Finset.mem_range.mp hj rw [Finset.mem_coe, plainCompressible, Finset.mem_filter] refine ⟨mem_binWords.mpr ?_, ?_⟩ · simp only [enc, List.length_append, List.length_replicate, List.length_cons] omega · have hrun : M p = some (enc p) := by simp [M, hpk] exact lt_of_le_of_lt (plainK_le_of_eq hrun) (by exact_mod_cast hpk) rw [← hP_card] exact Finset.card_le_card_of_injOn enc hmaps hinj /-- **Sharper Exact Prefix-Free Incompressibility Counting Bound** (factor-2 tighter than plain!): for every prefix machine `U`, `n`, and `k ≥ 1`, `|{x ∈ {0,1}^n : K_U(x) < k}| ≤ 2^(k - 1)` (strictly `< 2^k - 1` whenever `k ≥ 2`). -/ theorem prefix_incompressibility_card_le (U : PrefixMachine) (n k : ℕ) (hk : 1 ≤ k) : (prefixCompressible U n k).card ≤ 2 ^ (k - 1) := by classical have hex : ∀ x ∈ prefixCompressible U n k, ∃ p : List Bool, U.run p = some x ∧ p.length ≤ k - 1 := by intro x hx have hlt : prefixK U x < k := (Finset.mem_filter.mp hx).2 have hfin : prefixK U x ≠ ⊤ := ne_top_of_lt hlt have hle : prefixK U x ≤ ((k - 1 : ℕ) : ℕ∞) := by rw [← ENat.natCast_toNat hfin] at hlt ⊢ have hlt_nat : (prefixK U x).toNat < k := by exact_mod_cast hlt exact_mod_cast (by omega : (prefixK U x).toNat ≤ k - 1) exact exists_program_of_prefixK_le hle choose f hfU hflen using hex let S : Finset (List Bool) := (prefixCompressible U n k).attach.image (fun x => f x.1 x.2) have hS_pf : IsPrefixFree (S : Set (List Bool)) := by intro p1 hp1 p2 hp2 hpre rcases Finset.mem_image.mp hp1 with ⟨x1, _, rfl⟩ rcases Finset.mem_image.mp hp2 with ⟨x2, _, rfl⟩ exact U.prefix_free _ (by simp [hfU x1.1 x1.2]) _ (by simp [hfU x2.1 x2.2]) hpre have hS_len : ∀ p ∈ S, p.length ≤ k - 1 := by intro p hp rcases Finset.mem_image.mp hp with ⟨x, _, rfl⟩ exact hflen x.1 x.2 have hS_card : S.card = (prefixCompressible U n k).card := by rw [Finset.card_image_of_injective] · exact Finset.card_attach · intro x1 x2 heq dsimp only at heq have h1 := hfU x1.1 x1.2 have h2 := hfU x2.1 x2.2 rw [heq] at h1 exact Subtype.ext (Option.some.inj (h1.symm.trans h2)) have hkraft := kraft_nat_exact hS_pf hS_len have hge : S.card ≤ ∑ p ∈ S, 2 ^ ((k - 1) - p.length) := by calc S.card = ∑ _p ∈ S, 1 := by simp _ ≤ ∑ p ∈ S, 2 ^ ((k - 1) - p.length) := Finset.sum_le_sum fun p _ => Nat.one_le_pow _ 2 (by norm_num) omega /-- **Exact Sharpness Witness for Prefix-Free Counting**: for every `1 ≤ k ≤ n + 1`, there exists a prefix-free machine achieving exact equality `|{x ∈ {0,1}^n : K_U(x) < k}| = 2^(k - 1)`. -/ theorem prefix_incompressibility_sharp_witness (n k : ℕ) (hk1 : 1 ≤ k) (hkn : k - 1 ≤ n) : ∃ U : PrefixMachine, (prefixCompressible U n k).card = 2 ^ (k - 1) := by classical let pad : List Bool → List Bool := fun p => p ++ List.replicate (n - (k - 1)) false let U : PrefixMachine := { run := fun p => if p.length = k - 1 then some (pad p) else none prefix_free := by intro p1 hp1 p2 hp2 hpre change ((if p1.length = k - 1 then some (pad p1) else none).isSome = true) at hp1 change ((if p2.length = k - 1 then some (pad p2) else none).isSome = true) at hp2 have h1 : p1.length = k - 1 := by by_contra hc; simp [hc] at hp1 have h2 : p2.length = k - 1 := by by_contra hc; simp [hc] at hp2 rcases hpre with ⟨suf, rfl⟩ have hsuf : suf = [] := by simp only [List.length_append] at h2 exact List.eq_nil_of_length_eq_zero (by omega) simp [hsuf] } refine ⟨U, le_antisymm (prefix_incompressibility_card_le U n k hk1) ?_⟩ have hmaps : Set.MapsTo pad (binWords (k - 1) : Set (List Bool)) (prefixCompressible U n k : Set (List Bool)) := by intro p hp have hplen : p.length = k - 1 := mem_binWords.mp hp rw [Finset.mem_coe, prefixCompressible, Finset.mem_filter] refine ⟨mem_binWords.mpr ?_, ?_⟩ · simp only [pad, List.length_append, List.length_replicate, hplen] omega · have hrun : U.run p = some (pad p) := by simp [U, hplen] exact lt_of_le_of_lt (prefixK_le_of_eq hrun) (by exact_mod_cast (by omega : p.length < k)) have hinj : Set.InjOn pad (binWords (k - 1) : Set (List Bool)) := by intro p1 _ p2 _ heq exact List.append_cancel_right heq rw [← card_binWords (k - 1)] exact Finset.card_le_card_of_injOn pad hmaps hinj /-- **Universal Non-Asymptotic Prefix-vs-Plain Gap Theorem**: For the identity plain machine (`C_id(x) = |x|`), EVERY prefix-free machine `U` and EVERY horizon `N ≥ 1` has a string `x` of length `n < N` with `K_U(x) - C_id(x) ≥ Nat.clog 2 N` (equivalently, `K_U(x) ≥ |x| + Nat.clog 2 N`, even though `C_id(x) = |x|`). -/ theorem universal_prefix_vs_plain_gap (U : PrefixMachine) {N : ℕ} (hN : 1 ≤ N) : ∃ x : List Bool, x.length < N ∧ (x.length + Nat.clog 2 N : ℕ∞) ≤ prefixK U x ∧ plainK some x = x.length := by classical -- Note `plainK some x = x.length` for every `x` have hplain : ∀ x : List Bool, plainK some x = x.length := by intro x apply le_antisymm (plainK_le_of_eq (M := some) rfl) refine le_iInf₂ fun p hp => ?_ have : p = x := Option.some.inj hp rw [this] by_contra hall push Not at hall -- Suppose every string of length `n < N` has `prefixK U x < n + clog 2 N` have hlt_all : ∀ x : List Bool, x.length < N → prefixK U x < (x.length + Nat.clog 2 N : ℕ∞) := by intro x hx by_contra hge exact hall x hx (not_lt.mp hge) (hplain x) cases hc : Nat.clog 2 N with | zero => -- If `clog 2 N = 0`, then `N = 1` and `prefixK U [] < 0`, impossible! have h0 := hlt_all [] (by simp; omega) simp [hc] at h0 | succ c => -- For every `x` of length `< N`, `prefixK U x ≤ |x| + c` let X : Finset (List Bool) := (Finset.range N).biUnion binWords have hex : ∀ x ∈ X, ∃ p : List Bool, U.run p = some x ∧ p.length ≤ x.length + c := by intro x hx rcases Finset.mem_biUnion.mp hx with ⟨n, hn, hmem⟩ have hxn : x.length = n := mem_binWords.mp hmem have hlt := hlt_all x (hxn ▸ Finset.mem_range.mp hn) rw [hc] at hlt have hfin : prefixK U x ≠ ⊤ := ne_top_of_lt hlt have hle : prefixK U x ≤ ((x.length + c : ℕ) : ℕ∞) := by rw [← ENat.natCast_toNat hfin] at hlt ⊢ have hlt_nat : (prefixK U x).toNat < x.length + (c + 1) := by exact_mod_cast hlt exact_mod_cast (by omega : (prefixK U x).toNat ≤ x.length + c) exact exists_program_of_prefixK_le hle choose f hfU hflen using hex let S : Finset (List Bool) := X.attach.image (fun x => f x.1 x.2) have hS_pf : IsPrefixFree (S : Set (List Bool)) := by intro p1 hp1 p2 hp2 hpre rcases Finset.mem_image.mp hp1 with ⟨x1, _, rfl⟩ rcases Finset.mem_image.mp hp2 with ⟨x2, _, rfl⟩ exact U.prefix_free _ (by simp [hfU x1.1 x1.2]) _ (by simp [hfU x2.1 x2.2]) hpre have hinj : Function.Injective (fun x : {x // x ∈ X} => f x.1 x.2) := by intro x1 x2 heq dsimp only at heq have h1 := hfU x1.1 x1.2 have h2 := hfU x2.1 x2.2 rw [heq] at h1 exact Subtype.ext (Option.some.inj (h1.symm.trans h2)) have hkraft := kraft_finset_real hS_pf have hsum_eq : ∑ p ∈ S, (2 : ℝ)⁻¹ ^ p.length = ∑ x ∈ X.attach, (2 : ℝ)⁻¹ ^ (f x.1 x.2).length := Finset.sum_image (fun a _ b _ h => hinj h) have hsum_ge : ∑ x ∈ X.attach, (2 : ℝ)⁻¹ ^ (x.1.length + c) ≤ ∑ x ∈ X.attach, (2 : ℝ)⁻¹ ^ (f x.1 x.2).length := by refine Finset.sum_le_sum fun x _ => ?_ exact pow_le_pow_of_le_one (by positivity) (by norm_num) (hflen x.1 x.2) have hsum_eval : ∑ x ∈ X.attach, (2 : ℝ)⁻¹ ^ (x.1.length + c) = (N : ℝ) * (2 : ℝ)⁻¹ ^ c := by rw [Finset.sum_attach X (fun x => (2 : ℝ)⁻¹ ^ (x.length + c))] rw [Finset.sum_biUnion] · calc ∑ n ∈ Finset.range N, ∑ x ∈ binWords n, (2 : ℝ)⁻¹ ^ (x.length + c) = ∑ n ∈ Finset.range N, ∑ _x ∈ binWords n, (2 : ℝ)⁻¹ ^ (n + c) := by refine Finset.sum_congr rfl fun n _ => Finset.sum_congr rfl fun x hx => ?_ rw [mem_binWords.mp hx] _ = ∑ n ∈ Finset.range N, (2 : ℝ) ^ n * (2 : ℝ)⁻¹ ^ (n + c) := by refine Finset.sum_congr rfl fun n _ => ?_ simp [card_binWords] _ = ∑ _n ∈ Finset.range N, (2 : ℝ)⁻¹ ^ c := by refine Finset.sum_congr rfl fun n _ => ?_ rw [pow_add, inv_pow, ← mul_assoc] field_simp _ = (N : ℝ) * (2 : ℝ)⁻¹ ^ c := by simp · intro a _ b _ hab change Disjoint (binWords a) (binWords b) rw [Finset.disjoint_left] intro w hwa hwb exact hab ((mem_binWords.mp hwa).symm.trans (mem_binWords.mp hwb)) have hN_le : (N : ℝ) ≤ (2 : ℝ) ^ c := by have h1 : (N : ℝ) * (2 : ℝ)⁻¹ ^ c ≤ 1 := by linarith rw [inv_pow] at h1 have hpos : (0 : ℝ) < (2 : ℝ) ^ c := by positivity have h2 : (N : ℝ) / (2 : ℝ) ^ c ≤ 1 := by simpa [div_eq_mul_inv] using h1 exact (div_le_one hpos).mp h2 have hN_le_nat : N ≤ 2 ^ c := by exact_mod_cast hN_le have hclog_le : Nat.clog 2 N ≤ c := Nat.clog_le_iff_le_pow (by norm_num) |>.mpr hN_le_nat omega /-! ## Section 5: Chaitin's Halting Probability `Ω_U`, Halting Capture & Randomness Deficit -/ /-- The halting domain of prefix machine `U`. -/ def HaltingDom (U : PrefixMachine) : Set (List Bool) := {p : List Bool | (U.run p).isSome} /-- Halting programs of length `≤ L`. -/ noncomputable def haltingUpTo (U : PrefixMachine) (L : ℕ) : Finset (List Bool) := by classical exact ((Finset.range (L + 1)).biUnion binWords).filter (fun p => (U.run p).isSome) /-- `L`-horizon partial sum `Ω_U^{(L)} = ∑_{|p| ≤ L, U(p)↓} 2^{-|p|}`. -/ noncomputable def omegaPartial (U : PrefixMachine) (L : ℕ) : ℝ := ∑ p ∈ haltingUpTo U L, (2 : ℝ)⁻¹ ^ p.length /-- Chaitin's halting probability `Ω_U = ∑'_{p ∈ HaltingDom U} 2^{-|p|}`. -/ noncomputable def chaitinOmega (U : PrefixMachine) : ℝ := ∑' p : HaltingDom U, (2 : ℝ)⁻¹ ^ p.1.length lemma chaitinOmega_summable (U : PrefixMachine) : Summable (fun p : HaltingDom U => (2 : ℝ)⁻¹ ^ p.1.length) := kraft_countable_summable U.prefix_free theorem chaitinOmega_nonneg (U : PrefixMachine) : 0 ≤ chaitinOmega U := tsum_nonneg fun _ => by positivity theorem chaitinOmega_le_one (U : PrefixMachine) : chaitinOmega U ≤ 1 := kraft_countable_tsum_le_one U.prefix_free theorem omegaPartial_mono (U : PrefixMachine) : Monotone (omegaPartial U) := by classical intro L1 L2 hL refine Finset.sum_le_sum_of_subset_of_nonneg ?_ (fun _ _ _ => by positivity) intro p hp simp only [haltingUpTo, Finset.mem_filter, Finset.mem_biUnion, Finset.mem_range] at hp ⊢ rcases hp with ⟨⟨m, hm, hmem⟩, hrun⟩ exact ⟨⟨m, by omega, hmem⟩, hrun⟩ lemma finset_halting_sum_le_chaitinOmega (U : PrefixMachine) {F : Finset (List Bool)} (hF : ∀ p ∈ F, (U.run p).isSome) : ∑ p ∈ F, (2 : ℝ)⁻¹ ^ p.length ≤ chaitinOmega U := by classical let g : {p // p ∈ F} → HaltingDom U := fun p => ⟨p.1, hF p.1 p.2⟩ let F' : Finset (HaltingDom U) := F.attach.image g have hsum : ∑ p ∈ F, (2 : ℝ)⁻¹ ^ p.length = ∑ q ∈ F', (2 : ℝ)⁻¹ ^ q.1.length := by rw [Finset.sum_image] · exact (Finset.sum_attach F (fun p => (2 : ℝ)⁻¹ ^ p.length)).symm · intro a _ b _ h have hval : (g a).1 = (g b).1 := congrArg Subtype.val h exact Subtype.ext hval rw [hsum] exact (chaitinOmega_summable U).sum_le_tsum F' (fun _ _ => by positivity) theorem omegaPartial_le_chaitinOmega (U : PrefixMachine) (L : ℕ) : omegaPartial U L ≤ chaitinOmega U := finset_halting_sum_le_chaitinOmega U (fun _ hp => (Finset.mem_filter.mp hp).2) /-- **Monotone Convergence of Chaitin Partial Sums**: `lim_{L → ∞} Ω_U^{(L)} = Ω_U`. -/ theorem tendsto_omegaPartial_chaitinOmega (U : PrefixMachine) : Tendsto (omegaPartial U) atTop (nhds (chaitinOmega U)) := by classical have hsum := chaitinOmega_summable U rw [Metric.tendsto_atTop] intro ε hε obtain ⟨F, hF⟩ := Metric.tendsto_atTop.mp hsum.hasSum ε hε set L0 := F.sup (fun p : HaltingDom U => p.1.length) refine ⟨L0, fun L hL => ?_⟩ rw [Real.dist_eq, abs_of_nonpos (sub_nonpos.mpr (omegaPartial_le_chaitinOmega U L)), neg_sub] -- `F` embeds into `haltingUpTo U L` let F_img : Finset (List Bool) := F.image Subtype.val have hsub : F_img ⊆ haltingUpTo U L := by intro p hp rcases Finset.mem_image.mp hp with ⟨q, hqF, rfl⟩ have hqlen : q.1.length ≤ L := le_trans (Finset.le_sup (f := fun r : HaltingDom U => r.1.length) hqF) hL simp only [haltingUpTo, Finset.mem_filter, Finset.mem_biUnion, Finset.mem_range] exact ⟨⟨q.1.length, by omega, mem_binWords.mpr rfl⟩, q.2⟩ have hF_le : ∑ q ∈ F, (2 : ℝ)⁻¹ ^ q.1.length ≤ omegaPartial U L := by calc ∑ q ∈ F, (2 : ℝ)⁻¹ ^ q.1.length = ∑ p ∈ F_img, (2 : ℝ)⁻¹ ^ p.length := by rw [Finset.sum_image] intro a _ b _ h; exact Subtype.ext h _ ≤ omegaPartial U L := Finset.sum_le_sum_of_subset_of_nonneg hsub (fun _ _ _ => by positivity) have hdist := hF F Subset.rfl rw [Real.dist_eq, abs_of_nonpos (sub_nonpos.mpr (hsum.sum_le_tsum F (fun _ _ => by positivity))), neg_sub] at hdist change chaitinOmega U - ∑ i ∈ F, (2 : ℝ)⁻¹ ^ i.1.length < ε at hdist linarith /-- **Exact Chaitin Halting-Capture Theorem**: if a finite subset `F ⊆ HaltingDom U` captures strictly more Kraft mass than `Ω_U - 2^{-n}` (in particular, once `∑_{q ∈ F} 2^{-|q|} ≥ ω_n`, where `ω_n = ⌊2^n Ω_U⌋ / 2^n` is the `n`-bit binary truncation of `Ω_U`), then `F` MUST contain **EVERY** halting program of length `≤ n`. -/ theorem chaitin_omega_halting_capture (U : PrefixMachine) (n : ℕ) {F : Finset (List Bool)} (hF : ∀ q ∈ F, (U.run q).isSome) (hmass : chaitinOmega U - (2 : ℝ)⁻¹ ^ n < ∑ q ∈ F, (2 : ℝ)⁻¹ ^ q.length) : ∀ p : List Bool, (U.run p).isSome → p.length ≤ n → p ∈ F := by classical intro p hp hlen by_contra hpF have hF' : ∀ q ∈ insert p F, (U.run q).isSome := by intro q hq rcases Finset.mem_insert.mp hq with rfl | hqF · exact hp · exact hF q hqF have hle := finset_halting_sum_le_chaitinOmega U hF' rw [Finset.sum_insert hpF] at hle have hpow : (2 : ℝ)⁻¹ ^ n ≤ (2 : ℝ)⁻¹ ^ p.length := pow_le_pow_of_le_one (by positivity) (by norm_num) hlen linarith /-- Exact `n`-bit dyadic truncation of `Ω_U`: `⌊2^n Ω_U⌋ / 2^n`, satisfying `Ω_U - 2^{-n} < ω_n ≤ Ω_U`. -/ noncomputable def omegaTrunc (U : PrefixMachine) (n : ℕ) : ℝ := (⌊(2 : ℝ) ^ n * chaitinOmega U⌋ : ℝ) / (2 : ℝ) ^ n lemma omegaTrunc_bounds (U : PrefixMachine) (n : ℕ) : chaitinOmega U - (2 : ℝ)⁻¹ ^ n < omegaTrunc U n ∧ omegaTrunc U n ≤ chaitinOmega U := by have hpos : (0 : ℝ) < (2 : ℝ) ^ n := by positivity have hfl1 : (⌊(2 : ℝ) ^ n * chaitinOmega U⌋ : ℝ) ≤ (2 : ℝ) ^ n * chaitinOmega U := Int.floor_le _ have hfl2 : (2 : ℝ) ^ n * chaitinOmega U - 1 < (⌊(2 : ℝ) ^ n * chaitinOmega U⌋ : ℝ) := Int.sub_one_lt_floor _ constructor · rw [omegaTrunc, inv_pow, lt_div_iff₀ hpos] have hmul : (chaitinOmega U - ((2 : ℝ) ^ n)⁻¹) * (2 : ℝ) ^ n = (2 : ℝ) ^ n * chaitinOmega U - 1 := by field_simp rw [hmul] exact hfl2 · rw [omegaTrunc, div_le_iff₀ hpos] linarith /-- **Exact Chaitin Prefix-Incompressibility Lower Bound**: Given any prefix-free decoder `D` that, from a program for the `n`-bit prefix `ω_n` of `Ω_U`, enumerates a halting subset `F(ω_n)` with mass `≥ ω_n` and outputs any string `y_n` absent from the outputs of `F(ω_n)`, any universal machine `U` simulating `D` with additive overhead `c_D` satisfies `K_U(ω_n) ≥ n + 1 - c_D` (written in `ℕ∞` without subtraction: `n + 1 ≤ K_U(ω_n) + c_D`). -/ theorem chaitin_omega_incompressibility (U : PrefixMachine) (n cD : ℕ) (omegaPrefixCode y_n : List Bool) {F : Finset (List Bool)} (hF : ∀ q ∈ F, (U.run q).isSome) (hmass : omegaTrunc U n ≤ ∑ q ∈ F, (2 : ℝ)⁻¹ ^ q.length) (havoid : ∀ q ∈ F, U.run q ≠ some y_n) (hsim : prefixK U y_n ≤ prefixK U omegaPrefixCode + cD) : ((n + 1 : ℕ) : ℕ∞) ≤ prefixK U omegaPrefixCode + cD := by have hcap := chaitin_omega_halting_capture U n hF (lt_of_lt_of_le (omegaTrunc_bounds U n).1 hmass) have hy_gt : ((n + 1 : ℕ) : ℕ∞) ≤ prefixK U y_n := by refine le_iInf₂ fun p hp => ?_ by_contra hlt have hlen : p.length ≤ n := by have : p.length < n + 1 := by exact_mod_cast (not_le.mp hlt) omega have hpF := hcap p (by simp [hp]) hlen exact havoid p hpF hp exact le_trans hy_gt hsim /-- **Exact Finite-Horizon Algorithmic Randomness Deficit & Average Complexity Lower Bound**: If every `x ∈ {0,1}^n` has a finite shortest prefix-free program of length `k(x) = K_U(x)`, then: 1. Exact Kraft-Test Sum: `(1 / 2^n) ∑_{x ∈ {0,1}^n} 2^(n - k(x)) ≤ Ω_U ≤ 1`. 2. Exact Exponential Deficit Tail: `|{x ∈ {0,1}^n : n - k(x) ≥ d}| / 2^n ≤ Ω_U * 2^(-d) ≤ 2^(-d)`. 3. **Exact Jensen Average Prefix-Complexity Lower Bound** (sharper than `n` by `log₂(1/Ω_U)`!): `(1 / 2^n) ∑_{x ∈ {0,1}^n} k(x) ≥ n + log₂(1 / Ω_U) ≥ n` (whenever `Ω_U > 0`). -/ theorem algorithmic_randomness_deficit_bounds (U : PrefixMachine) (n : ℕ) (k : List Bool → ℕ) (hk : ∀ x ∈ binWords n, prefixK U x = k x) : (∑ x ∈ binWords n, (2 : ℝ)⁻¹ ^ k x ≤ chaitinOmega U) ∧ (∀ d : ℕ, ((binWords n).filter (fun x => k x + d ≤ n)).card ≤ chaitinOmega U * (2 : ℝ) ^ (n - d)) ∧ (0 < chaitinOmega U → (n : ℝ) + Real.logb 2 (1 / chaitinOmega U) ≤ ((2 : ℝ) ^ n)⁻¹ * ∑ x ∈ binWords n, (k x : ℝ)) := by classical have hex : ∀ x ∈ binWords n, ∃ p : List Bool, U.run p = some x ∧ p.length ≤ k x := by intro x hx exact exists_program_of_prefixK_le (le_of_eq (hk x hx)) choose f hfU hflen using hex let g : {x // x ∈ binWords n} → List Bool := fun x => f x.1 x.2 let S : Finset (List Bool) := (binWords n).attach.image g have hS_halt : ∀ p ∈ S, (U.run p).isSome := by intro p hp rcases Finset.mem_image.mp hp with ⟨x, _, rfl⟩ simp [g, hfU x.1 x.2] have hinj : Function.Injective g := by intro x1 x2 heq have h1 := hfU x1.1 x1.2 have h2 := hfU x2.1 x2.2 change f x1.1 x1.2 = f x2.1 x2.2 at heq rw [heq] at h1 exact Subtype.ext (Option.some.inj (h1.symm.trans h2)) have hpart1 : ∑ x ∈ binWords n, (2 : ℝ)⁻¹ ^ k x ≤ chaitinOmega U := by calc ∑ x ∈ binWords n, (2 : ℝ)⁻¹ ^ k x = ∑ x ∈ (binWords n).attach, (2 : ℝ)⁻¹ ^ k x.1 := (Finset.sum_attach (binWords n) (fun x => (2 : ℝ)⁻¹ ^ k x)).symm _ ≤ ∑ x ∈ (binWords n).attach, (2 : ℝ)⁻¹ ^ (g x).length := Finset.sum_le_sum fun x _ => pow_le_pow_of_le_one (by positivity) (by norm_num) (hflen x.1 x.2) _ = ∑ p ∈ S, (2 : ℝ)⁻¹ ^ p.length := (Finset.sum_image (f := fun p : List Bool => (2 : ℝ)⁻¹ ^ p.length) (fun a _ b _ h => hinj h)).symm _ ≤ chaitinOmega U := finset_halting_sum_le_chaitinOmega U hS_halt refine ⟨hpart1, ?_, ?_⟩ · -- Part 2: Exact exponential deficit tail intro d set A := (binWords n).filter (fun x => k x + d ≤ n) have hA_sub : A ⊆ binWords n := Finset.filter_subset _ _ have hsum_A : ∑ x ∈ A, (2 : ℝ)⁻¹ ^ k x ≤ chaitinOmega U := le_trans (Finset.sum_le_sum_of_subset_of_nonneg hA_sub (fun _ _ _ => by positivity)) hpart1 have hlow : (A.card : ℝ) * (2 : ℝ)⁻¹ ^ (n - d) ≤ ∑ x ∈ A, (2 : ℝ)⁻¹ ^ k x := by calc (A.card : ℝ) * (2 : ℝ)⁻¹ ^ (n - d) = ∑ _x ∈ A, (2 : ℝ)⁻¹ ^ (n - d) := by simp _ ≤ ∑ x ∈ A, (2 : ℝ)⁻¹ ^ k x := by refine Finset.sum_le_sum fun x hx => ?_ have hkd : k x + d ≤ n := (Finset.mem_filter.mp hx).2 exact pow_le_pow_of_le_one (by positivity) (by norm_num) (by omega) have hpos : (0 : ℝ) < (2 : ℝ) ^ (n - d) := by positivity have h1 : (A.card : ℝ) * (2 : ℝ)⁻¹ ^ (n - d) ≤ chaitinOmega U := le_trans hlow hsum_A rw [inv_pow] at h1 have h2 : (A.card : ℝ) ≤ chaitinOmega U * (2 : ℝ) ^ (n - d) := by have := mul_le_mul_of_nonneg_right h1 (le_of_lt hpos) field_simp at this linarith exact h2 · -- Part 3: Exact Jensen average complexity lower bound intro hΩpos have hlog2 : 0 < Real.log 2 := Real.log_pos (by norm_num) have hcard : ((binWords n).card : ℝ) = (2 : ℝ) ^ n := by exact_mod_cast card_binWords n have hpos2n : (0 : ℝ) < (2 : ℝ) ^ n := by positivity -- Apply Jensen's inequality to convex `Real.exp` with uniform weights `1 / 2^n` let w : List Bool → ℝ := fun _ => ((2 : ℝ) ^ n)⁻¹ have hw_nonneg : ∀ x ∈ binWords n, 0 ≤ w x := fun _ _ => by positivity have hw_sum : ∑ x ∈ binWords n, w x = 1 := by simp only [w, Finset.sum_const, nsmul_eq_mul, hcard] field_simp have hjensen := ConvexOn.map_sum_le convexOn_exp hw_nonneg hw_sum (p := fun x => -(k x : ℝ) * Real.log 2) (fun _ _ => Set.mem_univ _) simp only [smul_eq_mul] at hjensen have hexp_eq : ∀ x : List Bool, Real.exp (-(k x : ℝ) * Real.log 2) = (2 : ℝ)⁻¹ ^ k x := by intro x rw [neg_mul, Real.exp_neg, Real.exp_nat_mul, Real.exp_log (by norm_num : (0 : ℝ) < 2), inv_pow] have hrhs_le : ∑ x ∈ binWords n, w x * Real.exp (-(k x : ℝ) * Real.log 2) ≤ ((2 : ℝ) ^ n)⁻¹ * chaitinOmega U := by calc ∑ x ∈ binWords n, w x * Real.exp (-(k x : ℝ) * Real.log 2) = ((2 : ℝ) ^ n)⁻¹ * ∑ x ∈ binWords n, (2 : ℝ)⁻¹ ^ k x := by rw [Finset.mul_sum] refine Finset.sum_congr rfl fun x _ => by rw [hexp_eq x] _ ≤ ((2 : ℝ) ^ n)⁻¹ * chaitinOmega U := mul_le_mul_of_nonneg_left hpart1 (by positivity) have hcomb := le_trans hjensen hrhs_le have hlog_le := Real.log_le_log (Real.exp_pos _) hcomb rw [Real.log_exp, Real.log_mul (by positivity) (ne_of_gt hΩpos), Real.log_inv, Real.log_pow] at hlog_le have hlhs_simp : ∑ x ∈ binWords n, w x * (-(k x : ℝ) * Real.log 2) = -(((2 : ℝ) ^ n)⁻¹ * ∑ x ∈ binWords n, (k x : ℝ)) * Real.log 2 := by simp only [w] have hterm : ∀ x ∈ binWords n, ((2 : ℝ) ^ n)⁻¹ * (-(k x : ℝ) * Real.log 2) = (-(((2 : ℝ) ^ n)⁻¹ * Real.log 2)) * (k x : ℝ) := fun x _ => by ring rw [Finset.sum_congr rfl hterm, ← Finset.mul_sum] ring rw [hlhs_simp] at hlog_le rw [Real.logb, one_div, Real.log_inv] rw [le_iff_lt_or_eq] have hfinal : ((n : ℝ) + -Real.log (chaitinOmega U) / Real.log 2) * Real.log 2 ≤ (((2 : ℝ) ^ n)⁻¹ * ∑ x ∈ binWords n, (k x : ℝ)) * Real.log 2 := by have hdiv : (-Real.log (chaitinOmega U) / Real.log 2) * Real.log 2 = -Real.log (chaitinOmega U) := div_mul_cancel₀ _ (ne_of_gt hlog2) nlinarith have := le_of_mul_le_mul_right hfinal hlog2 exact le_iff_lt_or_eq.mp this /-! ## Section 6: Axiom Audit -/ #print axioms kraft_nat_exact #print axioms kraft_nat_eq_iff_complete #print axioms kraft_finset_real #print axioms kraft_countable_summable #print axioms kraft_countable_tsum_le_one #print axioms isPrefixFree_uniquelyDecodable #print axioms mcmillan_countable_tsum_le_one #print axioms kraft_converse_constructive #print axioms eliasDelim_prefixFree #print axioms eliasIdentityMachine_prefixK_le #print axioms universal_prefix_invariance #print axioms plain_incompressibility_card_le #print axioms plain_incompressibility_sharp_witness #print axioms prefix_incompressibility_card_le #print axioms prefix_incompressibility_sharp_witness #print axioms universal_prefix_vs_plain_gap #print axioms chaitinOmega_nonneg #print axioms chaitinOmega_le_one #print axioms omegaPartial_mono #print axioms omegaPartial_le_chaitinOmega #print axioms tendsto_omegaPartial_chaitinOmega #print axioms chaitin_omega_halting_capture #print axioms chaitin_omega_incompressibility #print axioms algorithmic_randomness_deficit_bounds end AlgorithmicInformation