src/HOL/Isar_Examples/Mutilated_Checkerboard.thy
 author haftmann Wed Jun 30 16:46:44 2010 +0200 (2010-06-30) changeset 37659 14cabf5fa710 parent 35416 d8d7d1b785af child 37671 fa53d267dab3 permissions -rw-r--r--
more speaking names
```     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 definition below :: "nat => nat set" where
```
```    64   "below n == {i. i < n}"
```
```    65
```
```    66 lemma below_less_iff [iff]: "(i: below k) = (i < k)"
```
```    67   by (simp add: below_def)
```
```    68
```
```    69 lemma below_0: "below 0 = {}"
```
```    70   by (simp add: below_def)
```
```    71
```
```    72 lemma Sigma_Suc1:
```
```    73     "m = n + 1 ==> below m <*> B = ({n} <*> B) Un (below n <*> B)"
```
```    74   by (simp add: below_def less_Suc_eq) blast
```
```    75
```
```    76 lemma Sigma_Suc2:
```
```    77     "m = n + 2 ==> A <*> below m =
```
```    78       (A <*> {n}) Un (A <*> {n + 1}) Un (A <*> below n)"
```
```    79   by (auto simp add: below_def)
```
```    80
```
```    81 lemmas Sigma_Suc = Sigma_Suc1 Sigma_Suc2
```
```    82
```
```    83
```
```    84 subsection {* Basic properties of ``evnodd'' *}
```
```    85
```
```    86 definition evnodd :: "(nat * nat) set => nat => (nat * nat) set" where
```
```    87   "evnodd A b == A Int {(i, j). (i + j) mod 2 = b}"
```
```    88
```
```    89 lemma evnodd_iff:
```
```    90     "(i, j): evnodd A b = ((i, j): A  & (i + j) mod 2 = b)"
```
```    91   by (simp add: evnodd_def)
```
```    92
```
```    93 lemma evnodd_subset: "evnodd A b <= A"
```
```    94   by (unfold evnodd_def, rule Int_lower1)
```
```    95
```
```    96 lemma evnoddD: "x : evnodd A b ==> x : A"
```
```    97   by (rule subsetD, rule evnodd_subset)
```
```    98
```
```    99 lemma evnodd_finite: "finite A ==> finite (evnodd A b)"
```
```   100   by (rule finite_subset, rule evnodd_subset)
```
```   101
```
```   102 lemma evnodd_Un: "evnodd (A Un B) b = evnodd A b Un evnodd B b"
```
```   103   by (unfold evnodd_def) blast
```
```   104
```
```   105 lemma evnodd_Diff: "evnodd (A - B) b = evnodd A b - evnodd B b"
```
```   106   by (unfold evnodd_def) blast
```
```   107
```
```   108 lemma evnodd_empty: "evnodd {} b = {}"
```
```   109   by (simp add: evnodd_def)
```
```   110
```
```   111 lemma evnodd_insert: "evnodd (insert (i, j) C) b =
```
```   112     (if (i + j) mod 2 = b
```
```   113       then insert (i, j) (evnodd C b) else evnodd C b)"
```
```   114   by (simp add: evnodd_def)
```
```   115
```
```   116
```
```   117 subsection {* Dominoes *}
```
```   118
```
```   119 inductive_set
```
```   120   domino :: "(nat * nat) set set"
```
```   121   where
```
```   122     horiz: "{(i, j), (i, j + 1)} : domino"
```
```   123   | vertl: "{(i, j), (i + 1, j)} : domino"
```
```   124
```
```   125 lemma dominoes_tile_row:
```
```   126   "{i} <*> below (2 * n) : tiling domino"
```
```   127   (is "?B n : ?T")
```
```   128 proof (induct n)
```
```   129   case 0
```
```   130   show ?case by (simp add: below_0 tiling.empty)
```
```   131 next
```
```   132   case (Suc n)
```
```   133   let ?a = "{i} <*> {2 * n + 1} Un {i} <*> {2 * n}"
```
```   134   have "?B (Suc n) = ?a Un ?B n"
```
```   135     by (auto simp add: Sigma_Suc Un_assoc)
```
```   136   moreover have "... : ?T"
```
```   137   proof (rule tiling.Un)
```
```   138     have "{(i, 2 * n), (i, 2 * n + 1)} : domino"
```
```   139       by (rule domino.horiz)
```
```   140     also have "{(i, 2 * n), (i, 2 * n + 1)} = ?a" by blast
```
```   141     finally show "... : domino" .
```
```   142     show "?B n : ?T" by (rule Suc)
```
```   143     show "?a <= - ?B n" by blast
```
```   144   qed
```
```   145   ultimately show ?case by simp
```
```   146 qed
```
```   147
```
```   148 lemma dominoes_tile_matrix:
```
```   149   "below m <*> below (2 * n) : tiling domino"
```
```   150   (is "?B m : ?T")
```
```   151 proof (induct m)
```
```   152   case 0
```
```   153   show ?case by (simp add: below_0 tiling.empty)
```
```   154 next
```
```   155   case (Suc m)
```
```   156   let ?t = "{m} <*> below (2 * n)"
```
```   157   have "?B (Suc m) = ?t Un ?B m" by (simp add: Sigma_Suc)
```
```   158   moreover have "... : ?T"
```
```   159   proof (rule tiling_Un)
```
```   160     show "?t : ?T" by (rule dominoes_tile_row)
```
```   161     show "?B m : ?T" by (rule Suc)
```
```   162     show "?t Int ?B m = {}" by blast
```
```   163   qed
```
```   164   ultimately show ?case by simp
```
```   165 qed
```
```   166
```
```   167 lemma domino_singleton:
```
```   168   assumes d: "d : domino" and "b < 2"
```
```   169   shows "EX i j. evnodd d b = {(i, j)}"  (is "?P d")
```
```   170   using d
```
```   171 proof induct
```
```   172   from `b < 2` have b_cases: "b = 0 | b = 1" by arith
```
```   173   fix i j
```
```   174   note [simp] = evnodd_empty evnodd_insert mod_Suc
```
```   175   from b_cases show "?P {(i, j), (i, j + 1)}" by rule auto
```
```   176   from b_cases show "?P {(i, j), (i + 1, j)}" by rule auto
```
```   177 qed
```
```   178
```
```   179 lemma domino_finite:
```
```   180   assumes d: "d: domino"
```
```   181   shows "finite d"
```
```   182   using d
```
```   183 proof induct
```
```   184   fix i j :: nat
```
```   185   show "finite {(i, j), (i, j + 1)}" by (intro finite.intros)
```
```   186   show "finite {(i, j), (i + 1, j)}" by (intro finite.intros)
```
```   187 qed
```
```   188
```
```   189
```
```   190 subsection {* Tilings of dominoes *}
```
```   191
```
```   192 lemma tiling_domino_finite:
```
```   193   assumes t: "t : tiling domino"  (is "t : ?T")
```
```   194   shows "finite t"  (is "?F t")
```
```   195   using t
```
```   196 proof induct
```
```   197   show "?F {}" by (rule finite.emptyI)
```
```   198   fix a t assume "?F t"
```
```   199   assume "a : domino" then have "?F a" by (rule domino_finite)
```
```   200   from this and `?F t` show "?F (a Un t)" by (rule finite_UnI)
```
```   201 qed
```
```   202
```
```   203 lemma tiling_domino_01:
```
```   204   assumes t: "t : tiling domino"  (is "t : ?T")
```
```   205   shows "card (evnodd t 0) = card (evnodd t 1)"
```
```   206   using t
```
```   207 proof induct
```
```   208   case empty
```
```   209   show ?case by (simp add: evnodd_def)
```
```   210 next
```
```   211   case (Un a t)
```
```   212   let ?e = evnodd
```
```   213   note hyp = `card (?e t 0) = card (?e t 1)`
```
```   214     and at = `a <= - t`
```
```   215   have card_suc:
```
```   216     "!!b. b < 2 ==> card (?e (a Un t) b) = Suc (card (?e t b))"
```
```   217   proof -
```
```   218     fix b :: nat assume "b < 2"
```
```   219     have "?e (a Un t) b = ?e a b Un ?e t b" by (rule evnodd_Un)
```
```   220     also obtain i j where e: "?e a b = {(i, j)}"
```
```   221     proof -
```
```   222       from `a \<in> domino` and `b < 2`
```
```   223       have "EX i j. ?e a b = {(i, j)}" by (rule domino_singleton)
```
```   224       then show ?thesis by (blast intro: that)
```
```   225     qed
```
```   226     moreover have "... Un ?e t b = insert (i, j) (?e t b)" by simp
```
```   227     moreover have "card ... = Suc (card (?e t b))"
```
```   228     proof (rule card_insert_disjoint)
```
```   229       from `t \<in> tiling domino` have "finite t"
```
```   230         by (rule tiling_domino_finite)
```
```   231       then show "finite (?e t b)"
```
```   232         by (rule evnodd_finite)
```
```   233       from e have "(i, j) : ?e a b" by simp
```
```   234       with at show "(i, j) ~: ?e t b" by (blast dest: evnoddD)
```
```   235     qed
```
```   236     ultimately show "?thesis b" by simp
```
```   237   qed
```
```   238   then have "card (?e (a Un t) 0) = Suc (card (?e t 0))" by simp
```
```   239   also from hyp have "card (?e t 0) = card (?e t 1)" .
```
```   240   also from card_suc have "Suc ... = card (?e (a Un t) 1)"
```
```   241     by simp
```
```   242   finally show ?case .
```
```   243 qed
```
```   244
```
```   245
```
```   246 subsection {* Main theorem *}
```
```   247
```
```   248 definition mutilated_board :: "nat => nat => (nat * nat) set" where
```
```   249   "mutilated_board m n ==
```
```   250     below (2 * (m + 1)) <*> below (2 * (n + 1))
```
```   251       - {(0, 0)} - {(2 * m + 1, 2 * n + 1)}"
```
```   252
```
```   253 theorem mutil_not_tiling: "mutilated_board m n ~: tiling domino"
```
```   254 proof (unfold mutilated_board_def)
```
```   255   let ?T = "tiling domino"
```
```   256   let ?t = "below (2 * (m + 1)) <*> below (2 * (n + 1))"
```
```   257   let ?t' = "?t - {(0, 0)}"
```
```   258   let ?t'' = "?t' - {(2 * m + 1, 2 * n + 1)}"
```
```   259
```
```   260   show "?t'' ~: ?T"
```
```   261   proof
```
```   262     have t: "?t : ?T" by (rule dominoes_tile_matrix)
```
```   263     assume t'': "?t'' : ?T"
```
```   264
```
```   265     let ?e = evnodd
```
```   266     have fin: "finite (?e ?t 0)"
```
```   267       by (rule evnodd_finite, rule tiling_domino_finite, rule t)
```
```   268
```
```   269     note [simp] = evnodd_iff evnodd_empty evnodd_insert evnodd_Diff
```
```   270     have "card (?e ?t'' 0) < card (?e ?t' 0)"
```
```   271     proof -
```
```   272       have "card (?e ?t' 0 - {(2 * m + 1, 2 * n + 1)})
```
```   273         < card (?e ?t' 0)"
```
```   274       proof (rule card_Diff1_less)
```
```   275         from _ fin show "finite (?e ?t' 0)"
```
```   276           by (rule finite_subset) auto
```
```   277         show "(2 * m + 1, 2 * n + 1) : ?e ?t' 0" by simp
```
```   278       qed
```
```   279       then show ?thesis by simp
```
```   280     qed
```
```   281     also have "... < card (?e ?t 0)"
```
```   282     proof -
```
```   283       have "(0, 0) : ?e ?t 0" by simp
```
```   284       with fin have "card (?e ?t 0 - {(0, 0)}) < card (?e ?t 0)"
```
```   285         by (rule card_Diff1_less)
```
```   286       then show ?thesis by simp
```
```   287     qed
```
```   288     also from t have "... = card (?e ?t 1)"
```
```   289       by (rule tiling_domino_01)
```
```   290     also have "?e ?t 1 = ?e ?t'' 1" by simp
```
```   291     also from t'' have "card ... = card (?e ?t'' 0)"
```
```   292       by (rule tiling_domino_01 [symmetric])
```
```   293     finally have "... < ..." . then show False ..
```
```   294   qed
```
```   295 qed
```
```   296
```
```   297 end
```