src/HOL/ex/Dedekind_Real.thy
author haftmann
Mon Mar 23 19:05:14 2015 +0100 (2015-03-23)
changeset 59814 2d9cf954a829
parent 57514 bdc2c6b40bf2
child 59815 cce82e360c2f
permissions -rw-r--r--
modernized
     1 (*  Title:      HOL/ex/Dedekind_Real.thy
     2     Author:     Jacques D. Fleuriot, University of Cambridge
     3     Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
     4 
     5 The positive reals as Dedekind sections of positive
     6 rationals. Fundamentals of Abstract Analysis [Gleason- p. 121]
     7 provides some of the definitions.
     8 *)
     9 
    10 theory Dedekind_Real
    11 imports Complex_Main
    12 begin
    13 
    14 section {* Positive real numbers *}
    15 
    16 text{*Could be generalized and moved to @{text Groups}*}
    17 lemma add_eq_exists: "\<exists>x. a+x = (b::rat)"
    18 by (rule_tac x="b-a" in exI, simp)
    19 
    20 definition
    21   cut :: "rat set => bool" where
    22   "cut A = ({} \<subset> A &
    23             A < {r. 0 < r} &
    24             (\<forall>y \<in> A. ((\<forall>z. 0<z & z < y --> z \<in> A) & (\<exists>u \<in> A. y < u))))"
    25 
    26 lemma interval_empty_iff:
    27   "{y. (x::'a::unbounded_dense_linorder) < y \<and> y < z} = {} \<longleftrightarrow> \<not> x < z"
    28   by (auto dest: dense)
    29 
    30 
    31 lemma cut_of_rat: 
    32   assumes q: "0 < q" shows "cut {r::rat. 0 < r & r < q}" (is "cut ?A")
    33 proof -
    34   from q have pos: "?A < {r. 0 < r}" by force
    35   have nonempty: "{} \<subset> ?A"
    36   proof
    37     show "{} \<subseteq> ?A" by simp
    38     show "{} \<noteq> ?A"
    39       by (force simp only: q eq_commute [of "{}"] interval_empty_iff)
    40   qed
    41   show ?thesis
    42     by (simp add: cut_def pos nonempty,
    43         blast dest: dense intro: order_less_trans)
    44 qed
    45 
    46 
    47 typedef preal = "Collect cut"
    48   by (blast intro: cut_of_rat [OF zero_less_one])
    49 
    50 lemma Abs_preal_induct [induct type: preal]:
    51   "(\<And>x. cut x \<Longrightarrow> P (Abs_preal x)) \<Longrightarrow> P x"
    52   using Abs_preal_induct [of P x] by simp
    53 
    54 lemma Rep_preal:
    55   "cut (Rep_preal x)"
    56   using Rep_preal [of x] by simp
    57 
    58 definition
    59   psup :: "preal set => preal" where
    60   "psup P = Abs_preal (\<Union>X \<in> P. Rep_preal X)"
    61 
    62 definition
    63   add_set :: "[rat set,rat set] => rat set" where
    64   "add_set A B = {w. \<exists>x \<in> A. \<exists>y \<in> B. w = x + y}"
    65 
    66 definition
    67   diff_set :: "[rat set,rat set] => rat set" where
    68   "diff_set A B = {w. \<exists>x. 0 < w & 0 < x & x \<notin> B & x + w \<in> A}"
    69 
    70 definition
    71   mult_set :: "[rat set,rat set] => rat set" where
    72   "mult_set A B = {w. \<exists>x \<in> A. \<exists>y \<in> B. w = x * y}"
    73 
    74 definition
    75   inverse_set :: "rat set => rat set" where
    76   "inverse_set A = {x. \<exists>y. 0 < x & x < y & inverse y \<notin> A}"
    77 
    78 instantiation preal :: "{ord, plus, minus, times, inverse, one}"
    79 begin
    80 
    81 definition
    82   preal_less_def:
    83     "R < S == Rep_preal R < Rep_preal S"
    84 
    85 definition
    86   preal_le_def:
    87     "R \<le> S == Rep_preal R \<subseteq> Rep_preal S"
    88 
    89 definition
    90   preal_add_def:
    91     "R + S == Abs_preal (add_set (Rep_preal R) (Rep_preal S))"
    92 
    93 definition
    94   preal_diff_def:
    95     "R - S == Abs_preal (diff_set (Rep_preal R) (Rep_preal S))"
    96 
    97 definition
    98   preal_mult_def:
    99     "R * S == Abs_preal (mult_set (Rep_preal R) (Rep_preal S))"
   100 
   101 definition
   102   preal_inverse_def:
   103     "inverse R == Abs_preal (inverse_set (Rep_preal R))"
   104 
   105 definition "R / S = R * inverse (S\<Colon>preal)"
   106 
   107 definition
   108   preal_one_def:
   109     "1 == Abs_preal {x. 0 < x & x < 1}"
   110 
   111 instance ..
   112 
   113 end
   114 
   115 
   116 text{*Reduces equality on abstractions to equality on representatives*}
   117 declare Abs_preal_inject [simp]
   118 declare Abs_preal_inverse [simp]
   119 
   120 lemma rat_mem_preal: "0 < q ==> cut {r::rat. 0 < r & r < q}"
   121 by (simp add: cut_of_rat)
   122 
   123 lemma preal_nonempty: "cut A ==> \<exists>x\<in>A. 0 < x"
   124   unfolding cut_def [abs_def] by blast
   125 
   126 lemma preal_Ex_mem: "cut A \<Longrightarrow> \<exists>x. x \<in> A"
   127   apply (drule preal_nonempty)
   128   apply fast
   129   done
   130 
   131 lemma preal_imp_psubset_positives: "cut A ==> A < {r. 0 < r}"
   132   by (force simp add: cut_def)
   133 
   134 lemma preal_exists_bound: "cut A ==> \<exists>x. 0 < x & x \<notin> A"
   135   apply (drule preal_imp_psubset_positives)
   136   apply auto
   137   done
   138 
   139 lemma preal_exists_greater: "[| cut A; y \<in> A |] ==> \<exists>u \<in> A. y < u"
   140   unfolding cut_def [abs_def] by blast
   141 
   142 lemma preal_downwards_closed: "[| cut A; y \<in> A; 0 < z; z < y |] ==> z \<in> A"
   143   unfolding cut_def [abs_def] by blast
   144 
   145 text{*Relaxing the final premise*}
   146 lemma preal_downwards_closed':
   147      "[| cut A; y \<in> A; 0 < z; z \<le> y |] ==> z \<in> A"
   148 apply (simp add: order_le_less)
   149 apply (blast intro: preal_downwards_closed)
   150 done
   151 
   152 text{*A positive fraction not in a positive real is an upper bound.
   153  Gleason p. 122 - Remark (1)*}
   154 
   155 lemma not_in_preal_ub:
   156   assumes A: "cut A"
   157     and notx: "x \<notin> A"
   158     and y: "y \<in> A"
   159     and pos: "0 < x"
   160   shows "y < x"
   161 proof (cases rule: linorder_cases)
   162   assume "x<y"
   163   with notx show ?thesis
   164     by (simp add:  preal_downwards_closed [OF A y] pos)
   165 next
   166   assume "x=y"
   167   with notx and y show ?thesis by simp
   168 next
   169   assume "y<x"
   170   thus ?thesis .
   171 qed
   172 
   173 text {* preal lemmas instantiated to @{term "Rep_preal X"} *}
   174 
   175 lemma mem_Rep_preal_Ex: "\<exists>x. x \<in> Rep_preal X"
   176 thm preal_Ex_mem
   177 by (rule preal_Ex_mem [OF Rep_preal])
   178 
   179 lemma Rep_preal_exists_bound: "\<exists>x>0. x \<notin> Rep_preal X"
   180 by (rule preal_exists_bound [OF Rep_preal])
   181 
   182 lemmas not_in_Rep_preal_ub = not_in_preal_ub [OF Rep_preal]
   183 
   184 
   185 subsection{*Properties of Ordering*}
   186 
   187 instance preal :: order
   188 proof
   189   fix w :: preal
   190   show "w \<le> w" by (simp add: preal_le_def)
   191 next
   192   fix i j k :: preal
   193   assume "i \<le> j" and "j \<le> k"
   194   then show "i \<le> k" by (simp add: preal_le_def)
   195 next
   196   fix z w :: preal
   197   assume "z \<le> w" and "w \<le> z"
   198   then show "z = w" by (simp add: preal_le_def Rep_preal_inject)
   199 next
   200   fix z w :: preal
   201   show "z < w \<longleftrightarrow> z \<le> w \<and> \<not> w \<le> z"
   202   by (auto simp add: preal_le_def preal_less_def Rep_preal_inject)
   203 qed  
   204 
   205 lemma preal_imp_pos: "[|cut A; r \<in> A|] ==> 0 < r"
   206 by (insert preal_imp_psubset_positives, blast)
   207 
   208 instance preal :: linorder
   209 proof
   210   fix x y :: preal
   211   show "x <= y | y <= x"
   212     apply (auto simp add: preal_le_def)
   213     apply (rule ccontr)
   214     apply (blast dest: not_in_Rep_preal_ub intro: preal_imp_pos [OF Rep_preal]
   215              elim: order_less_asym)
   216     done
   217 qed
   218 
   219 instantiation preal :: distrib_lattice
   220 begin
   221 
   222 definition
   223   "(inf \<Colon> preal \<Rightarrow> preal \<Rightarrow> preal) = min"
   224 
   225 definition
   226   "(sup \<Colon> preal \<Rightarrow> preal \<Rightarrow> preal) = max"
   227 
   228 instance
   229   by intro_classes
   230     (auto simp add: inf_preal_def sup_preal_def max_min_distrib2)
   231 
   232 end
   233 
   234 subsection{*Properties of Addition*}
   235 
   236 lemma preal_add_commute: "(x::preal) + y = y + x"
   237 apply (unfold preal_add_def add_set_def)
   238 apply (rule_tac f = Abs_preal in arg_cong)
   239 apply (force simp add: add.commute)
   240 done
   241 
   242 text{*Lemmas for proving that addition of two positive reals gives
   243  a positive real*}
   244 
   245 text{*Part 1 of Dedekind sections definition*}
   246 lemma add_set_not_empty:
   247      "[|cut A; cut B|] ==> {} \<subset> add_set A B"
   248 apply (drule preal_nonempty)+
   249 apply (auto simp add: add_set_def)
   250 done
   251 
   252 text{*Part 2 of Dedekind sections definition.  A structured version of
   253 this proof is @{text preal_not_mem_mult_set_Ex} below.*}
   254 lemma preal_not_mem_add_set_Ex:
   255      "[|cut A; cut B|] ==> \<exists>q>0. q \<notin> add_set A B"
   256 apply (insert preal_exists_bound [of A] preal_exists_bound [of B], auto) 
   257 apply (rule_tac x = "x+xa" in exI)
   258 apply (simp add: add_set_def, clarify)
   259 apply (drule (3) not_in_preal_ub)+
   260 apply (force dest: add_strict_mono)
   261 done
   262 
   263 lemma add_set_not_rat_set:
   264    assumes A: "cut A" 
   265        and B: "cut B"
   266      shows "add_set A B < {r. 0 < r}"
   267 proof
   268   from preal_imp_pos [OF A] preal_imp_pos [OF B]
   269   show "add_set A B \<subseteq> {r. 0 < r}" by (force simp add: add_set_def) 
   270 next
   271   show "add_set A B \<noteq> {r. 0 < r}"
   272     by (insert preal_not_mem_add_set_Ex [OF A B], blast) 
   273 qed
   274 
   275 text{*Part 3 of Dedekind sections definition*}
   276 lemma add_set_lemma3:
   277      "[|cut A; cut B; u \<in> add_set A B; 0 < z; z < u|] 
   278       ==> z \<in> add_set A B"
   279 proof (unfold add_set_def, clarify)
   280   fix x::rat and y::rat
   281   assume A: "cut A" 
   282     and B: "cut B"
   283     and [simp]: "0 < z"
   284     and zless: "z < x + y"
   285     and x:  "x \<in> A"
   286     and y:  "y \<in> B"
   287   have xpos [simp]: "0<x" by (rule preal_imp_pos [OF A x])
   288   have ypos [simp]: "0<y" by (rule preal_imp_pos [OF B y])
   289   have xypos [simp]: "0 < x+y" by (simp add: pos_add_strict)
   290   let ?f = "z/(x+y)"
   291   have fless: "?f < 1" by (simp add: zless pos_divide_less_eq)
   292   show "\<exists>x' \<in> A. \<exists>y'\<in>B. z = x' + y'"
   293   proof (intro bexI)
   294     show "z = x*?f + y*?f"
   295       by (simp add: distrib_right [symmetric] divide_inverse ac_simps
   296           order_less_imp_not_eq2)
   297   next
   298     show "y * ?f \<in> B"
   299     proof (rule preal_downwards_closed [OF B y])
   300       show "0 < y * ?f"
   301         by (simp add: divide_inverse zero_less_mult_iff)
   302     next
   303       show "y * ?f < y"
   304         by (insert mult_strict_left_mono [OF fless ypos], simp)
   305     qed
   306   next
   307     show "x * ?f \<in> A"
   308     proof (rule preal_downwards_closed [OF A x])
   309       show "0 < x * ?f"
   310         by (simp add: divide_inverse zero_less_mult_iff)
   311     next
   312       show "x * ?f < x"
   313         by (insert mult_strict_left_mono [OF fless xpos], simp)
   314     qed
   315   qed
   316 qed
   317 
   318 text{*Part 4 of Dedekind sections definition*}
   319 lemma add_set_lemma4:
   320      "[|cut A; cut B; y \<in> add_set A B|] ==> \<exists>u \<in> add_set A B. y < u"
   321 apply (auto simp add: add_set_def)
   322 apply (frule preal_exists_greater [of A], auto) 
   323 apply (rule_tac x="u + ya" in exI)
   324 apply (auto intro: add_strict_left_mono)
   325 done
   326 
   327 lemma mem_add_set:
   328      "[|cut A; cut B|] ==> cut (add_set A B)"
   329 apply (simp (no_asm_simp) add: cut_def)
   330 apply (blast intro!: add_set_not_empty add_set_not_rat_set
   331                      add_set_lemma3 add_set_lemma4)
   332 done
   333 
   334 lemma preal_add_assoc: "((x::preal) + y) + z = x + (y + z)"
   335 apply (simp add: preal_add_def mem_add_set Rep_preal)
   336 apply (force simp add: add_set_def ac_simps)
   337 done
   338 
   339 instance preal :: ab_semigroup_add
   340 proof
   341   fix a b c :: preal
   342   show "(a + b) + c = a + (b + c)" by (rule preal_add_assoc)
   343   show "a + b = b + a" by (rule preal_add_commute)
   344 qed
   345 
   346 
   347 subsection{*Properties of Multiplication*}
   348 
   349 text{*Proofs essentially same as for addition*}
   350 
   351 lemma preal_mult_commute: "(x::preal) * y = y * x"
   352 apply (unfold preal_mult_def mult_set_def)
   353 apply (rule_tac f = Abs_preal in arg_cong)
   354 apply (force simp add: mult.commute)
   355 done
   356 
   357 text{*Multiplication of two positive reals gives a positive real.*}
   358 
   359 text{*Lemmas for proving positive reals multiplication set in @{typ preal}*}
   360 
   361 text{*Part 1 of Dedekind sections definition*}
   362 lemma mult_set_not_empty:
   363      "[|cut A; cut B|] ==> {} \<subset> mult_set A B"
   364 apply (insert preal_nonempty [of A] preal_nonempty [of B]) 
   365 apply (auto simp add: mult_set_def)
   366 done
   367 
   368 text{*Part 2 of Dedekind sections definition*}
   369 lemma preal_not_mem_mult_set_Ex:
   370   assumes A: "cut A" 
   371     and B: "cut B"
   372   shows "\<exists>q. 0 < q & q \<notin> mult_set A B"
   373 proof -
   374   from preal_exists_bound [OF A] obtain x where 1 [simp]: "0 < x" "x \<notin> A" by blast
   375   from preal_exists_bound [OF B] obtain y where 2 [simp]: "0 < y" "y \<notin> B" by blast
   376   show ?thesis
   377   proof (intro exI conjI)
   378     show "0 < x*y" by simp
   379     show "x * y \<notin> mult_set A B"
   380     proof -
   381       {
   382         fix u::rat and v::rat
   383         assume u: "u \<in> A" and v: "v \<in> B" and xy: "x*y = u*v"
   384         moreover from A B 1 2 u v have "u<x" and "v<y" by (blast dest: not_in_preal_ub)+
   385         moreover
   386         from A B 1 2 u v have "0\<le>v"
   387           by (blast intro: preal_imp_pos [OF B] order_less_imp_le)
   388         moreover
   389         from A B 1 `u < x` `v < y` `0 \<le> v`
   390         have "u*v < x*y" by (blast intro: mult_strict_mono)
   391         ultimately have False by force
   392       }
   393       thus ?thesis by (auto simp add: mult_set_def)
   394     qed
   395   qed
   396 qed
   397 
   398 lemma mult_set_not_rat_set:
   399   assumes A: "cut A" 
   400     and B: "cut B"
   401   shows "mult_set A B < {r. 0 < r}"
   402 proof
   403   show "mult_set A B \<subseteq> {r. 0 < r}"
   404     by (force simp add: mult_set_def
   405       intro: preal_imp_pos [OF A] preal_imp_pos [OF B] mult_pos_pos)
   406   show "mult_set A B \<noteq> {r. 0 < r}"
   407     using preal_not_mem_mult_set_Ex [OF A B] by blast
   408 qed
   409 
   410 
   411 
   412 text{*Part 3 of Dedekind sections definition*}
   413 lemma mult_set_lemma3:
   414      "[|cut A; cut B; u \<in> mult_set A B; 0 < z; z < u|] 
   415       ==> z \<in> mult_set A B"
   416 proof (unfold mult_set_def, clarify)
   417   fix x::rat and y::rat
   418   assume A: "cut A" 
   419     and B: "cut B"
   420     and [simp]: "0 < z"
   421     and zless: "z < x * y"
   422     and x:  "x \<in> A"
   423     and y:  "y \<in> B"
   424   have [simp]: "0<y" by (rule preal_imp_pos [OF B y])
   425   show "\<exists>x' \<in> A. \<exists>y' \<in> B. z = x' * y'"
   426   proof
   427     show "\<exists>y'\<in>B. z = (z/y) * y'"
   428     proof
   429       show "z = (z/y)*y"
   430         by (simp add: divide_inverse mult.commute [of y] mult.assoc
   431                       order_less_imp_not_eq2)
   432       show "y \<in> B" by fact
   433     qed
   434   next
   435     show "z/y \<in> A"
   436     proof (rule preal_downwards_closed [OF A x])
   437       show "0 < z/y"
   438         by (simp add: zero_less_divide_iff)
   439       show "z/y < x" by (simp add: pos_divide_less_eq zless)
   440     qed
   441   qed
   442 qed
   443 
   444 text{*Part 4 of Dedekind sections definition*}
   445 lemma mult_set_lemma4:
   446      "[|cut A; cut B; y \<in> mult_set A B|] ==> \<exists>u \<in> mult_set A B. y < u"
   447 apply (auto simp add: mult_set_def)
   448 apply (frule preal_exists_greater [of A], auto) 
   449 apply (rule_tac x="u * ya" in exI)
   450 apply (auto intro: preal_imp_pos [of A] preal_imp_pos [of B] 
   451                    mult_strict_right_mono)
   452 done
   453 
   454 
   455 lemma mem_mult_set:
   456      "[|cut A; cut B|] ==> cut (mult_set A B)"
   457 apply (simp (no_asm_simp) add: cut_def)
   458 apply (blast intro!: mult_set_not_empty mult_set_not_rat_set
   459                      mult_set_lemma3 mult_set_lemma4)
   460 done
   461 
   462 lemma preal_mult_assoc: "((x::preal) * y) * z = x * (y * z)"
   463 apply (simp add: preal_mult_def mem_mult_set Rep_preal)
   464 apply (force simp add: mult_set_def ac_simps)
   465 done
   466 
   467 instance preal :: ab_semigroup_mult
   468 proof
   469   fix a b c :: preal
   470   show "(a * b) * c = a * (b * c)" by (rule preal_mult_assoc)
   471   show "a * b = b * a" by (rule preal_mult_commute)
   472 qed
   473 
   474 
   475 text{* Positive real 1 is the multiplicative identity element *}
   476 
   477 lemma preal_mult_1: "(1::preal) * z = z"
   478 proof (induct z)
   479   fix A :: "rat set"
   480   assume A: "cut A"
   481   have "{w. \<exists>u. 0 < u \<and> u < 1 & (\<exists>v \<in> A. w = u * v)} = A" (is "?lhs = A")
   482   proof
   483     show "?lhs \<subseteq> A"
   484     proof clarify
   485       fix x::rat and u::rat and v::rat
   486       assume upos: "0<u" and "u<1" and v: "v \<in> A"
   487       have vpos: "0<v" by (rule preal_imp_pos [OF A v])
   488       hence "u*v < 1*v" by (simp only: mult_strict_right_mono upos `u < 1` v)
   489       thus "u * v \<in> A"
   490         by (force intro: preal_downwards_closed [OF A v] mult_pos_pos 
   491           upos vpos)
   492     qed
   493   next
   494     show "A \<subseteq> ?lhs"
   495     proof clarify
   496       fix x::rat
   497       assume x: "x \<in> A"
   498       have xpos: "0<x" by (rule preal_imp_pos [OF A x])
   499       from preal_exists_greater [OF A x]
   500       obtain v where v: "v \<in> A" and xlessv: "x < v" ..
   501       have vpos: "0<v" by (rule preal_imp_pos [OF A v])
   502       show "\<exists>u. 0 < u \<and> u < 1 \<and> (\<exists>v\<in>A. x = u * v)"
   503       proof (intro exI conjI)
   504         show "0 < x/v"
   505           by (simp add: zero_less_divide_iff xpos vpos)
   506         show "x / v < 1"
   507           by (simp add: pos_divide_less_eq vpos xlessv)
   508         show "\<exists>v'\<in>A. x = (x / v) * v'"
   509         proof
   510           show "x = (x/v)*v"
   511             by (simp add: divide_inverse mult.assoc vpos
   512                           order_less_imp_not_eq2)
   513           show "v \<in> A" by fact
   514         qed
   515       qed
   516     qed
   517   qed
   518   thus "1 * Abs_preal A = Abs_preal A"
   519     by (simp add: preal_one_def preal_mult_def mult_set_def 
   520                   rat_mem_preal A)
   521 qed
   522 
   523 instance preal :: comm_monoid_mult
   524 by intro_classes (rule preal_mult_1)
   525 
   526 
   527 subsection{*Distribution of Multiplication across Addition*}
   528 
   529 lemma mem_Rep_preal_add_iff:
   530       "(z \<in> Rep_preal(R+S)) = (\<exists>x \<in> Rep_preal R. \<exists>y \<in> Rep_preal S. z = x + y)"
   531 apply (simp add: preal_add_def mem_add_set Rep_preal)
   532 apply (simp add: add_set_def) 
   533 done
   534 
   535 lemma mem_Rep_preal_mult_iff:
   536       "(z \<in> Rep_preal(R*S)) = (\<exists>x \<in> Rep_preal R. \<exists>y \<in> Rep_preal S. z = x * y)"
   537 apply (simp add: preal_mult_def mem_mult_set Rep_preal)
   538 apply (simp add: mult_set_def) 
   539 done
   540 
   541 lemma distrib_subset1:
   542      "Rep_preal (w * (x + y)) \<subseteq> Rep_preal (w * x + w * y)"
   543 apply (auto simp add: Bex_def mem_Rep_preal_add_iff mem_Rep_preal_mult_iff)
   544 apply (force simp add: distrib_left)
   545 done
   546 
   547 lemma preal_add_mult_distrib_mean:
   548   assumes a: "a \<in> Rep_preal w"
   549     and b: "b \<in> Rep_preal w"
   550     and d: "d \<in> Rep_preal x"
   551     and e: "e \<in> Rep_preal y"
   552   shows "\<exists>c \<in> Rep_preal w. a * d + b * e = c * (d + e)"
   553 proof
   554   let ?c = "(a*d + b*e)/(d+e)"
   555   have [simp]: "0<a" "0<b" "0<d" "0<e" "0<d+e"
   556     by (blast intro: preal_imp_pos [OF Rep_preal] a b d e pos_add_strict)+
   557   have cpos: "0 < ?c"
   558     by (simp add: zero_less_divide_iff zero_less_mult_iff pos_add_strict)
   559   show "a * d + b * e = ?c * (d + e)"
   560     by (simp add: divide_inverse mult.assoc order_less_imp_not_eq2)
   561   show "?c \<in> Rep_preal w"
   562   proof (cases rule: linorder_le_cases)
   563     assume "a \<le> b"
   564     hence "?c \<le> b"
   565       by (simp add: pos_divide_le_eq distrib_left mult_right_mono
   566                     order_less_imp_le)
   567     thus ?thesis by (rule preal_downwards_closed' [OF Rep_preal b cpos])
   568   next
   569     assume "b \<le> a"
   570     hence "?c \<le> a"
   571       by (simp add: pos_divide_le_eq distrib_left mult_right_mono
   572                     order_less_imp_le)
   573     thus ?thesis by (rule preal_downwards_closed' [OF Rep_preal a cpos])
   574   qed
   575 qed
   576 
   577 lemma distrib_subset2:
   578      "Rep_preal (w * x + w * y) \<subseteq> Rep_preal (w * (x + y))"
   579 apply (auto simp add: Bex_def mem_Rep_preal_add_iff mem_Rep_preal_mult_iff)
   580 apply (drule_tac w=w and x=x and y=y in preal_add_mult_distrib_mean, auto)
   581 done
   582 
   583 lemma preal_add_mult_distrib2: "(w * ((x::preal) + y)) = (w * x) + (w * y)"
   584 apply (rule Rep_preal_inject [THEN iffD1])
   585 apply (rule equalityI [OF distrib_subset1 distrib_subset2])
   586 done
   587 
   588 lemma preal_add_mult_distrib: "(((x::preal) + y) * w) = (x * w) + (y * w)"
   589 by (simp add: preal_mult_commute preal_add_mult_distrib2)
   590 
   591 instance preal :: comm_semiring
   592 by intro_classes (rule preal_add_mult_distrib)
   593 
   594 
   595 subsection{*Existence of Inverse, a Positive Real*}
   596 
   597 lemma mem_inv_set_ex:
   598   assumes A: "cut A" shows "\<exists>x y. 0 < x & x < y & inverse y \<notin> A"
   599 proof -
   600   from preal_exists_bound [OF A]
   601   obtain x where [simp]: "0<x" "x \<notin> A" by blast
   602   show ?thesis
   603   proof (intro exI conjI)
   604     show "0 < inverse (x+1)"
   605       by (simp add: order_less_trans [OF _ less_add_one]) 
   606     show "inverse(x+1) < inverse x"
   607       by (simp add: less_imp_inverse_less less_add_one)
   608     show "inverse (inverse x) \<notin> A"
   609       by (simp add: order_less_imp_not_eq2)
   610   qed
   611 qed
   612 
   613 text{*Part 1 of Dedekind sections definition*}
   614 lemma inverse_set_not_empty:
   615      "cut A ==> {} \<subset> inverse_set A"
   616 apply (insert mem_inv_set_ex [of A])
   617 apply (auto simp add: inverse_set_def)
   618 done
   619 
   620 text{*Part 2 of Dedekind sections definition*}
   621 
   622 lemma preal_not_mem_inverse_set_Ex:
   623    assumes A: "cut A"  shows "\<exists>q. 0 < q & q \<notin> inverse_set A"
   624 proof -
   625   from preal_nonempty [OF A]
   626   obtain x where x: "x \<in> A" and  xpos [simp]: "0<x" ..
   627   show ?thesis
   628   proof (intro exI conjI)
   629     show "0 < inverse x" by simp
   630     show "inverse x \<notin> inverse_set A"
   631     proof -
   632       { fix y::rat 
   633         assume ygt: "inverse x < y"
   634         have [simp]: "0 < y" by (simp add: order_less_trans [OF _ ygt])
   635         have iyless: "inverse y < x" 
   636           by (simp add: inverse_less_imp_less [of x] ygt)
   637         have "inverse y \<in> A"
   638           by (simp add: preal_downwards_closed [OF A x] iyless)}
   639      thus ?thesis by (auto simp add: inverse_set_def)
   640     qed
   641   qed
   642 qed
   643 
   644 lemma inverse_set_not_rat_set:
   645    assumes A: "cut A"  shows "inverse_set A < {r. 0 < r}"
   646 proof
   647   show "inverse_set A \<subseteq> {r. 0 < r}"  by (force simp add: inverse_set_def)
   648 next
   649   show "inverse_set A \<noteq> {r. 0 < r}"
   650     by (insert preal_not_mem_inverse_set_Ex [OF A], blast)
   651 qed
   652 
   653 text{*Part 3 of Dedekind sections definition*}
   654 lemma inverse_set_lemma3:
   655      "[|cut A; u \<in> inverse_set A; 0 < z; z < u|] 
   656       ==> z \<in> inverse_set A"
   657 apply (auto simp add: inverse_set_def)
   658 apply (auto intro: order_less_trans)
   659 done
   660 
   661 text{*Part 4 of Dedekind sections definition*}
   662 lemma inverse_set_lemma4:
   663      "[|cut A; y \<in> inverse_set A|] ==> \<exists>u \<in> inverse_set A. y < u"
   664 apply (auto simp add: inverse_set_def)
   665 apply (drule dense [of y]) 
   666 apply (blast intro: order_less_trans)
   667 done
   668 
   669 
   670 lemma mem_inverse_set:
   671      "cut A ==> cut (inverse_set A)"
   672 apply (simp (no_asm_simp) add: cut_def)
   673 apply (blast intro!: inverse_set_not_empty inverse_set_not_rat_set
   674                      inverse_set_lemma3 inverse_set_lemma4)
   675 done
   676 
   677 
   678 subsection{*Gleason's Lemma 9-3.4, page 122*}
   679 
   680 lemma Gleason9_34_exists:
   681   assumes A: "cut A"
   682     and "\<forall>x\<in>A. x + u \<in> A"
   683     and "0 \<le> z"
   684   shows "\<exists>b\<in>A. b + (of_int z) * u \<in> A"
   685 proof (cases z rule: int_cases)
   686   case (nonneg n)
   687   show ?thesis
   688   proof (simp add: nonneg, induct n)
   689     case 0
   690     from preal_nonempty [OF A]
   691     show ?case  by force 
   692   next
   693     case (Suc k)
   694     then obtain b where b: "b \<in> A" "b + of_nat k * u \<in> A" ..
   695     hence "b + of_int (int k)*u + u \<in> A" by (simp add: assms)
   696     thus ?case by (force simp add: algebra_simps b)
   697   qed
   698 next
   699   case (neg n)
   700   with assms show ?thesis by simp
   701 qed
   702 
   703 lemma Gleason9_34_contra:
   704   assumes A: "cut A"
   705     shows "[|\<forall>x\<in>A. x + u \<in> A; 0 < u; 0 < y; y \<notin> A|] ==> False"
   706 proof (induct u, induct y)
   707   fix a::int and b::int
   708   fix c::int and d::int
   709   assume bpos [simp]: "0 < b"
   710     and dpos [simp]: "0 < d"
   711     and closed: "\<forall>x\<in>A. x + (Fract c d) \<in> A"
   712     and upos: "0 < Fract c d"
   713     and ypos: "0 < Fract a b"
   714     and notin: "Fract a b \<notin> A"
   715   have cpos [simp]: "0 < c" 
   716     by (simp add: zero_less_Fract_iff [OF dpos, symmetric] upos) 
   717   have apos [simp]: "0 < a" 
   718     by (simp add: zero_less_Fract_iff [OF bpos, symmetric] ypos) 
   719   let ?k = "a*d"
   720   have frle: "Fract a b \<le> Fract ?k 1 * (Fract c d)" 
   721   proof -
   722     have "?thesis = ((a * d * b * d) \<le> c * b * (a * d * b * d))"
   723       by (simp add: order_less_imp_not_eq2 ac_simps) 
   724     moreover
   725     have "(1 * (a * d * b * d)) \<le> c * b * (a * d * b * d)"
   726       by (rule mult_mono, 
   727           simp_all add: int_one_le_iff_zero_less zero_less_mult_iff 
   728                         order_less_imp_le)
   729     ultimately
   730     show ?thesis by simp
   731   qed
   732   have k: "0 \<le> ?k" by (simp add: order_less_imp_le zero_less_mult_iff)  
   733   from Gleason9_34_exists [OF A closed k]
   734   obtain z where z: "z \<in> A" 
   735              and mem: "z + of_int ?k * Fract c d \<in> A" ..
   736   have less: "z + of_int ?k * Fract c d < Fract a b"
   737     by (rule not_in_preal_ub [OF A notin mem ypos])
   738   have "0<z" by (rule preal_imp_pos [OF A z])
   739   with frle and less show False by (simp add: Fract_of_int_eq) 
   740 qed
   741 
   742 
   743 lemma Gleason9_34:
   744   assumes A: "cut A"
   745     and upos: "0 < u"
   746   shows "\<exists>r \<in> A. r + u \<notin> A"
   747 proof (rule ccontr, simp)
   748   assume closed: "\<forall>r\<in>A. r + u \<in> A"
   749   from preal_exists_bound [OF A]
   750   obtain y where y: "y \<notin> A" and ypos: "0 < y" by blast
   751   show False
   752     by (rule Gleason9_34_contra [OF A closed upos ypos y])
   753 qed
   754 
   755 
   756 
   757 subsection{*Gleason's Lemma 9-3.6*}
   758 
   759 lemma lemma_gleason9_36:
   760   assumes A: "cut A"
   761     and x: "1 < x"
   762   shows "\<exists>r \<in> A. r*x \<notin> A"
   763 proof -
   764   from preal_nonempty [OF A]
   765   obtain y where y: "y \<in> A" and  ypos: "0<y" ..
   766   show ?thesis 
   767   proof (rule classical)
   768     assume "~(\<exists>r\<in>A. r * x \<notin> A)"
   769     with y have ymem: "y * x \<in> A" by blast 
   770     from ypos mult_strict_left_mono [OF x]
   771     have yless: "y < y*x" by simp 
   772     let ?d = "y*x - y"
   773     from yless have dpos: "0 < ?d" and eq: "y + ?d = y*x" by auto
   774     from Gleason9_34 [OF A dpos]
   775     obtain r where r: "r\<in>A" and notin: "r + ?d \<notin> A" ..
   776     have rpos: "0<r" by (rule preal_imp_pos [OF A r])
   777     with dpos have rdpos: "0 < r + ?d" by arith
   778     have "~ (r + ?d \<le> y + ?d)"
   779     proof
   780       assume le: "r + ?d \<le> y + ?d" 
   781       from ymem have yd: "y + ?d \<in> A" by (simp add: eq)
   782       have "r + ?d \<in> A" by (rule preal_downwards_closed' [OF A yd rdpos le])
   783       with notin show False by simp
   784     qed
   785     hence "y < r" by simp
   786     with ypos have  dless: "?d < (r * ?d)/y"
   787       by (simp add: pos_less_divide_eq mult.commute [of ?d]
   788                     mult_strict_right_mono dpos)
   789     have "r + ?d < r*x"
   790     proof -
   791       have "r + ?d < r + (r * ?d)/y" by (simp add: dless)
   792       also from ypos have "... = (r/y) * (y + ?d)"
   793         by (simp only: algebra_simps divide_inverse, simp)
   794       also have "... = r*x" using ypos
   795         by simp
   796       finally show "r + ?d < r*x" .
   797     qed
   798     with r notin rdpos
   799     show "\<exists>r\<in>A. r * x \<notin> A" by (blast dest:  preal_downwards_closed [OF A])
   800   qed  
   801 qed
   802 
   803 subsection{*Existence of Inverse: Part 2*}
   804 
   805 lemma mem_Rep_preal_inverse_iff:
   806       "(z \<in> Rep_preal(inverse R)) = 
   807        (0 < z \<and> (\<exists>y. z < y \<and> inverse y \<notin> Rep_preal R))"
   808 apply (simp add: preal_inverse_def mem_inverse_set Rep_preal)
   809 apply (simp add: inverse_set_def) 
   810 done
   811 
   812 lemma Rep_preal_one:
   813      "Rep_preal 1 = {x. 0 < x \<and> x < 1}"
   814 by (simp add: preal_one_def rat_mem_preal)
   815 
   816 lemma subset_inverse_mult_lemma:
   817   assumes xpos: "0 < x" and xless: "x < 1"
   818   shows "\<exists>r u y. 0 < r & r < y & inverse y \<notin> Rep_preal R & 
   819     u \<in> Rep_preal R & x = r * u"
   820 proof -
   821   from xpos and xless have "1 < inverse x" by (simp add: one_less_inverse_iff)
   822   from lemma_gleason9_36 [OF Rep_preal this]
   823   obtain r where r: "r \<in> Rep_preal R" 
   824              and notin: "r * (inverse x) \<notin> Rep_preal R" ..
   825   have rpos: "0<r" by (rule preal_imp_pos [OF Rep_preal r])
   826   from preal_exists_greater [OF Rep_preal r]
   827   obtain u where u: "u \<in> Rep_preal R" and rless: "r < u" ..
   828   have upos: "0<u" by (rule preal_imp_pos [OF Rep_preal u])
   829   show ?thesis
   830   proof (intro exI conjI)
   831     show "0 < x/u" using xpos upos
   832       by (simp add: zero_less_divide_iff)  
   833     show "x/u < x/r" using xpos upos rpos
   834       by (simp add: divide_inverse mult_less_cancel_left rless) 
   835     show "inverse (x / r) \<notin> Rep_preal R" using notin
   836       by (simp add: divide_inverse mult.commute) 
   837     show "u \<in> Rep_preal R" by (rule u) 
   838     show "x = x / u * u" using upos 
   839       by (simp add: divide_inverse mult.commute) 
   840   qed
   841 qed
   842 
   843 lemma subset_inverse_mult: 
   844      "Rep_preal 1 \<subseteq> Rep_preal(inverse R * R)"
   845 apply (auto simp add: Bex_def Rep_preal_one mem_Rep_preal_inverse_iff 
   846                       mem_Rep_preal_mult_iff)
   847 apply (blast dest: subset_inverse_mult_lemma) 
   848 done
   849 
   850 lemma inverse_mult_subset_lemma:
   851   assumes rpos: "0 < r" 
   852     and rless: "r < y"
   853     and notin: "inverse y \<notin> Rep_preal R"
   854     and q: "q \<in> Rep_preal R"
   855   shows "r*q < 1"
   856 proof -
   857   have "q < inverse y" using rpos rless
   858     by (simp add: not_in_preal_ub [OF Rep_preal notin] q)
   859   hence "r * q < r/y" using rpos
   860     by (simp add: divide_inverse mult_less_cancel_left)
   861   also have "... \<le> 1" using rpos rless
   862     by (simp add: pos_divide_le_eq)
   863   finally show ?thesis .
   864 qed
   865 
   866 lemma inverse_mult_subset:
   867      "Rep_preal(inverse R * R) \<subseteq> Rep_preal 1"
   868 apply (auto simp add: Bex_def Rep_preal_one mem_Rep_preal_inverse_iff
   869                       mem_Rep_preal_mult_iff)
   870 apply (simp add: zero_less_mult_iff preal_imp_pos [OF Rep_preal]) 
   871 apply (blast intro: inverse_mult_subset_lemma) 
   872 done
   873 
   874 lemma preal_mult_inverse: "inverse R * R = (1::preal)"
   875 apply (rule Rep_preal_inject [THEN iffD1])
   876 apply (rule equalityI [OF inverse_mult_subset subset_inverse_mult]) 
   877 done
   878 
   879 lemma preal_mult_inverse_right: "R * inverse R = (1::preal)"
   880 apply (rule preal_mult_commute [THEN subst])
   881 apply (rule preal_mult_inverse)
   882 done
   883 
   884 
   885 text{*Theorems needing @{text Gleason9_34}*}
   886 
   887 lemma Rep_preal_self_subset: "Rep_preal (R) \<subseteq> Rep_preal(R + S)"
   888 proof 
   889   fix r
   890   assume r: "r \<in> Rep_preal R"
   891   have rpos: "0<r" by (rule preal_imp_pos [OF Rep_preal r])
   892   from mem_Rep_preal_Ex 
   893   obtain y where y: "y \<in> Rep_preal S" ..
   894   have ypos: "0<y" by (rule preal_imp_pos [OF Rep_preal y])
   895   have ry: "r+y \<in> Rep_preal(R + S)" using r y
   896     by (auto simp add: mem_Rep_preal_add_iff)
   897   show "r \<in> Rep_preal(R + S)" using r ypos rpos 
   898     by (simp add:  preal_downwards_closed [OF Rep_preal ry]) 
   899 qed
   900 
   901 lemma Rep_preal_sum_not_subset: "~ Rep_preal (R + S) \<subseteq> Rep_preal(R)"
   902 proof -
   903   from mem_Rep_preal_Ex 
   904   obtain y where y: "y \<in> Rep_preal S" ..
   905   have ypos: "0<y" by (rule preal_imp_pos [OF Rep_preal y])
   906   from  Gleason9_34 [OF Rep_preal ypos]
   907   obtain r where r: "r \<in> Rep_preal R" and notin: "r + y \<notin> Rep_preal R" ..
   908   have "r + y \<in> Rep_preal (R + S)" using r y
   909     by (auto simp add: mem_Rep_preal_add_iff)
   910   thus ?thesis using notin by blast
   911 qed
   912 
   913 lemma Rep_preal_sum_not_eq: "Rep_preal (R + S) \<noteq> Rep_preal(R)"
   914 by (insert Rep_preal_sum_not_subset, blast)
   915 
   916 text{*at last, Gleason prop. 9-3.5(iii) page 123*}
   917 lemma preal_self_less_add_left: "(R::preal) < R + S"
   918 apply (unfold preal_less_def less_le)
   919 apply (simp add: Rep_preal_self_subset Rep_preal_sum_not_eq [THEN not_sym])
   920 done
   921 
   922 
   923 subsection{*Subtraction for Positive Reals*}
   924 
   925 text{*Gleason prop. 9-3.5(iv), page 123: proving @{prop "A < B ==> \<exists>D. A + D =
   926 B"}. We define the claimed @{term D} and show that it is a positive real*}
   927 
   928 text{*Part 1 of Dedekind sections definition*}
   929 lemma diff_set_not_empty:
   930      "R < S ==> {} \<subset> diff_set (Rep_preal S) (Rep_preal R)"
   931 apply (auto simp add: preal_less_def diff_set_def elim!: equalityE) 
   932 apply (frule_tac x1 = S in Rep_preal [THEN preal_exists_greater])
   933 apply (drule preal_imp_pos [OF Rep_preal], clarify)
   934 apply (cut_tac a=x and b=u in add_eq_exists, force) 
   935 done
   936 
   937 text{*Part 2 of Dedekind sections definition*}
   938 lemma diff_set_nonempty:
   939      "\<exists>q. 0 < q & q \<notin> diff_set (Rep_preal S) (Rep_preal R)"
   940 apply (cut_tac X = S in Rep_preal_exists_bound)
   941 apply (erule exE)
   942 apply (rule_tac x = x in exI, auto)
   943 apply (simp add: diff_set_def) 
   944 apply (auto dest: Rep_preal [THEN preal_downwards_closed])
   945 done
   946 
   947 lemma diff_set_not_rat_set:
   948   "diff_set (Rep_preal S) (Rep_preal R) < {r. 0 < r}" (is "?lhs < ?rhs")
   949 proof
   950   show "?lhs \<subseteq> ?rhs" by (auto simp add: diff_set_def) 
   951   show "?lhs \<noteq> ?rhs" using diff_set_nonempty by blast
   952 qed
   953 
   954 text{*Part 3 of Dedekind sections definition*}
   955 lemma diff_set_lemma3:
   956      "[|R < S; u \<in> diff_set (Rep_preal S) (Rep_preal R); 0 < z; z < u|] 
   957       ==> z \<in> diff_set (Rep_preal S) (Rep_preal R)"
   958 apply (auto simp add: diff_set_def) 
   959 apply (rule_tac x=x in exI) 
   960 apply (drule Rep_preal [THEN preal_downwards_closed], auto)
   961 done
   962 
   963 text{*Part 4 of Dedekind sections definition*}
   964 lemma diff_set_lemma4:
   965      "[|R < S; y \<in> diff_set (Rep_preal S) (Rep_preal R)|] 
   966       ==> \<exists>u \<in> diff_set (Rep_preal S) (Rep_preal R). y < u"
   967 apply (auto simp add: diff_set_def) 
   968 apply (drule Rep_preal [THEN preal_exists_greater], clarify) 
   969 apply (cut_tac a="x+y" and b=u in add_eq_exists, clarify)  
   970 apply (rule_tac x="y+xa" in exI) 
   971 apply (auto simp add: ac_simps)
   972 done
   973 
   974 lemma mem_diff_set:
   975      "R < S ==> cut (diff_set (Rep_preal S) (Rep_preal R))"
   976 apply (unfold cut_def)
   977 apply (blast intro!: diff_set_not_empty diff_set_not_rat_set
   978                      diff_set_lemma3 diff_set_lemma4)
   979 done
   980 
   981 lemma mem_Rep_preal_diff_iff:
   982       "R < S ==>
   983        (z \<in> Rep_preal(S-R)) = 
   984        (\<exists>x. 0 < x & 0 < z & x \<notin> Rep_preal R & x + z \<in> Rep_preal S)"
   985 apply (simp add: preal_diff_def mem_diff_set Rep_preal)
   986 apply (force simp add: diff_set_def) 
   987 done
   988 
   989 
   990 text{*proving that @{term "R + D \<le> S"}*}
   991 
   992 lemma less_add_left_lemma:
   993   assumes Rless: "R < S"
   994     and a: "a \<in> Rep_preal R"
   995     and cb: "c + b \<in> Rep_preal S"
   996     and "c \<notin> Rep_preal R"
   997     and "0 < b"
   998     and "0 < c"
   999   shows "a + b \<in> Rep_preal S"
  1000 proof -
  1001   have "0<a" by (rule preal_imp_pos [OF Rep_preal a])
  1002   moreover
  1003   have "a < c" using assms by (blast intro: not_in_Rep_preal_ub ) 
  1004   ultimately show ?thesis
  1005     using assms by (simp add: preal_downwards_closed [OF Rep_preal cb])
  1006 qed
  1007 
  1008 lemma less_add_left_le1:
  1009        "R < (S::preal) ==> R + (S-R) \<le> S"
  1010 apply (auto simp add: Bex_def preal_le_def mem_Rep_preal_add_iff 
  1011                       mem_Rep_preal_diff_iff)
  1012 apply (blast intro: less_add_left_lemma) 
  1013 done
  1014 
  1015 subsection{*proving that @{term "S \<le> R + D"} --- trickier*}
  1016 
  1017 lemma lemma_sum_mem_Rep_preal_ex:
  1018      "x \<in> Rep_preal S ==> \<exists>e. 0 < e & x + e \<in> Rep_preal S"
  1019 apply (drule Rep_preal [THEN preal_exists_greater], clarify) 
  1020 apply (cut_tac a=x and b=u in add_eq_exists, auto) 
  1021 done
  1022 
  1023 lemma less_add_left_lemma2:
  1024   assumes Rless: "R < S"
  1025     and x:     "x \<in> Rep_preal S"
  1026     and xnot: "x \<notin>  Rep_preal R"
  1027   shows "\<exists>u v z. 0 < v & 0 < z & u \<in> Rep_preal R & z \<notin> Rep_preal R & 
  1028                      z + v \<in> Rep_preal S & x = u + v"
  1029 proof -
  1030   have xpos: "0<x" by (rule preal_imp_pos [OF Rep_preal x])
  1031   from lemma_sum_mem_Rep_preal_ex [OF x]
  1032   obtain e where epos: "0 < e" and xe: "x + e \<in> Rep_preal S" by blast
  1033   from  Gleason9_34 [OF Rep_preal epos]
  1034   obtain r where r: "r \<in> Rep_preal R" and notin: "r + e \<notin> Rep_preal R" ..
  1035   with x xnot xpos have rless: "r < x" by (blast intro: not_in_Rep_preal_ub)
  1036   from add_eq_exists [of r x]
  1037   obtain y where eq: "x = r+y" by auto
  1038   show ?thesis 
  1039   proof (intro exI conjI)
  1040     show "r \<in> Rep_preal R" by (rule r)
  1041     show "r + e \<notin> Rep_preal R" by (rule notin)
  1042     show "r + e + y \<in> Rep_preal S" using xe eq by (simp add: ac_simps)
  1043     show "x = r + y" by (simp add: eq)
  1044     show "0 < r + e" using epos preal_imp_pos [OF Rep_preal r]
  1045       by simp
  1046     show "0 < y" using rless eq by arith
  1047   qed
  1048 qed
  1049 
  1050 lemma less_add_left_le2: "R < (S::preal) ==> S \<le> R + (S-R)"
  1051 apply (auto simp add: preal_le_def)
  1052 apply (case_tac "x \<in> Rep_preal R")
  1053 apply (cut_tac Rep_preal_self_subset [of R], force)
  1054 apply (auto simp add: Bex_def mem_Rep_preal_add_iff mem_Rep_preal_diff_iff)
  1055 apply (blast dest: less_add_left_lemma2)
  1056 done
  1057 
  1058 lemma less_add_left: "R < (S::preal) ==> R + (S-R) = S"
  1059 by (blast intro: antisym [OF less_add_left_le1 less_add_left_le2])
  1060 
  1061 lemma less_add_left_Ex: "R < (S::preal) ==> \<exists>D. R + D = S"
  1062 by (fast dest: less_add_left)
  1063 
  1064 lemma preal_add_less2_mono1: "R < (S::preal) ==> R + T < S + T"
  1065 apply (auto dest!: less_add_left_Ex simp add: preal_add_assoc)
  1066 apply (rule_tac y1 = D in preal_add_commute [THEN subst])
  1067 apply (auto intro: preal_self_less_add_left simp add: preal_add_assoc [symmetric])
  1068 done
  1069 
  1070 lemma preal_add_less2_mono2: "R < (S::preal) ==> T + R < T + S"
  1071 by (auto intro: preal_add_less2_mono1 simp add: preal_add_commute [of T])
  1072 
  1073 lemma preal_add_right_less_cancel: "R + T < S + T ==> R < (S::preal)"
  1074 apply (insert linorder_less_linear [of R S], auto)
  1075 apply (drule_tac R = S and T = T in preal_add_less2_mono1)
  1076 apply (blast dest: order_less_trans) 
  1077 done
  1078 
  1079 lemma preal_add_left_less_cancel: "T + R < T + S ==> R <  (S::preal)"
  1080 by (auto elim: preal_add_right_less_cancel simp add: preal_add_commute [of T])
  1081 
  1082 lemma preal_add_less_cancel_left: "(T + (R::preal) < T + S) = (R < S)"
  1083 by (blast intro: preal_add_less2_mono2 preal_add_left_less_cancel)
  1084 
  1085 lemma preal_add_le_cancel_left: "(T + (R::preal) \<le> T + S) = (R \<le> S)"
  1086 by (simp add: linorder_not_less [symmetric] preal_add_less_cancel_left) 
  1087 
  1088 lemma preal_add_right_cancel: "(R::preal) + T = S + T ==> R = S"
  1089 apply (insert linorder_less_linear [of R S], safe)
  1090 apply (drule_tac [!] T = T in preal_add_less2_mono1, auto)
  1091 done
  1092 
  1093 lemma preal_add_left_cancel: "C + A = C + B ==> A = (B::preal)"
  1094 by (auto intro: preal_add_right_cancel simp add: preal_add_commute)
  1095 
  1096 instance preal :: linordered_cancel_ab_semigroup_add
  1097 proof
  1098   fix a b c :: preal
  1099   show "a + b = a + c \<Longrightarrow> b = c" by (rule preal_add_left_cancel)
  1100   show "a \<le> b \<Longrightarrow> c + a \<le> c + b" by (simp only: preal_add_le_cancel_left)
  1101 qed
  1102 
  1103 
  1104 subsection{*Completeness of type @{typ preal}*}
  1105 
  1106 text{*Prove that supremum is a cut*}
  1107 
  1108 text{*Part 1 of Dedekind sections definition*}
  1109 
  1110 lemma preal_sup_set_not_empty:
  1111      "P \<noteq> {} ==> {} \<subset> (\<Union>X \<in> P. Rep_preal(X))"
  1112 apply auto
  1113 apply (cut_tac X = x in mem_Rep_preal_Ex, auto)
  1114 done
  1115 
  1116 
  1117 text{*Part 2 of Dedekind sections definition*}
  1118 
  1119 lemma preal_sup_not_exists:
  1120      "\<forall>X \<in> P. X \<le> Y ==> \<exists>q. 0 < q & q \<notin> (\<Union>X \<in> P. Rep_preal(X))"
  1121 apply (cut_tac X = Y in Rep_preal_exists_bound)
  1122 apply (auto simp add: preal_le_def)
  1123 done
  1124 
  1125 lemma preal_sup_set_not_rat_set:
  1126      "\<forall>X \<in> P. X \<le> Y ==> (\<Union>X \<in> P. Rep_preal(X)) < {r. 0 < r}"
  1127 apply (drule preal_sup_not_exists)
  1128 apply (blast intro: preal_imp_pos [OF Rep_preal])  
  1129 done
  1130 
  1131 text{*Part 3 of Dedekind sections definition*}
  1132 lemma preal_sup_set_lemma3:
  1133      "[|P \<noteq> {}; \<forall>X \<in> P. X \<le> Y; u \<in> (\<Union>X \<in> P. Rep_preal(X)); 0 < z; z < u|]
  1134       ==> z \<in> (\<Union>X \<in> P. Rep_preal(X))"
  1135 by (auto elim: Rep_preal [THEN preal_downwards_closed])
  1136 
  1137 text{*Part 4 of Dedekind sections definition*}
  1138 lemma preal_sup_set_lemma4:
  1139      "[|P \<noteq> {}; \<forall>X \<in> P. X \<le> Y; y \<in> (\<Union>X \<in> P. Rep_preal(X)) |]
  1140           ==> \<exists>u \<in> (\<Union>X \<in> P. Rep_preal(X)). y < u"
  1141 by (blast dest: Rep_preal [THEN preal_exists_greater])
  1142 
  1143 lemma preal_sup:
  1144      "[|P \<noteq> {}; \<forall>X \<in> P. X \<le> Y|] ==> cut (\<Union>X \<in> P. Rep_preal(X))"
  1145 apply (unfold cut_def)
  1146 apply (blast intro!: preal_sup_set_not_empty preal_sup_set_not_rat_set
  1147                      preal_sup_set_lemma3 preal_sup_set_lemma4)
  1148 done
  1149 
  1150 lemma preal_psup_le:
  1151      "[| \<forall>X \<in> P. X \<le> Y;  x \<in> P |] ==> x \<le> psup P"
  1152 apply (simp (no_asm_simp) add: preal_le_def) 
  1153 apply (subgoal_tac "P \<noteq> {}") 
  1154 apply (auto simp add: psup_def preal_sup) 
  1155 done
  1156 
  1157 lemma psup_le_ub: "[| P \<noteq> {}; \<forall>X \<in> P. X \<le> Y |] ==> psup P \<le> Y"
  1158 apply (simp (no_asm_simp) add: preal_le_def)
  1159 apply (simp add: psup_def preal_sup) 
  1160 apply (auto simp add: preal_le_def)
  1161 done
  1162 
  1163 text{*Supremum property*}
  1164 lemma preal_complete:
  1165      "[| P \<noteq> {}; \<forall>X \<in> P. X \<le> Y |] ==> (\<exists>X \<in> P. Z < X) = (Z < psup P)"
  1166 apply (simp add: preal_less_def psup_def preal_sup)
  1167 apply (auto simp add: preal_le_def)
  1168 apply (rename_tac U) 
  1169 apply (cut_tac x = U and y = Z in linorder_less_linear)
  1170 apply (auto simp add: preal_less_def)
  1171 done
  1172 
  1173 section {*Defining the Reals from the Positive Reals*}
  1174 
  1175 definition
  1176   realrel   ::  "((preal * preal) * (preal * preal)) set" where
  1177   "realrel = {p. \<exists>x1 y1 x2 y2. p = ((x1,y1),(x2,y2)) & x1+y2 = x2+y1}"
  1178 
  1179 definition "Real = UNIV//realrel"
  1180 
  1181 typedef real = Real
  1182   morphisms Rep_Real Abs_Real
  1183   unfolding Real_def by (auto simp add: quotient_def)
  1184 
  1185 definition
  1186   (** these don't use the overloaded "real" function: users don't see them **)
  1187   real_of_preal :: "preal => real" where
  1188   "real_of_preal m = Abs_Real (realrel `` {(m + 1, 1)})"
  1189 
  1190 instantiation real :: "{zero, one, plus, minus, uminus, times, inverse, ord, abs, sgn}"
  1191 begin
  1192 
  1193 definition
  1194   real_zero_def: "0 = Abs_Real(realrel``{(1, 1)})"
  1195 
  1196 definition
  1197   real_one_def: "1 = Abs_Real(realrel``{(1 + 1, 1)})"
  1198 
  1199 definition
  1200   real_add_def: "z + w =
  1201        the_elem (\<Union>(x,y) \<in> Rep_Real(z). \<Union>(u,v) \<in> Rep_Real(w).
  1202                  { Abs_Real(realrel``{(x+u, y+v)}) })"
  1203 
  1204 definition
  1205   real_minus_def: "- r =  the_elem (\<Union>(x,y) \<in> Rep_Real(r). { Abs_Real(realrel``{(y,x)}) })"
  1206 
  1207 definition
  1208   real_diff_def: "r - (s::real) = r + - s"
  1209 
  1210 definition
  1211   real_mult_def:
  1212     "z * w =
  1213        the_elem (\<Union>(x,y) \<in> Rep_Real(z). \<Union>(u,v) \<in> Rep_Real(w).
  1214                  { Abs_Real(realrel``{(x*u + y*v, x*v + y*u)}) })"
  1215 
  1216 definition
  1217   real_inverse_def: "inverse (R::real) = (THE S. (R = 0 & S = 0) | S * R = 1)"
  1218 
  1219 definition
  1220   real_divide_def: "R / (S::real) = R * inverse S"
  1221 
  1222 definition
  1223   real_le_def: "z \<le> (w::real) \<longleftrightarrow>
  1224     (\<exists>x y u v. x+v \<le> u+y & (x,y) \<in> Rep_Real z & (u,v) \<in> Rep_Real w)"
  1225 
  1226 definition
  1227   real_less_def: "x < (y\<Colon>real) \<longleftrightarrow> x \<le> y \<and> x \<noteq> y"
  1228 
  1229 definition
  1230   real_abs_def:  "abs (r::real) = (if r < 0 then - r else r)"
  1231 
  1232 definition
  1233   real_sgn_def: "sgn (x::real) = (if x=0 then 0 else if 0<x then 1 else - 1)"
  1234 
  1235 instance ..
  1236 
  1237 end
  1238 
  1239 subsection {* Equivalence relation over positive reals *}
  1240 
  1241 lemma preal_trans_lemma:
  1242   assumes "x + y1 = x1 + y"
  1243     and "x + y2 = x2 + y"
  1244   shows "x1 + y2 = x2 + (y1::preal)"
  1245 proof -
  1246   have "(x1 + y2) + x = (x + y2) + x1" by (simp add: ac_simps)
  1247   also have "... = (x2 + y) + x1"  by (simp add: assms)
  1248   also have "... = x2 + (x1 + y)"  by (simp add: ac_simps)
  1249   also have "... = x2 + (x + y1)"  by (simp add: assms)
  1250   also have "... = (x2 + y1) + x"  by (simp add: ac_simps)
  1251   finally have "(x1 + y2) + x = (x2 + y1) + x" .
  1252   thus ?thesis by (rule add_right_imp_eq)
  1253 qed
  1254 
  1255 
  1256 lemma realrel_iff [simp]: "(((x1,y1),(x2,y2)) \<in> realrel) = (x1 + y2 = x2 + y1)"
  1257 by (simp add: realrel_def)
  1258 
  1259 lemma equiv_realrel: "equiv UNIV realrel"
  1260 apply (auto simp add: equiv_def refl_on_def sym_def trans_def realrel_def)
  1261 apply (blast dest: preal_trans_lemma) 
  1262 done
  1263 
  1264 text{*Reduces equality of equivalence classes to the @{term realrel} relation:
  1265   @{term "(realrel `` {x} = realrel `` {y}) = ((x,y) \<in> realrel)"} *}
  1266 lemmas equiv_realrel_iff = 
  1267        eq_equiv_class_iff [OF equiv_realrel UNIV_I UNIV_I]
  1268 
  1269 declare equiv_realrel_iff [simp]
  1270 
  1271 
  1272 lemma realrel_in_real [simp]: "realrel``{(x,y)}: Real"
  1273 by (simp add: Real_def realrel_def quotient_def, blast)
  1274 
  1275 declare Abs_Real_inject [simp]
  1276 declare Abs_Real_inverse [simp]
  1277 
  1278 
  1279 text{*Case analysis on the representation of a real number as an equivalence
  1280       class of pairs of positive reals.*}
  1281 lemma eq_Abs_Real [case_names Abs_Real, cases type: real]: 
  1282      "(!!x y. z = Abs_Real(realrel``{(x,y)}) ==> P) ==> P"
  1283 apply (rule Rep_Real [of z, unfolded Real_def, THEN quotientE])
  1284 apply (drule arg_cong [where f=Abs_Real])
  1285 apply (auto simp add: Rep_Real_inverse)
  1286 done
  1287 
  1288 
  1289 subsection {* Addition and Subtraction *}
  1290 
  1291 lemma real_add_congruent2_lemma:
  1292      "[|a + ba = aa + b; ab + bc = ac + bb|]
  1293       ==> a + ab + (ba + bc) = aa + ac + (b + (bb::preal))"
  1294 apply (simp add: add.assoc)
  1295 apply (rule add.left_commute [of ab, THEN ssubst])
  1296 apply (simp add: add.assoc [symmetric])
  1297 apply (simp add: ac_simps)
  1298 done
  1299 
  1300 lemma real_add:
  1301      "Abs_Real (realrel``{(x,y)}) + Abs_Real (realrel``{(u,v)}) =
  1302       Abs_Real (realrel``{(x+u, y+v)})"
  1303 proof -
  1304   have "(\<lambda>z w. (\<lambda>(x,y). (\<lambda>(u,v). {Abs_Real (realrel `` {(x+u, y+v)})}) w) z)
  1305         respects2 realrel"
  1306     by (auto simp add: congruent2_def, blast intro: real_add_congruent2_lemma) 
  1307   thus ?thesis
  1308     by (simp add: real_add_def UN_UN_split_split_eq
  1309                   UN_equiv_class2 [OF equiv_realrel equiv_realrel])
  1310 qed
  1311 
  1312 lemma real_minus: "- Abs_Real(realrel``{(x,y)}) = Abs_Real(realrel `` {(y,x)})"
  1313 proof -
  1314   have "(\<lambda>(x,y). {Abs_Real (realrel``{(y,x)})}) respects realrel"
  1315     by (auto simp add: congruent_def add.commute) 
  1316   thus ?thesis
  1317     by (simp add: real_minus_def UN_equiv_class [OF equiv_realrel])
  1318 qed
  1319 
  1320 instance real :: ab_group_add
  1321 proof
  1322   fix x y z :: real
  1323   show "(x + y) + z = x + (y + z)"
  1324     by (cases x, cases y, cases z, simp add: real_add add.assoc)
  1325   show "x + y = y + x"
  1326     by (cases x, cases y, simp add: real_add add.commute)
  1327   show "0 + x = x"
  1328     by (cases x, simp add: real_add real_zero_def ac_simps)
  1329   show "- x + x = 0"
  1330     by (cases x, simp add: real_minus real_add real_zero_def add.commute)
  1331   show "x - y = x + - y"
  1332     by (simp add: real_diff_def)
  1333 qed
  1334 
  1335 
  1336 subsection {* Multiplication *}
  1337 
  1338 lemma real_mult_congruent2_lemma:
  1339      "!!(x1::preal). [| x1 + y2 = x2 + y1 |] ==>
  1340           x * x1 + y * y1 + (x * y2 + y * x2) =
  1341           x * x2 + y * y2 + (x * y1 + y * x1)"
  1342 apply (simp add: add.left_commute add.assoc [symmetric])
  1343 apply (simp add: add.assoc distrib_left [symmetric])
  1344 apply (simp add: add.commute)
  1345 done
  1346 
  1347 lemma real_mult_congruent2:
  1348     "(%p1 p2.
  1349         (%(x1,y1). (%(x2,y2). 
  1350           { Abs_Real (realrel``{(x1*x2 + y1*y2, x1*y2+y1*x2)}) }) p2) p1)
  1351      respects2 realrel"
  1352 apply (rule congruent2_commuteI [OF equiv_realrel], clarify)
  1353 apply (simp add: mult.commute add.commute)
  1354 apply (auto simp add: real_mult_congruent2_lemma)
  1355 done
  1356 
  1357 lemma real_mult:
  1358       "Abs_Real((realrel``{(x1,y1)})) * Abs_Real((realrel``{(x2,y2)})) =
  1359        Abs_Real(realrel `` {(x1*x2+y1*y2,x1*y2+y1*x2)})"
  1360 by (simp add: real_mult_def UN_UN_split_split_eq
  1361          UN_equiv_class2 [OF equiv_realrel equiv_realrel real_mult_congruent2])
  1362 
  1363 lemma real_mult_commute: "(z::real) * w = w * z"
  1364 by (cases z, cases w, simp add: real_mult ac_simps ac_simps)
  1365 
  1366 lemma real_mult_assoc: "((z1::real) * z2) * z3 = z1 * (z2 * z3)"
  1367 apply (cases z1, cases z2, cases z3)
  1368 apply (simp add: real_mult algebra_simps)
  1369 done
  1370 
  1371 lemma real_mult_1: "(1::real) * z = z"
  1372 apply (cases z)
  1373 apply (simp add: real_mult real_one_def algebra_simps)
  1374 done
  1375 
  1376 lemma real_add_mult_distrib: "((z1::real) + z2) * w = (z1 * w) + (z2 * w)"
  1377 apply (cases z1, cases z2, cases w)
  1378 apply (simp add: real_add real_mult algebra_simps)
  1379 done
  1380 
  1381 text{*one and zero are distinct*}
  1382 lemma real_zero_not_eq_one: "0 \<noteq> (1::real)"
  1383 proof -
  1384   have "(1::preal) < 1 + 1"
  1385     by (simp add: preal_self_less_add_left)
  1386   thus ?thesis
  1387     by (simp add: real_zero_def real_one_def)
  1388 qed
  1389 
  1390 instance real :: comm_ring_1
  1391 proof
  1392   fix x y z :: real
  1393   show "(x * y) * z = x * (y * z)" by (rule real_mult_assoc)
  1394   show "x * y = y * x" by (rule real_mult_commute)
  1395   show "1 * x = x" by (rule real_mult_1)
  1396   show "(x + y) * z = x * z + y * z" by (rule real_add_mult_distrib)
  1397   show "0 \<noteq> (1::real)" by (rule real_zero_not_eq_one)
  1398 qed
  1399 
  1400 subsection {* Inverse and Division *}
  1401 
  1402 lemma real_zero_iff: "Abs_Real (realrel `` {(x, x)}) = 0"
  1403 by (simp add: real_zero_def add.commute)
  1404 
  1405 text{*Instead of using an existential quantifier and constructing the inverse
  1406 within the proof, we could define the inverse explicitly.*}
  1407 
  1408 lemma real_mult_inverse_left_ex: "x \<noteq> 0 ==> \<exists>y. y*x = (1::real)"
  1409 apply (simp add: real_zero_def real_one_def, cases x)
  1410 apply (cut_tac x = xa and y = y in linorder_less_linear)
  1411 apply (auto dest!: less_add_left_Ex simp add: real_zero_iff)
  1412 apply (rule_tac
  1413         x = "Abs_Real (realrel``{(1, inverse (D) + 1)})"
  1414        in exI)
  1415 apply (rule_tac [2]
  1416         x = "Abs_Real (realrel``{(inverse (D) + 1, 1)})" 
  1417        in exI)
  1418 apply (auto simp add: real_mult preal_mult_inverse_right algebra_simps)
  1419 done
  1420 
  1421 lemma real_mult_inverse_left: "x \<noteq> 0 ==> inverse(x)*x = (1::real)"
  1422 apply (simp add: real_inverse_def)
  1423 apply (drule real_mult_inverse_left_ex, safe)
  1424 apply (rule theI, assumption, rename_tac z)
  1425 apply (subgoal_tac "(z * x) * y = z * (x * y)")
  1426 apply (simp add: mult.commute)
  1427 apply (rule mult.assoc)
  1428 done
  1429 
  1430 
  1431 subsection{*The Real Numbers form a Field*}
  1432 
  1433 instance real :: field_inverse_zero
  1434 proof
  1435   fix x y z :: real
  1436   show "x \<noteq> 0 ==> inverse x * x = 1" by (rule real_mult_inverse_left)
  1437   show "x / y = x * inverse y" by (simp add: real_divide_def)
  1438   show "inverse 0 = (0::real)" by (simp add: real_inverse_def)
  1439 qed
  1440 
  1441 
  1442 subsection{*The @{text "\<le>"} Ordering*}
  1443 
  1444 lemma real_le_refl: "w \<le> (w::real)"
  1445 by (cases w, force simp add: real_le_def)
  1446 
  1447 text{*The arithmetic decision procedure is not set up for type preal.
  1448   This lemma is currently unused, but it could simplify the proofs of the
  1449   following two lemmas.*}
  1450 lemma preal_eq_le_imp_le:
  1451   assumes eq: "a+b = c+d" and le: "c \<le> a"
  1452   shows "b \<le> (d::preal)"
  1453 proof -
  1454   have "c+d \<le> a+d" by (simp add: le)
  1455   hence "a+b \<le> a+d" by (simp add: eq)
  1456   thus "b \<le> d" by simp
  1457 qed
  1458 
  1459 lemma real_le_lemma:
  1460   assumes l: "u1 + v2 \<le> u2 + v1"
  1461     and "x1 + v1 = u1 + y1"
  1462     and "x2 + v2 = u2 + y2"
  1463   shows "x1 + y2 \<le> x2 + (y1::preal)"
  1464 proof -
  1465   have "(x1+v1) + (u2+y2) = (u1+y1) + (x2+v2)" by (simp add: assms)
  1466   hence "(x1+y2) + (u2+v1) = (x2+y1) + (u1+v2)" by (simp add: ac_simps)
  1467   also have "... \<le> (x2+y1) + (u2+v1)" by (simp add: assms)
  1468   finally show ?thesis by simp
  1469 qed
  1470 
  1471 lemma real_le: 
  1472      "(Abs_Real(realrel``{(x1,y1)}) \<le> Abs_Real(realrel``{(x2,y2)})) =  
  1473       (x1 + y2 \<le> x2 + y1)"
  1474 apply (simp add: real_le_def)
  1475 apply (auto intro: real_le_lemma)
  1476 done
  1477 
  1478 lemma real_le_antisym: "[| z \<le> w; w \<le> z |] ==> z = (w::real)"
  1479 by (cases z, cases w, simp add: real_le)
  1480 
  1481 lemma real_trans_lemma:
  1482   assumes "x + v \<le> u + y"
  1483     and "u + v' \<le> u' + v"
  1484     and "x2 + v2 = u2 + y2"
  1485   shows "x + v' \<le> u' + (y::preal)"
  1486 proof -
  1487   have "(x+v') + (u+v) = (x+v) + (u+v')" by (simp add: ac_simps)
  1488   also have "... \<le> (u+y) + (u+v')" by (simp add: assms)
  1489   also have "... \<le> (u+y) + (u'+v)" by (simp add: assms)
  1490   also have "... = (u'+y) + (u+v)"  by (simp add: ac_simps)
  1491   finally show ?thesis by simp
  1492 qed
  1493 
  1494 lemma real_le_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::real)"
  1495 apply (cases i, cases j, cases k)
  1496 apply (simp add: real_le)
  1497 apply (blast intro: real_trans_lemma)
  1498 done
  1499 
  1500 instance real :: order
  1501 proof
  1502   fix u v :: real
  1503   show "u < v \<longleftrightarrow> u \<le> v \<and> \<not> v \<le> u" 
  1504     by (auto simp add: real_less_def intro: real_le_antisym)
  1505 qed (assumption | rule real_le_refl real_le_trans real_le_antisym)+
  1506 
  1507 (* Axiom 'linorder_linear' of class 'linorder': *)
  1508 lemma real_le_linear: "(z::real) \<le> w | w \<le> z"
  1509 apply (cases z, cases w)
  1510 apply (auto simp add: real_le real_zero_def ac_simps)
  1511 done
  1512 
  1513 instance real :: linorder
  1514   by (intro_classes, rule real_le_linear)
  1515 
  1516 lemma real_le_eq_diff: "(x \<le> y) = (x-y \<le> (0::real))"
  1517 apply (cases x, cases y) 
  1518 apply (auto simp add: real_le real_zero_def real_diff_def real_add real_minus
  1519                       ac_simps)
  1520 apply (simp_all add: add.assoc [symmetric])
  1521 done
  1522 
  1523 lemma real_add_left_mono: 
  1524   assumes le: "x \<le> y" shows "z + x \<le> z + (y::real)"
  1525 proof -
  1526   have "z + x - (z + y) = (z + -z) + (x - y)" 
  1527     by (simp add: algebra_simps) 
  1528   with le show ?thesis 
  1529     by (simp add: real_le_eq_diff[of x] real_le_eq_diff[of "z+x"])
  1530 qed
  1531 
  1532 lemma real_sum_gt_zero_less: "(0 < S + (-W::real)) ==> (W < S)"
  1533 by (simp add: linorder_not_le [symmetric] real_le_eq_diff [of S])
  1534 
  1535 lemma real_less_sum_gt_zero: "(W < S) ==> (0 < S + (-W::real))"
  1536 by (simp add: linorder_not_le [symmetric] real_le_eq_diff [of S])
  1537 
  1538 lemma real_mult_order: "[| 0 < x; 0 < y |] ==> (0::real) < x * y"
  1539 apply (cases x, cases y)
  1540 apply (simp add: linorder_not_le [where 'a = real, symmetric] 
  1541                  linorder_not_le [where 'a = preal] 
  1542                   real_zero_def real_le real_mult)
  1543   --{*Reduce to the (simpler) @{text "\<le>"} relation *}
  1544 apply (auto dest!: less_add_left_Ex
  1545      simp add: algebra_simps preal_self_less_add_left)
  1546 done
  1547 
  1548 lemma real_mult_less_mono2: "[| (0::real) < z; x < y |] ==> z * x < z * y"
  1549 apply (rule real_sum_gt_zero_less)
  1550 apply (drule real_less_sum_gt_zero [of x y])
  1551 apply (drule real_mult_order, assumption)
  1552 apply (simp add: algebra_simps)
  1553 done
  1554 
  1555 instantiation real :: distrib_lattice
  1556 begin
  1557 
  1558 definition
  1559   "(inf \<Colon> real \<Rightarrow> real \<Rightarrow> real) = min"
  1560 
  1561 definition
  1562   "(sup \<Colon> real \<Rightarrow> real \<Rightarrow> real) = max"
  1563 
  1564 instance
  1565   by default (auto simp add: inf_real_def sup_real_def max_min_distrib2)
  1566 
  1567 end
  1568 
  1569 
  1570 subsection{*The Reals Form an Ordered Field*}
  1571 
  1572 instance real :: linordered_field_inverse_zero
  1573 proof
  1574   fix x y z :: real
  1575   show "x \<le> y ==> z + x \<le> z + y" by (rule real_add_left_mono)
  1576   show "x < y ==> 0 < z ==> z * x < z * y" by (rule real_mult_less_mono2)
  1577   show "\<bar>x\<bar> = (if x < 0 then -x else x)" by (simp only: real_abs_def)
  1578   show "sgn x = (if x=0 then 0 else if 0<x then 1 else - 1)"
  1579     by (simp only: real_sgn_def)
  1580 qed
  1581 
  1582 text{*The function @{term real_of_preal} requires many proofs, but it seems
  1583 to be essential for proving completeness of the reals from that of the
  1584 positive reals.*}
  1585 
  1586 lemma real_of_preal_add:
  1587      "real_of_preal ((x::preal) + y) = real_of_preal x + real_of_preal y"
  1588 by (simp add: real_of_preal_def real_add algebra_simps)
  1589 
  1590 lemma real_of_preal_mult:
  1591      "real_of_preal ((x::preal) * y) = real_of_preal x* real_of_preal y"
  1592 by (simp add: real_of_preal_def real_mult algebra_simps)
  1593 
  1594 
  1595 text{*Gleason prop 9-4.4 p 127*}
  1596 lemma real_of_preal_trichotomy:
  1597       "\<exists>m. (x::real) = real_of_preal m | x = 0 | x = -(real_of_preal m)"
  1598 apply (simp add: real_of_preal_def real_zero_def, cases x)
  1599 apply (auto simp add: real_minus ac_simps)
  1600 apply (cut_tac x = xa and y = y in linorder_less_linear)
  1601 apply (auto dest!: less_add_left_Ex simp add: add.assoc [symmetric])
  1602 done
  1603 
  1604 lemma real_of_preal_leD:
  1605       "real_of_preal m1 \<le> real_of_preal m2 ==> m1 \<le> m2"
  1606 by (simp add: real_of_preal_def real_le)
  1607 
  1608 lemma real_of_preal_lessI: "m1 < m2 ==> real_of_preal m1 < real_of_preal m2"
  1609 by (auto simp add: real_of_preal_leD linorder_not_le [symmetric])
  1610 
  1611 lemma real_of_preal_lessD:
  1612       "real_of_preal m1 < real_of_preal m2 ==> m1 < m2"
  1613 by (simp add: real_of_preal_def real_le linorder_not_le [symmetric])
  1614 
  1615 lemma real_of_preal_less_iff [simp]:
  1616      "(real_of_preal m1 < real_of_preal m2) = (m1 < m2)"
  1617 by (blast intro: real_of_preal_lessI real_of_preal_lessD)
  1618 
  1619 lemma real_of_preal_le_iff:
  1620      "(real_of_preal m1 \<le> real_of_preal m2) = (m1 \<le> m2)"
  1621 by (simp add: linorder_not_less [symmetric])
  1622 
  1623 lemma real_of_preal_zero_less: "0 < real_of_preal m"
  1624 apply (insert preal_self_less_add_left [of 1 m])
  1625 apply (auto simp add: real_zero_def real_of_preal_def
  1626                       real_less_def real_le_def ac_simps)
  1627 apply (rule_tac x="m + 1" in exI, rule_tac x="1" in exI)
  1628 apply (simp add: ac_simps)
  1629 done
  1630 
  1631 lemma real_of_preal_minus_less_zero: "- real_of_preal m < 0"
  1632 by (simp add: real_of_preal_zero_less)
  1633 
  1634 lemma real_of_preal_not_minus_gt_zero: "~ 0 < - real_of_preal m"
  1635 proof -
  1636   from real_of_preal_minus_less_zero
  1637   show ?thesis by (blast dest: order_less_trans)
  1638 qed
  1639 
  1640 
  1641 subsection{*Theorems About the Ordering*}
  1642 
  1643 lemma real_gt_zero_preal_Ex: "(0 < x) = (\<exists>y. x = real_of_preal y)"
  1644 apply (auto simp add: real_of_preal_zero_less)
  1645 apply (cut_tac x = x in real_of_preal_trichotomy)
  1646 apply (blast elim!: real_of_preal_not_minus_gt_zero [THEN notE])
  1647 done
  1648 
  1649 lemma real_gt_preal_preal_Ex:
  1650      "real_of_preal z < x ==> \<exists>y. x = real_of_preal y"
  1651 by (blast dest!: real_of_preal_zero_less [THEN order_less_trans]
  1652              intro: real_gt_zero_preal_Ex [THEN iffD1])
  1653 
  1654 lemma real_ge_preal_preal_Ex:
  1655      "real_of_preal z \<le> x ==> \<exists>y. x = real_of_preal y"
  1656 by (blast dest: order_le_imp_less_or_eq real_gt_preal_preal_Ex)
  1657 
  1658 lemma real_less_all_preal: "y \<le> 0 ==> \<forall>x. y < real_of_preal x"
  1659 by (auto elim: order_le_imp_less_or_eq [THEN disjE] 
  1660             intro: real_of_preal_zero_less [THEN [2] order_less_trans] 
  1661             simp add: real_of_preal_zero_less)
  1662 
  1663 lemma real_less_all_real2: "~ 0 < y ==> \<forall>x. y < real_of_preal x"
  1664 by (blast intro!: real_less_all_preal linorder_not_less [THEN iffD1])
  1665 
  1666 subsection {* Completeness of Positive Reals *}
  1667 
  1668 text {*
  1669   Supremum property for the set of positive reals
  1670 
  1671   Let @{text "P"} be a non-empty set of positive reals, with an upper
  1672   bound @{text "y"}.  Then @{text "P"} has a least upper bound
  1673   (written @{text "S"}).
  1674 
  1675   FIXME: Can the premise be weakened to @{text "\<forall>x \<in> P. x\<le> y"}?
  1676 *}
  1677 
  1678 lemma posreal_complete:
  1679   assumes positive_P: "\<forall>x \<in> P. (0::real) < x"
  1680     and not_empty_P: "\<exists>x. x \<in> P"
  1681     and upper_bound_Ex: "\<exists>y. \<forall>x \<in> P. x<y"
  1682   shows "\<exists>S. \<forall>y. (\<exists>x \<in> P. y < x) = (y < S)"
  1683 proof (rule exI, rule allI)
  1684   fix y
  1685   let ?pP = "{w. real_of_preal w \<in> P}"
  1686 
  1687   show "(\<exists>x\<in>P. y < x) = (y < real_of_preal (psup ?pP))"
  1688   proof (cases "0 < y")
  1689     assume neg_y: "\<not> 0 < y"
  1690     show ?thesis
  1691     proof
  1692       assume "\<exists>x\<in>P. y < x"
  1693       have "\<forall>x. y < real_of_preal x"
  1694         using neg_y by (rule real_less_all_real2)
  1695       thus "y < real_of_preal (psup ?pP)" ..
  1696     next
  1697       assume "y < real_of_preal (psup ?pP)"
  1698       obtain "x" where x_in_P: "x \<in> P" using not_empty_P ..
  1699       hence "0 < x" using positive_P by simp
  1700       hence "y < x" using neg_y by simp
  1701       thus "\<exists>x \<in> P. y < x" using x_in_P ..
  1702     qed
  1703   next
  1704     assume pos_y: "0 < y"
  1705 
  1706     then obtain py where y_is_py: "y = real_of_preal py"
  1707       by (auto simp add: real_gt_zero_preal_Ex)
  1708 
  1709     obtain a where "a \<in> P" using not_empty_P ..
  1710     with positive_P have a_pos: "0 < a" ..
  1711     then obtain pa where "a = real_of_preal pa"
  1712       by (auto simp add: real_gt_zero_preal_Ex)
  1713     hence "pa \<in> ?pP" using `a \<in> P` by auto
  1714     hence pP_not_empty: "?pP \<noteq> {}" by auto
  1715 
  1716     obtain sup where sup: "\<forall>x \<in> P. x < sup"
  1717       using upper_bound_Ex ..
  1718     from this and `a \<in> P` have "a < sup" ..
  1719     hence "0 < sup" using a_pos by arith
  1720     then obtain possup where "sup = real_of_preal possup"
  1721       by (auto simp add: real_gt_zero_preal_Ex)
  1722     hence "\<forall>X \<in> ?pP. X \<le> possup"
  1723       using sup by (auto simp add: real_of_preal_lessI)
  1724     with pP_not_empty have psup: "\<And>Z. (\<exists>X \<in> ?pP. Z < X) = (Z < psup ?pP)"
  1725       by (rule preal_complete)
  1726 
  1727     show ?thesis
  1728     proof
  1729       assume "\<exists>x \<in> P. y < x"
  1730       then obtain x where x_in_P: "x \<in> P" and y_less_x: "y < x" ..
  1731       hence "0 < x" using pos_y by arith
  1732       then obtain px where x_is_px: "x = real_of_preal px"
  1733         by (auto simp add: real_gt_zero_preal_Ex)
  1734 
  1735       have py_less_X: "\<exists>X \<in> ?pP. py < X"
  1736       proof
  1737         show "py < px" using y_is_py and x_is_px and y_less_x
  1738           by (simp add: real_of_preal_lessI)
  1739         show "px \<in> ?pP" using x_in_P and x_is_px by simp
  1740       qed
  1741 
  1742       have "(\<exists>X \<in> ?pP. py < X) ==> (py < psup ?pP)"
  1743         using psup by simp
  1744       hence "py < psup ?pP" using py_less_X by simp
  1745       thus "y < real_of_preal (psup {w. real_of_preal w \<in> P})"
  1746         using y_is_py and pos_y by (simp add: real_of_preal_lessI)
  1747     next
  1748       assume y_less_psup: "y < real_of_preal (psup ?pP)"
  1749 
  1750       hence "py < psup ?pP" using y_is_py
  1751         by (simp add: real_of_preal_lessI)
  1752       then obtain "X" where py_less_X: "py < X" and X_in_pP: "X \<in> ?pP"
  1753         using psup by auto
  1754       then obtain x where x_is_X: "x = real_of_preal X"
  1755         by (simp add: real_gt_zero_preal_Ex)
  1756       hence "y < x" using py_less_X and y_is_py
  1757         by (simp add: real_of_preal_lessI)
  1758 
  1759       moreover have "x \<in> P" using x_is_X and X_in_pP by simp
  1760 
  1761       ultimately show "\<exists> x \<in> P. y < x" ..
  1762     qed
  1763   qed
  1764 qed
  1765 
  1766 text {*
  1767   \medskip Completeness
  1768 *}
  1769 
  1770 lemma reals_complete:
  1771   fixes S :: "real set"
  1772   assumes notempty_S: "\<exists>X. X \<in> S"
  1773     and exists_Ub: "bdd_above S"
  1774   shows "\<exists>x. (\<forall>s\<in>S. s \<le> x) \<and> (\<forall>y. (\<forall>s\<in>S. s \<le> y) \<longrightarrow> x \<le> y)"
  1775 proof -
  1776   obtain X where X_in_S: "X \<in> S" using notempty_S ..
  1777   obtain Y where Y_isUb: "\<forall>s\<in>S. s \<le> Y"
  1778     using exists_Ub by (auto simp: bdd_above_def)
  1779   let ?SHIFT = "{z. \<exists>x \<in>S. z = x + (-X) + 1} \<inter> {x. 0 < x}"
  1780 
  1781   {
  1782     fix x
  1783     assume S_le_x: "\<forall>s\<in>S. s \<le> x"
  1784     {
  1785       fix s
  1786       assume "s \<in> {z. \<exists>x\<in>S. z = x + - X + 1}"
  1787       hence "\<exists> x \<in> S. s = x + -X + 1" ..
  1788       then obtain x1 where x1: "x1 \<in> S" "s = x1 + (-X) + 1" ..
  1789       then have "x1 \<le> x" using S_le_x by simp
  1790       with x1 have "s \<le> x + - X + 1" by arith
  1791     }
  1792     then have "\<forall>s\<in>?SHIFT. s \<le> x + (-X) + 1"
  1793       by auto
  1794   } note S_Ub_is_SHIFT_Ub = this
  1795 
  1796   have *: "\<forall>s\<in>?SHIFT. s \<le> Y + (-X) + 1" using Y_isUb by (rule S_Ub_is_SHIFT_Ub)
  1797   have "\<forall>s\<in>?SHIFT. s < Y + (-X) + 2"
  1798   proof
  1799     fix s assume "s\<in>?SHIFT"
  1800     with * have "s \<le> Y + (-X) + 1" by simp
  1801     also have "\<dots> < Y + (-X) + 2" by simp
  1802     finally show "s < Y + (-X) + 2" .
  1803   qed
  1804   moreover have "\<forall>y \<in> ?SHIFT. 0 < y" by auto
  1805   moreover have shifted_not_empty: "\<exists>u. u \<in> ?SHIFT"
  1806     using X_in_S and Y_isUb by auto
  1807   ultimately obtain t where t_is_Lub: "\<forall>y. (\<exists>x\<in>?SHIFT. y < x) = (y < t)"
  1808     using posreal_complete [of ?SHIFT] unfolding bdd_above_def by blast
  1809 
  1810   show ?thesis
  1811   proof
  1812     show "(\<forall>s\<in>S. s \<le> (t + X + (-1))) \<and> (\<forall>y. (\<forall>s\<in>S. s \<le> y) \<longrightarrow> (t + X + (-1)) \<le> y)"
  1813     proof safe
  1814       fix x
  1815       assume "\<forall>s\<in>S. s \<le> x"
  1816       hence "\<forall>s\<in>?SHIFT. s \<le> x + (-X) + 1"
  1817         using S_Ub_is_SHIFT_Ub by simp
  1818       then have "\<not> x + (-X) + 1 < t"
  1819         by (subst t_is_Lub[rule_format, symmetric]) (simp add: not_less)
  1820       thus "t + X + -1 \<le> x" by arith
  1821     next
  1822       fix y
  1823       assume y_in_S: "y \<in> S"
  1824       obtain "u" where u_in_shift: "u \<in> ?SHIFT" using shifted_not_empty ..
  1825       hence "\<exists> x \<in> S. u = x + - X + 1" by simp
  1826       then obtain "x" where x_and_u: "u = x + - X + 1" ..
  1827       have u_le_t: "u \<le> t"
  1828       proof (rule dense_le)
  1829         fix x assume "x < u" then have "x < t"
  1830           using u_in_shift t_is_Lub by auto
  1831         then show "x \<le> t"  by simp
  1832       qed
  1833 
  1834       show "y \<le> t + X + -1"
  1835       proof cases
  1836         assume "y \<le> x"
  1837         moreover have "x = u + X + - 1" using x_and_u by arith
  1838         moreover have "u + X + - 1  \<le> t + X + -1" using u_le_t by arith
  1839         ultimately show "y  \<le> t + X + -1" by arith
  1840       next
  1841         assume "~(y \<le> x)"
  1842         hence x_less_y: "x < y" by arith
  1843 
  1844         have "x + (-X) + 1 \<in> ?SHIFT" using x_and_u and u_in_shift by simp
  1845         hence "0 < x + (-X) + 1" by simp
  1846         hence "0 < y + (-X) + 1" using x_less_y by arith
  1847         hence *: "y + (-X) + 1 \<in> ?SHIFT" using y_in_S by simp
  1848         have "y + (-X) + 1 \<le> t"
  1849         proof (rule dense_le)
  1850           fix x assume "x < y + (-X) + 1" then have "x < t"
  1851             using * t_is_Lub by auto
  1852           then show "x \<le> t"  by simp
  1853         qed
  1854         thus ?thesis by simp
  1855       qed
  1856     qed
  1857   qed
  1858 qed
  1859 
  1860 subsection {* The Archimedean Property of the Reals *}
  1861 
  1862 theorem reals_Archimedean:
  1863   fixes x :: real
  1864   assumes x_pos: "0 < x"
  1865   shows "\<exists>n. inverse (of_nat (Suc n)) < x"
  1866 proof (rule ccontr)
  1867   assume contr: "\<not> ?thesis"
  1868   have "\<forall>n. x * of_nat (Suc n) <= 1"
  1869   proof
  1870     fix n
  1871     from contr have "x \<le> inverse (of_nat (Suc n))"
  1872       by (simp add: linorder_not_less)
  1873     hence "x \<le> (1 / (of_nat (Suc n)))"
  1874       by (simp add: inverse_eq_divide)
  1875     moreover have "(0::real) \<le> of_nat (Suc n)"
  1876       by (rule of_nat_0_le_iff)
  1877     ultimately have "x * of_nat (Suc n) \<le> (1 / of_nat (Suc n)) * of_nat (Suc n)"
  1878       by (rule mult_right_mono)
  1879     thus "x * of_nat (Suc n) \<le> 1" by (simp del: of_nat_Suc)
  1880   qed
  1881   hence 2: "bdd_above {z. \<exists>n. z = x * (of_nat (Suc n))}"
  1882     by (auto intro!: bdd_aboveI[of _ 1])
  1883   have 1: "\<exists>X. X \<in> {z. \<exists>n. z = x* (of_nat (Suc n))}" by auto
  1884   obtain t where
  1885     upper: "\<And>z. z \<in> {z. \<exists>n. z = x * of_nat (Suc n)} \<Longrightarrow> z \<le> t" and
  1886     least: "\<And>y. (\<And>a. a \<in> {z. \<exists>n. z = x * of_nat (Suc n)} \<Longrightarrow> a \<le> y) \<Longrightarrow> t \<le> y"
  1887     using reals_complete[OF 1 2] by auto
  1888 
  1889 
  1890   have "t \<le> t + - x"
  1891   proof (rule least)
  1892     fix a assume a: "a \<in> {z. \<exists>n. z = x * (of_nat (Suc n))}"
  1893     have "\<forall>n::nat. x * of_nat n \<le> t + - x"
  1894     proof
  1895       fix n
  1896       have "x * of_nat (Suc n) \<le> t"
  1897         by (simp add: upper)
  1898       hence  "x * (of_nat n) + x \<le> t"
  1899         by (simp add: distrib_left)
  1900       thus  "x * (of_nat n) \<le> t + - x" by arith
  1901     qed    hence "\<forall>m. x * of_nat (Suc m) \<le> t + - x" by (simp del: of_nat_Suc)
  1902     with a show "a \<le> t + - x"
  1903       by auto
  1904   qed
  1905   thus False using x_pos by arith
  1906 qed
  1907 
  1908 text {*
  1909   There must be other proofs, e.g. @{text Suc} of the largest
  1910   integer in the cut representing @{text "x"}.
  1911 *}
  1912 
  1913 lemma reals_Archimedean2: "\<exists>n. (x::real) < of_nat (n::nat)"
  1914 proof cases
  1915   assume "x \<le> 0"
  1916   hence "x < of_nat (1::nat)" by simp
  1917   thus ?thesis ..
  1918 next
  1919   assume "\<not> x \<le> 0"
  1920   hence x_greater_zero: "0 < x" by simp
  1921   hence "0 < inverse x" by simp
  1922   then obtain n where "inverse (of_nat (Suc n)) < inverse x"
  1923     using reals_Archimedean by blast
  1924   hence "inverse (of_nat (Suc n)) * x < inverse x * x"
  1925     using x_greater_zero by (rule mult_strict_right_mono)
  1926   hence "inverse (of_nat (Suc n)) * x < 1"
  1927     using x_greater_zero by simp
  1928   hence "of_nat (Suc n) * (inverse (of_nat (Suc n)) * x) < of_nat (Suc n) * 1"
  1929     by (rule mult_strict_left_mono) (simp del: of_nat_Suc)
  1930   hence "x < of_nat (Suc n)"
  1931     by (simp add: algebra_simps del: of_nat_Suc)
  1932   thus "\<exists>(n::nat). x < of_nat n" ..
  1933 qed
  1934 
  1935 instance real :: archimedean_field
  1936 proof
  1937   fix r :: real
  1938   obtain n :: nat where "r < of_nat n"
  1939     using reals_Archimedean2 ..
  1940   then have "r \<le> of_int (int n)"
  1941     by simp
  1942   then show "\<exists>z. r \<le> of_int z" ..
  1943 qed
  1944 
  1945 end