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