-
Notifications
You must be signed in to change notification settings - Fork 1
/
shanoi4.v
3843 lines (3770 loc) · 168 KB
/
shanoi4.v
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
From mathcomp Require Import all_ssreflect finmap.
From hanoi Require Import extra star gdist lhanoi3 ghanoi ghanoi4 shanoi.
Open Scope nat_scope.
Set Implicit Arguments.
Unset Strict Implicit.
(******************************************************************************)
(* *)
(* This file proves the formula that gives the distance between two perfect *)
(* configurations for the star puzzle. It follows the proof given by Thierry *)
(* Bousch in La tour de Stockmeyer *)
(* *)
(* *)
(******************************************************************************)
Section sHanoi4.
Local Notation "c1 `-->_s c2" := (smove c1 c2)
(format "c1 `-->_s c2", at level 60).
Local Notation "c1 `-->*_s c2" := (connect smove c1 c2)
(format "c1 `-->*_s c2", at level 60).
(* the peg at the center is 0 *)
Let p0 : peg 4 := ord0.
(* specialize version *)
Lemma pE4 (p1 p2 p3 : peg 4) p :
p1 != p2 -> p1 != p3 -> p2 != p3 ->
p1 != p0 -> p2 != p0 -> p3 != p0 ->
[\/ p = p0, p = p1, p = p2 | p = p3].
Proof.
rewrite /p0.
by case: (peg4E p1) => ->; rewrite ?eqxx //;
case: (peg4E p2) => ->; rewrite ?eqxx //;
case: (peg4E p3) => ->; rewrite ?eqxx //;
case: (peg4E p) => ->; rewrite ?eqxx // => *;
try ((by apply: Or41) || (by apply: Or42) ||
(by apply: Or43) || (by apply: Or44)).
Qed.
(* Lifting lhanoi3 to shanoi4 *)
Section lift.
Variable p1 p2 p3 : peg 4.
Hypothesis p1Dp0 : p1 != p0.
Hypothesis p2Dp0 : p2 != p0.
Hypothesis p3Dp0 : p3 != p0.
Hypothesis p1Dp2 : p1 != p2.
Hypothesis p1Dp3 : p1 != p3.
Hypothesis p2Dp3 : p2 != p3.
Variable n m : nat.
Definition clmap (c : configuration 3 n) : configuration 4 n :=
[ffun i =>
if c i == 0 :> nat then p1
else if c i == 1 :> nat then p0 else p2].
Definition clmerge (c : configuration 3 n) : configuration 4 (n + m) :=
cmerge (clmap c) `c[p3].
Lemma on_top_clmerge x c : on_top x c -> on_top (tlshift m x) (clmerge c).
Proof.
move=> /on_topP oH; apply/on_topP => d.
rewrite !ffunE tsplit_tlshift /=.
case: tsplitP => y; rewrite !ffunE => ->.
case: (_ == _) => [/eqP|]; first by rewrite (negPf p1Dp3).
case: (_ == _) => /eqP; first by rewrite eq_sym (negPf p3Dp0).
by rewrite (negPf p2Dp3).
case: eqP => [cxE0|cxD0].
case: eqP => [cyE0 _| _].
by rewrite leq_add2r oH //; apply: val_inj; rewrite /= cyE0.
by case: (_ == _) => /eqP; rewrite (negPf p1Dp0, negPf p1Dp2).
case: eqP => [cxE1|cxD1].
case: (_ == _) => [/eqP|]; first by rewrite eq_sym (negPf p1Dp0).
case: eqP => [cyE1 _|_ /eqP].
by rewrite leq_add2r oH //; apply: val_inj; rewrite /= cyE1.
by rewrite eq_sym (negPf p2Dp0).
have cxE2 : c x = 2 :> nat by case: (c x) cxD0 cxD1 => [] [|[|[]]].
case: eqP => [_ /eqP|cyD0].
by rewrite eq_sym (negPf p1Dp2).
case: eqP => [_ /eqP|cyD1 _].
by rewrite (negPf p2Dp0).
have cyE2 : c y = 2 :> nat by case: (c y) cyD0 cyD1 => [] [|[|[]]].
by rewrite leq_add2r oH //; apply: val_inj; rewrite /= cyE2.
Qed.
Lemma move_clmerge c1 c2 : lmove c1 c2 -> clmerge c1 `-->_s clmerge c2.
Proof.
move=> /moveP [d1 [H1d1 H2d1 H3d1 H4d1]].
apply/moveP; exists (tlshift m d1); split => //.
- move: H1d1; rewrite !ffunE tsplit_tlshift !ffunE.
rewrite /lrel /= /srel /ghanoi.srel /=.
case: (c1 d1) => [] [|[|[|]]] //=.
- case: (c2 d1) => [] [|[|[|]]] //=.
by rewrite p1Dp0 muln0.
- case: (c2 d1) => [] [|[|[|]]] //= _ _ _.
by rewrite eq_sym p1Dp0.
by rewrite eq_sym p2Dp0.
case: (c2 d1) => [] [|[|[|]]] //= _ _ _.
by rewrite p2Dp0 muln0.
- move=> d2; rewrite !ffunE; case: tsplitP => j; rewrite !ffunE //.
move=> d2E /eqP/val_eqP.
rewrite /= d2E eqn_add2r => H.
by rewrite (@H2d1 j).
- by apply: on_top_clmerge.
by apply: on_top_clmerge.
Qed.
Lemma path_clmerge c cs :
path (move (@lrel 3)) c cs ->
path smove (clmerge c) [seq clmerge c | c <- cs].
Proof.
elim: cs c => //= c1 cs IH c2 /andP[mH pH].
by rewrite move_clmerge // IH.
Qed.
Lemma gdist_clmerge c1 c2 :
`d[clmerge c1, clmerge c2]_smove <= `d[c1, c2]_(move (@lrel 3)).
Proof.
have /gpath_connect[pa1 pa1H] := move_lconnect3 c1 c2.
rewrite (gpath_dist pa1H) -(size_map clmerge).
apply: gdist_path_le; first by rewrite path_clmerge // (gpath_path pa1H).
by rewrite [LHS]last_map (gpath_last pa1H).
Qed.
End lift.
(* We first prove the lower bound *)
Lemma leq_shanoi4 p1 p2 n :
p1 != p0 -> p2 != p0 -> p1 != p2 ->
`d[`c[p1, n],`c[p2, n]]_smove <= (S_[1] n).*2.
Proof.
elim/ltn_ind : n p1 p2 => [] [|n] IH p1 p2 p1Dp0 p2Dp0 p1Dp2.
rewrite alphaS_small double0.
rewrite (_ : `c[p1] = `c[p2]) ?gdist0 //.
by apply/ffunP=> [] [].
rewrite S1E S1_bigmin -muln2 -bigminMr.
apply/bigmin_leqP => i iLn.
have iLn1 : i <= n.+1 by apply: leq_trans iLn _.
rewrite -{-5}(subnK iLn1).
rewrite eq_sym in p2Dp0.
have [p3 [[p3Dp2 p3Dp0 p3Dp1] _]] := peg4comp3 p1Dp0 p1Dp2 p2Dp0.
rewrite eq_sym in p2Dp0.
pose p1n := `c[p1, n.+1 - i].
pose p2n := `c[p2, n.+1 - i].
pose p3n := `c[p3, n.+1 - i].
pose p1i := `c[p1, i].
pose p2i := `c[p2, i].
pose p3i := `c[p3, i].
pose c1 := cmerge p1n p1i.
pose c2 := cmerge p1n p3i.
pose c3 := cmerge p2n p3i.
pose c4 := cmerge p2n p2i.
have <- : c1 = `c[p1].
apply/ffunP => j; rewrite /c1 !ffunE.
by case: tsplitP => k; rewrite !ffunE.
have <- : c4 = `c[p2].
apply/ffunP => j; rewrite /c1 !ffunE.
by case: tsplitP => k; rewrite !ffunE.
apply: leq_trans
(_ : _ <= `d[c1, c2]_smove + `d[c2, c3]_smove + `d[c3, c4]_smove) _.
apply: leq_trans (leq_add (gdist_triangular _ _ _ _) (leqnn _)).
apply: gdist_triangular.
rewrite -addnn !mulnDl [X in _ <= X]addnAC !muln2.
repeat apply: leq_add; first 2 last.
- apply: leq_trans (gdist_merger _ _ _) _; first by apply: sirr.
by apply: shanoi_connect.
by rewrite -S1E; apply: IH.
- apply: leq_trans (gdist_merger _ _ _) _; first by apply: sirr.
by apply: shanoi_connect.
by rewrite -S1E; apply: IH => //; rewrite eq_sym.
rewrite div2K; last first.
rewrite -subn1 oddB ?expn_gt0 // addbT.
by rewrite oddX orbT.
have -> : c2 = clmerge p1 p2 p3 _ `c[ord0].
apply/ffunP=> x; rewrite !ffunE.
by case: tsplitP => // j; rewrite !ffunE /=.
have -> : c3 = clmerge p1 p2 p3 _ `c[inord 2].
apply/ffunP=> x; rewrite !ffunE.
by case: tsplitP => // j; rewrite !ffunE /= inordK.
apply: leq_trans (gdist_clmerge _ _ _ _ _ _ _ _ _) _ => //;
try by rewrite eq_sym.
rewrite gdist_lhanoi3p /lrel /= inordK //=.
case: eqP=> [/val_eqP/=|]; first by rewrite inordK.
by rewrite muln1.
Qed.
(* *)
(* Distance for a list *)
Fixpoint distanceL n (l : seq (configuration 4 n)) : nat :=
if l is a :: l1 then
if l1 is b :: _ then `d[a, b]_smove + distanceL l1
else 0
else 0.
Notation " D[ l ] " := (distanceL l)
(format " D[ l ]").
(* Alternating peg : depending on the parity of n it is p1 or p2 *)
Definition apeg p1 p2 n : peg 4 := if odd n then p2 else p1.
Notation " a[ p1 , p2 ] " := (apeg p1 p2)
(format " a[ p1 , p2 ]").
Lemma apeg32E a p1 p2 : a[p1, p2] (3 * a).-2 = a[p1, p2] a.
Proof.
case: a => // a.
rewrite /apeg -subn2 oddB; last by rewrite mulnS.
by rewrite oddM /=; case: odd.
Qed.
Lemma apeg0 p1 p2 : a[p1, p2] 0 = p1.
Proof. by []. Qed.
Lemma apegS p1 p2 n : a[p1, p2] n.+1 = a[p2, p1] n.
Proof. by rewrite /apeg /=; case: odd. Qed.
Lemma apegO p1 p2 n : a[p1, p2] n = a[p2, p1] (~~ (odd n)).
Proof. by rewrite /apeg /=; case: odd. Qed.
Lemma apeg_double p1 p2 n : a[p1, p2] (n.*2) = p1.
Proof. by rewrite apegO odd_double. Qed.
Lemma apeg_neq p1 p2 p3 n : p1 != p3 -> p2 != p3 -> a[p1, p2] n != p3.
Proof. by rewrite /apeg /=; case: odd; rewrite // eq_sym. Qed.
Lemma apeg_eqC p1 p2 n : (a[p1, p2] n == a[p2, p1] n) = (p1 == p2).
Proof. by rewrite /apeg /=; case: odd; rewrite // eq_sym. Qed.
Lemma apegD p1 p2 m n : a[p1, p2] (m + n) = a[ a[p1, p2] n, a[p2, p1] n] m.
Proof. by rewrite /apeg oddD; do 2 case: odd. Qed.
Lemma apegMr p1 p2 m n : odd m -> a[p1, p2] (m * n) = a[p1, p2] n.
Proof. by rewrite /apeg; rewrite oddM => ->. Qed.
Lemma codom_apeg (A : finType) (f : {ffun A -> _}) p1 p2 n :
(codom f \subset [:: a[p1, p2] n; a[p2, p1] n]) =
(codom f \subset [:: p1; p2]).
Proof. by rewrite /apeg; case: odd; rewrite // codom_subC. Qed.
(* This is lemma 2.1 *)
Lemma sgdist_pair n (p2 : peg 4) (u v : configuration 4 n.+1)
(p1 := u ldisk) (p3 := v ldisk):
{st : (configuration 4 n * configuration 4 n) |
p1 != p2 -> p1 != p3 -> p2 != p3 ->
p1 != p0 -> p2 != p0 -> p3 != p0 ->
[/\ codom st.1 \subset [:: p2; p3], codom st.2 \subset [:: p1; p2] &
`d[u, v]_smove = D[[:: ↓[u]; st.1; st.2; ↓[v]]].+2]}.
Proof.
case: eqP => [_|/eqP p1Dp2]; first by exists (↓[u], ↓[v]).
case: eqP => [_|/eqP p1Dp3]; first by exists (↓[u], ↓[v]).
case: eqP => [_|/eqP p2Dp3]; first by exists (↓[u], ↓[v]).
case: eqP => [_|/eqP p1Dp0]; first by exists (↓[u], ↓[v]).
case: eqP => [_|/eqP p2Dp0]; first by exists (↓[u], ↓[v]).
case: eqP => [_|/eqP p3Dp0]; first by exists (↓[u], ↓[v]).
have pE p : [\/ p = p0, p = p1, p = p2 | p = p3] by apply: pE4.
have [cs /= csH] := gpath_connect (shanoi_connect u v).
have vIcs : v \in cs.
have := mem_last u cs; rewrite (gpath_last csH).
rewrite inE; case: eqP => // vEu.
by case/eqP : p1Dp3; rewrite /p3 vEu.
case: (@split_first _ cs (fun c => c ldisk != p1)) => [|[[sp cs1] cs2]].
apply/negP=> /allP /(_ _ vIcs).
rewrite /= -topredE /= negbK.
by rewrite eq_sym (negPf p1Dp3).
case=> /allP spH spLE csE.
pose s := last u cs1.
have sMsp : s `-->_s sp.
by have := gpath_path csH; rewrite csE cat_path => /and3P[].
have sLp1 : s ldisk = p1.
move: (spH s); rewrite /s; case: (cs1) => //= c cs3 /(_ (mem_last _ _)).
by rewrite -topredE negbK => /eqP.
have spLp0 : sp ldisk = p0.
apply/eqP.
have /(_ ldisk) := move_diskr sMsp.
rewrite eq_sym sLp1 => /(_ spLE) /andP[].
case: (_ =P p0) => // /val_eqP /=.
have /eqP/val_eqP/= := p1Dp0.
by case: (p1 : nat) => // k; case: (sp _ : nat).
have sCd : codom (↓[s]) \subset [:: p2; p3].
apply/subsetP=> i; rewrite !inE => /codomP[j] ->.
case: (pE (↓[s] j)) => [||->|->]; rewrite ?eqxx ?orbT //.
rewrite ffunE trshift_lift /=.
move/eqP; rewrite -spLp0 -[_ == _]negbK; case/negP.
apply: move_on_toplDr sMsp _ _; first by rewrite sLp1 spLp0.
by rewrite /= /bump [n <= j]leqNgt ltn_ord add0n ltnW.
rewrite ffunE trshift_lift /=.
move/eqP; rewrite -sLp1 -[_ == _]negbK; case/negP.
apply: move_on_toplDl sMsp _ _; first by rewrite sLp1 spLp0.
by rewrite /= /bump [n <= j]leqNgt ltn_ord add0n.
have vIcs2 : v \in cs2.
move: vIcs; rewrite csE !(inE, mem_cat) => /or3P[|/eqP vEs|] //.
by move=> /spH; rewrite /= -topredE negbK eq_sym (negPf p1Dp3).
by case/eqP : p3Dp0; rewrite /p3 vEs.
case: (@split_last _ (sp :: cs2) (fun c => c ldisk != p3)) =>
[|/= [[t cs3] cs4]/=].
apply/negP=> /allP /(_ _ (mem_head _ _)).
by rewrite /= -topredE /= negbK spLp0 eq_sym (negPf p3Dp0).
case=> // tLE tH scs2E.
have vIcs4 : v \in cs4.
have := gpath_last csH; rewrite csE scs2E !last_cat /=.
case: (cs4) => /= [tEv|c1 cs5 <-]; last by apply: mem_last.
by case/eqP: tLE; rewrite tEv.
case: cs4 tH scs2E vIcs4 => // tp cs4 /allP tH scs2E vItpcs4.
have tMtp : t `-->_s tp.
by have := gpath_path csH; rewrite csE scs2E !cat_path /= => /and5P[].
have tpLp3 : tp ldisk = p3.
by move: (tH _ (mem_head _ _)); rewrite /= -topredE negbK => /eqP.
have tLp0 : t ldisk = p0.
apply/eqP.
have /(_ ldisk) := move_diskr tMtp.
rewrite tpLp3 => /(_ tLE) /andP[].
case: (_ =P p0) => // /val_eqP /=.
have /eqP/val_eqP/= := p3Dp0.
by case: (p3 : nat) => // k; case: (t _ : nat).
have tCd : codom (↓[t]) \subset [:: p1; p2].
apply/subsetP=> i; rewrite !inE => /codomP[j] ->.
case: (pE (↓[t] j)) => [|->|->|]; rewrite ?eqxx ?orbT //.
rewrite ffunE trshift_lift /=.
move/eqP; rewrite -tLp0 -[_ == _]negbK; case/negP.
apply: move_on_toplDl tMtp _ _; first by rewrite tpLp3 tLp0 eq_sym.
by rewrite /= /bump [n <= j]leqNgt ltn_ord add0n.
rewrite ffunE trshift_lift /=.
move/eqP; rewrite -tpLp3 -[_ == _]negbK; case/negP.
apply: move_on_toplDr tMtp _ _; first by rewrite tpLp3 tLp0 eq_sym.
by rewrite /= /bump [n <= j]leqNgt ltn_ord add0n ltnW.
exists (↓[s], ↓[t]); split => //=.
rewrite addn0.
move: csH; rewrite csE => csH.
rewrite (gdist_cat csH) -/s.
move: csH => /gpath_catr; rewrite -/s => csH.
rewrite gdist_cunlift_eq //; try apply: sirr; last by apply: shanoi_connect.
rewrite -!addnS.
congr (_ + _).
have ->: ↓[s] = ↓[sp].
have sLDspL : s ldisk != sp ldisk by rewrite sLp1 spLp0.
apply/ffunP => i; rewrite !ffunE.
apply: move_disk1 sMsp sLDspL _.
by apply/negP => /eqP/val_eqP /=; rewrite eqn_leq leqNgt ltn_ord.
rewrite (gdist_cons csH) addnS; congr (_).+1.
move: csH => /gpath_consr csH.
have ctEctp : ↓[t] = ↓[tp].
have tLDtpL : t ldisk != tp ldisk by rewrite tLp0 tpLp3 eq_sym.
apply/ffunP => i; rewrite !ffunE.
apply: move_disk1 tMtp tLDtpL _.
by apply/negP => /eqP/val_eqP /=; rewrite eqn_leq leqNgt ltn_ord.
case: cs3 scs2E => [[spEt cs2E]|c3 cs3 /= [spE cs2E]].
move: csH; rewrite spEt cs2E => csH.
rewrite gdist0 add0n (gdist_cons csH) ctEctp.
move: csH => /gpath_consr csH.
rewrite gdist_cunlift_eq //; try by apply: sirr.
by apply: shanoi_connect.
move: csH; rewrite cs2E -cat_rcons => csH.
rewrite (gdist_cat csH) last_rcons.
rewrite gdist_cunlift_eq //; try apply: sirr; last 2 first.
- by apply: shanoi_connect.
- by rewrite tLp0.
congr (_ + _).
move: csH => /gpath_catr; rewrite last_rcons => csH.
rewrite (gdist_cons csH); congr (_).+1.
move: csH => /gpath_consr csH.
rewrite ctEctp gdist_cunlift_eq //; try apply: sirr.
by apply: shanoi_connect.
Qed.
Definition sgdist1 n (p2 : peg 4) (u v : configuration 4 n.+1) :=
let: (exist (x, _) _) := sgdist_pair p2 u v in x.
Definition sgdist2 n (p2 : peg 4) (u v : configuration 4 n.+1) :=
let: (exist (_, x) _) := sgdist_pair p2 u v in x.
Lemma sgdistE n (p2 : peg 4) (u v : configuration 4 n.+1)
(p1 := u ldisk) (p3 := v ldisk):
p1 != p2 -> p1 != p3 -> p2 != p3 ->
p1 != p0 -> p2 != p0 -> p3 != p0 ->
[/\ codom (sgdist1 p2 u v) \subset [:: p2; p3],
codom (sgdist2 p2 u v) \subset [:: p1; p2] &
`d[u, v]_smove = D[[:: ↓[u]; sgdist1 p2 u v; sgdist2 p2 u v; ↓[v]]].+2].
Proof.
move=> p1Dp2 p1Dp3 p2Dp3 p1Dp0 p2Dp0 p3Dp0.
by rewrite /sgdist1 /sgdist2; case: sgdist_pair => [] [x y] [].
Qed.
(* The beta function *)
Definition beta n l k :=
if ((1 < l) && (k == n.-1)) then α_[l] k else (α_[1] k).*2.
Local Notation "β_[ n , l ]" := (beta n l) (format "β_[ n , l ]" ).
Lemma leq_beta n l k : α_[l] k <= β_[n, l] k.
Proof.
rewrite /beta; case: (_ < _) => /=; last first.
by apply: bound_alphaL.
by case: (_ == _) => //=; apply: bound_alphaL.
Qed.
Lemma geq_beta n l k : β_[n, l] k <= (α_[1] k).*2.
Proof.
rewrite /beta; case: (_ < _) => //=.
by case: (_ == _) => //=; apply: bound_alphaL.
Qed.
Lemma beta1E n k : β_[n, 1] k = (α_[1] k).*2.
Proof. by rewrite /beta ltnn. Qed.
Definition sp n (a : configuration 4 n) l p :=
\sum_(k < n) (a k != p) * β_[n, l] k.
Lemma sum_beta_S n l (a : configuration 4 n.+1) p : 1 < l ->
sp a l p =
\sum_(k < n) ((↓[a] k != p) * (α_[1] k).*2) + (a ord_max != p) * α_[l] n.
Proof.
move=> l_gt1; rewrite /sp.
rewrite big_ord_recr /= /beta l_gt1 eqxx /=; congr (_ + _).
apply: eq_bigr => i _; rewrite !ffunE; congr ((a _ != _) * _).
by apply: val_inj.
by rewrite eqn_leq [_ <= i]leqNgt ltn_ord andbF.
Qed.
Lemma leq_sum_beta n l a :
\sum_(k < n) a k * β_[n, l] k <= \sum_(k < n) a k * (α_[1] k).*2.
Proof.
by apply: leq_sum => i _; rewrite leq_mul2l geq_beta orbT.
Qed.
Lemma sum_alpha_diffE n (f : configuration 4 n.+1) (p1 p2 : peg 4) v1 v2 :
1 < v1 -> 1 < v2 -> p1 != p2 -> codom f \subset [:: p1; p2] ->
sp f v1 p1 + sp f v2 p2 <= (S_[1] n).*2 + α_[maxn v1 v2] n.
Proof.
move=> v1_gt1 v2_gt1 p1Dp2 cH.
rewrite !sum_beta_S //.
rewrite -addnA -addnCA addnC !addnA -addnA leq_add //.
rewrite leq_eqVlt; apply/orP; left; apply/eqP.
rewrite -addnn !dsum_alphaLE -!big_split /=; apply: eq_bigr => i _.
rewrite !ffunE; set v := trshift _ _.
have /subsetP := cH => /(_ (f v) (codom_f _ _)).
rewrite !inE addnn.
case: eqP => /= x1; case: eqP => /= x2; rewrite ?(mul1n, add0n, addn0) //.
by move/eqP: x2; rewrite x1 (negPf p1Dp2).
have /subsetP/(_ (f ord_max) (codom_f _ _)) := cH.
rewrite !inE.
case: eqP => /= x1; case: eqP => /= x2 // _; rewrite ?(mul1n, add0n, addn0) //;
apply: (increasingE (increasing_alphaL_l _)).
by apply: leq_maxr.
by apply: leq_maxl.
Qed.
(* This corresponds to 4.1 *)
Section Case1.
Definition sd n l (u : {ffun 'I_l.+1 -> configuration 4 n}) :=
\sum_(i < l) `d[u (inord i), u (inord i.+1)]_smove.
Variable n : nat.
Hypothesis IH:
forall (l : nat) (p1 p2 p3 : (Equality.clone _ 'I_4))
(u : {ffun 'I_l.+1 -> configuration 4 n.+1}),
p1 != p2 ->
p1 != p3 ->
p2 != p3 ->
p1 != p0 ->
p2 != p0 ->
p3 != p0 ->
(forall k : 'I_l.+1,
0 < k < l -> codom (u k) \subset [:: p2; a[p1, p3] k]) ->
(S_[l] n.+1).*2 <= sd u +
sp (u ord0) l (a[p1, p3] 0) +
sp (u ord_max) l (a[p1, p3] l).
Variable l : nat.
Variables p1 p2 p3: peg 4.
Variable u : {ffun 'I_l.+2 -> configuration 4 n.+2}.
Hypothesis p1Dp2 : p1 != p2.
Hypothesis p1Dp3 : p1 != p3.
Hypothesis p2Dp3 : p2 != p3.
Hypothesis p1Dp0 : p1 != p0.
Hypothesis p2Dp0 : p2 != p0.
Hypothesis p3Dp0 : p3 != p0.
Hypothesis cH : forall k : 'I_l.+2,
0 < k < l.+1 -> codom (u k) \subset [:: p2; a[p1, p3] k].
Let u':= ([ffun i => ↓[u i]] : {ffun 'I_l.+2 -> configuration 4 n.+1})
: {ffun 'I_l.+2 -> configuration 4 n.+1}.
Lemma Hcodom : forall k : 'I_l.+2,
0 < k < l.+1 -> codom (u' k) \subset [:: p2; a[p1, p3] k].
Proof. by move=> k kH; rewrite ffunE; apply/codom_liftr/cH. Qed.
Lemma apeg13D2 a : a[p1, p3] a != p2.
Proof. by rewrite /apeg; case: odd; rewrite // eq_sym. Qed.
Lemma apeg13D0 a : a[p1, p3] a != p0.
Proof. by rewrite /apeg; case: odd; rewrite // eq_sym. Qed.
Hypothesis KH1 : u ord0 ord_max = p1.
Hypothesis KH2 : u ord_max ord_max != a[p3, p1] l.
Hypothesis l_gt0 : l != 0.
Lemma case1 :
(S_[l.+1] n.+2).*2 <=
sd u + sp (u ord0) l.+1 p1 +
sp (u ord_max) l.+1 (a[p3, p1] l).
Proof.
have apeg13D0 := apeg13D0; have apeg13D2 := apeg13D2.
have il1E : inord l.+1 = ord_max :> 'I_l.+2
by apply/val_eqP; rewrite /= inordK.
have iE : exists i, u (inord i) ord_max != a[p1, p3] i.
by exists l.+1; rewrite apegS il1E.
case: (@ex_minnP _ iE) => a aH aMin.
have aMin1 i : i < a -> u (inord i) ord_max = a[p1, p3] i.
by case: (u (inord i) ord_max =P a[p1, p3] i) => // /eqP /aMin; case: leqP.
have aLld1 : a <= l.+1 by apply: aMin; rewrite apegS il1E.
have a_gt0 : 0 < a by case: (a) aH; rewrite // apeg0 -KH1 inord_eq0 //= eqxx.
pose ai : 'I_l.+2 := inord a.
have aiE : ai = a :> nat by rewrite inordK.
pose b := l.+1 - a.
pose bi : 'I_l.+2 := inord b.
have biE : bi = b :> nat by rewrite inordK // /b ltn_subLR // leq_addl.
have l_ggt0 : 0 < l by case: l l_gt0.
have [/andP[a_gt1 aLlm1]|] := boolP (2 <= a <= l.-1).
have aLl : a <= l by rewrite (leq_trans aLlm1) // -subn1 leq_subr.
have b_gt1 : 1 < b by rewrite leq_subRL // addn2 ltnS -[l]prednK.
have uaiLEp2 : u ai ldisk = p2.
have /cH/subsetP/(_ (u ai ldisk) (codom_f _ _)) : 0 < ai < l.+1.
by rewrite aiE (leq_trans _ a_gt1) //= ltnS (leq_trans aLl1)
// ssrnat.leq_pred.
by rewrite !inE aiE (negPf aH) orbF => /eqP.
pose si i : configuration 4 n.+1 :=
sgdist1 p2 (u (inord i)) (u (inord i.+1)).
pose ti i : configuration 4 n.+1 :=
sgdist2 p2 (u (inord i)) (u (inord i.+1)).
have sitiH i : i < a.-1 ->
[/\ codom (si i) \subset [:: p2; u (inord i.+1) ldisk ],
codom (ti i) \subset [:: u (inord i) ldisk; p2] &
`d[u (inord i), u (inord i.+1)]_smove =
D[[:: ↓[u (inord i)]; si i; ti i; ↓[u (inord i.+1)]]].+2].
move=> iLa.
have iLa1 : i < a by rewrite (leq_trans iLa) // ssrnat.leq_pred.
apply: sgdistE => //.
- by rewrite aMin1.
- rewrite !aMin1 //; last by rewrite -[a]prednK.
by rewrite apegS apeg_eqC.
- by rewrite aMin1 1?eq_sym // -[a]prednK.
- by rewrite aMin1.
by rewrite aMin1 // -[a]prednK.
pose sam1 : configuration 4 n.+1 :=
sgdist1 (a[p1, p3] a) (u (inord a.-1)) (u (inord a)).
pose tam1 : configuration 4 n.+1 :=
sgdist2 (a[p1, p3] a) (u (inord a.-1)) (u (inord a)).
have [sam1C tam1C duam1ua1E] :
[/\ codom sam1 \subset [:: a[p1, p3] a; u (inord a) ldisk ],
codom tam1 \subset [:: u (inord a.-1) ldisk; a[p1, p3] a] &
`d[u (inord a.-1), u (inord a)]_smove =
D[[:: ↓[u (inord a.-1)]; sam1; tam1; ↓[u (inord a)]]].+2].
apply: sgdistE => //.
- by rewrite aMin1 ?prednK // -{2}[a]prednK //= apegS apeg_eqC.
- by rewrite uaiLEp2 aMin1 ?prednK.
- by rewrite uaiLEp2.
- by rewrite aMin1 ?prednK.
by rewrite uaiLEp2.
have {}tam1C : codom tam1 \subset [:: p1; p3].
move: tam1C; rewrite aMin1; last by rewrite -{2}[a]prednK.
rewrite -subn1 apegO oddB //= [a[_, _]a]apegO; case: odd => //=.
by rewrite codom_subC.
pose u1 :=
[ffun i =>
if ((i : 'I_(3 * a).-2.+1) == 3 * a.-1 :>nat) then ↓[u (inord a.-1)]
else if (i == (3 * a.-1).+1 :>nat) then sam1
else if (i %% 3) == 0 then ↓[u (inord (i %/ 3))]
else if (i %% 3) == 1 then si (i %/ 3)
else ti (i %/ 3)].
have u10E : u1 ord0 = ↓[u ord0].
rewrite ffunE /= ifN ?inord_eq0 //.
by rewrite neq_ltn muln_gt0 /= -ltnS prednK //= a_gt1.
have uiME : u1 ord_max = sam1.
rewrite ffunE /= eqn_leq leqNgt -{2}[a]prednK // mulnS.
rewrite addSn add2n ltnS leqnn /=.
by rewrite -{1}[a]prednK // mulnS eqxx.
pose u2 := [ffun i : 'I_3 =>
if i == 0 :> nat then sam1 else
if i == 1 :> nat then tam1 else ↓[u ai]].
pose u3 := [ffun i => ↓[u (inord ((i : 'I_b.+1) + a))]].
have P1 : a.*2 + sd u1 + sd u2 + sd u3 <= sd u.
have G b1 : b1 = a ->
\sum_(i < (3 * b1).-2) `d[u1 (inord i), u1 (inord i.+1)]_smove =
\sum_(i < (3 * a.-1)) `d[u1 (inord i), u1 (inord i.+1)]_smove +
`d[↓[u (inord a.-1)], sam1]_smove.
move=> b1Ea.
have ta2E : (3 * b1).-2 = (3 * a.-1).+1.
by rewrite b1Ea -{1}[a]prednK // mulnS.
rewrite ta2E big_ord_recr /=; congr (_ + `d[_,_]_smove).
by rewrite ffunE ifT// inordK // -{2}b1Ea ?ta2E.
rewrite ffunE inordK; last by rewrite -{2}b1Ea ?ta2E.
by rewrite eqn_leq ltnn /= eqxx.
rewrite {}[sd u1]G //.
have -> := @sum3E _ (fun i => `d[u1 (inord i), u1 (inord i.+1)]_smove).
have -> : a.*2 = 2 + \sum_(i < a.-1) 2.
rewrite sum_nat_const /= cardT size_enum_ord.
by rewrite muln2 -(doubleD 1) add1n prednK.
rewrite !addnA -[2 + _ + _]addnA -big_split /=.
rewrite [2 + _]addnC -!addnA 2![X in _ + X <= _]addnA addnA.
have -> : sd u =
\sum_(i < a.-1) (`d[u (inord i), u (inord i.+1)]_smove) +
`d[u (inord a.-1), u ai]_smove +
\sum_(i < b) `d[u (inord (a +i)), u (inord (a + i.+1))]_smove.
have F := big_mkord xpredT
(fun i => `d[u (inord i), u (inord i.+1)]_smove).
rewrite -{}[sd u]F.
rewrite (big_cat_nat _ _ _ (_ : _ <= (a.-1).+1)) //=; last first.
by rewrite prednK.
rewrite big_mkord big_ord_recr /= prednK //.
rewrite -{9}[a]add0n big_addn -/b big_mkord.
congr (_ + _ + _).
by apply: eq_bigr => i; rewrite addnC addnS.
apply: leq_add; first apply: leq_add.
- rewrite leq_eqVlt; apply/orP; left; apply/eqP.
apply: eq_bigr => i _.
have -> : u1 (inord (3 * i)) = ↓[u (inord i)].
rewrite ffunE inordK; last first.
rewrite ltnS (leq_trans (_ : _ <= 3 * (a.-1))) //.
by rewrite leq_mul2l /= ltnW.
by rewrite -{2}[a]prednK // mulnS addSn add2n /= ltnW.
rewrite eqn_mul2l /= eqn_leq [_ <= i]leqNgt ltn_ord andbF.
rewrite eqn_leq ltn_mul2l /= ltnNge [i <= _]ltnW // andbF.
by rewrite mod3E eqxx mulKn.
have -> : u1 (inord (3 * i).+1) = si i.
rewrite ffunE inordK; last first.
rewrite ltnS (leq_trans (_ : _ <= 3 * (a.-1))) //.
by rewrite ltn_mul2l /=.
by rewrite -{2}[a]prednK // mulnS addSn add2n /= ltnW.
rewrite eqn_leq [_ <= _.+1]leqNgt.
rewrite (leq_trans (_ : (3 * i).+2 <= (3 * i.+1))) ?andbF //;
last 2 first.
- by rewrite mulnS addSn add2n.
- by rewrite leq_mul2l /=.
rewrite eqn_leq !ltnS [_ <= 3 * i]leq_mul2l.
by rewrite [_ <= i]leqNgt ltn_ord andbF mod3E /= div3E.
have -> : u1 (inord (3 * i).+2) = ti i.
rewrite ffunE inordK; last first.
rewrite ltnS (leq_trans (_ : _ <= 3 * (a.-1))) //.
rewrite (leq_trans (_ : _ <= 3 * (i.+1))) //.
by rewrite mulnS addSn add2n.
by rewrite leq_mul2l /=.
by rewrite -{2}[a]prednK // mulnS addSn add2n /= ltnW.
rewrite eqn_leq [_ <= _.+1]leqNgt.
rewrite -[(3 * i).+3](mulnDr 3 1 i) leq_mul2l ltn_ord andbF.
rewrite eqn_leq !ltnS [_ <= _.+1]leqNgt.
rewrite (leq_trans (_ : (3 * i).+1 < 3 * (i.+1))) ?andbF //;
last 2 first.
- by rewrite mulnS addSn add2n.
- by rewrite leq_mul2l /=.
by rewrite mod3E /= div3E.
have -> : u1 (inord (3 * i).+3) = ↓[u (inord i.+1)].
have -> : (3 * i).+3 = 3 * (i.+1) by rewrite mulnS addSn add2n.
rewrite ffunE inordK; last first.
rewrite ltnS (leq_trans (_ : _ <= 3 * (a.-1))) //.
by rewrite leq_mul2l /=.
by rewrite -{2}[a]prednK // mulnS addSn add2n /= ltnW.
rewrite eqn_mul2l /=; case: eqP => [->//|_].
rewrite eqn_leq [_ <= 3 * i.+1]leqNgt ltnS leq_mul2l /= ltn_ord andbF.
by rewrite mod3E /= div3E.
case: (sitiH i) => // _ _ ->.
by rewrite add2n /= addnA addn0.
- rewrite leq_eqVlt; apply/orP; left; apply/eqP.
rewrite /sd !big_ord_recr big_ord0 //= add0n !addnA.
have -> : u2 (inord 0) = sam1 by rewrite ffunE /= inordK.
have -> : u2 (inord 1) = tam1 by rewrite ffunE /= inordK.
have -> : u2 (inord 2) = ↓[u ai] by rewrite ffunE /= inordK.
by move: duam1ua1E; rewrite /= addn0 add2n !addnA !addSn.
apply: leq_sum => i _.
rewrite ffunE inordK; last by rewrite ltnS ltnW.
rewrite ffunE inordK; last by rewrite ltnS.
rewrite addSn [i + _]addnC addnS.
by apply/gdist_cunlift/shanoi_connect.
set x1P1 := sd _ in P1; set x2P1 := sd _ in P1.
set x3P1 := sd _ in P1; set xP1 := sd _ in P1.
rewrite -/xP1.
have cH2 (k : 'I_(3 * a).-2.+1) :
0 < k < (3 * a).-2 -> codom (u1 k) \subset [:: p2; a[p1, p3] k].
move=> /andP[k_gt0 k_lt].
have kL3a1 : k <= (3 * a.-1).
by rewrite -ltnS (leq_trans k_lt) // -{1}[a]prednK // mulnS.
have kLa1 : k %/ 3 <= a.-1.
by rewrite -(mulKn a.-1 (isT : 0 < 3)) leq_div2r.
rewrite ffunE; case: eqP => [kH|/eqP kH].
rewrite kH apegMr //.
have := @Hcodom(inord a.-1); rewrite inordK //.
rewrite ffunE; apply.
by rewrite -ltnS prednK // a_gt1.
by rewrite prednK // (leq_trans aLld1).
have k3La2 : k %/ 3 <= a.-2.
rewrite -ltnS [a.-2.+1]prednK //; last first.
by rewrite -subn1 ltn_subRL.
rewrite ltn_divLR // [_ * 3]mulnC.
by move: kL3a1 kH; case: ltngtP.
rewrite eqn_leq [_ <= k]leqNgt (leq_trans k_lt) ?andbF //; last first.
by rewrite -{1}[a]prednK // mulnS addSn add2n.
have k3La : k %/ 3 <= a.
by rewrite (leq_trans kLa1) // -subn1 leq_subr.
case: eqP => k3mH.
apply: codom_liftr.
rewrite {2}(divn_eq k 3) k3mH addn0 [in a[_,_] _]mulnC apegMr //.
have := @cH (inord (k %/3)); rewrite inordK; last first.
by rewrite (leq_ltn_trans k3La).
apply.
rewrite (leq_ltn_trans k3La) ?ltnS //.
move: k_gt0; rewrite andbT {1}(divn_eq k 3) k3mH addn0 muln_gt0.
by case/andP.
case: eqP => k3mH1.
case: (sitiH ((k %/ 3))) => /=.
by rewrite -[a.-1]prednK // -ltnS prednK.
set pp := u _ _; (suff <- : pp = a[p1, p3] k by []); rewrite {}/pp.
set i := inord _; have <- : a[p1, p3] i = a[p1, p3] k.
rewrite (divn_eq k 3) k3mH1 addn1 apegS mulnC apegMr //.
by rewrite /i !inordK ?apegS //= !ltnS (leq_trans k3La).
have H2 : (k %/ 3).+1 < l.+2 by rewrite !ltnS (leq_trans k3La).
rewrite -(@aMin1 i); last first.
rewrite !inordK // -{2}[a]prednK // ltnS // -[a.-1]prednK // .
by rewrite -subn1 subn_gt0.
congr (u _ _).
by apply/val_eqP; rewrite /= !inordK.
rewrite codom_subC.
case: (sitiH ((k %/ 3))) => /= [|_].
by rewrite -[a.-1]prednK // -ltnS prednK.
set pp := u _ _; (suff <- : pp = a[p1, p3] k by []); rewrite {}/pp.
set i := inord _; have <- : a[p1, p3] i = a[p1, p3] k.
have k3mH2 : k %% 3 = 2.
have := ltn_mod k 3; move: k3mH k3mH1.
by case: modn => // [] [|[]].
rewrite (divn_eq k 3) k3mH2 addn2 !apegS mulnC apegMr //.
by rewrite /i !inordK ?apegS //= !ltnS (leq_trans k3La).
have H2 : (k %/ 3) < l.+2 by rewrite !ltnS (leq_trans k3La).
rewrite -(@aMin1 i); last by rewrite !inordK // -{2}[a]prednK .
congr (u _ _).
by apply/val_eqP; rewrite /= !inordK.
have {cH2}P2 := IH p1Dp2 p1Dp3 p2Dp3 p1Dp0 p2Dp0 p3Dp0 cH2.
have a3B2_gt1 : 1 < (3 * a).-2.
rewrite -subn2 leq_subRL.
by rewrite (leq_trans (_ : 4 <= 3 * 2)) // leq_mul2l.
by rewrite (leq_trans (_ : 2 <= 3 * 2)) // leq_mul2l.
rewrite u10E uiME apeg0 in P2.
have {}P2 := leq_trans P2 (leq_add (leq_add (leqnn _) (leq_sum_beta _ _))
(leqnn _)).
pose pa := a[p1, p3] a; pose paS := a[p1, p3] a.+1.
have cH3 (k : 'I_3) :
0 < k < 2 -> codom (u2 k) \subset [:: paS; a[p2, pa] k].
case: k => [] [|[|]] //= iH _.
by rewrite /pa /paS !apegS apeg0 ffunE /= codom_apeg codom_subC.
rewrite -/x1P1 in P2.
have {cH3}P3 :
(S_[2] n.+1).*2 <= sd u2 + sp (u2 ord0) 2 (a[p2, pa] 0) +
sp (u2 ord_max) 2 (a[p2, pa] 2).
apply: IH cH3 => //; try by rewrite /pa /paS // eq_sym.
by rewrite /pa /paS apegS apeg_eqC eq_sym.
rewrite !ffunE /= apeg0 -/x2P1 in P3.
have cH4 (k : 'I_b.+1) :
0 < k < b -> codom (u3 k) \subset [:: p2; a[pa, paS] k].
rewrite /b .
move=> /andP[k_gt0 kLb].
have kBound : 0 < k + a < l.+1.
rewrite (leq_trans a_gt0) ?leq_addl //=.
by move: kLb; rewrite /b ltn_subRL addnC.
rewrite ffunE codom_liftr //.
have := @cH (inord (k + a)).
rewrite /pa /paS apegS inordK; first by rewrite -apegD; apply.
by rewrite ltnW // ltnS; case/andP: kBound.
have {cH4}P4 :
(S_[b] n.+1).*2 <= sd u3 + sp (u3 ord0) b (a[pa, paS] 0) +
sp (u3 ord_max) b (a[pa, paS] b).
apply: IH cH4 => //; try by rewrite /pa /paS // eq_sym.
by rewrite /pa /paS apegS apeg_eqC.
have {}P4 := leq_trans P4 (leq_add (leqnn _) (leq_sum_beta _ _)).
rewrite apeg0 /= (_ : a[_, _] b = a[p3, p1] l) in P4; last first.
by rewrite /pa /paS apegS -apegD subnK ?apegS.
rewrite !ffunE /= add0n -/ai in P4.
rewrite -/x3P1 subnK // (_ : inord l.+1 = ord_max) in P4; last first.
by apply/val_eqP; rewrite /= inordK.
rewrite [X in _ <= _ + X + _]sum_beta_S //= KH1 eqxx addn0.
set xS := \sum_(_ < _) _ in P2; rewrite -/xS.
rewrite [X in _ <= _ + _ + X]sum_beta_S //= KH2 mul1n.
set yS := \sum_(_ < _) _ in P4; rewrite -/yS.
set x1S := sp _ _ _ in P2; set y1S := sp _ _ _ in P3.
have x1Sy1SE : x1S + y1S <= (S_[1] n).*2 + α_[maxn (3 * a).-2 2] n.
apply: sum_alpha_diffE => //.
rewrite apeg32E //.
by move: sam1C; rewrite -uiME uaiLEp2.
rewrite (maxn_idPl _) // in x1Sy1SE.
set x2S := sp _ _ _ in P3; set y2S := sp _ _ _ in P4.
have x2y2SE : x2S + y2S <= (S_[1] n).*2 + α_[maxn 2 b] n.
apply: sum_alpha_diffE => //; first by rewrite /pa eq_sym.
have /Hcodom : 0 < (inord a : 'I_l.+2) < l.+1 by rewrite inordK // a_gt0.
by rewrite ffunE inordK.
rewrite -addnn {1}dsum_alphaL_S in P2.
rewrite -addnn {1}dsum_alphaL_S in P3.
rewrite -addnn {1}dsum_alphaL_S.
have F4 k : S_[a.-1] k.+1 <= S_[(3 *a).-2] k + a.-1.
apply: leq_trans (dsum_alpha3l _ _) _.
rewrite leq_add2r.
apply: (increasingE (increasing_dsum_alphaL_l _)).
by rewrite -{2}[a]prednK // mulnS addSn add2n /=.
have F4n := F4 n.
have {F4}F4n1 := F4 n.+1.
have F5 k :
S_[l.+1] k.+1 + S_[1] k.+1 + S_[b.+1] k.+1 <=
S_[a.-1] k.+1 + S_[b.+2] k.+1 + S_[b] k + (α_[1] k).*2.
have <- : a + b = l.+1 by rewrite addnC subnK.
rewrite -[X in _ <= X]addnA (leq_add _ (dsum_alphaL_alpha k b_gt1)) //.
have := concaveEk1 1 a.-2 b.+1 (concave_dsum_alphaL_l k.+1).
rewrite add1n prednK; last by rewrite -subn1 ltn_subRL addn0.
by rewrite -addSnnS prednK // add1n [X in _ <= X]addnC.
have F5n := F5 n.
have {F5}F5n1 := F5 n.+1.
have F6 k : S_[b.+2] k + S_[1] k <= S_[b.+1] k + S_[2] k.
by have := concaveEk1 1 1 b (concave_dsum_alphaL_l k).
have F6n := F6 n.+1.
have {F6}F6n1 := F6 n.+2.
have F72 := dsum_alphaL_S 2 n.+1.
have F721 := dsum_alphaL_S 2 n.
have F711 := dsum_alphaL_S 1 n.+1.
have F71 := dsum_alphaL_S 1 n.
have F73 := dsum_alphaL_S b n.
have F8 : α_[2] n.+1 <= α_[1] n.+2.
have ->: α_[1] n.+2 = α_[3] n.+1 by rewrite alphaL_3E.
by apply: increasing_alphaL_l.
have F9 : α_[1] (n.+2) <= (α_[1] n).*2.+2.
apply: leq_trans (leqnSn _).
by apply: alphaL_4_5.
(* Lia should work now *)
gsimpl.
applyr P1.
rewrite -(leq_add2r x1S); applyr P2.
rewrite -(leq_add2r (y1S + x2S)); applyr P3.
rewrite -(leq_add2r y2S); applyr P4.
applyl x2y2SE; applyl x1Sy1SE.
rewrite -[in 2 * a](prednK a_gt0).
applyr F4n; applyr F4n1; gsimpl.
rewrite (maxn_idPr _) //.
rewrite -(leq_add2r (S_[1] n.+1 + S_[b.+1] n.+1)); applyl F5n.
rewrite -(leq_add2r (S_[1] n.+2 + S_[b.+1] n.+2)); applyl F5n1.
gsimpl.
rewrite -(leq_add2r (S_[1] n.+1)); applyl F6n; gsimpl.
rewrite F71; gsimpl.
rewrite !add1n {}F73; gsimpl.
rewrite -(leq_add2r (S_[1] n.+2 + S_[2] n.+2)).
applyl F6n1; gsimpl.
rewrite {}F72 {}F721 {}F711 {}F71; gsimpl.
applyl F8.
by applyl F9; gsimpl.
rewrite negb_and -leqNgt -ltnNge => oH.
have [/andP[a_gt1 /eqP bE1]|] := boolP ((1 < a) && (b == 1)).
(* 2 <= a and b = 1 *)
have am1_gt0 : 0 < a.-1 by rewrite -subn1 subn_gt0.
have lEa : l = a.
have : a + b = l.+1 by rewrite addnC subnK //.
by rewrite bE1 addn1 => [] [].
have uaiLEp2 : u ai ldisk = p2.
have /cH/subsetP/(_ (u ai ldisk) (codom_f _ _)) : 0 < ai < l.+1.
by rewrite aiE a_gt0 lEa /=.
by rewrite !inE aiE (negPf aH) orbF => /eqP.
pose si i : configuration 4 n.+1 :=
sgdist1 p2 (u (inord i)) (u (inord i.+1)).
pose ti i : configuration 4 n.+1 :=
sgdist2 p2 (u (inord i)) (u (inord i.+1)).
have sitiH i : i < a.-1 ->
[/\ codom (si i) \subset [:: p2; u (inord i.+1) ldisk ],
codom (ti i) \subset [:: u (inord i) ldisk; p2] &
`d[u (inord i), u (inord i.+1)]_smove =
D[[:: ↓[u (inord i)]; si i; ti i; ↓[u (inord i.+1)]]].+2].
move=> iLa.
have iLa1 : i < a by rewrite (leq_trans iLa) // ssrnat.leq_pred.
apply: sgdistE => //.
- by rewrite aMin1.
- rewrite !aMin1 //; last by rewrite -[a]prednK.
by rewrite apegS apeg_eqC.
- by rewrite aMin1 1?eq_sym // -[a]prednK.
- by rewrite aMin1.
by rewrite aMin1 // -[a]prednK.
pose pa := a[p1, p3] a.
pose sam1 : configuration 4 n.+1 := sgdist1 pa (u (inord a.-1)) (u (inord a)).
pose tam1 : configuration 4 n.+1 := sgdist2 pa (u (inord a.-1)) (u (inord a)).
have [sam1C tam1C duam1ua1E] :
[/\ codom sam1 \subset [:: pa; u (inord a) ldisk ],
codom tam1 \subset [:: u (inord a.-1) ldisk; pa] &
`d[u (inord a.-1), u (inord a)]_smove =
D[[:: ↓[u (inord a.-1)]; sam1; tam1; ↓[u (inord a)]]].+2].
apply: sgdistE; rewrite /pa //.
- by rewrite aMin1 ?prednK // -{2}[a]prednK //= apegS apeg_eqC.
- by rewrite uaiLEp2 aMin1 // prednK.
- by rewrite uaiLEp2.
- by rewrite aMin1 // prednK.
by rewrite uaiLEp2.
have {}tam1C : codom tam1 \subset [:: p1; p3].
move: tam1C; rewrite aMin1; last by rewrite -{2}[a]prednK.
by rewrite /pa -{2}[a]prednK // apegS codom_apeg.
pose u1 :=
[ffun i =>
if ((i : 'I_(3 * a).-2.+1) == 3 * a.-1 :>nat) then ↓[u (inord a.-1)]
else if (i == (3 * a.-1).+1 :>nat) then sam1
else if (i %% 3) == 0 then ↓[u (inord (i %/ 3))]
else if (i %% 3) == 1 then si (i %/ 3)
else ti (i %/ 3)].
have u10E : u1 ord0 = ↓[u ord0].
rewrite ffunE /= ifN ?inord_eq0 //.
by rewrite neq_ltn muln_gt0 /= -ltnS prednK //= a_gt1.
have uiME : u1 ord_max = sam1.
rewrite ffunE /= eqn_leq leqNgt -{2}[a]prednK // mulnS.
rewrite addSn add2n ltnS leqnn /=.
by rewrite -{1}[a]prednK // mulnS eqxx.
pose u2 :=
[ffun i : 'I_4 =>
if i == 0 :> nat then sam1 else
if i == 1 :> nat then tam1 else
if i == 2 :> nat then ↓[u ai] else ↓[u (inord (l.+1))]].
have P1 : a.*2 + sd u1 + sd u2 <= sd u.
have G b1 : b1 = a ->
\sum_(i < (3 * b1).-2) `d[u1 (inord i), u1 (inord i.+1)]_smove =
\sum_(i < (3 * a.-1)) `d[u1 (inord i), u1 (inord i.+1)]_smove +
`d[↓[u (inord a.-1)], sam1]_smove.
move=> b1Ea.
have ta2E : (3 * b1).-2 = (3 * a.-1).+1.
by rewrite b1Ea -{1}[a]prednK // mulnS.
rewrite ta2E big_ord_recr /=; congr (_ + `d[_,_]_smove).
by rewrite ffunE ifT// inordK // -{2}b1Ea ?ta2E.
rewrite ffunE inordK; last by rewrite -{2}b1Ea ?ta2E.
by rewrite eqn_leq ltnn /= eqxx.
rewrite {}[sd u1]G //.
have -> := @sum3E _ (fun i => `d[u1 (inord i), u1 (inord i.+1)]_smove).
have -> : a.*2 = 2 + \sum_(i < a.-1) 2.
rewrite sum_nat_const /= cardT size_enum_ord.
by rewrite muln2 -(doubleD 1) add1n prednK.
rewrite !addnA -[2 + _ + _]addnA -big_split /=.
rewrite [sd u2]big_ord_recr /=.
rewrite [u2 (inord 2)]ffunE inordK //=.
rewrite [u2 (inord 3)]ffunE inordK //=.
rewrite [2 + _]addnC -!addnA 2![X in _ + X <= _]addnA addnA.
have -> : sd u =
\sum_(i < a.-1) (`d[u (inord i), u (inord i.+1)]_smove) +
`d[u (inord a.-1), u ai]_smove + `d[u ai, u (inord ai.+1)]_smove.
rewrite -lEa (_ : ai = inord l); last first.
by apply/val_eqP; rewrite /= aiE inordK // lEa.
case: l l_gt0 u => //= l1 _ f.
rewrite /sd 2!big_ord_recr //=; congr (_ + _ + `d[f _, f _]_smove).
by apply/val_eqP; rewrite /= !inordK.
apply: leq_add; first apply: leq_add.
- rewrite leq_eqVlt; apply/orP; left; apply/eqP.
apply: eq_bigr => i _.
have -> : u1 (inord (3 * i)) = ↓[u (inord i)].
rewrite ffunE inordK; last first.
rewrite ltnS (leq_trans (_ : _ <= 3 * (a.-1))) //.
by rewrite leq_mul2l /= ltnW.
by rewrite -{2}[a]prednK // mulnS addSn add2n /= ltnW.
rewrite eqn_mul2l /= eqn_leq [_ <= i]leqNgt ltn_ord andbF.
rewrite eqn_leq ltn_mul2l /= ltnNge [i <= _]ltnW // andbF.
by rewrite mod3E /= div3E.
have -> : u1 (inord (3 * i).+1) = si i.
rewrite ffunE inordK; last first.
rewrite ltnS (leq_trans (_ : _ <= 3 * (a.-1))) //.
by rewrite ltn_mul2l /=.
by rewrite -{2}[a]prednK // mulnS addSn add2n /= ltnW.
rewrite eqn_leq [_ <= _.+1]leqNgt.
rewrite (leq_trans (_ : (3 * i).+2 <= (3 * i.+1))) ?andbF //;
last 2 first.
- by rewrite mulnS addSn add2n.
- by rewrite leq_mul2l /=.
rewrite eqn_leq !ltnS [_ <= 3 * i]leq_mul2l.
by rewrite [_ <= i]leqNgt ltn_ord andbF mod3E /= div3E.
have -> : u1 (inord (3 * i).+2) = ti i.
rewrite ffunE inordK; last first.
rewrite ltnS (leq_trans (_ : _ <= 3 * (a.-1))) //.
rewrite (leq_trans (_ : _ <= 3 * (i.+1))) //.
by rewrite mulnS addSn add2n.
by rewrite leq_mul2l /=.
by rewrite -{2}[a]prednK // mulnS addSn add2n /= ltnW.
rewrite eqn_leq [_ <= _.+1]leqNgt.
rewrite -[(3 * i).+3](mulnDr 3 1 i) leq_mul2l ltn_ord andbF.
rewrite eqn_leq !ltnS [_ <= _.+1]leqNgt.
rewrite (leq_trans (_ : (3 * i).+1 < 3 * (i.+1))) ?andbF //;
last 2 first.
- by rewrite mulnS addSn add2n.
- by rewrite leq_mul2l /=.
by rewrite mod3E /= div3E.
have -> : u1 (inord (3 * i).+3) = ↓[u (inord i.+1)].
have -> : (3 * i).+3 = 3 * (i.+1) by rewrite mulnS addSn add2n.
rewrite ffunE inordK; last first.
rewrite ltnS (leq_trans (_ : _ <= 3 * (a.-1))) //.
by rewrite leq_mul2l /=.
by rewrite -{2}[a]prednK // mulnS addSn add2n /= ltnW.
rewrite eqn_mul2l /=; case: eqP => [->//|_].
rewrite eqn_leq [_ <= 3 * i.+1]leqNgt ltnS leq_mul2l /= ltn_ord andbF.
by rewrite mod3E /= div3E.
case: (sitiH i) => //= _ _.
by rewrite !addnA addn0 add2n.
- rewrite leq_eqVlt; apply/orP; left; apply/eqP.
rewrite !big_ord_recr big_ord0 //= add0n !addnA.