l3lab/ntp-mathlib huggingface dataset:
/-- `graphEquiv₁ f` is the natural equivalence between `Fin n` and `graph f`,
mapping `i` to `(f i, i)`. -/
def graphEquiv₁ (f : Fin n → α) : Fin n ≃ graph f where
  toFun i := ⟨(f i, i), by simp [graph]⟩
  invFun p := p.1.2
  left_inv i := by simp
  right_inv := fun ⟨⟨x, i⟩, h⟩ => by
    -- Porting note: was `simpa [graph] using h`
    simp only [graph, Finset.mem_image, Finset.mem_univ, true_and] at h
    obtain ⟨i', hi'⟩ := h
    

Proof state:
case intro
n : ℕ
α : Type u_1
inst✝ : LinearOrder α
f : Fin n → α
x✝ : { x // x ∈ graph f }
x : α
i : Fin n
h : (x, i) ∈ graph f
i' : Fin n
hi' : (f i', i') = (x, i)
⊢ (fun i => { val := (f i, i), property := (_ : (f i, i) ∈ Finset.image (fun i => (f i, i)) Finset.univ) })
      ((fun p => (↑p).2) { val := (x, i), property := h }) =
    { val := (x, i), property := h }

Next tactic:
obtain ⟨-, rfl⟩ := Prod.mk.inj_iff.mp hi'
/-- `graphEquiv₁ f` is the natural equivalence between `Fin n` and `graph f`,
mapping `i` to `(f i, i)`. -/
def graphEquiv₁ (f : Fin n → α) : Fin n ≃ graph f where
  toFun i := ⟨(f i, i), by simp [graph]⟩
  invFun p := p.1.2
  left_inv i := by simp
  right_inv := fun ⟨⟨x, i⟩, h⟩ => by
    -- Porting note: was `simpa [graph] using h`
    simp only [graph, Finset.mem_image, Finset.mem_univ, true_and] at h
    obtain ⟨i', hi'⟩ := h
    obtain ⟨-, rfl⟩ := Prod.mk.inj_iff.mp hi'
    

Proof state:
case intro.intro
n : ℕ
α : Type u_1
inst✝ : LinearOrder α
f : Fin n → α
x✝ : { x // x ∈ graph f }
x : α
i' : Fin n
h : (x, i') ∈ graph f
hi' : (f i', i') = (x, i')
⊢ (fun i => { val := (f i, i), property := (_ : (f i, i) ∈ Finset.image (fun i => (f i, i)) Finset.univ) })
      ((fun p => (↑p).2) { val := (x, i'), property := h }) =
    { val := (x, i'), property := h }

Next tactic:
simpa
/-- `graphEquiv₂ f` is an equivalence between `Fin n` and `graph f` that respects the order.
-/
def graphEquiv₂ (f : Fin n → α) : Fin n ≃o graph f :=
  Finset.orderIsoOfFin _ (by 

Proof state:
n : ℕ
α : Type u_1
inst✝ : LinearOrder α
f : Fin n → α
⊢ Finset.card (graph f) = n

Next tactic:
simp
theorem self_comp_sort (f : Fin n → α) : f ∘ sort f = graph.proj ∘ graphEquiv₂ f :=
  show graph.proj ∘ (graphEquiv₁ f ∘ (graphEquiv₁ f).symm) ∘ (graphEquiv₂ f).toEquiv = _ by 

Proof state:
n : ℕ
α : Type u_1
inst✝ : LinearOrder α
f : Fin n → α
⊢ graph.proj ∘ (⇑(graphEquiv₁ f) ∘ ⇑(graphEquiv₁ f).symm) ∘ ⇑(graphEquiv₂ f).toEquiv = graph.proj ∘ ⇑(graphEquiv₂ f)

Next tactic:
simp
theorem monotone_proj (f : Fin n → α) : Monotone (graph.proj : graph f → α) := by
  

Proof state:
n : ℕ
α : Type u_1
inst✝ : LinearOrder α
f : Fin n → α
⊢ Monotone graph.proj

Next tactic:
rintro ⟨⟨x, i⟩, hx⟩ ⟨⟨y, j⟩, hy⟩ (_ | h)
theorem monotone_proj (f : Fin n → α) : Monotone (graph.proj : graph f → α) := by
  rintro ⟨⟨x, i⟩, hx⟩ ⟨⟨y, j⟩, hy⟩ (_ | h)
  · 

Proof state:
case mk.mk.mk.mk.left
n : ℕ
α : Type u_1
inst✝ : LinearOrder α
f : Fin n → α
x : α
i : Fin n
hx : (x, i) ∈ graph f
y : α
j : Fin n
hy : (y, j) ∈ graph f
h✝ : x < y
⊢ graph.proj { val := (x, i), property := hx } ≤ graph.proj { val := (y, j), property := hy }

Next tactic:
exact le_of_lt ‹_›
theorem monotone_proj (f : Fin n → α) : Monotone (graph.proj : graph f → α) := by
  rintro ⟨⟨x, i⟩, hx⟩ ⟨⟨y, j⟩, hy⟩ (_ | h)
  · exact le_of_lt ‹_›
  · 

Proof state:
case mk.mk.mk.mk.right
n : ℕ
α : Type u_1
inst✝ : LinearOrder α
f : Fin n → α
x : α
i : Fin n
hx : (x, i) ∈ graph f
j : Fin n
hy : (x, j) ∈ graph f
h✝ : i ≤ j
⊢ graph.proj { val := (x, i), property := hx } ≤ graph.proj { val := (x, j), property := hy }

Next tactic:
simp [graph.proj]
theorem monotone_sort (f : Fin n → α) : Monotone (f ∘ sort f) := by
  

Proof state:
n : ℕ
α : Type u_1
inst✝ : LinearOrder α
f : Fin n → α
⊢ Monotone (f ∘ ⇑(sort f))

Next tactic:
rw [self_comp_sort]
theorem monotone_sort (f : Fin n → α) : Monotone (f ∘ sort f) := by
  rw [self_comp_sort]
  

Proof state:
n : ℕ
α : Type u_1
inst✝ : LinearOrder α
f : Fin n → α
⊢ Monotone (graph.proj ∘ ⇑(graphEquiv₂ f))

Next tactic:
exact (monotone_proj f).comp (graphEquiv₂ f).monotone
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
⊢ ↑j < Fintype.card { i // f i ≤ a } ↔ f j ≤ a

Next tactic:
suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · 

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
⊢ ↑j < Fintype.card { i // f i ≤ a } ↔ f j ≤ a

Next tactic:
refine ⟨h1 j, fun h ↦ ?_⟩
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
⊢ ↑j < Fintype.card { i // f i ≤ a }

Next tactic:
by_contra! hc
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
⊢ False

Next tactic:
let p : Fin m → Prop := fun x ↦ f x ≤ a
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
p : Fin m → Prop := fun x => f x ≤ a
⊢ False

Next tactic:
let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
    

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
p : Fin m → Prop := fun x => f x ≤ a
q : Fin m → Prop := fun x => ↑x < Fintype.card { i // f i ≤ a }
⊢ False

Next tactic:
let q' : {i // f i ≤ a} → Prop := fun x ↦ q x
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
    let q' : {i // f i ≤ a} → Prop := fun x ↦ q x
    

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
p : Fin m → Prop := fun x => f x ≤ a
q : Fin m → Prop := fun x => ↑x < Fintype.card { i // f i ≤ a }
q' : { i // f i ≤ a } → Prop := fun x => q ↑x
⊢ False

Next tactic:
have hw : 0 < Fintype.card {j : {x : Fin m // f x ≤ a} // ¬ q' j} :=
      Fintype.card_pos_iff.2 ⟨⟨⟨j, h⟩, not_lt.2 hc⟩⟩
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
    let q' : {i // f i ≤ a} → Prop := fun x ↦ q x
    have hw : 0 < Fintype.card {j : {x : Fin m // f x ≤ a} // ¬ q' j} :=
      Fintype.card_pos_iff.2 ⟨⟨⟨j, h⟩, not_lt.2 hc⟩⟩
    

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
p : Fin m → Prop := fun x => f x ≤ a
q : Fin m → Prop := fun x => ↑x < Fintype.card { i // f i ≤ a }
q' : { i // f i ≤ a } → Prop := fun x => q ↑x
hw : 0 < Fintype.card { j // ¬q' j }
⊢ False

Next tactic:
apply hw.ne'
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
    let q' : {i // f i ≤ a} → Prop := fun x ↦ q x
    have hw : 0 < Fintype.card {j : {x : Fin m // f x ≤ a} // ¬ q' j} :=
      Fintype.card_pos_iff.2 ⟨⟨⟨j, h⟩, not_lt.2 hc⟩⟩
    apply hw.ne'
    

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
p : Fin m → Prop := fun x => f x ≤ a
q : Fin m → Prop := fun x => ↑x < Fintype.card { i // f i ≤ a }
q' : { i // f i ≤ a } → Prop := fun x => q ↑x
hw : 0 < Fintype.card { j // ¬q' j }
⊢ Fintype.card { j // ¬q' j } = 0

Next tactic:
have he := Fintype.card_congr <| Equiv.sumCompl <| q'
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
    let q' : {i // f i ≤ a} → Prop := fun x ↦ q x
    have hw : 0 < Fintype.card {j : {x : Fin m // f x ≤ a} // ¬ q' j} :=
      Fintype.card_pos_iff.2 ⟨⟨⟨j, h⟩, not_lt.2 hc⟩⟩
    apply hw.ne'
    have he := Fintype.card_congr <| Equiv.sumCompl <| q'
    

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
p : Fin m → Prop := fun x => f x ≤ a
q : Fin m → Prop := fun x => ↑x < Fintype.card { i // f i ≤ a }
q' : { i // f i ≤ a } → Prop := fun x => q ↑x
hw : 0 < Fintype.card { j // ¬q' j }
he : Fintype.card ({ a // q' a } ⊕ { a // ¬q' a }) = Fintype.card { i // f i ≤ a }
⊢ Fintype.card { j // ¬q' j } = 0

Next tactic:
have h4 := (Fintype.card_congr (@Equiv.subtypeSubtypeEquivSubtype _ p q (h1 _)))
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
    let q' : {i // f i ≤ a} → Prop := fun x ↦ q x
    have hw : 0 < Fintype.card {j : {x : Fin m // f x ≤ a} // ¬ q' j} :=
      Fintype.card_pos_iff.2 ⟨⟨⟨j, h⟩, not_lt.2 hc⟩⟩
    apply hw.ne'
    have he := Fintype.card_congr <| Equiv.sumCompl <| q'
    have h4 := (Fintype.card_congr (@Equiv.subtypeSubtypeEquivSubtype _ p q (h1 _)))
    

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
p : Fin m → Prop := fun x => f x ≤ a
q : Fin m → Prop := fun x => ↑x < Fintype.card { i // f i ≤ a }
q' : { i // f i ≤ a } → Prop := fun x => q ↑x
hw : 0 < Fintype.card { j // ¬q' j }
he : Fintype.card ({ a // q' a } ⊕ { a // ¬q' a }) = Fintype.card { i // f i ≤ a }
h4 : Fintype.card { x // q ↑x } = Fintype.card (Subtype q)
⊢ Fintype.card { j // ¬q' j } = 0

Next tactic:
have h_le : Fintype.card { i // f i ≤ a } ≤ m := by
      conv_rhs => rw [← Fintype.card_fin m]
      exact Fintype.card_subtype_le _
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
    let q' : {i // f i ≤ a} → Prop := fun x ↦ q x
    have hw : 0 < Fintype.card {j : {x : Fin m // f x ≤ a} // ¬ q' j} :=
      Fintype.card_pos_iff.2 ⟨⟨⟨j, h⟩, not_lt.2 hc⟩⟩
    apply hw.ne'
    have he := Fintype.card_congr <| Equiv.sumCompl <| q'
    have h4 := (Fintype.card_congr (@Equiv.subtypeSubtypeEquivSubtype _ p q (h1 _)))
    have h_le : Fintype.card { i // f i ≤ a } ≤ m := by
      

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
p : Fin m → Prop := fun x => f x ≤ a
q : Fin m → Prop := fun x => ↑x < Fintype.card { i // f i ≤ a }
q' : { i // f i ≤ a } → Prop := fun x => q ↑x
hw : 0 < Fintype.card { j // ¬q' j }
he : Fintype.card ({ a // q' a } ⊕ { a // ¬q' a }) = Fintype.card { i // f i ≤ a }
h4 : Fintype.card { x // q ↑x } = Fintype.card (Subtype q)
⊢ Fintype.card { i // f i ≤ a } ≤ m

Next tactic:
conv_rhs => rw [← Fintype.card_fin m]
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
    let q' : {i // f i ≤ a} → Prop := fun x ↦ q x
    have hw : 0 < Fintype.card {j : {x : Fin m // f x ≤ a} // ¬ q' j} :=
      Fintype.card_pos_iff.2 ⟨⟨⟨j, h⟩, not_lt.2 hc⟩⟩
    apply hw.ne'
    have he := Fintype.card_congr <| Equiv.sumCompl <| q'
    have h4 := (Fintype.card_congr (@Equiv.subtypeSubtypeEquivSubtype _ p q (h1 _)))
    have h_le : Fintype.card { i // f i ≤ a } ≤ m := by
      conv_rhs => 

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
p : Fin m → Prop := fun x => f x ≤ a
q : Fin m → Prop := fun x => ↑x < Fintype.card { i // f i ≤ a }
q' : { i // f i ≤ a } → Prop := fun x => q ↑x
hw : 0 < Fintype.card { j // ¬q' j }
he : Fintype.card ({ a // q' a } ⊕ { a // ¬q' a }) = Fintype.card { i // f i ≤ a }
h4 : Fintype.card { x // q ↑x } = Fintype.card (Subtype q)
| m

Next tactic:
rw [← Fintype.card_fin m]
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
    let q' : {i // f i ≤ a} → Prop := fun x ↦ q x
    have hw : 0 < Fintype.card {j : {x : Fin m // f x ≤ a} // ¬ q' j} :=
      Fintype.card_pos_iff.2 ⟨⟨⟨j, h⟩, not_lt.2 hc⟩⟩
    apply hw.ne'
    have he := Fintype.card_congr <| Equiv.sumCompl <| q'
    have h4 := (Fintype.card_congr (@Equiv.subtypeSubtypeEquivSubtype _ p q (h1 _)))
    have h_le : Fintype.card { i // f i ≤ a } ≤ m := by
      conv_rhs => rw [← Fintype.card_fin m]
      

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
p : Fin m → Prop := fun x => f x ≤ a
q : Fin m → Prop := fun x => ↑x < Fintype.card { i // f i ≤ a }
q' : { i // f i ≤ a } → Prop := fun x => q ↑x
hw : 0 < Fintype.card { j // ¬q' j }
he : Fintype.card ({ a // q' a } ⊕ { a // ¬q' a }) = Fintype.card { i // f i ≤ a }
h4 : Fintype.card { x // q ↑x } = Fintype.card (Subtype q)
⊢ Fintype.card { i // f i ≤ a } ≤ Fintype.card (Fin m)

Next tactic:
exact Fintype.card_subtype_le _
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
    let q' : {i // f i ≤ a} → Prop := fun x ↦ q x
    have hw : 0 < Fintype.card {j : {x : Fin m // f x ≤ a} // ¬ q' j} :=
      Fintype.card_pos_iff.2 ⟨⟨⟨j, h⟩, not_lt.2 hc⟩⟩
    apply hw.ne'
    have he := Fintype.card_congr <| Equiv.sumCompl <| q'
    have h4 := (Fintype.card_congr (@Equiv.subtypeSubtypeEquivSubtype _ p q (h1 _)))
    have h_le : Fintype.card { i // f i ≤ a } ≤ m := by
      conv_rhs => rw [← Fintype.card_fin m]
      exact Fintype.card_subtype_le _
    

Proof state:
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
h1 : ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a
h : f j ≤ a
hc : Fintype.card { i // f i ≤ a } ≤ ↑j
p : Fin m → Prop := fun x => f x ≤ a
q : Fin m → Prop := fun x => ↑x < Fintype.card { i // f i ≤ a }
q' : { i // f i ≤ a } → Prop := fun x => q ↑x
hw : 0 < Fintype.card { j // ¬q' j }
he : Fintype.card ({ a // q' a } ⊕ { a // ¬q' a }) = Fintype.card { i // f i ≤ a }
h4 : Fintype.card { x // q ↑x } = Fintype.card (Subtype q)
h_le : Fintype.card { i // f i ≤ a } ≤ m
⊢ Fintype.card { j // ¬q' j } = 0

Next tactic:
rwa [Fintype.card_sum, h4, Fintype.card_fin_lt_of_le h_le, add_right_eq_self] at he
/-- If `f₀ ≤ f₁ ≤ f₂ ≤ ⋯` is a sorted `m`-tuple of elements of `α`, then for any `j : Fin m` and
`a : α` we have `j < #{i | fᵢ ≤ a}` iff `fⱼ ≤ a`. -/
theorem lt_card_le_iff_apply_le_of_monotone [PartialOrder α] [DecidableRel (α := α) LE.le]
    {m : ℕ} (f : Fin m → α) (a : α) (h_sorted : Monotone f) (j : Fin m) :
    j < Fintype.card {i // f i ≤ a} ↔ f j ≤ a := by
  suffices h1 : ∀ k : Fin m, (k < Fintype.card {i // f i ≤ a}) → f k ≤ a
  · refine ⟨h1 j, fun h ↦ ?_⟩
    by_contra! hc
    let p : Fin m → Prop := fun x ↦ f x ≤ a
    let q : Fin m → Prop := fun x ↦ x < Fintype.card {i // f i ≤ a}
    let q' : {i // f i ≤ a} → Prop := fun x ↦ q x
    have hw : 0 < Fintype.card {j : {x : Fin m // f x ≤ a} // ¬ q' j} :=
      Fintype.card_pos_iff.2 ⟨⟨⟨j, h⟩, not_lt.2 hc⟩⟩
    apply hw.ne'
    have he := Fintype.card_congr <| Equiv.sumCompl <| q'
    have h4 := (Fintype.card_congr (@Equiv.subtypeSubtypeEquivSubtype _ p q (h1 _)))
    have h_le : Fintype.card { i // f i ≤ a } ≤ m := by
      conv_rhs => rw [← Fintype.card_fin m]
      exact Fintype.card_subtype_le _
    rwa [Fintype.card_sum, h4, Fintype.card_fin_lt_of_le h_le, add_right_eq_self] at he
  

Proof state:
case h1
n : ℕ
α : Type u_1
inst✝¹ : PartialOrder α
inst✝ : DecidableRel LE.le
m : ℕ
f : Fin m → α
a : α
h_sorted : Monotone f
j : Fin m
⊢ ∀ (k : Fin m), ↑k < Fintype.card { i // f i ≤ a } → f k ≤ a

Next tactic:
intro _ h