Skip to content

Commit 00e2907

Browse files
wkrozowskidatokrat
andauthored
feat: add union on DTreeMap/TreeMap/TreeSet (#10896)
This PR adds union operations on DTreeMap/TreeMap/TreeSet and their raw variants and provides lemmas about union operations. --------- Co-authored-by: Paul Reichert <[email protected]>
1 parent ec77590 commit 00e2907

File tree

25 files changed

+2407
-6
lines changed

25 files changed

+2407
-6
lines changed

src/Std/Data/DHashMap/Lemmas.lean

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1760,10 +1760,15 @@ theorem mem_of_mem_union_of_not_mem_left [EquivBEq α]
17601760
exact @Raw₀.contains_of_contains_union_of_contains_eq_false_left _ _ _ _ ⟨m₁.1, _⟩ ⟨m₂.1, _⟩ _ _ m₁.2 m₂.2 k h₁ h₂
17611761

17621762
/- Equiv -/
1763-
theorem union_insert_right_equiv_union_insert [EquivBEq α] [LawfulHashable α] {p : (a : α) × β a} :
1763+
theorem union_insert_right_equiv_insert_union [EquivBEq α] [LawfulHashable α] {p : (a : α) × β a} :
17641764
(m₁ ∪ (m₂.insert p.fst p.snd)) ~m ((m₁ ∪ m₂).insert p.fst p.snd) :=
17651765
⟨@Raw₀.union_insert_right_equiv_insert_union _ _ _ _ ⟨m₁.1, m₁.2.size_buckets_pos⟩ ⟨m₂.1, m₂.2.size_buckets_pos⟩ _ _ p m₁.2 m₂.2
17661766

1767+
@[deprecated union_insert_right_equiv_insert_union (since := "2025-11-03")]
1768+
theorem union_insert_right_equiv_union_insert [EquivBEq α] [LawfulHashable α] {p : (a : α) × β a} :
1769+
(m₁ ∪ (m₂.insert p.fst p.snd)) ~m ((m₁ ∪ m₂).insert p.fst p.snd) :=
1770+
union_insert_right_equiv_insert_union
1771+
17671772
/- get? -/
17681773
theorem get?_union [LawfulBEq α] {k : α} :
17691774
(m₁ ∪ m₂).get? k = (m₂.get? k).or (m₁.get? k) :=

src/Std/Data/DHashMap/RawLemmas.lean

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1865,12 +1865,18 @@ theorem mem_of_mem_union_of_not_mem_left [EquivBEq α]
18651865
simp_to_raw using Raw₀.contains_of_contains_union_of_contains_eq_false_left
18661866

18671867
/- Equiv -/
1868-
theorem union_insert_right_equiv_union_insert [EquivBEq α] [LawfulHashable α] {p : (a : α) × β a}
1868+
theorem union_insert_right_equiv_insert_union [EquivBEq α] [LawfulHashable α] {p : (a : α) × β a}
18691869
(h₁ : m₁.WF) (h₂ : m₂.WF) :
18701870
(m₁ ∪ (m₂.insert p.fst p.snd)).Equiv ((m₁ ∪ m₂).insert p.fst p.snd) := by
18711871
simp only [Union.union]
18721872
simp_to_raw using Raw₀.union_insert_right_equiv_insert_union
18731873

1874+
@[deprecated union_insert_right_equiv_insert_union (since := "2025-11-03")]
1875+
theorem union_insert_right_equiv_union_insert [EquivBEq α] [LawfulHashable α] {p : (a : α) × β a}
1876+
(h₁ : m₁.WF) (h₂ : m₂.WF) :
1877+
(m₁ ∪ (m₂.insert p.fst p.snd)).Equiv ((m₁ ∪ m₂).insert p.fst p.snd) :=
1878+
union_insert_right_equiv_insert_union h₁ h₂
1879+
18741880
/- get? -/
18751881
theorem get?_union [LawfulBEq α] (h₁ : m₁.WF) (h₂ : m₂.WF)
18761882
{k : α} :

src/Std/Data/DTreeMap/Basic.lean

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,25 @@ appearance.
993993
def insertMany {ρ} [ForIn Id ρ ((a : α) × β a)] (t : DTreeMap α β cmp) (l : ρ) : DTreeMap α β cmp :=
994994
letI : Ord α := ⟨cmp⟩; ⟨t.inner.insertMany l t.wf.balanced, t.wf.insertMany⟩
995995

996+
/--
997+
Inserts multiple mappings into the tree map by iterating over the given collection and calling
998+
`insertIfNew`. If the same key appears multiple times, the first occurrence takes precedence.
999+
-/
1000+
@[inline]
1001+
def insertManyIfNew {ρ} [ForIn Id ρ ((a : α) × β a)] (t : DTreeMap α β cmp) (l : ρ) : DTreeMap α β cmp :=
1002+
letI : Ord α := ⟨cmp⟩; ⟨t.inner.insertManyIfNew l t.wf.balanced, t.wf.insertManyIfNew⟩
1003+
1004+
/--
1005+
Computes the union of the given tree maps. If a key appears in both maps, the entry contained in
1006+
the second argument will appear in the result.
1007+
1008+
This function always merges the smaller map into the larger map.
1009+
-/
1010+
def union (t₁ t₂ : DTreeMap α β cmp) : DTreeMap α β cmp :=
1011+
letI : Ord α := ⟨cmp⟩; ⟨t₁.inner.union t₂.inner t₁.wf.balanced t₂.wf.balanced, @Impl.WF.union α β _ t₁.inner t₁.wf t₂.inner t₂.wf⟩
1012+
1013+
instance : Union (DTreeMap α β cmp) := ⟨union⟩
1014+
9961015
/--
9971016
Erases multiple mappings from the tree map by iterating over the given collection and calling
9981017
`erase`.

0 commit comments

Comments
 (0)