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