src/HOL/OrderedGroup.thy
author haftmann
Fri Oct 19 19:45:29 2007 +0200 (2007-10-19)
changeset 25102 db3e412c4cb1
parent 25090 4a50b958391a
child 25194 37a1743f0fc3
permissions -rw-r--r--
antisymmetry not a default intro rule any longer
     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 begin
   678 
   679 lemma add_inf_distrib_left:
   680   "a + inf b c = inf (a + b) (a + c)"
   681 apply (rule antisym)
   682 apply (simp_all add: le_infI)
   683 apply (rule add_le_imp_le_left [of "uminus a"])
   684 apply (simp only: add_assoc [symmetric], simp)
   685 apply rule
   686 apply (rule add_le_imp_le_left[of "a"], simp only: add_assoc[symmetric], simp)+
   687 done
   688 
   689 lemma add_inf_distrib_right:
   690   "inf a b + c = inf (a + c) (b + c)"
   691 proof -
   692   have "c + inf a b = inf (c+a) (c+b)" by (simp add: add_inf_distrib_left)
   693   thus ?thesis by (simp add: add_commute)
   694 qed
   695 
   696 end
   697 
   698 class lordered_ab_group_join = pordered_ab_group_add + upper_semilattice
   699 begin
   700 
   701 lemma add_sup_distrib_left:
   702   "a + sup b c = sup (a + b) (a + c)" 
   703 apply (rule antisym)
   704 apply (rule add_le_imp_le_left [of "uminus a"])
   705 apply (simp only: add_assoc[symmetric], simp)
   706 apply rule
   707 apply (rule add_le_imp_le_left [of "a"], simp only: add_assoc[symmetric], simp)+
   708 apply (rule le_supI)
   709 apply (simp_all)
   710 done
   711 
   712 lemma add_sup_distrib_right:
   713   "sup a b + c = sup (a+c) (b+c)"
   714 proof -
   715   have "c + sup a b = sup (c+a) (c+b)" by (simp add: add_sup_distrib_left)
   716   thus ?thesis by (simp add: add_commute)
   717 qed
   718 
   719 end
   720 
   721 class lordered_ab_group = pordered_ab_group_add + lattice
   722 begin
   723 
   724 subclass lordered_ab_group_meet by unfold_locales
   725 subclass lordered_ab_group_join by unfold_locales
   726 
   727 end
   728 
   729 context lordered_ab_group
   730 begin
   731 
   732 lemmas add_sup_inf_distribs = add_inf_distrib_right add_inf_distrib_left add_sup_distrib_right add_sup_distrib_left
   733 
   734 lemma inf_eq_neg_sup: "inf a b = - sup (-a) (-b)"
   735 proof (rule inf_unique)
   736   fix a b :: 'a
   737   show "- sup (-a) (-b) \<le> a"
   738     by (rule add_le_imp_le_right [of _ "sup (uminus a) (uminus b)"])
   739       (simp, simp add: add_sup_distrib_left)
   740 next
   741   fix a b :: 'a
   742   show "- sup (-a) (-b) \<le> b"
   743     by (rule add_le_imp_le_right [of _ "sup (uminus a) (uminus b)"])
   744       (simp, simp add: add_sup_distrib_left)
   745 next
   746   fix a b c :: 'a
   747   assume "a \<le> b" "a \<le> c"
   748   then show "a \<le> - sup (-b) (-c)" by (subst neg_le_iff_le [symmetric])
   749     (simp add: le_supI)
   750 qed
   751   
   752 lemma sup_eq_neg_inf: "sup a b = - inf (-a) (-b)"
   753 proof (rule sup_unique)
   754   fix a b :: 'a
   755   show "a \<le> - inf (-a) (-b)"
   756     by (rule add_le_imp_le_right [of _ "inf (uminus a) (uminus b)"])
   757       (simp, simp add: add_inf_distrib_left)
   758 next
   759   fix a b :: 'a
   760   show "b \<le> - inf (-a) (-b)"
   761     by (rule add_le_imp_le_right [of _ "inf (uminus a) (uminus b)"])
   762       (simp, simp add: add_inf_distrib_left)
   763 next
   764   fix a b c :: 'a
   765   assume "a \<le> c" "b \<le> c"
   766   then show "- inf (-a) (-b) \<le> c" by (subst neg_le_iff_le [symmetric])
   767     (simp add: le_infI)
   768 qed
   769 
   770 lemma add_eq_inf_sup: "a + b = sup a b + inf a b"
   771 proof -
   772   have "0 = - inf 0 (a-b) + inf (a-b) 0" by (simp add: inf_commute)
   773   hence "0 = sup 0 (b-a) + inf (a-b) 0" by (simp add: inf_eq_neg_sup)
   774   hence "0 = (-a + sup a b) + (inf a b + (-b))"
   775     apply (simp add: add_sup_distrib_left add_inf_distrib_right)
   776     by (simp add: diff_minus add_commute)
   777   thus ?thesis
   778     apply (simp add: compare_rls)
   779     apply (subst add_left_cancel [symmetric, of "plus a b" "plus (sup a b) (inf a b)" "uminus a"])
   780     apply (simp only: add_assoc, simp add: add_assoc[symmetric])
   781     done
   782 qed
   783 
   784 subsection {* Positive Part, Negative Part, Absolute Value *}
   785 
   786 definition
   787   nprt :: "'a \<Rightarrow> 'a" where
   788   "nprt x = inf x 0"
   789 
   790 definition
   791   pprt :: "'a \<Rightarrow> 'a" where
   792   "pprt x = sup x 0"
   793 
   794 lemma prts: "a = pprt a + nprt a"
   795   by (simp add: pprt_def nprt_def add_eq_inf_sup[symmetric])
   796 
   797 lemma zero_le_pprt[simp]: "0 \<le> pprt a"
   798   by (simp add: pprt_def)
   799 
   800 lemma nprt_le_zero[simp]: "nprt a \<le> 0"
   801   by (simp add: nprt_def)
   802 
   803 lemma le_eq_neg: "a \<le> - b \<longleftrightarrow> a + b \<le> 0" (is "?l = ?r")
   804 proof -
   805   have a: "?l \<longrightarrow> ?r"
   806     apply (auto)
   807     apply (rule add_le_imp_le_right[of _ "uminus b" _])
   808     apply (simp add: add_assoc)
   809     done
   810   have b: "?r \<longrightarrow> ?l"
   811     apply (auto)
   812     apply (rule add_le_imp_le_right[of _ "b" _])
   813     apply (simp)
   814     done
   815   from a b show ?thesis by blast
   816 qed
   817 
   818 lemma pprt_0[simp]: "pprt 0 = 0" by (simp add: pprt_def)
   819 lemma nprt_0[simp]: "nprt 0 = 0" by (simp add: nprt_def)
   820 
   821 lemma pprt_eq_id [simp, noatp]: "0 \<le> x \<Longrightarrow> pprt x = x"
   822   by (simp add: pprt_def le_iff_sup sup_ACI)
   823 
   824 lemma nprt_eq_id [simp, noatp]: "x \<le> 0 \<Longrightarrow> nprt x = x"
   825   by (simp add: nprt_def le_iff_inf inf_ACI)
   826 
   827 lemma pprt_eq_0 [simp, noatp]: "x \<le> 0 \<Longrightarrow> pprt x = 0"
   828   by (simp add: pprt_def le_iff_sup sup_ACI)
   829 
   830 lemma nprt_eq_0 [simp, noatp]: "0 \<le> x \<Longrightarrow> nprt x = 0"
   831   by (simp add: nprt_def le_iff_inf inf_ACI)
   832 
   833 lemma sup_0_imp_0: "sup a (- a) = 0 \<Longrightarrow> a = 0"
   834 proof -
   835   {
   836     fix a::'a
   837     assume hyp: "sup a (-a) = 0"
   838     hence "sup a (-a) + a = a" by (simp)
   839     hence "sup (a+a) 0 = a" by (simp add: add_sup_distrib_right) 
   840     hence "sup (a+a) 0 <= a" by (simp)
   841     hence "0 <= a" by (blast intro: order_trans inf_sup_ord)
   842   }
   843   note p = this
   844   assume hyp:"sup a (-a) = 0"
   845   hence hyp2:"sup (-a) (-(-a)) = 0" by (simp add: sup_commute)
   846   from p[OF hyp] p[OF hyp2] show "a = 0" by simp
   847 qed
   848 
   849 lemma inf_0_imp_0: "inf a (-a) = 0 \<Longrightarrow> a = 0"
   850 apply (simp add: inf_eq_neg_sup)
   851 apply (simp add: sup_commute)
   852 apply (erule sup_0_imp_0)
   853 done
   854 
   855 lemma inf_0_eq_0 [simp, noatp]: "inf a (- a) = 0 \<longleftrightarrow> a = 0"
   856   by (rule, erule inf_0_imp_0) simp
   857 
   858 lemma sup_0_eq_0 [simp, noatp]: "sup a (- a) = 0 \<longleftrightarrow> a = 0"
   859   by (rule, erule sup_0_imp_0) simp
   860 
   861 lemma zero_le_double_add_iff_zero_le_single_add [simp]:
   862   "0 \<le> a + a \<longleftrightarrow> 0 \<le> a"
   863 proof
   864   assume "0 <= a + a"
   865   hence a:"inf (a+a) 0 = 0" by (simp add: le_iff_inf inf_commute)
   866   have "(inf a 0)+(inf a 0) = inf (inf (a+a) 0) a" (is "?l=_")
   867     by (simp add: add_sup_inf_distribs inf_ACI)
   868   hence "?l = 0 + inf a 0" by (simp add: a, simp add: inf_commute)
   869   hence "inf a 0 = 0" by (simp only: add_right_cancel)
   870   then show "0 <= a" by (simp add: le_iff_inf inf_commute)    
   871 next  
   872   assume a: "0 <= a"
   873   show "0 <= a + a" by (simp add: add_mono[OF a a, simplified])
   874 qed
   875 
   876 lemma double_zero: "a + a = 0 \<longleftrightarrow> a = 0"
   877 proof
   878   assume assm: "a + a = 0"
   879   then have "a + a + - a = - a" by simp
   880   then have "a + (a + - a) = - a" by (simp only: add_assoc)
   881   then have a: "- a = a" by simp (*FIXME tune proof*)
   882   show "a = 0" apply (rule antisym)
   883   apply (unfold neg_le_iff_le [symmetric, of a])
   884   unfolding a apply simp
   885   unfolding zero_le_double_add_iff_zero_le_single_add [symmetric, of a]
   886   unfolding assm unfolding le_less apply simp_all done
   887 next
   888   assume "a = 0" then show "a + a = 0" by simp
   889 qed
   890 
   891 lemma zero_less_double_add_iff_zero_less_single_add:
   892   "0 < a + a \<longleftrightarrow> 0 < a"
   893 proof (cases "a = 0")
   894   case True then show ?thesis by auto
   895 next
   896   case False then show ?thesis (*FIXME tune proof*)
   897   unfolding less_le apply simp apply rule
   898   apply clarify
   899   apply rule
   900   apply assumption
   901   apply (rule notI)
   902   unfolding double_zero [symmetric, of a] apply simp
   903   done
   904 qed
   905 
   906 lemma double_add_le_zero_iff_single_add_le_zero [simp]:
   907   "a + a \<le> 0 \<longleftrightarrow> a \<le> 0" 
   908 proof -
   909   have "a + a \<le> 0 \<longleftrightarrow> 0 \<le> - (a + a)" by (subst le_minus_iff, simp)
   910   moreover have "\<dots> \<longleftrightarrow> a \<le> 0" by (simp add: zero_le_double_add_iff_zero_le_single_add)
   911   ultimately show ?thesis by blast
   912 qed
   913 
   914 lemma double_add_less_zero_iff_single_less_zero [simp]:
   915   "a + a < 0 \<longleftrightarrow> a < 0"
   916 proof -
   917   have "a + a < 0 \<longleftrightarrow> 0 < - (a + a)" by (subst less_minus_iff, simp)
   918   moreover have "\<dots> \<longleftrightarrow> a < 0" by (simp add: zero_less_double_add_iff_zero_less_single_add)
   919   ultimately show ?thesis by blast
   920 qed
   921 
   922 end
   923 
   924 lemmas add_sup_inf_distribs = add_inf_distrib_right add_inf_distrib_left add_sup_distrib_right add_sup_distrib_left
   925 
   926 class lordered_ab_group_abs = lordered_ab_group + abs +
   927   assumes abs_lattice: "\<bar>x\<bar> = sup x (- x)"
   928 begin
   929 
   930 lemma abs_zero [simp]: "\<bar>0\<bar> = 0"
   931   by (simp add: abs_lattice)
   932 
   933 lemma abs_eq_0 [simp]: "\<bar>a\<bar> = 0 \<longleftrightarrow> a = 0"
   934   by (simp add: abs_lattice)
   935 
   936 lemma abs_0_eq [simp, noatp]: "0 = \<bar>a\<bar> \<longleftrightarrow> a = 0"
   937 proof -
   938   have "(0 = abs a) = (abs a = 0)" by (simp only: eq_ac)
   939   thus ?thesis by simp
   940 qed
   941 
   942 lemma neg_inf_eq_sup [simp]: "- inf a b = sup (-a) (-b)"
   943   by (simp add: inf_eq_neg_sup)
   944 
   945 lemma neg_sup_eq_inf [simp]: "- sup a b = inf (-a) (-b)"
   946   by (simp del: neg_inf_eq_sup add: sup_eq_neg_inf)
   947 
   948 lemma abs_ge_zero [simp]: "0 \<le> \<bar>a\<bar>"
   949 proof -
   950   have a: "a \<le> \<bar>a\<bar>" and b: "- a \<le> \<bar>a\<bar>" by (auto simp add: abs_lattice)
   951   show ?thesis by (rule add_mono [OF a b, simplified])
   952 qed
   953   
   954 lemma abs_le_zero_iff [simp]: "\<bar>a\<bar> \<le> 0 \<longleftrightarrow> a = 0" 
   955 proof
   956   assume "\<bar>a\<bar> \<le> 0"
   957   then have "\<bar>a\<bar> = 0" by (rule antisym) simp
   958   thus "a = 0" by simp
   959 next
   960   assume "a = 0"
   961   thus "\<bar>a\<bar> \<le> 0" by simp
   962 qed
   963 
   964 lemma zero_less_abs_iff [simp]: "0 < \<bar>a\<bar> \<longleftrightarrow> a \<noteq> 0"
   965   by (simp add: less_le)
   966 
   967 lemma abs_not_less_zero [simp]: "\<not> \<bar>a\<bar> < 0"
   968 proof -
   969   have a: "\<And>x y. x \<le> y \<Longrightarrow> \<not> y < x" by auto
   970   show ?thesis by (simp add: a)
   971 qed
   972 
   973 lemma abs_ge_self: "a \<le> \<bar>a\<bar>"
   974   by (auto simp add: abs_lattice)
   975 
   976 lemma abs_ge_minus_self: "- a \<le> \<bar>a\<bar>"
   977   by (auto simp add: abs_lattice)
   978 
   979 lemma abs_minus_cancel [simp]: "\<bar>-a\<bar> = \<bar>a\<bar>"
   980   by (simp add: abs_lattice sup_commute)
   981 
   982 lemma abs_idempotent [simp]: "\<bar>\<bar>a\<bar>\<bar> = \<bar>a\<bar>"
   983 apply (simp add: abs_lattice [of "abs a"])
   984 apply (subst sup_absorb1)
   985 apply (rule order_trans [of _ zero])
   986 by auto
   987 
   988 lemma abs_minus_commute: 
   989   "\<bar>a - b\<bar> = \<bar>b - a\<bar>"
   990 proof -
   991   have "\<bar>a - b\<bar> = \<bar>- (a - b)\<bar>" by (simp only: abs_minus_cancel)
   992   also have "... = \<bar>b - a\<bar>" by simp
   993   finally show ?thesis .
   994 qed
   995 
   996 lemma abs_prts: "\<bar>a\<bar> = pprt a - nprt a"
   997 proof -
   998   have "0 \<le> \<bar>a\<bar>" by simp
   999   then have "0 \<le> sup a (- a)" unfolding abs_lattice .
  1000   then have "sup (sup a (- a)) 0 = sup a (- a)" by (rule sup_absorb1)
  1001   then show ?thesis
  1002     by (simp add: add_sup_inf_distribs sup_ACI
  1003       pprt_def nprt_def diff_minus abs_lattice)
  1004 qed
  1005   
  1006 lemma zero_le_iff_zero_nprt: "0 \<le> a \<longleftrightarrow> nprt a = 0"
  1007   by (simp add: le_iff_inf nprt_def inf_commute)
  1008 
  1009 lemma le_zero_iff_zero_pprt: "a \<le> 0 \<longleftrightarrow> pprt a = 0"
  1010   by (simp add: le_iff_sup pprt_def sup_commute)
  1011 
  1012 lemma le_zero_iff_pprt_id: "0 \<le> a \<longleftrightarrow> pprt a = a"
  1013   by (simp add: le_iff_sup pprt_def sup_commute)
  1014 
  1015 lemma zero_le_iff_nprt_id: "a \<le> 0 \<longleftrightarrow> nprt a = a"
  1016 by (simp add: le_iff_inf nprt_def inf_commute)
  1017 
  1018 lemma pprt_mono [simp, noatp]: "a \<le> b \<Longrightarrow> pprt a \<le> pprt b"
  1019   by (simp add: le_iff_sup pprt_def sup_ACI sup_assoc [symmetric, of a])
  1020 
  1021 lemma nprt_mono [simp, noatp]: "a \<le> b \<Longrightarrow> nprt a \<le> nprt b"
  1022   by (simp add: le_iff_inf nprt_def inf_ACI inf_assoc [symmetric, of a])
  1023 
  1024 lemma pprt_neg: "pprt (- x) = - nprt x"
  1025   by (simp add: pprt_def nprt_def)
  1026 
  1027 lemma nprt_neg: "nprt (- x) = - pprt x"
  1028   by (simp add: pprt_def nprt_def)
  1029 
  1030 lemma abs_of_nonneg [simp]: "0 \<le> a \<Longrightarrow> \<bar>a\<bar> = a"
  1031   by (simp add: iffD1 [OF zero_le_iff_zero_nprt]
  1032     iffD1[OF le_zero_iff_pprt_id] abs_prts)
  1033 
  1034 lemma abs_of_pos: "0 < x \<Longrightarrow> \<bar>x\<bar> = x"
  1035   by (rule abs_of_nonneg, rule less_imp_le)
  1036 
  1037 lemma abs_of_nonpos [simp]: "a \<le> 0 \<Longrightarrow> \<bar>a\<bar> = - a"
  1038   by (simp add: iffD1 [OF le_zero_iff_zero_pprt]
  1039     iffD1 [OF zero_le_iff_nprt_id] abs_prts)
  1040 
  1041 lemma abs_of_neg: "x < 0 \<Longrightarrow> \<bar>x\<bar> = - x"
  1042   by (rule abs_of_nonpos, rule less_imp_le)
  1043 
  1044 lemma abs_leI: "a \<le> b \<Longrightarrow> - a \<le> b \<Longrightarrow> \<bar>a\<bar> \<le> b"
  1045   by (simp add: abs_lattice le_supI)
  1046 
  1047 lemma le_minus_self_iff: "a \<le> - a \<longleftrightarrow> a \<le> 0"
  1048 proof -
  1049   from add_le_cancel_left [of "uminus a" "plus a a" zero]
  1050   have "(a <= -a) = (a+a <= 0)" 
  1051     by (simp add: add_assoc[symmetric])
  1052   thus ?thesis by simp
  1053 qed
  1054 
  1055 lemma minus_le_self_iff: "- a \<le> a \<longleftrightarrow> 0 \<le> a"
  1056 proof -
  1057   from add_le_cancel_left [of "uminus a" zero "plus a a"]
  1058   have "(-a <= a) = (0 <= a+a)" 
  1059     by (simp add: add_assoc[symmetric])
  1060   thus ?thesis by simp
  1061 qed
  1062 
  1063 lemma abs_le_D1: "\<bar>a\<bar> \<le> b \<Longrightarrow> a \<le> b"
  1064   by (insert abs_ge_self, blast intro: order_trans)
  1065 
  1066 lemma abs_le_D2: "\<bar>a\<bar> \<le> b \<Longrightarrow> - a \<le> b"
  1067   by (insert abs_le_D1 [of "uminus a"], simp)
  1068 
  1069 lemma abs_le_iff: "\<bar>a\<bar> \<le> b \<longleftrightarrow> a \<le> b \<and> - a \<le> b"
  1070   by (blast intro: abs_leI dest: abs_le_D1 abs_le_D2)
  1071 
  1072 lemma abs_triangle_ineq: "\<bar>a + b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
  1073 proof -
  1074   have g:"abs a + abs b = sup (a+b) (sup (-a-b) (sup (-a+b) (a + (-b))))" (is "_=sup ?m ?n")
  1075     by (simp add: abs_lattice add_sup_inf_distribs sup_ACI diff_minus)
  1076   have a:"a+b <= sup ?m ?n" by (simp)
  1077   have b:"-a-b <= ?n" by (simp) 
  1078   have c:"?n <= sup ?m ?n" by (simp)
  1079   from b c have d: "-a-b <= sup ?m ?n" by(rule order_trans)
  1080   have e:"-a-b = -(a+b)" by (simp add: diff_minus)
  1081   from a d e have "abs(a+b) <= sup ?m ?n" 
  1082     by (drule_tac abs_leI, auto)
  1083   with g[symmetric] show ?thesis by simp
  1084 qed
  1085 
  1086 lemma abs_triangle_ineq2: "\<bar>a\<bar> - \<bar>b\<bar> \<le> \<bar>a - b\<bar>"
  1087   apply (simp add: compare_rls)
  1088   apply (subgoal_tac "abs a = abs (plus (minus a b) b)")
  1089   apply (erule ssubst)
  1090   apply (rule abs_triangle_ineq)
  1091   apply (rule arg_cong) back
  1092   apply (simp add: compare_rls)
  1093 done
  1094 
  1095 lemma abs_triangle_ineq3: "\<bar>\<bar>a\<bar> - \<bar>b\<bar>\<bar> \<le> \<bar>a - b\<bar>"
  1096   apply (subst abs_le_iff)
  1097   apply auto
  1098   apply (rule abs_triangle_ineq2)
  1099   apply (subst abs_minus_commute)
  1100   apply (rule abs_triangle_ineq2)
  1101 done
  1102 
  1103 lemma abs_triangle_ineq4: "\<bar>a - b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
  1104 proof -
  1105   have "abs(a - b) = abs(a + - b)"
  1106     by (subst diff_minus, rule refl)
  1107   also have "... <= abs a + abs (- b)"
  1108     by (rule abs_triangle_ineq)
  1109   finally show ?thesis
  1110     by simp
  1111 qed
  1112 
  1113 lemma abs_diff_triangle_ineq: "\<bar>a + b - (c + d)\<bar> \<le> \<bar>a - c\<bar> + \<bar>b - d\<bar>"
  1114 proof -
  1115   have "\<bar>a + b - (c+d)\<bar> = \<bar>(a-c) + (b-d)\<bar>" by (simp add: diff_minus add_ac)
  1116   also have "... \<le> \<bar>a-c\<bar> + \<bar>b-d\<bar>" by (rule abs_triangle_ineq)
  1117   finally show ?thesis .
  1118 qed
  1119 
  1120 lemma abs_add_abs[simp]:
  1121   "\<bar>\<bar>a\<bar> + \<bar>b\<bar>\<bar> = \<bar>a\<bar> + \<bar>b\<bar>" (is "?L = ?R")
  1122 proof (rule antisym)
  1123   show "?L \<ge> ?R" by(rule abs_ge_self)
  1124 next
  1125   have "?L \<le> \<bar>\<bar>a\<bar>\<bar> + \<bar>\<bar>b\<bar>\<bar>" by(rule abs_triangle_ineq)
  1126   also have "\<dots> = ?R" by simp
  1127   finally show "?L \<le> ?R" .
  1128 qed
  1129 
  1130 end
  1131 
  1132 lemma sup_eq_if:
  1133   fixes a :: "'a\<Colon>{lordered_ab_group, linorder}"
  1134   shows "sup a (- a) = (if a < 0 then - a else a)"
  1135 proof -
  1136   note add_le_cancel_right [of a a "- a", symmetric, simplified]
  1137   moreover note add_le_cancel_right [of "-a" a a, symmetric, simplified]
  1138   then show ?thesis by (auto simp: sup_max max_def)
  1139 qed
  1140 
  1141 lemma abs_if_lattice:
  1142   fixes a :: "'a\<Colon>{lordered_ab_group_abs, linorder}"
  1143   shows "\<bar>a\<bar> = (if a < 0 then - a else a)"
  1144   by auto
  1145 
  1146 
  1147 text {* Needed for abelian cancellation simprocs: *}
  1148 
  1149 lemma add_cancel_21: "((x::'a::ab_group_add) + (y + z) = y + u) = (x + z = u)"
  1150 apply (subst add_left_commute)
  1151 apply (subst add_left_cancel)
  1152 apply simp
  1153 done
  1154 
  1155 lemma add_cancel_end: "(x + (y + z) = y) = (x = - (z::'a::ab_group_add))"
  1156 apply (subst add_cancel_21[of _ _ _ 0, simplified])
  1157 apply (simp add: add_right_cancel[symmetric, of "x" "-z" "z", simplified])
  1158 done
  1159 
  1160 lemma less_eqI: "(x::'a::pordered_ab_group_add) - y = x' - y' \<Longrightarrow> (x < y) = (x' < y')"
  1161 by (simp add: less_iff_diff_less_0[of x y] less_iff_diff_less_0[of x' y'])
  1162 
  1163 lemma le_eqI: "(x::'a::pordered_ab_group_add) - y = x' - y' \<Longrightarrow> (y <= x) = (y' <= x')"
  1164 apply (simp add: le_iff_diff_le_0[of y x] le_iff_diff_le_0[of  y' x'])
  1165 apply (simp add: neg_le_iff_le[symmetric, of "y-x" 0] neg_le_iff_le[symmetric, of "y'-x'" 0])
  1166 done
  1167 
  1168 lemma eq_eqI: "(x::'a::ab_group_add) - y = x' - y' \<Longrightarrow> (x = y) = (x' = y')"
  1169 by (simp add: eq_iff_diff_eq_0[of x y] eq_iff_diff_eq_0[of x' y'])
  1170 
  1171 lemma diff_def: "(x::'a::ab_group_add) - y == x + (-y)"
  1172 by (simp add: diff_minus)
  1173 
  1174 lemma add_minus_cancel: "(a::'a::ab_group_add) + (-a + b) = b"
  1175 by (simp add: add_assoc[symmetric])
  1176 
  1177 lemma le_add_right_mono: 
  1178   assumes 
  1179   "a <= b + (c::'a::pordered_ab_group_add)"
  1180   "c <= d"    
  1181   shows "a <= b + d"
  1182   apply (rule_tac order_trans[where y = "b+c"])
  1183   apply (simp_all add: prems)
  1184   done
  1185 
  1186 lemmas group_simps =
  1187   mult_ac
  1188   add_ac
  1189   add_diff_eq diff_add_eq diff_diff_eq diff_diff_eq2
  1190   diff_eq_eq eq_diff_eq diff_minus [symmetric] uminus_add_conv_diff
  1191   diff_less_eq less_diff_eq diff_le_eq le_diff_eq
  1192 
  1193 lemma estimate_by_abs:
  1194   "a + b <= (c::'a::lordered_ab_group_abs) \<Longrightarrow> a <= c + abs b" 
  1195 proof -
  1196   assume "a+b <= c"
  1197   hence 2: "a <= c+(-b)" by (simp add: group_simps)
  1198   have 3: "(-b) <= abs b" by (rule abs_ge_minus_self)
  1199   show ?thesis by (rule le_add_right_mono[OF 2 3])
  1200 qed
  1201 
  1202 subsection {* Tools setup *}
  1203 
  1204 lemma add_mono_thms_ordered_semiring [noatp]:
  1205   fixes i j k :: "'a\<Colon>pordered_ab_semigroup_add"
  1206   shows "i \<le> j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
  1207     and "i = j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
  1208     and "i \<le> j \<and> k = l \<Longrightarrow> i + k \<le> j + l"
  1209     and "i = j \<and> k = l \<Longrightarrow> i + k = j + l"
  1210 by (rule add_mono, clarify+)+
  1211 
  1212 lemma add_mono_thms_ordered_field [noatp]:
  1213   fixes i j k :: "'a\<Colon>pordered_cancel_ab_semigroup_add"
  1214   shows "i < j \<and> k = l \<Longrightarrow> i + k < j + l"
  1215     and "i = j \<and> k < l \<Longrightarrow> i + k < j + l"
  1216     and "i < j \<and> k \<le> l \<Longrightarrow> i + k < j + l"
  1217     and "i \<le> j \<and> k < l \<Longrightarrow> i + k < j + l"
  1218     and "i < j \<and> k < l \<Longrightarrow> i + k < j + l"
  1219 by (auto intro: add_strict_right_mono add_strict_left_mono
  1220   add_less_le_mono add_le_less_mono add_strict_mono)
  1221 
  1222 text{*Simplification of @{term "x-y < 0"}, etc.*}
  1223 lemmas diff_less_0_iff_less [simp] = less_iff_diff_less_0 [symmetric]
  1224 lemmas diff_eq_0_iff_eq [simp, noatp] = eq_iff_diff_eq_0 [symmetric]
  1225 lemmas diff_le_0_iff_le [simp] = le_iff_diff_le_0 [symmetric]
  1226 
  1227 ML {*
  1228 structure ab_group_add_cancel = Abel_Cancel(
  1229 struct
  1230 
  1231 (* term order for abelian groups *)
  1232 
  1233 fun agrp_ord (Const (a, _)) = find_index (fn a' => a = a')
  1234       [@{const_name HOL.zero}, @{const_name HOL.plus},
  1235         @{const_name HOL.uminus}, @{const_name HOL.minus}]
  1236   | agrp_ord _ = ~1;
  1237 
  1238 fun termless_agrp (a, b) = (Term.term_lpo agrp_ord (a, b) = LESS);
  1239 
  1240 local
  1241   val ac1 = mk_meta_eq @{thm add_assoc};
  1242   val ac2 = mk_meta_eq @{thm add_commute};
  1243   val ac3 = mk_meta_eq @{thm add_left_commute};
  1244   fun solve_add_ac thy _ (_ $ (Const (@{const_name HOL.plus},_) $ _ $ _) $ _) =
  1245         SOME ac1
  1246     | solve_add_ac thy _ (_ $ x $ (Const (@{const_name HOL.plus},_) $ y $ z)) =
  1247         if termless_agrp (y, x) then SOME ac3 else NONE
  1248     | solve_add_ac thy _ (_ $ x $ y) =
  1249         if termless_agrp (y, x) then SOME ac2 else NONE
  1250     | solve_add_ac thy _ _ = NONE
  1251 in
  1252   val add_ac_proc = Simplifier.simproc @{theory}
  1253     "add_ac_proc" ["x + y::'a::ab_semigroup_add"] solve_add_ac;
  1254 end;
  1255 
  1256 val cancel_ss = HOL_basic_ss settermless termless_agrp
  1257   addsimprocs [add_ac_proc] addsimps
  1258   [@{thm add_0_left}, @{thm add_0_right}, @{thm diff_def},
  1259    @{thm minus_add_distrib}, @{thm minus_minus}, @{thm minus_zero},
  1260    @{thm right_minus}, @{thm left_minus}, @{thm add_minus_cancel},
  1261    @{thm minus_add_cancel}];
  1262   
  1263 val eq_reflection = @{thm eq_reflection};
  1264   
  1265 val thy_ref = Theory.check_thy @{theory};
  1266 
  1267 val T = @{typ "'a\<Colon>ab_group_add"};
  1268 
  1269 val eqI_rules = [@{thm less_eqI}, @{thm le_eqI}, @{thm eq_eqI}];
  1270 
  1271 val dest_eqI = 
  1272   fst o HOLogic.dest_bin "op =" HOLogic.boolT o HOLogic.dest_Trueprop o concl_of;
  1273 
  1274 end);
  1275 *}
  1276 
  1277 ML_setup {*
  1278   Addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv];
  1279 *}
  1280 
  1281 end