-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathMonadic.thy
830 lines (678 loc) · 41 KB
/
Monadic.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
theory Monadic
imports Implementation "~~/src/HOL/Library/Monad_Syntax"
begin
datatype Exception = IllegalArgumentException
datatype ('e,'a) Result = Success 'a | Exception 'e
datatype 'a Action = Action "NodeData \<Rightarrow> (NodeData * RoutedMessage list * (Exception,'a) Result)"
definition runM :: "'a Action \<Rightarrow> NodeData \<Rightarrow> (NodeData * RoutedMessage list * (Exception,'a) Result)"
where "runM ma \<equiv> case ma of Action unwrapped_ma \<Rightarrow> unwrapped_ma"
lemma runM_Action[simp]: "runM (Action f) = f" by (simp add: runM_def)
lemma runM_inject[intro]: "(\<And>nd. runM ma nd = runM mb nd) \<Longrightarrow> ma = mb" by (cases ma, cases mb, auto simp add: runM_def)
definition return :: "'a \<Rightarrow> 'a Action" where "return a \<equiv> Action (\<lambda> nd. (nd, [], Success a))"
lemma runM_return[simp]: "runM (return a) nd = (nd, [], Success a)" unfolding runM_def return_def by simp
definition Action_bind :: "'a Action \<Rightarrow> ('a \<Rightarrow> 'b Action) \<Rightarrow> 'b Action"
where "Action_bind ma mf \<equiv> Action (\<lambda> nd0. case runM ma nd0 of
(nd1, msgs1, result1) \<Rightarrow> (case result1 of
Exception e \<Rightarrow> (nd1, msgs1, Exception e)
| Success a \<Rightarrow> (case runM (mf a) nd1 of
(nd2, msgs2, result2) \<Rightarrow> (nd2, msgs1 @ msgs2, result2))))"
adhoc_overloading bind Action_bind
lemma runM_bind: "runM (a \<bind> f) nd0 = (case runM a nd0 of (nd1, msgs1, result1) \<Rightarrow> (case result1 of Exception e \<Rightarrow> (nd1, msgs1, Exception e) | Success b \<Rightarrow> (case runM (f b) nd1 of (nd2, msgs2, c) \<Rightarrow> (nd2, msgs1@msgs2, c))))"
unfolding Action_bind_def by auto
lemma return_bind[simp]: "do { a' <- return a; f a' } = f a"
apply (intro runM_inject) by (simp add: runM_bind)
lemma bind_return[simp]: "do { a' <- f; return a' } = f"
proof (intro runM_inject)
fix nd
obtain nd1 msgs1 result1 where result1: "runM f nd = (nd1, msgs1, result1)" by (cases "runM f nd", blast)
show "runM (f \<bind> return) nd = runM f nd"
by (cases result1, simp_all add: runM_bind result1)
qed
lemma bind_bind_assoc[simp]:
fixes f :: "'a Action"
shows "do { b <- do { a <- f; g a }; h b } = do { a <- f; b <- g a; h b }" (is "?LHS = ?RHS")
proof (intro runM_inject)
fix nd0
show "runM ?LHS nd0 = runM ?RHS nd0"
proof (cases "runM f nd0")
case fields1: (fields nd1 msgs1 result1)
show ?thesis
proof (cases result1)
case Exception show ?thesis by (simp add: runM_bind fields1 Exception)
next
case Success1: (Success b)
show ?thesis
proof (cases "runM (g b) nd1")
case fields2: (fields nd2 msgs2 result2)
show ?thesis
proof (cases result2)
case Exception show ?thesis by (simp add: runM_bind fields1 fields2 Success1 Exception)
next
case Success2: (Success c)
show ?thesis
by (cases "runM (h c) nd2", simp add: runM_bind fields1 Success1 fields2 Success2)
qed
qed
qed
qed
qed
definition getNodeData :: "NodeData Action" where "getNodeData \<equiv> Action (\<lambda>nd. (nd, [], Success nd))"
definition setNodeData :: "NodeData \<Rightarrow> unit Action" where "setNodeData nd \<equiv> Action (\<lambda>_. (nd, [], Success ()))"
lemma runM_getNodeData[simp]: "runM getNodeData nd = (nd, [], Success nd)" by (simp add: runM_def getNodeData_def)
lemma runM_setNodeData[simp]: "runM (setNodeData nd') nd = (nd', [], Success ())" by (simp add: runM_def setNodeData_def)
lemma runM_getNodeData_continue[simp]: "runM (do { nd' <- getNodeData; f nd' }) nd = runM (f nd) nd" by (simp add: runM_bind)
lemma runM_setNodeData_continue[simp]: "runM (do { setNodeData nd'; f }) nd = runM f nd'" by (simp add: runM_bind)
definition modifyNodeData :: "(NodeData \<Rightarrow> NodeData) \<Rightarrow> unit Action" where "modifyNodeData f = getNodeData \<bind> (setNodeData \<circ> f)"
lemma runM_modifyNodeData[simp]: "runM (modifyNodeData f) nd = (f nd, [], Success ())" by (simp add: modifyNodeData_def runM_bind)
lemma runM_modifyNodeData_continue[simp]: "runM (do { modifyNodeData f; a }) nd = runM a (f nd)" by (simp add: runM_bind)
definition tell :: "RoutedMessage list \<Rightarrow> unit Action" where "tell rms \<equiv> Action (\<lambda>nd. (nd, rms, Success ()))"
lemma runM_tell[simp]: "runM (tell rms) nd = (nd, rms, Success ())" by (simp add: runM_def tell_def)
lemma runM_tell_contiue[simp]: "runM (do { tell rms; a }) nd = (let (nd, rms', x) = runM a nd in (nd, rms@rms', x))" by (simp add: runM_bind tell_def)
definition send :: "RoutedMessage \<Rightarrow> unit Action" where "send rm = tell [rm]"
definition throw :: "Exception \<Rightarrow> 'a Action" where "throw e = Action (\<lambda>nd. (nd, [], Exception e))"
lemma runM_throw[simp]: "runM (throw e) nd = (nd, [], Exception e)" by (simp add: runM_def throw_def)
lemma throw_continue[simp]: "do { throw e; a } = throw e" by (intro runM_inject, simp add: runM_bind)
definition catch :: "'a Action \<Rightarrow> (Exception \<Rightarrow> 'a Action) \<Rightarrow> 'a Action"
where "catch go onException = Action (\<lambda>nd0. case runM go nd0 of (nd1, rms1, result1) \<Rightarrow> (case result1 of Success _ \<Rightarrow> (nd1, rms1, result1) | Exception e \<Rightarrow> runM (tell rms1 \<then> onException e) nd1))"
lemma catch_throw[simp]: "catch (throw e) handle = handle e" by (intro runM_inject, simp add: catch_def)
lemma catch_return[simp]: "catch (return a) handle = return a" by (intro runM_inject, simp add: catch_def)
lemma catch_getNodeData[simp]: "catch getNodeData handle = getNodeData" by (intro runM_inject, simp add: catch_def)
lemma catch_getNodeData_continue[simp]: "catch (do { nd <- getNodeData; f nd }) handle = do { nd <- getNodeData; catch (f nd) handle }" by (intro runM_inject, simp add: catch_def)
lemma catch_setNodeData[simp]: "catch (setNodeData nd) handle = setNodeData nd" by (intro runM_inject, simp add: catch_def)
lemma catch_setNodeData_continue[simp]: "catch (do { setNodeData nd; f }) handle = do { setNodeData nd; catch f handle }" by (intro runM_inject, simp add: catch_def)
lemma catch_modifyNodeData[simp]: "catch (modifyNodeData f) handle = modifyNodeData f" by (intro runM_inject, simp add: catch_def)
lemma catch_modifyNodeData_continue[simp]: "catch (do { modifyNodeData f; g }) handle = do { modifyNodeData f; catch g handle }" by (intro runM_inject, simp add: catch_def)
lemma catch_tell[simp]: "catch (tell rms) handle = tell rms" by (intro runM_inject, simp add: catch_def)
lemma catch_tell_continue[simp]: "catch (do { tell rms; f }) handle = do { tell rms; catch f handle }"
proof (intro runM_inject)
fix nd0
show "runM (catch (do { tell rms; f }) handle) nd0 = runM (do { tell rms; catch f handle }) nd0"
proof (cases "runM f nd0")
case fields1: (fields nd1 msgs1 result1)
show ?thesis
proof (cases result1)
case (Exception e) show ?thesis by (cases "runM (handle e) nd1", simp add: catch_def fields1 Exception)
next
case Success1: (Success b)
show ?thesis
by (simp add: catch_def fields1 Success1)
qed
qed
qed
lemma catch_send[simp]: "catch (send rm) handle = send rm" by (simp add: send_def)
lemma catch_send_continue[simp]: "catch (do { send rm; f }) handle = do { send rm; catch f handle }" by (simp add: send_def)
definition gets :: "(NodeData \<Rightarrow> 'a) \<Rightarrow> 'a Action" where "gets f \<equiv> do { nd <- getNodeData; return (f nd) }"
definition getCurrentClusterState where "getCurrentClusterState = gets currentClusterState"
definition getCurrentNode where "getCurrentNode = gets currentNode"
definition getCurrentTerm where "getCurrentTerm = gets currentTerm"
definition getCurrentVotingNodes where "getCurrentVotingNodes = gets currentVotingNodes"
definition getElectionWon where "getElectionWon = gets electionWon"
definition getFirstUncommittedSlot where "getFirstUncommittedSlot = gets firstUncommittedSlot"
definition getJoinVotes where "getJoinVotes = gets joinVotes"
definition getLastAcceptedData where "getLastAcceptedData = gets lastAcceptedData"
definition getPublishPermitted where "getPublishPermitted = gets publishPermitted"
definition getPublishVotes where "getPublishVotes = gets publishVotes"
definition sets where "sets f x = modifyNodeData (f (\<lambda>_. x))"
definition setCurrentClusterState where "setCurrentClusterState = sets currentClusterState_update"
definition setCurrentNode where "setCurrentNode = sets currentNode_update"
definition setCurrentTerm where "setCurrentTerm = sets currentTerm_update"
definition setCurrentVotingNodes where "setCurrentVotingNodes = sets currentVotingNodes_update"
definition setElectionWon where "setElectionWon = sets electionWon_update"
definition setFirstUncommittedSlot where "setFirstUncommittedSlot = sets firstUncommittedSlot_update"
definition setJoinVotes where "setJoinVotes = sets joinVotes_update"
definition setLastAcceptedData where "setLastAcceptedData = sets lastAcceptedData_update"
definition setPublishPermitted where "setPublishPermitted = sets publishPermitted_update"
definition setPublishVotes where "setPublishVotes = sets publishVotes_update"
definition modifies where "modifies f g = modifyNodeData (f g)"
definition modifyJoinVotes where "modifyJoinVotes = modifies joinVotes_update"
definition modifyPublishVotes where "modifyPublishVotes = modifies publishVotes_update"
definition modifyCurrentClusterState where "modifyCurrentClusterState = modifies currentClusterState_update"
definition "when" :: "bool \<Rightarrow> unit Action \<Rightarrow> unit Action" where "when c a \<equiv> if c then a else return ()"
definition unless :: "bool \<Rightarrow> unit Action \<Rightarrow> unit Action" where "unless \<equiv> when \<circ> Not"
lemma runM_when: "runM (when c a) nd = (if c then runM a nd else (nd, [], Success ()))"
by (auto simp add: when_def)
lemma runM_unless: "runM (unless c a) nd = (if c then (nd, [], Success ()) else runM a nd)"
by (auto simp add: unless_def when_def)
lemma runM_when_continue: "runM (do { when c a; b }) nd = (if c then runM (do {a;b}) nd else runM b nd)"
by (auto simp add: when_def)
lemma runM_unless_continue: "runM (do { unless c a; b }) nd = (if c then runM b nd else runM (do {a;b}) nd)"
by (auto simp add: unless_def when_def)
lemma catch_when[simp]: "catch (when c a) onException = when c (catch a onException)"
by (intro runM_inject, simp add: catch_def runM_when)
lemma catch_unless[simp]: "catch (unless c a) onException = unless c (catch a onException)"
by (intro runM_inject, simp add: catch_def runM_unless)
lemma catch_when_continue[simp]: "catch (do { when c a; b }) onException = (if c then catch (do {a;b}) onException else catch b onException)"
by (intro runM_inject, simp add: catch_def runM_when_continue)
lemma catch_unless_continue[simp]: "catch (do { unless c a; b }) onException = (if c then catch b onException else catch (do {a;b}) onException)"
by (intro runM_inject, simp add: catch_def runM_unless_continue)
definition ensureCorrectDestination :: "Destination \<Rightarrow> unit Action"
where "ensureCorrectDestination d \<equiv> do {
n <- getCurrentNode;
when (d \<notin> { Broadcast, OneNode n }) (throw IllegalArgumentException)
}"
lemma runM_ensureCorrectDestination_continue:
"runM (do { ensureCorrectDestination d; go }) nd = (if d \<in> { Broadcast, OneNode (currentNode nd) } then runM go nd else (nd, [], Exception IllegalArgumentException))"
by (simp add: ensureCorrectDestination_def getCurrentNode_def gets_def runM_when_continue)
definition broadcast :: "Message \<Rightarrow> unit Action"
where "broadcast msg \<equiv> do {
n <- getCurrentNode;
send \<lparr> sender = n, destination = Broadcast, payload = msg \<rparr>
}"
lemma runM_broadcast[simp]: "runM (broadcast msg) nd = (nd, [\<lparr> sender = currentNode nd, destination = Broadcast, payload = msg \<rparr>], Success ())"
by (simp add: broadcast_def getCurrentNode_def gets_def send_def)
definition sendTo :: "Node \<Rightarrow> Message \<Rightarrow> unit Action"
where "sendTo d msg \<equiv> do {
n <- getCurrentNode;
send \<lparr> sender = n, destination = OneNode d, payload = msg \<rparr>
}"
lemma runM_sendTo[simp]: "runM (sendTo d msg) nd = (nd, [\<lparr> sender = currentNode nd, destination = OneNode d, payload = msg \<rparr>], Success ())"
by (simp add: sendTo_def getCurrentNode_def gets_def send_def)
definition ignoringExceptions :: "unit Action \<Rightarrow> unit Action" where "ignoringExceptions go \<equiv> catch go (\<lambda>_. return ())"
lemma None_lt[simp]: "NO_TERM < t = (t \<noteq> NO_TERM)" by (cases t, simp_all)
definition getLastAcceptedTerm :: "TermOption Action"
where
"getLastAcceptedTerm \<equiv> do {
lastAcceptedData <- getLastAcceptedData;
case lastAcceptedData of
None \<Rightarrow> return NO_TERM
| Some tv \<Rightarrow> return (SomeTerm (tvTerm tv))
}"
definition doStartJoin :: "Node \<Rightarrow> Term \<Rightarrow> unit Action"
where
"doStartJoin newMaster newTerm \<equiv> do {
currentTerm <- getCurrentTerm;
when (newTerm \<le> currentTerm) (throw IllegalArgumentException);
setCurrentTerm newTerm;
setJoinVotes {};
setElectionWon False;
setPublishPermitted True;
setPublishVotes {};
firstUncommittedSlot <- getFirstUncommittedSlot;
lastAcceptedTerm <- getLastAcceptedTerm;
sendTo newMaster (Vote firstUncommittedSlot newTerm lastAcceptedTerm)
}"
definition doVote :: "Node \<Rightarrow> Slot \<Rightarrow> Term \<Rightarrow> TermOption \<Rightarrow> unit Action"
where
"doVote sourceNode voteFirstUncommittedSlot voteTerm voteLastAcceptedTerm \<equiv> do {
currentTerm <- getCurrentTerm;
when (voteTerm \<noteq> currentTerm) (throw IllegalArgumentException);
firstUncommittedSlot <- getFirstUncommittedSlot;
when (voteFirstUncommittedSlot > firstUncommittedSlot) (throw IllegalArgumentException);
lastAcceptedTerm <- getLastAcceptedTerm;
when (voteFirstUncommittedSlot = firstUncommittedSlot
\<and> voteLastAcceptedTerm > lastAcceptedTerm)
(throw IllegalArgumentException);
modifyJoinVotes (insert sourceNode);
joinVotes <- getJoinVotes;
currentVotingNodes <- getCurrentVotingNodes;
let electionWon' = card (joinVotes \<inter> currentVotingNodes) * 2 > card currentVotingNodes;
setElectionWon electionWon';
publishPermitted <- getPublishPermitted;
when (electionWon' \<and> publishPermitted \<and> lastAcceptedTerm \<noteq> NO_TERM) (do {
setPublishPermitted False;
lastAcceptedValue <- gets lastAcceptedValue; (* NB must be present since lastAcceptedTermInSlot \<noteq> NO_TERM *)
broadcast (PublishRequest firstUncommittedSlot currentTerm lastAcceptedValue)
})
}"
definition doPublishRequest :: "Node \<Rightarrow> Slot \<Rightarrow> TermValue \<Rightarrow> unit Action"
where
"doPublishRequest sourceNode requestSlot newAcceptedState \<equiv> do {
currentTerm <- getCurrentTerm;
when (tvTerm newAcceptedState \<noteq> currentTerm) (throw IllegalArgumentException);
firstUncommittedSlot <- getFirstUncommittedSlot;
when (requestSlot \<noteq> firstUncommittedSlot) (throw IllegalArgumentException);
setLastAcceptedData (Some newAcceptedState);
sendTo sourceNode (PublishResponse requestSlot (tvTerm newAcceptedState))
}"
record SlotTerm =
stSlot :: Slot
stTerm :: Term
definition ApplyCommitFromSlotTerm :: "SlotTerm \<Rightarrow> Message"
where "ApplyCommitFromSlotTerm st = ApplyCommit (stSlot st) (stTerm st)"
definition doPublishResponse :: "Node \<Rightarrow> SlotTerm \<Rightarrow> unit Action"
where
"doPublishResponse sourceNode slotTerm \<equiv> do {
currentTerm <- getCurrentTerm;
when (stTerm slotTerm \<noteq> currentTerm) (throw IllegalArgumentException);
firstUncommittedSlot <- getFirstUncommittedSlot;
when (stSlot slotTerm \<noteq> firstUncommittedSlot) (throw IllegalArgumentException);
modifyPublishVotes (insert sourceNode);
publishVotes <- getPublishVotes;
currentVotingNodes <- getCurrentVotingNodes;
when (card (publishVotes \<inter> currentVotingNodes) * 2 > card currentVotingNodes)
(broadcast (ApplyCommitFromSlotTerm slotTerm))
}"
definition doCommit :: "SlotTerm \<Rightarrow> unit Action"
where
"doCommit slotTerm \<equiv> do {
lastAcceptedTermInSlot <- getLastAcceptedTerm;
when (SomeTerm (stTerm slotTerm) \<noteq> lastAcceptedTermInSlot) (throw IllegalArgumentException);
firstUncommittedSlot <- getFirstUncommittedSlot;
when (stSlot slotTerm \<noteq> firstUncommittedSlot) (throw IllegalArgumentException);
lastAcceptedValue <- gets lastAcceptedValue; (* NB must be not None since lastAcceptedTerm = Some t *)
(case lastAcceptedValue of
ClusterStateDiff diff
\<Rightarrow> modifyCurrentClusterState diff
| Reconfigure votingNodes \<Rightarrow> do {
setCurrentVotingNodes (set votingNodes);
joinVotes <- getJoinVotes;
setElectionWon (card (joinVotes \<inter> (set votingNodes)) * 2 > card (set votingNodes))
}
| NoOp \<Rightarrow> return ());
setFirstUncommittedSlot (firstUncommittedSlot + 1);
setLastAcceptedData None;
setPublishPermitted True;
setPublishVotes {}
}"
definition generateCatchup :: "Node \<Rightarrow> unit Action"
where
"generateCatchup sourceNode \<equiv> do {
firstUncommittedSlot <- getFirstUncommittedSlot;
currentVotingNodes <- getCurrentVotingNodes;
currentClusterState <- getCurrentClusterState;
sendTo sourceNode (CatchUpResponse firstUncommittedSlot currentVotingNodes currentClusterState)
}"
definition applyCatchup :: "Slot \<Rightarrow> Node set \<Rightarrow> ClusterState \<Rightarrow> unit Action"
where
"applyCatchup catchUpSlot catchUpConfiguration catchUpState \<equiv> do {
firstUncommittedSlot <- getFirstUncommittedSlot;
when (catchUpSlot \<le> firstUncommittedSlot) (throw IllegalArgumentException);
setFirstUncommittedSlot catchUpSlot;
setCurrentVotingNodes catchUpConfiguration;
setCurrentClusterState catchUpState;
setLastAcceptedData None;
setJoinVotes {};
setElectionWon False;
setPublishVotes {};
setPublishPermitted True
}"
definition doClientValue :: "Value \<Rightarrow> unit Action"
where
"doClientValue x \<equiv> do {
electionWon <- getElectionWon;
when (\<not> electionWon) (throw IllegalArgumentException);
publishPermitted <- getPublishPermitted;
when (\<not> publishPermitted) (throw IllegalArgumentException);
lastAcceptedTermInSlot <- getLastAcceptedTerm;
when (lastAcceptedTermInSlot \<noteq> NO_TERM) (throw IllegalArgumentException);
setPublishPermitted False;
currentTerm <- getCurrentTerm;
firstUncommittedSlot <- getFirstUncommittedSlot;
broadcast (PublishRequest firstUncommittedSlot currentTerm x)
}"
definition doDiscardJoinVotes :: "unit Action"
where
"doDiscardJoinVotes \<equiv> do {
setJoinVotes {};
setElectionWon False
}"
definition doReboot :: "unit Action"
where
"doReboot \<equiv> modifyNodeData (\<lambda>nd.
(* persistent fields *)
\<lparr> currentNode = currentNode nd
, currentTerm = currentTerm nd
, firstUncommittedSlot = firstUncommittedSlot nd
, currentVotingNodes = currentVotingNodes nd
, currentClusterState = currentClusterState nd
, lastAcceptedData = lastAcceptedData nd
(* transient fields *)
, joinVotes = {}
, electionWon = False
, publishPermitted = False
, publishVotes = {} \<rparr>)"
definition ProcessMessageAction :: "RoutedMessage \<Rightarrow> unit Action"
where "ProcessMessageAction rm \<equiv> Action (\<lambda>nd. case ProcessMessage nd rm of (nd', messageOption) \<Rightarrow> (nd', case messageOption of None \<Rightarrow> [] | Some m \<Rightarrow> [m], Success ()))"
definition dispatchMessageInner :: "RoutedMessage \<Rightarrow> unit Action"
where "dispatchMessageInner m \<equiv> case payload m of
StartJoin t \<Rightarrow> doStartJoin (sender m) t
| Vote i t a \<Rightarrow> doVote (sender m) i t a
| ClientValue x \<Rightarrow> doClientValue x
| PublishRequest i t x \<Rightarrow> doPublishRequest (sender m) i \<lparr> tvTerm = t, tvValue = x \<rparr>
| PublishResponse i t \<Rightarrow> doPublishResponse (sender m) \<lparr> stSlot = i, stTerm = t \<rparr>
| ApplyCommit i t \<Rightarrow> doCommit \<lparr> stSlot = i, stTerm = t \<rparr>
| CatchUpRequest \<Rightarrow> generateCatchup (sender m)
| CatchUpResponse i conf cs \<Rightarrow> applyCatchup i conf cs
| DiscardJoinVotes \<Rightarrow> doDiscardJoinVotes
| Reboot \<Rightarrow> doReboot"
definition dispatchMessage :: "RoutedMessage \<Rightarrow> unit Action"
where "dispatchMessage m \<equiv> ignoringExceptions (do {
ensureCorrectDestination (destination m);
dispatchMessageInner m
})"
lemma getLastAcceptedTermInSlot_gets[simp]: "getLastAcceptedTerm = gets lastAcceptedTerm"
proof (intro runM_inject)
fix nd
show "runM getLastAcceptedTerm nd = runM (gets lastAcceptedTerm) nd"
by (cases "lastAcceptedData nd", simp_all add: gets_def getLastAcceptedTerm_def getLastAcceptedData_def
getFirstUncommittedSlot_def lastAcceptedTerm_def)
qed
lemma monadic_implementation_is_faithful:
"dispatchMessage = ProcessMessageAction"
proof (intro ext runM_inject)
fix rm nd
show "runM (dispatchMessage rm) nd = runM (ProcessMessageAction rm) nd" (is "?LHS = ?RHS")
proof (cases "destination rm \<in> {Broadcast, OneNode (currentNode nd)}")
case False
hence 1: "\<And>f. runM (do { ensureCorrectDestination (destination rm); f }) nd = (nd, [], Exception IllegalArgumentException)"
by (simp add: runM_ensureCorrectDestination_continue)
from False
show ?thesis
unfolding ProcessMessageAction_def dispatchMessage_def
by (simp add: ignoringExceptions_def catch_def 1 ProcessMessage_def)
next
case dest_ok: True
hence 1: "runM (dispatchMessage rm) nd = runM (ignoringExceptions (dispatchMessageInner rm)) nd"
by (simp add: dispatchMessage_def ignoringExceptions_def catch_def runM_ensureCorrectDestination_continue)
also have "... = runM (ProcessMessageAction rm) nd" (is "?LHS = ?RHS")
proof (cases "payload rm")
case (StartJoin t)
have "?LHS = runM (ignoringExceptions (doStartJoin (sender rm) t)) nd" (is "_ = ?STEP")
by (simp add: dispatchMessageInner_def StartJoin)
also consider
(a) "t \<le> currentTerm nd"
| (b) "currentTerm nd < t" "case lastAcceptedTerm nd of NO_TERM \<Rightarrow> False | SomeTerm x \<Rightarrow> t \<le> x"
| (c) "currentTerm nd < t" "case lastAcceptedTerm nd of NO_TERM \<Rightarrow> True | SomeTerm x \<Rightarrow> x < t"
proof (cases "t \<le> currentTerm nd")
case True thus ?thesis by (intro a)
next
case 1: False
with b c show ?thesis
by (cases "case lastAcceptedTerm nd of NO_TERM \<Rightarrow> False | SomeTerm x \<Rightarrow> t \<le> x", auto, cases "lastAcceptedTerm nd", auto)
qed
hence "?STEP = ?RHS"
proof cases
case a
thus ?thesis
by (simp add: StartJoin ProcessMessageAction_def dispatchMessage_def ProcessMessage_def Let_def runM_unless
doStartJoin_def getCurrentTerm_def gets_def setJoinVotes_def sets_def setCurrentTerm_def
setPublishPermitted_def setPublishVotes_def getFirstUncommittedSlot_def handleStartJoin_def ensureCurrentTerm_def setElectionWon_def
ignoringExceptions_def catch_def runM_when_continue)
next
case b
with StartJoin dest_ok show ?thesis
by (cases "lastAcceptedTerm nd ", simp_all add: ProcessMessageAction_def dispatchMessage_def ProcessMessage_def Let_def
doStartJoin_def getCurrentTerm_def gets_def setJoinVotes_def sets_def setCurrentTerm_def runM_unless lastAcceptedTerm_def
setPublishPermitted_def setPublishVotes_def getFirstUncommittedSlot_def handleStartJoin_def ensureCurrentTerm_def setElectionWon_def
ignoringExceptions_def catch_def runM_when_continue)
next
case c with StartJoin dest_ok show ?thesis
by (cases "lastAcceptedTerm nd", simp_all add: ProcessMessageAction_def dispatchMessage_def ProcessMessage_def Let_def
doStartJoin_def getCurrentTerm_def gets_def setJoinVotes_def sets_def setCurrentTerm_def runM_unless lastAcceptedTerm_def
setPublishPermitted_def setPublishVotes_def getFirstUncommittedSlot_def handleStartJoin_def ensureCurrentTerm_def setElectionWon_def
ignoringExceptions_def catch_def runM_when_continue)
qed
finally show ?thesis by simp
next
case (Vote i t a)
have "?LHS = runM (ignoringExceptions (doVote (sender rm) i t a)) nd" (is "_ = ?STEP")
by (simp add: dispatchMessageInner_def Vote)
also have "... = ?RHS"
proof (cases "firstUncommittedSlot nd < i")
case True
with Vote dest_ok show ?thesis
by (simp add: dispatchMessage_def runM_unless
doVote_def gets_def getFirstUncommittedSlot_def ProcessMessage_def
ProcessMessageAction_def handleVote_def ignoringExceptions_def getCurrentTerm_def)
next
case False hence le: "i \<le> firstUncommittedSlot nd" by simp
show ?thesis
proof (cases "t = currentTerm nd")
case False
with Vote dest_ok le show ?thesis
by (simp add: dispatchMessage_def runM_when runM_unless
doVote_def gets_def getFirstUncommittedSlot_def getCurrentTerm_def
ProcessMessage_def ProcessMessageAction_def handleVote_def ignoringExceptions_def)
next
case t: True
show ?thesis
proof (cases "i = firstUncommittedSlot nd")
case False
with Vote dest_ok le t show ?thesis
by (simp add: dispatchMessage_def Let_def runM_when_continue
doVote_def runM_when runM_unless
gets_def getFirstUncommittedSlot_def getCurrentTerm_def
getJoinVotes_def getCurrentVotingNodes_def
getPublishPermitted_def ignoringExceptions_def broadcast_def getCurrentNode_def
modifies_def modifyJoinVotes_def send_def
sets_def setElectionWon_def setPublishPermitted_def lastAcceptedValue_def
ProcessMessage_def ProcessMessageAction_def handleVote_def
addElectionVote_def publishValue_def isQuorum_def majorities_def)
next
case i: True
show ?thesis
proof (cases a)
case a: NO_TERM
show ?thesis
proof (cases "isQuorum nd (insert (sender rm) (joinVotes nd))")
case not_quorum: False
hence not_quorum_card: "\<not> card (currentVotingNodes nd) < card (insert (sender rm) (joinVotes nd) \<inter> currentVotingNodes nd) * 2"
by (simp add: isQuorum_def majorities_def)
have "?STEP = (nd\<lparr>electionWon := False, joinVotes := insert (sender rm) (joinVotes nd)\<rparr>, [], Success ())"
by (simp add: ignoringExceptions_def i t a doVote_def catch_def
gets_def getCurrentTerm_def runM_when_continue getFirstUncommittedSlot_def
modifyJoinVotes_def modifies_def getJoinVotes_def
getCurrentVotingNodes_def Let_def setElectionWon_def sets_def runM_when
not_quorum_card getPublishPermitted_def)
also from dest_ok have "... = ?RHS"
by (simp add: ProcessMessageAction_def ProcessMessage_def Vote handleVote_def
i t a addElectionVote_def not_quorum publishValue_def Let_def)
finally show ?thesis .
next
case quorum: True
hence quorum_card: "card (currentVotingNodes nd) < card (insert (sender rm) (joinVotes nd) \<inter> currentVotingNodes nd) * 2"
by (simp add: isQuorum_def majorities_def)
show ?thesis
proof (cases "publishPermitted nd \<and> lastAcceptedTerm nd \<noteq> NO_TERM")
case False
hence "?STEP = (nd\<lparr>electionWon := True, joinVotes := insert (sender rm) (joinVotes nd)\<rparr>, [], Success ())"
by (auto simp add: ignoringExceptions_def i t a doVote_def catch_def
gets_def getCurrentTerm_def runM_when_continue getFirstUncommittedSlot_def
modifyJoinVotes_def modifies_def getJoinVotes_def
getCurrentVotingNodes_def Let_def setElectionWon_def sets_def runM_when
quorum_card getPublishPermitted_def)
also from False dest_ok have "... = ?RHS"
by (simp add: ProcessMessageAction_def ProcessMessage_def Vote handleVote_def
i t a addElectionVote_def quorum publishValue_def Let_def)
finally show ?thesis .
next
case True
hence "?STEP = (nd\<lparr>electionWon := True, publishPermitted := False,
joinVotes := insert (sender rm) (joinVotes nd)\<rparr>,
[\<lparr>sender = currentNode nd, destination = Broadcast,
payload = PublishRequest (firstUncommittedSlot nd) (currentTerm nd)
(lastAcceptedValue nd) \<rparr>], Success ())"
by (auto simp add: ignoringExceptions_def i t a doVote_def catch_def
gets_def getCurrentTerm_def runM_when_continue getFirstUncommittedSlot_def
modifyJoinVotes_def modifies_def getJoinVotes_def
getCurrentVotingNodes_def Let_def setElectionWon_def sets_def runM_when
quorum_card getPublishPermitted_def setPublishPermitted_def lastAcceptedValue_def)
also from True dest_ok have "... = ?RHS"
by (simp add: ProcessMessageAction_def ProcessMessage_def Vote handleVote_def
i t a addElectionVote_def quorum publishValue_def Let_def lastAcceptedValue_def)
finally show ?thesis .
qed
qed
next
case a: (SomeTerm voteLastAcceptedTerm)
show ?thesis
proof (cases "lastAcceptedTerm nd")
case lat: NO_TERM
have "?STEP = (nd, [], Success ())"
by (auto simp add: ignoringExceptions_def i t a lat doVote_def catch_def
gets_def getCurrentTerm_def runM_when_continue getFirstUncommittedSlot_def)
also from dest_ok have "... = ?RHS"
by (simp add: ProcessMessageAction_def ProcessMessage_def Vote handleVote_def
i t a lat)
finally show ?thesis .
next
case lat: (SomeTerm nodeLastAcceptedTerm)
show ?thesis
proof (cases "voteLastAcceptedTerm \<le> nodeLastAcceptedTerm")
case False
hence "?STEP = (nd, [], Success ())"
by (simp add: ignoringExceptions_def i t a lat doVote_def catch_def
gets_def getCurrentTerm_def runM_when_continue getFirstUncommittedSlot_def)
also from False dest_ok have "... = ?RHS"
by (simp add: ProcessMessageAction_def ProcessMessage_def Vote handleVote_def
i t a lat max_def addElectionVote_def publishValue_def)
finally show ?thesis by simp
next
case True
show ?thesis
proof (cases "isQuorum nd (insert (sender rm) (joinVotes nd))")
case not_quorum: False
hence not_quorum_card: "\<not> card (currentVotingNodes nd) < card (insert (sender rm) (joinVotes nd) \<inter> currentVotingNodes nd) * 2"
by (simp add: isQuorum_def majorities_def)
from True
have "?STEP = (nd\<lparr>electionWon := False,
joinVotes := insert (sender rm) (joinVotes nd)\<rparr>, [], Success ())"
by (simp add: ignoringExceptions_def i t a lat doVote_def catch_def
gets_def getCurrentTerm_def runM_when_continue getFirstUncommittedSlot_def
modifyJoinVotes_def modifies_def getJoinVotes_def
getCurrentVotingNodes_def Let_def setElectionWon_def sets_def runM_when
not_quorum_card getPublishPermitted_def)
also from dest_ok True have "... = ?RHS"
by (simp add: ProcessMessageAction_def ProcessMessage_def Vote handleVote_def
i t a lat max_def addElectionVote_def not_quorum publishValue_def Let_def)
finally show ?thesis .
next
case quorum: True
hence quorum_card: "card (currentVotingNodes nd) < card (insert (sender rm) (joinVotes nd) \<inter> currentVotingNodes nd) * 2"
by (simp add: isQuorum_def majorities_def)
show ?thesis
proof (cases "publishPermitted nd")
case False
with True
have "?STEP = (nd\<lparr>electionWon := True,
joinVotes := insert (sender rm) (joinVotes nd)\<rparr>, [], Success ())"
by (simp add: ignoringExceptions_def i t a lat doVote_def catch_def
gets_def getCurrentTerm_def runM_when_continue getFirstUncommittedSlot_def
modifyJoinVotes_def modifies_def getJoinVotes_def
getCurrentVotingNodes_def Let_def setElectionWon_def sets_def runM_when
quorum_card getPublishPermitted_def setPublishPermitted_def)
also from False dest_ok have "... = ?RHS"
by (simp add: ProcessMessageAction_def ProcessMessage_def Vote handleVote_def
i t a lat True addElectionVote_def quorum publishValue_def Let_def)
finally show ?thesis .
next
case publishPermitted: True
have "?STEP = (nd\<lparr>electionWon := True, publishPermitted := False,
joinVotes := insert (sender rm) (joinVotes nd)\<rparr>,
[\<lparr>sender = currentNode nd, destination = Broadcast,
payload = PublishRequest (firstUncommittedSlot nd) (currentTerm nd)
(lastAcceptedValue nd) \<rparr>], Success ())"
apply (auto simp add: ignoringExceptions_def i t a lat True doVote_def catch_def
gets_def getCurrentTerm_def runM_when_continue getFirstUncommittedSlot_def
modifyJoinVotes_def modifies_def getJoinVotes_def
getCurrentVotingNodes_def Let_def setElectionWon_def sets_def runM_when
quorum_card getPublishPermitted_def setPublishPermitted_def lastAcceptedValue_def)
using True publishPermitted by auto
also from publishPermitted True dest_ok have "... = ?RHS"
by (simp add: ProcessMessageAction_def ProcessMessage_def Vote handleVote_def
i t a lat True addElectionVote_def quorum publishValue_def Let_def lastAcceptedValue_def)
finally show ?thesis .
qed
qed
qed
qed
qed
qed
qed
qed
finally show ?thesis .
next
case (ClientValue x)
with dest_ok show ?thesis
by (simp add: ProcessMessageAction_def dispatchMessageInner_def
doClientValue_def gets_def getElectionWon_def
runM_unless getPublishPermitted_def setPublishPermitted_def sets_def
getCurrentTerm_def getFirstUncommittedSlot_def ProcessMessage_def handleClientValue_def
publishValue_def runM_when ignoringExceptions_def ClientValue catch_def runM_when_continue)
next
case (PublishRequest i t x) with dest_ok show ?thesis
by (simp add: ProcessMessageAction_def dispatchMessageInner_def
doPublishRequest_def gets_def getCurrentTerm_def getFirstUncommittedSlot_def
sets_def setLastAcceptedData_def ignoringExceptions_def catch_def runM_when_continue
getCurrentNode_def runM_unless send_def
ProcessMessage_def handlePublishRequest_def runM_when)
next
case (PublishResponse i t) with dest_ok show ?thesis
by (simp add: ProcessMessageAction_def dispatchMessageInner_def
doPublishResponse_def gets_def getCurrentTerm_def getFirstUncommittedSlot_def
broadcast_def getCurrentNode_def runM_unless send_def
modifyPublishVotes_def modifies_def getPublishVotes_def getCurrentVotingNodes_def
runM_when ignoringExceptions_def catch_def runM_when_continue
ProcessMessage_def handlePublishResponse_def commitIfQuorate_def isQuorum_def majorities_def
ApplyCommitFromSlotTerm_def)
next
case (ApplyCommit i t)
show ?thesis
proof (cases "lastAcceptedValue nd")
case NoOp
with ApplyCommit dest_ok show ?thesis
by (simp add: ProcessMessageAction_def dispatchMessageInner_def
doCommit_def runM_unless runM_when
gets_def getFirstUncommittedSlot_def
sets_def setFirstUncommittedSlot_def setLastAcceptedData_def
setPublishPermitted_def setPublishVotes_def
ProcessMessage_def handleApplyCommit_def applyAcceptedValue_def
ignoringExceptions_def catch_def runM_when_continue)
next
case Reconfigure
with ApplyCommit dest_ok show ?thesis
by (simp add: ProcessMessageAction_def dispatchMessageInner_def
doCommit_def runM_unless runM_when
gets_def getFirstUncommittedSlot_def
getJoinVotes_def
sets_def setFirstUncommittedSlot_def setLastAcceptedData_def
setPublishPermitted_def setPublishVotes_def
setCurrentVotingNodes_def setElectionWon_def
ProcessMessage_def handleApplyCommit_def applyAcceptedValue_def majorities_def
ignoringExceptions_def catch_def runM_when_continue)
next
case ClusterStateDiff
with ApplyCommit dest_ok show ?thesis
by (simp add: ProcessMessageAction_def dispatchMessageInner_def
doCommit_def runM_unless runM_when
gets_def getFirstUncommittedSlot_def
sets_def setFirstUncommittedSlot_def
modifies_def modifyCurrentClusterState_def
setPublishPermitted_def setPublishVotes_def setLastAcceptedData_def
ProcessMessage_def handleApplyCommit_def applyAcceptedValue_def
ignoringExceptions_def catch_def runM_when_continue)
qed
next
case CatchUpRequest
with dest_ok show ?thesis
by (simp add: ProcessMessageAction_def dispatchMessageInner_def
generateCatchup_def
gets_def getFirstUncommittedSlot_def getCurrentVotingNodes_def getCurrentClusterState_def
ProcessMessage_def handleCatchUpRequest_def ignoringExceptions_def catch_def runM_when_continue)
next
case (CatchUpResponse i conf cs)
with dest_ok show ?thesis
by (simp add: ProcessMessageAction_def dispatchMessageInner_def
applyCatchup_def gets_def getFirstUncommittedSlot_def
sets_def setFirstUncommittedSlot_def
setPublishPermitted_def setPublishVotes_def setLastAcceptedData_def
setCurrentVotingNodes_def setCurrentClusterState_def setJoinVotes_def
setElectionWon_def runM_unless
ProcessMessage_def handleCatchUpResponse_def
ignoringExceptions_def catch_def runM_when_continue)
next
case Reboot
with dest_ok show ?thesis
by (simp add: ProcessMessageAction_def dispatchMessageInner_def
doReboot_def ProcessMessage_def handleReboot_def ignoringExceptions_def catch_def runM_when_continue)
next
case DiscardJoinVotes
with dest_ok show ?thesis
by (simp add: ProcessMessageAction_def dispatchMessageInner_def
doDiscardJoinVotes_def ProcessMessage_def handleDiscardJoinVotes_def ignoringExceptions_def catch_def
runM_when_continue setJoinVotes_def sets_def setElectionWon_def)
qed
finally show ?thesis .
qed
qed
end