Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: verify insertMany method for adding lists to HashMaps #6211

Draft
wants to merge 63 commits into
base: master
Choose a base branch
from

Conversation

monsterkrampe
Copy link
Contributor

This PR verifies the insertMany method on HashMaps for the special case of inserting lists.

@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Nov 25, 2024
@leanprover-community-bot
Copy link
Collaborator

leanprover-community-bot commented Nov 25, 2024

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 935fcfb6ec84342163a86058856edcb1dca70ff4 --onto 884a9ea2ff70bb4d0c6da4a1c23ffc26c3a974ee. (2024-11-25 13:30:54)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 0a22f8fa6fef34a6798785785237c66cab173197 --onto 9a17919ef11c2dba824498229633b8333a0b53d9. (2024-11-26 15:53:38)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase b378fe98a7877276b7dff70527b6c924e672c435 --onto 9a17919ef11c2dba824498229633b8333a0b53d9. (2024-11-27 15:47:35)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase b378fe98a7877276b7dff70527b6c924e672c435 --onto 609346f5e03f3f2d559e7cc905f72e950c3ed8c4. (2024-11-28 08:49:25)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase b378fe98a7877276b7dff70527b6c924e672c435 --onto 6d495586a1836da3e12efb4fbf9946bd9088ac5f. (2024-11-29 10:41:50)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase b378fe98a7877276b7dff70527b6c924e672c435 --onto 86f303774a7fbf38406ae13bd4b66f2753643a45. (2024-11-30 15:42:21)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase b378fe98a7877276b7dff70527b6c924e672c435 --onto 27df5e968a675780a7bcd105a19774c0c59295d8. (2024-12-01 19:35:29)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase b378fe98a7877276b7dff70527b6c924e672c435 --onto 3c5e612dc54733cd707becb929457d2f9d8ca6fd. (2024-12-02 15:27:36)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d9d54c1f99363f42d1485f6b67f281a5985f684e --onto 3c5e612dc54733cd707becb929457d2f9d8ca6fd. (2024-12-02 22:28:30)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d9d54c1f99363f42d1485f6b67f281a5985f684e --onto cb600ed9b436e4290b819b0529f8454490bffeb6. (2024-12-03 16:10:17)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d9d54c1f99363f42d1485f6b67f281a5985f684e --onto 019f8e175f9650b829aca8ee501f41c0a5d9076d. (2024-12-05 15:37:52)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d9d54c1f99363f42d1485f6b67f281a5985f684e --onto 8a3a806b1a8a1f0de8868c5bf96375b5dd6581fd. (2024-12-10 18:29:24)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d9d54c1f99363f42d1485f6b67f281a5985f684e --onto 8709ca35e9ddb977f7d050751e04981e2ab0d5c7. (2024-12-11 11:07:25)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d9d54c1f99363f42d1485f6b67f281a5985f684e --onto 48be424eaa2ae06972e9cfec4d355906b532204d. (2024-12-12 18:12:24)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d9d54c1f99363f42d1485f6b67f281a5985f684e --onto a8a160b09147c3225150703ac727eea6ee9a3b0e. (2024-12-15 19:38:27)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d9d54c1f99363f42d1485f6b67f281a5985f684e --onto 6a839796fd537d14055e4a51a78c5272a1b52fb8. (2024-12-28 15:45:57)
  • ✅ Mathlib branch lean-pr-testing-6211 has successfully built against this PR. (2024-12-28 18:08:08) View Log
  • ✅ Mathlib branch lean-pr-testing-6211 has successfully built against this PR. (2025-01-05 18:03:16) View Log
  • ✅ Mathlib branch lean-pr-testing-6211 has successfully built against this PR. (2025-01-05 20:19:35) View Log
  • ✅ Mathlib branch lean-pr-testing-6211 has successfully built against this PR. (2025-01-05 21:40:27) View Log
  • ✅ Mathlib branch lean-pr-testing-6211 has successfully built against this PR. (2025-01-05 22:37:08) View Log

@TwoFX TwoFX self-assigned this Nov 26, 2024
src/Std/Data/DHashMap/Basic.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/RawLemmas.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/RawLemmas.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/RawLemmas.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/RawLemmas.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/List/Associative.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/List/Associative.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/RawLemmas.lean Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/RawLemmas.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/RawLemmas.lean Outdated Show resolved Hide resolved
Copy link
Member

@TwoFX TwoFX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RawLemmas.lean is looking pretty good now, you should be able to start adding the user-facing lemmas after addressing these final two style remarks, but please don't forget about #6211 (comment) (which GitHub unhelpfully doesn't show on the PR page...)

