-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathbvi_tailrecProofScript.sml
2494 lines (2396 loc) · 88.3 KB
/
bvi_tailrecProofScript.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
(*
Correctness proof for bvi_tailrec
*)
open preamble bviSemTheory bviPropsTheory bvi_tailrecTheory
(* TODO
- It should be possible to prove that we can replace the simplified
compile_exp by the old compile_exp without touching the evaluate-
theorem. Benefits:
* Less code duplication
* Can inline auxiliary calls more easily
*)
val _ = temp_delsimps ["NORMEQ_CONV"]
val _ = temp_delsimps ["lift_disj_eq", "lift_imp_disj"]
val _ = new_theory "bvi_tailrecProof";
val _ = set_grammar_ancestry ["bvi_tailrec","bviProps","bviSem"];
val find_code_def = bvlSemTheory.find_code_def;
val s = mk_var("s",
type_of ``bviSem$evaluate`` |> strip_fun |> snd |> dest_prod |> snd
|> type_subst [alpha|->``:num#'c``,beta|->``:'ffi``]);
Theorem get_bin_args_SOME[simp]:
∀exp q. get_bin_args exp = SOME q
⇔
∃e1 e2 op. q = (e1, e2) ∧ exp = Op op [e1; e2]
Proof
Cases \\ rw [get_bin_args_def]
\\ rw[bvlPropsTheory.case_eq_thms]
\\ rw[EQ_IMP_THM]
QED
Theorem opbinargs_SOME[simp]:
!exp opr. opbinargs opr exp = SOME q
<=>
opr <> Noop /\ ?x y. q = (x, y) /\ exp = Op (to_op opr) [x;y]
Proof
Cases \\ Cases \\ fs [opbinargs_def, to_op_def, op_eq_def]
\\ rw [EQ_IMP_THM]
QED
Theorem decide_ty_simp1[simp]:
(decide_ty ty1 ty2 = Int <=> ty1 = Int /\ ty2 = Int) /\
(decide_ty ty1 ty2 = List <=> ty1 = List /\ ty2 = List)
Proof
Cases_on `ty1` \\ Cases_on `ty2` \\ fs [decide_ty_def]
QED
Theorem list_to_v_simp[simp]:
!xs. v_to_list (list_to_v xs) = SOME xs
Proof
Induct \\ fs [bvlSemTheory.v_to_list_def, bvlSemTheory.list_to_v_def]
QED
Theorem to_op_11[simp]:
to_op op1 = to_op op2 <=> op1 = op2
Proof
Cases_on `op1` \\ Cases_on `op2` \\ rw [to_op_def]
QED
Theorem to_op_eq_simp[simp]:
(to_op x = Add <=> (x = Plus)) /\
(to_op x = Mult <=> (x = Times)) /\
(to_op x = Mod <=> (x = Noop)) /\
(to_op x = ListAppend <=> (x = Append)) /\
(Add = to_op x <=> (x = Plus)) /\
(Mult = to_op x <=> (x = Times)) /\
(ListAppend = to_op x <=> (x = Append)) /\
(Mod = to_op x <=> (x = Noop))
Proof
Cases_on`x` \\ rw[to_op_def]
QED
Theorem op_eq_simp[simp]:
(op_eq Plus x <=> (?xs. x = Op Add xs)) /\
(op_eq Times x <=> (?xs. x = Op Mult xs)) /\
(op_eq Append x <=> (?xs. x = Op ListAppend xs))
Proof
Cases_on`x` \\ rw[op_eq_def]
QED
Theorem scan_expr_EVERY_SING[simp]:
EVERY P (scan_expr ts loc [x]) ⇔ P (HD (scan_expr ts loc [x]))
Proof
`LENGTH (scan_expr ts loc [x]) = 1` by fs []
\\ Cases_on `scan_expr ts loc [x]` \\ fs []
QED
Theorem try_update_LENGTH[simp]:
LENGTH (try_update ty idx ts) = LENGTH ts
Proof
Cases_on `idx` \\ rw [try_update_def]
QED
Theorem update_context_LENGTH[simp]:
LENGTH (update_context ty ts x y) = LENGTH ts
Proof
rw [update_context_def, try_update_LENGTH]
QED
Theorem decide_ty_simp[simp]:
decide_ty ty1 ty2 = ty3 /\ ty3 <> Any <=>
ty1 = ty3 /\ ty2 = ty3 /\ ty3 <> Any
Proof
Cases_on `ty1` \\ Cases_on `ty2`
\\ fs [decide_ty_def] \\ rw [EQ_IMP_THM]
\\ Cases_on `ty3` \\ fs []
QED
Definition ty_rel_def:
ty_rel = LIST_REL
(\v t. (t = Int ==> ?k. v = Number k) /\
(t = List ==> ?ys. v_to_list v = SOME ys))
End
val v_ty_thms = { nchotomy = v_ty_nchotomy, case_def = v_ty_case_def };
val v_ty_cases = prove_case_eq_thm v_ty_thms
val assoc_op_ty_thms = { nchotomy = assoc_op_nchotomy, case_def = assoc_op_case_def }
val assoc_op_cases = prove_case_eq_thm assoc_op_ty_thms
val case_eq_thms = CONJ v_ty_cases (CONJ assoc_op_cases bviPropsTheory.case_eq_thms)
Theorem list_to_v_imp:
!x xs. v_to_list x = SOME xs ==> list_to_v xs = x
Proof
recInduct bvlSemTheory.v_to_list_ind
\\ rw [bvlSemTheory.v_to_list_def]
\\ fs [case_eq_thms] \\ rw []
\\ fs [bvlSemTheory.list_to_v_def]
QED
val s1 = mk_var("s",
type_of ``bviSem$evaluate`` |> strip_fun |> snd |> dest_prod |> snd
|> type_subst [alpha|->``:'c``,beta|->``:'ffi``]);
Theorem term_ok_int_SING:
!ts exp env ^s1 r t.
term_ok_int ts exp /\
ty_rel env ts /\
evaluate ([exp], env, s) = (r, t) ==>
s = t /\
?v. r = Rval [v] /\
(!^s1. evaluate ([exp], env, s) = (r, s)) /\
?k. v = Number k
Proof
recInduct term_ok_int_ind \\ rw [] \\ Cases_on `expr`
\\ qhdtm_x_assum `term_ok_int` mp_tac
\\ once_rewrite_tac [term_ok_int_def]
\\ fs [case_elim_thms, case_eq_thms, pair_case_eq, bool_case_eq] \\ rw []
\\ TRY
(fs [ty_rel_def, LIST_REL_EL_EQN, evaluate_def, pair_case_eq, bool_case_eq]
\\ NO_TAC)
\\ TRY
(rename1 `is_const op` \\ Cases_on `op` \\ fs []
\\ fs [evaluate_def, pair_case_eq, bool_case_eq, do_app_def,
do_app_aux_def, bvlSemTheory.do_app_def, small_int_def,
backend_commonTheory.small_enough_int_def]
\\ rw [] \\ fs [])
\\ rename1 `is_arith op` \\ Cases_on `op` \\ fs [is_arith_def]
\\ fs [evaluate_def, pair_case_eq, bool_case_eq, do_app_def,
do_app_aux_def, bvlSemTheory.do_app_def, small_int_def,
backend_commonTheory.small_enough_int_def,
case_elim_thms, case_eq_thms]
\\ rw [] \\ fs []
\\ imp_res_tac evaluate_SING_IMP \\ fs [] \\ rw []
\\ res_tac \\ rw [] \\ fs []
\\ fs [bvl_to_bvi_id]
QED
Theorem term_ok_any_SING:
(!ts list exp env ^s1 r t.
term_ok_any ts list exp /\
ty_rel env ts /\
evaluate ([exp], env, s) = (r, t) ==>
s = t /\
?v. r = Rval [v] /\
(!^s1. evaluate ([exp], env, s) = (r, s)) /\
(list ==> ?ys. v_to_list v = SOME ys))
Proof
recInduct term_ok_any_ind \\ rw []
\\ qhdtm_x_assum `term_ok_any` mp_tac
\\ once_rewrite_tac [term_ok_any_def] \\ fs []
\\ TRY PURE_TOP_CASE_TAC \\ fs [] \\ rw [] \\ fs []
\\ TRY
(rename1 `Var n` \\ rw []
\\ rfs [ty_rel_def, evaluate_def, LIST_REL_EL_EQN, case_elim_thms,
case_elim_thms, pair_case_eq, bool_case_eq] \\ rw [] \\ fs []
\\ NO_TAC)
\\ TRY
(rename1 `is_const op` \\ Cases_on `op`
\\ fs [evaluate_def, do_app_def, do_app_aux_def, bvlSemTheory.do_app_def,
case_eq_thms, case_elim_thms, pair_case_eq]
\\ fs [backend_commonTheory.small_enough_int_def, small_int_def])
\\ fs [is_op_thms]
\\ TRY
(rename1 `Op (Cons 0) []`
\\ fs [evaluate_def, do_app_def, do_app_aux_def, bvlSemTheory.do_app_def,
case_eq_thms, case_elim_thms, pair_case_eq]
\\ fs [bvl_to_bvi_id]
\\ rw [] \\ fs [bvlSemTheory.v_to_list_def]
\\ EVAL_TAC)
\\ TRY
(rename1 `is_rel op` \\ Cases_on `op` \\ fs [is_rel_def]
\\ fs [evaluate_def, pair_case_eq, case_elim_thms, case_eq_thms] \\ rw []
\\ imp_res_tac evaluate_SING_IMP \\ rw [] \\ fs []
\\ imp_res_tac term_ok_int_SING \\ fs []
\\ rfs [do_app_def, do_app_aux_def, bvlSemTheory.do_app_def] \\ rw []
\\ fs [bvl_to_bvi_id])
\\ TRY
(rename1 `is_arith op` \\ Cases_on `op` \\ fs [is_arith_def]
\\ fs [evaluate_def, pair_case_eq, case_elim_thms, case_eq_thms] \\ rw []
\\ imp_res_tac term_ok_int_SING \\ fs [] \\ rw []
\\ fs [do_app_def, do_app_aux_def, bvlSemTheory.do_app_def] \\ rw []
\\ fs [bvl_to_bvi_id])
\\ fs [evaluate_def, pair_case_eq, case_eq_thms, case_elim_thms] \\ rw []
\\ imp_res_tac evaluate_IMP_LENGTH \\ fs []
\\ fs [LENGTH_EQ_NUM_compute] \\ rw []
\\ fs [LENGTH_EQ_NUM_compute] \\ rw [] \\ fs []
\\ res_tac \\ fs [] \\ rw []
\\ fs [do_app_def, do_app_aux_def, bvlSemTheory.do_app_def] \\ rw []
\\ fs [bvl_to_bvi_id]
\\ fs [bvlSemTheory.v_to_list_def] \\ EVAL_TAC
QED
Theorem term_ok_SING:
!ts ty exp env ^s1 r t.
term_ok ts ty exp /\
ty_rel env ts /\
evaluate ([exp], env, s) = (r, t) ==>
s = t /\
?v. r = Rval [v] /\
(!^s1. evaluate ([exp], env, s) = (r, s)) /\
case ty of
Int => ?k. v = Number k
| List => ?ys. v_to_list v = SOME ys
| _ => T
Proof
rw [term_ok_def, case_elim_thms, case_eq_thms] \\ every_case_tac \\ fs []
\\ metis_tac [term_ok_int_SING, term_ok_any_SING]
QED
Definition op_id_val_def:
op_id_val Plus = Number 0 /\
op_id_val Times = Number 1 /\
op_id_val Append = Block nil_tag [] /\
op_id_val Noop = Number 6333
End
Theorem scan_expr_not_Noop:
∀exp ts loc tt ty r ok op.
scan_expr ts loc [exp] = [(tt, ty, r, SOME op)] ⇒
op ≠ Noop
Proof
Induct
\\ rw [scan_expr_def]
\\ rpt (pairarg_tac \\ fs []) \\ rw []
\\ fs[bvlPropsTheory.case_eq_thms]
\\ fs [from_op_def] \\ rveq
\\ rfs [case_eq_thms, bool_case_eq] \\ fs []
\\ metis_tac []
QED
Definition env_rel_def:
env_rel ty opt acc env1 env2 <=>
isPREFIX env1 env2 /\
(opt ⇒
LENGTH env1 = acc /\
LENGTH env2 > acc /\
case ty of
Int => ?k. EL acc env2 = Number k
| List => ?ys. v_to_list (EL acc env2) = SOME ys
| Any => F)
End
Overload in_ns_2[local] = ``λn. n MOD bvl_to_bvi_namespaces = 2``
Definition code_rel_def:
code_rel c1 c2 ⇔
∀loc arity exp op.
lookup loc c1 = SOME (arity, exp) ⇒
(check_exp loc arity exp = NONE ⇒
lookup loc c2 = SOME (arity, exp)) ∧
(check_exp loc arity exp = SOME op ⇒
∃n.
∀exp_aux exp_opt.
compile_exp loc n arity exp = SOME (exp_aux, exp_opt) ⇒
lookup loc c2 = SOME (arity, exp_aux) ∧
lookup n c2 = SOME (arity + 1, exp_opt))
End
Triviality code_rel_find_code_SOME:
∀c1 c2 (args: v list) a exp.
code_rel c1 c2 ∧
find_code (SOME n) args c1 = SOME (a, exp) ⇒
find_code (SOME n) args c2 ≠ NONE
Proof
rw [find_code_def, code_rel_def]
\\ pop_assum mp_tac
\\ rpt (PURE_TOP_CASE_TAC \\ fs [])
\\ first_x_assum drule
\\ fs [compile_exp_def]
\\ CASE_TAC \\ fs [] \\ rw []
\\ pairarg_tac \\ fs []
QED
Triviality code_rel_find_code_NONE:
∀c1 c2 (args: v list) a exp.
code_rel c1 c2 ∧
find_code NONE args c1 = SOME (a, exp) ⇒
find_code NONE args c2 ≠ NONE
Proof
rw [find_code_def, code_rel_def]
\\ pop_assum mp_tac
\\ rpt (PURE_TOP_CASE_TAC \\ fs []) \\ rw []
>-
(first_x_assum drule
\\ fs [compile_exp_def]
\\ CASE_TAC \\ fs [] \\ rw []
\\ pairarg_tac \\ fs [])
\\ first_x_assum drule
\\ fs [compile_exp_def]
\\ CASE_TAC \\ fs [] \\ rw []
\\ pairarg_tac \\ fs []
QED
Theorem code_rel_domain:
∀c1 c2.
code_rel c1 c2 ⇒ domain c1 ⊆ domain c2
Proof
simp [code_rel_def, SUBSET_DEF]
\\ CCONTR_TAC \\ fs []
\\ Cases_on `lookup x c1`
>- fs [lookup_NONE_domain]
\\ fs [GSYM lookup_NONE_domain]
\\ rename1 `SOME z`
\\ PairCases_on `z`
\\ first_x_assum drule
\\ fs [compile_exp_def]
\\ CASE_TAC \\ fs [] \\ rw []
\\ pairarg_tac \\ fs []
QED
Theorem evaluate_let_wrap:
∀x op vs ^s1 r t.
op ≠ Noop ⇒
evaluate ([let_wrap (LENGTH vs) (id_from_op op) x], vs, s) =
evaluate ([x], vs ++ [op_id_val op] ++ vs, s)
Proof
rw []
\\ `LENGTH vs + 0 ≤ LENGTH vs` by fs []
\\ drule (GEN_ALL (ISPEC s1 (Q.GEN `st` (SPEC_ALL evaluate_genlist_vars))))
\\ disch_then (qspec_then `s` mp_tac)
\\ simp [let_wrap_def, evaluate_def]
\\ once_rewrite_tac [evaluate_APPEND]
\\ simp [pair_case_eq, case_eq_thms, case_elim_thms, PULL_EXISTS, bool_case_eq]
\\ Cases_on `op` \\ EVAL_TAC \\ rw []
\\ AP_TERM_TAC
\\ fs [state_component_equality]
QED
Theorem evaluate_complete_ind:
∀P.
(∀xs s.
(∀ys t.
exp2_size ys < exp2_size xs ∧ t.clock ≤ s.clock ∨ t.clock < s.clock ⇒
P ys t) ⇒
P xs s) ⇒
∀(xs: bvi$exp list) ^s. P xs s
Proof
rpt strip_tac
\\ `∃sz. exp2_size xs = sz` by fs []
\\ `∃ck0. s.clock = ck0` by fs []
\\ ntac 2 (pop_assum mp_tac)
\\ qspec_tac (`xs`,`xs`)
\\ qspec_tac (`s`,`s`)
\\ qspec_tac (`sz`,`sz`)
\\ completeInduct_on `ck0`
\\ strip_tac
\\ completeInduct_on `sz`
\\ fs [PULL_FORALL, AND_IMP_INTRO, GSYM CONJ_ASSOC]
\\ rpt strip_tac \\ rveq
\\ last_x_assum match_mp_tac
\\ rpt strip_tac
\\ simp []
\\ fs [LESS_OR_EQ]
QED
Theorem EVERY_LAST1:
!xs y. EVERY P xs /\ LAST1 xs = SOME y ==> P y
Proof
ho_match_mp_tac LAST1_ind \\ rw [LAST1_def] \\ fs []
QED
Theorem scan_expr_LENGTH:
∀ts loc xs ys.
scan_expr ts loc xs = ys ⇒
EVERY (λy. LENGTH (FST y) = LENGTH ts) ys
Proof
ho_match_mp_tac scan_expr_ind
\\ rw [scan_expr_def] \\ fs []
\\ rpt (pairarg_tac \\ fs [])
\\ TRY (PURE_CASE_TAC \\ fs [case_eq_thms, case_elim_thms, pair_case_eq])
\\ rw [try_update_LENGTH]
\\ fs [LAST1_def, case_eq_thms] \\ rw [] \\ fs []
\\ imp_res_tac EVERY_LAST1 \\ fs []
\\ Cases_on `op` \\ fs [arg_ty_def, update_context_def, check_op_def]
QED
Theorem ty_rel_decide_ty:
∀ts tt env.
(ty_rel env ts ∨ ty_rel env tt) ∧ LENGTH ts = LENGTH tt ⇒
ty_rel env (MAP2 decide_ty ts tt)
Proof
Induct \\ rw [] \\ fs []
\\ Cases_on `tt` \\ rfs [ty_rel_def]
\\ EVAL_TAC \\ fs [] \\ rveq
\\ Cases_on `h` \\ fs [] \\ Cases_on `h'` \\ simp [decide_ty_def]
QED
Triviality ty_rel_APPEND:
∀env ts ws vs.
ty_rel env ts ∧ ty_rel vs ws ⇒ ty_rel (vs ++ env) (ws ++ ts)
Proof
rw []
\\ sg `LENGTH ws = LENGTH vs`
>- (fs [ty_rel_def, LIST_REL_EL_EQN])
\\ fs [ty_rel_def, LIST_REL_APPEND_EQ]
QED
Theorem LAST1_thm:
!xs. LAST1 xs = NONE <=> xs = []
Proof
Induct \\ rw [LAST1_def]
\\ Cases_on `xs` \\ fs [LAST1_def]
QED
Theorem try_update_EL:
n < LENGTH ts ==>
EL n (try_update ty idx ts) =
case idx of
NONE => EL n ts
| SOME m =>
if n < m then
EL n ts
else if n > m then
EL n ts
else if EL n ts = Any \/ EL n ts = ty then
ty
else
EL n ts
Proof
Cases_on `idx`
\\ rw [try_update_def]
\\ fs [EL_LENGTH_APPEND, EL_APPEND1, EL_TAKE, EL_APPEND2, EL_DROP]
\\ `n = x` by fs [] \\ fs []
QED
Theorem try_update_twice:
n < LENGTH ts ==>
EL n (try_update ty idx1 (try_update ty idx2 ts)) =
case (idx1, idx2) of
(NONE, NONE) => EL n ts
| (NONE, SOME b) =>
if n <> b then EL n ts
else if EL n ts = Any then ty
else EL n ts
| (SOME a, NONE) =>
if n <> a then EL n ts
else if EL n ts = Any then ty
else EL n ts
| (SOME a, SOME b) =>
if n <> a /\ n <> b then EL n ts
else if EL n ts = Any then ty
else EL n ts
Proof
rw [] \\ rpt (PURE_TOP_CASE_TAC \\ fs [])
\\ fs [try_update_EL]
\\ rpt (PURE_CASE_TAC \\ fs [])
QED
Theorem index_of_simp[simp]:
index_of exp = SOME n <=> exp = Var n
Proof
Cases_on `exp` \\ rw [index_of_def]
QED
Theorem scan_expr_ty_rel:
∀ts loc xs env ys s vs t.
ty_rel env ts ∧
scan_expr ts loc xs = ys ∧
evaluate (xs, env, s) = (Rval vs, t) ⇒
EVERY (ty_rel env o FST) ys ∧
ty_rel vs (MAP (FST o SND) ys)
Proof
ho_match_mp_tac scan_expr_ind
\\ fs [scan_expr_def]
\\ rpt conj_tac
\\ rpt gen_tac
\\ simp [evaluate_def]
\\ TRY (fs [ty_rel_def] \\ NO_TAC)
>- (* Cons *)
(fs [case_eq_thms, pair_case_eq, case_elim_thms, PULL_EXISTS] \\ rw []
\\ rpt (pairarg_tac \\ fs [])
\\ fs [ty_rel_def]
\\ res_tac \\ fs [] \\ rw [])
>- (* Var *)
(rw []
\\ fs [ty_rel_def, LIST_REL_EL_EQN]
\\ rw []
\\ metis_tac [])
\\ strip_tac
\\ rpt gen_tac
\\ rpt (pairarg_tac \\ fs []) \\ rveq
\\ TRY (* All but Let, Op, If *)
(fs [case_eq_thms, pair_case_eq, case_elim_thms, bool_case_eq, PULL_EXISTS]
\\ rw []
\\ res_tac \\ fs [] \\ rw []
\\ TRY (metis_tac [])
\\ imp_res_tac evaluate_SING_IMP \\ fs []
\\ imp_res_tac scan_expr_LENGTH \\ fs []
\\ TRY (fs [ty_rel_def] \\ NO_TAC)
\\ Cases_on `ty1` \\ fs []
\\ TRY (metis_tac [ty_rel_decide_ty])
\\ fs [decide_ty_def, ty_rel_def]
\\ metis_tac [])
>- (* If *)
(fs [pair_case_eq, case_eq_thms, case_elim_thms, PULL_EXISTS] \\ rw []
\\ imp_res_tac evaluate_IMP_LENGTH \\ fs [] \\ rveq
\\ fs [LENGTH_EQ_NUM_compute] \\ rveq
\\ fs [LENGTH_EQ_NUM_compute] \\ rveq
\\ qpat_x_assum `(_,_) = _` (assume_tac o GSYM) \\ fs []
\\ TRY
(imp_res_tac scan_expr_LENGTH \\ fs []
\\ metis_tac [ty_rel_decide_ty])
\\ rpt (PURE_CASE_TAC \\ fs [])
\\ res_tac
\\ fs [ty_rel_def, decide_ty_def])
>- (* Let *)
(fs [case_eq_thms, pair_case_eq, case_elim_thms, bool_case_eq]
\\ fs [PULL_EXISTS]
\\ rpt (gen_tac ORELSE DISCH_TAC) \\ fs []
\\ reverse conj_tac
\\ qpat_x_assum `scan_expr _ _ [x] = _` mp_tac
\\ CASE_TAC \\ fs [LAST1_thm]
\\ strip_tac
\\ res_tac \\ rfs []
\\ TRY (fs [ty_rel_def, LIST_REL_LENGTH] \\ NO_TAC)
\\ TRY
(pop_assum mp_tac
\\ rw [ty_rel_def] \\ fs [] \\ rfs []
\\ pop_assum mp_tac
\\ rw [ty_rel_def]
\\ res_tac
\\ fs [LIST_REL_EL_EQN]
\\ imp_res_tac scan_expr_LENGTH \\ fs []
\\ imp_res_tac evaluate_IMP_LENGTH \\ fs [] \\ rveq
\\ fs [ty_rel_def, LIST_REL_EL_EQN]
\\ NO_TAC)
\\ rw []
\\ imp_res_tac evaluate_IMP_LENGTH \\ fs [] \\ rveq
\\ fs [LENGTH_EQ_NUM_compute] \\ rveq
\\ imp_res_tac EVERY_LAST1 \\ fs []
\\ fs [ty_rel_APPEND]
\\ rpt (qpat_x_assum `ty_rel _ _` mp_tac)
\\ rw [ty_rel_def, LIST_REL_EL_EQN]
\\ rfs [EL_DROP]
\\ `n + LENGTH vs' < LENGTH tu` by fs []
\\ rpt (first_x_assum drule) \\ rw []
\\ rfs [EL_APPEND1, EL_APPEND2, EL_LENGTH_APPEND])
\\ CASE_TAC \\ fs []
>-
(rw [case_eq_thms, case_elim_thms, IS_SOME_EXISTS, PULL_EXISTS, bool_case_eq,
pair_case_eq, from_op_def, arg_ty_def, op_ty_def]
\\ fs [pair_case_eq, case_elim_thms, case_eq_thms] \\ rw [] \\ fs []
\\ fs [op_type_def, arg_ty_def, ty_rel_def, get_bin_args_def, case_elim_thms,
case_eq_thms] \\ rw [] \\ fs [evaluate_def] \\ rw [] \\ fs []
\\ fs [check_op_def, opbinargs_def, try_swap_def, get_bin_args_def]
\\ TRY (Cases_on `op`) \\ fs [do_app_def, do_app_aux_def,
bvlSemTheory.do_app_def, bool_case_eq,
case_elim_thms, case_eq_thms] \\ rw [] \\ fs []
\\ fs [bvlSemTheory.v_to_list_def] \\ EVAL_TAC)
\\ rveq
\\ fs [evaluate_def]
\\ Cases_on `op` \\ fs [from_op_def, arg_ty_def]
\\ fs [pair_case_eq, case_eq_thms, case_elim_thms, bool_case_eq] \\ rw []
\\ imp_res_tac evaluate_SING_IMP \\ fs [] \\ rveq
\\ fs [do_app_def, do_app_aux_def, bvlSemTheory.do_app_def]
\\ fs [pair_case_eq, case_eq_thms, case_elim_thms, bool_case_eq] \\ rw []
\\ fs [check_op_def, term_ok_def, opbinargs_def, get_bin_args_def, op_type_def]
\\ fs [ty_rel_def, LIST_REL_EL_EQN] \\ rw []
\\ fs [bool_case_eq, try_swap_def]
\\ pop_assum mp_tac
\\ fs [update_context_def, try_update_twice]
\\ rpt (PURE_TOP_CASE_TAC \\ fs []) \\ rveq
\\ fs [evaluate_def, bool_case_eq, pair_case_eq, case_eq_thms, case_elim_thms] \\ rveq
\\ fs [] \\ rveq \\ rfs []
\\ metis_tac []
QED
Theorem check_op_thm:
check_op ts opr loc exp = SOME expr ==>
?x y.
is_rec loc x /\
term_ok ts (op_type opr) y /\
expr = Op (to_op opr) [y; x] /\
(exp = Op (to_op opr) [x; y] \/ exp = Op (to_op opr) [y; x])
Proof
rw [check_op_def, opbinargs_def] \\ CCONTR_TAC \\ fs [] \\ rw []
\\ Cases_on `opr` \\ fs [try_swap_def, opbinargs_def, get_bin_args_def,
apply_op_def, op_type_def, term_ok_def, case_eq_thms,
IS_SOME_EXISTS, case_elim_thms]
\\ rw [] \\ fs [to_op_def]
\\ metis_tac []
QED
Theorem rewrite_scan_expr:
!loc next op acc ts exp tt ty p exp2 r opr.
rewrite loc next op acc ts exp = (p,exp2) /\
op <> Noop /\
scan_expr ts loc [exp] = [(tt, ty, r, opr)] ==>
case opr of
SOME op1 => op = op1 ==> p
| NONE => ~p
Proof
recInduct rewrite_ind
\\ rw [rewrite_def, scan_expr_def] \\ fs []
\\ rpt (pairarg_tac \\ fs []) \\ rveq
\\ rpt (PURE_TOP_CASE_TAC \\ fs [])
\\ fs [IS_SOME_EXISTS, case_elim_thms, pair_case_eq, case_eq_thms] \\ rw [] \\ fs []
\\ imp_res_tac check_op_thm \\ fs []
\\ rw [] \\ fs [to_op_def, is_const_def]
\\ TRY (Cases_on `opr` \\ fs [])
\\ fs [opbinargs_def, get_bin_args_def, from_op_def, to_op_def]
QED
Theorem scan_expr_op_type:
!ts loc xs ys.
scan_expr ts loc xs = ys ==>
EVERY (\(tt,ty,r,opr).
case opr of
SOME op => ty <> Any ==> op_type op = ty
| NONE => T) ys
Proof
recInduct scan_expr_ind \\ rw [scan_expr_def] \\ fs []
\\ rpt (pairarg_tac \\ fs []) \\ rw []
\\ fs [case_elim_thms] \\ rw []
\\ fs [] \\ rw []
\\ Cases_on `op` \\ fs [op_type_def, from_op_def]
\\ fs [get_bin_args_def, arg_ty_def, check_op_def, opbinargs_def]
\\ rpt (PURE_TOP_CASE_TAC \\ fs [])
\\ Cases_on `ty1` \\ Cases_on `ty2` \\ rfs [decide_ty_def]
QED
Definition optimized_code_def:
optimized_code loc arity exp n c op =
∃exp_aux exp_opt.
compile_exp loc n arity exp = SOME (exp_aux, exp_opt) ∧
check_exp loc arity exp = SOME op ∧
lookup loc c = SOME (arity, exp_aux) ∧
lookup n c = SOME (arity + 1, exp_opt)
End
Theorem code_rel_subspt:
code_rel c1 x1 ∧ subspt x1 x2 ⇒ code_rel c1 x2
Proof
rw[code_rel_def]
\\ fs[subspt_lookup]
\\ first_x_assum drule
\\ disch_then(qspec_then`op`mp_tac)
\\ rw[] \\ qexists_tac`n` \\ rw[]
QED
Theorem compile_prog_LENGTH:
∀n prog. LENGTH (SND (bvi_tailrec$compile_prog n prog)) ≥ LENGTH prog
Proof
recInduct compile_prog_ind
\\ conj_tac
>- fs [compile_prog_def]
\\ rw []
\\ Cases_on `compile_exp loc next arity exp` \\ fs []
>-
(fs [compile_prog_def]
\\ pairarg_tac \\ fs [])
\\ PairCases_on `x`
\\ fs [compile_prog_def]
\\ pairarg_tac \\ fs []
QED
Definition free_names_def:
free_names n (name: num) ⇔ ∀k. n + bvl_to_bvi_namespaces*k ≠ name
End
Triviality more_free_names:
free_names n name ⇒ free_names (n + bvl_to_bvi_namespaces) name
Proof
fs [free_names_def] \\ rpt strip_tac
\\ first_x_assum (qspec_then `k + 1` mp_tac) \\ strip_tac
\\ rw []
QED
Triviality is_free_name:
free_names n name ⇒ n ≠ name
Proof
fs [free_names_def] \\ strip_tac
\\ first_x_assum (qspec_then `0` mp_tac) \\ strip_tac \\ rw []
QED
Triviality compile_exp_next_addr:
compile_exp loc next args exp = NONE ⇒
compile_exp loc (next + bvl_to_bvi_namespaces) args exp = NONE
Proof
fs [compile_exp_def]
\\ every_case_tac
\\ pairarg_tac \\ fs []
QED
Theorem compile_prog_untouched:
∀next prog prog2 loc exp arity.
free_names next loc ∧
lookup loc (fromAList prog) = SOME (arity, exp) ∧
check_exp loc arity exp = NONE ∧
compile_exp loc next arity exp = NONE ∧
compile_prog next prog = (next1, prog2) ⇒
lookup loc (fromAList prog2) = SOME (arity, exp)
Proof
ho_match_mp_tac compile_prog_ind \\ rw []
\\ fs [fromAList_def, lookup_def]
\\ Cases_on `loc' = loc` \\ rw []
>-
(Cases_on `lookup loc (fromAList xs)`
\\ fs [compile_prog_def]
\\ rpt (pairarg_tac \\ fs [])
\\ rfs [] \\ rw []
\\ simp [fromAList_def])
\\ fs [lookup_insert]
\\ Cases_on `compile_exp loc next arity exp` \\ fs []
>-
(fs [compile_prog_def]
\\ pairarg_tac \\ fs [] \\ rw []
\\ fs [fromAList_def, lookup_insert])
\\ PairCases_on `x`
\\ imp_res_tac more_free_names
\\ imp_res_tac compile_exp_next_addr
\\ fs [compile_prog_def]
\\ pairarg_tac \\ fs [] \\ rw []
\\ fs [fromAList_def, lookup_insert]
\\ first_x_assum drule
\\ disch_then drule
\\ rw [fromAList_def, lookup_insert, is_free_name]
QED
Triviality EVERY_free_names_SUCSUC:
∀xs.
EVERY (free_names n o FST) xs ⇒
EVERY (free_names (n + bvl_to_bvi_namespaces) o FST) xs
Proof
Induct
\\ strip_tac \\ fs []
\\ strip_tac
\\ imp_res_tac more_free_names
QED
Theorem compile_prog_touched:
∀next prog prog2 loc exp arity.
ALL_DISTINCT (MAP FST prog) ∧
EVERY (free_names next o FST) prog ∧
free_names next loc ∧
lookup loc (fromAList prog) = SOME (arity, exp) ∧
check_exp loc arity exp = SOME op ∧
compile_prog next prog = (next1, prog2) ⇒
∃k. ∀exp_aux exp_opt.
compile_exp loc (next + bvl_to_bvi_namespaces * k) arity exp = SOME (exp_aux, exp_opt) ⇒
lookup loc (fromAList prog2) = SOME (arity, exp_aux) ∧
lookup (next + bvl_to_bvi_namespaces * k) (fromAList prog2) = SOME (arity + 1, exp_opt)
Proof
ho_match_mp_tac compile_prog_ind \\ rw []
\\ fs [fromAList_def, lookup_def]
\\ pop_assum mp_tac
\\ simp [compile_prog_def]
\\ rpt (pairarg_tac \\ fs [])
\\ PURE_TOP_CASE_TAC \\ fs []
>-
(rw []
\\ qpat_x_assum `compile_exp _ _ _ _ = _` mp_tac
\\ simp [Once compile_exp_def, check_exp_def]
\\ `LENGTH (scan_expr (REPLICATE arity Any) loc [exp]) = LENGTH [exp]` by fs []
\\ CASE_TAC \\ fs []
\\ PairCases_on `h` \\ fs [] \\ rveq
\\ qpat_x_assum `_ = SOME (_, _)` mp_tac
\\ simp [lookup_insert, fromAList_def]
\\ IF_CASES_TAC \\ strip_tac
\\ rw [] \\ rfs []
\\ rveq \\ fs []
\\ TRY (pairarg_tac \\ fs [])
\\ first_x_assum drule
\\ disch_then drule \\ rw []
\\ fs [lookup_insert, fromAList_def]
\\ qexists_tac `k` \\ rw []
\\ fs [free_names_def,backend_commonTheory.bvl_to_bvi_namespaces_def])
\\ PURE_CASE_TAC \\ rw []
\\ qpat_x_assum `lookup _ _ = SOME (_,_)` mp_tac
\\ fs [lookup_insert, fromAList_def]
\\ IF_CASES_TAC \\ fs [] \\ rw []
\\ imp_res_tac more_free_names
\\ rfs [EVERY_free_names_SUCSUC]
\\ fs [lookup_insert, fromAList_def, free_names_def]
\\ TRY (qexists_tac `0` \\ fs [backend_commonTheory.bvl_to_bvi_namespaces_def] \\ NO_TAC)
\\ first_x_assum (qspec_then `loc'` assume_tac)
\\ first_x_assum drule
\\ disch_then drule \\ rw []
\\ qexists_tac `k + 1` \\ fs []
\\ simp [LEFT_ADD_DISTRIB]
\\ fs[backend_commonTheory.bvl_to_bvi_namespaces_def]
QED
Triviality check_exp_NONE_compile_exp:
check_exp loc arity exp = NONE ⇒ compile_exp loc next arity exp = NONE
Proof
fs [compile_exp_def]
QED
Triviality check_exp_SOME_compile_exp:
check_exp loc arity exp = SOME p ⇒
∃q. compile_exp loc next arity exp = SOME q
Proof
fs [compile_exp_def, check_exp_def]
\\ rw [] \\ rw []
\\ pairarg_tac \\ fs []
QED
Triviality EVERY_free_names_thm:
EVERY (free_names next o FST) prog ∧
lookup loc (fromAList prog) = SOME x ⇒
free_names next loc
Proof
rw [lookup_fromAList, EVERY_MEM]
\\ imp_res_tac ALOOKUP_MEM
\\ first_x_assum (qspec_then `(loc, x)` mp_tac) \\ rw []
QED
Theorem compile_prog_code_rel:
compile_prog next prog = (next1, prog2) ∧
ALL_DISTINCT (MAP FST prog) ∧
EVERY (free_names next o FST) prog ⇒
code_rel (fromAList prog) (fromAList prog2)
Proof
rw [code_rel_def]
\\ imp_res_tac EVERY_free_names_thm
>- metis_tac [check_exp_NONE_compile_exp, compile_prog_untouched]
\\ drule compile_prog_touched
\\ rpt (disch_then drule) \\ rw []
\\ qexists_tac `bvl_to_bvi_namespaces * k + next` \\ fs []
\\ `0 < bvl_to_bvi_namespaces` by EVAL_TAC
\\ simp[ADD_MODULUS]
QED
Theorem compile_prog_next_mono:
∀n xs n1 ys. compile_prog n xs = (n1,ys) ⇒ ∃k. n1 = n + bvl_to_bvi_namespaces * k
Proof
recInduct compile_prog_ind
\\ rw[compile_prog_def]
\\ rpt(pairarg_tac \\ fs[bvlPropsTheory.case_eq_thms])
\\ rveq \\ fs[]
\\ TRY(qexists_tac`0` \\ simp[] \\ NO_TAC)
\\ TRY(qexists_tac`k` \\ simp[] \\ NO_TAC)
\\ TRY(qexists_tac`k+1` \\ simp[] \\ NO_TAC)
QED
Theorem compile_prog_MEM:
compile_prog n xs = (n1,ys) /\ MEM e (MAP FST ys) ==>
MEM e (MAP FST xs) \/ (n <= e /\ e < n1 /\ (∃k. e = n + k * bvl_to_bvi_namespaces))
Proof
qspec_tac (`e`,`e`)
\\ qspec_tac (`n1`,`n1`)
\\ qspec_tac (`ys`,`ys`)
\\ qspec_tac (`n`,`n`)
\\ qspec_tac (`xs`,`xs`)
\\ Induct
>- fs [compile_prog_def]
\\ gen_tac
\\ PairCases_on `h`
\\ rename1 `(name, arity, exp)`
\\ simp [compile_prog_def]
\\ rpt gen_tac
\\ rpt (pairarg_tac \\ fs [])
\\ PURE_CASE_TAC \\ fs []
\\ TRY (PURE_CASE_TAC \\ fs [])
\\ fs [MEM_MAP, PULL_EXISTS, FORALL_PROD]
\\ rpt strip_tac \\ rveq \\ fs []
\\ TRY (metis_tac [])
\\ rveq
\\ imp_res_tac compile_prog_next_mono \\ fs[]
\\ first_x_assum drule
\\ TRY (simp[backend_commonTheory.bvl_to_bvi_namespaces_def] \\
rw[] \\ rpt disj2_tac \\ qexists_tac`0` \\ simp[] \\ NO_TAC)
\\ disch_then drule
\\ strip_tac
>- metis_tac []
\\ fs []
\\ rpt disj2_tac
\\ qexists_tac`k'' + 1` \\ simp[]
QED
Triviality compile_prog_intro:
∀xs n ys n1 name.
¬MEM name (MAP FST xs) ∧
free_names n name ∧
compile_prog n xs = (n1, ys) ⇒
¬MEM name (MAP FST ys) ∧
free_names n1 name
Proof
Induct
>- fs [compile_prog_def]
\\ gen_tac
\\ PairCases_on `h`
\\ rpt gen_tac
\\ simp [compile_prog_def]
\\ rpt (pairarg_tac \\ fs [])
\\ PURE_TOP_CASE_TAC \\ fs []
>-
(rpt strip_tac \\ rveq \\ fs []
\\ metis_tac [])
\\ PURE_CASE_TAC \\ fs []
\\ rpt strip_tac \\ rveq \\ fs []
\\ metis_tac [is_free_name,more_free_names]
QED
Theorem compile_prog_ALL_DISTINCT:
compile_prog n xs = (n1,ys) /\ ALL_DISTINCT (MAP FST xs) /\
EVERY (free_names n o FST) xs ==>
ALL_DISTINCT (MAP FST ys) /\
EVERY (free_names n1 o FST) ys
Proof
qspec_tac (`n1`,`n1`)
\\ qspec_tac (`ys`,`ys`)
\\ qspec_tac (`n`,`n`)
\\ qspec_tac (`xs`,`xs`)
\\ Induct
>- fs [compile_prog_def]
\\ gen_tac
\\ PairCases_on `h`
\\ rename1 `(name, arity, exp)`
\\ simp [compile_prog_def]
\\ rpt gen_tac
\\ rpt (pairarg_tac \\ fs [])
\\ PURE_CASE_TAC \\ fs []
>-
(rpt strip_tac \\ fs [] \\ rveq
\\ qpat_x_assum `_ = (_, ys'')` kall_tac
\\ res_tac
\\ simp [MAP]
\\ metis_tac [more_free_names, compile_prog_intro])
\\ PURE_CASE_TAC \\ fs []
\\ reverse(rpt strip_tac) \\ rveq
\\ fs [is_free_name]
\\ imp_res_tac EVERY_free_names_SUCSUC
\\ res_tac
\\ simp []
>- (
fs[free_names_def]
\\ imp_res_tac compile_prog_next_mono
\\ rveq \\ fs[]
\\ `bvl_to_bvi_namespaces ≠ 0` by EVAL_TAC
\\ CCONTR_TAC \\ fs[] \\ rveq \\ fs[]
\\ first_x_assum(qspec_then`k+k''+1`mp_tac)
\\ simp[] )
\\ reverse conj_tac
>-
(CCONTR_TAC \\ fs []
\\ drule (GEN_ALL compile_prog_MEM)
\\ disch_then drule
\\ simp [MEM_MAP]
\\ fs [EVERY_MEM]
\\ `0 < bvl_to_bvi_namespaces` by EVAL_TAC \\ fs[]
\\ gen_tac
\\ Cases_on `MEM y xs` \\ fs []
\\ res_tac
\\ fs [is_free_name])
\\ CCONTR_TAC \\ fs []
\\ drule (GEN_ALL compile_prog_MEM)
\\ disch_then drule
\\ simp [MEM_MAP]
\\ metis_tac [compile_prog_intro, more_free_names]
QED
Definition namespace_rel_def:
namespace_rel (c1:'a spt) (c2:'a spt) ⇔
(∀n. n ∈ domain c2 ∧ bvl_num_stubs ≤ n ⇒ if in_ns_2 n then n ∉ domain c1 else n ∈ domain c1) ∧
(∀n. n ∈ domain c1 ∧ bvl_num_stubs ≤ n ⇒ ¬(in_ns_2 n)) ∧
(∀n. n ∈ domain c2 ∧ n < bvl_num_stubs ⇒ n ∈ domain c1)
End
Definition input_condition_def:
input_condition next prog ⇔
EVERY (free_names next o FST) prog ∧
ALL_DISTINCT (MAP FST prog) ∧
EVERY ($~ o in_ns_2 o FST) (FILTER ((<=) bvl_num_stubs o FST) prog) ∧