-
Notifications
You must be signed in to change notification settings - Fork 58
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
Specify behavior of std::collections::HashMap #1165
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks very much for putting this together! It will be very handy to have (even partial) support for reasoning about HashMaps.
Is |
Sorry for the long delay on this. We discussed hash map specs for a while at the retreat, and IIRC, the decision then was to add a new trait and add the capability to write specs dependent on this trait bound. However, it might be some time before we get that working. Meanwhile, this PR is available now, and the feature is a long-requested one. Should we proceed with this, with the expectation of later moving to what we decided on at the retreat? |
This code adds specifications for the standard-library type
std::collections::HashMap
. It also adds utility structuresHashMapWithView
andStringHashMap
.Most of the specification for
HashMap
only applies if you useHashMap<Key, Value>
. If you use some custom build hasher, e.g., withHashMap<Key, Value, CustomBuildHasher>
, the specification won't specify much.Likewise, the specification is only meaningful when you know that
Key::hash
is deterministic and that only identical keys satisfy the executable==
operator. We have an axiom that all primitive types andBox
es thereof meet these requirements. But if you want to use some other key typeMyKey
, you need to explicitly state your assumption that it satisfies these requirements withassume(vstd::std_specs::hash::obeys_key_model::<MyKey>());
. In the future, we plan to devise a way for you to prove that it satisfies these requirements so you don't have to make such an assumption.To make most use of the specification, you should use
broadcast use vstd::std_specs::hash::group_hash_axioms;
. This will bring various useful axioms about the behavior of aHashMap
into the ambient reasoning context. In the future, if we find that having these axioms in scope doesn't impact performance, we may put them into the global ambient context so you don't have to explicitlybroadcast use
them.For cases where you want the view of a
HashMap
to beMap<<Key as View>::V, Value>
instead ofMap<Key, Value>
, usevstd::hash_map::HashMapWithView
. For the specific case that you want the key to beString
, usevstd::hash_map::StringHashMap
.