Theorem: Ordnode.dual_balanceL
File: Mathlib/Data/Ordmap/Ordset.lean

tactic: unfold balanceL balanceR
state_before: α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
⊢ (l.balanceL x r).dual = r.dual.balanceR x l.dual
state_after: α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
⊢ (Ordnode.casesOn (motive := fun t => id r = t → Ordnode α) (id r)
        (fun h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l) (fun h => Ordnode.singleton x)
            (fun ls ll lx lr h =>
              Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr = t → Ordnode α) lr (fun h => node 2 l x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr)
                    (fun h => node 3 ll lx (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls + 1) ll lx (node (lrs + 1) lr x nil)
                      else node (ls + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs l_1 x_1 r_1 h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l) (fun h => node (rs + 1) nil x r)
            (fun ls ll lx lr h =>
              if ls > delta * rs then
                Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll) (fun h => nil)
                  (fun lls l x_2 r_2 h =>
                    Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        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 + lrl.size + 1) ll lx lrl) lrx
                            (node (lrr.size + rs + 1) lrr x r))
                      ⋯)
                  ⋯
              else node (ls + rs + 1) l x r)
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id r.dual = t → Ordnode α) (id r.dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual) (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl (fun h => node 2 nil x l.dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls l_1 x_1 r_1 h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual) (fun h => node (ls + 1) r.dual x nil)
          (fun rs rl rx rr h =>
            if rs > delta * ls then
              Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr) (fun h => nil)
                (fun rrs l x_2 r_2 h =>
                  Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then node (ls + rs + 1) (node (ls + rls + 1) r.dual x rl) rx rr
                      else
                        node (ls + rs + 1) (node (ls + rll.size + 1) r.dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx rr))
                    ⋯)
                ⋯
            else node (ls + rs + 1) r.dual x l.dual)
          ⋯)
      ⋯

tactic: cases' r with rs rl rx rr
state_before: α : Type u_1
l : Ordnode α
x : α
r : Ordnode α
⊢ (Ordnode.casesOn (motive := fun t => id r = t → Ordnode α) (id r)
        (fun h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l) (fun h => Ordnode.singleton x)
            (fun ls ll lx lr h =>
              Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr = t → Ordnode α) lr (fun h => node 2 l x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr)
                    (fun h => node 3 ll lx (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls + 1) ll lx (node (lrs + 1) lr x nil)
                      else node (ls + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs l_1 x_1 r_1 h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l) (fun h => node (rs + 1) nil x r)
            (fun ls ll lx lr h =>
              if ls > delta * rs then
                Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll) (fun h => nil)
                  (fun lls l x_2 r_2 h =>
                    Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        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 + lrl.size + 1) ll lx lrl) lrx
                            (node (lrr.size + rs + 1) lrr x r))
                      ⋯)
                  ⋯
              else node (ls + rs + 1) l x r)
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id r.dual = t → Ordnode α) (id r.dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual) (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl (fun h => node 2 nil x l.dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls l_1 x_1 r_1 h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual) (fun h => node (ls + 1) r.dual x nil)
          (fun rs rl rx rr h =>
            if rs > delta * ls then
              Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr) (fun h => nil)
                (fun rrs l x_2 r_2 h =>
                  Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then node (ls + rs + 1) (node (ls + rls + 1) r.dual x rl) rx rr
                      else
                        node (ls + rs + 1) (node (ls + rll.size + 1) r.dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx rr))
                    ⋯)
                ⋯
            else node (ls + rs + 1) r.dual x l.dual)
          ⋯)
      ⋯
state_after: case nil
α : Type u_1
l : Ordnode α
x : α
⊢ (Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil)
        (fun h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l) (fun h => Ordnode.singleton x)
            (fun ls ll lx lr h =>
              Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr = t → Ordnode α) lr (fun h => node 2 l x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr)
                    (fun h => node 3 ll lx (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls + 1) ll lx (node (lrs + 1) lr x nil)
                      else node (ls + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs l_1 x_1 r h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l) (fun h => node (rs + 1) nil x nil)
            (fun ls ll lx lr h =>
              if ls > delta * rs then
                Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then node (ls + rs + 1) ll lx (node (rs + lrs + 1) lr x nil)
                        else
                          node (ls + rs + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx
                            (node (lrr.size + rs + 1) lrr x nil))
                      ⋯)
                  ⋯
              else node (ls + rs + 1) l x nil)
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual) (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl (fun h => node 2 nil x l.dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls l_1 x_1 r h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual)
          (fun h => node (ls + 1) nil.dual x nil)
          (fun rs rl rx rr h =>
            if rs > delta * ls then
              Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then node (ls + rs + 1) (node (ls + rls + 1) nil.dual x rl) rx rr
                      else
                        node (ls + rs + 1) (node (ls + rll.size + 1) nil.dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx rr))
                    ⋯)
                ⋯
            else node (ls + rs + 1) nil.dual x l.dual)
          ⋯)
      ⋯

case node
α : Type u_1
l : Ordnode α
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ (Ordnode.casesOn (motive := fun t => id (node rs rl rx rr) = t → Ordnode α) (id (node rs rl rx rr))
        (fun h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l) (fun h => Ordnode.singleton x)
            (fun ls ll lx lr h =>
              Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr = t → Ordnode α) lr (fun h => node 2 l x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr)
                    (fun h => node 3 ll lx (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls + 1) ll lx (node (lrs + 1) lr x nil)
                      else node (ls + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs_1 l_1 x_1 r h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l)
            (fun h => node (rs_1 + 1) nil x (node rs rl rx rr))
            (fun ls ll lx lr h =>
              if ls > delta * rs_1 then
                Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then
                          node (ls + rs_1 + 1) ll lx (node (rs_1 + lrs + 1) lr x (node rs rl rx rr))
                        else
                          node (ls + rs_1 + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx
                            (node (lrr.size + rs_1 + 1) lrr x (node rs rl rx rr)))
                      ⋯)
                  ⋯
              else node (ls + rs_1 + 1) l x (node rs rl rx rr))
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id (node rs rl rx rr).dual = t → Ordnode α) (id (node rs rl rx rr).dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual) (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl (fun h => node 2 nil x l.dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls l_1 x_1 r h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual)
          (fun h => node (ls + 1) (node rs rl rx rr).dual x nil)
          (fun rs_1 rl_1 rx_1 rr_1 h =>
            if rs_1 > delta * ls then
              Ordnode.casesOn (motive := fun t => id rr_1 = t → Ordnode α) (id rr_1) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl_1 = t → Ordnode α) (id rl_1) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then
                        node (ls + rs_1 + 1) (node (ls + rls + 1) (node rs rl rx rr).dual x rl_1) rx_1 rr_1
                      else
                        node (ls + rs_1 + 1) (node (ls + rll.size + 1) (node rs rl rx rr).dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx_1 rr_1))
                    ⋯)
                ⋯
            else node (ls + rs_1 + 1) (node rs rl rx rr).dual x l.dual)
          ⋯)
      ⋯

