forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icing_optimisationProofsScript.sml
2651 lines (2584 loc) · 120 KB
/
icing_optimisationProofsScript.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 proofs for peephole optimisations supported by PrincessCake
Each optimisation is defined in icing_optimisationsScript.sml.
This file proves the low-level correctness theorems for a single
application of the optimisation.
Real-valued identity proofs are in icing_realIdProofsScript.sml.
The overall correctness proof for a particular run of the optimiser
from source_to_source2Script is build using the automation in
icing_optimisationsLib and the general theorems from
source_to_source2ProofsScript.
*)
open bossLib ml_translatorLib;
open semanticPrimitivesTheory evaluatePropsTheory;
open fpValTreeTheory fpSemPropsTheory fpOptTheory fpOptPropsTheory
icing_optimisationsTheory icing_rewriterTheory source_to_source2ProofsTheory
floatToRealTheory floatToRealProofsTheory evaluateTheory
pureExpsTheory binary_ieeeTheory;
val _ = temp_delsimps ["lift_disj_eq", "lift_imp_disj"]
open preamble;
val _ = new_theory "icing_optimisationProofs";
val state_eqs = [state_component_equality, fpState_component_equality];
(** Automatically prove trivial goals about fp oracle **)
val fp_inv_tac = imp_res_tac evaluate_fp_opts_inv \\ fs[];
Theorem evaluate_append_rws:
∀ opts st1 env es st2 r.
evaluate st1 env es = (st2, r) ⇒
∃ fpOpt fpOptR.
evaluate
(st1 with fp_state := st1.fp_state with <|rws := st1.fp_state.rws ++ opts; opts := fpOpt |>)
env es =
(st2 with fp_state := st2.fp_state with <| rws := st2.fp_state.rws ++ opts; opts := fpOptR |>, r)
Proof
rpt strip_tac
\\ imp_res_tac (SIMP_RULE std_ss [] evaluate_fp_rws_up)
\\ first_x_assum (qspec_then ‘st1.fp_state.rws ++ opts’ mp_tac)
\\ impl_tac \\ fs[]
\\ strip_tac \\ qexists_tac ‘fpOpt’
\\ fs state_eqs
\\ fp_inv_tac
QED
(** Extend evaluate statement t with rewrites in rws **)
fun extend_eval_tac t rws =
qpat_assum t (mp_then Any (fn thm => Q.SPEC_THEN rws mp_tac thm)
evaluate_append_rws);
(** replace the fp oracle choice function in t1 with such that it ends with the
oracle t2 **)
fun optUntil_tac t1 t2 =
qpat_x_assum t1 (mp_then Any mp_tac (CONJUNCT1 optUntil_evaluate_ok))
\\ disch_then (qspec_then t2 assume_tac) \\ fs[];
(** Automatically proves the cases theorem for rewrite r **)
fun prove_cases_thm r =
rpt gen_tac \\ simp[r, DefnBase.one_line_ify NONE rewriteFPexp_def]
\\ rpt (TOP_CASE_TAC \\ fs[])
\\ fs[matchesFPexp_def, appFPexp_def,
CaseEq"option", CaseEq"exp", CaseEq "op",CaseEq "list"]
\\ rpt strip_tac
\\ rveq \\ fs[] \\ rpt (pop_assum mp_tac)
\\ EVAL_TAC \\ fs[];
(** Automatically proves the cases theorem for rewrite r that is defined as the reverse of rewrite r_rev **)
fun prove_cases_reverse_thm r r_rev =
fs[r, reverse_tuple_def, r_rev]
\\ prove_cases_thm r
Theorem rwAllWordTree_comp_left:
! b v1 v2 vres insts rws.
rwAllWordTree insts rws v1 = SOME vres ==>
rwAllWordTree (MAP (λ inst. case inst of |RewriteApp p id => RewriteApp (Left p) id) insts) rws (Fp_bop b v1 v2) =
SOME (Fp_bop b vres v2)
Proof
Induct_on `insts` \\ rpt strip_tac
\\ fs[rwAllWordTree_def]
\\ Cases_on `h` \\ fs[rwAllWordTree_def, option_case_eq]
\\ res_tac
\\ first_x_assum (qspecl_then [`v2`, `b`] assume_tac)
\\ fs[]
\\ fs[rwAllWordTree_def, rwFp_pathWordTree_def, option_map_def]
QED
Theorem rwAllWordTree_comp_right:
! b v1 v2 vres insts rws.
rwAllWordTree insts rws v2 = SOME vres ==>
rwAllWordTree (MAP (λ inst. case inst of |RewriteApp p id => RewriteApp (Right p) id) insts) rws (Fp_bop b v1 v2) =
SOME (Fp_bop b v1 vres)
Proof
Induct_on `insts` \\ rpt strip_tac
\\ fs[rwAllWordTree_def]
\\ Cases_on `h` \\ fs[rwAllWordTree_def, option_case_eq]
\\ res_tac
\\ first_x_assum (qspecl_then [`v1`, `b`] assume_tac)
\\ fs[]
\\ fs[rwAllWordTree_def, rwFp_pathWordTree_def, option_map_def]
QED
(**
Case theorems for application of each rewrite
They allow to do a case-distinction on whether the rewrite fired or not in the
simulation proofs, thus reducing a case split over the AST of the expression
to a case split of whether the rewrite fired or not
**)
Theorem fp_comm_gen_cases:
!e fpBop.
(? e1 e2.
e = (App (FP_bop fpBop) [e1; e2]) /\
isPureExp e /\
rewriteFPexp [fp_comm_gen fpBop] (App (FP_bop fpBop) [e1; e2]) =
App (FP_bop fpBop) [e2; e1]) \/
(rewriteFPexp [fp_comm_gen fpBop] e = e)
Proof
prove_cases_thm fp_comm_gen_def
QED
Theorem fp_assoc_gen_cases:
!e fpBop.
(? e1 e2 e3.
e = (App (FP_bop fpBop) [App (FP_bop fpBop) [e1; e2]; e3]) /\
isPureExp e /\
rewriteFPexp [fp_assoc_gen fpBop] (App (FP_bop fpBop) [App (FP_bop fpBop) [e1; e2]; e3]) =
App (FP_bop fpBop) [e1; (App (FP_bop fpBop) [e2; e3])]) \/
(rewriteFPexp [fp_assoc_gen fpBop] e = e)
Proof
prove_cases_thm fp_assoc_gen_def
QED
Theorem fp_assoc2_gen_cases:
∀ e fpBop.
(∃ e1 e2 e3.
e = (App (FP_bop fpBop) [e1; (App (FP_bop fpBop) [e2; e3])]) ∧
isPureExp e ∧ isFpArithExp e ∧
rewriteFPexp [fp_assoc2_gen fpBop] (App (FP_bop fpBop) [e1; (App (FP_bop fpBop) [e2; e3])]) =
(App (FP_bop fpBop) [App (FP_bop fpBop) [e1; e2]; e3])) ∨
(rewriteFPexp [fp_assoc2_gen fpBop] e = e)
Proof
rpt gen_tac \\ Cases_on `e`
\\ fs[fp_assoc2_gen_def, reverse_tuple_def, fp_assoc_gen_def, rewriteFPexp_def, isPureExp_def, matchesFPexp_def]
\\ rename1 `App op els`
\\ Cases_on `op` \\ fs[isPureOp_def]
\\ Cases_on ‘isPureExpList els ∧
isFpArithPat (Binop fpBop (PatVar 0) (Binop fpBop (PatVar 1) (PatVar 2))) ∧
isFpArithPat (Binop fpBop (Binop fpBop (PatVar 0) (PatVar 1)) (PatVar 2)) ∧
isFpArithExp (App (FP_bop f) els)’ \\ fs[]
\\ Cases_on ‘els’ \\ fs[]
\\ Cases_on ‘t’ \\ fs[]
\\ Cases_on ‘t'’ \\ fs[]
\\ Cases_on ‘fpBop = f’ \\ fs[]
\\ Cases_on ‘isPureExpList [h;h']’ \\ fs[isPureExp_def]
\\ fs[substLookup_def]
\\ Cases_on ‘h'’ \\ fs[]
\\ Cases_on ‘o'’ \\ fs[]
\\ Cases_on ‘l’ \\ fs[]
\\ Cases_on ‘t’ \\ fs[]
\\ Cases_on ‘t'’ \\ fs[]
\\ fs[isPureExp_def]
\\ Cases_on ‘f = f'’ \\ fs[] \\ rveq
\\ EVAL_TAC
QED
Theorem fp_fma_intro_cases:
∀ e.
(∃ e1 e2 e3.
e = (App (FP_bop FP_Add) [App (FP_bop FP_Mul) [e1; e2]; e3]) ∧
isPureExp e ∧
rewriteFPexp [fp_fma_intro] (App (FP_bop FP_Add) [App (FP_bop FP_Mul) [e1; e2]; e3]) =
App (FP_top FP_Fma) [e3;e1;e2]) ∨
(rewriteFPexp [fp_fma_intro] e = e)
Proof
prove_cases_thm fp_fma_intro_def
QED
Theorem fp_sub_add_cases:
∀ e.
(∃ e1 e2.
e = (App (FP_bop FP_Sub) [e1; e2]) ∧
isPureExp e ∧
rewriteFPexp [fp_sub_add] (App (FP_bop FP_Sub) [e1; e2]) =
App (FP_bop FP_Add) [e1; App (FP_uop FP_Neg) [e2]]) ∨
(rewriteFPexp [fp_sub_add] e = e)
Proof
prove_cases_thm fp_sub_add_def
QED
Theorem fp_add_sub_cases:
∀ e.
(∃ e1 e2.
e = App (FP_bop FP_Add) [e1; App (FP_uop FP_Neg) [e2]] ∧
isPureExp e ∧
rewriteFPexp [fp_add_sub] (App (FP_bop FP_Add) [e1; App (FP_uop FP_Neg) [e2]]) =
App (FP_bop FP_Sub) [e1; e2]) ∨
(rewriteFPexp [fp_add_sub] e = e)
Proof
prove_cases_reverse_thm fp_add_sub_def fp_sub_add_def
QED
Theorem fp_neg_push_mul_r_cases:
∀ e.
(∃ e1 e2 e3.
e = (App (FP_bop FP_Add) [App (FP_uop FP_Neg) [App (FP_bop FP_Mul) [e1; e2]]; e3]) ∧
isPureExp e ∧
rewriteFPexp [fp_neg_push_mul_r]
(App (FP_bop FP_Add) [App (FP_uop FP_Neg) [App (FP_bop FP_Mul) [e1; e2]]; e3]) =
(App (FP_bop FP_Add) [App (FP_bop FP_Mul) [e1; App (FP_uop FP_Neg) [e2]]; e3])) ∨
(rewriteFPexp [fp_neg_push_mul_r] e = e)
Proof
prove_cases_thm fp_neg_push_mul_r_def
QED
Theorem fp_times_zero_cases:
∀ e.
(∃ e1.
e = (App (FP_bop FP_Mul) [e1; App FpFromWord [Lit (Word64 0w)]]) ∧
isPureExp (App (FP_bop FP_Mul) [e1; App FpFromWord [Lit (Word64 0w)]]) ∧
isFpArithExp e ∧
rewriteFPexp [fp_times_zero] e =
(App FpFromWord [Lit (Word64 0w)])) ∨
(rewriteFPexp [fp_times_zero] e = e)
Proof
prove_cases_thm fp_times_zero_def
\\ rpt strip_tac
\\ fs[CaseEq"option", CaseEq"prod", CaseEq"list", CaseEq"lit"]
\\ fs[NULL_EQ]
QED
Theorem fp_times_one_cases:
∀ e.
(∃ e1.
e = (App (FP_bop FP_Mul) [e1; App FpFromWord [Lit (Word64 4607182418800017408w)]]) ∧
isPureExp (App (FP_bop FP_Mul) [e1; App FpFromWord [Lit (Word64 4607182418800017408w)]]) ∧
isFpArithExp e ∧
rewriteFPexp [fp_times_one] e = e1) ∨
(rewriteFPexp [fp_times_one] e = e)
Proof
prove_cases_thm fp_times_one_def
\\ rpt strip_tac
\\ fs[CaseEq"option", CaseEq"prod", CaseEq"list", CaseEq"lit"]
\\ fs[NULL_EQ]
\\ fs[substAdd_def, substUpdate_def] \\ rveq
\\ fs[substLookup_def]
QED
Theorem fp_times_minus_one_neg_cases:
∀ e.
(∃ e1.
e = (App (FP_bop FP_Mul) [e1; App FpFromWord [Lit (Word64 13830554455654793216w)]]) ∧
isPureExp (App (FP_bop FP_Mul) [e1; App FpFromWord [Lit (Word64 13830554455654793216w)]]) ∧
isFpArithExp (App (FP_bop FP_Mul) [e1; App FpFromWord [Lit (Word64 13830554455654793216w)]]) ∧
rewriteFPexp [fp_times_minus_one_neg] e = App (FP_uop FP_Neg) [e1]) ∨
(rewriteFPexp [fp_times_minus_one_neg] e = e)
Proof
prove_cases_thm fp_times_minus_one_neg_def
\\ rpt strip_tac
\\ fs[CaseEq"option", CaseEq"prod", CaseEq"list", CaseEq"lit"]
\\ fs[NULL_EQ]
\\ fs[substAdd_def, substUpdate_def] \\ rveq
\\ fs[substLookup_def]
QED
Theorem fp_neg_times_minus_one_cases:
∀ e.
(∃ e1.
e = App (FP_uop FP_Neg) [e1] ∧
isPureExp (App (FP_uop FP_Neg) [e1]) ∧
isFpArithExp (App (FP_uop FP_Neg) [e1]) ∧
rewriteFPexp [fp_neg_times_minus_one] e = (App (FP_bop FP_Mul) [e1; App FpFromWord [Lit (Word64 13830554455654793216w)]])) ∨
(rewriteFPexp [fp_neg_times_minus_one] e = e)
Proof
prove_cases_reverse_thm fp_neg_times_minus_one_def fp_times_minus_one_neg_def
QED
Theorem fp_times_two_to_add_cases:
∀ e.
(∃ e1 e2.
e = App (FP_bop FP_Mul) [e1; App FpFromWord [Lit (Word64 4611686018427387904w)]] ∧
isPureExp e ∧ isFpArithExp e ∧
rewriteFPexp [fp_times_two_to_add] e =
App (FP_bop FP_Add) [e1; e1]) ∨
(rewriteFPexp [fp_times_two_to_add] e = e)
Proof
prove_cases_thm fp_times_two_to_add_def
\\ rpt strip_tac
\\ fs[CaseEq"option", CaseEq"prod", CaseEq"list", CaseEq"lit"]
\\ fs[NULL_EQ]
\\ fs[substAdd_def, substUpdate_def] \\ rveq
\\ fs[substLookup_def]
QED
Theorem fp_times_three_to_add_cases:
∀ e.
(∃ e1 e2.
e = App (FP_bop FP_Mul) [e1; App FpFromWord [Lit (Word64 4613937818241073152w)]] ∧
isPureExp e ∧ isFpArithExp e ∧
rewriteFPexp [fp_times_three_to_add] e =
App (FP_bop FP_Add) [App (FP_bop FP_Add) [e1; e1]; e1]) ∨
(rewriteFPexp [fp_times_three_to_add] e = e)
Proof
prove_cases_thm fp_times_three_to_add_def
\\ rpt strip_tac
\\ fs[CaseEq"option", CaseEq"prod", CaseEq"list", CaseEq"lit"]
\\ fs[NULL_EQ]
\\ fs[substAdd_def, substUpdate_def] \\ rveq
\\ fs[substLookup_def]
QED
Theorem fp_plus_zero_cases:
∀ e.
(∃ e1.
e = (App (FP_bop FP_Add) [e1; App FpFromWord [Lit (Word64 0w)]]) ∧
isPureExp e ∧ isFpArithExp e ∧
rewriteFPexp [fp_plus_zero] e = e1) ∨
(rewriteFPexp [fp_plus_zero] e = e)
Proof
prove_cases_thm fp_plus_zero_def
\\ rpt strip_tac
\\ fs[CaseEq"option", CaseEq"prod", CaseEq"list", CaseEq"lit"]
\\ rveq
\\ fs[NULL_EQ]
\\ fs[substLookup_def, substAdd_def, substUpdate_def]
QED
Theorem fp_times_into_div_cases:
∀ e.
(∃ e1 e2 e3.
e = App (FP_bop FP_Mul) [ App (FP_bop FP_Div) [e1; e2]; e3] ∧
isPureExp e ∧
rewriteFPexp [fp_times_into_div] e = App (FP_bop FP_Div) [ App (FP_bop FP_Mul) [e1; e3]; e2]) ∨
(rewriteFPexp [fp_times_into_div] e = e)
Proof
prove_cases_thm fp_times_into_div_def
QED
Theorem fp_same_sub_cases:
∀ e.
(∃ e1.
e = (App (FP_bop FP_Sub) [e1; e1]) ∧
isPureExp e ∧ isFpArithExp e ∧
rewriteFPexp [fp_same_sub] e = App FpFromWord [Lit (Word64 0w)]) ∨
(rewriteFPexp [fp_same_sub] e = e)
Proof
prove_cases_thm fp_same_sub_def
QED
Theorem fp_distribute_gen_cases:
∀ e fpBopInner fpBopOuter.
(∃ e1 e2 e3.
e = App (FP_bop fpBopOuter) [ App (FP_bop fpBopInner) [e1; e2]; App (FP_bop fpBopInner) [e3; e2]] ∧
isPureExp e ∧ isFpArithExp e ∧
rewriteFPexp [fp_distribute_gen fpBopInner fpBopOuter] e = App (FP_bop fpBopInner) [ App (FP_bop fpBopOuter) [e1; e3]; e2]) ∨
(rewriteFPexp [fp_distribute_gen fpBopInner fpBopOuter] e = e)
Proof
prove_cases_thm fp_distribute_gen_def
QED
Theorem fp_undistribute_gen_cases:
∀ e fpBopInner fpBopOuter.
(∃ e1 e2 e3.
e = App (FP_bop fpBopInner) [ App (FP_bop fpBopOuter) [e1; e3]; e2] ∧
isPureExp e ∧
rewriteFPexp [fp_undistribute_gen fpBopInner fpBopOuter] e = App (FP_bop fpBopOuter) [ App (FP_bop fpBopInner) [e1; e2]; App (FP_bop fpBopInner) [e3; e2]]) ∨
(rewriteFPexp [fp_undistribute_gen fpBopInner fpBopOuter] e = e)
Proof
prove_cases_reverse_thm fp_undistribute_gen_def fp_distribute_gen_def
QED
(** Define some simplified versions of theorems that make it
easier to prove simulations **)
val eval_fp_opt_inv =
SIMP_RULE std_ss [] fpSemPropsTheory.evaluate_fp_opts_inv
|> CONJ_LIST 2 |> hd;
val isPureExp_ignores_state =
EVAL_RULE isPureExpList_swap_ffi
|> CONJ_LIST 2
|> hd;
(** t should be one of the optimisations from icing_optimisationsTheory **)
fun fp_rws_append_opt t =
SIMP_RULE std_ss [] evaluate_fp_rws_append
|> CONJ_LIST 2
|> map (SPEC_ALL) |> map (GEN ``(opts:(fp_pat #fp_pat) list)``)
|> map (Q.SPEC `[^t]`) |> map GEN_ALL
|> LIST_CONJ;
Theorem evaluate_rewrite_hoisting:
(∀ e1 (st1:'a semanticPrimitives$state) env st2 v fp.
st1.fp_state.canOpt = FPScope Opt ∧
evaluate st1 env [e1] = (st2, Rval [v]) ∧
fp_translate v = SOME (FP_WordTree fp) ∧
isPureExp e1 ∧ isFpArithExp e1 ⇒
∃ vUnOpt fpUnOpt sched choices.
evaluate (st1 with fp_state:= st1.fp_state with opts:= λ x. []) env [e1] =
(st2 with fp_state := st2.fp_state with <| opts := λ x. []; choices := choices |>, Rval [vUnOpt]) ∧
fp_translate vUnOpt = SOME (FP_WordTree fpUnOpt) ∧
do_fprw (Rval (FP_WordTree fpUnOpt)) sched st1.fp_state.rws = SOME (Rval (FP_WordTree fp))) ∧
(∀ es env fps.
isPureExpList es ∧ isFpArithExpList es ⇒
∀ (st1:'a semanticPrimitives$state) st2 e v fp.
MEM e es ⇒
st1.fp_state.canOpt = FPScope Opt ∧
evaluate st1 env [e] = (st2, Rval [v]) ∧
fp_translate v = SOME (FP_WordTree fp)⇒
∃ vUnOpt fpUnOpt sched choices.
evaluate (st1 with fp_state:= st1.fp_state with opts:= λ x. []) env [e] =
(st2 with fp_state := st2.fp_state with <| opts := λ x. []; choices := choices |>, Rval [vUnOpt]) ∧
fp_translate vUnOpt = SOME (FP_WordTree fpUnOpt) ∧
do_fprw (Rval (FP_WordTree fpUnOpt)) sched st1.fp_state.rws = SOME (Rval (FP_WordTree fp)))
Proof
ho_match_mp_tac isFpArithExp_ind \\ rpt strip_tac
\\ TRY (qpat_x_assum `isPureExp _` mp_tac \\ simp[isPureExp_def])
\\ TRY (qpat_x_assum `isFpArithExp _` mp_tac \\ simp[isFpArithExp_def])
>- (
fs[evaluate_def, CaseEq"option"] \\ rveq
\\ fs state_eqs \\ qexistsl_tac [‘[]’] \\ fs[do_fprw_def, rwAllWordTree_def])
>- (
fs[evaluate_def, CaseEq"option", astTheory.getOpClass_def,
do_app_def]
\\ rveq \\ rpt strip_tac
\\ fs state_eqs \\ qexistsl_tac [‘[]’] \\ fs[do_fprw_def, rwAllWordTree_def])
>- (
fs[quantHeuristicsTheory.LENGTH_TO_EXISTS_CONS] \\ rpt strip_tac \\ rveq
\\ qpat_x_assum `evaluate _ _ _ = _` mp_tac
\\ simp[evaluate_def, CaseEq"option",
astTheory.getOpClass_def, do_app_def, CaseEq"result"]
\\ ntac 2 (TOP_CASE_TAC \\ fs[])
\\ imp_res_tac evaluate_sing \\ rveq \\ gs[]
\\ strip_tac \\ fs[CaseEq"option", CaseEq"prod", CaseEq"v", astTheory.isFpBool_def]
\\ rveq
\\ rename1 ‘evaluate st1 env [_] = (st2, _)’
\\ ‘st2.fp_state.canOpt = FPScope Opt’ by fp_inv_tac
\\ gs[PULL_EXISTS]
\\ first_x_assum (fn ith => qpat_x_assum `evaluate _ _ _ = _`
(fn th => mp_then Any mp_tac ith th))
\\ rpt (disch_then drule) \\ strip_tac
\\ fs ([shift_fp_opts_def] @ state_eqs)
\\ rveq
\\ qexists_tac ‘FP_WordTree (fp_uop e1 fpUnOpt)’
\\ qexists_tac ‘fp_uop e1 fpUnOpt’ \\ fs[fp_translate_def, GSYM PULL_EXISTS]
\\ fs[do_fprw_def, CaseEq "option"]
\\ first_x_assum $ mp_then Any assume_tac rwAllWordTree_comp_unop
\\ first_x_assum $ qspec_then ‘e1’ strip_assume_tac
\\ qexists_tac ‘insts_new ++ (case do_fprw
(Rval (FP_WordTree (fp_uop e1 w1))) (st2.fp_state.opts 0)
st2.fp_state.rws of
| NONE => []
| SOME _ => (st2.fp_state.opts 0))’(* TODO *)
\\ qexists_tac ‘st2 with
fp_state :=
st2.fp_state with <|opts := (λx. []); choices := choices|>’
\\ fs state_eqs
\\ fs[do_fprw_def, rwAllWordTree_def]
\\ Cases_on ‘rwAllWordTree (st2.fp_state.opts 0) st2.fp_state.rws (fp_uop e1 w1)’
\\ fs[] \\ rveq \\ fs[fp_uop_def, fp_translate_def]
\\ irule rwAllWordTree_chaining_exact
\\ asm_exists_tac \\ fs[] \\ rveq \\ fs[rwAllWordTree_def]
\\ fp_inv_tac)
>- (
fs[quantHeuristicsTheory.LENGTH_TO_EXISTS_CONS] \\ rpt strip_tac \\ rveq
\\ qpat_x_assum `evaluate _ _ _ = _` mp_tac
\\ simp[evaluate_def, CaseEq"option",
astTheory.getOpClass_def, do_app_def, CaseEq"result", evaluate_case_case]
\\ ntac 2 (TOP_CASE_TAC \\ fs[])
\\ ntac 2 (TOP_CASE_TAC \\ fs[])
\\ imp_res_tac evaluate_sing \\ rveq \\ gs[]
\\ strip_tac \\ fs[CaseEq"option", CaseEq"prod", CaseEq"v", astTheory.isFpBool_def]
\\ rveq
\\ rename1 ‘evaluate st1 env [e2] = (st2, _)’
\\ rename1 ‘evaluate st2 env [e1] = (st3, _)’
\\ ‘st2.fp_state.canOpt = FPScope Opt’ by fp_inv_tac
\\ ‘st3.fp_state.canOpt = FPScope Opt’ by fp_inv_tac
\\ gs[]
\\ first_assum (fn ith => qpat_x_assum `evaluate _ _ _ = _`
(fn th => mp_then Any mp_tac ith th))
\\ rpt (disch_then drule)
\\ disch_then (qspec_then ‘w1’ mp_tac) \\ impl_tac \\ fs[]
\\ strip_tac
\\ first_assum (fn ith => qpat_x_assum `evaluate _ _ [e2] = _`
(fn th => mp_then Any mp_tac ith th))
\\ rpt (disch_then drule)
\\ disch_then (qspec_then ‘w2’ mp_tac) \\ impl_tac \\ fs[]
\\ strip_tac
\\ fs ([shift_fp_opts_def] @ state_eqs)
\\ fs[PULL_EXISTS]
\\ qexists_tac ‘FP_WordTree (fp_bop v1 fpUnOpt fpUnOpt')’
\\ qexists_tac ‘fp_bop v1 fpUnOpt fpUnOpt'’
\\ fs[fp_translate_def, do_fprw_def, CaseEq"option"]
\\ first_x_assum $ mp_then Any mp_tac rwAllWordTree_comp_right
\\ first_x_assum $ mp_then Any mp_tac rwAllWordTree_comp_left
\\ disch_then (qspecl_then [‘v1’, ‘fpUnOpt'’] mp_tac)
\\ qmatch_goalsub_abbrev_tac ‘rwAllWordTree sched_e2 _ (Fp_bop v1 _ _) = _’
\\ strip_tac
\\ disch_then (qspecl_then [‘v1’, ‘w1’] mp_tac)
\\ qmatch_goalsub_abbrev_tac ‘rwAllWordTree sched_e1 _ (Fp_bop v1 _ _) = _’
\\ strip_tac \\ rveq \\ gs[]
\\ rveq \\ gs[]
\\ qexists_tac ‘sched_e2 ++ sched_e1 ++
(case rwAllWordTree (st3.fp_state.opts 0) st3.fp_state.rws
(fp_bop v1 w1 w2) of
| NONE => []
| SOME _ => (st3.fp_state.opts 0))’
\\ qpat_x_assum `evaluate _ _ [e1] = _` $ mp_then Any mp_tac $ CONJUNCT1 evaluate_add_choices
\\ disch_then (qspec_then ‘choices'’ assume_tac)
\\ fs state_eqs
\\ qexists_tac ‘choices + choices' - st2.fp_state.choices +1’ (* TODO *)
\\ qexists_tac ‘st2 with
fp_state :=
st2.fp_state with <|opts := (λx. []); choices := choices'|>’
\\ rpt conj_tac
\\ fs state_eqs
\\ fs[rwAllWordTree_def]
\\ irule rwAllWordTree_chaining_exact
\\ qexists_tac ‘Fp_bop v1 w1 w2’ \\ conj_tac
\\ TRY (irule rwAllWordTree_chaining_exact \\ fs[fp_bop_def]
\\ fp_inv_tac \\ NO_TAC)
\\ TOP_CASE_TAC \\ fs[fp_bop_def, rwAllWordTree_def, fp_translate_def]
\\ rveq \\ fs[fp_translate_def] \\ fp_inv_tac)
>- (
fs[quantHeuristicsTheory.LENGTH_TO_EXISTS_CONS] \\ rpt strip_tac \\ rveq
\\ qpat_x_assum `evaluate _ _ _ = _` mp_tac
\\ simp[SimpL “$==>”, evaluate_def, CaseEq"option",
astTheory.getOpClass_def, do_app_def, CaseEq"result", evaluate_case_case]
\\ ntac 6 (TOP_CASE_TAC \\ fs[])
\\ imp_res_tac evaluate_sing \\ rveq \\ gs[]
\\ simp[SimpL “$==>”, CaseEq"option", CaseEq"prod", CaseEq"v", astTheory.isFpBool_def]
\\ rpt strip_tac
\\ rveq
\\ rename [‘evaluate st1 env [e3] = (st2, _)’,
‘evaluate st2 env [e2] = (st3, _)’,
‘evaluate st3 env [e1] = (st4, _)’]
\\ ‘st2.fp_state.canOpt = FPScope Opt’ by fp_inv_tac
\\ ‘st3.fp_state.canOpt = FPScope Opt’ by fp_inv_tac
\\ ‘st4.fp_state.canOpt = FPScope Opt’ by fp_inv_tac
\\ gs[]
\\ first_assum (fn ith => qpat_x_assum `evaluate _ _ _ = _`
(fn th => mp_then Any mp_tac ith th))
\\ rpt (disch_then drule)
\\ disch_then (qspec_then ‘w1’ mp_tac) \\ impl_tac \\ fs[]
\\ strip_tac
\\ first_assum (fn ith => qpat_x_assum `evaluate _ _ [e2] = _`
(fn th => mp_then Any mp_tac ith th))
\\ rpt (disch_then drule)
\\ disch_then (qspec_then ‘w2’ mp_tac) \\ impl_tac \\ fs[]
\\ strip_tac
\\ first_assum (fn ith => qpat_x_assum `evaluate _ _ [e3] = _`
(fn th => mp_then Any mp_tac ith th))
\\ rpt (disch_then drule)
\\ disch_then (qspec_then ‘w3’ mp_tac) \\ impl_tac \\ fs[]
\\ strip_tac
\\ fs ([shift_fp_opts_def] @ state_eqs)
\\ fs[PULL_EXISTS]
\\ qexists_tac ‘FP_WordTree (fp_top v2 fpUnOpt fpUnOpt' fpUnOpt'')’
\\ qexists_tac ‘fp_top v2 fpUnOpt fpUnOpt' fpUnOpt''’
\\ rpt (qpat_x_assum ‘do_fprw _ _ _ = _’ mp_tac)
\\ simp[fp_translate_def, do_fprw_def, CaseEq"option"] \\ rveq
\\ rpt strip_tac
\\ first_x_assum $ mp_then Any mp_tac rwAllWordTree_comp_terop_r
\\ first_x_assum $ mp_then Any mp_tac rwAllWordTree_comp_terop_c
\\ first_x_assum $ mp_then Any mp_tac rwAllWordTree_comp_terop_l
\\ disch_then $ qspecl_then [‘fpUnOpt'’, ‘fpUnOpt''’, ‘v2’]
$ qx_choose_then ‘sched1’ assume_tac
\\ disch_then $ qspecl_then [‘w1’, ‘fpUnOpt''’, ‘v2’]
$ qx_choose_then ‘sched2’ assume_tac
\\ disch_then $ qspecl_then [‘w1’, ‘w2’, ‘v2’]
$ qx_choose_then ‘sched3’ assume_tac
\\ rveq \\ gs[]
\\ qexists_tac ‘sched1 ++ sched2 ++ sched3 ++
(case do_fprw (Rval (FP_WordTree (fp_top v2 w1 w2 w3)))
(st4.fp_state.opts 0) st4.fp_state.rws of
| NONE => []
| SOME _ => (st4.fp_state.opts 0))’
\\ simp[evaluate_def, astTheory.getOpClass_def, do_app_def]
\\ qpat_x_assum `evaluate _ _ [e2] = _` $ mp_then Any mp_tac $ CONJUNCT1 evaluate_add_choices
\\ disch_then (qspec_then ‘choices''’ assume_tac)
\\ fs state_eqs
\\ qpat_x_assum `evaluate _ _ [e1] = _` $ mp_then Any mp_tac $ CONJUNCT1 evaluate_add_choices
\\ disch_then (qspec_then ‘choices' + choices'' - st2.fp_state.choices’ assume_tac)
\\ fs state_eqs \\ rewrite_tac [CONJ_ASSOC]
\\ conj_tac
>- (conj_tac
\\ fs[shift_fp_opts_def, do_fprw_def, astTheory.isFpBool_def, rwAllWordTree_def])
\\ irule rwAllWordTree_chaining_exact
\\ qexists_tac ‘Fp_top v2 w1 w2 w3’ \\ conj_tac
>- (irule rwAllWordTree_chaining_exact
\\ qexists_tac ‘fp_top v2 w1 w2 fpUnOpt''’ \\ fs[fp_top_def]
\\ irule rwAllWordTree_chaining_exact
\\ qexists_tac ‘fp_top v2 w1 fpUnOpt' fpUnOpt''’ \\ fs[fp_top_def]
\\ ‘st1.fp_state.rws = st3.fp_state.rws ∧ st2.fp_state.rws = st1.fp_state.rws’
by fp_inv_tac
\\ gs[])
\\ TOP_CASE_TAC \\ fs[fp_top_def, rwAllWordTree_def, fp_translate_def]
\\ rveq \\ fs[do_fprw_def, CaseEq"option"] \\ rveq
\\ fs[fp_translate_def] \\ rveq \\ fp_inv_tac)
>- (fs[])
\\ fs[]
>- (rveq \\ res_tac \\ gs[isFpArithExp_def, isPureExp_def])
\\ fs[isFpArithExp_def, isPureExp_def]
QED
(**
Optimisation simulation proofs
In combination with the automation from icing_optimisationsLib and the
correctness proofs from source_to_source2Proofs, we automatically
construct backwards simulation proofs for a run of the optimiser
**)
Theorem fp_times_zero_correct:
∀ st1 st2 env e r.
is_rewriteFPexp_correct [fp_times_zero] st1 st2 env e r
Proof
rw[is_rewriteFPexp_correct_def]
\\ REVERSE (qspecl_then [`e`] strip_assume_tac fp_times_zero_cases)
>- (
fs[]
\\ extend_eval_tac ‘evaluate st1 _ _ = _’ ‘[fp_times_zero]’
\\ strip_tac
\\ pop_assum (mp_then Any mp_tac (CONJUNCT1 evaluate_add_choices))
\\ disch_then (qspec_then ‘st1.fp_state.choices’ assume_tac)
\\ fsrw_tac [SATISFY_ss] [])
\\ qpat_x_assum `evaluate _ _ _ = _` mp_tac
\\ simp[SimpL “$==>”, evaluate_def, astTheory.getOpClass_def, astTheory.isFpBool_def, do_app_def]
\\ rpt strip_tac \\ rveq
\\ simp[evaluate_def, astTheory.getOpClass_def, astTheory.isFpBool_def, do_app_def, evaluate_case_case]
\\ ‘isFpArithExp e1’ by (gs[isFpArithExp_def])
\\ drule (CONJUNCT1 icing_rewriterProofsTheory.isFpArithExp_matched_evaluates)
\\ disch_then $ qspecl_then [‘env’, ‘st1’] mp_tac
\\ impl_tac
>- gs[freeVars_fp_bound_def]
\\ rpt strip_tac \\ rveq
\\ ‘st2.fp_state.canOpt = FPScope Opt’ by fp_inv_tac
\\ ‘st2 = st1 with fp_state := st2.fp_state’
by (imp_res_tac isPureExp_same_ffi \\ fs[isPureExp_def]
\\ res_tac
\\ fs[state_component_equality, shift_fp_opts_def, CaseEq"option", CaseEq"v"])
\\ qpat_x_assum ‘evaluate _ _ [e1] = _’ $ mp_then Any mp_tac isPureExp_evaluate_change_oracle
\\ disch_then $ qspecl_then [‘fp_times_zero’, ‘st1’,
‘λ x.
if (x = 0)
then [RewriteApp Here (LENGTH st1.fp_state.rws + 1)]
else []’] mp_tac
\\ impl_tac
>- (rpt conj_tac \\ gs[isPureExp_def])
\\ rpt strip_tac
\\ pop_assum mp_tac
\\ qmatch_goalsub_abbrev_tac ‘evaluate st1N _ [e1] = _’
\\ strip_tac
\\ qexists_tac ‘oracle’ \\ qexists_tac ‘st1.fp_state.choices’
\\ qmatch_goalsub_abbrev_tac ‘evaluate st1Upd _ [e1]’
\\ ‘st1N = st1Upd’
by (unabbrev_all_tac \\ gs[state_component_equality, fpState_component_equality])
\\ rveq
\\ gs[fp_translate_def, shift_fp_opts_def, state_component_equality,
fpState_component_equality, do_fprw_def]
\\ simp[do_fprw_def, rwAllWordTree_def, nth_len]
\\ simp[EVAL “rwFp_pathWordTree fp_times_zero Here
(fp_bop FP_Mul fp (Fp_const 0w))”,
instWordTree_def, substLookup_def]
QED
Theorem fp_same_sub_correct:
∀ st1 st2 env e r.
is_rewriteFPexp_correct [fp_same_sub] st1 st2 env e r
Proof
rw[is_rewriteFPexp_correct_def]
\\ REVERSE (qspecl_then [`e`] strip_assume_tac fp_same_sub_cases)
>- (
fs[]
\\ extend_eval_tac ‘evaluate st1 _ _ = _’ ‘[fp_same_sub]’
\\ strip_tac
\\ pop_assum (mp_then Any mp_tac (CONJUNCT1 evaluate_add_choices))
\\ disch_then (qspec_then ‘st1.fp_state.choices’ assume_tac)
\\ fsrw_tac [SATISFY_ss] [])
\\ rveq \\ pop_assum $ gs o single
\\ qpat_x_assum `evaluate _ _ _ = _` mp_tac
\\ simp[SimpL “$==>”, evaluate_def, astTheory.getOpClass_def, astTheory.isFpBool_def, do_app_def]
\\ rpt strip_tac \\ rveq
\\ simp[evaluate_def, astTheory.getOpClass_def, astTheory.isFpBool_def, do_app_def, evaluate_case_case]
\\ ‘isFpArithExp e1’ by (gs[isFpArithExp_def])
\\ drule (CONJUNCT1 icing_rewriterProofsTheory.isFpArithExp_matched_evaluates)
\\ disch_then $ qspecl_then [‘env’, ‘st1’] mp_tac
\\ impl_tac
>- gs[freeVars_fp_bound_def]
\\ rpt strip_tac \\ rveq
\\ ‘st2.fp_state.canOpt = FPScope Opt ∧ ~st2.fp_state.real_sem’ by fp_inv_tac
\\ ‘st2 = st1 with fp_state := st2.fp_state’
by (imp_res_tac isPureExp_same_ffi \\ fs[isPureExp_def]
\\ res_tac
\\ fs[state_component_equality, shift_fp_opts_def, CaseEq"option", CaseEq"v"])
\\ qpat_assum `evaluate _ _ [e1] = _` $ mp_then Any mp_tac isPureExp_evaluate_change_oracle
\\ disch_then $ qspecl_then [‘fp_same_sub’, ‘st1’] mp_tac
\\ gs[GSYM PULL_FORALL]
\\ impl_tac >- gs[isPureExp_def]
\\ strip_tac
\\ first_assum $ qspec_then ‘λ x.
if (x = 0)
then [RewriteApp Here (LENGTH st1.fp_state.rws + 1)]
else []’ strip_assume_tac
\\ first_x_assum $ qspec_then ‘oracle’ strip_assume_tac
\\ gs[state_component_equality, fpState_component_equality]
\\ pop_assum mp_tac
\\ qmatch_goalsub_abbrev_tac ‘evaluate st1N _ _ = _’ \\ strip_tac
\\ qexists_tac ‘st1N.fp_state.opts’ \\ qexists_tac ‘st1.fp_state.choices’
\\ qmatch_goalsub_abbrev_tac ‘evaluate st1Upd _ _’
\\ ‘st1N = st1Upd’
by (unabbrev_all_tac \\ gs[state_component_equality, fpState_component_equality])
\\ rveq \\ gs[]
\\ qpat_x_assum ‘evaluate st1N _ _ = _’ kall_tac
\\ qpat_x_assum ‘evaluate _ _ _ = _’ $ mp_then Any mp_tac $ CONJUNCT1 evaluate_add_choices
\\ disch_then $ qspec_then ‘st1.fp_state.choices + (st2.fp_state.choices - st1.fp_state.choices)’ mp_tac
\\ gs[state_component_equality, fpState_component_equality]
\\ strip_tac
\\ gs[fp_translate_def, shift_fp_opts_def, state_component_equality, fpState_component_equality]
\\ simp[do_fprw_def, rwAllWordTree_def, nth_len]
\\ simp[EVAL “rwFp_pathWordTree fp_same_sub Here
(fp_bop FP_Sub fp fp)”,
instWordTree_def, substLookup_def]
QED
Theorem fp_add_sub_correct:
∀ st1 st2 env e r.
is_rewriteFPexp_correct [fp_add_sub] st1 st2 env e r
Proof
rw[is_rewriteFPexp_correct_def]
\\ qspecl_then [`e`] strip_assume_tac
(ONCE_REWRITE_RULE [DISJ_COMM] fp_add_sub_cases)
>- (
fs[]
\\ extend_eval_tac ‘evaluate st1 _ _ = _’ ‘[fp_add_sub]’
\\ strip_tac
\\ pop_assum (mp_then Any mp_tac (CONJUNCT1 evaluate_add_choices))
\\ disch_then (qspec_then ‘st1.fp_state.choices’ assume_tac)
\\ fsrw_tac [SATISFY_ss] [])
\\ rveq \\ fs[]
\\ qpat_x_assum `_ = (_, _)` (mp_tac o SIMP_RULE std_ss [evaluate_def])
\\ simp[REVERSE_DEF, astTheory.isFpBool_def, Once evaluate_cons,
evaluate_case_case]
\\ ntac 4 (TOP_CASE_TAC \\ fs[])
\\ imp_res_tac evaluate_sing \\ rveq \\ fs[]
\\ simp[do_app_def, CaseEq"option", CaseEq"v", CaseEq"prod"]
\\ rpt strip_tac \\ rveq \\ fs[] \\ rveq
\\ rename [‘evaluate st1 env [e1] = (st2, Rval [v1])’,
‘evaluate st2 env [e2] = (st3, Rval [v2])’]
\\ ‘st3.fp_state.canOpt = FPScope Opt’ by fp_inv_tac
\\ fs[]
\\ ‘st2 = st1 with fp_state := st2.fp_state ∧
st3 = st1 with fp_state := st3.fp_state’
by (imp_res_tac isPureExp_same_ffi \\ fs[isPureExp_def]
\\ res_tac \\ fs[state_component_equality])
\\ qpat_assum `evaluate _ _ [e2] = _`
(mp_then Any mp_tac isPureExp_evaluate_change_oracle)
\\ fs[isPureExp_def]
\\ disch_then (
qspecl_then [
‘fp_add_sub’,
‘st1 with fp_state := st1.fp_state with choices :=
st1.fp_state.choices + (st3.fp_state.choices - st2.fp_state.choices)’,
‘λ x. if (x = 0)
then [RewriteApp Here (LENGTH st1.fp_state.rws + 1)] ++
(case do_fprw (Rval (FP_WordTree (fp_bop FP_Sub w1 w2)))
(st3.fp_state.opts 0) st3.fp_state.rws of
| NONE => [] | SOME r_opt => st3.fp_state.opts x)
else []’] mp_tac)
\\ impl_tac >- fp_inv_tac
\\ strip_tac
\\ qpat_assum `evaluate _ _ [e1] = _`
(mp_then Any mp_tac isPureExp_evaluate_change_oracle)
\\ fs[isPureExp_def]
\\ disch_then (
qspecl_then [
‘fp_add_sub’,
‘st1’, ‘λ x . if x = 0 then [] else oracle (x-1)’] mp_tac)
\\ impl_tac >- fp_inv_tac
\\ strip_tac
\\ ‘st2.fp_state.rws = st1.fp_state.rws’ by fp_inv_tac
\\ pop_assum (fs o single)
\\ pop_assum (mp_then Any mp_tac (CONJUNCT1 evaluate_add_choices))
\\ disch_then (qspec_then ‘st1.fp_state.choices’ assume_tac)
\\ qexists_tac ‘oracle'’ \\ qexists_tac ‘st1.fp_state.choices’
\\ simp[evaluate_def]
\\ simp[REVERSE_DEF, astTheory.getOpClass_def, astTheory.isFpBool_def,
Once evaluate_cons, evaluate_case_case]
\\ fs state_eqs
\\ simp([do_app_def, shift_fp_opts_def] @ state_eqs)
\\ simp[Once do_fprw_def, rwAllWordTree_def]
\\ qpat_x_assum `evaluate _ _ [e2] = _` $ mp_then Any mp_tac (CONJUNCT1 evaluate_add_choices)
\\ disch_then $ qspec_then ‘st1.fp_state.choices + (st2.fp_state.choices - st1.fp_state.choices) + 1’ assume_tac
\\ gs state_eqs \\ pop_assum mp_tac
\\ qmatch_goalsub_abbrev_tac ‘evaluate st1Upd _ _ = _’ \\ rpt strip_tac
\\ qmatch_goalsub_abbrev_tac ‘evaluate st1New _ _’
\\ ‘st1Upd = st1New’ by (unabbrev_all_tac \\ gs (FUN_EQ_THM :: state_eqs))
\\ pop_assum $ gs o single
\\ gs (fp_translate_def :: state_eqs) \\ unabbrev_all_tac
\\ rpt conj_tac
>- fp_inv_tac
>- fp_inv_tac
\\ qpat_x_assum `_ = Rval _` (fs o single o GSYM)
\\ simp[do_fprw_def, rwAllWordTree_def, nth_len]
\\ simp[EVAL ``rwFp_pathWordTree (fp_add_sub) Here (fp_bop FP_Add w1 (fp_uop FP_Neg w2))``,
instWordTree_def, substLookup_def]
\\ Cases_on `rwAllWordTree (st3.fp_state.opts 0) st3.fp_state.rws (fp_bop FP_Sub w1 w2)`
\\ fs[rwAllWordTree_def, fpValTreeTheory.fp_bop_def]
\\ imp_res_tac rwAllWordTree_append_opt
\\ first_x_assum (qspec_then `[fp_add_sub]` assume_tac)
\\ `st3.fp_state.rws = st1.fp_state.rws` by fp_inv_tac
\\ fs[]
QED
Theorem fp_add_sub_correct_unfold =
REWRITE_RULE [fp_add_sub_def, reverse_tuple_def, fp_sub_add_def] fp_add_sub_correct;
Theorem fp_neg_times_minus_one_correct:
∀ st1 st2 env e r.
is_rewriteFPexp_correct [fp_neg_times_minus_one] st1 st2 env e r
Proof
rw[is_rewriteFPexp_correct_def]
\\ REVERSE (qspecl_then [`e`] strip_assume_tac fp_neg_times_minus_one_cases)
>- (
fs[]
\\ extend_eval_tac ‘evaluate st1 _ _ = _’ ‘[fp_neg_times_minus_one]’
\\ strip_tac
\\ pop_assum (mp_then Any mp_tac (CONJUNCT1 evaluate_add_choices))
\\ disch_then (qspec_then ‘st1.fp_state.choices’ assume_tac)
\\ fsrw_tac [SATISFY_ss] [])
\\ gs[]
\\ imp_res_tac evaluate_sing
\\ pop_assum (fs o single)
\\ ‘∃ fp. v = FP_WordTree fp’
by (fs[freeVars_fp_bound_def]
\\ mp_tac (GEN_ALL icing_rewriterProofsTheory.rewriteFPexp_returns_fp)
\\ disch_then $ qspecl_then [‘st1’, ‘st2’, ‘e’, ‘FST(fp_neg_times_minus_one)’,
‘SND(fp_neg_times_minus_one)’, ‘env’,
‘App (FP_bop FP_Mul) [e1; App FpFromWord [Lit (Word64 0xBFF0000000000000w)]]’,
‘v’]
mp_tac
\\ impl_tac \\ gs[isFpArithExp_def, isPureExp_def])
\\ rveq
\\ qpat_x_assum ‘evaluate _ _ _ = _’ mp_tac
\\ simp[REVERSE_DEF, astTheory.getOpClass_def, astTheory.isFpBool_def,
Once evaluate_def, Once evaluate_cons, evaluate_case_case]
\\ ntac 2 (simp[Once evaluate_def, astTheory.isFpBool_def, astTheory.getOpClass_def, do_app_def])
\\ ntac 2 (TOP_CASE_TAC \\ fs[])
\\ imp_res_tac evaluate_sing \\ rveq
\\ fs[do_app_def] \\ Cases_on ‘fp_translate v’ \\ gs[]
\\ Cases_on ‘x’ \\ gs[fp_translate_def]
\\ ‘q.fp_state.canOpt = FPScope Opt’ by fp_inv_tac
\\ gs[] \\ rpt strip_tac \\ rveq
\\ rename1 ‘evaluate _ env [e1] = (st2, Rval [v])’
\\ ‘~ st2.fp_state.real_sem’ by fp_inv_tac
\\ ‘st1.fp_state.rws = st2.fp_state.rws’ by fp_inv_tac
\\ ‘st2 = st1 with fp_state := st2.fp_state’
by (imp_res_tac isPureExp_same_ffi \\ fs[isPureExp_def]
\\ res_tac
\\ fs[state_component_equality, shift_fp_opts_def, CaseEq"option", CaseEq"v"])
\\ ntac 2(simp[REVERSE_DEF, astTheory.getOpClass_def, astTheory.isFpBool_def,
Once evaluate_def, Once evaluate_cons,
evaluate_case_case, do_app_def])
\\ qpat_assum `evaluate _ _ [e1] = _`
(mp_then Any mp_tac isPureExp_evaluate_change_oracle)
\\ fs[isPureExp_def]
\\ disch_then (
qspecl_then [
‘fp_neg_times_minus_one’,
‘st1 with fp_state := st1.fp_state with choices :=
st1.fp_state.choices’,
‘λ x. if (x = 0)
then [RewriteApp Here (LENGTH st1.fp_state.rws + 1)] ++
(case do_fprw
(Rval
(FP_WordTree (fp_bop FP_Mul f (Fp_const 0xBFF0000000000000w))))
(st2.fp_state.opts 0) st2.fp_state.rws of
| NONE => [] | SOME r_opt => st2.fp_state.opts x)
else []’] mp_tac)
\\ impl_tac >- fp_inv_tac
\\ strip_tac \\ gs state_eqs
\\ qexists_tac ‘oracle’ \\ qexists_tac ‘st1.fp_state.choices’
\\ gs[shift_fp_opts_def]
\\ simp state_eqs
\\ simp[do_fprw_def, rwAllWordTree_def, nth_len]
\\ simp[EVAL ``rwFp_pathWordTree fp_neg_times_minus_one Here
(fp_uop FP_Neg f)``,
instWordTree_def, substLookup_def]
\\ fs[do_fprw_def, CaseEq"option"] \\ rveq
\\ gs[rwAllWordTree_def, fp_bop_def]
\\ imp_res_tac rwAllWordTree_append_opt
\\ first_x_assum (qspec_then `[fp_neg_times_minus_one]` assume_tac)
\\ gs[]
QED
Theorem fp_neg_times_minus_one_correct_unfold =
REWRITE_RULE [fp_neg_times_minus_one_def, reverse_tuple_def,
fp_times_minus_one_neg_def] fp_neg_times_minus_one_correct;
Theorem fp_distribute_gen_correct:
∀ fpBop1 fpBop2 st1 st2 env e r.
is_rewriteFPexp_correct [fp_distribute_gen fpBop1 fpBop2] st1 st2 env e r
Proof
rw[is_rewriteFPexp_correct_def]
\\ reverse (qspecl_then [‘e’, ‘fpBop1’, ‘fpBop2’] strip_assume_tac fp_distribute_gen_cases)
>- (
fs[]
\\ extend_eval_tac ‘evaluate st1 _ _ = _’ ‘[fp_distribute_gen fpBop1 fpBop2]’
\\ strip_tac
\\ pop_assum (mp_then Any mp_tac (CONJUNCT1 evaluate_add_choices))
\\ disch_then (qspec_then ‘st1.fp_state.choices’ assume_tac)
\\ fsrw_tac [SATISFY_ss] [])
\\ rveq \\ gs[]
\\ qpat_x_assum ‘evaluate _ _ _ = (_, _)’ mp_tac
\\ simp[SimpL “$==>”, evaluate_def, REVERSE_DEF, astTheory.getOpClass_def,
astTheory.isFpBool_def, Once evaluate_cons, evaluate_case_case,
CaseEq"result", CaseEq"prod"]
\\ rpt strip_tac \\ rveq \\ gs[]
\\ imp_res_tac evaluate_sing \\ rveq \\ gs[] \\ rveq
\\ ntac 2 (qpat_x_assum ‘case do_app _ _ _ of |_ => _’mp_tac)
\\ simp[do_app_def, Once (CaseEq"option"), CaseEq"prod"]
\\ ntac 2 (simp [Once (CaseEq"option"), CaseEq"v"])
\\ strip_tac \\ rveq \\ gs[]
\\ ntac 3 (simp [Once (CaseEq"option"), CaseEq"v", CaseEq"prod"])
\\ strip_tac \\ rveq \\ gs (shift_fp_opts_def :: state_eqs)
\\ rename [‘evaluate st1 env [e2] = (st2, Rval [v2])’,
‘evaluate st2 env [e3] = (st3, Rval [v3])’,
‘evaluate st3 env [e1] = (st4, Rval [v1])’]
\\ ‘st4.fp_state.canOpt = FPScope Opt’ by fp_inv_tac
\\ ‘st4.fp_state.rws = st1.fp_state.rws’ by fp_inv_tac
\\ ‘~ st4.fp_state.real_sem’ by fp_inv_tac
\\ gs[]
\\ ‘st2 = st1 with fp_state := st2.fp_state ∧
st3 = st1 with fp_state := st3.fp_state ∧
st4 = st1 with fp_state := st4.fp_state’ by (
imp_res_tac isPureExp_same_ffi
\\ fs[isPureExp_def]
\\ res_tac
\\ fs[state_component_equality])
\\ ntac 3 (qpat_x_assum ‘evaluate _ _ _ = _’ $ mp_then Any mp_tac (CONJUNCT1 evaluate_rewrite_hoisting))
\\ rename1 ‘fp_translate v3 = SOME (FP_WordTree w3)’
\\ rename1 ‘fp_translate v2 = SOME (FP_WordTree w2)’
\\ rename1 ‘fp_translate v1 = SOME (FP_WordTree w1)’
\\ disch_then $ qspec_then ‘w2’ mp_tac \\ impl_tac
>- (fp_inv_tac \\ fs[isPureExp_def, isFpArithExp_def])
\\ strip_tac
\\ disch_then $ qspec_then ‘w3’ mp_tac \\ impl_tac
>- (fp_inv_tac \\ fs[isPureExp_def, isFpArithExp_def])
\\ strip_tac
\\ disch_then $ qspec_then ‘w1’ mp_tac \\ impl_tac
>- (fp_inv_tac \\ fs[isPureExp_def, isFpArithExp_def])
\\ strip_tac
(* Construct a new rewrite schedule that we apply at the end *)
\\ rveq \\ gs[]
\\ rename1 ‘do_fprw (Rval (FP_WordTree fpUnOpt1)) _ st3.fp_state.rws = SOME (Rval (FP_WordTree w1))’
\\ rename1 ‘do_fprw (Rval (FP_WordTree fpUnOpt2)) _ _ = SOME (Rval (FP_WordTree w2))’
\\ rename1 ‘do_fprw (Rval (FP_WordTree fpUnOpt3)) _ _ = SOME (Rval (FP_WordTree w3))’
\\ rpt (qpat_x_assum ‘do_fprw _ _ _ = SOME _’ mp_tac)
\\ simp[do_fprw_def, CaseEq"option"] \\ rpt strip_tac
\\ qpat_x_assum `rwAllWordTree _ _ fpUnOpt1 = _` $ mp_then Any mp_tac rwAllWordTree_comp_left
\\ disch_then (qspecl_then [‘fpBop2’, ‘fpUnOpt3’] mp_tac)
\\ qmatch_goalsub_abbrev_tac ‘rwAllWordTree sched1 _ _ = _’
\\ qpat_x_assum `rwAllWordTree _ _ fpUnOpt3 = _` $ mp_then Any mp_tac rwAllWordTree_comp_right
\\ disch_then (qspecl_then [‘fpBop2’, ‘w1’] mp_tac)
\\ qmatch_goalsub_abbrev_tac ‘rwAllWordTree sched3 _ _ = _’
\\ rpt strip_tac
\\ ‘rwAllWordTree (sched1 ++ sched3 ++ case do_fprw (Rval (FP_WordTree (fp_bop fpBop2 w1 w3)))
(st4.fp_state.opts 0) st1.fp_state.rws of
| NONE => []
| SOME _ => st4.fp_state.opts 0) st1.fp_state.rws (Fp_bop fpBop2 fpUnOpt1 fpUnOpt3) =SOME w1'’
by (irule rwAllWordTree_chaining_exact
\\ qexists_tac ‘Fp_bop fpBop2 w1 w3’ \\ conj_tac
>- (irule rwAllWordTree_chaining_exact
\\ ‘st1.fp_state.rws = st2.fp_state.rws ∧
st1.fp_state.rws = st3.fp_state.rws’
by fp_inv_tac
\\ qexists_tac ‘Fp_bop fpBop2 w1 fpUnOpt3’ \\ gs[])
\\ fs[do_fprw_def, CaseEq"option", rwAllWordTree_def] \\ rveq
\\ fs[fp_translate_def] \\ rveq \\ fs[fp_bop_def])
\\ pop_assum $ mp_then Any mp_tac rwAllWordTree_comp_left
\\ disch_then $ qspecl_then [‘fpBop1’, ‘fpUnOpt2’] mp_tac
\\ qmatch_goalsub_abbrev_tac ‘rwAllWordTree sched_comb _ _’
\\ strip_tac
\\ qpat_x_assum ‘rwAllWordTree _ _ fpUnOpt2 = _’ $ mp_then Any mp_tac rwAllWordTree_comp_right
\\ disch_then $ qspecl_then [‘fpBop1’, ‘w1'’] mp_tac
\\ qmatch_goalsub_abbrev_tac ‘rwAllWordTree sched2 _ _’ \\ strip_tac
\\ rpt (qpat_x_assum ‘evaluate _ _ _ = _’ (fn th => mp_then Any mp_tac (CONJUNCT1 evaluate_fp_opts_inv) th \\ mp_tac th))
\\ simp state_eqs \\ rpt strip_tac
\\ simp[evaluate_def, astTheory.getOpClass_def, astTheory.isFpBool_def,
Once evaluate_cons, evaluate_case_case]
\\ qpat_assum `evaluate _ _ [e1] = _`
(mp_then Any mp_tac isPureExp_evaluate_change_oracle)
\\ fs[isPureExp_def]
\\ disch_then (
qspecl_then [
‘fp_distribute_gen fpBop1 fpBop2’,
‘st1 with fp_state := st1.fp_state with choices := st1.fp_state.choices’,
‘λ x. if (x = 1) then
([RewriteApp Here (LENGTH st1.fp_state.rws + 1)] ++ sched_comb ++ sched2 ++
case do_fprw (Rval (FP_WordTree (fp_bop fpBop1 w1' w2)))
(st4.fp_state.opts 1) st4.fp_state.rws of
| NONE => []