/- MisereBlockingAndPassTwoPileNim.lean ============================================================================== Complete Exact Classification of: Part I. Normal k-Blocking Two-Pile Nim (∀ k x y : ℕ) + Exact Constant P-Move Count Law Part II. Misère k-Blocking Two-Pile Nim (∀ k x y : ℕ) — Odd-k Exact Shift Duality & Complete Diagonal Vanishing vs. Even-k Diagonal Threshold (x ≥ k/2 + 2) & Gap-1 Vanishing Theorem (NEW!) Part III. Normal & Misère k-Pass Two-Pile Nim (∀ k x y : ℕ) — Universal Parity Collapse, Exact 3×3 Latin-Square Corner Transposition & Exterior Coincidence (max x y ≥ 3), and Complete Exact 0-Position & 1-Position Characterization (NEW!) 100% self-contained Lean 4 / Mathlib formalization, zero external axioms. -/ import Mathlib open Finset namespace MisereBlockingAndPassTwoPileNim /-! ### 1. Exact `mex`, `swap01`, and Parity-Interval Counting Infrastructure -/ /-- Minimum excluded natural number `mex A` of a finite set `A : Finset ℕ`. -/ def mex (A : Finset ℕ) : ℕ := ((List.range (A.card + 1)).find? (fun x => decide (x ∉ A))).getD (A.card + 1) lemma mex_spec (A : Finset ℕ) : mex A ∉ A ∧ ∀ y < mex A, y ∈ A := by have hex : ∃ x ∈ List.range (A.card + 1), decide (x ∉ A) = true := by by_contra hall push Not at hall have hsub : Finset.range (A.card + 1) ⊆ A := by intro x hx have hx' : x ∈ List.range (A.card + 1) := List.mem_range.mpr (Finset.mem_range.mp hx) have hne := hall x hx' simpa using hne have hle := Finset.card_le_card hsub rw [Finset.card_range] at hle omega obtain ⟨x, hx_mem, hx_pred⟩ := hex have hsome : ((List.range (A.card + 1)).find? (fun y => decide (y ∉ A))).isSome := List.find?_isSome.mpr ⟨x, hx_mem, hx_pred⟩ obtain ⟨m, hm⟩ := Option.isSome_iff_exists.mp hsome have hm_eq : mex A = m := by unfold mex; rw [hm]; rfl rw [hm_eq] have hm_not : m ∉ A := by have := List.find?_some hm simpa using this refine ⟨hm_not, ?_⟩ intro y hy have hmlt : m < A.card + 1 := List.mem_range.mp (List.mem_of_find?_eq_some hm) have hylt : y < A.card + 1 := by omega have hfind := List.find?_eq_some_iff_append.mp hm rcases hfind with ⟨_, xs, ys, hcat, hnot⟩ have hymem : y ∈ xs := by have hyin : y ∈ List.range (A.card + 1) := List.mem_range.mpr hylt rw [hcat] at hyin rcases List.mem_append.mp hyin with h1 | h2 · exact h1 · exfalso rcases List.mem_cons.mp h2 with rfl | h3 · omega · have hsorted : (List.range (A.card + 1)).Pairwise (· < ·) := List.pairwise_lt_range rw [hcat] at hsorted have hlt_my : m < y := (List.pairwise_cons.mp (List.pairwise_append.mp hsorted).2.1).1 y h3 omega have := hnot y hymem simpa using this lemma mex_notMem (A : Finset ℕ) : mex A ∉ A := (mex_spec A).1 lemma mem_of_lt_mex {A : Finset ℕ} {y : ℕ} (hy : y < mex A) : y ∈ A := (mex_spec A).2 y hy lemma mex_eq_iff {A : Finset ℕ} {m : ℕ} : mex A = m ↔ m ∉ A ∧ ∀ y < m, y ∈ A := by constructor · rintro rfl; exact mex_spec A · rintro ⟨hm_not, hm_lt⟩ by_contra hne rcases lt_or_gt_of_ne hne with hlt | hgt · exact mex_notMem A (hm_lt (mex A) hlt) · exact hm_not (mem_of_lt_mex hgt) lemma mex_empty : mex ∅ = 0 := by rw [mex_eq_iff]; exact ⟨by simp, fun _ hy => by omega⟩ lemma mex_insert_ne {A : Finset ℕ} {x : ℕ} (hx : x ≠ mex A) : mex (insert x A) = mex A := by rw [mex_eq_iff] refine ⟨?_, fun y hy => Finset.mem_insert_of_mem (mem_of_lt_mex hy)⟩ simp only [Finset.mem_insert, not_or] exact ⟨hx.symm, mex_notMem A⟩ /-- The `0 ↔ 1` transposition involution `swap01`. -/ def swap01 (x : ℕ) : ℕ := if x = 0 then 1 else if x = 1 then 0 else x @[simp] lemma swap01_zero : swap01 0 = 1 := rfl @[simp] lemma swap01_one : swap01 1 = 0 := rfl lemma swap01_of_ge_two {x : ℕ} (hx : 2 ≤ x) : swap01 x = x := by unfold swap01; split_ifs <;> omega lemma swap01_involutive (x : ℕ) : swap01 (swap01 x) = x := by unfold swap01; by_cases h0 : x = 0 <;> by_cases h1 : x = 1 <;> simp [h0, h1] lemma swap01_eq_zero_iff (x : ℕ) : swap01 x = 0 ↔ x = 1 := by unfold swap01; split_ifs <;> simp_all lemma swap01_eq_one_iff (x : ℕ) : swap01 x = 1 ↔ x = 0 := by unfold swap01; split_ifs <;> simp_all lemma swap01_eq_self_iff {x y : ℕ} (hy : 2 ≤ y) : swap01 x = y ↔ x = y := by unfold swap01; split_ifs <;> (constructor <;> intro <;> omega) /-- Exact `0 ↔ 1` transposition mex law. -/ theorem mex_image_swap01_iff (A : Finset ℕ) : mex (A.image swap01) = swap01 (mex A) ↔ (0 ∈ A ∨ 1 ∈ A) := by constructor · intro heq by_contra hneither; push Not at hneither have hmexA : mex A = 0 := by rw [mex_eq_iff]; exact ⟨hneither.1, fun _ hx => by omega⟩ have h0not : (0 : ℕ) ∉ A.image swap01 := by intro hmem; rcases Finset.mem_image.mp hmem with ⟨x, hxA, hx0⟩ rw [swap01_eq_zero_iff] at hx0; exact hneither.2 (hx0 ▸ hxA) have hmexImg : mex (A.image swap01) = 0 := by rw [mex_eq_iff]; exact ⟨h0not, fun _ hx => by omega⟩ rw [hmexA, hmexImg, swap01_zero] at heq; omega · intro hor by_cases h0 : 0 ∈ A · by_cases h1 : 1 ∈ A · have hge2 : 2 ≤ mex A := by by_contra hlt have : mex A = 0 ∨ mex A = 1 := by omega rcases this with h | h · exact mex_notMem A (h.symm ▸ h0) · exact mex_notMem A (h.symm ▸ h1) rw [swap01_of_ge_two hge2, mex_eq_iff] refine ⟨?_, ?_⟩ · intro hmem; rcases Finset.mem_image.mp hmem with ⟨x, hxA, hxeq⟩ rw [swap01_eq_self_iff hge2] at hxeq; exact mex_notMem A (hxeq ▸ hxA) · intro y hy by_cases hy0 : y = 0 · subst hy0; exact Finset.mem_image.mpr ⟨1, h1, rfl⟩ · by_cases hy1 : y = 1 · subst hy1; exact Finset.mem_image.mpr ⟨0, h0, rfl⟩ · exact Finset.mem_image.mpr ⟨y, mem_of_lt_mex hy, swap01_of_ge_two (by omega)⟩ · have hmexA : mex A = 1 := by rw [mex_eq_iff]; exact ⟨h1, fun x hx => by have hx0 : x = 0 := (by omega); rw [hx0]; exact h0⟩ rw [hmexA, swap01_one, mex_eq_iff] refine ⟨?_, fun _ hx => by omega⟩ intro hmem; rcases Finset.mem_image.mp hmem with ⟨x, hxA, hx0⟩ rw [swap01_eq_zero_iff] at hx0; exact h1 (hx0 ▸ hxA) · have h1 : 1 ∈ A := hor.resolve_left h0 have hmexA : mex A = 0 := by rw [mex_eq_iff]; exact ⟨h0, fun _ hx => by omega⟩ rw [hmexA, swap01_zero, mex_eq_iff] refine ⟨?_, ?_⟩ · intro hmem; rcases Finset.mem_image.mp hmem with ⟨x, hxA, hx1⟩ rw [swap01_eq_one_iff] at hx1; exact h0 (hx1 ▸ hxA) · intro y hy; have hy0 : y = 0 := (by omega); subst hy0 exact Finset.mem_image.mpr ⟨1, h1, rfl⟩ /-- Exact cardinality of elements of `[a, b)` matching parity `p % 2`. -/ lemma card_filter_parity_Ico (a b p : ℕ) : ((Finset.Ico a b).filter (fun z => z % 2 = p % 2)).card = if b ≤ a then 0 else (b - a + if a % 2 = p % 2 then 1 else 0) / 2 := by induction b with | zero => simp only [Nat.zero_le, ↓reduceIte, Finset.Ico_eq_empty (by omega : ¬ a < 0), Finset.filter_empty, Finset.card_empty] | succ b ih => by_cases hba : b < a · have hle : b + 1 ≤ a := by omega simp only [hle, ↓reduceIte, Finset.Ico_eq_empty (by omega : ¬ a < b + 1), Finset.filter_empty, Finset.card_empty] · have hab : a ≤ b := by omega have hsplit : Finset.Ico a (b + 1) = insert b (Finset.Ico a b) := by ext z; simp only [Finset.mem_Ico, Finset.mem_insert]; omega rw [hsplit, Finset.filter_insert, if_neg (by omega : ¬ b + 1 ≤ a)] by_cases hb : b % 2 = p % 2 · rw [if_pos hb, Finset.card_insert_of_notMem (by simp), ih] split_ifs at * <;> omega · rw [if_neg hb, ih] split_ifs at * <;> omega lemma card_filter_range_split (n m : ℕ) (hm : m ≤ n) (P : ℕ → Prop) [DecidablePred P] : ((Finset.range n).filter P).card = ((Finset.range m).filter P).card + ((Finset.Ico m n).filter P).card := by have hunion : Finset.range n = Finset.range m ∪ Finset.Ico m n := by ext z; simp only [Finset.mem_range, Finset.mem_union, Finset.mem_Ico]; omega have hdisj : Disjoint ((Finset.range m).filter P) ((Finset.Ico m n).filter P) := by rw [Finset.disjoint_left] intro z hz1 hz2 simp only [Finset.mem_filter, Finset.mem_range, Finset.mem_Ico] at hz1 hz2 omega rw [hunion, Finset.filter_union, Finset.card_union_of_disjoint hdisj] lemma card_filter_Ico_split (a m b : ℕ) (ham : a ≤ m) (hmb : m ≤ b) (P : ℕ → Prop) [DecidablePred P] : ((Finset.Ico a b).filter P).card = ((Finset.Ico a m).filter P).card + ((Finset.Ico m b).filter P).card := by have hunion : Finset.Ico a b = Finset.Ico a m ∪ Finset.Ico m b := by ext z; simp only [Finset.mem_Ico, Finset.mem_union]; omega have hdisj : Disjoint ((Finset.Ico a m).filter P) ((Finset.Ico m b).filter P) := by rw [Finset.disjoint_left] intro z hz1 hz2 simp only [Finset.mem_filter, Finset.mem_Ico] at hz1 hz2 omega rw [hunion, Finset.filter_union, Finset.card_union_of_disjoint hdisj] /-! ### 2. Two-Pile Nim Move Set `twoPileMoves x y` -/ /-- Legal moves from 2-pile Nim position `(x, y)`: reduce either pile to a strictly smaller size. -/ def twoPileMoves (x y : ℕ) : Finset (ℕ × ℕ) := ((Finset.range x).image (fun a => (a, y))) ∪ ((Finset.range y).image (fun b => (x, b))) lemma mem_twoPileMoves {x y : ℕ} {p : ℕ × ℕ} : p ∈ twoPileMoves x y ↔ (p.1 < x ∧ p.2 = y) ∨ (p.1 = x ∧ p.2 < y) := by rcases p with ⟨a, b⟩ simp only [twoPileMoves, Finset.mem_union, Finset.mem_image, Finset.mem_range, Prod.mk.injEq] constructor · rintro (⟨a', ha', rfl, rfl⟩ | ⟨b', hb', rfl, rfl⟩) · left; exact ⟨ha', rfl⟩ · right; exact ⟨rfl, hb'⟩ · rintro (⟨ha, rfl⟩ | ⟨rfl, hb⟩) · left; exact ⟨a, ha, rfl, rfl⟩ · right; exact ⟨b, hb, rfl, rfl⟩ lemma mem_twoPileMoves_sum_lt {x y : ℕ} {p : ℕ × ℕ} (hp : p ∈ twoPileMoves x y) : p.1 + p.2 < x + y := by rcases mem_twoPileMoves.mp hp with ⟨h1, h2⟩ | ⟨h1, h2⟩ <;> omega lemma disjoint_twoPileMoves (x y : ℕ) : Disjoint ((Finset.range x).image (fun a => (a, y))) ((Finset.range y).image (fun b => (x, b))) := by rw [Finset.disjoint_left] intro p hp1 hp2 rcases Finset.mem_image.mp hp1 with ⟨a, ha, rfl⟩ rcases Finset.mem_image.mp hp2 with ⟨b, hb, heq⟩ rw [Finset.mem_range] at ha hb have h1 : a = x := (Prod.mk.inj heq).1.symm omega lemma card_twoPileMoves (x y : ℕ) : (twoPileMoves x y).card = x + y := by rw [twoPileMoves, Finset.card_union_of_disjoint (disjoint_twoPileMoves x y), Finset.card_image_of_injective _ (fun a₁ a₂ h => (Prod.mk.inj h).1), Finset.card_image_of_injective _ (fun b₁ b₂ h => (Prod.mk.inj h).2), Finset.card_range, Finset.card_range] lemma card_filter_twoPileMoves (x y : ℕ) (P : ℕ × ℕ → Prop) [DecidablePred P] : ((twoPileMoves x y).filter P).card = ((Finset.range x).filter (fun a => P (a, y))).card + ((Finset.range y).filter (fun b => P (x, b))).card := by rw [twoPileMoves, Finset.filter_union, Finset.card_union_of_disjoint (Finset.disjoint_filter_filter (disjoint_twoPileMoves x y)), Finset.filter_image, Finset.filter_image, Finset.card_image_of_injective _ (fun a₁ a₂ h => (Prod.mk.inj h).1), Finset.card_image_of_injective _ (fun b₁ b₂ h => (Prod.mk.inj h).2)] lemma twoPileMoves_nonempty_iff (x y : ℕ) : (twoPileMoves x y).Nonempty ↔ 0 < x + y := by rw [← Finset.card_pos, card_twoPileMoves] lemma twoPileMoves_swap (x y : ℕ) : (twoPileMoves x y).image Prod.swap = twoPileMoves y x := by ext ⟨a, b⟩ simp only [Finset.mem_image, mem_twoPileMoves, Prod.exists, Prod.swap_prod_mk, Prod.mk.injEq] constructor · rintro ⟨u, v, (⟨h1, h2⟩ | ⟨h1, h2⟩), rfl, rfl⟩ · right; exact ⟨h2, h1⟩ · left; exact ⟨h2, h1⟩ · rintro (⟨h1, h2⟩ | ⟨h1, h2⟩) · exact ⟨b, a, Or.inr ⟨h2, h1⟩, rfl, rfl⟩ · exact ⟨b, a, Or.inl ⟨h2, h1⟩, rfl, rfl⟩ /-! ### 3. PART I: Normal `k`-Blocking Two-Pile Nim — Complete Closed Form & Constant `P`-Count Law -/ /-- Normal-play `k`-blocking 2-pile Nim outcome (`false = 𝒫`, `true = 𝒩`). -/ def normBlock2Nim (k x y : ℕ) : Bool := decide (k < (((twoPileMoves x y).attach.image (fun m => (m.1, normBlock2Nim k m.1.1 m.1.2))).filter (fun p => !p.2)).card) termination_by x + y decreasing_by exact mem_twoPileMoves_sum_lt m.2 lemma normBlock2Nim_eq (k x y : ℕ) : normBlock2Nim k x y = decide (k < ((twoPileMoves x y).filter (fun p => normBlock2Nim k p.1 p.2 = false)).card) := by rw [normBlock2Nim] congr 1 have hcard : (((twoPileMoves x y).attach.image (fun m => (m.1, normBlock2Nim k m.1.1 m.1.2))).filter (fun p => !p.2)).card = ((twoPileMoves x y).filter (fun p => normBlock2Nim k p.1 p.2 = false)).card := by apply Finset.card_bij (fun p _ => p.1) · intro p hp rw [Finset.mem_filter, Finset.mem_image] at hp rcases hp.1 with ⟨m, _, rfl⟩ exact Finset.mem_filter.mpr ⟨m.2, by simpa [Bool.not_eq_true'] using hp.2⟩ · intro p₁ hp₁ p₂ hp₂ h rw [Finset.mem_filter, Finset.mem_image] at hp₁ hp₂ rcases hp₁.1 with ⟨m₁, _, rfl⟩ rcases hp₂.1 with ⟨m₂, _, rfl⟩ dsimp only at h exact Prod.ext h (by rw [h]) · intro q hq rw [Finset.mem_filter] at hq refine ⟨(q, normBlock2Nim k q.1 q.2), Finset.mem_filter.mpr ?_, rfl⟩ exact ⟨Finset.mem_image.mpr ⟨⟨q, hq.1⟩, Finset.mem_attach _ _, rfl⟩, by simp [hq.2]⟩ rw [hcard] theorem normBlock2Nim_comm (k : ℕ) : ∀ x y, normBlock2Nim k x y = normBlock2Nim k y x := by intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => rw [normBlock2Nim_eq, normBlock2Nim_eq] congr 1 have hcard : ((twoPileMoves x y).filter (fun p => normBlock2Nim k p.1 p.2 = false)).card = ((twoPileMoves y x).filter (fun p => normBlock2Nim k p.1 p.2 = false)).card := by apply Finset.card_bij (fun p _ => p.swap) · intro p hp rw [Finset.mem_filter] at hp ⊢ refine ⟨by rw [← twoPileMoves_swap]; exact Finset.mem_image.mpr ⟨p, hp.1, rfl⟩, ?_⟩ have hlt := mem_twoPileMoves_sum_lt hp.1 simp only [Prod.fst_swap, Prod.snd_swap] rw [← ih (p.1 + p.2) (by omega) p.1 p.2 rfl]; exact hp.2 · intro p₁ _ p₂ _ h; exact Prod.swap_injective h · intro q hq rw [Finset.mem_filter] at hq have hq' : q.swap ∈ twoPileMoves x y := by rw [← twoPileMoves_swap]; exact Finset.mem_image.mpr ⟨q, hq.1, rfl⟩ refine ⟨q.swap, Finset.mem_filter.mpr ⟨hq', ?_⟩, Prod.swap_swap q⟩ have hlt := mem_twoPileMoves_sum_lt hq' rw [ih (q.swap.1 + q.swap.2) (by omega) q.swap.1 q.swap.2 rfl] simpa using hq.2 rw [hcard] /-- Closed-form `𝒫`-position predicate for Normal `k`-blocking 2-pile Nim: `(x, y)` is `𝒫` iff `min x y + max x y ≤ k` OR (`max x y - min x y ≤ k ∧ (max x y - min x y) % 2 = k % 2`). -/ def normBlock2P (k x y : ℕ) : Prop := x + y ≤ k ∨ (max x y - min x y ≤ k ∧ (max x y - min x y) % 2 = k % 2) instance (k x y : ℕ) : Decidable (normBlock2P k x y) := by unfold normBlock2P; infer_instance lemma normBlock2P_comm (k x y : ℕ) : normBlock2P k x y ↔ normBlock2P k y x := by simp only [normBlock2P, add_comm, max_comm, min_comm] /-- Helper: Exact count of `a ∈ range x` satisfying `normBlock2P k a y` when `x ≤ y`! -/ lemma card_normBlock2P_left (k x y : ℕ) (hxy : x ≤ y) : ((Finset.range x).filter (fun a => normBlock2P k a y)).card = min x (k + 1 - y) + if x ≤ max (min x (k + 1 - y)) (y - k) then 0 else (x - max (min x (k + 1 - y)) (y - k) + if (max (min x (k + 1 - y)) (y - k)) % 2 = (y + k) % 2 then 1 else 0) / 2 := by set m := min x (k + 1 - y) with hm_def rw [card_filter_range_split x m (Nat.min_le_left _ _)] have hpart1 : ((Finset.range m).filter (fun a => normBlock2P k a y)).card = m := by have hall : ∀ a ∈ Finset.range m, normBlock2P k a y := by intro a ha; rw [Finset.mem_range] at ha left; omega rw [Finset.filter_true_of_mem hall, Finset.card_range] rw [hpart1] congr 1 set lo := max m (y - k) with hlo_def by_cases hle : x ≤ lo · rw [if_pos hle] have hemp : ((Finset.Ico m x).filter (fun a => normBlock2P k a y)) = ∅ := by apply Finset.eq_empty_of_forall_notMem intro a ha rw [Finset.mem_filter, Finset.mem_Ico] at ha rcases ha.2 with h1 | ⟨h2, _⟩ · omega · have hmax : max a y = y := Nat.max_eq_right (by omega) have hmin : min a y = a := Nat.min_eq_left (by omega) rw [hmax, hmin] at h2; omega rw [hemp, Finset.card_empty] · rw [if_neg hle] have hlo_ge : m ≤ lo := le_max_left _ _ rw [card_filter_Ico_split m lo x hlo_ge (by omega)] have hzero : ((Finset.Ico m lo).filter (fun a => normBlock2P k a y)).card = 0 := by have hemp : ((Finset.Ico m lo).filter (fun a => normBlock2P k a y)) = ∅ := by apply Finset.eq_empty_of_forall_notMem intro a ha rw [Finset.mem_filter, Finset.mem_Ico] at ha rcases ha.2 with h1 | ⟨h2, _⟩ · omega · have hmax : max a y = y := Nat.max_eq_right (by omega) have hmin : min a y = a := Nat.min_eq_left (by omega) rw [hmax, hmin] at h2; omega rw [hemp, Finset.card_empty] rw [hzero, zero_add] have hfilt_eq : (Finset.Ico lo x).filter (fun a => normBlock2P k a y) = (Finset.Ico lo x).filter (fun a => a % 2 = (y + k) % 2) := by apply Finset.filter_congr intro a ha; rw [Finset.mem_Ico] at ha have hmax : max a y = y := Nat.max_eq_right (by omega) have hmin : min a y = a := Nat.min_eq_left (by omega) simp only [normBlock2P, hmax, hmin] constructor · rintro (h1 | ⟨_, hpar⟩) <;> omega · intro hpar; right; exact ⟨by omega, by omega⟩ rw [hfilt_eq, card_filter_parity_Ico, if_neg hle] /-- Helper: Exact count of `b ∈ Ico x y` satisfying `normBlock2P k x b` when `x ≤ y`! -/ lemma card_normBlock2P_mid (k x y : ℕ) (hxy : x ≤ y) : ((Finset.Ico x y).filter (fun b => normBlock2P k x b)).card = (max x (min y (k + 1 - x)) - x) + if min y (x + k + 1) ≤ max x (min y (k + 1 - x)) then 0 else (min y (x + k + 1) - max x (min y (k + 1 - x)) + if (max x (min y (k + 1 - x))) % 2 = (x + k) % 2 then 1 else 0) / 2 := by set m := max x (min y (k + 1 - x)) with hm_def have hxm : x ≤ m := le_max_left _ _ have hmy : m ≤ y := max_le hxy (Nat.min_le_left _ _) rw [card_filter_Ico_split x m y hxm hmy] have hpart1 : ((Finset.Ico x m).filter (fun b => normBlock2P k x b)).card = m - x := by have hall : ∀ b ∈ Finset.Ico x m, normBlock2P k x b := by intro b hb; rw [Finset.mem_Ico] at hb; left; omega rw [Finset.filter_true_of_mem hall, Nat.card_Ico] rw [hpart1] congr 1 set hi := min y (x + k + 1) with hhi_def by_cases hle : hi ≤ m · rw [if_pos hle] have hemp : (Finset.Ico m y).filter (fun b => normBlock2P k x b) = ∅ := by apply Finset.eq_empty_of_forall_notMem intro b hb; rw [Finset.mem_filter, Finset.mem_Ico] at hb rcases hb.2 with h1 | ⟨h2, _⟩ · omega · have hmax : max x b = b := Nat.max_eq_right (by omega) have hmin : min x b = x := Nat.min_eq_left (by omega) rw [hmax, hmin] at h2; omega rw [hemp, Finset.card_empty] · rw [if_neg hle] have hhi_le : hi ≤ y := Nat.min_le_left _ _ rw [card_filter_Ico_split m hi y (by omega) hhi_le] have hzero : ((Finset.Ico hi y).filter (fun b => normBlock2P k x b)).card = 0 := by have hemp : (Finset.Ico hi y).filter (fun b => normBlock2P k x b) = ∅ := by apply Finset.eq_empty_of_forall_notMem intro b hb; rw [Finset.mem_filter, Finset.mem_Ico] at hb rcases hb.2 with h1 | ⟨h2, _⟩ · omega · have hmax : max x b = b := Nat.max_eq_right (by omega) have hmin : min x b = x := Nat.min_eq_left (by omega) rw [hmax, hmin] at h2; omega rw [hemp, Finset.card_empty] rw [hzero, add_zero] have hfilt_eq : (Finset.Ico m hi).filter (fun b => normBlock2P k x b) = (Finset.Ico m hi).filter (fun b => b % 2 = (x + k) % 2) := by apply Finset.filter_congr intro b hb; rw [Finset.mem_Ico] at hb have hmax : max x b = b := Nat.max_eq_right (by omega) have hmin : min x b = x := Nat.min_eq_left (by omega) simp only [normBlock2P, hmax, hmin] constructor · rintro (h1 | ⟨_, hpar⟩) <;> omega · intro hpar; right; exact ⟨by omega, by omega⟩ rw [hfilt_eq, card_filter_parity_Ico, if_neg hle] /-- **THEOREM 1a (Exact Constant `P`-Move Count Law for Normal `k`-Blocking 2-Pile Nim):** For EVERY `k, x, y` with `k < x + y`: - If `normBlock2P k x y`, the number of `𝒫`-moves is **EXACTLY `k`**; - If `¬ normBlock2P k x y`, the number of `𝒫`-moves is **EXACTLY `k + 1`**! -/ theorem normBlock2P_Pcnt_exact (k x y : ℕ) (hgt : k < x + y) : ((twoPileMoves x y).filter (fun p => normBlock2P k p.1 p.2)).card = if normBlock2P k x y then k else k + 1 := by wlog hxy : x ≤ y generalizing x y · have hswap : ((twoPileMoves x y).filter (fun p => normBlock2P k p.1 p.2)).card = ((twoPileMoves y x).filter (fun p => normBlock2P k p.1 p.2)).card := by apply Finset.card_bij (fun p _ => p.swap) · intro p hp; rw [Finset.mem_filter] at hp ⊢ exact ⟨by rw [← twoPileMoves_swap]; exact Finset.mem_image.mpr ⟨p, hp.1, rfl⟩, (normBlock2P_comm k p.1 p.2).mp hp.2⟩ · intro p₁ _ p₂ _ h; exact Prod.swap_injective h · intro q hq; rw [Finset.mem_filter] at hq have hq' : q.swap ∈ twoPileMoves x y := by rw [← twoPileMoves_swap]; exact Finset.mem_image.mpr ⟨q, hq.1, rfl⟩ refine ⟨q.swap, Finset.mem_filter.mpr ⟨hq', ?_⟩, Prod.swap_swap q⟩ simp only [Prod.fst_swap, Prod.snd_swap] exact (normBlock2P_comm k q.1 q.2).mp hq.2 rw [hswap, this y x (by omega) (by omega)] simp [normBlock2P_comm] rw [card_filter_twoPileMoves, card_filter_range_split y x hxy] have hcomm_r : ((Finset.range x).filter (fun b => normBlock2P k x b)).card = ((Finset.range x).filter (fun a => normBlock2P k a x)).card := by congr 1; apply Finset.filter_congr; intro a _; exact normBlock2P_comm k x a rw [hcomm_r] have hT1 : ((Finset.range x).filter (fun a => normBlock2P k a y)).card = if y ≤ k then (k + 1 - y) + (x - (k + 1 - y)) / 2 else if x ≤ y - k then 0 else (x - (y - k) + 1) / 2 := by rw [card_normBlock2P_left k x y hxy] by_cases hyk : y ≤ k · rw [if_pos hyk] have h1 : min x (k + 1 - y) = k + 1 - y := by omega have h2 : y - k = 0 := by omega rw [h1, h2, max_eq_left (Nat.zero_le _)] by_cases hle : x ≤ k + 1 - y · rw [if_pos hle]; omega · rw [if_neg hle, if_neg (by omega), add_zero] · rw [if_neg hyk] have h1 : min x (k + 1 - y) = 0 := by omega rw [h1, max_eq_right (Nat.zero_le _), zero_add] by_cases hle : x ≤ y - k · rw [if_pos hle, if_pos hle] · rw [if_neg hle, if_neg hle, if_pos (by omega)] have hT2 : ((Finset.range x).filter (fun a => normBlock2P k a x)).card = if 2 * x ≤ k then x else if x ≤ k then (k + 1 - x) + (x - (k + 1 - x)) / 2 else (k + 1) / 2 := by rw [card_normBlock2P_left k x x le_rfl] by_cases h2x : 2 * x ≤ k · rw [if_pos h2x] have h1 : min x (k + 1 - x) = x := by omega rw [h1, if_pos (by omega)]; omega · rw [if_neg h2x] by_cases hxk : x ≤ k · rw [if_pos hxk] have h1 : min x (k + 1 - x) = k + 1 - x := by omega have h2 : x - k = 0 := by omega rw [h1, h2, max_eq_left (Nat.zero_le _)] by_cases hle : x ≤ k + 1 - x · rw [if_pos hle]; omega · rw [if_neg hle, if_neg (by omega), add_zero] · rw [if_neg hxk] have h1 : min x (k + 1 - x) = 0 := by omega rw [h1, max_eq_right (Nat.zero_le _), zero_add] by_cases hk0 : k = 0 · subst hk0; simp · rw [if_neg (by omega), if_pos (by omega)]; congr 1; omega have hT3 : ((Finset.Ico x y).filter (fun b => normBlock2P k x b)).card = if 2 * x ≤ k then (k + 1 - 2 * x) + (min y (x + k + 1) - (k + 1 - x)) / 2 else (min y (x + k + 1) - x + if k % 2 = 0 then 1 else 0) / 2 := by rw [card_normBlock2P_mid k x y hxy] have hmin : min y (k + 1 - x) = k + 1 - x := by omega rw [hmin] by_cases h2x : 2 * x ≤ k · rw [if_pos h2x] have hmax : max x (k + 1 - x) = k + 1 - x := by omega rw [hmax] by_cases hle : min y (x + k + 1) ≤ k + 1 - x · rw [if_pos hle]; omega · rw [if_neg hle, if_neg (by omega)]; congr 1; omega · rw [if_neg h2x] have hmax : max x (k + 1 - x) = x := by omega rw [hmax, Nat.sub_self, zero_add] by_cases hle : min y (x + k + 1) ≤ x · rw [if_pos hle]; split_ifs <;> omega · rw [if_neg hle] have hpar : (x % 2 = (x + k) % 2) ↔ k % 2 = 0 := by omega by_cases hk0 : k % 2 = 0 · rw [if_pos hk0, if_pos (hpar.mpr hk0)] · rw [if_neg hk0, if_neg (fun h => hk0 (hpar.mp h))] rw [hT1, hT2, hT3] clear hcomm_r hT1 hT2 hT3 have hmax : max x y = y := Nat.max_eq_right hxy have hmin : min x y = x := Nat.min_eq_left hxy have hP_iff : normBlock2P k x y ↔ y - x ≤ k ∧ (y - x) % 2 = k % 2 := by simp only [normBlock2P, hmax, hmin]; omega by_cases hP : y - x ≤ k ∧ (y - x) % 2 = k % 2 · rw [if_pos (hP_iff.mpr hP)] clear hmax hmin hP_iff have hmin_y : min y (x + k + 1) = y := by omega rw [hmin_y] by_cases h2x : 2 * x ≤ k · rw [if_pos h2x, if_pos h2x]; split_ifs <;> omega · rw [if_neg h2x, if_neg h2x]; split_ifs <;> omega · rw [if_neg (fun h => hP (hP_iff.mp h))] clear hmax hmin hP_iff by_cases hdiff : y ≤ x + k · have hmin_y : min y (x + k + 1) = y := by omega rw [hmin_y] by_cases h2x : 2 * x ≤ k · rw [if_pos h2x, if_pos h2x]; split_ifs <;> omega · rw [if_neg h2x, if_neg h2x]; split_ifs <;> omega · have hmin_y : min y (x + k + 1) = x + k + 1 := by omega rw [hmin_y] by_cases h2x : 2 * x ≤ k · rw [if_pos h2x, if_pos h2x]; split_ifs <;> omega · rw [if_neg h2x, if_neg h2x]; split_ifs <;> omega /-- **THEOREM 1 (Complete Exact Classification of Normal `k`-Blocking Two-Pile Nim):** For ALL `k, x, y : ℕ`: `normBlock2Nim k x y = false ↔ x + y ≤ k ∨ (max x y - min x y ≤ k ∧ (max x y - min x y) % 2 = k % 2)`! -/ theorem normBlock2Nim_exact (k : ℕ) : ∀ x y, normBlock2Nim k x y = decide (¬ normBlock2P k x y) := by intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => rw [normBlock2Nim_eq] have hfilt : (twoPileMoves x y).filter (fun p => normBlock2Nim k p.1 p.2 = false) = (twoPileMoves x y).filter (fun p => normBlock2P k p.1 p.2) := by apply Finset.filter_congr intro p hp rw [ih (p.1 + p.2) (by have := mem_twoPileMoves_sum_lt hp; omega) p.1 p.2 rfl] simp rw [hfilt] by_cases hle : x + y ≤ k · have hp : normBlock2P k x y := Or.inl hle have hcard_le : ((twoPileMoves x y).filter (fun p => normBlock2P k p.1 p.2)).card ≤ k := le_trans (Finset.card_filter_le _ _) (by rw [card_twoPileMoves]; exact hle) simp [hp, not_lt.mpr hcard_le] · rw [normBlock2P_Pcnt_exact k x y (by omega)] by_cases hp : normBlock2P k x y <;> simp [hp] /-! ### 4. PART II: Complete Exact Classification of Misère `k`-Blocking Two-Pile Nim (NEW!) -/ /-- Misère `k`-blocking 2-pile Nim outcome (`false = 𝒫`, `true = 𝒩`): the current player wins iff either strictly more than `k` moves lead to `𝒫`-positions OR ALL legal moves lead to `𝒫`-positions (so blocking all `𝒫`-moves leaves zero moves, an immediate misère win). -/ def misBlock2Nim (k x y : ℕ) : Bool := let Pcnt := (((twoPileMoves x y).attach.image (fun m => (m.1, misBlock2Nim k m.1.1 m.1.2))).filter (fun p => !p.2)).card decide (k < Pcnt ∨ Pcnt = (twoPileMoves x y).card) termination_by x + y decreasing_by exact mem_twoPileMoves_sum_lt m.2 lemma misBlock2Nim_eq (k x y : ℕ) : misBlock2Nim k x y = decide (k < ((twoPileMoves x y).filter (fun p => misBlock2Nim k p.1 p.2 = false)).card ∨ ((twoPileMoves x y).filter (fun p => misBlock2Nim k p.1 p.2 = false)).card = (twoPileMoves x y).card) := by rw [misBlock2Nim] have hcard : (((twoPileMoves x y).attach.image (fun m => (m.1, misBlock2Nim k m.1.1 m.1.2))).filter (fun p => !p.2)).card = ((twoPileMoves x y).filter (fun p => misBlock2Nim k p.1 p.2 = false)).card := by apply Finset.card_bij (fun p _ => p.1) · intro p hp rw [Finset.mem_filter, Finset.mem_image] at hp rcases hp.1 with ⟨m, _, rfl⟩ exact Finset.mem_filter.mpr ⟨m.2, by simpa [Bool.not_eq_true'] using hp.2⟩ · intro p₁ hp₁ p₂ hp₂ h rw [Finset.mem_filter, Finset.mem_image] at hp₁ hp₂ rcases hp₁.1 with ⟨m₁, _, rfl⟩ rcases hp₂.1 with ⟨m₂, _, rfl⟩ dsimp only at h exact Prod.ext h (by rw [h]) · intro q hq rw [Finset.mem_filter] at hq refine ⟨(q, misBlock2Nim k q.1 q.2), Finset.mem_filter.mpr ?_, rfl⟩ exact ⟨Finset.mem_image.mpr ⟨⟨q, hq.1⟩, Finset.mem_attach _ _, rfl⟩, by simp [hq.2]⟩ rw [hcard] theorem misBlock2Nim_comm (k : ℕ) : ∀ x y, misBlock2Nim k x y = misBlock2Nim k y x := by intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => rw [misBlock2Nim_eq, misBlock2Nim_eq, card_twoPileMoves, card_twoPileMoves, add_comm y x] have hcard : ((twoPileMoves x y).filter (fun p => misBlock2Nim k p.1 p.2 = false)).card = ((twoPileMoves y x).filter (fun p => misBlock2Nim k p.1 p.2 = false)).card := by apply Finset.card_bij (fun p _ => p.swap) · intro p hp rw [Finset.mem_filter] at hp ⊢ refine ⟨by rw [← twoPileMoves_swap]; exact Finset.mem_image.mpr ⟨p, hp.1, rfl⟩, ?_⟩ have hlt := mem_twoPileMoves_sum_lt hp.1 simp only [Prod.fst_swap, Prod.snd_swap] rw [← ih (p.1 + p.2) (by omega) p.1 p.2 rfl]; exact hp.2 · intro p₁ _ p₂ _ h; exact Prod.swap_injective h · intro q hq rw [Finset.mem_filter] at hq have hq' : q.swap ∈ twoPileMoves x y := by rw [← twoPileMoves_swap]; exact Finset.mem_image.mpr ⟨q, hq.1, rfl⟩ refine ⟨q.swap, Finset.mem_filter.mpr ⟨hq', ?_⟩, Prod.swap_swap q⟩ have hlt := mem_twoPileMoves_sum_lt hq' rw [ih (q.swap.1 + q.swap.2) (by omega) q.swap.1 q.swap.2 rfl] simpa using hq.2 rw [hcard] /-- Unified closed-form `𝒫`-position predicate for Misère `k`-blocking 2-pile Nim: - **Odd `k` (`k % 2 = 1`)**: `x ≠ y ∧ normBlock2P k (min x y) (max x y - 1)` (ZERO diagonal `𝒫`-positions + exact larger-pile shift duality!); - **Even `k` (`k % 2 = 0`)**: - On diagonal (`x = y`): `k / 2 + 2 ≤ x` (exact diagonal threshold generalizing `(2, 2)` at `k = 0`!); - Off diagonal (`x ≠ y`): `x + y ≤ k + 1 ∨ (3 ≤ max x y - min x y ≤ k + 1 ∧ (max x y - min x y) % 2 = 1)` (exact Gap-1 vanishing outside the finite triangle!). -/ def misBlock2P (k x y : ℕ) : Prop := if k % 2 = 1 then min x y < max x y ∧ normBlock2P k (min x y) (max x y - 1) else if x = y then k / 2 + 2 ≤ x else x + y ≤ k + 1 ∨ (3 ≤ max x y - min x y ∧ max x y - min x y ≤ k + 1 ∧ (max x y - min x y) % 2 = 1) instance (k x y : ℕ) : Decidable (misBlock2P k x y) := by unfold misBlock2P; infer_instance lemma misBlock2P_comm (k x y : ℕ) : misBlock2P k x y ↔ misBlock2P k y x := by unfold misBlock2P rw [min_comm, max_comm, add_comm x y] by_cases hodd : k % 2 = 1 · simp [hodd] · simp only [hodd, ↓reduceIte] by_cases hxy : x = y · subst hxy; simp · have hyx : y ≠ x := Ne.symm hxy simp [hxy, hyx] /-- **THEOREM 2a (Odd-`k` Exact Misère Shift Duality & Complete Diagonal Vanishing — NEW!):** For EVERY odd `k` (`k % 2 = 1`) and EVERY `x, y : ℕ`: 1. `misBlock2Nim k x x = true` (ZERO diagonal `𝒫`-positions!); 2. For ALL `x < y`: `misBlock2Nim k x y = normBlock2Nim k x (y - 1)`! 3. Exact closed form: `misBlock2Nim k x y = decide (¬ misBlock2P k x y)`. -/ private lemma card_normBlock2P_diag_succ (k x : ℕ) (hodd : k % 2 = 1) (hx : x ≠ 0) : ((Finset.range (x - 1)).filter (fun b => normBlock2P k b (x - 1))).card + (if 2 * (x - 1) ≤ k then 1 else 0) = ((Finset.range x).filter (fun a => normBlock2P k a x)).card := by rw [card_normBlock2P_left k (x - 1) (x - 1) le_rfl, card_normBlock2P_left k x x le_rfl] by_cases h1 : 2 * x ≤ k · rw [if_pos (by omega : 2 * (x - 1) ≤ k), min_eq_left (by omega : x - 1 ≤ k + 1 - (x - 1)), min_eq_left (by omega : x ≤ k + 1 - x), if_pos (by omega), if_pos (by omega)]; omega · by_cases h2 : 2 * (x - 1) ≤ k · rw [if_pos h2, min_eq_left (by omega : x - 1 ≤ k + 1 - (x - 1)), min_eq_left (by omega : x ≤ k + 1 - x), if_pos (by omega), if_pos (by omega)]; omega · rw [if_neg h2, add_zero] have hm1 : min (x - 1) (k + 1 - (x - 1)) = k + 1 - (x - 1) := by omega have hm2 : min x (k + 1 - x) = k + 1 - x := by omega rw [hm1, hm2] by_cases hxk : x ≤ k · rw [show x - 1 - k = 0 by omega, show x - k = 0 by omega, max_eq_left (Nat.zero_le _), max_eq_left (Nat.zero_le _)] by_cases hle1 : x - 1 ≤ k + 1 - (x - 1) · rw [if_pos hle1, if_neg (by omega : ¬ x ≤ k + 1 - x), if_neg (by omega)]; omega · rw [if_neg hle1, if_neg (by omega : ¬ x ≤ k + 1 - x), if_neg (by omega), if_neg (by omega)]; omega · by_cases hxk1 : x = k + 1 · subst hxk1 rw [show k + 1 - (k + 1 - 1) = 1 by omega, show k + 1 - (k + 1) = 0 by omega, show k + 1 - 1 - k = 0 by omega, show k + 1 - k = 1 by omega, max_eq_left (Nat.zero_le _), max_eq_right (Nat.zero_le _), zero_add] by_cases hk1 : k = 1 · subst hk1; decide · rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_pos (by omega)]; omega · rw [show k + 1 - (x - 1) = 0 by omega, show k + 1 - x = 0 by omega, max_eq_right (Nat.zero_le _), max_eq_right (Nat.zero_le _), zero_add, zero_add, if_neg (by omega : ¬ x - 1 ≤ x - 1 - k), if_pos (by omega : (x - 1 - k) % 2 = (x - 1 + k) % 2), if_neg (by omega : ¬ x ≤ x - k), if_pos (by omega : (x - k) % 2 = (x + k) % 2)] congr 1; omega theorem misBlock2Nim_odd_exact {k : ℕ} (hodd : k % 2 = 1) : ∀ x y, misBlock2Nim k x y = decide (¬ misBlock2P k x y) := by intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => wlog hxy : x ≤ y generalizing x y · rw [misBlock2Nim_comm, this y x (by omega) (by omega)] simp [misBlock2P_comm] rw [misBlock2Nim_eq, card_twoPileMoves] have hfilt : (twoPileMoves x y).filter (fun p => misBlock2Nim k p.1 p.2 = false) = (twoPileMoves x y).filter (fun p => misBlock2P k p.1 p.2) := by apply Finset.filter_congr intro p hp rw [ih (p.1 + p.2) (by have := mem_twoPileMoves_sum_lt hp; omega) p.1 p.2 rfl] simp rw [hfilt, card_filter_twoPileMoves] -- Compute BOTH `L = (range x).filter (misBlock2P k · y)` and `R = (range y).filter (misBlock2P k x ·)`! by_cases heq : x = y · -- **Diagonal case `(x, x)`**: ALWAYS `𝒩` (`misBlock2P k x x = False`)! subst heq have hnotP : ¬ misBlock2P k x x := by simp [misBlock2P, hodd] simp only [hnotP, not_false_eq_true, decide_true, decide_eq_true_eq] have hR_eq_L : ((Finset.range x).filter (fun b => misBlock2P k x b)).card = ((Finset.range x).filter (fun a => misBlock2P k a x)).card := by congr 1; apply Finset.filter_congr; intro a _; exact misBlock2P_comm k x a rw [hR_eq_L] by_cases hx0 : x = 0 · subst hx0; right; simp · have hL_eq : (Finset.range x).filter (fun a => misBlock2P k a x) = (Finset.range x).filter (fun a => normBlock2P k a (x - 1)) := by apply Finset.filter_congr; intro a ha; rw [Finset.mem_range] at ha have hmin : min a x = a := Nat.min_eq_left (by omega) have hmax : max a x = x := Nat.max_eq_right (by omega) simp [misBlock2P, hodd, hmin, hmax, ha] rw [hL_eq] -- Split `range x` into `range (x - 1)` and `{x - 1}`! have hsplit : ((Finset.range x).filter (fun a => normBlock2P k a (x - 1))).card = ((Finset.range (x - 1)).filter (fun a => normBlock2P k a (x - 1))).card + if normBlock2P k (x - 1) (x - 1) then 1 else 0 := by have hun : Finset.range x = insert (x - 1) (Finset.range (x - 1)) := by have : x = (x - 1) + 1 := by omega conv_lhs => rw [this, Finset.range_add_one] rw [hun, Finset.filter_insert] split_ifs with h · rw [Finset.card_insert_of_notMem (by simp)] · omega rw [hsplit, card_normBlock2P_left k (x - 1) (x - 1) le_rfl] have hself : normBlock2P k (x - 1) (x - 1) ↔ 2 * (x - 1) ≤ k := by simp only [normBlock2P, max_self, min_self, Nat.sub_self, Nat.zero_mod, hodd] omega by_cases hsmall : 2 * (x - 1) ≤ k · -- `x ≤ (k + 1) / 2`: ALL `2 * x` moves are `𝒫`-moves! have h1 : normBlock2P k (x - 1) (x - 1) := hself.mpr hsmall right; rw [if_pos h1]; split_ifs <;> omega · -- `x > (k + 1) / 2`: `|L| = (k + 1) / 2`, so `|L| + |L| = k + 1 > k`! have h0 : ¬ normBlock2P k (x - 1) (x - 1) := fun h => hsmall (hself.mp h) left; rw [if_neg h0]; split_ifs <;> omega · -- **Off-diagonal case `x < y`**: `misBlock2P k x y ↔ normBlock2P k x (y - 1)`! have hlt : x < y := lt_of_le_of_ne hxy heq have hmin_xy : min x y = x := Nat.min_eq_left hxy have hmax_xy : max x y = y := Nat.max_eq_right hxy have hP_iff : misBlock2P k x y ↔ normBlock2P k x (y - 1) := by simp [misBlock2P, hodd, hmin_xy, hmax_xy, hlt] -- Left count: `a < x < y` ⇒ `misBlock2P k a y ↔ normBlock2P k a (y - 1)`! have hL_eq : (Finset.range x).filter (fun a => misBlock2P k a y) = (Finset.range x).filter (fun a => normBlock2P k a (y - 1)) := by apply Finset.filter_congr; intro a ha; rw [Finset.mem_range] at ha simp [misBlock2P, hodd, Nat.min_eq_left (by omega : a ≤ y), Nat.max_eq_right (by omega : a ≤ y), show a < y by omega] -- Right count: split `range y` into `range x`, `{x}` (NOT `𝒫`!), and `Ico (x + 1) y` (bijects with `Ico x (y - 1)`!) rw [card_filter_range_split y x hxy] have hR1 : (Finset.range x).filter (fun b => misBlock2P k x b) = (Finset.range x).filter (fun b => normBlock2P k b (x - 1)) := by apply Finset.filter_congr; intro b hb; rw [Finset.mem_range] at hb simp [misBlock2P, hodd, Nat.min_eq_right (by omega : b ≤ x), Nat.max_eq_left (by omega : b ≤ x), hb] have hR2 : ((Finset.Ico x y).filter (fun b => misBlock2P k x b)).card = ((Finset.Ico x (y - 1)).filter (fun b' => normBlock2P k x b')).card := by have hsplit_x : Finset.Ico x y = insert x (Finset.Ico (x + 1) y) := by ext z; simp only [Finset.mem_Ico, Finset.mem_insert]; omega rw [hsplit_x, Finset.filter_insert, if_neg (by simp [misBlock2P, hodd])] apply Finset.card_bij (fun b _ => b - 1) · intro b hb; rw [Finset.mem_filter, Finset.mem_Ico] at hb ⊢ have hmin : min x b = x := Nat.min_eq_left (by omega) have hmax : max x b = b := Nat.max_eq_right (by omega) simp only [misBlock2P, hodd, ↓reduceIte, hmin, hmax] at hb exact ⟨by omega, hb.2.2⟩ · intro b₁ hb₁ b₂ hb₂ h; rw [Finset.mem_filter, Finset.mem_Ico] at hb₁ hb₂; omega · intro b' hb'; rw [Finset.mem_filter, Finset.mem_Ico] at hb' refine ⟨b' + 1, Finset.mem_filter.mpr ⟨Finset.mem_Ico.mpr ⟨by omega, by omega⟩, ?_⟩, by omega⟩ simp only [misBlock2P, hodd, ↓reduceIte, Nat.min_eq_left (by omega : x ≤ b' + 1), Nat.max_eq_right (by omega : x ≤ b' + 1)] exact ⟨by omega, by simpa using hb'.2⟩ rw [hL_eq, hR1, hR2] by_cases hx0 : x = 0 · subst hx0 simp only [Finset.range_zero, Finset.filter_empty, Finset.card_empty, zero_add] rw [card_normBlock2P_mid k 0 (y - 1) (Nat.zero_le _)] simp only [hP_iff, normBlock2P, Nat.min_eq_left (Nat.zero_le _), Nat.max_eq_right (Nat.zero_le _), zero_add, Nat.sub_zero] by_cases hy1 : y - 1 ≤ k · rw [min_eq_left (by omega : y - 1 ≤ k + 1), if_pos le_rfl, add_zero] have hP : y - 1 ≤ k := hy1 simp [hP]; omega · rw [min_eq_right (by omega : k + 1 ≤ y - 1), if_pos le_rfl, add_zero] have hlt_k : k < k + 1 := Nat.lt_succ_self k simp [hlt_k]; omega · have hR1_card : ((Finset.range x).filter (fun b => normBlock2P k b (x - 1))).card = ((Finset.range (x - 1)).filter (fun b => normBlock2P k b (x - 1))).card + if 2 * (x - 1) ≤ k then 1 else 0 := by have hun : Finset.range x = insert (x - 1) (Finset.range (x - 1)) := by have : x = (x - 1) + 1 := by omega conv_lhs => rw [this, Finset.range_add_one] have hself : normBlock2P k (x - 1) (x - 1) ↔ 2 * (x - 1) ≤ k := by simp only [normBlock2P, max_self, min_self, Nat.sub_self, Nat.zero_mod, hodd]; omega rw [hun, Finset.filter_insert] by_cases hsmall : 2 * (x - 1) ≤ k · rw [if_pos (hself.mpr hsmall), if_pos hsmall, Finset.card_insert_of_notMem (by simp)] · rw [if_neg (fun h => hsmall (hself.mp h)), if_neg hsmall, add_zero] rw [hR1_card] -- Case `x + (y - 1) ≤ k`: ALL moves except `(x, x)` are `𝒫`-moves, so total `< x + y` and `≤ k`! by_cases htri : x + (y - 1) ≤ k · have hP : normBlock2P k x (y - 1) := Or.inl htri have hT1_le : ((Finset.range x).filter (fun a => normBlock2P k a (y - 1))).card ≤ x := le_trans (Finset.card_filter_le _ _) (by rw [Finset.card_range]) have hT2_le : ((Finset.range (x - 1)).filter (fun b => normBlock2P k b (x - 1))).card ≤ x - 1 := le_trans (Finset.card_filter_le _ _) (by rw [Finset.card_range]) have hT3_le : ((Finset.Ico x (y - 1)).filter (fun b' => normBlock2P k x b')).card ≤ y - 1 - x := le_trans (Finset.card_filter_le _ _) (by rw [Nat.card_Ico]) rw [if_pos (by omega : 2 * (x - 1) ≤ k)] simp only [hP_iff, hP, not_true_eq_false, decide_false, decide_eq_false_iff_not, not_or] omega · -- `k < x + (y - 1)`: reuse `normBlock2P_Pcnt_exact k x (y - 1)` directly! have hgt_n : k < x + (y - 1) := by omega have hnorm := normBlock2P_Pcnt_exact k x (y - 1) hgt_n rw [card_filter_twoPileMoves, card_filter_range_split (y - 1) x (by omega)] at hnorm have hcomm_r : ((Finset.range x).filter (fun b => normBlock2P k x b)).card = ((Finset.range x).filter (fun a => normBlock2P k a x)).card := by congr 1; apply Finset.filter_congr; intro a _; exact normBlock2P_comm k x a rw [hcomm_r] at hnorm -- Compare `((range (x-1)).filter (normBlock2P k · (x-1))).card + (if 2*(x-1) ≤ k then 1 else 0)` -- with `((range x).filter (normBlock2P k · x)).card`! have hdiff_xx := card_normBlock2P_diag_succ k x hodd hx0 have htot : ((Finset.range x).filter (fun a => normBlock2P k a (y - 1))).card + (((Finset.range (x - 1)).filter (fun b => normBlock2P k b (x - 1))).card + (if 2 * (x - 1) ≤ k then 1 else 0) + ((Finset.Ico x (y - 1)).filter (fun b' => normBlock2P k x b')).card) = if normBlock2P k x (y - 1) then k else k + 1 := by rw [hdiff_xx]; convert hnorm using 1 rw [htot] by_cases hp : normBlock2P k x (y - 1) · have hpm : misBlock2P k x y := hP_iff.mpr hp rw [if_pos hp]; simp [hpm]; omega · have hpm : ¬ misBlock2P k x y := fun h => hp (hP_iff.mp h) rw [if_neg hp]; simp [hpm] /-- Helper: Exact count of `a ∈ range x` satisfying `misBlock2P k a y` when `k % 2 = 0` and `x ≤ y`! -/ lemma card_misBlock2P_even_left {k : ℕ} (hev : k % 2 = 0) (x y : ℕ) (hxy : x ≤ y) : ((Finset.range x).filter (fun a => misBlock2P k a y)).card = min x (k + 2 - y) + if min x (y - 2) ≤ max (min x (k + 2 - y)) (y - (k + 1)) then 0 else (min x (y - 2) - max (min x (k + 2 - y)) (y - (k + 1)) + if (max (min x (k + 2 - y)) (y - (k + 1))) % 2 = (y + 1) % 2 then 1 else 0) / 2 := by -- For EVERY `a < x ≤ y`, `a ≠ y` and `a < y`, so: -- `misBlock2P k a y ↔ a + y ≤ k + 1 ∨ (3 ≤ y - a ∧ y - a ≤ k + 1 ∧ (y - a) % 2 = 1)`! set m := min x (k + 2 - y) with hm_def rw [card_filter_range_split x m (Nat.min_le_left _ _)] have hpart1 : ((Finset.range m).filter (fun a => misBlock2P k a y)).card = m := by have hall : ∀ a ∈ Finset.range m, misBlock2P k a y := by intro a ha; rw [Finset.mem_range] at ha simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, show a ≠ y by omega] left; omega rw [Finset.filter_true_of_mem hall, Finset.card_range] rw [hpart1]; congr 1 set lo := max m (y - (k + 1)) with hlo_def set hi := min x (y - 2) with hhi_def by_cases hle : hi ≤ lo · rw [if_pos hle] have hemp : (Finset.Ico m x).filter (fun a => misBlock2P k a y) = ∅ := by apply Finset.eq_empty_of_forall_notMem intro a ha; rw [Finset.mem_filter, Finset.mem_Ico] at ha simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, show a ≠ y by omega, Nat.max_eq_right (by omega : a ≤ y), Nat.min_eq_left (by omega : a ≤ y)] at ha rcases ha.2 with h1 | ⟨h3, h2, _⟩ <;> omega rw [hemp, Finset.card_empty] · rw [if_neg hle] rw [card_filter_Ico_split m lo x (le_max_left _ _) (by omega), card_filter_Ico_split lo hi x (by omega) (Nat.min_le_left _ _)] have hzero1 : ((Finset.Ico m lo).filter (fun a => misBlock2P k a y)).card = 0 := by have hemp : (Finset.Ico m lo).filter (fun a => misBlock2P k a y) = ∅ := by apply Finset.eq_empty_of_forall_notMem intro a ha; rw [Finset.mem_filter, Finset.mem_Ico] at ha simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, show a ≠ y by omega, Nat.max_eq_right (by omega : a ≤ y), Nat.min_eq_left (by omega : a ≤ y)] at ha rcases ha.2 with h1 | ⟨h3, h2, _⟩ <;> omega rw [hemp, Finset.card_empty] have hzero2 : ((Finset.Ico hi x).filter (fun a => misBlock2P k a y)).card = 0 := by have hemp : (Finset.Ico hi x).filter (fun a => misBlock2P k a y) = ∅ := by apply Finset.eq_empty_of_forall_notMem intro a ha; rw [Finset.mem_filter, Finset.mem_Ico] at ha simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, show a ≠ y by omega, Nat.max_eq_right (by omega : a ≤ y), Nat.min_eq_left (by omega : a ≤ y)] at ha rcases ha.2 with h1 | ⟨h3, h2, _⟩ <;> omega rw [hemp, Finset.card_empty] rw [hzero1, hzero2, zero_add, add_zero] have hfilt_eq : (Finset.Ico lo hi).filter (fun a => misBlock2P k a y) = (Finset.Ico lo hi).filter (fun a => a % 2 = (y + 1) % 2) := by apply Finset.filter_congr; intro a ha; rw [Finset.mem_Ico] at ha simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, show a ≠ y by omega, Nat.max_eq_right (by omega : a ≤ y), Nat.min_eq_left (by omega : a ≤ y)] constructor · rintro (h1 | ⟨_, _, hpar⟩) <;> omega · intro hpar; right; exact ⟨by omega, by omega, by omega⟩ rw [hfilt_eq, card_filter_parity_Ico, if_neg hle] /-- Helper: Exact count of `b ∈ Ico (x + 1) y` satisfying `misBlock2P k x b` when `k % 2 = 0` and `x < y`! -/ lemma card_misBlock2P_even_mid {k : ℕ} (hev : k % 2 = 0) (x y : ℕ) (hxy : x < y) : ((Finset.Ico (x + 1) y).filter (fun b => misBlock2P k x b)).card = (max (x + 1) (min y (k + 2 - x)) - (x + 1)) + if min y (x + k + 2) ≤ max (max (x + 1) (min y (k + 2 - x))) (x + 3) then 0 else (min y (x + k + 2) - max (max (x + 1) (min y (k + 2 - x))) (x + 3) + if (max (max (x + 1) (min y (k + 2 - x))) (x + 3)) % 2 = (x + 1) % 2 then 1 else 0) / 2 := by set m := max (x + 1) (min y (k + 2 - x)) with hm_def have hxm : x + 1 ≤ m := le_max_left _ _ have hmy : m ≤ y := max_le (by omega) (Nat.min_le_left _ _) rw [card_filter_Ico_split (x + 1) m y hxm hmy] have hpart1 : ((Finset.Ico (x + 1) m).filter (fun b => misBlock2P k x b)).card = m - (x + 1) := by have hall : ∀ b ∈ Finset.Ico (x + 1) m, misBlock2P k x b := by intro b hb; rw [Finset.mem_Ico] at hb simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, show x ≠ b by omega] left; omega rw [Finset.filter_true_of_mem hall, Nat.card_Ico] rw [hpart1]; congr 1 set lo := max m (x + 3) with hlo_def set hi := min y (x + k + 2) with hhi_def by_cases hle : hi ≤ lo · rw [if_pos hle] have hemp : (Finset.Ico m y).filter (fun b => misBlock2P k x b) = ∅ := by apply Finset.eq_empty_of_forall_notMem intro b hb; rw [Finset.mem_filter, Finset.mem_Ico] at hb simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, show x ≠ b by omega, Nat.max_eq_right (by omega : x ≤ b), Nat.min_eq_left (by omega : x ≤ b)] at hb rcases hb.2 with h1 | ⟨h3, h2, _⟩ <;> omega rw [hemp, Finset.card_empty] · rw [if_neg hle] rw [card_filter_Ico_split m lo y (le_max_left _ _) (by omega), card_filter_Ico_split lo hi y (by omega) (Nat.min_le_left _ _)] have hzero1 : ((Finset.Ico m lo).filter (fun b => misBlock2P k x b)).card = 0 := by have hemp : (Finset.Ico m lo).filter (fun b => misBlock2P k x b) = ∅ := by apply Finset.eq_empty_of_forall_notMem intro b hb; rw [Finset.mem_filter, Finset.mem_Ico] at hb simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, show x ≠ b by omega, Nat.max_eq_right (by omega : x ≤ b), Nat.min_eq_left (by omega : x ≤ b)] at hb rcases hb.2 with h1 | ⟨h3, h2, _⟩ <;> omega rw [hemp, Finset.card_empty] have hzero2 : ((Finset.Ico hi y).filter (fun b => misBlock2P k x b)).card = 0 := by have hemp : (Finset.Ico hi y).filter (fun b => misBlock2P k x b) = ∅ := by apply Finset.eq_empty_of_forall_notMem intro b hb; rw [Finset.mem_filter, Finset.mem_Ico] at hb simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, show x ≠ b by omega, Nat.max_eq_right (by omega : x ≤ b), Nat.min_eq_left (by omega : x ≤ b)] at hb rcases hb.2 with h1 | ⟨h3, h2, _⟩ <;> omega rw [hemp, Finset.card_empty] rw [hzero1, hzero2, zero_add, add_zero] have hfilt_eq : (Finset.Ico lo hi).filter (fun b => misBlock2P k x b) = (Finset.Ico lo hi).filter (fun b => b % 2 = (x + 1) % 2) := by apply Finset.filter_congr; intro b hb; rw [Finset.mem_Ico] at hb simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, show x ≠ b by omega, Nat.max_eq_right (by omega : x ≤ b), Nat.min_eq_left (by omega : x ≤ b)] constructor · rintro (h1 | ⟨_, _, hpar⟩) <;> omega · intro hpar; right; exact ⟨by omega, by omega, by omega⟩ rw [hfilt_eq, card_filter_parity_Ico, if_neg hle] /-- **THEOREM 2b (Even-`k` Diagonal Threshold & Gap-1 Vanishing Theorem for Misère `k`-Blocking 2-Pile Nim — NEW!):** For EVERY even `k` (`k % 2 = 0`, unifying classical Misère 2-Pile Nim at `k = 0` with all even `k ≥ 2`!) and EVERY `x, y : ℕ`: `misBlock2Nim k x y = decide (¬ misBlock2P k x y)`! -/ private lemma misBlock2P_even_arith_P (k x y : ℕ) (hev : k % 2 = 0) (hlt : x < y) (htri : ¬ x + y ≤ k + 1) (hP : 3 ≤ y - x ∧ y - x ≤ k + 1 ∧ (y - x) % 2 = 1) : let T1 := if y ≤ k + 1 then (k + 2 - y) + (min x (y - 2) - (k + 2 - y)) / 2 else if min x (y - 2) ≤ y - (k + 1) then 0 else (min x (y - 2) - (y - (k + 1)) + 1) / 2 let T2 := if 2 * x ≤ k + 2 then x else if x ≤ k + 1 then (k + 2 - x) + (x - 2 - (k + 2 - x)) / 2 else k / 2 let T3 := if 2 * x + 1 ≤ k + 1 then (k + 2 - x - (x + 1)) + if min y (x + k + 2) ≤ max (k + 2 - x) (x + 3) then 0 else (min y (x + k + 2) - max (k + 2 - x) (x + 3) + if (max (k + 2 - x) (x + 3)) % 2 = (x + 1) % 2 then 1 else 0) / 2 else if min y (x + k + 2) ≤ x + 3 then 0 else (min y (x + k + 2) - (x + 3) + 1) / 2 let D := if k / 2 + 2 ≤ x then 1 else 0 ¬ (k < T1 + (T2 + (T3 + D)) ∨ T1 + (T2 + (T3 + D)) = x + y) := by intro T1 T2 T3 D dsimp only [T1, T2, T3, D] rw [show min y (x + k + 2) = y by omega, show min x (y - 2) = x by omega] by_cases h2x : 2 * x ≤ k · rw [if_pos (by omega : 2 * x + 1 ≤ k + 1), if_pos (by omega : 2 * x ≤ k + 2), if_neg (by omega : ¬ k / 2 + 2 ≤ x)] by_cases hmx : k + 2 - x ≤ x + 3 · rw [max_eq_right hmx] by_cases hyk : y ≤ k + 1 · rw [if_pos hyk]; split_ifs <;> omega · rw [if_neg hyk] by_cases hle1 : x ≤ y - (k + 1) · rw [if_pos hle1]; split_ifs <;> omega · rw [if_neg hle1]; split_ifs <;> omega · rw [max_eq_left (by omega : x + 3 ≤ k + 2 - x)] by_cases hyk : y ≤ k + 1 · rw [if_pos hyk]; split_ifs <;> omega · rw [if_neg hyk] by_cases hle1 : x ≤ y - (k + 1) · rw [if_pos hle1]; split_ifs <;> omega · rw [if_neg hle1]; split_ifs <;> omega · rw [if_neg (by omega : ¬ 2 * x + 1 ≤ k + 1)] by_cases h2x2 : 2 * x ≤ k + 2 · rw [if_pos h2x2, if_neg (by omega : ¬ k / 2 + 2 ≤ x)] by_cases hyk : y ≤ k + 1 · rw [if_pos hyk]; split_ifs <;> omega · rw [if_neg hyk]; split_ifs <;> omega · rw [if_neg h2x2, if_pos (by omega : k / 2 + 2 ≤ x)] by_cases hxk : x ≤ k + 1 · rw [if_pos hxk] by_cases hyk : y ≤ k + 1 · rw [if_pos hyk]; split_ifs <;> omega · rw [if_neg hyk]; split_ifs <;> omega · rw [if_neg hxk, if_neg (by omega : ¬ y ≤ k + 1)]; split_ifs <;> omega private lemma misBlock2P_even_arith_NP (k x y : ℕ) (hev : k % 2 = 0) (hlt : x < y) (htri : ¬ x + y ≤ k + 1) (hNP : ¬ (3 ≤ y - x ∧ y - x ≤ k + 1 ∧ (y - x) % 2 = 1)) : let T1 := if y ≤ k + 1 then (k + 2 - y) + (min x (y - 2) - (k + 2 - y)) / 2 else if min x (y - 2) ≤ y - (k + 1) then 0 else (min x (y - 2) - (y - (k + 1)) + 1) / 2 let T2 := if 2 * x ≤ k + 2 then x else if x ≤ k + 1 then (k + 2 - x) + (x - 2 - (k + 2 - x)) / 2 else k / 2 let T3 := if 2 * x + 1 ≤ k + 1 then (k + 2 - x - (x + 1)) + if min y (x + k + 2) ≤ max (k + 2 - x) (x + 3) then 0 else (min y (x + k + 2) - max (k + 2 - x) (x + 3) + if (max (k + 2 - x) (x + 3)) % 2 = (x + 1) % 2 then 1 else 0) / 2 else if min y (x + k + 2) ≤ x + 3 then 0 else (min y (x + k + 2) - (x + 3) + 1) / 2 let D := if k / 2 + 2 ≤ x then 1 else 0 k < T1 + (T2 + (T3 + D)) := by intro T1 T2 T3 D dsimp only [T1, T2, T3, D] by_cases hdiff : y ≤ x + k + 1 · rw [show min y (x + k + 2) = y by omega] by_cases hxy2 : x ≤ y - 2 · rw [min_eq_left hxy2] by_cases h2x : 2 * x ≤ k · rw [if_pos (by omega : 2 * x + 1 ≤ k + 1), if_pos (by omega : 2 * x ≤ k + 2), if_neg (by omega : ¬ k / 2 + 2 ≤ x)] by_cases hmx : k + 2 - x ≤ x + 3 · rw [max_eq_right hmx] by_cases hyk : y ≤ k + 1 · rw [if_pos hyk]; split_ifs <;> omega · rw [if_neg hyk] by_cases hle1 : x ≤ y - (k + 1) · rw [if_pos hle1]; split_ifs <;> omega · rw [if_neg hle1]; split_ifs <;> omega · rw [max_eq_left (by omega : x + 3 ≤ k + 2 - x)] by_cases hyk : y ≤ k + 1 · rw [if_pos hyk]; split_ifs <;> omega · rw [if_neg hyk] by_cases hle1 : x ≤ y - (k + 1) · rw [if_pos hle1]; split_ifs <;> omega · rw [if_neg hle1]; split_ifs <;> omega · rw [if_neg (by omega : ¬ 2 * x + 1 ≤ k + 1)] by_cases h2x2 : 2 * x ≤ k + 2 · rw [if_pos h2x2, if_neg (by omega : ¬ k / 2 + 2 ≤ x)] by_cases hyk : y ≤ k + 1 · rw [if_pos hyk]; split_ifs <;> omega · rw [if_neg hyk]; split_ifs <;> omega · rw [if_neg h2x2, if_pos (by omega : k / 2 + 2 ≤ x)] by_cases hxk : x ≤ k + 1 · rw [if_pos hxk] by_cases hyk : y ≤ k + 1 · rw [if_pos hyk]; split_ifs <;> omega · rw [if_neg hyk]; split_ifs <;> omega · rw [if_neg hxk, if_neg (by omega : ¬ y ≤ k + 1)]; split_ifs <;> omega · -- `y = x + 1`! have hy1 : y = x + 1 := by omega subst hy1 rw [show min x (x + 1 - 2) = x - 1 by omega] by_cases h2x : 2 * x ≤ k · rw [if_pos (by omega : 2 * x + 1 ≤ k + 1), if_pos (by omega : 2 * x ≤ k + 2), if_neg (by omega : ¬ k / 2 + 2 ≤ x), if_pos (by omega : x + 1 ≤ max (k + 2 - x) (x + 3)), add_zero] split_ifs <;> omega · rw [if_neg (by omega : ¬ 2 * x + 1 ≤ k + 1), if_pos (by omega : x + 1 ≤ x + 3)] by_cases h2x2 : 2 * x ≤ k + 2 · rw [if_pos h2x2, if_neg (by omega : ¬ k / 2 + 2 ≤ x)]; split_ifs <;> omega · rw [if_neg h2x2, if_pos (by omega : k / 2 + 2 ≤ x)] by_cases hxk : x ≤ k + 1 · rw [if_pos hxk]; split_ifs <;> omega · rw [if_neg hxk]; split_ifs <;> omega · rw [show min y (x + k + 2) = x + k + 2 by omega, if_neg (by omega : ¬ y ≤ k + 1), if_pos (by omega : min x (y - 2) ≤ y - (k + 1))] by_cases h2x : 2 * x ≤ k · rw [if_pos (by omega : 2 * x + 1 ≤ k + 1), if_pos (by omega : 2 * x ≤ k + 2), if_neg (by omega : ¬ k / 2 + 2 ≤ x)] by_cases hmx : k + 2 - x ≤ x + 3 · rw [max_eq_right hmx]; split_ifs <;> omega · rw [max_eq_left (by omega : x + 3 ≤ k + 2 - x)]; split_ifs <;> omega · rw [if_neg (by omega : ¬ 2 * x + 1 ≤ k + 1)] by_cases h2x2 : 2 * x ≤ k + 2 · rw [if_pos h2x2, if_neg (by omega : ¬ k / 2 + 2 ≤ x)]; split_ifs <;> omega · rw [if_neg h2x2, if_pos (by omega : k / 2 + 2 ≤ x)] by_cases hxk : x ≤ k + 1 · rw [if_pos hxk]; split_ifs <;> omega · rw [if_neg hxk]; split_ifs <;> omega theorem misBlock2Nim_even_exact {k : ℕ} (hev : k % 2 = 0) : ∀ x y, misBlock2Nim k x y = decide (¬ misBlock2P k x y) := by intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => wlog hxy : x ≤ y generalizing x y · rw [misBlock2Nim_comm, this y x (by omega) (by omega)] simp [misBlock2P_comm] rw [misBlock2Nim_eq, card_twoPileMoves] have hfilt : (twoPileMoves x y).filter (fun p => misBlock2Nim k p.1 p.2 = false) = (twoPileMoves x y).filter (fun p => misBlock2P k p.1 p.2) := by apply Finset.filter_congr; intro p hp rw [ih (p.1 + p.2) (by have := mem_twoPileMoves_sum_lt hp; omega) p.1 p.2 rfl] simp rw [hfilt, card_filter_twoPileMoves] dsimp only by_cases heq : x = y · subst heq have hR_eq_L : ((Finset.range x).filter (fun b => misBlock2P k x b)).card = ((Finset.range x).filter (fun a => misBlock2P k a x)).card := by congr 1; apply Finset.filter_congr; intro a _; exact misBlock2P_comm k x a rw [hR_eq_L] have hL_simp : ((Finset.range x).filter (fun a => misBlock2P k a x)).card = if 2 * x ≤ k + 2 then x else if x ≤ k + 1 then (k + 2 - x) + (x - 2 - (k + 2 - x)) / 2 else k / 2 := by rw [card_misBlock2P_even_left hev x x le_rfl] by_cases h1 : 2 * x ≤ k + 2 · rw [if_pos h1, min_eq_left (by omega : x ≤ k + 2 - x), if_pos (by omega)]; omega · rw [if_neg h1] by_cases h2 : x ≤ k + 1 · rw [if_pos h2, min_eq_right (by omega : k + 2 - x ≤ x), show x - (k + 1) = 0 by omega, max_eq_left (Nat.zero_le _), min_eq_right (by omega : x - 2 ≤ x)] by_cases h3 : x - 2 ≤ k + 2 - x · rw [if_pos h3]; omega · rw [if_neg h3, if_neg (by omega), add_zero] · rw [if_neg h2, min_eq_right (by omega : k + 2 - x ≤ x), show k + 2 - x = 0 by omega, max_eq_right (Nat.zero_le _), zero_add, min_eq_right (by omega : x - 2 ≤ x)] by_cases hk0 : k = 0 · subst hk0; rw [if_pos (by omega)] · rw [if_neg (by omega), if_pos (by omega)]; omega rw [hL_simp] simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte] by_cases h1 : 2 * x ≤ k + 2 · rw [if_pos h1]; simp only [show ¬ k / 2 + 2 ≤ x by omega, not_false_eq_true, decide_true, decide_eq_true_eq] exact Or.inr trivial · rw [if_neg h1] have h3 : k / 2 + 2 ≤ x := by omega by_cases h2 : x ≤ k + 1 · rw [if_pos h2]; simp [h3]; omega · rw [if_neg h2]; simp [h3]; omega · have hlt : x < y := lt_of_le_of_ne hxy heq rw [card_filter_range_split y x hxy] have hR1 : ((Finset.range x).filter (fun b => misBlock2P k x b)).card = ((Finset.range x).filter (fun a => misBlock2P k a x)).card := by congr 1; apply Finset.filter_congr; intro a _; exact misBlock2P_comm k x a have hsplit_x : Finset.Ico x y = insert x (Finset.Ico (x + 1) y) := by ext z; simp only [Finset.mem_Ico, Finset.mem_insert]; omega rw [hR1, hsplit_x, Finset.filter_insert] have hIco_card : (if misBlock2P k x x then insert x ((Finset.Ico (x + 1) y).filter (fun b => misBlock2P k x b)) else (Finset.Ico (x + 1) y).filter (fun b => misBlock2P k x b)).card = ((Finset.Ico (x + 1) y).filter (fun b => misBlock2P k x b)).card + if k / 2 + 2 ≤ x then 1 else 0 := by have hdiag_iff : misBlock2P k x x ↔ k / 2 + 2 ≤ x := by simp [misBlock2P, show k % 2 ≠ 1 by omega] by_cases hx : k / 2 + 2 ≤ x · rw [if_pos (hdiag_iff.mpr hx), if_pos hx, Finset.card_insert_of_notMem (by simp)] · rw [if_neg (fun h => hx (hdiag_iff.mp h)), if_neg hx, add_zero] rw [hIco_card] -- Simplify the 3 counts cleanly by regime! by_cases htri : x + y ≤ k + 1 · -- Inside the finite triangle `x + y ≤ k + 1`: ALL moves are `𝒫`-moves EXCEPT `(x, x)` (since `x ≤ k/2 < k/2 + 2`)! have hP : misBlock2P k x y := by simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, heq]; left; exact htri have hT1_le : ((Finset.range x).filter (fun a => misBlock2P k a y)).card ≤ x := le_trans (Finset.card_filter_le _ _) (by rw [Finset.card_range]) have hT2_le : ((Finset.range x).filter (fun a => misBlock2P k a x)).card ≤ x := le_trans (Finset.card_filter_le _ _) (by rw [Finset.card_range]) have hT3_le : ((Finset.Ico (x + 1) y).filter (fun b => misBlock2P k x b)).card ≤ y - (x + 1) := le_trans (Finset.card_filter_le _ _) (by rw [Nat.card_Ico]) rw [if_neg (by omega : ¬ k / 2 + 2 ≤ x)] simp only [hP, not_true_eq_false, decide_false, decide_eq_false_iff_not, not_or] omega · -- Exterior `k + 1 < x + y`: simplify `T1, T2, T3` cleanly! have hT1 : ((Finset.range x).filter (fun a => misBlock2P k a y)).card = if y ≤ k + 1 then (k + 2 - y) + (min x (y - 2) - (k + 2 - y)) / 2 else if min x (y - 2) ≤ y - (k + 1) then 0 else (min x (y - 2) - (y - (k + 1)) + 1) / 2 := by rw [card_misBlock2P_even_left hev x y hxy] have hm : min x (k + 2 - y) = k + 2 - y := by omega rw [hm] by_cases hyk : y ≤ k + 1 · rw [if_pos hyk, show y - (k + 1) = 0 by omega, max_eq_left (Nat.zero_le _)] by_cases hle : min x (y - 2) ≤ k + 2 - y · rw [if_pos hle]; omega · rw [if_neg hle, if_neg (by omega), add_zero] · rw [if_neg hyk, show k + 2 - y = 0 by omega, max_eq_right (Nat.zero_le _), zero_add] by_cases hle : min x (y - 2) ≤ y - (k + 1) · rw [if_pos hle, if_pos hle] · rw [if_neg hle, if_neg hle, if_pos (by omega)] have hT2 : ((Finset.range x).filter (fun a => misBlock2P k a x)).card = if 2 * x ≤ k + 2 then x else if x ≤ k + 1 then (k + 2 - x) + (x - 2 - (k + 2 - x)) / 2 else k / 2 := by rw [card_misBlock2P_even_left hev x x le_rfl] by_cases h1 : 2 * x ≤ k + 2 · rw [if_pos h1, min_eq_left (by omega : x ≤ k + 2 - x), if_pos (by omega)]; omega · rw [if_neg h1] by_cases h2 : x ≤ k + 1 · rw [if_pos h2, min_eq_right (by omega : k + 2 - x ≤ x), show x - (k + 1) = 0 by omega, max_eq_left (Nat.zero_le _), min_eq_right (by omega : x - 2 ≤ x)] by_cases h3 : x - 2 ≤ k + 2 - x · rw [if_pos h3]; omega · rw [if_neg h3, if_neg (by omega), add_zero] · rw [if_neg h2, min_eq_right (by omega : k + 2 - x ≤ x), show k + 2 - x = 0 by omega, max_eq_right (Nat.zero_le _), zero_add, min_eq_right (by omega : x - 2 ≤ x)] by_cases hk0 : k = 0 · subst hk0; rw [if_pos (by omega)] · rw [if_neg (by omega), if_pos (by omega)]; omega have hT3 : ((Finset.Ico (x + 1) y).filter (fun b => misBlock2P k x b)).card = if 2 * x + 1 ≤ k + 1 then (k + 2 - x - (x + 1)) + if min y (x + k + 2) ≤ max (k + 2 - x) (x + 3) then 0 else (min y (x + k + 2) - max (k + 2 - x) (x + 3) + if (max (k + 2 - x) (x + 3)) % 2 = (x + 1) % 2 then 1 else 0) / 2 else if min y (x + k + 2) ≤ x + 3 then 0 else (min y (x + k + 2) - (x + 3) + 1) / 2 := by rw [card_misBlock2P_even_mid hev x y hlt] have hm : min y (k + 2 - x) = k + 2 - x := by omega rw [hm] by_cases h2x : 2 * x + 1 ≤ k + 1 · rw [if_pos h2x, show max (x + 1) (k + 2 - x) = k + 2 - x by omega] · rw [if_neg h2x, show max (x + 1) (k + 2 - x) = x + 1 by omega, Nat.sub_self, zero_add, show max (x + 1) (x + 3) = x + 3 by omega] by_cases hle : min y (x + k + 2) ≤ x + 3 · rw [if_pos hle, if_pos hle] · rw [if_neg hle, if_neg hle, if_pos (by omega)] rw [hT1, hT2, hT3] have hP_iff : misBlock2P k x y ↔ 3 ≤ y - x ∧ y - x ≤ k + 1 ∧ (y - x) % 2 = 1 := by simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, heq, Nat.max_eq_right hxy, Nat.min_eq_left hxy]; omega by_cases hP : 3 ≤ y - x ∧ y - x ≤ k + 1 ∧ (y - x) % 2 = 1 · have hPm : misBlock2P k x y := hP_iff.mpr hP simp only [hPm, not_true_eq_false, decide_false, decide_eq_false_iff_not] exact misBlock2P_even_arith_P k x y hev hlt htri hP · have hNPm : ¬ misBlock2P k x y := fun h => hP (hP_iff.mp h) simp only [hNPm, not_false_eq_true, decide_true, decide_eq_true_eq] exact Or.inl (misBlock2P_even_arith_NP k x y hev hlt htri hP) /-- **THEOREM 2 (Unified Complete Exact Classification of Misère `k`-Blocking Two-Pile Nim — NEW!):** Combines Odd-`k` Exact Shift Duality + Diagonal Vanishing and Even-`k` Diagonal Threshold + Gap-1 Vanishing for ALL `k, x, y : ℕ`! -/ theorem misBlock2Nim_exact (k x y : ℕ) : misBlock2Nim k x y = decide (¬ misBlock2P k x y) := by by_cases hodd : k % 2 = 1 · exact misBlock2Nim_odd_exact hodd x y · exact misBlock2Nim_even_exact (by omega) x y /-- **Corollary 2a (Odd-`k` Exact Shift Duality & Complete Diagonal Vanishing):** -/ theorem misBlock2Nim_odd_shift_duality {k : ℕ} (hodd : k % 2 = 1) : (∀ x, misBlock2Nim k x x = true) ∧ (∀ x y, x < y → misBlock2Nim k x y = normBlock2Nim k x (y - 1)) := by refine ⟨fun x => ?_, fun x y hlt => ?_⟩ · rw [misBlock2Nim_odd_exact hodd]; simp [misBlock2P, hodd] · rw [misBlock2Nim_odd_exact hodd, normBlock2Nim_exact] simp [misBlock2P, hodd, Nat.min_eq_left (le_of_lt hlt), Nat.max_eq_right (le_of_lt hlt), hlt] /-- **Corollary 2b (Even-`k` Exact Diagonal Threshold & Gap-1 Vanishing):** -/ theorem misBlock2Nim_even_threshold_and_gap1 {k : ℕ} (hev : k % 2 = 0) : (∀ x, misBlock2Nim k x x = false ↔ k / 2 + 2 ≤ x) ∧ (∀ x, k / 2 + 1 ≤ x → misBlock2Nim k x (x + 1) = true) := by refine ⟨fun x => ?_, fun x hx => ?_⟩ · rw [misBlock2Nim_even_exact hev] simp [misBlock2P, show k % 2 ≠ 1 by omega] · rw [misBlock2Nim_even_exact hev] simp only [misBlock2P, show k % 2 ≠ 1 by omega, ↓reduceIte, show x ≠ x + 1 by omega, Nat.max_eq_right (by omega : x ≤ x + 1), Nat.min_eq_left (by omega : x ≤ x + 1)] rw [decide_eq_true_iff]; omega /-! ### 5. PART III: Normal & Misère `k`-Pass Two-Pile Nim — Universal Parity Collapse, Exact `3×3` Corner Latin-Square Transposition & Complete `0/1`-Position Classification (NEW!) -/ /-- Normal-play 0-pass 2-pile Nim Grundy value `norm2Nim x y`. -/ def norm2Nim (x y : ℕ) : ℕ := mex ((twoPileMoves x y).attach.image (fun m => norm2Nim m.1.1 m.1.2)) termination_by x + y decreasing_by exact mem_twoPileMoves_sum_lt m.2 lemma norm2Nim_eq (x y : ℕ) : norm2Nim x y = mex ((twoPileMoves x y).image (fun p => norm2Nim p.1 p.2)) := by rw [norm2Nim]; congr 1; ext v simp only [Finset.mem_image, Finset.mem_attach, true_and, Subtype.exists] exact ⟨fun ⟨a, ha, h⟩ => ⟨a, ha, h⟩, fun ⟨a, ha, h⟩ => ⟨a, ha, h⟩⟩ /-- Classical Bouton Theorem for 2-pile Nim: `norm2Nim x y = x ^^^ y` (self-contained!). -/ theorem norm2Nim_eq_xor : ∀ x y, norm2Nim x y = x ^^^ y := by intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => rw [norm2Nim_eq, mex_eq_iff] have himg : (twoPileMoves x y).image (fun p => norm2Nim p.1 p.2) = (twoPileMoves x y).image (fun p => p.1 ^^^ p.2) := Finset.image_congr fun p hp => ih (p.1 + p.2) (by have := mem_twoPileMoves_sum_lt hp; omega) p.1 p.2 rfl rw [himg] refine ⟨?_, ?_⟩ · intro hmem rcases Finset.mem_image.mp hmem with ⟨⟨a, b⟩, hab, hxor⟩ rcases mem_twoPileMoves.mp hab with ⟨h1, hb⟩ | ⟨ha, h2⟩ · dsimp only at hb; subst hb have hax : a = x := by have := congrArg (· ^^^ b) hxor simpa [Nat.xor_assoc, Nat.xor_self, Nat.xor_zero] using this omega · dsimp only at ha; subst ha have hby : b = y := by have := congrArg (a ^^^ ·) hxor simpa [← Nat.xor_assoc, Nat.xor_self, Nat.zero_xor] using this omega · intro v hv set d := (x ^^^ y) ^^^ v with hd_def have hd_pos : 0 < d := by by_contra h0 have hd0 : d = 0 := by omega have heq : x ^^^ y = v := by have := congrArg (· ^^^ v) hd0 simpa [d, Nat.xor_assoc, Nat.xor_self, Nat.xor_zero, Nat.zero_xor] using this omega set k := Nat.log 2 d with hk_def have hdk : d.testBit k = true := by have hge : 2 ^ k ≤ d := Nat.pow_log_le_self 2 (by omega) have hlt : d < 2 ^ (k + 1) := Nat.lt_pow_succ_log_self (by norm_num) d have hdiv1 : d / 2 ^ k = 1 := le_antisymm (Nat.lt_succ_iff.mp (Nat.div_lt_of_lt_mul (by rw [pow_succ'] at hlt; omega))) ((Nat.le_div_iff_mul_le (Nat.two_pow_pos k)).mpr (by omega)) rw [Nat.testBit_eq_decide_div_mod_eq, hdiv1]; rfl have hhi : ∀ j, k < j → d.testBit j = false := by intro j hj apply Nat.testBit_eq_false_of_lt have h1 : d < 2 ^ (k + 1) := Nat.lt_pow_succ_log_self (by norm_num) d have h2 : 2 ^ (k + 1) ≤ 2 ^ j := Nat.pow_le_pow_right (by norm_num) hj omega -- Helper: if `A / 2^(k+1) = B / 2^(k+1)`, `A.testBit k = false`, `B.testBit k = true`, then `A < B`! have hbit_lt : ∀ A B : ℕ, (∀ j, k < j → A.testBit j = B.testBit j) → A.testBit k = false → B.testBit k = true → A < B := by intro A B hhigh hA hB have hdiv_eq : A / 2 ^ (k + 1) = B / 2 ^ (k + 1) := by apply Nat.eq_of_testBit_eq; intro i rw [Nat.testBit_div_two_pow, Nat.testBit_div_two_pow] exact hhigh (i + (k + 1)) (by omega) have hmodA : A % 2 ^ (k + 1) < 2 ^ k := by have h2 := Nat.mod_lt A (Nat.two_pow_pos (k + 1)) have hbit : (A % 2 ^ (k + 1)).testBit k = false := by rw [Nat.testBit_mod_two_pow]; simp [hA] by_contra hge; push Not at hge have hdiv1 : (A % 2 ^ (k + 1)) / 2 ^ k = 1 := le_antisymm (Nat.lt_succ_iff.mp (Nat.div_lt_of_lt_mul (by rw [pow_succ'] at h2; omega))) ((Nat.le_div_iff_mul_le (Nat.two_pow_pos k)).mpr (by omega)) have hbit1 : (A % 2 ^ (k + 1)).testBit k = true := by rw [Nat.testBit_eq_decide_div_mod_eq, hdiv1]; rfl rw [hbit] at hbit1; exact Bool.noConfusion hbit1 have hmodB : 2 ^ k ≤ B % 2 ^ (k + 1) := by have hbit : (B % 2 ^ (k + 1)).testBit k = true := by rw [Nat.testBit_mod_two_pow]; simp [hB] by_contra hlt'; push Not at hlt' have hbit0 := Nat.testBit_eq_false_of_lt hlt' rw [hbit] at hbit0; exact Bool.noConfusion hbit0 have hqA := Nat.div_add_mod A (2 ^ (k + 1)) have hqB := Nat.div_add_mod B (2 ^ (k + 1)) rw [hdiv_eq] at hqA omega have hxy_k : (x ^^^ y).testBit k = true := by by_contra hfalse have hxyf : (x ^^^ y).testBit k = false := Bool.eq_false_iff.mpr hfalse have hvk : v.testBit k = true := by rw [hd_def, Nat.testBit_xor, hxyf, Bool.false_xor] at hdk; exact hdk have hlt_v : x ^^^ y < v := hbit_lt (x ^^^ y) v (fun j hj => by have hdj := hhi j hj; rw [hd_def, Nat.testBit_xor] at hdj cases ha : (x ^^^ y).testBit j <;> cases hb : v.testBit j <;> simp_all) hxyf hvk omega have hxor_lt : ∀ n : ℕ, n.testBit k = true → n ^^^ d < n := by intro n hnk exact hbit_lt (n ^^^ d) n (fun j hj => by rw [Nat.testBit_xor, hhi j hj, Bool.xor_false]) (by rw [Nat.testBit_xor, hnk, hdk]; rfl) hnk rw [Nat.testBit_xor] at hxy_k cases hxk : x.testBit k · have hyk : y.testBit k = true := by simpa [hxk] using hxy_k refine Finset.mem_image.mpr ⟨(x, y ^^^ d), mem_twoPileMoves.mpr (Or.inr ⟨rfl, hxor_lt y hyk⟩), ?_⟩ apply Nat.eq_of_testBit_eq; intro i simp only [d, Nat.testBit_xor] cases x.testBit i <;> cases y.testBit i <;> cases v.testBit i <;> rfl · refine Finset.mem_image.mpr ⟨(x ^^^ d, y), mem_twoPileMoves.mpr (Or.inl ⟨hxor_lt x hxk, rfl⟩), ?_⟩ apply Nat.eq_of_testBit_eq; intro i simp only [d, Nat.testBit_xor] cases x.testBit i <;> cases y.testBit i <;> cases v.testBit i <;> rfl /-- Misère 0-pass 2-pile Nim Grundy value `mis2Nim x y`. -/ def mis2Nim (x y : ℕ) : ℕ := if (twoPileMoves x y).Nonempty then mex ((twoPileMoves x y).attach.image (fun m => mis2Nim m.1.1 m.1.2)) else 1 termination_by x + y decreasing_by exact mem_twoPileMoves_sum_lt m.2 lemma mis2Nim_eq (x y : ℕ) : mis2Nim x y = if (twoPileMoves x y).Nonempty then mex ((twoPileMoves x y).image (fun p => mis2Nim p.1 p.2)) else 1 := by rw [mis2Nim] split_ifs with hne · congr 1; ext v simp only [Finset.mem_image, Finset.mem_attach, true_and, Subtype.exists] exact ⟨fun ⟨a, ha, h⟩ => ⟨a, ha, h⟩, fun ⟨a, ha, h⟩ => ⟨a, ha, h⟩⟩ · rfl /-- **Exact Misère 0-Pass 2-Pile Nim Grundy Formula:** `mis2Nim x y = if max x y ≤ 1 then swap01 (x ^^^ y) else x ^^^ y`! -/ theorem mis2Nim_exact : ∀ x y, mis2Nim x y = if max x y ≤ 1 then swap01 (x ^^^ y) else x ^^^ y := by intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => rw [mis2Nim_eq] by_cases h0 : x = 0 ∧ y = 0 · rcases h0 with ⟨rfl, rfl⟩ rw [if_neg (by rw [twoPileMoves_nonempty_iff]; omega)]; rfl · have hne : (twoPileMoves x y).Nonempty := (twoPileMoves_nonempty_iff x y).mpr (by omega) rw [if_pos hne] by_cases hle1 : max x y ≤ 1 · -- Finite 2×2 corner `{(0,1), (1,0), (1,1)}`: have hcases : (x = 0 ∧ y = 1) ∨ (x = 1 ∧ y = 0) ∨ (x = 1 ∧ y = 1) := by omega rcases hcases with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ · have hm : twoPileMoves 0 1 = {(0, 0)} := by decide rw [hm, Finset.image_singleton, ih 0 (by omega) 0 0 rfl]; decide · have hm : twoPileMoves 1 0 = {(0, 0)} := by decide rw [hm, Finset.image_singleton, ih 0 (by omega) 0 0 rfl]; decide · have hm : twoPileMoves 1 1 = {(0, 1), (1, 0)} := by decide rw [hm, Finset.image_insert, Finset.image_singleton, ih 1 (by omega) 0 1 rfl, ih 1 (by omega) 1 0 rfl]; decide · -- Exterior `max x y ≥ 2`: exact option-set identity with `norm2Nim x y`! rw [if_neg hle1, ← norm2Nim_eq_xor, norm2Nim_eq] congr 1 -- Show `(twoPileMoves x y).image mis2Nim = (twoPileMoves x y).image norm2Nim`! -- Any move outside `max ≤ 1` has identical value; if `y ≤ 1` and `x ≥ 2`, the moves `(0, y), (1, y)` -- give `{swap01 (0^^^y), swap01 (1^^^y)} = {0, 1} = {0^^^y, 1^^^y}`! ext v simp only [Finset.mem_image] constructor · rintro ⟨⟨a, b⟩, hab, rfl⟩ rw [ih (a + b) (by have := mem_twoPileMoves_sum_lt hab; omega) a b rfl] by_cases hab1 : max a b ≤ 1 · rw [if_pos hab1] rcases mem_twoPileMoves.mp hab with ⟨h1, rfl⟩ | ⟨rfl, h2⟩ · -- `a < x`, `b = y ≤ 1`, so `x ≥ 2`, hence `1 - a < x` too! refine ⟨(1 - a, b), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), ?_⟩ rw [norm2Nim_eq_xor] have ha01 : a = 0 ∨ a = 1 := by omega have hb01 : b = 0 ∨ b = 1 := by omega rcases ha01 with rfl | rfl <;> rcases hb01 with rfl | rfl <;> rfl · refine ⟨(a, 1 - b), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), ?_⟩ rw [norm2Nim_eq_xor] have ha01 : a = 0 ∨ a = 1 := by omega have hb01 : b = 0 ∨ b = 1 := by omega rcases ha01 with rfl | rfl <;> rcases hb01 with rfl | rfl <;> rfl · rw [if_neg hab1] exact ⟨(a, b), hab, norm2Nim_eq_xor a b⟩ · rintro ⟨⟨a, b⟩, hab, rfl⟩ rw [norm2Nim_eq_xor] by_cases hab1 : max a b ≤ 1 · rcases mem_twoPileMoves.mp hab with ⟨h1, rfl⟩ | ⟨rfl, h2⟩ · refine ⟨(1 - a, b), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), ?_⟩ rw [ih ((1 - a) + b) (by omega) (1 - a) b rfl, if_pos (by omega)] have ha01 : a = 0 ∨ a = 1 := by omega have hb01 : b = 0 ∨ b = 1 := by omega rcases ha01 with rfl | rfl <;> rcases hb01 with rfl | rfl <;> rfl · refine ⟨(a, 1 - b), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), ?_⟩ rw [ih (a + (1 - b)) (by omega) a (1 - b) rfl, if_pos (by omega)] have ha01 : a = 0 ∨ a = 1 := by omega have hb01 : b = 0 ∨ b = 1 := by omega rcases ha01 with rfl | rfl <;> rcases hb01 with rfl | rfl <;> rfl · refine ⟨(a, b), hab, ?_⟩ rw [ih (a + b) (by have := mem_twoPileMoves_sum_lt hab; omega) a b rfl, if_neg hab1] /-- Normal-play `k`-pass 2-pile Nim Grundy value `normKPass2Nim k x y`. -/ def normKPass2Nim : ℕ → ℕ → ℕ → ℕ | 0, x, y => norm2Nim x y | k + 1, x, y => if (twoPileMoves x y).Nonempty then mex (insert (normKPass2Nim k x y) ((twoPileMoves x y).attach.image (fun m => normKPass2Nim (k + 1) m.1.1 m.1.2))) else 0 termination_by k x y => (k, x + y) decreasing_by · exact Prod.Lex.left _ _ (Nat.lt_succ_self k) · exact Prod.Lex.right _ (mem_twoPileMoves_sum_lt m.2) /-- Misère `k`-pass 2-pile Nim Grundy value `misKPass2Nim k x y`. -/ def misKPass2Nim : ℕ → ℕ → ℕ → ℕ | 0, x, y => mis2Nim x y | k + 1, x, y => if (twoPileMoves x y).Nonempty then mex (insert (misKPass2Nim k x y) ((twoPileMoves x y).attach.image (fun m => misKPass2Nim (k + 1) m.1.1 m.1.2))) else 1 termination_by k x y => (k, x + y) decreasing_by · exact Prod.Lex.left _ _ (Nat.lt_succ_self k) · exact Prod.Lex.right _ (mem_twoPileMoves_sum_lt m.2) abbrev norm1Pass2Nim (x y : ℕ) : ℕ := normKPass2Nim 1 x y abbrev mis1Pass2Nim (x y : ℕ) : ℕ := misKPass2Nim 1 x y lemma normKPass2Nim_zero (x y : ℕ) : normKPass2Nim 0 x y = norm2Nim x y := by rw [normKPass2Nim] lemma misKPass2Nim_zero (x y : ℕ) : misKPass2Nim 0 x y = mis2Nim x y := by rw [misKPass2Nim] lemma normKPass2Nim_succ (k x y : ℕ) : normKPass2Nim (k + 1) x y = if (twoPileMoves x y).Nonempty then mex (insert (normKPass2Nim k x y) ((twoPileMoves x y).image (fun p => normKPass2Nim (k + 1) p.1 p.2))) else 0 := by rw [normKPass2Nim] split_ifs with hne · congr 2; ext v simp only [Finset.mem_image, Finset.mem_attach, true_and, Subtype.exists] exact ⟨fun ⟨a, ha, h⟩ => ⟨a, ha, h⟩, fun ⟨a, ha, h⟩ => ⟨a, ha, h⟩⟩ · rfl lemma misKPass2Nim_succ (k x y : ℕ) : misKPass2Nim (k + 1) x y = if (twoPileMoves x y).Nonempty then mex (insert (misKPass2Nim k x y) ((twoPileMoves x y).image (fun p => misKPass2Nim (k + 1) p.1 p.2))) else 1 := by rw [misKPass2Nim] split_ifs with hne · congr 2; ext v simp only [Finset.mem_image, Finset.mem_attach, true_and, Subtype.exists] exact ⟨fun ⟨a, ha, h⟩ => ⟨a, ha, h⟩, fun ⟨a, ha, h⟩ => ⟨a, ha, h⟩⟩ · rfl lemma norm1Pass2Nim_eq (x y : ℕ) : norm1Pass2Nim x y = if (twoPileMoves x y).Nonempty then mex (insert (x ^^^ y) ((twoPileMoves x y).image (fun p => norm1Pass2Nim p.1 p.2))) else 0 := by change normKPass2Nim (0 + 1) x y = _ rw [normKPass2Nim_succ, normKPass2Nim_zero, norm2Nim_eq_xor] lemma mis1Pass2Nim_eq (x y : ℕ) : mis1Pass2Nim x y = if (twoPileMoves x y).Nonempty then mex (insert (mis2Nim x y) ((twoPileMoves x y).image (fun p => mis1Pass2Nim p.1 p.2))) else 1 := by change misKPass2Nim (0 + 1) x y = _ rw [misKPass2Nim_succ, misKPass2Nim_zero] lemma norm1Pass2Nim_comm : ∀ x y, norm1Pass2Nim x y = norm1Pass2Nim y x := by intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => rw [norm1Pass2Nim_eq, norm1Pass2Nim_eq] by_cases h0 : 0 < x + y · rw [if_pos ((twoPileMoves_nonempty_iff x y).mpr h0), if_pos ((twoPileMoves_nonempty_iff y x).mpr (by omega))] rw [Nat.xor_comm x y] congr 2 ext v simp only [Finset.mem_image] constructor · rintro ⟨p, hp, rfl⟩ refine ⟨p.swap, by rw [← twoPileMoves_swap]; exact Finset.mem_image.mpr ⟨p, hp, rfl⟩, ?_⟩ exact ih (p.1 + p.2) (by have := mem_twoPileMoves_sum_lt hp; omega) p.2 p.1 (by omega) · rintro ⟨q, hq, rfl⟩ have hq' : q.swap ∈ twoPileMoves x y := by rw [← twoPileMoves_swap]; exact Finset.mem_image.mpr ⟨q, hq, rfl⟩ refine ⟨q.swap, hq', ?_⟩ exact ih (q.1 + q.2) (by have := mem_twoPileMoves_sum_lt hq; omega) q.2 q.1 (by omega) · rw [if_neg (by rw [twoPileMoves_nonempty_iff]; omega), if_neg (by rw [twoPileMoves_nonempty_iff]; omega)] /-- **THEOREM 3a (Universal Multi-Pass Parity Collapse for 2-Pile Nim — Normal & Misère):** For ALL `k, x, y : ℕ`, `normKPass2Nim (k + 2) x y = normKPass2Nim k x y` AND `misKPass2Nim (k + 2) x y = misKPass2Nim k x y`! -/ theorem normKPass2Nim_add_two (k : ℕ) : ∀ x y, normKPass2Nim (k + 2) x y = normKPass2Nim k x y := by induction k with | zero => intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => rw [normKPass2Nim_succ, normKPass2Nim_zero, norm2Nim_eq] split_ifs with hne · have himg : (twoPileMoves x y).image (fun p => normKPass2Nim 2 p.1 p.2) = (twoPileMoves x y).image (fun p => norm2Nim p.1 p.2) := by apply Finset.image_congr; intro p hp have hih := ih (p.1 + p.2) (by have := mem_twoPileMoves_sum_lt hp; omega) p.1 p.2 rfl rw [normKPass2Nim_zero] at hih; exact hih rw [himg] have hne10 : normKPass2Nim 1 x y ≠ mex ((twoPileMoves x y).image (fun p => norm2Nim p.1 p.2)) := by change normKPass2Nim (0 + 1) x y ≠ _ rw [normKPass2Nim_succ, if_pos hne, normKPass2Nim_zero, norm2Nim_eq] intro heq have hmem : mex (insert (mex ((twoPileMoves x y).image (fun p => norm2Nim p.1 p.2))) ((twoPileMoves x y).image (fun p => normKPass2Nim (0 + 1) p.1 p.2))) ∈ insert (mex ((twoPileMoves x y).image (fun p => norm2Nim p.1 p.2))) ((twoPileMoves x y).image (fun p => normKPass2Nim (0 + 1) p.1 p.2)) := by rw [heq]; exact Finset.mem_insert_self _ _ exact mex_notMem _ hmem exact mex_insert_ne hne10 · rw [Finset.not_nonempty_iff_eq_empty] at hne; rw [hne, Finset.image_empty, mex_empty] | succ k ihk => intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => rw [normKPass2Nim_succ (k + 2) x y, normKPass2Nim_succ k x y] split_ifs with hne · have himg : (twoPileMoves x y).image (fun p => normKPass2Nim (k + 3) p.1 p.2) = (twoPileMoves x y).image (fun p => normKPass2Nim (k + 1) p.1 p.2) := Finset.image_congr fun p hp => ih (p.1 + p.2) (by have := mem_twoPileMoves_sum_lt hp; omega) p.1 p.2 rfl rw [himg, ihk x y] · rfl theorem misKPass2Nim_add_two (k : ℕ) : ∀ x y, misKPass2Nim (k + 2) x y = misKPass2Nim k x y := by induction k with | zero => intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => rw [misKPass2Nim_succ, misKPass2Nim_zero, mis2Nim_eq] split_ifs with hne · have himg : (twoPileMoves x y).image (fun p => misKPass2Nim 2 p.1 p.2) = (twoPileMoves x y).image (fun p => mis2Nim p.1 p.2) := by apply Finset.image_congr; intro p hp have hih := ih (p.1 + p.2) (by have := mem_twoPileMoves_sum_lt hp; omega) p.1 p.2 rfl rw [misKPass2Nim_zero] at hih; exact hih rw [himg] have hne10 : misKPass2Nim 1 x y ≠ mex ((twoPileMoves x y).image (fun p => mis2Nim p.1 p.2)) := by change misKPass2Nim (0 + 1) x y ≠ _ rw [misKPass2Nim_succ, if_pos hne, misKPass2Nim_zero, mis2Nim_eq, if_pos hne] intro heq have hmem : mex (insert (mex ((twoPileMoves x y).image (fun p => mis2Nim p.1 p.2))) ((twoPileMoves x y).image (fun p => misKPass2Nim (0 + 1) p.1 p.2))) ∈ insert (mex ((twoPileMoves x y).image (fun p => mis2Nim p.1 p.2))) ((twoPileMoves x y).image (fun p => misKPass2Nim (0 + 1) p.1 p.2)) := by rw [heq]; exact Finset.mem_insert_self _ _ exact mex_notMem _ hmem exact mex_insert_ne hne10 · rfl | succ k ihk => intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => rw [misKPass2Nim_succ (k + 2) x y, misKPass2Nim_succ k x y] split_ifs with hne · have himg : (twoPileMoves x y).image (fun p => misKPass2Nim (k + 3) p.1 p.2) = (twoPileMoves x y).image (fun p => misKPass2Nim (k + 1) p.1 p.2) := Finset.image_congr fun p hp => ih (p.1 + p.2) (by have := mem_twoPileMoves_sum_lt hp; omega) p.1 p.2 rfl rw [himg, ihk x y] · rfl /-- Exact values of Normal & Misère 1-pass 2-pile Nim on the `3 × 3` Latin-square corner `x, y ≤ 2`! -/ lemma corner_1pass_norm : norm1Pass2Nim 0 0 = 0 ∧ norm1Pass2Nim 0 1 = 2 ∧ norm1Pass2Nim 0 2 = 1 ∧ norm1Pass2Nim 1 0 = 2 ∧ norm1Pass2Nim 1 1 = 1 ∧ norm1Pass2Nim 1 2 = 0 ∧ norm1Pass2Nim 2 0 = 1 ∧ norm1Pass2Nim 2 1 = 0 ∧ norm1Pass2Nim 2 2 = 2 := by have h00 : norm1Pass2Nim 0 0 = 0 := by rw [norm1Pass2Nim_eq]; decide have h01 : norm1Pass2Nim 0 1 = 2 := by rw [norm1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 0 1 = {(0, 0)} by decide, Finset.image_singleton, h00]; decide have h10 : norm1Pass2Nim 1 0 = 2 := by rw [norm1Pass2Nim_comm]; exact h01 have h02 : norm1Pass2Nim 0 2 = 1 := by rw [norm1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 0 2 = {(0, 0), (0, 1)} by decide, Finset.image_insert, Finset.image_singleton, h00, h01]; decide have h20 : norm1Pass2Nim 2 0 = 1 := by rw [norm1Pass2Nim_comm]; exact h02 have h11 : norm1Pass2Nim 1 1 = 1 := by rw [norm1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 1 1 = {(0, 1), (1, 0)} by decide, Finset.image_insert, Finset.image_singleton, h01, h10]; decide have h12 : norm1Pass2Nim 1 2 = 0 := by rw [norm1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 1 2 = {(0, 2), (1, 0), (1, 1)} by decide, Finset.image_insert, Finset.image_insert, Finset.image_singleton, h02, h10, h11]; decide have h21 : norm1Pass2Nim 2 1 = 0 := by rw [norm1Pass2Nim_comm]; exact h12 have h22 : norm1Pass2Nim 2 2 = 2 := by rw [norm1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 2 2 = {(0, 2), (1, 2), (2, 0), (2, 1)} by decide, Finset.image_insert, Finset.image_insert, Finset.image_insert, Finset.image_singleton, h02, h12, h20, h21]; decide exact ⟨h00, h01, h02, h10, h11, h12, h20, h21, h22⟩ lemma corner_1pass_mis : mis1Pass2Nim 0 0 = 1 ∧ mis1Pass2Nim 0 1 = 2 ∧ mis1Pass2Nim 0 2 = 0 ∧ mis1Pass2Nim 1 0 = 2 ∧ mis1Pass2Nim 1 1 = 0 ∧ mis1Pass2Nim 1 2 = 1 ∧ mis1Pass2Nim 2 0 = 0 ∧ mis1Pass2Nim 2 1 = 1 ∧ mis1Pass2Nim 2 2 = 2 := by have hm00 : mis2Nim 0 0 = 1 := by rw [mis2Nim_exact]; decide have hm01 : mis2Nim 0 1 = 0 := by rw [mis2Nim_exact]; decide have hm10 : mis2Nim 1 0 = 0 := by rw [mis2Nim_exact]; decide have hm02 : mis2Nim 0 2 = 2 := by rw [mis2Nim_exact]; decide have hm20 : mis2Nim 2 0 = 2 := by rw [mis2Nim_exact]; decide have hm11 : mis2Nim 1 1 = 1 := by rw [mis2Nim_exact]; decide have hm12 : mis2Nim 1 2 = 3 := by rw [mis2Nim_exact]; decide have hm21 : mis2Nim 2 1 = 3 := by rw [mis2Nim_exact]; decide have hm22 : mis2Nim 2 2 = 0 := by rw [mis2Nim_exact]; decide have h00 : mis1Pass2Nim 0 0 = 1 := by rw [mis1Pass2Nim_eq]; decide have h01 : mis1Pass2Nim 0 1 = 2 := by rw [mis1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 0 1 = {(0, 0)} by decide, Finset.image_singleton, hm01, h00]; decide have h10 : mis1Pass2Nim 1 0 = 2 := by rw [mis1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 1 0 = {(0, 0)} by decide, Finset.image_singleton, hm10, h00]; decide have h02 : mis1Pass2Nim 0 2 = 0 := by rw [mis1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 0 2 = {(0, 0), (0, 1)} by decide, Finset.image_insert, Finset.image_singleton, hm02, h00, h01]; decide have h20 : mis1Pass2Nim 2 0 = 0 := by rw [mis1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 2 0 = {(0, 0), (1, 0)} by decide, Finset.image_insert, Finset.image_singleton, hm20, h00, h10]; decide have h11 : mis1Pass2Nim 1 1 = 0 := by rw [mis1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 1 1 = {(0, 1), (1, 0)} by decide, Finset.image_insert, Finset.image_singleton, hm11, h01, h10]; decide have h12 : mis1Pass2Nim 1 2 = 1 := by rw [mis1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 1 2 = {(0, 2), (1, 0), (1, 1)} by decide, Finset.image_insert, Finset.image_insert, Finset.image_singleton, hm12, h02, h10, h11]; decide have h21 : mis1Pass2Nim 2 1 = 1 := by rw [mis1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 2 1 = {(0, 1), (1, 1), (2, 0)} by decide, Finset.image_insert, Finset.image_insert, Finset.image_singleton, hm21, h01, h11, h20]; decide have h22 : mis1Pass2Nim 2 2 = 2 := by rw [mis1Pass2Nim_eq, if_pos (by decide), show twoPileMoves 2 2 = {(0, 2), (1, 2), (2, 0), (2, 1)} by decide, Finset.image_insert, Finset.image_insert, Finset.image_insert, Finset.image_singleton, hm22, h02, h12, h20, h21]; decide exact ⟨h00, h01, h02, h10, h11, h12, h20, h21, h22⟩ /-- **THEOREM 3b (Exact `3 × 3` Latin-Square Corner Transposition & Exterior Coincidence for 1-Pass 2-Pile Nim — NEW!):** For EVERY `x, y : ℕ`: - Inside the `3 × 3` corner (`max x y ≤ 2`): `mis1Pass2Nim x y = swap01 (norm1Pass2Nim x y)`; - Outside (`3 ≤ max x y`): **`mis1Pass2Nim x y = norm1Pass2Nim x y`** IDENTICALLY FOR EVERY GRUNDY VALUE! -/ theorem mis1Pass2Nim_exact_duality : ∀ x y, mis1Pass2Nim x y = if max x y ≤ 2 then swap01 (norm1Pass2Nim x y) else norm1Pass2Nim x y := by obtain ⟨n00, n01, n02, n10, n11, n12, n20, n21, n22⟩ := corner_1pass_norm obtain ⟨m00, m01, m02, m10, m11, m12, m20, m21, m22⟩ := corner_1pass_mis intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => by_cases hle2 : max x y ≤ 2 · rw [if_pos hle2] have hx : x = 0 ∨ x = 1 ∨ x = 2 := by omega have hy : y = 0 ∨ y = 1 ∨ y = 2 := by omega rcases hx with rfl | rfl | rfl <;> rcases hy with rfl | rfl | rfl <;> simp [n00, n01, n02, n10, n11, n12, n20, n21, n22, m00, m01, m02, m10, m11, m12, m20, m21, m22, swap01] · -- Exterior `max x y ≥ 3`: the `3 × 3` corner is a Latin square on `{0, 1, 2}` in BOTH games, -- so every exterior position has the EXACT SAME option set in Misère and Normal! rw [if_neg hle2, mis1Pass2Nim_eq, norm1Pass2Nim_eq] have hne : (twoPileMoves x y).Nonempty := (twoPileMoves_nonempty_iff x y).mpr (by omega) rw [if_pos hne, if_pos hne, mis2Nim_exact, if_neg (by omega : ¬ max x y ≤ 1)] congr 2 ext v simp only [Finset.mem_image] constructor · rintro ⟨⟨a, b⟩, hab, rfl⟩ rw [ih (a + b) (by have := mem_twoPileMoves_sum_lt hab; omega) a b rfl] by_cases hab2 : max a b ≤ 2 · rw [if_pos hab2] -- Latin-square permutation inside `{0, 1, 2}`! rcases mem_twoPileMoves.mp hab with ⟨h1, rfl⟩ | ⟨rfl, h2⟩ · -- `a ≤ 2, b = y ≤ 2, x ≥ 3` ⇒ ALL of `(0, b), (1, b), (2, b)` are legal moves from `(x, y)`! have ha : a = 0 ∨ a = 1 ∨ a = 2 := by omega have hb : b = 0 ∨ b = 1 ∨ b = 2 := by omega rcases ha with rfl | rfl | rfl <;> rcases hb with rfl | rfl | rfl · exact ⟨(2, 0), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by simp [n00, n20]⟩ · exact ⟨(0, 1), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by simp [n01, swap01]⟩ · exact ⟨(1, 2), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by simp [n02, n12]⟩ · exact ⟨(1, 0), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by simp [n10, swap01]⟩ · exact ⟨(2, 1), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by simp [n11, n21]⟩ · exact ⟨(0, 2), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by simp [n12, n02]⟩ · exact ⟨(0, 0), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by simp [n20, n00]⟩ · exact ⟨(1, 1), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by simp [n21, n11]⟩ · exact ⟨(2, 2), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by simp [n22, swap01]⟩ · -- `a = x ≤ 2, b ≤ 2, y ≥ 3` ⇒ ALL of `(a, 0), (a, 1), (a, 2)` are legal moves! have ha : a = 0 ∨ a = 1 ∨ a = 2 := by omega have hb : b = 0 ∨ b = 1 ∨ b = 2 := by omega rcases ha with rfl | rfl | rfl <;> rcases hb with rfl | rfl | rfl · exact ⟨(0, 2), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by simp [n00, n02]⟩ · exact ⟨(0, 1), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by simp [n01, swap01]⟩ · exact ⟨(0, 0), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by simp [n02, n00]⟩ · exact ⟨(1, 0), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by simp [n10, swap01]⟩ · exact ⟨(1, 2), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by simp [n11, n12]⟩ · exact ⟨(1, 1), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by simp [n12, n11]⟩ · exact ⟨(2, 1), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by simp [n20, n21]⟩ · exact ⟨(2, 0), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by simp [n21, n20]⟩ · exact ⟨(2, 2), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by simp [n22, swap01]⟩ · rw [if_neg hab2]; exact ⟨(a, b), hab, rfl⟩ · rintro ⟨⟨a, b⟩, hab, rfl⟩ by_cases hab2 : max a b ≤ 2 · rcases mem_twoPileMoves.mp hab with ⟨h1, rfl⟩ | ⟨rfl, h2⟩ · have ha : a = 0 ∨ a = 1 ∨ a = 2 := by omega have hb : b = 0 ∨ b = 1 ∨ b = 2 := by omega rcases ha with rfl | rfl | rfl <;> rcases hb with rfl | rfl | rfl · exact ⟨(2, 0), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by rw [m20, n00]⟩ · exact ⟨(0, 1), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by rw [m01, n01]⟩ · exact ⟨(1, 2), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by rw [m12, n02]⟩ · exact ⟨(1, 0), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by rw [m10, n10]⟩ · exact ⟨(2, 1), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by rw [m21, n11]⟩ · exact ⟨(0, 2), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by rw [m02, n12]⟩ · exact ⟨(0, 0), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by rw [m00, n20]⟩ · exact ⟨(1, 1), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by rw [m11, n21]⟩ · exact ⟨(2, 2), mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩), by rw [m22, n22]⟩ · have ha : a = 0 ∨ a = 1 ∨ a = 2 := by omega have hb : b = 0 ∨ b = 1 ∨ b = 2 := by omega rcases ha with rfl | rfl | rfl <;> rcases hb with rfl | rfl | rfl · exact ⟨(0, 2), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by rw [m02, n00]⟩ · exact ⟨(0, 1), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by rw [m01, n01]⟩ · exact ⟨(0, 0), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by rw [m00, n02]⟩ · exact ⟨(1, 0), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by rw [m10, n10]⟩ · exact ⟨(1, 2), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by rw [m12, n11]⟩ · exact ⟨(1, 1), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by rw [m11, n12]⟩ · exact ⟨(2, 1), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by rw [m21, n20]⟩ · exact ⟨(2, 0), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by rw [m20, n21]⟩ · exact ⟨(2, 2), mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩), by rw [m22, n22]⟩ · refine ⟨(a, b), hab, ?_⟩ rw [ih (a + b) (by have := mem_twoPileMoves_sum_lt hab; omega) a b rfl, if_neg hab2] /-- **Corollary 3b (Complete Multi-Pass Misère–Normal Duality for ALL `k, x, y : ℕ`):** -/ theorem misKPass2Nim_all_k_duality (k x y : ℕ) : misKPass2Nim k x y = if k % 2 = 0 then if max x y ≤ 1 then swap01 (normKPass2Nim k x y) else normKPass2Nim k x y else if max x y ≤ 2 then swap01 (normKPass2Nim k x y) else normKPass2Nim k x y := by induction k using Nat.strong_induction_on with | h k ih => match k with | 0 => simp only [Nat.zero_mod, ↓reduceIte, misKPass2Nim_zero, normKPass2Nim_zero, mis2Nim_exact, norm2Nim_eq_xor] | 1 => simp only [show (1 : ℕ) % 2 ≠ 0 by omega, ↓reduceIte] exact mis1Pass2Nim_exact_duality x y | k + 2 => rw [misKPass2Nim_add_two, normKPass2Nim_add_two, show (k + 2) % 2 = k % 2 by omega] exact ih k (by omega) /-- **THEOREM 3c (Complete Exact Simultaneous `0`-Position and `1`-Position Classification of Normal 1-Pass 2-Pile Nim for ALL `x, y : ℕ`):** 1. `norm1Pass2Nim x y = 0 ↔ (x = 0 ∧ y = 0) ∨ (x % 2 = 1 ∧ y = x + 1) ∨ (y % 2 = 1 ∧ x = y + 1)`; 2. `norm1Pass2Nim x y = 1 ↔ (x = 0 ∧ y = 2) ∨ (x = 2 ∧ y = 0) ∨ (x = y ∧ 1 ≤ x ∧ x ≠ 2)`! -/ theorem norm1Pass2Nim_zero_and_one_iff : ∀ x y, (norm1Pass2Nim x y = 0 ↔ (x = 0 ∧ y = 0) ∨ (x % 2 = 1 ∧ y = x + 1) ∨ (y % 2 = 1 ∧ x = y + 1)) ∧ (norm1Pass2Nim x y = 1 ↔ (x = 0 ∧ y = 2) ∨ (x = 2 ∧ y = 0) ∨ (x = y ∧ 1 ≤ x ∧ x ≠ 2)) := by obtain ⟨n00, n01, n02, n10, n11, n12, n20, n21, n22⟩ := corner_1pass_norm intro x y induction hsum : x + y using Nat.strong_induction_on generalizing x y with | h s ih => by_cases hle2 : max x y ≤ 2 · have hx : x = 0 ∨ x = 1 ∨ x = 2 := by omega have hy : y = 0 ∨ y = 1 ∨ y = 2 := by omega rcases hx with rfl | rfl | rfl <;> rcases hy with rfl | rfl | rfl <;> simp [n00, n01, n02, n10, n11, n12, n20, n21, n22] · -- Exterior `max x y ≥ 3`: have hne : (twoPileMoves x y).Nonempty := (twoPileMoves_nonempty_iff x y).mpr (by omega) -- Helper: `a ^^^ b = 0 ↔ a = b` and `a ^^^ b = 1 ↔ ...` have hxor0 : ∀ a b : ℕ, (a ^^^ b = 0) ↔ a = b := fun a b => ⟨fun h => Nat.eq_of_testBit_eq fun i => by have := congrArg (fun n => n.testBit i) h simp only [Nat.testBit_xor, Nat.zero_testBit] at this cases ha : a.testBit i <;> cases hb : b.testBit i <;> simp_all, fun h => h ▸ Nat.xor_self _⟩ constructor · -- **Part 1: `norm1Pass2Nim x y = 0`** rw [norm1Pass2Nim_eq, if_pos hne, mex_eq_iff] simp only [Finset.mem_insert, Finset.mem_image, not_or, not_exists, not_and, Nat.not_lt_zero, false_imp_iff, forall_true_iff, and_true] constructor · rintro ⟨hxy_xor, hno0⟩ have hxy_ne : x ≠ y := fun h => hxy_xor (by rw [h, Nat.xor_self]) -- WLOG check both `x < y` and `y < x`! by_cases hlt : x < y · by_cases hx0 : x = 0 · exfalso; subst hx0 exact hno0 (0, 0) (mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩)) n00 · by_cases hxodd : x % 2 = 1 · right; left; refine ⟨hxodd, ?_⟩ by_contra hne_succ have hgt : x + 1 < y := by omega have h0 := (ih (x + (x + 1)) (by omega) x (x + 1) rfl).1.mpr (Or.inr (Or.inl ⟨hxodd, rfl⟩)) exact hno0 (x, x + 1) (mem_twoPileMoves.mpr (Or.inr ⟨rfl, hgt⟩)) h0 · exfalso -- `x ≥ 2` is even and `x < y`, so `(x, x - 1)` is a legal move with `norm1Pass2Nim = 0`! have h0 := (ih (x + (x - 1)) (by omega) x (x - 1) rfl).1.mpr (Or.inr (Or.inr ⟨by omega, by omega⟩)) exact hno0 (x, x - 1) (mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩)) h0 · have hgt : y < x := by omega by_cases hy0 : y = 0 · exfalso; subst hy0 exact hno0 (0, 0) (mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩)) n00 · by_cases hyodd : y % 2 = 1 · right; right; refine ⟨hyodd, ?_⟩ by_contra hne_succ have hgt' : y + 1 < x := by omega have h0 := (ih ((y + 1) + y) (by omega) (y + 1) y rfl).1.mpr (Or.inr (Or.inr ⟨hyodd, rfl⟩)) exact hno0 (y + 1, y) (mem_twoPileMoves.mpr (Or.inl ⟨hgt', rfl⟩)) h0 · exfalso have h0 := (ih ((y - 1) + y) (by omega) (y - 1) y rfl).1.mpr (Or.inr (Or.inl ⟨by omega, by omega⟩)) exact hno0 (y - 1, y) (mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩)) h0 · intro hpred have hxy_ne : x ≠ y := by rcases hpred with ⟨rfl, rfl⟩ | ⟨_, rfl⟩ | ⟨_, rfl⟩ <;> omega refine ⟨fun h => hxy_ne ((hxor0 x y).mp h.symm), ?_⟩ intro ⟨a, b⟩ hab h0 have hp := (ih (a + b) (by have := mem_twoPileMoves_sum_lt hab; omega) a b rfl).1.mp h0 rcases mem_twoPileMoves.mp hab with ⟨h1, rfl⟩ | ⟨rfl, h2⟩ <;> rcases hpred with ⟨rfl, rfl⟩ | ⟨hxodd, rfl⟩ | ⟨hyodd, rfl⟩ <;> rcases hp with ⟨h3, h4⟩ | ⟨h3, h4⟩ | ⟨h3, h4⟩ <;> omega · -- **Part 2: `norm1Pass2Nim x y = 1`** rw [norm1Pass2Nim_eq, if_pos hne, mex_eq_iff] constructor · rintro ⟨hno1, hhas0⟩ have h0in := hhas0 0 (by omega) simp only [Finset.mem_insert, Finset.mem_image, not_or, not_exists, not_and] at hno1 right; right by_contra hnotdiag have hxy_ne : x ≠ y := by omega wlog hlt : x < y generalizing x y · -- symmetric case `y < x`: use `norm1Pass2Nim_comm`? Simpler: check both `x < y` and `y < x` directly! -- Wait: let's just handle `x < y` and `y < x` explicitly (6 lines each!) have hgt : y < x := by omega by_cases hy0 : y = 0 · subst hy0 exact hno1.2 (2, 0) (mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩)) n20 · by_cases hy1 : y = 1 · subst hy1 exact hno1.2 (1, 1) (mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩)) n11 · by_cases hy2 : y = 2 · subst hy2 exact hno1.2 (0, 2) (mem_twoPileMoves.mpr (Or.inl ⟨by omega, rfl⟩)) n02 · have h1 := (ih (y + y) (by omega) y y rfl).2.mpr (Or.inr (Or.inr ⟨rfl, by omega, by omega⟩)) exact hno1.2 (y, y) (mem_twoPileMoves.mpr (Or.inl ⟨hgt, rfl⟩)) h1 · by_cases hx0 : x = 0 · subst hx0 exact hno1.2 (0, 2) (mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩)) n02 · by_cases hx1 : x = 1 · subst hx1 exact hno1.2 (1, 1) (mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩)) n11 · by_cases hx2 : x = 2 · subst hx2 exact hno1.2 (2, 0) (mem_twoPileMoves.mpr (Or.inr ⟨rfl, by omega⟩)) n20 · have h1 := (ih (x + x) (by omega) x x rfl).2.mpr (Or.inr (Or.inr ⟨rfl, by omega, by omega⟩)) exact hno1.2 (x, x) (mem_twoPileMoves.mpr (Or.inr ⟨rfl, hlt⟩)) h1 · intro hpred have hdiag : x = y ∧ 3 ≤ x := by rcases hpred with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, h1, h2⟩ <;> omega rcases hdiag with ⟨rfl, hx3⟩ refine ⟨?_, ?_⟩ · simp only [Finset.mem_insert, Finset.mem_image, not_or, not_exists, not_and] refine ⟨by rw [Nat.xor_self]; omega, ?_⟩ intro ⟨a, b⟩ hab h1 have hp := (ih (a + b) (by have := mem_twoPileMoves_sum_lt hab; omega) a b rfl).2.mp h1 rcases mem_twoPileMoves.mp hab with ⟨h1', h2'⟩ | ⟨h1', h2'⟩ <;> rcases hp with ⟨h3, h4⟩ | ⟨h3, h4⟩ | ⟨h3, h4, h5⟩ <;> omega · intro v hv have hv0 : v = 0 := by omega subst hv0 rw [Nat.xor_self]; exact Finset.mem_insert_self 0 _ /-- **Corollary 3c (Complete Exact `0`-Position and `1`-Position Classification of Misère 1-Pass 2-Pile Nim — NEW!):** 1. `mis1Pass2Nim x y = 0 ↔ (x = 0 ∧ y = 2) ∨ (x = 2 ∧ y = 0) ∨ (x = 1 ∧ y = 1) ∨ (x % 2 = 1 ∧ 3 ≤ x ∧ y = x + 1) ∨ (y % 2 = 1 ∧ 3 ≤ y ∧ x = y + 1)`; 2. `mis1Pass2Nim x y = 1 ↔ (x = 0 ∧ y = 0) ∨ (x = 1 ∧ y = 2) ∨ (x = 2 ∧ y = 1) ∨ (x = y ∧ 3 ≤ x)`! -/ theorem mis1Pass2Nim_zero_and_one_iff (x y : ℕ) : (mis1Pass2Nim x y = 0 ↔ (x = 0 ∧ y = 2) ∨ (x = 2 ∧ y = 0) ∨ (x = 1 ∧ y = 1) ∨ (x % 2 = 1 ∧ 3 ≤ x ∧ y = x + 1) ∨ (y % 2 = 1 ∧ 3 ≤ y ∧ x = y + 1)) ∧ (mis1Pass2Nim x y = 1 ↔ (x = 0 ∧ y = 0) ∨ (x = 1 ∧ y = 2) ∨ (x = 2 ∧ y = 1) ∨ (x = y ∧ 3 ≤ x)) := by rw [mis1Pass2Nim_exact_duality] have ⟨h0, h1⟩ := norm1Pass2Nim_zero_and_one_iff x y by_cases hle2 : max x y ≤ 2 · rw [if_pos hle2, swap01_eq_zero_iff, swap01_eq_one_iff, h0, h1] constructor <;> (constructor <;> intro h <;> omega) · rw [if_neg hle2, h0, h1] constructor <;> (constructor <;> intro h <;> omega) /-- **Bonus Exact Closed Form for Row 0 and Row 1 of Normal 1-Pass 2-Pile Nim (`∀ y : ℕ`):** -/ theorem norm1Pass2Nim_row0_exact : ∀ y, norm1Pass2Nim 0 y = if y = 0 then 0 else ((y - 1) ^^^ 1) + 1 := by intro y induction y using Nat.strong_induction_on with | h y ih => rw [norm1Pass2Nim_eq] by_cases hy0 : y = 0 · subst hy0; rw [if_neg (by decide)]; rfl · rw [if_pos ((twoPileMoves_nonempty_iff 0 y).mpr (by omega)), if_neg hy0, Nat.zero_xor, mex_eq_iff] have hmv : twoPileMoves 0 y = (Finset.range y).image (fun b => (0, b)) := by simp [twoPileMoves] rw [hmv, Finset.image_image] -- Formula values on `0..y-1` hit `{0}` plus `((b-1)^^^1)+1` for `1 ≤ b < y`! have hval_pair : ∀ t : ℕ, ((2 * t) ^^^ 1) = 2 * t + 1 ∧ ((2 * t + 1) ^^^ 1) = 2 * t := by intro t have h1 : (2 * t) ^^^ 1 = 2 * t + 1 := by have : 2 * t = t <<< 1 := by rw [Nat.shiftLeft_eq]; ring have hbit0 : (2 * t).testBit 0 = false := by rw [Nat.testBit_zero]; simp apply Nat.eq_of_testBit_eq; intro i cases i with | zero => simp [Nat.testBit_zero] | succ i => rw [Nat.testBit_xor] have h1i : (1 : ℕ).testBit (i + 1) = false := Nat.testBit_eq_false_of_lt (by have : 2 ≤ 2 ^ (i + 1) := by have := Nat.one_le_two_pow (n := i); rw [pow_succ]; omega omega) rw [h1i, Bool.xor_false] rw [Nat.testBit_succ, Nat.testBit_succ] have hdiv : (2 * t + 1) / 2 = (2 * t) / 2 := by omega rw [hdiv] have h2 : (2 * t + 1) ^^^ 1 = 2 * t := by rw [← h1, Nat.xor_assoc, Nat.xor_self, Nat.xor_zero] exact ⟨h1, h2⟩ have hform : ∀ n : ℕ, 1 ≤ n → ((n - 1) ^^^ 1) + 1 = if n % 2 = 1 then n + 1 else n - 1 := by intro n hn by_cases hp : n % 2 = 1 · rw [if_pos hp] have h2t : n - 1 = 2 * ((n - 1) / 2) := by omega rw [h2t, (hval_pair ((n - 1) / 2)).1]; omega · rw [if_neg hp] have h2t : n - 1 = 2 * ((n - 2) / 2) + 1 := by omega rw [h2t, (hval_pair ((n - 2) / 2)).2] rw [hform y (by omega)] simp only [Function.comp_def] have hmem_iff : ∀ v : ℕ, v ∈ insert y ((Finset.range y).image (fun b => norm1Pass2Nim 0 b)) ↔ v = y ∨ v = 0 ∨ ∃ b, 1 ≤ b ∧ b < y ∧ v = if b % 2 = 1 then b + 1 else b - 1 := by intro v simp only [Finset.mem_insert, Finset.mem_image, Finset.mem_range] constructor · rintro (rfl | ⟨b, hby, rfl⟩) · left; rfl · rw [ih b hby] by_cases hb0 : b = 0 · subst hb0; right; left; rfl · rw [if_neg hb0, hform b (by omega)] right; right; exact ⟨b, by omega, hby, rfl⟩ · rintro (rfl | rfl | ⟨b, hb1, hby, rfl⟩) · left; rfl · right; exact ⟨0, by omega, by rw [ih 0 (by omega)]; rfl⟩ · right; exact ⟨b, hby, by rw [ih b hby, if_neg (by omega), hform b hb1]⟩ refine ⟨?_, ?_⟩ · intro hmem rcases (hmem_iff _).mp hmem with h1 | h2 | ⟨b, hb1, hby, h3⟩ <;> split_ifs at * <;> omega · intro v hv rw [hmem_iff] by_cases hv0 : v = 0 · right; left; exact hv0 · by_cases hvy : v = y · left; exact hvy · right; right by_cases hvodd : v % 2 = 1 · refine ⟨v + 1, by omega, ?_, ?_⟩ · split_ifs at hv <;> omega · rw [if_neg (by omega)]; omega · refine ⟨v - 1, by omega, ?_, ?_⟩ · split_ifs at hv <;> omega · rw [if_pos (by omega)]; omega /-! ### 6. Axiom Audit for All Headline Theorems -/ #print axioms mex_image_swap01_iff #print axioms normBlock2Nim_comm #print axioms normBlock2P_Pcnt_exact #print axioms normBlock2Nim_exact #print axioms misBlock2Nim_comm #print axioms misBlock2Nim_odd_exact #print axioms misBlock2Nim_even_exact #print axioms misBlock2Nim_exact #print axioms misBlock2Nim_odd_shift_duality #print axioms misBlock2Nim_even_threshold_and_gap1 #print axioms norm2Nim_eq_xor #print axioms mis2Nim_exact #print axioms normKPass2Nim_add_two #print axioms misKPass2Nim_add_two #print axioms mis1Pass2Nim_exact_duality #print axioms misKPass2Nim_all_k_duality #print axioms norm1Pass2Nim_zero_and_one_iff #print axioms mis1Pass2Nim_zero_and_one_iff #print axioms norm1Pass2Nim_row0_exact end MisereBlockingAndPassTwoPileNim