import Mathlibnamespace RookWord/-- Four possible column offsets followed by a fifth letter for a vacant row. -/abbrev Word (n : ℕ) := Fin n → Fin 5def advance {n : ℕ} (i : Fin n) (k : ℕ) : Fin n :=⟨(i.val + k) % n, Nat.mod_lt _ (Nat.zero_lt_of_lt i.isLt)⟩def Local {n : ℕ} (w : Word n) : Prop :=∀ (i : Fin n) (t : ℕ), 1 ≤ t → t ≤ 3 → (w i).val < 4 →(w (advance i t)).val < 4 → (w i).val ≠ (w (advance i t)).val + tdef Single {n : ℕ} (w : Word n) : Prop := ∃! i, (w i).val = 4noncomputable instance validDecidable {n : ℕ} (w : Word n) :Decidable (Local w ∧ Single w) := Classical.propDecidable _end RookWord-- Definitions in the statement proposed in upstream PR #111761.namespace OMA.S_oeis_a005777def board (n : ℕ) : Finset (Fin n × Fin n) :=Finset.univ.filter fun p => (p.2.val + n - p.1.val) % n < 4def IsRookPlacement {n : ℕ} (S : Finset (Fin n × Fin n)) : Prop :=(∀ p ∈ S, ∀ q ∈ S, p.1 = q.1 → p = q) ∧(∀ p ∈ S, ∀ q ∈ S, p.2 = q.2 → p = q)instance {n : ℕ} (S : Finset (Fin n × Fin n)) : Decidable (IsRookPlacement S) :=instDecidableAnddef a : ℕ → ℕ| 0 => 0| n + 1 => (((board (n + 1)).powersetCard n).filter IsRookPlacement).cardend OMA.S_oeis_a005777namespace RookWordopen OMA.S_oeis_a005777noncomputable sectionvariable {n : ℕ}theorem advance_add (i : Fin n) (u v : ℕ) :advance (advance i u) v = advance i (u+v) := byapply Fin.extsimp [advance, Nat.mod_add_mod, Nat.add_assoc]theorem advance_cancel_right {i j : Fin n} (t : ℕ)(e : advance i t = advance j t) : i = j := byhave h : (i.val+t) ≡ (j.val+t) [MOD n] := congrArg Fin.val ehave h' := Nat.ModEq.add_right_cancel' t hexact Fin.ext (by simpa [Nat.ModEq, Nat.mod_eq_of_lt i.isLt, Nat.mod_eq_of_lt j.isLt] using h')theorem advance_cancel_left (i : Fin n) {u v : ℕ} (hu : u<n) (hv : v<n)(e : advance i u = advance i v) : u=v := byhave h : (i.val+u) ≡ (i.val+v) [MOD n] := congrArg Fin.val ehave h' := Nat.ModEq.add_left_cancel' i.val hsimpa [Nat.ModEq,Nat.mod_eq_of_lt hu,Nat.mod_eq_of_lt hv] using h'def offset (i j : Fin n) : ℕ := (j.val+n-i.val)%ntheorem advance_offset (i j : Fin n) : advance i (offset i j) = j := byapply Fin.extchange (i.val + ((j.val+n-i.val)%n))%n=j.valhave hi := i.isLtrw [Nat.add_mod_mod,show i.val+(j.val+n-i.val)=j.val+n by omega]simp [Nat.mod_eq_of_lt j.isLt]theorem offset_advance (i : Fin n) {t : ℕ} (ht : t<n) : offset i (advance i t) = t :=advance_cancel_left i (Nat.mod_lt _ (Nat.zero_lt_of_lt i.isLt)) ht (advance_offset _ _)def ColInj (w : Word n) : Prop :=∀ i j, (w i).val<4 → (w j).val<4 →advance i (w i).val = advance j (w j).val → i=jtheorem local_iff_inj {w : Word n} (hn : 4≤n) : Local w ↔ ColInj w := byconstructor· intro hhave cannot {i j : Fin n} (hi : (w i).val<4) (hj : (w j).val<4)(hlt : (w j).val<(w i).val) (e : advance i (w i).val = advance j (w j).val) : False := bylet t := (w i).val-(w j).valhave ht : t+(w j).val=(w i).val := by dsimp [t]; omegahave eq : advance i t = j := advance_cancel_right (w j).val (by rw [advance_add,ht,e])have hn' := h i t (by dsimp [t]; omega) (by dsimp [t]; omega) hirw [eq] at hn'exact hn' hj (by omega)intro i j hi hj ercases lt_trichotomy (w i).val (w j).val with h'|h'|h'· exact (cannot hj hi h' e.symm).elim· rw [h'] at eexact advance_cancel_right _ e· exact (cannot hi hj h' e).elim· intro h i t hp hq hi hj eqhave e : advance i (w i).val = advance (advance i t) (w (advance i t)).val := byrw [advance_add,eq,Nat.add_comm]have ei := h i (advance i t) hi hj ehave ez : advance i 0 = i := by apply Fin.ext; simp [advance,Nat.mod_eq_of_lt i.isLt]have := advance_cancel_left i (by omega : 0<n) (by omega : t<n) (ez.trans ei)omegadef occupied (w : Word n) : Finset (Fin n) := Finset.univ.filter (fun i => (w i).val<4)def placement (w : Word n) : Finset (Fin n × Fin n) :=(occupied w).image (fun i => (i, advance i (w i).val))@[simp] theorem mem_placement {w : Word n} {i j : Fin n} :(i,j) ∈ placement w ↔ (w i).val < 4 ∧ advance i (w i).val = j := bysimp [placement, occupied]theorem placement_card (w : Word n) : (placement w).card=(occupied w).card :=Finset.card_image_of_injective _ (fun _ _ h => congrArg Prod.fst h)theorem placement_board (hn : 4≤n) (w : Word n) : placement w ⊆ board n := byrintro ⟨i,j⟩ hobtain ⟨hi,rfl⟩ := mem_placement.mp hsuffices offset i (advance i (w i).val)<4 by simpa [board,offset] using thisrwa [offset_advance _ (by omega)]theorem rook_iff (w : Word n) : IsRookPlacement (placement w) ↔ ColInj w := byconstructor· intro h i j hi hj eexact congrArg Prod.fst (h.2 _ (mem_placement.mpr ⟨hi,rfl⟩) _ (mem_placement.mpr ⟨hj,rfl⟩) e)· intro hconstructor· rintro ⟨i,j⟩ hp ⟨k,l⟩ hq (rfl : i=k)obtain ⟨_,rfl⟩ := mem_placement.mp hpobtain ⟨_,rfl⟩ := mem_placement.mp hqrfl· rintro ⟨i,j⟩ hp ⟨k,l⟩ hq (rfl : j=l)obtain ⟨hi,ei⟩ := mem_placement.mp hpobtain ⟨hk,ek⟩ := mem_placement.mp hqrw [h i k hi hk (ei.trans ek.symm)]noncomputable def wordOf (s : Finset (Fin n × Fin n)) (i : Fin n) : Fin 5 :=if h : ∃ j, (i,j) ∈ s then Fin.ofNat 5 (offset i h.choose) else 4theorem wordOf_mem {s : Finset (Fin n × Fin n)} (hs : s ⊆ board n)(hr : IsRookPlacement s) {i j : Fin n} (h : (i,j)∈s) : (wordOf s i).val = offset i j := bylet hex : ∃ j, (i,j)∈s := ⟨j,h⟩have eq := congrArg Prod.snd (hr.1 _ hex.choose_spec _ h rfl)change hex.choose=j at eqhave bound : offset i j<4 := (Finset.mem_filter.mp (hs h)).2simp [wordOf, hex, eq, Fin.ofNat, Nat.mod_eq_of_lt (by omega : offset i j<5)]theorem placement_wordOf {s : Finset (Fin n × Fin n)} (hs : s⊆board n)(hr : IsRookPlacement s) : placement (wordOf s)=s := byext ⟨i,j⟩constructor· intro hpobtain ⟨hi,e⟩ := mem_placement.mp hpby_cases hex : ∃ k, (i,k)∈s· obtain ⟨k,hk⟩ := hexrw [wordOf_mem hs hr hk,advance_offset] at esimpa [←e] using hk· simp [wordOf,hex] at hi· intro happly mem_placement.mprrw [wordOf_mem hs hr h,advance_offset]exact ⟨(Finset.mem_filter.mp (hs h)).2,rfl⟩theorem wordOf_placement (hn : 4≤n) (w : Word n) (h : ColInj w) : wordOf (placement w)=w := byfunext iby_cases hi : (w i).val<4· apply Fin.exthave hz := wordOf_mem (placement_board hn w) ((rook_iff w).mpr h) (mem_placement.mpr ⟨hi,rfl⟩)simpa [offset_advance _ (by omega : (w i).val<n)] using hz· have hex : ¬ ∃ k, (i,k)∈placement w := byrintro ⟨k,hk⟩rw [mem_placement] at hkexact hi hk.1apply Fin.extrw [wordOf, dif_neg hex]simpomegaendend RookWord/-!# The eight-state automaton for the four-diagonal boardA row of the board either carries a rook in one of the four admissible columnoffsets (`0,1,2,3`) or stays empty (letter `4`). When rows are read from top tobottom, the only information the next row needs is which of the three followingcolumns are already occupied; that is an element of `Fin 8`.* `step m d` is the mask after the letter `d` is read in mask `m`;* `allowed m d` says that the letter `d` may be read in mask `m`;* `st a b c` is the mask recorded by the three consecutive letters `a`,`b`,`c`.The bridge lemmas `st_step` and `allowed_st` are proved by exhaustively checkingthe 625 letter quadruples.-/set_option maxRecDepth 10000namespace RookMask/-- The eight three-bit occupancy masks. -/abbrev Mask := Fin 8/-- The letter `d` occupies the column `d` positions to the right of the currentrow when `1 ≤ d ≤ 3`; the letters `0` and `4` (empty row) contribute nothing. -/def letterBit (d : Fin 5) : ℕ := if d.val = 0 ∨ d.val = 4 then 0 else 2 ^ (d.val - 1)/-- Push one letter through the three-bit window. -/def step (m : Mask) (d : Fin 5) : Mask :=⟨((m.val >>> 1) ||| letterBit d) % 8, Nat.mod_lt _ (by decide : 0 < 8)⟩/-- A letter may be read unless its column is already occupied by an earlier row.The four offset letters use bits `0..3` (bit `3` is never set, so offset `3` isalways free) and the empty letter `4` uses bit `4`, which is also never set. -/def allowed (m : Mask) (d : Fin 5) : Bool := (m.val &&& (1 <<< d.val)) == 0/-- The mask recorded by three consecutive letters: bit `j` (`j = 0,1,2`) saysthat column `j` to the right of the next row is occupied. -/def st (a b c : Fin 5) : Mask :=⟨((if a.val = 3 ∨ b.val = 2 ∨ c.val = 1 then 1 else 0) +(if b.val = 3 ∨ c.val = 2 then 2 else 0) + (if c.val = 3 then 4 else 0)) % 8,Nat.mod_lt _ (by decide : 0 < 8)⟩/-- The local constraint for a window `(a,b,c)` followed by the letter `d`:no occupied earlier row shares a column with row `d`. -/abbrev Loc4 (a b c d : Fin 5) : Prop :=(a.val < 4 → d.val < 4 → a.val ≠ d.val + 3) ∧(b.val < 4 → d.val < 4 → b.val ≠ d.val + 2) ∧(c.val < 4 → d.val < 4 → c.val ≠ d.val + 1)/-- Reading a new letter shifts the window by one place. -/theorem st_step : ∀ q : Fin 5 × Fin 5 × Fin 5 × Fin 5,st q.2.1 q.2.2.1 q.2.2.2 = step (st q.1 q.2.1 q.2.2.1) q.2.2.2 := bydecide/-- The automaton accepts exactly the locally admissible letters. -/theorem allowed_st : ∀ q : Fin 5 × Fin 5 × Fin 5 × Fin 5,allowed (st q.1 q.2.1 q.2.2.1) q.2.2.2 = true ↔ Loc4 q.1 q.2.1 q.2.2.1 q.2.2.2 := bydecide/-- Unpacked form of `allowed_st`. -/theorem allowed_st' (a b c d : Fin 5) :allowed (st a b c) d = true ↔(a.val < 4 → d.val < 4 → a.val ≠ d.val + 3) ∧(b.val < 4 → d.val < 4 → b.val ≠ d.val + 2) ∧(c.val < 4 → d.val < 4 → c.val ≠ d.val + 1) :=allowed_st (a, b, c, d)/-- Unpacked form of `st_step`. -/theorem st_step' (a b c d : Fin 5) : st b c d = step (st a b c) d :=st_step (a, b, c, d)/-- Three empty rows reset the mask completely, so the mask three steps after anempty row depends only on the two letters that follow it. -/theorem step_three_val : ∀ q : Mask × Fin 5 × Fin 5,(step (step (step q.1 4) q.2.1) q.2.2).val= (letterBit q.2.1 >>> 1) ||| letterBit q.2.2 := bydecide/-- The three-letter window starting with an empty row. -/theorem st_empty_val : ∀ q : Fin 5 × Fin 5,(st 4 q.1 q.2).val = (letterBit q.1 >>> 1) ||| letterBit q.2 := bydecide/-- Three shifts discard the whole mask: the three-letter window is recovered fromany starting mask after three steps. -/theorem st_eq_step_three : ∀ q : Mask × Fin 5 × Fin 5 × Fin 5,st q.2.1 q.2.2.1 q.2.2.2 = step (step (step q.1 q.2.1) q.2.2.1) q.2.2.2 := bydecideend RookMaskset_option maxRecDepth 10000namespace RookPathsopen Finsetvariable {ι : Type}/-! ## The weighted walk count -//-- `walkCount T n u v` counts the weighted walks of length `n` from `u` to `v`in the weighted graph whose weight matrix is `T`. -/def walkCount [Fintype ι] [DecidableEq ι] (T : ι → ι → ℕ) (n : ℕ) (u v : ι) : ℕ :=∑ p : Fin (n+1) → ι,if p 0 = u ∧ p ⟨n, Nat.lt_succ_self n⟩ = v then∏ t : Fin n, T (p t.castSucc) (p t.succ)else 0/-- The definition of `walkCount` with the last vertex written as `Fin.last`. -/theorem walkCount_last [Fintype ι] [DecidableEq ι] (T : ι → ι → ℕ) (n : ℕ) (u v : ι) :walkCount T n u v= ∑ p : Fin (n+1) → ι,(if p 0 = u ∧ p (Fin.last n) = v then∏ t : Fin n, T (p t.castSucc) (p t.succ)else 0) := rfl/-! ## Prepending a vertex to a walk -//-- The function obtained by prepending `a` to `q`. -/def consFun (a : ι) {n : ℕ} (q : Fin n → ι) : Fin (n+1) → ι := Fin.cons a qtheorem consFun_zero (a : ι) {n : ℕ} (q : Fin n → ι) : consFun a q 0 = a := rfltheorem consFun_succ (a : ι) {n : ℕ} (q : Fin n → ι) (i : Fin n) :consFun a q i.succ = q i := rfltheorem consFun_last (a : ι) {n : ℕ} (q : Fin (n+1) → ι) :consFun a q (Fin.last (n+1)) = q (Fin.last n) := byrw [show Fin.last (n+1) = (Fin.last n).succ from rfl, consFun_succ]theorem consFun_tail (a : ι) {n : ℕ} (q : Fin n → ι) : Fin.tail (consFun a q) = q := rfl/-- Splitting off the first value of a function `Fin (n+1) → ι`. -/def consEquiv (ι : Type) (n : ℕ) : ι × (Fin n → ι) ≃ (Fin (n+1) → ι) wheretoFun x := consFun x.1 x.2invFun p := (p 0, Fin.tail p)left_inv x := byobtain ⟨a, q⟩ := xsimp [consFun_zero, consFun_tail]right_inv p := Fin.cons_self_tail ptheorem consEquiv_apply {n : ℕ} (a : ι) (q : Fin n → ι) :consEquiv ι n (a, q) = consFun a q := rfl/-! ## The integrand at a prepended walk -/theorem prod_cons (T : ι → ι → ℕ) (n : ℕ) (a : ι) (q : Fin (n+1) → ι) :∏ t : Fin (n+1), T (consFun a q t.castSucc) (consFun a q t.succ)= T a (q 0) * ∏ t : Fin n, T (q t.castSucc) (q t.succ) := byrw [Fin.prod_univ_succ]congr 1theorem cons_integrand [DecidableEq ι] (T : ι → ι → ℕ) (n : ℕ) (u v : ι)(a : ι) (q : Fin (n+1) → ι) :(if consFun a q 0 = u ∧ consFun a q (Fin.last (n+1)) = v then∏ t : Fin (n+1), T (consFun a q t.castSucc) (consFun a q t.succ)else 0)= (if a = u ∧ q (Fin.last n) = v thenT a (q 0) * ∏ t : Fin n, T (q t.castSucc) (q t.succ)else 0) := byhave hc : consFun a q 0 = a := consFun_zero a qhave hl : consFun a q (Fin.last (n+1)) = q (Fin.last n) := consFun_last a qby_cases h : a = u ∧ q (Fin.last n) = v· rw [if_pos ⟨by rw [hc]; exact h.1, by rw [hl]; exact h.2⟩, if_pos h, prod_cons]· rw [if_neg (fun hh => h ⟨by rw [← hh.1]; exact hc, by rw [← hh.2]; exact hl⟩), if_neg h]/-! ## Splitting the sum over the first step -/theorem walkCount_succ_sum [Fintype ι] [DecidableEq ι] (T : ι → ι → ℕ) (n : ℕ) (u v : ι) :walkCount T (n+1) u v= ∑ a : ι, ∑ q : Fin (n+1) → ι,(if a = u ∧ q (Fin.last n) = v thenT a (q 0) * ∏ t : Fin n, T (q t.castSucc) (q t.succ)else 0) := byrw [walkCount_last]rw [← Equiv.sum_comp (consEquiv ι (n+1)) (fun p : Fin ((n+1)+1) → ι =>(if p 0 = u ∧ p (Fin.last (n+1)) = v then∏ t : Fin (n+1), T (p t.castSucc) (p t.succ)else 0))]rw [Fintype.sum_prod_type]refine Finset.sum_congr rfl (fun a _ => Finset.sum_congr rfl (fun q _ => ?_))simp only [consEquiv_apply]exact cons_integrand T n u v a qtheorem collapse_head [Fintype ι] [DecidableEq ι] (T : ι → ι → ℕ) (n : ℕ) (u v : ι) :(∑ a : ι, ∑ q : Fin (n+1) → ι,(if a = u ∧ q (Fin.last n) = v thenT a (q 0) * ∏ t : Fin n, T (q t.castSucc) (q t.succ)else 0))= ∑ q : Fin (n+1) → ι,(if q (Fin.last n) = v thenT u (q 0) * ∏ t : Fin n, T (q t.castSucc) (q t.succ)else 0) := byrw [Finset.sum_comm]refine Finset.sum_congr rfl (fun q _ => ?_)rw [Finset.sum_eq_single u]· by_cases hv : q (Fin.last n) = v <;> simp [hv]· intro b _ hbsimp [hb]· intro hexact absurd (Finset.mem_univ u) htheorem mul_walkCount_sum [Fintype ι] [DecidableEq ι] (T : ι → ι → ℕ) (n : ℕ) (u v : ι) :(∑ z : ι, T u z * walkCount T n z v)= ∑ q : Fin (n+1) → ι,(if q (Fin.last n) = v thenT u (q 0) * ∏ t : Fin n, T (q t.castSucc) (q t.succ)else 0) := byhave h : ∀ z : ι, T u z * walkCount T n z v= ∑ q : Fin (n+1) → ι,T u z * (if q 0 = z ∧ q (Fin.last n) = v then∏ t : Fin n, T (q t.castSucc) (q t.succ) else 0) := byintro zrw [walkCount_last, Finset.mul_sum]rw [Finset.sum_congr rfl (fun z _ => h z)]rw [Finset.sum_comm]refine Finset.sum_congr rfl (fun q _ => ?_)rw [Finset.sum_eq_single (q 0)]· by_cases hv : q (Fin.last n) = v <;> simp [hv]· intro b _ hbhave hb' : ¬ (q 0 = b) := fun e => hb e.symmsimp [hb']· intro hexact absurd (Finset.mem_univ (q 0)) h/-! ## The recursion -/theorem walkCount_succ [Fintype ι] [DecidableEq ι] (T : ι → ι → ℕ) (n : ℕ) (u v : ι) :walkCount T (n+1) u v = ∑ z : ι, T u z * walkCount T n z v := byrw [walkCount_succ_sum, collapse_head, ← mul_walkCount_sum]/-! ## The base case -/theorem walkCount_zero [Fintype ι] [DecidableEq ι] (T : ι → ι → ℕ) (u v : ι) :walkCount T 0 u v = if u = v then 1 else 0 := byrw [walkCount, Finset.sum_eq_single (fun _ : Fin (0+1) => u)]· simp· intro p _ hphave h0 : p 0 ≠ u := byintro happly hpfunext ihave hi : i = 0 := Fin.ext (by have := i.isLt; omega)rw [hi]exact hsimp [h0]· intro hexact absurd (Finset.mem_univ (fun _ : Fin (0+1) => u)) h/-! ## The length-one case -/theorem walkCount_one [Fintype ι] [DecidableEq ι] (T : ι → ι → ℕ) (u v : ι) :walkCount T 1 u v = T u v := byrw [show (1 : ℕ) = 0 + 1 from rfl, walkCount_succ]simp only [walkCount_zero]rw [Finset.sum_eq_single v]· simp· intro b _ hbsimp [hb]· intro hexact absurd (Finset.mem_univ v) h/-! ## The main theorem -/theorem walkCount_eq_pow {ι : Type} [Fintype ι] [DecidableEq ι](T : ι → ι → ℕ) (n : ℕ) (u v : ι) :walkCount T n u v = ((Matrix.of T : Matrix ι ι ℕ) ^ n) u v := byinduction n generalizing u v with| zero => rw [walkCount_zero, pow_zero, Matrix.one_apply]| succ n ih =>rw [walkCount_succ, pow_succ', Matrix.mul_apply]refine Finset.sum_congr rfl (fun z _ => ?_)rw [ih z v]simp [Matrix.of_apply]/-- The recursion splitting off the *last* step. -/theorem walkCount_succ' [Fintype ι] [DecidableEq ι] (T : ι → ι → ℕ) (n : ℕ) (u v : ι) :walkCount T (n+1) u v = ∑ z : ι, walkCount T n u z * T z v := byrw [walkCount_eq_pow (n := n + 1), pow_succ, Matrix.mul_apply]refine Finset.sum_congr rfl (fun z _ => ?_)rw [walkCount_eq_pow T n u z]simp [Matrix.of_apply]end RookPaths/-!# `RookRec`The `16 × 16` integer transfer matrix `BZ` of the two-layer eight-stateautomaton, its annihilating polynomial`(t - 1)^3 * (t^3 - t^2 - t - 1)^4`,the sum `fZ n` of its eight "layer 0" entries `(i, 8+i)`, the linear recurrencesatisfied by `fZ`, and the first fifteen values of `fZ`.Everything is proved in the kernel: the matrix identities are closed by`decide +kernel`, and the recurrence is obtained from the matrix identity byapplying the additive functional `phi` that sums the eight selected entries.Note: the short name `minpoly` is already used by Mathlib, so the declarationslive in the namespace `RookRec` (`RookRec.minpoly`, `RookRec.fZ`, ...).-/set_option maxRecDepth 10000set_option maxHeartbeats 0open Matrixnamespace RookRec/-- The two-layer transfer matrix with 16 states. -/def BZ : Matrix (Fin 16) (Fin 16) ℤ :=!![1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0;0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0;0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0;0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0;0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0;0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0;0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0;0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0;0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0;0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0;0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0;0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0;0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0;0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0;0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1;0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]/-- Sum of the eight entries `(i, 8+i)`, `i = 0,…,7`, i.e. the "layer 0" states. -/def fZ (n : ℕ) : ℤ := ∑ i : Fin 8, (BZ ^ n) ⟨i.val, by omega⟩ ⟨i.val + 8, by omega⟩/-- `(t-1)^3 (t^3-t^2-t-1)^4` annihilates `BZ`. -/theorem minpoly : BZ ^ 15 - 7 • BZ ^ 14 + 17 • BZ ^ 13 - 15 • BZ ^ 12 + 5 • BZ ^ 11- 19 • BZ ^ 10 + 29 • BZ ^ 9 - 3 • BZ ^ 8 + 3 • BZ ^ 7 - 21 • BZ ^ 6+ 3 • BZ ^ 5 + 3 • BZ ^ 4 + 7 • BZ ^ 3 - BZ ^ 2 - BZ - 1 = 0 := bydecide +kernel/-- The additive functional summing the eight layer-0 entries of a matrix. -/def phi (M : Matrix (Fin 16) (Fin 16) ℤ) : ℤ :=∑ i : Fin 8, M ⟨i.val, by omega⟩ ⟨i.val + 8, by omega⟩theorem fZ_eq (n : ℕ) : fZ n = phi (BZ ^ n) := rfltheorem phi_zero : phi 0 = 0 := bysimp [phi]theorem phi_add (A B : Matrix (Fin 16) (Fin 16) ℤ) : phi (A + B) = phi A + phi B := bysimp [phi, Finset.sum_add_distrib]theorem phi_sub (A B : Matrix (Fin 16) (Fin 16) ℤ) : phi (A - B) = phi A - phi B := bysimp [phi, Finset.sum_sub_distrib]theorem phi_nsmul (k : ℕ) (A : Matrix (Fin 16) (Fin 16) ℤ) : phi (k • A) = (k : ℤ) * phi A := bysimp [phi, Finset.mul_sum]/-- The linear recurrence for `fZ`. -/theorem fZ_rec (n : ℕ) : fZ (n + 15) =fZ n + fZ (n+1) + fZ (n+2) - 7 * fZ (n+3) - 3 * fZ (n+4) - 3 * fZ (n+5)+ 21 * fZ (n+6) - 3 * fZ (n+7) + 3 * fZ (n+8) - 29 * fZ (n+9) + 19 * fZ (n+10)- 5 * fZ (n+11) + 15 * fZ (n+12) - 17 * fZ (n+13) + 7 * fZ (n+14) := byhave h := congrArg (fun M : Matrix (Fin 16) (Fin 16) ℤ => phi (BZ ^ n * M)) minpolysimp only [Matrix.mul_zero, phi_zero, Matrix.mul_sub, Matrix.mul_add, Matrix.mul_smul,Matrix.mul_one, phi_add, phi_sub, phi_nsmul, ← pow_add, ← pow_succ, ← fZ_eq] at hlinear_combination h/-- The first fifteen values of `fZ`, starting at `n = 0`. -/theorem fZ_vals : (List.range 15).map fZ =[0, 1, 8, 33, 96, 265, 672, 1617, 3776, 8577, 19080, 41745, 90048, 191945, 404992] := bydecide +kernelend RookRec/-!# Counting the wordsFor `n ≥ 4` the rook numbers of the four-diagonal board equal the number ofcyclic words with exactly one empty letter satisfying the local constraint.That word count is in turn the number of closed labelled walks of theeight-state automaton `RookMask`, read with two layers that record whether theempty row has already been passed.-/set_option maxRecDepth 10000set_option maxHeartbeats 0namespace RookCountopen RookMask RookWord OMA.S_oeis_a005777/-- Two layers: `false` before the empty row, `true` after it. -/abbrev St := Bool × Mask/-- The letters that may label the step `x → y`. -/def lbl (x y : St) : Finset (Fin 5) :=Finset.univ.filter fun d =>allowed x.2 d = true ∧(if d.val = 4 then x.1 = false ∧ y.1 = true else x.1 = y.1) ∧step x.2 d = y.2@[simp] theorem mem_lbl {x y : St} {d : Fin 5} :d ∈ lbl x y ↔ allowed x.2 d = true ∧(if d.val = 4 then x.1 = false ∧ y.1 = true else x.1 = y.1) ∧step x.2 d = y.2 := bysimp [lbl]/-- Labelled walks of length `n` from `u` to `v`. -/abbrev WalkSet (n : ℕ) (u v : St) : Type :={ P : (Fin (n + 1) → St) × (Fin n → Fin 5) //P.1 0 = u ∧ P.1 ⟨n, Nat.lt_succ_self n⟩ = v ∧∀ t : Fin n, P.2 t ∈ lbl (P.1 t.castSucc) (P.1 t.succ) }instance (n : ℕ) (u v : St) : Fintype (WalkSet n u v) := byunfold WalkSetinfer_instancevariable {n : ℕ}/-- `advance` by the full length is the identity. -/theorem advance_self {n : ℕ} (i : Fin n) : advance i n = i := byapply Fin.extshow (i.val + n) % n = i.valrw [Nat.add_mod_right, Nat.mod_eq_of_lt i.isLt]/-- Reduction of `Fin (n+1)` indices modulo `n`. -/def toFin (hn : 0 < n) (j : Fin (n + 1)) : Fin n := ⟨j.val % n, Nat.mod_lt _ hn⟩theorem toFin_zero (hn : 0 < n) : toFin (n := n) hn (0 : Fin (n + 1)) = ⟨0, hn⟩ := byapply Fin.extsimp [toFin]theorem toFin_last (hn : 0 < n) : toFin (n := n) hn ⟨n, Nat.lt_succ_self n⟩ = ⟨0, hn⟩ := byapply Fin.extsimp [toFin]theorem toFin_castSucc (hn : 0 < n) (t : Fin n) : toFin (n := n) hn t.castSucc = t := byapply Fin.extshow t.val % n = t.valexact Nat.mod_eq_of_lt t.isLttheorem toFin_succ (hn : 0 < n) (t : Fin n) : toFin (n := n) hn t.succ = advance t 1 := byapply Fin.extrfl/-- The mask read by the word `w` at position `i`. -/def msk (w : Word n) (i : Fin n) : Mask :=st (w (advance i (n - 3))) (w (advance i (n - 2))) (w (advance i (n - 1)))theorem msk_step (hn : 4 ≤ n) (w : Word n) (i : Fin n) :msk w (advance i 1) = step (msk w i) (w i) := byunfold mskrw [advance_add, advance_add, advance_add,show 1 + (n - 3) = n - 2 by omega,show 1 + (n - 2) = n - 1 by omega,show 1 + (n - 1) = n by omega, advance_self]exact st_step (w (advance i (n - 3)), w (advance i (n - 2)), w (advance i (n - 1)), w i)/-- Local constraints, indexed from a position backwards. -/theorem local_allowed (hn : 4 ≤ n) (w : Word n) (hL : Local w) (i : Fin n) :allowed (msk w i) (w i) = true := byshow allowed (st (w (advance i (n - 3))) (w (advance i (n - 2))) (w (advance i (n - 1))))(w i) = truerefine (allowed_st' _ _ _ _).mpr ⟨?_, ?_, ?_⟩· intro ha hd hchave e : advance (advance i (n - 3)) 3 = i := byrw [advance_add, show (n - 3) + 3 = n by omega, advance_self]refine hL (advance i (n - 3)) 3 (by omega) (by omega) ha ?_ ?_· rw [e]; exact hd· rw [e]; exact hc· intro ha hd hchave e : advance (advance i (n - 2)) 2 = i := byrw [advance_add, show (n - 2) + 2 = n by omega, advance_self]refine hL (advance i (n - 2)) 2 (by omega) (by omega) ha ?_ ?_· rw [e]; exact hd· rw [e]; exact hc· intro ha hd hchave e : advance (advance i (n - 1)) 1 = i := byrw [advance_add, show (n - 1) + 1 = n by omega, advance_self]refine hL (advance i (n - 1)) 1 (by omega) (by omega) ha ?_ ?_· rw [e]; exact hd· rw [e]; exact hc/-! ### From a word to a closed labelled walk -//-- Position of the empty row. -/noncomputable def emptyPos {w : Word n} (hS : Single w) : Fin n := hS.choosetheorem emptyPos_val {w : Word n} (hS : Single w) : (w (emptyPos hS)).val = 4 :=hS.choose_spec.1theorem emptyPos_unique {w : Word n} (hS : Single w) {i : Fin n} (hi : (w i).val = 4) :i = emptyPos hS := hS.choose_spec.2 i hi/-- The layer of a position of the walk. -/noncomputable def lay {w : Word n} (hS : Single w) (j : Fin (n + 1)) : Bool :=decide ((emptyPos hS).val < j.val)theorem lay_zero {w : Word n} (hS : Single w) : lay hS (0 : Fin (n + 1)) = false := bysimp [lay]theorem lay_last {w : Word n} (hS : Single w) (hn : 4 ≤ n) :lay hS ⟨n, Nat.lt_succ_self n⟩ = true := bysimp only [lay, decide_eq_true_eq]have := (emptyPos hS).isLtomega/-- A step that is not the empty row does not change the layer. -/theorem lay_succ_of_ne {w : Word n} (hS : Single w) (t : Fin n) (h : t ≠ emptyPos hS) :lay hS t.succ = lay hS t.castSucc := byhave ht : (emptyPos hS).val ≠ t.val := fun hc => h (Fin.ext hc.symm)simp only [lay, Fin.val_succ, Fin.val_castSucc]rcases Nat.lt_or_gt_of_ne ht with hlt | hlt· rw [decide_eq_true_iff.mpr hlt, decide_eq_true_iff.mpr (by omega)]· rw [decide_eq_false_iff_not.mpr (by omega), decide_eq_false_iff_not.mpr (by omega)]/-- The empty step flips the layer. -/theorem lay_succ_empty {w : Word n} (hS : Single w) :lay hS (emptyPos hS).succ = true ∧ lay hS (emptyPos hS).castSucc = false := byrefine ⟨?_, ?_⟩· simp only [lay, Fin.val_succ, decide_eq_true_eq]; omega· simp only [lay, Fin.val_castSucc, decide_eq_false_iff_not]; omega/-! ### The labelled walks are counted by the transfer matrix -//-- Walks of length `n+1` from `u` to `v` whose state after one step is `a`. -/abbrev fiber (n : ℕ) (u v a : St) : Type :={ P : WalkSet (n + 1) u v // P.1.1 ⟨1, by omega⟩ = a }instance (n : ℕ) (u v a : St) : Fintype (fiber n u v a) := byunfold fiber; infer_instance/-- The first label of a walk whose second state is fixed. -/theorem fiber_label {n : ℕ} {u v a : St} (P : fiber n u v a) :P.1.1.2 (0 : Fin (n + 1)) ∈ lbl u a := byhave h := P.1.2.2.2 (0 : Fin (n + 1))rw [show Fin.castSucc (0 : Fin (n + 1)) = (0 : Fin (n + 2)) from rfl,show Fin.succ (0 : Fin (n + 1)) = (⟨1, by omega⟩ : Fin (n + 2)) from rfl,P.1.2.1, P.2] at hexact h/-- The tail of a walk is a walk. -/noncomputable def fiberTail {n : ℕ} {u v a : St} (P : fiber n u v a) : WalkSet n a v :=⟨(Fin.tail P.1.1.1, Fin.tail P.1.1.2), byshow P.1.1.1 (Fin.succ 0) = aexact P.2,byshow P.1.1.1 (Fin.succ ⟨n, Nat.lt_succ_self n⟩) = vhave h := P.1.2.2.1have e : Fin.succ ⟨n, Nat.lt_succ_self n⟩ = (⟨n + 1, by omega⟩ : Fin (n + 2)) := rflrw [e]; exact h,byintro texact P.1.2.2.2 t.succ⟩/-- Prepending a state and a label to a walk. -/noncomputable def consWalk {n : ℕ} (u : St) (v a : St) (d : {d : Fin 5 // d ∈ lbl u a})(Q : WalkSet n a v) : fiber n u v a :=⟨⟨(Fin.cons u Q.1.1, Fin.cons d.1 Q.1.2), rfl, byshow Q.1.1 ⟨n, Nat.lt_succ_self n⟩ = vexact Q.2.2.1,byintro trefine Fin.cases ?_ ?_ t· show d.1 ∈ lbl u (Q.1.1 0)rw [Q.2.1]exact d.2· intro kshow Q.1.2 k ∈ lbl (Q.1.1 (Fin.castSucc k)) (Q.1.1 (Fin.succ k))exact Q.2.2.2 k⟩, byshow Q.1.1 0 = aexact Q.2.1⟩@[simp] theorem fiberTail_path {n : ℕ} {u v a : St} (P : fiber n u v a) :(fiberTail P).1.1 = Fin.tail P.1.1.1 := rfl@[simp] theorem fiberTail_label {n : ℕ} {u v a : St} (P : fiber n u v a) :(fiberTail P).1.2 = Fin.tail P.1.1.2 := rfl@[simp] theorem consWalk_path {n : ℕ} (u v a : St) (d : {d : Fin 5 // d ∈ lbl u a})(Q : WalkSet n a v) : (consWalk u v a d Q).1.1.1 = Fin.cons u Q.1.1 := rfl@[simp] theorem consWalk_label {n : ℕ} (u v a : St) (d : {d : Fin 5 // d ∈ lbl u a})(Q : WalkSet n a v) : (consWalk u v a d Q).1.1.2 = Fin.cons d.1 Q.1.2 := rfl@[simp] theorem consWalk_second {n : ℕ} (u v a : St) (d : {d : Fin 5 // d ∈ lbl u a})(Q : WalkSet n a v) : (consWalk u v a d Q).1.1.1 ⟨1, by omega⟩ = a := byhave h : (consWalk u v a d Q).1.1.1 ⟨1, by omega⟩ = Q.1.1 0 := rflrw [h, Q.2.1]theorem fiberTail_consWalk {n : ℕ} (u v a : St) (d : {d : Fin 5 // d ∈ lbl u a})(Q : WalkSet n a v) : fiberTail (consWalk u v a d Q) = Q := byapply Subtype.extapply Prod.ext· simp only [fiberTail_path, consWalk_path, Fin.tail_cons]· simp only [fiberTail_label, consWalk_label, Fin.tail_cons]theorem consWalk_fiberTail {n : ℕ} {u v a : St} (P : fiber n u v a) :consWalk u v a ⟨P.1.1.2 0, fiber_label P⟩ (fiberTail P) = P := byapply Subtype.extapply Subtype.extapply Prod.ext· funext irefine Fin.cases ?_ ?_ i· show u = P.1.1.1 0exact P.1.2.1.symm· intro krfl· show Fin.cons (P.1.1.2 0) (Fin.tail P.1.1.2) = P.1.1.2exact Fin.cons_self_tail P.1.1.2/-- Splitting the first step. -/noncomputable def fiberEquiv (n : ℕ) (u v a : St) :fiber n u v a ≃ {d : Fin 5 // d ∈ lbl u a} × WalkSet n a v wheretoFun P := (⟨P.1.1.2 0, fiber_label P⟩, fiberTail P)invFun x := consWalk u v a x.1 x.2left_inv P := consWalk_fiberTail Pright_inv x := byobtain ⟨d, Q⟩ := xexact Prod.ext (Subtype.ext rfl) (fiberTail_consWalk u v a d Q)theorem card_walkSet_zero (u v : St) : Fintype.card (WalkSet 0 u v) = if u = v then 1 else 0 := byhave hu : u = (u.1, u.2) := rflhave hv : v = (v.1, v.2) := rflrw [hu, hv]generalize u.1 = bu at *generalize u.2 = mu at *generalize v.1 = bv at *generalize v.2 = mv at *fin_cases bu <;> fin_cases mu <;> fin_cases bv <;> fin_cases mv <;> decidetheorem card_walkSet_succ (n : ℕ) (u v : St) :Fintype.card (WalkSet (n + 1) u v) = ∑ a : St, Fintype.card (fiber n u v a) := byrw [← Fintype.card_sigma]refine Fintype.card_congr ⟨fun P => ⟨P.1.1 ⟨1, by omega⟩, P, rfl⟩, fun x => x.2.1, ?_, ?_⟩· intro P; rfl· intro xobtain ⟨a, P, hP⟩ := xhave : P.1.1 ⟨1, by omega⟩ = a := hPsubst thisrfltheorem card_fiber (n : ℕ) (u v a : St) :Fintype.card (fiber n u v a) = (lbl u a).card * Fintype.card (WalkSet n a v) := byhave h : Fintype.card {d : Fin 5 // d ∈ lbl u a} = (lbl u a).card := byrw [Fintype.card_subtype]have hh : ({x | x ∈ lbl u a} : Finset (Fin 5)) = lbl u a := byext xsimprw [hh]rw [Fintype.card_congr (fiberEquiv n u v a), Fintype.card_prod, h]/-- The labelled walks of length `n` are counted by the transfer matrix. -/theorem card_walkSet (n : ℕ) (u v : St) :Fintype.card (WalkSet n u v) =RookPaths.walkCount (fun x y => (lbl x y).card) n u v := byinduction n generalizing u with| zero => rw [card_walkSet_zero, RookPaths.walkCount_zero]| succ n ih =>rw [card_walkSet_succ, RookPaths.walkCount_succ]refine Finset.sum_congr rfl (fun a _ => ?_)rw [card_fiber, ih a, mul_comm]/-! ### From a closed walk back to a word -/section WalkToWordvariable {i : Mask}/-- The step relation read off from a labelled walk. -/theorem walk_step (P : WalkSet n (false, i) (true, i)) (t : Fin n) :step (P.1.1 t.castSucc).2 (P.1.2 t) = (P.1.1 t.succ).2 :=(mem_lbl.mp (P.2.2.2 t)).2.2/-- The allowedness read off from a labelled walk. -/theorem walk_allowed (P : WalkSet n (false, i) (true, i)) (t : Fin n) :allowed (P.1.1 t.castSucc).2 (P.1.2 t) = true :=(mem_lbl.mp (P.2.2.2 t)).1/-- The layer rule read off from a labelled walk. -/theorem walk_layer (P : WalkSet n (false, i) (true, i)) (t : Fin n) :(if (P.1.2 t).val = 4 then (P.1.1 t.castSucc).1 = false ∧ (P.1.1 t.succ).1 = trueelse (P.1.1 t.castSucc).1 = (P.1.1 t.succ).1) :=(mem_lbl.mp (P.2.2.2 t)).2.1/-- The mask of a closed walk is the three-letter window of its word. -/theorem walk_mask (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n) :∀ k : ℕ, ∀ h3 : 3 ≤ k, ∀ hk : k ≤ n,(P.1.1 ⟨k, by omega⟩).2= st (P.1.2 ⟨k - 3, by omega⟩) (P.1.2 ⟨k - 2, by omega⟩)(P.1.2 ⟨k - 1, by omega⟩) := byintro k h3refine Nat.le_induction ?base ?step k h3· intro hkhave e2 : (P.1.1 (⟨3, by omega⟩ : Fin (n + 1))).2= step (P.1.1 (⟨2, by omega⟩ : Fin (n + 1))).2 (P.1.2 (⟨2, by omega⟩ : Fin n)) := byhave h1 : (⟨2, by omega⟩ : Fin n).succ = (⟨3, by omega⟩ : Fin (n + 1)) := rflhave h2 : (⟨2, by omega⟩ : Fin n).castSucc = (⟨2, by omega⟩ : Fin (n + 1)) := rflrw [← h1, ← h2]; exact (walk_step P _).symmhave e1 : (P.1.1 (⟨2, by omega⟩ : Fin (n + 1))).2= step (P.1.1 (⟨1, by omega⟩ : Fin (n + 1))).2 (P.1.2 (⟨1, by omega⟩ : Fin n)) := byhave h1 : (⟨1, by omega⟩ : Fin n).succ = (⟨2, by omega⟩ : Fin (n + 1)) := rflhave h2 : (⟨1, by omega⟩ : Fin n).castSucc = (⟨1, by omega⟩ : Fin (n + 1)) := rflrw [← h1, ← h2]; exact (walk_step P _).symmhave e0 : (P.1.1 (⟨1, by omega⟩ : Fin (n + 1))).2= step (P.1.1 (⟨0, by omega⟩ : Fin (n + 1))).2 (P.1.2 (⟨0, by omega⟩ : Fin n)) := byhave h1 : (⟨0, by omega⟩ : Fin n).succ = (⟨1, by omega⟩ : Fin (n + 1)) := rflhave h2 : (⟨0, by omega⟩ : Fin n).castSucc = (⟨0, by omega⟩ : Fin (n + 1)) := rflrw [← h1, ← h2]; exact (walk_step P _).symmrw [e2, e1, e0]exact (st_eq_step_three ((P.1.1 (⟨0, by omega⟩ : Fin (n + 1))).2,P.1.2 ⟨0, by omega⟩, P.1.2 ⟨1, by omega⟩, P.1.2 ⟨2, by omega⟩)).symm· intro k hk ih hk1have hkn : k < n := by omegahave e : (P.1.1 (⟨k + 1, by omega⟩ : Fin (n + 1))).2= step (P.1.1 (⟨k, by omega⟩ : Fin (n + 1))).2 (P.1.2 (⟨k, hkn⟩ : Fin n)) := byhave h1 : (⟨k, hkn⟩ : Fin n).succ = (⟨k + 1, by omega⟩ : Fin (n + 1)) := rflhave h2 : (⟨k, hkn⟩ : Fin n).castSucc = (⟨k, by omega⟩ : Fin (n + 1)) := rflrw [← h1, ← h2]; exact (walk_step P _).symmhave step1 : (P.1.1 (⟨k + 1, by omega⟩ : Fin (n + 1))).2= st (P.1.2 ⟨k - 2, by omega⟩) (P.1.2 ⟨k - 1, by omega⟩) (P.1.2 (⟨k, hkn⟩ : Fin n)) := byrw [e]rw [show (P.1.1 (⟨k, by omega⟩ : Fin (n + 1))).2= st (P.1.2 ⟨k - 3, by omega⟩) (P.1.2 ⟨k - 2, by omega⟩)(P.1.2 ⟨k - 1, by omega⟩) from ih (by omega)]exact (st_step' _ _ _ _).symmrw [step1]have i1 : (⟨k - 2, by omega⟩ : Fin n) = ⟨k + 1 - 3, by omega⟩ := Fin.ext (by simp)have i2 : (⟨k - 1, by omega⟩ : Fin n) = ⟨k + 1 - 2, by omega⟩ := Fin.ext (by simp)have i3 : (⟨k, hkn⟩ : Fin n) = ⟨k + 1 - 1, by omega⟩ := Fin.ext (by simp)rw [i1, i2, i3]/-- Advancing from zero by `k < n` lands on `k`. -/theorem advance_zero {n : ℕ} (k : ℕ) (hk : k < n) :advance (⟨0, by omega⟩ : Fin n) k = ⟨k, hk⟩ := byapply Fin.extshow (0 + k) % n = krw [Nat.zero_add, Nat.mod_eq_of_lt hk]/-- The mask at position `0`, read cyclically, is the three-letter window. -/theorem msk_zero (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n) :msk P.1.2 (⟨0, by omega⟩ : Fin n)= st (P.1.2 ⟨n - 3, by omega⟩) (P.1.2 ⟨n - 2, by omega⟩)(P.1.2 ⟨n - 1, by omega⟩) := byshow st (P.1.2 (advance (⟨0, by omega⟩ : Fin n) (n - 3)))(P.1.2 (advance (⟨0, by omega⟩ : Fin n) (n - 2)))(P.1.2 (advance (⟨0, by omega⟩ : Fin n) (n - 1))) = _rw [advance_zero (n - 3) (by omega), advance_zero (n - 2) (by omega),advance_zero (n - 1) (by omega)]/-- The mask at the first position is the endpoint mask of the walk. -/theorem walk_mask_zero (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n) :(P.1.1 (⟨0, by omega⟩ : Fin (n + 1))).2 = msk P.1.2 (⟨0, by omega⟩ : Fin n) := byhave h0 : (P.1.1 (⟨0, by omega⟩ : Fin (n + 1))).2 = i := congrArg Prod.snd P.2.1have hn' : (P.1.1 (⟨n, Nat.lt_succ_self n⟩ : Fin (n + 1))).2 = i :=congrArg Prod.snd P.2.2.1have hm := walk_mask P hn n (by omega) (by omega)calc (P.1.1 (⟨0, by omega⟩ : Fin (n + 1))).2 = i := h0_ = (P.1.1 (⟨n, Nat.lt_succ_self n⟩ : Fin (n + 1))).2 := hn'.symm_ = st (P.1.2 ⟨n - 3, by omega⟩) (P.1.2 ⟨n - 2, by omega⟩) (P.1.2 ⟨n - 1, by omega⟩) := hm_ = msk P.1.2 (⟨0, by omega⟩ : Fin n) := (msk_zero P hn).symm/-- The mask at position one. -/theorem walk_mask_one (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n) :(P.1.1 (⟨1, by omega⟩ : Fin (n + 1))).2 = msk P.1.2 (⟨1, by omega⟩ : Fin n) := byhave hstep : (P.1.1 (⟨1, by omega⟩ : Fin (n + 1))).2= step (P.1.1 (⟨0, by omega⟩ : Fin (n + 1))).2 (P.1.2 (⟨0, by omega⟩ : Fin n)) := byhave h1 : (⟨0, by omega⟩ : Fin n).succ = (⟨1, by omega⟩ : Fin (n + 1)) := rflhave h2 : (⟨0, by omega⟩ : Fin n).castSucc = (⟨0, by omega⟩ : Fin (n + 1)) := rflrw [← h1, ← h2]; exact (walk_step P _).symmhave hm : msk P.1.2 (⟨1, by omega⟩ : Fin n) =step (msk P.1.2 (⟨0, by omega⟩ : Fin n)) (P.1.2 (⟨0, by omega⟩ : Fin n)) := byhave e : (⟨1, by omega⟩ : Fin n) = advance (⟨0, by omega⟩ : Fin n) 1 :=Fin.ext (by show (1 : ℕ) = (0 + 1) % n; rw [Nat.mod_eq_of_lt (by omega : 1 < n)])rw [e, msk_step hn P.1.2]rw [hstep, walk_mask_zero P hn, ← hm]/-- The mask at position two. -/theorem walk_mask_two (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n) :(P.1.1 (⟨2, by omega⟩ : Fin (n + 1))).2 = msk P.1.2 (⟨2, by omega⟩ : Fin n) := byhave hstep : (P.1.1 (⟨2, by omega⟩ : Fin (n + 1))).2= step (P.1.1 (⟨1, by omega⟩ : Fin (n + 1))).2 (P.1.2 (⟨1, by omega⟩ : Fin n)) := byhave h1 : (⟨1, by omega⟩ : Fin n).succ = (⟨2, by omega⟩ : Fin (n + 1)) := rflhave h2 : (⟨1, by omega⟩ : Fin n).castSucc = (⟨1, by omega⟩ : Fin (n + 1)) := rflrw [← h1, ← h2]; exact (walk_step P _).symmhave hm : msk P.1.2 (⟨2, by omega⟩ : Fin n) =step (msk P.1.2 (⟨1, by omega⟩ : Fin n)) (P.1.2 (⟨1, by omega⟩ : Fin n)) := byhave e : (⟨2, by omega⟩ : Fin n) = advance (⟨1, by omega⟩ : Fin n) 1 :=Fin.ext (by show (2 : ℕ) = (1 + 1) % n; rw [Nat.mod_eq_of_lt (by omega : 2 < n)])rw [e, msk_step hn P.1.2]rw [hstep, walk_mask_one P hn, ← hm]/-- The window identity for positions `j` with `j ≥ 3`. -/theorem msk_toFin_ge (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n) (j : Fin (n + 1))(hj : 3 ≤ j.val) :msk P.1.2 (toFin (by omega : 0 < n) j)= st (P.1.2 ⟨j.val - 3, by omega⟩) (P.1.2 ⟨j.val - 2, by omega⟩)(P.1.2 ⟨j.val - 1, by omega⟩) := byshow st (P.1.2 (advance (toFin (by omega : 0 < n) j) (n - 3)))(P.1.2 (advance (toFin (by omega : 0 < n) j) (n - 2)))(P.1.2 (advance (toFin (by omega : 0 < n) j) (n - 1))) = _have hjn : j.val ≤ n := Nat.le_of_lt_succ j.isLthave key : ∀ t : ℕ, 1 ≤ t → t ≤ 3 → (advance (toFin (by omega : 0 < n) j) (n - t)).val= j.val - t := byintro t ht1 ht3have hjt : t ≤ j.val := le_trans ht3 hjshow ((j.val % n) + (n - t)) % n = j.val - trcases Nat.lt_or_eq_of_le hjn with hlt | heq· rw [Nat.mod_eq_of_lt hlt]have e : j.val + (n - t) = (j.val - t) + n := by omegarw [e, Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)]· rw [heq, Nat.mod_self, Nat.zero_add, Nat.mod_eq_of_lt (by omega)]have e3 : advance (toFin (by omega : 0 < n) j) (n - 3) = (⟨j.val - 3, by omega⟩ : Fin n) :=Fin.ext (key 3 (by omega) (by omega))have e2 : advance (toFin (by omega : 0 < n) j) (n - 2) = (⟨j.val - 2, by omega⟩ : Fin n) :=Fin.ext (key 2 (by omega) (by omega))have e1 : advance (toFin (by omega : 0 < n) j) (n - 1) = (⟨j.val - 1, by omega⟩ : Fin n) :=Fin.ext (key 1 (by omega) (by omega))rw [e3, e2, e1]/-- The mask of a closed walk is the three-letter window of its word, at every position. -/theorem walk_mask_toFin (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n) :∀ j : Fin (n + 1), (P.1.1 j).2 = msk P.1.2 (toFin (by omega : 0 < n) j) := byintro jrcases Nat.lt_or_ge j.val 3 with hlt | hge· interval_cases hj : j.val· rw [show j = (⟨0, by omega⟩ : Fin (n + 1)) from Fin.ext (by simp [hj]),walk_mask_zero P hn]congr 1· rw [show j = (⟨1, by omega⟩ : Fin (n + 1)) from Fin.ext (by simp [hj]),walk_mask_one P hn]congr 1exact Fin.ext (by show (1 : ℕ) = 1 % n; rw [Nat.mod_eq_of_lt]; omega)· rw [show j = (⟨2, by omega⟩ : Fin (n + 1)) from Fin.ext (by simp [hj]),walk_mask_two P hn]congr 1exact Fin.ext (by show (2 : ℕ) = 2 % n; rw [Nat.mod_eq_of_lt]; omega)· have hm := walk_mask P hn j.val hge (Nat.le_of_lt_succ j.isLt)have hmj : (P.1.1 j).2= st (P.1.2 ⟨j.val - 3, by omega⟩) (P.1.2 ⟨j.val - 2, by omega⟩)(P.1.2 ⟨j.val - 1, by omega⟩) := hmrw [hmj, msk_toFin_ge P hn j hge]/-- The allowedness of the mask sequence at every position. -/theorem walk_allowed_all (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n) (j : Fin n) :allowed (msk P.1.2 j) (P.1.2 j) = true := byhave h := walk_allowed P jhave e : toFin (by omega : 0 < n) j.castSucc = j := byapply Fin.extshow j.val % n = j.valexact Nat.mod_eq_of_lt j.isLtrw [walk_mask_toFin P hn j.castSucc, e] at hexact h/-- A closed walk reads a locally admissible word. -/theorem walk_local (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n) : Local P.1.2 := byintro t k hk1 hk3 hi hjshow (P.1.2 t).val ≠ (P.1.2 (advance t k)).val + khave hA := walk_allowed_all P hn (advance t k)interval_cases k· have e : advance (advance t 1) (n - 1) = t := byrw [advance_add, show 1 + (n - 1) = n by omega, advance_self]have h := (allowed_st' (P.1.2 (advance (advance t 1) (n - 3)))(P.1.2 (advance (advance t 1) (n - 2))) (P.1.2 (advance (advance t 1) (n - 1)))(P.1.2 (advance t 1))).mp hAhave hh := h.2.2rw [e] at hhexact hh hi hj· have e : advance (advance t 2) (n - 2) = t := byrw [advance_add, show 2 + (n - 2) = n by omega, advance_self]have h := (allowed_st' (P.1.2 (advance (advance t 2) (n - 3)))(P.1.2 (advance (advance t 2) (n - 2))) (P.1.2 (advance (advance t 2) (n - 1)))(P.1.2 (advance t 2))).mp hAhave hh := h.2.1rw [e] at hhexact hh hi hj· have e : advance (advance t 3) (n - 3) = t := byrw [advance_add, show 3 + (n - 3) = n by omega, advance_self]have h := (allowed_st' (P.1.2 (advance (advance t 3) (n - 3)))(P.1.2 (advance (advance t 3) (n - 2))) (P.1.2 (advance (advance t 3) (n - 1)))(P.1.2 (advance t 3))).mp hAhave hh := h.1rw [e] at hhexact hh hi hj/-- The layer sequence of a closed walk, read as a function on `ℕ`. -/def layer (P : WalkSet n (false, i) (true, i)) (j : ℕ) : Bool :=if h : j < n + 1 then (P.1.1 ⟨j, h⟩).1 else falsetheorem layer_zero (P : WalkSet n (false, i) (true, i)) : layer P 0 = false := byrw [layer, dif_pos (Nat.zero_lt_succ n)]exact congrArg Prod.fst P.2.1theorem layer_last (P : WalkSet n (false, i) (true, i)) : layer P n = true := byrw [layer, dif_pos (Nat.lt_succ_self n)]exact congrArg Prod.fst P.2.2.1theorem layer_castSucc (P : WalkSet n (false, i) (true, i)) {t : ℕ} (ht : t < n) :layer P t = (P.1.1 (⟨t, ht⟩ : Fin n).castSucc).1 := byrw [layer, dif_pos (by omega : t < n + 1)]congr 1theorem layer_succ (P : WalkSet n (false, i) (true, i)) {t : ℕ} (ht : t < n) :layer P (t + 1) = if (P.1.2 ⟨t, ht⟩).val = 4 then true else layer P t := byrw [layer, layer, dif_pos (by omega : t + 1 < n + 1), dif_pos (by omega : t < n + 1)]by_cases h4 : (P.1.2 ⟨t, ht⟩).val = 4· rw [if_pos h4]have h := walk_layer P (⟨t, ht⟩ : Fin n)rw [if_pos h4] at hrw [show (⟨t, ht⟩ : Fin n).succ = (⟨t + 1, by omega⟩ : Fin (n + 1)) from Fin.ext rfl] at hexact h.2· rw [if_neg h4]have h := walk_layer P (⟨t, ht⟩ : Fin n)rw [if_neg h4] at hrw [show (⟨t, ht⟩ : Fin n).castSucc = (⟨t, by omega⟩ : Fin (n + 1)) from Fin.ext rfl,show (⟨t, ht⟩ : Fin n).succ = (⟨t + 1, by omega⟩ : Fin (n + 1)) from Fin.ext rfl] at hexact h.symmtheorem layer_mono (P : WalkSet n (false, i) (true, i)) {t : ℕ} (ht : t < n)(h : layer P t = true) : layer P (t + 1) = true := byrw [layer_succ P ht]by_cases h4 : (P.1.2 ⟨t, ht⟩).val = 4· rw [if_pos h4]· rw [if_neg h4]; exact htheorem layer_up (P : WalkSet n (false, i) (true, i)) :∀ a b : ℕ, a ≤ b → b ≤ n → layer P a = true → layer P b = true := byintro a b hab hbninduction b, hab using Nat.le_induction with| base => intro h; exact h| succ p hp ih =>intro hexact layer_mono P (by omega) (ih (by omega) h)/-- A closed walk reads a word with exactly one empty row. -/theorem walk_single (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n) : Single P.1.2 := byhave hex : ∃ t : Fin n, (P.1.2 t).val = 4 := byby_contra hallpush Not at hallhave hfalse : ∀ j, j ≤ n → layer P j = false := byintro jinduction j with| zero => intro _; exact layer_zero P| succ p ih =>intro hphave hpn : p < n := by omegarw [layer_succ P hpn, if_neg (hall ⟨p, hpn⟩)]exact ih (by omega)have h := hfalse n (le_refl n)rw [layer_last P] at hexact Bool.noConfusion hobtain ⟨t0, ht0⟩ := hexrefine ⟨t0, ht0, ?_⟩intro s hshave hsF : layer P s.val = false := byrw [layer_castSucc P s.isLt]have h := walk_layer P srw [if_pos hs] at hexact h.1have htF : layer P t0.val = false := byrw [layer_castSucc P t0.isLt]have h := walk_layer P t0rw [if_pos ht0] at hexact h.1have hsT : layer P (s.val + 1) = true := byrw [layer_succ P s.isLt, if_pos hs]have htT : layer P (t0.val + 1) = true := byrw [layer_succ P t0.isLt, if_pos ht0]apply Fin.extby_contra hnercases Nat.lt_or_gt_of_ne hne with hlt | hlt· have hup := layer_up P (s.val + 1) t0.val (by omega) (by omega) hsTrw [htF] at hupexact Bool.noConfusion hup· have hup := layer_up P (t0.val + 1) s.val (by omega) (by omega) htTrw [hsF] at hupexact Bool.noConfusion hup/-- The closed labelled walk produced by a locally admissible word with one empty row. -/noncomputable def wordToWalk (hn : 4 ≤ n) (w : Word n) (hL : Local w) (hS : Single w) :Σ i : Mask, WalkSet n (false, i) (true, i) :=⟨msk w ⟨0, by omega⟩,⟨(fun j => (lay hS j, msk w (toFin (by omega : 0 < n) j)), w),byapply Prod.ext· exact lay_zero hS· show msk w (toFin (by omega : 0 < n) (0 : Fin (n + 1))) = msk w ⟨0, by omega⟩rw [toFin_zero],byapply Prod.ext· exact lay_last hS hn· show msk w (toFin (by omega : 0 < n) ⟨n, Nat.lt_succ_self n⟩)= msk w ⟨0, by omega⟩rw [toFin_last],byintro trw [mem_lbl]have ht : toFin (by omega : 0 < n) t.castSucc = t := toFin_castSucc _ thave hs : toFin (by omega : 0 < n) t.succ = advance t 1 := toFin_succ _ trefine ⟨?_, ?_, ?_⟩· show allowed (msk w (toFin (by omega : 0 < n) t.castSucc)) (w t) = truerw [ht]; exact local_allowed hn w hL t· show (if (w t).val = 4 then lay hS t.castSucc = false ∧ lay hS t.succ = trueelse lay hS t.castSucc = lay hS t.succ)by_cases h4 : (w t).val = 4· have hte : t = emptyPos hS := emptyPos_unique hS h4subst hterw [if_pos h4]exact ⟨(lay_succ_empty hS).2, (lay_succ_empty hS).1⟩· rw [if_neg h4]exact (lay_succ_of_ne hS t (fun hc => h4 (hc ▸ emptyPos_val hS))).symm· show step (msk w (toFin (by omega : 0 < n) t.castSucc)) (w t)= msk w (toFin (by omega : 0 < n) t.succ)rw [ht, hs]exact (msk_step hn w t).symm⟩⟩/-- The layer is `true` exactly when an empty row occurs strictly before it. -/theorem layer_iff (P : WalkSet n (false, i) (true, i)) :∀ j, j ≤ n → (layer P j = true ↔ ∃ t : Fin n, t.val < j ∧ (P.1.2 t).val = 4) := byintro jinduction j with| zero =>intro _constructor· intro hrw [layer_zero P] at hexact Bool.noConfusion h· rintro ⟨t, ht, _⟩exact absurd ht (Nat.not_lt_zero _)| succ p ih =>intro hphave hpn : p < n := by omegarw [layer_succ P hpn]by_cases h4 : (P.1.2 ⟨p, hpn⟩).val = 4· rw [if_pos h4]constructor· intro _; exact ⟨⟨p, hpn⟩, Nat.lt_succ_self p, h4⟩· intro _; rfl· rw [if_neg h4]constructor· intro hobtain ⟨t, ht, hd⟩ := (ih (Nat.le_of_succ_le hp)).mp hexact ⟨t, by omega, hd⟩· rintro ⟨t, ht, hd⟩refine (ih (Nat.le_of_succ_le hp)).mpr ⟨t, ?_, hd⟩rcases Nat.lt_or_eq_of_le (by omega : t.val ≤ p) with hlt | heq· exact hlt· exfalsohave hte : t = ⟨p, hpn⟩ := Fin.ext heqrw [hte] at hdexact h4 hd/-- The layer of a closed walk is the layer of the word it reads. -/theorem layer_lay (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n){j : ℕ} (hj : j < n + 1) :layer P j = lay (walk_single P hn) ⟨j, hj⟩ := byhave hS := walk_single P hnhave hspec : (P.1.2 (emptyPos hS)).val = 4 := emptyPos_val hShave hiff : layer P j = true ↔ (emptyPos hS).val < j := byrw [layer_iff P j (Nat.le_of_lt_succ hj)]constructor· rintro ⟨t, ht, hd⟩have hte : t = emptyPos hS := emptyPos_unique hS hdrw [hte] at htexact ht· intro hexact ⟨emptyPos hS, h, hspec⟩have hv : (emptyPos hS).val < j ∨ ¬ ((emptyPos hS).val < j) := em _rcases hv with hpos | hneg· rw [(hiff).mpr hpos]show true = decide ((emptyPos hS).val < j)rw [decide_eq_true_iff.mpr hpos]· have hfl : layer P j = false := byrcases Bool.eq_false_or_eq_true (layer P j) with h | h· exact absurd (hiff.mp h) hneg· exact hrw [hfl]show false = decide ((emptyPos hS).val < j)rw [decide_eq_false_iff_not.mpr hneg]/-- The starting mask of a closed walk is the mask of its word at position zero. -/theorem walk_mask_start (P : WalkSet n (false, i) (true, i)) (hn : 4 ≤ n) :msk P.1.2 ⟨0, by omega⟩ = i := byhave h := walk_mask_toFin P hn (⟨0, by omega⟩ : Fin (n + 1))have h2 : (P.1.1 (⟨0, by omega⟩ : Fin (n + 1))).2 = i := congrArg Prod.snd P.2.1rw [h2] at hhave e : toFin (by omega : 0 < n) (⟨0, by omega⟩ : Fin (n + 1)) = (⟨0, by omega⟩ : Fin n) :=toFin_zero _rw [e] at hexact h.symm/-- Exactly one empty row, phrased as a cardinality of the empty positions. -/theorem single_iff_emptyCard (w : Word n) :Single w ↔ (Finset.univ.filter (fun i => (w i).val = 4)).card = 1 := byconstructor· rintro ⟨i0, hi0, huniq⟩rw [Finset.card_eq_one]refine ⟨i0, ?_⟩ext jsimp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_singleton]exact ⟨fun hj => huniq j hj, fun hj => by rw [hj]; exact hi0⟩· intro hobtain ⟨i0, hi0⟩ := Finset.card_eq_one.mp hrefine ⟨i0, ?_, ?_⟩· have : i0 ∈ Finset.univ.filter (fun i => (w i).val = 4) := by rw [hi0]; simpsimpa using this· intro j hjhave hj' : j ∈ Finset.univ.filter (fun i => (w i).val = 4) := by simpa using hjrw [hi0] at hj'simpa using hj'/-- The occupied rows and the empty rows of a word split the indices. -/theorem card_occupied_add_empty (w : Word n) :(occupied w).card + (Finset.univ.filter (fun i => (w i).val = 4)).card = n := byset E := Finset.univ.filter (fun i => (w i).val = 4) with hEhave hunion : occupied w ∪ E = (Finset.univ : Finset (Fin n)) := byext isimp only [occupied, hE, Finset.mem_union, Finset.mem_filter, Finset.mem_univ, true_and]constructor· intro _; exact trivial· intro _by_cases h : (w i).val = 4· exact Or.inr h· exact Or.inl (by omega)have hdis : Disjoint (occupied w) E := byrw [Finset.disjoint_left]intro i hi hjsimp only [occupied, hE, Finset.mem_filter, Finset.mem_univ, true_and] at hi hjomegahave h := Finset.card_union_of_disjoint hdisrw [hunion, Finset.card_univ, Fintype.card_fin] at hexact h.symm/-- Exactly one empty row, phrased as a cardinality. -/theorem single_iff_card (w : Word n) : Single w ↔ (occupied w).card + 1 = n := byhave hsplit := card_occupied_add_empty whave hiff := single_iff_emptyCard wconstructor· intro hhave h1 := hiff.mp homega· intro happly hiff.mpromega/-- The rook numbers are the numbers of locally admissible words with one empty row. -/theorem a_eq_words (hn : 4 ≤ n) :a n = Fintype.card {w : Word n // Local w ∧ Single w} := byobtain ⟨m, rfl⟩ : ∃ m, n = m + 1 := ⟨n - 1, by omega⟩have ha : a (m + 1) = (((board (m + 1)).powersetCard m).filter IsRookPlacement).card := rflrw [ha, Fintype.card_subtype]refine Finset.card_bij (fun S _ => wordOf S) ?_ ?_ ?_· intro S hSrw [Finset.mem_filter] at hSobtain ⟨hS, hpl⟩ := hSrw [Finset.mem_filter]have hboard : S ⊆ board (m + 1) := (Finset.mem_powersetCard.mp hS).1have hcard : S.card = m := (Finset.mem_powersetCard.mp hS).2have hinj : ColInj (wordOf S) := byrw [← rook_iff, placement_wordOf hboard hpl]exact hplrefine ⟨Finset.mem_univ _, (local_iff_inj (by omega)).mpr hinj, ?_⟩rw [single_iff_card]rw [← placement_card (wordOf S), placement_wordOf hboard hpl, hcard]· intro S₁ hS₁ S₂ hS₂ heqrw [Finset.mem_filter] at hS₁ hS₂have hb₁ : S₁ ⊆ board (m + 1) := (Finset.mem_powersetCard.mp hS₁.1).1have hb₂ : S₂ ⊆ board (m + 1) := (Finset.mem_powersetCard.mp hS₂.1).1rw [← placement_wordOf hb₁ hS₁.2, ← placement_wordOf hb₂ hS₂.2, heq]· intro w hwrw [Finset.mem_filter] at hwobtain ⟨_, hL, hS⟩ := hwhave hboard : placement w ⊆ board (m + 1) := placement_board (by omega) whave hcol : ColInj w := (local_iff_inj (by omega)).mp hLhave hcard : (placement w).card = m := byrw [placement_card]have h := (single_iff_card w).mp hSomegarefine ⟨placement w, ?_, wordOf_placement (by omega) w hcol⟩rw [Finset.mem_filter]exact ⟨Finset.mem_powersetCard.mpr ⟨hboard, hcard⟩, (rook_iff w).mpr hcol⟩/-- Closed labelled walks, without fixing the endpoint mask. -/abbrev WalkTotal (n : ℕ) : Type :={ P : (Fin (n + 1) → St) × (Fin n → Fin 5) //(∃ i : Mask, P.1 0 = (false, i) ∧ P.1 ⟨n, Nat.lt_succ_self n⟩ = (true, i)) ∧(∀ t : Fin n, P.2 t ∈ lbl (P.1 t.castSucc) (P.1 t.succ)) }instance (n : ℕ) : Fintype (WalkTotal n) := by unfold WalkTotal; infer_instance/-- The endpoint mask is the mask of the first state. -/theorem walkTotal_start (P : WalkTotal n) :P.1.1 ⟨n, Nat.lt_succ_self n⟩ = (true, (P.1.1 0).2) := byobtain ⟨i, h0, hn⟩ := P.2.1have hfst : (P.1.1 0).2 = i := by rw [h0]rw [hfst]exact hn/-- A `WalkTotal` is a closed walk with its endpoint mask fixed. -/noncomputable abbrev walkTotalToSet (P : WalkTotal n) :WalkSet n (false, (P.1.1 0).2) (true, (P.1.1 0).2) :=⟨P.1, byobtain ⟨i, h0, hn⟩ := P.2.1rw [h0], walkTotal_start P, P.2.2⟩theorem walkTotal_local (P : WalkTotal n) (hn : 4 ≤ n) : Local P.1.2 :=walk_local (walkTotalToSet P) hntheorem walkTotal_single (P : WalkTotal n) (hn : 4 ≤ n) : Single P.1.2 :=walk_single (walkTotalToSet P) hn/-- The layer at an index is the first component of the path there. -/theorem layer_of_set (P : WalkSet n (false, i) (true, i)) {j : ℕ} (hj : j < n + 1) :layer P j = (P.1.1 ⟨j, hj⟩).1 := byrw [layer, dif_pos hj]@[simp] theorem wordToWalk_fst (hn : 4 ≤ n) (w : Word n) (hL : Local w) (hS : Single w) :(wordToWalk hn w hL hS).1 = msk w ⟨0, by omega⟩ := rfl@[simp] theorem wordToWalk_path (hn : 4 ≤ n) (w : Word n) (hL : Local w) (hS : Single w) :(wordToWalk hn w hL hS).2.1.1= fun j => (lay hS j, msk w (toFin (by omega : 0 < n) j)) := rfl@[simp] theorem wordToWalk_label (hn : 4 ≤ n) (w : Word n) (hL : Local w) (hS : Single w) :(wordToWalk hn w hL hS).2.1.2 = w := rfl/-- The words with one empty row are exactly the closed labelled walks. -/noncomputable def wordEquivTotal (hn : 4 ≤ n) :{w : Word n // Local w ∧ Single w} ≃ WalkTotal n wheretoFun w :=let W := wordToWalk hn w.1 w.2.1 w.2.2⟨W.2.1, ⟨W.1, W.2.2.1, W.2.2.2.1⟩, W.2.2.2.2⟩invFun P := ⟨P.1.2, walkTotal_local P hn, walkTotal_single P hn⟩left_inv w := byapply Subtype.extrflright_inv P := byapply Subtype.extapply Prod.ext· funext japply Prod.ext· rw [wordToWalk_path]simp only []rw [← layer_lay (walkTotalToSet P) hn j.isLt, layer_of_set (walkTotalToSet P) j.isLt]· rw [wordToWalk_path]simp only []exact (walk_mask_toFin (walkTotalToSet P) hn j).symm· rw [wordToWalk_label]/-- The number of words with one empty row is the number of closed labelled walks. -/theorem card_words_eq_total (hn : 4 ≤ n) :Fintype.card {w : Word n // Local w ∧ Single w} = Fintype.card (WalkTotal n) :=Fintype.card_congr (wordEquivTotal hn)/-- The fiber of the endpoint mask is a fixed-endpoint walk set. -/noncomputable def fiberTotalEquiv (n : ℕ) (i : Mask) :{P : WalkTotal n // (P.1.1 0).2 = i} ≃ WalkSet n (false, i) (true, i) wheretoFun X :=⟨X.1.1, byobtain ⟨i', h0, hn⟩ := X.1.2.1have hi : i' = i := byhave h2 : (X.1.1.1 0).2 = i := X.2rw [h0] at h2exact h2rw [hi] at h0exact h0,byobtain ⟨i', h0, hn⟩ := X.1.2.1have hi : i' = i := byhave h2 : (X.1.1.1 0).2 = i := X.2rw [h0] at h2exact h2rw [hi] at hnexact hn,X.1.2.2⟩invFun Q := ⟨⟨Q.1, ⟨i, Q.2.1, Q.2.2.1⟩, Q.2.2.2⟩, by rw [Q.2.1]⟩left_inv X := byapply Subtype.extapply Subtype.extrflright_inv Q := byapply Subtype.extrfl/-- The closed walks split into the fibers of their endpoint mask. -/theorem card_total_eq_sum (n : ℕ) :Fintype.card (WalkTotal n) = ∑ i : Mask, Fintype.card (WalkSet n (false, i) (true, i)) := byhave h := Finset.card_eq_sum_card_fiberwise(s := (Finset.univ : Finset (WalkTotal n))) (t := (Finset.univ : Finset Mask))(f := fun P : WalkTotal n => (P.1.1 0).2) (fun x _ => Finset.mem_univ _)rw [Finset.card_univ] at hrw [h]refine Finset.sum_congr rfl (fun i _ => ?_)rw [← Fintype.card_subtype]exact Fintype.card_congr (fiberTotalEquiv n i)/-- The rook numbers equal the sum over closed walks. -/theorem a_eq_walks (hn : 4 ≤ n) :a n = ∑ i : Mask, Fintype.card (WalkSet n (false, i) (true, i)) := byrw [a_eq_words hn, card_words_eq_total hn, card_total_eq_sum]/-- The rook numbers are the entries of the transfer matrix power. -/theorem a_eq_matrix (hn : 4 ≤ n) :(a n : ℤ) = ∑ i : Mask,((Matrix.of (fun x y : St => (lbl x y).card) : Matrix St St ℕ) ^ n)(false, i) (true, i) := byrw [a_eq_walks hn]push_castrefine Finset.sum_congr rfl (fun i _ => ?_)rw [card_walkSet,RookPaths.walkCount_eq_pow (fun x y : St => (lbl x y).card) n (false, i) (true, i)]/-- The two-layer state read as an index of `Fin 16`. -/def stToFin (x : St) : Fin 16 :=⟨(if x.1 then 8 else 0) + x.2.val, byhave := x.2.isLtsplit <;> omega⟩theorem stToFin_inj : ∀ p : St × St, stToFin p.1 = stToFin p.2 → p.1 = p.2 := bydecidetheorem stToFin_injective : Function.Injective stToFin := fun x y h => stToFin_inj (x, y) h/-- The two-layer state space is `Fin 16`. -/noncomputable def stEquiv : St ≃ Fin 16 :=Equiv.ofBijective stToFin (byrw [Fintype.bijective_iff_injective_and_card]exact ⟨stToFin_injective, bysimp [Fintype.card_prod, Fintype.card_fin, Fintype.card_bool]⟩)@[simp] theorem stEquiv_apply (x : St) : stEquiv x = stToFin x := rfl/-- The counting weight of the automaton is the literal transfer matrix. -/theorem lbl_card_eq_BZ : ∀ p : St × St, ((lbl p.1 p.2).card : ℤ) = RookRec.BZ (stToFin p.1) (stToFin p.2) := bydecide/-- The integer weight matrix of the automaton. -/def BZint : Matrix St St ℤ := Matrix.of fun x y => ((lbl x y).card : ℤ)theorem BZint_apply (x y : St) : BZint x y = ((lbl x y).card : ℤ) := rfl/-- The powers of the weight matrix are the powers of the literal matrix. -/theorem BZint_pow : ∀ (n : ℕ) (x y : St),(BZint ^ n) x y = (RookRec.BZ ^ n) (stEquiv x) (stEquiv y)| 0, x, y => byrw [pow_zero, pow_zero]simp only [Matrix.one_apply]by_cases h : x = y· subst h; simp· have h2 : ¬ (stToFin x = stToFin y) := fun hc => h (stToFin_injective hc)split_ifs with h3· exact absurd h3 (fun hc => h (stToFin_injective hc))· rfl| n + 1, x, y => byrw [pow_succ', pow_succ', Matrix.mul_apply, Matrix.mul_apply]refine Fintype.sum_equiv stEquiv(fun z : St => BZint x z * (BZint ^ n) z y)(fun w : Fin 16 => RookRec.BZ (stEquiv x) w * (RookRec.BZ ^ n) w (stEquiv y)) ?_intro zrw [BZint_apply, lbl_card_eq_BZ (x, z), BZint_pow n z y]rfl/-- The rook numbers are the transfer matrix sequence. -/theorem a_eq_fZ (hn : 4 ≤ n) : (a n : ℤ) = RookRec.fZ n := byhave hcast : ∀ (k : ℕ) (x y : St),(((Matrix.of (fun x y : St => (lbl x y).card) : Matrix St St ℕ) ^ k) x y : ℤ)= (BZint ^ k) x y := byintro kinduction k with| zero =>intro x yrw [pow_zero, pow_zero, Matrix.one_apply, Matrix.one_apply]by_cases h : x = y· subst h; simp· simp [h]| succ k ih =>intro x yrw [pow_succ', pow_succ', Matrix.mul_apply, Matrix.mul_apply, Nat.cast_sum]refine Finset.sum_congr rfl (fun z _ => ?_)simp only [Matrix.of_apply]rw [Nat.cast_mul, ← BZint_apply x z, ih z y]rw [a_eq_matrix hn, RookRec.fZ_eq]have hleft : (∑ i : Mask,((((Matrix.of (fun x y : St => (lbl x y).card) : Matrix St St ℕ) ^ n)(false, i) (true, i) : ℤ)))= ∑ i : Mask, (BZint ^ n) (false, i) (true, i) :=Finset.sum_congr rfl (fun i _ => hcast n (false, i) (true, i))refine Eq.trans hleft ?_have hphi : RookRec.phi (RookRec.BZ ^ n)= ∑ i : Fin 8, (RookRec.BZ ^ n) ⟨i.val, by omega⟩ ⟨i.val + 8, by omega⟩ := rflrw [hphi]refine Finset.sum_congr rfl (fun i _ => ?_)rw [BZint_pow]have h1 : stEquiv ((false, i) : St) = (⟨i.val, by omega⟩ : Fin 16) := byapply Fin.extsimp [stEquiv_apply, stToFin]have h2 : stEquiv ((true, i) : St) = (⟨i.val + 8, by omega⟩ : Fin 16) := byapply Fin.extsimp [stEquiv_apply, stToFin]omegarw [h1, h2]end WalkToWordend RookCount/-!# The transfer-matrix sequence satisfies the quadratic recurrence`fZ` obeys the linear recurrence of `(t-1)^3 (t^3-t^2-t-1)^4`; from its firstfifteen values we get the values up to index 25, and then the `Q`-residual`e n = ∑ k < 12, q k * fZ (n-k)` vanishes for `n ≥ 11`.-/set_option maxRecDepth 10000set_option maxHeartbeats 0open scoped BigOperatorsnamespace RookFinalopen RookCount RookRec/-- The coefficients of `Q = (1-X)^2 (1-X-X^2-X^3)^3`. -/def qc : ℕ → ℤ| 0 => 1| 1 => -5| 2 => 7| 3 => -1| 4 => 2| 5 => -10| 6 => 2| 7 => 2| 8 => 5| 9 => -1| 10 => -1| 11 => -1| _ => 0/-- The first fifteen values of `fZ`. -/def vec : Fin 15 → ℤ :=![0, 1, 8, 33, 96, 265, 672, 1617, 3776, 8577, 19080, 41745, 90048, 191945, 404992]theorem fZ_vec : ∀ k : Fin 15, RookRec.fZ k.val = vec k := byintro kfin_cases k <;> decide +kernel/-- `fZ` at a small index. -/theorem fZ_small (k : ℕ) (hk : k < 15) : RookRec.fZ k = vec ⟨k, hk⟩ :=fZ_vec ⟨k, hk⟩/-- The values up to index 25. -/theorem fZ_15 : RookRec.fZ 15 = 846945 := byhave h := RookRec.fZ_rec 0simp only [Nat.zero_add] at hrw [fZ_small 0 (by omega), fZ_small 1 (by omega), fZ_small 2 (by omega),fZ_small 3 (by omega), fZ_small 4 (by omega), fZ_small 5 (by omega),fZ_small 6 (by omega), fZ_small 7 (by omega), fZ_small 8 (by omega),fZ_small 9 (by omega), fZ_small 10 (by omega), fZ_small 11 (by omega),fZ_small 12 (by omega), fZ_small 13 (by omega), fZ_small 14 (by omega)] at hsimp only [vec] at hnorm_num at hexact h/-- The values of `fZ`, extended by the recurrence of `(t-1)^3 (t^3-t^2-t-1)^4`. -/def fzv : ℕ → ℤ| 0 => 0| 1 => 1| 2 => 8| 3 => 33| 4 => 96| 5 => 265| 6 => 672| 7 => 1617| 8 => 3776| 9 => 8577| 10 => 19080| 11 => 41745| 12 => 90048| 13 => 191945| 14 => 404992| n + 15 =>fzv n + fzv (n + 1) + fzv (n + 2) - 7 * fzv (n + 3) - 3 * fzv (n + 4)- 3 * fzv (n + 5) + 21 * fzv (n + 6) - 3 * fzv (n + 7) + 3 * fzv (n + 8)- 29 * fzv (n + 9) + 19 * fzv (n + 10) - 5 * fzv (n + 11) + 15 * fzv (n + 12)- 17 * fzv (n + 13) + 7 * fzv (n + 14)/-- The two tables of initial values agree. -/theorem vec_eq_fzv : ∀ k : Fin 15, vec k = fzv k.val := byintro kfin_cases k <;> decide +kernel/-- The values of `fZ` up to index 25. -/theorem fZ_eq_fzv : ∀ n, n ≤ 25 → RookRec.fZ n = fzv n := byintro ninduction n using Nat.strong_induction_on with| _ n ih =>intro hnrcases Nat.lt_or_ge n 15 with h | h· rw [fZ_vec ⟨n, h⟩, vec_eq_fzv ⟨n, h⟩]· obtain ⟨m, rfl⟩ : ∃ m, n = m + 15 := ⟨n - 15, by omega⟩rw [RookRec.fZ_rec m]show _ = fzv m + fzv (m + 1) + fzv (m + 2) - 7 * fzv (m + 3) - 3 * fzv (m + 4)- 3 * fzv (m + 5) + 21 * fzv (m + 6) - 3 * fzv (m + 7) + 3 * fzv (m + 8)- 29 * fzv (m + 9) + 19 * fzv (m + 10) - 5 * fzv (m + 11) + 15 * fzv (m + 12)- 17 * fzv (m + 13) + 7 * fzv (m + 14)rw [← ih m (by omega) (by omega), ← ih (m + 1) (by omega) (by omega),← ih (m + 2) (by omega) (by omega), ← ih (m + 3) (by omega) (by omega),← ih (m + 4) (by omega) (by omega), ← ih (m + 5) (by omega) (by omega),← ih (m + 6) (by omega) (by omega), ← ih (m + 7) (by omega) (by omega),← ih (m + 8) (by omega) (by omega), ← ih (m + 9) (by omega) (by omega),← ih (m + 10) (by omega) (by omega), ← ih (m + 11) (by omega) (by omega),← ih (m + 12) (by omega) (by omega), ← ih (m + 13) (by omega) (by omega),← ih (m + 14) (by omega) (by omega)]/-- The recurrence coefficients of `fZ`, from `(t-1)^3 (t^3-t^2-t-1)^4`. -/def cc : ℕ → ℤ| 0 => 1| 1 => 1| 2 => 1| 3 => -7| 4 => -3| 5 => -3| 6 => 21| 7 => -3| 8 => 3| 9 => -29| 10 => 19| 11 => -5| 12 => 15| 13 => -17| 14 => 7| _ => 0/-- The recurrence for `fZ`, in `Finset.range` form. -/theorem fZ_step (n : ℕ) :RookRec.fZ (n + 15) = ∑ j ∈ Finset.range 15, cc j * RookRec.fZ (n + j) := byrw [RookRec.fZ_rec]simp only [Finset.sum_range_succ, Finset.sum_range_zero, cc]ring/-- The `Q`-residual of the sequence. -/def e (n : ℕ) : ℤ := ∑ k ∈ Finset.range 12, qc k * RookRec.fZ (n - k)/-- The residual satisfies the same recurrence, from index 11 on. -/theorem e_rec {n : ℕ} (hn : 11 ≤ n) :e (n + 15) = ∑ j ∈ Finset.range 15, cc j * e (n + j) := byhave hstep : ∀ k ∈ Finset.range 12,RookRec.fZ (n + 15 - k) = ∑ j ∈ Finset.range 15, cc j * RookRec.fZ (n - k + j) := byintro k hkrw [Finset.mem_range] at hkrw [show n + 15 - k = (n - k) + 15 by omega, fZ_step]calc e (n + 15)= ∑ k ∈ Finset.range 12, qc k * ∑ j ∈ Finset.range 15,cc j * RookRec.fZ (n - k + j) := byrefine Finset.sum_congr rfl (fun k hk => ?_)rw [hstep k hk]_ = ∑ k ∈ Finset.range 12, ∑ j ∈ Finset.range 15,qc k * (cc j * RookRec.fZ (n - k + j)) := bysimp only [Finset.mul_sum]_ = ∑ j ∈ Finset.range 15, ∑ k ∈ Finset.range 12,qc k * (cc j * RookRec.fZ (n - k + j)) := byrw [Finset.sum_comm]_ = ∑ j ∈ Finset.range 15, cc j * ∑ k ∈ Finset.range 12,qc k * RookRec.fZ (n - k + j) := byrefine Finset.sum_congr rfl (fun j hj => ?_)rw [Finset.mul_sum]exact Finset.sum_congr rfl (fun k _ => by ring)_ = ∑ j ∈ Finset.range 15, cc j * e (n + j) := byrefine Finset.sum_congr rfl (fun j hj => ?_)congr 1refine Finset.sum_congr rfl (fun k hk => ?_)rw [Finset.mem_range] at hkrw [show n - k + j = n + j - k by omega]/-- The residual vanishes. -/theorem e_zero : ∀ n, 11 ≤ n → e n = 0 := byhave hbase : ∀ n, 11 ≤ n → n ≤ 25 → e n = 0 := byintro n h1 h2have hsub : e n = ∑ k ∈ Finset.range 12, qc k * fzv (n - k) := bysimp only [e]refine Finset.sum_congr rfl (fun k hk => ?_)rw [Finset.mem_range] at hkrw [fZ_eq_fzv (n - k) (by omega)]rw [hsub]interval_cases n <;> norm_num [Finset.sum_range_succ, qc, fzv]intro ninduction n using Nat.strong_induction_on with| _ n ih =>intro hnrcases Nat.lt_or_ge n 26 with h | h· exact hbase n hn (by omega)· have hrec := e_rec (n := n - 15) (by omega)rw [show n - 15 + 15 = n by omega] at hrecrw [hrec]refine Finset.sum_eq_zero (fun j hj => ?_)rw [Finset.mem_range] at hjrw [ih (n - 15 + j) (by omega) (by omega), mul_zero]end RookFinal/-!# The generating function of the rook numbers`Q F = W` where `F = Σ fZ(n) X^n`, `Q = (1-X)^2 (1-X-X^2-X^3)^3` and `W` is theexplicit degree-ten polynomial `Q(X + 8X^2) + X^3 P`.-/set_option maxRecDepth 10000set_option maxHeartbeats 0open scoped BigOperatorsnamespace RookMainopen RookCount RookRec RookFinal/-- The extension values. -/theorem fzv_0 : fzv 0 = 0 := rfltheorem fzv_1 : fzv 1 = 1 := rfltheorem fzv_2 : fzv 2 = 8 := rfltheorem fzv_3 : fzv 3 = 33 := rfltheorem fzv_4 : fzv 4 = 96 := rfltheorem fzv_5 : fzv 5 = 265 := rfltheorem fzv_6 : fzv 6 = 672 := rfltheorem fzv_7 : fzv 7 = 1617 := rfltheorem fzv_8 : fzv 8 = 3776 := rfltheorem fzv_9 : fzv 9 = 8577 := rfltheorem fzv_10 : fzv 10 = 19080 := rfltheorem fzv_11 : fzv 11 = 41745 := rfltheorem fzv_12 : fzv 12 = 90048 := rfltheorem fzv_13 : fzv 13 = 191945 := rfltheorem fzv_14 : fzv 14 = 404992 := rfltheorem fzv_15 : fzv 15 = 846945 := rfltheorem fzv_16 : fzv 16 = 1757440 := rfltheorem fzv_17 : fzv 17 = 3621697 := rfltheorem fzv_18 : fzv 18 = 7417800 := rfltheorem fzv_19 : fzv 19 = 15109313 := rfltheorem fzv_20 : fzv 20 = 30623520 := rfltheorem fzv_21 : fzv 21 = 61788489 := rfltheorem fzv_22 : fzv 22 = 124158496 := rfltheorem fzv_23 : fzv 23 = 248549937 := rfltheorem fzv_24 : fzv 24 = 495852480 := rfltheorem fzv_25 : fzv 25 = 986076225 := rfl/-- The values of `fZ` as explicit integers. -/theorem fZv_0 : RookRec.fZ 0 = 0 := byrw [fZ_eq_fzv 0 (by omega)]exact fzv_0theorem fZv_1 : RookRec.fZ 1 = 1 := byrw [fZ_eq_fzv 1 (by omega)]exact fzv_1theorem fZv_2 : RookRec.fZ 2 = 8 := byrw [fZ_eq_fzv 2 (by omega)]exact fzv_2theorem fZv_3 : RookRec.fZ 3 = 33 := byrw [fZ_eq_fzv 3 (by omega)]exact fzv_3theorem fZv_4 : RookRec.fZ 4 = 96 := byrw [fZ_eq_fzv 4 (by omega)]exact fzv_4theorem fZv_5 : RookRec.fZ 5 = 265 := byrw [fZ_eq_fzv 5 (by omega)]exact fzv_5theorem fZv_6 : RookRec.fZ 6 = 672 := byrw [fZ_eq_fzv 6 (by omega)]exact fzv_6theorem fZv_7 : RookRec.fZ 7 = 1617 := byrw [fZ_eq_fzv 7 (by omega)]exact fzv_7theorem fZv_8 : RookRec.fZ 8 = 3776 := byrw [fZ_eq_fzv 8 (by omega)]exact fzv_8theorem fZv_9 : RookRec.fZ 9 = 8577 := byrw [fZ_eq_fzv 9 (by omega)]exact fzv_9theorem fZv_10 : RookRec.fZ 10 = 19080 := byrw [fZ_eq_fzv 10 (by omega)]exact fzv_10theorem fZv_11 : RookRec.fZ 11 = 41745 := byrw [fZ_eq_fzv 11 (by omega)]exact fzv_11theorem fZv_12 : RookRec.fZ 12 = 90048 := byrw [fZ_eq_fzv 12 (by omega)]exact fzv_12theorem fZv_13 : RookRec.fZ 13 = 191945 := byrw [fZ_eq_fzv 13 (by omega)]exact fzv_13theorem fZv_14 : RookRec.fZ 14 = 404992 := byrw [fZ_eq_fzv 14 (by omega)]exact fzv_14theorem fZv_15 : RookRec.fZ 15 = 846945 := byrw [fZ_eq_fzv 15 (by omega)]exact fzv_15theorem fZv_16 : RookRec.fZ 16 = 1757440 := byrw [fZ_eq_fzv 16 (by omega)]exact fzv_16theorem fZv_17 : RookRec.fZ 17 = 3621697 := byrw [fZ_eq_fzv 17 (by omega)]exact fzv_17theorem fZv_18 : RookRec.fZ 18 = 7417800 := byrw [fZ_eq_fzv 18 (by omega)]exact fzv_18theorem fZv_19 : RookRec.fZ 19 = 15109313 := byrw [fZ_eq_fzv 19 (by omega)]exact fzv_19theorem fZv_20 : RookRec.fZ 20 = 30623520 := byrw [fZ_eq_fzv 20 (by omega)]exact fzv_20theorem fZv_21 : RookRec.fZ 21 = 61788489 := byrw [fZ_eq_fzv 21 (by omega)]exact fzv_21theorem fZv_22 : RookRec.fZ 22 = 124158496 := byrw [fZ_eq_fzv 22 (by omega)]exact fzv_22theorem fZv_23 : RookRec.fZ 23 = 248549937 := byrw [fZ_eq_fzv 23 (by omega)]exact fzv_23theorem fZv_24 : RookRec.fZ 24 = 495852480 := byrw [fZ_eq_fzv 24 (by omega)]exact fzv_24theorem fZv_25 : RookRec.fZ 25 = 986076225 := byrw [fZ_eq_fzv 25 (by omega)]exact fzv_25/-- `Q = (1-X)^2 (1-X-X^2-X^3)^3` as a power series. -/theorem monomial_eq_C_mul_X (k : ℕ) (a : ℤ) :PowerSeries.monomial k a = PowerSeries.C a * PowerSeries.X ^ k := byext nrw [PowerSeries.coeff_monomial, PowerSeries.coeff_C_mul, PowerSeries.coeff_X_pow,mul_ite, mul_one, mul_zero]theorem coeff_C_mul_X_pow (a : ℤ) (k n : ℕ) :PowerSeries.coeff (R := ℤ) n (PowerSeries.C a * PowerSeries.X ^ k)= if n = k then a else 0 := byrw [PowerSeries.coeff_C_mul, PowerSeries.coeff_X_pow]by_cases h : n = k <;> simp [h]theorem coeff_nat_mul_X_pow (c : ℕ) (k n : ℕ) :PowerSeries.coeff (R := ℤ) n ((c : PowerSeries ℤ) * PowerSeries.X ^ k)= if n = k then (c : ℤ) else 0 := byhave hc : ((c : PowerSeries ℤ)) = PowerSeries.C (c : ℤ) := rflrw [hc, PowerSeries.coeff_C_mul, PowerSeries.coeff_X_pow]by_cases h : n = k <;> simp [h]noncomputable def Qser : PowerSeries ℤ :=∑ k ∈ Finset.range 12, PowerSeries.monomial k (qc k)noncomputable def Fser : PowerSeries ℤ := PowerSeries.mk fun n => RookRec.fZ n/-- The coefficients of `P`. -/def pc : ℕ → ℤ| 0 => 33| 1 => -69| 2 => 16| 3 => -14| 4 => 82| 5 => -8| 6 => -20| 7 => -38| 8 => 9| 9 => 9| 10 => 8| _ => 0noncomputable def Pser : PowerSeries ℤ :=∑ k ∈ Finset.range 11, PowerSeries.monomial k (pc k)noncomputable def Wser : PowerSeries ℤ := PowerSeries.mk fun n =>if n = 1 then 1 else if n = 2 then 3 else if n = 4 then -14 else if n = 5 then 10else if n = 6 then -8 else if n = 7 then 4 else if n = 8 then 10else if n = 9 then 1 else if n = 10 then 1 else 0theorem coeff_Wser (n : ℕ) : PowerSeries.coeff (R := ℤ) n Wser =(if n = 1 then 1 else if n = 2 then 3 else if n = 4 then -14 else if n = 5 then 10else if n = 6 then -8 else if n = 7 then 4 else if n = 8 then 10else if n = 9 then 1 else if n = 10 then 1 else 0) := byrw [Wser, PowerSeries.coeff_mk]theorem coeff_Fser (n : ℕ) : PowerSeries.coeff (R := ℤ) n Fser = RookRec.fZ n := bysimp [Fser]theorem pc_zero : ∀ k, 11 ≤ k → pc k = 0 := byintro k hkrcases k with _|_|_|_|_|_|_|_|_|_|_|m· omega· omega· omega· omega· omega· omega· omega· omega· omega· omega· omega· rfltheorem qc_zero : ∀ k, 12 ≤ k → qc k = 0 := byintro k hkrcases k with _|_|_|_|_|_|_|_|_|_|_|_|m· omega· omega· omega· omega· omega· omega· omega· omega· omega· omega· omega· omega· rfltheorem coeff_Qser (n : ℕ) : PowerSeries.coeff (R := ℤ) n Qser = qc n := byby_cases hn : n < 12· have h1 : ∀ k ∈ Finset.range 12, k ≠ n →PowerSeries.coeff (R := ℤ) n (PowerSeries.monomial k (qc k)) = 0 := byintro k hk hnerw [PowerSeries.coeff_monomial, if_neg (Ne.symm hne)]have h2 : n ∉ Finset.range 12 →PowerSeries.coeff (R := ℤ) n (PowerSeries.monomial n (qc n)) = 0 := byintro hmemrw [Finset.mem_range] at hmemomegacalc PowerSeries.coeff (R := ℤ) n Qser= ∑ k ∈ Finset.range 12,PowerSeries.coeff (R := ℤ) n (PowerSeries.monomial k (qc k)) := byrw [Qser, map_sum]_ = PowerSeries.coeff (R := ℤ) n (PowerSeries.monomial n (qc n)) :=Finset.sum_eq_single n h1 h2_ = qc n := by rw [PowerSeries.coeff_monomial, if_pos rfl]· have hq := qc_zero n (by omega)rw [hq]have h1 : ∀ k ∈ Finset.range 12,PowerSeries.coeff (R := ℤ) n (PowerSeries.monomial k (qc k)) = 0 := byintro k hkrw [Finset.mem_range] at hkrw [PowerSeries.coeff_monomial, if_neg (by omega)]rw [Qser, map_sum, Finset.sum_eq_zero h1]theorem coeff_Pser (n : ℕ) : PowerSeries.coeff (R := ℤ) n Pser = pc n := byby_cases hn : n < 11· have h1 : ∀ k ∈ Finset.range 11, k ≠ n →PowerSeries.coeff (R := ℤ) n (PowerSeries.monomial k (pc k)) = 0 := byintro k hk hnerw [PowerSeries.coeff_monomial, if_neg (Ne.symm hne)]have h2 : n ∉ Finset.range 11 →PowerSeries.coeff (R := ℤ) n (PowerSeries.monomial n (pc n)) = 0 := byintro hmemrw [Finset.mem_range] at hmemomegacalc PowerSeries.coeff (R := ℤ) n Pser= ∑ k ∈ Finset.range 11,PowerSeries.coeff (R := ℤ) n (PowerSeries.monomial k (pc k)) := byrw [Pser, map_sum]_ = PowerSeries.coeff (R := ℤ) n (PowerSeries.monomial n (pc n)) :=Finset.sum_eq_single n h1 h2_ = pc n := by rw [PowerSeries.coeff_monomial, if_pos rfl]· have hp := pc_zero n (by omega)rw [hp]have h1 : ∀ k ∈ Finset.range 11,PowerSeries.coeff (R := ℤ) n (PowerSeries.monomial k (pc k)) = 0 := byintro k hkrw [Finset.mem_range] at hkrw [PowerSeries.coeff_monomial, if_neg (by omega)]rw [Pser, map_sum, Finset.sum_eq_zero h1]theorem coeff_Qser_mul_X (n : ℕ) :PowerSeries.coeff (R := ℤ) n (Qser * PowerSeries.X)= if 1 ≤ n then qc (n - 1) else 0 := byhave hX : Qser * PowerSeries.X = Qser * PowerSeries.X ^ 1 := by rw [pow_one]rw [hX, PowerSeries.coeff_mul_X_pow']split_ifs with h· rw [coeff_Qser]· rfltheorem coeff_Qser_mul_X2 (n : ℕ) :PowerSeries.coeff (R := ℤ) n (Qser * PowerSeries.X ^ 2)= if 2 ≤ n then qc (n - 2) else 0 := byrw [PowerSeries.coeff_mul_X_pow']split_ifs with h· rw [coeff_Qser]· rfltheorem coeff_X3_mul_Pser (n : ℕ) :PowerSeries.coeff (R := ℤ) n (PowerSeries.X ^ 3 * Pser)= if 3 ≤ n then pc (n - 3) else 0 := byrw [PowerSeries.coeff_X_pow_mul']split_ifs with h· rw [coeff_Pser]· rfltheorem Wser_eq :Wser = Qser * (PowerSeries.X + 8 * PowerSeries.X ^ 2)+ PowerSeries.X ^ 3 * Pser := byext nshow PowerSeries.coeff (R := ℤ) n Wser= PowerSeries.coeff (R := ℤ) n(Qser * (PowerSeries.X + PowerSeries.C 8 * PowerSeries.X ^ 2) + PowerSeries.X ^ 3 * Pser)rw [coeff_Wser, map_add, mul_add, map_add]rw [show Qser * (PowerSeries.C 8 * PowerSeries.X ^ 2)= PowerSeries.C 8 * (Qser * PowerSeries.X ^ 2) from by ring]rw [PowerSeries.coeff_C_mul, coeff_Qser_mul_X, coeff_Qser_mul_X2, coeff_X3_mul_Pser]rcases Nat.lt_or_ge n 14 with h | h· interval_cases n <;> norm_num [qc, pc]· have h1 : n ≠ 1 := by omegahave h2 : n ≠ 2 := by omegahave h3 : n ≠ 3 := by omegahave h4 : n ≠ 4 := by omegahave h5 : n ≠ 5 := by omegahave h6 : n ≠ 6 := by omegahave h7 : n ≠ 7 := by omegahave h8 : n ≠ 8 := by omegahave h9 : n ≠ 9 := by omegahave h10 : n ≠ 10 := by omegahave hq1 : qc (n - 1) = 0 := qc_zero (n - 1) (by omega)have hq2 : qc (n - 2) = 0 := qc_zero (n - 2) (by omega)have hp3 : pc (n - 3) = 0 := pc_zero (n - 3) (by omega)simp [h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, hq1, hq2, hp3]theorem coeff_Qser_mul (n : ℕ) : PowerSeries.coeff (R := ℤ) n (Qser * Fser) = e n := bysimp only [Qser, e, Finset.sum_mul, map_sum, monomial_eq_C_mul_X]refine Finset.sum_congr rfl (fun k hk => ?_)rw [Finset.mem_range] at hkrw [mul_assoc, PowerSeries.coeff_C_mul, PowerSeries.coeff_X_pow_mul', coeff_Fser]by_cases hkn : k ≤ n· rw [if_pos hkn]· rw [if_neg hkn, mul_zero, show n - k = 0 by omega, fZv_0]ringtheorem coeff_Wser_zero {n : ℕ} (hn : 11 ≤ n) : PowerSeries.coeff (R := ℤ) n Wser = 0 := byrw [coeff_Wser]have h1 : n ≠ 1 := by omegahave h2 : n ≠ 2 := by omegahave h4 : n ≠ 4 := by omegahave h5 : n ≠ 5 := by omegahave h6 : n ≠ 6 := by omegahave h7 : n ≠ 7 := by omegahave h8 : n ≠ 8 := by omegahave h9 : n ≠ 9 := by omegahave h10 : n ≠ 10 := by omegasimp [h1, h2, h4, h5, h6, h7, h8, h9, h10]theorem e_small : ∀ n, n ≤ 10 → e n = PowerSeries.coeff (R := ℤ) n Wser := byintro n hninterval_cases n <;>simp only [e, Finset.sum_range_succ, Finset.sum_range_zero, qc, coeff_Wser] <;>(try rw [fZv_0]) <;> (try rw [fZv_1]) <;> (try rw [fZv_2]) <;> (try rw [fZv_3]) <;> (try rw [fZv_4]) <;> (try rw [fZv_5]) <;> (try rw [fZv_6]) <;> (try rw [fZv_7]) <;> (try rw [fZv_8]) <;> (try rw [fZv_9]) <;> (try rw [fZv_10]) <;>norm_numtheorem Qser_mul_Fser : Qser * Fser = Wser := byext nrw [coeff_Qser_mul]by_cases hn : n ≤ 10· exact e_small n hn· rw [e_zero n (by omega), coeff_Wser_zero (by omega)]theorem a_zero : OMA.S_oeis_a005777.a 0 = 0 := rfltheorem a_one : OMA.S_oeis_a005777.a 1 = 1 := by decidetheorem a_two : OMA.S_oeis_a005777.a 2 = 4 := by decidetheorem a_three : OMA.S_oeis_a005777.a 3 = 18 := by decide/-- The generating function of the rook numbers is a shift of `F`. -/theorem mk_a :PowerSeries.mk (fun n => (OMA.S_oeis_a005777.a n : ℤ))= Fser - 4 * PowerSeries.X ^ 2 - 15 * PowerSeries.X ^ 3 := byext nshow PowerSeries.coeff (R := ℤ) n (PowerSeries.mk fun n => (OMA.S_oeis_a005777.a n : ℤ))= PowerSeries.coeff (R := ℤ) n(Fser - PowerSeries.C 4 * PowerSeries.X ^ 2 - PowerSeries.C 15 * PowerSeries.X ^ 3)rw [PowerSeries.coeff_mk, map_sub, map_sub, PowerSeries.coeff_C_mul,PowerSeries.coeff_C_mul, PowerSeries.coeff_X_pow, PowerSeries.coeff_X_pow, coeff_Fser]rcases Nat.lt_or_ge n 4 with h | h· interval_cases n <;>simp [a_zero, a_one, a_two, a_three, fZv_0, fZv_1, fZv_2, fZv_3]· rw [if_neg (by omega), if_neg (by omega), a_eq_fZ (by omega)]norm_num/-- The generating function of the rook numbers satisfies the conjectured identity. -/theorem main_identity :(PowerSeries.mk (fun n => (OMA.S_oeis_a005777.a n : ℤ))- (PowerSeries.X + 4 * PowerSeries.X ^ 2 - 15 * PowerSeries.X ^ 3)) * Qser= PowerSeries.X ^ 3 * Pser := byrw [mk_a]show (Fser - 4 * PowerSeries.X ^ 2 - 15 * PowerSeries.X ^ 3- (PowerSeries.X + 4 * PowerSeries.X ^ 2 - 15 * PowerSeries.X ^ 3)) * Qser= PowerSeries.X ^ 3 * Pserhave key : (Fser - 4 * PowerSeries.X ^ 2 - 15 * PowerSeries.X ^ 3- (PowerSeries.X + 4 * PowerSeries.X ^ 2 - 15 * PowerSeries.X ^ 3)) * Qser= Qser * Fser - Qser * (PowerSeries.X + 8 * PowerSeries.X ^ 2) := byringrw [key, Qser_mul_Fser, Wser_eq]ringtheorem Qser_eq :Qser = (1 - PowerSeries.X) ^ 2 * (1 - PowerSeries.X - PowerSeries.X ^ 2 - PowerSeries.X ^ 3) ^ 3 := byhave q0 : (PowerSeries.C (1) : PowerSeries ℤ) = 1 := by simphave q1 : (PowerSeries.C (-5) : PowerSeries ℤ) = -5 := by simphave q2 : (PowerSeries.C (7) : PowerSeries ℤ) = 7 := by simphave q3 : (PowerSeries.C (-1) : PowerSeries ℤ) = -1 := by simphave q4 : (PowerSeries.C (2) : PowerSeries ℤ) = 2 := by simphave q5 : (PowerSeries.C (-10) : PowerSeries ℤ) = -10 := by simphave q6 : (PowerSeries.C (2) : PowerSeries ℤ) = 2 := by simphave q7 : (PowerSeries.C (2) : PowerSeries ℤ) = 2 := by simphave q8 : (PowerSeries.C (5) : PowerSeries ℤ) = 5 := by simphave q9 : (PowerSeries.C (-1) : PowerSeries ℤ) = -1 := by simphave q10 : (PowerSeries.C (-1) : PowerSeries ℤ) = -1 := by simphave q11 : (PowerSeries.C (-1) : PowerSeries ℤ) = -1 := by simpsimp only [Qser, Finset.sum_range_succ, Finset.sum_range_zero, qc, monomial_eq_C_mul_X,q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11]ringtheorem Pser_eq :Pser = 33 - 69 * PowerSeries.X + 16 * PowerSeries.X ^ 2 - 14 * PowerSeries.X ^ 3+ 82 * PowerSeries.X ^ 4 - 8 * PowerSeries.X ^ 5 - 20 * PowerSeries.X ^ 6- 38 * PowerSeries.X ^ 7 + 9 * PowerSeries.X ^ 8 + 9 * PowerSeries.X ^ 9+ 8 * PowerSeries.X ^ 10 := byhave p0 : (PowerSeries.C (33) : PowerSeries ℤ) = 33 := by simphave p1 : (PowerSeries.C (-69) : PowerSeries ℤ) = -69 := by simphave p2 : (PowerSeries.C (16) : PowerSeries ℤ) = 16 := by simphave p3 : (PowerSeries.C (-14) : PowerSeries ℤ) = -14 := by simphave p4 : (PowerSeries.C (82) : PowerSeries ℤ) = 82 := by simphave p5 : (PowerSeries.C (-8) : PowerSeries ℤ) = -8 := by simphave p6 : (PowerSeries.C (-20) : PowerSeries ℤ) = -20 := by simphave p7 : (PowerSeries.C (-38) : PowerSeries ℤ) = -38 := by simphave p8 : (PowerSeries.C (9) : PowerSeries ℤ) = 9 := by simphave p9 : (PowerSeries.C (9) : PowerSeries ℤ) = 9 := by simphave p10 : (PowerSeries.C (8) : PowerSeries ℤ) = 8 := by simpsimp only [Pser, Finset.sum_range_succ, Finset.sum_range_zero, pc, monomial_eq_C_mul_X,p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10]ring/-- The conjecture of Kotesovec (2016). -/theorem the_conjecture :(PowerSeries.mk (fun n => (OMA.S_oeis_a005777.a n : ℤ))- (PowerSeries.X + 4 * PowerSeries.X ^ 2 - 15 * PowerSeries.X ^ 3))* ((1 - PowerSeries.X) ^ 2* (1 - PowerSeries.X - PowerSeries.X ^ 2 - PowerSeries.X ^ 3) ^ 3)= PowerSeries.X ^ 3* (33 - 69 * PowerSeries.X + 16 * PowerSeries.X ^ 2 - 14 * PowerSeries.X ^ 3+ 82 * PowerSeries.X ^ 4 - 8 * PowerSeries.X ^ 5 - 20 * PowerSeries.X ^ 6- 38 * PowerSeries.X ^ 7 + 9 * PowerSeries.X ^ 8 + 9 * PowerSeries.X ^ 9+ 8 * PowerSeries.X ^ 10) := byrw [← Qser_eq, ← Pser_eq]exact main_identityend RookMainnamespace OMA.S_oeis_a005777open PowerSeries in/-- Kotesovec's 2016 conjectured generating function for A005777. -/theorem main :(PowerSeries.mk (fun n : ℕ => (a n : ℤ)) -(X + 4 * X ^ 2 - 15 * X ^ 3)) *((1 - X) ^ 2 * (1 - X - X ^ 2 - X ^ 3) ^ 3) =X ^ 3 * (33 - 69 * X + 16 * X ^ 2 - 14 * X ^ 3 + 82 * X ^ 4 - 8 * X ^ 5 -20 * X ^ 6 - 38 * X ^ 7 + 9 * X ^ 8 + 9 * X ^ 9 + 8 * X ^ 10) := byrw [← RookMain.Qser_eq, ← RookMain.Pser_eq]exact RookMain.main_identityend OMA.S_oeis_a005777