tactic: cases' l with ls ll lx lr
state_before: case nil
α : Type u_1
l : Ordnode α
x : α
⊢ (Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil)
        (fun h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l) (fun h => Ordnode.singleton x)
            (fun ls ll lx lr h =>
              Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr = t → Ordnode α) lr (fun h => node 2 l x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr)
                    (fun h => node 3 ll lx (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls + 1) ll lx (node (lrs + 1) lr x nil)
                      else node (ls + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs l_1 x_1 r h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l) (fun h => node (rs + 1) nil x nil)
            (fun ls ll lx lr h =>
              if ls > delta * rs then
                Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then node (ls + rs + 1) ll lx (node (rs + lrs + 1) lr x nil)
                        else
                          node (ls + rs + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx
                            (node (lrr.size + rs + 1) lrr x nil))
                      ⋯)
                  ⋯
              else node (ls + rs + 1) l x nil)
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual) (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl (fun h => node 2 nil x l.dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls l_1 x_1 r h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual)
          (fun h => node (ls + 1) nil.dual x nil)
          (fun rs rl rx rr h =>
            if rs > delta * ls then
              Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then node (ls + rs + 1) (node (ls + rls + 1) nil.dual x rl) rx rr
                      else
                        node (ls + rs + 1) (node (ls + rll.size + 1) nil.dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx rr))
                    ⋯)
                ⋯
            else node (ls + rs + 1) nil.dual x l.dual)
          ⋯)
      ⋯
state_after: case nil.nil
α : Type u_1
x : α
⊢ (Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil)
        (fun h =>
          Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil) (fun h => Ordnode.singleton x)
            (fun ls ll lx lr h =>
              Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr = t → Ordnode α) lr (fun h => node 2 nil x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr)
                    (fun h => node 3 ll lx (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls + 1) ll lx (node (lrs + 1) lr x nil)
                      else node (ls + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs l x_1 r h =>
          Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil) (fun h => node (rs + 1) nil x nil)
            (fun ls ll lx lr h =>
              if ls > delta * rs then
                Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then node (ls + rs + 1) ll lx (node (rs + lrs + 1) lr x nil)
                        else
                          node (ls + rs + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx
                            (node (lrr.size + rs + 1) lrr x nil))
                      ⋯)
                  ⋯
              else node (ls + rs + 1) nil x nil)
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual) (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl (fun h => node 2 nil x nil.dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls l x_1 r h =>
        Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual)
          (fun h => node (ls + 1) nil.dual x nil)
          (fun rs rl rx rr h =>
            if rs > delta * ls then
              Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then node (ls + rs + 1) (node (ls + rls + 1) nil.dual x rl) rx rr
                      else
                        node (ls + rs + 1) (node (ls + rll.size + 1) nil.dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx rr))
                    ⋯)
                ⋯
            else node (ls + rs + 1) nil.dual x nil.dual)
          ⋯)
      ⋯

case nil.node
α : Type u_1
x : α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
⊢ (Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil)
        (fun h =>
          Ordnode.casesOn (motive := fun t => id (node ls ll lx lr) = t → Ordnode α) (id (node ls ll lx lr))
            (fun h => Ordnode.singleton x)
            (fun ls_1 ll_1 lx_1 lr_1 h =>
              Ordnode.casesOn (motive := fun t => id ll_1 = t → Ordnode α) (id ll_1)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr_1 = t → Ordnode α) lr_1
                    (fun h => node 2 (node ls ll lx lr) x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx_1) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr_1 = t → Ordnode α) (id lr_1)
                    (fun h => node 3 ll_1 lx_1 (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls_1 + 1) ll_1 lx_1 (node (lrs + 1) lr_1 x nil)
                      else
                        node (ls_1 + 1) (node (lls + lrl.size + 1) ll_1 lx_1 lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs l x_1 r h =>
          Ordnode.casesOn (motive := fun t => id (node ls ll lx lr) = t → Ordnode α) (id (node ls ll lx lr))
            (fun h => node (rs + 1) nil x nil)
            (fun ls_1 ll_1 lx_1 lr_1 h =>
              if ls_1 > delta * rs then
                Ordnode.casesOn (motive := fun t => id ll_1 = t → Ordnode α) (id ll_1) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr_1 = t → Ordnode α) (id lr_1) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then node (ls_1 + rs + 1) ll_1 lx_1 (node (rs + lrs + 1) lr_1 x nil)
                        else
                          node (ls_1 + rs + 1) (node (lls + lrl.size + 1) ll_1 lx_1 lrl) lrx
                            (node (lrr.size + rs + 1) lrr x nil))
                      ⋯)
                  ⋯
              else node (ls_1 + rs + 1) (node ls ll lx lr) x nil)
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id (node ls ll lx lr).dual = t → Ordnode α) (id (node ls ll lx lr).dual)
          (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl
                  (fun h => node 2 nil x (node ls ll lx lr).dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls_1 l x_1 r h =>
        Ordnode.casesOn (motive := fun t => id (node ls ll lx lr).dual = t → Ordnode α) (id (node ls ll lx lr).dual)
          (fun h => node (ls_1 + 1) nil.dual x nil)
          (fun rs rl rx rr h =>
            if rs > delta * ls_1 then
              Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then node (ls_1 + rs + 1) (node (ls_1 + rls + 1) nil.dual x rl) rx rr
                      else
                        node (ls_1 + rs + 1) (node (ls_1 + rll.size + 1) nil.dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx rr))
                    ⋯)
                ⋯
            else node (ls_1 + rs + 1) nil.dual x (node ls ll lx lr).dual)
          ⋯)
      ⋯

tactic: cases' ll with lls lll llx llr <;> cases' lr with lrs lrl lrx lrr <;> dsimp only [dual, id] <;>
  try rfl
