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