Lean / Reader
Lean readerWallisEncoding.lean
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.
import WallisTypesimport Mathlib.Data.Finset.Sortimport Mathlib.Order.Interval.Finset.Finimport Mathlib.Data.Fintype.Permimport Mathlib.Logic.Equiv.Setnamespace WallisEncodingopen Finset/-- A permutation with longest increasing subsequence of length exactly n-m. -/def AtDefect {n : ℕ} (m : ℕ) (p : Equiv.Perm (Fin n)) : Prop :=(∃ s : Finset (Fin n), s.card = n-m ∧ StrictMonoOn p (↑s)) ∧∀ s : Finset (Fin n), StrictMonoOn p (↑s) → s.card ≤ n-mdef near {n : ℕ} (m : ℕ) (i j : Fin n) :=i.val ≤ j.val + m ∧ j.val ≤ i.val + m/-- Restricting an increasing subsequence to entries below the distinguished entrypreserves membership after applying the permutation. -/theorem image_below {n : ℕ} (p : Equiv.Perm (Fin n)) (s : Finset (Fin n))(h : StrictMonoOn p (↑s)) {x : Fin n} (hx : x ∈ s) :(s ∩ Iio x).image p = s.image p ∩ Iio (p x) := byext ysimp only [mem_image, mem_inter, mem_Iio]constructor· rintro ⟨z, ⟨hz, hl⟩, rfl⟩exact ⟨⟨z, hz, rfl⟩, h hz hx hl⟩· rintro ⟨⟨z, hz, rfl⟩, hl⟩exact ⟨z, ⟨hz, (h.lt_iff_lt hz hx).1 hl⟩, rfl⟩theorem interval_bounds {n : ℕ} (s : Finset (Fin n)) (x : Fin n) :(s ∩ Iio x).card ≤ x.val ∧ x.val ≤ (s ∩ Iio x).card + sᶜ.card := byconstructor· simpa using card_le_card (inter_subset_right (s₂ := Iio x) (s₁ := s))· have h := card_inter_add_card_sdiff (Iio x) shave h' : Iio x \ s ⊆ sᶜ := by intro i hi; exact mem_compl.2 (mem_sdiff.1 hi).2have hl := card_le_card h'rw [inter_comm, Fin.card_Iio] at homega/-- All entries of an increasing subsequence of length at least n-m are closeto the diagonal of the permutation matrix. -/theorem near_of_long {n m : ℕ} (p : Equiv.Perm (Fin n)) (s : Finset (Fin n))(h : StrictMonoOn p (↑s)) (hlen : n-m ≤ s.card) {x : Fin n} (hx : x ∈ s) :near m x (p x) := byhave h1 := interval_bounds s xhave h2 := interval_bounds (s.image p) (p x)have hh := congrArg Finset.card (image_below p s h hx)rw [card_image_of_injective _ p.injective] at hhsimp only [card_compl, Fintype.card_fin, card_image_of_injective _ p.injective] at h1 h2unfold nearomegaopen WallisGapabbrev Code (n m : ℕ) := SubsetOfSize n m × SubsetOfSize n m × Equiv.Perm (Fin m)def Separated {n m : ℕ} (a b : SubsetOfSize n m) : Prop :=∀ i ∈ a.val, ∀ j ∈ b.val, ¬near m i jtheorem compl_size {n m : ℕ} (a : SubsetOfSize n m) : a.valᶜ.card = n-m := byrw [card_compl, Fintype.card_fin, a.property]/-- Increasing bijection connecting the ordered complements of the two exceptional sets. -/noncomputable def spine {n m : ℕ} (a b : SubsetOfSize n m) : ↥a.valᶜ ≃o ↥b.valᶜ :=(a.valᶜ.orderIsoOfFin (compl_size a)).symm.trans(b.valᶜ.orderIsoOfFin (compl_size b))abbrev Fiber {n m : ℕ} (a b : SubsetOfSize n m) :={p : Equiv.Perm (Fin n) // ∀ x : ↥a.valᶜ, p x = spine a b x}noncomputable def exceptionalSlots {n m : ℕ} (a : SubsetOfSize n m) :((↑(a.valᶜ) : Set (Fin n))ᶜ : Set (Fin n)) ≃ Fin m :=(Equiv.subtypeEquivRight (p := fun x => x ∈ (↑(a.valᶜ) : Set (Fin n))ᶜ)(q := fun x => x ∈ a.val) (by intro x; simp)).trans (a.val.orderIsoOfFin a.property).symm.toEquivnoncomputable def fiberEquiv {n m : ℕ} (a b : SubsetOfSize n m) : Fiber a b ≃ Equiv.Perm (Fin m) :=(Equiv.Set.compl (s := (↑(a.valᶜ) : Set (Fin n))) (t := (↑(b.valᶜ) : Set (Fin n)))(spine a b).toEquiv).trans ((exceptionalSlots a).equivCongr (exceptionalSlots b))noncomputable def decode {n m : ℕ} (c : Code n m) : Equiv.Perm (Fin n) :=((fiberEquiv c.1 c.2.1).symm c.2.2).valtheorem decode_spine {n m : ℕ} (c : Code n m) (x : ↥c.1.valᶜ) :decode c x = spine c.1 c.2.1 x :=((fiberEquiv c.1 c.2.1).symm c.2.2).property xtheorem decode_mono {n m : ℕ} (c : Code n m) :StrictMonoOn (decode c) (↑(c.1.valᶜ)) := byintro i hi j hj hijsimpa only [decode_spine c (⟨i, hi⟩ : ↥c.1.valᶜ), decode_spine c (⟨j,hj⟩ : ↥c.1.valᶜ)] using(show decode c (⟨i,hi⟩ : ↥c.1.valᶜ) < decode c (⟨j,hj⟩ : ↥c.1.valᶜ) from byrw [decode_spine, decode_spine]exact (spine c.1 c.2.1).strictMono hij)theorem decode_mem_iff {n m : ℕ} (c : Code n m) (x : Fin n) :decode c x ∈ c.2.1.val ↔ x ∈ c.1.val := byconstructor· intro hby_contra hxhave H := (spine c.1 c.2.1 (⟨x, mem_compl.2 hx⟩ : ↥c.1.valᶜ)).propertyrw [← decode_spine] at Hexact (mem_compl.1 H) h· intro hby_contra hxobtain ⟨y, hy⟩ := (spine c.1 c.2.1).surjective (⟨decode c x, mem_compl.2 hx⟩ : ↥c.2.1.valᶜ)have heq : decode c (↑y) = decode c x := (decode_spine c y).trans (congrArg Subtype.val hy)exact (mem_compl.1 y.property) ((decode c).injective heq ▸ h)theorem unique_long {n m : ℕ} (c : Code n m) (hg : Separated c.1 c.2.1)(s : Finset (Fin n)) (h : StrictMonoOn (decode c) (↑s)) (hlen : n-m ≤ s.card) :s = c.1.valᶜ := byrefine eq_of_subset_of_card_le ?_ (by simpa [compl_size] using hlen)intro x hxapply mem_compl.2intro heexact hg x he _ ((decode_mem_iff c x).2 he) (near_of_long _ _ h hlen hx)theorem defect_of_separated {n m : ℕ} (c : Code n m) (hg : Separated c.1 c.2.1) :AtDefect m (decode c) := byrefine ⟨⟨_, compl_size c.1, decode_mono c⟩, ?_⟩intro s hby_contra! hlrw [unique_long c hg s h hl.le, compl_size] at hlomega/-- The ordered restriction of a permutation agreeing with a spine is forced. -/theorem fiber_of_mono {n m : ℕ} (a b : SubsetOfSize n m) (p : Equiv.Perm (Fin n))(hm : StrictMonoOn p (↑(a.valᶜ))) (hab : ∀ x, p x ∈ b.val ↔ x ∈ a.val) :∀ x : ↥a.valᶜ, p x = spine a b x := bylet ea := a.valᶜ.orderIsoOfFin (compl_size a)let eb := b.valᶜ.orderIsoOfFin (compl_size b)have uniq := orderEmbOfFin_unique (compl_size b)(f := fun i : Fin (n-m) => p (ea i))(fun i => mem_compl.2 ((hab _).not.2 (mem_compl.1 (ea i).property)))(fun i j hij => hm (ea i).property (ea j).property (ea.strictMono hij))intro xhave hu := congrFun uniq (ea.symm x)simpa only [ea.apply_symm_apply] using hulemma perm_mem_image {n : ℕ} (p : Equiv.Perm (Fin n)) (s : Finset (Fin n)) (x : Fin n) :p x ∈ s.image p ↔ x ∈ s := bysimp [Finset.mem_image, p.injective.eq_iff]theorem exists_code {n m : ℕ} (hm : m ≤ n) (p : Equiv.Perm (Fin n)) (hp : AtDefect m p) :∃ c : Code n m, decode c = p := byobtain ⟨s, hs, hmono⟩ := hp.1let a : SubsetOfSize n m := ⟨sᶜ, by rw [card_compl,Fintype.card_fin,hs]; omega⟩let b : SubsetOfSize n m := ⟨a.val.image p, by rw [card_image_of_injective _ p.injective, a.property]⟩have h : StrictMonoOn p (↑a.valᶜ) := by simpa [a] using hmonohave hh : ∀ x, p x ∈ b.val ↔ x ∈ a.val := fun x => perm_mem_image p a.val xlet f : Fiber a b := ⟨p, fiber_of_mono a b p h hh⟩exact ⟨(a,b,fiberEquiv a b f), by simp [decode, f]⟩theorem decode_image {n m : ℕ} (c : Code n m) : c.1.val.image (decode c) = c.2.1.val := byext xobtain ⟨y, rfl⟩ := (decode c).surjective xexact (perm_mem_image (decode c) c.1.val y).trans (decode_mem_iff c y).symm/-- Any well-separated encoding is the unique encoding of its permutation,even when compared with an unrestricted encoding. -/theorem decode_unique {n m : ℕ} (c d : Code n m) (hc : Separated c.1 c.2.1)(heq : decode c = decode d) : c = d := byobtain ⟨a,b,f⟩ := cobtain ⟨a',b',g⟩ := dhave hmono := decode_mono (a',b',g)rw [← heq] at hmonohave h1 := unique_long (a,b,f) hc a'.valᶜ hmono (compl_size a').gehave ha : a' = a := Subtype.ext (compl_injective h1)subst a'have h2 := decode_image (a,b',g)rw [← heq, decode_image] at h2have hb : b = b' := Subtype.ext h2subst b'have hf : (fiberEquiv a b).symm f = (fiberEquiv a b).symm g := Subtype.ext heqrw [(fiberEquiv a b).symm.injective hf]lemma rank_injective {n : ℕ} (s : Finset (Fin n)) {x y : Fin n} (hx : x ∈ s) (hy : y ∈ s)(h : (s ∩ Iio x).card = (s ∩ Iio y).card) : x = y := byhave inc : ∀ a b : Fin n, a ∈ s → a < b → (s ∩ Iio a).card < (s ∩ Iio b).card := byintro a b ha habapply Finset.card_lt_cardrefine Finset.ssubset_iff_subset_ne.mpr ⟨?_, ?_⟩· intro z hzexact mem_inter.2 ⟨(mem_inter.1 hz).1, mem_Iio.2 ((mem_Iio.1 (mem_inter.1 hz).2).trans hab)⟩· intro ehave aR : a ∈ s ∩ Iio b := mem_inter.2 ⟨ha, mem_Iio.2 hab⟩rw [← e] at aRexact (lt_irrefl a) (mem_Iio.1 (mem_inter.1 aR).2)rcases lt_trichotomy x y with h'|h'|h'· exact False.elim ((ne_of_lt (inc x y hx h')) h)· exact h'· exact False.elim ((ne_of_lt (inc y x hy h')) h.symm)lemma ranks_add {n : ℕ} (a : Finset (Fin n)) (u : Fin n) :(a.filter (· < u)).card + (aᶜ ∩ Iio u).card = u.val := byhave h := card_inter_add_card_sdiff (Iio u) ahave e₁ : a.filter (· < u) = Iio u ∩ a := by ext x; simp [and_comm]have e₂ : aᶜ ∩ Iio u = Iio u \ a := by ext x; simp [and_comm]simpa only [e₁, e₂, Fin.card_Iio] using htheorem fixed_iff_matchingRank {n m : ℕ} (c : Code n m) (u : Fin n) (ha : u ∉ c.1.val)(hb : u ∉ c.2.1.val) : decode c u = u ↔ MatchingRank (c.1,c.2.1,u) := byhave hu : u ∈ c.1.valᶜ := mem_compl.2 hahave hv : decode c u ∈ c.2.1.valᶜ := mem_compl.2 ((decode_mem_iff c u).not.2 ha)have H := congrArg Finset.card (image_below (decode c) c.1.valᶜ (decode_mono c) hu)rw [card_image_of_injective _ (decode c).injective] at Hhave hc : c.1.valᶜ.image (decode c) = c.2.1.valᶜ := byext xobtain ⟨y,rfl⟩ := (decode c).surjective xsimp only [perm_mem_image, mem_compl, decode_mem_iff]rw [hc] at Hhave h1 := ranks_add c.1.val uhave h2 := ranks_add c.2.1.val uchange _ ↔ (c.1.val.filter (· < u)).card = (c.2.1.val.filter (· < u)).cardconstructor· intro hhrw [hh] at Homega· intro hhexact rank_injective _ hv (mem_compl.2 hb) (by omega)end WallisEncoding