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