-
Notifications
You must be signed in to change notification settings - Fork 4
/
Wasm_Checker_Properties.thy
1498 lines (1471 loc) · 52.5 KB
/
Wasm_Checker_Properties.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 {* Correctness of Type Checker *}
theory Wasm_Checker_Properties imports Wasm_Checker Wasm_Properties begin
subsection {* Soundness *}
lemma b_e_check_single_type_sound:
assumes "type_update (Type x1) (to_ct_list t_in) (Type t_out) = Type x2"
"c_types_agree (Type x2) tm"
"\<C> \<turnstile> [e] : (t_in _> t_out)"
shows "\<exists>tn. c_types_agree (Type x1) tn \<and> \<C> \<turnstile> [e] : (tn _> tm)"
using assms(2) b_e_typing.weakening[OF assms(3)] type_update_type[OF assms(1)]
by auto
lemma b_e_check_single_top_sound:
assumes "type_update (TopType x1) (to_ct_list t_in) (Type t_out) = TopType x2"
"c_types_agree (TopType x2) tm"
"\<C> \<turnstile> [e] : (t_in _> t_out)"
shows "\<exists>tn. c_types_agree (TopType x1) tn \<and> \<C> \<turnstile> [e] : (tn _> tm)"
proof -
obtain t_ag where t_ag_def:"ct_suffix (to_ct_list t_out) x2"
"tm = t_ag @ t_out"
"c_types_agree (TopType x1) (t_ag @ t_in)"
using type_update_top_top[OF assms(1,2)]
by fastforce
hence "\<C> \<turnstile> [e] : (t_ag@t_in _> t_ag@t_out)"
using b_e_typing.weakening[OF assms(3)]
by fastforce
thus ?thesis
using t_ag_def
by fastforce
qed
lemma b_e_check_single_top_not_bot_sound:
assumes "type_update ts (to_ct_list t_in) (TopType []) = ts'"
"ts \<noteq> Bot"
"ts' \<noteq> Bot"
shows "\<exists>tn. c_types_agree ts tn \<and> suffix t_in tn"
proof (cases ts)
case (TopType x1)
then obtain t_int where "consume (TopType x1) (to_ct_list t_in) = t_int" "t_int \<noteq> Bot"
using assms(1,2,3)
by fastforce
thus ?thesis
using TopType ct_suffix_ct_list_compat_exists ct_suffix_ts_conv_suffix
unfolding consume.simps
by (metis append_Nil c_types_agree.simps(2) ct_suffix_def)
next
case (Type x2)
then obtain t_int where "consume (Type x2) (to_ct_list t_in) = t_int" "t_int \<noteq> Bot"
using assms(1,2,3)
by fastforce
thus ?thesis
using c_types_agree_id Type consume_type suffixI ct_suffix_ts_conv_suffix
by fastforce
next
case Bot
thus ?thesis
using assms(2)
by simp
qed
lemma b_e_check_single_type_not_bot_sound:
assumes "type_update ts (to_ct_list t_in) (Type t_out) = ts'"
"ts \<noteq> Bot"
"ts' \<noteq> Bot"
"c_types_agree ts' tm"
"\<C> \<turnstile> [e] : (t_in _> t_out)"
shows "\<exists>tn. c_types_agree ts tn \<and> \<C> \<turnstile> [e] : (tn _> tm)"
using assms b_e_check_single_type_sound
proof (cases ts)
case (TopType x1)
then obtain x1' where x_def:"TopType x1' = ts'"
using assms
by (simp, metis (full_types) produce.simps(1) produce.simps(6))
thus ?thesis
using assms b_e_check_single_top_sound TopType
by fastforce
next
case (Type x2)
then obtain x2' where x_def:"Type x2' = ts'"
using assms
by (simp, metis (full_types) produce.simps(2) produce.simps(6))
thus ?thesis
using assms b_e_check_single_type_sound Type
by fastforce
next
case Bot
thus ?thesis
using assms(2)
by simp
qed
lemma b_e_check_single_sound_unop_testop_cvtop:
assumes "check_single \<C> e tn' = tm'"
"(e = (Unop t uw) \<and> unop_t_agree op t)
\<or> (e = (Testop t uv) \<and> is_int_t t)
\<or> (e = (Cvtop t1 Convert t sx) \<and> convert_cond t1 t sx)
\<or> (e = (Cvtop t1 Reinterpret t sx) \<and> ((t1 \<noteq> t) \<and> t_length t1 = t_length t \<and> sx = None))"
"c_types_agree tm' tm"
"tn' \<noteq> Bot"
"tm' \<noteq> Bot"
shows "\<exists>tn. c_types_agree tn' tn \<and> \<C> \<turnstile> [e] : (tn _> tm)"
proof -
have "(e = (Cvtop t1 Convert t sx) \<Longrightarrow> convert_cond t1 t sx)"
using assms(2)
by simp
have 1:"type_update tn' (to_ct_list [t]) (Type [arity_1_result e]) = tm'"
using assms arity_1_result_def
unfolding to_ct_list_def
apply (simp del: convert_cond.simps)
apply fastforce
done
have "\<C> \<turnstile> [e] : ([t] _> [arity_1_result e])"
using assms(2) b_e_typing.intros(2,3,6,9,10)
unfolding arity_1_result_def
apply simp
apply (metis (no_types, lifting) assms(1,5) b_e.simps(801,803,805) b_e_typing.reinterpret b_e_typing.testop check_single.simps(2))
done
thus ?thesis
using b_e_check_single_type_not_bot_sound[OF 1 assms(4,5,3)]
by fastforce
qed
lemma b_e_check_single_sound_binop_relop:
assumes "check_single \<C> e tn' = tm'"
"((e = Binop t iop \<and> binop_t_agree op t)
\<or> (e = Relop t irop \<and> relop_t_agree irop t))"
"c_types_agree tm' tm"
"tn' \<noteq> Bot"
"tm' \<noteq> Bot"
shows "\<exists>tn. c_types_agree tn' tn \<and> \<C> \<turnstile> [e] : (tn _> tm)"
proof -
have "type_update tn' (to_ct_list [t,t]) (Type [arity_2_result e]) = tm'"
using assms arity_2_result_def
unfolding to_ct_list_def
by auto
moreover
have "\<C> \<turnstile> [e] : ([t,t] _> [arity_2_result e])"
using assms(2) b_e_typing.intros(4,5,7,8)
unfolding arity_2_result_def
by (metis (no_types) assms(1,2,5) b_e.simps(802,804) b_e_typing.relop binop check_single.simps(3))
ultimately
show ?thesis
using b_e_check_single_type_not_bot_sound[OF _ assms(4,5,3)]
by fastforce
qed
lemma b_e_type_checker_sound:
assumes "b_e_type_checker \<C> es (tn _> tm)"
shows "\<C> \<turnstile> es : (tn _> tm)"
proof -
fix e tn'
have "b_e_type_checker \<C> es (tn _> tm) \<Longrightarrow>
\<C> \<turnstile> es : (tn _> tm)"
and "\<And>tm' tm.
check \<C> es tn' = tm' \<Longrightarrow>
c_types_agree tm' tm \<Longrightarrow>
\<exists>tn. c_types_agree tn' tn \<and> \<C> \<turnstile> es : (tn _> tm)"
and "\<And>tm' tm.
check_single \<C> e tn' = tm' \<Longrightarrow>
c_types_agree tm' tm \<Longrightarrow>
tn' \<noteq> Bot \<Longrightarrow>
tm' \<noteq> Bot \<Longrightarrow>
\<exists>tn. c_types_agree tn' tn \<and> \<C> \<turnstile> [e] : (tn _> tm)"
proof (induction rule: b_e_type_checker_check_check_single.induct)
case (1 \<C> es tn' tm)
thus ?case
by simp
next
case (2 \<C> es' ts)
show ?case
proof (cases es')
case Nil
thus ?thesis
using 2(5,6)
by (simp add: b_e_type_empty)
next
case (Cons e es)
thus ?thesis
proof (cases ts)
case (TopType x1)
have check_expand:"check \<C> es (check_single \<C> e ts) = tm'"
using 2(5,6) TopType Cons
by simp
obtain ts' where ts'_def:"check_single \<C> e ts = ts'"
by blast
obtain t_int where t_int_def:"\<C> \<turnstile> es : (t_int _> tm)"
"c_types_agree ts' t_int"
using 2(2)[OF Cons TopType check_expand 2(6)] ts'_def
by blast
obtain t_int' where "c_types_agree ts t_int'" "\<C> \<turnstile> [e] : (t_int' _> t_int)"
using 2(1)[OF Cons _ ts'_def] TopType c_types_agree.simps(3) t_int_def(2)
by blast
thus ?thesis
using t_int_def(1) b_e_type_comp_conc Cons
by fastforce
next
case (Type x2)
have check_expand:"check \<C> es (check_single \<C> e ts) = tm'"
using 2(5,6) Type Cons
by simp
obtain ts' where ts'_def:"check_single \<C> e ts = ts'"
by blast
obtain t_int where t_int_def:"\<C> \<turnstile> es : (t_int _> tm)"
"c_types_agree ts' t_int"
using 2(4)[OF Cons Type check_expand 2(6)] ts'_def
by blast
obtain t_int' where "c_types_agree ts t_int'" "\<C> \<turnstile> [e] : (t_int' _> t_int)"
using 2(3)[OF Cons _ ts'_def] Type c_types_agree.simps(3) t_int_def(2)
by blast
thus ?thesis
using t_int_def(1) b_e_type_comp_conc Cons
by fastforce
next
case Bot
then show ?thesis
using 2(5,6) Cons
by auto
qed
qed
next
case (3 \<C> v ts)
hence "type_update ts [] (Type [typeof v]) = tm'"
by simp
moreover
have "\<C> \<turnstile> [C v] : ([] _> [typeof v])"
using b_e_typing.intros(1)
by blast
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 3(3,4,2)]
by (metis list.simps(8) to_ct_list_def)
next
case (4 \<C> t op ts)
hence "unop_t_agree op t"
by (simp, meson)
thus ?case
using b_e_check_single_sound_unop_testop_cvtop 4
by fastforce
next
case (5 \<C> t op ts)
hence "binop_t_agree op t"
by (simp, meson)
thus ?case
using b_e_check_single_sound_binop_relop 5
by fastforce
next
case (6 \<C> t op ts)
hence "is_int_t t"
by (simp, meson)
thus ?case
using b_e_check_single_sound_unop_testop_cvtop 6
by fastforce
next
case (7 \<C> t op ts)
hence "relop_t_agree op t"
by (simp, meson)
thus ?case
using b_e_check_single_sound_binop_relop 7
by fastforce
next
case (8 \<C> t1 t2 sx ts)
hence "convert_cond t1 t2 sx"
by (simp del: convert_cond.simps, meson)
thus ?case
using b_e_check_single_sound_unop_testop_cvtop 8
by fastforce
next
case (9 \<C> t1 t2 sx ts)
hence "t1 \<noteq> t2 \<and> t_length t1 = t_length t2 \<and> sx = None"
by (simp, presburger)
thus ?case
using b_e_check_single_sound_unop_testop_cvtop 9
by fastforce
next
case (10 \<C> ts)
thus ?case
using b_e_typing.intros(8) c_types_agree_not_bot_exists
by blast
next
case (11 \<C> ts)
thus ?case
using b_e_typing.intros(9,32)
by fastforce
next
case (12 \<C> ts)
thus ?case
proof (cases ts)
case (TopType x1)
thus ?thesis
proof (cases x1 rule: List.rev_cases)
case Nil
have "\<C> \<turnstile> [Drop] : (tm@[T_i32] _> tm)"
using b_e_typing.intros(10,32)
by fastforce
thus ?thesis
using c_types_agree_top1 Nil TopType
by fastforce
next
case (snoc ys y)
hence temp1:"(consume (TopType (ys@[y])) [TAny]) = tm'"
using 12 TopType type_update_empty
by (metis check_single.simps(10))
hence temp2:"c_types_agree (TopType ys) tm"
using consume_top_geq[OF temp1] 12(2,3,4)
by (metis Suc_leI add_diff_cancel_right' append_eq_conv_conj consume.simps(2)
ct_suffix_def length_Cons length_append list.size(3) trans_le_add2
zero_less_Suc)
obtain t where "ct_list_compat [y] (to_ct_list [t])"
using ct_list_compat_exists
unfolding ct_list_compat_def to_ct_list_def list_all2_map2
by (metis list_all2_Cons1 list_all2_Nil)
hence "c_types_agree ts (tm@[t])"
using temp2 ct_suffix_extend_ct_list_compat snoc TopType
by (simp add: to_ct_list_def)
thus ?thesis
using b_e_typing.intros(10,32)
by fastforce
qed
next
case (Type x2)
thus ?thesis
proof (cases x2 rule: List.rev_cases)
case Nil
hence "(consume (Type []) [TAny]) = tm'"
using 12 Type type_update_empty
by fastforce
thus ?thesis
using 12(4) ct_list_compat_def ct_suffix_def to_ct_list_def
by simp
next
case (snoc ys y)
hence temp1:"(consume (Type (ys@[y])) [TAny]) = tm'"
using 12 Type type_update_empty
by (metis check_single.simps(10))
hence temp2:"c_types_agree (Type ys) tm"
using 12(2,3,4) ct_suffix_def
by (simp, metis One_nat_def butlast_conv_take butlast_snoc c_types_agree.simps(1)
length_Cons list.size(3))
obtain t where "ct_list_compat [TSome y] (to_ct_list [t])"
using ct_list_compat_exists
unfolding ct_list_compat_def to_ct_list_def list_all2_map2
by (metis list_all2_Cons1 list_all2_Nil)
hence "c_types_agree ts (tm@[t])"
using temp2 ct_suffix_extend_ct_list_compat snoc Type
by (simp add: ct_list_compat_def to_ct_list_def)
thus ?thesis
using b_e_typing.intros(10,32)
by fastforce
qed
qed simp
next
case (13 \<C> ts)
thus ?case
proof (cases ts)
case (TopType x1)
consider
(1) "length x1 = 0"
| (2) "length x1 = 1"
| (3) "length x1 = 2"
| (4) "length x1 \<ge> 3"
by linarith
thus ?thesis
proof (cases)
case 1
hence "tm' = TopType [TAny]"
using TopType 13
by simp
then obtain t'' tm'' where tm_def:"tm = tm''@[t'']"
using 13(2) ct_suffix_def
by (simp, metis Nil_is_append_conv append_butlast_last_id checker_type.inject(1)
ct_prefixI ct_prefix_nil(2) produce.simps(1) produce_nil)
have "\<C> \<turnstile> [Select] : ([t'',t'',T_i32] _> [t''])"
using b_e_typing.intros(11)
by blast
thus ?thesis
using TopType 13 1 tm_def b_e_typing.intros(32) c_types_agree.simps(2) c_types_agree_top1
by fastforce
next
case 2
have "type_update_select (TopType x1) = tm'"
using 13 TopType
unfolding check_single.simps
by simp
hence x1_def:"ct_list_compat x1 [TSome T_i32]" "tm' = TopType [TAny]"
using type_update_select_length1[OF _ 2 13(4)]
by simp_all
then obtain t'' tm'' where tm_def:"tm = tm''@[t'']"
using 13(2) ct_suffix_def
by (metis Nil_is_append_conv append_butlast_last_id c_types_agree.simps(2) ct_prefixI
ct_prefix_nil(2) list.simps(8) to_ct_list_def)
have "c_types_agree (TopType x1) ((tm''@[t'',t''])@[T_i32])"
using x1_def(1)
by (metis c_types_agree_top2 list.simps(8,9) to_ct_list_def)
thus ?thesis
using TopType b_e_typing.intros(11,32) tm_def
by auto
next
case 3
have "type_update_select (TopType x1) = tm'"
using 13 TopType
unfolding check_single.simps
by simp
then obtain ct1 ct2 where x1_def:"x1 = [ct1, ct2]"
"ct_compat ct2 (TSome T_i32)"
"tm' = TopType [ct1]"
using type_update_select_length2[OF _ 3 13(4)]
by blast
then obtain t'' tm'' where tm_def:"tm = tm''@[t'']"
"ct_list_compat [ct1] [(TSome t'')]"
using 13(2) c_types_agree_imp_ct_list_compat[of "[ct1]" tm]
by (metis append_Nil2 append_butlast_last_id append_eq_append_conv_if append_eq_conv_conj
ct_list_compat_length diff_Suc_1 length_Cons length_butlast length_map
list.simps(8,9) list.size(3) nat.distinct(2) to_ct_list_def)
hence "ct_list_compat x1 (to_ct_list [ t'', T_i32])"
using x1_def(1,2)
unfolding ct_list_compat_def to_ct_list_def
by fastforce
hence "c_types_agree (TopType x1) ((tm''@[t''])@[t'',T_i32])"
using c_types_agree_top2
by blast
thus ?thesis
using TopType b_e_typing.intros(11,32) tm_def
by auto
next
case 4
then obtain nat where nat_def:"length x1 = Suc (Suc (Suc nat))"
by (metis add_eq_if diff_Suc_1 le_Suc_ex numeral_3_eq_3 nat.distinct(2))
hence tm'_def:"type_update_select (TopType x1) = tm'"
using 13 TopType
by simp
then obtain tm_int where "(select_return_top x1
(x1 ! (length x1 - 2))
(x1 ! (length x1 - 3))) = tm_int"
"tm_int \<noteq> Bot"
using nat_def 13(4)
unfolding type_update_select.simps
by fastforce
then obtain x2 where x2_def:"(select_return_top x1
(x1 ! (length x1 - 2))
(x1 ! (length x1 - 3))) = TopType x2"
using select_return_top_exists
by fastforce
have "ct_suffix x1 [TAny, TAny, TSome T_i32] \<or> ct_suffix [TAny, TAny, TSome T_i32] x1"
using tm'_def nat_def 13(4)
by (simp, metis (full_types) produce.simps(6))
hence tm'_eq:"tm' = TopType x2"
using tm'_def nat_def 13(4) x2_def
by force
then obtain cts' ct1 ct2 ct3 where cts'_def:"x1 = cts'@[ct1, ct2, ct3]"
"ct_compat ct3 (TSome T_i32)"
using type_update_select_length3 tm'_def 4
by blast
then obtain c' cm' where tm_def:"tm = cm'@[c']"
"ct_suffix cts' (to_ct_list cm')"
"ct_compat (x1 ! (length x1 - 2)) (TSome c')"
"ct_compat (x1 ! (length x1 - 3)) (TSome c')"
using select_return_top_ct_compat[OF x2_def 4] tm'_eq 4 13(2)
by fastforce
then obtain as bs where cm'_def:"cm' = as@bs"
"ct_list_compat (to_ct_list bs) cts'"
using ct_list_compat_cons_ct_list1 ct_list_compat_ts_conv_eq
by (metis ct_suffix_def to_ct_list_append(2))
hence "ct_compat ct1 (TSome c')"
"ct_compat ct2 (TSome c')"
using cts'_def tm_def
apply simp_all
apply (metis append.assoc append_Cons append_Nil length_append_singleton nth_append_length)
done
hence "c_types_agree ts (cm'@[c',c',T_i32])"
using c_types_agree_top2[of _ _ as] cm'_def(1) TopType
ct_list_compat_concat[OF ct_list_compat_commute[OF cm'_def(2)]] cts'_def
unfolding to_ct_list_def ct_list_compat_def
by fastforce
thus ?thesis
using b_e_typing.intros(11,32) tm_def
by auto
qed
next
(* TODO: refactor *)
case (Type x2)
hence x2_cond:"(length x2 \<ge> 3 \<and> (x2!(length x2-2)) = (x2!(length x2-3)))"
using 13
by (simp, meson)
hence tm'_def:"consume (Type x2) [TAny, TSome T_i32] = tm'"
using 13 Type
by simp
obtain ts' ts'' where cts_def:"x2 = ts'@ ts''" "length ts'' = 3"
using x2_cond
by (metis append_take_drop_id diff_diff_cancel length_drop)
then obtain t1 ts''2 where "ts'' = t1#ts''2" "length ts''2 = Suc (Suc 0)"
using List.length_Suc_conv[of ts' "Suc (Suc 0)"]
by (metis length_Suc_conv numeral_3_eq_3)
then obtain t2 t3 where "ts'' = [t1,t2,t3]"
using List.length_Suc_conv[of ts''2 "Suc 0"]
by (metis length_0_conv length_Suc_conv)
hence cts_def2:"x2 = ts'@ [t1,t2,t3]"
using cts_def
by simp
have ts'_suffix:"ct_suffix [TAny, TSome T_i32] (to_ct_list (ts' @ [t1, t2, t3]))"
using tm'_def 13(4)
by (simp, metis cts_def2)
hence tm'_def:"tm' = Type (ts'@[t1])"
using tm'_def 13(4) cts_def2
by simp
obtain as bs where "(to_ct_list (ts' @ [t1])) @ (to_ct_list ([t2, t3])) = as@bs"
"ct_list_compat bs [TAny, TSome T_i32]"
using ts'_suffix
unfolding ct_suffix_def to_ct_list_def
by fastforce
hence "t3 = T_i32"
unfolding to_ct_list_def ct_list_compat_def
by (metis (no_types, lifting) Nil_is_map_conv append_eq_append_conv ct_compat.simps(1)
length_Cons list.sel(1,3) list.simps(9) list_all2_Cons2 list_all2_lengthD)
moreover
have "t1 = t2"
using x2_cond cts_def2
by (simp, metis append.left_neutral append_Cons append_assoc length_append_singleton
nth_append_length)
ultimately
have "c_types_agree (Type x2) ((ts'@[t1,t1])@[T_i32])"
using cts_def2
by simp
thus ?thesis
using b_e_typing.intros(11,32) Type tm'_def 13(2)
by fastforce
qed simp
next
case (14 \<C> tn'' tm'' es ts)
hence "type_update ts (to_ct_list tn'') (Type tm'') = tm'"
by auto
moreover
have "(b_e_type_checker (\<C>\<lparr>label := ([tm''] @ (label \<C>))\<rparr>) es (tn'' _> tm''))"
using 14
by (simp, meson)
hence "\<C> \<turnstile> [Block (tn'' _> tm'') es] : (tn'' _> tm'')"
using b_e_typing.intros(12)[OF _ 14(1)]
by blast
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 14(4,5,3)]
by blast
next
case (15 \<C> tn'' tm'' es ts)
hence "type_update ts (to_ct_list tn'') (Type tm'') = tm'"
by auto
moreover
have "(b_e_type_checker (\<C>\<lparr>label := ([tn''] @ (label \<C>))\<rparr>) es (tn'' _> tm''))"
using 15
by (simp, meson)
hence "\<C> \<turnstile> [Loop (tn'' _> tm'') es] : (tn'' _> tm'')"
using b_e_typing.intros(13)[OF _ 15(1)]
by blast
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 15(4,5,3)]
by blast
next
case (16 \<C> tn'' tm'' es1 es2 ts)
hence "type_update ts (to_ct_list (tn''@[T_i32])) (Type tm'') = tm'"
by auto
moreover
have "(b_e_type_checker (\<C>\<lparr>label := ([tm''] @ (label \<C>))\<rparr>) es1 (tn'' _> tm''))"
"(b_e_type_checker (\<C>\<lparr>label := ([tm''] @ (label \<C>))\<rparr>) es2 (tn'' _> tm''))"
using 16
by (simp, meson)+
hence "\<C> \<turnstile> [If (tn'' _> tm'') es1 es2] : (tn''@[T_i32] _> tm'')"
using b_e_typing.intros(14)[OF _ 16(1,2)]
by blast
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 16(5,6,4)]
by blast
next
case (17 \<C> i ts)
hence "type_update ts (to_ct_list ((label \<C>)!i)) (TopType []) = tm'"
by auto
moreover
have "i < length (label \<C>)"
using 17
by (simp, meson)
ultimately
show ?case
using b_e_check_single_top_not_bot_sound[OF _ 17(3,4)]
b_e_typing.intros(15)
b_e_typing.intros(32)
by (metis suffix_def)
next
case (18 \<C> i ts)
hence "type_update ts (to_ct_list ((label \<C>)!i @ [T_i32])) (Type ((label \<C>)!i)) = tm'"
by auto
moreover
have "i < length (label \<C>)"
using 18
by (simp, meson)
hence "\<C> \<turnstile> [Br_if i] : ((label \<C>)!i @ [T_i32] _> (label \<C>)!i)"
using b_e_typing.intros(16)
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 18(3,4,2)]
by fastforce
next
case (19 \<C> "is" i ts)
then obtain tls where tls_def:"(same_lab (is@[i]) (label \<C>)) = Some tls"
by fastforce
hence "type_update ts (to_ct_list (tls @ [T_i32])) (TopType []) = tm'"
using 19
by simp
thus ?case
using b_e_check_single_top_not_bot_sound[OF _ 19(3,4)]
b_e_typing.intros(17)[OF same_lab_conv_list_all[OF tls_def]]
b_e_typing.intros(32)
by (metis suffix_def)
next
case (20 \<C> ts)
then obtain ts_r where "(return \<C>) = Some ts_r"
by fastforce
moreover
hence "type_update ts (to_ct_list ts_r) (TopType []) = tm'"
using 20
by simp
ultimately
show ?case
using b_e_check_single_top_not_bot_sound[OF _ 20(3,4)]
b_e_typing.intros(18,32)
by (metis suffix_def)
next
case (21 \<C> i ts)
obtain tn'' tm'' where func_def:"(func_t \<C>)!i = (tn'' _> tm'')"
using tf.exhaust
by blast
hence "type_update ts (to_ct_list tn'') (Type tm'') = tm'"
using 21
by auto
moreover
have "i < length (func_t \<C>)"
using 21
by (simp, meson)
hence "\<C> \<turnstile> [Call i] : (tn'' _> tm'')"
using b_e_typing.intros(19) func_def
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 21(3,4,2)]
by fastforce
next
case (22 \<C> i ts)
obtain tn'' tm'' where type_def:"(types_t \<C>)!i = (tn'' _> tm'')"
using tf.exhaust
by blast
hence "type_update ts (to_ct_list (tn''@[T_i32])) (Type tm'') = tm'"
using 22
by auto
moreover
have "length (table \<C>) \<ge> 1 \<and> i < length (types_t \<C>)"
using 22
by (simp, meson)
hence "\<C> \<turnstile> [Call_indirect i] : (tn''@[T_i32] _> tm'')"
using b_e_typing.intros(20) type_def
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 22(3,4,2)]
by fastforce
next
case (23 \<C> i ts)
hence "type_update ts [] (Type [(local \<C>)!i]) = tm'"
by auto
moreover
have "i < length (local \<C>)"
using 23
by (simp, meson)
hence "\<C> \<turnstile> [Get_local i] : ([] _> [(local \<C>)!i])"
using b_e_typing.intros(21)
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 23(3,4,2)]
unfolding to_ct_list_def
by (metis list.map_disc_iff)
next
case (24 \<C> i ts)
hence "type_update ts (to_ct_list [(local \<C>)!i]) (Type []) = tm'"
unfolding to_ct_list_def
by auto
moreover
have "i < length (local \<C>)"
using 24
by (simp, meson)
hence "\<C> \<turnstile> [Set_local i] : ([(local \<C>)!i] _> [])"
using b_e_typing.intros(22)
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 24(3,4,2)]
by fastforce
next
case (25 \<C> i ts)
hence "type_update ts (to_ct_list [(local \<C>)!i]) (Type [(local \<C>)!i]) = tm'"
unfolding to_ct_list_def
by auto
moreover
have "i < length (local \<C>)"
using 25
by (simp, meson)
hence "\<C> \<turnstile> [Tee_local i] : ([(local \<C>)!i] _> [(local \<C>)!i])"
using b_e_typing.intros(23)
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 25(3,4,2)]
by fastforce
next
case (26 \<C> i ts)
hence "type_update ts [] (Type [tg_t ((global \<C>)!i)]) = tm'"
by auto
moreover
have "i < length (global \<C>)"
using 26
by (simp, meson)
hence "\<C> \<turnstile> [Get_global i] : ([] _> [tg_t ((global \<C>)!i)])"
using b_e_typing.intros(24)
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 26(3,4,2)]
unfolding to_ct_list_def
by (metis list.map_disc_iff)
next
case (27 \<C> i ts)
hence "type_update ts (to_ct_list [tg_t ((global \<C>)!i)]) (Type []) = tm'"
unfolding to_ct_list_def
by auto
moreover
have "i < length (global \<C>) \<and> is_mut (global \<C> ! i)"
using 27
by (simp, meson)
then obtain t where "(global \<C> ! i) = \<lparr>tg_mut = T_mut, tg_t = t\<rparr>" "i < length (global \<C>)"
unfolding is_mut_def
by (cases "global \<C> ! i", auto)
hence "\<C> \<turnstile> [Set_global i] : ([tg_t (global \<C> ! i)] _> [])"
using b_e_typing.intros(25)[of i \<C> "tg_t (global \<C> ! i)"]
unfolding is_mut_def tg_t_def
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 27(3,4,2)]
by fastforce
next
case (28 \<C> t tp_sx a off ts)
hence "type_update ts (to_ct_list [T_i32]) (Type [t]) = tm'"
unfolding to_ct_list_def
by auto
moreover
have "length (memory \<C>) \<ge> 1 \<and> load_store_t_bounds a (option_projl tp_sx) t"
using 28
by (simp, meson)
hence "\<C> \<turnstile> [Load t tp_sx a off] : ([T_i32] _> [t])"
using b_e_typing.intros(26)
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 28(3,4,2)]
by fastforce
next
case (29 \<C> t tp a off ts)
hence "type_update ts (to_ct_list [T_i32,t]) (Type []) = tm'"
unfolding to_ct_list_def
by auto
moreover
have "length (memory \<C>) \<ge> 1 \<and> load_store_t_bounds a tp t"
using 29
by (simp, meson)
hence "\<C> \<turnstile> [Store t tp a off] : ([T_i32,t] _> [])"
using b_e_typing.intros(27)
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 29(3,4,2)]
by fastforce
next
case (30 \<C> ts)
hence "type_update ts [] (Type [T_i32]) = tm'"
by auto
moreover
have "length (memory \<C>) \<ge> 1 "
using 30
by (simp, meson)
hence "\<C> \<turnstile> [Current_memory] : ([] _> [T_i32])"
using b_e_typing.intros(28)
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 30(3,4,2)]
unfolding to_ct_list_def
by (metis list.map_disc_iff)
next
case (31 \<C> ts)
hence "type_update ts (to_ct_list [T_i32]) (Type [T_i32]) = tm'"
unfolding to_ct_list_def
by auto
moreover
have "length (memory \<C>) \<ge> 1 "
using 31
by (simp, meson)
hence "\<C> \<turnstile> [Grow_memory] : ([T_i32] _> [T_i32])"
using b_e_typing.intros(29)
by fastforce
ultimately
show ?case
using b_e_check_single_type_not_bot_sound[OF _ 31(3,4,2)]
by fastforce
qed
thus ?thesis
using assms
by simp
qed
subsection {* Completeness *}
lemma check_single_imp:
assumes "check_single \<C> e ctn = ctm"
"ctm \<noteq> Bot"
shows "check_single \<C> e = id
\<or> check_single \<C> e = (\<lambda>ctn. type_update_select ctn)
\<or> (\<exists>cons prods. (check_single \<C> e = (\<lambda>ctn. type_update ctn cons prods)))"
using assms
apply (cases rule: check_single.cases[of "(\<C>, e, ctn)"])
apply (fastforce split: if_splits option.splits tf.splits)+
done
lemma check_equiv_fold:
"check \<C> es ts = foldl (\<lambda> ts e. (case ts of Bot \<Rightarrow> Bot | _ \<Rightarrow> check_single \<C> e ts)) ts es"
proof (induction es arbitrary: ts)
case Nil
thus ?case
by simp
next
case (Cons e es)
obtain ts' where ts'_def:"check \<C> (e # es) ts = ts'"
by blast
show ?case
proof (cases "ts = Bot")
case True
thus ?thesis
using ts'_def
by (induction es, simp_all)
next
case False
thus ?thesis
using ts'_def Cons
by (cases ts, simp_all)
qed
qed
lemma check_neq_bot_snoc:
assumes "check \<C> (es@[e]) ts \<noteq> Bot"
shows "check \<C> es ts \<noteq> Bot"
using assms
proof (induction es arbitrary: ts)
case Nil
thus ?case
by (cases ts, simp_all)
next
case (Cons a es)
thus ?case
by (cases ts, simp_all)
qed
lemma check_unfold_snoc:
assumes "check \<C> es ts \<noteq> Bot"
shows "check \<C> (es@[e]) ts = check_single \<C> e (check \<C> es ts)"
proof -
obtain f where f_def:"f = (\<lambda> e ts. (case ts of Bot \<Rightarrow> Bot | _ \<Rightarrow> check_single \<C> e ts))"
by blast
have f_simp:"\<And>ts. ts \<noteq> Bot \<Longrightarrow> (f e ts = check_single \<C> e ts)"
proof -
fix ts
show "ts \<noteq> Bot \<Longrightarrow> (f e ts = check_single \<C> e ts)"
using f_def
by (cases ts, simp_all)
qed
have "check \<C> (es@[e]) ts = foldl (\<lambda> ts e. (case ts of Bot \<Rightarrow> Bot | _ \<Rightarrow> check_single \<C> e ts)) ts (es@[e])"
using check_equiv_fold
by simp
also
have "... = foldr (\<lambda> e ts. (case ts of Bot \<Rightarrow> Bot | _ \<Rightarrow> check_single \<C> e ts)) (rev (es@[e])) ts"
using foldl_conv_foldr
by fastforce
also
have "... = f e (foldr (\<lambda> e ts. (case ts of Bot \<Rightarrow> Bot | _ \<Rightarrow> check_single \<C> e ts)) (rev es) ts)"
using f_def
by simp
also
have "... = f e (check \<C> es ts)"
using foldr_conv_foldl[of _ "(rev es)" ts] rev_rev_ident[of es] check_equiv_fold
by simp
also
have "... = check_single \<C> e (check \<C> es ts)"
using assms f_simp
by simp
finally
show ?thesis .
qed
lemma check_single_imp_weakening:
assumes "check_single \<C> e (Type t1s) = ctm"
"ctm \<noteq> Bot"
"c_types_agree ctn t1s"
"c_types_agree ctm t2s"
shows "\<exists>ctm'. check_single \<C> e ctn = ctm' \<and> c_types_agree ctm' t2s"
proof -
consider (1) "check_single \<C> e = id"
| (2) "check_single \<C> e = (\<lambda>ctn. type_update_select ctn)"
| (3) "(\<exists>cons prods. (check_single \<C> e = (\<lambda>ctn. type_update ctn cons prods)))"
using check_single_imp assms
by blast
thus ?thesis
proof (cases)
case 1
thus ?thesis
using assms(1,3,4)
by fastforce
next
(* TODO: better proof *)
case 2
note outer_2 = 2
hence t1s_cond:"(length t1s \<ge> 3 \<and> (t1s!(length t1s-2)) = (t1s!(length t1s-3)))"
using assms(1,2)
by (simp, meson)
hence ctm_def:"ctm = consume (Type t1s) [TAny, TSome T_i32]"
using assms(1,2) 2
by simp
then obtain c_t where c_t_def:"ctm = Type c_t"
using assms(2)
by (meson consume.simps(1))
hence t2s_eq:"t2s = c_t"
using assms(4)
by simp
hence t2s_len:"length t2s > 0"
using t1s_cond ctm_def c_t_def assms(2)
by (metis Suc_leI Suc_n_not_le_n checker_type.inject(2) consume.simps(1)
diff_is_0_eq dual_order.trans length_0_conv length_Cons length_greater_0_conv
nat.simps(3) numeral_3_eq_3 take_eq_Nil)
have t1s_suffix_full:"ct_suffix [TAny, TSome T_i32] (to_ct_list t1s)"
using assms(2) ctm_def ct_suffix_less
by (metis consume.simps(1))
hence t1s_suffix:"ct_suffix [TSome T_i32] (to_ct_list t1s)"
using assms(2) ctm_def ct_suffix_less
by (metis append_butlast_last_id last.simps list.distinct(1))
obtain t t1s' where t1s_suffix2:"t1s = t1s'@[t,t,T_i32]"
using type_update_select_type_length3 assms(1) c_t_def outer_2
by fastforce
hence t2s_def:"t2s = t1s'@[t]"
using ctm_def c_t_def t2s_eq t1s_suffix assms(2) t1s_suffix_full
by simp
show ?thesis
using assms(1,3,4)
proof (cases ctn)
case (TopType x1)
consider
(1) "length x1 = 0"
| (2) "length x1 = 1"
| (3) "length x1 = 2"
| (4) "length x1 \<ge> 3"
by linarith
thus ?thesis
proof (cases)
case 1
hence "check_single \<C> e ctn = TopType [TAny]"
using 2 TopType
by simp
thus ?thesis
using ct_suffix_singleton to_ct_list_def t2s_len
by auto
next
case 2
hence "ct_suffix [TSome T_i32] x1"
using assms(3) TopType ct_suffix_imp_ct_list_compat ct_suffix_shared t1s_suffix
by (metis One_nat_def append_Nil c_types_agree.simps(2) ct_list_compat_commute ct_suffix_def
diff_self_eq_0 drop_0 length_Cons list.size(3))
hence "check_single \<C> e ctn = TopType [TAny]"
using outer_2 TopType 2
by simp
thus ?thesis
using t2s_len ct_suffix_singleton
by (simp add: to_ct_list_def)
next
case 3
have "ct_list_compat (to_ct_list t1s) (to_ct_list (t1s' @ [t, t, T_i32]))"
using t1s_suffix2
by (simp add: ct_list_compat_ts_conv_eq)
hence temp1:"to_ct_list t1s = (to_ct_list (t1s' @ [t])) @ (to_ct_list [t, T_i32])"
using t1s_suffix2 to_ct_list_def
by simp
hence "ct_suffix (to_ct_list [t, T_i32]) (to_ct_list t1s)"
using ct_suffix_def[of "(to_ct_list [t, T_i32])" "(to_ct_list t1s)"]
by (simp add: ct_suffix_cons_it)