Lean / Reader
Lean reader

Proof.lean

445 lines58 declarationsno sorryimports OMAStatements.S_mo_52886

Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports OMAStatements.S_mo_52886, which it does not have: you can read it there, but it will not check. Build the project itself to check it.

  1. import OMAStatements.S_mo_52886
  2. namespace OMA.S_mo_52886
  3. private def dot (p q : Point) : Int := p.1*q.1+p.2*q.2
  4. theorem actDir_parity : ∀ g d : Fin 8, (actDir g d).val % 2 = d.val % 2 := by decide
  5. theorem nonidentity_rotation_fixes_no_dir :
  6. ∀ g d : Fin 8, g ≠ 0 → g.val < 4 → actDir g d ≠ d := by decide
  7. theorem reflection_fixed_dirs :
  8. ∀ g d : Fin 8, 4 ≤ g.val → (actDir g d = d ↔ d.val = g.val-4 ∨ d.val = g.val) := by
  9. decide
  10. theorem fixed_directions_parallel : ∀ (g d e : Fin 8), g ≠ 0 →
  11. actDir g d = d → actDir g e = e → d = e ∨ plus (point d) (point e) = (0,0) := by decide
  12. private theorem point_action : ∀ g d, point (actDir g d) = actPoint g (point d) := by decide
  13. private theorem action_origin (g : Fin 8) : actPoint g (0,0) = (0,0) := by
  14. unfold actPoint
  15. split <;> decide
  16. private theorem action_add (g : Fin 8) (p q : Point) :
  17. actPoint g (plus p q) = plus (actPoint g p) (actPoint g q) := by
  18. unfold actPoint
  19. split <;> simp [plus, Int.neg_add]
  20. private theorem action_injective (g : Fin 8) (p q : Point) :
  21. actPoint g p = actPoint g q → p = q := by
  22. rcases p with ⟨x,y⟩
  23. rcases q with ⟨z,t⟩
  24. unfold actPoint
  25. split <;> simp <;> grind
  26. private theorem plus_injective (p q r : Point) : plus p q = plus p r → q = r := by
  27. rcases p with ⟨x,y⟩
  28. rcases q with ⟨a,b⟩
  29. rcases r with ⟨c,d⟩
  30. simp [plus]
  31. private theorem nodup_map_iff (f : α → β) (hf : ∀ p q, f p = f q → p = q) (s : List α) :
  32. (s.map f).Nodup ↔ s.Nodup := by
  33. induction s with
  34. | nil => simp
  35. | cons a s ih =>
  36. simp only [List.map_cons, List.nodup_cons, List.mem_map, ih]
  37. constructor
  38. · rintro ⟨hn,hs⟩
  39. exact ⟨fun ha => hn ⟨a,ha,rfl⟩, hs⟩
  40. · rintro ⟨hn,hs⟩
  41. exact ⟨fun ⟨x,hx,heq⟩ => hn (hf x a heq ▸ hx), hs⟩
  42. theorem vertices_actWord (g : Fin 8) (w : List (Fin 8)) :
  43. vertices (actWord g w) = (vertices w).map (actPoint g) := by
  44. induction w with
  45. | nil => simp [actWord,vertices,action_origin]
  46. | cons d w ih =>
  47. change (0,0) :: (vertices (actWord g w)).map (plus (point (actDir g d))) =
  48. ((0,0) :: (vertices w).map (plus (point d))).map (actPoint g)
  49. rw [ih, List.map_cons, action_origin, List.map_map, List.map_map, point_action]
  50. congr 1
  51. apply List.map_congr_left
  52. intro p hp
  53. exact (action_add g (point d) p).symm
  54. theorem selfAvoid_actWord (g : Fin 8) (w : List (Fin 8)) :
  55. selfAvoid (actWord g w) ↔ selfAvoid w := by
  56. unfold selfAvoid
  57. rw [vertices_actWord]
  58. exact nodup_map_iff _ (action_injective g) _
  59. theorem selfAvoid_tail (d : Fin 8) (w : List (Fin 8)) :
  60. selfAvoid (d :: w) → selfAvoid w := by
  61. intro h
  62. exact (nodup_map_iff _ (plus_injective (point d)) _).mp (List.nodup_cons.mp h).2
  63. private theorem origin_mem (w : List (Fin 8)) : ((0,0) : Point) ∈ vertices w := by
  64. cases w <;> simp [vertices]
  65. private theorem no_immediate_return (d e : Fin 8) (w : List (Fin 8))
  66. (h : selfAvoid (d :: e :: w)) : plus (point d) (point e) ≠ (0,0) := by
  67. intro he
  68. apply (List.nodup_cons.mp h).1
  69. apply List.mem_map.mpr
  70. refine ⟨point e, ?_, he⟩
  71. change point e ∈ (0,0) :: (vertices w).map (plus (point e))
  72. apply List.mem_cons_of_mem
  73. exact List.mem_map.mpr ⟨(0,0),origin_mem w,by simp [plus]⟩
  74. private theorem fixed_cons {g : Fin 8} (hg : g ≠ 0) (rest : List (Fin 8)) (d : Fin 8)
  75. (h : selfAvoid (d :: rest)) (hf : actWord g (d :: rest) = d :: rest) :
  76. d :: rest = List.replicate (rest.length+1) d := by
  77. induction rest generalizing d with
  78. | nil => rfl
  79. | cons e w ih =>
  80. have hfix : actDir g d = d ∧ actWord g (e :: w) = e :: w := List.cons.inj hf
  81. have efix : actDir g e = e := (List.cons.inj hfix.2).1
  82. have de : d = e := (fixed_directions_parallel g d e hg hfix.1 efix).resolve_right
  83. (no_immediate_return d e w h)
  84. subst e
  85. have tail := ih d (selfAvoid_tail _ _ h) hfix.2
  86. change d :: d :: w = d :: List.replicate (w.length+1) d
  87. exact congrArg (List.cons d) tail
  88. theorem fixed_word_iff (g : Fin 8) (w : List (Fin 8)) (hg : g ≠ 0) (hw : w ≠ [])
  89. (h : selfAvoid w) :
  90. actWord g w = w ↔ ∃ d, actDir g d = d ∧ w = List.replicate w.length d := by
  91. cases w with
  92. | nil => contradiction
  93. | cons d w =>
  94. constructor
  95. · intro hf
  96. exact ⟨d,(List.cons.inj hf).1,fixed_cons hg w d h hf⟩
  97. · rintro ⟨e,he,hw'⟩
  98. rw [hw']
  99. simp [actWord,he]
  100. private theorem unit_dot_pos : ∀ d : Fin 8, 0 < dot (point d) (point d) := by decide
  101. private theorem dot_plus (p q r : Point) : dot p (plus q r) = dot p q + dot p r := by
  102. simp [dot,plus,Int.mul_add]
  103. omega
  104. private theorem repeat_forward (d : Fin 8) (n : Nat) :
  105. ∀ p ∈ vertices (List.replicate n d), 0 ≤ dot (point d) p := by
  106. induction n with
  107. | zero => simp [vertices,dot]
  108. | succ n ih =>
  109. intro p hp
  110. rw [List.replicate_succ] at hp
  111. rcases List.mem_cons.mp hp with h | h
  112. · subst p; simp [dot]
  113. · obtain ⟨q,hq,rfl⟩ := List.mem_map.mp h
  114. rw [dot_plus]
  115. have := ih q hq
  116. have := unit_dot_pos d
  117. omega
  118. theorem selfAvoid_replicate (n : Nat) (d : Fin 8) : selfAvoid (List.replicate n d) := by
  119. induction n with
  120. | zero => simp [selfAvoid,vertices]
  121. | succ n ih =>
  122. rw [List.replicate_succ]
  123. apply List.nodup_cons.mpr
  124. constructor
  125. · intro hz
  126. obtain ⟨p,hp,heq⟩ := List.mem_map.mp hz
  127. have hd := dot_plus (point d) (point d) p
  128. rw [heq] at hd
  129. have h0 : dot (point d) (0,0) = 0 := by simp [dot]
  130. have := unit_dot_pos d
  131. have := repeat_forward d n p hp
  132. omega
  133. · exact (nodup_map_iff _ (plus_injective (point d)) _).mpr ih
  134. private def product (g h : Fin 8) : Fin 8 :=
  135. ⟨((g.val % 4 + (if g.val < 4 then h.val%4 else 4-h.val%4)) % 4 +
  136. (if (g.val < 4) ↔ (h.val < 4) then 0 else 4)) % 8, by omega⟩
  137. private def inverse (g : Fin 8) : Fin 8 :=
  138. ⟨if g.val < 4 then (4-g.val)%4 else g.val, by split <;> omega⟩
  139. private theorem dir_product : ∀ g h d, actDir (product g h) d = actDir g (actDir h d) := by decide
  140. private theorem dir_zero : ∀ d, actDir 0 d = d := by decide
  141. private theorem prod_inverse : ∀ g, product (inverse g) g = 0 := by decide
  142. private theorem prod_inverse' : ∀ g, product g (inverse g) = 0 := by decide
  143. private theorem all_directions : ∀ g : Fin 8, g ∈ directions := by decide
  144. private theorem dirs_nodup : directions.Nodup := by decide
  145. private theorem left_permute : ∀ g, (directions.map (product g)).Perm directions := by decide
  146. private theorem word_product (g h : Fin 8) (w : Word) :
  147. actWord (product g h) w = actWord g (actWord h w) := by
  148. induction w with
  149. | nil => rfl
  150. | cons d w ih => simpa only [actWord, List.map_cons, dir_product] using congrArg (actDir g (actDir h d) :: ·) ih
  151. private theorem word_zero (w : Word) : actWord 0 w = w := by
  152. rw [actWord, show actDir 0 = id from funext dir_zero, List.map_id]
  153. private theorem inject_word (g : Fin 8) (x y : Word) (h : actWord g x = actWord g y) : x=y := by
  154. simpa only [← word_product, prod_inverse, word_zero] using congrArg (actWord (inverse g)) h
  155. private theorem same_iff (w v : Word) : sameShape w v ↔ ∃ g, actWord g w = v := by
  156. simp [sameShape, List.any_eq_true, all_directions]
  157. private theorem same_action (w v : Word) (g : Fin 8) : sameShape (actWord g w) v = sameShape w v := by
  158. apply Bool.eq_iff_iff.mpr
  159. rw [same_iff, same_iff]
  160. constructor
  161. · rintro ⟨h,rfl⟩
  162. exact ⟨product h g, word_product ..⟩
  163. · rintro ⟨h,rfl⟩
  164. refine ⟨product h (inverse g), ?_⟩
  165. rw [word_product, ← word_product (inverse g) g, prod_inverse, word_zero]
  166. private theorem rep_action (ws : List Word) (w : Word) (g : Fin 8) :
  167. representative ws (actWord g w) = representative ws w := by
  168. unfold representative
  169. rw [show sameShape (actWord g w) = sameShape w from funext (fun v => same_action w v g)]
  170. private theorem rep_exists (ws : List Word) (w : Word) (hw : w ∈ ws) :
  171. ∃ g, representative ws w = some (actWord g w) := by
  172. cases he : representative ws w with
  173. | none =>
  174. have hh := (List.find?_eq_none.mp he) w hw
  175. exact False.elim (hh ((same_iff ..).2 ⟨0, word_zero w⟩))
  176. | some v =>
  177. obtain ⟨g,rfl⟩ := (same_iff w v).1 (List.find?_some he)
  178. exact ⟨g,rfl⟩
  179. theorem mem_walks (w : Word) (a b : Nat) :
  180. w ∈ walks a b ↔ w.length = a+b ∧ selfAvoid w ∧ diagonalCount w=b := by
  181. have enum (n : Nat) (x : Word) : x ∈ allWords n ↔ x.length = n := by
  182. induction n generalizing x with
  183. | zero => simp [allWords]
  184. | succ n ih =>
  185. cases x with
  186. | nil => simp [allWords]
  187. | cons d x => simp [allWords, List.mem_flatMap, all_directions, ih]
  188. simp [walks, enum, selfAvoid]
  189. private theorem nodup_map_inj (xs : List α) (f : α → β) (hi : ∀ x y, f x = f y → x=y)
  190. (hn : xs.Nodup) : (xs.map f).Nodup := by
  191. induction xs with
  192. | nil => simp
  193. | cons x xs ih =>
  194. rcases List.nodup_cons.mp hn with ⟨hx,ht⟩
  195. apply List.nodup_cons.mpr
  196. constructor
  197. · rintro hl
  198. obtain ⟨y,hy,he⟩ := List.mem_map.mp hl
  199. exact hx (hi y x he ▸ hy)
  200. · exact ih ht
  201. theorem walks_nodup (a b : Nat) : (walks a b).Nodup := by
  202. have enum (n : Nat) : (allWords n).Nodup := by
  203. induction n with
  204. | zero => simp [allWords]
  205. | succ n ih =>
  206. apply List.pairwise_flatMap.mpr
  207. constructor
  208. · intro d hd
  209. exact nodup_map_inj _ _ (fun _ _ h => (List.cons.inj h).2) ih
  210. · apply List.Pairwise.imp _ dirs_nodup
  211. intro x y h v hv w hw
  212. obtain ⟨v',_,rfl⟩ := List.mem_map.mp hv
  213. obtain ⟨w',_,rfl⟩ := List.mem_map.mp hw
  214. exact fun he => h (List.cons.inj he).1
  215. exact (enum _).filter _
  216. private theorem diagonal_action (g : Fin 8) (w : Word) : diagonalCount (actWord g w) = diagonalCount w := by
  217. simp [diagonalCount, actWord, Function.comp_def, actDir_parity]
  218. private theorem walk_action (g : Fin 8) (w : Word) (a b : Nat) :
  219. actWord g w ∈ walks a b ↔ w ∈ walks a b := by
  220. simp only [mem_walks, selfAvoid_actWord, diagonal_action]
  221. simp [actWord]
  222. private theorem perm_walks (g : Fin 8) (a b : Nat) :
  223. ((walks a b).map (actWord g)).Perm (walks a b) := by
  224. apply (List.perm_ext_iff_of_nodup (nodup_map_inj _ _ (inject_word g) (walks_nodup ..)) (walks_nodup ..)).2
  225. intro w
  226. rw [List.mem_map]
  227. constructor
  228. · rintro ⟨u,hu,rfl⟩
  229. exact (walk_action ..).2 hu
  230. · intro hw
  231. refine ⟨actWord (inverse g) w,(walk_action ..).2 hw,?_⟩
  232. rw [← word_product, prod_inverse', word_zero]
  233. private theorem representative_multiplicity (ws : List Word) (w : Word) (hw : w ∈ ws) :
  234. directions.countP (fun g => isRepresentative ws (actWord g w)) =
  235. directions.countP (fun g => actWord g w == w) := by
  236. obtain ⟨h,hh⟩ := rep_exists ws w hw
  237. rw [← (left_permute h).countP_eq (fun g => isRepresentative ws (actWord g w)), List.countP_map]
  238. apply List.countP_congr
  239. intro g _
  240. simp only [Function.comp_apply, isRepresentative, rep_action, hh, beq_iff_eq, Option.some.injEq, word_product]
  241. exact ⟨fun e => (inject_word h _ _ e).symm, fun e => congrArg (actWord h) e.symm⟩
  242. /-- Each anchored legal walk shares its square-symmetry orbit with exactly one
  243. word counted by the representative predicate. -/
  244. theorem unique_representative (a b : Nat) (w : Word) (hw : w ∈ walks a b) :
  245. ∃ v, v ∈ walks a b ∧ sameShape w v ∧ isRepresentative (walks a b) v ∧
  246. ∀ u, sameShape w u → isRepresentative (walks a b) u → u = v := by
  247. obtain ⟨g,hg⟩ := rep_exists (walks a b) w hw
  248. refine ⟨actWord g w,(walk_action ..).2 hw,(same_iff ..).2 ⟨g,rfl⟩,?_,?_⟩
  249. · simp only [isRepresentative, rep_action, hg, beq_self_eq_true]
  250. · intro u hu hr
  251. obtain ⟨h,rfl⟩ := (same_iff ..).1 hu
  252. simp only [isRepresentative, rep_action, hg, beq_iff_eq, Option.some.injEq] at hr
  253. exact hr.symm
  254. private theorem swap_counts (xs : List α) (ys : List β) (p : α → β → Bool) :
  255. (xs.map (fun x => ys.countP (p x))).sum =
  256. (ys.map (fun y => xs.countP (fun x => p x y))).sum := by
  257. induction xs with
  258. | nil =>
  259. simp only [List.map_nil, List.sum_nil, List.countP_nil]
  260. induction ys with
  261. | nil => rfl
  262. | cons _ ys ih => simpa only [List.map_cons, List.sum_cons, Nat.zero_add] using ih
  263. | cons x xs ih =>
  264. simp only [List.map_cons, List.sum_cons, ih]
  265. clear ih
  266. induction ys with
  267. | nil => simp
  268. | cons y ys hy =>
  269. simp only [List.map_cons, List.sum_cons, List.countP_cons] at hy ⊢
  270. split <;> omega
  271. private theorem burnside (a b : Nat) :
  272. 8 * shapeCount a b =
  273. ((walks a b).map (fun w => directions.countP (fun g => actWord g w == w))).sum := by
  274. let ws := walks a b
  275. let r := isRepresentative ws
  276. have hon (g : Fin 8) : ws.countP (fun w => r (actWord g w)) = ws.countP r := by
  277. simpa only [List.countP_map, Function.comp_def] using (perm_walks g a b).countP_eq r
  278. calc
  279. 8 * shapeCount a b = (directions.map (fun g => ws.countP (fun w => r (actWord g w)))).sum := by
  280. simp only [hon, directions, List.map_cons, List.map_nil, List.sum_cons, List.sum_nil]
  281. change 8 * ws.countP r = _
  282. omega
  283. _ = (ws.map (fun w => directions.countP (fun g => r (actWord g w)))).sum := swap_counts ..
  284. _ = _ := congrArg List.sum (List.map_congr_left fun w hw => representative_multiplicity ws w hw)
  285. private def straight (w : Word) : Bool :=
  286. directions.any (fun d => List.replicate w.length d == w)
  287. private theorem straight_iff (w : Word) : straight w ↔ ∃ d, w = List.replicate w.length d := by
  288. simp only [straight, List.any_eq_true, all_directions, true_and, beq_iff_eq]
  289. exact exists_congr fun _ => eq_comm
  290. private theorem rep_dir_inj (n : Nat) (hn : n ≠ 0) (x y : Fin 8) :
  291. List.replicate n x = List.replicate n y → x=y := by
  292. cases n with
  293. | zero => contradiction
  294. | succ n => exact fun h => (List.cons.inj h).1
  295. private theorem rep_stab (n : Nat) (hn : n ≠ 0) (d : Fin 8) :
  296. directions.countP (fun g => actWord g (List.replicate n d) == List.replicate n d) = 2 := by
  297. have table : ∀ d : Fin 8, directions.countP (fun g => actDir g d == d) = 2 := by decide
  298. rw [← table d]
  299. apply List.countP_congr
  300. intro g _
  301. simp only [actWord, List.map_replicate, beq_iff_eq]
  302. exact ⟨rep_dir_inj n hn _ _, congrArg (List.replicate n)⟩
  303. private theorem stab_size (w : Word) (hn : w ≠ []) (ha : selfAvoid w) :
  304. directions.countP (fun g => actWord g w == w) = 1 + (if straight w then 1 else 0) := by
  305. by_cases ht : straight w
  306. · obtain ⟨d,he⟩ := (straight_iff w).1 ht
  307. simp only [ht, if_true, Nat.reduceAdd]
  308. have hlen : w.length ≠ 0 := by simpa using hn
  309. simpa only [← he] using rep_stab w.length hlen d
  310. · rw [if_neg ht, Nat.add_zero]
  311. have table : directions.countP (fun g => g == 0) = 1 := by decide
  312. rw [← table]
  313. apply List.countP_congr
  314. intro g _
  315. simp only [beq_iff_eq]
  316. constructor
  317. · intro he
  318. by_cases hg : g=0
  319. · exact hg
  320. · obtain ⟨d,_,hh⟩ := (fixed_word_iff g w hg hn ha).1 he
  321. exact False.elim (ht ((straight_iff w).2 ⟨d,hh⟩))
  322. · rintro rfl
  323. exact word_zero w
  324. private theorem sum_stab (a b : Nat) (hn : a+b ≠ 0) :
  325. 8*shapeCount a b = rootedCount a b + (walks a b).countP straight := by
  326. rw [burnside]
  327. have changeWeight : (walks a b).map (fun w => directions.countP (fun g => actWord g w == w)) =
  328. (walks a b).map (fun w => 1 + (if straight w then 1 else 0)) := by
  329. apply List.map_congr_left
  330. intro w hw
  331. rcases (mem_walks ..).1 hw with ⟨hl,ha,_⟩
  332. apply stab_size w _ ha
  333. rintro rfl
  334. exact hn hl.symm
  335. rw [changeWeight]
  336. unfold rootedCount
  337. generalize walks a b = ws
  338. induction ws with
  339. | nil => simp
  340. | cons w ws ih =>
  341. simp only [List.map_cons, List.sum_cons, List.length_cons, List.countP_cons] at *
  342. split <;> omega
  343. private theorem count_straight (a b : Nat) (hn : a+b ≠ 0) :
  344. (walks a b).countP straight = if a=0 ∨ b=0 then 4 else 0 := by
  345. let n := a+b
  346. let ds := directions.filter (fun d => diagonalCount (List.replicate n d) == b)
  347. have hp : ((walks a b).filter straight).Perm (ds.map (List.replicate n)) := by
  348. apply (List.perm_ext_iff_of_nodup ((walks_nodup a b).filter _)
  349. (nodup_map_inj _ _ (rep_dir_inj n hn) (dirs_nodup.filter _))).2
  350. intro w
  351. simp only [List.mem_filter, mem_walks, straight_iff, List.mem_map]
  352. constructor
  353. · rintro ⟨⟨hl,_,hc⟩,d,hd⟩
  354. rw [hl] at hd
  355. refine ⟨d,⟨all_directions d,?_⟩,hd.symm⟩
  356. change (diagonalCount (List.replicate (a+b) d) == b) = true
  357. simpa only [← hd, beq_iff_eq] using hc
  358. · rintro ⟨d,hm,rfl⟩
  359. obtain ⟨_,hc⟩ := hm
  360. exact ⟨⟨List.length_replicate, selfAvoid_replicate .., beq_iff_eq.mp hc⟩,d,by simp⟩
  361. rw [List.countP_eq_length_filter, hp.length_eq, List.length_map]
  362. unfold ds n
  363. have table : ∀ (x y : Bool),
  364. (directions.filter (fun d => if d.val % 2 == 1 then x else y)).length =
  365. (if x then 4 else 0) + (if y then 4 else 0) := by decide
  366. have counts (d : Fin 8) : (diagonalCount (List.replicate (a+b) d) == b) =
  367. (if d.val % 2 == 1 then a+b == b else 0 == b) := by
  368. unfold diagonalCount
  369. rw [List.countP_replicate]
  370. split <;> rfl
  371. rw [show (fun d => diagonalCount (List.replicate (a+b) d) == b) = _ from funext counts, table]
  372. simp only [beq_iff_eq]
  373. split <;> split <;> split <;> omega
  374. /-- Exact number of square-symmetry classes of anchored vertex-self-avoiding walks,
  375. with prescribed counts of axial and diagonal edges. -/
  376. theorem orbit_formula (a b : Nat) :
  377. 8 * shapeCount a b = rootedCount a b + correction a b := by
  378. by_cases hn : a+b = 0
  379. · obtain ⟨rfl,rfl⟩ : a=0 ∧ b=0 := by omega
  380. decide
  381. · rw [sum_stab a b hn, count_straight a b hn]
  382. simp only [correction, if_neg hn]
  383. theorem shapeCount_eq (a b : Nat) :
  384. shapeCount a b = (rootedCount a b + correction a b) / 8 := by
  385. have h := orbit_formula a b
  386. omega
  387. end OMA.S_mo_52886
  388. theorem OMA.solution : OMA.S_mo_52886.claim := by
  389. exact OMA.S_mo_52886.shapeCount_eq