-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNotLockStep.dfy
277 lines (239 loc) · 8.34 KB
/
NotLockStep.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
// RUN: /compile:0 /nologo /noNLarith /noCheating:1 /rlimit:1000000
include "../armada/code/dafny/util/option.s.dfy"
include "../armada/code/dafny/util/collections/seqs.s.dfy"
include "../armada/code/dafny/util/collections/seqs.i.dfy"
include "../armada/code/dafny/fl/spec/refinement.s.dfy"
include "../armada/code/dafny/fl/util/invariants.i.dfy"
/*
var x := 0;
x := x + 1;
x := x + 1;
output x;
------------------------
var x := 0;
x := x + 2;
output x;
*/
module Prelude {
type Tid = int
datatype Event = Event(value: int)
}
module Layer0 {
import opened Prelude
import opened util_option_s
import opened GeneralRefinementModule
import opened InvariantsModule
datatype State = State(x: int, log: seq<Event>)
datatype MainPC = MainPC0 | MainPC1 | MainPC2
datatype TotalState = TotalState(shared: State, local: Option<MainPC>)
predicate SharedInit(s: State)
{
&& s.x == 0
&& s.log == []
}
predicate Init(s: TotalState)
{
&& SharedInit(s.shared)
&& s.local == Some(MainPC0)
}
predicate Inv(s: TotalState)
{
match s.local
case None => true
case Some(pc) =>
match pc {
case MainPC0 => s.shared.x == 0
case MainPC1 => s.shared.x == 1
case MainPC2 => s.shared.x == 2
}
}
function GetInv(): iset<TotalState>
{
iset s | Inv(s)
}
predicate Inc(s: State, s': State)
{
&& s'.x == s.x + 1
&& s'.log == s.log
}
predicate Print(s: State, s': State, v: int)
{
&& s'.x == s.x
&& s'.log == s.log + [Event(v)]
}
predicate Next(s: TotalState, s': TotalState)
{
match s.local
case Some(pc) =>
match pc {
case MainPC0 =>
&& Inc(s.shared, s'.shared)
&& s'.local == Some(MainPC1)
case MainPC1 =>
&& Inc(s.shared, s'.shared)
&& s'.local == Some(MainPC2)
case MainPC2 =>
&& Print(s.shared, s'.shared, s.shared.x)
&& s'.local.None?
}
case None => false
}
function GetSpec(): Spec<TotalState>
{
Spec(iset s | Init(s), iset s, s' | Next(s, s') :: StatePair(s, s'))
}
lemma lemma_InvIsInvariant()
ensures IsSpecInvariant(GetInv(), GetSpec())
{
lemma_EstablishSpecInvariantPure(GetInv(), GetSpec());
}
}
module Layer1 {
import opened Prelude
import opened util_option_s
import opened GeneralRefinementModule
datatype State = State(x: int, log: seq<Event>)
datatype MainPC = MainPC0 | MainPC1
datatype TotalState = TotalState(shared: State, local: Option<MainPC>)
predicate SharedInit(s: State)
{
&& s.x == 0
&& s.log == []
}
predicate Init(s: TotalState)
{
&& SharedInit(s.shared)
&& s.local == Some(MainPC0)
}
predicate IncBy2(s: State, s': State)
{
&& s'.x == s.x + 2
&& s'.log == s.log
}
predicate Print(s: State, s': State, v: int)
{
&& s'.x == s.x
&& s'.log == s.log + [Event(v)]
}
predicate Next(s: TotalState, s': TotalState)
{
|| s' == s
|| match s.local
case Some(pc) =>
match pc {
case MainPC0 =>
&& IncBy2(s.shared, s'.shared)
&& s'.local == Some(MainPC1)
case MainPC1 =>
&& Print(s.shared, s'.shared, s.shared.x)
&& s'.local.None?
}
case None => false
}
function GetSpec(): Spec<TotalState>
{
Spec(iset s | Init(s), iset s, s' | Next(s, s') :: StatePair(s, s'))
}
}
module Sim01 {
import opened util_option_s
import opened util_collections_seqs_s
import opened util_collections_seqs_i
import opened GeneralRefinementModule
import opened InvariantsModule
import Low = Layer0
import High = Layer1
predicate RefinementPredicate(l: Low.TotalState, h: High.TotalState)
{
l.shared.log == h.shared.log
}
function RefinementSet(): iset<RefinementPair<Low.TotalState, High.TotalState>>
{
iset l, h | RefinementPredicate(l, h) :: RefinementPair(l, h)
}
predicate SimulationRelation(l: Low.TotalState, h: High.TotalState)
{
&& l.shared.log == h.shared.log
&& (l.local.None? <==> h.local.None?)
&& match l.local
case None => true
case Some(lpc) =>
match lpc
case MainPC0 =>
&& h.local.v == High.MainPC0
&& h.shared.x == 0
case MainPC1 =>
&& h.local.v == High.MainPC0
&& h.shared.x == 0
case MainPC2 =>
&& h.local.v == High.MainPC1
&& h.shared.x == 2
}
function GetSimulationSet(): iset<RefinementPair<Low.TotalState, High.TotalState>>
{
iset l, h | SimulationRelation(l, h) :: RefinementPair(l, h)
}
lemma lemma_Refinement()
ensures SpecRefinesSpec(Low.GetSpec(), High.GetSpec(), RefinementSet())
{
var l_spec := Low.GetSpec();
var h_spec := High.GetSpec();
var relation := RefinementSet();
forall lb | BehaviorSatisfiesSpec(lb, l_spec)
ensures BehaviorRefinesSpec(lb, h_spec, relation)
{
assert lb[0] in l_spec.init;
var h0 := High.TotalState(High.State(0, []), Some(High.MainPC0));
assert RefinementPair(lb[0], h0) in relation;
assert h0 in h_spec.init;
var hb := [h0];
var li := 0;
while li < |lb| - 1
invariant 0 <= li < |lb|
invariant |hb| == li + 1
invariant BehaviorSatisfiesSpec(hb, h_spec)
invariant forall i :: 0 <= i <= li ==> SimulationRelation(lb[i], hb[i])
{
var l := lb[li];
var l' := lb[li + 1];
var h := last(hb);
Low.lemma_InvIsInvariant();
lemma_SpecInvariantHoldsAtStep(lb, li, l_spec, Low.GetInv());
assert BehaviorSatisfiesSpec(lb, l_spec);
assert l in Low.GetInv() && Low.Inv(l);
assert SimulationRelation(l, h);
assert StatePair(l, l') in l_spec.next;
var h': High.TotalState;
if l.local.Some? {
match l.local.v
case MainPC0 => {
assert l'.shared.log == l.shared.log;
assert l'.local.Some?;
h' := h;
assert SimulationRelation(l', h');
assert StatePair(h, h') in h_spec.next;
}
case MainPC1 => {
h' := High.TotalState(High.State(l'.shared.x, l'.shared.log), Some(High.MainPC1));
assert SimulationRelation(l', h');
assert StatePair(h, h') in h_spec.next;
}
case MainPC2 => {
h' := High.TotalState(High.State(l'.shared.x, l'.shared.log), None);
assert SimulationRelation(l', h');
assert StatePair(h, h') in h_spec.next;
}
}
hb := hb + [h'];
li := li + 1;
}
assert lb == lb[..li+1];
var lh_map := ConvertMapToSeq(|lb|, map i | 0 <= i < |lb| :: RefinementRange(i, i));
forall i, j |
0 <= i < |lb| && lh_map[i].first <= j <= lh_map[i].last
ensures RefinementPair(lb[i], hb[j]) in relation
{}
assert BehaviorRefinesBehaviorUsingRefinementMap(lb, hb, relation, lh_map);
}
}
}