l3lab/ntp-mathlib huggingface dataset:
theorem Valid'.balanceL {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂)
    (H : (∃ l', Raised l' (size l) ∧ BalancedSz l' (size r)) ∨
        ∃ r', Raised (size r) r' ∧ BalancedSz (size l) r') :
    Valid' o₁ (@balanceL α l x r) o₂ := by
  rw [balanceL_eq_balance' hl.3 hr.3 hl.2 hr.2 H]
  refine' hl.balance' hr _
  rcases H with (⟨l', e, H⟩ | ⟨r', e, H⟩)
  · exact ⟨_, _, H, Or.inl ⟨e.dist_le', rfl⟩⟩
  · 

Proof state:
case inr.intro.intro
α : Type u_1
inst✝ : Preorder α
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
hl : Valid' o₁ l ↑x
hr : Valid' (↑x) r o₂
r' : ℕ
e : Raised (size r) r'
H : BalancedSz (size l) r'
⊢ ∃ l' r', BalancedSz l' r' ∧ (Nat.dist (size l) l' ≤ 1 ∧ size r = r' ∨ Nat.dist (size r) r' ≤ 1 ∧ size l = l')

Next tactic:
exact ⟨_, _, H, Or.inr ⟨e.dist_le, rfl⟩⟩
theorem Valid'.balanceR_aux {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂)
    (H₁ : size r = 0 → size l ≤ 1) (H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r)
    (H₃ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) : Valid' o₁ (@balanceR α l x r) o₂ := by
  

Proof state:
α : Type u_1
inst✝ : Preorder α
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
hl : Valid' o₁ l ↑x
hr : Valid' (↑x) r o₂
H₁ : size r = 0 → size l ≤ 1
H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r
H₃ : 2 * size r ≤ 9 * size l + 5 ∨ size r ≤ 3
⊢ Valid' o₁ (balanceR l x r) o₂

Next tactic:
rw [Valid'.dual_iff, dual_balanceR]
theorem Valid'.balanceR_aux {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂)
    (H₁ : size r = 0 → size l ≤ 1) (H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r)
    (H₃ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) : Valid' o₁ (@balanceR α l x r) o₂ := by
  rw [Valid'.dual_iff, dual_balanceR]
  

Proof state:
α : Type u_1
inst✝ : Preorder α
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
hl : Valid' o₁ l ↑x
hr : Valid' (↑x) r o₂
H₁ : size r = 0 → size l ≤ 1
H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r
H₃ : 2 * size r ≤ 9 * size l + 5 ∨ size r ≤ 3
⊢ Valid' o₂ (Ordnode.balanceL (Ordnode.dual r) x (Ordnode.dual l)) o₁

Next tactic:
have := hr.dual.balanceL_aux hl.dual
theorem Valid'.balanceR_aux {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂)
    (H₁ : size r = 0 → size l ≤ 1) (H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r)
    (H₃ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) : Valid' o₁ (@balanceR α l x r) o₂ := by
  rw [Valid'.dual_iff, dual_balanceR]
  have := hr.dual.balanceL_aux hl.dual
  

Proof state:
α : Type u_1
inst✝ : Preorder α
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
hl : Valid' o₁ l ↑x
hr : Valid' (↑x) r o₂
H₁ : size r = 0 → size l ≤ 1
H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r
H₃ : 2 * size r ≤ 9 * size l + 5 ∨ size r ≤ 3
this :
  (size (Ordnode.dual r) = 0 → size (Ordnode.dual l) ≤ 1) →
    (1 ≤ size (Ordnode.dual r) → 1 ≤ size (Ordnode.dual l) → size (Ordnode.dual l) ≤ delta * size (Ordnode.dual r)) →
      2 * size (Ordnode.dual r) ≤ 9 * size (Ordnode.dual l) + 5 ∨ size (Ordnode.dual r) ≤ 3 →
        Valid' o₂ (Ordnode.balanceL (Ordnode.dual r) x (Ordnode.dual l)) o₁
⊢ Valid' o₂ (Ordnode.balanceL (Ordnode.dual r) x (Ordnode.dual l)) o₁

Next tactic:
rw [size_dual, size_dual] at this
theorem Valid'.balanceR_aux {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂)
    (H₁ : size r = 0 → size l ≤ 1) (H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r)
    (H₃ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) : Valid' o₁ (@balanceR α l x r) o₂ := by
  rw [Valid'.dual_iff, dual_balanceR]
  have := hr.dual.balanceL_aux hl.dual
  rw [size_dual, size_dual] at this
  

Proof state:
α : Type u_1
inst✝ : Preorder α
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
hl : Valid' o₁ l ↑x
hr : Valid' (↑x) r o₂
H₁ : size r = 0 → size l ≤ 1
H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r
H₃ : 2 * size r ≤ 9 * size l + 5 ∨ size r ≤ 3
this :
  (size r = 0 → size l ≤ 1) →
    (1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r) →
      2 * size r ≤ 9 * size l + 5 ∨ size r ≤ 3 → Valid' o₂ (Ordnode.balanceL (Ordnode.dual r) x (Ordnode.dual l)) o₁
⊢ Valid' o₂ (Ordnode.balanceL (Ordnode.dual r) x (Ordnode.dual l)) o₁

Next tactic:
exact this H₁ H₂ H₃
theorem Valid'.balanceR {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂)
    (H : (∃ l', Raised (size l) l' ∧ BalancedSz l' (size r)) ∨
        ∃ r', Raised r' (size r) ∧ BalancedSz (size l) r') :
    Valid' o₁ (@balanceR α l x r) o₂ := by
  

Proof state:
α : Type u_1
inst✝ : Preorder α
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
hl : Valid' o₁ l ↑x
hr : Valid' (↑x) r o₂
H : (∃ l', Raised (size l) l' ∧ BalancedSz l' (size r)) ∨ ∃ r', Raised r' (size r) ∧ BalancedSz (size l) r'
⊢ Valid' o₁ (Ordnode.balanceR l x r) o₂

Next tactic:
rw [Valid'.dual_iff, dual_balanceR]
theorem Valid'.balanceR {l} {x : α} {r o₁ o₂} (hl : Valid' o₁ l x) (hr : Valid' x r o₂)
    (H : (∃ l', Raised (size l) l' ∧ BalancedSz l' (size r)) ∨
        ∃ r', Raised r' (size r) ∧ BalancedSz (size l) r') :
    Valid' o₁ (@balanceR α l x r) o₂ := by
  rw [Valid'.dual_iff, dual_balanceR]; 

Proof state:
α : Type u_1
inst✝ : Preorder α
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
hl : Valid' o₁ l ↑x
hr : Valid' (↑x) r o₂
H : (∃ l', Raised (size l) l' ∧ BalancedSz l' (size r)) ∨ ∃ r', Raised r' (size r) ∧ BalancedSz (size l) r'
⊢ Valid' o₂ (Ordnode.balanceL (Ordnode.dual r) x (Ordnode.dual l)) o₁

Next tactic:
exact hr.dual.balanceL hl.dual (balance_sz_dual H)
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  

Proof state:
α : Type u_1
inst✝ : Preorder α
s : ℕ
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
H : Valid' o₁ (Ordnode.node s l x r) o₂
⊢ Valid' o₁ (eraseMax (Ordnode.node' l x r)) ↑(findMax' x r) ∧
    size (Ordnode.node' l x r) = size (eraseMax (Ordnode.node' l x r)) + 1

Next tactic:
have := H.2.eq_node'
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  have := H.2.eq_node'; 

Proof state:
α : Type u_1
inst✝ : Preorder α
s : ℕ
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
H : Valid' o₁ (Ordnode.node s l x r) o₂
this : Ordnode.node s l x r = Ordnode.node' l x r
⊢ Valid' o₁ (eraseMax (Ordnode.node' l x r)) ↑(findMax' x r) ∧
    size (Ordnode.node' l x r) = size (eraseMax (Ordnode.node' l x r)) + 1

Next tactic:
rw [this] at H
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  have := H.2.eq_node'; rw [this] at H; 

Proof state:
α : Type u_1
inst✝ : Preorder α
s : ℕ
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
H : Valid' o₁ (Ordnode.node' l x r) o₂
this : Ordnode.node s l x r = Ordnode.node' l x r
⊢ Valid' o₁ (eraseMax (Ordnode.node' l x r)) ↑(findMax' x r) ∧
    size (Ordnode.node' l x r) = size (eraseMax (Ordnode.node' l x r)) + 1

Next tactic:
clear this
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  have := H.2.eq_node'; rw [this] at H; clear this
  

Proof state:
α : Type u_1
inst✝ : Preorder α
s : ℕ
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
H : Valid' o₁ (Ordnode.node' l x r) o₂
⊢ Valid' o₁ (eraseMax (Ordnode.node' l x r)) ↑(findMax' x r) ∧
    size (Ordnode.node' l x r) = size (eraseMax (Ordnode.node' l x r)) + 1

Next tactic:
induction' r with rs rl rx rr _ IHrr generalizing l x o₁
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  have := H.2.eq_node'; rw [this] at H; clear this
  induction' r with rs rl rx rr _ IHrr generalizing l x o₁
  · 

Proof state:
case nil
α : Type u_1
inst✝ : Preorder α
s : ℕ
o₂ : WithTop α
l : Ordnode α
x : α
o₁ : WithBot α
H : Valid' o₁ (Ordnode.node' l x nil) o₂
⊢ Valid' o₁ (eraseMax (Ordnode.node' l x nil)) ↑(findMax' x nil) ∧
    size (Ordnode.node' l x nil) = size (eraseMax (Ordnode.node' l x nil)) + 1

Next tactic:
exact ⟨H.left, rfl⟩
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  have := H.2.eq_node'; rw [this] at H; clear this
  induction' r with rs rl rx rr _ IHrr generalizing l x o₁
  · exact ⟨H.left, rfl⟩
  

Proof state:
case node
α : Type u_1
inst✝ : Preorder α
s : ℕ
o₂ : WithTop α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
l_ih✝ :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rl) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rl)) ↑(findMax' x rl) ∧
        size (Ordnode.node' l x rl) = size (eraseMax (Ordnode.node' l x rl)) + 1
