{
  "status": "proved",
  "decl": "Gomory.exists_domino_tiling",
  "lean_source": "import Mathlib.Data.Fintype.EquivFin\nimport Mathlib.Data.Fintype.Prod\nimport Mathlib.Data.Nat.Dist\n\n/-!\n# Gomory's theorem on the mutilated chessboard\n\nPartition the board into aligned two-by-two blocks. Route a matching along a\npath of blocks from the even hole to the odd hole, leaving all other blocks\nmatched internally.\n-/\n\nnamespace Gomory\n\n/-- A square of the ordinary eight-by-eight chessboard. -/\nabbrev Square := Fin 8 × Fin 8\n\n/-- The checkerboard colour, given by the parity of the sum of the coordinates. -/\ndef colour (x : Square) : ℕ := (x.1.val + x.2.val) % 2\n\n/-- Two squares share an edge. Coordinate differences are taken in the naturals. -/\ndef Adjacent (x y : Square) : Prop :=\n  (x.1 = y.1 ∧ Nat.dist x.2.val y.2.val = 1) ∨\n  (x.2 = y.2 ∧ Nat.dist x.1.val y.1.val = 1)\n\n/-- The squares that remain after removing `a` and `b`. -/\nabbrev Remaining (a b : Square) := {x : Square // x ≠ a ∧ x ≠ b}\n\nprivate abbrev Block := Fin 4 × Fin 4\nprivate abbrev Slot := Block × Fin 2\n\n-- A block and a row within that block specify one square of each colour.\nprivate def square (s : Slot) (odd : Bool) : Square :=\n  (⟨2 * s.1.1.val + s.2.val, by have := s.1.1.isLt; have := s.2.isLt; omega⟩,\n   ⟨2 * s.1.2.val + (if odd then 1 - s.2.val else s.2.val), by\n     have := s.1.2.isLt\n     have := s.2.isLt\n     split <;> omega⟩)\n\nprivate theorem colour_square (s : Slot) (odd : Bool) :\n    colour (square s odd) = odd.toNat := by\n  have := s.2.isLt\n  cases odd <;> simp [colour, square] <;> omega\n\nprivate def encode (x : Square) : Slot × Bool :=\n  (((⟨x.1.val / 2, by have := x.1.isLt; omega⟩,\n     ⟨x.2.val / 2, by have := x.2.isLt; omega⟩),\n    ⟨x.1.val % 2, Nat.mod_lt _ (by decide)⟩),\n   decide (colour x = 1))\n\nprivate def boardEquiv : Slot × Bool ≃ Square where\n  toFun p := square p.1 p.2\n  invFun := encode\n  left_inv := by\n    rintro ⟨⟨⟨r, c⟩, i⟩, odd⟩\n    have hi := i.isLt\n    cases odd <;>\n      simp [encode, square, colour, Prod.ext_iff, Fin.ext_iff] <;> omega\n  right_inv := by\n    intro x\n    apply Prod.ext <;> apply Fin.ext\n    · simp [encode, square]\n      omega\n    · by_cases h : colour x = 1 <;> simp [encode, square, h] <;>\n        unfold colour at h <;> omega\n\nprivate theorem adjacent_in_block {x y : Slot} (h : x.1 = y.1) :\n    Adjacent (square x false) (square y true) := by\n  have hr := congrArg (fun p : Block => p.1.val) h\n  have hc := congrArg (fun p : Block => p.2.val) h\n  have hx := x.2.isLt\n  have hy := y.2.isLt\n  simp [Adjacent, square, Fin.ext_iff, Nat.dist]\n  omega\n\nprivate def blockDist (p q : Block) : ℕ :=\n  Nat.dist p.1.val q.1.val + Nat.dist p.2.val q.2.val\n\n-- Approach the starting block by one step in a coordinate that differs.\n-- The two slots specify an even square at the new end and an odd square\n-- at the previous end, sharing the edge between their blocks.\nprivate theorem previous_block (p q : Block) (h : blockDist p q ≠ 0) :\n    ∃ c d : Slot, c.1 = q ∧ blockDist p d.1 + 1 = blockDist p q ∧\n      Adjacent (square c false) (square d true) := by\n  have hqr := q.1.isLt\n  have hqc := q.2.isLt\n  have hpr := p.1.isLt\n  have hpc := p.2.isLt\n  by_cases hr : p.1.val < q.1.val\n  · refine ⟨(q, 0), ((⟨q.1.val - 1, by omega⟩, q.2), 1), rfl, ?_, ?_⟩\n    · simp [blockDist, Nat.dist]\n      omega\n    · right\n      simp [square, Nat.dist]\n      omega\n  by_cases hr' : q.1.val < p.1.val\n  · refine ⟨(q, 1), ((⟨q.1.val + 1, by omega⟩, q.2), 0), rfl, ?_, ?_⟩\n    · simp [blockDist, Nat.dist]\n      omega\n    · right\n      simp [square, Nat.dist]\n      omega\n  by_cases hc : p.2.val < q.2.val\n  · refine ⟨(q, 0), ((q.1, ⟨q.2.val - 1, by omega⟩), 0), rfl, ?_, ?_⟩\n    · simp [blockDist, Nat.dist]\n      omega\n    · left\n      simp [square, Nat.dist]\n      omega\n  · have hc' : q.2.val < p.2.val := by\n      simp [blockDist, Nat.dist] at h\n      omega\n    refine ⟨(q, 1), ((q.1, ⟨q.2.val + 1, by omega⟩), 1), rfl, ?_, ?_⟩\n    · simp [blockDist, Nat.dist]\n      omega\n    · left\n      simp [square, Nat.dist]\n      omega\n\n-- Regard the even and odd squares as two copies of Slot. The assignment\n-- sends the even hole to the odd hole, and all other assignments are dominoes.\n-- Blocks farther from the start than the current endpoint retain the identity\n-- matching, making both slots in the next block available for rerouting.\nprivate theorem route_matching (a b : Slot) :\n    ∃ e : Equiv.Perm Slot, e a = b ∧\n      (∀ x, x ≠ a → Adjacent (square x false) (square (e x) true)) ∧\n      (∀ x, blockDist a.1 b.1 < blockDist a.1 x.1 → e x = x) := by\n  generalize hn : blockDist a.1 b.1 = n\n  induction n using Nat.strong_induction_on generalizing b with\n  | ind n ih =>\n    by_cases hzero : n = 0\n    · have hab : a.1 = b.1 := by\n        simp [blockDist, Nat.dist] at hn\n        exact Prod.ext (Fin.ext (by omega)) (Fin.ext (by omega))\n      refine ⟨Equiv.swap a b, Equiv.swap_apply_left _ _, ?_, ?_⟩\n      · intro x hx\n        apply adjacent_in_block\n        by_cases hxb : x = b\n        · subst x\n          simpa only [Equiv.swap_apply_right] using hab.symm\n        · rw [Equiv.swap_apply_of_ne_of_ne hx hxb]\n      · intro x hx\n        have hxa : x ≠ a := by\n          rintro rfl\n          simp [blockDist, Nat.dist] at hx\n        have hxb : x ≠ b := by\n          rintro rfl\n          omega\n        exact Equiv.swap_apply_of_ne_of_ne hxa hxb\n    · obtain ⟨c, d, hc, hd, hadj⟩ := previous_block a.1 b.1 (by omega)\n      obtain ⟨e, hea, he_adj, he_fixed⟩ :=\n        ih (blockDist a.1 d.1) (by omega) d rfl\n      have hc_fixed : e c = c := he_fixed c (by rw [hc]; omega)\n      have hb_fixed : e b = b := he_fixed b (by omega)\n      have hd_ne_c : d ≠ c := by\n        rintro rfl\n        rw [hc] at hd\n        omega\n      have hd_ne_b : d ≠ b := by\n        rintro rfl\n        omega\n      -- First place the desired odd hole opposite c in the matching.\n      -- Then redirect c to the old odd hole and a to the new one.\n      let e' := (Equiv.swap a c).trans (e.trans (Equiv.swap c b))\n      refine ⟨e', ?_, ?_, ?_⟩\n      · simp [e', hc_fixed]\n      · intro x hx\n        by_cases hxc : x = c\n        · subst x\n          simpa [e', hea, Equiv.swap_apply_of_ne_of_ne hd_ne_c hd_ne_b] using hadj\n        · have hexc : e x ≠ c := fun h => hxc (e.injective (h.trans hc_fixed.symm))\n          by_cases hxb : x = b\n          · subst x\n            apply adjacent_in_block\n            simp [e', Equiv.swap_apply_of_ne_of_ne hx hxc, hb_fixed, hc]\n          · have hexb : e x ≠ b := fun h => hxb (e.injective (h.trans hb_fixed.symm))\n            simpa [e', Equiv.swap_apply_of_ne_of_ne hx hxc,\n              Equiv.swap_apply_of_ne_of_ne hexc hexb] using he_adj x hx\n      · intro x hx\n        have hxa : x ≠ a := by\n          rintro rfl\n          simp [blockDist, Nat.dist] at hx\n        have hxc : x ≠ c := by\n          rintro rfl\n          rw [hc] at hx\n          omega\n        have hxb : x ≠ b := by\n          rintro rfl\n          omega\n        simp [e', Equiv.swap_apply_of_ne_of_ne hxa hxc,\n          he_fixed x (by omega), Equiv.swap_apply_of_ne_of_ne hxc hxb]\n\nprivate theorem card_slots_except (a : Slot) : Fintype.card {x : Slot // x ≠ a} = 31 := by\n  rw [Fintype.card_subtype_compl (· = a), Fintype.card_prod, Fintype.card_prod]\n  simp\n\nprivate abbrev EncodedRemaining (a b : Slot) :=\n  {x : Slot × Bool // x ≠ (a, false) ∧ x ≠ (b, true)}\n\n-- An index names an even slot other than a. Its partner is obtained\n-- from the matching, which sends a to b and hence omits b on these indices.\nprivate noncomputable def indexMatching {a b : Slot} (e : Equiv.Perm Slot) (h : e a = b) :\n    Fin 31 × Bool ≃ EncodedRemaining a b := by\n  let k := (Fintype.equivFinOfCardEq (card_slots_except a)).symm\n  let g (p : Fin 31 × Bool) : EncodedRemaining a b :=\n    ⟨(if p.2 then e (k p.1).val else (k p.1).val, p.2), by\n      have hk := (k p.1).property\n      have he : e (k p.1).val ≠ b := fun he => hk (e.injective (he.trans h.symm))\n      rcases p with ⟨i, bit⟩\n      cases bit <;> simpa⟩\n  refine Equiv.ofBijective g ⟨?_, ?_⟩\n  · rintro ⟨i, bi⟩ ⟨j, bj⟩ hij\n    have hb : bi = bj := congrArg (fun x : EncodedRemaining a b => x.val.2) hij\n    subst bj\n    have hs := congrArg (fun x : EncodedRemaining a b => x.val.1) hij\n    have hk : (k i).val = (k j).val := by\n      cases bi\n      · exact hs\n      · exact e.injective hs\n    exact Prod.ext (k.injective (Subtype.ext hk)) rfl\n  · rintro ⟨⟨x, bit⟩, hx⟩\n    cases bit\n    · have ha : x ≠ a := fun hxa => hx.1 (by rw [hxa])\n      refine ⟨(k.symm ⟨x, ha⟩, false), Subtype.ext ?_⟩\n      simp [g]\n    · have hb : e.symm x ≠ a := by\n        intro he\n        apply hx.2\n        rw [← h, ← he, e.apply_symm_apply]\n      refine ⟨(k.symm ⟨e.symm x, hb⟩, true), Subtype.ext ?_⟩\n      simp [g]\n\nprivate theorem indexMatching_adjacent {a b : Slot} (e : Equiv.Perm Slot) (h : e a = b)\n    (hadj : ∀ x, x ≠ a → Adjacent (square x false) (square (e x) true)) (i : Fin 31) :\n    Adjacent (boardEquiv (indexMatching e h (i, false)).val)\n      (boardEquiv (indexMatching e h (i, true)).val) :=\n  hadj _ ((Fintype.equivFinOfCardEq (card_slots_except a)).symm i).property\n\nprivate theorem adjacent_symm {x y : Square} (h : Adjacent x y) : Adjacent y x := by\n  rcases h with ⟨h, hd⟩ | ⟨h, hd⟩\n  · exact Or.inl ⟨h.symm, (Nat.dist_comm _ _).trans hd⟩\n  · exact Or.inr ⟨h.symm, (Nat.dist_comm _ _).trans hd⟩\n\nprivate theorem tiling_of_indexing {a b : Square} (k : Fin 31 × Bool ≃ Remaining a b)\n    (hk : ∀ i, Adjacent (k (i, false)).val (k (i, true)).val) :\n    ∃ f : Remaining a b → Remaining a b, Function.Involutive f ∧\n      (∀ x, Adjacent x.val (f x).val) ∧ ∀ i : Fin 31, f (k (i, false)) = k (i, true) := by\n  let f (x : Remaining a b) := k ((k.symm x).1, !(k.symm x).2)\n  refine ⟨f, ?_, ?_, ?_⟩\n  · intro x\n    simp [f]\n  · intro x\n    obtain ⟨⟨i, bit⟩, rfl⟩ := k.surjective x\n    cases bit\n    · simpa [f] using hk i\n    · simpa [f] using adjacent_symm (hk i)\n  · intro i\n    simp [f]\n\nprivate theorem exists_indexing_of_even_odd (a b : Square) (ha : colour a = 0)\n    (hb : colour b = 1) :\n    ∃ k : Fin 31 × Bool ≃ Remaining a b,\n      ∀ i, Adjacent (k (i, false)).val (k (i, true)).val := by\n  obtain ⟨⟨s, cs⟩, rfl⟩ := boardEquiv.surjective a\n  obtain ⟨⟨t, ct⟩, rfl⟩ := boardEquiv.surjective b\n  change colour (square s cs) = 0 at ha\n  change colour (square t ct) = 1 at hb\n  rw [colour_square] at ha hb\n  have hcs : cs = false := by cases cs <;> simp_all\n  have hct : ct = true := by cases ct <;> simp_all\n  subst cs\n  subst ct\n  obtain ⟨e, he, hadj, _⟩ := route_matching s t\n  let transport : EncodedRemaining s t ≃\n      Remaining (boardEquiv (s, false)) (boardEquiv (t, true)) :=\n    boardEquiv.subtypeEquiv (fun _ => by simp)\n  exact ⟨(indexMatching e he).trans transport, indexMatching_adjacent e he hadj⟩\n\n/-- Gomory's theorem (Wikidata Q2916568).\n\n“If two squares of opposite colors are removed, then the remaining board can always be tiled\nwith dominoes.” Here the board is the ordinary 8×8 chessboard.\n\nThe involution pairs each square with its domino partner. The equivalence indexes\nthe two endpoints of each domino by a common index in `Fin 31` and distinct bits. -/\ntheorem exists_domino_tiling (a b : Square) (h : colour a ≠ colour b) :\n    ∃ (f : Remaining a b → Remaining a b) (k : Fin 31 × Bool ≃ Remaining a b),\n      Function.Involutive f ∧ (∀ x, Adjacent x.val (f x).val) ∧\n        ∀ i : Fin 31, f (k (i, false)) = k (i, true) := by\n  have ha : colour a < 2 := Nat.mod_lt _ (by decide)\n  have hb : colour b < 2 := Nat.mod_lt _ (by decide)\n  obtain ⟨k, hk⟩ : ∃ k : Fin 31 × Bool ≃ Remaining a b,\n      ∀ i, Adjacent (k (i, false)).val (k (i, true)).val := by\n    by_cases hae : colour a = 0\n    · exact exists_indexing_of_even_odd a b hae (by omega)\n    · obtain ⟨k, hk⟩ := exists_indexing_of_even_odd b a (by omega) (by omega)\n      let swapHoles : Remaining b a ≃ Remaining a b :=\n        Equiv.subtypeEquivRight (fun _ => and_comm)\n      exact ⟨k.trans swapHoles, hk⟩\n  obtain ⟨f, hf, hfa, hfk⟩ := tiling_of_indexing k hk\n  exact ⟨f, k, hf, hfa, hfk⟩\n\nend Gomory\n\n#print axioms Gomory.exists_domino_tiling\n",
  "build_output": "'Gomory.exists_domino_tiling' depends on axioms: [propext, Classical.choice, Quot.sound]\n",
  "explanation": "The theorem supplies a domino partner for every square left after removing one even square and one odd square. It also indexes all dominoes by Fin 31.\n\nPartition the board into sixteen aligned 2×2 blocks. In a block, the local row selects one even square and one odd square. A permutation of the 32 block-and-row slots thus matches even squares to odd squares. Temporarily match the two holes to each other and require every other pair to share an edge.\n\nWhen the holes lie in one block, a swap inside that block puts the holes together. For a farther target, move one block toward the starting block and apply strong induction on Manhattan distance. Use as the previous odd hole the square sharing the boundary with a chosen even square in the target block. Both slots in the target block still have their original within-block matches because its distance is larger. Swap odd destinations within the target block so that the chosen even square maps to the desired odd hole. Then swap the assignments of that even square and the original even hole. This creates one crossing domino and pairs the two actual holes. The other pair in the target block stays internal, where every even square shares an edge with every odd square. Restrict the resulting permutation to the 31 even slots other than the hole, then use the matching to label their 31 odd partners with the same indices.\n\nBoth toolchain archives passed the SHA-256 checks from the bundle manifest. The file compiled with lean -DautoImplicit=false Gomory.lean using Lean 4.36.0 and the task's prebuilt Mathlib bundle for fc4f59e577feb1a4956671bc04ac18aaaf8cd842. UNVERIFIED: independent-agent rebuild and review.\n\n| Target and statement | Standard proof and informal length | Existing Mathlib | Needed here | Grade |\n| --- | --- | --- | --- | --- | --- |\n| Q2916568. Any two opposite-colour holes in an 8×8 board leave a tiling by 31 edge dominoes. | One short paragraph: a cyclic traversal of the board splits at the holes into two even-length runs, each tiled in consecutive pairs. The formal route uses the block argument above. | Fin, finite products, Nat.dist, Equiv.swap, Equiv.subtypeEquiv, Fintype.card_subtype_compl and Fintype.equivFinOfCardEq. Loogle located the swap API. Moogle is UNVERIFIED because its TLS certificate check failed. | Board adjacency and colour, block encoding and routing invariant, and transport to a numbered tiling. 305 Lean lines. | A |",
  "fidelity": [
    "Board and colour: Square is definitionally Fin 8 × Fin 8, and colour is the sum of the natural coordinate values modulo 2.",
    "Arbitrary holes: a and b are universally quantified with the sole hypothesis colour a ≠ colour b. The proof handles either order of the colours and permits both holes in one block.",
    "Exact remainder: Remaining a b consists of the squares unequal to both a and b. The tiling equivalence maps onto this entire subtype.",
    "Domino shape: Adjacent requires equality in one coordinate and natural distance one in the other. This yields two distinct squares and excludes diagonal steps and steps across the 0–7 boundary.",
    "Disjointness and count: k is a bijection from Fin 31 × Bool. Each index supplies two different endpoints, and endpoints belonging to distinct indices are different. Surjectivity covers every remaining square, so exactly 31 dominoes cover 62 squares.",
    "Partner function: f is an involution on Remaining a b, pairs each square with an adjacent square, and sends the false endpoint of each numbered domino to its true endpoint.",
    "A local scratch module importing the freshly built Gomory module derived adjacency directly with natural successor equalities and computed the cardinality of Remaining as 62. Independent fidelity review is UNVERIFIED."
  ],
  "prior_work": [
    {
      "addresses": [
        "https://leanprover-community.github.io/1000-missing.html",
        "https://leanprover-community.github.io/1000.html"
      ],
      "finding": "Q2916568 is listed as Gomory's theorem on the missing page. The finished page had no match for the id or theorem name."
    },
    {
      "addresses": [
        "https://api.github.com/repos/leanprover-community/mathlib4/commits/55b1401a3a21c8aebb70df9d5f815fd75828fa90",
        "https://github.com/leanprover-community/mathlib4"
      ],
      "finding": "Searched all 9,351 Lean files from the checked Mathlib master snapshot for Gomory, Q2916568, chessboard, mutilated chessboard and domino tiling. Zero text matches."
    },
    {
      "addresses": [
        "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4+is%3Apr+is%3Aopen+%28Gomory+OR+chessboard+OR+mutilated%29",
        "https://github.com/leanprover-community/mathlib4/pulls"
      ],
      "finding": "The GitHub search API returned zero open Mathlib PRs for Gomory, chessboard or mutilated. Each term also returned zero when queried separately."
    },
    {
      "addresses": [
        "https://api.github.com/repos/rjwalters/lean-genius/git/trees/d8540c5a48f69a9f57b87c549c6e09dad63a0670?recursive=1",
        "https://github.com/rjwalters/lean-genius/tree/main/proofs/Proofs"
      ],
      "finding": "Searched the contents of 6,087 Lean files in proofs/Proofs from the downloaded main archive. The chessboard matches concerned knight's tours. No match identified the target theorem."
    },
    {
      "addresses": [
        "https://api.github.com/search/repositories?q=%22mutilated+chessboard%22+language%3ALean",
        "https://api.github.com/search/code?q=%22Gomory%22+language%3ALean",
        "https://gist.githubusercontent.com/avigad/1080e327ad6806884c9c5091f5c449bd/raw",
        "https://github.com/leansolving/leancsp/raw/refs/heads/main/README.md",
        "https://artwaste.land/strata/the-mutilated-chessboard/"
      ],
      "finding": "Public indexed searches found related Lean work on the same-colour corner obstruction. Avigad's Lean declaration proves the impossibility for (0, 0) and (7, 7). LeanCSP describes the obstruction on 4×4 and 6×6 boards. The Verification Venue reports a Lean proof of the 8×8 corner obstruction and a separate exhaustive matching check. None of these sources supplied a Lean theorem for arbitrary opposite-colour holes. UNVERIFIED: exhaustive GitHub code search, because the unauthenticated code-search API returned HTTP 401."
    },
    {
      "addresses": [
        "https://leanprover-community.github.io/archive/stream/113489-new-members/topic/Advice.20on.20Definitions.html",
        "https://leanprover-community.github.io/archive/"
      ],
      "finding": "Indexed archive searches for Gomory, mutilated chessboard and domino tiling found the linked modelling discussion, which points to the Isabelle corner example. No announcement of the target Lean theorem surfaced. UNVERIFIED: exhaustive archive coverage."
    }
  ],
  "database_entry": {
    "wikidata_id": "Q2916568",
    "title": "Gomory's theorem",
    "decl": "Gomory.exists_domino_tiling",
    "file": "Gomory.lean"
  },
  "stuck": null,
  "sources": [
    "https://leanprover-community.github.io/1000-missing.html",
    "https://leanprover-community.github.io/1000.html",
    "https://api.github.com/repos/leanprover-community/mathlib4/commits/55b1401a3a21c8aebb70df9d5f815fd75828fa90",
    "https://api.github.com/repos/rjwalters/lean-genius/git/trees/d8540c5a48f69a9f57b87c549c6e09dad63a0670?recursive=1",
    "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4+is%3Apr+is%3Aopen+%28Gomory+OR+chessboard+OR+mutilated%29",
    "https://api.github.com/search/repositories?q=%22mutilated+chessboard%22+language%3ALean",
    "https://gist.githubusercontent.com/avigad/1080e327ad6806884c9c5091f5c449bd/raw",
    "https://leanprover-community.github.io/archive/stream/113489-new-members/topic/Advice.20on.20Definitions.html",
    "https://en.wikipedia.org/wiki/Mutilated_chessboard_problem",
    "https://artwaste.land/strata/the-mutilated-chessboard/",
    "https://github.com/leansolving/leancsp/raw/refs/heads/main/README.md",
    "https://loogle.lean-lang.org/json?q=Equiv.swap",
    "https://github.com/Identity-md/lean-bundles/releases/download/pequalnp-e125f04d0dc6/fetch.json",
    "https://raw.githubusercontent.com/leanprover-community/mathlib4/fc4f59e577feb1a4956671bc04ac18aaaf8cd842/Mathlib/Logic/Equiv/Basic.lean",
    "https://raw.githubusercontent.com/leanprover-community/mathlib4/fc4f59e577feb1a4956671bc04ac18aaaf8cd842/Mathlib/Data/Nat/Dist.lean",
    "https://raw.githubusercontent.com/leanprover-community/mathlib4/fc4f59e577feb1a4956671bc04ac18aaaf8cd842/Mathlib/Data/Fintype/EquivFin.lean"
  ]
}
