src/HOL/OrderedGroup.thy
author haftmann
Thu Oct 18 09:20:57 2007 +0200 (2007-10-18)
changeset 25077 c2ec5e589d78
parent 25062 af5ef0d4d655
child 25090 4a50b958391a
permissions -rw-r--r--
continued localization
     1 (*  Title:   HOL/OrderedGroup.thy
     2     ID:      $Id$
     3     Author:  Gertrud Bauer, Steven Obua, Lawrence C Paulson, and Markus Wenzel,
     4              with contributions by Jeremy Avigad
     5 *)
     6 
     7 header {* Ordered Groups *}
     8 
     9 theory OrderedGroup
    10 imports Lattices
    11 uses "~~/src/Provers/Arith/abel_cancel.ML"
    12 begin
    13 
    14 text {*
    15   The theory of partially ordered groups is taken from the books:
    16   \begin{itemize}
    17   \item \emph{Lattice Theory} by Garret Birkhoff, American Mathematical Society 1979 
    18   \item \emph{Partially Ordered Algebraic Systems}, Pergamon Press 1963
    19   \end{itemize}
    20   Most of the used notions can also be looked up in 
    21   \begin{itemize}
    22   \item \url{http://www.mathworld.com} by Eric Weisstein et. al.
    23   \item \emph{Algebra I} by van der Waerden, Springer.
    24   \end{itemize}
    25 *}
    26 
    27 subsection {* Semigroups and Monoids *}
    28 
    29 class semigroup_add = plus +
    30   assumes add_assoc: "(a + b) + c = a + (b + c)"
    31 
    32 class ab_semigroup_add = semigroup_add +
    33   assumes add_commute: "a + b = b + a"
    34 begin
    35 
    36 lemma add_left_commute: "a + (b + c) = b + (a + c)"
    37   by (rule mk_left_commute [of "plus", OF add_assoc add_commute])
    38 
    39 theorems add_ac = add_assoc add_commute add_left_commute
    40 
    41 end
    42 
    43 theorems add_ac = add_assoc add_commute add_left_commute
    44 
    45 class semigroup_mult = times +
    46   assumes mult_assoc: "(a * b) * c = a * (b * c)"
    47 
    48 class ab_semigroup_mult = semigroup_mult +
    49   assumes mult_commute: "a * b = b * a"
    50 begin
    51 
    52 lemma mult_left_commute: "a * (b * c) = b * (a * c)"
    53   by (rule mk_left_commute [of "times", OF mult_assoc mult_commute])
    54 
    55 theorems mult_ac = mult_assoc mult_commute mult_left_commute
    56 
    57 end
    58 
    59 theorems mult_ac = mult_assoc mult_commute mult_left_commute
    60 
    61 class monoid_add = zero + semigroup_add +
    62   assumes add_0_left [simp]: "0 + a = a"
    63     and add_0_right [simp]: "a + 0 = a"
    64 
    65 class comm_monoid_add = zero + ab_semigroup_add +
    66   assumes add_0: "0 + a = a"
    67 begin
    68 
    69 subclass monoid_add
    70   by unfold_locales (insert add_0, simp_all add: add_commute)
    71 
    72 end
    73 
    74 class monoid_mult = one + semigroup_mult +
    75   assumes mult_1_left [simp]: "1 * a  = a"
    76   assumes mult_1_right [simp]: "a * 1 = a"
    77 
    78 class comm_monoid_mult = one + ab_semigroup_mult +
    79   assumes mult_1: "1 * a = a"
    80 begin
    81 
    82 subclass monoid_mult
    83   by unfold_locales (insert mult_1, simp_all add: mult_commute) 
    84 
    85 end
    86 
    87 class cancel_semigroup_add = semigroup_add +
    88   assumes add_left_imp_eq: "a + b = a + c \<Longrightarrow> b = c"
    89   assumes add_right_imp_eq: "b + a = c + a \<Longrightarrow> b = c"
    90 
    91 class cancel_ab_semigroup_add = ab_semigroup_add +
    92   assumes add_imp_eq: "a + b = a + c \<Longrightarrow> b = c"
    93 begin
    94 
    95 subclass cancel_semigroup_add
    96 proof unfold_locales
    97   fix a b c :: 'a
    98   assume "a + b = a + c" 
    99   then show "b = c" by (rule add_imp_eq)
   100 next
   101   fix a b c :: 'a
   102   assume "b + a = c + a"
   103   then have "a + b = a + c" by (simp only: add_commute)
   104   then show "b = c" by (rule add_imp_eq)
   105 qed
   106 
   107 end context cancel_ab_semigroup_add begin
   108 
   109 lemma add_left_cancel [simp]:
   110   "a + b = a + c \<longleftrightarrow> b = c"
   111   by (blast dest: add_left_imp_eq)
   112 
   113 lemma add_right_cancel [simp]:
   114   "b + a = c + a \<longleftrightarrow> b = c"
   115   by (blast dest: add_right_imp_eq)
   116 
   117 end
   118 
   119 subsection {* Groups *}
   120 
   121 class group_add = minus + monoid_add +
   122   assumes left_minus [simp]: "- a + a = 0"
   123   assumes diff_minus: "a - b = a + (- b)"
   124 begin
   125 
   126 lemma minus_add_cancel: "- a + (a + b) = b"
   127   by (simp add: add_assoc[symmetric])
   128 
   129 lemma minus_zero [simp]: "- 0 = 0"
   130 proof -
   131   have "- 0 = - 0 + (0 + 0)" by (simp only: add_0_right)
   132   also have "\<dots> = 0" by (rule minus_add_cancel)
   133   finally show ?thesis .
   134 qed
   135 
   136 lemma minus_minus [simp]: "- (- a) = a"
   137 proof -
   138   have "- (- a) = - (- a) + (- a + a)" by simp
   139   also have "\<dots> = a" by (rule minus_add_cancel)
   140   finally show ?thesis .
   141 qed
   142 
   143 lemma right_minus [simp]: "a + - a = 0"
   144 proof -
   145   have "a + - a = - (- a) + - a" by simp
   146   also have "\<dots> = 0" by (rule left_minus)
   147   finally show ?thesis .
   148 qed
   149 
   150 lemma right_minus_eq: "a - b = 0 \<longleftrightarrow> a = b"
   151 proof
   152   assume "a - b = 0"
   153   have "a = (a - b) + b" by (simp add:diff_minus add_assoc)
   154   also have "\<dots> = b" using `a - b = 0` by simp
   155   finally show "a = b" .
   156 next
   157   assume "a = b" thus "a - b = 0" by (simp add: diff_minus)
   158 qed
   159 
   160 lemma equals_zero_I:
   161   assumes "a + b = 0"
   162   shows "- a = b"
   163 proof -
   164   have "- a = - a + (a + b)" using assms by simp
   165   also have "\<dots> = b" by (simp add: add_assoc[symmetric])
   166   finally show ?thesis .
   167 qed
   168 
   169 lemma diff_self [simp]: "a - a = 0"
   170   by (simp add: diff_minus)
   171 
   172 lemma diff_0 [simp]: "0 - a = - a"
   173   by (simp add: diff_minus)
   174 
   175 lemma diff_0_right [simp]: "a - 0 = a" 
   176   by (simp add: diff_minus)
   177 
   178 lemma diff_minus_eq_add [simp]: "a - - b = a + b"
   179   by (simp add: diff_minus)
   180 
   181 lemma neg_equal_iff_equal [simp]:
   182   "- a = - b \<longleftrightarrow> a = b" 
   183 proof 
   184   assume "- a = - b"
   185   hence "- (- a) = - (- b)"
   186     by simp
   187   thus "a = b" by simp
   188 next
   189   assume "a = b"
   190   thus "- a = - b" by simp
   191 qed
   192 
   193 lemma neg_equal_0_iff_equal [simp]:
   194   "- a = 0 \<longleftrightarrow> a = 0"
   195   by (subst neg_equal_iff_equal [symmetric], simp)
   196 
   197 lemma neg_0_equal_iff_equal [simp]:
   198   "0 = - a \<longleftrightarrow> 0 = a"
   199   by (subst neg_equal_iff_equal [symmetric], simp)
   200 
   201 text{*The next two equations can make the simplifier loop!*}
   202 
   203 lemma equation_minus_iff:
   204   "a = - b \<longleftrightarrow> b = - a"
   205 proof -
   206   have "- (- a) = - b \<longleftrightarrow> - a = b" by (rule neg_equal_iff_equal)
   207   thus ?thesis by (simp add: eq_commute)
   208 qed
   209 
   210 lemma minus_equation_iff:
   211   "- a = b \<longleftrightarrow> - b = a"
   212 proof -
   213   have "- a = - (- b) \<longleftrightarrow> a = -b" by (rule neg_equal_iff_equal)
   214   thus ?thesis by (simp add: eq_commute)
   215 qed
   216 
   217 end
   218 
   219 class ab_group_add = minus + comm_monoid_add +
   220   assumes ab_left_minus: "- a + a = 0"
   221   assumes ab_diff_minus: "a - b = a + (- b)"
   222 
   223 subclass (in ab_group_add) group_add
   224   by unfold_locales (simp_all add: ab_left_minus ab_diff_minus)
   225 
   226 subclass (in ab_group_add) cancel_semigroup_add
   227 proof unfold_locales
   228   fix a b c :: 'a
   229   assume "a + b = a + c"
   230   then have "- a + a + b = - a + a + c"
   231     unfolding add_assoc by simp
   232   then show "b = c" by simp
   233 next
   234   fix a b c :: 'a
   235   assume "b + a = c + a"
   236   then have "b + (a + - a) = c + (a + - a)"
   237     unfolding add_assoc [symmetric] by simp
   238   then show "b = c" by simp
   239 qed
   240 
   241 subclass (in ab_group_add) cancel_ab_semigroup_add
   242 proof unfold_locales
   243   fix a b c :: 'a
   244   assume "a + b = a + c"
   245   then have "- a + a + b = - a + a + c"
   246     unfolding add_assoc by simp
   247   then show "b = c" by simp
   248 qed
   249 
   250 context ab_group_add
   251 begin
   252 
   253 lemma uminus_add_conv_diff:
   254   "- a + b = b - a"
   255   by (simp add:diff_minus add_commute)
   256 
   257 lemma minus_add_distrib [simp]:
   258   "- (a + b) = - a + - b"
   259   by (rule equals_zero_I) (simp add: add_ac)
   260 
   261 lemma minus_diff_eq [simp]:
   262   "- (a - b) = b - a"
   263   by (simp add: diff_minus add_commute)
   264 
   265 lemma add_diff_eq: "a + (b - c) = (a + b) - c"
   266   by (simp add: diff_minus add_ac)
   267 
   268 lemma diff_add_eq: "(a - b) + c = (a + c) - b"
   269   by (simp add: diff_minus add_ac)
   270 
   271 lemma diff_eq_eq: "a - b = c \<longleftrightarrow> a = c + b"
   272   by (auto simp add: diff_minus add_assoc)
   273 
   274 lemma eq_diff_eq: "a = c - b \<longleftrightarrow> a + b = c"
   275   by (auto simp add: diff_minus add_assoc)
   276 
   277 lemma diff_diff_eq: "(a - b) - c = a - (b + c)"
   278   by (simp add: diff_minus add_ac)
   279 
   280 lemma diff_diff_eq2: "a - (b - c) = (a + c) - b"
   281   by (simp add: diff_minus add_ac)
   282 
   283 lemma diff_add_cancel: "a - b + b = a"
   284   by (simp add: diff_minus add_ac)
   285 
   286 lemma add_diff_cancel: "a + b - b = a"
   287   by (simp add: diff_minus add_ac)
   288 
   289 lemmas compare_rls =
   290        diff_minus [symmetric]
   291        add_diff_eq diff_add_eq diff_diff_eq diff_diff_eq2
   292        diff_eq_eq eq_diff_eq
   293 
   294 lemma eq_iff_diff_eq_0: "a = b \<longleftrightarrow> a - b = 0"
   295   by (simp add: compare_rls)
   296 
   297 end
   298 
   299 subsection {* (Partially) Ordered Groups *} 
   300 
   301 class pordered_ab_semigroup_add = order + ab_semigroup_add +
   302   assumes add_left_mono: "a \<le> b \<Longrightarrow> c + a \<le> c + b"
   303 begin
   304 
   305 lemma add_right_mono:
   306   "a \<le> b \<Longrightarrow> a + c \<le> b + c"
   307   by (simp add: add_commute [of _ c] add_left_mono)
   308 
   309 text {* non-strict, in both arguments *}
   310 lemma add_mono:
   311   "a \<le> b \<Longrightarrow> c \<le> d \<Longrightarrow> a + c \<le> b + d"
   312   apply (erule add_right_mono [THEN order_trans])
   313   apply (simp add: add_commute add_left_mono)
   314   done
   315 
   316 end
   317 
   318 class pordered_cancel_ab_semigroup_add =
   319   pordered_ab_semigroup_add + cancel_ab_semigroup_add
   320 begin
   321 
   322 lemma add_strict_left_mono:
   323   "a < b \<Longrightarrow> c + a < c + b"
   324   by (auto simp add: less_le add_left_mono)
   325 
   326 lemma add_strict_right_mono:
   327   "a < b \<Longrightarrow> a + c < b + c"
   328   by (simp add: add_commute [of _ c] add_strict_left_mono)
   329 
   330 text{*Strict monotonicity in both arguments*}
   331 lemma add_strict_mono:
   332   "a < b \<Longrightarrow> c < d \<Longrightarrow> a + c < b + d"
   333 apply (erule add_strict_right_mono [THEN less_trans])
   334 apply (erule add_strict_left_mono)
   335 done
   336 
   337 lemma add_less_le_mono:
   338   "a < b \<Longrightarrow> c \<le> d \<Longrightarrow> a + c < b + d"
   339 apply (erule add_strict_right_mono [THEN less_le_trans])
   340 apply (erule add_left_mono)
   341 done
   342 
   343 lemma add_le_less_mono:
   344   "a \<le> b \<Longrightarrow> c < d \<Longrightarrow> a + c < b + d"
   345 apply (erule add_right_mono [THEN le_less_trans])
   346 apply (erule add_strict_left_mono) 
   347 done
   348 
   349 end
   350 
   351 class pordered_ab_semigroup_add_imp_le =
   352   pordered_cancel_ab_semigroup_add +
   353   assumes add_le_imp_le_left: "c + a \<le> c + b \<Longrightarrow> a \<le> b"
   354 begin
   355 
   356 lemma add_less_imp_less_left:
   357    assumes less: "c + a < c + b"
   358    shows "a < b"
   359 proof -
   360   from less have le: "c + a <= c + b" by (simp add: order_le_less)
   361   have "a <= b" 
   362     apply (insert le)
   363     apply (drule add_le_imp_le_left)
   364     by (insert le, drule add_le_imp_le_left, assumption)
   365   moreover have "a \<noteq> b"
   366   proof (rule ccontr)
   367     assume "~(a \<noteq> b)"
   368     then have "a = b" by simp
   369     then have "c + a = c + b" by simp
   370     with less show "False"by simp
   371   qed
   372   ultimately show "a < b" by (simp add: order_le_less)
   373 qed
   374 
   375 lemma add_less_imp_less_right:
   376   "a + c < b + c \<Longrightarrow> a < b"
   377 apply (rule add_less_imp_less_left [of c])
   378 apply (simp add: add_commute)  
   379 done
   380 
   381 lemma add_less_cancel_left [simp]:
   382   "c + a < c + b \<longleftrightarrow> a < b"
   383   by (blast intro: add_less_imp_less_left add_strict_left_mono) 
   384 
   385 lemma add_less_cancel_right [simp]:
   386   "a + c < b + c \<longleftrightarrow> a < b"
   387   by (blast intro: add_less_imp_less_right add_strict_right_mono)
   388 
   389 lemma add_le_cancel_left [simp]:
   390   "c + a \<le> c + b \<longleftrightarrow> a \<le> b"
   391   by (auto, drule add_le_imp_le_left, simp_all add: add_left_mono) 
   392 
   393 lemma add_le_cancel_right [simp]:
   394   "a + c \<le> b + c \<longleftrightarrow> a \<le> b"
   395   by (simp add: add_commute [of a c] add_commute [of b c])
   396 
   397 lemma add_le_imp_le_right:
   398   "a + c \<le> b + c \<Longrightarrow> a \<le> b"
   399   by simp
   400 
   401 lemma max_add_distrib_left:
   402   "max x y + z = max (x + z) (y + z)"
   403   unfolding max_def by auto
   404 
   405 lemma min_add_distrib_left:
   406   "min x y + z = min (x + z) (y + z)"
   407   unfolding min_def by auto
   408 
   409 end
   410 
   411 class pordered_ab_group_add =
   412   ab_group_add + pordered_ab_semigroup_add
   413 begin
   414 
   415 subclass pordered_cancel_ab_semigroup_add
   416   by unfold_locales
   417 
   418 subclass pordered_ab_semigroup_add_imp_le
   419 proof unfold_locales
   420   fix a b c :: 'a
   421   assume "c + a \<le> c + b"
   422   hence "(-c) + (c + a) \<le> (-c) + (c + b)" by (rule add_left_mono)
   423   hence "((-c) + c) + a \<le> ((-c) + c) + b" by (simp only: add_assoc)
   424   thus "a \<le> b" by simp
   425 qed
   426 
   427 end
   428 
   429 context pordered_ab_group_add
   430 begin
   431 
   432 lemma max_diff_distrib_left:
   433   shows "max x y - z = max (x - z) (y - z)"
   434   by (simp add: diff_minus, rule max_add_distrib_left) 
   435 
   436 lemma min_diff_distrib_left:
   437   shows "min x y - z = min (x - z) (y - z)"
   438   by (simp add: diff_minus, rule min_add_distrib_left) 
   439 
   440 lemma le_imp_neg_le:
   441   assumes "a \<le> b"
   442   shows "-b \<le> -a"
   443 proof -
   444   have "-a+a \<le> -a+b"
   445     using `a \<le> b` by (rule add_left_mono) 
   446   hence "0 \<le> -a+b"
   447     by simp
   448   hence "0 + (-b) \<le> (-a + b) + (-b)"
   449     by (rule add_right_mono) 
   450   thus ?thesis
   451     by (simp add: add_assoc)
   452 qed
   453 
   454 lemma neg_le_iff_le [simp]: "- b \<le> - a \<longleftrightarrow> a \<le> b"
   455 proof 
   456   assume "- b \<le> - a"
   457   hence "- (- a) \<le> - (- b)"
   458     by (rule le_imp_neg_le)
   459   thus "a\<le>b" by simp
   460 next
   461   assume "a\<le>b"
   462   thus "-b \<le> -a" by (rule le_imp_neg_le)
   463 qed
   464 
   465 lemma neg_le_0_iff_le [simp]: "- a \<le> 0 \<longleftrightarrow> 0 \<le> a"
   466   by (subst neg_le_iff_le [symmetric], simp)
   467 
   468 lemma neg_0_le_iff_le [simp]: "0 \<le> - a \<longleftrightarrow> a \<le> 0"
   469   by (subst neg_le_iff_le [symmetric], simp)
   470 
   471 lemma neg_less_iff_less [simp]: "- b < - a \<longleftrightarrow> a < b"
   472   by (force simp add: less_le) 
   473 
   474 lemma neg_less_0_iff_less [simp]: "- a < 0 \<longleftrightarrow> 0 < a"
   475   by (subst neg_less_iff_less [symmetric], simp)
   476 
   477 lemma neg_0_less_iff_less [simp]: "0 < - a \<longleftrightarrow> a < 0"
   478   by (subst neg_less_iff_less [symmetric], simp)
   479 
   480 text{*The next several equations can make the simplifier loop!*}
   481 
   482 lemma less_minus_iff: "a < - b \<longleftrightarrow> b < - a"
   483 proof -
   484   have "(- (-a) < - b) = (b < - a)" by (rule neg_less_iff_less)
   485   thus ?thesis by simp
   486 qed
   487 
   488 lemma minus_less_iff: "- a < b \<longleftrightarrow> - b < a"
   489 proof -
   490   have "(- a < - (-b)) = (- b < a)" by (rule neg_less_iff_less)
   491   thus ?thesis by simp
   492 qed
   493 
   494 lemma le_minus_iff: "a \<le> - b \<longleftrightarrow> b \<le> - a"
   495 proof -
   496   have mm: "!! a (b::'a). (-(-a)) < -b \<Longrightarrow> -(-b) < -a" by (simp only: minus_less_iff)
   497   have "(- (- a) <= -b) = (b <= - a)" 
   498     apply (auto simp only: le_less)
   499     apply (drule mm)
   500     apply (simp_all)
   501     apply (drule mm[simplified], assumption)
   502     done
   503   then show ?thesis by simp
   504 qed
   505 
   506 lemma minus_le_iff: "- a \<le> b \<longleftrightarrow> - b \<le> a"
   507   by (auto simp add: le_less minus_less_iff)
   508 
   509 lemma less_iff_diff_less_0: "a < b \<longleftrightarrow> a - b < 0"
   510 proof -
   511   have  "(a < b) = (a + (- b) < b + (-b))"  
   512     by (simp only: add_less_cancel_right)
   513   also have "... =  (a - b < 0)" by (simp add: diff_minus)
   514   finally show ?thesis .
   515 qed
   516 
   517 lemma diff_less_eq: "a - b < c \<longleftrightarrow> a < c + b"
   518 apply (subst less_iff_diff_less_0 [of a])
   519 apply (rule less_iff_diff_less_0 [of _ c, THEN ssubst])
   520 apply (simp add: diff_minus add_ac)
   521 done
   522 
   523 lemma less_diff_eq: "a < c - b \<longleftrightarrow> a + b < c"
   524 apply (subst less_iff_diff_less_0 [of "plus a b"])
   525 apply (subst less_iff_diff_less_0 [of a])
   526 apply (simp add: diff_minus add_ac)
   527 done
   528 
   529 lemma diff_le_eq: "a - b \<le> c \<longleftrightarrow> a \<le> c + b"
   530   by (auto simp add: le_less diff_less_eq diff_add_cancel add_diff_cancel)
   531 
   532 lemma le_diff_eq: "a \<le> c - b \<longleftrightarrow> a + b \<le> c"
   533   by (auto simp add: le_less less_diff_eq diff_add_cancel add_diff_cancel)
   534 
   535 lemmas compare_rls =
   536        diff_minus [symmetric]
   537        add_diff_eq diff_add_eq diff_diff_eq diff_diff_eq2
   538        diff_less_eq less_diff_eq diff_le_eq le_diff_eq
   539        diff_eq_eq eq_diff_eq
   540 
   541 text{*This list of rewrites simplifies (in)equalities by bringing subtractions
   542   to the top and then moving negative terms to the other side.
   543   Use with @{text add_ac}*}
   544 lemmas (in -) compare_rls =
   545        diff_minus [symmetric]
   546        add_diff_eq diff_add_eq diff_diff_eq diff_diff_eq2
   547        diff_less_eq less_diff_eq diff_le_eq le_diff_eq
   548        diff_eq_eq eq_diff_eq
   549 
   550 lemma le_iff_diff_le_0: "a \<le> b \<longleftrightarrow> a - b \<le> 0"
   551   by (simp add: compare_rls)
   552 
   553 end
   554 
   555 class ordered_ab_semigroup_add =
   556   linorder + pordered_ab_semigroup_add
   557 
   558 class ordered_cancel_ab_semigroup_add =
   559   linorder + pordered_cancel_ab_semigroup_add
   560 
   561 subclass (in ordered_cancel_ab_semigroup_add) ordered_ab_semigroup_add
   562   by unfold_locales
   563 
   564 subclass (in ordered_cancel_ab_semigroup_add) pordered_ab_semigroup_add_imp_le
   565 proof unfold_locales
   566   fix a b c :: 'a
   567   assume le: "c + a <= c + b"  
   568   show "a <= b"
   569   proof (rule ccontr)
   570     assume w: "~ a \<le> b"
   571     hence "b <= a" by (simp add: linorder_not_le)
   572     hence le2: "c + b <= c + a" by (rule add_left_mono)
   573     have "a = b" 
   574       apply (insert le)
   575       apply (insert le2)
   576       apply (drule antisym, simp_all)
   577       done
   578     with w show False 
   579       by (simp add: linorder_not_le [symmetric])
   580   qed
   581 qed
   582 
   583 -- {* FIXME localize the following *}
   584 
   585 lemma add_increasing:
   586   fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
   587   shows  "[|0\<le>a; b\<le>c|] ==> b \<le> a + c"
   588 by (insert add_mono [of 0 a b c], simp)
   589 
   590 lemma add_increasing2:
   591   fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
   592   shows  "[|0\<le>c; b\<le>a|] ==> b \<le> a + c"
   593 by (simp add:add_increasing add_commute[of a])
   594 
   595 lemma add_strict_increasing:
   596   fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
   597   shows "[|0<a; b\<le>c|] ==> b < a + c"
   598 by (insert add_less_le_mono [of 0 a b c], simp)
   599 
   600 lemma add_strict_increasing2:
   601   fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
   602   shows "[|0\<le>a; b<c|] ==> b < a + c"
   603 by (insert add_le_less_mono [of 0 a b c], simp)
   604 
   605 
   606 subsection {* Support for reasoning about signs *}
   607 
   608 lemma add_pos_pos: "0 < 
   609     (x::'a::{comm_monoid_add,pordered_cancel_ab_semigroup_add}) 
   610       ==> 0 < y ==> 0 < x + y"
   611 apply (subgoal_tac "0 + 0 < x + y")
   612 apply simp
   613 apply (erule add_less_le_mono)
   614 apply (erule order_less_imp_le)
   615 done
   616 
   617 lemma add_pos_nonneg: "0 < 
   618     (x::'a::{comm_monoid_add,pordered_cancel_ab_semigroup_add}) 
   619       ==> 0 <= y ==> 0 < x + y"
   620 apply (subgoal_tac "0 + 0 < x + y")
   621 apply simp
   622 apply (erule add_less_le_mono, assumption)
   623 done
   624 
   625 lemma add_nonneg_pos: "0 <= 
   626     (x::'a::{comm_monoid_add,pordered_cancel_ab_semigroup_add}) 
   627       ==> 0 < y ==> 0 < x + y"
   628 apply (subgoal_tac "0 + 0 < x + y")
   629 apply simp
   630 apply (erule add_le_less_mono, assumption)
   631 done
   632 
   633 lemma add_nonneg_nonneg: "0 <= 
   634     (x::'a::{comm_monoid_add,pordered_cancel_ab_semigroup_add}) 
   635       ==> 0 <= y ==> 0 <= x + y"
   636 apply (subgoal_tac "0 + 0 <= x + y")
   637 apply simp
   638 apply (erule add_mono, assumption)
   639 done
   640 
   641 lemma add_neg_neg: "(x::'a::{comm_monoid_add,pordered_cancel_ab_semigroup_add})
   642     < 0 ==> y < 0 ==> x + y < 0"
   643 apply (subgoal_tac "x + y < 0 + 0")
   644 apply simp
   645 apply (erule add_less_le_mono)
   646 apply (erule order_less_imp_le)
   647 done
   648 
   649 lemma add_neg_nonpos: 
   650     "(x::'a::{comm_monoid_add,pordered_cancel_ab_semigroup_add}) < 0 
   651       ==> y <= 0 ==> x + y < 0"
   652 apply (subgoal_tac "x + y < 0 + 0")
   653 apply simp
   654 apply (erule add_less_le_mono, assumption)
   655 done
   656 
   657 lemma add_nonpos_neg: 
   658     "(x::'a::{comm_monoid_add,pordered_cancel_ab_semigroup_add}) <= 0 
   659       ==> y < 0 ==> x + y < 0"
   660 apply (subgoal_tac "x + y < 0 + 0")
   661 apply simp
   662 apply (erule add_le_less_mono, assumption)
   663 done
   664 
   665 lemma add_nonpos_nonpos: 
   666     "(x::'a::{comm_monoid_add,pordered_cancel_ab_semigroup_add}) <= 0 
   667       ==> y <= 0 ==> x + y <= 0"
   668 apply (subgoal_tac "x + y <= 0 + 0")
   669 apply simp
   670 apply (erule add_mono, assumption)
   671 done
   672 
   673 
   674 subsection {* Lattice Ordered (Abelian) Groups *}
   675 
   676 class lordered_ab_group_meet = pordered_ab_group_add + lower_semilattice
   677 
   678 class lordered_ab_group_join = pordered_ab_group_add + upper_semilattice
   679 
   680 class lordered_ab_group = pordered_ab_group_add + lattice
   681 
   682 instance lordered_ab_group \<subseteq> lordered_ab_group_meet by default
   683 instance lordered_ab_group \<subseteq> lordered_ab_group_join by default
   684 
   685 lemma add_inf_distrib_left: "a + inf b c = inf (a + b) (a + (c::'a::{pordered_ab_group_add, lower_semilattice}))"
   686 apply (rule order_antisym)
   687 apply (simp_all add: le_infI)
   688 apply (rule add_le_imp_le_left [of "-a"])
   689 apply (simp only: add_assoc[symmetric], simp)
   690 apply rule
   691 apply (rule add_le_imp_le_left[of "a"], simp only: add_assoc[symmetric], simp)+
   692 done
   693 
   694 lemma add_sup_distrib_left: "a + sup b c = sup (a + b) (a+ (c::'a::{pordered_ab_group_add, upper_semilattice}))" 
   695 apply (rule order_antisym)
   696 apply (rule add_le_imp_le_left [of "-a"])
   697 apply (simp only: add_assoc[symmetric], simp)
   698 apply rule
   699 apply (rule add_le_imp_le_left [of "a"], simp only: add_assoc[symmetric], simp)+
   700 apply (rule le_supI)
   701 apply (simp_all)
   702 done
   703 
   704 lemma add_inf_distrib_right: "inf a b + (c::'a::lordered_ab_group) = inf (a+c) (b+c)"
   705 proof -
   706   have "c + inf a b = inf (c+a) (c+b)" by (simp add: add_inf_distrib_left)
   707   thus ?thesis by (simp add: add_commute)
   708 qed
   709 
   710 lemma add_sup_distrib_right: "sup a b + (c::'a::lordered_ab_group) = sup (a+c) (b+c)"
   711 proof -
   712   have "c + sup a b = sup (c+a) (c+b)" by (simp add: add_sup_distrib_left)
   713   thus ?thesis by (simp add: add_commute)
   714 qed
   715 
   716 lemmas add_sup_inf_distribs = add_inf_distrib_right add_inf_distrib_left add_sup_distrib_right add_sup_distrib_left
   717 
   718 lemma inf_eq_neg_sup: "inf a (b\<Colon>'a\<Colon>lordered_ab_group) = - sup (-a) (-b)"
   719 proof (rule inf_unique)
   720   fix a b :: 'a
   721   show "- sup (-a) (-b) \<le> a" by (rule add_le_imp_le_right [of _ "sup (-a) (-b)"])
   722     (simp, simp add: add_sup_distrib_left)
   723 next
   724   fix a b :: 'a
   725   show "- sup (-a) (-b) \<le> b" by (rule add_le_imp_le_right [of _ "sup (-a) (-b)"])
   726     (simp, simp add: add_sup_distrib_left)
   727 next
   728   fix a b c :: 'a
   729   assume "a \<le> b" "a \<le> c"
   730   then show "a \<le> - sup (-b) (-c)" by (subst neg_le_iff_le [symmetric])
   731     (simp add: le_supI)
   732 qed
   733   
   734 lemma sup_eq_neg_inf: "sup a (b\<Colon>'a\<Colon>lordered_ab_group) = - inf (-a) (-b)"
   735 proof (rule sup_unique)
   736   fix a b :: 'a
   737   show "a \<le> - inf (-a) (-b)" by (rule add_le_imp_le_right [of _ "inf (-a) (-b)"])
   738     (simp, simp add: add_inf_distrib_left)
   739 next
   740   fix a b :: 'a
   741   show "b \<le> - inf (-a) (-b)" by (rule add_le_imp_le_right [of _ "inf (-a) (-b)"])
   742     (simp, simp add: add_inf_distrib_left)
   743 next
   744   fix a b c :: 'a
   745   assume "a \<le> c" "b \<le> c"
   746   then show "- inf (-a) (-b) \<le> c" by (subst neg_le_iff_le [symmetric])
   747     (simp add: le_infI)
   748 qed
   749 
   750 lemma add_eq_inf_sup: "a + b = sup a b + inf a (b\<Colon>'a\<Colon>lordered_ab_group)"
   751 proof -
   752   have "0 = - inf 0 (a-b) + inf (a-b) 0" by (simp add: inf_commute)
   753   hence "0 = sup 0 (b-a) + inf (a-b) 0" by (simp add: inf_eq_neg_sup)
   754   hence "0 = (-a + sup a b) + (inf a b + (-b))"
   755     apply (simp add: add_sup_distrib_left add_inf_distrib_right)
   756     by (simp add: diff_minus add_commute)
   757   thus ?thesis
   758     apply (simp add: compare_rls)
   759     apply (subst add_left_cancel[symmetric, of "a+b" "sup a b + inf a b" "-a"])
   760     apply (simp only: add_assoc, simp add: add_assoc[symmetric])
   761     done
   762 qed
   763 
   764 subsection {* Positive Part, Negative Part, Absolute Value *}
   765 
   766 definition
   767   nprt :: "'a \<Rightarrow> ('a::lordered_ab_group)" where
   768   "nprt x = inf x 0"
   769 
   770 definition
   771   pprt :: "'a \<Rightarrow> ('a::lordered_ab_group)" where
   772   "pprt x = sup x 0"
   773 
   774 lemma prts: "a = pprt a + nprt a"
   775 by (simp add: pprt_def nprt_def add_eq_inf_sup[symmetric])
   776 
   777 lemma zero_le_pprt[simp]: "0 \<le> pprt a"
   778 by (simp add: pprt_def)
   779 
   780 lemma nprt_le_zero[simp]: "nprt a \<le> 0"
   781 by (simp add: nprt_def)
   782 
   783 lemma le_eq_neg: "(a \<le> -b) = (a + b \<le> (0::_::lordered_ab_group))" (is "?l = ?r")
   784 proof -
   785   have a: "?l \<longrightarrow> ?r"
   786     apply (auto)
   787     apply (rule add_le_imp_le_right[of _ "-b" _])
   788     apply (simp add: add_assoc)
   789     done
   790   have b: "?r \<longrightarrow> ?l"
   791     apply (auto)
   792     apply (rule add_le_imp_le_right[of _ "b" _])
   793     apply (simp)
   794     done
   795   from a b show ?thesis by blast
   796 qed
   797 
   798 lemma pprt_0[simp]: "pprt 0 = 0" by (simp add: pprt_def)
   799 lemma nprt_0[simp]: "nprt 0 = 0" by (simp add: nprt_def)
   800 
   801 lemma pprt_eq_id[simp,noatp]: "0 <= x \<Longrightarrow> pprt x = x"
   802   by (simp add: pprt_def le_iff_sup sup_aci)
   803 
   804 lemma nprt_eq_id[simp,noatp]: "x <= 0 \<Longrightarrow> nprt x = x"
   805   by (simp add: nprt_def le_iff_inf inf_aci)
   806 
   807 lemma pprt_eq_0[simp,noatp]: "x <= 0 \<Longrightarrow> pprt x = 0"
   808   by (simp add: pprt_def le_iff_sup sup_aci)
   809 
   810 lemma nprt_eq_0[simp,noatp]: "0 <= x \<Longrightarrow> nprt x = 0"
   811   by (simp add: nprt_def le_iff_inf inf_aci)
   812 
   813 lemma sup_0_imp_0: "sup a (-a) = 0 \<Longrightarrow> a = (0::'a::lordered_ab_group)"
   814 proof -
   815   {
   816     fix a::'a
   817     assume hyp: "sup a (-a) = 0"
   818     hence "sup a (-a) + a = a" by (simp)
   819     hence "sup (a+a) 0 = a" by (simp add: add_sup_distrib_right) 
   820     hence "sup (a+a) 0 <= a" by (simp)
   821     hence "0 <= a" by (blast intro: order_trans inf_sup_ord)
   822   }
   823   note p = this
   824   assume hyp:"sup a (-a) = 0"
   825   hence hyp2:"sup (-a) (-(-a)) = 0" by (simp add: sup_commute)
   826   from p[OF hyp] p[OF hyp2] show "a = 0" by simp
   827 qed
   828 
   829 lemma inf_0_imp_0: "inf a (-a) = 0 \<Longrightarrow> a = (0::'a::lordered_ab_group)"
   830 apply (simp add: inf_eq_neg_sup)
   831 apply (simp add: sup_commute)
   832 apply (erule sup_0_imp_0)
   833 done
   834 
   835 lemma inf_0_eq_0[simp,noatp]: "(inf a (-a) = 0) = (a = (0::'a::lordered_ab_group))"
   836 by (auto, erule inf_0_imp_0)
   837 
   838 lemma sup_0_eq_0[simp,noatp]: "(sup a (-a) = 0) = (a = (0::'a::lordered_ab_group))"
   839 by (auto, erule sup_0_imp_0)
   840 
   841 lemma zero_le_double_add_iff_zero_le_single_add[simp]: "(0 \<le> a + a) = (0 \<le> (a::'a::lordered_ab_group))"
   842 proof
   843   assume "0 <= a + a"
   844   hence a:"inf (a+a) 0 = 0" by (simp add: le_iff_inf inf_commute)
   845   have "(inf a 0)+(inf a 0) = inf (inf (a+a) 0) a" (is "?l=_") by (simp add: add_sup_inf_distribs inf_aci)
   846   hence "?l = 0 + inf a 0" by (simp add: a, simp add: inf_commute)
   847   hence "inf a 0 = 0" by (simp only: add_right_cancel)
   848   then show "0 <= a" by (simp add: le_iff_inf inf_commute)    
   849 next  
   850   assume a: "0 <= a"
   851   show "0 <= a + a" by (simp add: add_mono[OF a a, simplified])
   852 qed
   853 
   854 lemma double_add_le_zero_iff_single_add_le_zero[simp]: "(a + a <= 0) = ((a::'a::lordered_ab_group) <= 0)" 
   855 proof -
   856   have "(a + a <= 0) = (0 <= -(a+a))" by (subst le_minus_iff, simp)
   857   moreover have "\<dots> = (a <= 0)" by (simp add: zero_le_double_add_iff_zero_le_single_add)
   858   ultimately show ?thesis by blast
   859 qed
   860 
   861 lemma double_add_less_zero_iff_single_less_zero[simp]: "(a+a<0) = ((a::'a::{pordered_ab_group_add,linorder}) < 0)" (is ?s)
   862 proof cases
   863   assume a: "a < 0"
   864   thus ?s by (simp add:  add_strict_mono[OF a a, simplified])
   865 next
   866   assume "~(a < 0)" 
   867   hence a:"0 <= a" by (simp)
   868   hence "0 <= a+a" by (simp add: add_mono[OF a a, simplified])
   869   hence "~(a+a < 0)" by simp
   870   with a show ?thesis by simp 
   871 qed
   872 
   873 class lordered_ab_group_abs = lordered_ab_group + abs +
   874   assumes abs_lattice: "abs x = sup x (uminus x)"
   875 
   876 lemma abs_zero[simp]: "abs 0 = (0::'a::lordered_ab_group_abs)"
   877 by (simp add: abs_lattice)
   878 
   879 lemma abs_eq_0[simp]: "(abs a = 0) = (a = (0::'a::lordered_ab_group_abs))"
   880 by (simp add: abs_lattice)
   881 
   882 lemma abs_0_eq[simp]: "(0 = abs a) = (a = (0::'a::lordered_ab_group_abs))"
   883 proof -
   884   have "(0 = abs a) = (abs a = 0)" by (simp only: eq_ac)
   885   thus ?thesis by simp
   886 qed
   887 
   888 declare abs_0_eq [noatp] (*essentially the same as the other one*)
   889 
   890 lemma neg_inf_eq_sup[simp]: "- inf a (b::_::lordered_ab_group) = sup (-a) (-b)"
   891 by (simp add: inf_eq_neg_sup)
   892 
   893 lemma neg_sup_eq_inf[simp]: "- sup a (b::_::lordered_ab_group) = inf (-a) (-b)"
   894 by (simp del: neg_inf_eq_sup add: sup_eq_neg_inf)
   895 
   896 lemma sup_eq_if: "sup a (-a) = (if a < 0 then -a else (a::'a::{lordered_ab_group, linorder}))"
   897 proof -
   898   note b = add_le_cancel_right[of a a "-a",symmetric,simplified]
   899   have c: "a + a = 0 \<Longrightarrow> -a = a" by (rule add_right_imp_eq[of _ a], simp)
   900   show ?thesis by (auto simp add: max_def b linorder_not_less sup_max)
   901 qed
   902 
   903 lemma abs_if_lattice: "\<bar>a\<bar> = (if a < 0 then -a else (a::'a::{lordered_ab_group_abs, linorder}))"
   904 proof -
   905   show ?thesis by (simp add: abs_lattice sup_eq_if)
   906 qed
   907 
   908 lemma abs_ge_zero[simp]: "0 \<le> abs (a::'a::lordered_ab_group_abs)"
   909 proof -
   910   have a:"a <= abs a" and b:"-a <= abs a" by (auto simp add: abs_lattice)
   911   show ?thesis by (rule add_mono[OF a b, simplified])
   912 qed
   913   
   914 lemma abs_le_zero_iff [simp]: "(abs a \<le> (0::'a::lordered_ab_group_abs)) = (a = 0)" 
   915 proof
   916   assume "abs a <= 0"
   917   hence "abs a = 0" by (auto dest: order_antisym)
   918   thus "a = 0" by simp
   919 next
   920   assume "a = 0"
   921   thus "abs a <= 0" by simp
   922 qed
   923 
   924 lemma zero_less_abs_iff [simp]: "(0 < abs a) = (a \<noteq> (0::'a::lordered_ab_group_abs))"
   925 by (simp add: order_less_le)
   926 
   927 lemma abs_not_less_zero [simp]: "~ abs a < (0::'a::lordered_ab_group_abs)"
   928 proof -
   929   have a:"!! x (y::_::order). x <= y \<Longrightarrow> ~(y < x)" by auto
   930   show ?thesis by (simp add: a)
   931 qed
   932 
   933 lemma abs_ge_self: "a \<le> abs (a::'a::lordered_ab_group_abs)"
   934 by (simp add: abs_lattice)
   935 
   936 lemma abs_ge_minus_self: "-a \<le> abs (a::'a::lordered_ab_group_abs)"
   937 by (simp add: abs_lattice)
   938 
   939 lemma abs_prts: "abs (a::_::lordered_ab_group_abs) = pprt a - nprt a"
   940 apply (simp add: pprt_def nprt_def diff_minus)
   941 apply (simp add: add_sup_inf_distribs sup_aci abs_lattice[symmetric])
   942 apply (subst sup_absorb2, auto)
   943 done
   944 
   945 lemma abs_minus_cancel [simp]: "abs (-a) = abs(a::'a::lordered_ab_group_abs)"
   946 by (simp add: abs_lattice sup_commute)
   947 
   948 lemma abs_idempotent [simp]: "abs (abs a) = abs (a::'a::lordered_ab_group_abs)"
   949 apply (simp add: abs_lattice[of "abs a"])
   950 apply (subst sup_absorb1)
   951 apply (rule order_trans[of _ 0])
   952 by auto
   953 
   954 lemma abs_minus_commute: 
   955   fixes a :: "'a::lordered_ab_group_abs"
   956   shows "abs (a-b) = abs(b-a)"
   957 proof -
   958   have "abs (a-b) = abs (- (a-b))" by (simp only: abs_minus_cancel)
   959   also have "... = abs(b-a)" by simp
   960   finally show ?thesis .
   961 qed
   962 
   963 lemma zero_le_iff_zero_nprt: "(0 \<le> a) = (nprt a = 0)"
   964 by (simp add: le_iff_inf nprt_def inf_commute)
   965 
   966 lemma le_zero_iff_zero_pprt: "(a \<le> 0) = (pprt a = 0)"
   967 by (simp add: le_iff_sup pprt_def sup_commute)
   968 
   969 lemma le_zero_iff_pprt_id: "(0 \<le> a) = (pprt a = a)"
   970 by (simp add: le_iff_sup pprt_def sup_commute)
   971 
   972 lemma zero_le_iff_nprt_id: "(a \<le> 0) = (nprt a = a)"
   973 by (simp add: le_iff_inf nprt_def inf_commute)
   974 
   975 lemma pprt_mono[simp,noatp]: "(a::_::lordered_ab_group) <= b \<Longrightarrow> pprt a <= pprt b"
   976   by (simp add: le_iff_sup pprt_def sup_aci)
   977 
   978 lemma nprt_mono[simp,noatp]: "(a::_::lordered_ab_group) <= b \<Longrightarrow> nprt a <= nprt b"
   979   by (simp add: le_iff_inf nprt_def inf_aci)
   980 
   981 lemma pprt_neg: "pprt (-x) = - nprt x"
   982   by (simp add: pprt_def nprt_def)
   983 
   984 lemma nprt_neg: "nprt (-x) = - pprt x"
   985   by (simp add: pprt_def nprt_def)
   986 
   987 lemma abs_of_nonneg [simp]: "0 \<le> a \<Longrightarrow> abs a = (a::'a::lordered_ab_group_abs)"
   988 by (simp add: iffD1[OF zero_le_iff_zero_nprt] iffD1[OF le_zero_iff_pprt_id] abs_prts)
   989 
   990 lemma abs_of_pos: "0 < (x::'a::lordered_ab_group_abs) ==> abs x = x";
   991 by (rule abs_of_nonneg, rule order_less_imp_le);
   992 
   993 lemma abs_of_nonpos [simp]: "a \<le> 0 \<Longrightarrow> abs a = -(a::'a::lordered_ab_group_abs)"
   994 by (simp add: iffD1[OF le_zero_iff_zero_pprt] iffD1[OF zero_le_iff_nprt_id] abs_prts)
   995 
   996 lemma abs_of_neg: "(x::'a::lordered_ab_group_abs) <  0 ==> 
   997   abs x = - x"
   998 by (rule abs_of_nonpos, rule order_less_imp_le)
   999 
  1000 lemma abs_leI: "[|a \<le> b; -a \<le> b|] ==> abs a \<le> (b::'a::lordered_ab_group_abs)"
  1001 by (simp add: abs_lattice le_supI)
  1002 
  1003 lemma le_minus_self_iff: "(a \<le> -a) = (a \<le> (0::'a::lordered_ab_group))"
  1004 proof -
  1005   from add_le_cancel_left[of "-a" "a+a" "0"] have "(a <= -a) = (a+a <= 0)" 
  1006     by (simp add: add_assoc[symmetric])
  1007   thus ?thesis by simp
  1008 qed
  1009 
  1010 lemma minus_le_self_iff: "(-a \<le> a) = (0 \<le> (a::'a::lordered_ab_group))"
  1011 proof -
  1012   from add_le_cancel_left[of "-a" "0" "a+a"] have "(-a <= a) = (0 <= a+a)" 
  1013     by (simp add: add_assoc[symmetric])
  1014   thus ?thesis by simp
  1015 qed
  1016 
  1017 lemma abs_le_D1: "abs a \<le> b ==> a \<le> (b::'a::lordered_ab_group_abs)"
  1018 by (insert abs_ge_self, blast intro: order_trans)
  1019 
  1020 lemma abs_le_D2: "abs a \<le> b ==> -a \<le> (b::'a::lordered_ab_group_abs)"
  1021 by (insert abs_le_D1 [of "-a"], simp)
  1022 
  1023 lemma abs_le_iff: "(abs a \<le> b) = (a \<le> b & -a \<le> (b::'a::lordered_ab_group_abs))"
  1024 by (blast intro: abs_leI dest: abs_le_D1 abs_le_D2)
  1025 
  1026 lemma abs_triangle_ineq: "abs(a+b) \<le> abs a + abs(b::'a::lordered_ab_group_abs)"
  1027 proof -
  1028   have g:"abs a + abs b = sup (a+b) (sup (-a-b) (sup (-a+b) (a + (-b))))" (is "_=sup ?m ?n")
  1029     by (simp add: abs_lattice add_sup_inf_distribs sup_aci diff_minus)
  1030   have a:"a+b <= sup ?m ?n" by (simp)
  1031   have b:"-a-b <= ?n" by (simp) 
  1032   have c:"?n <= sup ?m ?n" by (simp)
  1033   from b c have d: "-a-b <= sup ?m ?n" by(rule order_trans)
  1034   have e:"-a-b = -(a+b)" by (simp add: diff_minus)
  1035   from a d e have "abs(a+b) <= sup ?m ?n" 
  1036     by (drule_tac abs_leI, auto)
  1037   with g[symmetric] show ?thesis by simp
  1038 qed
  1039 
  1040 lemma abs_triangle_ineq2: "abs (a::'a::lordered_ab_group_abs) - 
  1041     abs b <= abs (a - b)"
  1042   apply (simp add: compare_rls)
  1043   apply (subgoal_tac "abs a = abs (a - b + b)")
  1044   apply (erule ssubst)
  1045   apply (rule abs_triangle_ineq)
  1046   apply (rule arg_cong);back;
  1047   apply (simp add: compare_rls)
  1048 done
  1049 
  1050 lemma abs_triangle_ineq3: 
  1051     "abs(abs (a::'a::lordered_ab_group_abs) - abs b) <= abs (a - b)"
  1052   apply (subst abs_le_iff)
  1053   apply auto
  1054   apply (rule abs_triangle_ineq2)
  1055   apply (subst abs_minus_commute)
  1056   apply (rule abs_triangle_ineq2)
  1057 done
  1058 
  1059 lemma abs_triangle_ineq4: "abs ((a::'a::lordered_ab_group_abs) - b) <= 
  1060     abs a + abs b"
  1061 proof -;
  1062   have "abs(a - b) = abs(a + - b)"
  1063     by (subst diff_minus, rule refl)
  1064   also have "... <= abs a + abs (- b)"
  1065     by (rule abs_triangle_ineq)
  1066   finally show ?thesis
  1067     by simp
  1068 qed
  1069 
  1070 lemma abs_diff_triangle_ineq:
  1071      "\<bar>(a::'a::lordered_ab_group_abs) + b - (c+d)\<bar> \<le> \<bar>a-c\<bar> + \<bar>b-d\<bar>"
  1072 proof -
  1073   have "\<bar>a + b - (c+d)\<bar> = \<bar>(a-c) + (b-d)\<bar>" by (simp add: diff_minus add_ac)
  1074   also have "... \<le> \<bar>a-c\<bar> + \<bar>b-d\<bar>" by (rule abs_triangle_ineq)
  1075   finally show ?thesis .
  1076 qed
  1077 
  1078 lemma abs_add_abs[simp]:
  1079 fixes a:: "'a::{lordered_ab_group_abs}"
  1080 shows "abs(abs a + abs b) = abs a + abs b" (is "?L = ?R")
  1081 proof (rule order_antisym)
  1082   show "?L \<ge> ?R" by(rule abs_ge_self)
  1083 next
  1084   have "?L \<le> \<bar>\<bar>a\<bar>\<bar> + \<bar>\<bar>b\<bar>\<bar>" by(rule abs_triangle_ineq)
  1085   also have "\<dots> = ?R" by simp
  1086   finally show "?L \<le> ?R" .
  1087 qed
  1088 
  1089 text {* Needed for abelian cancellation simprocs: *}
  1090 
  1091 lemma add_cancel_21: "((x::'a::ab_group_add) + (y + z) = y + u) = (x + z = u)"
  1092 apply (subst add_left_commute)
  1093 apply (subst add_left_cancel)
  1094 apply simp
  1095 done
  1096 
  1097 lemma add_cancel_end: "(x + (y + z) = y) = (x = - (z::'a::ab_group_add))"
  1098 apply (subst add_cancel_21[of _ _ _ 0, simplified])
  1099 apply (simp add: add_right_cancel[symmetric, of "x" "-z" "z", simplified])
  1100 done
  1101 
  1102 lemma less_eqI: "(x::'a::pordered_ab_group_add) - y = x' - y' \<Longrightarrow> (x < y) = (x' < y')"
  1103 by (simp add: less_iff_diff_less_0[of x y] less_iff_diff_less_0[of x' y'])
  1104 
  1105 lemma le_eqI: "(x::'a::pordered_ab_group_add) - y = x' - y' \<Longrightarrow> (y <= x) = (y' <= x')"
  1106 apply (simp add: le_iff_diff_le_0[of y x] le_iff_diff_le_0[of  y' x'])
  1107 apply (simp add: neg_le_iff_le[symmetric, of "y-x" 0] neg_le_iff_le[symmetric, of "y'-x'" 0])
  1108 done
  1109 
  1110 lemma eq_eqI: "(x::'a::ab_group_add) - y = x' - y' \<Longrightarrow> (x = y) = (x' = y')"
  1111 by (simp add: eq_iff_diff_eq_0[of x y] eq_iff_diff_eq_0[of x' y'])
  1112 
  1113 lemma diff_def: "(x::'a::ab_group_add) - y == x + (-y)"
  1114 by (simp add: diff_minus)
  1115 
  1116 lemma add_minus_cancel: "(a::'a::ab_group_add) + (-a + b) = b"
  1117 by (simp add: add_assoc[symmetric])
  1118 
  1119 lemma  le_add_right_mono: 
  1120   assumes 
  1121   "a <= b + (c::'a::pordered_ab_group_add)"
  1122   "c <= d"    
  1123   shows "a <= b + d"
  1124   apply (rule_tac order_trans[where y = "b+c"])
  1125   apply (simp_all add: prems)
  1126   done
  1127 
  1128 lemmas group_simps =
  1129   mult_ac
  1130   add_ac
  1131   add_diff_eq diff_add_eq diff_diff_eq diff_diff_eq2
  1132   diff_eq_eq eq_diff_eq  diff_minus[symmetric] uminus_add_conv_diff
  1133   diff_less_eq less_diff_eq diff_le_eq le_diff_eq
  1134 
  1135 lemma estimate_by_abs:
  1136   "a + b <= (c::'a::lordered_ab_group_abs) \<Longrightarrow> a <= c + abs b" 
  1137 proof -
  1138   assume "a+b <= c"
  1139   hence 2: "a <= c+(-b)" by (simp add: group_simps)
  1140   have 3: "(-b) <= abs b" by (rule abs_ge_minus_self)
  1141   show ?thesis by (rule le_add_right_mono[OF 2 3])
  1142 qed
  1143 
  1144 lemma add_mono_thms_ordered_semiring [noatp]:
  1145   fixes i j k :: "'a\<Colon>pordered_ab_semigroup_add"
  1146   shows "i \<le> j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
  1147     and "i = j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
  1148     and "i \<le> j \<and> k = l \<Longrightarrow> i + k \<le> j + l"
  1149     and "i = j \<and> k = l \<Longrightarrow> i + k = j + l"
  1150 by (rule add_mono, clarify+)+
  1151 
  1152 lemma add_mono_thms_ordered_field [noatp]:
  1153   fixes i j k :: "'a\<Colon>pordered_cancel_ab_semigroup_add"
  1154   shows "i < j \<and> k = l \<Longrightarrow> i + k < j + l"
  1155     and "i = j \<and> k < l \<Longrightarrow> i + k < j + l"
  1156     and "i < j \<and> k \<le> l \<Longrightarrow> i + k < j + l"
  1157     and "i \<le> j \<and> k < l \<Longrightarrow> i + k < j + l"
  1158     and "i < j \<and> k < l \<Longrightarrow> i + k < j + l"
  1159 by (auto intro: add_strict_right_mono add_strict_left_mono
  1160   add_less_le_mono add_le_less_mono add_strict_mono)
  1161 
  1162 
  1163 subsection {* Tools setup *}
  1164 
  1165 text{*Simplification of @{term "x-y < 0"}, etc.*}
  1166 lemmas diff_less_0_iff_less [simp] = less_iff_diff_less_0 [symmetric]
  1167 lemmas diff_eq_0_iff_eq [simp, noatp] = eq_iff_diff_eq_0 [symmetric]
  1168 lemmas diff_le_0_iff_le [simp] = le_iff_diff_le_0 [symmetric]
  1169 
  1170 ML {*
  1171 structure ab_group_add_cancel = Abel_Cancel(
  1172 struct
  1173 
  1174 (* term order for abelian groups *)
  1175 
  1176 fun agrp_ord (Const (a, _)) = find_index (fn a' => a = a')
  1177       [@{const_name HOL.zero}, @{const_name HOL.plus},
  1178         @{const_name HOL.uminus}, @{const_name HOL.minus}]
  1179   | agrp_ord _ = ~1;
  1180 
  1181 fun termless_agrp (a, b) = (Term.term_lpo agrp_ord (a, b) = LESS);
  1182 
  1183 local
  1184   val ac1 = mk_meta_eq @{thm add_assoc};
  1185   val ac2 = mk_meta_eq @{thm add_commute};
  1186   val ac3 = mk_meta_eq @{thm add_left_commute};
  1187   fun solve_add_ac thy _ (_ $ (Const (@{const_name HOL.plus},_) $ _ $ _) $ _) =
  1188         SOME ac1
  1189     | solve_add_ac thy _ (_ $ x $ (Const (@{const_name HOL.plus},_) $ y $ z)) =
  1190         if termless_agrp (y, x) then SOME ac3 else NONE
  1191     | solve_add_ac thy _ (_ $ x $ y) =
  1192         if termless_agrp (y, x) then SOME ac2 else NONE
  1193     | solve_add_ac thy _ _ = NONE
  1194 in
  1195   val add_ac_proc = Simplifier.simproc @{theory}
  1196     "add_ac_proc" ["x + y::'a::ab_semigroup_add"] solve_add_ac;
  1197 end;
  1198 
  1199 val cancel_ss = HOL_basic_ss settermless termless_agrp
  1200   addsimprocs [add_ac_proc] addsimps
  1201   [@{thm add_0_left}, @{thm add_0_right}, @{thm diff_def},
  1202    @{thm minus_add_distrib}, @{thm minus_minus}, @{thm minus_zero},
  1203    @{thm right_minus}, @{thm left_minus}, @{thm add_minus_cancel},
  1204    @{thm minus_add_cancel}];
  1205   
  1206 val eq_reflection = @{thm eq_reflection};
  1207   
  1208 val thy_ref = Theory.check_thy @{theory};
  1209 
  1210 val T = @{typ "'a\<Colon>ab_group_add"};
  1211 
  1212 val eqI_rules = [@{thm less_eqI}, @{thm le_eqI}, @{thm eq_eqI}];
  1213 
  1214 val dest_eqI = 
  1215   fst o HOLogic.dest_bin "op =" HOLogic.boolT o HOLogic.dest_Trueprop o concl_of;
  1216 
  1217 end);
  1218 *}
  1219 
  1220 ML_setup {*
  1221   Addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv];
  1222 *}
  1223 
  1224 end