-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4_exec_semScript.sml
2820 lines (2611 loc) · 78 KB
/
p4_exec_semScript.sml
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
open HolKernel boolLib Parse bossLib;
val _ = new_theory "p4_exec_sem";
open p4Syntax;
open ottTheory;
open p4Theory p4_auxTheory;
(*********************************)
(* Expression-related shorthands *)
Definition is_v_def:
(is_v (e_v v) = T) /\
(is_v _ = F)
End
Definition get_v_def:
(get_v (e_v v) = SOME v) /\
(get_v _ = NONE)
End
Definition is_v_bool_def:
(is_v_bool (e_v (v_bool b)) = T) /\
(is_v_bool _ = F)
End
Definition is_v_bit_def:
(is_v_bit (e_v (v_bit bitv)) = T) /\
(is_v_bit _ = F)
End
(* NOTE: Error messages serialised using 32 bits *)
Definition is_v_err_def:
(is_v_err (e_v (v_bit (bl, 32))) = T) /\
(is_v_err _ = F)
End
Definition is_v_str_def:
(is_v_str (e_v (v_str x)) = T) /\
(is_v_str _ = F)
End
Definition is_var_def:
(is_var (e_var x) = T) /\
(is_var _ = F)
End
Definition unop_exec_def:
(unop_exec unop_neg (v_bool b) = SOME (v_bool ~b))
/\
(unop_exec unop_compl (v_bit bitv) = SOME (v_bit (bitv_bl_unop bnot bitv)))
/\
(unop_exec unop_neg_signed (v_bit bitv) = SOME (v_bit (bitv_unop unop_neg_signed bitv)))
/\
(unop_exec unop_un_plus (v_bit bitv) = SOME (v_bit bitv))
/\
(unop_exec unop v = NONE)
End
Definition e_exec_unop_def:
(e_exec_unop unop (e_v v) = unop_exec unop v)
/\
(e_exec_unop _ _ = NONE)
End
Definition cast_exec_def:
(cast_exec (cast_unsigned n) (v_bit bitv) = SOME (v_bit $ bitv_cast n bitv))
/\
(cast_exec (cast_unsigned n) (v_bool b) = SOME (v_bit $ bool_cast n b))
/\
(cast_exec (cast_bool) (v_bit bitv) = SOME (v_bool $ to_bool_cast bitv))
/\
(cast_exec _ _ = NONE)
End
Definition e_exec_cast_def:
(e_exec_cast (cast_unsigned n) (e_v v) = cast_exec (cast_unsigned n) v)
/\
(e_exec_cast (cast_bool) (e_v v) = cast_exec (cast_bool) v)
/\
(e_exec_cast _ _ = NONE)
End
(* TODO: Split binop into binop, binpred, ... to reduce copypaste? *)
Definition binop_exec_def:
(binop_exec binop_mul (v_bit bitv1) (v_bit bitv2) =
case bitv_binop binop_mul bitv1 bitv2 of
| SOME bitv3 => SOME (v_bit bitv3)
| NONE => NONE)
/\
(binop_exec binop_div (v_bit bitv1) (v_bit bitv2) =
case bitv_binop binop_div bitv1 bitv2 of
| SOME bitv3 => SOME (v_bit bitv3)
| NONE => NONE)
/\
(binop_exec binop_mod (v_bit bitv1) (v_bit bitv2) =
case bitv_binop binop_mod bitv1 bitv2 of
| SOME bitv3 => SOME (v_bit bitv3)
| NONE => NONE)
/\
(binop_exec binop_add (v_bit bitv1) (v_bit bitv2) =
case bitv_binop binop_add bitv1 bitv2 of
| SOME bitv3 => SOME (v_bit bitv3)
| NONE => NONE)
/\
(binop_exec binop_sat_add (v_bit bitv1) (v_bit bitv2) =
case bitv_binop binop_sat_add bitv1 bitv2 of
| SOME bitv3 => SOME (v_bit bitv3)
| NONE => NONE)
/\
(binop_exec binop_sub (v_bit bitv1) (v_bit bitv2) =
case bitv_binop binop_sub bitv1 bitv2 of
| SOME bitv3 => SOME (v_bit bitv3)
| NONE => NONE)
/\
(binop_exec binop_sat_sub (v_bit bitv1) (v_bit bitv2) =
case bitv_binop binop_sat_sub bitv1 bitv2 of
| SOME bitv3 => SOME (v_bit bitv3)
| NONE => NONE)
/\
(binop_exec binop_shl (v_bit bitv1) (v_bit bitv2) =
SOME (v_bit (bitv_bl_binop shiftl bitv1 ((\(bl, n). (v2n bl, n)) bitv2))))
/\
(binop_exec binop_shr (v_bit bitv1) (v_bit bitv2) =
SOME (v_bit (bitv_bl_binop shiftr bitv1 ((\(bl, n). (v2n bl, n)) bitv2))))
/\
(binop_exec binop_le (v_bit bitv1) (v_bit bitv2) =
case bitv_binpred binop_le bitv1 bitv2 of
| SOME b => SOME (v_bool b)
| NONE => NONE)
/\
(binop_exec binop_ge (v_bit bitv1) (v_bit bitv2) =
case bitv_binpred binop_ge bitv1 bitv2 of
| SOME b => SOME (v_bool b)
| NONE => NONE)
/\
(binop_exec binop_lt (v_bit bitv1) (v_bit bitv2) =
case bitv_binpred binop_lt bitv1 bitv2 of
| SOME b => SOME (v_bool b)
| NONE => NONE)
/\
(binop_exec binop_gt (v_bit bitv1) (v_bit bitv2) =
case bitv_binpred binop_gt bitv1 bitv2 of
| SOME b => SOME (v_bool b)
| NONE => NONE)
/\
(* TODO: This would generalize easily in theory, but
* gives rise to enormously many autogenerated cases *)
(binop_exec binop_neq (v_bit bitv1) (v_bit bitv2) =
SOME (v_bool (bitv1 <> bitv2)))
/\
(binop_exec binop_neq (v_bool b1) (v_bool b2) =
SOME (v_bool (b1 <> b2)))
/\
(binop_exec binop_eq (v_bit bitv1) (v_bit bitv2) =
SOME (v_bool (bitv1 = bitv2)))
/\
(binop_exec binop_eq (v_bool b1) (v_bool b2) =
SOME (v_bool (b1 = b2)))
/\
(binop_exec binop_and (v_bit bitv1) (v_bit bitv2) =
SOME (v_bit (bitv_bl_binop band bitv1 bitv2)))
/\
(binop_exec binop_xor (v_bit bitv1) (v_bit bitv2) =
SOME (v_bit (bitv_bl_binop bxor bitv1 bitv2)))
/\
(binop_exec binop_or (v_bit bitv1) (v_bit bitv2) =
SOME (v_bit (bitv_bl_binop bor bitv1 bitv2)))
/\
(binop_exec binop v1 v2 = NONE)
End
Definition e_exec_binop_def:
(e_exec_binop (e_v v1) binop (e_v v2) = binop_exec binop v1 v2)
/\
(e_exec_binop _ _ _ = NONE)
End
Definition e_exec_short_circuit_def:
(e_exec_short_circuit (v_bool T) binop_bin_and e = SOME e)
/\
(e_exec_short_circuit (v_bool F) binop_bin_and e = SOME (e_v (v_bool F)))
/\
(e_exec_short_circuit (v_bool T) binop_bin_or e = SOME (e_v (v_bool T)))
/\
(e_exec_short_circuit (v_bool F) binop_bin_or e = SOME e)
/\
(e_exec_short_circuit _ _ _ = NONE)
End
(* Field access *)
Definition e_exec_acc_def:
(e_exec_acc (e_acc (e_v (v_struct f_v_list)) f) =
case FIND (\(k, v). k = f) f_v_list of
| SOME (f, v) => SOME (e_v v)
| NONE => NONE)
/\
(e_exec_acc (e_acc (e_v (v_header boolv f_v_list)) f) =
case FIND (\(k, v). k = f) f_v_list of
| SOME (f, v) => SOME (e_v v)
| NONE => NONE)
/\
(e_exec_acc _ = NONE)
End
Definition e_exec_select_def:
(e_exec_select (e_v v) s_l_x_l x =
case v of
| v_struct x_v_l =>
(case (FIND (\ (s_list, x'). match_all (ZIP(SND $ UNZIP x_v_l,s_list))) s_l_x_l) of
| SOME (s_list, x') => SOME x'
| NONE => SOME x)
| _ => SOME x) /\
(e_exec_select _ _ _ = NONE)
End
Definition e_exec_concat_def:
(e_exec_concat (e_v (v_bit bitv1)) (e_v (v_bit bitv2)) =
SOME (v_bit (bitv_concat bitv1 bitv2)))
/\
(e_exec_concat _ _ = NONE)
End
Definition e_exec_slice_def:
(e_exec_slice (e_v (v_bit bitv1)) (e_v (v_bit bitv2)) (e_v (v_bit bitv3)) =
SOME (v_bit (slice bitv1 bitv2 bitv3)))
/\
(e_exec_slice _ _ _ = NONE)
End
(********************************)
(* Statement-related shorthands *)
Definition is_empty_def:
(is_empty stmt_empty = T) /\
(is_empty _ = F)
End
Definition is_empty_singleton_def:
(is_empty_singleton [stmt_empty] = T) /\
(is_empty_singleton _ = F)
End
Definition get_ret_v_def:
(get_ret_v (stmt_ret (e_v v)) = SOME v) /\
(get_ret_v (stmt_seq (stmt_ret (e_v v)) stmt) = SOME v) /\
(get_ret_v _ = NONE)
End
Definition stmt_exec_ass_def:
(stmt_exec_ass lval (e_v v) ss =
assign ss v lval)
/\
(stmt_exec_ass _ _ _ = NONE)
End
Definition stmt_exec_init_def:
(stmt_exec_init varn (e_v v) ss = initialise ss varn v)
End
Definition stmt_exec_trans_def:
(stmt_exec_trans (e_v (v_str x)) = SOME (status_trans x))
/\
(stmt_exec_trans _ = NONE)
End
Definition stmt_exec_cond_def:
(stmt_exec_cond (e_v (v_bool T)) =
SOME T)
/\
(stmt_exec_cond (e_v (v_bool F)) =
SOME F)
/\
(stmt_exec_cond _ = NONE)
End
Definition e_state_size_def:
(e_state_size ((ctx:'a ctx), (g_scope_list:g_scope_list), (scope_list:scope_list), (e:e)) = e_size e)
End
(* TODO: Write explicit NONE-reducing clauses for operands of wrong types?
* This would reduce warnings *)
(* TODO: Use let-statements to avoid duplicate "MAP FST", et cetera *)
(* Defn.tgoal (Hol_defn "e_exec" ` *)
Definition e_exec_def:
(********************)
(* Variable look-up *)
(e_exec (ctx:'a ctx) (g_scope_list:g_scope_list) (scope_list:scope_list) (e_var x) =
case lookup_vexp2 scope_list g_scope_list x of
| SOME v => SOME (e_v v, [])
| NONE => NONE)
/\
(******************************)
(* Struct/header field access *)
(e_exec ctx g_scope_list scope_list (e_acc e_v_struct x) =
if is_v e_v_struct
then
(case e_exec_acc (e_acc e_v_struct x) of
| SOME v => SOME (v, [])
| NONE => NONE)
else
(case e_exec ctx g_scope_list scope_list e_v_struct of
| SOME (e_v_struct', frame_list) =>
SOME (e_acc e_v_struct' x, frame_list)
| NONE => NONE))
/\
(*********************************)
(* Struct/header field reduction *)
(e_exec ctx g_scope_list scope_list (e_struct x_e_l) =
case unred_mem_index (MAP SND x_e_l) of
| SOME i =>
(case e_exec ctx g_scope_list scope_list (EL i (MAP SND x_e_l)) of
| SOME (e', frame_list) => SOME (e_struct (ZIP (MAP FST x_e_l, (LUPDATE e' i (MAP SND x_e_l)))), frame_list)
| NONE => NONE)
| NONE => SOME (e_v (v_struct (ZIP (MAP FST x_e_l, vl_of_el (MAP SND x_e_l)))), []))
/\
(************************)
(* Function/extern call *)
(e_exec (apply_table_f, ext_map, func_map, b_func_map, pars_map, tbl_map) g_scope_list scope_list (e_call funn e_l) =
(case lookup_funn_sig_body funn func_map b_func_map ext_map of
| SOME (stmt, x_d_l) =>
if LENGTH x_d_l = LENGTH e_l
then
(case unred_arg_index (MAP SND x_d_l) e_l of
| SOME i =>
(case e_exec (apply_table_f, ext_map, func_map, b_func_map, pars_map, tbl_map) g_scope_list scope_list (EL i e_l) of
| SOME (e', frame_list) => SOME (e_call funn (LUPDATE e' i e_l), frame_list)
| NONE => NONE)
| NONE =>
(case copyin (MAP FST x_d_l) (MAP SND x_d_l) e_l g_scope_list scope_list of
| SOME scope =>
SOME (e_var (varn_star funn), [(funn, [stmt], [scope])])
| NONE => NONE))
else NONE
| NONE => NONE))
/\
(********)
(* Cast *)
(e_exec ctx g_scope_list scope_list (e_cast cast e) =
if is_v e
then
(case e_exec_cast cast e of
| SOME v => SOME (e_v v, [])
| NONE => NONE)
else
(case e_exec ctx g_scope_list scope_list e of
| SOME (e', frame_list) => SOME (e_cast cast e', frame_list)
| NONE => NONE))
/\
(********************)
(* Unary arithmetic *)
(e_exec ctx g_scope_list scope_list (e_unop unop e) =
if is_v e
then
(case e_exec_unop unop e of
| SOME v => SOME (e_v v, [])
| NONE => NONE)
else
(case e_exec ctx g_scope_list scope_list e of
| SOME (e', frame_list) => SOME (e_unop unop e', frame_list)
| NONE => NONE))
/\
(*********************)
(* Binary arithmetic *)
(e_exec ctx g_scope_list scope_list (e_binop e1 binop e2) =
(case e1 of
| (e_v v) =>
if is_short_circuitable binop
then
(case e_exec_short_circuit v binop e2 of
| SOME e' => SOME (e', [])
| NONE => NONE)
else if is_v e2
then
(case e_exec_binop e1 binop e2 of
| SOME v' => SOME (e_v v', [])
| NONE => NONE)
else
(case e_exec ctx g_scope_list scope_list e2 of
| SOME (e2', frame_list) => SOME (e_binop e1 binop e2', frame_list)
| NONE => NONE)
| _ =>
(case e_exec ctx g_scope_list scope_list e1 of
| SOME (e1', frame_list) => SOME (e_binop e1' binop e2, frame_list)
| NONE => NONE)))
/\
(**********)
(* Select *)
(e_exec ctx g_scope_list scope_list (e_select e s_l_x_l x) =
if is_v e
then
(case e_exec_select e s_l_x_l x of
| SOME x' => SOME (e_v (v_str x'), [])
| NONE => NONE)
else
(case e_exec ctx g_scope_list scope_list e of
| SOME (e', frame_list) => SOME (e_select e' s_l_x_l x, frame_list)
| NONE => NONE))
/\
(*****************)
(* Concatenation *)
(e_exec ctx g_scope_list scope_list (e_concat e1 e2) =
if is_v_bit e1
then
(if is_v_bit e2
then
(case e_exec_concat e1 e2 of
| SOME v => SOME (e_v v, [])
| NONE => NONE)
else
(case e_exec ctx g_scope_list scope_list e2 of
| SOME (e2', frame_list) => SOME (e_concat e1 e2', frame_list)
| NONE => NONE))
else
(case e_exec ctx g_scope_list scope_list e1 of
| SOME (e1', frame_list) => SOME (e_concat e1' e2, frame_list)
| NONE => NONE))
/\
(***********)
(* Slicing *)
(e_exec ctx g_scope_list scope_list (e_slice e1 e2 e3) =
if (is_v_bit e2 /\ is_v_bit e3)
then
(if is_v_bit e1
then
(case e_exec_slice e1 e2 e3 of
| SOME v => SOME (e_v v, [])
| NONE => NONE)
else
(case e_exec ctx g_scope_list scope_list e1 of
| SOME (e1', frame_list) => SOME (e_slice e1' e2 e3, frame_list)
| NONE => NONE))
else NONE)
/\
(e_exec _ _ _ _ = NONE)
Termination
WF_REL_TAC `measure e_state_size` >>
fs [e_state_size_def, e_size_def] >>
REPEAT STRIP_TAC >| [
IMP_RES_TAC unred_arg_index_in_range >>
IMP_RES_TAC rich_listTheory.EL_MEM >>
IMP_RES_TAC e3_size_mem >>
fs [],
IMP_RES_TAC unred_mem_index_in_range >>
IMP_RES_TAC rich_listTheory.EL_MEM >>
`e_size (EL i (MAP SND x_e_l)) < e1_size x_e_l` suffices_by (
fs []
) >>
`e2_size (EL i (MAP FST x_e_l), EL i (MAP SND x_e_l)) < e1_size x_e_l` suffices_by (
rpt strip_tac >>
irule arithmeticTheory.LESS_TRANS >>
qexists_tac `e2_size (EL i (MAP FST x_e_l),EL i (MAP SND x_e_l))` >>
fs [e_e2_size_less]
) >>
subgoal `MEM (EL i x_e_l) x_e_l` >- (
irule rich_listTheory.EL_MEM >>
fs [listTheory.LENGTH_MAP]
) >>
imp_res_tac e1_size_mem >>
metis_tac [EL_pair_list, listTheory.LENGTH_MAP]
]
End
(*
val e_exec_ind = tryfind (uncurry DB.fetch) [("scratch", "e_exec_ind"),
("p4_exec_sem", "e_exec_ind")];
val _ = save_thm("e_exec_ind", e_exec_ind);
*)
(*
val measure_stmt_state_size_def = Define `
(measure_stmt_state_size ((ctx:ctx), (g_scope_list:g_scope_list), (frame_list:frame_list), (ctrl:ctrl), (status:status))
((ctx':ctx), (g_scope_list':g_scope_list), (frame_list':frame_list), (ctrl':ctrl), (status':status)) =
if LENGTH frame_list <> LENGTH frame_list'
then LENGTH frame_list < LENGTH frame_list'
else if LENGTH (FST $ SND $ HD frame_list) <> LENGTH (FST $ SND $ HD frame_list')
then LENGTH (FST $ SND $ HD frame_list) < LENGTH (FST $ SND $ HD frame_list')
else (stmt_size (HD $ FST $ SND $ HD frame_list) < stmt_size (HD $ FST $ SND $ HD frame_list')))
`;
*)
Definition is_consts_exec_def:
(is_consts_exec [] = T) /\
(is_consts_exec (h::t) = ((is_const h) /\ (is_consts_exec t)))
End
Theorem vl_of_el_MAP_e_v:
!el.
is_consts_exec el ==>
el = MAP e_v (vl_of_el el)
Proof
Induct_on `el` >- (
fs [is_consts_exec_def, vl_of_el_def]
) >>
rpt strip_tac >>
fs [is_consts_exec_def, vl_of_el_def] >>
Cases_on `h` >> (
fs [is_const_def]
) >>
fs [v_of_e_def]
QED
Definition stmt_exec_def:
(******************************************)
(* Catch-all clauses for special statuses *)
(stmt_exec (ctx:'a ctx) ((ascope:'a, g_scope_list:g_scope_list, frame_list:frame_list, status_returnv v):'a state) = NONE)
/\
(stmt_exec _ (_, _, _, status_trans x) = NONE)
/\
(* Empty frame list *)
(stmt_exec _ (_, _, [], _) = NONE)
/\
(* Empty scope stack *)
(stmt_exec _ (_, _, [(funn, stmt_stack, [])], _) = NONE)
/\
(**************)
(* Assignment *)
(stmt_exec ctx (ascope, g_scope_list, [(funn, [stmt_ass lval e], scope_list)], status_running) =
if is_v e
then
(case stmt_exec_ass lval e (scope_list++g_scope_list) of
| SOME scope_list'' =>
(case separate scope_list'' of
| (SOME g_scope_list', SOME scope_list') =>
SOME (ascope, g_scope_list', [(funn, [stmt_empty], scope_list')], status_running)
| _ => NONE)
| NONE => NONE)
else
(case e_exec ctx g_scope_list scope_list e of
| SOME (e', frame_list) =>
SOME (ascope, g_scope_list, frame_list++[(funn, [stmt_ass lval e'], scope_list)], status_running)
| _ => NONE))
/\
(**************)
(* Transition *)
(stmt_exec ctx (ascope, g_scope_list, [(funn, [stmt_trans e], scope_list)], status_running) =
if is_v e
then
if is_v_str e
then
(case stmt_exec_trans e of
| SOME status' => SOME (ascope, g_scope_list, [(funn, [stmt_empty], scope_list)], status')
| NONE => NONE)
else NONE
else
(case e_exec ctx g_scope_list scope_list e of
| SOME (e', frame_list) =>
SOME (ascope, g_scope_list, frame_list++[(funn, [stmt_trans e'], scope_list)], status_running)
| NONE => NONE))
/\
(***************)
(* Conditional *)
(stmt_exec ctx (ascope, g_scope_list, [(funn, [stmt_cond e stmt1 stmt2], scope_list)], status_running) =
(* TODO: Make this more efficient by using a single get_v_bool e *)
if is_v_bool e
then
(case stmt_exec_cond e of
| SOME T => SOME (ascope, g_scope_list, [(funn, [stmt1], scope_list)], status_running)
| SOME F => SOME (ascope, g_scope_list, [(funn, [stmt2], scope_list)], status_running)
| NONE => NONE)
else
(case e_exec ctx g_scope_list scope_list e of
| SOME (e', frame_list) =>
SOME (ascope, g_scope_list, frame_list++[(funn, [stmt_cond e' stmt1 stmt2], scope_list)], status_running)
| NONE => NONE))
/\
(*********************)
(* Table application *)
(stmt_exec (apply_table_f, ext_map, func_map, b_func_map, pars_map, tbl_map) (ascope, g_scope_list, [(funn, [stmt_app t_name e_l], scope_list)], status_running) =
(case index_not_const e_l of
| SOME i =>
(case e_exec (apply_table_f, ext_map, func_map, b_func_map, pars_map, tbl_map) g_scope_list scope_list (EL i e_l) of
| SOME (e', frame_list) =>
SOME (ascope, g_scope_list, frame_list++[(funn, [stmt_app t_name (LUPDATE e' i e_l)], scope_list)], status_running)
| NONE => NONE)
| NONE =>
(case ALOOKUP tbl_map t_name of
| SOME (mk_l, (default_f, default_f_args)) =>
(if LENGTH mk_l = LENGTH e_l
then
(case apply_table_f (t_name, e_l, mk_l, (default_f, default_f_args), ascope) of
| SOME (f, f_args) =>
(if is_consts_exec f_args
then
SOME (ascope, g_scope_list, [(funn, [stmt_ass lval_null (e_call (funn_name f) f_args)], scope_list)], status_running)
else NONE)
| NONE => NONE)
else NONE)
| NONE => NONE)))
/\
(**********)
(* Return *)
(stmt_exec ctx (ascope, g_scope_list, [(funn, [stmt_ret e], scope_list)], status_running) =
(case get_v e of
| SOME v => SOME (ascope, g_scope_list, [(funn, [stmt_empty], scope_list)], status_returnv v)
| NONE =>
(case e_exec ctx g_scope_list scope_list e of
| SOME (e', frame_list) =>
SOME (ascope, g_scope_list, frame_list++[(funn, [stmt_ret e'], scope_list)], status_running)
| NONE => NONE)))
/\
(**********)
(* Extern *)
(stmt_exec (apply_table_f, ext_map, func_map, b_func_map, pars_map, tbl_map) (ascope, g_scope_list, [(funn, [stmt_ext], scope_list)], status_running) =
(case lookup_ext_fun funn ext_map of
| SOME ext_fun =>
(case ext_fun (ascope, g_scope_list, scope_list) of
| SOME (ascope', scope_list', status') =>
SOME (ascope', g_scope_list, [(funn, [stmt_empty], scope_list')], status')
| NONE => NONE)
| NONE => NONE))
/\
(*********)
(* Block *)
(stmt_exec ctx (ascope, g_scope_list, [(funn, [stmt_block decl_list stmt], scope_list)], status_running) =
SOME (ascope, g_scope_list, [(funn, [stmt]++[stmt_empty], ((declare_list_in_fresh_scope decl_list)::scope_list))], status_running))
/\
(************)
(* Sequence *)
(stmt_exec ctx (ascope, g_scope_list, [(funn, [stmt_seq stmt1 stmt2], scope_list)], status_running) =
if is_empty stmt1
then SOME (ascope, g_scope_list, [(funn, [stmt2], scope_list)], status_running)
else
(* Note: this only allows for 0 or 1 frame being added, or (exclusively) 1 stmt element *)
(case stmt_exec ctx (ascope, g_scope_list, [(funn, [stmt1], scope_list)], status_running) of
| SOME (ascope', g_scope_list', [(funn, [stmt1'], scope_list')], status') =>
(case status' of
| status_running =>
SOME (ascope', g_scope_list', [(funn, [stmt_seq stmt1' stmt2], scope_list')], status_running)
| _ =>
SOME (ascope', g_scope_list', [(funn, [stmt1'], scope_list')], status'))
| SOME (ascope', g_scope_list', [(funn, stmt1''::[stmt1'], scope_list')], status_running) =>
SOME (ascope', g_scope_list', [(funn, [stmt1'']++[stmt_seq stmt1' stmt2], scope_list')], status_running)
| SOME (ascope', g_scope_list', (frame::[(funn, [stmt1'], scope_list')]), status_running) =>
SOME (ascope', g_scope_list', (frame::[(funn, [stmt_seq stmt1' stmt2], scope_list')]), status_running)
| _ => NONE))
/\
(*********************)
(* Stmt stack clause *)
(*********************)
(* TODO: Should the case statement be handled by better matching in the clause? *)
(stmt_exec ctx (ascope, g_scope_list, [(funn, stmt_empty::stmt_stack, scope_list)], status) =
case stmt_stack of
| [] => NONE
| _ =>
(case scope_list of
| [] => NONE
| (h_scope::scope_list') => SOME (ascope, g_scope_list, [(funn, stmt_stack, TL (h_scope::scope_list'))], status)))
/\
(* TODO: This clause gets expanded into multiple clauses to account for all different
* statements. An alternative solution, which may be even more convoluted,
* could be to make mutually recursive functions *)
(stmt_exec ctx (ascope, g_scope_list, [(funn, stmt::stmt_stack, scope_list)], status) =
(case stmt_exec ctx (ascope, g_scope_list, [(funn, [stmt], scope_list)], status) of
| SOME (ascope', g_scope_list', frame_list', status') =>
(case frame_list' of
(* Regular case *)
| [(funn, [stmt'], scope_list')] =>
SOME (ascope', g_scope_list', [(funn, stmt'::stmt_stack, scope_list')], status')
(* Block entry *)
| [(funn, stmt''::[stmt'], scope_list')] =>
SOME (ascope', g_scope_list', [(funn, stmt''::(stmt'::stmt_stack), scope_list')], status')
(* Function call *)
| ((funn', [stmt'], scope_list')::[(funn, [stmt''], scope_list)]) =>
SOME (ascope', g_scope_list', ((funn', [stmt'], scope_list')::[(funn, stmt''::stmt_stack, scope_list)]), status')
(* TODO: What can happen here? *)
| _ => NONE)
| NONE => NONE))
/\
(stmt_exec _ _ = NONE)
End
Theorem exec_stmt_SOME_init_running:
!ctx ascope ascope' g_scope_list g_scope_list' funn stmt frame_list' scope_list status status'.
stmt_exec ctx (ascope, g_scope_list, [(funn, [stmt], scope_list)], status) =
SOME (ascope', g_scope_list', frame_list', status') ==>
status = status_running
Proof
rpt strip_tac >>
Cases_on `stmt` >> (
fs []
) >> (
Cases_on `status` >> (
fs [stmt_exec_def]
)
)
QED
Theorem e_exec_new_frame:
!ctx g_scope_list scope_list e e' frame_list'.
e_exec ctx g_scope_list scope_list e = SOME (e',frame_list') ==>
(frame_list' = [] \/
?funn stmt scope. frame_list' = [(funn, [stmt], [scope])])
Proof
`!ctx g_scope_list scope_list e.
(\ctx' g_scope_list' scope_list' e'.
!e'' frame_list''.
e_exec ctx' g_scope_list' scope_list' e' = SOME (e'', frame_list'') ==>
(frame_list'' = [] \/
?funn'' stmt'' scope''. frame_list'' = [(funn'', [stmt''], [scope''])])
) ctx g_scope_list scope_list e` suffices_by (
metis_tac []
) >>
irule e_exec_ind >>
fs [e_exec_def] >>
rpt strip_tac >| [
Cases_on `lookup_funn_sig_body funn func_map b_func_map ext_map` >> (
fs []
) >>
PairCases_on `x` >>
fs [] >>
Cases_on `unred_arg_index (MAP SND x1) e_l` >> (
fs []
) >| [
Cases_on `copyin (MAP FST x1) (MAP SND x1) e_l g_scope_list scope_list` >> (
fs []
) >>
metis_tac [],
Cases_on `e_exec (apply_table_f,ext_map,func_map,b_func_map,pars_map,tbl_map) g_scope_list
scope_list (EL x e_l)` >> (
fs []
) >>
PairCases_on `x'` >>
fs []
],
(* Unop *)
Cases_on `is_v e` >> (
fs []
) >| [
Cases_on `e_exec_cast cast e` >> (
fs []
),
Cases_on `e_exec ctx g_scope_list scope_list e` >> (
fs []
) >>
PairCases_on `x` >>
fs []
],
(* TODO: Weird blob goal... *)
Cases_on `e1` >> (
fs [e_exec_def]
) >| [
Cases_on `is_short_circuitable binop` >> (
fs []
) >| [
Cases_on `e_exec_short_circuit v binop e2` >> (
fs []
),
Cases_on `is_v e2` >> (
fs []
) >| [
Cases_on `e_exec_binop (e_v v) binop e2` >> (
fs []
),
Cases_on `e_exec ctx g_scope_list scope_list e2` >> (
fs []
) >>
PairCases_on `x` >>
fs []
]
],
Cases_on `lookup_vexp2 scope_list g_scope_list v` >> (
fs []
),
Cases_on `is_v e` >> (
fs []
) >- (
Cases_on `e_exec_acc (e_acc e s)` >> (
fs []
)
) >>
Cases_on `e_exec ctx g_scope_list scope_list e` >> (
fs []
) >>
PairCases_on `x` >>
fs [],
Cases_on `is_v e` >> (
fs []
) >| [
Cases_on `e_exec_unop u e` >> (
fs []
),
Cases_on `e_exec ctx g_scope_list scope_list e` >> (
fs []
) >>
PairCases_on `x` >>
fs []
],
(* Cast *)
Cases_on `is_v e` >> (
fs []
) >| [
Cases_on `e_exec_cast c e` >> (
fs []
),
Cases_on `e_exec ctx g_scope_list scope_list e` >> (
fs []
) >>
PairCases_on `x` >>
fs []
],
(* TODO: Interesting... *)
Cases_on `case e of
e_v v =>
if is_short_circuitable b then
case e_exec_short_circuit v b e0 of
NONE => NONE
| SOME e' => SOME (e',[])
else if is_v e0 then
case e_exec_binop e b e0 of
NONE => NONE
| SOME v' => SOME (e_v v',[])
else
(case e_exec ctx g_scope_list scope_list e0 of
NONE => NONE
| SOME (e2',frame_list) =>
SOME (e_binop e b e2',frame_list))
| e_var v25 =>
(case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list))
| e_list v26 =>
(case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list))
| e_acc v27 v28 =>
(case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list))
| e_unop v29 v30 =>
(case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list))
| e_cast v29 v30 =>
(case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list))
| e_binop v31 v32 v33 =>
(case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list))
| e_concat v34 v35 =>
(case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list))
| e_slice v36 v37 v38 =>
(case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list))
| e_call v39 v40 =>
(case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list))
| e_select v41 v42 v43 =>
(case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list))
| e_struct v44 =>
(case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list))
| e_header v45 v46 =>
case e_exec ctx g_scope_list scope_list e of
NONE => NONE
| SOME (e1',frame_list) => SOME (e_binop e1' b e0,frame_list)` >> (
fs []
) >>
PairCases_on `x` >>
fs [],
(* Concatenation *)
Cases_on `is_v_bit e` >> Cases_on `is_v_bit e0` >> (
fs []
) >| [
Cases_on `e_exec_concat e e0` >> (
fs []
),
Cases_on `e_exec ctx g_scope_list scope_list e0` >> (
fs []
) >>
PairCases_on `x` >>
fs [],
Cases_on `e_exec ctx g_scope_list scope_list e` >> (
fs []
) >>
PairCases_on `x` >>
fs [],
Cases_on `e_exec ctx g_scope_list scope_list e` >> (
fs []
) >>
PairCases_on `x` >>
fs []
],
(* Slicing *)
Cases_on `is_v_bit e0` >> Cases_on `is_v_bit e1'` >> (
fs []
) >>
Cases_on `is_v_bit e` >> (
fs []
) >| [
Cases_on `e_exec_slice e e0 e1'` >> (
fs []
),
Cases_on `e_exec ctx g_scope_list scope_list e` >> (
fs []
) >>
PairCases_on `x` >>
fs []
],
(* Function call *)
Cases_on `e_exec ctx g_scope_list scope_list (e_call f l)` >> (
fs []
) >>
PairCases_on `x` >>
fs [],
(* Select *)
Cases_on `is_v e` >> (
fs []
) >| [
Cases_on `e_exec_select e l s` >> (
fs []
),
Cases_on `e_exec ctx g_scope_list scope_list e` >> (
fs []
) >>
PairCases_on `x` >>
fs []
],
(* Struct *)
Cases_on `unred_mem_index (MAP SND l)` >> (
fs []
) >>
Cases_on `e_exec ctx g_scope_list scope_list (EL x (MAP SND l))` >> (
fs []
) >>
PairCases_on `x'` >>
fs []
],
(* Slicing *)
Cases_on `is_v_bit e1` >> (
fs []
) >| [
Cases_on `e_exec_slice e1 e2 e3` >> (
fs []
),
Cases_on `e_exec ctx g_scope_list scope_list e1` >> (
fs []
) >>
PairCases_on `x` >>
fs []
],
(* Concatenation *)
Cases_on `is_v_bit e1` >> Cases_on `is_v_bit e2` >> (
fs []
) >| [
Cases_on `e_exec_concat e1 e2` >> (
fs []
),
Cases_on `e_exec ctx g_scope_list scope_list e2` >> (
fs []
) >>
PairCases_on `x` >>
fs [],
Cases_on `e_exec ctx g_scope_list scope_list e1` >> (
fs []
) >>
PairCases_on `x` >>
fs [],
Cases_on `e_exec ctx g_scope_list scope_list e1` >> (
fs []
) >>
PairCases_on `x` >>