-
Notifications
You must be signed in to change notification settings - Fork 1
/
ghanoi.v
1881 lines (1643 loc) · 64 KB
/
ghanoi.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
(******************************************************************************)
(* *)
(* Generalised Hanoi Problem *)
(* *)
(******************************************************************************)
(* *)
(* We consider a generalisation of Hanoi problem : *)
(* a parameter n : the number of pegs *)
(* a parameter r : r p1 p2 tells that a disk can go from p1 -> p2 *)
(* *)
(* peg n == type for pegs (there are n pegs) *)
(* disk k == type for disks (there are k disk) *)
(* configuration k n == type for hanoi configuration with *)
(* k disks and n pegs *)
(* ldisk == the largest disk *)
(* sdisk == the smallest disk *)
(* d1 \larger d2 == disk d1 is larger than disk d2 *)
(* c d == the peg on which the disk d in the configuration c *)
(* `c[p] == a configuration with n disk where all the disks *)
(* `p[p1, p2] == pick a peg (if possible) that is diffenent from p1 *)
(* and p2 *)
(* are on peg p *)
(* on_top d c == the disk c is on top of its peg on the *)
(* configuration c *)
(* rrel == a regular relation between pegs, rrel p1 p2 is *)
(* true iff peg p1 is different from peg p2 *)
(* lrel == a linear relation between pegs, lrel p1 p2 is *)
(* true iff peg p1 is next to peg p2 *)
(* srel == a star relation between pegs, srel p1 p2 is *)
(* true iff p1 != p2 and p1 or p2 is the 0 peg *)
(* move r c1 c2 == checks if going from configuration c1 *)
(* to configuration c2 is a move compatible with *)
(* relation r (a relation over pegs) *)
(* cdisjoint c1 c2 == configurations c1 and c2 are on different pegs *)
(* cmerge c1 c2 == merge a configuration c1 with m disk and a *)
(* a configuration with n disks to get a configuration*)
(* with m + n disk *)
(* crshift c == right shift a configuration c with m + n disks, to *)
(* get a configuration with n disks taking the disks *)
(* larger than m *)
(* clshift c == right shift a configuration c with m + n disks, to *)
(* get a configuration with n disks taking the disks *)
(* smaller than m *)
(* ↑[c]_ p == lift a configuration by adding a largest disk on *)
(* peg p. This lifting is done from the largest one *)
(* so to accomodate the usual induction *)
(* ↓[c] == unlift a configuration by removing the largest *)
(* disk *)
(* plift i p == lift a configuration by adding a new empty peg i *)
(* *)
(******************************************************************************)
From mathcomp Require Import all_ssreflect finmap.
From hanoi Require Import extra gdist.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Section GHanoi.
Variable q : nat.
(******************************************************************************)
(* The pegs are the elements of 'I_q *)
(******************************************************************************)
Definition peg := 'I_q.
Variable r : rel peg.
Hypothesis irH : irreflexive r.
Hypothesis symH : symmetric r.
Section Disk.
(******************************************************************************)
(* The disks are represented with the element of 'I_n with *)
(* the idea that disk d1 is larger than disk d2 is d2 <= d1. *)
(******************************************************************************)
Variable n : nat.
Definition disk := 'I_n.
Definition mk_disk m (H : m < n) : disk := Ordinal H.
(******************************************************************************)
(* Given a configuration c, the disks on the peg p can be reconstructed by *)
(* the list in decreasing order of the disk d such that c d = p *)
(******************************************************************************)
Definition configuration := {ffun disk -> peg}.
(* A perfect configuration is one where all the pegs are on a single peg p *)
Definition perfect p : configuration := [ffun d => p].
End Disk.
Arguments perfect [n].
Local Notation "`c[ p ] " := (perfect p)
(format "`c[ p ]", at level 5).
(* The smallest disk *)
Definition sdisk {n} : disk n.+1 := ord0.
Lemma disk1_all (d : disk 1) : d = sdisk.
Proof. by apply/val_eqP; case: d => [] []. Qed.
Lemma configuration1_eq (c1 c2 : configuration 1) :
(c1 == c2) = (c1 sdisk == c2 sdisk).
Proof.
apply/eqP/eqP; first by move=> /ffunP/(_ sdisk).
by move=> H; apply/ffunP=> i; rewrite [i]disk1_all.
Qed.
(* The largest disk *)
Definition ldisk {n} : disk n.+1 := ord_max.
(******************************************************************************)
(* The disk d is on top of peg (c d) *)
(******************************************************************************)
Definition on_top n (d : disk n) (c : configuration n) :=
[forall d1 : disk n, (c d == c d1) ==> (d <= d1)].
Lemma on_topP n (d : disk n) (c : configuration n) :
reflect (forall d1, c d = c d1 -> d <= d1) (on_top d c).
Proof.
apply: (iffP forallP) => [H d1 cdEcd1|H d1].
by have /implyP->// := H d1; apply/eqP.
by apply/implyP=> /eqP /H.
Qed.
(******************************************************************************)
(* A move is a relation between two configurations c1 c2: *)
(* there must exist a disk d1, that is the only one that has changed of *)
(* peg (c1 d1 != c2 d1) that is on top of c1 and c2 *)
(******************************************************************************)
Definition move {n} : rel (configuration n) :=
[rel c1 c2 | [exists d1 : disk n,
[&& r ((c1 : configuration n) d1) (c2 d1),
[forall d2, (d1 != d2) ==> (c1 d2 == c2 d2)],
on_top d1 c1 &
on_top d1 c2]]].
Lemma moveP n (c1 c2 : configuration n) :
reflect
(exists d1,
[/\ r (c1 d1) (c2 d1),
forall d2, d1 != d2 -> c1 d2 = c2 d2,
on_top d1 c1 &
on_top d1 c2])
(move c1 c2).
Proof.
apply: (iffP existsP) =>
[[d /and4P[H1 /forallP H2 H3 H4]]|[d [H1 H2 H3 H4]]]; exists d.
by split => // d1 H; apply/eqP/(implyP (H2 _)).
rewrite H1 H3 H4 ! andbT /=; apply/forallP => d1; apply/implyP => H.
by rewrite H2.
Qed.
Lemma move_on_topl n d (c1 c2 : configuration n) :
move c1 c2 -> c1 d != c2 d -> on_top d c1.
Proof.
case/moveP => d1 [H1 H2 H3 H4 H5].
rewrite (_ : d = d1); first by apply: H3.
apply/eqP; case: eqP => /eqP H //.
by case/eqP: H5; apply: H2; rewrite eq_sym.
Qed.
Lemma move_on_topr n d (c1 c2 : configuration n) :
move c1 c2 -> c1 d != c2 d -> on_top d c2.
Proof.
case/moveP => d1 [H1 H2 H3 H4 H5].
rewrite (_ : d = d1); first by apply: H4.
apply/eqP; case: eqP => /eqP H //.
by case/eqP: H5; apply: H2; rewrite eq_sym.
Qed.
(* this holds if r is symmetric *)
Lemma move_sym n (c1 c2 : configuration n) : move c1 c2 = move c2 c1.
Proof.
by apply/moveP/moveP=> [] [d [H1 H2 H3 H4]];
exists d; split; rewrite 1?symH 1?eq_sym // => e dDe; apply/sym_equal/H2.
Qed.
(* In a move, the disk that moves accomodates r *)
Lemma move_diskr n (d : disk n) (c1 c2 : configuration n) :
move c1 c2 -> c1 d != c2 d -> r (c1 d) (c2 d).
Proof.
case/moveP=> d1 [H1 H2 H3 H4] /eqP c1dDc2d.
by have [/eqP<-|/H2] := boolP (d1 == d).
Qed.
(* In a move, only one disk moves *)
Lemma move_disk1 n (d1 d2 : disk n) (c1 c2 : configuration n) :
move c1 c2 -> c1 d1 != c2 d1 -> d1 != d2 -> c1 d2 = c2 d2.
Proof.
case/moveP=> d3 [H1 H2 H3 H4] c1d1Dc2d1 d1Dd2.
have [/eqP d3Ed1|d3Dd1] := boolP (d3 == d1); last first.
by case/eqP: c1d1Dc2d1; apply: H2.
by apply: H2; rewrite d3Ed1.
Qed.
Lemma move_on_toplDl n (d d1 : disk n) (c1 c2 : configuration n) :
move c1 c2 -> c1 d != c2 d -> d1 < d -> c1 d1 != c1 d.
Proof.
move=> c1Mc2 c1Dc2; rewrite eq_sym ltnNge; apply: contra => /eqP.
by have /on_topP := move_on_topl c1Mc2 c1Dc2; apply.
Qed.
Lemma move_on_toplDr n (d d1 : disk n) (c1 c2 : configuration n) :
move c1 c2 -> c1 d != c2 d -> d1 <= d -> c1 d1 != c2 d.
Proof.
move=> c1Mc2 c1Dc2; rewrite leq_eqVlt => /orP[/val_eqP->//|dLd1].
move: (dLd1); rewrite eq_sym ltnNge (move_disk1 c1Mc2 c1Dc2) //; last first.
by rewrite neq_ltn dLd1 orbT.
apply: contra => /eqP.
by have /on_topP := move_on_topr c1Mc2 c1Dc2; apply.
Qed.
Lemma move_on_toprDr n (d d1 : disk n) (c1 c2 : configuration n) :
move c1 c2 -> c1 d != c2 d -> d1 < d -> c2 d1 != c2 d.
Proof.
move=> c1Mc2 c1Dc2; rewrite eq_sym ltnNge; apply: contra => /eqP.
by have /on_topP := move_on_topr c1Mc2 c1Dc2; apply.
Qed.
Lemma move_on_toprDl n (d d1 : disk n) (c1 c2 : configuration n) :
move c1 c2 -> c1 d != c2 d -> d1 <= d -> c2 d1 != c1 d.
Proof.
move=> c1Mc2 c1Dc2; rewrite leq_eqVlt => /orP[/val_eqP->//|dLd1].
by rewrite eq_sym.
move: (dLd1); rewrite ltnNge -(move_disk1 c1Mc2 c1Dc2) eq_sym //; last first.
by rewrite neq_ltn dLd1.
apply: contra => /eqP.
have /on_topP := move_on_topl c1Mc2 c1Dc2; apply.
Qed.
(* configuration on different pegs *)
Definition cdisjoint m n (c1 : configuration m) (c2 : configuration n) :=
[forall i, [forall j, c1 i != c2 j]].
Lemma cdisjointP m n (c1 : configuration m) (c2 : configuration n) :
reflect (forall i j, c1 i != c2 j) (cdisjoint c1 c2).
Proof.
apply: (iffP forallP) => [H i j| H i].
by move/forallP : (H i); apply.
by apply/forallP=> j; apply: H.
Qed.
(* merging two configurations : c1 for the small disks, c2 for the big ones *)
Definition cmerge m n (c1 : configuration m) (c2 : configuration n) :=
[ffun i => match tsplit i with inl j => c1 j | inr j => c2 j end].
(* right shifting a configuration : taking the disks smaller than n *)
Definition crshift m n (c : configuration (m + n)) : configuration n :=
[ffun i => c (trshift m i)].
(* left shifting a configuration : taking the disks larger than n *)
Definition clshift m n (c : configuration (m + n)) : configuration m :=
[ffun i => c (tlshift n i)].
(* Sanity check *)
Lemma cmergeK m n (c : configuration (m + n)) :
cmerge (clshift c) (crshift c) = c.
Proof.
apply/ffunP=> i; rewrite !ffunE.
by case: tsplitP => [] j iE; rewrite !ffunE /=;
congr fun_of_fin; apply/val_eqP/eqP.
Qed.
Lemma tsplit_tlshift k m (x : 'I_k) : tsplit (tlshift m x) = inl x.
Proof.
case: tsplitP=> [j /= jE|k1 /= /eqP].
by have := ltn_ord j; rewrite -jE ltnNge leq_addl.
rewrite eqn_add2r => /eqP H.
by congr (inl _); apply: val_inj.
Qed.
Lemma tsplit_trshift k m (x : 'I_k) : tsplit (trshift m x) = inr x.
Proof.
case: tsplitP => [j /= jE|k1 /= xE].
by congr (inr _); apply: val_inj.
by have := ltn_ord x; rewrite xE ltnNge leq_addl.
Qed.
Lemma on_top_merger m n x (c1 : configuration m) (c2 : configuration n) :
on_top (trshift m x) (cmerge c1 c2) = on_top x c2.
Proof.
apply/on_topP/on_topP.
move=> Hx d2 cxEcd2.
have := Hx (trshift m d2).
by rewrite !ffunE !tsplit_trshift => /(_ cxEcd2).
move=> H d; rewrite !ffunE tsplit_trshift /=.
case: tsplitP => [j -> H1 | k -> _].
by apply: H.
by apply: leq_trans (ltnW _) (leq_addl _ _).
Qed.
Lemma move_merger m n (c : configuration n) (c1 c2 : configuration m) :
move (cmerge c c1) (cmerge c c2) = move c1 c2.
Proof.
apply/moveP/moveP => [] [d1 [H1d1 H2d1 H3d1 H4d1]]; last first.
exists (trshift n d1); split => //; try by rewrite on_top_merger.
by rewrite !ffunE /= tsplit_trshift.
move=> d2; rewrite !ffunE; case: tsplitP => // k d2E H.
apply: H2d1 => //; apply: contra H => /eqP->.
by apply/eqP/val_eqP/eqP.
move: H1d1; rewrite !ffunE.
case: tsplitP => [j jE H1x|k _]; last by rewrite irH.
have d1E : (trshift n j) = d1 by apply/val_eqP/eqP.
rewrite -d1E in H3d1 H4d1.
exists j; split => //; try by rewrite -(on_top_merger _ c).
move=> d2 kDd2.
have := H2d1 (trshift n d2).
rewrite !ffunE tsplit_trshift => H.
apply: H.
apply: contra kDd2 => /eqP/val_eqP /=.
by rewrite jE.
Qed.
Lemma path_merger m n (c1 : configuration m)
(c2 : configuration n) (cs : seq (configuration _)) :
path move (cmerge c1 c2) [seq cmerge c1 c | c <- cs] =
path move c2 cs.
Proof. by elim: cs c2 => //= c3 cs IH c2; rewrite move_merger IH. Qed.
Lemma connect_merger m n (c : configuration m) (c1 c2 : configuration n) :
connect move c1 c2 -> connect move (cmerge c c1) (cmerge c c2).
Proof.
move=> /connectP[x]; rewrite -(path_merger c) => Hp Hl.
apply/connectP; eexists; first exact: Hp.
by rewrite Hl [RHS]last_map.
Qed.
(* this should be equality *)
Lemma gdist_merger m n (c: configuration m) (c1 c2 : configuration n) :
connect move c1 c2 ->
`d[cmerge c c1, cmerge c c2]_move <= `d[c1, c2]_move.
Proof.
move=> /gpath_connect[p1 p1H].
rewrite (gpath_dist p1H) -(size_map (cmerge c)).
apply: gdist_path_le; first by rewrite path_merger (gpath_path p1H).
by rewrite [LHS]last_map (gpath_last p1H).
Qed.
Lemma on_top_mergel m n (c1 : configuration m) (c2 : configuration n) d :
cdisjoint c1 c2 -> on_top d c1 -> on_top (tlshift n d) (cmerge c1 c2).
Proof.
move=> c1Dc2 /on_topP dTc; apply/on_topP => d1.
rewrite !ffunE tsplit_tlshift /=.
case: tsplitP => j -> c1dc1j; last first.
by rewrite leq_add2r; apply: dTc.
by have /cdisjointP/(_ d j)/eqP[] := c1Dc2.
Qed.
Lemma on_top_mergel_inv m n (c1 : configuration m) (c2 : configuration n) d :
on_top (tlshift n d) (cmerge c1 c2) -> on_top d c1.
Proof.
move=> /on_topP dTc; apply/on_topP => d1 Hc.
have := dTc (tlshift n d1).
by rewrite !ffunE !tsplit_tlshift /= leq_add2r; apply.
Qed.
Lemma move_mergel m n (c1 c2 : configuration m) (c : configuration n) :
move (cmerge c1 c) (cmerge c2 c) -> move c1 c2.
Proof.
move=> /moveP [x [H1x H2x H3x H4x]]; apply/moveP.
move: H1x; rewrite !ffunE.
case: tsplitP => [j | k kE J1x]; first by rewrite irH.
have xE : (tlshift n k) = x by apply/val_eqP/eqP.
rewrite -xE in H3x H4x.
exists k; split => //; try by apply: on_top_mergel_inv; eassumption.
move=> d2 kDd2.
have := H2x (tlshift n d2).
rewrite !ffunE tsplit_tlshift => H.
apply: H.
apply: contra kDd2 => /eqP/val_eqP /=.
by rewrite kE eqn_add2r.
Qed.
Lemma move_mergel_inv m n (c1 c2 : configuration m)
(c : configuration n) :
cdisjoint c1 c -> cdisjoint c2 c ->
move c1 c2 -> move (cmerge c1 c) (cmerge c2 c).
Proof.
move=> c1Dc c2Dc /moveP[x [H1x H2x H3x H4x]]; apply/moveP.
exists (tlshift n x); split => //; try by apply/on_top_mergel.
by rewrite !ffunE /= tsplit_tlshift.
move=> d2; rewrite !ffunE; case: tsplitP => // k d2E H.
apply: H2x => //; apply: contra H => /eqP->.
by apply/eqP/val_eqP/eqP.
Qed.
Lemma path_mergel m n (c1 : configuration m)
(c2 : configuration n) (cs : seq (configuration _)) :
path move (cmerge c1 c2) [seq cmerge i c2 | i <- cs] ->
path move c1 cs.
Proof. by elim: cs c1 => //= c3 cs IH c1 /andP[/move_mergel-> /IH]. Qed.
Lemma path_mergel_inv m n (c1 : configuration m)
(c2 : configuration n) (cs : seq (configuration _)) :
all (fun i => cdisjoint i c2) (c1 :: cs) ->
path move c1 cs ->
path move (cmerge c1 c2) [seq cmerge i c2 | i <- cs].
Proof.
elim: cs c1 => //= c3 cs IH c1 /and3P[c1Dc2 c3Dc2 Dcs]
/andP[/move_mergel_inv ->// /IH->//].
by rewrite c3Dc2.
Qed.
Lemma on_top_shift m n (c : configuration (m + n)) d :
on_top d c ->
match tsplit d with
| inl d1 => on_top d1 (clshift c)
| inr d2 => on_top d2 (crshift c)
end.
Proof.
rewrite -{1}[d](tsplitK d).
case: tsplitP => /= j dE /on_topP /= dT;
apply/on_topP => d1; rewrite !ffunE /= => H.
by apply: dT H.
by have /= := dT _ H; rewrite leq_add2r.
Qed.
Lemma move_clshift m n (c1 c2 : configuration (m + n)) :
move c1 c2 -> (clshift c1) != (clshift c2) ->
move (clshift c1) (clshift c2).
Proof.
move=> /moveP [x].
rewrite -(tsplitK x).
have := @on_top_shift _ _ c1 x.
have := @on_top_shift _ _ c2 x.
case: tsplitP => /= [j xE c2H c1H [H1j H2j H3j H4j] c1Dc2|
k xE c2H c1H [H1k H2k H3k H4k] c1Dc2].
case/eqP: c1Dc2; apply/ffunP => i.
rewrite !ffunE.
apply/H2j/eqP/val_eqP; rewrite eqn_leq /= negb_and.
by rewrite orbC -ltnNge (leq_trans (ltn_ord _)) // leq_addl.
apply/moveP; exists k; split=> [|d2 kDd2||]; rewrite ?ffunE //.
- apply: H2k => /=.
by apply/eqP/val_eqP; rewrite /= eqn_add2r; apply/val_eqP/eqP.
- apply: c1H; rewrite (_ : x = (tlshift n k)) //.
by apply/val_eqP/eqP => /=.
apply: c2H; rewrite (_ : x = (tlshift n k)) //.
by apply/val_eqP/eqP => /=.
Qed.
Lemma move_crshift m n (c1 c2 : configuration (m + n)) :
move c1 c2 -> (crshift c1) != (crshift c2) ->
move (crshift c1) (crshift c2).
Proof.
move=> /moveP [x].
rewrite -(tsplitK x).
have := @on_top_shift _ _ c1 x.
have := @on_top_shift _ _ c2 x.
case: tsplitP => /= [j xE c2H c1H [H1j H2j H3j H4j] c1Dc2|
k xE c2H c1H [H1k H2k H3k H4k] c1Dc2]; last first.
case/eqP: c1Dc2; apply/ffunP => i.
rewrite !ffunE.
apply/H2k/eqP/val_eqP; rewrite eqn_leq /= negb_and.
by rewrite -ltnNge (leq_trans (ltn_ord _)) // leq_addl.
apply/moveP; exists j; split=> [|d2 kDd2||]; rewrite ?ffunE //.
- by apply: H2j.
- apply: c1H; rewrite (_ : x = (trshift m j)) //.
by apply/val_eqP/eqP => /=.
apply: c2H; rewrite (_ : x = (trshift m j)) //.
by apply/val_eqP/eqP => /=.
Qed.
Fixpoint rm_rep (A : eqType) (a : A) (s : seq A) :=
if s is b :: s1 then
if a == b then rm_rep b s1 else b :: rm_rep b s1
else [::].
Lemma subseq_rm_rep (A : eqType) (a : A) s : subseq (rm_rep a s) s.
Proof.
elim: s a => // b s IH a.
rewrite [rm_rep _ _]/=; case: (a == b).
by apply: subseq_trans (IH _) (subseq_cons _ _).
by rewrite /= eqxx IH.
Qed.
Lemma subset_rm_rep (A : eqType) (a : A) s : {subset rm_rep a s <= s}.
Proof. by apply/mem_subseq/subseq_rm_rep. Qed.
Lemma size_rm_rep (A : eqType) (a : A) s : size (rm_rep a s) <= size s.
Proof. by apply/size_subseq/subseq_rm_rep. Qed.
Lemma size_rm_rep_cons (A : eqType) (a b : A) s :
size (rm_rep b s) <= size (rm_rep a (b :: s)).
Proof. by rewrite /=; case: (_ == _) => //=. Qed.
Lemma size_rm_rep_subset (A : finType) (a : A) (s1 : {set A}) (s2 : seq A) :
{subset s1 <= s2} -> a \notin s1 -> #|s1| <= size (rm_rep a s2).
Proof.
elim: s2 s1 a => /= [s1 a aS _|b s2 IH s1 a s1S aNIs1].
rewrite leqn0 cards_eq0; apply/eqP/setP=> i.
by rewrite inE; apply/idP => /aS.
case: eqP=> [aEb|aDb].
rewrite -aEb; apply: IH => //.
move=> i is1; have := is1; have := s1S _ is1; rewrite inE.
by case/orP=> [/eqP->|//]; rewrite -aEb => aIs1; case/negP: aNIs1.
have [bIs1|bNIs1]/= := boolP (b \in s1); last first.
apply: leq_trans (IH _ _ _ bNIs1) _ => //.
move=> i is1; have := is1; have := s1S _ is1; rewrite inE.
by case/orP=> [/eqP->|//] => bIs1; case/negP: bNIs1.
rewrite (cardsD1 b) bIs1 ltnS.
apply: IH => [i|].
by rewrite !inE => /andP[iDb1 /s1S]; rewrite inE (negPf iDb1).
by rewrite !inE eqxx.
Qed.
Lemma last_rm_rep (A : eqType) (a : A) s : last a (rm_rep a s) = last a s.
Proof.
by elim: s a => //= b l IH a; case: (_ =P _) => /= [->|_]; apply: IH.
Qed.
Lemma cat_rm_rep (A : eqType) (a : A) s1 s2 :
rm_rep a (s1 ++ s2) = rm_rep a s1 ++ rm_rep (last a s1) s2.
Proof.
elim: s1 s2 a => //= b s1 IH s2 a.
case: eqP => [aEb|aDb] /=; first by apply: IH.
by congr (_ :: _); apply: IH.
Qed.
Lemma mem_rm_rep (A : eqType) (a b: A) s :
b != a -> b \in s -> b \in rm_rep a s.
Proof.
elim: s a => //= c s IH a bDa; rewrite inE.
case: (boolP (b == c)) => /= [/eqP<- _|bDc bIs].
by rewrite eq_sym (negPf bDa) inE eqxx.
by have := IH _ bDc bIs; case: (_ == _); rewrite // inE orbC => ->.
Qed.
Lemma path_clshift m n (c : configuration (m + n)) cs :
path move c cs ->
path move (clshift c) (rm_rep (clshift c) [seq (clshift i) | i <- cs]).
Proof.
elim: cs c => //= c1 cs IH c => /andP[cMc1 c1Pcs].
case: eqP => [->|/eqP cDc1 /=]; first by apply: IH.
by rewrite move_clshift //=; apply: IH.
Qed.
Lemma path_crshift m n (c : configuration (m + n)) cs :
path move c cs ->
path move (crshift c) (rm_rep (crshift c) [seq (crshift i) | i <- cs]).
Proof.
elim: cs c => //= c1 cs IH c => /andP[cMc1 c1Pcs].
case: eqP => [->|/eqP cDc1 /=]; first by apply: IH.
by rewrite move_crshift //=; apply: IH.
Qed.
Lemma path_shift m n (c : configuration (m + n)) cs :
path move c cs ->
size cs = (size (rm_rep (clshift c) [seq (clshift i) | i <- cs]) +
size(rm_rep (crshift c) [seq (crshift i) | i <- cs]))%nat.
Proof.
elim: cs c => //= c1 cs IH c /andP[/moveP[d [d2H d2H1 d2H2 d2H3]] c1Pcs].
case: (tsplitP d) => [j dE | k dE].
rewrite ifT; last first.
apply/eqP/ffunP=> i; rewrite !ffunE.
apply: d2H1; apply/eqP => /val_eqP /=.
by rewrite dE eqn_leq andbC leqNgt (leq_trans (ltn_ord _)) ?leq_addl.
rewrite ifN /=; last first.
apply/eqP => /ffunP /(_ j); rewrite !ffunE => cE.
move: d2H; rewrite (_ : c d = c1 d) ?(irH) //.
by rewrite (_ : d = (trshift m j)) //=; apply/val_eqP/eqP.
rewrite addnS; congr _.+1.
by apply: IH.
rewrite ifN /=; last first.
apply/eqP => /ffunP /(_ k); rewrite !ffunE => cE.
move: d2H; rewrite (_ : c d = c1 d) ?(irH) //.
by rewrite (_ : d = (tlshift n k)) //=; apply/val_eqP/eqP.
rewrite ifT; last first.
apply/eqP/ffunP=> i; rewrite !ffunE.
apply: d2H1; apply/eqP => /val_eqP /=.
by rewrite dE eqn_leq leqNgt (leq_trans (ltn_ord _)) ?leq_addl.
congr _.+1.
by apply: IH.
Qed.
Lemma gdist_clshift n m (c1 c2 : configuration (m + n)) :
connect move c1 c2 ->
`d[clshift c1, clshift c2]_move <= `d[c1, c2]_move.
Proof.
move=> /gpath_connect[p pH].
have := size_rm_rep (clshift c1) [seq (clshift i) | i <- p].
rewrite (gpath_dist pH) size_map; move/(leq_trans _); apply.
apply: gdist_path_le; first by apply/path_clshift/(gpath_path pH).
by rewrite last_rm_rep last_map (gpath_last pH).
Qed.
Lemma gdist_crshift n m (c1 c2 : configuration (m + n)) :
connect move c1 c2 ->
`d[crshift c1, crshift c2]_move <= `d[c1, c2]_move.
Proof.
move=> /gpath_connect[p pH].
have := size_rm_rep (crshift c1) [seq (crshift i) | i <- p].
rewrite (gpath_dist pH) size_map; move/(leq_trans _); apply.
apply: gdist_path_le; first by apply/path_crshift/(gpath_path pH).
by rewrite last_rm_rep last_map (gpath_last pH).
Qed.
Lemma gdist_cshift n m (c1 c2 : configuration (m + n)) :
connect move c1 c2 ->
`d[clshift c1, clshift c2]_move + `d[crshift c1, crshift c2]_move
<= `d[c1, c2]_move.
Proof.
move=> /gpath_connect[p pH].
rewrite (gpath_dist pH) (path_shift (gpath_path pH)).
apply: leq_add.
apply: gdist_path_le; first by apply/path_clshift/(gpath_path pH).
by rewrite last_rm_rep last_map (gpath_last pH).
apply: gdist_path_le; first by apply/path_crshift/(gpath_path pH).
by rewrite last_rm_rep last_map (gpath_last pH).
Qed.
Definition cliftrn m n p (c : configuration n) : configuration (m + n) :=
cmerge `c[p] c.
Definition cliftr n : _ -> _ -> configuration n.+1 := @cliftrn 1 n.
Notation " ↑[ c ]_ p" := (cliftr p c) (at level 5, format "↑[ c ]_ p").
Definition cliftln m n p (c : configuration m) : configuration (m + n) :=
cmerge c `c[p].
Definition cliftl n c p : configuration (n + 1) := (@cliftln n 1 c p).
Lemma cliftr_ldisk n p (c : configuration n) : ↑[c]_p ldisk = p.
Proof.
rewrite ffunE; case: tsplitP => /= [j nE|t].
by have := ltn_ord j; rewrite {2}nE ltnn.
by rewrite ffunE.
Qed.
Lemma on_top_liftrn n m p x (c : configuration n) :
on_top (trshift m x) (cliftrn m p c) = on_top x c.
Proof. exact: on_top_merger. Qed.
Lemma move_liftrn n m p (c1 c2 : configuration n) :
move (cliftrn m p c1) (cliftrn m p c2) = move c1 c2.
Proof. exact: move_merger. Qed.
Lemma path_liftrn n m p (c : configuration n) (cs : seq (configuration _)) :
path move (cliftrn m p c) (map (cliftrn m p) cs) =
path move c cs.
Proof. exact: path_merger. Qed.
Lemma connect_liftrn n m p (c1 c2 : configuration n) :
connect move c1 c2 -> connect move (cliftrn m p c1) (cliftrn m p c2).
Proof. exact: connect_merger. Qed.
Lemma gdist_liftrn n m p (c1 c2 : configuration n) :
connect move c1 c2 ->
`d[cliftrn m p c1, cliftrn m p c2]_move <= `d[c1, c2]_move.
Proof. exact: gdist_merger. Qed.
Lemma move_liftr n p (c1 c2 : configuration n) :
move ↑[c1]_p ↑[c2]_p = move c1 c2.
Proof. by exact: move_liftrn 1 p c1 c2. Qed.
Lemma perfect_liftr n p : ↑[`c[p]]_p = `c[p] :> configuration n.+1.
Proof.
apply/ffunP => i; rewrite !ffunE.
by case: tsplitP => [j|k]; rewrite !ffunE.
Qed.
Definition cunliftr {n} (c : configuration n.+1) : configuration n :=
@crshift 1 n c.
Notation " ↓[ c ]" := (cunliftr c) (at level 5, format "↓[ c ]").
Lemma cliftrK n p : cancel (cliftr p) (cunliftr : _ -> configuration n).
Proof. by move=> c; apply/ffunP => i; rewrite !ffunE tsplit_trshift. Qed.
Lemma cunliftrK n (c : configuration n.+1) : ↑[↓[c]]_(c ldisk) = c.
Proof.
apply/ffunP => i; rewrite !ffunE.
case: tsplitP => [] j iE; rewrite !ffunE; congr fun_of_fin;
apply/val_eqP/eqP => //=.
by rewrite iE; case: (j) => [] [].
Qed.
Lemma cliftr_inj n p : injective (@cliftr n p).
Proof. by move=> c1 c2 c1Ec2; rewrite -[c1](cliftrK p) c1Ec2 cliftrK. Qed.
Lemma map_eqr (T1 T2 : eqType) (f : T1 -> T2) (s1 s2 : seq T1) :
injective f ->
([seq f i | i <- s1] == [seq f i | i <- s2]) = (s1 == s2).
Proof.
elim: s1 s2 => [[|] //|a s1 IH [|b s2]//=] fInj.
rewrite !eqseq_cons IH //.
case: (a =P b) => [->//|]; first by rewrite eqxx.
by case: (f a =P f b) => [/fInj|//].
Qed.
Lemma eq_map_liftr n p (cs1 cs2 : seq (configuration n)) :
([seq ↑[i]_p | i <- cs1] == [seq ↑[i]_p | i <- cs2]) = (cs1 == cs2).
Proof. by apply/map_eqr/cliftr_inj. Qed.
Lemma perfect_unliftr n p : ↓[`c[p]] = `c[p] :> configuration n.
Proof. by apply/ffunP => i; rewrite !ffunE. Qed.
Lemma s2f_liftr n (c : configuration n.+1) (p : peg) :
s2f ([set i | c i == p] :\ ord_max) = s2f [set i | cunliftr c i == p].
Proof.
apply/fsetP=> i.
apply/imfsetP/imfsetP => [] [/= j].
rewrite !inE => /andP[jDn /eqP cjEp] jEi.
have jLn : j < n.
rewrite -val_eqE /= in jDn.
by have := ltn_ord j; rewrite ltnS leq_eqVlt (negPf jDn).
exists (Ordinal jLn) => //=; rewrite !inE ffunE; apply/eqP.
by rewrite -cjEp; congr (c _); apply: val_eqP.
rewrite inE ffunE => /eqP cjEp iEj; exists (trshift 1 j) => //.
by rewrite !inE cjEp -val_eqE //= neq_ltn ltn_ord /=.
Qed.
Lemma codom_liftr n (c : configuration n.+1) (s : seq peg) :
codom c \subset s -> codom ↓[c] \subset s.
Proof.
move=> H; apply/subsetP => i /mapP[j _ ->].
by rewrite ffunE; apply: (subsetP H); apply: codom_f.
Qed.
Lemma move_ldisk n (c1 c2 : configuration n.+1) :
move c1 c2 -> c1 ldisk != c2 ldisk -> ↓[c1] = ↓[c2].
Proof.
move=> c1Mc2 c10Dc20.
apply/ffunP=> i; rewrite !ffunE /=.
apply: move_disk1 c1Mc2 c10Dc20 _.
apply/eqP/val_eqP => /=.
by rewrite eqn_leq negb_and -ltnNge ltn_ord.
Qed.
Lemma move_unliftr n (c1 c2 : configuration n.+1) :
c1 ldisk = c2 ldisk -> move ↓[c1] ↓[c2] = move c1 c2.
Proof.
by move=> c1lEc2l; rewrite -(@move_liftr _ (c1 ldisk)) {2}c1lEc2l !cunliftrK.
Qed.
Lemma path_move_rev (n : nat) (c : configuration n) cs :
path move (last c cs) (rev (belast c cs)) = path move c cs.
Proof.
by rewrite rev_path; apply: eq_path => c1 c2; exact: move_sym.
Qed.
Lemma path_liftr n p (c : configuration n) (cs : seq (configuration _)) :
path move ↑[c]_p [seq ↑[i]_p | i <- cs] = path move c cs.
Proof. by apply: (@path_merger 1). Qed.
Lemma connect_liftr n p (c1 c2 : configuration n) :
connect move c1 c2 -> connect move ↑[c1]_p ↑[c2]_p.
Proof. by apply: (@connect_merger 1). Qed.
Lemma gdist_liftr n p (c1 c2 : configuration n) :
connect move c1 c2 -> `d[↑[c1]_p, ↑[c2]_p]_move <= `d[c1, c2]_move .
Proof. by apply: (@gdist_merger 1). Qed.
Lemma path_unlift_eq n (c : configuration n.+1) (cs : seq (configuration _)) :
(forall c1, c1 \in cs -> c1 ldisk = c ldisk)->
path move ↓[c] [seq ↓[i] | i <- cs] = path move c cs.
Proof.
move=> H.
rewrite -(@path_liftr _ (c ldisk)).
rewrite cunliftrK -map_comp.
congr path.
elim: cs H => //= a cs IH H.
rewrite -{1}(H a).
by rewrite cunliftrK IH // => v Hv; apply: H; rewrite inE orbC Hv.
by rewrite inE eqxx.
Qed.
Lemma path_unlift n (c : configuration n.+1) (cs : seq (configuration _)) :
path move c cs ->
path move ↓[c] (rm_rep ↓[c] [seq ↓[i] | i <- cs]).
Proof. by move=> H; apply: path_crshift. Qed.
Lemma gdist_cunlift n (c1 c2 : configuration n.+1) :
connect move c1 c2 -> `d[↓[c1], ↓[c2]]_move <= `d[c1, c2]_move.
Proof. by move=> H; apply: gdist_crshift. Qed.
Lemma gdist_cunlift_eq n (c1 c2 : configuration n.+1) :
irreflexive r ->
connect move c1 c2 -> c1 ldisk = c2 ldisk ->
`d[c1, c2]_move = `d[↓[c1], ↓[c2]]_move.
Proof.
move=> ir c1Cc2 c1Ec2; apply/eqP; rewrite eqn_leq gdist_cunlift // andbT.
rewrite -{1}[c1]cunliftrK -{1}[c2]cunliftrK -c1Ec2.
apply: gdist_liftr => //.
case/connectP: c1Cc2 => p pH c2E.
apply/connectP; exists (rm_rep (↓[c1]) ([seq ↓[i] | i <- p])) => //.
by apply: path_unlift.
by rewrite last_rm_rep c2E last_map.
Qed.
Lemma perfect_liftrn m n p :
cliftrn m p (`c[p]) = `c[p] :> configuration (m + n).
Proof.
by apply/ffunP => i; rewrite !ffunE; case: tsplitP => j; rewrite !ffunE.
Qed.
(* case distinction that depends if the largest disk has move *)
(* if it has not moved, we get the path on the unlifted configuration *)
(* uf it has moved, we get the decomposition till the first move *)
Inductive pathS_spec (n : nat) (c : configuration n.+1)
(cs : seq (configuration n.+1)) :
forall (b : bool), Type
:=
pathS_specW :
forall (p := c ldisk) (c1 := ↓[c]) (cs1 := [seq ↓[i] | i <- cs]),
cs = [seq ↑[i]_p | i <- cs1] -> path move c1 cs1 -> pathS_spec c cs true |
pathS_spec_move :
forall (p1 := c ldisk) p2 cs1 cs2
(c1 := ↓[c])
(c2 := ↑[last c1 cs1]_p2),
p1 != p2 -> r p1 p2 ->
cs = [seq ↑[i]_p1 | i <- cs1] ++ c2 :: cs2 ->
path move c1 cs1 -> move ↑[last c1 cs1]_p1 c2 ->
path move c2 cs2 ->
pathS_spec c cs true |
pathS_spec_false : pathS_spec c cs false.
(* Inversion theorem on a path for disk n.+1 *)
Lemma pathSP n (c : configuration n.+1) cs : pathS_spec c cs (path move c cs).
Proof.
have [Hp|_] := boolP (path _ _ _); last by apply: pathS_spec_false.
pose f (c1 : configuration n.+1) := c1 ldisk != c ldisk.
have [Hh|/hasPn Hn] := boolP (has f cs); last first.
have csE : cs = [seq ↑[i]_(c ldisk) | i <- [seq ↓[i] | i <- cs]].
rewrite -map_comp -[LHS]map_id.
apply/eq_in_map => x /Hn; rewrite negbK => /eqP <-/=.
by rewrite cunliftrK.
apply: pathS_specW csE _.
by rewrite path_unlift_eq // => c1 /Hn; rewrite negbK => /eqP.
pose n1 := find f cs; pose lc1 := nth c cs n1.
pose p1 := c ldisk; pose p2 := lc1 ldisk.
have p1Dp2 : p1 != p2.
by have := nth_find c Hh; rewrite eq_sym.
pose c1 := ↓[c].
pose lcs1 := take n1 cs; pose cs2 := drop n1.+1 cs.
have slcs1 : size lcs1 = n1 by rewrite size_take -has_find Hh.
have Plcs1 c2 : c2 \in lcs1 -> c2 ldisk = p1.
move=> c2Ilcs1p; move: (c2Ilcs1p).
rewrite -index_mem slcs1 => /(before_find c) /idP /negP.
rewrite negbK => /eqP.
by rewrite -[cs](cat_take_drop n1) nth_cat index_mem c2Ilcs1p nth_index.
pose cs1 := map cunliftr lcs1.
have lcs1E : lcs1 = [seq ↑[i]_p1 | i <- cs1].
rewrite -map_comp -[LHS]map_id.
apply/eq_in_map => x /Plcs1 H.
by rewrite /= -H cunliftrK.
have csE : cs = [seq ↑[i]_p1 | i <- cs1] ++ lc1 :: cs2.
by rewrite -[cs](cat_take_drop n1.+1) -cat_rcons -lcs1E
(take_nth c) // -has_find.
have Hm : move (last c lcs1) lc1.
by move: Hp; rewrite csE lcs1E cat_path /= => /and3P[].
have lc1E : lc1 = cliftr p2 (last c1 cs1).
rewrite /p2.
have Hd : (last c lcs1) ldisk != lc1 ldisk.
case: (lcs1) Plcs1 => //= a l -> //.
by apply: mem_last.
rewrite last_map -[LHS]cunliftrK.
congr cliftr.
apply/ffunP=> i; rewrite !ffunE /=.
apply/sym_equal/(move_disk1 Hm Hd).
apply/eqP/val_eqP => /=.
by rewrite eqn_leq negb_and -ltnNge ltn_ord.
have Hm1 : move ↑[last c1 cs1]_p1 ↑[last c1 cs1]_p2.
have ->: ↑[last ↓[c] cs1]_p1 = last c lcs1.
by rewrite -[in LHS]last_map -lcs1E cunliftrK.
by rewrite -lc1E.
apply: pathS_spec_move (p1Dp2) _ _ _ (Hm1) _ => //.
- have := @move_diskr _ ldisk _ _ Hm1.
by rewrite !cliftr_ldisk; apply.
- by rewrite csE lc1E -lcs1E.
- rewrite path_unlift_eq => //.
by move: Hp; rewrite -[cs](cat_take_drop n1) cat_path => /andP[].
rewrite -lc1E.
by move: Hp; rewrite csE cat_path /= => /and3P[].
Qed.
(* we can restrict a path from n.+1 to n by removing all the moves of the *)
(* largest disk *)
Lemma pathS_restrict n (c : configuration n.+1) cs :
path move c cs ->
{cs'|
[/\ path move ↓[c] cs',
last ↓[c] cs' = ↓[last c cs] &
size cs' <= size cs ?=
iff (cs == [seq ↑[i]_(c ldisk) | i <- cs'])]}.
Proof.
elim: cs c => /= [c _|c1 cs IH c /andP[cMc1 /IH[cs1 [c1Pcs1 lccs1Mlc1cs S]]]].
by exists [::]; split; rewrite ?setd_id //; apply: leqif_refl; rewrite !eqxx.
have [/eqP c1dEcd|c1dDcd] := boolP (c ldisk == c1 ldisk).
exists (cunliftr c1 :: cs1); split=> //=.
by rewrite move_unliftr // cMc1.
by rewrite eqseq_cons c1dEcd cunliftrK eqxx.
have -> : cunliftr c = cunliftr c1 by apply: move_ldisk.
exists cs1; split=> //=.
apply/leqifP; case: eqP=> [H|/= _]; last by rewrite ltnS S.
by rewrite -[_.+1]/(size (c1 :: cs)) H size_map.
Qed.
(* we can restrict a path from n.+1 to n *)
Lemma pathS_restrictD n (c : configuration n.+1) c1 cs :
path move c cs -> c1 \in cs -> c1 ldisk != c ldisk ->
{cs'| [/\ path move ↓[c] cs',
last ↓[c] cs' = ↓[last c cs] & size cs' < size cs]}.
Proof.
move=> cPcs c1Ics c1dDcd.
have [cs1 [H1 H2 H3]] := pathS_restrict cPcs.
exists cs1; split => //.
move/leqifP : H3; case: eqP => // cs1Ecs.
case/eqP: c1dDcd.
move: c1Ics; rewrite cs1Ecs => /mapP[x xIcs1 ->].
by rewrite cliftr_ldisk.
Qed.
(* connect is symmetric *)
(* there should be a shorter proof since move n is symmetric *)
Lemma connect_sym n : connect_sym (@move n).
Proof.
move=> c1 c2.
apply/connectP/connectP=> [] [p H1 H2].
exists (rev (belast c1 p)); first by rewrite H2 path_move_rev.
by case: (p) H2 => [->//|c3 p1 _]; rewrite rev_cons last_rcons.
exists (rev (belast c2 p)); first by rewrite H2 path_move_rev.
by case: (p) H2 => [->//|c3 p1 _]; rewrite rev_cons last_rcons.
Qed.
End GHanoi.
Arguments perfect {q n}.
Arguments cunliftr {q n}.
Notation " ↑[ c ]_ p" := (cliftr p c) (at level 5, format "↑[ c ]_ p").
Notation " ↓[ c ]" := (cunliftr c) (at level 5, format "↓[ c ]").
Notation "`c[ p ] " := (perfect p) (format "`c[ p ]", at level 5).
Notation "`c[ p , n ] " := ((perfect p) : configuration _ n)
(format "`c[ p , n ]", at level 5).
Lemma on_top1 k (d : disk 1) (c : configuration k 1) : on_top d c.
Proof. by apply/on_topP=> [] [] [] //=; case: d => [] []. Qed.
Lemma perfect_eqE n k (p1 p2 : peg k) : (`c[p1, n.+1] == `c[p2]) = (p1 == p2).
Proof.
apply/eqP/eqP => [|<-] // cp1Ecp2.
rewrite -(_ : `c[p1, n.+1] sdisk = p1); last by rewrite ffunE.
by rewrite cp1Ecp2 ffunE.
Qed.
Section PLift.
Variables n q : nat.
Variables i : disk q.+1.
Variable r1 : rel (disk q).
Variable r2 : rel (disk q.+1).
Let p := lift i.
Hypothesis r2Rr1 : forall i j, r2 (p i) (p j) = r1 i j.
Definition plift (c : configuration q n) : configuration q.+1 n :=
[ffun j => p (c j)].
Lemma plift_on_top c1 x : on_top x (plift c1) = on_top x c1.
Proof.
apply/on_topP/on_topP => H d; have := H d; rewrite !ffunE => H1 H2; apply: H1.
by rewrite H2.
by apply: lift_inj H2.
Qed.
Lemma plift_move (c1 c2 : configuration q n) :
move r2 (plift c1) (plift c2) = move r1 c1 c2.
Proof.
apply/moveP/moveP => [] [x [H1x H2x H3x H4x]]; exists x; split => //.
- by rewrite !ffunE in H1x; rewrite -r2Rr1.
- by move=> d2 xDd2; have := H2x _ xDd2; rewrite !ffunE => /lift_inj.
- by rewrite plift_on_top in H3x.
- by rewrite plift_on_top in H4x.
- by rewrite !ffunE r2Rr1.
- by move=> d2 /H2x; rewrite !ffunE => ->.
- by rewrite plift_on_top.
by rewrite plift_on_top.
Qed.
Lemma plift_path (c1 : configuration q n) cs :
path (move r2) (plift c1) [seq plift i | i <- cs] =
path (move r1) c1 cs.
Proof.
elim: cs c1 => //= c2 cs IH c1.
by rewrite plift_move IH.
Qed.
Lemma gdist_plift (c1 c2 : configuration q n) :
connect (move r1) c1 c2 ->
`d[plift c1, plift c2]_(move r2) <= `d[c1, c2]_(move r1).
Proof.