state_before: case nil.node
α : Type u_1
x : α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
⊢ (Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil)
        (fun h =>
          Ordnode.casesOn (motive := fun t => id (node ls ll lx lr) = t → Ordnode α) (id (node ls ll lx lr))
            (fun h => Ordnode.singleton x)
            (fun ls_1 ll_1 lx_1 lr_1 h =>
              Ordnode.casesOn (motive := fun t => id ll_1 = t → Ordnode α) (id ll_1)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr_1 = t → Ordnode α) lr_1
                    (fun h => node 2 (node ls ll lx lr) x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx_1) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr_1 = t → Ordnode α) (id lr_1)
                    (fun h => node 3 ll_1 lx_1 (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls_1 + 1) ll_1 lx_1 (node (lrs + 1) lr_1 x nil)
                      else
                        node (ls_1 + 1) (node (lls + lrl.size + 1) ll_1 lx_1 lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs l x_1 r h =>
          Ordnode.casesOn (motive := fun t => id (node ls ll lx lr) = t → Ordnode α) (id (node ls ll lx lr))
            (fun h => node (rs + 1) nil x nil)
            (fun ls_1 ll_1 lx_1 lr_1 h =>
              if ls_1 > delta * rs then
                Ordnode.casesOn (motive := fun t => id ll_1 = t → Ordnode α) (id ll_1) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr_1 = t → Ordnode α) (id lr_1) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then node (ls_1 + rs + 1) ll_1 lx_1 (node (rs + lrs + 1) lr_1 x nil)
                        else
                          node (ls_1 + rs + 1) (node (lls + lrl.size + 1) ll_1 lx_1 lrl) lrx
                            (node (lrr.size + rs + 1) lrr x nil))
                      ⋯)
                  ⋯
              else node (ls_1 + rs + 1) (node ls ll lx lr) x nil)
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id (node ls ll lx lr).dual = t → Ordnode α) (id (node ls ll lx lr).dual)
          (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl
                  (fun h => node 2 nil x (node ls ll lx lr).dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls_1 l x_1 r h =>
        Ordnode.casesOn (motive := fun t => id (node ls ll lx lr).dual = t → Ordnode α) (id (node ls ll lx lr).dual)
          (fun h => node (ls_1 + 1) nil.dual x nil)
          (fun rs rl rx rr h =>
            if rs > delta * ls_1 then
              Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then node (ls_1 + rs + 1) (node (ls_1 + rls + 1) nil.dual x rl) rx rr
                      else
                        node (ls_1 + rs + 1) (node (ls_1 + rll.size + 1) nil.dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx rr))
                    ⋯)
                ⋯
            else node (ls_1 + rs + 1) nil.dual x (node ls ll lx lr).dual)
          ⋯)
      ⋯
state_after: case nil.node.node.node
α : Type u_1
x : α
ls : ℕ
lx : α
lls : ℕ
lll : Ordnode α
llx : α
llr : Ordnode α
lrs : ℕ
lrl : Ordnode α
lrx : α
lrr : Ordnode α
⊢ (if lrs < ratio * lls then node (ls + 1) (node lls lll llx llr) lx (node (lrs + 1) (node lrs lrl lrx lrr) x nil)
      else
        node (ls + 1) (node (lls + lrl.size + 1) (node lls lll llx llr) lx lrl) lrx
          (node (lrr.size + 1) lrr x nil)).dual =
    if lrs < ratio * lls then
      node (ls + 1) (node (lrs + 1) nil x (node lrs lrr.dual lrx lrl.dual)) lx (node lls llr.dual llx lll.dual)
    else
      node (ls + 1) (node (lrr.dual.size + 1) nil x lrr.dual) lrx
        (node (lrl.dual.size + lls + 1) lrl.dual lx (node lls llr.dual llx lll.dual))

tactic: split_ifs with h <;> repeat simp [h, add_comm]
state_before: case nil.node.node.node
α : Type u_1
x : α
ls : ℕ
lx : α
lls : ℕ
lll : Ordnode α
llx : α
llr : Ordnode α
lrs : ℕ
lrl : Ordnode α
lrx : α
lrr : Ordnode α
⊢ (if lrs < ratio * lls then node (ls + 1) (node lls lll llx llr) lx (node (lrs + 1) (node lrs lrl lrx lrr) x nil)
      else
        node (ls + 1) (node (lls + lrl.size + 1) (node lls lll llx llr) lx lrl) lrx
          (node (lrr.size + 1) lrr x nil)).dual =
    if lrs < ratio * lls then
      node (ls + 1) (node (lrs + 1) nil x (node lrs lrr.dual lrx lrl.dual)) lx (node lls llr.dual llx lll.dual)
    else
      node (ls + 1) (node (lrr.dual.size + 1) nil x lrr.dual) lrx
        (node (lrl.dual.size + lls + 1) lrl.dual lx (node lls llr.dual llx lll.dual))
state_after: no goals

tactic: rfl
state_before: case nil.nil
α : Type u_1
x : α
⊢ (Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil)
        (fun h =>
          Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil) (fun h => Ordnode.singleton x)
            (fun ls ll lx lr h =>
              Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr = t → Ordnode α) lr (fun h => node 2 nil x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr)
                    (fun h => node 3 ll lx (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls + 1) ll lx (node (lrs + 1) lr x nil)
                      else node (ls + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs l x_1 r h =>
          Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil) (fun h => node (rs + 1) nil x nil)
            (fun ls ll lx lr h =>
              if ls > delta * rs then
                Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then node (ls + rs + 1) ll lx (node (rs + lrs + 1) lr x nil)
                        else
                          node (ls + rs + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx
                            (node (lrr.size + rs + 1) lrr x nil))
                      ⋯)
                  ⋯
              else node (ls + rs + 1) nil x nil)
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual) (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl (fun h => node 2 nil x nil.dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls l x_1 r h =>
        Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual)
          (fun h => node (ls + 1) nil.dual x nil)
          (fun rs rl rx rr h =>
            if rs > delta * ls then
              Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then node (ls + rs + 1) (node (ls + rls + 1) nil.dual x rl) rx rr
                      else
                        node (ls + rs + 1) (node (ls + rll.size + 1) nil.dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx rr))
                    ⋯)
                ⋯
            else node (ls + rs + 1) nil.dual x nil.dual)
          ⋯)
      ⋯
state_after: no goals

tactic: rfl
state_before: case nil.node.node.nil
α : Type u_1
x : α
ls : ℕ
lx : α
lls : ℕ
lll : Ordnode α
llx : α
llr : Ordnode α
⊢ node 3 (node 1 nil x nil) lx (node lls llr.dual llx lll.dual) =
    node 3 (Ordnode.singleton x) lx (node lls llr.dual llx lll.dual)
state_after: no goals

tactic: simp [h, add_comm]
state_before: case neg
α : Type u_1
x : α
ls : ℕ
lx : α
lls : ℕ
lll : Ordnode α
llx : α
llr : Ordnode α
lrs : ℕ
lrl : Ordnode α
lrx : α
lrr : Ordnode α
h : ¬lrs < ratio * lls
⊢ (node (ls + 1) (node (lls + lrl.size + 1) (node lls lll llx llr) lx lrl) lrx (node (lrr.size + 1) lrr x nil)).dual =
    node (ls + 1) (node (lrr.dual.size + 1) nil x lrr.dual) lrx
      (node (lrl.dual.size + lls + 1) lrl.dual lx (node lls llr.dual llx lll.dual))
state_after: no goals

