l3lab/ntp-mathlib huggingface dataset:
/-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when
`f` and `g` monovary together. Stated by permuting the entries of `g`. -/
theorem MonovaryOn.sum_smul_comp_perm_le_sum_smul (hfg : MonovaryOn f g s)
    (hσ : { x | σ x ≠ x } ⊆ s) : (∑ i in s, f i • g (σ i)) ≤ ∑ i in s, f i • g i := by
  classical
    revert hσ σ hfg
    -- Porting note: Specify `p` to get around `∀ {σ}` in the current goal.
    apply Finset.induction_on_max_value (fun i ↦ toLex (g i, f i))
      (p := fun t ↦ ∀ {σ : Perm ι}, MonovaryOn f g t → { x | σ x ≠ x } ⊆ t →
        (∑ i in t, f i • g (σ i)) ≤ ∑ i in t, f i • g i) s
    · simp only [le_rfl, Finset.sum_empty, imp_true_iff]
    intro a s has hamax hind σ hfg hσ
    set τ : Perm ι := σ.trans (swap a (σ a)) with hτ
    have hτs : { x | τ x ≠ x } ⊆ s := by
      intro x hx
      simp only [Ne.def, Set.mem_setOf_eq, Equiv.coe_trans, Equiv.swap_comp_apply] at hx
      split_ifs at hx with h₁ h₂
      · obtain rfl | hax := eq_or_ne x a
        · contradiction
        · exact mem_of_mem_insert_of_ne (hσ fun h ↦ hax <| h.symm.trans h₁) hax
      · exact (hx <| σ.injective h₂.symm).elim
      · exact mem_of_mem_insert_of_ne (hσ hx) (ne_of_apply_ne _ h₂)
    specialize hind (hfg.subset <| subset_insert _ _) hτs
    simp_rw [sum_insert has]
    refine' le_trans _ (add_le_add_left hind _)
    obtain hσa | hσa := eq_or_ne a (σ a)
    · rw [hτ, ← hσa, swap_self, trans_refl]
    have h1s : σ⁻¹ a ∈ s := by
      rw [Ne.def, ← inv_eq_iff_eq] at hσa
      refine' mem_of_mem_insert_of_ne (hσ fun h ↦ hσa _) hσa
      

Proof state:
ι : Type u_1
α : Type u_2
β : Type u_3
inst✝³ : LinearOrderedRing α
inst✝² : LinearOrderedAddCommGroup β
inst✝¹ : Module α β
inst✝ : OrderedSMul α β
s✝ : Finset ι
f : ι → α
g : ι → β
a : ι
s : Finset ι
has : a ∉ s
hamax : ∀ x ∈ s, toLex (g x, f x) ≤ toLex (g a, f a)
σ : Perm ι
hfg : MonovaryOn f g ↑(insert a s)
hσ : {x | σ x ≠ x} ⊆ ↑(insert a s)
τ : Perm ι := σ.trans (Equiv.swap a (σ a))
hτ : τ = σ.trans (Equiv.swap a (σ a))
hτs : {x | τ x ≠ x} ⊆ ↑s
hind : ∑ i in s, f i • g (τ i) ≤ ∑ i in s, f i • g i
hσa : ¬σ⁻¹ a = a
h : σ (σ⁻¹ a) = σ⁻¹ a
⊢ σ⁻¹ a = a

