-- Mathlib/.lake/packages/lean4/src/lean/Std/Data/DHashMap/Internal/WF.lean
theorem toListModel_alterₘ [BEq α] [EquivBEq α] [Hashable α] [LawfulHashable α]
    {m : Raw₀ α (fun _ => β)} (h : Raw.WFImp m.1) {a : α} {f : Option β → Option β} :
    Perm (toListModel (Const.alterₘ m a f).1.2) (Const.alterKey a f (toListModel m.1.2))

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 β
hc : containsKey a (toListModel m.val.buckets) = false
x✝ : Option β
b✝ : β
hs : f none = some b✝
⊢ toListModel (m.consₘ a b✝).expandIfNecessary.val.buckets ~
    match f none with
    | none => eraseKey a (toListModel m.val.buckets)
    | some v => insertEntry a v (toListModel m.val.buckets)

Tactic:
simp only [hs]

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 β
hc : containsKey a (toListModel m.val.buckets) = false
x✝ : Option β
b✝ : β
hs : f none = some b✝
⊢ toListModel (m.consₘ a b✝).expandIfNecessary.val.buckets ~ insertEntry a b✝ (toListModel m.val.buckets)

Proof state:
case h_2
α : 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 β
hc : containsKey a (toListModel m.val.buckets) = false
x✝ : Option β
b✝ : β
heq✝ : f none = some b✝
⊢ toListModel (m.consₘ a b✝).expandIfNecessary.val.buckets ~
    match f none with
    | none => eraseKey a (toListModel m.val.buckets)
    | some v => insertEntry a v (toListModel m.val.buckets)

Tactic:
next hs =>
simp only [hs]
refine Perm.trans (toListModel_expandIfNecessary _) ?_
refine Perm.trans (toListModel_consₘ m h _ _) ?_
rw [insertEntry_of_containsKey_eq_false]
exact hc

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 β
⊢ toListModel (alterₘ m a f).val.buckets ~ Const.alterKey a f (toListModel m.val.buckets)

Tactic:
rw [Const.alterₘ]

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 β
⊢ toListModel
      (if h : m.containsₘ a = true then
            let buckets' := updateBucket m.val.buckets ⋯ a fun l => AssocList.Const.alter a f l;
            let size' := if containsₘ ⟨withComputedSize buckets', ⋯⟩ a = true then m.val.size else m.val.size - 1;
            ⟨{ size := size', buckets := buckets' }, ⋯⟩
          else
            match f none with
            | none => m
            | some b => (m.consₘ a b).expandIfNecessary).val.buckets ~
    Const.alterKey a f (toListModel m.val.buckets)

Proof state:
case isTrue
α : 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✝ : m.containsₘ a = true
⊢ toListModel
      (let buckets' := updateBucket m.val.buckets ⋯ a fun l => AssocList.Const.alter a f l;
          let size' := if containsₘ ⟨withComputedSize buckets', ⋯⟩ a = true then m.val.size else m.val.size - 1;
          ⟨{ size := size', buckets := buckets' }, ⋯⟩).val.buckets ~
    Const.alterKey a f (toListModel m.val.buckets)

Tactic:
exact toListModel_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 β
hc : containsKey a (toListModel m.val.buckets) = false
x✝ : Option β
b✝ : β
hs : f none = some b✝
⊢ toListModel (m.consₘ a b✝).expandIfNecessary.val.buckets ~ insertEntry a b✝ (toListModel m.val.buckets)

Tactic:
refine Perm.trans (toListModel_expandIfNecessary _) ?_

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 β
hc : containsKey a (toListModel m.val.buckets) = false
x✝ : Option β
b✝ : β
hs : f none = some b✝
⊢ toListModel (m.consₘ a b✝).val.buckets ~ insertEntry a b✝ (toListModel m.val.buckets)

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 β
hc : containsKey a (toListModel m.val.buckets) = false
⊢ toListModel
      (match f none with
          | none => m
          | some b => (m.consₘ a b).expandIfNecessary).val.buckets ~
    match f none with
    | none => eraseKey a (toListModel m.val.buckets)
    | some v => insertEntry a v (toListModel m.val.buckets)

Tactic:
split

Result:
case h_1
α : 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 β
hc : containsKey a (toListModel m.val.buckets) = false
x✝ : Option β
heq✝ : f none = none
⊢ toListModel m.val.buckets ~
    match f none with
    | none => eraseKey a (toListModel m.val.buckets)
    | some v => insertEntry a v (toListModel m.val.buckets)

case h_2
α : 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 β
hc : containsKey a (toListModel m.val.buckets) = false
x✝ : Option β
b✝ : β
heq✝ : f none = some b✝
⊢ toListModel (m.consₘ a b✝).expandIfNecessary.val.buckets ~
    match f none with
    | none => eraseKey a (toListModel m.val.buckets)
    | some v => insertEntry a v (toListModel m.val.buckets)

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 β
hc : containsKey a (toListModel m.val.buckets) = false
x✝ : Option β
b✝ : β
hs : f none = some b✝
⊢ toListModel (m.consₘ a b✝).val.buckets ~ insertEntry a b✝ (toListModel m.val.buckets)

Tactic:
refine Perm.trans (toListModel_consₘ m 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 β
hc : containsKey a (toListModel m.val.buckets) = false
x✝ : Option β
b✝ : β
hs : f none = some b✝
⊢ ⟨a, b✝⟩ :: toListModel m.val.buckets ~ insertEntry a b✝ (toListModel m.val.buckets)