tactic: cases' l with ls ll lx lr
state_before: case node
α : Type u_1
l : Ordnode α
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ (Ordnode.casesOn (motive := fun t => id (node rs rl rx rr) = t → Ordnode α) (id (node rs rl rx rr))
        (fun h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l) (fun h => Ordnode.singleton x)
            (fun ls ll lx lr h =>
              Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr = t → Ordnode α) lr (fun h => node 2 l x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr)
                    (fun h => node 3 ll lx (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls + 1) ll lx (node (lrs + 1) lr x nil)
                      else node (ls + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs_1 l_1 x_1 r h =>
          Ordnode.casesOn (motive := fun t => id l = t → Ordnode α) (id l)
            (fun h => node (rs_1 + 1) nil x (node rs rl rx rr))
            (fun ls ll lx lr h =>
              if ls > delta * rs_1 then
                Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then
                          node (ls + rs_1 + 1) ll lx (node (rs_1 + lrs + 1) lr x (node rs rl rx rr))
                        else
                          node (ls + rs_1 + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx
                            (node (lrr.size + rs_1 + 1) lrr x (node rs rl rx rr)))
                      ⋯)
                  ⋯
              else node (ls + rs_1 + 1) l x (node rs rl rx rr))
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id (node rs rl rx rr).dual = t → Ordnode α) (id (node rs rl rx rr).dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual) (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl (fun h => node 2 nil x l.dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls l_1 x_1 r h =>
        Ordnode.casesOn (motive := fun t => id l.dual = t → Ordnode α) (id l.dual)
          (fun h => node (ls + 1) (node rs rl rx rr).dual x nil)
          (fun rs_1 rl_1 rx_1 rr_1 h =>
            if rs_1 > delta * ls then
              Ordnode.casesOn (motive := fun t => id rr_1 = t → Ordnode α) (id rr_1) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl_1 = t → Ordnode α) (id rl_1) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then
                        node (ls + rs_1 + 1) (node (ls + rls + 1) (node rs rl rx rr).dual x rl_1) rx_1 rr_1
                      else
                        node (ls + rs_1 + 1) (node (ls + rll.size + 1) (node rs rl rx rr).dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx_1 rr_1))
                    ⋯)
                ⋯
            else node (ls + rs_1 + 1) (node rs rl rx rr).dual x l.dual)
          ⋯)
      ⋯
state_after: case node.nil
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ (Ordnode.casesOn (motive := fun t => id (node rs rl rx rr) = t → Ordnode α) (id (node rs rl rx rr))
        (fun h =>
          Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil) (fun h => Ordnode.singleton x)
            (fun ls ll lx lr h =>
              Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr = t → Ordnode α) lr (fun h => node 2 nil x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr)
                    (fun h => node 3 ll lx (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls + 1) ll lx (node (lrs + 1) lr x nil)
                      else node (ls + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs_1 l x_1 r h =>
          Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil)
            (fun h => node (rs_1 + 1) nil x (node rs rl rx rr))
            (fun ls ll lx lr h =>
              if ls > delta * rs_1 then
                Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then
                          node (ls + rs_1 + 1) ll lx (node (rs_1 + lrs + 1) lr x (node rs rl rx rr))
                        else
                          node (ls + rs_1 + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx
                            (node (lrr.size + rs_1 + 1) lrr x (node rs rl rx rr)))
                      ⋯)
                  ⋯
              else node (ls + rs_1 + 1) nil x (node rs rl rx rr))
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id (node rs rl rx rr).dual = t → Ordnode α) (id (node rs rl rx rr).dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual) (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl (fun h => node 2 nil x nil.dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls l x_1 r h =>
        Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual)
          (fun h => node (ls + 1) (node rs rl rx rr).dual x nil)
          (fun rs_1 rl_1 rx_1 rr_1 h =>
            if rs_1 > delta * ls then
              Ordnode.casesOn (motive := fun t => id rr_1 = t → Ordnode α) (id rr_1) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl_1 = t → Ordnode α) (id rl_1) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then
                        node (ls + rs_1 + 1) (node (ls + rls + 1) (node rs rl rx rr).dual x rl_1) rx_1 rr_1
                      else
                        node (ls + rs_1 + 1) (node (ls + rll.size + 1) (node rs rl rx rr).dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx_1 rr_1))
                    ⋯)
                ⋯
            else node (ls + rs_1 + 1) (node rs rl rx rr).dual x nil.dual)
          ⋯)
      ⋯

case node.node
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
⊢ (Ordnode.casesOn (motive := fun t => id (node rs rl rx rr) = t → Ordnode α) (id (node rs rl rx rr))
        (fun h =>
          Ordnode.casesOn (motive := fun t => id (node ls ll lx lr) = t → Ordnode α) (id (node ls ll lx lr))
            (fun h => Ordnode.singleton x)
            (fun ls_1 ll_1 lx_1 lr_1 h =>
              Ordnode.casesOn (motive := fun t => id ll_1 = t → Ordnode α) (id ll_1)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr_1 = t → Ordnode α) lr_1
                    (fun h => node 2 (node ls ll lx lr) x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx_1) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr_1 = t → Ordnode α) (id lr_1)
                    (fun h => node 3 ll_1 lx_1 (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls_1 + 1) ll_1 lx_1 (node (lrs + 1) lr_1 x nil)
                      else
                        node (ls_1 + 1) (node (lls + lrl.size + 1) ll_1 lx_1 lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs_1 l x_1 r h =>
          Ordnode.casesOn (motive := fun t => id (node ls ll lx lr) = t → Ordnode α) (id (node ls ll lx lr))
            (fun h => node (rs_1 + 1) nil x (node rs rl rx rr))
            (fun ls_1 ll_1 lx_1 lr_1 h =>
              if ls_1 > delta * rs_1 then
                Ordnode.casesOn (motive := fun t => id ll_1 = t → Ordnode α) (id ll_1) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr_1 = t → Ordnode α) (id lr_1) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then
                          node (ls_1 + rs_1 + 1) ll_1 lx_1 (node (rs_1 + lrs + 1) lr_1 x (node rs rl rx rr))
                        else
                          node (ls_1 + rs_1 + 1) (node (lls + lrl.size + 1) ll_1 lx_1 lrl) lrx
                            (node (lrr.size + rs_1 + 1) lrr x (node rs rl rx rr)))
                      ⋯)
                  ⋯
              else node (ls_1 + rs_1 + 1) (node ls ll lx lr) x (node rs rl rx rr))
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id (node rs rl rx rr).dual = t → Ordnode α) (id (node rs rl rx rr).dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id (node ls ll lx lr).dual = t → Ordnode α) (id (node ls ll lx lr).dual)
          (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl
                  (fun h => node 2 nil x (node ls ll lx lr).dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls_1 l x_1 r h =>
        Ordnode.casesOn (motive := fun t => id (node ls ll lx lr).dual = t → Ordnode α) (id (node ls ll lx lr).dual)
          (fun h => node (ls_1 + 1) (node rs rl rx rr).dual x nil)
          (fun rs_1 rl_1 rx_1 rr_1 h =>
            if rs_1 > delta * ls_1 then
              Ordnode.casesOn (motive := fun t => id rr_1 = t → Ordnode α) (id rr_1) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl_1 = t → Ordnode α) (id rl_1) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then
                        node (ls_1 + rs_1 + 1) (node (ls_1 + rls + 1) (node rs rl rx rr).dual x rl_1) rx_1 rr_1
                      else
                        node (ls_1 + rs_1 + 1) (node (ls_1 + rll.size + 1) (node rs rl rx rr).dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx_1 rr_1))
                    ⋯)
                ⋯
            else node (ls_1 + rs_1 + 1) (node rs rl rx rr).dual x (node ls ll lx lr).dual)
          ⋯)
      ⋯

