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