IHrr :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rr) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rr)) ↑(findMax' x rr) ∧
        size (Ordnode.node' l x rr) = size (eraseMax (Ordnode.node' l x rr)) + 1
l : Ordnode α
x : α
o₁ : WithBot α
H : Valid' o₁ (Ordnode.node' l x (Ordnode.node rs rl rx rr)) o₂
⊢ Valid' o₁ (eraseMax (Ordnode.node' l x (Ordnode.node rs rl rx rr))) ↑(findMax' x (Ordnode.node rs rl rx rr)) ∧
    size (Ordnode.node' l x (Ordnode.node rs rl rx rr)) =
      size (eraseMax (Ordnode.node' l x (Ordnode.node rs rl rx rr))) + 1

Next tactic:
have := H.2.2.2.eq_node'
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  have := H.2.eq_node'; rw [this] at H; clear this
  induction' r with rs rl rx rr _ IHrr generalizing l x o₁
  · exact ⟨H.left, rfl⟩
  have := H.2.2.2.eq_node'; 

Proof state:
case node
α : Type u_1
inst✝ : Preorder α
s : ℕ
o₂ : WithTop α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
l_ih✝ :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rl) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rl)) ↑(findMax' x rl) ∧
        size (Ordnode.node' l x rl) = size (eraseMax (Ordnode.node' l x rl)) + 1
IHrr :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rr) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rr)) ↑(findMax' x rr) ∧
        size (Ordnode.node' l x rr) = size (eraseMax (Ordnode.node' l x rr)) + 1
