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

Proof state:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.buckets.WF
c✝ : Bool
h₁ : ∀ (x : α × β), x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList → ¬(x.fst == k) = true
this : ?m.58846
⊢ (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.WF

Tactic:
split <;> [exact this; refine expand_WF this]

Result:
no goals

Proof state:
case refine_2
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h✝ : m.buckets.WF
c✝ : Bool
h₁ : ∃ x, x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList ∧ (x.fst == k) = true
x : α × β
hx₁ : x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
hx₂ : (x.fst == k) = true
H :
  AssocList.All
    (fun k_1 x => ((hash k_1).toUSize % USize.ofNat m.buckets.val.size).toNat = (mkIdx ⋯ (hash k).toUSize).val.toNat)
    m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val]
a : α × β
h : a ∈ (AssocList.replace k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]).toList
⊢ (fun k_1 x => ((hash k_1).toUSize % USize.ofNat m.buckets.val.size).toNat = (mkIdx ⋯ (hash k).toUSize).val.toNat)
    a.fst a.snd

Tactic:
simp only [AssocList.All, Array.ugetElem_eq_getElem, AssocList.toList_replace] at H h ⊢

Result:
case refine_2
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h✝ : m.buckets.WF
c✝ : Bool
h₁ : ∃ x, x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList ∧ (x.fst == k) = true
x : α × β
hx₁ : x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
hx₂ : (x.fst == k) = true
H :
  ∀ (a : α × β),
    a ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList →
      ((hash a.fst).toUSize % USize.ofNat m.buckets.val.size).toNat = (mkIdx ⋯ (hash k).toUSize).val.toNat
a : α × β
h :
  a ∈
    List.replaceF (fun x => bif x.fst == k then some (k, v) else none)
      m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
⊢ ((hash a.fst).toUSize % USize.ofNat m.buckets.val.size).toNat = (mkIdx ⋯ (hash k).toUSize).val.toNat

Proof state:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.buckets.WF
c✝ : Bool
h₁ : ∀ (x : α × β), x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList → ¬(x.fst == k) = true
⊢ { 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.WF

Tactic:
refine h.update (.cons ?_) (fun H a h => ?_)

Result:
case refine_1
α : Type u_1
β : Type u_2
inst✝³ : BEq α
inst✝² : Hashable α
m : Imp α β
k : α
v : β
h : m.buckets.WF
c✝ : Bool
h₁ : ∀ (x : α × β), x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList → ¬(x.fst == k) = true
inst✝¹ : PartialEquivBEq α
inst✝ : LawfulHashable α
⊢ ∀ (a' : α × β), a' ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val].toList → ¬((k, v).fst == a'.fst) = true

case refine_2
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h✝ : m.buckets.WF
c✝ : Bool
h₁ : ∀ (x : α × β), x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList → ¬(x.fst == k) = true
H :
  AssocList.All
    (fun k_1 x => ((hash k_1).toUSize % USize.ofNat m.buckets.val.size).toNat = (mkIdx ⋯ (hash k).toUSize).val.toNat)
    m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val]
a : α × β
h : a ∈ (AssocList.cons k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]).toList
⊢ (fun k_1 x => ((hash k_1).toUSize % USize.ofNat m.buckets.val.size).toNat = (mkIdx ⋯ (hash k).toUSize).val.toNat)
    a.fst a.snd

Proof state:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.buckets.WF
⊢ (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.WF

Tactic:
split

Result:
case h_1
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.buckets.WF
c✝ : Bool
heq✝ : AssocList.contains k m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat] = 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]) ⋯ }.buckets.WF

case h_2
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.buckets.WF
c✝ : Bool
heq✝ : AssocList.contains k 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.WF

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

Tactic:
simp only [AssocList.contains_eq, ne_eq, List.any_eq_true, not_exists, not_and] at h₁

Result:
α : Type u_1
β : Type u_2
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
v : β
h : m.buckets.WF
c✝ : Bool
h₁ : ∀ (x : α × β), x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList → ¬(x.fst == k) = true
⊢ (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.WF