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

Proof state:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h : m.size = m.buckets.size
c✝ : Bool
w✝¹ w✝ : List (AssocList α β)
left✝ : w✝¹.length = (mkIdx ⋯ (hash k).toUSize).val.toNat
H : ∃ x, x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList ∧ (x.fst == k) = true
a : α × β
h₁ : a ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
h₂ : (a.fst == k) = true
⊢ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList.length =
    (List.eraseP (fun x => x.fst == k) m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList).length + 1

Tactic:
refine have ⟨_, _, _, _, _, h, eq⟩ := List.exists_of_eraseP h₁ h₂; eq ▸ ?_

Result:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h✝ : m.size = m.buckets.size
c✝ : Bool
w✝⁴ w✝³ : List (AssocList α β)
left✝² : w✝⁴.length = (mkIdx ⋯ (hash k).toUSize).val.toNat
H : ∃ x, x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList ∧ (x.fst == k) = true
a : α × β
h₁ : a ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
h₂ : (a.fst == k) = true
w✝² : α × β
w✝¹ w✝ : List (α × β)
left✝¹ : ∀ (b : α × β), b ∈ w✝¹ → ¬(b.fst == k) = true
left✝ : (w✝².fst == k) = true
h : m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList = w✝¹ ++ w✝² :: w✝
eq : List.eraseP (fun x => x.fst == k) m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList = w✝¹ ++ w✝
⊢ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList.length = (w✝¹ ++ w✝).length + 1

Proof state:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h : m.size = m.buckets.size
c✝ : Bool
w✝¹ w✝ : List (AssocList α β)
left✝ : w✝¹.length = (mkIdx ⋯ (hash k).toUSize).val.toNat
H : ∃ x, x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList ∧ (x.fst == k) = true
⊢ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList.length =
    (List.eraseP (fun x => x.fst == k) m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList).length + 1

Tactic:
have ⟨a, h₁, h₂⟩ := H

Result:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h : m.size = m.buckets.size
c✝ : Bool
w✝¹ w✝ : List (AssocList α β)
left✝ : w✝¹.length = (mkIdx ⋯ (hash k).toUSize).val.toNat
H : ∃ x, x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList ∧ (x.fst == k) = true
a : α × β
h₁ : a ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
h₂ : (a.fst == k) = true
⊢ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList.length =
    (List.eraseP (fun x => x.fst == k) m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList).length + 1

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

Tactic:
exact h

Result:
no goals

Proof state:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h : m.size = m.buckets.size
c✝ : Bool
H : AssocList.contains k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] = true
⊢ (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.erase k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯).val.toList).sum

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

Result:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h : m.size = m.buckets.size
c✝ : Bool
H : 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.erase k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯).val.toList =
    w✝¹ ++ AssocList.erase k 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.erase k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] :: w✝)).sum

Proof state:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h : m.size = m.buckets.size
c✝ : Bool
H : 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.erase k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯).val.toList =
    w✝¹ ++ AssocList.erase k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] :: w✝
⊢ (List.map (fun x => x.toList.length) w✝¹).sum + (?n + (List.map (fun x => x.toList.length) w✝).sum + 1) - 1 =
    (List.map (fun x => x.toList.length) w✝¹).sum +
      ((List.eraseP (fun x => x.fst == k) m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList).length +
        (List.map (fun x => x.toList.length) w✝).sum)

Tactic:
rfl

Result:
no goals