-
Notifications
You must be signed in to change notification settings - Fork 78
/
iterate.ml
3105 lines (2744 loc) · 129 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 *)
(* ========================================================================= *)
needs "sets.ml";;
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 EMPTY_NUMSEG = prove
(`!m n. n < m ==> m..n = {}`,
REWRITE_TAC[NUMSEG_EMPTY]);;
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 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 [DISCH_TAC; MESON_TAC[FINITE_IMAGE; FINITE_NUMSEG]] THEN
MP_TAC(ISPECL [`1..CARD(s:A->bool)`; `s:A->bool`]
CARD_EQ_BIJECTIONS) THEN
ASM_REWRITE_TAC[FINITE_NUMSEG; CARD_NUMSEG_1] THEN
MATCH_MP_TAC MONO_EXISTS THEN SET_TAC[]);;
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_INT_SEG = 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:A. !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: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 (f:A->B) =
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:A->B) 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:A->B) 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:A->B) 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:A->B) s. (support op f s) SUBSET s`,
SIMP_TAC[SUBSET; IN_SUPPORT]);;
let FINITE_SUPPORT = prove
(`!op (f:A->B) s. FINITE s ==> FINITE(support op f s)`,
MESON_TAC[SUPPORT_SUBSET; FINITE_SUBSET]);;
let SUPPORT_CLAUSES = prove
(`(!(f:A->C). support op f {} = {}) /\
(!(f:A->C) 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:A->C) x s. support op f (s DELETE x) = (support op f s) DELETE x) /\
(!(f:A->C) s t. support op f (s UNION t) =
(support op f s) UNION (support op f t)) /\
(!(f:A->C) s t. support op f (s INTER t) =
(support op f s) INTER (support op f t)) /\
(!(f:A->C) s t. support op f (s DIFF t) =
(support op f s) DIFF (support op f t)) /\
(!(f:A->B) (g:B->C) 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->B) 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->B) 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:A->B) s. iterate op (support op f s) f = iterate op s f`,
SIMP_TAC[iterate; SUPPORT_SUPPORT]);;
let ITERATE_EXPAND_CASES = prove
(`!op (f:A->B) 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. 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 (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:A->C). iterate op {} f = neutral op) /\
(!(f:B->C) 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:A->B) 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:A->B) 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:A->B).
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 (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 (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_CLOSED_NONEMPTY = prove
(`!op. monoidal op
==> !P. (!x y. P x /\ P y ==> P (op x y))
==> !f:A->B s. FINITE s /\ ~(s = {}) /\
(!x. x IN s ==> P(f x))
==> P(iterate op s f)`,
GEN_TAC THEN DISCH_TAC THEN GEN_TAC THEN DISCH_TAC THEN GEN_TAC THEN
REWRITE_TAC[IMP_CONJ] THEN MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
REWRITE_TAC[FORALL_IN_INSERT; NOT_IN_EMPTY; NOT_INSERT_EMPTY] THEN
MAP_EVERY X_GEN_TAC [`a:A`; `t:A->bool`] THEN
ASM_CASES_TAC `t:A->bool = {}` THEN
ASM_SIMP_TAC[ITERATE_SING] THEN ASM_SIMP_TAC[ITERATE_CLAUSES]);;
let ITERATE_RELATED_NONEMPTY = prove
(`!op. monoidal op
==> !R. (!x1 y1 x2 y2. R x1 x2 /\ R y1 y2 ==> R (op x1 y1) (op x2 y2))
==> !f:A->B g s.
FINITE s /\
~(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 DISCH_TAC THEN GEN_TAC THEN
GEN_TAC THEN REWRITE_TAC[IMP_CONJ] THEN
MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
REWRITE_TAC[FORALL_IN_INSERT; NOT_IN_EMPTY; NOT_INSERT_EMPTY] THEN
MAP_EVERY X_GEN_TAC [`a:A`; `t:A->bool`] THEN
ASM_CASES_TAC `t:A->bool = {}` THEN
ASM_SIMP_TAC[ITERATE_SING] THEN ASM_SIMP_TAC[ITERATE_CLAUSES]);;
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->B) 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(ONCE_DEPTH_CONV GEN_BETA_CONV) THEN
ASM_SIMP_TAC[PAIR_EQ];
DISCH_THEN SUBST1_TAC THEN REWRITE_TAC[o_DEF] THEN
CONV_TAC(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_RESTRICT_SET = prove
(`!op. monoidal op
==> !P s f:A->B. iterate op {x | x IN s /\ P x} f =
iterate op s (\x. if P x then f x else neutral op)`,
REPEAT STRIP_TAC THEN
ONCE_REWRITE_TAC[GSYM ITERATE_SUPPORT] THEN
REWRITE_TAC[support; IN_ELIM_THM] THEN
REWRITE_TAC[MESON[] `~((if P x then f x else a) = a) <=> P x /\ ~(f x = a)`;
GSYM CONJ_ASSOC] THEN
FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP ITERATE_EQ) THEN
SIMP_TAC[IN_ELIM_THM]);;
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 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 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:A->B) 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 SYM_CONV THEN
FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP ITERATE_SUPERSET) THEN
ASM SET_TAC[]);;
let ITERATE_SWAP = prove
(`!op. monoidal op
==> !f:A->B->C s t.
FINITE s /\ FINITE t
==> iterate op s (\i. iterate op t (f i)) =
iterate op t (\j. iterate op s (\i. f i j))`,
GEN_TAC THEN DISCH_TAC THEN
GEN_TAC THEN REWRITE_TAC[IMP_CONJ; RIGHT_FORALL_IMP_THM] THEN
MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
ASM_SIMP_TAC[ITERATE_CLAUSES] THEN
ASM_SIMP_TAC[ITERATE_EQ_NEUTRAL; GSYM ITERATE_OP]);;
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_IMAGE_GEN = prove
(`!op. monoidal op
==> !f:A->B g:A->C s.
FINITE s
==> iterate op s g =
iterate op (IMAGE f s)
(\y. iterate op {x | x IN s /\ f x = y} g)`,
REPEAT STRIP_TAC THEN MATCH_MP_TAC EQ_TRANS THEN EXISTS_TAC
`iterate op s (\x:A. iterate op {y:B | y IN IMAGE f s /\ (f x = y)}
(\y. (g:A->C) x))` THEN
CONJ_TAC THENL
[FIRST_ASSUM(MATCH_MP_TAC o MATCH_MP ITERATE_EQ) THEN
ASM_REWRITE_TAC[] THEN X_GEN_TAC `x:A` THEN DISCH_TAC THEN
SUBGOAL_THEN `{y | y IN IMAGE (f:A->B) s /\ f x = y} = {(f x)}`
SUBST1_TAC THENL [ASM SET_TAC[]; ASM_SIMP_TAC[ITERATE_SING]];
ASM_SIMP_TAC[ITERATE_RESTRICT_SET] THEN
FIRST_ASSUM(fun th -> W(MP_TAC o PART_MATCH (lhand o rand)
(MATCH_MP ITERATE_SWAP th) o lhand o snd)) THEN
ASM_SIMP_TAC[FINITE_IMAGE]]);;
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 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 :A =
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_CLAUSES_NUMSEG_LT = prove
(`!op. monoidal op
==> iterate op {i | i < 0} f :A = neutral op /\
(!k. iterate op {i | i < SUC k} f =
op (iterate op {i | i < k} f) (f k))`,
SIMP_TAC[NUMSEG_CLAUSES_LT; ITERATE_CLAUSES; FINITE_NUMSEG_LT] THEN
REWRITE_TAC[IN_ELIM_THM; LT_REFL; monoidal] THEN MESON_TAC[]);;
let ITERATE_CLAUSES_NUMSEG_LE = prove
(`!op. monoidal op
==> iterate op {i | i <= 0} f :A = f 0 /\
(!k. iterate op {i | i <= SUC k} f =
op (iterate op {i | i <= k} f) (f(SUC k)))`,
SIMP_TAC[NUMSEG_CLAUSES_LE; ITERATE_CLAUSES;
FINITE_NUMSEG_LE; ITERATE_SING] THEN
REWRITE_TAC[monoidal; IN_ELIM_THM; ARITH_RULE `~(SUC k <= k)`] THEN
MESON_TAC[]);;
let ITERATE_PAIR = prove
(`!op. monoidal op
==> !f m n. iterate op (2*m..2*n+1) f :A =
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 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);;
(* ------------------------------------------------------------------------- *)
(* A more general notion of iteration, using a specific order (<<=) *)
(* and hence applying to non-commutative operations, as well as giving *)
(* more refined notions of domain ("dom") and neutral element ("neut"). *)
(* Otherwise it tries to be stylistically similar to "iterate" above. *)
(* ------------------------------------------------------------------------- *)
let iterato = (new_specification ["iterato"] o prove)
(`?itty.
!dom neut op (<<=) k (f:K->A).
itty dom neut op (<<=) k f =
if FINITE {i | i IN k /\ f i IN dom DIFF {neut}} /\
~({i | i IN k /\ f i IN dom DIFF {neut}} = {})
then let i = if ?i. i IN k /\ f i IN dom DIFF {neut} /\
!j. j <<= i /\ j IN k /\ f j IN dom DIFF {neut}
==> j = i
then @i. i IN k /\ f i IN dom DIFF {neut} /\
!j. j <<= i /\ j IN k /\
f j IN dom DIFF {neut}
==> j = i
else @i. i IN k /\ f i IN dom DIFF {neut} in
op (f i) (itty dom neut op (<<=)
{j | j IN k DELETE i /\ f j IN dom DIFF {neut}} f)
else neut`,
REPLICATE_TAC 4 (ONCE_REWRITE_TAC[GSYM SKOLEM_THM]) THEN REPEAT GEN_TAC THEN
GEN_REWRITE_TAC I [EXISTS_SWAP_FUN_THM] THEN REWRITE_TAC[] THEN
GEN_REWRITE_TAC BINDER_CONV [SWAP_FORALL_THM] THEN
ONCE_REWRITE_TAC[GSYM SKOLEM_THM] THEN GEN_TAC THEN
MATCH_MP_TAC(MATCH_MP WF_REC (ISPEC
`\k. CARD {i | i IN k /\ (f:K->A) i IN dom DIFF {neut}}` WF_MEASURE)) THEN
REWRITE_TAC[MEASURE] THEN REPEAT STRIP_TAC THEN
COND_CASES_TAC THEN ASM_REWRITE_TAC[] THEN
LET_TAC THEN CONV_TAC(ONCE_DEPTH_CONV let_CONV) THEN
AP_TERM_TAC THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
REWRITE_TAC[SET_RULE
`{i | i IN k DIFF {a} /\ P i} = {i | i IN k /\ P i} DELETE a`] THEN
MATCH_MP_TAC CARD_PSUBSET THEN ASM_REWRITE_TAC[] THEN
REWRITE_TAC[PSUBSET_ALT] THEN CONJ_TAC THENL [SET_TAC[]; ALL_TAC] THEN
REWRITE_TAC[IN_ELIM_THM; IN_DELETE; GSYM CONJ_ASSOC] THEN
REWRITE_TAC[SET_RULE `p /\ q /\ ~(p /\ ~r /\ q) <=> r /\ p /\ q`] THEN
REWRITE_TAC[UNWIND_THM2] THEN
FIRST_X_ASSUM(MP_TAC o REWRITE_RULE[GSYM MEMBER_NOT_EMPTY] o CONJUNCT2) THEN
REWRITE_TAC[IN_ELIM_THM; IN_DELETE] THEN ASM_MESON_TAC[]);;
let ITERATO_SUPPORT = prove
(`!dom neut op (<<=) k (f:K->A).
iterato dom neut op (<<=) {i | i IN k /\ f i IN dom DIFF {neut}} f =
iterato dom neut op (<<=) k f`,
REPEAT GEN_TAC THEN ONCE_REWRITE_TAC[iterato] THEN
REWRITE_TAC[CONJ_ASSOC; SET_RULE `y IN {x | x IN s /\ P x} /\ P y <=>
y IN {x | x IN s /\ P x}`] THEN
REWRITE_TAC[IN_ELIM_THM; GSYM CONJ_ASSOC] THEN
COND_CASES_TAC THEN ASM_REWRITE_TAC[] THEN LET_TAC THEN
CONV_TAC(ONCE_DEPTH_CONV let_CONV) THEN AP_TERM_TAC THEN
AP_THM_TAC THEN AP_TERM_TAC THEN ASM SET_TAC[]);;
let ITERATO_EXPAND_CASES = prove
(`!dom neut op (<<=) k (f:K->A).
iterato dom neut op (<<=) k f =
if FINITE {i | i IN k /\ f i IN dom DIFF {neut}}
then iterato dom neut op (<<=) {i | i IN k /\ f i IN dom DIFF {neut}} f
else neut`,
REPEAT GEN_TAC THEN COND_CASES_TAC THENL
[REWRITE_TAC[ITERATO_SUPPORT];
GEN_REWRITE_TAC LAND_CONV [iterato] THEN ASM_REWRITE_TAC[]]);;
let ITERATO_CLAUSES_GEN = prove
(`!dom neut op (<<=) (f:K->A).
(iterato dom neut op (<<=) {} f = neut) /\
(!i k. FINITE {j | j IN k /\ f j IN dom DIFF {neut}} /\
(!j. j IN k ==> i = j \/ i <<= j \/ j <<= i) /\
(!j. j <<= i /\ j IN k /\ f j IN dom DIFF {neut} ==> j = i)
==> iterato dom neut op (<<=) (i INSERT k) f =
if f i IN dom ==> f i = neut \/ i IN k
then iterato dom neut op (<<=) k f
else op (f i) (iterato dom neut op (<<=) k f))`,
REPEAT GEN_TAC THEN CONJ_TAC THENL
[GEN_REWRITE_TAC LAND_CONV [iterato] THEN
ASM_REWRITE_TAC[NOT_IN_EMPTY; EMPTY_GSPEC];
REPEAT GEN_TAC THEN STRIP_TAC] THEN
ASM_CASES_TAC `(i:K) IN k` THEN
ASM_SIMP_TAC[COND_SWAP; SET_RULE `i IN k ==> i INSERT k = k`] THEN
REWRITE_TAC[SET_RULE `x IN dom ==> x = a <=> ~(x IN dom DIFF {a})`] THEN
REWRITE_TAC[COND_SWAP] THEN COND_CASES_TAC THENL
[GEN_REWRITE_TAC LAND_CONV [iterato] THEN
FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [IN_DIFF]) THEN
REWRITE_TAC[IN_SING] THEN STRIP_TAC;
ONCE_REWRITE_TAC[GSYM ITERATO_SUPPORT] THEN
AP_THM_TAC THEN AP_TERM_TAC THEN ASM SET_TAC[]] THEN
MATCH_MP_TAC(MESON[]
`q /\ p /\ x = z ==> (if p /\ q then x else y) = z`) THEN
REPEAT CONJ_TAC THENL
[ASM SET_TAC[];
MATCH_MP_TAC FINITE_SUBSET THEN
EXISTS_TAC `i INSERT {j | j IN k /\ (f:K->A) j IN dom DIFF {neut}}` THEN
ASM_REWRITE_TAC[FINITE_INSERT] THEN ASM SET_TAC[];
ALL_TAC] THEN
COND_CASES_TAC THENL
[FIRST_X_ASSUM(K ALL_TAC o check (is_exists o concl));
FIRST_X_ASSUM(MP_TAC o SPEC `i:K` o REWRITE_RULE[NOT_EXISTS_THM]) THEN
ASM SET_TAC[]] THEN
SUBGOAL_THEN
`(@i'. i' IN i INSERT k /\
(f:K->A) i' IN dom DIFF {neut} /\
(!j. j <<= i' /\ j IN i INSERT k /\ f j IN dom DIFF {neut}
==> j = i')) = i`
SUBST1_TAC THENL
[ALL_TAC;
CONV_TAC(ONCE_DEPTH_CONV let_CONV) THEN
ASM_SIMP_TAC[SET_RULE `~(i IN k) ==> (i INSERT k) DELETE i = k`] THEN
REWRITE_TAC[ITERATO_SUPPORT]] THEN
MATCH_MP_TAC SELECT_UNIQUE THEN X_GEN_TAC `j:K` THEN