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; · 

Proof state:
case node.nil
α : 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₂
hr : Valid' o₁ nil o₂
sep : All (fun x => All (fun y => x < y) nil) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size nil)
⊢ Valid' o₁ (glue (Ordnode.node ls ll lx lr) nil) o₂ ∧
    size (glue (Ordnode.node ls ll lx lr) nil) = size (Ordnode.node ls ll lx lr) + size nil

Next tactic:
exact ⟨hl, 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⟩
  

Proof state:
case node.node
α : 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))
⊢ Valid' o₁ (glue (Ordnode.node ls ll lx lr) (Ordnode.node rs rl rx rr)) o₂ ∧
    size (glue (Ordnode.node ls ll lx lr) (Ordnode.node rs rl rx rr)) =
      size (Ordnode.node ls ll lx lr) + size (Ordnode.node rs rl rx rr)

Next tactic:
dsimp [glue]
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]; 

Proof state:
case node.node
α : 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))
⊢ Valid' o₁
      (if ls > rs then Ordnode.balanceR (splitMax' ll lx lr).1 (splitMax' ll lx lr).2 (Ordnode.node rs rl rx rr)
      else Ordnode.balanceL (Ordnode.node ls ll lx lr) (splitMin' rl rx rr).1 (splitMin' rl rx rr).2)
      o₂ ∧
    size
        (if ls > rs then Ordnode.balanceR (splitMax' ll lx lr).1 (splitMax' ll lx lr).2 (Ordnode.node rs rl rx rr)
        else Ordnode.balanceL (Ordnode.node ls ll lx lr) (splitMin' rl rx rr).1 (splitMin' rl rx rr).2) =
      ls + rs

Next tactic:
split_ifs
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
  · 

Proof state:
case pos
α : 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.balanceR (splitMax' ll lx lr).1 (splitMax' ll lx lr).2 (Ordnode.node rs rl rx rr)) o₂ ∧
    size (Ordnode.balanceR (splitMax' ll lx lr).1 (splitMax' ll lx lr).2 (Ordnode.node rs rl rx rr)) = ls + rs

Next tactic:
rw [splitMax_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]
    

Proof state:
case pos
α : 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.balanceR (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).1
        (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).2 (Ordnode.node rs rl rx rr))
      o₂ ∧
    size
        (Ordnode.balanceR (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).1
          (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).2 (Ordnode.node rs rl rx rr)) =
      ls + rs
case pos.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'.eraseMax_aux hl 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
    

Proof state:
case pos.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' o₁ (eraseMax (Ordnode.node' ll lx lr)) ↑(findMax' lx lr)
e : size (Ordnode.node' ll lx lr) = size (eraseMax (Ordnode.node' ll lx lr)) + 1
⊢ Valid' o₁
      (Ordnode.balanceR (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).1
        (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).2 (Ordnode.node rs rl rx rr))
      o₂ ∧
    size
        (Ordnode.balanceR (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).1
          (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).2 (Ordnode.node rs rl rx rr)) =
      ls + rs