l : Ordnode α
x : α
o₁ : WithBot α
H : Valid' o₁ (Ordnode.node' l x (Ordnode.node rs rl rx rr)) o₂
this : Ordnode.node rs rl rx rr = Ordnode.node' rl rx rr
⊢ Valid' o₁ (eraseMax (Ordnode.node' l x (Ordnode.node rs rl rx rr))) ↑(findMax' x (Ordnode.node rs rl rx rr)) ∧
    size (Ordnode.node' l x (Ordnode.node rs rl rx rr)) =
      size (eraseMax (Ordnode.node' l x (Ordnode.node rs rl rx rr))) + 1

Next tactic:
rw [this] at H ⊢
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  have := H.2.eq_node'; rw [this] at H; clear this
  induction' r with rs rl rx rr _ IHrr generalizing l x o₁
  · exact ⟨H.left, rfl⟩
  have := H.2.2.2.eq_node'; rw [this] at H ⊢
  

Proof state:
case node
α : Type u_1
inst✝ : Preorder α
s : ℕ
o₂ : WithTop α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
l_ih✝ :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rl) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rl)) ↑(findMax' x rl) ∧
        size (Ordnode.node' l x rl) = size (eraseMax (Ordnode.node' l x rl)) + 1
IHrr :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rr) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rr)) ↑(findMax' x rr) ∧
        size (Ordnode.node' l x rr) = size (eraseMax (Ordnode.node' l x rr)) + 1