tactic: dsimp only [dual, id]
state_before: case node.node
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
⊢ (Ordnode.casesOn (motive := fun t => id (node rs rl rx rr) = t → Ordnode α) (id (node rs rl rx rr))
        (fun h =>
          Ordnode.casesOn (motive := fun t => id (node ls ll lx lr) = t → Ordnode α) (id (node ls ll lx lr))
            (fun h => Ordnode.singleton x)
            (fun ls_1 ll_1 lx_1 lr_1 h =>
              Ordnode.casesOn (motive := fun t => id ll_1 = t → Ordnode α) (id ll_1)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr_1 = t → Ordnode α) lr_1
                    (fun h => node 2 (node ls ll lx lr) x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx_1) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr_1 = t → Ordnode α) (id lr_1)
                    (fun h => node 3 ll_1 lx_1 (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls_1 + 1) ll_1 lx_1 (node (lrs + 1) lr_1 x nil)
                      else
                        node (ls_1 + 1) (node (lls + lrl.size + 1) ll_1 lx_1 lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs_1 l x_1 r h =>
          Ordnode.casesOn (motive := fun t => id (node ls ll lx lr) = t → Ordnode α) (id (node ls ll lx lr))
            (fun h => node (rs_1 + 1) nil x (node rs rl rx rr))
            (fun ls_1 ll_1 lx_1 lr_1 h =>
              if ls_1 > delta * rs_1 then
                Ordnode.casesOn (motive := fun t => id ll_1 = t → Ordnode α) (id ll_1) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr_1 = t → Ordnode α) (id lr_1) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then
                          node (ls_1 + rs_1 + 1) ll_1 lx_1 (node (rs_1 + lrs + 1) lr_1 x (node rs rl rx rr))
                        else
                          node (ls_1 + rs_1 + 1) (node (lls + lrl.size + 1) ll_1 lx_1 lrl) lrx
                            (node (lrr.size + rs_1 + 1) lrr x (node rs rl rx rr)))
                      ⋯)
                  ⋯
              else node (ls_1 + rs_1 + 1) (node ls ll lx lr) x (node rs rl rx rr))
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id (node rs rl rx rr).dual = t → Ordnode α) (id (node rs rl rx rr).dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id (node ls ll lx lr).dual = t → Ordnode α) (id (node ls ll lx lr).dual)
          (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl
                  (fun h => node 2 nil x (node ls ll lx lr).dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls_1 l x_1 r h =>
        Ordnode.casesOn (motive := fun t => id (node ls ll lx lr).dual = t → Ordnode α) (id (node ls ll lx lr).dual)
          (fun h => node (ls_1 + 1) (node rs rl rx rr).dual x nil)
          (fun rs_1 rl_1 rx_1 rr_1 h =>
            if rs_1 > delta * ls_1 then
              Ordnode.casesOn (motive := fun t => id rr_1 = t → Ordnode α) (id rr_1) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl_1 = t → Ordnode α) (id rl_1) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then
                        node (ls_1 + rs_1 + 1) (node (ls_1 + rls + 1) (node rs rl rx rr).dual x rl_1) rx_1 rr_1
                      else
                        node (ls_1 + rs_1 + 1) (node (ls_1 + rll.size + 1) (node rs rl rx rr).dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx_1 rr_1))
                    ⋯)
                ⋯
            else node (ls_1 + rs_1 + 1) (node rs rl rx rr).dual x (node ls ll lx lr).dual)
          ⋯)
      ⋯
state_after: case node.node
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
⊢ (if ls > delta * rs then
        rec (motive := fun t => ll = t → Ordnode α) (fun h => nil)
          (fun size l x_1 r l_ih r_ih h =>
            rec (motive := fun t => lr = t → Ordnode α) (fun h => nil)
              (fun size_1 l x_2 r l_ih r_ih h =>
                if size_1 < ratio * size then node (ls + rs + 1) ll lx (node (rs + size_1 + 1) lr x (node rs rl rx rr))
                else
                  node (ls + rs + 1) (node (size + l.size + 1) ll lx l) x_2
                    (node (r.size + rs + 1) r x (node rs rl rx rr)))
              lr ⋯)
          ll ⋯
      else node (ls + rs + 1) (node ls ll lx lr) x (node rs rl rx rr)).dual =
    if ls > delta * rs then
      rec (motive := fun t => ll.dual = t → Ordnode α) (fun h => nil)
        (fun size l x_1 r l_ih r_ih h =>
          rec (motive := fun t => lr.dual = t → Ordnode α) (fun h => nil)
            (fun size_1 l x_2 r l_ih r_ih h =>
              if size_1 < ratio * size then
                node (rs + ls + 1) (node (rs + size_1 + 1) (node rs rr.dual rx rl.dual) x lr.dual) lx ll.dual
              else
                node (rs + ls + 1) (node (rs + l.size + 1) (node rs rr.dual rx rl.dual) x l) x_2
                  (node (r.size + size + 1) r lx ll.dual))
            lr.dual ⋯)
        ll.dual ⋯
    else node (rs + ls + 1) (node rs rr.dual rx rl.dual) x (node ls lr.dual lx ll.dual)

tactic: split_ifs
state_before: case node.node
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
⊢ (if ls > delta * rs then
        rec (motive := fun t => ll = t → Ordnode α) (fun h => nil)
          (fun size l x_1 r l_ih r_ih h =>
            rec (motive := fun t => lr = t → Ordnode α) (fun h => nil)
              (fun size_1 l x_2 r l_ih r_ih h =>
                if size_1 < ratio * size then node (ls + rs + 1) ll lx (node (rs + size_1 + 1) lr x (node rs rl rx rr))
                else
                  node (ls + rs + 1) (node (size + l.size + 1) ll lx l) x_2
                    (node (r.size + rs + 1) r x (node rs rl rx rr)))
              lr ⋯)
          ll ⋯
      else node (ls + rs + 1) (node ls ll lx lr) x (node rs rl rx rr)).dual =
    if ls > delta * rs then
      rec (motive := fun t => ll.dual = t → Ordnode α) (fun h => nil)
        (fun size l x_1 r l_ih r_ih h =>
          rec (motive := fun t => lr.dual = t → Ordnode α) (fun h => nil)
            (fun size_1 l x_2 r l_ih r_ih h =>
              if size_1 < ratio * size then
                node (rs + ls + 1) (node (rs + size_1 + 1) (node rs rr.dual rx rl.dual) x lr.dual) lx ll.dual
              else
                node (rs + ls + 1) (node (rs + l.size + 1) (node rs rr.dual rx rl.dual) x l) x_2
                  (node (r.size + size + 1) r lx ll.dual))
            lr.dual ⋯)
        ll.dual ⋯
    else node (rs + ls + 1) (node rs rr.dual rx rl.dual) x (node ls lr.dual lx ll.dual)
