Lean / Reader
Lean reader

HouseSpectrum.lean

269 lines64 declarationsno sorryimports Mathlib.Tactic.FinCases Mathlib.Data.Fintype.Basic Mathlib.Data.List.Nodup Mathlib.Data.Finset.Max Mathlib.Data.Finset.Card Mathlib.Data.List.Permutation +1
  1. import Mathlib.Tactic.FinCases
  2. import Mathlib.Data.Fintype.Basic
  3. import Mathlib.Data.List.Nodup
  4. import Mathlib.Data.Finset.Max
  5. import Mathlib.Data.Finset.Card
  6. import Mathlib.Data.List.Permutation
  7. import Mathlib.Data.Fin.VecNotation
  8. /-! An exact spectrum for Problem 4.2 of Halfpap, Lidický, and Masařík.
  9. The ten edges of K₅ are listed in lexicographic order. The complement
  10. of a spanning path in K₅ is the house graph. -/
  11. namespace HouseSpectrum
  12. abbrev Edge := Fin 10
  13. def endpoints : Edge → Nat × Nat :=
  14. ![(0,1), (0,2), (0,3), (0,4), (1,2), (1,3), (1,4), (2,3), (2,4), (3,4)]
  15. def incident (e f : Edge) : Bool :=
  16. let (a,b) := endpoints e
  17. let (u,v) := endpoints f
  18. a == u || a == v || b == u || b == v
  19. def edges := List.finRange 10
  20. def houseEdges (order : List Nat) : List Edge :=
  21. let path := order.zipWith (fun a b => (min a b, max a b)) order.tail
  22. edges.filter fun e => !path.contains (endpoints e)
  23. def houses : List (List Edge) := (List.range 5).permutations'.map houseEdges
  24. def Proper {α} (c : Edge → α) : Prop :=
  25. ∀ e f, e ≠ f → incident e f = true → c e ≠ c f
  26. def AvoidsHouse {α} (c : Edge → α) : Prop :=
  27. ∀ h ∈ houses, (h.map c).Nodup → False
  28. def used {α} [DecidableEq α] (c : Edge → α) : Nat := (Finset.univ.image c).card
  29. def Feasible (k : Nat) : Prop :=
  30. ∃ c : Edge → Nat, Proper c ∧ AvoidsHouse c ∧ used c = k
  31. instance {α} [DecidableEq α] (c : Edge → α) : Decidable (Proper c) :=
  32. inferInstanceAs (Decidable (∀ (e f : Edge), e ≠ f → incident e f = true → c e ≠ c f))
  33. instance {α} [DecidableEq α] (c : Edge → α) : Decidable (AvoidsHouse c) :=
  34. inferInstanceAs (Decidable (∀ h ∈ houses, (h.map c).Nodup → False))
  35. /-- The first edge with the color of e. -/
  36. def representative (c : Edge → Nat) (e : Edge) : Edge :=
  37. (Finset.univ.filter fun j => c j = c e).min' ⟨e, by simp⟩
  38. theorem repr_color (c : Edge → Nat) (e : Edge) :
  39. c (representative c e) = c e :=
  40. (Finset.mem_filter.mp (Finset.min'_mem (Finset.univ.filter fun j => c j = c e) _)).2
  41. theorem repr_le (c : Edge → Nat) (e : Edge) : representative c e ≤ e :=
  42. Finset.min'_le _ _ (by simp)
  43. theorem repr_eq (c : Edge → Nat) (e f : Edge) :
  44. representative c e = representative c f ↔ c e = c f := by
  45. constructor
  46. · intro h
  47. simpa only [repr_color] using congrArg c h
  48. · intro h
  49. unfold representative
  50. congr 1
  51. ext j
  52. simp [h]
  53. theorem used_repr (c : Edge → Nat) : used (representative c) = used c := by
  54. unfold used
  55. let s := Finset.univ.image (representative c)
  56. have inj : Set.InjOn c (s : Set Edge) := by
  57. intro a ha b hb hab
  58. obtain ⟨i, _, rfl⟩ := Finset.mem_image.mp ha
  59. obtain ⟨j, _, rfl⟩ := Finset.mem_image.mp hb
  60. exact (repr_eq c i j).mpr (by simpa only [repr_color] using hab)
  61. calc
  62. _ = (s.image c).card := (Finset.card_image_of_injOn inj).symm
  63. _ = _ := by simp [s, Finset.image_image, Function.comp_def, repr_color]
  64. def choices (e : Edge) : List Edge :=
  65. edges.filter fun j => decide (j ≤ e) && (j == e || !incident e j)
  66. theorem repr_choice (c : Edge → Nat) (hc : Proper c) (e : Edge) :
  67. representative c e ∈ choices e := by
  68. have he : representative c e ∈ edges := by simp [edges, List.mem_finRange]
  69. by_cases h : e = representative c e
  70. · simp [choices, ← h, edges, List.mem_finRange]
  71. · have hi : incident e (representative c e) = false := by
  72. cases hi : incident e (representative c e) with
  73. | false => rfl
  74. | true => exact False.elim (hc _ _ h hi (repr_color c e).symm)
  75. simp [choices, repr_le, hi, he]
  76. /-- Coordinates 0–3 form a star, so each is its own representative. -/
  77. def coloringRow (a b c d e f : Edge) : Edge → Edge := ![0,1,2,3,a,b,c,d,e,f]
  78. def allowed (r : Edge → Edge) : Bool :=
  79. decide (Proper r → AvoidsHouse r → used r = 5 ∨ used r = 7)
  80. /-- Certificate 0–99 exhibits incident edges of equal color, certificate 100
  81. checks the palette size, and a larger certificate encodes a spanning path. -/
  82. def certify (r : Edge → Edge) (z : Nat) : Bool :=
  83. if z < 100 then
  84. let i : Edge := Fin.ofNat 10 (z / 10)
  85. let j : Edge := Fin.ofNat 10 (z % 10)
  86. decide (i ≠ j ∧ incident i j = true ∧ r i = r j)
  87. else if z = 100 then decide (used r = 5 ∨ used r = 7)
  88. else
  89. let n := z - 101
  90. let p := [n % 5, n / 5 % 5, n / 25 % 5, n / 125 % 5, n / 625 % 5]
  91. decide (p.Perm (List.range 5) ∧ ((houseEdges p).map r).Nodup)
  92. theorem certify_sound (r : Edge → Edge) (z : Nat) (hc : certify r z = true) :
  93. allowed r = true := by
  94. apply decide_eq_true
  95. intro proper avoids
  96. unfold certify at hc
  97. split at hc
  98. · obtain ⟨hne, hi, heq⟩ := of_decide_eq_true hc
  99. exact False.elim (proper _ _ hne hi heq)
  100. · split at hc
  101. · exact of_decide_eq_true hc
  102. · obtain ⟨hperm, hn⟩ := of_decide_eq_true hc
  103. exact False.elim (avoids _ (List.mem_map.mpr ⟨_, List.mem_permutations'.mpr hperm, rfl⟩) hn)
  104. /-- One twelve-bit witness per row, packed in lexicographic order of the
  105. six free representatives. Each word covers the 64 choices of the last three. -/
  106. def witnessWords : List Nat := [
  107. 21231016646847301677251001551585684695590862651402010004096810485384074531710512648235684892927058336126103625455450509522048546844455886948839726653192587285716006575498941266485980699967515867724311266961087285375968526492467256,
  108. 17439763674195997806313322703119686823398475405950682322929522283076518638235247074779098121152072768186186962653975425514992107943658476680448648266640862613774097410734821893849133855690727341138485461028316865089394616672903214,
  109. 1141546203357337882947943813977223305201866720235238443229087526324541972900484732205365612977553836926308768166657401661513490775003954074022400528689393222261131162690735051513841223644438730433405872898676603558711787849336991822,
  110. 37912529042213640687626303148182553406336177299091226703308314799623262153562049910032569159574184333327618387565487378359443249537029677104721033645983937885292773606406080501889400553482220953902277145925479766231236866629455950,
  111. 17439763674195997806313322703119686823398475405950682322929522283076518638235247074779098121152072768186186962653975425514992107943658476680448648266640862613774097410734821893849133855690727341138485461028316865089394616672903214,
  112. 1141546204283614461404242443772001639650822624511305410146830464818375052240593719697994227580637338597575693842934401647566303153934833362498133158601775329559453717101542757524295301214175625408051969698993918500484845014224265294,
  113. 1141546203583255078750150089548122122896142997445459570022775703054438812681815684213277482028717176353740838378777575049293837317062485876800918574925922504518117614937680862646355666634205653580725773946371303922284684236630020174,
  114. 17439763674195997806313322703119686823398475405950682322929522283076518638235247074779098121152072768186186962653975425514992107943658476680448648266640862613774097410734821893849133855690727341138485461028316865089394616672903214,
  115. 1149128776010609529819200504673171039533857107439571372445541771126410380702331779958877260934362659924605601058633617451783569218194785150287305190107297532473199593025689754435381559244006535740222068665608008140363750571856158798,
  116. 21231016646847301677251001551623966567615535276809526306175070605484457472634213830165858582272088587357097171926578692290000662984155481174485759733768473279071073130194770020187578364433137072000324794950140666845038135369498680,
  117. 37912528813534697859417097312176947190076073618339870272212440640565058225341625681019157921352014828182886144740631968106641424418180282699621492249643824844794095517827059177093204596679866931586455915982078005275141169754648654,
  118. 1141546203357337882947943813977472137358536823417667485675727523069103956296238813204976957097471929229077468545932129283725836844357571315097962510775593974458538535069595070504869460609885093554275842349396709252122524164718571598,
  119. 17060638376930867419219554818269258848976769418864797924604967450835724754795350399240422075040071186269095941726715090177709670814448509796091068956496496035213790945284064896156761380567015877200692298832049107152668646745231405,
  120. 17060638376930867419219554818269258848976769418864797924604967450835724754795350399240422075040071186269095941726715090177709670814448509796091068956496496035213790945284064896156761380567015877200692298832049107152668646745231405,
  121. 17060638376930867419219554818269258848976769418864797924604967450835724754795350399240422075040071186269095941726715090177709670814448509796091068956496496035213790945284064896156761380567015877200692298832049107152668646745231405,
  122. 1141546203583255078750150089548370955055567440236586191382231851431812171901209836206857461978563553704730748906001018767749654597983423481215318002275745918723829507001626121017704958119376336911628895338614686776329832915978702926,
  123. 951983566949859681389221845853047852235646397451574851851174976037784165009370957780381425034868793930296345882639459135112351671137625912306338764137721806120154229364256333158555429300652056643451311270332922636492075560216092750,
  124. 1149128776010609529819200504685052774870139812507981379499394142275628847812343847599037771141147032959748786187499610885560762253725045847431681359658804280105751668419759799588244137796470896163499125077849191580125957595307761742,
  125. 21231016646847301677251001551623966567615535276809526306175070605484457472634213830165858582272088587357097171926578778887816479235758145524024441368084528399377162065242391870772858606927841980516417082990994444456654315949621304,
  126. 951760458618845479023047857316691144020142478556370822000025969185471660741855864895254180310109508655960701807986933736390136640149200530277947058793913162741231054484532924816285468630175949490868791132759413685462063360063549518,
  127. 1111123644110153498091899422139008903675529133714181826993137949779650919051028675998827524243151465624601037707208293509031920669477817213006714073049843962291335592145324786613386142378440992362207075857056433812881555597524447310,
  128. 1141276827533818652702279618110332652907472560824180137629967144663281215754319992215457834962654383435312111133341792262986068684288621929217789905206917127986679286569191514061470642903288721945856440645635957339144133637199646798,
  129. 1065470280758553291668890622154179533451041717205520425997177747888241857975329759856779536050691058900670624491477160672368734938844511479526306949523535065720282913174555678209624296714194014053406766137538500114993607032608972878,
  130. 1111123686402586543706314909354253832673694846450303056341070344948941392820156128839915139303465345192946771049569479191688993662001427982068303767623175317508756265791175402526107863144914940525725020919319683056575744400148455502,
  131. 883694748176962859362557301512200703587465824157357668121381528401324375025173886229283799985992124992677548469822546786166249977450531955711540696620990963195830227807006368737180588704315897017693166637134594029252156433564454990,
  132. 883694787984263621313593626658441581281028957432004466725761781022269320824648936014421652107546458203319934707452501971967148170381536707382283494817564974752768903578306989784390094352900915406531570080102223168077816519744348238,
  133. 1149128776010609529819395672414211448258734514586683545437326062509993785212027826120883109698817569846284629460120047761407630827515415444441538700828918858647765800852467487220944507612667846270776867988726262914430350457919823950]
  134. def sampleWitness (a b c d e f : Edge) : Nat :=
  135. let group := (choices 4).idxOf a * 9 + (choices 5).idxOf b * 3 + (choices 6).idxOf c
  136. let row := (choices 7).idxOf d * 16 + (choices 8).idxOf e * 4 + (choices 9).idxOf f
  137. ((witnessWords[group]?.getD 0) >>> (12 * row)) % 4096
  138. def lastThree (a b c : Edge) : Bool :=
  139. (choices 7).all fun d => (choices 8).all fun e => (choices 9).all fun f =>
  140. certify (coloringRow a b c d e f) (sampleWitness a b c d e f)
  141. set_option maxRecDepth 100000
  142. set_option maxHeartbeats 0
  143. private theorem choice_0 : choices 0 = [0] := by decide
  144. private theorem choice_1 : choices 1 = [1] := by decide
  145. private theorem choice_2 : choices 2 = [2] := by decide
  146. private theorem choice_3 : choices 3 = [3] := by decide
  147. private theorem choice_4 : choices 4 = [2, 3, 4] := by decide
  148. private theorem choice_5 : choices 5 = [1, 3, 5] := by decide
  149. private theorem choice_6 : choices 6 = [1, 2, 6] := by decide
  150. private theorem case_2_1_1 : lastThree 2 1 1 = true := by decide +kernel
  151. private theorem case_2_1_2 : lastThree 2 1 2 = true := by decide +kernel
  152. private theorem case_2_1_6 : lastThree 2 1 6 = true := by decide +kernel
  153. private theorem case_2_3_1 : lastThree 2 3 1 = true := by decide +kernel
  154. private theorem case_2_3_2 : lastThree 2 3 2 = true := by decide +kernel
  155. private theorem case_2_3_6 : lastThree 2 3 6 = true := by decide +kernel
  156. private theorem case_2_5_1 : lastThree 2 5 1 = true := by decide +kernel
  157. private theorem case_2_5_2 : lastThree 2 5 2 = true := by decide +kernel
  158. private theorem case_2_5_6 : lastThree 2 5 6 = true := by decide +kernel
  159. private theorem case_3_1_1 : lastThree 3 1 1 = true := by decide +kernel
  160. private theorem case_3_1_2 : lastThree 3 1 2 = true := by decide +kernel
  161. private theorem case_3_1_6 : lastThree 3 1 6 = true := by decide +kernel
  162. private theorem case_3_3_1 : lastThree 3 3 1 = true := by decide +kernel
  163. private theorem case_3_3_2 : lastThree 3 3 2 = true := by decide +kernel
  164. private theorem case_3_3_6 : lastThree 3 3 6 = true := by decide +kernel
  165. private theorem case_3_5_1 : lastThree 3 5 1 = true := by decide +kernel
  166. private theorem case_3_5_2 : lastThree 3 5 2 = true := by decide +kernel
  167. private theorem case_3_5_6 : lastThree 3 5 6 = true := by decide +kernel
  168. private theorem case_4_1_1 : lastThree 4 1 1 = true := by decide +kernel
  169. private theorem case_4_1_2 : lastThree 4 1 2 = true := by decide +kernel
  170. private theorem case_4_1_6 : lastThree 4 1 6 = true := by decide +kernel
  171. private theorem case_4_3_1 : lastThree 4 3 1 = true := by decide +kernel
  172. private theorem case_4_3_2 : lastThree 4 3 2 = true := by decide +kernel
  173. private theorem case_4_3_6 : lastThree 4 3 6 = true := by decide +kernel
  174. private theorem case_4_5_1 : lastThree 4 5 1 = true := by decide +kernel
  175. private theorem case_4_5_2 : lastThree 4 5 2 = true := by decide +kernel
  176. private theorem case_4_5_6 : lastThree 4 5 6 = true := by decide +kernel
  177. theorem enumeration :
  178. (choices 4).all (fun a => (choices 5).all fun b => (choices 6).all fun c => lastThree a b c) =
  179. true := by
  180. simp [choice_4, choice_5, choice_6,
  181. case_2_1_1, case_2_1_2, case_2_1_6, case_2_3_1, case_2_3_2, case_2_3_6, case_2_5_1, case_2_5_2, case_2_5_6, case_3_1_1, case_3_1_2, case_3_1_6, case_3_3_1, case_3_3_2, case_3_3_6, case_3_5_1, case_3_5_2, case_3_5_6, case_4_1_1, case_4_1_2, case_4_1_6, case_4_3_1, case_4_3_2, case_4_3_6, case_4_5_1, case_4_5_2, case_4_5_6]
  182. theorem allowed_repr (c : Edge → Nat) (hp : Proper c) : allowed (representative c) = true := by
  183. let r := representative c
  184. have h i := repr_choice c hp i
  185. have cert := List.all_eq_true.mp enumeration (r 4) (h 4)
  186. have cert := List.all_eq_true.mp cert (r 5) (h 5)
  187. have cert := List.all_eq_true.mp cert (r 6) (h 6)
  188. have cert := List.all_eq_true.mp cert (r 7) (h 7)
  189. have cert := List.all_eq_true.mp cert (r 8) (h 8)
  190. have cert := List.all_eq_true.mp cert (r 9) (h 9)
  191. have star0 : r 0 = 0 := by simpa only [choice_0, List.mem_singleton] using h 0
  192. have star1 : r 1 = 1 := by simpa only [choice_1, List.mem_singleton] using h 1
  193. have star2 : r 2 = 2 := by simpa only [choice_2, List.mem_singleton] using h 2
  194. have star3 : r 3 = 3 := by simpa only [choice_3, List.mem_singleton] using h 3
  195. have eq : coloringRow (r 4) (r 5) (r 6) (r 7) (r 8) (r 9) = r := by
  196. funext i
  197. fin_cases i <;> first | rfl | exact star0.symm | exact star1.symm | exact star2.symm | exact star3.symm
  198. have result := certify_sound _ _ cert
  199. rwa [eq] at result
  200. theorem feasible_iff (k : Nat) : Feasible k ↔ k = 5 ∨ k = 7 := by
  201. constructor
  202. · rintro ⟨c, hp, ha, hk⟩
  203. let r := representative c
  204. have cert := allowed_repr c hp
  205. have proper : Proper r := by
  206. intro i j h d eq
  207. exact hp i j h d ((repr_eq c i j).mp eq)
  208. have avoid : AvoidsHouse r := by
  209. intro h hh hn
  210. apply ha h hh
  211. have eq : (h.map r).map c = h.map c := by
  212. simp [r, List.map_map, Function.comp_def, repr_color]
  213. rw [← eq]
  214. apply List.Nodup.map_on _ hn
  215. intro a ha b hb heq
  216. rcases List.mem_map.mp ha with ⟨i, _, rfl⟩
  217. rcases List.mem_map.mp hb with ⟨j, _, rfl⟩
  218. apply (repr_eq c i j).mpr
  219. simpa [r, repr_color] using heq
  220. have result := (of_decide_eq_true cert) proper avoid
  221. simpa [r, used_repr, hk] using result
  222. · rintro (rfl | rfl)
  223. · refine ⟨![0,1,2,3,4,3,1,0,2,4], ?_, ?_, ?_⟩ <;> decide +kernel
  224. · refine ⟨![0,1,2,3,4,5,6,6,5,4], ?_, ?_, ?_⟩ <;> decide +kernel
  225. theorem not_an_interval :
  226. ¬ (∀ a b k : Nat, Feasible a → Feasible b → a ≤ k → k ≤ b → Feasible k) := by
  227. intro h
  228. have middle := h 5 7 6 ((feasible_iff 5).mpr (Or.inl rfl))
  229. ((feasible_iff 7).mpr (Or.inr rfl)) (by decide) (by decide)
  230. exact (by decide : ¬ (6 = 5 ∨ 6 = 7)) ((feasible_iff 6).mp middle)
  231. #print axioms feasible_iff
  232. #print axioms not_an_interval
  233. end HouseSpectrum