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

Specify behavior of std::collections::HashMap #1165

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

jaylorch
Copy link
Collaborator

@jaylorch jaylorch commented Jun 14, 2024

This code adds specifications for the standard-library type std::collections::HashMap. It also adds utility structures HashMapWithView and StringHashMap.

Most of the specification for HashMap only applies if you use HashMap<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 and Boxes thereof meet these requirements. But if you want to use some other key type MyKey, you need to explicitly state your assumption that it satisfies these requirements with assume(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 a HashMap 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 explicitly broadcast use them.

For cases where you want the view of a HashMap to be Map<<Key as View>::V, Value> instead of Map<Key, Value>, use vstd::hash_map::HashMapWithView. For the specific case that you want the key to be String, use vstd::hash_map::StringHashMap.

Copy link
Collaborator

@parno parno left a 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.

source/vstd/std_specs/hash.rs Show resolved Hide resolved
source/vstd/std_specs/hash.rs Outdated Show resolved Hide resolved
source/vstd/std_specs/hash.rs Outdated Show resolved Hide resolved
source/rust_verify_test/tests/hash.rs Outdated Show resolved Hide resolved
source/vstd/std_specs/hash.rs Outdated Show resolved Hide resolved
@jaylorch
Copy link
Collaborator Author

Is verifier::prune_unless_this_module_is_used effective enough that we can just add these axioms into the global ambient context, without worrying about performance implications?

@tjhance
Copy link
Collaborator

tjhance commented Jul 8, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants