Compare commits

...

11 Commits

Author SHA1 Message Date
Kim Morrison
7c5687ebe5 . 2024-12-05 13:45:10 +11:00
Kim Morrison
ce8c71a253 suggestion from review 2024-12-05 13:38:56 +11:00
Kim Morrison
e1e720f8c2 cleanup 2024-12-04 14:30:51 +11:00
Kim Morrison
66f41a2d71 still can't be right 2024-12-04 14:26:15 +11:00
Kim Morrison
9153fdb534 merge master 2024-12-04 13:25:01 +11:00
Kim Morrison
53f2d787ce . 2024-11-28 20:54:17 +11:00
Kim Morrison
b234016b88 Merge remote-tracking branch 'origin/master' into toList_map_fst 2024-11-27 14:33:35 +11:00
Kim Morrison
53d95cd263 cleanup 2024-11-27 13:19:49 +11:00
Kim Morrison
a8bb5bdf3a cleanup 2024-11-27 13:10:36 +11:00
Kim Morrison
dcebe8f236 feat: DHashMap.toList_map_fst 2024-11-27 12:45:20 +11:00
Kim Morrison
27f3fd1317 feat: Array fold lemmas 2024-11-27 12:43:51 +11:00
9 changed files with 85 additions and 11 deletions

View File

@@ -37,6 +37,17 @@ theorem foldl_eq {f : δ → (a : α) → β a → δ} {init : δ} {l : AssocLis
l.foldl f init = l.toList.foldl (fun d p => f d p.1 p.2) init := by
induction l generalizing init <;> simp_all [foldl, Id.run, foldlM]
theorem foldlM_id {f : δ (a : α) β a δ} {init : δ} {l : AssocList α β} :
l.foldlM (m := Id) f init = l.foldl f init := rfl
@[simp]
theorem foldr_eq {f : (a : α) β a δ δ} {init : δ} {l : AssocList α β} :
l.foldr f init = l.toList.foldr (fun p d => f p.1 p.2 d) init := by
induction l generalizing init <;> simp_all [foldr, Id.run, foldrM]
theorem foldrM_id {f : (a : α) β a δ δ} {init : δ} {l : AssocList α β} :
l.foldrM (m := Id) f init = l.foldr f init := rfl
@[simp]
theorem length_eq {l : AssocList α β} : l.length = l.toList.length := by
rw [length, foldl_eq]

View File

@@ -158,6 +158,10 @@ namespace DHashMap.Internal
def toListModel (buckets : Array (AssocList α β)) : List ((a : α) × β a) :=
buckets.toList.flatMap AssocList.toList
/-- Internal implementation detail of the hash map -/
def Const.toListModel {β} (buckets : Array (AssocList α (fun _ => β))) : List (α × β) :=
(Internal.toListModel buckets).map fun a, b => (a, b)
/-- Internal implementation detail of the hash map -/
@[inline] def computeSize (buckets : Array (AssocList α β)) : Nat :=
buckets.foldl (fun d b => d + b.length) 0

View File

@@ -39,4 +39,7 @@ def values {β : Type v} : List ((_ : α) × β) → List β
| [] => []
| _, v :: l => v :: values l
theorem map_fst_eq_keys {l : List ((a : α) × β a)} : l.map Sigma.fst = keys l := by
induction l <;> simp_all [keys]
end Std.DHashMap.Internal.List

View File

@@ -85,6 +85,7 @@ private def queryNames : Array Name :=
``Const.get_eq_getValue, ``get!_eq_getValueCast!, ``getD_eq_getValueCastD,
``Const.get!_eq_getValue!, ``Const.getD_eq_getValueD, ``getKey?_eq_getKey?,
``getKey_eq_getKey, ``getKeyD_eq_getKeyD, ``getKey!_eq_getKey!,
``Raw.keys_eq_keys_toListModel, ``Raw.toList_eq_toListModel,
``Raw.length_keys_eq_length_keys, ``Raw.isEmpty_keys_eq_isEmpty_keys,
``Raw.contains_keys_eq_contains_keys, ``Raw.mem_keys_iff_contains_keys,
``Raw.pairwise_keys_iff_pairwise_keys]
@@ -821,7 +822,7 @@ theorem length_keys [EquivBEq α] [LawfulHashable α] (h : m.1.WF) :
@[simp]
theorem isEmpty_keys [EquivBEq α] [LawfulHashable α] (h: m.1.WF):
m.1.keys.isEmpty = m.1.isEmpty:= by
m.1.keys.isEmpty = m.1.isEmpty := by
simp_to_model using List.isEmpty_keys_eq_isEmpty
@[simp]
@@ -832,13 +833,18 @@ theorem contains_keys [EquivBEq α] [LawfulHashable α] (h : m.1.WF) {k : α} :
@[simp]
theorem mem_keys [LawfulBEq α] [LawfulHashable α] (h : m.1.WF) {k : α} :
k m.1.keys m.contains k := by
simp_to_model
rw [List.containsKey_eq_keys_contains]
simp_to_model
rw [List.containsKey_eq_keys_contains, List.elem_iff]
theorem distinct_keys [EquivBEq α] [LawfulHashable α] (h : m.1.WF) :
m.1.keys.Pairwise (fun a b => (a == b) = false) := by
simp_to_model using (Raw.WF.out h).distinct.distinct
@[simp]
theorem toList_map_fst {α β} (m : Raw₀ α β) :
m.1.toList.map Sigma.fst = m.1.keys := by
simp_to_model using List.map_fst_eq_keys
end Raw₀
end Std.DHashMap.Internal

