-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnightsTour.thy
3573 lines (3149 loc) · 151 KB
/
KnightsTour.thy
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
(* Author: Lukas Koller *)
theory KnightsTour
imports Main
begin
section \<open>Introduction and Definitions\<close>
text \<open>A Knight's path is a sequence of moves on a chessboard s.t. every step in sequence is a
valid move for a Knight and that the Knight visits every square on the boards exactly once.
A Knight is a chess figure that is only able to move two squares vertically and one square
horizontally or two squares horizontally and one square vertically. Finding a Knight's path is an
instance of the Hamiltonian Path Problem. A Knight's circuit is a Knight's path, where additionally
the Knight can move from the last square to the first square of the path, forming a loop.
@{cite "cull_decurtins_1987"} proves the existence of a Knight's path on a \<open>n\<times>m\<close>-board for
sufficiently large \<open>n\<close> and \<open>m\<close>. The main idea for the proof is to inductivly construct a Knight's
path for the \<open>n\<times>m\<close>-board from a few pre-computed Knight's paths for small boards, i.e. \<open>5\<times>5\<close>,
\<open>8\<times>6\<close>, ..., \<open>8\<times>9\<close>. The paths for small boards are transformed (i.e. transpose, mirror, translate)
and concatenated to create paths for larger boards.
While formalizing the proofs I discovered two mistakes in the original proof in
@{cite "cull_decurtins_1987"}: (i) the pre-computed path for the \<open>6\<times>6\<close>-board that ends in
the upper-left (in Figure 2) and (ii) the pre-computed path for the \<open>8\<times>8\<close>-board that ends in
the upper-left (in Figure 5) are incorrect. I.e. on the \<open>6\<times>6\<close>-board the Knight cannot step
from square 26 to square 27; in the \<open>8\<times>8\<close>-board the Knight cannot step from square 27 to
square 28. In this formalization I have replaced the two incorrect paths with correct paths.\<close>
text \<open>A square on a board is identified by its coordinates.\<close>
type_synonym square = "int \<times> int"
text \<open>A board is represented as a set of squares. Note, that this allows boards to have an
arbitrary shape and do not necessarily need to be rectangular.\<close>
type_synonym board = "square set"
text \<open>A (rectangular) \<open>(n\<times>m)\<close>-board is the set of all squares \<open>(i,j)\<close> where \<open>1 \<le> i \<le> n\<close>
and \<open>1 \<le> j \<le> m\<close>. \<open>(1,1)\<close> is the lower-left corner, and \<open>(n,m)\<close> is the upper-right corner.\<close>
definition board :: "nat \<Rightarrow> nat \<Rightarrow> board" where
"board n m = {(i,j) |i j. 1 \<le> i \<and> i \<le> int n \<and> 1 \<le> j \<and> j \<le> int m}"
text \<open>A path is a sequence of steps on a board. A path is represented by the list of visited
squares on the board. Each square on the \<open>(n\<times>m)\<close>-board is identified by its coordinates \<open>(i,j)\<close>.\<close>
type_synonym path = "square list"
text \<open>A Knight can only move two squares vertically and one square horizontally or two squares
horizontally and one square vertically. Thus, a knight at position \<open>(i,j)\<close> can only move
to \<open>(i\<plusminus>1,j\<plusminus>2)\<close> or \<open>(i\<plusminus>2,j\<plusminus>1)\<close>.\<close>
definition valid_step :: "square \<Rightarrow> square \<Rightarrow> bool" where
"valid_step s\<^sub>i s\<^sub>j \<equiv> (case s\<^sub>i of (i,j) \<Rightarrow> s\<^sub>j \<in> {(i+1,j+2),(i-1,j+2),(i+1,j-2),(i-1,j-2),
(i+2,j+1),(i-2,j+1),(i+2,j-1),(i-2,j-1)})"
text \<open>Now we define an inductive predicate that characterizes a Knight's path. A square \<open>s\<^sub>i\<close> can be
pre-pended to a current Knight's path \<open>s\<^sub>j#ps\<close> if (i) there is a valid step from the square \<open>s\<^sub>i\<close> to
the first square \<open>s\<^sub>j\<close> of the current path and (ii) the square \<open>s\<^sub>i\<close> has not been visited yet.\<close>
inductive knights_path :: "board \<Rightarrow> path \<Rightarrow> bool" where
"knights_path {s\<^sub>i} [s\<^sub>i]"
| "s\<^sub>i \<notin> b \<Longrightarrow> valid_step s\<^sub>i s\<^sub>j \<Longrightarrow> knights_path b (s\<^sub>j#ps) \<Longrightarrow> knights_path (b \<union> {s\<^sub>i}) (s\<^sub>i#s\<^sub>j#ps)"
code_pred knights_path .
text \<open>A sequence is a Knight's circuit iff the sequence if a Knight's path and there is a valid
step from the last square to the first square.\<close>
definition "knights_circuit b ps \<equiv> (knights_path b ps \<and> valid_step (last ps) (hd ps))"
section \<open>Executable Checker for a Knight's Path\<close>
text \<open>This section gives the implementation and correctness-proof for an executable checker for a
knights-path wrt. the definition @{const knights_path}.\<close>
subsection \<open>Implementation of an Executable Checker\<close>
fun row_exec :: "nat \<Rightarrow> int set" where
"row_exec 0 = {}"
| "row_exec m = insert (int m) (row_exec (m-1))"
fun board_exec_aux :: "nat \<Rightarrow> int set \<Rightarrow> board" where
"board_exec_aux 0 M = {}"
| "board_exec_aux k M = {(int k,j) |j. j \<in> M} \<union> board_exec_aux (k-1) M"
text \<open>Compute a board.\<close>
fun board_exec :: "nat \<Rightarrow> nat \<Rightarrow> board" where
"board_exec n m = board_exec_aux n (row_exec m)"
fun step_checker :: "square \<Rightarrow> square \<Rightarrow> bool" where
"step_checker (i,j) (i',j') =
((i+1,j+2) = (i',j') \<or> (i-1,j+2) = (i',j') \<or> (i+1,j-2) = (i',j') \<or> (i-1,j-2) = (i',j')
\<or> (i+2,j+1) = (i',j') \<or> (i-2,j+1) = (i',j') \<or> (i+2,j-1) = (i',j') \<or> (i-2,j-1) = (i',j'))"
fun path_checker :: "board \<Rightarrow> path \<Rightarrow> bool" where
"path_checker b [] = False"
| "path_checker b [s\<^sub>i] = ({s\<^sub>i} = b)"
| "path_checker b (s\<^sub>i#s\<^sub>j#ps) = (s\<^sub>i \<in> b \<and> step_checker s\<^sub>i s\<^sub>j \<and> path_checker (b - {s\<^sub>i}) (s\<^sub>j#ps))"
fun circuit_checker :: "board \<Rightarrow> path \<Rightarrow> bool" where
"circuit_checker b ps = (path_checker b ps \<and> step_checker (last ps) (hd ps))"
subsection \<open>Correctness Proof of the Executable Checker\<close>
lemma row_exec_leq: "j \<in> row_exec m \<longleftrightarrow> 1 \<le> j \<and> j \<le> int m"
by (induction m) auto
lemma board_exec_aux_leq_mem: "(i,j) \<in> board_exec_aux k M \<longleftrightarrow> 1 \<le> i \<and> i \<le> int k \<and> j \<in> M"
by (induction k M rule: board_exec_aux.induct) auto
lemma board_exec_leq: "(i,j) \<in> board_exec n m \<longleftrightarrow> 1 \<le> i \<and> i \<le> int n \<and> 1 \<le> j \<and> j \<le> int m"
using board_exec_aux_leq_mem row_exec_leq by auto
lemma board_exec_correct: "board n m = board_exec n m"
unfolding board_def using board_exec_leq by auto
lemma step_checker_correct: "step_checker s\<^sub>i s\<^sub>j \<longleftrightarrow> valid_step s\<^sub>i s\<^sub>j"
proof
assume "step_checker s\<^sub>i s\<^sub>j"
then show "valid_step s\<^sub>i s\<^sub>j"
unfolding valid_step_def
apply (cases s\<^sub>i)
apply (cases s\<^sub>j)
apply auto
done
next
assume assms: "valid_step s\<^sub>i s\<^sub>j"
then show "step_checker s\<^sub>i s\<^sub>j"
unfolding valid_step_def by auto
qed
lemma step_checker_rev: "step_checker (i,j) (i',j') \<Longrightarrow> step_checker (i',j') (i,j)"
apply (simp only: step_checker.simps)
by (elim disjE) auto
lemma knights_path_intro_rev:
assumes "s\<^sub>i \<in> b" "valid_step s\<^sub>i s\<^sub>j" "knights_path (b - {s\<^sub>i}) (s\<^sub>j#ps)"
shows "knights_path b (s\<^sub>i#s\<^sub>j#ps)"
using assms
proof -
assume assms: "s\<^sub>i \<in> b" "valid_step s\<^sub>i s\<^sub>j" "knights_path (b - {s\<^sub>i}) (s\<^sub>j#ps)"
then have "s\<^sub>i \<notin> (b - {s\<^sub>i})" "b - {s\<^sub>i} \<union> {s\<^sub>i} = b"
by auto
then show ?thesis
using assms knights_path.intros(2)[of s\<^sub>i "b - {s\<^sub>i}"] by auto
qed
text \<open>Final correctness corollary for the executable checker @{const path_checker}.\<close>
lemma path_checker_correct: "path_checker b ps \<longleftrightarrow> knights_path b ps"
proof
assume "path_checker b ps"
then show "knights_path b ps"
proof (induction rule: path_checker.induct)
case (3 s\<^sub>i s\<^sub>j xs b)
then show ?case using step_checker_correct knights_path_intro_rev by auto
qed (auto intro: knights_path.intros)
next
assume "knights_path b ps"
then show "path_checker b ps"
using step_checker_correct
by (induction rule: knights_path.induct) (auto elim: knights_path.cases)
qed
corollary knights_path_exec_simp: "knights_path (board n m) ps \<longleftrightarrow> path_checker (board_exec n m) ps"
using board_exec_correct path_checker_correct[symmetric] by simp
lemma circuit_checker_correct: "circuit_checker b ps \<longleftrightarrow> knights_circuit b ps"
unfolding knights_circuit_def using path_checker_correct step_checker_correct by auto
corollary knights_circuit_exec_simp:
"knights_circuit (board n m) ps \<longleftrightarrow> circuit_checker (board_exec n m) ps"
using board_exec_correct circuit_checker_correct[symmetric] by simp
section \<open>Basic Properties of @{const knights_path} and @{const knights_circuit}\<close>
lemma board_leq_subset: "n\<^sub>1 \<le> n\<^sub>2 \<and> m\<^sub>1 \<le> m\<^sub>2 \<Longrightarrow> board n\<^sub>1 m\<^sub>1 \<subseteq> board n\<^sub>2 m\<^sub>2"
unfolding board_def by auto
lemma finite_row_exec: "finite (row_exec m)"
by (induction m) auto
lemma finite_board_exec_aux: "finite M \<Longrightarrow> finite (board_exec_aux n M)"
by (induction n) auto
lemma board_finite: "finite (board n m)"
using finite_board_exec_aux finite_row_exec by (simp only: board_exec_correct) auto
lemma card_row_exec: "card (row_exec m) = m"
proof (induction m)
case (Suc m)
have "int (Suc m) \<notin> row_exec m"
using row_exec_leq by auto
then have "card (insert (int (Suc m)) (row_exec m)) = 1 + card (row_exec m)"
using card_Suc_eq by (metis Suc plus_1_eq_Suc row_exec.simps(1))
then have "card (row_exec (Suc m)) = 1 + card (row_exec m)"
by auto
then show ?case using Suc.IH by auto
qed auto
lemma set_comp_ins:
"{(k,j) |j. j \<in> insert x M} = insert (k,x) {(k,j) |j. j \<in> M}" (is "?Mi = ?iM")
proof
show "?Mi \<subseteq> ?iM"
proof
fix y assume "y \<in> ?Mi"
then obtain j where [simp]: "y = (k,j)" and "j \<in> insert x M" by blast
then have "j = x \<or> j \<in> M" by auto
then show "y \<in> ?iM" by (elim disjE) auto
qed
next
show "?iM \<subseteq> ?Mi"
proof
fix y assume "y \<in> ?iM"
then obtain j where [simp]: "y = (k,j)" and "j \<in> insert x M" by blast
then have "j = x \<or> j \<in> M" by auto
then show "y \<in> ?Mi" by (elim disjE) auto
qed
qed
lemma finite_card_set_comp: "finite M \<Longrightarrow> card {(k,j) |j. j \<in> M} = card M"
proof (induction M rule: finite_induct)
case (insert x M)
then show ?case using set_comp_ins[of k x M] by auto
qed auto
lemma card_board_exec_aux: "finite M \<Longrightarrow> card (board_exec_aux k M) = k * card M"
proof (induction k)
case (Suc k)
let ?M'="{(int (Suc k),j) |j. j \<in> M}"
let ?rec_k="board_exec_aux k M"
have finite: "finite ?M'" "finite ?rec_k"
using Suc finite_board_exec_aux by auto
then have card_Un_simp: "card (?M' \<union> ?rec_k) = card ?M' + card ?rec_k"
using board_exec_aux_leq_mem card_Un_Int[of ?M' ?rec_k] by auto
have card_M: "card ?M' = card M"
using Suc finite_card_set_comp by auto
have "card (board_exec_aux (Suc k) M) = card ?M' + card ?rec_k"
using card_Un_simp by auto
also have "... = card M + k * card M"
using Suc card_M by auto
also have "... = (Suc k) * card M"
by auto
finally show ?case .
qed auto
lemma card_board: "card (board n m) = n * m"
proof -
have "card (board n m) = card (board_exec_aux n (row_exec m))"
using board_exec_correct by auto
also have "... = n * m"
using card_row_exec card_board_exec_aux finite_row_exec by auto
finally show ?thesis .
qed
lemma knights_path_board_non_empty: "knights_path b ps \<Longrightarrow> b \<noteq> {}"
by (induction arbitrary: ps rule: knights_path.induct) auto
lemma knights_path_board_m_n_geq_1: "knights_path (board n m) ps \<Longrightarrow> min n m \<ge> 1"
unfolding board_def using knights_path_board_non_empty by fastforce
lemma knights_path_non_nil: "knights_path b ps \<Longrightarrow> ps \<noteq> []"
by (induction arbitrary: b rule: knights_path.induct) auto
lemma knights_path_set_eq: "knights_path b ps \<Longrightarrow> set ps = b"
by (induction rule: knights_path.induct) auto
lemma knights_path_subset:
"knights_path b\<^sub>1 ps\<^sub>1 \<Longrightarrow> knights_path b\<^sub>2 ps\<^sub>2 \<Longrightarrow> set ps\<^sub>1 \<subseteq> set ps\<^sub>2 \<longleftrightarrow> b\<^sub>1 \<subseteq> b\<^sub>2"
using knights_path_set_eq by auto
lemma knights_path_board_unique: "knights_path b\<^sub>1 ps \<Longrightarrow> knights_path b\<^sub>2 ps \<Longrightarrow> b\<^sub>1 = b\<^sub>2"
using knights_path_set_eq by auto
lemma valid_step_neq: "valid_step s\<^sub>i s\<^sub>j \<Longrightarrow> s\<^sub>i \<noteq> s\<^sub>j"
unfolding valid_step_def by auto
lemma valid_step_non_transitive: "valid_step s\<^sub>i s\<^sub>j \<Longrightarrow> valid_step s\<^sub>j s\<^sub>k \<Longrightarrow> \<not>valid_step s\<^sub>i s\<^sub>k"
proof -
assume assms: "valid_step s\<^sub>i s\<^sub>j" "valid_step s\<^sub>j s\<^sub>k"
obtain i\<^sub>i j\<^sub>i i\<^sub>j j\<^sub>j i\<^sub>k j\<^sub>k where [simp]: "s\<^sub>i = (i\<^sub>i,j\<^sub>i)" "s\<^sub>j = (i\<^sub>j,j\<^sub>j)" "s\<^sub>k = (i\<^sub>k,j\<^sub>k)" by force
then have "step_checker (i\<^sub>i,j\<^sub>i) (i\<^sub>j,j\<^sub>j)" "step_checker (i\<^sub>j,j\<^sub>j) (i\<^sub>k,j\<^sub>k)"
using assms step_checker_correct by auto
then show "\<not>valid_step s\<^sub>i s\<^sub>k"
apply (simp add: step_checker_correct[symmetric])
apply (elim disjE)
apply auto
done
qed
lemma knights_path_distinct: "knights_path b ps \<Longrightarrow> distinct ps"
proof (induction rule: knights_path.induct)
case (2 s\<^sub>i b s\<^sub>j ps)
then have "s\<^sub>i \<notin> set (s\<^sub>j # ps)"
using knights_path_set_eq valid_step_neq by blast
then show ?case using 2 by auto
qed auto
lemma knights_path_length: "knights_path b ps \<Longrightarrow> length ps = card b"
using knights_path_set_eq knights_path_distinct by (metis distinct_card)
lemma knights_path_take:
assumes "knights_path b ps" "0 < k" "k < length ps"
shows "knights_path (set (take k ps)) (take k ps)"
using assms
proof (induction arbitrary: k rule: knights_path.induct)
case (2 s\<^sub>i b s\<^sub>j ps)
then have "k = 1 \<or> k = 2 \<or> 2 < k" by force
then show ?case
using 2
proof (elim disjE)
assume "k = 2"
then have "take k (s\<^sub>i#s\<^sub>j#ps) = [s\<^sub>i,s\<^sub>j]" "s\<^sub>i \<notin> {s\<^sub>j}" using 2 valid_step_neq by auto
then show ?thesis using 2 knights_path.intros by auto
next
assume "2 < k"
then have k_simps: "k-2 = k-1-1" "0 < k-2" "k-2 < length ps" and
take_simp1: "take k (s\<^sub>i#s\<^sub>j#ps) = s\<^sub>i#take (k-1) (s\<^sub>j#ps)" and
take_simp2: "take k (s\<^sub>i#s\<^sub>j#ps) = s\<^sub>i#s\<^sub>j#take (k-1-1) ps"
using assms 2 take_Cons'[of k s\<^sub>i "s\<^sub>j#ps"] take_Cons'[of "k-1" s\<^sub>j ps] by auto
then have "knights_path (set (take (k-1) (s\<^sub>j#ps))) (take (k-1) (s\<^sub>j#ps))"
using 2 k_simps by auto
then have kp: "knights_path (set (take (k-1) (s\<^sub>j#ps))) (s\<^sub>j#take (k-2) ps)"
using take_Cons'[of "k-1" s\<^sub>j ps] by (auto simp: k_simps elim: knights_path.cases)
have no_mem: "s\<^sub>i \<notin> set (take (k-1) (s\<^sub>j#ps))"
using 2 set_take_subset[of "k-1" "s\<^sub>j#ps"] knights_path_set_eq by blast
have "knights_path (set (take (k-1) (s\<^sub>j#ps)) \<union> {s\<^sub>i}) (s\<^sub>i#s\<^sub>j#take (k-2) ps)"
using knights_path.intros(2)[OF no_mem \<open>valid_step s\<^sub>i s\<^sub>j\<close> kp] by auto
then show ?thesis using k_simps take_simp2 knights_path_set_eq by metis
qed (auto intro: knights_path.intros)
qed auto
lemma knights_path_drop:
assumes "knights_path b ps" "0 < k" "k < length ps"
shows "knights_path (set (drop k ps)) (drop k ps)"
using assms
proof (induction arbitrary: k rule: knights_path.induct)
case (2 s\<^sub>i b s\<^sub>j ps)
then have "(k = 1 \<and> ps = []) \<or> (k = 1 \<and> ps \<noteq> []) \<or> 1 < k" by force
then show ?case
using 2
proof (elim disjE)
assume "k = 1 \<and> ps \<noteq> []"
then show ?thesis using 2 knights_path_set_eq by force
next
assume "1 < k"
then have "0 < k-1" "k-1 < length (s\<^sub>j#ps)" "drop k (s\<^sub>i#s\<^sub>j#ps) = drop (k-1) (s\<^sub>j#ps)"
using assms 2 drop_Cons'[of k s\<^sub>i "s\<^sub>j#ps"] by auto
then show ?thesis
using 2 by auto
qed (auto intro: knights_path.intros)
qed auto
text \<open>A Knight's path can be split to form two new disjoint Knight's paths.\<close>
corollary knights_path_split:
assumes "knights_path b ps" "0 < k" "k < length ps"
shows
"\<exists>b\<^sub>1 b\<^sub>2. knights_path b\<^sub>1 (take k ps) \<and> knights_path b\<^sub>2 (drop k ps) \<and> b\<^sub>1 \<union> b\<^sub>2 = b \<and> b\<^sub>1 \<inter> b\<^sub>2 = {}"
using assms
proof -
let ?b\<^sub>1="set (take k ps)"
let ?b\<^sub>2="set (drop k ps)"
have kp1: "knights_path ?b\<^sub>1 (take k ps)" and kp2: "knights_path ?b\<^sub>2 (drop k ps)"
using assms knights_path_take knights_path_drop by auto
have union: "?b\<^sub>1 \<union> ?b\<^sub>2 = b"
using assms knights_path_set_eq by (metis append_take_drop_id set_append)
have inter: "?b\<^sub>1 \<inter> ?b\<^sub>2 = {}"
using assms knights_path_distinct by (metis append_take_drop_id distinct_append)
show ?thesis using kp1 kp2 union inter by auto
qed
text \<open>Append two disjoint Knight's paths.\<close>
corollary knights_path_append:
assumes "knights_path b\<^sub>1 ps\<^sub>1" "knights_path b\<^sub>2 ps\<^sub>2" "b\<^sub>1 \<inter> b\<^sub>2 = {}" "valid_step (last ps\<^sub>1) (hd ps\<^sub>2)"
shows "knights_path (b\<^sub>1 \<union> b\<^sub>2) (ps\<^sub>1 @ ps\<^sub>2)"
using assms
proof (induction arbitrary: ps\<^sub>2 b\<^sub>2 rule: knights_path.induct)
case (1 s\<^sub>i)
then have "s\<^sub>i \<notin> b\<^sub>2" "ps\<^sub>2 \<noteq> []" "valid_step s\<^sub>i (hd ps\<^sub>2)" "knights_path b\<^sub>2 (hd ps\<^sub>2#tl ps\<^sub>2)"
using knights_path_non_nil by auto
then have "knights_path (b\<^sub>2 \<union> {s\<^sub>i}) (s\<^sub>i#hd ps\<^sub>2#tl ps\<^sub>2)"
using knights_path.intros by blast
then show ?case using \<open>ps\<^sub>2 \<noteq> []\<close> by auto
next
case (2 s\<^sub>i b\<^sub>1 s\<^sub>j ps\<^sub>1)
then have "s\<^sub>i \<notin> b\<^sub>1 \<union> b\<^sub>2" "valid_step s\<^sub>i s\<^sub>j" "knights_path (b\<^sub>1 \<union> b\<^sub>2) (s\<^sub>j#ps\<^sub>1@ps\<^sub>2)" by auto
then have "knights_path (b\<^sub>1 \<union> b\<^sub>2 \<union> {s\<^sub>i}) (s\<^sub>i#s\<^sub>j#ps\<^sub>1@ps\<^sub>2)"
using knights_path.intros by auto
then show ?case by auto
qed
lemma valid_step_rev: "valid_step s\<^sub>i s\<^sub>j \<Longrightarrow> valid_step s\<^sub>j s\<^sub>i"
using step_checker_correct step_checker_rev by (metis prod.exhaust_sel)
text \<open>Reverse a Knight's path.\<close>
corollary knights_path_rev:
assumes "knights_path b ps"
shows "knights_path b (rev ps)"
using assms
proof (induction rule: knights_path.induct)
case (2 s\<^sub>i b s\<^sub>j ps)
then have "knights_path {s\<^sub>i} [s\<^sub>i]" "b \<inter> {s\<^sub>i} = {}" "valid_step (last (rev (s\<^sub>j # ps))) (hd [s\<^sub>i])"
using valid_step_rev by (auto intro: knights_path.intros)
then have "knights_path (b \<union> {s\<^sub>i}) ((rev (s\<^sub>j#ps))@[s\<^sub>i])"
using 2 knights_path_append by blast
then show ?case by auto
qed (auto intro: knights_path.intros)
text \<open>Reverse a Knight's circuit.\<close>
corollary knights_circuit_rev:
assumes "knights_circuit b ps"
shows "knights_circuit b (rev ps)"
using assms knights_path_rev valid_step_rev
unfolding knights_circuit_def by (auto simp: hd_rev last_rev)
(* Function to rotate a Knight's circuit to start with (1,1),(3,2),... *)
(* fun rot_c_acc :: "path \<Rightarrow> path \<Rightarrow> path" where
"rot_c_acc (s\<^sub>i#s\<^sub>j#ps) acc =
(if s\<^sub>i = (1,1) then
if s\<^sub>j = (3,2) then s\<^sub>i#rev (s\<^sub>j#ps@acc) else s\<^sub>i#s\<^sub>j#ps@acc
else rot_c_acc (s\<^sub>j#ps) (s\<^sub>i#acc))"
| "rot_c_acc _ acc = []"
definition "rot_c ps \<equiv> rot_c_acc ps []" *)
lemma knights_circuit_rotate1:
assumes "knights_circuit b (s\<^sub>i#ps)"
shows "knights_circuit b (ps@[s\<^sub>i])"
proof (cases "ps = []")
case True
then show ?thesis using assms by auto
next
case False
have kp1: "knights_path b (s\<^sub>i#ps)" "valid_step (last (s\<^sub>i#ps)) (hd (s\<^sub>i#ps))"
using assms unfolding knights_circuit_def by auto
then have kp_elim: "s\<^sub>i \<notin> (b - {s\<^sub>i})" "valid_step s\<^sub>i (hd ps)" "knights_path (b - {s\<^sub>i}) ps"
using \<open>ps \<noteq> []\<close> by (auto elim: knights_path.cases)
then have vs': "valid_step (last (ps@[s\<^sub>i])) (hd (ps@[s\<^sub>i]))"
using \<open>ps \<noteq> []\<close> valid_step_rev by auto
have kp2: "knights_path {s\<^sub>i} [s\<^sub>i]" "(b - {s\<^sub>i}) \<inter> {s\<^sub>i} = {}"
by (auto intro: knights_path.intros)
have vs: "valid_step (last ps) (hd [s\<^sub>i])"
using \<open>ps \<noteq> []\<close> \<open>valid_step (last (s\<^sub>i#ps)) (hd (s\<^sub>i#ps))\<close> by auto
have "(b - {s\<^sub>i}) \<union> {s\<^sub>i} = b"
using kp1 kp_elim knights_path_set_eq by force
then show ?thesis
unfolding knights_circuit_def
using vs knights_path_append[OF \<open>knights_path (b - {s\<^sub>i}) ps\<close> kp2] vs' by auto
qed
text \<open>A Knight's circuit can be rotated to start at any square on the board.\<close>
lemma knights_circuit_rotate_to:
assumes "knights_circuit b ps" "hd (drop k ps) = s\<^sub>i" "k < length ps"
shows "\<exists>ps'. knights_circuit b ps' \<and> hd ps' = s\<^sub>i"
using assms
proof (induction k arbitrary: b ps)
case (Suc k)
let ?s\<^sub>j="hd ps"
let ?ps'="tl ps"
show ?case
proof (cases "s\<^sub>i = ?s\<^sub>j")
case True
then show ?thesis using Suc by auto
next
case False
then have "?ps' \<noteq> []"
using Suc by (metis drop_Nil drop_Suc drop_eq_Nil2 le_antisym nat_less_le)
then have "knights_circuit b (?s\<^sub>j#?ps')"
using Suc by (metis list.exhaust_sel tl_Nil)
then have "knights_circuit b (?ps'@[?s\<^sub>j])" "hd (drop k (?ps'@[?s\<^sub>j])) = s\<^sub>i"
using Suc knights_circuit_rotate1 by (auto simp: drop_Suc)
then show ?thesis using Suc by auto
qed
qed auto
text \<open>For positive boards (1,1) can only have (2,3) and (3,2) as a neighbour.\<close>
lemma valid_step_1_1:
assumes "valid_step (1,1) (i,j)" "i > 0" "j > 0"
shows "(i,j) = (2,3) \<or> (i,j) = (3,2)"
using assms unfolding valid_step_def by auto
lemma list_len_g_1_split: "length xs > 1 \<Longrightarrow> \<exists>x\<^sub>1 x\<^sub>2 xs'. xs = x\<^sub>1#x\<^sub>2#xs'"
proof (induction xs)
case (Cons x xs)
then have "length xs > 0" by auto
then have "length xs \<ge> 1" by presburger
then have "length xs = 1 \<or> length xs > 1" by auto
then show ?case
proof (elim disjE)
assume "length xs = 1"
then obtain x\<^sub>1 where [simp]: "xs = [x\<^sub>1]"
using length_Suc_conv[of xs 0] by auto
then show ?thesis by auto
next
assume "1 < length xs"
then show ?thesis using Cons by auto
qed
qed auto
lemma list_len_g_3_split: "length xs > 3 \<Longrightarrow> \<exists>x\<^sub>1 x\<^sub>2 xs' x\<^sub>3. xs = x\<^sub>1#x\<^sub>2#xs'@[x\<^sub>3]"
proof (induction xs)
case (Cons x xs)
then have "length xs = 3 \<or> length xs > 3" by auto
then show ?case
proof (elim disjE)
assume "length xs = 3"
then obtain x\<^sub>1 xs\<^sub>1 where [simp]: "xs = x\<^sub>1#xs\<^sub>1" "length xs\<^sub>1 = 2"
using length_Suc_conv[of xs 2] by auto
then obtain x\<^sub>2 xs\<^sub>2 where [simp]: "xs\<^sub>1 = x\<^sub>2#xs\<^sub>2" "length xs\<^sub>2 = 1"
using length_Suc_conv[of xs\<^sub>1 1] by auto
then obtain x\<^sub>3 where [simp]: "xs\<^sub>2 = [x\<^sub>3]"
using length_Suc_conv[of xs\<^sub>2 0] by auto
then show ?thesis by auto
next
assume "length xs > 3"
then show ?thesis using Cons by auto
qed
qed auto
text \<open>Any Knight's circuit on a positive board can be rotated to start with (1,1) and
end with (3,2).\<close>
corollary rotate_knights_circuit:
assumes "knights_circuit (board n m) ps" "min n m \<ge> 5"
shows "\<exists>ps. knights_circuit (board n m) ps \<and> hd ps = (1,1) \<and> last ps = (3,2)"
using assms
proof -
let ?b="board n m"
have "knights_path ?b ps"
using assms unfolding knights_circuit_def by auto
then have "(1,1) \<in> set ps"
using assms knights_path_set_eq by (auto simp: board_def)
then obtain k where "hd (drop k ps) = (1,1)" "k < length ps"
by (metis hd_drop_conv_nth in_set_conv_nth)
then obtain ps\<^sub>r where ps\<^sub>r_prems: "knights_circuit ?b ps\<^sub>r" "hd ps\<^sub>r = (1,1)"
using assms knights_circuit_rotate_to by blast
then have kp: "knights_path ?b ps\<^sub>r" and "valid_step (last ps\<^sub>r) (1,1)"
unfolding knights_circuit_def by auto
have "(1,1) \<in> ?b" "(1,2) \<in> ?b" "(1,3) \<in> ?b"
using assms unfolding board_def by auto
then have "(1,1) \<in> set ps\<^sub>r" "(1,2) \<in> set ps\<^sub>r" "(1,3) \<in> set ps\<^sub>r"
using kp knights_path_set_eq by auto
have "3 < card ?b"
using assms board_leq_subset card_board[of 5 5]
card_mono[OF board_finite[of n m], of "board 5 5"] by auto
then have "3 < length ps\<^sub>r"
using knights_path_length kp by auto
then obtain s\<^sub>j ps' s\<^sub>k where [simp]: "ps\<^sub>r = (1,1)#s\<^sub>j#ps'@[s\<^sub>k]"
using \<open>hd ps\<^sub>r = (1,1)\<close> list_len_g_3_split[of ps\<^sub>r] by auto
have "s\<^sub>j \<noteq> s\<^sub>k"
using kp knights_path_distinct by force
have vs_s\<^sub>k: "valid_step s\<^sub>k (1,1)"
using \<open>valid_step (last ps\<^sub>r) (1,1)\<close> by simp
have vs_s\<^sub>j: "valid_step (1,1) s\<^sub>j" and kp': "knights_path (?b - {(1,1)}) (s\<^sub>j#ps'@[s\<^sub>k])"
using kp by (auto elim: knights_path.cases)
have "s\<^sub>j \<in> set ps\<^sub>r" "s\<^sub>k \<in> set ps\<^sub>r" by auto
then have "s\<^sub>j \<in> ?b" "s\<^sub>k \<in> ?b"
using kp knights_path_set_eq by blast+
then have "0 < fst s\<^sub>j \<and> 0 < snd s\<^sub>j" "0 < fst s\<^sub>k \<and> 0 < snd s\<^sub>k"
unfolding board_def by auto
then have "s\<^sub>k = (2,3) \<or> s\<^sub>k = (3,2)" "s\<^sub>j = (2,3) \<or> s\<^sub>j = (3,2)"
using vs_s\<^sub>k vs_s\<^sub>j valid_step_1_1 valid_step_rev by (metis prod.collapse)+
then have "s\<^sub>k = (3,2) \<or> s\<^sub>j = (3,2)"
using \<open>s\<^sub>j \<noteq> s\<^sub>k\<close> by auto
then show ?thesis
proof (elim disjE)
assume "s\<^sub>k = (3,2)"
then have "last ps\<^sub>r = (3,2)" by auto
then show ?thesis using ps\<^sub>r_prems by auto
next
assume "s\<^sub>j = (3,2)"
then have vs: "valid_step (last ((1,1)#rev (s\<^sub>j#ps'@[s\<^sub>k]))) (hd ((1,1)#rev (s\<^sub>j#ps'@[s\<^sub>k])))"
unfolding valid_step_def by auto
have rev_simp: "rev (s\<^sub>j#ps'@[s\<^sub>k]) = s\<^sub>k#(rev ps')@[s\<^sub>j]" by auto
have "knights_path (?b - {(1,1)}) (rev (s\<^sub>j#ps'@[s\<^sub>k]))"
using knights_path_rev[OF kp'] by auto
then have "(1,1) \<notin> (?b - {(1,1)})" "valid_step (1,1) s\<^sub>k"
"knights_path (?b - {(1,1)}) (s\<^sub>k#(rev ps')@[s\<^sub>j])"
using assms vs_s\<^sub>k valid_step_rev by (auto simp: rev_simp)
then have "knights_path (?b - {(1, 1)} \<union> {(1, 1)}) ((1,1)#s\<^sub>k#(rev ps')@[s\<^sub>j])"
using knights_path.intros(2)[of "(1,1)" "?b - {(1,1)}" s\<^sub>k "(rev ps')@[s\<^sub>j]"] by auto
then have "knights_path ?b ((1,1)#rev (s\<^sub>j#ps'@[s\<^sub>k]))"
using assms by (simp add: board_def insert_absorb rev_simp)
then have "knights_circuit ?b ((1,1)#rev (s\<^sub>j#ps'@[s\<^sub>k]))"
unfolding knights_circuit_def using vs by auto
then show ?thesis
using \<open>s\<^sub>j = (3,2)\<close> by auto
qed
qed
section \<open>Transposing Paths and Boards\<close>
subsection \<open>Implementation of Path and Board Transposition\<close>
definition "transpose_square s\<^sub>i = (case s\<^sub>i of (i,j) \<Rightarrow> (j,i))"
fun transpose :: "path \<Rightarrow> path" where
"transpose [] = []"
| "transpose (s\<^sub>i#ps) = (transpose_square s\<^sub>i)#transpose ps"
definition transpose_board :: "board \<Rightarrow> board" where
"transpose_board b \<equiv> {(j,i) |i j. (i,j) \<in> b}"
subsection \<open>Correctness of Path and Board Transposition\<close>
lemma transpose2: "transpose_square (transpose_square s\<^sub>i) = s\<^sub>i"
unfolding transpose_square_def by (auto split: prod.splits)
lemma transpose_nil: "ps = [] \<longleftrightarrow> transpose ps = []"
using transpose.elims by blast
lemma transpose_length: "length ps = length (transpose ps)"
by (induction ps) auto
lemma hd_transpose: "ps \<noteq>[] \<Longrightarrow> hd (transpose ps) = transpose_square (hd ps)"
by (induction ps) (auto simp: transpose_square_def)
lemma last_transpose: "ps \<noteq>[] \<Longrightarrow> last (transpose ps) = transpose_square (last ps)"
proof (induction ps)
case (Cons s\<^sub>i ps)
then show ?case
proof (cases "ps = []")
case True
then show ?thesis using Cons by (auto simp: transpose_square_def)
next
case False
then show ?thesis using Cons transpose_nil by auto
qed
qed auto
lemma take_transpose:
shows "take k (transpose ps) = transpose (take k ps)"
proof (induction ps arbitrary: k)
case Nil
then show ?case by auto
next
case (Cons s\<^sub>i ps)
then obtain i j where "s\<^sub>i = (i,j)" by force
then have "k = 0 \<or> k > 0" by auto
then show ?case
proof (elim disjE)
assume "k > 0"
then show ?thesis using Cons.IH by (auto simp: \<open>s\<^sub>i = (i,j)\<close> take_Cons')
qed auto
qed
lemma drop_transpose:
shows "drop k (transpose ps) = transpose (drop k ps)"
proof (induction ps arbitrary: k)
case Nil
then show ?case by auto
next
case (Cons s\<^sub>i ps)
then obtain i j where "s\<^sub>i = (i,j)" by force
then have "k = 0 \<or> k > 0" by auto
then show ?case
proof (elim disjE)
assume "k > 0"
then show ?thesis using Cons.IH by (auto simp: \<open>s\<^sub>i = (i,j)\<close> drop_Cons')
qed auto
qed
lemma transpose_board_correct: "s\<^sub>i \<in> b \<longleftrightarrow> (transpose_square s\<^sub>i) \<in> transpose_board b"
unfolding transpose_board_def transpose_square_def by (auto split: prod.splits)
lemma transpose_board: "transpose_board (board n m) = board m n"
unfolding board_def using transpose_board_correct by (auto simp: transpose_square_def)
lemma insert_transpose_board:
"insert (transpose_square s\<^sub>i) (transpose_board b) = transpose_board (insert s\<^sub>i b)"
unfolding transpose_board_def transpose_square_def by (auto split: prod.splits)
lemma transpose_board2: "transpose_board (transpose_board b) = b"
unfolding transpose_board_def by auto
lemma transpose_union: "transpose_board (b\<^sub>1 \<union> b\<^sub>2) = transpose_board b\<^sub>1 \<union> transpose_board b\<^sub>2"
unfolding transpose_board_def by auto
lemma transpose_valid_step:
"valid_step s\<^sub>i s\<^sub>j \<longleftrightarrow> valid_step (transpose_square s\<^sub>i) (transpose_square s\<^sub>j)"
unfolding valid_step_def transpose_square_def by (auto split: prod.splits)
lemma transpose_knights_path':
assumes "knights_path b ps"
shows "knights_path (transpose_board b) (transpose ps)"
using assms
proof (induction rule: knights_path.induct)
case (1 s\<^sub>i)
then have "transpose_board {s\<^sub>i} = {transpose_square s\<^sub>i}" "transpose [s\<^sub>i] = [transpose_square s\<^sub>i]"
using transpose_board_correct by (auto simp: transpose_square_def split: prod.splits)
then show ?case by (auto intro: knights_path.intros)
next
case (2 s\<^sub>i b s\<^sub>j ps)
then have prems: "transpose_square s\<^sub>i \<notin> transpose_board b"
"valid_step (transpose_square s\<^sub>i) (transpose_square s\<^sub>j)"
and "transpose (s\<^sub>j#ps) = transpose_square s\<^sub>j#transpose ps"
using 2 transpose_board_correct transpose_valid_step by auto
then show ?case
using 2 knights_path.intros(2)[OF prems] insert_transpose_board by auto
qed
corollary transpose_knights_path:
assumes "knights_path (board n m) ps"
shows "knights_path (board m n) (transpose ps)"
using assms transpose_knights_path'[of "board n m" ps] by (auto simp: transpose_board)
corollary transpose_knights_circuit:
assumes "knights_circuit (board n m) ps"
shows "knights_circuit (board m n) (transpose ps)"
using assms
proof -
have "knights_path (board n m) ps" and vs: "valid_step (last ps) (hd ps)"
using assms unfolding knights_circuit_def by auto
then have kp_t: "knights_path (board m n) (transpose ps)" and "ps \<noteq> []"
using transpose_knights_path knights_path_non_nil by auto
then have "valid_step (last (transpose ps)) (hd (transpose ps))"
using vs hd_transpose last_transpose transpose_valid_step by auto
then show ?thesis using kp_t by (auto simp: knights_circuit_def)
qed
section \<open>Mirroring Paths and Boards\<close>
subsection \<open>Implementation of Path and Board Mirroring\<close>
abbreviation "min1 ps \<equiv> Min ((fst) ` set ps)"
abbreviation "max1 ps \<equiv> Max ((fst) ` set ps)"
abbreviation "min2 ps \<equiv> Min ((snd) ` set ps)"
abbreviation "max2 ps \<equiv> Max ((snd) ` set ps)"
definition mirror1_square :: "int \<Rightarrow> square \<Rightarrow> square" where
"mirror1_square n s\<^sub>i = (case s\<^sub>i of (i,j) \<Rightarrow> (n-i,j))"
fun mirror1_aux :: "int \<Rightarrow> path \<Rightarrow> path" where
"mirror1_aux n [] = []"
| "mirror1_aux n (s\<^sub>i#ps) = (mirror1_square n s\<^sub>i)#mirror1_aux n ps"
definition "mirror1 ps = mirror1_aux (max1 ps + min1 ps) ps"
definition mirror1_board :: "int \<Rightarrow> board \<Rightarrow> board" where
"mirror1_board n b \<equiv> {mirror1_square n s\<^sub>i |s\<^sub>i. s\<^sub>i \<in> b}"
definition mirror2_square :: "int \<Rightarrow> square \<Rightarrow> square" where
"mirror2_square m s\<^sub>i = (case s\<^sub>i of (i,j) \<Rightarrow> (i,m-j))"
fun mirror2_aux :: "int \<Rightarrow> path \<Rightarrow> path" where
"mirror2_aux m [] = []"
| "mirror2_aux m (s\<^sub>i#ps) = (mirror2_square m s\<^sub>i)#mirror2_aux m ps"
definition "mirror2 ps = mirror2_aux (max2 ps + min2 ps) ps"
definition mirror2_board :: "int \<Rightarrow> board \<Rightarrow> board" where
"mirror2_board m b \<equiv> {mirror2_square m s\<^sub>i |s\<^sub>i. s\<^sub>i \<in> b}"
subsection \<open>Correctness of Path and Board Mirroring\<close>
lemma mirror1_board_id: "mirror1_board (int n+1) (board n m) = board n m" (is "_ = ?b")
proof
show "mirror1_board (int n+1) ?b \<subseteq> ?b"
proof
fix s\<^sub>i'
assume assms: "s\<^sub>i' \<in> mirror1_board (int n+1) ?b"
then obtain i' j' where [simp]: "s\<^sub>i' = (i',j')" by force
then have "(i',j') \<in> mirror1_board (int n+1) ?b"
using assms by auto
then obtain i j where "(i,j) \<in> ?b" "mirror1_square (int n+1) (i,j) = (i',j')"
unfolding mirror1_board_def by auto
then have "1 \<le> i \<and> i \<le> int n" "1 \<le> j \<and> j \<le> int m" "i'=(int n+1)-i" "j'=j"
unfolding board_def mirror1_square_def by auto
then have "1 \<le> i' \<and> i' \<le> int n" "1 \<le> j' \<and> j' \<le> int m"
by auto
then show "s\<^sub>i' \<in> ?b"
unfolding board_def by auto
qed
next
show "?b \<subseteq> mirror1_board (int n+1) ?b"
proof
fix s\<^sub>i
assume assms: "s\<^sub>i \<in> ?b"
then obtain i j where [simp]: "s\<^sub>i = (i,j)" by force
then have "(i,j) \<in> ?b"
using assms by auto
then have "1 \<le> i \<and> i \<le> int n" "1 \<le> j \<and> j \<le> int m"
unfolding board_def by auto
then obtain i' j' where "i'=(int n+1)-i" "j'=j" by auto
then have "(i',j') \<in> ?b" "mirror1_square (int n+1) (i',j') = (i,j)"
using \<open>1 \<le> i \<and> i \<le> int n\<close> \<open>1 \<le> j \<and> j \<le> int m\<close>
unfolding mirror1_square_def by (auto simp: board_def)
then show "s\<^sub>i \<in> mirror1_board (int n+1) ?b"
unfolding mirror1_board_def by force
qed
qed
lemma mirror2_board_id: "mirror2_board (int m+1) (board n m) = board n m" (is "_ = ?b")
proof
show "mirror2_board (int m+1) ?b \<subseteq> ?b"
proof
fix s\<^sub>i'
assume assms: "s\<^sub>i' \<in> mirror2_board (int m+1) ?b"
then obtain i' j' where [simp]: "s\<^sub>i' = (i',j')" by force
then have "(i',j') \<in> mirror2_board (int m+1) ?b"
using assms by auto
then obtain i j where "(i,j) \<in> ?b" "mirror2_square (int m+1) (i,j) = (i',j')"
unfolding mirror2_board_def by auto
then have "1 \<le> i \<and> i \<le> int n" "1 \<le> j \<and> j \<le> int m" "i'=i" "j'=(int m+1)-j"
unfolding board_def mirror2_square_def by auto
then have "1 \<le> i' \<and> i' \<le> int n" "1 \<le> j' \<and> j' \<le> int m"
by auto
then show "s\<^sub>i' \<in> ?b"
unfolding board_def by auto
qed
next
show "?b \<subseteq> mirror2_board (int m+1) ?b"
proof
fix s\<^sub>i
assume assms: "s\<^sub>i \<in> ?b"
then obtain i j where [simp]: "s\<^sub>i = (i,j)" by force
then have "(i,j) \<in> ?b"
using assms by auto
then have "1 \<le> i \<and> i \<le> int n" "1 \<le> j \<and> j \<le> int m"
unfolding board_def by auto
then obtain i' j' where "i'=i" "j'=(int m+1)-j" by auto
then have "(i',j') \<in> ?b" "mirror2_square (int m+1) (i',j') = (i,j)"
using \<open>1 \<le> i \<and> i \<le> int n\<close> \<open>1 \<le> j \<and> j \<le> int m\<close>
unfolding mirror2_square_def by (auto simp: board_def)
then show "s\<^sub>i \<in> mirror2_board (int m+1) ?b"
unfolding mirror2_board_def by force
qed
qed
lemma knights_path_min1: "knights_path (board n m) ps \<Longrightarrow> min1 ps = 1"
proof -
assume assms: "knights_path (board n m) ps"
then have "min n m \<ge> 1"
using knights_path_board_m_n_geq_1 by auto
then have "(1,1) \<in> board n m" and ge_1: "\<forall>(i,j) \<in> board n m. i \<ge> 1"
unfolding board_def by auto
then have finite: "finite ((fst) ` board n m)" and
non_empty: "(fst) ` board n m \<noteq> {}" and
mem_1: "1 \<in> (fst) ` board n m"
using board_finite by auto (metis fstI image_eqI)
then have "Min ((fst) ` board n m) = 1"
using ge_1 by (auto simp: Min_eq_iff)
then show ?thesis
using assms knights_path_set_eq by auto
qed
lemma knights_path_min2: "knights_path (board n m) ps \<Longrightarrow> min2 ps = 1"
proof -
assume assms: "knights_path (board n m) ps"
then have "min n m \<ge> 1"
using knights_path_board_m_n_geq_1 by auto
then have "(1,1) \<in> board n m" and ge_1: "\<forall>(i,j) \<in> board n m. j \<ge> 1"
unfolding board_def by auto
then have finite: "finite ((snd) ` board n m)" and
non_empty: "(snd) ` board n m \<noteq> {}" and
mem_1: "1 \<in> (snd) ` board n m"
using board_finite by auto (metis sndI image_eqI)
then have "Min ((snd) ` board n m) = 1"
using ge_1 by (auto simp: Min_eq_iff)
then show ?thesis
using assms knights_path_set_eq by auto
qed
lemma knights_path_max1: "knights_path (board n m) ps \<Longrightarrow> max1 ps = int n"
proof -
assume assms: "knights_path (board n m) ps"
then have "min n m \<ge> 1"
using knights_path_board_m_n_geq_1 by auto
then have "(int n,1) \<in> board n m" and leq_n: "\<forall>(i,j) \<in> board n m. i \<le> int n"
unfolding board_def by auto
then have finite: "finite ((fst) ` board n m)" and
non_empty: "(fst) ` board n m \<noteq> {}" and
mem_1: "int n \<in> (fst) ` board n m"
using board_finite by auto (metis fstI image_eqI)
then have "Max ((fst) ` board n m) = int n"
using leq_n by (auto simp: Max_eq_iff)
then show ?thesis
using assms knights_path_set_eq by auto
qed
lemma knights_path_max2: "knights_path (board n m) ps \<Longrightarrow> max2 ps = int m"
proof -
assume assms: "knights_path (board n m) ps"
then have "min n m \<ge> 1"
using knights_path_board_m_n_geq_1 by auto
then have "(1,int m) \<in> board n m" and leq_m: "\<forall>(i,j) \<in> board n m. j \<le> int m"
unfolding board_def by auto
then have finite: "finite ((snd) ` board n m)" and
non_empty: "(snd) ` board n m \<noteq> {}" and
mem_1: "int m \<in> (snd) ` board n m"
using board_finite by auto (metis sndI image_eqI)
then have "Max ((snd) ` board n m) = int m"
using leq_m by (auto simp: Max_eq_iff)
then show ?thesis
using assms knights_path_set_eq by auto
qed
lemma mirror1_aux_nil: "ps = [] \<longleftrightarrow> mirror1_aux m ps = []"
using mirror1_aux.elims by blast
lemma mirror1_nil: "ps = [] \<longleftrightarrow> mirror1 ps = []"
unfolding mirror1_def using mirror1_aux_nil by blast
lemma mirror2_aux_nil: "ps = [] \<longleftrightarrow> mirror2_aux m ps = []"
using mirror2_aux.elims by blast
lemma mirror2_nil: "ps = [] \<longleftrightarrow> mirror2 ps = []"
unfolding mirror2_def using mirror2_aux_nil by blast
lemma length_mirror1_aux: "length ps = length (mirror1_aux n ps)"
by (induction ps) auto
lemma length_mirror1: "length ps = length (mirror1 ps)"
unfolding mirror1_def using length_mirror1_aux by auto
lemma length_mirror2_aux: "length ps = length (mirror2_aux n ps)"
by (induction ps) auto
lemma length_mirror2: "length ps = length (mirror2 ps)"
unfolding mirror2_def using length_mirror2_aux by auto
lemma mirror1_board_iff:"s\<^sub>i \<notin> b \<longleftrightarrow> mirror1_square n s\<^sub>i \<notin> mirror1_board n b"
unfolding mirror1_board_def mirror1_square_def by (auto split: prod.splits)
lemma mirror2_board_iff:"s\<^sub>i \<notin> b \<longleftrightarrow> mirror2_square n s\<^sub>i \<notin> mirror2_board n b"
unfolding mirror2_board_def mirror2_square_def by (auto split: prod.splits)
lemma insert_mirror1_board:
"insert (mirror1_square n s\<^sub>i) (mirror1_board n b) = mirror1_board n (insert s\<^sub>i b)"
unfolding mirror1_board_def mirror1_square_def by (auto split: prod.splits)
lemma insert_mirror2_board:
"insert (mirror2_square n s\<^sub>i) (mirror2_board n b) = mirror2_board n (insert s\<^sub>i b)"
unfolding mirror2_board_def mirror2_square_def by (auto split: prod.splits)
lemma "(i::int) = i'+1 \<Longrightarrow> n-i=n-(i'+1)"
by auto
lemma valid_step_mirror1:
"valid_step s\<^sub>i s\<^sub>j \<longleftrightarrow> valid_step (mirror1_square n s\<^sub>i) (mirror1_square n s\<^sub>j)"
proof
assume assms: "valid_step s\<^sub>i s\<^sub>j"
obtain i j i' j' where [simp]: "s\<^sub>i = (i,j)" "s\<^sub>j = (i',j')" by force
then have "valid_step (n-i,j) (n-i',j')"
using assms unfolding valid_step_def
apply simp
apply (elim disjE)
apply auto
done
then show "valid_step (mirror1_square n s\<^sub>i) (mirror1_square n s\<^sub>j)"
unfolding mirror1_square_def by auto
next
assume assms: "valid_step (mirror1_square n s\<^sub>i) (mirror1_square n s\<^sub>j)"
obtain i j i' j' where [simp]: "s\<^sub>i = (i,j)" "s\<^sub>j = (i',j')" by force
then have "valid_step (i,j) (i',j')"
using assms unfolding valid_step_def mirror1_square_def
apply simp
apply (elim disjE)
apply auto
done
then show "valid_step s\<^sub>i s\<^sub>j"
unfolding mirror1_square_def by auto
qed
lemma valid_step_mirror2:
"valid_step s\<^sub>i s\<^sub>j \<longleftrightarrow> valid_step (mirror2_square m s\<^sub>i) (mirror2_square m s\<^sub>j)"
proof
assume assms: "valid_step s\<^sub>i s\<^sub>j"
obtain i j i' j' where [simp]: "s\<^sub>i = (i,j)" "s\<^sub>j = (i',j')" by force
then have "valid_step (i,m-j) (i',m-j')"
using assms unfolding valid_step_def
apply simp
apply (elim disjE)
apply auto
done
then show "valid_step (mirror2_square m s\<^sub>i) (mirror2_square m s\<^sub>j)"
unfolding mirror2_square_def by auto
next
assume assms: "valid_step (mirror2_square m s\<^sub>i) (mirror2_square m s\<^sub>j)"
obtain i j i' j' where [simp]: "s\<^sub>i = (i,j)" "s\<^sub>j = (i',j')" by force
then have "valid_step (i,j) (i',j')"
using assms unfolding valid_step_def mirror2_square_def
apply simp
apply (elim disjE)
apply auto
done
then show "valid_step s\<^sub>i s\<^sub>j"
unfolding mirror1_square_def by auto
qed
lemma hd_mirror1:
assumes "knights_path (board n m) ps" "hd ps = (i,j)"
shows "hd (mirror1 ps) = (int n+1-i,j)"
using assms
proof -