l : Ordnode α
x : α
o₁ : WithBot α
H : Valid' o₁ (Ordnode.node' l x (Ordnode.node' rl rx rr)) o₂
this : Ordnode.node rs rl rx rr = Ordnode.node' rl rx rr
⊢ Valid' o₁ (eraseMax (Ordnode.node' l x (Ordnode.node' rl rx rr))) ↑(findMax' x (Ordnode.node' rl rx rr)) ∧
    size (Ordnode.node' l x (Ordnode.node' rl rx rr)) = size (eraseMax (Ordnode.node' l x (Ordnode.node' rl rx rr))) + 1

Next tactic:
rcases IHrr H.right with ⟨h, e⟩
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  have := H.2.eq_node'; rw [this] at H; clear this
  induction' r with rs rl rx rr _ IHrr generalizing l x o₁
  · exact ⟨H.left, rfl⟩
  have := H.2.2.2.eq_node'; rw [this] at H ⊢
  rcases IHrr H.right with ⟨h, e⟩
  

Proof state:
case node.intro
α : Type u_1
inst✝ : Preorder α
s : ℕ
o₂ : WithTop α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
l_ih✝ :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rl) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rl)) ↑(findMax' x rl) ∧
        size (Ordnode.node' l x rl) = size (eraseMax (Ordnode.node' l x rl)) + 1
IHrr :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rr) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rr)) ↑(findMax' x rr) ∧
        size (Ordnode.node' l x rr) = size (eraseMax (Ordnode.node' l x rr)) + 1
l : Ordnode α
x : α
o₁ : WithBot α
H : Valid' o₁ (Ordnode.node' l x (Ordnode.node' rl rx rr)) o₂
this : Ordnode.node rs rl rx rr = Ordnode.node' rl rx rr
h : Valid' (↑x) (eraseMax (Ordnode.node' rl rx rr)) ↑(findMax' rx rr)
e : size (Ordnode.node' rl rx rr) = size (eraseMax (Ordnode.node' rl rx rr)) + 1
⊢ Valid' o₁ (eraseMax (Ordnode.node' l x (Ordnode.node' rl rx rr))) ↑(findMax' x (Ordnode.node' rl rx rr)) ∧
    size (Ordnode.node' l x (Ordnode.node' rl rx rr)) = size (eraseMax (Ordnode.node' l x (Ordnode.node' rl rx rr))) + 1

