-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsr.dfy
416 lines (383 loc) · 15 KB
/
csr.dfy
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
predicate ValidCSRIndex(indices: seq<int>, indptr: seq<int>)
{
|indptr| >= 1 &&
indptr[0] == 0 &&
indptr[|indptr| - 1] == |indices| &&
(forall i :: 0 <= i < |indices| ==> 0 <= indices[i]) &&
(forall i :: 0 <= i < |indptr| - 1 ==> 0 <= indptr[i] <= |indices|) &&
(forall i, j :: 0 <= i < j < |indptr| ==> indptr[i] <= indptr[j])
}
predicate Unique(indices: seq<int>, indptr: seq<int>)
requires ValidCSRIndex(indices, indptr)
{
forall row :: 0 <= row < |indptr|-1 ==> forall i,j :: indptr[row] <= i < j < indptr[row+1] ==> indices[i] != indices[j]
}
predicate method Canonical(indices: seq<int>, indptr: seq<int>)
requires ValidCSRIndex(indices, indptr)
{
forall row :: 0 <= row < |indptr|-1 ==> forall i,j :: indptr[row] <= i < j < indptr[row+1] ==> indices[i] < indices[j]
}
function getX(indices: seq<int>, indptr: seq<int>, j: int) : int
requires ValidCSRIndex(indices, indptr)
requires 0 <= j < |indices|
decreases |indptr|
{
if |indptr| == 2 then 0 else (
if j >= indptr[|indptr| - 2] then |indptr| - 2
else getX(indices[..indptr[|indptr|-2]], indptr[..|indptr|-1], j)
)
}
lemma XIsUniqueAndInBounds(indices: seq<int>, indptr: seq<int>, j: int, x: int)
requires ValidCSRIndex(indices, indptr)
requires 0 <= j < |indices|
requires x == getX(indices, indptr, j)
decreases |indptr|
ensures 0 <= x <= |indptr| - 2
ensures indptr[x] <= j < indptr[x+1]
ensures forall x1 :: 0 <= x1 < x ==> j >= indptr[x1+1]
ensures forall x1 :: x < x1 <= |indptr|-2 ==> j < indptr[x1]
ensures forall x1 :: 0 <= x1 <= |indptr| - 2 ==> ((indptr[x1] <= j < indptr[x1+1]) <==> (x1 == x))
{
if |indptr| == 2
{
assert x == 0;
assert 0 <= x <= |indptr| - 2;
assert indptr[x] == 0 <= j;
assert indptr[x+1] == indptr[|indptr| - 1] == |indices| > j;
}
else
{
if j >= indptr[|indptr| - 2]
{
assert x == |indptr| - 2;
assert 0 <= x <= |indptr| - 2;
assert indptr[x+1] == indptr[|indptr| - 1] == |indices| > j;
}
else
{
var head_indices, head_indptr := indices[..indptr[|indptr|-2]], indptr[..|indptr|-1];
var head_x := getX(head_indices, head_indptr, j);
assert x == head_x;
XIsUniqueAndInBounds(head_indices, head_indptr, j, head_x);
}
}
}
lemma JBoundsDetermineX(indices: seq<int>, indptr: seq<int>, j: int, x: int)
requires ValidCSRIndex(indices, indptr)
requires 0 <= j < |indices|
requires 0 <= x <= |indptr| - 2
requires indptr[x] <= j < indptr[x+1]
decreases |indptr|
ensures getX(indices, indptr, j) == x
{
if |indptr| == 2
{
assert x == 0;
assert 0 <= x <= |indptr| - 2;
assert indptr[x] == 0 <= j;
assert indptr[x+1] == indptr[|indptr| - 1] == |indices| > j;
}
else
{
if j >= indptr[|indptr| - 2]
{
assert x == |indptr| - 2;
assert 0 <= x <= |indptr| - 2;
assert indptr[x+1] == indptr[|indptr| - 1] == |indices| > j;
}
else
{
var head_indices, head_indptr := indices[..indptr[|indptr|-2]], indptr[..|indptr|-1];
var head_x := getX(head_indices, head_indptr, j);
JBoundsDetermineX(head_indices, head_indptr, j, head_x);
assert x == head_x;
}
}
}
function getY(indices: seq<int>, indptr: seq<int>, j: int) : int
requires ValidCSRIndex(indices, indptr)
requires 0 <= j < |indices|
{
indices[j]
}
function getJs(indices: seq<int>, indptr: seq<int>, x: int, y: int) : set<int>
requires ValidCSRIndex(indices, indptr)
{
set j | 0 <= j < |indices| && getX(indices, indptr, j) == x && getY(indices, indptr, j) == y
}
predicate JExists(indices: seq<int>, indptr: seq<int>, x: int, y: int)
requires ValidCSRIndex(indices, indptr)
requires Unique(indices, indptr)
{
|getJs(indices, indptr, x, y)| == 1
}
function DataAt(data: seq<int>, indices: seq<int>, indptr: seq<int>, x: int, y: int): set<int>
requires |data| == |indices|
requires ValidCSRIndex(indices, indptr)
requires Unique(indices, indptr)
{
set j | j in getJs(indices, indptr, x, y) :: data[j]
}
lemma XYUniqueInUniqueMatrix(indices: seq<int>, indptr: seq<int>, j1: int, j2: int)
requires ValidCSRIndex(indices, indptr)
requires Unique(indices, indptr)
requires 0 <= j1 < |indices|
requires 0 <= j2 < |indices|
requires getX(indices, indptr, j1) == getX(indices, indptr, j2)
requires getY(indices, indptr, j1) == getY(indices, indptr, j2)
ensures j1 == j2
{
if j1 != j2
{
var x := getX(indices, indptr, j1);
XIsUniqueAndInBounds(indices, indptr, j1, x);
XIsUniqueAndInBounds(indices, indptr, j2, x);
// Proof by contradiction (Dafny verifies even without this, but it is included for completeness)
assert indptr[x] <= j1 < indptr[x+1];
assert indptr[x] <= j2 < indptr[x+1];
if j1 < j2
{
assert indptr[x] <= j1 < j2 < indptr[x+1] ==> indices[j1] != indices[j2];
}
else
{
assert indptr[x] <= j2 < j1 < indptr[x+1] ==> indices[j2] != indices[j1];
}
assert getY(indices, indptr, j1) == indices[j1] != indices[j2] == getY(indices, indptr, j2);
assert false;
}
}
lemma JUniqueInUniqueMatrix(indices: seq<int>, indptr: seq<int>, x: int, y: int, j: int)
requires ValidCSRIndex(indices, indptr)
requires Unique(indices, indptr)
requires 0 <= j < |indices|
requires getX(indices, indptr, j) == x
requires getY(indices, indptr, j) == y
ensures getJs(indices, indptr, x, y) == {j}
{
var jset := getJs(indices, indptr, x, y);
if jset != {j}
{
assert exists j2 :: j2 in jset && j2 != j;
forall j2 | j2 in jset && j2 != j
ensures false
{
XYUniqueInUniqueMatrix(indices, indptr, j, j2);
}
}
}
lemma DataUniqueInUniqueMatrix(data: seq<int>, indices: seq<int>, indptr: seq<int>, x: int, y: int, j: int)
requires ValidCSRIndex(indices, indptr)
requires Unique(indices, indptr)
requires 0 <= j < |indices|
requires |data| == |indices|
requires getX(indices, indptr, j) == x
requires getY(indices, indptr, j) == y
ensures DataAt(data, indices, indptr, x, y) == {data[j]}
{
JUniqueInUniqueMatrix(indices, indptr, x, y, j);
}
lemma JExistenceConditionForGivenXY(indices: seq<int>, indptr: seq<int>, ncols: int, x: int, y: int)
requires ValidCSRIndex(indices, indptr)
requires Unique(indices, indptr)
requires 0 <= x < |indptr| - 1
ensures JExists(indices, indptr, x, y) <==> y in indices[indptr[x]..indptr[x+1]];
{
if y in indices[indptr[x]..indptr[x+1]]
{
forall j | indptr[x] <= j < indptr[x+1] && indices[j] == y
ensures JExists(indices, indptr, x, y)
{
JBoundsDetermineX(indices, indptr, j, x);
assert getX(indices, indptr, j) == x;
JUniqueInUniqueMatrix(indices, indptr, x, y, j);
}
}
if JExists(indices, indptr, x, y)
{
forall j | j in getJs(indices, indptr, x, y)
ensures y in indices[indptr[x]..indptr[x+1]]
{
assert getX(indices, indptr, j) == x;
XIsUniqueAndInBounds(indices, indptr, j, x);
assert y in indices[indptr[x]..indptr[x+1]];
}
}
}
lemma JExistenceConditionForGivenX(indices: seq<int>, indptr: seq<int>, ncols: int, x: int)
requires ValidCSRIndex(indices, indptr)
requires Unique(indices, indptr)
requires 0 <= x < |indptr| - 1
ensures forall y :: 0 <= y < ncols ==>
(JExists(indices, indptr, x, y) <==> y in indices[indptr[x]..indptr[x+1]])
{
forall y | 0 <= y < ncols
ensures (JExists(indices, indptr, x, y) <==> y in indices[indptr[x]..indptr[x+1]])
{
if y in indices[indptr[x]..indptr[x+1]]
{
forall j | indptr[x] <= j < indptr[x+1] && indices[j] == y
ensures JExists(indices, indptr, x, y)
{
JBoundsDetermineX(indices, indptr, j, x);
assert getX(indices, indptr, j) == x;
JUniqueInUniqueMatrix(indices, indptr, x, y, j);
}
}
if JExists(indices, indptr, x, y)
{
forall j | j in getJs(indices, indptr, x, y)
ensures y in indices[indptr[x]..indptr[x+1]]
{
assert getX(indices, indptr, j) == x;
XIsUniqueAndInBounds(indices, indptr, j, x);
assert y in indices[indptr[x]..indptr[x+1]];
}
}
}
}
lemma JExistenceCondition(indices: seq<int>, indptr: seq<int>, ncols: int)
requires ValidCSRIndex(indices, indptr)
requires Unique(indices, indptr)
ensures forall x :: 0 <= x < |indptr| - 1 ==>
forall y :: 0 <= y < ncols ==>
(JExists(indices, indptr, x, y) <==> y in indices[indptr[x]..indptr[x+1]])
{
forall x | 0 <= x < |indptr| - 1
ensures forall y :: 0 <= y < ncols ==> (JExists(indices, indptr, x, y) <==> y in indices[indptr[x]..indptr[x+1]])
{
JExistenceConditionForGivenX(indices, indptr, ncols, x);
}
}
// Check if (indices1, indptr1) describes a partial CSR matrix of (indices2, indptr2) obtained
// by removing zero or more of the latter's final row(s).
predicate IsHeadMatrix(indices1: seq<int>, indptr1: seq<int>, indices2: seq<int>, indptr2: seq<int>)
requires ValidCSRIndex(indices1, indptr1)
requires ValidCSRIndex(indices2, indptr2)
{
|indptr1| <= |indptr2| &&
(forall i :: 0 <= i < |indptr1| ==> indptr1[i] == indptr2[i]) &&
(forall j :: 0 <= j < |indices1| ==> indices1[j] == indices2[j])
}
predicate IsHeadMatrixWithData (data1: seq<int>, indices1: seq<int>, indptr1: seq<int>, data2: seq<int>, indices2: seq<int>, indptr2: seq<int>)
requires ValidCSRIndex(indices1, indptr1)
requires |data1| == |indices1|
requires ValidCSRIndex(indices2, indptr2)
requires |data2| == |indices2|
{
IsHeadMatrix(indices1, indptr1, indices2, indptr2) &&
(forall j :: 0 <= j < |data1| ==> data1[j] == data2[j])
}
lemma AddingRowsPreservesExistingPositions(indices1: seq<int>, indptr1: seq<int>, indices2: seq<int>, indptr2: seq<int>)
requires ValidCSRIndex(indices1, indptr1)
requires ValidCSRIndex(indices2, indptr2)
requires IsHeadMatrix(indices1, indptr1, indices2, indptr2)
ensures forall j :: 0 <= j < |indices1| ==> getX(indices1, indptr1, j) == getX(indices2, indptr2, j)
ensures forall j :: 0 <= j < |indices1| ==> getY(indices1, indptr1, j) == getY(indices2, indptr2, j)
{
forall j | 0 <= j < |indices1|
ensures getX(indices1, indptr1, j) == getX(indices2, indptr2, j)
{
var x1, x2 := getX(indices1, indptr1, j), getX(indices2, indptr2, j);
XIsUniqueAndInBounds(indices1, indptr1, j, x1);
XIsUniqueAndInBounds(indices2, indptr2, j, x2);
assert 0 <= x2 <= |indptr1| - 2;
assert x1 == x2;
}
}
lemma AddingRowsPreservesJExists(indices1: seq<int>, indptr1: seq<int>, indices2: seq<int>, indptr2: seq<int>, ncols: int)
requires ValidCSRIndex(indices1, indptr1)
requires Unique(indices1, indptr1)
requires ValidCSRIndex(indices2, indptr2)
requires Unique(indices2, indptr2)
requires IsHeadMatrix(indices1, indptr1, indices2, indptr2)
ensures forall x, y :: 0 <= x < |indptr1| - 1 && 0 <= y < ncols ==> (
JExists(indices1, indptr1, x, y) <==> JExists(indices2, indptr2, x, y)
)
{
forall x, y | 0 <= x < |indptr1| - 1 && 0 <= y < ncols
ensures JExists(indices1, indptr1, x, y) <==> JExists(indices2, indptr2, x, y)
{
JExistenceConditionForGivenXY(indices1, indptr1, ncols, x, y);
JExistenceConditionForGivenXY(indices2, indptr2, ncols, x, y);
}
}
lemma AddingRowsPreservesExistingData(data1: seq<int>, indices1: seq<int>, indptr1: seq<int>, data2: seq<int>, indices2: seq<int>, indptr2: seq<int>, ncols: int)
requires ValidCSRIndex(indices1, indptr1)
requires Unique(indices1, indptr1)
requires |data1| == |indices1|
requires ValidCSRIndex(indices2, indptr2)
requires Unique(indices2, indptr2)
requires |data2| == |indices2|
requires IsHeadMatrixWithData(data1, indices1, indptr1, data2, indices2, indptr2)
ensures forall x, y :: 0 <= x < |indptr1| - 1 && 0 <= y < ncols ==> (
(JExists(indices1, indptr1, x, y) ==>
DataAt(data1, indices1, indptr1, x, y) == DataAt(data2, indices2, indptr2, x, y))
)
{
AddingRowsPreservesJExists(indices1, indptr1, indices2, indptr2, ncols);
forall x,y | 0 <= x < |indptr1| - 1 && 0 <= y < ncols
ensures (JExists(indices1, indptr1, x, y) ==>
DataAt(data1, indices1, indptr1, x, y) == DataAt(data2, indices2, indptr2, x, y))
{
if JExists(indices1, indptr1, x, y)
{
forall j | j in getJs(indices1, indptr1, x, y)
ensures DataAt(data1, indices1, indptr1, x, y) == DataAt(data2, indices2, indptr2, x, y)
{
AddingRowsPreservesExistingPositions(indices1, indptr1, indices2, indptr2);
DataUniqueInUniqueMatrix(data1, indices1, indptr1, x, y, j);
assert DataAt(data1, indices1, indptr1, x, y) == {data1[j]};
assert getX(indices2, indptr2, j) == x;
assert getY(indices2, indptr2, j) == y;
JUniqueInUniqueMatrix(indices2, indptr2, x, y, j);
assert DataAt(data2, indices2, indptr2, x, y) == {data2[j]};
}
}
}
}
class CSRMatrix {
var data: seq<int>
var indices: seq<int>
var indptr: seq<int>
var nrows: int
var ncols: int
constructor (data: seq<int>, indices: seq<int>, indptr: seq<int>, nrows: int, ncols: int)
requires |data| == |indices|
requires ValidCSRIndex(indices, indptr)
requires Unique(indices, indptr)
requires |indptr| == nrows + 1
requires 0 <= ncols
requires forall j :: 0 <= j < |indices| ==> indices[j] < ncols
ensures this.data == data
ensures this.indices == indices
ensures this.indptr == indptr
ensures this.nrows == nrows
ensures this.ncols == ncols
ensures Valid()
{
this.data := data;
this.indices := indices;
this.indptr := indptr;
this.nrows := nrows;
this.ncols := ncols;
JExistenceCondition(indices, indptr, ncols);
}
predicate Valid()
reads this
{
|data| == |indices| &&
ValidCSRIndex(this.indices, this.indptr) &&
Unique(this.indices, this.indptr) &&
RowColValid() &&
// Putting the existence condition in the predicate minimizes the need for repetitive assertions elsewhere,
// even if it can be proven using the above predicates.
forall x, y :: 0 <= x < |indptr| - 1 && 0 <= y < ncols ==>
(JExists(indices, indptr, x, y) <==> y in indices[indptr[x]..indptr[x+1]])
}
predicate RowColValid()
reads this
{
|indptr| == nrows + 1 && 0 <= ncols && forall j :: 0 <= j < |indices| ==> indices[j] < ncols
}
}