state_after: case pos
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
h✝ : ls > delta * rs
⊢ (rec (motive := fun t => ll = t → Ordnode α) (fun h => nil)
        (fun size l x_1 r l_ih r_ih h =>
          rec (motive := fun t => lr = t → Ordnode α) (fun h => nil)
            (fun size_1 l x_2 r l_ih r_ih h =>
              if size_1 < ratio * size then node (ls + rs + 1) ll lx (node (rs + size_1 + 1) lr x (node rs rl rx rr))
              else
                node (ls + rs + 1) (node (size + l.size + 1) ll lx l) x_2
                  (node (r.size + rs + 1) r x (node rs rl rx rr)))
            lr ⋯)
        ll ⋯).dual =
    rec (motive := fun t => ll.dual = t → Ordnode α) (fun h => nil)
      (fun size l x_1 r l_ih r_ih h =>
        rec (motive := fun t => lr.dual = t → Ordnode α) (fun h => nil)
          (fun size_1 l x_2 r l_ih r_ih h =>
            if size_1 < ratio * size then
              node (rs + ls + 1) (node (rs + size_1 + 1) (node rs rr.dual rx rl.dual) x lr.dual) lx ll.dual
            else
              node (rs + ls + 1) (node (rs + l.size + 1) (node rs rr.dual rx rl.dual) x l) x_2
                (node (r.size + size + 1) r lx ll.dual))
          lr.dual ⋯)
      ll.dual ⋯

case neg
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
h✝ : ¬ls > delta * rs
⊢ (node (ls + rs + 1) (node ls ll lx lr) x (node rs rl rx rr)).dual =
    node (rs + ls + 1) (node rs rr.dual rx rl.dual) x (node ls lr.dual lx ll.dual)

tactic: swap
state_before: case pos
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
h✝ : ls > delta * rs
⊢ (rec (motive := fun t => ll = t → Ordnode α) (fun h => nil)
        (fun size l x_1 r l_ih r_ih h =>
          rec (motive := fun t => lr = t → Ordnode α) (fun h => nil)
            (fun size_1 l x_2 r l_ih r_ih h =>
              if size_1 < ratio * size then node (ls + rs + 1) ll lx (node (rs + size_1 + 1) lr x (node rs rl rx rr))
              else
                node (ls + rs + 1) (node (size + l.size + 1) ll lx l) x_2
                  (node (r.size + rs + 1) r x (node rs rl rx rr)))
            lr ⋯)
        ll ⋯).dual =
    rec (motive := fun t => ll.dual = t → Ordnode α) (fun h => nil)
      (fun size l x_1 r l_ih r_ih h =>
        rec (motive := fun t => lr.dual = t → Ordnode α) (fun h => nil)
          (fun size_1 l x_2 r l_ih r_ih h =>
            if size_1 < ratio * size then
              node (rs + ls + 1) (node (rs + size_1 + 1) (node rs rr.dual rx rl.dual) x lr.dual) lx ll.dual
            else
              node (rs + ls + 1) (node (rs + l.size + 1) (node rs rr.dual rx rl.dual) x l) x_2
                (node (r.size + size + 1) r lx ll.dual))
          lr.dual ⋯)
      ll.dual ⋯

case neg
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
h✝ : ¬ls > delta * rs
⊢ (node (ls + rs + 1) (node ls ll lx lr) x (node rs rl rx rr)).dual =
    node (rs + ls + 1) (node rs rr.dual rx rl.dual) x (node ls lr.dual lx ll.dual)
state_after: case neg
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
h✝ : ¬ls > delta * rs
⊢ (node (ls + rs + 1) (node ls ll lx lr) x (node rs rl rx rr)).dual =
    node (rs + ls + 1) (node rs rr.dual rx rl.dual) x (node ls lr.dual lx ll.dual)

case pos
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
h✝ : ls > delta * rs
⊢ (rec (motive := fun t => ll = t → Ordnode α) (fun h => nil)
        (fun size l x_1 r l_ih r_ih h =>
          rec (motive := fun t => lr = t → Ordnode α) (fun h => nil)
            (fun size_1 l x_2 r l_ih r_ih h =>
              if size_1 < ratio * size then node (ls + rs + 1) ll lx (node (rs + size_1 + 1) lr x (node rs rl rx rr))
              else
                node (ls + rs + 1) (node (size + l.size + 1) ll lx l) x_2
                  (node (r.size + rs + 1) r x (node rs rl rx rr)))
            lr ⋯)
        ll ⋯).dual =
    rec (motive := fun t => ll.dual = t → Ordnode α) (fun h => nil)
      (fun size l x_1 r l_ih r_ih h =>
        rec (motive := fun t => lr.dual = t → Ordnode α) (fun h => nil)
          (fun size_1 l x_2 r l_ih r_ih h =>
            if size_1 < ratio * size then
              node (rs + ls + 1) (node (rs + size_1 + 1) (node rs rr.dual rx rl.dual) x lr.dual) lx ll.dual
            else
              node (rs + ls + 1) (node (rs + l.size + 1) (node rs rr.dual rx rl.dual) x l) x_2
                (node (r.size + size + 1) r lx ll.dual))
          lr.dual ⋯)
      ll.dual ⋯

tactic: cases' ll with lls lll llx llr <;> cases' lr with lrs lrl lrx lrr <;> try rfl
state_before: case pos
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
h✝ : ls > delta * rs
⊢ (rec (motive := fun t => ll = t → Ordnode α) (fun h => nil)
        (fun size l x_1 r l_ih r_ih h =>
          rec (motive := fun t => lr = t → Ordnode α) (fun h => nil)
            (fun size_1 l x_2 r l_ih r_ih h =>
              if size_1 < ratio * size then node (ls + rs + 1) ll lx (node (rs + size_1 + 1) lr x (node rs rl rx rr))
              else
                node (ls + rs + 1) (node (size + l.size + 1) ll lx l) x_2
                  (node (r.size + rs + 1) r x (node rs rl rx rr)))
            lr ⋯)
        ll ⋯).dual =
    rec (motive := fun t => ll.dual = t → Ordnode α) (fun h => nil)
      (fun size l x_1 r l_ih r_ih h =>
        rec (motive := fun t => lr.dual = t → Ordnode α) (fun h => nil)
          (fun size_1 l x_2 r l_ih r_ih h =>
            if size_1 < ratio * size then
              node (rs + ls + 1) (node (rs + size_1 + 1) (node rs rr.dual rx rl.dual) x lr.dual) lx ll.dual
            else
              node (rs + ls + 1) (node (rs + l.size + 1) (node rs rr.dual rx rl.dual) x l) x_2
                (node (r.size + size + 1) r lx ll.dual))
          lr.dual ⋯)
      ll.dual ⋯
