-- Mathlib/.lake/packages/batteries/Batteries/Data/HashMap/WF.lean
theorem insert_size [BEq α] [Hashable α] {m : Imp α β} {k v}
    (h : m.size = m.buckets.size) :
    (insert m k v).size = (insert m k v).buckets.size

Proof state:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.size = m.buckets.size
⊢ (m.insert k v).size = (m.insert k v).buckets.size

Tactic:
dsimp [insert, cond]

Result:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.size = m.buckets.size
⊢ (match AssocList.contains k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] with
      | true =>
        { size := m.size,
          buckets :=
            m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
              (AssocList.replace k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯ }
      | false =>
        if numBucketsForCapacity (m.size + 1) ≤ m.buckets.val.size then
          { size := m.size + 1,
            buckets :=
              m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
                (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯ }
        else
          expand (m.size + 1)
            (m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
              (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯)).size =
    (match AssocList.contains k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] with
        | true =>
          { size := m.size,
            buckets :=
              m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
                (AssocList.replace k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯ }
        | false =>
          if numBucketsForCapacity (m.size + 1) ≤ m.buckets.val.size then
            { size := m.size + 1,
              buckets :=
                m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
                  (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯ }
          else
            expand (m.size + 1)
              (m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
                (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯)).buckets.size

Proof state:
case h_2.isTrue
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.size = m.buckets.size
c✝ : Bool
heq✝ : AssocList.contains k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] = false
h✝ : numBucketsForCapacity (m.size + 1) ≤ m.buckets.val.size
⊢ { size := m.size + 1,
        buckets :=
          m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
            (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯ }.size =
    { size := m.size + 1,
          buckets :=
            m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
              (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯ }.buckets.size

Tactic:
unfold Buckets.size

Result:
case h_2.isTrue
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.size = m.buckets.size
c✝ : Bool
heq✝ : AssocList.contains k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] = false
h✝ : numBucketsForCapacity (m.size + 1) ≤ m.buckets.val.size
⊢ { size := m.size + 1,
        buckets :=
          m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
            (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯ }.size =
    (List.map (fun x => x.toList.length)
        { size := m.size + 1,
                buckets :=
                  m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
                    (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯ }.buckets.val.toList).sum

Proof state:
case h_2.isFalse
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.size = m.buckets.size
c✝ : Bool
heq✝ : AssocList.contains k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] = false
h✝ : ¬numBucketsForCapacity (m.size + 1) ≤ m.buckets.val.size
⊢ (List.map (fun x => x.toList.length) m.buckets.val.toList).sum + 1 =
    (List.map (fun x => x.toList.length)
        (m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
              (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯).val.toList).sum

Tactic:
refine have ⟨_, _, h₁, _, eq⟩ := Buckets.exists_of_update ..; eq ▸ ?_

Result:
case h_2.isFalse
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.size = m.buckets.size
c✝ : Bool
heq✝ : AssocList.contains k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] = false
h✝ : ¬numBucketsForCapacity (m.size + 1) ≤ m.buckets.val.size
w✝¹ w✝ : List (AssocList α β)
h₁ : m.buckets.val.toList = w✝¹ ++ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val] :: w✝
left✝ : w✝¹.length = (mkIdx ⋯ (hash k).toUSize).val.toNat
eq :
  (m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
          (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯).val.toList =
    w✝¹ ++ AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] :: w✝
⊢ (List.map (fun x => x.toList.length) m.buckets.val.toList).sum + 1 =
    (List.map (fun x => x.toList.length)
        (w✝¹ ++ AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] :: w✝)).sum

Proof state:
case h_1
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.size = m.buckets.size
c✝ : Bool
heq✝ : AssocList.contains k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] = true
w✝¹ w✝ : List (AssocList α β)
h₁ : m.buckets.val.toList = w✝¹ ++ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val] :: w✝
left✝ : w✝¹.length = (mkIdx ⋯ (hash k).toUSize).val.toNat
eq :
  (m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
          (AssocList.replace k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯).val.toList =
    w✝¹ ++ AssocList.replace k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] :: w✝
⊢ { size := m.size,
        buckets :=
          m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
            (AssocList.replace k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯ }.size =
    (List.map (fun x => x.toList.length)
        (w✝¹ ++ AssocList.replace k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] :: w✝)).sum

Tactic:
simp [h, h₁, Buckets.size_eq]

Result:
no goals

Proof state:
case h_2.isFalse
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.size = m.buckets.size
c✝ : Bool
heq✝ : AssocList.contains k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] = false
h✝ : ¬numBucketsForCapacity (m.size + 1) ≤ m.buckets.val.size
⊢ (expand (m.size + 1)
        (m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
          (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯)).size =
    (expand (m.size + 1)
          (m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
            (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯)).buckets.size

Tactic:
rw [expand_size]

Result:
case h_2.isFalse
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.size = m.buckets.size
c✝ : Bool
heq✝ : AssocList.contains k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] = false
h✝ : ¬numBucketsForCapacity (m.size + 1) ≤ m.buckets.val.size
⊢ (expand (m.size + 1)
        (m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
          (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯)).size =
    (m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
        (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯).size