import Stdimport Init.Data.Vector.Lemmasset_option maxRecDepth 20000set_option maxHeartbeats 2000000/-! OEIS A296553. Row masks are read from the low bit to the high bit.The allowed diagonals are northeast/southwest. Missing neighbours are zero.-/namespace SevenColumnsabbrev Row := Fin 128def rows : List Row := List.finRange 128def bit (r : Row) (j : Nat) : Nat := if j < 7 then (r.val / 2^j) % 2 else 0def left (r : Row) (j : Nat) : Nat := if j=0 then 0 else bit r (j-1)def neighbours (a b c : Row) (j : Nat) : Nat :=left b j + bit b (j+1) + bit a j + bit a (j+1) + bit c j + left c jdef legal (a b c : Row) : Bool :=(List.range 7).all fun j =>bit b j == 0 || neighbours a b c j == 3 || neighbours a b c j == 6/-- Test the centres of all consecutive three-row windows and the lower boundary. -/def legalFrom (a b : Row) : List Row → Bool| [] => legal a b 0| c::cs => legal a b c && legalFrom b c cs/-- Enumerate every height-n board, regardless of legality. -/def boards : Nat → List (List Row)| 0 => [[]]| n+1 => rows.flatMap fun r => (boards n).map (r :: ·)private theorem rows_complete : ∀ r : Row, r ∈ rows := by decideprivate theorem rows_distinct : rows.Nodup := by decidetheorem boards_complete (n : Nat) (rs : List Row) : rs ∈ boards n ↔ rs.length = n := byinduction n generalizing rs with| zero => cases rs <;> simp [boards]| succ n ih =>cases rs with| nil => simp [boards]| cons r rs =>simp only [boards,List.mem_flatMap,List.mem_map,List.cons.injEq,List.length_cons,Nat.succ.injEq]constructor· rintro ⟨_,_,_,hm,_,rfl⟩exact (ih _).1 hm· intro hexact ⟨r,rows_complete r,rs,(ih _).2 h,rfl,rfl⟩theorem boards_distinct (n : Nat) : (boards n).Nodup := byinduction n with| zero => simp [boards]| succ n ih =>apply List.pairwise_flatMap.2constructor· intro r _apply List.pairwise_map.2exact ih.imp (by intro a b h e; exact h (List.cons.inj e).2)· apply rows_distinct.impintro a b hab x hx y hy hxyobtain ⟨_,_,rfl⟩ := List.mem_map.1 hxobtain ⟨_,_,rfl⟩ := List.mem_map.1 hyexact hab (List.cons.inj hxy).1/-- Literal count by testing each binary board. The leading zero row satisfiesthe local rule vacuously and supplies the upper boundary. -/def count (n : Nat) : Nat := (boards n).countP (legalFrom 0 0)def addOver (xs : List α) (f : α → Nat) : Nat := (xs.map f).sumdef ways : Nat → Row → Row → Nat| 0, a, b => if legal a b 0 then 1 else 0| n+1, a, b => addOver rows (fun c => if legal a b c then ways n b c else 0)private theorem countBlocks (as : List α) (chunks : α → List β) (ok : β → Bool) :(as.flatMap chunks).countP ok = addOver as (fun a => (chunks a).countP ok) := byinduction as with| nil => simp [addOver]| cons a as ih => simp [addOver, List.flatMap_cons, ih, Nat.add_comm]private theorem addOver_congr (as : List α) (f g : α → Nat) (h : ∀ a, f a = g a) :addOver as f = addOver as g := by rw [funext h]theorem literalToWays (n : Nat) (a b : Row) :(boards n).countP (legalFrom a b) = ways n a b := byinduction n generalizing a b with| zero => cases h : legal a b 0 <;> simp [boards, legalFrom, ways, List.countP_cons, h]| succ n ih =>rw [boards, countBlocks, ways]apply addOver_congrintro crw [List.countP_map]cases h : legal a b c· simp [Function.comp_def, legalFrom, h]· simpa [Function.comp_def, legalFrom, h] using ih b c-- Coefficients of the monic annihilator in ascending powers of the row transfer.def coeffs : List Int :=[32, 240, 232, 548, 756, 1976, -2288, 1838, -21442, -7780, -34788, -37932,-33078, -2154, 704, 294736, 24482, 530604, 135039, 420016, 517484, -869054, 2130413, -3674191,5400079, -4188764, 789199, -410228, -11654682, -6465553, -9457004, -31000112, 10981024, -30438653, 45848828, 32672793,52675399, 113786869, 9378456, 95000990, 10227231, -139556805, -28179420, -236898345, -138209710, -33518032, -110748382, 72607087,-81166026, 224789646, -73293787, -161306712, 174314283, -680157267, -188212023, 421179302, -400618103, 573024311, 681294406, -41083456,851423783, 1031100925, -206904338, 357910222, 704633482, -818533349, 41594025, -343853834, -1458976208, 157134954, -2084156057, -1235271349,605520439, -3599495882, 1541821801, 2749070443, -3018495652, 4214401193, 3890099285, -2151246774, 4272955126, 2499557801, -3487372144, 1055469139,-70589995, -4803477499, -1036228023, -1227060879, -2483734699, 1347145948, 46131585, 657989217, 3514882780, -225592091, -275980305, 2082571735,-1812120002, -2453956521, 433342430, -1113683755, -1973221778, 469952819, 726919424, -287487967, 829429328, 1347901384, 463642457, 493792223,610623632, 63241894, -243744562, -204629493, -274313160, -373611443, -255746296, -67919007, -48446304, -7854085, 132832159, 93777683,19464721, 85979612, 48054937, -52806406, -9468506, 9580631, -51154820, -27963553, 19015130, -5100294, -5414056, 23741580,13917716, 2008873, 10102441, 2733616, -5256246, -985077, -3633780, -6257792, -965053, -253569, -2131392, 824141,1854736, -94671, 267337, 1024371, -64349, -372261, 155472, -84585, -282058, 14797, 41565, -60750,28982, 51446, -7291, 2440, 12947, -6434, -5518, 1574, -1306, -1408, 786, 286,-141, 131, 67, -47, -5, 4, -4, -1, 1]/- All 128 × 128 boundary pairs are retained. Each tabulated vector hasO(1) indexed access. Its integer entries allow signed recurrence coefficients. -/abbrev Table (α : Type) := Vector (Vector α 128) 128abbrev V := Table Intdef tab (f : Row → Row → α) : Table α :=Vector.ofFn fun a => Vector.ofFn fun b => f a bdef entry (u : Table α) (a b : Row) : α := (u[a.val])[b.val]@[simp] theorem entry_tab (f : Row → Row → α) (a b : Row) :entry (tab f) a b = f a b := by simp [entry, tab]@[simp] theorem entry_tab_fun (f : Row → Row → α) (a : Row) :entry (tab f) a = f a := funext (entry_tab f a)private theorem table_ext {u v : Table α} (h : ∀ a b, entry u a b = entry v a b) :u = v := byapply Vector.extintro i hiapply Vector.extintro j hjexact h ⟨i,hi⟩ ⟨j,hj⟩def edges : Table (List Row) := tab fun a b => rows.filter (legal a b)def base : V := tab fun a b => if legal a b 0 then 1 else 0def zeroV : V := tab fun _ _ => 0def combine (c : Int) (u v : V) : V := tab fun a b => c * entry u a b + entry v a bdef step (v : V) : V := tab fun a b => ((entry edges a b).map (entry v b)).sumdef iter : Nat → V → V| 0,v => v| n+1,v => step (iter n v)private theorem filter_sum_cast (xs : List Row) (p : Row → Bool) (f : Row → Nat) :((addOver xs (fun c => if p c then f c else 0) : Nat) : Int) =((xs.filter p).map (fun c => (f c : Int))).sum := byinduction xs with| nil => simp [addOver]| cons x xs ih =>cases hx : p x <;> simp [addOver, hx, List.filter_cons] at *all_goals exact ihprivate theorem iter_eq_ways (n : Nat) (a b : Row) :entry (iter n base) a b = (ways n a b : Int) := byinduction n generalizing a b with| zero => cases h : legal a b 0 <;> simp [iter, base, ways, h]| succ n ih =>simp only [iter, step, entry_tab, ways, edges]rw [filter_sum_cast]congr 1apply List.map_congr_leftintro c _exact ih b cprivate theorem sum_distrib (xs : List Row) (c : Int) (f g : Row → Int) :(xs.map (fun i => c * f i + g i)).sum =c * (xs.map f).sum + (xs.map g).sum := byinduction xs with| nil => simp| cons x xs ih =>simp only [List.map_cons, List.sum_cons, ih, Int.mul_add]omegaprivate theorem step_combine (c : Int) (u v : V) :step (combine c u v) = combine c (step u) (step v) := byapply table_extintro a bsimp [combine, step, entry_tab, sum_distrib, Function.comp_def]private theorem step_zero : step zeroV = zeroV := byhave hz (xs : List Row) : (xs.map (fun _ => (0 : Int))).sum = 0 := byinduction xs with| nil => rfl| cons x xs ih => simp [ih]apply table_extintro a bsimp [step, zeroV, Function.comp_def, hz]def weighted : List Int → V → V| [],_ => zeroV| c::cs,v => combine c v (weighted cs (step v))private theorem weighted_comm (cs : List Int) (v : V) :weighted cs (step v) = step (weighted cs v) := byinduction cs generalizing v with| nil => exact step_zero.symm| cons c cs ih => simp only [weighted, step_combine, ih]private theorem shift_weighted (cs : List Int) (n : Nat) (v : V) :weighted cs (iter n v) = iter n (weighted cs v) := byinduction n with| zero => rfl| succ n ih => simp only [iter, weighted_comm, ih]private theorem iter_zero (n : Nat) : iter n zeroV = zeroV := byinduction n with| zero => rfl| succ n ih => simp [iter, ih, step_zero]def dotSeq : List Int → (Nat → Int) → Nat → Int| [],_,_ => 0| c::cs,f,n => c*f n + dotSeq cs f (n+1)private theorem read_weighted (cs : List Int) (v : V) (n : Nat) (a b : Row) :entry (weighted cs (iter n v)) a b =dotSeq cs (fun k => entry (iter k v) a b) n := byinduction cs generalizing n with| nil => simp [weighted, zeroV, dotSeq]| cons c cs ih =>simp only [weighted, combine, entry_tab, dotSeq]exact congrArg (fun z => c * entry (iter n v) a b + z) (ih (n+1))/-- The finite certificate is an exact equality of 16,384 integer entries. -/theorem finiteIdentity : weighted coeffs base = zeroV := by native_decide/-- The ascending monic 176th-order polynomial annihilates the literal boardcount at every shift, starting with the empty board. -/theorem recurrence (n : Nat) : dotSeq coeffs (fun k => (count k : Int)) n = 0 := byhave h (k : Nat) : (count k : Int) = entry (iter k base) 0 0 := byrw [iter_eq_ways, ← literalToWays]rflrw [show (fun k => (count k : Int)) = fun k => entry (iter k base) 0 0 from funext h]rw [← read_weighted, shift_weighted, finiteIdentity, iter_zero]rfltheorem polynomialShape : coeffs.length = 177 ∧ coeffs.head? = some 32 ∧coeffs.getLast? = some 1 := by decidedef numerator : List Int :=[1, 0, -4, 6, 5, -59, 3, 134, -270, -129, 1199, -169,-1431, 4436, 983, -11046, 3967, 5817, -37605, -829, 53574, -43508, -1889, 199076,-20472, -154468, 274415, -43873, -774682, 27615, 257247, -1198894, -170803, 1888175, 150920, 449372,3855153, 1507529, -234250, 1854778, -3670527, -6493853, -35708, -10422294, -13667324, 9968058, 5562042, -13884332,23167116, 35103270, -15610930, -447592, 33576392, -37909083, -60853417, -4614898, -45334789, -63503752, 37065843, 57486058,54441627, 170512766, 227657629, 105684659, 42711427, 89752910, -158246538, -507406151, -298181323, -264548841, -887115309, -305862715,524394705, -359861330, -122470846, 1669107216, 791374888, -498849948, 1590492746, 1088679744, -1678106521, -85461549, 59931556, -2140697309,-440832938, -30019275, -315218880, 1854892608, 449223010, 823125885, 3213227336, -745971157, -1279976801, 1967700834, -2235300930, -3146168822,1343540621, -2791826365, -2328742516, 2326986441, -1453894194, -308278460, 2400855174, 47980913, 1340237658, 1179498246, -100778937, 884030923,61812646, -341352263, 354505675, -666187050, -577396804, 163743247, -855032624, -610005538, 7198417, -398859074, -114742799, 248609339,-182747238, 283398981, 462844550, 43346387, 103476181, 99412393, -150378717, 80895514, 18786265, 64003491, 68651413, 88261781,135284777, 34954748, 55100990, -28221291, -88917704, -33160851, -75467107, -51658602, -8247800, -36176245, 32125026, -2016842,27282233, 8331294, 8381530, 5730720, 3328409, -3625419, 4789535, -6082777, 3283983, -2252806, 509198, -438266,-583152, -63565, -488436, 27058, -245156, 30858, 8498, 50564, 34422, 36264, 5332, 20414,-2418, 2056, -2216, -788, -548, -232, -240, -32]#print axioms recurrence#print axioms polynomialShape/-- The literal counts for heights n, n+1, ..., n+m-1. -/def literalPrefix : Nat → Nat → List Int| 0,_ => []| m+1,n => (count n : Int) :: literalPrefix m (n+1)private def fastPrefix : Nat → V → List Int| 0,_ => []| m+1,v => entry v 0 0 :: fastPrefix m (step v)private theorem prefix_eq (m n : Nat) :fastPrefix m (iter n base) = literalPrefix m n := byinduction m generalizing n with| zero => rfl| succ m ih =>simp only [fastPrefix, literalPrefix, iter_eq_ways]rw [← literalToWays]exact congrArg (List.cons (count n : Int)) (ih (n+1))/-- Initial coefficients of the convolution by Q, in ascending power order. -/def multiplyPrefix (xs : List Int) : List Int :=let q := coeffs.reverse(List.range xs.length).map (fun n =>((List.range (n+1)).map (fun k => q[k]! * xs[n-k]!)).sum)/-- Every coefficient up to degree 175 of Q times the literal count seriesis certified. The recurrence supplies the vanishing coefficients above 175. -/theorem initialNumerator : multiplyPrefix (literalPrefix 176 0) = numerator := byrw [← prefix_eq]native_decide#print axioms initialNumeratordef bezoutP : List Int :=[-14322, 16181, 29492, -24340, 9754, 28177, -15467, 10004, -17022, 14432, 4537, 7567,-15515, 22516, 10998, -10184, 28050, 9159, 20596, 18441, 29739, 10440, -12522, -31761,22793, -29288, 25183, 23073, -17278, -5645, 27274, 10236, -18851, 28719, -17349, 24605,19284, 6468, -28718, 23038, -17344, -10924, 18014, 14163, -1914, -27389, 6779, -6175,-2643, -28171, 9967, -8634, 19290, 31200, 8542, 11619, -2976, -29833, -31028, 9532,-4268, -19112, 16908, 1052, 3206, -14013, -3246, 17276, -2963, -3339, 15727, -18049,-7886, 6863, -1743, 14393, -1115, 3470, 8405, -18395, -29075, -24194, 5316, -5169,8606, -3308, -10212, -16139, 30219, -11670, -7937, -6904, -24385, 15105, -7245, -20515,-27989, 2481, -10769, 3582, 16998, 10785, -22456, 27465, 7501, -30054, -24923, -1250,-19304, 28388, -16685, 32133, 6137, -29648, -4122, 7194, 31539, -2472, -5107, -3539,15955, -1728, -19558, 15892, 24248, -25612, -24976, 3183, 23910, -2579, 11714, 7935,18695, -1651, 32425, -30696, 6442, 7389, -25663, -11115, 26011, -29830, -28871, -13635,-5448, 19348, -23889, -23466, 14657, 31758, 27085, -30493, -32023, 25728, 15328, 12320,-5539, -6601, 13290, 13013, 16466, 4695, 19128, -15398, 4219, 7454, -18928, -12869,24044, -19045, -11110, 7557, 10685, -5293, 21187, 17357]def bezoutQ : List Int :=[14323, -1858, -31346, 13405, -15715, -8086, -15471, -21716, -10636, -17523, -8893, 16013,-16952, 25784, 12274, 31956, 8118, 8849, -31331, 1840, -27016, 5907, 27701, 4048,2271, 11524, 20668, -9525, 6902, -9345, 25616, -20828, 25971, -28609, -20159, 14305,-18384, -10026, -15410, 11353, -31660, 12518, 25769, 4931, -16105, -14890, -25153, -6882,-23460, 28368, -10930, -19576, -29539, -30569, 28558, 28905, 30482, -30729, -583, 14746,-23637, -31085, -29712, 24320, -13360, 11828, -833, -32109, 18457, 30826, -32704, -9435,13275, 16399, 1524, -16368, 31227, 11054, -16164, -232, 14594, -22285, 687, 8499,-5272, 19956, 11298, -3744, 12895, -10731, -32201, -5352, 17665, 1177, -6768, 5841,15917, -22283, -14400, 14308, -7235, 1027, 26345, 28760, -16125, 11278, -29905, -2869,-15261, 11637, 20866, -11964, 24290, -3956, -339, 5358, 21377, -23701, -25511, 14863,-23613, 12595, -23075, -4460, -26184, -32447, 3382, -24857, 10584, 2242, 14896, -14406,3153, 27635, 916, 11653, 3927, -16574, 20640, 17623, -19468, -1741, 6164, 16167,-1885, 26168, 12116, -3843, -1308, -14868, 2520, 17752, 8066, -6284, -31633, 12527,-5762, 14322, 16920, -28404, 25632, -1898, -529, 6078, 11158, -14144, -32289, -13435,-24338, 10077, 24914, 10685, -5293, 21187, 17357]def productCoeff (f g : Array Int) (n : Nat) : Int :=((List.range (n+1)).map (fun i => (f.getD i 0) * (g.getD (n-i) 0))).sum/-- Coefficient-by-coefficient Bezout certificate U·P + V·Q = 1 mod 65521.The four lengths ensure the maximum product degree is 350. -/theorem bezoutCertificate :numerator.length = 176 ∧ coeffs.length = 177 ∧bezoutP.length = 176 ∧ bezoutQ.length = 175 ∧(List.range 351).map (fun n =>(productCoeff bezoutP.toArray numerator.toArray n +productCoeff bezoutQ.toArray coeffs.reverse.toArray n) % 65521) =1 :: List.replicate 350 0 := by native_decide#print axioms bezoutCertificateend SevenColumns