Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
d28319d
push work so far
wkrozowski Nov 19, 2025
fc9c400
Sublemma works
wkrozowski Nov 19, 2025
26bffd8
Sublemma now works
wkrozowski Nov 19, 2025
d586c3f
Main lemma goes through
wkrozowski Nov 19, 2025
ae7a4ac
Add first user-facing lemmas
wkrozowski Nov 19, 2025
54f89f9
Add working model lemma
wkrozowski Nov 20, 2025
9b0035c
Work on the train
wkrozowski Nov 20, 2025
a560481
Lemmas go through
wkrozowski Nov 20, 2025
44f9398
Progress with the const BEq
wkrozowski Nov 26, 2025
f4bce74
push
wkrozowski Nov 26, 2025
4d63159
Add const lemma
wkrozowski Nov 26, 2025
09452de
Add perm lemmas
wkrozowski Nov 26, 2025
fa88c40
Congruence lemmas continued
wkrozowski Nov 26, 2025
fcad743
fix scoping issue
wkrozowski Nov 26, 2025
bd7dca5
Remove unit variant and add the raw variant
wkrozowski Nov 26, 2025
f37ad5f
Add lemmas to DHashMap
wkrozowski Nov 26, 2025
9ce4b8c
Add Raw DHashMapLemmas
wkrozowski Nov 26, 2025
3afdb45
Done with lemmas
wkrozowski Nov 26, 2025
d985064
ExtHashMap done
wkrozowski Nov 26, 2025
7ed80a9
Add BEq to ExtHashSet
wkrozowski Nov 27, 2025
99b62eb
Merge branch 'master' into wojciech/hashmap_beq2
wkrozowski Nov 27, 2025
4a93e11
Fix merge issue
wkrozowski Nov 27, 2025
a15ea22
initial work
wkrozowski Nov 27, 2025
59a0ec6
Remove unnecessary hashable instance
wkrozowski Nov 27, 2025
92eede4
Merge branch 'wojciech/hashmap_beq1' into wojciech/treemap_beq
wkrozowski Nov 27, 2025
2e28da4
Stash changes
wkrozowski Nov 27, 2025
53e4653
Remove unnecessary hashable
wkrozowski Nov 27, 2025
f9bbd2a
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Nov 27, 2025
1390755
Finish toListModel lemmas
wkrozowski Nov 27, 2025
112ebed
Add BEq on Raw DTreeMap
wkrozowski Nov 27, 2025
6edb039
Add BEq to DTreeMap
wkrozowski Nov 27, 2025
3ec5359
Stash
wkrozowski Nov 27, 2025
cea11fe
Continue removing useless Hashable instance
wkrozowski Nov 27, 2025
ae7d8d2
Merge branch 'wojciech/hashmap_beq1' into wojciech/treemap_beq
wkrozowski Nov 27, 2025
a87d1e0
Remove whitespace
wkrozowski Nov 27, 2025
9a2c774
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Nov 27, 2025
d0b0a73
Internal lemmas now work
wkrozowski Nov 27, 2025
1a3dec3
DTreeMap lemmas are now working
wkrozowski Nov 27, 2025
9bab5e7
Raw DTreeMap now works
wkrozowski Nov 27, 2025
3379700
Finish adding all the lemmas
wkrozowski Nov 27, 2025
07ef952
Do the extensional variants
wkrozowski Nov 27, 2025
59738d1
Fix formatting
wkrozowski Nov 27, 2025
d2f27fe
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Nov 27, 2025
396efbd
Merge branch 'master' into wojciech/hashmap_beq2
wkrozowski Nov 27, 2025
8a84316
Remove unused code
wkrozowski Nov 27, 2025
0c4f5a0
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Nov 27, 2025
b928c86
Refactor ExtDHashMap code
wkrozowski Nov 28, 2025
16bac66
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Nov 28, 2025
72ea979
Refactor ExtDTreeMap
wkrozowski Nov 28, 2025
483f612
One more refactor
wkrozowski Nov 28, 2025
e480158
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Nov 28, 2025
4d23872
Merge branch 'master' into wojciech/hashmap_beq2
wkrozowski Dec 1, 2025
9b3cbe2
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Dec 1, 2025
d1f18b9
First part of addressing Markus' comments
wkrozowski Dec 2, 2025
dbc9784
Proof golfing
wkrozowski Dec 2, 2025
40ef92e
Fix notation
wkrozowski Dec 2, 2025
b3b2f97
Apply Markus' suggestions
wkrozowski Dec 2, 2025
84b1bd7
Try refactoring `ExtDHashMap`
wkrozowski Dec 2, 2025
1a93a9f
cleanup extensional variants
wkrozowski Dec 2, 2025
2053ca9
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Dec 2, 2025
afa8693
Refactor
wkrozowski Dec 2, 2025
4e012a6
further renaming
wkrozowski Dec 2, 2025
a2b304d
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Dec 2, 2025
93620fa
Minor changes
wkrozowski Dec 2, 2025
b503c77
Simplify lemmas
wkrozowski Dec 2, 2025
5f625c1
Merge branch 'master' into wojciech/treemap_beq
wkrozowski Dec 2, 2025
3bd07be
Refactor
wkrozowski Dec 4, 2025
7ef173e
Further refactor
wkrozowski Dec 4, 2025
c41aa54
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Dec 4, 2025
3b281d5
Remove `beqUnit`
wkrozowski Dec 4, 2025
d4cb27a
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Dec 4, 2025
c47995a
Weaken the instances for the Const variant
wkrozowski Dec 5, 2025
8adfa66
Cleanup
wkrozowski Dec 5, 2025
81ca6a5
Further cleanup
wkrozowski Dec 5, 2025
b1ed589
Further cleanup
wkrozowski Dec 5, 2025
06c5d1d
One more round of cleanup
wkrozowski Dec 5, 2025
ea94efb
Fix whitespace
wkrozowski Dec 5, 2025
24a11a8
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Dec 5, 2025
0afaf3e
One more round of cleanup
wkrozowski Dec 5, 2025
3079fa6
Further cleanup
wkrozowski Dec 5, 2025
8e79493
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Dec 5, 2025
3be824f
DTreeMap decidable beq
wkrozowski Dec 5, 2025
ad3f669
TreeMap decidable equiv
wkrozowski Dec 5, 2025
e15aca6
Add TreeSet decidable equiv
wkrozowski Dec 5, 2025
20da2a0
Fix instance names
wkrozowski Dec 5, 2025
f52d89c
Add the extensional instances
wkrozowski Dec 5, 2025
a343ae2
Weaken the instances
wkrozowski Dec 5, 2025
9efe6cc
Merge branch 'wojciech/treemap_beq' into wojciech/treemap_deceq
wkrozowski Dec 5, 2025
f353aea
Proof golf
wkrozowski Dec 5, 2025
3438415
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Dec 5, 2025
2119365
Merge branch 'wojciech/treemap_beq' into wojciech/treemap_deceq
wkrozowski Dec 5, 2025
3074be8
Add `any_eq`
wkrozowski Dec 8, 2025
02434df
Minor cleanup
wkrozowski Dec 8, 2025
a269546
chore: proof golf
wkrozowski Dec 8, 2025
5ccdbe2
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Dec 8, 2025
01bdba5
chore: docstrings
wkrozowski Dec 8, 2025
b2c392f
chore: docstrings
wkrozowski Dec 8, 2025
ffb6c3f
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Dec 8, 2025
60b8411
chore: one more docstring fix
wkrozowski Dec 8, 2025
45f54d9
Merge branch 'wojciech/treemap_beq' into wojciech/treemap_deceq
wkrozowski Dec 8, 2025
741ef4d
chore: cleanup
wkrozowski Dec 8, 2025
d0e16d9
chore: cleanup
wkrozowski Dec 8, 2025
b72f7b9
Merge branch 'wojciech/hashmap_beq2' into wojciech/treemap_beq
wkrozowski Dec 8, 2025
0e755c7
chore: fix docstrings
wkrozowski Dec 8, 2025
d0a57a9
Merge branch 'wojciech/treemap_beq' into wojciech/treemap_deceq
wkrozowski Dec 8, 2025
6acecb3
Merge branch 'master' into wojciech/treemap_beq
wkrozowski Dec 10, 2025
2a1bec4
Merge branch 'wojciech/treemap_beq' into wojciech/treemap_deceq
wkrozowski Dec 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Std/Data/DTreeMap.lean
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ public import Std.Data.DTreeMap.AdditionalOperations
public import Std.Data.DTreeMap.Lemmas
public import Std.Data.DTreeMap.Iterator
public import Std.Data.DTreeMap.Slice
public import Std.Data.DTreeMap.DecidableEquiv
23 changes: 15 additions & 8 deletions src/Std/Data/DTreeMap/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -870,17 +870,11 @@ end Const

