-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4_e_subject_reductionScript.sml
6720 lines (4982 loc) · 165 KB
/
p4_e_subject_reductionScript.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 liteLib simpLib Parse bossLib;
open arithmeticTheory stringTheory containerTheory pred_setTheory
listTheory finite_mapTheory;
open p4Lib;
open blastLib bitstringLib;
open p4Theory;
open p4_auxTheory;
open p4_deterTheory;
open bitstringTheory;
open wordsTheory;
open optionTheory;
open sumTheory;
open stringTheory;
open ottTheory;
open pairTheory;
open rich_listTheory;
open arithmeticTheory;
open alistTheory;
open numeralTheory;
(*tactics*)
fun OPEN_EXP_RED_TAC exp_term =
(Q.PAT_X_ASSUM `e_red c scope scopest ^exp_term exp2 fr` (fn thm => ASSUME_TAC (SIMP_RULE (srw_ss()) [Once e_sem_cases] thm)))
fun OPEN_STMT_RED_TAC stm_term =
(Q.PAT_X_ASSUM `stmt_red ct (ab, gsl,[(fun,[^stm_term],gam)],st) stat` (fn thm => ASSUME_TAC (SIMP_RULE (srw_ss()) [Once stmt_sem_cases] thm)))
fun OPEN_V_TYP_TAC v_term =
(Q.PAT_X_ASSUM `v_typ v_term t bll` (fn thm => ASSUME_TAC (SIMP_RULE (srw_ss()) [Once v_typ_cases] thm)))
fun OPEN_EXP_TYP_TAC exp_term =
(Q.PAT_X_ASSUM ` e_typ (t1,t2) t exp_term ta bll` (fn thm => ASSUME_TAC (SIMP_RULE (srw_ss()) [Once e_typ_cases] thm)))
fun OPEN_STMT_TYP_TAC stmt_term =
(Q.PAT_X_ASSUM ` stmt_typ (t1,t2) t f stmt_term` (fn thm => ASSUME_TAC (SIMP_RULE (srw_ss()) [Once stmt_typ_cases] thm)))
fun INST_FST trm_list = (FIRST_X_ASSUM ((qspecl_then trm_list assume_tac))) >> fs[] ;
fun INST_LAST trm_list = (LAST_X_ASSUM ((qspecl_then trm_list assume_tac))) >> fs[] ;
val EXP_GOAL_TYP_IH_TAC = SIMP_TAC (srw_ss()) [Once e_typ_cases] >>
fs[] >>
METIS_TAC [] >>
fs[];
val _ = new_theory "p4_e_subject_reduction";
val t_passed_elem_def = Define ‘
t_passed_elem funn delta_g delta_b delta_t t_scope_list_g =
((t_map_to_pass funn delta_b),
(t_tbl_to_pass funn delta_b delta_t),
(t_scopes_to_pass funn delta_g delta_b t_scope_list_g))
’;
(****** Subject Reduction for expression ******)
(*t_scopes_consistent is with respect to the expression's global, not the passed, because at that point we are comparing with respect to the passed scope that is already exsists in the expression *)
val sr_exp_def = Define `
sr_exp (e) (ty:'a itself) =
! e' gscope (scopest:scope list) framel t_scope_list t_scope_list_g T_e tau b
(c:'a ctx) order delta_g delta_b delta_t delta_x f f_called stmt_called copied_in_scope Prs_n
apply_table_f ext_map func_map b_func_map pars_map tbl_map .
(type_scopes_list (gscope) (t_scope_list_g) ) /\
(type_scopes_list (scopest) (t_scope_list)) /\
(star_not_in_sl (scopest) ) /\
(* (parseError_in_gs t_scope_list_g [t_scope_list]) ∧ *)
(c = ( apply_table_f , ext_map , func_map , b_func_map , pars_map , tbl_map ) ) ∧
(WT_c c order t_scope_list_g delta_g delta_b delta_x delta_t Prs_n) /\
(e_red c gscope scopest e e' framel ) /\
(e_typ ( t_scope_list_g , t_scope_list ) T_e (e) tau b) /\
(T_e = (order, f, (delta_g, delta_b, delta_x, delta_t))) ==>
((?b'. (e_typ ( t_scope_list_g , t_scope_list ) T_e (e') tau b')) /\
( (framel = [f_called, [stmt_called] , copied_in_scope] ) ==>
? passed_tslg passed_delta_t passed_delta_b passed_gscope t_scope_list_fr .
order (order_elem_f f_called) (order_elem_f f) ∧
t_passed_elem f_called delta_g delta_b delta_t t_scope_list_g = (SOME passed_delta_b, SOME passed_delta_t , SOME passed_tslg) ∧
scopes_to_pass f_called func_map b_func_map gscope = SOME passed_gscope ∧
t_scopes_consistent T_e (t_scope_list) (t_scope_list_g) (t_scope_list_fr) ∧
frame_typ (passed_tslg,t_scope_list_fr) (order, f_called , (delta_g,passed_delta_b, delta_x, passed_delta_t)) Prs_n passed_gscope copied_in_scope [stmt_called] )
)
`;
(****** Subject reduction for expression list ******)
val sr_exp_list_def = Define `
sr_exp_list (l : e list) (ty:'a itself) =
!(e : e). MEM e l ==> sr_exp (e) (ty:'a itself)
`;
(****** Subject reduction for strexp list ******)
val sr_strexp_list_def = Define `
sr_strexp_list (l : (string#e) list) (ty:'a itself)
= ! (e:e) . MEM e (SND (UNZIP l)) ==> sr_exp(e) (ty:'a itself)
`;
(****** Subject reduction for str e tup list ******)
val sr_strexp_tup_def = Define `
sr_strexp_tup (tup : (string#e)) (ty:'a itself)
= sr_exp ((SND tup)) (ty:'a itself)
`;
(******
define an inverse function of vl_of_el
*)
val el_of_vl_def = Define `
el_of_vl vl = MAP (\(v). (e_v v)) (vl)
`;
(*
a value list where each member is a constant imples that
the conversion back to an expression list hold
*)
val vl_el_conv = prove( ``
! l l'. (l = vl_of_el l') /\ (is_consts l') ==>
(l' = el_of_vl l) ``,
Induct_on `l` >>
Induct_on `l'` >>
REPEAT STRIP_TAC >>
fs[el_of_vl_def, vl_of_el_def] >>
rw[] >>
Cases_on `h` >>
fs[v_of_e_def, is_const_def, is_consts_def]
);
(** value typing theorems and lemmas **)
(*
if an expression value (e_v v) is well typed,
then that value v is also well typed with the same tau
*)
val ev_types_v = prove (``
! v tau t_scope_list_g t_scope_list T_e .
e_typ (t_scope_list_g,t_scope_list) T_e (e_v v) (tau) F ==>
v_typ (v) (tau) F ``,
REPEAT STRIP_TAC >>
OPEN_EXP_TYP_TAC ``e_v v`` >>
fs[] ) ;
(*
if the expression a constant, then the value of that expression
should be able to be typed with the same tau
*)
val e_types_v = prove (``
! v e tau t_scope_list_g t_scope_list T_e .
is_const(e) /\
e_typ (t_scope_list_g,t_scope_list) T_e (e) (tau) F ==>
v_typ ( THE (v_of_e e)) (tau) F ``,
REPEAT STRIP_TAC >>
OPEN_EXP_TYP_TAC ``e`` >>
fs[] >>
fs[v_of_e_def, is_const_def]
) ;
(*
for a list of expressions l',
if all the list members are constants, then accessing any member is a constant
*)
Theorem EL_consts_is_const:
!l i. i < LENGTH l /\ is_consts (l) ==>
is_const (EL i (l))
Proof
REPEAT STRIP_TAC >>
fs[is_consts_def] >>
fs[is_const_def] >>
fs[EVERY_EL]
QED
(*
if the expression list contains only constant values,
then those values of these expressions
should be able to be typed with the same tau at the same index of that expression
*)
val evl_types_vl_F = prove(``
!l l' i t_scope_list_g t_scope_list T_e.
(LENGTH l = LENGTH l') /\
(i<LENGTH l) /\
is_consts (l) /\
(e_typ (t_scope_list_g,t_scope_list) T_e
(EL i l)
(t_tau (EL i l')) F) ==>
v_typ (EL i (vl_of_el l)) (t_tau (EL i l')) F ``,
Induct_on `l` >>
Induct_on `l'` >>
fs[] >>
REPEAT STRIP_TAC >>
(* first we know that each member of the e list is a constant, via: *)
ASSUME_TAC EL_consts_is_const >>
LAST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL [`(h'::l): (e list)`, `i`])) >>
fs[] >>
RES_TAC >>
(* simplify the assumptions further to show that
EL i (h'::l) = e_v v *)
Cases_on `EL i (h'::l)` >>
fs[is_consts_def, is_const_def, EVERY_EL] >>
rw[] >>
(* now we know that the only expression can by typed with such conditions
is a constant, i.e. (e_v v), thus we can type it with v_typ *)
IMP_RES_TAC e_types_v >>
gvs[] >>
(* now we should prove the property for both the head and the tail *)
fs[Once EL_compute] >>
CASE_TAC >| [
(* i=0 *)
rw[] >>
fs[vl_of_el_def]
,
(* i ≠ 0 *)
FIRST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL
[`l'`,`(i:num)-1`])) >>
fs[] >>
fs[numeral_pre, PRE_SUB1, PRE_SUC_EQ ,ADD1] >>
rw[] >>
Cases_on `i = 1` >>
fs[] >>
gvs[v_of_e_def] >>
RES_TAC >>
gvs[vl_of_el_def] >>
subgoal ` EL (i − 1) (HD l::TL l) = EL (PRE (i − 1)) (TL l) ` >- (
`0 < i - 1` by fs[] >>
ASSUME_TAC EL_CONS >>
Q.PAT_X_ASSUM `∀n. 0 < n ⇒ ∀x l. EL n (x::l) = EL (PRE n) l`
(STRIP_ASSUME_TAC o (Q.SPECL [`i-1`])) >>
RES_TAC >>
fs[EL_CONS] ) >>
subgoal `(HD l::TL l) = l ` >- (
`0 < i` by fs[] >>
`0 < LENGTH l` by fs[] >>
`~(0 >= LENGTH l)` by fs[] >>
`0 ≥ LENGTH l ⇔ l = []` by fs[quantHeuristicsTheory.LIST_LENGTH_0] >>
`~(l = [])` by fs[] >>
fs[NULL] >>
ASSUME_TAC NULL_LENGTH >>
ASSUME_TAC CONS >>
RES_TAC >>
FULL_SIMP_TAC list_ss [CONS, NULL_LENGTH, NULL_DEF, NULL_EQ] ) >>
FIRST_X_ASSUM
(STRIP_ASSUME_TAC o (Q.SPECL [`t_scope_list_g`, `t_scope_list`, `T_e`])) >>
gvs[] >>
fs[EL_CONS, PRE_SUB1]
]
);
(*
The constant can never be typed as an lvalue
*)
val value_is_lval = prove ( ``
∀v tau t_scope_list_g t_scope_list T_e.
~ e_typ (t_scope_list_g,t_scope_list) T_e (e_v v) tau T ``,
fs[Once e_typ_cases] >>
fs[clause_name_def] >>
fs[Once v_typ_cases]
);
(*
given l (expression value list), l' (type list), l'' (lval indication list), where ith element
of l can be typed with ith element of l' and l'', then the can type
the values of the list l, the same exact way using v_typ.
*)
Theorem evl_types_vl_blist:
∀l l' l'' i t_scope_list_g t_scope_list T_e.
LENGTH l = LENGTH l' /\ LENGTH l = LENGTH l'' ∧ i < LENGTH l ∧ is_consts l ∧
e_typ (t_scope_list_g,t_scope_list) T_e (EL i l) (t_tau (EL i l')) (EL i l'') ⇒
v_typ (EL i (vl_of_el l)) (t_tau (EL i l')) F
Proof
Induct_on `l` >>
Induct_on `l'` >>
Induct_on `l''` >>
fs[] >>
REPEAT STRIP_TAC >>
(* we already know that this should hold whever v is an not an lval from evl_types_vl_F
so we need to make cases on the bool representation of the lval, and show that if
the value is lval, this is incorrect, else use theorem evl_types_vl_F *)
Cases_on `EL i (h::l'')` >>
fs[] >| [
(*show that this is impossible*)
IMP_RES_TAC ev_types_v >>
ASSUME_TAC EL_consts_is_const >>
LAST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL [`(h''::l): (e list)`, `i`])) >>
fs[] >>
RES_TAC >>
Cases_on `EL i (h''::l)` >>
fs[is_consts_def] >>
fs[is_const_def] >>
fs[EVERY_EL] >>
rw[] >>
Cases_on `v` >>
OPEN_EXP_TYP_TAC ``(e_v v)`` >>
gvs[] >>
OPEN_V_TYP_TAC ``(v)`` >> fs[]
,
IMP_RES_TAC evl_types_vl_F >>
gvs[]
]
QED
(** similar & similarl theorems **)
(* variable name x cannot be looked up in the scope sc if and only if it cannot be looked up
in the typing scope tc *)
Theorem R_ALOOKUP_NONE_scopes:
! R v x t sc tc.
similar R tc sc ==>
((NONE = ALOOKUP sc x) <=>
(NONE = ALOOKUP tc x ) )
Proof
Induct_on `sc` >>
Induct_on `tc` >>
RW_TAC list_ss [similar_def] >>
rw[ALOOKUP_MEM] >>
REPEAT STRIP_TAC >>
PairCases_on `h` >>
PairCases_on `h'` >>
fs[similar_def] >>
rw[] >>
LAST_X_ASSUM
( STRIP_ASSUME_TAC o (Q.SPECL [`R`,`x`,`tc`])) >>
fs[similar_def,LIST_REL_def]
QED
(*
TODO: fix the comments everywhere, here the v and t are tuples where teh second element is an lval op
if the variable name is in the scope sc and has the value v , as well as the typing scope tc with the type t , and also we know that tc |- sc, then v : (tau, F)
*)
Theorem R_ALOOKUP_scopes:
! R v x t sc tc.
( similar R tc sc /\
(SOME v = ALOOKUP sc x) /\
(SOME t = ALOOKUP tc x ) ) ==>
(R t v)
Proof
Induct_on `sc` >>
Induct_on `tc` >>
RW_TAC list_ss [similar_def] >>
rw[ALOOKUP_MEM] >>
FULL_SIMP_TAC list_ss [ALOOKUP_def, ALOOKUP_MEM] >>
REPEAT STRIP_TAC >>
PairCases_on `h` >>
PairCases_on `h'` >>
fs[similar_def] >>
rw[] >>
(*lastone*)
Cases_on `h'0 = x` >>
fs[] >>
rw[] >>
LAST_X_ASSUM
( STRIP_ASSUME_TAC o (Q.SPECL [`R`,`v`,`x`,`t`, `tc`])) >>
fs[similar_def,LIST_REL_def]
QED
Theorem R_find_topmost_map_scopesl:
! R x l1 l2 scl tcl.
( similarl R tcl scl /\
(SOME l1 = find_topmost_map tcl x) /\
(SOME l2 = find_topmost_map scl x ) ) ==>
((similar R (SND l1) (SND l2)) /\ (FST l1 = FST l2) )
Proof
simp [find_topmost_map_def] >>
NTAC 7 STRIP_TAC >>
Cases_on `INDEX_FIND 0 (λsc. IS_SOME (ALOOKUP sc x)) scl` >>
Cases_on `INDEX_FIND 0 (λsc. IS_SOME (ALOOKUP sc x)) tcl` >>
fs[] >>
rw[] >>
PairCases_on `l1` >>
PairCases_on `l2` >>
(* first and last subgoal are going to be the same, so use FIRST tactical *)
Cases_on`l10 = l20` >> FIRST [
(*if the index of both lists are equal show that similarity holds *)
gvs[] >>
fs[similarl_def] >>
IMP_RES_TAC P_holds_on_curent >>
fs[] >>
(* we know that the property of INDEX_FIND does now return NONE*)
Cases_on `ALOOKUP l21 x` >>
Cases_on `ALOOKUP l11 x` >>
fs[] >>
rw[] >>
(*since the ith element is the same then the relartion R should
hold in the same index for both*)
fsrw_tac [][LIST_REL_EL_EQN,MEM_EL] >>
IMP_RES_TAC LIST_REL_MEM_IMP >>
IMP_RES_TAC prop_in_range >>
RES_TAC >>
(*simplify the scope by using the ith element notaion*)
subgoal `EL l10 tcl = l11` >-
IMP_RES_TAC INDEX_FIND_EQ_SOME_0 >>
subgoal `EL l10 scl = l21` >-
IMP_RES_TAC INDEX_FIND_EQ_SOME_0 >>
IMP_RES_TAC R_ALOOKUP_scopes >>
rw[similar_def]
,
(*case of index not compatable*)
(*prove by contradiction*)
CCONTR_TAC >>
gvs[] >>
(*the property holds on both l11 and l21*)
fs[similarl_def] >>
IMP_RES_TAC P_holds_on_curent >>
fs[] >>
(*simplify all the lookup cases *)
Cases_on `ALOOKUP l21 x` >>
Cases_on `ALOOKUP l11 x` >>
fs[] >>
rw[] >>
(*show that the relation holds on both index l20 and l110 for both scl and tcl*)
fsrw_tac [][LIST_REL_EL_EQN,MEM_EL] >>
IMP_RES_TAC LIST_REL_MEM_IMP >>
IMP_RES_TAC prop_in_range >>
subgoal `similar R (EL l20 tcl) (EL l20 scl) /\ similar R (EL l10 tcl) (EL l10 scl)` >-
(fs[] >>
RES_TAC) >>
(*use this lemma to indicate that if a relation holds on NONE *)
IMP_RES_TAC R_ALOOKUP_NONE_scopes >>
subgoal `∀j'. j' < l10 ⇒ ¬(λsc. IS_SOME (ALOOKUP sc x)) (EL j' tcl)` >-
IMP_RES_TAC INDEX_FIND_EQ_SOME_0 >>
subgoal `∀j'. j' < l20 ⇒ ¬(λsc. IS_SOME (ALOOKUP sc x)) (EL j' scl)` >-
IMP_RES_TAC INDEX_FIND_EQ_SOME_0 >>
subgoal `EL l10 tcl = l11` >-
IMP_RES_TAC INDEX_FIND_EQ_SOME_0 >>
subgoal `EL l20 scl = l21` >-
IMP_RES_TAC INDEX_FIND_EQ_SOME_0 >>
`l20 < l10 \/ l10 < l20` by fs[] >>
fs[similar_def,LIST_REL_def] >>
fsrw_tac [][LIST_REL_EL_EQN,MEM_EL] >>
IMP_RES_TAC LIST_REL_MEM_IMP >>
IMP_RES_TAC prop_in_range >>
RES_TAC >>
IMP_RES_TAC P_holds_on_curent >>
RES_TAC >>
fs[similar_def] >>
rw[]
,
fs[]
]
QED
Theorem R_topmost_map_scopesl:
! R x l1 l2 scl tcl.
( similarl R tcl scl /\
(SOME l1 = topmost_map tcl x) /\
(SOME l2 = topmost_map scl x ) ) ==>
(similar R l1 l2)
Proof
simp [topmost_map_def] >>
REPEAT STRIP_TAC >>
Cases_on `find_topmost_map scl x` >>
Cases_on `find_topmost_map tcl x` >>
fs[] >>
rw[] >>
PairCases_on `x'` >>
PairCases_on `x''` >>
gvs[] >>
ASSUME_TAC R_find_topmost_map_scopesl >>
LAST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL [`R`, `x`, `(x''0,l1)`, `(x'0,l2)`, `scl`, `tcl`])) >>
fs[]
QED
Theorem R_lookup_map_scopesl:
! R v x t scl tcl.
( similarl R tcl scl /\
(SOME v = lookup_map scl x) /\
(SOME t = lookup_map tcl x ) ) ==>
(R t v)
Proof
fs[lookup_map_def] >>
REPEAT STRIP_TAC >>
Cases_on `topmost_map tcl x` >>
Cases_on `topmost_map scl x` >>
fs[] >>
rw[] >>
ASSUME_TAC R_topmost_map_scopesl >>
LAST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL [`R`, `x`, `x'`, `x''`, `scl`, `tcl`])) >>
gvs[] >>
Cases_on `ALOOKUP x'' x` >>
Cases_on `ALOOKUP x' x` >>
fs[] >>
rw[] >>
fs[] >>
IMP_RES_TAC R_ALOOKUP_scopes >>
LAST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL [`x`, `v`])) >>
fs[]
QED
Theorem type_scopes_list_LENGTH:
! l1 l2 . type_scopes_list l1 l2 ==>
(LENGTH l1 = LENGTH l2)
Proof
fs[type_scopes_list_def, similarl_def, similar_def] >>
REPEAT STRIP_TAC >>
IMP_RES_TAC LIST_REL_LENGTH
QED
Theorem type_scopes_list_APPEND:
! l1 l2 l3 l4. type_scopes_list l1 l2 /\
type_scopes_list l3 l4 ==>
type_scopes_list (l1++l3) (l2++l4)
Proof
fs[type_scopes_list_def, similarl_def, similar_def] >>
REPEAT STRIP_TAC >>
IMP_RES_TAC LIST_REL_APPEND
QED
val varn_is_typed = prove (``
! gsl gtsl sl tsl varn v tau .
type_scopes_list gsl gtsl ∧
type_scopes_list sl tsl ∧
SOME v = lookup_vexp2 sl gsl varn ∧
SOME tau = lookup_tau tsl gtsl varn ==>
v_typ v (t_tau tau) F
``,
REPEAT STRIP_TAC >>
IMP_RES_TAC type_scopes_list_LENGTH >>
fs[lookup_vexp2_def] >>
fs[lookup_tau_def] >>
Cases_on `lookup_map (sl ⧺ gsl) varn` >>
Cases_on `lookup_map (tsl ⧺ gtsl) varn` >>
fs[] >> rw[] >>
subgoal `type_scopes_list (sl ⧺ gsl) (tsl ⧺ gtsl)` >-
IMP_RES_TAC type_scopes_list_APPEND >>
PairCases_on `x` >>
PairCases_on `x'` >> gvs[] >>
fs[type_scopes_list_def] >>
subgoal `∀x t.
SOME t = lookup_map (sl ⧺ gsl) x ⇒
∀v. SOME v = lookup_map (tsl ⧺ gtsl) x ⇒
(λ(v,lop1) (t,lop2). v_typ v (t_tau t) F ∧ lop1 = lop2) t v` >-
(IMP_RES_TAC R_lookup_map_scopesl >>
fs[]) >>
Q.PAT_X_ASSUM `∀x v. _`
( STRIP_ASSUME_TAC o (Q.SPECL [`varn`,`(v,x1)`])) >>
gvs[]
);
(*
if var star is not in the domain of the scope s,
then it is not a member of that list
*)
val star_MEM = prove ( ``
!s f.
star_not_in_s (s) ==> ~MEM (varn_star f) (MAP FST s) ``,
REPEAT STRIP_TAC >>
fs[star_not_in_s_def] >>
FIRST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL
[`f`])) >>
fs[ALOOKUP_NONE]
);
(*
if star is not in any scope of scope lists l, then we should not
find any index and value returned while searching in l's scopes
*)
val star_not_in_s_implies_none = prove ( ``
!l . EVERY (λs. star_not_in_s s) l ==>
!f . INDEX_FIND 0 (λsc. IS_SOME (ALOOKUP sc (varn_star f))) (l) = NONE
``,
Induct >>
fs[star_not_in_s_def, INDEX_FIND_def] >>
REPEAT STRIP_TAC >>
RES_TAC >>
fs[P_NONE_hold]
);
Theorem lookup_map_in_gsl_lemma:
! v lvalop f sl gsl.
SOME (v,lvalop) = lookup_map (sl ⧺ gsl) (varn_star f) /\
star_not_in_sl sl ==>
SOME (v,lvalop) = lookup_map gsl (varn_star f)
Proof
REPEAT STRIP_TAC >>
fs[star_not_in_sl_def] >>
fs[lookup_vexp2_def] >>
fs[lookup_map_def] >>
fs[topmost_map_def] >>
fs[find_topmost_map_def] >>
Cases_on `INDEX_FIND 0 (λsc. IS_SOME (ALOOKUP sc (varn_star f)))
(sl ⧺ gsl)` >>
rw[] >> fs[] >>
PairCases_on `x` >>
rw[] >> fs[] >>
Cases_on `ALOOKUP x1 (varn_star f)` >>
rw[] >> fs[] >>
PairCases_on `x` >>
rw[] >> fs[] >>
gvs[] >>
Cases_on `INDEX_FIND 0 (λsc. IS_SOME (ALOOKUP sc (varn_star f))) gsl` >>
rw[] >> fs[] >>
gvs[] >| [
IMP_RES_TAC index_find_concat1 >>
fs[EVERY_MEM] >>
IMP_RES_TAC index_mem >>
fs[EVERY_MEM] >>
RES_TAC >>
fs[] >>
IMP_RES_TAC ALOOKUP_MEM >>
IMP_RES_TAC mem_triple_map_fst >>
IMP_RES_TAC star_MEM
,
PairCases_on `x` >>
fs[] >>
Cases_on `ALOOKUP x1' (varn_star f)` >>
rw[] >> fs[] >| [
IMP_RES_TAC index_find_concat1 >>
IMP_RES_TAC P_holds_on_curent >>
gvs[]
,
PairCases_on `x` >>
fs[] >>
IMP_RES_TAC star_not_in_s_implies_none>>
LAST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL [`f`])) >>
fs[] >>
IMP_RES_TAC index_find_concat2 >>
fs[]
]
]
QED
Theorem lookup_in_gsl_lemma:
! v f sl gsl.
SOME v = lookup_vexp2 sl gsl (varn_star f) /\
star_not_in_sl sl ==>
SOME v = lookup_vexp2 [] gsl (varn_star f)
Proof
REPEAT STRIP_TAC >>
fs[lookup_vexp2_def] >>
Cases_on `lookup_map (sl ⧺ gsl) (varn_star f)` >>
Cases_on `lookup_map gsl (varn_star f)` >> fs[] >>
PairCases_on `x` >> fs[] >>
ASSUME_TAC lookup_map_in_gsl_lemma >>
FIRST_X_ASSUM
(STRIP_ASSUME_TAC o (Q.SPECL [`v`,`x1`,`f`,`sl`, `gsl`])) >> gvs[]
QED
Theorem lookup_map_none_lemma1:
! t_scope_list_g varn. LENGTH t_scope_list_g = 2 /\
lookup_map t_scope_list_g (varn) = NONE ==>
(ALOOKUP (EL 1 t_scope_list_g) (varn) = NONE /\
ALOOKUP (EL 0 t_scope_list_g) (varn) = NONE)
Proof
REPEAT STRIP_TAC >>
fs[lookup_map_def] >>
fs[topmost_map_def] >>
fs[find_topmost_map_def] >>
Cases_on `INDEX_FIND 0 (λsc. IS_SOME (ALOOKUP sc varn)) t_scope_list_g` >>
fs[] >> rw[] >> FIRST[
(* first and third subgoals *)
IMP_RES_TAC index_none_not_every >>
FULL_SIMP_TAC (std_ss) [combinTheory.o_DEF] >>
fs[EVERY_EL] >>
FIRST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL [`0`])) >> gvs[]
,
(* second and fourth subgoals *)
PairCases_on `x` >>
fs[] >>
Cases_on `ALOOKUP x1 varn` >>
fs[] >> rw[] >>
fs[INDEX_FIND_EQ_SOME_0]
]
QED
val lookup_glb_local_ctx = prove ( ``
! func_map delta_g func_map delta_b b_func_map s stmt_called
xdl txdl ext_map delta_x tau.
dom_tmap_ei delta_g delta_b /\
dom_g_eq delta_g func_map /\
dom_b_eq delta_b b_func_map /\
SOME (stmt_called,xdl) =
lookup_funn_sig_body (funn_name s) func_map b_func_map ext_map /\
SOME (txdl,tau) = t_lookup_funn (funn_name s) delta_g delta_b delta_x ==>
((ALOOKUP func_map s = SOME (stmt_called,xdl) /\
ALOOKUP delta_g s = SOME (txdl,tau) /\
(ALOOKUP b_func_map s = NONE /\
ALOOKUP delta_b s = NONE )) \/
(ALOOKUP func_map s = NONE /\
ALOOKUP delta_g s = NONE /\
ALOOKUP b_func_map s = SOME (stmt_called,xdl) /\
ALOOKUP delta_b s = SOME (txdl,tau))
) ``,
REPEAT STRIP_TAC >>
fs[lookup_funn_sig_def, lookup_funn_sig_body_def] >>
fs[t_lookup_funn_def] >>
rfs[] >> rw[] >>
Cases_on `ALOOKUP b_func_map s` >>
Cases_on `ALOOKUP delta_b s` >>
fs[] >| [
(*not found in block, so should be global function*)
Cases_on `ALOOKUP func_map s` >>
Cases_on `ALOOKUP delta_g s` >>
fs[] >> rw[] >>
PairCases_on `x` >>
PairCases_on `x'` >>
rfs[] >>
RES_TAC >>
gvs[]
,
(* found b in env but not in typing ctx*)
rfs[dom_b_eq_def, dom_eq_def] >>
rfs[is_lookup_defined_def] >>
FIRST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL
[`s`])) >>
gvs[]
,
rfs[dom_b_eq_def, dom_eq_def] >>
rfs[is_lookup_defined_def] >>
FIRST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL
[`s`])) >>
gvs[]
,
PairCases_on `x` >>
PairCases_on `x'` >>
rfs[] >>
RES_TAC >>
gvs[] >>
rfs[dom_g_eq_def, dom_eq_def] >>
rfs[is_lookup_defined_def] >>
FIRST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL
[`s`])) >>
gvs[] >>
Cases_on `ALOOKUP func_map s` >> gvs[] >>
Cases_on `ALOOKUP delta_g s` >> gvs[] >>
fs[dom_tmap_ei_def] >>
fs[dom_empty_intersection_def] >>
FIRST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL
[`s`])) >>
gvs[]
]
);
val t_lookup_funn_lemma_old = prove ( ``
! delta_g delta_b delta_x f txdl tau .
SOME (txdl,tau) = t_lookup_funn f delta_g [] [] /\
dom_tmap_ei delta_g delta_b ==>
(? tau' txdl' . ( SOME (txdl',tau') = t_lookup_funn f delta_g delta_b []) /\
( SOME (txdl',tau') = t_lookup_funn f delta_g delta_b delta_x) /\
(txdl = txdl' /\ tau = tau'))``,
REPEAT STRIP_TAC >>
Cases_on `f` >>
fs[t_lookup_funn_def] >>
REPEAT (BasicProvers.FULL_CASE_TAC >> gvs[]) >>
fs[dom_tmap_ei_def] >>
rfs[dom_empty_intersection_def] >>
FIRST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL [`s`])) >> gvs[]
);
val t_lookup_funn_lemma = prove ( ``
! delta_g delta_b delta_x f txdl tau .
SOME (txdl,tau) = t_lookup_funn f delta_g [] delta_x /\
dom_tmap_ei delta_g delta_b ==>
(? tau' txdl' . ( SOME (txdl',tau') = t_lookup_funn f delta_g delta_b delta_x) /\
(txdl = txdl' /\ tau = tau'))``,
REPEAT STRIP_TAC >>
fs[t_lookup_funn_def] >>
REPEAT (BasicProvers.FULL_CASE_TAC >> gvs[]) >>
fs[dom_tmap_ei_def] >>
rfs[dom_empty_intersection_def] >>
FIRST_X_ASSUM (STRIP_ASSUME_TAC o (Q.SPECL [`s`])) >> gvs[]
);
val t_lookup_funn_blk_lemma = prove ( ``
! delta_g delta_b delta_x f txdl tau .
SOME (txdl,tau) = t_lookup_funn (f) [] delta_b [] ==>
(? tau' txdl' . ( SOME (txdl',tau') = t_lookup_funn f delta_g delta_b []) /\
( SOME (txdl',tau') = t_lookup_funn f delta_g delta_b delta_x) /\
(txdl = txdl' /\ tau = tau')) ``,
REPEAT STRIP_TAC >>
Cases_on `f` >>
fs[t_lookup_funn_def] >>
Cases_on `ALOOKUP delta_b s` >>
fs[] >> rw[]
);
Theorem t_lookup_funn_ext_lemma:
! delta_g delta_b delta_x f txdl tau .
SOME (txdl,tau) = t_lookup_funn (f) [] [] delta_x ==>
(? tau' txdl' . ( SOME (txdl',tau') = t_lookup_funn f delta_g delta_b delta_x) /\
(txdl = txdl' /\ tau = tau'))
Proof
REPEAT STRIP_TAC >>
Cases_on `f` >>
fs[t_lookup_funn_def]
QED
val WT_c_imp_global_or_local_lookup = prove ( ``