Lean / Reader
Lean readerBoundary.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports OMAReductions.R_oeis_a401670_R1, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
import OMAReductions.R_oeis_a401670_R1/-! A direct free-square block construction for the first limit in A401670. -/namespace KingDimeropen OMA.S_oeis_a401670 Filteropen scoped Topologyabbrev Matching (n : ℕ) := {mate : Cell n → Cell n // Function.Involutive mate ∧∀ u, mate u ≠ u ∧ Near false n (mate u).1 u.1 ∧ Near false n (mate u).2 u.2}def flip (n : ℕ) (i : Fin (2*n)) : Fin (2*n) :=⟨if i.val % 2 = 0 then i.val + 1 else i.val - 1, by split <;> omega⟩@[simp] lemma flip_flip (n : ℕ) (i : Fin (2*n)) : flip n (flip n i) = i := byapply Fin.extsimp only [flip]split_ifs <;> omegalemma flip_ne (n : ℕ) (i : Fin (2*n)) : flip n i ≠ i := byintro hhave h' := congrArg Fin.val hsimp only [flip] at h'split_ifs at h' <;> omegalemma flip_dist (n : ℕ) (i : Fin (2*n)) : Nat.dist (flip n i).val i.val ≤ 1 := bysimp only [flip, Nat.dist]split_ifs <;> omegaabbrev Axis (n k : ℕ) := (Fin (n/k) × Fin (2*k)) ⊕ Fin (2*(n%k))def axis (n k : ℕ) : Axis n k ≃ Fin (2*n) :=((Equiv.sumCongr finProdFinEquiv (Equiv.refl _)).trans finSumFinEquiv).trans(finCongr (by have h := Nat.mod_add_div n k; nlinarith))lemma axis_left (n k : ℕ) (b : Fin (n/k)) (v : Fin (2*k)) :((axis n k) (.inl (b, v))).val = v.val + (2*k)*b.val := rfllemma axis_right (n k : ℕ) (w : Fin (2*(n%k))) :((axis n k) (.inr w)).val = (n/k)*(2*k) + w.val := rflabbrev Block (n k : ℕ) := Fin (n/k) × Fin (n/k)def rawMate {n k : ℕ} (tiles : Block n k → Matching k) :Axis n k × Axis n k → Axis n k × Axis n k| (.inl (a, x), .inl (b, y)) =>let p := (tiles (a,b)).val (x,y)(.inl (a,p.1), .inl (b,p.2))| (.inr x, .inl (b, y)) => (.inr x, .inl (b,flip k y))| (.inl x, .inr y) => (.inl x, .inr (flip (n%k) y))| (.inr x, .inr y) => (.inr x, .inr (flip (n%k) y))lemma raw_involutive {n k : ℕ} (tiles : Block n k → Matching k) :Function.Involutive (rawMate tiles) := byrintro ⟨⟨a,x⟩ | x, ⟨b,y⟩ | y⟩· have h := (tiles (a,b)).property.1 (x,y)simp only [rawMate, Prod.mk.eta, h]· simp [rawMate]· simp [rawMate]· simp [rawMate]lemma raw_valid {n k : ℕ} (tiles : Block n k → Matching k)(p : Axis n k × Axis n k) :rawMate tiles p ≠ p ∧Near false n (axis n k (rawMate tiles p).1) (axis n k p.1) ∧Near false n (axis n k (rawMate tiles p).2) (axis n k p.2) := byrcases p with ⟨⟨a,x⟩ | x, ⟨b,y⟩ | y⟩· obtain ⟨hne, hx, hy⟩ := (tiles (a,b)).property.2 (x,y)refine ⟨?_, ?_, ?_⟩· intro hsimp only [rawMate, Prod.mk.injEq, Sum.inl.injEq, true_and] at hexact hne (Prod.ext h.1 h.2)· simpa [Near, rawMate, axis_left, Nat.dist_add_add_right] using hx· simpa [Near, rawMate, axis_left, Nat.dist_add_add_right] using hy· simp [rawMate, Near, axis_right, Nat.dist_add_add_left, flip_ne, flip_dist]· simp [rawMate, Near, axis_left, Nat.dist_add_add_right, flip_ne, flip_dist]· simp [rawMate, Near, axis_right, Nat.dist_add_add_left, flip_ne, flip_dist]abbrev board (n k : ℕ) := (axis n k).prodCongr (axis n k)def glue {n k : ℕ} (tiles : Block n k → Matching k) : Matching n :=⟨fun z => board n k (rawMate tiles ((board n k).symm z)), byconstructor· intro zsimp only [Equiv.symm_apply_apply]rw [raw_involutive tiles, Equiv.apply_symm_apply]· intro zobtain ⟨p, rfl⟩ := (board n k).surjective zsimp only [Equiv.symm_apply_apply]obtain ⟨hn, hx, hy⟩ := raw_valid tiles pexact ⟨fun h => hn ((board n k).injective h), hx, hy⟩⟩lemma glue_injective (n k : ℕ) : Function.Injective (@glue n k) := byintro A B hfunext bapply Subtype.extfunext phave h' := congrFun (congrArg Subtype.val h) (board n k (.inl (b.1,p.1), .inl (b.2,p.2)))change board n k (rawMate A _) = board n k (rawMate B _) at h'simp only [Equiv.symm_apply_apply] at h'have hin := (board n k).injective h'simp only [rawMate, Prod.mk.eta, Prod.mk.injEq, Sum.inl.injEq, true_and] at hinexact Prod.ext hin.1 hin.2theorem free_pack (n k : ℕ) : tilingCount false k ^ ((n/k)^2) ≤ tilingCount false n := byhave h := Nat.card_le_card_of_injective (@glue n k) (glue_injective n k)simpa [tilingCount, Matching, Block, Nat.card_fun, Nat.card_prod, Nat.card_fin, pow_two] using hlemma free_pos (n : ℕ) : 0 < tilingCount false n := bylet mate : Cell n → Cell n := fun p => (p.1, flip n p.2)have valid : ∀ p, mate p ≠ p ∧ Near false n (mate p).1 p.1 ∧ Near false n (mate p).2 p.2 := byrintro ⟨x,y⟩simp [mate, Near, flip_ne, flip_dist]have inv : Function.Involutive mate := by rintro ⟨x,y⟩; simp [mate]exact Nat.card_pos_iff.2 ⟨⟨⟨mate, inv, valid⟩⟩, inferInstance⟩def delta {m : ℕ} (j i : Fin m) (h : Nat.dist j.val i.val ≤ 1) : Fin 3 :=⟨j.val + 1 - i.val, by simp only [Nat.dist] at h; omega⟩def encode {n : ℕ} (M : Matching n) (u : Cell n) : Fin 3 × Fin 3 :=(delta (M.val u).1 u.1 (by simpa [Near] using (M.property.2 u).2.1),delta (M.val u).2 u.2 (by simpa [Near] using (M.property.2 u).2.2))lemma encode_injective (n : ℕ) : Function.Injective (@encode n) := byintro A B happly Subtype.extfunext uobtain ⟨_,hA₁,hA₂⟩ := A.property.2 uobtain ⟨_,hB₁,hB₂⟩ := B.property.2 usimp [Near, Nat.dist] at hA₁ hA₂ hB₁ hB₂have hx := congrArg (fun p : Fin 3 × Fin 3 => p.1.val) (congrFun h u)have hy := congrArg (fun p : Fin 3 × Fin 3 => p.2.val) (congrFun h u)simp only [encode, delta] at hx hyapply Prod.ext <;> apply Fin.ext <;> omegalemma free_upper (n : ℕ) : tilingCount false n ≤ 9^((2*n)^2) := byhave h := Nat.card_le_card_of_injective (@encode n) (encode_injective n)simpa [tilingCount, Matching, Cell, Nat.card_fun, Nat.card_prod, Nat.card_fin, pow_two] using hnoncomputable def density (n : ℕ) : ℝ := Real.log (tilingCount false n : ℝ) / (4 * (n : ℝ)^2)lemma density_bd (n : ℕ) : density n ≤ Real.log 9 := byrcases eq_or_ne n 0 with rfl | hn· simpa [density] using (Real.log_nonneg (by norm_num : (1:ℝ) ≤ 9))have h := Real.log_le_log (Nat.cast_pos.mpr (free_pos n))(by exact_mod_cast free_upper n : (tilingCount false n : ℝ) ≤ (9:ℝ)^((2*n)^2))rw [Real.log_pow] at hpush_cast at hunfold densityapply (div_le_iff₀ (by positivity)).2exact h.trans_eq (by ring)lemma density_pack {k n : ℕ} (hk : 0 < k) (hkn : k ≤ n) :density k * (1 - (k : ℝ) / n)^2 ≤ density n := byhave hn : 0 < n := hk.trans_le hknhave kr : 0 < (k : ℝ) := by exact_mod_cast hkhave nr : 0 < (n : ℝ) := by exact_mod_cast hnhave fr : 0 < (tilingCount false k : ℝ) := by exact_mod_cast free_pos khave logpos : 0 ≤ Real.log (tilingCount false k : ℝ) := Real.log_nonneg (by exact_mod_cast free_pos k)have h := Real.log_le_log (pow_pos fr ((n/k)^2))(by exact_mod_cast free_pack n k : (tilingCount false k : ℝ)^((n/k)^2) ≤ (tilingCount false n : ℝ))rw [Real.log_pow] at hpush_cast at hhave rem := Nat.mod_lt n hkhave split := Nat.mod_add_div n khave small : n ≤ (n/k)*k + k := by nlinarithhave small' : (n:ℝ) ≤ (n/k:ℕ)*k + k := by exact_mod_cast smallhave bound : ((n:ℝ) - k)^2 ≤ ((n/k:ℕ):ℝ)^2 * (k:ℝ)^2 := byrw [← mul_pow]gcongr <;> linarith [show (k:ℝ) ≤ n by exact_mod_cast hkn]unfold densitycalc_ = Real.log (tilingCount false k : ℝ) * (n-k)^2 / (4*(k:ℝ)^2*(n:ℝ)^2) := by field_simp_ ≤ Real.log (tilingCount false k : ℝ) * (((n/k:ℕ):ℝ)^2*(k:ℝ)^2) / (4*(k:ℝ)^2*(n:ℝ)^2) := by gcongr_ = ((n/k:ℕ):ℝ)^2 * Real.log (tilingCount false k : ℝ) / (4*(n:ℝ)^2) := by field_simp_ ≤ _ := by gcongrnoncomputable def freeEntropy : ℝ := sSup (Set.range fun j : ℕ => density (j+1))/-- The unrestricted first limit with the original `tilingCount` and a specified limit. -/theorem density_to_freeEntropy : Tendsto density atTop (𝓝 freeEntropy) := bylet E := Set.range (fun j : ℕ => density (j+1))have hb : BddAbove E := ⟨Real.log 9, by rintro _ ⟨j,rfl⟩; exact density_bd _⟩have he : E.Nonempty := ⟨_, ⟨0,rfl⟩⟩change Tendsto density atTop (𝓝 (sSup E))apply tendsto_order.2constructor· intro b hltobtain ⟨_, ⟨j, rfl⟩, hj⟩ := (lt_csSup_iff hb he).1 hltlet k := j+1have hz := tendsto_const_div_atTop_nhds_zero_nat (k:ℝ)have hs : Tendsto (fun n : ℕ => density k * (1 - (k:ℝ)/n)^2)atTop (𝓝 (density k)) := bysimpa using ((tendsto_const_nhds (x := density k)).mul (((tendsto_const_nhds (x := (1:ℝ))).sub hz).pow 2))filter_upwards [(tendsto_order.1 hs).1 b hj, eventually_ge_atTop k] with n h₁ h₂exact lt_of_lt_of_le h₁ (density_pack (Nat.succ_pos _) h₂)· intro b hb'filter_upwards [eventually_ge_atTop (1:ℕ)] with n hnobtain ⟨j, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : n ≠ 0)exact lt_of_le_of_lt (le_csSup hb ⟨j,rfl⟩) hb'theorem free_entropy_exists : ∃ s : ℝ,Tendsto (fun n : ℕ => Real.log (tilingCount false n : ℝ) / (4 * (n : ℝ)^2)) atTop (𝓝 s) :=⟨freeEntropy, density_to_freeEntropy⟩end KingDimer/-! A finite-state perfect matching on a two-cell collar. -/namespace KingCollarvariable {α ι : Type*} [DecidableEq α] [DecidableEq ι]/-- `false` is the outer channel and `true` the inner channel. -/abbrev Extended (α ι : Type*) := α ⊕ (ι × Bool)structure Data (α ι : Type*) [DecidableEq α] [DecidableEq ι] wherepartialMate : α → αinv : Function.Involutive partialMateslot : α → ιmarked : ι → Boolowner : ι → αattached : ∀ x, partialMate x = x → marked (slot x) = true ∧ owner (slot x) = xmarked_owner : ∀ i, marked i = true → partialMate (owner i) = owner i ∧ slot (owner i) = inext : ι ≃ ιcarry : ι → Boolstep : ∀ i, carry (next i) = xor (carry i) (marked i)def Data.mate (D : Data α ι) : Extended α ι → Extended α ι| .inl u => if D.partialMate u = u then .inr (D.slot u, true) else .inl (D.partialMate u)| .inr (i,true) => if D.marked i then .inl (D.owner i)else if D.carry i then .inr (D.next i, false) else .inr (i, false)| .inr (i,false) => if D.carry i then .inr (D.next.symm i, !(D.marked (D.next.symm i)))else if D.marked i then .inr (D.next i, false) else .inr (i, true)lemma Data.mate_involutive (D : Data α ι) : Function.Involutive D.mate := byrintro (u | ⟨i,b⟩)· by_cases h : D.partialMate u = u· obtain ⟨hm,ho⟩ := D.attached u hsimp [Data.mate,h,hm,ho]· simp [Data.mate,h,Ne.symm h,D.inv u]· cases b with| true =>cases hm : D.marked i with| true =>obtain ⟨hx,hi⟩ := D.marked_owner i hmsimp [Data.mate,hm,hx,hi]| false =>cases hc : D.carry i <;> simp [Data.mate,hc,hm,D.step]| false =>cases hc : D.carry i with| false => cases hm : D.marked i <;> simp [Data.mate,hc,hm,D.step]| true =>obtain ⟨j,rfl⟩ := D.next.surjective ihave hs := D.step jcases hm : D.marked j <;> cases hj : D.carry j <;> simp_all [Data.mate]lemma Data.mate_ne (D : Data α ι) (hmove : ∀ i, D.next i ≠ i) (p : Extended α ι) : D.mate p ≠ p := bycases p with| inl u => by_cases h : D.partialMate u = u <;> simp [Data.mate,h]| inr p =>rcases p with ⟨i,b⟩have back : D.next.symm i ≠ i := byintro hexact hmove i (by rw [← h, D.next.apply_symm_apply, h])cases b <;> cases hc : D.carry i <;> cases hm : D.marked i <;>simp [Data.mate,hc,hm,hmove,back]lemma Data.mate_rel (D : Data α ι) (R : Extended α ι → Extended α ι → Prop)(symm : ∀ {u v}, R u v → R v u)(old : ∀ u, D.partialMate u ≠ u → R (.inl u) (.inl (D.partialMate u)))(hang : ∀ u, D.partialMate u = u → R (.inl u) (.inr (D.slot u,true)))(rad : ∀ i, R (.inr (i,false)) (.inr (i,true)))(ahead : ∀ i b, R (.inr (i,b)) (.inr (D.next i,false))) : ∀ z, R z (D.mate z) := byrintro (u | ⟨i,b⟩)· by_cases h : D.partialMate u=u· simpa [Data.mate,h] using hang u h· simpa [Data.mate,h] using old u h· cases b with| false =>cases hc : D.carry i with| false =>cases hm : D.marked i· simpa [Data.mate,hc,hm] using rad i· simpa [Data.mate,hc,hm] using ahead i false| true =>simpa [Data.mate,hc] using symm (ahead (D.next.symm i) (!(D.marked (D.next.symm i))))| true =>cases hm : D.marked i with| true =>obtain ⟨hx,hi⟩ := D.marked_owner i hmsimpa [Data.mate,hm,hi] using symm (hang (D.owner i) hx)| false =>cases hc : D.carry i· simpa [Data.mate,hc,hm] using symm (rad i)· simpa [Data.mate,hc,hm] using ahead i trueend KingCollarnamespace KingCollardef cycle {m : ℕ} (hm : 1 < m) : Fin m ≃ Fin m wheretoFun i := ⟨if i.val+1 < m then i.val+1 else 0, by split <;> omega⟩invFun i := ⟨if i.val=0 then m-1 else i.val-1, by split <;> omega⟩left_inv i := by have hi := i.isLt; apply Fin.ext; dsimp; split_ifs at * <;> omegaright_inv i := by have hi := i.isLt; apply Fin.ext; dsimp; split_ifs at * <;> omegalemma cycle_move {m : ℕ} (hm : 1 < m) (i : Fin m) : cycle hm i ≠ i := byintro hhave h' := congrArg Fin.val hsimp only [cycle, Equiv.coe_fn_mk] at h'split_ifs at h' <;> omegadef before {m : ℕ} (S : Finset (Fin m)) (r : ℕ) : ℕ := (S.filter fun j => j.val < r).cardlemma before_zero {m : ℕ} (S : Finset (Fin m)) : before S 0 = 0 := by simp [before]lemma before_full {m : ℕ} (S : Finset (Fin m)) : before S m = S.card := by simp [before]lemma before_succ {m : ℕ} (S : Finset (Fin m)) (i : Fin m) : before S (i.val+1) =before S i.val + if i ∈ S then 1 else 0 := byclassicalby_cases hi : i ∈ S· have heq : (S.filter fun j => j.val < i.val+1) = insert i (S.filter fun j => j.val < i.val) := byext jby_cases h : j=i· subst j; simp [hi]· have hne : j.val ≠ i.val := fun H => h (Fin.ext H)simp only [Finset.mem_filter,Finset.mem_insert,h,false_or]constructor <;> rintro ⟨hs,hlt⟩ <;> exact ⟨hs,by omega⟩unfold beforerw [heq,Finset.card_insert_of_notMem (by simp),if_pos hi]· have heq : (S.filter fun j => j.val < i.val+1) = S.filter fun j => j.val < i.val := byext jby_cases h : j=i· subst j; simp [hi]· have hne : j.val ≠ i.val := fun H => h (Fin.ext H)simp only [Finset.mem_filter]constructor <;> rintro ⟨hs,hlt⟩ <;> exact ⟨hs,by omega⟩unfold beforerw [heq,if_neg hi,Nat.add_zero]def flag {m : ℕ} (S : Finset (Fin m)) (i : Fin m) : Bool := decide (before S i.val % 2 = 1)lemma flag_step {m : ℕ} (hm : 1 < m) (S : Finset (Fin m)) (he : S.card % 2 = 0)(i : Fin m) : flag S (cycle hm i) = xor (flag S i) (decide (i ∈ S)) := byhave hs := before_succ S iby_cases hn : i.val+1 < m· simp only [flag,cycle,Equiv.coe_fn_mk,if_pos hn]by_cases hi : i ∈ S <;> by_cases ho : before S i.val % 2 = 1 <;>simp [hi,ho] at hs ⊢ <;> omega· have hlast : i.val+1 = m := by omegarw [hlast,before_full] at hssimp only [flag,cycle,Equiv.coe_fn_mk,if_neg hn,before_zero]by_cases hi : i ∈ S <;> by_cases ho : before S i.val % 2 = 1 <;>simp [hi,ho] at hs ⊢ <;> omegaend KingCollarnamespace KingCollarabbrev Pt (L : ℕ) := Fin L × Fin Labbrev Ring (L : ℕ) := Fin 4 × Fin (L+2)abbrev Pair := ℕ × ℕdef coords {L : ℕ} (u : Pt L) : Pair := (u.1.val,u.2.val)def turn (L : ℕ) (q : Fin 4) (p : Pair) : Pair :=match q.val with| 0 => p| 1 => (p.2,L+3-p.1)| 2 => (L+3-p.1,L+3-p.2)| _ => (L+3-p.2,p.1)def unt (L : ℕ) (q : Fin 4) (p : Pair) : Pair :=match q.val with| 0 => p| 1 => (L+3-p.2,p.1)| 2 => (L+3-p.1,L+3-p.2)| _ => (p.2,L+3-p.1)def quad (L : ℕ) (p : Pair) : ℕ :=if p.1 < 2 ∧ 2 ≤ p.2 then 0 else if L+2 ≤ p.2 ∧ 2 ≤ p.1 then 1else if L+2 ≤ p.1 ∧ p.2 < L+2 then 2 else 3def top (L : ℕ) (t : ℕ) (b : Bool) : Pair :=if t<L then (b.toNat, t+2) else if t=L then (0,L+2+b.toNat) else (1,L+3-b.toNat)lemma top_bounds (L : ℕ) (t : Fin (L+2)) (b : Bool) :(top L t b).1 < 2 ∧ 2 ≤ (top L t b).2 ∧ (top L t b).2 < L+4 := bycases b <;> simp [top] <;> split_ifs <;> simp_alllemma top_inj {L : ℕ} {t u : Fin (L+2)} {b c : Bool} (h : top L t b = top L u c) : t=u ∧ b=c := byhave ht := t.isLthave hu := u.isLtcases b <;> cases c <;> simp only [top,Bool.toNat] at h <;> split_ifs at h <;>simp_all [Prod.mk.injEq,Fin.ext_iff] <;> omegalemma where_turn (L : ℕ) (q : Fin 4) (p : Pair) (hr : p.1<2) (hc : 2≤p.2) (hd : p.2<L+4) :quad L (turn L q p) = q.val ∧ unt L q (turn L q p) = p ∧(turn L q p).1 < L+4 ∧ (turn L q p).2 < L+4 := byrcases p with ⟨x,y⟩dsimp at hr hc hdfin_cases q <;> simp [turn,unt,quad] <;> split_ifs <;> simp_all <;> omegadef outside {L : ℕ} (i : Ring L) (b : Bool) : Pt (L+4) :=let p := turn L i.1 (top L i.2 b)let h := top_bounds L i.2 blet h' := where_turn L i.1 _ h.1 h.2.1 h.2.2(⟨p.1,h'.2.2.1⟩,⟨p.2,h'.2.2.2⟩)def inside {L : ℕ} (p : Pt L) : Pt (L+4) :=(⟨p.1.val+2,by omega⟩,⟨p.2.val+2,by omega⟩)lemma outside_inj {L : ℕ} {i j : Ring L} {b c : Bool}(h : outside i b = outside j c) : i=j ∧ b=c := byrcases i with ⟨q,t⟩; rcases j with ⟨s,u⟩have hp := congrArg coords hchange turn L q (top L t b) = turn L s (top L u c) at hpobtain ⟨h₁,h₂,h₃⟩ := top_bounds L t bobtain ⟨k₁,k₂,k₃⟩ := top_bounds L u cobtain ⟨hq,huq,_⟩ := where_turn L q _ h₁ h₂ h₃obtain ⟨hs,hus,_⟩ := where_turn L s _ k₁ k₂ k₃have eq : q=s := Fin.ext (hq.symm.trans ((congrArg (quad L) hp).trans hs))subst shave ht := top_inj (huq.symm.trans ((congrArg (unt L q) hp).trans hus))simp [ht.1,ht.2]lemma outside_border {L : ℕ} (i : Ring L) (b : Bool) :(outside i b).1.val < 2 ∨ (outside i b).2.val < 2 ∨L+2 ≤ (outside i b).1.val ∨ L+2 ≤ (outside i b).2.val := byobtain ⟨hr,_,_⟩ := top_bounds L i.2 brcases i with ⟨q,t⟩fin_cases q <;> simp [outside,turn] at * <;> omegadef place {L : ℕ} : Pt L ⊕ (Ring L × Bool) → Pt (L+4)| .inl x => inside x| .inr (i,b) => outside i blemma place_injective (L : ℕ) : Function.Injective (@place L) := byhave separate (x : Pt L) (i : Ring L) (b : Bool) : inside x ≠ outside i b := byintro hhave bor := outside_border i brw [← h] at bordsimp [inside] at boromegarintro (x | ⟨i,b⟩) (y | ⟨j,c⟩) h· have h' := congrArg coords hdsimp [place,inside,coords] at h'simp only [Prod.mk.injEq,Nat.add_right_cancel_iff] at h'exact congrArg Sum.inl (Prod.ext (Fin.ext h'.1) (Fin.ext h'.2))· exact (separate _ _ _ h).elim· exact (separate _ _ _ h.symm).elim· obtain ⟨rfl,rfl⟩ := outside_inj hrflnoncomputable def placeEquiv (L : ℕ) : Pt L ⊕ (Ring L × Bool) ≃ Pt (L+4) :=Equiv.ofBijective place ((Fintype.bijective_iff_injective_and_card _).2⟨place_injective L,by simp [Pt,Ring,Fintype.card_prod]; ring⟩)end KingCollarnamespace KingCollardef Adj (p q : Pair) : Prop := Nat.dist p.1 q.1 ≤ 1 ∧ Nat.dist p.2 q.2 ≤ 1lemma Adj.symm {p q : Pair} (h : Adj p q) : Adj q p := by simpa [Adj,Nat.dist_comm] using habbrev rank (L : ℕ) : Ring L ≃ Fin (4*(L+2)) := finProdFinEquivdef advance (L : ℕ) : Ring L ≃ Ring L :=((rank L).trans (cycle (by omega))).trans (rank L).symmdef nextPair (L : ℕ) (i : Ring L) : Ring L :=if h : i.2.val+1<L+2 then (i.1,⟨i.2.val+1,h⟩)else (⟨if i.1.val<3 then i.1.val+1 else 0,by split <;> omega⟩,0)lemma advance_eq (L : ℕ) (i : Ring L) : advance L i = nextPair L i := byapply (rank L).injectivechange (rank L) ((rank L).symm (cycle (by omega) ((rank L) i))) = _rw [Equiv.apply_symm_apply]apply Fin.extrcases i with ⟨q,t⟩have ht := t.isLtfin_cases q <;> simp [cycle,rank,finProdFinEquiv,nextPair] <;>split_ifs <;> simp_all <;> omegalemma advance_move (L : ℕ) (i : Ring L) : advance L i ≠ i := byintro hhave hp := congrArg (rank L) hchange (rank L) ((rank L).symm (cycle (by omega) ((rank L) i))) = (rank L) i at hprw [Equiv.apply_symm_apply] at hpexact cycle_move _ _ hplemma radial_adj (L : ℕ) (i : Ring L) : Adj (coords (outside i false)) (coords (outside i true)) := byrcases i with ⟨q,t⟩have ht := t.isLtfin_cases q <;> simp [outside,coords,Adj,top,turn,Nat.dist] <;> split_ifs <;> simp_alllemma turn_adj (L : ℕ) (q : Fin 4) (p r : Pair) (hp : p.1<L+4 ∧ p.2<L+4)(hr : r.1<L+4 ∧ r.2<L+4) (h : Adj p r) : Adj (turn L q p) (turn L q r) := byrcases p with ⟨a,b⟩; rcases r with ⟨c,d⟩fin_cases q <;> simp [turn,Adj,Nat.dist] at * <;> omegalemma ahead_adj (L : ℕ) (i : Ring L) (b : Bool) :Adj (coords (outside i b)) (coords (outside (advance L i) false)) := byrw [advance_eq]rcases i with ⟨q,t⟩have ht := t.isLtby_cases hn : t.val+1<L+2· simp only [nextPair,dif_pos hn]change Adj (turn L q (top L t b)) (turn L q (top L (t.val+1) false))obtain ⟨h₁,_,h₂⟩ := top_bounds L t bobtain ⟨k₁,_,k₂⟩ := top_bounds L ⟨t.val+1,hn⟩ falseapply turn_adj L q _ _ ⟨by omega,h₂⟩ ⟨by exact lt_trans k₁ (by omega),k₂⟩cases b <;> simp [Adj,top,Nat.dist] <;> split_ifs <;> simp_all <;> omega· have he : t.val=L+1 := by omegacases L with| zero => fin_cases q <;> cases b <;> simp [outside,coords,Adj,turn,nextPair,he,Nat.dist,top]| succ L => fin_cases q <;> cases b <;> simp [outside,coords,Adj,turn,nextPair,he,Nat.dist,top]def Border {L : ℕ} (x : Pt L) : Prop :=x.1.val = 0 ∨ x.2.val+1 = L ∨ x.1.val+1 = L ∨ x.2.val = 0def slot {L : ℕ} (x : Pt L) : Ring L :=if x.1.val=0 then (0,⟨x.2.val,by omega⟩)else if x.2.val+1=L then (1,⟨x.1.val,by omega⟩)else if x.1.val+1=L then (2,⟨L-1-x.2.val,by omega⟩)else (3,⟨L-1-x.1.val,by omega⟩)def decodeSlot {L : ℕ} (i : Ring L) : Pair :=match i.1.val with| 0 => (0,i.2.val)| 1 => (i.2.val,L-1)| 2 => (L-1,L-1-i.2.val)| _ => (L-1-i.2.val,0)lemma decode_slot {L : ℕ} (p : Pt L) (hp : Border p) : decodeSlot (slot p) = coords p := byrcases p with ⟨x,y⟩have hx := x.isLthave hy := y.isLtsimp only [Border,coords,slot] at *split_ifs <;> simp [decodeSlot] <;> omegalemma slot_injective {L : ℕ} {x y : Pt L} (hx : Border x) (hy : Border y) (h : slot x = slot y) : x=y := byhave hc := (decode_slot x hx).symm.trans ((congrArg decodeSlot h).trans (decode_slot y hy))exact Prod.ext (Fin.ext (congrArg Prod.fst hc)) (Fin.ext (congrArg Prod.snd hc))lemma attachment_adj {L : ℕ} (p : Pt L) (hp : Border p) :Adj (coords (inside p)) (coords (outside (slot p) true)) := byrcases p with ⟨x,y⟩have hx := x.isLthave hy := y.isLtsimp only [Border,slot] at *split_ifs <;> simp [coords,inside,outside,turn,top,Adj,Nat.dist] <;>(try split_ifs) <;> simp_all <;> omegalemma inner_adj {L : ℕ} {p q : Pt L} (h : Adj (coords p) (coords q)) :Adj (coords (inside p)) (coords (inside q)) := bysimpa [Adj,coords,inside,Nat.dist_add_add_right] using hend KingCollarnamespace KingCollaropen OMA.S_oeis_a401670abbrev Matched (w : Bool) (n : ℕ) := {mate : Cell n → Cell n // Function.Involutive mate ∧∀ u, mate u ≠ u ∧ Near w n (mate u).1 u.1 ∧ Near w n (mate u).2 u.2}instance decAdj (p q : Pair) : Decidable (Adj p q) := by unfold Adj; infer_instancedef cleaned {n : ℕ} (M : Matched true n) (u : Cell n) : Cell n :=if Adj (coords (M.val u)) (coords u) then M.val u else ulemma cleaned_inv {n : ℕ} (M : Matched true n) : Function.Involutive (cleaned M) := byintro uby_cases h : Adj (coords (M.val u)) (coords u)· simp [cleaned,h,M.property.1 u,h.symm]· simp [cleaned,h]lemma cleaned_fix {n : ℕ} (M : Matched true n) (u : Cell n) : cleaned M u = u ↔¬Adj (coords (M.val u)) (coords u) := bysimp [cleaned,(M.property.2 u).1]lemma fixed_border {n : ℕ} (M : Matched true n) (u : Cell n) (hu : cleaned M u=u) : Border u := byhave hn := (cleaned_fix M u).1 huobtain ⟨_,hx,hy⟩ := M.property.2 uhave hu₁ := u.1.isLt; have hu₂ := u.2.isLthave hm₁ := (M.val u).1.isLt; have hm₂ := (M.val u).2.isLtsimp [Near,Adj,coords,Border,Nat.dist] at *omegalemma cleaned_adj {n : ℕ} (M : Matched true n) (u : Cell n) : Adj (coords u) (coords (cleaned M u)) := byby_cases h : Adj (coords (M.val u)) (coords u)· simpa [cleaned,h] using h.symm· rw [cleaned,if_neg h]simp [Adj]def fixedCells {n : ℕ} (M : Matched true n) : Finset (Cell n) :=Finset.univ.filter fun u => cleaned M u=ulemma card_fixed_even {n : ℕ} (M : Matched true n) : (fixedCells M).card%2=0 := byclassicallet f : Function.End (Cell n) := cleaned Mhave hpow : f ^ 2 ^ 1 = 1 := byfunext xchange cleaned M (cleaned M x) = xexact cleaned_inv M xhave h := Equiv.Perm.card_fixedPoints_modEq (p:=2) (n:=1) hpowhave eqn : Fintype.card (Function.fixedPoints f) = (fixedCells M).card := byrw [← Fintype.card_coe]apply Fintype.card_congrapply Equiv.subtypeEquivRightintro xsimp [Function.fixedPoints,Function.IsFixedPt,f,fixedCells]rw [eqn] at hsimpa [Nat.ModEq,Cell,Fintype.card_prod,Nat.mul_mod] using h.symmdef markedSet {n : ℕ} (M : Matched true n) : Finset (Ring (2*n)) := (fixedCells M).image slotlemma mem_marked {n : ℕ} (M : Matched true n) (i : Ring (2*n)) : i ∈ markedSet M ↔∃ u, cleaned M u=u ∧ slot u=i := by simp [markedSet,fixedCells]lemma marked_even {n : ℕ} (M : Matched true n) : (markedSet M).card%2=0 := byhave hin : Set.InjOn (@slot (2*n)) (↑(fixedCells M) : Set (Cell n)) := byintro x hx y hy hxysimp [fixedCells] at hx hyexact slot_injective (fixed_border M x hx) (fixed_border M y hy) hxyrw [markedSet,Finset.card_image_of_injOn hin]exact card_fixed_even Mnoncomputable def owner {n : ℕ} (hn : 0<n) (M : Matched true n) (i : Ring (2*n)) : Cell n :=if h : i ∈ markedSet M then ((mem_marked M i).1 h).choose else (⟨0,by omega⟩,⟨0,by omega⟩)lemma owner_spec {n : ℕ} (hn : 0<n) (M : Matched true n) (i : Ring (2*n)) (hm : i ∈ markedSet M) :cleaned M (owner hn M i) = owner hn M i ∧ slot (owner hn M i) = i := bysimpa only [owner,dif_pos hm] using ((mem_marked M i).1 hm).choose_specnoncomputable def collarData {n : ℕ} (hn : 0<n) (M : Matched true n) : Data (Cell n) (Ring (2*n)) wherepartialMate := cleaned Minv := cleaned_inv Mslot := slotmarked i := decide (i ∈ markedSet M)owner := owner hn Mattached u hu := byhave hm := (mem_marked M _).2 ⟨u,hu,rfl⟩obtain ⟨hfix,hs⟩ := owner_spec hn M _ hmexact ⟨by simpa using hm, slot_injective (fixed_border M _ hfix) (fixed_border M _ hu) hs⟩marked_owner i hm := owner_spec hn M i (by simpa using hm)next := advance _carry i := flag ((markedSet M).map (rank (2*n)).toEmbedding) (rank _ i)step i := byhave he : ((markedSet M).map (rank (2*n)).toEmbedding).card%2=0 := by simpa using marked_even Mhave hh := flag_step (by omega : 1 < 4*(2*n+2)) _ he (rank _ i)have hr : rank (2*n) (advance (2*n) i) = cycle (by omega) (rank (2*n) i) :=(rank (2*n)).apply_symm_apply _rw [hr]simpa using hhend KingCollarnamespace KingCollaropen OMA.S_oeis_a401670noncomputable def big (n : ℕ) : Extended (Cell n) (Ring (2*n)) ≃ Cell (n+2) :=(placeEquiv (2*n)).trans ((finCongr (by omega)).prodCongr (finCongr (by omega)))lemma big_coords (n : ℕ) (p : Extended (Cell n) (Ring (2*n))) : coords (big n p) = coords (place p) := rflnoncomputable def enlarge {n : ℕ} (hn : 0<n) (M : Matched true n) : Matched false (n+2) := bylet D := collarData hn Mrefine ⟨fun u => big n (D.mate ((big n).symm u)), ?_, ?_⟩· intro usimp only [Equiv.symm_apply_apply]rw [D.mate_involutive,Equiv.apply_symm_apply]· intro uobtain ⟨p,rfl⟩ := (big n).surjective usimp only [Equiv.symm_apply_apply]let R (x y : Extended (Cell n) (Ring (2*n))) := Adj (coords (place x)) (coords (place y))have rel := D.mate_rel R (fun h => h.symm)(fun x _ => inner_adj (cleaned_adj M x))(fun x hx => attachment_adj x (fixed_border M x hx))(radial_adj _) (ahead_adj _) phave had : Adj (coords (big n (D.mate p))) (coords (big n p)) := rel.symmexact ⟨fun h => D.mate_ne (advance_move _) _ ((big n).injective h), Or.inl had.1, Or.inl had.2⟩def axisCode {m : ℕ} (x y : Fin m) : Fin 5 :=if x.val=y.val then 0 else if x.val+1=y.val then 1 else if y.val+1=x.val then 2else if x.val<y.val then 3 else 4lemma axis_decode {n : ℕ} {x y z : Fin (2*n)} (hy : Near true n y x) (hz : Near true n z x)(h : axisCode x y = axisCode x z) : y=z := byapply Fin.exthave hv := congrArg Fin.val hsimp [Near,Nat.dist] at hy hzsimp only [axisCode] at hvsplit_ifs at hv <;> norm_num at hv <;> omegadef code {n : ℕ} (M : Matched true n) (x : Cell n) : Fin 5 × Fin 5 :=(axisCode x.1 (M.val x).1,axisCode x.2 (M.val x).2)lemma code_decode {n : ℕ} (A B : Matched true n) (x : Cell n) (h : code A x = code B x) : A.val x=B.val x :=Prod.ext (axis_decode (A.property.2 x).2.1 (B.property.2 x).2.1 (congrArg Prod.fst h))(axis_decode (A.property.2 x).2.2 (B.property.2 x).2.2 (congrArg Prod.snd h))noncomputable def boundaryCode {n : ℕ} (hn : 0<n) (M : Matched true n) (i : Ring (2*n)) : Fin 5 × Fin 5 :=code M (owner hn M i)lemma owner_of_fixed {n : ℕ} (hn : 0<n) (M : Matched true n) (u : Cell n) (hu : cleaned M u=u) :owner hn M (slot u) = u := ((collarData hn M).attached u hu).2lemma clean_old {n : ℕ} (M : Matched true n) (u : Cell n) (hu : cleaned M u≠u) : cleaned M u = M.val u := byhave h : Adj (coords (M.val u)) (coords u) := byby_contra hfexact hu ((cleaned_fix M u).2 hf)simp [cleaned,h]lemma enlarge_inj {n : ℕ} (hn : 0<n) :Function.Injective (fun M : Matched true n => (enlarge hn M,boundaryCode hn M)) := byintro A B hhave hf := congrArg (fun p : Matched false (n+2) × (Ring (2*n) → Fin 5 × Fin 5) => p.1.val) hhave hb := congrArg Prod.snd happly Subtype.extfunext uhave hbig := congrFun hf (big n (.inl u))change big n ((collarData hn A).mate ((big n).symm (big n (.inl u)))) =big n ((collarData hn B).mate ((big n).symm (big n (.inl u)))) at hbigsimp only [Equiv.symm_apply_apply] at hbighave hm := (big n).injective hbigchange (if cleaned A u=u then Sum.inr (slot u,true) else Sum.inl (cleaned A u)) =(if cleaned B u=u then Sum.inr (slot u,true) else Sum.inl (cleaned B u)) at hmby_cases ha : cleaned A u=u <;> by_cases hb' : cleaned B u=u· apply code_decodesimpa only [boundaryCode,owner_of_fixed hn A u ha,owner_of_fixed hn B u hb'] using congrFun hb (slot u)· simp [ha,hb'] at hm· simp [ha,hb'] at hm· simp only [if_neg ha,if_neg hb',Sum.inl.injEq] at hmsimpa only [clean_old A u ha,clean_old B u hb'] using hmtheorem torus_enlarge_bound {n : ℕ} (hn : 0<n) :tilingCount true n ≤ tilingCount false (n+2) * 25^(4*(2*n+2)) := byhave h := Nat.card_le_card_of_injective _ (enlarge_inj hn)simpa [tilingCount,Matched,Ring,Nat.card_prod,Nat.card_fun,Nat.card_fin] using hlemma free_le_torus (n : ℕ) : tilingCount false n ≤ tilingCount true n := bylet f (M : Matched false n) : Matched true n := ⟨M.val,M.property.1, fun x =>⟨(M.property.2 x).1,Or.inl (by simpa [Near] using (M.property.2 x).2.1),Or.inl (by simpa [Near] using (M.property.2 x).2.2)⟩⟩exact Nat.card_le_card_of_injective f (fun A B h => Subtype.ext (congrArg (fun z : Matched true n => z.val) h))end KingCollarnamespace KingCollaropen KingDimer OMA.S_oeis_a401670 Filteropen scoped Topologylemma shifted_free_limit : Tendsto (fun n : ℕ =>Real.log (tilingCount false (n+2) : ℝ) / (4*(n:ℝ)^2)) atTop (𝓝 freeEntropy) := byhave base := density_to_freeEntropy.comp (tendsto_add_atTop_nat 2)have factor : Tendsto (fun n : ℕ => (1+(2:ℝ)/n)^2) atTop (𝓝 1) := bysimpa using ((tendsto_const_nhds (x:=(1:ℝ))).add (tendsto_const_div_atTop_nhds_zero_nat (2:ℝ))).pow 2have h := base.mul factorsimp only [mul_one] at happly h.congr'filter_upwards [eventually_ge_atTop (1:ℕ)] with n hnhave hnr : (n:ℝ) ≠ 0 := by exact_mod_cast (by omega : n ≠ 0)simp only [Function.comp_apply,density,Nat.cast_add,Nat.cast_ofNat]field_simplemma torus_log_bound {n : ℕ} (hn : 0<n) :Real.log (tilingCount true n : ℝ) ≤ Real.log (tilingCount false (n+2) : ℝ) +(4*(2*(n:ℝ)+2))*Real.log 25 := byhave hp : 0 < (tilingCount true n : ℝ) := by exact_mod_cast (free_pos n).trans_le (free_le_torus n)have hu : (tilingCount true n : ℝ) ≤ (tilingCount false (n+2) : ℝ)*(25:ℝ)^(4*(2*n+2)) := byexact_mod_cast torus_enlarge_bound hnhave h := Real.log_le_log hp huhave hf : 0 < (tilingCount false (n+2) : ℝ) := by exact_mod_cast free_pos _rw [Real.log_mul hf.ne' (by positivity),Real.log_pow] at hpush_cast at hexact h/-- Boundary effects in the registered torus model vanish after division by area. -/theorem boundary_limit : OMA.R_oeis_a401670_R1.H2 := bylet E (n : ℕ) := Real.log 25 * (2/(n:ℝ) + 2/(n:ℝ)^2)have hz := tendsto_const_div_atTop_nhds_zero_nat (2:ℝ)have hz2 : Tendsto (fun n : ℕ => (2:ℝ)/(n:ℝ)^2) atTop (𝓝 0) := bysimpa only [div_mul_div_comm,mul_one,← sq,zero_pow (by decide : 2≠0)] usinghz.mul (tendsto_const_div_atTop_nhds_zero_nat (1:ℝ))have he : Tendsto E atTop (𝓝 0) := by simpa [E] using (hz.add hz2).const_mul (Real.log 25)have upper := (shifted_free_limit.sub density_to_freeEntropy).add hesimp only [sub_self,add_zero] at upperapply tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds upper· filter_upwards with napply div_nonneg _ (by positivity)apply sub_nonneg.mprexact Real.log_le_log (by exact_mod_cast free_pos n) (by exact_mod_cast free_le_torus n)· filter_upwards [eventually_ge_atTop (1:ℕ)] with n hnhave nr : 0 < (n:ℝ) := by exact_mod_cast hnhave hb := torus_log_bound hndsimp [density,E]calc_ ≤ (Real.log (tilingCount false (n+2) : ℝ) + (4*(2*(n:ℝ)+2))*Real.log 25 - Real.log (tilingCount false n : ℝ)) /(4*(n:ℝ)^2) := by gcongr_ = _ := by field_simp; ringend KingCollartheorem OMA.solution : OMA.R_oeis_a401670_R1.H2 := KingCollar.boundary_limit