Skip to content

Lean formalization of the Riemann Hypothesis (Alpöge, 2026-11-20) being cleaned up by members of the Lean community.

Python
68.8 KB
Raw Blame
module

import Mathlib.Order.CompletePartialOrder
public import Lib.Adelic.Witt.WittTheorem

/-!
# Floor groups, Weyl elements and the Bessel subgroup of a split unitary group

We study the unitary group of the form `T.splitGram` over a commutative ring with involution. This
form has `𝔰.ϱ` hyperbolic planes and a middle block `T`. We define the floor groups `H_l`, `𝒩_l`,
`Q_l` and Weyl elements exchanging `e_a` and `e*_a`. These serve a descent through the hyperbolic
pairs, one pair at a time. We then study the Bessel subgroup `S` (`New.Shape.Form.besselS`),
generated by the block unitriangular isometries `𝖴` (`New.Shape.Form.besselU`) and the isometries
`H` (`New.Shape.Form.besselH`) that fix all basis vectors outside `κ`.

## Main definitions and results

* `New.Shape.Form.floorGroup`: the fixer `H_l` of the floors below `l`. It decreases from `⊤` at
  `l = 0` to `⊥` for `l ≥ ϱ + 2`.
* `New.Shape.Form.unipotentBelow`, `New.Floors.FloorGroups.floorUnipotent`,
  `New.Shape.Form.partialUnitriangularGroup`: the groups `𝒩_{<l}`, `𝒩_l = H_l ∩ 𝒩_{<l+1}` and `Q_l`.
  The first increases to `New.Shape.Form.besselU` and the third decreases to it.
* `New.Floors.FloorGroups.leadingMinor_frameGram_partialUnitriangularGroup_mul`,
  `New.Filling.leadingMinor_frameGram_besselU_mul`: the leading minors of order at most `l` of the
  matrix of the products `h(f_a, γ f_b)` depend only on the double coset of `γ` under `Q_l`, and all
  of them only on the double coset under `𝖴`.
* `New.besselS_le_normalizer_besselU`: `S` normalises `𝖴`.
* `New.div_mem_unitaryLevel_of_besselU_mul`: the `h` in `H` with `h y` in `𝖴 K`, for a principal
  congruence subgroup `K`, lie in one right coset of `H ∩ K`.
* `New.simpleRootSum`, `New.simpleRootSum_mul`, `New.simpleRootSum_eq_zero_of_mem_besselH`: the sum
  of the entries just above the diagonal on the frame is additive on `S` and zero on `H`.

Notions from other files:
* `New.Shape.Form.splitGram`: the Gram matrix `J` of ϱ hyperbolic planes and a middle space with
  Gram matrix `T`: identity blocks pair the first and last ϱ indices, `T` is the middle block, all
  else is zero. `T.G` is its unitary group.
* `New.Shape.BesselIndex`: the basis indices. `inl a` is `e_a`, `eStar a` = `inr (inr a)` is `e*_a`;
  the middle indices are those in `κ` and one more, `z`. `𝔰.n` is `ϱ + 1`.
* `New.BesselIndex.level`: `a` at `e_a`, `ϱ` on the middle block, `2ϱ - a` at `e*_a`.
* `New.Shape.besselFrame`: the frame `f = (e_0, …, e_{ϱ-1}, z)`; `𝔰.top` is the position of `z`.
* `New.Shape.Form.besselU`: the isometries that are block upper unitriangular for the levels.

## Relation to the literature

The file belongs to two known theories: isotropic flags in unitary groups (see
`Lib.DiscreteSeries.Bruhat.ComplexPlace`), and Poincaré series along a Bessel subgroup with their
unfolding, for which it supplies the groups. Sources: A. Borel, Introduction to automorphic forms,
Proc. Sympos. Pure Math. 9 (1966) (Poincaré series); V. Platonov, A. Rapinchuk, Algebraic groups and
number theory (1994) §7.1; for the set-up, W. T. Gan, B. Gross, D. Prasad, Astérisque 346 (2012).
-/

@[expose] public section

noncomputable section

/-!
## Floor groups and Weyl elements over a ring with involution

`New.Floors.FloorGroups.floorOf` is the floor of a basis index: `a` on the pair `e_a, e*_a`, `ϱ` on
`z`, and `ϱ + 1` on `κ`.
-/

namespace New.Floors.FloorGroups

open Matrix BesselIndex Sum Subgroup Ring Fintype

variable {A : Type} [CommRing A] [StarRing A] {𝔰 : Shape} {T : 𝔰.Form A} (l : ℕ) {a : Fin 𝔰.ϱ}

/-! ### Floors, the opposite basis vector and the Weyl elements -/

/-- The floor of a basis index: `a` for both members `inl a` and `inr (inr a)` of the hyperbolic
pair number `a < ϱ`, `ϱ` for the last vector `z = inr (inl (inr ()))` of the middle block, and
`ϱ + 1` for the middle indices `inr (inl (inl k))`, `k : κ`. -/
def floorOf : 𝔰.BesselIndex → ℕ
  | inl a | eStar a => a
  | inr (inl (inl _)) => 𝔰.n
  | _ => 𝔰.ϱ

/-- The involution of the basis indices that exchanges `inl a` and `inr (inr a)`, the two members
of a hyperbolic pair, and fixes each index `inr (inl m)` of the middle block. -/
def oppositeIndex : 𝔰.BesselIndex → 𝔰.BesselIndex
  | inl a => eStar a
  | eStar a => inl a
  | i => i

attribute [simp] floorOf.eq_1 floorOf.eq_2 floorOf.eq_3 floorOf.eq_4 oppositeIndex.eq_1
  oppositeIndex.eq_2 oppositeIndex.eq_3

/-- For `b ≤ ϱ` and an isometry `g`, the vector `g⁻¹ f_b`, where `f = (e_0, …, e_{ϱ-1}, z)` is the
frame `𝔰.besselFrame`. -/
def orbitPoint (b : Fin (𝔰.ϱ + 1)) (g : T.G) : 𝔰.BesselIndex → A :=
  g⁻¹.val.val *ᵥ Pi.single (𝔰.besselFrame b) 1

/-- Floor against level: an index is either in a hyperbolic pair (floor below `ϱ`, and level equal
to the floor or to `2ϱ` minus it) or in the middle block (level `ϱ`, floor `ϱ` or `ϱ + 1`). -/
@[grind .]
lemma floorOf_level_cases (i : 𝔰.BesselIndex) :
    (floorOf i = level i ∨ level i + floorOf i = 2 * 𝔰.ϱ) ∧ floorOf i < 𝔰.ϱ ∨
    level i = 𝔰.ϱ ∧ 𝔰.ϱ ≤ floorOf i ∧ floorOf i ≤ 𝔰.n := by
  obtain a | (k | u) | a := i
  · simp only [floorOf, level, elim_inl, true_or, Fin.is_lt, and_self]
  · simp only [floorOf, level, elim_inr, elim_inl, Nat.add_eq_left, one_ne_zero,
      false_or, add_lt_iff_neg_left, not_lt_zero, and_false, le_add_iff_nonneg_right,
      zero_le, Std.le_refl, and_self, or_true]
  · simp only [floorOf, level, elim_inr, elim_inl, true_or, lt_self_iff_false,
      and_false, Std.le_refl, le_add_iff_nonneg_right, zero_le, and_self, or_true]
  · simp only [floorOf, level, elim_inr, Fin.is_lt, and_true]; lia

lemma oppositeIndex_oppositeIndex (i : 𝔰.BesselIndex) : oppositeIndex (oppositeIndex i) = i := by
  obtain a | m | a := i <;> rfl

lemma floorOf_oppositeIndex (i : 𝔰.BesselIndex) : floorOf (oppositeIndex i) = floorOf i := by
  obtain a | m | a := i <;> simp only [oppositeIndex, floorOf]

/-- `oppositeIndex` reflects the level in `ϱ`. -/
lemma level_oppositeIndex_add_level (i : 𝔰.BesselIndex) :
    level (oppositeIndex i) + level i = 2 * 𝔰.ϱ := by
  obtain a | m | a := i
  · change 2 * 𝔰.ϱ - a + a = 2 * 𝔰.ϱ
    lia
  · exact (two_mul 𝔰.ϱ).symm
  · change a + (2 * 𝔰.ϱ - a) = 2 * 𝔰.ϱ
    lia

/-- `oppositeIndex` is an involution of the basis indices that keeps the floor `floorOf` and
reflects the level in `ϱ`. -/
lemma oppositeIndex_involutive_and_floorOf_level (i : 𝔰.BesselIndex) :
    oppositeIndex (oppositeIndex i) = i ∧
    floorOf (oppositeIndex i) = floorOf i ∧
    level (oppositeIndex i) + level i = 2 * 𝔰.ϱ :=
  ⟨oppositeIndex_oppositeIndex i, floorOf_oppositeIndex i, level_oppositeIndex_add_level i⟩

omit [StarRing A] in
/-- In the rows and columns of the hyperbolic pairs the Gram matrix is the permutation matrix of
`oppositeIndex`. -/
@[simp]
lemma splitGram_eq_ite_oppositeIndex (i j : 𝔰.BesselIndex)
    (h_pair : floorOf i < 𝔰.ϱ ∨ floorOf j < 𝔰.ϱ) :
    T.splitGram i j = if oppositeIndex i = j then 1 else 0 := by
  revert i j
  simp [splitGram, Sum.forall, floorOf, oppositeIndex, Matrix.one_apply]

open scoped Classical in
/-- Write `e_a`, `e*_a` for the basis vectors `inl a`, `inr (inr a)`. On each floor `a < ϱ` where
`P a` holds and `c = p a` is a unit, this isometry sends `e_a` to `c e*_a` and `e*_a` to
`(star c)⁻¹ e_a`; it fixes all other basis vectors. -/
def weylOn (P : ℕ → Prop) (p : ℕ → A) : T.G :=
  let M (p : ℕ → A) : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A :=
    of fun i j ↦
      let swapped : Prop := P (floorOf j) ∧ floorOf j < 𝔰.ϱ ∧ IsUnit (p (floorOf j))
      if i = (if swapped then oppositeIndex j else j) then
        if swapped then (if level j < 𝔰.ϱ then p (floorOf j) else (star (p (floorOf j)))⁻¹ʳ) else 1
      else
        0
  have hM (q : ℕ → A) : M q * M (star q) = 1 := by
    ext
    simp only [Pi.star_apply, isUnit_star, star_star, Matrix.mul_apply, of_apply, mul_ite,
      ite_mul, one_mul, zero_mul, mul_one, mul_zero, Finset.sum_ite_eq', Finset.mem_univ,
      ↓reduceIte, Matrix.one_apply, M]
    grind [oppositeIndex_involutive_and_floorOf_level, inverse_mul_cancel]
  { val := ⟨M p, M (star p), hM p, by simpa only [star_star] using hM (star p)⟩
    property := by
      rw [mem_unitaryGroupOf_iff]
      ext
      simp only [inverse_star, Matrix.mul_apply, star_apply, of_apply, apply_ite star,
        star_star, star_one, star_zero, ite_mul, one_mul, zero_mul, Finset.sum_ite_eq',
        Finset.mem_univ, ↓reduceIte, mul_ite, mul_one, mul_zero, M]
      grind [splitGram_eq_ite_oppositeIndex, oppositeIndex_involutive_and_floorOf_level,
        inverse_mul_cancel] }

/-- The Weyl element `w_p` of the first `l` hyperbolic pairs: `e_a ↦ p_a e*_a` and
`e*_a ↦ e_a / p̄_a` on the pairs `a < l`, fixing the other basis vectors. -/
def weylElem (p : ℕ → Aˣ) : T.G := weylOn (· < l) (p ·)

