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