src/HOL/Fields.thy
author hoelzl
Fri Oct 24 15:07:51 2014 +0200 (2014-10-24)
changeset 58776 95e58e04e534
parent 58512 dc4d76dfa8f0
child 58826 2ed2eaabe3df
permissions -rw-r--r--
use NO_MATCH-simproc for distribution rules in field_simps, otherwise field_simps on '(a / (c + d)) * (e + f)' can be non-terminating
     1 (*  Title:      HOL/Fields.thy
     2     Author:     Gertrud Bauer
     3     Author:     Steven Obua
     4     Author:     Tobias Nipkow
     5     Author:     Lawrence C Paulson
     6     Author:     Markus Wenzel
     7     Author:     Jeremy Avigad
     8 *)
     9 
    10 header {* Fields *}
    11 
    12 theory Fields
    13 imports Rings
    14 begin
    15 
    16 subsection {* Division rings *}
    17 
    18 text {*
    19   A division ring is like a field, but without the commutativity requirement.
    20 *}
    21 
    22 class inverse =
    23   fixes inverse :: "'a \<Rightarrow> 'a"
    24     and divide :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "'/" 70)
    25 
    26 setup {* Sign.add_const_constraint
    27   (@{const_name "divide"}, SOME @{typ "'a \<Rightarrow> 'a \<Rightarrow> 'a"}) *}
    28 
    29 
    30 context semiring
    31 begin
    32 
    33 lemma [field_simps]:
    34   shows distrib_left_NO_MATCH: "NO_MATCH a (x / y) \<Longrightarrow> a * (b + c) = a * b + a * c"
    35     and distrib_right_NO_MATCH: "NO_MATCH c (x / y) \<Longrightarrow> (a + b) * c = a * c + b * c"
    36   by (rule distrib_left distrib_right)+
    37 
    38 end
    39 
    40 context ring
    41 begin
    42 
    43 lemma [field_simps]:
    44   shows left_diff_distrib_NO_MATCH: "NO_MATCH c (x / y) \<Longrightarrow> (a - b) * c = a * c - b * c"
    45     and right_diff_distrib_NO_MATCH: "NO_MATCH a (x / y) \<Longrightarrow> a * (b - c) = a * b - a * c"
    46   by (rule left_diff_distrib right_diff_distrib)+
    47 
    48 end
    49 
    50 setup {* Sign.add_const_constraint
    51   (@{const_name "divide"}, SOME @{typ "'a::inverse \<Rightarrow> 'a \<Rightarrow> 'a"}) *}
    52 
    53 text{* Lemmas @{text divide_simps} move division to the outside and eliminates them on (in)equalities. *}
    54 
    55 named_theorems divide_simps "rewrite rules to eliminate divisions"
    56 
    57 class division_ring = ring_1 + inverse +
    58   assumes left_inverse [simp]:  "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
    59   assumes right_inverse [simp]: "a \<noteq> 0 \<Longrightarrow> a * inverse a = 1"
    60   assumes divide_inverse: "a / b = a * inverse b"
    61 begin
    62 
    63 subclass ring_1_no_zero_divisors
    64 proof
    65   fix a b :: 'a
    66   assume a: "a \<noteq> 0" and b: "b \<noteq> 0"
    67   show "a * b \<noteq> 0"
    68   proof
    69     assume ab: "a * b = 0"
    70     hence "0 = inverse a * (a * b) * inverse b" by simp
    71     also have "\<dots> = (inverse a * a) * (b * inverse b)"
    72       by (simp only: mult.assoc)
    73     also have "\<dots> = 1" using a b by simp
    74     finally show False by simp
    75   qed
    76 qed
    77 
    78 lemma nonzero_imp_inverse_nonzero:
    79   "a \<noteq> 0 \<Longrightarrow> inverse a \<noteq> 0"
    80 proof
    81   assume ianz: "inverse a = 0"
    82   assume "a \<noteq> 0"
    83   hence "1 = a * inverse a" by simp
    84   also have "... = 0" by (simp add: ianz)
    85   finally have "1 = 0" .
    86   thus False by (simp add: eq_commute)
    87 qed
    88 
    89 lemma inverse_zero_imp_zero:
    90   "inverse a = 0 \<Longrightarrow> a = 0"
    91 apply (rule classical)
    92 apply (drule nonzero_imp_inverse_nonzero)
    93 apply auto
    94 done
    95 
    96 lemma inverse_unique: 
    97   assumes ab: "a * b = 1"
    98   shows "inverse a = b"
    99 proof -
   100   have "a \<noteq> 0" using ab by (cases "a = 0") simp_all
   101   moreover have "inverse a * (a * b) = inverse a" by (simp add: ab)
   102   ultimately show ?thesis by (simp add: mult.assoc [symmetric])
   103 qed
   104 
   105 lemma nonzero_inverse_minus_eq:
   106   "a \<noteq> 0 \<Longrightarrow> inverse (- a) = - inverse a"
   107 by (rule inverse_unique) simp
   108 
   109 lemma nonzero_inverse_inverse_eq:
   110   "a \<noteq> 0 \<Longrightarrow> inverse (inverse a) = a"
   111 by (rule inverse_unique) simp
   112 
   113 lemma nonzero_inverse_eq_imp_eq:
   114   assumes "inverse a = inverse b" and "a \<noteq> 0" and "b \<noteq> 0"
   115   shows "a = b"
   116 proof -
   117   from `inverse a = inverse b`
   118   have "inverse (inverse a) = inverse (inverse b)" by (rule arg_cong)
   119   with `a \<noteq> 0` and `b \<noteq> 0` show "a = b"
   120     by (simp add: nonzero_inverse_inverse_eq)
   121 qed
   122 
   123 lemma inverse_1 [simp]: "inverse 1 = 1"
   124 by (rule inverse_unique) simp
   125 
   126 lemma nonzero_inverse_mult_distrib: 
   127   assumes "a \<noteq> 0" and "b \<noteq> 0"
   128   shows "inverse (a * b) = inverse b * inverse a"
   129 proof -
   130   have "a * (b * inverse b) * inverse a = 1" using assms by simp
   131   hence "a * b * (inverse b * inverse a) = 1" by (simp only: mult.assoc)
   132   thus ?thesis by (rule inverse_unique)
   133 qed
   134 
   135 lemma division_ring_inverse_add:
   136   "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> inverse a + inverse b = inverse a * (a + b) * inverse b"
   137 by (simp add: algebra_simps)
   138 
   139 lemma division_ring_inverse_diff:
   140   "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> inverse a - inverse b = inverse a * (b - a) * inverse b"
   141 by (simp add: algebra_simps)
   142 
   143 lemma right_inverse_eq: "b \<noteq> 0 \<Longrightarrow> a / b = 1 \<longleftrightarrow> a = b"
   144 proof
   145   assume neq: "b \<noteq> 0"
   146   {
   147     hence "a = (a / b) * b" by (simp add: divide_inverse mult.assoc)
   148     also assume "a / b = 1"
   149     finally show "a = b" by simp
   150   next
   151     assume "a = b"
   152     with neq show "a / b = 1" by (simp add: divide_inverse)
   153   }
   154 qed
   155 
   156 lemma nonzero_inverse_eq_divide: "a \<noteq> 0 \<Longrightarrow> inverse a = 1 / a"
   157 by (simp add: divide_inverse)
   158 
   159 lemma divide_self [simp]: "a \<noteq> 0 \<Longrightarrow> a / a = 1"
   160 by (simp add: divide_inverse)
   161 
   162 lemma divide_zero_left [simp]: "0 / a = 0"
   163 by (simp add: divide_inverse)
   164 
   165 lemma inverse_eq_divide [field_simps, divide_simps]: "inverse a = 1 / a"
   166 by (simp add: divide_inverse)
   167 
   168 lemma add_divide_distrib: "(a+b) / c = a/c + b/c"
   169 by (simp add: divide_inverse algebra_simps)
   170 
   171 lemma divide_1 [simp]: "a / 1 = a"
   172   by (simp add: divide_inverse)
   173 
   174 lemma times_divide_eq_right [simp]: "a * (b / c) = (a * b) / c"
   175   by (simp add: divide_inverse mult.assoc)
   176 
   177 lemma minus_divide_left: "- (a / b) = (-a) / b"
   178   by (simp add: divide_inverse)
   179 
   180 lemma nonzero_minus_divide_right: "b \<noteq> 0 ==> - (a / b) = a / (- b)"
   181   by (simp add: divide_inverse nonzero_inverse_minus_eq)
   182 
   183 lemma nonzero_minus_divide_divide: "b \<noteq> 0 ==> (-a) / (-b) = a / b"
   184   by (simp add: divide_inverse nonzero_inverse_minus_eq)
   185 
   186 lemma divide_minus_left [simp]: "(-a) / b = - (a / b)"
   187   by (simp add: divide_inverse)
   188 
   189 lemma diff_divide_distrib: "(a - b) / c = a / c - b / c"
   190   using add_divide_distrib [of a "- b" c] by simp
   191 
   192 lemma nonzero_eq_divide_eq [field_simps]: "c \<noteq> 0 \<Longrightarrow> a = b / c \<longleftrightarrow> a * c = b"
   193 proof -
   194   assume [simp]: "c \<noteq> 0"
   195   have "a = b / c \<longleftrightarrow> a * c = (b / c) * c" by simp
   196   also have "... \<longleftrightarrow> a * c = b" by (simp add: divide_inverse mult.assoc)
   197   finally show ?thesis .
   198 qed
   199 
   200 lemma nonzero_divide_eq_eq [field_simps]: "c \<noteq> 0 \<Longrightarrow> b / c = a \<longleftrightarrow> b = a * c"
   201 proof -
   202   assume [simp]: "c \<noteq> 0"
   203   have "b / c = a \<longleftrightarrow> (b / c) * c = a * c" by simp
   204   also have "... \<longleftrightarrow> b = a * c" by (simp add: divide_inverse mult.assoc) 
   205   finally show ?thesis .
   206 qed
   207 
   208 lemma nonzero_neg_divide_eq_eq [field_simps]: "b \<noteq> 0 \<Longrightarrow> - (a / b) = c \<longleftrightarrow> - a = c * b"
   209   using nonzero_divide_eq_eq[of b "-a" c] by (simp add: divide_minus_left)
   210 
   211 lemma nonzero_neg_divide_eq_eq2 [field_simps]: "b \<noteq> 0 \<Longrightarrow> c = - (a / b) \<longleftrightarrow> c * b = - a"
   212   using nonzero_neg_divide_eq_eq[of b a c] by auto
   213 
   214 lemma divide_eq_imp: "c \<noteq> 0 \<Longrightarrow> b = a * c \<Longrightarrow> b / c = a"
   215   by (simp add: divide_inverse mult.assoc)
   216 
   217 lemma eq_divide_imp: "c \<noteq> 0 \<Longrightarrow> a * c = b \<Longrightarrow> a = b / c"
   218   by (drule sym) (simp add: divide_inverse mult.assoc)
   219 
   220 lemma add_divide_eq_iff [field_simps]:
   221   "z \<noteq> 0 \<Longrightarrow> x + y / z = (x * z + y) / z"
   222   by (simp add: add_divide_distrib nonzero_eq_divide_eq)
   223 
   224 lemma divide_add_eq_iff [field_simps]:
   225   "z \<noteq> 0 \<Longrightarrow> x / z + y = (x + y * z) / z"
   226   by (simp add: add_divide_distrib nonzero_eq_divide_eq)
   227 
   228 lemma diff_divide_eq_iff [field_simps]:
   229   "z \<noteq> 0 \<Longrightarrow> x - y / z = (x * z - y) / z"
   230   by (simp add: diff_divide_distrib nonzero_eq_divide_eq eq_diff_eq)
   231 
   232 lemma minus_divide_add_eq_iff [field_simps]:
   233   "z \<noteq> 0 \<Longrightarrow> - (x / z) + y = (- x + y * z) / z"
   234   by (simp add: add_divide_distrib diff_divide_eq_iff divide_minus_left)
   235 
   236 lemma divide_diff_eq_iff [field_simps]:
   237   "z \<noteq> 0 \<Longrightarrow> x / z - y = (x - y * z) / z"
   238   by (simp add: field_simps)
   239 
   240 lemma minus_divide_diff_eq_iff [field_simps]:
   241   "z \<noteq> 0 \<Longrightarrow> - (x / z) - y = (- x - y * z) / z"
   242   by (simp add: divide_diff_eq_iff[symmetric] divide_minus_left)
   243 
   244 end
   245 
   246 class division_ring_inverse_zero = division_ring +
   247   assumes inverse_zero [simp]: "inverse 0 = 0"
   248 begin
   249 
   250 lemma divide_zero [simp]:
   251   "a / 0 = 0"
   252   by (simp add: divide_inverse)
   253 
   254 lemma divide_self_if [simp]:
   255   "a / a = (if a = 0 then 0 else 1)"
   256   by simp
   257 
   258 lemma inverse_nonzero_iff_nonzero [simp]:
   259   "inverse a = 0 \<longleftrightarrow> a = 0"
   260   by rule (fact inverse_zero_imp_zero, simp)
   261 
   262 lemma inverse_minus_eq [simp]:
   263   "inverse (- a) = - inverse a"
   264 proof cases
   265   assume "a=0" thus ?thesis by simp
   266 next
   267   assume "a\<noteq>0" 
   268   thus ?thesis by (simp add: nonzero_inverse_minus_eq)
   269 qed
   270 
   271 lemma inverse_inverse_eq [simp]:
   272   "inverse (inverse a) = a"
   273 proof cases
   274   assume "a=0" thus ?thesis by simp
   275 next
   276   assume "a\<noteq>0" 
   277   thus ?thesis by (simp add: nonzero_inverse_inverse_eq)
   278 qed
   279 
   280 lemma inverse_eq_imp_eq:
   281   "inverse a = inverse b \<Longrightarrow> a = b"
   282   by (drule arg_cong [where f="inverse"], simp)
   283 
   284 lemma inverse_eq_iff_eq [simp]:
   285   "inverse a = inverse b \<longleftrightarrow> a = b"
   286   by (force dest!: inverse_eq_imp_eq)
   287 
   288 lemma add_divide_eq_if_simps [divide_simps]:
   289     "a + b / z = (if z = 0 then a else (a * z + b) / z)"
   290     "a / z + b = (if z = 0 then b else (a + b * z) / z)"
   291     "- (a / z) + b = (if z = 0 then b else (-a + b * z) / z)"
   292     "a - b / z = (if z = 0 then a else (a * z - b) / z)"
   293     "a / z - b = (if z = 0 then -b else (a - b * z) / z)"
   294     "- (a / z) - b = (if z = 0 then -b else (- a - b * z) / z)"
   295   by (simp_all add: add_divide_eq_iff divide_add_eq_iff diff_divide_eq_iff divide_diff_eq_iff
   296       minus_divide_diff_eq_iff)
   297 
   298 lemma [divide_simps]:
   299   shows divide_eq_eq: "b / c = a \<longleftrightarrow> (if c \<noteq> 0 then b = a * c else a = 0)"
   300     and eq_divide_eq: "a = b / c \<longleftrightarrow> (if c \<noteq> 0 then a * c = b else a = 0)"
   301     and minus_divide_eq_eq: "- (b / c) = a \<longleftrightarrow> (if c \<noteq> 0 then - b = a * c else a = 0)"
   302     and eq_minus_divide_eq: "a = - (b / c) \<longleftrightarrow> (if c \<noteq> 0 then a * c = - b else a = 0)"
   303   by (auto simp add:  field_simps)
   304 
   305 end
   306 
   307 subsection {* Fields *}
   308 
   309 class field = comm_ring_1 + inverse +
   310   assumes field_inverse: "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
   311   assumes field_divide_inverse: "a / b = a * inverse b"
   312 begin
   313 
   314 subclass division_ring
   315 proof
   316   fix a :: 'a
   317   assume "a \<noteq> 0"
   318   thus "inverse a * a = 1" by (rule field_inverse)
   319   thus "a * inverse a = 1" by (simp only: mult.commute)
   320 next
   321   fix a b :: 'a
   322   show "a / b = a * inverse b" by (rule field_divide_inverse)
   323 qed
   324 
   325 subclass idom ..
   326 
   327 text{*There is no slick version using division by zero.*}
   328 lemma inverse_add:
   329   "[| a \<noteq> 0;  b \<noteq> 0 |]
   330    ==> inverse a + inverse b = (a + b) * inverse a * inverse b"
   331 by (simp add: division_ring_inverse_add ac_simps)
   332 
   333 lemma nonzero_mult_divide_mult_cancel_left [simp]:
   334 assumes [simp]: "b\<noteq>0" and [simp]: "c\<noteq>0" shows "(c*a)/(c*b) = a/b"
   335 proof -
   336   have "(c*a)/(c*b) = c * a * (inverse b * inverse c)"
   337     by (simp add: divide_inverse nonzero_inverse_mult_distrib)
   338   also have "... =  a * inverse b * (inverse c * c)"
   339     by (simp only: ac_simps)
   340   also have "... =  a * inverse b" by simp
   341     finally show ?thesis by (simp add: divide_inverse)
   342 qed
   343 
   344 lemma nonzero_mult_divide_mult_cancel_right [simp]:
   345   "\<lbrakk>b \<noteq> 0; c \<noteq> 0\<rbrakk> \<Longrightarrow> (a * c) / (b * c) = a / b"
   346 by (simp add: mult.commute [of _ c])
   347 
   348 lemma times_divide_eq_left [simp]: "(b / c) * a = (b * a) / c"
   349   by (simp add: divide_inverse ac_simps)
   350 
   351 text{*It's not obvious whether @{text times_divide_eq} should be
   352   simprules or not. Their effect is to gather terms into one big
   353   fraction, like a*b*c / x*y*z. The rationale for that is unclear, but
   354   many proofs seem to need them.*}
   355 
   356 lemmas times_divide_eq = times_divide_eq_right times_divide_eq_left
   357 
   358 lemma add_frac_eq:
   359   assumes "y \<noteq> 0" and "z \<noteq> 0"
   360   shows "x / y + w / z = (x * z + w * y) / (y * z)"
   361 proof -
   362   have "x / y + w / z = (x * z) / (y * z) + (y * w) / (y * z)"
   363     using assms by simp
   364   also have "\<dots> = (x * z + y * w) / (y * z)"
   365     by (simp only: add_divide_distrib)
   366   finally show ?thesis
   367     by (simp only: mult.commute)
   368 qed
   369 
   370 text{*Special Cancellation Simprules for Division*}
   371 
   372 lemma nonzero_mult_divide_cancel_right [simp]:
   373   "b \<noteq> 0 \<Longrightarrow> a * b / b = a"
   374   using nonzero_mult_divide_mult_cancel_right [of 1 b a] by simp
   375 
   376 lemma nonzero_mult_divide_cancel_left [simp]:
   377   "a \<noteq> 0 \<Longrightarrow> a * b / a = b"
   378 using nonzero_mult_divide_mult_cancel_left [of 1 a b] by simp
   379 
   380 lemma nonzero_divide_mult_cancel_right [simp]:
   381   "\<lbrakk>a \<noteq> 0; b \<noteq> 0\<rbrakk> \<Longrightarrow> b / (a * b) = 1 / a"
   382 using nonzero_mult_divide_mult_cancel_right [of a b 1] by simp
   383 
   384 lemma nonzero_divide_mult_cancel_left [simp]:
   385   "\<lbrakk>a \<noteq> 0; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / (a * b) = 1 / b"
   386 using nonzero_mult_divide_mult_cancel_left [of b a 1] by simp
   387 
   388 lemma nonzero_mult_divide_mult_cancel_left2 [simp]:
   389   "\<lbrakk>b \<noteq> 0; c \<noteq> 0\<rbrakk> \<Longrightarrow> (c * a) / (b * c) = a / b"
   390 using nonzero_mult_divide_mult_cancel_left [of b c a] by (simp add: ac_simps)
   391 
   392 lemma nonzero_mult_divide_mult_cancel_right2 [simp]:
   393   "\<lbrakk>b \<noteq> 0; c \<noteq> 0\<rbrakk> \<Longrightarrow> (a * c) / (c * b) = a / b"
   394 using nonzero_mult_divide_mult_cancel_right [of b c a] by (simp add: ac_simps)
   395 
   396 lemma diff_frac_eq:
   397   "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> x / y - w / z = (x * z - w * y) / (y * z)"
   398   by (simp add: field_simps)
   399 
   400 lemma frac_eq_eq:
   401   "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> (x / y = w / z) = (x * z = w * y)"
   402   by (simp add: field_simps)
   403 
   404 lemma divide_minus1 [simp]: "x / - 1 = - x"
   405   using nonzero_minus_divide_right [of "1" x] by simp
   406   
   407 end
   408 
   409 class field_inverse_zero = field +
   410   assumes field_inverse_zero: "inverse 0 = 0"
   411 begin
   412 
   413 subclass division_ring_inverse_zero proof
   414 qed (fact field_inverse_zero)
   415 
   416 text{*This version builds in division by zero while also re-orienting
   417       the right-hand side.*}
   418 lemma inverse_mult_distrib [simp]:
   419   "inverse (a * b) = inverse a * inverse b"
   420 proof cases
   421   assume "a \<noteq> 0 & b \<noteq> 0" 
   422   thus ?thesis by (simp add: nonzero_inverse_mult_distrib ac_simps)
   423 next
   424   assume "~ (a \<noteq> 0 & b \<noteq> 0)" 
   425   thus ?thesis by force
   426 qed
   427 
   428 lemma inverse_divide [simp]:
   429   "inverse (a / b) = b / a"
   430   by (simp add: divide_inverse mult.commute)
   431 
   432 
   433 text {* Calculations with fractions *}
   434 
   435 text{* There is a whole bunch of simp-rules just for class @{text
   436 field} but none for class @{text field} and @{text nonzero_divides}
   437 because the latter are covered by a simproc. *}
   438 
   439 lemma mult_divide_mult_cancel_left:
   440   "c \<noteq> 0 \<Longrightarrow> (c * a) / (c * b) = a / b"
   441 apply (cases "b = 0")
   442 apply simp_all
   443 done
   444 
   445 lemma mult_divide_mult_cancel_right:
   446   "c \<noteq> 0 \<Longrightarrow> (a * c) / (b * c) = a / b"
   447 apply (cases "b = 0")
   448 apply simp_all
   449 done
   450 
   451 lemma divide_divide_eq_right [simp]:
   452   "a / (b / c) = (a * c) / b"
   453   by (simp add: divide_inverse ac_simps)
   454 
   455 lemma divide_divide_eq_left [simp]:
   456   "(a / b) / c = a / (b * c)"
   457   by (simp add: divide_inverse mult.assoc)
   458 
   459 lemma divide_divide_times_eq:
   460   "(x / y) / (z / w) = (x * w) / (y * z)"
   461   by simp
   462 
   463 text {*Special Cancellation Simprules for Division*}
   464 
   465 lemma mult_divide_mult_cancel_left_if [simp]:
   466   shows "(c * a) / (c * b) = (if c = 0 then 0 else a / b)"
   467   by (simp add: mult_divide_mult_cancel_left)
   468 
   469 
   470 text {* Division and Unary Minus *}
   471 
   472 lemma minus_divide_right:
   473   "- (a / b) = a / - b"
   474   by (simp add: divide_inverse)
   475 
   476 lemma divide_minus_right [simp]:
   477   "a / - b = - (a / b)"
   478   by (simp add: divide_inverse)
   479 
   480 lemma minus_divide_divide:
   481   "(- a) / (- b) = a / b"
   482 apply (cases "b=0", simp) 
   483 apply (simp add: nonzero_minus_divide_divide) 
   484 done
   485 
   486 lemma inverse_eq_1_iff [simp]:
   487   "inverse x = 1 \<longleftrightarrow> x = 1"
   488   by (insert inverse_eq_iff_eq [of x 1], simp) 
   489 
   490 lemma divide_eq_0_iff [simp]:
   491   "a / b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
   492   by (simp add: divide_inverse)
   493 
   494 lemma divide_cancel_right [simp]:
   495   "a / c = b / c \<longleftrightarrow> c = 0 \<or> a = b"
   496   apply (cases "c=0", simp)
   497   apply (simp add: divide_inverse)
   498   done
   499 
   500 lemma divide_cancel_left [simp]:
   501   "c / a = c / b \<longleftrightarrow> c = 0 \<or> a = b" 
   502   apply (cases "c=0", simp)
   503   apply (simp add: divide_inverse)
   504   done
   505 
   506 lemma divide_eq_1_iff [simp]:
   507   "a / b = 1 \<longleftrightarrow> b \<noteq> 0 \<and> a = b"
   508   apply (cases "b=0", simp)
   509   apply (simp add: right_inverse_eq)
   510   done
   511 
   512 lemma one_eq_divide_iff [simp]:
   513   "1 = a / b \<longleftrightarrow> b \<noteq> 0 \<and> a = b"
   514   by (simp add: eq_commute [of 1])
   515 
   516 lemma times_divide_times_eq:
   517   "(x / y) * (z / w) = (x * z) / (y * w)"
   518   by simp
   519 
   520 lemma add_frac_num:
   521   "y \<noteq> 0 \<Longrightarrow> x / y + z = (x + z * y) / y"
   522   by (simp add: add_divide_distrib)
   523 
   524 lemma add_num_frac:
   525   "y \<noteq> 0 \<Longrightarrow> z + x / y = (x + z * y) / y"
   526   by (simp add: add_divide_distrib add.commute)
   527 
   528 end
   529 
   530 
   531 subsection {* Ordered fields *}
   532 
   533 class linordered_field = field + linordered_idom
   534 begin
   535 
   536 lemma positive_imp_inverse_positive: 
   537   assumes a_gt_0: "0 < a" 
   538   shows "0 < inverse a"
   539 proof -
   540   have "0 < a * inverse a" 
   541     by (simp add: a_gt_0 [THEN less_imp_not_eq2])
   542   thus "0 < inverse a" 
   543     by (simp add: a_gt_0 [THEN less_not_sym] zero_less_mult_iff)
   544 qed
   545 
   546 lemma negative_imp_inverse_negative:
   547   "a < 0 \<Longrightarrow> inverse a < 0"
   548   by (insert positive_imp_inverse_positive [of "-a"], 
   549     simp add: nonzero_inverse_minus_eq less_imp_not_eq)
   550 
   551 lemma inverse_le_imp_le:
   552   assumes invle: "inverse a \<le> inverse b" and apos: "0 < a"
   553   shows "b \<le> a"
   554 proof (rule classical)
   555   assume "~ b \<le> a"
   556   hence "a < b"  by (simp add: linorder_not_le)
   557   hence bpos: "0 < b"  by (blast intro: apos less_trans)
   558   hence "a * inverse a \<le> a * inverse b"
   559     by (simp add: apos invle less_imp_le mult_left_mono)
   560   hence "(a * inverse a) * b \<le> (a * inverse b) * b"
   561     by (simp add: bpos less_imp_le mult_right_mono)
   562   thus "b \<le> a"  by (simp add: mult.assoc apos bpos less_imp_not_eq2)
   563 qed
   564 
   565 lemma inverse_positive_imp_positive:
   566   assumes inv_gt_0: "0 < inverse a" and nz: "a \<noteq> 0"
   567   shows "0 < a"
   568 proof -
   569   have "0 < inverse (inverse a)"
   570     using inv_gt_0 by (rule positive_imp_inverse_positive)
   571   thus "0 < a"
   572     using nz by (simp add: nonzero_inverse_inverse_eq)
   573 qed
   574 
   575 lemma inverse_negative_imp_negative:
   576   assumes inv_less_0: "inverse a < 0" and nz: "a \<noteq> 0"
   577   shows "a < 0"
   578 proof -
   579   have "inverse (inverse a) < 0"
   580     using inv_less_0 by (rule negative_imp_inverse_negative)
   581   thus "a < 0" using nz by (simp add: nonzero_inverse_inverse_eq)
   582 qed
   583 
   584 lemma linordered_field_no_lb:
   585   "\<forall>x. \<exists>y. y < x"
   586 proof
   587   fix x::'a
   588   have m1: "- (1::'a) < 0" by simp
   589   from add_strict_right_mono[OF m1, where c=x] 
   590   have "(- 1) + x < x" by simp
   591   thus "\<exists>y. y < x" by blast
   592 qed
   593 
   594 lemma linordered_field_no_ub:
   595   "\<forall> x. \<exists>y. y > x"
   596 proof
   597   fix x::'a
   598   have m1: " (1::'a) > 0" by simp
   599   from add_strict_right_mono[OF m1, where c=x] 
   600   have "1 + x > x" by simp
   601   thus "\<exists>y. y > x" by blast
   602 qed
   603 
   604 lemma less_imp_inverse_less:
   605   assumes less: "a < b" and apos:  "0 < a"
   606   shows "inverse b < inverse a"
   607 proof (rule ccontr)
   608   assume "~ inverse b < inverse a"
   609   hence "inverse a \<le> inverse b" by simp
   610   hence "~ (a < b)"
   611     by (simp add: not_less inverse_le_imp_le [OF _ apos])
   612   thus False by (rule notE [OF _ less])
   613 qed
   614 
   615 lemma inverse_less_imp_less:
   616   "inverse a < inverse b \<Longrightarrow> 0 < a \<Longrightarrow> b < a"
   617 apply (simp add: less_le [of "inverse a"] less_le [of "b"])
   618 apply (force dest!: inverse_le_imp_le nonzero_inverse_eq_imp_eq) 
   619 done
   620 
   621 text{*Both premises are essential. Consider -1 and 1.*}
   622 lemma inverse_less_iff_less [simp]:
   623   "0 < a \<Longrightarrow> 0 < b \<Longrightarrow> inverse a < inverse b \<longleftrightarrow> b < a"
   624   by (blast intro: less_imp_inverse_less dest: inverse_less_imp_less) 
   625 
   626 lemma le_imp_inverse_le:
   627   "a \<le> b \<Longrightarrow> 0 < a \<Longrightarrow> inverse b \<le> inverse a"
   628   by (force simp add: le_less less_imp_inverse_less)
   629 
   630 lemma inverse_le_iff_le [simp]:
   631   "0 < a \<Longrightarrow> 0 < b \<Longrightarrow> inverse a \<le> inverse b \<longleftrightarrow> b \<le> a"
   632   by (blast intro: le_imp_inverse_le dest: inverse_le_imp_le) 
   633 
   634 
   635 text{*These results refer to both operands being negative.  The opposite-sign
   636 case is trivial, since inverse preserves signs.*}
   637 lemma inverse_le_imp_le_neg:
   638   "inverse a \<le> inverse b \<Longrightarrow> b < 0 \<Longrightarrow> b \<le> a"
   639 apply (rule classical) 
   640 apply (subgoal_tac "a < 0") 
   641  prefer 2 apply force
   642 apply (insert inverse_le_imp_le [of "-b" "-a"])
   643 apply (simp add: nonzero_inverse_minus_eq) 
   644 done
   645 
   646 lemma less_imp_inverse_less_neg:
   647    "a < b \<Longrightarrow> b < 0 \<Longrightarrow> inverse b < inverse a"
   648 apply (subgoal_tac "a < 0") 
   649  prefer 2 apply (blast intro: less_trans) 
   650 apply (insert less_imp_inverse_less [of "-b" "-a"])
   651 apply (simp add: nonzero_inverse_minus_eq) 
   652 done
   653 
   654 lemma inverse_less_imp_less_neg:
   655    "inverse a < inverse b \<Longrightarrow> b < 0 \<Longrightarrow> b < a"
   656 apply (rule classical) 
   657 apply (subgoal_tac "a < 0") 
   658  prefer 2
   659  apply force
   660 apply (insert inverse_less_imp_less [of "-b" "-a"])
   661 apply (simp add: nonzero_inverse_minus_eq) 
   662 done
   663 
   664 lemma inverse_less_iff_less_neg [simp]:
   665   "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> inverse a < inverse b \<longleftrightarrow> b < a"
   666 apply (insert inverse_less_iff_less [of "-b" "-a"])
   667 apply (simp del: inverse_less_iff_less 
   668             add: nonzero_inverse_minus_eq)
   669 done
   670 
   671 lemma le_imp_inverse_le_neg:
   672   "a \<le> b \<Longrightarrow> b < 0 ==> inverse b \<le> inverse a"
   673   by (force simp add: le_less less_imp_inverse_less_neg)
   674 
   675 lemma inverse_le_iff_le_neg [simp]:
   676   "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> inverse a \<le> inverse b \<longleftrightarrow> b \<le> a"
   677   by (blast intro: le_imp_inverse_le_neg dest: inverse_le_imp_le_neg) 
   678 
   679 lemma one_less_inverse:
   680   "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> 1 < inverse a"
   681   using less_imp_inverse_less [of a 1, unfolded inverse_1] .
   682 
   683 lemma one_le_inverse:
   684   "0 < a \<Longrightarrow> a \<le> 1 \<Longrightarrow> 1 \<le> inverse a"
   685   using le_imp_inverse_le [of a 1, unfolded inverse_1] .
   686 
   687 lemma pos_le_divide_eq [field_simps]: "0 < c \<Longrightarrow> a \<le> b / c \<longleftrightarrow> a * c \<le> b"
   688 proof -
   689   assume less: "0<c"
   690   hence "(a \<le> b/c) = (a*c \<le> (b/c)*c)"
   691     by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
   692   also have "... = (a*c \<le> b)"
   693     by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult.assoc) 
   694   finally show ?thesis .
   695 qed
   696 
   697 lemma neg_le_divide_eq [field_simps]: "c < 0 \<Longrightarrow> a \<le> b / c \<longleftrightarrow> b \<le> a * c"
   698 proof -
   699   assume less: "c<0"
   700   hence "(a \<le> b/c) = ((b/c)*c \<le> a*c)"
   701     by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
   702   also have "... = (b \<le> a*c)"
   703     by (simp add: less_imp_not_eq [OF less] divide_inverse mult.assoc) 
   704   finally show ?thesis .
   705 qed
   706 
   707 lemma pos_less_divide_eq [field_simps]: "0 < c \<Longrightarrow> a < b / c \<longleftrightarrow> a * c < b"
   708 proof -
   709   assume less: "0<c"
   710   hence "(a < b/c) = (a*c < (b/c)*c)"
   711     by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
   712   also have "... = (a*c < b)"
   713     by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult.assoc) 
   714   finally show ?thesis .
   715 qed
   716 
   717 lemma neg_less_divide_eq [field_simps]: "c < 0 \<Longrightarrow> a < b / c \<longleftrightarrow> b < a * c"
   718 proof -
   719   assume less: "c<0"
   720   hence "(a < b/c) = ((b/c)*c < a*c)"
   721     by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
   722   also have "... = (b < a*c)"
   723     by (simp add: less_imp_not_eq [OF less] divide_inverse mult.assoc) 
   724   finally show ?thesis .
   725 qed
   726 
   727 lemma pos_divide_less_eq [field_simps]: "0 < c \<Longrightarrow> b / c < a \<longleftrightarrow> b < a * c"
   728 proof -
   729   assume less: "0<c"
   730   hence "(b/c < a) = ((b/c)*c < a*c)"
   731     by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
   732   also have "... = (b < a*c)"
   733     by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult.assoc) 
   734   finally show ?thesis .
   735 qed
   736 
   737 lemma neg_divide_less_eq [field_simps]: "c < 0 \<Longrightarrow> b / c < a \<longleftrightarrow> a * c < b"
   738 proof -
   739   assume less: "c<0"
   740   hence "(b/c < a) = (a*c < (b/c)*c)"
   741     by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
   742   also have "... = (a*c < b)"
   743     by (simp add: less_imp_not_eq [OF less] divide_inverse mult.assoc) 
   744   finally show ?thesis .
   745 qed
   746 
   747 lemma pos_divide_le_eq [field_simps]: "0 < c \<Longrightarrow> b / c \<le> a \<longleftrightarrow> b \<le> a * c"
   748 proof -
   749   assume less: "0<c"
   750   hence "(b/c \<le> a) = ((b/c)*c \<le> a*c)"
   751     by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
   752   also have "... = (b \<le> a*c)"
   753     by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult.assoc) 
   754   finally show ?thesis .
   755 qed
   756 
   757 lemma neg_divide_le_eq [field_simps]: "c < 0 \<Longrightarrow> b / c \<le> a \<longleftrightarrow> a * c \<le> b"
   758 proof -
   759   assume less: "c<0"
   760   hence "(b/c \<le> a) = (a*c \<le> (b/c)*c)"
   761     by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
   762   also have "... = (a*c \<le> b)"
   763     by (simp add: less_imp_not_eq [OF less] divide_inverse mult.assoc) 
   764   finally show ?thesis .
   765 qed
   766 
   767 text{* The following @{text field_simps} rules are necessary, as minus is always moved atop of
   768 division but we want to get rid of division. *}
   769 
   770 lemma pos_le_minus_divide_eq [field_simps]: "0 < c \<Longrightarrow> a \<le> - (b / c) \<longleftrightarrow> a * c \<le> - b"
   771   unfolding minus_divide_left by (rule pos_le_divide_eq)
   772 
   773 lemma neg_le_minus_divide_eq [field_simps]: "c < 0 \<Longrightarrow> a \<le> - (b / c) \<longleftrightarrow> - b \<le> a * c"
   774   unfolding minus_divide_left by (rule neg_le_divide_eq)
   775 
   776 lemma pos_less_minus_divide_eq [field_simps]: "0 < c \<Longrightarrow> a < - (b / c) \<longleftrightarrow> a * c < - b"
   777   unfolding minus_divide_left by (rule pos_less_divide_eq)
   778 
   779 lemma neg_less_minus_divide_eq [field_simps]: "c < 0 \<Longrightarrow> a < - (b / c) \<longleftrightarrow> - b < a * c"
   780   unfolding minus_divide_left by (rule neg_less_divide_eq)
   781 
   782 lemma pos_minus_divide_less_eq [field_simps]: "0 < c \<Longrightarrow> - (b / c) < a \<longleftrightarrow> - b < a * c"
   783   unfolding minus_divide_left by (rule pos_divide_less_eq)
   784 
   785 lemma neg_minus_divide_less_eq [field_simps]: "c < 0 \<Longrightarrow> - (b / c) < a \<longleftrightarrow> a * c < - b"
   786   unfolding minus_divide_left by (rule neg_divide_less_eq)
   787 
   788 lemma pos_minus_divide_le_eq [field_simps]: "0 < c \<Longrightarrow> - (b / c) \<le> a \<longleftrightarrow> - b \<le> a * c"
   789   unfolding minus_divide_left by (rule pos_divide_le_eq)
   790 
   791 lemma neg_minus_divide_le_eq [field_simps]: "c < 0 \<Longrightarrow> - (b / c) \<le> a \<longleftrightarrow> a * c \<le> - b"
   792   unfolding minus_divide_left by (rule neg_divide_le_eq)
   793 
   794 lemma frac_less_eq:
   795   "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> x / y < w / z \<longleftrightarrow> (x * z - w * y) / (y * z) < 0"
   796   by (subst less_iff_diff_less_0) (simp add: diff_frac_eq )
   797 
   798 lemma frac_le_eq:
   799   "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> x / y \<le> w / z \<longleftrightarrow> (x * z - w * y) / (y * z) \<le> 0"
   800   by (subst le_iff_diff_le_0) (simp add: diff_frac_eq )
   801 
   802 text{* Lemmas @{text sign_simps} is a first attempt to automate proofs
   803 of positivity/negativity needed for @{text field_simps}. Have not added @{text
   804 sign_simps} to @{text field_simps} because the former can lead to case
   805 explosions. *}
   806 
   807 lemmas sign_simps = algebra_simps zero_less_mult_iff mult_less_0_iff
   808 
   809 lemmas (in -) sign_simps = algebra_simps zero_less_mult_iff mult_less_0_iff
   810 
   811 (* Only works once linear arithmetic is installed:
   812 text{*An example:*}
   813 lemma fixes a b c d e f :: "'a::linordered_field"
   814 shows "\<lbrakk>a>b; c<d; e<f; 0 < u \<rbrakk> \<Longrightarrow>
   815  ((a-b)*(c-d)*(e-f))/((c-d)*(e-f)*(a-b)) <
   816  ((e-f)*(a-b)*(c-d))/((e-f)*(a-b)*(c-d)) + u"
   817 apply(subgoal_tac "(c-d)*(e-f)*(a-b) > 0")
   818  prefer 2 apply(simp add:sign_simps)
   819 apply(subgoal_tac "(c-d)*(e-f)*(a-b)*u > 0")
   820  prefer 2 apply(simp add:sign_simps)
   821 apply(simp add:field_simps)
   822 done
   823 *)
   824 
   825 lemma divide_pos_pos[simp]:
   826   "0 < x ==> 0 < y ==> 0 < x / y"
   827 by(simp add:field_simps)
   828 
   829 lemma divide_nonneg_pos:
   830   "0 <= x ==> 0 < y ==> 0 <= x / y"
   831 by(simp add:field_simps)
   832 
   833 lemma divide_neg_pos:
   834   "x < 0 ==> 0 < y ==> x / y < 0"
   835 by(simp add:field_simps)
   836 
   837 lemma divide_nonpos_pos:
   838   "x <= 0 ==> 0 < y ==> x / y <= 0"
   839 by(simp add:field_simps)
   840 
   841 lemma divide_pos_neg:
   842   "0 < x ==> y < 0 ==> x / y < 0"
   843 by(simp add:field_simps)
   844 
   845 lemma divide_nonneg_neg:
   846   "0 <= x ==> y < 0 ==> x / y <= 0" 
   847 by(simp add:field_simps)
   848 
   849 lemma divide_neg_neg:
   850   "x < 0 ==> y < 0 ==> 0 < x / y"
   851 by(simp add:field_simps)
   852 
   853 lemma divide_nonpos_neg:
   854   "x <= 0 ==> y < 0 ==> 0 <= x / y"
   855 by(simp add:field_simps)
   856 
   857 lemma divide_strict_right_mono:
   858      "[|a < b; 0 < c|] ==> a / c < b / c"
   859 by (simp add: less_imp_not_eq2 divide_inverse mult_strict_right_mono 
   860               positive_imp_inverse_positive)
   861 
   862 
   863 lemma divide_strict_right_mono_neg:
   864      "[|b < a; c < 0|] ==> a / c < b / c"
   865 apply (drule divide_strict_right_mono [of _ _ "-c"], simp)
   866 apply (simp add: less_imp_not_eq nonzero_minus_divide_right [symmetric])
   867 done
   868 
   869 text{*The last premise ensures that @{term a} and @{term b} 
   870       have the same sign*}
   871 lemma divide_strict_left_mono:
   872   "[|b < a; 0 < c; 0 < a*b|] ==> c / a < c / b"
   873   by (auto simp: field_simps zero_less_mult_iff mult_strict_right_mono)
   874 
   875 lemma divide_left_mono:
   876   "[|b \<le> a; 0 \<le> c; 0 < a*b|] ==> c / a \<le> c / b"
   877   by (auto simp: field_simps zero_less_mult_iff mult_right_mono)
   878 
   879 lemma divide_strict_left_mono_neg:
   880   "[|a < b; c < 0; 0 < a*b|] ==> c / a < c / b"
   881   by (auto simp: field_simps zero_less_mult_iff mult_strict_right_mono_neg)
   882 
   883 lemma mult_imp_div_pos_le: "0 < y ==> x <= z * y ==>
   884     x / y <= z"
   885 by (subst pos_divide_le_eq, assumption+)
   886 
   887 lemma mult_imp_le_div_pos: "0 < y ==> z * y <= x ==>
   888     z <= x / y"
   889 by(simp add:field_simps)
   890 
   891 lemma mult_imp_div_pos_less: "0 < y ==> x < z * y ==>
   892     x / y < z"
   893 by(simp add:field_simps)
   894 
   895 lemma mult_imp_less_div_pos: "0 < y ==> z * y < x ==>
   896     z < x / y"
   897 by(simp add:field_simps)
   898 
   899 lemma frac_le: "0 <= x ==> 
   900     x <= y ==> 0 < w ==> w <= z  ==> x / z <= y / w"
   901   apply (rule mult_imp_div_pos_le)
   902   apply simp
   903   apply (subst times_divide_eq_left)
   904   apply (rule mult_imp_le_div_pos, assumption)
   905   apply (rule mult_mono)
   906   apply simp_all
   907 done
   908 
   909 lemma frac_less: "0 <= x ==> 
   910     x < y ==> 0 < w ==> w <= z  ==> x / z < y / w"
   911   apply (rule mult_imp_div_pos_less)
   912   apply simp
   913   apply (subst times_divide_eq_left)
   914   apply (rule mult_imp_less_div_pos, assumption)
   915   apply (erule mult_less_le_imp_less)
   916   apply simp_all
   917 done
   918 
   919 lemma frac_less2: "0 < x ==> 
   920     x <= y ==> 0 < w ==> w < z  ==> x / z < y / w"
   921   apply (rule mult_imp_div_pos_less)
   922   apply simp_all
   923   apply (rule mult_imp_less_div_pos, assumption)
   924   apply (erule mult_le_less_imp_less)
   925   apply simp_all
   926 done
   927 
   928 lemma less_half_sum: "a < b ==> a < (a+b) / (1+1)"
   929 by (simp add: field_simps zero_less_two)
   930 
   931 lemma gt_half_sum: "a < b ==> (a+b)/(1+1) < b"
   932 by (simp add: field_simps zero_less_two)
   933 
   934 subclass unbounded_dense_linorder
   935 proof
   936   fix x y :: 'a
   937   from less_add_one show "\<exists>y. x < y" .. 
   938   from less_add_one have "x + (- 1) < (x + 1) + (- 1)" by (rule add_strict_right_mono)
   939   then have "x - 1 < x + 1 - 1" by simp
   940   then have "x - 1 < x" by (simp add: algebra_simps)
   941   then show "\<exists>y. y < x" ..
   942   show "x < y \<Longrightarrow> \<exists>z>x. z < y" by (blast intro!: less_half_sum gt_half_sum)
   943 qed
   944 
   945 lemma nonzero_abs_inverse:
   946      "a \<noteq> 0 ==> \<bar>inverse a\<bar> = inverse \<bar>a\<bar>"
   947 apply (auto simp add: neq_iff abs_if nonzero_inverse_minus_eq 
   948                       negative_imp_inverse_negative)
   949 apply (blast intro: positive_imp_inverse_positive elim: less_asym) 
   950 done
   951 
   952 lemma nonzero_abs_divide:
   953      "b \<noteq> 0 ==> \<bar>a / b\<bar> = \<bar>a\<bar> / \<bar>b\<bar>"
   954   by (simp add: divide_inverse abs_mult nonzero_abs_inverse) 
   955 
   956 lemma field_le_epsilon:
   957   assumes e: "\<And>e. 0 < e \<Longrightarrow> x \<le> y + e"
   958   shows "x \<le> y"
   959 proof (rule dense_le)
   960   fix t assume "t < x"
   961   hence "0 < x - t" by (simp add: less_diff_eq)
   962   from e [OF this] have "x + 0 \<le> x + (y - t)" by (simp add: algebra_simps)
   963   then have "0 \<le> y - t" by (simp only: add_le_cancel_left)
   964   then show "t \<le> y" by (simp add: algebra_simps)
   965 qed
   966 
   967 end
   968 
   969 class linordered_field_inverse_zero = linordered_field + field_inverse_zero
   970 begin
   971 
   972 lemma inverse_positive_iff_positive [simp]:
   973   "(0 < inverse a) = (0 < a)"
   974 apply (cases "a = 0", simp)
   975 apply (blast intro: inverse_positive_imp_positive positive_imp_inverse_positive)
   976 done
   977 
   978 lemma inverse_negative_iff_negative [simp]:
   979   "(inverse a < 0) = (a < 0)"
   980 apply (cases "a = 0", simp)
   981 apply (blast intro: inverse_negative_imp_negative negative_imp_inverse_negative)
   982 done
   983 
   984 lemma inverse_nonnegative_iff_nonnegative [simp]:
   985   "0 \<le> inverse a \<longleftrightarrow> 0 \<le> a"
   986   by (simp add: not_less [symmetric])
   987 
   988 lemma inverse_nonpositive_iff_nonpositive [simp]:
   989   "inverse a \<le> 0 \<longleftrightarrow> a \<le> 0"
   990   by (simp add: not_less [symmetric])
   991 
   992 lemma one_less_inverse_iff: "1 < inverse x \<longleftrightarrow> 0 < x \<and> x < 1"
   993   using less_trans[of 1 x 0 for x]
   994   by (cases x 0 rule: linorder_cases) (auto simp add: field_simps)
   995 
   996 lemma one_le_inverse_iff: "1 \<le> inverse x \<longleftrightarrow> 0 < x \<and> x \<le> 1"
   997 proof (cases "x = 1")
   998   case True then show ?thesis by simp
   999 next
  1000   case False then have "inverse x \<noteq> 1" by simp
  1001   then have "1 \<noteq> inverse x" by blast
  1002   then have "1 \<le> inverse x \<longleftrightarrow> 1 < inverse x" by (simp add: le_less)
  1003   with False show ?thesis by (auto simp add: one_less_inverse_iff)
  1004 qed
  1005 
  1006 lemma inverse_less_1_iff: "inverse x < 1 \<longleftrightarrow> x \<le> 0 \<or> 1 < x"
  1007   by (simp add: not_le [symmetric] one_le_inverse_iff) 
  1008 
  1009 lemma inverse_le_1_iff: "inverse x \<le> 1 \<longleftrightarrow> x \<le> 0 \<or> 1 \<le> x"
  1010   by (simp add: not_less [symmetric] one_less_inverse_iff) 
  1011 
  1012 lemma [divide_simps]:
  1013   shows le_divide_eq: "a \<le> b / c \<longleftrightarrow> (if 0 < c then a * c \<le> b else if c < 0 then b \<le> a * c else a \<le> 0)"
  1014     and divide_le_eq: "b / c \<le> a \<longleftrightarrow> (if 0 < c then b \<le> a * c else if c < 0 then a * c \<le> b else 0 \<le> a)"
  1015     and less_divide_eq: "a < b / c \<longleftrightarrow> (if 0 < c then a * c < b else if c < 0 then b < a * c else a < 0)"
  1016     and divide_less_eq: "b / c < a \<longleftrightarrow> (if 0 < c then b < a * c else if c < 0 then a * c < b else 0 < a)"
  1017     and le_minus_divide_eq: "a \<le> - (b / c) \<longleftrightarrow> (if 0 < c then a * c \<le> - b else if c < 0 then - b \<le> a * c else a \<le> 0)"
  1018     and minus_divide_le_eq: "- (b / c) \<le> a \<longleftrightarrow> (if 0 < c then - b \<le> a * c else if c < 0 then a * c \<le> - b else 0 \<le> a)"
  1019     and less_minus_divide_eq: "a < - (b / c) \<longleftrightarrow> (if 0 < c then a * c < - b else if c < 0 then - b < a * c else  a < 0)"
  1020     and minus_divide_less_eq: "- (b / c) < a \<longleftrightarrow> (if 0 < c then - b < a * c else if c < 0 then a * c < - b else 0 < a)"
  1021   by (auto simp: field_simps not_less dest: antisym)
  1022 
  1023 text {*Division and Signs*}
  1024 
  1025 lemma
  1026   shows zero_less_divide_iff: "0 < a / b \<longleftrightarrow> 0 < a \<and> 0 < b \<or> a < 0 \<and> b < 0"
  1027     and divide_less_0_iff: "a / b < 0 \<longleftrightarrow> 0 < a \<and> b < 0 \<or> a < 0 \<and> 0 < b"
  1028     and zero_le_divide_iff: "0 \<le> a / b \<longleftrightarrow> 0 \<le> a \<and> 0 \<le> b \<or> a \<le> 0 \<and> b \<le> 0"
  1029     and divide_le_0_iff: "a / b \<le> 0 \<longleftrightarrow> 0 \<le> a \<and> b \<le> 0 \<or> a \<le> 0 \<and> 0 \<le> b"
  1030   by (auto simp add: divide_simps)
  1031 
  1032 text {* Division and the Number One *}
  1033 
  1034 text{*Simplify expressions equated with 1*}
  1035 
  1036 lemma zero_eq_1_divide_iff [simp]: "0 = 1 / a \<longleftrightarrow> a = 0"
  1037   by (cases "a = 0") (auto simp: field_simps)
  1038 
  1039 lemma one_divide_eq_0_iff [simp]: "1 / a = 0 \<longleftrightarrow> a = 0"
  1040   using zero_eq_1_divide_iff[of a] by simp
  1041 
  1042 text{*Simplify expressions such as @{text "0 < 1/x"} to @{text "0 < x"}*}
  1043 
  1044 lemma zero_le_divide_1_iff [simp]:
  1045   "0 \<le> 1 / a \<longleftrightarrow> 0 \<le> a"
  1046   by (simp add: zero_le_divide_iff)
  1047 
  1048 lemma zero_less_divide_1_iff [simp]:
  1049   "0 < 1 / a \<longleftrightarrow> 0 < a"
  1050   by (simp add: zero_less_divide_iff)
  1051 
  1052 lemma divide_le_0_1_iff [simp]:
  1053   "1 / a \<le> 0 \<longleftrightarrow> a \<le> 0"
  1054   by (simp add: divide_le_0_iff)
  1055 
  1056 lemma divide_less_0_1_iff [simp]:
  1057   "1 / a < 0 \<longleftrightarrow> a < 0"
  1058   by (simp add: divide_less_0_iff)
  1059 
  1060 lemma divide_right_mono:
  1061      "[|a \<le> b; 0 \<le> c|] ==> a/c \<le> b/c"
  1062 by (force simp add: divide_strict_right_mono le_less)
  1063 
  1064 lemma divide_right_mono_neg: "a <= b 
  1065     ==> c <= 0 ==> b / c <= a / c"
  1066 apply (drule divide_right_mono [of _ _ "- c"])
  1067 apply auto
  1068 done
  1069 
  1070 lemma divide_left_mono_neg: "a <= b 
  1071     ==> c <= 0 ==> 0 < a * b ==> c / a <= c / b"
  1072   apply (drule divide_left_mono [of _ _ "- c"])
  1073   apply (auto simp add: mult.commute)
  1074 done
  1075 
  1076 lemma inverse_le_iff: "inverse a \<le> inverse b \<longleftrightarrow> (0 < a * b \<longrightarrow> b \<le> a) \<and> (a * b \<le> 0 \<longrightarrow> a \<le> b)"
  1077   by (cases a 0 b 0 rule: linorder_cases[case_product linorder_cases])
  1078      (auto simp add: field_simps zero_less_mult_iff mult_le_0_iff)
  1079 
  1080 lemma inverse_less_iff: "inverse a < inverse b \<longleftrightarrow> (0 < a * b \<longrightarrow> b < a) \<and> (a * b \<le> 0 \<longrightarrow> a < b)"
  1081   by (subst less_le) (auto simp: inverse_le_iff)
  1082 
  1083 lemma divide_le_cancel: "a / c \<le> b / c \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
  1084   by (simp add: divide_inverse mult_le_cancel_right)
  1085 
  1086 lemma divide_less_cancel: "a / c < b / c \<longleftrightarrow> (0 < c \<longrightarrow> a < b) \<and> (c < 0 \<longrightarrow> b < a) \<and> c \<noteq> 0"
  1087   by (auto simp add: divide_inverse mult_less_cancel_right)
  1088 
  1089 text{*Simplify quotients that are compared with the value 1.*}
  1090 
  1091 lemma le_divide_eq_1:
  1092   "(1 \<le> b / a) = ((0 < a & a \<le> b) | (a < 0 & b \<le> a))"
  1093 by (auto simp add: le_divide_eq)
  1094 
  1095 lemma divide_le_eq_1:
  1096   "(b / a \<le> 1) = ((0 < a & b \<le> a) | (a < 0 & a \<le> b) | a=0)"
  1097 by (auto simp add: divide_le_eq)
  1098 
  1099 lemma less_divide_eq_1:
  1100   "(1 < b / a) = ((0 < a & a < b) | (a < 0 & b < a))"
  1101 by (auto simp add: less_divide_eq)
  1102 
  1103 lemma divide_less_eq_1:
  1104   "(b / a < 1) = ((0 < a & b < a) | (a < 0 & a < b) | a=0)"
  1105 by (auto simp add: divide_less_eq)
  1106 
  1107 lemma divide_nonneg_nonneg [simp]:
  1108   "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x / y"
  1109   by (auto simp add: divide_simps)
  1110 
  1111 lemma divide_nonpos_nonpos:
  1112   "x \<le> 0 \<Longrightarrow> y \<le> 0 \<Longrightarrow> 0 \<le> x / y"
  1113   by (auto simp add: divide_simps)
  1114 
  1115 lemma divide_nonneg_nonpos:
  1116   "0 \<le> x \<Longrightarrow> y \<le> 0 \<Longrightarrow> x / y \<le> 0"
  1117   by (auto simp add: divide_simps)
  1118 
  1119 lemma divide_nonpos_nonneg:
  1120   "x \<le> 0 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x / y \<le> 0"
  1121   by (auto simp add: divide_simps)
  1122 
  1123 text {*Conditional Simplification Rules: No Case Splits*}
  1124 
  1125 lemma le_divide_eq_1_pos [simp]:
  1126   "0 < a \<Longrightarrow> (1 \<le> b/a) = (a \<le> b)"
  1127 by (auto simp add: le_divide_eq)
  1128 
  1129 lemma le_divide_eq_1_neg [simp]:
  1130   "a < 0 \<Longrightarrow> (1 \<le> b/a) = (b \<le> a)"
  1131 by (auto simp add: le_divide_eq)
  1132 
  1133 lemma divide_le_eq_1_pos [simp]:
  1134   "0 < a \<Longrightarrow> (b/a \<le> 1) = (b \<le> a)"
  1135 by (auto simp add: divide_le_eq)
  1136 
  1137 lemma divide_le_eq_1_neg [simp]:
  1138   "a < 0 \<Longrightarrow> (b/a \<le> 1) = (a \<le> b)"
  1139 by (auto simp add: divide_le_eq)
  1140 
  1141 lemma less_divide_eq_1_pos [simp]:
  1142   "0 < a \<Longrightarrow> (1 < b/a) = (a < b)"
  1143 by (auto simp add: less_divide_eq)
  1144 
  1145 lemma less_divide_eq_1_neg [simp]:
  1146   "a < 0 \<Longrightarrow> (1 < b/a) = (b < a)"
  1147 by (auto simp add: less_divide_eq)
  1148 
  1149 lemma divide_less_eq_1_pos [simp]:
  1150   "0 < a \<Longrightarrow> (b/a < 1) = (b < a)"
  1151 by (auto simp add: divide_less_eq)
  1152 
  1153 lemma divide_less_eq_1_neg [simp]:
  1154   "a < 0 \<Longrightarrow> b/a < 1 <-> a < b"
  1155 by (auto simp add: divide_less_eq)
  1156 
  1157 lemma eq_divide_eq_1 [simp]:
  1158   "(1 = b/a) = ((a \<noteq> 0 & a = b))"
  1159 by (auto simp add: eq_divide_eq)
  1160 
  1161 lemma divide_eq_eq_1 [simp]:
  1162   "(b/a = 1) = ((a \<noteq> 0 & a = b))"
  1163 by (auto simp add: divide_eq_eq)
  1164 
  1165 lemma abs_inverse [simp]:
  1166      "\<bar>inverse a\<bar> = 
  1167       inverse \<bar>a\<bar>"
  1168 apply (cases "a=0", simp) 
  1169 apply (simp add: nonzero_abs_inverse) 
  1170 done
  1171 
  1172 lemma abs_divide [simp]:
  1173      "\<bar>a / b\<bar> = \<bar>a\<bar> / \<bar>b\<bar>"
  1174 apply (cases "b=0", simp) 
  1175 apply (simp add: nonzero_abs_divide) 
  1176 done
  1177 
  1178 lemma abs_div_pos: "0 < y ==> 
  1179     \<bar>x\<bar> / y = \<bar>x / y\<bar>"
  1180   apply (subst abs_divide)
  1181   apply (simp add: order_less_imp_le)
  1182 done
  1183 
  1184 lemma zero_le_divide_abs_iff [simp]: "(0 \<le> a / abs b) = (0 \<le> a | b = 0)" 
  1185 by (auto simp: zero_le_divide_iff)
  1186 
  1187 lemma divide_le_0_abs_iff [simp]: "(a / abs b \<le> 0) = (a \<le> 0 | b = 0)" 
  1188 by (auto simp: divide_le_0_iff)
  1189 
  1190 lemma field_le_mult_one_interval:
  1191   assumes *: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
  1192   shows "x \<le> y"
  1193 proof (cases "0 < x")
  1194   assume "0 < x"
  1195   thus ?thesis
  1196     using dense_le_bounded[of 0 1 "y/x"] *
  1197     unfolding le_divide_eq if_P[OF `0 < x`] by simp
  1198 next
  1199   assume "\<not>0 < x" hence "x \<le> 0" by simp
  1200   obtain s::'a where s: "0 < s" "s < 1" using dense[of 0 "1\<Colon>'a"] by auto
  1201   hence "x \<le> s * x" using mult_le_cancel_right[of 1 x s] `x \<le> 0` by auto
  1202   also note *[OF s]
  1203   finally show ?thesis .
  1204 qed
  1205 
  1206 end
  1207 
  1208 code_identifier
  1209   code_module Fields \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
  1210 
  1211 end