Next tactic:
refine' ⟨Valid'.balanceL H.left h (Or.inr ⟨_, Or.inr e, H.3.1⟩), _⟩
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  have := H.2.eq_node'; rw [this] at H; clear this
  induction' r with rs rl rx rr _ IHrr generalizing l x o₁
  · exact ⟨H.left, rfl⟩
  have := H.2.2.2.eq_node'; rw [this] at H ⊢
  rcases IHrr H.right with ⟨h, e⟩
  refine' ⟨Valid'.balanceL H.left h (Or.inr ⟨_, Or.inr e, H.3.1⟩), _⟩
  

Proof state:
case node.intro
α : Type u_1
inst✝ : Preorder α
s : ℕ
o₂ : WithTop α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
l_ih✝ :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rl) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rl)) ↑(findMax' x rl) ∧
        size (Ordnode.node' l x rl) = size (eraseMax (Ordnode.node' l x rl)) + 1
IHrr :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rr) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rr)) ↑(findMax' x rr) ∧
        size (Ordnode.node' l x rr) = size (eraseMax (Ordnode.node' l x rr)) + 1
l : Ordnode α
x : α
o₁ : WithBot α
H : Valid' o₁ (Ordnode.node' l x (Ordnode.node' rl rx rr)) o₂
this : Ordnode.node rs rl rx rr = Ordnode.node' rl rx rr
h : Valid' (↑x) (eraseMax (Ordnode.node' rl rx rr)) ↑(findMax' rx rr)
e : size (Ordnode.node' rl rx rr) = size (eraseMax (Ordnode.node' rl rx rr)) + 1
⊢ size (Ordnode.node' l x (Ordnode.node' rl rx rr)) = size (eraseMax (Ordnode.node' l x (Ordnode.node' rl rx rr))) + 1

Next tactic:
rw [eraseMax, size_balanceL H.3.2.1 h.3 H.2.2.1 h.2 (Or.inr ⟨_, Or.inr e, H.3.1⟩)]
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  have := H.2.eq_node'; rw [this] at H; clear this
  induction' r with rs rl rx rr _ IHrr generalizing l x o₁
  · exact ⟨H.left, rfl⟩
  have := H.2.2.2.eq_node'; rw [this] at H ⊢
  rcases IHrr H.right with ⟨h, e⟩
  refine' ⟨Valid'.balanceL H.left h (Or.inr ⟨_, Or.inr e, H.3.1⟩), _⟩
  rw [eraseMax, size_balanceL H.3.2.1 h.3 H.2.2.1 h.2 (Or.inr ⟨_, Or.inr e, H.3.1⟩)]
  

Proof state:
case node.intro
α : Type u_1
inst✝ : Preorder α
s : ℕ
o₂ : WithTop α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
l_ih✝ :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rl) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rl)) ↑(findMax' x rl) ∧
        size (Ordnode.node' l x rl) = size (eraseMax (Ordnode.node' l x rl)) + 1
IHrr :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rr) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rr)) ↑(findMax' x rr) ∧
        size (Ordnode.node' l x rr) = size (eraseMax (Ordnode.node' l x rr)) + 1
