l3lab/ntp-mathlib huggingface dataset:
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its left
side grow by 1, or its right side shrink by 1. -/
def balanceL (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id r with rs
  · cases' id l with ls ll lx lr
    · exact ι x
    · cases' id ll with lls
      · cases' lr with _ _ lrx
        · exact node 2 l x nil
        · exact node 3 (ι lx) lrx ι x
      · cases' id lr with lrs lrl lrx lrr
        · exact node 3 ll lx ι x
        · exact
            if lrs < ratio * lls then node (ls + 1) ll lx (node (lrs + 1) lr x nil)
            else
              node (ls + 1) (node (lls + size lrl + 1) ll lx lrl) lrx
                (node (size lrr + 1) lrr x nil)
  · cases' id l with ls ll lx lr
    · exact node (rs + 1) nil x r
    · refine' if ls > delta * rs then _ else node (ls + rs + 1) l x r
      cases' id ll with lls
      · exact nil
      --should not happen
      cases' id lr with lrs lrl lrx lrr
      · exact nil
      --should not happen
      

Proof state:
case node.node.node.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
l✝¹ : Ordnode α
x✝¹ : α
r✝¹ : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
lls : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
lrs : ℕ
lrl : Ordnode α
lrx : α
lrr : Ordnode α
⊢ Ordnode α

Next tactic:
exact
        if lrs < ratio * lls then node (ls + rs + 1) ll lx (node (rs + lrs + 1) lr x r)
        else
          node (ls + rs + 1) (node (lls + size lrl + 1) ll lx lrl) lrx
            (node (size lrr + rs + 1) lrr x r)
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  

Proof state:
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id l with ls
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · 

Proof state:
case nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id r with rs rl rx rr
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · 

Proof state:
case nil.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
⊢ Ordnode α

Next tactic:
exact ι x
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · 

Proof state:
case nil.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id rr with rrs
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · 

Proof state:
case nil.node.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ Ordnode α

Next tactic:
cases' rl with _ _ rlx
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · 

Proof state:
case nil.node.nil.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rx : α
rr : Ordnode α
⊢ Ordnode α

Next tactic:
exact node 2 nil x r
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · 

Proof state:
case nil.node.nil.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rx : α
rr : Ordnode α
size✝ : ℕ
l✝ : Ordnode α
rlx : α
r✝ : Ordnode α
⊢ Ordnode α

Next tactic:
exact node 3 (ι x) rlx ι rx
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · exact node 3 (ι x) rlx ι rx
      · 

Proof state:
case nil.node.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
rrs : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id rl with rls rll rlx rlr
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · exact node 3 (ι x) rlx ι rx
      · cases' id rl with rls rll rlx rlr
        · 

Proof state:
case nil.node.node.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
rrs : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
⊢ Ordnode α

Next tactic:
exact node 3 (ι x) rx rr
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · exact node 3 (ι x) rlx ι rx
      · cases' id rl with rls rll rlx rlr
        · exact node 3 (ι x) rx rr
        · 

Proof state:
case nil.node.node.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
rrs : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
rls : ℕ
rll : Ordnode α
rlx : α
rlr : Ordnode α
⊢ Ordnode α

Next tactic:
exact
            if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
            else
              node (rs + 1) (node (size rll + 1) nil x rll) rlx
                (node (size rlr + rrs + 1) rlr rx rr)
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · exact node 3 (ι x) rlx ι rx
      · cases' id rl with rls rll rlx rlr
        · exact node 3 (ι x) rx rr
        · exact
            if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
            else
              node (rs + 1) (node (size rll + 1) nil x rll) rlx
                (node (size rlr + rrs + 1) rlr rx rr)
  · 

Proof state:
case node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id r with rs rl rx rr
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · exact node 3 (ι x) rlx ι rx
      · cases' id rl with rls rll rlx rlr
        · exact node 3 (ι x) rx rr
        · exact
            if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
            else
              node (rs + 1) (node (size rll + 1) nil x rll) rlx
                (node (size rlr + rrs + 1) rlr rx rr)
  · cases' id r with rs rl rx rr
    · 

Proof state:
case node.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
⊢ Ordnode α

Next tactic:
exact node (ls + 1) l x nil
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · exact node 3 (ι x) rlx ι rx
      · cases' id rl with rls rll rlx rlr
        · exact node 3 (ι x) rx rr
        · exact
            if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
            else
              node (rs + 1) (node (size rll + 1) nil x rll) rlx
                (node (size rlr + rrs + 1) rlr rx rr)
  · cases' id r with rs rl rx rr
    · exact node (ls + 1) l x nil
    · 

Proof state:
case node.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ Ordnode α

Next tactic:
refine' if rs > delta * ls then _ else node (ls + rs + 1) l x r
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · exact node 3 (ι x) rlx ι rx
      · cases' id rl with rls rll rlx rlr
        · exact node 3 (ι x) rx rr
        · exact
            if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
            else
              node (rs + 1) (node (size rll + 1) nil x rll) rlx
                (node (size rlr + rrs + 1) rlr rx rr)
  · cases' id r with rs rl rx rr
    · exact node (ls + 1) l x nil
    · refine' if rs > delta * ls then _ else node (ls + rs + 1) l x r
      

Proof state:
case node.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id rr with rrs
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · exact node 3 (ι x) rlx ι rx
      · cases' id rl with rls rll rlx rlr
        · exact node 3 (ι x) rx rr
        · exact
            if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
            else
              node (rs + 1) (node (size rll + 1) nil x rll) rlx
                (node (size rlr + rrs + 1) rlr rx rr)
  · cases' id r with rs rl rx rr
    · exact node (ls + 1) l x nil
    · refine' if rs > delta * ls then _ else node (ls + rs + 1) l x r
      cases' id rr with rrs
      · 

Proof state:
case node.node.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ Ordnode α

Next tactic:
exact nil
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · exact node 3 (ι x) rlx ι rx
      · cases' id rl with rls rll rlx rlr
        · exact node 3 (ι x) rx rr
        · exact
            if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
            else
              node (rs + 1) (node (size rll + 1) nil x rll) rlx
                (node (size rlr + rrs + 1) rlr rx rr)
  · cases' id r with rs rl rx rr
    · exact node (ls + 1) l x nil
    · refine' if rs > delta * ls then _ else node (ls + rs + 1) l x r
      cases' id rr with rrs
      · exact nil
      --should not happen
      

Proof state:
case node.node.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
l✝¹ : Ordnode α
x✝¹ : α
r✝¹ : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
rrs : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id rl with rls rll rlx rlr
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · exact node 3 (ι x) rlx ι rx
      · cases' id rl with rls rll rlx rlr
        · exact node 3 (ι x) rx rr
        · exact
            if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
            else
              node (rs + 1) (node (size rll + 1) nil x rll) rlx
                (node (size rlr + rrs + 1) rlr rx rr)
  · cases' id r with rs rl rx rr
    · exact node (ls + 1) l x nil
    · refine' if rs > delta * ls then _ else node (ls + rs + 1) l x r
      cases' id rr with rrs
      · exact nil
      --should not happen
      cases' id rl with rls rll rlx rlr
      · 

Proof state:
case node.node.node.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
l✝¹ : Ordnode α
x✝¹ : α
r✝¹ : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
rrs : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
⊢ Ordnode α

Next tactic:
exact nil
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had its right
side grow by 1, or its left side shrink by 1. -/
def balanceR (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rr with rrs
      · cases' rl with _ _ rlx
        · exact node 2 nil x r
        · exact node 3 (ι x) rlx ι rx
      · cases' id rl with rls rll rlx rlr
        · exact node 3 (ι x) rx rr
        · exact
            if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
            else
              node (rs + 1) (node (size rll + 1) nil x rll) rlx
                (node (size rlr + rrs + 1) rlr rx rr)
  · cases' id r with rs rl rx rr
    · exact node (ls + 1) l x nil
    · refine' if rs > delta * ls then _ else node (ls + rs + 1) l x r
      cases' id rr with rrs
      · exact nil
      --should not happen
      cases' id rl with rls rll rlx rlr
      · exact nil
      --should not happen
      

Proof state:
case node.node.node.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
l✝¹ : Ordnode α
x✝¹ : α
r✝¹ : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
rrs : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
rls : ℕ
rll : Ordnode α
rlx : α
rlr : Ordnode α
⊢ Ordnode α

Next tactic:
exact
        if rls < ratio * rrs then node (ls + rs + 1) (node (ls + rls + 1) l x rl) rx rr
        else
          node (ls + rs + 1) (node (ls + size rll + 1) l x rll) rlx
            (node (size rlr + rrs + 1) rlr rx rr)
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had one side change
by at most 1. -/
def balance (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  

Proof state:
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id l with ls ll lx lr
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had one side change
by at most 1. -/
def balance (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls ll lx lr
  · 

Proof state:
case nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id r with rs rl rx rr
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had one side change
by at most 1. -/
def balance (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls ll lx lr
  · cases' id r with rs rl rx rr
    · 

Proof state:
case nil.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
⊢ Ordnode α

Next tactic:
exact ι x
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had one side change
by at most 1. -/
def balance (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls ll lx lr
  · cases' id r with rs rl rx rr
    · exact ι x
    · 

Proof state:
case nil.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id rl with rls rll rlx rlr
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had one side change
by at most 1. -/
def balance (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls ll lx lr
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rl with rls rll rlx rlr
      · 

Proof state:
case nil.node.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ Ordnode α

Next tactic:
cases id rr
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had one side change
by at most 1. -/
def balance (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls ll lx lr
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rl with rls rll rlx rlr
      · cases id rr
        · 

Proof state:
case nil.node.nil.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ Ordnode α

Next tactic:
exact node 2 nil x r
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had one side change
by at most 1. -/
def balance (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls ll lx lr
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rl with rls rll rlx rlr
      · cases id rr
        · exact node 2 nil x r
        · 

Proof state:
case nil.node.nil.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
size✝ : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
⊢ Ordnode α

Next tactic:
exact node 3 (ι x) rx rr
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had one side change
by at most 1. -/
def balance (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls ll lx lr
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rl with rls rll rlx rlr
      · cases id rr
        · exact node 2 nil x r
        · exact node 3 (ι x) rx rr
      · 

Proof state:
case nil.node.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
rls : ℕ
rll : Ordnode α
rlx : α
rlr : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id rr with rrs
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had one side change
by at most 1. -/
def balance (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls ll lx lr
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rl with rls rll rlx rlr
      · cases id rr
        · exact node 2 nil x r
        · exact node 3 (ι x) rx rr
      · cases' id rr with rrs
        · 

Proof state:
case nil.node.node.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
rls : ℕ
rll : Ordnode α
rlx : α
rlr : Ordnode α
⊢ Ordnode α

Next tactic:
exact node 3 (ι x) rlx ι rx
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had one side change
by at most 1. -/
def balance (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls ll lx lr
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rl with rls rll rlx rlr
      · cases id rr
        · exact node 2 nil x r
        · exact node 3 (ι x) rx rr
      · cases' id rr with rrs
        · exact node 3 (ι x) rlx ι rx
        · 

Proof state:
case nil.node.node.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
rls : ℕ
rll : Ordnode α
rlx : α
rlr : Ordnode α
rrs : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
⊢ Ordnode α

Next tactic:
exact
            if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
            else
              node (rs + 1) (node (size rll + 1) nil x rll) rlx
                (node (size rlr + rrs + 1) rlr rx rr)
/-- **Internal use only**

O(1). Rebalance a tree which was previously balanced but has had one side change
by at most 1. -/
def balance (l : Ordnode α) (x : α) (r : Ordnode α) : Ordnode α := by
  -- porting notes: removed `clean`
  cases' id l with ls ll lx lr
  · cases' id r with rs rl rx rr
    · exact ι x
    · cases' id rl with rls rll rlx rlr
      · cases id rr
        · exact node 2 nil x r
        · exact node 3 (ι x) rx rr
      · cases' id rr with rrs
        · exact node 3 (ι x) rlx ι rx
        · exact
            if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
            else
              node (rs + 1) (node (size rll + 1) nil x rll) rlx
                (node (size rlr + rrs + 1) rlr rx rr)
  · 

Proof state:
case node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id r with rs rl rx rr