-
Notifications
You must be signed in to change notification settings - Fork 131
/
sptreeScript.sml
2561 lines (2285 loc) · 81.3 KB
/
sptreeScript.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
open HolKernel Parse boolLib bossLib;
open arithmeticTheory
open logrootTheory
open listTheory
open alistTheory
open pred_setTheory
open dep_rewrite
val _ = new_theory "sptree";
(* A log-time random-access, extensible array implementation with union.
The "array" can be gappy: there doesn't have to be an element at
any particular index, and, being a finite thing, there is obviously
a maximum index past which there are no elements at all. It is
possible to update at an index past the current maximum index. It
is also possible to delete values at any index.
Should EVAL well.
The insert, delete and union operations all preserve a
well-formedness condition ("wf") that ensures there is only one
possible representation for any given finite-map.
It is tricky to traverse the array and extract a list of elements.
There are three array->list operations defined:
- toSortedAList: produces an assocation list in index order.
- toAList: produces an association list in a mixed-up order. Defined via
foldi, and related to foldi and mapi by theorems. Slowest to EVAL.
- toList: roughly equals MAP SND (toAList t), although in a different mixed
up order. By far the fastest to EVAL.
*)
val _ = Datatype`spt = LN | LS 'a | BN spt spt | BS spt 'a spt`
(* Leaf-None, Leaf-Some, Branch-None, Branch-Some *)
Type num_map[pp] = “:'a spt”
Type num_set[pp] = “:unit spt”
val _ = overload_on ("isEmpty", ``\t. t = LN``)
val wf_def = Define`
(wf LN <=> T) /\
(wf (LS a) <=> T) /\
(wf (BN t1 t2) <=> wf t1 /\ wf t2 /\ ~(isEmpty t1 /\ isEmpty t2)) /\
(wf (BS t1 a t2) <=> wf t1 /\ wf t2 /\ ~(isEmpty t1 /\ isEmpty t2))
`;
Definition lookup_def[nocompute]:
(lookup k LN = NONE) /\
(lookup k (LS a) = if k = 0 then SOME a else NONE) /\
(lookup k (BN t1 t2) =
if k = 0 then NONE
else lookup ((k - 1) DIV 2) (if EVEN k then t1 else t2)) /\
(lookup k (BS t1 a t2) =
if k = 0 then SOME a
else lookup ((k - 1) DIV 2) (if EVEN k then t1 else t2))
Termination WF_REL_TAC `measure FST` >> simp[DIV_LT_X]
End
Theorem lookup_rwts[simp]:
lookup k LN = NONE /\
lookup 0 (LS a) = SOME a /\
lookup 0 (BN t1 t2) = NONE /\
lookup 0 (BS t1 a t2) = SOME a
Proof
simp[lookup_def]
QED
Definition insert_def[nocompute]:
(insert k a LN = if k = 0 then LS a
else if EVEN k then BN (insert ((k-1) DIV 2) a LN) LN
else BN LN (insert ((k-1) DIV 2) a LN)) /\
(insert k a (LS a') =
if k = 0 then LS a
else if EVEN k then BS (insert ((k-1) DIV 2) a LN) a' LN
else BS LN a' (insert ((k-1) DIV 2) a LN)) /\
(insert k a (BN t1 t2) =
if k = 0 then BS t1 a t2
else if EVEN k then BN (insert ((k - 1) DIV 2) a t1) t2
else BN t1 (insert ((k - 1) DIV 2) a t2)) /\
(insert k a (BS t1 a' t2) =
if k = 0 then BS t1 a t2
else if EVEN k then BS (insert ((k - 1) DIV 2) a t1) a' t2
else BS t1 a' (insert ((k - 1) DIV 2) a t2))
Termination
WF_REL_TAC `measure FST` >> simp[DIV_LT_X]
End
val insert_ind = theorem "insert_ind";
val mk_BN_def = Define `
(mk_BN LN LN = LN) /\
(mk_BN t1 t2 = BN t1 t2)`;
val mk_BS_def = Define `
(mk_BS LN x LN = LS x) /\
(mk_BS t1 x t2 = BS t1 x t2)`;
val delete_def = zDefine`
(delete k LN = LN) /\
(delete k (LS a) = if k = 0 then LN else LS a) /\
(delete k (BN t1 t2) =
if k = 0 then BN t1 t2
else if EVEN k then
mk_BN (delete ((k - 1) DIV 2) t1) t2
else
mk_BN t1 (delete ((k - 1) DIV 2) t2)) /\
(delete k (BS t1 a t2) =
if k = 0 then BN t1 t2
else if EVEN k then
mk_BS (delete ((k - 1) DIV 2) t1) a t2
else
mk_BS t1 a (delete ((k - 1) DIV 2) t2))
`;
val fromList_def = Define`
fromList l = SND (FOLDL (\(i,t) a. (i + 1, insert i a t)) (0,LN) l)
`;
Definition size_def[simp]:
(size LN = 0) /\
(size (LS a) = 1) /\
(size (BN t1 t2) = size t1 + size t2) /\
(size (BS t1 a t2) = size t1 + size t2 + 1)
End
Theorem insert_notEmpty[simp]: ~isEmpty (insert k a t)
Proof
Cases_on `t` >> rw[Once insert_def]
QED
val wf_insert = store_thm(
"wf_insert",
``!k a t. wf t ==> wf (insert k a t)``,
ho_match_mp_tac (theorem "insert_ind") >>
rpt strip_tac >>
simp[Once insert_def] >> rw[wf_def, insert_notEmpty] >> fs[wf_def]);
val mk_BN_thm = prove(
``!t1 t2. mk_BN t1 t2 =
if isEmpty t1 /\ isEmpty t2 then LN else BN t1 t2``,
REPEAT Cases >> EVAL_TAC);
val mk_BS_thm = prove(
``!t1 t2. mk_BS t1 x t2 =
if isEmpty t1 /\ isEmpty t2 then LS x else BS t1 x t2``,
REPEAT Cases >> EVAL_TAC);
val wf_delete = store_thm(
"wf_delete",
``!t k. wf t ==> wf (delete k t)``,
Induct >> rw[wf_def, delete_def, mk_BN_thm, mk_BS_thm] >>
rw[wf_def] >> rw[] >> fs[] >> metis_tac[]);
Theorem lookup_insert1[simp]: !k a t. lookup k (insert k a t) = SOME a
Proof
ho_match_mp_tac (theorem "insert_ind") >> rpt strip_tac >>
simp[Once insert_def] >> rw[lookup_def]
QED
val DIV2_EQ_DIV2 = prove(
``(m DIV 2 = n DIV 2) <=>
(m = n) \/
(n = m + 1) /\ EVEN m \/
(m = n + 1) /\ EVEN n``,
`0 < 2` by simp[] >>
map_every qabbrev_tac [`nq = n DIV 2`, `nr = n MOD 2`] >>
qspec_then `2` mp_tac DIVISION >> asm_simp_tac bool_ss [] >>
disch_then (qspec_then `n` mp_tac) >> asm_simp_tac bool_ss [] >>
map_every qabbrev_tac [`mq = m DIV 2`, `mr = m MOD 2`] >>
qspec_then `2` mp_tac DIVISION >> asm_simp_tac bool_ss [] >>
disch_then (qspec_then `m` mp_tac) >> asm_simp_tac bool_ss [] >>
rw[] >> markerLib.RM_ALL_ABBREVS_TAC >>
simp[EVEN_ADD, EVEN_MULT] >>
`!p. p < 2 ==> (EVEN p <=> (p = 0))`
by (rpt strip_tac >> `(p = 0) \/ (p = 1)` by decide_tac >> simp[]) >>
simp[]);
val EVEN_PRE = prove(
``x <> 0 ==> (EVEN (x - 1) <=> ~EVEN x)``,
Induct_on `x` >> simp[] >> Cases_on `x` >> fs[] >>
simp_tac (srw_ss()) [EVEN]);
val lookup_insert = store_thm(
"lookup_insert",
``!k2 v t k1. lookup k1 (insert k2 v t) =
if k1 = k2 then SOME v else lookup k1 t``,
ho_match_mp_tac (theorem "insert_ind") >> rpt strip_tac >>
simp[Once insert_def] >> rw[lookup_def] >> simp[] >| [
fs[lookup_def] >> pop_assum mp_tac >> Cases_on `k1 = 0` >> simp[] >>
COND_CASES_TAC >> simp[lookup_def, DIV2_EQ_DIV2, EVEN_PRE],
fs[lookup_def] >> pop_assum mp_tac >> Cases_on `k1 = 0` >> simp[] >>
COND_CASES_TAC >> simp[lookup_def, DIV2_EQ_DIV2, EVEN_PRE] >>
rpt strip_tac >> metis_tac[EVEN_PRE],
fs[lookup_def] >> COND_CASES_TAC >>
simp[lookup_def, DIV2_EQ_DIV2, EVEN_PRE],
fs[lookup_def] >> COND_CASES_TAC >>
simp[lookup_def, DIV2_EQ_DIV2, EVEN_PRE] >>
rpt strip_tac >> metis_tac[EVEN_PRE],
simp[DIV2_EQ_DIV2, EVEN_PRE],
simp[DIV2_EQ_DIV2, EVEN_PRE] >> COND_CASES_TAC
>- metis_tac [EVEN_PRE] >> simp[],
simp[DIV2_EQ_DIV2, EVEN_PRE],
simp[DIV2_EQ_DIV2, EVEN_PRE] >> COND_CASES_TAC
>- metis_tac [EVEN_PRE] >> simp[]
])
val union_def = Define`
(union LN t = t) /\
(union (LS a) t =
case t of
| LN => LS a
| LS b => LS a
| BN t1 t2 => BS t1 a t2
| BS t1 _ t2 => BS t1 a t2) /\
(union (BN t1 t2) t =
case t of
| LN => BN t1 t2
| LS a => BS t1 a t2
| BN t1' t2' => BN (union t1 t1') (union t2 t2')
| BS t1' a t2' => BS (union t1 t1') a (union t2 t2')) /\
(union (BS t1 a t2) t =
case t of
| LN => BS t1 a t2
| LS a' => BS t1 a t2
| BN t1' t2' => BS (union t1 t1') a (union t2 t2')
| BS t1' a' t2' => BS (union t1 t1') a (union t2 t2'))
`;
Theorem isEmpty_union[simp]:
isEmpty (union m1 m2) <=> isEmpty m1 /\ isEmpty m2
Proof
map_every Cases_on [`m1`, `m2`] >> simp[union_def]
QED
val wf_union = store_thm(
"wf_union",
``!m1 m2. wf m1 /\ wf m2 ==> wf (union m1 m2)``,
Induct >> simp[wf_def, union_def] >>
Cases_on `m2` >> simp[wf_def,isEmpty_union] >>
metis_tac[]);
val optcase_lemma = prove(
``(case opt of NONE => NONE | SOME v => SOME v) = opt``,
Cases_on `opt` >> simp[]);
val lookup_union = store_thm(
"lookup_union",
``!m1 m2 k. lookup k (union m1 m2) =
case lookup k m1 of
NONE => lookup k m2
| SOME v => SOME v``,
Induct >> simp[lookup_def] >- simp[union_def] >>
Cases_on `m2` >> simp[lookup_def, union_def] >>
rw[optcase_lemma]);
val inter_def = Define`
(inter LN t = LN) /\
(inter (LS a) t =
case t of
| LN => LN
| LS b => LS a
| BN t1 t2 => LN
| BS t1 _ t2 => LS a) /\
(inter (BN t1 t2) t =
case t of
| LN => LN
| LS a => LN
| BN t1' t2' => mk_BN (inter t1 t1') (inter t2 t2')
| BS t1' a t2' => mk_BN (inter t1 t1') (inter t2 t2')) /\
(inter (BS t1 a t2) t =
case t of
| LN => LN
| LS a' => LS a
| BN t1' t2' => mk_BN (inter t1 t1') (inter t2 t2')
| BS t1' a' t2' => mk_BS (inter t1 t1') a (inter t2 t2'))
`;
val inter_eq_def = Define`
(inter_eq LN t = LN) /\
(inter_eq (LS a) t =
case t of
| LN => LN
| LS b => if a = b then LS a else LN
| BN t1 t2 => LN
| BS t1 b t2 => if a = b then LS a else LN) /\
(inter_eq (BN t1 t2) t =
case t of
| LN => LN
| LS a => LN
| BN t1' t2' => mk_BN (inter_eq t1 t1') (inter_eq t2 t2')
| BS t1' a t2' => mk_BN (inter_eq t1 t1') (inter_eq t2 t2')) /\
(inter_eq (BS t1 a t2) t =
case t of
| LN => LN
| LS a' => if a' = a then LS a else LN
| BN t1' t2' => mk_BN (inter_eq t1 t1') (inter_eq t2 t2')
| BS t1' a' t2' =>
if a' = a then
mk_BS (inter_eq t1 t1') a (inter_eq t2 t2')
else mk_BN (inter_eq t1 t1') (inter_eq t2 t2'))`;
val difference_def = Define`
(difference LN t = LN) /\
(difference (LS a) t =
case t of
| LN => LS a
| LS b => LN
| BN t1 t2 => LS a
| BS t1 b t2 => LN) /\
(difference (BN t1 t2) t =
case t of
| LN => BN t1 t2
| LS a => BN t1 t2
| BN t1' t2' => mk_BN (difference t1 t1') (difference t2 t2')
| BS t1' a t2' => mk_BN (difference t1 t1') (difference t2 t2')) /\
(difference (BS t1 a t2) t =
case t of
| LN => BS t1 a t2
| LS a' => BN t1 t2
| BN t1' t2' => mk_BS (difference t1 t1') a (difference t2 t2')
| BS t1' a' t2' => mk_BN (difference t1 t1') (difference t2 t2'))`;
val wf_mk_BN = prove(
``!t1 t2. wf (mk_BN t1 t2) <=> wf t1 /\ wf t2``,
map_every Cases_on [`t1`,`t2`] >> fs [mk_BN_def,wf_def]);
val wf_mk_BS = prove(
``!t1 x t2. wf (mk_BS t1 x t2) <=> wf t1 /\ wf t2``,
map_every Cases_on [`t1`,`t2`] >> fs [mk_BS_def,wf_def]);
val wf_inter = store_thm(
"wf_inter[simp]",
``!m1 m2. wf (inter m1 m2)``,
Induct >> simp[wf_def, inter_def] >>
Cases_on `m2` >> simp[wf_def,wf_mk_BS,wf_mk_BN]);
val lookup_mk_BN = prove(
``lookup k (mk_BN t1 t2) = lookup k (BN t1 t2)``,
map_every Cases_on [`t1`,`t2`] >> fs [mk_BN_def,lookup_def]);
val lookup_mk_BS = prove(
``lookup k (mk_BS t1 x t2) = lookup k (BS t1 x t2)``,
map_every Cases_on [`t1`,`t2`] >> fs [mk_BS_def,lookup_def]);
val lookup_inter = store_thm(
"lookup_inter",
``!m1 m2 k. lookup k (inter m1 m2) =
case (lookup k m1,lookup k m2) of
| (SOME v, SOME w) => SOME v
| _ => NONE``,
Induct >> simp[lookup_def] >> Cases_on `m2` >>
simp[lookup_def, inter_def, lookup_mk_BS, lookup_mk_BN] >>
rw[optcase_lemma] >> BasicProvers.CASE_TAC);
val lookup_inter_eq = store_thm(
"lookup_inter_eq",
``!m1 m2 k. lookup k (inter_eq m1 m2) =
case lookup k m1 of
| NONE => NONE
| SOME v => (if lookup k m2 = SOME v then SOME v else NONE)``,
Induct >> simp[lookup_def] >> Cases_on `m2` >>
simp[lookup_def, inter_eq_def, lookup_mk_BS, lookup_mk_BN] >>
rw[optcase_lemma] >> REPEAT BasicProvers.CASE_TAC >>
fs [lookup_def, lookup_mk_BS, lookup_mk_BN]);
val lookup_inter_EQ = store_thm("lookup_inter_EQ",
``((lookup x (inter t1 t2) = SOME y) <=>
(lookup x t1 = SOME y) /\ lookup x t2 <> NONE) /\
((lookup x (inter t1 t2) = NONE) <=>
(lookup x t1 = NONE) \/ (lookup x t2 = NONE))``,
fs [lookup_inter] \\ BasicProvers.EVERY_CASE_TAC);
val lookup_inter_assoc = store_thm("lookup_inter_assoc",
``lookup x (inter t1 (inter t2 t3)) =
lookup x (inter (inter t1 t2) t3)``,
fs [lookup_inter] \\ BasicProvers.EVERY_CASE_TAC)
val lookup_difference = store_thm(
"lookup_difference",
``!m1 m2 k. lookup k (difference m1 m2) =
if lookup k m2 = NONE then lookup k m1 else NONE``,
Induct >> simp[lookup_def] >> Cases_on `m2` >>
simp[lookup_def, difference_def, lookup_mk_BS, lookup_mk_BN] >>
rw[optcase_lemma] >> REPEAT BasicProvers.CASE_TAC >>
fs [lookup_def, lookup_mk_BS, lookup_mk_BN])
Definition lrnext_def[nocompute]:
lrnext n = if n = 0 then 1 else 2 * lrnext ((n - 1) DIV 2)
Termination
WF_REL_TAC `measure I` \\ fs [DIV_LT_X] \\ REPEAT STRIP_TAC \\ DECIDE_TAC
End
Triviality silly:
NUMERAL (SUC x) = SUC x /\
ZERO + ZERO = 0 /\
BIT2 n <> 0 /\
(x + x) DIV 2 = x /\
(BIT2 n - 1) DIV 2 = n
Proof
reverse (rpt conj_tac)
>- (simp_tac bool_ss [BIT2, SimpL “$DIV”, ONE, ADD_CLAUSES,
SUB_MONO_EQ, SUB_0] >>
simp[ADD_DIV_RWT, ADD1] >>
metis_tac[MULT_DIV, DECIDE “0 < 2”, MULT_COMM])
>- simp_tac (srw_ss()) [DECIDE “n + n = n * 2”, MULT_DIV]
>- REWRITE_TAC[BIT2, ADD_CLAUSES, numTheory.NOT_SUC] >>
simp[NUMERAL_DEF, ALT_ZERO]
QED
Theorem lrnext_thm[compute]:
(lrnext ZERO = 1) /\
(!n. lrnext (BIT1 n) = 2 * lrnext n) /\
(!n. lrnext (BIT2 n) = 2 * lrnext n) /\
(!a. lrnext 0 = 1) /\
(!n a. lrnext (NUMERAL n) = lrnext n)
Proof
REPEAT STRIP_TAC
THEN1 simp[ALT_ZERO, Once lrnext_def]
THEN1 (simp[Once lrnext_def, SimpLHS] >>
REWRITE_TAC[BIT1, ADD_CLAUSES, numTheory.NOT_SUC,
SUB_MONO_EQ, SUB_0, silly])
THEN1 (simp[Once lrnext_def, SimpLHS] >> REWRITE_TAC [silly])
THEN1 simp[Once lrnext_def]
THEN1 simp[NUMERAL_DEF]
QED
Theorem lrnext_eq:
!n. sptree$lrnext n = 2 ** (LOG 2 (n + 1))
Proof
strip_tac >> completeInduct_on `n` >> rw[] >>
rw[Once lrnext_def] >>
first_x_assum $ qspec_then `(n - 1) DIV 2` mp_tac >>
impl_tac >> rw[] >- simp[DIV_LT_X] >>
simp[GSYM EXP] >> Cases_on `EVEN n` >>
gvs[GSYM ODD_EVEN] >> imp_res_tac EVEN_ODD_EXISTS >> gvs[]
>- (`(2 * m - 1) DIV 2 = m - 1` by simp[DIV_EQ_X] >> simp[LOG_add_digit])
>- simp[Once LOG_RWT, SimpRHS, ADD1]
QED
Definition domain_def[simp,nocompute]:
domain LN = {} /\
domain (LS _) = {0} /\
domain (BN t1 t2) =
IMAGE (\n. 2 * n + 2) (domain t1) UNION
IMAGE (\n. 2 * n + 1) (domain t2) /\
domain (BS t1 _ t2) =
{0} UNION IMAGE (\n. 2 * n + 2) (domain t1) UNION
IMAGE (\n. 2 * n + 1) (domain t2)
End
Theorem FINITE_domain[simp]: FINITE (domain t)
Proof Induct_on `t` >> simp[]
QED
val DIV2 = DIVISION |> Q.SPEC `2` |> REWRITE_RULE [DECIDE ``0 < 2``]
val even_lem = Q.prove(
`EVEN k /\ k <> 0 ==> (2 * ((k - 1) DIV 2) + 2 = k)`,
qabbrev_tac `k0 = k - 1` >>
strip_tac >> `k = k0 + 1` by simp[Abbr`k0`] >>
pop_assum SUBST_ALL_TAC >> qunabbrev_tac `k0` >>
fs[EVEN_ADD] >>
assume_tac (Q.SPEC `k0` DIV2) >>
map_every qabbrev_tac [`q = k0 DIV 2`, `r = k0 MOD 2`] >>
markerLib.RM_ALL_ABBREVS_TAC >>
fs[EVEN_ADD, EVEN_MULT] >>
`(r = 0) \/ (r = 1)` by simp[] >> fs[])
val odd_lem = Q.prove(
`~EVEN k /\ k <> 0 ==> (2 * ((k - 1) DIV 2) + 1 = k)`,
qabbrev_tac `k0 = k - 1` >>
strip_tac >> `k = k0 + 1` by simp[Abbr`k0`] >>
pop_assum SUBST_ALL_TAC >> qunabbrev_tac `k0` >>
fs[EVEN_ADD] >>
assume_tac (Q.SPEC `k0` DIV2) >>
map_every qabbrev_tac [`q = k0 DIV 2`, `r = k0 MOD 2`] >>
markerLib.RM_ALL_ABBREVS_TAC >>
fs[EVEN_ADD, EVEN_MULT] >>
`(r = 0) \/ (r = 1)` by simp[] >> fs[])
val even_lem' = CONV_RULE (RAND_CONV (ONCE_REWRITE_CONV [EQ_SYM_EQ])) even_lem
val odd_lem' = CONV_RULE (RAND_CONV (ONCE_REWRITE_CONV [EQ_SYM_EQ])) odd_lem
val even_imposs = Q.prove(
‘EVEN n ==> !m. n <> 2 * m + 1’,
rpt strip_tac >> fs[EVEN_ADD, EVEN_MULT]);
val odd_imposs = Q.prove(
‘~EVEN n ==> !m. n <> 2 * m + 2’,
rpt strip_tac >> fs[EVEN_ADD, EVEN_MULT]);
fun writeL th = CONV_TAC (LAND_CONV (ONCE_REWRITE_CONV [th]))
Theorem IN_domain:
!n x t1 t2.
(n IN domain LN <=> F) /\
(n IN domain (LS x) <=> (n = 0)) /\
(n IN domain (BN t1 t2) <=>
n <> 0 /\ (if EVEN n then ((n-1) DIV 2) IN domain t1
else ((n-1) DIV 2) IN domain t2)) /\
(n IN domain (BS t1 x t2) <=>
(n = 0) \/ (if EVEN n then ((n-1) DIV 2) IN domain t1
else ((n-1) DIV 2) IN domain t2))
Proof
simp[domain_def] >> rpt strip_tac >> Cases_on ‘n = 0’ >> simp[] >>
Cases_on ‘EVEN n’ >> simp[] >>
(drule_then assume_tac even_imposs ORELSE drule_then assume_tac odd_imposs) >>
simp[] >>
(drule_all_then writeL even_lem' ORELSE drule_all_then writeL odd_lem') >>
simp[]
QED
val size_insert = Q.store_thm(
"size_insert",
`!k v m. size (insert k v m) = if k IN domain m then size m else size m + 1`,
ho_match_mp_tac insert_ind >> rpt conj_tac >> simp[] >>
rpt strip_tac >> simp[Once insert_def]
>- rw[]
>- rw[]
>- (Cases_on `k = 0` >> simp[] >> fs[] >> Cases_on `EVEN k` >> fs[]
>- (`!n. k <> 2 * n + 1` by (rpt strip_tac >> fs[EVEN_ADD, EVEN_MULT]) >>
qabbrev_tac `k2 = (k - 1) DIV 2` >>
`k = 2 * k2 + 2` suffices_by rw[] >>
simp[Abbr`k2`, even_lem]) >>
`!n. k <> 2 * n + 2` by (rpt strip_tac >> fs[EVEN_ADD, EVEN_MULT]) >>
qabbrev_tac `k2 = (k - 1) DIV 2` >>
`k = 2 * k2 + 1` suffices_by rw[] >>
simp[Abbr`k2`, odd_lem])
>- (Cases_on `k = 0` >> simp[] >> fs[] >> Cases_on `EVEN k` >> fs[]
>- (`!n. k <> 2 * n + 1` by (rpt strip_tac >> fs[EVEN_ADD, EVEN_MULT]) >>
qabbrev_tac `k2 = (k - 1) DIV 2` >>
`k = 2 * k2 + 2` suffices_by rw[] >>
simp[Abbr`k2`, even_lem]) >>
`!n. k <> 2 * n + 2` by (rpt strip_tac >> fs[EVEN_ADD, EVEN_MULT]) >>
qabbrev_tac `k2 = (k - 1) DIV 2` >>
`k = 2 * k2 + 1` suffices_by rw[] >>
simp[Abbr`k2`, odd_lem]))
val lookup_fromList = store_thm(
"lookup_fromList",
``lookup n (fromList l) = if n < LENGTH l then SOME (EL n l)
else NONE``,
simp[fromList_def] >>
`!i n t. lookup n (SND (FOLDL (\ (i,t) a. (i+1,insert i a t)) (i,t) l)) =
if n < i then lookup n t
else if n < LENGTH l + i then SOME (EL (n - i) l)
else lookup n t`
suffices_by (simp[] >> strip_tac >> simp[lookup_def]) >>
Induct_on `l` >> simp[] >> pop_assum kall_tac >>
rw[lookup_insert] >>
full_simp_tac (srw_ss() ++ ARITH_ss) [] >>
`0 < n - i` by simp[] >>
Cases_on `n - i` >> fs[] >>
qmatch_assum_rename_tac `n - i = SUC nn` >>
`nn = n - (i + 1)` by decide_tac >> simp[]);
val bit_cases = prove(
``!n. (n = 0) \/ (?m. n = 2 * m + 1) \/ (?m. n = 2 * m + 2)``,
Induct >> simp[] >> fs[]
>- (disj2_tac >> qexists_tac `m` >> simp[])
>- (disj1_tac >> qexists_tac `SUC m` >> simp[]))
val oddevenlemma = prove(
``2 * y + 1 <> 2 * x + 2``,
disch_then (mp_tac o AP_TERM ``EVEN``) >>
simp[EVEN_ADD, EVEN_MULT]);
val MULT2_DIV' = prove(
``(2 * m DIV 2 = m) /\ ((2 * m + 1) DIV 2 = m)``,
simp[DIV_EQ_X]);
val domain_lookup = store_thm(
"domain_lookup",
``!t k. k IN domain t <=> ?v. lookup k t = SOME v``,
Induct >> simp[domain_def, lookup_def] >> rpt gen_tac >>
qspec_then `k` STRUCT_CASES_TAC bit_cases >>
simp[oddevenlemma, EVEN_ADD, EVEN_MULT,
EQ_MULT_LCANCEL, MULT2_DIV']);
val lookup_inter_alt = store_thm("lookup_inter_alt",
``lookup x (inter t1 t2) =
if x IN domain t2 then lookup x t1 else NONE``,
fs [lookup_inter,domain_lookup]
\\ Cases_on `lookup x t2` \\ fs [] \\ Cases_on `lookup x t1` \\ fs []);
val lookup_NONE_domain = store_thm(
"lookup_NONE_domain",
``(lookup k t = NONE) <=> k NOTIN domain t``,
simp[domain_lookup] >> Cases_on `lookup k t` >> simp[]);
Theorem domain_union[simp]:
domain (union t1 t2) = domain t1 UNION domain t2
Proof
simp[EXTENSION, domain_lookup, lookup_union] >>
qx_gen_tac `k` >> Cases_on `lookup k t1` >> simp[]
QED
Theorem domain_inter[simp]:
domain (inter t1 t2) = domain t1 INTER domain t2
Proof
simp[EXTENSION, domain_lookup, lookup_inter] >>
rw [] >> Cases_on `lookup x t1` >> fs[] >>
BasicProvers.CASE_TAC
QED
Theorem domain_insert[simp]:
domain (insert k v t) = k INSERT domain t
Proof
simp[domain_lookup, EXTENSION, lookup_insert] >>
metis_tac[]
QED
Theorem domain_difference[simp]:
!t1 t2 . domain (difference t1 t2) = (domain t1) DIFF (domain t2)
Proof
simp[EXTENSION, domain_lookup, lookup_difference] >>
rw [] >> Cases_on `lookup x t1` >> fs[] >> Cases_on `lookup x t2` >> rw[]
QED
val domain_sing = save_thm(
"domain_sing",
domain_insert |> Q.INST [`t` |-> `LN`] |> SIMP_RULE bool_ss [domain_def]);
val domain_fromList = store_thm(
"domain_fromList",
``domain (fromList l) = count (LENGTH l)``,
simp[fromList_def] >>
`!i t. domain (SND (FOLDL (\ (i,t) a. (i + 1, insert i a t)) (i,t) l)) =
domain t UNION IMAGE ((+) i) (count (LENGTH l))`
suffices_by (simp[] >> strip_tac >> simp[EXTENSION]) >>
Induct_on `l` >> simp[EXTENSION, EQ_IMP_THM] >>
rpt strip_tac >> simp[DECIDE ``(x = x + y) <=> (y = 0)``] >>
qmatch_assum_rename_tac `nn < SUC (LENGTH l)` >>
Cases_on `nn` >> fs[] >> metis_tac[ADD1]);
val size_domain = Q.store_thm("size_domain",
`!t. size t = CARD (domain t)`,
Induct_on `t`
>- rw[size_def, domain_def]
>- rw[size_def, domain_def]
>> rw[CARD_UNION_EQN, CARD_INJ_IMAGE]
>-
(`IMAGE (\n. 2 * n + 2) (domain t) INTER
IMAGE (\n. 2 * n + 1) (domain t') = {}`
by (rw[GSYM DISJOINT_DEF, IN_DISJOINT]
>> Cases_on `ODD x`
>> fs[ODD_EXISTS, ADD1, oddevenlemma])
>> simp[]) >>
`(({0} INTER IMAGE (\n. 2 * n + 2) (domain t)) = {}) /\
(({0} UNION (IMAGE (\n. 2 * n + 2) (domain t)))
INTER (IMAGE (\n. 2 * n + 1) (domain t')) = {})`
by (rw[GSYM DISJOINT_DEF, IN_DISJOINT]
>> Cases_on `ODD x`
>> fs[ODD_EXISTS, ADD1, oddevenlemma])
>> simp[]);
val ODD_IMP_NOT_ODD = prove(
``!k. ODD k ==> ~(ODD (k-1))``,
Cases >> fs [ODD]);
val lookup_delete = store_thm(
"lookup_delete",
``!t k1 k2.
lookup k1 (delete k2 t) = if k1 = k2 then NONE
else lookup k1 t``,
Induct >> simp[delete_def, lookup_def]
>> rw [lookup_def,lookup_mk_BN,lookup_mk_BS]
>> sg `(k1 - 1) DIV 2 <> (k2 - 1) DIV 2`
>> simp[DIV2_EQ_DIV2, EVEN_PRE]
>> fs [] >> CCONTR_TAC >> fs [] >> srw_tac [] []
>> fs [EVEN_ODD] >> imp_res_tac ODD_IMP_NOT_ODD);
val domain_delete = store_thm(
"domain_delete[simp]",
``domain (delete k t) = domain t DELETE k``,
simp[EXTENSION, domain_lookup, lookup_delete] >>
metis_tac[]);
val foldi_def = Define`
(foldi f i acc LN = acc) /\
(foldi f i acc (LS a) = f i a acc) /\
(foldi f i acc (BN t1 t2) =
let inc = lrnext i
in
foldi f (i + inc) (foldi f (i + 2 * inc) acc t1) t2) /\
(foldi f i acc (BS t1 a t2) =
let inc = lrnext i
in
foldi f (i + inc) (f i a (foldi f (i + 2 * inc) acc t1)) t2)
`;
Definition spt_acc_def:
(spt_acc i 0 = i) /\
(spt_acc i (SUC k) =
spt_acc (i + if EVEN (SUC k) then 2 * lrnext i else lrnext i) (k DIV 2))
Termination WF_REL_TAC`measure SND` \\ simp[DIV_LT_X]
End
val spt_acc_thm = Q.store_thm("spt_acc_thm",
`spt_acc i k = if k = 0 then i else spt_acc (i + if EVEN k then 2 * lrnext i else lrnext i) ((k-1) DIV 2)`,
rw[spt_acc_def] \\ Cases_on`k` \\ fs[spt_acc_def]);
val lemmas = prove(
``(!x. EVEN (2 * x + 2)) /\
(!x. ODD (2 * x + 1))``,
conj_tac >- (
simp[EVEN_EXISTS] >> rw[] >>
qexists_tac`SUC x` >> simp[] ) >>
simp[ODD_EXISTS,ADD1] >>
metis_tac[] )
val bit_induction = prove(
``!P. P 0 /\ (!n. P n ==> P (2 * n + 1)) /\
(!n. P n ==> P (2 * n + 2)) ==>
!n. P n``,
gen_tac >> strip_tac >> completeInduct_on `n` >> simp[] >>
qspec_then `n` strip_assume_tac bit_cases >> simp[]);
val lrnext212 = prove(
``(lrnext (2 * m + 1) = 2 * lrnext m) /\
(lrnext (2 * m + 2) = 2 * lrnext m)``,
conj_tac >| [
`2 * m + 1 = BIT1 m` suffices_by simp[lrnext_thm] >>
simp_tac bool_ss [BIT1, TWO, ONE, MULT_CLAUSES, ADD_CLAUSES],
`2 * m + 2 = BIT2 m` suffices_by simp[lrnext_thm] >>
simp_tac bool_ss [BIT2, TWO, ONE, MULT_CLAUSES, ADD_CLAUSES]
]);
val lrlemma1 = prove(
``lrnext (i + lrnext i) = 2 * lrnext i``,
qid_spec_tac `i` >> ho_match_mp_tac bit_induction >>
simp[lrnext212, lrnext_thm] >> conj_tac
>- (gen_tac >>
`2 * i + (2 * lrnext i + 1) = 2 * (i + lrnext i) + 1`
by decide_tac >> simp[lrnext212]) >>
qx_gen_tac `i` >>
`2 * i + (2 * lrnext i + 2) = 2 * (i + lrnext i) + 2`
by decide_tac >>
simp[lrnext212]);
val lrlemma2 = prove(
``lrnext (i + 2 * lrnext i) = 2 * lrnext i``,
qid_spec_tac `i` >> ho_match_mp_tac bit_induction >>
simp[lrnext212, lrnext_thm] >> conj_tac
>- (qx_gen_tac `i` >>
`2 * i + (4 * lrnext i + 1) = 2 * (i + 2 * lrnext i) + 1`
by decide_tac >> simp[lrnext212]) >>
gen_tac >>
`2 * i + (4 * lrnext i + 2) = 2 * (i + 2 * lrnext i) + 2`
by decide_tac >> simp[lrnext212])
val spt_acc_eqn = Q.store_thm("spt_acc_eqn",
`!k i. spt_acc i k = lrnext i * k + i`,
ho_match_mp_tac bit_induction
\\ rw[]
>- rw[spt_acc_def]
>- (
rw[Once spt_acc_thm]
>- fs[EVEN_ODD,lemmas]
\\ simp[MULT2_DIV']
\\ simp[lrlemma1] )
>- (
ONCE_REWRITE_TAC[spt_acc_thm]
\\ simp[]
\\ reverse(rw[])
>- fs[EVEN_ODD,lemmas]
\\ simp[MULT2_DIV']
\\ simp[lrlemma2]));
val spt_acc_0 = Q.store_thm("spt_acc_0",
`!k. spt_acc 0 k = k`, rw[spt_acc_eqn,lrnext_thm]);
val set_foldi_keys = store_thm(
"set_foldi_keys",
``!t a i. foldi (\k v a. k INSERT a) i a t =
a UNION IMAGE (\n. i + lrnext i * n) (domain t)``,
Induct_on `t` >> simp[foldi_def, GSYM IMAGE_COMPOSE,
combinTheory.o_ABS_R]
>- simp[Once INSERT_SING_UNION, UNION_COMM]
>- (simp[EXTENSION] >> rpt gen_tac >>
Cases_on `x IN a` >> simp[lrlemma1, lrlemma2, LEFT_ADD_DISTRIB]) >>
simp[EXTENSION] >> rpt gen_tac >>
Cases_on `x IN a'` >> simp[lrlemma1, lrlemma2, LEFT_ADD_DISTRIB])
val domain_foldi = save_thm(
"domain_foldi",
set_foldi_keys |> SPEC_ALL |> Q.INST [`i` |-> `0`, `a` |-> `{}`]
|> SIMP_RULE (srw_ss()) [lrnext_thm]
|> SYM);
val _ = computeLib.add_persistent_funs ["domain_foldi"]
Definition mapi0_def[simp]:
(mapi0 f i LN = LN) /\
(mapi0 f i (LS a) = LS (f i a)) /\
(mapi0 f i (BN t1 t2) =
let inc = lrnext i in
mk_BN (mapi0 f (i + 2 * inc) t1) (mapi0 f (i + inc) t2)) /\
(mapi0 f i (BS t1 a t2) =
let inc = lrnext i in
mk_BS (mapi0 f (i + 2 * inc) t1) (f i a) (mapi0 f (i + inc) t2))
End
Definition mapi_def: mapi f pt = mapi0 f 0 pt
End
val lookup_mapi0 = Q.store_thm("lookup_mapi0",
`!pt i k.
lookup k (mapi0 f i pt) =
case lookup k pt of NONE => NONE
| SOME v => SOME (f (spt_acc i k) v)`,
Induct \\ rw[mapi0_def,lookup_def,lookup_mk_BN,lookup_mk_BS] \\ fs[]
\\ TRY (simp[spt_acc_eqn] \\ NO_TAC)
\\ CASE_TAC \\ simp[Once spt_acc_thm,SimpRHS]);
val lookup_mapi = Q.store_thm("lookup_mapi",
`lookup k (mapi f pt) = OPTION_MAP (f k) (lookup k pt)`,
rw[mapi_def,lookup_mapi0,spt_acc_0]
\\ CASE_TAC \\ fs[]);
val toAList_def = Define `
toAList = foldi (\k v a. (k,v)::a) 0 []`
val set_toAList_lemma = prove(
``!t a i. set (foldi (\k v a. (k,v) :: a) i a t) =
set a UNION IMAGE (\n. (i + lrnext i * n,
THE (lookup n t))) (domain t)``,
Induct_on `t`
\\ fs [foldi_def,GSYM IMAGE_COMPOSE,lookup_def]
THEN1 fs [Once INSERT_SING_UNION, UNION_COMM]
THEN1 (simp[EXTENSION] \\ rpt gen_tac \\
Cases_on `MEM x a` \\ simp[lrlemma1, lrlemma2, LEFT_ADD_DISTRIB]
\\ fs [MULT2_DIV',EVEN_ADD,EVEN_DOUBLE])
\\ simp[EXTENSION] \\ rpt gen_tac
\\ Cases_on `MEM x a'` \\ simp[lrlemma1, lrlemma2, LEFT_ADD_DISTRIB]
\\ fs [MULT2_DIV',EVEN_ADD,EVEN_DOUBLE])
|> Q.SPECL [`t`,`[]`,`0`] |> GEN_ALL
|> SIMP_RULE (srw_ss()) [GSYM toAList_def,lrnext_thm,MEM,LIST_TO_SET,
UNION_EMPTY,EXTENSION,
pairTheory.FORALL_PROD]
val MEM_toAList = store_thm("MEM_toAList",
``!t k v. MEM (k,v) (toAList t) <=> (lookup k t = SOME v)``,
fs [set_toAList_lemma,domain_lookup] \\ REPEAT STRIP_TAC
\\ Cases_on `lookup k t` \\ fs []
\\ REPEAT STRIP_TAC \\ EQ_TAC \\ fs []);
val ALOOKUP_toAList = store_thm("ALOOKUP_toAList",
``!t x. ALOOKUP (toAList t) x = lookup x t``,
strip_tac>>strip_tac>>Cases_on `lookup x t` >-
simp[ALOOKUP_FAILS,MEM_toAList] >>
Cases_on`ALOOKUP (toAList t) x`>-
fs[ALOOKUP_FAILS,MEM_toAList] >>
imp_res_tac ALOOKUP_MEM >>
fs[MEM_toAList])
val insert_union = store_thm("insert_union",
``!k v s. insert k v s = union (insert k v LN) s``,
completeInduct_on`k` >> simp[Once insert_def] >> rw[] >>
simp[Once union_def] >>
Cases_on`s`>>simp[Once insert_def] >>
simp[Once union_def] >>
first_x_assum match_mp_tac >>
simp[arithmeticTheory.DIV_LT_X])
val domain_empty = store_thm("domain_empty",
``!t. wf t ==> ((t = LN) <=> (domain t = EMPTY))``,
simp[] >> Induct >> simp[wf_def] >> metis_tac[])
val toAList_append = prove(
``!t n ls.
foldi (\k v a. (k,v)::a) n ls t =
foldi (\k v a. (k,v)::a) n [] t ++ ls``,
Induct
>- simp[foldi_def]
>- simp[foldi_def]
>- (
simp_tac std_ss [foldi_def,LET_THM] >> rpt gen_tac >>
first_assum(fn th =>
CONV_TAC(LAND_CONV(RATOR_CONV(RAND_CONV(REWR_CONV th))))) >>
first_assum(fn th =>
CONV_TAC(LAND_CONV(REWR_CONV th))) >>
first_assum(fn th =>
CONV_TAC(RAND_CONV(LAND_CONV(REWR_CONV th)))) >>
metis_tac[APPEND_ASSOC] ) >>
simp_tac std_ss [foldi_def,LET_THM] >> rpt gen_tac >>
first_assum(fn th =>
CONV_TAC(LAND_CONV(RATOR_CONV(RAND_CONV(RAND_CONV(REWR_CONV th)))))) >>
first_assum(fn th =>
CONV_TAC(LAND_CONV(REWR_CONV th))) >>
first_assum(fn th =>
CONV_TAC(RAND_CONV(LAND_CONV(REWR_CONV th)))) >>
metis_tac[APPEND_ASSOC,APPEND] )
val toAList_inc = prove(
``!t n.
foldi (\k v a. (k,v)::a) n [] t =
MAP (\(k,v). (n + lrnext n * k,v)) (foldi (\k v a. (k,v)::a) 0 [] t)``,
Induct
>- simp[foldi_def]
>- simp[foldi_def]
>- (
simp_tac std_ss [foldi_def,LET_THM] >> rpt gen_tac >>
CONV_TAC(LAND_CONV(REWR_CONV toAList_append)) >>
CONV_TAC(RAND_CONV(RAND_CONV(REWR_CONV toAList_append))) >>
first_assum(fn th =>
CONV_TAC(LAND_CONV(LAND_CONV(REWR_CONV th)))) >>
first_assum(fn th =>
CONV_TAC(LAND_CONV(RAND_CONV(REWR_CONV th)))) >>
first_assum(fn th =>
CONV_TAC(RAND_CONV(RAND_CONV(LAND_CONV(REWR_CONV th))))) >>
first_assum(fn th =>
CONV_TAC(RAND_CONV(RAND_CONV(RAND_CONV(REWR_CONV th))))) >>
rpt(pop_assum kall_tac) >>
simp[MAP_MAP_o,combinTheory.o_DEF,APPEND_11_LENGTH] >>
simp[MAP_EQ_f] >>
simp[lrnext_thm,pairTheory.UNCURRY,pairTheory.FORALL_PROD] >>
simp[lrlemma1,lrlemma2] )
>- (
simp_tac std_ss [foldi_def,LET_THM] >> rpt gen_tac >>
CONV_TAC(LAND_CONV(REWR_CONV toAList_append)) >>
CONV_TAC(RAND_CONV(RAND_CONV(REWR_CONV toAList_append))) >>
first_assum(fn th =>
CONV_TAC(LAND_CONV(LAND_CONV(REWR_CONV th)))) >>
first_assum(fn th =>
CONV_TAC(LAND_CONV(RAND_CONV(RAND_CONV(REWR_CONV th))))) >>
first_assum(fn th =>
CONV_TAC(RAND_CONV(RAND_CONV(LAND_CONV(REWR_CONV th))))) >>
first_assum(fn th =>
CONV_TAC(RAND_CONV(RAND_CONV(RAND_CONV(RAND_CONV(REWR_CONV th)))))) >>
rpt(pop_assum kall_tac) >>
simp[MAP_MAP_o,combinTheory.o_DEF,APPEND_11_LENGTH] >>
simp[MAP_EQ_f] >>
simp[lrnext_thm,pairTheory.UNCURRY,pairTheory.FORALL_PROD] >>
simp[lrlemma1,lrlemma2] ))
val ALL_DISTINCT_MAP_FST_toAList = store_thm("ALL_DISTINCT_MAP_FST_toAList",
``!t. ALL_DISTINCT (MAP FST (toAList t))``,
simp[toAList_def] >>
Induct >> simp[foldi_def] >- (
CONV_TAC(RAND_CONV(RAND_CONV(RATOR_CONV(RAND_CONV(REWR_CONV toAList_inc))))) >>
CONV_TAC(RAND_CONV(RAND_CONV(REWR_CONV toAList_append))) >>
CONV_TAC(RAND_CONV(RAND_CONV(LAND_CONV(REWR_CONV toAList_inc)))) >>
simp[MAP_MAP_o,combinTheory.o_DEF,pairTheory.UNCURRY,lrnext_thm] >>
simp[ALL_DISTINCT_APPEND] >>
rpt conj_tac >- (
qmatch_abbrev_tac`ALL_DISTINCT (MAP f ls)` >>
`MAP f ls = MAP (\x. 2 * x + 1) (MAP FST ls)` by (
simp[MAP_MAP_o,combinTheory.o_DEF,Abbr`f`] ) >>
pop_assum SUBST1_TAC >> qunabbrev_tac`f` >>
match_mp_tac ALL_DISTINCT_MAP_INJ >>
simp[] )
>- (
qmatch_abbrev_tac`ALL_DISTINCT (MAP f ls)` >>
`MAP f ls = MAP (\x. 2 * x + 2) (MAP FST ls)` by (
simp[MAP_MAP_o,combinTheory.o_DEF,Abbr`f`] ) >>
pop_assum SUBST1_TAC >> qunabbrev_tac`f` >>
match_mp_tac ALL_DISTINCT_MAP_INJ >>
simp[] ) >>
simp[MEM_MAP,PULL_EXISTS,pairTheory.EXISTS_PROD] >>
metis_tac[ODD_EVEN,lemmas] ) >>
gen_tac >>
CONV_TAC(RAND_CONV(RAND_CONV(RATOR_CONV(RAND_CONV(RAND_CONV(REWR_CONV toAList_inc)))))) >>
CONV_TAC(RAND_CONV(RAND_CONV(REWR_CONV toAList_append))) >>
CONV_TAC(RAND_CONV(RAND_CONV(LAND_CONV(REWR_CONV toAList_inc)))) >>
simp[MAP_MAP_o,combinTheory.o_DEF,pairTheory.UNCURRY,lrnext_thm] >>
simp[ALL_DISTINCT_APPEND] >>
rpt conj_tac >- (
qmatch_abbrev_tac`ALL_DISTINCT (MAP f ls)` >>
`MAP f ls = MAP (\x. 2 * x + 1) (MAP FST ls)` by (
simp[MAP_MAP_o,combinTheory.o_DEF,Abbr`f`] ) >>
pop_assum SUBST1_TAC >> qunabbrev_tac`f` >>
match_mp_tac ALL_DISTINCT_MAP_INJ >>
simp[] )
>- ( simp[MEM_MAP] )
>- (
qmatch_abbrev_tac`ALL_DISTINCT (MAP f ls)` >>
`MAP f ls = MAP (\x. 2 * x + 2) (MAP FST ls)` by (
simp[MAP_MAP_o,combinTheory.o_DEF,Abbr`f`] ) >>
pop_assum SUBST1_TAC >> qunabbrev_tac`f` >>
match_mp_tac ALL_DISTINCT_MAP_INJ >>
simp[] ) >>
simp[MEM_MAP,PULL_EXISTS,pairTheory.EXISTS_PROD] >>
metis_tac[ODD_EVEN,lemmas] )
Theorem LENGTH_toAList[simp]:
LENGTH (toAList t) = size t
Proof
`LENGTH (toAList t) = LENGTH (MAP FST (toAList t))` by simp[]>>
pop_assum SUBST_ALL_TAC>>
DEP_REWRITE_TAC[GSYM ALL_DISTINCT_CARD_LIST_TO_SET]>>
simp[ALL_DISTINCT_MAP_FST_toAList,size_domain]>>
AP_TERM_TAC>>
rw[pred_setTheory.EXTENSION]>>
simp[MEM_MAP,pairTheory.EXISTS_PROD,MEM_toAList,domain_lookup]
QED
val _ = remove_ovl_mapping "lrnext" {Name = "lrnext", Thy = "sptree"}
val foldi_FOLDR_toAList_lemma = prove(
``!t n a ls. foldi f n (FOLDR (UNCURRY f) a ls) t =