'returns' clause for exec functions #851
Replies: 4 comments 4 replies
-
FWIW, Dafny has a similar concept, except in the other direction. It's called a "function-by-method" and you attach the method implementation to the function (a.k.a. spec).
Seems reasonable to make it automatic, unless we can think of an example where the programmer would accidentally use the exec function in a spec and expect a warning/error instead of silently using the |
Beta Was this translation helpful? Give feedback.
-
This seems very nice. Is it possible to do away with the To answer your questions:
|
Beta Was this translation helpful? Give feedback.
-
If I were writing the ensures clause manually, I would tend to write it as:
I think upcasting the result to |
Beta Was this translation helpful? Give feedback.
-
Closely related proposals: dual-mode functions ( Inspirationfn foo(x: bool, y: bool) -> Vec<bool> {
vec![x, y]
} For Rust functions such as the one above, there is essentially only one reasonable specification for it. As a practical example, consider combinators of
|
Beta Was this translation helpful? Give feedback.
-
Verus currently has
when_used_as_spec
which lets you use an exec function in spec contexts. This is particularly useful for functions likeu64::wrapping_add
orOption::unwrap
orVec::len
- pure functions that have clear uses in both spec code and exec code.Unfortunately, it can be a little cumbersome to use. It also has the unexpected behavior that the exec function doesn't have to agree with the spec function.
I propose incorporating this into the verus signature syntax. For example:
In general:
returns P
is equivalent toensures return_value == P
. This is checked like an ordinary ensures clause.returns
clause can be used in spec position where of course it has the meaning of the expressionP
.In other words, the above is roughly equivalent to:
Even without the second bullet, I think the
returns
clauses is a cleaner way to write some specs.Questions:
Should the when_used_as_spec effect be automatic, or should it require an additional annotation?
Should the 'requires' clause of the exec function become a 'recommends' clause on the spec function?
Beta Was this translation helpful? Give feedback.
All reactions