/-- The Weyl element `w_l(c)` of floor `l`: `e_l ↦ c e*_l` and `e*_l ↦ e_l / c̄`, fixing the other
basis vectors; it is `1` if `l ≥ ϱ` or `c` is not a unit. -/
def floorWeyl (c : A) : T.G := weylOn (· = l) fun _ ↦ c

/-- The inverse, in the sense of `Ring.inverse`, of the conjugate of a unit `u` is the conjugate of
`u⁻¹`. -/
@[simp↓]
lemma inverse_star_unit (u : Aˣ) : (star ↑u)⁻¹ʳ = star u⁻¹.val :=
  Ring.inverse_unit ⟨star u.val, star u⁻¹.val,
    by rw [← star_mul, u.inv_mul, star_one], by rw [← star_mul, u.mul_inv, star_one]⟩

/-! ### The subgroups and their entries -/

/-- The group `H_l` of the isometries that fix every basis vector of floor below `l`. -/
def _root_.New.Shape.Form.floorGroup : Subgroup T.G :=
  (agreeWithOneOn (fun _ j : 𝔰.BesselIndex ↦ floorOf j < l) A
    fun _ _ _ hj ↦ Or.inr hj).subgroupOf T.G

/-- The group `𝒩_{<l}` of the isometries that are unitriangular when the basis vectors of floor
below `l` keep their level and all others are given the middle level `𝔰.ϱ`. -/
def _root_.New.Shape.Form.unipotentBelow : Subgroup T.G :=
  (unitriangular (fun i : 𝔰.BesselIndex ↦ if floorOf i < l then level i else 𝔰.ϱ) A).subgroupOf T.G

/-- The group `𝒩_l = H_l ∩ 𝒩_{<l+1}`: the elements of `unipotentBelow (l + 1)` that fix every
basis vector of floor below `l`. -/
def floorUnipotent : Subgroup T.G := T.floorGroup l ⊓ T.unipotentBelow (l + 1)

/-- The group `Q_l`. Its elements agree with the identity matrix at every entry `(i, j)` with
`level j ≤ level i` whose column `j` has floor and level both below `l`; when `𝔰.n < l` they do
so at every entry with `level j ≤ level i`. -/
def _root_.New.Shape.Form.partialUnitriangularGroup : Subgroup T.G :=
  (agreeWithOneOn
      (fun i j : 𝔰.BesselIndex ↦ level j ≤ level i ∧ (floorOf j < l ∧ level j < l ∨ 𝔰.n < l))
      A <| by
    -- Given a prescribed entry `(i, j)` and any `k`, the entry `(k, j)` is prescribed if
    -- `level j ≤ level k`, and `(i, k)` is otherwise, as `floorOf k ≤ level k + 1 ≤ level j`.
    intro i k j ⟨hji, hj_low⟩
    obtain hjk | hkj := le_or_gt (level j) (level k)
    · exact .inr ⟨hjk, hj_low⟩
    · have hk_floor : floorOf k ≤ level k + 1 := by
        have := floorOf_level_cases k
        lia
      exact .inl ⟨hkj.le.trans hji, by lia⟩).subgroupOf T.G

export Shape.Form (floorGroup unipotentBelow partialUnitriangularGroup)

variable {l} {g s σ τ : T.G} {b : Fin 𝔰.n} {e v : 𝔰.BesselIndex → A} {P : ℕ → Prop} {p : ℕ → Aˣ}
  {j : ℕ}

/-- An isometry is in the fixer `floorGroup l = H_l` if and only if it agrees with `1` in every
column of floor below `l`. -/
lemma mem_floorGroup :
    g ∈ floorGroup l ↔
      ∀ i j : 𝔰.BesselIndex, floorOf j < l →
        g.val i j = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) i j :=
  Iff.rfl

/-- Two rows have the same dot product with a column if they agree wherever it is nonzero. -/
private lemma dotProduct_eq_on_support {ι : Type*} [Fintype ι] {R : Type*} [Semiring R]
    {u v w : ι → R} (h : ∀ k, w k ≠ 0 → u k = v k) : u ⬝ᵥ w = v ⬝ᵥ w := by
  apply Finset.sum_congr rfl
  intro k _
  by_cases hk : w k = 0 <;> simp [hk, h]

omit [StarRing A] in
/-- At an index of a hyperbolic pair, the coordinate of `J e`, where `J = T.splitGram`, is the
coordinate of `e` at the other index of the pair. -/
lemma splitGram_mulVec_apply (i : 𝔰.BesselIndex) (hi : floorOf i < 𝔰.ϱ) :
    (T.splitGram *ᵥ e) i = e (oppositeIndex i) := by
  revert i
  simp only [splitGram, fromBlocks_mulVec, zero_mulVec, fromCols_mulVec, one_mulVec,
    zero_add, fromRows_mulVec, add_zero, Sum.forall, floorOf, Fin.is_lt, elim_inl,
    Function.comp_apply, oppositeIndex, imp_self, implies_true, elim_inr, Pi.add_apply,
    Pi.zero_apply, add_lt_iff_neg_left, not_lt_zero, IsEmpty.forall_iff,
    lt_self_iff_false, and_self]

/-- Read row `i` of `J g = (g⁻¹)ᴴ J`. On a hyperbolic pair the left side picks out the opposite
row of `g`. On the right, only the nonzero entries of the Gram column contribute. -/
private lemma row_eq_one_of_inv_col (i k : 𝔰.BesselIndex) (hi : floorOf i < 𝔰.ϱ)
    (h : ∀ x, T.splitGram x k ≠ 0 → g⁻¹.val x i = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) x i) :
    g.val (oppositeIndex i) k = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) (oppositeIndex i) k := by
  have row (M : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) :
      (T.splitGram * M) i k = M (oppositeIndex i) k :=
    splitGram_mulVec_apply (e := M.col k) i hi
  have h_form : T.splitGram * g.val.val = g⁻¹.val.valᴴ * T.splitGram := by
    simpa only [InvMemClass.coe_inv, coe_units_inv, inv_inv] using U.form_mul_inv_eq g⁻¹
  rw [← row g.val.val, h_form]
  trans ((1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A)ᴴ * T.splitGram) i k
  · exact dotProduct_eq_on_support fun x hx ↦ congrArg star (h x hx)
  · simpa using row 1

/-- An element of `H_l = floorGroup l` agrees with `1` in the rows of floor below `l` that belong to
a hyperbolic pair. -/
lemma floorGroup_apply_row (hg : g ∈ floorGroup l) (i j : 𝔰.BesselIndex) (hi : floorOf i < l)
    (hi_pair : floorOf i < 𝔰.ϱ) :
    g.val i j = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) i j := by
  -- The column `oppositeIndex i` of `g⁻¹` is that of `1`, so the identity gives row `i` of `g`.
  rw [← oppositeIndex_oppositeIndex i]
  apply row_eq_one_of_inv_col _ _ (by rwa [floorOf_oppositeIndex])
  exact fun x _ ↦ mem_floorGroup.1 (inv_mem hg) x _ (by rwa [floorOf_oppositeIndex])

/-- An element of `H_l = floorGroup l` agrees with `1` in the rows of floor below both `l` and
`𝔰.ϱ`, and in the columns of floor below `l`. -/
lemma apply_eq_one_apply_of_mem_floorGroup (hg : g ∈ floorGroup l) (i j : 𝔰.BesselIndex)
    (h_floor : floorOf i < l ∧ floorOf i < 𝔰.ϱ ∨ floorOf j < l) :
    g.val i j = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) i j :=
  h_floor.elim (fun ⟨hi, hi_pair⟩ ↦ floorGroup_apply_row hg i j hi hi_pair)
    (mem_floorGroup.1 hg i j)

/-- An isometry is in `unipotentBelow l = 𝒩_{<l}` if and only if it agrees with `1` at every entry
`(i, j)` where the level of `j` is at most that of `i`, the floors from `l` on having level `ϱ`. -/
lemma mem_unipotentBelow :
    g ∈ unipotentBelow l ↔
      ∀ i j : 𝔰.BesselIndex,
        (if floorOf j < l then level j else 𝔰.ϱ) ≤ (if floorOf i < l then level i else 𝔰.ϱ) →
          g.val i j = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) i j :=
  Iff.rfl

/-- An isometry is in `partialUnitriangularGroup l = Q_l` if and only if it agrees with `1` at every
entry `(i, j)` with `level j ≤ level i` whose column has floor and level below `l` (or with any
column, if `ϱ + 1 < l`). -/
lemma mem_partialUnitriangularGroup :
    g ∈ partialUnitriangularGroup l ↔
      ∀ i j : 𝔰.BesselIndex, level j ≤ level i → floorOf j < l ∧ level j < l ∨ 𝔰.n < l →
        g.val i j = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) i j :=
  forall₂_congr fun _ _ ↦ and_imp

/-- The floor of the frame vector number `b` is `b`. -/
lemma floorOf_besselFrame : floorOf (𝔰.besselFrame b) = b := by
  induction b using Fin.lastCases <;> simp [floorOf]

attribute [grind .] mem_floorGroup mem_unipotentBelow mem_partialUnitriangularGroup
  floorOf_besselFrame
  level_besselFrame apply_eq_one_apply_of_mem_floorGroup

/-- The vector `g⁻¹ f_b` is `e` if and only if `g` sends `e` to the frame vector `f_b`. -/
lemma orbitPoint_eq_iff : orbitPoint b g = e ↔ g *ᵥ e = Pi.single (𝔰.besselFrame b) 1 :=
  -- Matrices act on vectors by `*ᵥ`, so this is `g⁻¹ • f_b = e ↔ g • e = f_b`.
  (inv_smul_eq_iff (g := g)).trans eq_comm

/-- The frame vector `b` is the only basis vector of floor `b` and level at most `b`. -/
lemma eq_besselFrame_of_floorOf_eq {i : 𝔰.BesselIndex} (h_floor : floorOf i = b)
    (h_level : level i ≤ b) : i = 𝔰.besselFrame b := by
  revert i
  -- In each case for `b`, compare floor and level of each kind of index with those of `f_b`.
  induction b using Fin.lastCases
  · simp only [Fin.val_last, level, besselFrame_top, Sum.forall, floorOf, elim_inl,
      Fin.is_le', reduceCtorEq, imp_false, not_true_eq_false, elim_inr, inr.injEq,
      Std.le_refl, inl.injEq, forall_const, Nat.add_eq_left, one_ne_zero,
      tsub_le_iff_right, not_le]
    lia
  · simp only [Fin.val_castSucc, level, besselFrame_castSucc, Sum.forall, floorOf, elim_inl,
      Fin.val_fin_le, inl.injEq, elim_inr, reduceCtorEq, imp_false, not_le, Fin.is_lt,
      implies_true, true_and]
    lia

