-- 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
⊢ toListModel
      (match f none with
          | none => m
          | some b => (m.consₘ a b).expandIfNecessary).val.buckets ~
    Const.alterKey a f (toListModel m.val.buckets)

Tactic:
rw [Const.alterKey, getValue?_eq_none.mpr hc]

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

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

Tactic:
split

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

case isFalse
α : 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
      (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:
α : 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 β
hn : f none = none
⊢ toListModel m.val.buckets ~ eraseKey a (toListModel m.val.buckets)

Tactic:
rw [eraseKey_of_containsKey_eq_false]

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 β
hn : f none = none
⊢ containsKey a (toListModel m.val.buckets) = false

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 : ¬m.containsₘ a = true
⊢ toListModel
      (match f none with
          | none => m
          | some b => (m.consₘ a b).expandIfNecessary).val.buckets ~
    Const.alterKey a f (toListModel m.val.buckets)

Tactic:
rw [Bool.not_eq_true, containsₘ_eq_containsKey h] at hc

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
⊢ toListModel
      (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:
α : 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)

Tactic:
rw [insertEntry_of_containsKey_eq_false]

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✝
⊢ containsKey a (toListModel m.val.buckets) = false

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✝
⊢ containsKey a (toListModel m.val.buckets) = false

Tactic:
exact hc

Result:
no goals

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

Tactic:
next hn =>
simp only [hn]
rw [eraseKey_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 β
hc : containsKey a (toListModel m.val.buckets) = false
x✝ : Option β
hn : f none = none
⊢ containsKey a (toListModel m.val.buckets) = false

Tactic:
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 β
hc : containsKey a (toListModel m.val.buckets) = false
x✝ : Option β
hn : 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)

Tactic:
simp only [hn]

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 β
hn : f none = none
⊢ toListModel m.val.buckets ~ eraseKey a (toListModel m.val.buckets)