-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutex.thy
560 lines (453 loc) · 29.4 KB
/
Mutex.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
(* *********************************************************************
Theory Mutex.thy is part of a framework for modelling,
verification and transformation of concurrent imperative
programs. Copyright (c) 2021 M. Bortin
The framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For more details see the license agreement (LICENSE) you should have
received along with the framework.
******************************************************************* *)
theory Mutex
imports RG_tactics
begin
section "Modelling the mutual exclusion algorithm by G. L. Peterson"
record 'a mutex =
flag0 :: bool
flag1 :: bool
turn :: bool
turn_aux0 :: bool
turn_aux1 :: bool
shared :: "'a"
local0 :: "'a"
local1 :: "'a"
definition "cond0 s \<equiv> turn_aux1 s \<longrightarrow> flag1 s \<longrightarrow> \<not>turn s"
definition "cond1 s \<equiv> turn_aux0 s \<longrightarrow> flag0 s \<longrightarrow> turn s"
subsection "The model with auxiliaries"
definition
"thread0_aux cs P \<equiv>
\<acute>flag0 := True;
\<lbrakk>ann: \<lbrace> \<acute>flag0 \<and> \<not>\<acute>turn_aux0 \<and> P \<acute>shared \<rbrace> \<rbrakk>\<langle>\<acute>turn := True;
\<acute>turn_aux0 := True\<rangle>;
WHILE \<lbrace>\<acute>flag1 \<and> \<acute>turn\<rbrace>
\<lbrakk> inv: \<lbrace> \<acute>flag0 \<and> \<acute>turn_aux0 \<and> P \<acute>shared \<rbrace> \<rbrakk>
DO SKIP OD;
cs;
\<acute>flag0 := False"
definition
"thread1_aux cs P \<equiv>
\<acute>flag1 := True;
\<lbrakk>ann: \<lbrace> \<acute>flag1 \<and> \<not>\<acute>turn_aux1 \<and> P \<acute>shared \<rbrace> \<rbrakk>\<langle>\<acute>turn := False;
\<acute>turn_aux1 := True\<rangle>;
WHILE \<lbrace> \<acute>flag0 \<and> \<not>\<acute>turn \<rbrace>
\<lbrakk> inv: \<lbrace> \<acute>flag1 \<and> \<acute>turn_aux1 \<and> P \<acute>shared \<rbrace> \<rbrakk>
DO SKIP OD;
cs;
\<acute>flag1 := False"
definition
"mutex_aux P0 cs0 Q0 P1 cs1 Q1 \<equiv>
INTERLEAVING-BEGIN
\<lbrakk> rely: \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>local0 = \<ordfeminine>local0 \<and>
(\<ordmasculine>turn_aux0 \<longrightarrow> \<ordmasculine>flag0 \<longrightarrow> \<ordmasculine>cond0 \<longrightarrow> \<ordmasculine>shared = \<ordfeminine>shared) \<and>
(\<ordmasculine>cond0 \<longrightarrow> \<ordfeminine>cond0) \<and>
(P0 \<ordmasculine>shared \<longrightarrow> P0 \<ordfeminine>shared) \<and> (Q0 \<ordmasculine>shared \<longrightarrow> Q0 \<ordfeminine>shared) \<rbrace>,
pre: \<lbrace> \<not>\<acute>turn_aux0 \<and> P0 \<acute>shared \<rbrace>,
post: \<lbrace> Q0 \<acute>shared \<rbrace>
\<rbrakk>
thread0_aux cs0 P0
\<parallel>
\<lbrakk> rely: \<lbrace> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local1 = \<ordfeminine>local1 \<and>
(\<ordmasculine>turn_aux1 \<longrightarrow> \<ordmasculine>flag1 \<longrightarrow> \<ordmasculine>cond1 \<longrightarrow> \<ordmasculine>shared = \<ordfeminine>shared) \<and>
(\<ordmasculine>cond1 \<longrightarrow> \<ordfeminine>cond1) \<and>
(P1 \<ordmasculine>shared \<longrightarrow> P1 \<ordfeminine>shared) \<and> (Q1 \<ordmasculine>shared \<longrightarrow> Q1 \<ordfeminine>shared) \<rbrace>,
pre: \<lbrace> \<not>\<acute>turn_aux1 \<and> P1 \<acute>shared \<rbrace>,
post: \<lbrace> Q1 \<acute>shared \<rbrace>
\<rbrakk>
thread1_aux cs1 P1
INTERLEAVING-END"
subsection "The model without auxiliaries"
definition
"thread0 cs \<equiv>
\<acute>flag0 := True;
\<acute>turn := True;
WHILE \<lbrace>\<acute>flag1 \<and> \<acute>turn\<rbrace>
DO SKIP OD;
cs;
\<acute>flag0 := False"
definition
"thread1 cs \<equiv>
\<acute>flag1 := True;
\<acute>turn := False;
WHILE \<lbrace>\<acute>flag0 \<and> \<not>\<acute>turn\<rbrace>
DO SKIP OD;
cs;
\<acute>flag1 := False"
definition
"mutex cs0 cs1 \<equiv>
INTERLEAVING-BEGIN
thread0 cs0
\<parallel>
thread1 cs1
INTERLEAVING-END"
section "Properties"
definition "mutexR0 = {(s, t). turn_aux0 t \<and> flag0 t \<and> cond0 t \<and> s = t}"
lemma thread0_aux:
" \<Turnstile> cs0
RELY \<lbrace> \<ordmasculine>local0 = \<ordfeminine>local0 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> P0 \<acute>shared \<rbrace>
POST \<lbrace> Q0 \<acute>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local1 = \<ordfeminine>local1 \<and>
(P1 \<ordmasculine>shared \<longrightarrow> P1 \<ordfeminine>shared) \<and> (Q1 \<ordmasculine>shared \<longrightarrow> Q1 \<ordfeminine>shared) \<rbrace> \<Longrightarrow>
\<Turnstile> cs0 \<sqsupseteq>\<^bsub>mutexR0\<^esub> cs0 \<Longrightarrow>
\<Turnstile> thread0_aux cs0 P0
RELY \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>local0 = \<ordfeminine>local0 \<and>
(\<ordmasculine>turn_aux0 \<longrightarrow> \<ordmasculine>flag0 \<longrightarrow> \<ordmasculine>cond0 \<longrightarrow> \<ordmasculine>shared = \<ordfeminine>shared) \<and>
(\<ordmasculine>cond0 \<longrightarrow> \<ordfeminine>cond0) \<and>
(P0 \<ordmasculine>shared \<longrightarrow> P0 \<ordfeminine>shared) \<and> (Q0 \<ordmasculine>shared \<longrightarrow> Q0 \<ordfeminine>shared) \<rbrace>
PRE \<lbrace> \<not>\<acute>turn_aux0 \<and> P0 \<acute>shared \<rbrace>
POST \<lbrace> Q0 \<acute>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local1 = \<ordfeminine>local1 \<and>
(\<ordmasculine>turn_aux1 \<longrightarrow> \<ordmasculine>flag1 \<longrightarrow> \<ordmasculine>cond1 \<longrightarrow> \<ordmasculine>shared = \<ordfeminine>shared) \<and>
(\<ordmasculine>cond1 \<longrightarrow> \<ordfeminine>cond1) \<and>
(P1 \<ordmasculine>shared \<longrightarrow> P1 \<ordfeminine>shared) \<and> (Q1 \<ordmasculine>shared \<longrightarrow> Q1 \<ordfeminine>shared) \<rbrace>"
unfolding thread0_aux_def
apply rg_tac
apply(simp (no_asm) add: cond1_def)
apply(erule prog_corr_RG, assumption)
apply(thin_tac _)+
apply clarsimp
apply(rename_tac s t t')
apply(clarsimp simp: mutexR0_def)
apply(rule_tac b=t' in relcompI, simp, simp)
apply(clarsimp simp: mutexR0_def cond0_def)
apply(clarsimp simp: mutexR0_def)
apply(simp (no_asm) add: cond1_def)
apply(thin_tac _)+
apply(clarsimp simp: mutexR0_def)
apply(rule conjI)
apply(simp add: cond0_def cond1_def)
apply(simp add: cond1_def)+
done
definition "mutexR1 = {(s, t). s = t \<and> turn_aux1 t \<and> flag1 t \<and> cond1 t}"
lemma thread1_aux:
" \<Turnstile> cs1
RELY \<lbrace> \<ordmasculine>local1 = \<ordfeminine>local1 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> P1 \<acute>shared \<rbrace>
POST \<lbrace> Q1 \<acute>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local0 = \<ordfeminine>local0 \<and>
(P0 \<ordmasculine>shared \<longrightarrow> P0 \<ordfeminine>shared) \<and> (Q0 \<ordmasculine>shared \<longrightarrow> Q0 \<ordfeminine>shared) \<rbrace> \<Longrightarrow>
\<Turnstile> cs1 \<sqsupseteq>\<^bsub>mutexR1\<^esub> cs1 \<Longrightarrow>
\<Turnstile> thread1_aux cs1 P1
RELY \<lbrace> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local1 = \<ordfeminine>local1 \<and>
(\<ordmasculine>turn_aux1 \<longrightarrow> \<ordmasculine>flag1 \<longrightarrow> \<ordmasculine>cond1 \<longrightarrow> \<ordmasculine>shared = \<ordfeminine>shared) \<and>
(\<ordmasculine>cond1 \<longrightarrow> \<ordfeminine>cond1) \<and>
(P1 \<ordmasculine>shared \<longrightarrow> P1 \<ordfeminine>shared) \<and> (Q1 \<ordmasculine>shared \<longrightarrow> Q1 \<ordfeminine>shared) \<rbrace>
PRE \<lbrace> \<not>\<acute>turn_aux1 \<and> P1 \<acute>shared \<rbrace>
POST \<lbrace> Q1 \<acute>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>local0 = \<ordfeminine>local0 \<and>
(\<ordmasculine>turn_aux0 \<longrightarrow> \<ordmasculine>flag0 \<longrightarrow> \<ordmasculine>cond0 \<longrightarrow> \<ordmasculine>shared = \<ordfeminine>shared) \<and>
(\<ordmasculine>cond0 \<longrightarrow> \<ordfeminine>cond0) \<and>
(P0 \<ordmasculine>shared \<longrightarrow> P0 \<ordfeminine>shared) \<and> (Q0 \<ordmasculine>shared \<longrightarrow> Q0 \<ordfeminine>shared) \<rbrace>"
unfolding thread1_aux_def
apply rg_tac
apply(simp (no_asm) add: cond0_def)
apply(erule prog_corr_RG, assumption)
apply(thin_tac _)+
apply clarsimp
apply(rename_tac s t t')
apply(clarsimp simp: mutexR1_def)
apply(rule_tac b=t' in relcompI, simp, simp)
apply(clarsimp simp: mutexR1_def cond1_def)
apply(clarsimp simp: mutexR1_def)
apply(simp (no_asm) add: cond0_def)
apply(thin_tac _)+
apply(clarsimp simp: mutexR1_def)
apply(rule conjI)
apply(simp add: cond0_def cond1_def)
apply(simp add: cond0_def)+
done
lemma mutex_aux :
" \<Turnstile> cs0
RELY \<lbrace> \<ordmasculine>local0 = \<ordfeminine>local0 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> P0 \<acute>shared \<rbrace>
POST \<lbrace> Q0 \<acute>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local1 = \<ordfeminine>local1 \<and>
(P1 \<ordmasculine>shared \<longrightarrow> P1 \<ordfeminine>shared) \<and> (Q1 \<ordmasculine>shared \<longrightarrow> Q1 \<ordfeminine>shared) \<rbrace> \<Longrightarrow>
\<Turnstile> cs1
RELY \<lbrace> \<ordmasculine>local1 = \<ordfeminine>local1 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> P1 \<acute>shared \<rbrace>
POST \<lbrace> Q1 \<acute>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local0 = \<ordfeminine>local0 \<and>
(P0 \<ordmasculine>shared \<longrightarrow> P0 \<ordfeminine>shared) \<and> (Q0 \<ordmasculine>shared \<longrightarrow> Q0 \<ordfeminine>shared) \<rbrace> \<Longrightarrow>
\<Turnstile> cs0 \<sqsupseteq>\<^bsub>mutexR0\<^esub> cs0 \<Longrightarrow>
\<Turnstile> cs1 \<sqsupseteq>\<^bsub>mutexR1\<^esub> cs1 \<Longrightarrow>
\<Turnstile> mutex_aux P0 cs0 Q0 P1 cs1 Q1
RELY Id
PRE \<lbrace> P0 \<acute>shared \<and> P1 \<acute>shared \<and> \<not> \<acute>turn_aux0 \<and> \<not> \<acute>turn_aux1 \<rbrace>
POST \<lbrace> Q0 \<acute>shared \<and> Q1 \<acute>shared \<rbrace>
GUAR \<lbrace> True \<rbrace>"
unfolding mutex_aux_def
by(rg_tac use: thread0_aux thread1_aux)
section "The property of mutex by program correspondence"
definition "mutexR = {(s, t). flag0 s = flag0 t \<and>
flag1 s = flag1 t \<and>
turn s = turn t \<and>
shared s = shared t \<and>
local0 s = local0 t \<and>
local1 s = local1 t}"
lemma mutex :
" \<Turnstile> cs0
RELY \<lbrace> \<ordmasculine>local0 = \<ordfeminine>local0 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> P0 \<acute>shared \<rbrace>
POST \<lbrace> Q0 \<acute>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local1 = \<ordfeminine>local1 \<and>
(P1 \<ordmasculine>shared \<longrightarrow> P1 \<ordfeminine>shared) \<and> (Q1 \<ordmasculine>shared \<longrightarrow> Q1 \<ordfeminine>shared) \<rbrace> \<Longrightarrow>
\<Turnstile> cs1
RELY \<lbrace> \<ordmasculine>local1 = \<ordfeminine>local1 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> P1 \<acute>shared \<rbrace>
POST \<lbrace> Q1 \<acute>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local0 = \<ordfeminine>local0 \<and>
(P0 \<ordmasculine>shared \<longrightarrow> P0 \<ordfeminine>shared) \<and> (Q0 \<ordmasculine>shared \<longrightarrow> Q0 \<ordfeminine>shared) \<rbrace> \<Longrightarrow>
\<Turnstile> cs0 \<sqsupseteq>\<^bsub>mutexR0\<^esub> cs0 \<Longrightarrow>
\<Turnstile> cs1 \<sqsupseteq>\<^bsub>mutexR1\<^esub> cs1 \<Longrightarrow>
\<Turnstile> cs0 \<sqsupseteq>\<^bsub>mutexR\<^esub> cs0 \<Longrightarrow>
\<Turnstile> cs1 \<sqsupseteq>\<^bsub>mutexR\<^esub> cs1 \<Longrightarrow>
\<Turnstile> mutex cs0 cs1
RELY Id
PRE \<lbrace> P0 \<acute>shared \<and> P1 \<acute>shared \<rbrace>
POST \<lbrace> Q0 \<acute>shared \<and> Q1 \<acute>shared \<rbrace>
GUAR \<lbrace> True \<rbrace>"
apply(rule_tac r=mutexR in prog_corr_RG[rotated 1])
apply(erule mutex_aux, assumption+)
apply fast
apply clarsimp
apply(rule_tac a="x\<lparr> turn_aux0 := False, turn_aux1 := False \<rparr>" in ImageI)
apply(clarsimp simp: mutexR_def)+
apply(simp add: mutex_aux_def mutex_def)
apply plain_prog_corr_tac
apply(clarsimp simp: thread1_aux_def thread1_def)
apply plain_prog_corr_tac
apply(clarsimp simp: thread0_aux_def thread0_def)
apply plain_prog_corr_tac
done
section "Lifting the mutex-rule to state relations as pre/postconditions"
lemma mutex2 :
" \<Turnstile>\<^sub>2 cs0
RELY \<lbrace> \<ordmasculine>local0 = \<ordfeminine>local0 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> P0 \<ordmasculine>shared \<ordfeminine>shared \<rbrace>
POST \<lbrace> Q0 \<ordmasculine>shared \<ordfeminine>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local1 = \<ordfeminine>local1 \<and>
(\<forall>v. P1 v \<ordmasculine>shared \<longrightarrow> P1 v \<ordfeminine>shared) \<and> (\<forall>v. Q1 v \<ordmasculine>shared \<longrightarrow> Q1 v \<ordfeminine>shared) \<rbrace> \<Longrightarrow>
\<Turnstile>\<^sub>2 cs1
RELY \<lbrace> \<ordmasculine>local1 = \<ordfeminine>local1 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> P1 \<ordmasculine>shared \<ordfeminine>shared \<rbrace>
POST \<lbrace> Q1 \<ordmasculine>shared \<ordfeminine>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local0 = \<ordfeminine>local0 \<and>
(\<forall>v. P0 v \<ordmasculine>shared \<longrightarrow> P0 v \<ordfeminine>shared) \<and> (\<forall>v. Q0 v \<ordmasculine>shared \<longrightarrow> Q0 v \<ordfeminine>shared) \<rbrace> \<Longrightarrow>
\<Turnstile> cs0 \<sqsupseteq>\<^bsub>mutexR0\<^esub> cs0 \<Longrightarrow>
\<Turnstile> cs1 \<sqsupseteq>\<^bsub>mutexR1\<^esub> cs1 \<Longrightarrow>
\<Turnstile> cs0 \<sqsupseteq>\<^bsub>mutexR\<^esub> cs0 \<Longrightarrow>
\<Turnstile> cs1 \<sqsupseteq>\<^bsub>mutexR\<^esub> cs1 \<Longrightarrow>
\<Turnstile>\<^sub>2 mutex cs0 cs1
RELY Id
PRE \<lbrace> P0 \<ordmasculine>shared \<ordfeminine>shared \<and> P1 \<ordmasculine>shared \<ordfeminine>shared \<rbrace>
POST \<lbrace> Q0 \<ordmasculine>shared \<ordfeminine>shared \<and> Q1 \<ordmasculine>shared \<ordfeminine>shared \<rbrace>
GUAR \<lbrace> True \<rbrace>"
apply(subst HoareTripleRG2_def, clarify)
apply(subgoal_tac "\<lbrace> P0 \<ordmasculine>shared \<ordfeminine>shared \<and> P1 \<ordmasculine>shared \<ordfeminine>shared\<rbrace> `` {\<sigma>} =
\<lbrace> P0 (shared \<sigma>) \<acute>shared \<and> P1 (shared \<sigma>) \<acute>shared \<rbrace>")
apply(subgoal_tac "\<lbrace> Q0 \<ordmasculine>shared \<ordfeminine>shared \<and> Q1 \<ordmasculine>shared \<ordfeminine>shared\<rbrace> `` {\<sigma>} =
\<lbrace> Q0 (shared \<sigma>) \<acute>shared \<and> Q1 (shared \<sigma>) \<acute>shared \<rbrace>")
apply(erule ssubst)+
apply(subst (asm) HoareTripleRG2_def)+
apply(drule_tac x=\<sigma> in spec)+
apply(rule mutex)
apply(erule ConseqRG, rule subset_refl, clarsimp+)
apply(erule ConseqRG, rule subset_refl, clarsimp+)
apply(rule set_eqI, simp)+
done
section "An example"
definition
"shared_upd0 \<equiv> \<acute>local0 := \<acute>shared;
\<acute>local0 := {0::int} \<union> \<acute>local0;
\<acute>shared := \<acute>local0"
definition
"shared_upd1 \<equiv> \<acute>local1 := \<acute>shared;
\<acute>local1 := {1::int} \<union> \<acute>local1;
\<acute>shared := \<acute>local1"
definition
"concurrent_upds \<equiv> mutex shared_upd0 shared_upd1"
lemma shared_upd0 :
"\<Turnstile>\<^sub>2 shared_upd0
RELY \<lbrace> \<ordmasculine>local0 = \<ordfeminine>local0 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> \<ordmasculine>shared \<subseteq> \<ordfeminine>shared \<rbrace>
POST \<lbrace> 0 \<in> \<ordfeminine>shared \<and> \<ordmasculine>shared \<subseteq> \<ordfeminine>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local1 = \<ordfeminine>local1 \<and>
\<ordmasculine>shared \<subseteq> \<ordfeminine>shared \<rbrace> "
unfolding shared_upd0_def HoareTripleRG2_def
by(clarify, rg_tac, fast+)
lemma shared_upd1 :
"\<Turnstile>\<^sub>2 shared_upd1
RELY \<lbrace> \<ordmasculine>local1 = \<ordfeminine>local1 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> \<ordmasculine>shared \<subseteq> \<ordfeminine>shared \<rbrace>
POST \<lbrace> 1 \<in> \<ordfeminine>shared \<and> \<ordmasculine>shared \<subseteq> \<ordfeminine>shared \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local0 = \<ordfeminine>local0 \<and>
\<ordmasculine>shared \<subseteq> \<ordfeminine>shared \<rbrace> "
unfolding shared_upd1_def HoareTripleRG2_def
by(clarify, rg_tac, fast+)
lemma concurrent_upds' :
"\<Turnstile>\<^sub>2 concurrent_upds
RELY Id
PRE \<lbrace> \<ordmasculine>shared \<subseteq> \<ordfeminine>shared \<and> \<ordmasculine>shared \<subseteq> \<ordfeminine>shared \<rbrace>
POST \<lbrace> (0 \<in> \<ordfeminine>shared \<and> \<ordmasculine>shared \<subseteq> \<ordfeminine>shared) \<and>
(1 \<in> \<ordfeminine>shared \<and> \<ordmasculine>shared \<subseteq> \<ordfeminine>shared ) \<rbrace>
GUAR \<lbrace> True \<rbrace>"
unfolding concurrent_upds_def
apply(rule mutex2)
apply(rule ConseqRG2[OF shared_upd0], simp_all, clarsimp, rule conjI, fast, fast)
apply(rule ConseqRG2[OF shared_upd1], simp_all, clarsimp, rule conjI, fast, fast)
apply(simp_all add: shared_upd0_def shared_upd1_def mutexR0_def mutexR1_def mutexR_def
cond0_def cond1_def)
apply plain_prog_corr_tac+
done
corollary concurrent_upds :
"\<Turnstile>\<^sub>2 concurrent_upds
RELY Id
PRE Id
POST \<lbrace> 0 \<in> \<ordfeminine>shared \<and> 1 \<in> \<ordfeminine>shared \<and> \<ordmasculine>shared \<subseteq> \<ordfeminine>shared \<rbrace>
GUAR \<lbrace> True \<rbrace>"
by(rule ConseqRG2[OF concurrent_upds'], clarsimp+)
text "A more accurate description of @{term concurrent_upds} behaviour is that
the output value of shared is obtained by inserting 0 and 1 into
its initial value."
lemma shared_upd0_eq :
"\<Turnstile>\<^sub>2 shared_upd0
RELY \<lbrace> \<ordmasculine>local0 = \<ordfeminine>local0 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> \<ordmasculine>shared = \<ordfeminine>shared \<or> \<ordmasculine>shared \<union> {1} = \<ordfeminine>shared \<rbrace>
POST \<lbrace> (\<ordfeminine>shared = \<ordmasculine>shared \<union> {0} \<or> \<ordfeminine>shared = \<ordmasculine>shared \<union> {0} \<union> {1}) \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local1 = \<ordfeminine>local1 \<and>
(\<ordmasculine>shared = \<ordfeminine>shared \<or> \<ordmasculine>shared \<union> {0} = \<ordfeminine>shared) \<rbrace> "
unfolding shared_upd0_def HoareTripleRG2_def
by(clarify, rg_tac, fast)
lemma shared_upd1_eq :
"\<Turnstile>\<^sub>2 shared_upd1
RELY \<lbrace> \<ordmasculine>local1 = \<ordfeminine>local1 \<and> \<ordmasculine>shared = \<ordfeminine>shared \<rbrace>
PRE \<lbrace> \<ordmasculine>shared = \<ordfeminine>shared \<or> \<ordmasculine>shared \<union> {0} = \<ordfeminine>shared \<rbrace>
POST \<lbrace> (\<ordfeminine>shared = \<ordmasculine>shared \<union> {1} \<or> \<ordfeminine>shared = \<ordmasculine>shared \<union> {0} \<union> {1}) \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and> \<ordmasculine>local0 = \<ordfeminine>local0 \<and>
(\<ordmasculine>shared = \<ordfeminine>shared \<or> \<ordmasculine>shared \<union> {1} = \<ordfeminine>shared) \<rbrace> "
unfolding shared_upd1_def HoareTripleRG2_def
by(clarify, rg_tac, fast)
lemma concurrent_upds'_eq :
"\<Turnstile>\<^sub>2 concurrent_upds
RELY Id
PRE \<lbrace> (\<ordmasculine>shared = \<ordfeminine>shared \<or> \<ordmasculine>shared \<union> {1} = \<ordfeminine>shared) \<and>
(\<ordmasculine>shared = \<ordfeminine>shared \<or> \<ordmasculine>shared \<union> {0} = \<ordfeminine>shared) \<rbrace>
POST \<lbrace> (\<ordfeminine>shared = \<ordmasculine>shared \<union> {0} \<or> \<ordfeminine>shared = \<ordmasculine>shared \<union> {0} \<union> {1}) \<and>
(\<ordfeminine>shared = \<ordmasculine>shared \<union> {1} \<or> \<ordfeminine>shared = \<ordmasculine>shared \<union> {0} \<union> {1}) \<rbrace>
GUAR \<lbrace> True \<rbrace>"
unfolding concurrent_upds_def
apply(rule mutex2)
apply(rule ConseqRG2[OF shared_upd0_eq], simp_all, clarsimp, rule conjI, blast, blast)
apply(rule ConseqRG2[OF shared_upd1_eq], simp_all, clarsimp, rule conjI, blast, blast)
apply(simp_all add: shared_upd0_def shared_upd1_def mutexR0_def mutexR1_def mutexR_def
cond0_def cond1_def)
apply plain_prog_corr_tac+
done
corollary concurrent_upds_eq :
"\<Turnstile>\<^sub>2 concurrent_upds
RELY Id
PRE Id
POST \<lbrace> \<ordmasculine>shared \<union> {0, 1} = \<ordfeminine>shared \<rbrace>
GUAR \<lbrace> True \<rbrace>"
by(rule ConseqRG2[OF concurrent_upds'_eq], clarsimp+, blast, simp)
section "Deriving the global guarantees for liveness"
definition
"cs_cond \<rho> cs =
(\<Turnstile> cs
RELY \<lbrace> True \<rbrace>
PRE \<lbrace> True \<rbrace>
POST \<lbrace> True \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn = \<ordfeminine>turn \<and>
\<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<rbrace>)"
text "Note that the rely condition \<ordmasculine>flag0 = \<ordfeminine>flag0 in thread0_auxG below
is solely due to the annotations in the definition of thread0_aux
which are too detailed for the current setting.
This condition could be simply discarded if the annotations are
adjusted accordingly."
lemma thread0_auxG:
"cs_cond \<rho> cs0 \<Longrightarrow>
\<Turnstile> thread0_aux cs0 (\<lambda>a. True)
RELY \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<rbrace>
PRE \<lbrace> \<not>\<acute>turn_aux0 \<rbrace>
POST \<lbrace> True \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<and>
(\<ordmasculine>turn_aux0 \<longrightarrow> \<ordfeminine>turn_aux0) \<and>
(\<ordmasculine>turn_aux0 \<longrightarrow> \<ordmasculine>turn = \<ordfeminine>turn) \<and>
(\<ordmasculine>turn_aux0 \<longrightarrow> \<ordfeminine>flag0 \<longrightarrow> \<ordmasculine>flag0) \<rbrace>"
unfolding thread0_aux_def cs_cond_def
apply rg_tac
apply(erule ConseqRG, clarsimp+)
done
lemma thread1_auxG:
"cs_cond \<rho> cs1 \<Longrightarrow>
\<Turnstile> thread1_aux cs1 (\<lambda>a. True)
RELY \<lbrace> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<rbrace>
PRE \<lbrace> \<not>\<acute>turn_aux1 \<rbrace>
POST \<lbrace> True \<rbrace>
GUAR \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<and>
(\<ordmasculine>turn_aux1 \<longrightarrow> \<ordfeminine>turn_aux1) \<and>
(\<ordmasculine>turn_aux1 \<longrightarrow> \<ordmasculine>turn = \<ordfeminine>turn) \<and>
(\<ordmasculine>turn_aux1 \<longrightarrow> \<ordfeminine>flag1 \<longrightarrow> \<ordmasculine>flag1) \<rbrace>"
unfolding thread1_aux_def cs_cond_def
apply rg_tac
apply(erule ConseqRG, clarsimp+)
done
definition
"mutex_auxG cs0 cs1 \<equiv>
INTERLEAVING-BEGIN
\<lbrakk> rely: \<lbrace> \<ordmasculine>flag0 = \<ordfeminine>flag0 \<and> \<ordmasculine>turn_aux0 = \<ordfeminine>turn_aux0 \<rbrace>,
pre: \<lbrace> \<not>\<acute>turn_aux0 \<rbrace>,
post: UNIV
\<rbrakk>
thread0_aux cs0 (\<lambda>a. True)
\<parallel>
\<lbrakk> rely: \<lbrace> \<ordmasculine>flag1 = \<ordfeminine>flag1 \<and> \<ordmasculine>turn_aux1 = \<ordfeminine>turn_aux1 \<rbrace>,
pre: \<lbrace> \<not>\<acute>turn_aux1 \<rbrace>,
post: UNIV
\<rbrakk>
thread1_aux cs1 (\<lambda>a. True)
INTERLEAVING-END"
lemma mutex_auxG :
"cs_cond \<rho> cs0 \<Longrightarrow>
cs_cond \<rho> cs1 \<Longrightarrow>
\<Turnstile>i mutex_auxG cs0 cs1
RELY Id
PRE \<lbrace> \<not>\<acute>turn_aux0 \<and> \<not>\<acute>turn_aux1 \<rbrace>
POST \<lbrace> True \<rbrace>
GUAR \<lbrace> (\<ordmasculine>turn_aux0 \<longrightarrow> \<ordmasculine>turn_aux1 \<longrightarrow> \<ordmasculine>turn = \<ordfeminine>turn) \<and>
(\<ordmasculine>turn_aux0 \<longrightarrow> \<ordfeminine>turn_aux0) \<and>
(\<ordmasculine>turn_aux0 \<longrightarrow> \<ordfeminine>flag0 \<longrightarrow> \<ordmasculine>flag0) \<and>
(\<ordmasculine>turn_aux1 \<longrightarrow> \<ordfeminine>turn_aux1) \<and>
(\<ordmasculine>turn_aux1 \<longrightarrow> \<ordfeminine>flag1 \<longrightarrow> \<ordmasculine>flag1) \<rbrace>"
unfolding mutex_auxG_def
apply rg_tac
apply(rule ConseqRG, rule thread1_auxG, simp+)
apply(rule conjI, clarsimp)
apply(case_tac j, clarsimp+)
apply(rule ConseqRG, rule thread0_auxG, simp+)
apply(rule conjI, clarsimp+)
done
end