Next tactic:
rwa [apply_inv_self, eq_comm] at h
/-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when
`f` and `g` monovary together. Stated by permuting the entries of `g`. -/
theorem MonovaryOn.sum_smul_comp_perm_le_sum_smul (hfg : MonovaryOn f g s)
    (hσ : { x | σ x ≠ x } ⊆ s) : (∑ i in s, f i • g (σ i)) ≤ ∑ i in s, f i • g i := by
  classical
    revert hσ σ hfg
    -- Porting note: Specify `p` to get around `∀ {σ}` in the current goal.
    apply Finset.induction_on_max_value (fun i ↦ toLex (g i, f i))
      (p := fun t ↦ ∀ {σ : Perm ι}, MonovaryOn f g t → { x | σ x ≠ x } ⊆ t →
        (∑ i in t, f i • g (σ i)) ≤ ∑ i in t, f i • g i) s
    · simp only [le_rfl, Finset.sum_empty, imp_true_iff]
    intro a s has hamax hind σ hfg hσ
    set τ : Perm ι := σ.trans (swap a (σ a)) with hτ
    have hτs : { x | τ x ≠ x } ⊆ s := by
      intro x hx
      simp only [Ne.def, Set.mem_setOf_eq, Equiv.coe_trans, Equiv.swap_comp_apply] at hx
      split_ifs at hx with h₁ h₂
      · obtain rfl | hax := eq_or_ne x a
        · contradiction
        · exact mem_of_mem_insert_of_ne (hσ fun h ↦ hax <| h.symm.trans h₁) hax
      · exact (hx <| σ.injective h₂.symm).elim
      · exact mem_of_mem_insert_of_ne (hσ hx) (ne_of_apply_ne _ h₂)
    specialize hind (hfg.subset <| subset_insert _ _) hτs
    simp_rw [sum_insert has]
    refine' le_trans _ (add_le_add_left hind _)
    obtain hσa | hσa := eq_or_ne a (σ a)
    · rw [hτ, ← hσa, swap_self, trans_refl]
    have h1s : σ⁻¹ a ∈ s := by
      rw [Ne.def, ← inv_eq_iff_eq] at hσa
      refine' mem_of_mem_insert_of_ne (hσ fun h ↦ hσa _) hσa
      rwa [apply_inv_self, eq_comm] at h
    

Proof state:
case step.inr
ι : Type u_1
α : Type u_2
β : Type u_3
inst✝³ : LinearOrderedRing α
inst✝² : LinearOrderedAddCommGroup β
inst✝¹ : Module α β
inst✝ : OrderedSMul α β
s✝ : Finset ι
f : ι → α
g : ι → β
a : ι
s : Finset ι
has : a ∉ s
hamax : ∀ x ∈ s, toLex (g x, f x) ≤ toLex (g a, f a)
σ : Perm ι
hfg : MonovaryOn f g ↑(insert a s)
hσ : {x | σ x ≠ x} ⊆ ↑(insert a s)
τ : Perm ι := σ.trans (Equiv.swap a (σ a))
hτ : τ = σ.trans (Equiv.swap a (σ a))
hτs : {x | τ x ≠ x} ⊆ ↑s
hind : ∑ i in s, f i • g (τ i) ≤ ∑ i in s, f i • g i
hσa : a ≠ σ a
h1s : σ⁻¹ a ∈ s
⊢ f a • g (σ a) + ∑ i in s, f i • g (σ i) ≤ f a • g a + ∑ i in s, f i • g (τ i)

Next tactic:
simp only [← s.sum_erase_add _ h1s, add_comm]
/-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when
`f` and `g` monovary together. Stated by permuting the entries of `g`. -/
theorem MonovaryOn.sum_smul_comp_perm_le_sum_smul (hfg : MonovaryOn f g s)
    (hσ : { x | σ x ≠ x } ⊆ s) : (∑ i in s, f i • g (σ i)) ≤ ∑ i in s, f i • g i := by
  classical
    revert hσ σ hfg
    -- Porting note: Specify `p` to get around `∀ {σ}` in the current goal.
    apply Finset.induction_on_max_value (fun i ↦ toLex (g i, f i))
      (p := fun t ↦ ∀ {σ : Perm ι}, MonovaryOn f g t → { x | σ x ≠ x } ⊆ t →
        (∑ i in t, f i • g (σ i)) ≤ ∑ i in t, f i • g i) s
    · simp only [le_rfl, Finset.sum_empty, imp_true_iff]
    intro a s has hamax hind σ hfg hσ
    set τ : Perm ι := σ.trans (swap a (σ a)) with hτ
    have hτs : { x | τ x ≠ x } ⊆ s := by
      intro x hx
      simp only [Ne.def, Set.mem_setOf_eq, Equiv.coe_trans, Equiv.swap_comp_apply] at hx
      split_ifs at hx with h₁ h₂
      · obtain rfl | hax := eq_or_ne x a
        · contradiction
        · exact mem_of_mem_insert_of_ne (hσ fun h ↦ hax <| h.symm.trans h₁) hax
      · exact (hx <| σ.injective h₂.symm).elim
      · exact mem_of_mem_insert_of_ne (hσ hx) (ne_of_apply_ne _ h₂)
    specialize hind (hfg.subset <| subset_insert _ _) hτs
    simp_rw [sum_insert has]
    refine' le_trans _ (add_le_add_left hind _)
    obtain hσa | hσa := eq_or_ne a (σ a)
    · rw [hτ, ← hσa, swap_self, trans_refl]
    have h1s : σ⁻¹ a ∈ s := by
      rw [Ne.def, ← inv_eq_iff_eq] at hσa
      refine' mem_of_mem_insert_of_ne (hσ fun h ↦ hσa _) hσa
      rwa [apply_inv_self, eq_comm] at h
    simp only [← s.sum_erase_add _ h1s, add_comm]
    

