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

Proof state:
α : Type u_1
β : Type u_2
f : α → β → β
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h : m.buckets.WF
⊢ (m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
      (AssocList.modify k f m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯).WF

Tactic:
refine h.update (fun H => ?_) (fun H a h => ?_) <;> simp at h ⊢

Result:
case refine_1
α : Type u_1
β : Type u_2
f : α → β → β
inst✝³ : BEq α
inst✝² : Hashable α
m : Imp α β
k : α
h : m.buckets.WF
inst✝¹ : PartialEquivBEq α
inst✝ : LawfulHashable α
H : List.Pairwise (fun a b => ¬(a.fst == b.fst) = true) m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val].toList
⊢ List.Pairwise (fun a b => ¬(a.fst == b.fst) = true)
    (List.replaceF (fun x => bif x.fst == k then some (k, f x.fst x.snd) else none)
      m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList)

case refine_2
α : Type u_1
β : Type u_2
f : α → β → β
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h✝ : m.buckets.WF
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 ∈
    List.replaceF (fun x => bif x.fst == k then some (k, f x.fst x.snd) else none)
      m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
⊢ (hash a.fst).toNat % 2 ^ System.Platform.numBits % (m.buckets.val.size % 2 ^ System.Platform.numBits) =
    (mkIdx ⋯ (hash k).toUSize).val.toNat

Proof state:
α : Type u_1
β : Type u_2
f : α → β → β
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
h✝¹ : m.buckets.WF
a : α × β
k : α
h : a ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
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
h✝ :
  a ∈
    List.replaceF (fun x => bif x.fst == k then some (k, f x.fst x.snd) else none)
      m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
⊢ (hash a.fst).toNat % 2 ^ System.Platform.numBits % (m.buckets.val.size % 2 ^ System.Platform.numBits) =
    (mkIdx ⋯ (hash k).toUSize).val.toNat

Tactic:
exact H _ h

Result:
no goals

Proof state:
case refine_2
α : Type u_1
β : Type u_2
f : α → β → β
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h✝ : m.buckets.WF
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 ∈
    List.replaceF (fun x => bif x.fst == k then some (k, f x.fst x.snd) else none)
      m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
⊢ (hash a.fst).toNat % 2 ^ System.Platform.numBits % (m.buckets.val.size % 2 ^ System.Platform.numBits) =
    (mkIdx ⋯ (hash k).toUSize).val.toNat

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

Result:
case refine_2
α : Type u_1
β : Type u_2
f : α → β → β
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h✝ : m.buckets.WF
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, f x.fst x.snd) else none)
      m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
⊢ (hash a.fst).toNat % 2 ^ System.Platform.numBits % (m.buckets.val.size % 2 ^ System.Platform.numBits) =
    (mkIdx ⋯ (hash k).toUSize).val.toNat

Proof state:
case refine_1
α : Type u_1
β : Type u_2
f : α → β → β
inst✝³ : BEq α
inst✝² : Hashable α
m : Imp α β
k : α
h : m.buckets.WF
inst✝¹ : PartialEquivBEq α
inst✝ : LawfulHashable α
H : List.Pairwise (fun a b => ¬(a.fst == b.fst) = true) m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val].toList
⊢ List.Pairwise (fun a b => ¬(a.fst == b.fst) = true)
    (List.replaceF (fun x => bif x.fst == k then some (k, f x.fst x.snd) else none)
      m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList)

Tactic:
exact pairwise_replaceF H

Result:
no goals

Proof state:
case refine_2
α : Type u_1
β : Type u_2
f : α → β → β
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h✝ : m.buckets.WF
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, f x.fst x.snd) else none)
      m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
⊢ (hash a.fst).toNat % 2 ^ System.Platform.numBits % (m.buckets.val.size % 2 ^ System.Platform.numBits) =
    (mkIdx ⋯ (hash k).toUSize).val.toNat

Tactic:
match mem_replaceF h with
| .inl rfl => rfl
| .inr h => exact H _ h

Result:
no goals

Proof state:
α : Type u_1
β : Type u_2
f : α → β → β
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h : m.buckets.WF
⊢ ((m.buckets.update (mkIdx ⋯ (hash k).toUSize).val AssocList.nil ⋯).update (mkIdx ⋯ (hash k).toUSize).val
      (AssocList.modify k f m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯).WF

Tactic:
rw [Buckets.update_update]

Result:
α : Type u_1
β : Type u_2
f : α → β → β
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h : m.buckets.WF
⊢ (m.buckets.update (mkIdx ⋯ (hash k).toUSize).val
      (AssocList.modify k f m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯).WF

Proof state:
α : Type u_1
β : Type u_2
f : α → β → β
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h : m.buckets.WF
⊢ (m.modify k f).buckets.WF

Tactic:
dsimp [modify, cond]

Result:
α : Type u_1
β : Type u_2
f : α → β → β
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
k : α
h : m.buckets.WF
⊢ ((m.buckets.update (mkIdx ⋯ (hash k).toUSize).val AssocList.nil ⋯).update (mkIdx ⋯ (hash k).toUSize).val
      (AssocList.modify k f m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]) ⋯).WF

Proof state:
α : Type u_1
β : Type u_2
f : α → β → β
inst✝¹ : BEq α
inst✝ : Hashable α
m : Imp α β
h✝ : m.buckets.WF
a : α × β
H :
  ∀ (a_1 : α × β),
    a_1 ∈ m.buckets.val[(mkIdx ⋯ (hash a.fst).toUSize).val.toNat].toList →
      ((hash a_1.fst).toUSize % USize.ofNat m.buckets.val.size).toNat = (mkIdx ⋯ (hash a.fst).toUSize).val.toNat
h :
  a ∈
    List.replaceF (fun x => bif x.fst == a.fst then some (a.fst, f x.fst x.snd) else none)
      m.buckets.val[(mkIdx ⋯ (hash a.fst).toUSize).val.toNat].toList
⊢ (hash a.fst).toNat % 2 ^ System.Platform.numBits % (m.buckets.val.size % 2 ^ System.Platform.numBits) =
    (mkIdx ⋯ (hash a.fst).toUSize).val.toNat

Tactic:
rfl

Result:
no goals