state_after: case pos.node.node
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
lx : α
h✝ : ls > delta * rs
lls : ℕ
lll : Ordnode α
llx : α
llr : Ordnode α
lrs : ℕ
lrl : Ordnode α
lrx : α
lrr : Ordnode α
⊢ (rec (motive := fun t => node lls lll llx llr = t → Ordnode α) (fun h => nil)
        (fun size l x_1 r l_ih r_ih h =>
          rec (motive := fun t => node lrs lrl lrx lrr = t → Ordnode α) (fun h => nil)
            (fun size_1 l x_2 r l_ih r_ih h =>
              if size_1 < ratio * size then
                node (ls + rs + 1) (node lls lll llx llr) lx
                  (node (rs + size_1 + 1) (node lrs lrl lrx lrr) x (node rs rl rx rr))
              else
                node (ls + rs + 1) (node (size + l.size + 1) (node lls lll llx llr) lx l) x_2
                  (node (r.size + rs + 1) r x (node rs rl rx rr)))
            (node lrs lrl lrx lrr) ⋯)
        (node lls lll llx llr) ⋯).dual =
    rec (motive := fun t => (node lls lll llx llr).dual = t → Ordnode α) (fun h => nil)
      (fun size l x_1 r l_ih r_ih h =>
        rec (motive := fun t => (node lrs lrl lrx lrr).dual = t → Ordnode α) (fun h => nil)
          (fun size_1 l x_2 r l_ih r_ih h =>
            if size_1 < ratio * size then
              node (rs + ls + 1) (node (rs + size_1 + 1) (node rs rr.dual rx rl.dual) x (node lrs lrl lrx lrr).dual) lx
                (node lls lll llx llr).dual
            else
              node (rs + ls + 1) (node (rs + l.size + 1) (node rs rr.dual rx rl.dual) x l) x_2
                (node (r.size + size + 1) r lx (node lls lll llx llr).dual))
          (node lrs lrl lrx lrr).dual ⋯)
      (node lls lll llx llr).dual ⋯

tactic: dsimp only [dual, id]
state_before: case pos.node.node
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
lx : α
h✝ : ls > delta * rs
lls : ℕ
lll : Ordnode α
llx : α
llr : Ordnode α
lrs : ℕ
lrl : Ordnode α
lrx : α
lrr : Ordnode α
⊢ (rec (motive := fun t => node lls lll llx llr = t → Ordnode α) (fun h => nil)
        (fun size l x_1 r l_ih r_ih h =>
          rec (motive := fun t => node lrs lrl lrx lrr = t → Ordnode α) (fun h => nil)
            (fun size_1 l x_2 r l_ih r_ih h =>
              if size_1 < ratio * size then
                node (ls + rs + 1) (node lls lll llx llr) lx
                  (node (rs + size_1 + 1) (node lrs lrl lrx lrr) x (node rs rl rx rr))
              else
                node (ls + rs + 1) (node (size + l.size + 1) (node lls lll llx llr) lx l) x_2
                  (node (r.size + rs + 1) r x (node rs rl rx rr)))
            (node lrs lrl lrx lrr) ⋯)
        (node lls lll llx llr) ⋯).dual =
    rec (motive := fun t => (node lls lll llx llr).dual = t → Ordnode α) (fun h => nil)
      (fun size l x_1 r l_ih r_ih h =>
        rec (motive := fun t => (node lrs lrl lrx lrr).dual = t → Ordnode α) (fun h => nil)
          (fun size_1 l x_2 r l_ih r_ih h =>
            if size_1 < ratio * size then
              node (rs + ls + 1) (node (rs + size_1 + 1) (node rs rr.dual rx rl.dual) x (node lrs lrl lrx lrr).dual) lx
                (node lls lll llx llr).dual
            else
              node (rs + ls + 1) (node (rs + l.size + 1) (node rs rr.dual rx rl.dual) x l) x_2
                (node (r.size + size + 1) r lx (node lls lll llx llr).dual))
          (node lrs lrl lrx lrr).dual ⋯)
      (node lls lll llx llr).dual ⋯
state_after: case pos.node.node
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
lx : α
h✝ : ls > delta * rs
lls : ℕ
lll : Ordnode α
llx : α
llr : Ordnode α
lrs : ℕ
lrl : Ordnode α
lrx : α
lrr : Ordnode α
⊢ (if lrs < ratio * lls then
        node (ls + rs + 1) (node lls lll llx llr) lx (node (rs + lrs + 1) (node lrs lrl lrx lrr) x (node rs rl rx rr))
      else
        node (ls + rs + 1) (node (lls + lrl.size + 1) (node lls lll llx llr) lx lrl) lrx
          (node (lrr.size + rs + 1) lrr x (node rs rl rx rr))).dual =
    if lrs < ratio * lls then
      node (rs + ls + 1) (node (rs + lrs + 1) (node rs rr.dual rx rl.dual) x (node lrs lrr.dual lrx lrl.dual)) lx
        (node lls llr.dual llx lll.dual)
    else
      node (rs + ls + 1) (node (rs + lrr.dual.size + 1) (node rs rr.dual rx rl.dual) x lrr.dual) lrx
        (node (lrl.dual.size + lls + 1) lrl.dual lx (node lls llr.dual llx lll.dual))

tactic: split_ifs with h <;> simp [h, add_comm]
state_before: case pos.node.node
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
lx : α
h✝ : ls > delta * rs
lls : ℕ
lll : Ordnode α
llx : α
llr : Ordnode α
lrs : ℕ
lrl : Ordnode α
lrx : α
lrr : Ordnode α
⊢ (if lrs < ratio * lls then
        node (ls + rs + 1) (node lls lll llx llr) lx (node (rs + lrs + 1) (node lrs lrl lrx lrr) x (node rs rl rx rr))
      else
        node (ls + rs + 1) (node (lls + lrl.size + 1) (node lls lll llx llr) lx lrl) lrx
          (node (lrr.size + rs + 1) lrr x (node rs rl rx rr))).dual =
    if lrs < ratio * lls then
      node (rs + ls + 1) (node (rs + lrs + 1) (node rs rr.dual rx rl.dual) x (node lrs lrr.dual lrx lrl.dual)) lx
        (node lls llr.dual llx lll.dual)
    else
      node (rs + ls + 1) (node (rs + lrr.dual.size + 1) (node rs rr.dual rx rl.dual) x lrr.dual) lrx
        (node (lrl.dual.size + lls + 1) lrl.dual lx (node lls llr.dual llx lll.dual))
state_after: no goals