/-- An element `s` of `H_b = floorGroup b` lies in `Q_{b+1} = partialUnitriangularGroup (b + 1)`
exactly when it fixes the frame vector `f_b = 𝔰.besselFrame b`, that is, when
`orbitPoint b s = s⁻¹ f_b` equals `f_b`. -/
lemma mem_partialUnitriangularGroup_succ_iff (hs : s ∈ floorGroup b) :
    s ∈ partialUnitriangularGroup (b + 1) ↔ orbitPoint b s = Pi.single (𝔰.besselFrame b) 1 := by
  have h_column : orbitPoint b s = Pi.single (𝔰.besselFrame b) 1 ↔
      ∀ i : 𝔰.BesselIndex, s.val i (𝔰.besselFrame b) =
        (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) i (𝔰.besselFrame b) := by
    rw [orbitPoint_eq_iff]
    simp only [mulVec_single, MulOpposite.op_one, one_smul, funext_iff, col_apply,
      Pi.single_apply, ← Matrix.one_apply, Sum.forall]
  rw [h_column, mem_partialUnitriangularGroup]
  refine ⟨fun h_unitri i ↦ ?_, fun h_frame i j _ hj ↦ ?_⟩
  · by_cases hi : level (𝔰.besselFrame b) ≤ level i
    · exact h_unitri i _ hi (.inl (by simp only [floorOf_besselFrame,
        lt_add_iff_pos_right, Order.lt_one_iff, level_besselFrame, and_self]))
    · -- A row of level below `b` has floor below `b`, so there `s` agrees with `1`.
      have hi_lt : level i < b := not_le.1 (by rwa [level_besselFrame] at hi)
      have h_cases := floorOf_level_cases i
      obtain ⟨h_low, h_pair⟩ : floorOf i < b ∧ floorOf i < 𝔰.ϱ := ⟨by lia, by lia⟩
      exact floorGroup_apply_row hs i _ h_low h_pair
  · by_cases h_floor : floorOf j < b
    · exact mem_floorGroup.1 hs i j h_floor
    · -- The only other column with a condition is that of the frame vector.
      obtain ⟨h_floor_eq, h_level⟩ : floorOf j = b ∧ level j ≤ b := by lia
      rw [eq_besselFrame_of_floorOf_eq h_floor_eq h_level]
      exact h_frame i

/-- Two elements `σ`, `τ` of `H_b = floorGroup b` that send the frame vector `f_b` to the same
vector (`orbitPoint b σ⁻¹` is `σ f_b`) differ on the right by an element of `Q_{b+1} ∩ H_b`. -/
lemma inv_mul_mem_of_orbitPoint_eq (hσ : σ ∈ floorGroup b) (hτ : τ ∈ floorGroup b)
    (h_orbit : orbitPoint b σ⁻¹ = orbitPoint b τ⁻¹) :
    σ⁻¹ * τ ∈ partialUnitriangularGroup (b + 1) ⊓ floorGroup b := by
  have h_mem : σ⁻¹ * τ ∈ floorGroup b := mul_mem (inv_mem hσ) hτ
  have h_image : τ.val.val *ᵥ Pi.single (𝔰.besselFrame b) 1 =
      σ.val.val *ᵥ Pi.single (𝔰.besselFrame b) 1 := by
    simpa only [mulVec_single, MulOpposite.op_one, one_smul, orbitPoint, inv_inv] using
      h_orbit.symm
  refine mem_inf.2 ⟨(mem_partialUnitriangularGroup_succ_iff h_mem).2 (orbitPoint_eq_iff.2 ?_),
    h_mem⟩
  calc (σ⁻¹ * τ).val.val *ᵥ Pi.single (𝔰.besselFrame b) 1
      = σ⁻¹.val.val *ᵥ τ.val.val *ᵥ Pi.single (𝔰.besselFrame b) 1 := (mulVec_mulVec _ _ _).symm
    _ = σ⁻¹.val.val *ᵥ σ.val.val *ᵥ Pi.single (𝔰.besselFrame b) 1 := by rw [h_image]
    _ = Pi.single (𝔰.besselFrame b) 1 := by
        simp only [InvMemClass.coe_inv, coe_units_inv, mulVec_mulVec, isUnits_det_units,
          nonsing_inv_mul, one_mulVec]

/-- The unitriangular group `𝖴 = besselU` lies in every `Q_l = partialUnitriangularGroup l`. -/
@[simp] lemma _root_.New.mem_partialUnitriangularGroup_of_mem_besselU (hg : g ∈ besselU) :
    g ∈ partialUnitriangularGroup l :=
  mem_partialUnitriangularGroup.2 fun i j h_level _ ↦ mem_besselU.1 hg i j h_level

/-- For `l ≥ ϱ` the group `𝒩_{<l} = unipotentBelow l` is the whole unitriangular group `𝖴`. -/
lemma unipotentBelow_eq_besselU (hl : 𝔰.ϱ ≤ l) : unipotentBelow l = T.besselU := by
  ext g
  rw [mem_unipotentBelow, mem_besselU]
  refine ⟨fun h i j hij ↦ h i j ?_, fun h i j hij ↦ h i j ?_⟩
  · have := floorOf_level_cases i; have := floorOf_level_cases j; split_ifs <;> lia
  · have := floorOf_level_cases i; have := floorOf_level_cases j; split_ifs at hij <;> lia

/-! ### Inclusions, monotonicity and extreme cases -/

/-- Every `𝒩_{<l} = unipotentBelow l` lies in the unitriangular group `𝖴`. -/
lemma unipotentBelow_le_besselU : unipotentBelow l ≤ T.besselU := by
  intro g hg
  rw [mem_besselU]
  intro i j hij
  refine mem_unipotentBelow.1 hg i j ?_
  have := floorOf_level_cases i; have := floorOf_level_cases j; split_ifs <;> lia

/-- Every `𝒩_{<j} = unipotentBelow j` lies in every `Q_l = partialUnitriangularGroup l`. -/
lemma unipotentBelow_le_partialUnitriangularGroup :
    T.unipotentBelow j ≤ partialUnitriangularGroup l :=
  fun _ hg ↦ mem_partialUnitriangularGroup_of_mem_besselU (unipotentBelow_le_besselU hg)

/-- The groups `𝒩_{<j} = unipotentBelow j` increase with `j`. -/
lemma unipotentBelow_le_succ : T.unipotentBelow j ≤ unipotentBelow (j + 1) :=
  fun _ hg ↦ mem_unipotentBelow.2 fun i k h_level ↦ mem_unipotentBelow.1 hg i k <| by
    have := floorOf_level_cases i
    have := floorOf_level_cases k
    split_ifs at h_level ⊢ <;> lia

/-- The groups `Q_l = partialUnitriangularGroup l` decrease as `l` grows. -/
lemma partialUnitriangularGroup_anti : Antitone T.partialUnitriangularGroup := by
  intro a b hab g hg
  rw [mem_partialUnitriangularGroup] at hg ⊢
  exact fun i j hji hj ↦ hg i j hji
    (hj.imp (fun ⟨h1, h2⟩ ↦ ⟨h1.trans_le hab, h2.trans_le hab⟩) (fun h ↦ h.trans_le hab))

/-- The fixers `H_l = floorGroup l` decrease as `l` grows. -/
lemma floorGroup_anti : Antitone T.floorGroup := by
  intro a b hab g hg
  rw [mem_floorGroup] at hg ⊢
  exact fun i j hj ↦ hg i j (hj.trans_le hab)

@[simp]
lemma floorGroup_zero : T.floorGroup 0 = ⊤ :=
  eq_top_iff.2 fun _ _ ↦ mem_floorGroup.2 fun _ _ h ↦ absurd h (Nat.not_lt_zero _)

/-- The fixer `H_l` is trivial for `l ≥ ϱ + 2`, as every floor is at most `ϱ + 1`. -/
lemma floorGroup_eq_bot (hl : 𝔰.ϱ + 2 ≤ l) : T.floorGroup l = ⊥ := by
  refine bot_unique fun g hg ↦ ?_
  ext i j
  have hj := floorOf_level_cases j
  exact mem_floorGroup.1 hg i j (by lia)

/-- The group `Q_{ϱ+2}` is the unitriangular group `𝖴`. -/
@[simp]
lemma partialUnitriangularGroup_add_two_eq_besselU :
    partialUnitriangularGroup (𝔰.ϱ + 2) = T.besselU := by
  ext g
  rw [mem_partialUnitriangularGroup, mem_besselU]
  exact ⟨fun h i j hij ↦ h i j hij (.inr (Nat.lt_succ_self 𝔰.n)), fun h i j hij _ ↦ h i j hij⟩

/-- In the fixer `H_ϱ` of all hyperbolic pairs, `Q_{ϱ+1}` cuts out the fixer `H_{ϱ+1}` of these
pairs and of `z`. -/
lemma partialUnitriangularGroup_inf_floorGroup_last :
    partialUnitriangularGroup (𝔰.top + 1) ⊓ floorGroup 𝔰.top = T.floorGroup 𝔰.n := by
  ext g
  rw [mem_inf, mem_partialUnitriangularGroup, mem_floorGroup, mem_floorGroup]
  refine ⟨?_, fun hf ↦ ⟨fun i j hij hj ↦ hf i j ?_, fun i j hj ↦ hf i j (hj.trans
    (Nat.lt_succ_self _))⟩⟩
  · rintro ⟨hp, hf⟩ i j hj
    obtain hlt | heq := Nat.lt_or_eq_of_le (Nat.le_of_lt_succ hj)
    · exact hf i j hlt
    · have h_top : 𝔰.top = 𝔰.ϱ := rfl
      have h_n : 𝔰.n = 𝔰.ϱ + 1 := rfl
      have hjj := floorOf_level_cases j
      by_cases hi : floorOf i < 𝔰.top
      · exact floorGroup_apply_row hf i j hi (by lia)
      · have hii := floorOf_level_cases i
        exact hp i j (by lia) (.inl ⟨hj, by lia⟩)
  · exact hj.elim And.left fun hn ↦ absurd hn (Nat.lt_irrefl _)

/-- The fixer `H_l = floorGroup l` lies in `Q_l = partialUnitriangularGroup l`. -/
@[simp] lemma _root_.New.mem_partialUnitriangularGroup_of_mem_floorGroup (hg : g ∈ floorGroup l) :
    g ∈ partialUnitriangularGroup l :=
  mem_partialUnitriangularGroup.2 fun i j _ hj ↦ mem_floorGroup.1 hg i j <| by
    have := floorOf_level_cases j
    rcases hj with ⟨h1, h2⟩ | hn <;> lia

/-- The fixer `H_l` of the floors below `l` meets the unitriangular group `𝒩_{<j}` of the floors
below `j` trivially when `j ≤ l` or `ϱ ≤ l`. -/
lemma eq_one_of_mem_floorGroup_inf (hg : g ∈ floorGroup l ⊓ unipotentBelow j)
    (hj : j ≤ l ∨ 𝔰.ϱ ≤ l) : g = 1 := by
  obtain ⟨hg_floor, hg_unipotent⟩ := mem_inf.1 hg
  ext row col
  by_cases h_fixed : floorOf row < l ∧ floorOf row < 𝔰.ϱ ∨ floorOf col < l
  · exact apply_eq_one_apply_of_mem_floorGroup hg_floor row col h_fixed
  · -- Otherwise both indices are given the middle level `ϱ`.
    have hrow := floorOf_level_cases row; have hcol := floorOf_level_cases col
    exact mem_unipotentBelow.1 hg_unipotent row col (by split_ifs <;> lia)

/-- For `s` in the step `𝒩_l = floorUnipotent l` and `a ≠ l`, the entry of `s` just above the
diagonal at the frame vectors `(a, a + 1)` vanishes. -/
lemma floorUnipotent_superdiag_eq_zero (a : Fin 𝔰.ϱ) (hs : s ∈ floorUnipotent l) (hl : l ≠ a) :
    s.val (𝔰.besselFrame a.castSucc) (𝔰.besselFrame a.succ) = 0 := by
  obtain ⟨h_floor, h_unipotent⟩ := mem_inf.1 hs
  rw [← one_apply_ne (α := A) (besselFrame_injective.ne a.castSucc_lt_succ.ne)]
  obtain h_lt | h_gt := hl.symm.lt_or_gt
  · -- The row `a` has floor below `l`.
    exact apply_eq_one_apply_of_mem_floorGroup h_floor _ _
      (.inl (by simp only [besselFrame_castSucc, floorOf, h_lt, Fin.is_lt, and_self]))
  · -- Both indices are given the middle level `ϱ`.
    refine mem_unipotentBelow.1 h_unipotent _ _ ?_
    simp only [floorOf_besselFrame, level_besselFrame, Fin.val_castSucc, Fin.val_succ]
    split_ifs <;> lia

