src/HOL/Hilbert_Choice.thy
author hoelzl
Fri Feb 19 13:40:50 2016 +0100 (2016-02-19)
changeset 62378 85ed00c1fe7c
parent 62343 24106dc44def
child 62521 6383440f41a8
permissions -rw-r--r--
generalize more theorems to support enat and ennreal
     1 (*  Title:      HOL/Hilbert_Choice.thy
     2     Author:     Lawrence C Paulson, Tobias Nipkow
     3     Copyright   2001  University of Cambridge
     4 *)
     5 
     6 section \<open>Hilbert's Epsilon-Operator and the Axiom of Choice\<close>
     7 
     8 theory Hilbert_Choice
     9 imports Wellfounded
    10 keywords "specification" :: thy_goal
    11 begin
    12 
    13 subsection \<open>Hilbert's epsilon\<close>
    14 
    15 axiomatization Eps :: "('a => bool) => 'a" where
    16   someI: "P x ==> P (Eps P)"
    17 
    18 syntax (epsilon)
    19   "_Eps"        :: "[pttrn, bool] => 'a"    ("(3\<some>_./ _)" [0, 10] 10)
    20 syntax (HOL)
    21   "_Eps"        :: "[pttrn, bool] => 'a"    ("(3@ _./ _)" [0, 10] 10)
    22 syntax
    23   "_Eps"        :: "[pttrn, bool] => 'a"    ("(3SOME _./ _)" [0, 10] 10)
    24 translations
    25   "SOME x. P" == "CONST Eps (%x. P)"
    26 
    27 print_translation \<open>
    28   [(@{const_syntax Eps}, fn _ => fn [Abs abs] =>
    29       let val (x, t) = Syntax_Trans.atomic_abs_tr' abs
    30       in Syntax.const @{syntax_const "_Eps"} $ x $ t end)]
    31 \<close> \<comment> \<open>to avoid eta-contraction of body\<close>
    32 
    33 definition inv_into :: "'a set => ('a => 'b) => ('b => 'a)" where
    34 "inv_into A f == %x. SOME y. y : A & f y = x"
    35 
    36 abbreviation inv :: "('a => 'b) => ('b => 'a)" where
    37 "inv == inv_into UNIV"
    38 
    39 
    40 subsection \<open>Hilbert's Epsilon-operator\<close>
    41 
    42 text\<open>Easier to apply than \<open>someI\<close> if the witness comes from an
    43 existential formula\<close>
    44 lemma someI_ex [elim?]: "\<exists>x. P x ==> P (SOME x. P x)"
    45 apply (erule exE)
    46 apply (erule someI)
    47 done
    48 
    49 text\<open>Easier to apply than \<open>someI\<close> because the conclusion has only one
    50 occurrence of @{term P}.\<close>
    51 lemma someI2: "[| P a;  !!x. P x ==> Q x |] ==> Q (SOME x. P x)"
    52   by (blast intro: someI)
    53 
    54 text\<open>Easier to apply than \<open>someI2\<close> if the witness comes from an
    55 existential formula\<close>
    56 
    57 lemma someI2_ex: "[| \<exists>a. P a; !!x. P x ==> Q x |] ==> Q (SOME x. P x)"
    58   by (blast intro: someI2)
    59 
    60 lemma someI2_bex: "[| \<exists>a\<in>A. P a; !!x. x \<in> A \<and> P x ==> Q x |] ==> Q (SOME x. x \<in> A \<and> P x)"
    61   by (blast intro: someI2)
    62 
    63 lemma some_equality [intro]:
    64      "[| P a;  !!x. P x ==> x=a |] ==> (SOME x. P x) = a"
    65 by (blast intro: someI2)
    66 
    67 lemma some1_equality: "[| EX!x. P x; P a |] ==> (SOME x. P x) = a"
    68 by blast
    69 
    70 lemma some_eq_ex: "P (SOME x. P x) =  (\<exists>x. P x)"
    71 by (blast intro: someI)
    72 
    73 lemma some_in_eq: "(SOME x. x \<in> A) \<in> A \<longleftrightarrow> A \<noteq> {}"
    74   unfolding ex_in_conv[symmetric] by (rule some_eq_ex)
    75 
    76 lemma some_eq_trivial [simp]: "(SOME y. y=x) = x"
    77 apply (rule some_equality)
    78 apply (rule refl, assumption)
    79 done
    80 
    81 lemma some_sym_eq_trivial [simp]: "(SOME y. x=y) = x"
    82 apply (rule some_equality)
    83 apply (rule refl)
    84 apply (erule sym)
    85 done
    86 
    87 
    88 subsection\<open>Axiom of Choice, Proved Using the Description Operator\<close>
    89 
    90 lemma choice: "\<forall>x. \<exists>y. Q x y ==> \<exists>f. \<forall>x. Q x (f x)"
    91 by (fast elim: someI)
    92 
    93 lemma bchoice: "\<forall>x\<in>S. \<exists>y. Q x y ==> \<exists>f. \<forall>x\<in>S. Q x (f x)"
    94 by (fast elim: someI)
    95 
    96 lemma choice_iff: "(\<forall>x. \<exists>y. Q x y) \<longleftrightarrow> (\<exists>f. \<forall>x. Q x (f x))"
    97 by (fast elim: someI)
    98 
    99 lemma choice_iff': "(\<forall>x. P x \<longrightarrow> (\<exists>y. Q x y)) \<longleftrightarrow> (\<exists>f. \<forall>x. P x \<longrightarrow> Q x (f x))"
   100 by (fast elim: someI)
   101 
   102 lemma bchoice_iff: "(\<forall>x\<in>S. \<exists>y. Q x y) \<longleftrightarrow> (\<exists>f. \<forall>x\<in>S. Q x (f x))"
   103 by (fast elim: someI)
   104 
   105 lemma bchoice_iff': "(\<forall>x\<in>S. P x \<longrightarrow> (\<exists>y. Q x y)) \<longleftrightarrow> (\<exists>f. \<forall>x\<in>S. P x \<longrightarrow> Q x (f x))"
   106 by (fast elim: someI)
   107 
   108 lemma dependent_nat_choice:
   109   assumes  1: "\<exists>x. P 0 x" and
   110            2: "\<And>x n. P n x \<Longrightarrow> \<exists>y. P (Suc n) y \<and> Q n x y"
   111   shows "\<exists>f. \<forall>n. P n (f n) \<and> Q n (f n) (f (Suc n))"
   112 proof (intro exI allI conjI)
   113   fix n def f \<equiv> "rec_nat (SOME x. P 0 x) (\<lambda>n x. SOME y. P (Suc n) y \<and> Q n x y)"
   114   have "P 0 (f 0)" "\<And>n. P n (f n) \<Longrightarrow> P (Suc n) (f (Suc n)) \<and> Q n (f n) (f (Suc n))"
   115     using someI_ex[OF 1] someI_ex[OF 2] by (simp_all add: f_def)
   116   then show "P n (f n)" "Q n (f n) (f (Suc n))"
   117     by (induct n) auto
   118 qed
   119 
   120 
   121 subsection \<open>Function Inverse\<close>
   122 
   123 lemma inv_def: "inv f = (%y. SOME x. f x = y)"
   124 by(simp add: inv_into_def)
   125 
   126 lemma inv_into_into: "x : f ` A ==> inv_into A f x : A"
   127 apply (simp add: inv_into_def)
   128 apply (fast intro: someI2)
   129 done
   130 
   131 lemma inv_id [simp]: "inv id = id"
   132 by (simp add: inv_into_def id_def)
   133 
   134 lemma inv_into_f_f [simp]:
   135   "[| inj_on f A;  x : A |] ==> inv_into A f (f x) = x"
   136 apply (simp add: inv_into_def inj_on_def)
   137 apply (blast intro: someI2)
   138 done
   139 
   140 lemma inv_f_f: "inj f ==> inv f (f x) = x"
   141 by simp
   142 
   143 lemma f_inv_into_f: "y : f`A  ==> f (inv_into A f y) = y"
   144 apply (simp add: inv_into_def)
   145 apply (fast intro: someI2)
   146 done
   147 
   148 lemma inv_into_f_eq: "[| inj_on f A; x : A; f x = y |] ==> inv_into A f y = x"
   149 apply (erule subst)
   150 apply (fast intro: inv_into_f_f)
   151 done
   152 
   153 lemma inv_f_eq: "[| inj f; f x = y |] ==> inv f y = x"
   154 by (simp add:inv_into_f_eq)
   155 
   156 lemma inj_imp_inv_eq: "[| inj f; ALL x. f(g x) = x |] ==> inv f = g"
   157   by (blast intro: inv_into_f_eq)
   158 
   159 text\<open>But is it useful?\<close>
   160 lemma inj_transfer:
   161   assumes injf: "inj f" and minor: "!!y. y \<in> range(f) ==> P(inv f y)"
   162   shows "P x"
   163 proof -
   164   have "f x \<in> range f" by auto
   165   hence "P(inv f (f x))" by (rule minor)
   166   thus "P x" by (simp add: inv_into_f_f [OF injf])
   167 qed
   168 
   169 lemma inj_iff: "(inj f) = (inv f o f = id)"
   170 apply (simp add: o_def fun_eq_iff)
   171 apply (blast intro: inj_on_inverseI inv_into_f_f)
   172 done
   173 
   174 lemma inv_o_cancel[simp]: "inj f ==> inv f o f = id"
   175 by (simp add: inj_iff)
   176 
   177 lemma o_inv_o_cancel[simp]: "inj f ==> g o inv f o f = g"
   178 by (simp add: comp_assoc)
   179 
   180 lemma inv_into_image_cancel[simp]:
   181   "inj_on f A ==> S <= A ==> inv_into A f ` f ` S = S"
   182 by(fastforce simp: image_def)
   183 
   184 lemma inj_imp_surj_inv: "inj f ==> surj (inv f)"
   185 by (blast intro!: surjI inv_into_f_f)
   186 
   187 lemma surj_f_inv_f: "surj f ==> f(inv f y) = y"
   188 by (simp add: f_inv_into_f)
   189 
   190 lemma inv_into_injective:
   191   assumes eq: "inv_into A f x = inv_into A f y"
   192       and x: "x: f`A"
   193       and y: "y: f`A"
   194   shows "x=y"
   195 proof -
   196   have "f (inv_into A f x) = f (inv_into A f y)" using eq by simp
   197   thus ?thesis by (simp add: f_inv_into_f x y)
   198 qed
   199 
   200 lemma inj_on_inv_into: "B <= f`A ==> inj_on (inv_into A f) B"
   201 by (blast intro: inj_onI dest: inv_into_injective injD)
   202 
   203 lemma bij_betw_inv_into: "bij_betw f A B ==> bij_betw (inv_into A f) B A"
   204 by (auto simp add: bij_betw_def inj_on_inv_into)
   205 
   206 lemma surj_imp_inj_inv: "surj f ==> inj (inv f)"
   207 by (simp add: inj_on_inv_into)
   208 
   209 lemma surj_iff: "(surj f) = (f o inv f = id)"
   210 by (auto intro!: surjI simp: surj_f_inv_f fun_eq_iff[where 'b='a])
   211 
   212 lemma surj_iff_all: "surj f \<longleftrightarrow> (\<forall>x. f (inv f x) = x)"
   213   unfolding surj_iff by (simp add: o_def fun_eq_iff)
   214 
   215 lemma surj_imp_inv_eq: "[| surj f; \<forall>x. g(f x) = x |] ==> inv f = g"
   216 apply (rule ext)
   217 apply (drule_tac x = "inv f x" in spec)
   218 apply (simp add: surj_f_inv_f)
   219 done
   220 
   221 lemma bij_imp_bij_inv: "bij f ==> bij (inv f)"
   222 by (simp add: bij_def inj_imp_surj_inv surj_imp_inj_inv)
   223 
   224 lemma inv_equality: "[| !!x. g (f x) = x;  !!y. f (g y) = y |] ==> inv f = g"
   225 apply (rule ext)
   226 apply (auto simp add: inv_into_def)
   227 done
   228 
   229 lemma inv_inv_eq: "bij f ==> inv (inv f) = f"
   230 apply (rule inv_equality)
   231 apply (auto simp add: bij_def surj_f_inv_f)
   232 done
   233 
   234 (** bij(inv f) implies little about f.  Consider f::bool=>bool such that
   235     f(True)=f(False)=True.  Then it's consistent with axiom someI that
   236     inv f could be any function at all, including the identity function.
   237     If inv f=id then inv f is a bijection, but inj f, surj(f) and
   238     inv(inv f)=f all fail.
   239 **)
   240 
   241 lemma inv_into_comp:
   242   "[| inj_on f (g ` A); inj_on g A; x : f ` g ` A |] ==>
   243   inv_into A (f o g) x = (inv_into A g o inv_into (g ` A) f) x"
   244 apply (rule inv_into_f_eq)
   245   apply (fast intro: comp_inj_on)
   246  apply (simp add: inv_into_into)
   247 apply (simp add: f_inv_into_f inv_into_into)
   248 done
   249 
   250 lemma o_inv_distrib: "[| bij f; bij g |] ==> inv (f o g) = inv g o inv f"
   251 apply (rule inv_equality)
   252 apply (auto simp add: bij_def surj_f_inv_f)
   253 done
   254 
   255 lemma image_surj_f_inv_f: "surj f ==> f ` (inv f ` A) = A"
   256   by (simp add: surj_f_inv_f image_comp comp_def)
   257 
   258 lemma image_inv_f_f: "inj f ==> inv f ` (f ` A) = A"
   259   by simp
   260 
   261 lemma inv_image_comp: "inj f ==> inv f ` (f ` X) = X"
   262   by (fact image_inv_f_f)
   263 
   264 lemma bij_image_Collect_eq: "bij f ==> f ` Collect P = {y. P (inv f y)}"
   265 apply auto
   266 apply (force simp add: bij_is_inj)
   267 apply (blast intro: bij_is_surj [THEN surj_f_inv_f, symmetric])
   268 done
   269 
   270 lemma bij_vimage_eq_inv_image: "bij f ==> f -` A = inv f ` A"
   271 apply (auto simp add: bij_is_surj [THEN surj_f_inv_f])
   272 apply (blast intro: bij_is_inj [THEN inv_into_f_f, symmetric])
   273 done
   274 
   275 lemma finite_fun_UNIVD1:
   276   assumes fin: "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
   277   and card: "card (UNIV :: 'b set) \<noteq> Suc 0"
   278   shows "finite (UNIV :: 'a set)"
   279 proof -
   280   from fin have finb: "finite (UNIV :: 'b set)" by (rule finite_fun_UNIVD2)
   281   with card have "card (UNIV :: 'b set) \<ge> Suc (Suc 0)"
   282     by (cases "card (UNIV :: 'b set)") (auto simp add: card_eq_0_iff)
   283   then obtain n where "card (UNIV :: 'b set) = Suc (Suc n)" "n = card (UNIV :: 'b set) - Suc (Suc 0)" by auto
   284   then obtain b1 b2 where b1b2: "(b1 :: 'b) \<noteq> (b2 :: 'b)" by (auto simp add: card_Suc_eq)
   285   from fin have "finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. inv f b1))" by (rule finite_imageI)
   286   moreover have "UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. inv f b1)"
   287   proof (rule UNIV_eq_I)
   288     fix x :: 'a
   289     from b1b2 have "x = inv (\<lambda>y. if y = x then b1 else b2) b1" by (simp add: inv_into_def)
   290     thus "x \<in> range (\<lambda>f::'a \<Rightarrow> 'b. inv f b1)" by blast
   291   qed
   292   ultimately show "finite (UNIV :: 'a set)" by simp
   293 qed
   294 
   295 text \<open>
   296   Every infinite set contains a countable subset. More precisely we
   297   show that a set \<open>S\<close> is infinite if and only if there exists an
   298   injective function from the naturals into \<open>S\<close>.
   299 
   300   The ``only if'' direction is harder because it requires the
   301   construction of a sequence of pairwise different elements of an
   302   infinite set \<open>S\<close>. The idea is to construct a sequence of
   303   non-empty and infinite subsets of \<open>S\<close> obtained by successively
   304   removing elements of \<open>S\<close>.
   305 \<close>
   306 
   307 lemma infinite_countable_subset:
   308   assumes inf: "\<not> finite (S::'a set)"
   309   shows "\<exists>f. inj (f::nat \<Rightarrow> 'a) \<and> range f \<subseteq> S"
   310   \<comment> \<open>Courtesy of Stephan Merz\<close>
   311 proof -
   312   def Sseq \<equiv> "rec_nat S (\<lambda>n T. T - {SOME e. e \<in> T})"
   313   def pick \<equiv> "\<lambda>n. (SOME e. e \<in> Sseq n)"
   314   { fix n have "Sseq n \<subseteq> S" "\<not> finite (Sseq n)" by (induct n) (auto simp add: Sseq_def inf) }
   315   moreover then have *: "\<And>n. pick n \<in> Sseq n"
   316     unfolding pick_def by (subst (asm) finite.simps) (auto simp add: ex_in_conv intro: someI_ex)
   317   ultimately have "range pick \<subseteq> S" by auto
   318   moreover
   319   { fix n m
   320     have "pick n \<notin> Sseq (n + Suc m)" by (induct m) (auto simp add: Sseq_def pick_def)
   321     with * have "pick n \<noteq> pick (n + Suc m)" by auto
   322   }
   323   then have "inj pick" by (intro linorder_injI) (auto simp add: less_iff_Suc_add)
   324   ultimately show ?thesis by blast
   325 qed
   326 
   327 lemma infinite_iff_countable_subset: "\<not> finite S \<longleftrightarrow> (\<exists>f. inj (f::nat \<Rightarrow> 'a) \<and> range f \<subseteq> S)"
   328   \<comment> \<open>Courtesy of Stephan Merz\<close>
   329   using finite_imageD finite_subset infinite_UNIV_char_0 infinite_countable_subset by auto
   330 
   331 lemma image_inv_into_cancel:
   332   assumes SURJ: "f`A=A'" and SUB: "B' \<le> A'"
   333   shows "f `((inv_into A f)`B') = B'"
   334   using assms
   335 proof (auto simp add: f_inv_into_f)
   336   let ?f' = "(inv_into A f)"
   337   fix a' assume *: "a' \<in> B'"
   338   then have "a' \<in> A'" using SUB by auto
   339   then have "a' = f (?f' a')"
   340     using SURJ by (auto simp add: f_inv_into_f)
   341   then show "a' \<in> f ` (?f' ` B')" using * by blast
   342 qed
   343 
   344 lemma inv_into_inv_into_eq:
   345   assumes "bij_betw f A A'" "a \<in> A"
   346   shows "inv_into A' (inv_into A f) a = f a"
   347 proof -
   348   let ?f' = "inv_into A f"   let ?f'' = "inv_into A' ?f'"
   349   have 1: "bij_betw ?f' A' A" using assms
   350   by (auto simp add: bij_betw_inv_into)
   351   obtain a' where 2: "a' \<in> A'" and 3: "?f' a' = a"
   352     using 1 \<open>a \<in> A\<close> unfolding bij_betw_def by force
   353   hence "?f'' a = a'"
   354     using \<open>a \<in> A\<close> 1 3 by (auto simp add: f_inv_into_f bij_betw_def)
   355   moreover have "f a = a'" using assms 2 3
   356     by (auto simp add: bij_betw_def)
   357   ultimately show "?f'' a = f a" by simp
   358 qed
   359 
   360 lemma inj_on_iff_surj:
   361   assumes "A \<noteq> {}"
   362   shows "(\<exists>f. inj_on f A \<and> f ` A \<le> A') \<longleftrightarrow> (\<exists>g. g ` A' = A)"
   363 proof safe
   364   fix f assume INJ: "inj_on f A" and INCL: "f ` A \<le> A'"
   365   let ?phi = "\<lambda>a' a. a \<in> A \<and> f a = a'"  let ?csi = "\<lambda>a. a \<in> A"
   366   let ?g = "\<lambda>a'. if a' \<in> f ` A then (SOME a. ?phi a' a) else (SOME a. ?csi a)"
   367   have "?g ` A' = A"
   368   proof
   369     show "?g ` A' \<le> A"
   370     proof clarify
   371       fix a' assume *: "a' \<in> A'"
   372       show "?g a' \<in> A"
   373       proof cases
   374         assume Case1: "a' \<in> f ` A"
   375         then obtain a where "?phi a' a" by blast
   376         hence "?phi a' (SOME a. ?phi a' a)" using someI[of "?phi a'" a] by blast
   377         with Case1 show ?thesis by auto
   378       next
   379         assume Case2: "a' \<notin> f ` A"
   380         hence "?csi (SOME a. ?csi a)" using assms someI_ex[of ?csi] by blast
   381         with Case2 show ?thesis by auto
   382       qed
   383     qed
   384   next
   385     show "A \<le> ?g ` A'"
   386     proof-
   387       {fix a assume *: "a \<in> A"
   388        let ?b = "SOME aa. ?phi (f a) aa"
   389        have "?phi (f a) a" using * by auto
   390        hence 1: "?phi (f a) ?b" using someI[of "?phi(f a)" a] by blast
   391        hence "?g(f a) = ?b" using * by auto
   392        moreover have "a = ?b" using 1 INJ * by (auto simp add: inj_on_def)
   393        ultimately have "?g(f a) = a" by simp
   394        with INCL * have "?g(f a) = a \<and> f a \<in> A'" by auto
   395       }
   396       thus ?thesis by force
   397     qed
   398   qed
   399   thus "\<exists>g. g ` A' = A" by blast
   400 next
   401   fix g  let ?f = "inv_into A' g"
   402   have "inj_on ?f (g ` A')"
   403     by (auto simp add: inj_on_inv_into)
   404   moreover
   405   {fix a' assume *: "a' \<in> A'"
   406    let ?phi = "\<lambda> b'. b' \<in> A' \<and> g b' = g a'"
   407    have "?phi a'" using * by auto
   408    hence "?phi(SOME b'. ?phi b')" using someI[of ?phi] by blast
   409    hence "?f(g a') \<in> A'" unfolding inv_into_def by auto
   410   }
   411   ultimately show "\<exists>f. inj_on f (g ` A') \<and> f ` g ` A' \<subseteq> A'" by auto
   412 qed
   413 
   414 lemma Ex_inj_on_UNION_Sigma:
   415   "\<exists>f. (inj_on f (\<Union>i \<in> I. A i) \<and> f ` (\<Union>i \<in> I. A i) \<le> (SIGMA i : I. A i))"
   416 proof
   417   let ?phi = "\<lambda> a i. i \<in> I \<and> a \<in> A i"
   418   let ?sm = "\<lambda> a. SOME i. ?phi a i"
   419   let ?f = "\<lambda>a. (?sm a, a)"
   420   have "inj_on ?f (\<Union>i \<in> I. A i)" unfolding inj_on_def by auto
   421   moreover
   422   { { fix i a assume "i \<in> I" and "a \<in> A i"
   423       hence "?sm a \<in> I \<and> a \<in> A(?sm a)" using someI[of "?phi a" i] by auto
   424     }
   425     hence "?f ` (\<Union>i \<in> I. A i) \<le> (SIGMA i : I. A i)" by auto
   426   }
   427   ultimately
   428   show "inj_on ?f (\<Union>i \<in> I. A i) \<and> ?f ` (\<Union>i \<in> I. A i) \<le> (SIGMA i : I. A i)"
   429   by auto
   430 qed
   431 
   432 lemma inv_unique_comp:
   433   assumes fg: "f \<circ> g = id"
   434     and gf: "g \<circ> f = id"
   435   shows "inv f = g"
   436   using fg gf inv_equality[of g f] by (auto simp add: fun_eq_iff)
   437 
   438 
   439 subsection \<open>The Cantor-Bernstein Theorem\<close>
   440 
   441 lemma Cantor_Bernstein_aux:
   442   shows "\<exists>A' h. A' \<le> A \<and>
   443                 (\<forall>a \<in> A'. a \<notin> g`(B - f ` A')) \<and>
   444                 (\<forall>a \<in> A'. h a = f a) \<and>
   445                 (\<forall>a \<in> A - A'. h a \<in> B - (f ` A') \<and> a = g(h a))"
   446 proof-
   447   obtain H where H_def: "H = (\<lambda> A'. A - (g`(B - (f ` A'))))" by blast
   448   have 0: "mono H" unfolding mono_def H_def by blast
   449   then obtain A' where 1: "H A' = A'" using lfp_unfold by blast
   450   hence 2: "A' = A - (g`(B - (f ` A')))" unfolding H_def by simp
   451   hence 3: "A' \<le> A" by blast
   452   have 4: "\<forall>a \<in> A'.  a \<notin> g`(B - f ` A')"
   453   using 2 by blast
   454   have 5: "\<forall>a \<in> A - A'. \<exists>b \<in> B - (f ` A'). a = g b"
   455   using 2 by blast
   456   (*  *)
   457   obtain h where h_def:
   458   "h = (\<lambda> a. if a \<in> A' then f a else (SOME b. b \<in> B - (f ` A') \<and> a = g b))" by blast
   459   hence "\<forall>a \<in> A'. h a = f a" by auto
   460   moreover
   461   have "\<forall>a \<in> A - A'. h a \<in> B - (f ` A') \<and> a = g(h a)"
   462   proof
   463     fix a assume *: "a \<in> A - A'"
   464     let ?phi = "\<lambda> b. b \<in> B - (f ` A') \<and> a = g b"
   465     have "h a = (SOME b. ?phi b)" using h_def * by auto
   466     moreover have "\<exists>b. ?phi b" using 5 *  by auto
   467     ultimately show  "?phi (h a)" using someI_ex[of ?phi] by auto
   468   qed
   469   ultimately show ?thesis using 3 4 by blast
   470 qed
   471 
   472 theorem Cantor_Bernstein:
   473   assumes INJ1: "inj_on f A" and SUB1: "f ` A \<le> B" and
   474           INJ2: "inj_on g B" and SUB2: "g ` B \<le> A"
   475   shows "\<exists>h. bij_betw h A B"
   476 proof-
   477   obtain A' and h where 0: "A' \<le> A" and
   478   1: "\<forall>a \<in> A'. a \<notin> g`(B - f ` A')" and
   479   2: "\<forall>a \<in> A'. h a = f a" and
   480   3: "\<forall>a \<in> A - A'. h a \<in> B - (f ` A') \<and> a = g(h a)"
   481   using Cantor_Bernstein_aux[of A g B f] by blast
   482   have "inj_on h A"
   483   proof (intro inj_onI)
   484     fix a1 a2
   485     assume 4: "a1 \<in> A" and 5: "a2 \<in> A" and 6: "h a1 = h a2"
   486     show "a1 = a2"
   487     proof(cases "a1 \<in> A'")
   488       assume Case1: "a1 \<in> A'"
   489       show ?thesis
   490       proof(cases "a2 \<in> A'")
   491         assume Case11: "a2 \<in> A'"
   492         hence "f a1 = f a2" using Case1 2 6 by auto
   493         thus ?thesis using INJ1 Case1 Case11 0
   494         unfolding inj_on_def by blast
   495       next
   496         assume Case12: "a2 \<notin> A'"
   497         hence False using 3 5 2 6 Case1 by force
   498         thus ?thesis by simp
   499       qed
   500     next
   501     assume Case2: "a1 \<notin> A'"
   502       show ?thesis
   503       proof(cases "a2 \<in> A'")
   504         assume Case21: "a2 \<in> A'"
   505         hence False using 3 4 2 6 Case2 by auto
   506         thus ?thesis by simp
   507       next
   508         assume Case22: "a2 \<notin> A'"
   509         hence "a1 = g(h a1) \<and> a2 = g(h a2)" using Case2 4 5 3 by auto
   510         thus ?thesis using 6 by simp
   511       qed
   512     qed
   513   qed
   514   (*  *)
   515   moreover
   516   have "h ` A = B"
   517   proof safe
   518     fix a assume "a \<in> A"
   519     thus "h a \<in> B" using SUB1 2 3 by (cases "a \<in> A'") auto
   520   next
   521     fix b assume *: "b \<in> B"
   522     show "b \<in> h ` A"
   523     proof(cases "b \<in> f ` A'")
   524       assume Case1: "b \<in> f ` A'"
   525       then obtain a where "a \<in> A' \<and> b = f a" by blast
   526       thus ?thesis using 2 0 by force
   527     next
   528       assume Case2: "b \<notin> f ` A'"
   529       hence "g b \<notin> A'" using 1 * by auto
   530       hence 4: "g b \<in> A - A'" using * SUB2 by auto
   531       hence "h(g b) \<in> B \<and> g(h(g b)) = g b"
   532       using 3 by auto
   533       hence "h(g b) = b" using * INJ2 unfolding inj_on_def by auto
   534       thus ?thesis using 4 by force
   535     qed
   536   qed
   537   (*  *)
   538   ultimately show ?thesis unfolding bij_betw_def by auto
   539 qed
   540 
   541 subsection \<open>Other Consequences of Hilbert's Epsilon\<close>
   542 
   543 text \<open>Hilbert's Epsilon and the @{term split} Operator\<close>
   544 
   545 text\<open>Looping simprule\<close>
   546 lemma split_paired_Eps: "(SOME x. P x) = (SOME (a,b). P(a,b))"
   547   by simp
   548 
   549 lemma Eps_case_prod: "Eps (case_prod P) = (SOME xy. P (fst xy) (snd xy))"
   550   by (simp add: split_def)
   551 
   552 lemma Eps_case_prod_eq [simp]: "(@(x',y'). x = x' & y = y') = (x,y)"
   553   by blast
   554 
   555 
   556 text\<open>A relation is wellfounded iff it has no infinite descending chain\<close>
   557 lemma wf_iff_no_infinite_down_chain:
   558   "wf r = (~(\<exists>f. \<forall>i. (f(Suc i),f i) \<in> r))"
   559 apply (simp only: wf_eq_minimal)
   560 apply (rule iffI)
   561  apply (rule notI)
   562  apply (erule exE)
   563  apply (erule_tac x = "{w. \<exists>i. w=f i}" in allE, blast)
   564 apply (erule contrapos_np, simp, clarify)
   565 apply (subgoal_tac "\<forall>n. rec_nat x (%i y. @z. z:Q & (z,y) :r) n \<in> Q")
   566  apply (rule_tac x = "rec_nat x (%i y. @z. z:Q & (z,y) :r)" in exI)
   567  apply (rule allI, simp)
   568  apply (rule someI2_ex, blast, blast)
   569 apply (rule allI)
   570 apply (induct_tac "n", simp_all)
   571 apply (rule someI2_ex, blast+)
   572 done
   573 
   574 lemma wf_no_infinite_down_chainE:
   575   assumes "wf r" obtains k where "(f (Suc k), f k) \<notin> r"
   576 using \<open>wf r\<close> wf_iff_no_infinite_down_chain[of r] by blast
   577 
   578 
   579 text\<open>A dynamically-scoped fact for TFL\<close>
   580 lemma tfl_some: "\<forall>P x. P x --> P (Eps P)"
   581   by (blast intro: someI)
   582 
   583 
   584 subsection \<open>Least value operator\<close>
   585 
   586 definition
   587   LeastM :: "['a => 'b::ord, 'a => bool] => 'a" where
   588   "LeastM m P == SOME x. P x & (\<forall>y. P y --> m x <= m y)"
   589 
   590 syntax
   591   "_LeastM" :: "[pttrn, 'a => 'b::ord, bool] => 'a"    ("LEAST _ WRT _. _" [0, 4, 10] 10)
   592 translations
   593   "LEAST x WRT m. P" == "CONST LeastM m (%x. P)"
   594 
   595 lemma LeastMI2:
   596   "P x ==> (!!y. P y ==> m x <= m y)
   597     ==> (!!x. P x ==> \<forall>y. P y --> m x \<le> m y ==> Q x)
   598     ==> Q (LeastM m P)"
   599   apply (simp add: LeastM_def)
   600   apply (rule someI2_ex, blast, blast)
   601   done
   602 
   603 lemma LeastM_equality:
   604   "P k ==> (!!x. P x ==> m k <= m x)
   605     ==> m (LEAST x WRT m. P x) = (m k::'a::order)"
   606   apply (rule LeastMI2, assumption, blast)
   607   apply (blast intro!: order_antisym)
   608   done
   609 
   610 lemma wf_linord_ex_has_least:
   611   "wf r ==> \<forall>x y. ((x,y):r^+) = ((y,x)~:r^*) ==> P k
   612     ==> \<exists>x. P x & (!y. P y --> (m x,m y):r^*)"
   613   apply (drule wf_trancl [THEN wf_eq_minimal [THEN iffD1]])
   614   apply (drule_tac x = "m`Collect P" in spec, force)
   615   done
   616 
   617 lemma ex_has_least_nat:
   618     "P k ==> \<exists>x. P x & (\<forall>y. P y --> m x <= (m y::nat))"
   619   apply (simp only: pred_nat_trancl_eq_le [symmetric])
   620   apply (rule wf_pred_nat [THEN wf_linord_ex_has_least])
   621    apply (simp add: less_eq linorder_not_le pred_nat_trancl_eq_le, assumption)
   622   done
   623 
   624 lemma LeastM_nat_lemma:
   625     "P k ==> P (LeastM m P) & (\<forall>y. P y --> m (LeastM m P) <= (m y::nat))"
   626   apply (simp add: LeastM_def)
   627   apply (rule someI_ex)
   628   apply (erule ex_has_least_nat)
   629   done
   630 
   631 lemmas LeastM_natI = LeastM_nat_lemma [THEN conjunct1]
   632 
   633 lemma LeastM_nat_le: "P x ==> m (LeastM m P) <= (m x::nat)"
   634 by (rule LeastM_nat_lemma [THEN conjunct2, THEN spec, THEN mp], assumption, assumption)
   635 
   636 
   637 subsection \<open>Greatest value operator\<close>
   638 
   639 definition
   640   GreatestM :: "['a => 'b::ord, 'a => bool] => 'a" where
   641   "GreatestM m P == SOME x. P x & (\<forall>y. P y --> m y <= m x)"
   642 
   643 definition
   644   Greatest :: "('a::ord => bool) => 'a" (binder "GREATEST " 10) where
   645   "Greatest == GreatestM (%x. x)"
   646 
   647 syntax
   648   "_GreatestM" :: "[pttrn, 'a => 'b::ord, bool] => 'a"
   649       ("GREATEST _ WRT _. _" [0, 4, 10] 10)
   650 translations
   651   "GREATEST x WRT m. P" == "CONST GreatestM m (%x. P)"
   652 
   653 lemma GreatestMI2:
   654   "P x ==> (!!y. P y ==> m y <= m x)
   655     ==> (!!x. P x ==> \<forall>y. P y --> m y \<le> m x ==> Q x)
   656     ==> Q (GreatestM m P)"
   657   apply (simp add: GreatestM_def)
   658   apply (rule someI2_ex, blast, blast)
   659   done
   660 
   661 lemma GreatestM_equality:
   662  "P k ==> (!!x. P x ==> m x <= m k)
   663     ==> m (GREATEST x WRT m. P x) = (m k::'a::order)"
   664   apply (rule_tac m = m in GreatestMI2, assumption, blast)
   665   apply (blast intro!: order_antisym)
   666   done
   667 
   668 lemma Greatest_equality:
   669   "P (k::'a::order) ==> (!!x. P x ==> x <= k) ==> (GREATEST x. P x) = k"
   670   apply (simp add: Greatest_def)
   671   apply (erule GreatestM_equality, blast)
   672   done
   673 
   674 lemma ex_has_greatest_nat_lemma:
   675   "P k ==> \<forall>x. P x --> (\<exists>y. P y & ~ ((m y::nat) <= m x))
   676     ==> \<exists>y. P y & ~ (m y < m k + n)"
   677   apply (induct n, force)
   678   apply (force simp add: le_Suc_eq)
   679   done
   680 
   681 lemma ex_has_greatest_nat:
   682   "P k ==> \<forall>y. P y --> m y < b
   683     ==> \<exists>x. P x & (\<forall>y. P y --> (m y::nat) <= m x)"
   684   apply (rule ccontr)
   685   apply (cut_tac P = P and n = "b - m k" in ex_has_greatest_nat_lemma)
   686     apply (subgoal_tac [3] "m k <= b", auto)
   687   done
   688 
   689 lemma GreatestM_nat_lemma:
   690   "P k ==> \<forall>y. P y --> m y < b
   691     ==> P (GreatestM m P) & (\<forall>y. P y --> (m y::nat) <= m (GreatestM m P))"
   692   apply (simp add: GreatestM_def)
   693   apply (rule someI_ex)
   694   apply (erule ex_has_greatest_nat, assumption)
   695   done
   696 
   697 lemmas GreatestM_natI = GreatestM_nat_lemma [THEN conjunct1]
   698 
   699 lemma GreatestM_nat_le:
   700   "P x ==> \<forall>y. P y --> m y < b
   701     ==> (m x::nat) <= m (GreatestM m P)"
   702   apply (blast dest: GreatestM_nat_lemma [THEN conjunct2, THEN spec, of P])
   703   done
   704 
   705 
   706 text \<open>\medskip Specialization to \<open>GREATEST\<close>.\<close>
   707 
   708 lemma GreatestI: "P (k::nat) ==> \<forall>y. P y --> y < b ==> P (GREATEST x. P x)"
   709   apply (simp add: Greatest_def)
   710   apply (rule GreatestM_natI, auto)
   711   done
   712 
   713 lemma Greatest_le:
   714     "P x ==> \<forall>y. P y --> y < b ==> (x::nat) <= (GREATEST x. P x)"
   715   apply (simp add: Greatest_def)
   716   apply (rule GreatestM_nat_le, auto)
   717   done
   718 
   719 
   720 subsection \<open>An aside: bounded accessible part\<close>
   721 
   722 text \<open>Finite monotone eventually stable sequences\<close>
   723 
   724 lemma finite_mono_remains_stable_implies_strict_prefix:
   725   fixes f :: "nat \<Rightarrow> 'a::order"
   726   assumes S: "finite (range f)" "mono f" and eq: "\<forall>n. f n = f (Suc n) \<longrightarrow> f (Suc n) = f (Suc (Suc n))"
   727   shows "\<exists>N. (\<forall>n\<le>N. \<forall>m\<le>N. m < n \<longrightarrow> f m < f n) \<and> (\<forall>n\<ge>N. f N = f n)"
   728   using assms
   729 proof -
   730   have "\<exists>n. f n = f (Suc n)"
   731   proof (rule ccontr)
   732     assume "\<not> ?thesis"
   733     then have "\<And>n. f n \<noteq> f (Suc n)" by auto
   734     then have "\<And>n. f n < f (Suc n)"
   735       using  \<open>mono f\<close> by (auto simp: le_less mono_iff_le_Suc)
   736     with lift_Suc_mono_less_iff[of f]
   737     have *: "\<And>n m. n < m \<Longrightarrow> f n < f m" by auto
   738     have "inj f"
   739     proof (intro injI)
   740       fix x y
   741       assume "f x = f y"
   742       then show "x = y" by (cases x y rule: linorder_cases) (auto dest: *)
   743     qed
   744     with \<open>finite (range f)\<close> have "finite (UNIV::nat set)"
   745       by (rule finite_imageD)
   746     then show False by simp
   747   qed
   748   then obtain n where n: "f n = f (Suc n)" ..
   749   def N \<equiv> "LEAST n. f n = f (Suc n)"
   750   have N: "f N = f (Suc N)"
   751     unfolding N_def using n by (rule LeastI)
   752   show ?thesis
   753   proof (intro exI[of _ N] conjI allI impI)
   754     fix n assume "N \<le> n"
   755     then have "\<And>m. N \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> f m = f N"
   756     proof (induct rule: dec_induct)
   757       case (step n) then show ?case
   758         using eq[rule_format, of "n - 1"] N
   759         by (cases n) (auto simp add: le_Suc_eq)
   760     qed simp
   761     from this[of n] \<open>N \<le> n\<close> show "f N = f n" by auto
   762   next
   763     fix n m :: nat assume "m < n" "n \<le> N"
   764     then show "f m < f n"
   765     proof (induct rule: less_Suc_induct[consumes 1])
   766       case (1 i)
   767       then have "i < N" by simp
   768       then have "f i \<noteq> f (Suc i)"
   769         unfolding N_def by (rule not_less_Least)
   770       with \<open>mono f\<close> show ?case by (simp add: mono_iff_le_Suc less_le)
   771     qed auto
   772   qed
   773 qed
   774 
   775 lemma finite_mono_strict_prefix_implies_finite_fixpoint:
   776   fixes f :: "nat \<Rightarrow> 'a set"
   777   assumes S: "\<And>i. f i \<subseteq> S" "finite S"
   778     and inj: "\<exists>N. (\<forall>n\<le>N. \<forall>m\<le>N. m < n \<longrightarrow> f m \<subset> f n) \<and> (\<forall>n\<ge>N. f N = f n)"
   779   shows "f (card S) = (\<Union>n. f n)"
   780 proof -
   781   from inj obtain N where inj: "(\<forall>n\<le>N. \<forall>m\<le>N. m < n \<longrightarrow> f m \<subset> f n)" and eq: "(\<forall>n\<ge>N. f N = f n)" by auto
   782 
   783   { fix i have "i \<le> N \<Longrightarrow> i \<le> card (f i)"
   784     proof (induct i)
   785       case 0 then show ?case by simp
   786     next
   787       case (Suc i)
   788       with inj[rule_format, of "Suc i" i]
   789       have "(f i) \<subset> (f (Suc i))" by auto
   790       moreover have "finite (f (Suc i))" using S by (rule finite_subset)
   791       ultimately have "card (f i) < card (f (Suc i))" by (intro psubset_card_mono)
   792       with Suc show ?case using inj by auto
   793     qed
   794   }
   795   then have "N \<le> card (f N)" by simp
   796   also have "\<dots> \<le> card S" using S by (intro card_mono)
   797   finally have "f (card S) = f N" using eq by auto
   798   then show ?thesis using eq inj[rule_format, of N]
   799     apply auto
   800     apply (case_tac "n < N")
   801     apply (auto simp: not_less)
   802     done
   803 qed
   804 
   805 
   806 subsection \<open>More on injections, bijections, and inverses\<close>
   807 
   808 lemma infinite_imp_bij_betw:
   809 assumes INF: "\<not> finite A"
   810 shows "\<exists>h. bij_betw h A (A - {a})"
   811 proof(cases "a \<in> A")
   812   assume Case1: "a \<notin> A"  hence "A - {a} = A" by blast
   813   thus ?thesis using bij_betw_id[of A] by auto
   814 next
   815   assume Case2: "a \<in> A"
   816   have "\<not> finite (A - {a})" using INF by auto
   817   with infinite_iff_countable_subset[of "A - {a}"] obtain f::"nat \<Rightarrow> 'a"
   818   where 1: "inj f" and 2: "f ` UNIV \<le> A - {a}" by blast
   819   obtain g where g_def: "g = (\<lambda> n. if n = 0 then a else f (Suc n))" by blast
   820   obtain A' where A'_def: "A' = g ` UNIV" by blast
   821   have temp: "\<forall>y. f y \<noteq> a" using 2 by blast
   822   have 3: "inj_on g UNIV \<and> g ` UNIV \<le> A \<and> a \<in> g ` UNIV"
   823   proof(auto simp add: Case2 g_def, unfold inj_on_def, intro ballI impI,
   824         case_tac "x = 0", auto simp add: 2)
   825     fix y  assume "a = (if y = 0 then a else f (Suc y))"
   826     thus "y = 0" using temp by (case_tac "y = 0", auto)
   827   next
   828     fix x y
   829     assume "f (Suc x) = (if y = 0 then a else f (Suc y))"
   830     thus "x = y" using 1 temp unfolding inj_on_def by (case_tac "y = 0", auto)
   831   next
   832     fix n show "f (Suc n) \<in> A" using 2 by blast
   833   qed
   834   hence 4: "bij_betw g UNIV A' \<and> a \<in> A' \<and> A' \<le> A"
   835   using inj_on_imp_bij_betw[of g] unfolding A'_def by auto
   836   hence 5: "bij_betw (inv g) A' UNIV"
   837   by (auto simp add: bij_betw_inv_into)
   838   (*  *)
   839   obtain n where "g n = a" using 3 by auto
   840   hence 6: "bij_betw g (UNIV - {n}) (A' - {a})"
   841   using 3 4 unfolding A'_def
   842   by clarify (rule bij_betw_subset, auto simp: image_set_diff)
   843   (*  *)
   844   obtain v where v_def: "v = (\<lambda> m. if m < n then m else Suc m)" by blast
   845   have 7: "bij_betw v UNIV (UNIV - {n})"
   846   proof(unfold bij_betw_def inj_on_def, intro conjI, clarify)
   847     fix m1 m2 assume "v m1 = v m2"
   848     thus "m1 = m2"
   849     by(case_tac "m1 < n", case_tac "m2 < n",
   850        auto simp add: inj_on_def v_def, case_tac "m2 < n", auto)
   851   next
   852     show "v ` UNIV = UNIV - {n}"
   853     proof(auto simp add: v_def)
   854       fix m assume *: "m \<noteq> n" and **: "m \<notin> Suc ` {m'. \<not> m' < n}"
   855       {assume "n \<le> m" with * have 71: "Suc n \<le> m" by auto
   856        then obtain m' where 72: "m = Suc m'" using Suc_le_D by auto
   857        with 71 have "n \<le> m'" by auto
   858        with 72 ** have False by auto
   859       }
   860       thus "m < n" by force
   861     qed
   862   qed
   863   (*  *)
   864   obtain h' where h'_def: "h' = g o v o (inv g)" by blast
   865   hence 8: "bij_betw h' A' (A' - {a})" using 5 7 6
   866   by (auto simp add: bij_betw_trans)
   867   (*  *)
   868   obtain h where h_def: "h = (\<lambda> b. if b \<in> A' then h' b else b)" by blast
   869   have "\<forall>b \<in> A'. h b = h' b" unfolding h_def by auto
   870   hence "bij_betw h  A' (A' - {a})" using 8 bij_betw_cong[of A' h] by auto
   871   moreover
   872   {have "\<forall>b \<in> A - A'. h b = b" unfolding h_def by auto
   873    hence "bij_betw h  (A - A') (A - A')"
   874    using bij_betw_cong[of "A - A'" h id] bij_betw_id[of "A - A'"] by auto
   875   }
   876   moreover
   877   have "(A' Int (A - A') = {} \<and> A' \<union> (A - A') = A) \<and>
   878         ((A' - {a}) Int (A - A') = {} \<and> (A' - {a}) \<union> (A - A') = A - {a})"
   879   using 4 by blast
   880   ultimately have "bij_betw h A (A - {a})"
   881   using bij_betw_combine[of h A' "A' - {a}" "A - A'" "A - A'"] by simp
   882   thus ?thesis by blast
   883 qed
   884 
   885 lemma infinite_imp_bij_betw2:
   886 assumes INF: "\<not> finite A"
   887 shows "\<exists>h. bij_betw h A (A \<union> {a})"
   888 proof(cases "a \<in> A")
   889   assume Case1: "a \<in> A"  hence "A \<union> {a} = A" by blast
   890   thus ?thesis using bij_betw_id[of A] by auto
   891 next
   892   let ?A' = "A \<union> {a}"
   893   assume Case2: "a \<notin> A" hence "A = ?A' - {a}" by blast
   894   moreover have "\<not> finite ?A'" using INF by auto
   895   ultimately obtain f where "bij_betw f ?A' A"
   896   using infinite_imp_bij_betw[of ?A' a] by auto
   897   hence "bij_betw(inv_into ?A' f) A ?A'" using bij_betw_inv_into by blast
   898   thus ?thesis by auto
   899 qed
   900 
   901 lemma bij_betw_inv_into_left:
   902 assumes BIJ: "bij_betw f A A'" and IN: "a \<in> A"
   903 shows "(inv_into A f) (f a) = a"
   904 using assms unfolding bij_betw_def
   905 by clarify (rule inv_into_f_f)
   906 
   907 lemma bij_betw_inv_into_right:
   908 assumes "bij_betw f A A'" "a' \<in> A'"
   909 shows "f(inv_into A f a') = a'"
   910 using assms unfolding bij_betw_def using f_inv_into_f by force
   911 
   912 lemma bij_betw_inv_into_subset:
   913 assumes BIJ: "bij_betw f A A'" and
   914         SUB: "B \<le> A" and IM: "f ` B = B'"
   915 shows "bij_betw (inv_into A f) B' B"
   916 using assms unfolding bij_betw_def
   917 by (auto intro: inj_on_inv_into)
   918 
   919 
   920 subsection \<open>Specification package -- Hilbertized version\<close>
   921 
   922 lemma exE_some: "[| Ex P ; c == Eps P |] ==> P c"
   923   by (simp only: someI_ex)
   924 
   925 ML_file "Tools/choice_specification.ML"
   926 
   927 end