l : Ordnode α
x : α
o₁ : WithBot α
H : Valid' o₁ (Ordnode.node' l x (Ordnode.node' rl rx rr)) o₂
this : Ordnode.node rs rl rx rr = Ordnode.node' rl rx rr
h : Valid' (↑x) (eraseMax (Ordnode.node' rl rx rr)) ↑(findMax' rx rr)
e : size (Ordnode.node' rl rx rr) = size (eraseMax (Ordnode.node' rl rx rr)) + 1
⊢ size (Ordnode.node' l x (Ordnode.node' rl rx rr)) = size l + size (eraseMax (Ordnode.node' rl rx rr)) + 1 + 1

Next tactic:
rw [size, e]
theorem Valid'.eraseMax_aux {s l x r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' o₁ (@eraseMax α (.node' l x r)) ↑(findMax' x r) ∧
      size (.node' l x r) = size (eraseMax (.node' l x r)) + 1 := by
  have := H.2.eq_node'; rw [this] at H; clear this
  induction' r with rs rl rx rr _ IHrr generalizing l x o₁
  · exact ⟨H.left, rfl⟩
  have := H.2.2.2.eq_node'; rw [this] at H ⊢
  rcases IHrr H.right with ⟨h, e⟩
  refine' ⟨Valid'.balanceL H.left h (Or.inr ⟨_, Or.inr e, H.3.1⟩), _⟩
  rw [eraseMax, size_balanceL H.3.2.1 h.3 H.2.2.1 h.2 (Or.inr ⟨_, Or.inr e, H.3.1⟩)]
  rw [size, e]; 

Proof state:
case node.intro
α : Type u_1
inst✝ : Preorder α
s : ℕ
o₂ : WithTop α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
l_ih✝ :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rl) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rl)) ↑(findMax' x rl) ∧
        size (Ordnode.node' l x rl) = size (eraseMax (Ordnode.node' l x rl)) + 1
IHrr :
  ∀ {l : Ordnode α} {x : α} {o₁ : WithBot α},
    Valid' o₁ (Ordnode.node' l x rr) o₂ →
      Valid' o₁ (eraseMax (Ordnode.node' l x rr)) ↑(findMax' x rr) ∧
        size (Ordnode.node' l x rr) = size (eraseMax (Ordnode.node' l x rr)) + 1
