forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiscScript.sml
2674 lines (2306 loc) · 94.9 KB
/
miscScript.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
(*
Miscellaneous definitions and minor lemmas used throughout the
development.
*)
open HolKernel bossLib boolLib boolSimps lcsymtacs Parse libTheory
open optionTheory combinTheory listTheory pred_setTheory finite_mapTheory alistTheory rich_listTheory llistTheory arithmeticTheory pairTheory sortingTheory relationTheory totoTheory comparisonTheory bitTheory sptreeTheory wordsTheory wordsLib set_sepTheory indexedListsTheory stringTheory
ASCIInumbersLib
(* Misc. lemmas (without any compiler constants) *)
val _ = new_theory "misc"
val _ = ParseExtras.temp_tight_equality()
(* this is copied in preamble.sml, but needed here to avoid cyclic dep *)
fun drule th =
first_assum(mp_tac o MATCH_MP (ONCE_REWRITE_RULE[GSYM AND_IMP_INTRO] th))
val rveq = rpt BasicProvers.VAR_EQ_TAC
(* -- *)
(* TODO: move/categorize *)
val _ = numLib.prefer_num();
val IMP_IMP = save_thm("IMP_IMP",METIS_PROVE[]``(P /\ (Q ==> R)) ==> ((P ==> Q) ==> R)``);
val SUBSET_IMP = Q.store_thm("SUBSET_IMP",
`s SUBSET t ==> (x IN s ==> x IN t)`,
fs[pred_setTheory.SUBSET_DEF]);
val fmap_eq_flookup = save_thm("fmap_eq_flookup",FLOOKUP_EXT |> REWRITE_RULE[FUN_EQ_THM]);
val oHD_def = Define`oHD l = case l of [] => NONE | h::_ => SOME h`
val oHD_thm = Q.store_thm("oHD_thm[simp]",
`oHD [] = NONE ∧ oHD (h::t) = SOME h`,
rw[oHD_def]);
val safeTL_def = Define`safeTL [] = [] ∧ safeTL (h::t) = t`
val revdroprev = Q.store_thm("revdroprev",
`∀l n.
n ≤ LENGTH l ⇒ (REVERSE (DROP n (REVERSE l)) = TAKE (LENGTH l - n) l)`,
ho_match_mp_tac listTheory.SNOC_INDUCT >> simp[] >> rpt strip_tac >>
rename1 `n ≤ SUC (LENGTH l)` >>
`n = 0 ∨ ∃m. n = SUC m` by (Cases_on `n` >> simp[]) >> simp[]
>- simp[TAKE_APPEND2] >>
simp[TAKE_APPEND1] >>
`LENGTH l + 1 - SUC m = LENGTH l - m`
suffices_by (disch_then SUBST_ALL_TAC >> simp[]) >>
simp[]);
val revtakerev = Q.store_thm("revtakerev",
`∀n l. n ≤ LENGTH l ⇒ REVERSE (TAKE n (REVERSE l)) = DROP (LENGTH l - n) l`,
Induct >> simp[DROP_LENGTH_NIL] >>
qx_gen_tac `l` >>
`l = [] ∨ ∃f e. l = SNOC e f` by metis_tac[SNOC_CASES] >> simp[] >>
simp[DROP_APPEND1]);
val times_add_o = Q.store_thm("times_add_o",
`(λn:num. k * n + x) = ($+ x) o ($* k)`,
rw[FUN_EQ_THM]);
val SORTED_inv_image_LESS_PLUS = Q.store_thm("SORTED_inv_image_LESS_PLUS",
`SORTED (inv_image $< (arithmetic$+ k)) = SORTED $<`,
simp[FUN_EQ_THM]
\\ Induct
\\ Q.ISPEC_THEN`$+ k`(fn th => simp[MATCH_MP SORTED_EQ th])
(MATCH_MP transitive_inv_image transitive_LESS)
\\ simp[MATCH_MP SORTED_EQ transitive_LESS]);
val SORTED_GENLIST_TIMES = Q.store_thm("SORTED_GENLIST_TIMES",
`0 < k ⇒ ∀n. SORTED prim_rec$< (GENLIST ($* k) n)`,
strip_tac
\\ Induct \\ simp[GENLIST,SNOC_APPEND]
\\ match_mp_tac SORTED_APPEND
\\ simp[MEM_GENLIST,PULL_EXISTS]);
val read_bytearray_def = Define `
(read_bytearray a 0 get_byte = SOME []) /\
(read_bytearray a (SUC n) get_byte =
case get_byte a of
| NONE => NONE
| SOME b => case read_bytearray (a+1w) n get_byte of
| NONE => NONE
| SOME bs => SOME (b::bs))`
val read_bytearray_LENGTH = Q.store_thm("read_bytearray_LENGTH",
`!n a f x.
(read_bytearray a n f = SOME x) ==> (LENGTH x = n)`,
Induct \\ fs [read_bytearray_def] \\ REPEAT STRIP_TAC
\\ BasicProvers.EVERY_CASE_TAC \\ fs [] \\ rw [] \\ res_tac);
val shift_seq_def = Define `
shift_seq k s = \i. s (i + k:num)`;
val SUM_SET_IN_LT = Q.store_thm("SUM_SET_IN_LT",
`!s x y. FINITE s /\ x IN s /\ y < x ==> y < SUM_SET s`,
metis_tac[SUM_SET_IN_LE,LESS_LESS_EQ_TRANS]);
val BIJ_IMP_11 = Q.store_thm("BIJ_IMP_11",
`BIJ f UNIV UNIV ==> !x y. (f x = f y) = (x = y)`,
full_simp_tac(srw_ss())[BIJ_DEF,INJ_DEF] \\ metis_tac []);
val ALOOKUP_MAP_gen = Q.store_thm("ALOOKUP_MAP_gen",
`∀f al x.
ALOOKUP (MAP (λ(x,y). (x,f x y)) al) x =
OPTION_MAP (f x) (ALOOKUP al x)`,
gen_tac >> Induct >> simp[] >>
Cases >> simp[] >> srw_tac[][]);
val FST_EQ_EQUIV = Q.store_thm("FST_EQ_EQUIV",
`(FST x = y) <=> ?z. x = (y,z)`,
Cases_on `x` \\ full_simp_tac(srw_ss())[]);
val map_fromAList = Q.store_thm("map_fromAList",
`map f (fromAList ls) = fromAList (MAP (λ(k,v). (k, f v)) ls)`,
Induct_on`ls` >> simp[fromAList_def] >>
Cases >> simp[fromAList_def] >>
simp[wf_fromAList,map_insert])
val LLOOKUP_def = Define `
(LLOOKUP [] n = NONE) /\
(LLOOKUP (x::xs) n = if n = 0 then SOME x else LLOOKUP xs (n-1:num))`;
val LLOOKUP_EQ_EL = Q.store_thm("LLOOKUP_EQ_EL",
`!xs n y. LLOOKUP xs n = SOME y <=> n < LENGTH xs /\ (y = EL n xs)`,
Induct \\ fs [LLOOKUP_def] \\ rw [] THEN1 metis_tac []
\\ Cases_on `n` \\ fs [ADD1] \\ eq_tac \\ rw []);
val LLOOKUP_THM = Q.store_thm("LLOOKUP_THM",
`!xs n. LLOOKUP xs n = if n < LENGTH xs then SOME (EL n xs) else NONE`,
Induct \\ full_simp_tac(srw_ss())[LLOOKUP_def] \\ srw_tac[][] THEN1 decide_tac
\\ Cases_on `xs` \\ full_simp_tac(srw_ss())[] \\ Cases_on `n` \\ full_simp_tac(srw_ss())[] \\ decide_tac);
val LLOOKUP_DROP = Q.store_thm("LLOOKUP_DROP",
`(LLOOKUP (DROP f xs) n = LLOOKUP xs (f + n))`,
Cases_on `DROP f xs = []` \\ full_simp_tac(srw_ss())[] \\ full_simp_tac(srw_ss())[DROP_NIL]
\\ full_simp_tac(srw_ss())[LLOOKUP_THM] THEN1 decide_tac
\\ `f + n < LENGTH xs <=> n < LENGTH xs - f` by decide_tac \\ full_simp_tac(srw_ss())[]
\\ srw_tac[][] \\ ONCE_REWRITE_TAC [ADD_COMM]
\\ match_mp_tac (GSYM EL_DROP) \\ decide_tac);
val LLOOKUP_TAKE_IMP = Q.store_thm("LLOOKUP_TAKE_IMP",
`(LLOOKUP (TAKE f xs) n = SOME x) ==>
(LLOOKUP xs n = SOME x)`,
simp[LLOOKUP_THM,LENGTH_TAKE_EQ] >>
srw_tac[ARITH_ss][]
\\ match_mp_tac (GSYM EL_TAKE)
\\ fsrw_tac[ARITH_ss][]);
val LLOOKUP_LUPDATE = Q.store_thm("LLOOKUP_LUPDATE",
`!xs i n x. LLOOKUP (LUPDATE x i xs) n =
if i <> n then LLOOKUP xs n else
if i < LENGTH xs then SOME x else NONE`,
Induct \\ full_simp_tac(srw_ss())[LLOOKUP_def,LUPDATE_def]
\\ Cases_on `i` \\ full_simp_tac(srw_ss())[LLOOKUP_def,LUPDATE_def]
\\ rpt strip_tac \\ srw_tac[][] \\ full_simp_tac(srw_ss())[] \\ `F` by decide_tac);
val _ = Datatype `
app_list = List ('a list) | Append app_list app_list | Nil`
val append_aux_def = Define `
(append_aux Nil aux = aux) /\
(append_aux (List xs) aux = xs ++ aux) /\
(append_aux (Append l1 l2) aux = append_aux l1 (append_aux l2 aux))`;
val append_def = Define `
append l = append_aux l []`;
val append_aux_thm = Q.store_thm("append_aux_thm",
`!l xs. append_aux l xs = append_aux l [] ++ xs`,
Induct \\ metis_tac [APPEND,APPEND_ASSOC,append_aux_def]);
val append_thm = Q.store_thm("append_thm[simp]",
`append (Append l1 l2) = append l1 ++ append l2 /\
append (List xs) = xs /\
append Nil = []`,
fs [append_def,append_aux_def]
\\ once_rewrite_tac [append_aux_thm] \\ fs []);
val SmartAppend_def = Define`
(SmartAppend Nil l2 = l2) ∧
(SmartAppend l1 Nil = l1) ∧
(SmartAppend l1 l2 = Append l1 l2)`;
val _ = export_rewrites["SmartAppend_def"];
val SmartAppend_thm = Q.store_thm("SmartAppend_thm",
`∀l1 l2.
SmartAppend l1 l2 =
if l1 = Nil then l2 else
if l2 = Nil then l1 else Append l1 l2`,
Cases \\ Cases \\ rw[]);
val append_SmartAppend = Q.store_thm("append_SmartAppend[simp]",
`append (SmartAppend l1 l2) = append l1 ++ append l2`,
rw[append_def,SmartAppend_thm,append_aux_def]
\\ rw[Once append_aux_thm]);
val GENLIST_eq_MAP = Q.store_thm("GENLIST_eq_MAP",
`GENLIST f n = MAP g ls ⇔
LENGTH ls = n ∧ ∀m. m < n ⇒ f m = g (EL m ls)`,
srw_tac[][LIST_EQ_REWRITE,EQ_IMP_THM,EL_MAP])
val GENLIST_ID = Q.store_thm("GENLIST_ID",
`!x. GENLIST (\i. EL i x) (LENGTH x) = x`,
HO_MATCH_MP_TAC SNOC_INDUCT
\\ full_simp_tac(srw_ss())[] \\ simp_tac std_ss [GENLIST,GSYM ADD1]
\\ full_simp_tac(srw_ss())[SNOC_APPEND,rich_listTheory.EL_LENGTH_APPEND]
\\ rpt strip_tac \\ once_rewrite_tac [EQ_SYM_EQ]
\\ pop_assum (fn th => simp_tac std_ss [Once (GSYM th)])
\\ full_simp_tac(srw_ss())[GENLIST_FUN_EQ] \\ srw_tac[][]
\\ match_mp_tac (GSYM rich_listTheory.EL_APPEND1) \\ full_simp_tac(srw_ss())[]);
val ZIP_GENLIST1 = Q.store_thm("ZIP_GENLIST1",
`∀l f n. LENGTH l = n ⇒ ZIP (GENLIST f n,l) = GENLIST (λx. (f x, EL x l)) n`,
Induct \\ rw[] \\ rw[GENLIST_CONS,o_DEF]);
val MAP2i_def = Define`
(MAP2i f [] [] = []) /\
(MAP2i f (h1::t1) (h2::t2) = f 0 h1 h2::MAP2i (f o SUC) t1 t2)`;
val _ = export_rewrites["MAP2i_def"];
val MAP2i_ind = theorem"MAP2i_ind";
val LENGTH_MAP2i = Q.store_thm("LENGTH_MAP2i[simp]",
`∀f l1 l2. LENGTH l1 = LENGTH l2 ⇒ LENGTH (MAP2i f l1 l2) = LENGTH l2`,
ho_match_mp_tac MAP2i_ind \\ rw[]);
val EL_MAP2i = Q.store_thm("EL_MAP2i",
`∀f l1 l2 n. n < LENGTH l1 ∧ n < LENGTH l2 ⇒
EL n (MAP2i f l1 l2) = f n (EL n l1) (EL n l2)`,
ho_match_mp_tac MAP2i_ind \\ rw[]
\\ Cases_on`n` \\ fs[]);
val MAP3_def = Define`
(MAP3 f [] [] [] = []) /\
(MAP3 f (h1::t1) (h2::t2) (h3::t3) = f h1 h2 h3::MAP3 f t1 t2 t3)`;
val _ = export_rewrites["MAP3_def"];
val MAP3_ind = theorem"MAP3_ind";
val LENGTH_MAP3 = Q.store_thm("LENGTH_MAP3[simp]",
`∀f l1 l2 l3. LENGTH l1 = LENGTH l3 /\ LENGTH l2 = LENGTH l3 ⇒ LENGTH (MAP3 f l1 l2 l3) = LENGTH l3`,
ho_match_mp_tac MAP3_ind \\ rw[]);
val EL_MAP3 = Q.store_thm("EL_MAP3",
`∀f l1 l2 l3 n. n < LENGTH l1 ∧ n < LENGTH l2 ∧ n < LENGTH l3 ⇒
EL n (MAP3 f l1 l2 l3) = f (EL n l1) (EL n l2) (EL n l3)`,
ho_match_mp_tac MAP3_ind \\ rw[]
\\ Cases_on`n` \\ fs[]);
val LENGTH_TAKE_EQ_MIN = Q.store_thm("LENGTH_TAKE_EQ_MIN",
`!n xs. LENGTH (TAKE n xs) = MIN n (LENGTH xs)`,
simp[LENGTH_TAKE_EQ] \\ full_simp_tac(srw_ss())[MIN_DEF] \\ decide_tac);
val hd_drop = Q.store_thm ("hd_drop",
`!n l. n < LENGTH l ⇒ HD (DROP n l) = EL n l`,
Induct_on `l` >>
srw_tac[][DROP_def] >>
`n - 1 < LENGTH l` by decide_tac >>
res_tac >>
`0 < n` by decide_tac >>
srw_tac[][EL_CONS] >>
`n - 1 = PRE n` by decide_tac >>
srw_tac[][]);
val INJ_EXTEND = Q.store_thm("INJ_EXTEND",
`INJ b s t /\ ~(x IN s) /\ ~(y IN t) ==>
INJ ((x =+ y) b) (x INSERT s) (y INSERT t)`,
full_simp_tac(srw_ss())[INJ_DEF,combinTheory.APPLY_UPDATE_THM] \\ METIS_TAC []);
val MEM_LIST_REL = Q.store_thm("MEM_LIST_REL",
`!xs ys P x. LIST_REL P xs ys /\ MEM x xs ==> ?y. MEM y ys /\ P x y`,
Induct \\ Cases_on `ys` \\ full_simp_tac(srw_ss())[] \\ REPEAT STRIP_TAC \\ full_simp_tac(srw_ss())[]
\\ RES_TAC \\ METIS_TAC []);
val LIST_REL_MEM = Q.store_thm("LIST_REL_MEM",
`!xs ys P. LIST_REL P xs ys <=>
LIST_REL (\x y. MEM x xs /\ MEM y ys ==> P x y) xs ys`,
full_simp_tac(srw_ss())[LIST_REL_EL_EQN] \\ METIS_TAC [MEM_EL]);
val LIST_REL_REVERSE_EQ =
IMP_ANTISYM_RULE
(EVERY2_REVERSE |> SPEC_ALL)
(EVERY2_REVERSE |> Q.SPECL[`R`,`REVERSE l1`,`REVERSE l2`]
|> SIMP_RULE std_ss [REVERSE_REVERSE])
|> SYM |> curry save_thm"LIST_REL_REVERSE_EQ";
val LIST_REL_GENLIST_I = Q.store_thm("LIST_REL_GENLIST_I",
`!xs. LIST_REL P (GENLIST I (LENGTH xs)) xs =
!n. n < LENGTH xs ==> P n (EL n xs)`,
HO_MATCH_MP_TAC SNOC_INDUCT
\\ FULL_SIMP_TAC (srw_ss()) [LENGTH,GENLIST,SNOC_APPEND]
\\ FULL_SIMP_TAC std_ss [LIST_REL_APPEND_SING]
\\ REPEAT STRIP_TAC \\ EQ_TAC \\ REPEAT STRIP_TAC THEN1
(Cases_on `n < LENGTH xs`
\\ FULL_SIMP_TAC std_ss [rich_listTheory.EL_APPEND1]
\\ `n = LENGTH xs` by DECIDE_TAC
\\ FULL_SIMP_TAC std_ss [rich_listTheory.EL_APPEND2,EL,HD])
THEN1 (`n < SUC (LENGTH xs)` by DECIDE_TAC \\ RES_TAC
\\ POP_ASSUM MP_TAC \\ Q.PAT_X_ASSUM `!x.bb` (K ALL_TAC)
\\ FULL_SIMP_TAC std_ss [rich_listTheory.EL_APPEND1])
\\ POP_ASSUM (MP_TAC o Q.SPEC `LENGTH xs`)
\\ FULL_SIMP_TAC std_ss [rich_listTheory.EL_APPEND2,EL,HD]);
val LIST_REL_lookup_fromList = Q.store_thm("LIST_REL_lookup_fromList",
`LIST_REL (\v x. lookup v (fromList args) = SOME x)
(GENLIST I (LENGTH args)) args`,
SIMP_TAC std_ss [lookup_fromList,LIST_REL_GENLIST_I]);
val lookup_fromList_outside = Q.store_thm("lookup_fromList_outside",
`!k. LENGTH args <= k ==> (lookup k (fromList args) = NONE)`,
SIMP_TAC std_ss [lookup_fromList] \\ DECIDE_TAC);
val lemmas = Q.prove(
`(2 + 2 * n - 1 = 2 * n + 1:num) /\
(2 + 2 * n' = 2 * n'' + 2 <=> n' = n'':num) /\
(2 * m = 2 * n <=> (m = n)) /\
((2 * n'' + 1) DIV 2 = n'') /\
((2 * n) DIV 2 = n) /\
(2 + 2 * n' <> 2 * n'' + 1) /\
(2 * m + 1 <> 2 * n' + 2)`,
REPEAT STRIP_TAC \\ SIMP_TAC std_ss []
THEN1 DECIDE_TAC
THEN1 DECIDE_TAC
THEN1 DECIDE_TAC
\\ full_simp_tac(srw_ss())[ONCE_REWRITE_RULE [MULT_COMM] MULT_DIV]
\\ full_simp_tac(srw_ss())[ONCE_REWRITE_RULE [MULT_COMM] DIV_MULT]
\\ IMP_RES_TAC (METIS_PROVE [] ``(m = n) ==> (m MOD 2 = n MOD 2)``)
\\ POP_ASSUM MP_TAC \\ SIMP_TAC std_ss []
\\ ONCE_REWRITE_TAC [MATCH_MP (GSYM MOD_PLUS) (DECIDE ``0 < 2:num``)]
\\ EVAL_TAC \\ full_simp_tac(srw_ss())[MOD_EQ_0,ONCE_REWRITE_RULE [MULT_COMM] MOD_EQ_0]);
val IN_domain = Q.store_thm("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))`,
full_simp_tac(srw_ss())[domain_def] \\ REPEAT STRIP_TAC
\\ Cases_on `n = 0` \\ full_simp_tac(srw_ss())[]
\\ Cases_on `EVEN n` \\ full_simp_tac(srw_ss())[]
\\ full_simp_tac(srw_ss())[GSYM ODD_EVEN]
\\ IMP_RES_TAC EVEN_ODD_EXISTS
\\ full_simp_tac(srw_ss())[ADD1] \\ full_simp_tac(srw_ss())[lemmas]
\\ Cases_on `m` \\ full_simp_tac(srw_ss())[MULT_CLAUSES]
\\ REPEAT STRIP_TAC \\ EQ_TAC \\ REPEAT STRIP_TAC
\\ full_simp_tac(srw_ss())[lemmas])
val map_map_K = Q.store_thm("map_map_K",
`!t. map (K a) (map (K a) t) = map (K a) t`,
Induct \\ full_simp_tac(srw_ss())[map_def]);
val lookup_map_K = Q.store_thm("lookup_map_K",
`!t n. lookup n (map (K x) t) = if n IN domain t then SOME x else NONE`,
Induct \\ full_simp_tac(srw_ss())[IN_domain,map_def,lookup_def]
\\ REPEAT STRIP_TAC \\ Cases_on `n = 0` \\ full_simp_tac(srw_ss())[]
\\ Cases_on `EVEN n` \\ full_simp_tac(srw_ss())[]);
val lookup_any_def = Define `
lookup_any x sp d =
case lookup x sp of
| NONE => d
| SOME m => m`;
val alist_insert_def = Define `
(alist_insert [] xs t = t) /\
(alist_insert vs [] t = t) /\
(alist_insert (v::vs) (x::xs) t = insert v x (alist_insert vs xs t))`
val lookup_alist_insert = Q.store_thm("lookup_alist_insert",
`!x y t z. LENGTH x = LENGTH y ==>
(lookup z (alist_insert x y t) =
case ALOOKUP (ZIP(x,y)) z of SOME a => SOME a | NONE => lookup z t)`,
ho_match_mp_tac (fetch "-" "alist_insert_ind")>>
srw_tac[][]>-
(Cases_on`y`>>
full_simp_tac(srw_ss())[LENGTH,alist_insert_def]) >>
Cases_on`z=x`>>
srw_tac[][lookup_def,alist_insert_def]>>
full_simp_tac(srw_ss())[lookup_insert])
val domain_alist_insert = Q.store_thm("domain_alist_insert",
`!a b locs. LENGTH a = LENGTH b ==>
domain (alist_insert a b locs) = domain locs UNION set a`,
Induct_on`a`>>Cases_on`b`>>full_simp_tac(srw_ss())[alist_insert_def]>>srw_tac[][]>>
metis_tac[INSERT_UNION_EQ,UNION_COMM])
val fromList2_def = Define `
fromList2 l = SND (FOLDL (\(i,t) a. (i + 2,insert i a t)) (0,LN) l)`
val EVEN_fromList2_lemma = Q.prove(
`!l n t.
EVEN n /\ (!x. x IN domain t ==> EVEN x) ==>
!x. x IN domain (SND (FOLDL (\(i,t) a. (i + 2,insert i a t)) (n,t) l)) ==> EVEN x`,
Induct \\ full_simp_tac(srw_ss())[FOLDL] \\ REPEAT STRIP_TAC \\ full_simp_tac(srw_ss())[PULL_FORALL]
\\ FIRST_X_ASSUM (MP_TAC o Q.SPECL [`n+2`,`insert n h t`,`x`])
\\ full_simp_tac(srw_ss())[] \\ SRW_TAC [] [] \\ POP_ASSUM MATCH_MP_TAC
\\ REPEAT STRIP_TAC \\ full_simp_tac(srw_ss())[] \\ full_simp_tac(srw_ss())[EVEN_EXISTS]
\\ Q.EXISTS_TAC `SUC m` \\ DECIDE_TAC);
val EVEN_fromList2 = Q.store_thm("EVEN_fromList2",
`!l n. n IN domain (fromList2 l) ==> EVEN n`,
ASSUME_TAC (EVEN_fromList2_lemma
|> Q.SPECL [`l`,`0`,`LN`]
|> SIMP_RULE (srw_ss()) [GSYM fromList2_def]
|> GEN_ALL) \\ full_simp_tac(srw_ss())[]);
val SUBMAP_FDOM_SUBSET = Q.store_thm("SUBMAP_FDOM_SUBSET",
`f1 ⊑ f2 ⇒ FDOM f1 ⊆ FDOM f2`,
srw_tac[][SUBMAP_DEF,SUBSET_DEF])
val SUBMAP_FRANGE_SUBSET = Q.store_thm("SUBMAP_FRANGE_SUBSET",
`f1 ⊑ f2 ⇒ FRANGE f1 ⊆ FRANGE f2`,
srw_tac[][SUBMAP_DEF,SUBSET_DEF,IN_FRANGE] >> metis_tac[])
val FDIFF_def = Define `
FDIFF f1 s = DRESTRICT f1 (COMPL s)`;
val FDOM_FDIFF = Q.store_thm("FDOM_FDIFF",
`x IN FDOM (FDIFF refs f2) <=> x IN FDOM refs /\ ~(x IN f2)`,
full_simp_tac(srw_ss())[FDIFF_def,DRESTRICT_DEF]);
val INJ_FAPPLY_FUPDATE = Q.store_thm("INJ_FAPPLY_FUPDATE",
`INJ ($' f) (FDOM f) (FRANGE f) ∧
s = k INSERT FDOM f ∧ v ∉ FRANGE f ∧
t = v INSERT FRANGE f
⇒
INJ ($' (f |+ (k,v))) s t`,
srw_tac[][INJ_DEF,FAPPLY_FUPDATE_THM] >> srw_tac[][] >>
pop_assum mp_tac >> srw_tac[][] >>
full_simp_tac(srw_ss())[IN_FRANGE] >>
METIS_TAC[])
val NUM_NOT_IN_FDOM =
MATCH_MP IN_INFINITE_NOT_FINITE (CONJ INFINITE_NUM_UNIV
(Q.ISPEC `f:num|->'a` FDOM_FINITE))
|> SIMP_RULE std_ss [IN_UNIV]
|> curry save_thm "NUM_NOT_IN_FDOM";
val EXISTS_NOT_IN_FDOM_LEMMA = Q.prove(
`?x. ~(x IN FDOM (refs:num|->'a))`,
METIS_TAC [NUM_NOT_IN_FDOM]);
val LEAST_NOTIN_FDOM = Q.store_thm("LEAST_NOTIN_FDOM",
`(LEAST ptr. ptr NOTIN FDOM (refs:num|->'a)) NOTIN FDOM refs`,
ASSUME_TAC (EXISTS_NOT_IN_FDOM_LEMMA |>
SIMP_RULE std_ss [whileTheory.LEAST_EXISTS]) \\ full_simp_tac(srw_ss())[]);
val list_to_num_set_def = Define `
(list_to_num_set [] = LN) /\
(list_to_num_set (n::ns) = insert n () (list_to_num_set ns))`;
val list_insert_def = Define `
(list_insert [] t = t) /\
(list_insert (n::ns) t = list_insert ns (insert n () t))`;
val domain_list_to_num_set = Q.store_thm("domain_list_to_num_set",
`!xs. x IN domain (list_to_num_set xs) <=> MEM x xs`,
Induct \\ full_simp_tac(srw_ss())[list_to_num_set_def]);
val domain_list_insert = Q.store_thm("domain_list_insert",
`!xs x t.
x IN domain (list_insert xs t) <=> MEM x xs \/ x IN domain t`,
Induct \\ full_simp_tac(srw_ss())[list_insert_def] \\ METIS_TAC []);
val domain_FOLDR_delete = Q.store_thm("domain_FOLDR_delete",
`∀ls live. domain (FOLDR delete live ls) =
(domain live) DIFF (set ls)`,
Induct>>
full_simp_tac(srw_ss())[DIFF_INSERT,EXTENSION]>>
metis_tac[])
val lookup_list_to_num_set = Q.store_thm("lookup_list_to_num_set",
`!xs. lookup x (list_to_num_set xs) = if MEM x xs then SOME () else NONE`,
Induct \\ srw_tac [] [list_to_num_set_def,lookup_def,lookup_insert] \\ full_simp_tac(srw_ss())[]);
val OPTION_BIND_SOME = Q.store_thm("OPTION_BIND_SOME",
`∀f. OPTION_BIND f SOME = f`,
Cases >> simp[])
val take1 = Q.store_thm ("take1",
`!l. l ≠ [] ⇒ TAKE 1 l = [EL 0 l]`,
Induct_on `l` >> srw_tac[][]);
val take1_drop = Q.store_thm (
"take1_drop",
`!n l. n < LENGTH l ==> (TAKE 1 (DROP n l) = [EL n l])`,
Induct_on `l` \\ rw[] \\
Cases_on `n` \\ rw[EL_restricted]
);
val SPLIT_LIST = Q.store_thm("SPLIT_LIST",
`!xs.
?ys zs. (xs = ys ++ zs) /\
(LENGTH xs DIV 2 = LENGTH ys)`,
REPEAT STRIP_TAC
\\ Q.LIST_EXISTS_TAC [`TAKE (LENGTH xs DIV 2) xs`,`DROP (LENGTH xs DIV 2) xs`]
\\ REPEAT STRIP_TAC \\ full_simp_tac(srw_ss())[TAKE_DROP]
\\ MATCH_MP_TAC (GSYM LENGTH_TAKE)
\\ full_simp_tac(srw_ss())[DIV_LE_X] \\ DECIDE_TAC);
val EXISTS_ZIP = Q.store_thm ("EXISTS_ZIP",
`!l f. EXISTS (\(x,y). f x) l = EXISTS f (MAP FST l)`,
Induct_on `l` >>
srw_tac[][] >>
Cases_on `h` >>
full_simp_tac(srw_ss())[] >>
metis_tac []);
val EVERY_ZIP = Q.store_thm ("EVERY_ZIP",
`!l f. EVERY (\(x,y). f x) l = EVERY f (MAP FST l)`,
Induct_on `l` >>
srw_tac[][] >>
Cases_on `h` >>
full_simp_tac(srw_ss())[] >>
metis_tac []);
val ZIP_MAP_FST_SND_EQ = Q.store_thm("ZIP_MAP_FST_SND_EQ",
`∀ls. ZIP (MAP FST ls,MAP SND ls) = ls`,
Induct>>full_simp_tac(srw_ss())[])
val tlookup_def = Define `
tlookup m k = case lookup m k of NONE => 0:num | SOME k => k`;
val any_el_def = Define `
(any_el n [] d = d) /\
(any_el n (x::xs) d = if n = 0 then x else any_el (n-1:num) xs d)`
val list_max_def = Define `
(list_max [] = 0:num) /\
(list_max (x::xs) =
let m = list_max xs in
if m < x then x else m)`
val list_inter_def = Define `
list_inter xs ys = FILTER (\y. MEM y xs) ys`;
val SING_HD = Q.store_thm("SING_HD",
`(([HD xs] = xs) <=> (LENGTH xs = 1)) /\
((xs = [HD xs]) <=> (LENGTH xs = 1))`,
Cases_on `xs` \\ full_simp_tac(srw_ss())[LENGTH_NIL] \\ METIS_TAC []);
val ALOOKUP_SNOC = Q.store_thm("ALOOKUP_SNOC",
`∀ls p k. ALOOKUP (SNOC p ls) k =
case ALOOKUP ls k of SOME v => SOME v |
NONE => if k = FST p then SOME (SND p) else NONE`,
Induct >> simp[] >>
Cases >> simp[] >> srw_tac[][])
val ALOOKUP_GENLIST = Q.store_thm("ALOOKUP_GENLIST",
`∀f n k. ALOOKUP (GENLIST (λi. (i,f i)) n) k = if k < n then SOME (f k) else NONE`,
gen_tac >> Induct >> simp[GENLIST] >> srw_tac[][] >> full_simp_tac(srw_ss())[ALOOKUP_SNOC] >>
srw_tac[][] >> fsrw_tac[ARITH_ss][])
val ALOOKUP_ZIP_FAIL = Q.store_thm("ALOOKUP_ZIP_FAIL",
`∀A B x.
LENGTH A = LENGTH B ⇒
(ALOOKUP (ZIP (A,B)) x = NONE ⇔ ¬MEM x A)`,
srw_tac[][]>>Q.ISPECL_THEN [`ZIP(A,B)`,`x`] assume_tac ALOOKUP_NONE >>
full_simp_tac(srw_ss())[MAP_ZIP])
val anub_def = Define`
(anub [] acc = []) ∧
(anub ((k,v)::ls) acc =
if MEM k acc then anub ls acc else
(k,v)::(anub ls (k::acc)))`
val anub_ind = theorem"anub_ind"
val EVERY_anub_imp = Q.store_thm("EVERY_anub_imp",
`∀ls acc x y.
EVERY P (anub ((x,y)::ls) acc) ∧ x ∉ set acc
⇒
P (x,y) ∧ EVERY P (anub ls (x::acc))`,
ho_match_mp_tac anub_ind >> srw_tac[][anub_def] >>
full_simp_tac(srw_ss())[MEM_MAP,PULL_EXISTS,FORALL_PROD,EXISTS_PROD])
val ALOOKUP_anub = Q.store_thm("ALOOKUP_anub",
`ALOOKUP (anub ls acc) k =
if MEM k acc then ALOOKUP (anub ls acc) k
else ALOOKUP ls k`,
qid_spec_tac`acc` >>
Induct_on`ls` >>
srw_tac[][anub_def] >>
Cases_on`h`>>srw_tac[][anub_def]>>full_simp_tac(srw_ss())[] >- (
first_x_assum(qspec_then`acc`mp_tac) >>
srw_tac[][] ) >>
first_x_assum(qspec_then`q::acc`mp_tac) >>
srw_tac[][])
val anub_eq_nil = Q.store_thm("anub_eq_nil",
`anub x y = [] ⇔ EVERY (combin$C MEM y) (MAP FST x)`,
qid_spec_tac`y` >>
Induct_on`x`>>srw_tac[][anub_def]>>
Cases_on`h`>>srw_tac[][anub_def])
val EVERY_anub_suff = Q.store_thm("EVERY_anub_suff",
`∀ls acc.
(∀x. ¬MEM x acc ⇒ case ALOOKUP ls x of SOME v => P (x,v) | NONE => T)
⇒ EVERY P (anub ls acc)`,
Induct >> simp[anub_def] >>
Cases >> simp[anub_def] >> srw_tac[][] >- (
first_x_assum(match_mp_tac) >>
srw_tac[][] >>
res_tac >>
pop_assum mp_tac >> IF_CASES_TAC >> full_simp_tac(srw_ss())[] )
>- (
res_tac >> full_simp_tac(srw_ss())[] ) >>
first_x_assum match_mp_tac >>
srw_tac[][] >> res_tac >> full_simp_tac(srw_ss())[] >>
`q ≠ x` by full_simp_tac(srw_ss())[] >> full_simp_tac(srw_ss())[])
val anub_notin_acc = Q.store_thm("anub_notin_acc",
`∀ls acc. MEM x acc ⇒ ¬MEM x (MAP FST (anub ls acc))`,
Induct >> simp[anub_def] >>
Cases >> simp[anub_def] >> srw_tac[][] >>
metis_tac[])
val anub_tl_anub = Q.store_thm("anub_tl_anub",
`∀x y h t. anub x y = h::t ⇒ ∃a b. t = anub a b ∧ set a ⊆ set x ∧ set b ⊆ set ((FST h)::y)`,
Induct >> srw_tac[][anub_def] >>
Cases_on`h`>>full_simp_tac(srw_ss())[anub_def] >>
pop_assum mp_tac >> srw_tac[][] >>
res_tac >> srw_tac[][] >>
full_simp_tac(srw_ss())[SUBSET_DEF] >>
metis_tac[MEM] )
val anub_all_distinct_keys = Q.store_thm("anub_all_distinct_keys",
`∀ls acc.
ALL_DISTINCT acc ⇒
ALL_DISTINCT ((MAP FST (anub ls acc)) ++ acc)`,
Induct>>srw_tac[][anub_def]>>PairCases_on`h`>>full_simp_tac(srw_ss())[anub_def]>>
srw_tac[][]>>
`ALL_DISTINCT (h0::acc)` by full_simp_tac(srw_ss())[ALL_DISTINCT]>>res_tac>>
full_simp_tac(srw_ss())[ALL_DISTINCT_APPEND]>>
metis_tac[])
val MEM_anub_ALOOKUP = Q.store_thm("MEM_anub_ALOOKUP",
`MEM (k,v) (anub ls []) ⇒
ALOOKUP ls k = SOME v`,
srw_tac[][]>>
Q.ISPECL_THEN[`ls`,`[]`] assume_tac anub_all_distinct_keys>>
Q.ISPECL_THEN [`ls`,`k`,`[]`] assume_tac (GEN_ALL ALOOKUP_anub)>>
full_simp_tac(srw_ss())[]>>
metis_tac[ALOOKUP_ALL_DISTINCT_MEM])
val FEMPTY_FUPDATE_EQ = Q.store_thm("FEMPTY_FUPDATE_EQ",
`∀x y. (FEMPTY |+ x = FEMPTY |+ y) ⇔ (x = y)`,
Cases >> Cases >> srw_tac[][fmap_eq_flookup,FDOM_FUPDATE,FLOOKUP_UPDATE] >>
Cases_on`q=q'`>>srw_tac[][] >- (
srw_tac[][EQ_IMP_THM] >>
pop_assum(qspec_then`q`mp_tac) >> srw_tac[][] ) >>
qexists_tac`q`>>srw_tac[][])
val FUPDATE_LIST_EQ_FEMPTY = Q.store_thm("FUPDATE_LIST_EQ_FEMPTY",
`∀fm ls. fm |++ ls = FEMPTY ⇔ fm = FEMPTY ∧ ls = []`,
srw_tac[][EQ_IMP_THM,FUPDATE_LIST_THM] >>
full_simp_tac(srw_ss())[GSYM fmap_EQ_THM,FDOM_FUPDATE_LIST])
val IS_SOME_EXISTS = Q.store_thm("IS_SOME_EXISTS",
`∀opt. IS_SOME opt ⇔ ∃x. opt = SOME x`,
Cases >> simp[])
val _ = type_abbrev("num_set",``:unit spt``);
val _ = type_abbrev("num_map",``:'a spt``);
val toAList_domain = Q.store_thm("toAList_domain",`
∀x. MEM x (MAP FST (toAList t)) ⇔ x ∈ domain t`,
full_simp_tac(srw_ss())[EXISTS_PROD,MEM_MAP,MEM_toAList,domain_lookup])
val domain_nat_set_from_list = Q.store_thm("domain_nat_set_from_list",
`∀ls ns. domain (FOLDL (λs n. insert n () s) ns ls) = domain ns ∪ set ls`,
Induct >> simp[sptreeTheory.domain_insert] >>
srw_tac[][EXTENSION] >> metis_tac[])
val _ = export_rewrites["domain_nat_set_from_list"]
val wf_nat_set_from_list = Q.store_thm("wf_nat_set_from_list",
`∀ls ns. wf ns ⇒ wf (FOLDL (λs n. insert n z s) ns ls)`,
Induct >> simp[] >> srw_tac[][sptreeTheory.wf_insert])
val BIT_11 = Q.store_thm("BIT_11",
`∀n m. (BIT n = BIT m) ⇔ (n = m)`,
simp[EQ_IMP_THM] >>
Induct >> simp[BIT0_ODD,FUN_EQ_THM] >- (
Cases >> simp[] >>
qexists_tac`1` >> simp[GSYM BIT_DIV2,BIT_ZERO] ) >>
simp[GSYM BIT_DIV2] >>
Cases >> simp[GSYM BIT_DIV2] >- (
qexists_tac`1` >>
simp[BIT_ZERO] >>
simp[BIT_def,BITS_THM] ) >>
srw_tac[][] >>
first_x_assum MATCH_MP_TAC >>
simp[FUN_EQ_THM] >>
gen_tac >>
first_x_assum(qspec_then`x*2`mp_tac) >>
simp[arithmeticTheory.MULT_DIV])
val BIT_11_2 = Q.store_thm("BIT_11_2",
`∀n m. (∀z. (z < 2 ** (MAX n m)) ⇒ (BIT n z ⇔ BIT m z)) ⇔ (n = m)`,
simp[Once EQ_IMP_THM] >>
Induct >- (
simp[] >>
Cases >> simp[] >>
qexists_tac`2 ** SUC n - 1` >>
simp[BIT_EXP_SUB1] ) >>
Cases >> simp[] >- (
qexists_tac`2 ** SUC n - 1` >>
simp[BIT_EXP_SUB1] ) >>
strip_tac >>
first_x_assum MATCH_MP_TAC >>
qx_gen_tac`z` >>
first_x_assum(qspec_then`z*2`mp_tac) >>
simp[GSYM BIT_DIV2,arithmeticTheory.MULT_DIV] >>
srw_tac[][] >> first_x_assum MATCH_MP_TAC >>
full_simp_tac(srw_ss())[arithmeticTheory.MAX_DEF] >>
srw_tac[][] >> full_simp_tac(srw_ss())[] >>
simp[arithmeticTheory.EXP])
val binary_induct = Q.store_thm("binary_induct",
`∀P. P (0:num) ∧ (∀n. P n ⇒ P (2*n) ∧ P (2*n+1)) ⇒ ∀n. P n`,
gen_tac >> strip_tac >>
completeInduct_on`n` >>
Cases_on`n=0`>>simp[]>>
`n DIV 2 < n ∧ ((n = 2 * (n DIV 2)) ∨ (n = 2 * (n DIV 2) + 1))` by (
simp[DIV_MULT_THM2] >>
`n MOD 2 < 2` by (
MATCH_MP_TAC arithmeticTheory.MOD_LESS >>
simp[] ) >>
simp[] ) >>
metis_tac[])
val BIT_TIMES2 = Q.store_thm("BIT_TIMES2",
`BIT z (2 * n) ⇔ 0 < z ∧ BIT (PRE z) n`,
Cases_on`z`>>simp[]>-(
simp[BIT0_ODD] >>
simp[arithmeticTheory.ODD_EVEN] >>
simp[arithmeticTheory.EVEN_DOUBLE] ) >>
qmatch_rename_tac`BIT (SUC z) (2 * n) ⇔ BIT z n` >>
qspecl_then[`z`,`n`,`1`]mp_tac BIT_SHIFT_THM >>
simp[arithmeticTheory.ADD1])
val BIT_TIMES2_1 = Q.store_thm("BIT_TIMES2_1",
`∀n z. BIT z (2 * n + 1) ⇔ (z=0) ∨ BIT z (2 * n)`,
Induct >> simp_tac std_ss [] >- (
simp_tac std_ss [BIT_ZERO] >>
Cases_on`z`>>simp_tac std_ss [BIT0_ODD] >>
simp_tac arith_ss [GSYM BIT_DIV2,BIT_ZERO] ) >>
Cases >> simp_tac std_ss [BIT0_ODD] >- (
simp_tac std_ss [arithmeticTheory.ODD_EXISTS,arithmeticTheory.ADD1] >>
metis_tac[] ) >>
simp_tac std_ss [GSYM BIT_DIV2] >>
qspec_then`2`mp_tac arithmeticTheory.ADD_DIV_RWT >>
simp[] >>
disch_then(qspecl_then[`2 * SUC n`,`1`]mp_tac) >>
simp_tac std_ss [] >>
simp_tac std_ss [arithmeticTheory.MOD_EQ_0_DIVISOR] >>
metis_tac[] )
val LOG2_TIMES2 = Q.store_thm("LOG2_TIMES2",
`0 < n ⇒ (LOG2 (2 * n) = SUC (LOG2 n))`,
srw_tac[][LOG2_def] >>
qspecl_then[`1`,`2`,`n`]mp_tac logrootTheory.LOG_EXP >>
simp[arithmeticTheory.ADD1])
val LOG2_TIMES2_1 = Q.store_thm("LOG2_TIMES2_1",
`∀n. 0 < n ⇒ (LOG2 (2 * n + 1) = LOG2 (2 * n))`,
srw_tac[][LOG2_def] >>
MATCH_MP_TAC logrootTheory.LOG_UNIQUE >>
simp[GSYM LOG2_def,LOG2_TIMES2] >>
simp[arithmeticTheory.EXP] >>
conj_tac >- (
MATCH_MP_TAC arithmeticTheory.LESS_EQ_TRANS >>
qexists_tac`2*n` >> simp[] >>
qspec_then`n`mp_tac logrootTheory.LOG_MOD >>
simp[] >> strip_tac >>
qmatch_assum_abbrev_tac`n = X` >>
qsuff_tac`2 ** LOG2 n ≤ X` >- srw_tac[][] >>
qunabbrev_tac`X` >>
simp[LOG2_def] ) >>
simp[GSYM arithmeticTheory.ADD1] >>
match_mp_tac arithmeticTheory.LESS_NOT_SUC >>
`4:num = 2 * 2` by simp[] >>
pop_assum SUBST1_TAC >>
REWRITE_TAC[Once (GSYM arithmeticTheory.MULT_ASSOC)] >>
simp[] >>
conj_asm1_tac >- (
qspec_then`n`mp_tac logrootTheory.LOG_MOD >>
simp[] >> strip_tac >>
qmatch_assum_abbrev_tac`n = X` >>
qsuff_tac`X < 2 * 2 ** LOG2 n` >- srw_tac[][] >>
qunabbrev_tac`X` >>
simp[LOG2_def] >>
qmatch_abbrev_tac`(a:num) + b < 2 * a` >>
qsuff_tac`n MOD a < a` >- simp[] >>
MATCH_MP_TAC arithmeticTheory.MOD_LESS >>
simp[Abbr`a`] ) >>
qmatch_abbrev_tac`X ≠ Y` >>
qsuff_tac`EVEN X ∧ ODD Y` >- metis_tac[arithmeticTheory.EVEN_ODD] >>
conj_tac >- (
simp[Abbr`X`,arithmeticTheory.EVEN_EXISTS] >>
qexists_tac`2 * 2 ** LOG2 n` >>
simp[] ) >>
simp[Abbr`Y`,arithmeticTheory.ODD_EXISTS] >>
metis_tac[])
val C_BIT_11 = Q.store_thm("C_BIT_11",
`∀n m. (∀z. (z ≤ LOG2 (MAX n m)) ⇒ (BIT z n ⇔ BIT z m)) ⇔ (n = m)`,
simp_tac std_ss [Once EQ_IMP_THM] >>
ho_match_mp_tac binary_induct >>
simp_tac std_ss [] >>
conj_tac >- (
Cases >> simp_tac arith_ss [] >>
qexists_tac`LOG2 (SUC n)` >>
simp_tac arith_ss [BIT_LOG2,BIT_ZERO] ) >>
gen_tac >> strip_tac >>
simp_tac std_ss [BIT_TIMES2,BIT_TIMES2_1] >>
srw_tac[][] >- (
Cases_on`n=0`>>full_simp_tac std_ss []>-(
Cases_on`m=0`>>full_simp_tac std_ss []>>
first_x_assum(qspec_then`LOG2 m`mp_tac)>>simp_tac std_ss [BIT_ZERO] >>
full_simp_tac std_ss [BIT_LOG2]) >>
`¬ODD m` by (
simp_tac std_ss [SYM BIT0_ODD] >>
first_x_assum(qspec_then`0`mp_tac) >>
simp_tac std_ss [] ) >>
full_simp_tac std_ss [arithmeticTheory.ODD_EVEN] >>
full_simp_tac std_ss [arithmeticTheory.EVEN_EXISTS] >>
simp_tac std_ss [arithmeticTheory.EQ_MULT_LCANCEL] >>
first_x_assum MATCH_MP_TAC >>
srw_tac[][] >>
first_x_assum(qspec_then`SUC z`mp_tac) >>
impl_tac >- (
full_simp_tac std_ss [arithmeticTheory.MAX_DEF] >>
srw_tac[][] >> full_simp_tac arith_ss [LOG2_TIMES2] ) >>
simp_tac std_ss [BIT_TIMES2] ) >>
Cases_on`n=0`>>full_simp_tac std_ss []>-(
full_simp_tac std_ss [BIT_ZERO] >>
Cases_on`m=0`>>full_simp_tac std_ss [BIT_ZERO] >>
Cases_on`m=1`>>full_simp_tac std_ss []>>
first_x_assum(qspec_then`LOG2 m`mp_tac) >>
full_simp_tac std_ss [arithmeticTheory.MAX_DEF,BIT_LOG2] >>
spose_not_then strip_assume_tac >>
qspec_then`m`mp_tac logrootTheory.LOG_MOD >>
full_simp_tac arith_ss [GSYM LOG2_def] ) >>
`ODD m` by (
simp_tac std_ss [SYM BIT0_ODD] >>
first_x_assum(qspec_then`0`mp_tac) >>
simp_tac std_ss [] ) >>
full_simp_tac std_ss [arithmeticTheory.ODD_EXISTS,arithmeticTheory.ADD1] >>
simp_tac std_ss [arithmeticTheory.EQ_MULT_LCANCEL] >>
first_x_assum MATCH_MP_TAC >>
srw_tac[][] >>
first_x_assum(qspec_then`SUC z`mp_tac) >>
impl_tac >- (
full_simp_tac std_ss [arithmeticTheory.MAX_DEF] >>
srw_tac[][] >> full_simp_tac arith_ss [LOG2_TIMES2_1,LOG2_TIMES2] ) >>
full_simp_tac arith_ss [BIT_TIMES2_1,BIT_TIMES2])
val BIT_num_from_bin_list_leading = Q.store_thm("BIT_num_from_bin_list_leading",
`∀l x. EVERY ($> 2) l ∧ LENGTH l ≤ x ⇒ ¬BIT x (num_from_bin_list l)`,
simp[numposrepTheory.num_from_bin_list_def] >>
srw_tac[][] >>
MATCH_MP_TAC NOT_BIT_GT_TWOEXP >>
MATCH_MP_TAC arithmeticTheory.LESS_LESS_EQ_TRANS >>
qexists_tac`2 ** LENGTH l` >>
simp[numposrepTheory.l2n_lt] )
val word_bit_test = Q.store_thm("word_bit_test",
`word_bit n w <=> ((w && n2w (2 ** n)) <> 0w:'a word)`,
srw_tac [wordsLib.WORD_BIT_EQ_ss, boolSimps.CONJ_ss]
[wordsTheory.word_index, DECIDE ``0n < d ==> (n <= d - 1) = (n < d)``])
val least_from_def = Define`
least_from P n = if (∃x. P x ∧ n ≤ x) then $LEAST (λx. P x ∧ n ≤ x) else $LEAST P`
val LEAST_thm = Q.store_thm("LEAST_thm",
`$LEAST P = least_from P 0`,
srw_tac[][least_from_def,ETA_AX])
val least_from_thm = Q.store_thm("least_from_thm",
`least_from P n = if P n then n else least_from P (n+1)`,
srw_tac[][least_from_def] >>
numLib.LEAST_ELIM_TAC >> srw_tac[][] >> full_simp_tac(srw_ss())[] >> res_tac >>
TRY(metis_tac[arithmeticTheory.LESS_OR_EQ]) >- (
numLib.LEAST_ELIM_TAC >> srw_tac[][] >> full_simp_tac(srw_ss())[] >- metis_tac[] >>
qmatch_rename_tac`a = b` >>
`n ≤ b` by DECIDE_TAC >>
Cases_on`b < a` >-metis_tac[] >>
spose_not_then strip_assume_tac >>
`a < b` by DECIDE_TAC >>
`¬(n + 1 ≤ a)` by metis_tac[] >>
`a = n` by DECIDE_TAC >>
full_simp_tac(srw_ss())[] )
>- (
Cases_on`n+1≤x`>-metis_tac[]>>
`x = n` by DECIDE_TAC >>
full_simp_tac(srw_ss())[] )
>- (
`¬(n ≤ x)` by metis_tac[] >>
`x = n` by DECIDE_TAC >>
full_simp_tac(srw_ss())[] ))
val FILTER_F = Q.store_thm("FILTER_F",
`∀ls. FILTER (λx. F) ls = []`,
Induct >> simp[])
val _ = export_rewrites["FILTER_F"]
val OPTREL_SOME = Q.store_thm("OPTREL_SOME",
`(!R x y. OPTREL R (SOME x) y <=> (?z. y = SOME z /\ R x z)) /\
(!R x y. OPTREL R x (SOME y) <=> (?z. x = SOME z /\ R z y))`,
srw_tac[][optionTheory.OPTREL_def])
val LIST_REL_O = Q.store_thm("LIST_REL_O",
`∀R1 R2 l1 l2. LIST_REL (R1 O R2) l1 l2 ⇔ ∃l3. LIST_REL R2 l1 l3 ∧ LIST_REL R1 l3 l2`,
rpt gen_tac >>
simp[EVERY2_EVERY,EVERY_MEM,EQ_IMP_THM,GSYM AND_IMP_INTRO,MEM_ZIP,PULL_EXISTS,O_DEF] >>
srw_tac[][] >- (
full_simp_tac(srw_ss())[GSYM RIGHT_EXISTS_IMP_THM,SKOLEM_THM] >>
qexists_tac`GENLIST f (LENGTH l2)` >>
simp[MEM_ZIP,PULL_EXISTS] ) >>
metis_tac[])
val OPTREL_O_lemma = Q.prove(
`∀R1 R2 l1 l2. OPTREL (R1 O R2) l1 l2 ⇔ ∃l3. OPTREL R2 l1 l3 ∧ OPTREL R1 l3 l2`,
srw_tac[][optionTheory.OPTREL_def,EQ_IMP_THM,O_DEF,PULL_EXISTS] >> metis_tac[])
val OPTREL_O = Q.store_thm("OPTREL_O",
`∀R1 R2. OPTREL (R1 O R2) = OPTREL R1 O OPTREL R2`,
srw_tac[][FUN_EQ_THM,OPTREL_O_lemma,O_DEF])
val FUNPOW_mono = Q.store_thm("FUNPOW_mono",
`(∀x y. R1 x y ⇒ R2 x y) ∧
(∀R1 R2. (∀x y. R1 x y ⇒ R2 x y) ⇒ ∀x y. f R1 x y ⇒ f R2 x y) ⇒
∀n x y. FUNPOW f n R1 x y ⇒ FUNPOW f n R2 x y`,
strip_tac >> Induct >> simp[] >>
simp[arithmeticTheory.FUNPOW_SUC] >>
first_x_assum match_mp_tac >> srw_tac[][])
val OPTREL_trans = Q.store_thm("OPTREL_trans",
`∀R x y z. (∀a b c. (x = SOME a) ∧ (y = SOME b) ∧ (z = SOME c) ∧ R a b ∧ R b c ⇒ R a c)
∧ OPTREL R x y ∧ OPTREL R y z ⇒ OPTREL R x z`,
srw_tac[][optionTheory.OPTREL_def])
val UPDATE_LIST_def = Define`
UPDATE_LIST = FOLDL (combin$C (UNCURRY UPDATE))`
val _ = Parse.add_infix("=++",500,Parse.LEFT)
val _ = Parse.overload_on("=++",``UPDATE_LIST``)
val UPDATE_LIST_THM = Q.store_thm("UPDATE_LIST_THM",
`∀f. (f =++ [] = f) ∧ ∀h t. (f =++ (h::t) = (FST h =+ SND h) f =++ t)`,
srw_tac[][UPDATE_LIST_def,pairTheory.UNCURRY])
val APPLY_UPDATE_LIST_ALOOKUP = Q.store_thm("APPLY_UPDATE_LIST_ALOOKUP",
`∀ls f x. (f =++ ls) x = case ALOOKUP (REVERSE ls) x of NONE => f x | SOME y => y`,
Induct >> simp[UPDATE_LIST_THM,ALOOKUP_APPEND] >>
Cases >> simp[combinTheory.APPLY_UPDATE_THM] >>
srw_tac[][] >> BasicProvers.CASE_TAC)
val IS_SUFFIX_CONS = Q.store_thm("IS_SUFFIX_CONS",
`∀l1 l2 a. IS_SUFFIX l1 l2 ⇒ IS_SUFFIX (a::l1) l2`,
srw_tac[][rich_listTheory.IS_SUFFIX_APPEND] >>
qexists_tac`a::l` >>srw_tac[][])
val INFINITE_INJ_NOT_SURJ = Q.store_thm("INFINITE_INJ_NOT_SURJ",
`∀s. INFINITE s ⇔ (s ≠ ∅) ∧ (∃f. INJ f s s ∧ ¬SURJ f s s)`,
srw_tac[][EQ_IMP_THM] >- (
PROVE_TAC[INFINITE_INHAB,MEMBER_NOT_EMPTY] )
>- (
full_simp_tac(srw_ss())[infinite_num_inj] >>
qexists_tac`λx. if ∃n. x = f n then f (SUC (LEAST n. x = f n)) else x` >>
conj_asm1_tac >- (
full_simp_tac(srw_ss())[INJ_IFF] >>
conj_asm1_tac >- srw_tac[][] >>
srw_tac[][] >- (
numLib.LEAST_ELIM_TAC >>
conj_tac >- PROVE_TAC[] >>
srw_tac[][] ) >>
numLib.LEAST_ELIM_TAC >>
srw_tac[][] >>
metis_tac[] ) >>
full_simp_tac(srw_ss())[SURJ_DEF,INJ_IFF] >>
qexists_tac`f 0` >>
simp[] >>
srw_tac[][] >>
metis_tac[]) >>
full_simp_tac(srw_ss())[SURJ_DEF] >- (full_simp_tac(srw_ss())[INJ_IFF] >> metis_tac[]) >>
simp[infinite_num_inj] >>
qexists_tac`λn. FUNPOW f n x` >>
simp[INJ_IFF] >>
conj_asm1_tac >- (
Induct >>
simp[arithmeticTheory.FUNPOW_SUC] >>
full_simp_tac(srw_ss())[INJ_IFF] ) >>
Induct >> simp[] >- (
Cases >> simp[arithmeticTheory.FUNPOW_SUC] >>
metis_tac[] ) >>
Cases >> simp[arithmeticTheory.FUNPOW_SUC] >> full_simp_tac(srw_ss())[INJ_IFF] >>