internlm/Lean-Github huggingface dataset:
Theorem: Permutation.mem_inversions
Proof state before:
case succ.refine_2.inr
n : Nat
x : Permutation n
iv : Inversion x
i j : Fin n
hlt✝ : i < j
hinv : x[↑i] > x[↑j]
k : Nat
hk : k < n
invs : Array (Inversion x)
IH : ↑j < k → { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs
hjk : ↑j < k + 1
this : ↑i < k
l : Nat
hl : l < k
invs' : Array (Inversion x)
hil : ↑i < l + 1 ∨ ↑j < k
hlt : ↑j < k
IH' : { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs'
⊢ { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈
    if h : x[l] > x[k] then
      Array.push invs'
        { fst := { val := l, isLt := (_ : l < n) }, snd := { val := k, isLt := hk },
          lt := (_ : { val := l, isLt := (_ : l < n) } < { val := k, isLt := hk }),
          inverted := (_ : x[↑{ val := l, isLt := (_ : l < n) }] > x[↑{ val := k, isLt := hk }]) }
    else invs'
Tactic:
if hx : x[l]'(Nat.lt_trans hl hk) > x[k]'hk then
  rewrite [dif_pos hx]
  exact Array.mem_push.mpr <| Or.inl IH'
else
  rewrite [dif_neg hx]; exact IH'
Proof state after:
no goals
Theorem: Permutation.mem_inversions
Proof state before:
n : Nat
x : Permutation n
iv : Inversion x
i j : Fin n
hlt✝ : i < j
hinv : x[↑i] > x[↑j]
k : Nat
hk : k < n
invs : Array (Inversion x)
IH : ↑j < k → { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs
hjk : ↑j < k + 1
this : ↑i < k
l : Nat
hl : l < k
invs' : Array (Inversion x)
hil : ↑i < l + 1 ∨ ↑j < k
hlt : ↑j < k
IH' : { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs'
hx : x[l] > x[k]
⊢ { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈
    if h : x[l] > x[k] then
      Array.push invs'
        { fst := { val := l, isLt := (_ : l < n) }, snd := { val := k, isLt := hk },
          lt := (_ : { val := l, isLt := (_ : l < n) } < { val := k, isLt := hk }),
          inverted := (_ : x[↑{ val := l, isLt := (_ : l < n) }] > x[↑{ val := k, isLt := hk }]) }
    else invs'
Tactic:
rewrite [dif_pos hx]
Proof state after:
n : Nat
x : Permutation n
iv : Inversion x
i j : Fin n
hlt✝ : i < j
hinv : x[↑i] > x[↑j]
k : Nat
hk : k < n
invs : Array (Inversion x)
IH : ↑j < k → { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs
hjk : ↑j < k + 1
this : ↑i < k
l : Nat
hl : l < k
invs' : Array (Inversion x)
hil : ↑i < l + 1 ∨ ↑j < k
hlt : ↑j < k
IH' : { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs'
hx : x[l] > x[k]
⊢ { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈
    Array.push invs'
      { fst := { val := l, isLt := (_ : l < n) }, snd := { val := k, isLt := hk },
        lt := (_ : { val := l, isLt := (_ : l < n) } < { val := k, isLt := hk }),
        inverted := (_ : x[↑{ val := l, isLt := (_ : l < n) }] > x[↑{ val := k, isLt := hk }]) }
Theorem: Permutation.mem_inversions
Proof state before:
n : Nat
x : Permutation n
iv : Inversion x
i j : Fin n
hlt✝ : i < j
hinv : x[↑i] > x[↑j]
k : Nat
hk : k < n
invs : Array (Inversion x)
IH : ↑j < k → { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs
hjk : ↑j < k + 1
this : ↑i < k
l : Nat
hl : l < k
invs' : Array (Inversion x)
hil : ↑i < l + 1 ∨ ↑j < k
hlt : ↑j < k
IH' : { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs'
hx : x[l] > x[k]
⊢ { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈
    Array.push invs'
      { fst := { val := l, isLt := (_ : l < n) }, snd := { val := k, isLt := hk },
        lt := (_ : { val := l, isLt := (_ : l < n) } < { val := k, isLt := hk }),
        inverted := (_ : x[↑{ val := l, isLt := (_ : l < n) }] > x[↑{ val := k, isLt := hk }]) }
Tactic:
exact Array.mem_push.mpr <| Or.inl IH'
Proof state after:
no goals
Theorem: Permutation.mem_inversions
Proof state before:
n : Nat
x : Permutation n
iv : Inversion x
i j : Fin n
hlt✝ : i < j
hinv : x[↑i] > x[↑j]
k : Nat
hk : k < n
invs : Array (Inversion x)
IH : ↑j < k → { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs
hjk : ↑j < k + 1
this : ↑i < k
l : Nat
hl : l < k
invs' : Array (Inversion x)
hil : ↑i < l + 1 ∨ ↑j < k
hlt : ↑j < k
IH' : { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs'
hx : ¬x[l] > x[k]
⊢ { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈
    if h : x[l] > x[k] then
      Array.push invs'
        { fst := { val := l, isLt := (_ : l < n) }, snd := { val := k, isLt := hk },
          lt := (_ : { val := l, isLt := (_ : l < n) } < { val := k, isLt := hk }),
          inverted := (_ : x[↑{ val := l, isLt := (_ : l < n) }] > x[↑{ val := k, isLt := hk }]) }
    else invs'
Tactic:
rewrite [dif_neg hx]
Proof state after:
n : Nat
x : Permutation n
iv : Inversion x
i j : Fin n
hlt✝ : i < j
hinv : x[↑i] > x[↑j]
k : Nat
hk : k < n
invs : Array (Inversion x)
IH : ↑j < k → { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs
hjk : ↑j < k + 1
this : ↑i < k
l : Nat
hl : l < k
invs' : Array (Inversion x)
hil : ↑i < l + 1 ∨ ↑j < k
hlt : ↑j < k
IH' : { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs'
hx : ¬x[l] > x[k]
⊢ { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs'
Theorem: Permutation.mem_inversions
Proof state before:
n : Nat
x : Permutation n
iv : Inversion x
i j : Fin n
hlt✝ : i < j
hinv : x[↑i] > x[↑j]
k : Nat
hk : k < n
invs : Array (Inversion x)
IH : ↑j < k → { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs
hjk : ↑j < k + 1
this : ↑i < k
l : Nat
hl : l < k
invs' : Array (Inversion x)
hil : ↑i < l + 1 ∨ ↑j < k
hlt : ↑j < k
IH' : { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs'
hx : ¬x[l] > x[k]
⊢ { fst := i, snd := j, lt := hlt✝, inverted := hinv } ∈ invs'
Tactic:
exact IH'
Proof state after:
no goals
Theorem: Permutation.nodup_inversions
Proof state before:
n : Nat
x : Permutation n
⊢ Array.Nodup (inversions x)
Tactic:
suffices x.inversions.Nodup ∧ ∀ iv, iv ∈ x.inversions → iv.snd.1 < n
from this.1
Proof state after:
n : Nat
x : Permutation n
⊢ Array.Nodup (inversions x) ∧ ∀ (iv : Inversion x), iv ∈ inversions x → ↑iv.snd < n
Theorem: Permutation.nodup_inversions
Proof state before:
n : Nat
x : Permutation n
⊢ Array.Nodup (inversions x) ∧ ∀ (iv : Inversion x), iv ∈ inversions x → ↑iv.snd < n
Tactic:
dsimp [inversions]
Proof state after:
n : Nat
x : Permutation n
⊢ Array.Nodup
      (Nat.fold' n
        (fun j =>
          Nat.fold' ↑j fun i ivs =>
            if h : x[↑i] > x[↑j] then
              Array.push ivs { fst := Fin.castLE (_ : ↑j ≤ n) i, snd := j, lt := (_ : ↑i < ↑j), inverted := h }
            else ivs)
        #[]) ∧
    ∀ (iv : Inversion x),
      iv ∈
          Nat.fold' n
            (fun j =>
              Nat.fold' ↑j fun i ivs =>
                if h : x[↑i] > x[↑j] then
                  Array.push ivs { fst := Fin.castLE (_ : ↑j ≤ n) i, snd := j, lt := (_ : ↑i < ↑j), inverted := h }
                else ivs)
            #[] →
        ↑iv.snd < n
Theorem: Permutation.nodup_inversions
Proof state before:
n : Nat
x : Permutation n
⊢ Array.Nodup
      (Nat.fold' n
        (fun j =>
          Nat.fold' ↑j fun i ivs =>
            if h : x[↑i] > x[↑j] then
              Array.push ivs { fst := Fin.castLE (_ : ↑j ≤ n) i, snd := j, lt := (_ : ↑i < ↑j), inverted := h }
            else ivs)
        #[]) ∧
    ∀ (iv : Inversion x),
      iv ∈
          Nat.fold' n
            (fun j =>
              Nat.fold' ↑j fun i ivs =>
                if h : x[↑i] > x[↑j] then
                  Array.push ivs { fst := Fin.castLE (_ : ↑j ≤ n) i, snd := j, lt := (_ : ↑i < ↑j), inverted := h }
                else ivs)
            #[] →
        ↑iv.snd < n
Tactic:
refine Nat.fold'_induction (motive := fun k (ivs : Array (Inversion x)) => ivs.Nodup ∧ ∀ iv, iv ∈ ivs → iv.snd.1 < k) n _ #[] ?_ ?_
Proof state after:
case refine_1
n : Nat
x : Permutation n
⊢ (fun k ivs => Array.Nodup ivs ∧ ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k) 0 #[]

case refine_2
n : Nat
x : Permutation n
⊢ ∀ (i : Nat) (hi : i < n) (a : Array (Inversion x)),
    (fun k ivs => Array.Nodup ivs ∧ ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k) i a →
      (fun k ivs => Array.Nodup ivs ∧ ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k) (i + 1)
        (Nat.fold' (↑{ val := i, isLt := hi })
          (fun i_1 ivs =>
            if h : x[↑i_1] > x[↑{ val := i, isLt := hi }] then
              Array.push ivs
                { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                  lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
            else ivs)
          a)
Theorem: Permutation.nodup_inversions
Proof state before:
case refine_1
n : Nat
x : Permutation n
⊢ (fun k ivs => Array.Nodup ivs ∧ ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k) 0 #[]

case refine_2
n : Nat
x : Permutation n
⊢ ∀ (i : Nat) (hi : i < n) (a : Array (Inversion x)),
    (fun k ivs => Array.Nodup ivs ∧ ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k) i a →
      (fun k ivs => Array.Nodup ivs ∧ ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k) (i + 1)
        (Nat.fold' (↑{ val := i, isLt := hi })
          (fun i_1 ivs =>
            if h : x[↑i_1] > x[↑{ val := i, isLt := hi }] then
              Array.push ivs
                { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                  lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
            else ivs)
          a)
Tactic:
all_goals simp
Proof state after:
case refine_1
n : Nat
x : Permutation n
⊢ Array.Nodup #[] ∧ ∀ (iv : Inversion x), iv ∈ #[] → ↑iv.snd < 0

case refine_2
n : Nat
x : Permutation n
⊢ ∀ (i : Nat) (hi : i < n) (a : Array (Inversion x)),
    Array.Nodup a →
      (∀ (iv : Inversion x), iv ∈ a → ↑iv.snd < i) →
        Array.Nodup
            (Nat.fold' i
              (fun i_1 ivs =>
                if h : x[↑i_1] > x[i] then
                  Array.push ivs
                    { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                      lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                else ivs)
              a) ∧
          ∀ (iv : Inversion x),
            iv ∈
                Nat.fold' i
                  (fun i_1 ivs =>
                    if h : x[↑i_1] > x[i] then
                      Array.push ivs
                        { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                          lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                    else ivs)
                  a →
              ↑iv.snd < i + 1
Theorem: Permutation.nodup_inversions
Proof state before:
case refine_1
n : Nat
x : Permutation n
⊢ Array.Nodup #[] ∧ ∀ (iv : Inversion x), iv ∈ #[] → ↑iv.snd < 0

case refine_2
n : Nat
x : Permutation n
⊢ ∀ (i : Nat) (hi : i < n) (a : Array (Inversion x)),
    Array.Nodup a →
      (∀ (iv : Inversion x), iv ∈ a → ↑iv.snd < i) →
        Array.Nodup
            (Nat.fold' i
              (fun i_1 ivs =>
                if h : x[↑i_1] > x[i] then
                  Array.push ivs
                    { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                      lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                else ivs)
              a) ∧
          ∀ (iv : Inversion x),
            iv ∈
                Nat.fold' i
                  (fun i_1 ivs =>
                    if h : x[↑i_1] > x[i] then
                      Array.push ivs
                        { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                          lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                    else ivs)
                  a →
              ↑iv.snd < i + 1
Tactic:
. exact ⟨.nil, fun _ h => nomatch h⟩
Proof state after:
case refine_2
n : Nat
x : Permutation n
⊢ ∀ (i : Nat) (hi : i < n) (a : Array (Inversion x)),
    Array.Nodup a →
      (∀ (iv : Inversion x), iv ∈ a → ↑iv.snd < i) →
        Array.Nodup
            (Nat.fold' i
              (fun i_1 ivs =>
                if h : x[↑i_1] > x[i] then
                  Array.push ivs
                    { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                      lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                else ivs)
              a) ∧
          ∀ (iv : Inversion x),
            iv ∈
                Nat.fold' i
                  (fun i_1 ivs =>
                    if h : x[↑i_1] > x[i] then
                      Array.push ivs
                        { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                          lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                    else ivs)
                  a →
              ↑iv.snd < i + 1
Theorem: Permutation.nodup_inversions
Proof state before:
case refine_2
n : Nat
x : Permutation n
⊢ ∀ (i : Nat) (hi : i < n) (a : Array (Inversion x)),
    Array.Nodup a →
      (∀ (iv : Inversion x), iv ∈ a → ↑iv.snd < i) →
        Array.Nodup
            (Nat.fold' i
              (fun i_1 ivs =>
                if h : x[↑i_1] > x[i] then
                  Array.push ivs
                    { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                      lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                else ivs)
              a) ∧
          ∀ (iv : Inversion x),
            iv ∈
                Nat.fold' i
                  (fun i_1 ivs =>
                    if h : x[↑i_1] > x[i] then
                      Array.push ivs
                        { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                          lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                    else ivs)
                  a →
              ↑iv.snd < i + 1
Tactic:
. intro k hk ivs ivs_nodup ivs_lt
  generalize hivs' : Nat.fold' k _ ivs = ivs'
  suffices Array.Nodup ivs' ∧ ∀ iv, iv ∈ ivs' → iv.snd.1 < k ∨ (iv.snd.1 = k ∧ iv.fst.1 < k) by
    refine And.imp_right ?_ this
    intro hivs' iv hiv
    apply Nat.succ_le_succ; apply Nat.le_iff_lt_or_eq.mpr
    exact Or.imp_right And.left (hivs' iv hiv)
  cases hivs'
  refine Nat.fold'_induction (motive := fun l (ivs' : Array (Inversion x)) => ivs'.Nodup ∧ ∀ iv, iv ∈ ivs' → iv.snd.1 < k ∨ (iv.snd.1 = k ∧ iv.fst.1 < l)) k _ ivs ?_ ?_
  all_goals simp
  . exact And.intro ivs_nodup fun iv hiv => Or.inl <| ivs_lt iv hiv
  . intro l hl ivs' ivs'_nodup ivs'_lt
    if hx : x[l]'(Nat.lt_trans hl hk) > x[k] then
      simp only [dif_pos hx, Array.nodup_push, Array.mem_push, ivs'_nodup, true_and]
      constructor
      . intro h
        specialize ivs'_lt _ h; dsimp at ivs'_lt
        refine ivs'_lt.elim ?_ ?_
        . exact Nat.lt_irrefl k
        . intro h; exact Nat.lt_irrefl l h.2
      . intro iv hiv
        cases hiv with
        | inl hmem =>
          refine Or.imp_right ?_ <| ivs'_lt iv hmem
          exact And.imp_right Nat.le.step
        | inr heq =>
          cases heq; dsimp
          exact Or.inr ⟨rfl, l.lt_succ_self⟩
    else
      simp only [dif_neg hx]
      apply And.intro ivs'_nodup
      intro iv hiv; specialize ivs'_lt iv hiv
      refine Or.imp_right ?_ ivs'_lt
      exact And.imp_right Nat.le.step
Proof state after:
no goals
Theorem: Permutation.nodup_inversions
Proof state before:
case refine_2
n : Nat
x : Permutation n
⊢ ∀ (i : Nat) (hi : i < n) (a : Array (Inversion x)),
    (fun k ivs => Array.Nodup ivs ∧ ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k) i a →
      (fun k ivs => Array.Nodup ivs ∧ ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k) (i + 1)
        (Nat.fold' (↑{ val := i, isLt := hi })
          (fun i_1 ivs =>
            if h : x[↑i_1] > x[↑{ val := i, isLt := hi }] then
              Array.push ivs
                { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                  lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
            else ivs)
          a)
Tactic:
simp
Proof state after:
case refine_2
n : Nat
x : Permutation n
⊢ ∀ (i : Nat) (hi : i < n) (a : Array (Inversion x)),
    Array.Nodup a →
      (∀ (iv : Inversion x), iv ∈ a → ↑iv.snd < i) →
        Array.Nodup
            (Nat.fold' i
              (fun i_1 ivs =>
                if h : x[↑i_1] > x[i] then
                  Array.push ivs
                    { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                      lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                else ivs)
              a) ∧
          ∀ (iv : Inversion x),
            iv ∈
                Nat.fold' i
                  (fun i_1 ivs =>
                    if h : x[↑i_1] > x[i] then
                      Array.push ivs
                        { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                          lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                    else ivs)
                  a →
              ↑iv.snd < i + 1
Theorem: Permutation.nodup_inversions
Proof state before:
case refine_1
n : Nat
x : Permutation n
⊢ Array.Nodup #[] ∧ ∀ (iv : Inversion x), iv ∈ #[] → ↑iv.snd < 0
Tactic:
exact ⟨.nil, fun _ h => nomatch h⟩
Proof state after:
no goals
Theorem: Permutation.nodup_inversions
Proof state before:
case refine_2
n : Nat
x : Permutation n
⊢ ∀ (i : Nat) (hi : i < n) (a : Array (Inversion x)),
    Array.Nodup a →
      (∀ (iv : Inversion x), iv ∈ a → ↑iv.snd < i) →
        Array.Nodup
            (Nat.fold' i
              (fun i_1 ivs =>
                if h : x[↑i_1] > x[i] then
                  Array.push ivs
                    { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                      lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                else ivs)
              a) ∧
          ∀ (iv : Inversion x),
            iv ∈
                Nat.fold' i
                  (fun i_1 ivs =>
                    if h : x[↑i_1] > x[i] then
                      Array.push ivs
                        { fst := Fin.castLE (_ : ↑{ val := i, isLt := hi } ≤ n) i_1, snd := { val := i, isLt := hi },
                          lt := (_ : ↑i_1 < ↑{ val := i, isLt := hi }), inverted := h }
                    else ivs)
                  a →
              ↑iv.snd < i + 1
Tactic:
intro k hk ivs ivs_nodup ivs_lt
Proof state after:
case refine_2
n : Nat
x : Permutation n
k : Nat
hk : k < n
ivs : Array (Inversion x)
ivs_nodup : Array.Nodup ivs
ivs_lt : ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k
⊢ Array.Nodup
      (Nat.fold' k
        (fun i ivs =>
          if h : x[↑i] > x[k] then
            Array.push ivs
              { fst := Fin.castLE (_ : ↑{ val := k, isLt := hk } ≤ n) i, snd := { val := k, isLt := hk },
                lt := (_ : ↑i < ↑{ val := k, isLt := hk }), inverted := h }
          else ivs)
        ivs) ∧
    ∀ (iv : Inversion x),
      iv ∈
          Nat.fold' k
            (fun i ivs =>
              if h : x[↑i] > x[k] then
                Array.push ivs
                  { fst := Fin.castLE (_ : ↑{ val := k, isLt := hk } ≤ n) i, snd := { val := k, isLt := hk },
                    lt := (_ : ↑i < ↑{ val := k, isLt := hk }), inverted := h }
              else ivs)
            ivs →
        ↑iv.snd < k + 1
Theorem: Permutation.nodup_inversions
Proof state before:
case refine_2
n : Nat
x : Permutation n
k : Nat
hk : k < n
ivs : Array (Inversion x)
ivs_nodup : Array.Nodup ivs
ivs_lt : ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k
⊢ Array.Nodup
      (Nat.fold' k
        (fun i ivs =>
          if h : x[↑i] > x[k] then
            Array.push ivs
              { fst := Fin.castLE (_ : ↑{ val := k, isLt := hk } ≤ n) i, snd := { val := k, isLt := hk },
                lt := (_ : ↑i < ↑{ val := k, isLt := hk }), inverted := h }
          else ivs)
        ivs) ∧
    ∀ (iv : Inversion x),
      iv ∈
          Nat.fold' k
            (fun i ivs =>
              if h : x[↑i] > x[k] then
                Array.push ivs
                  { fst := Fin.castLE (_ : ↑{ val := k, isLt := hk } ≤ n) i, snd := { val := k, isLt := hk },
                    lt := (_ : ↑i < ↑{ val := k, isLt := hk }), inverted := h }
              else ivs)
            ivs →
        ↑iv.snd < k + 1
Tactic:
generalize hivs' : Nat.fold' k _ ivs = ivs'
Proof state after:
case refine_2
n : Nat
x : Permutation n
k : Nat
hk : k < n
ivs : Array (Inversion x)
ivs_nodup : Array.Nodup ivs
ivs_lt : ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k
ivs' : Array (Inversion x)
hivs' :
  Nat.fold' k
      (fun i ivs =>
        if h : x[↑i] > x[k] then
          Array.push ivs
            { fst := Fin.castLE (_ : ↑{ val := k, isLt := hk } ≤ n) i, snd := { val := k, isLt := hk },
              lt := (_ : ↑i < ↑{ val := k, isLt := hk }), inverted := h }
        else ivs)
      ivs =
    ivs'
⊢ Array.Nodup ivs' ∧ ∀ (iv : Inversion x), iv ∈ ivs' → ↑iv.snd < k + 1
Theorem: Permutation.nodup_inversions
Proof state before:
case refine_2
n : Nat
x : Permutation n
k : Nat
hk : k < n
ivs : Array (Inversion x)
ivs_nodup : Array.Nodup ivs
ivs_lt : ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k
ivs' : Array (Inversion x)
hivs' :
  Nat.fold' k
      (fun i ivs =>
        if h : x[↑i] > x[k] then
          Array.push ivs
            { fst := Fin.castLE (_ : ↑{ val := k, isLt := hk } ≤ n) i, snd := { val := k, isLt := hk },
              lt := (_ : ↑i < ↑{ val := k, isLt := hk }), inverted := h }
        else ivs)
      ivs =
    ivs'
⊢ Array.Nodup ivs' ∧ ∀ (iv : Inversion x), iv ∈ ivs' → ↑iv.snd < k + 1
Tactic:
suffices Array.Nodup ivs' ∧ ∀ iv, iv ∈ ivs' → iv.snd.1 < k ∨ (iv.snd.1 = k ∧ iv.fst.1 < k) by
  refine And.imp_right ?_ this
  intro hivs' iv hiv
  apply Nat.succ_le_succ; apply Nat.le_iff_lt_or_eq.mpr
  exact Or.imp_right And.left (hivs' iv hiv)
Proof state after:
case refine_2
n : Nat
x : Permutation n
k : Nat
hk : k < n
ivs : Array (Inversion x)
ivs_nodup : Array.Nodup ivs
ivs_lt : ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k
ivs' : Array (Inversion x)
hivs' :
  Nat.fold' k
      (fun i ivs =>
        if h : x[↑i] > x[k] then
          Array.push ivs
            { fst := Fin.castLE (_ : ↑{ val := k, isLt := hk } ≤ n) i, snd := { val := k, isLt := hk },
              lt := (_ : ↑i < ↑{ val := k, isLt := hk }), inverted := h }
        else ivs)
      ivs =
    ivs'
⊢ Array.Nodup ivs' ∧ ∀ (iv : Inversion x), iv ∈ ivs' → ↑iv.snd < k ∨ ↑iv.snd = k ∧ ↑iv.fst < k
Theorem: Permutation.nodup_inversions
Proof state before:
case refine_2
n : Nat
x : Permutation n
k : Nat
hk : k < n
ivs : Array (Inversion x)
ivs_nodup : Array.Nodup ivs
ivs_lt : ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k
ivs' : Array (Inversion x)
hivs' :
  Nat.fold' k
      (fun i ivs =>
        if h : x[↑i] > x[k] then
          Array.push ivs
            { fst := Fin.castLE (_ : ↑{ val := k, isLt := hk } ≤ n) i, snd := { val := k, isLt := hk },
              lt := (_ : ↑i < ↑{ val := k, isLt := hk }), inverted := h }
        else ivs)
      ivs =
    ivs'
⊢ Array.Nodup ivs' ∧ ∀ (iv : Inversion x), iv ∈ ivs' → ↑iv.snd < k ∨ ↑iv.snd = k ∧ ↑iv.fst < k
Tactic:
cases hivs'
Proof state after:
case refine_2.refl
n : Nat
x : Permutation n
k : Nat
hk : k < n
ivs : Array (Inversion x)
ivs_nodup : Array.Nodup ivs
ivs_lt : ∀ (iv : Inversion x), iv ∈ ivs → ↑iv.snd < k
⊢ Array.Nodup
      (Nat.fold' k
        (fun i ivs =>
          if h : x[↑i] > x[k] then
            Array.push ivs
              { fst := Fin.castLE (_ : ↑{ val := k, isLt := hk } ≤ n) i, snd := { val := k, isLt := hk },
                lt := (_ : ↑i < ↑{ val := k, isLt := hk }), inverted := h }
          else ivs)
        ivs) ∧
    ∀ (iv : Inversion x),
      iv ∈
          Nat.fold' k
            (fun i ivs =>
              if h : x[↑i] > x[k] then
                Array.push ivs
                  { fst := Fin.castLE (_ : ↑{ val := k, isLt := hk } ≤ n) i, snd := { val := k, isLt := hk },
                    lt := (_ : ↑i < ↑{ val := k, isLt := hk }), inverted := h }
              else ivs)
            ivs →
        ↑iv.snd < k ∨ ↑iv.snd = k ∧ ↑iv.fst < k