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