Proof state:
case step.inr
ι : Type u_1
α : Type u_2
β : Type u_3
inst✝³ : LinearOrderedRing α
inst✝² : LinearOrderedAddCommGroup β
inst✝¹ : Module α β
inst✝ : OrderedSMul α β
s✝ : Finset ι
f : ι → α
g : ι → β
a : ι
s : Finset ι
has : a ∉ s
hamax : ∀ x ∈ s, toLex (g x, f x) ≤ toLex (g a, f a)
σ : Perm ι
hfg : MonovaryOn f g ↑(insert a s)
hσ : {x | σ x ≠ x} ⊆ ↑(insert a s)
τ : Perm ι := σ.trans (Equiv.swap a (σ a))
hτ : τ = σ.trans (Equiv.swap a (σ a))
hτs : {x | τ x ≠ x} ⊆ ↑s
hind : ∑ i in s, f i • g (τ i) ≤ ∑ i in s, f i • g i
hσa : a ≠ σ a
h1s : σ⁻¹ a ∈ s
⊢ f a • g (σ a) + (f (σ⁻¹ a) • g (σ (σ⁻¹ a)) + ∑ i in erase s (σ⁻¹ a), f i • g (σ i)) ≤
    f a • g a +
      (f (σ⁻¹ a) • g ((σ.trans (Equiv.swap a (σ a))) (σ⁻¹ a)) +
        ∑ x in erase s (σ⁻¹ a), f x • g ((σ.trans (Equiv.swap a (σ a))) x))

Next tactic:
rw [← add_assoc, ← add_assoc]
/-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when
`f` and `g` monovary together. Stated by permuting the entries of `g`. -/
theorem MonovaryOn.sum_smul_comp_perm_le_sum_smul (hfg : MonovaryOn f g s)
    (hσ : { x | σ x ≠ x } ⊆ s) : (∑ i in s, f i • g (σ i)) ≤ ∑ i in s, f i • g i := by
  classical
    revert hσ σ hfg
    -- Porting note: Specify `p` to get around `∀ {σ}` in the current goal.
    apply Finset.induction_on_max_value (fun i ↦ toLex (g i, f i))
      (p := fun t ↦ ∀ {σ : Perm ι}, MonovaryOn f g t → { x | σ x ≠ x } ⊆ t →
        (∑ i in t, f i • g (σ i)) ≤ ∑ i in t, f i • g i) s
    · simp only [le_rfl, Finset.sum_empty, imp_true_iff]
    intro a s has hamax hind σ hfg hσ
    set τ : Perm ι := σ.trans (swap a (σ a)) with hτ
    have hτs : { x | τ x ≠ x } ⊆ s := by
      intro x hx
      simp only [Ne.def, Set.mem_setOf_eq, Equiv.coe_trans, Equiv.swap_comp_apply] at hx
      split_ifs at hx with h₁ h₂
      · obtain rfl | hax := eq_or_ne x a
        · contradiction
        · exact mem_of_mem_insert_of_ne (hσ fun h ↦ hax <| h.symm.trans h₁) hax
      · exact (hx <| σ.injective h₂.symm).elim
      · exact mem_of_mem_insert_of_ne (hσ hx) (ne_of_apply_ne _ h₂)
    specialize hind (hfg.subset <| subset_insert _ _) hτs
    simp_rw [sum_insert has]
    refine' le_trans _ (add_le_add_left hind _)
    obtain hσa | hσa := eq_or_ne a (σ a)
    · rw [hτ, ← hσa, swap_self, trans_refl]
    have h1s : σ⁻¹ a ∈ s := by
      rw [Ne.def, ← inv_eq_iff_eq] at hσa
      refine' mem_of_mem_insert_of_ne (hσ fun h ↦ hσa _) hσa
      rwa [apply_inv_self, eq_comm] at h
    simp only [← s.sum_erase_add _ h1s, add_comm]
    rw [← add_assoc, ← add_assoc]
    

