{
  "verdict": "pass",
  "decl": "Gomory.exists_domino_tiling",
  "author_status": "proved",
  "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",
  "source_sha256": "43423e847a6364653c4c81f0fef90a57addd19b96647a2f755506c3a91a8ca8b",
  "build_output": "Input artifact SHA-256 matched the supplied pin (local and HTTPS copies).\nLean and prebuilt package archive SHA-256 checks both passed against fetch.json. Used the supplied Mathlib bundle for fc4f59e577feb1a4956671bc04ac18aaaf8cd842.\nLean (version 4.36.0, x86_64-unknown-linux-gnu, commit 611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9, Release)\n$ lean -DautoImplicit=false Gomory.lean\n'Gomory.exists_domino_tiling' depends on axioms: [propext, Classical.choice, Quot.sound]\nExit code: 0. No errors or warnings. A whole-source token scan found none of the eight forbidden terms.\n",
  "fidelity_review": {
    "board": "Square is Fin 8 × Fin 8, so each coordinate ranges from 0 to 7.",
    "holes": "a and b range over all squares. The only hypothesis is colour a ≠ colour b, with colour defined as the sum of the natural coordinates modulo two. It applies in either order and includes holes in the same 2×2 block. It is satisfiable, for example at (0,0) and (0,1), and entails a ≠ b.",
    "remainder": "Remaining a b is exactly the subtype of squares unequal to both a and b. Both the domain and codomain of f are that subtype.",
    "adjacency": "Adjacent requires either equal first coordinates and natural distance one in the second coordinate, or equal second coordinates and natural distance one in the first. This is edge adjacency and excludes diagonal moves, wrap-around, and self-pairs.",
    "coverage_and_count": "Involutivity makes each partner point back to its original square. The equivalence k covers every remaining square exactly once. For each i in Fin 31 the two distinct images k(i,false) and k(i,true) are paired by f, and involutivity gives the reverse pairing. The images are disjoint between indices by injectivity and exhaustive by surjectivity. This explicitly supplies 31 dominoes and covers 62 squares.",
    "assessment": "Faithful to the full vetted opposite-colour existence theorem."
  },
  "prior_work": [
    {
      "sources": [
        "https://leanprover-community.github.io/1000-missing.html",
        "https://leanprover-community.github.io/1000.html",
        "https://raw.githubusercontent.com/leanprover-community/mathlib4/master/docs/1000.yaml",
        "https://api.github.com/repos/leanprover-community/mathlib4/commits/55b1401a3a21c8aebb70df9d5f815fd75828fa90"
      ],
      "finding": "Q2916568 appears on the missing list and has a title-only entry in docs/1000.yaml. The finished list has no Q2916568 or Gomory entry. A downloaded Mathlib master snapshot had no matching Lean source in Mathlib, Archive, or Counterexamples for Gomory, Q2916568, chessboard, mutilated chessboard, exists_domino_tiling, or domino tiling."
    },
    {
      "sources": [
        "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4+is%3Apr+is%3Aopen+Gomory",
        "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4+is%3Apr+is%3Aopen+chessboard",
        "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4+is%3Apr+is%3Aopen+domino",
        "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4+is%3Apr+is%3Aopen+Q2916568"
      ],
      "finding": "Independent GitHub issue-search API queries found zero open Mathlib4 PRs for Gomory, chessboard, domino, and Q2916568."
    },
    {
      "sources": [
        "https://api.github.com/repos/rjwalters/lean-genius/git/trees/d8540c5a48f69a9f57b87c549c6e09dad63a0670?recursive=1"
      ],
      "finding": "Searched the contents of 6,087 Lean files under proofs/Proofs at the cited lean-genius commit. Four chessboard hits refer to knight’s tours. No matching proof of the target was found."
    },
    {
      "sources": [
        "https://github.com/search?q=%22mutilated+chessboard%22+language%3ALean&type=code",
        "https://gist.githubusercontent.com/avigad/1080e327ad6806884c9c5091f5c449bd/raw",
        "https://light.artwaste.land/strata/the-mutilated-chessboard/",
        "https://raw.githubusercontent.com/SauersML/Cairn/6d1569de034a1ccc5cdc26880780b21442fe2bed/README.md"
      ],
      "finding": "Indexed GitHub and web searches for the theorem name, ID, declaration name, and opposite-colour domino tiling found no earlier Lean proof of the target. Avigad’s Lean source removes (0,0) and (7,7) and proves the corner obstruction. The Verification Venue describes a Lean proof of impossibility and an exhaustive matching computation for two-hole boards. Cairn’s README gives the one-dark, one-light observation. UNVERIFIED: exhaustive GitHub code search requires sign-in."
    },
    {
      "sources": [
        "https://leanprover-community.github.io/archive/stream/113489-new-members/topic/Advice.20on.20Definitions.html"
      ],
      "finding": "Indexed Lean Zulip archive searches for Gomory, mutilated chessboard, and domino tiling found a discussion citing an Isabelle experiment. No Lean proof of the opposite-colour result surfaced. UNVERIFIED: exhaustive Zulip coverage and global priority."
    }
  ],
  "explanation": "Remove any two differently coloured squares from the ordinary chessboard. The proof supplies exactly 31 edge-sharing pairs that cover all remaining squares once. It divides the board into sixteen 2×2 blocks. Each row of each block gives a slot with one square of each colour. A permutation of the 32 slots pairs even squares with odd squares. One designated pair joins the two holes and is discarded. Every other pairing must share an edge. If the holes lie in one block, the proof swaps the two hole slots in the identity permutation. Every even square in that block is adjacent to both of its odd squares. For holes in different blocks it inducts on the Manhattan distance between their blocks. It first moves the odd hole to a nearer block at a square sharing an edge with an even square of the destination block. The induction leaves the destination block internally paired. Two swaps then pair that destination even square across the block boundary, pair the original even hole with the actual odd hole, and keep any other changed ordinary pair within the destination block. Deleting the designated pair leaves 31 even slots and their 31 distinct odd partners. The proof indexes them by Fin 31 × Bool and flips the Boolean to obtain the partner involution.",
  "database_entry": {
    "wikidata_id": "Q2916568",
    "title": "Gomory's theorem",
    "decl": "Gomory.exists_domino_tiling",
    "file": "Gomory.lean",
    "yaml": "Q2916568:\n  title: Gomory's theorem\n  decl: Gomory.exists_domino_tiling\n"
  },
  "stuck": null,
  "repairs": [],
  "sources": [
    "https://api.imd.fun/artifacts/aa7606f5f9af082a6218f2295e28b623b4362c06386c4f2747d7dbe225ddf093",
    "https://github.com/Identity-md/lean-bundles/releases/download/pequalnp-e125f04d0dc6/fetch.json",
    "https://leanprover-community.github.io/1000-missing.html",
    "https://leanprover-community.github.io/1000.html",
    "https://raw.githubusercontent.com/leanprover-community/mathlib4/master/docs/1000.yaml",
    "https://api.github.com/repos/leanprover-community/mathlib4/commits/55b1401a3a21c8aebb70df9d5f815fd75828fa90",
    "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4+is%3Apr+is%3Aopen+Gomory",
    "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4+is%3Apr+is%3Aopen+chessboard",
    "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4+is%3Apr+is%3Aopen+domino",
    "https://api.github.com/search/issues?q=repo%3Aleanprover-community%2Fmathlib4+is%3Apr+is%3Aopen+Q2916568",
    "https://api.github.com/repos/rjwalters/lean-genius/git/trees/d8540c5a48f69a9f57b87c549c6e09dad63a0670?recursive=1",
    "https://github.com/search?q=%22mutilated+chessboard%22+language%3ALean&type=code",
    "https://leanprover-community.github.io/archive/stream/113489-new-members/topic/Advice.20on.20Definitions.html",
    "https://gist.githubusercontent.com/avigad/1080e327ad6806884c9c5091f5c449bd/raw",
    "https://light.artwaste.land/strata/the-mutilated-chessboard/",
    "https://raw.githubusercontent.com/SauersML/Cairn/6d1569de034a1ccc5cdc26880780b21442fe2bed/README.md"
  ]
}