View File

@@ -107,12 +107,19 @@ theorem foldRev_cons_key {l : Raw α β} {acc : List α} :
l.foldRev (fun acc k _ => k :: acc) acc = List.keys (toListModel l.buckets) ++ acc := by
rw [foldRev_cons_apply, keys_eq_map]
theorem toList_perm_toListModel {m : Raw α β} : Perm m.toList (toListModel m.buckets) := by
theorem toList_eq_toListModel {m : Raw α β} : m.toList = toListModel m.buckets := by
simp [Raw.toList, foldRev_cons]
theorem keys_eq_keys_toListModel {m : Raw α β} :
m.keys = List.keys (toListModel m.buckets) := by
simp [Raw.keys, foldRev_cons_key]
theorem toList_perm_toListModel {m : Raw α β} : Perm m.toList (toListModel m.buckets) :=
Perm.of_eq toList_eq_toListModel
theorem keys_perm_keys_toListModel {m : Raw α β} :
Perm m.keys (List.keys (toListModel m.buckets)) := by
simp [Raw.keys, foldRev_cons_key, keys_eq_map]
Perm m.keys (List.keys (toListModel m.buckets)) :=
Perm.of_eq keys_eq_keys_toListModel
theorem length_keys_eq_length_keys {m : Raw α β} :
m.keys.length = (List.keys (toListModel m.buckets)).length :=
@@ -135,6 +142,15 @@ theorem pairwise_keys_iff_pairwise_keys [BEq α] [PartialEquivBEq α] {m : Raw
(List.keys (toListModel m.buckets)).Pairwise (fun a b => (a == b) = false) :=
keys_perm_keys_toListModel.pairwise_iff BEq.symm_false
theorem Const.toList_eq_toListModel {β} {m : Raw α (fun _ => β)} :
Raw.Const.toList m = Const.toListModel m.buckets := by
simp only [Raw.Const.toList, Const.toListModel, Raw.toList_eq_toListModel, Raw.toList,
Raw.foldRev, Raw.foldRevM, Array.id_run_foldrM, AssocList.foldrM_id]
rw [ Array.foldr_hom (List.map _) _ (g₂ := fun l acc => foldr (fun p d => (p.fst, p.snd) :: d) acc l.toList)]
· simp
· intro l acc
induction l <;> simp_all
end Raw
namespace Raw₀

View File

@@ -960,12 +960,17 @@ theorem contains_keys [EquivBEq α] [LawfulHashable α] {k : α} :
@[simp]
theorem mem_keys [LawfulBEq α] [LawfulHashable α] {k : α} :
k m.keys k m := by
k m.keys k m := by
rw [mem_iff_contains]
exact Raw₀.mem_keys m.1, _ m.2
theorem distinct_keys [EquivBEq α] [LawfulHashable α] :
m.keys.Pairwise (fun a b => (a == b) = false) :=
m.keys.Pairwise (fun a b => (a == b) = false) :=
Raw₀.distinct_keys m.1, m.2.size_buckets_pos m.2
@[simp]
theorem toList_map_fst :
m.toList.map Sigma.fst = m.keys :=
Raw₀.toList_map_fst m.1, m.2.size_buckets_pos
end Std.DHashMap

View File

@@ -1039,7 +1039,7 @@ theorem contains_keys [EquivBEq α] [LawfulHashable α] (h : m.WF) {k : α} :
@[simp]
theorem mem_keys [LawfulBEq α] [LawfulHashable α] (h : m.WF) {k : α} :
k m.keys k m := by
k m.keys k m := by
rw [mem_iff_contains]
simp_to_raw using Raw₀.mem_keys m, _ h
@@ -1047,6 +1047,11 @@ theorem distinct_keys [EquivBEq α] [LawfulHashable α] (h : m.WF) :
m.keys.Pairwise (fun a b => (a == b) = false) := by
simp_to_raw using Raw₀.distinct_keys m, h.size_buckets_pos h
@[simp]
theorem toList_map_fst (h : m.WF) :
m.toList.map Sigma.fst = m.keys := by
simp_to_raw using Raw₀.toList_map_fst m, h.size_buckets_pos
end Raw
end Std.DHashMap

View File

@@ -701,6 +701,18 @@ theorem distinct_keys [EquivBEq α] [LawfulHashable α] :
m.keys.Pairwise (fun a b => (a == b) = false) :=
DHashMap.distinct_keys
@[simp]
theorem toList_inner :
m.inner.toList = m.toList.map fun k, v => k, v := by
simp [DHashMap.toList, toList, DHashMap.Const.toList,
DHashMap.Internal.Raw.toList_eq_toListModel, DHashMap.Internal.Raw.Const.toList_eq_toListModel,
DHashMap.Internal.Const.toListModel, Function.comp_def]
@[simp]
theorem toList_map_fst :
m.toList.map Prod.fst = m.keys := by
simpa using DHashMap.toList_map_fst (m := m.1)
end
end Std.HashMap

View File

@@ -699,13 +699,25 @@ theorem contains_keys [EquivBEq α] [LawfulHashable α] (h : m.WF) {k : α} :
@[simp]
theorem mem_keys [LawfulBEq α] [LawfulHashable α] (h : m.WF) {k : α} :
k m.keys k m :=
k m.keys k m :=
DHashMap.Raw.mem_keys h.out
theorem distinct_keys [EquivBEq α] [LawfulHashable α] (h : m.WF) :
m.keys.Pairwise (fun a b => (a == b) = false) :=
m.keys.Pairwise (fun a b => (a == b) = false) :=
DHashMap.Raw.distinct_keys h.out
@[simp]
theorem toList_inner {α β} (m : Raw α β) :
m.inner.toList = m.toList.map fun k, v => k, v := by
simp [toList,
DHashMap.Internal.Raw.toList_eq_toListModel, DHashMap.Internal.Raw.Const.toList_eq_toListModel,
DHashMap.Internal.Const.toListModel, Function.comp_def]
@[simp]
theorem toList_map_fst (h : m.WF) :
m.toList.map Prod.fst = m.keys := by
simpa using DHashMap.Raw.toList_map_fst (m := m.inner) h.out
end Raw
end Std.HashMap