-- 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:
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

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

Result:
no goals

Proof state:
case refine_2.tail
α : 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 : List.Mem 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

Tactic:
exact H _ h

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
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

Tactic:
cases h with
| head => rfl
| tail _ h => exact H _ h

Result:
no goals

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

Tactic:
dsimp [insert, cond]

Result:
α : 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

Proof state:
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
x : α × β
hx₁ : x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
hx₂ : (x.fst == k) = true
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)
    (AssocList.replace k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]).toList

Tactic:
simp only [AssocList.toList_replace]

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
x : α × β
hx₁ : x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
hx₂ : (x.fst == k) = true
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, v) else none)
      m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList)

Proof state:
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

Tactic:
exact fun a h h' => h₁ a h (PartialEquivBEq.symm h')

Result:
no goals

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
x : α × β
hx₁ : x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
hx₂ : (x.fst == k) = 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

Tactic:
refine h.update (fun H => ?_) (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
x : α × β
hx₁ : x ∈ m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat].toList
hx₂ : (x.fst == k) = true
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)
    (AssocList.replace k v m.buckets.val[(mkIdx ⋯ (hash k).toUSize).val.toNat]).toList

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