src/HOL/Isar_Examples/Mutilated_Checkerboard.thy
 changeset 33026 8f35633c4922 parent 32960 69916a850301 child 35416 d8d7d1b785af
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Isar_Examples/Mutilated_Checkerboard.thy	Tue Oct 20 19:37:09 2009 +0200
1.3 @@ -0,0 +1,300 @@
1.4 +(*  Title:      HOL/Isar_Examples/Mutilated_Checkerboard.thy
1.5 +    Author:     Markus Wenzel, TU Muenchen (Isar document)
1.6 +    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory (original scripts)
1.7 +*)
1.8 +
1.9 +header {* The Mutilated Checker Board Problem *}
1.10 +
1.11 +theory Mutilated_Checkerboard
1.12 +imports Main
1.13 +begin
1.14 +
1.15 +text {*
1.16 + The Mutilated Checker Board Problem, formalized inductively.  See
1.17 + \cite{paulson-mutilated-board} and
1.18 + \url{http://isabelle.in.tum.de/library/HOL/Induct/Mutil.html} for the
1.19 + original tactic script version.
1.20 +*}
1.21 +
1.22 +subsection {* Tilings *}
1.23 +
1.24 +inductive_set
1.25 +  tiling :: "'a set set => 'a set set"
1.26 +  for A :: "'a set set"
1.27 +  where
1.28 +    empty: "{} : tiling A"
1.29 +  | Un: "a : A ==> t : tiling A ==> a <= - t ==> a Un t : tiling A"
1.30 +
1.31 +
1.32 +text "The union of two disjoint tilings is a tiling."
1.33 +
1.34 +lemma tiling_Un:
1.35 +  assumes "t : tiling A" and "u : tiling A" and "t Int u = {}"
1.36 +  shows "t Un u : tiling A"
1.37 +proof -
1.38 +  let ?T = "tiling A"
1.39 +  from `t : ?T` and `t Int u = {}`
1.40 +  show "t Un u : ?T"
1.41 +  proof (induct t)
1.42 +    case empty
1.43 +    with `u : ?T` show "{} Un u : ?T" by simp
1.44 +  next
1.45 +    case (Un a t)
1.46 +    show "(a Un t) Un u : ?T"
1.47 +    proof -
1.48 +      have "a Un (t Un u) : ?T"
1.49 +        using `a : A`
1.50 +      proof (rule tiling.Un)
1.51 +        from `(a Un t) Int u = {}` have "t Int u = {}" by blast
1.52 +        then show "t Un u: ?T" by (rule Un)
1.53 +        from `a <= - t` and `(a Un t) Int u = {}`
1.54 +        show "a <= - (t Un u)" by blast
1.55 +      qed
1.56 +      also have "a Un (t Un u) = (a Un t) Un u"
1.57 +        by (simp only: Un_assoc)
1.58 +      finally show ?thesis .
1.59 +    qed
1.60 +  qed
1.61 +qed
1.62 +
1.63 +
1.64 +subsection {* Basic properties of ``below'' *}
1.65 +
1.66 +constdefs
1.67 +  below :: "nat => nat set"
1.68 +  "below n == {i. i < n}"
1.69 +
1.70 +lemma below_less_iff [iff]: "(i: below k) = (i < k)"
1.71 +  by (simp add: below_def)
1.72 +
1.73 +lemma below_0: "below 0 = {}"
1.74 +  by (simp add: below_def)
1.75 +
1.76 +lemma Sigma_Suc1:
1.77 +    "m = n + 1 ==> below m <*> B = ({n} <*> B) Un (below n <*> B)"
1.78 +  by (simp add: below_def less_Suc_eq) blast
1.79 +
1.80 +lemma Sigma_Suc2:
1.81 +    "m = n + 2 ==> A <*> below m =
1.82 +      (A <*> {n}) Un (A <*> {n + 1}) Un (A <*> below n)"
1.83 +  by (auto simp add: below_def)
1.84 +
1.85 +lemmas Sigma_Suc = Sigma_Suc1 Sigma_Suc2
1.86 +
1.87 +
1.88 +subsection {* Basic properties of ``evnodd'' *}
1.89 +
1.90 +constdefs
1.91 +  evnodd :: "(nat * nat) set => nat => (nat * nat) set"
1.92 +  "evnodd A b == A Int {(i, j). (i + j) mod 2 = b}"
1.93 +
1.94 +lemma evnodd_iff:
1.95 +    "(i, j): evnodd A b = ((i, j): A  & (i + j) mod 2 = b)"
1.96 +  by (simp add: evnodd_def)
1.97 +
1.98 +lemma evnodd_subset: "evnodd A b <= A"
1.99 +  by (unfold evnodd_def, rule Int_lower1)
1.100 +
1.101 +lemma evnoddD: "x : evnodd A b ==> x : A"
1.102 +  by (rule subsetD, rule evnodd_subset)
1.103 +
1.104 +lemma evnodd_finite: "finite A ==> finite (evnodd A b)"
1.105 +  by (rule finite_subset, rule evnodd_subset)
1.106 +
1.107 +lemma evnodd_Un: "evnodd (A Un B) b = evnodd A b Un evnodd B b"
1.108 +  by (unfold evnodd_def) blast
1.109 +
1.110 +lemma evnodd_Diff: "evnodd (A - B) b = evnodd A b - evnodd B b"
1.111 +  by (unfold evnodd_def) blast
1.112 +
1.113 +lemma evnodd_empty: "evnodd {} b = {}"
1.114 +  by (simp add: evnodd_def)
1.115 +
1.116 +lemma evnodd_insert: "evnodd (insert (i, j) C) b =
1.117 +    (if (i + j) mod 2 = b
1.118 +      then insert (i, j) (evnodd C b) else evnodd C b)"
1.119 +  by (simp add: evnodd_def)
1.120 +
1.121 +
1.122 +subsection {* Dominoes *}
1.123 +
1.124 +inductive_set
1.125 +  domino :: "(nat * nat) set set"
1.126 +  where
1.127 +    horiz: "{(i, j), (i, j + 1)} : domino"
1.128 +  | vertl: "{(i, j), (i + 1, j)} : domino"
1.129 +
1.130 +lemma dominoes_tile_row:
1.131 +  "{i} <*> below (2 * n) : tiling domino"
1.132 +  (is "?B n : ?T")
1.133 +proof (induct n)
1.134 +  case 0
1.135 +  show ?case by (simp add: below_0 tiling.empty)
1.136 +next
1.137 +  case (Suc n)
1.138 +  let ?a = "{i} <*> {2 * n + 1} Un {i} <*> {2 * n}"
1.139 +  have "?B (Suc n) = ?a Un ?B n"
1.140 +    by (auto simp add: Sigma_Suc Un_assoc)
1.141 +  moreover have "... : ?T"
1.142 +  proof (rule tiling.Un)
1.143 +    have "{(i, 2 * n), (i, 2 * n + 1)} : domino"
1.144 +      by (rule domino.horiz)
1.145 +    also have "{(i, 2 * n), (i, 2 * n + 1)} = ?a" by blast
1.146 +    finally show "... : domino" .
1.147 +    show "?B n : ?T" by (rule Suc)
1.148 +    show "?a <= - ?B n" by blast
1.149 +  qed
1.150 +  ultimately show ?case by simp
1.151 +qed
1.152 +
1.153 +lemma dominoes_tile_matrix:
1.154 +  "below m <*> below (2 * n) : tiling domino"
1.155 +  (is "?B m : ?T")
1.156 +proof (induct m)
1.157 +  case 0
1.158 +  show ?case by (simp add: below_0 tiling.empty)
1.159 +next
1.160 +  case (Suc m)
1.161 +  let ?t = "{m} <*> below (2 * n)"
1.162 +  have "?B (Suc m) = ?t Un ?B m" by (simp add: Sigma_Suc)
1.163 +  moreover have "... : ?T"
1.164 +  proof (rule tiling_Un)
1.165 +    show "?t : ?T" by (rule dominoes_tile_row)
1.166 +    show "?B m : ?T" by (rule Suc)
1.167 +    show "?t Int ?B m = {}" by blast
1.168 +  qed
1.169 +  ultimately show ?case by simp
1.170 +qed
1.171 +
1.172 +lemma domino_singleton:
1.173 +  assumes d: "d : domino" and "b < 2"
1.174 +  shows "EX i j. evnodd d b = {(i, j)}"  (is "?P d")
1.175 +  using d
1.176 +proof induct
1.177 +  from `b < 2` have b_cases: "b = 0 | b = 1" by arith
1.178 +  fix i j
1.179 +  note [simp] = evnodd_empty evnodd_insert mod_Suc
1.180 +  from b_cases show "?P {(i, j), (i, j + 1)}" by rule auto
1.181 +  from b_cases show "?P {(i, j), (i + 1, j)}" by rule auto
1.182 +qed
1.183 +
1.184 +lemma domino_finite:
1.185 +  assumes d: "d: domino"
1.186 +  shows "finite d"
1.187 +  using d
1.188 +proof induct
1.189 +  fix i j :: nat
1.190 +  show "finite {(i, j), (i, j + 1)}" by (intro finite.intros)
1.191 +  show "finite {(i, j), (i + 1, j)}" by (intro finite.intros)
1.192 +qed
1.193 +
1.194 +
1.195 +subsection {* Tilings of dominoes *}
1.196 +
1.197 +lemma tiling_domino_finite:
1.198 +  assumes t: "t : tiling domino"  (is "t : ?T")
1.199 +  shows "finite t"  (is "?F t")
1.200 +  using t
1.201 +proof induct
1.202 +  show "?F {}" by (rule finite.emptyI)
1.203 +  fix a t assume "?F t"
1.204 +  assume "a : domino" then have "?F a" by (rule domino_finite)
1.205 +  from this and `?F t` show "?F (a Un t)" by (rule finite_UnI)
1.206 +qed
1.207 +
1.208 +lemma tiling_domino_01:
1.209 +  assumes t: "t : tiling domino"  (is "t : ?T")
1.210 +  shows "card (evnodd t 0) = card (evnodd t 1)"
1.211 +  using t
1.212 +proof induct
1.213 +  case empty
1.214 +  show ?case by (simp add: evnodd_def)
1.215 +next
1.216 +  case (Un a t)
1.217 +  let ?e = evnodd
1.218 +  note hyp = `card (?e t 0) = card (?e t 1)`
1.219 +    and at = `a <= - t`
1.220 +  have card_suc:
1.221 +    "!!b. b < 2 ==> card (?e (a Un t) b) = Suc (card (?e t b))"
1.222 +  proof -
1.223 +    fix b :: nat assume "b < 2"
1.224 +    have "?e (a Un t) b = ?e a b Un ?e t b" by (rule evnodd_Un)
1.225 +    also obtain i j where e: "?e a b = {(i, j)}"
1.226 +    proof -
1.227 +      from `a \<in> domino` and `b < 2`
1.228 +      have "EX i j. ?e a b = {(i, j)}" by (rule domino_singleton)
1.229 +      then show ?thesis by (blast intro: that)
1.230 +    qed
1.231 +    moreover have "... Un ?e t b = insert (i, j) (?e t b)" by simp
1.232 +    moreover have "card ... = Suc (card (?e t b))"
1.233 +    proof (rule card_insert_disjoint)
1.234 +      from `t \<in> tiling domino` have "finite t"
1.235 +        by (rule tiling_domino_finite)
1.236 +      then show "finite (?e t b)"
1.237 +        by (rule evnodd_finite)
1.238 +      from e have "(i, j) : ?e a b" by simp
1.239 +      with at show "(i, j) ~: ?e t b" by (blast dest: evnoddD)
1.240 +    qed
1.241 +    ultimately show "?thesis b" by simp
1.242 +  qed
1.243 +  then have "card (?e (a Un t) 0) = Suc (card (?e t 0))" by simp
1.244 +  also from hyp have "card (?e t 0) = card (?e t 1)" .
1.245 +  also from card_suc have "Suc ... = card (?e (a Un t) 1)"
1.246 +    by simp
1.247 +  finally show ?case .
1.248 +qed
1.249 +
1.250 +
1.251 +subsection {* Main theorem *}
1.252 +
1.253 +constdefs
1.254 +  mutilated_board :: "nat => nat => (nat * nat) set"
1.255 +  "mutilated_board m n ==
1.256 +    below (2 * (m + 1)) <*> below (2 * (n + 1))
1.257 +      - {(0, 0)} - {(2 * m + 1, 2 * n + 1)}"
1.258 +
1.259 +theorem mutil_not_tiling: "mutilated_board m n ~: tiling domino"
1.260 +proof (unfold mutilated_board_def)
1.261 +  let ?T = "tiling domino"
1.262 +  let ?t = "below (2 * (m + 1)) <*> below (2 * (n + 1))"
1.263 +  let ?t' = "?t - {(0, 0)}"
1.264 +  let ?t'' = "?t' - {(2 * m + 1, 2 * n + 1)}"
1.265 +
1.266 +  show "?t'' ~: ?T"
1.267 +  proof
1.268 +    have t: "?t : ?T" by (rule dominoes_tile_matrix)
1.269 +    assume t'': "?t'' : ?T"
1.270 +
1.271 +    let ?e = evnodd
1.272 +    have fin: "finite (?e ?t 0)"
1.273 +      by (rule evnodd_finite, rule tiling_domino_finite, rule t)
1.274 +
1.275 +    note [simp] = evnodd_iff evnodd_empty evnodd_insert evnodd_Diff
1.276 +    have "card (?e ?t'' 0) < card (?e ?t' 0)"
1.277 +    proof -
1.278 +      have "card (?e ?t' 0 - {(2 * m + 1, 2 * n + 1)})
1.279 +        < card (?e ?t' 0)"
1.280 +      proof (rule card_Diff1_less)
1.281 +        from _ fin show "finite (?e ?t' 0)"
1.282 +          by (rule finite_subset) auto
1.283 +        show "(2 * m + 1, 2 * n + 1) : ?e ?t' 0" by simp
1.284 +      qed
1.285 +      then show ?thesis by simp
1.286 +    qed
1.287 +    also have "... < card (?e ?t 0)"
1.288 +    proof -
1.289 +      have "(0, 0) : ?e ?t 0" by simp
1.290 +      with fin have "card (?e ?t 0 - {(0, 0)}) < card (?e ?t 0)"
1.291 +        by (rule card_Diff1_less)
1.292 +      then show ?thesis by simp
1.293 +    qed
1.294 +    also from t have "... = card (?e ?t 1)"
1.295 +      by (rule tiling_domino_01)
1.296 +    also have "?e ?t 1 = ?e ?t'' 1" by simp
1.297 +    also from t'' have "card ... = card (?e ?t'' 0)"
1.298 +      by (rule tiling_domino_01 [symmetric])
1.299 +    finally have "... < ..." . then show False ..
1.300 +  qed
1.301 +qed
1.302 +
1.303 +end
```