Theorem: Std.RBNode.Balanced.ins
File: Std/Data/RBMap/WF.lean

tactic: exact .balanced (.red .nil .nil)
state_before: case nil
α : Type u_1
c : RBColor
n : Nat
cmp : α → α → Ordering
v : α
t : RBNode α
⊢ RedRed (isRed nil = red) (ins cmp v nil) 0

tactic: unfold ins
state_before: case red
α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n : Nat
b : RBNode α
x : α
hl : Balanced a black n
hr : Balanced b black n
ihl : RedRed (isRed a = red) (ins cmp v a) n
ihr : RedRed (isRed b = red) (ins cmp v b) n
⊢ RedRed (isRed (node red a x b) = red) (ins cmp v (node red a x b)) n

tactic: split
state_before: case red
α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n : Nat
b : RBNode α
x : α
hl : Balanced a black n
hr : Balanced b black n
ihl : RedRed (isRed a = red) (ins cmp v a) n
ihr : RedRed (isRed b = red) (ins cmp v b) n
⊢ RedRed (isRed (node red a x b) = red)
    (match cmp v x with
    | Ordering.lt => node red (ins cmp v a) x b
    | Ordering.gt => node red a x (ins cmp v b)
    | Ordering.eq => node red a v b)
    n

tactic: match ins cmp v a, ihl with
| _, .balanced .nil => exact .balanced (.red .nil hr)
| _, .balanced (.red ha hb) => exact .redred rfl (.red ha hb) hr
| _, .balanced (.black ha hb) => exact .balanced (.red (.black ha hb) hr)
| _, .redred h .. => cases hl <;> cases h
state_before: case red.h_1
α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n : Nat
b : RBNode α
x : α
hl : Balanced a black n
hr : Balanced b black n
ihl : RedRed (isRed a = red) (ins cmp v a) n
ihr : RedRed (isRed b = red) (ins cmp v b) n
x✝ : Ordering
heq✝ : cmp v x = Ordering.lt
⊢ RedRed (isRed (node red a x b) = red) (node red (ins cmp v a) x b) n

tactic: exact .balanced (.red .nil hr)
state_before: α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n : Nat
b : RBNode α
x : α
ihl : RedRed (isRed a = red) (ins cmp v a) n
x✝ : Ordering
heq✝ : cmp v x = Ordering.lt
hl : Balanced a black 0
hr : Balanced b black 0
ihr : RedRed (isRed b = red) (ins cmp v b) 0
⊢ RedRed (isRed (node red a x b) = red) (node red nil x b) 0

tactic: exact .redred rfl (.red ha hb) hr
state_before: α : Type u_1
c : RBColor
n✝¹ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n✝ : Nat
b : RBNode α
x : α
ihl : RedRed (isRed a = red) (ins cmp v a) n✝
x✝ : Ordering
heq✝ : cmp v x = Ordering.lt
n : Nat
l✝ : RBNode α
v✝ : α
r✝ : RBNode α
ha : Balanced l✝ black n
hb : Balanced r✝ black n
hl : Balanced a black n
hr : Balanced b black n
ihr : RedRed (isRed b = red) (ins cmp v b) n
⊢ RedRed (isRed (node red a x b) = red) (node red (node red l✝ v✝ r✝) x b) n

tactic: exact .balanced (.red (.black ha hb) hr)
state_before: α : Type u_1
c : RBColor
n✝¹ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n : Nat
b : RBNode α
x : α
ihl : RedRed (isRed a = red) (ins cmp v a) n
x✝ : Ordering
heq✝ : cmp v x = Ordering.lt
l✝ : RBNode α
v✝ : α
r✝ : RBNode α
n✝ : Nat
c₁✝ c₂✝ : RBColor
ha : Balanced l✝ c₁✝ n✝
hb : Balanced r✝ c₂✝ n✝
hl : Balanced a black (n✝ + 1)
hr : Balanced b black (n✝ + 1)
ihr : RedRed (isRed b = red) (ins cmp v b) (n✝ + 1)
⊢ RedRed (isRed (node red a x b) = red) (node red (node black l✝ v✝ r✝) x b) (n✝ + 1)

