-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtypeSoundScript.sml
3325 lines (3257 loc) · 99.4 KB
/
typeSoundScript.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
(*
Proof of type soundness: a type-correct program does not crash.
*)
open preamble;
open astTheory typeSystemTheory semanticPrimitivesTheory fpSemTheory
evaluateTheory;
open namespacePropsTheory fpSemPropsTheory;
open semanticPrimitivesPropsTheory;
open evaluatePropsTheory;
open weakeningTheory typeSysPropsTheory typeSoundInvariantsTheory;
open semanticsTheory;
local open primSemEnvTheory in end;
val _ = temp_delsimps ["NORMEQ_CONV"]
val _ = temp_delsimps ["lift_disj_eq", "lift_imp_disj", "getOpClass_def"]
val _ = new_theory "typeSound";
val type_num_defs = LIST_CONJ [
Tarray_num_def,
Tbool_num_def,
Tchar_num_def,
Texn_num_def,
Tfn_num_def,
Tint_num_def,
Tlist_num_def,
Tref_num_def,
Tstring_num_def,
Ttup_num_def,
Tvector_num_def,
Tword64_num_def,
Tword8_num_def,
Tword8array_num_def,
Tdouble_num_def,
Treal_num_def];
Theorem list_rel_flat:
!R l1 l2. LIST_REL (LIST_REL R) l1 l2 ⇒ LIST_REL R (FLAT l1) (FLAT l2)
Proof
Induct_on `l1`
>> rw [FLAT]
>> rw [FLAT]
>> irule EVERY2_APPEND_suff
>> rw []
QED
Triviality fst_triple:
(\ (x,y,z). x) = FST
Proof
rw [FUN_EQ_THM]
>> pairarg_tac
>> rw []
QED
Theorem disjoint_image:
!s1 s2 f. DISJOINT (IMAGE f s1) (IMAGE f s2) ⇒ DISJOINT s1 s2
Proof
rw [DISJOINT_DEF, EXTENSION]
>> metis_tac []
QED
Triviality sing_list:
!l. LENGTH l = 1 ⇔ ?x. l = [x]
Proof
Cases
>> rw []
>> Cases_on `t`
>> rw []
QED
Triviality EVERY_LIST_REL:
EVERY (\x. f x y) l = LIST_REL (\x y. f x y) l (REPLICATE (LENGTH l) y)
Proof
induct_on `l` >>
srw_tac[][REPLICATE]
QED
Theorem v_unchanged[simp]:
!tenv x. tenv with v := tenv.v = tenv
Proof
srw_tac[][type_env_component_equality]
QED
Theorem check_dup_ctors_thm:
check_dup_ctors (tvs,tn,condefs) = ALL_DISTINCT (MAP FST condefs)
Proof
rw [check_dup_ctors_def] >>
induct_on `condefs` >>
rw [] >>
pairarg_tac >>
fs [] >>
eq_tac >>
rw [] >>
induct_on `condefs` >>
rw [] >>
pairarg_tac >>
fs []
QED
(* Classifying values of basic types *)
Theorem prim_canonical_values_thm:
(type_v tvs ctMap tenvS v Tint ∧ ctMap_ok ctMap ⇒ (∃n. v = Litv (IntLit n))) ∧
(type_v tvs ctMap tenvS v Tchar ∧ ctMap_ok ctMap ⇒ (∃c. v = Litv (Char c))) ∧
(type_v tvs ctMap tenvS v Tstring ∧ ctMap_ok ctMap ⇒ (∃s. v = Litv (StrLit s))) ∧
(type_v tvs ctMap tenvS v Tword8 ∧ ctMap_ok ctMap ⇒ (∃n. v = Litv (Word8 n))) ∧
(type_v tvs ctMap tenvS v Tword64 ∧ ctMap_ok ctMap ⇒ (∃n. v = Litv (Word64 n))) /\
(type_v tvs ctMap tenvS v Tdouble /\ ctMap_ok ctMap ==> (? f w. v = FP_WordTree f)) ∧
(type_v tvs ctMap tenvS v Treal /\ ctMap_ok ctMap ==> (? r. v = Real r)) ∧
(type_v tvs ctMap tenvS v (Ttup ts) ∧ ctMap_ok ctMap ⇒
(∃vs. v = Conv NONE vs ∧ LENGTH ts = LENGTH vs)) ∧
(type_v tvs ctMap tenvS v (Tfn t1 t2) ∧ ctMap_ok ctMap ⇒
(∃env n e. v = Closure env n e) ∨
(∃env funs n. v = Recclosure env funs n)) ∧
(type_v tvs ctMap tenvS v (Tref t1) ∧ ctMap_ok ctMap ∧ type_s ctMap envS tenvS ⇒
(∃n v2. v = Loc T n ∧ store_lookup n envS = SOME (Refv v2) ∧
type_v 0 ctMap tenvS v2 t1)) ∧
(type_v tvs ctMap tenvS v Tword8array ∧ ctMap_ok ctMap ∧ type_s ctMap envS tenvS ⇒
(∃n ws. v = Loc T n ∧ store_lookup n envS = SOME (W8array ws) ∧
FLOOKUP tenvS n = SOME W8array_t)) ∧
(type_v tvs ctMap tenvS v (Tarray t) ∧ ctMap_ok ctMap ∧ type_s ctMap envS tenvS ⇒
(∃n vs. v = Loc T n ∧ store_lookup n envS = SOME (Varray vs) ∧
EVERY (\v. type_v 0 ctMap tenvS v t) vs ∧
FLOOKUP tenvS n = SOME (Varray_t t))) ∧
(type_v tvs ctMap tenvS v (Tvector t) ∧ ctMap_ok ctMap ⇒
(?vs. v = Vectorv vs ∧ EVERY (\v. type_v tvs ctMap tenvS v t) vs))
Proof
strip_tac >>
rpt (conj_tac) >>
simp [Once type_v_cases] >>
fs [prim_type_nums_def, ctMap_ok_def, type_num_defs] >>
rw [] >>
imp_res_tac type_funs_Tfn >>
fs [type_num_defs] >>
TRY (Cases_on `stamp` >> res_tac >> fs [] >> NO_TAC) >>
fs [type_s_def]
>- metis_tac [LIST_REL_LENGTH] >>
res_tac >>
Cases_on `v` >>
fs [type_sv_def]
QED
Triviality has_lists_v_to_list:
!ctMap tvs tenvS t3.
ctMap_ok ctMap ∧
ctMap_has_lists ctMap ∧
type_v tvs ctMap tenvS v (Tlist t3)
⇒
?vs. v_to_list v = SOME vs ∧
EVERY (\v. type_v tvs ctMap tenvS v t3) vs ∧
(t3 = Tchar ⇒ ?vs. v_to_char_list v = SOME vs) ∧
(t3 = Tstring ⇒ ∃str. vs_to_string vs = SOME str)
Proof
measureInduct_on `v_size v` >>
srw_tac[][] >>
pop_assum mp_tac >>
srw_tac[][Once type_v_cases] >>
full_simp_tac(srw_ss())[] >>
imp_res_tac type_funs_Tfn >>
fs [] >>
TRY (fs [type_num_defs] >> NO_TAC) >>
`?cn. stamp = TypeStamp cn list_type_num`
by (
fs [ctMap_ok_def, ctMap_has_lists_def] >>
metis_tac [same_type_def, stamp_nchotomy]) >>
rw [] >>
full_simp_tac(srw_ss())[ctMap_has_lists_def] >>
`cn = "::" ∨ cn = "[]"` by metis_tac [NOT_SOME_NONE] >>
srw_tac[][] >>
full_simp_tac(srw_ss())[] >>
srw_tac[][] >>
fs [EVERY2_THM] >>
full_simp_tac(srw_ss())[] >>
srw_tac[][v_to_list_def,v_to_char_list_def,vs_to_string_def] >>
full_simp_tac(srw_ss())[type_subst_def] >>
rename1 `type_v _ _ _ v _` >>
LAST_X_ASSUM (mp_tac o Q.SPEC `v`) >>
simp [v_size_def] >>
disch_then drule >>
simp [] >>
disch_then drule >>
fs [flookup_fupdate_list] >>
rw [] >>
rw [] >>
imp_res_tac (SIMP_RULE (srw_ss()) [] prim_canonical_values_thm) >>
rw [v_to_char_list_def, vs_to_string_def]
QED
Theorem ctor_canonical_values_thm:
(type_v tvs ctMap tenvS v Tbool ∧ ctMap_ok ctMap ∧ ctMap_has_bools ctMap ⇒
(∃b. v = Boolv b)) /\
(type_v tvs ctMap tenvS v (Tlist t) ∧ ctMap_ok ctMap ∧ ctMap_has_lists ctMap ⇒
?vs.
v_to_list v = SOME vs ∧
EVERY (\v. type_v tvs ctMap tenvS v t) vs ∧
(t = Tchar ⇒ ?vs. v_to_char_list v = SOME vs) ∧
(t = Tstring ⇒ ∃str. vs_to_string vs = SOME str)) ∧
(type_v tvs ctMap tenvS v (Tapp ts ti) ∧
ctMap_ok ctMap ∧
FLOOKUP ctMap stamp = SOME (tvs',ts',ti) ⇒
(?cn n vs. same_type stamp (TypeStamp cn n) ∧ v = Conv (SOME (TypeStamp cn n)) vs) ∨
(?n vs. same_type stamp (ExnStamp n) ∧ v = Conv (SOME (ExnStamp n)) vs))
Proof
rw []
>- (
fs [Once type_v_cases] >>
full_simp_tac std_ss [ctMap_has_bools_def, Boolv_def, type_num_defs, ctMap_ok_def] >>
imp_res_tac type_funs_Tfn
>- (
`stamp = TypeStamp "True" bool_type_num ∨ stamp = TypeStamp "False" bool_type_num`
by metis_tac [NOT_SOME_NONE, same_type_def, stamp_nchotomy] >>
var_eq_tac >>
rpt (qpat_x_assum `LIST_REL _ _ _` mp_tac) >>
rpt (qpat_x_assum `FLOOKUP _ _ = SOME _` mp_tac) >>
simp_tac list_ss [] >>
metis_tac [])
>- fs [])
>- metis_tac [has_lists_v_to_list, Tlist_def, Tchar_def, Tstring_def]
>- (
fs [Once type_v_cases, ctMap_ok_def, type_num_defs] >>
rw [] >>
fs [] >>
res_tac >>
fs [] >>
imp_res_tac type_funs_Tfn >>
fs [prim_type_nums_def, type_num_defs] >>
Cases_on `stamp` >>
fs [same_type_def] >>
res_tac >>
fs [] >>
Cases_on `stamp'` >>
fs [same_type_def] >>
res_tac >>
fs [])
QED
Triviality same_type_refl:
!t. same_type t t
Proof
Cases_on `t` >>
rw [same_type_def]
QED
Triviality eq_same_type:
(!v1 v2 tvs ctMap cns tenvS t.
ctMap_ok ctMap ∧
type_v tvs ctMap tenvS v1 t ∧
type_v tvs ctMap tenvS v2 t
⇒
do_eq v1 v2 ≠ Eq_type_error) ∧
(!vs1 vs2 tvs ctMap cns tenvS ts.
ctMap_ok ctMap ∧
LIST_REL (type_v tvs ctMap tenvS) vs1 ts ∧
LIST_REL (type_v tvs ctMap tenvS) vs2 ts
⇒
do_eq_list vs1 vs2 ≠ Eq_type_error)
Proof
ho_match_mp_tac do_eq_ind >>
rw [do_eq_def] >>
TRY (
(* Solve most non-constructor cases *)
ONCE_REWRITE_TAC [type_v_cases] >>
rw [] >>
CCONTR_TAC >>
fs [] >>
imp_res_tac type_funs_Tfn >>
fs [prim_type_nums_def, lit_same_type_def,ctor_same_type_def, type_num_defs] >>
NO_TAC) >>
TRY (
(* Solve trivial constructor cases *)
ONCE_REWRITE_TAC [type_v_cases] >>
CCONTR_TAC >>
fs [] >>
rw [] >>
fs [ctMap_ok_def] >>
rw [] >>
imp_res_tac type_funs_Tfn >>
TRY (fs [prim_type_nums_def, type_num_defs] >> NO_TAC) >>
Cases_on `stamp` >>
res_tac >>
fs [prim_type_nums_def, type_num_defs] >>
NO_TAC) >>
TRY (
(* floating-point value trees *)
rename1 `Boolv (compress_bool fp)` >>
fs[Once type_v_cases] >> NO_TAC)
>- (
(* Same constructor and type *)
rpt (qpat_x_assum `type_v _ _ _ _ _` mp_tac) >>
ONCE_REWRITE_TAC [type_v_cases] >>
rw [] >>
fs [] >>
metis_tac [])
>- (
(* Different constructor and type *)
ONCE_REWRITE_TAC [type_v_cases] >>
CCONTR_TAC >>
fs [] >>
rw [] >>
fs [ctor_same_type_def] >>
fs [ctMap_ok_def] >>
rw [] >>
metis_tac [prim_type_nums_def, same_type_refl, stamp_nchotomy, MEM,
Q.prove (`Ttup_num ≠ Texn_num`, rw [type_num_defs])])
(* Vectors *)
>- (
rpt (qpat_x_assum `type_v _ _ _ _ _` mp_tac) >>
ONCE_REWRITE_TAC [type_v_cases] >>
srw_tac[][] >>
full_simp_tac(srw_ss())[combinTheory.o_DEF, EVERY_LIST_REL] >>
`(\x y. type_v tvs ctMap tenvS x y) = type_v tvs ctMap tenvS` by metis_tac [] >>
full_simp_tac(srw_ss())[] >>
metis_tac [])
>- (FULL_CASE_TAC \\
full_simp_tac(srw_ss())[bool_case_eq]
\\ metis_tac[])
QED
Triviality type_env_conv_thm:
∀ctMap envC tenvC.
nsAll2 (type_ctor ctMap) envC tenvC ⇒
∀cn tvs ts tn ti.
(nsLookup tenvC cn = SOME (tvs,ts,ti) ⇒
?cn' stamp.
nsLookup envC cn = SOME (LENGTH ts,stamp) ∧
FLOOKUP ctMap stamp = SOME (tvs, ts, ti)) ∧
(nsLookup tenvC cn = NONE ⇒ nsLookup envC cn = NONE)
Proof
rw []
>> imp_res_tac nsAll2_nsLookup2
>> TRY (PairCases_on `v1`)
>> fs [type_ctor_def] >>
rw [] >>
metis_tac [nsAll2_nsLookup_none]
QED
Triviality type_funs_fst:
!tenv tenvE funs tenv'.
type_funs tenv tenvE funs tenv'
⇒
MAP FST funs = MAP FST tenv'
Proof
induct_on `funs` >>
srw_tac[][] >>
pop_assum (mp_tac o SIMP_RULE (srw_ss()) [Once type_e_cases]) >>
srw_tac[][] >>
srw_tac[][] >>
metis_tac []
QED
Triviality type_recfun_env_help:
∀fn funs funs' ctMap tenv bindings tenvE env tenvS tvs bindings'.
ALL_DISTINCT (MAP FST funs') ∧
tenv_ok tenv ∧
type_all_env ctMap tenvS env (tenv with v := add_tenvE tenvE tenv.v) ∧
tenv_val_exp_ok tenvE ∧
num_tvs tenvE = 0 ∧
(!fn t. (ALOOKUP bindings fn = SOME t) ⇒ (ALOOKUP bindings' fn = SOME t)) ∧
type_funs tenv (bind_var_list 0 bindings' (bind_tvar tvs tenvE)) funs' bindings' ∧
type_funs tenv (bind_var_list 0 bindings' (bind_tvar tvs tenvE)) funs bindings
⇒
LIST_REL (λ(x,y) (x',y'). x = x' ∧ (λ(tvs,t). type_v tvs ctMap tenvS y t) y')
(MAP (λ(fn,n,e). (fn,Recclosure env funs' fn)) funs)
(MAP (λ(x,t). (x,tvs,t)) bindings)
Proof
induct_on `funs`
>> srw_tac[][]
>> pop_assum mp_tac
>> simp [Once type_e_cases]
>> rw []
>> rw []
>- (
simp [Once type_v_cases]
>> qexists_tac `tenv`
>> qexists_tac `tenvE`
>> rw []
>> fs [type_all_env_def]
>> simp []
>> qexists_tac `bindings'`
>> rw []
>> imp_res_tac type_funs_fst
>> fs []
>> first_x_assum (qspec_then `fn` mp_tac)
>> rw []
>> drule ALOOKUP_MEM
>> rw [MEM_MAP]
>> metis_tac [FST])
>- (
first_x_assum irule
>> simp []
>> qexists_tac `bindings'`
>> qexists_tac `tenv`
>> qexists_tac `tenvE`
>> simp []
>> rw []
>> fs []
>> metis_tac [SOME_11, NOT_SOME_NONE])
QED
Triviality type_recfun_env:
∀funs ctMap tenvS tvs tenv tenvE env bindings.
tenv_ok tenv ∧
type_all_env ctMap tenvS env (tenv with v := add_tenvE tenvE tenv.v) ∧
tenv_val_exp_ok tenvE ∧
num_tvs tenvE = 0 ∧
type_funs tenv (bind_var_list 0 bindings (bind_tvar tvs tenvE)) funs bindings ∧
ALL_DISTINCT (MAP FST funs)
⇒
LIST_REL (λ(x,y) (x',y'). x = x' ∧ (λ(tvs,t). type_v tvs ctMap tenvS y t) y')
(MAP (λ(fn,n,e). (fn,Recclosure env funs fn)) funs)
(MAP (λ(x,t). (x,tvs,t)) bindings)
Proof
metis_tac [type_recfun_env_help]
QED
val type_v_exn = SIMP_RULE (srw_ss()) [] (Q.prove (
`!tvs cenv senv.
ctMap_has_exns cenv ⇒
type_v tvs cenv senv (Conv (SOME chr_stamp) []) Texn ∧
type_v tvs cenv senv (Conv (SOME subscript_stamp) []) Texn ∧
type_v tvs cenv senv (Conv (SOME bind_stamp) []) Texn ∧
type_v tvs cenv senv (Conv (SOME div_stamp) []) Texn`,
ONCE_REWRITE_TAC [type_v_cases] >>
srw_tac[][ctMap_has_exns_def] >>
metis_tac [type_v_rules]));
(*
Triviality v_to_list_type:
!v vs.
ctMap_ok ctMap ∧
ctMap_has_lists ctMap ∧
v_to_list v = SOME vs ∧
type_v 0 ctMap tenvS v (Tapp [t] (TC_name (Short "list")))
⇒
type_v tvs ctMap tenvS (Vectorv vs) (Tapp [t] TC_vector)
Proof
ho_match_mp_tac v_to_list_ind >>
srw_tac[][v_to_list_def]
>- full_simp_tac(srw_ss())[Once type_v_cases] >>
every_case_tac >>
full_simp_tac(srw_ss())[] >>
srw_tac[][] >>
qpat_x_assum `type_v x0 x1 x2 (Conv x3 x4) x5` (mp_tac o SIMP_RULE (srw_ss()) [Once type_v_cases]) >>
srw_tac[][] >>
srw_tac[][Once type_v_cases] >>
res_tac >>
full_simp_tac(srw_ss())[ctMap_has_lists_def] >>
srw_tac[][] >>
full_simp_tac(srw_ss())[type_subst_def, flookup_fupdate_list]
>- metis_tac [type_v_weakening, weakCT_refl, weakS_refl] >>
srw_tac[][] >>
full_simp_tac(srw_ss())[tid_exn_to_tc_def] >>
res_tac >>
FIRST_X_ASSUM (mp_tac o SIMP_RULE (srw_ss()) [Once type_v_cases]) >>
srw_tac[][]
QED
Triviality v_to_char_list_type:
!v vs.
ctMap_has_lists ctMap ∧
v_to_char_list v = SOME vs ∧
type_v 0 ctMap tenvS v (Tapp [t] (TC_name (Short "list")))
⇒
type_v tvs ctMap tenvS (Litv (StrLit (IMPLODE vs))) (Tstring)
Proof
ho_match_mp_tac v_to_char_list_ind >>
srw_tac[][v_to_char_list_def]
>- full_simp_tac(srw_ss())[Once type_v_cases] >>
every_case_tac >>
full_simp_tac(srw_ss())[] >>
srw_tac[][] >>
qpat_x_assum `type_v x0 x1 x2 (Conv x3 x4) x5` (mp_tac o SIMP_RULE (srw_ss()) [Once type_v_cases]) >>
srw_tac[][] >>
srw_tac[][Once type_v_cases]
QED
*)
Triviality type_v_Boolv:
ctMap_has_bools ctMap ⇒ type_v tvs ctMap tenvS (Boolv b) Tbool
Proof
srw_tac[][Boolv_def] >>
srw_tac[][Once type_v_cases,LENGTH_NIL] >>
full_simp_tac(srw_ss())[ctMap_has_bools_def] >>
srw_tac[][Once type_v_cases]
QED
Triviality remove_lambda_prod:
(\ (x,y). P x y) = (\xy. P (FST xy) (SND xy))
Proof
rw [FUN_EQ_THM]
>> pairarg_tac
>> rw []
QED
Theorem opapp_type_sound:
!ctMap tenvS vs ts t.
ctMap_ok ctMap ∧
type_op Opapp ts t ∧
LIST_REL (type_v 0 ctMap tenvS) vs ts
⇒
?env e tenv tenvE.
tenv_ok tenv ∧
tenv_val_exp_ok tenvE ∧
num_tvs tenvE = 0 ∧
type_all_env ctMap tenvS env (tenv with v := add_tenvE tenvE tenv.v) ∧
type_e tenv tenvE e t ∧
do_opapp vs = SOME (env,e)
Proof
rw [type_op_cases] >>
fs [] >>
rw [] >>
MAP_EVERY (TRY o drule o SIMP_RULE (srw_ss()) [] o GEN_ALL)
(CONJUNCTS prim_canonical_values_thm) >>
rw [do_opapp_def]
>- (
rename1 `type_v _ _ _ (Closure env n e) _`
>> qpat_x_assum `type_v _ _ _ (Closure env n e) _` mp_tac
>> simp [Once type_v_cases]
>> rw []
>> qexists_tac `tenv`
>> qexists_tac `Bind_name n 0 t2 (bind_tvar 0 tenvE)`
>> rw []
>> fs [tenv_ok_def, type_all_env_def, tenv_val_exp_ok_def, bind_tvar_def]
>> simp [add_tenvE_def]
>> irule nsAll2_nsBind
>> simp [])
>- (
rename1 `type_v _ _ _ (Recclosure env funs n) _`
>> qpat_x_assum `type_v _ _ _ (Recclosure env funs n) _` mp_tac
>> simp [Once type_v_cases]
>> rw []
>> imp_res_tac type_funs_find_recfun
>> fs []
>> rw []
>> imp_res_tac (SIMP_RULE (srw_ss()) [Tfn_def] type_recfun_lookup)
>> fs []
>> qmatch_assum_abbrev_tac `type_e _ b _ _`
>> qexists_tac `tenv`
>> qexists_tac `b`
>> fs [type_all_env_def]
>> unabbrev_all_tac
>> rw [tenv_val_exp_ok_def, add_tenvE_def]
>- metis_tac [type_v_freevars]
>- (
irule tenv_val_exp_ok_bvl_funs
>> simp []
>> metis_tac [])
>- (
irule nsAll2_nsBind
>> simp [build_rec_env_merge, nsAppend_to_nsBindList, add_tenvE_bvl]
>> irule nsAll2_nsBindList
>> simp []
>> irule type_recfun_env
>> simp [type_all_env_def]
>> metis_tac [])
>- (
simp [remove_lambda_prod]
>> metis_tac []))
QED
Definition store_type_extension_def:
store_type_extension tenvS1 tenvS2 =
?tenvS'. (tenvS2 = FUNION tenvS' tenvS1) ∧
(!l. (FLOOKUP tenvS' l = NONE) ∨ (FLOOKUP tenvS1 l = NONE))
End
Theorem store_type_extension_weakS:
!tenvS1 tenvS2.
store_type_extension tenvS1 tenvS2 ⇒ weakS tenvS2 tenvS1
Proof
srw_tac[][store_type_extension_def, weakS_def, FLOOKUP_FUNION] >>
full_simp_tac(srw_ss())[SUBMAP_DEF, FLOOKUP_DEF, FUNION_DEF] >>
metis_tac []
QED
Theorem store_type_extension_refl:
!tenvS. store_type_extension tenvS tenvS
Proof
rw [store_type_extension_def] >>
qexists_tac `FEMPTY` >>
rw []
QED
Theorem store_type_extension_trans:
!s1 s2 s3.
store_type_extension s1 s2 ∧ store_type_extension s2 s3 ⇒
store_type_extension s1 s3
Proof
rw [store_type_extension_def]
>> qexists_tac `FUNION tenvS'' tenvS'`
>> rw [FUNION_ASSOC, FLOOKUP_FUNION]
>> CASE_TAC
>> rw []
>> fs [FLOOKUP_FUNION]
>> first_x_assum (qspec_then `l` mp_tac)
>> rw []
>> every_case_tac
>> fs []
QED
Theorem store_assign_type_sound:
!ctMap tenvS store sv st l.
type_s ctMap store tenvS ∧
FLOOKUP tenvS l = SOME st ∧
type_sv ctMap tenvS sv st
⇒
?store'.
store_assign l sv store = SOME store' ∧
type_s ctMap store' tenvS
Proof
rw [store_assign_def, type_s_def, store_v_same_type_def]
>- (
first_x_assum (qspec_then `l` mp_tac)
>> rw [store_lookup_def]
>> fs [FLOOKUP_DEF])
>- (
first_x_assum (qspec_then `l` mp_tac)
>> fs [store_lookup_def]
>> every_case_tac
>> fs []
>> Cases_on `st`
>> fs [type_sv_def])
>- (
first_x_assum (qspec_then `l'` mp_tac)
>> rw [store_lookup_def])
>- (
fs [store_lookup_def, EL_LUPDATE]
>> rw []
>> fs [])
QED
Theorem store_alloc_type_sound:
!ctMap tenvS store sv st.
ctMap_ok ctMap ∧
type_s ctMap store tenvS ∧
type_sv ctMap tenvS sv st
⇒
?store' tenvS' n.
store_type_extension tenvS tenvS' ∧
store_alloc sv store = (store', n) ∧
type_s ctMap store' tenvS' ∧
FLOOKUP tenvS' n = SOME st
Proof
rw [store_alloc_def]
>> qexists_tac `tenvS |+ (LENGTH store, st)`
>> rw [store_type_extension_def, FLOOKUP_UPDATE]
>- (
qexists_tac `FEMPTY |+ (LENGTH store, st)`
>> fs [type_s_def]
>> rw [FLOOKUP_UPDATE, fmap_eq_flookup, FLOOKUP_FUNION]
>> rw []
>> fs [store_lookup_def]
>> CCONTR_TAC
>> fs []
>> `l < l` by metis_tac [option_nchotomy]
>> fs [])
>- (
fs [type_s_def, store_lookup_def, FLOOKUP_UPDATE, GSYM SNOC_APPEND]
>> rw []
>> rw [EL_LENGTH_SNOC, EL_SNOC]
>> irule type_sv_weakening
>> rw [weakS_def]
>> qexists_tac `ctMap`
>> rw [weakCT_refl]
>> qexists_tac `tenvS`
>> rw []
>> CCONTR_TAC
>> fs [FLOOKUP_DEF]
>> fs []
>> res_tac
>> fs [])
QED
(*
Theorem store_lookup_type_sound:
!ctMap tenvS store n st.
type_s ctMap store tenvS ∧
FLOOKUP tenvS n = SOME st
⇒
?sv.
store_lookup n store = SOME sv ∧
type_sv ctMap tenvS sv st
Proof
rw [type_s_def]
>> metis_tac []
QED
*)
Theorem type_v_list_to_v:
!x xs t.
type_v n ctMap tenvS x t /\
v_to_list x = SOME xs ==>
type_v n ctMap tenvS (list_to_v xs) t
Proof
recInduct v_to_list_ind \\ rw [Once type_v_cases]
\\ fs [v_to_list_def, list_to_v_def] \\ rw []
\\ fs [list_to_v_def]
\\ FULL_CASE_TAC \\ fs [] \\ rw []
\\ fs [list_to_v_def]
\\ qpat_x_assum `type_v _ _ _ _ _` mp_tac
\\ rw [Once type_v_cases] \\ simp [Once type_v_cases]
QED
Theorem type_v_list_to_v_APPEND:
!xs ys t.
ctMap_has_lists ctMap /\
type_v 0 ctMap tenvS (list_to_v xs) (Tapp [t] Tlist_num) /\
type_v 0 ctMap tenvS (list_to_v ys) (Tapp [t] Tlist_num)
==>
type_v 0 ctMap tenvS (list_to_v (xs ++ ys)) (Tapp [t] Tlist_num)
Proof
Induct \\ rw [list_to_v_def]
\\ ntac 2 (pop_assum mp_tac)
\\ rw [Once type_v_cases]
\\ rw [Once type_v_cases]
\\ rename1 `_ = [t1;t2]`
\\ `LENGTH ts = LENGTH [t1;t2]` by metis_tac [LENGTH_MAP]
\\ fs [LENGTH_EQ_NUM_compute] \\ rveq
\\ fs [] \\ rveq
\\ imp_res_tac ctMap_has_lists_def \\ fs [] \\ rveq
\\ ntac 2 (pop_assum kall_tac)
\\ qpat_x_assum `type_v _ _ _ (_ xs) _` mp_tac
\\ EVAL_TAC \\ strip_tac
\\ first_x_assum (qspec_then `ys` mp_tac)
\\ EVAL_TAC \\ metis_tac [Tlist_num_def]
QED
Theorem op_type_sound:
!ctMap tenvS vs op ts t store (ffi : 'ffi ffi_state).
good_ctMap ctMap ∧
op ≠ Opapp ∧
(~ (getOpClass op = Icing)) /\ (* FP soundness separate *)
type_s ctMap store tenvS ∧
type_op op ts t ∧
check_freevars 0 [] t ∧
LIST_REL (type_v 0 ctMap tenvS) vs (REVERSE ts)
⇒
?tenvS' store' ffi' r.
store_type_extension tenvS tenvS' ∧
type_s ctMap store' tenvS' ∧
do_app (store,ffi) op (REVERSE vs) = SOME ((store', ffi'), r) ∧
case r of
| Rval v => type_v 0 ctMap tenvS' v t
| Rerr (Rraise v) => type_v 0 ctMap tenvS' v Texn
| Rerr (Rabort(Rffi_error _)) => T
| Rerr (Rabort _) => F
Proof
rw [type_op_cases, good_ctMap_def] >>
fs [] >>
rw [] >>
rpt (
MAP_EVERY (TRY o drule o SIMP_RULE (srw_ss()) [] o GEN_ALL)
(CONJUNCTS prim_canonical_values_thm) >>
qpat_x_assum `type_v _ _ _ _ _` mp_tac) >>
rw [] >>
rw [do_opapp_def]
>> TRY ( (* FP cases *)
fs[getOpClass_def] >> NO_TAC)
>> TRY ( (* simple cases *)
rw [do_app_cases, PULL_EXISTS] >>
simp [Once type_v_cases] >>
qexists_tac `tenvS` >>
rw [store_type_extension_refl] >>
NO_TAC)
>> TRY ( (* Integer ops *)
rename1 `Opn _` >>
rw [do_app_cases, PULL_EXISTS] >>
rename1 `(op = Divide ∨ op = Module) ∧ divisor = 0`
>> Cases_on `(op = Divide ∨ op = Module) ∧ divisor = 0`
>- (
fs []
>> metis_tac [type_v_exn, store_type_extension_refl, div_exn_v_def])
>- (
fs []
>> simp [Once type_v_cases]
>> metis_tac [store_type_extension_refl]))
>> TRY ( (* Boolean ops *)
rename1 `Opb _` >>
rw [do_app_cases, PULL_EXISTS] >>
metis_tac [type_v_Boolv, store_type_extension_refl, Tbool_def])
>> TRY ( (* Equality *)
rename1`Equality` >>
rw [do_app_cases, PULL_EXISTS] >>
metis_tac [Tbool_def, type_v_Boolv, store_type_extension_refl, eq_result_nchotomy, eq_same_type])
>> TRY ( (* real comparisons *)
rename1`Real_cmp cmp` >>
rw [do_app_cases, PULL_EXISTS] >>
simp [Once type_v_cases] >>
qexists_tac `tenvS` >>
rw [store_type_extension_refl, Boolv_def] >> fs[ctMap_has_bools_def] >> NO_TAC)
>> TRY ( (* ref update *)
rename1 `Opassign` >>
res_tac >>
rw [do_app_cases, PULL_EXISTS] >>
simp [Once type_v_cases]
>> qpat_x_assum `type_v _ _ _ (Loc _ _) _` mp_tac
>> simp [Once type_v_cases]
>> rw [type_num_defs]
>> metis_tac [type_sv_def, store_type_extension_refl, store_assign_type_sound])
>> TRY ( (* ref alloc *)
rename1 `Opref`
>> rw [do_app_cases, PULL_EXISTS]
>> simp [Once type_v_cases]
>> rename1 `type_v _ _ _ v t`
>> `type_sv ctMap tenvS (Refv v) (Ref_t t)` by rw [type_sv_def]
>> drule store_alloc_type_sound
>> rpt (disch_then drule)
>> rw []
>> metis_tac [type_v_freevars])
>> TRY ( (* deref *)
rename1 `Opderef`
>> res_tac
>> rw [do_app_cases, PULL_EXISTS]
>> rw []
>> metis_tac [store_type_extension_refl])
>> TRY ( (* W8array alloc *)
rename1 `Aw8alloc`
>> rw [do_app_cases, PULL_EXISTS]
>> rename1 `type_v _ _ _ (Litv (Word8 w)) _`
>> rename1 `type_v _ _ _ (Litv (IntLit n)) _`
>> `type_sv ctMap tenvS (W8array (REPLICATE (Num (ABS n)) w)) W8array_t`
by rw [type_sv_def]
>> drule store_alloc_type_sound
>> rpt (disch_then drule)
>> rw []
>> Cases_on `n < 0`
>> simp [type_v_exn, sub_exn_v_def]
>- metis_tac [store_type_extension_refl]
>> simp [Once type_v_cases]
>> metis_tac [store_type_extension_refl])
>> TRY ( (* W8array lookup *)
rename1 `Aw8sub` >>
rw [do_app_cases, PULL_EXISTS] >>
first_x_assum drule >>
rw []
>> Cases_on `n < 0`
>> rw [PULL_EXISTS, type_v_exn, sub_exn_v_def]
>- metis_tac [store_type_extension_refl]
>- (
Cases_on `Num (ABS n) ≥ LENGTH ws`
>> rw []
>> simp [type_v_exn]
>> simp [Once type_v_cases]
>> metis_tac [store_type_extension_refl]))
>> TRY ( (* W8array length *)
rename1 `Aw8length` >>
res_tac >>
rw [do_app_cases, PULL_EXISTS] >>
simp [Once type_v_cases]
>> metis_tac [store_type_extension_refl])
>> TRY ( (* W8array assignment *)
rename1 `Aw8update` >>
rw [do_app_cases, PULL_EXISTS] >>
first_x_assum drule >>
rw [] >>
rename1 `type_v _ _ _ (Litv (IntLit z)) _` >>
rename1 `type_v _ _ _ (Loc _ l) _`
>> Cases_on `z < 0`
>> fs [type_v_exn, sub_exn_v_def]
>- metis_tac [store_type_extension_refl]
>> rename1 `store_lookup _ _ = SOME (W8array ws)`
>> Cases_on `Num (ABS z) ≥ LENGTH ws`
>> rw [type_v_exn]
>- metis_tac [store_type_extension_refl]
>> simp [Once type_v_cases]
>> `type_sv ctMap tenvS (W8array (LUPDATE n (Num (ABS z)) ws)) W8array_t`
by rw [type_sv_def]
>> drule store_assign_type_sound
>> rpt (disch_then drule)
>> rw []
>> metis_tac [store_type_extension_refl])
>> TRY ( (* copy string *)
rename1 `CopyStrStr` >>
rw [do_app_cases, PULL_EXISTS] >>
rename1`copy_array a b c`
\\ Cases_on`copy_array a b c` \\ simp[]
\\ simp[type_v_exn, sub_exn_v_def]
>- metis_tac[store_type_extension_refl]
\\ simp[Once type_v_cases]
>- metis_tac[store_type_extension_refl] )
>> TRY ( (* copy string/array *)
rename1 `CopyStrAw8` >>
rw [do_app_cases, PULL_EXISTS] >>
res_tac >>
rw [] >>
rename1`copy_array a b c`
\\ Cases_on`copy_array a b c` \\ simp[]
\\ simp[type_v_exn, sub_exn_v_def]
>- metis_tac[store_type_extension_refl]
\\ simp[Once type_v_cases]
>> drule store_assign_type_sound
>> rpt (disch_then drule)
>> rw []
>> metis_tac [store_type_extension_refl, type_sv_def])
>> TRY ( (* copy array/string *)
rename1 `CopyAw8Str` >>
res_tac >>
rw [do_app_cases, PULL_EXISTS] >>
rename1`copy_array a b c`
\\ Cases_on`copy_array a b c` \\ simp[]
\\ simp[type_v_exn, sub_exn_v_def]
>- metis_tac[store_type_extension_refl]
\\ simp[Once type_v_cases]
>- metis_tac[store_type_extension_refl] )
>> TRY ( (* copy array/array *)
rename1 `CopyAw8Aw8` >>
rw [do_app_cases, PULL_EXISTS] >>
res_tac >>
rw [] >>
rename1`copy_array a b c`
\\ Cases_on`copy_array a b c` \\ simp[]
\\ simp[type_v_exn, sub_exn_v_def]
>- metis_tac[store_type_extension_refl]
\\ simp[Once type_v_cases]
>> drule store_assign_type_sound
>> rpt (disch_then drule)
>> rw []
>> metis_tac [store_type_extension_refl, type_sv_def])
>> TRY ( (* Int to Char *)
rename1`Chr` >>
rw [do_app_cases, PULL_EXISTS] >>
Cases_on `n < 0 ∨ n > 255`
>> rw []
>> rw []
>> simp [type_v_exn, chr_exn_v_def]
>> fs []
>> simp [Once type_v_cases]
>> metis_tac [store_type_extension_refl])
>> TRY ( (* character boolean ops *)
rename1 `Chopb` >>
rw [do_app_cases, PULL_EXISTS] >>
metis_tac [type_v_Boolv, store_type_extension_refl, Tbool_def])
>> TRY ( (* list to string *)
rename1 `Implode` >>
rw [do_app_cases, PULL_EXISTS] >>
MAP_EVERY (TRY o drule o SIMP_RULE (srw_ss()) [] o GEN_ALL)
(CONJUNCTS ctor_canonical_values_thm) >>
rw [] >>
simp [Once type_v_cases] >>
metis_tac [store_type_extension_refl])
>> TRY ( (* string to list *)
rename1 `Explode` >>
rw [do_app_cases, PULL_EXISTS] >>
MAP_EVERY (TRY o drule o SIMP_RULE (srw_ss()) [] o GEN_ALL)
(CONJUNCTS ctor_canonical_values_thm) >>
rw [] >>
goal_assum (first_assum o mp_then Any mp_tac) >>
simp [store_type_extension_refl] >>
qspec_tac (`s`,`s`) >> Induct >>
fs [IMPLODE_EXPLODE_I,list_to_v_def,ctMap_has_lists_def] >>
once_rewrite_tac [type_v_cases] >> simp [] >>
simp [type_subst_def,FLOOKUP_UPDATE,FUPDATE_LIST,check_freevars_def] >>
once_rewrite_tac [type_v_cases] >> simp [])
>> TRY ( (* string lookup *)
rename1 `Strsub` >>
rw [do_app_cases, PULL_EXISTS] >>
Cases_on `n < 0`
>> rw [type_v_exn, sub_exn_v_def]
>- metis_tac [store_type_extension_refl]
>> Cases_on `Num (ABS n) ≥ LENGTH s`
>> rw [type_v_exn]
>- metis_tac [store_type_extension_refl]
>> simp [Once type_v_cases, EVERY_EL]
>> metis_tac [store_type_extension_refl])
>> TRY ( (* string concat *)
rename1`Strcat` >>
rw [do_app_cases, PULL_EXISTS] >>
simp[Once type_v_cases]
\\ imp_res_tac has_lists_v_to_list
\\ rveq \\ fs[]
\\ metis_tac[store_type_extension_refl] )
>> TRY ( (* list to vector *)
rename1`VfromList` >>
rw [do_app_cases, PULL_EXISTS] >>
MAP_EVERY (TRY o drule o SIMP_RULE (srw_ss()) [] o GEN_ALL)
(CONJUNCTS ctor_canonical_values_thm) >>
rw [] >>
rw [] >>
simp [Once type_v_cases] >>
fs [check_freevars_def] >>
metis_tac [store_type_extension_refl])
>> TRY ( (* vector lookup *)
rename1 `Vsub` >>
rw [do_app_cases, PULL_EXISTS]
>> Cases_on `n < 0`
>> rw [PULL_EXISTS, type_v_exn, sub_exn_v_def]
>- metis_tac [store_type_extension_refl]
>> Cases_on `Num (ABS n) ≥ LENGTH vs` >>
rw []
>- (
rw [type_v_exn, Once type_v_cases] >>
metis_tac [store_type_extension_refl]) >>
fs [EVERY_EL]
>> `Num (ABS n) < LENGTH vs` by decide_tac
>> metis_tac [store_type_extension_refl])
>> TRY ( (* Array alloc *)
rename1 `Aalloc` >>
rw [do_app_cases, PULL_EXISTS] >>