src/HOL/Induct/Mutil.thy
changeset 28718 ef16499edaab
parent 23746 a455e69c31cc
child 28720 a08c37b478b2
equal deleted inserted replaced
28717:848ffc6b0b8a 28718:ef16499edaab
     1 (*  Title:      HOL/Induct/Mutil.thy
     1 (*  Title:      HOL/Induct/Mutil.thy
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1996  University of Cambridge
     4                 Tobias Nipkow, TUM (part 2)
     5 *)
     5 *)
     6 
     6 
     7 header {* The Mutilated Chess Board Problem *}
     7 header {* The Mutilated Chess Board Problem *}
     8 
     8 
     9 theory Mutil imports Main begin
     9 theory Mutil imports Main begin
       
    10 
       
    11 subsection{* The Mutilated Chess Board Cannot be Tiled by Dominoes *}
    10 
    12 
    11 text {*
    13 text {*
    12   The Mutilated Chess Board Problem, formalized inductively.
    14   The Mutilated Chess Board Problem, formalized inductively.
    13 
    15 
    14   Originator is Max Black, according to J A Robinson.  Popularized as
    16   Originator is Max Black, according to J A Robinson.  Popularized as
    51   apply (induct set: tiling)
    53   apply (induct set: tiling)
    52   apply (auto simp add: Un_assoc)
    54   apply (auto simp add: Un_assoc)
    53   done
    55   done
    54 
    56 
    55 
    57 
       
    58 lemma tiling_Diff1E [intro]:
       
    59 assumes "t-a \<in> tiling A"  "a \<in> A"  "a \<subseteq> t"
       
    60 shows "t \<in> tiling A"
       
    61 proof -
       
    62   from assms(2-3) have  "EX r. t = r Un a & r Int a = {}"
       
    63     by (metis Diff_disjoint Int_commute Un_Diff_cancel Un_absorb1 Un_commute)
       
    64   thus ?thesis using assms(1,2)
       
    65     by (auto simp:Un_Diff)
       
    66        (metis Compl_Diff_eq Diff_Compl Diff_empty Int_commute Un_Diff_cancel
       
    67               Un_commute double_complement tiling.Un)
       
    68 qed
       
    69 
       
    70 
    56 text {* \medskip Chess boards *}
    71 text {* \medskip Chess boards *}
    57 
    72 
    58 lemma Sigma_Suc1 [simp]:
    73 lemma Sigma_Suc1 [simp]:
    59      "lessThan (Suc n) \<times> B = ({n} \<times> B) \<union> ((lessThan n) \<times> B)"
    74      "{0..< Suc n} \<times> B = ({n} \<times> B) \<union> ({0..<n} \<times> B)"
    60   by auto
    75   by auto
    61 
    76 
    62 lemma Sigma_Suc2 [simp]:
    77 lemma Sigma_Suc2 [simp]:
    63      "A \<times> lessThan (Suc n) = (A \<times> {n}) \<union> (A \<times> (lessThan n))"
    78      "A \<times> {0..< Suc n} = (A \<times> {n}) \<union> (A \<times> {0..<n})"
    64   by auto
    79   by auto
    65 
    80 
    66 lemma sing_Times_lemma: "({i} \<times> {n}) \<union> ({i} \<times> {m}) = {(i, m), (i, n)}"
    81 lemma dominoes_tile_row [intro!]: "{i} \<times> {0..< 2*n} \<in> tiling domino"
    67   by auto
    82 apply (induct n)
    68 
    83 apply (simp_all del:Un_insert_left add: Un_assoc [symmetric])
    69 lemma dominoes_tile_row [intro!]: "{i} \<times> lessThan (2 * n) \<in> tiling domino"
    84 done
    70   apply (induct n)
    85 
    71    apply (simp_all add: Un_assoc [symmetric])
    86 lemma dominoes_tile_matrix: "{0..<m} \<times> {0..< 2*n} \<in> tiling domino"
    72   apply (rule tiling.Un)
       
    73     apply (auto simp add: sing_Times_lemma)
       
    74   done
       
    75 
       
    76 lemma dominoes_tile_matrix: "(lessThan m) \<times> lessThan (2 * n) \<in> tiling domino"
       
    77   by (induct m) auto
    87   by (induct m) auto
    78 
    88 
    79 
    89 
    80 text {* \medskip @{term coloured} and Dominoes *}
    90 text {* \medskip @{term coloured} and Dominoes *}
    81 
    91 
   119 
   129 
   120 
   130 
   121 text {* \medskip Final argument is surprisingly complex *}
   131 text {* \medskip Final argument is surprisingly complex *}
   122 
   132 
   123 theorem gen_mutil_not_tiling:
   133 theorem gen_mutil_not_tiling:
   124        "t \<in> tiling domino ==>
   134   "t \<in> tiling domino ==>
   125 	 (i + j) mod 2 = 0 ==> (m + n) mod 2 = 0 ==>
   135   (i + j) mod 2 = 0 ==> (m + n) mod 2 = 0 ==>
   126 	 {(i, j), (m, n)} \<subseteq> t
   136   {(i, j), (m, n)} \<subseteq> t
   127        ==> (t - {(i, j)} - {(m, n)}) \<notin> tiling domino"
   137   ==> (t - {(i,j)} - {(m,n)}) \<notin> tiling domino"
   128   apply (rule notI)
   138 apply (rule notI)
   129   apply (subgoal_tac
   139 apply (subgoal_tac
   130           "card (whites \<inter> (t - {(i, j)} - {(m, n)})) <
   140   "card (whites \<inter> (t - {(i,j)} - {(m,n)})) <
   131            card (blacks \<inter> (t - {(i, j)} - {(m, n)}))")
   141    card (blacks \<inter> (t - {(i,j)} - {(m,n)}))")
   132    apply (force simp only: tiling_domino_0_1)
   142  apply (force simp only: tiling_domino_0_1)
   133   apply (simp add: tiling_domino_0_1 [symmetric])
   143 apply (simp add: tiling_domino_0_1 [symmetric])
   134   apply (simp add: coloured_def card_Diff2_less)
   144 apply (simp add: coloured_def card_Diff2_less)
   135   done
   145 done
   136 
   146 
   137 text {* Apply the general theorem to the well-known case *}
   147 text {* Apply the general theorem to the well-known case *}
   138 
   148 
   139 theorem mutil_not_tiling:
   149 theorem mutil_not_tiling:
   140        "t = lessThan (2 * Suc m) \<times> lessThan (2 * Suc n)
   150   "t = {0..< 2 * Suc m} \<times> {0..< 2 * Suc n}
   141 	 ==> t - {(0, 0)} - {(Suc (2 * m), Suc (2 * n))} \<notin> tiling domino"
   151    ==> t - {(0,0)} - {(Suc(2 * m), Suc(2 * n))} \<notin> tiling domino"
   142   apply (rule gen_mutil_not_tiling)
   152 apply (rule gen_mutil_not_tiling)
   143      apply (blast intro!: dominoes_tile_matrix)
   153  apply (blast intro!: dominoes_tile_matrix)
   144     apply auto
   154 apply auto
   145   done
   155 done
       
   156 
       
   157 
       
   158 subsection{* The Mutilated Chess Board Can be Tiled by Ls *}
       
   159 
       
   160 text{* We remove any square from a chess board of size $2^n \times 2^n$.
       
   161 The result can be tiled by L-shaped tiles.
       
   162 Found in Velleman's \emph{How to Prove it}.
       
   163 
       
   164 The four possible L-shaped tiles are obtained by dropping
       
   165 one of the four squares from $\{(x,y),(x+1,y),(x,y+1),(x+1,y+1)\}$: *}
       
   166 
       
   167 definition "L2 (x::nat) (y::nat) = {(x,y), (x+1,y), (x, y+1)}"
       
   168 definition "L3 (x::nat) (y::nat) = {(x,y), (x+1,y), (x+1, y+1)}"
       
   169 definition "L0 (x::nat) (y::nat) = {(x+1,y), (x,y+1), (x+1, y+1)}"
       
   170 definition "L1 (x::nat) (y::nat) = {(x,y), (x,y+1), (x+1, y+1)}"
       
   171 
       
   172 text{* All tiles: *}
       
   173 
       
   174 definition Ls :: "(nat * nat) set set" where
       
   175 "Ls \<equiv> { L0 x y | x y. True} \<union> { L1 x y | x y. True} \<union>
       
   176       { L2 x y | x y. True} \<union> { L3 x y | x y. True}"
       
   177 
       
   178 lemma LinLs: "L0 i j : Ls & L1 i j : Ls & L2 i j : Ls & L3 i j : Ls"
       
   179 by(fastsimp simp:Ls_def)
       
   180 
       
   181 
       
   182 text{* Square $2^n \times 2^n$ grid, shifted by $i$ and $j$: *}
       
   183 
       
   184 definition "square2 (n::nat) (i::nat) (j::nat) = {i..< 2^n+i} \<times> {j..< 2^n+j}"
       
   185 
       
   186 lemma in_square2[simp]:
       
   187   "(a,b) : square2 n i j \<longleftrightarrow> i\<le>a \<and> a<2^n+i \<and> j\<le>b \<and> b<2^n+j"
       
   188 by(simp add:square2_def)
       
   189 
       
   190 lemma square2_Suc: "square2 (Suc n) i j =
       
   191   square2 n i j \<union> square2 n (2^n + i) j \<union> square2 n i (2^n + j) \<union>
       
   192   square2 n (2^n + i) (2^n + j)"
       
   193 by(auto simp:square2_def)
       
   194 
       
   195 lemma square2_disj: "square2 n i j \<inter> square2 n x y = {} \<longleftrightarrow>
       
   196   (2^n+i \<le> x \<or> 2^n+x \<le> i) \<or> (2^n+j \<le> y \<or> 2^n+y \<le> j)" (is "?A = ?B")
       
   197 proof-
       
   198   { assume ?B hence ?A by(auto simp:square2_def) }
       
   199   moreover
       
   200   { assume "\<not> ?B"
       
   201     hence "(max i x, max j y) : square2 n i j \<inter> square2 n x y" by simp
       
   202     hence "\<not> ?A" by blast }
       
   203   ultimately show ?thesis by blast
       
   204 qed
       
   205 
       
   206 text{* Some specific lemmas: *}
       
   207 
       
   208 lemma pos_pow2: "(0::nat) < 2^(n::nat)"
       
   209 by simp
       
   210 
       
   211 declare nat_zero_less_power_iff[simp del] zero_less_power[simp del]
       
   212 
       
   213 lemma Diff_insert_if: shows
       
   214   "B \<noteq> {} \<Longrightarrow> a:A \<Longrightarrow> A - insert a B = (A-B - {a})" and
       
   215   "B \<noteq> {} \<Longrightarrow> a ~: A \<Longrightarrow> A - insert a B = A-B"
       
   216 by auto
       
   217 
       
   218 lemma DisjI1: "A Int B = {} \<Longrightarrow> (A-X) Int B = {}"
       
   219 by blast
       
   220 lemma DisjI2: "A Int B = {} \<Longrightarrow> A Int (B-X) = {}"
       
   221 by blast
       
   222 
       
   223 text{* The main theorem: *}
       
   224 
       
   225 theorem Ls_can_tile: "i \<le> a \<Longrightarrow> a < 2^n + i \<Longrightarrow> j \<le> b \<Longrightarrow> b < 2^n + j
       
   226   \<Longrightarrow> square2 n i j - {(a,b)} : tiling Ls"
       
   227 proof(induct n arbitrary: a b i j)
       
   228   case 0 thus ?case by (simp add:square2_def)
       
   229 next
       
   230   case (Suc n) note IH = Suc(1) and a = Suc(2-3) and b = Suc(4-5)
       
   231   hence "a<2^n+i \<and> b<2^n+j \<or>
       
   232          2^n+i\<le>a \<and> a<2^(n+1)+i \<and> b<2^n+j \<or>
       
   233          a<2^n+i \<and> 2^n+j\<le>b \<and> b<2^(n+1)+j \<or>
       
   234          2^n+i\<le>a \<and> a<2^(n+1)+i \<and> 2^n+j\<le>b \<and> b<2^(n+1)+j" (is "?A|?B|?C|?D")
       
   235     by simp arith
       
   236   moreover
       
   237   { assume "?A"
       
   238     hence "square2 n i j - {(a,b)} : tiling Ls" using IH a b by auto
       
   239     moreover have "square2 n (2^n+i) j - {(2^n+i,2^n+j - 1)} : tiling Ls"
       
   240       by(rule IH)(insert pos_pow2[of n], auto)
       
   241     moreover have "square2 n i (2^n+j) - {(2^n+i - 1, 2^n+j)} : tiling Ls"
       
   242       by(rule IH)(insert pos_pow2[of n], auto)
       
   243     moreover have "square2 n (2^n+i) (2^n+j) - {(2^n+i, 2^n+j)} : tiling Ls"
       
   244       by(rule IH)(insert pos_pow2[of n], auto)
       
   245     ultimately
       
   246     have "square2 (n+1) i j - {(a,b)} - L0 (2^n+i - 1) (2^n+j - 1) \<in> tiling Ls"
       
   247       using  a b `?A`
       
   248       by (clarsimp simp: square2_Suc L0_def Un_Diff Diff_insert_if)
       
   249          (fastsimp intro!: tiling_UnI DisjI1 DisjI2 square2_disj[THEN iffD2]
       
   250                    simp:Int_Un_distrib2)
       
   251   } moreover
       
   252   { assume "?B"
       
   253     hence "square2 n (2^n+i) j - {(a,b)} : tiling Ls" using IH a b by auto
       
   254     moreover have "square2 n i j - {(2^n+i - 1,2^n+j - 1)} : tiling Ls"
       
   255       by(rule IH)(insert pos_pow2[of n], auto)
       
   256     moreover have "square2 n i (2^n+j) - {(2^n+i - 1, 2^n+j)} : tiling Ls"
       
   257       by(rule IH)(insert pos_pow2[of n], auto)
       
   258     moreover have "square2 n (2^n+i) (2^n+j) - {(2^n+i, 2^n+j)} : tiling Ls"
       
   259       by(rule IH)(insert pos_pow2[of n], auto)
       
   260     ultimately
       
   261     have "square2 (n+1) i j - {(a,b)} - L1 (2^n+i - 1) (2^n+j - 1) \<in> tiling Ls"
       
   262       using  a b `?B`
       
   263       by (simp add: square2_Suc L1_def Un_Diff Diff_insert_if le_diff_conv2)
       
   264          (fastsimp intro!: tiling_UnI DisjI1 DisjI2 square2_disj[THEN iffD2]
       
   265                    simp:Int_Un_distrib2)
       
   266   } moreover
       
   267   { assume "?C"
       
   268     hence "square2 n i (2^n+j) - {(a,b)} : tiling Ls" using IH a b by auto
       
   269     moreover have "square2 n i j - {(2^n+i - 1,2^n+j - 1)} : tiling Ls"
       
   270       by(rule IH)(insert pos_pow2[of n], auto)
       
   271     moreover have "square2 n (2^n+i) j - {(2^n+i, 2^n+j - 1)} : tiling Ls"
       
   272       by(rule IH)(insert pos_pow2[of n], auto)
       
   273     moreover have "square2 n (2^n+i) (2^n+j) - {(2^n+i, 2^n+j)} : tiling Ls"
       
   274       by(rule IH)(insert pos_pow2[of n], auto)
       
   275     ultimately
       
   276     have "square2 (n+1) i j - {(a,b)} - L3 (2^n+i - 1) (2^n+j - 1) \<in> tiling Ls"
       
   277       using  a b `?C`
       
   278       by (simp add: square2_Suc L3_def Un_Diff Diff_insert_if le_diff_conv2)
       
   279          (fastsimp intro!: tiling_UnI DisjI1 DisjI2 square2_disj[THEN iffD2]
       
   280                    simp:Int_Un_distrib2)
       
   281   } moreover
       
   282   { assume "?D"
       
   283     hence "square2 n (2^n+i) (2^n+j) -{(a,b)} : tiling Ls" using IH a b by auto
       
   284     moreover have "square2 n i j - {(2^n+i - 1,2^n+j - 1)} : tiling Ls"
       
   285       by(rule IH)(insert pos_pow2[of n], auto)
       
   286     moreover have "square2 n (2^n+i) j - {(2^n+i, 2^n+j - 1)} : tiling Ls"
       
   287       by(rule IH)(insert pos_pow2[of n], auto)
       
   288     moreover have "square2 n i (2^n+j) - {(2^n+i - 1, 2^n+j)} : tiling Ls"
       
   289       by(rule IH)(insert pos_pow2[of n], auto)
       
   290     ultimately
       
   291     have "square2 (n+1) i j - {(a,b)} - L2 (2^n+i - 1) (2^n+j - 1) \<in> tiling Ls"
       
   292       using  a b `?D`
       
   293       by (simp add: square2_Suc L2_def Un_Diff Diff_insert_if le_diff_conv2)
       
   294          (fastsimp intro!: tiling_UnI DisjI1 DisjI2 square2_disj[THEN iffD2]
       
   295                    simp:Int_Un_distrib2)
       
   296   } moreover
       
   297   have "?A \<Longrightarrow> L0 (2^n + i - 1) (2^n + j - 1) \<subseteq> square2 (n+1) i j - {(a, b)}"
       
   298     using a b by(simp add:L0_def) arith moreover
       
   299   have "?B \<Longrightarrow> L1 (2^n + i - 1) (2^n + j - 1) \<subseteq> square2 (n+1) i j - {(a, b)}"
       
   300     using a b by(simp add:L1_def) arith moreover
       
   301   have "?C \<Longrightarrow> L3 (2^n + i - 1) (2^n + j - 1) \<subseteq> square2 (n+1) i j - {(a, b)}"
       
   302     using a b by(simp add:L3_def) arith moreover
       
   303   have "?D \<Longrightarrow> L2 (2^n + i - 1) (2^n + j - 1) \<subseteq> square2 (n+1) i j - {(a, b)}"
       
   304     using a b by(simp add:L2_def) arith
       
   305   ultimately show ?case
       
   306     apply simp
       
   307     apply(erule disjE)
       
   308     apply (metis LinLs tiling_Diff1E)
       
   309     apply (metis LinLs tiling_Diff1E)
       
   310     done
       
   311 qed
       
   312 
       
   313 corollary Ls_can_tile00:
       
   314   "a < 2^n \<Longrightarrow> b < 2^n \<Longrightarrow> square2 n 0 0 - {(a, b)} \<in> tiling Ls"
       
   315 by(rule Ls_can_tile) auto
   146 
   316 
   147 end
   317 end