forked from jrh13/hol-light
-
Notifications
You must be signed in to change notification settings - Fork 12
/
iterate.ml
2548 lines (2230 loc) · 105 KB
/
iterate.ml
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
(* ========================================================================= *)
(* Generic iterated operations and special cases of sums over N and R. *)
(* *)
(* (c) Copyright, John Harrison 1998-2007 *)
(* (c) Copyright, Lars Schewe 2007 *)
(* ========================================================================= *)
set_jrh_lexer;;
Pb_printer.set_file_tags ["iterate.ml"];;
open System;;
open Lib;;
open Fusion;;
open Basics;;
open Printer;;
open Parser;;
open Equal;;
open Bool;;
open Drule;;
open Tactics;;
open Simp;;
open Theorems;;
open Class;;
open Trivia;;
open Meson;;
open Pair;;
open Nums;;
open Arith;;
open Calc_num;;
open Realax;;
open Calc_int;;
open Realarith;;
open Reals;;
open Calc_rat;;
open Ints;;
open Sets;;
prioritize_num();;
(* ------------------------------------------------------------------------- *)
(* A natural notation for segments of the naturals. *)
(* ------------------------------------------------------------------------- *)
parse_as_infix("..",(15,"right"));;
let numseg = new_definition
`m..n = {x:num | m <= x /\ x <= n}`;;
let FINITE_NUMSEG = prove
(`!m n. FINITE(m..n)`,
REPEAT GEN_TAC THEN MATCH_MP_TAC FINITE_SUBSET THEN
EXISTS_TAC `{x:num | x <= n}` THEN REWRITE_TAC[FINITE_NUMSEG_LE] THEN
SIMP_TAC[SUBSET; IN_ELIM_THM; numseg]);;
let NUMSEG_COMBINE_R = prove
(`!m p n. m <= p + 1 /\ p <= n ==> ((m..p) UNION ((p+1)..n) = m..n)`,
REWRITE_TAC[EXTENSION; IN_UNION; numseg; IN_ELIM_THM] THEN ARITH_TAC);;
let NUMSEG_COMBINE_L = prove
(`!m p n. m <= p /\ p <= n + 1 ==> ((m..(p-1)) UNION (p..n) = m..n)`,
REWRITE_TAC[EXTENSION; IN_UNION; numseg; IN_ELIM_THM] THEN ARITH_TAC);;
let NUMSEG_LREC = prove
(`!m n. m <= n ==> (m INSERT ((m+1)..n) = m..n)`,
REWRITE_TAC[EXTENSION; IN_INSERT; numseg; IN_ELIM_THM] THEN ARITH_TAC);;
let NUMSEG_RREC = prove
(`!m n. m <= n ==> (n INSERT (m..(n-1)) = m..n)`,
REWRITE_TAC[EXTENSION; IN_INSERT; numseg; IN_ELIM_THM] THEN ARITH_TAC);;
let NUMSEG_REC = prove
(`!m n. m <= SUC n ==> (m..SUC n = (SUC n) INSERT (m..n))`,
SIMP_TAC[GSYM NUMSEG_RREC; SUC_SUB1]);;
let IN_NUMSEG = prove
(`!m n p. p IN (m..n) <=> m <= p /\ p <= n`,
REWRITE_TAC[numseg; IN_ELIM_THM]);;
let IN_NUMSEG_0 = prove
(`!m n. m IN (0..n) <=> m <= n`,
REWRITE_TAC[IN_NUMSEG; LE_0]);;
let NUMSEG_SING = prove
(`!n. n..n = {n}`,
REWRITE_TAC[EXTENSION; IN_SING; IN_NUMSEG] THEN ARITH_TAC);;
let NUMSEG_EMPTY = prove
(`!m n. (m..n = {}) <=> n < m`,
REWRITE_TAC[EXTENSION; NOT_IN_EMPTY; IN_NUMSEG] THEN
MESON_TAC[NOT_LE; LE_TRANS; LE_REFL]);;
let FINITE_SUBSET_NUMSEG = prove
(`!s:num->bool. FINITE s <=> ?n. s SUBSET 0..n`,
GEN_TAC THEN EQ_TAC THENL
[REWRITE_TAC[SUBSET; IN_NUMSEG; LE_0] THEN
SPEC_TAC(`s:num->bool`,`s:num->bool`) THEN
MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
REWRITE_TAC[IN_INSERT; NOT_IN_EMPTY] THEN
MESON_TAC[LE_CASES; LE_REFL; LE_TRANS];
MESON_TAC[FINITE_SUBSET; FINITE_NUMSEG]]);;
let CARD_NUMSEG_LEMMA = prove
(`!m d. CARD(m..(m+d)) = d + 1`,
GEN_TAC THEN INDUCT_TAC THEN
ASM_SIMP_TAC[ADD_CLAUSES; NUMSEG_REC; NUMSEG_SING; FINITE_RULES;
ARITH_RULE `m <= SUC(m + d)`; CARD_CLAUSES; FINITE_NUMSEG;
NOT_IN_EMPTY; ARITH; IN_NUMSEG; ARITH_RULE `~(SUC n <= n)`]);;
let CARD_NUMSEG = prove
(`!m n. CARD(m..n) = (n + 1) - m`,
REPEAT GEN_TAC THEN
DISJ_CASES_THEN MP_TAC (ARITH_RULE `n:num < m \/ m <= n`) THENL
[ASM_MESON_TAC[NUMSEG_EMPTY; CARD_CLAUSES;
ARITH_RULE `n < m ==> ((n + 1) - m = 0)`];
SIMP_TAC[LE_EXISTS; LEFT_IMP_EXISTS_THM; CARD_NUMSEG_LEMMA] THEN
REPEAT STRIP_TAC THEN ARITH_TAC]);;
let HAS_SIZE_NUMSEG = prove
(`!m n. (m..n) HAS_SIZE ((n + 1) - m)`,
REWRITE_TAC[HAS_SIZE; FINITE_NUMSEG; CARD_NUMSEG]);;
let CARD_NUMSEG_1 = prove
(`!n. CARD(1..n) = n`,
REWRITE_TAC[CARD_NUMSEG] THEN ARITH_TAC);;
let HAS_SIZE_NUMSEG_1 = prove
(`!n. (1..n) HAS_SIZE n`,
REWRITE_TAC[CARD_NUMSEG; HAS_SIZE; FINITE_NUMSEG] THEN ARITH_TAC);;
let NUMSEG_CLAUSES = prove
(`(!m. m..0 = if m = 0 then {0} else {}) /\
(!m n. m..SUC n = if m <= SUC n then (SUC n) INSERT (m..n) else m..n)`,
REPEAT STRIP_TAC THEN COND_CASES_TAC THEN
GEN_REWRITE_TAC "iterate.ml:I" I [EXTENSION] THEN
REWRITE_TAC[IN_NUMSEG; NOT_IN_EMPTY; IN_INSERT] THEN
POP_ASSUM MP_TAC THEN ARITH_TAC);;
let FINITE_INDEX_NUMSEG = prove
(`!s:A->bool.
FINITE s =
?f. (!i j. i IN (1..CARD(s)) /\ j IN (1..CARD(s)) /\ (f i = f j)
==> (i = j)) /\
(s = IMAGE f (1..CARD(s)))`,
GEN_TAC THEN EQ_TAC THENL
[ALL_TAC; MESON_TAC[FINITE_NUMSEG; FINITE_IMAGE]] THEN
DISCH_TAC THEN
MP_TAC(ISPECL [`s:A->bool`; `CARD(s:A->bool)`] HAS_SIZE_INDEX) THEN
ASM_REWRITE_TAC[HAS_SIZE] THEN
DISCH_THEN(X_CHOOSE_THEN `f:num->A` STRIP_ASSUME_TAC) THEN
EXISTS_TAC `\n. f(n - 1):A` THEN
ASM_REWRITE_TAC[EXTENSION; IN_IMAGE; IN_NUMSEG] THEN
CONJ_TAC THENL
[REWRITE_TAC[ARITH_RULE `1 <= i /\ i <= n <=> ~(i = 0) /\ i - 1 < n`] THEN
ASM_MESON_TAC[ARITH_RULE
`~(x = 0) /\ ~(y = 0) /\ (x - 1 = y - 1) ==> (x = y)`];
ASM_MESON_TAC
[ARITH_RULE `m < C ==> (m = (m + 1) - 1) /\ 1 <= m + 1 /\ m + 1 <= C`;
ARITH_RULE `1 <= i /\ i <= n <=> ~(i = 0) /\ i - 1 < n`]]);;
let FINITE_INDEX_NUMBERS = prove
(`!s:A->bool.
FINITE s =
?k:num->bool f. (!i j. i IN k /\ j IN k /\ (f i = f j) ==> (i = j)) /\
FINITE k /\ (s = IMAGE f k)`,
MESON_TAC[FINITE_INDEX_NUMSEG; FINITE_NUMSEG; FINITE_IMAGE]);;
let INTER_NUMSEG = prove
(`!m n p q. (m..n) INTER (p..q) = (MAX m p)..(MIN n q)`,
REWRITE_TAC[EXTENSION; IN_INTER; IN_NUMSEG] THEN ARITH_TAC);;
let DISJOINT_NUMSEG = prove
(`!m n p q. DISJOINT (m..n) (p..q) <=> n < p \/ q < m \/ n < m \/ q < p`,
REWRITE_TAC[DISJOINT; NUMSEG_EMPTY; INTER_NUMSEG] THEN ARITH_TAC);;
let NUMSEG_ADD_SPLIT = prove
(`!m n p. m <= n + 1 ==> (m..(n+p) = (m..n) UNION (n+1..n+p))`,
REWRITE_TAC[EXTENSION; IN_UNION; IN_NUMSEG] THEN ARITH_TAC);;
let NUMSEG_OFFSET_IMAGE = prove
(`!m n p. (m+p..n+p) = IMAGE (\i. i + p) (m..n)`,
REWRITE_TAC[EXTENSION; IN_IMAGE; IN_NUMSEG] THEN
REPEAT GEN_TAC THEN EQ_TAC THENL
[DISCH_THEN(fun th -> EXISTS_TAC `x - p:num` THEN MP_TAC th); ALL_TAC] THEN
ARITH_TAC);;
let SUBSET_NUMSEG = prove
(`!m n p q. (m..n) SUBSET (p..q) <=> n < m \/ p <= m /\ n <= q`,
REPEAT GEN_TAC THEN REWRITE_TAC[SUBSET; IN_NUMSEG] THEN
EQ_TAC THENL [MESON_TAC[LE_TRANS; NOT_LE; LE_REFL]; ARITH_TAC]);;
(* ------------------------------------------------------------------------- *)
(* Equivalence with the more ad-hoc comprehension notation. *)
(* ------------------------------------------------------------------------- *)
let NUMSEG_LE = prove
(`!n. {x | x <= n} = 0..n`,
REWRITE_TAC[EXTENSION; IN_NUMSEG; IN_ELIM_THM] THEN ARITH_TAC);;
let NUMSEG_LT = prove
(`!n. {x | x < n} = if n = 0 then {} else 0..(n-1)`,
GEN_TAC THEN COND_CASES_TAC THEN
REWRITE_TAC[EXTENSION; IN_NUMSEG; IN_ELIM_THM; NOT_IN_EMPTY] THEN
ASM_ARITH_TAC);;
(* ------------------------------------------------------------------------- *)
(* Conversion to evaluate m..n for specific numerals. *)
(* ------------------------------------------------------------------------- *)
let NUMSEG_CONV =
let pth_0 = MESON[NUMSEG_EMPTY] `n < m ==> m..n = {}`
and pth_1 = MESON[NUMSEG_SING] `m..m = {m}`
and pth_2 = MESON[NUMSEG_LREC; ADD1] `m <= n ==> m..n = m INSERT (SUC m..n)`
and ns_tm = `(..)` and m_tm = `m:num` and n_tm = `n:num` in
let rec NUMSEG_CONV tm =
let nstm,nt = dest_comb tm in
let nst,mt = dest_comb nstm in
if nst <> ns_tm then failwith "NUMSEG_CONV" else
let m = dest_numeral mt and n = dest_numeral nt in
if n </ m then MP_CONV NUM_LT_CONV (INST [mt,m_tm; nt,n_tm] pth_0)
else if n =/ m then INST [mt,m_tm] pth_1
else let th = MP_CONV NUM_LE_CONV (INST [mt,m_tm; nt,n_tm] pth_2) in
CONV_RULE(funpow 2 RAND_CONV
(LAND_CONV NUM_SUC_CONV THENC NUMSEG_CONV)) th in
NUMSEG_CONV;;
(* ------------------------------------------------------------------------- *)
(* Topological sorting of a finite set. *)
(* ------------------------------------------------------------------------- *)
let TOPOLOGICAL_SORT = prove
(`!(<<). (!x y:A. x << y /\ y << x ==> x = y) /\
(!x y z. x << y /\ y << z ==> x << z)
==> !n s. s HAS_SIZE n
==> ?f. s = IMAGE f (1..n) /\
(!j k. j IN 1..n /\ k IN 1..n /\ j < k
==> ~(f k << f j))`,
GEN_TAC THEN DISCH_TAC THEN
SUBGOAL_THEN `!n s. s HAS_SIZE n /\ ~(s = {})
==> ?a:A. a IN s /\ !b. b IN (s DELETE a) ==> ~(b << a)`
ASSUME_TAC THENL
[INDUCT_TAC THEN
REWRITE_TAC[HAS_SIZE_0; HAS_SIZE_SUC; TAUT `~(a /\ ~a)`] THEN
X_GEN_TAC `s:A->bool` THEN STRIP_TAC THEN
FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [GSYM MEMBER_NOT_EMPTY]) THEN
DISCH_THEN(X_CHOOSE_TAC `a:A`) THEN
FIRST_X_ASSUM(MP_TAC o SPEC `a:A`) THEN ASM_REWRITE_TAC[] THEN
DISCH_TAC THEN FIRST_X_ASSUM(MP_TAC o SPEC `s DELETE (a:A)`) THEN
ASM_SIMP_TAC[SET_RULE `a IN s ==> (s DELETE a = {} <=> s = {a})`] THEN
ASM_CASES_TAC `s = {a:A}` THEN ASM_REWRITE_TAC[] THENL
[EXISTS_TAC `a:A` THEN SET_TAC[]; ALL_TAC] THEN
DISCH_THEN(X_CHOOSE_THEN `b:A` STRIP_ASSUME_TAC) THEN
ASM_CASES_TAC `((a:A) << (b:A)) :bool` THENL
[EXISTS_TAC `a:A`; EXISTS_TAC `b:A`] THEN ASM SET_TAC[];
ALL_TAC] THEN
INDUCT_TAC THENL
[SIMP_TAC[HAS_SIZE_0; NUMSEG_CLAUSES; ARITH; IMAGE_CLAUSES; NOT_IN_EMPTY];
ALL_TAC] THEN
REWRITE_TAC[HAS_SIZE_SUC] THEN X_GEN_TAC `s:A->bool` THEN STRIP_TAC THEN
FIRST_X_ASSUM(MP_TAC o SPECL [`SUC n`; `s:A->bool`]) THEN
ASM_REWRITE_TAC[HAS_SIZE_SUC] THEN
DISCH_THEN(X_CHOOSE_THEN `a:A` MP_TAC) THEN STRIP_TAC THEN
FIRST_X_ASSUM(MP_TAC o SPEC `s DELETE (a:A)`) THEN ASM_SIMP_TAC[] THEN
DISCH_THEN(X_CHOOSE_THEN `f:num->A` STRIP_ASSUME_TAC) THEN
EXISTS_TAC `\k. if k = 1 then a:A else f(k - 1)` THEN
SIMP_TAC[ARITH_RULE `1 <= k ==> ~(SUC k = 1)`; SUC_SUB1] THEN
SUBGOAL_THEN `!i. i IN 1..SUC n <=> i = 1 \/ 1 < i /\ (i - 1) IN 1..n`
(fun th -> REWRITE_TAC[EXTENSION; IN_IMAGE; th])
THENL [REWRITE_TAC[IN_NUMSEG] THEN ARITH_TAC; ALL_TAC] THEN CONJ_TAC THENL
[X_GEN_TAC `b:A` THEN ASM_CASES_TAC `b:A = a` THENL
[ASM_MESON_TAC[]; ALL_TAC] THEN
FIRST_ASSUM(fun th -> ONCE_REWRITE_TAC[MATCH_MP
(SET_RULE `~(b = a) ==> (b IN s <=> b IN (s DELETE a))`) th]) THEN
ONCE_REWRITE_TAC[COND_RAND] THEN
ASM_REWRITE_TAC[IN_IMAGE; IN_NUMSEG] THEN
EQ_TAC THENL [ALL_TAC; MESON_TAC[]] THEN
DISCH_THEN(X_CHOOSE_TAC `i:num`) THEN EXISTS_TAC `i + 1` THEN
ASM_SIMP_TAC[ARITH_RULE `1 <= x ==> 1 < x + 1 /\ ~(x + 1 = 1)`; ADD_SUB];
MAP_EVERY X_GEN_TAC [`j:num`; `k:num`] THEN
MAP_EVERY ASM_CASES_TAC [`j = 1`; `k = 1`] THEN
ASM_REWRITE_TAC[LT_REFL] THENL
[STRIP_TAC THEN FIRST_X_ASSUM MATCH_MP_TAC THEN ASM SET_TAC[];
ARITH_TAC;
STRIP_TAC THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
ASM_REWRITE_TAC[] THEN ASM_ARITH_TAC]]);;
(* ------------------------------------------------------------------------- *)
(* Analogous finiteness theorem for segments of integers. *)
(* ------------------------------------------------------------------------- *)
let FINITE_INTSEG = prove
(`(!l r. FINITE {x:int | l <= x /\ x <= r}) /\
(!l r. FINITE {x:int | l <= x /\ x < r}) /\
(!l r. FINITE {x:int | l < x /\ x <= r}) /\
(!l r. FINITE {x:int | l < x /\ x < r})`,
MATCH_MP_TAC(TAUT `(a ==> b) /\ a ==> a /\ b`) THEN CONJ_TAC THENL
[DISCH_TAC THEN REPEAT CONJ_TAC THEN POP_ASSUM MP_TAC THEN
REPEAT(MATCH_MP_TAC MONO_FORALL THEN GEN_TAC) THEN
MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ_ALT] FINITE_SUBSET) THEN
REWRITE_TAC[SUBSET; IN_ELIM_THM] THEN INT_ARITH_TAC;
REPEAT GEN_TAC THEN ASM_CASES_TAC `&0:int <= r - l` THEN
ASM_SIMP_TAC[INT_ARITH `~(&0 <= r - l:int) ==> ~(l <= x /\ x <= r)`] THEN
ASM_SIMP_TAC[EMPTY_GSPEC; FINITE_EMPTY] THEN
MATCH_MP_TAC FINITE_SUBSET THEN
EXISTS_TAC `IMAGE (\n. l + &n) (0..num_of_int(r - l))` THEN
ASM_SIMP_TAC[FINITE_IMAGE; FINITE_NUMSEG] THEN
REWRITE_TAC[SUBSET; IN_IMAGE; IN_ELIM_THM] THEN
REWRITE_TAC[GSYM INT_OF_NUM_LE; IN_NUMSEG] THEN
X_GEN_TAC `x:int` THEN STRIP_TAC THEN EXISTS_TAC `num_of_int(x - l)` THEN
ASM_SIMP_TAC[INT_OF_NUM_OF_INT; INT_SUB_LE] THEN ASM_INT_ARITH_TAC]);;
(* ------------------------------------------------------------------------- *)
(* Generic iteration of operation over set with finite support. *)
(* ------------------------------------------------------------------------- *)
let neutral = new_definition
`neutral op = @x. !y. (op x y = y) /\ (op y x = y)`;;
let monoidal = new_definition
`monoidal op <=> (!x y. op x y = op y x) /\
(!x y z. op x (op y z) = op (op x y) z) /\
(!x:A. op (neutral op) x = x)`;;
let MONOIDAL_AC = prove
(`!op. monoidal op
==> (!a. op (neutral op) a = a) /\
(!a. op a (neutral op) = a) /\
(!a b. op a b = op b a) /\
(!a b c. op (op a b) c = op a (op b c)) /\
(!a b c. op a (op b c) = op b (op a c))`,
REWRITE_TAC[monoidal] THEN MESON_TAC[]);;
let support = new_definition
`support op (f:A->B) s = {x | x IN s /\ ~(f x = neutral op)}`;;
let iterate = new_definition
`iterate op (s:A->bool) f =
if FINITE(support op f s)
then ITSET (\x a. op (f x) a) (support op f s) (neutral op)
else neutral op`;;
let IN_SUPPORT = prove
(`!op f x s. x IN (support op f s) <=> x IN s /\ ~(f x = neutral op)`,
REWRITE_TAC[support; IN_ELIM_THM]);;
let SUPPORT_SUPPORT = prove
(`!op f s. support op f (support op f s) = support op f s`,
REWRITE_TAC[support; IN_ELIM_THM; EXTENSION] THEN REWRITE_TAC[CONJ_ACI]);;
let SUPPORT_EMPTY = prove
(`!op f s. (!x. x IN s ==> (f(x) = neutral op)) <=> (support op f s = {})`,
REWRITE_TAC[IN_SUPPORT; EXTENSION; IN_ELIM_THM; NOT_IN_EMPTY] THEN
MESON_TAC[]);;
let SUPPORT_SUBSET = prove
(`!op f s. (support op f s) SUBSET s`,
SIMP_TAC[SUBSET; IN_SUPPORT]);;
let FINITE_SUPPORT = prove
(`!op f s. FINITE s ==> FINITE(support op f s)`,
MESON_TAC[SUPPORT_SUBSET; FINITE_SUBSET]);;
let SUPPORT_CLAUSES = prove
(`(!f. support op f {} = {}) /\
(!f x s. support op f (x INSERT s) =
if f(x) = neutral op then support op f s
else x INSERT (support op f s)) /\
(!f x s. support op f (s DELETE x) = (support op f s) DELETE x) /\
(!f s t. support op f (s UNION t) =
(support op f s) UNION (support op f t)) /\
(!f s t. support op f (s INTER t) =
(support op f s) INTER (support op f t)) /\
(!f s t. support op f (s DIFF t) =
(support op f s) DIFF (support op f t)) /\
(!f g s. support op g (IMAGE f s) = IMAGE f (support op (g o f) s))`,
REWRITE_TAC[support; EXTENSION; IN_ELIM_THM; IN_INSERT; IN_DELETE; o_THM;
IN_IMAGE; NOT_IN_EMPTY; IN_UNION; IN_INTER; IN_DIFF; COND_RAND] THEN
REPEAT STRIP_TAC THEN TRY COND_CASES_TAC THEN ASM_MESON_TAC[]);;
let SUPPORT_DELTA = prove
(`!op s f a. support op (\x. if x = a then f(x) else neutral op) s =
if a IN s then support op f {a} else {}`,
REWRITE_TAC[EXTENSION; support; IN_ELIM_THM; IN_SING] THEN
REPEAT GEN_TAC THEN REPEAT COND_CASES_TAC THEN
ASM_REWRITE_TAC[IN_ELIM_THM; NOT_IN_EMPTY]);;
let FINITE_SUPPORT_DELTA = prove
(`!op f a. FINITE(support op (\x. if x = a then f(x) else neutral op) s)`,
REWRITE_TAC[SUPPORT_DELTA] THEN REPEAT GEN_TAC THEN
COND_CASES_TAC THEN SIMP_TAC[FINITE_RULES; FINITE_SUPPORT]);;
(* ------------------------------------------------------------------------- *)
(* Key lemmas about the generic notion. *)
(* ------------------------------------------------------------------------- *)
let ITERATE_SUPPORT = prove
(`!op f s. iterate op (support op f s) f = iterate op s f`,
SIMP_TAC[iterate; SUPPORT_SUPPORT]);;
let ITERATE_EXPAND_CASES = prove
(`!op f s. iterate op s f =
if FINITE(support op f s) then iterate op (support op f s) f
else neutral op`,
SIMP_TAC[iterate; SUPPORT_SUPPORT]);;
let ITERATE_CLAUSES_GEN = prove
(`!op. monoidal op
==> (!(f:A->B). iterate op {} f = neutral op) /\
(!f x s. monoidal op /\ FINITE(support op (f:A->B) s)
==> (iterate op (x INSERT s) f =
if x IN s then iterate op s f
else op (f x) (iterate op s f)))`,
GEN_TAC THEN STRIP_TAC THEN
ONCE_REWRITE_TAC[AND_FORALL_THM] THEN GEN_TAC THEN
MP_TAC(ISPECL [`\x a. (op:B->B->B) ((f:A->B)(x)) a`; `neutral op :B`]
FINITE_RECURSION) THEN
ANTS_TAC THENL [ASM_MESON_TAC[monoidal]; ALL_TAC] THEN
REPEAT STRIP_TAC THEN
ASM_REWRITE_TAC[iterate; SUPPORT_CLAUSES; FINITE_RULES] THEN
GEN_REWRITE_TAC "iterate.ml:(LAND_CONV o RATOR_CONV o LAND_CONV)" (LAND_CONV o RATOR_CONV o LAND_CONV) [COND_RAND] THEN
ASM_REWRITE_TAC[SUPPORT_CLAUSES; FINITE_INSERT; COND_ID] THEN
ASM_CASES_TAC `(f:A->B) x = neutral op` THEN
ASM_SIMP_TAC[IN_SUPPORT] THEN COND_CASES_TAC THEN ASM_MESON_TAC[monoidal]);;
let ITERATE_CLAUSES = prove
(`!op. monoidal op
==> (!f. iterate op {} f = neutral op) /\
(!f x s. FINITE(s)
==> (iterate op (x INSERT s) f =
if x IN s then iterate op s f
else op (f x) (iterate op s f)))`,
SIMP_TAC[ITERATE_CLAUSES_GEN; FINITE_SUPPORT]);;
let ITERATE_UNION = prove
(`!op. monoidal op
==> !f s t. FINITE s /\ FINITE t /\ DISJOINT s t
==> (iterate op (s UNION t) f =
op (iterate op s f) (iterate op t f))`,
let lemma = prove
(`(s UNION (x INSERT t) = x INSERT (s UNION t)) /\
(DISJOINT s (x INSERT t) <=> ~(x IN s) /\ DISJOINT s t)`,
SET_TAC[]) in
GEN_TAC THEN DISCH_TAC THEN GEN_TAC THEN GEN_TAC THEN
REWRITE_TAC[IMP_CONJ; RIGHT_FORALL_IMP_THM] THEN REPEAT DISCH_TAC THEN
MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
ASM_SIMP_TAC[ITERATE_CLAUSES; IN_UNION; UNION_EMPTY; REAL_ADD_RID; lemma;
FINITE_UNION] THEN
ASM_MESON_TAC[monoidal]);;
let ITERATE_UNION_GEN = prove
(`!op. monoidal op
==> !(f:A->B) s t. FINITE(support op f s) /\ FINITE(support op f t) /\
DISJOINT (support op f s) (support op f t)
==> (iterate op (s UNION t) f =
op (iterate op s f) (iterate op t f))`,
ONCE_REWRITE_TAC[GSYM ITERATE_SUPPORT] THEN
SIMP_TAC[SUPPORT_CLAUSES; ITERATE_UNION]);;
let ITERATE_DIFF = prove
(`!op. monoidal op
==> !f s t. FINITE s /\ t SUBSET s
==> (op (iterate op (s DIFF t) f) (iterate op t f) =
iterate op s f)`,
let lemma = prove
(`t SUBSET s ==> (s = (s DIFF t) UNION t) /\ DISJOINT (s DIFF t) t`,
SET_TAC[]) in
MESON_TAC[lemma; ITERATE_UNION; FINITE_UNION; FINITE_SUBSET; SUBSET_DIFF]);;
let ITERATE_DIFF_GEN = prove
(`!op. monoidal op
==> !f:A->B s t. FINITE (support op f s) /\
(support op f t) SUBSET (support op f s)
==> (op (iterate op (s DIFF t) f) (iterate op t f) =
iterate op s f)`,
ONCE_REWRITE_TAC[GSYM ITERATE_SUPPORT] THEN
SIMP_TAC[SUPPORT_CLAUSES; ITERATE_DIFF]);;
let ITERATE_INCL_EXCL = prove
(`!op. monoidal op
==> !s t f. FINITE s /\ FINITE t
==> op (iterate op s f) (iterate op t f) =
op (iterate op (s UNION t) f)
(iterate op (s INTER t) f)`,
REPEAT STRIP_TAC THEN
ONCE_REWRITE_TAC[SET_RULE
`a UNION b = ((a DIFF b) UNION (b DIFF a)) UNION (a INTER b)`] THEN
GEN_REWRITE_TAC "iterate.ml:(LAND_CONV o LAND_CONV o ONCE_DEPTH_CONV)" (LAND_CONV o LAND_CONV o ONCE_DEPTH_CONV)
[SET_RULE `s:A->bool = s DIFF t UNION s INTER t`] THEN
GEN_REWRITE_TAC "iterate.ml:(LAND_CONV o RAND_CONV o ONCE_DEPTH_CONV)" (LAND_CONV o RAND_CONV o ONCE_DEPTH_CONV)
[SET_RULE `t:A->bool = t DIFF s UNION s INTER t`] THEN
ASM_SIMP_TAC[ITERATE_UNION; FINITE_UNION; FINITE_DIFF; FINITE_INTER;
SET_RULE `DISJOINT (s DIFF s' UNION s' DIFF s) (s INTER s')`;
SET_RULE `DISJOINT (s DIFF s') (s' DIFF s)`;
SET_RULE `DISJOINT (s DIFF s') (s' INTER s)`;
SET_RULE `DISJOINT (s DIFF s') (s INTER s')`] THEN
FIRST_X_ASSUM(fun th -> REWRITE_TAC[MATCH_MP MONOIDAL_AC th]));;
let ITERATE_CLOSED = prove
(`!op. monoidal op
==> !P. P(neutral op) /\ (!x y. P x /\ P y ==> P (op x y))
==> !f:A->B s. (!x. x IN s /\ ~(f x = neutral op) ==> P(f x))
==> P(iterate op s f)`,
REPEAT STRIP_TAC THEN ONCE_REWRITE_TAC[ITERATE_EXPAND_CASES] THEN
REPEAT(POP_ASSUM MP_TAC) THEN REWRITE_TAC[GSYM IN_SUPPORT] THEN
COND_CASES_TAC THEN ASM_SIMP_TAC[] THEN POP_ASSUM MP_TAC THEN
SPEC_TAC(`support op (f:A->B) s`,`s:A->bool`) THEN
MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
ASM_SIMP_TAC[ITERATE_CLAUSES; FINITE_INSERT; IN_INSERT]);;
let ITERATE_RELATED = prove
(`!op. monoidal op
==> !R. R (neutral op) (neutral op) /\
(!x1 y1 x2 y2. R x1 x2 /\ R y1 y2 ==> R (op x1 y1) (op x2 y2))
==> !f:A->B g s.
FINITE s /\
(!x. x IN s ==> R (f x) (g x))
==> R (iterate op s f) (iterate op s g)`,
GEN_TAC THEN DISCH_TAC THEN GEN_TAC THEN STRIP_TAC THEN GEN_TAC THEN
GEN_TAC THEN REWRITE_TAC[IMP_CONJ] THEN
MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
ASM_SIMP_TAC[ITERATE_CLAUSES; FINITE_INSERT; IN_INSERT]);;
let ITERATE_EQ_NEUTRAL = prove
(`!op. monoidal op
==> !f:A->B s. (!x. x IN s ==> (f(x) = neutral op))
==> (iterate op s f = neutral op)`,
REPEAT STRIP_TAC THEN
SUBGOAL_THEN `support op (f:A->B) s = {}` ASSUME_TAC THENL
[ASM_MESON_TAC[EXTENSION; NOT_IN_EMPTY; IN_SUPPORT];
ASM_MESON_TAC[ITERATE_CLAUSES; FINITE_RULES; ITERATE_SUPPORT]]);;
let ITERATE_SING = prove
(`!op. monoidal op ==> !f:A->B x. (iterate op {x} f = f x)`,
SIMP_TAC[ITERATE_CLAUSES; FINITE_RULES; NOT_IN_EMPTY] THEN
MESON_TAC[monoidal]);;
let ITERATE_DELETE = prove
(`!op. monoidal op
==> !f:A->B s a. FINITE s /\ a IN s
==> op (f a) (iterate op (s DELETE a) f) =
iterate op s f`,
MESON_TAC[ITERATE_CLAUSES; FINITE_DELETE; IN_DELETE; INSERT_DELETE]);;
let ITERATE_DELTA = prove
(`!op. monoidal op
==> !f a s. iterate op s (\x. if x = a then f(x) else neutral op) =
if a IN s then f(a) else neutral op`,
GEN_TAC THEN DISCH_TAC THEN ONCE_REWRITE_TAC[GSYM ITERATE_SUPPORT] THEN
REWRITE_TAC[SUPPORT_DELTA] THEN REPEAT GEN_TAC THEN COND_CASES_TAC THEN
ASM_SIMP_TAC[ITERATE_CLAUSES] THEN REWRITE_TAC[SUPPORT_CLAUSES] THEN
COND_CASES_TAC THEN ASM_SIMP_TAC[ITERATE_CLAUSES; ITERATE_SING]);;
let ITERATE_IMAGE = prove
(`!op. monoidal op
==> !f:A->B g:B->C s.
(!x y. x IN s /\ y IN s /\ (f x = f y) ==> (x = y))
==> (iterate op (IMAGE f s) g = iterate op s (g o f))`,
GEN_TAC THEN DISCH_TAC THEN GEN_TAC THEN GEN_TAC THEN
SUBGOAL_THEN
`!s. FINITE s /\
(!x y:A. x IN s /\ y IN s /\ (f x = f y) ==> (x = y))
==> (iterate op (IMAGE f s) (g:B->C) = iterate op s (g o f))`
ASSUME_TAC THENL
[REWRITE_TAC[IMP_CONJ] THEN MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
ASM_SIMP_TAC[ITERATE_CLAUSES; IMAGE_CLAUSES; FINITE_IMAGE] THEN
REWRITE_TAC[o_THM; IN_INSERT] THEN ASM_MESON_TAC[IN_IMAGE];
GEN_TAC THEN DISCH_TAC THEN ONCE_REWRITE_TAC[ITERATE_EXPAND_CASES] THEN
REPEAT STRIP_TAC THEN MATCH_MP_TAC(TAUT
`(a <=> a') /\ (a' ==> (b = b'))
==> (if a then b else c) = (if a' then b' else c)`) THEN
REWRITE_TAC[SUPPORT_CLAUSES] THEN REPEAT STRIP_TAC THENL
[MATCH_MP_TAC FINITE_IMAGE_INJ_EQ THEN ASM_MESON_TAC[IN_SUPPORT];
FIRST_X_ASSUM MATCH_MP_TAC THEN ASM_MESON_TAC[IN_SUPPORT]]]);;
let ITERATE_BIJECTION = prove
(`!op. monoidal op
==> !f:A->B p s.
(!x. x IN s ==> p(x) IN s) /\
(!y. y IN s ==> ?!x. x IN s /\ p(x) = y)
==> iterate op s f = iterate op s (f o p)`,
REPEAT STRIP_TAC THEN MATCH_MP_TAC EQ_TRANS THEN
EXISTS_TAC `iterate op (IMAGE (p:A->A) s) (f:A->B)` THEN CONJ_TAC THENL
[AP_THM_TAC THEN AP_TERM_TAC THEN REWRITE_TAC[EXTENSION; IN_IMAGE];
FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP
(INST_TYPE [aty,bty] ITERATE_IMAGE))] THEN
ASM_MESON_TAC[]);;
let ITERATE_ITERATE_PRODUCT = prove
(`!op. monoidal op
==> !s:A->bool t:A->B->bool x:A->B->C.
FINITE s /\ (!i. i IN s ==> FINITE(t i))
==> iterate op s (\i. iterate op (t i) (x i)) =
iterate op {i,j | i IN s /\ j IN t i} (\(i,j). x i j)`,
GEN_TAC THEN DISCH_TAC THEN
REWRITE_TAC[IMP_CONJ; RIGHT_FORALL_IMP_THM] THEN
MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
ASM_SIMP_TAC[NOT_IN_EMPTY; SET_RULE `{a,b | F} = {}`; ITERATE_CLAUSES] THEN
REWRITE_TAC[SET_RULE `{i,j | i IN a INSERT s /\ j IN t i} =
IMAGE (\j. a,j) (t a) UNION {i,j | i IN s /\ j IN t i}`] THEN
ASM_SIMP_TAC[FINITE_INSERT; ITERATE_CLAUSES; IN_INSERT] THEN
REPEAT STRIP_TAC THEN
FIRST_ASSUM(fun th ->
W(MP_TAC o PART_MATCH (lhand o rand) (MATCH_MP ITERATE_UNION th) o
rand o snd)) THEN
ANTS_TAC THENL
[ASM_SIMP_TAC[FINITE_IMAGE; FINITE_PRODUCT_DEPENDENT; IN_INSERT] THEN
REWRITE_TAC[DISJOINT; EXTENSION; IN_IMAGE; IN_INTER; NOT_IN_EMPTY;
IN_ELIM_THM; EXISTS_PAIR_THM; FORALL_PAIR_THM; PAIR_EQ] THEN
ASM_MESON_TAC[];
ALL_TAC] THEN
DISCH_THEN SUBST1_TAC THEN AP_THM_TAC THEN AP_TERM_TAC THEN
FIRST_ASSUM(fun th ->
W(MP_TAC o PART_MATCH (lhand o rand) (MATCH_MP ITERATE_IMAGE th) o
rand o snd)) THEN
ANTS_TAC THENL
[SIMP_TAC[FORALL_PAIR_THM] THEN CONV_TAC "iterate.ml:(ONCE_DEPTH_CONV GEN_BETA_CONV)" (ONCE_DEPTH_CONV GEN_BETA_CONV) THEN
ASM_SIMP_TAC[PAIR_EQ];
DISCH_THEN SUBST1_TAC THEN REWRITE_TAC[o_DEF] THEN
CONV_TAC "iterate.ml:(ONCE_DEPTH_CONV GEN_BETA_CONV)" (ONCE_DEPTH_CONV GEN_BETA_CONV) THEN REWRITE_TAC[ETA_AX]]);;
let ITERATE_EQ = prove
(`!op. monoidal op
==> !f:A->B g s.
(!x. x IN s ==> f x = g x) ==> iterate op s f = iterate op s g`,
REPEAT STRIP_TAC THEN ONCE_REWRITE_TAC[ITERATE_EXPAND_CASES] THEN
SUBGOAL_THEN `support op g s = support op (f:A->B) s` SUBST1_TAC THENL
[REWRITE_TAC[EXTENSION; IN_SUPPORT] THEN ASM_MESON_TAC[]; ALL_TAC] THEN
COND_CASES_TAC THEN ASM_REWRITE_TAC[] THEN
SUBGOAL_THEN
`FINITE(support op (f:A->B) s) /\
(!x. x IN (support op f s) ==> f x = g x)`
MP_TAC THENL [ASM_MESON_TAC[IN_SUPPORT]; REWRITE_TAC[IMP_CONJ]] THEN
SPEC_TAC(`support op (f:A->B) s`,`t:A->bool`) THEN
MATCH_MP_TAC FINITE_INDUCT_STRONG THEN ASM_SIMP_TAC[ITERATE_CLAUSES] THEN
MESON_TAC[IN_INSERT]);;
let ITERATE_EQ_GENERAL = prove
(`!op. monoidal op
==> !s:A->bool t:B->bool f:A->C g h.
(!y. y IN t ==> ?!x. x IN s /\ h(x) = y) /\
(!x. x IN s ==> h(x) IN t /\ g(h x) = f x)
==> iterate op s f = iterate op t g`,
REPEAT STRIP_TAC THEN
SUBGOAL_THEN `t = IMAGE (h:A->B) s` SUBST1_TAC THENL
[REWRITE_TAC[EXTENSION; IN_IMAGE] THEN ASM_MESON_TAC[]; ALL_TAC] THEN
MATCH_MP_TAC EQ_TRANS THEN
EXISTS_TAC `iterate op s ((g:B->C) o (h:A->B))` THEN CONJ_TAC THENL
[ASM_MESON_TAC[ITERATE_EQ; o_THM];
CONV_TAC "iterate.ml:SYM_CONV" SYM_CONV THEN
FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP ITERATE_IMAGE) THEN
ASM_MESON_TAC[]]);;
let ITERATE_EQ_GENERAL_INVERSES = prove
(`!op. monoidal op
==> !s:A->bool t:B->bool f:A->C g h k.
(!y. y IN t ==> k(y) IN s /\ h(k y) = y) /\
(!x. x IN s ==> h(x) IN t /\ k(h x) = x /\ g(h x) = f x)
==> iterate op s f = iterate op t g`,
REPEAT STRIP_TAC THEN
FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP ITERATE_EQ_GENERAL) THEN
EXISTS_TAC `h:A->B` THEN ASM_MESON_TAC[]);;
let ITERATE_INJECTION = prove
(`!op. monoidal op
==> !f:A->B p:A->A s.
FINITE s /\
(!x. x IN s ==> p x IN s) /\
(!x y. x IN s /\ y IN s /\ p x = p y ==> x = y)
==> iterate op s (f o p) = iterate op s f`,
REPEAT STRIP_TAC THEN CONV_TAC "iterate.ml:SYM_CONV" SYM_CONV THEN
FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP ITERATE_BIJECTION) THEN
MP_TAC(ISPECL [`s:A->bool`; `p:A->A`] SURJECTIVE_IFF_INJECTIVE) THEN
ASM_REWRITE_TAC[SUBSET; IN_IMAGE] THEN ASM_MESON_TAC[]);;
let ITERATE_UNION_NONZERO = prove
(`!op. monoidal op
==> !f:A->B s t.
FINITE(s) /\ FINITE(t) /\
(!x. x IN (s INTER t) ==> f x = neutral(op))
==> iterate op (s UNION t) f =
op (iterate op s f) (iterate op t f)`,
REPEAT STRIP_TAC THEN ONCE_REWRITE_TAC[GSYM ITERATE_SUPPORT] THEN
REWRITE_TAC[SUPPORT_CLAUSES] THEN
FIRST_ASSUM(MATCH_MP_TAC o MATCH_MP ITERATE_UNION) THEN
ASM_SIMP_TAC[FINITE_SUPPORT; DISJOINT; IN_INTER; IN_SUPPORT; EXTENSION] THEN
ASM_MESON_TAC[IN_INTER; NOT_IN_EMPTY]);;
let ITERATE_OP = prove
(`!op. monoidal op
==> !f g s. FINITE s
==> iterate op s (\x. op (f x) (g x)) =
op (iterate op s f) (iterate op s g)`,
GEN_TAC THEN DISCH_TAC THEN
GEN_TAC THEN GEN_TAC THEN MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
ASM_SIMP_TAC[ITERATE_CLAUSES; MONOIDAL_AC]);;
let ITERATE_SUPERSET = prove
(`!op. monoidal op
==> !f:A->B u v.
u SUBSET v /\
(!x. x IN v /\ ~(x IN u) ==> f(x) = neutral op)
==> iterate op v f = iterate op u f`,
REPEAT STRIP_TAC THEN ONCE_REWRITE_TAC[GSYM ITERATE_SUPPORT] THEN
AP_THM_TAC THEN AP_TERM_TAC THEN
REWRITE_TAC[support; EXTENSION; IN_ELIM_THM] THEN ASM_MESON_TAC[SUBSET]);;
let ITERATE_UNIV = prove
(`!op. monoidal op
==> !f:A->B s. support op f UNIV SUBSET s
==> iterate op s f = iterate op UNIV f`,
REWRITE_TAC[support; SUBSET; IN_ELIM_THM] THEN
REPEAT STRIP_TAC THEN CONV_TAC "iterate.ml:SYM_CONV" SYM_CONV THEN
FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP ITERATE_SUPERSET) THEN
ASM SET_TAC[]);;
let ITERATE_IMAGE_NONZERO = prove
(`!op. monoidal op
==> !g:B->C f:A->B s.
FINITE s /\
(!x y. x IN s /\ y IN s /\ ~(x = y) /\ f x = f y
==> g(f x) = neutral op)
==> iterate op (IMAGE f s) g = iterate op s (g o f)`,
GEN_TAC THEN DISCH_TAC THEN
GEN_TAC THEN GEN_TAC THEN ONCE_REWRITE_TAC[IMP_CONJ] THEN
MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
ASM_SIMP_TAC[IMAGE_CLAUSES; ITERATE_CLAUSES; FINITE_IMAGE] THEN
MAP_EVERY X_GEN_TAC [`a:A`; `s:A->bool`] THEN
REWRITE_TAC[IN_INSERT] THEN REPEAT STRIP_TAC THEN
SUBGOAL_THEN `iterate op s ((g:B->C) o (f:A->B)) = iterate op (IMAGE f s) g`
SUBST1_TAC THENL [ASM_MESON_TAC[]; ALL_TAC] THEN
REWRITE_TAC[IN_IMAGE] THEN COND_CASES_TAC THEN ASM_REWRITE_TAC[o_THM] THEN
SUBGOAL_THEN `(g:B->C) ((f:A->B) a) = neutral op` SUBST1_TAC THEN
ASM_MESON_TAC[MONOIDAL_AC]);;
let ITERATE_CASES = prove
(`!op. monoidal op
==> !s P f g:A->B.
FINITE s
==> iterate op s (\x. if P x then f x else g x) =
op (iterate op {x | x IN s /\ P x} f)
(iterate op {x | x IN s /\ ~P x} g)`,
REPEAT STRIP_TAC THEN MATCH_MP_TAC EQ_TRANS THEN
EXISTS_TAC
`op (iterate op {x | x IN s /\ P x} (\x. if P x then f x else (g:A->B) x))
(iterate op {x | x IN s /\ ~P x} (\x. if P x then f x else g x))` THEN
CONJ_TAC THENL
[FIRST_ASSUM(fun th -> ASM_SIMP_TAC[GSYM(MATCH_MP ITERATE_UNION th);
FINITE_RESTRICT;
SET_RULE `DISJOINT {x | x IN s /\ P x} {x | x IN s /\ ~P x}`]) THEN
AP_THM_TAC THEN AP_TERM_TAC THEN SET_TAC[];
BINOP_TAC THEN FIRST_ASSUM(MATCH_MP_TAC o MATCH_MP ITERATE_EQ) THEN
SIMP_TAC[IN_ELIM_THM]]);;
let ITERATE_OP_GEN = prove
(`!op. monoidal op
==> !f g:A->B s.
FINITE(support op f s) /\ FINITE(support op g s)
==> iterate op s (\x. op (f x) (g x)) =
op (iterate op s f) (iterate op s g)`,
REPEAT STRIP_TAC THEN ONCE_REWRITE_TAC[GSYM ITERATE_SUPPORT] THEN
MATCH_MP_TAC EQ_TRANS THEN
EXISTS_TAC `iterate op (support op f s UNION support op g s)
(\x. op ((f:A->B) x) (g x))` THEN
CONJ_TAC THENL
[CONV_TAC "iterate.ml:SYM_CONV" SYM_CONV;
ASM_SIMP_TAC[ITERATE_OP; FINITE_UNION] THEN BINOP_TAC] THEN
FIRST_ASSUM(MATCH_MP_TAC o MATCH_MP ITERATE_SUPERSET) THEN
REWRITE_TAC[support; IN_ELIM_THM; SUBSET; IN_UNION] THEN
ASM_MESON_TAC[monoidal]);;
let ITERATE_CLAUSES_NUMSEG = prove
(`!op. monoidal op
==> (!m. iterate op (m..0) f = if m = 0 then f(0) else neutral op) /\
(!m n. iterate op (m..SUC n) f =
if m <= SUC n then op (iterate op (m..n) f) (f(SUC n))
else iterate op (m..n) f)`,
REWRITE_TAC[NUMSEG_CLAUSES] THEN REPEAT STRIP_TAC THEN
COND_CASES_TAC THEN
ASM_SIMP_TAC[ITERATE_CLAUSES; FINITE_NUMSEG; IN_NUMSEG; FINITE_EMPTY] THEN
REWRITE_TAC[ARITH_RULE `~(SUC n <= n)`; NOT_IN_EMPTY] THEN
ASM_MESON_TAC[monoidal]);;
let ITERATE_PAIR = prove
(`!op. monoidal op
==> !f m n. iterate op (2*m..2*n+1) f =
iterate op (m..n) (\i. op (f(2*i)) (f(2*i+1)))`,
GEN_TAC THEN DISCH_TAC THEN GEN_TAC THEN GEN_TAC THEN
INDUCT_TAC THEN CONV_TAC "iterate.ml:NUM_REDUCE_CONV" NUM_REDUCE_CONV THENL
[ASM_SIMP_TAC[num_CONV `1`; ITERATE_CLAUSES_NUMSEG] THEN
REWRITE_TAC[ARITH_RULE `2 * m <= SUC 0 <=> m = 0`] THEN
COND_CASES_TAC THEN ASM_REWRITE_TAC[MULT_EQ_0; ARITH];
REWRITE_TAC[ARITH_RULE `2 * SUC n + 1 = SUC(SUC(2 * n + 1))`] THEN
ASM_SIMP_TAC[ITERATE_CLAUSES_NUMSEG] THEN
REWRITE_TAC[ARITH_RULE `2 * m <= SUC(SUC(2 * n + 1)) <=> m <= SUC n`;
ARITH_RULE `2 * m <= SUC(2 * n + 1) <=> m <= SUC n`] THEN
COND_CASES_TAC THEN ASM_REWRITE_TAC[] THEN
REWRITE_TAC[ARITH_RULE `2 * SUC n = SUC(2 * n + 1)`;
ARITH_RULE `2 * SUC n + 1 = SUC(SUC(2 * n + 1))`] THEN
ASM_MESON_TAC[monoidal]]);;
let ITERATE_REFLECT = prove
(`!op:A->A->A.
monoidal op
==> !x m n. iterate op (m..n) x =
if n < m then neutral op
else iterate op (0..n-m) (\i. x(n - i))`,
REWRITE_TAC[GSYM NUMSEG_EMPTY] THEN REPEAT STRIP_TAC THEN
COND_CASES_TAC THENL
[ASM_MESON_TAC[ITERATE_CLAUSES];
RULE_ASSUM_TAC(REWRITE_RULE[NUMSEG_EMPTY; NOT_LT])] THEN
FIRST_ASSUM(MP_TAC o
ISPECL [`\i:num. n - i`; `x:num->A`; `0..n-m`] o
MATCH_MP (INST_TYPE [`:X`,`:A`] ITERATE_IMAGE)) THEN
REWRITE_TAC[o_DEF; IN_NUMSEG] THEN
ANTS_TAC THENL [ARITH_TAC; DISCH_THEN(SUBST1_TAC o SYM)] THEN
AP_THM_TAC THEN AP_TERM_TAC THEN
REWRITE_TAC[EXTENSION; IN_IMAGE; IN_NUMSEG] THEN
REWRITE_TAC[UNWIND_THM2; ARITH_RULE
`x = n - y /\ 0 <= y /\ y <= n - m <=>
y = n - x /\ x <= n /\ y <= n - m`] THEN
ASM_ARITH_TAC);;
(* ------------------------------------------------------------------------- *)
(* Sums of natural numbers. *)
(* ------------------------------------------------------------------------- *)
prioritize_num();;
let nsum = new_definition
`nsum = iterate (+)`;;
let NEUTRAL_ADD = prove
(`neutral((+):num->num->num) = 0`,
REWRITE_TAC[neutral] THEN MATCH_MP_TAC SELECT_UNIQUE THEN
MESON_TAC[ADD_CLAUSES]);;
let NEUTRAL_MUL = prove
(`neutral(( * ):num->num->num) = 1`,
REWRITE_TAC[neutral] THEN MATCH_MP_TAC SELECT_UNIQUE THEN
MESON_TAC[MULT_CLAUSES; MULT_EQ_1]);;
let MONOIDAL_ADD = prove
(`monoidal((+):num->num->num)`,
REWRITE_TAC[monoidal; NEUTRAL_ADD] THEN ARITH_TAC);;
let MONOIDAL_MUL = prove
(`monoidal(( * ):num->num->num)`,
REWRITE_TAC[monoidal; NEUTRAL_MUL] THEN ARITH_TAC);;
let NSUM_DEGENERATE = prove
(`!f s. ~(FINITE {x | x IN s /\ ~(f x = 0)}) ==> nsum s f = 0`,
REPEAT GEN_TAC THEN REWRITE_TAC[nsum] THEN
SIMP_TAC[iterate; support; NEUTRAL_ADD]);;
let NSUM_CLAUSES = prove
(`(!f. nsum {} f = 0) /\
(!x f s. FINITE(s)
==> (nsum (x INSERT s) f =
if x IN s then nsum s f else f(x) + nsum s f))`,
REWRITE_TAC[nsum; GSYM NEUTRAL_ADD] THEN
ONCE_REWRITE_TAC[SWAP_FORALL_THM] THEN
MATCH_MP_TAC ITERATE_CLAUSES THEN REWRITE_TAC[MONOIDAL_ADD]);;
let NSUM_UNION = prove
(`!f s t. FINITE s /\ FINITE t /\ DISJOINT s t
==> (nsum (s UNION t) f = nsum s f + nsum t f)`,
SIMP_TAC[nsum; ITERATE_UNION; MONOIDAL_ADD]);;
let NSUM_DIFF = prove
(`!f s t. FINITE s /\ t SUBSET s
==> (nsum (s DIFF t) f = nsum s f - nsum t f)`,
REPEAT STRIP_TAC THEN
MATCH_MP_TAC(ARITH_RULE `(x + z = y:num) ==> (x = y - z)`) THEN
ASM_SIMP_TAC[nsum; ITERATE_DIFF; MONOIDAL_ADD]);;
let NSUM_INCL_EXCL = prove
(`!s t (f:A->num).
FINITE s /\ FINITE t
==> nsum s f + nsum t f = nsum (s UNION t) f + nsum (s INTER t) f`,
REWRITE_TAC[nsum; GSYM NEUTRAL_ADD] THEN
MATCH_MP_TAC ITERATE_INCL_EXCL THEN REWRITE_TAC[MONOIDAL_ADD]);;
let NSUM_SUPPORT = prove
(`!f s. nsum (support (+) f s) f = nsum s f`,
SIMP_TAC[nsum; iterate; SUPPORT_SUPPORT]);;
let NSUM_ADD = prove
(`!f g s. FINITE s ==> (nsum s (\x. f(x) + g(x)) = nsum s f + nsum s g)`,
SIMP_TAC[nsum; ITERATE_OP; MONOIDAL_ADD]);;
let NSUM_ADD_GEN = prove
(`!f g s.
FINITE {x | x IN s /\ ~(f x = 0)} /\ FINITE {x | x IN s /\ ~(g x = 0)}
==> nsum s (\x. f x + g x) = nsum s f + nsum s g`,
REWRITE_TAC[GSYM NEUTRAL_ADD; GSYM support; nsum] THEN
MATCH_MP_TAC ITERATE_OP_GEN THEN ACCEPT_TAC MONOIDAL_ADD);;
let NSUM_EQ_0 = prove
(`!f s. (!x:A. x IN s ==> (f(x) = 0)) ==> (nsum s f = 0)`,
REWRITE_TAC[nsum; GSYM NEUTRAL_ADD] THEN
SIMP_TAC[ITERATE_EQ_NEUTRAL; MONOIDAL_ADD]);;
let NSUM_0 = prove
(`!s:A->bool. nsum s (\n. 0) = 0`,
SIMP_TAC[NSUM_EQ_0]);;
let NSUM_LMUL = prove
(`!f c s:A->bool. nsum s (\x. c * f(x)) = c * nsum s f`,
REPEAT GEN_TAC THEN ASM_CASES_TAC `c = 0` THEN
ASM_REWRITE_TAC[MULT_CLAUSES; NSUM_0] THEN REWRITE_TAC[nsum] THEN
ONCE_REWRITE_TAC[ITERATE_EXPAND_CASES] THEN
SUBGOAL_THEN `support (+) (\x:A. c * f(x)) s = support (+) f s` SUBST1_TAC
THENL [ASM_SIMP_TAC[support; MULT_EQ_0; NEUTRAL_ADD]; ALL_TAC] THEN
COND_CASES_TAC THEN REWRITE_TAC[NEUTRAL_ADD; MULT_CLAUSES] THEN
UNDISCH_TAC `FINITE (support (+) f (s:A->bool))` THEN
SPEC_TAC(`support (+) f (s:A->bool)`,`t:A->bool`) THEN
REWRITE_TAC[GSYM nsum] THEN MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
SIMP_TAC[NSUM_CLAUSES; MULT_CLAUSES; LEFT_ADD_DISTRIB]);;
let NSUM_RMUL = prove
(`!f c s:A->bool. nsum s (\x. f(x) * c) = nsum s f * c`,
ONCE_REWRITE_TAC[MULT_SYM] THEN REWRITE_TAC[NSUM_LMUL]);;
let NSUM_LE = prove
(`!f g s. FINITE(s) /\ (!x. x IN s ==> f(x) <= g(x))
==> nsum s f <= nsum s g`,
ONCE_REWRITE_TAC[IMP_CONJ] THEN
GEN_TAC THEN GEN_TAC THEN MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
SIMP_TAC[NSUM_CLAUSES; LE_REFL; LE_ADD2; IN_INSERT]);;
let NSUM_LT = prove
(`!f g s:A->bool.
FINITE(s) /\ (!x. x IN s ==> f(x) <= g(x)) /\
(?x. x IN s /\ f(x) < g(x))
==> nsum s f < nsum s g`,
REPEAT GEN_TAC THEN
REPEAT(DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC MP_TAC)) THEN
DISCH_THEN(X_CHOOSE_THEN `a:A` STRIP_ASSUME_TAC) THEN
SUBGOAL_THEN `s = (a:A) INSERT (s DELETE a)` SUBST1_TAC THENL
[UNDISCH_TAC `a:A IN s` THEN SET_TAC[]; ALL_TAC] THEN
ASM_SIMP_TAC[NSUM_CLAUSES; FINITE_DELETE; IN_DELETE] THEN
ASM_SIMP_TAC[LTE_ADD2; NSUM_LE; IN_DELETE; FINITE_DELETE]);;
let NSUM_LT_ALL = prove
(`!f g s. FINITE s /\ ~(s = {}) /\ (!x. x IN s ==> f(x) < g(x))
==> nsum s f < nsum s g`,
MESON_TAC[MEMBER_NOT_EMPTY; LT_IMP_LE; NSUM_LT]);;
let NSUM_EQ = prove
(`!f g s. (!x. x IN s ==> (f x = g x)) ==> (nsum s f = nsum s g)`,
REWRITE_TAC[nsum] THEN
MATCH_MP_TAC ITERATE_EQ THEN REWRITE_TAC[MONOIDAL_ADD]);;
let NSUM_CONST = prove
(`!c s. FINITE s ==> (nsum s (\n. c) = (CARD s) * c)`,
GEN_TAC THEN MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
SIMP_TAC[NSUM_CLAUSES; CARD_CLAUSES] THEN
REPEAT STRIP_TAC THEN ARITH_TAC);;
let NSUM_POS_BOUND = prove
(`!f b s. FINITE s /\ nsum s f <= b ==> !x:A. x IN s ==> f x <= b`,
GEN_TAC THEN GEN_TAC THEN REWRITE_TAC[IMP_CONJ] THEN
MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
SIMP_TAC[NSUM_CLAUSES; NOT_IN_EMPTY; IN_INSERT] THEN
MESON_TAC[LE_0; ARITH_RULE
`0 <= x /\ 0 <= y /\ x + y <= b ==> x <= b /\ y <= b`]);;
let NSUM_EQ_0_IFF = prove
(`!s. FINITE s ==> (nsum s f = 0 <=> !x. x IN s ==> f x = 0)`,
REPEAT STRIP_TAC THEN EQ_TAC THEN ASM_SIMP_TAC[NSUM_EQ_0] THEN
ASM_MESON_TAC[ARITH_RULE `n = 0 <=> n <= 0`; NSUM_POS_BOUND]);;
let NSUM_POS_LT = prove
(`!f s:A->bool.
FINITE s /\ (?x. x IN s /\ 0 < f x)
==> 0 < nsum s f`,
SIMP_TAC[ARITH_RULE `0 < n <=> ~(n = 0)`; NSUM_EQ_0_IFF] THEN MESON_TAC[]);;
let NSUM_POS_LT_ALL = prove
(`!s f:A->num.
FINITE s /\ ~(s = {}) /\ (!i. i IN s ==> 0 < f i) ==> 0 < nsum s f`,
REPEAT STRIP_TAC THEN MATCH_MP_TAC NSUM_POS_LT THEN
ASM_MESON_TAC[MEMBER_NOT_EMPTY; REAL_LT_IMP_LE]);;
let NSUM_DELETE = prove
(`!f s a. FINITE s /\ a IN s ==> f(a) + nsum(s DELETE a) f = nsum s f`,
SIMP_TAC[nsum; ITERATE_DELETE; MONOIDAL_ADD]);;
let NSUM_SING = prove
(`!f x. nsum {x} f = f(x)`,
SIMP_TAC[NSUM_CLAUSES; FINITE_RULES; NOT_IN_EMPTY; ADD_CLAUSES]);;
let NSUM_DELTA = prove
(`!s a. nsum s (\x. if x = a:A then b else 0) = if a IN s then b else 0`,
REWRITE_TAC[nsum; GSYM NEUTRAL_ADD] THEN
SIMP_TAC[ITERATE_DELTA; MONOIDAL_ADD]);;
let NSUM_SWAP = prove
(`!f:A->B->num s t.
FINITE(s) /\ FINITE(t)
==> (nsum s (\i. nsum t (f i)) = nsum t (\j. nsum s (\i. f i j)))`,
GEN_TAC THEN REWRITE_TAC[IMP_CONJ; RIGHT_FORALL_IMP_THM] THEN