Lean / Reader
Lean reader

A202883.lean

253 lines40 declarationsno sorryimports Std
  1. import Std
  2. /-!
  3. # The five-term recurrence for OEIS A202883
  4. Run `lean A202883.lean` with Lean 4.15.0. No mathlib dependency.
  5. `a n` filters the actual list of boards by the local neighbour condition.
  6. The main results are `recurrence`, `initial`, and `generating_function_coefficients`.
  7. -/
  8. namespace A202883
  9. /-- The two digits are the entries in the left and right cells, respectively. -/
  10. inductive Row where
  11. | r00 | r01 | r02 | r10 | r11 | r12 | r20 | r21 | r22
  12. deriving DecidableEq, Repr
  13. open Row
  14. def rows : List Row := [r00,r01,r02,r10,r11,r12,r20,r21,r22]
  15. theorem rows_complete (r : Row) : r ∈ rows := by cases r <;> decide
  16. theorem rows_nodup : rows.Nodup := by decide
  17. def left : Row → Nat
  18. | r00 | r01 | r02 => 0
  19. | r10 | r11 | r12 => 1
  20. | r20 | r21 | r22 => 2
  21. def right : Row → Nat
  22. | r00 | r10 | r20 => 0
  23. | r01 | r11 | r21 => 1
  24. | r02 | r12 | r22 => 2
  25. def supported (cell across up down : Nat) : Bool :=
  26. cell == 0 || decide (cell ≤ across) || decide (cell ≤ up) || decide (cell ≤ down)
  27. def rowOK (up here down : Row) : Bool :=
  28. supported (left here) (right here) (left up) (left down) &&
  29. supported (right here) (left here) (right up) (right down)
  30. /-- A missing neighbour has value zero, which can never support a positive cell. -/
  31. def legalFrom (up here : Row) : List Row → Bool
  32. | [] => rowOK up here r00
  33. | r :: rs => rowOK up here r && legalFrom here r rs
  34. def boards : Nat → List (List Row)
  35. | 0 => [[]]
  36. | n+1 => rows.flatMap fun r => (boards n).map (r :: ·)
  37. theorem mem_boards (n : Nat) (b : List Row) : b ∈ boards n ↔ b.length = n := by
  38. induction n generalizing b with
  39. | zero => cases b <;> simp [boards]
  40. | succ n ih =>
  41. cases b with
  42. | nil => simp [boards, List.mem_flatMap, List.mem_map]
  43. | cons r rs =>
  44. simp [boards, List.mem_flatMap, List.mem_map, rows_complete, ih]
  45. theorem boards_nodup (n : Nat) : (boards n).Nodup := by
  46. induction n with
  47. | zero => decide
  48. | succ n ih =>
  49. apply List.pairwise_flatMap.mpr
  50. constructor
  51. · intro r _
  52. exact ih.map (r :: ·) (by intro x y h eq; exact h (List.cons.inj eq).2)
  53. · apply rows_nodup.imp
  54. intro r s hrs x hx y hy eq
  55. obtain ⟨x', _, rfl⟩ := List.mem_map.mp hx
  56. obtain ⟨y', _, rfl⟩ := List.mem_map.mp hy
  57. exact hrs (List.cons.inj eq).1
  58. /-- The extra top row 00 is always legal and supplies no positive support. -/
  59. def a (n : Nat) : Nat := ((boards n).filter (legalFrom r00 r00)).length
  60. private def total {α : Type} (xs : List α) (f : α → Nat) : Nat :=
  61. match xs with
  62. | [] => 0
  63. | x :: tail => f x + total tail f
  64. private theorem total_append {α : Type} (xs ys : List α) (f : α → Nat) :
  65. total (xs ++ ys) f = total xs f + total ys f := by
  66. induction xs with
  67. | nil => simp [total]
  68. | cons x xs ih => simp [total, ih, Nat.add_assoc]
  69. private theorem total_map {α β : Type} (xs : List α) (g : α → β) (f : β → Nat) :
  70. total (xs.map g) f = total xs (fun x => f (g x)) := by
  71. induction xs with
  72. | nil => rfl
  73. | cons x xs ih => simp [total, ih]
  74. private theorem total_flatMap {α β : Type} (xs : List α) (g : α → List β) (f : β → Nat) :
  75. total (xs.flatMap g) f = total xs (fun x => total (g x) f) := by
  76. induction xs with
  77. | nil => rfl
  78. | cons x xs ih => simp [total, total_append, ih]
  79. private theorem total_zero {α : Type} (xs : List α) : total xs (fun _ => 0) = 0 := by
  80. induction xs with
  81. | nil => rfl
  82. | cons x xs ih => simp [total, ih]
  83. private theorem total_indicator {α : Type} (xs : List α) (p : α → Bool) :
  84. (xs.filter p).length = total xs (fun x => if p x then 1 else 0) := by
  85. induction xs with
  86. | nil => rfl
  87. | cons x xs ih => cases h : p x <;> simp [List.filter, total, h, ih, Nat.add_comm]
  88. abbrev Values := Row → Row → Nat
  89. def advance (f : Values) (up here : Row) : Nat :=
  90. total rows fun r => if rowOK up here r then f here r else 0
  91. def suffix : Nat → Values
  92. | 0 => fun up here => if rowOK up here r00 then 1 else 0
  93. | n+1 => advance (suffix n)
  94. private theorem enumeration (n : Nat) (up here : Row) :
  95. total (boards n) (fun b => if legalFrom up here b then 1 else 0) = suffix n up here := by
  96. induction n generalizing up here with
  97. | zero => simp [boards, total, legalFrom, suffix]
  98. | succ n ih =>
  99. simp only [boards, total_flatMap, total_map, suffix, advance]
  100. apply congrArg (total rows)
  101. funext r
  102. cases h : rowOK up here r <;> simp [legalFrom, h, total_zero, ih]
  103. theorem a_eq_suffix (n : Nat) : a n = suffix n r00 r00 :=
  104. (total_indicator _ _).trans (enumeration n r00 r00)
  105. /-- Five consecutive steps can be certified by a small table. The columns are
  106. 00, 11, 22, then the settled and pending forms of 01, 02, 12, in that order.
  107. None of the entries is assumed: the next three lemmas check each local identity. -/
  108. private def column (up here : Row) : Nat :=
  109. match here with
  110. | r00 => 0 | r11 => 1 | r22 => 2
  111. | r01 => if 1 ≤ right up then 3 else 4
  112. | r10 => if 1 ≤ left up then 3 else 4
  113. | r02 => if 2 ≤ right up then 5 else 6
  114. | r20 => if 2 ≤ left up then 5 else 6
  115. | r12 => if 2 ≤ right up then 7 else 8
  116. | r21 => if 2 ≤ left up then 7 else 8
  117. private def certificate (k : Nat) : List Nat :=
  118. match k with
  119. | 0 => [1,1,1,1,0,1,0,1,0]
  120. | 1 => [3,5,9,4,3,6,3,7,3]
  121. | 2 => [35,37,51,36,27,43,22,44,22]
  122. | 3 => [265,283,369,274,190,317,138,326,138]
  123. | 4 => [1849,2017,2751,1933,1340,2300,1012,2384,1012]
  124. | 5 => [13345,14531,19851,13938,9737,16598,7435,17191,7435]
  125. | _ => []
  126. private def sample (k : Nat) (up here : Row) : Nat :=
  127. (certificate k)[column up here]?.getD 0
  128. private theorem sample_zero : suffix 0 = sample 0 := by
  129. funext u r
  130. cases u <;> cases r <;> rfl
  131. set_option maxHeartbeats 3000000 in
  132. private theorem sample_step (k : Nat) (hk : k < 5) : advance (sample k) = sample (k+1) := by
  133. funext u r
  134. have h : k = 0 ∨ k = 1 ∨ k = 2 ∨ k = 3 ∨ k = 4 := by omega
  135. rcases h with h | h | h | h | h <;> subst k <;> cases u <;> cases r <;> rfl
  136. private theorem small (k : Nat) (hk : k ≤ 5) : suffix k = sample k := by
  137. induction k with
  138. | zero => exact sample_zero
  139. | succ k ih => exact (congrArg advance (ih (by omega))).trans (sample_step k (by omega))
  140. private def combine (a b c d e : Nat) := 6*a + 2*b + 46*c + 25*d + 36*e
  141. private theorem sample_identity (u r : Row) :
  142. sample 5 u r = combine (sample 4 u r) (sample 3 u r) (sample 2 u r)
  143. (sample 1 u r) (sample 0 u r) := by
  144. cases u <;> cases r <;> rfl
  145. private theorem total_linear {α : Type} (xs : List α) (a b c d e : α → Nat) :
  146. total xs (fun x => combine (a x) (b x) (c x) (d x) (e x)) =
  147. combine (total xs a) (total xs b) (total xs c) (total xs d) (total xs e) := by
  148. induction xs with
  149. | nil => rfl
  150. | cons x xs ih =>
  151. simp only [total]
  152. rw [ih]
  153. unfold combine
  154. omega
  155. private theorem advance_linear (a b c d e : Values) (u r : Row) :
  156. advance (fun i j => combine (a i j) (b i j) (c i j) (d i j) (e i j)) u r =
  157. combine (advance a u r) (advance b u r) (advance c u r) (advance d u r) (advance e u r) := by
  158. unfold advance
  159. rw [← total_linear]
  160. apply congrArg (total rows)
  161. funext s
  162. cases h : rowOK u r s <;> simp [h, combine]
  163. private theorem suffix_recurrence (n : Nat) (u r : Row) :
  164. suffix (n+5) u r = combine (suffix (n+4) u r) (suffix (n+3) u r)
  165. (suffix (n+2) u r) (suffix (n+1) u r) (suffix n u r) := by
  166. induction n generalizing u r with
  167. | zero =>
  168. simpa only [small 5 (by decide), small 4 (by decide), small 3 (by decide),
  169. small 2 (by decide), small 1 (by decide), small 0 (by decide),
  170. Nat.zero_add] using sample_identity u r
  171. | succ n ih =>
  172. have eq : suffix (n+5) = fun i j => combine (suffix (n+4) i j) (suffix (n+3) i j)
  173. (suffix (n+2) i j) (suffix (n+1) i j) (suffix n i j) := by
  174. funext i j; exact ih i j
  175. exact (congrArg (fun f => advance f u r) eq).trans
  176. (advance_linear (suffix (n+4)) (suffix (n+3)) (suffix (n+2))
  177. (suffix (n+1)) (suffix n) u r)
  178. /-- The main theorem, quantified over all heights starting at five. -/
  179. theorem recurrence (n : Nat) :
  180. a (n+5) = 6*a (n+4) + 2*a (n+3) + 46*a (n+2) + 25*a (n+1) + 36*a n := by
  181. simpa only [a_eq_suffix, combine] using suffix_recurrence n r00 r00
  182. theorem initial : (a 0, a 1, a 2, a 3, a 4) = (1, 3, 35, 265, 1849) := by
  183. simp only [a_eq_suffix, small 0 (by decide), small 1 (by decide), small 2 (by decide),
  184. small 3 (by decide), small 4 (by decide)]
  185. rfl
  186. /-- Coefficient of x^n in x^d times B(x), where B(x) starts with a(1). -/
  187. def shifted : Nat → Nat → Nat
  188. | 0, n => a (n+1)
  189. | _+1, 0 => 0
  190. | d+1, n+1 => shifted d n
  191. def numeratorCoeff : Nat → Nat
  192. | 0 => 3 | 1 => 17 | 2 => 49 | 3 => 51 | 4 => 36 | _ => 0
  193. /-- Q(x)B(x)=P(x) coefficient by coefficient, with negative terms moved to the right.
  194. Here B(x)=a(1)+a(2)x+..., P=3+17x+49x²+51x³+36x⁴, and
  195. Q=1-6x-2x²-46x³-25x⁴-36x⁵. -/
  196. theorem generating_function_coefficients (n : Nat) :
  197. a (n+1) = 6*shifted 1 n + 2*shifted 2 n + 46*shifted 3 n +
  198. 25*shifted 4 n + 36*shifted 5 n + numeratorCoeff n := by
  199. match n with
  200. | 0 | 1 | 2 | 3 | 4 =>
  201. simp only [shifted, numeratorCoeff, a_eq_suffix, Nat.reduceAdd,
  202. small 0 (by decide), small 1 (by decide), small 2 (by decide),
  203. small 3 (by decide), small 4 (by decide), small 5 (by decide)] <;> rfl
  204. | n+5 =>
  205. simp only [shifted, numeratorCoeff, Nat.add_zero]
  206. exact recurrence (n+1)
  207. #print axioms recurrence
  208. #print axioms initial
  209. #print axioms generating_function_coefficients
  210. end A202883