l : Ordnode α
x : α
o₁ : WithBot α
H : Valid' o₁ (Ordnode.node' l x (Ordnode.node' rl rx rr)) o₂
this : Ordnode.node rs rl rx rr = Ordnode.node' rl rx rr
h : Valid' (↑x) (eraseMax (Ordnode.node' rl rx rr)) ↑(findMax' rx rr)
e : size (Ordnode.node' rl rx rr) = size (eraseMax (Ordnode.node' rl rx rr)) + 1
⊢ size l + (size (eraseMax (Ordnode.node' rl rx rr)) + 1) + 1 =
    size l + size (eraseMax (Ordnode.node' rl rx rr)) + 1 + 1

Next tactic:
rfl
theorem Valid'.eraseMin_aux {s l} {x : α} {r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' ↑(findMin' l x) (@eraseMin α (.node' l x r)) o₂ ∧
      size (.node' l x r) = size (eraseMin (.node' l x r)) + 1 := by
  

Proof state:
α : Type u_1
inst✝ : Preorder α
s : ℕ
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
H : Valid' o₁ (Ordnode.node s l x r) o₂
⊢ Valid' (↑(findMin' l x)) (eraseMin (Ordnode.node' l x r)) o₂ ∧
    size (Ordnode.node' l x r) = size (eraseMin (Ordnode.node' l x r)) + 1

Next tactic:
have := H.dual.eraseMax_aux
theorem Valid'.eraseMin_aux {s l} {x : α} {r o₁ o₂} (H : Valid' o₁ (.node s l x r) o₂) :
    Valid' ↑(findMin' l x) (@eraseMin α (.node' l x r)) o₂ ∧
      size (.node' l x r) = size (eraseMin (.node' l x r)) + 1 := by
  have := H.dual.eraseMax_aux
  

Proof state:
α : Type u_1
inst✝ : Preorder α
s : ℕ
l : Ordnode α
x : α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
H : Valid' o₁ (Ordnode.node s l x r) o₂
this :
  Valid' o₂ (eraseMax (Ordnode.node' (Ordnode.dual r) x (Ordnode.dual l))) ↑(findMax' x (Ordnode.dual l)) ∧
    size (Ordnode.node' (Ordnode.dual r) x (Ordnode.dual l)) =
      size (eraseMax (Ordnode.node' (Ordnode.dual r) x (Ordnode.dual l))) + 1
⊢ Valid' (↑(findMin' l x)) (eraseMin (Ordnode.node' l x r)) o₂ ∧
    size (Ordnode.node' l x r) = size (eraseMin (Ordnode.node' l x r)) + 1

Next tactic:
rwa [← dual_node', size_dual, ← dual_eraseMin, size_dual, ← Valid'.dual_iff, findMax'_dual]
    at this
theorem eraseMin.valid : ∀ {t}, @Valid α _ t → Valid (eraseMin t)
  | nil, _ => valid_nil
  | node _ l x r, h => by 

Proof state:
α : Type u_1
inst✝ : Preorder α
size✝ : ℕ
l : Ordnode α
x : α
r : Ordnode α
h : Valid (node size✝ l x r)
⊢ Valid (eraseMin (node size✝ l x r))

Next tactic:
rw [h.2.eq_node']
theorem eraseMin.valid : ∀ {t}, @Valid α _ t → Valid (eraseMin t)
  | nil, _ => valid_nil
  | node _ l x r, h => by rw [h.2.eq_node']; 

Proof state:
α : Type u_1
inst✝ : Preorder α
size✝ : ℕ
l : Ordnode α
x : α
r : Ordnode α
h : Valid (node size✝ l x r)
⊢ Valid (eraseMin (node' l x r))

Next tactic:
exact h.eraseMin_aux.1.valid
theorem eraseMax.valid {t} (h : @Valid α _ t) : Valid (eraseMax t) := by
  

Proof state:
α : Type u_1
inst✝ : Preorder α
t : Ordnode α
h : Valid t
⊢ Valid (eraseMax t)

Next tactic:
rw [Valid.dual_iff, dual_eraseMax]
theorem eraseMax.valid {t} (h : @Valid α _ t) : Valid (eraseMax t) := by
  rw [Valid.dual_iff, dual_eraseMax]; 

Proof state:
α : Type u_1
inst✝ : Preorder α
t : Ordnode α
h : Valid t
⊢ Valid (eraseMin (dual t))

Next tactic:
exact eraseMin.valid h.dual
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
  

Proof state:
α : Type u_1
inst✝ : Preorder α
l r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
hl : Valid' o₁ l o₂
hr : Valid' o₁ r o₂
sep : All (fun x => All (fun y => x < y) r) l
bal : BalancedSz (size l) (size r)
⊢ Valid' o₁ (glue l r) o₂ ∧ size (glue l r) = size l + size r

Next tactic:
cases' l with ls ll lx lr
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; · 

Proof state:
case nil
α : Type u_1
inst✝ : Preorder α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
hr : Valid' o₁ r o₂
hl : Valid' o₁ nil o₂
sep : All (fun x => All (fun y => x < y) r) nil
bal : BalancedSz (size nil) (size r)
⊢ Valid' o₁ (glue nil r) o₂ ∧ size (glue nil r) = size nil + size r

Next tactic:
exact ⟨hr, (zero_add _).symm⟩
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⟩
  

Proof state:
case node
α : Type u_1
inst✝ : Preorder α
r : Ordnode α
o₁ : WithBot α
o₂ : WithTop α
hr : Valid' o₁ r o₂
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
hl : Valid' o₁ (Ordnode.node ls ll lx lr) o₂
sep : All (fun x => All (fun y => x < y) r) (Ordnode.node ls ll lx lr)
bal : BalancedSz (size (Ordnode.node ls ll lx lr)) (size r)
⊢ Valid' o₁ (glue (Ordnode.node ls ll lx lr) r) o₂ ∧
    size (glue (Ordnode.node ls ll lx lr) r) = size (Ordnode.node ls ll lx lr) + size r

Next tactic:
cases' r with rs rl rx rr