/-! ### Conjugation and projections -/

/-- The fixer `H_l` of the floors below `l` normalises the unitriangular group `𝒩_{<l}` of these
floors. -/
lemma conj_mem_unipotentBelow (hσ : σ ∈ floorGroup l) (hg : g ∈ unipotentBelow l) :
    σ * g * σ⁻¹ ∈ unipotentBelow l :=
  -- `σ⁻¹` is block triangular for the levels that define `unipotentBelow l`.
  have h_tri :
      σ⁻¹.val.val.BlockTriangular fun i : 𝔰.BesselIndex ↦
        if floorOf i < l then level i else 𝔰.ϱ := by
    intro i j hij
    rw [← one_apply_ne (α := A) (ne_of_apply_ne
      (fun k : 𝔰.BesselIndex ↦ if floorOf k < l then level k else 𝔰.ϱ) hij.ne')]
    by_cases hj : floorOf j < l
    · exact mem_floorGroup.1 (inv_mem hσ) i j hj
    · have hi := floorOf_level_cases i
      dsimp only at hij
      exact apply_eq_one_apply_of_mem_floorGroup (inv_mem hσ) i j
        (.inl ⟨by split_ifs at hij <;> lia, by split_ifs at hij <;> lia⟩)
  conj_mem_unitriangular (d := σ⁻¹.val) h_tri hg

/-- The fixer `H_{l+1}` of the floors up to `l` normalises the step `𝒩_l = H_l ∩ 𝒩_{<l+1}`. -/
lemma conj_mem_floorUnipotent (hg : g ∈ floorUnipotent l) (σ : T.G)
    (hσ : σ ∈ floorGroup (l + 1)) : σ * g * σ⁻¹ ∈ floorUnipotent l := by
  obtain ⟨hg_floor, hg_unipotent⟩ := mem_inf.1 hg
  have hσ_floor : σ ∈ floorGroup l := floorGroup_anti l.le_succ hσ
  exact mem_inf.2 ⟨mul_mem (mul_mem hσ_floor hg_floor) (inv_mem hσ_floor),
    conj_mem_unipotentBelow hσ hg_unipotent⟩

/-- Conjugation by the fixer `H_{l+1}` of the floors up to `l` keeps the entry at the frame vectors
`(f_a, f_{a+1})` of an element of the unitriangular group `𝒩_{<l}` of the floors below `l`. -/
lemma superdiag_conj_eq (hg : g ∈ unipotentBelow l) (hs : s ∈ floorGroup (l + 1)) :
    (s * g * s⁻¹).val (𝔰.besselFrame a.castSucc) (𝔰.besselFrame a.succ) =
      g.val (𝔰.besselFrame a.castSucc) (𝔰.besselFrame a.succ) := by
  by_cases ha : a < l
  · -- The row `a` of `s` and the column `a + 1` of `s⁻¹`, in the frame, are those of `1`.
    have h_row (k : 𝔰.BesselIndex) : s.val (𝔰.besselFrame a.castSucc) k =
        (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) (𝔰.besselFrame a.castSucc) k :=
      apply_eq_one_apply_of_mem_floorGroup hs _ k (.inl (by simp only [besselFrame_castSucc,
        floorOf, Order.lt_add_one_iff, ha.le, Fin.is_lt, and_self]))
    have h_col (k : 𝔰.BesselIndex) : s⁻¹.val k (𝔰.besselFrame a.succ) =
        (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) k (𝔰.besselFrame a.succ) :=
      apply_eq_one_apply_of_mem_floorGroup (inv_mem hs) k _
        (.inr (by simp only [floorOf_besselFrame, Fin.val_succ, Order.lt_add_one_iff,
          Order.add_one_le_iff, ha]))
    simp only [Subgroup.coe_mul, Units.val_mul, Matrix.mul_apply, h_row, h_col]
    simp only [Matrix.one_apply, besselFrame_castSucc, ite_mul, one_mul, zero_mul,
      Finset.sum_ite_eq, Finset.mem_univ, ↓reduceIte, mul_ite, mul_one, mul_zero,
      Finset.sum_ite_eq']
  · -- Both matrices lie in `unipotentBelow l`, where this entry vanishes.
    have h_eq {x : T.G} (hx : x ∈ unipotentBelow l) :
        x.val (𝔰.besselFrame a.castSucc) (𝔰.besselFrame a.succ) =
          (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) _ _ :=
      mem_unipotentBelow.1 hx _ _ (by simp only [floorOf_besselFrame, Fin.val_castSucc,
        Fin.val_succ, ha, ↓reduceIte, not_lt.2 (Nat.le_succ_of_le (not_lt.1 ha)), le_rfl])
    rw [h_eq (conj_mem_unipotentBelow (floorGroup_anti l.le_succ hs) hg), h_eq hg]

section

omit [StarRing A]

/-- The column of the Gram matrix at an index `i` of a hyperbolic pair is the basis vector at the
other index of the pair. -/
@[simp]
lemma splitGram_col (i : 𝔰.BesselIndex) (hi : floorOf i < 𝔰.ϱ) :
    T.splitGram.col i = Pi.single (oppositeIndex i) 1 := by
  revert i
  simp only [funext_iff, col_apply, Pi.single_apply, Sum.forall, floorOf, Fin.is_lt,
    true_or, splitGram_eq_ite_oppositeIndex, oppositeIndex, reduceCtorEq, ↓reduceIte,
    implies_true, add_lt_iff_neg_left, not_lt_zero, or_true, inr.injEq, lt_self_iff_false,
    and_self, inl.injEq, IsEmpty.forall_iff, and_true, true_and]

omit [StarRing A] in
/-- Multiplying by the split Gram matrix reads the opposite row at a hyperbolic basis index. -/
lemma splitGram_mul_apply_inl (M : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) (a : Fin 𝔰.ϱ)
    (j : 𝔰.BesselIndex) : (T.splitGram * M) (inl a) j = M (eStar a) j :=
  splitGram_mulVec_apply (e := M.col j) (inl a) a.is_lt

/-- At an index of a hyperbolic pair, the coordinate of `e J`, where `J = T.splitGram`, is the
coordinate of `e` at the other index of the pair. -/
lemma vecMul_splitGram_apply (i : 𝔰.BesselIndex) (hi : floorOf i < 𝔰.ϱ) :
    (e ᵥ* T.splitGram) i = e (oppositeIndex i) := by
  revert i
  simp only [splitGram, vecMul_fromBlocks, vecMul_zero, vecMul_fromRows, vecMul_one,
    zero_add, vecMul_fromCols, add_zero, Sum.forall, floorOf, Fin.is_lt, elim_inl,
    Function.comp_apply, oppositeIndex, imp_self, implies_true, elim_inr, Pi.add_apply,
    Pi.zero_apply, add_lt_iff_neg_left, not_lt_zero, IsEmpty.forall_iff,
    lt_self_iff_false, and_self]

/-- Multiplication by `T.splitGram` on either side exchanges the two coordinates of a hyperbolic
pair. -/
@[simp] lemma splitGram_mulVec_and_vecMul_apply (i : 𝔰.BesselIndex) (hi : floorOf i < 𝔰.ϱ) :
    (T.splitGram *ᵥ e) i = e (oppositeIndex i) ∧
    (e ᵥ* T.splitGram) i = e (oppositeIndex i) :=
  ⟨splitGram_mulVec_apply i hi, vecMul_splitGram_apply i hi⟩

/-- The inclusion matrix of the indices of floor at least `l` extends a vector on these indices by
zero. -/
lemma inclMatrix_val_mulVec (u : {i : 𝔰.BesselIndex // l ≤ floorOf i} → A) (i : 𝔰.BesselIndex) :
    (inclMatrix A Subtype.val *ᵥ u) i = if h : l ≤ floorOf i then u ⟨i, h⟩ else 0 := by
  by_cases hi : l ≤ floorOf i <;>
    simp [inclMatrix, mulVec_eq_sum, Matrix.one_apply, Subtype.coe_eq_iff, hi, mul_ite]

end

/-- Let `l ≤ ϱ` and let `s` fix the basis vectors of floor below `l`. Applying the block of `s` on
the indices of floor at least `l` and then extending by zero is the same as extending by zero and
then applying `s`. -/
lemma inclMatrix_mulVec_floor_block (hl : l ≤ 𝔰.ϱ) (hs : s ∈ floorGroup l)
    (u : {i : 𝔰.BesselIndex // l ≤ floorOf i} → A) :
    inclMatrix A Subtype.val *ᵥ
        s.val.val.toSquareBlockProp (fun i : 𝔰.BesselIndex ↦ l ≤ floorOf i) *ᵥ u =
      s.val.val *ᵥ inclMatrix A Subtype.val *ᵥ u := by
  rw [mulVec_mulVec, mulVec_mulVec, mul_inclMatrix]
  congr 1
  ext i j
  -- Extend the column `j` of the block by zero; `s` vanishes in the rows of floor below `l`.
  change (inclMatrix A Subtype.val *ᵥ
    fun k : {i : 𝔰.BesselIndex // l ≤ floorOf i} ↦ s.val k j) i = s.val i j
  rw [inclMatrix_val_mulVec]
  split_ifs with hi
  · rfl
  · rw [apply_eq_one_apply_of_mem_floorGroup hs i j (.inl (by lia)),
      one_apply_ne (by rintro rfl; exact hi j.property)]

/-- For `σ` in the fixer `H_b` of the floors below `b` and `s` in `Q_{b+1}`, the column of `σ` at
the frame vector `f_b` vanishes in the rows of floor below `b` and is that of `σ * s` in the other
rows. -/
lemma floorGroup_apply_besselFrame (hσ : σ ∈ floorGroup b)
    (hs : s ∈ partialUnitriangularGroup (b + 1)) (i : 𝔰.BesselIndex) :
    σ.val i (𝔰.besselFrame b) =
      if floorOf i < b then 0 else (σ * s).val i (𝔰.besselFrame b) := by
  split_ifs with hi
  · rw [apply_eq_one_apply_of_mem_floorGroup hσ i _ (.inl ⟨hi, hi.trans_le b.is_le⟩),
      one_apply_ne (by rintro rfl; rw [floorOf_besselFrame] at hi; exact hi.false)]
  -- Compare `σ * 1` with `σ * s` term by term: row `i` of `σ` vanishes in the columns of floor
  -- below `b`. At the indices where the row is nonzero, the column of `s` is that of `1`.
  conv_lhs => rw [← Matrix.mul_one σ.val.val]
  change _ ⬝ᵥ _ = _ ⬝ᵥ _
  rw [dotProduct_comm (σ.val.val i), dotProduct_comm (σ.val.val i)]
  apply dotProduct_eq_on_support
  intro k hk
  have h_floor : b ≤ floorOf k := by
    by_contra! h
    exact hk (by rw [mem_floorGroup.1 hσ i k h, one_apply_ne (by rintro rfl; exact hi h)])
  symm
  exact mem_partialUnitriangularGroup.1 hs k _ (by grind)
    (.inl (by simp [floorOf_besselFrame, level_besselFrame]))

/-- A column of `weylOn P p` on a floor that is exchanged has its only entry in the opposite row. -/
lemma weylOn_apply_of_swapped {p : ℕ → A} (i : 𝔰.BesselIndex) {k : 𝔰.BesselIndex}
    (hk : P (floorOf k) ∧ floorOf k < 𝔰.ϱ ∧ IsUnit (p (floorOf k))) :
    (weylOn P p : T.G).val i k =
      if i = oppositeIndex k then
        if level k < 𝔰.ϱ then p (floorOf k) else (star (p (floorOf k)))⁻¹ʳ
      else 0 := by
  simp only [weylOn, Pi.star_apply, isUnit_star, star_star, of_apply, hk, and_self, ↓reduceIte]

/-- A column of `weylOn P p` on a floor that is not exchanged is that of the identity matrix. -/
lemma weylOn_apply_of_not_swapped {p : ℕ → A} (i : 𝔰.BesselIndex) {k : 𝔰.BesselIndex}
    (hk : ¬(P (floorOf k) ∧ floorOf k < 𝔰.ϱ ∧ IsUnit (p (floorOf k)))) :
    (weylOn P p : T.G).val i k = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) i k := by
  simp only [weylOn, Pi.star_apply, isUnit_star, star_star, of_apply, hk, ↓reduceIte,
    Matrix.one_apply]

open scoped Classical in
/-- Right multiplication by a Weyl element exchanges and scales the selected columns of `M`
and leaves every other column in place. -/
lemma mul_weylOn_apply {ι : Type*} (M : Matrix ι 𝔰.BesselIndex A)
    (P : ℕ → Prop) (p : ℕ → A) (i : ι) (k : 𝔰.BesselIndex) :
    (M * (weylOn P p : T.G).val.val) i k =
      if P (floorOf k) ∧ floorOf k < 𝔰.ϱ ∧ IsUnit (p (floorOf k)) then
        M i (oppositeIndex k) * (if level k < 𝔰.ϱ then p (floorOf k) else (star (p (floorOf k)))⁻¹ʳ)
      else M i k := by
  by_cases hk : P (floorOf k) ∧ floorOf k < 𝔰.ϱ ∧ IsUnit (p (floorOf k))
  · simp [hk, Matrix.mul_apply, weylOn_apply_of_swapped _ hk, mul_ite]
  · simp [hk, Matrix.mul_apply, weylOn_apply_of_not_swapped _ hk, Matrix.one_apply, mul_ite]

/-- The Weyl element of floor `l` fixes the basis vectors of floor below `l`. -/
@[simp]
lemma floorWeyl_mem_floorGroup {c : A} : floorWeyl l c ∈ T.floorGroup l :=
  mem_floorGroup.2 fun i _ h_floor ↦
    weylOn_apply_of_not_swapped i fun h_swapped ↦ h_floor.ne h_swapped.left

/-- The Weyl element `weylOn P p`, which exchanges the hyperbolic pairs on the floors satisfying
`P`, commutes with the fixer `H_l` of the floors below `l` if `P` holds only below `l`. -/
lemma commute_weylOn {p : ℕ → A} (hP : ∀ a : ℕ, P a → a < l) (hs : s ∈ floorGroup l) :
    Commute (weylOn P p) s := by
  classical
  ext i k
  let sw (x : 𝔰.BesselIndex) : Prop := P (floorOf x) ∧ floorOf x < 𝔰.ϱ ∧ IsUnit (p (floorOf x))
  have h_entry (x y : 𝔰.BesselIndex) (hxy : floorOf x < l ∧ floorOf x < 𝔰.ϱ ∨ floorOf y < l) :
      s.val x y = if x = y then 1 else 0 := apply_eq_one_apply_of_mem_floorGroup hs x y hxy
  have h_weyl (x y : 𝔰.BesselIndex) : (weylOn P p : T.G).val x y =
      if sw y then if x = oppositeIndex y then
        if level y < 𝔰.ϱ then p (floorOf y) else (star (p (floorOf y)))⁻¹ʳ else 0
      else if x = y then 1 else 0 := by
    by_cases hy : sw y
    · rw [ite_eq_left hy, weylOn_apply_of_swapped x hy]
    · rw [ite_eq_right hy, weylOn_apply_of_not_swapped x hy, Matrix.one_apply]
  let i' : 𝔰.BesselIndex := if sw i then oppositeIndex i else i
  let k' : 𝔰.BesselIndex := if sw k then oppositeIndex k else k
  have h_left : (weylOn P p * s).val i k = (weylOn P p : T.G).val i i' * s.val i' k :=
    sum_eq_single i' fun j hj_ne ↦ by
      dsimp only
      rw [h_weyl i j]
      by_cases hj : sw j
      · rw [ite_eq_left hj, ite_eq_right (fun h_opp ↦ hj_ne ?_), zero_mul]
        have h_swi : sw i := by rw [h_opp]; simp only [sw, floorOf_oppositeIndex]; exact hj
        simp only [i', ite_eq_left h_swi, h_opp, oppositeIndex_oppositeIndex]
      · rw [ite_eq_right hj, ite_eq_right (fun h_eq ↦ hj_ne ?_), zero_mul]
        subst h_eq; exact (ite_eq_right hj).symm
  have h_right : (s * weylOn P p).val i k = s.val i k' * (weylOn P p : T.G).val k' k :=
    sum_eq_single k' fun j hj_ne ↦ by
      dsimp only
      rw [h_weyl j k]
      by_cases hk : sw k
      · rw [ite_eq_left hk, ite_eq_right (fun h_opp ↦ hj_ne ?_), mul_zero]
        simp only [k', ite_eq_left hk, h_opp]
      · rw [ite_eq_right hk, ite_eq_right (fun h_eq ↦ hj_ne ?_), mul_zero]
        simp only [k', ite_eq_right hk, h_eq]
  rw [h_left, h_right]
  by_cases hi : sw i <;> by_cases hk : sw k
  · have hsi : floorOf i' < l ∧ floorOf i' < 𝔰.ϱ := by
      simp only [i', ite_eq_left hi, floorOf_oppositeIndex]
      exact ⟨hP _ hi.1, hi.2.1⟩
    have hsk : floorOf k < l := hP _ hk.1
    have hi' : i' = oppositeIndex i := ite_eq_left hi
    have hk' : k' = oppositeIndex k := ite_eq_left hk
    rw [h_entry i' k (.inl hsi), h_entry i k' (.inl ⟨hP _ hi.1, hi.2.1⟩)]
    simp only [hi', hk']
    by_cases heq : oppositeIndex i = k
    · subst heq; simp only [oppositeIndex_oppositeIndex, ↓reduceIte, mul_one, one_mul]
    · have heq' : i ≠ oppositeIndex k := fun h ↦ heq (by rw [h, oppositeIndex_oppositeIndex])
      simp only [ite_eq_right heq, ite_eq_right heq', mul_zero, zero_mul]
  · have hsi : floorOf i' < l ∧ floorOf i' < 𝔰.ϱ := by
      simp only [i', ite_eq_left hi, floorOf_oppositeIndex]
      exact ⟨hP _ hi.1, hi.2.1⟩
    have hswi' : sw i' := by
      simp only [sw, i', ite_eq_left hi, floorOf_oppositeIndex]; exact hi
    have hne1 : i' ≠ k := fun h ↦ hk (h ▸ hswi')
    have hne2 : i ≠ k' := fun h ↦ hk (by simp only [k', ite_eq_right hk] at h; exact h ▸ hi)
    rw [h_entry i' k (.inl hsi), h_entry i k' (.inl ⟨hP _ hi.1, hi.2.1⟩),
      ite_eq_right hne1, ite_eq_right hne2, mul_zero, zero_mul]
  · have hsk : floorOf k < l := hP _ hk.1
    have hsi : floorOf k' < l ∧ floorOf k' < 𝔰.ϱ := by
      simp only [k', ite_eq_left hk, floorOf_oppositeIndex]
      exact ⟨hsk, hk.2.1⟩
    have hne1 : i' ≠ k := fun h ↦ hi
      (by simp only [i', ite_eq_right hi] at h; exact h.symm ▸ hk)
    have hswk' : sw k' := by
      simp only [sw, k', ite_eq_left hk, floorOf_oppositeIndex]; exact hk
    have hne2 : i ≠ k' := fun h ↦ hi (h.symm ▸ hswk')
    rw [h_entry i' k (.inr hsk), h_entry i k' (.inr hsi.1),
      ite_eq_right hne1, ite_eq_right hne2, mul_zero, zero_mul]
  · rw [show i' = i from ite_eq_right hi, show k' = k from ite_eq_right hk,
      h_weyl i i, h_weyl k k, ite_eq_right hi, ite_eq_right hk,
      ite_eq_left rfl, ite_eq_left rfl, one_mul, mul_one]

@[simp]
lemma weylElem_zero : (weylElem 0 p : T.G) = 1 := by
  ext i k
  exact weylOn_apply_of_not_swapped i
    (by simp only [not_lt_zero, Units.isUnit, and_true, false_and, not_false_eq_true])

/-- The Weyl element of length `l + 1` is the Weyl element of floor `l` with parameter `p l` times
the Weyl element of length `l`. -/
lemma weylElem_succ : (weylElem (l + 1) p : T.G) = floorWeyl l (p l).val * weylElem l p := by
  ext i k
  by_cases hk : floorOf k < l ∧ floorOf k < 𝔰.ϱ
  · -- The right factor moves `k` to the opposite index, which the left factor fixes.
    have h_floor : floorOf (oppositeIndex k) ≠ l := by
      rw [floorOf_oppositeIndex]; exact hk.left.ne
    by_cases hi : i = oppositeIndex k
    · simp only [weylElem, weylOn, Order.lt_add_one_iff, Units.isUnit, and_true,
        ↓inverse_star_unit, Pi.star_apply, isUnit_star, star_star, inverse_invertible,
        invOf_units, hi, of_apply, hk.left.le, hk, and_self, ↓reduceIte, floorWeyl,
        MulMemClass.mk_mul_mk, Units.val_mul, Matrix.mul_apply, mul_ite, ite_mul, one_mul,
        zero_mul, mul_zero, Finset.sum_ite_eq', Finset.mem_univ, h_floor, false_and]
    · simp only [weylElem, weylOn, Order.lt_add_one_iff, Units.isUnit, and_true,
        ↓inverse_star_unit, Pi.star_apply, isUnit_star, star_star, inverse_invertible,
        invOf_units, of_apply, hk.left.le, hk, and_self, ↓reduceIte, hi, floorWeyl,
        MulMemClass.mk_mul_mk, Units.val_mul, Matrix.mul_apply, mul_ite, ite_mul, one_mul,
        zero_mul, mul_zero, Finset.sum_ite_eq', Finset.mem_univ, h_floor, false_and,
        ite_self]
  · -- The right factor fixes `k`; both sides move it exactly when its floor is `l < ϱ`.
    have h_iff : floorOf k ≤ l ∧ floorOf k < 𝔰.ϱ ↔ floorOf k = l ∧ floorOf k < 𝔰.ϱ := by lia
    by_cases hl : floorOf k = l ∧ floorOf k < 𝔰.ϱ
    · simp only [weylElem, weylOn, ← hl.left, Order.lt_add_one_iff, Units.isUnit, and_true,
        ↓inverse_star_unit, Pi.star_apply, isUnit_star, star_star, inverse_invertible,
        invOf_units, of_apply, Std.le_refl, hl.right, and_self, ↓reduceIte, floorWeyl,
        MulMemClass.mk_mul_mk, Units.val_mul, Matrix.mul_apply, lt_self_iff_false, mul_ite,
        mul_one, mul_zero, Finset.sum_ite_eq', Finset.mem_univ]
    · simp only [weylElem, weylOn, Order.lt_add_one_iff, Units.isUnit, and_true,
        ↓inverse_star_unit, Pi.star_apply, isUnit_star, star_star, inverse_invertible,
        invOf_units, of_apply, h_iff, hl, ↓reduceIte, floorWeyl, MulMemClass.mk_mul_mk,
        Units.val_mul, Matrix.mul_apply, hk, mul_ite, mul_one, mul_zero, Finset.sum_ite_eq',
        Finset.mem_univ]

/-- A Weyl element depends only on the parameters of the pairs it exchanges. -/
lemma weylElem_eq_of_agree {q : ℕ → Aˣ} (h : ∀ a < l, p a = q a) :
    (weylElem l p : T.G) = weylElem l q := by
  ext i k
  by_cases hk : floorOf k < l <;> simp [weylElem, weylOn, *]

/-- The Weyl element of length `l` does not depend on the parameter `p l`. -/
@[simp]
lemma weylElem_update (c : Aˣ) : (weylElem l (Function.update p l c) : T.G) = weylElem l p :=
  weylElem_eq_of_agree fun _ ha ↦ Function.update_of_ne ha.ne ..

/-- The Weyl element of length `l` commutes with the fixer of the floors below `l`. -/
lemma commute_weylElem (hs : s ∈ floorGroup l) : Commute (weylElem l p) s :=
  commute_weylOn (fun _ ha ↦ ha) hs

/-- Conjugating the parameters inverts the Weyl element. -/
@[simp]
lemma weylElem_star : (weylElem l (star p) : T.G) = (weylElem l p)⁻¹ := by
  -- In `weylOn` the inverse matrix is by definition the one with the parameters `star p`.
  ext
  rfl

lemma weylElem_left_comm (hs : s ∈ floorGroup l) :
    weylElem l p * (s * g) = s * (weylElem l p * g) :=
  (commute_weylElem hs).left_comm g

lemma weylElem_inv_left_comm (hs : s ∈ floorGroup l) :
    (weylElem l p)⁻¹ * (s * g) = s * ((weylElem l p)⁻¹ * g) :=
  (commute_weylElem hs).inv_left.left_comm g

/-- For `s` in the fixer `H_l` of the floors below `l`, the Weyl element `w_p` of length `l` and its
inverse can be moved past `s` on the left. -/
lemma weylElem_mul_left_comm (hs : s ∈ floorGroup l) :
    weylElem l p * (s * g) = s * (weylElem l p * g) ∧
    (weylElem l p)⁻¹ * (s * g) = s * ((weylElem l p)⁻¹ * g) :=
  ⟨weylElem_left_comm hs, weylElem_inv_left_comm hs⟩

/-- The diagonal entry `b` of the frame Gram matrix of `σ τ⁻¹`, that is the hermitian product of
`f_b` and `σ τ⁻¹ f_b`, is the hermitian product of `σ⁻¹ f_b = orbitPoint b σ` and
`τ⁻¹ f_b = orbitPoint b τ` (for isometries `σ τ : T.G` and `b : Fin 𝔰.n`), where `f_b` is the frame
vector number `b`. -/
lemma frameGram_eq_form_orbitPoint :
    T.frameGram (σ * τ⁻¹).val b b = star (orbitPoint b σ) ⬝ᵥ T.splitGram *ᵥ orbitPoint b τ := by
  -- Move both vectors by the isometry `σ`, which takes `orbitPoint b σ` to the frame vector.
  rw [← U.form_mulVec_mulVec σ, orbitPoint_eq_iff.1 rfl, orbitPoint, mulVec_mulVec, mulVec_mulVec]
  simp only [frameGram, coe_mul, InvMemClass.coe_inv, Units.val_mul, coe_units_inv,
    submatrix_apply, Pi.star_single, star_one, mul_assoc, mulVec_single, MulOpposite.op_one,
    one_smul, single_dotProduct, col_apply, one_mul]

/-- The hermitian product of `e_a` with its image `c e*_a` under the Weyl element of floor `a`
is `c`. -/
lemma frameGram_floorWeyl (c : Aˣ) :
    T.frameGram (floorWeyl a c : T.G).val a.castSucc a.castSucc = c := by
  simp only [frameGram, floorWeyl, submatrix_apply, besselFrame_castSucc, mul_weylOn_apply]
  simp [level, splitGram_eq_ite_oppositeIndex]

/-! ### Leading minors of the products of the frame vectors -/

/-- An element `s` of `Q_l` changes each frame vector of index below `l` by a combination of the
earlier ones. So a right factor `s` keeps the leading minors of order `j ≤ l` of the block of a
matrix `M` on the frame. -/
lemma leadingMinor_submatrix_mul_of_mem_partialUnitriangularGroup {l : ℕ} {s : T.G}
    (hs : s ∈ partialUnitriangularGroup l) {j : Fin (𝔰.n + 1)} (hj : j.val ≤ l)
    (M : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) :
    leadingMinor ((M * s.val.val).submatrix 𝔰.besselFrame 𝔰.besselFrame) j =
      leadingMinor (M.submatrix 𝔰.besselFrame 𝔰.besselFrame) j := by
  let f (a : Fin j) : 𝔰.BesselIndex := 𝔰.besselFrame (a.castLE j.is_le)
  have f_injective : Function.Injective f := besselFrame_injective.comp (Fin.castLE_injective _)
  have f_level (a : Fin j) : level (f a) = a := level_besselFrame _
  have f_floor (a : Fin j) : floorOf (f a) = a := floorOf_besselFrame
  have h_column (i : 𝔰.BesselIndex) (b : Fin j) (hi : b ≤ level i) :
      s.val.val i (f b) = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) i (f b) :=
    have hb : b.val < l := b.is_lt.trans_le hj
    mem_partialUnitriangularGroup.1 hs i (f b) ((f_level b).trans_le hi)
      (.inl ⟨(f_floor b).trans_lt hb, (f_level b).trans_lt hb⟩)
  -- An index of level below `b` is that of an earlier frame vector.
  have h_level (i : 𝔰.BesselIndex) (hi : i ∉ Set.range f) (b : Fin j) : b ≤ level i := by
    by_contra! h_lt
    have h_floor : floorOf i = level i := by
      have := floorOf_level_cases i; have := b.is_lt; lia
    exact hi ⟨⟨level i, h_lt.trans b.is_lt⟩, (eq_besselFrame_of_floorOf_eq h_floor le_rfl).symm⟩
  have h_block : (M * s.val.val).submatrix f f = M.submatrix f f * s.val.val.submatrix f f := by
    refine submatrix_mul_of_eq_zero_outside _ _ f f f f_injective fun _ b i hi ↦ ?_
    rw [h_column i b (h_level i hi b), one_apply_ne fun h_eq ↦ hi ⟨b, h_eq.symm⟩, mul_zero]
  have h_det : (s.val.val.submatrix f f).det = 1 := by
    refine det_eq_one_of_unitriangular (lev := Fin.val) fun a b hba ↦ ?_
    rw [← submatrix_one f f_injective]
    exact h_column (f a) b (hba.trans_eq (f_level a).symm)
  exact det_eq_of_eq_mul_det_one _ h_det h_block

/-- The leading minors of order `j ≤ l` of the matrix `T.frameGram γ` of the products
`h(f_a, γ f_b)` of the frame vectors, h being the form `splitGram T`, do not change when `γ` is
multiplied on either side by elements of `Q_l`. Here `Q_l` = `partialUnitriangularGroup l`, whose
elements agree with the identity at each entry (i, j) with level j ≤ level i whose column j has
floor and level below l. -/
lemma leadingMinor_frameGram_partialUnitriangularGroup_mul {l : ℕ} {s s' : T.G}
    (hs : s ∈ partialUnitriangularGroup l) (hs' : s' ∈ partialUnitriangularGroup l) (γ : T.G)
    {j : Fin (𝔰.n + 1)} (hj : j.val ≤ l) :
    leadingMinor (T.frameGram (s⁻¹ * γ * s').val.val) j =
      leadingMinor (T.frameGram γ.val.val) j := by
  -- With `J = T.splitGram` we have `J s⁻¹ = sᴴ J`, so the conjugate transpose turns the factor on
  -- the left into a factor on the right.
  have h_form : T.splitGram * (s⁻¹ * γ * s').val.val =
      ((T.splitGram * γ.val.val)ᴴ * s.val.val)ᴴ * s'.val.val := by
    simp only [coe_mul, InvMemClass.coe_inv, Units.val_mul, coe_units_inv, ← Matrix.mul_assoc,
      conjTranspose_mul, conjTranspose_conjTranspose, ← U.form_mul_inv_eq]
  have h_conj (M : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) :
      leadingMinor (Mᴴ.submatrix 𝔰.besselFrame 𝔰.besselFrame) j =
        star (leadingMinor (M.submatrix 𝔰.besselFrame 𝔰.besselFrame) j) :=
    det_conjTranspose ((M.submatrix _ _).submatrix _ _)
  rw [frameGram, h_form, leadingMinor_submatrix_mul_of_mem_partialUnitriangularGroup hs' hj, h_conj,
    leadingMinor_submatrix_mul_of_mem_partialUnitriangularGroup hs hj, h_conj, star_star, frameGram]

/-- The leading minors of the matrix `T.frameGram γ` of the products `h(f_a, γ f_b)` of the frame
vectors do not change when `γ` is multiplied on either side by elements of the unipotent group
`New.Shape.Form.besselU`. -/
lemma _root_.New.Filling.leadingMinor_frameGram_besselU_mul {γ u u' : T.G} (hu : u ∈ T.besselU)
    (hu' : u' ∈ T.besselU) (j : Fin (𝔰.ϱ + 1 + 1)) :
    leadingMinor (T.frameGram (u⁻¹ * γ * u').val) j = leadingMinor (T.frameGram γ) j :=
  leadingMinor_frameGram_partialUnitriangularGroup_mul
    (mem_partialUnitriangularGroup_of_mem_besselU hu)
    (mem_partialUnitriangularGroup_of_mem_besselU hu') γ le_rfl

/-- Let `l ≤ ϱ`, and let `w_p = weylElem l p` be the isometry with `e_a ↦ p a • e*_a` and
`e*_a ↦ e_a / conj (p a)` for `a < l`, fixing the other basis vectors. For `j ≤ l`, the leading
principal minor of order `j` of `frameGram w_p` equals `p 0 * ⋯ * p (j - 1)`. Here `frameGram w_p`
is the matrix of the products `h(f_a, w_p f_b)`, and `f = (e_0, …, e_{ϱ-1}, z)` is the frame. -/
lemma leadingMinor_frameGram_weylElem {l : ℕ} (hl : l ≤ 𝔰.ϱ) (p : ℕ → Aˣ) {j : Fin (𝔰.n + 1)}
    (hj : j.val ≤ l) :
    leadingMinor (T.frameGram (weylElem l p : T.G).val.val) j =
      (∏ a ∈ Finset.range j, p a).val := by
  -- `w_p` sends `e_b` to `p b` times `e*_b`, so the block is diagonal.
  have h_frame (a : Fin j) : 𝔰.besselFrame (a.castLE j.is_le) = inl ⟨a, by lia⟩ :=
    besselFrame_castSucc ⟨a, by lia⟩
  have h_block : (T.frameGram (weylElem l p : T.G).val.val).submatrix (Fin.castLE j.is_le)
      (Fin.castLE j.is_le) = diagonal fun a : Fin j ↦ (p a).val := by
    ext a b
    simp only [frameGram, submatrix_apply, h_frame]
    rw [splitGram_mul_apply_inl]
    let k : Fin 𝔰.ϱ := ⟨b, by lia⟩
    rw [weylElem, weylOn_apply_of_swapped _ (k := inl k)
      ⟨show b.val < l by lia, k.is_lt, (p _).isUnit⟩]
    simp only [oppositeIndex, level, floorOf, elim_inl, Fin.is_lt, ↓reduceIte, diagonal_apply,
      inr.injEq, Fin.ext_iff]
    by_cases h : a = b <;> simp_all [k, Fin.ext_iff]
  rw [leadingMinor, h_block, det_diagonal, Units.coe_prod, Finset.prod_range]

/-! ### Action on coordinates of vectors -/

/-- An index has level at most `ϱ` or is some `e*_b`, of floor `b`. -/
lemma level_le_or_exists_eq_eStar (k : 𝔰.BesselIndex) :
    level k ≤ 𝔰.ϱ ∨ ∃ b : Fin 𝔰.ϱ, k = eStar b ∧ floorOf k = b := by
  obtain a | m | a := k <;> simp [level, floorOf]

/-- If the coordinates `e*_b` of `v : 𝔰.BesselIndex → A` with `b < j` vanish (`j : ℕ`), an element
of the unitriangular group `𝒩_{<j}` of the floors below `j` keeps the coordinates of `v` of floor at
least `j` and those of level above `ϱ`. -/
lemma unipotentBelow_mulVec_apply (hg : g ∈ unipotentBelow j)
    (hv : ∀ b : Fin 𝔰.ϱ, b < j → v (eStar b) = 0) (i : 𝔰.BesselIndex)
    (hi : j ≤ floorOf i ∨ 𝔰.ϱ < level i) :
    (g *ᵥ v) i = v i := by
  -- Compare with `1 *ᵥ v`. Where row `i` of `g` may differ from `1`, `v` vanishes.
  conv_rhs => rw [← one_mulVec v]
  apply dotProduct_eq_on_support
  intro k hk
  apply mem_unipotentBelow.1 hg
  obtain hk_le | ⟨b, rfl, hkb⟩ := level_le_or_exists_eq_eStar k <;> split_ifs <;> grind

/-- Let `s` lie in `Q_j`, the group of isometries that are unitriangular in the columns of floor
and level below `j`. For `a < j`, the row `e*_a` of `s` is that of `1` outside the columns `e*_b`
with `b < a`. -/
lemma partialUnitriangularGroup_apply_eStar (hs : s ∈ partialUnitriangularGroup j) (ha : a < j)
    (k : 𝔰.BesselIndex) (hk : ∀ b : Fin 𝔰.ϱ, b < a → k ≠ eStar b) :
    s.val (eStar a) k = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex A) (eStar a) k := by
  -- The column `inl a` of `s⁻¹` agrees with `1` at levels at least `a`.
  -- Every row where the Gram column `k` is nonzero has such a level.
  apply row_eq_one_of_inv_col (inl a) k a.is_lt
  intro x hx
  apply mem_partialUnitriangularGroup.1 (inv_mem hs) x (inl a) _ (.inl ⟨ha, ha⟩)
  obtain b | m | b := x
  · by_contra! hb
    exact hx (by rw [splitGram_eq_ite_oppositeIndex (inl b) k (.inl b.is_lt),
      ite_eq_right (show oppositeIndex (inl b) ≠ k from (hk b hb).symm)])
  · exact a.is_lt.le
  · change (a : ℕ) ≤ 2 * 𝔰.ϱ - b
    omega

/-- For `a < j`, an element `s` of `Q_j` (`partialUnitriangularGroup j`, `j : ℕ`) keeps, under `*ᵥ`,
the coordinate `e*_a` of a `v : 𝔰.BesselIndex → A` that is a vector whose coordinates `e*_b` with
`b < a` vanish. -/
lemma partialUnitriangularGroup_mulVec_eStar (hs : s ∈ partialUnitriangularGroup j) (ha : a < j)
    (hv : ∀ b : Fin 𝔰.ϱ, b < a → v (eStar b) = 0) : (s.val.val *ᵥ v) (eStar a) = v (eStar a) := by
  -- The two rows agree wherever `v` is nonzero.
  conv_rhs => rw [← one_mulVec v]
  exact dotProduct_eq_on_support fun k hk ↦
    partialUnitriangularGroup_apply_eStar hs ha k fun b hb h_eq ↦ hk (h_eq ▸ hv b hb)

end New.Floors.FloorGroups

/-!
## The Bessel subgroup, its unipotent part and the simple root sum

`New.frameMatrix` is the square block of a matrix on the frame.
-/

namespace New

open Finset Matrix Subgroup Fin Floors FloorGroups

variable {R : Type} [CommRing R] [StarRing R] {𝔰 : Shape} {T : 𝔰.Form R}

/-- The group H: the floor group H_{ϱ+1}, the fixer of all basis vectors outside `κ`. -/
abbrev Shape.Form.besselH : Subgroup T.G := T.floorGroup 𝔰.n

/-- The Bessel subgroup S: the group `Q_{ϱ+1}` of the isometries whose columns at the frame vectors
agree with those of the identity matrix on and below the diagonal blocks. It contains the
unipotent group `New.Shape.Form.besselU` and H = `New.Shape.Form.besselH`. -/
abbrev Shape.Form.besselS : Subgroup T.G := T.partialUnitriangularGroup 𝔰.n

export Shape.Form (besselH besselS)

variable {Q : Subring R} {g : Rˣ} {h u s : T.G}

/-- If the middle block `T` is hermitian and invertible, so is the whole Gram matrix. -/
instance [h : IsHermitianUnit T] : IsHermitianUnit T.splitGram where
  conjTranspose_eq := by
    simp only [splitGram, fromBlocks_conjTranspose, conjTranspose_zero,
      conjTranspose_fromRows_eq_fromCols_conjTranspose, conjTranspose_one,
      conjTranspose_fromCols_eq_fromRows_conjTranspose, h.conjTranspose_eq]
  isUnit := .of_mul_eq_one _ (splitGram_mul_splitGram_eq_one h.isUnit.mul_val_inv)

/-! ### Elements of `H` and the frame -/

omit [StarRing R] in
/-- A right factor that fixes the basis vectors outside `κ` does not change the image of the
frame. -/
lemma frameImage_mul_of_mem_agreeWithOneOn {g : GL 𝔰.BesselIndex R}
    (hg : g ∈ agreeWithOneOn (fun _ j : 𝔰.BesselIndex ↦ floorOf j < 𝔰.n) R
      fun _ _ _ hj ↦ Or.inr hj)
    (x : Matrix 𝔰.BesselIndex 𝔰.BesselIndex R) :
    frameImage (x * g.val) = frameImage x := by
  ext i b
  have h_col (k : 𝔰.BesselIndex) : g.val k (𝔰.besselFrame b) =
      (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex R) k (𝔰.besselFrame b) :=
    hg k _ (floorOf_besselFrame.trans_lt b.is_lt)
  simp only [submatrix_apply, id_eq, Matrix.mul_apply, h_col, Matrix.one_apply, mul_ite,
    mul_one, mul_zero, sum_ite_eq', mem_univ, ↓reduceIte]

/-- A right factor in `H` does not change the block on the frame. -/
lemma frameMatrix_mul_besselH (s : T.G) (hh : h ∈ besselH) : frameMatrix (s * h) = frameMatrix s :=
  funext fun i ↦ congrFun (frameImage_mul_of_mem_agreeWithOneOn hh s) (𝔰.besselFrame i)

lemma mem_besselFixer_of_mem_besselU0 (hu : u ∈ besselU0) : u ∈ besselFixer :=
  (mem_inf.1 hu).right

/-- An element of `H` fixes the frame vectors, whose floors are at most `ϱ`. -/
lemma mem_besselFixer_of_mem_besselH (hh : h ∈ besselH) : h ∈ besselFixer := by
  rintro i _ ⟨b, rfl⟩
  exact mem_floorGroup.1 hh i _ (floorOf_besselFrame.trans_lt b.is_lt)

private lemma exists_besselFrame_of_floor_and_level {j : 𝔰.BesselIndex}
    (h_floor : floorOf j < 𝔰.n) (h_level : BesselIndex.level j < 𝔰.n) :
    ∃ a : Fin (𝔰.ϱ + 1), 𝔰.besselFrame a = j :=
  ⟨⟨floorOf j, h_floor⟩, (eq_besselFrame_of_floorOf_eq rfl
    (by have := floorOf_level_cases j; lia)).symm⟩

/-- An isometry that fixes every frame vector lies in the Bessel subgroup `S`. -/
lemma mem_besselS_of_mem_besselFixer (hs : s ∈ besselFixer) : s ∈ besselS := by
  refine mem_partialUnitriangularGroup.2 fun i j _ hj ↦ ?_
  -- The columns on which `S` puts a condition are those of the frame vectors.
  obtain ⟨h_floor, h_level⟩ := hj.resolve_right (lt_irrefl _)
  exact hs i j (exists_besselFrame_of_floor_and_level h_floor h_level)

/-- An isometry `s` lies in the Bessel subgroup `besselS` if and only if, for every vector `f_a` of
the frame `(e_0, …, e_{ϱ-1}, z)`, the difference `s f_a - f_a` lies in the span of the earlier frame
vectors. -/
lemma Filling.mem_besselS_iff_frameImage :
    s ∈ besselS ↔
      ∀ (i : SplitIndex 𝔰.ϱ 𝔰.MiddleIndex) (a : Fin (𝔰.ϱ + 1)),
        a.val ≤ BesselIndex.level i → frameImage s.val.val i a = frameImage 1 i a := by
  rw [mem_partialUnitriangularGroup]
  constructor
  · intro h i a ha
    exact h i (𝔰.besselFrame a) (by rwa [level_besselFrame])
      (Or.inl (by simp only [floorOf_besselFrame, a.isLt, level_besselFrame, and_self]))
  · intro h i j hji hj
    -- the condition on `j` says that it indexes a frame vector
    obtain ⟨h_floor, h_level⟩ := hj.resolve_right (lt_irrefl _)
    obtain ⟨a, rfl⟩ := exists_besselFrame_of_floor_and_level h_floor h_level
    exact h i a (by rwa [level_besselFrame] at hji)

/-! ### `S` normalises `𝖴` -/

/-- The Bessel subgroup `S`, whose elements are unitriangular in the columns of the frame vectors,
normalises the group `𝖴` of block unitriangular isometries. -/
lemma besselS_le_normalizer_besselU : T.besselS ≤ normalizer T.besselU := by
  refine le_normalizer_iff.2 fun s hs u hu ↦ ?_
  -- `s` is block upper triangular for the levels: `partialUnitriangularGroup_apply_eStar` gives the
  -- rows of the `eStar a`, the definition of `partialUnitriangularGroup` the columns of the frame.
  have h_tri : s.val.val.BlockTriangular BesselIndex.level := by
    intro i j hij
    rw [← one_apply_ne (α := R) (ne_of_apply_ne BesselIndex.level hij.ne')]
    by_cases hj : floorOf j < 𝔰.n ∧ BesselIndex.level j < 𝔰.n
    · exact mem_partialUnitriangularGroup.1 hs i j hij.le (.inl hj)
    · -- Here `j` has level at least `ϱ`, so `i` is some `e*_a`.
      have h_cases := floorOf_level_cases j
      obtain ⟨a, rfl, -⟩ := (level_le_or_exists_eq_eStar i).resolve_left (fun hle ↦ by lia)
      refine partialUnitriangularGroup_apply_eStar hs (by lia) j fun b hb h_eq ↦ ?_
      simp only [h_eq, BesselIndex.level, Sum.elim_inr] at hij
      lia
  -- So is `s⁻¹`, and conjugation by such a matrix keeps the unitriangular group.
  exact conj_mem_unitriangular (d := s.val⁻¹) ((parabolic _).inv_mem h_tri) (mem_subgroupOf.1 hu)

/-! ### The additive character on `S` -/

/-- For s in the unitary group, the sum σ(s) of the entries just above the diagonal of the block
of s on the frame (e_0, …, e_{ϱ-1}, z). -/
def simpleRootSum (s : T.G) : R :=
  ∑ a : Fin 𝔰.ϱ, s.val (𝔰.besselFrame (castSucc a)) (𝔰.besselFrame a.succ)

/-- The sum σ of the entries just above the diagonal on the frame is additive on the Bessel
subgroup S. -/
lemma simpleRootSum_mul (hh : h ∈ besselS) (hu : u ∈ besselS) :
    simpleRootSum (h * u) = simpleRootSum h + simpleRootSum u := by
  rw [simpleRootSum, simpleRootSum, simpleRootSum, ← sum_add_distrib]
  refine sum_congr rfl fun i _ ↦ ?_
  set x : 𝔰.BesselIndex := 𝔰.besselFrame i.castSucc
  set y : 𝔰.BesselIndex := 𝔰.besselFrame i.succ
  -- In the entry `(x, y)` of the product only the terms with summed index `y` or `x` survive: the
  -- column of `u` vanishes at the other indices of level `> i`, and the row of `h` at those of
  -- level `< i`.
  have h_entry : (h * u).val x y = h.val x y * u.val y y + h.val x x * u.val x y := by
    refine Fintype.sum_eq_add y x (besselFrame_injective.ne i.castSucc_lt_succ.ne.symm)
      fun z ⟨hzy, hzx⟩ ↦ ?_
    dsimp only
    by_cases hz : BesselIndex.level z ≤ i.val
    · have h_cases := floorOf_level_cases z
      rw [mem_partialUnitriangularGroup.1 hh x z ((level_besselFrame i.castSucc).symm ▸ hz)
        (.inl ⟨by lia, by lia⟩), one_apply_ne hzx.symm, zero_mul]
    · have h_col : u.val z y = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex R) z y :=
        Filling.mem_besselS_iff_frameImage.mp hu z i.succ (Nat.lt_of_not_ge hz)
      rw [h_col, one_apply_ne hzy, mul_zero]
  have diag {s : T.G} (hs : s ∈ besselS) (b : Fin 𝔰.n) :
      s.val (𝔰.besselFrame b) (𝔰.besselFrame b) = 1 :=
    (Filling.mem_besselS_iff_frameImage.mp hs _ b (level_besselFrame b).ge).trans (one_apply_eq _)
  rw [h_entry, diag hh i.castSucc, diag hu i.succ, mul_one, one_mul]

/-! ### Principal congruence subgroups and the factor `H` -/

/-- If `u` is integral over `Q` and congruent to `1` modulo `g` (`New.unitaryLevel`), the sum of
its entries just above the diagonal on the frame is divisible by `g` in `Q`. -/
lemma inv_mul_simpleRootSum_mem (hu : u ∈ unitaryLevel Q g T.splitGram) :
    (g⁻¹ : Rˣ) * simpleRootSum u ∈ Q := by
  rw [simpleRootSum, mul_sum]
  refine sum_mem fun a _ ↦ ?_
  have h_entry :
      (g⁻¹ • (u.val.val - 1)) (𝔰.besselFrame a.castSucc) (𝔰.besselFrame a.succ) ∈ Q :=
    (mem_unitaryLevel_def.1 hu).right _ _
  have h_ne : 𝔰.besselFrame a.castSucc ≠ 𝔰.besselFrame a.succ :=
    besselFrame_injective.ne castSucc_lt_succ.ne
  rwa [Matrix.smul_apply, Matrix.sub_apply, one_apply_ne h_ne, sub_zero, Units.smul_def,
    smul_eq_mul] at h_entry

/-- The sum of the entries just above the diagonal on the frame vanishes on `H`, whose columns at
the frame vectors are those of `1`. -/
lemma simpleRootSum_eq_zero_of_mem_besselH (hh : h ∈ besselH) : simpleRootSum h = 0 :=
  sum_eq_zero fun a _ ↦ (mem_floorGroup.1 hh _ _
    (floorOf_besselFrame.trans_lt a.succ.is_lt)).trans
    (one_apply_ne (besselFrame_injective.ne a.castSucc_lt_succ.ne))

/-- For `u` in `𝖴` and `h` in `H`, each entry of `h` is the entry at the same place of `u * h` or
of `1`. -/
lemma besselH_apply_eq_or (hu : u ∈ besselU) (hh : h ∈ besselH) (i j : 𝔰.BesselIndex) :
    h.val i j = (u * h).val i j ∨ h.val i j = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex R) i j := by
  have h_lt : 𝔰.ϱ < 𝔰.n := Nat.lt_succ_self _
  by_cases h_col : floorOf j < 𝔰.n
  · exact .inr (apply_eq_one_apply_of_mem_floorGroup hh i j (.inr h_col))
  by_cases h_row : floorOf i < 𝔰.ϱ
  · exact .inr (apply_eq_one_apply_of_mem_floorGroup hh i j (.inl ⟨h_row.trans h_lt, h_row⟩))
  -- Now `i` lies in the middle block, of level `ϱ`, and only the term `x = i` of the product stays.
  have h_level_i : BesselIndex.level i = 𝔰.ϱ := by have := floorOf_level_cases i; lia
  refine .inl (Eq.symm ?_)
  rw [Subgroup.coe_mul, Units.val_mul, Matrix.mul_apply, Fintype.sum_eq_single i fun x hx ↦ ?_,
    mem_besselU.1 hu i i le_rfl, one_apply_eq, one_mul]
  by_cases hx_floor : floorOf x < 𝔰.ϱ
  · rw [apply_eq_one_apply_of_mem_floorGroup hh x j (.inl ⟨hx_floor.trans h_lt, hx_floor⟩),
      one_apply_ne (by rintro rfl; lia), mul_zero]
  · rw [mem_besselU.1 hu i x (by have := floorOf_level_cases x; lia), one_apply_ne (Ne.symm hx),
      zero_mul]

/-- Let `u` be block unitriangular (`besselU`) and let `h` fix the basis vectors outside `κ`
(`besselH`). If `u * h` is integral over `Q` and congruent to `1` modulo `g` (`New.unitaryLevel`),
then so is `h`. -/
lemma mem_unitaryLevel_of_besselU_mul (hu : u ∈ besselU) (hh : h ∈ besselH)
    (h_level : u * h ∈ unitaryLevel Q g T.splitGram) :
    h ∈ unitaryLevel Q g T.splitGram := by
  -- Apply `besselH_apply_eq_or` to `h`, and to `h⁻¹`: the inverse `(u * h)⁻¹` is `h⁻¹` multiplied
  -- on the left by the unipotent `h⁻¹ * u⁻¹ * h`.
  have h_normalizer : h⁻¹ ∈ normalizer T.besselU :=
    besselS_le_normalizer_besselU (mem_partialUnitriangularGroup_of_mem_floorGroup (inv_mem hh))
  have h_conj : h⁻¹ * u⁻¹ * h ∈ besselU := by
    simpa only [inv_inv] using (mem_normalizer_iff.1 h_normalizer u⁻¹).1 (inv_mem hu)
  have h_inv : (u * h)⁻¹ = h⁻¹ * u⁻¹ * h * h⁻¹ := by group
  have h_inv_entry (i j : 𝔰.BesselIndex) : h⁻¹.val i j = (u * h)⁻¹.val i j ∨
      h⁻¹.val i j = (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex R) i j := by
    rw [h_inv]
    exact besselH_apply_eq_or h_conj (inv_mem hh) i j
  have h_one (i j : 𝔰.BesselIndex) : (1 : Matrix 𝔰.BesselIndex 𝔰.BesselIndex R) i j ∈ Q := by
    simp only [Matrix.one_apply, apply_ite (· ∈ Q), one_mem, zero_mem, ite_self]
  obtain ⟨h_intGL, h_congr⟩ := mem_unitaryLevel_def.1 h_level
  obtain ⟨h_int, h_inv_int⟩ : (∀ i j : 𝔰.BesselIndex, (u * h).val i j ∈ Q) ∧
      ∀ i j : 𝔰.BesselIndex, (u * h)⁻¹.val i j ∈ Q := mem_intGL.1 h_intGL
  -- So integrality and the congruence modulo `g` pass from `u * h` to `h`, entry by entry.
  refine mem_unitaryLevel_def.2 ⟨mem_intGL.2 ⟨fun i j ↦ ?_, fun i j ↦ ?_⟩, fun i j ↦ ?_⟩
  · obtain h_eq | h_eq := besselH_apply_eq_or hu hh i j <;> rw [h_eq]
    exacts [h_int i j, h_one i j]
  · rw [← Subgroup.coe_inv]
    obtain h_eq | h_eq := h_inv_entry i j <;> rw [h_eq]
    exacts [h_inv_int i j, h_one i j]
  · rw [Matrix.smul_apply, Matrix.sub_apply]
    obtain h_eq | h_eq := besselH_apply_eq_or hu hh i j <;> rw [h_eq]
    · exact h_congr i j
    · simp only [sub_self, smul_zero, zero_mem]

/-- Let `h` and `h'` lie in `H`. If `u * (h * y)` and `u' * (h' * y)` lie in the congruence subgroup
`K(g)` (`New.unitaryLevel`) for some `u` and `u'` in `𝖴`, then so does `h' / h`. So the `h` in `H`
with `h * y` in `𝖴 K(g)` lie in one right coset of `H ∩ K(g)`. -/
lemma div_mem_unitaryLevel_of_besselU_mul {h h' u u' y : T.G} (hh : h ∈ T.besselH)
    (hh' : h' ∈ T.besselH) (hu : u ∈ T.besselU) (hu' : u' ∈ T.besselU)
    (h_level : u * (h * y) ∈ unitaryLevel Q g T.splitGram)
    (h_level' : u' * (h' * y) ∈ unitaryLevel Q g T.splitGram) :
    h' / h ∈ unitaryLevel Q g T.splitGram := by
  -- `H` normalises `𝖴`, so the quotient of the two elements of `K(g)` lies in `𝖴 (h' / h)`.
  have h_div : h' / h ∈ T.besselH := div_mem hh' hh
  have h_conj : h' / h * u⁻¹ * (h' / h)⁻¹ ∈ T.besselU :=
    (besselS_le_normalizer_besselU (mem_partialUnitriangularGroup_of_mem_floorGroup h_div) u⁻¹).1
      (inv_mem hu)
  have h_eq : u' * (h' / h * u⁻¹ * (h' / h)⁻¹) * (h' / h) = u' * (h' * y) / (u * (h * y)) := by
    simp only [div_eq_mul_inv, mul_assoc, _root_.mul_inv_rev, inv_inv, inv_mul_cancel_left,
      mul_inv_cancel, mul_one, mul_inv_cancel_left]
  exact mem_unitaryLevel_of_besselU_mul (mul_mem hu' h_conj) h_div
    (by simpa only [h_eq] using div_mem h_level' h_level)

end New

end