omega-principia
This project has not described itself yet.
Brought by drakestapleton
omega-principia / proofs / CoveringArrayStructuralW2.lean
| 1 | /- |
| 2 | SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | experiments/math_8h_comb/lean/CoveringArrayStructuralW2.lean |
| 4 | |
| 5 | General structural theorems for Covering Arrays of arbitrary strength t, |
| 6 | column count k, and alphabet size v (Lean 4.36.0 core, 0 sorry, 0 native_decide, |
| 7 | standard axioms only): |
| 8 | 1. Redundant-row deletion theorem (the exact mathematical basis of our |
| 9 | progressive row-shrinking post-optimizer: removing any row whose every |
| 10 | t-way interaction is covered by another row preserves t-coverage). |
| 11 | 2. Column-permutation invariance theorem (any injective column map preserves |
| 12 | order-independent strength-t covering array validity). |
| 13 | 3. Symbol-surjection / alphabet-quotient theorem (any family of per-column |
| 14 | surjections Fin v -> Fin w maps a CA(N; t, k, v) to a CA(N; t, k, w)). |
| 15 | 4. Direct-product (Kronecker) composition theorem (combining CA(N1; t, k, v1) |
| 16 | and CA(N2; t, k, v2) yields CA(N1 * N2; t, k, v1 * v2)). |
| 17 | 5. Four literal broken-model negative controls (`neg_ctrl_*`). |
| 18 | -/ |
| 19 | |
| 20 | namespace CoveringArrayStructuralW2 |
| 21 | |
| 22 | /-- A function `f : Fin t -> Fin k` is strictly increasing (selects `t` distinct columns). -/ |
| 23 | def StrictMonoCols {t k : Nat} (cols : Fin t → Fin k) : Prop := |
| 24 | ∀ i j : Fin t, i.val < j.val → (cols i).val < (cols j).val |
| 25 | |
| 26 | /-- A matrix `M : Fin N -> Fin k -> Fin v` is a strength-`t` covering array if for |
| 27 | every strictly increasing `t`-tuple of columns and every target pattern `pat : Fin t -> Fin v`, |
| 28 | there exists a row `r : Fin N` matching `pat` on `cols`. -/ |
| 29 | def IsCoveringArray (N t k v : Nat) (M : Fin N → Fin k → Fin v) : Prop := |
| 30 | ∀ (cols : Fin t → Fin k), StrictMonoCols cols → |
| 31 | ∀ (pat : Fin t → Fin v), |
| 32 | ∃ r : Fin N, ∀ i : Fin t, M r (cols i) = pat i |
| 33 | |
| 34 | /-- Order-independent strength-`t` covering array property (over all injective column tuples). -/ |
| 35 | def IsCoveringArrayInj (N t k v : Nat) (M : Fin N → Fin k → Fin v) : Prop := |
| 36 | ∀ (cols : Fin t → Fin k), Function.Injective cols → |
| 37 | ∀ (pat : Fin t → Fin v), |
| 38 | ∃ r : Fin N, ∀ i : Fin t, M r (cols i) = pat i |
| 39 | |
| 40 | /-- Helper: any strictly increasing map `Fin t -> Fin k` is injective. -/ |
| 41 | theorem strictMonoCols_injective {t k : Nat} {cols : Fin t → Fin k} |
| 42 | (h : StrictMonoCols cols) : Function.Injective cols := by |
| 43 | intro i j hij |
| 44 | by_cases hlt : i.val < j.val |
| 45 | · have := h i j hlt |
| 46 | rw [hij] at this |
| 47 | exact False.elim (Nat.lt_irrefl _ this) |
| 48 | · by_cases hgt : j.val < i.val |
| 49 | · have := h j i hgt |
| 50 | rw [hij] at this |
| 51 | exact False.elim (Nat.lt_irrefl _ this) |
| 52 | · apply Fin.ext |
| 53 | omega |
| 54 | |
| 55 | /-- Theorem 1 (Redundant-Row Deletion): |
| 56 | If `M` is a `CA(N + 1; t, k, v)` and the last row `r_last` is redundant |
| 57 | (every `t`-way interaction it hits is also hit by some row `r < N`), |
| 58 | then dropping the last row yields a valid `CA(N; t, k, v)`. -/ |
| 59 | theorem ca_drop_redundant_last_row |
| 60 | {N t k v : Nat} (M : Fin (N + 1) → Fin k → Fin v) |
| 61 | (hCA : IsCoveringArray (N + 1) t k v M) |
| 62 | (hRed : ∀ (cols : Fin t → Fin k), StrictMonoCols cols → |
| 63 | ∃ r : Fin N, ∀ i : Fin t, |
| 64 | M ⟨r.val, Nat.lt_succ_of_lt r.isLt⟩ (cols i) = M ⟨N, Nat.lt_succ_self N⟩ (cols i)) : |
| 65 | IsCoveringArray N t k v (fun r c => M ⟨r.val, Nat.lt_succ_of_lt r.isLt⟩ c) := by |
| 66 | intro cols hcols pat |
| 67 | obtain ⟨r, hr⟩ := hCA cols hcols pat |
| 68 | by_cases hlt : r.val < N |
| 69 | · refine ⟨⟨r.val, hlt⟩, ?_⟩ |
| 70 | intro i |
| 71 | exact hr i |
| 72 | · have hEq : r = ⟨N, Nat.lt_succ_self N⟩ := by |
| 73 | apply Fin.ext |
| 74 | have hrlt : r.val < N + 1 := r.isLt |
| 75 | simp only [] |
| 76 | omega |
| 77 | subst hEq |
| 78 | obtain ⟨r', hr'⟩ := hRed cols hcols |
| 79 | refine ⟨r', ?_⟩ |
| 80 | intro i |
| 81 | dsimp only |
| 82 | rw [hr' i, hr i] |
| 83 | |
| 84 | /-- Theorem 2 (Injective Column-Map Pullback / Permutation Invariance): |
| 85 | If `M` is an order-independent `CA(N; t, k, v)` and `σ : Fin k' -> Fin k` is injective |
| 86 | (in particular, any column permutation or column subset projection), |
| 87 | then `fun r c => M r (σ c)` is a `CA(N; t, k', v)`. -/ |
| 88 | theorem ca_pullback_injective_cols |
| 89 | {N t k k' v : Nat} (M : Fin N → Fin k → Fin v) |
| 90 | (hCA : IsCoveringArrayInj N t k v M) |
| 91 | (σ : Fin k' → Fin k) (hσ : Function.Injective σ) : |
| 92 | IsCoveringArrayInj N t k' v (fun r c => M r (σ c)) := by |
| 93 | intro cols hcols pat |
| 94 | have hcomp : Function.Injective (fun i => σ (cols i)) := fun i j h => hcols (hσ h) |
| 95 | exact hCA (fun i => σ (cols i)) hcomp pat |
| 96 | |
| 97 | /-- Theorem 3 (Per-Column Symbol-Surjection / Alphabet Quotient): |
| 98 | If `M` is a `CA(N; t, k, v)` and for each column `c : Fin k`, `φ c : Fin v -> Fin w` |
| 99 | has a right inverse `ψ c : Fin w -> Fin v` (equivalently, `φ c` is surjective), |
| 100 | then `fun r c => φ c (M r c)` is a `CA(N; t, k, w)`. |
| 101 | In particular, per-column symbol permutations (`w = v`) and alphabet fusion (`w < v`) |
| 102 | both preserve strength-`t` coverage with ZERO extra axioms. -/ |
| 103 | theorem ca_map_symbols_surjective |
| 104 | {N t k v w : Nat} (M : Fin N → Fin k → Fin v) |
| 105 | (hCA : IsCoveringArray N t k v M) |
| 106 | (φ : Fin k → Fin v → Fin w) |
| 107 | (ψ : Fin k → Fin w → Fin v) |
| 108 | (hSec : ∀ c : Fin k, ∀ y : Fin w, φ c (ψ c y) = y) : |
| 109 | IsCoveringArray N t k w (fun r c => φ c (M r c)) := by |
| 110 | intro cols hcols pat |
| 111 | let pre : Fin t → Fin v := fun i => ψ (cols i) (pat i) |
| 112 | obtain ⟨r, hr⟩ := hCA cols hcols pre |
| 113 | refine ⟨r, ?_⟩ |
| 114 | intro i |
| 115 | dsimp only |
| 116 | rw [hr i] |
| 117 | exact hSec (cols i) (pat i) |
| 118 | |
| 119 | /-- Helper: `a < A * B -> a / B < A` for `Nat`. -/ |
| 120 | private theorem div_lt_of_lt_mul_right {a A B : Nat} (h : a < A * B) : a / B < A := by |
| 121 | have h' : a < B * A := by rw [Nat.mul_comm]; exact h |
| 122 | exact Nat.div_lt_of_lt_mul h' |
| 123 | |
| 124 | /-- Theorem 4 (Direct-Product / Kronecker Composition): |
| 125 | Given `M1 : CA(N1; t, k, v1)` and `M2 : CA(N2; t, k, v2)` with `v2 > 0, N2 > 0`, |
| 126 | their Kronecker product has `N1 * N2` rows and is a `CA(N1 * N2; t, k, v1 * v2)`. -/ |
| 127 | theorem ca_kronecker_product |
| 128 | {N1 N2 t k v1 v2 : Nat} (hv2 : 0 < v2) (hN2 : 0 < N2) |
| 129 | (M1 : Fin N1 → Fin k → Fin v1) |
| 130 | (M2 : Fin N2 → Fin k → Fin v2) |
| 131 | (hCA1 : IsCoveringArray N1 t k v1 M1) |
| 132 | (hCA2 : IsCoveringArray N2 t k v2 M2) : |
| 133 | IsCoveringArray (N1 * N2) t k (v1 * v2) |
| 134 | (fun r c => |
| 135 | let r1 : Fin N1 := ⟨r.val / N2, div_lt_of_lt_mul_right r.isLt⟩ |
| 136 | let r2 : Fin N2 := ⟨r.val % N2, Nat.mod_lt r.val hN2⟩ |
| 137 | let a := M1 r1 c |
| 138 | let b := M2 r2 c |
| 139 | ⟨a.val * v2 + b.val, by |
| 140 | have ha : a.val + 1 ≤ v1 := a.isLt |
| 141 | have hb : b.val < v2 := b.isLt |
| 142 | calc a.val * v2 + b.val < a.val * v2 + v2 := Nat.add_lt_add_left hb _ |
| 143 | _ = (a.val + 1) * v2 := by rw [Nat.succ_mul] |
| 144 | _ ≤ v1 * v2 := Nat.mul_le_mul_right v2 ha⟩) := by |
| 145 | intro cols hcols pat |
| 146 | let pat1 : Fin t → Fin v1 := fun i => |
| 147 | ⟨(pat i).val / v2, div_lt_of_lt_mul_right (pat i).isLt⟩ |
| 148 | let pat2 : Fin t → Fin v2 := fun i => |
| 149 | ⟨(pat i).val % v2, Nat.mod_lt (pat i).val hv2⟩ |
| 150 | obtain ⟨r1, hr1⟩ := hCA1 cols hcols pat1 |
| 151 | obtain ⟨r2, hr2⟩ := hCA2 cols hcols pat2 |
| 152 | have hrLt : r1.val * N2 + r2.val < N1 * N2 := by |
| 153 | have h1 : r1.val + 1 ≤ N1 := r1.isLt |
| 154 | have h2 : r2.val < N2 := r2.isLt |
| 155 | calc r1.val * N2 + r2.val < r1.val * N2 + N2 := Nat.add_lt_add_left h2 _ |
| 156 | _ = (r1.val + 1) * N2 := by rw [Nat.succ_mul] |
| 157 | _ ≤ N1 * N2 := Nat.mul_le_mul_right N2 h1 |
| 158 | refine ⟨⟨r1.val * N2 + r2.val, hrLt⟩, ?_⟩ |
| 159 | intro i |
| 160 | apply Fin.ext |
| 161 | dsimp only |
| 162 | have hdiv : (r1.val * N2 + r2.val) / N2 = r1.val := by |
| 163 | rw [Nat.mul_comm, Nat.mul_add_div hN2, Nat.div_eq_of_lt r2.isLt, Nat.add_zero] |
| 164 | have hmod : (r1.val * N2 + r2.val) % N2 = r2.val := by |
| 165 | rw [Nat.mul_comm, Nat.mul_add_mod, Nat.mod_eq_of_lt r2.isLt] |
| 166 | have hr1' : (⟨(r1.val * N2 + r2.val) / N2, div_lt_of_lt_mul_right hrLt⟩ : Fin N1) = r1 := Fin.ext hdiv |
| 167 | have hr2' : (⟨(r1.val * N2 + r2.val) % N2, Nat.mod_lt _ hN2⟩ : Fin N2) = r2 := Fin.ext hmod |
| 168 | rw [hr1', hr2', hr1 i, hr2 i] |
| 169 | dsimp only [pat1, pat2] |
| 170 | rw [Nat.mul_comm ((pat i).val / v2) v2] |
| 171 | exact Nat.div_add_mod (pat i).val v2 |
| 172 | |
| 173 | /-! ## Broken-Model Negative Controls (`neg_ctrl_*`) -/ |
| 174 | |
| 175 | /-- Negative control 1: a constant all-zero matrix is NOT a binary `CA(4; 2, 2, 2)`. -/ |
| 176 | theorem neg_ctrl_constant_matrix_not_ca : |
| 177 | ¬ IsCoveringArray 4 2 2 2 (fun _ _ => ⟨0, by decide⟩) := by |
| 178 | intro h |
| 179 | have hmono : StrictMonoCols (fun i : Fin 2 => i) := by |
| 180 | intro i j hij; exact hij |
| 181 | obtain ⟨r, hr⟩ := h (fun i => i) hmono (fun _ => ⟨1, by decide⟩) |
| 182 | have h0 := congrArg Fin.val (hr ⟨0, by decide⟩) |
| 183 | dsimp only at h0 |
| 184 | exact absurd h0 (by decide) |
| 185 | |
| 186 | /-- Negative control 2: dropping a NON-redundant row from the complete 4-row `CA(4; 2, 2, 2)` |
| 187 | breaks coverage (3 rows cannot cover 4 distinct pairs). -/ |
| 188 | theorem neg_ctrl_nonredundant_row_drop_breaks_ca : |
| 189 | let M : Fin 4 → Fin 2 → Fin 2 := fun r c => |
| 190 | if c.val = 0 then ⟨r.val / 2, by omega⟩ else ⟨r.val % 2, by omega⟩ |
| 191 | ¬ IsCoveringArray 3 2 2 2 (fun r c => M ⟨r.val, Nat.lt_succ_of_lt r.isLt⟩ c) := by |
| 192 | intro M h |
| 193 | have hmono : StrictMonoCols (fun i : Fin 2 => i) := by |
| 194 | intro i j hij; exact hij |
| 195 | obtain ⟨r, hr⟩ := h (fun i => i) hmono (fun _ => ⟨1, by decide⟩) |
| 196 | have h0 := congrArg Fin.val (hr ⟨0, by decide⟩) |
| 197 | have h1 := congrArg Fin.val (hr ⟨1, by decide⟩) |
| 198 | rcases r with ⟨rv, hrv⟩ |
| 199 | match rv, hrv, h0, h1 with |
| 200 | | 0, _, h0, _ => simp [M] at h0 |
| 201 | | 1, _, h0, _ => simp [M] at h0 |
| 202 | | 2, _, _, h1 => simp [M] at h1 |
| 203 | |
| 204 | /-- Negative control 3: a non-surjective symbol map (`fun _ _ => 0`) fails to preserve |
| 205 | `CA(4; 2, 2, 2)` even when the input is a valid `CA(4; 2, 2, 2)`. -/ |
| 206 | theorem neg_ctrl_nonsurjective_symbol_map_fails : |
| 207 | let M : Fin 4 → Fin 2 → Fin 2 := fun r c => |
| 208 | if c.val = 0 then ⟨r.val / 2, by omega⟩ else ⟨r.val % 2, by omega⟩ |
| 209 | let φ : Fin 2 → Fin 2 → Fin 2 := fun _ _ => ⟨0, by decide⟩ |
| 210 | ¬ IsCoveringArray 4 2 2 2 (fun r c => φ c (M r c)) := by |
| 211 | intro M φ h |
| 212 | have hmono : StrictMonoCols (fun i : Fin 2 => i) := by |
| 213 | intro i j hij; exact hij |
| 214 | obtain ⟨r, hr⟩ := h (fun i => i) hmono (fun _ => ⟨1, by decide⟩) |
| 215 | have h0 := congrArg Fin.val (hr ⟨0, by decide⟩) |
| 216 | simp only [φ] at h0 |
| 217 | exact absurd h0 (by decide) |
| 218 | |
| 219 | /-- Negative control 4: a non-injective column map (`fun _ => 0`) breaks `CA(4; 2, 2, 2)` |
| 220 | because duplicated columns can never realize pattern `(0, 1)`. -/ |
| 221 | theorem neg_ctrl_noninjective_col_map_fails : |
| 222 | let M : Fin 4 → Fin 2 → Fin 2 := fun r c => |
| 223 | if c.val = 0 then ⟨r.val / 2, by omega⟩ else ⟨r.val % 2, by omega⟩ |
| 224 | let σ : Fin 2 → Fin 2 := fun _ => ⟨0, by decide⟩ |
| 225 | ¬ IsCoveringArray 4 2 2 2 (fun r c => M r (σ c)) := by |
| 226 | intro M σ h |
| 227 | have hmono : StrictMonoCols (fun i : Fin 2 => i) := by |
| 228 | intro i j hij; exact hij |
| 229 | obtain ⟨r, hr⟩ := h (fun i => i) hmono (fun i => i) |
| 230 | have h0 := hr ⟨0, by decide⟩ |
| 231 | have h1 := hr ⟨1, by decide⟩ |
| 232 | simp only [σ] at h0 h1 |
| 233 | rw [h0] at h1 |
| 234 | exact absurd (congrArg Fin.val h1) (by decide) |
| 235 | |
| 236 | end CoveringArrayStructuralW2 |
| 237 | |
| 238 | #print axioms CoveringArrayStructuralW2.ca_drop_redundant_last_row |
| 239 | #print axioms CoveringArrayStructuralW2.ca_pullback_injective_cols |
| 240 | #print axioms CoveringArrayStructuralW2.ca_map_symbols_surjective |
| 241 | #print axioms CoveringArrayStructuralW2.ca_kronecker_product |
| 242 | #print axioms CoveringArrayStructuralW2.neg_ctrl_constant_matrix_not_ca |
| 243 | #print axioms CoveringArrayStructuralW2.neg_ctrl_nonredundant_row_drop_breaks_ca |
| 244 | #print axioms CoveringArrayStructuralW2.neg_ctrl_nonsurjective_symbol_map_fails |
| 245 | #print axioms CoveringArrayStructuralW2.neg_ctrl_noninjective_col_map_fails |