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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
|
(* (c) Copyright 2006-2016 Microsoft Corporation and Inria. *)
(* Distributed under the terms of CeCILL-B. *)
From mathcomp Require Import ssreflect ssrfun ssrbool eqtype ssrnat seq.
(******************************************************************************)
(* The basic theory of paths over an eqType; this file is essentially a *)
(* complement to seq.v. Paths are non-empty sequences that obey a progression *)
(* relation. They are passed around in three parts: the head and tail of the *)
(* sequence, and a proof of a (boolean) predicate asserting the progression. *)
(* This "exploded" view is rarely embarrassing, as the first two parameters *)
(* are usually inferred from the type of the third; on the contrary, it saves *)
(* the hassle of constantly constructing and destructing a dependent record. *)
(* We define similarly cycles, for which we allow the empty sequence, *)
(* which represents a non-rooted empty cycle; by contrast, the "empty" path *)
(* from a point x is the one-item sequence containing only x. *)
(* We allow duplicates; uniqueness, if desired (as is the case for several *)
(* geometric constructions), must be asserted separately. We do provide *)
(* shorthand, but only for cycles, because the equational properties of *)
(* "path" and "uniq" are unfortunately incompatible (esp. wrt "cat"). *)
(* We define notations for the common cases of function paths, where the *)
(* progress relation is actually a function. In detail: *)
(* path e x p == x :: p is an e-path [:: x_0; x_1; ... ; x_n], i.e., we *)
(* e x_i x_{i+1} for all i < n. The path x :: p starts at x *)
(* and ends at last x p. *)
(* fpath f x p == x :: p is an f-path, where f is a function, i.e., p is of *)
(* the form [:: f x; f (f x); ...]. This is just a notation *)
(* for path (frel f) x p. *)
(* sorted e s == s is an e-sorted sequence: either s = [::], or s = x :: p *)
(* is an e-path (this is often used with e = leq or ltn). *)
(* cycle e c == c is an e-cycle: either c = [::], or c = x :: p with *)
(* x :: (rcons p x) an e-path. *)
(* fcycle f c == c is an f-cycle, for a function f. *)
(* traject f x n == the f-path of size n starting at x *)
(* := [:: x; f x; ...; iter n.-1 f x] *)
(* looping f x n == the f-paths of size greater than n starting at x loop *)
(* back, or, equivalently, traject f x n contains all *)
(* iterates of f at x. *)
(* merge e s1 s2 == the e-sorted merge of sequences s1 and s2: this is always *)
(* a permutation of s1 ++ s2, and is e-sorted when s1 and s2 *)
(* are and e is total. *)
(* sort e s == a permutation of the sequence s, that is e-sorted when e *)
(* is total (computed by a merge sort with the merge function *)
(* above). This sort function is also designed to be stable. *)
(* mem2 s x y == x, then y occur in the sequence (path) s; this is *)
(* non-strict: mem2 s x x = (x \in s). *)
(* next c x == the successor of the first occurrence of x in the sequence *)
(* c (viewed as a cycle), or x if x \notin c. *)
(* prev c x == the predecessor of the first occurrence of x in the *)
(* sequence c (viewed as a cycle), or x if x \notin c. *)
(* arc c x y == the sub-arc of the sequence c (viewed as a cycle) starting *)
(* at the first occurrence of x in c, and ending just before *)
(* the next occurrence of y (in cycle order); arc c x y *)
(* returns an unspecified sub-arc of c if x and y do not both *)
(* occur in c. *)
(* ucycle e c <-> ucycleb e c (ucycle e c is a Coercion target of type Prop) *)
(* ufcycle f c <-> c is a simple f-cycle, for a function f. *)
(* shorten x p == the tail a duplicate-free subpath of x :: p with the same *)
(* endpoints (x and last x p), obtained by removing all loops *)
(* from x :: p. *)
(* rel_base e e' h b <-> the function h is a functor from relation e to *)
(* relation e', EXCEPT at points whose image under h satisfy *)
(* the "base" predicate b: *)
(* e' (h x) (h y) = e x y UNLESS b (h x) holds *)
(* This is the statement of the side condition of the path *)
(* functorial mapping lemma map_path. *)
(* fun_base f f' h b <-> the function h is a functor from function f to f', *)
(* except at the preimage of predicate b under h. *)
(* We also provide three segmenting dependently-typed lemmas (splitP, splitPl *)
(* and splitPr) whose elimination split a path x0 :: p at an internal point x *)
(* as follows: *)
(* - splitP applies when x \in p; it replaces p with (rcons p1 x ++ p2), so *)
(* that x appears explicitly at the end of the left part. The elimination *)
(* of splitP will also simultaneously replace take (index x p) with p1 and *)
(* drop (index x p).+1 p with p2. *)
(* - splitPl applies when x \in x0 :: p; it replaces p with p1 ++ p2 and *)
(* simultaneously generates an equation x = last x0 p. *)
(* - splitPr applies when x \in p; it replaces p with (p1 ++ x :: p2), so x *)
(* appears explicitly at the start of the right part. *)
(* The parts p1 and p2 are computed using index/take/drop in all cases, but *)
(* only splitP attempts to substitute the explicit values. The substitution *)
(* of p can be deferred using the dependent equation generation feature of *)
(* ssreflect, e.g.: case/splitPr def_p: {1}p / x_in_p => [p1 p2] generates *)
(* the equation p = p1 ++ p2 instead of performing the substitution outright. *)
(* Similarly, eliminating the loop removal lemma shortenP simultaneously *)
(* replaces shorten e x p with a fresh constant p', and last x p with *)
(* last x p'. *)
(* Note that although all "path" functions actually operate on the *)
(* underlying sequence, we provide a series of lemmas that define their *)
(* interaction with the path and cycle predicates, e.g., the cat_path equation*)
(* can be used to split the path predicate after splitting the underlying *)
(* sequence. *)
(******************************************************************************)
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Section Paths.
Variables (n0 : nat) (T : Type).
Section Path.
Variables (x0_cycle : T) (e : rel T).
Fixpoint path x (p : seq T) :=
if p is y :: p' then e x y && path y p' else true.
Lemma cat_path x p1 p2 : path x (p1 ++ p2) = path x p1 && path (last x p1) p2.
Proof. by elim: p1 x => [|y p1 Hrec] x //=; rewrite Hrec -!andbA. Qed.
Lemma rcons_path x p y : path x (rcons p y) = path x p && e (last x p) y.
Proof. by rewrite -cats1 cat_path /= andbT. Qed.
Lemma pathP x p x0 :
reflect (forall i, i < size p -> e (nth x0 (x :: p) i) (nth x0 p i))
(path x p).
Proof.
elim: p x => [|y p IHp] x /=; first by left.
apply: (iffP andP) => [[e_xy /IHp e_p [] //] | e_p].
by split; [apply: (e_p 0) | apply/(IHp y) => i; apply: e_p i.+1].
Qed.
Definition cycle p := if p is x :: p' then path x (rcons p' x) else true.
Lemma cycle_path p : cycle p = path (last x0_cycle p) p.
Proof. by case: p => //= x p; rewrite rcons_path andbC. Qed.
Lemma rot_cycle p : cycle (rot n0 p) = cycle p.
Proof.
case: n0 p => [|n] [|y0 p] //=; first by rewrite /rot /= cats0.
rewrite /rot /= -[p in RHS](cat_take_drop n) -cats1 -catA cat_path.
case: (drop n p) => [|z0 q]; rewrite /= -cats1 !cat_path /= !andbT andbC //.
by rewrite last_cat; repeat bool_congr.
Qed.
Lemma rotr_cycle p : cycle (rotr n0 p) = cycle p.
Proof. by rewrite -rot_cycle rotrK. Qed.
Definition sorted s := if s is x :: s' then path x s' else true.
Lemma path_sorted x s : path x s -> sorted s.
Proof. by case: s => //= y s /andP[]. Qed.
Lemma path_min_sorted x s : all (e x) s -> path x s = sorted s.
Proof. by case: s => //= y s /andP [->]. Qed.
End Path.
Section SubPath_in.
Variable (P : {pred T}) (e e' : rel T).
Hypothesis (ee' : {in P &, subrel e e'}).
Lemma sub_path_in x s : all P (x :: s) -> path e x s -> path e' x s.
Proof.
by elim: s x => //= y s ihs x /and3P [? ? ?] /andP [/ee' -> //]; apply/ihs/andP.
Qed.
Lemma sub_cycle_in s : all P s -> cycle e s -> cycle e' s.
Proof.
case: s => //= x s /andP [Px Ps].
by apply: sub_path_in; rewrite /= all_rcons Px.
Qed.
Lemma sub_sorted_in s : all P s -> sorted e s -> sorted e' s.
Proof. by case: s => //; apply: sub_path_in. Qed.
End SubPath_in.
Section EqPath_in.
Variable (P : {pred T}) (e e' : rel T).
Hypothesis (ee' : {in P &, e =2 e'}).
Let e_e' : {in P &, subrel e e'}. Proof. by move=> ? ? ? ?; rewrite ee'. Qed.
Let e'_e : {in P &, subrel e' e}. Proof. by move=> ? ? ? ?; rewrite ee'. Qed.
Lemma eq_path_in x s : all P (x :: s) -> path e x s = path e' x s.
Proof. by move=> Pxs; apply/idP/idP; apply: sub_path_in Pxs. Qed.
Lemma eq_cycle_in s : all P s -> cycle e s = cycle e' s.
Proof. by move=> Ps; apply/idP/idP; apply: sub_cycle_in Ps. Qed.
End EqPath_in.
Section SubPath.
Variables e e' : rel T.
Lemma sub_path : subrel e e' -> forall x p, path e x p -> path e' x p.
Proof. by move=> ? ? ?; apply/sub_path_in/all_predT; apply: in2W. Qed.
Lemma sub_cycle : subrel e e' -> subpred (cycle e) (cycle e').
Proof. by move=> ee' [] // ? ?; apply: sub_path. Qed.
Lemma sub_sorted : subrel e e' -> subpred (sorted e) (sorted e').
Proof. by move=> ee' [] //=; apply: sub_path. Qed.
Lemma eq_path : e =2 e' -> path e =2 path e'.
Proof. by move=> ? ? ?; apply/eq_path_in/all_predT; apply: in2W. Qed.
Lemma eq_cycle : e =2 e' -> cycle e =1 cycle e'.
Proof. by move=> ee' [] // ? ?; apply: eq_path. Qed.
End SubPath.
Section Transitive_in.
Variables (P : {pred T}) (leT : rel T).
Hypothesis leT_tr : {in P & &, transitive leT}.
Lemma path_mask_in x m s :
all P (x :: s) -> path leT x s -> path leT x (mask m s).
Proof.
elim: m s x => [|[] m ih] [|y s] x //=.
by case/and3P=> ? ? ? /andP [-> /ih ->] //; apply/andP.
case/andP=> Px Pys /andP [xy ys]; case/andP: (Pys) => Py Ps.
case: (mask _ _) (all_mask m Ps) (ih s y Pys ys) => //=.
by move=> z t /andP [Pz Pt] /andP [] /(leT_tr Py Px Pz xy) ->.
Qed.
Lemma path_filter_in x a s :
all P (x :: s) -> path leT x s -> path leT x (filter a s).
Proof. by move=> Pxs; rewrite filter_mask; exact: path_mask_in. Qed.
Lemma sorted_mask_in m s : all P s -> sorted leT s -> sorted leT (mask m s).
Proof.
elim: m s => [|[] m ih] [|x s] //= Pxs; first exact: path_mask_in.
by move/path_sorted/ih; apply; case/andP: Pxs.
Qed.
Lemma sorted_filter_in a s : all P s -> sorted leT s -> sorted leT (filter a s).
Proof. rewrite filter_mask; exact: sorted_mask_in. Qed.
End Transitive_in.
Section Transitive.
Variable (leT : rel T).
Lemma order_path_min x s : transitive leT -> path leT x s -> all (leT x) s.
Proof.
move=> leT_tr; elim: s => //= y [//|z s] ihs /andP[xy yz]; rewrite xy {}ihs//.
by move: yz => /= /andP [/(leT_tr _ _ _ xy) ->].
Qed.
Hypothesis leT_tr : transitive leT.
Let leT_tr' : {in predT & &, transitive leT}. Proof. exact: in3W. Qed.
Lemma path_mask x m s : path leT x s -> path leT x (mask m s).
Proof. exact/path_mask_in/all_predT. Qed.
Lemma path_filter x a s : path leT x s -> path leT x (filter a s).
Proof. exact/path_filter_in/all_predT. Qed.
Lemma sorted_mask m s : sorted leT s -> sorted leT (mask m s).
Proof. exact/sorted_mask_in/all_predT. Qed.
Lemma sorted_filter a s : sorted leT s -> sorted leT (filter a s).
Proof. exact/sorted_filter_in/all_predT. Qed.
Lemma path_sortedE x s : path leT x s = all (leT x) s && sorted leT s.
Proof.
apply/idP/idP => [xs|/andP[/path_min_sorted<-//]].
by rewrite order_path_min//; apply: path_sorted xs.
Qed.
Lemma sorted_ltn_nth x0 s : sorted leT s ->
{in [pred n | n < size s] &, {homo nth x0 s : i j / i < j >-> leT i j}}.
Proof.
elim: s => //= x s ihs path_xs [|i] [|j] //=; rewrite -!topredE /= !ltnS.
- by move=> _ js _; apply/all_nthP/js/order_path_min.
- exact/ihs/path_sorted/path_xs.
Qed.
Hypothesis leT_refl : reflexive leT.
Lemma sorted_leq_nth x0 s : sorted leT s ->
{in [pred n | n < size s] &, {homo nth x0 s : i j / i <= j >-> leT i j}}.
Proof.
move=> s_sorted x y xs ys.
by rewrite leq_eqVlt=> /predU1P[->//|]; apply: sorted_ltn_nth.
Qed.
End Transitive.
End Paths.
Arguments pathP {T e x p}.
Arguments path_sorted {T e x s}.
Arguments path_min_sorted {T e x s}.
Arguments path_mask_in {T P leT} leT_tr {x m s}.
Arguments path_filter_in {T P leT} leT_tr {x a s}.
Arguments sorted_mask_in {T P leT} leT_tr {m s}.
Arguments sorted_filter_in {T P leT} leT_tr {a s}.
Arguments order_path_min {T leT x s}.
Arguments path_mask {T leT} leT_tr {x} m {s}.
Arguments path_filter {T leT} leT_tr {x} a {s}.
Arguments sorted_mask {T leT} leT_tr m {s}.
Arguments sorted_filter {T leT} leT_tr a {s}.
Arguments path_sortedE {T leT} leT_tr x s.
Arguments sorted_ltn_nth {T leT} leT_tr x0 {s}.
Arguments sorted_leq_nth {T leT} leT_tr leT_refl x0 {s}.
Lemma cycle_catC (T : Type) (e : rel T) (p q : seq T) :
cycle e (p ++ q) = cycle e (q ++ p).
Proof. by rewrite -rot_size_cat rot_cycle. Qed.
Section RevPath.
Variables (T : Type) (e : rel T).
Lemma rev_path x p :
path e (last x p) (rev (belast x p)) = path (fun z => e^~ z) x p.
Proof.
elim: p x => //= y p IHp x; rewrite rev_cons rcons_path -{}IHp andbC.
by rewrite -(last_cons x) -rev_rcons -lastI rev_cons last_rcons.
Qed.
Lemma rev_cycle p : cycle e (rev p) = cycle (fun z => e^~ z) p.
Proof.
case: p => //= x p; rewrite -rev_path last_rcons belast_rcons rev_cons.
by rewrite -[in LHS]cats1 cycle_catC.
Qed.
Lemma rev_sorted p : sorted e (rev p) = sorted (fun z => e^~ z) p.
Proof. by case: p => //= x p; rewrite -rev_path lastI rev_rcons. Qed.
End RevPath.
Section HomoPath.
Variables (T T' : Type) (P : {pred T}) (f : T -> T') (e : rel T) (e' : rel T').
Lemma path_map x s : path e' (f x) (map f s) = path (relpre f e') x s.
Proof. by elim: s x => //= y s <-. Qed.
Lemma cycle_map s : cycle e' (map f s) = cycle (relpre f e') s.
Proof. by case: s => //= ? ?; rewrite -map_rcons path_map. Qed.
Lemma sorted_map s : sorted e' (map f s) = sorted (relpre f e') s.
Proof. by case: s; last apply: path_map. Qed.
Lemma homo_path_in x s : {in P &, {homo f : x y / e x y >-> e' x y}} ->
all P (x :: s) -> path e x s -> path e' (f x) (map f s).
Proof. by move=> f_mono; rewrite path_map; apply: sub_path_in. Qed.
Lemma homo_cycle_in s : {in P &, {homo f : x y / e x y >-> e' x y}} ->
all P s -> cycle e s -> cycle e' (map f s).
Proof. by move=> f_mono; rewrite cycle_map; apply: sub_cycle_in. Qed.
Lemma homo_sorted_in s : {in P &, {homo f : x y / e x y >-> e' x y}} ->
all P s -> sorted e s -> sorted e' (map f s).
Proof. by move=> f_mono; rewrite sorted_map; apply: sub_sorted_in. Qed.
Lemma mono_path_in x s : {in P &, {mono f : x y / e x y >-> e' x y}} ->
all P (x :: s) -> path e' (f x) (map f s) = path e x s.
Proof. by move=> f_mono; rewrite path_map; apply: eq_path_in. Qed.
Lemma mono_cycle_in s : {in P &, {mono f : x y / e x y >-> e' x y}} ->
all P s -> cycle e' (map f s) = cycle e s.
Proof. by move=> f_mono; rewrite cycle_map; apply: eq_cycle_in. Qed.
Lemma mono_sorted_in s : {in P &, {mono f : x y / e x y >-> e' x y}} ->
all P s -> sorted e' (map f s) = sorted e s.
Proof. by case: s => // x s; apply: mono_path_in. Qed.
Lemma homo_path x s : {homo f : x y / e x y >-> e' x y} ->
path e x s -> path e' (f x) (map f s).
Proof. by move=> f_homo; rewrite path_map; apply: sub_path. Qed.
Lemma homo_cycle : {homo f : x y / e x y >-> e' x y} ->
{homo map f : s / cycle e s >-> cycle e' s}.
Proof. by move=> f_homo s hs; rewrite cycle_map (sub_cycle _ hs). Qed.
Lemma homo_sorted : {homo f : x y / e x y >-> e' x y} ->
{homo map f : s / sorted e s >-> sorted e' s}.
Proof. by move/homo_path => ? []. Qed.
Lemma mono_path x s : {mono f : x y / e x y >-> e' x y} ->
path e' (f x) (map f s) = path e x s.
Proof. by move=> f_mon; rewrite path_map; apply: eq_path. Qed.
Lemma mono_cycle : {mono f : x y / e x y >-> e' x y} ->
{mono map f : s / cycle e s >-> cycle e' s}.
Proof. by move=> ? ?; rewrite cycle_map; apply: eq_cycle. Qed.
Lemma mono_sorted : {mono f : x y / e x y >-> e' x y} ->
{mono map f : s / sorted e s >-> sorted e' s}.
Proof. by move=> f_mon [] //= x s; apply: mono_path. Qed.
End HomoPath.
Arguments path_map {T T' f e'}.
Arguments cycle_map {T T' f e'}.
Arguments sorted_map {T T' f e'}.
Arguments homo_path_in {T T' P f e e' x s}.
Arguments homo_cycle_in {T T' P f e e' s}.
Arguments homo_sorted_in {T T' P f e e' s}.
Arguments mono_path_in {T T' P f e e' x s}.
Arguments mono_cycle_in {T T' P f e e' s}.
Arguments mono_sorted_in {T T' P f e e' s}.
Arguments homo_path {T T' f e e' x s}.
Arguments homo_cycle {T T' f e e'}.
Arguments homo_sorted {T T' f e e'}.
Arguments mono_path {T T' f e e' x s}.
Arguments mono_cycle {T T' f e e'}.
Arguments mono_sorted {T T' f e e'}.
Section EqSorted.
Variables (T : eqType) (leT : rel T).
Implicit Type s : seq T.
Local Notation path := (path leT).
Local Notation sorted := (sorted leT).
Hypothesis leT_tr : transitive leT.
Lemma subseq_path x s1 s2 : subseq s1 s2 -> path x s2 -> path x s1.
Proof. by case/subseqP => m _ ->; apply/path_mask. Qed.
Lemma subseq_sorted s1 s2 : subseq s1 s2 -> sorted s2 -> sorted s1.
Proof. by case/subseqP => m _ ->; apply/sorted_mask. Qed.
Lemma sorted_uniq : irreflexive leT -> forall s, sorted s -> uniq s.
Proof.
move=> leT_irr; elim=> //= x s IHs s_ord.
rewrite (IHs (path_sorted s_ord)) andbT; apply/negP=> s_x.
by case/allPn: (order_path_min leT_tr s_ord); exists x; rewrite // leT_irr.
Qed.
Lemma sorted_eq : antisymmetric leT ->
forall s1 s2, sorted s1 -> sorted s2 -> perm_eq s1 s2 -> s1 = s2.
Proof.
move=> leT_asym; elim=> [|x1 s1 IHs1] s2 //= ord_s1 ord_s2 eq_s12.
by case: {+}s2 (perm_size eq_s12).
have s2_x1: x1 \in s2 by rewrite -(perm_mem eq_s12) mem_head.
case: s2 s2_x1 eq_s12 ord_s2 => //= x2 s2; rewrite in_cons.
case: eqP => [<- _| ne_x12 /= s2_x1] eq_s12 ord_s2.
by rewrite {IHs1}(IHs1 s2) ?(@path_sorted _ leT x1) // -(perm_cons x1).
case: (ne_x12); apply: leT_asym; rewrite (allP (order_path_min _ ord_s2))//.
have: x2 \in x1 :: s1 by rewrite (perm_mem eq_s12) mem_head.
case/predU1P=> [eq_x12 | s1_x2]; first by case ne_x12.
by rewrite (allP (order_path_min _ ord_s1)).
Qed.
Lemma irr_sorted_eq : irreflexive leT ->
forall s1 s2, sorted s1 -> sorted s2 -> s1 =i s2 -> s1 = s2.
Proof.
move=> leT_irr s1 s2 s1_sort s2_sort eq_s12.
have: antisymmetric leT.
by move=> m n /andP[? ltnm]; case/idP: (leT_irr m); apply: leT_tr ltnm.
by move/sorted_eq; apply=> //; apply: uniq_perm => //; apply: sorted_uniq.
Qed.
Lemma sorted_ltn_index s :
sorted s -> {in s &, forall x y, index x s < index y s -> leT x y}.
Proof.
case: s => // x0 s' s_sorted x y xs ys /(sorted_ltn_nth leT_tr x0 s_sorted).
by rewrite ?nth_index ?[_ \in gtn _]index_mem //; apply.
Qed.
Hypothesis leT_refl : reflexive leT.
Lemma sorted_leq_index s :
sorted s -> {in s &, forall x y, index x s <= index y s -> leT x y}.
Proof.
case: s => // x0 s' s_sorted x y xs ys.
move/(sorted_leq_nth leT_tr leT_refl x0 s_sorted).
by rewrite ?nth_index ?[_ \in gtn _]index_mem //; apply.
Qed.
End EqSorted.
Arguments sorted_ltn_index {T leT} leT_tr {s}.
Arguments sorted_leq_index {T leT} leT_tr leT_refl {s}.
Section EqPath.
Variables (n0 : nat) (T : eqType) (e : rel T).
Implicit Type p : seq T.
Variant split x : seq T -> seq T -> seq T -> Type :=
Split p1 p2 : split x (rcons p1 x ++ p2) p1 p2.
Lemma splitP p x (i := index x p) :
x \in p -> split x p (take i p) (drop i.+1 p).
Proof. by rewrite -has_pred1 => /split_find[? ? ? /eqP->]; constructor. Qed.
Variant splitl x1 x : seq T -> Type :=
Splitl p1 p2 of last x1 p1 = x : splitl x1 x (p1 ++ p2).
Lemma splitPl x1 p x : x \in x1 :: p -> splitl x1 x p.
Proof.
rewrite inE; case: eqP => [->| _ /splitP[]]; first by rewrite -(cat0s p).
by split; apply: last_rcons.
Qed.
Variant splitr x : seq T -> Type :=
Splitr p1 p2 : splitr x (p1 ++ x :: p2).
Lemma splitPr p x : x \in p -> splitr x p.
Proof. by case/splitP=> p1 p2; rewrite cat_rcons. Qed.
Fixpoint next_at x y0 y p :=
match p with
| [::] => if x == y then y0 else x
| y' :: p' => if x == y then y' else next_at x y0 y' p'
end.
Definition next p x := if p is y :: p' then next_at x y y p' else x.
Fixpoint prev_at x y0 y p :=
match p with
| [::] => if x == y0 then y else x
| y' :: p' => if x == y' then y else prev_at x y0 y' p'
end.
Definition prev p x := if p is y :: p' then prev_at x y y p' else x.
Lemma next_nth p x :
next p x = if x \in p then
if p is y :: p' then nth y p' (index x p) else x
else x.
Proof.
case: p => //= y0 p.
elim: p {2 3 5}y0 => [|y' p IHp] y /=; rewrite (eq_sym y) inE;
by case: ifP => // _; apply: IHp.
Qed.
Lemma prev_nth p x :
prev p x = if x \in p then
if p is y :: p' then nth y p (index x p') else x
else x.
Proof.
case: p => //= y0 p; rewrite inE orbC.
elim: p {2 5}y0 => [|y' p IHp] y; rewrite /= ?inE // (eq_sym y').
by case: ifP => // _; apply: IHp.
Qed.
Lemma mem_next p x : (next p x \in p) = (x \in p).
Proof.
rewrite next_nth; case p_x: (x \in p) => //.
case: p (index x p) p_x => [|y0 p'] //= i _; rewrite inE.
have [lt_ip | ge_ip] := ltnP i (size p'); first by rewrite orbC mem_nth.
by rewrite nth_default ?eqxx.
Qed.
Lemma mem_prev p x : (prev p x \in p) = (x \in p).
Proof.
rewrite prev_nth; case p_x: (x \in p) => //; case: p => [|y0 p] // in p_x *.
by apply mem_nth; rewrite /= ltnS index_size.
Qed.
(* ucycleb is the boolean predicate, but ucycle is defined as a Prop *)
(* so that it can be used as a coercion target. *)
Definition ucycleb p := cycle e p && uniq p.
Definition ucycle p : Prop := cycle e p && uniq p.
(* Projections, used for creating local lemmas. *)
Lemma ucycle_cycle p : ucycle p -> cycle e p.
Proof. by case/andP. Qed.
Lemma ucycle_uniq p : ucycle p -> uniq p.
Proof. by case/andP. Qed.
Lemma next_cycle p x : cycle e p -> x \in p -> e x (next p x).
Proof.
case: p => //= y0 p; elim: p {1 3 5}y0 => [|z p IHp] y /=; rewrite inE.
by rewrite andbT; case: (x =P y) => // ->.
by case/andP=> eyz /IHp; case: (x =P y) => // ->.
Qed.
Lemma prev_cycle p x : cycle e p -> x \in p -> e (prev p x) x.
Proof.
case: p => //= y0 p; rewrite inE orbC.
elim: p {1 5}y0 => [|z p IHp] y /=; rewrite ?inE.
by rewrite andbT; case: (x =P y0) => // ->.
by case/andP=> eyz /IHp; case: (x =P z) => // ->.
Qed.
Lemma rot_ucycle p : ucycle (rot n0 p) = ucycle p.
Proof. by rewrite /ucycle rot_uniq rot_cycle. Qed.
Lemma rotr_ucycle p : ucycle (rotr n0 p) = ucycle p.
Proof. by rewrite /ucycle rotr_uniq rotr_cycle. Qed.
(* The "appears no later" partial preorder defined by a path. *)
Definition mem2 p x y := y \in drop (index x p) p.
Lemma mem2l p x y : mem2 p x y -> x \in p.
Proof.
by rewrite /mem2 -!index_mem size_drop ltn_subRL; apply/leq_ltn_trans/leq_addr.
Qed.
Lemma mem2lf {p x y} : x \notin p -> mem2 p x y = false.
Proof. exact/contraNF/mem2l. Qed.
Lemma mem2r p x y : mem2 p x y -> y \in p.
Proof.
by rewrite -[in y \in p](cat_take_drop (index x p) p) mem_cat orbC /mem2 => ->.
Qed.
Lemma mem2rf {p x y} : y \notin p -> mem2 p x y = false.
Proof. exact/contraNF/mem2r. Qed.
Lemma mem2_cat p1 p2 x y :
mem2 (p1 ++ p2) x y = mem2 p1 x y || mem2 p2 x y || (x \in p1) && (y \in p2).
Proof.
rewrite [LHS]/mem2 index_cat fun_if if_arg !drop_cat addKn.
case: ifPn => [p1x | /mem2lf->]; last by rewrite ltnNge leq_addr orbF.
by rewrite index_mem p1x mem_cat -orbA (orb_idl (@mem2r _ _ _)).
Qed.
Lemma mem2_splice p1 p3 x y p2 :
mem2 (p1 ++ p3) x y -> mem2 (p1 ++ p2 ++ p3) x y.
Proof.
by rewrite !mem2_cat mem_cat andb_orr orbC => /or3P[]->; rewrite ?orbT.
Qed.
Lemma mem2_splice1 p1 p3 x y z :
mem2 (p1 ++ p3) x y -> mem2 (p1 ++ z :: p3) x y.
Proof. exact: mem2_splice [::z]. Qed.
Lemma mem2_cons x p y z :
mem2 (x :: p) y z = (if x == y then z \in x :: p else mem2 p y z).
Proof. by rewrite [LHS]/mem2 /=; case: ifP. Qed.
Lemma mem2_seq1 x y z : mem2 [:: x] y z = (y == x) && (z == x).
Proof. by rewrite mem2_cons eq_sym inE. Qed.
Lemma mem2_last y0 p x : mem2 p x (last y0 p) = (x \in p).
Proof.
apply/idP/idP; first exact: mem2l; rewrite -index_mem /mem2 => p_x.
by rewrite -nth_last -(subnKC p_x) -nth_drop mem_nth // size_drop subnSK.
Qed.
Lemma mem2l_cat {p1 p2 x} : x \notin p1 -> mem2 (p1 ++ p2) x =1 mem2 p2 x.
Proof. by move=> p1'x y; rewrite mem2_cat (negPf p1'x) mem2lf ?orbF. Qed.
Lemma mem2r_cat {p1 p2 x y} : y \notin p2 -> mem2 (p1 ++ p2) x y = mem2 p1 x y.
Proof.
by move=> p2'y; rewrite mem2_cat (negPf p2'y) -orbA orbC andbF mem2rf.
Qed.
Lemma mem2lr_splice {p1 p2 p3 x y} :
x \notin p2 -> y \notin p2 -> mem2 (p1 ++ p2 ++ p3) x y = mem2 (p1 ++ p3) x y.
Proof.
move=> p2'x p2'y; rewrite catA !mem2_cat !mem_cat.
by rewrite (negPf p2'x) (negPf p2'y) (mem2lf p2'x) andbF !orbF.
Qed.
Lemma mem2E s x y :
mem2 s x y = subseq (if x == y then [:: x] else [:: x; y]) s.
Proof.
elim: s => [| h s]; first by case: ifP.
rewrite mem2_cons => ->.
do 2 rewrite inE (fun_if subseq) !if_arg !sub1seq /=.
by have [->|] := eqVneq; case: eqVneq.
Qed.
Variant split2r x y : seq T -> Type :=
Split2r p1 p2 of y \in x :: p2 : split2r x y (p1 ++ x :: p2).
Lemma splitP2r p x y : mem2 p x y -> split2r x y p.
Proof.
move=> pxy; have px := mem2l pxy.
have:= pxy; rewrite /mem2 (drop_nth x) ?index_mem ?nth_index //.
by case/splitP: px => p1 p2; rewrite cat_rcons.
Qed.
Fixpoint shorten x p :=
if p is y :: p' then
if x \in p then shorten x p' else y :: shorten y p'
else [::].
Variant shorten_spec x p : T -> seq T -> Type :=
ShortenSpec p' of path e x p' & uniq (x :: p') & subpred (mem p') (mem p) :
shorten_spec x p (last x p') p'.
Lemma shortenP x p : path e x p -> shorten_spec x p (last x p) (shorten x p).
Proof.
move=> e_p; have: x \in x :: p by apply: mem_head.
elim: p x {1 3 5}x e_p => [|y2 p IHp] x y1.
by rewrite mem_seq1 => _ /eqP->.
rewrite inE orbC /= => /andP[ey12 {}/IHp IHp].
case: ifPn => [y2p_x _ | not_y2p_x /eqP def_x].
have [p' e_p' Up' p'p] := IHp _ y2p_x.
by split=> // y /p'p; apply: predU1r.
have [p' e_p' Up' p'p] := IHp y2 (mem_head y2 p).
have{} p'p z: z \in y2 :: p' -> z \in y2 :: p.
by rewrite !inE; case: (z == y2) => // /p'p.
rewrite -(last_cons y1) def_x; split=> //=; first by rewrite ey12.
by rewrite (contra (p'p y1)) -?def_x.
Qed.
End EqPath.
(* Ordered paths and sorting. *)
Section SortSeq.
Variables (T : Type) (leT : rel T).
Local Notation path := (path leT).
Local Notation sorted := (sorted leT).
Fixpoint merge s1 :=
if s1 is x1 :: s1' then
let fix merge_s1 s2 :=
if s2 is x2 :: s2' then
if leT x1 x2 then x1 :: merge s1' s2 else x2 :: merge_s1 s2'
else s1 in
merge_s1
else id.
Arguments merge !s1 !s2 : rename.
Fixpoint merge_sort_push s1 ss :=
match ss with
| [::] :: ss' | [::] as ss' => s1 :: ss'
| s2 :: ss' => [::] :: merge_sort_push (merge s2 s1) ss'
end.
Fixpoint merge_sort_pop s1 ss :=
if ss is s2 :: ss' then merge_sort_pop (merge s2 s1) ss' else s1.
Fixpoint merge_sort_rec ss s :=
if s is [:: x1, x2 & s'] then
let s1 := if leT x1 x2 then [:: x1; x2] else [:: x2; x1] in
merge_sort_rec (merge_sort_push s1 ss) s'
else merge_sort_pop s ss.
Definition sort := merge_sort_rec [::].
(* The following definition `sort_rec1` is an auxiliary function for *)
(* inductive reasoning on `sort`. One can rewrite `sort le s` to *)
(* `sort_rec1 le [::] s` by `sortE` and apply the simple structural induction *)
(* on `s` to reason about it. *)
Fixpoint sort_rec1 ss s :=
if s is x :: s then sort_rec1 (merge_sort_push [:: x] ss) s else
merge_sort_pop [::] ss.
Lemma sortE s : sort s = sort_rec1 [::] s.
Proof.
transitivity (sort_rec1 [:: nil] s); last by case: s.
rewrite /sort; move: [::] {2}_.+1 (ltnSn (size s)./2) => ss n.
by elim: n => // n IHn in ss s *; case: s => [|x [|y s]] //= /IHn->.
Qed.
Hypothesis leT_total : total leT.
Lemma merge_path x s1 s2 : path x s1 -> path x s2 -> path x (merge s1 s2).
Proof.
elim: s1 s2 x => //= x1 s1 IHs1.
elim=> //= x2 s2 IHs2 x /andP[le_x_x1 ord_s1] /andP[le_x_x2 ord_s2].
case: ifP => le_x21 /=; first by rewrite le_x_x1 {}IHs1 //= le_x21.
by rewrite le_x_x2 IHs2 //=; have:= leT_total x1 x2; rewrite le_x21 /= => ->.
Qed.
Lemma merge_sorted s1 s2 : sorted s1 -> sorted s2 -> sorted (merge s1 s2).
Proof.
case: s1 s2 => [|x1 s1] [|x2 s2] //= ord_s1 ord_s2.
case: ifP => le_x21 /=; first by apply: merge_path => //=; rewrite le_x21.
apply: (@merge_path x2 (x1 :: s1)) => //=.
by have:= (leT_total x1 x2); rewrite le_x21 /= => ->.
Qed.
Lemma sort_sorted s : sorted (sort s).
Proof.
rewrite sortE; have: all sorted [::] by [].
elim: s [::] => /= [|x s ihs] ss allss.
- elim: ss [::] (isT : sorted [::]) allss => //= s ss ihss t ht /andP [hs].
exact/ihss/merge_sorted.
- apply/ihs; elim: ss [:: x] allss (isT : sorted [:: x]) => /= [_ _ -> //|].
by move=> {x s ihs} [|x s] ss ihss t /andP [] hs allss ht;
[rewrite /= ht | apply/ihss/merge_sorted].
Qed.
Lemma size_merge s1 s2 : size (merge s1 s2) = size (s1 ++ s2).
Proof.
rewrite size_cat; elim: s1 s2 => // x s1 IH1.
elim=> //= [|y s2 IH2]; first by rewrite addn0.
by case: leT; rewrite /= ?IH1 ?IH2 !addnS.
Qed.
Remark size_merge_sort_push s1 :
let graded ss := forall i, size (nth [::] ss i) \in pred2 0 (2 ^ (i + 1)) in
size s1 = 2 -> {homo merge_sort_push s1 : ss / graded ss}.
Proof.
set n := {2}1; rewrite -[RHS]/(2 ^ n) => graded sz_s1 ss.
elim: ss => [|s2 ss IHss] in (n) graded s1 sz_s1 * => sz_ss i //=.
by case: i => [|[]] //; rewrite sz_s1 inE eqxx orbT.
case: s2 i => [|x s2] [|i] //= in sz_ss *; first by rewrite sz_s1 inE eqxx orbT.
exact: (sz_ss i.+1).
rewrite addSnnS; apply: IHss i => [|i]; last by rewrite -addSnnS (sz_ss i.+1).
by rewrite size_merge size_cat sz_s1 (eqnP (sz_ss 0)) addnn expnS mul2n.
Qed.
Hypothesis leT_tr : transitive leT.
Lemma sorted_merge s t : sorted (s ++ t) -> merge s t = s ++ t.
Proof.
elim: s => //= x s; case: t; rewrite ?cats0 //= => y t ih hp.
move: (order_path_min leT_tr hp).
by rewrite ih ?(path_sorted hp) // all_cat /= => /and3P [_ -> _].
Qed.
Lemma sorted_sort s : sorted s -> sort s = s.
Proof.
pose catss := foldr (fun x => cat ^~ x) (Nil T).
rewrite -{1 3}[s]/(catss [::] ++ s) sortE; elim: s [::] => /= [|x s ihs] ss.
- elim: ss [::] => //= s ss ihss t; rewrite -catA => h_sorted.
rewrite -ihss ?sorted_merge //.
by elim: (catss _) h_sorted => //= ? ? ih /path_sorted.
- move=> h_sorted.
suff x_ss_E: catss (merge_sort_push [:: x] ss) = catss ([:: x] :: ss)
by rewrite (catA _ [:: _]) -[catss _ ++ _]/(catss ([:: x] :: ss)) -x_ss_E
ihs // x_ss_E /= -catA.
have {h_sorted}: sorted (catss ss ++ [:: x]).
case: (catss _) h_sorted => //= ? ?.
by rewrite (catA _ [:: _]) cat_path => /andP [].
elim: ss [:: x] => {x s ihs} //= -[|x s] ss ihss t h_sorted;
rewrite /= cats0 // sorted_merge ?ihss ?catA //.
by elim: (catss ss) h_sorted => //= ? ? ih /path_sorted.
Qed.
End SortSeq.
Arguments merge {T} relT !s1 !s2 : rename.
Arguments merge_path {T leT} leT_total {x s1 s2}.
Arguments merge_sorted {T leT} leT_total {s1 s2}.
Arguments sort_sorted {T leT} leT_total s.
Arguments sorted_merge {T leT} leT_tr {s t}.
Arguments sorted_sort {T leT} leT_tr {s}.
Section SortMap.
Variables (T T' : Type) (f : T' -> T).
Section Monotonicity.
Variables (leT' : rel T') (leT : rel T).
Hypothesis f_mono : {mono f : x y / leT' x y >-> leT x y}.
Lemma map_merge : {morph map f : s1 s2 / merge leT' s1 s2 >-> merge leT s1 s2}.
Proof.
elim=> //= x s1 IHs1; elim => [|y s2 IHs2] //=; rewrite f_mono.
by case: leT'; rewrite /= ?IHs1 ?IHs2.
Qed.
Lemma map_sort : {morph map f : s1 / sort leT' s1 >-> sort leT s1}.
Proof.
move=> s; rewrite !sortE -[[::] in RHS]/(map (map f) [::]).
elim: s [::] => /= [|x s ihs] ss; rewrite -/(map f [::]) -/(map f [:: _]);
first by elim: ss [::] => //= x ss ihss ?; rewrite ihss map_merge.
rewrite ihs -/(map f [:: x]); congr sort_rec1.
by elim: ss [:: x] => {x s ihs} [|[|x s] ss ihss] //= ?; rewrite ihss map_merge.
Qed.
End Monotonicity.
Variable leT : rel T.
Lemma merge_map s1 s2 :
merge leT (map f s1) (map f s2) = map f (merge (relpre f leT) s1 s2).
Proof. exact/esym/map_merge. Qed.
Lemma sort_map s : sort leT (map f s) = map f (sort (relpre f leT) s).
Proof. exact/esym/map_sort. Qed.
End SortMap.
Arguments map_merge {T T' f leT' leT}.
Arguments map_sort {T T' f leT' leT}.
Arguments merge_map {T T' f leT}.
Arguments sort_map {T T' f leT}.
Section EqSortSeq.
Variables (T : eqType) (leT : rel T).
Lemma perm_merge s1 s2 : perm_eql (merge leT s1 s2) (s1 ++ s2).
Proof.
apply/permPl; rewrite perm_sym; elim: s1 s2 => //= x1 s1 IHs1.
elim; rewrite ?cats0 //= => x2 s2 IHs2.
by case: ifP; last rewrite (perm_catCA (_ :: _) [:: x2]); rewrite perm_cons.
Qed.
Lemma mem_merge s1 s2 : merge leT s1 s2 =i s1 ++ s2.
Proof. by apply: perm_mem; rewrite perm_merge. Qed.
Lemma merge_uniq s1 s2 : uniq (merge leT s1 s2) = uniq (s1 ++ s2).
Proof. by apply: perm_uniq; rewrite perm_merge. Qed.
Lemma perm_sort s : perm_eql (sort leT s) s.
Proof.
apply/permPl; rewrite sortE perm_sym -{1}[s]/(flatten [::] ++ s).
elim: s [::] => /= [|x s ihs] ss.
- elim: ss [::] => //= s ss ihss t.
by rewrite -(permPr (ihss _)) -catA perm_catCA perm_cat2l -perm_merge.
- rewrite -(permPr (ihs _)) -(perm_catCA [:: x]) catA perm_cat2r.
elim: {x s ihs} ss [:: x] => [|[|x s] ss ihss] t //.
by rewrite -(permPr (ihss _)) catA perm_cat2r perm_catC -perm_merge.
Qed.
Lemma mem_sort s : sort leT s =i s.
Proof. by apply: perm_mem; rewrite perm_sort. Qed.
Lemma sort_uniq s : uniq (sort leT s) = uniq s.
Proof. by apply: perm_uniq; rewrite perm_sort. Qed.
Lemma perm_sortP :
total leT -> transitive leT -> antisymmetric leT ->
forall s1 s2, reflect (sort leT s1 = sort leT s2) (perm_eq s1 s2).
Proof.
move=> leT_total leT_tr leT_asym s1 s2.
apply: (iffP idP) => eq12; last by rewrite -perm_sort eq12 perm_sort.
apply: (sorted_eq leT_tr leT_asym); rewrite ?sort_sorted //.
by rewrite perm_sort (permPl eq12) -perm_sort.
Qed.
End EqSortSeq.
Lemma perm_iota_sort (T : Type) (leT : rel T) x0 s :
{i_s : seq nat | perm_eq i_s (iota 0 (size s)) &
sort leT s = map (nth x0 s) i_s}.
Proof.
exists (sort (relpre (nth x0 s) leT) (iota 0 (size s))).
by rewrite perm_sort.
by rewrite -[X in sort leT X](mkseq_nth x0) sort_map.
Qed.
Lemma size_sort (T : Type) (leT : rel T) s : size (sort leT s) = size s.
Proof.
case: s => [|x s] //; have [s1 pp qq] := perm_iota_sort leT x (x :: s).
by rewrite qq size_map (perm_size pp) size_iota.
Qed.
Lemma ltn_sorted_uniq_leq s : sorted ltn s = uniq s && sorted leq s.
Proof.
case: s => //= n s; elim: s n => //= m s IHs n.
rewrite inE ltn_neqAle negb_or IHs -!andbA.
case sn: (n \in s); last do !bool_congr.
rewrite andbF; apply/and5P=> [[ne_nm lenm _ _ le_ms]]; case/negP: ne_nm.
by rewrite eqn_leq lenm; apply: (allP (order_path_min leq_trans le_ms)).
Qed.
Lemma iota_sorted i n : sorted leq (iota i n).
Proof. by elim: n i => // [[|n] //= IHn] i; rewrite IHn leqW. Qed.
Lemma iota_ltn_sorted i n : sorted ltn (iota i n).
Proof. by rewrite ltn_sorted_uniq_leq iota_sorted iota_uniq. Qed.
Section Stability_merge.
Variables (T : Type) (leT leT' : rel T).
Hypothesis (leT_total : total leT) (leT'_tr : transitive leT').
Let leT_lex := [rel x y | leT x y && (leT y x ==> leT' x y)].
Lemma merge_stable_path x s1 s2 :
all (fun y => all (leT' y) s2) s1 ->
path leT_lex x s1 -> path leT_lex x s2 -> path leT_lex x (merge leT s1 s2).
Proof.
elim: s1 s2 x => //= x s1 ih1; elim => //= y s2 ih2 h.
rewrite all_predI -andbA => /and4P [xy' xs2 ys1 s1s2].
case/andP => hx xs1 /andP [] hy ys2; case: ifP => xy /=; rewrite (hx, hy) /=.
- by apply: ih1; rewrite ?all_predI ?ys1 //= xy xy' implybT.
- by apply: ih2; have:= leT_total x y; rewrite ?xs2 //= xy => /= ->.
Qed.
Lemma merge_stable_sorted s1 s2 :
all (fun x => all (leT' x) s2) s1 ->
sorted leT_lex s1 -> sorted leT_lex s2 -> sorted leT_lex (merge leT s1 s2).
Proof.
case: s1 s2 => [|x s1] [|y s2] //=; rewrite all_predI -andbA.
case/and4P => [xy' xs2 ys1 s1s2] xs1 ys2; rewrite -/(merge _ (_ :: _)).
by case: ifP (leT_total x y) => /= xy yx; apply/merge_stable_path;
rewrite /= ?(all_predI, xs2, ys1, xy, yx, xy', implybT).
Qed.
End Stability_merge.
Section Stability_iota.
Variables (leN : rel nat) (leN_total : total leN) (leN_tr : transitive leN).
Let lt_lex := [rel n m | leN n m && (leN m n ==> (n < m))].
Local Arguments iota : simpl never.
Local Arguments size : simpl never.
Local Arguments cat : simpl never.
Let push_invariant := fix push_invariant (ss : seq (seq nat)) :=
if ss is s :: ss' then
sorted lt_lex s && perm_eq s (iota (size (flatten ss')) (size s)) &&
push_invariant ss'
else
true.
Let push_stable s1 ss :
push_invariant (s1 :: ss) -> push_invariant (merge_sort_push leN s1 ss).
Proof.
elim: ss s1 => [] // [] //= m s2 ss ihss s1; rewrite -2!andbA.
move=> /and5P [sorted_s1 perm_s1 sorted_s2 perm_s2 hss]; apply: ihss.
rewrite hss size_merge size_cat iotaD addnC -size_cat perm_merge perm_cat //.
rewrite merge_stable_sorted // (perm_all _ perm_s2); apply/allP => n.
rewrite mem_iota (perm_all _ perm_s1) => /andP [_ n_lt]; apply/allP => p.
by rewrite mem_iota size_cat addnC => /andP [] /(leq_trans n_lt).
Qed.
Let pop_stable s1 ss :
push_invariant (s1 :: ss) -> sorted lt_lex (merge_sort_pop leN s1 ss).
Proof.
elim: ss s1 => [s1 /andP [] /andP [] //|s2 ss ihss s1]; rewrite /= -2!andbA.
move=> /and5P [sorted_s1 perm_s1 sorted_s2 perm_s2 hss]; apply: ihss.
rewrite /= hss size_merge size_cat iotaD addnC -size_cat perm_merge perm_cat //.
rewrite merge_stable_sorted // (perm_all _ perm_s2); apply/allP => n.
rewrite mem_iota (perm_all _ perm_s1) => /andP [_ n_lt]; apply/allP => p.
by rewrite mem_iota size_cat addnC => /andP [] /(leq_trans n_lt).
Qed.
Lemma sort_iota_stable n : sorted lt_lex (sort leN (iota 0 n)).
Proof.
rewrite sortE -[0]/(size (@flatten nat [::])).
have: push_invariant [::] by [].
elim: n [::] => [|n ihn] ss hss; first exact: pop_stable.
have: push_invariant ([:: size (flatten ss)] :: ss) by rewrite /= perm_refl.
move/push_stable/ihn; congr (sorted _ (sort_rec1 _ _ (iota _ _))).
rewrite -[_.+1]/(size ([:: size (flatten ss)] ++ _)).
elim: (ss) [:: _] => [|[|? ?] ? ihss] //= ?.
by rewrite ihss !size_cat size_merge size_cat -addnA addnCA.
Qed.
End Stability_iota.
Lemma sort_stable T (leT leT' : rel T) :
total leT -> transitive leT' -> forall s : seq T, sorted leT' s ->
sorted [rel x y | leT x y && (leT y x ==> leT' x y)] (sort leT s).
Proof.
move=> leT_total leT'_tr s sorted_s; case Ds: s => // [x s1].
rewrite -{s1}Ds -(mkseq_nth x s) sort_map.
have leN_total: total (relpre (nth x s) leT) by move=> n m; apply: leT_total.
apply: (homo_sorted_in _ (allss _)) (sort_iota_stable leN_total _) => /= y z.
rewrite !mem_sort !mem_iota !leq0n add0n /= => ys zs /andP [->] /=.
by case: (leT _ _); first apply: sorted_ltn_nth.
Qed.
Lemma filter_sort T (leT : rel T) :
total leT -> transitive leT ->
forall p s, filter p (sort leT s) = sort leT (filter p s).
Proof.
move=> leT_total leT_tr p s; case Ds: s => // [x s1].
pose leN := relpre (nth x s) leT.
pose lt_lex := [rel n m | leN n m && (leN m n ==> (n < m))].
have lt_lex_tr: transitive lt_lex.
rewrite /lt_lex /leN => ? ? ? /= /andP [xy xy'] /andP [yz yz'].
rewrite (leT_tr _ _ _ xy yz); apply/implyP => zx; move: xy' yz'.
by rewrite (leT_tr _ _ _ yz zx) (leT_tr _ _ _ zx xy); apply: ltn_trans.
rewrite -{s1}Ds -(mkseq_nth x s) !(filter_map, sort_map); congr map.
apply/(@irr_sorted_eq _ lt_lex); rewrite /lt_lex /leN //=.
- by move=> ?; rewrite /= ltnn implybF andbN.
- exact/sorted_filter/sort_iota_stable.
- exact/sort_stable/sorted_filter/iota_ltn_sorted/ltn_trans/ltn_trans.
- by move=> ?; rewrite !(mem_filter, mem_sort).
Qed.
Section Stability_mask.
Variables (T : Type) (leT : rel T).
Variables (leT_total : total leT) (leT_tr : transitive leT).
Lemma mask_sort s m :
{m_s : bitseq | mask m_s (sort leT s) = sort leT (mask m s)}.
Proof.
case Ds: {-}s => [|x s1]; [by rewrite Ds; case: m; exists [::] | clear s1 Ds].
rewrite -(mkseq_nth x s) -map_mask !sort_map.
exists [seq i \in mask m (iota 0 (size s)) |
i <- sort (xrelpre (nth x s) leT) (iota 0 (size s))].
rewrite -map_mask -filter_mask [in RHS]mask_filter ?iota_uniq ?filter_sort //.
by move=> ? ? ?; exact: leT_tr.
Qed.
Lemma sorted_mask_sort s m :
sorted leT (mask m s) -> {m_s | mask m_s (sort leT s) = mask m s}.
Proof. by move/(sorted_sort leT_tr) <-; exact: mask_sort. Qed.
End Stability_mask.
Section Stability_subseq.
Variables (T : eqType) (leT : rel T).
Variables (leT_total : total leT) (leT_tr : transitive leT).
Lemma subseq_sort : {homo sort leT : t s / subseq t s}.
Proof.
move=> _ s /subseqP [m _ ->].
case: (mask_sort leT_total leT_tr s m) => m' <-; exact: mask_subseq.
Qed.
Lemma sorted_subseq_sort t s :
subseq t s -> sorted leT t -> subseq t (sort leT s).
Proof. by move=> subseq_ts /(sorted_sort leT_tr) <-; exact: subseq_sort. Qed.
Lemma mem2_sort s x y : leT x y -> mem2 s x y -> mem2 (sort leT s) x y.
Proof.
move=> lexy; rewrite !mem2E => /subseq_sort.
by case: eqP => // _; rewrite {1}/sort /= lexy /=.
Qed.
End Stability_subseq.
(* Function trajectories. *)
Notation fpath f := (path (coerced_frel f)).
Notation fcycle f := (cycle (coerced_frel f)).
Notation ufcycle f := (ucycle (coerced_frel f)).
Prenex Implicits path next prev cycle ucycle mem2.
Section Trajectory.
Variables (T : Type) (f : T -> T).
Fixpoint traject x n := if n is n'.+1 then x :: traject (f x) n' else [::].
Lemma trajectS x n : traject x n.+1 = x :: traject (f x) n.
Proof. by []. Qed.
Lemma trajectSr x n : traject x n.+1 = rcons (traject x n) (iter n f x).
Proof. by elim: n x => //= n IHn x; rewrite IHn -iterSr. Qed.
Lemma last_traject x n : last x (traject (f x) n) = iter n f x.
Proof. by case: n => // n; rewrite iterSr trajectSr last_rcons. Qed.
Lemma traject_iteri x n :
traject x n = iteri n (fun i => rcons^~ (iter i f x)) [::].
Proof. by elim: n => //= n <-; rewrite -trajectSr. Qed.
Lemma size_traject x n : size (traject x n) = n.
Proof. by elim: n x => //= n IHn x //=; rewrite IHn. Qed.
Lemma nth_traject i n : i < n -> forall x, nth x (traject x n) i = iter i f x.
Proof.
elim: n => // n IHn; rewrite ltnS => le_i_n x.
rewrite trajectSr nth_rcons size_traject.
by case: ltngtP le_i_n => [? _||->] //; apply: IHn.
Qed.
Lemma trajectD m n x :
traject x (m + n) = traject x m ++ traject (iter m f x) n.
Proof. by elim: m => //m IHm in x *; rewrite addSn !trajectS IHm -iterSr. Qed.
Lemma take_traject n k x : k <= n -> take k (traject x n) = traject x k.
Proof. by move=> /subnKC<-; rewrite trajectD take_size_cat ?size_traject. Qed.
End Trajectory.
Section EqTrajectory.
Variables (T : eqType) (f : T -> T).
Lemma eq_fpath f' : f =1 f' -> fpath f =2 fpath f'.
Proof. by move/eq_frel/eq_path. Qed.
Lemma eq_fcycle f' : f =1 f' -> fcycle f =1 fcycle f'.
Proof. by move/eq_frel/eq_cycle. Qed.
Lemma fpathE x p : fpath f x p -> p = traject f (f x) (size p).
Proof. by elim: p => //= y p IHp in x * => /andP[/eqP{y}<- /IHp<-]. Qed.
Lemma fpathP x p : reflect (exists n, p = traject f (f x) n) (fpath f x p).
Proof.
apply: (iffP idP) => [/fpathE->|[n->]]; first by exists (size p).
by elim: n => //= n IHn in x *; rewrite eqxx IHn.
Qed.
Lemma fpath_traject x n : fpath f x (traject f (f x) n).
Proof. by apply/(fpathP x); exists n. Qed.
Definition looping x n := iter n f x \in traject f x n.
Lemma loopingP x n :
reflect (forall m, iter m f x \in traject f x n) (looping x n).
Proof.
apply: (iffP idP) => loop_n; last exact: loop_n.
case: n => // n in loop_n *; elim=> [|m /= IHm]; first exact: mem_head.
move: (fpath_traject x n) loop_n; rewrite /looping !iterS -last_traject /=.
move: (iter m f x) IHm => y /splitPl[p1 p2 def_y].
rewrite cat_path last_cat def_y; case: p2 => // z p2 /and3P[_ /eqP-> _] _.
by rewrite inE mem_cat mem_head !orbT.
Qed.
Lemma trajectP x n y :
reflect (exists2 i, i < n & y = iter i f x) (y \in traject f x n).
Proof.
elim: n x => [|n IHn] x /=; first by right; case.
rewrite inE; have [-> | /= neq_xy] := eqP; first by left; exists 0.
apply: {IHn}(iffP (IHn _)) => [[i] | [[|i]]] // lt_i_n ->.
by exists i.+1; rewrite ?iterSr.
by exists i; rewrite ?iterSr.
Qed.
Lemma looping_uniq x n : uniq (traject f x n.+1) = ~~ looping x n.
Proof.
rewrite /looping; elim: n x => [|n IHn] x //.
rewrite [n.+1 in LHS]lock [iter]lock /= -!lock {}IHn -iterSr -negb_or inE.
congr (~~ _); apply: orb_id2r => /trajectP no_loop.
apply/idP/eqP => [/trajectP[m le_m_n def_x] | {1}<-]; last first.
by rewrite iterSr -last_traject mem_last.
have loop_m: looping x m.+1 by rewrite /looping iterSr -def_x mem_head.
have/trajectP[[|i] // le_i_m def_fn1x] := loopingP _ _ loop_m n.+1.
by case: no_loop; exists i; rewrite -?iterSr // -ltnS (leq_trans le_i_m).
Qed.
End EqTrajectory.
Arguments fpathP {T f x p}.
Arguments loopingP {T f x n}.
Arguments trajectP {T f x n y}.
Prenex Implicits traject.
Section Fcycle.
Variables (T : eqType) (f : T -> T) (p : seq T) (f_p : fcycle f p).
Lemma nextE (x : T) (p_x : x \in p) : next p x = f x.
Proof. exact/esym/eqP/(next_cycle f_p). Qed.
Lemma mem_fcycle : {homo f : x / x \in p}.
Proof. by move=> x xp; rewrite -nextE// mem_next. Qed.
Lemma inj_cycle : {in p &, injective f}.
Proof.
apply: can_in_inj (iter (size p).-1 f) _ => x /rot_to[i q rip].
have /fpathE qxE : fcycle f (x :: q) by rewrite -rip rot_cycle.
have -> : size p = size (rcons q x) by rewrite size_rcons -(size_rot i) rip.
by rewrite -iterSr -last_traject prednK -?qxE ?size_rcons// last_rcons.
Qed.
End Fcycle.
Section UniqCycle.
Variables (n0 : nat) (T : eqType) (e : rel T) (p : seq T).
Hypothesis Up : uniq p.
Lemma prev_next : cancel (next p) (prev p).
Proof.
move=> x; rewrite prev_nth mem_next next_nth; case p_x: (x \in p) => //.
case Dp: p Up p_x => // [y q]; rewrite [uniq _]/= -Dp => /andP[q'y Uq] p_x.
rewrite -[RHS](nth_index y p_x); congr (nth y _ _); set i := index x p.
have: i <= size q by rewrite -index_mem -/i Dp in p_x.
case: ltngtP => // [lt_i_q|->] _; first by rewrite index_uniq.
by apply/eqP; rewrite nth_default // eqn_leq index_size leqNgt index_mem.
Qed.
Lemma next_prev : cancel (prev p) (next p).
Proof.
move=> x; rewrite next_nth mem_prev prev_nth; case p_x: (x \in p) => //.
case def_p: p p_x => // [y q]; rewrite -def_p => p_x.
rewrite index_uniq //; last by rewrite def_p ltnS index_size.
case q_x: (x \in q); first exact: nth_index.
rewrite nth_default; last by rewrite leqNgt index_mem q_x.
by apply/eqP; rewrite def_p inE q_x orbF eq_sym in p_x.
Qed.
Lemma cycle_next : fcycle (next p) p.
Proof.
case def_p: p Up => [|x q] Uq //; rewrite -[in next _]def_p.
apply/(pathP x)=> i; rewrite size_rcons => le_i_q.
rewrite -cats1 -cat_cons nth_cat le_i_q /= next_nth {}def_p mem_nth //.
rewrite index_uniq // nth_cat /= ltn_neqAle andbC -ltnS le_i_q.
by case: (i =P _) => //= ->; rewrite subnn nth_default.
Qed.
Lemma cycle_prev : cycle (fun x y => x == prev p y) p.
Proof.
apply: etrans cycle_next; symmetry; case def_p: p => [|x q] //.
by apply: eq_path; rewrite -def_p; apply: (can2_eq prev_next next_prev).
Qed.
Lemma cycle_from_next : (forall x, x \in p -> e x (next p x)) -> cycle e p.
Proof.
case: p (next p) cycle_next => //= [x q] n; rewrite -(belast_rcons x q x).
move: {q}(rcons q x) => q n_q /allP.
by elim: q x n_q => //= _ q IHq x /andP[/eqP <- n_q] /andP[-> /IHq->].
Qed.
Lemma cycle_from_prev : (forall x, x \in p -> e (prev p x) x) -> cycle e p.
Proof.
move=> e_p; apply: cycle_from_next => x.
by rewrite -mem_next => /e_p; rewrite prev_next.
Qed.
Lemma next_rot : next (rot n0 p) =1 next p.
Proof.
move=> x; have n_p := cycle_next; rewrite -(rot_cycle n0) in n_p.
case p_x: (x \in p); last by rewrite !next_nth mem_rot p_x.
by rewrite (eqP (next_cycle n_p _)) ?mem_rot.
Qed.
Lemma prev_rot : prev (rot n0 p) =1 prev p.
Proof.
move=> x; have p_p := cycle_prev; rewrite -(rot_cycle n0) in p_p.
case p_x: (x \in p); last by rewrite !prev_nth mem_rot p_x.
by rewrite (eqP (prev_cycle p_p _)) ?mem_rot.
Qed.
End UniqCycle.
Section UniqRotrCycle.
Variables (n0 : nat) (T : eqType) (p : seq T).
Hypothesis Up : uniq p.
Lemma next_rotr : next (rotr n0 p) =1 next p. Proof. exact: next_rot. Qed.
Lemma prev_rotr : prev (rotr n0 p) =1 prev p. Proof. exact: prev_rot. Qed.
End UniqRotrCycle.
Section UniqCycleRev.
Variable T : eqType.
Implicit Type p : seq T.
Lemma prev_rev p : uniq p -> prev (rev p) =1 next p.
Proof.
move=> Up x; case p_x: (x \in p); last first.
by rewrite next_nth prev_nth mem_rev p_x.
case/rot_to: p_x (Up) => [i q def_p] Urp; rewrite -rev_uniq in Urp.
rewrite -(prev_rotr i Urp); do 2 rewrite -(prev_rotr 1) ?rotr_uniq //.
rewrite -rev_rot -(next_rot i Up) {i p Up Urp}def_p.
by case: q => // y q; rewrite !rev_cons !(=^~ rcons_cons, rotr1_rcons) /= eqxx.
Qed.
Lemma next_rev p : uniq p -> next (rev p) =1 prev p.
Proof. by move=> Up x; rewrite -[p in RHS]revK prev_rev // rev_uniq. Qed.
End UniqCycleRev.
Section MapPath.
Variables (T T' : Type) (h : T' -> T) (e : rel T) (e' : rel T').
Definition rel_base (b : pred T) :=
forall x' y', ~~ b (h x') -> e (h x') (h y') = e' x' y'.
Lemma map_path b x' p' (Bb : rel_base b) :
~~ has (preim h b) (belast x' p') ->
path e (h x') (map h p') = path e' x' p'.
Proof. by elim: p' x' => [|y' p' IHp'] x' //= /norP[/Bb-> /IHp'->]. Qed.
End MapPath.
Section MapEqPath.
Variables (T T' : eqType) (h : T' -> T) (e : rel T) (e' : rel T').
Hypothesis Ih : injective h.
Lemma mem2_map x' y' p' : mem2 (map h p') (h x') (h y') = mem2 p' x' y'.
Proof. by rewrite [LHS]/mem2 (index_map Ih) -map_drop mem_map. Qed.
Lemma next_map p : uniq p -> forall x, next (map h p) (h x) = h (next p x).
Proof.
move=> Up x; case p_x: (x \in p); last by rewrite !next_nth (mem_map Ih) p_x.
case/rot_to: p_x => i p' def_p.
rewrite -(next_rot i Up); rewrite -(map_inj_uniq Ih) in Up.
rewrite -(next_rot i Up) -map_rot {i p Up}def_p /=.
by case: p' => [|y p''] //=; rewrite !eqxx.
Qed.
Lemma prev_map p : uniq p -> forall x, prev (map h p) (h x) = h (prev p x).
Proof.
move=> Up x; rewrite -[x in LHS](next_prev Up) -(next_map Up).
by rewrite prev_next ?map_inj_uniq.
Qed.
End MapEqPath.
Definition fun_base (T T' : eqType) (h : T' -> T) f f' :=
rel_base h (frel f) (frel f').
Section CycleArc.
Variable T : eqType.
Implicit Type p : seq T.
Definition arc p x y := let px := rot (index x p) p in take (index y px) px.
Lemma arc_rot i p : uniq p -> {in p, arc (rot i p) =2 arc p}.
Proof.
move=> Up x p_x y; congr (fun q => take (index y q) q); move: Up p_x {y}.
rewrite -{1 2 5 6}(cat_take_drop i p) /rot cat_uniq => /and3P[_ Up12 _].
rewrite !drop_cat !take_cat !index_cat mem_cat orbC.
case p2x: (x \in drop i p) => /= => [_ | p1x].
rewrite index_mem p2x [x \in _](negbTE (hasPn Up12 _ p2x)) /= addKn.
by rewrite ltnNge leq_addr catA.
by rewrite p1x index_mem p1x addKn ltnNge leq_addr /= catA.
Qed.
Lemma left_arc x y p1 p2 (p := x :: p1 ++ y :: p2) :
uniq p -> arc p x y = x :: p1.
Proof.
rewrite /arc /p [index x _]/= eqxx rot0 -cat_cons cat_uniq index_cat.
move: (x :: p1) => xp1 /and3P[_ /norP[/= /negbTE-> _] _].
by rewrite eqxx addn0 take_size_cat.
Qed.
Lemma right_arc x y p1 p2 (p := x :: p1 ++ y :: p2) :
uniq p -> arc p y x = y :: p2.
Proof.
rewrite -[p]cat_cons -rot_size_cat rot_uniq => Up.
by rewrite arc_rot ?left_arc ?mem_head.
Qed.
Variant rot_to_arc_spec p x y :=
RotToArcSpec i p1 p2 of x :: p1 = arc p x y
& y :: p2 = arc p y x
& rot i p = x :: p1 ++ y :: p2 :
rot_to_arc_spec p x y.
Lemma rot_to_arc p x y :
uniq p -> x \in p -> y \in p -> x != y -> rot_to_arc_spec p x y.
Proof.
move=> Up p_x p_y ne_xy; case: (rot_to p_x) (p_y) (Up) => [i q def_p] q_y.
rewrite -(mem_rot i) def_p inE eq_sym (negbTE ne_xy) in q_y.
rewrite -(rot_uniq i) def_p.
case/splitPr: q / q_y def_p => q1 q2 def_p Uq12; exists i q1 q2 => //.
by rewrite -(arc_rot i Up p_x) def_p left_arc.
by rewrite -(arc_rot i Up p_y) def_p right_arc.
Qed.
End CycleArc.
Prenex Implicits arc.
Notation "@ 'eq_sorted'" :=
(deprecate eq_sorted sorted_eq) (at level 10, only parsing) : fun_scope.
Notation "@ 'eq_sorted_irr'" := (deprecate eq_sorted_irr irr_sorted_eq)
(at level 10, only parsing) : fun_scope.
Notation "@ 'sorted_lt_nth'" := (deprecate sorted_lt_nth sorted_ltn_nth)
(at level 10, only parsing) : fun_scope.
Notation "@ 'sorted_le_nth'" := (deprecate sorted_le_nth sorted_leq_nth)
(at level 10, only parsing) : fun_scope.
Notation "@ 'ltn_index'" := (deprecate ltn_index sorted_ltn_index)
(at level 10, only parsing) : fun_scope.
Notation "@ 'leq_index'" := (deprecate leq_index sorted_leq_index)
(at level 10, only parsing) : fun_scope.
Notation "@ 'subseq_order_path'" := (deprecate subseq_order_path subseq_path)
(at level 10, only parsing) : fun_scope.
Notation eq_sorted :=
(fun le_tr le_asym => @eq_sorted _ _ le_tr le_asym _ _) (only parsing).
Notation eq_sorted_irr :=
(fun le_tr le_irr => @eq_sorted_irr _ _ le_tr le_irr _ _) (only parsing).
Notation sorted_lt_nth :=
(fun leT_tr x0 s_sorted => @sorted_lt_nth _ _ leT_tr x0 _ s_sorted _ _)
(only parsing).
Notation sorted_le_nth :=
(fun leT_tr leT_refl x0 s_sorted =>
@sorted_le_nth _ _ leT_tr leT_refl x0 _ s_sorted _ _) (only parsing).
Notation ltn_index :=
(fun leT_tr s_sorted => @ltn_index _ _ leT_tr _ s_sorted _ _) (only parsing).
Notation leq_index :=
(fun leT_tr leT_refl s_sorted =>
@leq_index _ _ leT_tr leT_refl _ s_sorted _ _) (only parsing).
Notation subseq_order_path :=
(fun leT_tr => @subseq_order_path _ _ leT_tr _ _ _) (only parsing).
|