Proof state:
case step.inr
ι : Type u_1
α : Type u_2
β : Type u_3
inst✝³ : LinearOrderedRing α
inst✝² : LinearOrderedAddCommGroup β
inst✝¹ : Module α β
inst✝ : OrderedSMul α β
s✝ : Finset ι
f : ι → α
g : ι → β
a : ι
s : Finset ι
has : a ∉ s
hamax : ∀ x ∈ s, toLex (g x, f x) ≤ toLex (g a, f a)
σ : Perm ι
hfg : MonovaryOn f g ↑(insert a s)
hσ : {x | σ x ≠ x} ⊆ ↑(insert a s)
τ : Perm ι := σ.trans (Equiv.swap a (σ a))
hτ : τ = σ.trans (Equiv.swap a (σ a))
hτs : {x | τ x ≠ x} ⊆ ↑s
hind : ∑ i in s, f i • g (τ i) ≤ ∑ i in s, f i • g i
hσa : a ≠ σ a
h1s : σ⁻¹ a ∈ s
⊢ f a • g (σ a) + f (σ⁻¹ a) • g (σ (σ⁻¹ a)) + ∑ i in erase s (σ⁻¹ a), f i • g (σ i) ≤
    f a • g a + f (σ⁻¹ a) • g ((σ.trans (Equiv.swap a (σ a))) (σ⁻¹ a)) +
      ∑ x in erase s (σ⁻¹ a), f x • g ((σ.trans (Equiv.swap a (σ a))) x)

Next tactic:
simp only [hτ, swap_apply_left, Function.comp_apply, Equiv.coe_trans, apply_inv_self]
/-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when
`f` and `g` monovary together. Stated by permuting the entries of `g`. -/
theorem MonovaryOn.sum_smul_comp_perm_le_sum_smul (hfg : MonovaryOn f g s)
    (hσ : { x | σ x ≠ x } ⊆ s) : (∑ i in s, f i • g (σ i)) ≤ ∑ i in s, f i • g i := by
  classical
    revert hσ σ hfg
    -- Porting note: Specify `p` to get around `∀ {σ}` in the current goal.
    apply Finset.induction_on_max_value (fun i ↦ toLex (g i, f i))
      (p := fun t ↦ ∀ {σ : Perm ι}, MonovaryOn f g t → { x | σ x ≠ x } ⊆ t →
        (∑ i in t, f i • g (σ i)) ≤ ∑ i in t, f i • g i) s
    · simp only [le_rfl, Finset.sum_empty, imp_true_iff]
    intro a s has hamax hind σ hfg hσ
    set τ : Perm ι := σ.trans (swap a (σ a)) with hτ
    have hτs : { x | τ x ≠ x } ⊆ s := by
      intro x hx
      simp only [Ne.def, Set.mem_setOf_eq, Equiv.coe_trans, Equiv.swap_comp_apply] at hx
      split_ifs at hx with h₁ h₂
      · obtain rfl | hax := eq_or_ne x a
        · contradiction
        · exact mem_of_mem_insert_of_ne (hσ fun h ↦ hax <| h.symm.trans h₁) hax
      · exact (hx <| σ.injective h₂.symm).elim
      · exact mem_of_mem_insert_of_ne (hσ hx) (ne_of_apply_ne _ h₂)
    specialize hind (hfg.subset <| subset_insert _ _) hτs
    simp_rw [sum_insert has]
    refine' le_trans _ (add_le_add_left hind _)
    obtain hσa | hσa := eq_or_ne a (σ a)
    · rw [hτ, ← hσa, swap_self, trans_refl]
    have h1s : σ⁻¹ a ∈ s := by
      rw [Ne.def, ← inv_eq_iff_eq] at hσa
      refine' mem_of_mem_insert_of_ne (hσ fun h ↦ hσa _) hσa
      rwa [apply_inv_self, eq_comm] at h
    simp only [← s.sum_erase_add _ h1s, add_comm]
    rw [← add_assoc, ← add_assoc]
    simp only [hτ, swap_apply_left, Function.comp_apply, Equiv.coe_trans, apply_inv_self]
    

