0

I am trying to verify my code in Dafny and I encountered a problem: I have a method that is iterating over a sequence and changes it. The method changes the sequence according to the elements in the sequence. I would like to add a post condition like this: "if the elements in the sequence are X then something should happen". The problem is that the method changes the set (adds element etc.) and I want to check the condition of the original sequence. Is there an elegant way of doing that in Dafny? (The only way I could think of right now is keeping global var of the original condition of the sequence, but I am looking for the right way of doing that).

Code example:

method changeSeq(p: class1, s: seq<class1>)
ensures |s| == 10 ==> p in s
{
    if (|s| == 10){
        s := s + [p];
    }
}

In the code, I want the post condition to check original s stat, and not its stat after we changed it.

Ariel B
  • 31
  • 1

1 Answers1

3

you can use old for old value of a variable like s == old(s).

Here is one example: http://rise4fun.com/Dafny/fhQgD

From Dafny Documentation 22.18. Old Expressions

OldExpression_ = "old" "(" Expression(allowLemma: true, allowLambda: true) ")"

An old expression is used in postconditions. old(e) evaluates to the value expression e had on entry to the current method. Note that old only affects heap dereferences, like o.f and a[i]. In particular, old has no effect on the value returned for local variables or out-parameters.

Jon
  • 9,156
  • 9
  • 56
  • 73
deLta
  • 561
  • 2
  • 12
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/15027572) – Abhishek Pandey Jan 28 '17 at 09:33
  • How can you say that this does not provide answer? I understand the problem and think that this the correct answer. – deLta Jan 28 '17 at 15:19
  • Is there some documentation for *old* which you could link to? – Jon Jan 28 '17 at 19:56
  • Unfortunately I couldn't find any documentation. I came across it via example of Dafny codes. So Here is one that might help understand it: http://rise4fun.com/Dafny/fhQgD – deLta Jan 28 '17 at 20:22
  • @Jonathan727 `old()` is documented briefly in section 22.18 of the [Dafny Reference Manual](https://github.com/Microsoft/dafny/blob/master/Docs/DafnyRef/out/DafnyRef.pdf). You can find examples in the [Dafny test suite](https://github.com/Microsoft/dafny/tree/master/Test) by searching for "old". For a good, moderately complex example, see [dafny1/Queue.dfy](https://github.com/Microsoft/dafny/blob/master/Test/dafny1/Queue.dfy). – James Wilcox Aug 07 '17 at 19:07