lean4-htt/tests/lean/run/grind_map.lean
Kim Morrison 6395d69140
feat: add HashMap.get*_filter* lemmas specialized for LawfulBEq (#8399)
This PR adds variants of `HashMap.getElem?_filter` that assume
`LawfulBEq` and have a simpler right-hand-side. `simp` can already
achieve these, via rewriting with `getKey_eq` under the lambda. However
`grind` can not, and these lemmas help `grind` work with `HashMap`
goals. There are variants for all variants of `HashMap`,
`getElem?/getElem/getElem!/getD`, and for `filter` and `filterMap`.
2025-05-20 03:04:32 +00:00

50 lines
1.8 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Std.Data.HashMap
import Std.Data.DHashMap
import Std.Data.ExtHashMap
import Std.Data.HashSet
import Std.Data.TreeMap
set_option grind.warning false
open Std
section
variable [BEq α] [LawfulBEq α] [Hashable α] [LawfulHashable α ]
example : (∅ : DHashMap α β).isEmpty := by grind
example (m : DHashMap α β) (h : m = ∅) : m.isEmpty := by grind
example : (((∅ : HashMap Nat Nat).insert 3 6).insert 4 7).contains 3 := by grind
example : (((∅ : HashMap Nat Nat).insert 3 6).insert 4 7).contains 9 == false := by grind
example (m : HashMap Nat Nat) (h : m.contains 3) : (m.erase 2).contains 3 := by grind
example (m : HashMap Nat Nat) (h : (m.erase 2).contains 3) : m.contains 3 := by grind
example (m : HashMap Nat Nat) : (m.erase 3).contains 3 = false := by grind
example (m : HashMap Nat Nat) (h : m.contains 3 = false) : (m.erase 2).contains 3 = false := by grind
-- Insert twice
example (m : HashMap Nat Nat) : m.size ≤ ((m.insert 1 2).insert 3 4).size := by grind
example (m : HashMap Nat Nat) : ((m.insert 1 2).insert 3 4).size ≤ m.size + 2 := by grind
-- Insert the same key twice
example (m : HashMap Nat Nat) : m.size ≤ ((m.insert 1 2).insert 1 4).size := by grind
example (m : HashMap Nat Nat) : ((m.insert 1 2).insert 1 4).size ≤ m.size + 1 := by grind
example : (((∅ : HashMap Nat Nat).insert 3 6).erase 4)[3]? = some 6 := by grind
open scoped HashMap in
example (m : HashMap Nat Nat) :
(m.insert 1 2).filter (fun k _ => k > 1000) ~m m.filter fun k _ => k > 1000 := by
apply HashMap.Equiv.of_forall_getElem?_eq
grind (gen := 6)
example [BEq α] [LawfulBEq α] [Hashable α] [LawfulHashable α]
{m : HashMap α β} {f : α → β → γ} {k : α} :
(m.map f)[k]? = m[k]?.map (f k) := by
grind
example (m : Std.TreeMap Nat Bool) : (m.insert 37 true)[32]? = m[32]? := by
grind
end