Skip to content

Commit 7a5c0c0

Browse files
committed
chore: convert DHashMap to a structure
1 parent 140a633 commit 7a5c0c0

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

src/Std/Data/DHashMap/Basic.lean

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ be used in nested inductive types. For these use cases, `Std.DHashMap.Raw` and
6161
For a variant that is more convenient for use in proofs because of extensionalities, see
6262
`Std.ExtDHashMap` which is defined in the module `Std.Data.ExtDHashMap`.
6363
-/
64-
def DHashMap (α : Type u) (β : α → Type v) [BEq α] [Hashable α] := { m : DHashMap.Raw α β // m.WF }
64+
structure DHashMap (α : Type u) (β : α → Type v) [BEq α] [Hashable α] where
65+
/-- Internal implementation detail of the hash map. -/
66+
inner : DHashMap.Raw α β
67+
/-- Internal implementation detail of the hash map. -/
68+
wf : inner.WF
6569

6670
namespace DHashMap
6771

src/Std/Data/DHashMap/Lemmas.lean

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ namespace Std.DHashMap
2929

3030
variable {m : DHashMap α β}
3131

32+
private theorem ext {t t' : DHashMap α β} : t.inner = t'.inner → t = t' := by
33+
cases t; cases t'; rintro rfl; rfl
34+
3235
@[simp, grind =]
3336
theorem isEmpty_emptyWithCapacity {c} : (emptyWithCapacity c : DHashMap α β).isEmpty :=
3437
Raw₀.isEmpty_emptyWithCapacity
@@ -51,14 +54,10 @@ theorem mem_iff_contains {a : α} : a ∈ m ↔ m.contains a :=
5154

5255
-- While setting up the API, often use this in the reverse direction,
5356
-- but prefer this direction for users.
54-
@[simp, grind =]
57+
@[simp, grind _=_]
5558
theorem contains_iff_mem {a : α} : m.contains a ↔ a ∈ m :=
5659
Iff.rfl
5760

58-
-- We need to specify the pattern for the reverse direction manually,
59-
-- as the default heuristic leaves the `DHashMap α β` argument as a wildcard.
60-
grind_pattern contains_iff_mem => @Membership.mem α (DHashMap α β) _ m a
61-
6261
theorem contains_congr [EquivBEq α] [LawfulHashable α] {a b : α} (hab : a == b) :
6362
m.contains a = m.contains b :=
6463
Raw₀.contains_congr _ m.2 hab
@@ -169,7 +168,7 @@ theorem size_insert_le [EquivBEq α] [LawfulHashable α] {k : α} {v : β k} :
169168

170169
@[simp, grind =]
171170
theorem erase_emptyWithCapacity {k : α} {c : Nat} : (emptyWithCapacity c : DHashMap α β).erase k = emptyWithCapacity c :=
172-
Subtype.eq (congrArg Subtype.val (Raw₀.erase_emptyWithCapacity (k := k)) :)
171+
ext <| congrArg Subtype.val (Raw₀.erase_emptyWithCapacity (k := k))
173172

174173
@[simp, grind =]
175174
theorem erase_empty {k : α} : (∅ : DHashMap α β).erase k = ∅ :=
@@ -218,7 +217,7 @@ theorem containsThenInsert_fst {k : α} {v : β k} : (m.containsThenInsert k v).
218217

219218
@[simp, grind =]
220219
theorem containsThenInsert_snd {k : α} {v : β k} : (m.containsThenInsert k v).2 = m.insert k v :=
221-
Subtype.eq <| (congrArg Subtype.val (Raw₀.containsThenInsert_snd _ (k := k)) :)
220+
ext <| congrArg Subtype.val (Raw₀.containsThenInsert_snd _ (k := k))
222221

223222
@[simp, grind =]
224223
theorem containsThenInsertIfNew_fst {k : α} {v : β k} :
@@ -228,7 +227,7 @@ theorem containsThenInsertIfNew_fst {k : α} {v : β k} :
228227
@[simp, grind =]
229228
theorem containsThenInsertIfNew_snd {k : α} {v : β k} :
230229
(m.containsThenInsertIfNew k v).2 = m.insertIfNew k v :=
231-
Subtype.eq <| (congrArg Subtype.val (Raw₀.containsThenInsertIfNew_snd _ (k := k)) :)
230+
ext <| congrArg Subtype.val (Raw₀.containsThenInsertIfNew_snd _ (k := k))
232231

233232
@[simp, grind =]
234233
theorem get?_emptyWithCapacity [LawfulBEq α] {a : α} {c} : (emptyWithCapacity c : DHashMap α β).get? a = none :=
@@ -1116,7 +1115,7 @@ theorem getThenInsertIfNew?_fst [LawfulBEq α] {k : α} {v : β k} :
11161115
@[simp, grind =]
11171116
theorem getThenInsertIfNew?_snd [LawfulBEq α] {k : α} {v : β k} :
11181117
(m.getThenInsertIfNew? k v).2 = m.insertIfNew k v :=
1119-
Subtype.eq <| (congrArg Subtype.val (Raw₀.getThenInsertIfNew?_snd _ (k := k)) :)
1118+
ext <| congrArg Subtype.val (Raw₀.getThenInsertIfNew?_snd _ (k := k))
11201119

11211120
namespace Const
11221121

@@ -1129,7 +1128,7 @@ theorem getThenInsertIfNew?_fst {k : α} {v : β} : (getThenInsertIfNew? m k v).
11291128
@[simp, grind =]
11301129
theorem getThenInsertIfNew?_snd {k : α} {v : β} :
11311130
(getThenInsertIfNew? m k v).2 = m.insertIfNew k v :=
1132-
Subtype.eq <| (congrArg Subtype.val (Raw₀.Const.getThenInsertIfNew?_snd _ (k := k)) :)
1131+
ext <| congrArg Subtype.val (Raw₀.Const.getThenInsertIfNew?_snd _ (k := k))
11331132

11341133
end Const
11351134

@@ -1402,16 +1401,16 @@ variable {ρ : Type w} [ForIn Id ρ ((a : α) × β a)]
14021401
@[simp, grind =]
14031402
theorem insertMany_nil :
14041403
m.insertMany [] = m :=
1405-
Subtype.eq (congrArg Subtype.val (Raw₀.insertMany_nil ⟨m.1, m.2.size_buckets_pos⟩) :)
1404+
ext <| congrArg Subtype.val (Raw₀.insertMany_nil ⟨m.1, m.2.size_buckets_pos⟩)
14061405

14071406
@[simp, grind =]
14081407
theorem insertMany_list_singleton {k : α} {v : β k} :
14091408
m.insertMany [⟨k, v⟩] = m.insert k v :=
1410-
Subtype.eq (congrArg Subtype.val (Raw₀.insertMany_list_singleton ⟨m.1, m.2.size_buckets_pos⟩) :)
1409+
ext <| congrArg Subtype.val (Raw₀.insertMany_list_singleton ⟨m.1, m.2.size_buckets_pos⟩)
14111410

14121411
@[grind _=_] theorem insertMany_cons {l : List ((a : α) × β a)} {k : α} {v : β k} :
14131412
m.insertMany (⟨k, v⟩ :: l) = (m.insert k v).insertMany l :=
1414-
Subtype.eq (congrArg Subtype.val (Raw₀.insertMany_cons ⟨m.1, m.2.size_buckets_pos⟩) :)
1413+
ext <| congrArg Subtype.val (Raw₀.insertMany_cons ⟨m.1, m.2.size_buckets_pos⟩)
14151414

14161415
@[grind _=_]
14171416
theorem insertMany_append {l₁ l₂ : List ((a : α) × β a)} :
@@ -1607,17 +1606,17 @@ variable {ρ : Type w} [ForIn Id ρ (α × β)]
16071606
@[simp, grind =]
16081607
theorem insertMany_nil :
16091608
insertMany m [] = m :=
1610-
Subtype.eq (congrArg Subtype.val (Raw₀.Const.insertMany_nil ⟨m.1, m.2.size_buckets_pos⟩) :)
1609+
ext <| congrArg Subtype.val (Raw₀.Const.insertMany_nil ⟨m.1, m.2.size_buckets_pos⟩)
16111610

16121611
@[simp, grind =]
16131612
theorem insertMany_list_singleton {k : α} {v : β} :
16141613
insertMany m [⟨k, v⟩] = m.insert k v :=
1615-
Subtype.eq (congrArg Subtype.val
1616-
(Raw₀.Const.insertMany_list_singleton ⟨m.1, m.2.size_buckets_pos⟩) :)
1614+
ext <| congrArg Subtype.val
1615+
(Raw₀.Const.insertMany_list_singleton ⟨m.1, m.2.size_buckets_pos⟩)
16171616

16181617
@[grind _=_] theorem insertMany_cons {l : List (α × β)} {k : α} {v : β} :
16191618
insertMany m (⟨k, v⟩ :: l) = insertMany (m.insert k v) l :=
1620-
Subtype.eq (congrArg Subtype.val (Raw₀.Const.insertMany_cons ⟨m.1, m.2.size_buckets_pos⟩) :)
1619+
ext <| congrArg Subtype.val (Raw₀.Const.insertMany_cons ⟨m.1, m.2.size_buckets_pos⟩)
16211620

16221621
@[grind _=_]
16231622
theorem insertMany_append {l₁ l₂ : List (α × β)} :
@@ -1819,19 +1818,19 @@ variable {ρ : Type w} [ForIn Id ρ α]
18191818
@[simp]
18201819
theorem insertManyIfNewUnit_nil :
18211820
insertManyIfNewUnit m [] = m :=
1822-
Subtype.eq (congrArg Subtype.val
1823-
(Raw₀.Const.insertManyIfNewUnit_nil ⟨m.1, m.2.size_buckets_pos⟩) :)
1821+
ext <| congrArg Subtype.val
1822+
(Raw₀.Const.insertManyIfNewUnit_nil ⟨m.1, m.2.size_buckets_pos⟩)
18241823

18251824
@[simp]
18261825
theorem insertManyIfNewUnit_list_singleton {k : α} :
18271826
insertManyIfNewUnit m [k] = m.insertIfNew k () :=
1828-
Subtype.eq (congrArg Subtype.val
1829-
(Raw₀.Const.insertManyIfNewUnit_list_singleton ⟨m.1, m.2.size_buckets_pos⟩) :)
1827+
ext <| congrArg Subtype.val
1828+
(Raw₀.Const.insertManyIfNewUnit_list_singleton ⟨m.1, m.2.size_buckets_pos⟩)
18301829

18311830
theorem insertManyIfNewUnit_cons {l : List α} {k : α} :
18321831
insertManyIfNewUnit m (k :: l) = insertManyIfNewUnit (m.insertIfNew k ()) l :=
1833-
Subtype.eq (congrArg Subtype.val
1834-
(Raw₀.Const.insertManyIfNewUnit_cons ⟨m.1, m.2.size_buckets_pos⟩) :)
1832+
ext <| congrArg Subtype.val
1833+
(Raw₀.Const.insertManyIfNewUnit_cons ⟨m.1, m.2.size_buckets_pos⟩)
18351834

18361835
@[elab_as_elim]
18371836
theorem insertManyIfNewUnit_ind {motive : DHashMap α (fun _ => Unit) → Prop}
@@ -2005,16 +2004,16 @@ namespace DHashMap
20052004
@[simp, grind =]
20062005
theorem ofList_nil :
20072006
ofList ([] : List ((a : α) × (β a))) = ∅ :=
2008-
Subtype.eq (congrArg Subtype.val (Raw₀.insertMany_emptyWithCapacity_list_nil (α := α)) :)
2007+
ext <| congrArg Subtype.val (Raw₀.insertMany_emptyWithCapacity_list_nil (α := α))
20092008

20102009
@[simp, grind =]
20112010
theorem ofList_singleton {k : α} {v : β k} :
20122011
ofList [⟨k, v⟩] = (∅: DHashMap α β).insert k v :=
2013-
Subtype.eq (congrArg Subtype.val (Raw₀.insertMany_emptyWithCapacity_list_singleton (α := α)) :)
2012+
ext <| congrArg Subtype.val (Raw₀.insertMany_emptyWithCapacity_list_singleton (α := α))
20142013

20152014
@[grind _=_] theorem ofList_cons {k : α} {v : β k} {tl : List ((a : α) × (β a))} :
20162015
ofList (⟨k, v⟩ :: tl) = ((∅ : DHashMap α β).insert k v).insertMany tl :=
2017-
Subtype.eq (congrArg Subtype.val (Raw₀.insertMany_emptyWithCapacity_list_cons (α := α)) :)
2016+
ext <| congrArg Subtype.val (Raw₀.insertMany_emptyWithCapacity_list_cons (α := α))
20182017

20192018
theorem ofList_eq_insertMany_empty {l : List ((a : α) × β a)} :
20202019
ofList l = insertMany (∅ : DHashMap α β) l := rfl
@@ -2154,16 +2153,16 @@ variable {β : Type v}
21542153
@[simp, grind =]
21552154
theorem ofList_nil :
21562155
ofList ([] : List (α × β)) = ∅ :=
2157-
Subtype.eq (congrArg Subtype.val (Raw₀.Const.insertMany_emptyWithCapacity_list_nil (α:= α)) :)
2156+
ext <| congrArg Subtype.val (Raw₀.Const.insertMany_emptyWithCapacity_list_nil (α:= α))
21582157

21592158
@[simp, grind =]
21602159
theorem ofList_singleton {k : α} {v : β} :
21612160
ofList [⟨k, v⟩] = (∅ : DHashMap α (fun _ => β)).insert k v :=
2162-
Subtype.eq (congrArg Subtype.val (Raw₀.Const.insertMany_emptyWithCapacity_list_singleton (α:= α)) :)
2161+
ext <| congrArg Subtype.val (Raw₀.Const.insertMany_emptyWithCapacity_list_singleton (α:= α))
21632162

21642163
@[grind _=_] theorem ofList_cons {k : α} {v : β} {tl : List (α × β)} :
21652164
ofList (⟨k, v⟩ :: tl) = insertMany ((∅ : DHashMap α (fun _ => β)).insert k v) tl :=
2166-
Subtype.eq (congrArg Subtype.val (Raw₀.Const.insertMany_emptyWithCapacity_list_cons (α:= α)) :)
2165+
ext <| congrArg Subtype.val (Raw₀.Const.insertMany_emptyWithCapacity_list_cons (α:= α))
21672166

21682167
theorem ofList_eq_insertMany_empty {l : List (α × β)} :
21692168
ofList l = insertMany (∅ : DHashMap α (fun _ => β)) l := rfl
@@ -2299,17 +2298,17 @@ theorem isEmpty_ofList [EquivBEq α] [LawfulHashable α]
22992298
@[simp]
23002299
theorem unitOfList_nil :
23012300
unitOfList ([] : List α) = ∅ :=
2302-
Subtype.eq (congrArg Subtype.val (Raw₀.Const.insertManyIfNewUnit_emptyWithCapacity_list_nil (α := α)) :)
2301+
ext <| congrArg Subtype.val (Raw₀.Const.insertManyIfNewUnit_emptyWithCapacity_list_nil (α := α))
23032302

23042303
@[simp]
23052304
theorem unitOfList_singleton {k : α} :
23062305
unitOfList [k] = (∅ : DHashMap α (fun _ => Unit)).insertIfNew k () :=
2307-
Subtype.eq (congrArg Subtype.val (Raw₀.Const.insertManyIfNewUnit_emptyWithCapacity_list_singleton (α := α)) :)
2306+
ext <| congrArg Subtype.val (Raw₀.Const.insertManyIfNewUnit_emptyWithCapacity_list_singleton (α := α))
23082307

23092308
theorem unitOfList_cons {hd : α} {tl : List α} :
23102309
unitOfList (hd :: tl) =
23112310
insertManyIfNewUnit ((∅ : DHashMap α (fun _ => Unit)).insertIfNew hd ()) tl :=
2312-
Subtype.eq (congrArg Subtype.val (Raw₀.Const.insertManyIfNewUnit_emptyWithCapacity_list_cons (α := α)) :)
2311+
ext <| congrArg Subtype.val (Raw₀.Const.insertManyIfNewUnit_emptyWithCapacity_list_cons (α := α))
23132312

23142313
@[simp]
23152314
theorem contains_unitOfList [EquivBEq α] [LawfulHashable α]

0 commit comments

Comments
 (0)