forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_wordsScript.sml
2213 lines (2132 loc) · 82.5 KB
/
pull_wordsScript.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
(**
Implementation and correctness proof of the global constant lifting
(Section 7.2)
**)
open semanticPrimitivesTheory evaluateTheory
icing_rewriterTheory icing_optimisationsTheory
icing_optimisationProofsTheory fpOptTheory fpValTreeTheory
namespacePropsTheory ml_progTheory
optPlannerTheory source_to_source2Theory source_to_source2ProofsTheory;
open preamble;
val _ = new_theory "pull_words";
Triviality exp_size_lemma:
(∀f n e l. MEM (f,n,e) l ⇒ exp_size e ≤ exp1_size l) ∧
(∀n e l. MEM (n,e) l ⇒ exp_size e ≤ exp3_size l) ∧
(∀e l. MEM e l ⇒ exp_size e ≤ exp6_size l)
Proof
rpt conj_tac \\ Induct_on ‘l’ \\ fs []
\\ rw [] \\ gvs [astTheory.exp_size_def] \\ res_tac \\ gvs []
\\ first_x_assum drule \\ fs []
QED
Definition gather_constants_exp_def:
gather_constants_exp (Lit (Word64 w)) = [w] ∧
gather_constants_exp (FpOptimise sc e) = gather_constants_exp e ∧
gather_constants_exp (Lit l) = [] ∧
gather_constants_exp (Var x) = [] ∧
gather_constants_exp (Raise e) = gather_constants_exp e ∧
gather_constants_exp (Handle e pes) =
(gather_constants_exp e) ++
(FLAT (MAP (λ (p,e). gather_constants_exp e) pes)) ∧
gather_constants_exp (Con mod exps) =
FLAT (MAP gather_constants_exp exps) ∧
gather_constants_exp (Fun s e) = gather_constants_exp e ∧
gather_constants_exp (App op exps) = FLAT (MAP gather_constants_exp exps) ∧
gather_constants_exp (Log lop e2 e3) =
(gather_constants_exp e2) ++ (gather_constants_exp e3) ∧
gather_constants_exp (If e1 e2 e3) =
(gather_constants_exp e1) ++ (gather_constants_exp e2) ++
(gather_constants_exp e3) ∧
gather_constants_exp (Mat e pes) =
(gather_constants_exp e) ++
FLAT ((MAP (λ (p,e). gather_constants_exp e) pes)) ∧
gather_constants_exp (Let so e1 e2) =
(gather_constants_exp e1) ++ (gather_constants_exp e2) ∧
gather_constants_exp (Letrec ses e) =
(gather_constants_exp e) ∧
gather_constants_exp (Tannot e t) =
(gather_constants_exp e) ∧
gather_constants_exp (Lannot e l) =
(gather_constants_exp e)
Termination
WF_REL_TAC ‘measure (λ e. exp_size e)’
\\ rw [astTheory.exp_size_def]
\\ imp_res_tac exp_size_lemma \\ gvs []
End
Definition gather_used_identifiers_pat_def:
gather_used_identifiers_pat Pany = [] ∧
gather_used_identifiers_pat (Pvar v) = [v] ∧
gather_used_identifiers_pat (Plit _) = [] ∧
gather_used_identifiers_pat (Pref p) = gather_used_identifiers_pat p ∧
gather_used_identifiers_pat (Ptannot p _) = gather_used_identifiers_pat p ∧
gather_used_identifiers_pat (Pcon (SOME id) pats) =
(let used_in_pats = FLAT (MAP gather_used_identifiers_pat pats) in
case id of
| (Short v) => v::used_in_pats
| (Long m (Short v)) => m::v::used_in_pats) ∧
gather_used_identifiers_pat (Pcon NONE pats) =
FLAT (MAP gather_used_identifiers_pat pats)
Termination
WF_REL_TAC ‘measure (λ p. pat_size p)’ \\ fs[]
\\ rpt conj_tac
\\ fs[astTheory.pat_size_def]
\\ Induct_on ‘pats’ \\ rpt strip_tac \\ fs[astTheory.pat_size_def]
\\ ‘∀ x l. MEM x l ⇒ pat_size x ≤ pat1_size l’ by (
rpt strip_tac
\\ Induct_on ‘l’ \\ fs[]
\\ rpt strip_tac
\\ fs[astTheory.pat_size_def]
)
\\ pop_assum imp_res_tac \\ fs[]
End
Definition gather_used_identifiers_exp_def:
gather_used_identifiers_exp (FpOptimise sc e) =
gather_used_identifiers_exp e ∧
gather_used_identifiers_exp (Lit l) = [] ∧
gather_used_identifiers_exp (Var x) =
(case x of
| Short v => [v]
| _ => []) ∧
gather_used_identifiers_exp (Raise e) = gather_used_identifiers_exp e ∧
gather_used_identifiers_exp (Handle e pes) =
(gather_used_identifiers_exp e) ++
(FLAT (MAP (λ (p,e). pat_bindings p [] ++
(gather_used_identifiers_exp e)) pes)) ∧
gather_used_identifiers_exp (Con mod exps) =
FLAT (MAP gather_used_identifiers_exp exps) ∧
gather_used_identifiers_exp (Fun s e) = [s] ++ gather_used_identifiers_exp e ∧
gather_used_identifiers_exp (App op exps) =
FLAT (MAP gather_used_identifiers_exp exps) ∧
gather_used_identifiers_exp (Log lop e2 e3) =
(gather_used_identifiers_exp e2) ++ (gather_used_identifiers_exp e3) ∧
gather_used_identifiers_exp (If e1 e2 e3) =
(gather_used_identifiers_exp e1) ++ (gather_used_identifiers_exp e2) ++
(gather_used_identifiers_exp e3) ∧
gather_used_identifiers_exp (Mat e pes) =
(gather_used_identifiers_exp e) ++
FLAT ((MAP (λ (p,e). pat_bindings p [] ++
gather_used_identifiers_exp e) pes)) ∧
gather_used_identifiers_exp (Let so e1 e2) =
(let expression_identifiers =
(gather_used_identifiers_exp e1) ++ (gather_used_identifiers_exp e2) in
case so of
| NONE => expression_identifiers
| SOME n => n::expression_identifiers) ∧
gather_used_identifiers_exp (Letrec ses e) =
FLAT (MAP (λ (n, p, e). n :: p :: gather_used_identifiers_exp e) ses) ++
(gather_used_identifiers_exp e) ∧
gather_used_identifiers_exp (Tannot e t) =
(gather_used_identifiers_exp e) ∧
gather_used_identifiers_exp (Lannot e l) =
(gather_used_identifiers_exp e)
Termination
WF_REL_TAC ‘measure (λ e. exp_size e)’
\\ rw [astTheory.exp_size_def]
\\ imp_res_tac exp_size_lemma \\ gvs []
End
(**
Walk over an AST and replace constants by variables that globally allocate
their value
**)
Definition replace_constants_exp_def:
replace_constants_exp al (Lit (Word64 w)) =
(case (ALOOKUP al w) of
| NONE => Lit (Word64 w)
| SOME v => (Var (Short v))) ∧
replace_constants_exp al (FpOptimise sc e) =
FpOptimise sc (replace_constants_exp al e) ∧
replace_constants_exp al (Lit l) = (Lit l) ∧
replace_constants_exp al (Var x) = (Var x) ∧
replace_constants_exp al (Raise e) = Raise (replace_constants_exp al e) ∧
replace_constants_exp al (Handle e pes) =
Handle (replace_constants_exp al e)
(MAP (λ (p,e). (p, replace_constants_exp al e)) pes) ∧
replace_constants_exp al (Con mod exps) =
Con mod (MAP (replace_constants_exp al) exps) ∧
replace_constants_exp al (Fun s e) = Fun s (replace_constants_exp al e) ∧
replace_constants_exp al (App op exps) =
App op (MAP (replace_constants_exp al) exps) ∧
replace_constants_exp al (Log lop e2 e3) =
Log lop (replace_constants_exp al e2) (replace_constants_exp al e3) ∧
replace_constants_exp al (If e1 e2 e3) =
If (replace_constants_exp al e1) (replace_constants_exp al e2) (replace_constants_exp al e3) ∧
replace_constants_exp al (Mat e pes) =
Mat (replace_constants_exp al e) ((MAP (λ (p,e). (p, replace_constants_exp al e)) pes)) ∧
replace_constants_exp al (Let so e1 e2) =
Let so (replace_constants_exp al e1) (replace_constants_exp al e2) ∧
replace_constants_exp al (Letrec ses e) =
Letrec (MAP (λ(f,n,e). (f,n,replace_constants_exp al e)) ses)
(replace_constants_exp al e) ∧
replace_constants_exp al (Tannot e t) =
Tannot (replace_constants_exp al e) t ∧
replace_constants_exp al (Lannot e l) =
Lannot (replace_constants_exp al e) l
Termination
WF_REL_TAC ‘measure (λ (al, e). exp_size e)’
\\ rw [astTheory.exp_size_def]
\\ imp_res_tac exp_size_lemma \\ gvs []
End
Definition build_cnst_list_def:
build_cnst_list [] vars n = [] ∧
build_cnst_list (w1::ws) vars n =
let newName = STRCAT ("GLOB_CONST") (toString n) in
if (MEM newName vars)
then build_cnst_list ws vars (SUC n)
else (w1, newName)::build_cnst_list ws (newName::vars) (SUC n)
End
Definition build_decl_list_def:
build_decl_list [] = [] ∧
build_decl_list ((w1,x)::ws) =
(Dlet unknown_loc (Pvar x) (Lit (Word64 w1))) :: build_decl_list ws
End
Definition gather_constants_decl_def:
gather_constants_decl [Dlet loc p e] =
gather_constants_exp e ∧
gather_constants_decl (d1::d2::ds) =
(gather_constants_decl [d1] ++ gather_constants_decl (d2::ds)) ∧
gather_constants_decl [d] = []
End
Definition gather_used_identifiers_decl_def:
gather_used_identifiers_decl [Dlet loc p e] =
(pat_bindings p [] ++ gather_used_identifiers_exp e) ∧
gather_used_identifiers_decl (d1::d2::ds) =
(gather_used_identifiers_decl [d1] ++ gather_used_identifiers_decl (d2::ds)) ∧
gather_used_identifiers_decl [Dlocal lds ds] =
gather_used_identifiers_decl lds ++ gather_used_identifiers_decl ds ∧
gather_used_identifiers_decl [Dletrec locs funs] =
FLAT (MAP (λ (n,m,e). n :: m :: gather_used_identifiers_exp e) funs) ∧
gather_used_identifiers_decl [Dmod mn ds] =
gather_used_identifiers_decl ds ∧
gather_used_identifiers_decl [d] = []
Termination
wf_rel_tac ‘measure dec1_size’
End
Definition replace_constants_decl_def:
replace_constants_decl [Dlet loc p e] ws =
[Dlet loc p (replace_constants_exp ws e)] ∧
replace_constants_decl (d1::d2::ds) ws =
replace_constants_decl [d1] ws ++ replace_constants_decl (d2::ds) ws ∧
replace_constants_decl [Dletrec locs funs] ws =
[Dletrec locs (MAP (I ## I ## replace_constants_exp ws) funs)] ∧
replace_constants_decl [Dmod mn ds] ws =
[Dmod mn (replace_constants_decl ds ws)] ∧
replace_constants_decl [Dlocal lds ds] ws =
[Dlocal (replace_constants_decl lds ws) (replace_constants_decl ds ws)] ∧
replace_constants_decl [d] ws = [d] ∧
replace_constants_decl [] ws = []
Termination
wf_rel_tac ‘measure (λ (ds, w). dec1_size ds)’
End
Definition lift_constants_decl_def:
lift_constants_decl ds =
let cnsts = gather_constants_decl ds;
vars = gather_used_identifiers_decl ds;
cnst_lst = build_cnst_list cnsts vars 0;
in
(build_decl_list cnst_lst) ++ (replace_constants_decl ds cnst_lst)
End
Inductive v_rel:
(∀v. v_rel (Litv v) (Litv v))
∧
(∀r. v_rel (Real r) (Real r))
∧
(∀r. v_rel (FP_WordTree r) (FP_WordTree r))
∧
(∀r. v_rel (FP_BoolTree r) (FP_BoolTree r))
∧
(∀r. v_rel (Loc r) (Loc r))
∧
(∀ env id. v_rel (Env env id) (Env env id))
∧
(∀s vs vs1.
LIST_REL v_rel vs vs1 ⇒
v_rel (Conv s vs) (Conv s vs1))
∧
(∀vs vs1.
LIST_REL v_rel vs vs1 ⇒
v_rel (Vectorv vs) (Vectorv vs1))
∧
(∀env v e env1.
DISJOINT (set (v::gather_used_identifiers_exp e)) (set (MAP SND al)) ∧
env_rel env env1 al ⇒
v_rel (Closure env v e) (Closure env1 v (replace_constants_exp al e)))
∧
(∀env v env1 funs.
DISJOINT (set (FLAT (MAP (λ(n,m,e). n::m::gather_used_identifiers_exp e) funs)))
(set (MAP SND al)) ∧
env_rel env env1 al ⇒
v_rel (Recclosure env funs v)
(Recclosure env1 (MAP (I ## I ## replace_constants_exp al) funs) v))
∧
(∀env env1.
env1.c = env.c ∧
(∀n v.
nsLookup env1.v n = SOME v ⇒
if nsLookup env.v n = NONE then
∃m. n = Short m ∧ MEM m (MAP SND al)
else ~∃m. n = Short m ∧ MEM m (MAP SND al)) ∧
(∀n w.
MEM (w,n) al ⇒ nsLookup env1.v (Short n) = SOME (Litv (Word64 w))) ∧
(∀n v.
nsLookup env.v n = SOME v ⇒
∃v1. v_rel v v1 ∧ nsLookup env1.v n = SOME v1) ⇒
env_rel env env1 al)
End
Theorem v_rel_simp[simp] =
[“v_rel (Litv v) w”,
“v_rel (Real r) w”,
“v_rel (FP_WordTree r) w”,
“v_rel (FP_BoolTree r) w”,
“v_rel (Loc r) w”,
“v_rel (Env env id) w”,
“v_rel (Conv s vs) w”,
“v_rel (Vectorv vs) w”,
“v_rel (Closure env v e) w”,
“v_rel (Recclosure env funs v) w”,
“v_rel w (Litv v)”,
“v_rel w (Real r)”,
“v_rel w (FP_WordTree r)”,
“v_rel w (FP_BoolTree r)”,
“v_rel w (Loc r)”,
“v_rel w (Env env id)”,
“v_rel w (Conv s vs)”,
“v_rel w (Vectorv vs)”,
“v_rel w (Closure env v e)”,
“v_rel w (Recclosure env funs v)”]
|> map (SIMP_CONV (srw_ss()) [Once v_rel_cases]) |> LIST_CONJ;
Theorem env_rel_def =
v_rel_cases |> CONJUNCT2 |> SIMP_RULE std_ss []
|> Q.SPECL [‘env1’, ‘env2’, ‘ws’]
|> Q.GEN ‘ws’ |> Q.GEN ‘env2’ |> Q.GEN ‘env1’;
Definition res_rel_def[simp]:
res_rel (Rval x) (Rval y) = LIST_REL v_rel x y ∧
res_rel (Rerr (Rraise v)) (Rerr (Rraise w)) = v_rel v w ∧
res_rel (Rerr (Rabort a)) (Rerr (Rabort b)) = (a = b) ∧
res_rel _ _ = F
End
Definition res1_rel_def[simp]:
res1_rel (Rval x) (Rval y) = v_rel x y ∧
res1_rel (Rerr (Rraise v)) (Rerr (Rraise w)) = v_rel v w ∧
res1_rel (Rerr (Rabort a)) (Rerr (Rabort b)) = (a = b) ∧
res1_rel _ _ = F
End
Definition ref_rel_def[simp]:
ref_rel (Refv v) (Refv w) = v_rel v w ∧
ref_rel (Varray vs) (Varray ws) = LIST_REL v_rel vs ws ∧
ref_rel (W8array bs) (W8array as) = (bs = as) ∧
ref_rel _ _ = F
End
Definition state_rel_def:
state_rel s t ⇔
t.clock = s.clock ∧
t.ffi = s.ffi ∧
t.next_type_stamp = s.next_type_stamp ∧
t.next_exn_stamp = s.next_exn_stamp ∧
t.fp_state = s.fp_state ∧
LIST_REL ref_rel s.refs t.refs
End
Inductive env_rel_strict:
env2.c = env1.c ∧
(∀ x v.
nsLookup env2.v x = SOME v ⇒
((∃ v1. nsLookup env1.v x = SOME v1 ∧ v_rel v1 v) ∧
(∀ m. x = Short m ⇒ ~ MEM m (MAP SND al)))) ∧
(∀ x v.
nsLookup env1.v x = SOME v ⇒
((∃ v2. nsLookup env2.v x = SOME v2 ∧ v_rel v v2) ∧
(∀ m. x = Short m ⇒ ~ MEM m (MAP SND al)))) ∧
(∀ x.
nsLookup env2.v x = NONE ⇒
nsLookup env1.v x = NONE) ∧
(∀ x.
nsLookup env1.v x = NONE ⇒
nsLookup env1.v x = NONE) ∧
(∀ (x:(string,string) id) p1 p2 (e3:(string,string,v) namespace).
p1 ≠ [] ∧ id_to_mods x = p1 ++ p2 ⇒
(nsLookupMod env1.v p1 = NONE ⇔ nsLookupMod env2.v p1 = NONE) ∧
(∀ env.
nsLookupMod env1.v p1 = SOME env ⇒
∃ env'. nsLookupMod env2.v p1 = SOME env' ∧
env_rel_strict <| v := env; c := nsEmpty |> <| v := env'; c := nsEmpty |> al) ∧
(∀ env.
nsLookupMod env2.v p1 = SOME env ⇒
∃ env'. nsLookupMod env1.v p1 = SOME env' ∧
env_rel_strict <| v := env'; c := nsEmpty |> <| v := env; c := nsEmpty |> al)) ⇒
env_rel_strict env1 env2 al
End
Theorem do_opapp_SOME_IMP:
do_opapp (REVERSE a) = SOME x ⇒ ∃a1 a2. a = [a1;a2]
Proof
fs [do_opapp_def,AllCaseEqs()] \\ rw []
\\ gvs [SWAP_REVERSE_SYM]
QED
Theorem do_opapp_NONE:
LIST_REL v_rel a a' ⇒
(do_opapp (REVERSE a) = NONE ⇔
do_opapp (REVERSE a') = NONE)
Proof
once_rewrite_tac [GSYM LIST_REL_REVERSE_EQ]
\\ qspec_tac (‘REVERSE a’,‘a’)
\\ qspec_tac (‘REVERSE a'’,‘a'’)
\\ fs [do_opapp_def,AllCaseEqs(),PULL_EXISTS]
\\ rw [] \\ eq_tac \\ rw []
\\ gvs [] \\ rw []
\\ fs [MAP_MAP_o,o_DEF,UNCURRY,LAMBDA_PROD]
\\ qpat_x_assum ‘_ = NONE’ mp_tac
\\ rename [‘_ (MAP _ funs2)’]
\\ rename [‘_ nn _ = NONE’]
\\ qid_spec_tac ‘funs2’
\\ Induct \\ fs [FORALL_PROD]
\\ once_rewrite_tac [find_recfun_def]
\\ fs [] \\ rw []
QED
Theorem list_rel_lookup_some_l:
∀ (xs:(string#v) list) ys x v1.
LIST_REL (λ (s1, x1) (s2, x2). s1 = s2 ∧ v_rel x1 x2) xs ys ∧
ALOOKUP xs x = SOME v1 ⇒
∃ v2.
nsLookup (alist_to_ns ys) ((Short x):(string,string) id) = SOME v2 ∧
v_rel v1 v2
Proof
Induct_on ‘xs’ >> gs[]
>> rpt strip_tac >> gs[] >> Cases_on ‘h’ >> gs[ALOOKUP_def]
>> Cases_on ‘y’ >> gs[] >> rveq >> gs[ml_progTheory.nsLookup_nsBind_compute]
>> Cases_on ‘q = x’ >> gs[]
QED
Theorem list_rel_lookup_some_r:
∀ (xs:(string#v) list) ys x v1.
LIST_REL (λ (s1, x1) (s2, x2). s1 = s2 ∧ v_rel x1 x2) xs ys ∧
ALOOKUP ys x = SOME v1 ⇒
∃ v2.
nsLookup (alist_to_ns xs) ((Short x):(string,string) id) = SOME v2 ∧
v_rel v2 v1
Proof
Induct_on ‘ys’ >> gs[]
>> rpt strip_tac >> gs[] >> Cases_on ‘h’ >> gs[ALOOKUP_def]
>> Cases_on ‘x'’ >> gs[] >> rveq >> gs[ml_progTheory.nsLookup_nsBind_compute]
>> Cases_on ‘q = x’ >> gs[]
QED
Theorem list_rel_lookup_none_r:
∀ xs ys x v1.
LIST_REL (λ (s1, x1) (s2, x2). s1 = s2 ∧ v_rel x1 x2) xs ys ∧
nsLookup ((alist_to_ns ys):(string,string,v) namespace) x = NONE ⇒
nsLookup (alist_to_ns xs) x = NONE
Proof
Induct_on ‘ys’ >> gs[]
>> rpt strip_tac >> gs[] >> Cases_on ‘h’ >> gs[ALOOKUP_def]
>> Cases_on ‘x'’ >> gs[] >> rveq
>> Cases_on ‘x’ >> gs[ml_progTheory.nsLookup_nsBind_compute]
>> Cases_on ‘q = n’ >> gs[]
QED
Theorem list_rel_lookup_none_l:
∀ xs ys x v1.
LIST_REL (λ (s1, x1) (s2, x2). s1 = s2 ∧ v_rel x1 x2) xs ys ∧
nsLookup ((alist_to_ns xs):(string,string,v) namespace) x = NONE ⇒
nsLookup (alist_to_ns ys) x = NONE
Proof
Induct_on ‘xs’ >> gs[]
>> rpt strip_tac >> gs[] >> Cases_on ‘h’ >> gs[ALOOKUP_def]
>> Cases_on ‘y’ >> gs[] >> rveq
>> Cases_on ‘x’ >> gs[ml_progTheory.nsLookup_nsBind_compute]
>> Cases_on ‘q = n’ >> gs[]
QED
Theorem env_rel_update_lemma:
env_rel (env with v := t) (env' with v := t') al ∧
¬MEM n (MAP SND al) ∧ v_rel x y ⇒
env_rel (env with v := nsBind n x t)
(env' with v := nsBind n y t') al
Proof
simp [env_rel_def] \\ rpt strip_tac
THEN1
(reverse (Cases_on ‘n'’) \\ fs [ml_progTheory.nsLookup_nsBind_compute]
THEN1 (first_x_assum drule \\ fs [])
\\ CASE_TAC \\ gvs []
\\ first_x_assum drule \\ fs [])
THEN1
(fs [MEM_MAP,EXISTS_PROD,ml_progTheory.nsLookup_nsBind_compute]
\\ rw [] \\ gvs [])
\\ Cases_on ‘n'’ \\ fs [ml_progTheory.nsLookup_nsBind_compute]
\\ rw [] \\ fs []
QED
Theorem env_rel_update:
env_rel env env' al ∧ ¬MEM n (MAP SND al) ∧ v_rel x y ⇒
env_rel (env with v := nsBind n x env.v)
(env' with v := nsBind n y env'.v) al
Proof
rw [] \\ irule env_rel_update_lemma \\ gvs []
QED
Theorem env_rel_strict_update_lemma:
env_rel_strict (env with v := t) (env' with v := t') al ∧
¬MEM n (MAP SND al) ∧ v_rel x y ⇒
env_rel_strict (env with v := nsBind n x t)
(env' with v := nsBind n y t') al
Proof
simp [Once env_rel_strict_cases] >> rpt strip_tac
>> simp[Once env_rel_strict_cases] >> rpt strip_tac
>- (
Cases_on ‘x'’ >> gs[nsLookup_nsBind_compute]
>> IF_CASES_TAC >> gs[])
>- (
Cases_on ‘x'’ >> gs[nsLookup_nsBind_compute]
>> Cases_on ‘n = m’ >> gs[])
>- (
Cases_on ‘x'’ >> gs[nsLookup_nsBind_compute]
>> IF_CASES_TAC >> gs[])
>- (
Cases_on ‘x'’ >> gs[nsLookup_nsBind_compute]
>> Cases_on ‘n = m’ >> gs[])
>- (
Cases_on ‘x'’ >> gs[nsLookup_nsBind_compute]
>> IF_CASES_TAC >> gs[])
>> Cases_on ‘t’ >> Cases_on ‘t'’ >> gs[namespaceTheory.nsBind_def, namespaceTheory.nsLookupMod_def]
>> first_x_assum $ qspecl_then [‘x'’, ‘p1’, ‘p2’] mp_tac
>> Cases_on ‘p1’
>> gs[namespaceTheory.nsLookupMod_def]
QED
Theorem env_rel_strict_update:
env_rel_strict env env' al ∧ ¬MEM n (MAP SND al) ∧ v_rel x y ⇒
env_rel_strict (env with v := nsBind n x env.v)
(env' with v := nsBind n y env'.v) al
Proof
rw [] \\ irule env_rel_strict_update_lemma \\ gvs []
QED
Theorem env_rel_nsAppend:
env_rel env1 env2 al ∧
env_rel_strict env3 env4 al ⇒
env_rel (extend_dec_env env3 env1) (extend_dec_env env4 env2) al
Proof
rpt strip_tac >> gs[env_rel_def, Once env_rel_strict_cases,extend_dec_env_def]
>> rpt conj_tac
>- (
rpt strip_tac >> reverse $ Cases_on ‘n’ >> gs[nsLookup_nsAppend_some]
>- (
res_tac
>> Cases_on ‘nsLookup env3.v (Long m i)’ >> gs[]
>> ‘nsLookup (nsAppend env3.v env1.v) (Long m i) = SOME x’
by (gs[nsLookup_nsAppend_some])
>> gs[])
>- (
rpt strip_tac >> res_tac
>> Cases_on ‘nsLookup env1.v (Long m i)’ >> gs[]
>> gs[nsLookup_nsAppend_none]
>> Cases_on ‘nsLookup env3.v (Long m i)’ >> gs[]
>> ‘nsLookupMod env4.v p1 = SOME e3’
by (res_tac >> gs[])
>> gs[])
>- (res_tac >> CCONTR_TAC >> gs[nsLookup_nsAppend_none])
>> Cases_on ‘nsLookup env1.v (Short n')’ >> gs[]
>- (
‘nsLookup (nsAppend env3.v env1.v) (Short n') = NONE’
by gs[nsLookup_nsAppend_none]
>> gs[]
>> res_tac >> pop_assum $ mp_tac
>> qpat_x_assum `nsLookup env1.v _ = _` $ rewrite_tac o single
>> gs[])
>> ‘nsLookup (nsAppend env3.v env1.v) (Short n') = SOME x’
by gs[nsLookup_nsAppend_some, namespaceTheory.id_to_mods_def]
>> gs[] >> res_tac
>> qpat_x_assum `if _ then _ else _` mp_tac
>> qpat_x_assum `nsLookup env1.v _ = _` $ rewrite_tac o single
>> gs[])
>- (
rpt strip_tac
>> ‘nsLookup env4.v (Short n) = NONE’
by (CCONTR_TAC >> gs[] >> Cases_on ‘nsLookup env4.v (Short n)’
>> gs[] >> res_tac >> gs[MEM_MAP])
>> res_tac >> gs[nsLookup_nsAppend_some, namespaceTheory.id_to_mods_def])
>> rpt strip_tac >> gs[nsLookup_nsAppend_some]
>- (res_tac >> asm_exists_tac >> gs[])
>> res_tac
>> ‘nsLookup env4.v n = NONE’
by (CCONTR_TAC >> gs[] >> Cases_on ‘nsLookup env4.v n’
>> gs[] >> res_tac >> gs[MEM_MAP])
>> gs[]
>> rpt strip_tac >> Cases_on ‘n’ >> gs[namespaceTheory.id_to_mods_def]
>> CCONTR_TAC
>> Cases_on ‘nsLookupMod env4.v p1’ >> gs[]
>> first_x_assum $ qspecl_then [‘p1’, ‘p2’] mp_tac >> impl_tac >- gs[]
>> rpt strip_tac
>> first_x_assum $ qspecl_then [‘Long m i’, ‘p1’, ‘p2’] mp_tac
>> impl_tac >- gs[namespaceTheory.id_to_mods_def]
>> rpt strip_tac
>> res_tac >> gs[]
QED
fun impl_subgoal_tac th =
let
val hyp_to_prove = lhand (concl th)
in
SUBGOAL_THEN hyp_to_prove (fn thm => assume_tac (MP th thm))
end;
Theorem id_to_mods_defined:
∀ ps. ∃ (id:(string,string) id).
id_to_mods id = ps
Proof
Induct_on ‘ps’
>- (qexists_tac ‘Short "x"’ >> gs[namespaceTheory.id_to_mods_def])
>> rpt strip_tac >> gs[]
>> qexists_tac ‘Long h id’ >> gs[namespaceTheory.id_to_mods_def]
QED
(*
Theorem env_rel_strict_nsAppend:
env_rel_strict env1 env2 al ∧
env_rel_strict env3 env4 al ⇒
env_rel_strict (extend_dec_env env3 env1) (extend_dec_env env4 env2) al
Proof
rpt strip_tac >> gs[env_rel_def, Once env_rel_strict_cases,extend_dec_env_def]
>> last_x_assum $ strip_assume_tac o SIMP_RULE std_ss [Once env_rel_strict_cases]
>> simp[Once env_rel_strict_cases]
>> rpt conj_tac
>- (
rpt strip_tac >> reverse $ Cases_on ‘x’ >> gs[nsLookup_nsAppend_some]
>- (res_tac >> qexists_tac ‘v1’ >> gs[])
>- (
res_tac >> asm_exists_tac >> gs[]
>> rpt strip_tac >> CCONTR_TAC >> gs[]
>> Cases_on ‘nsLookupMod env3.v p1’ >> gs[]
>> res_tac
>> ‘nsLookupMod env4.v p1 = SOME x’ by gs[]
>> gs[])
>- (res_tac >> CCONTR_TAC >> gs[nsLookup_nsAppend_none])
>> res_tac >> asm_exists_tac >> gs[namespaceTheory.id_to_mods_def])
>- (
rpt strip_tac >> reverse $ Cases_on ‘x’ >> gs[nsLookup_nsAppend_some]
>- (res_tac >> qexists_tac ‘v2’ >> gs[])
>- (
res_tac
>> ‘nsLookup env4.v (Long m i) = NONE’
by (Cases_on ‘nsLookup env4.v (Long m i)’ >> gs[]
>> res_tac >> gs[])
>> qexists_tac ‘v2’ >> gs[]
>> rpt strip_tac >> CCONTR_TAC >> gs[]
>> Cases_on ‘nsLookupMod env4.v p1’ >> gs[]
>> res_tac
>> ‘nsLookupMod env3.v p1 = SOME x’ by gs[]
>> gs[])
>- (res_tac >> CCONTR_TAC >> gs[nsLookup_nsAppend_none])
>> res_tac
>> ‘nsLookup env4.v (Short n) = NONE’
by (Cases_on ‘nsLookup env4.v (Short n)’ >> gs[]
>> res_tac >> gs[])
>> qexists_tac ‘v2’ >> gs[namespaceTheory.id_to_mods_def])
>- (
rpt strip_tac >> gs[nsLookup_nsAppend_none]
>> Cases_on ‘nsLookup env1.v x = NONE’ >> gs[]
>> res_tac >> asm_exists_tac >> gs[])
>> rpt strip_tac
>> first_x_assum $ (fn th => mp_tac th >> qspecl_then [‘x’, ‘p1’, ‘p2’] mp_tac th)
>> impl_tac >- gs[]
>> first_x_assum $ (fn th => mp_tac th >> qspecl_then [‘x’, ‘p1’, ‘p2’] mp_tac th)
>> impl_tac >- gs[]
>> rpt strip_tac >> gs[nsLookupMod_nsAppend_some]
>- (
EQ_TAC >> gs[nsLookupMod_nsAppend_none]
>> rpt strip_tac >> gs[]
>- (
DISJ2_TAC >> rpt strip_tac
>> qspec_then ‘p1'++ p2'’ strip_assume_tac id_to_mods_defined
>> MAP_EVERY qexists_tac [‘p1'’, ‘p2'’]
>> last_x_assum $ qspecl_then [‘id’, ‘p1'’, ‘p2'’] mp_tac
>> gs[] >> rpt strip_tac >> gs[])
>> DISJ2_TAC >> rpt strip_tac
>> qspec_then ‘p1'++ p2'’ strip_assume_tac id_to_mods_defined
>> MAP_EVERY qexists_tac [‘p1'’, ‘p2'’]
>> last_x_assum $ qspecl_then [‘id’, ‘p1'’, ‘p2'’] mp_tac
>> gs[] >> rpt strip_tac >> gs[])
>- (qexists_tac ‘env'’ >> gs[])
>- (
rpt strip_tac
>> qspec_then ‘p1'++ p2'’ strip_assume_tac id_to_mods_defined
>> CCONTR_TAC >> gs[]
>> Cases_on ‘nsLookupMod env4.v p1'’ >> gs[]
>> first_x_assum $ qspecl_then [‘p1'’, ‘p2'’] mp_tac
>> impl_tac >- gs[]
>> strip_tac
>> last_x_assum $ qspecl_then [‘id’, ‘p1'’, ‘p2'’] mp_tac
>> impl_tac >- gs[]
>> strip_tac >> gs[])
>- (qexists_tac ‘env'’ >> gs[])
>> rpt strip_tac
>> qspec_then ‘p1'++ p2'’ strip_assume_tac id_to_mods_defined
>> CCONTR_TAC >> gs[]
>> Cases_on ‘nsLookupMod env3.v p1'’ >> gs[]
>> first_x_assum $ qspecl_then [‘p1'’, ‘p2'’] mp_tac
>> impl_tac >- gs[]
>> strip_tac
>> last_x_assum $ qspecl_then [‘id’, ‘p1'’, ‘p2'’] mp_tac
>> impl_tac >- gs[]
>> strip_tac >> gs[]
QED
*)
Theorem env_rel_build_rec_env_decl:
env_rel env1 env2 al ∧
DISJOINT (set (FLAT (MAP (λ(n,m,e). n::m::gather_used_identifiers_exp e) funs)))
(set (MAP SND al)) ⇒
env_rel_strict <| v := (build_rec_env funs env1 nsEmpty); c := nsEmpty |>
<| v := (build_rec_env (MAP (I ## I ## replace_constants_exp al) funs) env2 nsEmpty);
c := nsEmpty |> al
Proof
fs [build_rec_env_def]
\\ qabbrev_tac ‘ff = Recclosure env1 funs’
\\ qabbrev_tac ‘gg = Recclosure env2
(MAP (I ## I ## replace_constants_exp al) funs)’
\\ disch_then (fn th => assume_tac th \\ mp_tac th)
\\ qid_spec_tac ‘funs’ \\ Induct
>- (
gs[Once env_rel_strict_cases]
\\ rpt strip_tac \\ Cases_on ‘p1’ \\ gs[])
\\ fs [FORALL_PROD] \\ rw []
\\ irule env_rel_strict_update_lemma \\ gvs[]
\\ unabbrev_all_tac \\ gvs [PULL_EXISTS]
\\ qexists_tac ‘al’ \\ gs[]
QED
Theorem env_rel_strict_empty:
env_rel_strict env1 env2 al ⇒
env_rel_strict <| v := env1.v; c := nsEmpty |>
<| v := env2.v; c := nsEmpty |> al
Proof
rw[Once env_rel_strict_cases] >> simp[Once env_rel_strict_cases]
>> rpt conj_tac >> TRY (first_x_assum $ MATCH_ACCEPT_TAC)
>> rpt strip_tac >> res_tac >> gs[]
QED
Theorem env_rel_nsLift:
∀ env1 env2 mn.
env_rel_strict env1 env2 al ⇒
env_rel_strict <| v := nsLift mn env1.v; c := nsLift mn env1.c |>
<| v := nsLift mn env2.v; c := nsLift mn env2.c |> al
Proof
rpt gen_tac
>> disch_then (fn th => assume_tac th >> assume_tac (SIMP_RULE std_ss [Once env_rel_strict_cases] th))
>> simp[Once env_rel_strict_cases] >> rpt strip_tac >> gs[]
>- (Cases_on ‘x’ >> gs[nsLookup_nsLift])
>- gs[nsLookup_nsLift]
>- (Cases_on ‘x’ >> gs[nsLookup_nsLift])
>- gs[nsLookup_nsLift]
>- (Cases_on ‘x’ >> gs[nsLookup_nsLift])
>- (
Cases_on ‘x’ >> gs[namespaceTheory.id_to_mods_def, nsLookupMod_nsLift]
>> TOP_CASE_TAC >> gs[]
>> Cases_on ‘mn = h’ >> gs[]
>> Cases_on ‘t’ >> gs[namespaceTheory.nsLookupMod_def]
>> first_x_assum $ qspecl_then [‘i’, ‘h'::t'’, ‘p2’] mp_tac
>> impl_tac >> gs[])
>- (
Cases_on ‘x’ >> gs[namespaceTheory.id_to_mods_def, nsLookupMod_nsLift]
>> TOP_CASE_TAC >> gs[] >> rveq
>> Cases_on ‘t’ >> gs[namespaceTheory.nsLookupMod_def]
>- (rveq >> irule env_rel_strict_empty >> gs[])
>> first_x_assum $ qspecl_then [‘i’, ‘h::t'’, ‘p2’] mp_tac
>> impl_tac >> gs[])
>> Cases_on ‘x’ >> gs[namespaceTheory.id_to_mods_def, nsLookupMod_nsLift]
>> TOP_CASE_TAC >> gs[] >> rveq
>> Cases_on ‘t’ >> gs[namespaceTheory.nsLookupMod_def]
>- (rveq >> irule env_rel_strict_empty >> gs[])
>> first_x_assum $ qspecl_then [‘i’, ‘h::t'’, ‘p2’] mp_tac
>> impl_tac >> gs[]
QED
Theorem env_rel_update_alist:
env_rel env env' al ∧ DISJOINT (set (MAP FST xs)) (set (MAP SND al)) ∧
LIST_REL (λ (s1, x1) (s2, x2). s1 = s2 ∧ v_rel x1 x2) xs ys ⇒
env_rel <| v := nsAppend ((alist_to_ns xs):(string,string,v) namespace) env.v; c := env.c |>
<| v := nsAppend ((alist_to_ns ys):(string,string,v) namespace) env'.v; c := env.c |> al
Proof
Induct_on ‘xs’ >> simp[env_rel_def]
>> rpt strip_tac
>- (
gs[namespacePropsTheory.nsLookup_nsAppend_some,
namespacePropsTheory.nsLookup_alist_to_ns_some,
ml_progTheory.nsLookup_nsAppend_Short]
>- (
rveq >> imp_res_tac list_rel_lookup_some_r
>> first_x_assum $ qspec_then ‘h::xs’ mp_tac >> gs[]
>> rpt strip_tac >> gs[]
>> gs[IN_DISJOINT,MEM_FLAT,MEM_MAP,FORALL_PROD,
namespacePropsTheory.nsLookup_alist_to_ns_some]
>> rpt strip_tac >> imp_res_tac ALOOKUP_MEM
>> ntac 2 $ first_x_assum $ qspec_then ‘x'’ assume_tac
>> gs[] >> rveq >> gs[])
>> imp_res_tac list_rel_lookup_none_r
>> first_x_assum $ qspec_then ‘h::xs’ mp_tac
>> gs[namespacePropsTheory.nsLookup_nsAppend_none] >> strip_tac
>> Cases_on ‘nsLookup env.v n’ >> gs[]
>- (first_x_assum drule >> gs[])
>> Cases_on ‘n’ >> gs[namespaceTheory.id_to_mods_def]
>- (last_x_assum drule >> gs[])
>> rpt strip_tac
>> Cases_on ‘p1’ >> gs[nsLookupMod_alist_to_ns])
>- (
rveq >> gs[ml_progTheory.nsLookup_nsAppend_Short]
>> TOP_CASE_TAC >> gs[namespacePropsTheory.nsLookup_alist_to_ns_some]
>> imp_res_tac list_rel_lookup_some_r
>> first_x_assum $ qspec_then ‘h :: xs’ mp_tac
>> gs[] >> rpt strip_tac
>> gs[namespacePropsTheory.nsLookup_alist_to_ns_some]
>> imp_res_tac ALOOKUP_MEM
>> gs[IN_DISJOINT,MEM_FLAT,MEM_MAP,FORALL_PROD] >> rveq
>- gs[]
>- gs[]
>- metis_tac[]
>> metis_tac[])
>> gs[namespacePropsTheory.nsLookup_nsAppend_some,
namespacePropsTheory.nsLookup_alist_to_ns_some]
>- (
imp_res_tac list_rel_lookup_some_l
>> first_x_assum $ qspec_then ‘y :: ys'’ mp_tac
>> gs[] >> rpt strip_tac
>> gs[namespacePropsTheory.nsLookup_alist_to_ns_some]
>> asm_exists_tac >> gs[])
>> imp_res_tac list_rel_lookup_none_l
>> first_x_assum $ qspec_then ‘y :: ys'’ mp_tac
>> gs[] >> rpt strip_tac
>> res_tac
>> asm_exists_tac >> gs[]
>> DISJ2_TAC >> rpt strip_tac
>> Cases_on ‘p1’ >> gs[nsLookupMod_alist_to_ns]
QED
Theorem env_rel_strict_update_alist:
∀ xs ys al.
DISJOINT (set (MAP FST xs)) (set (MAP SND al)) ∧
LIST_REL (λ (s1, x1) (s2, x2). s1 = s2 ∧ v_rel x1 x2) xs ys ⇒
env_rel_strict <| v := (alist_to_ns xs):(string,string,v) namespace ; c := nsEmpty |>
<| v := (alist_to_ns ys):(string,string,v) namespace ; c := nsEmpty |> al
Proof
Induct_on ‘xs’ >> simp[Once env_rel_strict_cases]
>> rpt strip_tac
>- (Cases_on ‘p1’ >> gs[])
>- (
Cases_on ‘h’ >> Cases_on ‘y’ >> gs[] >> rveq
>> Cases_on ‘x’ >> gs[nsLookup_nsBind_compute]
>- (
IF_CASES_TAC >> gs[]
>> res_tac >> pop_assum $ mp_tac >> rewrite_tac [Once env_rel_strict_cases]
>> rpt strip_tac >> gs[])
>> res_tac >> pop_assum $ mp_tac >> rewrite_tac [Once env_rel_strict_cases]
>> rpt strip_tac >> gs[])
>- (
Cases_on ‘h’ >> Cases_on ‘y’ >> gs[] >> rveq
>> gs[nsLookup_nsBind_compute]
>> Cases_on ‘q = m’ >> gs[]
>> res_tac
>> pop_assum $ mp_tac >> rewrite_tac [Once env_rel_strict_cases]
>> rpt strip_tac >> gs[])
>- (
Cases_on ‘h’ >> Cases_on ‘y’ >> gs[] >> rveq
>> Cases_on ‘x’ >> gs[nsLookup_nsBind_compute]
>- (
IF_CASES_TAC >> gs[]
>> res_tac >> pop_assum $ mp_tac >> rewrite_tac [Once env_rel_strict_cases]
>> rpt strip_tac >> gs[])
>> res_tac >> pop_assum $ mp_tac >> rewrite_tac [Once env_rel_strict_cases]
>> rpt strip_tac >> gs[])
>- (
Cases_on ‘h’ >> Cases_on ‘y’ >> gs[] >> rveq
>> gs[nsLookup_nsBind_compute]
>> Cases_on ‘q = m’ >> gs[]
>> res_tac
>> pop_assum $ mp_tac >> rewrite_tac [Once env_rel_strict_cases]
>> rpt strip_tac >> gs[])
>- (
Cases_on ‘h’ >> Cases_on ‘y’ >> gs[] >> rveq
>> Cases_on ‘x’ >> gs[nsLookup_nsBind_compute]
>- (
IF_CASES_TAC >> gs[]
>> res_tac >> pop_assum $ mp_tac >> rewrite_tac [Once env_rel_strict_cases]
>> rpt strip_tac >> gs[])
>> res_tac >> pop_assum $ mp_tac >> rewrite_tac [Once env_rel_strict_cases]
>> rpt strip_tac >> gs[])
>> Cases_on ‘p1’ >> gs[]
QED
Theorem env_rel_build_rec_env:
env_rel env env' al ∧
DISJOINT (set (FLAT (MAP (λ(n,m,e). n::m::gather_used_identifiers_exp e) l)))
(set (MAP SND al)) ⇒
env_rel (env with v := build_rec_env l env env.v)
(env' with
v :=
build_rec_env (MAP (I ## I ## replace_constants_exp al) l)
env' env'.v) al
Proof
fs [build_rec_env_def]
\\ qabbrev_tac ‘ff = Recclosure env l’
\\ qabbrev_tac ‘gg = Recclosure env'
(MAP (I ## I ## replace_constants_exp al) l)’
\\ disch_then (fn th => assume_tac th \\ mp_tac th)
\\ qid_spec_tac ‘l’ \\ Induct \\ fs []
\\ fs [FORALL_PROD] \\ rw []
\\ irule env_rel_update_lemma \\ gvs []
\\ unabbrev_all_tac \\ gvs []
\\ first_assum $ irule_at (Pos last) \\ fs []
QED
Theorem find_recfun_lemma:
∀l s n e.
find_recfun s l = SOME (n,e) ⇒
find_recfun s (MAP (I ## I ## f) l) = SOME (n,(f:'a->'a) e)
Proof
Induct \\ once_rewrite_tac [find_recfun_def]
\\ fs [FORALL_PROD] \\ rw []
QED
Definition match_rel_def[simp]:
match_rel No_match No_match = T ∧
match_rel Match_type_error Match_type_error = T ∧
match_rel (Match e1) (Match e2) =
LIST_REL (λ(s1,x1) (s2,x2). s1 = (s2:string) ∧ v_rel x1 x2) e1 e2 ∧
match_rel _ _ = F
End
Theorem LIST_REL_SYM:
∀ xs.
(∀ x. R x x) ⇒
LIST_REL R xs xs
Proof
Induct_on ‘xs’ >> gs[]
>> rpt strip_tac >> res_tac >> gs[]
QED
local
val pmatch_goal =
“(λ envC refs1 p a env1.
∀ refs2 a' r1 r2 env2.
LIST_REL ref_rel refs1 refs2 ∧
v_rel a a' ∧
match_rel (Match env1) (Match env2) ∧
pmatch envC refs1 p a env1 = r1 ∧
pmatch envC refs2 p a' env2 = r2 ⇒
match_rel r1 r2)”
val pmatch_list_goal =
“(λ envC refs1 p as env1.
∀ refs2 as' r1 r2 env2.
LIST_REL ref_rel refs1 refs2 ∧
LIST_REL v_rel as as' ∧
match_rel (Match env1) (Match env2) ∧
pmatch_list envC refs1 p as env1 = r1 ∧
pmatch_list envC refs2 p as' env2 = r2 ⇒
match_rel r1 r2)”
in
Theorem pmatch_single_lemma:
(∀ envC refs p a env.
^pmatch_goal envC refs p a env) ∧
(∀ envC refs p as env.
^pmatch_list_goal envC refs p as env)
Proof
qspecl_then [‘^pmatch_goal’, ‘^pmatch_list_goal’] irule pmatch_ind
>> rw[] >> gs[pmatch_def, match_rel_def]
>- (
Cases_on ‘nsLookup envC n’ >> gs[match_rel_def]
>> TOP_CASE_TAC >> gs[]
>> ntac 2 (COND_CASES_TAC >> gs[match_rel_def])
>> ‘LENGTH vs = LENGTH vs1’
by (irule LIST_REL_LENGTH >> asm_exists_tac >> gs[])
>> COND_CASES_TAC >> gs[match_rel_def]
>> first_x_assum irule >> gs[]
>> qexists_tac ‘st'’ >> gs[])
>- (
‘LENGTH vs = LENGTH vs1’
by (irule LIST_REL_LENGTH >> asm_exists_tac >> gs[])
>> COND_CASES_TAC >> gs[match_rel_def]
>> first_x_assum irule >> gs[]
>> qexists_tac ‘st'’ >> gs[])
>- (ntac 2 (COND_CASES_TAC >> gs[match_rel_def]))
>- (
reverse (TOP_CASE_TAC >> gs[])
>- (
‘match_rel (Match a) (pmatch envC refs2 p y env2)’
by (first_x_assum irule >> gs[])
>> Cases_on ‘pmatch envC refs2 p y env2’ >> gs[match_rel_def])
>- (
‘match_rel (Match_type_error) (pmatch envC refs2 p y env2)’
by (first_x_assum irule >> gs[])
>> Cases_on ‘pmatch envC refs2 p y env2’ >> gs[match_rel_def])
>> ‘match_rel (No_match) (pmatch envC refs2 p y env2)’
by (first_x_assum irule >> gs[])
>> Cases_on ‘pmatch envC refs2 p y env2’ >> gs[match_rel_def]
>> reverse (TOP_CASE_TAC >> gs[])
>- (
‘match_rel (Match a) (pmatch_list envC refs2 ps ys env2)’
by (first_x_assum irule >> gs[])
>> Cases_on ‘pmatch_list envC refs2 ps ys env2’ >> gs[match_rel_def])
>- (
‘match_rel (Match_type_error) (pmatch_list envC refs2 ps ys env2)’
by (first_x_assum irule >> gs[])
>> Cases_on ‘pmatch_list envC refs2 ps ys env2’ >> gs[match_rel_def])
>> ‘match_rel (No_match) (pmatch_list envC refs2 ps ys env2)’
by (first_x_assum irule >> gs[])
>> Cases_on ‘pmatch_list envC refs2 ps ys env2’ >> gs[match_rel_def])
>> ‘LENGTH s = LENGTH refs2’
by (irule LIST_REL_LENGTH >> asm_exists_tac >> gs[])
>> TOP_CASE_TAC >> gs[]
>- (
‘store_lookup lnum refs2 = NONE’