forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeSysPropsScript.sml
3337 lines (3140 loc) · 100 KB
/
typeSysPropsScript.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
(*
Theorems about the type system.
*)
open preamble
open libTheory astTheory namespaceTheory typeSystemTheory typeSoundInvariantsTheory;
open astPropsTheory;
open namespacePropsTheory;
local open semanticPrimitivesPropsTheory in end
val _ = temp_delsimps ["lift_disj_eq", "lift_imp_disj"]
val _ = new_theory "typeSysProps";
val find_recfun_def = semanticPrimitivesTheory.find_recfun_def;
(*
val same_tid_def = semanticPrimitivesTheory.same_tid_def;
val check_dup_ctors_cons = semanticPrimitivesPropsTheory.check_dup_ctors_cons;
val no_dup_types_def = semanticPrimitivesTheory.no_dup_types_def;
val decs_to_types_def = semanticPrimitivesTheory.decs_to_types_def;
*)
val _ = export_rewrites [
"typeSystem.Tarray_def",
"typeSystem.Tbool_def",
"typeSystem.Tchar_def",
"typeSystem.Texn_def",
"typeSystem.Tfn_def",
"typeSystem.Tint_def",
"typeSystem.Tlist_def",
"typeSystem.Tref_def",
"typeSystem.Tstring_def",
"typeSystem.Ttup_def",
"typeSystem.Tvector_def",
"typeSystem.Tword64_def",
"typeSystem.Tword8_def",
"typeSystem.Tword8array_def",
"typeSystem.Tdouble_def",
"typeSystem.Treal_def"]
(* ----------- Basic stuff ----------- *)
Theorem unchanged_tenv[simp]:
!(tenv : type_env).
<| v := tenv.v; c := tenv.c; t := tenv.t |> = tenv
Proof
rw [type_env_component_equality]
QED
(*
Theorem union_decls_assoc[simp]:
!decls1 decls2 decls3.
union_decls decls1 (union_decls decls2 decls3)
=
union_decls (union_decls decls1 decls2) decls3
Proof
srw_tac[][] >>
srw_tac[][union_decls_def] >>
metis_tac [UNION_ASSOC]
QED
Theorem union_decls_sym:
!decls1 decls2. union_decls decls1 decls2 = union_decls decls2 decls1
Proof
rw [union_decls_def] >>
rw [UNION_COMM]
QED
Theorem union_decls_mods[simp]:
(union_decls d1 d2).defined_mods = d1.defined_mods ∪ d2.defined_mods
Proof
rw [union_decls_def]
QED
Theorem union_decls_empty[simp]:
!d. union_decls empty_decls d = d ∧ union_decls d empty_decls = d
Proof
rw [union_decls_def, decls_component_equality, empty_decls_def]
QED
*)
Theorem extend_dec_tenv_assoc[simp]:
!tenv1 tenv2 tenv3.
extend_dec_tenv tenv1 (extend_dec_tenv tenv2 tenv3)
=
extend_dec_tenv (extend_dec_tenv tenv1 tenv2) tenv3
Proof
rw [extend_dec_tenv_def]
QED
Theorem tenv_val_ok_nsEmpty[simp]:
tenv_val_ok nsEmpty
Proof
rw [tenv_val_ok_def]
QED
Theorem tenv_ctor_ok_nsEmpty[simp]:
tenv_ctor_ok nsEmpty
Proof
rw [tenv_ctor_ok_def]
QED
Theorem tenv_abbrev_ok_nsEmpty[simp]:
tenv_abbrev_ok nsEmpty
Proof
rw [tenv_abbrev_ok_def]
QED
Theorem tenv_ok_empty[simp]:
tenv_ok <| v := nsEmpty; c := nsEmpty; t := nsEmpty |>
Proof
rw [tenv_ok_def, tenv_val_ok_def, tenv_ctor_ok_def, tenv_abbrev_ok_def]
QED
val type_pes_def = Define `
type_pes tvs tvs' tenv tenvE pes t1 t2 ⇔
(∀(p,e)::set pes.
∃bindings.
ALL_DISTINCT (pat_bindings p []) ∧
type_p tvs tenv p t1 bindings ∧
type_e tenv (bind_var_list tvs' bindings tenvE) e t2)`;
Theorem type_pes_cons:
!tvs tvs' tenv tenvE p e pes t1 t2.
type_pes tvs tvs' tenv tenvE ((p,e)::pes) t1 t2 ⇔
(ALL_DISTINCT (pat_bindings p []) ∧
(?bindings.
type_p tvs tenv p t1 bindings ∧
type_e tenv (bind_var_list tvs' bindings tenvE) e t2) ∧
type_pes tvs tvs' tenv tenvE pes t1 t2)
Proof
rw [type_pes_def, RES_FORALL] >>
eq_tac >>
rw [] >>
rw []
>- (
pop_assum (qspec_then `(p,e)` mp_tac)
>> rw [])
>- (
pop_assum (qspec_then `(p,e)` mp_tac)
>> rw []
>> metis_tac [])
>> metis_tac []
QED
(* ---------- check_freevars ---------- *)
Theorem check_freevars_add:
(!tvs tvs' t. check_freevars tvs tvs' t ⇒
!tvs''. tvs'' ≥ tvs ⇒ check_freevars tvs'' tvs' t)
Proof
ho_match_mp_tac check_freevars_ind >>
srw_tac[][check_freevars_def] >-
metis_tac [MEM_EL, EVERY_MEM] >>
decide_tac
QED
(* ---------- type_subst ---------- *)
Theorem check_freevars_subst_single:
!dbmax tvs t tvs' ts.
LENGTH tvs = LENGTH ts ∧
check_freevars dbmax tvs t ∧
EVERY (check_freevars dbmax tvs') ts
⇒
check_freevars dbmax tvs' (type_subst (alist_to_fmap (ZIP (tvs,ts))) t)
Proof
recInduct check_freevars_ind >>
srw_tac[][check_freevars_def, type_subst_def, EVERY_MAP]
>- (every_case_tac >>
full_simp_tac(srw_ss())[check_freevars_def, ALOOKUP_FAILS]
>- (imp_res_tac MEM_ZIP >>
full_simp_tac(srw_ss())[MEM_EL] >>
metis_tac [])
>- (imp_res_tac ALOOKUP_MEM >>
imp_res_tac MEM_ZIP >>
full_simp_tac(srw_ss())[MEM_EL, EVERY_MEM] >>
srw_tac[][] >>
metis_tac []))
>- full_simp_tac(srw_ss())[EVERY_MEM]
QED
Theorem check_freevars_subst_list:
!dbmax tvs tvs' ts ts'.
(LENGTH tvs = LENGTH ts) ∧
EVERY (check_freevars dbmax tvs) ts' ∧
EVERY (check_freevars dbmax tvs') ts
⇒
EVERY (check_freevars dbmax tvs') (MAP (type_subst (alist_to_fmap (ZIP (tvs,ts)))) ts')
Proof
induct_on `ts'` >>
srw_tac[][] >>
metis_tac [check_freevars_subst_single]
QED
(* ---------- deBruijn_inc ---------- *)
Theorem deBruijn_inc0:
(!t sk. deBruijn_inc sk 0 t = t) ∧
(!ts sk. MAP (deBruijn_inc sk 0) ts = ts)
Proof
ho_match_mp_tac t_induction >>
srw_tac[][deBruijn_inc_def] >>
metis_tac []
QED
Theorem deBruijn_inc_deBruijn_inc:
!sk i2 t i1.
deBruijn_inc sk i1 (deBruijn_inc sk i2 t) = deBruijn_inc sk (i1 + i2) t
Proof
ho_match_mp_tac deBruijn_inc_ind >>
srw_tac[][deBruijn_inc_def] >>
srw_tac[][] >-
decide_tac >-
decide_tac >>
induct_on `ts` >>
full_simp_tac(srw_ss())[]
QED
val deBuijn_inc_lem1 = Q.prove (
`!sk i2 t i1.
deBruijn_inc sk i1 (deBruijn_inc 0 (sk + i2) t) = deBruijn_inc 0 (i1 + (sk + i2)) t`,
ho_match_mp_tac deBruijn_inc_ind >>
srw_tac[][deBruijn_inc_def] >>
srw_tac[][] >-
decide_tac >-
decide_tac >>
induct_on `ts` >>
srw_tac[][]);
val type_subst_deBruijn_inc_single = Q.prove (
`!s t ts tvs inc sk.
(LENGTH tvs = LENGTH ts) ∧
(s = alist_to_fmap (ZIP (tvs,ts))) ∧
check_freevars 0 tvs t ⇒
(deBruijn_inc sk inc (type_subst s t) =
type_subst (alist_to_fmap (ZIP (tvs, MAP (\t. deBruijn_inc sk inc t) ts))) t)`,
recInduct type_subst_ind >>
srw_tac[][deBruijn_inc_def, type_subst_def, check_freevars_def]
>- (every_case_tac >>
full_simp_tac(srw_ss())[deBruijn_inc_def, ALOOKUP_NONE]
>- (imp_res_tac MEM_ZIP >>
full_simp_tac(srw_ss())[MEM_MAP, MEM_ZIP, MEM_EL] >>
metis_tac [FST, pair_CASES])
>- (imp_res_tac ALOOKUP_MEM >>
ntac 2 (pop_assum mp_tac) >>
simp [MEM_MAP, MEM_ZIP, MEM_EL, EL_MAP] >>
metis_tac [FST])
>- (pop_assum mp_tac >>
simp [ALOOKUP_ZIP_MAP_SND]))
>- (srw_tac[][rich_listTheory.MAP_EQ_f, MAP_MAP_o] >>
full_simp_tac(srw_ss())[EVERY_MEM] >>
metis_tac []));
Theorem type_subst_deBruijn_inc_list:
!ts' ts tvs inc sk.
(LENGTH tvs = LENGTH ts) ∧
EVERY (check_freevars 0 tvs) ts' ⇒
(MAP (deBruijn_inc sk inc) (MAP (type_subst (alist_to_fmap (ZIP (tvs,ts)))) ts') =
MAP (type_subst (alist_to_fmap (ZIP (tvs, MAP (\t. deBruijn_inc sk inc t) ts)))) ts')
Proof
induct_on `ts'` >>
srw_tac[][] >>
metis_tac [type_subst_deBruijn_inc_single]
QED
val check_freevars_deBruijn_inc = Q.prove (
`!tvs tvs' t. check_freevars tvs tvs' t ⇒
!n n'. check_freevars (n+tvs) tvs' (deBruijn_inc n' n t)`,
ho_match_mp_tac check_freevars_ind >>
srw_tac[][check_freevars_def, deBruijn_inc_def] >>
full_simp_tac(srw_ss())[EVERY_MAP, EVERY_MEM] >>
srw_tac[][check_freevars_def] >>
decide_tac);
Theorem nil_deBruijn_inc:
∀skip tvs t.
(check_freevars skip [] t ∨ check_freevars skip [] (deBruijn_inc skip tvs t))
⇒
(deBruijn_inc skip tvs t = t)
Proof
ho_match_mp_tac deBruijn_inc_ind >>
srw_tac[][deBruijn_inc_def, check_freevars_def] >-
decide_tac >-
(induct_on `ts` >>
srw_tac[][] >>
metis_tac []) >-
(induct_on `ts` >>
srw_tac[][] >>
metis_tac []) >>
metis_tac []
QED
(* ---------- deBruijn_subst ---------- *)
Theorem deBruijn_subst_check_freevars:
!tvs tvs' t ts n.
check_freevars tvs tvs' t ∧
EVERY (check_freevars tvs tvs') ts
⇒
check_freevars tvs tvs' (deBruijn_subst 0 ts t)
Proof
ho_match_mp_tac check_freevars_ind >>
srw_tac[][check_freevars_def, deBruijn_subst_def, EVERY_MAP] >>
full_simp_tac(srw_ss())[EVERY_MEM] >>
full_simp_tac(srw_ss())[MEM_EL] >-
metis_tac [] >>
decide_tac
QED
Theorem deBruijn_subst_check_freevars2:
!tvs tvs' t ts n tvs''.
check_freevars (LENGTH ts) tvs' t ∧
EVERY (check_freevars tvs tvs') ts
⇒
check_freevars tvs tvs' (deBruijn_subst 0 ts t)
Proof
ho_match_mp_tac check_freevars_ind >>
srw_tac[][check_freevars_def, deBruijn_subst_def, EVERY_MAP] >>
full_simp_tac(srw_ss())[EVERY_MEM] >>
full_simp_tac(srw_ss())[MEM_EL] >>
srw_tac[][] >>
metis_tac []
QED
Theorem check_freevars_subst_inc:
∀tvs tvs2 t.
check_freevars tvs tvs2 t ⇒
∀tvs' targs tvs1.
tvs = LENGTH targs + tvs' ∧
EVERY (check_freevars (tvs1 + tvs') tvs2) targs
⇒
check_freevars (tvs1 + tvs') tvs2
(deBruijn_subst 0 targs (deBruijn_inc (LENGTH targs) tvs1 t))
Proof
ho_match_mp_tac check_freevars_ind >>
srw_tac[][check_freevars_def, deBruijn_inc_def, deBruijn_subst_def, EVERY_MAP] >>
full_simp_tac(srw_ss())[EVERY_MEM] >>
cases_on `n < LENGTH targs` >>
srw_tac[][deBruijn_subst_def, check_freevars_def] >>
full_simp_tac(srw_ss())[MEM_EL] >-
metis_tac [] >-
metis_tac [] >>
decide_tac
QED
Theorem check_freevars_subst:
∀tvs tvs2 t.
check_freevars tvs tvs2 t ⇒
∀tvs' targs tvs1.
tvs = LENGTH targs + tvs' ∧
EVERY (check_freevars (tvs1 + tvs') tvs2) targs
⇒
check_freevars (tvs1 + tvs') tvs2 (deBruijn_subst 0 targs t)
Proof
ho_match_mp_tac check_freevars_ind >>
srw_tac[][check_freevars_def, deBruijn_inc_def, deBruijn_subst_def, EVERY_MAP] >>
full_simp_tac(srw_ss())[EVERY_MEM] >>
cases_on `n < LENGTH targs` >>
srw_tac[][deBruijn_subst_def, check_freevars_def] >>
full_simp_tac(srw_ss())[MEM_EL] >-
metis_tac [] >-
decide_tac >>
decide_tac
QED
val type_subst_deBruijn_subst_single = Q.prove (
`!s t tvs tvs' ts ts' inc.
(LENGTH tvs = LENGTH ts) ∧
check_freevars 0 tvs t ∧
(s = alist_to_fmap (ZIP (tvs,ts))) ⇒
(deBruijn_subst inc ts' (type_subst (alist_to_fmap (ZIP (tvs,ts))) t) =
type_subst (alist_to_fmap (ZIP (tvs,MAP (\t. deBruijn_subst inc ts' t) ts))) t)`,
recInduct type_subst_ind >>
srw_tac[][deBruijn_subst_def, deBruijn_inc_def, type_subst_def, check_freevars_def]
>- (every_case_tac >>
full_simp_tac(srw_ss())[deBruijn_subst_def, deBruijn_inc_def] >>
ntac 2 (pop_assum mp_tac) >>
simp [ALOOKUP_ZIP_MAP_SND])
>- (srw_tac[][rich_listTheory.MAP_EQ_f, MAP_MAP_o] >>
full_simp_tac(srw_ss())[EVERY_MEM] >>
metis_tac []));
Theorem type_subst_deBruijn_subst_list:
!t tvs tvs' ts ts' ts'' inc.
(LENGTH tvs = LENGTH ts) ∧
EVERY (check_freevars 0 tvs) ts'' ⇒
(MAP (deBruijn_subst inc ts') (MAP (type_subst (alist_to_fmap (ZIP (tvs,ts)))) ts'') =
MAP (type_subst (alist_to_fmap (ZIP (tvs,MAP (\t. deBruijn_subst inc ts' t) ts)))) ts'')
Proof
induct_on `ts''` >>
srw_tac[][] >>
metis_tac [type_subst_deBruijn_subst_single]
QED
val check_freevars_lem = Q.prove (
`!l tvs' t.
check_freevars l tvs' t ⇒
!targs n1 tvs.
(l = n1 + (LENGTH targs)) ∧
EVERY (check_freevars tvs tvs') targs
⇒
check_freevars (n1 + tvs) tvs'
(deBruijn_subst n1 (MAP (deBruijn_inc 0 n1) targs) t)`,
ho_match_mp_tac check_freevars_ind >>
srw_tac[][deBruijn_inc_def, deBruijn_subst_def, check_freevars_def] >|
[full_simp_tac(srw_ss())[EVERY_MAP, EVERY_MEM] >>
metis_tac [],
srw_tac[][check_freevars_def] >|
[full_simp_tac(srw_ss())[EVERY_MEM, MEM_EL] >>
`n - n1 < LENGTH targs` by decide_tac >>
srw_tac[][EL_MAP] >>
metis_tac [check_freevars_deBruijn_inc, MEM_EL,
arithmeticTheory.ADD_COMM, arithmeticTheory.ADD_ASSOC],
decide_tac,
decide_tac,
decide_tac]]);
Theorem nil_deBruijn_subst:
∀skip tvs t. check_freevars skip [] t ⇒ (deBruijn_subst skip tvs t = t)
Proof
ho_match_mp_tac deBruijn_subst_ind >>
srw_tac[][deBruijn_subst_def, check_freevars_def] >>
induct_on `ts'` >>
srw_tac[][]
QED
Theorem deBruijn_subst2:
(!t sk targs targs' tvs'.
check_freevars (LENGTH targs) [] t ⇒
(deBruijn_subst sk (MAP (deBruijn_inc 0 sk) targs') (deBruijn_subst 0 targs t) =
deBruijn_subst 0 (MAP (deBruijn_subst sk (MAP (deBruijn_inc 0 sk) targs')) targs) t)) ∧
(!ts sk targs targs' tvs'.
EVERY (check_freevars (LENGTH targs) []) ts ⇒
(MAP (deBruijn_subst sk (MAP (deBruijn_inc 0 sk) targs')) (MAP (deBruijn_subst 0 targs) ts) =
(MAP (deBruijn_subst 0 (MAP (deBruijn_subst sk (MAP (deBruijn_inc 0 sk) targs')) targs)) ts)))
Proof
ho_match_mp_tac t_induction >>
srw_tac[][deBruijn_subst_def, deBruijn_inc_def] >>
full_simp_tac(srw_ss())[EL_MAP, MAP_MAP_o, combinTheory.o_DEF] >>
srw_tac[][] >>
full_simp_tac (srw_ss()++ARITH_ss) [deBruijn_subst_def, check_freevars_def] >>
metis_tac []
QED
Theorem type_e_subst_lem3:
∀tvs tvs2 t.
check_freevars tvs tvs2 t ⇒
∀tvs' targs n.
(tvs = n + LENGTH targs) ∧
EVERY (check_freevars tvs' tvs2) targs
⇒
check_freevars (n + tvs') tvs2
(deBruijn_subst n (MAP (deBruijn_inc 0 n) targs) t)
Proof
ho_match_mp_tac check_freevars_ind >>
srw_tac[][check_freevars_def, deBruijn_inc_def, deBruijn_subst_def, EVERY_MAP] >>
full_simp_tac(srw_ss())[EVERY_MEM] >>
srw_tac[][] >>
full_simp_tac (srw_ss()++ARITH_ss) [check_freevars_def, EL_MAP, MEM_EL] >>
`n - n' < LENGTH targs` by decide_tac >>
metis_tac [check_freevars_deBruijn_inc]
QED
val type_e_subst_lem5 = Q.prove (
`(!t n inc n' targs.
deBruijn_inc n inc
(deBruijn_subst (n + n') (MAP (deBruijn_inc 0 (n + n')) targs) t) =
deBruijn_subst (n + inc + n') (MAP (deBruijn_inc 0 (n + inc + n')) targs)
(deBruijn_inc n inc t)) ∧
(!ts n inc n' targs.
MAP (deBruijn_inc n inc)
(MAP (deBruijn_subst (n + n') (MAP (deBruijn_inc 0 (n + n')) targs)) ts) =
MAP (deBruijn_subst (n + inc + n') (MAP (deBruijn_inc 0 (n + inc + n')) targs))
(MAP (deBruijn_inc n inc) ts))`,
ho_match_mp_tac t_induction >>
srw_tac[][deBruijn_subst_def, deBruijn_inc_def] >>
srw_tac[][] >>
full_simp_tac (srw_ss()++ARITH_ss) [EL_MAP] >>
metis_tac [deBuijn_inc_lem1]);
Theorem subst_inc_cancel:
(!t ts inc.
deBruijn_subst 0 ts (deBruijn_inc 0 (inc + LENGTH ts) t)
=
deBruijn_inc 0 inc t) ∧
(!ts' ts inc.
MAP (deBruijn_subst 0 ts) (MAP (deBruijn_inc 0 (inc + LENGTH ts)) ts')
=
MAP (deBruijn_inc 0 inc) ts')
Proof
ho_match_mp_tac t_induction >>
srw_tac[][deBruijn_subst_def, deBruijn_inc_def] >>
full_simp_tac (srw_ss()++ARITH_ss) [] >>
metis_tac []
QED
val type_e_subst_lem7 = Q.prove (
`(!t sk targs targs' tvs' tvs''.
(deBruijn_subst sk (MAP (deBruijn_inc 0 sk) targs') (deBruijn_subst 0 targs t) =
deBruijn_subst 0 (MAP (deBruijn_subst sk (MAP (deBruijn_inc 0 sk) targs')) targs)
(deBruijn_subst (LENGTH targs + sk) (MAP (deBruijn_inc 0 (LENGTH targs + sk)) targs') t))) ∧
(!ts sk targs targs' tvs' tvs''.
(MAP (deBruijn_subst sk (MAP (deBruijn_inc 0 sk) targs')) (MAP (deBruijn_subst 0 targs) ts) =
(MAP (deBruijn_subst 0 (MAP (deBruijn_subst sk (MAP (deBruijn_inc 0 sk) targs')) targs))
(MAP (deBruijn_subst (LENGTH targs + sk) (MAP (deBruijn_inc 0 (LENGTH targs + sk)) targs')) ts))))`,
ho_match_mp_tac t_induction >>
srw_tac[][deBruijn_subst_def, deBruijn_inc_def] >>
full_simp_tac(srw_ss())[EL_MAP, MAP_MAP_o, combinTheory.o_DEF] >>
srw_tac[][] >>
full_simp_tac (srw_ss()++ARITH_ss) [EL_MAP, deBruijn_subst_def, check_freevars_def] >>
rw[] >> fs[] >>
metis_tac [subst_inc_cancel, LENGTH_MAP]);
Theorem deBruijn_subst_id:
(!t n. check_freevars n [] t ⇒ (deBruijn_subst 0 (MAP Tvar_db (COUNT_LIST n)) t = t)) ∧
(!ts n. EVERY (check_freevars n []) ts ⇒ (MAP (deBruijn_subst 0 (MAP Tvar_db (COUNT_LIST n))) ts = ts))
Proof
Induct >>
srw_tac[][deBruijn_subst_def, LENGTH_COUNT_LIST, EL_MAP, EL_COUNT_LIST,
check_freevars_def] >>
metis_tac []
QED
Theorem deBruijn_subst_freevars:
!skip targs t tvs.
skip = 0 ∧
EVERY (check_freevars tvs []) targs ∧
check_freevars (LENGTH targs) [] t
⇒
check_freevars tvs [] (deBruijn_subst skip targs t)
Proof
ho_match_mp_tac deBruijn_subst_ind >>
srw_tac[][check_freevars_def, deBruijn_subst_def, EVERY_MAP] >>
full_simp_tac(srw_ss())[EVERY_MEM, MEM_EL] >>
metis_tac []
QED
(* ---------- tenv_abbrev stuff ---------- *)
Theorem tenv_abbrev_ok_lookup:
!tenvT tn tvs t.
tenv_abbrev_ok tenvT ∧
nsLookup tenvT tn = SOME (tvs,t)
⇒
check_freevars 0 tvs t
Proof
rw [tenv_abbrev_ok_def]
>> drule nsLookup_nsAll
>> disch_then drule
>> simp []
QED
Theorem check_freevars_type_name_subst:
!tvs t dbmax tenvT.
tenv_abbrev_ok tenvT ∧
check_type_names tenvT t ∧
check_freevars_ast tvs t
⇒
check_freevars dbmax tvs (type_name_subst tenvT t)
Proof
recInduct check_freevars_ast_ind >>
srw_tac[][type_name_subst_def, LET_THM] >>
every_case_tac >>
fs [check_type_names_def, check_freevars_def, check_freevars_ast_def, EVERY_MAP] >>
full_simp_tac(srw_ss())[EVERY_MEM] >>
match_mp_tac check_freevars_subst_single >>
srw_tac[][EVERY_MAP] >>
srw_tac[][EVERY_MEM] >>
imp_res_tac tenv_abbrev_ok_lookup >>
metis_tac [check_freevars_add, numeralTheory.numeral_distrib]
QED
Theorem tenv_abbrev_ok_merge:
!tenvT1 tenvT2.
tenv_abbrev_ok tenvT1 ∧ tenv_abbrev_ok tenvT2
⇒
tenv_abbrev_ok (nsAppend tenvT1 tenvT2)
Proof
rw [tenv_abbrev_ok_def]
>> irule nsAll_nsAppend
>> rw []
QED
(* ---------- tenv_ctor stuff ----------*)
Theorem type_ctor_long:
!ctMap mn id. type_ctor ctMap (Long mn id) = type_ctor ctMap id
Proof
rw [FUN_EQ_THM]
>> PairCases_on `x`
>> PairCases_on `x'`
>> simp [type_ctor_def, id_to_n_def]
QED
Theorem tenv_ctor_ok_merge[simp]:
!tenvC1 tenvC2.
tenv_ctor_ok tenvC1 ∧ tenv_ctor_ok tenvC2
⇒
tenv_ctor_ok (nsAppend tenvC1 tenvC2)
Proof
rw [tenv_ctor_ok_def]
>> irule nsAll_nsAppend
>> rw []
QED
Theorem tenv_ctor_ok_lookup:
!tenvC cn tvs ts tn.
tenv_ctor_ok tenvC ∧ nsLookup tenvC cn = SOME (tvs,ts,tn)
⇒
EVERY (check_freevars 0 tvs) ts
Proof
rw [tenv_ctor_ok_def]
>> drule nsLookup_nsAll
>> disch_then drule
>> simp []
QED
(* ---------- tenv_val_exp stuff ---------- *)
val deBruijn_subst_tenvE_def = Define `
(deBruijn_subst_tenvE targs Empty = Empty) ∧
(deBruijn_subst_tenvE targs (Bind_tvar tvs env) =
Bind_tvar tvs (deBruijn_subst_tenvE targs env)) ∧
(deBruijn_subst_tenvE targs (Bind_name x tvs t env) =
Bind_name x tvs (deBruijn_subst (tvs + num_tvs env)
(MAP (deBruijn_inc 0 (tvs + num_tvs env)) targs)
t)
(deBruijn_subst_tenvE targs env))`;
val db_merge_def = Define `
(db_merge Empty e = e) ∧
(db_merge (Bind_tvar tvs e1) e2 = Bind_tvar tvs (db_merge e1 e2)) ∧
(db_merge (Bind_name x tvs t e1) e2 = Bind_name x tvs t (db_merge e1 e2))`;
Theorem bind_tvar_rewrites[simp]:
(!tvs e1 e2. db_merge (bind_tvar tvs e1) e2 = bind_tvar tvs (db_merge e1 e2)) ∧
(!tvs e. num_tvs (bind_tvar tvs e) = tvs + num_tvs e) ∧
(!tvs e. num_tvs (Bind_tvar tvs e) = tvs + num_tvs e) ∧
(!tvs n t e. num_tvs (Bind_name n tvs t e) = num_tvs e) ∧
(!tvs e. num_tvs Empty = 0) ∧
(!n inc tvs e. tveLookup n inc (bind_tvar tvs e) = tveLookup n (inc+tvs) e) ∧
(!tvs e. tenv_val_exp_ok (bind_tvar tvs e) ⇔ tenv_val_exp_ok e) ∧
(!targs tvs e.
deBruijn_subst_tenvE targs (bind_tvar tvs e) =
bind_tvar tvs (deBruijn_subst_tenvE targs e))
Proof
srw_tac[][bind_tvar_def, deBruijn_subst_tenvE_def, db_merge_def, num_tvs_def,
tveLookup_def, tenv_val_exp_ok_def]
QED
Theorem bind_tvar0[simp]:
!x. bind_tvar 0 x = x
Proof
Cases_on `x`
>> rw [bind_tvar_def]
QED
Theorem tveLookup_subst_none:
!n inc e.
tveLookup n inc (deBruijn_subst_tenvE targs e) = NONE ⇔
tveLookup n inc e = NONE
Proof
induct_on `e` >>
srw_tac[][deBruijn_subst_tenvE_def, tveLookup_def]
QED
Theorem tveLookup_db_merge_none:
!n inc e1 e2.
tveLookup n inc (db_merge e1 e2) = NONE
⇔
tveLookup n inc e1 = NONE ∧ tveLookup n (num_tvs e1 + inc) e2 = NONE
Proof
Induct_on `e1`
>> rw [tveLookup_def, db_merge_def]
QED
Theorem tveLookup_inc_none:
!n inc e.
tveLookup n inc e = NONE
⇔
tveLookup n 0 e = NONE
Proof
Induct_on `e`
>> rw [tveLookup_def]
QED
Theorem tveLookup_freevars:
!n tvs tenvE tvs' t.
tenv_val_exp_ok tenvE ∧
num_tvs tenvE = 0 ∧
tveLookup n tvs tenvE = SOME (tvs',t)
⇒
check_freevars tvs' [] t
Proof
Induct_on `tenvE`
>> rw [tveLookup_def, tenv_val_exp_ok_def]
>> fs []
>> metis_tac [nil_deBruijn_inc]
QED
Theorem tveLookup_bvl:
!x tvs tvs' bindings tenvE.
tveLookup x tvs (bind_var_list tvs' bindings tenvE)
=
case ALOOKUP bindings x of
| SOME t => SOME (tvs',deBruijn_inc tvs' tvs t)
| NONE => tveLookup x tvs tenvE
Proof
Induct_on `bindings`
>> rw [bind_var_list_def]
>> PairCases_on `h`
>> rw [bind_var_list_def, tveLookup_def]
QED
Theorem bind_var_list_append:
!n te1 te2 te3.
bind_var_list n (te1++te2) te3 = bind_var_list n te1 (bind_var_list n te2 te3)
Proof
induct_on `te1` >>
srw_tac[][bind_var_list_def] >>
PairCases_on `h` >>
srw_tac[][bind_var_list_def]
QED
Theorem num_tvs_bind_var_list[simp]:
!tvs env tenvE. num_tvs (bind_var_list tvs env tenvE) = num_tvs tenvE
Proof
induct_on `env` >>
srw_tac[][num_tvs_def, bind_var_list_def] >>
PairCases_on `h` >>
srw_tac[][bind_var_list_def, num_tvs_def]
QED
Theorem tenv_val_exp_ok_bvl:
!tenvE env.
tenv_val_exp_ok tenvE ∧ EVERY (check_freevars (num_tvs tenvE) []) (MAP SND env)
⇒
tenv_val_exp_ok (bind_var_list 0 env tenvE)
Proof
Induct_on `env` >>
srw_tac[][tenv_val_exp_ok_def, bind_var_list_def] >>
PairCases_on `h` >>
srw_tac[][tenv_val_exp_ok_def, bind_var_list_def]
>> fs []
QED
Theorem tveLookup_subst_some:
∀n e targs tvs t inc.
tveLookup n inc e = SOME (tvs,t)
⇒
tveLookup n inc (deBruijn_subst_tenvE targs e) =
SOME (tvs, deBruijn_subst (tvs+inc+num_tvs e) (MAP (deBruijn_inc 0 (tvs+inc+num_tvs e)) targs) t)
Proof
induct_on `e` >>
srw_tac[][tveLookup_def,deBruijn_subst_tenvE_def, deBruijn_inc_def, num_tvs_def, type_e_subst_lem5]
>> metis_tac [arithmeticTheory.ADD_ASSOC]
QED
Theorem num_tvs_db_merge[simp]:
!e1 e2. num_tvs (db_merge e1 e2) = num_tvs e1 + num_tvs e2
Proof
induct_on `e1` >>
srw_tac[][num_tvs_def, db_merge_def] >>
decide_tac
QED
Theorem num_tvs_deBruijn_subst_tenvE[simp]:
!targs tenvE. num_tvs (deBruijn_subst_tenvE targs tenvE) = num_tvs tenvE
Proof
induct_on `tenvE` >>
srw_tac[][deBruijn_subst_tenvE_def, num_tvs_def]
QED
Theorem tveLookup_inc_some:
!n inc e tvs t inc2.
tveLookup n inc e = SOME (tvs, t)
⇒
?t'. (t = deBruijn_inc tvs inc t') ∧
(tveLookup n inc2 e = SOME (tvs, deBruijn_inc tvs inc2 t'))
Proof
induct_on `e` >>
srw_tac[][deBruijn_inc_def, tveLookup_def] >>
srw_tac[][] >>
metis_tac [deBruijn_inc_deBruijn_inc]
QED
Theorem tveLookup_add_inc:
!x inc tenv tvs t inc2.
(tveLookup x inc tenv = SOME (tvs,t))
⇒
(tveLookup x (inc2 + inc) tenv = SOME (tvs, deBruijn_inc tvs inc2 t))
Proof
induct_on `tenv` >>
srw_tac[][tveLookup_def] >>
srw_tac[][deBruijn_inc_deBruijn_inc] >>
metis_tac [arithmeticTheory.ADD_ASSOC]
QED
Theorem tveLookup_freevars_subst:
!tenvE targs n t inc.
EVERY (check_freevars (inc + num_tvs tenvE) []) targs ∧
tveLookup n inc tenvE = SOME (LENGTH targs,t) ∧
tenv_val_exp_ok tenvE
⇒
check_freevars (inc + num_tvs tenvE) [] (deBruijn_subst 0 targs t)
Proof
induct_on `tenvE` >>
rw [check_freevars_def, num_tvs_def, tveLookup_def, tenv_val_exp_ok_def] >>
metis_tac [deBruijn_subst_check_freevars, arithmeticTheory.ADD_ASSOC,
check_freevars_subst_inc]
QED
Theorem tenv_val_exp_ok_db_merge:
!e1 e2. tenv_val_exp_ok (db_merge e1 e2) ⇒ tenv_val_exp_ok e2
Proof
induct_on `e1` >>
srw_tac[][tenv_val_exp_ok_def, db_merge_def]
QED
val tveLookup_freevars = Q.prove (
`!e n inc t tvs.
tenv_val_exp_ok e ∧
tveLookup n inc e = SOME (tvs, t)
⇒
check_freevars (tvs+inc+num_tvs e) [] t`,
induct_on `e` >>
srw_tac[][tveLookup_def, num_tvs_def, tenv_val_exp_ok_def] >|
[metis_tac [arithmeticTheory.ADD_ASSOC],
imp_res_tac check_freevars_deBruijn_inc >>
metis_tac [arithmeticTheory.ADD_ASSOC, arithmeticTheory.ADD_COMM],
metis_tac []]);
Theorem tveLookup_no_tvs:
!tvs l tenv n t.
tenv_val_exp_ok tenv ∧
num_tvs tenv = 0
⇒
(tveLookup n tvs tenv = SOME (l,t)
⇔
tveLookup n 0 tenv = SOME (l,t))
Proof
induct_on `tenv` >>
srw_tac[][tveLookup_def, num_tvs_def, tenv_val_exp_ok_def] >>
eq_tac >>
srw_tac[][] >>
full_simp_tac(srw_ss())[] >>
metis_tac [nil_deBruijn_inc, deBruijn_inc0]
QED
Theorem deBruijn_subst_E_bvl:
!tenv1 tenv2 tvs.
deBruijn_subst_tenvE targs (bind_var_list tvs tenv1 tenv2)
=
bind_var_list tvs
(MAP (\(x,t). (x, deBruijn_subst (tvs + num_tvs tenv2) (MAP (deBruijn_inc 0 (tvs + num_tvs tenv2)) targs) t)) tenv1)
(deBruijn_subst_tenvE targs tenv2)
Proof
induct_on `tenv1` >>
srw_tac[][bind_var_list_def] >>
PairCases_on `h` >>
srw_tac[][bind_var_list_def, deBruijn_subst_tenvE_def]
QED
Theorem db_merge_bvl:
!tenv1 tenv2 tenv3 tvs.
db_merge (bind_var_list tvs tenv1 tenv2) tenv3
=
bind_var_list tvs tenv1 (db_merge tenv2 tenv3)
Proof
induct_on `tenv1` >>
srw_tac[][bind_var_list_def] >>
PairCases_on `h` >>
srw_tac[][bind_var_list_def, db_merge_def]
QED
Theorem tveLookup_db_merge_some:
!n inc tenvE1 tenvE2 tvs t.
tveLookup n inc (db_merge tenvE1 tenvE2) = SOME (tvs,t)
⇔
tveLookup n inc tenvE1 = SOME (tvs,t) ∨
(tveLookup n inc tenvE1 = NONE ∧
tveLookup n (num_tvs tenvE1 + inc) tenvE2 = SOME (tvs, t))
Proof
Induct_on `tenvE1`
>> rw [db_merge_def, tveLookup_def]
QED
(* ---------- type_op ---------- *)
val op_thms = { nchotomy = op_nchotomy, case_def = op_case_def };
val list_thms = { nchotomy = list_nchotomy, case_def = list_case_def };
val t_thms = { nchotomy = t_nchotomy, case_def = t_case_def };
val word_size_thms = { nchotomy = word_size_nchotomy, case_def = word_size_case_def };
val id_thms = { nchotomy = id_nchotomy, case_def = id_case_def };
val thms = [ op_thms, list_thms, t_thms, word_size_thms, id_thms ];
val eqs = ([pair_case_eq,bool_case_eq]@(List.map prove_case_eq_thm thms));
val elims = List.map prove_case_elim_thm thms;
val type_op_cases = save_thm("type_op_cases",
``type_op op ts t3``
|> (SIMP_CONV(srw_ss())(type_op_def::eqs@elims) THENC
SIMP_CONV (bool_ss++DNF_ss) [
PULL_EXISTS]));
(* ---------- type_p ---------- *)
Theorem type_ps_length:
∀tvs tenvC ps ts tenv.
type_ps tvs tenvC ps ts tenv ⇒ (LENGTH ps = LENGTH ts)
Proof
induct_on `ps` >>
srw_tac[][Once type_p_cases] >>
srw_tac[][] >>
metis_tac []
QED
Theorem type_p_freevars:
(!tvs tenvC p t env'.
type_p tvs tenvC p t env' ⇒
check_freevars tvs [] t ∧
EVERY (check_freevars tvs []) (MAP SND env')) ∧
(!tvs tenvC ps ts env'.
type_ps tvs tenvC ps ts env' ⇒
EVERY (check_freevars tvs []) ts ∧
EVERY (check_freevars tvs []) (MAP SND env'))
Proof
ho_match_mp_tac type_p_ind >>
srw_tac[][check_freevars_def, bind_tvar_def, bind_var_list_def] >>
metis_tac []
QED
Theorem type_p_subst:
(!n tenv p t new_bindings. type_p n tenv p t new_bindings ⇒
!targs' inc tvs targs.
tenv_abbrev_ok tenv.t ∧
tenv_ctor_ok tenv.c ∧
(n = inc + LENGTH targs) ∧
EVERY (check_freevars tvs []) targs ∧
(targs' = MAP (deBruijn_inc 0 inc) targs)
⇒
type_p (inc + tvs) tenv
p
(deBruijn_subst inc targs' t)
(MAP (\(x,t). (x, deBruijn_subst inc targs' t)) new_bindings)) ∧
(!n tenv ps ts new_bindings. type_ps n tenv ps ts new_bindings ⇒
!targs' inc targs tvs.
tenv_abbrev_ok tenv.t ∧
tenv_ctor_ok tenv.c ∧
(n = inc + LENGTH targs) ∧
EVERY (check_freevars tvs []) targs ∧
(targs' = MAP (deBruijn_inc 0 inc) targs)
⇒
type_ps (inc + tvs) tenv
ps
(MAP (deBruijn_subst inc targs') ts)
(MAP (\(x,t). (x, deBruijn_subst inc targs' t)) new_bindings))
Proof
ho_match_mp_tac type_p_strongind >>
srw_tac[][] >>
ONCE_REWRITE_TAC [type_p_cases] >>
simp [deBruijn_subst_def, OPTION_MAP_DEF]
>- metis_tac [check_freevars_lem]
>- metis_tac [check_freevars_lem]
>- (rw [] >>
srw_tac[][EVERY_MAP] >>
full_simp_tac(srw_ss())[EVERY_MEM] >>
srw_tac[][]
>- metis_tac [check_freevars_lem, EVERY_MEM]
>- metis_tac [type_subst_deBruijn_subst_list, tenv_ctor_ok_lookup])
>- metis_tac []
>- (conj_asm1_tac
>- (match_mp_tac nil_deBruijn_subst >>
match_mp_tac check_freevars_type_name_subst >>
`! n:num . n ≥ 0` by decide_tac >>
rw []) >>
metis_tac [])
>- metis_tac []
QED
Theorem type_p_bvl:
(!tvs tenvC p t bindings. type_p tvs tenvC p t bindings ⇒
!tenv'. tenv_val_exp_ok tenv' ⇒ tenv_val_exp_ok (bind_var_list tvs bindings tenv')) ∧
(!tvs tenvC ps ts bindings. type_ps tvs tenvC ps ts bindings ⇒
!tenv'. tenv_val_exp_ok tenv' ⇒ tenv_val_exp_ok (bind_var_list tvs bindings tenv'))
Proof
ho_match_mp_tac type_p_strongind >>
srw_tac[][bind_var_list_def, tenv_val_exp_ok_def, num_tvs_def, bind_var_list_append]
>- (
`tvs + num_tvs tenv' ≥ tvs` by decide_tac >>
metis_tac [check_freevars_add])
>>
first_x_assum match_mp_tac>>simp[tenv_val_exp_ok_def]>>
imp_res_tac type_p_freevars>>
`tvs + num_tvs tenv' ≥ tvs` by decide_tac >>
metis_tac [check_freevars_add]
QED
Theorem type_p_tenvV_indep:
(!p tvs tenv t bindings tenvV.