-
Notifications
You must be signed in to change notification settings - Fork 4
/
pure_letrec_delargScript.sml
2379 lines (2285 loc) · 82.7 KB
/
pure_letrec_delargScript.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
(*
Prove that unused argument can be deleted
*)
open HolKernel Parse boolLib bossLib term_tactic;
open fixedPointTheory arithmeticTheory listTheory stringTheory alistTheory
optionTheory pairTheory ltreeTheory llistTheory bagTheory dep_rewrite
BasicProvers pred_setTheory relationTheory rich_listTheory finite_mapTheory;
open pure_expTheory pure_valueTheory pure_evalTheory pure_eval_lemmasTheory
pure_exp_lemmasTheory pure_limitTheory pure_exp_relTheory
pure_alpha_equivTheory pure_miscTheory pure_congruenceTheory
pure_letrec_specTheory pure_inline_relTheory;
val _ = new_theory "pure_letrec_delarg";
Datatype:
info = <| fname : string ; (* function name *)
args : string list ; (* arguments up to argument to delete *)
arg : string ; (* argument to delete *)
w_arg : string ; (* argument to delete is this before deletion *)
args' : string list ; (* arguments after argument to delete *)
rhs_T : exp ; (* without argument *)
rhs_F : exp |> (* with argument *)
End
Definition mk_apps_def:
mk_apps b f xs v ys = Apps f (xs ++ (if b then [] else [v]) ++ ys)
End
Inductive remove_call_arg:
[~Apps_Var:]
(∀info xs ys xs1 ys1 R b1 b2.
LENGTH xs = LENGTH info.args ∧
LENGTH xs1 = LENGTH info.args' ∧
LIST_REL (remove_call_arg b1 b2 T info R) xs ys ∧
LIST_REL (remove_call_arg b1 b2 T info R) xs1 ys1 ⇒
remove_call_arg b1 b2 T info R (mk_apps b1 (Var info.fname) xs (Var info.w_arg) xs1)
(mk_apps b2 (Var info.fname) ys (Var info.w_arg) ys1))
[~Apps_Const:]
(∀info xs ys xs1 ys1 R c.
LENGTH xs = LENGTH info.args ∧
LENGTH xs1 = LENGTH info.args' ∧ closed c ∧
LIST_REL (remove_call_arg b1 b2 F info R) xs ys ∧
LIST_REL (remove_call_arg b1 b2 F info R) xs1 ys1 ⇒
remove_call_arg b1 b2 F info R (mk_apps b1 (Var info.fname) xs c xs1)
(mk_apps b2 (Var info.fname) ys c ys1))
[~Var:]
(∀info n b R.
n ≠ info.arg ∧ n ≠ info.fname ⇒
remove_call_arg b1 b2 b info R (Var n) (Var n))
[~Lam:]
(∀info n x y b R.
remove_call_arg b1 b2 b info R x y ∧
info.arg ≠ n ∧ info.w_arg ≠ n ∧ info.fname ≠ n ⇒
remove_call_arg b1 b2 b info R (Lam n x) (Lam n y))
[~App:]
(∀info f x g y b R.
remove_call_arg b1 b2 b info R f g ∧
remove_call_arg b1 b2 b info R x y ⇒
remove_call_arg b1 b2 b info R (App f x) (App g y))
[~Prim:]
(∀info n xs ys b R.
LIST_REL (remove_call_arg b1 b2 b info R) xs ys ⇒
remove_call_arg b1 b2 b info R (Prim n xs) (Prim n ys))
[~Letrec:]
(∀info xs x ys y b R.
LIST_REL (remove_call_arg b1 b2 b info R) (MAP SND xs) (MAP SND ys) ∧
DISJOINT {info.arg; info.fname; info.w_arg} (set (MAP FST xs)) ∧
MAP FST xs = MAP FST ys ∧
remove_call_arg b1 b2 b info R x y ⇒
remove_call_arg b1 b2 b info R (Letrec xs x) (Letrec ys y))
[~closed:]
(∀info b c1 c2 R.
closed c1 ∧ closed c2 ∧ R c1 c2 ⇒
remove_call_arg b1 b2 b info R c1 c2)
End
Definition info_ok_def:
info_ok i ⇔
(i.args = [] ⇒ i.args' ≠ []) ∧
i.arg ∉ freevars i.rhs_F ∧
i.arg ∉ freevars i.rhs_T ∧
ALL_DISTINCT (i.fname::i.arg::i.w_arg::i.args ++ i.args') ∧
remove_call_arg F T F i (=) i.rhs_F i.rhs_T
End
Definition mk_fun_def:
mk_fun b i =
Lams (i.args ++ (if b then [] else [i.arg]) ++ i.args')
(if b then i.rhs_T else i.rhs_F)
End
Definition mk_letrec_def:
mk_letrec b i x = Letrec [(i.fname,mk_fun b i)] x
End
Definition mk_rec_def:
mk_rec b i = mk_letrec b i (mk_fun b i)
End
Theorem remove_call_arg_mono[mono]:
(∀x y. R1 x y ⇒ R2 x y) ⇒
remove_call_arg b1 b2 b i R1 x y ⇒
remove_call_arg b1 b2 b i R2 x y
Proof
qsuff_tac ‘
∀b1 b2 b i R1 x y.
remove_call_arg b1 b2 b i R1 x y ⇒
(∀x y. R1 x y ⇒ R2 x y) ⇒
remove_call_arg b1 b2 b i R2 x y’
>- metis_tac []
\\ ho_match_mp_tac remove_call_arg_ind
\\ rpt strip_tac
\\ simp [Once remove_call_arg_cases]
\\ gvs [SF SFY_ss,SF ETA_ss]
\\ metis_tac []
QED
Inductive letrec_delarg:
[~Switch:]
(∀info x b1 b2.
remove_call_arg b1 b2 F info (letrec_delarg info) x y ∧
info_ok info ∧
freevars (mk_fun b1 info) ⊆ {info.fname} ∧
freevars (mk_fun b2 info) ⊆ {info.fname} ∧
letrec_delarg info x y ⇒
letrec_delarg info (mk_letrec b1 info x)
(mk_letrec b2 info y))
[~Apps:]
(∀info xs ys b1 b2 c.
LENGTH xs = LENGTH info.args ∧
LENGTH xs1 = LENGTH info.args' ∧ info_ok info ∧
LIST_REL (letrec_delarg info) xs ys ∧
LIST_REL (letrec_delarg info) xs1 ys1 ∧ closed c ∧
closed (mk_rec b1 info) ∧
closed (mk_rec b2 info) ⇒
letrec_delarg info
(mk_apps b1 (mk_rec b1 info) xs c xs1)
(mk_apps b2 (mk_rec b2 info) ys c ys1))
[~Var:]
(∀info n.
letrec_delarg info (Var n) (Var n))
[~Lam:]
(∀info n x y.
letrec_delarg info x y ⇒
letrec_delarg info (Lam n x) (Lam n y))
[~App:]
(∀info f g x y.
letrec_delarg info f g ∧ letrec_delarg info x y ⇒
letrec_delarg info (App f x) (App g y))
[~Prim:]
(∀info n xs ys.
LIST_REL (letrec_delarg info) xs ys ⇒
letrec_delarg info (Prim n xs) (Prim n ys))
[~Letrec:]
(∀info xs ys x y.
LIST_REL (letrec_delarg info) (MAP SND xs) (MAP SND ys) ∧
MAP FST xs = MAP FST ys ∧ letrec_delarg info x y ⇒
letrec_delarg info (Letrec xs x) (Letrec ys y))
End
Theorem letrec_delarg_refl:
∀i x. letrec_delarg i x x
Proof
gen_tac \\ ho_match_mp_tac freevars_ind
\\ rw [] \\ simp [Once letrec_delarg_cases]
\\ rpt disj2_tac
>- (Induct_on ‘es’ \\ fs [])
\\ Induct_on ‘lcs’ \\ fs [FORALL_PROD,SF DNF_ss]
\\ rw [] \\ res_tac \\ fs []
QED
Theorem remove_call_arg_sym:
∀b1 b2 b i R x y.
remove_call_arg b1 b2 b i R x y ⇒ remove_call_arg b2 b1 b i (λx y. R y x) y x
Proof
ho_match_mp_tac remove_call_arg_ind
\\ rpt strip_tac
\\ simp [Once remove_call_arg_cases]
\\ once_rewrite_tac [LIST_REL_SWAP] \\ fs []
\\ disj1_tac
\\ irule_at (Pos hd) EQ_REFL
\\ irule_at (Pos hd) EQ_REFL
\\ imp_res_tac LIST_REL_LENGTH
\\ fs [] \\ pop_assum $ irule_at Any
QED
Triviality LIST_REL_ignore_first:
∀xs ys. LIST_REL (λx y. P x) xs ys ⇔ EVERY P xs ∧ LENGTH xs = LENGTH ys
Proof
Induct \\ fs [PULL_EXISTS] \\ rw [] \\ eq_tac \\ rw [] \\ res_tac
\\ gvs [] \\ Cases_on ‘ys’ \\ gvs []
QED
Theorem remove_call_arg_dup:
∀b1 b2 b i R x y.
remove_call_arg b1 b2 b i R x y ⇒ remove_call_arg b1 b1 b i $= x x
Proof
ho_match_mp_tac remove_call_arg_ind
\\ rpt strip_tac
\\ simp [Once remove_call_arg_cases]
\\ gvs [LIST_REL_ignore_first]
\\ gvs [LIST_REL_same]
\\ disj1_tac
\\ gvs [LIST_REL_ignore_first]
\\ irule_at (Pos hd) EQ_REFL
\\ irule_at (Pos hd) EQ_REFL
\\ imp_res_tac LIST_REL_LENGTH
\\ gvs [LIST_REL_same]
QED
Theorem letrec_delarg_sym:
letrec_delarg i x y ⇔ letrec_delarg i y x
Proof
qsuff_tac ‘∀i x y. letrec_delarg i x y ⇒ letrec_delarg i y x’
>- metis_tac []
\\ ho_match_mp_tac letrec_delarg_ind
\\ rw []
\\ imp_res_tac LIST_REL_LENGTH
>- (irule letrec_delarg_Switch \\ fs []
\\ imp_res_tac remove_call_arg_sym \\ gvs [SF ETA_ss])
>- (irule letrec_delarg_Apps \\ fs []
\\ simp [Once LIST_REL_SWAP]
\\ simp [Once LIST_REL_SWAP])
>- (irule letrec_delarg_Var \\ fs [])
>- (irule letrec_delarg_Lam \\ fs [])
>- (irule letrec_delarg_App \\ fs [])
>- (irule letrec_delarg_Prim \\ fs [] \\ simp [Once LIST_REL_SWAP])
>- (irule letrec_delarg_Letrec \\ fs [] \\ simp [Once LIST_REL_SWAP])
QED
Triviality mk_letrec_neq[simp]:
mk_letrec b i x ≠ Lam v t ∧
mk_letrec b i x ≠ App x1 x2 ∧
mk_letrec b i x ≠ Prim p ps ∧
∀y. Lam v z ≠ Apps y (xs ++ [x]) ∧
Letrec bs bb ≠ Apps y (xs ++ [x]) ∧
Prim p ps ≠ Apps y (xs ++ [x])
Proof
fs [mk_letrec_def]
\\ Induct_on ‘xs’
\\ fs [Apps_def]
QED
Triviality EVERY_FLOOKUP_closed_lemma:
EVERY (λe. freevars e ⊆ set (MAP FST ys)) (MAP SND ys) ⇒
(∀n v.
FLOOKUP (FEMPTY |++ MAP (λ(g,x). (g,Letrec ys x)) ys) n = SOME v ⇒
closed v)
Proof
fs [alistTheory.flookup_fupdate_list,AllCaseEqs()]
\\ rw [] \\ imp_res_tac ALOOKUP_MEM
\\ gvs [MEM_MAP,EXISTS_PROD,EVERY_MEM,PULL_EXISTS]
\\ res_tac \\ fs []
QED
Triviality LIST_REL_freevars_lemma_1:
∀xs ys.
LIST_REL (λx y. freevars x = freevars y) xs ys ⇒
MAP freevars xs = MAP freevars ys
Proof
Induct \\ fs [PULL_EXISTS]
QED
Theorem remove_call_arg_freevars:
∀b1 b2 b i R x y.
remove_call_arg b1 b2 b i R x y ⇒ ~b ⇒ freevars x = freevars y
Proof
ho_match_mp_tac remove_call_arg_ind
\\ rpt strip_tac \\ gvs []
\\ gvs [closed_def]
\\ imp_res_tac LIST_REL_freevars_lemma_1 \\ gvs [mk_apps_def,SF ETA_ss]
\\ rw [] \\ gvs []
\\ gvs [MAP_MAP_o,combinTheory.o_DEF,LAMBDA_PROD]
\\ AP_THM_TAC
\\ ntac 4 AP_TERM_TAC
\\ pop_assum mp_tac
\\ qid_spec_tac ‘ys’
\\ qid_spec_tac ‘xs’
\\ Induct \\ gvs [FORALL_PROD,MAP_EQ_CONS,PULL_EXISTS]
QED
Theorem free_vars_mk_fun_subset:
info_ok i ⇒
freevars (mk_fun b2 i) ⊆ freevars (mk_fun b1 i)
Proof
strip_tac
\\ fs [mk_fun_def,info_ok_def]
\\ rpt IF_CASES_TAC \\ gvs []
\\ DEP_REWRITE_TAC [freevars_subst]
\\ fs [SUBSET_DEF]
\\ fs [FRANGE_DEF]
\\ gvs [SUBSET_DEF]
\\ rpt strip_tac
\\ CCONTR_TAC \\ gvs []
\\ drule remove_call_arg_freevars \\ gvs []
\\ CCONTR_TAC \\ gvs []
QED
Theorem free_vars_mk_fun:
info_ok i ⇒
freevars (mk_fun b i) = freevars (mk_fun T i)
Proof
strip_tac
\\ imp_res_tac free_vars_mk_fun_subset
\\ gvs [SUBSET_DEF,EXTENSION]
\\ metis_tac []
QED
Triviality LIST_REL_freevars_lemma:
∀xs ys.
LIST_REL (λx y. letrec_delarg i x y ∧ freevars x = freevars y) xs ys ⇒
MAP freevars xs = MAP freevars ys
Proof
Induct \\ fs [PULL_EXISTS]
QED
Theorem letrec_delarg_freevars:
∀i x y. letrec_delarg i x y ⇒ freevars x = freevars y
Proof
Induct_on ‘letrec_delarg’ \\ rw [] \\ gvs []
>- (fs [mk_letrec_def,mk_rec_def] \\ metis_tac [free_vars_mk_fun])
>- (gvs [mk_apps_def,closed_def]
\\ imp_res_tac LIST_REL_freevars_lemma \\ gvs []
\\ rw [] \\ gvs [])
>- (imp_res_tac LIST_REL_freevars_lemma \\ gvs [SF ETA_ss])
\\ last_x_assum mp_tac
\\ qid_spec_tac ‘xs’
\\ qid_spec_tac ‘ys’
\\ Induct \\ fs []
\\ fs [PULL_EXISTS]
\\ strip_tac \\ Cases \\ fs []
\\ strip_tac \\ res_tac \\ fs [UNCURRY]
\\ gvs [EXTENSION]
\\ metis_tac []
QED
Theorem subst_mk_apps:
subst m1 (mk_apps b1 f xs c xs1) =
mk_apps b1 (subst m1 f) (MAP (subst m1) xs) (subst m1 c) (MAP (subst m1) xs1)
Proof
fs [mk_apps_def,subst_Apps] \\ rw [] \\ fs []
QED
Theorem closed_mk_apps:
closed (mk_apps b1 f xs c xs1) ⇔
EVERY closed xs ∧ EVERY closed xs1 ∧ closed f ∧ (~b1 ⇒ closed c)
Proof
fs [mk_apps_def] \\ rw [] \\ fs [] \\ eq_tac \\ gvs []
QED
Theorem subst_remove_call_arg:
∀b1 b2 b i R x y.
remove_call_arg b1 b2 b i R x y ⇒
∀m1 m2.
R = letrec_delarg i ∧ ~b ∧
FDOM m1 = FDOM m2 ∧
i.fname ∉ FDOM m2 ∧
(∀k. k ∈ FDOM m2 ⇒
letrec_delarg i (m1 ' k) (m2 ' k) ∧ closed (m1 ' k) ∧
closed (m2 ' k)) ⇒
remove_call_arg b1 b2 F i (letrec_delarg i) (subst m1 x) (subst m2 y)
Proof
ho_match_mp_tac remove_call_arg_ind
\\ rpt strip_tac \\ gvs []
>-
(fs [subst_Apps]
\\ fs [subst_def,FLOOKUP_DEF,subst_mk_apps]
\\ irule remove_call_arg_Apps_Const \\ fs []
\\ rw []
\\ simp [LIST_REL_MAP]
\\ first_x_assum (fn th => mp_tac th \\ match_mp_tac LIST_REL_mono)
\\ rw [] \\ gvs [SF SFY_ss])
>-
(fs [subst_def,FLOOKUP_DEF]
\\ IF_CASES_TAC \\ fs []
>- (irule remove_call_arg_closed \\ res_tac \\ fs [])
\\ irule remove_call_arg_Var \\ fs [])
>-
(fs [subst_def] \\ irule remove_call_arg_Lam \\ fs []
\\ last_x_assum irule
\\ gvs [FDOM_DOMSUB,DOMSUB_FAPPLY_THM])
>-
(fs [subst_def] \\ irule remove_call_arg_App \\ fs [])
>-
(fs [subst_def] \\ irule remove_call_arg_Prim \\ fs [LIST_REL_MAP]
\\ last_x_assum mp_tac
\\ match_mp_tac LIST_REL_mono \\ fs [])
>-
(fs [subst_def] \\ irule remove_call_arg_Letrec \\ fs [LIST_REL_MAP]
\\ gvs [MAP_MAP_o,combinTheory.o_DEF,LAMBDA_PROD,FST_INTRO]
\\ reverse conj_tac
>-
(first_x_assum $ irule
\\ gvs [FDIFF_def,DRESTRICT_DEF])
\\ last_x_assum mp_tac
\\ match_mp_tac LIST_REL_mono
\\ gvs [FORALL_PROD]
\\ rw []
\\ first_x_assum $ irule
\\ gvs [FDIFF_def,DRESTRICT_DEF])
\\ irule remove_call_arg_closed \\ fs []
QED
Theorem boundvars_Apps:
∀es e. boundvars (Apps e es) = boundvars e ∪ BIGUNION (set (MAP boundvars es))
Proof
Induct \\ fs [Apps_def, AC UNION_COMM UNION_ASSOC]
QED
Theorem subst_remove_call_arg_F_F:
∀b1 b2 b i R x y.
remove_call_arg b1 b2 b i R x y ⇒
∀m1 m2.
R = letrec_delarg i ∧ ~b ∧
i.fname ∉ FDOM m1 ∧
(∀k. k ≠ i.arg ⇒ (k ∈ FDOM m1 <=> k ∈ FDOM m2)) ∧
info_ok i ∧
(∀k. k ∈ FDOM m2 ∧ k ≠ i.arg ⇒
letrec_delarg i (m1 ' k) (m2 ' k) ∧ closed (m1 ' k) ∧
closed (m2 ' k)) ⇒
remove_call_arg b1 b2 F i (letrec_delarg i) (subst m1 x) (subst m2 y)
Proof
ho_match_mp_tac remove_call_arg_ind
\\ rpt strip_tac \\ gvs []
>-
(fs [subst_Apps]
\\ ‘i.fname ∉ FDOM m2’ by (fs [info_ok_def] \\ metis_tac [])
\\ fs [subst_def,FLOOKUP_DEF,subst_mk_apps]
\\ irule remove_call_arg_Apps_Const \\ fs [info_ok_def]
\\ simp [LIST_REL_MAP] \\ rw []
\\ first_x_assum (fn th => mp_tac th \\ match_mp_tac LIST_REL_mono)
\\ rw [] \\ gvs [SF SFY_ss]
\\ first_x_assum $ irule \\ fs [boundvars_Apps,MEM_MAP]
\\ metis_tac [])
>-
(fs [subst_def,FLOOKUP_DEF]
\\ ‘n ∈ FDOM m1 <=> n ∈ FDOM m2’ by (fs [EXTENSION] \\ metis_tac [])
\\ IF_CASES_TAC \\ fs []
>- (irule remove_call_arg_closed \\ res_tac \\ fs [])
\\ irule remove_call_arg_Var \\ fs [])
>-
(fs [subst_def] \\ irule remove_call_arg_Lam \\ fs []
\\ last_x_assum irule
\\ gvs [FDOM_DOMSUB,DOMSUB_FAPPLY_THM,FLOOKUP_DEF]
\\ gvs [EXTENSION] \\ metis_tac [])
>-
(fs [subst_def] \\ irule remove_call_arg_App \\ fs [])
>-
(fs [subst_def] \\ irule remove_call_arg_Prim \\ fs [LIST_REL_MAP]
\\ last_x_assum mp_tac
\\ match_mp_tac LIST_REL_mono \\ fs [] \\ rw []
\\ first_x_assum $ irule \\ fs [boundvars_Apps,MEM_MAP]
\\ metis_tac [])
>-
(fs [subst_def] \\ irule remove_call_arg_Letrec \\ fs [LIST_REL_MAP]
\\ gvs [MAP_MAP_o,combinTheory.o_DEF,LAMBDA_PROD,FST_INTRO]
\\ reverse conj_tac
>-
(first_x_assum $ irule
\\ gvs [FDIFF_def,DRESTRICT_DEF,FLOOKUP_DEF])
\\ last_x_assum mp_tac
\\ match_mp_tac LIST_REL_mono
\\ gvs [FORALL_PROD]
\\ rw []
\\ first_x_assum $ irule
\\ gvs [FDIFF_def,DRESTRICT_DEF,FLOOKUP_DEF])
\\ irule remove_call_arg_closed \\ fs []
QED
Theorem subst_letrec_delarg:
∀i x y m1 m2.
letrec_delarg i x y ∧
FDOM m1 = FDOM m2 ∧
(∀k v1 v2.
FLOOKUP m1 k = SOME v1 ∧ FLOOKUP m2 k = SOME v2 ⇒
letrec_delarg i v1 v2 ∧ closed v1 ∧ closed v2) ⇒
letrec_delarg i (subst m1 x) (subst m2 y)
Proof
Induct_on ‘letrec_delarg’ \\ rw []
>-
(fs [mk_letrec_def,subst_def]
\\ ‘subst (FDIFF m1 {i.fname}) (mk_fun b1 i) = mk_fun b1 i ∧
subst (FDIFF m2 {i.fname}) (mk_fun b2 i) = mk_fun b2 i’ by
(rw [] \\ irule subst_ignore
\\ fs [IN_DISJOINT,SUBSET_DEF] \\ metis_tac [])
\\ fs [GSYM mk_letrec_def]
\\ irule letrec_delarg_Switch
\\ last_x_assum $ irule_at Any
\\ fs [FDIFF_def,DRESTRICT_DEF,FLOOKUP_DEF]
\\ drule_at (Pos last) remove_call_arg_mono
\\ disch_then $ qspec_then ‘letrec_delarg i’ mp_tac \\ fs []
\\ gvs [GSYM FDIFF_def] \\ strip_tac
\\ irule subst_remove_call_arg \\ gvs []
\\ gvs [FDIFF_def,DRESTRICT_DEF])
>-
(fs [subst_Apps,info_ok_def,subst_mk_apps]
\\ irule letrec_delarg_Apps \\ fs []
\\ fs [info_ok_def,LIST_REL_MAP] \\ rw []
\\ first_x_assum (fn th => mp_tac th \\ match_mp_tac LIST_REL_mono)
\\ gvs [SF SFY_ss])
>-
(fs [subst_def] \\ rpt CASE_TAC \\ fs [letrec_delarg_refl]
\\ res_tac \\ fs [] \\ gvs [FLOOKUP_DEF])
>-
(fs [subst_def]
\\ simp [Once letrec_delarg_cases] \\ disj2_tac
\\ last_x_assum irule \\ fs []
\\ fs [DOMSUB_FLOOKUP_THM,AllCaseEqs()]
\\ rw [] \\ res_tac \\ fs [SUBSET_DEF])
>-
(fs [subst_def]
\\ simp [Once letrec_delarg_cases]
\\ disj2_tac
\\ rpt $ last_x_assum $ irule_at Any \\ fs [])
>-
(fs [subst_def]
\\ simp [Once letrec_delarg_cases,SF ETA_ss] \\ disj2_tac
\\ last_x_assum mp_tac \\ fs []
\\ qid_spec_tac ‘ys’
\\ qid_spec_tac ‘xs’
\\ Induct \\ fs [PULL_EXISTS]
\\ rw [] \\ metis_tac [])
>-
(fs [subst_def]
\\ simp [Once letrec_delarg_cases] \\ disj2_tac \\ disj2_tac
\\ fs [MAP_MAP_o,combinTheory.o_DEF,UNCURRY,SF ETA_ss]
\\ reverse conj_tac
>-
(last_x_assum irule
\\ fs [FDOM_FDIFF,EXTENSION,FLOOKUP_FDIFF,SUBSET_DEF]
\\ rw [] \\ res_tac \\ fs [])
\\ last_x_assum mp_tac
\\ last_x_assum mp_tac
\\ pop_assum mp_tac
\\ pop_assum mp_tac
\\ pop_assum mp_tac
\\ pop_assum mp_tac
\\ qid_spec_tac ‘m2’
\\ qid_spec_tac ‘m1’
\\ qid_spec_tac ‘ys’
\\ qid_spec_tac ‘xs’
\\ Induct \\ fs [PULL_EXISTS]
\\ strip_tac \\ Cases \\ fs []
\\ rw []
>-
(first_x_assum irule
\\ fs [FDOM_FDIFF,EXTENSION,FLOOKUP_FDIFF,SUBSET_DEF]
\\ rw [] \\ res_tac \\ fs [])
\\ rewrite_tac [GSYM finite_mapTheory.FDIFF_FDOMSUB_INSERT]
\\ first_x_assum irule
\\ fs [FDOM_FDIFF,EXTENSION,FLOOKUP_FDIFF]
\\ fs [DOMSUB_FLOOKUP_THM,AllCaseEqs(),SUBSET_DEF]
\\ rw [] \\ res_tac \\ fs [])
QED
Theorem letrec_delarg_subst1:
letrec_delarg i a1 a2 ∧ letrec_delarg i z y ∧ closed a1 ∧ closed a2 ⇒
letrec_delarg i (subst1 v a1 z) (subst1 v a2 y)
Proof
strip_tac
\\ irule subst_letrec_delarg
\\ fs [FLOOKUP_DEF]
QED
Triviality eval_wh_Constructor_NIL_bisim =
eval_wh_Constructor_bisim |> Q.GEN ‘xs’ |> Q.SPEC ‘[]’ |> SIMP_RULE (srw_ss()) [];
Triviality LIST_REL_letrec_delarg_closed:
∀xs ys. LIST_REL (letrec_delarg i) xs ys ∧ EVERY closed xs ⇒ EVERY closed ys
Proof
Induct \\ rw [] \\ fs []
\\ imp_res_tac letrec_delarg_freevars \\ fs [closed_def]
QED
Theorem ALOOKUP_REVERSE_LIST_REL:
∀bs ys.
LIST_REL p (MAP SND bs) (MAP SND ys) ∧
MAP FST ys = MAP FST bs ∧
ALOOKUP (REVERSE (MAP (λ(g,x). (g,f x)) bs)) k' = SOME v1 ∧
ALOOKUP (REVERSE (MAP (λ(g,x). (g,h x)) ys)) k' = SOME v2 ⇒
∃x y. p x y ∧ v1 = f x ∧ v2 = h y ∧ MEM x (MAP SND bs) ∧ MEM y (MAP SND ys)
Proof
Induct using SNOC_INDUCT \\ fs [PULL_EXISTS]
\\ Cases \\ Cases using SNOC_CASES
\\ gvs [GSYM REVERSE_APPEND,MAP_SNOC,LIST_REL_SNOC,REVERSE_SNOC]
\\ rename [‘SND hh’] \\ PairCases_on ‘hh’ \\ fs []
\\ fs [AllCaseEqs()]
\\ rpt strip_tac \\ gvs []
\\ metis_tac []
QED
Theorem closed_mk_rec:
info_ok i ∧ freevars (mk_fun b1 i) ⊆ {i.fname} ⇒
closed (mk_rec b1 i)
Proof
gvs [closed_def,mk_rec_def,mk_letrec_def,mk_fun_def,info_ok_def]
\\ gvs [EXTENSION,SUBSET_DEF] \\ metis_tac []
QED
Theorem closed_mk_rec_copy:
info_ok i ∧ closed (mk_rec b1 i) ⇒ closed (mk_rec b2 i)
Proof
Cases_on ‘b1 = b2’ >- fs []
\\ Cases_on ‘b1’ \\ gvs []
\\ fs [mk_rec_def,mk_fun_def,mk_letrec_def,info_ok_def] \\ rw []
\\ drule remove_call_arg_freevars
\\ gvs [SUBSET_DEF] \\ rw []
\\ metis_tac []
QED
Theorem remove_call_arg_imp_letrec_delarg:
remove_call_arg b1 b2 F i (letrec_delarg i) x y ∧
info_ok i ∧
freevars (mk_fun b1 i) ⊆ {i.fname} ∧
freevars (mk_fun b2 i) ⊆ {i.fname} ⇒
letrec_delarg i (subst1 i.fname (mk_rec b1 i) x)
(subst1 i.fname (mk_rec b2 i) y)
Proof
qsuff_tac ‘∀b1 b2 b i x.
remove_call_arg b1 b2 b i (letrec_delarg i) x y ∧ info_ok i ∧ ~b ∧
freevars (mk_fun b1 i) ⊆ {i.fname} ∧
freevars (mk_fun b2 i) ⊆ {i.fname} ⇒
letrec_delarg i (subst1 i.fname (mk_rec b1 i) x)
(subst1 i.fname (mk_rec b2 i) y)’
>- metis_tac []
\\ Induct_on ‘remove_call_arg’
\\ rpt strip_tac \\ gvs []
>-
(imp_res_tac closed_mk_rec
\\ fs [subst_Apps,subst1_def,info_ok_def,subst_mk_apps]
\\ irule letrec_delarg_Apps \\ fs [] \\ fs [info_ok_def]
\\ gvs [LIST_REL_MAP] \\ rw []
\\ first_assum (fn th => mp_tac th \\ match_mp_tac LIST_REL_mono)
\\ fs [])
>- (gvs [subst1_def] \\ simp [Once letrec_delarg_cases])
>- (gvs [subst1_def] \\ simp [Once letrec_delarg_cases])
>- (gvs [subst1_def] \\ simp [Once letrec_delarg_cases])
>- (gvs [subst1_def] \\ irule letrec_delarg_Prim
\\ last_x_assum mp_tac
\\ qid_spec_tac ‘ys’
\\ qid_spec_tac ‘xs’
\\ Induct \\ fs [PULL_EXISTS])
>-
(gvs [subst1_def] \\ irule letrec_delarg_Letrec
\\ fs [MAP_MAP_o,combinTheory.o_DEF]
\\ fs [LAMBDA_PROD]
\\ rpt $ pop_assum mp_tac
\\ qid_spec_tac ‘ys’
\\ qid_spec_tac ‘xs’
\\ Induct \\ gvs [FORALL_PROD,PULL_EXISTS,MAP_EQ_CONS])
QED
Definition wh_Closures_def:
wh_Closures (v::vs) e = wh_Closure v (Lams vs e)
End
Triviality eval_wh_to_Apps_div:
∀xs x k.
eval_wh_to k x = wh_Diverge ⇒
eval_wh_to k (Apps x xs) = wh_Diverge
Proof
Induct \\ fs [Apps_def] \\ rw []
\\ last_x_assum irule \\ fs [eval_wh_to_def]
QED
Definition mk_body_def:
mk_body b i =
subst1 i.fname (mk_rec b i) (if b then i.rhs_T else i.rhs_F)
End
Triviality ignore_FDIFF:
DISJOINT (FDOM f) m ⇒ FDIFF f m = f
Proof
fs [fmap_EXT,FDIFF_def,DRESTRICT_DEF]
\\ fs [IN_DISJOINT,EXTENSION]
\\ metis_tac []
QED
Theorem eval_wh_to_mk_rec:
eval_wh_to k (mk_rec b i) ≠ wh_Diverge ∧ info_ok i ∧
freevars (mk_fun b i) ⊆ {i.fname} ⇒
0 < k ∧
∀l.
0 < l ⇒
eval_wh_to l (mk_rec b i) =
wh_Closures (i.args ++ (if b then [] else [i.arg]) ++ i.args') (mk_body b i)
Proof
fs [mk_rec_def,mk_letrec_def,eval_wh_to_def,AllCaseEqs()]
\\ strip_tac \\ gvs []
\\ fs [mk_fun_def]
\\ fs [subst_funs_def,bind_def,FUPDATE_LIST,FLOOKUP_UPDATE]
\\ fs [subst_Lams]
\\ fs [GSYM mk_fun_def,GSYM mk_rec_def,GSYM mk_letrec_def]
\\ DEP_REWRITE_TAC [ignore_FDIFF]
\\ fs [mk_body_def]
\\ Cases_on ‘i.args ++ (if b then [] else [i.arg]) ++ i.args'’ \\ fs []
\\ fs [Lams_def,eval_wh_Lam,eval_wh_to_def,wh_Closures_def]
\\ gvs [info_ok_def] \\ rw []
QED
Theorem eval_wh_mk_rec:
info_ok i ∧
freevars (mk_fun b i) ⊆ {i.fname} ⇒
eval_wh (mk_rec b i) =
wh_Closures (i.args ++ (if b then [] else [i.arg]) ++ i.args') (mk_body b i)
Proof
fs [mk_rec_def,mk_letrec_def,eval_wh_Letrec,AllCaseEqs()]
\\ strip_tac \\ gvs []
\\ fs [mk_fun_def]
\\ fs [subst_funs_def,bind_def,FUPDATE_LIST,FLOOKUP_UPDATE]
\\ fs [subst_Lams]
\\ fs [GSYM mk_fun_def,GSYM mk_rec_def,GSYM mk_letrec_def]
\\ DEP_REWRITE_TAC [ignore_FDIFF]
\\ fs [mk_body_def,info_ok_def]
\\ Cases_on ‘i.args ++ (if b then [] else [i.arg]) ++ i.args'’ \\ fs []
\\ fs [Lams_def,eval_wh_Lam,wh_Closures_def]
\\ rw []
QED
Theorem eval_wh_to_Apps:
∀xs vs x k e.
(∀l. 0 < l ⇒ eval_wh_to l x = wh_Closures vs e) ∧
LENGTH vs = LENGTH xs ∧ xs ≠ [] ∧ 0 < k ∧
EVERY closed xs ∧ ALL_DISTINCT vs ⇒
eval_wh_to k (Apps x xs) =
eval_wh_to (k-1) (subst (FEMPTY |++ (ZIP(vs,xs))) e)
Proof
Induct \\ fs [Apps_def] \\ rw []
\\ Cases_on ‘xs’ \\ gvs [Apps_def]
>- (gvs [LENGTH_EQ_NUM_compute,FUPDATE_LIST,eval_wh_to_def,wh_Closures_def,Lams_def]
\\ rw [bind_def] \\ gvs [FLOOKUP_UPDATE])
\\ Cases_on ‘vs’ \\ gvs [FUPDATE_LIST]
\\ rename [‘ALL_DISTINCT ts’]
\\ last_x_assum $ qspecl_then [‘ts’,‘App x h’,‘k’] mp_tac \\ fs []
\\ fs [wh_Closures_def,ADD_CLAUSES]
\\ rename [‘wh_Closure v (Lams args e)’]
\\ disch_then $ qspec_then ‘subst1 v h e’ mp_tac
\\ impl_tac
>-
(simp [eval_wh_to_def,bind_def,FLOOKUP_UPDATE,subst_Lams]
\\ Cases_on ‘args’ \\ fs [Lams_def,eval_wh_to_def,wh_Closures_def]
\\ qsuff_tac ‘FDIFF (FEMPTY |+ (v,h)) (h'' INSERT set t') = FEMPTY |+ (v,h)’
>- fs [] \\ gvs [fmap_EXT,FDIFF_def])
\\ strip_tac \\ fs []
\\ DEP_REWRITE_TAC [subst_subst_FUNION]
\\ gvs [FRANGE_DEF]
\\ AP_TERM_TAC
\\ AP_THM_TAC
\\ AP_TERM_TAC
\\ gvs [fmap_EXT]
\\ gvs [FDOM_FUPDATE_LIST,GSYM FUPDATE_LIST,FUNION_DEF]
\\ strip_tac \\ rw [] \\ gvs []
>-
(DEP_REWRITE_TAC [FUPDATE_LIST_APPLY_NOT_MEM]
\\ gvs [FAPPLY_FUPDATE_THM]
\\ DEP_REWRITE_TAC [MAP_FST_ZIP]
\\ fs [])
\\ irule FUPDATE_SAME_LIST_APPLY \\ fs []
QED
Theorem eval_wh_to_Apps_other:
∀xs vs x e.
eval_wh x = wh_Closures vs e ∧
LENGTH vs = LENGTH xs ∧ xs ≠ [] ∧
EVERY closed xs ∧ ALL_DISTINCT vs ⇒
eval_wh (Apps x xs) =
eval_wh (subst (FEMPTY |++ (ZIP(vs,xs))) e)
Proof
Induct \\ fs [Apps_def] \\ rw []
\\ Cases_on ‘xs’ \\ gvs [Apps_def]
>- (gvs [LENGTH_EQ_NUM_compute,FUPDATE_LIST,eval_wh_App,wh_Closures_def,Lams_def]
\\ rw [bind_def] \\ gvs [FLOOKUP_UPDATE])
\\ Cases_on ‘vs’ \\ gvs [FUPDATE_LIST]
\\ rename [‘ALL_DISTINCT ts’]
\\ last_x_assum $ qspecl_then [‘ts’,‘App x h’] mp_tac \\ fs []
\\ fs [wh_Closures_def,ADD_CLAUSES]
\\ rename [‘wh_Closure v (Lams args e)’]
\\ disch_then $ qspec_then ‘subst1 v h e’ mp_tac
\\ impl_tac
>-
(simp [eval_wh_App,bind_def,FLOOKUP_UPDATE,subst_Lams]
\\ Cases_on ‘args’ \\ fs [Lams_def,eval_wh_Lam,wh_Closures_def]
\\ qsuff_tac ‘FDIFF (FEMPTY |+ (v,h)) (h'' INSERT set t') = FEMPTY |+ (v,h)’
>- fs [] \\ gvs [fmap_EXT,FDIFF_def])
\\ strip_tac \\ fs []
\\ DEP_REWRITE_TAC [subst_subst_FUNION]
\\ gvs [FRANGE_DEF]
\\ AP_TERM_TAC
\\ AP_THM_TAC
\\ AP_TERM_TAC
\\ gvs [fmap_EXT]
\\ gvs [FDOM_FUPDATE_LIST,GSYM FUPDATE_LIST,FUNION_DEF]
\\ strip_tac \\ rw [] \\ gvs []
>-
(DEP_REWRITE_TAC [FUPDATE_LIST_APPLY_NOT_MEM]
\\ gvs [FAPPLY_FUPDATE_THM]
\\ DEP_REWRITE_TAC [MAP_FST_ZIP]
\\ fs [])
\\ irule FUPDATE_SAME_LIST_APPLY \\ fs []
QED
Theorem subst_simp:
LENGTH vs = LENGTH xs ∧ ~MEM v vs ∧ closed c ⇒
subst (FEMPTY |++ ZIP (vs ++ [v],xs ++ [c])) (if b1 then subst1 v c x else x) =
subst (FEMPTY |++ ZIP (vs ++ [v],xs ++ [c])) x
Proof
rw []
\\ DEP_REWRITE_TAC [subst_subst_FUNION]
\\ gvs [FRANGE_DEF]
\\ DEP_REWRITE_TAC [GSYM ZIP_APPEND] \\ fs []
\\ fs [FUPDATE_LIST,FOLDL_APPEND]
\\ AP_THM_TAC \\ AP_TERM_TAC
\\ gvs [fmap_EXT,FUNION_DEF]
\\ rw [] \\ fs [EXTENSION]
\\ metis_tac []
QED
Theorem freecars_mk_body:
closed (mk_rec b1 i) ⇒
freevars (mk_body b1 i) ⊆ set i.args ∪ {i.arg} ∪ set i.args'
Proof
rw [mk_body_def]
\\ DEP_REWRITE_TAC [freevars_subst]
\\ gvs [FRANGE_FUPDATE,closed_def,mk_rec_def,mk_letrec_def,mk_fun_def]
\\ last_x_assum mp_tac
\\ DEP_REWRITE_TAC [freevars_subst]
\\ gvs [FRANGE_FUPDATE,closed_def,mk_rec_def,mk_letrec_def,mk_fun_def]
\\ gvs [EXTENSION,SUBSET_DEF]
\\ metis_tac []
QED
Theorem freecars_mk_body_alt:
closed (mk_rec b1 i) ⇒
freevars (mk_body b1 i) ⊆
if b1 then set i.args ∪ set i.args' else set i.args ∪ {i.arg} ∪ set i.args'
Proof
rw [mk_body_def]
\\ DEP_REWRITE_TAC [freevars_subst]
\\ gvs [FRANGE_FUPDATE,closed_def,mk_rec_def,mk_letrec_def,mk_fun_def]
\\ last_x_assum mp_tac
\\ DEP_REWRITE_TAC [freevars_subst]
\\ gvs [FRANGE_FUPDATE,closed_def,mk_rec_def,mk_letrec_def,mk_fun_def]
\\ gvs [EXTENSION,SUBSET_DEF]
\\ metis_tac []
QED
Theorem FRANGE_FUPDATE_LIST_ZIP_IMP:
∀xs ys v m.
v ∈ FRANGE (m |++ ZIP (xs,ys)) ⇒
v ∈ FRANGE m ∨ MEM v ys
Proof
Induct \\ fs [ZIP_def,FUPDATE_LIST]
\\ Cases_on ‘ys’ \\ fs [ZIP_def,FUPDATE_LIST]
\\ rw [] \\ res_tac \\ asm_rewrite_tac []
\\ CCONTR_TAC \\ gvs []
\\ gvs [FRANGE_DEF,DOMSUB_FAPPLY_THM,AllCaseEqs()]
QED
Theorem LIST_REL_letrec_delarg_closed:
∀xs ys. LIST_REL (letrec_delarg i) xs ys ∧ EVERY closed xs ⇒ EVERY closed ys
Proof
Induct \\ fs [PULL_EXISTS] \\ rw []
\\ imp_res_tac letrec_delarg_freevars
\\ fs [closed_def]
QED
Theorem remove_call_arg_gen:
remove_call_arg F T F i $= i.rhs_F i.rhs_T ⇒
remove_call_arg b1 b2 F i $= (if b1 then i.rhs_T else i.rhs_F)
(if b2 then i.rhs_T else i.rhs_F)
Proof
Cases_on ‘b1 ≠ b2’ \\ fs []
>-
(Cases_on ‘b1’ \\ gvs [] \\ strip_tac
\\ drule remove_call_arg_sym
\\ simp [Once EQ_SYM_EQ, SF ETA_ss])
\\ Cases_on ‘b1’ \\ gvs [] \\ rw []
\\ drule remove_call_arg_sym
\\ simp [Once EQ_SYM_EQ, SF ETA_ss] \\ rw []
\\ imp_res_tac remove_call_arg_dup \\ fs []
QED
Triviality lemma1:
LENGTH xs2 = LENGTH xs1 ∧ ~MEM k xs1 ⇒
ALOOKUP (REVERSE (ZIP (xs1,xs2))) k = NONE
Proof
fs [ALOOKUP_NONE,MAP_REVERSE,MAP_FST_ZIP]
QED
Triviality lemma2:
∀xs1 ys1 vs k'.
LENGTH xs1 = LENGTH vs ∧
EVERY closed xs1 ∧ MEM k' vs ∧
LIST_REL (letrec_delarg i) xs1 ys1
⇒
∃x y.
ALOOKUP (REVERSE (ZIP (vs,xs1))) k' = SOME x ∧
ALOOKUP (REVERSE (ZIP (vs,ys1))) k' = SOME y ∧
letrec_delarg i x y ∧ closed x ∧ closed y
Proof
Induct using SNOC_INDUCT
\\ Cases_on ‘vs’ using SNOC_CASES
\\ Cases_on ‘ys1’ using SNOC_CASES
\\ gvs [] \\ rw []
\\ gvs [SNOC_APPEND]
\\ DEP_REWRITE_TAC [GSYM ZIP_APPEND]
\\ imp_res_tac LIST_REL_LENGTH
\\ fs [REVERSE_APPEND]
\\ imp_res_tac letrec_delarg_freevars
\\ rw []
\\ fs [closed_def]
QED
Theorem Apps_rec_lemma:
eval_wh_to k (mk_apps b1 (mk_rec b1 i) xs c xs1) ≠ wh_Diverge ∧
LENGTH xs = LENGTH i.args ∧
LENGTH xs1 = LENGTH i.args' ∧ info_ok i ∧
closed (mk_rec b1 i) ∧
closed (mk_rec b2 i) ∧
EVERY closed xs ∧ EVERY closed xs1 ∧ closed c ∧
LIST_REL (letrec_delarg i) xs ys ∧
LIST_REL (letrec_delarg i) xs1 ys1 ⇒
∃k1 x1 y1.
eval_wh_to k (mk_apps b1 (mk_rec b1 i) xs c xs1) = eval_wh_to k1 x1 ∧
eval_wh (mk_apps b2 (mk_rec b2 i) ys c ys1) = eval_wh y1 ∧
closed x1 ∧
letrec_delarg i x1 y1 ∧
k1 < k
Proof
rw []
\\ Cases_on ‘eval_wh_to k (mk_rec b1 i) = wh_Diverge’
>- (fs [mk_apps_def] \\ drule eval_wh_to_Apps_div \\ strip_tac \\ fs [])
\\ ‘freevars (mk_fun b1 i) ⊆ {i.fname} ∧
freevars (mk_fun b2 i) ⊆ {i.fname}’ by
fs [closed_def,mk_rec_def,mk_letrec_def,SUBSET_DIFF_EMPTY]
\\ drule_all eval_wh_to_mk_rec \\ strip_tac
\\ drule eval_wh_to_Apps
\\ simp [mk_apps_def]
\\ disch_then $ qspecl_then [‘xs ++ (if b1 then [] else [c]) ++ xs1’,‘k’] mp_tac
\\ impl_tac
>- (fs [info_ok_def,ALL_DISTINCT_APPEND]
\\ rw [] \\ gvs [] \\ CCONTR_TAC \\ gvs [])
\\ strip_tac \\ gvs []
\\ irule_at (Pos hd) EQ_REFL
\\ qexists_tac
‘(subst (FEMPTY |++ ZIP (i.args ++ (if b2 then [] else [i.arg]) ++ i.args',
ys ++ (if b2 then [] else [c]) ++ ys1)) (mk_body b2 i))’
\\ gvs [SF SFY_ss]
\\ conj_tac >-
(irule eval_wh_to_Apps_other
\\ irule_at Any eval_wh_mk_rec \\ fs []
\\ imp_res_tac LIST_REL_letrec_delarg_closed \\ fs []
\\ imp_res_tac LIST_REL_LENGTH
\\ fs [info_ok_def,ALL_DISTINCT_APPEND]
\\ rw [] \\ gvs []
\\ CCONTR_TAC \\ gvs [])
\\ conj_tac >-
(irule IMP_closed_subst
\\ conj_tac >-
(rw []
\\ drule $ REWRITE_RULE [SUBSET_DEF] FRANGE_FUPDATE_LIST_SUBSET
\\ fs []
\\ DEP_REWRITE_TAC [MAP_SND_ZIP] \\ fs [EVERY_MEM,info_ok_def]
\\ rw [] \\ res_tac \\ fs [])
\\ irule SUBSET_TRANS
\\ irule_at Any freecars_mk_body_alt \\ fs []
\\ gvs [FDOM_FUPDATE_LIST]
\\ DEP_REWRITE_TAC [MAP_FST_ZIP]
\\ gvs [SUBSET_DEF]
\\ rw [] \\ fs [SUBSET_DEF])
\\ fs [mk_body_def]
\\ DEP_ONCE_REWRITE_TAC [subst_subst]
\\ conj_tac >-
(fs [FRANGE_FUPDATE,FDOM_FUPDATE_LIST]
\\ DEP_REWRITE_TAC [MAP_FST_ZIP] \\ fs [info_ok_def]
\\ rw [] \\ imp_res_tac FRANGE_FUPDATE_LIST_ZIP_IMP \\ fs []
\\ fs [EVERY_MEM] \\ res_tac \\ fs [])
\\ qpat_abbrev_tac ‘pp = subst1 _ _ (subst _ _)’
\\ DEP_ONCE_REWRITE_TAC [subst_subst]
\\ unabbrev_all_tac
\\ conj_tac >-
(fs [FRANGE_FUPDATE,FDOM_FUPDATE_LIST]
\\ DEP_REWRITE_TAC [MAP_FST_ZIP] \\ fs [info_ok_def]
\\ imp_res_tac LIST_REL_LENGTH
\\ rw [] \\ imp_res_tac FRANGE_FUPDATE_LIST_ZIP_IMP \\ fs []
\\ fs [EVERY_MEM] \\ res_tac \\ fs []
\\ drule_all LIST_REL_MEM_ALT
\\ strip_tac \\ res_tac
\\ imp_res_tac letrec_delarg_freevars \\ gvs [closed_def] )
\\ irule remove_call_arg_imp_letrec_delarg \\ fs []
\\ irule subst_remove_call_arg_F_F
\\ simp []
\\ gvs [FDOM_FUPDATE_LIST]
\\ DEP_REWRITE_TAC [MAP_FST_ZIP]
\\ conj_tac
>- (imp_res_tac LIST_REL_LENGTH \\ gvs [] \\ rw [] \\ gvs [])
\\ conj_tac
>- (rw [] \\ gvs [] \\ rw [] \\ eq_tac \\ rw [] \\ gvs [])
\\ reverse conj_tac >-
(conj_tac >- (fs [info_ok_def] \\ rw [] \\ fs [])