summaryrefslogtreecommitdiff
path: root/Test/dafny0/LoopModifies.dfy
blob: 7cf62e3273e218be315a8d0f20646c7cf6aa075a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// RUN: %dafny /compile:0 /print:"%t.print" /dprint:"%t.dprint" "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

// regular modifies sanity test:
method Testing1(a: array<int>)
   requires a != null && a.Length > 0;
{
   a[0] := 0; // ERROR
}

// array inside while loop, without explict modifies clause:
method Testing2(a: array<int>)
   requires a != null && a.Length > 0;
{
   var i := 0;
   while(i < 10) 
      invariant 0 <= i <= 10;
   {
      a[0] := i; // ERROR
      i := i + 1;
   }
}

// array inside while loop, without explict modifies clause:
method Testing2A(a: array<int>)
   requires a != null && a.Length > 0;
   modifies a;
{
   var i := 0;
   while(i < 10) 
      invariant 0 <= i <= 10;
   {
      // now there is no problem.
      a[0] := i;
      i := i + 1;
   }
}

// array inside while loop, with explict modifies clause:
method Testing3(a: array<int>)
   requires a != null && a.Length > 0;
{
   var i := 0;
   while(i < 10) 
      invariant 0 <= i <= 10;
      modifies {};
   {
      a[0] := i; // ERROR
      i := i + 1;
   }
}

// modifies restricts:
method Testing4(a: array<int>)
   requires a != null && a.Length > 0;
   modifies a;
{
   var i := 0;
   while(i < 10) 
      invariant 0 <= i <= 10;
      modifies {};
   {
      a[0] := i; // ERROR
      i := i + 1;
   }
   // should be ok.
   a[0] := 1;
}

// modifies not a subset:
method Testing5(a: array<int>)
   requires a != null && a.Length > 0;
   modifies {};
{
   var i := 0;
   while(i < 10) // ERROR
      invariant 0 <= i <= 10;
      modifies a;
   {
      a[0] := i;
      i := i + 1;
   }
}

// modifies is a subset, but modifications occur outside:
method Testing6(a: array<int>, b: array<int>)
   requires a != null && a.Length > 0;
   requires b != null && b.Length > 0;
   modifies a, b;
{
   var i := 0;
   while(i < 10) 
      invariant 0 <= i <= 10;
      modifies a;
   {
      // cool.
      a[0] := i;

      // not cool.
      b[0] := i; // ERROR
      i := i + 1;
   }
}

// heap outside modifies is actually preserved:
method Testing7(a: array<int>, b: array<int>)
   requires a != null && a.Length > 0;
   requires b != null && b.Length > 0;
   requires a != b;
   modifies a, b;
{
   var i := 0;
   b[0] := 4;
   while(i < 10) 
      invariant 0 <= i <= 10;
      modifies a;
   {
      // still good.
      a[0] := i;
      i := i + 1;
   }
   // this is new, and good:
   assert b[0] == 4;
}

// modifies actually restrict frame when nested:
method Testing8(a: array<int>, b: array<int>, c: array<int>)
   requires a != null && a.Length > 0;
   requires b != null && b.Length > 0;
   requires c != null && c.Length > 0;
   requires a != b && b != c && c != a;
   modifies a, b, c;
{
   var i := 0;
   b[0] := 4;
   while(i < 10) 
      invariant 0 <= i <= 10;
      modifies a, b;
   {
      var j := 0;
      while(j < 10) 
         invariant 0 <= j <= 10;
         modifies a;
      {
         // happy:
         a[0] := j;
         // not happy:
         b[0] := i; // ERROR
         j := j + 1;
      }
      i := i + 1;
   }
   c[0] := 1;
}

// heap outside modifies preserved when nested:
method Testing9(a: array<int>, b: array<int>, c: array<int>)
   requires a != null && a.Length > 0;
   requires b != null && b.Length > 0;
   requires c != null && c.Length > 0;
   requires a != b && b != c && c != a;
   modifies a, b, c;
{
   var i := 0;
   b[0] := 4;
   while(i < 10) 
      invariant 0 <= i <= 10;
      modifies a, b;
   {
      var j := 0;
      b[0] := i;
      while(j < 10) 
         invariant 0 <= j <= 10;
         modifies a;
      {
         a[0] := j;
         j := j + 1;
      }
      assert b[0] == i;
      i := i + 1;
   }
   c[0] := 1;
}

// allocation, fresh tests:

// allocated things not automatically in modifies of loops:
method Testing10(a: array<int>)
   requires a != null && a.Length > 0;
   modifies a;
{
   var i := 0;
   var arr := new int[1];
   arr[0] := 1; // good, even though not in method modifies.
   while(i < 10) 
      invariant 0 <= i <= 10;
      modifies a;
   {
      arr[0] := 1; // ERROR
      i := i + 1;
   }
}

// unless no modifies given, in which case the context is used:
method Testing10a(a: array<int>)
   requires a != null && a.Length > 0;
   modifies a;
{
   var i := 0;
   var arr := new int[1];
   arr[0] := 1; // still good.
   while(i < 10) 
      invariant 0 <= i <= 10;
   {
      arr[0] := 1; // no modifies, so allowed to touch arr.
      i := i + 1;
   }
}


// loop inherits modifies clause of enclosing loop, not method.
method Testing10b(a: array<int>, b: array<int>)
   requires a != null && a.Length > 0;
   requires b != null && b.Length > 0;
   requires a != b;
   modifies a,b;
{
   b[0] := 4;
   var j := 0;
   while (j < 10)
      modifies a;
   {
      var i := 1;
      while (i < 10)
      // note: no explicit modifies clause
      {
         a[0] := 1;
		 i := i + 1;
      }
      assert b[0] == 4; // should be fine.
	  j := j + 1;
   }
}

// arr still accessible after loop that can't touch it.
method Testing11()
{
   var i := 0;
   var arr := new int[1];
   while(i < 10) 
      invariant 0 <= i <= 10;
      modifies {};
   {
      arr := new int[1];
      arr[0] := 1;
      var j := 0;
      while(j < 10) 
         invariant 0 <= j <= 10;
         modifies {};
      {
         // can't touch arr in here.
         j := j + 1;
      }
      arr[0] := 2;
      i := i + 1;
   }
}

// allocation inside a loop is still ok:
method Testing11a(a: array<int>)
   requires a != null && a.Length > 0;
   modifies a;
{
   var i := 0;
   while(i < 10) 
      invariant 0 <= i <= 10;
      modifies a;
   {
      var arr := new int[1];
      arr[0] := 1; // can modify arr, even though it
                   // is not in modifies because it is fresh.
      var j := 0;
      while(j < 10) 
         invariant 0 <= j <= 10;
         modifies a;
      {
         arr[0] := 3; // ERROR: can't touch arr, as allocated before modifies captured.
         j := j + 1;
      }
      i := i + 1;
   }
}

class Elem
{
   var i: int;
}

// capture of modifies clause at beginning of loop:
method Testing12(a: Elem, b: Elem, c: Elem)
   requires a != null && b != null && c != null;
   requires a != b && b != c && c != a; // all different.
   modifies a, b, c;
{
   var i := 0;
   var S := {a, b, c};
   // S "captured" here for purposes of modifies clause.
   while(S != {})
      modifies S;
      decreases S;
   {
      var j :| j in S;
      // these still good, even though S shrinks to not include them.
      a.i := i;
      b.i := i;
      c.i := i;
      S := S - {j};
      i := i + 1;
   }
}