src/Std/Data/DHashMap/Internal/RawLemmas.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/RawLemmas.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/List/Associative.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/List/Associative.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/List/Associative.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/List/Associative.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/List/Associative.lean Outdated Show resolved Hide resolved
src/Std/Data/DHashMap/Internal/List/Associative.lean Outdated Show resolved Hide resolved
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Dec 28, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Dec 28, 2024
@leanprover-community-bot leanprover-community-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Dec 28, 2024

theorem size_ofList_le [EquivBEq α] [LawfulHashable α]
{l : List (α × β)} :
(ofList l).size ≤ l.length := by
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(ofList l).size ≤ l.length := by
(ofList l).size ≤ l.length := by

congr

theorem insertMany_cons {l : List ((a : α) × β a)} {k : α} {v : β k} :
(m.insertMany (⟨k, v⟩ :: l)) = ((m.insert k v).insertMany l) := by
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(m.insertMany (⟨k, v⟩ :: l)) = ((m.insert k v).insertMany l) := by
m.insertMany (⟨k, v⟩ :: l) = (m.insert k v).insertMany l := by

rw [Raw₀.insertMany_cons ⟨m.1, _⟩]
rfl

theorem contains_insertMany_list [EquivBEq α] [LawfulHashable α]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a good simp lemma to me.

(m.insertMany l).size ≤ m.size + l.length :=
Raw₀.size_insertMany_list_le ⟨m.1, _⟩ m.2

theorem isEmpty_insertMany_list [EquivBEq α] [LawfulHashable α]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also a good simp lemma.

apply Subtype.eq
simp [insertMany]
rw [Raw₀.Const.insertMany_cons ⟨m.1, _⟩]
rfl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rfl
rfl

Comment on lines 1462 to 1464
simp only [ofList]
rw [get?_insertMany_list_of_contains_eq_false h]
apply get?_emptyc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love that you have multi-line proofs in DHashMap/Lemmas.lean and DHashMap/RawLemmas.lean, especially since the proofs are duplicated between the two files.

I think that you should prove things like

theorem size_insertMany_empty_list [EquivBEq α] [LawfulHashable α]
    {l : List ((a : α) × β a)} (distinct : l.Pairwise (fun a b => (a.1 == b.1) = false)) :
    ((Raw₀.empty : Raw₀ α β).insertMany l).1.1.size = l.length := by
  simp [size_insertMany_list _ Raw.WF.empty distinct]

in Internal/RawLemmas.lean and invoke that from DHashMap/Lemmas.lean and DHashMap/RawLemmas.lean.

@@ -224,6 +225,21 @@ def containsKey [BEq α] (a : α) : List ((a : α) × β a) → Bool
@[simp] theorem containsKey_cons [BEq α] {l : List ((a : α) × β a)} {k a : α} {v : β k} :
containsKey a (⟨k, v⟩ :: l) = (k == a || containsKey a l) := rfl

theorem containsKey_eq_false_iff [BEq α][PartialEquivBEq α] {l : List ((a : α) × β a)} {a : α} :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this below containsKey_eq_false_if_forall_mem_keys, then the proof is just
simp [containsKey_eq_false_iff_forall_mem_keys, keys_eq_map]

theorem DistinctKeys.def [BEq α] {l : List ((a : α) × β a)} :
DistinctKeys l ↔ l.Pairwise (fun a b => (a.1 == b.1) = false) :=
⟨fun h => by simpa [keys_eq_map, List.pairwise_map] using h.distinct,
fun h => ⟨by simpa [keys_eq_map, List.pairwise_map] using h⟩⟩
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun h => ⟨by simpa [keys_eq_map, List.pairwise_map] using h⟩⟩
fun h => ⟨by simpa [keys_eq_map, List.pairwise_map] using h⟩⟩

· rw [getEntry?_cons_of_true hk]
· rw [getEntry?_cons_of_false, ih hl.tail hkv]
exact BEq.neq_of_neq_of_beq (containsKey_eq_false_iff.1 hl.containsKey_eq_false _ hkv) hk

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

def Const.insertListIfNewUnitₘ [BEq α] [Hashable α](m : Raw₀ α (fun _ => Unit)) (l: List α): Raw₀ α (fun _ => Unit) :=
match l with
| .nil => m
| .cons hd tl => insertListIfNewUnitₘ (m.insertIfNew hd ()) tl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| .cons hd tl => insertListIfNewUnitₘ (m.insertIfNew hd ()) tl
| .cons hd tl => insertListIfNewUnitₘ (m.insertIfNew hd ()) tl

leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Jan 5, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jan 5, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Jan 5, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jan 5, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Jan 5, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jan 5, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Jan 5, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
builds-mathlib CI has verified that Mathlib builds against this PR toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants