-
Notifications
You must be signed in to change notification settings - Fork 84
/
inferSoundScript.sml
1138 lines (1115 loc) · 39.3 KB
/
inferSoundScript.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
(*
Proves soundness of the type inferencer: any type assignment
produced by the type inferencer is a valid type for the program.
*)
open preamble
open typeSystemTheory astTheory semanticPrimitivesTheory inferTheory unifyTheory infer_tTheory
astPropsTheory inferPropsTheory typeSysPropsTheory infer_eSoundTheory envRelTheory type_eDetermTheory
infer_eCompleteTheory namespacePropsTheory
val _ = new_theory "inferSound";
Triviality letrec_lemma2:
!funs_ts l l' s s'.
(!t1 t2. t_walkstar s t1 = t_walkstar s t2 ⇒ t_walkstar s' t1 = t_walkstar s' t2) ∧
(LENGTH funs_ts = LENGTH l) ∧
(LENGTH funs_ts = LENGTH l') ∧
MAP (λn. t_walkstar s (Infer_Tuvar n)) l' = MAP (t_walkstar s) funs_ts
⇒
(MAP2 (λ(f,x,e) t. (f,t)) l (MAP (λn. convert_t (t_walkstar s' (Infer_Tuvar n))) l')
=
MAP2 (λ(x,y,z) t. (x,convert_t (t_walkstar s' t))) l funs_ts)
Proof
induct_on `funs_ts` >>
cases_on `l` >>
cases_on `l'` >>
rw [] >>
fs [] >|
[PairCases_on `h` >>
rw [] >>
metis_tac [],
metis_tac []]
QED
Triviality sub_completion_empty:
!m n s s'. sub_completion m n s [] s' ⇔ count n ⊆ FDOM s' ∧ (∀uv. uv ∈ FDOM s' ⇒ check_t m ∅ (t_walkstar s' (Infer_Tuvar uv))) ∧ s = s'
Proof
rw [sub_completion_def, pure_add_constraints_def] >>
metis_tac []
QED
Triviality generalise_none:
(!t s' t' x.
check_t 0 x t ∧
generalise 0 0 FEMPTY t = (0, s', t')
⇒
s' = FEMPTY ∧
check_t 0 {} t) ∧
(!ts s' ts' x.
EVERY (check_t 0 x) ts ∧
generalise_list 0 0 FEMPTY ts = (0, s', ts')
⇒
s' = FEMPTY ∧
EVERY (check_t 0 {}) ts)
Proof
ho_match_mp_tac infer_t_induction >>
rw [generalise_def, check_t_def, LET_THM, LAMBDA_PROD]
>- (`?n s' t'. generalise_list 0 0 FEMPTY ts = (n,s',t')` by metis_tac [pair_CASES] >>
fs [] >>
rw [] >>
metis_tac [])
>- (`?n s' t'. generalise_list 0 0 FEMPTY ts = (n,s',t')` by metis_tac [pair_CASES] >>
fs [] >>
rw [] >>
metis_tac []) >>
`?n' s'' t''. generalise 0 0 FEMPTY t = (n',s'',t'')` by metis_tac [pair_CASES] >>
fs [] >>
`?n s' t'. generalise_list 0 n' s'' ts = (n,s',t')` by metis_tac [pair_CASES] >>
fs [] >>
rw [] >>
metis_tac []
QED
Triviality lookup_var_empty:
lookup_var x (bind_tvar tvs Empty) tenv =
lookup_var x Empty tenv
Proof
rw[bind_tvar_def,lookup_var_def,lookup_varE_def]>>
EVERY_CASE_TAC>>fs[tveLookup_def]
QED
(* TODO: This should be generalized eventually *)
Theorem env_rel_complete_bind:
env_rel_complete FEMPTY ienv tenv Empty ⇒
env_rel_complete FEMPTY ienv tenv (bind_tvar tvs Empty)
Proof
rw[env_rel_complete_def,lookup_var_empty]>>res_tac>>fs[]
>-
metis_tac[]
>>
match_mp_tac tscheme_approx_weakening>>qexists_tac`0`>>
HINT_EXISTS_TAC>>
fs[t_wfs_def]
QED
(* TODO: The generated set of type identifiers (tids)
must be related to st2.next_id in some way
The current relation might be wrong *)
(* the set of ids n1 .... n2-1 *)
Definition set_ids_def:
set_ids n1 (n2:num) = {m | n1 ≤ m ∧ m < n2}
End
Triviality set_ids_eq:
set_ids n1 n2 =
set (GENLIST (λx. x + n1) (n2-n1))
Proof
fs[set_ids_def,EXTENSION,MEM_MAP,MEM_GENLIST]>>
rw[EQ_IMP_THM]>>
qexists_tac`x-n1`>>fs[]
QED
Theorem set_ids_same[simp]:
set_ids x x = {}
Proof
rw[set_ids_eq]
QED
Theorem set_ids_eq_union:
x <= y /\ y <= z ==> set_ids x z = set_ids x y UNION set_ids y z
Proof
fs [set_ids_def, EXTENSION]
QED
Theorem set_ids_eq_union_eq:
x <= y /\ y <= z /\ s = set_ids y z
==> set_ids x z = set_ids x y UNION s
Proof
fs [set_ids_eq_union]
QED
fun str_tac strs = ConseqConv.CONSEQ_CONV_TAC
(ConseqConv.CONSEQ_REWRITE_CONV ([], strs, []));
Theorem infer_d_sound:
(!d tenv ienv st1 st2 ienv'.
infer_d ienv d st1 = (Success ienv', st2) ∧
env_rel tenv ienv ∧
start_type_id ≤ st1.next_id
⇒
type_d T tenv d (set_ids st1.next_id st2.next_id) (ienv_to_tenv ienv')) ∧
(!ds tenv ienv st1 st2 ienv'.
infer_ds ienv ds st1 = (Success ienv', st2) ∧
env_rel tenv ienv ∧
start_type_id ≤ st1.next_id
⇒
type_ds T tenv ds (set_ids st1.next_id st2.next_id) (ienv_to_tenv ienv'))
Proof
Induct
>- (
(* Dlet *)
rw[infer_d_def,success_eqns]>>
pairarg_tac \\ fs[success_eqns]
\\ fs[init_state_def] \\ rveq
\\ old_drule (CONJUNCT1 infer_e_sound)
\\ fs[init_state_def, env_rel_def]
\\ imp_res_tac(CONJUNCT1 infer_e_wfs) \\ fs[]
\\ old_drule (CONJUNCT1 infer_p_sound)
\\ simp[]
\\ `(init_infer_state st1).next_uvar = 0` by (fs [init_infer_state_def] >> rw []) >>
old_drule (CONJUNCT1 infer_p_wfs) >>
disch_then old_drule >>
strip_tac >>
old_drule t_unify_wfs >>
disch_then old_drule >>
strip_tac >>
old_drule (CONJUNCT1 infer_e_check_t) >>
impl_tac >- fs [ienv_ok_def] >>
strip_tac >>
old_drule (CONJUNCT1 infer_e_check_s) >>
simp [] >>
disch_then (qspec_then `0` mp_tac) >>
impl_tac >- simp [check_s_def, init_infer_state_def] >>
strip_tac >>
old_drule (CONJUNCT1 infer_p_check_t) >>
strip_tac >>
old_drule (CONJUNCT1 infer_p_check_s) >>
disch_then (qspec_then `0` mp_tac) >>
impl_tac >- fs [ienv_ok_def] >>
strip_tac >>
old_drule t_unify_check_s >>
simp [] >>
disch_then old_drule >>
simp [] >>
impl_tac >- metis_tac [infer_p_next_uvar_mono, check_t_more4] >>
strip_tac >>
pairarg_tac >>
rename1 `generalise_list _ _ _ _ = (tvs, s2, ts)` >>
rename1 `Success (t2,bindings), st1'` >>
`?ec1 last_sub.
ts = MAP (t_walkstar last_sub) (MAP SND bindings) ∧
t_wfs last_sub ∧
sub_completion tvs st1'.next_uvar s ec1 last_sub`
by (
`tvs = tvs +0 ` by DECIDE_TAC>>pop_assum SUBST1_TAC>>
old_drule generalise_complete>>fs[]>>
fs[LAMBDA_PROD, EVERY_MAP] >>
metis_tac[]) >>
old_drule sub_completion_unify2 >>
disch_then old_drule >>
strip_tac >>
old_drule (CONJUNCT1 sub_completion_infer_p) >>
disch_then old_drule >>
strip_tac >>
`env_rel_sound FEMPTY ienv tenv (bind_tvar tvs Empty)`
by (
`t_wfs FEMPTY` by rw [t_wfs_def]
>> metis_tac [env_rel_sound_extend_tvs]) >>
old_drule env_rel_e_sound_empty_to >>
disch_then old_drule >>
disch_then old_drule >>
strip_tac >>
strip_tac >>
disch_then old_drule >>
simp [] >>
disch_then old_drule >>
pop_assum (qspecl_then [`tenv`, `tvs`, `(t1,t2)::ec1`, `last_sub`] mp_tac) >>
impl_tac
>- fs [typeSoundInvariantsTheory.tenv_ok_def, env_rel_sound_def] >>
rw [] >>
`t_walkstar last_sub t2 = t_walkstar last_sub t1`
by (
imp_res_tac infer_e_wfs >>
imp_res_tac infer_p_wfs >>
imp_res_tac t_unify_wfs >>
metis_tac [sub_completion_apply, t_unify_apply]) >>
imp_res_tac infer_e_next_id_const >>
imp_res_tac infer_p_next_id_const >>
`st1.next_id = st1'.next_id` by fs[init_infer_state_def] >>
Cases_on `is_value e` >>
fs [success_eqns] >>
rw [Once type_d_cases, ienv_to_tenv_def]
>- (
qexists_tac `tvs` >>
qexists_tac `convert_t (t_walkstar last_sub t2)` >>
qexists_tac `convert_env last_sub bindings` >>
rw []
>- (
simp [ZIP_MAP, tenv_add_tvs_def] >>
simp [MAP_MAP_o, combinTheory.o_DEF, LAMBDA_PROD, convert_env_def])
>- (imp_res_tac infer_p_bindings >> fs [])
>- (
old_drule (GEN_ALL env_rel_complete_bind) >>
disch_then (qspec_then `tvs'` assume_tac) >>
old_drule (GEN_ALL infer_pe_complete) >>
`ALL_DISTINCT (pat_bindings p [])` by
(imp_res_tac type_p_pat_bindings>>
`MAP FST bindings = pat_bindings p []` by
(pop_assum sym_sub_tac>>
simp[convert_env_def,MAP_MAP_o,MAP_EQ_f,FORALL_PROD])>>
fs[])>>
rpt (disch_then old_drule) >>
disch_then (qspecl_then [`st1`,`<| loc := SOME l; err := ienv.inf_t |>`] mp_tac) >>
strip_tac >>
rfs [] >>
fs[] >>
rfs[] >>
rpt var_eq_tac >>
(* The "new" subcompletion s'' maps types in bindings to some type schemes with tvs' quantified variables *)
simp[LIST_REL_EL_EQN,EL_MAP,convert_env_def,tenv_add_tvs_def]>>rw[]>>
pairarg_tac>>fs[]>>
pairarg_tac>>fs[]>>
pairarg_tac>>fs[]>>
fs[tscheme_inst_def]>>
(* We need to instantiate the deB vars in t''', which are introduced under last_sub to the unification done in s'' *)
imp_res_tac generalise_subst>>
fs[]>>
(* Rewrite last_sub back into an infer_subst *)
`t_walkstar last_sub t'''' = infer_subst s2 (t_walkstar s t'''')` by
(fs[MAP_MAP_o,LIST_EQ_REWRITE,EL_MAP,infer_subst_FEMPTY]>>
pop_assum(qspec_then`n` assume_tac)>>rfs[])>>
fs[sub_completion_def]>>
Q.ISPECL_THEN [`tvs'`,`s''`] mp_tac (GEN_ALL generalise_subst_exist)>>
impl_tac>-
(fs[]>>
metis_tac[pure_add_constraints_success])>>
rw[]>>
(* This produces the appropriate substitution mentioned above *)
pop_assum (qspecl_then[`MAP (t_walkstar s) (MAP SND bindings)`,`[]`,`FEMPTY`,`tvs`,`s2`,`MAP (t_walkstar last_sub) (MAP SND bindings)`] mp_tac)>>
fs[]>>
impl_keep_tac
>-
(fs[EVERY_MEM,MEM_MAP,PULL_EXISTS]>>
fs[GSYM FORALL_AND_THM]>>fs[GSYM IMP_CONJ_THM]>>
ntac 2 strip_tac>>
CONJ_ASM2_TAC
>-
metis_tac[check_t_t_vars]
>>
match_mp_tac t_walkstar_check>> fs[]>>
last_x_assum (qspec_then `y'` assume_tac)>>rfs[]>>
fs[UNCURRY]>>
reverse CONJ_TAC>-
(match_mp_tac (check_t_more5|>CONJUNCT1|>MP_CANON)>>
HINT_EXISTS_TAC>>
fs[])>>
match_mp_tac (check_s_more3 |> MP_CANON)>>
qexists_tac `count st'.next_uvar`>>
fs[])
>>
rw[]>>
(* Pick the substitution, except turn it into deBruijn vars *)
qexists_tac`MAP convert_t subst'`>>fs[]>>
`check_t 0 (count st'.next_uvar) t''''` by
(fs[EVERY_EL]>>
last_x_assum(qspec_then`n` assume_tac)>>rfs[])>>
`check_t (LENGTH subst') {} (infer_subst s2 (t_walkstar s t''''))` by
(qpat_x_assum `A = infer_subst B C` sym_sub_tac>>
Q.SPECL_THEN [`count (st'.next_uvar)`,`last_sub`,`LENGTH subst'`,`t''''`] mp_tac (check_t_less |> CONJUNCT1 |>GEN_ALL)>>
simp[]>>
rw[]>>
`count st'.next_uvar ∩ COMPL(FDOM last_sub) = {}` by
(simp[EXTENSION]>>fs[SUBSET_DEF]>>
metis_tac[])>>
fs[])>>
CONJ_ASM1_TAC>-
metis_tac[check_t_to_check_freevars]>>
CONJ_TAC>-
(fs[EVERY_MAP,EVERY_MEM]>>
metis_tac[check_t_to_check_freevars])>>
imp_res_tac deBruijn_subst_convert>>
pop_assum(qspec_then `subst'`assume_tac)>>fs[]>>
AP_TERM_TAC>>
Q.ISPECL_THEN [`s''`,`s2`,`subst'`,`_`,`count st'.next_uvar`] mp_tac (GEN_ALL infer_deBruijn_subst_infer_subst_walkstar)>>
impl_tac>-
(fs[SUBSET_DEF]>>
rw[]>>
fs[IN_FRANGE]>>
metis_tac[pure_add_constraints_wfs])>>
rw[]>>
pop_assum kall_tac>>
pop_assum(qspec_then `t_walkstar s t''''` mp_tac)>>
impl_tac>-
(imp_res_tac infer_p_check_t>>
fs[EXTENSION,SUBSET_DEF]>>
fs[MEM_MAP,PULL_EXISTS]>>
imp_res_tac ALOOKUP_MEM>>
fs[FORALL_PROD,EXISTS_PROD]>>
CONJ_TAC>-
metis_tac[MEM_EL]>>
reverse CONJ_TAC>-
metis_tac[MEM_EL]
>>
fs[EVERY_MAP,MAP_MAP_o,EVERY_MEM,UNCURRY]>>
match_mp_tac t_walkstar_check>>fs[]>>
CONJ_TAC>-
(match_mp_tac check_s_more5>>
asm_exists_tac>>fs[])
>>
first_x_assum(qspec_then`(n',t'''')` mp_tac)>>
impl_tac>- metis_tac[MEM_EL]>>
imp_res_tac check_t_more5>>
fs[SUBSET_DEF,EXTENSION])
>>
rw[]>>
metis_tac[pure_add_constraints_wfs,t_walkstar_SUBMAP,pure_add_constraints_success]))
>- (
qexists_tac `convert_t (t_walkstar last_sub t2)`
>> qexists_tac `convert_env last_sub bindings`
>> rw []
>- (
simp [ZIP_MAP, tenv_add_tvs_def]
>> simp [MAP_MAP_o, combinTheory.o_DEF, LAMBDA_PROD, convert_env_def])
>-
(match_mp_tac (GEN_ALL infer_e_type_pe_determ)>>
qexists_tac`st1` >>
qexists_tac `<| loc := SOME l; err := ienv.inf_t |>` >>
HINT_EXISTS_TAC>>fs[]>>
imp_res_tac(CONJUNCT2 generalise_none)>>
pop_assum(qspec_then`count st1'.next_uvar` mp_tac)>>
impl_tac>>fs[EVERY_MAP,EVERY_MEM,FORALL_PROD]>>
rw[]>>res_tac>>
qpat_x_assum `t_wfs s` assume_tac>>
old_drule t_walkstar_check>>
disch_then match_mp_tac>>
rw[]
>- (match_mp_tac check_s_more5>>HINT_EXISTS_TAC>>fs[])
>- (match_mp_tac (SIMP_RULE (srw_ss()) [AND_IMP_INTRO, PULL_FORALL] (CONJUNCT1 check_t_more5))>>HINT_EXISTS_TAC>>fs[]))
>- (
imp_res_tac infer_p_bindings
>> fs [])
>- fs [bind_tvar_def]))
(* Dletrec*)
>- (
rw[infer_d_def] >>
fs[success_eqns] >>
rename1 `infer_funs <| loc := SOME loc; err := ienv.inf_t |> _ _ _ = _` >>
fs[init_state_def]>>
pairarg_tac>>fs[success_eqns]>>rw[]>>
`t_wfs (init_infer_state st1).subst` by rw [init_infer_state_def, t_wfs_def] >>
`(init_infer_state st1).next_uvar = 0` by (fs [init_infer_state_def] >> rw []) >>
`t_wfs st''''.subst` by
(imp_res_tac infer_e_wfs>>fs[])>>
(*MAP2 looks nasty to work with...*)
rename [`ALL_DISTINCT (MAP FST l)`] >>
qabbrev_tac `bindings = ZIP (MAP FST l,(MAP (λn. Infer_Tuvar n) (COUNT_LIST (LENGTH l))))`>>
qmatch_asmsub_abbrev_tac`nsAppend (alist_to_ns mapp)`>>
fs[]>>
`mapp = MAP (λ(n,t). (n,0,t)) bindings` by
(unabbrev_all_tac>>fs[MAP2_MAP,LENGTH_COUNT_LIST,ZIP_MAP,MAP_MAP_o,MAP_EQ_f,FORALL_PROD])>>
`ienv_val_ok (count (LENGTH l)) (nsAppend (alist_to_ns mapp) ienv.inf_v)` by
(fs[ienv_ok_def,ienv_val_ok_def]>>
match_mp_tac nsAll_nsAppend>>
rw[]
>-
(fs[Abbr`bindings`]>> match_mp_tac nsAll_alist_to_ns>>
fs[EVERY_MAP,EVERY_MEM,FORALL_PROD,MEM_ZIP,LENGTH_COUNT_LIST]>>rw[]>>
simp[EL_MAP,LENGTH_COUNT_LIST,EL_COUNT_LIST,check_t_def])
>>
fs[env_rel_def,ienv_ok_def,ienv_val_ok_def] >>
irule nsAll_mono>>
HINT_EXISTS_TAC>>
simp[FORALL_PROD]>>
metis_tac[check_t_more])>>
fs[Abbr`mapp`] >>
(* properties of infer_e *)
old_drule (el 4 (CONJUNCTS infer_e_check_t))>>
rfs[]>>strip_tac>>
old_drule (el 4 (CONJUNCTS infer_e_check_s))>>
disch_then(qspec_then`0` mp_tac)>>
impl_tac>-
fs[ienv_ok_def,init_infer_state_def,check_s_def,env_rel_def]>>
strip_tac>>
old_drule (el 4 (CONJUNCTS infer_e_next_uvar_mono))>>
simp[]>>strip_tac>>
old_drule generalise_complete>>fs[]>>
disch_then(qspec_then`st''''.next_uvar` mp_tac)>>
impl_keep_tac
>-
(rw[]
>- metis_tac [pure_add_constraints_wfs, infer_e_wfs, infer_st_rewrs]
>-
(match_mp_tac pure_add_constraints_check_s>>fs[]>>
ntac 2 HINT_EXISTS_TAC>>rfs[]>>
simp[EVERY_MEM,MEM_ZIP,FORALL_PROD]>>
rw[]
>-
fs[EL_MAP,EL_COUNT_LIST,LENGTH_COUNT_LIST,check_t_def]
>>
metis_tac[EVERY_MEM,MEM_EL])
>>
fs[EVERY_MEM,MEM_MAP,MEM_COUNT_LIST]>>rw[]>>
fs[check_t_def])
>>
rw[]>>
imp_res_tac sub_completion_add_constraints >>
`env_rel_sound last_sub ienv tenv (bind_tvar num_gen Empty)` by
(match_mp_tac env_rel_e_sound_empty_to>>
fs[sub_completion_wfs, env_rel_def]>>
match_mp_tac env_rel_sound_extend_tvs>>fs[t_wfs_def])>>
qabbrev_tac `tenv_v'' = bind_var_list 0 (convert_env last_sub bindings) (bind_tvar num_gen Empty)` >>
`num_tvs tenv_v'' = num_gen` by (unabbrev_all_tac>>fs[bind_tvar_def])>>
old_drule (el 4 (CONJUNCTS infer_e_sound)) >> fs[] >>
qmatch_asmsub_abbrev_tac`sub_completion num_gen _ _ constraints _`>>
disch_then(qspecl_then[`tenv`,`tenv_v''`,`constraints`,`last_sub`] mp_tac)>>fs[]>>
impl_tac>-
(rw[]
>-
fs[ienv_ok_def,env_rel_def]
>>
fs[Abbr`tenv_v''`]>>
match_mp_tac env_rel_sound_merge0>>fs[sub_completion_def]>>
fs[Abbr`bindings`]>>
fs[EVERY_MEM,MEM_ZIP,FORALL_PROD,LENGTH_COUNT_LIST]>>rw[]>>
fs[EL_MAP,LENGTH_COUNT_LIST,EL_COUNT_LIST,check_t_def,SUBSET_DEF])>>
strip_tac>>
fs[sub_completion_def]>>
imp_res_tac pure_add_constraints_apply>>
fs[Abbr`constraints`]>>
rfs[GSYM MAP_MAP_o,MAP_ZIP,LENGTH_COUNT_LIST]>>
rw[Once type_d_cases] >>
imp_res_tac infer_e_next_id_const >>
pop_assum mp_tac \\ rw[Once init_infer_state_def] >>
qexists_tac`convert_env last_sub bindings`>>
qexists_tac`num_tvs tenv_v''`>>fs[Abbr`tenv_v''`]>>
`(MAP2 (λ(x,y,z) t. (x,convert_t (t_walkstar last_sub t))) l funs_ts) = convert_env last_sub bindings` by
(fs[MAP2_MAP,convert_env_def,Abbr`bindings`]>>
match_mp_tac LIST_EQ_MAP_PAIR>>
fs[MAP_MAP_o,o_DEF,LAMBDA_PROD]>>
rw[]
>-
(match_mp_tac LIST_EQ>>fs[LENGTH_COUNT_LIST,EL_MAP,EL_ZIP]>>rw[]>>
pairarg_tac>>fs[])
>>
ntac 3 (pop_assum mp_tac)>>simp[LIST_EQ_REWRITE]>>
fs[LENGTH_COUNT_LIST,EL_MAP,EL_ZIP]>>rw[]>>
pairarg_tac>>fs[])>>
fs[]>>rw[]
>-
(simp[ienv_to_tenv_def, tenv_add_tvs_def, convert_env_def, MAP2_ZIP]>>
pop_assum mp_tac>>
simp[Abbr`bindings`,MAP_MAP_o,o_DEF,MAP2_MAP,LENGTH_COUNT_LIST,LAMBDA_PROD,tenv_add_tvs_def,LIST_EQ_REWRITE,convert_env_def,EL_MAP]>>
rw[]>>
first_x_assum(qspec_then`x` assume_tac)>>
rfs[]>>rpt(pairarg_tac>>fs[])>>
rfs[EL_ZIP,LENGTH_COUNT_LIST,EL_MAP,EL_COUNT_LIST]>>
rw[])
>>
imp_res_tac type_funs_distinct >> fs[FST_triple] >>
imp_res_tac type_funs_MAP_FST >>
imp_res_tac type_funs_Tfn>>
fs[env_rel_def] >>
old_drule (GEN_ALL infer_funs_complete)>>fs[]>>
disch_then (qspecl_then [`tvs'`,`tenv`,`st1`,`<| loc := SOME loc; err := ienv.inf_t |>`,`l`,`bindings'`] assume_tac)>>rfs[]>>
`st'.subst = st'''''.subst` by
metis_tac[pure_add_constraints_functional]>>
simp[LIST_REL_EL_EQN]>>
fs[convert_env_def,tenv_add_tvs_def]>>
CONJ_ASM1_TAC
>-
metis_tac[LENGTH_MAP]>>
rw[EL_MAP]>>
pairarg_tac>>fs[]>>
pairarg_tac>>fs[]>>
pairarg_tac>>fs[]>>
fs[tscheme_inst_def]>>
`t'' = Infer_Tuvar n` by
(pop_assum mp_tac>>fs[Abbr`bindings`]>>
`LENGTH bindings' = LENGTH funs_ts` by metis_tac[LENGTH_MAP]>>
fs[EL_ZIP,LENGTH_MAP,LENGTH_COUNT_LIST,EL_MAP,EL_COUNT_LIST])>>
`LENGTH bindings = LENGTH funs_ts` by metis_tac[LENGTH_MAP]>>
imp_res_tac generalise_subst>>
fs[]>>
(* Rewrite last_sub back into an infer_subst *)
`t_walkstar last_sub t'' = infer_subst s (t_walkstar st'''''.subst t'')` by
(fs[MAP_MAP_o,LIST_EQ_REWRITE,EL_MAP,infer_subst_FEMPTY]>>
pop_assum(qspec_then`n` assume_tac)>>
rfs[EL_COUNT_LIST,EL_MAP])>>
fs[sub_completion_def]>>
Q.ISPECL_THEN [`tvs'`,`s'`] mp_tac (GEN_ALL generalise_subst_exist)>>
impl_tac>-
(fs[]>>
metis_tac[pure_add_constraints_success])>>
rw[]>>
(* This produces the appropriate substitution mentioned above *)
pop_assum (qspecl_then[`MAP (t_walkstar st'''''.subst) (MAP (λn. Infer_Tuvar n) (COUNT_LIST (LENGTH funs_ts)))`,
`[]`,`FEMPTY`,`num_tvs tenv_v''`,`s`,`MAP (t_walkstar last_sub) funs_ts`] mp_tac)>>
fs[]>>
impl_keep_tac
>-
(fs[EVERY_MEM,MEM_MAP,PULL_EXISTS]>>
fs[GSYM FORALL_AND_THM]>>fs[GSYM IMP_CONJ_THM]>>
ntac 2 strip_tac>>
CONJ_ASM2_TAC
>-
metis_tac[check_t_t_vars]
>>
match_mp_tac t_walkstar_check>> fs[]>>
fs[check_t_def,MEM_COUNT_LIST]>>
rw[]
>-
(match_mp_tac (check_s_more3|> SIMP_RULE std_ss[PULL_FORALL,AND_IMP_INTRO])>>
asm_exists_tac>>fs[])
>>
res_tac>>
match_mp_tac (check_t_more5|> CONJUNCT1 |> SIMP_RULE std_ss[PULL_FORALL,AND_IMP_INTRO])>>
asm_exists_tac>>fs[])
>>
rw[]>>
(* Pick the substitution, except turn it into deBruijn vars *)
qexists_tac`MAP convert_t subst'`>>fs[]>>
`check_t 0 (count st'''''.next_uvar) (Infer_Tuvar n)` by
(fs[EVERY_EL]>>
last_x_assum(qspec_then`n` kall_tac)>>
last_x_assum(qspec_then`n` mp_tac)>>
fs[LENGTH_COUNT_LIST,EL_MAP,EL_COUNT_LIST])>>
`check_t (LENGTH subst') {} (infer_subst s (t_walkstar st'''''.subst (Infer_Tuvar n)))` by
(qpat_x_assum `A = infer_subst B C` sym_sub_tac>>
Q.SPECL_THEN [`count (st'''''.next_uvar)`,`last_sub`,`LENGTH subst'`,`Infer_Tuvar n`] mp_tac (check_t_less |> CONJUNCT1 |>GEN_ALL)>>
simp[]>>
rw[]>>
`count st'''''.next_uvar ∩ COMPL(FDOM last_sub) = {}` by
(simp[EXTENSION]>>fs[SUBSET_DEF]>>
metis_tac[])>>
fs[]>>metis_tac[])>>
CONJ_ASM1_TAC>-
metis_tac[check_t_to_check_freevars]>>
CONJ_TAC>-
(fs[EVERY_MAP,EVERY_MEM]>>
metis_tac[check_t_to_check_freevars])>>
imp_res_tac deBruijn_subst_convert>>
pop_assum(qspec_then `subst'`assume_tac)>>fs[]>>
`t = convert_t (t_walkstar s' (Infer_Tuvar n))` by
(rfs[EL_MAP,MAP_MAP_o]>>
qpat_x_assum`MAP SND bindings' = _` mp_tac>>
qpat_x_assum`_ = MAP (t_walkstar st'''''.subst) _` mp_tac>>
simp[LIST_EQ_REWRITE, LENGTH_COUNT_LIST] >>
disch_then(qspec_then`n`mp_tac) \\ simp[EL_MAP, LENGTH_COUNT_LIST, EL_COUNT_LIST]
\\ strip_tac
\\ disch_then(qspec_then`n`mp_tac)
\\ rw[]
\\ metis_tac[pure_add_constraints_success,t_compat_def])>>
simp[]>>
AP_TERM_TAC>>
Q.ISPECL_THEN [`s'`,`s`,`subst'`,`_`,`count st'.next_uvar`] mp_tac (GEN_ALL infer_deBruijn_subst_infer_subst_walkstar)>>
impl_tac>-
(fs[SUBSET_DEF]>>
rw[]>>
fs[IN_FRANGE]>>
metis_tac[pure_add_constraints_wfs])>>
rw[]>>
pop_assum kall_tac>>
pop_assum(qspec_then `t_walkstar st'''''.subst (Infer_Tuvar n)` mp_tac)>>
impl_tac>-
(fs[EXTENSION,SUBSET_DEF]>>
qpat_x_assum`MAP A (MAP B C ) = _ _ funs_ts` sym_sub_tac>>
fs[MEM_MAP,PULL_EXISTS]>>
imp_res_tac ALOOKUP_MEM>>
fs[FORALL_PROD,EXISTS_PROD]>>
CONJ_TAC>-
metis_tac[MEM_COUNT_LIST]>>
reverse CONJ_TAC>-
metis_tac[MEM_COUNT_LIST]
>>
fs[EVERY_MAP,MAP_MAP_o,EVERY_MEM,UNCURRY]>>
match_mp_tac t_walkstar_check>>fs[]>>
CONJ_TAC>-
(match_mp_tac check_s_more5>>
asm_exists_tac>>fs[])
>>
last_x_assum(qspec_then`n` mp_tac)>>fs[MEM_COUNT_LIST]>>
strip_tac>>
imp_res_tac check_t_more5>>
fs[SUBSET_DEF,EXTENSION])
>>
rw[]>>
metis_tac[pure_add_constraints_wfs,t_walkstar_SUBMAP,pure_add_constraints_success]
)
>- (
(* Dtype *)
rw[infer_d_def,success_eqns]>>
simp[Once type_d_cases]>>
simp[set_ids_eq]>>
qmatch_goalsub_abbrev_tac`set ls = _`>>
qexists_tac`ls`>>
fs[Abbr`ls`,n_fresh_id_def]>>
rveq>>fs[ienv_to_tenv_def]>>
fs[env_rel_def,env_rel_sound_def]>>
simp[ALL_DISTINCT_GENLIST]>>
fs[DISJOINT_DEF,EXTENSION,MEM_MAP,MEM_GENLIST]>>
pop_assum mp_tac>>
EVAL_TAC>>fs[])
>- (
(* Dtabbrev *)
rw[infer_d_def,success_eqns]>>
imp_res_tac type_name_check_subst_thm >>
imp_res_tac type_name_check_subst_state >>
fs [] >>
simp[Once type_d_cases]>>
fs[set_ids_def,ienv_to_tenv_def,env_rel_def, env_rel_sound_def] >>
metis_tac [])
>- (
(* Dexn *)
rw[infer_d_def,success_eqns]>>
imp_res_tac type_name_check_subst_thm >>
imp_res_tac type_name_check_subst_state >>
fs [] >>
simp[Once type_d_cases]>>
fs[set_ids_def,ienv_to_tenv_def,env_rel_def, env_rel_sound_def]>>
metis_tac[ETA_AX])
>- (
(* Dmod *)
rw[infer_d_def,success_eqns]>>
simp[Once type_d_cases]>>
first_x_assum old_drule>> disch_then old_drule>>fs[]>> strip_tac>>
HINT_EXISTS_TAC>>fs[]>>
simp[ienv_to_tenv_def,tenvLift_def,lift_ienv_def,nsLift_nsMap])
>- (
(* Dlocal *)
rw[infer_d_def, success_eqns]
>> rpt (first_x_assum old_drule >> rw [])
>> simp[Once type_d_cases]
>> goal_assum(first_assum o mp_then Any mp_tac)
>> str_tac [set_ids_eq_union_eq]
>> fs []
>> imp_res_tac infer_d_next_id_mono
>> fs []
>> first_x_assum (fn a =>
CHANGED_TAC (str_tac [a, env_rel_extend, env_rel_ienv_to_tenv]))
>> fs []
>> conj_tac
>- metis_tac [env_rel_def, infer_d_check]
>> fs [set_ids_def,EXTENSION,DISJOINT_DEF]
)
>- (
rename [`Denv`]
>> fs [infer_d_def,failwith_def])
>- (
(* infer_ds [] *)
fs[infer_d_def,success_eqns,env_rel_def]>>
rw[] >> EVAL_TAC)
>- (
(* infer_ds (d::ds) *)
rw[]>>
fs[infer_d_def,success_eqns]>>
rename1 `infer_d ienv1 _ _ = (Success ienv2, sti)` >>
rename1 `infer_ds _ _ _ = (Success ienv3, _)` >>
rpt(first_x_assum old_drule)>>
rpt(disch_then old_drule)>>
strip_tac>>strip_tac>>
simp[Once type_d_cases] >>
rw[]>>
qexists_tac `ienv_to_tenv ienv2` >>
qexists_tac `ienv_to_tenv ienv3` >>
qexists_tac`set_ids st1.next_id sti.next_id`>>
qexists_tac`set_ids sti.next_id st2.next_id`>>
imp_res_tac infer_d_next_id_mono>>
fs[ienv_to_tenv_extend]>>
rw[]
>-
fs[set_ids_def,EXTENSION]
>- (
first_x_assum match_mp_tac>>fs[]>>
match_mp_tac env_rel_extend>>fs[]>>
match_mp_tac env_rel_ienv_to_tenv>>fs[]>>
fs[env_rel_def]>>
metis_tac[infer_d_check])
>>
fs[set_ids_def,EXTENSION,DISJOINT_DEF])
QED
Theorem db_subst_infer_subst_swap2:
(!t s tvs uvar n.
t_wfs s ∧
check_t tvs {} t
⇒
(convert_t
(t_walkstar s
(infer_deBruijn_subst
(MAP (λn. Infer_Tuvar n) (COUNT_LIST tvs))
t)) =
deBruijn_subst 0
(MAP (convert_t o t_walkstar s)
(MAP (λn. Infer_Tuvar n) (COUNT_LIST tvs)))
(convert_t t))) ∧
(!ts s tvs uvar n.
t_wfs s ∧
EVERY (\t. check_t tvs {} t) ts ⇒
(MAP (convert_t o
t_walkstar s o
infer_deBruijn_subst (MAP (λn. Infer_Tuvar n) (COUNT_LIST tvs)))
ts =
MAP (deBruijn_subst 0 (MAP (convert_t o t_walkstar s) (MAP (λn. Infer_Tuvar n) (COUNT_LIST tvs))) o
convert_t)
ts))
Proof
ho_match_mp_tac infer_t_induction >>
rw [convert_t_def, deBruijn_subst_def, EL_MAP, t_walkstar_eqn1,
infer_deBruijn_subst_alt, MAP_MAP_o, combinTheory.o_DEF, check_t_def,
LENGTH_COUNT_LIST]
QED
(*
Theorem check_tscheme_inst_sound:
!tvs_impl t_impl tvs_spec t_spec.
check_t tvs_impl {} t_impl ∧
check_t tvs_spec {} t_spec ∧
check_tscheme_inst x (tvs_spec,t_spec) (tvs_impl,t_impl)
⇒
tscheme_inst (tvs_spec, convert_t t_spec) (tvs_impl, convert_t t_impl)
Proof
rw [check_tscheme_inst_def, tscheme_inst_def] >>
every_case_tac >>
fs [success_eqns] >>
rw [] >>
fs [init_state_def, init_infer_state_def] >>
var_eq_tac >>
fs [] >>
`t_wfs FEMPTY` by rw [t_wfs_def] >>
old_drule t_unify_apply >>
disch_then old_drule >>
rw [] >>
old_drule t_unify_wfs >>
disch_then old_drule >>
strip_tac >>
`t_walkstar s t_spec = t_spec` by metis_tac [t_walkstar_no_vars] >>
fs [] >>
rw [db_subst_infer_subst_swap2] >>
rw [MAP_MAP_o, combinTheory.o_DEF] >>
`?s'. ALL_DISTINCT (MAP FST s') ∧
(FEMPTY |++ s' = FUN_FMAP (\x. Infer_Tapp [] Tc_int) (count tvs_impl DIFF FDOM s))`
by metis_tac [fmap_to_list] >>
`FINITE (count tvs_impl DIFF FDOM s)` by metis_tac [FINITE_COUNT, FINITE_DIFF] >>
`t_wfs (s |++ s')`
by (
`t_vR s = t_vR (s |++ s')`
by (
rw [t_vR_eqn, FUN_EQ_THM] >>
cases_on `FLOOKUP (s |++ s') x'` >>
fs [flookup_update_list_none, flookup_update_list_some] >>
cases_on `FLOOKUP s x'` >>
fs [flookup_update_list_none, flookup_update_list_some] >>
`FLOOKUP (FEMPTY |++ s') x' = SOME x''` by rw [flookup_update_list_some] >>
pop_assum mp_tac >>
rw [FLOOKUP_FUN_FMAP, t_vars_eqn] >>
rw [FLOOKUP_FUN_FMAP, t_vars_eqn] >>
fs [FLOOKUP_DEF]) >>
fs [t_wfs_eqn]) >>
qexists_tac `MAP (\n. convert_t (t_walkstar (s |++ s') (Infer_Tuvar n))) (COUNT_LIST tvs_impl)` >>
rw [LENGTH_COUNT_LIST, check_t_to_check_freevars, EVERY_MAP] >>
`FDOM (FEMPTY |++ s') = count tvs_impl DIFF FDOM s` by metis_tac [FDOM_FMAP] >>
`check_s tvs_spec (count tvs_impl) s`
by (
old_drule t_unify_check_s >>
simp [] >>
disch_then irule >>
simp [check_s_def, check_t_infer_db_subst2] >>
metis_tac [check_t_more, check_t_more2, arithmeticTheory.ADD_COMM])
>- (
rw [EVERY_MEM] >>
irule check_t_to_check_freevars >>
irule t_walkstar_check >>
simp [check_t_def, FDOM_FUPDATE_LIST]
>> conj_tac >- (
fs [check_s_def, fdom_fupdate_list2] >>
rw [] >>
rw [FUPDATE_LIST_APPLY_NOT_MEM] >>
`count tvs_impl ⊆ FDOM s ∪ set (MAP FST s')` by rw [SUBSET_DEF]
>- metis_tac [check_t_more5]
>- metis_tac [check_t_more5] >>
`FLOOKUP (s |++ s') uv = SOME ((s |++ s') ' uv)`
by rw [FLOOKUP_DEF, FDOM_FUPDATE_LIST] >>
fs [flookup_update_list_some]
>- (
imp_res_tac ALOOKUP_MEM >>
fs[] >>
imp_res_tac (GSYM mem_to_flookup) >>
fs[] >>
ntac 2 (pop_assum mp_tac) >>
rw [FLOOKUP_FUN_FMAP] >>
rw [check_t_def])
>- (
pop_assum mp_tac >>
rw [FLOOKUP_DEF]))
>- (
fs [EXTENSION, MEM_COUNT_LIST] >>
res_tac >>
fs [FDOM_FUPDATE_LIST]))
>- (
imp_res_tac t_walkstar_no_vars >>
fs [] >>
rw [SIMP_RULE (srw_ss()) [MAP_MAP_o, combinTheory.o_DEF] (GSYM db_subst_infer_subst_swap2)] >>
AP_TERM_TAC >>
simp[MAP_GENLIST,COUNT_LIST_GENLIST,ETA_AX] >>
match_mp_tac (SIMP_RULE (srw_ss()) [GSYM RIGHT_FORALL_IMP_THM,AND_IMP_INTRO] no_vars_extend_subst) >>
rw []
>- (
rw [DISJOINT_DEF, EXTENSION] >>
metis_tac [])
>- (
imp_res_tac check_t_t_vars >>
fs [EXTENSION, SUBSET_DEF, COUNT_LIST_GENLIST, MAP_GENLIST] >>
metis_tac []))
QED
Theorem weak_tenv_ienv_to_tenv:
!ienv1 ienv2.
ienv_ok {} ienv1 ∧ ienv_ok {} ienv2 ∧
check_weak_ienv ienv1 ienv2 ⇒ weak_tenv (ienv_to_tenv ienv1) (ienv_to_tenv ienv2)
Proof
rw [check_weak_ienv_def, weak_tenv_def, ienv_to_tenv_def, GSYM nsSub_compute_thm] >>
simp [nsSub_nsMap] >>
fs [tscheme_inst2_def] >>
irule nsSub_mono2 >>
rw [] >>
HINT_EXISTS_TAC >>
rw [] >>
pairarg_tac >>
fs [] >>
pairarg_tac >>
fs [] >>
rw [] >>
fs [ienv_ok_def, ienv_val_ok_def] >>
old_drule nsLookup_nsAll >>
disch_then old_drule >>
rw [] >>
qpat_x_assum `nsAll _ ienv2.inf_v` mp_tac >>
old_drule nsLookup_nsAll >>
disch_then old_drule >>
rw [] >>
metis_tac [check_tscheme_inst_sound]
QED
Theorem weak_decls_ienv_to_tenv:
!idecls1 idecls2.
check_weak_decls idecls1 idecls2 ⇒ weak_decls (convert_decls idecls1) (convert_decls idecls2)
Proof
rw [check_weak_decls_def, weak_decls_def, convert_decls_def, SUBSET_DEF,
EVERY_MEM, list_subset_def, list_set_eq_def, EXTENSION] >>
metis_tac []
QED
Triviality check_freevars_nub:
(!t x fvs.
check_freevars x fvs t ⇒
check_freevars x (nub fvs) t) ∧
(!ts x fvs.
EVERY (check_freevars x fvs) ts ⇒
EVERY (check_freevars x (nub fvs)) ts)
Proof
Induct >>
rw [check_freevars_def] >> metis_tac[]
QED
Triviality check_specs_sound:
!mn tenvT idecls1 ienv1 specs st1 idecls2 ienv2 st2.
check_specs mn tenvT idecls1 ienv1 specs st1 = (Success (idecls2,ienv2), st2) ∧
tenv_abbrev_ok tenvT
⇒
?decls3 ienv3.
type_specs mn tenvT specs decls3 (ienv_to_tenv ienv3) ∧
convert_decls idecls2 = union_decls decls3 (convert_decls idecls1) ∧
ienv2 = extend_dec_ienv ienv3 ienv1
Proof
ho_match_mp_tac check_specs_ind >>
rw [check_specs_def, success_eqns]
>- (
rw [Once type_specs_cases] >>
qexists_tac `<|inf_v := nsEmpty; inf_c := nsEmpty; inf_t := nsEmpty|>` >>
rw [ienv_to_tenv_def, extend_dec_ienv_def, inf_env_component_equality])
>- (
first_x_assum old_drule >>
rw [] >>
qexists_tac `decls3` >>
qmatch_assum_abbrev_tac
`check_specs _ _ _ (ienv1 with inf_v := nsBind name new_binding ienv1.inf_v) _ _ = _` >>
simp [Once type_specs_cases] >>
qexists_tac `ienv3 with inf_v := nsAppend ienv3.inf_v (nsSing name new_binding)` >>
rw [extend_dec_ienv_def, extend_dec_tenv_def, ienv_to_tenv_def, nsMap_nsAppend,
nsAppend_nsSing]
>- (
HINT_EXISTS_TAC >>
rw [ienv_to_tenv_def] >>
unabbrev_all_tac >>
fs [] >>
qexists_tac `nub fvs` >>
conj_asm2_tac
>- (
rpt AP_TERM_TAC >>
old_drule check_freevars_type_name_subst >>
disch_then old_drule >>
disch_then old_drule >>
rw [convert_t_subst, LENGTH_COUNT_LIST, MAP_MAP_o, combinTheory.o_DEF,
convert_t_def, MAP_GENLIST, COUNT_LIST_GENLIST])
>- metis_tac [t_to_freevars_check, check_freevars_nub])
>- metis_tac [GSYM nsAppend_assoc, nsAppend_nsSing])
>- (
first_x_assum old_drule >>
impl_tac
>- (
irule tenv_abbrev_ok_merge >>
simp [] >>
rw [typeSoundInvariantsTheory.tenv_abbrev_ok_def] >>
irule nsAll_alist_to_ns >>
simp [EVERY_MAP] >>
rw [EVERY_MEM] >>
pairarg_tac >>
simp [] >>
pairarg_tac >>
simp [] >>
pairarg_tac >>
fs [] >>
rw [check_freevars_def, EVERY_MAP, EVERY_MEM]) >>
rw [] >>
simp [Once type_specs_cases, PULL_EXISTS] >>
qmatch_assum_abbrev_tac
`check_specs _ _ _ <| inf_v := _;
inf_c := nsAppend new_ctors _;
inf_t := nsAppend new_tdefs _ |> _ _ = _` >>
qexists_tac `extend_dec_ienv ienv3 <| inf_v := nsEmpty; inf_c := new_ctors;
inf_t := new_tdefs |>` >>
qexists_tac `ienv_to_tenv ienv3` >>
qexists_tac `decls3` >>
rw [ienv_to_tenv_def, extend_dec_ienv_def, extend_dec_tenv_def] >>
rw [union_decls_def, convert_decls_def] >>
rw [EXTENSION] >>
metis_tac [])
>- (
simp [Once type_specs_cases, PULL_EXISTS] >>
first_x_assum (qspec_then `nsBind tn (tvs,type_name_subst tenvT t) nsEmpty` mp_tac) >>
simp [] >>
disch_then old_drule >>
impl_tac
>- (
fs [typeSoundInvariantsTheory.tenv_abbrev_ok_def] >>
irule nsAll_nsBind >>
simp [] >>
irule check_freevars_type_name_subst >>
simp [typeSoundInvariantsTheory.tenv_abbrev_ok_def]) >>
rw [] >>
qmatch_assum_abbrev_tac
`check_specs _ _ _ (ienv1 with inf_t := nsBind name new_t _) _ _ = _` >>
qexists_tac `decls3` >>
qexists_tac `ienv3 with inf_t := nsAppend ienv3.inf_t (nsSing name new_t)` >>
qexists_tac `ienv_to_tenv ienv3` >>
rw [ienv_to_tenv_def, extend_dec_ienv_def, extend_dec_tenv_def] >>
metis_tac [nsAppend_nsSing, nsAppend_assoc])
>- (
first_x_assum old_drule >>
rw [] >>