-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdges.dfy
365 lines (265 loc) · 11 KB
/
Edges.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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// edges
//////////////////////////////////////////////////////////////////////////////
//
// some computations work with sets of edges, instead of sets of objects
// methods like edges(oo:set<Object>) or incomingEdges(o:Object)
// "generate" the edge-sets as needed
//
// one edge is pretty much a named tuple for a single edge
// f=from object, n=field name in f, t=to object
datatype Edge = Edge(f : Object, n : string, m : Mode, t : Object)
//messier than I would like...
//all edges generated by os.
//destintaions not necessarily in os
function edges(os : set<Object>) : (r : set <Edge>)
// reads (set o <- os, o2 <- o.ValidReadSet() :: o2)
// requires forall o <- os :: o.Ready() && o.Valid()
// reads os`fields, os`fieldModes
reads os
{
set o <- os, n <- o.fields
| n in o.fieldModes
:: Edge(o, n, o.fieldModes[n], o.fields[n])
}
/*opaque*/ function edge(o : Object, n : string) : (e : Edge) //set or singleton?
reads o
requires n in o.fieldModes && n in o.fields
ensures e == Edge(o, n, o.fieldModes[n], o.fields[n])
{
Edge(o, n, o.fieldModes[n], o.fields[n])
}
function edgesFromObjectFields(o : Object, ns : set<string>) : (r : set <Edge>)
reads o
{
set n <- ns
| n in o.fieldModes && n in o.fields
:: Edge(o, n, o.fieldModes[n], o.fields[n])
}
function edgesViaEFO(os : set<Object>) : (r : set <Edge>)
reads os
{ set o <- os, e <- edgesFromObject(o) :: e }
function edgesFromObject(o : Object) : (r : set <Edge>)
// ensures r == edges({o})
reads o
{
set n <- o.fields
| n in o.fieldModes // && n in o.fields
:: Edge(o, n, o.fieldModes[n], o.fields[n])
}
//standalone lemma saying what edges does.
//aim is to avoid cluttering Dafny with crud every time we use "edges"
//but to have all the stuff around if needed
//HMMM: ObjctsToEdges below seems more concise & more useful...
lemma {:foo} edgesWork(os : set<Object>, r : set <Edge>)
requires forall o <- os :: o.Ready() && o.Valid() //DO I want this or not? or a separate lemma
requires r == edges(os)
ensures forall edge <- r :: edge.n in edge.f.fields && edge.f.fields[edge.n] == edge.t
ensures (os == {}) ==> (r == {})
ensures forall o <- os, n <- o.fields :: (Edge(o, n, o.fieldModes[n], o.fields[n]) in r)
ensures forall e <- edges(os) :: (e.f in os) && (e.n in e.f.fields) && (e.m == e.f.fieldModes[e.n]) && (e.t == e.f.fields[e.n])
//ensures OutgoingReferencesAreInTheseObjects(os) ==> AllThoseEdgesAreWthinTheseObjects(os,r)
ensures forall edge <- r :: (edge.f in os)
ensures OutgoingReferencesAreInTheseObjects(os) ==> (forall edge <- r :: (edge.t in os))
ensures OutgoingReferencesAreInTheseObjects(os) ==> (e2o(r) <= os)
{}
lemma {:foo} edgesWorks2(os : set<Object>, es : set <Edge>)
requires forall o <- os :: o.Ready() && o.Valid()
requires es == edges(os)
{
edgesWork(os,es);
var incoming := partitionedIncomingEdges(es);
assert forall o <- os, n <- o.fields :: (Edge(o, n, o.fieldModes[n], o.fields[n]) in incoming[ o.fields[n] ]);
assert forall t <- incoming.Keys, e : Edge <- incoming[t] ::
(e.f in os) && (e.n in e.f.fields) && (e.m == e.f.fieldModes[e.n]) && (e.t == e.f.fields[e.n]);
}
predicate ObjectsToEdges(os : set<Object>, es : set<Edge>)
reads os + (set o <- os, v <- o.ValidReadSet() :: v) + (set o <- os, v <- o.fields.Values :: v) + (set e <- es :: e.f) + (set e <- es :: e.t)
requires forall o <- os :: o.Ready() && o.Valid() //DO I want this or not? or a separate lemma
// requires es == edges(os)
{
&& (forall e <- es :: e.n in e.f.fields && e.f.fields[e.n] == e.t)
&& ((os == {}) ==> (es == {}))
&& (forall o <- os, n <- o.fields :: (Edge(o, n, o.fieldModes[n], o.fields[n]) in es))
&& (forall e <- es :: (e.f in os) && (e.n in e.f.fields) && (e.m == e.f.fieldModes[e.n]) && (e.t == e.f.fields[e.n]))
}
lemma ObjectsToEdgesEquals(os : set<Object>, es1 : set<Edge>, es2 : set<Edge>)
requires forall o <- os :: o.Ready() && o.Valid()
requires ObjectsToEdges(os,es1)
requires ObjectsToEdges(os,es2)
ensures es1 == es2
{}
function incomingModesEdges(es : set<Edge>) : (rv : set<Mode>)
{
set e <- es :: e.m
}
predicate edgesAreConsistentWithDafnyHeap(es : set<Edge>)
// reads set e <- es, x <- {e.f, e.t} :: x
reads set e <- es :: e.f
//reads set e <- es :: e.t //do I need this
{
true
}
function outgoingEdges(f : Object, edges : set<Edge>) : (rs : set<Edge>)
ensures rs <= edges
{
set e <- edges | e.f == f
}
function incomingEdges(t : Object, edges : set<Edge>) : (rs : set<Edge>)
ensures rs <= edges
{
set e <- edges | e.t == t
}
function refCountEdges(t : Object, edges : set<Edge>) : nat
{
| incomingEdges(t, edges) |
}
lemma fieldEdgesAreOutgoing(os : set<Object>)
requires forall o <- os :: o.Ready() && o.Valid()
ensures
var edges := edges(os);
(forall e <- edges :: e.f.fields[e.n] == e.t) &&
(forall o <- os, n <- o.fields :: Edge(o, n, o.fieldModes[n], o.fields[n]) in edges)
{}
lemma edgesFromDisjointObjects(aa : set<Object>, bb : set<Object>)
requires forall o <- (aa + bb) :: o.Ready() && o.Valid()
requires aa !! bb
ensures
edges(aa + bb) == edges(aa) + edges(bb)
{}
lemma edgesFromWholeSetOfSetsOfDisjointObjects(ooo : set<set<Object>>)
requires forall oo <- ooo, o <- oo :: o.Ready() && o.Valid()
requires forall aa <- ooo, bb <- ooo :: aa !! bb
ensures
edges(set oo <- ooo, o <- oo :: o)
== (set oo <- ooo, e <- edges(oo) :: e)
== (set oo <- ooo, o <- oo, e <- edges({o}) :: e)
{}
//all objects FROM whcih edges leave in the edge-set
function {:todo "horible name"} FromObjectsEdges(es : set<Edge>) : set<Object> {
set e <- es :: e.f
}
lemma fewerEdgesLowerRefCounts(fewer : set<Edge>, extra : set<Edge>, os : set<Object>)
requires fewer !! extra
ensures forall o <- os :: refCountEdges(o, fewer) <= refCountEdges(o, fewer + extra)
{
RefCountDistributesOverDisjointEdges(os, fewer, extra);
}
lemma fewerEdgesFewerIncomers(fewer : set<Edge>, extra : set<Edge>, os : set<Object>)
requires fewer !! extra
ensures forall o <- os :: incomingEdges(o, fewer) <= incomingEdges(o, fewer + extra)
{
RefCountDistributesOverDisjointEdges(os, fewer, extra);
}
lemma fewerEdgesFewerOutgoings(fewer : set<Edge>, extra : set<Edge>, os : set<Object>)
requires fewer !! extra
ensures forall o <- os :: outgoingEdges(o, fewer) <= outgoingEdges(o, fewer + extra)
{
RefCountDistributesOverDisjointEdges(os, fewer, extra);
}
//kjx need to sort out API , is it he fewer + extra, or fewer vs more?
//ie are they disjoint, or what...
//os comes first or last
lemma fewerEdgesPreservesShit(fewer : set<Edge>, extra : set<Edge>, os : set<Object>)
requires fewer !! extra
// ensures forall o <- os :: refCountEdges(o, fewer) <= refCountEdges(o, fewer + extra)
{
RefCountDistributesOverDisjointEdges(os, fewer, extra);
}
function externalEdges(o: Region, edges : set<Edge>) : (rs : set<Edge>)
ensures rs <= edges
reads e2o(edges)
{
set e <- edges | e.t.region == o && e.f.region != o
}
//abbrev for heapExternalEdgesPartitionedByRegion - which I don't need right now
function HxR(edges : set<Edge>) : map<Region,set<Edge>>
reads e2o(edges)
{
heapExternalEdgesPartitionedByRegion(edges)
}
function heapExternalEdgesPartitionedByRegion(edges : set<Edge>) : map<Region,set<Edge>>
reads e2o(edges)
{
var heapExternalEdges := justHeapExternalEdges(edges);
var allRelevantHeapRegions := set he <- heapExternalEdges :: he.t.region;
map r <- allRelevantHeapRegions :: externalEdges(r, heapExternalEdges)
}
//or should this be "allExternalEdges"
//went with "just" with the idea "justX(a):r" means to filter ---- ensures r <= a
//meanwhile allX(a):r can do move - e.g. map all objects to all owning "regions"
///*opaque*/
function justHeapExternalEdges(edges : set<Edge>) : (rs : set<Edge>)
ensures rs <= edges
ensures (set e <- rs :: e.t.region) <= (set e <- edges :: e.t.region)
ensures (forall e <- rs :: e.f.region != e.t.region)
ensures (forall e <- rs :: e.t.region.Heap?)
ensures (forall e <- edges :: (((e.f.region != e.t.region) && (e.t.region.Heap?))
==> (e in rs)))
ensures (forall e <- rs :: e in edges)
ensures (forall e <- edges :: (((e.f.region != e.t.region) && (e.t.region.Heap?))))
==> (edges == rs)
ensures (forall e <- edges :: (e.f.region == e.t.region))
==> (rs == {})
// reads e2o(edges)`region
{
set e <- edges | e.f.region != e.t.region && e.t.region.Heap?
}
predicate e2oRV(edges : set<Edge>)
// reads (set o <- e2o(edges), o2 <- o.ValidReadSet() :: o2)
reads var ee := set o <- e2o(edges); ee`fields
reads var ee := set o <- e2o(edges); ee`fieldModes
{
forall e <- edges :: e.f.Ready() && e.f.Valid()
&& e.t.Ready() && e.t.Valid()
}
function e2o(edges : set<Edge>) : (rx : set<Object>)
{
(set e <- edges :: e.f) + (set e <- edges :: e.t)
}
//should this be called segde? -it's pretty much the inverse of edges. kinda.
lemma e2oMonotonic(less : set<Edge>, more : set<Edge>)
requires less <= more
ensures e2o(less) <= e2o(more)
ensures (set o <- e2o(less), o2 <- o.ValidReadSet() :: o2) <= (set o <- e2o(more), o2 <- o.ValidReadSet() :: o2)
{}
lemma o2e2o(os : set<Object>, es : set<Edge>)
requires es == edges(os)
requires forall o <- os :: o.Ready() && o.Valid() //hmm shojld that be a precondition or antecedent?
// requires forall e <- es :: e.f in os
// ensures forall e <- es :: e.f in os
ensures OutgoingReferencesAreInTheseObjects(os) ==> (e2o(es) <= os)
ensures OutgoingReferencesAreInTheseObjects(os) ==> (forall o <- e2o(es) :: o.Ready() && o.Valid())
{
assert forall e <- es :: e.f in os;
assert OutgoingReferencesAreInTheseObjects(os) ==> forall e <- es :: e.t in os;
}
//need to describe this properly
method LosingMyEdge(os : set<Object>, o : Object, n : string)
returns (e : Edge, es1 : set<Edge>, es2 : set<Edge>)
requires forall o <- os :: o.Ready() && o.Valid()
requires o in os
requires n in o.fields
modifies o`fields
ensures forall o <- os :: o.Ready() && o.Valid()
ensures es1 == old(edges(os))
ensures old(ObjectsToEdges(os,es1))
ensures es2 == edges(os)
ensures ObjectsToEdges(os,es2)
ensures o.fields == old(RemoveKey(o.fields,n))
ensures e == old(edge(o,n))
ensures es2 == es1 - {e}
ensures n !in o.fields.Keys
{
es1 := edges(os);
assert ObjectsToEdges(os,es1);
e := edge(o,n);
o.fields := RemoveKey(o.fields,n);
assert forall o <- os :: o.Ready() && o.Valid();
es2 := edges(os);
assert ObjectsToEdges(os,es2);
}