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