Proof state:
case step.inr
ι : Type u_1
α : Type u_2
β : Type u_3
inst✝³ : LinearOrderedRing α
inst✝² : LinearOrderedAddCommGroup β
inst✝¹ : Module α β
inst✝ : OrderedSMul α β
s✝ : Finset ι
f : ι → α
g : ι → β
a : ι
s : Finset ι
has : a ∉ s
hamax : ∀ x ∈ s, toLex (g x, f x) ≤ toLex (g a, f a)
σ : Perm ι
hfg : MonovaryOn f g ↑(insert a s)
hσ : {x | σ x ≠ x} ⊆ ↑(insert a s)
τ : Perm ι := σ.trans (Equiv.swap a (σ a))
hτ : τ = σ.trans (Equiv.swap a (σ a))
hτs : {x | τ x ≠ x} ⊆ ↑s
hind : ∑ i in s, f i • g (τ i) ≤ ∑ i in s, f i • g i
hσa : a ≠ σ a
h1s : σ⁻¹ a ∈ s
⊢ f a • g (σ a) + f (σ⁻¹ a) • g a + ∑ i in erase s (σ⁻¹ a), f i • g (σ i) ≤
    f a • g a + f (σ⁻¹ a) • g (σ a) + ∑ x in erase s (σ⁻¹ a), f x • g ((Equiv.swap a (σ a)) (σ x))

Next tactic:
refine' add_le_add (smul_add_smul_le_smul_add_smul' _ _) (sum_congr rfl fun x hx ↦ _).le
/-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when
`f` and `g` monovary together. Stated by permuting the entries of `g`. -/
theorem MonovaryOn.sum_smul_comp_perm_le_sum_smul (hfg : MonovaryOn f g s)
    (hσ : { x | σ x ≠ x } ⊆ s) : (∑ i in s, f i • g (σ i)) ≤ ∑ i in s, f i • g i := by
  classical
    revert hσ σ hfg
    -- Porting note: Specify `p` to get around `∀ {σ}` in the current goal.
    apply Finset.induction_on_max_value (fun i ↦ toLex (g i, f i))
      (p := fun t ↦ ∀ {σ : Perm ι}, MonovaryOn f g t → { x | σ x ≠ x } ⊆ t →
        (∑ i in t, f i • g (σ i)) ≤ ∑ i in t, f i • g i) s
    · simp only [le_rfl, Finset.sum_empty, imp_true_iff]
    intro a s has hamax hind σ hfg hσ
    set τ : Perm ι := σ.trans (swap a (σ a)) with hτ
    have hτs : { x | τ x ≠ x } ⊆ s := by
      intro x hx
      simp only [Ne.def, Set.mem_setOf_eq, Equiv.coe_trans, Equiv.swap_comp_apply] at hx
      split_ifs at hx with h₁ h₂
      · obtain rfl | hax := eq_or_ne x a
        · contradiction
        · exact mem_of_mem_insert_of_ne (hσ fun h ↦ hax <| h.symm.trans h₁) hax
      · exact (hx <| σ.injective h₂.symm).elim
      · exact mem_of_mem_insert_of_ne (hσ hx) (ne_of_apply_ne _ h₂)
    specialize hind (hfg.subset <| subset_insert _ _) hτs
    simp_rw [sum_insert has]
    refine' le_trans _ (add_le_add_left hind _)
    obtain hσa | hσa := eq_or_ne a (σ a)
    · rw [hτ, ← hσa, swap_self, trans_refl]
    have h1s : σ⁻¹ a ∈ s := by
      rw [Ne.def, ← inv_eq_iff_eq] at hσa
      refine' mem_of_mem_insert_of_ne (hσ fun h ↦ hσa _) hσa
      rwa [apply_inv_self, eq_comm] at h
    simp only [← s.sum_erase_add _ h1s, add_comm]
    rw [← add_assoc, ← add_assoc]
    simp only [hτ, swap_apply_left, Function.comp_apply, Equiv.coe_trans, apply_inv_self]
    refine' add_le_add (smul_add_smul_le_smul_add_smul' _ _) (sum_congr rfl fun x hx ↦ _).le
    · 

