-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_chat.thy
1195 lines (885 loc) · 45.6 KB
/
example_chat.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 "Example: Chat application"
theory example_chat
imports
program_verification_tactics
impl_language_loops
crdt_specs2
unique_ids
program_proof_rules_loops
app_verification_helpers
unique_ids_loops
proof_state_facts
begin
text_raw \<open>\DefineSnippet{chat_example_val}{\<close>
datatype val =
String string
| Bool bool
| Undef
| UserId int
| ChatId int
| MessageId int
| Found val val
| NotFound
text_raw \<open>}%EndSnippet\<close>
instance val :: countable
by countable_datatype
fun uniqueIds_val_r where
"uniqueIds_val_r (UserId i) = {}" \<comment> \<open>These are not considered Ids as they are not generated by this part of the app.\<close>
| "uniqueIds_val_r (MessageId i) = {to_nat i}"
| "uniqueIds_val_r (Found x y) = uniqueIds_val_r x \<union> uniqueIds_val_r y"
| "uniqueIds_val_r _ = {}"
instantiation val :: valueType begin
definition [simp]: "uniqueIds_val \<equiv> uniqueIds_val_r"
definition [simp]: "default_val \<equiv> Undef"
instance by (standard, auto)
end
instantiation val :: from_bool begin
definition [simp]: "from_bool_val \<equiv> Bool"
instance by (standard, auto)
end
fun stringval where
"stringval (String s) = s"
| "stringval _ = ''''"
text_raw \<open>\DefineSnippet{chat_example_messageDataOp}{\<close>
datatype messageDataOp =
Author "(val registerOp)"
| Content "(val registerOp)"
text_raw \<open>}%EndSnippet\<close>
instance messageDataOp :: countable
by countable_datatype
instantiation messageDataOp :: crdt_op begin
definition "uniqueIds_messageDataOp x \<equiv>
case x of
Author x \<Rightarrow> uniqueIds x
| Content x \<Rightarrow> uniqueIds x"
lemma [simp]: "uniqueIds (Author x) = uniqueIds x"
"uniqueIds (Content x) = uniqueIds x"
by (auto simp add: uniqueIds_messageDataOp_def)
definition [simp]: "default_messageDataOp = Author default"
definition "is_update_messageDataOp x \<equiv> case x of Author x \<Rightarrow> is_update x | Content x \<Rightarrow> is_update x"
lemma is_update_messageDataOp_simp[simp]:
"is_update (Author x) = is_update x"
"is_update (Content y) = is_update y"
by (auto simp add: is_update_messageDataOp_def)
instance by (standard, auto)
end
definition "isMessageId x \<equiv> case x of MessageId _ \<Rightarrow> True | _ \<Rightarrow> False"
text_raw \<open>\DefineSnippet{chat_example_operation}{\<close>
datatype operation =
Chat "(val setOp)"
| Message "((val, messageDataOp) mapOp)"
text_raw \<open>}%EndSnippet\<close>
instance operation :: countable
by countable_datatype
instantiation operation :: crdt_op begin
definition "uniqueIds_operation x \<equiv>
case x of
Chat x \<Rightarrow> uniqueIds x
| Message x \<Rightarrow> uniqueIds x"
lemma [simp]: "uniqueIds (Chat x) = uniqueIds x"
"uniqueIds (Message y) = uniqueIds y"
by (auto simp add: uniqueIds_operation_def)
definition [simp]: "default_operation = Chat default"
definition "is_update_operation x \<equiv> case x of Chat x \<Rightarrow> is_update x | Message x \<Rightarrow> is_update x"
instance by (standard, auto)
end
definition messageStruct :: "(messageDataOp, val) crdtSpec" where
"messageStruct \<equiv>
struct_field Author (register_spec Undef)
.\<or>. struct_field Content (register_spec Undef)
"
definition crdtSpec :: "(operation, val) crdtSpec" where
"crdtSpec \<equiv>
struct_field Message (map_sdw_spec messageStruct)
.\<or>. struct_field Chat set_rw_spec
"
definition messageStruct' :: "('a, messageDataOp, val) ccrdtSpec" where
"messageStruct' \<equiv>
struct_field' Author (register_spec' Undef)
.\<or>.. struct_field' Content (register_spec' Undef)
"
definition crdtSpec' :: "(operation, operation, val) ccrdtSpec" where
"crdtSpec' \<equiv>
struct_field' Message (map_sdw_spec' messageStruct')
.\<or>.. struct_field' Chat set_rw_spec'
"
lemma subset_map_map:
assumes "Cs \<subseteq> dom (map_map (calls ctxt) call_operation \<ggreater> C_in)"
shows "Cs \<subseteq> dom (calls ctxt)"
by (smt assms domI dom_map_map in_dom map_chain_eq_some subsetI)
lemma crdtSpec_rel:
shows "crdt_spec_rel crdtSpec crdtSpec'"
unfolding crdtSpec_def crdtSpec'_def
by (auto simp add: inj_def messageStruct_def messageStruct'_def)
lemma crdtSpec'_alt:
assumes wf: "operationContext_wf ctxt"
shows "crdtSpec oper ctxt res
\<longleftrightarrow> crdtSpec' oper (dom (calls ctxt)) (extract_op (calls ctxt)) (happensBefore ctxt) id res "
by (simp add: crdtSpec_rel local.wf operationContext_wf_hb_field use_crdt_spec_rel_toplevel)
lemma crdtSpec'_wf:
"ccrdtSpec_wf crdtSpec'"
unfolding crdtSpec'_def
by (auto simp add: messageStruct'_def split: operation.splits )
lemma crdtSpec'_simp_op:
assumes "Cs \<subseteq> vis"
shows
"crdtSpec' oper Cs (extract_op (s_calls |` vis)) s_happensBefore id r
= crdtSpec' oper Cs (extract_op (s_calls)) s_happensBefore id r"
apply (rule use_ccrdtSpec_wf[OF crdtSpec'_wf])
using assms by (auto simp add: map_same_on_def restrict_map_def extract_op_def split: option.splits call.splits)
lemma crdtSpec'_simp_hb:
assumes "Cs \<subseteq> vis"
shows "
crdtSpec' oper Cs op (s_happensBefore |r vis) id r
= crdtSpec' oper Cs op s_happensBefore id r"
apply (rule use_ccrdtSpec_wf[OF crdtSpec'_wf])
using assms by (auto simp add: rel_same_on_def restrict_relation_def split: option.splits call.splits)
text_raw \<open>\DefineSnippet{sendMessage_impl}{\<close>
definition sendMessage_impl :: "val \<Rightarrow> val \<Rightarrow> (val,operation,val) io" where
"sendMessage_impl from content \<equiv> do {
m \<leftarrow> newId isMessageId;
atomic (do {
call (Message (NestedOp m (Author (Assign from))));
call (Message (NestedOp m (Content (Assign content))));
call (Chat (Add m))
});
return m
}"
text_raw \<open>}%EndSnippet\<close>
text_raw \<open>\DefineSnippet{editMessage_impl}{\<close>
definition editMessage_impl :: "val \<Rightarrow> val \<Rightarrow> (val,operation,val) io" where
"editMessage_impl m newContent \<equiv> do {
atomic (do {
exists \<leftarrow> call (Message (KeyExists m));
if exists = Bool True then
call (Message (NestedOp m (Content (Assign newContent))))
else
skip
})
}"
text_raw \<open>}%EndSnippet\<close>
text_raw \<open>\DefineSnippet{deleteMessage_impl}{\<close>
definition deleteMessage_impl :: "val \<Rightarrow> (val,operation,val) io" where
"deleteMessage_impl m \<equiv> do {
atomic (do {
call (Chat (Remove m));
call (Message (DeleteKey m))
})
}"
text_raw \<open>}%EndSnippet\<close>
text_raw \<open>\DefineSnippet{getMessage_impl}{\<close>
definition getMessage_impl :: "val \<Rightarrow> (val,operation,val) io" where
"getMessage_impl m \<equiv> do {
atomic (do {
exists \<leftarrow> call (Message (KeyExists m));
if exists = Bool True then do {
author \<leftarrow> call (Message (NestedOp m (Author Read)));
content \<leftarrow> call (Message (NestedOp m (Content Read)));
return (Found author content)
} else do {
return NotFound
}
})
}"
text_raw \<open>}%EndSnippet\<close>
text \<open>
\DefineSnippet{getMessage_impl2}{
@{thm [display] getMessage_impl_def}
}%EndSnippet
\<close>
text_raw \<open>\DefineSnippet{chat_example_proc}{\<close>
datatype proc =
SendMessage string string
| EditMessage int string
| DeleteMessage int
| GetMessage int
text_raw \<open>}%EndSnippet\<close>
instance proc :: countable
by countable_datatype
instantiation proc :: valueType begin
definition "uniqueIds_proc proc \<equiv>
case proc of
SendMessage a c \<Rightarrow> {}
| EditMessage m s \<Rightarrow> uniqueIds (MessageId m)
| DeleteMessage m \<Rightarrow> uniqueIds (MessageId m)
| GetMessage m \<Rightarrow> uniqueIds (MessageId m)"
lemma [simp]:
"uniqueIds (SendMessage a c) = {}"
"uniqueIds (EditMessage m s) = uniqueIds (MessageId m)"
"uniqueIds (DeleteMessage m) = uniqueIds (MessageId m)"
"uniqueIds (GetMessage m) = uniqueIds (MessageId m)"
by (auto simp add: uniqueIds_proc_def)
definition [simp]: "default_proc \<equiv> SendMessage [] []"
instance by (standard, auto)
end
lemma inj_fields[simp]: "inj Chat"
"inj Message"
"inj Author"
"inj Content"
by (auto simp add: inj_def)
thm inj_fields
type_synonym localState = "val store \<times> uniqueId set \<times> (val, operation, val) io"
text_raw \<open>\DefineSnippet{chat_example_procedures}{\<close>
definition procedures :: "proc \<Rightarrow> (localState \<times> (localState, operation, val) procedureImpl)" where
"procedures invoc \<equiv>
case invoc of
SendMessage author content \<Rightarrow> toImpl' invoc (sendMessage_impl (String author) (String content))
| EditMessage m newContent \<Rightarrow> toImpl' invoc (editMessage_impl (MessageId m) (String newContent))
| DeleteMessage m \<Rightarrow> toImpl' invoc (deleteMessage_impl (MessageId m))
| GetMessage m \<Rightarrow> toImpl' invoc (getMessage_impl (MessageId m))
"
text_raw \<open>}%EndSnippet\<close>
(*
// getMessage returns correct authors
invariant (forall g: invocationId, m: MessageId, author: UserId, content: String ::
g.info == getMessage(m)
&& g.result == getMessage_res(found(author, content))
==> (exists s: invocationId, content2: String :: s.info == sendMessage(author, content2)))
*)
definition inv1 where
"inv1 op res \<equiv> \<forall>g m author content.
op g \<triangleq> GetMessage m
\<and> res g \<triangleq> Found (String author) content
\<longrightarrow> (\<exists>s content2. op s \<triangleq> SendMessage author content2)
"
subsection \<open>Additional invariants:\<close>
(*
// if there is an assignment of the content field, there also is one for the author field that happened before:
invariant forall c1: callId, m: MessageId, s: String ::
c1.op == message_content_assign(m, s)
==> (exists c2: callId, u: UserId ::
c2.op == message_author_assign(m, u)
&& c2 happened before c1)
*)
definition inv2 where
"inv2 cop hb \<equiv> \<forall>c1 m s.
cop c1 \<triangleq> Message (NestedOp m (Content (Assign s)))
\<longrightarrow> (\<exists>c2 u.
cop c2 \<triangleq> Message (NestedOp m (Author (Assign u)))
\<and> (c2, c1)\<in>hb) "
(*
// there is no update after a delete
invariant !(exists write: callId, delete: callId, m: MessageId ::
((exists u: UserId :: write.op == message_author_assign(m, u))
|| (exists s: String :: write.op == message_content_assign(m, s)))
&& delete.op == message_delete(m)
&& delete happened before write)
*)
definition inv3 where
"inv3 cop hb \<equiv>
\<nexists>write delete m no.
cop write \<triangleq> Message (NestedOp m no)
\<and> is_update no
\<and> cop delete \<triangleq> Message (DeleteKey m)
\<and> (delete,write)\<in>hb"
(*
// for every author-assignment there is a corresponding invocation of sendMessage
invariant forall c: callId, m: MessageId, u: UserId ::
c.op == message_author_assign(m, u)
==> (exists i: invocationId, s: String ::
i.info == sendMessage(u, s))
*)
definition inv4 where
"inv4 op cop \<equiv> \<forall>c m u.
cop c \<triangleq> Message (NestedOp m (Author (Assign (String u))))
\<longrightarrow> (\<exists>i s. op i \<triangleq> SendMessage u s) "
definition inv :: "(proc, operation, val) invContext \<Rightarrow> bool" where
"inv ctxt \<equiv>
inv1 (invocOp ctxt) (invocRes ctxt)
\<and> inv2 (Op ctxt) (happensBefore ctxt)
\<and> inv3 (Op ctxt) (happensBefore ctxt)
\<and> inv4 (invocOp ctxt) (Op ctxt)"
text \<open>\DefineSnippet{chat_example_messageStruct_def}{
@{thm [display] messageStruct_def}
}%EndSnippet\<close>
text \<open>\DefineSnippet{chat_example_crdtSpec_def}{
@{thm [display] crdtSpec_def}
}%EndSnippet\<close>
text \<open>\DefineSnippet{chat_example_inv1}{
@{thm [display] inv1_def}
}%EndSnippet\<close>
text \<open>\DefineSnippet{chat_example_inv2}{
@{thm [display] inv2_def}
}%EndSnippet\<close>
text \<open>\DefineSnippet{chat_example_inv3}{
@{thm [display] inv3_def}
}%EndSnippet\<close>
text \<open>\DefineSnippet{chat_example_inv4}{
@{thm [display] inv4_def}
}%EndSnippet\<close>
text \<open>\DefineSnippet{chat_example_inv}{
@{thm [display] inv_def}
}%EndSnippet\<close>
definition progr :: "(proc, localState, operation, val) prog" where
"progr \<equiv> \<lparr>
querySpec = crdtSpec,
procedure = procedures,
invariant = inv
\<rparr>"
lemma [simp]: "procedure progr = procedures"
"querySpec progr = crdtSpec"
"invariant progr = inv"
by (auto simp add: progr_def)
declare convert_spec_def[simp]
(*
lemma uniqueId_no_nested: "x \<in> uniqueIds uid \<Longrightarrow> x = (to_nat (uid :: val))"
by (auto simp add: uniqueIds_val_def split: val.splits)
lemma uniqueId_no_nested2: "x \<in> uniqueIds uid \<longleftrightarrow> (\<exists>u. x = to_nat (MessageId u) \<and> uid = MessageId u)"
by (auto simp add: uniqueIds_val_def split: val.splits)
*)
lemma progr_wf[simp]: "program_wellFormed progr"
proof (auto simp add: program_wellFormed_def)
show "invocations_cannot_guess_ids progr"
proof (rule invocations_cannot_guess_ids_io)
fix proc store localKnown cmd impl
assume a0: "procedure progr proc = ((store, localKnown, cmd), impl)"
thus "impl = impl_language_loops.toImpl \<and> localKnown = uniqueIds proc"
by (auto simp add: progr_def procedures_def split: proc.splits)
qed
show "queries_cannot_guess_ids crdtSpec"
by (auto simp add: inj_def crdtSpec_def messageStruct_def)
qed
lemma isMessageId_infinite[simp]: "infinite (Collect isMessageId)"
proof (rule infinite_if_mappable_to_nat)
show "\<exists>x\<in>Collect isMessageId. n \<le> (case x of MessageId n \<Rightarrow> nat n)" for n
by (rule bexI[where x="MessageId (int n)"],
auto simp add: isMessageId_def)
qed
\<comment> \<open>Move to helpers\<close>
lemma get_query_spec2:
assumes wf: "proof_state_wellFormed S"
and upd_call: "calls S upd_c \<triangleq> Call oper upd_r"
shows "\<exists>ctxt. querySpec (ps_prog S) oper ctxt upd_r"
proof -
from wf
obtain CS where rel: "proof_state_rel S CS"
using proof_state_wellFormed_def by blast
hence state_wf: "state_wellFormed CS"
using proof_state_rel_wf by blast
have upd_call': "calls CS upd_c \<triangleq> Call oper upd_r"
using rel upd_call proof_state_rel_calls[OF rel] by auto
have "prog CS = ps_prog S"
using proof_state_rel_prog rel by fastforce
from wf_queryspec[OF state_wf] upd_call' `prog CS = ps_prog S`
obtain ctxt where "querySpec (ps_prog S) oper ctxt upd_r"
apply atomize_elim
apply auto
by blast
thus ?thesis
by blast
qed
lemma select_field_some[simp]:
assumes "inj f"
shows "select_field f (f x) \<triangleq> x"
using assms by (auto simp add: select_field_def)
lemma select_field_none[simp]:
assumes "\<And>x y. g x \<noteq> f y"
shows "select_field f (g x) = None"
using assms by (auto simp add: select_field_def)
lemma query_result_undef:
assumes wf: "proof_state_wellFormed S"
and upd_call: "calls S upd_c \<triangleq> Call (Message (NestedOp (MessageId m) upd_op)) upd_r"
and upd_is_update: "is_update upd_op"
and progr: "ps_prog S = progr"
shows "upd_r = Undef"
proof -
obtain ctxt where "querySpec progr (Message (NestedOp (MessageId m) upd_op)) ctxt upd_r"
using get_query_spec2 local.wf progr upd_call by fastforce
thus "upd_r = Undef"
using upd_is_update
by (cases upd_op)
(auto simp add: inj_def crdtSpec_def struct_field_def map_sdw_spec_def map_spec_def messageStruct_def register_spec_def split: messageDataOp.splits registerOp.splits if_splits)
qed
declare invContext.defs[simp]
lemmas crdt_spec_defs =
toplevel_spec_def crdtSpec'_def struct_field'_def map_sdw_spec'_def map_spec'_def messageStruct'_def register_spec'_def
set_rw_spec'_def deleted_calls_sdw'_def
theorem chat_app_correct: "programCorrect progr"
proof M_show_programCorrect
subsection \<open>Initial State\<close>
case invariant_initial_state
show "invariant_all' (initialState progr)"
by (simp add: inv_def initialState_def invContextH2_calls inv1_def inv2_def inv3_def inv4_def invContextH2_happensBefore invContextH2_i_invocOp progr_def)
case (procedure_correct S i)
show "procedureCorrect S i"
proof (rule Initial_Label, rule show_initial_state_prop[OF procedure_correct], rule DC_final2, casify)
case (show_P S_pre proc initState impl)
have "invocOp S i \<triangleq> proc"
using show_P by auto
have "invocRes S i = None"
using show_P apply auto
using wf_result_after_invocation by blast
have "uniqueIds proc \<subseteq> knownIds S"
using show_P by auto
note show_P[simp]
show "procedureCorrect S i"
proof (cases proc)
subsection \<open>SendMessage\<close>
case (SendMessage author content)
show "procedureCorrect S i"
proof M_show_procedureCorrect
case after_invocation
show ?case
using show_P.invariant_pre
proof (auto simp add: inv_def invContextH2_simps)
show "inv1 (invocOp S_pre(i \<mapsto> proc)) (invocRes S_pre)" if "inv1 (invocOp S_pre) (invocRes S_pre)"
using show_P.i_fresh SendMessage that
by (auto simp add: inv1_def)
(metis option.discI show_P.i_fresh)
show "inv4 (invocOp S_pre(i \<mapsto> proc)) (Op S_pre)" if "inv4 (invocOp S_pre) (Op S_pre)"
using show_P.i_fresh SendMessage that
by (auto simp add: inv4_def)
(metis option.discI show_P.i_fresh)
qed
next
case execution
show "execution_s_correct S i"
using procedure_correct.in_initial_state
proof (fuzzy_rule execution_s_check_sound4)
show "currentProc S i \<triangleq> toImpl"
by (auto simp add: SendMessage procedures_def )
show "localState S i \<triangleq> (Map.empty, uniqueIds (SendMessage author content), sendMessage_impl (String author) (String content))"
by (auto simp add: SendMessage procedures_def )
show "invocOp S i \<triangleq> SendMessage author content"
using SendMessage \<open>invocOp S i \<triangleq> proc\<close> by blast
note sendMessage_impl_def[simp]
show "program_wellFormed (prog S)"
by simp
show "invariant_all' S"
using execution.in_initial_state by blast
show "crdt_spec_rel (querySpec progr) crdtSpec' "
by (simp add: crdtSpec_rel)
show "execution_s_check (invariant progr) crdtSpec' \<lparr>calls = s_calls, happensBefore = s_happensBefore, callOrigin = s_callOrigin, txOrigin = s_txOrigin, knownIds = s_knownIds, invocOp = s_invocOp(i \<mapsto> SendMessage author content), invocRes = s_invocRes(i := None), ps_i = i, ps_generatedLocal = {}, ps_generatedLocalPrivate = {}, ps_localKnown = uniqueIds (SendMessage author content), ps_vis = {}, ps_localCalls = [], ps_tx = None, ps_firstTx = True, ps_store = Map.empty, ps_prog = progr\<rparr> (sendMessage_impl (String author) (String content)) (finalCheck (invariant progr) i)"
if tx_fresh: "\<And>tx. s_txOrigin tx \<noteq> Some i"
and inv_initial: "invariant progr \<lparr>calls = s_calls, happensBefore = s_happensBefore, callOrigin = s_callOrigin, txOrigin = s_txOrigin, knownIds = s_knownIds, invocOp = s_invocOp(i \<mapsto> SendMessage author content), invocRes = s_invocRes(i := None)\<rparr>"
for s_calls s_happensBefore s_callOrigin s_txOrigin s_knownIds s_invocOp s_invocRes
proof (repliss_vcg_l, fuzzy_goal_cases "AtCommit" "AtReturn" )
case (AtCommit v vn tx s_calls' s_happensBefore' s_callOrigin' s_txOrigin' s_knownIds' vis' s_invocOp' s_invocRes' c res ca resa cb resb PS')
have "res = Undef"
using AtCommit.toplevel_spec
by (auto simp add: crdt_spec_defs)
have "resa = Undef"
using AtCommit.toplevel_spec2
by (auto simp add: crdt_spec_defs)
have "resb = Undef"
using AtCommit.toplevel_spec3
by (auto simp add: crdt_spec_defs)
have "in_sequence [c, ca, cb] c ca"
by (simp add: in_sequence_cons)
have "new_unique_not_in_calls s_calls' vn"
by (meson AtCommit.uid_is_private'2 uid_is_private'_def)
hence no_v: "vn \<notin> uniqueIds opr" if "cOp s_calls' c \<triangleq> opr" for c opr
using that
by (auto simp add: new_unique_not_in_calls_def cOp_Some_iff)
have no_delete_v: "cOp s_calls' delete \<noteq> Some (Message (DeleteKey v))" for delete
using `uniqueIds_val_r v = {vn}` by (auto dest!: no_v)
from AtCommit
show ?case
proof (auto simp add: inv_def, fuzzy_goal_cases inv2 inv3 inv4)
next
case (inv2 )
from inv2 show ?case
by (auto simp add: inv2_def updateHb_cases in_sequence_cons cong: conj_cong, meson)
next
case (inv3)
then show ?case
by (auto simp add: inv3_def in_sequence_cons updateHb_cases no_delete_v)
case (inv4)
then show ?case
by (auto simp add: inv4_def)
qed
next
case (AtReturn v tx s_calls' s_happensBefore' s_callOrigin' s_txOrigin' s_knownIds' vis' s_invocOp' s_invocRes' c res ca resa cb resb)
from AtReturn.inv2
show ?case
by (auto simp add: inv_def inv1_def split: if_splits)
blast
qed
qed
qed
next
subsection \<open>EditMessage\<close>
case (EditMessage m newContent)
show "procedureCorrect S i"
proof M_show_procedureCorrect
case after_invocation
show ?case
using show_P.invariant_pre EditMessage show_P.i_fresh
by (auto simp add: inv_def inv1_def inv2_def inv3_def inv4_def invContextH2_simps,
metis option.distinct(1) show_P.i_fresh)+
next
case execution
show "execution_s_correct S i"
using procedure_correct.in_initial_state
proof (fuzzy_rule execution_s_check_sound4)
show "currentProc S i \<triangleq> toImpl"
by (auto simp add: EditMessage procedures_def )
show "invocOp S i \<triangleq> EditMessage m newContent"
using EditMessage \<open>invocOp S i \<triangleq> proc\<close> by blast
show "localState S i \<triangleq> (Map.empty, uniqueIds (EditMessage m newContent), editMessage_impl (MessageId m) (String newContent))"
by (auto simp add: EditMessage procedures_def )
note editMessage_impl_def[simp]
show "program_wellFormed (prog S)"
by simp
show "invariant_all' S"
using execution.in_initial_state by blast
show "crdt_spec_rel (querySpec progr) crdtSpec'"
by (simp add: crdtSpec_rel)
show "execution_s_check (invariant progr) crdtSpec' \<lparr>calls = s_calls, happensBefore = s_happensBefore, callOrigin = s_callOrigin, txOrigin = s_txOrigin, knownIds = s_knownIds, invocOp = s_invocOp(i \<mapsto> EditMessage m newContent), invocRes = s_invocRes(i := None), ps_i = i, ps_generatedLocal = {}, ps_generatedLocalPrivate = {}, ps_localKnown = uniqueIds (EditMessage m newContent), ps_vis = {}, ps_localCalls = [], ps_tx = None, ps_firstTx = True, ps_store = Map.empty, ps_prog = progr\<rparr> (editMessage_impl (MessageId m) (String newContent)) (finalCheck (invariant progr) i)"
if tx_fresh: "\<And>tx. s_txOrigin tx \<noteq> Some i"
and inv_init: "invariant progr \<lparr>calls = s_calls, happensBefore = s_happensBefore, callOrigin = s_callOrigin, txOrigin = s_txOrigin, knownIds = s_knownIds, invocOp = s_invocOp(i \<mapsto> EditMessage m newContent), invocRes = s_invocRes(i := None)\<rparr>"
for s_calls s_happensBefore s_callOrigin s_txOrigin s_knownIds s_invocOp s_invocRes
proof (repliss_vcg_l, fuzzy_goal_cases "Exists_AtCommit" "Exists_AtReturn" "NotExists_AtCommit" "NotExists_AtReturn")
case (Exists_AtCommit tx s_calls' s_happensBefore' s_callOrigin' s_txOrigin' s_knownIds' vis' s_invocOp' s_invocRes' c ca resa PS')
note `c \<noteq> ca`[simp]
from `example_chat.inv
\<lparr>calls = s_calls', happensBefore = s_happensBefore', callOrigin = s_callOrigin', txOrigin = s_txOrigin',
knownIds = s_knownIds', invocOp = s_invocOp'(i \<mapsto> EditMessage m newContent),
invocRes = s_invocRes'(i := None)\<rparr>`
have i1: "inv1 (s_invocOp'(i \<mapsto> EditMessage m newContent)) (s_invocRes'(i := None))"
and i2: "inv4 (s_invocOp'(i \<mapsto> EditMessage m newContent)) (cOp s_calls')"
and i3: "inv2 (cOp s_calls') s_happensBefore'"
and i4: "inv3 (cOp s_calls') s_happensBefore'"
by (auto simp add: inv_def)
text \<open>From existance of entry, obtain information about the history:\<close>
from \<open>toplevel_spec crdtSpec' \<lparr>calls = s_calls', happensBefore = s_happensBefore'\<rparr> vis' (Message (KeyExists (MessageId m))) (Bool True)\<close>
obtain upd_c upd_op
where upd_vis: "upd_c \<in> vis'"
and upd_call: "extract_op s_calls' upd_c = Message (NestedOp (MessageId m) upd_op)"
and upd_is_update: "is_update upd_op"
and upd_not_deleted: "\<forall>d\<in>vis'. extract_op s_calls' d = Message (DeleteKey (MessageId m)) \<longrightarrow> (d, upd_c) \<in> s_happensBefore'"
by (auto simp add: crdt_spec_defs C_out_calls_def )
from upd_call and upd_vis
have upd_call': "cOp s_calls' upd_c \<triangleq> Message (NestedOp (MessageId m) upd_op)"
by (metis Exists_AtCommit.less_eq cOp_Some extract_op_def in_dom option.sel)
from upd_not_deleted
have upd_not_deleted': "\<forall>d\<in>vis'. cOp s_calls' d \<triangleq> Message (DeleteKey (MessageId m)) \<longrightarrow> (d, upd_c) \<in> s_happensBefore'"
by (meson Exists_AtCommit.less_eq cOp_Some_iff extract_op_eq in_mono)
with \<open>inv3 (cOp s_calls') s_happensBefore'\<close>
have no_delete: \<open>\<forall>d\<in>vis'. \<not> cOp s_calls' d \<triangleq> Message (DeleteKey (MessageId m))\<close>
using upd_call' upd_is_update by (auto simp add: inv3_def)
have "upd_c \<noteq> c"
using Exists_AtCommit.not_member upd_vis by blast
have "upd_c \<noteq> ca"
using Exists_AtCommit.not_member upd_vis by blast
text \<open>with @{term inv2} get an assignment of the author:\<close>
obtain upda_c upda_val
where upda_call: "cOp s_calls' upda_c \<triangleq> Message (NestedOp (MessageId m) (Author (Assign upda_val)))"
and upda_before_upd: "(upda_c, upd_c) \<in> s_happensBefore' \<or> upda_c = upd_c"
proof (atomize_elim, cases upd_op)
case (Author x)
obtain a where "x = Assign a"
using Author upd_is_update
by (cases x) auto
show " \<exists>upda_c upda_val.
cOp s_calls' upda_c \<triangleq> Message (NestedOp (MessageId m) (Author (Assign upda_val))) \<and>
((upda_c, upd_c) \<in> s_happensBefore' \<or> upda_c = upd_c)"
using Author \<open>x = Assign a\<close> upd_call' by blast
next
case (Content x)
from this
obtain content
where content_assign: "cOp s_calls' upd_c \<triangleq> Message (NestedOp (MessageId m) (Content (Assign content)))"
using registerOp.exhaust upd_call' upd_is_update by auto
from `inv2 (cOp s_calls') s_happensBefore'`[unfolded inv2_def, rule_format, OF content_assign]
show " \<exists>upda_c upda_val.
cOp s_calls' upda_c \<triangleq> Message (NestedOp (MessageId m) (Author (Assign upda_val))) \<and>
((upda_c, upd_c) \<in> s_happensBefore' \<or> upda_c = upd_c)"
by blast
qed
have upda_c_vis: "upda_c \<in> vis'"
using Exists_AtCommit.causallyConsistent causallyConsistent_def upd_vis upda_before_upd by fastforce
have "upda_c \<noteq> c"
using Exists_AtCommit \<open>upd_c \<noteq> c\<close> upda_before_upd by blast
have "upda_c \<noteq> ca"
using Exists_AtCommit \<open>upd_c \<noteq> ca\<close> upda_before_upd by blast
from Exists_AtCommit.inv
show ?case
proof (auto simp add: inv_def Exists_AtCommit.PS'_eq, fuzzy_goal_cases "inv2" "inv3" "inv4")
case (inv4)
then show ?case
by (auto simp add: inv4_def)
next
case (inv2)
show ?case
using \<open>upda_c \<in> vis'\<close> \<open>upda_c \<noteq> c\<close> \<open>upda_c \<noteq> ca\<close> upda_call inv2.inv2 Exists_AtCommit.not_member2
by (auto simp add: inv2_def updateHb_cases in_sequence_cons `c \<noteq> ca`[symmetric] cong: conj_cong)
blast
next
case (inv3)
text \<open>The update performed in the transaction is not after an delete, which we get from the query results.\<close>
from Exists_AtCommit.not_member3 inv3.inv3
no_delete
show ?case
by (auto simp add: inv3_def updateHb_cases in_sequence_cons )
qed
next
case (Exists_AtReturn tx s_calls' s_happensBefore' s_callOrigin' s_txOrigin' s_knownIds' vis' s_invocOp' s_invocRes' c ca resa)
from Exists_AtReturn.inv2
show ?case
by (auto simp add: inv_def inv1_def cong: conj_cong split: if_splits)
next
case (NotExists_AtCommit tx s_calls' s_happensBefore' s_callOrigin' s_txOrigin' s_knownIds' vis' s_invocOp' s_invocRes' c res PS')
then show ?case
proof (auto simp add: inv_def, fuzzy_goal_cases inv2 inv3 inv4)
case (inv4 )
then show ?case by (auto simp add: inv4_def)
next
case (inv2)
then show ?case by (auto simp add: inv2_def updateHb_single cong: conj_cong, fastforce)
next
case (inv3)
then show ?case
by (auto simp add: inv3_def updateHb_single)
qed
next
case (NotExists_AtReturn tx s_calls' s_happensBefore' s_callOrigin' s_txOrigin' s_knownIds' vis' s_invocOp' s_invocRes' c res)
then show ?case
by (auto simp add: inv_def inv1_def cong: conj_cong split: if_splits)
qed
qed
qed
next
subsection \<open>DeleteMessage\<close>
case (DeleteMessage m)
show "procedureCorrect S i"
proof M_show_procedureCorrect
case after_invocation
show ?case
using show_P.invariant_pre DeleteMessage show_P.i_fresh
by (auto simp add: inv_def inv1_def inv2_def inv3_def inv4_def invContextH2_simps,
metis option.distinct(1) show_P.i_fresh)+
next
case execution
show "execution_s_correct S i"
using procedure_correct.in_initial_state
proof (fuzzy_rule execution_s_check_sound4)
show "currentProc S i \<triangleq> toImpl"
by (auto simp add: DeleteMessage procedures_def )
show "localState S i \<triangleq> (Map.empty, uniqueIds (DeleteMessage m), deleteMessage_impl (MessageId m))"
by (auto simp add: DeleteMessage procedures_def )
show "invocOp S i \<triangleq> DeleteMessage m"
using DeleteMessage \<open>invocOp S i \<triangleq> proc\<close> by blast
note deleteMessage_impl_def[simp]
show "program_wellFormed (prog S)"
by simp
show "invariant_all' S"
using execution.in_initial_state by blast
show "crdt_spec_rel (querySpec progr) crdtSpec'"
by (simp add: crdtSpec_rel)
show "execution_s_check (invariant progr) crdtSpec' \<lparr>calls = s_calls, happensBefore = s_happensBefore, callOrigin = s_callOrigin, txOrigin = s_txOrigin, knownIds = s_knownIds, invocOp = s_invocOp(i \<mapsto> DeleteMessage m), invocRes = s_invocRes(i := None), ps_i = i, ps_generatedLocal = {}, ps_generatedLocalPrivate = {}, ps_localKnown = uniqueIds (DeleteMessage m), ps_vis = {}, ps_localCalls = [], ps_tx = None, ps_firstTx = True, ps_store = Map.empty, ps_prog = progr\<rparr> (deleteMessage_impl (MessageId m)) (finalCheck (invariant progr) i)"
if tx_fresh: "\<And>tx. s_txOrigin tx \<noteq> Some i"
and inv_i: "invariant progr \<lparr>calls = s_calls, happensBefore = s_happensBefore, callOrigin = s_callOrigin, txOrigin = s_txOrigin, knownIds = s_knownIds, invocOp = s_invocOp(i \<mapsto> DeleteMessage m), invocRes = s_invocRes(i := None)\<rparr>"
for s_calls s_happensBefore s_callOrigin s_txOrigin s_knownIds s_invocOp s_invocRes
proof (repliss_vcg_l, fuzzy_goal_cases "AtCommit" "AtReturn" )
case (AtCommit tx s_calls' s_happensBefore' s_callOrigin' s_txOrigin' s_knownIds' vis' s_invocOp' s_invocRes' c res ca resa PS')
have [simp]: "c \<noteq> ca"
by (simp add: AtCommit.not_c_eq)
from `example_chat.inv
\<lparr>calls = s_calls', happensBefore = s_happensBefore', callOrigin = s_callOrigin', txOrigin = s_txOrigin',
knownIds = s_knownIds', invocOp = s_invocOp'(i \<mapsto> DeleteMessage m), invocRes = s_invocRes'(i := None)\<rparr>`
have i1: "inv1 (s_invocOp'(i \<mapsto> DeleteMessage m)) (s_invocRes'(i := None))"
and i4: "inv4 (s_invocOp'(i \<mapsto> DeleteMessage m)) (cOp s_calls')"
and i2: "inv2 (cOp s_calls') s_happensBefore'"
and i3: "inv3 (cOp s_calls') s_happensBefore'"
by (auto simp add: inv_def)
show ?case
proof (auto simp add: inv_def AtCommit.PS'_eq, fuzzy_goal_cases inv1 inv2 inv3 inv4)
case inv1
from i1
show ?case
by simp
next
case inv4
from i4
show ?case
by (auto simp add: inv4_def)
next
case inv2
from i2
show ?case
using AtCommit.cOp_eq AtCommit.cOp_eq2
by (auto simp add: inv2_def updateHb_cases cong: conj_cong )
(metis option.distinct(1))
next
case inv3
then show ?case
using AtCommit.not_member2 i3
by (auto simp add: inv3_def updateHb_cases in_sequence_cons )
qed
next
case (AtReturn tx s_calls' s_happensBefore' s_callOrigin' s_txOrigin' s_knownIds' vis' s_invocOp' s_invocRes' c res ca resa)
then show ?case
by (auto simp add: inv_def inv1_def split: if_splits)
meson
qed
qed
qed
next
subsection \<open>GetMessage\<close>
case (GetMessage m)
show "procedureCorrect S i"
proof M_show_procedureCorrect
case after_invocation
show ?case
using show_P.invariant_pre GetMessage show_P.i_fresh
proof (auto simp add: inv_def invContextH2_simps, fuzzy_goal_cases inv1 inv4)
case inv1
then show ?case
apply (auto simp add: inv1_def state_wellFormed_invocation_before_result)
by (metis option.simps(3) show_P.i_fresh)
next
case inv4
then show ?case
apply (auto simp add: inv4_def)
by (metis option.distinct(1) show_P.i_fresh)
qed
next