-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistributed_System.thy
1829 lines (1747 loc) · 66.9 KB
/
Distributed_System.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
section \<open>Modelling distributed systems\<close>
text \<open>We assume familiarity with Chandy and Lamport's
paper \emph{Distributed Snapshots: Determining Global States of
Distributed Systems}~\cite{chandy}.\<close>
theory Distributed_System
imports Main
begin
type_synonym 'a fifo = "'a list"
type_synonym channel_id = nat
datatype 'm message =
Marker
| Msg 'm
datatype recording_state =
NotStarted
| Recording
| Done
text \<open>We characterize distributed systems by three underlying type variables:
Type variable 'p captures the processes of the underlying system.
Type variable 's describes the possible states of the processes.
Finally, type variable 'm describes all possible messages in said system.
Each process is in exactly one state at any point in time of the system.
Processes are interconnected by directed channels, which hold messages in-flight
between connected processes. There can be an arbitrary number of channels between
different processes. The entire state of the system including the (potentially unfinished)
snapshot state is called \emph{configuration}.\<close>
record ('p, 's, 'm) configuration =
states :: "'p \<Rightarrow> 's"
msgs :: "channel_id \<Rightarrow> 'm message fifo"
process_snapshot :: "'p \<Rightarrow> 's option"
channel_snapshot :: "channel_id \<Rightarrow> 'm fifo * recording_state"
text \<open>An event in Chandy and Lamport's formalization describes a
process' state transition, optionally producing or consuming
(but not both) a message on a channel. Additionally, a process may either initiate
a snapshot spontaneously, or is forced to do so by receiving a snapshot \emph{marker}
on one of it's incoming channels.\<close>
datatype ('p, 's, 'm) event =
isTrans: Trans (occurs_on: 'p) 's 's
| isSend: Send (getId: channel_id)
(occurs_on: 'p)
(partner: 'p)
's 's (getMsg: 'm)
| isRecv: Recv (getId: channel_id)
(occurs_on: 'p)
(partner: 'p)
's 's (getMsg: 'm)
| isSnapshot: Snapshot (occurs_on: 'p)
| isRecvMarker: RecvMarker (getId: channel_id)
(occurs_on: 'p)
(partner: 'p)
text \<open>We introduce abbreviations and type synoyms for commonly used terms.\<close>
type_synonym ('p, 's, 'm) trace = "('p, 's, 'm) event list"
abbreviation ps where "ps \<equiv> process_snapshot"
abbreviation cs where "cs \<equiv> channel_snapshot"
abbreviation no_snapshot_change where
"no_snapshot_change c c' \<equiv> ((\<forall>p'. ps c p' = ps c' p') \<and> (\<forall>i'. cs c i' = cs c' i'))"
abbreviation has_snapshotted where
"has_snapshotted c p \<equiv> process_snapshot c p \<noteq> None"
text \<open>A regular event is an event as described in Chandy and Lamport's
original paper: A state transition accompanied by the emission
or receiving of a message. Nonregular events are related to
snapshotting and receiving markers along communication channels.\<close>
definition regular_event[simp]:
"regular_event ev \<equiv> (isTrans ev \<or> isSend ev \<or> isRecv ev)"
lemma nonregular_event:
"~ regular_event ev = (isSnapshot ev \<or> isRecvMarker ev)"
by (meson event.distinct_disc event.exhaust_disc regular_event)
lemma event_occurs_on_unique:
assumes
"p \<noteq> q"
"occurs_on ev = p"
shows
"occurs_on ev \<noteq> q"
using assms by (cases ev, auto)
subsection \<open>The distributed system locale\<close>
text \<open>In order to capture Chandy and Lamport's computation system
we introduce two locales. The distributed system locale describes
global truths, such as the mapping from channel IDs to sender and
receiver processes, the transition relations for the underlying
computation system and the core assumption that no process has
a channel to itself. While not explicitly mentioned in Chandy's
and Lamport's work, it makes sense to assume that a channel need
not communicate to itself via messages, since it shares memory with
itself.\<close>
locale distributed_system =
fixes
channel :: "channel_id \<Rightarrow> ('p * 'p) option" and
trans :: "'p \<Rightarrow> 's \<Rightarrow> 's \<Rightarrow> bool" and
send :: "channel_id \<Rightarrow> 'p \<Rightarrow> 'p \<Rightarrow> 's \<Rightarrow> 's \<Rightarrow> 'm \<Rightarrow> bool" and
recv :: "channel_id \<Rightarrow> 'p \<Rightarrow> 'p \<Rightarrow> 's \<Rightarrow> 's \<Rightarrow> 'm \<Rightarrow> bool"
assumes
no_self_channel:
"\<forall>i. \<nexists>p. channel i = Some (p, p)"
begin
subsubsection \<open>State transitions\<close>
definition can_occur :: "('p, 's, 'm) event \<Rightarrow> ('p, 's, 'm) configuration \<Rightarrow> bool" where
"can_occur ev c \<equiv> (case ev of
Trans p s s' \<Rightarrow> states c p = s
\<and> trans p s s'
| Send i p q s s' msg \<Rightarrow> states c p = s
\<and> channel i = Some (p, q)
\<and> send i p q s s' msg
| Recv i p q s s' msg \<Rightarrow> states c p = s
\<and> channel i = Some (q, p)
\<and> length (msgs c i) > 0
\<and> hd (msgs c i) = Msg msg
\<and> recv i p q s s' msg
| Snapshot p \<Rightarrow> \<not> has_snapshotted c p
| RecvMarker i p q \<Rightarrow> channel i = Some (q, p)
\<and> length (msgs c i) > 0
\<and> hd (msgs c i) = Marker)"
definition src where
"src i p \<equiv> (\<exists>q. channel i = Some (p, q))"
definition dest where
"dest i q \<equiv> (\<exists>p. channel i = Some (p, q))"
lemma can_occur_Recv:
assumes
"can_occur (Recv i p q s s' m) c"
shows
"states c p = s \<and> channel i = Some (q, p) \<and> (\<exists>xs. msgs c i = Msg m # xs) \<and> recv i p q s s' m"
proof -
have "\<exists>xs. msgs c i = Msg m # xs"
using assms can_occur_def
by (metis (mono_tags, lifting) event.case(3) hd_Cons_tl length_greater_0_conv)
then show ?thesis using assms can_occur_def by auto
qed
abbreviation check_snapshot_occur where
"check_snapshot_occur c c' p \<equiv>
(can_occur (Snapshot p) c \<and>
(ps c' p = Some (states c p))
\<and> (\<forall>p'. states c p' = states c' p')
\<and> (\<forall>p'. (p' \<noteq> p) \<longrightarrow> ps c' p' = ps c p')
\<and> (\<forall>i. (\<exists>q. channel i = Some (p, q)) \<longrightarrow> msgs c' i = msgs c i @ [Marker])
\<and> (\<forall>i. (\<exists>q. channel i = Some (q, p)) \<longrightarrow> channel_snapshot c' i = (fst (channel_snapshot c i), Recording))
\<and> (\<forall>i. (\<nexists>q. channel i = Some (p, q)) \<longrightarrow> msgs c' i = msgs c i)
\<and> (\<forall>i. (\<nexists>q. channel i = Some (q, p)) \<longrightarrow> channel_snapshot c' i = channel_snapshot c i))"
abbreviation check_recv_marker_occur where
"check_recv_marker_occur c c' i p q \<equiv>
(can_occur (RecvMarker i p q) c
\<and> (\<forall>r. states c r = states c' r)
\<and> (\<forall>r. (r \<noteq> p) \<longrightarrow> process_snapshot c r = process_snapshot c' r)
\<and> (Marker # msgs c' i = msgs c i)
\<and> (channel_snapshot c' i = (fst (channel_snapshot c i), Done))
\<and> (if has_snapshotted c p
then (process_snapshot c p = process_snapshot c' p)
\<and> (\<forall>i'. (i' \<noteq> i) \<longrightarrow> msgs c' i' = msgs c i')
\<and> (\<forall>i'. (i' \<noteq> i) \<longrightarrow> channel_snapshot c i' = channel_snapshot c' i')
else (process_snapshot c' p = Some (states c p))
\<and> (\<forall>i'. i' \<noteq> i \<and> (\<exists>r. channel i' = Some (p, r))
\<longrightarrow> msgs c' i' = msgs c i' @ [Marker])
\<and> (\<forall>i'. i' \<noteq> i \<and> (\<exists>r. channel i' = Some (r, p))
\<longrightarrow> channel_snapshot c' i' = (fst (channel_snapshot c i'), Recording))
\<and> (\<forall>i'. i' \<noteq> i \<and> (\<nexists>r. channel i' = Some (p, r))
\<longrightarrow> msgs c' i' = msgs c i')
\<and> (\<forall>i'. i' \<noteq> i \<and> (\<nexists>r. channel i' = Some (r, p))
\<longrightarrow> channel_snapshot c' i' = channel_snapshot c i')))"
abbreviation check_trans_occur where
"check_trans_occur c c' p s s'\<equiv>
(can_occur (Trans p s s') c
\<and> (states c' p = s')
\<and> (\<forall>r. (r \<noteq> p) \<longrightarrow> states c' r = states c r)
\<and> (\<forall>i. msgs c' i = msgs c i)
\<and> (no_snapshot_change c c'))"
abbreviation check_send_occur where
"check_send_occur c c' i p q s s' msg \<equiv>
(can_occur (Send i p q s s' msg) c
\<and> (states c' p = s')
\<and> (\<forall>r. (r \<noteq> p) \<longrightarrow> states c' r = states c r)
\<and> (msgs c' i = msgs c i @ [Msg msg])
\<and> (\<forall>i'. i \<noteq> i' \<longrightarrow> msgs c' i' = msgs c i')
\<and> (no_snapshot_change c c'))"
abbreviation check_recv_occur where
"check_recv_occur c c' i p q s s' msg \<equiv>
(can_occur (Recv i p q s s' msg) c
\<and> (states c p = s \<and> states c' p = s')
\<and> (\<forall>r. (r \<noteq> p) \<longrightarrow> states c' r = states c r)
\<and> (msgs c i = Msg msg # msgs c' i)
\<and> (\<forall>i'. i \<noteq> i' \<longrightarrow> msgs c' i' = msgs c i')
\<and> (\<forall>r. process_snapshot c r = process_snapshot c' r)
\<and> (\<forall>i'. i' \<noteq> i \<longrightarrow> channel_snapshot c i' = channel_snapshot c' i')
\<and> (if snd (channel_snapshot c i) = Recording
then channel_snapshot c' i = (fst (channel_snapshot c i) @ [msg], Recording)
else channel_snapshot c i = channel_snapshot c' i))"
text \<open>The \emph{next} predicate lets us express configuration transitions
using events. The predicate $next(s_1, e, s_2)$ denotes the transition
of the configuration $s_1$ to $s_2$ via the event $e$. It ensures that
$e$ can occur in state $s_1$ and the state $s_2$ is correctly constructed
from $s_1$.\<close>
primrec "next" ::
"('p, 's, 'm) configuration
\<Rightarrow> ('p, 's, 'm) event
\<Rightarrow> ('p, 's, 'm) configuration
\<Rightarrow> bool"
("_ \<turnstile> _ \<mapsto> _" [70, 70, 70]) where
next_snapshot: "c \<turnstile> Snapshot p \<mapsto> c' =
check_snapshot_occur c c' p"
| next_recv_marker: "c \<turnstile> RecvMarker i p q \<mapsto> c' =
check_recv_marker_occur c c' i p q"
| next_trans: "c \<turnstile> Trans p s s' \<mapsto> c' =
check_trans_occur c c' p s s'"
| next_send: "c \<turnstile> Send i p q s s' msg \<mapsto> c' =
check_send_occur c c' i p q s s' msg"
| next_recv: "c \<turnstile> Recv i p q s s' msg \<mapsto> c' =
check_recv_occur c c' i p q s s' msg"
text \<open>Useful lemmas about state transitions\<close>
lemma state_and_event_determine_next:
assumes
"c \<turnstile> ev \<mapsto> c'" and
"c \<turnstile> ev \<mapsto> c''"
shows
"c' = c''"
proof (cases ev)
case (Snapshot p)
then have "states c' = states c''" using assms by auto
moreover have "msgs c' = msgs c''"
proof (rule ext)
fix i
show "msgs c' i = msgs c'' i"
proof (cases "channel i = None")
case True
then show ?thesis using Snapshot assms by auto
next
case False
then obtain r s where "channel i = Some (r, s)" by auto
with assms Snapshot show ?thesis by (cases "r = p", simp_all)
qed
qed
moreover have "process_snapshot c' = process_snapshot c''" by (metis Snapshot assms next_snapshot ext)
moreover have "channel_snapshot c' = channel_snapshot c''"
proof (rule ext)
fix i
show "channel_snapshot c' i = channel_snapshot c'' i"
proof (cases "channel i = None")
case True
then show ?thesis using assms Snapshot by simp
next
case False
then obtain r s where "channel i = Some (r, s)" by auto
with assms Snapshot show ?thesis by (cases "s = p", simp_all)
qed
qed
ultimately show "c' = c''" by simp
next
case (RecvMarker i p)
then have "states c' = states c''" using assms by auto
moreover have "msgs c' = msgs c''"
proof (rule ext)
fix i'
show "msgs c' i' = msgs c'' i'"
proof (cases "i' = i")
case True
then have "Marker # msgs c' i' = msgs c i'" using assms RecvMarker by simp
also have "... = Marker # msgs c'' i'" using assms RecvMarker `i' = i` by simp
finally show ?thesis by simp
next
case False
then show ?thesis
proof (cases "has_snapshotted c p")
case True
then show ?thesis using assms RecvMarker `i' \<noteq> i` by simp
next
case no_snap: False
then show ?thesis
proof (cases "channel i' = None")
case True
then show ?thesis using assms RecvMarker `i' \<noteq> i` no_snap by simp
next
case False
then obtain r s where "channel i' = Some (r, s)" by auto
with assms RecvMarker no_snap `i' \<noteq> i` show ?thesis by (cases "r = p"; simp_all)
qed
qed
qed
qed
moreover have "process_snapshot c' = process_snapshot c''"
proof (rule ext)
fix r
show "ps c' r = ps c'' r"
proof (cases "r \<noteq> p")
case True
then show ?thesis using assms RecvMarker by simp
next
case False
with assms RecvMarker `~ r \<noteq> p` show ?thesis by (cases "has_snapshotted c r", auto)
qed
qed
moreover have "channel_snapshot c' = channel_snapshot c''"
proof (rule ext)
fix i'
show "cs c' i' = cs c'' i'"
proof (cases "i' = i")
case True
then show ?thesis using assms RecvMarker by simp
next
case False
then show ?thesis
proof (cases "has_snapshotted c p")
case True
then show ?thesis using assms RecvMarker `i' \<noteq> i` by simp
next
case no_snap: False
then show ?thesis
proof (cases "channel i' = None")
case True
then show ?thesis using assms RecvMarker `i' \<noteq> i` no_snap by simp
next
case False
then obtain r s where "channel i' = Some (r, s)" by auto
with assms RecvMarker no_snap `i' \<noteq> i` show ?thesis by (cases "s = p"; simp_all)
qed
qed
qed
qed
ultimately show "c' = c''" by simp
next
case (Trans p s s')
then have "states c' = states c''" by (metis (no_types, lifting) assms next_trans ext)
moreover have "msgs c' = msgs c''" using assms Trans by auto
moreover have "process_snapshot c' = process_snapshot c''" using assms Trans by auto
moreover have "channel_snapshot c' = channel_snapshot c''" using assms Trans by auto
ultimately show "c' = c''" by simp
next
case (Send i p s s' m)
then have "states c' = states c''" by (metis (no_types, lifting) assms next_send ext)
moreover have "msgs c' = msgs c''"
proof (rule ext)
fix i'
from assms Send show "msgs c' i' = msgs c'' i'" by (cases "i' = i", simp_all)
qed
moreover have "process_snapshot c' = process_snapshot c''" using assms Send by auto
moreover have "channel_snapshot c' = channel_snapshot c''" using assms Send by auto
ultimately show "c' = c''" by simp
next
case (Recv i p s s' m)
then have "states c' = states c''" by (metis (no_types, lifting) assms next_recv ext)
moreover have "msgs c' = msgs c''"
proof (rule ext)
fix i'
from assms Recv show "msgs c' i' = msgs c'' i'" by (cases "i' = i", simp_all)
qed
moreover have "process_snapshot c' = process_snapshot c''" using assms Recv by auto
moreover have "channel_snapshot c' = channel_snapshot c''"
proof (rule ext)
fix i'
show "cs c' i' = cs c'' i'"
proof (cases "i' \<noteq> i")
case True
then show ?thesis using assms Recv by simp
next
case False
with assms Recv show ?thesis by (cases "snd (cs c i') = Recording", auto)
qed
qed
ultimately show "c' = c''" by simp
qed
lemma exists_next_if_can_occur:
assumes
"can_occur ev c"
shows
"\<exists>c'. c \<turnstile> ev \<mapsto> c'"
proof (cases ev)
case (Snapshot p)
let ?c = "\<lparr> states = states c,
msgs = %i. if (\<exists>q. channel i = Some (p, q)) then msgs c i @ [Marker] else msgs c i,
process_snapshot = %r. if r = p then Some (states c p) else ps c r,
channel_snapshot = %i. if (\<exists>q. channel i = Some (q, p)) then (fst (cs c i), Recording) else cs c i \<rparr>"
have "c \<turnstile> ev \<mapsto> ?c" using Snapshot assms by auto
then show ?thesis by blast
next
case (RecvMarker i p q)
show ?thesis
proof (cases "has_snapshotted c p")
case True
let ?c = "\<lparr> states = states c,
msgs = %i'. if i = i' then tl (msgs c i') else msgs c i',
process_snapshot = ps c,
channel_snapshot = %i'. if i = i' then (fst (cs c i'), Done) else cs c i' \<rparr>"
have "msgs c i = Marker # msgs ?c i"
using assms can_occur_def RecvMarker hd_Cons_tl by fastforce
then have "c \<turnstile> ev \<mapsto> ?c" using True RecvMarker assms by auto
then show ?thesis by blast
next
case False
let ?c = "\<lparr> states = states c,
msgs = %i'. if i' = i
then tl (msgs c i')
else if (\<exists>r. channel i' = Some (p, r))
then msgs c i' @ [Marker]
else msgs c i',
process_snapshot = %r. if r = p then Some (states c r) else ps c r,
channel_snapshot = %i'. if i = i' then (fst (cs c i'), Done)
else if (\<exists>r. channel i' = Some (r, p))
then (fst (cs c i'), Recording)
else cs c i' \<rparr>"
have "msgs c i = Marker # msgs ?c i"
using assms can_occur_def RecvMarker hd_Cons_tl by fastforce
moreover have "ps ?c p = Some (states c p)" by simp
ultimately have "c \<turnstile> ev \<mapsto> ?c" using RecvMarker assms False by auto
then show ?thesis by blast
qed
next
case (Trans p s s')
let ?c = "\<lparr> states = %r. if r = p then s' else states c r,
msgs = msgs c,
process_snapshot = ps c,
channel_snapshot = cs c \<rparr>"
have "c \<turnstile> ev \<mapsto> ?c"
using Trans assms by auto
then show ?thesis by blast
next
case (Send i p q s s' msg)
let ?c = "\<lparr> states = %r. if r = p then s' else states c r,
msgs = %i'. if i = i' then msgs c i' @ [Msg msg] else msgs c i',
process_snapshot = ps c,
channel_snapshot = cs c \<rparr>"
have "c \<turnstile> ev \<mapsto> ?c"
using Send assms by auto
then show ?thesis by blast
next
case (Recv i p q s s' msg)
then show ?thesis
proof (cases "snd (cs c i)")
case Recording
let ?c = "\<lparr> states = %r. if r = p then s' else states c r,
msgs = %i'. if i = i' then tl (msgs c i') else msgs c i',
process_snapshot = ps c,
channel_snapshot = %i'. if i = i'
then (fst (cs c i') @ [msg], Recording)
else cs c i'\<rparr>"
have "c \<turnstile> ev \<mapsto> ?c"
using Recv Recording assms can_occur_Recv by fastforce
then show ?thesis by blast
next
case Done
let ?c = "\<lparr> states = %r. if r = p then s' else states c r,
msgs = %i'. if i = i' then tl (msgs c i') else msgs c i',
process_snapshot = ps c,
channel_snapshot = cs c \<rparr>"
have "c \<turnstile> ev \<mapsto> ?c"
using Done Recv assms can_occur_Recv by fastforce
then show ?thesis by blast
next
case NotStarted
let ?c = "\<lparr> states = %r. if r = p then s' else states c r,
msgs = %i'. if i = i' then tl (msgs c i') else msgs c i',
process_snapshot = ps c,
channel_snapshot = cs c \<rparr>"
have "c \<turnstile> ev \<mapsto> ?c"
using NotStarted Recv assms can_occur_Recv by fastforce
then show ?thesis by blast
qed
qed
lemma exists_exactly_one_following_state:
"can_occur ev c \<Longrightarrow> \<exists>!c'. c \<turnstile> ev \<mapsto> c'"
using exists_next_if_can_occur state_and_event_determine_next by blast
lemma no_state_change_if_no_event:
assumes
"c \<turnstile> ev \<mapsto> c'" and
"occurs_on ev \<noteq> p"
shows
"states c p = states c' p \<and> process_snapshot c p = process_snapshot c' p"
using assms by (cases ev, auto)
lemma no_msgs_change_if_no_channel:
assumes
"c \<turnstile> ev \<mapsto> c'" and
"channel i = None"
shows
"msgs c i = msgs c' i"
using assms proof (cases ev)
case (RecvMarker cid p)
then have "cid \<noteq> i" using assms RecvMarker can_occur_def by fastforce
with assms RecvMarker show ?thesis by (cases "has_snapshotted c p", auto)
next
case (Send cid p s s' m)
then have "cid \<noteq> i" using assms Send can_occur_def by fastforce
then show ?thesis using assms Send by auto
next
case (Recv cid p s s' m)
then have "cid \<noteq> i" using assms Recv can_occur_def by fastforce
then show ?thesis using assms Recv by simp
qed simp_all
lemma no_cs_change_if_no_channel:
assumes
"c \<turnstile> ev \<mapsto> c'" and
"channel i = None"
shows
"cs c i = cs c' i"
using assms proof (cases ev)
case (RecvMarker cid p)
then have "cid \<noteq> i" using assms RecvMarker can_occur_def by fastforce
with assms RecvMarker show ?thesis by (cases "has_snapshotted c p", auto)
next
case (Send cid p s s' m)
then have "cid \<noteq> i" using assms Send can_occur_def by fastforce
then show ?thesis using assms Send by auto
next
case (Recv cid p s s' m)
then have "cid \<noteq> i" using assms Recv can_occur_def by fastforce
then show ?thesis using assms Recv by simp
qed simp_all
lemma no_msg_change_if_no_event:
assumes
"c \<turnstile> ev \<mapsto> c'" and
"isSend ev \<longrightarrow> getId ev \<noteq> i" and
"isRecv ev \<longrightarrow> getId ev \<noteq> i" and
"regular_event ev"
shows
"msgs c i = msgs c' i"
proof (cases "channel i = None")
case True
then show ?thesis using assms no_msgs_change_if_no_channel by simp
next
have "isTrans ev \<or> isSend ev \<or> isRecv ev" using assms by simp
then show ?thesis
proof (elim disjE)
assume "isTrans ev"
then show ?thesis
by (metis assms(1) event.collapse(1) next_trans)
next
assume "isSend ev"
then obtain i' r s u u' m where Send: "ev = Send i' r s u u' m" by (meson isSend_def)
then show ?thesis using Send assms by auto
next
assume "isRecv ev"
then obtain i' r s u u' m where "ev = Recv i' r s u u' m" by (meson isRecv_def)
then show ?thesis using assms by auto
qed
qed
lemma no_cs_change_if_no_event:
assumes
"c \<turnstile> ev \<mapsto> c'" and
"isRecv ev \<longrightarrow> getId ev \<noteq> i" and
"regular_event ev"
shows
"cs c i = cs c' i"
proof -
have "isTrans ev \<or> isSend ev \<or> isRecv ev" using assms by simp
then show ?thesis
proof (elim disjE)
assume "isTrans ev"
then show ?thesis
by (metis assms(1) event.collapse(1) next_trans)
next
assume "isSend ev"
then obtain i' r s u u' m where "ev = Send i' r s u u' m" by (meson isSend_def)
then show ?thesis using assms by auto
next
assume "isRecv ev"
then obtain i r s u u' m where "ev = Recv i r s u u' m" by (meson isRecv_def)
then show ?thesis using assms by auto
qed
qed
lemma happen_implies_can_occur:
assumes
"c \<turnstile> ev \<mapsto> c'"
shows
"can_occur ev c"
proof -
show ?thesis using assms by (cases ev, auto)
qed
lemma snapshot_increases_message_length:
assumes
"ev = Snapshot p" and
"c \<turnstile> ev \<mapsto> c'" and
"channel i = Some (q, r)"
shows
"length (msgs c i) \<le> length (msgs c' i)"
using assms by (cases "p = q", auto)
lemma recv_marker_changes_head_only_at_i:
assumes
"ev = RecvMarker i p q" and
"c \<turnstile> ev \<mapsto> c'" and
"i' \<noteq> i"
shows
"msgs c i' = [] \<or> hd (msgs c i') = hd (msgs c' i')"
proof (cases "channel i' = None")
case True
then show ?thesis using assms no_msgs_change_if_no_channel by presburger
next
case False
then show ?thesis
proof (cases "msgs c i'")
case Nil
then show ?thesis by simp
next
case (Cons m xs)
then obtain r s where "channel i' = Some (r, s)" using False by auto
then show ?thesis
proof (cases "has_snapshotted c p")
case True
then show ?thesis using assms by auto
next
case False
with assms show ?thesis by (cases "r = p", auto)
qed
qed
qed
lemma recv_marker_other_channels_not_shrinking:
assumes
"ev = RecvMarker i p q" and
"c \<turnstile> ev \<mapsto> c'"
shows
"length (msgs c i') \<le> length (msgs c' i') \<longleftrightarrow> i \<noteq> i'"
proof (rule iffI)
show "length (msgs c i') \<le> length (msgs c' i') \<Longrightarrow> i \<noteq> i'"
proof (rule ccontr)
assume asm: "~ i \<noteq> i'" "length (msgs c i') \<le> length (msgs c' i')"
then have "msgs c i = Marker # msgs c' i" using assms by auto
then have "length (msgs c i) > length (msgs c' i)" by simp
then have "length (msgs c i') > length (msgs c' i')" using asm by simp
then show False using asm by simp
qed
next
show "i \<noteq> i' \<Longrightarrow> length (msgs c i') \<le> length (msgs c' i')"
proof -
assume "i \<noteq> i'"
then show ?thesis
proof (cases "channel i' = None")
case True
then show ?thesis using assms no_msgs_change_if_no_channel by presburger
next
case False
then obtain r s where chan: "channel i' = Some (r, s)" by auto
then show ?thesis
proof (cases "has_snapshotted c p")
case True
with assms `i \<noteq> i'` show ?thesis by auto
next
case no_snap: False
then show ?thesis
proof (cases "p = r")
case True
then have "msgs c' i' = msgs c i' @ [Marker]" using `i \<noteq> i'` assms no_snap chan by auto
then show ?thesis by auto
next
case False
then show ?thesis using assms `i \<noteq> i'` chan no_snap by auto
qed
qed
qed
qed
qed
lemma regular_event_cannot_induce_snapshot:
assumes
"~ has_snapshotted c p" and
"c \<turnstile> ev \<mapsto> c'"
shows
"regular_event ev \<longrightarrow> ~ has_snapshotted c' p"
proof (cases ev)
case (Trans q s s')
then show ?thesis using assms(1) assms(2) by auto
next
case (Send q r s s' m)
then show ?thesis using assms by auto
next
case (Recv q r s s' m)
then show ?thesis using assms by auto
qed simp_all
lemma regular_event_preserves_process_snapshots:
assumes
"c \<turnstile> ev \<mapsto> c'"
shows
"regular_event ev \<Longrightarrow> ps c r = ps c' r"
proof (cases ev)
case (Trans p s s')
then show ?thesis
using assms by auto
next
case (Send p q s s' m)
then show ?thesis
using assms by auto
next
case (Recv p q s s' m)
then show ?thesis
using assms by auto
qed simp_all
lemma no_state_change_if_nonregular_event:
assumes
"~ regular_event ev" and
"c \<turnstile> ev \<mapsto> c'"
shows
"states c p = states c' p"
proof -
have "isSnapshot ev \<or> isRecvMarker ev" using nonregular_event assms by auto
then show ?thesis
proof (elim disjE, goal_cases)
case 1
then obtain q where "ev = Snapshot q"
by (meson isSnapshot_def)
then show ?thesis
using assms(2) by auto
next
case 2
then obtain i q r where "ev = RecvMarker i q r"
by (meson isRecvMarker_def)
then show ?thesis using assms(2) by auto
qed
qed
lemma nonregular_event_induces_snapshot:
assumes
"~ has_snapshotted c p" and
"c \<turnstile> ev \<mapsto> c'" and
"occurs_on ev = p" and
"~ regular_event ev"
shows
"~ regular_event ev \<longrightarrow> has_snapshotted c' p"
proof (cases ev)
case (Snapshot q)
then have "q = p" using assms by auto
then show ?thesis using Snapshot assms(2) by auto
next
case (RecvMarker i q r)
then have "q = p" using assms by auto
then show ?thesis using RecvMarker assms by auto
qed (simp_all add: assms)
lemma snapshot_state_unchanged:
assumes
step: "c \<turnstile> ev \<mapsto> c'" and
"has_snapshotted c p"
shows
"ps c p = ps c' p"
proof (cases "occurs_on ev = p")
case False
then show ?thesis
using local.step no_state_change_if_no_event by auto
next
case True
then show ?thesis
proof (cases "regular_event ev")
case True
then show ?thesis
using local.step regular_event_preserves_process_snapshots by auto
next
case False
have "isRecvMarker ev"
proof (rule ccontr)
have "isSnapshot ev \<or> isRecvMarker ev"
using False nonregular_event by blast
moreover assume "~ isRecvMarker ev"
ultimately have "isSnapshot ev" by simp
then have "ev = Snapshot p" by (metis True event.collapse(4))
then have "can_occur ev c"
using happen_implies_can_occur local.step by blast
then have "~ has_snapshotted c p" unfolding can_occur_def
by (simp add: \<open>ev = Snapshot p\<close>)
then show False using assms by auto
qed
then show ?thesis (* z3 sledgehammer fails for Isabelle2019 *)
proof -
have "\<exists>n pa. c \<turnstile> RecvMarker n p pa \<mapsto> c'"
by (metis True \<open>isRecvMarker ev\<close> event.collapse(5) local.step)
then show ?thesis
using assms(2) by force
qed
qed
qed
lemma message_must_be_delivered:
assumes
valid: "c \<turnstile> ev \<mapsto> c'" and
delivered: "(msgs c i \<noteq> [] \<and> hd (msgs c i) = m) \<and> (msgs c' i = [] \<or> hd (msgs c' i) \<noteq> m)"
shows
"(\<exists>p q. ev = RecvMarker i p q \<and> m = Marker)
\<or> (\<exists>p q s s' m'. ev = Recv i p q s s' m' \<and> m = Msg m')"
proof (cases ev)
case (Snapshot p)
then show ?thesis
proof (cases "msgs c i")
case Nil
then show ?thesis using delivered by simp
next
case (Cons m xs)
with assms Snapshot show ?thesis
proof (cases "channel i = None")
case True
then show ?thesis using assms Snapshot by auto
next
case False
then obtain r s where chan: "channel i = Some (r, s)" by auto
then show ?thesis
proof (cases "r = p")
case True
then have "msgs c' i = msgs c i @ [Marker]" using assms(1) Snapshot chan by auto
then show ?thesis using delivered by auto
next
case False
then have "msgs c' i = msgs c i" using assms Snapshot chan by simp
then show ?thesis using delivered Cons by simp
qed
qed
qed
next
case (RecvMarker i' p q)
then have "i' = i"
by (metis assms(1) delivered le_0_eq length_greater_0_conv list.size(3) recv_marker_changes_head_only_at_i recv_marker_other_channels_not_shrinking)
moreover have "Marker = m"
using `i' = i` RecvMarker assms(1) can_occur_def delivered by auto
moreover have "channel i = Some (q, p)"
using RecvMarker assms(1) calculation(1) can_occur_def by auto
ultimately show ?thesis using RecvMarker by simp
next
case (Trans p' s s')
then show ?thesis
using valid delivered by auto
next
case (Send p' q' s s' m')
then show ?thesis
by (metis (no_types, lifting) delivered distributed_system.next.simps(4) distributed_system_axioms hd_append2 snoc_eq_iff_butlast valid)
next
case (Recv i' p q s s' m')
then have "i = i'"
using assms(1) delivered by auto
also have "m = Msg m'"
by (metis (no_types, lifting) Recv delivered list.sel(1) next_recv valid)
ultimately show ?thesis using Recv by auto
qed
lemma message_must_be_delivered_2:
assumes
"c \<turnstile> ev \<mapsto> c'"
"m : set (msgs c i)"
"m \<notin> set (msgs c' i)"
shows
"(\<exists>p q. ev = RecvMarker i p q \<and> m = Marker) \<or> (\<exists>p q s s' m'. ev = Recv i p q s s' m' \<and> m = Msg m')"
proof -
have uneq_sets: "set (msgs c i) \<noteq> set (msgs c' i)"
using assms(2) assms(3) by blast
then obtain p q where chan: "channel i = Some (p, q)"
using assms no_msgs_change_if_no_channel by fastforce
then show ?thesis
proof (cases ev)
case (Snapshot p')
with Snapshot assms chan have "set (msgs c' i) = set (msgs c i)" by (cases "p' = p", auto)
then show ?thesis using uneq_sets by simp
next
case (Trans p' s s')
then show ?thesis using uneq_sets assms by simp
next
case (Send i' p' q' s s' m)
then show ?thesis
by (metis (no_types, lifting) UnCI assms(1) assms(2) assms(3) local.next.simps(4) set_append)
next
case (RecvMarker i' p' q')
have "i' = i"
proof (rule ccontr)
assume "~ i' = i"
show False using assms chan RecvMarker
proof (cases "has_snapshotted c p'")
case True
then show False using assms chan RecvMarker `~ i' = i` by simp
next
case False
then show False using assms chan RecvMarker `~ i' = i` by (cases "p' = p", simp_all)
qed
qed
moreover have "m = Marker"
proof -
have "msgs c i' = Marker # msgs c' i'" using assms chan RecvMarker by auto
then show ?thesis using assms `i' = i` by simp
qed
ultimately show ?thesis using RecvMarker by simp
next
case (Recv i' p' q' s s' m')
have "i' = i"
proof (rule ccontr)
assume "~ i' = i"
then show False
using Recv assms(1) uneq_sets by auto
qed
then have "i' = i \<and> m = Msg m'"
using Recv assms by auto
then show ?thesis using Recv by simp
qed
qed
lemma recv_marker_means_snapshotted_1:
assumes
"ev = RecvMarker i p q" and
"c \<turnstile> ev \<mapsto> c'"
shows
"has_snapshotted c' p"
using assms snapshot_state_unchanged by (cases "has_snapshotted c p", auto)
lemma recv_marker_means_snapshotted_2:
fixes
c c' :: "('p, 's, 'm) configuration" and
ev :: "('p, 's, 'm) event" and
i :: channel_id
assumes
"c \<turnstile> ev \<mapsto> c'" and
"Marker : set (msgs c i)" and
"Marker \<notin> set (msgs c' i)" and
"channel i = Some (q, p)"
shows
"has_snapshotted c' p"
proof -
have "\<exists>p q. ev = RecvMarker i p q"
using assms message_must_be_delivered_2 by blast
then obtain r s where RecvMarker: "ev = RecvMarker i r s"
by blast
then have "r = p"
using assms(1) assms(4) can_occur_def by auto
then show ?thesis
using recv_marker_means_snapshotted_1 assms RecvMarker by blast
qed
lemma event_stays_valid_if_no_occurrence:
assumes
"c \<turnstile> ev \<mapsto> c'" and
"occurs_on ev \<noteq> occurs_on ev'" and
"can_occur ev' c"
shows
"can_occur ev' c'"
proof (cases ev')
case (Trans p s s')
have "states c p = states c' p"
using Trans assms(1) assms(2) no_state_change_if_no_event by auto
moreover have "states c p = s" using can_occur_def assms Trans by simp
ultimately have "states c' p = s" by simp
moreover have "trans p s s'"
using Trans assms(3) can_occur_def by auto
ultimately show ?thesis
by (simp add: Trans can_occur_def)
next
case (Recv i p q s s' m)
then have "hd (msgs c i) = Msg m"
proof -
from Recv have "length (msgs c i) > 0" using assms(3) can_occur_def by auto
then obtain m' xs where mcqp: "msgs c i = m' # xs"
by (metis list.size(3) nat_less_le neq_Nil_conv)
then have "Msg m = m'"
proof (cases m', auto)
case Marker
then have "msgs c i = Marker # xs" by (simp add:mcqp)
then have "~ can_occur ev' c" using Recv can_occur_def by simp
then show False using assms(3) by simp
next
case (Msg msg)
then have "msgs c i = Msg msg # xs" by (simp add: mcqp)
then show "m = msg" using Recv can_occur_def assms(3) by simp
qed
then show ?thesis by (simp add: mcqp)
qed
show ?thesis
proof (rule ccontr)