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