/-- Check if any element satisfies the predicate, short-circuiting if a predicate fails. -/
@[inline]
def any (t : DTreeMap α β cmp) (p : (a : α) → β a → Bool) : Bool := Id.run $ do
for ⟨a, b⟩ in t do
if p a b then return true
return false
def any (t : DTreeMap α β cmp) (p : (a : α) → β a → Bool) : Bool := t.inner.any p

/-- Check if all elements satisfy the predicate, short-circuiting if a predicate fails. -/
@[inline]
def all (t : DTreeMap α β cmp) (p : (a : α) → β a → Bool) : Bool := Id.run $ do
for ⟨a, b⟩ in t do
if p a b = false then return false
return true
def all (t : DTreeMap α β cmp) (p : (a : α) → β a → Bool) : Bool := t.inner.all p

/-- Returns a list of all keys present in the tree map in ascending order. -/
@[inline]
Expand Down Expand Up @@ -1054,6 +1048,19 @@ def inter (t₁ t₂ : DTreeMap α β cmp) : DTreeMap α β cmp :=

instance : Inter (DTreeMap α β cmp) := ⟨inter⟩

/--
Compares two tree maps using Boolean equality on keys and values.

Returns `true` if the maps contain the same key-value pairs, `false` otherwise.
-/
def beq [LawfulEqCmp cmp] [∀ k, BEq (β k)] (t₁ t₂ : DTreeMap α β cmp) : Bool :=
letI : Ord α := ⟨cmp⟩; t₁.inner.beq t₂.inner

