src/HOL/Fields.thy
author hoelzl
Fri May 20 21:38:32 2011 +0200 (2011-05-20)
changeset 42904 4aedcff42de6
parent 36774 9e444b09fbef
child 44064 5bce8ff0d9ae
permissions -rw-r--r--
add divide_.._cancel, inverse_.._iff
     1 (*  Title:      HOL/Fields.thy
     2     Author:     Gertrud Bauer
     3     Author:     Steven Obua
     4     Author:     Tobias Nipkow
     5     Author:     Lawrence C Paulson
     6     Author:     Markus Wenzel
     7     Author:     Jeremy Avigad
     8 *)
     9 
    10 header {* Fields *}
    11 
    12 theory Fields
    13 imports Rings
    14 begin
    15 
    16 class field = comm_ring_1 + inverse +
    17   assumes field_inverse: "a \<noteq> 0 \<Longrightarrow> inverse a * a = 1"
    18   assumes field_divide_inverse: "a / b = a * inverse b"
    19 begin
    20 
    21 subclass division_ring
    22 proof
    23   fix a :: 'a
    24   assume "a \<noteq> 0"
    25   thus "inverse a * a = 1" by (rule field_inverse)
    26   thus "a * inverse a = 1" by (simp only: mult_commute)
    27 next
    28   fix a b :: 'a
    29   show "a / b = a * inverse b" by (rule field_divide_inverse)
    30 qed
    31 
    32 subclass idom ..
    33 
    34 text{*There is no slick version using division by zero.*}
    35 lemma inverse_add:
    36   "[| a \<noteq> 0;  b \<noteq> 0 |]
    37    ==> inverse a + inverse b = (a + b) * inverse a * inverse b"
    38 by (simp add: division_ring_inverse_add mult_ac)
    39 
    40 lemma nonzero_mult_divide_mult_cancel_left [simp, no_atp]:
    41 assumes [simp]: "b\<noteq>0" and [simp]: "c\<noteq>0" shows "(c*a)/(c*b) = a/b"
    42 proof -
    43   have "(c*a)/(c*b) = c * a * (inverse b * inverse c)"
    44     by (simp add: divide_inverse nonzero_inverse_mult_distrib)
    45   also have "... =  a * inverse b * (inverse c * c)"
    46     by (simp only: mult_ac)
    47   also have "... =  a * inverse b" by simp
    48     finally show ?thesis by (simp add: divide_inverse)
    49 qed
    50 
    51 lemma nonzero_mult_divide_mult_cancel_right [simp, no_atp]:
    52   "\<lbrakk>b \<noteq> 0; c \<noteq> 0\<rbrakk> \<Longrightarrow> (a * c) / (b * c) = a / b"
    53 by (simp add: mult_commute [of _ c])
    54 
    55 lemma times_divide_eq_left [simp]: "(b / c) * a = (b * a) / c"
    56   by (simp add: divide_inverse mult_ac)
    57 
    58 text {* These are later declared as simp rules. *}
    59 lemmas times_divide_eq [no_atp] = times_divide_eq_right times_divide_eq_left
    60 
    61 lemma add_frac_eq:
    62   assumes "y \<noteq> 0" and "z \<noteq> 0"
    63   shows "x / y + w / z = (x * z + w * y) / (y * z)"
    64 proof -
    65   have "x / y + w / z = (x * z) / (y * z) + (y * w) / (y * z)"
    66     using assms by simp
    67   also have "\<dots> = (x * z + y * w) / (y * z)"
    68     by (simp only: add_divide_distrib)
    69   finally show ?thesis
    70     by (simp only: mult_commute)
    71 qed
    72 
    73 text{*Special Cancellation Simprules for Division*}
    74 
    75 lemma nonzero_mult_divide_cancel_right [simp, no_atp]:
    76   "b \<noteq> 0 \<Longrightarrow> a * b / b = a"
    77   using nonzero_mult_divide_mult_cancel_right [of 1 b a] by simp
    78 
    79 lemma nonzero_mult_divide_cancel_left [simp, no_atp]:
    80   "a \<noteq> 0 \<Longrightarrow> a * b / a = b"
    81 using nonzero_mult_divide_mult_cancel_left [of 1 a b] by simp
    82 
    83 lemma nonzero_divide_mult_cancel_right [simp, no_atp]:
    84   "\<lbrakk>a \<noteq> 0; b \<noteq> 0\<rbrakk> \<Longrightarrow> b / (a * b) = 1 / a"
    85 using nonzero_mult_divide_mult_cancel_right [of a b 1] by simp
    86 
    87 lemma nonzero_divide_mult_cancel_left [simp, no_atp]:
    88   "\<lbrakk>a \<noteq> 0; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / (a * b) = 1 / b"
    89 using nonzero_mult_divide_mult_cancel_left [of b a 1] by simp
    90 
    91 lemma nonzero_mult_divide_mult_cancel_left2 [simp, no_atp]:
    92   "\<lbrakk>b \<noteq> 0; c \<noteq> 0\<rbrakk> \<Longrightarrow> (c * a) / (b * c) = a / b"
    93 using nonzero_mult_divide_mult_cancel_left [of b c a] by (simp add: mult_ac)
    94 
    95 lemma nonzero_mult_divide_mult_cancel_right2 [simp, no_atp]:
    96   "\<lbrakk>b \<noteq> 0; c \<noteq> 0\<rbrakk> \<Longrightarrow> (a * c) / (c * b) = a / b"
    97 using nonzero_mult_divide_mult_cancel_right [of b c a] by (simp add: mult_ac)
    98 
    99 lemma add_divide_eq_iff [field_simps]:
   100   "z \<noteq> 0 \<Longrightarrow> x + y / z = (z * x + y) / z"
   101   by (simp add: add_divide_distrib)
   102 
   103 lemma divide_add_eq_iff [field_simps]:
   104   "z \<noteq> 0 \<Longrightarrow> x / z + y = (x + z * y) / z"
   105   by (simp add: add_divide_distrib)
   106 
   107 lemma diff_divide_eq_iff [field_simps]:
   108   "z \<noteq> 0 \<Longrightarrow> x - y / z = (z * x - y) / z"
   109   by (simp add: diff_divide_distrib)
   110 
   111 lemma divide_diff_eq_iff [field_simps]:
   112   "z \<noteq> 0 \<Longrightarrow> x / z - y = (x - z * y) / z"
   113   by (simp add: diff_divide_distrib)
   114 
   115 lemma diff_frac_eq:
   116   "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> x / y - w / z = (x * z - w * y) / (y * z)"
   117   by (simp add: field_simps)
   118 
   119 lemma frac_eq_eq:
   120   "y \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> (x / y = w / z) = (x * z = w * y)"
   121   by (simp add: field_simps)
   122 
   123 end
   124 
   125 class field_inverse_zero = field +
   126   assumes field_inverse_zero: "inverse 0 = 0"
   127 begin
   128 
   129 subclass division_ring_inverse_zero proof
   130 qed (fact field_inverse_zero)
   131 
   132 text{*This version builds in division by zero while also re-orienting
   133       the right-hand side.*}
   134 lemma inverse_mult_distrib [simp]:
   135   "inverse (a * b) = inverse a * inverse b"
   136 proof cases
   137   assume "a \<noteq> 0 & b \<noteq> 0" 
   138   thus ?thesis by (simp add: nonzero_inverse_mult_distrib mult_ac)
   139 next
   140   assume "~ (a \<noteq> 0 & b \<noteq> 0)" 
   141   thus ?thesis by force
   142 qed
   143 
   144 lemma inverse_divide [simp]:
   145   "inverse (a / b) = b / a"
   146   by (simp add: divide_inverse mult_commute)
   147 
   148 
   149 text {* Calculations with fractions *}
   150 
   151 text{* There is a whole bunch of simp-rules just for class @{text
   152 field} but none for class @{text field} and @{text nonzero_divides}
   153 because the latter are covered by a simproc. *}
   154 
   155 lemma mult_divide_mult_cancel_left:
   156   "c \<noteq> 0 \<Longrightarrow> (c * a) / (c * b) = a / b"
   157 apply (cases "b = 0")
   158 apply simp_all
   159 done
   160 
   161 lemma mult_divide_mult_cancel_right:
   162   "c \<noteq> 0 \<Longrightarrow> (a * c) / (b * c) = a / b"
   163 apply (cases "b = 0")
   164 apply simp_all
   165 done
   166 
   167 lemma divide_divide_eq_right [simp, no_atp]:
   168   "a / (b / c) = (a * c) / b"
   169   by (simp add: divide_inverse mult_ac)
   170 
   171 lemma divide_divide_eq_left [simp, no_atp]:
   172   "(a / b) / c = a / (b * c)"
   173   by (simp add: divide_inverse mult_assoc)
   174 
   175 
   176 text {*Special Cancellation Simprules for Division*}
   177 
   178 lemma mult_divide_mult_cancel_left_if [simp,no_atp]:
   179   shows "(c * a) / (c * b) = (if c = 0 then 0 else a / b)"
   180   by (simp add: mult_divide_mult_cancel_left)
   181 
   182 
   183 text {* Division and Unary Minus *}
   184 
   185 lemma minus_divide_right:
   186   "- (a / b) = a / - b"
   187   by (simp add: divide_inverse)
   188 
   189 lemma divide_minus_right [simp, no_atp]:
   190   "a / - b = - (a / b)"
   191   by (simp add: divide_inverse)
   192 
   193 lemma minus_divide_divide:
   194   "(- a) / (- b) = a / b"
   195 apply (cases "b=0", simp) 
   196 apply (simp add: nonzero_minus_divide_divide) 
   197 done
   198 
   199 lemma eq_divide_eq:
   200   "a = b / c \<longleftrightarrow> (if c \<noteq> 0 then a * c = b else a = 0)"
   201   by (simp add: nonzero_eq_divide_eq)
   202 
   203 lemma divide_eq_eq:
   204   "b / c = a \<longleftrightarrow> (if c \<noteq> 0 then b = a * c else a = 0)"
   205   by (force simp add: nonzero_divide_eq_eq)
   206 
   207 lemma inverse_eq_1_iff [simp]:
   208   "inverse x = 1 \<longleftrightarrow> x = 1"
   209   by (insert inverse_eq_iff_eq [of x 1], simp) 
   210 
   211 lemma divide_eq_0_iff [simp, no_atp]:
   212   "a / b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
   213   by (simp add: divide_inverse)
   214 
   215 lemma divide_cancel_right [simp, no_atp]:
   216   "a / c = b / c \<longleftrightarrow> c = 0 \<or> a = b"
   217   apply (cases "c=0", simp)
   218   apply (simp add: divide_inverse)
   219   done
   220 
   221 lemma divide_cancel_left [simp, no_atp]:
   222   "c / a = c / b \<longleftrightarrow> c = 0 \<or> a = b" 
   223   apply (cases "c=0", simp)
   224   apply (simp add: divide_inverse)
   225   done
   226 
   227 lemma divide_eq_1_iff [simp, no_atp]:
   228   "a / b = 1 \<longleftrightarrow> b \<noteq> 0 \<and> a = b"
   229   apply (cases "b=0", simp)
   230   apply (simp add: right_inverse_eq)
   231   done
   232 
   233 lemma one_eq_divide_iff [simp, no_atp]:
   234   "1 = a / b \<longleftrightarrow> b \<noteq> 0 \<and> a = b"
   235   by (simp add: eq_commute [of 1])
   236 
   237 lemma times_divide_times_eq:
   238   "(x / y) * (z / w) = (x * z) / (y * w)"
   239   by simp
   240 
   241 lemma add_frac_num:
   242   "y \<noteq> 0 \<Longrightarrow> x / y + z = (x + z * y) / y"
   243   by (simp add: add_divide_distrib)
   244 
   245 lemma add_num_frac:
   246   "y \<noteq> 0 \<Longrightarrow> z + x / y = (x + z * y) / y"
   247   by (simp add: add_divide_distrib add.commute)
   248 
   249 end
   250 
   251 
   252 text {* Ordered Fields *}
   253 
   254 class linordered_field = field + linordered_idom
   255 begin
   256 
   257 lemma positive_imp_inverse_positive: 
   258   assumes a_gt_0: "0 < a" 
   259   shows "0 < inverse a"
   260 proof -
   261   have "0 < a * inverse a" 
   262     by (simp add: a_gt_0 [THEN less_imp_not_eq2])
   263   thus "0 < inverse a" 
   264     by (simp add: a_gt_0 [THEN less_not_sym] zero_less_mult_iff)
   265 qed
   266 
   267 lemma negative_imp_inverse_negative:
   268   "a < 0 \<Longrightarrow> inverse a < 0"
   269   by (insert positive_imp_inverse_positive [of "-a"], 
   270     simp add: nonzero_inverse_minus_eq less_imp_not_eq)
   271 
   272 lemma inverse_le_imp_le:
   273   assumes invle: "inverse a \<le> inverse b" and apos: "0 < a"
   274   shows "b \<le> a"
   275 proof (rule classical)
   276   assume "~ b \<le> a"
   277   hence "a < b"  by (simp add: linorder_not_le)
   278   hence bpos: "0 < b"  by (blast intro: apos less_trans)
   279   hence "a * inverse a \<le> a * inverse b"
   280     by (simp add: apos invle less_imp_le mult_left_mono)
   281   hence "(a * inverse a) * b \<le> (a * inverse b) * b"
   282     by (simp add: bpos less_imp_le mult_right_mono)
   283   thus "b \<le> a"  by (simp add: mult_assoc apos bpos less_imp_not_eq2)
   284 qed
   285 
   286 lemma inverse_positive_imp_positive:
   287   assumes inv_gt_0: "0 < inverse a" and nz: "a \<noteq> 0"
   288   shows "0 < a"
   289 proof -
   290   have "0 < inverse (inverse a)"
   291     using inv_gt_0 by (rule positive_imp_inverse_positive)
   292   thus "0 < a"
   293     using nz by (simp add: nonzero_inverse_inverse_eq)
   294 qed
   295 
   296 lemma inverse_negative_imp_negative:
   297   assumes inv_less_0: "inverse a < 0" and nz: "a \<noteq> 0"
   298   shows "a < 0"
   299 proof -
   300   have "inverse (inverse a) < 0"
   301     using inv_less_0 by (rule negative_imp_inverse_negative)
   302   thus "a < 0" using nz by (simp add: nonzero_inverse_inverse_eq)
   303 qed
   304 
   305 lemma linordered_field_no_lb:
   306   "\<forall>x. \<exists>y. y < x"
   307 proof
   308   fix x::'a
   309   have m1: "- (1::'a) < 0" by simp
   310   from add_strict_right_mono[OF m1, where c=x] 
   311   have "(- 1) + x < x" by simp
   312   thus "\<exists>y. y < x" by blast
   313 qed
   314 
   315 lemma linordered_field_no_ub:
   316   "\<forall> x. \<exists>y. y > x"
   317 proof
   318   fix x::'a
   319   have m1: " (1::'a) > 0" by simp
   320   from add_strict_right_mono[OF m1, where c=x] 
   321   have "1 + x > x" by simp
   322   thus "\<exists>y. y > x" by blast
   323 qed
   324 
   325 lemma less_imp_inverse_less:
   326   assumes less: "a < b" and apos:  "0 < a"
   327   shows "inverse b < inverse a"
   328 proof (rule ccontr)
   329   assume "~ inverse b < inverse a"
   330   hence "inverse a \<le> inverse b" by simp
   331   hence "~ (a < b)"
   332     by (simp add: not_less inverse_le_imp_le [OF _ apos])
   333   thus False by (rule notE [OF _ less])
   334 qed
   335 
   336 lemma inverse_less_imp_less:
   337   "inverse a < inverse b \<Longrightarrow> 0 < a \<Longrightarrow> b < a"
   338 apply (simp add: less_le [of "inverse a"] less_le [of "b"])
   339 apply (force dest!: inverse_le_imp_le nonzero_inverse_eq_imp_eq) 
   340 done
   341 
   342 text{*Both premises are essential. Consider -1 and 1.*}
   343 lemma inverse_less_iff_less [simp,no_atp]:
   344   "0 < a \<Longrightarrow> 0 < b \<Longrightarrow> inverse a < inverse b \<longleftrightarrow> b < a"
   345   by (blast intro: less_imp_inverse_less dest: inverse_less_imp_less) 
   346 
   347 lemma le_imp_inverse_le:
   348   "a \<le> b \<Longrightarrow> 0 < a \<Longrightarrow> inverse b \<le> inverse a"
   349   by (force simp add: le_less less_imp_inverse_less)
   350 
   351 lemma inverse_le_iff_le [simp,no_atp]:
   352   "0 < a \<Longrightarrow> 0 < b \<Longrightarrow> inverse a \<le> inverse b \<longleftrightarrow> b \<le> a"
   353   by (blast intro: le_imp_inverse_le dest: inverse_le_imp_le) 
   354 
   355 
   356 text{*These results refer to both operands being negative.  The opposite-sign
   357 case is trivial, since inverse preserves signs.*}
   358 lemma inverse_le_imp_le_neg:
   359   "inverse a \<le> inverse b \<Longrightarrow> b < 0 \<Longrightarrow> b \<le> a"
   360 apply (rule classical) 
   361 apply (subgoal_tac "a < 0") 
   362  prefer 2 apply force
   363 apply (insert inverse_le_imp_le [of "-b" "-a"])
   364 apply (simp add: nonzero_inverse_minus_eq) 
   365 done
   366 
   367 lemma less_imp_inverse_less_neg:
   368    "a < b \<Longrightarrow> b < 0 \<Longrightarrow> inverse b < inverse a"
   369 apply (subgoal_tac "a < 0") 
   370  prefer 2 apply (blast intro: less_trans) 
   371 apply (insert less_imp_inverse_less [of "-b" "-a"])
   372 apply (simp add: nonzero_inverse_minus_eq) 
   373 done
   374 
   375 lemma inverse_less_imp_less_neg:
   376    "inverse a < inverse b \<Longrightarrow> b < 0 \<Longrightarrow> b < a"
   377 apply (rule classical) 
   378 apply (subgoal_tac "a < 0") 
   379  prefer 2
   380  apply force
   381 apply (insert inverse_less_imp_less [of "-b" "-a"])
   382 apply (simp add: nonzero_inverse_minus_eq) 
   383 done
   384 
   385 lemma inverse_less_iff_less_neg [simp,no_atp]:
   386   "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> inverse a < inverse b \<longleftrightarrow> b < a"
   387 apply (insert inverse_less_iff_less [of "-b" "-a"])
   388 apply (simp del: inverse_less_iff_less 
   389             add: nonzero_inverse_minus_eq)
   390 done
   391 
   392 lemma le_imp_inverse_le_neg:
   393   "a \<le> b \<Longrightarrow> b < 0 ==> inverse b \<le> inverse a"
   394   by (force simp add: le_less less_imp_inverse_less_neg)
   395 
   396 lemma inverse_le_iff_le_neg [simp,no_atp]:
   397   "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> inverse a \<le> inverse b \<longleftrightarrow> b \<le> a"
   398   by (blast intro: le_imp_inverse_le_neg dest: inverse_le_imp_le_neg) 
   399 
   400 lemma one_less_inverse:
   401   "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> 1 < inverse a"
   402   using less_imp_inverse_less [of a 1, unfolded inverse_1] .
   403 
   404 lemma one_le_inverse:
   405   "0 < a \<Longrightarrow> a \<le> 1 \<Longrightarrow> 1 \<le> inverse a"
   406   using le_imp_inverse_le [of a 1, unfolded inverse_1] .
   407 
   408 lemma pos_le_divide_eq [field_simps]: "0 < c ==> (a \<le> b/c) = (a*c \<le> b)"
   409 proof -
   410   assume less: "0<c"
   411   hence "(a \<le> b/c) = (a*c \<le> (b/c)*c)"
   412     by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
   413   also have "... = (a*c \<le> b)"
   414     by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult_assoc) 
   415   finally show ?thesis .
   416 qed
   417 
   418 lemma neg_le_divide_eq [field_simps]: "c < 0 ==> (a \<le> b/c) = (b \<le> a*c)"
   419 proof -
   420   assume less: "c<0"
   421   hence "(a \<le> b/c) = ((b/c)*c \<le> a*c)"
   422     by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
   423   also have "... = (b \<le> a*c)"
   424     by (simp add: less_imp_not_eq [OF less] divide_inverse mult_assoc) 
   425   finally show ?thesis .
   426 qed
   427 
   428 lemma pos_less_divide_eq [field_simps]:
   429      "0 < c ==> (a < b/c) = (a*c < b)"
   430 proof -
   431   assume less: "0<c"
   432   hence "(a < b/c) = (a*c < (b/c)*c)"
   433     by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
   434   also have "... = (a*c < b)"
   435     by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult_assoc) 
   436   finally show ?thesis .
   437 qed
   438 
   439 lemma neg_less_divide_eq [field_simps]:
   440  "c < 0 ==> (a < b/c) = (b < a*c)"
   441 proof -
   442   assume less: "c<0"
   443   hence "(a < b/c) = ((b/c)*c < a*c)"
   444     by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
   445   also have "... = (b < a*c)"
   446     by (simp add: less_imp_not_eq [OF less] divide_inverse mult_assoc) 
   447   finally show ?thesis .
   448 qed
   449 
   450 lemma pos_divide_less_eq [field_simps]:
   451      "0 < c ==> (b/c < a) = (b < a*c)"
   452 proof -
   453   assume less: "0<c"
   454   hence "(b/c < a) = ((b/c)*c < a*c)"
   455     by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
   456   also have "... = (b < a*c)"
   457     by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult_assoc) 
   458   finally show ?thesis .
   459 qed
   460 
   461 lemma neg_divide_less_eq [field_simps]:
   462  "c < 0 ==> (b/c < a) = (a*c < b)"
   463 proof -
   464   assume less: "c<0"
   465   hence "(b/c < a) = (a*c < (b/c)*c)"
   466     by (simp add: mult_less_cancel_right_disj less_not_sym [OF less] del: times_divide_eq)
   467   also have "... = (a*c < b)"
   468     by (simp add: less_imp_not_eq [OF less] divide_inverse mult_assoc) 
   469   finally show ?thesis .
   470 qed
   471 
   472 lemma pos_divide_le_eq [field_simps]: "0 < c ==> (b/c \<le> a) = (b \<le> a*c)"
   473 proof -
   474   assume less: "0<c"
   475   hence "(b/c \<le> a) = ((b/c)*c \<le> a*c)"
   476     by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
   477   also have "... = (b \<le> a*c)"
   478     by (simp add: less_imp_not_eq2 [OF less] divide_inverse mult_assoc) 
   479   finally show ?thesis .
   480 qed
   481 
   482 lemma neg_divide_le_eq [field_simps]: "c < 0 ==> (b/c \<le> a) = (a*c \<le> b)"
   483 proof -
   484   assume less: "c<0"
   485   hence "(b/c \<le> a) = (a*c \<le> (b/c)*c)"
   486     by (simp add: mult_le_cancel_right less_not_sym [OF less] del: times_divide_eq)
   487   also have "... = (a*c \<le> b)"
   488     by (simp add: less_imp_not_eq [OF less] divide_inverse mult_assoc) 
   489   finally show ?thesis .
   490 qed
   491 
   492 text{* Lemmas @{text sign_simps} is a first attempt to automate proofs
   493 of positivity/negativity needed for @{text field_simps}. Have not added @{text
   494 sign_simps} to @{text field_simps} because the former can lead to case
   495 explosions. *}
   496 
   497 lemmas sign_simps [no_atp] = algebra_simps
   498   zero_less_mult_iff mult_less_0_iff
   499 
   500 lemmas (in -) sign_simps [no_atp] = algebra_simps
   501   zero_less_mult_iff mult_less_0_iff
   502 
   503 (* Only works once linear arithmetic is installed:
   504 text{*An example:*}
   505 lemma fixes a b c d e f :: "'a::linordered_field"
   506 shows "\<lbrakk>a>b; c<d; e<f; 0 < u \<rbrakk> \<Longrightarrow>
   507  ((a-b)*(c-d)*(e-f))/((c-d)*(e-f)*(a-b)) <
   508  ((e-f)*(a-b)*(c-d))/((e-f)*(a-b)*(c-d)) + u"
   509 apply(subgoal_tac "(c-d)*(e-f)*(a-b) > 0")
   510  prefer 2 apply(simp add:sign_simps)
   511 apply(subgoal_tac "(c-d)*(e-f)*(a-b)*u > 0")
   512  prefer 2 apply(simp add:sign_simps)
   513 apply(simp add:field_simps)
   514 done
   515 *)
   516 
   517 lemma divide_pos_pos:
   518   "0 < x ==> 0 < y ==> 0 < x / y"
   519 by(simp add:field_simps)
   520 
   521 lemma divide_nonneg_pos:
   522   "0 <= x ==> 0 < y ==> 0 <= x / y"
   523 by(simp add:field_simps)
   524 
   525 lemma divide_neg_pos:
   526   "x < 0 ==> 0 < y ==> x / y < 0"
   527 by(simp add:field_simps)
   528 
   529 lemma divide_nonpos_pos:
   530   "x <= 0 ==> 0 < y ==> x / y <= 0"
   531 by(simp add:field_simps)
   532 
   533 lemma divide_pos_neg:
   534   "0 < x ==> y < 0 ==> x / y < 0"
   535 by(simp add:field_simps)
   536 
   537 lemma divide_nonneg_neg:
   538   "0 <= x ==> y < 0 ==> x / y <= 0" 
   539 by(simp add:field_simps)
   540 
   541 lemma divide_neg_neg:
   542   "x < 0 ==> y < 0 ==> 0 < x / y"
   543 by(simp add:field_simps)
   544 
   545 lemma divide_nonpos_neg:
   546   "x <= 0 ==> y < 0 ==> 0 <= x / y"
   547 by(simp add:field_simps)
   548 
   549 lemma divide_strict_right_mono:
   550      "[|a < b; 0 < c|] ==> a / c < b / c"
   551 by (simp add: less_imp_not_eq2 divide_inverse mult_strict_right_mono 
   552               positive_imp_inverse_positive)
   553 
   554 
   555 lemma divide_strict_right_mono_neg:
   556      "[|b < a; c < 0|] ==> a / c < b / c"
   557 apply (drule divide_strict_right_mono [of _ _ "-c"], simp)
   558 apply (simp add: less_imp_not_eq nonzero_minus_divide_right [symmetric])
   559 done
   560 
   561 text{*The last premise ensures that @{term a} and @{term b} 
   562       have the same sign*}
   563 lemma divide_strict_left_mono:
   564   "[|b < a; 0 < c; 0 < a*b|] ==> c / a < c / b"
   565 by(auto simp: field_simps times_divide_eq zero_less_mult_iff mult_strict_right_mono)
   566 
   567 lemma divide_left_mono:
   568   "[|b \<le> a; 0 \<le> c; 0 < a*b|] ==> c / a \<le> c / b"
   569 by(auto simp: field_simps times_divide_eq zero_less_mult_iff mult_right_mono)
   570 
   571 lemma divide_strict_left_mono_neg:
   572   "[|a < b; c < 0; 0 < a*b|] ==> c / a < c / b"
   573 by(auto simp: field_simps times_divide_eq zero_less_mult_iff mult_strict_right_mono_neg)
   574 
   575 lemma mult_imp_div_pos_le: "0 < y ==> x <= z * y ==>
   576     x / y <= z"
   577 by (subst pos_divide_le_eq, assumption+)
   578 
   579 lemma mult_imp_le_div_pos: "0 < y ==> z * y <= x ==>
   580     z <= x / y"
   581 by(simp add:field_simps)
   582 
   583 lemma mult_imp_div_pos_less: "0 < y ==> x < z * y ==>
   584     x / y < z"
   585 by(simp add:field_simps)
   586 
   587 lemma mult_imp_less_div_pos: "0 < y ==> z * y < x ==>
   588     z < x / y"
   589 by(simp add:field_simps)
   590 
   591 lemma frac_le: "0 <= x ==> 
   592     x <= y ==> 0 < w ==> w <= z  ==> x / z <= y / w"
   593   apply (rule mult_imp_div_pos_le)
   594   apply simp
   595   apply (subst times_divide_eq_left)
   596   apply (rule mult_imp_le_div_pos, assumption)
   597   apply (rule mult_mono)
   598   apply simp_all
   599 done
   600 
   601 lemma frac_less: "0 <= x ==> 
   602     x < y ==> 0 < w ==> w <= z  ==> x / z < y / w"
   603   apply (rule mult_imp_div_pos_less)
   604   apply simp
   605   apply (subst times_divide_eq_left)
   606   apply (rule mult_imp_less_div_pos, assumption)
   607   apply (erule mult_less_le_imp_less)
   608   apply simp_all
   609 done
   610 
   611 lemma frac_less2: "0 < x ==> 
   612     x <= y ==> 0 < w ==> w < z  ==> x / z < y / w"
   613   apply (rule mult_imp_div_pos_less)
   614   apply simp_all
   615   apply (rule mult_imp_less_div_pos, assumption)
   616   apply (erule mult_le_less_imp_less)
   617   apply simp_all
   618 done
   619 
   620 text{*It's not obvious whether these should be simprules or not. 
   621   Their effect is to gather terms into one big fraction, like
   622   a*b*c / x*y*z. The rationale for that is unclear, but many proofs 
   623   seem to need them.*}
   624 
   625 lemma less_half_sum: "a < b ==> a < (a+b) / (1+1)"
   626 by (simp add: field_simps zero_less_two)
   627 
   628 lemma gt_half_sum: "a < b ==> (a+b)/(1+1) < b"
   629 by (simp add: field_simps zero_less_two)
   630 
   631 subclass dense_linorder
   632 proof
   633   fix x y :: 'a
   634   from less_add_one show "\<exists>y. x < y" .. 
   635   from less_add_one have "x + (- 1) < (x + 1) + (- 1)" by (rule add_strict_right_mono)
   636   then have "x - 1 < x + 1 - 1" by (simp only: diff_minus [symmetric])
   637   then have "x - 1 < x" by (simp add: algebra_simps)
   638   then show "\<exists>y. y < x" ..
   639   show "x < y \<Longrightarrow> \<exists>z>x. z < y" by (blast intro!: less_half_sum gt_half_sum)
   640 qed
   641 
   642 lemma nonzero_abs_inverse:
   643      "a \<noteq> 0 ==> \<bar>inverse a\<bar> = inverse \<bar>a\<bar>"
   644 apply (auto simp add: neq_iff abs_if nonzero_inverse_minus_eq 
   645                       negative_imp_inverse_negative)
   646 apply (blast intro: positive_imp_inverse_positive elim: less_asym) 
   647 done
   648 
   649 lemma nonzero_abs_divide:
   650      "b \<noteq> 0 ==> \<bar>a / b\<bar> = \<bar>a\<bar> / \<bar>b\<bar>"
   651   by (simp add: divide_inverse abs_mult nonzero_abs_inverse) 
   652 
   653 lemma field_le_epsilon:
   654   assumes e: "\<And>e. 0 < e \<Longrightarrow> x \<le> y + e"
   655   shows "x \<le> y"
   656 proof (rule dense_le)
   657   fix t assume "t < x"
   658   hence "0 < x - t" by (simp add: less_diff_eq)
   659   from e [OF this] have "x + 0 \<le> x + (y - t)" by (simp add: algebra_simps)
   660   then have "0 \<le> y - t" by (simp only: add_le_cancel_left)
   661   then show "t \<le> y" by (simp add: algebra_simps)
   662 qed
   663 
   664 end
   665 
   666 class linordered_field_inverse_zero = linordered_field + field_inverse_zero
   667 begin
   668 
   669 lemma le_divide_eq:
   670   "(a \<le> b/c) = 
   671    (if 0 < c then a*c \<le> b
   672              else if c < 0 then b \<le> a*c
   673              else  a \<le> 0)"
   674 apply (cases "c=0", simp) 
   675 apply (force simp add: pos_le_divide_eq neg_le_divide_eq linorder_neq_iff) 
   676 done
   677 
   678 lemma inverse_positive_iff_positive [simp]:
   679   "(0 < inverse a) = (0 < a)"
   680 apply (cases "a = 0", simp)
   681 apply (blast intro: inverse_positive_imp_positive positive_imp_inverse_positive)
   682 done
   683 
   684 lemma inverse_negative_iff_negative [simp]:
   685   "(inverse a < 0) = (a < 0)"
   686 apply (cases "a = 0", simp)
   687 apply (blast intro: inverse_negative_imp_negative negative_imp_inverse_negative)
   688 done
   689 
   690 lemma inverse_nonnegative_iff_nonnegative [simp]:
   691   "0 \<le> inverse a \<longleftrightarrow> 0 \<le> a"
   692   by (simp add: not_less [symmetric])
   693 
   694 lemma inverse_nonpositive_iff_nonpositive [simp]:
   695   "inverse a \<le> 0 \<longleftrightarrow> a \<le> 0"
   696   by (simp add: not_less [symmetric])
   697 
   698 lemma one_less_inverse_iff:
   699   "1 < inverse x \<longleftrightarrow> 0 < x \<and> x < 1"
   700 proof cases
   701   assume "0 < x"
   702     with inverse_less_iff_less [OF zero_less_one, of x]
   703     show ?thesis by simp
   704 next
   705   assume notless: "~ (0 < x)"
   706   have "~ (1 < inverse x)"
   707   proof
   708     assume "1 < inverse x"
   709     also with notless have "... \<le> 0" by simp
   710     also have "... < 1" by (rule zero_less_one) 
   711     finally show False by auto
   712   qed
   713   with notless show ?thesis by simp
   714 qed
   715 
   716 lemma one_le_inverse_iff:
   717   "1 \<le> inverse x \<longleftrightarrow> 0 < x \<and> x \<le> 1"
   718 proof (cases "x = 1")
   719   case True then show ?thesis by simp
   720 next
   721   case False then have "inverse x \<noteq> 1" by simp
   722   then have "1 \<noteq> inverse x" by blast
   723   then have "1 \<le> inverse x \<longleftrightarrow> 1 < inverse x" by (simp add: le_less)
   724   with False show ?thesis by (auto simp add: one_less_inverse_iff)
   725 qed
   726 
   727 lemma inverse_less_1_iff:
   728   "inverse x < 1 \<longleftrightarrow> x \<le> 0 \<or> 1 < x"
   729   by (simp add: not_le [symmetric] one_le_inverse_iff) 
   730 
   731 lemma inverse_le_1_iff:
   732   "inverse x \<le> 1 \<longleftrightarrow> x \<le> 0 \<or> 1 \<le> x"
   733   by (simp add: not_less [symmetric] one_less_inverse_iff) 
   734 
   735 lemma divide_le_eq:
   736   "(b/c \<le> a) = 
   737    (if 0 < c then b \<le> a*c
   738              else if c < 0 then a*c \<le> b
   739              else 0 \<le> a)"
   740 apply (cases "c=0", simp) 
   741 apply (force simp add: pos_divide_le_eq neg_divide_le_eq) 
   742 done
   743 
   744 lemma less_divide_eq:
   745   "(a < b/c) = 
   746    (if 0 < c then a*c < b
   747              else if c < 0 then b < a*c
   748              else  a < 0)"
   749 apply (cases "c=0", simp) 
   750 apply (force simp add: pos_less_divide_eq neg_less_divide_eq) 
   751 done
   752 
   753 lemma divide_less_eq:
   754   "(b/c < a) = 
   755    (if 0 < c then b < a*c
   756              else if c < 0 then a*c < b
   757              else 0 < a)"
   758 apply (cases "c=0", simp) 
   759 apply (force simp add: pos_divide_less_eq neg_divide_less_eq)
   760 done
   761 
   762 text {*Division and Signs*}
   763 
   764 lemma zero_less_divide_iff:
   765      "(0 < a/b) = (0 < a & 0 < b | a < 0 & b < 0)"
   766 by (simp add: divide_inverse zero_less_mult_iff)
   767 
   768 lemma divide_less_0_iff:
   769      "(a/b < 0) = 
   770       (0 < a & b < 0 | a < 0 & 0 < b)"
   771 by (simp add: divide_inverse mult_less_0_iff)
   772 
   773 lemma zero_le_divide_iff:
   774      "(0 \<le> a/b) =
   775       (0 \<le> a & 0 \<le> b | a \<le> 0 & b \<le> 0)"
   776 by (simp add: divide_inverse zero_le_mult_iff)
   777 
   778 lemma divide_le_0_iff:
   779      "(a/b \<le> 0) =
   780       (0 \<le> a & b \<le> 0 | a \<le> 0 & 0 \<le> b)"
   781 by (simp add: divide_inverse mult_le_0_iff)
   782 
   783 text {* Division and the Number One *}
   784 
   785 text{*Simplify expressions equated with 1*}
   786 
   787 lemma zero_eq_1_divide_iff [simp,no_atp]:
   788      "(0 = 1/a) = (a = 0)"
   789 apply (cases "a=0", simp)
   790 apply (auto simp add: nonzero_eq_divide_eq)
   791 done
   792 
   793 lemma one_divide_eq_0_iff [simp,no_atp]:
   794      "(1/a = 0) = (a = 0)"
   795 apply (cases "a=0", simp)
   796 apply (insert zero_neq_one [THEN not_sym])
   797 apply (auto simp add: nonzero_divide_eq_eq)
   798 done
   799 
   800 text{*Simplify expressions such as @{text "0 < 1/x"} to @{text "0 < x"}*}
   801 
   802 lemma zero_le_divide_1_iff [simp, no_atp]:
   803   "0 \<le> 1 / a \<longleftrightarrow> 0 \<le> a"
   804   by (simp add: zero_le_divide_iff)
   805 
   806 lemma zero_less_divide_1_iff [simp, no_atp]:
   807   "0 < 1 / a \<longleftrightarrow> 0 < a"
   808   by (simp add: zero_less_divide_iff)
   809 
   810 lemma divide_le_0_1_iff [simp, no_atp]:
   811   "1 / a \<le> 0 \<longleftrightarrow> a \<le> 0"
   812   by (simp add: divide_le_0_iff)
   813 
   814 lemma divide_less_0_1_iff [simp, no_atp]:
   815   "1 / a < 0 \<longleftrightarrow> a < 0"
   816   by (simp add: divide_less_0_iff)
   817 
   818 lemma divide_right_mono:
   819      "[|a \<le> b; 0 \<le> c|] ==> a/c \<le> b/c"
   820 by (force simp add: divide_strict_right_mono le_less)
   821 
   822 lemma divide_right_mono_neg: "a <= b 
   823     ==> c <= 0 ==> b / c <= a / c"
   824 apply (drule divide_right_mono [of _ _ "- c"])
   825 apply auto
   826 done
   827 
   828 lemma divide_left_mono_neg: "a <= b 
   829     ==> c <= 0 ==> 0 < a * b ==> c / a <= c / b"
   830   apply (drule divide_left_mono [of _ _ "- c"])
   831   apply (auto simp add: mult_commute)
   832 done
   833 
   834 lemma inverse_le_iff:
   835   "inverse a \<le> inverse b \<longleftrightarrow> (0 < a * b \<longrightarrow> b \<le> a) \<and> (a * b \<le> 0 \<longrightarrow> a \<le> b)"
   836 proof -
   837   { assume "a < 0"
   838     then have "inverse a < 0" by simp
   839     moreover assume "0 < b"
   840     then have "0 < inverse b" by simp
   841     ultimately have "inverse a < inverse b" by (rule less_trans)
   842     then have "inverse a \<le> inverse b" by simp }
   843   moreover
   844   { assume "b < 0"
   845     then have "inverse b < 0" by simp
   846     moreover assume "0 < a"
   847     then have "0 < inverse a" by simp
   848     ultimately have "inverse b < inverse a" by (rule less_trans)
   849     then have "\<not> inverse a \<le> inverse b" by simp }
   850   ultimately show ?thesis
   851     by (cases a 0 b 0 rule: linorder_cases[case_product linorder_cases])
   852        (auto simp: not_less zero_less_mult_iff mult_le_0_iff)
   853 qed
   854 
   855 lemma inverse_less_iff:
   856   "inverse a < inverse b \<longleftrightarrow> (0 < a * b \<longrightarrow> b < a) \<and> (a * b \<le> 0 \<longrightarrow> a < b)"
   857   by (subst less_le) (auto simp: inverse_le_iff)
   858 
   859 lemma divide_le_cancel:
   860   "a / c \<le> b / c \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
   861   by (simp add: divide_inverse mult_le_cancel_right)
   862 
   863 lemma divide_less_cancel:
   864   "a / c < b / c \<longleftrightarrow> (0 < c \<longrightarrow> a < b) \<and> (c < 0 \<longrightarrow> b < a) \<and> c \<noteq> 0"
   865   by (auto simp add: divide_inverse mult_less_cancel_right)
   866 
   867 text{*Simplify quotients that are compared with the value 1.*}
   868 
   869 lemma le_divide_eq_1 [no_atp]:
   870   "(1 \<le> b / a) = ((0 < a & a \<le> b) | (a < 0 & b \<le> a))"
   871 by (auto simp add: le_divide_eq)
   872 
   873 lemma divide_le_eq_1 [no_atp]:
   874   "(b / a \<le> 1) = ((0 < a & b \<le> a) | (a < 0 & a \<le> b) | a=0)"
   875 by (auto simp add: divide_le_eq)
   876 
   877 lemma less_divide_eq_1 [no_atp]:
   878   "(1 < b / a) = ((0 < a & a < b) | (a < 0 & b < a))"
   879 by (auto simp add: less_divide_eq)
   880 
   881 lemma divide_less_eq_1 [no_atp]:
   882   "(b / a < 1) = ((0 < a & b < a) | (a < 0 & a < b) | a=0)"
   883 by (auto simp add: divide_less_eq)
   884 
   885 
   886 text {*Conditional Simplification Rules: No Case Splits*}
   887 
   888 lemma le_divide_eq_1_pos [simp,no_atp]:
   889   "0 < a \<Longrightarrow> (1 \<le> b/a) = (a \<le> b)"
   890 by (auto simp add: le_divide_eq)
   891 
   892 lemma le_divide_eq_1_neg [simp,no_atp]:
   893   "a < 0 \<Longrightarrow> (1 \<le> b/a) = (b \<le> a)"
   894 by (auto simp add: le_divide_eq)
   895 
   896 lemma divide_le_eq_1_pos [simp,no_atp]:
   897   "0 < a \<Longrightarrow> (b/a \<le> 1) = (b \<le> a)"
   898 by (auto simp add: divide_le_eq)
   899 
   900 lemma divide_le_eq_1_neg [simp,no_atp]:
   901   "a < 0 \<Longrightarrow> (b/a \<le> 1) = (a \<le> b)"
   902 by (auto simp add: divide_le_eq)
   903 
   904 lemma less_divide_eq_1_pos [simp,no_atp]:
   905   "0 < a \<Longrightarrow> (1 < b/a) = (a < b)"
   906 by (auto simp add: less_divide_eq)
   907 
   908 lemma less_divide_eq_1_neg [simp,no_atp]:
   909   "a < 0 \<Longrightarrow> (1 < b/a) = (b < a)"
   910 by (auto simp add: less_divide_eq)
   911 
   912 lemma divide_less_eq_1_pos [simp,no_atp]:
   913   "0 < a \<Longrightarrow> (b/a < 1) = (b < a)"
   914 by (auto simp add: divide_less_eq)
   915 
   916 lemma divide_less_eq_1_neg [simp,no_atp]:
   917   "a < 0 \<Longrightarrow> b/a < 1 <-> a < b"
   918 by (auto simp add: divide_less_eq)
   919 
   920 lemma eq_divide_eq_1 [simp,no_atp]:
   921   "(1 = b/a) = ((a \<noteq> 0 & a = b))"
   922 by (auto simp add: eq_divide_eq)
   923 
   924 lemma divide_eq_eq_1 [simp,no_atp]:
   925   "(b/a = 1) = ((a \<noteq> 0 & a = b))"
   926 by (auto simp add: divide_eq_eq)
   927 
   928 lemma abs_inverse [simp]:
   929      "\<bar>inverse a\<bar> = 
   930       inverse \<bar>a\<bar>"
   931 apply (cases "a=0", simp) 
   932 apply (simp add: nonzero_abs_inverse) 
   933 done
   934 
   935 lemma abs_divide [simp]:
   936      "\<bar>a / b\<bar> = \<bar>a\<bar> / \<bar>b\<bar>"
   937 apply (cases "b=0", simp) 
   938 apply (simp add: nonzero_abs_divide) 
   939 done
   940 
   941 lemma abs_div_pos: "0 < y ==> 
   942     \<bar>x\<bar> / y = \<bar>x / y\<bar>"
   943   apply (subst abs_divide)
   944   apply (simp add: order_less_imp_le)
   945 done
   946 
   947 lemma field_le_mult_one_interval:
   948   assumes *: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
   949   shows "x \<le> y"
   950 proof (cases "0 < x")
   951   assume "0 < x"
   952   thus ?thesis
   953     using dense_le_bounded[of 0 1 "y/x"] *
   954     unfolding le_divide_eq if_P[OF `0 < x`] by simp
   955 next
   956   assume "\<not>0 < x" hence "x \<le> 0" by simp
   957   obtain s::'a where s: "0 < s" "s < 1" using dense[of 0 "1\<Colon>'a"] by auto
   958   hence "x \<le> s * x" using mult_le_cancel_right[of 1 x s] `x \<le> 0` by auto
   959   also note *[OF s]
   960   finally show ?thesis .
   961 qed
   962 
   963 end
   964 
   965 code_modulename SML
   966   Fields Arith
   967 
   968 code_modulename OCaml
   969   Fields Arith
   970 
   971 code_modulename Haskell
   972   Fields Arith
   973 
   974 end