src/HOL/ex/Set_Theory.thy
changeset 44276 fe769a0fcc96
parent 41460 ea56b98aee83
child 45966 03ce2b2a29a2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/ex/Set_Theory.thy	Thu Aug 18 13:10:24 2011 +0200
     1.3 @@ -0,0 +1,227 @@
     1.4 +(*  Title:      HOL/ex/Set_Theory.thy
     1.5 +    Author:     Tobias Nipkow and Lawrence C Paulson
     1.6 +    Copyright   1991  University of Cambridge
     1.7 +*)
     1.8 +
     1.9 +header {* Set Theory examples: Cantor's Theorem, Schröder-Bernstein Theorem, etc. *}
    1.10 +
    1.11 +theory Set_Theory
    1.12 +imports Main
    1.13 +begin
    1.14 +
    1.15 +text{*
    1.16 +  These two are cited in Benzmueller and Kohlhase's system description
    1.17 +  of LEO, CADE-15, 1998 (pages 139-143) as theorems LEO could not
    1.18 +  prove.
    1.19 +*}
    1.20 +
    1.21 +lemma "(X = Y \<union> Z) =
    1.22 +    (Y \<subseteq> X \<and> Z \<subseteq> X \<and> (\<forall>V. Y \<subseteq> V \<and> Z \<subseteq> V \<longrightarrow> X \<subseteq> V))"
    1.23 +  by blast
    1.24 +
    1.25 +lemma "(X = Y \<inter> Z) =
    1.26 +    (X \<subseteq> Y \<and> X \<subseteq> Z \<and> (\<forall>V. V \<subseteq> Y \<and> V \<subseteq> Z \<longrightarrow> V \<subseteq> X))"
    1.27 +  by blast
    1.28 +
    1.29 +text {*
    1.30 +  Trivial example of term synthesis: apparently hard for some provers!
    1.31 +*}
    1.32 +
    1.33 +schematic_lemma "a \<noteq> b \<Longrightarrow> a \<in> ?X \<and> b \<notin> ?X"
    1.34 +  by blast
    1.35 +
    1.36 +
    1.37 +subsection {* Examples for the @{text blast} paper *}
    1.38 +
    1.39 +lemma "(\<Union>x \<in> C. f x \<union> g x) = \<Union>(f ` C)  \<union>  \<Union>(g ` C)"
    1.40 +  -- {* Union-image, called @{text Un_Union_image} in Main HOL *}
    1.41 +  by blast
    1.42 +
    1.43 +lemma "(\<Inter>x \<in> C. f x \<inter> g x) = \<Inter>(f ` C) \<inter> \<Inter>(g ` C)"
    1.44 +  -- {* Inter-image, called @{text Int_Inter_image} in Main HOL *}
    1.45 +  by blast
    1.46 +
    1.47 +lemma singleton_example_1:
    1.48 +     "\<And>S::'a set set. \<forall>x \<in> S. \<forall>y \<in> S. x \<subseteq> y \<Longrightarrow> \<exists>z. S \<subseteq> {z}"
    1.49 +  by blast
    1.50 +
    1.51 +lemma singleton_example_2:
    1.52 +     "\<forall>x \<in> S. \<Union>S \<subseteq> x \<Longrightarrow> \<exists>z. S \<subseteq> {z}"
    1.53 +  -- {*Variant of the problem above. *}
    1.54 +  by blast
    1.55 +
    1.56 +lemma "\<exists>!x. f (g x) = x \<Longrightarrow> \<exists>!y. g (f y) = y"
    1.57 +  -- {* A unique fixpoint theorem --- @{text fast}/@{text best}/@{text meson} all fail. *}
    1.58 +  by metis
    1.59 +
    1.60 +
    1.61 +subsection {* Cantor's Theorem: There is no surjection from a set to its powerset *}
    1.62 +
    1.63 +lemma cantor1: "\<not> (\<exists>f:: 'a \<Rightarrow> 'a set. \<forall>S. \<exists>x. f x = S)"
    1.64 +  -- {* Requires best-first search because it is undirectional. *}
    1.65 +  by best
    1.66 +
    1.67 +schematic_lemma "\<forall>f:: 'a \<Rightarrow> 'a set. \<forall>x. f x \<noteq> ?S f"
    1.68 +  -- {*This form displays the diagonal term. *}
    1.69 +  by best
    1.70 +
    1.71 +schematic_lemma "?S \<notin> range (f :: 'a \<Rightarrow> 'a set)"
    1.72 +  -- {* This form exploits the set constructs. *}
    1.73 +  by (rule notI, erule rangeE, best)
    1.74 +
    1.75 +schematic_lemma "?S \<notin> range (f :: 'a \<Rightarrow> 'a set)"
    1.76 +  -- {* Or just this! *}
    1.77 +  by best
    1.78 +
    1.79 +
    1.80 +subsection {* The Schröder-Berstein Theorem *}
    1.81 +
    1.82 +lemma disj_lemma: "- (f ` X) = g ` (-X) \<Longrightarrow> f a = g b \<Longrightarrow> a \<in> X \<Longrightarrow> b \<in> X"
    1.83 +  by blast
    1.84 +
    1.85 +lemma surj_if_then_else:
    1.86 +  "-(f ` X) = g ` (-X) \<Longrightarrow> surj (\<lambda>z. if z \<in> X then f z else g z)"
    1.87 +  by (simp add: surj_def) blast
    1.88 +
    1.89 +lemma bij_if_then_else:
    1.90 +  "inj_on f X \<Longrightarrow> inj_on g (-X) \<Longrightarrow> -(f ` X) = g ` (-X) \<Longrightarrow>
    1.91 +    h = (\<lambda>z. if z \<in> X then f z else g z) \<Longrightarrow> inj h \<and> surj h"
    1.92 +  apply (unfold inj_on_def)
    1.93 +  apply (simp add: surj_if_then_else)
    1.94 +  apply (blast dest: disj_lemma sym)
    1.95 +  done
    1.96 +
    1.97 +lemma decomposition: "\<exists>X. X = - (g ` (- (f ` X)))"
    1.98 +  apply (rule exI)
    1.99 +  apply (rule lfp_unfold)
   1.100 +  apply (rule monoI, blast)
   1.101 +  done
   1.102 +
   1.103 +theorem Schroeder_Bernstein:
   1.104 +  "inj (f :: 'a \<Rightarrow> 'b) \<Longrightarrow> inj (g :: 'b \<Rightarrow> 'a)
   1.105 +    \<Longrightarrow> \<exists>h:: 'a \<Rightarrow> 'b. inj h \<and> surj h"
   1.106 +  apply (rule decomposition [where f=f and g=g, THEN exE])
   1.107 +  apply (rule_tac x = "(\<lambda>z. if z \<in> x then f z else inv g z)" in exI) 
   1.108 +    --{*The term above can be synthesized by a sufficiently detailed proof.*}
   1.109 +  apply (rule bij_if_then_else)
   1.110 +     apply (rule_tac [4] refl)
   1.111 +    apply (rule_tac [2] inj_on_inv_into)
   1.112 +    apply (erule subset_inj_on [OF _ subset_UNIV])
   1.113 +   apply blast
   1.114 +  apply (erule ssubst, subst double_complement, erule inv_image_comp [symmetric])
   1.115 +  done
   1.116 +
   1.117 +
   1.118 +subsection {* A simple party theorem *}
   1.119 +
   1.120 +text{* \emph{At any party there are two people who know the same
   1.121 +number of people}. Provided the party consists of at least two people
   1.122 +and the knows relation is symmetric. Knowing yourself does not count
   1.123 +--- otherwise knows needs to be reflexive. (From Freek Wiedijk's talk
   1.124 +at TPHOLs 2007.) *}
   1.125 +
   1.126 +lemma equal_number_of_acquaintances:
   1.127 +assumes "Domain R <= A" and "sym R" and "card A \<ge> 2"
   1.128 +shows "\<not> inj_on (%a. card(R `` {a} - {a})) A"
   1.129 +proof -
   1.130 +  let ?N = "%a. card(R `` {a} - {a})"
   1.131 +  let ?n = "card A"
   1.132 +  have "finite A" using `card A \<ge> 2` by(auto intro:ccontr)
   1.133 +  have 0: "R `` A <= A" using `sym R` `Domain R <= A`
   1.134 +    unfolding Domain_def sym_def by blast
   1.135 +  have h: "ALL a:A. R `` {a} <= A" using 0 by blast
   1.136 +  hence 1: "ALL a:A. finite(R `` {a})" using `finite A`
   1.137 +    by(blast intro: finite_subset)
   1.138 +  have sub: "?N ` A <= {0..<?n}"
   1.139 +  proof -
   1.140 +    have "ALL a:A. R `` {a} - {a} < A" using h by blast
   1.141 +    thus ?thesis using psubset_card_mono[OF `finite A`] by auto
   1.142 +  qed
   1.143 +  show "~ inj_on ?N A" (is "~ ?I")
   1.144 +  proof
   1.145 +    assume ?I
   1.146 +    hence "?n = card(?N ` A)" by(rule card_image[symmetric])
   1.147 +    with sub `finite A` have 2[simp]: "?N ` A = {0..<?n}"
   1.148 +      using subset_card_intvl_is_intvl[of _ 0] by(auto)
   1.149 +    have "0 : ?N ` A" and "?n - 1 : ?N ` A"  using `card A \<ge> 2` by simp+
   1.150 +    then obtain a b where ab: "a:A" "b:A" and Na: "?N a = 0" and Nb: "?N b = ?n - 1"
   1.151 +      by (auto simp del: 2)
   1.152 +    have "a \<noteq> b" using Na Nb `card A \<ge> 2` by auto
   1.153 +    have "R `` {a} - {a} = {}" by (metis 1 Na ab card_eq_0_iff finite_Diff)
   1.154 +    hence "b \<notin> R `` {a}" using `a\<noteq>b` by blast
   1.155 +    hence "a \<notin> R `` {b}" by (metis Image_singleton_iff assms(2) sym_def)
   1.156 +    hence 3: "R `` {b} - {b} <= A - {a,b}" using 0 ab by blast
   1.157 +    have 4: "finite (A - {a,b})" using `finite A` by simp
   1.158 +    have "?N b <= ?n - 2" using ab `a\<noteq>b` `finite A` card_mono[OF 4 3] by simp
   1.159 +    then show False using Nb `card A \<ge>  2` by arith
   1.160 +  qed
   1.161 +qed
   1.162 +
   1.163 +text {*
   1.164 +  From W. W. Bledsoe and Guohui Feng, SET-VAR. JAR 11 (3), 1993, pages
   1.165 +  293-314.
   1.166 +
   1.167 +  Isabelle can prove the easy examples without any special mechanisms,
   1.168 +  but it can't prove the hard ones.
   1.169 +*}
   1.170 +
   1.171 +lemma "\<exists>A. (\<forall>x \<in> A. x \<le> (0::int))"
   1.172 +  -- {* Example 1, page 295. *}
   1.173 +  by force
   1.174 +
   1.175 +lemma "D \<in> F \<Longrightarrow> \<exists>G. \<forall>A \<in> G. \<exists>B \<in> F. A \<subseteq> B"
   1.176 +  -- {* Example 2. *}
   1.177 +  by force
   1.178 +
   1.179 +lemma "P a \<Longrightarrow> \<exists>A. (\<forall>x \<in> A. P x) \<and> (\<exists>y. y \<in> A)"
   1.180 +  -- {* Example 3. *}
   1.181 +  by force
   1.182 +
   1.183 +lemma "a < b \<and> b < (c::int) \<Longrightarrow> \<exists>A. a \<notin> A \<and> b \<in> A \<and> c \<notin> A"
   1.184 +  -- {* Example 4. *}
   1.185 +  by force
   1.186 +
   1.187 +lemma "P (f b) \<Longrightarrow> \<exists>s A. (\<forall>x \<in> A. P x) \<and> f s \<in> A"
   1.188 +  -- {*Example 5, page 298. *}
   1.189 +  by force
   1.190 +
   1.191 +lemma "P (f b) \<Longrightarrow> \<exists>s A. (\<forall>x \<in> A. P x) \<and> f s \<in> A"
   1.192 +  -- {* Example 6. *}
   1.193 +  by force
   1.194 +
   1.195 +lemma "\<exists>A. a \<notin> A"
   1.196 +  -- {* Example 7. *}
   1.197 +  by force
   1.198 +
   1.199 +lemma "(\<forall>u v. u < (0::int) \<longrightarrow> u \<noteq> abs v)
   1.200 +    \<longrightarrow> (\<exists>A::int set. (\<forall>y. abs y \<notin> A) \<and> -2 \<in> A)"
   1.201 +  -- {* Example 8 now needs a small hint. *}
   1.202 +  by (simp add: abs_if, force)
   1.203 +    -- {* not @{text blast}, which can't simplify @{text "-2 < 0"} *}
   1.204 +
   1.205 +text {* Example 9 omitted (requires the reals). *}
   1.206 +
   1.207 +text {* The paper has no Example 10! *}
   1.208 +
   1.209 +lemma "(\<forall>A. 0 \<in> A \<and> (\<forall>x \<in> A. Suc x \<in> A) \<longrightarrow> n \<in> A) \<and>
   1.210 +  P 0 \<and> (\<forall>x. P x \<longrightarrow> P (Suc x)) \<longrightarrow> P n"
   1.211 +  -- {* Example 11: needs a hint. *}
   1.212 +by(metis nat.induct)
   1.213 +
   1.214 +lemma
   1.215 +  "(\<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)
   1.216 +    \<and> P n \<longrightarrow> P m"
   1.217 +  -- {* Example 12. *}
   1.218 +  by auto
   1.219 +
   1.220 +lemma
   1.221 +  "(\<forall>x. (\<exists>u. x = 2 * u) = (\<not> (\<exists>v. Suc x = 2 * v))) \<longrightarrow>
   1.222 +    (\<exists>A. \<forall>x. (x \<in> A) = (Suc x \<notin> A))"
   1.223 +  -- {* Example EO1: typo in article, and with the obvious fix it seems
   1.224 +      to require arithmetic reasoning. *}
   1.225 +  apply clarify
   1.226 +  apply (rule_tac x = "{x. \<exists>u. x = 2 * u}" in exI, auto)
   1.227 +   apply metis+
   1.228 +  done
   1.229 +
   1.230 +end