-
Notifications
You must be signed in to change notification settings - Fork 4
/
pure_typingPropsScript.sml
1962 lines (1877 loc) · 72.5 KB
/
pure_typingPropsScript.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 Parse boolLib bossLib BasicProvers dep_rewrite;
open pairTheory arithmeticTheory integerTheory stringTheory optionTheory
listTheory rich_listTheory alistTheory pred_setTheory finite_mapTheory;
open pure_miscTheory pure_cexpTheory pure_tcexpTheory pure_configTheory
pure_typingTheory pure_tcexp_lemmasTheory
val _ = new_theory "pure_typingProps";
(******************** Basic lemmas ********************)
Theorem type_ind:
∀P.
(∀n. P (TypeVar n)) ∧ (∀p. P (PrimTy p)) ∧ P Exception ∧
(∀l. (∀t. MEM t l ⇒ P t) ⇒ ∀n. P (TypeCons n l)) ∧
(∀l. (∀t. MEM t l ⇒ P t) ⇒ P (Tuple l)) ∧
(∀tf t. P tf ∧ P t ⇒ P (Function tf t)) ∧
(∀t. P t ⇒ P (Array t)) ∧ (∀t. P t ⇒ P (M t)) ⇒
(∀t. P t)
Proof
ntac 3 strip_tac >>
completeInduct_on `type_size t` >> rw[] >>
Cases_on `t` >> gvs[type_size_def] >>
last_x_assum irule >> rw[] >>
first_x_assum irule >> simp[] >>
Induct_on `l` >> rw[] >> gvs[type_size_def]
QED
Theorem type_atom_op_not_Loc:
type_atom_op op ts t ⇒ ∀n. op ≠ Lit $ Loc n
Proof
rw[type_atom_op_cases, type_lit_cases]
QED
Theorem type_atom_op_no_Bool:
type_atom_op op ts t ⇒ ¬ MEM Bool ts
Proof
rw[type_atom_op_cases] >> gvs[] >> Induct_on `ts` >> gvs[]
QED
Theorem get_PrimTys_SOME:
∀ts pts.
get_PrimTys ts = SOME pts ⇔ ts = MAP PrimTy pts
Proof
Induct >> rw[get_PrimTys_def] >>
Cases_on `h` >> gvs[get_PrimTys_def] >>
Cases_on `pts` >> gvs[] >> eq_tac >> rw[]
QED
Theorem Functions_APPEND:
∀as bs a.
Functions (as ++ bs) a = Functions as (Functions bs a)
Proof
Induct >> rw[Functions_def]
QED
Theorem Functions_eq_imp:
∀as a bs b.
Functions as a = Functions bs b ⇒
∃cs.
(as = bs ++ cs ∧ b = Functions cs a) ∨
(bs = as ++ cs ∧ a = Functions cs b)
Proof
Induct >> rw[Functions_def] >> csimp[Functions_def]
>- (qexists_tac `bs` >> simp[]) >>
Cases_on `bs` >> gvs[Functions_def]
QED
Theorem FINITE_reserved_cns[simp]:
FINITE reserved_cns
Proof
rw[reserved_cns_def]
QED
Theorem type_exception_Subscript:
namespace_ok ns ⇒ type_exception (FST ns) («Subscript», [])
Proof
PairCases_on `ns` >> rw[type_exception_def, namespace_ok_def] >>
gvs[ALL_DISTINCT_APPEND] >> drule_all ALOOKUP_ALL_DISTINCT_MEM >> simp[]
QED
Theorem cns_arities_ok_simps[simp]:
cns_arities_ok ns {} ∧
cns_arities_ok ns (a INSERT b) = (
((∃ar. a = {(«»,ar)}) ∨ (∃a'. a' ∈ ns_cns_arities ns ∧ a ⊆ a')) ∧
cns_arities_ok ns b) ∧
cns_arities_ok ns (x ∪ y) = (cns_arities_ok ns x ∧ cns_arities_ok ns y) ∧
cns_arities_ok ns (BIGUNION s) = (∀x. x ∈ s ⇒ cns_arities_ok ns x)
Proof
rw[cns_arities_ok_def] >> metis_tac[]
QED
(******************** Substitutions and shifts ********************)
Theorem shift_db_0[simp]:
∀skip. shift_db skip 0 = I
Proof
qsuff_tac `∀skip n t. n = 0 ⇒ shift_db skip n t = t` >- rw[FUN_EQ_THM] >>
recInduct shift_db_ind >> rw[shift_db_def] >>
rw[LIST_EQ_REWRITE] >> gvs[MEM_EL, PULL_EXISTS, EL_MAP]
QED
Theorem subst_db_NIL[simp]:
∀n. subst_db n [] = I
Proof
qsuff_tac `∀n ts t. ts = [] ⇒ subst_db n ts t = t` >- rw[FUN_EQ_THM] >>
recInduct subst_db_ind >> rw[subst_db_def] >>
rw[LIST_EQ_REWRITE] >> gvs[MEM_EL, PULL_EXISTS, EL_MAP]
QED
Theorem subst_db_unchanged:
∀skip ts t n.
freetyvars_ok n t ∧
n ≤ skip
⇒ subst_db skip ts t = t
Proof
recInduct subst_db_ind >> reverse $ rw[subst_db_def, freetyvars_ok_def]
>- metis_tac[] >- metis_tac[] >>
irule MAP_ID_ON >> gvs[EVERY_MEM] >> metis_tac[]
QED
Theorem shift_db_unchanged:
∀skip shift t n.
freetyvars_ok n t ∧
n ≤ skip
⇒ shift_db skip shift t = t
Proof
recInduct shift_db_ind >> reverse $ rw[shift_db_def, freetyvars_ok_def]
>- metis_tac[] >- metis_tac[] >>
irule MAP_ID_ON >> gvs[EVERY_MEM] >> metis_tac[]
QED
Theorem subst_db_shift_db_unchanged:
∀skip shift t ts m.
(m - skip) + LENGTH ts ≤ shift ∧
skip ≤ m
⇒ subst_db m ts (shift_db skip shift t) =
shift_db skip (shift - LENGTH ts) t
Proof
recInduct shift_db_ind >> rw[subst_db_def, shift_db_def] >>
rw[MAP_MAP_o, combinTheory.o_DEF, MAP_EQ_f]
QED
Theorem tsubst_tshift:
∀shift t ts m.
LENGTH ts ≤ shift
⇒ tsubst ts (tshift shift t) =
tshift (shift - LENGTH ts) t
Proof
rw[] >> irule subst_db_shift_db_unchanged >> simp[]
QED
Theorem subst_db_subst_db:
∀n tsn t m tsm.
n ≤ m
⇒ subst_db m tsm (subst_db n tsn t) =
subst_db n (MAP (subst_db m tsm) tsn)
(subst_db (m + LENGTH tsn) (MAP (shift_db n (LENGTH tsn)) tsm) t)
Proof
recInduct subst_db_ind >> rw[subst_db_def, EL_MAP]
>- gvs[subst_db_shift_db_unchanged] >>
rw[MAP_MAP_o, combinTheory.o_DEF, MAP_EQ_f]
QED
Theorem shift_db_shift_db:
∀m shiftm t n shiftn.
n ≤ m
⇒ shift_db (m + shiftn) shiftm (shift_db n shiftn t) =
shift_db n shiftn (shift_db m shiftm t)
Proof
recInduct shift_db_ind >> rw[shift_db_def] >>
rw[MAP_MAP_o, combinTheory.o_DEF, MAP_EQ_f]
QED
Theorem tshift_tshift:
∀t s1 s2.
tshift s1 (tshift s2 t) = tshift (s1 + s2) t
Proof
ho_match_mp_tac type_ind >> rw[shift_db_def] >>
rw[MAP_MAP_o, combinTheory.o_DEF, MAP_EQ_f]
QED
Theorem subst_db_shift_db_1:
∀n ts t m shift.
m ≤ n
⇒ subst_db (n + shift) (MAP (shift_db m shift) ts) (shift_db m shift t) =
shift_db m shift (subst_db n ts t)
Proof
recInduct subst_db_ind >> rw[shift_db_def, subst_db_def, EL_MAP] >>
rw[MAP_MAP_o, combinTheory.o_DEF, MAP_EQ_f]
QED
Theorem subst_db_shift_db_2:
∀n ts t m shift.
n ≤ m
⇒ subst_db n (MAP (shift_db m shift) ts) (shift_db (m + LENGTH ts) shift t) =
shift_db m shift (subst_db n ts t)
Proof
recInduct subst_db_ind >> rw[shift_db_def, subst_db_def, EL_MAP] >>
rw[MAP_MAP_o, combinTheory.o_DEF, MAP_EQ_f]
QED
Theorem shift_db_twice:
∀k m ty n. shift_db k n (shift_db k m ty) = shift_db k (n + m) ty
Proof
recInduct shift_db_ind >> rw[shift_db_def] >>
gvs[MAP_MAP_o, combinTheory.o_DEF, LAMBDA_PROD, MAP_EQ_f]
QED
(******************** Properties of types ********************)
Theorem freetyvars_ok_mono:
∀n t m.
freetyvars_ok n t ∧
n ≤ m
⇒ freetyvars_ok m t
Proof
recInduct freetyvars_ok_ind >> rw[freetyvars_ok_def] >> gvs[EVERY_MEM]
QED
Theorem freetyvars_ok_subst_db:
∀skip ts t n.
freetyvars_ok (n + LENGTH ts) t ∧
EVERY (freetyvars_ok n) ts ∧
skip ≤ n
⇒ freetyvars_ok n (subst_db skip ts t)
Proof
recInduct subst_db_ind >>
rw[subst_db_def, freetyvars_ok_def] >>
gvs[EVERY_MEM, MEM_MAP, PULL_EXISTS] >>
gvs[MEM_EL, PULL_EXISTS]
QED
Theorem freetyvars_ok_tsubst:
∀ts t n.
freetyvars_ok (n + LENGTH ts) t ∧
EVERY (freetyvars_ok n) ts
⇒ freetyvars_ok n (tsubst ts t)
Proof
rw[] >> irule freetyvars_ok_subst_db >> simp[]
QED
Theorem freetyvars_ok_shift_db:
∀skip shift t n.
freetyvars_ok n t
⇒ freetyvars_ok (n + shift) (shift_db skip shift t)
Proof
recInduct shift_db_ind >>
rw[shift_db_def, freetyvars_ok_def] >>
gvs[EVERY_MEM, MEM_MAP, PULL_EXISTS]
QED
Theorem type_wf_subst_db:
∀skip ts t tdefs.
type_wf tdefs t ∧
EVERY (type_wf tdefs) ts
⇒ type_wf tdefs (subst_db skip ts t)
Proof
recInduct subst_db_ind >> rw[subst_db_def, type_wf_def] >>
gvs[EVERY_MAP, EVERY_MEM] >> gvs[MEM_EL, PULL_EXISTS]
QED
Theorem type_wf_shift_db:
∀skip shift t tdefs.
type_wf tdefs t
⇒ type_wf tdefs (shift_db skip shift t)
Proof
recInduct shift_db_ind >> rw[shift_db_def, type_wf_def] >>
gvs[EVERY_MAP, EVERY_MEM]
QED
Theorem type_ok_mono:
∀tdefs n m t.
type_ok tdefs n t ∧
n ≤ m
⇒ type_ok tdefs m t
Proof
rw[type_ok_def] >> drule_all freetyvars_ok_mono >> simp[]
QED
Theorem type_ok_subst_db:
∀skip ts tdefs n.
type_ok tdefs (n + LENGTH ts) t ∧
EVERY (type_ok tdefs n) ts ∧
skip ≤ n
⇒ type_ok tdefs n (subst_db skip ts t)
Proof
rw[type_ok_def]
>- (irule freetyvars_ok_subst_db >> gvs[EVERY_MEM, type_ok_def])
>- (irule type_wf_subst_db >> gvs[EVERY_MEM, type_ok_def])
QED
Theorem type_ok_shift_db:
∀skip shift tdefs n t.
type_ok tdefs n t
⇒ type_ok tdefs (n + shift) (shift_db skip shift t)
Proof
rw[type_ok_def]
>- (irule freetyvars_ok_shift_db >> gvs[EVERY_MEM, type_ok_def])
>- (irule type_wf_shift_db >> gvs[EVERY_MEM, type_ok_def])
QED
Theorem type_ok:
(∀tds v n. type_ok tds n (TypeVar v) ⇔ v < n) ∧
(∀tds p n. type_ok tds n (PrimTy p) ⇔ T) ∧
(∀tds n. type_ok tds n Exception ⇔ T) ∧
(∀tds ts n c.
type_ok tds n (TypeCons c ts) ⇔
EVERY (λa. type_ok tds n a) ts ∧
∃ctors. oEL c tds = SOME (LENGTH ts, ctors)) ∧
(∀tds ts n. type_ok tds n (Tuple ts) ⇔ EVERY (λa. type_ok tds n a) ts) ∧
(∀tds ts t n.
type_ok tds n (Function tf t) ⇔
type_ok tds n tf ∧ type_ok tds n t) ∧
(∀tds t n. type_ok tds n (Array t) ⇔ type_ok tds n t) ∧
(∀tds t n. type_ok tds n (M t) ⇔ type_ok tds n t)
Proof
rw[type_ok_def, type_wf_def, freetyvars_ok_def] >>
gvs[EVERY_CONJ] >> eq_tac >> gvs[]
QED
Theorem freetyvars_ok_Functions:
∀ats rt db.
freetyvars_ok db (Functions ats rt) ⇔
EVERY (freetyvars_ok db) ats ∧
freetyvars_ok db rt
Proof
Induct >> rw[Functions_def, freetyvars_ok_def] >> eq_tac >> rw[]
QED
Theorem type_ok_Functions:
∀ats rt tds db.
type_ok tds db (Functions ats rt) ⇔
EVERY (type_ok tds db) ats ∧
type_ok tds db rt
Proof
Induct >> rw[Functions_def, type_ok] >> eq_tac >> rw[]
QED
Theorem subst_db_Functions:
∀ats rt n ts.
subst_db n ts (Functions ats rt) =
Functions (MAP (subst_db n ts) ats) (subst_db n ts rt)
Proof
Induct >> rw[Functions_def, subst_db_def]
QED
Theorem shift_db_Functions:
∀ats rt skip shift.
shift_db skip shift (Functions ats rt) =
Functions (MAP (shift_db skip shift) ats) (shift_db skip shift rt)
Proof
Induct >> rw[Functions_def, shift_db_def]
QED
(******************** Typing judgements ********************)
Theorem type_tcexp_freetyvars_ok:
∀ ns db st env e t.
EVERY (freetyvars_ok db) st ∧
EVERY (λ(v,scheme). freetyvars_ok_scheme db scheme) env ∧
namespace_ok ns ∧
type_tcexp ns db st env e t
⇒ freetyvars_ok db t
Proof
Induct_on `type_tcexp` >> rpt conj_tac >>
rw[type_ok_def, freetyvars_ok_def] >>
rgs[LIST_REL_EL_EQN, IMP_CONJ_THM, FORALL_AND_THM]
>- (
PairCases_on `s` >> gvs[specialises_def] >>
imp_res_tac ALOOKUP_MEM >> gvs[EVERY_MEM, type_ok_def] >>
first_x_assum drule >> strip_tac >> gvs[] >>
irule freetyvars_ok_tsubst >> simp[EVERY_MEM]
)
>- gvs[EVERY_EL]
>- gvs[EVERY_EL, type_ok_def]
>- gvs[oEL_THM, EVERY_EL]
>- gvs[freetyvars_ok_Functions]
>- (
gvs[freetyvars_ok_Functions, EVERY_EL, type_ok_def] >>
first_x_assum irule >> rw[] >>
simp[ZIP_MAP, GSYM MAP_REVERSE, EL_MAP, REVERSE_ZIP, EL_ZIP] >>
DEP_REWRITE_TAC[EL_REVERSE] >> gvs[]
)
>- (
ntac 2 $ first_x_assum irule >> gvs[EVERY_MEM, FORALL_PROD] >>
rw[] >> gvs[MEM_MAP, EXISTS_PROD] >>
first_x_assum drule >> rw[]
>- (
drule freetyvars_ok_shift_db >> rename1 `MEM (a,b,c) _` >>
disch_then $ qspecl_then [`b`,`new`] assume_tac >> gvs[]
)
>- (
irule freetyvars_ok_shift_db >> first_x_assum irule >> simp[]
)
)
>- (
first_x_assum irule >> simp[EVERY_REVERSE] >>
rw[EVERY_EL, EL_ZIP, EL_MAP] >> pairarg_tac >> gvs[EVERY_EL] >>
first_x_assum drule >> simp[]
)
>- (Cases_on `css` >> gvs[] >> PairCases_on `h` >> gvs[])
>- (
first_x_assum irule >> simp[EVERY_REVERSE] >>
gvs[EVERY_MEM, MEM_ZIP, PULL_EXISTS, EL_MAP] >> rw[] >>
last_x_assum irule >> simp[EL_MEM]
)
>- (
Cases_on `css` >> gvs[] >- gvs[namespace_ok_def] >>
PairCases_on `h` >> gvs[] >> first_x_assum $ irule >>
simp[EVERY_REVERSE, EVERY_MEM, MEM_ZIP, EL_MAP, PULL_EXISTS] >> rw[] >>
imp_res_tac ALOOKUP_MEM >> gvs[namespace_ok_def, EVERY_MEM, FORALL_PROD] >>
first_x_assum drule >> simp[MEM_EL, PULL_EXISTS] >>
disch_then drule >> rw[type_ok_def] >>
irule freetyvars_ok_mono >> goal_assum $ drule_at Any >> simp[]
)
>- (
gvs[EVERY_MEM, oEL_THM, namespace_ok_def, type_ok_def] >>
Cases_on `css` >> gvs[]
>- (Cases_on `usopt` >> gvs[MEM_EL, FORALL_PROD] >> PairCases_on `x` >> gvs[]) >>
first_x_assum $ qspec_then `h` assume_tac >> gvs[] >>
pairarg_tac >> gvs[] >>
qpat_x_assum `_ ⇒ freetyvars_ok db t` irule >>
simp[MEM_ZIP, EL_MAP, PULL_EXISTS] >> rw[] >>
irule freetyvars_ok_tsubst >> simp[EVERY_MEM] >>
imp_res_tac ALOOKUP_MEM >> gvs[MEM_EL, PULL_EXISTS] >>
qpat_x_assum `∀n. n < _ typedefs ⇒ _` drule >> simp[] >>
disch_then drule >> simp[] >>
qpat_x_assum `_ = EL _ _` $ assume_tac o GSYM >> simp[] >>
disch_then drule >> rw[] >>
irule freetyvars_ok_mono >> goal_assum $ drule_at Any >> simp[]
)
>- gvs[oEL_THM, EVERY_EL]
>- (
gvs[oEL_THM, EVERY_EL, namespace_ok_def] >>
drule ALOOKUP_MEM >> simp[MEM_EL] >>
strip_tac >> pop_assum $ assume_tac o GSYM >>
first_x_assum drule >> simp[] >> disch_then drule >> rw[type_ok_def] >>
irule freetyvars_ok_mono >> goal_assum $ drule_at Any >> simp[]
)
>- (
irule freetyvars_ok_tsubst >> gvs[SF ETA_ss] >>
gvs[oEL_THM, namespace_ok_def, EVERY_EL] >>
first_x_assum kall_tac >> first_x_assum drule >> simp[] >>
imp_res_tac ALOOKUP_MEM >> gvs[MEM_EL] >>
disch_then drule >> qpat_x_assum `_ = EL _ _` $ assume_tac o GSYM >> gvs[] >>
disch_then drule >> rw[type_ok_def] >>
irule freetyvars_ok_mono >> goal_assum $ drule_at Any >> simp[]
)
QED
Theorem type_tcexp_type_ok:
∀ ns db st env e t.
EVERY (type_ok (SND ns) db) st ∧
EVERY (λ(v,scheme). type_scheme_ok (SND ns) db scheme) env ∧
namespace_ok ns ∧
type_tcexp ns db st env e t
⇒ type_ok (SND ns) db t
Proof
Induct_on `type_tcexp` >> rpt conj_tac >> rw[type_ok] >>
rgs[LIST_REL_EL_EQN, IMP_CONJ_THM, FORALL_AND_THM]
>- (
PairCases_on `s` >> gvs[specialises_def] >>
imp_res_tac ALOOKUP_MEM >> gvs[EVERY_MEM] >>
first_x_assum drule >> strip_tac >> gvs[] >>
irule type_ok_subst_db >> simp[EVERY_MEM]
)
>- gvs[EVERY_EL]
>- gvs[EVERY_EL]
>- gvs[type_cons_def]
>- gvs[oEL_THM, EVERY_EL]
>- gvs[type_ok_Functions]
>- (
gvs[type_ok_Functions, EVERY_EL] >>
first_x_assum irule >> rw[] >>
simp[ZIP_MAP, GSYM MAP_REVERSE, EL_MAP, REVERSE_ZIP, EL_ZIP] >>
DEP_REWRITE_TAC[EL_REVERSE] >> gvs[]
)
>- (
ntac 2 $ first_x_assum irule >> gvs[EVERY_MEM, FORALL_PROD] >>
reverse $ rw[] >> gvs[MEM_MAP, EXISTS_PROD] >>
first_x_assum drule >> rw[]
>- (irule type_ok_shift_db >> simp[]) >>
rename1 `MEM (a,b,c) _` >>
drule type_ok_shift_db >>
disch_then $ qspecl_then [`b`,`new`] assume_tac >> gvs[]
)
>- (
first_x_assum irule >> simp[EVERY_REVERSE] >>
rw[EVERY_EL, EL_ZIP, EL_MAP] >> pairarg_tac >> gvs[EVERY_EL]
)
>- (Cases_on `css` >> gvs[] >> PairCases_on `h` >> gvs[])
>- (
first_x_assum irule >> simp[EVERY_REVERSE] >>
gvs[EVERY_MEM, MEM_ZIP, PULL_EXISTS, EL_MAP] >> rw[] >>
last_x_assum irule >> simp[EL_MEM]
)
>- (
Cases_on `css` >> gvs[] >- gvs[namespace_ok_def] >>
PairCases_on `h` >> gvs[] >> first_x_assum $ irule >>
simp[EVERY_REVERSE, EVERY_MEM, MEM_ZIP, EL_MAP, PULL_EXISTS] >> rw[] >>
imp_res_tac ALOOKUP_MEM >> gvs[namespace_ok_def, EVERY_MEM, FORALL_PROD] >>
first_x_assum drule >> simp[MEM_EL, PULL_EXISTS] >>
disch_then drule >> rw[] >>
irule type_ok_mono >> goal_assum $ drule_at Any >> simp[]
)
>- (
gvs[EVERY_MEM, oEL_THM, namespace_ok_def] >>
Cases_on `css` >> gvs[]
>- (Cases_on `usopt` >> gvs[MEM_EL, FORALL_PROD] >> PairCases_on `x` >> gvs[]) >>
last_x_assum $ qspec_then `h` assume_tac >> gvs[] >>
pairarg_tac >> gvs[] >>
qpat_x_assum `_ ⇒ type_ok typedefs db t` irule >>
simp[MEM_ZIP, EL_MAP, PULL_EXISTS] >> rw[] >>
irule type_ok_subst_db >> simp[EVERY_MEM] >>
imp_res_tac ALOOKUP_MEM >> gvs[MEM_EL, PULL_EXISTS] >>
qpat_x_assum `_ = EL _ _` $ assume_tac o GSYM >>
qpat_x_assum `∀n. n < _ typedefs ⇒ _` drule >> simp[] >>
disch_then drule >> simp[] >> disch_then drule >> rw[] >>
irule type_ok_mono >> goal_assum $ drule_at Any >> simp[]
)
>- gvs[oEL_THM, EVERY_EL]
>- (
gvs[oEL_THM, EVERY_EL, namespace_ok_def] >>
drule ALOOKUP_MEM >> simp[MEM_EL] >>
strip_tac >> pop_assum $ assume_tac o GSYM >>
first_x_assum drule >> simp[] >> disch_then drule >> rw[type_ok_def] >>
irule freetyvars_ok_mono >> goal_assum $ drule_at Any >> simp[]
)
>- (
irule type_ok_subst_db >> gvs[SF ETA_ss] >>
gvs[oEL_THM, namespace_ok_def, EVERY_EL] >>
first_x_assum kall_tac >> first_x_assum drule >> simp[] >>
imp_res_tac ALOOKUP_MEM >> gvs[MEM_EL] >>
disch_then drule >> qpat_x_assum `_ = EL _ _` $ assume_tac o GSYM >> gvs[] >>
disch_then drule >> rw[] >>
irule type_ok_mono >> goal_assum $ drule_at Any >> simp[]
)
QED
Theorem implodeEQ:
(implode x = y ⇔ (x = explode y)) ∧
(y = implode x ⇔ (explode y = x))
Proof
rw[EQ_IMP_THM] >> simp[]
QED
(* TODO move *)
Theorem ALL_DISTINCT_LENGTH_SET:
LENGTH a = LENGTH b ∧ set a = set b ∧ ALL_DISTINCT a ⇒ ALL_DISTINCT b
Proof
rw[] >>
drule PERM_ALL_DISTINCT_LENGTH >>
disch_then $ qspec_then `b` assume_tac >> gvs[] >>
metis_tac[sortingTheory.ALL_DISTINCT_PERM]
QED
Theorem type_tcexp_tcexp_wf:
∀ ns db st env e t.
EVERY (type_ok (SND ns) db) st ∧
EVERY (λ(v,scheme). type_scheme_ok (SND ns) db scheme) env ∧
namespace_ok ns ∧
type_tcexp ns db st env e t
⇒ tcexp_wf e
Proof
Induct_on `type_tcexp` >> rw[tcexp_wf_def, type_wf_def, type_ok] >>
rgs[EVERY_EL, LIST_REL_EL_EQN, EL_ZIP, EL_MAP] >>
simp[num_args_ok_def, num_monad_args_def] >>~-
([‘LLOOKUP somelist i = SOME e’, ‘i < LENGTH somelist’], gvs[oEL_THM])
>- (
gvs[type_exception_def, namespace_ok_def, ALL_DISTINCT_APPEND] >>
imp_res_tac ALOOKUP_MEM >> gvs[MEM_MAP, FORALL_PROD] >>
last_x_assum $ drule_at Concl >> rw[] >>
gvs[reserved_cns_def, implodeEQ]
)
>- (
gvs[type_cons_def, namespace_ok_def, ALL_DISTINCT_APPEND] >>
qsuff_tac `explode cname ∉ reserved_cns` >- simp[reserved_cns_def] >>
`MEM cname (MAP FST (FLAT (MAP SND typedefs)))` by (
simp[MEM_MAP, MEM_FLAT, EXISTS_PROD, PULL_EXISTS] >>
simp[Once MEM_EL, PULL_EXISTS, GSYM CONJ_ASSOC] >>
gvs[oEL_THM] >> goal_assum $ drule_at Any >> simp[] >>
imp_res_tac ALOOKUP_MEM >> simp[SF SFY_ss]) >>
first_x_assum $ drule_at Concl >> simp[] >> strip_tac >>
gvs[MEM_MAP, implodeEQ, FORALL_PROD] >>
gvs[GSYM implodeEQ] >> gs[mlstringTheory.implode_def]
)
>- simp[num_atomop_args_ok_def]
>- (
imp_res_tac get_PrimTys_SOME >> gvs[type_atom_op_cases] >>
simp[num_atomop_args_ok_def]
)
>- (
imp_res_tac type_tcexp_type_ok >>
gvs[EVERY_EL, LIST_REL_EL_EQN, type_ok, type_wf_def] >>
Cases_on `es` >> gvs[]
)
>- (Cases_on `xs` >> gvs[])
>- (
first_x_assum irule >> reverse $ rw[]
>- (irule type_ok_shift_db >> simp[]) >>
qpat_abbrev_tac `a = EL n env` >> PairCases_on `a` >> gvs[] >>
first_x_assum drule >> strip_tac >> gvs[] >>
drule type_ok_shift_db >> simp[]
)
>- (
first_x_assum irule >>
imp_res_tac type_tcexp_type_ok >> pop_assum irule >>
rgs[EVERY_EL, LIST_REL_EL_EQN, EL_MAP] >> reverse $ rw[]
>- (irule type_ok_shift_db >> simp[]) >>
qpat_abbrev_tac `a = EL n env` >> PairCases_on `a` >> gvs[] >>
first_x_assum drule >> strip_tac >> gvs[] >>
drule type_ok_shift_db >> simp[]
)
>- (
rw[] >> last_x_assum drule >>
pairarg_tac >> gvs[] >> pairarg_tac >> gvs[] >> strip_tac >>
pop_assum irule >> reverse $ rw[]
>- (
first_x_assum drule >> strip_tac >> gvs[] >>
drule type_ok_shift_db >> simp[]
)
>- (
DEP_REWRITE_TAC[EL_REVERSE] >> simp[] >>
qmatch_goalsub_abbrev_tac `EL m _` >>
`m < LENGTH schemes` by (unabbrev_all_tac >> gvs[]) >> simp[EL_ZIP] >>
Cases_on `EL m schemes` >> gvs[] >> last_x_assum drule >> rw[] >>
drule type_ok_shift_db >> simp[]
) >>
qmatch_goalsub_abbrev_tac `_ (_ m)` >>
PairCases_on `m` >> gvs[] >>
first_x_assum drule >> strip_tac >> gvs[] >>
drule type_ok_shift_db >> simp[]
)
>- (
rw[] >> first_x_assum drule >>
qmatch_goalsub_abbrev_tac `SND $ SND elem` >> PairCases_on `elem` >> gvs[]
)
>- (Cases_on `css` >> gvs[])
>- (
gvs[LENGTH_EQ_NUM_compute, EXISTS_PROD, numeral_less_thm] >>
rw[] >> gvs[EXTENSION, SF DNF_ss]
)
>- (
simp[MEM_FLAT, MEM_MAP, FORALL_PROD, DISJ_EQ_IMP, PULL_EXISTS] >>
rw[Once MEM_EL] >> pop_assum $ assume_tac o GSYM >>
first_x_assum drule >> simp[]
)
>- (
gvs[LENGTH_EQ_NUM_compute, EXISTS_PROD, numeral_less_thm] >>
rw[] >> gvs[EXTENSION, EQ_IMP_THM, SF DNF_ss]
)
>- simp[monad_cns_def]
>- simp[monad_cns_def]
>- (
first_x_assum irule >> last_x_assum assume_tac >>
drule_at (Pos last) type_tcexp_type_ok >> simp[EVERY_EL, type_ok] >> rw[] >>
DEP_REWRITE_TAC[EL_REVERSE] >> simp[EL_ZIP, EL_MAP]
)
>- simp[monad_cns_def] >~
[‘_ < LENGTH css ⇒ tcexp_wf (SND (SND (EL _ css)))’,
‘type_tcexp _ _ _ _ _ Exception’]
>- (
rw[] >> first_x_assum drule >> pairarg_tac >> gvs[] >> strip_tac >>
pop_assum irule >> rw[REVERSE_ZIP, EL_ZIP, GSYM MAP_REVERSE, EL_MAP] >>
DEP_REWRITE_TAC[EL_REVERSE] >> simp[] >>
qmatch_goalsub_abbrev_tac `EL m _` >>
`m < LENGTH tys` by (unabbrev_all_tac >> gvs[]) >>
imp_res_tac ALOOKUP_MEM >> pop_assum mp_tac >> rw[MEM_EL] >>
pop_assum $ assume_tac o GSYM >>
gvs[namespace_ok_def, EVERY_EL, FORALL_PROD] >>
last_x_assum drule >> simp[] >>
disch_then drule >> strip_tac >>
irule type_ok_mono >> goal_assum $ drule_at Any >> simp[]
)
>- (Cases_on `css` >> gvs[namespace_ok_def])
>- (
rw[] >> first_x_assum drule >> simp[ELIM_UNCURRY] >> strip_tac >> gvs[]
)
>- (
simp[MEM_FLAT, MEM_MAP, EXISTS_PROD, DISJ_EQ_IMP, PULL_EXISTS] >>
rw[Once MEM_EL] >> pop_assum $ assume_tac o GSYM >>
first_x_assum drule >> simp[] >> strip_tac >> gvs[]
)
>- (
gvs[namespace_ok_def, ALL_DISTINCT_APPEND] >>
irule ALL_DISTINCT_LENGTH_SET >> qexists_tac `MAP FST exndef` >> simp[]
)
>- (
gvs[namespace_ok_def, ALL_DISTINCT_APPEND] >>
first_x_assum $ drule_at Concl >> rw[reserved_cns_def] >>
simp[monad_cns_def, GSYM implodeEQ] >> gvs[MEM_MAP] >>
rpt strip_tac >> gvs[implodeEQ]) >>~-
([‘_ < LENGTH css ⇒ tcexp_wf (SND (SND (EL _ css)))’],
rw[] >> first_x_assum drule >> pairarg_tac >> gvs[] >> strip_tac >>
pop_assum irule >> gvs[EL_ZIP, EL_MAP] >> reverse $ rw[]
>- (imp_res_tac type_tcexp_type_ok >> gvs[type_ok, EVERY_EL]) >>
imp_res_tac type_tcexp_type_ok >> gvs[type_ok, EVERY_EL] >>
irule type_ok_subst_db >> simp[EVERY_EL] >>
gvs[namespace_ok_def, EVERY_EL, oEL_THM] >>
imp_res_tac ALOOKUP_MEM >> gvs[MEM_EL] >> pop_assum $ assume_tac o GSYM >>
qpat_x_assum ‘∀n. n < _ typedefs ⇒ _’ drule >> simp[] >>
disch_then drule >> rw[] >>
irule type_ok_mono >>
first_x_assum $ irule_at Any >> simp[]) >~
[‘type_tcexp _ _ _ _ _ (TypeCons tyid tyargs)’, ‘css ≠ []’]
>- (
gvs[namespace_ok_def, oEL_THM, EVERY_EL] >>
last_x_assum drule >> pairarg_tac >> gvs[] >> rw[] >>
Cases_on `css` >> gvs[] >>
Cases_on `usopt` >> gvs[] >> PairCases_on `x` >> gvs[]
) >~
[‘OPTION_ALL _ eopt’]
>- (
Cases_on ‘eopt’ >> gvs[] >> pairarg_tac >> gvs[] >>
rev_drule_at (Pos last) type_tcexp_type_ok >> simp[EVERY_EL, type_ok] >>
rw[] >> rpt (pairarg_tac >> gvs[]) >>
last_x_assum drule >> rw[] >> gvs[namespace_ok_def, ALL_DISTINCT_APPEND] >>
first_x_assum $ qspec_then `cn` mp_tac >> simp[Once MONO_NOT_EQ] >> rw[]
>- (
disj1_tac >> simp[MEM_MAP] >>
pop_assum mp_tac >> rw[monad_cns_def, reserved_cns_def] >> gvs[implodeEQ]
)
>- (
gvs[MEM_MAP, MEM_FLAT, PULL_EXISTS, EXISTS_PROD] >>
simp[Once MEM_EL, PULL_EXISTS] >> gvs[oEL_THM] >>
goal_assum $ drule_at Any >> simp[] >> imp_res_tac ALOOKUP_MEM >> simp[SF SFY_ss]
)
) >>~-
([‘type_tcexp _ _ _ _ _ (TypeCons tyid tyargs)’, ‘¬MEM v (FLAT (MAP _ css))’],
simp[MEM_FLAT, MEM_MAP, FORALL_PROD, DISJ_EQ_IMP, PULL_EXISTS] >>
rw[Once MEM_EL] >> pop_assum $ assume_tac o GSYM >>
last_x_assum drule >> simp[] >> strip_tac >> gvs[]) >>~-
([‘explode cn ∉ monad_cns’],
gvs[namespace_ok_def, ALL_DISTINCT_APPEND] >>
`MEM cn (MAP FST (FLAT (MAP SND typedefs)))` by (
simp[MEM_MAP, MEM_FLAT, EXISTS_PROD, PULL_EXISTS] >>
simp[Once MEM_EL, PULL_EXISTS, GSYM CONJ_ASSOC] >>
gvs[oEL_THM] >> goal_assum $ drule_at Any >> simp[] >>
pop_assum mp_tac >> rw[Once MEM_EL] >> gvs[EL_MAP] >>
last_x_assum drule >> rw[] >> pairarg_tac >> gvs[] >>
imp_res_tac ALOOKUP_MEM >> simp[SF SFY_ss]) >>
last_x_assum $ drule_at Concl >> simp[] >> strip_tac >>
gvs[reserved_cns_def, monad_cns_def] >>
simp[GSYM implodeEQ] >> gvs[MEM_MAP] >> rpt strip_tac >> gvs[implodeEQ]
)
>- (
rw[] >> first_x_assum drule >> simp[ELIM_UNCURRY] >> strip_tac >> gvs[]
)
>- (Cases_on `usopt` >> gvs[] >> PairCases_on `x` >> gvs[])
QED
Theorem type_tcexp_freevars_tcexp:
∀ns db st env e t.
type_tcexp ns db st env e t
⇒ freevars_tcexp e ⊆ set (MAP FST env)
Proof
Induct_on `type_tcexp` >> rw[] >>
simp[BIGUNION_SUBSET, MEM_MAP, PULL_EXISTS] >> rw[] >>
gvs[LIST_REL_EL_EQN, MEM_EL, MAP_ZIP, DIFF_SUBSET, SUBSET_INSERT_DELETE]
>- (
imp_res_tac ALOOKUP_MEM >> gvs[MEM_EL, EXISTS_PROD] >>
PairCases_on `s` >> goal_assum drule >> goal_assum drule
)
>- gvs[MAP_REVERSE, MAP_ZIP, DIFF_SUBSET]
>- gvs[MAP_MAP_o, combinTheory.o_DEF, LAMBDA_PROD, FST_THM]
>- (
gvs[MAP_REVERSE, MAP_ZIP] >>
simp[BIGUNION_SUBSET, MEM_MAP, PULL_EXISTS] >> rw[] >>
pairarg_tac >> gvs[MEM_EL] >> last_x_assum drule >>
pairarg_tac >> gvs[] >> pairarg_tac >> gvs[] >> strip_tac >>
pop_assum mp_tac >> gvs[MAP_MAP_o, combinTheory.o_DEF, UNCURRY] >>
simp[SF ETA_ss, MAP_ZIP]
)
>- (
gvs[GSYM SUBSET_INSERT_DELETE, BIGUNION_SUBSET, MEM_MAP,
PULL_EXISTS, FORALL_PROD, EVERY_MEM] >>
rw[] >> first_x_assum drule >> simp[]
)
>- gvs[MAP_REVERSE, MAP_ZIP, DIFF_SUBSET, GSYM SUBSET_INSERT_DELETE]
>- (
gvs[GSYM SUBSET_INSERT_DELETE, BIGUNION_SUBSET, MEM_MAP, PULL_EXISTS] >> rw[] >>
pairarg_tac >> gvs[EVERY_MEM] >>
first_x_assum drule >> simp[] >> strip_tac >> gvs[] >>
gvs[MAP_REVERSE, MAP_ZIP, DIFF_SUBSET]
) >>
Cases_on `usopt` >> gvs[]
>- (
gvs[GSYM SUBSET_INSERT_DELETE, BIGUNION_SUBSET, MEM_MAP, PULL_EXISTS] >>
rw[] >> pairarg_tac >> gvs[EVERY_MEM] >>
first_x_assum drule >> simp[] >> strip_tac >> gvs[] >>
gvs[MAP_REVERSE, MAP_ZIP, DIFF_SUBSET]
) >>
PairCases_on `x` >> gvs[] >>
gvs[GSYM SUBSET_INSERT_DELETE, BIGUNION_SUBSET, MEM_MAP, PULL_EXISTS] >>
rw[] >> pairarg_tac >> gvs[EVERY_MEM] >>
first_x_assum drule >> simp[] >> strip_tac >> gvs[] >>
gvs[MAP_REVERSE, MAP_ZIP, DIFF_SUBSET]
QED
Theorem type_tcexp_env_extensional:
∀ns db st env e t env'.
type_tcexp ns db st env e t ∧
(∀x. x ∈ freevars_tcexp e ⇒ ALOOKUP env x = ALOOKUP env' x)
⇒ type_tcexp ns db st env' e t
Proof
Induct_on `type_tcexp` >> rw[] >> rw[Once type_tcexp_cases] >>
rpt $ first_assum $ irule_at Any >> rw[ALOOKUP_MAP]
>- (
gvs[LIST_REL_EL_EQN, PULL_EXISTS, MEM_MAP] >> rw[] >>
first_x_assum drule >> strip_tac >> first_x_assum irule >> rw[] >>
first_x_assum irule >> goal_assum drule >> simp[EL_MEM]
)
>- (
gvs[LIST_REL_EL_EQN, PULL_EXISTS, MEM_MAP] >> rw[] >>
first_x_assum drule >> strip_tac >> first_x_assum irule >> rw[] >>
first_x_assum irule >> goal_assum drule >> simp[EL_MEM]
)
>- (
gvs[LIST_REL_EL_EQN, PULL_EXISTS, MEM_MAP] >> rw[] >>
first_x_assum drule >> strip_tac >> first_x_assum irule >> rw[] >>
first_x_assum irule >> goal_assum drule >> simp[EL_MEM]
)
>- (
gvs[LIST_REL_EL_EQN, PULL_EXISTS, MEM_MAP] >> rw[] >>
first_x_assum drule >> strip_tac >> first_x_assum irule >> rw[] >>
first_x_assum irule >> goal_assum drule >> simp[EL_MEM]
)
>- (
gvs[LIST_REL_EL_EQN, PULL_EXISTS, MEM_MAP] >> rw[] >>
first_x_assum drule >> strip_tac >> first_x_assum irule >> rw[] >>
first_x_assum irule >> disj2_tac >> goal_assum drule >> simp[EL_MEM]
)
>- (
rw[ALOOKUP_APPEND] >> CASE_TAC >> gvs[] >>
first_x_assum irule >> simp[] >> gvs[ALOOKUP_NONE, MAP_REVERSE, MAP_ZIP]
)
>- (
rw[ALOOKUP_APPEND] >> CASE_TAC >> gvs[] >>
first_x_assum irule >> simp[] >>
gvs[LIST_REL_EL_EQN, ALOOKUP_NONE, MAP_REVERSE, MAP_ZIP]
)
>- (
gvs[LIST_REL_EL_EQN, PULL_EXISTS, MEM_MAP] >> rw[] >>
rpt (pairarg_tac >> gvs[]) >>
first_x_assum drule >> simp[] >> strip_tac >> first_x_assum irule >> rw[] >>
simp[ALOOKUP_MAP, ALOOKUP_APPEND] >> CASE_TAC >> gvs[] >>
AP_TERM_TAC >> first_x_assum irule >> gvs[ALOOKUP_NONE, MAP_REVERSE, MAP_ZIP] >>
gvs[MEM_MAP] >> disj2_tac >> qexists `fn,body` >> simp[MEM_EL] >>
goal_assum drule >> simp[]
)
>- (
disj1_tac >> gvs[EVERY_MEM] >> rw[] >> rpt (pairarg_tac >> gvs[]) >>
first_x_assum drule >> simp[] >> strip_tac >> pop_assum irule >> rw[] >>
first_x_assum irule >> gvs[MEM_MAP, PULL_EXISTS, EXISTS_PROD] >>
disj2_tac >> rpt $ goal_assum $ drule_at Any >> simp[]
)
>- (
disj1_tac >> rpt $ first_x_assum $ irule_at Any >> rw[ALOOKUP_APPEND] >>
CASE_TAC >> gvs[] >> first_x_assum irule >> disj2_tac >> gvs[] >>
gvs[ALOOKUP_NONE, MAP_REVERSE, MAP_ZIP]
)
>- (
ntac 2 disj2_tac >> disj1_tac >>
gvs[EVERY_MEM] >> rw[] >> rpt (pairarg_tac >> gvs[]) >>
first_x_assum drule >> simp[] >> strip_tac >> gvs[] >>
first_x_assum $ irule_at Any >> rw[ALOOKUP_APPEND] >> CASE_TAC >> gvs[] >>
first_x_assum irule >> disj2_tac >> gvs[MEM_MAP, PULL_EXISTS, EXISTS_PROD] >>
rpt $ goal_assum $ drule_at Any >> gvs[ALOOKUP_NONE, MAP_REVERSE, MAP_ZIP]
)
>- (
rpt disj2_tac >> rpt $ first_x_assum $ irule_at Any >> simp[] >>
gvs[EVERY_MEM] >> rw[] >> pairarg_tac >> gvs[] >>
first_x_assum drule >> simp[] >> strip_tac >> gvs[] >>
first_x_assum irule >> rw[ALOOKUP_APPEND] >> CASE_TAC >> gvs[] >>
first_x_assum irule >> disj2_tac >> gvs[] >> disj2_tac >>
gvs[MEM_MAP, PULL_EXISTS, EXISTS_PROD] >>
rpt $ goal_assum $ drule_at Any >> gvs[ALOOKUP_NONE, MAP_REVERSE, MAP_ZIP]
)
>- (disj1_tac >> first_x_assum $ irule_at Any >> simp[])
>- (rpt disj2_tac >> rpt $ first_x_assum $ irule_at Any >> simp[])
QED
Theorem type_tcexp_weaken:
∀ns db st env e t db' st' env'.
type_tcexp ns db st env e t
⇒ type_tcexp ns (db + db') (st ++ st') (env ++ env') e t
Proof
ntac 3 gen_tac >>
Induct_on `type_tcexp` >> rw[] >> rw[Once type_tcexp_cases]
>- (
simp[ALOOKUP_APPEND] >> PairCases_on `s` >> gvs[specialises_def] >>
qexists_tac `subs` >> gvs[] >>
irule EVERY_MONOTONIC >> rw[] >> goal_assum $ drule_at Any >> rw[] >>
drule type_ok_mono >> simp[]
)
>- gvs[LIST_REL_EL_EQN]
>- metis_tac[]
>- (drule type_ok_mono >> simp[])
>- metis_tac[]
>- metis_tac[]
>- (goal_assum $ drule_at Any >> gvs[LIST_REL_EL_EQN])
>- (
goal_assum $ drule_at Any >> gvs[LIST_REL_EL_EQN] >>
irule EVERY_MONOTONIC >> rw[] >> goal_assum $ drule_at Any >> rw[] >>
drule type_ok_mono >> simp[]
)
>- gvs[oEL_THM, EL_APPEND_EQN]
>- (ntac 2 $ goal_assum $ drule_at Any >> gvs[LIST_REL_EL_EQN])
>- metis_tac[]
>- (rpt $ goal_assum $ drule_at Any >> gvs[LIST_REL_EL_EQN])
>- (
irule_at Any EQ_REFL >> simp[] >>
irule EVERY_MONOTONIC >> goal_assum $ drule_at Any >> rw[] >>
drule type_ok_mono >> simp[]
)
>- (qexistsl_tac [`new`,`t`] >> gvs[])
>- (
qexists_tac `schemes` >> gvs[LIST_REL_EL_EQN] >> rw[]
>- (
pairarg_tac >> gvs[] >> pairarg_tac >> gvs[] >>
last_x_assum drule >> gvs[]
)
>- (
irule EVERY_MONOTONIC >> rw[] >> goal_assum $ drule_at Any >> rw[] >>
pairarg_tac >> gvs[] >> drule type_ok_mono >> simp[]
)
)
>- (disj1_tac >> gvs[FORALL_PROD, EVERY_MEM] >> rw[] >> metis_tac[])
>- (disj1_tac >> first_x_assum $ irule_at Any >> gvs[APPEND_ASSOC_CONS])
>- (
disj2_tac >> disj2_tac >> disj1_tac >> gvs[EVERY_MEM, FORALL_PROD] >> rw[] >>
first_x_assum drule >> strip_tac >> simp[] >> gvs[APPEND_ASSOC_CONS] >>
pop_assum $ irule_at Any >> simp[]
)
>- (
disj2_tac >> disj2_tac >> disj2_tac >>
rpt $ goal_assum $ drule_at Any >> gvs[] >>
irule_at Any EVERY_MONOTONIC >>
goal_assum $ drule_at Any >> rw[] >> pairarg_tac >> gvs[] >>
gvs[APPEND_ASSOC_CONS]
)
>- (disj1_tac >> rpt $ goal_assum $ drule_at Any >> gvs[])
>- (disj2_tac >> disj2_tac >> rpt $ first_x_assum $ irule_at Any >> gvs[])
QED
Theorem type_tcexp_NestedCase_free:
∀ns db st env e t ce.
type_tcexp ns db st env e t ∧
e = tcexp_of ce
⇒ NestedCase_free ce
Proof
Induct_on `type_tcexp` >> rw[pure_cexpTheory.NestedCase_free_def] >>
rpt (qpat_x_assum ‘tcexp_of _ = _’ (assume_tac o SYM)) >>
qpat_x_assum ‘_ = tcexp_of _’ assume_tac >>
pop_assum mp_tac >> simp[Once $ DefnBase.one_line_ify NONE tcexp_of_def] >>
strip_tac >> gvs[AllCaseEqs()] >>
rgs[EVERY_EL, LIST_REL_EL_EQN, EL_ZIP, EL_MAP] >>
gvs[MAP_EQ_CONS, numeral_less_thm] >> rw[] >> gvs[] >>~-
([‘NestedCase_free (EL _ _)’], metis_tac[])
>~ [‘OPTION_ALL _ eopt’]
>- (Cases_on ‘eopt’ >> gvs[] >> pairarg_tac >> gvs[]) >>
gvs[ELIM_UNCURRY] >> metis_tac[]
QED
Theorem type_tcexp_cnames_arities:
∀ns db st env e t ce.
type_tcexp ns db st env e t ∧
namespace_ok ns ∧
e = tcexp_of ce
⇒ cns_arities_ok ns (cns_arities ce)
Proof
Induct_on `type_tcexp` >> rw[] >>
rpt (qpat_x_assum ‘tcexp_of _ = _’ (assume_tac o SYM)) >>
qpat_x_assum ‘_ = tcexp_of _’ assume_tac >>
pop_assum mp_tac >> simp[Once $ DefnBase.one_line_ify NONE tcexp_of_def] >>
strip_tac >> gvs[AllCaseEqs(), pure_cexpTheory.cns_arities_def, monad_cns_def] >>
rgs[EVERY_EL, LIST_REL_EL_EQN, EL_ZIP, EL_MAP] >>
simp[MEM_MAP, MEM_EL, PULL_EXISTS] >> gvs[MAP_EQ_CONS, numeral_less_thm] >>
rw[] >> gvs[] >> res_tac >> gvs[]
>- (
rw[DISJ_EQ_IMP] >> gvs[type_exception_def, ns_cns_arities_def] >>
irule_at Any OR_INTRO_THM1 >> simp[MEM_MAP, EXISTS_PROD] >>
irule_at Any EQ_REFL >> imp_res_tac ALOOKUP_MEM
)
>- (
PairCases_on `ns` >> simp[ns_cns_arities_def] >>
irule_at Any OR_INTRO_THM2 >> irule_at Any OR_INTRO_THM1 >> simp[]
)
>- (
PairCases_on `ns` >> simp[ns_cns_arities_def] >>
irule_at Any OR_INTRO_THM2 >> irule_at Any OR_INTRO_THM1 >> simp[]
)
>- (
rw[DISJ_EQ_IMP] >> gvs[type_cons_def, ns_cns_arities_def] >>