l3lab/ntp-mathlib huggingface dataset:
theorem Valid'.glue_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂)
    (sep : l.All fun x => r.All fun y => x < y) (bal : BalancedSz (size l) (size r)) :
    Valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := by
  cases' l with ls ll lx lr; · exact ⟨hr, (zero_add _).symm⟩
  cases' r with rs rl rx rr; · exact ⟨hl, rfl⟩
  dsimp [glue]; split_ifs
  · rw [splitMax_eq]
    cases' Valid'.eraseMax_aux hl with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceR v (hr.of_gt _ _) H, _⟩
      · refine' findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂)
          lx lr hl.1.2.to_nil (sep.2.2.imp _)
        exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.2.1)
      · exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2
      · rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1]; rfl
    refine' Or.inl ⟨_, Or.inr e, _⟩
    rwa [hl.2.eq_node'] at bal
  · 

Proof state:
case neg
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
⊢ Valid' o₁ (Ordnode.balanceL (Ordnode.node ls ll lx lr) (splitMin' rl rx rr).1 (splitMin' rl rx rr).2) o₂ ∧
    size (Ordnode.balanceL (Ordnode.node ls ll lx lr) (splitMin' rl rx rr).1 (splitMin' rl rx rr).2) = ls + rs

Next tactic:
rw [splitMin_eq]
theorem Valid'.glue_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂)
    (sep : l.All fun x => r.All fun y => x < y) (bal : BalancedSz (size l) (size r)) :
    Valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := by
  cases' l with ls ll lx lr; · exact ⟨hr, (zero_add _).symm⟩
  cases' r with rs rl rx rr; · exact ⟨hl, rfl⟩
  dsimp [glue]; split_ifs
  · rw [splitMax_eq]
    cases' Valid'.eraseMax_aux hl with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceR v (hr.of_gt _ _) H, _⟩
      · refine' findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂)
          lx lr hl.1.2.to_nil (sep.2.2.imp _)
        exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.2.1)
      · exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2
      · rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1]; rfl
    refine' Or.inl ⟨_, Or.inr e, _⟩
    rwa [hl.2.eq_node'] at bal
  · rw [splitMin_eq]
    

Proof state:
case neg
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
⊢ Valid' o₁
      (Ordnode.balanceL (Ordnode.node ls ll lx lr) (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).1
        (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).2)
      o₂ ∧
    size
        (Ordnode.balanceL (Ordnode.node ls ll lx lr) (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).1
          (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).2) =
      ls + rs
case neg.s
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
⊢ ℕ

Next tactic:
cases' Valid'.eraseMin_aux hr with v e
theorem Valid'.glue_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂)
    (sep : l.All fun x => r.All fun y => x < y) (bal : BalancedSz (size l) (size r)) :
    Valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := by
  cases' l with ls ll lx lr; · exact ⟨hr, (zero_add _).symm⟩
  cases' r with rs rl rx rr; · exact ⟨hl, rfl⟩
  dsimp [glue]; split_ifs
  · rw [splitMax_eq]
    cases' Valid'.eraseMax_aux hl with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceR v (hr.of_gt _ _) H, _⟩
      · refine' findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂)
          lx lr hl.1.2.to_nil (sep.2.2.imp _)
        exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.2.1)
      · exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2
      · rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1]; rfl
    refine' Or.inl ⟨_, Or.inr e, _⟩
    rwa [hl.2.eq_node'] at bal
  · rw [splitMin_eq]
    cases' Valid'.eraseMin_aux hr with v e
    

Proof state:
case neg.intro
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
v : Valid' (↑(findMin' rl rx)) (eraseMin (Ordnode.node' rl rx rr)) o₂
e : size (Ordnode.node' rl rx rr) = size (eraseMin (Ordnode.node' rl rx rr)) + 1
⊢ Valid' o₁
      (Ordnode.balanceL (Ordnode.node ls ll lx lr) (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).1
        (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).2)
      o₂ ∧
    size
        (Ordnode.balanceL (Ordnode.node ls ll lx lr) (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).1
          (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).2) =
      ls + rs
case neg.s
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
⊢ ℕ

Next tactic:
suffices H : _ by
      refine' ⟨Valid'.balanceL (hl.of_lt _ _) v H, _⟩
      · refine' @findMin'_all (P := fun a : α => Bounded nil o₁ (a : WithBot α))
          rl rx (sep.2.1.1.imp _) hr.1.1.to_nil
        exact fun y h => hl.1.1.to_nil.mono_right (le_of_lt h)
      · exact
          @findMin'_all _ (fun a => All (· < a) (.node ls ll lx lr)) rl rx
            (all_iff_forall.2 fun x hx => sep.imp fun y hy => all_iff_forall.1 hy.1 _ hx)
            (sep.imp fun y hy => hy.2.1)
      · rw [size_balanceL hl.3 v.3 hl.2 v.2 H, add_assoc, ← e, hr.2.1]; rfl
theorem Valid'.glue_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂)
    (sep : l.All fun x => r.All fun y => x < y) (bal : BalancedSz (size l) (size r)) :
    Valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := by
  cases' l with ls ll lx lr; · exact ⟨hr, (zero_add _).symm⟩
  cases' r with rs rl rx rr; · exact ⟨hl, rfl⟩
  dsimp [glue]; split_ifs
  · rw [splitMax_eq]
    cases' Valid'.eraseMax_aux hl with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceR v (hr.of_gt _ _) H, _⟩
      · refine' findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂)
          lx lr hl.1.2.to_nil (sep.2.2.imp _)
        exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.2.1)
      · exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2
      · rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1]; rfl
    refine' Or.inl ⟨_, Or.inr e, _⟩
    rwa [hl.2.eq_node'] at bal
  · rw [splitMin_eq]
    cases' Valid'.eraseMin_aux hr with v e
    suffices H : _ by
      

Proof state:
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
v : Valid' (↑(findMin' rl rx)) (eraseMin (Ordnode.node' rl rx rr)) o₂
e : size (Ordnode.node' rl rx rr) = size (eraseMin (Ordnode.node' rl rx rr)) + 1
H : ?m.346575
⊢ Valid' o₁
      (Ordnode.balanceL (Ordnode.node ls ll lx lr) (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).1
        (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).2)
      o₂ ∧
    size
        (Ordnode.balanceL (Ordnode.node ls ll lx lr) (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).1
          (findMin' rl rx, eraseMin (Ordnode.node ?neg.s✝ rl rx rr)).2) =
      ls + rs

Next tactic:
refine' ⟨Valid'.balanceL (hl.of_lt _ _) v H, _⟩
theorem Valid'.glue_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂)
    (sep : l.All fun x => r.All fun y => x < y) (bal : BalancedSz (size l) (size r)) :
    Valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := by
  cases' l with ls ll lx lr; · exact ⟨hr, (zero_add _).symm⟩
  cases' r with rs rl rx rr; · exact ⟨hl, rfl⟩
  dsimp [glue]; split_ifs
  · rw [splitMax_eq]
    cases' Valid'.eraseMax_aux hl with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceR v (hr.of_gt _ _) H, _⟩
      · refine' findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂)
          lx lr hl.1.2.to_nil (sep.2.2.imp _)
        exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.2.1)
      · exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2
      · rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1]; rfl
    refine' Or.inl ⟨_, Or.inr e, _⟩
    rwa [hl.2.eq_node'] at bal
  · rw [splitMin_eq]
    cases' Valid'.eraseMin_aux hr with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceL (hl.of_lt _ _) v H, _⟩
      · 

Proof state:
case refine'_1
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
v : Valid' (↑(findMin' rl rx)) (eraseMin (Ordnode.node' rl rx rr)) o₂
e : size (Ordnode.node' rl rx rr) = size (eraseMin (Ordnode.node' rl rx rr)) + 1
H :
  (∃ l',
      Raised l' (size (Ordnode.node ls ll lx lr)) ∧
        BalancedSz l' (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2)) ∨
    ∃ r',
      Raised (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2) r' ∧
        BalancedSz (size (Ordnode.node ls ll lx lr)) r'
⊢ Bounded nil o₁ ↑(findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).1

Next tactic:
refine' @findMin'_all (P := fun a : α => Bounded nil o₁ (a : WithBot α))
          rl rx (sep.2.1.1.imp _) hr.1.1.to_nil
theorem Valid'.glue_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂)
    (sep : l.All fun x => r.All fun y => x < y) (bal : BalancedSz (size l) (size r)) :
    Valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := by
  cases' l with ls ll lx lr; · exact ⟨hr, (zero_add _).symm⟩
  cases' r with rs rl rx rr; · exact ⟨hl, rfl⟩
  dsimp [glue]; split_ifs
  · rw [splitMax_eq]
    cases' Valid'.eraseMax_aux hl with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceR v (hr.of_gt _ _) H, _⟩
      · refine' findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂)
          lx lr hl.1.2.to_nil (sep.2.2.imp _)
        exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.2.1)
      · exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2
      · rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1]; rfl
    refine' Or.inl ⟨_, Or.inr e, _⟩
    rwa [hl.2.eq_node'] at bal
  · rw [splitMin_eq]
    cases' Valid'.eraseMin_aux hr with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceL (hl.of_lt _ _) v H, _⟩
      · refine' @findMin'_all (P := fun a : α => Bounded nil o₁ (a : WithBot α))
          rl rx (sep.2.1.1.imp _) hr.1.1.to_nil
        

Proof state:
case refine'_1
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
v : Valid' (↑(findMin' rl rx)) (eraseMin (Ordnode.node' rl rx rr)) o₂
e : size (Ordnode.node' rl rx rr) = size (eraseMin (Ordnode.node' rl rx rr)) + 1
H :
  (∃ l',
      Raised l' (size (Ordnode.node ls ll lx lr)) ∧
        BalancedSz l' (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2)) ∨
    ∃ r',
      Raised (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2) r' ∧
        BalancedSz (size (Ordnode.node ls ll lx lr)) r'
⊢ ∀ (a : α), lx < a → Bounded nil o₁ ↑a

Next tactic:
exact fun y h => hl.1.1.to_nil.mono_right (le_of_lt h)
theorem Valid'.glue_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂)
    (sep : l.All fun x => r.All fun y => x < y) (bal : BalancedSz (size l) (size r)) :
    Valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := by
  cases' l with ls ll lx lr; · exact ⟨hr, (zero_add _).symm⟩
  cases' r with rs rl rx rr; · exact ⟨hl, rfl⟩
  dsimp [glue]; split_ifs
  · rw [splitMax_eq]
    cases' Valid'.eraseMax_aux hl with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceR v (hr.of_gt _ _) H, _⟩
      · refine' findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂)
          lx lr hl.1.2.to_nil (sep.2.2.imp _)
        exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.2.1)
      · exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2
      · rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1]; rfl
    refine' Or.inl ⟨_, Or.inr e, _⟩
    rwa [hl.2.eq_node'] at bal
  · rw [splitMin_eq]
    cases' Valid'.eraseMin_aux hr with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceL (hl.of_lt _ _) v H, _⟩
      · refine' @findMin'_all (P := fun a : α => Bounded nil o₁ (a : WithBot α))
          rl rx (sep.2.1.1.imp _) hr.1.1.to_nil
        exact fun y h => hl.1.1.to_nil.mono_right (le_of_lt h)
      · 

Proof state:
case refine'_2
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
v : Valid' (↑(findMin' rl rx)) (eraseMin (Ordnode.node' rl rx rr)) o₂
e : size (Ordnode.node' rl rx rr) = size (eraseMin (Ordnode.node' rl rx rr)) + 1
H :
  (∃ l',
      Raised l' (size (Ordnode.node ls ll lx lr)) ∧
        BalancedSz l' (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2)) ∨
    ∃ r',
      Raised (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2) r' ∧
        BalancedSz (size (Ordnode.node ls ll lx lr)) r'
⊢ All (fun x => x < (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).1)
    (Ordnode.node ls ll lx lr)

Next tactic:
exact
          @findMin'_all _ (fun a => All (· < a) (.node ls ll lx lr)) rl rx
            (all_iff_forall.2 fun x hx => sep.imp fun y hy => all_iff_forall.1 hy.1 _ hx)
            (sep.imp fun y hy => hy.2.1)
theorem Valid'.glue_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂)
    (sep : l.All fun x => r.All fun y => x < y) (bal : BalancedSz (size l) (size r)) :
    Valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := by
  cases' l with ls ll lx lr; · exact ⟨hr, (zero_add _).symm⟩
  cases' r with rs rl rx rr; · exact ⟨hl, rfl⟩
  dsimp [glue]; split_ifs
  · rw [splitMax_eq]
    cases' Valid'.eraseMax_aux hl with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceR v (hr.of_gt _ _) H, _⟩
      · refine' findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂)
          lx lr hl.1.2.to_nil (sep.2.2.imp _)
        exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.2.1)
      · exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2
      · rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1]; rfl
    refine' Or.inl ⟨_, Or.inr e, _⟩
    rwa [hl.2.eq_node'] at bal
  · rw [splitMin_eq]
    cases' Valid'.eraseMin_aux hr with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceL (hl.of_lt _ _) v H, _⟩
      · refine' @findMin'_all (P := fun a : α => Bounded nil o₁ (a : WithBot α))
          rl rx (sep.2.1.1.imp _) hr.1.1.to_nil
        exact fun y h => hl.1.1.to_nil.mono_right (le_of_lt h)
      · exact
          @findMin'_all _ (fun a => All (· < a) (.node ls ll lx lr)) rl rx
            (all_iff_forall.2 fun x hx => sep.imp fun y hy => all_iff_forall.1 hy.1 _ hx)
            (sep.imp fun y hy => hy.2.1)
      · 

Proof state:
case refine'_3
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
v : Valid' (↑(findMin' rl rx)) (eraseMin (Ordnode.node' rl rx rr)) o₂
e : size (Ordnode.node' rl rx rr) = size (eraseMin (Ordnode.node' rl rx rr)) + 1
H :
  (∃ l',
      Raised l' (size (Ordnode.node ls ll lx lr)) ∧
        BalancedSz l' (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2)) ∨
    ∃ r',
      Raised (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2) r' ∧
        BalancedSz (size (Ordnode.node ls ll lx lr)) r'
⊢ size
      (Ordnode.balanceL (Ordnode.node ls ll lx lr)
        (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).1
        (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2) =
    ls + rs

Next tactic:
rw [size_balanceL hl.3 v.3 hl.2 v.2 H, add_assoc, ← e, hr.2.1]
theorem Valid'.glue_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂)
    (sep : l.All fun x => r.All fun y => x < y) (bal : BalancedSz (size l) (size r)) :
    Valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := by
  cases' l with ls ll lx lr; · exact ⟨hr, (zero_add _).symm⟩
  cases' r with rs rl rx rr; · exact ⟨hl, rfl⟩
  dsimp [glue]; split_ifs
  · rw [splitMax_eq]
    cases' Valid'.eraseMax_aux hl with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceR v (hr.of_gt _ _) H, _⟩
      · refine' findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂)
          lx lr hl.1.2.to_nil (sep.2.2.imp _)
        exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.2.1)
      · exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2
      · rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1]; rfl
    refine' Or.inl ⟨_, Or.inr e, _⟩
    rwa [hl.2.eq_node'] at bal
  · rw [splitMin_eq]
    cases' Valid'.eraseMin_aux hr with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceL (hl.of_lt _ _) v H, _⟩
      · refine' @findMin'_all (P := fun a : α => Bounded nil o₁ (a : WithBot α))
          rl rx (sep.2.1.1.imp _) hr.1.1.to_nil
        exact fun y h => hl.1.1.to_nil.mono_right (le_of_lt h)
      · exact
          @findMin'_all _ (fun a => All (· < a) (.node ls ll lx lr)) rl rx
            (all_iff_forall.2 fun x hx => sep.imp fun y hy => all_iff_forall.1 hy.1 _ hx)
            (sep.imp fun y hy => hy.2.1)
      · rw [size_balanceL hl.3 v.3 hl.2 v.2 H, add_assoc, ← e, hr.2.1]; 

Proof state:
case refine'_3
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
v : Valid' (↑(findMin' rl rx)) (eraseMin (Ordnode.node' rl rx rr)) o₂
e : size (Ordnode.node' rl rx rr) = size (eraseMin (Ordnode.node' rl rx rr)) + 1
H :
  (∃ l',
      Raised l' (size (Ordnode.node ls ll lx lr)) ∧
        BalancedSz l' (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2)) ∨
    ∃ r',
      Raised (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2) r' ∧
        BalancedSz (size (Ordnode.node ls ll lx lr)) r'
⊢ size (Ordnode.node ls ll lx lr) + size (Ordnode.node' rl rx rr) = ls + (size rl + size rr + 1)

Next tactic:
rfl
theorem Valid'.glue_aux {l r o₁ o₂} (hl : Valid' o₁ l o₂) (hr : Valid' o₁ r o₂)
    (sep : l.All fun x => r.All fun y => x < y) (bal : BalancedSz (size l) (size r)) :
    Valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := by
  cases' l with ls ll lx lr; · exact ⟨hr, (zero_add _).symm⟩
  cases' r with rs rl rx rr; · exact ⟨hl, rfl⟩
  dsimp [glue]; split_ifs
  · rw [splitMax_eq]
    cases' Valid'.eraseMax_aux hl with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceR v (hr.of_gt _ _) H, _⟩
      · refine' findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂)
          lx lr hl.1.2.to_nil (sep.2.2.imp _)
        exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.2.1)
      · exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2
      · rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1]; rfl
    refine' Or.inl ⟨_, Or.inr e, _⟩
    rwa [hl.2.eq_node'] at bal
  · rw [splitMin_eq]
    cases' Valid'.eraseMin_aux hr with v e
    suffices H : _ by
      refine' ⟨Valid'.balanceL (hl.of_lt _ _) v H, _⟩
      · refine' @findMin'_all (P := fun a : α => Bounded nil o₁ (a : WithBot α))
          rl rx (sep.2.1.1.imp _) hr.1.1.to_nil
        exact fun y h => hl.1.1.to_nil.mono_right (le_of_lt h)
      · exact
          @findMin'_all _ (fun a => All (· < a) (.node ls ll lx lr)) rl rx
            (all_iff_forall.2 fun x hx => sep.imp fun y hy => all_iff_forall.1 hy.1 _ hx)
            (sep.imp fun y hy => hy.2.1)
      · rw [size_balanceL hl.3 v.3 hl.2 v.2 H, add_assoc, ← e, hr.2.1]; rfl
    

Proof state:
case neg.intro
α : Type u_1
inst✝ : Preorder α
o₁ : WithBot α
o₂ : WithTop α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
hr : Valid' o₁ (Ordnode.node rs rl rx rr) o₂
sep : All (fun x => All (fun y => x < y) (Ordnode.node rs rl rx rr)) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size (Ordnode.node rs rl rx rr))
h✝ : ¬ls > rs
v : Valid' (↑(findMin' rl rx)) (eraseMin (Ordnode.node' rl rx rr)) o₂
e : size (Ordnode.node' rl rx rr) = size (eraseMin (Ordnode.node' rl rx rr)) + 1
⊢ (∃ l',
      Raised l' (size (Ordnode.node ls ll lx lr)) ∧
        BalancedSz l' (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2)) ∨
    ∃ r',
      Raised (size (findMin' rl rx, eraseMin (Ordnode.node (size rl + size rr + 1) rl rx rr)).2) r' ∧
        BalancedSz (size (Ordnode.node ls ll lx lr)) r'

Next tactic:
refine' Or.inr ⟨_, Or.inr e, _⟩