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

Counterexamples involving seqs should present elements ordered by index #2975

Merged

Conversation

dijkstracula
Copy link
Contributor

@dijkstracula dijkstracula commented Nov 2, 2022

I'm working on debugging a particular counterexample involving a sequence (at least in theory - hence the debugging!) of contiguous intervals, like [(0, 4), (5, 9), (10, 20)]. In this case, figuring out where I'm going wrong really means needing to look at sequence elements in the order that they appear. This is the counterexample I'm given (extracted to the clipboard with this IDE patch):

At "{" (file:https:///Users/ntaylor/school/smrt/src/Disks/ZonedDisk.dfy:131):
...
    @1:seq<_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64>> = (Length := 6286, [0] := @3, [6285] := @4, [6283] := @5, [6284] := @6)
    @3:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 0, _1 := 2448)
    @4:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 281, _1 := 6135)
    @5:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 2480, _1 := 2481)
    @6:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 6133, _1 := 6134)

The particulars of the counterexample aren't of interest, save the indices that Boogie/Z3 generates for me in the sequence itself. Only after awhile did I notice that the sequence in the counterexample is actually out of order: @1's indices are 0, 6285, 6283, 6284 (Either it's a coincidence or Boogie is giving us the initial and final elements first?). I would have expected those indices to be in ascending order (i.e. 0, 6283, 6284, 6285) and would expect the intermediate subexpressions to be ordered similarly.

This patch modifies how the Dafny model expands element variables so that index-by-index sequence generation is sorted:

At "{" (file:https:///Users/ntaylor/school/smrt/src/Disks/ZonedDisk.dfy:131):
...
    @1:seq<_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64>> = (Length := 6286, [0] := @3, [6283] := @4, [6284] := @5, [6285] := @6)
    @3:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 0, _1 := 2448)
    @4:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 2480, _1 := 2481)
    @5:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 6133, _1 := 6134)
    @6:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 281, _1 := 6135)

I'm not exactly sure of the current behaviour in the "build operator" case, and so I left that code branch unmodified.

(While I was in there, I found an incorrect docstring in an unrelated function related to passing null as an argument, and correspondingly fixed that.)

Fixes #

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Copy link
Member

@MikaelMayer MikaelMayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic, yes, that seems like a good fix.
Can you add something for RELEASE_NOTES.md? See the instructions in the file.

I'm working on debugging a particular counterexample involving a sequence of
(at least in theory - hence the debugging!) contiguous intervals, like `[(0,
4), (5, 9), (10, 20)]`.  In this case, figuring out where I'm going wrong
really means needing to look at sequence elements in the order that they
appear.  This is the counterexample I'm given:

```
At "{" (file:https:///Users/ntaylor/school/smrt/src/Disks/ZonedDisk.dfy:131):
...
    @1:seq<_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64>> = (Length := 6286, [0] := @3, [6285] := @4, [6283] := @5, [6284] := @6)
    @3:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 0, _1 := 2448)
    @4:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 281, _1 := 6135)
    @5:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 2480, _1 := 2481)
    @6:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 6133, _1 := 6134)
```

The particulars of the counterexample aren't of interest, save the indies that
Boogie/Z3 generates for me.  Only after awhile did I notice that the sequence
in the counterexample is actually out of order: `@1`'s indices are `0, 6285,
6283, 6284` (Either it's a coincidence or Boogie is giving us the initial and
final elements first?).  I would have expected those indices to be in ascending
order (i.e. `0, 6283, 6284, 6285`) and would expect the intermediate
subexpressions to be ordered similarly.

This patch modifies how the Dafny model expands element variables so that
index-by-index sequence generation is sorted:

```
At "{" (file:https:///Users/ntaylor/school/smrt/src/Disks/ZonedDisk.dfy:131):
...
    @1:seq<_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64>> = (Length := 6286, [0] := @3, [6283] := @4, [6284] := @5, [6285] := @6)
    @3:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 0, _1 := 2448)
    @4:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 2480, _1 := 2481)
    @5:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 6133, _1 := 6134)
    @6:_System.Tuple2<NativeTypes.uint64, NativeTypes.uint64> = _#Make2(_0 := 281, _1 := 6135)
```

I'm not exactly sure of the current behaviour in the "build operator" case, and
so I left that code branch unmodified.
Copy link
Member

@MikaelMayer MikaelMayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks for this feature !

@MikaelMayer MikaelMayer enabled auto-merge (squash) November 3, 2022 19:31
@MikaelMayer MikaelMayer merged commit 6a18ce5 into dafny-lang:master Nov 3, 2022
@dijkstracula dijkstracula deleted the nathan/ordered_seq_indices branch November 3, 2022 19:41
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

2 participants