src/HOL/Isar_Examples/Mutilated_Checkerboard.thy
author blanchet
Thu Sep 11 18:54:36 2014 +0200 (2014-09-11)
changeset 58306 117ba6cbe414
parent 55656 eb07b0acbebc
child 58614 7338eb25226c
permissions -rw-r--r--
renamed 'rep_datatype' to 'old_rep_datatype' (HOL)
     1 (*  Title:      HOL/Isar_Examples/Mutilated_Checkerboard.thy
     2     Author:     Markus Wenzel, TU Muenchen (Isar document)
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory (original scripts)
     4 *)
     5 
     6 header {* The Mutilated Checker Board Problem *}
     7 
     8 theory Mutilated_Checkerboard
     9 imports Main
    10 begin
    11 
    12 text {* The Mutilated Checker Board Problem, formalized inductively.
    13   See \cite{paulson-mutilated-board} for the original tactic script version. *}
    14 
    15 subsection {* Tilings *}
    16 
    17 inductive_set tiling :: "'a set set \<Rightarrow> 'a set set"
    18   for A :: "'a set set"
    19 where
    20   empty: "{} \<in> tiling A"
    21 | Un: "a \<in> A \<Longrightarrow> t \<in> tiling A \<Longrightarrow> a \<subseteq> - t \<Longrightarrow> a \<union> t \<in> tiling A"
    22 
    23 
    24 text "The union of two disjoint tilings is a tiling."
    25 
    26 lemma tiling_Un:
    27   assumes "t \<in> tiling A"
    28     and "u \<in> tiling A"
    29     and "t \<inter> u = {}"
    30   shows "t \<union> u \<in> tiling A"
    31 proof -
    32   let ?T = "tiling A"
    33   from `t \<in> ?T` and `t \<inter> u = {}`
    34   show "t \<union> u \<in> ?T"
    35   proof (induct t)
    36     case empty
    37     with `u \<in> ?T` show "{} \<union> u \<in> ?T" by simp
    38   next
    39     case (Un a t)
    40     show "(a \<union> t) \<union> u \<in> ?T"
    41     proof -
    42       have "a \<union> (t \<union> u) \<in> ?T"
    43         using `a \<in> A`
    44       proof (rule tiling.Un)
    45         from `(a \<union> t) \<inter> u = {}` have "t \<inter> u = {}" by blast
    46         then show "t \<union> u \<in> ?T" by (rule Un)
    47         from `a \<subseteq> - t` and `(a \<union> t) \<inter> u = {}`
    48         show "a \<subseteq> - (t \<union> u)" by blast
    49       qed
    50       also have "a \<union> (t \<union> u) = (a \<union> t) \<union> u"
    51         by (simp only: Un_assoc)
    52       finally show ?thesis .
    53     qed
    54   qed
    55 qed
    56 
    57 
    58 subsection {* Basic properties of ``below'' *}
    59 
    60 definition below :: "nat \<Rightarrow> nat set"
    61   where "below n = {i. i < n}"
    62 
    63 lemma below_less_iff [iff]: "i \<in> below k \<longleftrightarrow> i < k"
    64   by (simp add: below_def)
    65 
    66 lemma below_0: "below 0 = {}"
    67   by (simp add: below_def)
    68 
    69 lemma Sigma_Suc1: "m = n + 1 \<Longrightarrow> below m \<times> B = ({n} \<times> B) \<union> (below n \<times> B)"
    70   by (simp add: below_def less_Suc_eq) blast
    71 
    72 lemma Sigma_Suc2:
    73   "m = n + 2 \<Longrightarrow>
    74     A \<times> below m = (A \<times> {n}) \<union> (A \<times> {n + 1}) \<union> (A \<times> below n)"
    75   by (auto simp add: below_def)
    76 
    77 lemmas Sigma_Suc = Sigma_Suc1 Sigma_Suc2
    78 
    79 
    80 subsection {* Basic properties of ``evnodd'' *}
    81 
    82 definition evnodd :: "(nat \<times> nat) set \<Rightarrow> nat \<Rightarrow> (nat \<times> nat) set"
    83   where "evnodd A b = A \<inter> {(i, j). (i + j) mod 2 = b}"
    84 
    85 lemma evnodd_iff: "(i, j) \<in> evnodd A b \<longleftrightarrow> (i, j) \<in> A  \<and> (i + j) mod 2 = b"
    86   by (simp add: evnodd_def)
    87 
    88 lemma evnodd_subset: "evnodd A b \<subseteq> A"
    89   unfolding evnodd_def by (rule Int_lower1)
    90 
    91 lemma evnoddD: "x \<in> evnodd A b \<Longrightarrow> x \<in> A"
    92   by (rule subsetD) (rule evnodd_subset)
    93 
    94 lemma evnodd_finite: "finite A \<Longrightarrow> finite (evnodd A b)"
    95   by (rule finite_subset) (rule evnodd_subset)
    96 
    97 lemma evnodd_Un: "evnodd (A \<union> B) b = evnodd A b \<union> evnodd B b"
    98   unfolding evnodd_def by blast
    99 
   100 lemma evnodd_Diff: "evnodd (A - B) b = evnodd A b - evnodd B b"
   101   unfolding evnodd_def by blast
   102 
   103 lemma evnodd_empty: "evnodd {} b = {}"
   104   by (simp add: evnodd_def)
   105 
   106 lemma evnodd_insert: "evnodd (insert (i, j) C) b =
   107     (if (i + j) mod 2 = b
   108       then insert (i, j) (evnodd C b) else evnodd C b)"
   109   by (simp add: evnodd_def)
   110 
   111 
   112 subsection {* Dominoes *}
   113 
   114 inductive_set domino :: "(nat \<times> nat) set set"
   115 where
   116   horiz: "{(i, j), (i, j + 1)} \<in> domino"
   117 | vertl: "{(i, j), (i + 1, j)} \<in> domino"
   118 
   119 lemma dominoes_tile_row:
   120   "{i} \<times> below (2 * n) \<in> tiling domino"
   121   (is "?B n \<in> ?T")
   122 proof (induct n)
   123   case 0
   124   show ?case by (simp add: below_0 tiling.empty)
   125 next
   126   case (Suc n)
   127   let ?a = "{i} \<times> {2 * n + 1} \<union> {i} \<times> {2 * n}"
   128   have "?B (Suc n) = ?a \<union> ?B n"
   129     by (auto simp add: Sigma_Suc Un_assoc)
   130   also have "\<dots> \<in> ?T"
   131   proof (rule tiling.Un)
   132     have "{(i, 2 * n), (i, 2 * n + 1)} \<in> domino"
   133       by (rule domino.horiz)
   134     also have "{(i, 2 * n), (i, 2 * n + 1)} = ?a" by blast
   135     finally show "\<dots> \<in> domino" .
   136     show "?B n \<in> ?T" by (rule Suc)
   137     show "?a \<subseteq> - ?B n" by blast
   138   qed
   139   finally show ?case .
   140 qed
   141 
   142 lemma dominoes_tile_matrix:
   143   "below m \<times> below (2 * n) \<in> tiling domino"
   144   (is "?B m \<in> ?T")
   145 proof (induct m)
   146   case 0
   147   show ?case by (simp add: below_0 tiling.empty)
   148 next
   149   case (Suc m)
   150   let ?t = "{m} \<times> below (2 * n)"
   151   have "?B (Suc m) = ?t \<union> ?B m" by (simp add: Sigma_Suc)
   152   also have "\<dots> \<in> ?T"
   153   proof (rule tiling_Un)
   154     show "?t \<in> ?T" by (rule dominoes_tile_row)
   155     show "?B m \<in> ?T" by (rule Suc)
   156     show "?t \<inter> ?B m = {}" by blast
   157   qed
   158   finally show ?case .
   159 qed
   160 
   161 lemma domino_singleton:
   162   assumes "d \<in> domino"
   163     and "b < 2"
   164   shows "\<exists>i j. evnodd d b = {(i, j)}"  (is "?P d")
   165   using assms
   166 proof induct
   167   from `b < 2` have b_cases: "b = 0 \<or> b = 1" by arith
   168   fix i j
   169   note [simp] = evnodd_empty evnodd_insert mod_Suc
   170   from b_cases show "?P {(i, j), (i, j + 1)}" by rule auto
   171   from b_cases show "?P {(i, j), (i + 1, j)}" by rule auto
   172 qed
   173 
   174 lemma domino_finite:
   175   assumes "d \<in> domino"
   176   shows "finite d"
   177   using assms
   178 proof induct
   179   fix i j :: nat
   180   show "finite {(i, j), (i, j + 1)}" by (intro finite.intros)
   181   show "finite {(i, j), (i + 1, j)}" by (intro finite.intros)
   182 qed
   183 
   184 
   185 subsection {* Tilings of dominoes *}
   186 
   187 lemma tiling_domino_finite:
   188   assumes t: "t \<in> tiling domino"  (is "t \<in> ?T")
   189   shows "finite t"  (is "?F t")
   190   using t
   191 proof induct
   192   show "?F {}" by (rule finite.emptyI)
   193   fix a t assume "?F t"
   194   assume "a \<in> domino"
   195   then have "?F a" by (rule domino_finite)
   196   from this and `?F t` show "?F (a \<union> t)" by (rule finite_UnI)
   197 qed
   198 
   199 lemma tiling_domino_01:
   200   assumes t: "t \<in> tiling domino"  (is "t \<in> ?T")
   201   shows "card (evnodd t 0) = card (evnodd t 1)"
   202   using t
   203 proof induct
   204   case empty
   205   show ?case by (simp add: evnodd_def)
   206 next
   207   case (Un a t)
   208   let ?e = evnodd
   209   note hyp = `card (?e t 0) = card (?e t 1)`
   210     and at = `a \<subseteq> - t`
   211   have card_suc:
   212     "\<And>b. b < 2 \<Longrightarrow> card (?e (a \<union> t) b) = Suc (card (?e t b))"
   213   proof -
   214     fix b :: nat
   215     assume "b < 2"
   216     have "?e (a \<union> t) b = ?e a b \<union> ?e t b" by (rule evnodd_Un)
   217     also obtain i j where e: "?e a b = {(i, j)}"
   218     proof -
   219       from `a \<in> domino` and `b < 2`
   220       have "\<exists>i j. ?e a b = {(i, j)}" by (rule domino_singleton)
   221       then show ?thesis by (blast intro: that)
   222     qed
   223     also have "\<dots> \<union> ?e t b = insert (i, j) (?e t b)" by simp
   224     also have "card \<dots> = Suc (card (?e t b))"
   225     proof (rule card_insert_disjoint)
   226       from `t \<in> tiling domino` have "finite t"
   227         by (rule tiling_domino_finite)
   228       then show "finite (?e t b)"
   229         by (rule evnodd_finite)
   230       from e have "(i, j) \<in> ?e a b" by simp
   231       with at show "(i, j) \<notin> ?e t b" by (blast dest: evnoddD)
   232     qed
   233     finally show "?thesis b" .
   234   qed
   235   then have "card (?e (a \<union> t) 0) = Suc (card (?e t 0))" by simp
   236   also from hyp have "card (?e t 0) = card (?e t 1)" .
   237   also from card_suc have "Suc \<dots> = card (?e (a \<union> t) 1)"
   238     by simp
   239   finally show ?case .
   240 qed
   241 
   242 
   243 subsection {* Main theorem *}
   244 
   245 definition mutilated_board :: "nat \<Rightarrow> nat \<Rightarrow> (nat \<times> nat) set"
   246   where
   247     "mutilated_board m n =
   248       below (2 * (m + 1)) \<times> below (2 * (n + 1))
   249         - {(0, 0)} - {(2 * m + 1, 2 * n + 1)}"
   250 
   251 theorem mutil_not_tiling: "mutilated_board m n \<notin> tiling domino"
   252 proof (unfold mutilated_board_def)
   253   let ?T = "tiling domino"
   254   let ?t = "below (2 * (m + 1)) \<times> below (2 * (n + 1))"
   255   let ?t' = "?t - {(0, 0)}"
   256   let ?t'' = "?t' - {(2 * m + 1, 2 * n + 1)}"
   257 
   258   show "?t'' \<notin> ?T"
   259   proof
   260     have t: "?t \<in> ?T" by (rule dominoes_tile_matrix)
   261     assume t'': "?t'' \<in> ?T"
   262 
   263     let ?e = evnodd
   264     have fin: "finite (?e ?t 0)"
   265       by (rule evnodd_finite, rule tiling_domino_finite, rule t)
   266 
   267     note [simp] = evnodd_iff evnodd_empty evnodd_insert evnodd_Diff
   268     have "card (?e ?t'' 0) < card (?e ?t' 0)"
   269     proof -
   270       have "card (?e ?t' 0 - {(2 * m + 1, 2 * n + 1)})
   271         < card (?e ?t' 0)"
   272       proof (rule card_Diff1_less)
   273         from _ fin show "finite (?e ?t' 0)"
   274           by (rule finite_subset) auto
   275         show "(2 * m + 1, 2 * n + 1) \<in> ?e ?t' 0" by simp
   276       qed
   277       then show ?thesis by simp
   278     qed
   279     also have "\<dots> < card (?e ?t 0)"
   280     proof -
   281       have "(0, 0) \<in> ?e ?t 0" by simp
   282       with fin have "card (?e ?t 0 - {(0, 0)}) < card (?e ?t 0)"
   283         by (rule card_Diff1_less)
   284       then show ?thesis by simp
   285     qed
   286     also from t have "\<dots> = card (?e ?t 1)"
   287       by (rule tiling_domino_01)
   288     also have "?e ?t 1 = ?e ?t'' 1" by simp
   289     also from t'' have "card \<dots> = card (?e ?t'' 0)"
   290       by (rule tiling_domino_01 [symmetric])
   291     finally have "\<dots> < \<dots>" . then show False ..
   292   qed
   293 qed
   294 
   295 end