Proof state:
case step.inr.refine'_1
ι : Type u_1
α : Type u_2
β : Type u_3
inst✝³ : LinearOrderedRing α
inst✝² : LinearOrderedAddCommGroup β
inst✝¹ : Module α β
inst✝ : OrderedSMul α β
s✝ : Finset ι
f : ι → α
g : ι → β
a : ι
s : Finset ι
has : a ∉ s
hamax : ∀ x ∈ s, toLex (g x, f x) ≤ toLex (g a, f a)
σ : Perm ι
hfg : MonovaryOn f g ↑(insert a s)
hσ : {x | σ x ≠ x} ⊆ ↑(insert a s)
τ : Perm ι := σ.trans (Equiv.swap a (σ a))
hτ : τ = σ.trans (Equiv.swap a (σ a))
hτs : {x | τ x ≠ x} ⊆ ↑s
hind : ∑ i in s, f i • g (τ i) ≤ ∑ i in s, f i • g i
hσa : a ≠ σ a
h1s : σ⁻¹ a ∈ s
⊢ f (σ⁻¹ a) ≤ f a

Next tactic:
specialize hamax (σ⁻¹ a) h1s
/-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when
`f` and `g` monovary together. Stated by permuting the entries of `g`. -/
theorem MonovaryOn.sum_smul_comp_perm_le_sum_smul (hfg : MonovaryOn f g s)
    (hσ : { x | σ x ≠ x } ⊆ s) : (∑ i in s, f i • g (σ i)) ≤ ∑ i in s, f i • g i := by
  classical
    revert hσ σ hfg
    -- Porting note: Specify `p` to get around `∀ {σ}` in the current goal.
    apply Finset.induction_on_max_value (fun i ↦ toLex (g i, f i))
      (p := fun t ↦ ∀ {σ : Perm ι}, MonovaryOn f g t → { x | σ x ≠ x } ⊆ t →
        (∑ i in t, f i • g (σ i)) ≤ ∑ i in t, f i • g i) s
    · simp only [le_rfl, Finset.sum_empty, imp_true_iff]
    intro a s has hamax hind σ hfg hσ
    set τ : Perm ι := σ.trans (swap a (σ a)) with hτ
    have hτs : { x | τ x ≠ x } ⊆ s := by
      intro x hx
      simp only [Ne.def, Set.mem_setOf_eq, Equiv.coe_trans, Equiv.swap_comp_apply] at hx
      split_ifs at hx with h₁ h₂
      · obtain rfl | hax := eq_or_ne x a
        · contradiction
        · exact mem_of_mem_insert_of_ne (hσ fun h ↦ hax <| h.symm.trans h₁) hax
      · exact (hx <| σ.injective h₂.symm).elim
      · exact mem_of_mem_insert_of_ne (hσ hx) (ne_of_apply_ne _ h₂)
    specialize hind (hfg.subset <| subset_insert _ _) hτs
    simp_rw [sum_insert has]
    refine' le_trans _ (add_le_add_left hind _)
    obtain hσa | hσa := eq_or_ne a (σ a)
    · rw [hτ, ← hσa, swap_self, trans_refl]
    have h1s : σ⁻¹ a ∈ s := by
      rw [Ne.def, ← inv_eq_iff_eq] at hσa
      refine' mem_of_mem_insert_of_ne (hσ fun h ↦ hσa _) hσa
      rwa [apply_inv_self, eq_comm] at h
    simp only [← s.sum_erase_add _ h1s, add_comm]
    rw [← add_assoc, ← add_assoc]
    simp only [hτ, swap_apply_left, Function.comp_apply, Equiv.coe_trans, apply_inv_self]
    refine' add_le_add (smul_add_smul_le_smul_add_smul' _ _) (sum_congr rfl fun x hx ↦ _).le
    · specialize hamax (σ⁻¹ a) h1s
      

Proof state:
case step.inr.refine'_1
ι : Type u_1
α : Type u_2
β : Type u_3
inst✝³ : LinearOrderedRing α
inst✝² : LinearOrderedAddCommGroup β
inst✝¹ : Module α β
inst✝ : OrderedSMul α β
s✝ : Finset ι
f : ι → α
g : ι → β
a : ι
s : Finset ι
has : a ∉ s
σ : Perm ι
hfg : MonovaryOn f g ↑(insert a s)
hσ : {x | σ x ≠ x} ⊆ ↑(insert a s)
τ : Perm ι := σ.trans (Equiv.swap a (σ a))
hτ : τ = σ.trans (Equiv.swap a (σ a))
hτs : {x | τ x ≠ x} ⊆ ↑s
hind : ∑ i in s, f i • g (τ i) ≤ ∑ i in s, f i • g i
hσa : a ≠ σ a
h1s : σ⁻¹ a ∈ s
hamax : toLex (g (σ⁻¹ a), f (σ⁻¹ a)) ≤ toLex (g a, f a)
⊢ f (σ⁻¹ a) ≤ f a

