-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADT.dfy
262 lines (227 loc) · 6.76 KB
/
ADT.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
include "Collections/Set.dfy"
include "Collections/SetObject.dfy"
module ADT {
import opened Set
import opened SetObject
type pos = n: int | n > 0 witness 1
trait {:termination false} ADT<M> {
ghost const ReprDepth: pos
ghost function ReprFamily(n: nat): set<object>
decreases n
//ensures forall i, j | 0 <= i < j <= n :: ReprFamily(i) <= ReprFamily(j)
ensures n > 0 ==> ReprFamily(n) >= ReprFamily(n-1)
reads this, if n == 0 then {} else ReprFamily(n-1)
ghost function Repr(): set<object>
reads this, ReprFamily(ReprDepth-1)
{
ReprFamily(ReprDepth)
}
lemma ReprLemma()
ensures Repr() == ReprFamily(ReprDepth)
{}
ghost predicate Valid()
reads this, Repr()
ghost function Model(): M
reads this, Repr()
requires Valid()
}
datatype ADT2<A, B> = ADT2(a: ADT<A>, b: ADT<B>)
{
ghost function Repr(): set<object>
reads a, a.ReprFamily(a.ReprDepth-1)
reads b, b.ReprFamily(b.ReprDepth-1)
{
{a} + a.Repr() + {b} + b.Repr()
}
ghost predicate Valid()
reads Repr()
//reads a, a.Repr(), b, b.Repr()
ensures Valid() ==> a.Valid()
ensures Valid() ==> b.Valid()
{
&& a.Valid()
&& b.Valid()
//&& {a} + a.Repr() !! {b} + b.Repr()
&& a.Repr() !! b.Repr()
&& a !in b.Repr()
&& b !in a.Repr()
&& a != b
}
}
ghost predicate Valid2<A, B>(a: ADT<A>, b: ADT<B>)
reads a, a.Repr(), b, b.Repr()
ensures Valid2(a, b) ==> a.Valid()
ensures Valid2(a, b) ==> b.Valid()
{
&& a.Valid()
&& b.Valid()
//&& {a} + a.Repr() !! {b} + b.Repr()
&& a.Repr() !! b.Repr()
&& a !in b.Repr()
&& b !in a.Repr()
&& a != b
}
twostate predicate MaintainsValid2<A, B>(a: ADT<A>, b: ADT<B>)
reads a, a.Repr(), b, b.Repr()
requires old(Valid2(a, b))
{
&& Valid2(a, b)
&& fresh(a.Repr() - old(a.Repr()))
&& fresh(b.Repr() - old(b.Repr()))
}
datatype ADT3<A, B, C> = ADT3(a: ADT<A>, b: ADT<B>, c: ADT<C>)
{
ghost function Repr(): set<object>
reads a, a.ReprFamily(a.ReprDepth-1)
reads b, b.ReprFamily(b.ReprDepth-1)
reads c, c.ReprFamily(c.ReprDepth-1)
{
{a} + a.Repr() + {b} + b.Repr() + {c} + c.Repr()
}
twostate predicate Fresh()
reads Repr()
{
&& fresh(a.Repr() - old(a.Repr()))
&& fresh(b.Repr() - old(b.Repr()))
&& fresh(c.Repr() - old(c.Repr()))
}
ghost predicate {:inline} Valid()
//reads a, a.Repr(), b, b.Repr(), c, c.Repr()
reads Repr()
ensures Valid() ==> a.Valid()
ensures Valid() ==> b.Valid()
ensures Valid() ==> c.Valid()
{
&& a.Valid()
&& b.Valid()
&& c.Valid()
//&& {a} + a.Repr() !! {b} + b.Repr()
&& a.Repr() !! b.Repr()
&& a !in b.Repr()
&& b !in a.Repr()
&& a != b
//&& {a} + a.Repr() !! {c} + c.Repr()
&& a.Repr() !! c.Repr()
&& a !in c.Repr()
&& c !in a.Repr()
&& a != c
//&& {b} + b.Repr() !! {c} + c.Repr()
&& b.Repr() !! c.Repr()
&& b !in c.Repr()
&& c !in b.Repr()
&& b != c
}
}
twostate lemma MaintainsValid2Helper<A, B>(a: ADT<A>, b: ADT<B>)
requires a.Valid()
requires fresh(a.Repr() - old(a.Repr()))
requires old(allocated(a.Repr()))
requires old(allocated(b.Repr()))
requires allocated(b.Repr())
requires b.Repr() == old(b.Repr())
requires unchanged({b} + b.Repr())
//requires old(a.Repr()) !! b.Repr()
requires old(Valid2(a, b))
ensures MaintainsValid2(a, b)
ensures allocated(a.Repr())
ensures allocated(b.Repr())
{
//reveal Valid2();
}
ghost predicate Valid3<A, B, C>(a: ADT<A>, b: ADT<B>, c: ADT<C>)
reads a, a.Repr(), b, b.Repr(), c, c.Repr()
ensures Valid3(a, b, c) ==> a.Valid()
ensures Valid3(a, b, c) ==> b.Valid()
ensures Valid3(a, b, c) ==> c.Valid()
{
&& a.Valid()
&& b.Valid()
&& c.Valid()
//&& {a} + a.Repr() !! {b} + b.Repr()
&& a.Repr() !! b.Repr()
&& a !in b.Repr()
&& b !in a.Repr()
&& a != b
//&& {a} + a.Repr() !! {c} + c.Repr()
&& a.Repr() !! c.Repr()
&& a !in c.Repr()
&& c !in a.Repr()
&& a != c
//&& {b} + b.Repr() !! {c} + c.Repr()
&& b.Repr() !! c.Repr()
&& b !in c.Repr()
&& c !in b.Repr()
&& b != c
}
twostate lemma MaintainsValid3Helper<A, B, C>(a: ADT<A>, b: ADT<B>, c: ADT<C>)
requires a.Valid()
requires fresh(a.Repr() - old(a.Repr()))
requires old(allocated(a.Repr()))
requires old(allocated(b.Repr()))
requires old(allocated(c.Repr()))
requires allocated(b.Repr())
requires allocated(c.Repr())
requires b.Repr() == old(b.Repr())
requires c.Repr() == old(c.Repr())
requires unchanged({b} + b.Repr())
requires unchanged({c} + c.Repr())
requires old(Valid3(a, b, c))
ensures Valid3(a, b, c)
ensures fresh(a.Repr() - old(a.Repr()))
ensures fresh(b.Repr() - old(b.Repr()))
ensures fresh(c.Repr() - old(c.Repr()))
ensures allocated(a.Repr())
ensures allocated(b.Repr())
ensures allocated(c.Repr())
{}
ghost predicate ValidDistinctADTs(adts: seq<ADT>)
reads set r | r in adts :: r
reads BigUnion(set r | r in adts :: r.Repr())
{
&& (forall r | r in adts :: r.Valid())
&& (forall r, s | r in adts && s in adts && [r, s] <= adts ::
{r} + r.Repr() !! {s} + s.Repr())
}
ghost predicate ValidDistinctObjs(objects: seq<object>)
{
forall r, s | r in objects && s in objects && [r, s] <= objects ::
{r} !! {s}
}
ghost predicate ValidDistinct(adts: seq<ADT>, objs: seq<object>)
reads set r | r in adts :: r
reads BigUnion(set r | r in adts :: r.Repr())
{
&& (forall r | r in adts :: r.Valid())
&& (forall r, s | r in adts && s in adts && [r, s] <= adts ::
{r} + r.Repr() !! {s} + s.Repr())
&& (forall r, s | r in objs && s in objs && [r, s] <= objs ::
{r} !! {s})
&& (forall r, s | r in adts && s in objs ::
{r} + r.Repr() !! {s})
}
twostate lemma Disjoint(new r: set<object>, rr: set<object>, s: set<object>)
requires fresh(r - rr)
//requires unchanged(s)
requires rr !! s
ensures r !! s
{}
/*
twostate lemma DisjointFromFresh(x: ADT, y: ADT)
requires old(allocated({x} + x.Repr()))
requires old(allocated({y} + y.Repr()))
requires allocated({x} + x.Repr())
requires allocated({y} + y.Repr())
requires fresh(x.Repr() - old(x.Repr()))
requires fresh(y.Repr() - old(y.Repr()))
requires old({x} + x.Repr() !! {y} + y.Repr())
ensures {x} + x.Repr() !! {y} + y.Repr()
{
assert allocated(old(y.Repr()));
assert old(allocated(y.Repr()));
assert allocated(y.Repr());
assume unchanged(y);
assume unchanged(y.Repr());
assume unchanged({y} + y.Repr());
}
*/
}