-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpure_inline_relScript.sml
1580 lines (1516 loc) · 43.1 KB
/
pure_inline_relScript.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
(*
Theorem that help prove inlining correct
*)
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
combinTheory;
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_seqTheory pure_demandTheory pure_barendregtTheory;
val _ = new_theory "pure_inline_rel";
(*------------------------------------------------------------------------------*
Definition of precondition pre
*------------------------------------------------------------------------------*)
Definition vars_of_def:
vars_of [] = {} ∧
vars_of ((v,e)::rest) = {v} ∪ freevars e ∪ vars_of rest
End
Definition pre_def:
pre l x ⇔
barendregt x ∧
DISJOINT (boundvars x) (freevars x) ∧
DISJOINT (boundvars x) (vars_of l)
End
(*------------------------------------------------------------------------------*
Definition of inline_rel
*------------------------------------------------------------------------------*)
Inductive inline_rel:
[~refl:]
(∀l t.
inline_rel l t t)
[~Var:]
(∀v x l.
MEM (v, x) l ⇒
inline_rel l (Var v) x)
[~Let:]
(∀l v x y.
inline_rel l x x' ∧
inline_rel (l ++ [(v,x)]) y y' ⇒
inline_rel l (Let v x y) (Let v x' y'))
[~Prim:]
(∀l p xs ys.
LIST_REL (inline_rel l) xs ys ⇒
inline_rel l (Prim p xs) (Prim p ys))
[~App:]
(∀l t1 t2 u1 u2.
inline_rel l t1 u1 ∧
inline_rel l t2 u2 ⇒
inline_rel l (App t1 t2) (App u1 u2))
[~Lam:]
(∀l t u w.
inline_rel l t u ⇒
inline_rel l (Lam w t) (Lam w u))
[~Letrec:]
(∀l t u xs ys.
LIST_REL (λ(n,t1) (m,u1). n = m ∧ inline_rel l t1 u1) xs ys ∧
inline_rel l t u ⇒
inline_rel l (Letrec xs t) (Letrec ys u))
[~spec:]
(∀l t u v x y x1.
inline_rel l x y ∧
(∀e. Letrec [(v, x)] e ≅ Let v x1 e) ∧
v ∉ freevars x1 ∧
DISJOINT (boundvars t) (boundvars x1) ∧
DISJOINT (boundvars t) (freevars x1) ∧
inline_rel (l ++ [(v,x1)]) t u ⇒
inline_rel l (Letrec [(v, x)] t) (Letrec [(v, y)] u))
[~trans:]
(∀l x y z.
inline_rel l x y ∧
inline_rel l y z ∧
pre l y ⇒
inline_rel l x z)
[~simp:]
(∀l t u u1.
inline_rel l t u ∧
u ≅ u1 ⇒
inline_rel l t u1)
End
(*------------------------------------------------------------------------------*
Definition of lets_ok
*------------------------------------------------------------------------------*)
Definition lets_ok_def:
(lets_ok [] ⇔ T) ∧
(lets_ok ((v,x)::rest) ⇔
v ∉ freevars x ∧
DISJOINT ({v} ∪ freevars x) (set (MAP FST rest)) ∧
lets_ok rest)
End
(*------------------------------------------------------------------------------*
Lemmas about lets_ok etc.
*------------------------------------------------------------------------------*)
Theorem vars_of_DISJOINT_MAP_FST:
DISJOINT s (vars_of xs) ⇒ DISJOINT s (set (MAP FST xs))
Proof
rw []
\\ Induct_on `xs` \\ reverse $ rw [vars_of_def]
\\ PairCases_on ‘h’ \\ gvs [vars_of_def]
\\ gvs [DISJOINT_SYM]
QED
Theorem vars_of_append:
∀xs ys. vars_of (xs ++ ys) = vars_of xs ∪ vars_of ys
Proof
rw []
\\ Induct_on ‘xs’ \\ rw []
>- fs [vars_of_def]
\\ Cases_on `h` \\ Cases_on `r` \\ rw []
>- (
fs [vars_of_def]
\\ fs [UNION_ASSOC]
)
\\ fs [vars_of_def]
\\ fs [UNION_ASSOC]
QED
Theorem vars_of_DISJOINT_FILTER:
∀xs ys. DISJOINT s (vars_of ys) ⇒
DISJOINT s (vars_of (FILTER P ys))
Proof
rw []
\\ Induct_on ‘ys’ \\ reverse $ rw []
>- (
last_x_assum irule
\\ qsuff_tac `DISJOINT s (vars_of ([h] ++ ys))`
>- fs [vars_of_append, vars_of_def, DISJOINT_SYM]
\\ fs [Once APPEND]
)
\\ sg `DISJOINT s (vars_of ([h] ++ ys))`
>- simp [Once APPEND]
\\ qsuff_tac `DISJOINT s (vars_of ([h] ++ FILTER P ys))`
>- simp [Once APPEND]
\\ fs [vars_of_append]
\\ simp [Once DISJOINT_SYM]
\\ last_x_assum irule
\\ fs [DISJOINT_SYM]
QED
Theorem vars_of_not_in_MAP_FST:
v ∉ vars_of xs ⇒ ¬MEM v (MAP FST xs)
Proof
rw []
\\ Induct_on `xs`
>- rw [vars_of_def,MAP]
\\ Cases_on `h`
\\ rw [vars_of_def,MAP]
QED
Theorem lets_ok_SNOC_alt:
lets_ok xs ∧ v ∉ freevars x1 ∧ ~MEM v (MAP FST xs) ∧
v ∉ vars_of xs
⇒
lets_ok (xs ++ [(v,x1)])
Proof
Induct_on ‘xs’
\\ fs [lets_ok_def,FORALL_PROD,vars_of_def]
\\ rw [] \\ simp [DISJOINT_SYM]
QED
Theorem lets_ok_SNOC:
lets_ok xs ∧ pre xs (Let v x y) ⇒
lets_ok (xs ++ [(v,x)])
Proof
strip_tac
\\ irule lets_ok_SNOC_alt \\ fs []
\\ fs [pre_def]
\\ imp_res_tac vars_of_not_in_MAP_FST
QED
Theorem map_fst_subset_vars_of:
∀xs. set (MAP FST xs) ⊆ vars_of xs
Proof
Induct \\ fs [vars_of_def,FORALL_PROD] \\ fs [SUBSET_DEF]
QED
(*------------------------------------------------------------------------------*
Lemmas and precondition pre
*------------------------------------------------------------------------------*)
Theorem pre_App:
pre xs (App x y) ⇒ pre xs x ∧ pre xs y
Proof
gvs [pre_def,barendregt_alt_def]
\\ fs [IN_DISJOINT]
\\ metis_tac []
QED
Theorem pre_Prim:
pre xs (Prim x ys) ⇒ EVERY (pre xs) ys
Proof
gvs [pre_def,EVERY_MEM,MEM_MAP,PULL_EXISTS,barendregt_alt_def]
QED
Theorem pre_Lam:
pre xs (Lam w x) ⇒
¬MEM w (MAP FST xs) ∧ w ∉ vars_of xs ∧ pre xs x
Proof
fs [pre_def,barendregt_alt_def] \\ strip_tac
\\ imp_res_tac vars_of_not_in_MAP_FST \\ fs []
\\ gvs [IN_DISJOINT] \\ metis_tac []
QED
Theorem pre_SNOC:
pre xs x ∧
v ∉ freevars x1 ∧ v ∉ boundvars x ∧
DISJOINT (boundvars x) (boundvars x1) ∧
DISJOINT (boundvars x) (freevars x1) ⇒
pre (xs ++ [(v,x1)]) x
Proof
fs [pre_def,vars_of_append,vars_of_def,barendregt_alt_def] \\ rw []
\\ simp [DISJOINT_SYM]
QED
Theorem pre_Let:
pre xs (Let v x y) ⇒
pre xs x ∧ pre (xs ++ [(v,x)]) y ∧
¬MEM v (MAP FST xs) ∧ v ∉ vars_of xs
Proof
fs [lets_ok_def,pre_def,barendregt_alt_def]
\\ simp [DISJOINT_SYM,vars_of_append,vars_of_def]
\\ strip_tac
\\ imp_res_tac vars_of_not_in_MAP_FST \\ fs []
\\ gvs [IN_DISJOINT]
\\ metis_tac []
QED
Theorem pre_Letrec:
pre xs (Letrec ys x) ⇒
pre xs x ∧ EVERY (pre xs) (MAP SND ys) ∧
DISJOINT (set (MAP FST ys)) (boundvars x) ∧
EVERY (λ(v,e). ¬MEM v (MAP FST xs) ∧ v ∉ vars_of xs) ys
Proof
rpt strip_tac
>-
(gvs [pre_def,barendregt_alt_def]
\\ gvs [IN_DISJOINT] \\ metis_tac [])
>-
(pop_assum mp_tac \\ fs [pre_def] \\ strip_tac
\\ ‘EVERY barendregt (MAP SND ys)’ by gvs [barendregt_alt_def]
\\ gvs [EVERY_MEM,MEM_MAP,PULL_EXISTS,FORALL_PROD,pre_def,IN_DISJOINT]
\\ gvs [IN_DISJOINT,SF SFY_ss]
\\ rw [] \\ CCONTR_TAC \\ gvs []
\\ first_x_assum drule
\\ gvs [barendregt_def,IN_DISJOINT]
\\ metis_tac [])
\\ pop_assum mp_tac \\ fs [pre_def,barendregt_alt_def]
\\ gvs [EVERY_MEM,MEM_MAP,PULL_EXISTS,FORALL_PROD,pre_def]
\\ rpt disch_tac
\\ rpt gen_tac
\\ rpt disch_tac \\ fs []
\\ ‘set (MAP FST xs) ⊆ vars_of xs’ by gvs [map_fst_subset_vars_of]
\\ gvs [EVERY_MEM,MEM_MAP,PULL_EXISTS,FORALL_PROD,pre_def,IN_DISJOINT,SUBSET_DEF]
\\ metis_tac []
QED
(*------------------------------------------------------------------------------*
Theorems about pushing and pulling let-expressions
*------------------------------------------------------------------------------*)
Theorem FLOOKUP_closed_FRANGE_closed:
∀f. (∀n v. FLOOKUP f n = SOME v ⇒ closed v) ⇔
(∀v. v ∈ FRANGE f ⇒ closed v)
Proof
rw []
\\ simp [FRANGE_FLOOKUP]
\\ rw [EQ_IMP_THM]
\\ first_x_assum irule
\\ qexists ‘n’
\\ rw []
QED
Theorem subst1_notin:
∀w e e1. w ∉ freevars e ⇒
subst1 w e1 e = e
Proof
rw []
\\ qspecl_then [‘FEMPTY |+ (w, e1)’, ‘e’, ‘w’] assume_tac subst_fdomsub
\\ fs []
QED
Theorem Let_Var3:
w ∉ freevars e ⇒
(Let w e (Var w) ≅? Let w e e) b
Proof
rw []
\\ irule eval_wh_IMP_exp_eq
\\ fs [subst_def,eval_wh_Seq] \\ rw [] \\ fs []
\\ fs [eval_wh_App,eval_wh_Lam,bind1_def]
\\ rw [subst1_def]
\\ AP_TERM_TAC
\\ sg ‘∀v. v ∈ FRANGE (f \\ w) ⇒ closed v’
>- (
simp [GSYM FLOOKUP_closed_FRANGE_closed]
\\ simp [DOMSUB_FLOOKUP_THM]
\\ rw []
\\ res_tac
)
\\ qsuff_tac ‘w ∉ freevars (subst (f \\ w) e)’
>- (
rw []
\\ irule EQ_TRANS
\\ irule_at (Pos last) $ GSYM subst1_notin
\\ simp [subst_fdomsub]
)
\\ qsuff_tac ‘closed (subst (f \\ w) e)’
>- (
rw []
\\ fs [closed_def]
)
\\ gvs [GSYM subst_fdomsub]
QED
Theorem exp_eq_subst_IMP_exp_eq:
(∀f. (∀n v. FLOOKUP f n = SOME v ⇒ closed v) ∧
freevars x ⊆ FDOM f ∧ freevars y ⊆ FDOM f ⇒
(subst f x ≅? subst f y) b) ⇒
(x ≅? y) b
Proof
rw []
\\ irule (iffRL exp_eq_open_bisimilarity_freevars)
\\ simp [open_bisimilarity_def]
\\ rw []
\\ first_x_assum (qspecl_then [‘f’] assume_tac)
\\ gs []
\\ simp [bind_def]
\\ rw []
\\ reverse (qsuff_tac ‘(subst f x ≅? subst f y) b’)
>- (
simp []
\\ first_x_assum irule
\\ rw []
\\ first_x_assum irule
\\ qexists ‘n’
\\ rw []
)
\\ rw []
\\ irule (iffLR (GSYM app_bisimilarity_eq))
\\ rw []
\\ rw []
\\ irule IMP_closed_subst
\\ rw []
\\ gvs [FLOOKUP_closed_FRANGE_closed]
QED
Theorem Let_Lam:
v ≠ w ∧ w ∉ freevars x ⇒
(Let v x (Lam w t) ≅? Lam w (Let v x t)) b
Proof
rw []
\\ irule exp_eq_subst_IMP_exp_eq
\\ rw [subst_def]
\\ irule exp_eq_trans
\\ irule_at Any beta_equality
\\ conj_asm1_tac
>- (
irule IMP_closed_subst
\\ fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
)
\\ rw [subst_def]
\\ rw [exp_eq_Lam_removed]
\\ simp [exp_eq_sym]
\\ irule exp_eq_trans
\\ irule_at Any beta_equality
\\ rw []
>- (
irule IMP_closed_subst
\\ fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
\\ simp [DOMSUB_FAPPLY_NEQ]
\\ fs [SUBSET_DEF]
)
\\ simp [DOMSUB_FUPDATE_NEQ]
\\ simp [exp_eq_refl]
\\ reverse $ qsuff_tac
‘(subst (f \\ w \\ v) t = subst (f \\ v \\ w) t)
∧ (subst (f \\ w) x = subst f x)’
>- (
rw []
>- (
rw []
\\ simp [DOMSUB_COMMUTES]
)
\\ irule (GSYM subst_fdomsub)
\\ rw []
)
\\ rw []
\\ rw [exp_eq_refl]
QED
Theorem FST_intro:
(λ(p1,p2). p1) = FST
Proof
simp [FUN_EQ_THM,FORALL_PROD]
QED
Theorem Let_Letrec:
∀v x xs e.
EVERY (λ(n,u). n ∉ freevars x) xs ∧
¬MEM v (MAP FST xs) ∧
DISJOINT (freevars x) (set (MAP FST xs)) ⇒
(Let v x (Letrec xs e) ≅? Letrec (MAP (λ(n,t). (n, Let v x t)) xs) (Let v x e)) b
Proof
rw []
\\ irule exp_eq_subst_IMP_exp_eq
\\ rw []
\\ fs [MAP_MAP_o,o_DEF,LAMBDA_PROD,FST_intro]
\\ simp [subst_def]
\\ irule exp_eq_trans
\\ irule_at Any beta_equality
\\ conj_asm1_tac
>- (
irule IMP_closed_subst
\\ fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
)
\\ rw [subst_def]
\\ fs [MAP_MAP_o,o_DEF,LAMBDA_PROD,FST_intro]
\\ irule exp_eq_Letrec_cong
\\ fs [MAP_MAP_o,o_DEF,LAMBDA_PROD,FST_intro]
\\ reverse $ conj_tac
>- (
simp [Once exp_eq_sym]
\\ irule exp_eq_trans
\\ irule_at Any beta_equality
\\ conj_asm1_tac
>- (
irule IMP_closed_subst
\\ fs [FDOM_FDIFF,SUBSET_DEF,IN_DISJOINT]
\\ fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
\\ fs [FDIFF_def,DRESTRICT_DEF]
\\ metis_tac []
)
\\ DEP_REWRITE_TAC [subst_subst_FUNION]
\\ conj_tac
>- (
fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
\\ fs [FDIFF_def,DRESTRICT_DEF,DOMSUB_FAPPLY_THM]
)
\\ qmatch_goalsub_abbrev_tac ‘(subst f1 e ≅? subst f2 e) b’
\\ qsuff_tac ‘f1 = f2’
>- simp [exp_eq_refl]
\\ unabbrev_all_tac
\\ rpt MK_COMB_TAC \\ fs []
\\ simp [FLOOKUP_EXT,FUN_EQ_THM,FLOOKUP_FDIFF,DOMSUB_FLOOKUP_THM,FLOOKUP_UPDATE]
\\ rw []
\\ fs []
\\ irule subst_FDIFF'
\\ fs [EVERY_MEM,FORALL_PROD,MEM_MAP,PULL_EXISTS]
\\ rw []
\\ res_tac
)
\\ simp [LIST_REL_MAP_MAP]
\\ fs [EVERY_MEM,FORALL_PROD]
\\ rw []
\\ res_tac
\\ DEP_REWRITE_TAC [subst_subst_FUNION]
\\ conj_tac
>- (
fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
\\ fs [FDIFF_def,DRESTRICT_DEF,DOMSUB_FAPPLY_THM]
)
\\ simp [Once exp_eq_sym]
\\ rw [subst_def]
\\ irule exp_eq_trans
\\ irule_at Any beta_equality
\\ conj_asm1_tac
>- (
irule IMP_closed_subst
\\ fs [FDOM_FDIFF,SUBSET_DEF,IN_DISJOINT]
\\ fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
\\ fs [FDIFF_def,DRESTRICT_DEF]
\\ metis_tac []
)
\\ DEP_REWRITE_TAC [subst_subst_FUNION]
\\ conj_tac
>- (
fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
\\ fs [FDIFF_def,DRESTRICT_DEF,DOMSUB_FAPPLY_THM]
)
\\ qmatch_goalsub_abbrev_tac ‘(subst f1 p_2 ≅? subst f2 p_2) b’
\\ qsuff_tac ‘f1 = f2’
>- simp [exp_eq_refl]
\\ unabbrev_all_tac
\\ rpt MK_COMB_TAC \\ fs []
\\ simp [FLOOKUP_EXT,FUN_EQ_THM,FLOOKUP_FDIFF,DOMSUB_FLOOKUP_THM,FLOOKUP_UPDATE]
\\ rw []
\\ fs []
\\ irule subst_FDIFF'
\\ fs [EVERY_MEM,FORALL_PROD,MEM_MAP,PULL_EXISTS]
\\ rw []
\\ res_tac
QED
Theorem inline_rel_Var_trans:
∀v x x1 x2 l.
MEM (v,x) l ∧ x ≅ x1 ∧ pre l x1 ∧ inline_rel l x1 x2 ⇒
inline_rel l (Var v) x2
Proof
rw []
\\ irule inline_rel_trans
\\ pop_assum $ irule_at Any \\ fs []
\\ irule_at Any inline_rel_simp
\\ last_x_assum $ irule_at Any
\\ irule_at Any inline_rel_Var
\\ fs []
QED
Theorem Lets_snoc:
∀xs. Lets (xs ++ [(v,x)]) y = Lets xs (Let v x y)
Proof
Induct \\ fs [Lets_def]
\\ PairCases \\ Cases_on ‘h1’ \\ fs [Lets_def]
QED
Theorem exp_eq_Let_cong:
(e1 ≅? e2) b ∧ (bod1 ≅? bod2) b ⇒
(Let v e1 bod1 ≅? Let v e2 bod2) b
Proof
simp[exp_eq_App_cong, exp_eq_Lam_cong]
QED
Theorem Lets_Lam:
v ∉ set (MAP FST xs) ∧
v ∉ vars_of xs ⇒
(Lets xs (Lam v x) ≅? Lam v (Lets xs x)) b
Proof
rw []
\\ Induct_on `xs` \\ rw [Lets_def]
>- simp [exp_eq_refl]
\\ Cases_on `h` \\ rw [Lets_def]
\\ simp [Once exp_eq_sym]
\\ irule exp_eq_trans
\\ once_rewrite_tac [exp_eq_sym]
\\ irule_at Any Let_Lam
\\ fs [FST,vars_of_def]
\\ irule exp_eq_Let_cong
\\ fs [exp_eq_refl]
QED
Theorem Lets_App:
∀xs x y. (Lets xs (App x y) ≅? App (Lets xs x) (Lets xs y)) b
Proof
Induct \\ fs [Lets_def,exp_eq_refl]
\\ PairCases \\ fs [Lets_def]
\\ rw []
\\ irule exp_eq_trans
\\ irule_at Any pure_congruenceTheory.Let_App
\\ irule exp_eq_App_cong \\ fs [exp_eq_refl]
\\ irule exp_eq_Lam_cong \\ fs [exp_eq_refl]
QED
Theorem Lets_Let:
v ∉ set (MAP FST xs) ∧
v ∉ vars_of xs ⇒
(Lets xs (Let v x y) ≅? Let v (Lets xs x) (Lets xs y)) b
Proof
rw []
\\ irule exp_eq_trans
\\ irule_at Any Lets_App
\\ irule exp_eq_App_cong \\ fs [exp_eq_refl,Lets_Lam]
QED
Theorem Letrec1_Letrec:
∀v x xs e.
EVERY (λ(n,u). n ∉ freevars x) xs ∧
¬MEM v (MAP FST xs) ⇒
(Letrec [(v, x)] (Letrec xs e) ≅? Letrec (MAP (λ(n,t). (n, Letrec [(v, x)] t)) xs) (Letrec [(v, x)] e)) b
Proof
rw []
\\ sg `DISJOINT (set (MAP FST xs)) (freevars x)`
>- (
fs [IN_DISJOINT]
\\ rw []
\\ fs [MEM_MAP,FST,NOT_EXISTS]
\\ Cases_on `x' ∉ freevars x` \\ rw []
\\ Cases_on `y`
\\ fs [EVERY_MEM]
\\ last_x_assum $ qspec_then `(q, r)` assume_tac
\\ gvs []
)
\\ irule exp_eq_subst_IMP_exp_eq
\\ rw []
\\ fs [MAP_MAP_o,o_DEF,LAMBDA_PROD,FST_intro]
\\ simp [subst_def]
\\ irule exp_eq_trans
\\ irule_at Any beta_equality_Letrec
\\ fs [MAP_MAP_o,o_DEF,LAMBDA_PROD,FST_intro,FDIFF_FDIFF,subst_funs_def]
\\ sg `(∀w. w ∈ FRANGE (FDIFF f {v}) ⇒ closed w)`
>- (
rw []
\\ fs [FLOOKUP_DEF,PULL_EXISTS,FRANGE_DEF]
\\ first_x_assum $ qspec_then `x'` assume_tac
\\ gvs []
\\ fs [FDIFF_def,DRESTRICT_DEF]
)
\\ conj_tac
>- (
fs [freevars_subst,DIFF_SUBSET]
\\ irule SUBSET_TRANS
\\ last_x_assum $ irule_at Any
\\ fs [SUBSET_UNION]
\\ fs [SUBSET_DEF]
)
\\ fs [FLOOKUP_UPDATE,FUPDATE_LIST]
\\ sg `closed (Letrec [(v,subst (FDIFF f {v}) x)] (subst (FDIFF f {v}) x))`
>- (
fs [freevars_subst,DIFF_SUBSET]
\\ irule SUBSET_TRANS
\\ last_x_assum $ irule_at Any
\\ fs [SUBSET_UNION]
\\ fs [SUBSET_DEF]
)
\\ fs [bind1_def]
\\ rw [subst_def]
\\ fs [MAP_MAP_o,o_DEF,LAMBDA_PROD,FST_intro,FDIFF_FDIFF,subst_funs_def,FDIFF_FUPDATE]
\\ irule exp_eq_Letrec_cong
\\ rw []
>- fs [MAP_MAP_o,o_DEF,LAMBDA_PROD,FST]
>- (
fs [MAP_MAP_o,o_DEF,LAMBDA_PROD,SND,LIST_REL_EVERY_ZIP,ZIP_MAP,EVERY_MAP]
\\ fs [EVERY_MEM]
\\ rw []
\\ Cases_on `e'`
\\ rw []
\\ DEP_REWRITE_TAC [subst_subst_FUNION]
\\ conj_tac
>- (
fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
\\ fs [FDIFF_def,DRESTRICT_DEF,DOMSUB_FAPPLY_THM]
)
\\ fs [FUNION_FUPDATE_2]
\\ simp [Once exp_eq_sym]
\\ irule exp_eq_trans
\\ irule_at Any beta_equality_Letrec
\\ fs [MAP_MAP_o,o_DEF,LAMBDA_PROD,FST_intro,FDIFF_FDIFF,subst_funs_def]
\\ sg `(∀w. w ∈ FRANGE (FDIFF f (set (MAP FST xs) ∪ {v})) ⇒ closed w)`
>- (
rw []
\\ fs [FLOOKUP_DEF,PULL_EXISTS,FRANGE_DEF]
\\ first_x_assum $ qspec_then `x'` assume_tac
\\ gvs []
\\ fs [FDIFF_def,DRESTRICT_DEF]
)
\\ conj_tac
>- (
fs [freevars_subst,DIFF_SUBSET]
\\ fs [SUBSET_UNION]
\\ fs [SUBSET_DEF]
\\ fs [IN_DISJOINT]
\\ rw []
\\ metis_tac []
)
\\ fs [FLOOKUP_UPDATE,FUPDATE_LIST]
\\ sg `closed (Letrec [(v,subst (FDIFF f (set (MAP FST xs) ∪ {v})) x)]
(subst (FDIFF f (set (MAP FST xs) ∪ {v})) x))`
>- (
fs [freevars_subst,DIFF_SUBSET]
\\ fs [SUBSET_UNION]
\\ fs [SUBSET_DEF]
\\ fs [IN_DISJOINT]
\\ rw []
\\ metis_tac []
)
\\ fs [bind1_def]
\\ DEP_REWRITE_TAC [subst_subst_FUNION]
\\ conj_tac
>- (
fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
\\ fs [FDIFF_def,DRESTRICT_DEF,DOMSUB_FAPPLY_THM]
)
\\ fs [FUNION_FUPDATE_2]
\\ simp [Once exp_eq_sym]
\\ qmatch_goalsub_abbrev_tac ‘(subst f1 e1 ≅? subst f2 e2) b’
\\ qsuff_tac ‘f1 = f2’
>- simp [exp_eq_refl]
\\ unabbrev_all_tac
\\ fs [UNION_COMM]
\\ MK_COMB_TAC \\ fs []
\\ sg `FDIFF f ({v} ∪ set (MAP FST xs)) = FDIFF (FDIFF f {v}) (set (MAP FST xs))`
>- fs [FDIFF_FDIFF]
\\ fs []
\\ irule subst_FDIFF'
\\ rw []
\\ fs [IN_DISJOINT]
\\ res_tac
\\ metis_tac []
)
\\ DEP_REWRITE_TAC [subst_subst_FUNION]
\\ conj_tac
>- (
fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
\\ fs [FDIFF_def,DRESTRICT_DEF,DOMSUB_FAPPLY_THM]
)
\\ fs [FUNION_FUPDATE_2]
\\ simp [Once exp_eq_sym]
\\ irule exp_eq_trans
\\ irule_at Any beta_equality_Letrec
\\ fs [MAP_MAP_o,o_DEF,LAMBDA_PROD,FST_intro,FDIFF_FDIFF,subst_funs_def]
\\ sg `(∀w. w ∈ FRANGE (FDIFF f (set (MAP FST xs) ∪ {v})) ⇒ closed w)`
>- (
rw []
\\ fs [FLOOKUP_DEF,PULL_EXISTS,FRANGE_DEF]
\\ first_x_assum $ qspec_then `x'` assume_tac
\\ gvs []
\\ fs [FDIFF_def,DRESTRICT_DEF]
)
\\ conj_tac
>- (
fs [freevars_subst,DIFF_SUBSET]
\\ fs [SUBSET_UNION]
\\ fs [SUBSET_DEF]
\\ fs [IN_DISJOINT]
\\ rw []
\\ metis_tac []
)
\\ fs [FLOOKUP_UPDATE,FUPDATE_LIST]
\\ sg `closed (Letrec [(v,subst (FDIFF f (set (MAP FST xs) ∪ {v})) x)]
(subst (FDIFF f (set (MAP FST xs) ∪ {v})) x))`
>- (
fs [freevars_subst,DIFF_SUBSET]
\\ fs [SUBSET_UNION]
\\ fs [SUBSET_DEF]
\\ fs [IN_DISJOINT]
\\ rw []
\\ metis_tac []
)
\\ fs [bind1_def]
\\ DEP_REWRITE_TAC [subst_subst_FUNION]
\\ conj_tac
>- (
fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
\\ fs [FDIFF_def,DRESTRICT_DEF,DOMSUB_FAPPLY_THM]
)
\\ fs [FUNION_FUPDATE_2]
\\ simp [Once exp_eq_sym]
\\ qmatch_goalsub_abbrev_tac ‘(subst f1 e1 ≅? subst f2 e2) b’
\\ qsuff_tac ‘f1 = f2’
>- simp [exp_eq_refl]
\\ unabbrev_all_tac
\\ fs [UNION_COMM]
\\ MK_COMB_TAC \\ fs []
\\ sg `FDIFF f ({v} ∪ set (MAP FST xs)) = FDIFF (FDIFF f {v}) (set (MAP FST xs))`
>- fs [FDIFF_FDIFF]
\\ fs []
\\ irule subst_FDIFF'
\\ rw []
\\ fs [IN_DISJOINT]
\\ res_tac
\\ metis_tac []
QED
Theorem Lets_Letrec:
EVERY (λ(v, e). v ∉ set (MAP FST xs) ∧ v ∉ vars_of xs) l ⇒
(Lets xs (Letrec l y) ≅? Letrec (MAP (λ(v, e). (v, Lets xs e)) l) (Lets xs y)) b
Proof
rw []
\\ Induct_on `xs` \\ rw [Lets_def]
>- (
irule exp_eq_Letrec_cong
\\ fs [MAP_MAP_o, o_DEF,LAMBDA_PROD,FST]
\\ conj_tac
>- fs [FST_intro]
\\ simp [exp_eq_refl]
\\ fs [LIST_REL_MAP_MAP, LAMBDA_PROD, SND]
\\ fs [EVERY_MEM]
\\ rw []
\\ Cases_on `e` \\ rw []
\\ simp [exp_eq_refl]
)
\\ Cases_on `h` \\ rw [Lets_def]
\\ qsuff_tac `(Let q r (Lets xs (Letrec l y)) ≅?
Letrec (MAP (λ(v,t). (v,Let q r t)) (MAP (λ(v,t). (v,Lets xs t)) l))
(Let q r (Lets xs y))) b`
>- fs [MAP_MAP_o, o_DEF, LAMBDA_PROD]
\\ irule exp_eq_trans
\\ irule_at Any Let_Letrec
\\ fs [vars_of_def]
\\ fs [EVERY_MAP, LAMBDA_PROD]
\\ fs [MAP_MAP_o, o_DEF, LAMBDA_PROD, FST_intro]
\\ conj_tac
>-
(fs [EVERY_MEM]
\\ rw [MEM_MAP]
\\ PairCases_on `e` \\ rw []
\\ last_x_assum $ qspec_then `(e0,e1)` assume_tac
\\ gvs [])
\\ sg `EVERY
(λv.
(v ≠ q ∧ ¬MEM v (MAP FST xs)) ∧ v ∉ freevars r ∧
v ∉ vars_of xs) (MAP FST l)`
>- (fs [EVERY_MAP, LAMBDA_PROD, FST, SF CONJ_ss]
\\ gvs [EVERY_MEM,FORALL_PROD] \\ metis_tac [])
\\ conj_tac >-
(fs [EVERY_MEM]
\\ reverse $ Cases_on `MEM q (MAP FST l)`
>- simp []
\\ last_x_assum $ qspec_then `q` assume_tac
\\ gvs [])
\\ conj_tac >-
(fs [EVERY_MEM, IN_DISJOINT]
\\ rw []
\\ last_x_assum $ qspec_then `x` assume_tac
\\ Cases_on `MEM x (MAP FST l)` \\ rw [])
\\ irule exp_eq_Let_cong
\\ simp [exp_eq_refl]
\\ first_x_assum irule
\\ fs [EVERY_MEM]
\\ rw []
\\ first_x_assum $ qspec_then `e` assume_tac
\\ gvs []
\\ PairCases_on `e` \\ simp []
\\ gvs []
QED
Theorem Lets_Prim:
(Lets xs (Prim op ys) ≅? Prim op (MAP (Lets xs) ys)) b
Proof
rw []
\\ Induct_on `xs` \\ rw [Lets_def]
>- (
irule exp_eq_Prim_cong
\\ simp [LIST_REL_MAP2,Lets_def]
\\ simp [LIST_REL_EL_EQN]
\\ simp [exp_eq_refl]
)
\\ PairCases_on `h` \\ rw [Lets_def]
\\ qsuff_tac `(Let h0 h1 (Lets xs (Prim op ys)) ≅?
Prim op (MAP (Let h0 h1) (MAP (Lets xs) ys))) b`
>-
(rw []
\\ fs [MAP_MAP_o, o_DEF,Lets_def]
\\ irule exp_eq_trans
\\ first_x_assum $ irule_at Any
\\ qsuff_tac `(MAP (λx. Let h0 h1 (Lets xs x)) ys) = (MAP (Lets ((h0,h1)::xs)) ys)`
>- simp [exp_eq_refl]
\\ simp [MAP_EQ_EVERY2,Lets_def]
\\ simp [EVERY2_refl_EQ])
\\ rw [Lets_def]
\\ irule exp_eq_trans
\\ irule_at Any Let_Prim
\\ irule exp_eq_Let_cong
\\ simp [exp_eq_refl,Lets_def]
QED
Theorem Lets_cong:
(x ≅? y) b ⇒ (Lets xs x ≅? Lets xs y) b
Proof
rw []
\\ Induct_on `xs`
>- rw [Lets_def]
\\ PairCases \\ rw [Lets_def]
\\ irule exp_eq_Let_cong
\\ rw [exp_eq_refl]
QED
Theorem Lets_append:
∀xs ys e. Lets (xs ++ ys) e = Lets xs (Lets ys e)
Proof
rw []
\\ Induct_on `xs`
>- rw [Lets_def]
\\ Cases_on `h`
\\ rw [Lets_def]
QED
Theorem Let_ignore:
∀v t e. v ∉ freevars e ⇒ (Let v t e ≅? e) b
Proof
rw []
\\ irule exp_eq_subst_IMP_exp_eq
\\ rw []
\\ simp [subst_def]
\\ irule exp_eq_trans
\\ irule_at Any beta_equality
\\ conj_asm1_tac
>- (
irule IMP_closed_subst
\\ fs [FLOOKUP_DEF, PULL_EXISTS, FRANGE_DEF]
)
\\ sg ‘v ∉ freevars (subst (f \\ v) e)’
>- (
sg ‘(∀w. w ∈ FRANGE (f \\ v) ⇒ closed w)’
>- (
simp [FRANGE_FLOOKUP]
\\ rw [EQ_IMP_THM]
\\ first_x_assum irule
\\ qexists ‘k’
\\ rw []
\\ fs [DOMSUB_FLOOKUP_THM]
)
\\ simp [freevars_subst]
)
\\ simp [subst1_notin]
\\ simp [GSYM subst_fdomsub]
\\ simp [exp_eq_refl]
QED
Theorem Letrec1_ignore:
∀v t e. v ∉ freevars e ⇒ (Letrec [(v, t)] e ≅? e) b
Proof
rw []
\\ irule pure_exp_relTheory.eval_wh_IMP_exp_eq
\\ rw [] \\ gvs [subst_def,eval_wh_Letrec,subst_funs_def,bind_def]
\\ gvs [FUPDATE_LIST,FLOOKUP_UPDATE]
\\ DEP_REWRITE_TAC [freevars_subst]
\\ conj_tac
>- fs [FRANGE_DEF,FLOOKUP_DEF,PULL_EXISTS,FDIFF_def,DRESTRICT_DEF]
\\ reverse IF_CASES_TAC >- gvs [SUBSET_DEF]
\\ AP_TERM_TAC
\\ irule EQ_TRANS
\\ irule_at Any subst1_ignore
\\ DEP_REWRITE_TAC [freevars_subst]
\\ conj_tac
>- fs [FRANGE_DEF,FLOOKUP_DEF,PULL_EXISTS,FDIFF_def,DRESTRICT_DEF]
\\ gvs []
\\ once_rewrite_tac [EQ_SYM_EQ]
\\ irule subst_FDIFF'
\\ gvs []
QED
Theorem Let_Letrec1:
∀v t w u e.
v ≠ w ∧ v ∉ freevars u ∧ w ∉ freevars t ⇒
(Let w u (Letrec [(v, t)] e) ≅? Letrec [(v, t)] (Let w u e)) b
Proof
rw []
\\ qspecl_then [‘w’, ‘u’, ‘[(v, t)]’, ‘e’] assume_tac Let_Letrec
\\ fs [MAP]
\\ gvs []
\\ irule exp_eq_trans
\\ first_x_assum $ irule_at Any
\\ irule exp_eq_Letrec_cong
\\ rw [exp_eq_refl,MAP]
\\ irule Let_ignore
\\ rw []
QED
Theorem Letrec1_Let:
∀v t w u e.
v ≠ w ∧ v ∉ freevars u ∧ w ∉ freevars t ⇒
(Letrec [(v, t)] (Let w u e) ≅? Let w u (Letrec [(v, t)] e)) b
Proof
rw []
\\ simp [Once exp_eq_sym]
\\ irule Let_Letrec1
\\ rw []
QED
Theorem Let_Let:
∀v t w u e.
v ≠ w ∧ v ∉ freevars u ∧ w ∉ freevars t ⇒
(Let v t (Let w u e) ≅? Let w u (Let v t e)) b
Proof
rw []
\\ irule exp_eq_trans
\\ irule_at Any Let_App
\\ irule exp_eq_App_cong
\\ rw []
>- (
irule Let_ignore
\\ rw []
)
\\ irule exp_eq_trans
\\ irule_at Any Let_Lam
\\ rw []
\\ irule exp_eq_refl
QED
Theorem set_DIFF_DELETE_EMPTY:
a ⊆ b ∧
x ∉ a ⇒
a DIFF (b DELETE x) = ∅
Proof
fs [EXTENSION,SUBSET_DEF] \\ metis_tac []
QED
Theorem Lets_copy:
∀e x.
lets_ok [e] ⇒
(Lets [e] x ≅? Lets [e; e] x) b
Proof
rw []
\\ Induct_on `e` \\ rw []
\\ fs [lets_ok_def,Lets_def]
\\ irule exp_eq_subst_IMP_exp_eq
\\ rw []
\\ simp [subst_def]
\\ irule exp_eq_trans
\\ irule_at Any beta_equality
\\ conj_asm1_tac