Next tactic:
rw [Prod.Lex.le_iff] at hamax
/-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when
`f` and `g` monovary together. Stated by permuting the entries of `g`. -/
theorem MonovaryOn.sum_smul_comp_perm_le_sum_smul (hfg : MonovaryOn f g s)
    (hσ : { x | σ x ≠ x } ⊆ s) : (∑ i in s, f i • g (σ i)) ≤ ∑ i in s, f i • g i := by
  classical
    revert hσ σ hfg
    -- Porting note: Specify `p` to get around `∀ {σ}` in the current goal.
    apply Finset.induction_on_max_value (fun i ↦ toLex (g i, f i))
      (p := fun t ↦ ∀ {σ : Perm ι}, MonovaryOn f g t → { x | σ x ≠ x } ⊆ t →
        (∑ i in t, f i • g (σ i)) ≤ ∑ i in t, f i • g i) s
    · simp only [le_rfl, Finset.sum_empty, imp_true_iff]
    intro a s has hamax hind σ hfg hσ
    set τ : Perm ι := σ.trans (swap a (σ a)) with hτ
    have hτs : { x | τ x ≠ x } ⊆ s := by
      intro x hx
      simp only [Ne.def, Set.mem_setOf_eq, Equiv.coe_trans, Equiv.swap_comp_apply] at hx
      split_ifs at hx with h₁ h₂
      · obtain rfl | hax := eq_or_ne x a
        · contradiction
        · exact mem_of_mem_insert_of_ne (hσ fun h ↦ hax <| h.symm.trans h₁) hax
      · exact (hx <| σ.injective h₂.symm).elim
      · exact mem_of_mem_insert_of_ne (hσ hx) (ne_of_apply_ne _ h₂)
    specialize hind (hfg.subset <| subset_insert _ _) hτs
    simp_rw [sum_insert has]
    refine' le_trans _ (add_le_add_left hind _)
    obtain hσa | hσa := eq_or_ne a (σ a)
    · rw [hτ, ← hσa, swap_self, trans_refl]
    have h1s : σ⁻¹ a ∈ s := by
      rw [Ne.def, ← inv_eq_iff_eq] at hσa
      refine' mem_of_mem_insert_of_ne (hσ fun h ↦ hσa _) hσa
      rwa [apply_inv_self, eq_comm] at h
    simp only [← s.sum_erase_add _ h1s, add_comm]
    rw [← add_assoc, ← add_assoc]
    simp only [hτ, swap_apply_left, Function.comp_apply, Equiv.coe_trans, apply_inv_self]
    refine' add_le_add (smul_add_smul_le_smul_add_smul' _ _) (sum_congr rfl fun x hx ↦ _).le
    · specialize hamax (σ⁻¹ a) h1s
      rw [Prod.Lex.le_iff] at hamax
      

Proof state:
case step.inr.refine'_1
ι : Type u_1
α : Type u_2
β : Type u_3
inst✝³ : LinearOrderedRing α
inst✝² : LinearOrderedAddCommGroup β
inst✝¹ : Module α β
inst✝ : OrderedSMul α β
s✝ : Finset ι
f : ι → α
g : ι → β
a : ι
s : Finset ι
has : a ∉ s
σ : Perm ι
hfg : MonovaryOn f g ↑(insert a s)
hσ : {x | σ x ≠ x} ⊆ ↑(insert a s)
τ : Perm ι := σ.trans (Equiv.swap a (σ a))
hτ : τ = σ.trans (Equiv.swap a (σ a))
hτs : {x | τ x ≠ x} ⊆ ↑s
hind : ∑ i in s, f i • g (τ i) ≤ ∑ i in s, f i • g i
hσa : a ≠ σ a
h1s : σ⁻¹ a ∈ s
hamax :
  (g (σ⁻¹ a), f (σ⁻¹ a)).1 < (g a, f a).1 ∨
    (g (σ⁻¹ a), f (σ⁻¹ a)).1 = (g a, f a).1 ∧ (g (σ⁻¹ a), f (σ⁻¹ a)).2 ≤ (g a, f a).2
