Lean / Reader
Lean reader

WallisEncoding.lean

227 lines26 declarationsno sorryimports WallisTypes Mathlib.Data.Finset.Sort Mathlib.Order.Interval.Finset.Fin Mathlib.Data.Fintype.Perm Mathlib.Logic.Equiv.Set

Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports WallisTypes, which it does not have: you can read it there, but it will not check. Build the project itself to check it.

  1. import WallisTypes
  2. import Mathlib.Data.Finset.Sort
  3. import Mathlib.Order.Interval.Finset.Fin
  4. import Mathlib.Data.Fintype.Perm
  5. import Mathlib.Logic.Equiv.Set
  6. namespace WallisEncoding
  7. open Finset
  8. /-- A permutation with longest increasing subsequence of length exactly n-m. -/
  9. def AtDefect {n : ℕ} (m : ℕ) (p : Equiv.Perm (Fin n)) : Prop :=
  10. (∃ s : Finset (Fin n), s.card = n-m ∧ StrictMonoOn p (↑s)) ∧
  11. ∀ s : Finset (Fin n), StrictMonoOn p (↑s) → s.card ≤ n-m
  12. def near {n : ℕ} (m : ℕ) (i j : Fin n) :=
  13. i.val ≤ j.val + m ∧ j.val ≤ i.val + m
  14. /-- Restricting an increasing subsequence to entries below the distinguished entry
  15. preserves membership after applying the permutation. -/
  16. theorem image_below {n : ℕ} (p : Equiv.Perm (Fin n)) (s : Finset (Fin n))
  17. (h : StrictMonoOn p (↑s)) {x : Fin n} (hx : x ∈ s) :
  18. (s ∩ Iio x).image p = s.image p ∩ Iio (p x) := by
  19. ext y
  20. simp only [mem_image, mem_inter, mem_Iio]
  21. constructor
  22. · rintro ⟨z, ⟨hz, hl⟩, rfl⟩
  23. exact ⟨⟨z, hz, rfl⟩, h hz hx hl⟩
  24. · rintro ⟨⟨z, hz, rfl⟩, hl⟩
  25. exact ⟨z, ⟨hz, (h.lt_iff_lt hz hx).1 hl⟩, rfl⟩
  26. theorem interval_bounds {n : ℕ} (s : Finset (Fin n)) (x : Fin n) :
  27. (s ∩ Iio x).card ≤ x.val ∧ x.val ≤ (s ∩ Iio x).card + sᶜ.card := by
  28. constructor
  29. · simpa using card_le_card (inter_subset_right (s₂ := Iio x) (s₁ := s))
  30. · have h := card_inter_add_card_sdiff (Iio x) s
  31. have h' : Iio x \ s ⊆ sᶜ := by intro i hi; exact mem_compl.2 (mem_sdiff.1 hi).2
  32. have hl := card_le_card h'
  33. rw [inter_comm, Fin.card_Iio] at h
  34. omega
  35. /-- All entries of an increasing subsequence of length at least n-m are close
  36. to the diagonal of the permutation matrix. -/
  37. theorem near_of_long {n m : ℕ} (p : Equiv.Perm (Fin n)) (s : Finset (Fin n))
  38. (h : StrictMonoOn p (↑s)) (hlen : n-m ≤ s.card) {x : Fin n} (hx : x ∈ s) :
  39. near m x (p x) := by
  40. have h1 := interval_bounds s x
  41. have h2 := interval_bounds (s.image p) (p x)
  42. have hh := congrArg Finset.card (image_below p s h hx)
  43. rw [card_image_of_injective _ p.injective] at hh
  44. simp only [card_compl, Fintype.card_fin, card_image_of_injective _ p.injective] at h1 h2
  45. unfold near
  46. omega
  47. open WallisGap
  48. abbrev Code (n m : ℕ) := SubsetOfSize n m × SubsetOfSize n m × Equiv.Perm (Fin m)
  49. def Separated {n m : ℕ} (a b : SubsetOfSize n m) : Prop :=
  50. ∀ i ∈ a.val, ∀ j ∈ b.val, ¬near m i j
  51. theorem compl_size {n m : ℕ} (a : SubsetOfSize n m) : a.valᶜ.card = n-m := by
  52. rw [card_compl, Fintype.card_fin, a.property]
  53. /-- Increasing bijection connecting the ordered complements of the two exceptional sets. -/
  54. noncomputable def spine {n m : ℕ} (a b : SubsetOfSize n m) : ↥a.valᶜ ≃o ↥b.valᶜ :=
  55. (a.valᶜ.orderIsoOfFin (compl_size a)).symm.trans
  56. (b.valᶜ.orderIsoOfFin (compl_size b))
  57. abbrev Fiber {n m : ℕ} (a b : SubsetOfSize n m) :=
  58. {p : Equiv.Perm (Fin n) // ∀ x : ↥a.valᶜ, p x = spine a b x}
  59. noncomputable def exceptionalSlots {n m : ℕ} (a : SubsetOfSize n m) :
  60. ((↑(a.valᶜ) : Set (Fin n))ᶜ : Set (Fin n)) ≃ Fin m :=
  61. (Equiv.subtypeEquivRight (p := fun x => x ∈ (↑(a.valᶜ) : Set (Fin n))ᶜ)
  62. (q := fun x => x ∈ a.val) (by intro x; simp)).trans (a.val.orderIsoOfFin a.property).symm.toEquiv
  63. noncomputable def fiberEquiv {n m : ℕ} (a b : SubsetOfSize n m) : Fiber a b ≃ Equiv.Perm (Fin m) :=
  64. (Equiv.Set.compl (s := (↑(a.valᶜ) : Set (Fin n))) (t := (↑(b.valᶜ) : Set (Fin n)))
  65. (spine a b).toEquiv).trans ((exceptionalSlots a).equivCongr (exceptionalSlots b))
  66. noncomputable def decode {n m : ℕ} (c : Code n m) : Equiv.Perm (Fin n) :=
  67. ((fiberEquiv c.1 c.2.1).symm c.2.2).val
  68. theorem decode_spine {n m : ℕ} (c : Code n m) (x : ↥c.1.valᶜ) :
  69. decode c x = spine c.1 c.2.1 x :=
  70. ((fiberEquiv c.1 c.2.1).symm c.2.2).property x
  71. theorem decode_mono {n m : ℕ} (c : Code n m) :
  72. StrictMonoOn (decode c) (↑(c.1.valᶜ)) := by
  73. intro i hi j hj hij
  74. simpa only [decode_spine c (⟨i, hi⟩ : ↥c.1.valᶜ), decode_spine c (⟨j,hj⟩ : ↥c.1.valᶜ)] using
  75. (show decode c (⟨i,hi⟩ : ↥c.1.valᶜ) < decode c (⟨j,hj⟩ : ↥c.1.valᶜ) from by
  76. rw [decode_spine, decode_spine]
  77. exact (spine c.1 c.2.1).strictMono hij)
  78. theorem decode_mem_iff {n m : ℕ} (c : Code n m) (x : Fin n) :
  79. decode c x ∈ c.2.1.val ↔ x ∈ c.1.val := by
  80. constructor
  81. · intro h
  82. by_contra hx
  83. have H := (spine c.1 c.2.1 (⟨x, mem_compl.2 hx⟩ : ↥c.1.valᶜ)).property
  84. rw [← decode_spine] at H
  85. exact (mem_compl.1 H) h
  86. · intro h
  87. by_contra hx
  88. obtain ⟨y, hy⟩ := (spine c.1 c.2.1).surjective (⟨decode c x, mem_compl.2 hx⟩ : ↥c.2.1.valᶜ)
  89. have heq : decode c (↑y) = decode c x := (decode_spine c y).trans (congrArg Subtype.val hy)
  90. exact (mem_compl.1 y.property) ((decode c).injective heq ▸ h)
  91. theorem unique_long {n m : ℕ} (c : Code n m) (hg : Separated c.1 c.2.1)
  92. (s : Finset (Fin n)) (h : StrictMonoOn (decode c) (↑s)) (hlen : n-m ≤ s.card) :
  93. s = c.1.valᶜ := by
  94. refine eq_of_subset_of_card_le ?_ (by simpa [compl_size] using hlen)
  95. intro x hx
  96. apply mem_compl.2
  97. intro he
  98. exact hg x he _ ((decode_mem_iff c x).2 he) (near_of_long _ _ h hlen hx)
  99. theorem defect_of_separated {n m : ℕ} (c : Code n m) (hg : Separated c.1 c.2.1) :
  100. AtDefect m (decode c) := by
  101. refine ⟨⟨_, compl_size c.1, decode_mono c⟩, ?_⟩
  102. intro s h
  103. by_contra! hl
  104. rw [unique_long c hg s h hl.le, compl_size] at hl
  105. omega
  106. /-- The ordered restriction of a permutation agreeing with a spine is forced. -/
  107. theorem fiber_of_mono {n m : ℕ} (a b : SubsetOfSize n m) (p : Equiv.Perm (Fin n))
  108. (hm : StrictMonoOn p (↑(a.valᶜ))) (hab : ∀ x, p x ∈ b.val ↔ x ∈ a.val) :
  109. ∀ x : ↥a.valᶜ, p x = spine a b x := by
  110. let ea := a.valᶜ.orderIsoOfFin (compl_size a)
  111. let eb := b.valᶜ.orderIsoOfFin (compl_size b)
  112. have uniq := orderEmbOfFin_unique (compl_size b)
  113. (f := fun i : Fin (n-m) => p (ea i))
  114. (fun i => mem_compl.2 ((hab _).not.2 (mem_compl.1 (ea i).property)))
  115. (fun i j hij => hm (ea i).property (ea j).property (ea.strictMono hij))
  116. intro x
  117. have hu := congrFun uniq (ea.symm x)
  118. simpa only [ea.apply_symm_apply] using hu
  119. lemma perm_mem_image {n : ℕ} (p : Equiv.Perm (Fin n)) (s : Finset (Fin n)) (x : Fin n) :
  120. p x ∈ s.image p ↔ x ∈ s := by
  121. simp [Finset.mem_image, p.injective.eq_iff]
  122. theorem exists_code {n m : ℕ} (hm : m ≤ n) (p : Equiv.Perm (Fin n)) (hp : AtDefect m p) :
  123. ∃ c : Code n m, decode c = p := by
  124. obtain ⟨s, hs, hmono⟩ := hp.1
  125. let a : SubsetOfSize n m := ⟨sᶜ, by rw [card_compl,Fintype.card_fin,hs]; omega⟩
  126. let b : SubsetOfSize n m := ⟨a.val.image p, by rw [card_image_of_injective _ p.injective, a.property]⟩
  127. have h : StrictMonoOn p (↑a.valᶜ) := by simpa [a] using hmono
  128. have hh : ∀ x, p x ∈ b.val ↔ x ∈ a.val := fun x => perm_mem_image p a.val x
  129. let f : Fiber a b := ⟨p, fiber_of_mono a b p h hh⟩
  130. exact ⟨(a,b,fiberEquiv a b f), by simp [decode, f]⟩
  131. theorem decode_image {n m : ℕ} (c : Code n m) : c.1.val.image (decode c) = c.2.1.val := by
  132. ext x
  133. obtain ⟨y, rfl⟩ := (decode c).surjective x
  134. exact (perm_mem_image (decode c) c.1.val y).trans (decode_mem_iff c y).symm
  135. /-- Any well-separated encoding is the unique encoding of its permutation,
  136. even when compared with an unrestricted encoding. -/
  137. theorem decode_unique {n m : ℕ} (c d : Code n m) (hc : Separated c.1 c.2.1)
  138. (heq : decode c = decode d) : c = d := by
  139. obtain ⟨a,b,f⟩ := c
  140. obtain ⟨a',b',g⟩ := d
  141. have hmono := decode_mono (a',b',g)
  142. rw [← heq] at hmono
  143. have h1 := unique_long (a,b,f) hc a'.valᶜ hmono (compl_size a').ge
  144. have ha : a' = a := Subtype.ext (compl_injective h1)
  145. subst a'
  146. have h2 := decode_image (a,b',g)
  147. rw [← heq, decode_image] at h2
  148. have hb : b = b' := Subtype.ext h2
  149. subst b'
  150. have hf : (fiberEquiv a b).symm f = (fiberEquiv a b).symm g := Subtype.ext heq
  151. rw [(fiberEquiv a b).symm.injective hf]
  152. lemma rank_injective {n : ℕ} (s : Finset (Fin n)) {x y : Fin n} (hx : x ∈ s) (hy : y ∈ s)
  153. (h : (s ∩ Iio x).card = (s ∩ Iio y).card) : x = y := by
  154. have inc : ∀ a b : Fin n, a ∈ s → a < b → (s ∩ Iio a).card < (s ∩ Iio b).card := by
  155. intro a b ha hab
  156. apply Finset.card_lt_card
  157. refine Finset.ssubset_iff_subset_ne.mpr ⟨?_, ?_⟩
  158. · intro z hz
  159. exact mem_inter.2 ⟨(mem_inter.1 hz).1, mem_Iio.2 ((mem_Iio.1 (mem_inter.1 hz).2).trans hab)⟩
  160. · intro e
  161. have aR : a ∈ s ∩ Iio b := mem_inter.2 ⟨ha, mem_Iio.2 hab⟩
  162. rw [← e] at aR
  163. exact (lt_irrefl a) (mem_Iio.1 (mem_inter.1 aR).2)
  164. rcases lt_trichotomy x y with h'|h'|h'
  165. · exact False.elim ((ne_of_lt (inc x y hx h')) h)
  166. · exact h'
  167. · exact False.elim ((ne_of_lt (inc y x hy h')) h.symm)
  168. lemma ranks_add {n : ℕ} (a : Finset (Fin n)) (u : Fin n) :
  169. (a.filter (· < u)).card + (aᶜ ∩ Iio u).card = u.val := by
  170. have h := card_inter_add_card_sdiff (Iio u) a
  171. have e₁ : a.filter (· < u) = Iio u ∩ a := by ext x; simp [and_comm]
  172. have e₂ : aᶜ ∩ Iio u = Iio u \ a := by ext x; simp [and_comm]
  173. simpa only [e₁, e₂, Fin.card_Iio] using h
  174. theorem fixed_iff_matchingRank {n m : ℕ} (c : Code n m) (u : Fin n) (ha : u ∉ c.1.val)
  175. (hb : u ∉ c.2.1.val) : decode c u = u ↔ MatchingRank (c.1,c.2.1,u) := by
  176. have hu : u ∈ c.1.valᶜ := mem_compl.2 ha
  177. have hv : decode c u ∈ c.2.1.valᶜ := mem_compl.2 ((decode_mem_iff c u).not.2 ha)
  178. have H := congrArg Finset.card (image_below (decode c) c.1.valᶜ (decode_mono c) hu)
  179. rw [card_image_of_injective _ (decode c).injective] at H
  180. have hc : c.1.valᶜ.image (decode c) = c.2.1.valᶜ := by
  181. ext x
  182. obtain ⟨y,rfl⟩ := (decode c).surjective x
  183. simp only [perm_mem_image, mem_compl, decode_mem_iff]
  184. rw [hc] at H
  185. have h1 := ranks_add c.1.val u
  186. have h2 := ranks_add c.2.1.val u
  187. change _ ↔ (c.1.val.filter (· < u)).card = (c.2.1.val.filter (· < u)).card
  188. constructor
  189. · intro hh
  190. rw [hh] at H
  191. omega
  192. · intro hh
  193. exact rank_injective _ hv (mem_compl.2 hb) (by omega)
  194. end WallisEncoding