@@ -995,6 +995,20 @@ theorem toListModel_insertListₘ [BEq α] [Hashable α] [EquivBEq α] [LawfulHa
995995 apply Perm.trans (ih (wfImp_insert h))
996996 apply List.insertList_perm_of_perm_first (toListModel_insert h) (wfImp_insert h).distinct
997997
998+ /-! # `insertListₘ` -/
999+
1000+ theorem toListModel_insertListIfNewₘ [BEq α] [Hashable α] [EquivBEq α] [LawfulHashable α]
1001+ {m : Raw₀ α β} {l : List ((a : α) × β a)} (h : Raw.WFImp m.1 ) :
1002+ Perm (toListModel (insertListIfNewₘ m l).1 .buckets)
1003+ (List.insertListIfNew (toListModel m.1 .buckets) l) := by
1004+ induction l generalizing m with
1005+ | nil =>
1006+ simp [insertListIfNewₘ, List.insertListIfNew]
1007+ | cons hd tl ih =>
1008+ simp only [insertListIfNewₘ, List.insertListIfNew]
1009+ apply Perm.trans (ih (wfImp_insertIfNew h))
1010+ apply List.insertListIfNew_perm_of_perm_first (toListModel_insertIfNew h) (wfImp_insertIfNew h).distinct
1011+
9981012/-! # `unionₘ` -/
9991013
10001014theorem insertMany_eq_insertListₘ_toListModel [BEq α] [Hashable α] (m m₂ : Raw₀ α β) :
@@ -1014,17 +1028,39 @@ theorem insertMany_eq_insertListₘ_toListModel [BEq α] [Hashable α] (m m₂ :
10141028 simp only [List.foldl_cons, insertListₘ]
10151029 apply ih
10161030
1031+
1032+ theorem insertManyIfNew_eq_insertListIfNewₘ_toListModel [BEq α] [Hashable α] (m m₂ : Raw₀ α β) :
1033+ insertManyIfNew m m₂.1 = insertListIfNewₘ m (toListModel m₂.1 .buckets) := by
1034+ simp only [insertManyIfNew, bind_pure_comp, map_pure, bind_pure]
1035+ simp only [ForIn.forIn]
1036+ simp only [Raw.forIn_eq_forIn_toListModel, forIn_pure_yield_eq_foldl, Id.run_pure]
1037+ generalize toListModel m₂.val.buckets = l
1038+ suffices ∀ (t : { m' // ∀ (P : Raw₀ α β → Prop ),
1039+ (∀ {m'' : Raw₀ α β} {a : α} {b : β a}, P m'' → P (m''.insertIfNew a b)) → P m → P m' }),
1040+ (List.foldl (fun m' p => ⟨m'.val.insertIfNew p.1 p.2 , fun P h₁ h₂ => h₁ (m'.2 _ h₁ h₂)⟩) t l).val =
1041+ t.val.insertListIfNewₘ l from this _
1042+ intro t
1043+ induction l generalizing m with
1044+ | nil => simp [insertListIfNewₘ]
1045+ | cons hd tl ih =>
1046+ simp only [List.foldl_cons, insertListₘ]
1047+ apply ih
1048+
10171049theorem union_eq_unionₘ [BEq α] [Hashable α] (m₁ m₂ : Raw₀ α β) :
10181050 union m₁ m₂ = unionₘ m₁ m₂ := by
10191051 rw [union, unionₘ]
1020- split <;> rw [insertMany_eq_insertListₘ_toListModel]
1052+ split
1053+ · rw [insertManyIfNew_eq_insertListIfNewₘ_toListModel]
1054+ · rw [insertMany_eq_insertListₘ_toListModel]
10211055
10221056theorem toListModel_unionₘ [BEq α] [Hashable α] [EquivBEq α] [LawfulHashable α]
10231057 {m₁ m₂ : Raw₀ α β} (h₁ : Raw.WFImp m₁.1 ) (h₂ : Raw.WFImp m₂.1 ) :
10241058 Perm (toListModel (unionₘ m₁ m₂).1 .buckets)
10251059 (List.insertSmallerList (toListModel m₁.1 .buckets) (toListModel m₂.1 .buckets)) := by
10261060 rw [unionₘ, insertSmallerList, h₁.size_eq, h₂.size_eq]
1027- split <;> exact toListModel_insertListₘ ‹_›
1061+ split
1062+ · exact toListModel_insertListIfNewₘ ‹_›
1063+ · exact toListModel_insertListₘ ‹_›
10281064
10291065end Raw₀
10301066
@@ -1165,14 +1201,36 @@ theorem toListModel_insertMany_list [BEq α] [Hashable α] [EquivBEq α] [Lawful
11651201 apply toListModel_insertListₘ
11661202 exact h
11671203
1204+ /-! # `insertManyIfNew` -/
1205+
1206+ theorem wfImp_insertManyIfNew [BEq α] [Hashable α] [EquivBEq α] [LawfulHashable α] {ρ : Type w}
1207+ [ForIn Id ρ ((a : α) × β a)] {m : Raw₀ α β} {l : ρ} (h : Raw.WFImp m.1 ) :
1208+ Raw.WFImp (m.insertManyIfNew l).1 .1 :=
1209+ Raw.WF.out ((m.insertManyIfNew l).2 _ Raw.WF.insertIfNew₀ (.wf m.2 h))
1210+
1211+ theorem wf_insertManyIfNew₀ [BEq α] [Hashable α] [EquivBEq α] [LawfulHashable α] {ρ : Type w}
1212+ [ForIn Id ρ ((a : α) × β a)] {m : Raw α β} {h : 0 < m.buckets.size} {l : ρ} (h' : m.WF) :
1213+ (Raw₀.insertManyIfNew ⟨m, h⟩ l).1 .1 .WF :=
1214+ (Raw₀.insertManyIfNew ⟨m, h⟩ l).2 _ Raw.WF.insertIfNew₀ h'
1215+
1216+ theorem toListModel_insertManyIfNew_list [BEq α] [Hashable α] [EquivBEq α] [LawfulHashable α]
1217+ {m : Raw₀ α β} {l : List ((a : α) × (β a))} (h : Raw.WFImp m.1 ) :
1218+ Perm (toListModel (insertManyIfNew m l).1 .1 .buckets)
1219+ (List.insertListIfNew (toListModel m.1 .buckets) l) := by
1220+ rw [insertManyIfNew_eq_insertListIfNewₘ]
1221+ apply toListModel_insertListIfNewₘ
1222+ exact h
1223+
11681224/-! # `union` -/
11691225
11701226theorem wf_union₀ [BEq α] [Hashable α] [EquivBEq α] [LawfulHashable α]
11711227 {m₁ m₂ : Raw α β} {h₁ : 0 < m₁.buckets.size} {h₂ : 0 < m₂.buckets.size} (h'₁ : m₁.WF)
11721228 (h'₂ : m₂.WF) :
11731229 (Raw₀.union ⟨m₁, h₁⟩ ⟨m₂, h₂⟩).1 .WF := by
11741230 rw [union]
1175- split <;> exact wf_insertMany₀ ‹_›
1231+ split
1232+ · exact wf_insertManyIfNew₀ ‹_›
1233+ · exact wf_insertMany₀ ‹_›
11761234
11771235theorem toListModel_union [BEq α] [Hashable α] [EquivBEq α] [LawfulHashable α] {m₁ m₂ : Raw₀ α β}
11781236 (h₁ : Raw.WFImp m₁.1 ) (h₂ : Raw.WFImp m₂.1 ) :
0 commit comments