case pos.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'.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
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
      

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' o₁ (eraseMax (Ordnode.node' ll lx lr)) ↑(findMax' lx lr)
e : size (Ordnode.node' ll lx lr) = size (eraseMax (Ordnode.node' ll lx lr)) + 1
H : ?m.345468
⊢ Valid' o₁
      (Ordnode.balanceR (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).1
        (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).2 (Ordnode.node rs rl rx rr))
      o₂ ∧
    size
        (Ordnode.balanceR (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).1
          (eraseMax (Ordnode.node ?pos.s✝ ll lx lr), findMax' lx lr).2 (Ordnode.node rs rl rx rr)) =
      ls + rs

Next tactic:
refine' ⟨Valid'.balanceR v (hr.of_gt _ _) 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, _⟩
      · 

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' o₁ (eraseMax (Ordnode.node' ll lx lr)) ↑(findMax' lx lr)
e : size (Ordnode.node' ll lx lr) = size (eraseMax (Ordnode.node' ll lx lr)) + 1
H :
  (∃ l',
      Raised (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) l' ∧
        BalancedSz l' (size (Ordnode.node rs rl rx rr))) ∨
    ∃ r',
      Raised r' (size (Ordnode.node rs rl rx rr)) ∧
        BalancedSz (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) r'
⊢ Bounded nil (↑(eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).2) o₂

Next tactic:
refine' findMax'_all (P := fun a : α => Bounded nil (a : WithTop α) o₂)
          lx lr hl.1.2.to_nil (sep.2.2.imp _)
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 _)
        

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' o₁ (eraseMax (Ordnode.node' ll lx lr)) ↑(findMax' lx lr)
e : size (Ordnode.node' ll lx lr) = size (eraseMax (Ordnode.node' ll lx lr)) + 1
H :
  (∃ l',
      Raised (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) l' ∧
        BalancedSz l' (size (Ordnode.node rs rl rx rr))) ∨
    ∃ r',
      Raised r' (size (Ordnode.node rs rl rx rr)) ∧
        BalancedSz (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) r'
⊢ ∀ (a : α), All (fun y => a < y) (Ordnode.node rs rl rx rr) → Bounded nil (↑a) o₂

Next tactic:
exact fun x h => hr.1.2.to_nil.mono_left (le_of_lt h.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)
      · 

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' o₁ (eraseMax (Ordnode.node' ll lx lr)) ↑(findMax' lx lr)
e : size (Ordnode.node' ll lx lr) = size (eraseMax (Ordnode.node' ll lx lr)) + 1
H :
  (∃ l',
      Raised (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) l' ∧
        BalancedSz l' (size (Ordnode.node rs rl rx rr))) ∨
    ∃ r',
      Raised r' (size (Ordnode.node rs rl rx rr)) ∧
        BalancedSz (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) r'
⊢ All (fun x => x > (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).2)
    (Ordnode.node rs rl rx rr)

Next tactic:
exact @findMax'_all _ (fun a => All (· > a) (.node rs rl rx rr)) lx lr sep.2.1 sep.2.2
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
      · 

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' o₁ (eraseMax (Ordnode.node' ll lx lr)) ↑(findMax' lx lr)
e : size (Ordnode.node' ll lx lr) = size (eraseMax (Ordnode.node' ll lx lr)) + 1
H :
  (∃ l',
      Raised (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) l' ∧
        BalancedSz l' (size (Ordnode.node rs rl rx rr))) ∨
    ∃ r',
      Raised r' (size (Ordnode.node rs rl rx rr)) ∧
        BalancedSz (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) r'
⊢ size
      (Ordnode.balanceR (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1
        (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).2 (Ordnode.node rs rl rx rr)) =
    ls + rs

Next tactic:
rw [size_balanceR v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.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]; 

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' o₁ (eraseMax (Ordnode.node' ll lx lr)) ↑(findMax' lx lr)
e : size (Ordnode.node' ll lx lr) = size (eraseMax (Ordnode.node' ll lx lr)) + 1
H :
  (∃ l',
      Raised (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) l' ∧
        BalancedSz l' (size (Ordnode.node rs rl rx rr))) ∨
    ∃ r',
      Raised r' (size (Ordnode.node rs rl rx rr)) ∧
        BalancedSz (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) r'
⊢ size (Ordnode.node' ll lx lr) + size (Ordnode.node rs rl rx rr) = size ll + size lr + 1 + rs

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
    

Proof state:
case pos.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' o₁ (eraseMax (Ordnode.node' ll lx lr)) ↑(findMax' lx lr)
e : size (Ordnode.node' ll lx lr) = size (eraseMax (Ordnode.node' ll lx lr)) + 1
⊢ (∃ l',
      Raised (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) l' ∧
        BalancedSz l' (size (Ordnode.node rs rl rx rr))) ∨
    ∃ r',
      Raised r' (size (Ordnode.node rs rl rx rr)) ∧
        BalancedSz (size (eraseMax (Ordnode.node (size ll + size lr + 1) ll lx lr), findMax' lx lr).1) r'

Next tactic:
refine' Or.inl ⟨_, Or.inr 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, _⟩
    

Proof state:
case pos.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' o₁ (eraseMax (Ordnode.node' ll lx lr)) ↑(findMax' lx lr)
e : size (Ordnode.node' ll lx lr) = size (eraseMax (Ordnode.node' ll lx lr)) + 1
⊢ BalancedSz (size (Ordnode.node' ll lx lr)) (size (Ordnode.node rs rl rx rr))

Next tactic:
rwa [hl.2.eq_node'] at bal