/
linear_scanProofScript.sml
6243 lines (5914 loc) · 244 KB
/
linear_scanProofScript.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
(*
Proves correctness of the linear-scan register allocator.
*)
open preamble sptreeTheory reg_allocTheory linear_scanTheory reg_allocProofTheory libTheory
open ml_monadBaseTheory ml_monadBaseLib;
val _ = temp_delsimps ["lift_disj_eq", "lift_imp_disj"]
val _ = new_theory "linear_scanProof"
val _ = disable_tyabbrev_printing "type_ident"
val _ = disable_tyabbrev_printing "alist"
val _ = ParseExtras.temp_tight_equality();
val _ = monadsyntax.temp_add_monadsyntax()
Overload monad_bind[local] = ``st_ex_bind``
Overload monad_unitbind[local] = ``\x y. st_ex_bind x (\z. y)``
Overload monad_ignore_bind[local] = ``\x y. st_ex_bind x (\z. y)``
Overload return[local] = ``st_ex_return``
val _ = hide "state";
(* TODO: clean up this file: e.g., move things upstream *)
Theorem set_MAP_FST_toAList_eq_domain:
!s. set (MAP FST (toAList s)) = domain s
Proof
rw [EXTENSION, MEM_MAP, EXISTS_PROD, MEM_toAList, domain_lookup]
QED
Theorem numset_list_insert_FOLDL:
!l live. numset_list_insert l live = FOLDL (\live x. insert x () live) live l
Proof
Induct_on `l` >> rw [numset_list_insert_def]
QED
Theorem numset_list_insert_nottailrec_FOLDR:
!l live. numset_list_insert_nottailrec l live = FOLDR (\x live. insert x () live) live l
Proof
Induct_on `l` >> rw [numset_list_insert_nottailrec_def]
QED
Theorem both_numset_list_insert_equal:
!l live.
numset_list_insert l live = numset_list_insert_nottailrec (REVERSE l) live
Proof
rw [numset_list_insert_FOLDL, numset_list_insert_nottailrec_FOLDR, FOLDR_FOLDL_REVERSE]
QED
Theorem domain_numset_list_insert:
!l s. domain (numset_list_insert l s) = set l UNION domain s
Proof
Induct_on `l` >>
rw [numset_list_insert_def] >>
metis_tac [numset_list_insert_def, INSERT_UNION_EQ, UNION_COMM]
QED
(* why breaking encapsulation like this? To get rid of the assumption `wf s` *)
Theorem lookup_insert_id:
!x (y:unit) s. lookup x s = SOME () ==> s = insert x () s
Proof
recInduct insert_ind >>
rw []
THEN1 (
fs [lookup_def] >>
once_rewrite_tac [insert_def] >>
simp []
)
THEN1 (
fs [lookup_def] >>
Cases_on `EVEN k` >> fs [] >>
once_rewrite_tac [insert_def] >>
rw []
)
THEN1 (
fs [lookup_def] >>
Cases_on `EVEN k` >> fs [] >>
Cases_on `k=0` >> fs [] >>
once_rewrite_tac [insert_def] >>
rw []
)
QED
Theorem numset_list_insert_FILTER:
!l live.
numset_list_insert (FILTER (λx. lookup x live = NONE) l) live =
numset_list_insert l live
Proof
sg `!x l live. lookup x live = SOME () ==> lookup x (numset_list_insert_nottailrec l live) = SOME ()` THEN1 (
Induct_on `l` >>
rw [numset_list_insert_nottailrec_def, lookup_insert]
) >>
rw [both_numset_list_insert_equal, GSYM FILTER_REVERSE] >>
qabbrev_tac `l' = REVERSE l` >>
qpat_x_assum `Abbrev _` kall_tac >>
Induct_on `l'` >>
rw [numset_list_insert_nottailrec_def] >>
Cases_on `lookup h live` >> fs [NOT_SOME_NONE] >>
simp [lookup_insert_id]
QED
Theorem domain_numset_list_delete:
!l s. domain (numset_list_delete l s) = (domain s) DIFF (set l)
Proof
Induct_on `l` >>
rw [numset_list_delete_def, DIFF_INSERT]
QED
Theorem check_partial_col_success_INJ_lemma:
!l live flive f.
domain flive = IMAGE f (domain live) /\
INJ f (domain live) UNIV /\
check_partial_col f l live flive = SOME (live',flive')
==>
INJ f (set l UNION domain live) UNIV
Proof
Induct_on `l` >> rw [] >>
fs [check_partial_col_def] >>
Cases_on `lookup h live` THEN1 (
Cases_on `lookup (f h) flive` >> fs [] >>
`domain (insert (f h) () flive) = IMAGE f (domain (insert h () live))` by metis_tac [domain_insert, IMAGE_INSERT] >>
sg `INJ f (domain (insert h () live)) UNIV` THEN1 (
rw [domain_insert, INJ_INSERT] >>
`(f y) IN (domain flive)` by rw [] >>
`(f h) NOTIN (domain flive)` by rw [domain_lookup, option_nchotomy] >>
metis_tac []
) >>
`INJ f (set l UNION domain (insert h () live)) UNIV` by metis_tac [] >>
metis_tac [INSERT_UNION_EQ, UNION_COMM, domain_insert]
) >>
fs [INSERT_UNION_EQ] >>
`h IN (domain live)` by rw [domain_lookup] >>
`h IN (set l) UNION (domain live)` by metis_tac [SUBSET_DEF, SUBSET_UNION] >>
`!(x : num) s. x IN s ==> x INSERT s = s` by metis_tac [INSERT_applied, IN_DEF, EXTENSION] >>
metis_tac []
QED
Theorem check_partial_col_success_INJ:
!l live flive f.
domain flive = IMAGE f (domain live) /\
INJ f (domain live) UNIV /\
check_partial_col f l live flive = SOME (live',flive')
==>
INJ f (set l UNION domain live) UNIV /\
INJ f (domain live') UNIV
Proof
rw [] >>
`domain live' = set l UNION domain live` by metis_tac [check_partial_col_domain, FST] >>
metis_tac [check_partial_col_success_INJ_lemma]
QED
Theorem check_partial_col_input_monotone:
!f live1 flive1 live2 flive2 l v.
IMAGE f (domain live1) = domain flive1 /\ IMAGE f (domain live2) = domain flive2 ==>
domain live1 SUBSET domain live2 ==>
INJ f (domain live2) UNIV ==>
check_partial_col f l live2 flive2 = SOME v ==>
?livein1 flivein1. check_partial_col f l live1 flive1 = SOME (livein1, flivein1)
Proof
rw [] >>
PairCases_on `v` >>
`INJ f (set l UNION domain live2) UNIV` by metis_tac [check_partial_col_success_INJ] >>
`set l UNION domain live1 SUBSET set l UNION domain live2` by fs [SUBSET_DEF] >>
`INJ f (set l UNION domain live1) UNIV` by metis_tac [INJ_SUBSET, UNIV_SUBSET] >>
metis_tac [check_partial_col_success]
QED
Theorem numset_list_delete_IMAGE:
!f l live flive v.
domain flive = IMAGE f (domain live) ==>
INJ f (domain live) UNIV ==>
check_partial_col f l live flive = SOME v ==>
domain (numset_list_delete (MAP f l) flive) = IMAGE f (domain (numset_list_delete l live))
Proof
rw [] >>
PairCases_on `v` >>
`INJ f (set l UNION domain live) UNIV` by metis_tac [check_partial_col_success_INJ] >>
rw [domain_numset_list_delete, EXTENSION] >>
eq_tac >> strip_tac
THEN1 (
fs [MEM_MAP] >>
metis_tac []
)
THEN1 (
strip_tac
THEN1 metis_tac [] >>
CCONTR_TAC >> fs [MEM_MAP, INJ_IFF] >>
metis_tac []
)
QED
Theorem check_partial_col_IMAGE:
!f l live flive live' flive'.
(domain flive) = IMAGE f (domain live) ==>
check_partial_col f l live flive = SOME (live', flive') ==>
(domain flive') = IMAGE f (domain live')
Proof
Induct_on `l` >>
fs [check_partial_col_def] >> rw [] >>
Cases_on`lookup h live` >>
Cases_on`lookup (f h) flive` >>
fs [] >>
`domain (insert (f h) () flive) = IMAGE f (domain (insert h () live))` by rw [domain_insert] >>
metis_tac []
QED
Theorem branch_domain:
!(live1 : num_set) (live2 : num_set).
set (MAP FST (toAList (difference live2 live1))) UNION domain live1 = domain live1 UNION domain live2
Proof
`!(live1 : num_set) (live2 : num_set). set (MAP FST (toAList (difference live2 live1))) = domain (difference live2 live1)` by rw [EXTENSION, MEM_MAP, MEM_toAList, EXISTS_PROD, domain_lookup] >>
`!(s : num -> bool) (t : num -> bool). t DIFF s UNION s = s UNION t` by (rw [EXTENSION] >> Cases_on `x IN t` >> rw []) >>
rw [domain_difference]
QED
Theorem check_partial_col_branch_domain:
!(live1 : num_set) (live2 : num_set) flive1 liveout fliveout.
check_partial_col f (MAP FST (toAList (difference live2 live1))) live1 flive1 = SOME (liveout, fliveout) ==>
domain liveout = domain live1 UNION domain live2
Proof
metis_tac [branch_domain, check_partial_col_domain, FST]
QED
Theorem check_partial_col_branch_comm:
!f live1 flive1 live2 flive2 a b.
INJ f (domain live1) UNIV ==>
domain flive1 = IMAGE f (domain live1) /\ domain flive2 = IMAGE f (domain live2) ==>
check_partial_col f (MAP FST (toAList (difference live2 live1))) live1 flive1 = SOME (a, b) ==>
?c d. check_partial_col f (MAP FST (toAList (difference live1 live2))) live2 flive2 = SOME (c, d)
Proof
rw [] >>
`domain a = domain live1 UNION domain live2` by metis_tac [check_partial_col_domain, branch_domain, FST] >>
`INJ f (domain live1 UNION domain live2) UNIV` by metis_tac [check_partial_col_success_INJ] >>
`set (MAP FST (toAList (difference live1 live2))) UNION domain live2 = domain live1 UNION domain live2` by metis_tac [UNION_COMM, domain_difference, branch_domain] >>
metis_tac [check_partial_col_success]
QED
Theorem check_partial_col_list_monotone:
!f live flive (s1 : num_set) (s2 : num_set) a b.
domain flive = IMAGE f (domain live) ==>
INJ f (domain live) UNIV ==>
domain s1 SUBSET domain s2 ==>
check_partial_col f (MAP FST (toAList s2)) live flive= SOME (a, b) ==>
?c d. check_partial_col f (MAP FST (toAList s1)) live flive = SOME (c, d)
Proof
rw [] >>
`!(s : num_set). set (MAP FST (toAList s)) = domain s` by rw [EXTENSION, MEM_MAP, MEM_toAList, EXISTS_PROD, domain_lookup] >>
`INJ f (domain a) UNIV` by metis_tac [check_partial_col_success_INJ] >>
`domain a = (domain s2 UNION domain live)` by metis_tac [check_partial_col_domain, FST] >>
`domain s1 UNION domain live SUBSET domain a` by fs [SUBSET_DEF] >>
`INJ f (domain s1 UNION domain live) UNIV` by metis_tac [INJ_SUBSET, UNIV_SUBSET] >>
metis_tac [check_partial_col_success]
QED
Theorem check_live_tree_success:
!lt live flive live' flive' f.
domain flive = IMAGE f (domain live) /\
INJ f (domain live) UNIV /\
check_live_tree f lt live flive = SOME (live',flive') ==>
domain flive' = IMAGE f (domain live') /\
INJ f (domain live') UNIV
Proof
Induct_on `lt`
(* Writes *)
THEN1 (
rw [check_live_tree_def] >>
fs [case_eq_thms]
THEN1 (
metis_tac [numset_list_delete_IMAGE]
)
THEN1 (
`domain live' SUBSET domain live` by metis_tac [domain_numset_list_delete, DIFF_SUBSET] >>
metis_tac [INJ_SUBSET, UNIV_SUBSET]
)
)
(* Reads *)
THEN1 (
rw [check_live_tree_def]
THEN1 metis_tac [check_partial_col_IMAGE]
THEN1 metis_tac [check_partial_col_success_INJ]
)
(* Branch *)
THEN1 (
rw [check_live_tree_def] >>
fs [case_eq_thms] >> rveq
THEN1 (
metis_tac [check_partial_col_IMAGE]
)
THEN1 (
`INJ f (domain livein1) UNIV` by metis_tac [] >>
`domain flivein1 = IMAGE f (domain livein1)` by metis_tac [check_partial_col_IMAGE] >>
metis_tac [check_partial_col_success_INJ]
)
)
(* Seq *)
THEN1 (
rw [check_live_tree_def] >>
fs [case_eq_thms] >> rveq >>
`domain flivein2 = IMAGE f (domain livein2)` by metis_tac [] >>
`INJ f (domain livein2) UNIV` by metis_tac [] >>
metis_tac []
)
QED
Theorem ALL_DISTINCT_INJ_MAP:
!f l. ALL_DISTINCT (MAP f l) ==> INJ f (set l) UNIV
Proof
Induct_on `l` >> rw [INJ_INSERT] >>
`MEM (f y) (MAP f l)` by metis_tac [MEM_MAP] >>
`~(MEM (f y) (MAP f l))` by metis_tac []
QED
Theorem check_col_output:
!f live live' flive'.
check_col f live = SOME (live', flive') ==>
domain flive' = IMAGE f (domain live') /\
INJ f (domain live') UNIV
Proof
rw [check_col_def]
THEN1 (
rw [EXTENSION, domain_fromAList, MEM_MAP] >>
rw [MEM_toAList, EXISTS_PROD, domain_lookup, PULL_EXISTS]
)
THEN1 (
`INJ f (set (MAP FST (toAList live))) UNIV` by metis_tac [ALL_DISTINCT_INJ_MAP, MAP_MAP_o] >>
`set (MAP FST (toAList live)) = domain live` by rw [EXTENSION, MEM_MAP, MEM_toAList, EXISTS_PROD, domain_lookup] >>
metis_tac []
)
QED
Theorem check_col_success:
!f live.
INJ f (domain live) UNIV ==>
?flive. check_col f live = SOME (live, flive)
Proof
rw [check_col_def] >>
sg `!x y. MEM x (MAP FST (toAList live)) /\ MEM y (MAP FST (toAList live)) /\ (f x = f y) ==> (x = y)` THEN1 (
rw [MEM_toAList, MEM_MAP, EXISTS_PROD] >>
`x IN domain live /\ y IN domain live` by rw [domain_lookup] >>
fs [INJ_DEF]
) >>
metis_tac [ALL_DISTINCT_MAP_INJ, MAP_MAP_o, ALL_DISTINCT_MAP_FST_toAList]
QED
Theorem check_clash_tree_output:
!f ct live flive livein flivein.
domain flive = IMAGE f (domain live) /\
INJ f (domain live) UNIV /\
check_clash_tree f ct live flive = SOME (livein, flivein) ==>
domain flivein = IMAGE f (domain livein) /\
INJ f (domain livein) UNIV
Proof
Induct_on `ct` >>
simp [check_clash_tree_def]
(* Delta *)
THEN1 (
rpt gen_tac >> strip_tac >>
fs [case_eq_thms] >>
`domain (numset_list_delete (MAP f l) flive) = IMAGE f (domain (numset_list_delete l live))` by metis_tac [numset_list_delete_IMAGE] >>
`domain (numset_list_delete l live) SUBSET (domain live)` by rw [domain_numset_list_delete] >>
`INJ f (domain (numset_list_delete l live)) UNIV` by metis_tac [INJ_SUBSET, UNIV_SUBSET] >>
strip_tac
THEN1 metis_tac [check_partial_col_IMAGE]
THEN1 metis_tac [check_partial_col_success_INJ]
)
(* Set *)
THEN1 (
rw [] >> metis_tac [check_col_output]
)
(* Branch *)
THEN1 (
rpt gen_tac >> strip_tac >>
fs [case_eq_thms] >> rveq
THEN1 (
`domain ft1_out = IMAGE f (domain t1_out)` by metis_tac [check_partial_col_IMAGE] >>
`INJ f (domain t1_out) UNIV` by metis_tac [check_partial_col_success_INJ] >>
strip_tac
THEN1 metis_tac [check_partial_col_IMAGE]
THEN1 metis_tac [check_partial_col_success_INJ]
)
THEN1 metis_tac [check_col_output]
)
(* Seq *)
THEN1 (
rpt gen_tac >> strip_tac >>
fs [case_eq_thms] >> rveq >>
`domain ft2_out = IMAGE f (domain t2_out) /\ INJ f (domain t2_out) UNIV` by metis_tac [] >>
metis_tac []
)
QED
Theorem get_live_tree_correct_lemma:
!f live flive live' flive' ct livein' flivein'.
IMAGE f (domain live) = domain flive /\ IMAGE f (domain live') = domain flive' ==>
INJ f (domain live') UNIV ==>
domain live SUBSET domain live' ==>
check_live_tree f (get_live_tree ct) live' flive' = SOME (livein', flivein') ==>
?livein flivein. check_clash_tree f ct live flive = SOME (livein, flivein) /\
domain livein SUBSET domain livein'
Proof
Induct_on `ct`
(* Delta *)
THEN1 (
rw [check_clash_tree_def, get_live_tree_def, check_live_tree_def] >>
qabbrev_tac `lrd = l0` >> qabbrev_tac `lwr = l` >> rpt (qpat_x_assum `Abbrev _` kall_tac) >>
fs [case_eq_thms] >> rveq >>
`?v. check_partial_col f lwr live flive = SOME v` by metis_tac [check_partial_col_input_monotone] >>
rw [] >>
`INJ f (domain live) UNIV` by metis_tac [INJ_SUBSET, UNIV_SUBSET] >>
`IMAGE f (domain (numset_list_delete lwr live)) = domain (numset_list_delete (MAP f lwr) flive)` by metis_tac [numset_list_delete_IMAGE] >>
`IMAGE f (domain (numset_list_delete lwr live')) = domain (numset_list_delete (MAP f lwr) flive')` by metis_tac [numset_list_delete_IMAGE] >>
sg `INJ f (domain (numset_list_delete lwr live')) UNIV` THEN1 (
rw [domain_numset_list_delete] >>
`(domain live' DIFF set lwr) SUBSET (domain live')` by fs [SUBSET_DIFF] >>
metis_tac [SUBSET_DIFF, INJ_SUBSET, UNIV_SUBSET]
) >>
`domain (numset_list_delete lwr live) SUBSET domain (numset_list_delete lwr live')` by fs [domain_numset_list_delete, SUBSET_DEF] >>
`?live1' flive1'. check_partial_col f lrd (numset_list_delete lwr live) (numset_list_delete (MAP f lwr) flive) = SOME (live1', flive1')` by metis_tac [check_partial_col_input_monotone] >>
rw [] >>
imp_res_tac check_partial_col_domain >>
fs [] >> fs [domain_numset_list_delete, SUBSET_DEF]
)
(* Set *)
THEN1 (
rw [check_clash_tree_def, get_live_tree_def, check_live_tree_def] >>
imp_res_tac check_partial_col_domain >> fs [] >>
imp_res_tac check_partial_col_success_INJ >> rfs [] >>
`set (MAP FST (toAList s)) = domain s` by rw [EXTENSION, MEM_MAP, EXISTS_PROD, MEM_toAList, domain_lookup] >>
fs [] >>
`domain s SUBSET domain s UNION domain live'` by fs [] >>
`INJ f (domain s) UNIV` by metis_tac [INJ_SUBSET, UNIV_SUBSET] >>
`?livein flivein. check_col f s = SOME (livein, flivein)` by metis_tac [check_col_success] >>
rw [] >>
fs [check_col_def]
)
(* Branch *)
THEN1 (
rw [check_clash_tree_def, get_live_tree_def, check_live_tree_def] >>
Cases_on `o'` >> fs [check_live_tree_def] >>
fs [case_eq_thms] >> rveq >>
(* Try to get consistent names *)
TRY (
qabbrev_tac `cutset = x` >>
qabbrev_tac `livemid = livein2'` >> qabbrev_tac `flivemid = flivein2'` >>
rpt (qpat_x_assum `Abbrev _` kall_tac)
) >>
qabbrev_tac `livein1' = livein1` >> qabbrev_tac `flivein1' = flivein1` >>
qabbrev_tac `livein2' = livein2` >> qabbrev_tac `flivein2' = flivein2` >>
rpt (qpat_x_assum `Abbrev _` kall_tac) >>
`?livein1 flivein1. check_clash_tree f ct live flive = SOME (livein1, flivein1) /\ domain livein1 SUBSET domain livein1'` by metis_tac [] >>
`?livein2 flivein2. check_clash_tree f ct' live flive = SOME (livein2, flivein2) /\ domain livein2 SUBSET domain livein2'` by metis_tac [] >>
rw []
THEN1 (
imp_res_tac check_live_tree_success >> rfs [] >>
`INJ f (set (MAP FST (toAList (difference livein2' livein1')))
UNION domain livein1') UNIV` by
metis_tac [check_partial_col_success_INJ] >>
`set (MAP FST (toAList (difference livein2 livein1)))
UNION domain livein1 SUBSET set
(MAP FST (toAList (difference livein2' livein1')))
UNION domain livein1'` by
(REWRITE_TAC [branch_domain] >> fs [SUBSET_DEF]) >>
`INJ f (set (MAP FST (toAList (difference livein2 livein1)))
UNION domain livein1) UNIV` by
metis_tac [INJ_SUBSET, UNIV_SUBSET] >>
`INJ f (domain live) UNIV` by metis_tac [INJ_SUBSET, UNIV_SUBSET] >>
`domain flivein1 = IMAGE f (domain livein1)` by
metis_tac [check_clash_tree_output] >>
`?livein flivein. check_partial_col f
(MAP FST (toAList (difference livein2 livein1)))
livein1 flivein1 = SOME (livein, flivein)` by
metis_tac [check_partial_col_success] >>
rw [] >>
`domain livein = set (MAP FST (toAList (difference livein2 livein1))) UNION domain livein1` by metis_tac [check_partial_col_domain, FST] >>
`domain livein' = set (MAP FST (toAList (difference livein2' livein1'))) UNION domain livein1'` by metis_tac [check_partial_col_domain, FST] >>
fs []
)
THEN1 (
`domain flivein1' = IMAGE f (domain livein1') /\ INJ f (domain livein1') UNIV` by metis_tac [check_live_tree_success] >>
`domain flivein2' = IMAGE f (domain livein2') /\ INJ f (domain livein2') UNIV` by metis_tac [check_live_tree_success] >>
`domain flivemid = IMAGE f (domain livemid) /\ INJ f (domain livemid) UNIV` by metis_tac [check_partial_col_success_INJ, check_partial_col_IMAGE] >>
`INJ f (domain livein') UNIV` by metis_tac [check_partial_col_success_INJ] >>
`domain livein' = set (MAP FST (toAList cutset)) UNION domain livemid` by metis_tac [check_partial_col_domain, FST] >>
`set (MAP FST (toAList cutset)) = domain cutset` by rw [EXTENSION, MEM_MAP, EXISTS_PROD, MEM_toAList, domain_lookup] >>
`domain cutset SUBSET domain livein'` by fs [] >>
`INJ f (domain cutset) UNIV` by metis_tac [INJ_SUBSET, UNIV_SUBSET] >>
`?livein flivein. check_col f cutset = SOME (livein, flivein)` by metis_tac [check_col_success] >>
rw [] >>
fs [check_col_def]
)
)
(* Seq *)
THEN1 (
rw [check_clash_tree_def, get_live_tree_def, check_live_tree_def] >>
fs [case_eq_thms] >> rveq >>
`?t2_out ft2_out. check_clash_tree f ct' live flive = SOME (t2_out, ft2_out) /\ domain t2_out SUBSET domain livein2` by metis_tac [] >>
`INJ f (domain live) UNIV` by metis_tac [INJ_SUBSET, UNIV_SUBSET] >>
`IMAGE f (domain t2_out) = domain ft2_out` by metis_tac [check_clash_tree_output] >>
simp [] >>
`IMAGE f (domain livein2) = domain flivein2` by metis_tac [check_live_tree_success] >>
`INJ f (domain livein2) UNIV` by metis_tac [check_live_tree_success] >>
metis_tac []
)
QED
Theorem get_live_tree_correct:
!f live flive ct livein flivein.
IMAGE f (domain live) = domain flive ==>
INJ f (domain live) UNIV ==>
check_live_tree f (get_live_tree ct) live flive = SOME (livein, flivein) ==>
?livein' flivein'. check_clash_tree f ct live flive = SOME (livein', flivein')
Proof
metis_tac [get_live_tree_correct_lemma, SUBSET_REFL]
QED
Theorem get_live_tree_correct_LN:
!f ct livein flivein.
check_live_tree f (get_live_tree ct) LN LN = SOME (livein, flivein) ==>
?livein' flivein'. check_clash_tree f ct LN LN = SOME (livein', flivein')
Proof
rw [get_live_tree_correct]
QED
Theorem check_partial_col_numset_list_insert:
!f l live flive liveout fliveout.
check_partial_col f l live flive = SOME (liveout, fliveout) ==>
liveout = numset_list_insert l live
Proof
Induct_on `l` >>
rw [numset_list_insert_def, check_partial_col_def] >>
Cases_on `lookup h live` >> fs []
THEN1 (
Cases_on `lookup (f h) flive` >> fs [] >>
res_tac
)
THEN1 (
`live = insert h () live` by rw [lookup_insert_id] >>
metis_tac []
)
QED
Theorem check_live_tree_eq_get_live_backward:
!f lt live flive liveout fliveout.
check_live_tree f lt live flive = SOME (liveout, fliveout) ==>
liveout = get_live_backward lt live
Proof
Induct_on `lt` >>
rw [check_live_tree_def, get_live_backward_def]
(* Writes *)
THEN1 (
every_case_tac >> fs []
)
(* Reads *)
THEN1 (
metis_tac [check_partial_col_numset_list_insert]
)
(* Branch *)
THEN1 (
every_case_tac >> fs [] >>
res_tac >>
metis_tac [check_partial_col_numset_list_insert]
)
(* Seq *)
THEN1 (
every_case_tac >> fs [] >>
res_tac >>
metis_tac []
)
QED
Theorem fix_domination_fixes_domination:
!lt. domain (get_live_backward (fix_domination lt) LN) = EMPTY
Proof
rw [get_live_backward_def, fix_domination_def, domain_numset_list_delete] >>
rw [EXTENSION, MEM_MAP, MEM_toAList, EXISTS_PROD, domain_lookup]
QED
Theorem fix_domination_check_live_tree:
!f lt liveout fliveout.
check_live_tree f (fix_domination lt) LN LN = SOME (liveout, fliveout) ==>
?liveout' fliveout'. check_live_tree f lt LN LN = SOME (liveout', fliveout')
Proof
rw [check_live_tree_def, fix_domination_def] >>
Cases_on `check_live_tree f lt LN LN` >> fs [] >>
Cases_on `x` >> fs []
QED
Theorem size_of_live_tree_positive:
!lt. 0 <= size_of_live_tree lt
Proof
Induct_on `lt` >> rw [size_of_live_tree_def]
QED
Theorem check_number_property_strong_monotone_weak:
!P Q lt n live.
(!n' live'. P n' live' ==> Q n' live') /\
check_number_property_strong P lt n live ==>
check_number_property_strong Q lt n live
Proof
Induct_on `lt` >>
rw [check_number_property_strong_def] >>
res_tac >> simp []
QED
Theorem check_number_property_strong_monotone:
!P Q lt n live.
(!n' live'. (n - size_of_live_tree lt) <= n' /\ P n' live' ==> Q n' live') /\
check_number_property_strong P lt n live ==>
check_number_property_strong Q lt n live
Proof
Induct_on `lt` >>
simp [check_number_property_strong_def, size_of_live_tree_def]
(* Branch & Seq *)
>> (
rw []
THEN1 (
`!n' live'. n - size_of_live_tree lt' - size_of_live_tree lt <= n' /\ P n' live' ==> Q n' live'` by (
rw [] >>
`n - (size_of_live_tree lt + size_of_live_tree lt') <= n'` by intLib.COOPER_TAC >>
rw []
) >>
metis_tac []
)
THEN1 (
`0 <= size_of_live_tree lt /\ 0 <= size_of_live_tree lt'` by rw [size_of_live_tree_positive] >>
`(!n' live'. n - size_of_live_tree lt' <= n' /\ P n' live' ==> Q n' live')` by (
rw [] >>
`n - (size_of_live_tree lt + size_of_live_tree lt') <= n'` by intLib.COOPER_TAC >>
rw []
) >>
metis_tac []
)
)
QED
Theorem check_number_property_strong_end:
!P lt n live.
check_number_property_strong P lt n live ==>
P (n - size_of_live_tree lt) (get_live_backward lt live)
Proof
Induct_on `lt` >>
rw [check_number_property_strong_def, get_live_backward_def, size_of_live_tree_def] >>
(* Seq *)
res_tac >>
`n - size_of_live_tree lt' - size_of_live_tree lt = n - (size_of_live_tree lt + size_of_live_tree lt')` by intLib.COOPER_TAC >>
metis_tac []
QED
Theorem check_number_property_monotone_weak:
!P Q lt n live.
(!n' live'. P n' live' ==> Q n' live') /\
check_number_property P lt n live ==>
check_number_property Q lt n live
Proof
Induct_on `lt` >>
rw [check_number_property_def] >>
res_tac >> simp []
QED
Theorem lookup_numset_list_add_if:
!r l v s.
lookup r (numset_list_add_if l v s P) =
if MEM r l then
case lookup r s of
| (SOME vr) =>
if P v vr then SOME v
else SOME vr
| NONE =>
SOME v
else
lookup r s
Proof
Induct_on `l` >>
simp [numset_list_add_if_def] >>
rpt gen_tac >>
Cases_on `lookup h s` >> fs []
THEN1 (
Cases_on `MEM r l` >> fs [] >>
rw [lookup_insert]
) >>
Cases_on `r = h` >> fs [] >> rveq
THEN1 (
Cases_on `P v x` >> fs []
) >>
Cases_on `P v x` >> fs [] >>
simp [lookup_insert]
QED
Theorem lookup_numset_list_add_if_lt:
!r l v s.
lookup r (numset_list_add_if_lt l v s) =
if MEM r l then
case lookup r s of
| (SOME vr) =>
if v <= vr then SOME v
else SOME vr
| NONE =>
SOME v
else
lookup r s
Proof
simp [numset_list_add_if_lt_def] >>
rw [lookup_numset_list_add_if]
QED
Theorem lookup_numset_list_add_if_gt:
!r l v s.
lookup r (numset_list_add_if_gt l v s) =
if MEM r l then
case lookup r s of
| (SOME vr) =>
if vr <= v then SOME v
else SOME vr
| NONE =>
SOME v
else
lookup r s
Proof
simp [numset_list_add_if_gt_def] >>
rw [lookup_numset_list_add_if]
QED
Theorem domain_numset_list_add_if:
!l v s P. domain (numset_list_add_if l v s P) = set l UNION domain s
Proof
Induct_on `l` >>
rw [numset_list_add_if_def] >>
Cases_on `lookup h s`
THEN1 (
rw [EXTENSION] >>
metis_tac []
)
THEN1 (
`h IN domain s` by rw [domain_lookup] >>
rw [EXTENSION] >>
metis_tac []
)
QED
Theorem domain_numset_list_add_if_lt:
!l v s. domain (numset_list_add_if_lt l v s) = set l UNION domain s
Proof
rw [numset_list_add_if_lt_def, domain_numset_list_add_if]
QED
Theorem domain_numset_list_add_if_gt:
!l v s. domain (numset_list_add_if_gt l v s) = set l UNION domain s
Proof
rw [numset_list_add_if_gt_def, domain_numset_list_add_if]
QED
Theorem lookup_numset_list_delete:
!l s x. lookup x (numset_list_delete l s) = if MEM x l then NONE else lookup x s
Proof
Induct_on `l` >>
rw [numset_list_delete_def, lookup_delete] >>
fs []
QED
Theorem get_intervals_nout:
!lt n_in beg_in end_in n_out beg_out end_out.
(n_out, beg_out, end_out) = get_intervals lt n_in beg_in end_in ==>
n_out = n_in - (size_of_live_tree lt)
Proof
Induct_on `lt` >>
rw [get_intervals_def, size_of_live_tree_def] >>
rpt (pairarg_tac >> fs []) >>
`n_out = n2 - (size_of_live_tree lt)` by metis_tac [] >>
`n2 = n_in - (size_of_live_tree lt')` by metis_tac [] >>
intLib.COOPER_TAC
QED
Theorem get_intervals_withlive_nout:
!lt n_in beg_in end_in n_out beg_out end_out live.
(n_out, beg_out, end_out) = get_intervals_withlive lt n_in beg_in end_in live ==>
n_out = n_in - (size_of_live_tree lt)
Proof
Induct_on `lt` >>
rw [get_intervals_withlive_def, size_of_live_tree_def] >>
rpt (pairarg_tac >> fs []) >>
`n_out = n2 - (size_of_live_tree lt)` by metis_tac [] >>
`n2 = n_in - (size_of_live_tree lt')` by metis_tac [] >>
rveq >> intLib.COOPER_TAC
QED
Theorem get_intervals_intend_augment:
!lt n_in beg_in end_in n_out beg_out end_out.
(n_out, beg_out, end_out) = get_intervals lt n_in beg_in end_in ==>
!r v. lookup r end_in = SOME v ==> (?v'. lookup r end_out = SOME v' /\ v <= v')
Proof
Induct_on `lt` >>
rw [get_intervals_def]
(* Writes *)
THEN1 (
rw [lookup_numset_list_add_if_gt]
)
(* Reads *)
THEN1 (
rw [lookup_numset_list_add_if_gt]
) >>
(* Branch & Seq *)
rpt (pairarg_tac >> fs []) >>
`?v'. lookup r int_end2 = SOME v' /\ v <= v'` by metis_tac [] >>
`?v''. lookup r end_out = SOME v'' /\ v' <= v''` by metis_tac [] >>
rw [] >>
intLib.COOPER_TAC
QED
Theorem check_number_property_intend:
!end_out lt n_in live_in.
check_number_property (\n (live : num_set). !r. r IN domain live ==> ?v. lookup r end_out = SOME v /\ n+1 <= v) lt n_in live_in ==>
!r. r IN domain (get_live_backward lt live_in) ==> ?v. lookup r end_out = SOME v /\ n_in-(size_of_live_tree lt) <= v
Proof
Induct_on `lt` >>
rw [check_number_property_def, get_live_backward_def, size_of_live_tree_def]
(* Writes *)
THEN1 (
res_tac >>
rw [] >>
intLib.COOPER_TAC
)
(* Reads *)
THEN1 (
res_tac >>
rw [] >>
intLib.COOPER_TAC
)
(* Branch *)
THEN1 (
fs [branch_domain, domain_numset_list_insert] >>
res_tac >> rw [] >>
`0 <= size_of_live_tree lt /\ 0 <= size_of_live_tree lt'` by rw [size_of_live_tree_positive] >>
intLib.COOPER_TAC
)
(* Seq *)
THEN1 (
res_tac >> rw [] >>
intLib.COOPER_TAC
)
QED
Theorem get_intervals_live_less_end:
!lt n_in beg_in end_in live_in n_out beg_out end_out.
(n_out, beg_out, end_out) = get_intervals lt n_in beg_in end_in /\
(!r. r IN domain live_in ==> ?v. lookup r end_in = SOME v /\ n_in <= v) ==>
check_number_property (\n (live : num_set). !r. r IN domain live ==> ?v. lookup r end_out = SOME v /\ n+1 <= v) lt n_in live_in
Proof
Induct_on `lt` >>
simp [get_intervals_def, check_number_property_def] >>
rpt gen_tac >> strip_tac >> rveq
(* Writes *)
THEN1 (
rw [domain_numset_list_delete, lookup_numset_list_add_if_gt]
)
(* Reads *)
THEN1 (
rw [domain_numset_list_insert, lookup_numset_list_add_if_gt] >>
every_case_tac >> rw [] >> intLib.COOPER_TAC
) >>
(* Common Branch & Seq proof *)
rpt (pairarg_tac >> fs []) >>
`n2 = n_in - (size_of_live_tree lt') /\ n_out = n2 - (size_of_live_tree lt)` by metis_tac [get_intervals_nout] >>
`check_number_property (\n live. !r. r IN domain live ==> ?v. lookup r int_end2 = SOME v /\ n+1 <= v) lt' n_in live_in` by metis_tac [] >>
`check_number_property (\n live. !r. r IN domain live ==> ?v. lookup r end_out = SOME v /\ n+1 <= v) lt' n_in live_in` by (
sg `!n' (live' : num_set). (\n live. !r. r IN domain live ==>
?v. lookup r int_end2 = SOME v /\ n+1 <= v) n' live' ==>
(\n live. !r. r IN domain live ==>
?v. lookup r end_out = SOME v /\ n+1 <= v) n' live'` THEN1 (
rw [] >>
`?v. lookup r int_end2 = SOME v /\ n'+1 <= v` by rw [] >>
`?v'. lookup r end_out = SOME v' /\ v <= v'` by metis_tac [get_intervals_intend_augment] >>
rw [] >> intLib.COOPER_TAC
) >>
qspecl_then [`\n live. !r. r IN domain live ==> ?v. lookup r int_end2 = SOME v /\ n+1 <= v`,
`\n live. !r. r IN domain live ==> ?v. lookup r end_out = SOME v /\ n+1 <= v`, `lt'`, `n_in`, `live_in`]
assume_tac check_number_property_monotone_weak >>
rw []
) >>
`0 <= size_of_live_tree lt /\ 0 <= size_of_live_tree lt'` by rw [size_of_live_tree_positive] >>
simp []
(* Branch *)
THEN1 (
sg `!r. r IN domain live_in ==> ?v. lookup r int_end2 = SOME v /\ n2 <= v` THEN1 (
rw [] >>
`?v. lookup r end_in = SOME v /\ n_in <= v` by rw [] >>
`?v'. lookup r int_end2 = SOME v' /\ v <= v'` by metis_tac [get_intervals_intend_augment] >>
rw [] >>
intLib.COOPER_TAC
) >>
metis_tac []
)
(* Seq *)
THEN1 (
`!r. r IN domain (get_live_backward lt' live_in) ==> ?v. lookup r int_end2 = SOME v /\ n_in - size_of_live_tree lt' <= v` by metis_tac [check_number_property_intend] >>
metis_tac []
)
QED
Theorem get_intervals_withlive_intbeg_reduce:
!lt n_in beg_in end_in n_out beg_out end_out live.
(n_out, beg_out, end_out) = get_intervals_withlive lt n_in beg_in end_in live /\
(!r v. lookup r beg_in = SOME v ==> n_in <= v) ==>
(!r. option_CASE (lookup r beg_out) n_out (\x.x) <= option_CASE (lookup r beg_in) n_in (\x.x)) /\
(!r v. lookup r beg_out = SOME v ==> n_out <= v)
Proof
Induct_on `lt` >>
simp [get_intervals_withlive_def] >>
rpt gen_tac >> strip_tac
(* Writes *)
THEN1 (
rpt strip_tac >>
fs [lookup_numset_list_add_if_lt] >>
rw [] >>
every_case_tac >>
res_tac >>
rw [] >>
intLib.COOPER_TAC
)
(* Reads *)
THEN1 (
rpt strip_tac >>
fs [lookup_numset_list_delete] >>
rw [] >>
every_case_tac >>
res_tac >>
rw [] >>
intLib.COOPER_TAC
)
(* Branch *)
THEN1 (
rpt (pairarg_tac >> fs []) >>
`(!r. option_CASE (lookup r int_beg2) n2 (\x.x) <= option_CASE (lookup r beg_in) n_in (\x.x)) /\ (!r v. lookup r int_beg2 = SOME v ==> n2 <= v)` by (res_tac >> metis_tac []) >>
`!r v. lookup r (difference int_beg2 live) = SOME v ==> n2 <= v` by (rw [lookup_difference] >> res_tac) >>
`(!r. option_CASE (lookup r int_beg1) n1 (\x.x) <=
option_CASE (lookup r (difference int_beg2 live)) n2 (\x.x)) /\
(!r v. lookup r (difference int_beg2 live) = SOME v ==> n2 <= v) /\
(!r v. lookup r int_beg1 = SOME v ==> n1 <= v)` by
(res_tac >> metis_tac []) >>
fs [lookup_difference] >>
`n2 = n_in - size_of_live_tree lt'` by metis_tac [get_intervals_withlive_nout] >>
`n1 = n2 - size_of_live_tree lt` by metis_tac [get_intervals_withlive_nout] >>
`0 <= size_of_live_tree lt /\ 0 <= size_of_live_tree lt'` by rw [size_of_live_tree_positive] >>
rpt strip_tac
THEN1 (
rpt (first_x_assum (qspec_then `r` assume_tac)) >>
rpt CASE_TAC >>
Cases_on `lookup r int_beg1` >> Cases_on `lookup r int_beg2` >>
Cases_on `lookup r live` >>
rfs [] >> fs [set_MAP_FST_toAList_eq_domain] >> rw [] >>
rpt (qpat_x_assum `lookup _ _ = _` kall_tac) >>
intLib.COOPER_TAC
)
THEN1 res_tac
)
(* Seq*)
THEN1 (
rpt (pairarg_tac >> fs []) >>
`(!r. option_CASE (lookup r int_beg2) n2 (\x.x) <=
option_CASE (lookup r beg_in) n_in (\x.x)) /\
(!r v. lookup r beg_in = SOME v ==> n_in <= v) /\
(!r v. lookup r int_beg2 = SOME v ==> n2 <= v)` by
(res_tac >> metis_tac []) >>
`(!r. option_CASE (lookup r int_beg1) n1 (\x.x) <=
option_CASE (lookup r int_beg2) n2 (\x.x)) /\
(!r v. lookup r int_beg2 = SOME v ==> n2 <= v) /\
(!r v. lookup r int_beg1 = SOME v ==> n1 <= v)` by
(res_tac >> metis_tac []) >>
simp [lookup_numset_list_delete] >>
`n2 = n_in - size_of_live_tree lt'` by metis_tac [get_intervals_withlive_nout] >>
`n1 = n2 - size_of_live_tree lt` by metis_tac [get_intervals_withlive_nout] >>
`0 <= size_of_live_tree lt /\ 0 <= size_of_live_tree lt'` by rw [size_of_live_tree_positive] >>
rpt strip_tac
THEN1 (
rpt CASE_TAC >>
rpt (first_x_assum (qspec_then `r` assume_tac)) >>
rfs [] >> rw [] >>
intLib.COOPER_TAC
)
THEN1 res_tac
)
QED
Theorem get_intervals_withlive_intbeg_nout:
!lt n_in beg_in end_in n_out beg_out end_out live.
(n_out, beg_out, end_out) = get_intervals_withlive lt n_in beg_in end_in live /\
(!r v. lookup r beg_in = SOME v ==> n_in <= v) ==>
(!r v. lookup r beg_out = SOME v ==> n_out <= v)