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

Fine-grained frame expressions for arrays #1590

Open
RustanLeino opened this issue Nov 15, 2021 · 0 comments
Open

Fine-grained frame expressions for arrays #1590

RustanLeino opened this issue Nov 15, 2021 · 0 comments
Labels
kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny

Comments

@RustanLeino
Copy link
Collaborator

Frames (modifies and reads clauses) in Dafny usually operate at the granularity of objects. For example,

modifies obj

says that the enclosing method is allowed to modify the state of object obj (that is, any field of obj may be modified). But Dafny also has syntax that allow the frame to be specific about which fields of an object can be modified/read. That syntax uses a back-tick:

`

For example,

modifies obj`f

says that the enclosing method is allowed to modify the f field of object obj.

Missing in the language is the ability to do the same with the elements of an array. I propose extending frame expressions to also allow the back-tick syntax for arrays in modifies/reads clauses. For example, for a 1-dimensional array reference arr and a 2-dimensional array reference m,

modifies arr`[5]  // element 5 of arr may be modified
modifies m`[20, 30]  // element 20,30 of m may be modified
modifies arr`[..n]  // the first n elements of arr may be modified
modifies arr`[k..]`  // any element of arr with index k or higher may be modified
modifies arr`[k..n]  // any element of arr with index from k to n may be modified

In other words, what follows the back-tick in square brackets are legal indices/ranges of arrays today.

With the proposed syntax, a method

method AssignRange<X>(arr: array<X>, lo: int, hi: int, x: X)
  requires 0 <= lo <= hi <= arr.Length
  modifies arr
  ensures forall i :: 0 <= i < lo ==> arr[i] == old(arr[i])
  ensures forall i :: lo <= i < hi ==> arr[i] == x
  ensures ofrall i :: hi <= i < arr.Length ==> arr[i] == old(arr[i])

can more straightforwardly be specified by

method AssignRange<X>(arr: array<X>, lo: int, hi: int, x: X)
  requires 0 <= lo <= hi <= arr.Length
  modifies arr`[lo..hi]
  ensures forall i :: lo <= i < hi ==> arr[i] == x
@RustanLeino RustanLeino added the kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny label Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny
Projects
None yet
Development

No branches or pull requests

1 participant