instance [LawfulEqCmp cmp] [∀ k, BEq (β k)] : BEq (DTreeMap α β cmp) := ⟨beq⟩

@[inherit_doc DTreeMap.beq] def Const.beq {β : Type v} [BEq β] (t₁ t₂ : DTreeMap α (fun _ => β) cmp) : Bool :=
letI : Ord α := ⟨cmp⟩; Internal.Impl.Const.beq t₁.inner t₂.inner

/--
Computes the difference of the given tree maps.
This function always iterates through the smaller map.
Expand Down
27 changes: 27 additions & 0 deletions src/Std/Data/DTreeMap/DecidableEquiv.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/-
Copyright (c) 2025 Lean FRO, LLC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Wojciech Różowski
-/
module

prelude
public import Std.Data.DTreeMap.Internal.Lemmas
public import Std.Data.DTreeMap.Raw

public section

/-!
# Decidable equivalence for `DTreeMap`
-/

open Std.DTreeMap.Internal.Impl

namespace Std.DTreeMap

instance {α : Type u} {β : α → Type v} {cmp : α → α → Ordering} [TransCmp cmp] [LawfulEqCmp cmp] [DecidableEq α] [∀ k, DecidableEq (β k)] {t₁ t₂ : DTreeMap α β cmp} : Decidable (t₁ ~m t₂) :=
let : Ord α := ⟨cmp⟩;
let : Decidable (t₁.inner ~m t₂.inner) := decidableEquiv t₁.1 t₂.1 t₁.2 t₂.2;
decidable_of_iff _ ⟨fun h => ⟨h⟩, fun h => h.1⟩