tactic: cases hl <;> cases h
state_before: α : Type u_1
c : RBColor
n✝¹ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n✝ : Nat
b : RBNode α
x : α
ihl : RedRed (isRed a = red) (ins cmp v a) n✝
x✝¹ : Ordering
heq✝ : cmp v x = Ordering.lt
n : Nat
a✝² : RBNode α
c₁✝ : RBColor
b✝ : RBNode α
c₂✝ : RBColor
x✝ : α
h : isRed a = red
a✝¹ : Balanced a✝² c₁✝ n
a✝ : Balanced b✝ c₂✝ n
hl : Balanced a black n
hr : Balanced b black n
ihr : RedRed (isRed b = red) (ins cmp v b) n
⊢ RedRed (isRed (node red a x b) = red) (node red (node red a✝² x✝ b✝) x b) n

tactic: match ins cmp v b, ihr with
| _, .balanced .nil => exact .balanced (.red hl .nil)
| _, .balanced (.red ha hb) => exact .redred rfl hl (.red ha hb)
| _, .balanced (.black ha hb) => exact .balanced (.red hl (.black ha hb))
| _, .redred h .. => cases hr <;> cases h
state_before: case red.h_2
α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n : Nat
b : RBNode α
x : α
hl : Balanced a black n
hr : Balanced b black n
ihl : RedRed (isRed a = red) (ins cmp v a) n
ihr : RedRed (isRed b = red) (ins cmp v b) n
x✝ : Ordering
heq✝ : cmp v x = Ordering.gt
⊢ RedRed (isRed (node red a x b) = red) (node red a x (ins cmp v b)) n

tactic: exact .balanced (.red hl .nil)
state_before: α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n : Nat
b : RBNode α
x : α
ihr : RedRed (isRed b = red) (ins cmp v b) n
x✝ : Ordering
heq✝ : cmp v x = Ordering.gt
hl : Balanced a black 0
hr : Balanced b black 0
ihl : RedRed (isRed a = red) (ins cmp v a) 0
⊢ RedRed (isRed (node red a x b) = red) (node red a x nil) 0

tactic: exact .redred rfl hl (.red ha hb)
state_before: α : Type u_1
c : RBColor
n✝¹ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n✝ : Nat
b : RBNode α
x : α
ihr : RedRed (isRed b = red) (ins cmp v b) n✝
x✝ : Ordering
heq✝ : cmp v x = Ordering.gt
n : Nat
l✝ : RBNode α
v✝ : α
r✝ : RBNode α
ha : Balanced l✝ black n
hb : Balanced r✝ black n
hl : Balanced a black n
hr : Balanced b black n
ihl : RedRed (isRed a = red) (ins cmp v a) n
⊢ RedRed (isRed (node red a x b) = red) (node red a x (node red l✝ v✝ r✝)) n

tactic: exact .balanced (.red hl (.black ha hb))
state_before: α : Type u_1
c : RBColor
n✝¹ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n : Nat
b : RBNode α
x : α
ihr : RedRed (isRed b = red) (ins cmp v b) n
x✝ : Ordering
heq✝ : cmp v x = Ordering.gt
l✝ : RBNode α
v✝ : α
r✝ : RBNode α
n✝ : Nat
c₁✝ c₂✝ : RBColor
ha : Balanced l✝ c₁✝ n✝
hb : Balanced r✝ c₂✝ n✝
hl : Balanced a black (n✝ + 1)
hr : Balanced b black (n✝ + 1)
ihl : RedRed (isRed a = red) (ins cmp v a) (n✝ + 1)
⊢ RedRed (isRed (node red a x b) = red) (node red a x (node black l✝ v✝ r✝)) (n✝ + 1)

