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