-- Mathlib/.lake/packages/lean4/src/lean/Std/Data/DHashMap/Internal/WF.lean
theorem wfImp_alterₘ [BEq α] [EquivBEq α] [Hashable α] [LawfulHashable α] {m : Raw₀ α (fun _ => β)}
    (h : Raw.WFImp m.1) {a : α} {f : Option β → Option β} : Raw.WFImp (Const.alterₘ m a f).1 where
  buckets_hash_self := isHashSelf_alterₘ m h a f
  distinct := DistinctKeys.perm (toListModel_alterₘ h) h.distinct.constAlterKey
  size_eq

Proof state:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : containsKey a (toListModel m.val.buckets) = true
⊢ (if
        containsKey a
            (toListModel
              ⟨withComputedSize (updateBucket m.val.buckets ⋯ a fun l => AssocList.Const.alter a f l), ⋯⟩.val.buckets) =
          true then
      (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1) =
    if (f (some (getValue a (toListModel m.val.buckets) h₁))).isSome = true then (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1

Tactic:
simp only [buckets_withComputedSize]

Result:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : containsKey a (toListModel m.val.buckets) = true
⊢ (if containsKey a (toListModel (updateBucket m.val.buckets ⋯ a fun l => AssocList.Const.alter a f l)) = true then
      (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1) =
    if (f (some (getValue a (toListModel m.val.buckets) h₁))).isSome = true then (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1

Proof state:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : containsKey a (toListModel m.val.buckets) = true
⊢ Raw.WFImp ⟨withComputedSize (updateBucket m.val.buckets ⋯ a fun l => AssocList.Const.alter a f l), ⋯⟩.val

Tactic:
apply wfImp_updateBucket_alter h

Result:
no goals

Proof state:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : ¬containsKey a (toListModel m.val.buckets) = true
x✝ : Option β
heq : f none = none
⊢ m.val.size =
    (match f none with
      | none => eraseKey a (toListModel m.val.buckets)
      | some v => insertEntry a v (toListModel m.val.buckets)).length

Tactic:
rw [heq, h.size_eq, length_eraseKey, if_neg h₁]

Result:
no goals

Proof state:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : containsKey a (toListModel m.val.buckets) = true
⊢ (if containsKey a (toListModel (updateBucket m.val.buckets ⋯ a fun l => AssocList.Const.alter a f l)) = true then
      (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1) =
    if (f (some (getValue a (toListModel m.val.buckets) h₁))).isSome = true then (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1

Tactic:
simp only [containsKey_of_perm <| toListModel_updateBucket_alter h]

Result:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : containsKey a (toListModel m.val.buckets) = true
⊢ (if containsKey a (Const.alterKey a f (toListModel m.val.buckets)) = true then (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1) =
    if (f (some (getValue a (toListModel m.val.buckets) h₁))).isSome = true then (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1

Proof state:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : ¬containsKey a (toListModel m.val.buckets) = true
⊢ (match f none with
        | none => m
        | some b => (m.consₘ a b).expandIfNecessary).val.size =
    (match f (getValue? a (toListModel m.val.buckets)) with
      | none => eraseKey a (toListModel m.val.buckets)
      | some v => insertEntry a v (toListModel m.val.buckets)).length

Tactic:
rw [getValue?_eq_none.mpr <| Bool.not_eq_true _ ▸ h₁]

Result:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : ¬containsKey a (toListModel m.val.buckets) = true
⊢ (match f none with
        | none => m
        | some b => (m.consₘ a b).expandIfNecessary).val.size =
    (match f none with
      | none => eraseKey a (toListModel m.val.buckets)
      | some v => insertEntry a v (toListModel m.val.buckets)).length

Proof state:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : containsKey a (toListModel m.val.buckets) = true
⊢ (if
        containsₘ ⟨withComputedSize (updateBucket m.val.buckets ⋯ a fun l => AssocList.Const.alter a f l), ⋯⟩ a =
          true then
      (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1) =
    if (f (some (getValue a (toListModel m.val.buckets) h₁))).isSome = true then (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1

Tactic:
rw [containsₘ_eq_containsKey (by apply wfImp_updateBucket_alter h)]

Result:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : containsKey a (toListModel m.val.buckets) = true
⊢ (if
        containsKey a
            (toListModel
              ⟨withComputedSize (updateBucket m.val.buckets ⋯ a fun l => AssocList.Const.alter a f l), ⋯⟩.val.buckets) =
          true then
      (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1) =
    if (f (some (getValue a (toListModel m.val.buckets) h₁))).isSome = true then (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1

Proof state:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : containsKey a (toListModel m.val.buckets) = true
⊢ (if containsKey a (Const.alterKey a f (toListModel m.val.buckets)) = true then (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1) =
    if (f (some (getValue a (toListModel m.val.buckets) h₁))).isSome = true then (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1

Tactic:
rw [← getValue?_eq_some_getValue h₁]

Result:
α : Type u
β : Type v
inst✝³ : BEq α
inst✝² : EquivBEq α
inst✝¹ : Hashable α
inst✝ : LawfulHashable α
m : Raw₀ α fun x => β
h : Raw.WFImp m.val
a : α
f : Option β → Option β
h₁ : containsKey a (toListModel m.val.buckets) = true
⊢ (if containsKey a (Const.alterKey a f (toListModel m.val.buckets)) = true then (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1) =
    if (f (getValue? a (toListModel m.val.buckets))).isSome = true then (toListModel m.val.buckets).length
    else (toListModel m.val.buckets).length - 1