-- Mathlib/.lake/packages/batteries/Batteries/Data/RBMap/WF.lean
theorem Balanced.append {l r : RBNode α}
    (hl : l.Balanced c₁ n) (hr : r.Balanced c₂ n) :
    (l.append r).RedRed (c₁ = black → c₂ ≠ black) n

Proof state:
α : Type u_1
c₁ : RBColor
n : Nat
c₂ : RBColor
x✝³ x✝² a✝¹ : RBNode α
x✝¹ : α
b c : RBNode α
y✝ : α
d✝ : RBNode α
hl : (node red a✝¹ x✝¹ b).Balanced c₁ n
hr : (node red c y✝ d✝).Balanced c₂ n
ha : a✝¹.Balanced black n
hb : b.Balanced black n
hc : c.Balanced black n
hd : d✝.Balanced black n
l✝ a✝ : RBNode α
x✝ : α
b✝ : RBNode α
e : b.append c = node red a✝ x✝ b✝
hb' : a✝.Balanced black n
hc' : b✝.Balanced black n
IH : (b.append c).Balanced red n
⊢ RedRed (red = black → red ≠ black) (node red (node red a✝¹ x✝¹ a✝) x✝ (node red b✝ y✝ d✝)) n

Tactic:
exact .redred nofun (.red ha hb') (.red hc' hd)

Result:
no goals

Proof state:
α : Type u_1
c₁ : RBColor
n : Nat
c₂ : RBColor
x✝³ x✝² a✝⁴ : RBNode α
x✝¹ : α
b c : RBNode α
y✝ : α
d✝ : RBNode α
hl : (node black a✝⁴ x✝¹ b).Balanced c₁ n
c₁✝² : RBColor
n✝ : Nat
c₂✝² : RBColor
ha : a✝⁴.Balanced c₁✝² n✝
hb : b.Balanced c₂✝² n✝
hr : (node black c y✝ d✝).Balanced c₂ (n✝ + 1)
c₁✝¹ c₂✝¹ : RBColor
hc : c.Balanced c₁✝¹ n✝
hd : d✝.Balanced c₂✝¹ n✝
IH : RedRed (c₂✝² = black → c₁✝¹ ≠ black) (b.append c) n✝
l✝ : RBNode α
H✝ : ∀ (a : RBNode α) (x : α) (b_1 : RBNode α), b.append c = node red a x b_1 → False
a✝³ : RBNode α
c₁✝ : RBColor
b✝ : RBNode α
c₂✝ : RBColor
x✝ : α
a✝² : c₂✝² = black → c₁✝¹ ≠ black
a✝¹ : a✝³.Balanced c₁✝ n✝
a✝ : b✝.Balanced c₂✝ n✝
H : ∀ (a : RBNode α) (x : α) (b : RBNode α), node red a✝³ x✝ b✝ = node red a x b → False
⊢ RedRed (black = black → black ≠ black) (a✝⁴.balLeft x✝¹ (node black (node red a✝³ x✝ b✝) y✝ d✝)) (n✝ + 1)

Tactic:
cases H _ _ _ rfl

Result:
no goals

Proof state:
α : Type u_1
c₁ : RBColor
n : Nat
c₂ : RBColor
x✝² x✝¹ a✝ : RBNode α
x✝ : α
b c : RBNode α
y✝ : α
d✝ : RBNode α
hl : (node black a✝ x✝ b).Balanced c₁ n
c₁✝ : RBColor
n✝ : Nat
c₂✝ : RBColor
ha : a✝.Balanced c₁✝ n✝
hb : b.Balanced c₂✝ n✝
hr : (node black c y✝ d✝).Balanced c₂ (n✝ + 1)
⊢ RedRed (black = black → c₂ ≠ black)
    (match b.append c with
    | node red b' z c' => node red (node black a✝ x✝ b') z (node black c' y✝ d✝)
    | bc => a✝.balLeft x✝ (node black bc y✝ d✝))
    (n✝ + 1)

Tactic:
have .black hc hd := hr

Result:
α : Type u_1
c₁ : RBColor
n : Nat
c₂ : RBColor
x✝² x✝¹ a✝ : RBNode α
x✝ : α
b c : RBNode α
y✝ : α
d✝ : RBNode α
hl : (node black a✝ x✝ b).Balanced c₁ n
c₁✝¹ : RBColor
n✝ : Nat
c₂✝¹ : RBColor
ha : a✝.Balanced c₁✝¹ n✝
hb : b.Balanced c₂✝¹ n✝
hr : (node black c y✝ d✝).Balanced c₂ (n✝ + 1)
c₁✝ c₂✝ : RBColor
hc : c.Balanced c₁✝ n✝
hd : d✝.Balanced c₂✝ n✝
⊢ RedRed (black = black → black ≠ black)
    (match b.append c with
    | node red b' z c' => node red (node black a✝ x✝ b') z (node black c' y✝ d✝)
    | bc => a✝.balLeft x✝ (node black bc y✝ d✝))
    (n✝ + 1)

Proof state:
case h_2
α : Type u_1
c₁ : RBColor
n : Nat
c₂ : RBColor
l : RBNode α
hl : l.Balanced c₁ n
x✝² x✝¹ : RBNode α
x✝ : l = nil → False
hr : nil.Balanced c₂ n
⊢ RedRed (c₁ = black → c₂ ≠ black) l n

Tactic:
exact .balanced hl

Result:
no goals

