interplane.Get Omega
Read in your language

omega-principia

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-principia / proofs / ForcedChainContractionPhaseLaw.lean

1038 lines · 45824 bytes Raw Blame History
1/-
2SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3proofs/ForcedChainContractionPhaseLaw.lean
4
5Formal Lean 4 verification of:
6 1. Exact forced-out and forced-in edge elimination (`forced_out_elim_iff`, `forced_in_elim_iff`),
7 2. Premature 2-cycle and start-to-finish shortcut elimination
8 (`premature_2cycle_elim_iff`, `premature_sf_shortcut_elim_iff`),
9 3. Unified forced-chain contraction bijection (`chain_contract_bijection`) covering interior,
10 start-anchored, and finish-anchored contractions — proving exact #P tour-count invariance
11 `#Ham(G) = #Ham(G')`,
12 4. Dulmage-Mendelsohn non-cover edge elimination (`dm_noncover_edge_elim_iff`),
13 5. Odd-modulus residue-block sharding exactness & bit-0 alternation
14 (`odd_mod_block_partition_exact`, `odd_mod_flips_bit0`),
15 6. Four executed broken-model negative controls (`neg_ctrl_*`).
16
17Model-to-system link:
18 Mirrors `experiments/ham_tour_discovery/merge_quotient/contract_bench.om` (`DM_CONTRACT`,
19 `OMEGA_HAM_EXACT`, `CONTRACT_IE`) and `ie_bench.om` (`--mod-range`).
20 Zero `sorry`, zero `native_decide`, standard core axioms only (`#print axioms` checked).
21-/
22
23import Mathlib.Data.Finset.Basic
24import Mathlib.Data.Finset.Card
25import Mathlib.Data.Finset.Image
26import Mathlib.Data.Finset.SDiff
27import Mathlib.Data.Nat.Factorial.Basic
28import Mathlib.Logic.Equiv.Basic
29
30namespace ForcedChainContraction
31
32/-- Directed graph on an arbitrary vertex type `V`. -/
33structure Digraph (V : Type) where
34 edge : V → V → Prop
35
36/-- A non-empty directed path `v₀ → v₁ → ... → vₖ` in `G`. -/
37def IsChain {V : Type} (G : Digraph V) : List V → Prop
38 | [] => False
39 | [_] => True
40 | u :: v :: tl => G.edge u v ∧ IsChain G (v :: tl)
41
42/-- Adjacency of two consecutive elements `(u, v)` inside a list `p`. -/
43def AdjIn {V : Type} (u v : V) (p : List V) : Prop :=
44 ∃ l r : List V, p = l ++ u :: v :: r
45
46/-- Exact Hamiltonian `s → ... → f` path visiting every vertex of `S : Finset V` once. -/
47def IsHamPath {V : Type} (G : Digraph V) (S : Finset V) (s f : V) (p : List V) : Prop :=
48 p.Nodup ∧
49 p.head? = some s ∧
50 p.getLast? = some f ∧
51 (∀ v, v ∈ p ↔ v ∈ S) ∧
52 IsChain G p
53
54/-! ## Section 1: Structural List & Chain Lemmas -/
55
56lemma isChain_append_right {V : Type} {G : Digraph V} :
57 ∀ (l r : List V), IsChain G (l ++ r) → r ≠ [] → IsChain G r
58 | [], r, h, _ => h
59 | [_], r, h, hr => by
60 cases r with
61 | nil => exact absurd rfl hr
62 | cons v tl => exact h.2
63 | u :: v :: tl, r, h, hr =>
64 isChain_append_right (v :: tl) r h.2 hr
65
66lemma adjIn_edge {V : Type} {G : Digraph V} {u v : V} {p : List V}
67 (hc : IsChain G p) (hadj : AdjIn u v p) : G.edge u v := by
68 rcases hadj with ⟨l, r, rfl⟩
69 have hsub : IsChain G (u :: v :: r) :=
70 isChain_append_right l (u :: v :: r) hc (List.cons_ne_nil _ _)
71 exact hsub.1
72
73lemma exists_succ_of_mem_ne_last {V : Type} :
74 ∀ (p : List V) (x f : V),
75 p.getLast? = some f → x ∈ p → x ≠ f → ∃ y, AdjIn x y p
76 | [], _, _, hlast, _, _ => by simp at hlast
77 | [a], x, f, hlast, hmem, hne => by
78 simp at hlast hmem
79 subst hmem; subst hlast
80 exact absurd rfl hne
81 | a :: b :: tl, x, f, hlast, hmem, hne => by
82 rcases List.mem_cons.mp hmem with rfl | hmem'
83 · exact ⟨b, [], tl, rfl⟩
84 · have hlast' : (b :: tl).getLast? = some f := by
85 simpa [List.getLast?_cons_cons] using hlast
86 rcases exists_succ_of_mem_ne_last (b :: tl) x f hlast' hmem' hne with ⟨y, l, r, heq⟩
87 exact ⟨y, a :: l, r, by simp [heq]⟩
88
89lemma exists_pred_of_mem_ne_head {V : Type} :
90 ∀ (p : List V) (y s : V),
91 p.head? = some s → y ∈ p → y ≠ s → ∃ x, AdjIn x y p
92 | [], _, _, hhead, _, _ => by simp at hhead
93 | [a], y, s, hhead, hmem, hne => by
94 simp at hhead hmem
95 subst hmem; subst hhead
96 exact absurd rfl hne
97 | a :: b :: tl, y, s, hhead, hmem, hne => by
98 simp at hhead
99 subst hhead
100 rcases List.mem_cons.mp hmem with rfl | hmem'
101 · exact absurd rfl hne
102 · rcases List.mem_cons.mp hmem' with rfl | hmem''
103 · exact ⟨a, [], tl, rfl⟩
104 · by_cases hby : y = b
105 · subst hby; exact ⟨a, [], tl, rfl⟩
106 · rcases exists_pred_of_mem_ne_head (b :: tl) y b rfl hmem' hby with ⟨x, l, r, heq⟩
107 exact ⟨x, a :: l, r, by simp [heq]⟩
108
109lemma nodup_split_unique {V : Type} {x : V} {l₁ r₁ l₂ r₂ : List V}
110 (hnd : (l₁ ++ x :: r₁).Nodup)
111 (heq : l₁ ++ x :: r₁ = l₂ ++ x :: r₂) : l₁ = l₂ ∧ r₁ = r₂ := by
112 induction l₁ generalizing l₂ with
113 | nil =>
114 cases l₂ with
115 | nil =>
116 simp at heq
117 exact ⟨rfl, heq⟩
118 | cons a l₂' =>
119 simp at heq
120 rcases heq with ⟨hxa, htail⟩
121 subst hxa
122 have hx : x ∈ r₁ := by
123 rw [htail]
124 exact List.mem_append_right _ List.mem_cons_self
125 have hnot : x ∉ r₁ := (List.nodup_cons.mp hnd).1
126 exact absurd hx hnot
127 | cons a l₁' ih =>
128 cases l₂ with
129 | nil =>
130 simp at heq
131 rcases heq with ⟨hax, htail⟩
132 subst hax
133 have hx : a ∈ l₁' ++ a :: r₁ :=
134 List.mem_append_right _ List.mem_cons_self
135 have hnot : a ∉ l₁' ++ a :: r₁ := (List.nodup_cons.mp hnd).1
136 exact absurd hx hnot
137 | cons b l₂' =>
138 simp at heq
139 rcases heq with ⟨hab, htail⟩
140 subst hab
141 have hnd' : (l₁' ++ x :: r₁).Nodup := (List.nodup_cons.mp hnd).2
142 rcases ih hnd' htail with ⟨hl, hr⟩
143 exact ⟨by rw [hl], hr⟩
144
145lemma adjIn_unique_succ {V : Type} {p : List V} {x y₁ y₂ : V}
146 (hnd : p.Nodup) (h1 : AdjIn x y₁ p) (h2 : AdjIn x y₂ p) : y₁ = y₂ := by
147 rcases h1 with ⟨l₁, r₁, rfl⟩
148 rcases h2 with ⟨l₂, r₂, heq⟩
149 have ⟨_, hr⟩ := nodup_split_unique hnd heq
150 exact (List.cons.inj hr).1
151
152lemma adjIn_unique_pred {V : Type} {p : List V} {x₁ x₂ y : V}
153 (hnd : p.Nodup) (h1 : AdjIn x₁ y p) (h2 : AdjIn x₂ y p) : x₁ = x₂ := by
154 rcases h1 with ⟨l₁, r₁, rfl⟩
155 rcases h2 with ⟨l₂, r₂, heq⟩
156 have h1' : (l₁ ++ [x₁]) ++ y :: r₁ = (l₂ ++ [x₂]) ++ y :: r₂ := by
157 simpa [List.append_assoc] using heq
158 have hnd' : ((l₁ ++ [x₁]) ++ y :: r₁).Nodup := by
159 simpa [List.append_assoc] using hnd
160 have ⟨hl, _⟩ := nodup_split_unique hnd' h1'
161 have hlast := congrArg List.getLast? hl
162 simp at hlast
163 exact hlast
164
165lemma adjIn_mem_left {V : Type} {u v : V} {p : List V} (h : AdjIn u v p) : u ∈ p := by
166 rcases h with ⟨l, r, rfl⟩
167 exact List.mem_append_right _ List.mem_cons_self
168
169lemma adjIn_mem_right {V : Type} {u v : V} {p : List V} (h : AdjIn u v p) : v ∈ p := by
170 rcases h with ⟨l, r, rfl⟩
171 exact List.mem_append_right _ (List.mem_cons_of_mem _ List.mem_cons_self)
172
173lemma adjIn_ne {V : Type} {u v : V} {p : List V} (hnd : p.Nodup) (h : AdjIn u v p) : u ≠ v := by
174 rcases h with ⟨l, r, rfl⟩
175 intro heq
176 subst heq
177 have hsub : (u :: u :: r).Nodup := (List.nodup_append.mp hnd).2.1
178 exact (List.nodup_cons.mp hsub).1 List.mem_cons_self
179
180lemma isChain_mono_on_adj {V : Type} {G₁ G₂ : Digraph V} :
181 ∀ (p : List V),
182 IsChain G₁ p →
183 (∀ u v, AdjIn u v p → G₂.edge u v) →
184 IsChain G₂ p
185 | [], h, _ => h
186 | [_], _, _ => trivial
187 | u :: v :: tl, h, hedge => by
188 refine ⟨hedge u v ⟨[], tl, rfl⟩, ?_⟩
189 apply isChain_mono_on_adj (v :: tl) h.2
190 intro a b ⟨l, r, heq⟩
191 exact hedge a b ⟨u :: l, r, by simp [heq]⟩
192
193/-! ## Section 2: Forced-Out, Forced-In, 2-Cycle & Shortcut Eliminations -/
194
195/-- Theorem 1 (`forced_out_elim_iff`):
196 If `x ∈ S`, `x ≠ f`, and `y` is the unique out-neighbor of `x` in `S`, then deleting every
197 edge `u → y` with `u ≠ x` preserves `IsHamPath` pointwise (hence exact `#Ham`). -/
198theorem forced_out_elim_iff {V : Type} [DecidableEq V]
199 (G : Digraph V) (S : Finset V) (s f x y : V)
200 (hxS : x ∈ S) (hxf : x ≠ f)
201 (huniq : ∀ v ∈ S, G.edge x v → v = y)
202 (p : List V) :
203 let G' : Digraph V := ⟨fun u v => G.edge u v ∧ ¬(u ≠ x ∧ v = y)⟩
204 IsHamPath G S s f p ↔ IsHamPath G' S s f p := by
205 intro G'
206 constructor
207 · rintro ⟨hnd, hhd, hlt, hmem, hch⟩
208 refine ⟨hnd, hhd, hlt, hmem, ?_⟩
209 apply isChain_mono_on_adj p hch
210 intro u v hadj
211 refine ⟨adjIn_edge hch hadj, ?_⟩
212 rintro ⟨hux, hvy⟩
213 subst hvy
214 have hx_mem : x ∈ p := (hmem x).mpr hxS
215 rcases exists_succ_of_mem_ne_last p x f hlt hx_mem hxf with ⟨w, hadj_xw⟩
216 have hwS : w ∈ S := (hmem w).mp (adjIn_mem_right hadj_xw)
217 have hwy : w = v := huniq w hwS (adjIn_edge hch hadj_xw)
218 subst hwy
219 exact hux (adjIn_unique_pred hnd hadj hadj_xw)
220 · rintro ⟨hnd, hhd, hlt, hmem, hch⟩
221 refine ⟨hnd, hhd, hlt, hmem, ?_⟩
222 apply isChain_mono_on_adj p hch
223 intro u v hadj
224 exact (adjIn_edge hch hadj).1
225
226/-- Theorem 2 (`forced_in_elim_iff`):
227 If `y ∈ S`, `y ≠ s`, and `x` is the unique in-neighbor of `y` in `S`, then deleting every
228 edge `x → v` with `v ≠ y` preserves `IsHamPath` pointwise (hence exact `#Ham`). -/
229theorem forced_in_elim_iff {V : Type} [DecidableEq V]
230 (G : Digraph V) (S : Finset V) (s f x y : V)
231 (hyS : y ∈ S) (hys : y ≠ s)
232 (huniq : ∀ u ∈ S, G.edge u y → u = x)
233 (p : List V) :
234 let G' : Digraph V := ⟨fun u v => G.edge u v ∧ ¬(u = x ∧ v ≠ y)⟩
235 IsHamPath G S s f p ↔ IsHamPath G' S s f p := by
236 intro G'
237 constructor
238 · rintro ⟨hnd, hhd, hlt, hmem, hch⟩
239 refine ⟨hnd, hhd, hlt, hmem, ?_⟩
240 apply isChain_mono_on_adj p hch
241 intro u v hadj
242 refine ⟨adjIn_edge hch hadj, ?_⟩
243 rintro ⟨hux, hvy⟩
244 subst hux
245 have hy_mem : y ∈ p := (hmem y).mpr hyS
246 rcases exists_pred_of_mem_ne_head p y s hhd hy_mem hys with ⟨w, hadj_wy⟩
247 have hwS : w ∈ S := (hmem w).mp (adjIn_mem_left hadj_wy)
248 have hwx : w = u := huniq w hwS (adjIn_edge hch hadj_wy)
249 subst hwx
250 exact hvy (adjIn_unique_succ hnd hadj hadj_wy)
251 · rintro ⟨hnd, hhd, hlt, hmem, hch⟩
252 refine ⟨hnd, hhd, hlt, hmem, ?_⟩
253 apply isChain_mono_on_adj p hch
254 intro u v hadj
255 exact (adjIn_edge hch hadj).1
256
257/-- Theorem 3 (`premature_2cycle_elim_iff`):
258 If `x → y` is forced (unique out-neighbor of `x ≠ f`), then `y → x` can never appear in any
259 Hamiltonian path `p`, because `x → y → x` violates `p.Nodup`. -/
260theorem premature_2cycle_elim_iff {V : Type} [DecidableEq V]
261 (G : Digraph V) (S : Finset V) (s f x y : V)
262 (hxS : x ∈ S) (hxf : x ≠ f)
263 (huniq : ∀ v ∈ S, G.edge x v → v = y)
264 (p : List V) :
265 let G' : Digraph V := ⟨fun u v => G.edge u v ∧ ¬(u = y ∧ v = x)⟩
266 IsHamPath G S s f p ↔ IsHamPath G' S s f p := by
267 intro G'
268 constructor
269 · rintro ⟨hnd, hhd, hlt, hmem, hch⟩
270 refine ⟨hnd, hhd, hlt, hmem, ?_⟩
271 apply isChain_mono_on_adj p hch
272 intro u v hadj
273 refine ⟨adjIn_edge hch hadj, ?_⟩
274 rintro ⟨huy, hvx⟩
275 rw [huy, hvx] at hadj
276 have hx_mem : x ∈ p := (hmem x).mpr hxS
277 rcases exists_succ_of_mem_ne_last p x f hlt hx_mem hxf with ⟨w, hadj_xw⟩
278 have hwS : w ∈ S := (hmem w).mp (adjIn_mem_right hadj_xw)
279 have hwy : w = y := huniq w hwS (adjIn_edge hch hadj_xw)
280 rw [hwy] at hadj_xw
281 rcases hadj_xw with ⟨l₁, r₁, rfl⟩
282 rcases hadj with ⟨l₂, r₂, heq⟩
283 have h1 : (l₁ ++ [x]) ++ y :: r₁ = l₂ ++ y :: x :: r₂ := by
284 simpa [List.append_assoc] using heq
285 have hnd' : ((l₁ ++ [x]) ++ y :: r₁).Nodup := by
286 simpa [List.append_assoc] using hnd
287 have ⟨_, hr⟩ := nodup_split_unique hnd' h1
288 have hsub : (x :: y :: r₁).Nodup := (List.nodup_append.mp hnd).2.1
289 rw [hr] at hsub
290 have hx_in : x ∈ y :: x :: r₂ := List.mem_cons_of_mem _ List.mem_cons_self
291 exact (List.nodup_cons.mp hsub).1 hx_in
292 · rintro ⟨hnd, hhd, hlt, hmem, hch⟩
293 refine ⟨hnd, hhd, hlt, hmem, ?_⟩
294 apply isChain_mono_on_adj p hch
295 intro u v hadj
296 exact (adjIn_edge hch hadj).1
297
298/-- Theorem 4 (`premature_sf_shortcut_elim_iff`):
299 When `S.card > 2`, the direct shortcut `s → f` can never appear in any `IsHamPath G S s f p`. -/
300theorem premature_sf_shortcut_elim_iff {V : Type} [DecidableEq V]
301 (G : Digraph V) (S : Finset V) (s f : V)
302 (hcard : 2 < S.card)
303 (p : List V) :
304 let G' : Digraph V := ⟨fun u v => G.edge u v ∧ ¬(u = s ∧ v = f)⟩
305 IsHamPath G S s f p ↔ IsHamPath G' S s f p := by
306 intro G'
307 constructor
308 · rintro ⟨hnd, hhd, hlt, hmem, hch⟩
309 refine ⟨hnd, hhd, hlt, hmem, ?_⟩
310 apply isChain_mono_on_adj p hch
311 intro u v hadj
312 refine ⟨adjIn_edge hch hadj, ?_⟩
313 rintro ⟨hus, hvf⟩
314 rw [hus, hvf] at hadj
315 rcases hadj with ⟨l, r, rfl⟩
316 have hl_nil : l = [] := by
317 cases l with
318 | nil => rfl
319 | cons a l' =>
320 simp at hhd
321 subst hhd
322 have hs_in : a ∈ l' ++ a :: f :: r :=
323 List.mem_append_right _ List.mem_cons_self
324 exact absurd hs_in (List.nodup_cons.mp hnd).1
325 subst hl_nil
326 have hr_nil : r = [] := by
327 cases r with
328 | nil => rfl
329 | cons b r' =>
330 have hlt' : (b :: r').getLast? = some f := by
331 simpa [List.getLast?_cons_cons] using hlt
332 have hf_in : f ∈ b :: r' := List.mem_of_getLast? hlt'
333 have hnd_f : (f :: b :: r').Nodup := (List.nodup_cons.mp hnd).2
334 exact absurd hf_in (List.nodup_cons.mp hnd_f).1
335 subst hr_nil
336 have hto : ([s, f] : List V).toFinset = S := by
337 ext z
338 have hz := hmem z
339 simpa using hz
340 have hle : S.card ≤ 2 := by
341 rw [← hto]
342 calc ([s, f] : List V).toFinset.card
343 ≤ ([s, f] : List V).length := List.toFinset_card_le _
344 _ = 2 := rfl
345 omega
346 · rintro ⟨hnd, hhd, hlt, hmem, hch⟩
347 refine ⟨hnd, hhd, hlt, hmem, ?_⟩
348 apply isChain_mono_on_adj p hch
349 intro u v hadj
350 exact (adjIn_edge hch hadj).1
351
352/-! ## Section 3: Unified Forced-Chain Contraction Bijection (`#Ham(G) = #Ham(G')`) -/
353
354/-- Contracted digraph `G'` obtained by merging `y` into `x` (dropping any `y → x` 2-cycle). -/
355def contractGraph {V : Type} [DecidableEq V] (G : Digraph V) (x y : V) : Digraph V :=
356 ⟨fun u v => if u = x then (v ≠ x ∧ G.edge y v) else G.edge u v⟩
357
358/-- Remove `y` from a path `p` (valid when `x` is immediately followed by `y`). -/
359def contractPath {V : Type} [DecidableEq V] (y : V) (p : List V) : List V :=
360 p.filter (· ≠ y)
361
362/-- Expand `x` back into `x :: y` inside a contracted path `q`. -/
363def expandPath {V : Type} [DecidableEq V] (x y : V) : List V → List V
364 | [] => []
365 | v :: tl => if v = x then x :: y :: expandPath x y tl else v :: expandPath x y tl
366
367lemma contractPath_of_split {V : Type} [DecidableEq V] {x y : V} {l r : List V}
368 (hnd : (l ++ x :: y :: r).Nodup) :
369 contractPath y (l ++ x :: y :: r) = l ++ x :: r := by
370 have hy_l : y ∉ l := by
371 intro h
372 have := (List.nodup_append.mp hnd).2.2 y h y (List.mem_cons_of_mem _ List.mem_cons_self)
373 exact this rfl
374 have hxy : x ≠ y := adjIn_ne hnd ⟨l, r, rfl⟩
375 have hy_r : y ∉ r := by
376 have hsub : (x :: y :: r).Nodup := (List.nodup_append.mp hnd).2.1
377 exact (List.nodup_cons.mp (List.nodup_cons.mp hsub).2).1
378 have hfl : l.filter (fun a => !decide (a = y)) = l :=
379 List.filter_eq_self.mpr (fun a ha => by
380 have hne : a ≠ y := fun h => hy_l (h ▸ ha)
381 simp [hne])
382 have hfr : r.filter (fun a => !decide (a = y)) = r :=
383 List.filter_eq_self.mpr (fun a ha => by
384 have hne : a ≠ y := fun h => hy_r (h ▸ ha)
385 simp [hne])
386 have hxy_b : (!decide (x = y)) = true := by simp [hxy]
387 simp only [contractPath, decide_not, List.filter_append, List.filter_cons,
388 hxy_b, decide_true, Bool.not_true, Bool.false_eq_true, ↓reduceIte, hfl, hfr]
389
390lemma expandPath_of_split {V : Type} [DecidableEq V] {x y : V} {l r : List V}
391 (hnd : (l ++ x :: r).Nodup) :
392 expandPath x y (l ++ x :: r) = l ++ x :: y :: r := by
393 have hx_l : x ∉ l := by
394 intro h
395 exact (List.nodup_append.mp hnd).2.2 x h x List.mem_cons_self rfl
396 have hx_r : x ∉ r := by
397 have hsub : (x :: r).Nodup := (List.nodup_append.mp hnd).2.1
398 exact (List.nodup_cons.mp hsub).1
399 have hexp_none : ∀ (xs : List V), x ∉ xs → expandPath x y xs = xs := by
400 intro xs hxs
401 induction xs with
402 | nil => rfl
403 | cons a tl ih =>
404 have hax : a ≠ x := fun h => hxs (h ▸ List.mem_cons_self)
405 have htl : x ∉ tl := fun h => hxs (List.mem_cons_of_mem _ h)
406 simp [expandPath, hax, ih htl]
407 have hexp_app : ∀ (xs ys : List V), expandPath x y (xs ++ ys) = expandPath x y xs ++ expandPath x y ys := by
408 intro xs ys
409 induction xs with
410 | nil => rfl
411 | cons a tl ih =>
412 by_cases hax : a = x
413 · subst hax; simp [expandPath, ih]
414 · simp [expandPath, hax, ih]
415 rw [hexp_app, hexp_none l hx_l]
416 simp [expandPath, hexp_none r hx_r]
417
418lemma isChain_contract_split {V : Type} [DecidableEq V] {G : Digraph V} {x y : V} {l r : List V}
419 (hnd : (l ++ x :: y :: r).Nodup)
420 (hch : IsChain G (l ++ x :: y :: r)) :
421 IsChain (contractGraph G x y) (l ++ x :: r) := by
422 induction l with
423 | nil =>
424 cases r with
425 | nil => exact trivial
426 | cons w r' =>
427 have hxw : w ≠ x := by
428 intro h; subst h
429 have hmem : w ∈ y :: w :: r' := List.mem_cons_of_mem _ List.mem_cons_self
430 exact (List.nodup_cons.mp hnd).1 hmem
431 have hrec : IsChain (contractGraph G x y) (w :: r') := by
432 apply isChain_mono_on_adj (w :: r') hch.2.2
433 intro a b hadj
434 have hax : a ≠ x := by
435 intro h; subst h
436 have ha : a ∈ y :: w :: r' := List.mem_cons_of_mem _ (adjIn_mem_left hadj)
437 exact (List.nodup_cons.mp hnd).1 ha
438 simp [contractGraph, hax, adjIn_edge hch.2.2 hadj]
439 exact ⟨by simp [contractGraph, hxw, hch.2.1], hrec⟩
440 | cons a l' ih =>
441 have hnd' : (l' ++ x :: y :: r).Nodup := (List.nodup_cons.mp hnd).2
442 have hax : a ≠ x := by
443 intro h; subst h
444 have ha : a ∈ l' ++ a :: y :: r := List.mem_append_right _ List.mem_cons_self
445 exact (List.nodup_cons.mp hnd).1 ha
446 cases l' with
447 | nil =>
448 exact ⟨by simp [contractGraph, hax, hch.1], ih hnd' hch.2⟩
449 | cons b l'' =>
450 exact ⟨by simp [contractGraph, hax, hch.1], ih hnd' hch.2⟩
451
452lemma isChain_expand_split {V : Type} [DecidableEq V] {G : Digraph V} {x y : V} {l r : List V}
453 (hnd : (l ++ x :: r).Nodup)
454 (hxy_edge : G.edge x y)
455 (hch : IsChain (contractGraph G x y) (l ++ x :: r)) :
456 IsChain G (l ++ x :: y :: r) := by
457 induction l with
458 | nil =>
459 cases r with
460 | nil => exact ⟨hxy_edge, trivial⟩
461 | cons w r' =>
462 have hyw : G.edge y w := by
463 have h1 := hch.1
464 simp [contractGraph] at h1
465 exact h1.2
466 have hrec : IsChain G (w :: r') := by
467 apply isChain_mono_on_adj (w :: r') hch.2
468 intro a b hadj
469 have hax : a ≠ x := by
470 intro h; subst h
471 exact (List.nodup_cons.mp hnd).1 (adjIn_mem_left hadj)
472 have he := adjIn_edge hch.2 hadj
473 simp [contractGraph, hax] at he
474 exact he
475 exact ⟨hxy_edge, hyw, hrec⟩
476 | cons a l' ih =>
477 have hnd' : (l' ++ x :: r).Nodup := (List.nodup_cons.mp hnd).2
478 have hax : a ≠ x := by
479 intro h; subst h
480 exact (List.nodup_cons.mp hnd).1 (List.mem_append_right _ List.mem_cons_self)
481 cases l' with
482 | nil =>
483 have he := hch.1
484 simp [contractGraph, hax] at he
485 exact ⟨he, ih hnd' hch.2⟩
486 | cons b l'' =>
487 have he := hch.1
488 simp [contractGraph, hax] at he
489 exact ⟨he, ih hnd' hch.2⟩
490
491/-- Theorem 5 (`chain_contract_bijection`):
492 Unified exact contraction bijection for forced edge `x → y` (`out(x) = {y}`, `in(y) = {x}`,
493 `x ≠ f`, `y ≠ s`, `x ≠ y`). Covers interior `x → y`, start-anchored `s → y`, and
494 finish-anchored `x → f` uniformly, proving exact `#Ham(G) = #Ham(G')`. -/
495def chain_contract_bijection {V : Type} [DecidableEq V]
496 (G : Digraph V) (S : Finset V) (s f x y : V)
497 (hxS : x ∈ S) (hyS : y ∈ S) (hxy : x ≠ y)
498 (hxf : x ≠ f) (hys : y ≠ s)
499 (hxy_edge : G.edge x y)
500 (hout : ∀ v ∈ S, G.edge x v → v = y)
501 (_hin : ∀ u ∈ S, G.edge u y → u = x) :
502 { p : List V // IsHamPath G S s f p } ≃
503 { q : List V // IsHamPath (contractGraph G x y) (S.erase y) s (if y = f then x else f) q } where
504 toFun := fun ⟨p, hnd, hhd, hlt, hmem, hch⟩ =>
505 ⟨contractPath y p, by
506 have hx_mem : x ∈ p := (hmem x).mpr hxS
507 rcases exists_succ_of_mem_ne_last p x f hlt hx_mem hxf with ⟨w, hadj_xw⟩
508 have hwS : w ∈ S := (hmem w).mp (adjIn_mem_right hadj_xw)
509 have hwy : w = y := hout w hwS (adjIn_edge hch hadj_xw)
510 rw [hwy] at hadj_xw
511 rcases hadj_xw with ⟨l, r, hp⟩
512 subst hp
513 rw [contractPath_of_split hnd]
514 refine ⟨?_, ?_, ?_, ?_, isChain_contract_split hnd hch⟩
515 · have h1 := List.nodup_append.mp hnd
516 have h2 := List.nodup_cons.mp h1.2.1
517 have h3 := List.nodup_cons.mp h2.2
518 refine List.nodup_append.mpr ⟨h1.1, List.nodup_cons.mpr ⟨fun hx => h2.1 (List.mem_cons_of_mem _ hx), h3.2⟩, ?_⟩
519 intro a ha b hb
520 apply h1.2.2 a ha b
521 rcases List.mem_cons.mp hb with rfl | hbr
522 · exact List.mem_cons_self
523 · exact List.mem_cons_of_mem _ (List.mem_cons_of_mem _ hbr)
524 · cases l with
525 | nil => simpa using hhd
526 | cons a l' => simpa using hhd
527 · by_cases hyf : y = f
528 · subst hyf
529 have hr_nil : r = [] := by
530 cases r with
531 | nil => rfl
532 | cons b r' =>
533 have hlt' : (b :: r').getLast? = some y := by
534 have := hlt
535 rw [List.getLast?_append_of_ne_nil l (List.cons_ne_nil _ _)] at this
536 simpa [List.getLast?_cons_cons] using this
537 have hy_in : y ∈ b :: r' := List.mem_of_getLast? hlt'
538 have hsub : (x :: y :: b :: r').Nodup := (List.nodup_append.mp hnd).2.1
539 exact absurd hy_in (List.nodup_cons.mp (List.nodup_cons.mp hsub).2).1
540 subst hr_nil
541 simp
542 · cases r with
543 | nil =>
544 have : some y = some f := by
545 rw [List.getLast?_append_of_ne_nil l (List.cons_ne_nil _ _)] at hlt
546 simpa using hlt
547 exact absurd (Option.some.inj this) hyf
548 | cons b r' =>
549 have hlt' : (b :: r').getLast? = some f := by
550 rw [List.getLast?_append_of_ne_nil l (List.cons_ne_nil _ _)] at hlt
551 simpa [List.getLast?_cons_cons] using hlt
552 rw [ite_eq_right_iff.mpr (fun h => absurd h hyf), List.getLast?_append_of_ne_nil l (List.cons_ne_nil _ _)]
553 simpa [List.getLast?_cons_cons] using hlt'
554 · intro v
555 rw [Finset.mem_erase, ← hmem v]
556 constructor
557 · intro hv
558 have hvy : v ≠ y := by
559 intro h; subst h
560 have h1 := List.nodup_append.mp hnd
561 have h2 := List.nodup_cons.mp h1.2.1
562 have h3 := List.nodup_cons.mp h2.2
563 rcases List.mem_append.mp hv with hvl | hvr
564 · exact h1.2.2 v hvl v (List.mem_cons_of_mem _ List.mem_cons_self) rfl
565 · rcases List.mem_cons.mp hvr with hvx | hvrr
566 · exact hxy hvx.symm
567 · exact h3.1 hvrr
568 refine ⟨hvy, ?_⟩
569 rcases List.mem_append.mp hv with hvl | hvr
570 · exact List.mem_append_left _ hvl
571 · rcases List.mem_cons.mp hvr with rfl | hvrr
572 · exact List.mem_append_right _ List.mem_cons_self
573 · exact List.mem_append_right _ (List.mem_cons_of_mem _ (List.mem_cons_of_mem _ hvrr))
574 · rintro ⟨hvy, hv⟩
575 rcases List.mem_append.mp hv with hvl | hvr
576 · exact List.mem_append_left _ hvl
577 · rcases List.mem_cons.mp hvr with rfl | hv1
578 · exact List.mem_append_right _ List.mem_cons_self
579 · rcases List.mem_cons.mp hv1 with rfl | hvrr
580 · exact absurd rfl hvy
581 · exact List.mem_append_right _ (List.mem_cons_of_mem _ hvrr)⟩
582 invFun := fun ⟨q, hnd, hhd, hlt, hmem, hch⟩ =>
583 ⟨expandPath x y q, by
584 have hx_mem : x ∈ q := (hmem x).mpr (Finset.mem_erase.mpr ⟨hxy, hxS⟩)
585 rcases List.append_of_mem hx_mem with ⟨l, r, hq⟩
586 subst hq
587 rw [expandPath_of_split hnd]
588 have hy_q : y ∉ l ++ x :: r := fun h => (Finset.mem_erase.mp ((hmem y).mp h)).1 rfl
589 have hy_l : y ∉ l := fun h => hy_q (List.mem_append_left _ h)
590 have hy_r : y ∉ r := fun h => hy_q (List.mem_append_right _ (List.mem_cons_of_mem _ h))
591 refine ⟨?_, ?_, ?_, ?_, isChain_expand_split hnd hxy_edge hch⟩
592 · have h1 := List.nodup_append.mp hnd
593 have h2 := List.nodup_cons.mp h1.2.1
594 refine List.nodup_append.mpr ⟨h1.1, List.nodup_cons.mpr ⟨?_, List.nodup_cons.mpr ⟨hy_r, h2.2⟩⟩, ?_⟩
595 · intro hx; rcases List.mem_cons.mp hx with h | h
596 · exact hxy h
597 · exact h2.1 h
598 · intro a ha b hb
599 rcases List.mem_cons.mp hb with rfl | hb1
600 · exact h1.2.2 a ha b List.mem_cons_self
601 · rcases List.mem_cons.mp hb1 with rfl | hbr
602 · intro h; subst h; exact hy_l ha
603 · exact h1.2.2 a ha b (List.mem_cons_of_mem _ hbr)
604 · cases l with
605 | nil => simpa using hhd
606 | cons a l' => simpa using hhd
607 · by_cases hyf : y = f
608 · subst hyf
609 rw [ite_eq_left_iff.mpr (fun h => absurd rfl h)] at hlt
610 have hr_nil : r = [] := by
611 cases r with
612 | nil => rfl
613 | cons b r' =>
614 have hlt' : (b :: r').getLast? = some x := by
615 rw [List.getLast?_append_of_ne_nil l (List.cons_ne_nil _ _)] at hlt
616 simpa [List.getLast?_cons_cons] using hlt
617 have hx_in : x ∈ b :: r' := List.mem_of_getLast? hlt'
618 have hsub : (x :: b :: r').Nodup := (List.nodup_append.mp hnd).2.1
619 exact absurd hx_in (List.nodup_cons.mp hsub).1
620 subst hr_nil
621 simp
622 · rw [ite_eq_right_iff.mpr (fun h => absurd h hyf)] at hlt
623 cases r with
624 | nil =>
625 have : some x = some f := by
626 rw [List.getLast?_append_of_ne_nil l (List.cons_ne_nil _ _)] at hlt
627 simpa using hlt
628 exact absurd (Option.some.inj this) hxf
629 | cons b r' =>
630 have hlt' : (b :: r').getLast? = some f := by
631 rw [List.getLast?_append_of_ne_nil l (List.cons_ne_nil _ _)] at hlt
632 simpa [List.getLast?_cons_cons] using hlt
633 rw [List.getLast?_append_of_ne_nil l (List.cons_ne_nil _ _)]
634 simpa [List.getLast?_cons_cons] using hlt'
635 · intro v
636 constructor
637 · intro hv
638 rcases List.mem_append.mp hv with hvl | hvr
639 · exact (Finset.mem_erase.mp ((hmem v).mp (List.mem_append_left _ hvl))).2
640 · rcases List.mem_cons.mp hvr with rfl | hv1
641 · exact hxS
642 · rcases List.mem_cons.mp hv1 with rfl | hvrr
643 · exact hyS
644 · exact (Finset.mem_erase.mp ((hmem v).mp (List.mem_append_right _ (List.mem_cons_of_mem _ hvrr)))).2
645 · intro hvS
646 by_cases hvy : v = y
647 · subst hvy; exact List.mem_append_right _ (List.mem_cons_of_mem _ List.mem_cons_self)
648 · have hvq : v ∈ l ++ x :: r := (hmem v).mpr (Finset.mem_erase.mpr ⟨hvy, hvS⟩)
649 rcases List.mem_append.mp hvq with hvl | hvr
650 · exact List.mem_append_left _ hvl
651 · rcases List.mem_cons.mp hvr with rfl | hvrr
652 · exact List.mem_append_right _ List.mem_cons_self
653 · exact List.mem_append_right _ (List.mem_cons_of_mem _ (List.mem_cons_of_mem _ hvrr))⟩
654 left_inv := fun ⟨p, hnd, _, hlt, hmem, hch⟩ => by
655 have hx_mem : x ∈ p := (hmem x).mpr hxS
656 rcases exists_succ_of_mem_ne_last p x f hlt hx_mem hxf with ⟨w, hadj_xw⟩
657 have hwS : w ∈ S := (hmem w).mp (adjIn_mem_right hadj_xw)
658 have hwy : w = y := hout w hwS (adjIn_edge hch hadj_xw)
659 subst hwy
660 rcases hadj_xw with ⟨l, r, hp⟩
661 apply Subtype.ext
662 dsimp only
663 subst hp
664 rw [contractPath_of_split hnd]
665 have hnd_c : (l ++ x :: r).Nodup := by
666 have h1 := List.nodup_append.mp hnd
667 have h2 := List.nodup_cons.mp h1.2.1
668 have h3 := List.nodup_cons.mp h2.2
669 refine List.nodup_append.mpr ⟨h1.1, List.nodup_cons.mpr ⟨fun hx => h2.1 (List.mem_cons_of_mem _ hx), h3.2⟩, ?_⟩
670 intro a ha b hb
671 apply h1.2.2 a ha b
672 rcases List.mem_cons.mp hb with rfl | hbr
673 · exact List.mem_cons_self
674 · exact List.mem_cons_of_mem _ (List.mem_cons_of_mem _ hbr)
675 exact expandPath_of_split hnd_c
676 right_inv := fun ⟨q, hnd, _, _, hmem, _⟩ => by
677 have hx_mem : x ∈ q := (hmem x).mpr (Finset.mem_erase.mpr ⟨hxy, hxS⟩)
678 rcases List.append_of_mem hx_mem with ⟨l, r, hq⟩
679 apply Subtype.ext
680 dsimp only
681 subst hq
682 rw [expandPath_of_split hnd]
683 have hy_q : y ∉ l ++ x :: r := fun h => (Finset.mem_erase.mp ((hmem y).mp h)).1 rfl
684 have hy_l : y ∉ l := fun h => hy_q (List.mem_append_left _ h)
685 have hy_r : y ∉ r := fun h => hy_q (List.mem_append_right _ (List.mem_cons_of_mem _ h))
686 have hnd_e : (l ++ x :: y :: r).Nodup := by
687 have h1 := List.nodup_append.mp hnd
688 have h2 := List.nodup_cons.mp h1.2.1
689 refine List.nodup_append.mpr ⟨h1.1, List.nodup_cons.mpr ⟨?_, List.nodup_cons.mpr ⟨hy_r, h2.2⟩⟩, ?_⟩
690 · intro hx; rcases List.mem_cons.mp hx with h | h
691 · exact hxy h
692 · exact h2.1 h
693 · intro a ha b hb
694 rcases List.mem_cons.mp hb with rfl | hb1
695 · exact h1.2.2 a ha b List.mem_cons_self
696 · rcases List.mem_cons.mp hb1 with rfl | hbr
697 · intro h; subst h; exact hy_l ha
698 · exact h1.2.2 a ha b (List.mem_cons_of_mem _ hbr)
699 exact contractPath_of_split hnd_e
700
701/-! ## Section 4: Dulmage-Mendelsohn Non-Cover Edge Elimination -/
702
703/-- A bipartite perfect matching (cycle-path cover) of `B(G, S, s, f)`:
704 bijection `M : (S \ {f}) → (S \ {s})` using directed edges of `G`. -/
705structure BipartiteCover {V : Type} [DecidableEq V] (G : Digraph V) (S : Finset V) (s f : V) where
706 next : V → V
707 maps_to : ∀ u ∈ S.erase f, next u ∈ S.erase s
708 edge_ok : ∀ u ∈ S.erase f, G.edge u (next u)
709 inj_on : ∀ u₁ ∈ S.erase f, ∀ u₂ ∈ S.erase f, next u₁ = next u₂ → u₁ = u₂
710 surj_on : ∀ v ∈ S.erase s, ∃ u ∈ S.erase f, next u = v
711
712/-- Every Hamiltonian path `p` induces a `BipartiteCover` containing every edge of `p`. -/
713lemma hamPath_induces_cover {V : Type} [DecidableEq V]
714 (G : Digraph V) (S : Finset V) (s f : V) (p : List V)
715 (hp : IsHamPath G S s f p) :
716 ∃ M : BipartiteCover G S s f, ∀ u v, AdjIn u v p → u ∈ S.erase f ∧ M.next u = v := by
717 rcases hp with ⟨hnd, hhd, hlt, hmem, hch⟩
718 classical
719 let nextFn : V → V := fun u =>
720 if h : u ∈ S.erase f then
721 Classical.choose (exists_succ_of_mem_ne_last p u f hlt
722 ((hmem u).mpr (Finset.mem_erase.mp h).2) (Finset.mem_erase.mp h).1)
723 else u
724 have hspec : ∀ u (hu : u ∈ S.erase f), AdjIn u (nextFn u) p := by
725 intro u hu
726 simp only [nextFn, dite_eq_left_of_eq_true (eq_true hu)]
727 exact Classical.choose_spec (exists_succ_of_mem_ne_last p u f hlt
728 ((hmem u).mpr (Finset.mem_erase.mp hu).2) (Finset.mem_erase.mp hu).1)
729 have hmaps : ∀ u ∈ S.erase f, nextFn u ∈ S.erase s := by
730 intro u hu
731 have hadj := hspec u hu
732 refine Finset.mem_erase.mpr ⟨?_, (hmem _).mp (adjIn_mem_right hadj)⟩
733 intro heq
734 rcases hadj with ⟨l, r, hp_eq⟩
735 rw [heq] at hp_eq
736 cases l with
737 | nil =>
738 rw [hp_eq] at hhd
739 simp at hhd
740 subst hhd
741 exact adjIn_ne hnd ⟨[], r, hp_eq⟩ rfl
742 | cons a l' =>
743 rw [hp_eq] at hhd hnd
744 simp at hhd
745 subst hhd
746 have hs_in : a ∈ l' ++ u :: a :: r :=
747 List.mem_append_right _ (List.mem_cons_of_mem _ List.mem_cons_self)
748 exact (List.nodup_cons.mp hnd).1 hs_in
749 refine ⟨⟨nextFn, hmaps,
750 fun u hu => adjIn_edge hch (hspec u hu),
751 fun u₁ hu₁ u₂ hu₂ heq => by
752 have h1 := hspec u₁ hu₁
753 rw [heq] at h1
754 exact adjIn_unique_pred hnd h1 (hspec u₂ hu₂),
755 fun v hv => by
756 rcases exists_pred_of_mem_ne_head p v s hhd
757 ((hmem v).mpr (Finset.mem_erase.mp hv).2) (Finset.mem_erase.mp hv).1 with ⟨u, hadj_uv⟩
758 have huS : u ∈ S := (hmem u).mp (adjIn_mem_left hadj_uv)
759 have huf : u ≠ f := by
760 intro h; subst h
761 rcases hadj_uv with ⟨l, r, hp_eq⟩
762 rw [hp_eq] at hlt hnd
763 have hlt' : (v :: r).getLast? = some u := by
764 rw [List.getLast?_append_of_ne_nil l (List.cons_ne_nil _ _)] at hlt
765 simpa [List.getLast?_cons_cons] using hlt
766 have hu_in : u ∈ v :: r := List.mem_of_getLast? hlt'
767 have hsub : (u :: v :: r).Nodup := (List.nodup_append.mp hnd).2.1
768 exact (List.nodup_cons.mp hsub).1 hu_in
769 have hu : u ∈ S.erase f := Finset.mem_erase.mpr ⟨huf, huS⟩
770 exact ⟨u, hu, adjIn_unique_succ hnd (hspec u hu) hadj_uv⟩⟩, ?_⟩
771 intro u v hadj
772 have huS : u ∈ S := (hmem u).mp (adjIn_mem_left hadj)
773 have huf : u ≠ f := by
774 intro h; subst h
775 rcases hadj with ⟨l, r, hp_eq⟩
776 rw [hp_eq] at hlt hnd
777 have hlt' : (v :: r).getLast? = some u := by
778 rw [List.getLast?_append_of_ne_nil l (List.cons_ne_nil _ _)] at hlt
779 simpa [List.getLast?_cons_cons] using hlt
780 have hu_in : u ∈ v :: r := List.mem_of_getLast? hlt'
781 have hsub : (u :: v :: r).Nodup := (List.nodup_append.mp hnd).2.1
782 exact (List.nodup_cons.mp hsub).1 hu_in
783 have hu : u ∈ S.erase f := Finset.mem_erase.mpr ⟨huf, huS⟩
784 exact ⟨hu, adjIn_unique_succ hnd (hspec u hu) hadj⟩
785
786/-- Theorem 6 (`dm_noncover_edge_elim_iff`):
787 If edge `(a, b)` participates in ZERO bipartite perfect matchings of `B(G, S, s, f)`
788 (the exact Dulmage-Mendelsohn non-SCC criterion), then deleting `(a, b)` preserves
789 `IsHamPath G S s f p` pointwise (hence exact `#Ham`). -/
790theorem dm_noncover_edge_elim_iff {V : Type} [DecidableEq V]
791 (G : Digraph V) (S : Finset V) (s f a b : V)
792 (hnoncover : ∀ M : BipartiteCover G S s f, a ∈ S.erase f → M.next a ≠ b)
793 (p : List V) :
794 let G' : Digraph V := ⟨fun u v => G.edge u v ∧ ¬(u = a ∧ v = b)⟩
795 IsHamPath G S s f p ↔ IsHamPath G' S s f p := by
796 intro G'
797 constructor
798 · intro hp
799 rcases hamPath_induces_cover G S s f p hp with ⟨M, hM⟩
800 rcases hp with ⟨hnd, hhd, hlt, hmem, hch⟩
801 refine ⟨hnd, hhd, hlt, hmem, ?_⟩
802 apply isChain_mono_on_adj p hch
803 intro u v hadj
804 refine ⟨adjIn_edge hch hadj, ?_⟩
805 rintro ⟨hua, hvb⟩
806 rw [hua, hvb] at hadj
807 rcases hM a b hadj with ⟨hu, hnext⟩
808 exact hnoncover M hu hnext
809 · rintro ⟨hnd, hhd, hlt, hmem, hch⟩
810 refine ⟨hnd, hhd, hlt, hmem, ?_⟩
811 apply isChain_mono_on_adj p hch
812 intro u v hadj
813 exact (adjIn_edge hch hadj).1
814
815/-! ## Section 5: Odd-Modulus Residue-Block Sharding Exactness & Bit-0 Alternation -/
816
817/-- Sum of `f` over a list of `Nat` indices. -/
818def listSum (f : Nat → Nat) : List Nat → Nat
819 | [] => 0
820 | x :: xs => f x + listSum f xs
821
822/-- XOR (F_2 parity) of `f` over a list of `Nat` indices. -/
823def listXor (f : Nat → Bool) : List Nat → Bool
824 | [] => false
825 | x :: xs => xor (f x) (listXor f xs)
826
827lemma listSum_add_filter (f : Nat → Nat) (P : Nat → Bool) :
828 ∀ (xs : List Nat),
829 listSum f (xs.filter P) + listSum f (xs.filter (fun x => !P x)) = listSum f xs
830 | [] => rfl
831 | x :: xs => by
832 cases hP : P x with
833 | true =>
834 simp [List.filter, hP, listSum]
835 have ih := listSum_add_filter f P xs
836 omega
837 | false =>
838 simp [List.filter, hP, listSum]
839 have ih := listSum_add_filter f P xs
840 omega
841
842lemma listXor_xor_filter (f : Nat → Bool) (P : Nat → Bool) :
843 ∀ (xs : List Nat),
844 xor (listXor f (xs.filter P)) (listXor f (xs.filter (fun x => !P x))) = listXor f xs
845 | [] => rfl
846 | x :: xs => by
847 have ih := listXor_xor_filter f P xs
848 cases hP : P x with
849 | true =>
850 simp only [List.filter, hP, Bool.not_true, listXor, ← ih]
851 cases f x <;> cases listXor f (xs.filter P) <;> cases listXor f (xs.filter (fun x => !P x)) <;> rfl
852 | false =>
853 simp only [List.filter, hP, Bool.not_false, listXor, ← ih]
854 cases f x <;> cases listXor f (xs.filter P) <;> cases listXor f (xs.filter (fun x => !P x)) <;> rfl
855
856/-- Sum of `f` over elements of `xs` whose residue mod `M` is `< k`. -/
857def modPrefixSum (M : Nat) (f : Nat → Nat) (xs : List Nat) : Nat → Nat
858 | 0 => 0
859 | k + 1 => modPrefixSum M f xs k + listSum f (xs.filter (fun x => decide (x % M = k)))
860
861/-- XOR of `f` over elements of `xs` whose residue mod `M` is `< k`. -/
862def modPrefixXor (M : Nat) (f : Nat → Bool) (xs : List Nat) : Nat → Bool
863 | 0 => false
864 | k + 1 => xor (modPrefixXor M f xs k) (listXor f (xs.filter (fun x => decide (x % M = k))))
865
866lemma modPrefixSum_eq_filter (M : Nat) (f : Nat → Nat) (xs : List Nat) :
867 ∀ k, modPrefixSum M f xs k = listSum f (xs.filter (fun x => decide (x % M < k)))
868 | 0 => by simp [modPrefixSum, listSum]
869 | k + 1 => by
870 rw [modPrefixSum, modPrefixSum_eq_filter M f xs k]
871 have hpart : ∀ (ys : List Nat),
872 listSum f (ys.filter (fun x => decide (x % M < k))) +
873 listSum f (ys.filter (fun x => decide (x % M = k))) =
874 listSum f (ys.filter (fun x => decide (x % M < k + 1))) := by
875 intro ys
876 induction ys with
877 | nil => rfl
878 | cons y yt ih =>
879 by_cases h1 : y % M < k
880 · have h2 : ¬(y % M = k) := by omega
881 have h3 : y % M < k + 1 := by omega
882 simp [List.filter, h1, h2, h3, listSum]; omega
883 · by_cases h2 : y % M = k
884 · simp [List.filter, h2, listSum]; omega
885 · have h3 : ¬(y % M < k + 1) := by omega
886 simp [List.filter, h1, h2, h3]; exact ih
887 exact hpart xs
888
889lemma modPrefixXor_eq_filter (M : Nat) (f : Nat → Bool) (xs : List Nat) :
890 ∀ k, modPrefixXor M f xs k = listXor f (xs.filter (fun x => decide (x % M < k)))
891 | 0 => by simp [modPrefixXor, listXor]
892 | k + 1 => by
893 rw [modPrefixXor, modPrefixXor_eq_filter M f xs k]
894 have hpart : ∀ (ys : List Nat),
895 xor (listXor f (ys.filter (fun x => decide (x % M < k))))
896 (listXor f (ys.filter (fun x => decide (x % M = k)))) =
897 listXor f (ys.filter (fun x => decide (x % M < k + 1))) := by
898 intro ys
899 induction ys with
900 | nil => rfl
901 | cons y yt ih =>
902 by_cases h1 : y % M < k
903 · have h2 : ¬(y % M = k) := by omega
904 have h3 : y % M < k + 1 := by omega
905 have hd1 : decide (y % M < k) = true := decide_eq_true h1
906 have hd2 : decide (y % M = k) = false := decide_eq_false h2
907 have hd3 : decide (y % M < k + 1) = true := decide_eq_true h3
908 simp only [List.filter_cons, hd1, hd2, hd3, Bool.false_eq_true, ↓reduceIte, listXor, ← ih]
909 cases f y <;> cases listXor f (yt.filter (fun x => decide (x % M < k))) <;>
910 cases listXor f (yt.filter (fun x => decide (x % M = k))) <;> rfl
911 · by_cases h2 : y % M = k
912 · have h3 : y % M < k + 1 := by omega
913 have hd1 : decide (y % M < k) = false := decide_eq_false h1
914 have hd2 : decide (y % M = k) = true := decide_eq_true h2
915 have hd3 : decide (y % M < k + 1) = true := decide_eq_true h3
916 simp only [List.filter_cons, hd1, hd2, hd3, Bool.false_eq_true, ↓reduceIte, listXor, ← ih]
917 cases f y <;> cases listXor f (yt.filter (fun x => decide (x % M < k))) <;>
918 cases listXor f (yt.filter (fun x => decide (x % M = k))) <;> rfl
919 · have h3 : ¬(y % M < k + 1) := by omega
920 have hd1 : decide (y % M < k) = false := decide_eq_false h1
921 have hd2 : decide (y % M = k) = false := decide_eq_false h2
922 have hd3 : decide (y % M < k + 1) = false := decide_eq_false h3
923 simp only [List.filter_cons, hd1, hd2, hd3, Bool.false_eq_true, ↓reduceIte]; exact ih
924 exact hpart xs
925
926/-- Theorem 7a (`odd_mod_block_partition_exact`):
927 For any modulus `M ≥ 1` and any subset list `xs`, summing (for `IE_EXACT`) or XORing
928 (for `IE_PARITY_SWAR`) over all `M` residue classes `r ∈ {0, ..., M-1}` reproduces the
929 exact full sum / parity with zero duplication and zero omission. -/
930theorem odd_mod_block_partition_exact (M : Nat) (hM : 0 < M) (xs : List Nat)
931 (f : Nat → Nat) (g : Nat → Bool) :
932 modPrefixSum M f xs M = listSum f xs ∧ modPrefixXor M g xs M = listXor g xs := by
933 have hall : xs.filter (fun x => decide (x % M < M)) = xs :=
934 List.filter_eq_self.mpr (fun x _ => decide_eq_true (Nat.mod_lt x hM))
935 constructor
936 · rw [modPrefixSum_eq_filter, hall]
937 · rw [modPrefixXor_eq_filter, hall]
938
939/-- Theorem 7b (`odd_mod_flips_bit0`):
940 For any odd modulus `M` (`M % 2 = 1`, e.g. `M = 101` or `M = 153`), stepping by `M`
941 flips bit 0 on every iteration, eliminating bit-0 parity skew across shards. -/
942theorem odd_mod_flips_bit0 (M : Nat) (hodd : M % 2 = 1) (a : Nat) :
943 (a + M) % 2 ≠ a % 2 := by
944 omega
945
946/-! ## Section 6: Four Executed Broken-Model Negative Controls -/
947
948/-- Negative Control 1 (`neg_ctrl_nonforced_contract_unsound`):
949 Contracting an edge `2 → 3` that is NOT forced (`in(3) = {0, 2}` has size 2) breaks
950 Hamiltonian path existence: `G` has path `0 → 3 → 2 → 1` (YES), whereas `contractGraph G 2 3`
951 on `{0, 1, 2}` has NO `0 → ... → 1` Hamiltonian path because `2` loses its incoming edge
952 from `3` (`0 → 2` does not exist)! -/
953def G_nc1 : Digraph (Fin 4) :=
954 ⟨fun u v =>
955 (u = 0 ∧ v = 3) ∨ (u = 3 ∧ v = 2) ∨ (u = 2 ∧ v = 3) ∨ (u = 2 ∧ v = 1)⟩
956
957def S4 : Finset (Fin 4) := {0, 1, 2, 3}
958
959theorem neg_ctrl_nonforced_contract_unsound :
960 (∃ p, IsHamPath G_nc1 S4 0 1 p) ∧
961 ¬(∃ q, IsHamPath (contractGraph G_nc1 2 3) (S4.erase 3) 0 1 q) := by
962 constructor
963 · refine ⟨[0, 3, 2, 1], by decide, rfl, rfl, ?_, ?_⟩
964 · intro v
965 match v with
966 | ⟨0, _⟩ => decide +revert
967 | ⟨1, _⟩ => decide +revert
968 | ⟨2, _⟩ => decide +revert
969 | ⟨3, _⟩ => decide +revert
970 · simp [IsChain, G_nc1]
971 · rintro ⟨q, _, hhd, _, hmem, hch⟩
972 have h2 : (2 : Fin 4) ∈ q := (hmem 2).mpr (by decide)
973 rcases exists_pred_of_mem_ne_head q 2 0 hhd h2 (by decide) with ⟨u, hadj⟩
974 have hu_mem : u ∈ S4.erase 3 := (hmem u).mp (adjIn_mem_left hadj)
975 have hedge : (contractGraph G_nc1 2 3).edge u 2 := adjIn_edge hch hadj
976 match u with
977 | ⟨0, _⟩ => simp [contractGraph, G_nc1] at hedge
978 | ⟨1, _⟩ => simp [contractGraph, G_nc1] at hedge
979 | ⟨2, _⟩ => simp [contractGraph, G_nc1] at hedge
980 | ⟨3, _⟩ => simp [S4] at hu_mem
981
982/-- Negative Control 2 (`neg_ctrl_premature_2cycle_creates_bogus_loop`):
983 If a broken contractor forgets to drop `y → x` (`v ≠ x`) when merging `y = 3` into `x = 2`
984 on a graph with forced `2 → 3` and back-edge `3 → 2`, it creates a bogus self-loop `2 → 2`. -/
985def G_nc2 : Digraph (Fin 4) :=
986 ⟨fun u v => (u = 0 ∧ v = 2) ∨ (u = 2 ∧ v = 3) ∨ (u = 3 ∧ v = 2) ∨ (u = 3 ∧ v = 1)⟩
987
988def brokenContractGraph {V : Type} [DecidableEq V] (G : Digraph V) (x y : V) : Digraph V :=
989 ⟨fun u v => if u = x then G.edge y v else G.edge u v⟩
990
991theorem neg_ctrl_premature_2cycle_creates_bogus_loop :
992 (brokenContractGraph G_nc2 2 3).edge 2 2 ∧
993 ¬(contractGraph G_nc2 2 3).edge 2 2 := by
994 simp [brokenContractGraph, contractGraph, G_nc2]
995
996/-- Negative Control 3 (`neg_ctrl_premature_sf_shortcut_breaks_k2`):
997 Dropping `s → f` when `S.card = 2` (`S = {0, 1}`) falsely destroys the valid trivial tour `[0, 1]`. -/
998def G_nc3 : Digraph (Fin 2) := ⟨fun u v => u = 0 ∧ v = 1⟩
999def S2 : Finset (Fin 2) := {0, 1}
1000
1001theorem neg_ctrl_premature_sf_shortcut_breaks_k2 :
1002 let G_broken : Digraph (Fin 2) := ⟨fun u v => G_nc3.edge u v ∧ ¬(u = 0 ∧ v = 1)⟩
1003 (∃ p, IsHamPath G_nc3 S2 0 1 p) ∧ ¬(∃ p, IsHamPath G_broken S2 0 1 p) := by
1004 intro G_broken
1005 constructor
1006 · refine ⟨[0, 1], by decide, rfl, rfl, ?_, ?_⟩
1007 · intro v
1008 match v with
1009 | ⟨0, _⟩ => decide +revert
1010 | ⟨1, _⟩ => decide +revert
1011 · simp [IsChain, G_nc3]
1012 · rintro ⟨p, _, hhd, hlt, hmem, hch⟩
1013 have h0 : (0 : Fin 2) ∈ p := (hmem 0).mpr (by decide)
1014 rcases exists_succ_of_mem_ne_last p 0 1 hlt h0 (by decide) with ⟨w, hadj⟩
1015 have hedge := adjIn_edge hch hadj
1016 simp [G_broken, G_nc3] at hedge
1017
1018/-- Negative Control 4 (`neg_ctrl_even_mod_bit0_bias`):
1019 For even modulus `M = 2`, every element `x` in residue class `0 mod 2` has bit 0 equal to `0`
1020 (zero odd indices ever land in shard 0), proving why odd `M` (`gcd(M, 2) = 1`) is necessary. -/
1021theorem neg_ctrl_even_mod_bit0_bias (k : Nat) :
1022 (2 * k) % 2 = 0 ∧ ¬((2 * k) % 2 = 1) := by
1023 omega
1024
1025#print axioms forced_out_elim_iff
1026#print axioms forced_in_elim_iff
1027#print axioms premature_2cycle_elim_iff
1028#print axioms premature_sf_shortcut_elim_iff
1029#print axioms chain_contract_bijection
1030#print axioms dm_noncover_edge_elim_iff
1031#print axioms odd_mod_block_partition_exact
1032#print axioms odd_mod_flips_bit0
1033#print axioms neg_ctrl_nonforced_contract_unsound
1034#print axioms neg_ctrl_premature_2cycle_creates_bogus_loop
1035#print axioms neg_ctrl_premature_sf_shortcut_breaks_k2
1036#print axioms neg_ctrl_even_mod_bit0_bias
1037
1038end ForcedChainContraction