end Std.DTreeMap
41 changes: 41 additions & 0 deletions src/Std/Data/DTreeMap/Internal/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ private meta def queryMap : Std.DHashMap Name (fun _ => Name × Array (MacroM (T
⟨`getKeyD, (``getKeyD_eq_getKeyD, #[``(getKeyD_of_perm _)])⟩,
⟨`getKey!, (``getKey!_eq_getKey!, #[``(getKey!_of_perm _)])⟩,
⟨`toList, (``toList_eq_toListModel, #[])⟩,
⟨`beq, (``beq_eq_beqModel, #[])⟩,
⟨`Const.beq, (``Internal.Impl.Const.beq_eq_beqModel, #[])⟩,
⟨`keys, (``keys_eq_keys, #[])⟩,
⟨`Const.toList, (``Const.toList_eq_toListModel_map, #[])⟩,
⟨`foldlM, (``foldlM_eq_foldlM_toListModel, #[])⟩,
Expand Down Expand Up @@ -4851,6 +4853,37 @@ theorem get!_inter!_of_contains_eq_false_left [TransOrd α] [Inhabited β] (h₁

end Const

section BEq
variable {m₁ m₂ : Impl α β} [TransOrd α] [LawfulEqOrd α] [∀ k, BEq (β k)]

theorem Equiv.beq [∀ k, ReflBEq (β k)] (h₁ : m₁.WF) (h₂ : m₂.WF) : m₁.Equiv m₂ → beq m₁ m₂ := by
simp_to_model using List.beqModel_eq_true_of_perm

theorem equiv_of_beq [∀ k, LawfulBEq (β k)] (h₁ : m₁.WF) (h₂ : m₂.WF) : beq m₁ m₂ = true → m₁.Equiv m₂ := by
simp_to_model using List.perm_of_beqModel

theorem Equiv.beq_congr {m₃ m₄ : Impl α β} (h₁ : m₁.WF) (h₂ : m₂.WF) (h₃ : m₃.WF) (h₄ : m₄.WF) :
m₁.Equiv m₃ → m₂.Equiv m₄ → (Impl.beq m₁ m₂) = (Impl.beq m₃ m₄) := by
simp_to_model using List.beqModel_congr

end BEq

section

variable {β : Type v} [BEq β] {m₁ m₂ : Impl α (fun _ => β)}

theorem Const.Equiv.beq [TransOrd α] [ReflBEq β] (h₁ : m₁.WF) (h₂ : m₂.WF) : m₁.Equiv m₂ → Const.beq m₁ m₂ := by
simp_to_model using List.Const.beqModel_eq_true_of_perm

theorem Const.equiv_of_beq [TransOrd α] [LawfulEqOrd α] [LawfulBEq β] (h₁ : m₁.WF) (h₂ : m₂.WF) : Const.beq m₁ m₂ = true → m₁.Equiv m₂ := by
simp_to_model using List.Const.perm_of_beqModel

theorem Const.Equiv.beq_congr [TransOrd α] {m₃ m₄ : Impl α (fun _ => β)} (h₁ : m₁.WF) (h₂ : m₂.WF) (h₃ : m₃.WF) (h₄ : m₄.WF) :
m₁.Equiv m₃ → m₂.Equiv m₄ → Const.beq m₁ m₂ = Const.beq m₃ m₄ := by
simp_to_model using List.Const.beqModel_congr

end

section Diff

variable {m₁ m₂ : Impl α β}
Expand Down Expand Up @@ -10341,4 +10374,12 @@ end Const

end map

section

/-- Internal implementation detail -/
def decidableEquiv {α : Type u} {β : α → Type v} [Ord α] [TransOrd α] [LawfulEqOrd α] [DecidableEq α] [∀ k, DecidableEq (β k)] (t₁ t₂ : Impl α β) (h₁ : t₁.WF) (h₂ : t₂.WF) : Decidable (t₁.Equiv t₂) :=
decidable_of_iff (beq t₁ t₂ = true) ⟨equiv_of_beq h₁ h₂, Equiv.beq h₁ h₂⟩

end

end Std.DTreeMap.Internal.Impl
9 changes: 9 additions & 0 deletions src/Std/Data/DTreeMap/Internal/Operations.lean
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,10 @@ information but still assumes the preconditions of `filter`, otherwise might pan
def inter! [Ord α] (m₁ m₂ : Impl α β): Impl α β :=
if m₁.size ≤ m₂.size then m₁.filter! (fun k _ => m₂.contains k) else interSmaller m₁ m₂

/-- Internal implementation detail of the tree map -/
def beq [Ord α] [LawfulEqOrd α] [∀ k, BEq (β k)] (t₁ t₂ : Impl α β) : Bool :=
if t₁.size ≠ t₂.size then false else t₁.all (fun k v => t₂.get? k == some v)

/--
Computes the difference of the given tree maps.
This function always iterates through the smaller map.
Expand Down Expand Up @@ -950,6 +954,11 @@ def mergeWith! [Ord α] [LawfulEqOrd α] (mergeFn : (a : α) → β a → β a
namespace Const

variable {β : Type v}

/-- Internal implementation detail of the hash map -/
def beq [Ord α] [BEq β] (t₁ t₂ : Impl α fun _ => β) : Bool :=
if t₁.size ≠ t₂.size then false else t₁.all (fun k v => Const.get? t₂ k == some v)

local instance : Coe (Type v) (α → Type v) where coe γ := fun _ => γ

/--
Expand Down
14 changes: 14 additions & 0 deletions src/Std/Data/DTreeMap/Internal/Queries.lean
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ def forIn {m} [Monad m] (f : (a : α) → β a → δ → m (ForInStep δ)) (ini
instance [Monad m] : ForIn m (Impl α β) ((a : α) × β a) where
forIn m init f := m.forIn (fun a b acc => f ⟨a, b⟩ acc) init

/-- Implementation detail. -/
@[inline]
def any (t : Impl α β) (p : (a : α) → β a → Bool) : Bool := Id.run $ do
for ⟨a, b⟩ in t do
if p a b then return true
return false

/-- Implementation detail. -/
@[inline]
def all (t : Impl α β) (p : (a : α) → β a → Bool) : Bool := Id.run $ do
for ⟨a, b⟩ in t do
if p a b = false then return false
return true

/-- Returns a `List` of the keys in order. -/
@[inline] def keys (t : Impl α β) : List α :=
t.foldr (init := []) fun k _ l => k :: l
Expand Down
23 changes: 23 additions & 0 deletions src/Std/Data/DTreeMap/Internal/WF/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,29 @@ theorem WF.union! {_ : Ord α} [TransOrd α]
. exact WF.insertManyIfNew! h₂
. exact WF.insertMany! h₁

theorem all_eq_all_toListModel {p : (a : α) → β a → Bool} {m : Impl α β} :
m.all p = m.toListModel.all (fun x => p x.1 x.2) := by
simp [all, ForIn.forIn, bind_pure_comp, map_pure, Id.run_bind]
rw [forIn_eq_forIn_toListModel, ← toList_eq_toListModel, forIn_eq_forIn']
induction m.toList with
| nil => simp
| cons hd tl ih =>
simp only [forIn'_eq_forIn, List.all_cons]
by_cases h : p hd.fst hd.snd = false
· simp [h]
· simp only [forIn'_eq_forIn] at ih
simp [h, ih]

theorem beq_eq_beqModel {_ : Ord α} [BEq α] [TransOrd α] [LawfulBEq α] [LawfulBEqOrd α] [∀ k, BEq (β k)] {m₁ m₂ : Impl α β} (h₁ : m₁.WF) (h₂ : m₂.WF) :
Impl.beq m₁ m₂ = beqModel m₁.toListModel m₂.toListModel := by
simp [beq, beqModel, size_eq_length _ h₁.balanced, size_eq_length _ h₂.balanced, all_eq_all_toListModel,
get?_eq_getValueCast? h₂.ordered]

theorem Const.beq_eq_beqModel {β : Type v} {_ : Ord α} [BEq α] [TransOrd α] [LawfulBEqOrd α] [BEq β] {m₁ m₂ : Impl α (fun _ => β)} (h₁ : m₁.WF) (h₂ : m₂.WF) :
beq m₁ m₂ = Const.beqModel m₁.toListModel m₂.toListModel := by
simp [beq, Const.beqModel, size_eq_length _ h₁.balanced, size_eq_length _ h₂.balanced, all_eq_all_toListModel,
get?_eq_getValue? h₂.ordered]

theorem WF.constInsertMany! {β : Type v} {_ : Ord α} [TransOrd α] {ρ} [ForIn Id ρ (α × β)] {l : ρ}
{t : Impl α β} (h : t.WF) : (Const.insertMany! t l).1.WF :=
(Const.insertMany! t l).2 h (fun _ _ _ h' => h'.insert!)
Expand Down
34 changes: 32 additions & 2 deletions src/Std/Data/DTreeMap/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,36 @@ theorem get!_inter_of_not_mem_left [TransCmp cmp] [Inhabited β]

end Const

section BEq

variable {m₁ m₂ : DTreeMap α β cmp} [∀ k, BEq (β k)] [LawfulEqCmp cmp] [TransCmp cmp]

theorem Equiv.beq [∀ k, ReflBEq (β k)] (h : m₁ ~m m₂) : m₁ == m₂ :=
Impl.Equiv.beq m₁.2 m₂.2 h.1

theorem equiv_of_beq [∀ k, LawfulBEq (β k)] (h : m₁ == m₂) : m₁ ~m m₂ :=
⟨Impl.equiv_of_beq m₁.2 m₂.2 h⟩

theorem Equiv.beq_congr {m₃ m₄ : DTreeMap α β cmp} (w₁ : m₁ ~m m₃) (w₂ : m₂ ~m m₄) : (m₁ == m₂) = (m₃ == m₄) :=
Impl.Equiv.beq_congr m₁.2 m₂.2 m₃.2 m₄.2 w₁.1 w₂.1

end BEq

section

variable {β : Type v} {m₁ m₂ : DTreeMap α (fun _ => β) cmp} [BEq β]

theorem Const.Equiv.beq [TransCmp cmp] [ReflBEq β] (h : m₁ ~m m₂) : DTreeMap.Const.beq m₁ m₂ :=
Impl.Const.Equiv.beq m₁.2 m₂.2 h.1

theorem Const.equiv_of_beq [TransCmp cmp] [LawfulEqCmp cmp] [LawfulBEq β] (h : Const.beq m₁ m₂) : m₁ ~m m₂ :=
⟨Impl.Const.equiv_of_beq m₁.2 m₂.2 h⟩

theorem Const.Equiv.beq_congr [TransCmp cmp] {m₃ m₄ : DTreeMap α (fun _ => β) cmp} (w₁ : m₁ ~m m₃) (w₂ : m₂ ~m m₄) : Const.beq m₁ m₂ = Const.beq m₃ m₄ :=
Impl.Const.Equiv.beq_congr m₁.2 m₂.2 m₃.2 m₄.2 w₁.1 w₂.1

end

section Diff

variable {t₁ t₂ : DTreeMap α β cmp}
Expand Down Expand Up @@ -5351,10 +5381,10 @@ theorem forM_eq [TransCmp cmp] [Monad m] [LawfulMonad m] {f : (a : α) × β a
h.1.forM_eq t₁.2 t₂.2

theorem any_eq [TransCmp cmp] {p : (a : α) → β a → Bool} (h : t₁ ~m t₂) : t₁.any p = t₂.any p := by
simp only [any, h.forIn_eq]
simp only [any, Impl.any, ForIn.forIn, h.1.forIn_eq t₁.2 t₂.2]

theorem all_eq [TransCmp cmp] {p : (a : α) → β a → Bool} (h : t₁ ~m t₂) : t₁.all p = t₂.all p := by
simp only [all, h.forIn_eq]
simp only [all, Impl.all, ForIn.forIn, h.1.forIn_eq t₁.2 t₂.2]

theorem minKey?_eq [TransCmp cmp] (h : t₁ ~m t₂) : t₁.minKey? = t₂.minKey? :=
h.1.minKey?_eq t₁.2 t₂.2
Expand Down
1 change: 1 addition & 0 deletions src/Std/Data/DTreeMap/Raw.lean
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ public import Std.Data.DTreeMap.Raw.Lemmas
public import Std.Data.DTreeMap.Raw.WF
public import Std.Data.DTreeMap.Raw.Iterator
public import Std.Data.DTreeMap.Raw.Slice
public import Std.Data.DTreeMap.Raw.DecidableEquiv
19 changes: 12 additions & 7 deletions src/Std/Data/DTreeMap/Raw/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -604,16 +604,12 @@ def forInUncurried (f : α × β → δ → m (ForInStep δ)) (init : δ) (t : R
end Const

@[inline, inherit_doc DTreeMap.any]
def any (t : Raw α β cmp) (p : (a : α) → β a → Bool) : Bool := Id.run $ do
for ⟨a, b⟩ in t do
if p a b then return true
return false
def any (t : Raw α β cmp) (p : (a : α) → β a → Bool) : Bool :=
t.inner.any p

@[inline, inherit_doc DTreeMap.all]
def all (t : Raw α β cmp) (p : (a : α) → β a → Bool) : Bool := Id.run $ do
for ⟨a, b⟩ in t do
if p a b = false then return false
return true
t.inner.all p

@[inline, inherit_doc DTreeMap.keys]
def keys (t : Raw α β cmp) : List α :=
Expand Down Expand Up @@ -732,6 +728,15 @@ def inter (t₁ t₂ : Raw α β cmp) : Raw α β cmp :=

instance : Inter (Raw α β cmp) := ⟨inter⟩


@[inherit_doc DTreeMap.beq] def beq [LawfulEqCmp cmp] [∀ k, BEq (β k)] (t₁ t₂ : Raw α β cmp) : Bool :=
letI : Ord α := ⟨cmp⟩; t₁.inner.beq t₂.inner

instance [LawfulEqCmp cmp] [∀ k, BEq (β k)] : BEq (Raw α β cmp) := ⟨beq⟩

@[inherit_doc DTreeMap.beq] def Const.beq {β : Type v} [BEq β] (t₁ t₂ : Raw α (fun _ => β) cmp) : Bool :=
letI : Ord α := ⟨cmp⟩; Internal.Impl.Const.beq t₁.inner t₂.inner

/--
Computes the difference of the given tree maps.
This function always iterates through the smaller map.
Expand Down
27 changes: 27 additions & 0 deletions src/Std/Data/DTreeMap/Raw/DecidableEquiv.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/-
Copyright (c) 2025 Lean FRO, LLC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Wojciech Różowski
-/
module

prelude
public import Std.Data.DTreeMap.Internal.Lemmas
public import Std.Data.DTreeMap.Raw.Basic

public section

/-!
# Decidable equivalence for `DTreeMap.Raw`
-/

open Std.DTreeMap.Internal.Impl

namespace Std.DTreeMap.Raw

instance instDecidableEquiv {α : Type u} {β : α → Type v} {cmp : α → α → Ordering} [TransCmp cmp] [LawfulEqCmp cmp] [DecidableEq α] [∀ k, DecidableEq (β k)] {t₁ t₂ : Raw α β cmp} (h₁ : t₁.WF) (h₂ : t₂.WF) : Decidable (t₁ ~m t₂) :=
let : Ord α := ⟨cmp⟩;
let : Decidable (t₁.inner ~m t₂.inner) := decidableEquiv t₁.1 t₂.1 h₁ h₂;
decidable_of_iff _ ⟨fun h => ⟨h⟩, fun h => h.1⟩

end Std.DTreeMap.Raw
36 changes: 34 additions & 2 deletions src/Std/Data/DTreeMap/Raw/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -3044,6 +3044,36 @@ theorem get!_inter_of_not_mem_left [TransCmp cmp] [Inhabited β] (h₁ : m₁.WF

end Const

section BEq
variable {m₁ m₂ : Raw α β cmp} [∀ k, BEq (β k)] [LawfulEqCmp cmp] [TransCmp cmp]

theorem Equiv.beq [∀ k, ReflBEq (β k)] (h₁ : m₁.WF) (h₂ : m₂.WF) (h : m₁ ~m m₂) : beq m₁ m₂ :=
Impl.Equiv.beq h₁ h₂ h.1

theorem equiv_of_beq [∀ k, LawfulBEq (β k)] (h₁ : m₁.WF) (h₂ : m₂.WF) : beq m₁ m₂ = true → m₁ ~m m₂ := fun hyp =>
let : Ord α := ⟨cmp⟩; ⟨Impl.equiv_of_beq h₁.1 h₂.1 hyp⟩

theorem Equiv.beq_congr {m₃ m₄ : Raw α β cmp} (h₁ : m₁.WF) (h₂ : m₂.WF) (h₃ : m₃.WF) (h₄ : m₄.WF) :
m₁ ~m m₃ → m₂ ~m m₄ → Raw.beq m₁ m₂ = Raw.beq m₃ m₄ :=
fun w1 w2 => Impl.Equiv.beq_congr h₁ h₂ h₃ h₄ w1.1 w2.1

end BEq

section
variable {β : Type v} {m₁ m₂ : Raw α (fun _ => β) cmp}

theorem Const.Equiv.beq [TransCmp cmp] [BEq β] [ReflBEq β] (h₁ : m₁.WF) (h₂ : m₂.WF) : m₁ ~m m₂ → beq m₁ m₂ :=
fun h => Impl.Const.Equiv.beq h₁ h₂ h.1

theorem Const.equiv_of_beq [TransCmp cmp] [LawfulEqCmp cmp] [BEq β] [LawfulBEq β] (h₁ : m₁.WF) (h₂ : m₂.WF) : beq m₁ m₂ = true → m₁ ~m m₂ :=
fun hyp => let : Ord α := ⟨cmp⟩; ⟨Impl.Const.equiv_of_beq h₁.1 h₂.1 hyp⟩

theorem Const.Equiv.beq_congr [TransCmp cmp] [BEq β] {m₃ m₄ : Raw α (fun _ => β) cmp} (h₁ : m₁.WF) (h₂ : m₂.WF) (h₃ : m₃.WF) (h₄ : m₄.WF) :
m₁ ~m m₃ → m₂ ~m m₄ → Raw.Const.beq m₁ m₂ = Raw.Const.beq m₃ m₄ :=
fun w1 w2 => Impl.Const.Equiv.beq_congr h₁ h₂ h₃ h₄ w1.1 w2.1

end

section Diff

variable {t₁ t₂ : Raw α β cmp}
Expand Down Expand Up @@ -5269,11 +5299,13 @@ theorem forM_eq [TransCmp cmp] [Monad m] [LawfulMonad m] {f : (a : α) × β a

theorem any_eq [TransCmp cmp] {p : (a : α) → β a → Bool} (h₁ : t₁.WF) (h₂ : t₂.WF) (h : t₁ ~m t₂) :
t₁.any p = t₂.any p := by
simp only [any, h.forIn_eq h₁ h₂]
simp only [any, Impl.any, ForIn.forIn, bind_pure_comp, map_pure, h.1.forIn_eq h₁.1 h₂.1,
Id.run_bind]

theorem all_eq [TransCmp cmp] {p : (a : α) → β a → Bool} (h₁ : t₁.WF) (h₂ : t₂.WF) (h : t₁ ~m t₂) :
t₁.all p = t₂.all p := by
simp only [all, h.forIn_eq h₁ h₂]
simp only [all, Impl.all, ForIn.forIn, bind_pure_comp, map_pure, h.1.forIn_eq h₁.1 h₂.1,
Id.run_bind]

theorem minKey?_eq [TransCmp cmp] (h₁ : t₁.WF) (h₂ : t₂.WF) (h : t₁ ~m t₂) :
t₁.minKey? = t₂.minKey? :=
Expand Down
Loading
Loading