tactic: cases hr <;> cases h
state_before: α : Type u_1
c : RBColor
n✝¹ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n✝ : Nat
b : RBNode α
x : α
ihr : RedRed (isRed b = red) (ins cmp v b) n✝
x✝¹ : Ordering
heq✝ : cmp v x = Ordering.gt
n : Nat
a✝² : RBNode α
c₁✝ : RBColor
b✝ : RBNode α
c₂✝ : RBColor
x✝ : α
h : isRed b = red
a✝¹ : Balanced a✝² c₁✝ n
a✝ : Balanced b✝ c₂✝ n
hl : Balanced a black n
hr : Balanced b black n
ihl : RedRed (isRed a = red) (ins cmp v a) n
⊢ RedRed (isRed (node red a x b) = red) (node red a x (node red a✝² x✝ b✝)) n

tactic: exact .balanced (.red hl hr)
state_before: case red.h_3
α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
n : Nat
b : RBNode α
x : α
hl : Balanced a black n
hr : Balanced b black n
ihl : RedRed (isRed a = red) (ins cmp v a) n
ihr : RedRed (isRed b = red) (ins cmp v b) n
x✝ : Ordering
heq✝ : cmp v x = Ordering.eq
⊢ RedRed (isRed (node red a x b) = red) (node red a v b) n

tactic: unfold ins
state_before: case black
α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
ca : RBColor
n : Nat
b : RBNode α
cb : RBColor
x : α
hl : Balanced a ca n
hr : Balanced b cb n
ihl : RedRed (isRed a = red) (ins cmp v a) n
ihr : RedRed (isRed b = red) (ins cmp v b) n
⊢ RedRed (isRed (node black a x b) = red) (ins cmp v (node black a x b)) (n + 1)

tactic: split
state_before: case black
α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
ca : RBColor
n : Nat
b : RBNode α
cb : RBColor
x : α
hl : Balanced a ca n
hr : Balanced b cb n
ihl : RedRed (isRed a = red) (ins cmp v a) n
ihr : RedRed (isRed b = red) (ins cmp v b) n
⊢ RedRed (isRed (node black a x b) = red)
    (match cmp v x with
    | Ordering.lt => balance1 (ins cmp v a) x b
    | Ordering.gt => balance2 a x (ins cmp v b)
    | Ordering.eq => node black a v b)
    (n + 1)

tactic: exact have ⟨c, h⟩ := ihl.balance1 hr; .balanced h
state_before: case black.h_1
α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
ca : RBColor
n : Nat
b : RBNode α
cb : RBColor
x : α
hl : Balanced a ca n
hr : Balanced b cb n
ihl : RedRed (isRed a = red) (ins cmp v a) n
ihr : RedRed (isRed b = red) (ins cmp v b) n
x✝ : Ordering
heq✝ : cmp v x = Ordering.lt
⊢ RedRed (isRed (node black a x b) = red) (balance1 (ins cmp v a) x b) (n + 1)

tactic: exact have ⟨c, h⟩ := ihr.balance2 hl; .balanced h
state_before: case black.h_2
α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
ca : RBColor
n : Nat
b : RBNode α
cb : RBColor
x : α
hl : Balanced a ca n
hr : Balanced b cb n
ihl : RedRed (isRed a = red) (ins cmp v a) n
ihr : RedRed (isRed b = red) (ins cmp v b) n
x✝ : Ordering
heq✝ : cmp v x = Ordering.gt
⊢ RedRed (isRed (node black a x b) = red) (balance2 a x (ins cmp v b)) (n + 1)

tactic: exact .balanced (.black hl hr)
state_before: case black.h_3
α : Type u_1
c : RBColor
n✝ : Nat
cmp : α → α → Ordering
v : α
t a : RBNode α
ca : RBColor
n : Nat
b : RBNode α
cb : RBColor
x : α
hl : Balanced a ca n
hr : Balanced b cb n
ihl : RedRed (isRed a = red) (ins cmp v a) n
ihr : RedRed (isRed b = red) (ins cmp v b) n
x✝ : Ordering
heq✝ : cmp v x = Ordering.eq
⊢ RedRed (isRed (node black a x b) = red) (node black a v b) (n + 1)