Proof state:
α : Type u_1
c₁ : RBColor
n : Nat
c₂ : RBColor
x✝² x✝¹ a✝ : RBNode α
x✝ : α
b c : RBNode α
y✝ : α
d✝ : RBNode α
hl : (node black a✝ x✝ b).Balanced c₁ n
hr : (node black c y✝ d✝).Balanced c₂ n
⊢ RedRed (c₁ = black → c₂ ≠ black)
    (match b.append c with
    | node red b' z c' => node red (node black a✝ x✝ b') z (node black c' y✝ d✝)
    | bc => a✝.balLeft x✝ (node black bc y✝ d✝))
    n

Tactic:
have .black ha hb := hl

Result:
α : Type u_1
c₁ : RBColor
n : Nat
c₂ : RBColor
x✝² x✝¹ a✝ : RBNode α
x✝ : α
b c : RBNode α
y✝ : α
d✝ : RBNode α
hl : (node black a✝ x✝ b).Balanced c₁ n
c₁✝ : RBColor
n✝ : Nat
c₂✝ : RBColor
ha : a✝.Balanced c₁✝ n✝
hb : b.Balanced c₂✝ n✝
hr : (node black c y✝ d✝).Balanced c₂ (n✝ + 1)
⊢ RedRed (black = black → c₂ ≠ black)
    (match b.append c with
    | node red b' z c' => node red (node black a✝ x✝ b') z (node black c' y✝ d✝)
    | bc => a✝.balLeft x✝ (node black bc y✝ d✝))
    (n✝ + 1)

Proof state:
case h_1
α : Type u_1
c₁ : RBColor
n : Nat
c₂ : RBColor
x✝⁴ x✝³ : RBNode α
x✝² : α
b c : RBNode α
y✝ : α
d✝ : RBNode α
c₁✝¹ : RBColor
n✝ : Nat
c₂✝¹ : RBColor
hb : b.Balanced c₂✝¹ n✝
hr : (node black c y✝ d✝).Balanced c₂ (n✝ + 1)
c₁✝ c₂✝ : RBColor
hc : c.Balanced c₁✝ n✝
hd : d✝.Balanced c₂✝ n✝
IH : RedRed (c₂✝¹ = black → c₁✝ ≠ black) (b.append c) n✝
l✝¹ : RBNode α
H : ∀ (a : RBNode α) (x : α) (b_1 : RBNode α), b.append c = node red a x b_1 → False
bc : RBNode α
c✝ : RBColor
hbc : bc.Balanced c✝ n✝
x✝¹ : ∀ (a : RBNode α) (x : α) (b : RBNode α), bc = node red a x b → False
l✝ a✝ : RBNode α
x✝ : α
b✝ : RBNode α
hl : (node black (node red a✝ x✝ b✝) x✝² b).Balanced c₁ n
ha : (node red a✝ x✝ b✝).Balanced c₁✝¹ n✝
ha' : a✝.Balanced black n✝
hb' : b✝.Balanced black n✝
⊢ RedRed (black = black → black ≠ black) (node red (node black a✝ x✝ b✝) x✝² (node black bc y✝ d✝)) (n✝ + 1)

Tactic:
exact .balanced (.red (.black ha' hb') (.black hbc hd))

Result:
no goals

Proof state:
α : Type u_1
c₁ : RBColor
n : Nat
c₂ : RBColor
l r : RBNode α
hl : l.Balanced c₁ n
hr : r.Balanced c₂ n
⊢ RedRed (c₁ = black → c₂ ≠ black) (l.append r) n

Tactic:
unfold append

Result:
α : Type u_1
c₁ : RBColor
n : Nat
c₂ : RBColor
l r : RBNode α
hl : l.Balanced c₁ n
hr : r.Balanced c₂ n
⊢ RedRed (c₁ = black → c₂ ≠ black)
    (match l, r with
    | nil, x => x
    | x, nil => x
    | node red a x b, node red c y d =>
      match b.append c with
      | node red b' z c' => node red (node red a x b') z (node red c' y d)
      | bc => node red a x (node red bc y d)
    | node black a x b, node black c y d =>
      match b.append c with
      | node red b' z c' => node red (node black a x b') z (node black c' y d)
      | bc => a.balLeft x (node black bc y d)
    | a@h:(node black l v r), node red b x c => node red (a.append b) x c
    | node red a x b, c@h:(node black l v r) => node red a x (b.append c))
    n

Proof state:
α : Type u_1
c₁ : RBColor
n : Nat
c₂ : RBColor
x✝³ x✝² a✝¹ : RBNode α
x✝¹ : α
b c : RBNode α
y✝ : α
d✝ : RBNode α
hl : (node black a✝¹ x✝¹ b).Balanced c₁ n
c₁✝¹ : RBColor
n✝ : Nat
c₂✝¹ : RBColor
ha : a✝¹.Balanced c₁✝¹ n✝
hb : b.Balanced c₂✝¹ n✝
hr : (node black c y✝ d✝).Balanced c₂ (n✝ + 1)
c₁✝ c₂✝ : RBColor
hc : c.Balanced c₁✝ n✝
hd : d✝.Balanced c₂✝ n✝
IH : RedRed (c₂✝¹ = black → c₁✝ ≠ black) (b.append c) n✝
l✝ a✝ : RBNode α
x✝ : α
b✝ : RBNode α
e : b.append c = node red a✝ x✝ b✝
⊢ RedRed (black = black → black ≠ black) (node red (node black a✝¹ x✝¹ a✝) x✝ (node black b✝ y✝ d✝)) (n✝ + 1)

Tactic:
match e ▸ IH with
| .balanced (.red hb' hc') | .redred _ hb' hc' =>
exact .balanced (.red (.black ha hb') (.black hc' hd))

Result:
no goals