src/HOL/Hilbert_Choice.thy
author haftmann
Mon Oct 08 12:03:49 2012 +0200 (2012-10-08)
changeset 49739 13aa6d8268ec
parent 48891 c0eafbd55de3
child 49948 744934b818c7
permissions -rw-r--r--
consolidated names of theorems on composition;
generalized former theorem UN_o;
comp_assoc orients to the right, as is more common
     1 (*  Title:      HOL/Hilbert_Choice.thy
     2     Author:     Lawrence C Paulson, Tobias Nipkow
     3     Copyright   2001  University of Cambridge
     4 *)
     5 
     6 header {* Hilbert's Epsilon-Operator and the Axiom of Choice *}
     7 
     8 theory Hilbert_Choice
     9 imports Nat Wellfounded Plain
    10 keywords "specification" "ax_specification" :: thy_goal
    11 begin
    12 
    13 subsection {* Hilbert's epsilon *}
    14 
    15 axiomatization Eps :: "('a => bool) => 'a" where
    16   someI: "P x ==> P (Eps P)"
    17 
    18 syntax (epsilon)
    19   "_Eps"        :: "[pttrn, bool] => 'a"    ("(3\<some>_./ _)" [0, 10] 10)
    20 syntax (HOL)
    21   "_Eps"        :: "[pttrn, bool] => 'a"    ("(3@ _./ _)" [0, 10] 10)
    22 syntax
    23   "_Eps"        :: "[pttrn, bool] => 'a"    ("(3SOME _./ _)" [0, 10] 10)
    24 translations
    25   "SOME x. P" == "CONST Eps (%x. P)"
    26 
    27 print_translation {*
    28   [(@{const_syntax Eps}, 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 *} -- {* to avoid eta-contraction of body *}
    32 
    33 definition inv_into :: "'a set => ('a => 'b) => ('b => 'a)" where
    34 "inv_into A f == %x. SOME y. y : A & f y = x"
    35 
    36 abbreviation inv :: "('a => 'b) => ('b => 'a)" where
    37 "inv == inv_into UNIV"
    38 
    39 
    40 subsection {*Hilbert's Epsilon-operator*}
    41 
    42 text{*Easier to apply than @{text someI} if the witness comes from an
    43 existential formula*}
    44 lemma someI_ex [elim?]: "\<exists>x. P x ==> P (SOME x. P x)"
    45 apply (erule exE)
    46 apply (erule someI)
    47 done
    48 
    49 text{*Easier to apply than @{text someI} because the conclusion has only one
    50 occurrence of @{term P}.*}
    51 lemma someI2: "[| P a;  !!x. P x ==> Q x |] ==> Q (SOME x. P x)"
    52 by (blast intro: someI)
    53 
    54 text{*Easier to apply than @{text someI2} if the witness comes from an
    55 existential formula*}
    56 lemma someI2_ex: "[| \<exists>a. P a; !!x. P x ==> Q x |] ==> Q (SOME x. P x)"
    57 by (blast intro: someI2)
    58 
    59 lemma some_equality [intro]:
    60      "[| P a;  !!x. P x ==> x=a |] ==> (SOME x. P x) = a"
    61 by (blast intro: someI2)
    62 
    63 lemma some1_equality: "[| EX!x. P x; P a |] ==> (SOME x. P x) = a"
    64 by blast
    65 
    66 lemma some_eq_ex: "P (SOME x. P x) =  (\<exists>x. P x)"
    67 by (blast intro: someI)
    68 
    69 lemma some_eq_trivial [simp]: "(SOME y. y=x) = x"
    70 apply (rule some_equality)
    71 apply (rule refl, assumption)
    72 done
    73 
    74 lemma some_sym_eq_trivial [simp]: "(SOME y. x=y) = x"
    75 apply (rule some_equality)
    76 apply (rule refl)
    77 apply (erule sym)
    78 done
    79 
    80 
    81 subsection{*Axiom of Choice, Proved Using the Description Operator*}
    82 
    83 lemma choice: "\<forall>x. \<exists>y. Q x y ==> \<exists>f. \<forall>x. Q x (f x)"
    84 by (fast elim: someI)
    85 
    86 lemma bchoice: "\<forall>x\<in>S. \<exists>y. Q x y ==> \<exists>f. \<forall>x\<in>S. Q x (f x)"
    87 by (fast elim: someI)
    88 
    89 
    90 subsection {*Function Inverse*}
    91 
    92 lemma inv_def: "inv f = (%y. SOME x. f x = y)"
    93 by(simp add: inv_into_def)
    94 
    95 lemma inv_into_into: "x : f ` A ==> inv_into A f x : A"
    96 apply (simp add: inv_into_def)
    97 apply (fast intro: someI2)
    98 done
    99 
   100 lemma inv_id [simp]: "inv id = id"
   101 by (simp add: inv_into_def id_def)
   102 
   103 lemma inv_into_f_f [simp]:
   104   "[| inj_on f A;  x : A |] ==> inv_into A f (f x) = x"
   105 apply (simp add: inv_into_def inj_on_def)
   106 apply (blast intro: someI2)
   107 done
   108 
   109 lemma inv_f_f: "inj f ==> inv f (f x) = x"
   110 by simp
   111 
   112 lemma f_inv_into_f: "y : f`A  ==> f (inv_into A f y) = y"
   113 apply (simp add: inv_into_def)
   114 apply (fast intro: someI2)
   115 done
   116 
   117 lemma inv_into_f_eq: "[| inj_on f A; x : A; f x = y |] ==> inv_into A f y = x"
   118 apply (erule subst)
   119 apply (fast intro: inv_into_f_f)
   120 done
   121 
   122 lemma inv_f_eq: "[| inj f; f x = y |] ==> inv f y = x"
   123 by (simp add:inv_into_f_eq)
   124 
   125 lemma inj_imp_inv_eq: "[| inj f; ALL x. f(g x) = x |] ==> inv f = g"
   126   by (blast intro: inv_into_f_eq)
   127 
   128 text{*But is it useful?*}
   129 lemma inj_transfer:
   130   assumes injf: "inj f" and minor: "!!y. y \<in> range(f) ==> P(inv f y)"
   131   shows "P x"
   132 proof -
   133   have "f x \<in> range f" by auto
   134   hence "P(inv f (f x))" by (rule minor)
   135   thus "P x" by (simp add: inv_into_f_f [OF injf])
   136 qed
   137 
   138 lemma inj_iff: "(inj f) = (inv f o f = id)"
   139 apply (simp add: o_def fun_eq_iff)
   140 apply (blast intro: inj_on_inverseI inv_into_f_f)
   141 done
   142 
   143 lemma inv_o_cancel[simp]: "inj f ==> inv f o f = id"
   144 by (simp add: inj_iff)
   145 
   146 lemma o_inv_o_cancel[simp]: "inj f ==> g o inv f o f = g"
   147 by (simp add: comp_assoc)
   148 
   149 lemma inv_into_image_cancel[simp]:
   150   "inj_on f A ==> S <= A ==> inv_into A f ` f ` S = S"
   151 by(fastforce simp: image_def)
   152 
   153 lemma inj_imp_surj_inv: "inj f ==> surj (inv f)"
   154 by (blast intro!: surjI inv_into_f_f)
   155 
   156 lemma surj_f_inv_f: "surj f ==> f(inv f y) = y"
   157 by (simp add: f_inv_into_f)
   158 
   159 lemma inv_into_injective:
   160   assumes eq: "inv_into A f x = inv_into A f y"
   161       and x: "x: f`A"
   162       and y: "y: f`A"
   163   shows "x=y"
   164 proof -
   165   have "f (inv_into A f x) = f (inv_into A f y)" using eq by simp
   166   thus ?thesis by (simp add: f_inv_into_f x y)
   167 qed
   168 
   169 lemma inj_on_inv_into: "B <= f`A ==> inj_on (inv_into A f) B"
   170 by (blast intro: inj_onI dest: inv_into_injective injD)
   171 
   172 lemma bij_betw_inv_into: "bij_betw f A B ==> bij_betw (inv_into A f) B A"
   173 by (auto simp add: bij_betw_def inj_on_inv_into)
   174 
   175 lemma surj_imp_inj_inv: "surj f ==> inj (inv f)"
   176 by (simp add: inj_on_inv_into)
   177 
   178 lemma surj_iff: "(surj f) = (f o inv f = id)"
   179 by (auto intro!: surjI simp: surj_f_inv_f fun_eq_iff[where 'b='a])
   180 
   181 lemma surj_iff_all: "surj f \<longleftrightarrow> (\<forall>x. f (inv f x) = x)"
   182   unfolding surj_iff by (simp add: o_def fun_eq_iff)
   183 
   184 lemma surj_imp_inv_eq: "[| surj f; \<forall>x. g(f x) = x |] ==> inv f = g"
   185 apply (rule ext)
   186 apply (drule_tac x = "inv f x" in spec)
   187 apply (simp add: surj_f_inv_f)
   188 done
   189 
   190 lemma bij_imp_bij_inv: "bij f ==> bij (inv f)"
   191 by (simp add: bij_def inj_imp_surj_inv surj_imp_inj_inv)
   192 
   193 lemma inv_equality: "[| !!x. g (f x) = x;  !!y. f (g y) = y |] ==> inv f = g"
   194 apply (rule ext)
   195 apply (auto simp add: inv_into_def)
   196 done
   197 
   198 lemma inv_inv_eq: "bij f ==> inv (inv f) = f"
   199 apply (rule inv_equality)
   200 apply (auto simp add: bij_def surj_f_inv_f)
   201 done
   202 
   203 (** bij(inv f) implies little about f.  Consider f::bool=>bool such that
   204     f(True)=f(False)=True.  Then it's consistent with axiom someI that
   205     inv f could be any function at all, including the identity function.
   206     If inv f=id then inv f is a bijection, but inj f, surj(f) and
   207     inv(inv f)=f all fail.
   208 **)
   209 
   210 lemma inv_into_comp:
   211   "[| inj_on f (g ` A); inj_on g A; x : f ` g ` A |] ==>
   212   inv_into A (f o g) x = (inv_into A g o inv_into (g ` A) f) x"
   213 apply (rule inv_into_f_eq)
   214   apply (fast intro: comp_inj_on)
   215  apply (simp add: inv_into_into)
   216 apply (simp add: f_inv_into_f inv_into_into)
   217 done
   218 
   219 lemma o_inv_distrib: "[| bij f; bij g |] ==> inv (f o g) = inv g o inv f"
   220 apply (rule inv_equality)
   221 apply (auto simp add: bij_def surj_f_inv_f)
   222 done
   223 
   224 lemma image_surj_f_inv_f: "surj f ==> f ` (inv f ` A) = A"
   225 by (simp add: image_eq_UN surj_f_inv_f)
   226 
   227 lemma image_inv_f_f: "inj f ==> (inv f) ` (f ` A) = A"
   228 by (simp add: image_eq_UN)
   229 
   230 lemma inv_image_comp: "inj f ==> inv f ` (f`X) = X"
   231 by (auto simp add: image_def)
   232 
   233 lemma bij_image_Collect_eq: "bij f ==> f ` Collect P = {y. P (inv f y)}"
   234 apply auto
   235 apply (force simp add: bij_is_inj)
   236 apply (blast intro: bij_is_surj [THEN surj_f_inv_f, symmetric])
   237 done
   238 
   239 lemma bij_vimage_eq_inv_image: "bij f ==> f -` A = inv f ` A" 
   240 apply (auto simp add: bij_is_surj [THEN surj_f_inv_f])
   241 apply (blast intro: bij_is_inj [THEN inv_into_f_f, symmetric])
   242 done
   243 
   244 lemma finite_fun_UNIVD1:
   245   assumes fin: "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
   246   and card: "card (UNIV :: 'b set) \<noteq> Suc 0"
   247   shows "finite (UNIV :: 'a set)"
   248 proof -
   249   from fin have finb: "finite (UNIV :: 'b set)" by (rule finite_fun_UNIVD2)
   250   with card have "card (UNIV :: 'b set) \<ge> Suc (Suc 0)"
   251     by (cases "card (UNIV :: 'b set)") (auto simp add: card_eq_0_iff)
   252   then obtain n where "card (UNIV :: 'b set) = Suc (Suc n)" "n = card (UNIV :: 'b set) - Suc (Suc 0)" by auto
   253   then obtain b1 b2 where b1b2: "(b1 :: 'b) \<noteq> (b2 :: 'b)" by (auto simp add: card_Suc_eq)
   254   from fin have "finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. inv f b1))" by (rule finite_imageI)
   255   moreover have "UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. inv f b1)"
   256   proof (rule UNIV_eq_I)
   257     fix x :: 'a
   258     from b1b2 have "x = inv (\<lambda>y. if y = x then b1 else b2) b1" by (simp add: inv_into_def)
   259     thus "x \<in> range (\<lambda>f\<Colon>'a \<Rightarrow> 'b. inv f b1)" by blast
   260   qed
   261   ultimately show "finite (UNIV :: 'a set)" by simp
   262 qed
   263 
   264 lemma image_inv_into_cancel:
   265   assumes SURJ: "f`A=A'" and SUB: "B' \<le> A'"
   266   shows "f `((inv_into A f)`B') = B'"
   267   using assms
   268 proof (auto simp add: f_inv_into_f)
   269   let ?f' = "(inv_into A f)"
   270   fix a' assume *: "a' \<in> B'"
   271   then have "a' \<in> A'" using SUB by auto
   272   then have "a' = f (?f' a')"
   273     using SURJ by (auto simp add: f_inv_into_f)
   274   then show "a' \<in> f ` (?f' ` B')" using * by blast
   275 qed
   276 
   277 lemma inv_into_inv_into_eq:
   278   assumes "bij_betw f A A'" "a \<in> A"
   279   shows "inv_into A' (inv_into A f) a = f a"
   280 proof -
   281   let ?f' = "inv_into A f"   let ?f'' = "inv_into A' ?f'"
   282   have 1: "bij_betw ?f' A' A" using assms
   283   by (auto simp add: bij_betw_inv_into)
   284   obtain a' where 2: "a' \<in> A'" and 3: "?f' a' = a"
   285     using 1 `a \<in> A` unfolding bij_betw_def by force
   286   hence "?f'' a = a'"
   287     using `a \<in> A` 1 3 by (auto simp add: f_inv_into_f bij_betw_def)
   288   moreover have "f a = a'" using assms 2 3
   289     by (auto simp add: bij_betw_def)
   290   ultimately show "?f'' a = f a" by simp
   291 qed
   292 
   293 lemma inj_on_iff_surj:
   294   assumes "A \<noteq> {}"
   295   shows "(\<exists>f. inj_on f A \<and> f ` A \<le> A') \<longleftrightarrow> (\<exists>g. g ` A' = A)"
   296 proof safe
   297   fix f assume INJ: "inj_on f A" and INCL: "f ` A \<le> A'"
   298   let ?phi = "\<lambda>a' a. a \<in> A \<and> f a = a'"  let ?csi = "\<lambda>a. a \<in> A"
   299   let ?g = "\<lambda>a'. if a' \<in> f ` A then (SOME a. ?phi a' a) else (SOME a. ?csi a)"
   300   have "?g ` A' = A"
   301   proof
   302     show "?g ` A' \<le> A"
   303     proof clarify
   304       fix a' assume *: "a' \<in> A'"
   305       show "?g a' \<in> A"
   306       proof cases
   307         assume Case1: "a' \<in> f ` A"
   308         then obtain a where "?phi a' a" by blast
   309         hence "?phi a' (SOME a. ?phi a' a)" using someI[of "?phi a'" a] by blast
   310         with Case1 show ?thesis by auto
   311       next
   312         assume Case2: "a' \<notin> f ` A"
   313         hence "?csi (SOME a. ?csi a)" using assms someI_ex[of ?csi] by blast
   314         with Case2 show ?thesis by auto
   315       qed
   316     qed
   317   next
   318     show "A \<le> ?g ` A'"
   319     proof-
   320       {fix a assume *: "a \<in> A"
   321        let ?b = "SOME aa. ?phi (f a) aa"
   322        have "?phi (f a) a" using * by auto
   323        hence 1: "?phi (f a) ?b" using someI[of "?phi(f a)" a] by blast
   324        hence "?g(f a) = ?b" using * by auto
   325        moreover have "a = ?b" using 1 INJ * by (auto simp add: inj_on_def)
   326        ultimately have "?g(f a) = a" by simp
   327        with INCL * have "?g(f a) = a \<and> f a \<in> A'" by auto
   328       }
   329       thus ?thesis by force
   330     qed
   331   qed
   332   thus "\<exists>g. g ` A' = A" by blast
   333 next
   334   fix g  let ?f = "inv_into A' g"
   335   have "inj_on ?f (g ` A')"
   336     by (auto simp add: inj_on_inv_into)
   337   moreover
   338   {fix a' assume *: "a' \<in> A'"
   339    let ?phi = "\<lambda> b'. b' \<in> A' \<and> g b' = g a'"
   340    have "?phi a'" using * by auto
   341    hence "?phi(SOME b'. ?phi b')" using someI[of ?phi] by blast
   342    hence "?f(g a') \<in> A'" unfolding inv_into_def by auto
   343   }
   344   ultimately show "\<exists>f. inj_on f (g ` A') \<and> f ` g ` A' \<subseteq> A'" by auto
   345 qed
   346 
   347 lemma Ex_inj_on_UNION_Sigma:
   348   "\<exists>f. (inj_on f (\<Union> i \<in> I. A i) \<and> f ` (\<Union> i \<in> I. A i) \<le> (SIGMA i : I. A i))"
   349 proof
   350   let ?phi = "\<lambda> a i. i \<in> I \<and> a \<in> A i"
   351   let ?sm = "\<lambda> a. SOME i. ?phi a i"
   352   let ?f = "\<lambda>a. (?sm a, a)"
   353   have "inj_on ?f (\<Union> i \<in> I. A i)" unfolding inj_on_def by auto
   354   moreover
   355   { { fix i a assume "i \<in> I" and "a \<in> A i"
   356       hence "?sm a \<in> I \<and> a \<in> A(?sm a)" using someI[of "?phi a" i] by auto
   357     }
   358     hence "?f ` (\<Union> i \<in> I. A i) \<le> (SIGMA i : I. A i)" by auto
   359   }
   360   ultimately
   361   show "inj_on ?f (\<Union> i \<in> I. A i) \<and> ?f ` (\<Union> i \<in> I. A i) \<le> (SIGMA i : I. A i)"
   362   by auto
   363 qed
   364 
   365 subsection {* The Cantor-Bernstein Theorem *}
   366 
   367 lemma Cantor_Bernstein_aux:
   368   shows "\<exists>A' h. A' \<le> A \<and>
   369                 (\<forall>a \<in> A'. a \<notin> g`(B - f ` A')) \<and>
   370                 (\<forall>a \<in> A'. h a = f a) \<and>
   371                 (\<forall>a \<in> A - A'. h a \<in> B - (f ` A') \<and> a = g(h a))"
   372 proof-
   373   obtain H where H_def: "H = (\<lambda> A'. A - (g`(B - (f ` A'))))" by blast
   374   have 0: "mono H" unfolding mono_def H_def by blast
   375   then obtain A' where 1: "H A' = A'" using lfp_unfold by blast
   376   hence 2: "A' = A - (g`(B - (f ` A')))" unfolding H_def by simp
   377   hence 3: "A' \<le> A" by blast
   378   have 4: "\<forall>a \<in> A'.  a \<notin> g`(B - f ` A')"
   379   using 2 by blast
   380   have 5: "\<forall>a \<in> A - A'. \<exists>b \<in> B - (f ` A'). a = g b"
   381   using 2 by blast
   382   (*  *)
   383   obtain h where h_def:
   384   "h = (\<lambda> a. if a \<in> A' then f a else (SOME b. b \<in> B - (f ` A') \<and> a = g b))" by blast
   385   hence "\<forall>a \<in> A'. h a = f a" by auto
   386   moreover
   387   have "\<forall>a \<in> A - A'. h a \<in> B - (f ` A') \<and> a = g(h a)"
   388   proof
   389     fix a assume *: "a \<in> A - A'"
   390     let ?phi = "\<lambda> b. b \<in> B - (f ` A') \<and> a = g b"
   391     have "h a = (SOME b. ?phi b)" using h_def * by auto
   392     moreover have "\<exists>b. ?phi b" using 5 *  by auto
   393     ultimately show  "?phi (h a)" using someI_ex[of ?phi] by auto
   394   qed
   395   ultimately show ?thesis using 3 4 by blast
   396 qed
   397 
   398 theorem Cantor_Bernstein:
   399   assumes INJ1: "inj_on f A" and SUB1: "f ` A \<le> B" and
   400           INJ2: "inj_on g B" and SUB2: "g ` B \<le> A"
   401   shows "\<exists>h. bij_betw h A B"
   402 proof-
   403   obtain A' and h where 0: "A' \<le> A" and
   404   1: "\<forall>a \<in> A'. a \<notin> g`(B - f ` A')" and
   405   2: "\<forall>a \<in> A'. h a = f a" and
   406   3: "\<forall>a \<in> A - A'. h a \<in> B - (f ` A') \<and> a = g(h a)"
   407   using Cantor_Bernstein_aux[of A g B f] by blast
   408   have "inj_on h A"
   409   proof (intro inj_onI)
   410     fix a1 a2
   411     assume 4: "a1 \<in> A" and 5: "a2 \<in> A" and 6: "h a1 = h a2"
   412     show "a1 = a2"
   413     proof(cases "a1 \<in> A'")
   414       assume Case1: "a1 \<in> A'"
   415       show ?thesis
   416       proof(cases "a2 \<in> A'")
   417         assume Case11: "a2 \<in> A'"
   418         hence "f a1 = f a2" using Case1 2 6 by auto
   419         thus ?thesis using INJ1 Case1 Case11 0
   420         unfolding inj_on_def by blast
   421       next
   422         assume Case12: "a2 \<notin> A'"
   423         hence False using 3 5 2 6 Case1 by force
   424         thus ?thesis by simp
   425       qed
   426     next
   427     assume Case2: "a1 \<notin> A'"
   428       show ?thesis
   429       proof(cases "a2 \<in> A'")
   430         assume Case21: "a2 \<in> A'"
   431         hence False using 3 4 2 6 Case2 by auto
   432         thus ?thesis by simp
   433       next
   434         assume Case22: "a2 \<notin> A'"
   435         hence "a1 = g(h a1) \<and> a2 = g(h a2)" using Case2 4 5 3 by auto
   436         thus ?thesis using 6 by simp
   437       qed
   438     qed
   439   qed
   440   (*  *)
   441   moreover
   442   have "h ` A = B"
   443   proof safe
   444     fix a assume "a \<in> A"
   445     thus "h a \<in> B" using SUB1 2 3 by (cases "a \<in> A'") auto
   446   next
   447     fix b assume *: "b \<in> B"
   448     show "b \<in> h ` A"
   449     proof(cases "b \<in> f ` A'")
   450       assume Case1: "b \<in> f ` A'"
   451       then obtain a where "a \<in> A' \<and> b = f a" by blast
   452       thus ?thesis using 2 0 by force
   453     next
   454       assume Case2: "b \<notin> f ` A'"
   455       hence "g b \<notin> A'" using 1 * by auto
   456       hence 4: "g b \<in> A - A'" using * SUB2 by auto
   457       hence "h(g b) \<in> B \<and> g(h(g b)) = g b"
   458       using 3 by auto
   459       hence "h(g b) = b" using * INJ2 unfolding inj_on_def by auto
   460       thus ?thesis using 4 by force
   461     qed
   462   qed
   463   (*  *)
   464   ultimately show ?thesis unfolding bij_betw_def by auto
   465 qed
   466 
   467 subsection {*Other Consequences of Hilbert's Epsilon*}
   468 
   469 text {*Hilbert's Epsilon and the @{term split} Operator*}
   470 
   471 text{*Looping simprule*}
   472 lemma split_paired_Eps: "(SOME x. P x) = (SOME (a,b). P(a,b))"
   473   by simp
   474 
   475 lemma Eps_split: "Eps (split P) = (SOME xy. P (fst xy) (snd xy))"
   476   by (simp add: split_def)
   477 
   478 lemma Eps_split_eq [simp]: "(@(x',y'). x = x' & y = y') = (x,y)"
   479   by blast
   480 
   481 
   482 text{*A relation is wellfounded iff it has no infinite descending chain*}
   483 lemma wf_iff_no_infinite_down_chain:
   484   "wf r = (~(\<exists>f. \<forall>i. (f(Suc i),f i) \<in> r))"
   485 apply (simp only: wf_eq_minimal)
   486 apply (rule iffI)
   487  apply (rule notI)
   488  apply (erule exE)
   489  apply (erule_tac x = "{w. \<exists>i. w=f i}" in allE, blast)
   490 apply (erule contrapos_np, simp, clarify)
   491 apply (subgoal_tac "\<forall>n. nat_rec x (%i y. @z. z:Q & (z,y) :r) n \<in> Q")
   492  apply (rule_tac x = "nat_rec x (%i y. @z. z:Q & (z,y) :r)" in exI)
   493  apply (rule allI, simp)
   494  apply (rule someI2_ex, blast, blast)
   495 apply (rule allI)
   496 apply (induct_tac "n", simp_all)
   497 apply (rule someI2_ex, blast+)
   498 done
   499 
   500 lemma wf_no_infinite_down_chainE:
   501   assumes "wf r" obtains k where "(f (Suc k), f k) \<notin> r"
   502 using `wf r` wf_iff_no_infinite_down_chain[of r] by blast
   503 
   504 
   505 text{*A dynamically-scoped fact for TFL *}
   506 lemma tfl_some: "\<forall>P x. P x --> P (Eps P)"
   507   by (blast intro: someI)
   508 
   509 
   510 subsection {* Least value operator *}
   511 
   512 definition
   513   LeastM :: "['a => 'b::ord, 'a => bool] => 'a" where
   514   "LeastM m P == SOME x. P x & (\<forall>y. P y --> m x <= m y)"
   515 
   516 syntax
   517   "_LeastM" :: "[pttrn, 'a => 'b::ord, bool] => 'a"    ("LEAST _ WRT _. _" [0, 4, 10] 10)
   518 translations
   519   "LEAST x WRT m. P" == "CONST LeastM m (%x. P)"
   520 
   521 lemma LeastMI2:
   522   "P x ==> (!!y. P y ==> m x <= m y)
   523     ==> (!!x. P x ==> \<forall>y. P y --> m x \<le> m y ==> Q x)
   524     ==> Q (LeastM m P)"
   525   apply (simp add: LeastM_def)
   526   apply (rule someI2_ex, blast, blast)
   527   done
   528 
   529 lemma LeastM_equality:
   530   "P k ==> (!!x. P x ==> m k <= m x)
   531     ==> m (LEAST x WRT m. P x) = (m k::'a::order)"
   532   apply (rule LeastMI2, assumption, blast)
   533   apply (blast intro!: order_antisym)
   534   done
   535 
   536 lemma wf_linord_ex_has_least:
   537   "wf r ==> \<forall>x y. ((x,y):r^+) = ((y,x)~:r^*) ==> P k
   538     ==> \<exists>x. P x & (!y. P y --> (m x,m y):r^*)"
   539   apply (drule wf_trancl [THEN wf_eq_minimal [THEN iffD1]])
   540   apply (drule_tac x = "m`Collect P" in spec, force)
   541   done
   542 
   543 lemma ex_has_least_nat:
   544     "P k ==> \<exists>x. P x & (\<forall>y. P y --> m x <= (m y::nat))"
   545   apply (simp only: pred_nat_trancl_eq_le [symmetric])
   546   apply (rule wf_pred_nat [THEN wf_linord_ex_has_least])
   547    apply (simp add: less_eq linorder_not_le pred_nat_trancl_eq_le, assumption)
   548   done
   549 
   550 lemma LeastM_nat_lemma:
   551     "P k ==> P (LeastM m P) & (\<forall>y. P y --> m (LeastM m P) <= (m y::nat))"
   552   apply (simp add: LeastM_def)
   553   apply (rule someI_ex)
   554   apply (erule ex_has_least_nat)
   555   done
   556 
   557 lemmas LeastM_natI = LeastM_nat_lemma [THEN conjunct1]
   558 
   559 lemma LeastM_nat_le: "P x ==> m (LeastM m P) <= (m x::nat)"
   560 by (rule LeastM_nat_lemma [THEN conjunct2, THEN spec, THEN mp], assumption, assumption)
   561 
   562 
   563 subsection {* Greatest value operator *}
   564 
   565 definition
   566   GreatestM :: "['a => 'b::ord, 'a => bool] => 'a" where
   567   "GreatestM m P == SOME x. P x & (\<forall>y. P y --> m y <= m x)"
   568 
   569 definition
   570   Greatest :: "('a::ord => bool) => 'a" (binder "GREATEST " 10) where
   571   "Greatest == GreatestM (%x. x)"
   572 
   573 syntax
   574   "_GreatestM" :: "[pttrn, 'a => 'b::ord, bool] => 'a"
   575       ("GREATEST _ WRT _. _" [0, 4, 10] 10)
   576 translations
   577   "GREATEST x WRT m. P" == "CONST GreatestM m (%x. P)"
   578 
   579 lemma GreatestMI2:
   580   "P x ==> (!!y. P y ==> m y <= m x)
   581     ==> (!!x. P x ==> \<forall>y. P y --> m y \<le> m x ==> Q x)
   582     ==> Q (GreatestM m P)"
   583   apply (simp add: GreatestM_def)
   584   apply (rule someI2_ex, blast, blast)
   585   done
   586 
   587 lemma GreatestM_equality:
   588  "P k ==> (!!x. P x ==> m x <= m k)
   589     ==> m (GREATEST x WRT m. P x) = (m k::'a::order)"
   590   apply (rule_tac m = m in GreatestMI2, assumption, blast)
   591   apply (blast intro!: order_antisym)
   592   done
   593 
   594 lemma Greatest_equality:
   595   "P (k::'a::order) ==> (!!x. P x ==> x <= k) ==> (GREATEST x. P x) = k"
   596   apply (simp add: Greatest_def)
   597   apply (erule GreatestM_equality, blast)
   598   done
   599 
   600 lemma ex_has_greatest_nat_lemma:
   601   "P k ==> \<forall>x. P x --> (\<exists>y. P y & ~ ((m y::nat) <= m x))
   602     ==> \<exists>y. P y & ~ (m y < m k + n)"
   603   apply (induct n, force)
   604   apply (force simp add: le_Suc_eq)
   605   done
   606 
   607 lemma ex_has_greatest_nat:
   608   "P k ==> \<forall>y. P y --> m y < b
   609     ==> \<exists>x. P x & (\<forall>y. P y --> (m y::nat) <= m x)"
   610   apply (rule ccontr)
   611   apply (cut_tac P = P and n = "b - m k" in ex_has_greatest_nat_lemma)
   612     apply (subgoal_tac [3] "m k <= b", auto)
   613   done
   614 
   615 lemma GreatestM_nat_lemma:
   616   "P k ==> \<forall>y. P y --> m y < b
   617     ==> P (GreatestM m P) & (\<forall>y. P y --> (m y::nat) <= m (GreatestM m P))"
   618   apply (simp add: GreatestM_def)
   619   apply (rule someI_ex)
   620   apply (erule ex_has_greatest_nat, assumption)
   621   done
   622 
   623 lemmas GreatestM_natI = GreatestM_nat_lemma [THEN conjunct1]
   624 
   625 lemma GreatestM_nat_le:
   626   "P x ==> \<forall>y. P y --> m y < b
   627     ==> (m x::nat) <= m (GreatestM m P)"
   628   apply (blast dest: GreatestM_nat_lemma [THEN conjunct2, THEN spec, of P])
   629   done
   630 
   631 
   632 text {* \medskip Specialization to @{text GREATEST}. *}
   633 
   634 lemma GreatestI: "P (k::nat) ==> \<forall>y. P y --> y < b ==> P (GREATEST x. P x)"
   635   apply (simp add: Greatest_def)
   636   apply (rule GreatestM_natI, auto)
   637   done
   638 
   639 lemma Greatest_le:
   640     "P x ==> \<forall>y. P y --> y < b ==> (x::nat) <= (GREATEST x. P x)"
   641   apply (simp add: Greatest_def)
   642   apply (rule GreatestM_nat_le, auto)
   643   done
   644 
   645 
   646 subsection {* Specification package -- Hilbertized version *}
   647 
   648 lemma exE_some: "[| Ex P ; c == Eps P |] ==> P c"
   649   by (simp only: someI_ex)
   650 
   651 ML_file "Tools/choice_specification.ML"
   652 
   653 end