Lean / Reader
Lean reader

SevenColumns.lean

344 lines56 declarations3 × sorry / admit / native_decideimports Std Init.Data.Vector.Lemmas
  1. import Std
  2. import Init.Data.Vector.Lemmas
  3. set_option maxRecDepth 20000
  4. set_option maxHeartbeats 2000000
  5. /-! OEIS A296553. Row masks are read from the low bit to the high bit.
  6. The allowed diagonals are northeast/southwest. Missing neighbours are zero.
  7. -/
  8. namespace SevenColumns
  9. abbrev Row := Fin 128
  10. def rows : List Row := List.finRange 128
  11. def bit (r : Row) (j : Nat) : Nat := if j < 7 then (r.val / 2^j) % 2 else 0
  12. def left (r : Row) (j : Nat) : Nat := if j=0 then 0 else bit r (j-1)
  13. def neighbours (a b c : Row) (j : Nat) : Nat :=
  14. left b j + bit b (j+1) + bit a j + bit a (j+1) + bit c j + left c j
  15. def legal (a b c : Row) : Bool :=
  16. (List.range 7).all fun j =>
  17. bit b j == 0 || neighbours a b c j == 3 || neighbours a b c j == 6
  18. /-- Test the centres of all consecutive three-row windows and the lower boundary. -/
  19. def legalFrom (a b : Row) : List Row → Bool
  20. | [] => legal a b 0
  21. | c::cs => legal a b c && legalFrom b c cs
  22. /-- Enumerate every height-n board, regardless of legality. -/
  23. def boards : Nat → List (List Row)
  24. | 0 => [[]]
  25. | n+1 => rows.flatMap fun r => (boards n).map (r :: ·)
  26. private theorem rows_complete : ∀ r : Row, r ∈ rows := by decide
  27. private theorem rows_distinct : rows.Nodup := by decide
  28. theorem boards_complete (n : Nat) (rs : List Row) : rs ∈ boards n ↔ rs.length = n := by
  29. induction n generalizing rs with
  30. | zero => cases rs <;> simp [boards]
  31. | succ n ih =>
  32. cases rs with
  33. | nil => simp [boards]
  34. | cons r rs =>
  35. simp only [boards,List.mem_flatMap,List.mem_map,List.cons.injEq,List.length_cons,
  36. Nat.succ.injEq]
  37. constructor
  38. · rintro ⟨_,_,_,hm,_,rfl⟩
  39. exact (ih _).1 hm
  40. · intro h
  41. exact ⟨r,rows_complete r,rs,(ih _).2 h,rfl,rfl⟩
  42. theorem boards_distinct (n : Nat) : (boards n).Nodup := by
  43. induction n with
  44. | zero => simp [boards]
  45. | succ n ih =>
  46. apply List.pairwise_flatMap.2
  47. constructor
  48. · intro r _
  49. apply List.pairwise_map.2
  50. exact ih.imp (by intro a b h e; exact h (List.cons.inj e).2)
  51. · apply rows_distinct.imp
  52. intro a b hab x hx y hy hxy
  53. obtain ⟨_,_,rfl⟩ := List.mem_map.1 hx
  54. obtain ⟨_,_,rfl⟩ := List.mem_map.1 hy
  55. exact hab (List.cons.inj hxy).1
  56. /-- Literal count by testing each binary board. The leading zero row satisfies
  57. the local rule vacuously and supplies the upper boundary. -/
  58. def count (n : Nat) : Nat := (boards n).countP (legalFrom 0 0)
  59. def addOver (xs : List α) (f : α → Nat) : Nat := (xs.map f).sum
  60. def ways : Nat → Row → Row → Nat
  61. | 0, a, b => if legal a b 0 then 1 else 0
  62. | n+1, a, b => addOver rows (fun c => if legal a b c then ways n b c else 0)
  63. private theorem countBlocks (as : List α) (chunks : α → List β) (ok : β → Bool) :
  64. (as.flatMap chunks).countP ok = addOver as (fun a => (chunks a).countP ok) := by
  65. induction as with
  66. | nil => simp [addOver]
  67. | cons a as ih => simp [addOver, List.flatMap_cons, ih, Nat.add_comm]
  68. private theorem addOver_congr (as : List α) (f g : α → Nat) (h : ∀ a, f a = g a) :
  69. addOver as f = addOver as g := by rw [funext h]
  70. theorem literalToWays (n : Nat) (a b : Row) :
  71. (boards n).countP (legalFrom a b) = ways n a b := by
  72. induction n generalizing a b with
  73. | zero => cases h : legal a b 0 <;> simp [boards, legalFrom, ways, List.countP_cons, h]
  74. | succ n ih =>
  75. rw [boards, countBlocks, ways]
  76. apply addOver_congr
  77. intro c
  78. rw [List.countP_map]
  79. cases h : legal a b c
  80. · simp [Function.comp_def, legalFrom, h]
  81. · simpa [Function.comp_def, legalFrom, h] using ih b c
  82. -- Coefficients of the monic annihilator in ascending powers of the row transfer.
  83. def coeffs : List Int :=
  84. [32, 240, 232, 548, 756, 1976, -2288, 1838, -21442, -7780, -34788, -37932,
  85. -33078, -2154, 704, 294736, 24482, 530604, 135039, 420016, 517484, -869054, 2130413, -3674191,
  86. 5400079, -4188764, 789199, -410228, -11654682, -6465553, -9457004, -31000112, 10981024, -30438653, 45848828, 32672793,
  87. 52675399, 113786869, 9378456, 95000990, 10227231, -139556805, -28179420, -236898345, -138209710, -33518032, -110748382, 72607087,
  88. -81166026, 224789646, -73293787, -161306712, 174314283, -680157267, -188212023, 421179302, -400618103, 573024311, 681294406, -41083456,
  89. 851423783, 1031100925, -206904338, 357910222, 704633482, -818533349, 41594025, -343853834, -1458976208, 157134954, -2084156057, -1235271349,
  90. 605520439, -3599495882, 1541821801, 2749070443, -3018495652, 4214401193, 3890099285, -2151246774, 4272955126, 2499557801, -3487372144, 1055469139,
  91. -70589995, -4803477499, -1036228023, -1227060879, -2483734699, 1347145948, 46131585, 657989217, 3514882780, -225592091, -275980305, 2082571735,
  92. -1812120002, -2453956521, 433342430, -1113683755, -1973221778, 469952819, 726919424, -287487967, 829429328, 1347901384, 463642457, 493792223,
  93. 610623632, 63241894, -243744562, -204629493, -274313160, -373611443, -255746296, -67919007, -48446304, -7854085, 132832159, 93777683,
  94. 19464721, 85979612, 48054937, -52806406, -9468506, 9580631, -51154820, -27963553, 19015130, -5100294, -5414056, 23741580,
  95. 13917716, 2008873, 10102441, 2733616, -5256246, -985077, -3633780, -6257792, -965053, -253569, -2131392, 824141,
  96. 1854736, -94671, 267337, 1024371, -64349, -372261, 155472, -84585, -282058, 14797, 41565, -60750,
  97. 28982, 51446, -7291, 2440, 12947, -6434, -5518, 1574, -1306, -1408, 786, 286,
  98. -141, 131, 67, -47, -5, 4, -4, -1, 1]
  99. /- All 128 × 128 boundary pairs are retained. Each tabulated vector has
  100. O(1) indexed access. Its integer entries allow signed recurrence coefficients. -/
  101. abbrev Table (α : Type) := Vector (Vector α 128) 128
  102. abbrev V := Table Int
  103. def tab (f : Row → Row → α) : Table α :=
  104. Vector.ofFn fun a => Vector.ofFn fun b => f a b
  105. def entry (u : Table α) (a b : Row) : α := (u[a.val])[b.val]
  106. @[simp] theorem entry_tab (f : Row → Row → α) (a b : Row) :
  107. entry (tab f) a b = f a b := by simp [entry, tab]
  108. @[simp] theorem entry_tab_fun (f : Row → Row → α) (a : Row) :
  109. entry (tab f) a = f a := funext (entry_tab f a)
  110. private theorem table_ext {u v : Table α} (h : ∀ a b, entry u a b = entry v a b) :
  111. u = v := by
  112. apply Vector.ext
  113. intro i hi
  114. apply Vector.ext
  115. intro j hj
  116. exact h ⟨i,hi⟩ ⟨j,hj⟩
  117. def edges : Table (List Row) := tab fun a b => rows.filter (legal a b)
  118. def base : V := tab fun a b => if legal a b 0 then 1 else 0
  119. def zeroV : V := tab fun _ _ => 0
  120. def combine (c : Int) (u v : V) : V := tab fun a b => c * entry u a b + entry v a b
  121. def step (v : V) : V := tab fun a b => ((entry edges a b).map (entry v b)).sum
  122. def iter : Nat → V → V
  123. | 0,v => v
  124. | n+1,v => step (iter n v)
  125. private theorem filter_sum_cast (xs : List Row) (p : Row → Bool) (f : Row → Nat) :
  126. ((addOver xs (fun c => if p c then f c else 0) : Nat) : Int) =
  127. ((xs.filter p).map (fun c => (f c : Int))).sum := by
  128. induction xs with
  129. | nil => simp [addOver]
  130. | cons x xs ih =>
  131. cases hx : p x <;> simp [addOver, hx, List.filter_cons] at *
  132. all_goals exact ih
  133. private theorem iter_eq_ways (n : Nat) (a b : Row) :
  134. entry (iter n base) a b = (ways n a b : Int) := by
  135. induction n generalizing a b with
  136. | zero => cases h : legal a b 0 <;> simp [iter, base, ways, h]
  137. | succ n ih =>
  138. simp only [iter, step, entry_tab, ways, edges]
  139. rw [filter_sum_cast]
  140. congr 1
  141. apply List.map_congr_left
  142. intro c _
  143. exact ih b c
  144. private theorem sum_distrib (xs : List Row) (c : Int) (f g : Row → Int) :
  145. (xs.map (fun i => c * f i + g i)).sum =
  146. c * (xs.map f).sum + (xs.map g).sum := by
  147. induction xs with
  148. | nil => simp
  149. | cons x xs ih =>
  150. simp only [List.map_cons, List.sum_cons, ih, Int.mul_add]
  151. omega
  152. private theorem step_combine (c : Int) (u v : V) :
  153. step (combine c u v) = combine c (step u) (step v) := by
  154. apply table_ext
  155. intro a b
  156. simp [combine, step, entry_tab, sum_distrib, Function.comp_def]
  157. private theorem step_zero : step zeroV = zeroV := by
  158. have hz (xs : List Row) : (xs.map (fun _ => (0 : Int))).sum = 0 := by
  159. induction xs with
  160. | nil => rfl
  161. | cons x xs ih => simp [ih]
  162. apply table_ext
  163. intro a b
  164. simp [step, zeroV, Function.comp_def, hz]
  165. def weighted : List Int → V → V
  166. | [],_ => zeroV
  167. | c::cs,v => combine c v (weighted cs (step v))
  168. private theorem weighted_comm (cs : List Int) (v : V) :
  169. weighted cs (step v) = step (weighted cs v) := by
  170. induction cs generalizing v with
  171. | nil => exact step_zero.symm
  172. | cons c cs ih => simp only [weighted, step_combine, ih]
  173. private theorem shift_weighted (cs : List Int) (n : Nat) (v : V) :
  174. weighted cs (iter n v) = iter n (weighted cs v) := by
  175. induction n with
  176. | zero => rfl
  177. | succ n ih => simp only [iter, weighted_comm, ih]
  178. private theorem iter_zero (n : Nat) : iter n zeroV = zeroV := by
  179. induction n with
  180. | zero => rfl
  181. | succ n ih => simp [iter, ih, step_zero]
  182. def dotSeq : List Int → (Nat → Int) → Nat → Int
  183. | [],_,_ => 0
  184. | c::cs,f,n => c*f n + dotSeq cs f (n+1)
  185. private theorem read_weighted (cs : List Int) (v : V) (n : Nat) (a b : Row) :
  186. entry (weighted cs (iter n v)) a b =
  187. dotSeq cs (fun k => entry (iter k v) a b) n := by
  188. induction cs generalizing n with
  189. | nil => simp [weighted, zeroV, dotSeq]
  190. | cons c cs ih =>
  191. simp only [weighted, combine, entry_tab, dotSeq]
  192. exact congrArg (fun z => c * entry (iter n v) a b + z) (ih (n+1))
  193. /-- The finite certificate is an exact equality of 16,384 integer entries. -/
  194. theorem finiteIdentity : weighted coeffs base = zeroV := by native_decide
  195. /-- The ascending monic 176th-order polynomial annihilates the literal board
  196. count at every shift, starting with the empty board. -/
  197. theorem recurrence (n : Nat) : dotSeq coeffs (fun k => (count k : Int)) n = 0 := by
  198. have h (k : Nat) : (count k : Int) = entry (iter k base) 0 0 := by
  199. rw [iter_eq_ways, ← literalToWays]
  200. rfl
  201. rw [show (fun k => (count k : Int)) = fun k => entry (iter k base) 0 0 from funext h]
  202. rw [← read_weighted, shift_weighted, finiteIdentity, iter_zero]
  203. rfl
  204. theorem polynomialShape : coeffs.length = 177 ∧ coeffs.head? = some 32 ∧
  205. coeffs.getLast? = some 1 := by decide
  206. def numerator : List Int :=
  207. [1, 0, -4, 6, 5, -59, 3, 134, -270, -129, 1199, -169,
  208. -1431, 4436, 983, -11046, 3967, 5817, -37605, -829, 53574, -43508, -1889, 199076,
  209. -20472, -154468, 274415, -43873, -774682, 27615, 257247, -1198894, -170803, 1888175, 150920, 449372,
  210. 3855153, 1507529, -234250, 1854778, -3670527, -6493853, -35708, -10422294, -13667324, 9968058, 5562042, -13884332,
  211. 23167116, 35103270, -15610930, -447592, 33576392, -37909083, -60853417, -4614898, -45334789, -63503752, 37065843, 57486058,
  212. 54441627, 170512766, 227657629, 105684659, 42711427, 89752910, -158246538, -507406151, -298181323, -264548841, -887115309, -305862715,
  213. 524394705, -359861330, -122470846, 1669107216, 791374888, -498849948, 1590492746, 1088679744, -1678106521, -85461549, 59931556, -2140697309,
  214. -440832938, -30019275, -315218880, 1854892608, 449223010, 823125885, 3213227336, -745971157, -1279976801, 1967700834, -2235300930, -3146168822,
  215. 1343540621, -2791826365, -2328742516, 2326986441, -1453894194, -308278460, 2400855174, 47980913, 1340237658, 1179498246, -100778937, 884030923,
  216. 61812646, -341352263, 354505675, -666187050, -577396804, 163743247, -855032624, -610005538, 7198417, -398859074, -114742799, 248609339,
  217. -182747238, 283398981, 462844550, 43346387, 103476181, 99412393, -150378717, 80895514, 18786265, 64003491, 68651413, 88261781,
  218. 135284777, 34954748, 55100990, -28221291, -88917704, -33160851, -75467107, -51658602, -8247800, -36176245, 32125026, -2016842,
  219. 27282233, 8331294, 8381530, 5730720, 3328409, -3625419, 4789535, -6082777, 3283983, -2252806, 509198, -438266,
  220. -583152, -63565, -488436, 27058, -245156, 30858, 8498, 50564, 34422, 36264, 5332, 20414,
  221. -2418, 2056, -2216, -788, -548, -232, -240, -32]
  222. #print axioms recurrence
  223. #print axioms polynomialShape
  224. /-- The literal counts for heights n, n+1, ..., n+m-1. -/
  225. def literalPrefix : Nat → Nat → List Int
  226. | 0,_ => []
  227. | m+1,n => (count n : Int) :: literalPrefix m (n+1)
  228. private def fastPrefix : Nat → V → List Int
  229. | 0,_ => []
  230. | m+1,v => entry v 0 0 :: fastPrefix m (step v)
  231. private theorem prefix_eq (m n : Nat) :
  232. fastPrefix m (iter n base) = literalPrefix m n := by
  233. induction m generalizing n with
  234. | zero => rfl
  235. | succ m ih =>
  236. simp only [fastPrefix, literalPrefix, iter_eq_ways]
  237. rw [← literalToWays]
  238. exact congrArg (List.cons (count n : Int)) (ih (n+1))
  239. /-- Initial coefficients of the convolution by Q, in ascending power order. -/
  240. def multiplyPrefix (xs : List Int) : List Int :=
  241. let q := coeffs.reverse
  242. (List.range xs.length).map (fun n =>
  243. ((List.range (n+1)).map (fun k => q[k]! * xs[n-k]!)).sum)
  244. /-- Every coefficient up to degree 175 of Q times the literal count series
  245. is certified. The recurrence supplies the vanishing coefficients above 175. -/
  246. theorem initialNumerator : multiplyPrefix (literalPrefix 176 0) = numerator := by
  247. rw [← prefix_eq]
  248. native_decide
  249. #print axioms initialNumerator
  250. def bezoutP : List Int :=
  251. [-14322, 16181, 29492, -24340, 9754, 28177, -15467, 10004, -17022, 14432, 4537, 7567,
  252. -15515, 22516, 10998, -10184, 28050, 9159, 20596, 18441, 29739, 10440, -12522, -31761,
  253. 22793, -29288, 25183, 23073, -17278, -5645, 27274, 10236, -18851, 28719, -17349, 24605,
  254. 19284, 6468, -28718, 23038, -17344, -10924, 18014, 14163, -1914, -27389, 6779, -6175,
  255. -2643, -28171, 9967, -8634, 19290, 31200, 8542, 11619, -2976, -29833, -31028, 9532,
  256. -4268, -19112, 16908, 1052, 3206, -14013, -3246, 17276, -2963, -3339, 15727, -18049,
  257. -7886, 6863, -1743, 14393, -1115, 3470, 8405, -18395, -29075, -24194, 5316, -5169,
  258. 8606, -3308, -10212, -16139, 30219, -11670, -7937, -6904, -24385, 15105, -7245, -20515,
  259. -27989, 2481, -10769, 3582, 16998, 10785, -22456, 27465, 7501, -30054, -24923, -1250,
  260. -19304, 28388, -16685, 32133, 6137, -29648, -4122, 7194, 31539, -2472, -5107, -3539,
  261. 15955, -1728, -19558, 15892, 24248, -25612, -24976, 3183, 23910, -2579, 11714, 7935,
  262. 18695, -1651, 32425, -30696, 6442, 7389, -25663, -11115, 26011, -29830, -28871, -13635,
  263. -5448, 19348, -23889, -23466, 14657, 31758, 27085, -30493, -32023, 25728, 15328, 12320,
  264. -5539, -6601, 13290, 13013, 16466, 4695, 19128, -15398, 4219, 7454, -18928, -12869,
  265. 24044, -19045, -11110, 7557, 10685, -5293, 21187, 17357]
  266. def bezoutQ : List Int :=
  267. [14323, -1858, -31346, 13405, -15715, -8086, -15471, -21716, -10636, -17523, -8893, 16013,
  268. -16952, 25784, 12274, 31956, 8118, 8849, -31331, 1840, -27016, 5907, 27701, 4048,
  269. 2271, 11524, 20668, -9525, 6902, -9345, 25616, -20828, 25971, -28609, -20159, 14305,
  270. -18384, -10026, -15410, 11353, -31660, 12518, 25769, 4931, -16105, -14890, -25153, -6882,
  271. -23460, 28368, -10930, -19576, -29539, -30569, 28558, 28905, 30482, -30729, -583, 14746,
  272. -23637, -31085, -29712, 24320, -13360, 11828, -833, -32109, 18457, 30826, -32704, -9435,
  273. 13275, 16399, 1524, -16368, 31227, 11054, -16164, -232, 14594, -22285, 687, 8499,
  274. -5272, 19956, 11298, -3744, 12895, -10731, -32201, -5352, 17665, 1177, -6768, 5841,
  275. 15917, -22283, -14400, 14308, -7235, 1027, 26345, 28760, -16125, 11278, -29905, -2869,
  276. -15261, 11637, 20866, -11964, 24290, -3956, -339, 5358, 21377, -23701, -25511, 14863,
  277. -23613, 12595, -23075, -4460, -26184, -32447, 3382, -24857, 10584, 2242, 14896, -14406,
  278. 3153, 27635, 916, 11653, 3927, -16574, 20640, 17623, -19468, -1741, 6164, 16167,
  279. -1885, 26168, 12116, -3843, -1308, -14868, 2520, 17752, 8066, -6284, -31633, 12527,
  280. -5762, 14322, 16920, -28404, 25632, -1898, -529, 6078, 11158, -14144, -32289, -13435,
  281. -24338, 10077, 24914, 10685, -5293, 21187, 17357]
  282. def productCoeff (f g : Array Int) (n : Nat) : Int :=
  283. ((List.range (n+1)).map (fun i => (f.getD i 0) * (g.getD (n-i) 0))).sum
  284. /-- Coefficient-by-coefficient Bezout certificate U·P + V·Q = 1 mod 65521.
  285. The four lengths ensure the maximum product degree is 350. -/
  286. theorem bezoutCertificate :
  287. numerator.length = 176 ∧ coeffs.length = 177 ∧
  288. bezoutP.length = 176 ∧ bezoutQ.length = 175 ∧
  289. (List.range 351).map (fun n =>
  290. (productCoeff bezoutP.toArray numerator.toArray n +
  291. productCoeff bezoutQ.toArray coeffs.reverse.toArray n) % 65521) =
  292. 1 :: List.replicate 350 0 := by native_decide
  293. #print axioms bezoutCertificate
  294. end SevenColumns