Autoview #103
Closed
utaal
started this conversation in
Language design
Autoview
#103
Replies: 1 comment
-
We discussed this in the group meeting. The consensus is to go ahead and only use the implementation of the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I think we should make autoview behave more like Rust's autoderef. With the current autoview, if the following is defined for
Vec<T>
and
Seq<T>
hasThen, in a
#[spec]
context, whenv: Vec<T>
, thenv.len()
is automatically desugared tov.view().len()
.I propose two changes. The first, I think, uncontroversial.
1.
View
traitDefine a new
View
trait that declares thefn view(&self) -> T
function, instead of depending on the compiler finding a function namedview
in the impl.For the
Vec<T>
example,autoview(&self)
should be replaced with:2. Autoview functions not tagged with
#[autoview]
We should also consider to autoview functions that don't have an exec/proof counterpart, e.g.
Now,
len
is defined onSeq<int>
, but not onTree
, as it wouldn't make sense there.When
t: Tree
, I think we should still automatically desugart.len()
tot.view().len()
(ort.index(idx)
tot.view().index(idx)
) in a#[spec]
context. This behaviour would match that of rust's autoderef.API discoverability, potential for ambiguity
There may be a concern that it's harder to find the definition of autoviewed functions. However, the
View
trait impl for a type immediately points to the view type (Viewed
), making it trivial to lookup (in the fullness of time, we could highlight it in the emitted rustdoc).Additionally, because autoview is only applied in spec contexts, where calling proof or exec functions isn't possible, there's no risk of confusion for the user: they can expect every call to be to either a spec function on the concrete type, or a spec function on the view. In case a spec function with the same name is present both in the concrete type, and the view type (which we should discourage), we would never autoview that function and require the user to write
t.view().func()
.Convenience
In implementing the binary tree functions for chapter 1, exercise 22 of the summer school, https://github.com/secure-foundations/verus/blob/main/source/rust_verify/example/summer_school/chapter-1-22.rs#L77-L222 @jonhnet and I found that it was always natural to think of the tree as its view (a Seq) in spec contexts, and we would have liked to be able to leave off the call to
.view()
for.index(idx)
,.last()
, and.len()
. It never felt ambiguous whether we would be calling functions on theTree
or viewSeq<int>
because it was obvious which functions related to a sequence instead of a tree.In this example, it would have been pretty burdensome to get this behavior with the current restrictions. We would have needed implementation stubs on
Tree
forindex
,len
, andlast
(and any other function onSeq
we may have wanted to call.Reuse
Removing the restriction that a function exists on the concrete type also facilitates reuse of the types used as views, like in the
Tree
->Seq<int>
example above. A user can pick an existing type for theViewed
view type without having to worry about the interactions with the functions in the concrete type.Traits that support Rust features
With the current rules for autoview, I believe it's also awkward to support traits that support Rust features such as
Index
. I think it makes perfect sense, fort: Tree
, to be able to writet[0]
in a spec context to access the first element of its Seq representation; but that would currently require more boilerplate:With the proposed rules, this impl block is not needed.
Beta Was this translation helpful? Give feedback.
All reactions