src/HOL/Rings.thy
author haftmann
Thu Feb 19 11:53:36 2015 +0100 (2015-02-19)
changeset 59557 ebd8ecacfba6
parent 59555 05573e5504a9
child 59816 034b13f4efae
permissions -rw-r--r--
establish unique preferred fact names
     1 (*  Title:      HOL/Rings.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 {* Rings *}
    11 
    12 theory Rings
    13 imports Groups
    14 begin
    15 
    16 class semiring = ab_semigroup_add + semigroup_mult +
    17   assumes distrib_right[algebra_simps]: "(a + b) * c = a * c + b * c"
    18   assumes distrib_left[algebra_simps]: "a * (b + c) = a * b + a * c"
    19 begin
    20 
    21 text{*For the @{text combine_numerals} simproc*}
    22 lemma combine_common_factor:
    23   "a * e + (b * e + c) = (a + b) * e + c"
    24 by (simp add: distrib_right ac_simps)
    25 
    26 end
    27 
    28 class mult_zero = times + zero +
    29   assumes mult_zero_left [simp]: "0 * a = 0"
    30   assumes mult_zero_right [simp]: "a * 0 = 0"
    31 begin
    32 
    33 lemma mult_not_zero:
    34   "a * b \<noteq> 0 \<Longrightarrow> a \<noteq> 0 \<and> b \<noteq> 0"
    35   by auto
    36 
    37 end
    38 
    39 class semiring_0 = semiring + comm_monoid_add + mult_zero
    40 
    41 class semiring_0_cancel = semiring + cancel_comm_monoid_add
    42 begin
    43 
    44 subclass semiring_0
    45 proof
    46   fix a :: 'a
    47   have "0 * a + 0 * a = 0 * a + 0" by (simp add: distrib_right [symmetric])
    48   thus "0 * a = 0" by (simp only: add_left_cancel)
    49 next
    50   fix a :: 'a
    51   have "a * 0 + a * 0 = a * 0 + 0" by (simp add: distrib_left [symmetric])
    52   thus "a * 0 = 0" by (simp only: add_left_cancel)
    53 qed
    54 
    55 end
    56 
    57 class comm_semiring = ab_semigroup_add + ab_semigroup_mult +
    58   assumes distrib: "(a + b) * c = a * c + b * c"
    59 begin
    60 
    61 subclass semiring
    62 proof
    63   fix a b c :: 'a
    64   show "(a + b) * c = a * c + b * c" by (simp add: distrib)
    65   have "a * (b + c) = (b + c) * a" by (simp add: ac_simps)
    66   also have "... = b * a + c * a" by (simp only: distrib)
    67   also have "... = a * b + a * c" by (simp add: ac_simps)
    68   finally show "a * (b + c) = a * b + a * c" by blast
    69 qed
    70 
    71 end
    72 
    73 class comm_semiring_0 = comm_semiring + comm_monoid_add + mult_zero
    74 begin
    75 
    76 subclass semiring_0 ..
    77 
    78 end
    79 
    80 class comm_semiring_0_cancel = comm_semiring + cancel_comm_monoid_add
    81 begin
    82 
    83 subclass semiring_0_cancel ..
    84 
    85 subclass comm_semiring_0 ..
    86 
    87 end
    88 
    89 class zero_neq_one = zero + one +
    90   assumes zero_neq_one [simp]: "0 \<noteq> 1"
    91 begin
    92 
    93 lemma one_neq_zero [simp]: "1 \<noteq> 0"
    94 by (rule not_sym) (rule zero_neq_one)
    95 
    96 definition of_bool :: "bool \<Rightarrow> 'a"
    97 where
    98   "of_bool p = (if p then 1 else 0)" 
    99 
   100 lemma of_bool_eq [simp, code]:
   101   "of_bool False = 0"
   102   "of_bool True = 1"
   103   by (simp_all add: of_bool_def)
   104 
   105 lemma of_bool_eq_iff:
   106   "of_bool p = of_bool q \<longleftrightarrow> p = q"
   107   by (simp add: of_bool_def)
   108 
   109 lemma split_of_bool [split]:
   110   "P (of_bool p) \<longleftrightarrow> (p \<longrightarrow> P 1) \<and> (\<not> p \<longrightarrow> P 0)"
   111   by (cases p) simp_all
   112 
   113 lemma split_of_bool_asm:
   114   "P (of_bool p) \<longleftrightarrow> \<not> (p \<and> \<not> P 1 \<or> \<not> p \<and> \<not> P 0)"
   115   by (cases p) simp_all
   116   
   117 end  
   118 
   119 class semiring_1 = zero_neq_one + semiring_0 + monoid_mult
   120 
   121 text {* Abstract divisibility *}
   122 
   123 class dvd = times
   124 begin
   125 
   126 definition dvd :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infix "dvd" 50) where
   127   "b dvd a \<longleftrightarrow> (\<exists>k. a = b * k)"
   128 
   129 lemma dvdI [intro?]: "a = b * k \<Longrightarrow> b dvd a"
   130   unfolding dvd_def ..
   131 
   132 lemma dvdE [elim?]: "b dvd a \<Longrightarrow> (\<And>k. a = b * k \<Longrightarrow> P) \<Longrightarrow> P"
   133   unfolding dvd_def by blast 
   134 
   135 end
   136 
   137 context comm_monoid_mult
   138 begin
   139 
   140 subclass dvd .
   141 
   142 lemma dvd_refl [simp]:
   143   "a dvd a"
   144 proof
   145   show "a = a * 1" by simp
   146 qed
   147 
   148 lemma dvd_trans:
   149   assumes "a dvd b" and "b dvd c"
   150   shows "a dvd c"
   151 proof -
   152   from assms obtain v where "b = a * v" by (auto elim!: dvdE)
   153   moreover from assms obtain w where "c = b * w" by (auto elim!: dvdE)
   154   ultimately have "c = a * (v * w)" by (simp add: mult.assoc)
   155   then show ?thesis ..
   156 qed
   157 
   158 lemma one_dvd [simp]:
   159   "1 dvd a"
   160   by (auto intro!: dvdI)
   161 
   162 lemma dvd_mult [simp]:
   163   "a dvd c \<Longrightarrow> a dvd (b * c)"
   164   by (auto intro!: mult.left_commute dvdI elim!: dvdE)
   165 
   166 lemma dvd_mult2 [simp]:
   167   "a dvd b \<Longrightarrow> a dvd (b * c)"
   168   using dvd_mult [of a b c] by (simp add: ac_simps) 
   169 
   170 lemma dvd_triv_right [simp]:
   171   "a dvd b * a"
   172   by (rule dvd_mult) (rule dvd_refl)
   173 
   174 lemma dvd_triv_left [simp]:
   175   "a dvd a * b"
   176   by (rule dvd_mult2) (rule dvd_refl)
   177 
   178 lemma mult_dvd_mono:
   179   assumes "a dvd b"
   180     and "c dvd d"
   181   shows "a * c dvd b * d"
   182 proof -
   183   from `a dvd b` obtain b' where "b = a * b'" ..
   184   moreover from `c dvd d` obtain d' where "d = c * d'" ..
   185   ultimately have "b * d = (a * c) * (b' * d')" by (simp add: ac_simps)
   186   then show ?thesis ..
   187 qed
   188 
   189 lemma dvd_mult_left:
   190   "a * b dvd c \<Longrightarrow> a dvd c"
   191   by (simp add: dvd_def mult.assoc) blast
   192 
   193 lemma dvd_mult_right:
   194   "a * b dvd c \<Longrightarrow> b dvd c"
   195   using dvd_mult_left [of b a c] by (simp add: ac_simps)
   196   
   197 end
   198 
   199 class comm_semiring_1 = zero_neq_one + comm_semiring_0 + comm_monoid_mult
   200   (*previously almost_semiring*)
   201 begin
   202 
   203 subclass semiring_1 ..
   204 
   205 lemma dvd_0_left_iff [simp]:
   206   "0 dvd a \<longleftrightarrow> a = 0"
   207   by (auto intro: dvd_refl elim!: dvdE)
   208 
   209 lemma dvd_0_right [iff]:
   210   "a dvd 0"
   211 proof
   212   show "0 = a * 0" by simp
   213 qed
   214 
   215 lemma dvd_0_left:
   216   "0 dvd a \<Longrightarrow> a = 0"
   217   by simp
   218 
   219 lemma dvd_add [simp]:
   220   assumes "a dvd b" and "a dvd c"
   221   shows "a dvd (b + c)"
   222 proof -
   223   from `a dvd b` obtain b' where "b = a * b'" ..
   224   moreover from `a dvd c` obtain c' where "c = a * c'" ..
   225   ultimately have "b + c = a * (b' + c')" by (simp add: distrib_left)
   226   then show ?thesis ..
   227 qed
   228 
   229 end
   230 
   231 class semiring_dvd = comm_semiring_1 +
   232   assumes dvd_add_times_triv_left_iff [simp]: "a dvd c * a + b \<longleftrightarrow> a dvd b"
   233   assumes dvd_addD: "a dvd b + c \<Longrightarrow> a dvd b \<Longrightarrow> a dvd c"
   234 begin
   235 
   236 lemma dvd_add_times_triv_right_iff [simp]:
   237   "a dvd b + c * a \<longleftrightarrow> a dvd b"
   238   using dvd_add_times_triv_left_iff [of a c b] by (simp add: ac_simps)
   239 
   240 lemma dvd_add_triv_left_iff [simp]:
   241   "a dvd a + b \<longleftrightarrow> a dvd b"
   242   using dvd_add_times_triv_left_iff [of a 1 b] by simp
   243 
   244 lemma dvd_add_triv_right_iff [simp]:
   245   "a dvd b + a \<longleftrightarrow> a dvd b"
   246   using dvd_add_times_triv_right_iff [of a b 1] by simp
   247 
   248 lemma dvd_add_right_iff:
   249   assumes "a dvd b"
   250   shows "a dvd b + c \<longleftrightarrow> a dvd c"
   251   using assms by (auto dest: dvd_addD)
   252 
   253 lemma dvd_add_left_iff:
   254   assumes "a dvd c"
   255   shows "a dvd b + c \<longleftrightarrow> a dvd b"
   256   using assms dvd_add_right_iff [of a c b] by (simp add: ac_simps)
   257 
   258 end
   259 
   260 class no_zero_divisors = zero + times +
   261   assumes no_zero_divisors: "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a * b \<noteq> 0"
   262 begin
   263 
   264 lemma divisors_zero:
   265   assumes "a * b = 0"
   266   shows "a = 0 \<or> b = 0"
   267 proof (rule classical)
   268   assume "\<not> (a = 0 \<or> b = 0)"
   269   then have "a \<noteq> 0" and "b \<noteq> 0" by auto
   270   with no_zero_divisors have "a * b \<noteq> 0" by blast
   271   with assms show ?thesis by simp
   272 qed
   273 
   274 end
   275 
   276 class semiring_1_cancel = semiring + cancel_comm_monoid_add
   277   + zero_neq_one + monoid_mult
   278 begin
   279 
   280 subclass semiring_0_cancel ..
   281 
   282 subclass semiring_1 ..
   283 
   284 end
   285 
   286 class comm_semiring_1_cancel = comm_semiring + cancel_comm_monoid_add
   287   + zero_neq_one + comm_monoid_mult
   288 begin
   289 
   290 subclass semiring_1_cancel ..
   291 subclass comm_semiring_0_cancel ..
   292 subclass comm_semiring_1 ..
   293 
   294 end
   295 
   296 class ring = semiring + ab_group_add
   297 begin
   298 
   299 subclass semiring_0_cancel ..
   300 
   301 text {* Distribution rules *}
   302 
   303 lemma minus_mult_left: "- (a * b) = - a * b"
   304 by (rule minus_unique) (simp add: distrib_right [symmetric]) 
   305 
   306 lemma minus_mult_right: "- (a * b) = a * - b"
   307 by (rule minus_unique) (simp add: distrib_left [symmetric]) 
   308 
   309 text{*Extract signs from products*}
   310 lemmas mult_minus_left [simp] = minus_mult_left [symmetric]
   311 lemmas mult_minus_right [simp] = minus_mult_right [symmetric]
   312 
   313 lemma minus_mult_minus [simp]: "- a * - b = a * b"
   314 by simp
   315 
   316 lemma minus_mult_commute: "- a * b = a * - b"
   317 by simp
   318 
   319 lemma right_diff_distrib [algebra_simps]:
   320   "a * (b - c) = a * b - a * c"
   321   using distrib_left [of a b "-c "] by simp
   322 
   323 lemma left_diff_distrib [algebra_simps]:
   324   "(a - b) * c = a * c - b * c"
   325   using distrib_right [of a "- b" c] by simp
   326 
   327 lemmas ring_distribs =
   328   distrib_left distrib_right left_diff_distrib right_diff_distrib
   329 
   330 lemma eq_add_iff1:
   331   "a * e + c = b * e + d \<longleftrightarrow> (a - b) * e + c = d"
   332 by (simp add: algebra_simps)
   333 
   334 lemma eq_add_iff2:
   335   "a * e + c = b * e + d \<longleftrightarrow> c = (b - a) * e + d"
   336 by (simp add: algebra_simps)
   337 
   338 end
   339 
   340 lemmas ring_distribs =
   341   distrib_left distrib_right left_diff_distrib right_diff_distrib
   342 
   343 class comm_ring = comm_semiring + ab_group_add
   344 begin
   345 
   346 subclass ring ..
   347 subclass comm_semiring_0_cancel ..
   348 
   349 lemma square_diff_square_factored:
   350   "x * x - y * y = (x + y) * (x - y)"
   351   by (simp add: algebra_simps)
   352 
   353 end
   354 
   355 class ring_1 = ring + zero_neq_one + monoid_mult
   356 begin
   357 
   358 subclass semiring_1_cancel ..
   359 
   360 lemma square_diff_one_factored:
   361   "x * x - 1 = (x + 1) * (x - 1)"
   362   by (simp add: algebra_simps)
   363 
   364 end
   365 
   366 class comm_ring_1 = comm_ring + zero_neq_one + comm_monoid_mult
   367   (*previously ring*)
   368 begin
   369 
   370 subclass ring_1 ..
   371 subclass comm_semiring_1_cancel ..
   372 
   373 subclass semiring_dvd
   374 proof
   375   fix a b c
   376   show "a dvd c * a + b \<longleftrightarrow> a dvd b" (is "?P \<longleftrightarrow> ?Q")
   377   proof
   378     assume ?Q then show ?P by simp
   379   next
   380     assume ?P then obtain d where "c * a + b = a * d" ..
   381     then have "b = a * (d - c)" by (simp add: algebra_simps)
   382     then show ?Q ..
   383   qed
   384   assume "a dvd b + c" and "a dvd b"
   385   show "a dvd c"
   386   proof -
   387     from `a dvd b` obtain d where "b = a * d" ..
   388     moreover from `a dvd b + c` obtain e where "b + c = a * e" ..
   389     ultimately have "a * d + c = a * e" by simp
   390     then have "c = a * (e - d)" by (simp add: algebra_simps)
   391     then show "a dvd c" ..
   392   qed
   393 qed
   394 
   395 lemma dvd_minus_iff [simp]: "x dvd - y \<longleftrightarrow> x dvd y"
   396 proof
   397   assume "x dvd - y"
   398   then have "x dvd - 1 * - y" by (rule dvd_mult)
   399   then show "x dvd y" by simp
   400 next
   401   assume "x dvd y"
   402   then have "x dvd - 1 * y" by (rule dvd_mult)
   403   then show "x dvd - y" by simp
   404 qed
   405 
   406 lemma minus_dvd_iff [simp]: "- x dvd y \<longleftrightarrow> x dvd y"
   407 proof
   408   assume "- x dvd y"
   409   then obtain k where "y = - x * k" ..
   410   then have "y = x * - k" by simp
   411   then show "x dvd y" ..
   412 next
   413   assume "x dvd y"
   414   then obtain k where "y = x * k" ..
   415   then have "y = - x * - k" by simp
   416   then show "- x dvd y" ..
   417 qed
   418 
   419 lemma dvd_diff [simp]:
   420   "x dvd y \<Longrightarrow> x dvd z \<Longrightarrow> x dvd (y - z)"
   421   using dvd_add [of x y "- z"] by simp
   422 
   423 end
   424 
   425 class semiring_no_zero_divisors = semiring_0 + no_zero_divisors
   426 begin
   427 
   428 lemma mult_eq_0_iff [simp]:
   429   shows "a * b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
   430 proof (cases "a = 0 \<or> b = 0")
   431   case False then have "a \<noteq> 0" and "b \<noteq> 0" by auto
   432     then show ?thesis using no_zero_divisors by simp
   433 next
   434   case True then show ?thesis by auto
   435 qed
   436 
   437 end
   438 
   439 class ring_no_zero_divisors = ring + semiring_no_zero_divisors
   440 begin
   441 
   442 text{*Cancellation of equalities with a common factor*}
   443 lemma mult_cancel_right [simp]:
   444   "a * c = b * c \<longleftrightarrow> c = 0 \<or> a = b"
   445 proof -
   446   have "(a * c = b * c) = ((a - b) * c = 0)"
   447     by (simp add: algebra_simps)
   448   thus ?thesis by (simp add: disj_commute)
   449 qed
   450 
   451 lemma mult_cancel_left [simp]:
   452   "c * a = c * b \<longleftrightarrow> c = 0 \<or> a = b"
   453 proof -
   454   have "(c * a = c * b) = (c * (a - b) = 0)"
   455     by (simp add: algebra_simps)
   456   thus ?thesis by simp
   457 qed
   458 
   459 lemma mult_left_cancel:
   460   "c \<noteq> 0 \<Longrightarrow> c * a = c * b \<longleftrightarrow> a = b"
   461   by simp 
   462 
   463 lemma mult_right_cancel:
   464   "c \<noteq> 0 \<Longrightarrow> a * c = b * c \<longleftrightarrow> a = b"
   465   by simp 
   466 
   467 end
   468 
   469 class ring_1_no_zero_divisors = ring_1 + ring_no_zero_divisors
   470 begin
   471 
   472 lemma square_eq_1_iff:
   473   "x * x = 1 \<longleftrightarrow> x = 1 \<or> x = - 1"
   474 proof -
   475   have "(x - 1) * (x + 1) = x * x - 1"
   476     by (simp add: algebra_simps)
   477   hence "x * x = 1 \<longleftrightarrow> (x - 1) * (x + 1) = 0"
   478     by simp
   479   thus ?thesis
   480     by (simp add: eq_neg_iff_add_eq_0)
   481 qed
   482 
   483 lemma mult_cancel_right1 [simp]:
   484   "c = b * c \<longleftrightarrow> c = 0 \<or> b = 1"
   485 by (insert mult_cancel_right [of 1 c b], force)
   486 
   487 lemma mult_cancel_right2 [simp]:
   488   "a * c = c \<longleftrightarrow> c = 0 \<or> a = 1"
   489 by (insert mult_cancel_right [of a c 1], simp)
   490  
   491 lemma mult_cancel_left1 [simp]:
   492   "c = c * b \<longleftrightarrow> c = 0 \<or> b = 1"
   493 by (insert mult_cancel_left [of c 1 b], force)
   494 
   495 lemma mult_cancel_left2 [simp]:
   496   "c * a = c \<longleftrightarrow> c = 0 \<or> a = 1"
   497 by (insert mult_cancel_left [of c a 1], simp)
   498 
   499 end
   500 
   501 class idom = comm_ring_1 + no_zero_divisors
   502 begin
   503 
   504 subclass ring_1_no_zero_divisors ..
   505 
   506 lemma square_eq_iff: "a * a = b * b \<longleftrightarrow> (a = b \<or> a = - b)"
   507 proof
   508   assume "a * a = b * b"
   509   then have "(a - b) * (a + b) = 0"
   510     by (simp add: algebra_simps)
   511   then show "a = b \<or> a = - b"
   512     by (simp add: eq_neg_iff_add_eq_0)
   513 next
   514   assume "a = b \<or> a = - b"
   515   then show "a * a = b * b" by auto
   516 qed
   517 
   518 lemma dvd_mult_cancel_right [simp]:
   519   "a * c dvd b * c \<longleftrightarrow> c = 0 \<or> a dvd b"
   520 proof -
   521   have "a * c dvd b * c \<longleftrightarrow> (\<exists>k. b * c = (a * k) * c)"
   522     unfolding dvd_def by (simp add: ac_simps)
   523   also have "(\<exists>k. b * c = (a * k) * c) \<longleftrightarrow> c = 0 \<or> a dvd b"
   524     unfolding dvd_def by simp
   525   finally show ?thesis .
   526 qed
   527 
   528 lemma dvd_mult_cancel_left [simp]:
   529   "c * a dvd c * b \<longleftrightarrow> c = 0 \<or> a dvd b"
   530 proof -
   531   have "c * a dvd c * b \<longleftrightarrow> (\<exists>k. b * c = (a * k) * c)"
   532     unfolding dvd_def by (simp add: ac_simps)
   533   also have "(\<exists>k. b * c = (a * k) * c) \<longleftrightarrow> c = 0 \<or> a dvd b"
   534     unfolding dvd_def by simp
   535   finally show ?thesis .
   536 qed
   537 
   538 end
   539 
   540 text {*
   541   The theory of partially ordered rings is taken from the books:
   542   \begin{itemize}
   543   \item \emph{Lattice Theory} by Garret Birkhoff, American Mathematical Society 1979 
   544   \item \emph{Partially Ordered Algebraic Systems}, Pergamon Press 1963
   545   \end{itemize}
   546   Most of the used notions can also be looked up in 
   547   \begin{itemize}
   548   \item @{url "http://www.mathworld.com"} by Eric Weisstein et. al.
   549   \item \emph{Algebra I} by van der Waerden, Springer.
   550   \end{itemize}
   551 *}
   552 
   553 class ordered_semiring = semiring + comm_monoid_add + ordered_ab_semigroup_add +
   554   assumes mult_left_mono: "a \<le> b \<Longrightarrow> 0 \<le> c \<Longrightarrow> c * a \<le> c * b"
   555   assumes mult_right_mono: "a \<le> b \<Longrightarrow> 0 \<le> c \<Longrightarrow> a * c \<le> b * c"
   556 begin
   557 
   558 lemma mult_mono:
   559   "a \<le> b \<Longrightarrow> c \<le> d \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> c \<Longrightarrow> a * c \<le> b * d"
   560 apply (erule mult_right_mono [THEN order_trans], assumption)
   561 apply (erule mult_left_mono, assumption)
   562 done
   563 
   564 lemma mult_mono':
   565   "a \<le> b \<Longrightarrow> c \<le> d \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> c \<Longrightarrow> a * c \<le> b * d"
   566 apply (rule mult_mono)
   567 apply (fast intro: order_trans)+
   568 done
   569 
   570 end
   571 
   572 class ordered_cancel_semiring = ordered_semiring + cancel_comm_monoid_add
   573 begin
   574 
   575 subclass semiring_0_cancel ..
   576 
   577 lemma mult_nonneg_nonneg[simp]: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a * b"
   578 using mult_left_mono [of 0 b a] by simp
   579 
   580 lemma mult_nonneg_nonpos: "0 \<le> a \<Longrightarrow> b \<le> 0 \<Longrightarrow> a * b \<le> 0"
   581 using mult_left_mono [of b 0 a] by simp
   582 
   583 lemma mult_nonpos_nonneg: "a \<le> 0 \<Longrightarrow> 0 \<le> b \<Longrightarrow> a * b \<le> 0"
   584 using mult_right_mono [of a 0 b] by simp
   585 
   586 text {* Legacy - use @{text mult_nonpos_nonneg} *}
   587 lemma mult_nonneg_nonpos2: "0 \<le> a \<Longrightarrow> b \<le> 0 \<Longrightarrow> b * a \<le> 0" 
   588 by (drule mult_right_mono [of b 0], auto)
   589 
   590 lemma split_mult_neg_le: "(0 \<le> a & b \<le> 0) | (a \<le> 0 & 0 \<le> b) \<Longrightarrow> a * b \<le> 0" 
   591 by (auto simp add: mult_nonneg_nonpos mult_nonneg_nonpos2)
   592 
   593 end
   594 
   595 class linordered_semiring = ordered_semiring + linordered_cancel_ab_semigroup_add
   596 begin
   597 
   598 subclass ordered_cancel_semiring ..
   599 
   600 subclass ordered_comm_monoid_add ..
   601 
   602 lemma mult_left_less_imp_less:
   603   "c * a < c * b \<Longrightarrow> 0 \<le> c \<Longrightarrow> a < b"
   604 by (force simp add: mult_left_mono not_le [symmetric])
   605  
   606 lemma mult_right_less_imp_less:
   607   "a * c < b * c \<Longrightarrow> 0 \<le> c \<Longrightarrow> a < b"
   608 by (force simp add: mult_right_mono not_le [symmetric])
   609 
   610 end
   611 
   612 class linordered_semiring_1 = linordered_semiring + semiring_1
   613 begin
   614 
   615 lemma convex_bound_le:
   616   assumes "x \<le> a" "y \<le> a" "0 \<le> u" "0 \<le> v" "u + v = 1"
   617   shows "u * x + v * y \<le> a"
   618 proof-
   619   from assms have "u * x + v * y \<le> u * a + v * a"
   620     by (simp add: add_mono mult_left_mono)
   621   thus ?thesis using assms unfolding distrib_right[symmetric] by simp
   622 qed
   623 
   624 end
   625 
   626 class linordered_semiring_strict = semiring + comm_monoid_add + linordered_cancel_ab_semigroup_add +
   627   assumes mult_strict_left_mono: "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
   628   assumes mult_strict_right_mono: "a < b \<Longrightarrow> 0 < c \<Longrightarrow> a * c < b * c"
   629 begin
   630 
   631 subclass semiring_0_cancel ..
   632 
   633 subclass linordered_semiring
   634 proof
   635   fix a b c :: 'a
   636   assume A: "a \<le> b" "0 \<le> c"
   637   from A show "c * a \<le> c * b"
   638     unfolding le_less
   639     using mult_strict_left_mono by (cases "c = 0") auto
   640   from A show "a * c \<le> b * c"
   641     unfolding le_less
   642     using mult_strict_right_mono by (cases "c = 0") auto
   643 qed
   644 
   645 lemma mult_left_le_imp_le:
   646   "c * a \<le> c * b \<Longrightarrow> 0 < c \<Longrightarrow> a \<le> b"
   647 by (force simp add: mult_strict_left_mono _not_less [symmetric])
   648  
   649 lemma mult_right_le_imp_le:
   650   "a * c \<le> b * c \<Longrightarrow> 0 < c \<Longrightarrow> a \<le> b"
   651 by (force simp add: mult_strict_right_mono not_less [symmetric])
   652 
   653 lemma mult_pos_pos[simp]: "0 < a \<Longrightarrow> 0 < b \<Longrightarrow> 0 < a * b"
   654 using mult_strict_left_mono [of 0 b a] by simp
   655 
   656 lemma mult_pos_neg: "0 < a \<Longrightarrow> b < 0 \<Longrightarrow> a * b < 0"
   657 using mult_strict_left_mono [of b 0 a] by simp
   658 
   659 lemma mult_neg_pos: "a < 0 \<Longrightarrow> 0 < b \<Longrightarrow> a * b < 0"
   660 using mult_strict_right_mono [of a 0 b] by simp
   661 
   662 text {* Legacy - use @{text mult_neg_pos} *}
   663 lemma mult_pos_neg2: "0 < a \<Longrightarrow> b < 0 \<Longrightarrow> b * a < 0" 
   664 by (drule mult_strict_right_mono [of b 0], auto)
   665 
   666 lemma zero_less_mult_pos:
   667   "0 < a * b \<Longrightarrow> 0 < a \<Longrightarrow> 0 < b"
   668 apply (cases "b\<le>0")
   669  apply (auto simp add: le_less not_less)
   670 apply (drule_tac mult_pos_neg [of a b])
   671  apply (auto dest: less_not_sym)
   672 done
   673 
   674 lemma zero_less_mult_pos2:
   675   "0 < b * a \<Longrightarrow> 0 < a \<Longrightarrow> 0 < b"
   676 apply (cases "b\<le>0")
   677  apply (auto simp add: le_less not_less)
   678 apply (drule_tac mult_pos_neg2 [of a b])
   679  apply (auto dest: less_not_sym)
   680 done
   681 
   682 text{*Strict monotonicity in both arguments*}
   683 lemma mult_strict_mono:
   684   assumes "a < b" and "c < d" and "0 < b" and "0 \<le> c"
   685   shows "a * c < b * d"
   686   using assms apply (cases "c=0")
   687   apply (simp)
   688   apply (erule mult_strict_right_mono [THEN less_trans])
   689   apply (force simp add: le_less)
   690   apply (erule mult_strict_left_mono, assumption)
   691   done
   692 
   693 text{*This weaker variant has more natural premises*}
   694 lemma mult_strict_mono':
   695   assumes "a < b" and "c < d" and "0 \<le> a" and "0 \<le> c"
   696   shows "a * c < b * d"
   697 by (rule mult_strict_mono) (insert assms, auto)
   698 
   699 lemma mult_less_le_imp_less:
   700   assumes "a < b" and "c \<le> d" and "0 \<le> a" and "0 < c"
   701   shows "a * c < b * d"
   702   using assms apply (subgoal_tac "a * c < b * c")
   703   apply (erule less_le_trans)
   704   apply (erule mult_left_mono)
   705   apply simp
   706   apply (erule mult_strict_right_mono)
   707   apply assumption
   708   done
   709 
   710 lemma mult_le_less_imp_less:
   711   assumes "a \<le> b" and "c < d" and "0 < a" and "0 \<le> c"
   712   shows "a * c < b * d"
   713   using assms apply (subgoal_tac "a * c \<le> b * c")
   714   apply (erule le_less_trans)
   715   apply (erule mult_strict_left_mono)
   716   apply simp
   717   apply (erule mult_right_mono)
   718   apply simp
   719   done
   720 
   721 end
   722 
   723 class linordered_semiring_1_strict = linordered_semiring_strict + semiring_1
   724 begin
   725 
   726 subclass linordered_semiring_1 ..
   727 
   728 lemma convex_bound_lt:
   729   assumes "x < a" "y < a" "0 \<le> u" "0 \<le> v" "u + v = 1"
   730   shows "u * x + v * y < a"
   731 proof -
   732   from assms have "u * x + v * y < u * a + v * a"
   733     by (cases "u = 0")
   734        (auto intro!: add_less_le_mono mult_strict_left_mono mult_left_mono)
   735   thus ?thesis using assms unfolding distrib_right[symmetric] by simp
   736 qed
   737 
   738 end
   739 
   740 class ordered_comm_semiring = comm_semiring_0 + ordered_ab_semigroup_add + 
   741   assumes comm_mult_left_mono: "a \<le> b \<Longrightarrow> 0 \<le> c \<Longrightarrow> c * a \<le> c * b"
   742 begin
   743 
   744 subclass ordered_semiring
   745 proof
   746   fix a b c :: 'a
   747   assume "a \<le> b" "0 \<le> c"
   748   thus "c * a \<le> c * b" by (rule comm_mult_left_mono)
   749   thus "a * c \<le> b * c" by (simp only: mult.commute)
   750 qed
   751 
   752 end
   753 
   754 class ordered_cancel_comm_semiring = ordered_comm_semiring + cancel_comm_monoid_add
   755 begin
   756 
   757 subclass comm_semiring_0_cancel ..
   758 subclass ordered_comm_semiring ..
   759 subclass ordered_cancel_semiring ..
   760 
   761 end
   762 
   763 class linordered_comm_semiring_strict = comm_semiring_0 + linordered_cancel_ab_semigroup_add +
   764   assumes comm_mult_strict_left_mono: "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
   765 begin
   766 
   767 subclass linordered_semiring_strict
   768 proof
   769   fix a b c :: 'a
   770   assume "a < b" "0 < c"
   771   thus "c * a < c * b" by (rule comm_mult_strict_left_mono)
   772   thus "a * c < b * c" by (simp only: mult.commute)
   773 qed
   774 
   775 subclass ordered_cancel_comm_semiring
   776 proof
   777   fix a b c :: 'a
   778   assume "a \<le> b" "0 \<le> c"
   779   thus "c * a \<le> c * b"
   780     unfolding le_less
   781     using mult_strict_left_mono by (cases "c = 0") auto
   782 qed
   783 
   784 end
   785 
   786 class ordered_ring = ring + ordered_cancel_semiring 
   787 begin
   788 
   789 subclass ordered_ab_group_add ..
   790 
   791 lemma less_add_iff1:
   792   "a * e + c < b * e + d \<longleftrightarrow> (a - b) * e + c < d"
   793 by (simp add: algebra_simps)
   794 
   795 lemma less_add_iff2:
   796   "a * e + c < b * e + d \<longleftrightarrow> c < (b - a) * e + d"
   797 by (simp add: algebra_simps)
   798 
   799 lemma le_add_iff1:
   800   "a * e + c \<le> b * e + d \<longleftrightarrow> (a - b) * e + c \<le> d"
   801 by (simp add: algebra_simps)
   802 
   803 lemma le_add_iff2:
   804   "a * e + c \<le> b * e + d \<longleftrightarrow> c \<le> (b - a) * e + d"
   805 by (simp add: algebra_simps)
   806 
   807 lemma mult_left_mono_neg:
   808   "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> c * a \<le> c * b"
   809   apply (drule mult_left_mono [of _ _ "- c"])
   810   apply simp_all
   811   done
   812 
   813 lemma mult_right_mono_neg:
   814   "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> a * c \<le> b * c"
   815   apply (drule mult_right_mono [of _ _ "- c"])
   816   apply simp_all
   817   done
   818 
   819 lemma mult_nonpos_nonpos: "a \<le> 0 \<Longrightarrow> b \<le> 0 \<Longrightarrow> 0 \<le> a * b"
   820 using mult_right_mono_neg [of a 0 b] by simp
   821 
   822 lemma split_mult_pos_le:
   823   "(0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0) \<Longrightarrow> 0 \<le> a * b"
   824 by (auto simp add: mult_nonpos_nonpos)
   825 
   826 end
   827 
   828 class linordered_ring = ring + linordered_semiring + linordered_ab_group_add + abs_if
   829 begin
   830 
   831 subclass ordered_ring ..
   832 
   833 subclass ordered_ab_group_add_abs
   834 proof
   835   fix a b
   836   show "\<bar>a + b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
   837     by (auto simp add: abs_if not_le not_less algebra_simps simp del: add.commute dest: add_neg_neg add_nonneg_nonneg)
   838 qed (auto simp add: abs_if)
   839 
   840 lemma zero_le_square [simp]: "0 \<le> a * a"
   841   using linear [of 0 a]
   842   by (auto simp add: mult_nonpos_nonpos)
   843 
   844 lemma not_square_less_zero [simp]: "\<not> (a * a < 0)"
   845   by (simp add: not_less)
   846 
   847 end
   848 
   849 (* The "strict" suffix can be seen as describing the combination of linordered_ring and no_zero_divisors.
   850    Basically, linordered_ring + no_zero_divisors = linordered_ring_strict.
   851  *)
   852 class linordered_ring_strict = ring + linordered_semiring_strict
   853   + ordered_ab_group_add + abs_if
   854 begin
   855 
   856 subclass linordered_ring ..
   857 
   858 lemma mult_strict_left_mono_neg: "b < a \<Longrightarrow> c < 0 \<Longrightarrow> c * a < c * b"
   859 using mult_strict_left_mono [of b a "- c"] by simp
   860 
   861 lemma mult_strict_right_mono_neg: "b < a \<Longrightarrow> c < 0 \<Longrightarrow> a * c < b * c"
   862 using mult_strict_right_mono [of b a "- c"] by simp
   863 
   864 lemma mult_neg_neg: "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> 0 < a * b"
   865 using mult_strict_right_mono_neg [of a 0 b] by simp
   866 
   867 subclass ring_no_zero_divisors
   868 proof
   869   fix a b
   870   assume "a \<noteq> 0" then have A: "a < 0 \<or> 0 < a" by (simp add: neq_iff)
   871   assume "b \<noteq> 0" then have B: "b < 0 \<or> 0 < b" by (simp add: neq_iff)
   872   have "a * b < 0 \<or> 0 < a * b"
   873   proof (cases "a < 0")
   874     case True note A' = this
   875     show ?thesis proof (cases "b < 0")
   876       case True with A'
   877       show ?thesis by (auto dest: mult_neg_neg)
   878     next
   879       case False with B have "0 < b" by auto
   880       with A' show ?thesis by (auto dest: mult_strict_right_mono)
   881     qed
   882   next
   883     case False with A have A': "0 < a" by auto
   884     show ?thesis proof (cases "b < 0")
   885       case True with A'
   886       show ?thesis by (auto dest: mult_strict_right_mono_neg)
   887     next
   888       case False with B have "0 < b" by auto
   889       with A' show ?thesis by auto
   890     qed
   891   qed
   892   then show "a * b \<noteq> 0" by (simp add: neq_iff)
   893 qed
   894 
   895 lemma zero_less_mult_iff: "0 < a * b \<longleftrightarrow> 0 < a \<and> 0 < b \<or> a < 0 \<and> b < 0"
   896   by (cases a 0 b 0 rule: linorder_cases[case_product linorder_cases])
   897      (auto simp add: mult_neg_neg not_less le_less dest: zero_less_mult_pos zero_less_mult_pos2)
   898 
   899 lemma zero_le_mult_iff: "0 \<le> a * b \<longleftrightarrow> 0 \<le> a \<and> 0 \<le> b \<or> a \<le> 0 \<and> b \<le> 0"
   900   by (auto simp add: eq_commute [of 0] le_less not_less zero_less_mult_iff)
   901 
   902 lemma mult_less_0_iff:
   903   "a * b < 0 \<longleftrightarrow> 0 < a \<and> b < 0 \<or> a < 0 \<and> 0 < b"
   904   apply (insert zero_less_mult_iff [of "-a" b])
   905   apply force
   906   done
   907 
   908 lemma mult_le_0_iff:
   909   "a * b \<le> 0 \<longleftrightarrow> 0 \<le> a \<and> b \<le> 0 \<or> a \<le> 0 \<and> 0 \<le> b"
   910   apply (insert zero_le_mult_iff [of "-a" b]) 
   911   apply force
   912   done
   913 
   914 text{*Cancellation laws for @{term "c*a < c*b"} and @{term "a*c < b*c"},
   915    also with the relations @{text "\<le>"} and equality.*}
   916 
   917 text{*These ``disjunction'' versions produce two cases when the comparison is
   918  an assumption, but effectively four when the comparison is a goal.*}
   919 
   920 lemma mult_less_cancel_right_disj:
   921   "a * c < b * c \<longleftrightarrow> 0 < c \<and> a < b \<or> c < 0 \<and>  b < a"
   922   apply (cases "c = 0")
   923   apply (auto simp add: neq_iff mult_strict_right_mono 
   924                       mult_strict_right_mono_neg)
   925   apply (auto simp add: not_less 
   926                       not_le [symmetric, of "a*c"]
   927                       not_le [symmetric, of a])
   928   apply (erule_tac [!] notE)
   929   apply (auto simp add: less_imp_le mult_right_mono 
   930                       mult_right_mono_neg)
   931   done
   932 
   933 lemma mult_less_cancel_left_disj:
   934   "c * a < c * b \<longleftrightarrow> 0 < c \<and> a < b \<or> c < 0 \<and>  b < a"
   935   apply (cases "c = 0")
   936   apply (auto simp add: neq_iff mult_strict_left_mono 
   937                       mult_strict_left_mono_neg)
   938   apply (auto simp add: not_less 
   939                       not_le [symmetric, of "c*a"]
   940                       not_le [symmetric, of a])
   941   apply (erule_tac [!] notE)
   942   apply (auto simp add: less_imp_le mult_left_mono 
   943                       mult_left_mono_neg)
   944   done
   945 
   946 text{*The ``conjunction of implication'' lemmas produce two cases when the
   947 comparison is a goal, but give four when the comparison is an assumption.*}
   948 
   949 lemma mult_less_cancel_right:
   950   "a * c < b * c \<longleftrightarrow> (0 \<le> c \<longrightarrow> a < b) \<and> (c \<le> 0 \<longrightarrow> b < a)"
   951   using mult_less_cancel_right_disj [of a c b] by auto
   952 
   953 lemma mult_less_cancel_left:
   954   "c * a < c * b \<longleftrightarrow> (0 \<le> c \<longrightarrow> a < b) \<and> (c \<le> 0 \<longrightarrow> b < a)"
   955   using mult_less_cancel_left_disj [of c a b] by auto
   956 
   957 lemma mult_le_cancel_right:
   958    "a * c \<le> b * c \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
   959 by (simp add: not_less [symmetric] mult_less_cancel_right_disj)
   960 
   961 lemma mult_le_cancel_left:
   962   "c * a \<le> c * b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
   963 by (simp add: not_less [symmetric] mult_less_cancel_left_disj)
   964 
   965 lemma mult_le_cancel_left_pos:
   966   "0 < c \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> a \<le> b"
   967 by (auto simp: mult_le_cancel_left)
   968 
   969 lemma mult_le_cancel_left_neg:
   970   "c < 0 \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> b \<le> a"
   971 by (auto simp: mult_le_cancel_left)
   972 
   973 lemma mult_less_cancel_left_pos:
   974   "0 < c \<Longrightarrow> c * a < c * b \<longleftrightarrow> a < b"
   975 by (auto simp: mult_less_cancel_left)
   976 
   977 lemma mult_less_cancel_left_neg:
   978   "c < 0 \<Longrightarrow> c * a < c * b \<longleftrightarrow> b < a"
   979 by (auto simp: mult_less_cancel_left)
   980 
   981 end
   982 
   983 lemmas mult_sign_intros =
   984   mult_nonneg_nonneg mult_nonneg_nonpos
   985   mult_nonpos_nonneg mult_nonpos_nonpos
   986   mult_pos_pos mult_pos_neg
   987   mult_neg_pos mult_neg_neg
   988 
   989 class ordered_comm_ring = comm_ring + ordered_comm_semiring
   990 begin
   991 
   992 subclass ordered_ring ..
   993 subclass ordered_cancel_comm_semiring ..
   994 
   995 end
   996 
   997 class linordered_semidom = comm_semiring_1_cancel + linordered_comm_semiring_strict +
   998   (*previously linordered_semiring*)
   999   assumes zero_less_one [simp]: "0 < 1"
  1000 begin
  1001 
  1002 lemma pos_add_strict:
  1003   shows "0 < a \<Longrightarrow> b < c \<Longrightarrow> b < a + c"
  1004   using add_strict_mono [of 0 a b c] by simp
  1005 
  1006 lemma zero_le_one [simp]: "0 \<le> 1"
  1007 by (rule zero_less_one [THEN less_imp_le]) 
  1008 
  1009 lemma not_one_le_zero [simp]: "\<not> 1 \<le> 0"
  1010 by (simp add: not_le) 
  1011 
  1012 lemma not_one_less_zero [simp]: "\<not> 1 < 0"
  1013 by (simp add: not_less) 
  1014 
  1015 lemma less_1_mult:
  1016   assumes "1 < m" and "1 < n"
  1017   shows "1 < m * n"
  1018   using assms mult_strict_mono [of 1 m 1 n]
  1019     by (simp add:  less_trans [OF zero_less_one]) 
  1020 
  1021 lemma mult_left_le: "c \<le> 1 \<Longrightarrow> 0 \<le> a \<Longrightarrow> a * c \<le> a"
  1022   using mult_left_mono[of c 1 a] by simp
  1023 
  1024 lemma mult_le_one: "a \<le> 1 \<Longrightarrow> 0 \<le> b \<Longrightarrow> b \<le> 1 \<Longrightarrow> a * b \<le> 1"
  1025   using mult_mono[of a 1 b 1] by simp
  1026 
  1027 end
  1028 
  1029 class linordered_idom = comm_ring_1 +
  1030   linordered_comm_semiring_strict + ordered_ab_group_add +
  1031   abs_if + sgn_if
  1032   (*previously linordered_ring*)
  1033 begin
  1034 
  1035 subclass linordered_semiring_1_strict ..
  1036 subclass linordered_ring_strict ..
  1037 subclass ordered_comm_ring ..
  1038 subclass idom ..
  1039 
  1040 subclass linordered_semidom
  1041 proof
  1042   have "0 \<le> 1 * 1" by (rule zero_le_square)
  1043   thus "0 < 1" by (simp add: le_less)
  1044 qed 
  1045 
  1046 lemma linorder_neqE_linordered_idom:
  1047   assumes "x \<noteq> y" obtains "x < y" | "y < x"
  1048   using assms by (rule neqE)
  1049 
  1050 text {* These cancellation simprules also produce two cases when the comparison is a goal. *}
  1051 
  1052 lemma mult_le_cancel_right1:
  1053   "c \<le> b * c \<longleftrightarrow> (0 < c \<longrightarrow> 1 \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> 1)"
  1054 by (insert mult_le_cancel_right [of 1 c b], simp)
  1055 
  1056 lemma mult_le_cancel_right2:
  1057   "a * c \<le> c \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> 1) \<and> (c < 0 \<longrightarrow> 1 \<le> a)"
  1058 by (insert mult_le_cancel_right [of a c 1], simp)
  1059 
  1060 lemma mult_le_cancel_left1:
  1061   "c \<le> c * b \<longleftrightarrow> (0 < c \<longrightarrow> 1 \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> 1)"
  1062 by (insert mult_le_cancel_left [of c 1 b], simp)
  1063 
  1064 lemma mult_le_cancel_left2:
  1065   "c * a \<le> c \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> 1) \<and> (c < 0 \<longrightarrow> 1 \<le> a)"
  1066 by (insert mult_le_cancel_left [of c a 1], simp)
  1067 
  1068 lemma mult_less_cancel_right1:
  1069   "c < b * c \<longleftrightarrow> (0 \<le> c \<longrightarrow> 1 < b) \<and> (c \<le> 0 \<longrightarrow> b < 1)"
  1070 by (insert mult_less_cancel_right [of 1 c b], simp)
  1071 
  1072 lemma mult_less_cancel_right2:
  1073   "a * c < c \<longleftrightarrow> (0 \<le> c \<longrightarrow> a < 1) \<and> (c \<le> 0 \<longrightarrow> 1 < a)"
  1074 by (insert mult_less_cancel_right [of a c 1], simp)
  1075 
  1076 lemma mult_less_cancel_left1:
  1077   "c < c * b \<longleftrightarrow> (0 \<le> c \<longrightarrow> 1 < b) \<and> (c \<le> 0 \<longrightarrow> b < 1)"
  1078 by (insert mult_less_cancel_left [of c 1 b], simp)
  1079 
  1080 lemma mult_less_cancel_left2:
  1081   "c * a < c \<longleftrightarrow> (0 \<le> c \<longrightarrow> a < 1) \<and> (c \<le> 0 \<longrightarrow> 1 < a)"
  1082 by (insert mult_less_cancel_left [of c a 1], simp)
  1083 
  1084 lemma sgn_sgn [simp]:
  1085   "sgn (sgn a) = sgn a"
  1086 unfolding sgn_if by simp
  1087 
  1088 lemma sgn_0_0:
  1089   "sgn a = 0 \<longleftrightarrow> a = 0"
  1090 unfolding sgn_if by simp
  1091 
  1092 lemma sgn_1_pos:
  1093   "sgn a = 1 \<longleftrightarrow> a > 0"
  1094 unfolding sgn_if by simp
  1095 
  1096 lemma sgn_1_neg:
  1097   "sgn a = - 1 \<longleftrightarrow> a < 0"
  1098 unfolding sgn_if by auto
  1099 
  1100 lemma sgn_pos [simp]:
  1101   "0 < a \<Longrightarrow> sgn a = 1"
  1102 unfolding sgn_1_pos .
  1103 
  1104 lemma sgn_neg [simp]:
  1105   "a < 0 \<Longrightarrow> sgn a = - 1"
  1106 unfolding sgn_1_neg .
  1107 
  1108 lemma sgn_times:
  1109   "sgn (a * b) = sgn a * sgn b"
  1110 by (auto simp add: sgn_if zero_less_mult_iff)
  1111 
  1112 lemma abs_sgn: "\<bar>k\<bar> = k * sgn k"
  1113 unfolding sgn_if abs_if by auto
  1114 
  1115 lemma sgn_greater [simp]:
  1116   "0 < sgn a \<longleftrightarrow> 0 < a"
  1117   unfolding sgn_if by auto
  1118 
  1119 lemma sgn_less [simp]:
  1120   "sgn a < 0 \<longleftrightarrow> a < 0"
  1121   unfolding sgn_if by auto
  1122 
  1123 lemma abs_dvd_iff [simp]: "\<bar>m\<bar> dvd k \<longleftrightarrow> m dvd k"
  1124   by (simp add: abs_if)
  1125 
  1126 lemma dvd_abs_iff [simp]: "m dvd \<bar>k\<bar> \<longleftrightarrow> m dvd k"
  1127   by (simp add: abs_if)
  1128 
  1129 lemma dvd_if_abs_eq:
  1130   "\<bar>l\<bar> = \<bar>k\<bar> \<Longrightarrow> l dvd k"
  1131 by(subst abs_dvd_iff[symmetric]) simp
  1132 
  1133 text {* The following lemmas can be proven in more general structures, but
  1134 are dangerous as simp rules in absence of @{thm neg_equal_zero}, 
  1135 @{thm neg_less_pos}, @{thm neg_less_eq_nonneg}. *}
  1136 
  1137 lemma equation_minus_iff_1 [simp, no_atp]:
  1138   "1 = - a \<longleftrightarrow> a = - 1"
  1139   by (fact equation_minus_iff)
  1140 
  1141 lemma minus_equation_iff_1 [simp, no_atp]:
  1142   "- a = 1 \<longleftrightarrow> a = - 1"
  1143   by (subst minus_equation_iff, auto)
  1144 
  1145 lemma le_minus_iff_1 [simp, no_atp]:
  1146   "1 \<le> - b \<longleftrightarrow> b \<le> - 1"
  1147   by (fact le_minus_iff)
  1148 
  1149 lemma minus_le_iff_1 [simp, no_atp]:
  1150   "- a \<le> 1 \<longleftrightarrow> - 1 \<le> a"
  1151   by (fact minus_le_iff)
  1152 
  1153 lemma less_minus_iff_1 [simp, no_atp]:
  1154   "1 < - b \<longleftrightarrow> b < - 1"
  1155   by (fact less_minus_iff)
  1156 
  1157 lemma minus_less_iff_1 [simp, no_atp]:
  1158   "- a < 1 \<longleftrightarrow> - 1 < a"
  1159   by (fact minus_less_iff)
  1160 
  1161 end
  1162 
  1163 text {* Simprules for comparisons where common factors can be cancelled. *}
  1164 
  1165 lemmas mult_compare_simps =
  1166     mult_le_cancel_right mult_le_cancel_left
  1167     mult_le_cancel_right1 mult_le_cancel_right2
  1168     mult_le_cancel_left1 mult_le_cancel_left2
  1169     mult_less_cancel_right mult_less_cancel_left
  1170     mult_less_cancel_right1 mult_less_cancel_right2
  1171     mult_less_cancel_left1 mult_less_cancel_left2
  1172     mult_cancel_right mult_cancel_left
  1173     mult_cancel_right1 mult_cancel_right2
  1174     mult_cancel_left1 mult_cancel_left2
  1175 
  1176 text {* Reasoning about inequalities with division *}
  1177 
  1178 context linordered_semidom
  1179 begin
  1180 
  1181 lemma less_add_one: "a < a + 1"
  1182 proof -
  1183   have "a + 0 < a + 1"
  1184     by (blast intro: zero_less_one add_strict_left_mono)
  1185   thus ?thesis by simp
  1186 qed
  1187 
  1188 lemma zero_less_two: "0 < 1 + 1"
  1189 by (blast intro: less_trans zero_less_one less_add_one)
  1190 
  1191 end
  1192 
  1193 context linordered_idom
  1194 begin
  1195 
  1196 lemma mult_right_le_one_le:
  1197   "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> x * y \<le> x"
  1198   by (auto simp add: mult_le_cancel_left2)
  1199 
  1200 lemma mult_left_le_one_le:
  1201   "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> y * x \<le> x"
  1202   by (auto simp add: mult_le_cancel_right2)
  1203 
  1204 end
  1205 
  1206 text {* Absolute Value *}
  1207 
  1208 context linordered_idom
  1209 begin
  1210 
  1211 lemma mult_sgn_abs:
  1212   "sgn x * \<bar>x\<bar> = x"
  1213   unfolding abs_if sgn_if by auto
  1214 
  1215 lemma abs_one [simp]:
  1216   "\<bar>1\<bar> = 1"
  1217   by (simp add: abs_if)
  1218 
  1219 end
  1220 
  1221 class ordered_ring_abs = ordered_ring + ordered_ab_group_add_abs +
  1222   assumes abs_eq_mult:
  1223     "(0 \<le> a \<or> a \<le> 0) \<and> (0 \<le> b \<or> b \<le> 0) \<Longrightarrow> \<bar>a * b\<bar> = \<bar>a\<bar> * \<bar>b\<bar>"
  1224 
  1225 context linordered_idom
  1226 begin
  1227 
  1228 subclass ordered_ring_abs proof
  1229 qed (auto simp add: abs_if not_less mult_less_0_iff)
  1230 
  1231 lemma abs_mult:
  1232   "\<bar>a * b\<bar> = \<bar>a\<bar> * \<bar>b\<bar>" 
  1233   by (rule abs_eq_mult) auto
  1234 
  1235 lemma abs_mult_self:
  1236   "\<bar>a\<bar> * \<bar>a\<bar> = a * a"
  1237   by (simp add: abs_if) 
  1238 
  1239 lemma abs_mult_less:
  1240   "\<bar>a\<bar> < c \<Longrightarrow> \<bar>b\<bar> < d \<Longrightarrow> \<bar>a\<bar> * \<bar>b\<bar> < c * d"
  1241 proof -
  1242   assume ac: "\<bar>a\<bar> < c"
  1243   hence cpos: "0<c" by (blast intro: le_less_trans abs_ge_zero)
  1244   assume "\<bar>b\<bar> < d"
  1245   thus ?thesis by (simp add: ac cpos mult_strict_mono) 
  1246 qed
  1247 
  1248 lemma abs_less_iff:
  1249   "\<bar>a\<bar> < b \<longleftrightarrow> a < b \<and> - a < b" 
  1250   by (simp add: less_le abs_le_iff) (auto simp add: abs_if)
  1251 
  1252 lemma abs_mult_pos:
  1253   "0 \<le> x \<Longrightarrow> \<bar>y\<bar> * x = \<bar>y * x\<bar>"
  1254   by (simp add: abs_mult)
  1255 
  1256 lemma abs_diff_less_iff:
  1257   "\<bar>x - a\<bar> < r \<longleftrightarrow> a - r < x \<and> x < a + r"
  1258   by (auto simp add: diff_less_eq ac_simps abs_less_iff)
  1259 
  1260 end
  1261 
  1262 hide_fact (open) comm_mult_left_mono comm_mult_strict_left_mono distrib
  1263 
  1264 code_identifier
  1265   code_module Rings \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
  1266 
  1267 end
  1268