l3lab/ntp-mathlib huggingface dataset:
/-- **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)
  · cases' id r with rs rl rx rr
    · 

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

Next tactic:
cases' id ll with lls
/-- **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)
  · cases' id r with rs rl rx rr
    · cases' id ll with lls
      · 

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

Next tactic:
cases' lr with _ _ lrx
/-- **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)
  · cases' id r with rs rl rx rr
    · cases' id ll with lls
      · cases' lr with _ _ lrx
        · 

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

Next tactic:
exact node 2 l x nil
/-- **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)
  · cases' id r with rs rl rx rr
    · cases' id ll with lls
      · cases' lr with _ _ lrx
        · exact node 2 l x nil
        · 

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

Next tactic:
exact node 3 (ι lx) lrx ι 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
    · 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)
  · cases' id r with rs rl rx rr
    · cases' id ll with lls
      · cases' lr with _ _ lrx
        · exact node 2 l x nil
        · exact node 3 (ι lx) lrx ι x
      · 

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

Next tactic:
cases' id lr with lrs lrl lrx lrr
/-- **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)
  · cases' id r with rs rl rx rr
    · 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
        · 

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

Next tactic:
exact node 3 ll lx ι 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
    · 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)
  · cases' id r with rs rl rx rr
    · 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
        · 

Proof state:
case node.nil.node.node
α : Type u_1
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 + 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)
/-- **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)
  · cases' id r with rs rl rx rr
    · 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)
    · 

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

Next tactic:
refine'
        if delta * ls < rs then _ else if delta * rs < 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 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)
  · cases' id r with rs rl rx rr
    · 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)
    · refine'
        if delta * ls < rs then _ else if delta * rs < ls then _ else node (ls + rs + 1) l x r
      · 

Proof state:
case node.node.refine'_1
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : 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
      · 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)
  · cases' id r with rs rl rx rr
    · 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)
    · refine'
        if delta * ls < rs then _ else if delta * rs < ls then _ else node (ls + rs + 1) l x r
      · cases' id rl with rls rll rlx rlr
        · 

Proof state:
case node.node.refine'_1.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : 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 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)
  · cases' id r with rs rl rx rr
    · 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)
    · refine'
        if delta * ls < rs then _ else if delta * rs < ls then _ else node (ls + rs + 1) l x r
      · cases' id rl with rls rll rlx rlr
        · exact nil
        --should not happen
        

Proof state:
case node.node.refine'_1.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : 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
        · 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)
  · cases' id r with rs rl rx rr
    · 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)
    · refine'
        if delta * ls < rs then _ else if delta * rs < ls then _ else node (ls + rs + 1) l x r
      · cases' id rl with rls rll rlx rlr
        · exact nil
        --should not happen
        cases' id rr with rrs
        · 

Proof state:
case node.node.refine'_1.node.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
rls : ℕ
rll : Ordnode α
rlx : α
rlr : Ordnode α
⊢ Ordnode α

Next tactic:
exact nil
/-- **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)
  · cases' id r with rs rl rx rr
    · 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)
    · refine'
        if delta * ls < rs then _ else if delta * rs < ls then _ else node (ls + rs + 1) l x r
      · cases' id rl with rls rll rlx rlr
        · exact nil
        --should not happen
        cases' id rr with rrs
        · exact nil
        --should not happen
        

Proof state:
case node.node.refine'_1.node.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : 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 (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`
  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)
  · cases' id r with rs rl rx rr
    · 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)
    · refine'
        if delta * ls < rs then _ else if delta * rs < ls then _ else node (ls + rs + 1) l x r
      · cases' id rl with rls rll rlx rlr
        · exact nil
        --should not happen
        cases' id rr with rrs
        · exact nil
        --should not happen
        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)
      · 

Proof state:
case node.node.refine'_2
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id ll with lls
/-- **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)
  · cases' id r with rs rl rx rr
    · 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)
    · refine'
        if delta * ls < rs then _ else if delta * rs < ls then _ else node (ls + rs + 1) l x r
      · cases' id rl with rls rll rlx rlr
        · exact nil
        --should not happen
        cases' id rr with rrs
        · exact nil
        --should not happen
        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)
      · cases' id ll with lls
        · 

Proof state:
case node.node.refine'_2.nil
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : 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 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)
  · cases' id r with rs rl rx rr
    · 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)
    · refine'
        if delta * ls < rs then _ else if delta * rs < ls then _ else node (ls + rs + 1) l x r
      · cases' id rl with rls rll rlx rlr
        · exact nil
        --should not happen
        cases' id rr with rrs
        · exact nil
        --should not happen
        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)
      · cases' id ll with lls
        · exact nil
        --should not happen
        

Proof state:
case node.node.refine'_2.node
α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
lls : ℕ
l✝ : Ordnode α
x✝ : α
r✝ : Ordnode α
⊢ Ordnode α

Next tactic:
cases' id lr with lrs lrl lrx lrr