-
Notifications
You must be signed in to change notification settings - Fork 84
/
source_to_source2ProofsScript.sml
3398 lines (3262 loc) · 141 KB
/
source_to_source2ProofsScript.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
(*
Overall correctness proofs for optimisation functions
defined in source_to_source2Script.sml.
To prove a particular run correct, they are combined
using the automation in icing_optimisationsLib.sml with
the local correctness theorems from icing_optimisationProofsScript.sml.
*)
open icing_rewriterTheory icing_rewriterProofsTheory source_to_source2Theory
fpOptTheory fpOptPropsTheory
fpSemPropsTheory semanticPrimitivesTheory evaluateTheory
semanticsTheory semanticsPropsTheory pureExpsTheory floatToRealTheory
floatToRealProofsTheory evaluatePropsTheory namespaceTheory
fpSemPropsTheory mllistTheory optPlannerTheory;
local open ml_progTheory in end;
open icingTacticsLib preamble;
val _ = temp_delsimps ["lift_disj_eq", "lift_imp_disj"];
val _ = new_theory "source_to_source2Proofs";
(**
Helper theorems and definitions
**)
val choice_mono =
(CONJUNCT1 evaluate_fp_opts_inv) |> SPEC_ALL |> UNDISCH |> CONJUNCTS |> el 3 |> DISCH_ALL;
(* TODO: Move *)
Theorem nsMap_nsBind:
nsMap f (nsBind x v env) = nsBind x (f v) (nsMap f env)
Proof
Cases_on ‘env’ \\ gs[namespaceTheory.nsBind_def, nsMap_def]
QED
(* TODO: Move *)
Theorem list_result_rw:
∀ schedule st1 fp.
list_result (
case do_fprw (Rval (FP_WordTree fp)) schedule st1.fp_state.rws
of
NONE => Rval (FP_WordTree fp)
| SOME r_opt => r_opt) =
Rval [FP_WordTree
(case rwAllWordTree schedule st1.fp_state.rws fp of
NONE => fp
| SOME r_opt => r_opt)]
Proof
rpt strip_tac \\ gs[semanticPrimitivesTheory.do_fprw_def, CaseEq"option"]
\\ Cases_on ‘rwAllWordTree schedule st1.fp_state.rws fp’ \\ gs[]
QED
(* TODO: Move *)
Theorem result_cond_rw:
∀ schedule st fp.
(if st.fp_state.canOpt = FPScope Opt then
case do_fprw (Rval (FP_WordTree fp)) schedule st.fp_state.rws
of
NONE => Rval (FP_WordTree fp)
| SOME r_opt => r_opt else Rval (FP_WordTree fp)) =
Rval (FP_WordTree
(if st.fp_state.canOpt = FPScope Opt then
(case rwAllWordTree schedule st.fp_state.rws fp of
NONE => fp
| SOME r_opt => r_opt)
else fp))
Proof
rpt strip_tac \\ gs[semanticPrimitivesTheory.do_fprw_def, CaseEq"option"]
\\ COND_CASES_TAC \\ gs[]
\\ Cases_on ‘rwAllWordTree schedule st.fp_state.rws fp’ \\ gs[]
QED
(* TODO: Move *)
Theorem list_result_cond_rw:
∀ schedule st1 fp.
list_result (
if st1.fp_state.canOpt = FPScope Opt then
(case do_fprw (Rval (FP_WordTree fp)) schedule st1.fp_state.rws
of
NONE => Rval (FP_WordTree fp)
| SOME r_opt => r_opt)
else Rval (FP_WordTree fp)) =
Rval [FP_WordTree
(if st1.fp_state.canOpt = FPScope Opt then
(case rwAllWordTree schedule st1.fp_state.rws fp of
NONE => fp
| SOME r_opt => r_opt)
else fp)]
Proof
rpt strip_tac \\ gs[semanticPrimitivesTheory.do_fprw_def]
\\ COND_CASES_TAC \\ gs[CaseEq"option"]
\\ Cases_on ‘rwAllWordTree schedule st1.fp_state.rws fp’ \\ gs[]
QED
Theorem fp_state_opts_eq[local]:
fps with <| rws := rwsN; opts := fps.opts |> = fps with <| rws := rwsN |>
Proof
Cases_on `fps` \\ fs[fpState_component_equality]
QED
Theorem do_app_fp_inv:
do_app (refs, ffi) (FP_bop op) [v1; v2] = SOME ((refs2, ffi2), r) ==>
? w1 w2.
fp_translate v1 = SOME (FP_WordTree w1) /\ fp_translate v2 = SOME (FP_WordTree w2) /\
r = Rval (FP_WordTree (fp_bop op w1 w2))
Proof
Cases_on `op` \\ every_case_tac \\ fs[do_app_def] \\ rpt (TOP_CASE_TAC \\ fs[])
\\ rpt strip_tac \\ rveq \\ fs[]
QED
Theorem nth_len:
nth (l ++ [x]) (LENGTH l + 1) = SOME x
Proof
Induct_on `l` \\ fs[fpOptTheory.nth_def, ADD1]
QED
Definition freeVars_fp_bound_def:
freeVars_fp_bound e env =
∀ x. x IN FV e ⇒
∃ fp. nsLookup env.v x = SOME (FP_WordTree fp)
End
(* Correctness definition for rewriteFPexp
We need to handle the case where the expression returns an error, but we cannot
preserve the exact error, as reordering may change which error is returned *)
Definition is_rewriteFPexp_correct_def:
is_rewriteFPexp_correct rws (st1:'a semanticPrimitives$state) st2 env e r =
(evaluate st1 env [rewriteFPexp rws e] = (st2, Rval r) /\
freeVars_fp_bound e env ∧
st1.fp_state.canOpt = FPScope Opt /\
st1.fp_state.real_sem = F ==>
? fpOpt choices fpOptR choicesR.
evaluate
(st1 with fp_state := st1.fp_state with
<| rws := st1.fp_state.rws ++ rws; opts := fpOpt; choices := choices |>) env [e] =
(st2 with fp_state := st2.fp_state with
<| rws := st2.fp_state.rws ++ rws; opts := fpOptR; choices := choicesR |>, Rval r))
End
Definition freeVars_list_fp_bound_def:
freeVars_list_fp_bound exps env =
∀ x. x IN FV_list exps ⇒
∃ fp. nsLookup env.v x = SOME (FP_WordTree fp)
End
Definition is_rewriteFPexp_list_correct_def:
is_rewriteFPexp_list_correct rws (st1:'a semanticPrimitives$state) st2 env exps r =
(evaluate st1 env (MAP (rewriteFPexp rws) exps) = (st2, Rval r) /\
freeVars_list_fp_bound exps env ∧
st1.fp_state.canOpt = FPScope Opt /\
st1.fp_state.real_sem = F ==>
? fpOpt choices fpOptR choicesR.
evaluate
(st1 with fp_state := st1.fp_state with
<| rws := st1.fp_state.rws ++ rws; opts := fpOpt; choices := choices |>) env exps =
(st2 with fp_state := st2.fp_state with
<| rws := st2.fp_state.rws ++ rws; opts := fpOptR; choices := choicesR |>, Rval r))
End
Theorem empty_rw_correct:
! (st1 st2:'a semanticPrimitives$state) env e r.
is_rewriteFPexp_correct [] st1 st2 env e r
Proof
rpt strip_tac \\ fs[is_rewriteFPexp_correct_def, rewriteFPexp_def]
\\ rpt strip_tac \\ qexists_tac `st1.fp_state.opts`
\\ qexists_tac ‘st1.fp_state.choices’
\\ `st1 = st1 with fp_state := st1.fp_state with
<| rws := st1.fp_state.rws; opts := st1.fp_state.opts; choices := st1.fp_state.choices |>`
by (fs[semState_comp_eq, fpState_component_equality])
\\ pop_assum (fn thm => fs[GSYM thm])
\\ fs[fpState_component_equality, semState_comp_eq]
QED
Theorem choices_simp:
st.fp_state with choices := st.fp_state.choices = st.fp_state
Proof
fs[fpState_component_equality]
QED
Theorem rewriteExp_compositional:
! rws opt.
(! (st1 st2:'a semanticPrimitives$state) env e r.
is_rewriteFPexp_correct rws st1 st2 env e r) /\
(! (st1 st2:'a semanticPrimitives$state) env e r.
is_rewriteFPexp_correct [opt] st1 st2 env e r) ==>
! (st1 st2:'a semanticPrimitives$state) env e r.
is_rewriteFPexp_correct ([opt] ++ rws) st1 st2 env e r
Proof
rw[is_rewriteFPexp_correct_def]
\\ qpat_x_assum `_ = (_, _)` mp_tac
\\ PairCases_on `opt` \\ simp[rewriteFPexp_def]
\\ reverse TOP_CASE_TAC
>- (
rpt strip_tac
\\ first_x_assum (mp_then Any assume_tac (prep (CONJUNCT1 evaluate_fp_rws_append)))
\\ first_x_assum (qspecl_then [`[(opt0, opt1)] ++ rws`, `g`] strip_assume_tac)
\\ qexists_tac ‘fpOpt’ \\ qexists_tac ‘st1.fp_state.choices’
\\ fs[semState_comp_eq, fpState_component_equality, choices_simp])
\\ TOP_CASE_TAC \\ fs[]
>- (
rpt strip_tac
\\ first_x_assum (mp_then Any assume_tac (prep (CONJUNCT1 evaluate_fp_rws_append)))
\\ first_x_assum (qspecl_then [`[(opt0, opt1)]`, `\x. []`] assume_tac) \\ fs[]
\\ first_x_assum (fn thm => (first_x_assum (fn ithm => mp_then Any impl_subgoal_tac ithm thm)))
\\ fs[fpState_component_equality]
\\ qexists_tac ‘fpOpt'’ \\ qexists_tac ‘choices’
\\ fs[semState_comp_eq, fpState_component_equality])
\\ TOP_CASE_TAC \\ fs[]
>- (
rpt strip_tac
\\ first_x_assum (mp_then Any assume_tac (prep (CONJUNCT1 evaluate_fp_rws_append)))
\\ first_x_assum (qspecl_then [`[(opt0, opt1)]`, `\x. []`] assume_tac) \\ fs[]
\\ first_x_assum (fn thm => (first_x_assum (fn ithm => mp_then Any impl_subgoal_tac ithm thm)))
\\ fs[fpState_component_equality] \\ asm_exists_tac \\ fs[]
\\ TOP_CASE_TAC \\ fs[]
\\ qexists_tac ‘fpOptR’ \\ fs[semState_comp_eq, fpState_component_equality])
\\ rpt strip_tac \\ fs[]
\\ first_x_assum drule \\ fs[state_component_equality, fpState_component_equality]
\\ fs[PULL_EXISTS] \\ rpt gen_tac
\\ impl_tac
>- (fs[freeVars_fp_bound_def]
\\ qspecl_then [‘opt1’, ‘opt0’, ‘e’, ‘x'’, ‘x’, ‘[]’,
‘λ x. ∃ fp. nsLookup env.v x = SOME (FP_WordTree fp)’]
mp_tac match_preserves_FV
\\ impl_tac \\ fs[substLookup_def])
\\ strip_tac \\ pop_assum mp_tac
\\ qmatch_goalsub_abbrev_tac `evaluate st1N env [_] = (st2N, Rval r2)`
\\ rpt strip_tac
\\ first_x_assum (qspecl_then [`st1N`, `st2N`, `env`, `e`, `r2`] assume_tac)
\\ fs[rewriteFPexp_def] \\ rfs[]
\\ unabbrev_all_tac \\ fs[state_component_equality, fpState_component_equality]
\\ first_x_assum impl_subgoal_tac \\ fs[]
\\ first_x_assum (mp_then Any assume_tac (prep (CONJUNCT1 evaluate_fp_rws_up)))
\\ first_x_assum (qspec_then `st1.fp_state.rws ++ [(opt0, opt1)] ++ rws` impl_subgoal_tac)
>- (fs[SUBSET_DEF] \\ rpt strip_tac \\ fs[])
\\ fs[] \\ qexists_tac `fpOpt''` \\ qexists_tac ‘choices'’ \\ fs[]
\\ fs[semState_comp_eq, fpState_component_equality]
\\ imp_res_tac evaluate_fp_opts_inv \\ fs[]
QED
Theorem lift_rewriteFPexp_correct_list_strong:
! rws (st1 st2:'a semanticPrimitives$state) env exps r.
(! (st1 st2: 'a semanticPrimitives$state) env e r.
is_rewriteFPexp_correct rws st1 st2 env e r) ==>
is_rewriteFPexp_list_correct rws st1 st2 env exps r
Proof
Induct_on `exps`
\\ fs[is_rewriteFPexp_correct_def, is_rewriteFPexp_list_correct_def]
>- (rpt strip_tac \\ qexists_tac ‘st1.fp_state.opts’
\\ fs[semState_comp_eq, fpState_component_equality])
\\ rpt strip_tac \\ qpat_x_assum `_ = (_, _)` mp_tac
\\ simp[Once evaluate_cons]
\\ ntac 2 (reverse TOP_CASE_TAC \\ fs[])
\\ ntac 2 (reverse TOP_CASE_TAC \\ fs[])
\\ rpt strip_tac \\ rveq
\\ first_assum (qspecl_then [`st1`, `q`, `env`, `h`, `a`] impl_subgoal_tac)
\\ simp[Once evaluate_cons] \\ fs[] \\ rveq
>- (fs[freeVars_fp_bound_def, freeVars_list_fp_bound_def])
\\ first_x_assum (mp_then Any assume_tac (CONJUNCT1 optUntil_evaluate_ok))
\\ last_x_assum drule
\\ disch_then drule
\\ disch_then assume_tac \\ fs[]
\\ first_x_assum impl_subgoal_tac
>- (conj_tac
>- (fs[freeVars_fp_bound_def, freeVars_list_fp_bound_def])
\\ imp_res_tac evaluate_fp_opts_inv \\ fs[])
\\ fs[]
\\ first_x_assum (qspec_then `fpOpt'` assume_tac) \\ fs[]
\\ qexists_tac `optUntil (choicesR - choices) fpOpt fpOpt'`
\\ qexists_tac ‘choices’ \\ fs[]
\\ qpat_x_assum `evaluate _ _ exps = _`
(mp_then Any assume_tac (CONJUNCT1 evaluate_add_choices))
\\ first_x_assum (qspec_then ‘choicesR’ assume_tac)
\\ fs[fpState_component_equality, semState_comp_eq]
QED
Theorem lift_rewriteFPexp_correct_list:
! rws.
(! (st1 st2: 'a semanticPrimitives$state) env e r.
is_rewriteFPexp_correct rws st1 st2 env e r) ==>
! (st1 st2:'a semanticPrimitives$state) env exps r.
is_rewriteFPexp_list_correct rws st1 st2 env exps r
Proof
rpt strip_tac \\ drule lift_rewriteFPexp_correct_list_strong
\\ disch_then irule
QED
Theorem fpState_upd_id:
! s.
s with fp_state :=
s.fp_state with <| rws := s.fp_state.rws; opts := s.fp_state.opts |> = s
Proof
rpt strip_tac
\\ ‘s.fp_state with <| rws := s.fp_state.rws; opts := s.fp_state.opts |> = s.fp_state’
by (fs[fpState_component_equality])
\\ pop_assum (rewrite_tac o single)
\\ fs[semanticPrimitivesTheory.state_component_equality]
QED
Triviality MAP_MAP_triple:
! l f. MAP (λ (x,y,z). x) (MAP (λ (s1,s2,e). (s1,s2,f e)) l) = MAP (λ (x,y,z). x) l
Proof
Induct_on ‘l’ \\ fs[] \\ rpt strip_tac
\\ Cases_on ‘h’ \\ fs[] \\ Cases_on ‘r’ \\ fs[]
QED
Theorem MEM_for_all_MAPi:
! (P: 'a -> 'a -> bool) (f: 'a -> 'a) (g: 'a -> 'a) l l'.
(!x. MEM x l ==> P (f x) x) /\ (!x. P (g x) x) ==>
MAPi (λn e. if n = i then (f e) else (g e)) l = l' ==>
(!i. (i: num) < LENGTH l ==> P (EL i l') (EL i l))
Proof
rpt strip_tac
\\ imp_res_tac (INST_TYPE[alpha |-> beta] EL_MAPi)
\\ pop_assum ( Q.ISPEC_THEN ‘(λ (n :num) (e :'a). if n = (i :num) then ((f :'a -> 'a) e) else (g :'a -> 'a) e): num -> 'a -> 'a’ strip_assume_tac )
\\ fs[EVAL “(λn e. if n = i then f e else g e) i' (EL i' l)”]
\\ Cases_on ‘i = i'’ \\ rveq \\ fs[]
\\ qpat_x_assum ‘! x. MEM x l ==> P (f x) x’ (qspec_then ‘(EL i l)’ assume_tac)
\\ Q.ISPECL_THEN [‘l’, ‘(EL i l)’] strip_assume_tac MEM_EL
\\ last_assum ( (assume_tac o snd) o EQ_IMP_RULE ) \\ fs[]
\\ ‘EL i l = EL i l’ by fs[]
\\ first_x_assum irule
\\ qexists_tac ‘i’
\\ fs[]
QED
Theorem MAPi_EL_nested:
! l n. n < LENGTH l ==> EL n (MAPi (λ n e. (n, e)) l) = EL n (MAPi (λ n e. (n, (EL n l))) l)
Proof
rpt strip_tac
\\ qspecl_then [‘(λ n e. (n, e))’, ‘n’, ‘l’] imp_res_tac EL_MAPi
\\ qspecl_then [‘(λ n e. (n, (EL n l)))’, ‘n’, ‘l’] imp_res_tac EL_MAPi
\\ fs[EVAL “(λn e. (n,e)) n (EL n l)”, EVAL “(λn e. (n,EL n l)) n (EL n l)”]
QED
Theorem MAPi_EL_no_fun:
! l. MAPi (λ n e. (n, e)) l = MAPi (λ n e. (n, (EL n l))) l
Proof
rpt strip_tac
\\ qspecl_then [‘MAPi (λ n e. (n, e)) l’, ‘MAPi (λ n e. (n, (EL n l))) l’] strip_assume_tac LIST_EQ
\\ ‘LENGTH (MAPi (λ n e. (n, e)) l) = LENGTH (MAPi (λ n e. (n, (EL n l))) l)’ by fs[LENGTH_MAPi]
\\ fs[]
QED
Theorem MAPi_EL_list_quant:
! f l. MAPi (λ n e. f n e) l = MAPi (λ n e. f n (EL n l)) l
Proof
rpt strip_tac
\\ qspecl_then [‘MAPi (λ n e. f n e) l’, ‘MAPi (λ n e. f n (EL n l)) l’] strip_assume_tac LIST_EQ
\\ pop_assum irule
\\ rpt strip_tac \\ fs[LENGTH_MAPi]
QED
Theorem MAPi_EL_list:
MAPi (λ n e. f n e) l = MAPi (λ n e. f n (EL n l)) l
Proof
fs[MAPi_EL_list_quant]
QED
Theorem MAPi_same_EL_quant:
! exps. MAPi (λ n e. (EL n exps)) exps = exps
Proof
strip_tac
\\ qspecl_then [‘λ n e. e’, ‘exps’] assume_tac MAPi_EL_list_quant
\\ fs[]
QED
Theorem MAPi_same_EL:
MAPi (λ n e. (EL n exps)) exps = exps
Proof
fs[MAPi_same_EL_quant]
QED
local
val rewrites_no_effect =
“λ cfg path plan e. perform_rewrites cfg path plan e = e”
in
Theorem perform_rewrites_no_plan_same:
! cfg path plan e. plan = [] ==> ^rewrites_no_effect cfg path plan e
Proof
ho_match_mp_tac perform_rewrites_ind
\\ fs[perform_rewrites_def]
\\ strip_tac
>- (
rpt strip_tac
\\ Cases_on ‘cfg.canOpt’
\\ fs[rewriteFPexp_def]
)
>- (
rpt strip_tac
\\ Cases_on ‘cfg.canOpt’
\\ fs[rewriteFPexp_def]
\\ irule LIST_EQ
\\ fs[LENGTH_MAPi]
\\ rpt strip_tac
\\ Cases_on ‘x = i’
\\ fs[]
\\ TRY (
qpat_x_assum ‘!e. MEM e exps ==> _’ (qspec_then ‘EL i exps’ assume_tac)
\\ fs[MEM_EL]
\\ pop_assum irule
\\ qexists_tac ‘i’ \\ fs[]
)
\\ TRY (
Cases_on ‘EL i pes’ \\ fs[]
\\ qpat_x_assum ‘! p e. MEM (p, e) pes ==> _’ (qspecl_then [‘q’, ‘r’] assume_tac)
\\ fs[MEM_EL]
\\ pop_assum irule
\\ qexists_tac ‘i’ \\ fs[]
)
\\ TRY (
Cases_on ‘EL x pes’ \\ fs[]
)
)
QED
end
Theorem perform_rewrites_no_plan_simple:
perform_rewrites cfg path [] e = e
Proof
qspecl_then [‘cfg’, ‘path’, ‘[]’, ‘e’] strip_assume_tac perform_rewrites_no_plan_same
\\ fs[]
QED
Theorem enumerate_append:
! l l' n. enumerate n (l ++ l') = (enumerate n l) ++ (enumerate (n + LENGTH l) l')
Proof
Induct_on ‘l'’ \\ fs[miscTheory.enumerate_def]
\\ Induct_on ‘l’ \\ fs[miscTheory.enumerate_def]
QED
Definition freeVars_arithExp_bound_def:
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env (cfg: config)
Here (Lit l) =
T ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env (cfg: config)
Here (App op exps) =
(if isFpArithExp (App op exps) then freeVars_fp_bound (App op exps) env
else T) ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env (cfg: config)
Here (Var x) = T ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env (cfg: config)
Here e = T ∧
(* If we are not at the end of the path, further navigate through the AST *)
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg (Left _)
(Lit l) = T ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg (Left _)
(Var x) = T ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg (Center path)
(Raise e) =
freeVars_arithExp_bound st1 st2 env cfg path e ∧
(* We cannot support "Handle" expressions because we must be able to reorder exceptions *)
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg path
(Handle e pes) = T ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(ListIndex (i, path)) (Con mod exps) =
(EVERYi (λ n e. if (n = i)
then (freeVars_arithExp_bound st1 st2 env cfg path e)
else T) exps) ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Left _) (Fun s e) = T ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(ListIndex (i, path)) (App op exps) =
(EVERYi (λ n e. if (n = i)
then (freeVars_arithExp_bound st1 st2 env cfg path e)
else T) exps) ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Left path) (Log lop e2 e3) =
freeVars_arithExp_bound st1 st2 env cfg path e2 ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Right path) (Log lop e2 e3) =
freeVars_arithExp_bound st1 st2 env cfg path e3 ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Left path) (If e1 e2 e3) =
freeVars_arithExp_bound st1 st2 env cfg path e1 ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Center path) (If e1 e2 e3) =
freeVars_arithExp_bound st1 st2 env cfg path e2 ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Right path) (If e1 e2 e3) =
freeVars_arithExp_bound st1 st2 env cfg path e3 ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Left path) (Mat e pes) =
freeVars_arithExp_bound st1 st2 env cfg path e ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(ListIndex (i, path)) (Mat e pes) =
EVERYi (λ n (p, e').
if (n = i) then
∀ v env_v.
pmatch env.c st1.refs p (HD v) [] = Match env_v ⇒
∀ st1 st2. freeVars_arithExp_bound st1 st2
(env with v := nsAppend (alist_to_ns env_v) env.v) cfg path e'
else T) pes ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Left path) (Let so e1 e2) =
(freeVars_arithExp_bound st1 st2 env cfg path e1) ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Right path) (Let so e1 e2) =
(∀ r.
evaluate st1 env [e1] = (st2, Rval r) ⇒
(∀ st1 st2.
freeVars_arithExp_bound st1 st2 (env with v := nsOptBind so (HD r) env.v)
cfg path e2)) ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Center path) (Letrec ses e) =
(ALL_DISTINCT (MAP (λ(x,y,z). x) ses) ⇒
freeVars_arithExp_bound st1 st2 (env with v := build_rec_env ses env env.v) cfg path e) ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Center path) (Tannot e t) =
freeVars_arithExp_bound st1 st2 env cfg path e ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Center path) (Lannot e l) =
freeVars_arithExp_bound st1 st2 env cfg path e ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg
(Center path) (FpOptimise sc e) =
freeVars_arithExp_bound st1 st2 env (cfg with canOpt := if sc = Opt then T else F) path e ∧
freeVars_arithExp_bound (st1:'a semanticPrimitives$state) st2 env cfg _ e = T
End
Theorem rewriteFPexp_freeVars_fp_bound:
∀ rws e env.
rewriteFPexp rws e ≠ e ∧
freeVars_fp_bound e env ⇒
freeVars_fp_bound (rewriteFPexp rws e) env
Proof
Induct_on ‘rws’ \\ fs[rewriteFPexp_def]
\\ rpt strip_tac \\ fs[freeVars_fp_bound_def]
\\ imp_res_tac isFpArithExp_rewrite_preserved
\\ Cases_on ‘h’ \\ fs[rewriteFPexp_def]
\\ COND_CASES_TAC \\ fs[]
\\ TOP_CASE_TAC \\ fs[]
>- res_tac
\\ TOP_CASE_TAC \\ fs[]
>- res_tac
\\ qspecl_then [‘r’, ‘q’, ‘e’, ‘x'’, ‘x’, ‘[]’,
‘λ x. ∃ fp. nsLookup env.v x = SOME (FP_WordTree fp)’] mp_tac match_preserves_FV
\\ impl_tac \\ fs[substLookup_def]
\\ rpt strip_tac
\\ Cases_on ‘rewriteFPexp rws x' = x'’ \\ fs[]
\\ first_x_assum drule
\\ rpt (disch_then drule) \\ gs[]
QED
Definition isDoubleExp_def:
isDoubleExp (Var x) = T ∧
isDoubleExp (Lit l) = F ∧
isDoubleExp (Let x e1 e2) = (isDoubleExp e1 ∧ isDoubleExp e2) ∧
isDoubleExp (App FpFromWord [Lit (Word64 w)]) = T ∧
isDoubleExp (App op exps) =
(case op of
| FP_uop _ => isDoubleExpList exps
| FP_bop _ => isDoubleExpList exps
| FP_top _ => isDoubleExpList exps
| _ => F) ∧
isDoubleExp (FpOptimise sc e) = isDoubleExp e ∧
isDoubleExp _ = F
∧
isDoubleExpList [] = T ∧
isDoubleExpList (e1::es) = (isDoubleExp e1 ∧ isDoubleExpList es)
Termination
wf_rel_tac ‘measure (λ x. case x of |INL e => exp_size e |INR es => exp6_size es)’
End
Theorem isDoubleExp_evaluates:
(∀ e env (st1:'a semanticPrimitives$state) st2 r.
isDoubleExp e ∧
freeVars_fp_bound e env ∧
evaluate st1 env [e] = (st2, Rval r) ⇒
∃ fp. r = [FP_WordTree fp])
∧
(∀ exps env (st1:'a semanticPrimitives$state) st2 r.
isDoubleExpList exps ∧
freeVars_list_fp_bound exps env ⇒
∀ e. MEM e exps ⇒
evaluate st1 env [e] = (st2, Rval r) ⇒
∃ fp. r = [FP_WordTree fp])
Proof
ho_match_mp_tac isDoubleExp_ind
\\ rpt strip_tac \\ fs[isDoubleExp_def]
>- (gs[evaluate_def, freeVars_fp_bound_def] \\ rveq \\ gs[])
>- (
qpat_x_assum `evaluate _ _ _ = _` mp_tac
\\ gs[evaluate_def, CaseEq"result", CaseEq"prod"]
\\ rpt strip_tac \\ rveq
\\ last_x_assum (qspecl_then [‘env’, ‘st1’, ‘st'’, ‘v’] mp_tac)
\\ impl_tac >- gs[freeVars_fp_bound_def]
\\ strip_tac \\ rveq
\\ qpat_x_assum `evaluate _ (env with v := _) _ = _`
(fn th => first_x_assum (fn ith => mp_then Any mp_tac ith th))
\\ impl_tac
>- (
Cases_on ‘x’ \\ gs[namespaceTheory.nsOptBind_def, freeVars_fp_bound_def]
\\ rpt strip_tac \\ rename1 ‘nsBind y _ env.v’ \\ rename1 ‘x IN FV e2’
\\ Cases_on ‘x = Short y’ \\ gs[ml_progTheory.nsLookup_nsBind_compute])
\\ strip_tac \\ gs[])
>- (
gs[evaluate_def, CaseEq"prod", CaseEq"result", do_app_def,
astTheory.getOpClass_def, astTheory.isFpBool_def]
\\ rveq \\ gs[])
>~ [‘FpOptimise sc e’]
>- (
qpat_x_assum ‘evaluate _ _ _ = _’ mp_tac
\\ gs[evaluate_def, CaseEq"prod", CaseEq"result", do_app_def,
astTheory.getOpClass_def, astTheory.isFpBool_def, CaseEq"list"]
\\ strip_tac \\ gs[freeVars_fp_bound_def] \\ rveq
\\ qpat_x_assum `evaluate _ _ _ = _`
(fn th => first_x_assum (fn ith => mp_then Any mp_tac ith th))
\\ impl_tac
>- (gs[])
\\ strip_tac \\ gs[do_fpoptimise_def])
>~ [‘freeVars_list_fp_bound (e::exps)’, ‘evaluate st1 env [e2] = (st2, Rval r)’]
>- (
rveq \\ res_tac \\ pop_assum mp_tac \\ gs[freeVars_fp_bound_def, freeVars_list_fp_bound_def])
>~ [‘freeVars_list_fp_bound (e::exps)’, ‘MEM e2 exps’]
>- (
‘freeVars_list_fp_bound exps env’ by (gs[freeVars_list_fp_bound_def])
\\ res_tac \\ gs[])
\\ qpat_x_assum ‘evaluate _ _ _ = _’ mp_tac
\\ gs[evaluate_def, CaseEq"prod", CaseEq"result", do_app_def,
astTheory.getOpClass_def, astTheory.isFpBool_def, CaseEq"list"]
\\ rpt strip_tac \\ pop_assum mp_tac
\\ TOP_CASE_TAC \\ gs[CaseEq"prod"]
\\ pop_assum (fn th => rpt strip_tac \\ mp_tac th)
\\ rpt (TOP_CASE_TAC \\ gs[]) \\ rpt strip_tac \\ rveq
\\ gs[result_cond_rw] \\ rveq \\ gs[]
QED
Theorem isDoubleExp_evaluates_real:
(∀ e env (st1:'a semanticPrimitives$state) st2 r.
isDoubleExp e ∧
freeVars_real_bound e env ∧
evaluate st1 env [realify e] = (st2, Rval r) ⇒
∃ rn. r = [Real rn])
∧
(∀ exps env (st1:'a semanticPrimitives$state) st2 r.
isDoubleExpList exps ∧
freeVars_list_real_bound exps env ⇒
∀ e. MEM e exps ⇒
evaluate st1 env [realify e] = (st2, Rval r) ⇒
∃ rn. r = [Real rn])
Proof
ho_match_mp_tac isDoubleExp_ind
\\ rpt strip_tac \\ fs[isDoubleExp_def, realify_def]
>- (gs[evaluate_def, freeVars_real_bound_def] \\ rveq \\ gs[])
>- (
qpat_x_assum `evaluate _ _ _ = _` mp_tac
\\ gs[evaluate_def, CaseEq"result", CaseEq"prod"]
\\ rpt strip_tac \\ rveq
\\ last_x_assum (qspecl_then [‘env’, ‘st1’, ‘st'’, ‘v’] mp_tac)
\\ impl_tac >- gs[freeVars_real_bound_def]
\\ strip_tac \\ rveq
\\ qpat_x_assum `evaluate _ (env with v := _) _ = _`
(fn th => first_x_assum (fn ith => mp_then Any mp_tac ith th))
\\ impl_tac
>- (
Cases_on ‘x’ \\ gs[namespaceTheory.nsOptBind_def, freeVars_real_bound_def]
\\ rpt strip_tac \\ rename1 ‘nsBind y _ env.v’ \\ rename1 ‘x IN FV e2’
\\ Cases_on ‘x = Short y’ \\ gs[ml_progTheory.nsLookup_nsBind_compute])
\\ strip_tac \\ gs[])
>- (
gs[evaluate_def, CaseEq"prod", CaseEq"result", do_app_def,
astTheory.getOpClass_def, astTheory.isFpBool_def]
\\ Cases_on ‘st1.fp_state.real_sem’ \\ gs[]
\\ rveq \\ gs[])
>~ [‘FpOptimise sc e’]
>- (
qpat_x_assum ‘evaluate _ _ _ = _’ mp_tac
\\ gs[evaluate_def, CaseEq"prod", CaseEq"result", do_app_def,
astTheory.getOpClass_def, astTheory.isFpBool_def, CaseEq"list"]
\\ strip_tac \\ gs[freeVars_real_bound_def] \\ rveq
\\ qpat_x_assum `evaluate _ _ _ = _`
(fn th => first_x_assum (fn ith => mp_then Any mp_tac ith th))
\\ impl_tac
>- gs[]
\\ strip_tac \\ gs[do_fpoptimise_def])
>~ [‘freeVars_list_real_bound (e::exps)’, ‘evaluate st1 env [realify e2] = (st2, Rval r)’]
>- (
rveq \\ res_tac \\ pop_assum mp_tac \\ gs[freeVars_real_bound_def, freeVars_list_real_bound_def])
>~ [‘freeVars_list_real_bound (e::exps)’, ‘MEM e2 exps’]
>- (
‘freeVars_list_real_bound exps env’ by (gs[freeVars_list_real_bound_def])
\\ res_tac \\ gs[])
>~ [‘freeVars_real_bound (App (FP_top t_op) exps) env’]
>- (
qpat_x_assum ‘evaluate _ _ _ = _’ mp_tac
\\ gs[evaluate_def, CaseEq"prod", CaseEq"result", do_app_def,
astTheory.getOpClass_def, astTheory.isFpBool_def, CaseEq"list"]
\\ TOP_CASE_TAC \\ gs[CaseEq"prod"]
>- (gs[evaluate_def, CaseEq"prod", CaseEq"result", do_app_def,
astTheory.getOpClass_def, astTheory.isFpBool_def, CaseEq"list"])
\\ TOP_CASE_TAC \\ gs[CaseEq"prod"]
>- (gs[evaluate_def, CaseEq"prod", CaseEq"result", do_app_def,
astTheory.getOpClass_def, astTheory.isFpBool_def, CaseEq"list"])
\\ TOP_CASE_TAC \\ gs[CaseEq"prod"]
>- (gs[evaluate_def, CaseEq"prod", CaseEq"result", do_app_def,
astTheory.getOpClass_def, astTheory.isFpBool_def, CaseEq"list"])
\\ TOP_CASE_TAC \\ gs[CaseEq"prod"]
\\ gs[evaluate_def, CaseEq"prod", CaseEq"result", do_app_def,
astTheory.getOpClass_def, astTheory.isFpBool_def, CaseEq"list"]
\\ rpt strip_tac \\ rveq \\ gs[]
\\ pop_assum mp_tac \\ COND_CASES_TAC \\ gs[]
\\ TOP_CASE_TAC \\ gs[]
\\ pop_assum (fn th => rpt strip_tac \\ mp_tac th)
\\ rpt (TOP_CASE_TAC \\ gs[]) \\ rpt strip_tac \\ rveq
\\ qpat_x_assum ‘(if _ then _ else _)= (_, Rval _)’ mp_tac
\\ COND_CASES_TAC \\ gs[]
\\ TOP_CASE_TAC \\ gs[]
\\ pop_assum (fn th => rpt strip_tac \\ mp_tac th)
\\ rpt (TOP_CASE_TAC \\ gs[]) \\ rpt strip_tac \\ rveq
\\ gs[])
\\ qpat_x_assum ‘evaluate _ _ _ = _’ mp_tac
\\ gs[evaluate_def, CaseEq"prod", CaseEq"result", do_app_def,
astTheory.getOpClass_def, astTheory.isFpBool_def, CaseEq"list"]
\\ rpt strip_tac \\ pop_assum mp_tac
\\ ntac 2 (TOP_CASE_TAC \\ gs[CaseEq"prod"])
\\ pop_assum (fn th => rpt strip_tac \\ mp_tac th)
\\ rpt (TOP_CASE_TAC \\ gs[]) \\ rpt strip_tac \\ rveq
\\ gs[result_cond_rw] \\ rveq \\ gs[]
QED
Theorem EVERYi_T:
EVERYi (λ n x. T) ls
Proof
Induct_on ‘ls’ \\ gs[EVERYi_def]
\\ ‘(λ n x. T) o SUC = λ n x. T’ by gs[FUN_EQ_THM]
\\ pop_assum $ gs o single
QED
Theorem EVERYi_lift_MEM:
∀ exps cfg (st2:'a semanticPrimitives$state) st1 path cfg.
(∀ e.
MEM e exps ⇒
∀ (st2:'a semanticPrimitives$state) st1 path cfg.
freeVars_arithExp_bound st1 st2 env cfg path e) ⇒
∀ i.
EVERYi (λ n e. n = i ⇒ freeVars_arithExp_bound st1 st2 env cfg path e) exps
Proof
Induct_on ‘exps’ \\ gs[EVERYi_def]
\\ rpt strip_tac \\ gs[]
\\ Cases_on ‘i = 0’ \\ gs[]
>- (
‘(λ n e. n = i ⇒ freeVars_arithExp_bound st1 st2 env cfg path e) o SUC = (λ n e. T)’
by (rveq \\ gs[FUN_EQ_THM])
\\ rveq
\\ pop_assum (simp o single) \\ gs[EVERYi_T])
\\ ‘(λ n e. n = i ⇒ freeVars_arithExp_bound st1 st2 env cfg path e) o SUC =
(λ n e. n = i-1 ⇒ freeVars_arithExp_bound st1 st2 env cfg path e)’
by (gs[FUN_EQ_THM] \\ rpt strip_tac \\ EQ_TAC \\ gs[]
\\ rpt strip_tac \\ rveq \\ gs[])
\\ pop_assum $ gs o single
QED
Theorem EVERYi_lift_MEM_real:
∀ exps cfg (st2:'a semanticPrimitives$state) st1 path cfg.
(∀ e.
MEM e exps ⇒
∀ (st2:'a semanticPrimitives$state) st1 path cfg.
freeVars_realExp_bound st1 st2 env cfg path e) ⇒
∀ i.
EVERYi (λ n e. n = i ⇒ freeVars_realExp_bound st1 st2 env cfg path e) exps
Proof
Induct_on ‘exps’ \\ gs[EVERYi_def]
\\ rpt strip_tac \\ gs[]
\\ Cases_on ‘i = 0’ \\ gs[]
>- (
‘(λ n e. n = i ⇒ freeVars_realExp_bound st1 st2 env cfg path e) o SUC = (λ n e. T)’
by (rveq \\ gs[FUN_EQ_THM])
\\ rveq
\\ pop_assum (simp o single) \\ gs[EVERYi_T])
\\ ‘(λ n e. n = i ⇒ freeVars_realExp_bound st1 st2 env cfg path e) o SUC =
(λ n e. n = i-1 ⇒ freeVars_realExp_bound st1 st2 env cfg path e)’
by (gs[FUN_EQ_THM] \\ rpt strip_tac \\ EQ_TAC \\ gs[]
\\ rpt strip_tac \\ rveq \\ gs[])
\\ pop_assum $ gs o single
QED
Theorem isDoubleExp_freeVars_arithExp_bound:
(∀ e env.
isDoubleExp e ∧
freeVars_fp_bound e env ⇒
∀ (st1:'a semanticPrimitives$state) st2 cfg path.
freeVars_arithExp_bound st1 st2 env cfg path e)
∧
(∀ exps env.
isDoubleExpList exps ∧
freeVars_list_fp_bound exps env ⇒
∀ e. MEM e exps ⇒
∀ (st1:'a semanticPrimitives$state) st2 cfg path.
freeVars_arithExp_bound st1 st2 env cfg path e)
Proof
ho_match_mp_tac isDoubleExp_ind
\\ rpt strip_tac \\ gs[isDoubleExp_def]
>- (Cases_on ‘path’ \\ gs[freeVars_arithExp_bound_def])
>~ [‘Let x e1 e2’]
>- (
Cases_on ‘path’ \\ gs[freeVars_arithExp_bound_def]
>- (first_x_assum irule \\ gs[freeVars_fp_bound_def])
\\ rpt strip_tac \\ first_x_assum irule
\\ gs[freeVars_fp_bound_def]
\\ last_x_assum $ qspec_then ‘env’ mp_tac
\\ impl_tac >- gs[]
\\ rpt strip_tac
\\ Cases_on ‘x’ \\ gs[namespaceTheory.nsOptBind_def]
\\ imp_res_tac evaluate_sing \\ rveq \\ gs[]
\\ rename1 ‘y IN FV e2’
\\ Cases_on ‘y’ \\ gs[ml_progTheory.nsLookup_nsBind_compute]
\\ COND_CASES_TAC \\ gs[] \\ rveq
\\ last_x_assum $ mp_then Any drule (CONJUNCT1 isDoubleExp_evaluates)
\\ gs[] \\ disch_then irule
\\ gs[freeVars_fp_bound_def])
>- (
Cases_on ‘path’ \\ gs[freeVars_arithExp_bound_def]
\\ Cases_on ‘p’ \\ gs[freeVars_arithExp_bound_def, EVERYi_def]
\\ rpt strip_tac \\ Cases_on ‘r’ \\ gs[freeVars_arithExp_bound_def])
>- (
Cases_on ‘path’ \\ gs[freeVars_arithExp_bound_def]
\\ Cases_on ‘p’ \\ gs[freeVars_arithExp_bound_def]
\\ ‘freeVars_list_fp_bound exps env’
by gs[freeVars_fp_bound_def, freeVars_list_fp_bound_def]
\\ res_tac \\ gs[EVERYi_lift_MEM])
>- (
Cases_on ‘path’ \\ gs[freeVars_arithExp_bound_def]
\\ Cases_on ‘p’ \\ gs[freeVars_arithExp_bound_def]
\\ ‘freeVars_list_fp_bound exps env’
by gs[freeVars_fp_bound_def, freeVars_list_fp_bound_def]
\\ res_tac \\ gs[EVERYi_lift_MEM])
>- (
Cases_on ‘path’ \\ gs[freeVars_arithExp_bound_def]
\\ Cases_on ‘p’ \\ gs[freeVars_arithExp_bound_def]
\\ ‘freeVars_list_fp_bound exps env’
by gs[freeVars_fp_bound_def, freeVars_list_fp_bound_def]
\\ res_tac \\ gs[EVERYi_lift_MEM])
>- (
Cases_on ‘path’ \\ gs[freeVars_arithExp_bound_def]
\\ first_x_assum irule \\ gs[freeVars_fp_bound_def])
>- (
rveq \\ gs[]
\\ last_x_assum irule
\\ gs[freeVars_fp_bound_def, freeVars_list_fp_bound_def])
\\ ‘freeVars_list_fp_bound exps env’ by gs[freeVars_list_fp_bound_def]
\\ res_tac \\ first_x_assum irule
QED
Theorem isDoubleExp_freeVars_realExp_bound:
(∀ e env.
isDoubleExp e ∧
freeVars_real_bound e env ⇒
∀ (st1:'a semanticPrimitives$state) st2 cfg path.
freeVars_realExp_bound st1 st2 env cfg path e)
∧
(∀ exps env.
isDoubleExpList exps ∧
freeVars_list_real_bound exps env ⇒
∀ e. MEM e exps ⇒
∀ (st1:'a semanticPrimitives$state) st2 cfg path.
freeVars_realExp_bound st1 st2 env cfg path e)
Proof
ho_match_mp_tac isDoubleExp_ind
\\ rpt strip_tac \\ gs[isDoubleExp_def]
>- (Cases_on ‘path’ \\ gs[freeVars_realExp_bound_def])
>~ [‘Let x e1 e2’]
>- (
Cases_on ‘path’ \\ gs[freeVars_realExp_bound_def]
>- (first_x_assum irule \\ gs[freeVars_real_bound_def])
\\ rpt strip_tac \\ first_x_assum irule
\\ gs[freeVars_real_bound_def]
\\ last_x_assum $ qspec_then ‘env’ mp_tac
\\ impl_tac >- gs[]
\\ rpt strip_tac
\\ Cases_on ‘x’ \\ gs[namespaceTheory.nsOptBind_def]
\\ imp_res_tac evaluate_sing \\ rveq \\ gs[]
\\ rename1 ‘y IN FV e2’
\\ Cases_on ‘y’ \\ gs[ml_progTheory.nsLookup_nsBind_compute]
\\ COND_CASES_TAC \\ gs[] \\ rveq
\\ last_x_assum $ mp_then Any drule (CONJUNCT1 isDoubleExp_evaluates_real)
\\ gs[] \\ disch_then irule
\\ gs[freeVars_real_bound_def])
>- (
Cases_on ‘path’ \\ gs[freeVars_realExp_bound_def]
\\ Cases_on ‘p’ \\ gs[freeVars_realExp_bound_def, EVERYi_def]
\\ rpt strip_tac \\ Cases_on ‘r’ \\ gs[freeVars_realExp_bound_def])
>- (
Cases_on ‘path’ \\ gs[freeVars_realExp_bound_def]
\\ Cases_on ‘p’ \\ gs[freeVars_realExp_bound_def]
\\ ‘freeVars_list_real_bound exps env’
by gs[freeVars_real_bound_def, freeVars_list_real_bound_def]
\\ res_tac \\ gs[EVERYi_lift_MEM_real])
>- (
Cases_on ‘path’ \\ gs[freeVars_realExp_bound_def]
\\ Cases_on ‘p’ \\ gs[freeVars_realExp_bound_def]
\\ ‘freeVars_list_real_bound exps env’
by gs[freeVars_real_bound_def, freeVars_list_real_bound_def]
\\ res_tac \\ gs[EVERYi_lift_MEM_real])
>- (
Cases_on ‘path’ \\ gs[freeVars_realExp_bound_def]
\\ Cases_on ‘p’ \\ gs[freeVars_realExp_bound_def]
\\ ‘freeVars_list_real_bound exps env’
by gs[freeVars_real_bound_def, freeVars_list_real_bound_def]
\\ res_tac \\ gs[EVERYi_lift_MEM_real])
>- (
Cases_on ‘path’ \\ gs[freeVars_realExp_bound_def]
\\ first_x_assum irule \\ gs[freeVars_real_bound_def])
>- (
rveq \\ gs[]
\\ last_x_assum irule
\\ gs[freeVars_real_bound_def, freeVars_list_real_bound_def])
\\ ‘freeVars_list_real_bound exps env’ by gs[freeVars_list_real_bound_def]
\\ res_tac \\ first_x_assum irule
QED
Definition is_perform_rewrites_correct_def:
is_perform_rewrites_correct rws (st1:'a semanticPrimitives$state) st2 env cfg e r path <=>
evaluate st1 env [perform_rewrites cfg path rws e] = (st2, Rval r) /\
(∀ (st1:'a semanticPrimitives$state) st2. freeVars_arithExp_bound st1 st2 env cfg path e) ∧
(cfg.canOpt <=> st1.fp_state.canOpt = FPScope Opt) /\
st1.fp_state.canOpt <> Strict /\
(~ st1.fp_state.real_sem) ==>
? fpOpt choices fpOptR choicesR.
evaluate (st1 with fp_state :=
st1.fp_state with
<| rws := st1.fp_state.rws ++ rws;
opts := fpOpt;
choices := choices |>) env [e] =
(st2 with fp_state :=
st2.fp_state with
<| rws := st2.fp_state.rws ++ rws;
opts := fpOptR;
choices := choicesR |>,
Rval r)
End
val no_change_tac =
(qpat_x_assum ‘evaluate _ _ [_] = _’ (mp_then Any assume_tac (prep (CONJUNCT1 evaluate_fp_rws_append)))
\\ pop_assum (qspecl_then [‘rws’, ‘st2.fp_state.opts’] strip_assume_tac)
\\ fs[semState_comp_eq, fpState_component_equality]
\\ qexistsl_tac [‘fpOpt’, ‘st1.fp_state.choices’, ‘st2.fp_state.opts’, ‘st2.fp_state.choices’]
\\ imp_res_tac (CONJUNCT1 evaluate_add_choices)
\\ fs[semState_comp_eq, fpState_component_equality]);
fun ext_eval_tac t rws opts =
qpat_x_assum t $ mp_then Any (qspecl_then [rws, opts] strip_assume_tac)
(CONJUNCT1 evaluate_fp_rws_append);
fun ext_evalm_tac t rws opts =
qpat_x_assum t $ mp_then Any (qspecl_then [rws, opts] strip_assume_tac)
(CONJUNCT1 $ CONJUNCT2 evaluate_fp_rws_append);
fun ext_evald_tac t rws opts =
qpat_x_assum t $ mp_then Any (qspecl_then [rws, opts] strip_assume_tac)
(CONJUNCT2 $ CONJUNCT2 evaluate_fp_rws_append);
fun ext_choices_tac t choices =
qpat_x_assum t $ mp_then Any (qspec_then choices assume_tac) (CONJUNCT1 evaluate_add_choices)
fun ext_choicesm_tac t choices =
qpat_x_assum t $ mp_then Any (qspec_then choices assume_tac) (CONJUNCT1 $ CONJUNCT2 evaluate_add_choices)
fun ext_choicesd_tac t choices =
qpat_x_assum t $ mp_then Any (qspec_then choices assume_tac) (CONJUNCT2 $ CONJUNCT2 evaluate_add_choices)
fun get_IH t =
qpat_x_assum t (fn th => first_x_assum (fn ith => mp_then Any mp_tac ith th))
Theorem perform_rewrites_lift_reverse:
∀ exps (st1:'a semanticPrimitives$state) st2 env vs cfg path rws i.
(∀ (st1:'a semanticPrimitives$state) st2.
EVERYi
(λn e. n = i ⇒ freeVars_arithExp_bound st1 st2 env cfg path e)
exps) ∧
(∀ e. MEM e exps ⇒
∀ (st1:'a semanticPrimitives$state) st2 env r.
(∀ (st1:'a semanticPrimitives$state) st2. freeVars_arithExp_bound st1 st2 env cfg path e) ∧
(cfg.canOpt ⇔
st1.fp_state.canOpt = FPScope Opt) ∧
st1.fp_state.canOpt ≠ Strict ∧ ¬st1.fp_state.real_sem ∧
evaluate st1 env [perform_rewrites cfg path rws e] = (st2,Rval r) ⇒
∃ fpOpt choices fpOptR choicesR.
evaluate
(st1 with
fp_state :=
st1.fp_state with
<|rws := st1.fp_state.rws ++ rws; opts := fpOpt;
choices := choices|>) env [e] =
(st2 with
fp_state :=
st2.fp_state with
<|rws := st2.fp_state.rws ++ rws; opts := fpOptR;
choices := choicesR|>,Rval r)) ∧
(cfg.canOpt ⇔ st1.fp_state.canOpt = FPScope Opt) ∧
st1.fp_state.canOpt ≠ Strict ∧
(~ st1.fp_state.real_sem) ∧
evaluate st1 env (REVERSE
(MAPi
(λn e. if n = i then perform_rewrites cfg path rws e else e)
exps)) = (st2,Rval vs) ⇒
∃ fpOpt choices fpOptR choicesR.
evaluate
(st1 with
fp_state :=
st1.fp_state with
<|rws := st1.fp_state.rws ++ rws; opts := fpOpt;
choices := choices|>) env (REVERSE exps) =
(st2 with
fp_state :=
st2.fp_state with
<|rws := st2.fp_state.rws ++ rws; opts := fpOptR;
choices := choicesR|>,Rval vs)