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