forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiscScript.sml
4378 lines (3898 loc) · 122 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 Parse libTheory mp_then
open alignmentTheory alistTheory arithmeticTheory bitstringTheory bagTheory
byteTheory combinTheory dep_rewrite containerTheory listTheory
pred_setTheory finite_mapTheory rich_listTheory llistTheory optionTheory
pairTheory sortingTheory relationTheory totoTheory comparisonTheory
bitTheory sptreeTheory wordsTheory wordsLib set_sepTheory BasicProvers
indexedListsTheory stringTheory ASCIInumbersLib machine_ieeeTheory
local open bagLib addressTheory blastLib in end
(* Misc. lemmas (without any compiler constants) *)
val _ = new_theory "misc"
val _ = ParseExtras.tight_equality()
(* Note: This globally hides constants over the reals that gets imported through machine_ieeeTheory *)
val _ = remove_ovl_mapping "max" {Name="max", Thy="realax"}
val _ = remove_ovl_mapping "min" {Name="min", Thy="realax"}
val _ = remove_ovl_mapping "pos" {Name="pos", Thy="real"}
val _ = remove_ovl_mapping "abs" {Name="abs", Thy="realax"}
val _ = remove_ovl_mapping "inf" {Name="inf", Thy="real"}
(* 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
val match_exists_tac = part_match_exists_tac (hd o strip_conj)
val asm_exists_tac = first_assum(match_exists_tac o concl)
(* -- *)
(* TODO: move/categorize *)
val _ = numLib.prefer_num();
(* theorem behind impl_tac *)
val IMP_IMP = save_thm("IMP_IMP",METIS_PROVE[]``(P /\ (Q ==> R)) ==> ((P ==> Q) ==> R)``);
(* used elsewhere in cakeml *)
Theorem SUBSET_IMP:
s SUBSET t ==> (x IN s ==> x IN t)
Proof
fs[pred_setTheory.SUBSET_DEF]
QED
Theorem DROP_NIL:
∀n xs. DROP n xs = [] ⇔ LENGTH xs ≤ n
Proof
Induct \\ Cases_on ‘xs’ \\ fs [DROP_def]
QED
Theorem revdroprev:
∀l n.
n ≤ LENGTH l ⇒ (REVERSE (DROP n (REVERSE l)) = TAKE (LENGTH l - n) l)
Proof
fs [GSYM BUTLASTN_def,BUTLASTN_TAKE]
QED
Theorem revtakerev:
∀n l. n ≤ LENGTH l ⇒ REVERSE (TAKE n (REVERSE l)) = DROP (LENGTH l - n) l
Proof
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]
QED
Theorem times_add_o:
(λn:num. k * n + x) = ($+ x) o ($* k)
Proof
rw[FUN_EQ_THM]
QED
Theorem SORTED_inv_image_LESS_PLUS:
SORTED (inv_image $< (arithmetic$+ k)) = SORTED $<
Proof
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]
QED
Theorem SORTED_GENLIST_TIMES:
0 < k ⇒ ∀n. SORTED prim_rec$< (GENLIST ($* k) n)
Proof
strip_tac
\\ Induct \\ simp[GENLIST,SNOC_APPEND]
\\ simp[MEM_GENLIST,PULL_EXISTS,SORTED_APPEND]
QED
(* this is
read_bytearray a c gb = OPT_MMAP gb (GENLIST (λi. a + n2w i) c)
*)
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))`
(* HOL to have OPT_MMAP f l1 = SOME l2 ==> (LENGTH l2 = LENGTH l1) *)
Theorem read_bytearray_LENGTH:
!n a f x.
(read_bytearray a n f = SOME x) ==> (LENGTH x = n)
Proof
Induct \\ fs [read_bytearray_def] \\ REPEAT STRIP_TAC
\\ BasicProvers.EVERY_CASE_TAC \\ fs [] \\ rw [] \\ res_tac
QED
val shift_seq_def = Define `
shift_seq k s = \i. s (i + k:num)`;
(* TODO: Used once in all of CakeML: could probably be pushed back to use-site*)
Theorem SUM_SET_IN_LT:
!s x y. FINITE s /\ x IN s /\ y < x ==> y < SUM_SET s
Proof
metis_tac[SUM_SET_IN_LE,LESS_LESS_EQ_TRANS]
QED
(* only used in proof of tlookup_bij_iff *)
Theorem CARD_IMAGE_ID_BIJ:
∀s. FINITE s ⇒ (∀x. x ∈ s ⇒ f x ∈ s) ∧ CARD (IMAGE f s) = CARD s ⇒ BIJ f s s
Proof
rw[]
\\ `SURJ f s s` suffices_by metis_tac[FINITE_SURJ_BIJ]
\\ rw[IMAGE_SURJ]
\\ `IMAGE f s ⊆ s` by metis_tac[SUBSET_DEF,IN_IMAGE]
\\ metis_tac[SUBSET_EQ_CARD,IMAGE_FINITE]
QED
(* never used *)
Theorem CARD_IMAGE_EQ_BIJ:
∀s. FINITE s ⇒ CARD (IMAGE f s) = CARD s ⇒ BIJ f s (IMAGE f s)
Proof
rw[]
\\ `SURJ f s (IMAGE f s)` suffices_by metis_tac[FINITE_SURJ_BIJ]
\\ rw[IMAGE_SURJ]
QED
(* used only in clos_callProof -
HOL has DISJOINT_IMAGE:
|- (!x y. f x = f y <=> x = y) ==>
(DISJOINT (IMAGE f x) (IMAGE f y) <=> DISJOINT x y
*)
Theorem DISJOINT_IMAGE_SUC:
DISJOINT (IMAGE SUC x) (IMAGE SUC y) <=> DISJOINT x y
Proof
fs [IN_DISJOINT] \\ metis_tac [DECIDE ``(SUC n = SUC m) <=> (m = n)``]
QED
(* disgusting and used only in clos_callProof *)
Theorem IMAGE_SUC_SUBSET_UNION:
IMAGE SUC x SUBSET IMAGE SUC y UNION IMAGE SUC z <=>
x SUBSET y UNION z
Proof
fs [SUBSET_DEF] \\ metis_tac [DECIDE ``(SUC n = SUC m) <=> (m = n)``]
QED
Overload LLOOKUP = “λl n. oEL n l”
val LLOOKUP_def = save_thm("LLOOKUP_def", listTheory.oEL_def);
val LLOOKUP_EQ_EL = save_thm("LLOOKUP_EQ_EL", listTheory.oEL_EQ_EL);
val LLOOKUP_THM = save_thm("LLOOKUP_THM", listTheory.oEL_THM);
val LLOOOKUP_DROP = save_thm("LLOOKUP_DROP", listTheory.oEL_DROP);
val LLOOKUP_TAKE_IMP = save_thm("LLOOKUP_TAKE_IMP", listTheory.oEL_TAKE_E);
val LLOOKUP_LUPDATE = save_thm("LLOOKUP_LUPDATE", listTheory.oEL_LUPDATE);
(* app_list stuff should be in an app_list theory *)
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 []`;
Theorem append_aux_thm:
!l xs. append_aux l xs = append_aux l [] ++ xs
Proof
Induct \\ metis_tac [APPEND,APPEND_ASSOC,append_aux_def]
QED
Theorem append_thm[simp]:
append (Append l1 l2) = append l1 ++ append l2 /\
append (List xs) = xs /\
append Nil = []
Proof
fs [append_def,append_aux_def]
\\ once_rewrite_tac [append_aux_thm] \\ fs []
QED
val SmartAppend_def = Define`
(SmartAppend Nil l2 = l2) ∧
(SmartAppend l1 Nil = l1) ∧
(SmartAppend l1 l2 = Append l1 l2)`;
val _ = export_rewrites["SmartAppend_def"];
Theorem SmartAppend_thm:
∀l1 l2.
SmartAppend l1 l2 =
if l1 = Nil then l2 else
if l2 = Nil then l1 else Append l1 l2
Proof
Cases \\ Cases \\ rw[]
QED
Theorem append_SmartAppend[simp]:
append (SmartAppend l1 l2) = append l1 ++ append l2
Proof
rw[append_def,SmartAppend_thm,append_aux_def]
\\ rw[Once append_aux_thm]
QED
(* instant derivation from LIST_EQ_REWRITE *)
Theorem GENLIST_eq_MAP:
GENLIST f n = MAP g ls ⇔
LENGTH ls = n ∧ ∀m. m < n ⇒ f m = g (EL m ls)
Proof
srw_tac[][LIST_EQ_REWRITE,EQ_IMP_THM,EL_MAP]
QED
(* TODO - already in HOL as ZIP_GENLIST *)
Theorem ZIP_GENLIST1:
∀l f n. LENGTH l = n ⇒ ZIP (GENLIST f n,l) = GENLIST (λx. (f x, EL x l)) n
Proof
Induct \\ rw[] \\ rw[GENLIST_CONS,o_DEF]
QED
(* MAP3 never used *)
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";
Theorem LENGTH_MAP3[simp]:
∀f l1 l2 l3. LENGTH l1 = LENGTH l3 /\ LENGTH l2 = LENGTH l3 ⇒ LENGTH (MAP3 f l1 l2 l3) = LENGTH l3
Proof
ho_match_mp_tac MAP3_ind \\ rw[]
QED
Theorem 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)
Proof
ho_match_mp_tac MAP3_ind \\ rw[]
\\ Cases_on`n` \\ fs[]
QED
(* used once *)
Theorem MAP_REVERSE_STEP:
∀x f. x ≠ [] ⇒ MAP f (REVERSE x) = f (LAST x) :: MAP f (REVERSE (FRONT x))
Proof
recInduct SNOC_INDUCT
\\ rw [FRONT_APPEND]
QED
(* used three times, once with MIN_DEF alongside, which turns it into
LENGTH_TAKE_EQ
*)
Theorem LENGTH_TAKE_EQ_MIN:
!n xs. LENGTH (TAKE n xs) = MIN n (LENGTH xs)
Proof
simp[LENGTH_TAKE_EQ] \\ full_simp_tac(srw_ss())[MIN_DEF] \\ decide_tac
QED
(* should be switched in orientation; looks like an attempt to get congruence
rule *)
Theorem 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
Proof
full_simp_tac(srw_ss())[LIST_REL_EL_EQN] \\ METIS_TAC [MEM_EL]
QED
(* only used in theorem immediately below *)
Theorem LIST_REL_GENLIST_I:
!xs. LIST_REL P (GENLIST I (LENGTH xs)) xs =
!n. n < LENGTH xs ==> P n (EL n xs)
Proof
simp[LIST_REL_EL_EQN]
QED
(* only used in bvi_to_dataProof *)
Theorem LIST_REL_lookup_fromList:
LIST_REL (\v x. lookup v (fromList args) = SOME x)
(GENLIST I (LENGTH args)) args
Proof
SIMP_TAC std_ss [lookup_fromList,LIST_REL_GENLIST_I]
QED
Theorem LIST_REL_lookup_fromList_MAP:
LIST_REL (λv x. ∃z. lookup v (fromList args) = SOME z ∧ x = f z)
(GENLIST I (LENGTH args)) (MAP f args)
Proof
fs [LIST_REL_MAP2,LIST_REL_GENLIST_I,lookup_fromList]
QED
(* only used in examples/stackProg; oriented badly *)
Theorem LIST_REL_FRONT_LAST:
l1 <> [] /\ l2 <> [] ==>
(LIST_REL A l1 l2 <=>
LIST_REL A (FRONT l1) (FRONT l2) /\ A (LAST l1) (LAST l2))
Proof
map_every
(fn q => Q.ISPEC_THEN q FULL_STRUCT_CASES_TAC SNOC_CASES >>
fs[LIST_REL_SNOC])
[`l1`,`l2`]
QED
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)`,
intLib.ARITH_TAC);
val lookup_any_def = Define `
lookup_any x sp d =
case lookup x sp of
| NONE => d
| SOME m => m`;
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);
Theorem EVEN_fromList2:
!l n. n IN domain (fromList2 l) ==> EVEN n
Proof
ASSUME_TAC (EVEN_fromList2_lemma
|> Q.SPECL [`l`,`0`,`LN`]
|> SIMP_RULE (srw_ss()) [GSYM fromList2_def]
|> GEN_ALL) \\ full_simp_tac(srw_ss())[]
QED
Theorem SUBMAP_mono_FUPDATE_LIST:
∀ls f g.
DRESTRICT f (COMPL (set (MAP FST ls))) ⊑
DRESTRICT g (COMPL (set (MAP FST ls)))
⇒ f |++ ls ⊑ g |++ ls
Proof
Induct \\ rw[FUPDATE_LIST_THM, DRESTRICT_UNIV]
\\ first_x_assum MATCH_MP_TAC
\\ Cases_on`h`
\\ fs[SUBMAP_FLOOKUP_EQN]
\\ rw[] \\ fs[FLOOKUP_DRESTRICT, FLOOKUP_UPDATE]
\\ rw[] \\ fs[]
\\ METIS_TAC[]
QED
Theorem 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
Proof
srw_tac[][INJ_DEF,FAPPLY_FUPDATE_THM] >> srw_tac[][] >>
pop_assum mp_tac >> srw_tac[][] >>
full_simp_tac(srw_ss())[IN_FRANGE] >>
METIS_TAC[]
QED
(* used in only one place: stack_to_labProof *)
Theorem BIJ_FLOOKUP_MAP_KEYS:
BIJ bij UNIV UNIV ==>
FLOOKUP (MAP_KEYS (LINV bij UNIV) f) n = FLOOKUP f (bij n)
Proof
fs [FLOOKUP_DEF,MAP_KEYS_def,BIJ_DEF] \\ strip_tac
\\ match_mp_tac (METIS_PROVE []
``x=x'/\(x /\ x' ==> y=y') ==> (if x then y else z) = (if x' then y' else z)``)
\\ fs [] \\ rw []
THEN1 (eq_tac \\ rw [] \\ metis_tac [BIJ_LINV_INV,BIJ_DEF,IN_UNIV,LINV_DEF])
\\ `BIJ (LINV bij UNIV) UNIV UNIV` by metis_tac [BIJ_LINV_BIJ,BIJ_DEF]
\\ `INJ (LINV bij UNIV) (FDOM f) UNIV` by fs [INJ_DEF,IN_UNIV,BIJ_DEF]
\\ fs [MAP_KEYS_def] \\ metis_tac [BIJ_LINV_INV,BIJ_DEF,IN_UNIV,LINV_DEF]
QED
Theorem SPLIT_LIST:
!xs.
?ys zs. (xs = ys ++ zs) /\
(LENGTH xs DIV 2 = LENGTH ys)
Proof
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
QED
Theorem EXISTS_ZIP:
!l f. EXISTS (\(x,y). f x) l = EXISTS f (MAP FST l)
Proof
Induct_on `l` >>
srw_tac[][] >>
Cases_on `h` >>
full_simp_tac(srw_ss())[] >>
metis_tac []
QED
Theorem EVERY_ZIP:
!l f. EVERY (\(x,y). f x) l = EVERY f (MAP FST l)
Proof
Induct_on `l` >>
srw_tac[][] >>
Cases_on `h` >>
full_simp_tac(srw_ss())[] >>
metis_tac []
QED
Theorem every_zip_split:
!l1 l2 P Q.
LENGTH l1 = LENGTH l2 ⇒
(EVERY (\(x,y). P x ∧ Q y) (ZIP (l1, l2)) ⇔ EVERY P l1 ∧ EVERY Q l2)
Proof
Induct_on `l1`
>> simp []
>> Cases_on `l2`
>> rw []
>> metis_tac []
QED
Theorem every_shim:
!l P. EVERY (\(x,y). P y) l = EVERY P (MAP SND l)
Proof
Induct_on `l` >>
rw [] >>
PairCases_on `h` >>
rw []
QED
Theorem every_shim2:
!l P Q. EVERY (\(x,y). P x ∧ Q y) l = (EVERY (\x. P (FST x)) l ∧ EVERY (\x. Q (SND x)) l)
Proof
Induct_on `l` >>
rw [] >>
PairCases_on `h` >>
rw [] >>
metis_tac []
QED
Theorem MEM_ZIP2:
∀l1 l2 x.
MEM x (ZIP (l1,l2)) ⇒
∃n. n < LENGTH l1 ∧ n < LENGTH l2 ∧ x = (EL n l1,EL n l2)
Proof
Induct>>fs[ZIP_def]>>
Cases_on`l2`>>fs[ZIP_def]>>
rw[]
>-
(qexists_tac`0n`>>fs[])
>>
first_x_assum drule>>
rw[]>>
qexists_tac`SUC n`>>fs[]
QED
Theorem ZIP_MAP_FST_SND_EQ:
∀ls. ZIP (MAP FST ls,MAP SND ls) = ls
Proof
Induct>>full_simp_tac(srw_ss())[]
QED
Theorem MAP_FST_I_PAIR_MAP[simp]:
!xs. MAP FST (MAP (I ## f) xs) = MAP FST xs
Proof
Induct \\ fs [FORALL_PROD]
QED
Theorem EVERY_FST_SND:
EVERY (λ(a,b). P a ∧ Q b) ls ⇔ EVERY P (MAP FST ls) ∧ EVERY Q (MAP SND ls)
Proof
rw[EVERY_MEM,MEM_MAP,UNCURRY,EXISTS_PROD,FORALL_PROD,PULL_EXISTS]
\\ metis_tac[]
QED
val zlookup_def = Define `
zlookup m k = case lookup k m of NONE => 0n | SOME k => k`;
val tlookup_def = Define `
tlookup m k = case lookup k m of NONE => k | SOME k => k`;
Theorem tlookup_id:
x ∉ domain names
⇒ tlookup names x = x
Proof
rw[tlookup_def]
\\ fs[domain_lookup] \\ CASE_TAC \\ fs[]
QED
Theorem tlookup_bij_suff:
set (toList names) = domain names ⇒
BIJ (tlookup names) UNIV UNIV
Proof
strip_tac
\\ match_mp_tac BIJ_support
\\ qexists_tac`domain names`
\\ reverse conj_tac
>- (
simp[]
\\ rw[tlookup_def]
\\ CASE_TAC \\ fs[domain_lookup])
\\ `set (toList names) = IMAGE (tlookup names) (domain names)`
by (
pop_assum kall_tac
\\ simp[EXTENSION,tlookup_def,MEM_toList,domain_lookup]
\\ rw[EQ_IMP_THM] \\ fs[]
>- (qexists_tac`k` \\ fs[])
\\ metis_tac[] )
\\ match_mp_tac (MP_CANON CARD_IMAGE_ID_BIJ)
\\ fs[] \\ rw[] \\ fs[EXTENSION]
\\ metis_tac[]
QED
Theorem tlookup_bij_iff:
BIJ (tlookup names) UNIV UNIV ⇔
set (toList names) = domain names
Proof
rw[EQ_IMP_THM,tlookup_bij_suff]
\\ fs[BIJ_IFF_INV]
\\ rw[EXTENSION,domain_lookup,MEM_toList]
\\ rw[EQ_IMP_THM]
>- (
Cases_on`k=x` >- metis_tac[]
\\ spose_not_then strip_assume_tac
\\ `tlookup names x = x`
by (
simp[tlookup_def]
\\ CASE_TAC \\ fs[] )
\\ `tlookup names k = x`
by ( simp[tlookup_def] )
\\ metis_tac[] )
\\ Cases_on`x=v` >- metis_tac[]
\\ spose_not_then strip_assume_tac
\\ `tlookup names x = v`
by ( simp[tlookup_def] )
\\ `∀k. tlookup names k ≠ x`
by (
rw[tlookup_def]
\\ CASE_TAC \\ fs[]
\\ CCONTR_TAC \\ fs[]
\\ metis_tac[])
\\ metis_tac[]
QED
(* should be composition of oEL and as-yet-undefined "THE default" *)
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)`
Definition update_resize_def:
update_resize ls default v n =
if n < LENGTH ls then
LUPDATE v n ls
else
LUPDATE v n (ls ++ REPLICATE (n * 2 + 1 - LENGTH ls) default)
End
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)`
Theorem list_max_max:
∀ls. EVERY (λx. x ≤ list_max ls) ls
Proof
Induct>>full_simp_tac(srw_ss())[list_max_def,LET_THM]>>srw_tac[][]>>full_simp_tac(srw_ss())[EVERY_MEM]>>srw_tac[][]>>
res_tac >> decide_tac
QED
Theorem list_max_intro:
∀ls.
P 0 ∧ EVERY P ls ⇒ P (list_max ls)
Proof
Induct>>full_simp_tac(srw_ss())[list_max_def]>>srw_tac[][]>>
IF_CASES_TAC>>full_simp_tac(srw_ss())[]
QED
Theorem FOLDR_MAX_0_list_max:
∀ls. FOLDR MAX 0 ls = list_max ls
Proof
Induct \\ rw[list_max_def] \\ rw[MAX_DEF]
QED
(* never used *)
val list_inter_def = Define `
list_inter xs ys = FILTER (\y. MEM y xs) ys`;
val max3_def = Define`
max3 (x:num) y z = if x > y then (if z > x then z else x)
else (if z > y then z else y)`
val _ = export_rewrites["max3_def"];
Theorem 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
Proof
Induct >> simp[] >>
Cases >> simp[] >> srw_tac[][]
QED
Theorem ALOOKUP_GENLIST:
∀f n k. ALOOKUP (GENLIST (λi. (i,f i)) n) k = if k < n then SOME (f k) else NONE
Proof
gen_tac >> Induct >> simp[GENLIST] >> srw_tac[][] >> full_simp_tac(srw_ss())[ALOOKUP_SNOC] >>
srw_tac[][] >> fsrw_tac[ARITH_ss][]
QED
Theorem ALOOKUP_ZIP_FAIL:
∀A B x.
LENGTH A = LENGTH B ⇒
(ALOOKUP (ZIP (A,B)) x = NONE ⇔ ¬MEM x A)
Proof
srw_tac[][]>>Q.ISPECL_THEN [`ZIP(A,B)`,`x`] assume_tac ALOOKUP_NONE >>
full_simp_tac(srw_ss())[MAP_ZIP]
QED
Theorem MEM_ALOOKUP:
!xs x v.
ALL_DISTINCT (MAP FST xs) ==>
(MEM (x,v) xs <=> ALOOKUP xs x = SOME v)
Proof
Induct \\ fs [FORALL_PROD] \\ rw []
\\ res_tac \\ eq_tac \\ rw [] \\ rfs []
\\ imp_res_tac ALOOKUP_MEM
\\ fs [MEM_MAP,FORALL_PROD] \\ rfs []
QED
(* TODO - candidate for move to HOL, but in simpler form without accumulator *)
(* only used in inferProg *)
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"
Theorem 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))
Proof
ho_match_mp_tac anub_ind >> srw_tac[][anub_def] >>
full_simp_tac(srw_ss())[MEM_MAP,PULL_EXISTS,FORALL_PROD,EXISTS_PROD]
QED
(* terrible rewrite *)
Theorem ALOOKUP_anub:
ALOOKUP (anub ls acc) k =
if MEM k acc then ALOOKUP (anub ls acc) k
else ALOOKUP ls k
Proof
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[][]
QED
Theorem anub_eq_nil:
anub x y = [] ⇔ EVERY (combin$C MEM y) (MAP FST x)
Proof
qid_spec_tac`y` >>
Induct_on`x`>>srw_tac[][anub_def]>>
Cases_on`h`>>srw_tac[][anub_def]
QED
Theorem 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)
Proof
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())[]
QED
Theorem anub_notin_acc:
∀ls acc. MEM x acc ⇒ ¬MEM x (MAP FST (anub ls acc))
Proof
Induct >> simp[anub_def] >>
Cases >> simp[anub_def] >> srw_tac[][] >>
metis_tac[]
QED
Theorem 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)
Proof
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]
QED
Theorem anub_all_distinct_keys:
∀ls acc.
ALL_DISTINCT acc ⇒
ALL_DISTINCT ((MAP FST (anub ls acc)) ++ acc)
Proof
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[]
QED
Theorem MEM_anub_ALOOKUP:
MEM (k,v) (anub ls []) ⇒
ALOOKUP ls k = SOME v
Proof
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]
QED
Theorem IS_SOME_EXISTS:
∀opt. IS_SOME opt ⇔ ∃x. opt = SOME x
Proof
Cases >> simp[]
QED
Type num_set = ``:unit spt``
Type num_map = ``:'a spt``
Theorem toAList_domain:
∀x. MEM x (MAP FST (toAList t)) ⇔ x ∈ domain t
Proof
full_simp_tac(srw_ss())[EXISTS_PROD,MEM_MAP,MEM_toAList,domain_lookup]
QED
Theorem domain_nat_set_from_list:
∀ls ns. domain (FOLDL (λs n. insert n () s) ns ls) = domain ns ∪ set ls
Proof
Induct >> simp[sptreeTheory.domain_insert] >>
srw_tac[][EXTENSION] >> metis_tac[]
QED
val _ = export_rewrites["domain_nat_set_from_list"]
Theorem wf_nat_set_from_list:
∀ls ns. wf ns ⇒ wf (FOLDL (λs n. insert n z s) ns ls)
Proof
Induct >> simp[] >> srw_tac[][sptreeTheory.wf_insert]
QED
Theorem BIT_11:
∀n m. (BIT n = BIT m) ⇔ (n = m)
Proof
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]
QED
Theorem BIT_11_2:
∀n m. (∀z. (z < 2 ** (MAX n m)) ⇒ (BIT n z ⇔ BIT m z)) ⇔ (n = m)
Proof
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]
QED
(* only used below in proof of theorem that is in turn used just twice *)
Theorem LOG2_TIMES2:
0 < n ⇒ (LOG2 (2 * n) = SUC (LOG2 n))
Proof
srw_tac[][LOG2_def] >>
qspecl_then[`1`,`2`,`n`]mp_tac logrootTheory.LOG_EXP >>
simp[arithmeticTheory.ADD1]
QED
(* only used below in proof of theorem that is in turn used just twice *)
Theorem LOG2_TIMES2_1:
∀n. 0 < n ⇒ (LOG2 (2 * n + 1) = LOG2 (2 * n))
Proof
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[]
QED
(* used only twice, both times in candle/set-theory *)
Theorem C_BIT_11:
∀n m. (∀z. (z ≤ LOG2 (MAX n m)) ⇒ (BIT z n ⇔ BIT z m)) ⇔ (n = m)
Proof
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]
QED
Theorem BIT_num_from_bin_list_leading:
∀l x. EVERY ($> 2) l ∧ LENGTH l ≤ x ⇒ ¬BIT x (num_from_bin_list l)
Proof
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 (REVERSE l)` >>
simp[numposrepTheory.l2n_lt]
QED
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`
Theorem LEAST_thm:
$LEAST P = least_from P 0
Proof
srw_tac[][least_from_def,ETA_AX]
QED
Theorem least_from_thm:
least_from P n = if P n then n else least_from P (n+1)
Proof
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())[] )
QED
Theorem 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
Proof
strip_tac >> Induct >> simp[] >>
simp[arithmeticTheory.FUNPOW_SUC] >>
first_x_assum match_mp_tac >> srw_tac[][]
QED
Theorem FUNPOW_SUC_PLUS:
∀n a. FUNPOW SUC n = (+) n
Proof
Induct \\ simp[FUNPOW,FUN_EQ_THM]
QED
(* used just once; better as