⊢ f (σ⁻¹ a) ≤ f a

Next tactic:
cases' hamax with hamax hamax
/-- **Rearrangement Inequality**: Pointwise scalar multiplication of `f` and `g` is maximized when
`f` and `g` monovary together. Stated by permuting the entries of `g`. -/
theorem MonovaryOn.sum_smul_comp_perm_le_sum_smul (hfg : MonovaryOn f g s)
    (hσ : { x | σ x ≠ x } ⊆ s) : (∑ i in s, f i • g (σ i)) ≤ ∑ i in s, f i • g i := by
  classical
    revert hσ σ hfg
    -- Porting note: Specify `p` to get around `∀ {σ}` in the current goal.
    apply Finset.induction_on_max_value (fun i ↦ toLex (g i, f i))
      (p := fun t ↦ ∀ {σ : Perm ι}, MonovaryOn f g t → { x | σ x ≠ x } ⊆ t →
        (∑ i in t, f i • g (σ i)) ≤ ∑ i in t, f i • g i) s
    · simp only [le_rfl, Finset.sum_empty, imp_true_iff]
    intro a s has hamax hind σ hfg hσ
    set τ : Perm ι := σ.trans (swap a (σ a)) with hτ
    have hτs : { x | τ x ≠ x } ⊆ s := by
      intro x hx
      simp only [Ne.def, Set.mem_setOf_eq, Equiv.coe_trans, Equiv.swap_comp_apply] at hx
      split_ifs at hx with h₁ h₂
      · obtain rfl | hax := eq_or_ne x a
        · contradiction
        · exact mem_of_mem_insert_of_ne (hσ fun h ↦ hax <| h.symm.trans h₁) hax
      · exact (hx <| σ.injective h₂.symm).elim
      · exact mem_of_mem_insert_of_ne (hσ hx) (ne_of_apply_ne _ h₂)
    specialize hind (hfg.subset <| subset_insert _ _) hτs
    simp_rw [sum_insert has]
    refine' le_trans _ (add_le_add_left hind _)
    obtain hσa | hσa := eq_or_ne a (σ a)
    · rw [hτ, ← hσa, swap_self, trans_refl]
    have h1s : σ⁻¹ a ∈ s := by
      rw [Ne.def, ← inv_eq_iff_eq] at hσa
      refine' mem_of_mem_insert_of_ne (hσ fun h ↦ hσa _) hσa
      rwa [apply_inv_self, eq_comm] at h
    simp only [← s.sum_erase_add _ h1s, add_comm]
    rw [← add_assoc, ← add_assoc]
    simp only [hτ, swap_apply_left, Function.comp_apply, Equiv.coe_trans, apply_inv_self]
    refine' add_le_add (smul_add_smul_le_smul_add_smul' _ _) (sum_congr rfl fun x hx ↦ _).le
    · specialize hamax (σ⁻¹ a) h1s
      rw [Prod.Lex.le_iff] at hamax
      cases' hamax with hamax hamax
      · 

Proof state:
case step.inr.refine'_1.inl
ι : Type u_1
α : Type u_2
β : Type u_3
inst✝³ : LinearOrderedRing α
inst✝² : LinearOrderedAddCommGroup β
inst✝¹ : Module α β
inst✝ : OrderedSMul α β
s✝ : Finset ι
f : ι → α
g : ι → β
a : ι
s : Finset ι
has : a ∉ s
σ : Perm ι
hfg : MonovaryOn f g ↑(insert a s)
hσ : {x | σ x ≠ x} ⊆ ↑(insert a s)
τ : Perm ι := σ.trans (Equiv.swap a (σ a))
hτ : τ = σ.trans (Equiv.swap a (σ a))
hτs : {x | τ x ≠ x} ⊆ ↑s
hind : ∑ i in s, f i • g (τ i) ≤ ∑ i in s, f i • g i
hσa : a ≠ σ a
h1s : σ⁻¹ a ∈ s
hamax : (g (σ⁻¹ a), f (σ⁻¹ a)).1 < (g a, f a).1
⊢ f (σ⁻¹ a) ≤ f a

Next tactic:
exact hfg (mem_insert_of_mem h1s) (mem_insert_self _ _) hamax