-
Notifications
You must be signed in to change notification settings - Fork 66
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
Implement View and DeepView in slice and array #1078
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9ef84db
Implement View and DeepView in slice and array
Chris-Hawblitzel ecf0c53
Remove unnecessary verusfmt::skip
Chris-Hawblitzel 4d849c0
Merge branch 'main' into view-slice-array
Chris-Hawblitzel 5e2bfbf
Add broadcast group for slice
Chris-Hawblitzel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,18 +10,29 @@ pub use super::std_specs::vec::VecAdditionalSpecFns; | |
|
||
verus! { | ||
|
||
pub trait SliceAdditionalSpecFns<T> { | ||
impl<T> View for [T] { | ||
type V = Seq<T>; | ||
|
||
spec fn view(&self) -> Seq<T>; | ||
} | ||
|
||
impl<T: DeepView> DeepView for [T] { | ||
type V = Seq<T::V>; | ||
|
||
open spec fn deep_view(&self) -> Seq<T::V> { | ||
let v = self.view(); | ||
Seq::new(v.len(), |i: int| v[i].deep_view()) | ||
} | ||
} | ||
|
||
pub trait SliceAdditionalSpecFns<T>: View<V = Seq<T>> { | ||
spec fn spec_index(&self, i: int) -> T | ||
recommends | ||
0 <= i < self.view().len(), | ||
; | ||
} | ||
|
||
impl<T> SliceAdditionalSpecFns<T> for [T] { | ||
spec fn view(&self) -> Seq<T>; | ||
|
||
#[verifier(inline)] | ||
open spec fn spec_index(&self, i: int) -> T { | ||
self.view().index(i) | ||
|
@@ -39,6 +50,27 @@ pub exec fn slice_index_get<T>(slice: &[T], i: usize) -> (out: &T) | |
&slice[i] | ||
} | ||
|
||
////// Len (with autospec) | ||
pub open spec fn spec_slice_len<T>(slice: &[T]) -> usize; | ||
|
||
// This axiom is slightly better than defining spec_slice_len to just be `[email protected]() as usize` | ||
// (the axiom also shows that [email protected]() is in-bounds for usize) | ||
#[verifier(external_body)] | ||
pub broadcast proof fn axiom_spec_len<T>(slice: &[T]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will now need to be added to the |
||
ensures | ||
#[trigger] spec_slice_len(slice) == [email protected](), | ||
{ | ||
} | ||
|
||
#[verifier::external_fn_specification] | ||
#[verifier::when_used_as_spec(spec_slice_len)] | ||
pub fn slice_len<T>(slice: &[T]) -> (len: usize) | ||
ensures | ||
len == spec_slice_len(slice), | ||
{ | ||
slice.len() | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
#[verifier(external_body)] | ||
pub exec fn slice_to_vec<T: Copy>(slice: &[T]) -> (out: alloc::vec::Vec<T>) | ||
|
@@ -58,13 +90,4 @@ pub exec fn slice_subrange<T, 'a>(slice: &'a [T], i: usize, j: usize) -> (out: & | |
&slice[i..j] | ||
} | ||
|
||
#[verifier(external_fn_specification)] | ||
pub exec fn slice_len<T>(slice: &[T]) -> (length: usize) | ||
ensures | ||
length >= 0, | ||
length == [email protected](), | ||
{ | ||
slice.len() | ||
} | ||
|
||
} // verus! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just making a quick note here that @y1ca1 had some concerns (during a separate discussion) about this adding more triggers than explicitly reimplementing
deep_view
on each of the vstd data structures individually.Personally I think that implementing
deep_view
separately on each of (say)[u8]
,[u32]
,[u64]
and so on is not really a good idea (because it doesn't scale and would prevent usage on user-defined types without them manually also implementing deepview on[Foo]
), and that we should just go with the current approach.However, @y1ca1, if I've misunderstood what you meant, please do comment here.
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 for mentioning this. What I meant by "explicitly reimplementing"
deep_view
is to provide separate implementation on each of the methods for the generic slice type[T]
whose specs are described indeep_view
. This would arguably save a lot of triggers if one usesdeep_view
ubiquitously, but yeah, definitely less ergonomic and harder to maintain.