/
PromiseCombinators.thy
428 lines (347 loc) · 14.4 KB
/
PromiseCombinators.thy
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
417
418
419
420
421
422
423
424
425
426
427
428
theory PromiseCombinators imports Main Interleave SelfInv begin
datatype expr =
Num nat |
Tuple expr expr |
Pending expr expr |
PendingForever |
Resolve expr |
Reject expr |
All2 expr |
Race2 expr
datatype event =
EventResolve expr |
EventReject expr
text "Unfailable world"
inductive
UF_small_step :: "expr \<times> event list \<Rightarrow> expr \<times> event list \<Rightarrow> bool" (infix "\<Rightarrow>" 55)
where
UF_NumNumI[intro]: "(Num n, ev) \<Rightarrow> (Num n, ev)" |
UF_PendingResolveI[intro]: "(Pending a b, ev) \<Rightarrow> (Resolve a, ev@[EventResolve a])" |
UF_PendingPendingForeverI[intro]: "(Pending a b, ev) \<Rightarrow> (PendingForever, ev)" |
UF_PendingForeverPendingForeverI[intro]: "(PendingForever, ev) \<Rightarrow> (PendingForever, ev)" |
UF_All2ResolveI[intro]: "\<lbrakk> (All2 (Tuple a b), ev) \<Rightarrow> (All2 (Tuple (Resolve a') (Resolve b')), ev') \<rbrakk> \<Longrightarrow> (All2 (Tuple a b), ev) \<Rightarrow> (Resolve (Tuple a' b'), ev')" |
UF_Race2ResolveI1[intro]: "\<lbrakk> (Race2 (Tuple a b), ev) \<Rightarrow> (Race2 (Tuple (Resolve a') b'), ev') \<rbrakk> \<Longrightarrow> (Race2 (Tuple a b), ev) \<Rightarrow> (Resolve a', ev')" |
UF_Race2ResolveI2[intro]: "\<lbrakk> (Race2 (Tuple a b), ev) \<Rightarrow> (Race2 (Tuple a' (Resolve b')), ev') \<rbrakk> \<Longrightarrow> (Race2 (Tuple a b), ev) \<Rightarrow> (Resolve b', ev')" |
UF_TupleTupleI[intro]: "\<lbrakk> (a, ev) \<Rightarrow> (a', evA'); (b, ev) \<Rightarrow> (b', evB'); interleave evA' evB' ev' \<rbrakk> \<Longrightarrow> (Tuple a b, ev) \<Rightarrow> (Tuple a' b', ev')" |
UF_PendingPendingI[intro]: "\<lbrakk> (a, ev) \<Rightarrow> (a', evA'); (b, ev) \<Rightarrow> (b', evB'); interleave evA' evB' ev' \<rbrakk> \<Longrightarrow> (Pending a b, ev) \<Rightarrow> (Pending a' b', ev')" |
UF_ResolveResolveI[intro]: "\<lbrakk> (x, ev) \<Rightarrow> (x', ev') \<rbrakk> \<Longrightarrow> (Resolve x, ev) \<Rightarrow> (Resolve x', ev')" |
UF_All2All2I[intro]: "\<lbrakk> (Tuple a b, ev) \<Rightarrow> (Tuple a' b', ev') \<rbrakk> \<Longrightarrow> (All2 (Tuple a b), ev) \<Rightarrow> (All2 (Tuple a' b'), ev')" |
UF_Race2Race2I[intro]: "\<lbrakk> (Tuple a b, ev) \<Rightarrow> (Tuple a' b', ev') \<rbrakk> \<Longrightarrow> (Race2 (Tuple a b), ev) \<Rightarrow> (Race2 (Tuple a' b'), ev')"
inductive_cases UF_All2_ResolveE: "(All2 (Tuple a b), ev) \<Rightarrow> (Resolve (Tuple a' b'), ev')"
inductive_cases UF_All2_All2E: "(All2 (Tuple a b), ev) \<Rightarrow> (All2 (Tuple a' b'), ev')"
inductive_cases UF_Tuple_TupleE: "(Tuple a b, ev) \<Rightarrow> (Tuple a' b', ev')"
inductive_cases UF_Pending_ResolveE: "(Pending a b, ev) \<Rightarrow> (Resolve a', ev')"
inductive_cases UF_Race2_ResolveE: "(Race2 (Tuple a b), ev) \<Rightarrow> (Resolve a', ev')"
inductive_cases UF_Race2_Race2E: "(Race2 (Tuple a b), ev) \<Rightarrow> (Race2 (Tuple a' b'), ev')"
inductive_cases UF_PendingForever_FreeE: "(PendingForever, ev) \<Rightarrow> (x', ev')"
lemma "(Tuple (Num 0) (Num 1), []) \<Rightarrow> (Tuple (Num 0) (Num 1), [])"
apply(subst (2) append.right_neutral[symmetric])
apply(rule UF_TupleTupleI)
apply(rule UF_NumNumI)
apply(rule UF_NumNumI)
apply(force)
done
lemma "(Pending (Num 0) (Num 1), []) \<Rightarrow> (Resolve (Num 0), [EventResolve (Num 0)])"
apply(subst (2) append.left_neutral[symmetric])
apply(rule UF_PendingResolveI)
done
lemma "(Race2 (Tuple (Pending (Num 0) (Num 1)) (Pending (Num 2) (Num 3))), []) \<Rightarrow> (Resolve (Num 0), [EventResolve (Num 0)])"
apply(rule UF_Race2ResolveI1)
apply(rule UF_Race2Race2I)
apply(subst (2) append.right_neutral[symmetric])
apply(rule UF_TupleTupleI)
apply(rule UF_PendingResolveI)
apply(rule UF_PendingPendingI)
apply(rule UF_NumNumI)
apply(rule UF_NumNumI)
apply(rule interleave_Nil_NilI)
apply(simp)
apply(rule interleave_Cons_FreeI)
apply(rule interleave_Nil_NilI)
done
lemma "(Race2 (Tuple (Pending (Num 0) (Num 1)) (Pending (Num 2) (Num 3))), []) \<Rightarrow> (Resolve (Num 2), [EventResolve (Num 2)])"
apply(rule UF_Race2ResolveI2)
apply(rule UF_Race2Race2I)
apply(subst (2) append.right_neutral[symmetric])
apply(rule_tac ev="[]" in UF_TupleTupleI)
apply(rule UF_PendingPendingI)
apply(rule UF_NumNumI)
apply(rule UF_NumNumI)
apply(rule interleave_Nil_NilI)
apply(rule UF_PendingResolveI)
apply(simp)
apply(rule interleave_Free_ConsI)
apply(rule interleave_Nil_NilI)
done
lemma "(All2 (Tuple (Pending (Num 0) (Num 1)) (Pending (Num 2) (Num 3))), []) \<Rightarrow> (Resolve (Tuple (Num 0) (Num 2)), [EventResolve (Num 0)]@[EventResolve (Num 2)])"
apply(rule UF_All2ResolveI)
apply(rule UF_All2All2I)
apply(rule_tac ev="[]" in UF_TupleTupleI)
apply(rule UF_PendingResolveI)
apply(rule UF_PendingResolveI)
apply(clarsimp)
apply(auto)
done
lemma "(All2 (Tuple (Pending (Num 0) (Num 1)) (Pending (Num 2) (Num 3))), []) \<Rightarrow> (Resolve (Tuple (Num 0) (Num 2)), [EventResolve (Num 2)]@[EventResolve (Num 0)])"
apply(rule UF_All2ResolveI)
apply(rule UF_All2All2I)
apply(rule_tac ev="[]" in UF_TupleTupleI)
apply(rule UF_PendingResolveI)
apply(rule UF_PendingResolveI)
apply(auto)
done
fun
flip :: "expr \<Rightarrow> expr"
where
"flip (All2 a) = All2 (flip a)" |
"flip (Race2 a) = Race2 (flip a)" |
"flip (Tuple a b) = Tuple (flip b) (flip a)" |
"flip (Pending a b) = Pending (flip a) (flip b)" |
"flip (Resolve a) = Resolve (flip a)" |
"flip (Reject a) = Reject (flip a)" |
"flip (Num n) = Num n" |
"flip PendingForever = PendingForever"
theorem flip_self_inv: "self_inv flip"
apply(unfold self_inv_def)
apply(clarify)
apply(rule expr.induct)
apply(auto)
done
lemma flip_flip_ident: "flip (flip x) = x"
apply(rule self_inv_self_inv_iff)
apply(rule flip_self_inv)
done
fun
flip_ev1 :: "event \<Rightarrow> event"
where
"flip_ev1 (EventResolve a) = EventResolve (flip a)" |
"flip_ev1 (EventReject b) = EventReject (flip b)"
theorem flip_ev1_self_inv: "self_inv flip_ev1"
apply(unfold self_inv_def)
apply(clarify)
apply(case_tac "x")
apply(auto simp: self_inv_self_inv_iff flip_self_inv)
done
definition
flip_ev :: "event list \<Rightarrow> event list"
where
"flip_ev \<equiv> map flip_ev1"
theorem flip_ev_self_inv: "self_inv flip_ev"
apply(unfold flip_ev_def)
apply(rule map_self_inv)
apply(rule flip_ev1_self_inv)
done
lemma flip_ev_Nil: "flip_ev [] = []"
apply(unfold flip_ev_def)
apply(auto)
done
lemma flip_ev_flip_ev_ident: "flip_ev (flip_ev x) = x"
apply(rule self_inv_self_inv_iff)
apply(rule flip_ev_self_inv)
done
lemma UF_flipI[rule_format]: "x \<Rightarrow> x' \<longrightarrow> (\<forall>e ev e' ev'. x = (e, ev) \<and> x' = (e', ev') \<longrightarrow> (flip e, flip_ev ev) \<Rightarrow> (flip e', flip_ev ev'))"
apply(rule impI)
apply(erule UF_small_step.induct)
apply(auto simp: flip_ev_def)
apply(rule UF_TupleTupleI)
apply(auto intro: interleave_mapI)
apply(rule UF_PendingPendingI)
apply(auto intro: interleave_mapI simp add: interleave_flip)
done
lemma UF_flipD[rule_format]: "(flip x, flip_ev ev) \<Rightarrow> (flip x', flip_ev ev') \<longrightarrow> (x, ev) \<Rightarrow> (x', ev')"
apply(subgoal_tac "\<exists>y y' eev eev'. flip x = y \<and> flip x' = y' \<and> flip_ev ev = eev \<and> flip_ev ev' = eev' \<and> x = flip y \<and> x' = flip y' \<and> ev = flip_ev eev \<and> ev' = flip_ev eev'")
apply(elim exE conjE)
apply(erule ssubst)+
apply(rule impI)
apply(erule UF_flipI)
apply(auto simp add: flip_self_inv flip_ev_self_inv self_inv_self_inv_iff)
done
theorem UF_flip: "(flip x, flip_ev ev) \<Rightarrow> (flip x', flip_ev ev') \<longleftrightarrow> (x, ev) \<Rightarrow> (x', ev')"
apply(rule iffI)
apply(erule UF_flipD)
apply(erule UF_flipI)
apply(simp)
done
theorem UF_arbitrariness: "\<exists>x0 x1 ev1 x2 ev2. (x0, []) \<Rightarrow> (x1, ev1) \<and> (x0, []) \<Rightarrow> (x2, ev2) \<and> x1 \<noteq> x2"
apply(rule_tac x="Race2 (Tuple (Resolve (Num 1)) (Resolve (Num 2)))" in exI)
apply(rule_tac x="Resolve (Num 1)" in exI)
apply(rule_tac x="[]" in exI)
apply(rule_tac x="Resolve (Num 2)" in exI)
apply(rule_tac x="[]" in exI)
apply(intro conjI)
apply(subst (2) append.left_neutral[symmetric])
apply(rule UF_Race2ResolveI1)
apply(force)
apply(subst (2) append.left_neutral[symmetric])
apply(rule)+
apply(auto)
done
definition
Tuple3 :: "expr \<Rightarrow> expr \<Rightarrow> expr \<Rightarrow> expr"
where
"Tuple3 a b c \<equiv> Tuple a (Tuple b c)"
definition
All3 :: "expr \<Rightarrow> expr \<Rightarrow> expr \<Rightarrow> expr"
where
"All3 a b c \<equiv> All2 (Tuple a (All2 (Tuple b c)))"
theorem "\<exists>ev. (All3 (Pending (Num 3) (Num 0)) (Pending (Num 2) (Num 0)) (Pending (Num 1) (Num 0)), []) \<Rightarrow> (Resolve (Tuple3 (Num 3) (Num 2) (Num 1)), ev)"
apply(rule_tac x="[EventResolve (Num 3)]@[EventResolve (Num 2)]@[EventResolve (Num 1)]" in exI)
apply(unfold All3_def Tuple3_def)
apply(subst append_assoc[symmetric])+
apply(rule)+
apply(force)
apply(simp)
apply(rule)+
done
definition
events :: "expr \<Rightarrow> expr \<Rightarrow> event list set"
where
"events x x' \<equiv> { ev | ev. (x, []) \<Rightarrow> (x', ev) }"
lemma flip_Nil_flip_Free: "(flip x, []) \<Rightarrow> (flip x', ev) \<longleftrightarrow> (x, []) \<Rightarrow> (x', flip_ev ev)"
apply(subgoal_tac "\<exists>ev'. ev = flip_ev ev' \<and> ev' = flip_ev ev \<and> [] = flip_ev []")
apply(elim exE conjE)
apply(erule ssubst)+
apply(subst UF_flip)
apply(subst flip_ev_flip_ev_ident)
apply(subst flip_ev_Nil)
apply(rule refl)
apply(rule_tac x="flip_ev ev" in exI)
apply(force simp add: flip_ev_flip_ev_ident flip_ev_Nil)
done
lemma events_flip_flip1[rule_format]: "\<lbrakk> fev = flip_ev ` events (flip x) (flip x') \<rbrakk> \<Longrightarrow> fev \<subseteq> events x x'"
apply(erule ssubst)
apply(unfold events_def)
apply(subst flip_Nil_flip_Free)
apply(auto simp add: image_def)
done
lemma events_flip_flip2[rule_format]: "\<lbrakk> fev = flip_ev ` events (flip x) (flip x') \<rbrakk> \<Longrightarrow> events x x' \<subseteq> fev"
apply(subgoal_tac "\<exists>y y'. x = flip y \<and> x' = flip y'")
apply(elim exE conjE)
apply(erule ssubst)+
apply(simp only: flip_flip_ident)
apply(rule_tac x="flip_ev ` events (flip y) (flip y')" in self_inv_image_subsetI)
apply(rule flip_ev_self_inv)
apply(rule events_flip_flip1)
apply(rule refl)
apply(force simp add: flip_ev_flip_ev_ident)
apply(rule_tac x="flip x" in exI)
apply(rule_tac x="flip x'" in exI)
apply(force simp add: flip_flip_ident)
done
lemma events_flip_flip: "flip_ev ` events (flip x) (flip x') = events x x'"
apply(rule equalityI)
apply(rule events_flip_flip1)
apply(rule refl)
apply(rule events_flip_flip2)
apply(rule refl)
done
fun
to_appended_all :: "'a list \<Rightarrow> 'a list"
where
"to_appended_all [] = []" |
"to_appended_all [x] = [x]" |
"to_appended_all (x1#x2#xs) = ([x1]@[x2])@(to_appended_all xs)"
definition
appended_all :: "'a list \<Rightarrow> 'a list"
where
"appended_all \<equiv> to_appended_all"
lemma appended_all_map: "appended_all (map f x) = map f (appended_all x)"
apply(rule_tac P="\<lambda>x. appended_all (map f x) = map f (appended_all x)" in to_appended_all.induct)
apply(unfold appended_all_def flip_ev_def)
apply(auto)
done
theorem All2_events: "
events
(All2 (Tuple (Pending (Num 1) (Num 0)) (Pending (Num 2) (Num 0))))
(Resolve (Tuple (Num 1) (Num 2)))
= {
appended_all [EventResolve (Num 1), EventResolve (Num 2)],
appended_all [EventResolve (Num 2), EventResolve (Num 1)]
}"
apply(unfold events_def)
apply(intro equalityI subsetI)
apply(clarsimp)
apply(elim UF_All2_ResolveE UF_All2_All2E UF_Tuple_TupleE UF_Pending_ResolveE)
apply(auto simp add: interleave_single_single)
apply(unfold appended_all_def to_appended_all.simps)
apply(force)
apply(force)
apply(rule)+
apply(simp add: interleave_single_single)
apply(rule)+
apply(simp add: interleave_single_single)
done
theorem Race2L_events: "
events
(Race2 (Tuple (Pending (Num 1) (Num 0)) PendingForever))
(Resolve (Num 1))
= { appended_all [EventResolve (Num 1)] }"
apply(unfold events_def)
apply(intro equalityI subsetI)
apply(clarsimp)
apply(elim UF_Race2_ResolveE UF_Race2_Race2E UF_Tuple_TupleE UF_PendingForever_FreeE)
apply(clarsimp simp add: interleave_right_neutral)
apply(elim UF_Pending_ResolveE)
apply(force simp: appended_all_def)
apply(force)
apply(simp)
apply(clarify)
apply(unfold appended_all_def to_appended_all.simps append.right_neutral)
apply(rule UF_Race2ResolveI1)
apply(rule)+
apply(simp add: interleave_right_neutral)
done
theorem Race2R_events: "
events
(Race2 (Tuple PendingForever (Pending (Num 1) (Num 0))))
(Resolve (Num 1))
= { appended_all [EventResolve (Num 1)] }"
apply(subst events_flip_flip[symmetric])
apply(subst self_inv_flip [where f="image flip_ev"])
apply(rule image_self_inv)
apply(rule flip_ev_self_inv)
apply(simp del: One_nat_def add: flip_ev_def appended_all_map[symmetric])
apply(rule Race2L_events)
done
theorem "(
(Race2 (Tuple
(All2 (Tuple (Pending (Num 1) (Num 0)) (Pending (Num 1) (Num 0))))
(All2 (Tuple (Pending (Num 1) (Num 0)) (Pending (Num 2) (Num 0))))
)), []) \<Rightarrow>
(Resolve (Tuple (Num 1) (Num 1)), [EventResolve (Num 1)]@[EventResolve (Num 1)])
"
apply(rule UF_Race2ResolveI1)
apply(rule)+
apply(simp add: interleave_single_single)
apply(rule)+
apply(simp add: interleave_right_neutral)
done
theorem "(
(Race2 (Tuple
(All2 (Tuple (Pending (Num 1) (Num 0)) (Pending (Num 1) (Num 0))))
(All2 (Tuple (Pending (Num 1) (Num 0)) (Pending (Num 2) (Num 0))))
)), []) \<Rightarrow>
(Resolve (Tuple (Num 1) (Num 2)), [EventResolve (Num 2)]@[EventResolve (Num 1)])
"
apply(rule UF_Race2ResolveI2)
apply(rule)+
apply(simp add: interleave_left_neutral)
apply(rule)+
apply(auto)
done
theorem "(
(Race2 (Tuple
(All2 (Tuple (Pending (Num 1) (Num 0)) (Pending (Num 1) (Num 0))))
(All2 (Tuple (Pending (Num 1) (Num 0)) (Pending (Num 2) (Num 0))))
)), []) \<Rightarrow>
(Resolve (Tuple (Num 1) (Num 2)), [EventResolve (Num 1)]@[EventResolve (Num 2)])
"
apply(rule UF_Race2ResolveI2)
apply(rule)+
apply(simp add: interleave_left_neutral)
apply(rule interleave_Cons_FreeI)
apply(rule interleave_Free_ConsI)
apply(rule interleave_Nil_NilI)
apply(auto)
done
end