tactic: rfl
state_before: case node.nil
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
⊢ (Ordnode.casesOn (motive := fun t => id (node rs rl rx rr) = t → Ordnode α) (id (node rs rl rx rr))
        (fun h =>
          Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil) (fun h => Ordnode.singleton x)
            (fun ls ll lx lr h =>
              Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll)
                (fun h =>
                  Ordnode.casesOn (motive := fun t => lr = t → Ordnode α) lr (fun h => node 2 nil x nil)
                    (fun size l lrx r h => node 3 (Ordnode.singleton lx) lrx (Ordnode.singleton x)) ⋯)
                (fun lls l x_1 r h =>
                  Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr)
                    (fun h => node 3 ll lx (Ordnode.singleton x))
                    (fun lrs lrl lrx lrr h =>
                      if lrs < ratio * lls then node (ls + 1) ll lx (node (lrs + 1) lr x nil)
                      else node (ls + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx (node (lrr.size + 1) lrr x nil))
                    ⋯)
                ⋯)
            ⋯)
        (fun rs_1 l x_1 r h =>
          Ordnode.casesOn (motive := fun t => id nil = t → Ordnode α) (id nil)
            (fun h => node (rs_1 + 1) nil x (node rs rl rx rr))
            (fun ls ll lx lr h =>
              if ls > delta * rs_1 then
                Ordnode.casesOn (motive := fun t => id ll = t → Ordnode α) (id ll) (fun h => nil)
                  (fun lls l x_2 r h =>
                    Ordnode.casesOn (motive := fun t => id lr = t → Ordnode α) (id lr) (fun h => nil)
                      (fun lrs lrl lrx lrr h =>
                        if lrs < ratio * lls then
                          node (ls + rs_1 + 1) ll lx (node (rs_1 + lrs + 1) lr x (node rs rl rx rr))
                        else
                          node (ls + rs_1 + 1) (node (lls + lrl.size + 1) ll lx lrl) lrx
                            (node (lrr.size + rs_1 + 1) lrr x (node rs rl rx rr)))
                      ⋯)
                  ⋯
              else node (ls + rs_1 + 1) nil x (node rs rl rx rr))
            ⋯)
        ⋯).dual =
    Ordnode.casesOn (motive := fun t => id (node rs rl rx rr).dual = t → Ordnode α) (id (node rs rl rx rr).dual)
      (fun h =>
        Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual) (fun h => Ordnode.singleton x)
          (fun rs rl rx rr h =>
            Ordnode.casesOn (motive := fun t => id rr = t → Ordnode α) (id rr)
              (fun h =>
                Ordnode.casesOn (motive := fun t => rl = t → Ordnode α) rl (fun h => node 2 nil x nil.dual)
                  (fun size l rlx r h => node 3 (Ordnode.singleton x) rlx (Ordnode.singleton rx)) ⋯)
              (fun rrs l x_1 r h =>
                Ordnode.casesOn (motive := fun t => id rl = t → Ordnode α) (id rl)
                  (fun h => node 3 (Ordnode.singleton x) rx rr)
                  (fun rls rll rlx rlr h =>
                    if rls < ratio * rrs then node (rs + 1) (node (rls + 1) nil x rl) rx rr
                    else node (rs + 1) (node (rll.size + 1) nil x rll) rlx (node (rlr.size + rrs + 1) rlr rx rr))
                  ⋯)
              ⋯)
          ⋯)
      (fun ls l x_1 r h =>
        Ordnode.casesOn (motive := fun t => id nil.dual = t → Ordnode α) (id nil.dual)
          (fun h => node (ls + 1) (node rs rl rx rr).dual x nil)
          (fun rs_1 rl_1 rx_1 rr_1 h =>
            if rs_1 > delta * ls then
              Ordnode.casesOn (motive := fun t => id rr_1 = t → Ordnode α) (id rr_1) (fun h => nil)
                (fun rrs l x_2 r h =>
                  Ordnode.casesOn (motive := fun t => id rl_1 = t → Ordnode α) (id rl_1) (fun h => nil)
                    (fun rls rll rlx rlr h =>
                      if rls < ratio * rrs then
                        node (ls + rs_1 + 1) (node (ls + rls + 1) (node rs rl rx rr).dual x rl_1) rx_1 rr_1
                      else
                        node (ls + rs_1 + 1) (node (ls + rll.size + 1) (node rs rl rx rr).dual x rll) rlx
                          (node (rlr.size + rrs + 1) rlr rx_1 rr_1))
                    ⋯)
                ⋯
            else node (ls + rs_1 + 1) (node rs rl rx rr).dual x nil.dual)
          ⋯)
      ⋯
state_after: no goals

tactic: simp [add_comm]
state_before: case neg
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
ll : Ordnode α
lx : α
lr : Ordnode α
h✝ : ¬ls > delta * rs
⊢ (node (ls + rs + 1) (node ls ll lx lr) x (node rs rl rx rr)).dual =
    node (rs + ls + 1) (node rs rr.dual rx rl.dual) x (node ls lr.dual lx ll.dual)
state_after: no goals

tactic: rfl
state_before: case pos.node.nil
α : Type u_1
x : α
rs : ℕ
rl : Ordnode α
rx : α
rr : Ordnode α
ls : ℕ
lx : α
h✝ : ls > delta * rs
lls : ℕ
lll : Ordnode α
llx : α
llr : Ordnode α
⊢ (rec (motive := fun t => node lls lll llx llr = t → Ordnode α) (fun h => nil)
        (fun size l x_1 r l_ih r_ih h =>
          rec (motive := fun t => nil = t → Ordnode α) (fun h => nil)
            (fun size_1 l x_2 r l_ih r_ih h =>
              if size_1 < ratio * size then
                node (ls + rs + 1) (node lls lll llx llr) lx (node (rs + size_1 + 1) nil x (node rs rl rx rr))
              else
                node (ls + rs + 1) (node (size + l.size + 1) (node lls lll llx llr) lx l) x_2
                  (node (r.size + rs + 1) r x (node rs rl rx rr)))
            nil ⋯)
        (node lls lll llx llr) ⋯).dual =
    rec (motive := fun t => (node lls lll llx llr).dual = t → Ordnode α) (fun h => nil)
      (fun size l x_1 r l_ih r_ih h =>
        rec (motive := fun t => nil.dual = t → Ordnode α) (fun h => nil)
          (fun size_1 l x_2 r l_ih r_ih h =>
            if size_1 < ratio * size then
              node (rs + ls + 1) (node (rs + size_1 + 1) (node rs rr.dual rx rl.dual) x nil.dual) lx
                (node lls lll llx llr).dual
            else
              node (rs + ls + 1) (node (rs + l.size + 1) (node rs rr.dual rx rl.dual) x l) x_2
                (node (r.size + size + 1) r lx (node lls lll llx llr).dual))
          nil.dual ⋯)
      (node lls lll llx llr).dual ⋯
state_after: no goals