src/HOL/ex/set.thy
author nipkow
Thu Oct 22 09:27:48 2009 +0200 (2009-10-22)
changeset 33057 764547b68538
parent 32988 d1d4d7a08a66
child 34055 fdf294ee08b2
permissions -rw-r--r--
inv_onto -> inv_into
paulson@13058
     1
(*  Title:      HOL/ex/set.thy
paulson@13058
     2
    ID:         $Id$
paulson@13058
     3
    Author:     Tobias Nipkow and Lawrence C Paulson
paulson@13058
     4
    Copyright   1991  University of Cambridge
wenzelm@13107
     5
*)
paulson@13058
     6
ballarin@19982
     7
header {* Set Theory examples: Cantor's Theorem, Schröder-Bernstein Theorem, etc. *}
wenzelm@9100
     8
haftmann@16417
     9
theory set imports Main begin
wenzelm@9100
    10
wenzelm@13107
    11
text{*
wenzelm@13107
    12
  These two are cited in Benzmueller and Kohlhase's system description
wenzelm@13107
    13
  of LEO, CADE-15, 1998 (pages 139-143) as theorems LEO could not
wenzelm@13107
    14
  prove.
wenzelm@13107
    15
*}
paulson@13058
    16
wenzelm@13107
    17
lemma "(X = Y \<union> Z) =
wenzelm@13107
    18
    (Y \<subseteq> X \<and> Z \<subseteq> X \<and> (\<forall>V. Y \<subseteq> V \<and> Z \<subseteq> V \<longrightarrow> X \<subseteq> V))"
wenzelm@13107
    19
  by blast
paulson@13058
    20
wenzelm@13107
    21
lemma "(X = Y \<inter> Z) =
wenzelm@13107
    22
    (X \<subseteq> Y \<and> X \<subseteq> Z \<and> (\<forall>V. V \<subseteq> Y \<and> V \<subseteq> Z \<longrightarrow> V \<subseteq> X))"
wenzelm@13107
    23
  by blast
paulson@13058
    24
wenzelm@13107
    25
text {*
wenzelm@13107
    26
  Trivial example of term synthesis: apparently hard for some provers!
wenzelm@13107
    27
*}
paulson@13058
    28
wenzelm@13107
    29
lemma "a \<noteq> b \<Longrightarrow> a \<in> ?X \<and> b \<notin> ?X"
wenzelm@13107
    30
  by blast
wenzelm@13107
    31
wenzelm@13107
    32
wenzelm@13107
    33
subsection {* Examples for the @{text blast} paper *}
paulson@13058
    34
wenzelm@13107
    35
lemma "(\<Union>x \<in> C. f x \<union> g x) = \<Union>(f ` C)  \<union>  \<Union>(g ` C)"
wenzelm@13107
    36
  -- {* Union-image, called @{text Un_Union_image} in Main HOL *}
wenzelm@13107
    37
  by blast
paulson@13058
    38
wenzelm@13107
    39
lemma "(\<Inter>x \<in> C. f x \<inter> g x) = \<Inter>(f ` C) \<inter> \<Inter>(g ` C)"
wenzelm@13107
    40
  -- {* Inter-image, called @{text Int_Inter_image} in Main HOL *}
wenzelm@13107
    41
  by blast
paulson@13058
    42
paulson@16898
    43
lemma singleton_example_1:
paulson@16898
    44
     "\<And>S::'a set set. \<forall>x \<in> S. \<forall>y \<in> S. x \<subseteq> y \<Longrightarrow> \<exists>z. S \<subseteq> {z}"
paulson@18391
    45
  by blast
paulson@16898
    46
paulson@16898
    47
lemma singleton_example_2:
paulson@16898
    48
     "\<forall>x \<in> S. \<Union>S \<subseteq> x \<Longrightarrow> \<exists>z. S \<subseteq> {z}"
paulson@16898
    49
  -- {*Variant of the problem above. *}
paulson@18391
    50
  by blast
wenzelm@13107
    51
wenzelm@13107
    52
lemma "\<exists>!x. f (g x) = x \<Longrightarrow> \<exists>!y. g (f y) = y"
wenzelm@13107
    53
  -- {* A unique fixpoint theorem --- @{text fast}/@{text best}/@{text meson} all fail. *}
paulson@24573
    54
  by metis
paulson@13058
    55
paulson@13058
    56
wenzelm@13107
    57
subsection {* Cantor's Theorem: There is no surjection from a set to its powerset *}
paulson@13058
    58
wenzelm@13107
    59
lemma cantor1: "\<not> (\<exists>f:: 'a \<Rightarrow> 'a set. \<forall>S. \<exists>x. f x = S)"
wenzelm@13107
    60
  -- {* Requires best-first search because it is undirectional. *}
wenzelm@13107
    61
  by best
paulson@13058
    62
wenzelm@13107
    63
lemma "\<forall>f:: 'a \<Rightarrow> 'a set. \<forall>x. f x \<noteq> ?S f"
wenzelm@13107
    64
  -- {*This form displays the diagonal term. *}
wenzelm@13107
    65
  by best
paulson@13058
    66
wenzelm@13107
    67
lemma "?S \<notin> range (f :: 'a \<Rightarrow> 'a set)"
wenzelm@13107
    68
  -- {* This form exploits the set constructs. *}
wenzelm@13107
    69
  by (rule notI, erule rangeE, best)
paulson@13058
    70
wenzelm@13107
    71
lemma "?S \<notin> range (f :: 'a \<Rightarrow> 'a set)"
wenzelm@13107
    72
  -- {* Or just this! *}
wenzelm@13107
    73
  by best
wenzelm@13107
    74
paulson@13058
    75
wenzelm@13107
    76
subsection {* The Schröder-Berstein Theorem *}
paulson@13058
    77
wenzelm@13107
    78
lemma disj_lemma: "- (f ` X) = g ` (-X) \<Longrightarrow> f a = g b \<Longrightarrow> a \<in> X \<Longrightarrow> b \<in> X"
wenzelm@13107
    79
  by blast
paulson@13058
    80
paulson@13058
    81
lemma surj_if_then_else:
wenzelm@13107
    82
  "-(f ` X) = g ` (-X) \<Longrightarrow> surj (\<lambda>z. if z \<in> X then f z else g z)"
wenzelm@13107
    83
  by (simp add: surj_def) blast
paulson@13058
    84
wenzelm@13107
    85
lemma bij_if_then_else:
wenzelm@13107
    86
  "inj_on f X \<Longrightarrow> inj_on g (-X) \<Longrightarrow> -(f ` X) = g ` (-X) \<Longrightarrow>
wenzelm@13107
    87
    h = (\<lambda>z. if z \<in> X then f z else g z) \<Longrightarrow> inj h \<and> surj h"
wenzelm@13107
    88
  apply (unfold inj_on_def)
wenzelm@13107
    89
  apply (simp add: surj_if_then_else)
wenzelm@13107
    90
  apply (blast dest: disj_lemma sym)
wenzelm@13107
    91
  done
paulson@13058
    92
wenzelm@13107
    93
lemma decomposition: "\<exists>X. X = - (g ` (- (f ` X)))"
wenzelm@13107
    94
  apply (rule exI)
wenzelm@13107
    95
  apply (rule lfp_unfold)
wenzelm@13107
    96
  apply (rule monoI, blast)
wenzelm@13107
    97
  done
paulson@13058
    98
wenzelm@13107
    99
theorem Schroeder_Bernstein:
wenzelm@13107
   100
  "inj (f :: 'a \<Rightarrow> 'b) \<Longrightarrow> inj (g :: 'b \<Rightarrow> 'a)
wenzelm@13107
   101
    \<Longrightarrow> \<exists>h:: 'a \<Rightarrow> 'b. inj h \<and> surj h"
paulson@15488
   102
  apply (rule decomposition [where f=f and g=g, THEN exE])
paulson@15488
   103
  apply (rule_tac x = "(\<lambda>z. if z \<in> x then f z else inv g z)" in exI) 
paulson@15488
   104
    --{*The term above can be synthesized by a sufficiently detailed proof.*}
wenzelm@13107
   105
  apply (rule bij_if_then_else)
wenzelm@13107
   106
     apply (rule_tac [4] refl)
nipkow@33057
   107
    apply (rule_tac [2] inj_on_inv_into)
nipkow@15306
   108
    apply (erule subset_inj_on [OF _ subset_UNIV])
paulson@15488
   109
   apply blast
paulson@15488
   110
  apply (erule ssubst, subst double_complement, erule inv_image_comp [symmetric])
wenzelm@13107
   111
  done
paulson@13058
   112
paulson@13058
   113
nipkow@24853
   114
subsection {* A simple party theorem *}
nipkow@24853
   115
nipkow@24853
   116
text{* \emph{At any party there are two people who know the same
nipkow@24853
   117
number of people}. Provided the party consists of at least two people
nipkow@24853
   118
and the knows relation is symmetric. Knowing yourself does not count
nipkow@24853
   119
--- otherwise knows needs to be reflexive. (From Freek Wiedijk's talk
nipkow@24853
   120
at TPHOLs 2007.) *}
nipkow@24853
   121
nipkow@24853
   122
lemma equal_number_of_acquaintances:
nipkow@24853
   123
assumes "Domain R <= A" and "sym R" and "card A \<ge> 2"
nipkow@24853
   124
shows "\<not> inj_on (%a. card(R `` {a} - {a})) A"
nipkow@24853
   125
proof -
nipkow@24853
   126
  let ?N = "%a. card(R `` {a} - {a})"
nipkow@24853
   127
  let ?n = "card A"
nipkow@24853
   128
  have "finite A" using `card A \<ge> 2` by(auto intro:ccontr)
nipkow@24853
   129
  have 0: "R `` A <= A" using `sym R` `Domain R <= A`
nipkow@24853
   130
    unfolding Domain_def sym_def by blast
nipkow@24853
   131
  have h: "ALL a:A. R `` {a} <= A" using 0 by blast
nipkow@24853
   132
  hence 1: "ALL a:A. finite(R `` {a})" using `finite A`
nipkow@24853
   133
    by(blast intro: finite_subset)
nipkow@24853
   134
  have sub: "?N ` A <= {0..<?n}"
nipkow@24853
   135
  proof -
nipkow@24853
   136
    have "ALL a:A. R `` {a} - {a} < A" using h by blast
nipkow@24853
   137
    thus ?thesis using psubset_card_mono[OF `finite A`] by auto
nipkow@24853
   138
  qed
nipkow@24853
   139
  show "~ inj_on ?N A" (is "~ ?I")
nipkow@24853
   140
  proof
nipkow@24853
   141
    assume ?I
nipkow@24853
   142
    hence "?n = card(?N ` A)" by(rule card_image[symmetric])
nipkow@24853
   143
    with sub `finite A` have 2[simp]: "?N ` A = {0..<?n}"
nipkow@24853
   144
      using subset_card_intvl_is_intvl[of _ 0] by(auto)
nipkow@24853
   145
    have "0 : ?N ` A" and "?n - 1 : ?N ` A"  using `card A \<ge> 2` by simp+
nipkow@24853
   146
    then obtain a b where ab: "a:A" "b:A" and Na: "?N a = 0" and Nb: "?N b = ?n - 1"
nipkow@24853
   147
      by (auto simp del: 2)
nipkow@24853
   148
    have "a \<noteq> b" using Na Nb `card A \<ge> 2` by auto
nipkow@24853
   149
    have "R `` {a} - {a} = {}" by (metis 1 Na ab card_eq_0_iff finite_Diff)
nipkow@24853
   150
    hence "b \<notin> R `` {a}" using `a\<noteq>b` by blast
nipkow@24853
   151
    hence "a \<notin> R `` {b}" by (metis Image_singleton_iff assms(2) sym_def)
nipkow@24853
   152
    hence 3: "R `` {b} - {b} <= A - {a,b}" using 0 ab by blast
nipkow@24853
   153
    have 4: "finite (A - {a,b})" using `finite A` by simp
nipkow@24853
   154
    have "?N b <= ?n - 2" using ab `a\<noteq>b` `finite A` card_mono[OF 4 3] by simp
nipkow@24853
   155
    then show False using Nb `card A \<ge>  2` by arith
nipkow@24853
   156
  qed
nipkow@24853
   157
qed
nipkow@24853
   158
wenzelm@13107
   159
text {*
wenzelm@13107
   160
  From W. W. Bledsoe and Guohui Feng, SET-VAR. JAR 11 (3), 1993, pages
wenzelm@13107
   161
  293-314.
wenzelm@13107
   162
wenzelm@13107
   163
  Isabelle can prove the easy examples without any special mechanisms,
wenzelm@13107
   164
  but it can't prove the hard ones.
paulson@13058
   165
*}
paulson@13058
   166
wenzelm@13107
   167
lemma "\<exists>A. (\<forall>x \<in> A. x \<le> (0::int))"
wenzelm@13107
   168
  -- {* Example 1, page 295. *}
wenzelm@13107
   169
  by force
paulson@13058
   170
wenzelm@13107
   171
lemma "D \<in> F \<Longrightarrow> \<exists>G. \<forall>A \<in> G. \<exists>B \<in> F. A \<subseteq> B"
wenzelm@13107
   172
  -- {* Example 2. *}
wenzelm@13107
   173
  by force
paulson@13058
   174
wenzelm@13107
   175
lemma "P a \<Longrightarrow> \<exists>A. (\<forall>x \<in> A. P x) \<and> (\<exists>y. y \<in> A)"
wenzelm@13107
   176
  -- {* Example 3. *}
wenzelm@13107
   177
  by force
paulson@13058
   178
wenzelm@13107
   179
lemma "a < b \<and> b < (c::int) \<Longrightarrow> \<exists>A. a \<notin> A \<and> b \<in> A \<and> c \<notin> A"
wenzelm@13107
   180
  -- {* Example 4. *}
wenzelm@13107
   181
  by force
paulson@13058
   182
wenzelm@13107
   183
lemma "P (f b) \<Longrightarrow> \<exists>s A. (\<forall>x \<in> A. P x) \<and> f s \<in> A"
wenzelm@13107
   184
  -- {*Example 5, page 298. *}
wenzelm@13107
   185
  by force
paulson@13058
   186
wenzelm@13107
   187
lemma "P (f b) \<Longrightarrow> \<exists>s A. (\<forall>x \<in> A. P x) \<and> f s \<in> A"
wenzelm@13107
   188
  -- {* Example 6. *}
wenzelm@13107
   189
  by force
paulson@13058
   190
wenzelm@13107
   191
lemma "\<exists>A. a \<notin> A"
wenzelm@13107
   192
  -- {* Example 7. *}
wenzelm@13107
   193
  by force
paulson@13058
   194
wenzelm@13107
   195
lemma "(\<forall>u v. u < (0::int) \<longrightarrow> u \<noteq> abs v)
wenzelm@13107
   196
    \<longrightarrow> (\<exists>A::int set. (\<forall>y. abs y \<notin> A) \<and> -2 \<in> A)"
paulson@14353
   197
  -- {* Example 8 now needs a small hint. *}
paulson@14353
   198
  by (simp add: abs_if, force)
paulson@14353
   199
    -- {* not @{text blast}, which can't simplify @{text "-2 < 0"} *}
paulson@13058
   200
wenzelm@13107
   201
text {* Example 9 omitted (requires the reals). *}
paulson@13058
   202
wenzelm@13107
   203
text {* The paper has no Example 10! *}
paulson@13058
   204
wenzelm@13107
   205
lemma "(\<forall>A. 0 \<in> A \<and> (\<forall>x \<in> A. Suc x \<in> A) \<longrightarrow> n \<in> A) \<and>
wenzelm@13107
   206
  P 0 \<and> (\<forall>x. P x \<longrightarrow> P (Suc x)) \<longrightarrow> P n"
wenzelm@13107
   207
  -- {* Example 11: needs a hint. *}
wenzelm@13107
   208
  apply clarify
wenzelm@13107
   209
  apply (drule_tac x = "{x. P x}" in spec)
wenzelm@13107
   210
  apply force
wenzelm@13107
   211
  done
paulson@13058
   212
wenzelm@13107
   213
lemma
wenzelm@13107
   214
  "(\<forall>A. (0, 0) \<in> A \<and> (\<forall>x y. (x, y) \<in> A \<longrightarrow> (Suc x, Suc y) \<in> A) \<longrightarrow> (n, m) \<in> A)
wenzelm@13107
   215
    \<and> P n \<longrightarrow> P m"
wenzelm@13107
   216
  -- {* Example 12. *}
wenzelm@13107
   217
  by auto
paulson@13058
   218
wenzelm@13107
   219
lemma
wenzelm@13107
   220
  "(\<forall>x. (\<exists>u. x = 2 * u) = (\<not> (\<exists>v. Suc x = 2 * v))) \<longrightarrow>
wenzelm@13107
   221
    (\<exists>A. \<forall>x. (x \<in> A) = (Suc x \<notin> A))"
wenzelm@13107
   222
  -- {* Example EO1: typo in article, and with the obvious fix it seems
wenzelm@13107
   223
      to require arithmetic reasoning. *}
wenzelm@13107
   224
  apply clarify
wenzelm@13107
   225
  apply (rule_tac x = "{x. \<exists>u. x = 2 * u}" in exI, auto)
wenzelm@13107
   226
   apply (case_tac v, auto)
wenzelm@13107
   227
  apply (drule_tac x = "Suc v" and P = "\<lambda>x. ?a x \<noteq> ?b x" in spec, force)
wenzelm@13107
   228
  done
paulson@13058
   229
wenzelm@9100
   230
end